1/*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (C) 2006, 2007, 2008, 2009, 2010 Marc Balmer <marc@msys.ch>
5 * Copyright (C) 2000 Eugene M. Kim.  All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
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.
15 *
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.
27 */
28
29#include <sys/cdefs.h>
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 <net/if_dl.h>
36#include <net/if_types.h>
37#include <netinet/in.h>
38#include <netinet/if_ether.h>
39
40#include <err.h>
41#include <fcntl.h>
42#include <ifaddrs.h>
43#include <stdio.h>
44#include <stdlib.h>
45#include <string.h>
46#include <unistd.h>
47
48#define	_PATH_BPF	"/dev/bpf"
49
50#ifndef SYNC_LEN
51#define	SYNC_LEN	6
52#endif
53
54#ifndef DESTADDR_COUNT
55#define	DESTADDR_COUNT	16
56#endif
57
58static int	bind_if_to_bpf(char const *ifname, int bpf);
59static int	find_ether(char *dst, size_t len);
60static int	get_ether(char const *text, struct ether_addr *addr);
61static int	send_wakeup(int bpf, struct ether_addr const *addr);
62static void	usage(void);
63static int	wake(int bpf, const char *host);
64
65static void
66usage(void)
67{
68
69	(void)fprintf(stderr, "usage: wake [interface] lladdr [lladdr ...]\n");
70	exit(1);
71}
72
73static int
74wake(int bpf, const char *host)
75{
76	struct ether_addr macaddr;
77
78	if (get_ether(host, &macaddr) == -1)
79		return (-1);
80
81	return (send_wakeup(bpf, &macaddr));
82}
83
84static int
85bind_if_to_bpf(char const *ifname, int bpf)
86{
87	struct ifreq ifr;
88	u_int dlt;
89
90	if (strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name)) >=
91	    sizeof(ifr.ifr_name))
92		return (-1);
93
94	if (ioctl(bpf, BIOCSETIF, &ifr) == -1)
95		return (-1);
96
97	if (ioctl(bpf, BIOCGDLT, &dlt) == -1)
98		return (-1);
99
100	if (dlt != DLT_EN10MB)
101		return (-1);
102
103	return (0);
104}
105
106static int
107find_ether(char *dst, size_t len)
108{
109	struct ifaddrs *ifap, *ifa;
110	struct sockaddr_dl *sdl = NULL;
111	int nifs;
112
113	if (dst == NULL || len == 0)
114		return (0);
115
116	if (getifaddrs(&ifap) != 0)
117		return (-1);
118
119	/* XXX also check the link state */
120	for (nifs = 0, ifa = ifap; ifa; ifa = ifa->ifa_next)
121		if (ifa->ifa_addr->sa_family == AF_LINK &&
122		    ifa->ifa_flags & IFF_UP && ifa->ifa_flags & IFF_RUNNING) {
123			sdl = (struct sockaddr_dl *)ifa->ifa_addr;
124			if (sdl->sdl_type == IFT_ETHER) {
125				strlcpy(dst, ifa->ifa_name, len);
126				nifs++;
127			}
128		}
129
130	freeifaddrs(ifap);
131	return (nifs == 1 ? 0 : -1);
132}
133
134static int
135get_ether(char const *text, struct ether_addr *addr)
136{
137	struct ether_addr *paddr;
138
139	paddr = ether_aton(text);
140	if (paddr != NULL) {
141		*addr = *paddr;
142		return (0);
143	}
144	if (ether_hostton(text, addr)) {
145		warnx("no match for host %s found", text);
146		return (-1);
147	}
148	return (0);
149}
150
151static int
152send_wakeup(int bpf, struct ether_addr const *addr)
153{
154	struct {
155		struct ether_header hdr;
156		u_char data[SYNC_LEN + ETHER_ADDR_LEN * DESTADDR_COUNT];
157	} __packed pkt;
158	u_char *p;
159	ssize_t bw;
160	ssize_t len;
161	int i;
162
163	(void)memset(pkt.hdr.ether_dhost, 0xff, sizeof(pkt.hdr.ether_dhost));
164	pkt.hdr.ether_type = htons(0);
165	(void)memset(pkt.data, 0xff, SYNC_LEN);
166	for (p = pkt.data + SYNC_LEN, i = 0; i < DESTADDR_COUNT;
167	    p += ETHER_ADDR_LEN, i++)
168		bcopy(addr->octet, p, ETHER_ADDR_LEN);
169	p = (u_char *)&pkt;
170	len = sizeof(pkt);
171	bw = 0;
172	while (len) {
173		if ((bw = write(bpf, p, len)) == -1) {
174			warn("write()");
175			return (-1);
176		}
177		len -= bw;
178		p += bw;
179	}
180	return (0);
181}
182
183int
184main(int argc, char *argv[])
185{
186	int bpf, n, rval;
187	char ifname[IF_NAMESIZE];
188
189	if (argc < 2)
190		usage();
191
192	if ((bpf = open(_PATH_BPF, O_RDWR)) == -1)
193		err(1, "Cannot open bpf interface");
194
195	n = 2;
196	if (bind_if_to_bpf(argv[1], bpf) == -1) {
197		if (find_ether(ifname, sizeof(ifname)))
198			err(1, "Failed to determine ethernet interface");
199		if (bind_if_to_bpf(ifname, bpf) == -1)
200			err(1, "Cannot bind to interface `%s'", ifname);
201		--n;
202	} else
203		strlcpy(ifname, argv[1], sizeof(ifname));
204
205	if (n >= argc)
206		usage();
207	rval = 0;
208	for (; n < argc; n++) {
209		if (wake(bpf, argv[n]) != 0) {
210			rval = 1;
211			warn("Cannot send Wake on LAN frame over `%s' to `%s'",
212			    ifname, argv[n]);
213		}
214	}
215	exit(rval);
216}
217