2 * Copyright 2011, Christian Lamparter <chunkeey@googlemail.com>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation version 2 of the License.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 #include <sys/types.h>
26 #include <sys/socket.h>
28 #include <linux/types.h>
29 #include <linux/if_ether.h> /* ETH_P_ALL */
30 #include <linux/if_packet.h> /* sockaddr_ll */
31 #include <linux/if.h> /* IFNAMSIZ */
33 static int monitor_init(const char *ifname)
35 struct sockaddr_ll ll;
38 memset(&ll, 0, sizeof(ll));
39 ll.sll_family = AF_PACKET;
40 ll.sll_ifindex = if_nametoindex(ifname);
41 if (ll.sll_ifindex == 0) {
42 fprintf(stderr, "Monitor interface '%s' does not exist\n", ifname);
46 monitor_sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
47 if (monitor_sock < 0) {
48 fprintf(stderr, "socket(PF_PACKET,SOCK_RAW): %s\n", strerror(errno));
52 if (bind(monitor_sock, (struct sockaddr *) &ll, sizeof(ll)) < 0) {
53 fprintf(stderr, "bind(PACKET): %s\n", strerror(errno));
61 static int inject_frame(int s, const void *data, size_t len)
63 #define IEEE80211_RADIOTAP_F_FRAG 0x08
64 unsigned char rtap_hdr[] = {
65 0x00, 0x00, /* radiotap version */
66 0x0e, 0x00, /* radiotap length */
67 0x02, 0xc0, 0x00, 0x00, /* bmap: flags, tx and rx flags */
68 IEEE80211_RADIOTAP_F_FRAG, /* F_FRAG (fragment if required) */
70 0x00, 0x00, /* RX and TX flags to indicate that */
71 0x00, 0x00, /* this is the injected frame directly */
73 struct iovec iov[2] = {
75 .iov_base = &rtap_hdr,
76 .iov_len = sizeof(rtap_hdr),
79 .iov_base = (void *) data,
94 ret = sendmsg(s, &msg, 0);
100 static unsigned char wol_magic_tmpl[30 + 6 + 16 * 6] = {
101 0x08, 0x00, 0x00, 0x00,
102 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* RA */
103 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* TA */
104 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* SA */
107 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
110 static void prepare_wol(unsigned char *wol_magic, unsigned char *mac)
114 for (i = 0; i < 16; i++)
115 memcpy(&wol_magic[30 + i * 6], mac, 6);
120 fprintf(stderr, "Usage:\n");
121 fprintf(stderr, "\twol -i monitor_dev -m DE:VI:CE:MA:CW:OL -n #num -v\n");
123 fprintf(stderr, "\nDescription:\n");
124 fprintf(stderr, "\tThis utility generates a WOL packet for the"
125 "given [MAC] address and tries to injects"
126 "it into [monitor_dev]\n");
131 #define MAC_STR "%2X:%2X:%2X:%2X:%2X:%2X"
133 #define M(a, i) ((unsigned int *)&a[i])
134 #define MAC_ARG(a) M(a, 0), M(a, 1), M(a, 2), M(a, 3), M(a, 4), M(a, 5)
136 #define M2(a, i) (a[i])
137 #define MAC_ARG2(a) M2(a, 0), M2(a, 1), M2(a, 2), M2(a, 3), M2(a, 4), M2(a, 5)
139 int main(int argc, char **args)
141 int sock, err = 0, opt, num = 10;
142 unsigned char mac[ETH_ALEN];
143 char dev_name[IFNAMSIZ + 1] = { 0 };
144 bool has_mac = false, has_dev = false, verbose = false;
146 while ((opt = getopt(argc, args, "m:i:n:v")) != -EXIT_FAILURE) {
150 strncpy(dev_name, optarg, IFNAMSIZ);
154 err = sscanf(optarg, MAC_STR, MAC_ARG(mac)) != 6;
156 fprintf(stderr, "invalid MAC: \"%s\"\n", optarg);
160 err = sscanf(optarg, "%d", &num) != 1;
161 err |= num < 1 | num > 1000;
163 fprintf(stderr, "invalid tries: \"%s\"\n", optarg);
179 if (!has_mac || !has_dev || err)
183 fprintf(stdout, "Opening monitor injection interface [%s].\n", dev_name);
185 sock = monitor_init(dev_name);
190 fprintf(stdout, "Generating %d WOL packet for ["MAC_STR"].\n", num, MAC_ARG2(mac));
192 prepare_wol(wol_magic_tmpl, mac);
195 err = inject_frame(sock, wol_magic_tmpl, sizeof(wol_magic_tmpl));
197 fprintf(stderr, "failed to send WOL packet.\n");
199 } else if (verbose) {
200 fprintf(stdout, "WOL packet sent.\n");