1 /* $NetBSD: wake.c,v 1.10 2010/01/03 17:58:14 mbalmer Exp $ */
4 * Copyright (C) 2006, 2007, 2008, 2009, 2010 Marc Balmer <marc@msys.ch>
5 * Copyright (C) 2000 Eugene M. Kim. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Author's name may not be used endorse or promote products derived
14 * from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
20 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
24 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
25 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
29 /* Send Wake on LAN packets to hosts on a local Ethernet network */
31 #include <sys/types.h>
32 #include <sys/param.h>
33 #include <sys/queue.h>
34 #include <sys/ioctl.h>
35 #include <sys/socket.h>
40 #include <net/if_dl.h>
41 #include <net/if_types.h>
43 #include <netinet/in.h>
44 #include <netinet/if_ether.h>
63 #ifndef DESTADDR_COUNT
64 #define DESTADDR_COUNT 16
67 static void usage(void);
68 static int wake(int, const char *);
69 static int bind_if_to_bpf(char const *, int);
70 static int find_ether(char *, size_t);
71 static int get_ether(char const *, struct ether_addr
*);
72 static int send_wakeup(int, struct ether_addr
const *);
77 (void)fprintf(stderr
, "usage: %s [interface] lladdr [lladdr ...]\n",
83 wake(int bpf
, const char *host
)
85 struct ether_addr macaddr
;
87 if (get_ether(host
, &macaddr
) == -1)
90 return send_wakeup(bpf
, &macaddr
);
94 bind_if_to_bpf(char const *ifname
, int bpf
)
99 if (strlcpy(ifr
.ifr_name
, ifname
, sizeof(ifr
.ifr_name
)) >=
100 sizeof(ifr
.ifr_name
)) {
101 errno
= ENAMETOOLONG
;
104 if (ioctl(bpf
, BIOCSETIF
, &ifr
) == -1)
106 if (ioctl(bpf
, BIOCGDLT
, &dlt
) == -1)
108 if (dlt
!= DLT_EN10MB
) {
116 find_ether(char *dst
, size_t len
)
118 struct ifaddrs
*ifap
, *ifa
;
119 struct sockaddr_dl
*sdl
= NULL
;
122 if (dst
== NULL
|| len
== 0)
125 if (getifaddrs(&ifap
) != 0)
128 /* XXX also check the link state */
129 for (nifs
= 0, ifa
= ifap
; ifa
; ifa
= ifa
->ifa_next
)
130 if (ifa
->ifa_addr
->sa_family
== AF_LINK
&&
131 ifa
->ifa_flags
& IFF_UP
&& ifa
->ifa_flags
& IFF_RUNNING
) {
132 sdl
= (struct sockaddr_dl
*)ifa
->ifa_addr
;
133 if (sdl
->sdl_type
== IFT_ETHER
) {
134 strlcpy(dst
, ifa
->ifa_name
, len
);
140 return nifs
== 1 ? 0 : -1;
144 get_ether(char const *text
, struct ether_addr
*addr
)
146 struct ether_addr
*paddr
;
148 paddr
= ether_aton(text
);
153 if (ether_hostton(text
, addr
))
159 send_wakeup(int bpf
, struct ether_addr
const *addr
)
162 struct ether_header hdr
;
163 u_char data
[SYNC_LEN
+ ETHER_ADDR_LEN
* DESTADDR_COUNT
];
169 (void)memset(pkt
.hdr
.ether_dhost
, 0xff, sizeof(pkt
.hdr
.ether_dhost
));
170 pkt
.hdr
.ether_type
= htons(0);
171 (void)memset(pkt
.data
, 0xff, SYNC_LEN
);
172 for (p
= pkt
.data
+ SYNC_LEN
, i
= 0; i
< DESTADDR_COUNT
;
173 p
+= ETHER_ADDR_LEN
, i
++)
174 (void)memcpy(p
, addr
->ether_addr_octet
, ETHER_ADDR_LEN
);
175 p
= (u_char
*)(void *)&pkt
;
179 if ((bw
= write(bpf
, p
, len
)) == -1)
188 main(int argc
, char *argv
[])
191 char ifname
[IF_NAMESIZE
];
196 if ((bpf
= open(_PATH_BPF
, O_RDWR
)) == -1)
197 err(EXIT_FAILURE
, "Cannot open bpf interface");
200 if (bind_if_to_bpf(argv
[1], bpf
) == -1) {
201 if (find_ether(ifname
, sizeof(ifname
)))
202 err(EXIT_FAILURE
, "Failed to determine ethernet "
204 if (bind_if_to_bpf(ifname
, bpf
) == -1)
205 err(EXIT_FAILURE
, "Cannot bind to interface `%s'",
209 strlcpy(ifname
, argv
[1], sizeof(ifname
));
213 for (; n
< argc
; n
++)
214 if (wake(bpf
, argv
[n
]))
215 warn("Cannot send Wake on LAN frame over `%s' to `%s'",