xref: /freebsd-13-stable/usr.sbin/wpa/ndis_events/ndis_events.c (revision 3d497e17ebd33fe0f58d773e35ab994d750258d6)
1 /*-
2  * SPDX-License-Identifier: BSD-4-Clause
3  *
4  * Copyright (c) 2005
5  *      Bill Paul <wpaul@windriver.com>.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
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.
21  *
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.
33  */
34 
35 #include <sys/cdefs.h>
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/param.h>
46 #include <sys/socket.h>
47 #include <sys/ioctl.h>
48 #include <sys/errno.h>
49 #include <sys/sysctl.h>
50 #include <net/if.h>
51 #include <net/if_dl.h>
52 
53 #include <netinet/in.h>
54 #include <arpa/inet.h>
55 #include <netdb.h>
56 #include <net/route.h>
57 
58 #include <stdio.h>
59 #include <string.h>
60 #include <stdlib.h>
61 #include <unistd.h>
62 #include <err.h>
63 #include <syslog.h>
64 #include <stdarg.h>
65 
66 static int verbose = 0;
67 static int debug = 0;
68 static int all_events = 0;
69 
70 #define PROGNAME "ndis_events"
71 
72 #define WPA_SUPPLICANT_PORT	9876
73 #define NDIS_INDICATION_LEN	2048
74 
75 #define EVENT_CONNECT		0
76 #define EVENT_DISCONNECT	1
77 #define EVENT_MEDIA_SPECIFIC	2
78 
79 #define NDIS_STATUS_MEDIA_CONNECT		0x4001000B
80 #define NDIS_STATUS_MEDIA_DISCONNECT		0x4001000C
81 #define NDIS_STATUS_MEDIA_SPECIFIC_INDICATION	0x40010012
82 
83 struct ndis_evt {
84 	uint32_t		ne_sts;
85 	uint32_t		ne_len;
86 #ifdef notdef
87 	char			ne_buf[1];
88 #endif
89 };
90 
91 static int find_ifname(int, char *);
92 static int announce_event(char *, int, struct sockaddr_in *);
93 static void usage(void);
94 
95 static void
dbgmsg(const char * fmt,...)96 dbgmsg(const char *fmt, ...)
97 {
98 	va_list			ap;
99 
100 	va_start(ap, fmt);
101 	if (debug)
102 		vwarnx(fmt, ap);
103 	else
104 		vsyslog(LOG_ERR, fmt, ap);
105 	va_end(ap);
106 
107 	return;
108 }
109 
110 static int
find_ifname(idx,name)111 find_ifname(idx, name)
112 	int			idx;
113 	char			*name;
114 {
115 	int			mib[6];
116 	size_t			needed;
117 	struct if_msghdr	*ifm;
118 	struct sockaddr_dl	*sdl;
119 	char			*buf, *lim, *next;
120 
121 	needed = 0;
122 	mib[0] = CTL_NET;
123 	mib[1] = PF_ROUTE;
124 	mib[2] = 0;             /* protocol */
125 	mib[3] = 0;             /* wildcard address family */
126 	mib[4] = NET_RT_IFLIST;
127 	mib[5] = 0;             /* no flags */
128 
129 	if (sysctl (mib, 6, NULL, &needed, NULL, 0) < 0)
130 		return(EIO);
131 
132 	buf = malloc (needed);
133 	if (buf == NULL)
134 		return(ENOMEM);
135 
136 	if (sysctl (mib, 6, buf, &needed, NULL, 0) < 0) {
137 		free(buf);
138 		return(EIO);
139 	}
140 
141 	lim = buf + needed;
142 
143 	next = buf;
144 	while (next < lim) {
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';
151 				free (buf);
152 				return (0);
153 			}
154 		}
155 		next += ifm->ifm_msglen;
156 	}
157 
158 	free (buf);
159 
160 	return(ENOENT);
161 }
162 
163 static int
announce_event(ifname,sock,dst)164 announce_event(ifname, sock, dst)
165 	char			*ifname;
166 	int			sock;
167 	struct sockaddr_in	*dst;
168 {
169 	int			s;
170 	char			indication[NDIS_INDICATION_LEN];
171         struct ifreq            ifr;
172 	struct ndis_evt		*e;
173 	char			buf[512], *pos, *end;
174 	int			len, type, _type;
175 
176 	s = socket(PF_INET, SOCK_DGRAM, 0);
177 
178 	if (s < 0) {
179 		dbgmsg("socket creation failed");
180                 return(EINVAL);
181 	}
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 		if (verbose) {
193 			if (errno == ENOENT)
194 				dbgmsg("drained all events from %s",
195 				    ifname, errno);
196 			else
197 				dbgmsg("failed to read event info from %s: %d",
198 				    ifname, errno);
199 		}
200 		return(ENOENT);
201 	}
202 
203 	if (e->ne_sts == NDIS_STATUS_MEDIA_CONNECT) {
204 		type = EVENT_CONNECT;
205 		if (verbose)
206 			dbgmsg("Received a connect event for %s", ifname);
207 		if (!all_events) {
208 			close(s);
209 			return(0);
210 		}
211 	}
212 	if (e->ne_sts == NDIS_STATUS_MEDIA_DISCONNECT) {
213 		type = EVENT_DISCONNECT;
214 		if (verbose)
215 			dbgmsg("Received a disconnect event for %s", ifname);
216 		if (!all_events) {
217 			close(s);
218 			return(0);
219 		}
220 	}
221 	if (e->ne_sts == NDIS_STATUS_MEDIA_SPECIFIC_INDICATION) {
222 		type = EVENT_MEDIA_SPECIFIC;
223 		if (verbose)
224 			dbgmsg("Received a media-specific event for %s",
225 			    ifname);
226 	}
227 
228 	end = buf + sizeof(buf);
229 	_type = (int) type;
230 	memcpy(buf, &_type, sizeof(_type));
231 	pos = buf + sizeof(_type);
232 
233 	len = snprintf(pos + 1, end - pos - 1, "%s", ifname);
234 	if (len < 0) {
235 		close(s);
236 		return(ENOSPC);
237 	}
238 	if (len > 255)
239 		len = 255;
240 	*pos = (unsigned char) len;
241 	pos += 1 + len;
242 	if (e->ne_len) {
243 		if (e->ne_len > 255 || 1 + e->ne_len > end - pos) {
244 			dbgmsg("Not enough room for send_event data (%d)\n",
245 			    e->ne_len);
246 			close(s);
247 			return(ENOSPC);
248  		}
249 		*pos++ = (unsigned char) e->ne_len;
250 		memcpy(pos, (indication) + sizeof(struct ndis_evt), e->ne_len);
251 		pos += e->ne_len;
252 	}
253 
254 	len = sendto(sock, buf, pos - buf, 0, (struct sockaddr *) dst,
255 	    sizeof(struct sockaddr_in));
256 
257 	close(s);
258 	return(0);
259 }
260 
261 static void
usage()262 usage()
263 {
264 	fprintf(stderr, "Usage: ndis_events [-a] [-d] [-v]\n");
265 	exit(1);
266 }
267 
268 int
main(argc,argv)269 main(argc, argv)
270 	int			argc;
271 	char			*argv[];
272 {
273 	int			s, r, 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];
279 	int			ch;
280 
281 	while ((ch = getopt(argc, argv, "dva")) != -1) {
282 		switch(ch) {
283 		case 'd':
284 			debug++;
285 			break;
286 		case 'v':
287 			verbose++;
288 			break;
289 		case 'a':
290 			all_events++;
291 			break;
292 		default:
293 			usage();
294 			break;
295 		}
296 	}
297 
298 	if (!debug && daemon(0, 0))
299 		err(1, "failed to daemonize ourselves");
300 
301 	if (!debug)
302 		openlog(PROGNAME, LOG_PID | LOG_CONS, LOG_DAEMON);
303 
304 	bzero((char *)&sin, sizeof(sin));
305 
306 	/* Create a datagram  socket. */
307 
308 	s = socket(PF_INET, SOCK_DGRAM, 0);
309 	if (s < 0) {
310 		dbgmsg("socket creation failed");
311 		exit(1);
312 	}
313 
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);
317 
318 	/* Create a routing socket. */
319 
320 	r = socket (PF_ROUTE, SOCK_RAW, 0);
321 	if (r < 0) {
322 		dbgmsg("routing socket creation failed");
323 		exit(1);
324 	}
325 
326 	/* Now sit and spin, waiting for events. */
327 
328 	if (verbose)
329 		dbgmsg("Listening for events");
330 
331 	while (1) {
332 		n = read(r, msg, NDIS_INDICATION_LEN);
333 		rtm = (struct rt_msghdr *)msg;
334 		if (rtm->rtm_type != RTM_IFINFO)
335 			continue;
336 		ifm = (struct if_msghdr *)msg;
337 		if (find_ifname(ifm->ifm_index, ifname))
338 			continue;
339 		if (strstr(ifname, "ndis")) {
340 			while(announce_event(ifname, s, &sin) == 0)
341 				;
342 		} else {
343 			if (verbose)
344 				dbgmsg("Skipping ifinfo message from %s",
345 				    ifname);
346 		}
347 	}
348 
349 	/* NOTREACHED */
350 	exit(0);
351 }
352