Setting up repository
[linux-libre-firmware.git] / carl9170fw / tools / src / wol.c
1 /*
2  * Copyright 2011, Christian Lamparter <chunkeey@googlemail.com>
3  *
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.
7  *
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.
12  *
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.
16  */
17
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <errno.h>
22 #include <unistd.h>
23 #include <stdbool.h>
24
25 #include <sys/types.h>
26 #include <sys/socket.h>
27
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 */
32
33 static int monitor_init(const char *ifname)
34 {
35         struct sockaddr_ll ll;
36         int monitor_sock;
37
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);
43                 return -1;
44         }
45
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));
49                 return -1;
50         }
51
52         if (bind(monitor_sock, (struct sockaddr *) &ll, sizeof(ll)) < 0) {
53                 fprintf(stderr, "bind(PACKET): %s\n", strerror(errno));
54                 close(monitor_sock);
55                 return -1;
56         }
57
58         return monitor_sock;
59 }
60
61 static int inject_frame(int s, const void *data, size_t len)
62 {
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) */
69                 0x00,       /* padding */
70                 0x00, 0x00, /* RX and TX flags to indicate that */
71                 0x00, 0x00, /* this is the injected frame directly */
72         };
73         struct iovec iov[2] = {
74                 {
75                         .iov_base = &rtap_hdr,
76                         .iov_len = sizeof(rtap_hdr),
77                 },
78                 {
79                         .iov_base = (void *) data,
80                         .iov_len = len,
81                 }
82         };
83         struct msghdr msg = {
84                 .msg_name = NULL,
85                 .msg_namelen = 0,
86                 .msg_iov = iov,
87                 .msg_iovlen = 2,
88                 .msg_control = NULL,
89                 .msg_controllen = 0,
90                 .msg_flags = 0,
91         };
92         int ret;
93
94         ret = sendmsg(s, &msg, 0);
95         if (ret < 0)
96                 perror("sendmsg");
97         return ret;
98 }
99
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 */
105         0x00, 0x00,
106
107         0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
108 };
109
110 static void prepare_wol(unsigned char *wol_magic, unsigned char *mac)
111 {
112         int i;
113
114         for (i = 0; i < 16; i++)
115                 memcpy(&wol_magic[30 + i * 6], mac, 6);
116 }
117
118 void usage(void)
119 {
120         fprintf(stderr, "Usage:\n");
121         fprintf(stderr, "\twol -i monitor_dev -m DE:VI:CE:MA:CW:OL -n #num -v\n");
122
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");
127
128         exit(EXIT_FAILURE);
129 }
130
131 #define MAC_STR "%2X:%2X:%2X:%2X:%2X:%2X"
132
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)
135
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)
138
139 int main(int argc, char **args)
140 {
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;
145
146         while ((opt = getopt(argc, args, "m:i:n:v")) != -EXIT_FAILURE) {
147                 switch (opt) {
148                 case 'i':
149                         has_dev = true;
150                         strncpy(dev_name, optarg, IFNAMSIZ);
151                         break;
152                 case 'm':
153                         has_mac = true;
154                         err = sscanf(optarg, MAC_STR, MAC_ARG(mac)) != 6;
155                         if (err)
156                                 fprintf(stderr, "invalid MAC: \"%s\"\n", optarg);
157                         break;
158
159                 case 'n':
160                         err = sscanf(optarg, "%d", &num) != 1;
161                         err |= num < 1 | num > 1000;
162                         if (err)
163                                 fprintf(stderr, "invalid tries: \"%s\"\n", optarg);
164                         break;
165
166                 case 'v':
167                         verbose = true;
168                         break;
169
170                 default:
171                         err = -EINVAL;
172                         break;
173                 }
174
175                 if (err)
176                         break;
177         }
178
179         if (!has_mac || !has_dev || err)
180                 usage();
181
182         if (verbose)
183                 fprintf(stdout, "Opening monitor injection interface [%s].\n", dev_name);
184
185         sock = monitor_init(dev_name);
186         if (sock < 0)
187                 return EXIT_FAILURE;
188
189         if (verbose)
190                 fprintf(stdout, "Generating %d WOL packet for ["MAC_STR"].\n", num, MAC_ARG2(mac));
191
192         prepare_wol(wol_magic_tmpl, mac);
193
194         while (num--) {
195                 err = inject_frame(sock, wol_magic_tmpl, sizeof(wol_magic_tmpl));
196                 if (err < 0) {
197                         fprintf(stderr, "failed to send WOL packet.\n");
198                         break;
199                 } else if (verbose) {
200                         fprintf(stdout, "WOL packet sent.\n");
201                 }
202         }
203
204         close(sock);
205         if (err < 0)
206                 return EXIT_FAILURE;
207
208         return 0;
209 }