blob: f923149b9ec253be900a9c4a800225828b348e20 [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 *
Bernhard Reutner-Fischerdac7ff12006-04-12 17:55:51 +00005 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
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
52 * Scyld Computing Corporation
53 * 914 Bay Ridge Road, Suite 220
54 * Annapolis MD 21403
55 *
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.
65*/
66
67
Mike Frysingerb662f0d2005-05-11 03:59:53 +000068#include <netpacket/packet.h>
69#include <net/ethernet.h>
Mike Frysingerb662f0d2005-05-11 03:59:53 +000070#include <netinet/ether.h>
Mike Frysingerb662f0d2005-05-11 03:59:53 +000071#include <linux/if.h>
Mike Frysingerb662f0d2005-05-11 03:59:53 +000072
73#include "busybox.h"
74
75/* Note: PF_INET, SOCK_DGRAM, IPPROTO_UDP would allow SIOCGIFHWADDR to
76 * work as non-root, but we need SOCK_PACKET to specify the Ethernet
77 * destination address.
78 */
79#ifdef PF_PACKET
80# define whereto_t sockaddr_ll
Rob Landleyd921b2e2006-08-03 15:41:12 +000081# define make_socket() xsocket(PF_PACKET, SOCK_RAW, 0)
Mike Frysingerb662f0d2005-05-11 03:59:53 +000082#else
83# define whereto_t sockaddr
Rob Landleyd921b2e2006-08-03 15:41:12 +000084# define make_socket() xsocket(AF_INET, SOCK_PACKET, SOCK_PACKET)
Mike Frysingerb662f0d2005-05-11 03:59:53 +000085#endif
86
87#ifdef DEBUG
88# define bb_debug_msg(fmt, args...) fprintf(stderr, fmt, ## args)
89void bb_debug_dump_packet(unsigned char *outpack, int pktsize)
90{
91 int i;
92 printf("packet dump:\n");
93 for (i = 0; i < pktsize; ++i) {
94 printf("%2.2x ", outpack[i]);
Denis Vlasenkoc6f188d2006-10-26 00:37:00 +000095 if (i % 20 == 19) puts("");
Mike Frysingerb662f0d2005-05-11 03:59:53 +000096 }
97 printf("\n\n");
98}
99#else
100# define bb_debug_msg(fmt, args...)
101# define bb_debug_dump_packet(outpack, pktsize)
102#endif
103
104static inline void get_dest_addr(const char *arg, struct ether_addr *eaddr);
105static inline int get_fill(unsigned char *pkt, struct ether_addr *eaddr, int broadcast);
106static inline int get_wol_pw(const char *ethoptarg, unsigned char *wol_passwd);
107
Rob Landleyaffb7a62006-08-05 00:41:39 +0000108int ether_wake_main(int argc, char *argv[])
Mike Frysingerb662f0d2005-05-11 03:59:53 +0000109{
Denis Vlasenko4c978632007-02-03 03:31:13 +0000110 const char *ifname = "eth0";
111 char *pass = NULL;
Mike Frysingerb662f0d2005-05-11 03:59:53 +0000112 unsigned long flags;
113 unsigned char wol_passwd[6];
114 int wol_passwd_sz = 0;
115
116 int s; /* Raw socket */
117 int pktsize;
118 unsigned char outpack[1000];
119
120 struct ether_addr eaddr;
121 struct whereto_t whereto; /* who to wake up */
122
123 /* handle misc user options */
Denis Vlasenko67b23e62006-10-03 21:00:06 +0000124 flags = getopt32(argc, argv, "bi:p:", &ifname, &pass);
Mike Frysingerb662f0d2005-05-11 03:59:53 +0000125 if (optind == argc)
126 bb_show_usage();
127 if (pass)
128 wol_passwd_sz = get_wol_pw(pass, wol_passwd);
129
130 /* create the raw socket */
131 s = make_socket();
Mike Frysingerb662f0d2005-05-11 03:59:53 +0000132
133 /* now that we have a raw socket we can drop root */
Rob Landleyafb94ec2006-07-16 08:06:34 +0000134 xsetuid(getuid());
Mike Frysingerb662f0d2005-05-11 03:59:53 +0000135
136 /* look up the dest mac address */
137 get_dest_addr(argv[optind], &eaddr);
138
139 /* fill out the header of the packet */
140 pktsize = get_fill(outpack, &eaddr, flags /*& 1 [OPT_BROADCAST]*/);
141
142 bb_debug_dump_packet(outpack, pktsize);
143
144 /* Fill in the source address, if possible. */
145#ifdef __linux__
146 {
147 struct ifreq if_hwaddr;
148
Denis Vlasenko229b3d22006-11-27 23:44:57 +0000149 strncpy(if_hwaddr.ifr_name, ifname, sizeof(if_hwaddr.ifr_name));
Mike Frysingerb662f0d2005-05-11 03:59:53 +0000150 if (ioctl(s, SIOCGIFHWADDR, &if_hwaddr) < 0)
151 bb_perror_msg_and_die("SIOCGIFHWADDR on %s failed", ifname);
152
153 memcpy(outpack+6, if_hwaddr.ifr_hwaddr.sa_data, 6);
154
155# ifdef DEBUG
156 {
157 unsigned char *hwaddr = if_hwaddr.ifr_hwaddr.sa_data;
158 printf("The hardware address (SIOCGIFHWADDR) of %s is type %d "
159 "%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x\n\n", ifname,
160 if_hwaddr.ifr_hwaddr.sa_family, hwaddr[0], hwaddr[1],
161 hwaddr[2], hwaddr[3], hwaddr[4], hwaddr[5]);
162 }
163# endif
164 }
165#endif /* __linux__ */
166
167 bb_debug_dump_packet(outpack, pktsize);
168
169 /* append the password if specified */
170 if (wol_passwd_sz > 0) {
171 memcpy(outpack+pktsize, wol_passwd, wol_passwd_sz);
172 pktsize += wol_passwd_sz;
173 }
174
175 bb_debug_dump_packet(outpack, pktsize);
176
177 /* This is necessary for broadcasts to work */
178 if (flags /*& 1 [OPT_BROADCAST]*/) {
Denis Vlasenko48237b02006-11-22 23:22:06 +0000179 if (setsockopt_broadcast(s) < 0)
Mike Frysingerb662f0d2005-05-11 03:59:53 +0000180 bb_perror_msg("SO_BROADCAST");
181 }
182
183#if defined(PF_PACKET)
184 {
185 struct ifreq ifr;
186 strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
187 if (ioctl(s, SIOCGIFINDEX, &ifr) == -1)
188 bb_perror_msg_and_die("SIOCGIFINDEX");
189 memset(&whereto, 0, sizeof(whereto));
190 whereto.sll_family = AF_PACKET;
191 whereto.sll_ifindex = ifr.ifr_ifindex;
192 /* The manual page incorrectly claims the address must be filled.
193 We do so because the code may change to match the docs. */
194 whereto.sll_halen = ETH_ALEN;
195 memcpy(whereto.sll_addr, outpack, ETH_ALEN);
196 }
197#else
198 whereto.sa_family = 0;
199 strcpy(whereto.sa_data, ifname);
200#endif
201
202 if (sendto(s, outpack, pktsize, 0, (struct sockaddr *)&whereto, sizeof(whereto)) < 0)
203 bb_perror_msg(bb_msg_write_error);
204
205 close(s);
206
207 return EXIT_SUCCESS;
208}
209
210/* Convert the host ID string to a MAC address.
211 * The string may be a:
212 * Host name
213 * IP address string
214 * MAC address string
215*/
216static inline void get_dest_addr(const char *hostid, struct ether_addr *eaddr)
217{
218 struct ether_addr *eap;
219
220 eap = ether_aton(hostid);
221 if (eap) {
222 *eaddr = *eap;
223 bb_debug_msg("The target station address is %s\n\n", ether_ntoa(eaddr));
Mike Frysingerd84a35f2005-07-31 22:49:12 +0000224#if !defined(__UCLIBC__)
Mike Frysingerb662f0d2005-05-11 03:59:53 +0000225 } else if (ether_hostton(hostid, eaddr) == 0) {
226 bb_debug_msg("Station address for hostname %s is %s\n\n", hostid, ether_ntoa(eaddr));
Mike Frysingerd84a35f2005-07-31 22:49:12 +0000227#endif
Mike Frysingerb662f0d2005-05-11 03:59:53 +0000228 } else
229 bb_show_usage();
230}
231
232static inline int get_fill(unsigned char *pkt, struct ether_addr *eaddr, int broadcast)
233{
234 int offset, i;
235 unsigned char *station_addr = eaddr->ether_addr_octet;
236
237 if (broadcast)
238 memset(pkt+0, 0xff, 6);
239 else
240 memcpy(pkt, station_addr, 6);
241 memcpy(pkt+6, station_addr, 6);
242 pkt[12] = 0x08; /* Or 0x0806 for ARP, 0x8035 for RARP */
243 pkt[13] = 0x42;
244 offset = 14;
245
246 memset(pkt+offset, 0xff, 6);
247 offset += 6;
248
249 for (i = 0; i < 16; ++i) {
250 memcpy(pkt+offset, station_addr, 6);
251 offset += 6;
252 }
253
254 return offset;
255}
256
257static inline int get_wol_pw(const char *ethoptarg, unsigned char *wol_passwd)
258{
259 int passwd[6];
260 int byte_cnt, i;
261
262 /* handle MAC format */
263 byte_cnt = sscanf(ethoptarg, "%2x:%2x:%2x:%2x:%2x:%2x",
264 &passwd[0], &passwd[1], &passwd[2],
265 &passwd[3], &passwd[4], &passwd[5]);
266 /* handle IP format */
267 if (byte_cnt < 4)
268 byte_cnt = sscanf(ethoptarg, "%d.%d.%d.%d",
269 &passwd[0], &passwd[1], &passwd[2], &passwd[3]);
270 if (byte_cnt < 4) {
Denis Vlasenko89f0b342006-11-18 22:04:09 +0000271 bb_error_msg("cannot read Wake-On-LAN pass");
Mike Frysingerb662f0d2005-05-11 03:59:53 +0000272 return 0;
273 }
274
275 for (i = 0; i < byte_cnt; ++i)
276 wol_passwd[i] = passwd[i];
277
278 bb_debug_msg("password: %2.2x %2.2x %2.2x %2.2x (%d)\n\n",
279 wol_passwd[0], wol_passwd[1], wol_passwd[2], wol_passwd[3],
280 byte_cnt);
281
282 return byte_cnt;
283}