1 /* vi: set sw=4 ts=4: */
3 * ether-wake.c - Send a magic packet to wake up sleeping machines.
5 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
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/";
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"
15 * 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"
21 * 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"
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"
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.
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.
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.
51 * The author may be reached as becker@scyld, or C/O
52 * Scyld Computing Corporation
53 * 914 Bay Ridge Road, Suite 220
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.
62 * 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.
67 //usage:#define ether_wake_trivial_usage
68 //usage: "[-b] [-i iface] [-p aa:bb:cc:dd[:ee:ff]] MAC"
69 //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"
73 //usage: "\n -b Send wake-up packet to the broadcast address"
74 //usage: "\n -i iface Interface to use (default eth0)"
75 //usage: "\n -p pass Append four or six byte password PW to the packet"
77 #include <netpacket/packet.h>
78 #include <net/ethernet.h>
79 #include <netinet/ether.h>
84 /* Note: PF_INET, SOCK_DGRAM, IPPROTO_UDP would allow SIOCGIFHWADDR to
85 * work as non-root, but we need SOCK_PACKET to specify the Ethernet
86 * destination address.
89 # define whereto_t sockaddr_ll
90 # define make_socket() xsocket(PF_PACKET, SOCK_RAW, 0)
92 # define whereto_t sockaddr
93 # define make_socket() xsocket(AF_INET, SOCK_PACKET, SOCK_PACKET)
97 # define bb_debug_msg(fmt, args...) fprintf(stderr, fmt, ## args)
98 void bb_debug_dump_packet(unsigned char *outpack, int pktsize)
101 printf("packet dump:\n");
102 for (i = 0; i < pktsize; ++i) {
103 printf("%2.2x ", outpack[i]);
104 if (i % 20 == 19) bb_putchar('\n');
109 # define bb_debug_msg(fmt, args...) ((void)0)
110 # define bb_debug_dump_packet(outpack, pktsize) ((void)0)
113 /* Convert the host ID string to a MAC address.
114 * The string may be a:
119 static void get_dest_addr(const char *hostid, struct ether_addr *eaddr)
121 struct ether_addr *eap;
123 eap = ether_aton_r(hostid, eaddr);
125 bb_debug_msg("The target station address is %s\n\n", ether_ntoa(eap));
126 #if !defined(__UCLIBC_MAJOR__) \
127 || __UCLIBC_MAJOR__ > 0 \
128 || __UCLIBC_MINOR__ > 9 \
129 || (__UCLIBC_MINOR__ == 9 && __UCLIBC_SUBLEVEL__ >= 30)
130 } else if (ether_hostton(hostid, eaddr) == 0) {
131 bb_debug_msg("Station address for hostname %s is %s\n\n", hostid, ether_ntoa(eaddr));
138 static int get_fill(unsigned char *pkt, struct ether_addr *eaddr, int broadcast)
141 unsigned char *station_addr = eaddr->ether_addr_octet;
143 memset(pkt, 0xff, 6);
145 memcpy(pkt, station_addr, 6);
148 memcpy(pkt, station_addr, 6); /* 6 */
151 *pkt++ = 0x08; /* 12 */ /* Or 0x0806 for ARP, 0x8035 for RARP */
152 *pkt++ = 0x42; /* 13 */
154 memset(pkt, 0xff, 6); /* 14 */
156 for (i = 0; i < 16; ++i) {
158 memcpy(pkt, station_addr, 6); /* 20,26,32,... */
161 return 20 + 16*6; /* length of packet */
164 static int get_wol_pw(const char *ethoptarg, unsigned char *wol_passwd)
169 /* handle MAC format */
170 byte_cnt = sscanf(ethoptarg, "%2x:%2x:%2x:%2x:%2x:%2x",
171 &passwd[0], &passwd[1], &passwd[2],
172 &passwd[3], &passwd[4], &passwd[5]);
173 /* handle IP format */
174 // FIXME: why < 4?? should it be < 6?
176 byte_cnt = sscanf(ethoptarg, "%u.%u.%u.%u",
177 &passwd[0], &passwd[1], &passwd[2], &passwd[3]);
179 bb_error_msg("can't read Wake-On-LAN pass");
182 // TODO: check invalid numbers >255??
183 for (i = 0; i < byte_cnt; ++i)
184 wol_passwd[i] = passwd[i];
186 bb_debug_msg("password: %2.2x %2.2x %2.2x %2.2x (%d)\n\n",
187 wol_passwd[0], wol_passwd[1], wol_passwd[2], wol_passwd[3],
193 int ether_wake_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
194 int ether_wake_main(int argc UNUSED_PARAM, char **argv)
196 const char *ifname = "eth0";
199 unsigned char wol_passwd[6];
200 int wol_passwd_sz = 0;
201 int s; /* Raw socket */
203 unsigned char outpack[1000];
205 struct ether_addr eaddr;
206 struct whereto_t whereto; /* who to wake up */
208 /* handle misc user options */
209 opt_complementary = "=1";
210 flags = getopt32(argv, "bi:p:", &ifname, &pass);
211 if (flags & 4) /* -p */
212 wol_passwd_sz = get_wol_pw(pass, wol_passwd);
213 flags &= 1; /* we further interested only in -b [bcast] flag */
215 /* create the raw socket */
218 /* now that we have a raw socket we can drop root */
219 /* xsetuid(getuid()); - but save on code size... */
221 /* look up the dest mac address */
222 get_dest_addr(argv[optind], &eaddr);
224 /* fill out the header of the packet */
225 pktsize = get_fill(outpack, &eaddr, flags /* & 1 OPT_BROADCAST */);
227 bb_debug_dump_packet(outpack, pktsize);
229 /* Fill in the source address, if possible. */
232 struct ifreq if_hwaddr;
234 strncpy_IFNAMSIZ(if_hwaddr.ifr_name, ifname);
235 ioctl_or_perror_and_die(s, SIOCGIFHWADDR, &if_hwaddr, "SIOCGIFHWADDR on %s failed", ifname);
237 memcpy(outpack+6, if_hwaddr.ifr_hwaddr.sa_data, 6);
241 unsigned char *hwaddr = if_hwaddr.ifr_hwaddr.sa_data;
242 printf("The hardware address (SIOCGIFHWADDR) of %s is type %d "
243 "%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x\n\n", ifname,
244 if_hwaddr.ifr_hwaddr.sa_family, hwaddr[0], hwaddr[1],
245 hwaddr[2], hwaddr[3], hwaddr[4], hwaddr[5]);
249 #endif /* __linux__ */
251 bb_debug_dump_packet(outpack, pktsize);
253 /* append the password if specified */
254 if (wol_passwd_sz > 0) {
255 memcpy(outpack+pktsize, wol_passwd, wol_passwd_sz);
256 pktsize += wol_passwd_sz;
259 bb_debug_dump_packet(outpack, pktsize);
261 /* This is necessary for broadcasts to work */
262 if (flags /* & 1 OPT_BROADCAST */) {
263 if (setsockopt_broadcast(s) != 0)
264 bb_perror_msg("SO_BROADCAST");
267 #if defined(PF_PACKET)
270 strncpy_IFNAMSIZ(ifr.ifr_name, ifname);
271 xioctl(s, SIOCGIFINDEX, &ifr);
272 memset(&whereto, 0, sizeof(whereto));
273 whereto.sll_family = AF_PACKET;
274 whereto.sll_ifindex = ifr.ifr_ifindex;
275 /* The manual page incorrectly claims the address must be filled.
276 We do so because the code may change to match the docs. */
277 whereto.sll_halen = ETH_ALEN;
278 memcpy(whereto.sll_addr, outpack, ETH_ALEN);
281 whereto.sa_family = 0;
282 strcpy(whereto.sa_data, ifname);
284 xsendto(s, outpack, pktsize, (struct sockaddr *)&whereto, sizeof(whereto));
285 if (ENABLE_FEATURE_CLEAN_UP)