1 /* 2 * Copyright (C) 2006, 2007, 2008, 2009 Marc Balmer <marc@msys.ch> 3 * Copyright (C) 2000 Eugene M. Kim. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Author's name may not be used endorse or promote products derived 12 * from this software without specific prior written permission. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 16 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 17 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, 18 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 19 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 20 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 22 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 23 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 24 * POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27 #include <sys/cdefs.h> 28 __FBSDID("$FreeBSD$"); 29 30 #include <sys/ioctl.h> 31 #include <sys/socket.h> 32 #include <sys/time.h> 33 #include <net/bpf.h> 34 #include <net/if.h> 35 #include <netinet/in.h> 36 #include <netinet/if_ether.h> 37 38 #include <err.h> 39 #include <fcntl.h> 40 #include <stdio.h> 41 #include <stdlib.h> 42 #include <string.h> 43 #include <unistd.h> 44 45 #define _PATH_BPF "/dev/bpf" 46 47 #ifndef SYNC_LEN 48 #define SYNC_LEN 6 49 #endif 50 51 #ifndef DESTADDR_COUNT 52 #define DESTADDR_COUNT 16 53 #endif 54 55 static int bind_if_to_bpf(char const *ifname, int bpf); 56 static int get_ether(char const *text, struct ether_addr *addr); 57 static int send_wakeup(int bpf, struct ether_addr const *addr); 58 static void usage(void); 59 static int wake(const char *iface, const char *host); 60 61 static void 62 usage(void) 63 { 64 65 (void)fprintf(stderr, "usage: wake interface lladdr [lladdr ...]\n"); 66 exit(1); 67 } 68 69 static int 70 wake(const char *iface, const char *host) 71 { 72 struct ether_addr macaddr; 73 int bpf, res; 74 75 bpf = open(_PATH_BPF, O_RDWR); 76 if (bpf == -1) { 77 warn("no bpf"); 78 return (-1); 79 } 80 if (bind_if_to_bpf(iface, bpf) == -1 || 81 get_ether(host, &macaddr) == -1) { 82 (void)close(bpf); 83 return (-1); 84 } 85 res = send_wakeup(bpf, &macaddr); 86 (void)close(bpf); 87 return (res); 88 } 89 90 static int 91 bind_if_to_bpf(char const *ifname, int bpf) 92 { 93 struct ifreq ifr; 94 u_int dlt; 95 96 if (strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name)) >= 97 sizeof(ifr.ifr_name)) { 98 warnx("interface name too long: %s", ifname); 99 return (-1); 100 } 101 if (ioctl(bpf, BIOCSETIF, &ifr) == -1) { 102 warn("ioctl(%s)", "BIOCSETIF"); 103 return (-1); 104 } 105 if (ioctl(bpf, BIOCGDLT, &dlt) == -1) { 106 warn("ioctl(%s)", "BIOCGDLT"); 107 return (-1); 108 } 109 if (dlt != DLT_EN10MB) { 110 warnx("incompatible media"); 111 return (-1); 112 } 113 return (0); 114 } 115 116 static int 117 get_ether(char const *text, struct ether_addr *addr) 118 { 119 struct ether_addr *paddr; 120 121 paddr = ether_aton(text); 122 if (paddr != NULL) { 123 *addr = *paddr; 124 return (0); 125 } 126 if (ether_hostton(text, addr)) { 127 warnx("no match for host %s found", text); 128 return (-1); 129 } 130 return (0); 131 } 132 133 static int 134 send_wakeup(int bpf, struct ether_addr const *addr) 135 { 136 struct { 137 struct ether_header hdr; 138 u_char data[SYNC_LEN + ETHER_ADDR_LEN * DESTADDR_COUNT]; 139 } __packed pkt; 140 u_char *p; 141 ssize_t bw; 142 ssize_t len; 143 int i; 144 145 (void)memset(pkt.hdr.ether_dhost, 0xff, sizeof(pkt.hdr.ether_dhost)); 146 pkt.hdr.ether_type = htons(0); 147 (void)memset(pkt.data, 0xff, SYNC_LEN); 148 for (p = pkt.data + SYNC_LEN, i = 0; i < DESTADDR_COUNT; 149 p += ETHER_ADDR_LEN, i++) 150 bcopy(addr->octet, p, ETHER_ADDR_LEN); 151 p = (u_char *)&pkt; 152 len = sizeof(pkt); 153 bw = 0; 154 while (len) { 155 if ((bw = write(bpf, p, len)) == -1) { 156 warn("write()"); 157 return (-1); 158 } 159 len -= bw; 160 p += bw; 161 } 162 return (0); 163 } 164 165 int 166 main(int argc, char *argv[]) 167 { 168 int n; 169 170 if (argc < 3) 171 usage(); 172 173 for (n = 2; n < argc; n++) 174 if (wake(argv[1], argv[n])) 175 warnx("error sending Wake on LAN frame over %s to %s", 176 argv[1], argv[n]); 177 return (0); 178 } 179