blob: c38547dda422b412eba7866bebfaa888bfec3c05 [file] [log] [blame]
Bernhard Reutner-Fischerdac7ff12006-04-12 17:55:51 +00001/* vi: set sw=4 ts=4: */
Mike Frysingerb662f0d2005-05-11 03:59:53 +00002/*
3 * ether-wake.c - Send a magic packet to wake up sleeping machines.
4 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02005 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Mike Frysingerb662f0d2005-05-11 03:59:53 +00006 *
7 * Author: Donald Becker, http://www.scyld.com/"; http://www.scyld.com/wakeonlan.html
8 * Busybox port: Christian Volkmann <haveaniceday@online.de>
9 * Used version of ether-wake.c: v1.09 11/12/2003 Donald Becker, http://www.scyld.com/";
10 */
11
12/* full usage according Donald Becker
13 * usage: ether-wake [-i <ifname>] [-p aa:bb:cc:dd[:ee:ff]] 00:11:22:33:44:55\n"
14 *
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000015 * This program generates and transmits a Wake-On-LAN (WOL)\n"
16 * \"Magic Packet\", used for restarting machines that have been\n"
17 * soft-powered-down (ACPI D3-warm state).\n"
18 * It currently generates the standard AMD Magic Packet format, with\n"
19 * an optional password appended.\n"
Mike Frysingerb662f0d2005-05-11 03:59:53 +000020 *
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000021 * The single required parameter is the Ethernet MAC (station) address\n"
22 * of the machine to wake or a host ID with known NSS 'ethers' entry.\n"
23 * The MAC address may be found with the 'arp' program while the target\n"
24 * machine is awake.\n"
Mike Frysingerb662f0d2005-05-11 03:59:53 +000025 *
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000026 * Options:\n"
27 * -b Send wake-up packet to the broadcast address.\n"
28 * -D Increase the debug level.\n"
29 * -i ifname Use interface IFNAME instead of the default 'eth0'.\n"
30 * -p <pw> Append the four or six byte password PW to the packet.\n"
31 * A password is only required for a few adapter types.\n"
32 * The password may be specified in ethernet hex format\n"
33 * or dotted decimal (Internet address)\n"
34 * -p 00:22:44:66:88:aa\n"
35 * -p 192.168.1.1\n";
Mike Frysingerb662f0d2005-05-11 03:59:53 +000036 *
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000037 *
38 * This program generates and transmits a Wake-On-LAN (WOL) "Magic Packet",
39 * used for restarting machines that have been soft-powered-down
40 * (ACPI D3-warm state). It currently generates the standard AMD Magic Packet
41 * format, with an optional password appended.
42 *
43 * This software may be used and distributed according to the terms
44 * of the GNU Public License, incorporated herein by reference.
45 * Contact the author for use under other terms.
46 *
47 * This source file was originally part of the network tricks package, and
48 * is now distributed to support the Scyld Beowulf system.
49 * Copyright 1999-2003 Donald Becker and Scyld Computing Corporation.
50 *
51 * The author may be reached as becker@scyld, or C/O
Denys Vlasenko60cb48c2013-01-14 15:57:44 +010052 * Scyld Computing Corporation
53 * 914 Bay Ridge Road, Suite 220
54 * Annapolis MD 21403
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000055 *
Mike Frysingerb662f0d2005-05-11 03:59:53 +000056 * Notes:
57 * On some systems dropping root capability allows the process to be
58 * dumped, traced or debugged.
59 * If someone traces this program, they get control of a raw socket.
60 * Linux handles this safely, but beware when porting this program.
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000061 *
Mike Frysingerb662f0d2005-05-11 03:59:53 +000062 * An alternative to needing 'root' is using a UDP broadcast socket, however
63 * doing so only works with adapters configured for unicast+broadcast Rx
64 * filter. That configuration consumes more power.
Denys Vlasenkoeba5fae2013-10-14 19:04:20 +020065 */
Mike Frysingerb662f0d2005-05-11 03:59:53 +000066
Pere Orga5bc8c002011-04-11 03:29:49 +020067//usage:#define ether_wake_trivial_usage
Denys Vlasenkoeba5fae2013-10-14 19:04:20 +020068//usage: "[-b] [-i IFACE] [-p aa:bb:cc:dd[:ee:ff]/a.b.c.d] MAC"
Pere Orga5bc8c002011-04-11 03:29:49 +020069//usage:#define ether_wake_full_usage "\n\n"
70//usage: "Send a magic packet to wake up sleeping machines.\n"
71//usage: "MAC must be a station address (00:11:22:33:44:55) or\n"
72//usage: "a hostname with a known 'ethers' entry.\n"
Denys Vlasenkoeba5fae2013-10-14 19:04:20 +020073//usage: "\n -b Broadcast the packet"
74//usage: "\n -i IFACE Interface to use (default eth0)"
75//usage: "\n -p PASSWORD Append four or six byte PASSWORD to the packet"
Mike Frysingerb662f0d2005-05-11 03:59:53 +000076
Dan Fandrichf533ec82011-06-10 05:17:59 +020077#include "libbb.h"
Mike Frysingerb662f0d2005-05-11 03:59:53 +000078#include <netpacket/packet.h>
Mike Frysingerb662f0d2005-05-11 03:59:53 +000079#include <netinet/ether.h>
Mike Frysingerb662f0d2005-05-11 03:59:53 +000080#include <linux/if.h>
Mike Frysingerb662f0d2005-05-11 03:59:53 +000081
Mike Frysingerb662f0d2005-05-11 03:59:53 +000082/* Note: PF_INET, SOCK_DGRAM, IPPROTO_UDP would allow SIOCGIFHWADDR to
83 * work as non-root, but we need SOCK_PACKET to specify the Ethernet
84 * destination address.
85 */
86#ifdef PF_PACKET
87# define whereto_t sockaddr_ll
Rob Landleyd921b2e2006-08-03 15:41:12 +000088# define make_socket() xsocket(PF_PACKET, SOCK_RAW, 0)
Mike Frysingerb662f0d2005-05-11 03:59:53 +000089#else
90# define whereto_t sockaddr
Rob Landleyd921b2e2006-08-03 15:41:12 +000091# define make_socket() xsocket(AF_INET, SOCK_PACKET, SOCK_PACKET)
Mike Frysingerb662f0d2005-05-11 03:59:53 +000092#endif
93
94#ifdef DEBUG
95# define bb_debug_msg(fmt, args...) fprintf(stderr, fmt, ## args)
96void bb_debug_dump_packet(unsigned char *outpack, int pktsize)
97{
98 int i;
99 printf("packet dump:\n");
100 for (i = 0; i < pktsize; ++i) {
101 printf("%2.2x ", outpack[i]);
Denis Vlasenko4daad902007-09-27 10:20:47 +0000102 if (i % 20 == 19) bb_putchar('\n');
Mike Frysingerb662f0d2005-05-11 03:59:53 +0000103 }
104 printf("\n\n");
105}
106#else
Denis Vlasenko198714c2007-04-16 23:16:37 +0000107# define bb_debug_msg(fmt, args...) ((void)0)
108# define bb_debug_dump_packet(outpack, pktsize) ((void)0)
Mike Frysingerb662f0d2005-05-11 03:59:53 +0000109#endif
110
Denis Vlasenko198714c2007-04-16 23:16:37 +0000111/* Convert the host ID string to a MAC address.
112 * The string may be a:
113 * Host name
114 * IP address string
115 * MAC address string
Denys Vlasenko60cb48c2013-01-14 15:57:44 +0100116 */
Denis Vlasenko198714c2007-04-16 23:16:37 +0000117static void get_dest_addr(const char *hostid, struct ether_addr *eaddr)
118{
119 struct ether_addr *eap;
120
Denys Vlasenko37a658c2010-03-23 15:43:08 +0100121 eap = ether_aton_r(hostid, eaddr);
Denis Vlasenko198714c2007-04-16 23:16:37 +0000122 if (eap) {
Denys Vlasenko37a658c2010-03-23 15:43:08 +0100123 bb_debug_msg("The target station address is %s\n\n", ether_ntoa(eap));
Mike Frysinger445e7542013-03-12 11:13:22 -0400124#if !defined(__UCLIBC__) || UCLIBC_VERSION >= KERNEL_VERSION(0, 9, 30)
Denis Vlasenko198714c2007-04-16 23:16:37 +0000125 } else if (ether_hostton(hostid, eaddr) == 0) {
126 bb_debug_msg("Station address for hostname %s is %s\n\n", hostid, ether_ntoa(eaddr));
127#endif
Denys Vlasenko37a658c2010-03-23 15:43:08 +0100128 } else {
Denis Vlasenko198714c2007-04-16 23:16:37 +0000129 bb_show_usage();
Denys Vlasenko37a658c2010-03-23 15:43:08 +0100130 }
Denis Vlasenko198714c2007-04-16 23:16:37 +0000131}
132
Denys Vlasenkoeba5fae2013-10-14 19:04:20 +0200133#define PKT_HEADER_SIZE (20 + 16*6)
134static int fill_pkt_header(unsigned char *pkt, struct ether_addr *eaddr, int broadcast)
Denis Vlasenko198714c2007-04-16 23:16:37 +0000135{
136 int i;
137 unsigned char *station_addr = eaddr->ether_addr_octet;
138
139 memset(pkt, 0xff, 6);
140 if (!broadcast)
141 memcpy(pkt, station_addr, 6);
142 pkt += 6;
143
144 memcpy(pkt, station_addr, 6); /* 6 */
145 pkt += 6;
146
147 *pkt++ = 0x08; /* 12 */ /* Or 0x0806 for ARP, 0x8035 for RARP */
148 *pkt++ = 0x42; /* 13 */
149
150 memset(pkt, 0xff, 6); /* 14 */
151
152 for (i = 0; i < 16; ++i) {
153 pkt += 6;
154 memcpy(pkt, station_addr, 6); /* 20,26,32,... */
155 }
156
Denys Vlasenkoeba5fae2013-10-14 19:04:20 +0200157 return PKT_HEADER_SIZE; /* length of packet */
Denis Vlasenko198714c2007-04-16 23:16:37 +0000158}
159
160static int get_wol_pw(const char *ethoptarg, unsigned char *wol_passwd)
161{
162 unsigned passwd[6];
163 int byte_cnt, i;
164
165 /* handle MAC format */
166 byte_cnt = sscanf(ethoptarg, "%2x:%2x:%2x:%2x:%2x:%2x",
167 &passwd[0], &passwd[1], &passwd[2],
168 &passwd[3], &passwd[4], &passwd[5]);
169 /* handle IP format */
170// FIXME: why < 4?? should it be < 6?
171 if (byte_cnt < 4)
172 byte_cnt = sscanf(ethoptarg, "%u.%u.%u.%u",
173 &passwd[0], &passwd[1], &passwd[2], &passwd[3]);
174 if (byte_cnt < 4) {
Denys Vlasenko6331cf02009-11-13 09:08:27 +0100175 bb_error_msg("can't read Wake-On-LAN pass");
Denis Vlasenko198714c2007-04-16 23:16:37 +0000176 return 0;
177 }
178// TODO: check invalid numbers >255??
179 for (i = 0; i < byte_cnt; ++i)
180 wol_passwd[i] = passwd[i];
181
182 bb_debug_msg("password: %2.2x %2.2x %2.2x %2.2x (%d)\n\n",
183 wol_passwd[0], wol_passwd[1], wol_passwd[2], wol_passwd[3],
184 byte_cnt);
185
186 return byte_cnt;
187}
Mike Frysingerb662f0d2005-05-11 03:59:53 +0000188
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000189int ether_wake_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000190int ether_wake_main(int argc UNUSED_PARAM, char **argv)
Mike Frysingerb662f0d2005-05-11 03:59:53 +0000191{
Denis Vlasenko4c978632007-02-03 03:31:13 +0000192 const char *ifname = "eth0";
Denis Vlasenko517d1aa2007-04-16 23:23:33 +0000193 char *pass;
Denis Vlasenko198714c2007-04-16 23:16:37 +0000194 unsigned flags;
Mike Frysingerb662f0d2005-05-11 03:59:53 +0000195 unsigned char wol_passwd[6];
196 int wol_passwd_sz = 0;
Denys Vlasenkoe4dcba12010-10-28 18:57:19 +0200197 int s; /* Raw socket */
Mike Frysingerb662f0d2005-05-11 03:59:53 +0000198 int pktsize;
Denys Vlasenkoeba5fae2013-10-14 19:04:20 +0200199 unsigned char outpack[PKT_HEADER_SIZE + 6 /* max passwd size */ + 16 /* paranoia */];
Mike Frysingerb662f0d2005-05-11 03:59:53 +0000200
201 struct ether_addr eaddr;
Denys Vlasenkoe4dcba12010-10-28 18:57:19 +0200202 struct whereto_t whereto; /* who to wake up */
Mike Frysingerb662f0d2005-05-11 03:59:53 +0000203
204 /* handle misc user options */
Denis Vlasenko517d1aa2007-04-16 23:23:33 +0000205 opt_complementary = "=1";
Denis Vlasenkofe7cd642007-08-18 15:32:12 +0000206 flags = getopt32(argv, "bi:p:", &ifname, &pass);
Denis Vlasenko517d1aa2007-04-16 23:23:33 +0000207 if (flags & 4) /* -p */
Mike Frysingerb662f0d2005-05-11 03:59:53 +0000208 wol_passwd_sz = get_wol_pw(pass, wol_passwd);
Denis Vlasenko517d1aa2007-04-16 23:23:33 +0000209 flags &= 1; /* we further interested only in -b [bcast] flag */
Mike Frysingerb662f0d2005-05-11 03:59:53 +0000210
211 /* create the raw socket */
212 s = make_socket();
Mike Frysingerb662f0d2005-05-11 03:59:53 +0000213
214 /* now that we have a raw socket we can drop root */
Denis Vlasenko517d1aa2007-04-16 23:23:33 +0000215 /* xsetuid(getuid()); - but save on code size... */
Mike Frysingerb662f0d2005-05-11 03:59:53 +0000216
217 /* look up the dest mac address */
218 get_dest_addr(argv[optind], &eaddr);
219
220 /* fill out the header of the packet */
Denys Vlasenkoeba5fae2013-10-14 19:04:20 +0200221 pktsize = fill_pkt_header(outpack, &eaddr, flags /* & 1 OPT_BROADCAST */);
Mike Frysingerb662f0d2005-05-11 03:59:53 +0000222
223 bb_debug_dump_packet(outpack, pktsize);
224
225 /* Fill in the source address, if possible. */
226#ifdef __linux__
227 {
228 struct ifreq if_hwaddr;
229
Denis Vlasenko360d9662008-12-02 18:18:50 +0000230 strncpy_IFNAMSIZ(if_hwaddr.ifr_name, ifname);
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +0000231 ioctl_or_perror_and_die(s, SIOCGIFHWADDR, &if_hwaddr, "SIOCGIFHWADDR on %s failed", ifname);
Mike Frysingerb662f0d2005-05-11 03:59:53 +0000232
233 memcpy(outpack+6, if_hwaddr.ifr_hwaddr.sa_data, 6);
234
235# ifdef DEBUG
236 {
237 unsigned char *hwaddr = if_hwaddr.ifr_hwaddr.sa_data;
238 printf("The hardware address (SIOCGIFHWADDR) of %s is type %d "
Denys Vlasenko69675782013-01-14 01:34:48 +0100239 "%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x\n\n", ifname,
240 if_hwaddr.ifr_hwaddr.sa_family, hwaddr[0], hwaddr[1],
241 hwaddr[2], hwaddr[3], hwaddr[4], hwaddr[5]);
Mike Frysingerb662f0d2005-05-11 03:59:53 +0000242 }
243# endif
244 }
245#endif /* __linux__ */
246
247 bb_debug_dump_packet(outpack, pktsize);
248
249 /* append the password if specified */
250 if (wol_passwd_sz > 0) {
251 memcpy(outpack+pktsize, wol_passwd, wol_passwd_sz);
252 pktsize += wol_passwd_sz;
253 }
254
255 bb_debug_dump_packet(outpack, pktsize);
256
257 /* This is necessary for broadcasts to work */
Denis Vlasenko517d1aa2007-04-16 23:23:33 +0000258 if (flags /* & 1 OPT_BROADCAST */) {
Denis Vlasenko198714c2007-04-16 23:16:37 +0000259 if (setsockopt_broadcast(s) != 0)
Mike Frysingerb662f0d2005-05-11 03:59:53 +0000260 bb_perror_msg("SO_BROADCAST");
261 }
262
263#if defined(PF_PACKET)
264 {
265 struct ifreq ifr;
Denis Vlasenko360d9662008-12-02 18:18:50 +0000266 strncpy_IFNAMSIZ(ifr.ifr_name, ifname);
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +0000267 xioctl(s, SIOCGIFINDEX, &ifr);
Mike Frysingerb662f0d2005-05-11 03:59:53 +0000268 memset(&whereto, 0, sizeof(whereto));
269 whereto.sll_family = AF_PACKET;
270 whereto.sll_ifindex = ifr.ifr_ifindex;
271 /* The manual page incorrectly claims the address must be filled.
272 We do so because the code may change to match the docs. */
273 whereto.sll_halen = ETH_ALEN;
274 memcpy(whereto.sll_addr, outpack, ETH_ALEN);
275 }
276#else
277 whereto.sa_family = 0;
278 strcpy(whereto.sa_data, ifname);
279#endif
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000280 xsendto(s, outpack, pktsize, (struct sockaddr *)&whereto, sizeof(whereto));
Denis Vlasenko198714c2007-04-16 23:16:37 +0000281 if (ENABLE_FEATURE_CLEAN_UP)
282 close(s);
Mike Frysingerb662f0d2005-05-11 03:59:53 +0000283 return EXIT_SUCCESS;
284}