2 * SPDX-License-Identifier: BSD-4-Clause
5 * Bill Paul <wpaul@windriver.com>. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by Bill Paul.
18 * 4. Neither the name of the author nor the names of any co-contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
26 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
32 * THE POSSIBILITY OF SUCH DAMAGE.
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
39 * This program simulates the behavior of the ndis_events utility
40 * supplied with wpa_supplicant for Windows. The original utility
41 * is designed to translate Windows WMI events. We don't have WMI,
42 * but we need to supply certain event info to wpa_supplicant in
43 * order to make WPA2 work correctly, so we fake up the interface.
46 #include <sys/types.h>
47 #include <sys/param.h>
48 #include <sys/socket.h>
49 #include <sys/ioctl.h>
50 #include <sys/errno.h>
51 #include <sys/sysctl.h>
53 #include <net/if_dl.h>
55 #include <netinet/in.h>
56 #include <arpa/inet.h>
58 #include <net/route.h>
68 static int verbose = 0;
70 static int all_events = 0;
72 #define PROGNAME "ndis_events"
74 #define WPA_SUPPLICANT_PORT 9876
75 #define NDIS_INDICATION_LEN 2048
77 #define EVENT_CONNECT 0
78 #define EVENT_DISCONNECT 1
79 #define EVENT_MEDIA_SPECIFIC 2
81 #define NDIS_STATUS_MEDIA_CONNECT 0x4001000B
82 #define NDIS_STATUS_MEDIA_DISCONNECT 0x4001000C
83 #define NDIS_STATUS_MEDIA_SPECIFIC_INDICATION 0x40010012
93 static int find_ifname(int, char *);
94 static int announce_event(char *, int, struct sockaddr_in *);
95 static void usage(void);
98 dbgmsg(const char *fmt, ...)
106 vsyslog(LOG_ERR, fmt, ap);
113 find_ifname(idx, name)
119 struct if_msghdr *ifm;
120 struct sockaddr_dl *sdl;
121 char *buf, *lim, *next;
126 mib[2] = 0; /* protocol */
127 mib[3] = 0; /* wildcard address family */
128 mib[4] = NET_RT_IFLIST;
129 mib[5] = 0; /* no flags */
131 if (sysctl (mib, 6, NULL, &needed, NULL, 0) < 0)
134 buf = malloc (needed);
138 if (sysctl (mib, 6, buf, &needed, NULL, 0) < 0) {
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';
157 next += ifm->ifm_msglen;
166 announce_event(ifname, sock, dst)
169 struct sockaddr_in *dst;
172 char indication[NDIS_INDICATION_LEN];
175 char buf[512], *pos, *end;
176 int len, type, _type;
178 s = socket(PF_INET, SOCK_DGRAM, 0);
181 dbgmsg("socket creation failed");
185 bzero((char *)&ifr, sizeof(ifr));
186 e = (struct ndis_evt *)indication;
187 e->ne_len = NDIS_INDICATION_LEN - sizeof(struct ndis_evt);
189 strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
190 ifr.ifr_data = indication;
192 if (ioctl(s, SIOCGPRIVATE_0, &ifr) < 0) {
196 dbgmsg("drained all events from %s",
199 dbgmsg("failed to read event info from %s: %d",
205 if (e->ne_sts == NDIS_STATUS_MEDIA_CONNECT) {
206 type = EVENT_CONNECT;
208 dbgmsg("Received a connect event for %s", ifname);
214 if (e->ne_sts == NDIS_STATUS_MEDIA_DISCONNECT) {
215 type = EVENT_DISCONNECT;
217 dbgmsg("Received a disconnect event for %s", ifname);
223 if (e->ne_sts == NDIS_STATUS_MEDIA_SPECIFIC_INDICATION) {
224 type = EVENT_MEDIA_SPECIFIC;
226 dbgmsg("Received a media-specific event for %s",
230 end = buf + sizeof(buf);
232 memcpy(buf, &_type, sizeof(_type));
233 pos = buf + sizeof(_type);
235 len = snprintf(pos + 1, end - pos - 1, "%s", ifname);
242 *pos = (unsigned char) len;
245 if (e->ne_len > 255 || 1 + e->ne_len > end - pos) {
246 dbgmsg("Not enough room for send_event data (%d)\n",
251 *pos++ = (unsigned char) e->ne_len;
252 memcpy(pos, (indication) + sizeof(struct ndis_evt), e->ne_len);
256 len = sendto(sock, buf, pos - buf, 0, (struct sockaddr *) dst,
257 sizeof(struct sockaddr_in));
266 fprintf(stderr, "Usage: ndis_events [-a] [-d] [-v]\n");
276 struct sockaddr_in sin;
277 char msg[NDIS_INDICATION_LEN];
278 struct rt_msghdr *rtm;
279 struct if_msghdr *ifm;
280 char ifname[IFNAMSIZ];
283 while ((ch = getopt(argc, argv, "dva")) != -1) {
300 if (!debug && daemon(0, 0))
301 err(1, "failed to daemonize ourselves");
304 openlog(PROGNAME, LOG_PID | LOG_CONS, LOG_DAEMON);
306 bzero((char *)&sin, sizeof(sin));
308 /* Create a datagram socket. */
310 s = socket(PF_INET, SOCK_DGRAM, 0);
312 dbgmsg("socket creation failed");
316 sin.sin_family = AF_INET;
317 sin.sin_addr.s_addr = inet_addr("127.0.0.1");
318 sin.sin_port = htons(WPA_SUPPLICANT_PORT);
320 /* Create a routing socket. */
322 r = socket (PF_ROUTE, SOCK_RAW, 0);
324 dbgmsg("routing socket creation failed");
328 /* Now sit and spin, waiting for events. */
331 dbgmsg("Listening for events");
334 n = read(r, msg, NDIS_INDICATION_LEN);
335 rtm = (struct rt_msghdr *)msg;
336 if (rtm->rtm_type != RTM_IFINFO)
338 ifm = (struct if_msghdr *)msg;
339 if (find_ifname(ifm->ifm_index, ifname))
341 if (strstr(ifname, "ndis")) {
342 while(announce_event(ifname, s, &sin) == 0)
346 dbgmsg("Skipping ifinfo message from %s",