ether-wake.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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 source tree.
  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. /* full usage according Donald Becker
  12. * usage: ether-wake [-i <ifname>] [-p aa:bb:cc:dd[:ee:ff]] 00:11:22:33:44:55\n"
  13. *
  14. * This program generates and transmits a Wake-On-LAN (WOL)\n"
  15. * \"Magic Packet\", used for restarting machines that have been\n"
  16. * soft-powered-down (ACPI D3-warm state).\n"
  17. * It currently generates the standard AMD Magic Packet format, with\n"
  18. * an optional password appended.\n"
  19. *
  20. * The single required parameter is the Ethernet MAC (station) address\n"
  21. * of the machine to wake or a host ID with known NSS 'ethers' entry.\n"
  22. * The MAC address may be found with the 'arp' program while the target\n"
  23. * machine is awake.\n"
  24. *
  25. * Options:\n"
  26. * -b Send wake-up packet to the broadcast address.\n"
  27. * -D Increase the debug level.\n"
  28. * -i ifname Use interface IFNAME instead of the default 'eth0'.\n"
  29. * -p <pw> Append the four or six byte password PW to the packet.\n"
  30. * A password is only required for a few adapter types.\n"
  31. * The password may be specified in ethernet hex format\n"
  32. * or dotted decimal (Internet address)\n"
  33. * -p 00:22:44:66:88:aa\n"
  34. * -p 192.168.1.1\n";
  35. *
  36. *
  37. * This program generates and transmits a Wake-On-LAN (WOL) "Magic Packet",
  38. * used for restarting machines that have been soft-powered-down
  39. * (ACPI D3-warm state). It currently generates the standard AMD Magic Packet
  40. * format, with an optional password appended.
  41. *
  42. * This software may be used and distributed according to the terms
  43. * of the GNU Public License, incorporated herein by reference.
  44. * Contact the author for use under other terms.
  45. *
  46. * This source file was originally part of the network tricks package, and
  47. * is now distributed to support the Scyld Beowulf system.
  48. * Copyright 1999-2003 Donald Becker and Scyld Computing Corporation.
  49. *
  50. * The author may be reached as becker@scyld, or C/O
  51. * Scyld Computing Corporation
  52. * 914 Bay Ridge Road, Suite 220
  53. * Annapolis MD 21403
  54. *
  55. * Notes:
  56. * On some systems dropping root capability allows the process to be
  57. * dumped, traced or debugged.
  58. * If someone traces this program, they get control of a raw socket.
  59. * Linux handles this safely, but beware when porting this program.
  60. *
  61. * An alternative to needing 'root' is using a UDP broadcast socket, however
  62. * doing so only works with adapters configured for unicast+broadcast Rx
  63. * filter. That configuration consumes more power.
  64. */
  65. //config:config ETHER_WAKE
  66. //config: bool "ether-wake (4.9 kb)"
  67. //config: default y
  68. //config: select PLATFORM_LINUX
  69. //config: help
  70. //config: Send a magic packet to wake up sleeping machines.
  71. //applet:IF_ETHER_WAKE(APPLET_ODDNAME(ether-wake, ether_wake, BB_DIR_USR_SBIN, BB_SUID_DROP, ether_wake))
  72. //kbuild:lib-$(CONFIG_ETHER_WAKE) += ether-wake.o
  73. //usage:#define ether_wake_trivial_usage
  74. //usage: "[-b] [-i IFACE] [-p aa:bb:cc:dd[:ee:ff]/a.b.c.d] MAC"
  75. //usage:#define ether_wake_full_usage "\n\n"
  76. //usage: "Send a magic packet to wake up sleeping machines.\n"
  77. //usage: "MAC must be a station address (00:11:22:33:44:55) or\n"
  78. //usage: "a hostname with a known 'ethers' entry.\n"
  79. //usage: "\n -b Broadcast the packet"
  80. //usage: "\n -i IFACE Interface to use (default eth0)"
  81. //usage: "\n -p PASSWORD Append four or six byte PASSWORD to the packet"
  82. #include "libbb.h"
  83. #include <netpacket/packet.h>
  84. #include <netinet/ether.h>
  85. #include <linux/if.h>
  86. /* Note: PF_INET, SOCK_DGRAM, IPPROTO_UDP would allow SIOCGIFHWADDR to
  87. * work as non-root, but we need SOCK_PACKET to specify the Ethernet
  88. * destination address.
  89. */
  90. #ifdef PF_PACKET
  91. # define whereto_t sockaddr_ll
  92. # define make_socket() xsocket(PF_PACKET, SOCK_RAW, 0)
  93. #else
  94. # define whereto_t sockaddr
  95. # define make_socket() xsocket(AF_INET, SOCK_PACKET, SOCK_PACKET)
  96. #endif
  97. #ifdef DEBUG
  98. # define bb_debug_msg(fmt, args...) fprintf(stderr, fmt, ## args)
  99. void bb_debug_dump_packet(unsigned char *outpack, int pktsize)
  100. {
  101. int i;
  102. printf("packet dump:\n");
  103. for (i = 0; i < pktsize; ++i) {
  104. printf("%2.2x ", outpack[i]);
  105. if (i % 20 == 19) bb_putchar('\n');
  106. }
  107. printf("\n\n");
  108. }
  109. #else
  110. # define bb_debug_msg(fmt, args...) ((void)0)
  111. # define bb_debug_dump_packet(outpack, pktsize) ((void)0)
  112. #endif
  113. /* Convert the host ID string to a MAC address.
  114. * The string may be a:
  115. * Host name
  116. * IP address string
  117. * MAC address string
  118. */
  119. static void get_dest_addr(const char *hostid, struct ether_addr *eaddr)
  120. {
  121. struct ether_addr *eap;
  122. eap = ether_aton_r(hostid, eaddr);
  123. if (eap) {
  124. bb_debug_msg("The target station address is %s\n\n", ether_ntoa(eap));
  125. #if !defined(__UCLIBC__) || UCLIBC_VERSION >= KERNEL_VERSION(0, 9, 30)
  126. } else if (ether_hostton(hostid, eaddr) == 0) {
  127. bb_debug_msg("Station address for hostname %s is %s\n\n", hostid, ether_ntoa(eaddr));
  128. #endif
  129. } else {
  130. bb_show_usage();
  131. }
  132. }
  133. #define PKT_HEADER_SIZE (20 + 16*6)
  134. static int fill_pkt_header(unsigned char *pkt, struct ether_addr *eaddr, int broadcast)
  135. {
  136. int i;
  137. unsigned char *station_addr = eaddr->ether_addr_octet;
  138. memset(pkt, 0xff, 6);
  139. if (!broadcast)
  140. memcpy(pkt, station_addr, 6);
  141. pkt += 6;
  142. memcpy(pkt, station_addr, 6); /* 6 */
  143. pkt += 6;
  144. *pkt++ = 0x08; /* 12 */ /* Or 0x0806 for ARP, 0x8035 for RARP */
  145. *pkt++ = 0x42; /* 13 */
  146. memset(pkt, 0xff, 6); /* 14 */
  147. for (i = 0; i < 16; ++i) {
  148. pkt += 6;
  149. memcpy(pkt, station_addr, 6); /* 20,26,32,... */
  150. }
  151. return PKT_HEADER_SIZE; /* length of packet */
  152. }
  153. static int get_wol_pw(const char *ethoptarg, unsigned char *wol_passwd)
  154. {
  155. unsigned passwd[6];
  156. int byte_cnt, i;
  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_simple_error_msg("can't 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. bb_debug_msg("password: %2.2x %2.2x %2.2x %2.2x (%d)\n\n",
  174. wol_passwd[0], wol_passwd[1], wol_passwd[2], wol_passwd[3],
  175. byte_cnt);
  176. return byte_cnt;
  177. }
  178. int ether_wake_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  179. int ether_wake_main(int argc UNUSED_PARAM, char **argv)
  180. {
  181. const char *ifname = "eth0";
  182. char *pass;
  183. unsigned flags;
  184. unsigned char wol_passwd[6];
  185. int wol_passwd_sz = 0;
  186. int s; /* Raw socket */
  187. int pktsize;
  188. unsigned char outpack[PKT_HEADER_SIZE + 6 /* max passwd size */ + 16 /* paranoia */];
  189. struct ether_addr eaddr;
  190. struct whereto_t whereto; /* who to wake up */
  191. /* handle misc user options */
  192. flags = getopt32(argv, "^" "bi:p:" "\0" "=1", &ifname, &pass);
  193. if (flags & 4) /* -p */
  194. wol_passwd_sz = get_wol_pw(pass, wol_passwd);
  195. flags &= 1; /* we further interested only in -b [bcast] flag */
  196. /* create the raw socket */
  197. s = make_socket();
  198. /* now that we have a raw socket we can drop root */
  199. /* xsetuid(getuid()); - but save on code size... */
  200. /* look up the dest mac address */
  201. get_dest_addr(argv[optind], &eaddr);
  202. /* fill out the header of the packet */
  203. pktsize = fill_pkt_header(outpack, &eaddr, flags /* & 1 OPT_BROADCAST */);
  204. bb_debug_dump_packet(outpack, pktsize);
  205. /* Fill in the source address, if possible. */
  206. #ifdef __linux__
  207. {
  208. struct ifreq if_hwaddr;
  209. strncpy_IFNAMSIZ(if_hwaddr.ifr_name, ifname);
  210. ioctl_or_perror_and_die(s, SIOCGIFHWADDR, &if_hwaddr, "SIOCGIFHWADDR on %s failed", ifname);
  211. memcpy(outpack+6, if_hwaddr.ifr_hwaddr.sa_data, 6);
  212. # ifdef DEBUG
  213. {
  214. unsigned char *hwaddr = if_hwaddr.ifr_hwaddr.sa_data;
  215. printf("The hardware address (SIOCGIFHWADDR) of %s is type %d "
  216. "%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x\n\n", ifname,
  217. if_hwaddr.ifr_hwaddr.sa_family, hwaddr[0], hwaddr[1],
  218. hwaddr[2], hwaddr[3], hwaddr[4], hwaddr[5]);
  219. }
  220. # endif
  221. }
  222. #endif /* __linux__ */
  223. bb_debug_dump_packet(outpack, pktsize);
  224. /* append the password if specified */
  225. if (wol_passwd_sz > 0) {
  226. memcpy(outpack+pktsize, wol_passwd, wol_passwd_sz);
  227. pktsize += wol_passwd_sz;
  228. }
  229. bb_debug_dump_packet(outpack, pktsize);
  230. /* This is necessary for broadcasts to work */
  231. if (flags /* & 1 OPT_BROADCAST */) {
  232. if (setsockopt_broadcast(s) != 0)
  233. bb_simple_perror_msg("SO_BROADCAST");
  234. }
  235. #if defined(PF_PACKET)
  236. {
  237. struct ifreq ifr;
  238. strncpy_IFNAMSIZ(ifr.ifr_name, ifname);
  239. xioctl(s, SIOCGIFINDEX, &ifr);
  240. memset(&whereto, 0, sizeof(whereto));
  241. whereto.sll_family = AF_PACKET;
  242. whereto.sll_ifindex = ifr.ifr_ifindex;
  243. /* The manual page incorrectly claims the address must be filled.
  244. We do so because the code may change to match the docs. */
  245. whereto.sll_halen = ETH_ALEN;
  246. memcpy(whereto.sll_addr, outpack, ETH_ALEN);
  247. }
  248. #else
  249. whereto.sa_family = 0;
  250. strcpy(whereto.sa_data, ifname);
  251. #endif
  252. xsendto(s, outpack, pktsize, (struct sockaddr *)&whereto, sizeof(whereto));
  253. if (ENABLE_FEATURE_CLEAN_UP)
  254. close(s);
  255. return EXIT_SUCCESS;
  256. }