]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.sbin/wake/wake.c
Add wake, a tool to send Wake on LAN frames to hosts on a local Ethernet network
[FreeBSD/FreeBSD.git] / usr.sbin / wake / wake.c
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/types.h>
31 #include <sys/param.h>
32 #include <sys/queue.h>
33 #include <sys/ioctl.h>
34 #include <sys/socket.h>
35 #include <sys/time.h>
36 #include <net/bpf.h>
37 #include <net/if.h>
38
39 #include <netinet/in.h>
40 #include <netinet/if_ether.h>
41
42 #include <err.h>
43 #include <errno.h>
44 #include <fcntl.h>
45 #include <limits.h>
46 #include <stdarg.h>
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <string.h>
50 #include <sysexits.h>
51 #include <unistd.h>
52
53 #define _PATH_BPF       "/dev/bpf"
54
55 #ifndef SYNC_LEN
56 #define SYNC_LEN 6
57 #endif
58
59 #ifndef DESTADDR_COUNT
60 #define DESTADDR_COUNT 16
61 #endif
62
63 void usage(void);
64
65 int wake(const char *iface, const char *host);
66 int bind_if_to_bpf(char const *ifname, int bpf);
67 int get_ether(char const *text, struct ether_addr *addr);
68 int send_wakeup(int bpf, struct ether_addr const *addr);
69
70 void
71 usage(void)
72 {
73         (void)fprintf(stderr, "usage: wake interface lladdr\n");
74         exit(0);
75 }
76
77 int
78 wake(const char *iface, const char *host)
79 {
80         int res, bpf;
81         struct ether_addr macaddr;
82
83         bpf = open(_PATH_BPF, O_RDWR);
84         if (bpf == -1) {
85                 printf("no bpf\n");
86                 return -1;
87         }
88         if (bind_if_to_bpf(iface, bpf) == -1 ||
89             get_ether(host, &macaddr) == -1) {
90                 (void)close(bpf);
91                 return -1;
92         }
93         res = send_wakeup(bpf, &macaddr);
94         (void)close(bpf);
95         return res;
96 }
97
98 int
99 bind_if_to_bpf(char const *ifname, int bpf)
100 {
101         struct ifreq ifr;
102         u_int dlt;
103
104         if (strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name)) >=
105             sizeof(ifr.ifr_name))
106                 return -1;
107         if (ioctl(bpf, BIOCSETIF, &ifr) == -1)
108                 return -1;
109         if (ioctl(bpf, BIOCGDLT, &dlt) == -1)
110                 return -1;
111         if (dlt != DLT_EN10MB)
112                 return -1;
113         return 0;
114 }
115
116 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                 return -1;
128         return 0;
129 }
130
131 int
132 send_wakeup(int bpf, struct ether_addr const *addr)
133 {
134         struct {
135                 struct ether_header hdr;
136                 u_char data[SYNC_LEN + ETHER_ADDR_LEN * DESTADDR_COUNT];
137         } pkt;
138         u_char *p;
139         int i;
140         ssize_t bw;
141         ssize_t len;
142
143         (void)memset(pkt.hdr.ether_dhost, 0xff, sizeof(pkt.hdr.ether_dhost));
144         pkt.hdr.ether_type = htons(0);
145         (void)memset(pkt.data, 0xff, SYNC_LEN);
146         for (p = pkt.data + SYNC_LEN, i = 0; i < DESTADDR_COUNT;
147             p += ETHER_ADDR_LEN, i++)
148                 bcopy(addr->octet, p, ETHER_ADDR_LEN);
149         p = (u_char *)&pkt;
150         len = sizeof(pkt);
151         bw = 0;
152         while (len) {
153                 if ((bw = write(bpf, &pkt, sizeof(pkt))) == -1)
154                         return -1;
155                 len -= bw;
156                 p += bw;
157         }
158         return 0;
159 }
160
161 int
162 main(int argc, char *argv[])
163 {
164         int n;
165
166         if (argc < 3)
167                 usage();
168
169         for (n = 2; n < argc; n++)
170                 if (wake(argv[1], argv[n]))
171                         warnx("error sending Wake on LAN frame over %s to %s",
172                             argv[1], argv[n]);
173         return 0;
174 }