2012-12-15 08:40:33 +04:00
|
|
|
/* $NetBSD: wakeonlan.c,v 1.2 2012/12/15 04:40:33 jakllsch Exp $ */
|
2009-06-25 22:50:46 +04:00
|
|
|
|
2009-06-25 22:41:21 +04:00
|
|
|
/*
|
2010-01-03 20:58:14 +03:00
|
|
|
* Copyright (C) 2006, 2007, 2008, 2009, 2010 Marc Balmer <marc@msys.ch>
|
2009-06-25 22:41:21 +04: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.
|
|
|
|
*/
|
2009-06-25 22:50:46 +04:00
|
|
|
|
2010-01-03 20:58:14 +03:00
|
|
|
/* Send Wake on LAN packets to hosts on a local Ethernet network */
|
2009-06-25 22:41:21 +04:00
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/queue.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/time.h>
|
2010-01-03 20:58:14 +03:00
|
|
|
|
2009-06-25 22:41:21 +04:00
|
|
|
#include <net/bpf.h>
|
|
|
|
#include <net/if.h>
|
2010-01-03 20:58:14 +03:00
|
|
|
#include <net/if_dl.h>
|
|
|
|
#include <net/if_types.h>
|
2009-06-25 22:41:21 +04:00
|
|
|
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <netinet/if_ether.h>
|
|
|
|
|
|
|
|
#include <err.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
2010-01-03 20:58:14 +03:00
|
|
|
#include <ifaddrs.h>
|
2009-06-25 22:41:21 +04:00
|
|
|
#include <limits.h>
|
2009-06-26 13:00:49 +04:00
|
|
|
#include <paths.h>
|
2009-06-25 22:41:21 +04:00
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sysexits.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#ifndef SYNC_LEN
|
|
|
|
#define SYNC_LEN 6
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef DESTADDR_COUNT
|
|
|
|
#define DESTADDR_COUNT 16
|
|
|
|
#endif
|
|
|
|
|
2011-08-31 17:32:36 +04:00
|
|
|
__dead static void usage(void);
|
2009-06-26 21:38:32 +04:00
|
|
|
static int wake(int, const char *);
|
|
|
|
static int bind_if_to_bpf(char const *, int);
|
2010-01-03 20:58:14 +03:00
|
|
|
static int find_ether(char *, size_t);
|
2009-06-26 21:38:32 +04:00
|
|
|
static int get_ether(char const *, struct ether_addr *);
|
|
|
|
static int send_wakeup(int, struct ether_addr const *);
|
2009-06-25 22:41:21 +04:00
|
|
|
|
2009-06-26 21:38:32 +04:00
|
|
|
static void
|
2009-06-25 22:41:21 +04:00
|
|
|
usage(void)
|
|
|
|
{
|
2010-01-10 22:40:11 +03:00
|
|
|
fprintf(stderr, "usage: %s [interface] lladdr [lladdr ...]\n",
|
2009-12-21 11:42:39 +03:00
|
|
|
getprogname());
|
2009-07-18 12:35:19 +04:00
|
|
|
exit(EXIT_FAILURE);
|
2009-06-25 22:41:21 +04:00
|
|
|
}
|
|
|
|
|
2009-06-26 21:38:32 +04:00
|
|
|
static int
|
|
|
|
wake(int bpf, const char *host)
|
2009-06-25 22:41:21 +04:00
|
|
|
{
|
|
|
|
struct ether_addr macaddr;
|
|
|
|
|
2009-06-26 21:38:32 +04:00
|
|
|
if (get_ether(host, &macaddr) == -1)
|
2009-06-25 22:41:21 +04:00
|
|
|
return -1;
|
2009-06-26 21:38:32 +04:00
|
|
|
|
|
|
|
return send_wakeup(bpf, &macaddr);
|
2009-06-25 22:41:21 +04:00
|
|
|
}
|
|
|
|
|
2009-06-26 21:38:32 +04:00
|
|
|
static int
|
2009-06-25 22:41:21 +04: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)) >=
|
2009-06-26 21:38:32 +04:00
|
|
|
sizeof(ifr.ifr_name)) {
|
|
|
|
errno = ENAMETOOLONG;
|
2009-06-25 22:41:21 +04:00
|
|
|
return -1;
|
2009-06-26 21:38:32 +04:00
|
|
|
}
|
2009-06-25 22:41:21 +04:00
|
|
|
if (ioctl(bpf, BIOCSETIF, &ifr) == -1)
|
|
|
|
return -1;
|
|
|
|
if (ioctl(bpf, BIOCGDLT, &dlt) == -1)
|
|
|
|
return -1;
|
2009-06-26 21:38:32 +04:00
|
|
|
if (dlt != DLT_EN10MB) {
|
|
|
|
errno = EOPNOTSUPP;
|
2009-06-25 22:41:21 +04:00
|
|
|
return -1;
|
2009-06-26 21:38:32 +04:00
|
|
|
}
|
2009-06-25 22:41:21 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-01-03 20:58:14 +03:00
|
|
|
static int
|
|
|
|
find_ether(char *dst, size_t len)
|
|
|
|
{
|
|
|
|
struct ifaddrs *ifap, *ifa;
|
|
|
|
struct sockaddr_dl *sdl = NULL;
|
2010-01-04 14:34:39 +03:00
|
|
|
int nifs;
|
2010-01-03 20:58:14 +03:00
|
|
|
|
|
|
|
if (dst == NULL || len == 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (getifaddrs(&ifap) != 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
/* XXX also check the link state */
|
2010-01-04 14:34:39 +03:00
|
|
|
for (nifs = 0, ifa = ifap; ifa; ifa = ifa->ifa_next)
|
2010-01-03 20:58:14 +03:00
|
|
|
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);
|
2010-01-04 14:34:39 +03:00
|
|
|
nifs++;
|
2010-01-03 20:58:14 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
freeifaddrs(ifap);
|
2010-01-04 14:34:39 +03:00
|
|
|
return nifs == 1 ? 0 : -1;
|
2010-01-03 20:58:14 +03:00
|
|
|
}
|
|
|
|
|
2009-06-26 21:38:32 +04:00
|
|
|
static int
|
2009-06-25 22:41:21 +04:00
|
|
|
get_ether(char const *text, struct ether_addr *addr)
|
|
|
|
{
|
|
|
|
struct ether_addr *paddr;
|
|
|
|
|
|
|
|
paddr = ether_aton(text);
|
|
|
|
if (paddr != NULL) {
|
|
|
|
*addr = *paddr;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (ether_hostton(text, addr))
|
|
|
|
return -1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-06-26 21:38:32 +04:00
|
|
|
static int
|
2009-06-25 22:41:21 +04: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];
|
|
|
|
} pkt;
|
|
|
|
u_char *p;
|
|
|
|
int i;
|
2010-01-03 20:58:14 +03:00
|
|
|
ssize_t bw, len;
|
2009-06-25 22:41:21 +04:00
|
|
|
|
2010-01-10 22:40:11 +03:00
|
|
|
memset(pkt.hdr.ether_dhost, 0xff, sizeof(pkt.hdr.ether_dhost));
|
2012-12-15 08:40:33 +04:00
|
|
|
memset(pkt.hdr.ether_shost, 0x00, sizeof(pkt.hdr.ether_shost));
|
2009-06-25 22:41:21 +04:00
|
|
|
pkt.hdr.ether_type = htons(0);
|
2010-01-10 22:40:11 +03:00
|
|
|
memset(pkt.data, 0xff, SYNC_LEN);
|
2009-06-25 22:41:21 +04:00
|
|
|
for (p = pkt.data + SYNC_LEN, i = 0; i < DESTADDR_COUNT;
|
|
|
|
p += ETHER_ADDR_LEN, i++)
|
2010-01-10 22:40:11 +03:00
|
|
|
memcpy(p, addr->ether_addr_octet, ETHER_ADDR_LEN);
|
2009-06-26 21:38:32 +04:00
|
|
|
p = (u_char *)(void *)&pkt;
|
2009-06-25 22:41:21 +04:00
|
|
|
len = sizeof(pkt);
|
|
|
|
bw = 0;
|
|
|
|
while (len) {
|
2009-07-01 11:49:12 +04:00
|
|
|
if ((bw = write(bpf, p, len)) == -1)
|
2009-06-25 22:41:21 +04:00
|
|
|
return -1;
|
|
|
|
len -= bw;
|
|
|
|
p += bw;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
2009-06-26 21:38:32 +04:00
|
|
|
int bpf, n;
|
2010-01-03 20:58:14 +03:00
|
|
|
char ifname[IF_NAMESIZE];
|
2009-06-25 22:41:21 +04:00
|
|
|
|
2010-01-03 20:58:14 +03:00
|
|
|
if (argc < 2)
|
2009-06-25 22:41:21 +04:00
|
|
|
usage();
|
|
|
|
|
2009-06-26 21:38:32 +04:00
|
|
|
if ((bpf = open(_PATH_BPF, O_RDWR)) == -1)
|
2009-07-18 12:35:19 +04:00
|
|
|
err(EXIT_FAILURE, "Cannot open bpf interface");
|
2009-06-26 21:38:32 +04:00
|
|
|
|
2010-01-03 20:58:14 +03:00
|
|
|
n = 2;
|
|
|
|
if (bind_if_to_bpf(argv[1], bpf) == -1) {
|
|
|
|
if (find_ether(ifname, sizeof(ifname)))
|
|
|
|
err(EXIT_FAILURE, "Failed to determine ethernet "
|
|
|
|
"interface");
|
|
|
|
if (bind_if_to_bpf(ifname, bpf) == -1)
|
|
|
|
err(EXIT_FAILURE, "Cannot bind to interface `%s'",
|
|
|
|
ifname);
|
|
|
|
--n;
|
|
|
|
} else
|
|
|
|
strlcpy(ifname, argv[1], sizeof(ifname));
|
|
|
|
|
|
|
|
if (n >= argc)
|
|
|
|
usage();
|
|
|
|
for (; n < argc; n++)
|
2009-06-26 21:38:32 +04:00
|
|
|
if (wake(bpf, argv[n]))
|
|
|
|
warn("Cannot send Wake on LAN frame over `%s' to `%s'",
|
2010-01-03 20:58:14 +03:00
|
|
|
ifname, argv[n]);
|
2009-07-18 12:35:19 +04:00
|
|
|
return EXIT_SUCCESS;
|
2009-06-25 22:41:21 +04:00
|
|
|
}
|