Deleted Added
sdiff udiff text old ( 151214 ) new ( 151223 )
full compact
1/*-
2 * Copyright (c) 2005
3 * Bill Paul <wpaul@windriver.com>. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by Bill Paul.
16 * 4. Neither the name of the author nor the names of any co-contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
30 * THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33#include <sys/cdefs.h>
34__FBSDID("$FreeBSD: head/usr.sbin/wpa/ndis_events/ndis_events.c 151214 2005-10-10 17:51:12Z wpaul $");
35
36/*
37 * This program simulates the behavior of the ndis_events utility
38 * supplied with wpa_supplicant for Windows. The original utility
39 * is designed to translate Windows WMI events. We don't have WMI,
40 * but we need to supply certain event info to wpa_supplicant in
41 * order to make WPA2 work correctly, so we fake up the interface.
42 */
43
44#include <sys/types.h>
45#include <sys/cdefs.h>
46#include <sys/param.h>
47#include <sys/socket.h>
48#include <sys/ioctl.h>
49#include <sys/socket.h>
50#include <sys/errno.h>
51#include <sys/sysctl.h>
52#include <net/if.h>
53#include <net/if_dl.h>
54#include <net/if_var.h>
55
56#include <netinet/in.h>
57#include <arpa/inet.h>
58#include <netdb.h>
59#include <net/route.h>
60
61#include <stdio.h>
62#include <string.h>
63#include <stdlib.h>
64#include <unistd.h>
65#include <err.h>
66#include <syslog.h>
67#include <stdarg.h>
68
69static int verbose = 0;
70static int debug = 0;
71
72#define PROGNAME "ndis_events"
73
74#define WPA_SUPPLICANT_PORT 9876
75#define NDIS_INDICATION_LEN 2048
76
77#define EVENT_CONNECT 0
78#define EVENT_DISCONNECT 1
79#define EVENT_MEDIA_SPECIFIC 2
80
81#define NDIS_STATUS_MEDIA_CONNECT 0x4001000B
82#define NDIS_STATUS_MEDIA_DISCONNECT 0x4001000C
83#define NDIS_STATUS_MEDIA_SPECIFIC_INDICATION 0x40010012
84
85struct ndis_evt {
86 uint32_t ne_sts;
87 uint32_t ne_len;
88#ifdef notdef
89 char ne_buf[1];
90#endif
91};
92
93static int find_ifname(int, char *);
94static void announce_event(char *, int, struct sockaddr_in *);
95static void usage(char *);
96
97static void
98dbgmsg(const char *fmt, ...)
99{
100 va_list ap;
101
102 va_start(ap, fmt);
103 if (debug)
104 vwarnx(fmt, ap);
105 else
106 vsyslog(LOG_INFO, fmt, ap);
107 va_end(ap);
108
109 return;
110}
111
112static int
113find_ifname(idx, name)
114 int idx;
115 char *name;
116{
117 int mib[6];
118 size_t needed;
119 struct if_msghdr *ifm;
120 struct sockaddr_dl *sdl;
121 char *buf, *lim, *next;
122
123 needed = 0;
124 mib[0] = CTL_NET;
125 mib[1] = PF_ROUTE;
126 mib[2] = 0; /* protocol */
127 mib[3] = 0; /* wildcard address family */
128 mib[4] = NET_RT_IFLIST;
129 mib[5] = 0; /* no flags */
130
131 if (sysctl (mib, 6, NULL, &needed, NULL, 0) < 0)
132 return(EIO);
133
134 buf = malloc (needed);
135 if (buf == NULL)
136 return(ENOMEM);
137
138 if (sysctl (mib, 6, buf, &needed, NULL, 0) < 0) {
139 free(buf);
140 return(EIO);
141 }
142
143 lim = buf + needed;
144
145 next = buf;
146 while (next < lim) {
147 ifm = (struct if_msghdr *)next;
148 if (ifm->ifm_type == RTM_IFINFO) {
149 sdl = (struct sockaddr_dl *)(ifm + 1);
150 if (ifm->ifm_index == idx) {
151 strncpy(name, sdl->sdl_data, sdl->sdl_nlen);
152 name[sdl->sdl_nlen] = '\0';
153 free (buf);
154 return (0);
155 }
156 }
157 next += ifm->ifm_msglen;
158 }
159
160 free (buf);
161
162 return(ENOENT);
163}
164
165static void
166announce_event(ifname, sock, dst)
167 char *ifname;
168 int sock;
169 struct sockaddr_in *dst;
170{
171 int s;
172 char indication[NDIS_INDICATION_LEN];
173 struct ifreq ifr;
174 struct ndis_evt *e;
175 char buf[512], *pos, *end;
176 int len, type, _type;
177
178 s = socket(PF_INET, SOCK_DGRAM, 0);
179
180 if (s < 0)
181 return;
182
183 bzero((char *)&ifr, sizeof(ifr));
184 e = (struct ndis_evt *)indication;
185 e->ne_len = NDIS_INDICATION_LEN - sizeof(struct ndis_evt);
186
187 strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
188 ifr.ifr_data = indication;
189
190 if (ioctl(s, SIOCGPRIVATE_0, &ifr) < 0) {
191 close(s);
192 dbgmsg("failed to read event info from %s\n", ifname);
193 return;
194 }
195
196 if (e->ne_sts == NDIS_STATUS_MEDIA_CONNECT) {
197 type = EVENT_CONNECT;
198 if (verbose)
199 dbgmsg("Received a connect event for %s", ifname);
200 }
201 if (e->ne_sts == NDIS_STATUS_MEDIA_DISCONNECT) {
202 type = EVENT_DISCONNECT;
203 if (verbose)
204 dbgmsg("Received a disconnect event for %s", ifname);
205 }
206 if (e->ne_sts == NDIS_STATUS_MEDIA_SPECIFIC_INDICATION) {
207 type = EVENT_MEDIA_SPECIFIC;
208 if (verbose)
209 dbgmsg("Received a media-specific event for %s",
210 ifname);
211 }
212
213 end = buf + sizeof(buf);
214 _type = (int) type;
215 memcpy(buf, &_type, sizeof(_type));
216 pos = buf + sizeof(_type);
217
218 len = snprintf(pos + 1, end - pos - 1, "%s", ifname);
219 if (len < 0)
220 return;
221 if (len > 255)
222 len = 255;
223 *pos = (unsigned char) len;
224 pos += 1 + len;
225 if (e->ne_len) {
226 if (e->ne_len > 255 || 1 + e->ne_len > end - pos) {
227 dbgmsg("Not enough room for send_event data (%d)\n",
228 e->ne_len);
229 return;
230 }
231 *pos++ = (unsigned char) e->ne_len;
232 memcpy(pos, (indication) + sizeof(struct ndis_evt), e->ne_len);
233 pos += e->ne_len;
234 }
235
236 len = sendto(sock, buf, pos - buf, 0, (struct sockaddr *) dst,
237 sizeof(struct sockaddr_in));
238
239 close(s);
240 return;
241}
242
243static void
244usage(progname)
245 char *progname;
246{
247 fprintf(stderr, "Usage: ndis_events [-d] [-v]\n", progname);
248 exit(1);
249}
250
251int
252main(argc, argv)
253 int argc;
254 char *argv[];
255{
256 int s, r, n;
257 struct sockaddr_in sin;
258 char msg[NDIS_INDICATION_LEN];
259 struct rt_msghdr *rtm;
260 struct if_msghdr *ifm;
261 char ifname[IFNAMSIZ];
262 int ch;
263
264 while ((ch = getopt(argc, argv, "dv")) != -1) {
265 switch(ch) {
266 case 'd':
267 debug++;
268 break;
269 case 'v':
270 verbose++;
271 break;
272 default:
273 usage(PROGNAME);
274 break;
275 }
276 }
277
278 if (!debug && daemon(0, 0))
279 err(1, "failed to daemonize ourselves");
280
281 if (!debug)
282 openlog(PROGNAME, LOG_PID | LOG_CONS, LOG_DAEMON);
283
284 bzero((char *)&sin, sizeof(sin));
285
286 /* Create a datagram socket. */
287
288 s = socket(PF_INET, SOCK_DGRAM, 0);
289 if (s < 0) {
290 dbgmsg("socket creation failed");
291 exit(1);
292 }
293
294 sin.sin_family = AF_INET;
295 sin.sin_addr.s_addr = inet_addr("127.0.0.1");
296 sin.sin_port = htons(WPA_SUPPLICANT_PORT);
297
298 /* Create a routing socket. */
299
300 r = socket (PF_ROUTE, SOCK_RAW, 0);
301 if (r < 0) {
302 dbgmsg("routing socket creation failed");
303 exit(1);
304 }
305
306 /* Now sit and spin, waiting for events. */
307
308 if (verbose)
309 dbgmsg("Listening for events");
310
311 while (1) {
312 n = read(r, msg, NDIS_INDICATION_LEN);
313 rtm = (struct rt_msghdr *)msg;
314 if (rtm->rtm_type != RTM_IFINFO)
315 continue;
316 ifm = (struct if_msghdr *)msg;
317 if (find_ifname(ifm->ifm_index, ifname))
318 continue;
319 if (strstr(ifname, "ndis"))
320 announce_event(ifname, s, &sin);
321 else {
322 if (verbose)
323 dbgmsg("Skipping ifinfo message from %s",
324 ifname);
325 }
326 }
327
328 /* NOTREACHED */
329 exit(0);
330}