Home | History | Annotate | Line # | Download | only in wakeonlan
      1 /* $NetBSD: wakeonlan.c,v 1.2 2012/12/15 04:40:33 jakllsch Exp $ */
      2 
      3 /*
      4  * Copyright (C) 2006, 2007, 2008, 2009, 2010 Marc Balmer <marc (at) 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 /* Send Wake on LAN packets to hosts on a local Ethernet network */
     30 
     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>
     36 #include <sys/time.h>
     37 
     38 #include <net/bpf.h>
     39 #include <net/if.h>
     40 #include <net/if_dl.h>
     41 #include <net/if_types.h>
     42 
     43 #include <netinet/in.h>
     44 #include <netinet/if_ether.h>
     45 
     46 #include <err.h>
     47 #include <errno.h>
     48 #include <fcntl.h>
     49 #include <ifaddrs.h>
     50 #include <limits.h>
     51 #include <paths.h>
     52 #include <stdarg.h>
     53 #include <stdio.h>
     54 #include <stdlib.h>
     55 #include <string.h>
     56 #include <sysexits.h>
     57 #include <unistd.h>
     58 
     59 #ifndef SYNC_LEN
     60 #define SYNC_LEN 6
     61 #endif
     62 
     63 #ifndef DESTADDR_COUNT
     64 #define DESTADDR_COUNT 16
     65 #endif
     66 
     67 __dead 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 *);
     73 
     74 static void
     75 usage(void)
     76 {
     77 	fprintf(stderr, "usage: %s [interface] lladdr [lladdr ...]\n",
     78 	    getprogname());
     79 	exit(EXIT_FAILURE);
     80 }
     81 
     82 static int
     83 wake(int bpf, const char *host)
     84 {
     85 	struct ether_addr macaddr;
     86 
     87 	if (get_ether(host, &macaddr) == -1)
     88 		return -1;
     89 
     90 	return send_wakeup(bpf, &macaddr);
     91 }
     92 
     93 static int
     94 bind_if_to_bpf(char const *ifname, int bpf)
     95 {
     96 	struct ifreq ifr;
     97 	u_int dlt;
     98 
     99 	if (strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name)) >=
    100 	    sizeof(ifr.ifr_name)) {
    101 		errno = ENAMETOOLONG;
    102 		return -1;
    103 	}
    104 	if (ioctl(bpf, BIOCSETIF, &ifr) == -1)
    105 		return -1;
    106 	if (ioctl(bpf, BIOCGDLT, &dlt) == -1)
    107 		return -1;
    108 	if (dlt != DLT_EN10MB) {
    109 		errno = EOPNOTSUPP;
    110 		return -1;
    111 	}
    112 	return 0;
    113 }
    114 
    115 static int
    116 find_ether(char *dst, size_t len)
    117 {
    118 	struct ifaddrs *ifap, *ifa;
    119 	struct sockaddr_dl *sdl = NULL;
    120 	int nifs;
    121 
    122 	if (dst == NULL || len == 0)
    123 		return 0;
    124 
    125 	if (getifaddrs(&ifap) != 0)
    126 		return -1;
    127 
    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);
    135 				nifs++;
    136 			}
    137 		}
    138 
    139 	freeifaddrs(ifap);
    140 	return nifs == 1 ? 0 : -1;
    141 }
    142 
    143 static int
    144 get_ether(char const *text, struct ether_addr *addr)
    145 {
    146 	struct ether_addr *paddr;
    147 
    148 	paddr = ether_aton(text);
    149 	if (paddr != NULL) {
    150 		*addr = *paddr;
    151 		return 0;
    152 	}
    153 	if (ether_hostton(text, addr))
    154 		return -1;
    155 	return 0;
    156 }
    157 
    158 static int
    159 send_wakeup(int bpf, struct ether_addr const *addr)
    160 {
    161 	struct {
    162 		struct ether_header hdr;
    163 		u_char data[SYNC_LEN + ETHER_ADDR_LEN * DESTADDR_COUNT];
    164 	} pkt;
    165 	u_char *p;
    166 	int i;
    167 	ssize_t bw, len;
    168 
    169 	memset(pkt.hdr.ether_dhost, 0xff, sizeof(pkt.hdr.ether_dhost));
    170 	memset(pkt.hdr.ether_shost, 0x00, sizeof(pkt.hdr.ether_shost));
    171 	pkt.hdr.ether_type = htons(0);
    172 	memset(pkt.data, 0xff, SYNC_LEN);
    173 	for (p = pkt.data + SYNC_LEN, i = 0; i < DESTADDR_COUNT;
    174 	    p += ETHER_ADDR_LEN, i++)
    175 		memcpy(p, addr->ether_addr_octet, ETHER_ADDR_LEN);
    176 	p = (u_char *)(void *)&pkt;
    177 	len = sizeof(pkt);
    178 	bw = 0;
    179 	while (len) {
    180 		if ((bw = write(bpf, p, len)) == -1)
    181 			return -1;
    182 		len -= bw;
    183 		p += bw;
    184 	}
    185 	return 0;
    186 }
    187 
    188 int
    189 main(int argc, char *argv[])
    190 {
    191 	int bpf, n;
    192 	char ifname[IF_NAMESIZE];
    193 
    194 	if (argc < 2)
    195 		usage();
    196 
    197 	if ((bpf = open(_PATH_BPF, O_RDWR)) == -1)
    198 		err(EXIT_FAILURE, "Cannot open bpf interface");
    199 
    200 	n = 2;
    201 	if (bind_if_to_bpf(argv[1], bpf) == -1) {
    202 		if (find_ether(ifname, sizeof(ifname)))
    203 			err(EXIT_FAILURE, "Failed to determine ethernet "
    204 			    "interface");
    205 		if (bind_if_to_bpf(ifname, bpf) == -1)
    206 			err(EXIT_FAILURE, "Cannot bind to interface `%s'",
    207 			    ifname);
    208 		--n;
    209 	} else
    210 		strlcpy(ifname, argv[1], sizeof(ifname));
    211 
    212 	if (n >= argc)
    213 		usage();
    214 	for (; n < argc; n++)
    215 		if (wake(bpf, argv[n]))
    216 			warn("Cannot send Wake on LAN frame over `%s' to `%s'",
    217 			    ifname, argv[n]);
    218 	return EXIT_SUCCESS;
    219 }
    220