wakeonlan.c revision 1.1 1 1.1 uebayasi /* $NetBSD: wakeonlan.c,v 1.1 2012/05/26 01:58:21 uebayasi Exp $ */
2 1.1 uebayasi
3 1.1 uebayasi /*
4 1.1 uebayasi * Copyright (C) 2006, 2007, 2008, 2009, 2010 Marc Balmer <marc (at) msys.ch>
5 1.1 uebayasi * Copyright (C) 2000 Eugene M. Kim. All rights reserved.
6 1.1 uebayasi *
7 1.1 uebayasi * Redistribution and use in source and binary forms, with or without
8 1.1 uebayasi * modification, are permitted provided that the following conditions
9 1.1 uebayasi * are met:
10 1.1 uebayasi *
11 1.1 uebayasi * 1. Redistributions of source code must retain the above copyright
12 1.1 uebayasi * notice, this list of conditions and the following disclaimer.
13 1.1 uebayasi * 2. Author's name may not be used endorse or promote products derived
14 1.1 uebayasi * from this software without specific prior written permission.
15 1.1 uebayasi *
16 1.1 uebayasi * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 1.1 uebayasi * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 1.1 uebayasi * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 1.1 uebayasi * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
20 1.1 uebayasi * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 1.1 uebayasi * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22 1.1 uebayasi * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 1.1 uebayasi * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
24 1.1 uebayasi * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
25 1.1 uebayasi * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 1.1 uebayasi * POSSIBILITY OF SUCH DAMAGE.
27 1.1 uebayasi */
28 1.1 uebayasi
29 1.1 uebayasi /* Send Wake on LAN packets to hosts on a local Ethernet network */
30 1.1 uebayasi
31 1.1 uebayasi #include <sys/types.h>
32 1.1 uebayasi #include <sys/param.h>
33 1.1 uebayasi #include <sys/queue.h>
34 1.1 uebayasi #include <sys/ioctl.h>
35 1.1 uebayasi #include <sys/socket.h>
36 1.1 uebayasi #include <sys/time.h>
37 1.1 uebayasi
38 1.1 uebayasi #include <net/bpf.h>
39 1.1 uebayasi #include <net/if.h>
40 1.1 uebayasi #include <net/if_dl.h>
41 1.1 uebayasi #include <net/if_types.h>
42 1.1 uebayasi
43 1.1 uebayasi #include <netinet/in.h>
44 1.1 uebayasi #include <netinet/if_ether.h>
45 1.1 uebayasi
46 1.1 uebayasi #include <err.h>
47 1.1 uebayasi #include <errno.h>
48 1.1 uebayasi #include <fcntl.h>
49 1.1 uebayasi #include <ifaddrs.h>
50 1.1 uebayasi #include <limits.h>
51 1.1 uebayasi #include <paths.h>
52 1.1 uebayasi #include <stdarg.h>
53 1.1 uebayasi #include <stdio.h>
54 1.1 uebayasi #include <stdlib.h>
55 1.1 uebayasi #include <string.h>
56 1.1 uebayasi #include <sysexits.h>
57 1.1 uebayasi #include <unistd.h>
58 1.1 uebayasi
59 1.1 uebayasi #ifndef SYNC_LEN
60 1.1 uebayasi #define SYNC_LEN 6
61 1.1 uebayasi #endif
62 1.1 uebayasi
63 1.1 uebayasi #ifndef DESTADDR_COUNT
64 1.1 uebayasi #define DESTADDR_COUNT 16
65 1.1 uebayasi #endif
66 1.1 uebayasi
67 1.1 uebayasi __dead static void usage(void);
68 1.1 uebayasi static int wake(int, const char *);
69 1.1 uebayasi static int bind_if_to_bpf(char const *, int);
70 1.1 uebayasi static int find_ether(char *, size_t);
71 1.1 uebayasi static int get_ether(char const *, struct ether_addr *);
72 1.1 uebayasi static int send_wakeup(int, struct ether_addr const *);
73 1.1 uebayasi
74 1.1 uebayasi static void
75 1.1 uebayasi usage(void)
76 1.1 uebayasi {
77 1.1 uebayasi fprintf(stderr, "usage: %s [interface] lladdr [lladdr ...]\n",
78 1.1 uebayasi getprogname());
79 1.1 uebayasi exit(EXIT_FAILURE);
80 1.1 uebayasi }
81 1.1 uebayasi
82 1.1 uebayasi static int
83 1.1 uebayasi wake(int bpf, const char *host)
84 1.1 uebayasi {
85 1.1 uebayasi struct ether_addr macaddr;
86 1.1 uebayasi
87 1.1 uebayasi if (get_ether(host, &macaddr) == -1)
88 1.1 uebayasi return -1;
89 1.1 uebayasi
90 1.1 uebayasi return send_wakeup(bpf, &macaddr);
91 1.1 uebayasi }
92 1.1 uebayasi
93 1.1 uebayasi static int
94 1.1 uebayasi bind_if_to_bpf(char const *ifname, int bpf)
95 1.1 uebayasi {
96 1.1 uebayasi struct ifreq ifr;
97 1.1 uebayasi u_int dlt;
98 1.1 uebayasi
99 1.1 uebayasi if (strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name)) >=
100 1.1 uebayasi sizeof(ifr.ifr_name)) {
101 1.1 uebayasi errno = ENAMETOOLONG;
102 1.1 uebayasi return -1;
103 1.1 uebayasi }
104 1.1 uebayasi if (ioctl(bpf, BIOCSETIF, &ifr) == -1)
105 1.1 uebayasi return -1;
106 1.1 uebayasi if (ioctl(bpf, BIOCGDLT, &dlt) == -1)
107 1.1 uebayasi return -1;
108 1.1 uebayasi if (dlt != DLT_EN10MB) {
109 1.1 uebayasi errno = EOPNOTSUPP;
110 1.1 uebayasi return -1;
111 1.1 uebayasi }
112 1.1 uebayasi return 0;
113 1.1 uebayasi }
114 1.1 uebayasi
115 1.1 uebayasi static int
116 1.1 uebayasi find_ether(char *dst, size_t len)
117 1.1 uebayasi {
118 1.1 uebayasi struct ifaddrs *ifap, *ifa;
119 1.1 uebayasi struct sockaddr_dl *sdl = NULL;
120 1.1 uebayasi int nifs;
121 1.1 uebayasi
122 1.1 uebayasi if (dst == NULL || len == 0)
123 1.1 uebayasi return 0;
124 1.1 uebayasi
125 1.1 uebayasi if (getifaddrs(&ifap) != 0)
126 1.1 uebayasi return -1;
127 1.1 uebayasi
128 1.1 uebayasi /* XXX also check the link state */
129 1.1 uebayasi for (nifs = 0, ifa = ifap; ifa; ifa = ifa->ifa_next)
130 1.1 uebayasi if (ifa->ifa_addr->sa_family == AF_LINK &&
131 1.1 uebayasi ifa->ifa_flags & IFF_UP && ifa->ifa_flags & IFF_RUNNING) {
132 1.1 uebayasi sdl = (struct sockaddr_dl *)ifa->ifa_addr;
133 1.1 uebayasi if (sdl->sdl_type == IFT_ETHER) {
134 1.1 uebayasi strlcpy(dst, ifa->ifa_name, len);
135 1.1 uebayasi nifs++;
136 1.1 uebayasi }
137 1.1 uebayasi }
138 1.1 uebayasi
139 1.1 uebayasi freeifaddrs(ifap);
140 1.1 uebayasi return nifs == 1 ? 0 : -1;
141 1.1 uebayasi }
142 1.1 uebayasi
143 1.1 uebayasi static int
144 1.1 uebayasi get_ether(char const *text, struct ether_addr *addr)
145 1.1 uebayasi {
146 1.1 uebayasi struct ether_addr *paddr;
147 1.1 uebayasi
148 1.1 uebayasi paddr = ether_aton(text);
149 1.1 uebayasi if (paddr != NULL) {
150 1.1 uebayasi *addr = *paddr;
151 1.1 uebayasi return 0;
152 1.1 uebayasi }
153 1.1 uebayasi if (ether_hostton(text, addr))
154 1.1 uebayasi return -1;
155 1.1 uebayasi return 0;
156 1.1 uebayasi }
157 1.1 uebayasi
158 1.1 uebayasi static int
159 1.1 uebayasi send_wakeup(int bpf, struct ether_addr const *addr)
160 1.1 uebayasi {
161 1.1 uebayasi struct {
162 1.1 uebayasi struct ether_header hdr;
163 1.1 uebayasi u_char data[SYNC_LEN + ETHER_ADDR_LEN * DESTADDR_COUNT];
164 1.1 uebayasi } pkt;
165 1.1 uebayasi u_char *p;
166 1.1 uebayasi int i;
167 1.1 uebayasi ssize_t bw, len;
168 1.1 uebayasi
169 1.1 uebayasi memset(pkt.hdr.ether_dhost, 0xff, sizeof(pkt.hdr.ether_dhost));
170 1.1 uebayasi pkt.hdr.ether_type = htons(0);
171 1.1 uebayasi memset(pkt.data, 0xff, SYNC_LEN);
172 1.1 uebayasi for (p = pkt.data + SYNC_LEN, i = 0; i < DESTADDR_COUNT;
173 1.1 uebayasi p += ETHER_ADDR_LEN, i++)
174 1.1 uebayasi memcpy(p, addr->ether_addr_octet, ETHER_ADDR_LEN);
175 1.1 uebayasi p = (u_char *)(void *)&pkt;
176 1.1 uebayasi len = sizeof(pkt);
177 1.1 uebayasi bw = 0;
178 1.1 uebayasi while (len) {
179 1.1 uebayasi if ((bw = write(bpf, p, len)) == -1)
180 1.1 uebayasi return -1;
181 1.1 uebayasi len -= bw;
182 1.1 uebayasi p += bw;
183 1.1 uebayasi }
184 1.1 uebayasi return 0;
185 1.1 uebayasi }
186 1.1 uebayasi
187 1.1 uebayasi int
188 1.1 uebayasi main(int argc, char *argv[])
189 1.1 uebayasi {
190 1.1 uebayasi int bpf, n;
191 1.1 uebayasi char ifname[IF_NAMESIZE];
192 1.1 uebayasi
193 1.1 uebayasi if (argc < 2)
194 1.1 uebayasi usage();
195 1.1 uebayasi
196 1.1 uebayasi if ((bpf = open(_PATH_BPF, O_RDWR)) == -1)
197 1.1 uebayasi err(EXIT_FAILURE, "Cannot open bpf interface");
198 1.1 uebayasi
199 1.1 uebayasi n = 2;
200 1.1 uebayasi if (bind_if_to_bpf(argv[1], bpf) == -1) {
201 1.1 uebayasi if (find_ether(ifname, sizeof(ifname)))
202 1.1 uebayasi err(EXIT_FAILURE, "Failed to determine ethernet "
203 1.1 uebayasi "interface");
204 1.1 uebayasi if (bind_if_to_bpf(ifname, bpf) == -1)
205 1.1 uebayasi err(EXIT_FAILURE, "Cannot bind to interface `%s'",
206 1.1 uebayasi ifname);
207 1.1 uebayasi --n;
208 1.1 uebayasi } else
209 1.1 uebayasi strlcpy(ifname, argv[1], sizeof(ifname));
210 1.1 uebayasi
211 1.1 uebayasi if (n >= argc)
212 1.1 uebayasi usage();
213 1.1 uebayasi for (; n < argc; n++)
214 1.1 uebayasi if (wake(bpf, argv[n]))
215 1.1 uebayasi warn("Cannot send Wake on LAN frame over `%s' to `%s'",
216 1.1 uebayasi ifname, argv[n]);
217 1.1 uebayasi return EXIT_SUCCESS;
218 1.1 uebayasi }
219