ether-wake.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. /* ether-wake.c: Send a magic packet to wake up sleeping machines. */
  2. static char version_msg[] =
  3. "ether-wake.c: v1.09 11/12/2003 Donald Becker, http://www.scyld.com/";
  4. static char brief_usage_msg[] =
  5. "usage: ether-wake [-i <ifname>] [-p aa:bb:cc:dd[:ee:ff]] 00:11:22:33:44:55\n"
  6. " Use '-u' to see the complete set of options.\n";
  7. static char usage_msg[] =
  8. "usage: ether-wake [-i <ifname>] [-p aa:bb:cc:dd[:ee:ff]] 00:11:22:33:44:55\n"
  9. "\n"
  10. " This program generates and transmits a Wake-On-LAN (WOL)\n"
  11. " \"Magic Packet\", used for restarting machines that have been\n"
  12. " soft-powered-down (ACPI D3-warm state).\n"
  13. " It currently generates the standard AMD Magic Packet format, with\n"
  14. " an optional password appended.\n"
  15. "\n"
  16. " The single required parameter is the Ethernet MAC (station) address\n"
  17. " of the machine to wake.\n"
  18. " The MAC address may be found with the 'arp' program while the target\n"
  19. " machine is awake.\n"
  20. "\n"
  21. " Options:\n"
  22. " -b Send wake-up packet to the broadcast address.\n"
  23. " -D Increase the debug level.\n"
  24. " -i ifname Use interface IFNAME instead of the default 'eth0'.\n"
  25. " -p <pw> Append the four or six byte password PW to the packet.\n"
  26. " A password is only required for a few adapter types.\n"
  27. " The password may be specified in ethernet hex format\n"
  28. " or dotted decimal (Internet address)\n"
  29. " -p 00:22:44:66:88:aa\n"
  30. " -p 192.168.1.1\n";
  31. /*
  32. This program generates and transmits a Wake-On-LAN (WOL) "Magic Packet",
  33. used for restarting machines that have been soft-powered-down
  34. (ACPI D3-warm state). It currently generates the standard AMD Magic Packet
  35. format, with an optional password appended.
  36. This software may be used and distributed according to the terms
  37. of the GNU Public License, incorporated herein by reference.
  38. Contact the author for use under other terms.
  39. This source file was originally part of the network tricks package, and
  40. is now distributed to support the Scyld Beowulf system.
  41. Copyright 1999-2003 Donald Becker and Scyld Computing Corporation.
  42. The author may be reached as becker@scyld, or C/O
  43. Scyld Computing Corporation
  44. 914 Bay Ridge Road, Suite 220
  45. Annapolis MD 21403
  46. Notes:
  47. On some systems dropping root capability allows the process to be
  48. dumped, traced or debugged.
  49. If someone traces this program, they get control of a raw socket.
  50. Linux handles this safely, but beware when porting this program.
  51. An alternative to needing 'root' is using a UDP broadcast socket, however
  52. doing so only works with adapters configured for unicast+broadcast Rx
  53. filter. That configuration consumes more power.
  54. */
  55. #include <unistd.h>
  56. #include <stdlib.h>
  57. #include <stdio.h>
  58. #include <errno.h>
  59. #include <ctype.h>
  60. #include <string.h>
  61. #if 0 /* Only exists on some versions. */
  62. #include <ioctls.h>
  63. #endif
  64. #include <sys/socket.h>
  65. #include <sys/types.h>
  66. #include <sys/ioctl.h>
  67. #include <linux/if.h>
  68. #include <features.h>
  69. #if __GLIBC__ >= 2 && __GLIBC_MINOR >= 1
  70. #include <netpacket/packet.h>
  71. #include <net/ethernet.h>
  72. #else
  73. #include <asm/types.h>
  74. #include <linux/if_packet.h>
  75. #include <linux/if_ether.h>
  76. #endif
  77. #include <netdb.h>
  78. #include <netinet/ether.h>
  79. /* Grrr, no consistency between include versions.
  80. Enable this if setsockopt() isn't declared with your library. */
  81. #if 0
  82. extern int setsockopt __P ((int __fd, int __level, int __optname,
  83. __ptr_t __optval, int __optlen));
  84. #else /* New, correct head files. */
  85. #include <sys/socket.h>
  86. #endif
  87. u_char outpack[1000];
  88. int outpack_sz = 0;
  89. int debug = 0;
  90. u_char wol_passwd[6];
  91. int wol_passwd_sz = 0;
  92. static int opt_no_src_addr = 0, opt_broadcast = 0;
  93. static int get_dest_addr(const char *arg, struct ether_addr *eaddr);
  94. static int get_fill(unsigned char *pkt, struct ether_addr *eaddr);
  95. static int get_wol_pw(const char *optarg);
  96. int main(int argc, char *argv[])
  97. {
  98. char *ifname = "eth0";
  99. int one = 1; /* True, for socket options. */
  100. int s; /* Raw socket */
  101. int errflag = 0, verbose = 0, do_version = 0;
  102. int perm_failure = 0;
  103. int i, c, pktsize;
  104. #if defined(PF_PACKET)
  105. struct sockaddr_ll whereto;
  106. #else
  107. struct sockaddr whereto; /* who to wake up */
  108. #endif
  109. struct ether_addr eaddr;
  110. while ((c = getopt(argc, argv, "bDi:p:uvV")) != -1)
  111. switch (c) {
  112. case 'b': opt_broadcast++; break;
  113. case 'D': debug++; break;
  114. case 'i': ifname = optarg; break;
  115. case 'p': get_wol_pw(optarg); break;
  116. case 'u': printf(usage_msg); return 0;
  117. case 'v': verbose++; break;
  118. case 'V': do_version++; break;
  119. case '?':
  120. errflag++;
  121. }
  122. if (verbose || do_version)
  123. printf("%s\n", version_msg);
  124. if (errflag) {
  125. fprintf(stderr, brief_usage_msg);
  126. return 3;
  127. }
  128. if (optind == argc) {
  129. fprintf(stderr, "Specify the Ethernet address as 00:11:22:33:44:55.\n");
  130. return 3;
  131. }
  132. /* Note: PF_INET, SOCK_DGRAM, IPPROTO_UDP would allow SIOCGIFHWADDR to
  133. work as non-root, but we need SOCK_PACKET to specify the Ethernet
  134. destination address. */
  135. #if defined(PF_PACKET)
  136. s = socket(PF_PACKET, SOCK_RAW, 0);
  137. #else
  138. s = socket(AF_INET, SOCK_PACKET, SOCK_PACKET);
  139. #endif
  140. if (s < 0) {
  141. if (errno == EPERM)
  142. fprintf(stderr, "ether-wake: This program must be run as root.\n");
  143. else
  144. perror("ether-wake: socket");
  145. perm_failure++;
  146. }
  147. /* Don't revert if debugging allows a normal user to get the raw socket. */
  148. setuid(getuid());
  149. /* We look up the station address before reporting failure so that
  150. errors may be reported even when run as a normal user.
  151. */
  152. if (get_dest_addr(argv[optind], &eaddr) != 0)
  153. return 3;
  154. if (perm_failure && ! debug)
  155. return 2;
  156. pktsize = get_fill(outpack, &eaddr);
  157. /* Fill in the source address, if possible.
  158. The code to retrieve the local station address is Linux specific. */
  159. if (! opt_no_src_addr) {
  160. struct ifreq if_hwaddr;
  161. unsigned char *hwaddr = if_hwaddr.ifr_hwaddr.sa_data;
  162. strcpy(if_hwaddr.ifr_name, ifname);
  163. if (ioctl(s, SIOCGIFHWADDR, &if_hwaddr) < 0) {
  164. fprintf(stderr, "SIOCGIFHWADDR on %s failed: %s\n", ifname,
  165. strerror(errno));
  166. /* Magic packets still work if our source address is bogus, but
  167. we fail just to be anal. */
  168. return 1;
  169. }
  170. memcpy(outpack+6, if_hwaddr.ifr_hwaddr.sa_data, 6);
  171. if (verbose) {
  172. printf("The hardware address (SIOCGIFHWADDR) of %s is type %d "
  173. "%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x.\n", ifname,
  174. if_hwaddr.ifr_hwaddr.sa_family, hwaddr[0], hwaddr[1],
  175. hwaddr[2], hwaddr[3], hwaddr[4], hwaddr[5]);
  176. }
  177. }
  178. if (wol_passwd_sz > 0) {
  179. memcpy(outpack+pktsize, wol_passwd, wol_passwd_sz);
  180. pktsize += wol_passwd_sz;
  181. }
  182. if (verbose > 1) {
  183. printf("The final packet is: ");
  184. for (i = 0; i < pktsize; i++)
  185. printf(" %2.2x", outpack[i]);
  186. printf(".\n");
  187. }
  188. /* This is necessary for broadcasts to work */
  189. if (setsockopt(s, SOL_SOCKET, SO_BROADCAST, (char *)&one, sizeof(one)) < 0)
  190. perror("setsockopt: SO_BROADCAST");
  191. #if defined(PF_PACKET)
  192. {
  193. struct ifreq ifr;
  194. strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
  195. if (ioctl(s, SIOCGIFINDEX, &ifr) == -1) {
  196. fprintf(stderr, "SIOCGIFINDEX on %s failed: %s\n", ifname,
  197. strerror(errno));
  198. return 1;
  199. }
  200. memset(&whereto, 0, sizeof(whereto));
  201. whereto.sll_family = AF_PACKET;
  202. whereto.sll_ifindex = ifr.ifr_ifindex;
  203. /* The manual page incorrectly claims the address must be filled.
  204. We do so because the code may change to match the docs. */
  205. whereto.sll_halen = ETH_ALEN;
  206. memcpy(whereto.sll_addr, outpack, ETH_ALEN);
  207. }
  208. #else
  209. whereto.sa_family = 0;
  210. strcpy(whereto.sa_data, ifname);
  211. #endif
  212. if ((i = sendto(s, outpack, pktsize, 0, (struct sockaddr *)&whereto,
  213. sizeof(whereto))) < 0)
  214. perror("sendto");
  215. else if (debug)
  216. printf("Sendto worked ! %d.\n", i);
  217. #ifdef USE_SEND
  218. if (bind(s, (struct sockaddr *)&whereto, sizeof(whereto)) < 0)
  219. perror("bind");
  220. else if (send(s, outpack, 100, 0) < 0)
  221. perror("send");
  222. #endif
  223. #ifdef USE_SENDMSG
  224. {
  225. struct msghdr msghdr = { 0,};
  226. struct iovec iovector[1];
  227. msghdr.msg_name = &whereto;
  228. msghdr.msg_namelen = sizeof(whereto);
  229. msghdr.msg_iov = iovector;
  230. msghdr.msg_iovlen = 1;
  231. iovector[0].iov_base = outpack;
  232. iovector[0].iov_len = pktsize;
  233. if ((i = sendmsg(s, &msghdr, 0)) < 0)
  234. perror("sendmsg");
  235. else if (debug)
  236. printf("sendmsg worked, %d (%d).\n", i, errno);
  237. }
  238. #endif
  239. return 0;
  240. }
  241. /* Convert the host ID string to a MAC address.
  242. The string may be a
  243. Host name
  244. IP address string
  245. MAC address string
  246. */
  247. static int get_dest_addr(const char *hostid, struct ether_addr *eaddr)
  248. {
  249. struct ether_addr *eap;
  250. eap = ether_aton(hostid);
  251. if (eap) {
  252. *eaddr = *eap;
  253. if (debug)
  254. fprintf(stderr, "The target station address is %s.\n",
  255. ether_ntoa(eaddr));
  256. } else {
  257. (void)fprintf(stderr,
  258. "ether-wake: The Magic Packet host address must be "
  259. "specified as a station address, 00:11:22:33:44:55.\n");
  260. return -1;
  261. }
  262. return 0;
  263. }
  264. static int get_fill(unsigned char *pkt, struct ether_addr *eaddr)
  265. {
  266. int offset, i;
  267. unsigned char *station_addr = eaddr->ether_addr_octet;
  268. if (opt_broadcast)
  269. memset(pkt+0, 0xff, 6);
  270. else
  271. memcpy(pkt, station_addr, 6);
  272. memcpy(pkt+6, station_addr, 6);
  273. pkt[12] = 0x08; /* Or 0x0806 for ARP, 0x8035 for RARP */
  274. pkt[13] = 0x42;
  275. offset = 14;
  276. memset(pkt+offset, 0xff, 6);
  277. offset += 6;
  278. for (i = 0; i < 16; i++) {
  279. memcpy(pkt+offset, station_addr, 6);
  280. offset += 6;
  281. }
  282. if (debug) {
  283. fprintf(stderr, "Packet is ");
  284. for (i = 0; i < offset; i++)
  285. fprintf(stderr, " %2.2x", pkt[i]);
  286. fprintf(stderr, ".\n");
  287. }
  288. return offset;
  289. }
  290. static int get_wol_pw(const char *optarg)
  291. {
  292. int passwd[6];
  293. int byte_cnt;
  294. int i;
  295. byte_cnt = sscanf(optarg, "%2x:%2x:%2x:%2x:%2x:%2x",
  296. &passwd[0], &passwd[1], &passwd[2],
  297. &passwd[3], &passwd[4], &passwd[5]);
  298. if (byte_cnt < 4)
  299. byte_cnt = sscanf(optarg, "%d.%d.%d.%d",
  300. &passwd[0], &passwd[1], &passwd[2], &passwd[3]);
  301. if (byte_cnt < 4) {
  302. fprintf(stderr, "Unable to read the Wake-On-LAN password.\n");
  303. return 0;
  304. }
  305. printf(" The Magic packet password is %2.2x %2.2x %2.2x %2.2x (%d).\n",
  306. passwd[0], passwd[1], passwd[2], passwd[3], byte_cnt);
  307. for (i = 0; i < byte_cnt; i++)
  308. wol_passwd[i] = passwd[i];
  309. return wol_passwd_sz = byte_cnt;
  310. }
  311. #if 0
  312. {
  313. to = (struct sockaddr_in *)&whereto;
  314. to->sin_family = AF_INET;
  315. if (inet_aton(target, &to->sin_addr)) {
  316. hostname = target;
  317. }
  318. memset (&sa, 0, sizeof sa);
  319. sa.sa_family = AF_INET;
  320. strncpy (sa.sa_data, interface, sizeof sa.sa_data);
  321. sendto (sock, buf, bufix + len, 0, &sa, sizeof sa);
  322. strncpy (sa.sa_data, interface, sizeof sa.sa_data);
  323. #if 1
  324. sendto (sock, buf, bufix + len, 0, &sa, sizeof sa);
  325. #else
  326. bind (sock, &sa, sizeof sa);
  327. connect();
  328. send (sock, buf, bufix + len, 0);
  329. #endif
  330. }
  331. #endif
  332. /*
  333. * Local variables:
  334. * compile-command: "gcc -O -Wall -o ether-wake ether-wake.c"
  335. * c-indent-level: 4
  336. * c-basic-offset: 4
  337. * c-indent-level: 4
  338. * tab-width: 4
  339. * End:
  340. */