xref: /netbsd-src/sys/net80211/ieee80211_netbsd.c (revision 9aa0541bdf64142d9a27c2cf274394d60182818f)
1 /* $NetBSD: ieee80211_netbsd.c,v 1.19 2011/10/07 16:51:45 dyoung Exp $ */
2 /*-
3  * Copyright (c) 2003-2005 Sam Leffler, Errno Consulting
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. The name of the author may not be used to endorse or promote products
15  *    derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 #ifdef __FreeBSD__
31 __FBSDID("$FreeBSD: src/sys/net80211/ieee80211_freebsd.c,v 1.8 2005/08/08 18:46:35 sam Exp $");
32 #else
33 __KERNEL_RCSID(0, "$NetBSD: ieee80211_netbsd.c,v 1.19 2011/10/07 16:51:45 dyoung Exp $");
34 #endif
35 
36 /*
37  * IEEE 802.11 support (NetBSD-specific code)
38  */
39 #include <sys/param.h>
40 #include <sys/kernel.h>
41 #include <sys/systm.h>
42 #include <sys/mbuf.h>
43 #include <sys/proc.h>
44 #include <sys/sysctl.h>
45 #include <sys/once.h>
46 
47 #include <sys/socket.h>
48 
49 #include <net/if.h>
50 #include <net/if_media.h>
51 #include <net/if_ether.h>
52 #include <net/route.h>
53 
54 #include <net80211/ieee80211_netbsd.h>
55 #include <net80211/ieee80211_var.h>
56 #include <net80211/ieee80211_sysctl.h>
57 
58 #define	LOGICALLY_EQUAL(x, y)	(!(x) == !(y))
59 
60 static void ieee80211_sysctl_fill_node(struct ieee80211_node *,
61     struct ieee80211_node_sysctl *, int, const struct ieee80211_channel *,
62     uint32_t);
63 static struct ieee80211_node *ieee80211_node_walknext(
64     struct ieee80211_node_walk *);
65 static struct ieee80211_node *ieee80211_node_walkfirst(
66     struct ieee80211_node_walk *, u_short);
67 static int ieee80211_sysctl_node(SYSCTLFN_ARGS);
68 
69 #ifdef IEEE80211_DEBUG
70 int	ieee80211_debug = 0;
71 #endif
72 
73 typedef void (*ieee80211_setup_func)(void);
74 
75 __link_set_decl(ieee80211_funcs, ieee80211_setup_func);
76 
77 static int
78 ieee80211_init0(void)
79 {
80 	ieee80211_setup_func * const *ieee80211_setup, f;
81 
82         __link_set_foreach(ieee80211_setup, ieee80211_funcs) {
83 		f = (void*)*ieee80211_setup;
84 		(*f)();
85 	}
86 
87 	return 0;
88 }
89 
90 void
91 ieee80211_init(void)
92 {
93 	static ONCE_DECL(ieee80211_init_once);
94 
95 	RUN_ONCE(&ieee80211_init_once, ieee80211_init0);
96 }
97 
98 static int
99 ieee80211_sysctl_inact(SYSCTLFN_ARGS)
100 {
101 	int error, t;
102 	struct sysctlnode node;
103 
104 	node = *rnode;
105 	/* sysctl_lookup copies the product from t.  Then, it
106 	 * copies the new value onto t.
107 	 */
108 	t = *(int*)rnode->sysctl_data * IEEE80211_INACT_WAIT;
109 	node.sysctl_data = &t;
110 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
111 	if (error || newp == NULL)
112 		return (error);
113 
114 	/* The new value was in seconds.  Convert to inactivity-wait
115 	 * intervals.  There are IEEE80211_INACT_WAIT seconds per
116 	 * interval.
117 	 */
118 	*(int*)rnode->sysctl_data = t / IEEE80211_INACT_WAIT;
119 
120 	return (0);
121 }
122 
123 static int
124 ieee80211_sysctl_parent(SYSCTLFN_ARGS)
125 {
126 	struct ieee80211com *ic;
127 	char pname[IFNAMSIZ];
128 	struct sysctlnode node;
129 
130 	node = *rnode;
131 	ic = node.sysctl_data;
132 	strncpy(pname, ic->ic_ifp->if_xname, IFNAMSIZ);
133 	node.sysctl_data = pname;
134 	return sysctl_lookup(SYSCTLFN_CALL(&node));
135 }
136 
137 /*
138  * Create or get top of sysctl tree net.link.ieee80211.
139  */
140 static const struct sysctlnode *
141 ieee80211_sysctl_treetop(struct sysctllog **log)
142 {
143 	int rc;
144 	const struct sysctlnode *rnode;
145 
146 	if ((rc = sysctl_createv(log, 0, NULL, &rnode,
147 	    CTLFLAG_PERMANENT, CTLTYPE_NODE, "net", NULL,
148 	    NULL, 0, NULL, 0, CTL_NET, CTL_EOL)) != 0)
149 		goto err;
150 
151 	if ((rc = sysctl_createv(log, 0, &rnode, &rnode,
152 	    CTLFLAG_PERMANENT, CTLTYPE_NODE, "link",
153 	    "link-layer statistics and controls",
154 	    NULL, 0, NULL, 0, PF_LINK, CTL_EOL)) != 0)
155 		goto err;
156 
157 	if ((rc = sysctl_createv(log, 0, &rnode, &rnode,
158 	    CTLFLAG_PERMANENT, CTLTYPE_NODE, "ieee80211",
159 	    "IEEE 802.11 WLAN statistics and controls",
160 	    NULL, 0, NULL, 0, CTL_CREATE, CTL_EOL)) != 0)
161 		goto err;
162 
163 	return rnode;
164 err:
165 	printf("%s: sysctl_createv failed, rc = %d\n", __func__, rc);
166 	return NULL;
167 }
168 
169 void
170 ieee80211_sysctl_attach(struct ieee80211com *ic)
171 {
172 	int rc;
173 	const struct sysctlnode *cnode, *rnode;
174 	char num[sizeof("vap") + 14];		/* sufficient for 32 bits */
175 
176 	if ((rnode = ieee80211_sysctl_treetop(NULL)) == NULL)
177 		return;
178 
179 	snprintf(num, sizeof(num), "vap%u", ic->ic_vap);
180 
181 	if ((rc = sysctl_createv(&ic->ic_sysctllog, 0, &rnode, &rnode,
182 	    CTLFLAG_PERMANENT, CTLTYPE_NODE, num, SYSCTL_DESCR("virtual AP"),
183 	    NULL, 0, NULL, 0, CTL_CREATE, CTL_EOL)) != 0)
184 		goto err;
185 
186 	/* control debugging printfs */
187 	if ((rc = sysctl_createv(&ic->ic_sysctllog, 0, &rnode, &cnode,
188 	    CTLFLAG_PERMANENT|CTLFLAG_READONLY, CTLTYPE_STRING,
189 	    "parent", SYSCTL_DESCR("parent device"),
190 	    ieee80211_sysctl_parent, 0, ic, IFNAMSIZ, CTL_CREATE,
191 	    CTL_EOL)) != 0)
192 		goto err;
193 
194 #ifdef IEEE80211_DEBUG
195 	/* control debugging printfs */
196 	if ((rc = sysctl_createv(&ic->ic_sysctllog, 0, &rnode, &cnode,
197 	    CTLFLAG_PERMANENT|CTLFLAG_READWRITE, CTLTYPE_INT,
198 	    "debug", SYSCTL_DESCR("control debugging printfs"),
199 	    NULL, ieee80211_debug, &ic->ic_debug, 0,
200 	    CTL_CREATE, CTL_EOL)) != 0)
201 		goto err;
202 #endif
203 	/* XXX inherit from tunables */
204 	if ((rc = sysctl_createv(&ic->ic_sysctllog, 0, &rnode, &cnode,
205 	    CTLFLAG_PERMANENT|CTLFLAG_READWRITE, CTLTYPE_INT,
206 	    "inact_run", SYSCTL_DESCR("station inactivity timeout (sec)"),
207 	    ieee80211_sysctl_inact, 0, &ic->ic_inact_run, 0,
208 	    CTL_CREATE, CTL_EOL)) != 0)
209 		goto err;
210 	if ((rc = sysctl_createv(&ic->ic_sysctllog, 0, &rnode, &cnode,
211 	    CTLFLAG_PERMANENT|CTLFLAG_READWRITE, CTLTYPE_INT,
212 	    "inact_probe",
213 	    SYSCTL_DESCR("station inactivity probe timeout (sec)"),
214 	    ieee80211_sysctl_inact, 0, &ic->ic_inact_probe, 0,
215 	    CTL_CREATE, CTL_EOL)) != 0)
216 		goto err;
217 	if ((rc = sysctl_createv(&ic->ic_sysctllog, 0, &rnode, &cnode,
218 	    CTLFLAG_PERMANENT|CTLFLAG_READWRITE, CTLTYPE_INT,
219 	    "inact_auth",
220 	    SYSCTL_DESCR("station authentication timeout (sec)"),
221 	    ieee80211_sysctl_inact, 0, &ic->ic_inact_auth, 0,
222 	    CTL_CREATE, CTL_EOL)) != 0)
223 		goto err;
224 	if ((rc = sysctl_createv(&ic->ic_sysctllog, 0, &rnode, &cnode,
225 	    CTLFLAG_PERMANENT|CTLFLAG_READWRITE, CTLTYPE_INT,
226 	    "inact_init",
227 	    SYSCTL_DESCR("station initial state timeout (sec)"),
228 	    ieee80211_sysctl_inact, 0, &ic->ic_inact_init, 0,
229 	    CTL_CREATE, CTL_EOL)) != 0)
230 		goto err;
231 	if ((rc = sysctl_createv(&ic->ic_sysctllog, 0, &rnode, &cnode,
232 	    CTLFLAG_PERMANENT|CTLFLAG_READWRITE, CTLTYPE_INT,
233 	    "driver_caps", SYSCTL_DESCR("driver capabilities"),
234 	    NULL, 0, &ic->ic_caps, 0, CTL_CREATE, CTL_EOL)) != 0)
235 		goto err;
236 	if ((rc = sysctl_createv(&ic->ic_sysctllog, 0, &rnode, &cnode,
237 	    CTLFLAG_PERMANENT|CTLFLAG_READWRITE, CTLTYPE_INT,
238 	    "bmiss_max", SYSCTL_DESCR("consecutive beacon misses before scanning"),
239 	    NULL, 0, &ic->ic_bmiss_max, 0, CTL_CREATE, CTL_EOL)) != 0)
240 		goto err;
241 
242 	return;
243 err:
244 	printf("%s: sysctl_createv failed, rc = %d\n", __func__, rc);
245 }
246 
247 void
248 ieee80211_sysctl_detach(struct ieee80211com *ic)
249 {
250 	sysctl_teardown(&ic->ic_sysctllog);
251 }
252 
253 /*
254  * Pointers for testing:
255  *
256  *	If there are no interfaces, or else no 802.11 interfaces,
257  *	ieee80211_node_walkfirst must return NULL.
258  *
259  *	If there is any single 802.11 interface, ieee80211_node_walkfirst
260  *	must not return NULL.
261  */
262 static struct ieee80211_node *
263 ieee80211_node_walkfirst(struct ieee80211_node_walk *nw, u_short if_index)
264 {
265 	(void)memset(nw, 0, sizeof(*nw));
266 
267 	nw->nw_ifindex = if_index;
268 
269 	LIST_FOREACH(nw->nw_ic, &ieee80211com_head, ic_list) {
270 		if (if_index != 0 && nw->nw_ic->ic_ifp->if_index != if_index)
271 			continue;
272 		if (!TAILQ_EMPTY(&nw->nw_ic->ic_sta.nt_node))
273 			nw->nw_nt = &nw->nw_ic->ic_sta;
274 		else if (!TAILQ_EMPTY(&nw->nw_ic->ic_scan.nt_node))
275 			nw->nw_nt = &nw->nw_ic->ic_scan;
276 		else if (nw->nw_ic->ic_bss == NULL)
277 			continue;
278 		break;
279 	}
280 
281 	if (nw->nw_ic == NULL)
282 		return NULL;
283 
284 	if (nw->nw_nt == NULL)
285 		nw->nw_ni = nw->nw_ic->ic_bss;
286 	else
287 		nw->nw_ni = TAILQ_FIRST(&nw->nw_nt->nt_node);
288 
289 	return nw->nw_ni;
290 }
291 
292 static struct ieee80211_node *
293 ieee80211_node_walknext(struct ieee80211_node_walk *nw)
294 {
295 	if (nw->nw_nt != NULL)
296 		nw->nw_ni = TAILQ_NEXT(nw->nw_ni, ni_list);
297 	else
298 		nw->nw_ni = NULL;
299 
300 	while (nw->nw_ni == NULL) {
301 		if (nw->nw_nt == &nw->nw_ic->ic_sta) {
302 			nw->nw_nt = &nw->nw_ic->ic_scan;
303 			nw->nw_ni = TAILQ_FIRST(&nw->nw_nt->nt_node);
304 			continue;
305 		} else if (nw->nw_nt == &nw->nw_ic->ic_scan) {
306 			nw->nw_nt = NULL;
307 			nw->nw_ni = nw->nw_ic->ic_bss;
308 			continue;
309 		}
310 		KASSERT(nw->nw_nt == NULL);
311 		if (nw->nw_ifindex != 0)
312 			return NULL;
313 
314 		nw->nw_ic = LIST_NEXT(nw->nw_ic, ic_list);
315 		if (nw->nw_ic == NULL)
316 			return NULL;
317 
318 		nw->nw_nt = &nw->nw_ic->ic_sta;
319 		nw->nw_ni = TAILQ_FIRST(&nw->nw_nt->nt_node);
320 	}
321 
322 	return nw->nw_ni;
323 }
324 
325 static void
326 ieee80211_sysctl_fill_node(struct ieee80211_node *ni,
327     struct ieee80211_node_sysctl *ns, int ifindex,
328     const struct ieee80211_channel *chan0, uint32_t flags)
329 {
330 	ns->ns_ifindex = ifindex;
331 	ns->ns_capinfo = ni->ni_capinfo;
332 	ns->ns_flags = flags;
333 	(void)memcpy(ns->ns_macaddr, ni->ni_macaddr, sizeof(ns->ns_macaddr));
334 	(void)memcpy(ns->ns_bssid, ni->ni_bssid, sizeof(ns->ns_bssid));
335 	if (ni->ni_chan != IEEE80211_CHAN_ANYC) {
336 		ns->ns_freq = ni->ni_chan->ic_freq;
337 		ns->ns_chanflags = ni->ni_chan->ic_flags;
338 		ns->ns_chanidx = ni->ni_chan - chan0;
339 	} else {
340 		ns->ns_freq = ns->ns_chanflags = 0;
341 		ns->ns_chanidx = 0;
342 	}
343 	ns->ns_rssi = ni->ni_rssi;
344 	ns->ns_esslen = ni->ni_esslen;
345 	(void)memcpy(ns->ns_essid, ni->ni_essid, sizeof(ns->ns_essid));
346 	ns->ns_erp = ni->ni_erp;
347 	ns->ns_associd = ni->ni_associd;
348 	ns->ns_inact = ni->ni_inact * IEEE80211_INACT_WAIT;
349 	ns->ns_rstamp = ni->ni_rstamp;
350 	ns->ns_rates = ni->ni_rates;
351 	ns->ns_txrate = ni->ni_txrate;
352 	ns->ns_intval = ni->ni_intval;
353 	(void)memcpy(ns->ns_tstamp, &ni->ni_tstamp, sizeof(ns->ns_tstamp));
354 	ns->ns_txseq = ni->ni_txseqs[0];
355 	ns->ns_rxseq = ni->ni_rxseqs[0];
356 	ns->ns_fhdwell = ni->ni_fhdwell;
357 	ns->ns_fhindex = ni->ni_fhindex;
358 	ns->ns_fails = ni->ni_fails;
359 }
360 
361 /* Between two examinations of the sysctl tree, I expect each
362  * interface to add no more than 5 nodes.
363  */
364 #define IEEE80211_SYSCTL_NODE_GROWTH	5
365 
366 static int
367 ieee80211_sysctl_node(SYSCTLFN_ARGS)
368 {
369 	struct ieee80211_node_walk nw;
370 	struct ieee80211_node *ni;
371 	struct ieee80211_node_sysctl ns;
372 	char *dp;
373 	u_int cur_ifindex, ifcount, ifindex, last_ifindex, op, arg, hdr_type;
374 	uint32_t flags;
375 	size_t len, needed, eltsize, out_size;
376 	int error, s, saw_bss = 0, nelt;
377 
378 	if (namelen == 1 && name[0] == CTL_QUERY)
379 		return (sysctl_query(SYSCTLFN_CALL(rnode)));
380 
381 	if (namelen != IEEE80211_SYSCTL_NODENAMELEN)
382 		return (EINVAL);
383 
384 	/* ifindex.op.arg.header-type.eltsize.nelt */
385 	dp = oldp;
386 	len = (oldp != NULL) ? *oldlenp : 0;
387 	ifindex = name[IEEE80211_SYSCTL_NODENAME_IF];
388 	op = name[IEEE80211_SYSCTL_NODENAME_OP];
389 	arg = name[IEEE80211_SYSCTL_NODENAME_ARG];
390 	hdr_type = name[IEEE80211_SYSCTL_NODENAME_TYPE];
391 	eltsize = name[IEEE80211_SYSCTL_NODENAME_ELTSIZE];
392 	nelt = name[IEEE80211_SYSCTL_NODENAME_ELTCOUNT];
393 	out_size = MIN(sizeof(ns), eltsize);
394 
395 	if (op != IEEE80211_SYSCTL_OP_ALL || arg != 0 ||
396 	    hdr_type != IEEE80211_SYSCTL_T_NODE || eltsize < 1 || nelt < 0)
397 		return (EINVAL);
398 
399 	error = 0;
400 	needed = 0;
401 	ifcount = 0;
402 	last_ifindex = 0;
403 
404 	s = splnet();
405 
406 	for (ni = ieee80211_node_walkfirst(&nw, ifindex); ni != NULL;
407 	     ni = ieee80211_node_walknext(&nw)) {
408 		struct ieee80211com *ic;
409 
410 		ic = nw.nw_ic;
411 		cur_ifindex = ic->ic_ifp->if_index;
412 
413 		if (cur_ifindex != last_ifindex) {
414 			saw_bss = 0;
415 			ifcount++;
416 			last_ifindex = cur_ifindex;
417 		}
418 
419 		if (nelt <= 0)
420 			continue;
421 
422 		if (saw_bss && ni == ic->ic_bss)
423 			continue;
424 		else if (ni == ic->ic_bss) {
425 			saw_bss = 1;
426 			flags = IEEE80211_NODE_SYSCTL_F_BSS;
427 		} else
428 			flags = 0;
429 		if (ni->ni_table == &ic->ic_scan)
430 			flags |= IEEE80211_NODE_SYSCTL_F_SCAN;
431 		else if (ni->ni_table == &ic->ic_sta)
432 			flags |= IEEE80211_NODE_SYSCTL_F_STA;
433 		if (len >= eltsize) {
434 			ieee80211_sysctl_fill_node(ni, &ns, cur_ifindex,
435 			    &ic->ic_channels[0], flags);
436 			error = copyout(&ns, dp, out_size);
437 			if (error)
438 				goto cleanup;
439 			dp += eltsize;
440 			len -= eltsize;
441 		}
442 		needed += eltsize;
443 		if (nelt != INT_MAX)
444 			nelt--;
445 	}
446 cleanup:
447 	splx(s);
448 
449 	*oldlenp = needed;
450 	if (oldp == NULL)
451 		*oldlenp += ifcount * IEEE80211_SYSCTL_NODE_GROWTH * eltsize;
452 
453 	return (error);
454 }
455 
456 /*
457  * Setup sysctl(3) MIB, net.ieee80211.*
458  *
459  * TBD condition CTLFLAG_PERMANENT on being a module or not
460  */
461 SYSCTL_SETUP(sysctl_ieee80211, "sysctl ieee80211 subtree setup")
462 {
463 	int rc;
464 	const struct sysctlnode *cnode, *rnode;
465 
466 	if ((rnode = ieee80211_sysctl_treetop(clog)) == NULL)
467 		return;
468 
469 	if ((rc = sysctl_createv(clog, 0, &rnode, NULL,
470 	    CTLFLAG_PERMANENT, CTLTYPE_NODE, "nodes", "client/peer stations",
471 	    ieee80211_sysctl_node, 0, NULL, 0, CTL_CREATE, CTL_EOL)) != 0)
472 		goto err;
473 
474 #ifdef IEEE80211_DEBUG
475 	/* control debugging printfs */
476 	if ((rc = sysctl_createv(clog, 0, &rnode, &cnode,
477 	    CTLFLAG_PERMANENT|CTLFLAG_READWRITE, CTLTYPE_INT,
478 	    "debug", SYSCTL_DESCR("control debugging printfs"),
479 	    NULL, 0, &ieee80211_debug, 0, CTL_CREATE, CTL_EOL)) != 0)
480 		goto err;
481 #endif /* IEEE80211_DEBUG */
482 
483 	return;
484 err:
485 	printf("%s: sysctl_createv failed (rc = %d)\n", __func__, rc);
486 }
487 
488 int
489 ieee80211_node_dectestref(struct ieee80211_node *ni)
490 {
491 	if (atomic_dec_uint_nv(&ni->ni_refcnt) == 0) {
492 		atomic_inc_uint(&ni->ni_refcnt);
493 		return 1;
494 	} else
495 		return 0;
496 }
497 
498 void
499 ieee80211_drain_ifq(struct ifqueue *ifq)
500 {
501 	struct ieee80211_node *ni;
502 	struct mbuf *m;
503 
504 	for (;;) {
505 		IF_DEQUEUE(ifq, m);
506 		if (m == NULL)
507 			break;
508 
509 		ni = (struct ieee80211_node *)m->m_pkthdr.rcvif;
510 		KASSERT(ni != NULL);
511 		ieee80211_free_node(ni);
512 		m->m_pkthdr.rcvif = NULL;
513 
514 		m_freem(m);
515 	}
516 }
517 
518 
519 void
520 if_printf(struct ifnet *ifp, const char *fmt, ...)
521 {
522 	va_list ap;
523 	va_start(ap, fmt);
524 
525 	printf("%s: ", ifp->if_xname);
526 	vprintf(fmt, ap);
527 
528 	va_end(ap);
529 	return;
530 }
531 
532 /*
533  * Set the m_data pointer of a newly-allocated mbuf
534  * to place an object of the specified size at the
535  * end of the mbuf, longword aligned.
536  */
537 void
538 m_align(struct mbuf *m, int len)
539 {
540        int adjust;
541 
542        if (m->m_flags & M_EXT)
543 	       adjust = m->m_ext.ext_size - len;
544        else if (m->m_flags & M_PKTHDR)
545 	       adjust = MHLEN - len;
546        else
547 	       adjust = MLEN - len;
548        m->m_data += adjust &~ (sizeof(long)-1);
549 }
550 
551 /*
552  * Append the specified data to the indicated mbuf chain,
553  * Extend the mbuf chain if the new data does not fit in
554  * existing space.
555  *
556  * Return 1 if able to complete the job; otherwise 0.
557  */
558 int
559 m_append(struct mbuf *m0, int len, const void *cpv)
560 {
561 	struct mbuf *m, *n;
562 	int remainder, space;
563 	const char *cp = cpv;
564 
565 	for (m = m0; m->m_next != NULL; m = m->m_next)
566 		continue;
567 	remainder = len;
568 	space = M_TRAILINGSPACE(m);
569 	if (space > 0) {
570 		/*
571 		 * Copy into available space.
572 		 */
573 		if (space > remainder)
574 			space = remainder;
575 		memmove(mtod(m, char *) + m->m_len, cp, space);
576 		m->m_len += space;
577 		cp = cp + space, remainder -= space;
578 	}
579 	while (remainder > 0) {
580 		/*
581 		 * Allocate a new mbuf; could check space
582 		 * and allocate a cluster instead.
583 		 */
584 		n = m_get(M_DONTWAIT, m->m_type);
585 		if (n == NULL)
586 			break;
587 		n->m_len = min(MLEN, remainder);
588 		memmove(mtod(n, void *), cp, n->m_len);
589 		cp += n->m_len, remainder -= n->m_len;
590 		m->m_next = n;
591 		m = n;
592 	}
593 	if (m0->m_flags & M_PKTHDR)
594 		m0->m_pkthdr.len += len - remainder;
595 	return (remainder == 0);
596 }
597 
598 /*
599  * Allocate and setup a management frame of the specified
600  * size.  We return the mbuf and a pointer to the start
601  * of the contiguous data area that's been reserved based
602  * on the packet length.  The data area is forced to 32-bit
603  * alignment and the buffer length to a multiple of 4 bytes.
604  * This is done mainly so beacon frames (that require this)
605  * can use this interface too.
606  */
607 struct mbuf *
608 ieee80211_getmgtframe(u_int8_t **frm, u_int pktlen)
609 {
610 	struct mbuf *m;
611 	u_int len;
612 
613 	/*
614 	 * NB: we know the mbuf routines will align the data area
615 	 *     so we don't need to do anything special.
616 	 */
617 	/* XXX 4-address frame? */
618 	len = roundup(sizeof(struct ieee80211_frame) + pktlen, 4);
619 	IASSERT(len <= MCLBYTES, ("802.11 mgt frame too large: %u", len));
620 	if (len <= MHLEN) {
621 		m = m_gethdr(M_NOWAIT, MT_HEADER);
622 		/*
623 		 * Align the data in case additional headers are added.
624 		 * This should only happen when a WEP header is added
625 		 * which only happens for shared key authentication mgt
626 		 * frames which all fit in MHLEN.
627 		 */
628 		if (m != NULL)
629 			MH_ALIGN(m, len);
630 	} else
631 		m = m_getcl(M_NOWAIT, MT_HEADER, M_PKTHDR);
632 	if (m != NULL) {
633 		m->m_data += sizeof(struct ieee80211_frame);
634 		*frm = m->m_data;
635 		IASSERT((uintptr_t)*frm % 4 == 0, ("bad beacon boundary"));
636 	}
637 	return m;
638 }
639 
640 void
641 get_random_bytes(void *p, size_t n)
642 {
643 	u_int8_t *dp = p;
644 
645 	while (n > 0) {
646 		u_int32_t v = arc4random();
647 		size_t nb = n > sizeof(u_int32_t) ? sizeof(u_int32_t) : n;
648 		(void)memcpy(dp, &v, nb);
649 		dp += sizeof(u_int32_t), n -= nb;
650 	}
651 }
652 
653 void
654 ieee80211_notify_node_join(struct ieee80211com *ic, struct ieee80211_node *ni, int newassoc)
655 {
656 	struct ifnet *ifp = ic->ic_ifp;
657 	struct ieee80211_join_event iev;
658 
659 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE, "%s: %snode %s join\n",
660 	    ifp->if_xname, (ni == ic->ic_bss) ? "bss " : "",
661 	    ether_sprintf(ni->ni_macaddr));
662 
663 	memset(&iev, 0, sizeof(iev));
664 	if (ni == ic->ic_bss) {
665 		IEEE80211_ADDR_COPY(iev.iev_addr, ni->ni_bssid);
666 		rt_ieee80211msg(ifp, newassoc ?
667 			RTM_IEEE80211_ASSOC : RTM_IEEE80211_REASSOC,
668 			&iev, sizeof(iev));
669 		if_link_state_change(ifp, LINK_STATE_UP);
670 	} else {
671 		IEEE80211_ADDR_COPY(iev.iev_addr, ni->ni_macaddr);
672 		rt_ieee80211msg(ifp, newassoc ?
673 		    RTM_IEEE80211_JOIN : RTM_IEEE80211_REJOIN,
674 		    &iev, sizeof(iev));
675 	}
676 }
677 
678 void
679 ieee80211_notify_node_leave(struct ieee80211com *ic, struct ieee80211_node *ni)
680 {
681 	struct ifnet *ifp = ic->ic_ifp;
682 	struct ieee80211_leave_event iev;
683 
684 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE, "%s: %snode %s leave\n",
685 	    ifp->if_xname, (ni == ic->ic_bss) ? "bss " : "",
686 	    ether_sprintf(ni->ni_macaddr));
687 
688 	if (ni == ic->ic_bss) {
689 		rt_ieee80211msg(ifp, RTM_IEEE80211_DISASSOC, NULL, 0);
690 		if_link_state_change(ifp, LINK_STATE_DOWN);
691 	} else {
692 		/* fire off wireless event station leaving */
693 		memset(&iev, 0, sizeof(iev));
694 		IEEE80211_ADDR_COPY(iev.iev_addr, ni->ni_macaddr);
695 		rt_ieee80211msg(ifp, RTM_IEEE80211_LEAVE, &iev, sizeof(iev));
696 	}
697 }
698 
699 void
700 ieee80211_notify_scan_done(struct ieee80211com *ic)
701 {
702 	struct ifnet *ifp = ic->ic_ifp;
703 
704 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN,
705 		"%s: notify scan done\n", ic->ic_ifp->if_xname);
706 
707 	/* dispatch wireless event indicating scan completed */
708 	rt_ieee80211msg(ifp, RTM_IEEE80211_SCAN, NULL, 0);
709 }
710 
711 void
712 ieee80211_notify_replay_failure(struct ieee80211com *ic,
713 	const struct ieee80211_frame *wh, const struct ieee80211_key *k,
714 	u_int64_t rsc)
715 {
716 	struct ifnet *ifp = ic->ic_ifp;
717 
718 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO,
719 	    "[%s] %s replay detected <rsc %ju, csc %ju, keyix %u rxkeyix %u>\n",
720 	    ether_sprintf(wh->i_addr2), k->wk_cipher->ic_name,
721 	    (intmax_t) rsc, (intmax_t) k->wk_keyrsc,
722 	    k->wk_keyix, k->wk_rxkeyix);
723 
724 	if (ifp != NULL) {		/* NB: for cipher test modules */
725 		struct ieee80211_replay_event iev;
726 
727 		IEEE80211_ADDR_COPY(iev.iev_dst, wh->i_addr1);
728 		IEEE80211_ADDR_COPY(iev.iev_src, wh->i_addr2);
729 		iev.iev_cipher = k->wk_cipher->ic_cipher;
730 		if (k->wk_rxkeyix != IEEE80211_KEYIX_NONE)
731 			iev.iev_keyix = k->wk_rxkeyix;
732 		else
733 			iev.iev_keyix = k->wk_keyix;
734 		iev.iev_keyrsc = k->wk_keyrsc;
735 		iev.iev_rsc = rsc;
736 		rt_ieee80211msg(ifp, RTM_IEEE80211_REPLAY, &iev, sizeof(iev));
737 	}
738 }
739 
740 void
741 ieee80211_notify_michael_failure(struct ieee80211com *ic,
742 	const struct ieee80211_frame *wh, u_int keyix)
743 {
744 	struct ifnet *ifp = ic->ic_ifp;
745 
746 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO,
747 		"[%s] michael MIC verification failed <keyix %u>\n",
748 	       ether_sprintf(wh->i_addr2), keyix);
749 	ic->ic_stats.is_rx_tkipmic++;
750 
751 	if (ifp != NULL) {		/* NB: for cipher test modules */
752 		struct ieee80211_michael_event iev;
753 
754 		IEEE80211_ADDR_COPY(iev.iev_dst, wh->i_addr1);
755 		IEEE80211_ADDR_COPY(iev.iev_src, wh->i_addr2);
756 		iev.iev_cipher = IEEE80211_CIPHER_TKIP;
757 		iev.iev_keyix = keyix;
758 		rt_ieee80211msg(ifp, RTM_IEEE80211_MICHAEL, &iev, sizeof(iev));
759 	}
760 }
761 
762 void
763 ieee80211_load_module(const char *modname)
764 {
765 #ifdef notyet
766 	struct thread *td = curthread;
767 
768 	if (suser(td) == 0 && securelevel_gt(td->td_ucred, 0) == 0) {
769 		mtx_lock(&Giant);
770 		(void) linker_load_module(modname, NULL, NULL, NULL, NULL);
771 		mtx_unlock(&Giant);
772 	}
773 #else
774 	printf("%s: load the %s module by hand for now.\n", __func__, modname);
775 #endif
776 }
777