3 * Bill Paul <wpaul@windriver.com>. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
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.
44 #include <sys/types.h>
45 #include <sys/param.h>
46 #include <sys/socket.h>
47 #include <sys/ioctl.h>
48 #include <sys/errno.h>
49 #include <sys/sysctl.h>
51 #include <net/if_dl.h>
53 #include <netinet/in.h>
54 #include <arpa/inet.h>
56 #include <net/route.h>
66 static int verbose = 0;
68 static int all_events = 0;
70 #define PROGNAME "ndis_events"
72 #define WPA_SUPPLICANT_PORT 9876
73 #define NDIS_INDICATION_LEN 2048
75 #define EVENT_CONNECT 0
76 #define EVENT_DISCONNECT 1
77 #define EVENT_MEDIA_SPECIFIC 2
79 #define NDIS_STATUS_MEDIA_CONNECT 0x4001000B
80 #define NDIS_STATUS_MEDIA_DISCONNECT 0x4001000C
81 #define NDIS_STATUS_MEDIA_SPECIFIC_INDICATION 0x40010012
91 static int find_ifname(int, char *);
92 static int announce_event(char *, int, struct sockaddr_in *);
93 static void usage(void);
96 dbgmsg(const char *fmt, ...)
104 vsyslog(LOG_ERR, fmt, ap);
111 find_ifname(idx, name)
117 struct if_msghdr *ifm;
118 struct sockaddr_dl *sdl;
119 char *buf, *lim, *next;
124 mib[2] = 0; /* protocol */
125 mib[3] = 0; /* wildcard address family */
126 mib[4] = NET_RT_IFLIST;
127 mib[5] = 0; /* no flags */
129 if (sysctl (mib, 6, NULL, &needed, NULL, 0) < 0)
132 buf = malloc (needed);
136 if (sysctl (mib, 6, buf, &needed, NULL, 0) < 0) {
145 ifm = (struct if_msghdr *)next;
146 if (ifm->ifm_type == RTM_IFINFO) {
147 sdl = (struct sockaddr_dl *)(ifm + 1);
148 if (ifm->ifm_index == idx) {
149 strncpy(name, sdl->sdl_data, sdl->sdl_nlen);
150 name[sdl->sdl_nlen] = '\0';
155 next += ifm->ifm_msglen;
164 announce_event(ifname, sock, dst)
167 struct sockaddr_in *dst;
170 char indication[NDIS_INDICATION_LEN];
173 char buf[512], *pos, *end;
174 int len, type, _type;
176 s = socket(PF_INET, SOCK_DGRAM, 0);
179 dbgmsg("socket creation failed");
183 bzero((char *)&ifr, sizeof(ifr));
184 e = (struct ndis_evt *)indication;
185 e->ne_len = NDIS_INDICATION_LEN - sizeof(struct ndis_evt);
187 strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
188 ifr.ifr_data = indication;
190 if (ioctl(s, SIOCGPRIVATE_0, &ifr) < 0) {
194 dbgmsg("drained all events from %s",
197 dbgmsg("failed to read event info from %s: %d",
203 if (e->ne_sts == NDIS_STATUS_MEDIA_CONNECT) {
204 type = EVENT_CONNECT;
206 dbgmsg("Received a connect event for %s", ifname);
212 if (e->ne_sts == NDIS_STATUS_MEDIA_DISCONNECT) {
213 type = EVENT_DISCONNECT;
215 dbgmsg("Received a disconnect event for %s", ifname);
221 if (e->ne_sts == NDIS_STATUS_MEDIA_SPECIFIC_INDICATION) {
222 type = EVENT_MEDIA_SPECIFIC;
224 dbgmsg("Received a media-specific event for %s",
228 end = buf + sizeof(buf);
230 memcpy(buf, &_type, sizeof(_type));
231 pos = buf + sizeof(_type);
233 len = snprintf(pos + 1, end - pos - 1, "%s", ifname);
240 *pos = (unsigned char) len;
243 if (e->ne_len > 255 || 1 + e->ne_len > end - pos) {
244 dbgmsg("Not enough room for send_event data (%d)\n",
249 *pos++ = (unsigned char) e->ne_len;
250 memcpy(pos, (indication) + sizeof(struct ndis_evt), e->ne_len);
254 len = sendto(sock, buf, pos - buf, 0, (struct sockaddr *) dst,
255 sizeof(struct sockaddr_in));
264 fprintf(stderr, "Usage: ndis_events [-a] [-d] [-v]\n");
274 struct sockaddr_in sin;
275 char msg[NDIS_INDICATION_LEN];
276 struct rt_msghdr *rtm;
277 struct if_msghdr *ifm;
278 char ifname[IFNAMSIZ];
281 while ((ch = getopt(argc, argv, "dva")) != -1) {
298 if (!debug && daemon(0, 0))
299 err(1, "failed to daemonize ourselves");
302 openlog(PROGNAME, LOG_PID | LOG_CONS, LOG_DAEMON);
304 bzero((char *)&sin, sizeof(sin));
306 /* Create a datagram socket. */
308 s = socket(PF_INET, SOCK_DGRAM, 0);
310 dbgmsg("socket creation failed");
314 sin.sin_family = AF_INET;
315 sin.sin_addr.s_addr = inet_addr("127.0.0.1");
316 sin.sin_port = htons(WPA_SUPPLICANT_PORT);
318 /* Create a routing socket. */
320 r = socket (PF_ROUTE, SOCK_RAW, 0);
322 dbgmsg("routing socket creation failed");
326 /* Now sit and spin, waiting for events. */
329 dbgmsg("Listening for events");
332 n = read(r, msg, NDIS_INDICATION_LEN);
333 rtm = (struct rt_msghdr *)msg;
334 if (rtm->rtm_type != RTM_IFINFO)
336 ifm = (struct if_msghdr *)msg;
337 if (find_ifname(ifm->ifm_index, ifname))
339 if (strstr(ifname, "ndis")) {
340 while(announce_event(ifname, s, &sin) == 0)
344 dbgmsg("Skipping ifinfo message from %s",