2017-11-27 15:37:16 +00:00
|
|
|
/*-
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause-FreeBSD
|
|
|
|
*
|
2010-02-08 20:57:49 +00:00
|
|
|
* Copyright (C) 2006, 2007, 2008, 2009, 2010 Marc Balmer <marc@msys.ch>
|
2009-06-30 18:51:22 +00:00
|
|
|
* Copyright (C) 2000 Eugene M. Kim. All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Author's name may not be used endorse or promote products derived
|
|
|
|
* from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
|
|
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
|
|
|
|
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
|
|
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
|
|
|
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
|
|
|
|
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
2009-07-01 13:07:02 +00:00
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <net/bpf.h>
|
|
|
|
#include <net/if.h>
|
2010-02-08 20:57:49 +00:00
|
|
|
#include <net/if_dl.h>
|
|
|
|
#include <net/if_types.h>
|
2009-07-01 13:07:02 +00:00
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <netinet/if_ether.h>
|
|
|
|
|
2009-07-01 22:23:26 +00:00
|
|
|
#include <err.h>
|
|
|
|
#include <fcntl.h>
|
2010-02-08 20:57:49 +00:00
|
|
|
#include <ifaddrs.h>
|
2009-07-01 22:23:26 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2009-07-01 13:07:02 +00:00
|
|
|
#define _PATH_BPF "/dev/bpf"
|
2009-06-30 18:51:22 +00:00
|
|
|
|
|
|
|
#ifndef SYNC_LEN
|
2009-07-01 13:07:02 +00:00
|
|
|
#define SYNC_LEN 6
|
2009-06-30 18:51:22 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef DESTADDR_COUNT
|
2009-07-01 13:07:02 +00:00
|
|
|
#define DESTADDR_COUNT 16
|
2009-06-30 18:51:22 +00:00
|
|
|
#endif
|
|
|
|
|
2009-07-01 13:07:02 +00:00
|
|
|
static int bind_if_to_bpf(char const *ifname, int bpf);
|
2010-02-08 20:57:49 +00:00
|
|
|
static int find_ether(char *dst, size_t len);
|
2009-07-01 13:07:02 +00:00
|
|
|
static int get_ether(char const *text, struct ether_addr *addr);
|
|
|
|
static int send_wakeup(int bpf, struct ether_addr const *addr);
|
2009-07-01 22:23:26 +00:00
|
|
|
static void usage(void);
|
2010-02-08 20:57:49 +00:00
|
|
|
static int wake(int bpf, const char *host);
|
2009-06-30 18:51:22 +00:00
|
|
|
|
2009-07-01 13:07:02 +00:00
|
|
|
static void
|
2009-06-30 18:51:22 +00:00
|
|
|
usage(void)
|
|
|
|
{
|
2009-07-01 13:07:02 +00:00
|
|
|
|
2010-02-08 20:57:49 +00:00
|
|
|
(void)fprintf(stderr, "usage: wake [interface] lladdr [lladdr ...]\n");
|
2009-07-01 22:23:26 +00:00
|
|
|
exit(1);
|
2009-06-30 18:51:22 +00:00
|
|
|
}
|
|
|
|
|
2009-07-01 13:07:02 +00:00
|
|
|
static int
|
2010-02-08 20:57:49 +00:00
|
|
|
wake(int bpf, const char *host)
|
2009-06-30 18:51:22 +00:00
|
|
|
{
|
|
|
|
struct ether_addr macaddr;
|
|
|
|
|
2010-02-08 20:57:49 +00:00
|
|
|
if (get_ether(host, &macaddr) == -1)
|
2009-07-01 13:07:02 +00:00
|
|
|
return (-1);
|
2010-02-08 20:57:49 +00:00
|
|
|
|
2012-02-24 07:49:55 +00:00
|
|
|
return (send_wakeup(bpf, &macaddr));
|
2009-06-30 18:51:22 +00:00
|
|
|
}
|
|
|
|
|
2009-07-01 13:07:02 +00:00
|
|
|
static int
|
2009-06-30 18:51:22 +00:00
|
|
|
bind_if_to_bpf(char const *ifname, int bpf)
|
|
|
|
{
|
|
|
|
struct ifreq ifr;
|
|
|
|
u_int dlt;
|
|
|
|
|
|
|
|
if (strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name)) >=
|
2010-02-08 20:57:49 +00:00
|
|
|
sizeof(ifr.ifr_name))
|
2009-07-01 13:07:02 +00:00
|
|
|
return (-1);
|
2010-02-08 20:57:49 +00:00
|
|
|
|
|
|
|
if (ioctl(bpf, BIOCSETIF, &ifr) == -1)
|
2009-07-01 13:07:02 +00:00
|
|
|
return (-1);
|
2010-02-08 20:57:49 +00:00
|
|
|
|
|
|
|
if (ioctl(bpf, BIOCGDLT, &dlt) == -1)
|
2009-07-01 13:07:02 +00:00
|
|
|
return (-1);
|
2010-02-08 20:57:49 +00:00
|
|
|
|
|
|
|
if (dlt != DLT_EN10MB)
|
2009-07-01 13:07:02 +00:00
|
|
|
return (-1);
|
2010-02-08 20:57:49 +00:00
|
|
|
|
2009-07-01 13:07:02 +00:00
|
|
|
return (0);
|
2009-06-30 18:51:22 +00:00
|
|
|
}
|
|
|
|
|
2010-02-08 20:57:49 +00:00
|
|
|
static int
|
|
|
|
find_ether(char *dst, size_t len)
|
|
|
|
{
|
|
|
|
struct ifaddrs *ifap, *ifa;
|
|
|
|
struct sockaddr_dl *sdl = NULL;
|
|
|
|
int nifs;
|
|
|
|
|
|
|
|
if (dst == NULL || len == 0)
|
2012-02-24 07:49:55 +00:00
|
|
|
return (0);
|
2010-02-08 20:57:49 +00:00
|
|
|
|
|
|
|
if (getifaddrs(&ifap) != 0)
|
2012-02-24 07:49:55 +00:00
|
|
|
return (-1);
|
2010-02-08 20:57:49 +00:00
|
|
|
|
|
|
|
/* XXX also check the link state */
|
|
|
|
for (nifs = 0, ifa = ifap; ifa; ifa = ifa->ifa_next)
|
|
|
|
if (ifa->ifa_addr->sa_family == AF_LINK &&
|
|
|
|
ifa->ifa_flags & IFF_UP && ifa->ifa_flags & IFF_RUNNING) {
|
|
|
|
sdl = (struct sockaddr_dl *)ifa->ifa_addr;
|
|
|
|
if (sdl->sdl_type == IFT_ETHER) {
|
|
|
|
strlcpy(dst, ifa->ifa_name, len);
|
|
|
|
nifs++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
freeifaddrs(ifap);
|
2012-02-24 07:49:55 +00:00
|
|
|
return (nifs == 1 ? 0 : -1);
|
2010-02-08 20:57:49 +00:00
|
|
|
}
|
|
|
|
|
2009-07-01 13:07:02 +00:00
|
|
|
static int
|
2009-06-30 18:51:22 +00:00
|
|
|
get_ether(char const *text, struct ether_addr *addr)
|
|
|
|
{
|
|
|
|
struct ether_addr *paddr;
|
|
|
|
|
|
|
|
paddr = ether_aton(text);
|
|
|
|
if (paddr != NULL) {
|
|
|
|
*addr = *paddr;
|
2009-07-01 13:07:02 +00:00
|
|
|
return (0);
|
2009-06-30 18:51:22 +00:00
|
|
|
}
|
2009-07-01 13:07:02 +00:00
|
|
|
if (ether_hostton(text, addr)) {
|
|
|
|
warnx("no match for host %s found", text);
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
return (0);
|
2009-06-30 18:51:22 +00:00
|
|
|
}
|
|
|
|
|
2009-07-01 13:07:02 +00:00
|
|
|
static int
|
2009-06-30 18:51:22 +00:00
|
|
|
send_wakeup(int bpf, struct ether_addr const *addr)
|
|
|
|
{
|
|
|
|
struct {
|
|
|
|
struct ether_header hdr;
|
|
|
|
u_char data[SYNC_LEN + ETHER_ADDR_LEN * DESTADDR_COUNT];
|
2009-07-01 13:07:02 +00:00
|
|
|
} __packed pkt;
|
2009-07-01 22:23:26 +00:00
|
|
|
u_char *p;
|
2009-06-30 18:51:22 +00:00
|
|
|
ssize_t bw;
|
|
|
|
ssize_t len;
|
2009-07-01 13:07:02 +00:00
|
|
|
int i;
|
2009-06-30 18:51:22 +00:00
|
|
|
|
|
|
|
(void)memset(pkt.hdr.ether_dhost, 0xff, sizeof(pkt.hdr.ether_dhost));
|
|
|
|
pkt.hdr.ether_type = htons(0);
|
|
|
|
(void)memset(pkt.data, 0xff, SYNC_LEN);
|
|
|
|
for (p = pkt.data + SYNC_LEN, i = 0; i < DESTADDR_COUNT;
|
|
|
|
p += ETHER_ADDR_LEN, i++)
|
|
|
|
bcopy(addr->octet, p, ETHER_ADDR_LEN);
|
|
|
|
p = (u_char *)&pkt;
|
|
|
|
len = sizeof(pkt);
|
|
|
|
bw = 0;
|
|
|
|
while (len) {
|
2009-07-01 13:07:02 +00:00
|
|
|
if ((bw = write(bpf, p, len)) == -1) {
|
|
|
|
warn("write()");
|
|
|
|
return (-1);
|
|
|
|
}
|
2009-06-30 18:51:22 +00:00
|
|
|
len -= bw;
|
|
|
|
p += bw;
|
|
|
|
}
|
2009-07-01 13:07:02 +00:00
|
|
|
return (0);
|
2009-06-30 18:51:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
2012-02-24 07:54:08 +00:00
|
|
|
int bpf, n, rval;
|
2010-02-08 20:57:49 +00:00
|
|
|
char ifname[IF_NAMESIZE];
|
|
|
|
|
|
|
|
if (argc < 2)
|
|
|
|
usage();
|
|
|
|
|
|
|
|
if ((bpf = open(_PATH_BPF, O_RDWR)) == -1)
|
|
|
|
err(1, "Cannot open bpf interface");
|
|
|
|
|
|
|
|
n = 2;
|
|
|
|
if (bind_if_to_bpf(argv[1], bpf) == -1) {
|
|
|
|
if (find_ether(ifname, sizeof(ifname)))
|
|
|
|
err(1, "Failed to determine ethernet interface");
|
|
|
|
if (bind_if_to_bpf(ifname, bpf) == -1)
|
|
|
|
err(1, "Cannot bind to interface `%s'", ifname);
|
|
|
|
--n;
|
|
|
|
} else
|
|
|
|
strlcpy(ifname, argv[1], sizeof(ifname));
|
2009-06-30 18:51:22 +00:00
|
|
|
|
2010-02-08 20:57:49 +00:00
|
|
|
if (n >= argc)
|
2009-06-30 18:51:22 +00:00
|
|
|
usage();
|
2012-02-24 07:54:08 +00:00
|
|
|
rval = 0;
|
|
|
|
for (; n < argc; n++) {
|
|
|
|
if (wake(bpf, argv[n]) != 0) {
|
|
|
|
rval = 1;
|
2010-02-08 20:57:49 +00:00
|
|
|
warn("Cannot send Wake on LAN frame over `%s' to `%s'",
|
|
|
|
ifname, argv[n]);
|
2012-02-24 07:54:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
exit(rval);
|
2009-06-30 18:51:22 +00:00
|
|
|
}
|