1/* vi: set sw=4 ts=4: */ 2/* 3 * ether-wake.c - Send a magic packet to wake up sleeping machines. 4 * 5 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. 6 * 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 * 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" 20 * 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" 25 * 26 * 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"; 36 * 37 * 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 * 56 * 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. 61 * 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. 65*/ 66 67 68#include <netpacket/packet.h> 69#include <net/ethernet.h> 70#include <netinet/ether.h> 71#include <linux/if.h> 72 73#include "libbb.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 81# define make_socket() xsocket(PF_PACKET, SOCK_RAW, 0) 82#else 83# define whereto_t sockaddr 84# define make_socket() xsocket(AF_INET, SOCK_PACKET, SOCK_PACKET) 85#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]); 95 if (i % 20 == 19) bb_putchar('\n'); 96 } 97 printf("\n\n"); 98} 99#else 100# define bb_debug_msg(fmt, args...) ((void)0) 101# define bb_debug_dump_packet(outpack, pktsize) ((void)0) 102#endif 103 104/* Convert the host ID string to a MAC address. 105 * The string may be a: 106 * Host name 107 * IP address string 108 * MAC address string 109*/ 110static void get_dest_addr(const char *hostid, struct ether_addr *eaddr) 111{ 112 struct ether_addr *eap; 113 114 eap = ether_aton_r(hostid, eaddr); 115 if (eap) { 116 bb_debug_msg("The target station address is %s\n\n", ether_ntoa(eap)); 117#if !defined(__UCLIBC_MAJOR__) \ 118 || __UCLIBC_MAJOR__ > 0 \ 119 || __UCLIBC_MINOR__ > 9 \ 120 || (__UCLIBC_MINOR__ == 9 && __UCLIBC_SUBLEVEL__ >= 30) 121 } else if (ether_hostton(hostid, eaddr) == 0) { 122 bb_debug_msg("Station address for hostname %s is %s\n\n", hostid, ether_ntoa(eaddr)); 123#endif 124 } else { 125 bb_show_usage(); 126 } 127} 128 129static int get_fill(unsigned char *pkt, struct ether_addr *eaddr, int broadcast) 130{ 131 int i; 132 unsigned char *station_addr = eaddr->ether_addr_octet; 133 134 memset(pkt, 0xff, 6); 135 if (!broadcast) 136 memcpy(pkt, station_addr, 6); 137 pkt += 6; 138 139 memcpy(pkt, station_addr, 6); /* 6 */ 140 pkt += 6; 141 142 *pkt++ = 0x08; /* 12 */ /* Or 0x0806 for ARP, 0x8035 for RARP */ 143 *pkt++ = 0x42; /* 13 */ 144 145 memset(pkt, 0xff, 6); /* 14 */ 146 147 for (i = 0; i < 16; ++i) { 148 pkt += 6; 149 memcpy(pkt, station_addr, 6); /* 20,26,32,... */ 150 } 151 152 return 20 + 16*6; /* length of packet */ 153} 154 155static int get_wol_pw(const char *ethoptarg, unsigned char *wol_passwd) 156{ 157 unsigned passwd[6]; 158 int byte_cnt, i; 159 160 /* handle MAC format */ 161 byte_cnt = sscanf(ethoptarg, "%2x:%2x:%2x:%2x:%2x:%2x", 162 &passwd[0], &passwd[1], &passwd[2], 163 &passwd[3], &passwd[4], &passwd[5]); 164 /* handle IP format */ 165// FIXME: why < 4?? should it be < 6? 166 if (byte_cnt < 4) 167 byte_cnt = sscanf(ethoptarg, "%u.%u.%u.%u", 168 &passwd[0], &passwd[1], &passwd[2], &passwd[3]); 169 if (byte_cnt < 4) { 170 bb_error_msg("can't read Wake-On-LAN pass"); 171 return 0; 172 } 173// TODO: check invalid numbers >255?? 174 for (i = 0; i < byte_cnt; ++i) 175 wol_passwd[i] = passwd[i]; 176 177 bb_debug_msg("password: %2.2x %2.2x %2.2x %2.2x (%d)\n\n", 178 wol_passwd[0], wol_passwd[1], wol_passwd[2], wol_passwd[3], 179 byte_cnt); 180 181 return byte_cnt; 182} 183 184int ether_wake_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; 185int ether_wake_main(int argc UNUSED_PARAM, char **argv) 186{ 187 const char *ifname = "eth0"; 188 char *pass; 189 unsigned flags; 190 unsigned char wol_passwd[6]; 191 int wol_passwd_sz = 0; 192 int s; /* Raw socket */ 193 int pktsize; 194 unsigned char outpack[1000]; 195 196 struct ether_addr eaddr; 197 struct whereto_t whereto; /* who to wake up */ 198 199 /* handle misc user options */ 200 opt_complementary = "=1"; 201 flags = getopt32(argv, "bi:p:", &ifname, &pass); 202 if (flags & 4) /* -p */ 203 wol_passwd_sz = get_wol_pw(pass, wol_passwd); 204 flags &= 1; /* we further interested only in -b [bcast] flag */ 205 206 /* create the raw socket */ 207 s = make_socket(); 208 209 /* now that we have a raw socket we can drop root */ 210 /* xsetuid(getuid()); - but save on code size... */ 211 212 /* look up the dest mac address */ 213 get_dest_addr(argv[optind], &eaddr); 214 215 /* fill out the header of the packet */ 216 pktsize = get_fill(outpack, &eaddr, flags /* & 1 OPT_BROADCAST */); 217 218 bb_debug_dump_packet(outpack, pktsize); 219 220 /* Fill in the source address, if possible. */ 221#ifdef __linux__ 222 { 223 struct ifreq if_hwaddr; 224 225 strncpy_IFNAMSIZ(if_hwaddr.ifr_name, ifname); 226 ioctl_or_perror_and_die(s, SIOCGIFHWADDR, &if_hwaddr, "SIOCGIFHWADDR on %s failed", ifname); 227 228 memcpy(outpack+6, if_hwaddr.ifr_hwaddr.sa_data, 6); 229 230# ifdef DEBUG 231 { 232 unsigned char *hwaddr = if_hwaddr.ifr_hwaddr.sa_data; 233 printf("The hardware address (SIOCGIFHWADDR) of %s is type %d " 234 "%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x\n\n", ifname, 235 if_hwaddr.ifr_hwaddr.sa_family, hwaddr[0], hwaddr[1], 236 hwaddr[2], hwaddr[3], hwaddr[4], hwaddr[5]); 237 } 238# endif 239 } 240#endif /* __linux__ */ 241 242 bb_debug_dump_packet(outpack, pktsize); 243 244 /* append the password if specified */ 245 if (wol_passwd_sz > 0) { 246 memcpy(outpack+pktsize, wol_passwd, wol_passwd_sz); 247 pktsize += wol_passwd_sz; 248 } 249 250 bb_debug_dump_packet(outpack, pktsize); 251 252 /* This is necessary for broadcasts to work */ 253 if (flags /* & 1 OPT_BROADCAST */) { 254 if (setsockopt_broadcast(s) != 0) 255 bb_perror_msg("SO_BROADCAST"); 256 } 257 258#if defined(PF_PACKET) 259 { 260 struct ifreq ifr; 261 strncpy_IFNAMSIZ(ifr.ifr_name, ifname); 262 xioctl(s, SIOCGIFINDEX, &ifr); 263 memset(&whereto, 0, sizeof(whereto)); 264 whereto.sll_family = AF_PACKET; 265 whereto.sll_ifindex = ifr.ifr_ifindex; 266 /* The manual page incorrectly claims the address must be filled. 267 We do so because the code may change to match the docs. */ 268 whereto.sll_halen = ETH_ALEN; 269 memcpy(whereto.sll_addr, outpack, ETH_ALEN); 270 } 271#else 272 whereto.sa_family = 0; 273 strcpy(whereto.sa_data, ifname); 274#endif 275 xsendto(s, outpack, pktsize, (struct sockaddr *)&whereto, sizeof(whereto)); 276 if (ENABLE_FEATURE_CLEAN_UP) 277 close(s); 278 return EXIT_SUCCESS; 279} 280