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