xref: /freebsd/sys/net80211/ieee80211_freebsd.c (revision b1f9167f94059fd55c630891d359bcff987bd7eb)
1 /*-
2  * Copyright (c) 2003-2009 Sam Leffler, Errno Consulting
3  * 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  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25 
26 #include <sys/cdefs.h>
27 __FBSDID("$FreeBSD$");
28 
29 /*
30  * IEEE 802.11 support (FreeBSD-specific code)
31  */
32 #include "opt_wlan.h"
33 
34 #include <sys/param.h>
35 #include <sys/kernel.h>
36 #include <sys/systm.h>
37 #include <sys/linker.h>
38 #include <sys/mbuf.h>
39 #include <sys/module.h>
40 #include <sys/proc.h>
41 #include <sys/sysctl.h>
42 
43 #include <sys/socket.h>
44 
45 #include <net/bpf.h>
46 #include <net/if.h>
47 #include <net/if_var.h>
48 #include <net/if_dl.h>
49 #include <net/if_clone.h>
50 #include <net/if_media.h>
51 #include <net/if_types.h>
52 #include <net/ethernet.h>
53 #include <net/route.h>
54 #include <net/vnet.h>
55 
56 #include <net80211/ieee80211_var.h>
57 #include <net80211/ieee80211_input.h>
58 
59 SYSCTL_NODE(_net, OID_AUTO, wlan, CTLFLAG_RD, 0, "IEEE 80211 parameters");
60 
61 #ifdef IEEE80211_DEBUG
62 int	ieee80211_debug = 0;
63 SYSCTL_INT(_net_wlan, OID_AUTO, debug, CTLFLAG_RW, &ieee80211_debug,
64 	    0, "debugging printfs");
65 #endif
66 
67 static MALLOC_DEFINE(M_80211_COM, "80211com", "802.11 com state");
68 
69 #if __FreeBSD_version >= 1000020
70 static const char wlanname[] = "wlan";
71 static struct if_clone *wlan_cloner;
72 #endif
73 
74 /*
75  * Allocate/free com structure in conjunction with ifnet;
76  * these routines are registered with if_register_com_alloc
77  * below and are called automatically by the ifnet code
78  * when the ifnet of the parent device is created.
79  */
80 static void *
81 wlan_alloc(u_char type, struct ifnet *ifp)
82 {
83 	struct ieee80211com *ic;
84 
85 	ic = malloc(sizeof(struct ieee80211com), M_80211_COM, M_WAITOK|M_ZERO);
86 	ic->ic_ifp = ifp;
87 
88 	return (ic);
89 }
90 
91 static void
92 wlan_free(void *ic, u_char type)
93 {
94 	free(ic, M_80211_COM);
95 }
96 
97 static int
98 wlan_clone_create(struct if_clone *ifc, int unit, caddr_t params)
99 {
100 	struct ieee80211_clone_params cp;
101 	struct ieee80211vap *vap;
102 	struct ieee80211com *ic;
103 	struct ifnet *ifp;
104 	int error;
105 
106 	error = copyin(params, &cp, sizeof(cp));
107 	if (error)
108 		return error;
109 	ifp = ifunit(cp.icp_parent);
110 	if (ifp == NULL)
111 		return ENXIO;
112 	/* XXX move printfs to DIAGNOSTIC before release */
113 	if (ifp->if_type != IFT_IEEE80211) {
114 		if_printf(ifp, "%s: reject, not an 802.11 device\n", __func__);
115 		return ENXIO;
116 	}
117 	if (cp.icp_opmode >= IEEE80211_OPMODE_MAX) {
118 		if_printf(ifp, "%s: invalid opmode %d\n",
119 		    __func__, cp.icp_opmode);
120 		return EINVAL;
121 	}
122 	ic = ifp->if_l2com;
123 	if ((ic->ic_caps & ieee80211_opcap[cp.icp_opmode]) == 0) {
124 		if_printf(ifp, "%s mode not supported\n",
125 		    ieee80211_opmode_name[cp.icp_opmode]);
126 		return EOPNOTSUPP;
127 	}
128 	if ((cp.icp_flags & IEEE80211_CLONE_TDMA) &&
129 #ifdef IEEE80211_SUPPORT_TDMA
130 	    (ic->ic_caps & IEEE80211_C_TDMA) == 0
131 #else
132 	    (1)
133 #endif
134 	) {
135 		if_printf(ifp, "TDMA not supported\n");
136 		return EOPNOTSUPP;
137 	}
138 #if __FreeBSD_version >= 1000020
139 	vap = ic->ic_vap_create(ic, wlanname, unit,
140 			cp.icp_opmode, cp.icp_flags, cp.icp_bssid,
141 			cp.icp_flags & IEEE80211_CLONE_MACADDR ?
142 			    cp.icp_macaddr : (const uint8_t *)IF_LLADDR(ifp));
143 #else
144 	vap = ic->ic_vap_create(ic, ifc->ifc_name, unit,
145 			cp.icp_opmode, cp.icp_flags, cp.icp_bssid,
146 			cp.icp_flags & IEEE80211_CLONE_MACADDR ?
147 			    cp.icp_macaddr : (const uint8_t *)IF_LLADDR(ifp));
148 
149 #endif
150 
151 	return (vap == NULL ? EIO : 0);
152 }
153 
154 static void
155 wlan_clone_destroy(struct ifnet *ifp)
156 {
157 	struct ieee80211vap *vap = ifp->if_softc;
158 	struct ieee80211com *ic = vap->iv_ic;
159 
160 	ic->ic_vap_delete(vap);
161 }
162 
163 #if __FreeBSD_version < 1000020
164 IFC_SIMPLE_DECLARE(wlan, 0);
165 #endif
166 
167 void
168 ieee80211_vap_destroy(struct ieee80211vap *vap)
169 {
170 	CURVNET_SET(vap->iv_ifp->if_vnet);
171 #if __FreeBSD_version >= 1000020
172 	if_clone_destroyif(wlan_cloner, vap->iv_ifp);
173 #else
174 	if_clone_destroyif(&wlan_cloner, vap->iv_ifp);
175 #endif
176 	CURVNET_RESTORE();
177 }
178 
179 int
180 ieee80211_sysctl_msecs_ticks(SYSCTL_HANDLER_ARGS)
181 {
182 	int msecs = ticks_to_msecs(*(int *)arg1);
183 	int error, t;
184 
185 	error = sysctl_handle_int(oidp, &msecs, 0, req);
186 	if (error || !req->newptr)
187 		return error;
188 	t = msecs_to_ticks(msecs);
189 	*(int *)arg1 = (t < 1) ? 1 : t;
190 	return 0;
191 }
192 
193 static int
194 ieee80211_sysctl_inact(SYSCTL_HANDLER_ARGS)
195 {
196 	int inact = (*(int *)arg1) * IEEE80211_INACT_WAIT;
197 	int error;
198 
199 	error = sysctl_handle_int(oidp, &inact, 0, req);
200 	if (error || !req->newptr)
201 		return error;
202 	*(int *)arg1 = inact / IEEE80211_INACT_WAIT;
203 	return 0;
204 }
205 
206 static int
207 ieee80211_sysctl_parent(SYSCTL_HANDLER_ARGS)
208 {
209 	struct ieee80211com *ic = arg1;
210 	const char *name = ic->ic_ifp->if_xname;
211 
212 	return SYSCTL_OUT(req, name, strlen(name));
213 }
214 
215 static int
216 ieee80211_sysctl_radar(SYSCTL_HANDLER_ARGS)
217 {
218 	struct ieee80211com *ic = arg1;
219 	int t = 0, error;
220 
221 	error = sysctl_handle_int(oidp, &t, 0, req);
222 	if (error || !req->newptr)
223 		return error;
224 	IEEE80211_LOCK(ic);
225 	ieee80211_dfs_notify_radar(ic, ic->ic_curchan);
226 	IEEE80211_UNLOCK(ic);
227 	return 0;
228 }
229 
230 void
231 ieee80211_sysctl_attach(struct ieee80211com *ic)
232 {
233 }
234 
235 void
236 ieee80211_sysctl_detach(struct ieee80211com *ic)
237 {
238 }
239 
240 void
241 ieee80211_sysctl_vattach(struct ieee80211vap *vap)
242 {
243 	struct ifnet *ifp = vap->iv_ifp;
244 	struct sysctl_ctx_list *ctx;
245 	struct sysctl_oid *oid;
246 	char num[14];			/* sufficient for 32 bits */
247 
248 	ctx = (struct sysctl_ctx_list *) malloc(sizeof(struct sysctl_ctx_list),
249 		M_DEVBUF, M_NOWAIT | M_ZERO);
250 	if (ctx == NULL) {
251 		if_printf(ifp, "%s: cannot allocate sysctl context!\n",
252 			__func__);
253 		return;
254 	}
255 	sysctl_ctx_init(ctx);
256 	snprintf(num, sizeof(num), "%u", ifp->if_dunit);
257 	oid = SYSCTL_ADD_NODE(ctx, &SYSCTL_NODE_CHILDREN(_net, wlan),
258 		OID_AUTO, num, CTLFLAG_RD, NULL, "");
259 	SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
260 		"%parent", CTLTYPE_STRING | CTLFLAG_RD, vap->iv_ic, 0,
261 		ieee80211_sysctl_parent, "A", "parent device");
262 	SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
263 		"driver_caps", CTLFLAG_RW, &vap->iv_caps, 0,
264 		"driver capabilities");
265 #ifdef IEEE80211_DEBUG
266 	vap->iv_debug = ieee80211_debug;
267 	SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
268 		"debug", CTLFLAG_RW, &vap->iv_debug, 0,
269 		"control debugging printfs");
270 #endif
271 	SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
272 		"bmiss_max", CTLFLAG_RW, &vap->iv_bmiss_max, 0,
273 		"consecutive beacon misses before scanning");
274 	/* XXX inherit from tunables */
275 	SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
276 		"inact_run", CTLTYPE_INT | CTLFLAG_RW, &vap->iv_inact_run, 0,
277 		ieee80211_sysctl_inact, "I",
278 		"station inactivity timeout (sec)");
279 	SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
280 		"inact_probe", CTLTYPE_INT | CTLFLAG_RW, &vap->iv_inact_probe, 0,
281 		ieee80211_sysctl_inact, "I",
282 		"station inactivity probe timeout (sec)");
283 	SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
284 		"inact_auth", CTLTYPE_INT | CTLFLAG_RW, &vap->iv_inact_auth, 0,
285 		ieee80211_sysctl_inact, "I",
286 		"station authentication timeout (sec)");
287 	SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
288 		"inact_init", CTLTYPE_INT | CTLFLAG_RW, &vap->iv_inact_init, 0,
289 		ieee80211_sysctl_inact, "I",
290 		"station initial state timeout (sec)");
291 	if (vap->iv_htcaps & IEEE80211_HTC_HT) {
292 		SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
293 			"ampdu_mintraffic_bk", CTLFLAG_RW,
294 			&vap->iv_ampdu_mintraffic[WME_AC_BK], 0,
295 			"BK traffic tx aggr threshold (pps)");
296 		SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
297 			"ampdu_mintraffic_be", CTLFLAG_RW,
298 			&vap->iv_ampdu_mintraffic[WME_AC_BE], 0,
299 			"BE traffic tx aggr threshold (pps)");
300 		SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
301 			"ampdu_mintraffic_vo", CTLFLAG_RW,
302 			&vap->iv_ampdu_mintraffic[WME_AC_VO], 0,
303 			"VO traffic tx aggr threshold (pps)");
304 		SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
305 			"ampdu_mintraffic_vi", CTLFLAG_RW,
306 			&vap->iv_ampdu_mintraffic[WME_AC_VI], 0,
307 			"VI traffic tx aggr threshold (pps)");
308 	}
309 	if (vap->iv_caps & IEEE80211_C_DFS) {
310 		SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
311 			"radar", CTLTYPE_INT | CTLFLAG_RW, vap->iv_ic, 0,
312 			ieee80211_sysctl_radar, "I", "simulate radar event");
313 	}
314 	vap->iv_sysctl = ctx;
315 	vap->iv_oid = oid;
316 }
317 
318 void
319 ieee80211_sysctl_vdetach(struct ieee80211vap *vap)
320 {
321 
322 	if (vap->iv_sysctl != NULL) {
323 		sysctl_ctx_free(vap->iv_sysctl);
324 		free(vap->iv_sysctl, M_DEVBUF);
325 		vap->iv_sysctl = NULL;
326 	}
327 }
328 
329 int
330 ieee80211_node_dectestref(struct ieee80211_node *ni)
331 {
332 	/* XXX need equivalent of atomic_dec_and_test */
333 	atomic_subtract_int(&ni->ni_refcnt, 1);
334 	return atomic_cmpset_int(&ni->ni_refcnt, 0, 1);
335 }
336 
337 void
338 ieee80211_drain_ifq(struct ifqueue *ifq)
339 {
340 	struct ieee80211_node *ni;
341 	struct mbuf *m;
342 
343 	for (;;) {
344 		IF_DEQUEUE(ifq, m);
345 		if (m == NULL)
346 			break;
347 
348 		ni = (struct ieee80211_node *)m->m_pkthdr.rcvif;
349 		KASSERT(ni != NULL, ("frame w/o node"));
350 		ieee80211_free_node(ni);
351 		m->m_pkthdr.rcvif = NULL;
352 
353 		m_freem(m);
354 	}
355 }
356 
357 void
358 ieee80211_flush_ifq(struct ifqueue *ifq, struct ieee80211vap *vap)
359 {
360 	struct ieee80211_node *ni;
361 	struct mbuf *m, **mprev;
362 
363 	IF_LOCK(ifq);
364 	mprev = &ifq->ifq_head;
365 	while ((m = *mprev) != NULL) {
366 		ni = (struct ieee80211_node *)m->m_pkthdr.rcvif;
367 		if (ni != NULL && ni->ni_vap == vap) {
368 			*mprev = m->m_nextpkt;		/* remove from list */
369 			ifq->ifq_len--;
370 
371 			m_freem(m);
372 			ieee80211_free_node(ni);	/* reclaim ref */
373 		} else
374 			mprev = &m->m_nextpkt;
375 	}
376 	/* recalculate tail ptr */
377 	m = ifq->ifq_head;
378 	for (; m != NULL && m->m_nextpkt != NULL; m = m->m_nextpkt)
379 		;
380 	ifq->ifq_tail = m;
381 	IF_UNLOCK(ifq);
382 }
383 
384 /*
385  * As above, for mbufs allocated with m_gethdr/MGETHDR
386  * or initialized by M_COPY_PKTHDR.
387  */
388 #define	MC_ALIGN(m, len)						\
389 do {									\
390 	(m)->m_data += (MCLBYTES - (len)) &~ (sizeof(long) - 1);	\
391 } while (/* CONSTCOND */ 0)
392 
393 /*
394  * Allocate and setup a management frame of the specified
395  * size.  We return the mbuf and a pointer to the start
396  * of the contiguous data area that's been reserved based
397  * on the packet length.  The data area is forced to 32-bit
398  * alignment and the buffer length to a multiple of 4 bytes.
399  * This is done mainly so beacon frames (that require this)
400  * can use this interface too.
401  */
402 struct mbuf *
403 ieee80211_getmgtframe(uint8_t **frm, int headroom, int pktlen)
404 {
405 	struct mbuf *m;
406 	u_int len;
407 
408 	/*
409 	 * NB: we know the mbuf routines will align the data area
410 	 *     so we don't need to do anything special.
411 	 */
412 	len = roundup2(headroom + pktlen, 4);
413 	KASSERT(len <= MCLBYTES, ("802.11 mgt frame too large: %u", len));
414 	if (len < MINCLSIZE) {
415 		m = m_gethdr(M_NOWAIT, MT_DATA);
416 		/*
417 		 * Align the data in case additional headers are added.
418 		 * This should only happen when a WEP header is added
419 		 * which only happens for shared key authentication mgt
420 		 * frames which all fit in MHLEN.
421 		 */
422 		if (m != NULL)
423 			MH_ALIGN(m, len);
424 	} else {
425 		m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
426 		if (m != NULL)
427 			MC_ALIGN(m, len);
428 	}
429 	if (m != NULL) {
430 		m->m_data += headroom;
431 		*frm = m->m_data;
432 	}
433 	return m;
434 }
435 
436 #ifndef __NO_STRICT_ALIGNMENT
437 /*
438  * Re-align the payload in the mbuf.  This is mainly used (right now)
439  * to handle IP header alignment requirements on certain architectures.
440  */
441 struct mbuf *
442 ieee80211_realign(struct ieee80211vap *vap, struct mbuf *m, size_t align)
443 {
444 	int pktlen, space;
445 	struct mbuf *n;
446 
447 	pktlen = m->m_pkthdr.len;
448 	space = pktlen + align;
449 	if (space < MINCLSIZE)
450 		n = m_gethdr(M_NOWAIT, MT_DATA);
451 	else {
452 		n = m_getjcl(M_NOWAIT, MT_DATA, M_PKTHDR,
453 		    space <= MCLBYTES ?     MCLBYTES :
454 #if MJUMPAGESIZE != MCLBYTES
455 		    space <= MJUMPAGESIZE ? MJUMPAGESIZE :
456 #endif
457 		    space <= MJUM9BYTES ?   MJUM9BYTES : MJUM16BYTES);
458 	}
459 	if (__predict_true(n != NULL)) {
460 		m_move_pkthdr(n, m);
461 		n->m_data = (caddr_t)(ALIGN(n->m_data + align) - align);
462 		m_copydata(m, 0, pktlen, mtod(n, caddr_t));
463 		n->m_len = pktlen;
464 	} else {
465 		IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
466 		    mtod(m, const struct ieee80211_frame *), NULL,
467 		    "%s", "no mbuf to realign");
468 		vap->iv_stats.is_rx_badalign++;
469 	}
470 	m_freem(m);
471 	return n;
472 }
473 #endif /* !__NO_STRICT_ALIGNMENT */
474 
475 int
476 ieee80211_add_callback(struct mbuf *m,
477 	void (*func)(struct ieee80211_node *, void *, int), void *arg)
478 {
479 	struct m_tag *mtag;
480 	struct ieee80211_cb *cb;
481 
482 	mtag = m_tag_alloc(MTAG_ABI_NET80211, NET80211_TAG_CALLBACK,
483 			sizeof(struct ieee80211_cb), M_NOWAIT);
484 	if (mtag == NULL)
485 		return 0;
486 
487 	cb = (struct ieee80211_cb *)(mtag+1);
488 	cb->func = func;
489 	cb->arg = arg;
490 	m_tag_prepend(m, mtag);
491 	m->m_flags |= M_TXCB;
492 	return 1;
493 }
494 
495 void
496 ieee80211_process_callback(struct ieee80211_node *ni,
497 	struct mbuf *m, int status)
498 {
499 	struct m_tag *mtag;
500 
501 	mtag = m_tag_locate(m, MTAG_ABI_NET80211, NET80211_TAG_CALLBACK, NULL);
502 	if (mtag != NULL) {
503 		struct ieee80211_cb *cb = (struct ieee80211_cb *)(mtag+1);
504 		cb->func(ni, cb->arg, status);
505 	}
506 }
507 
508 /*
509  * Transmit a frame to the parent interface.
510  *
511  * TODO: if the transmission fails, make sure the parent node is freed
512  *   (the callers will first need modifying.)
513  */
514 int
515 ieee80211_parent_xmitpkt(struct ieee80211com *ic,
516 	struct mbuf *m)
517 {
518 	struct ifnet *parent = ic->ic_ifp;
519 	/*
520 	 * Assert the IC TX lock is held - this enforces the
521 	 * processing -> queuing order is maintained
522 	 */
523 	IEEE80211_TX_LOCK_ASSERT(ic);
524 
525 	return (parent->if_transmit(parent, m));
526 }
527 
528 /*
529  * Transmit a frame to the VAP interface.
530  */
531 int
532 ieee80211_vap_xmitpkt(struct ieee80211vap *vap, struct mbuf *m)
533 {
534 	struct ifnet *ifp = vap->iv_ifp;
535 
536 	/*
537 	 * When transmitting via the VAP, we shouldn't hold
538 	 * any IC TX lock as the VAP TX path will acquire it.
539 	 */
540 	IEEE80211_TX_UNLOCK_ASSERT(vap->iv_ic);
541 
542 	return (ifp->if_transmit(ifp, m));
543 
544 }
545 
546 #include <sys/libkern.h>
547 
548 void
549 get_random_bytes(void *p, size_t n)
550 {
551 	uint8_t *dp = p;
552 
553 	while (n > 0) {
554 		uint32_t v = arc4random();
555 		size_t nb = n > sizeof(uint32_t) ? sizeof(uint32_t) : n;
556 		bcopy(&v, dp, n > sizeof(uint32_t) ? sizeof(uint32_t) : n);
557 		dp += sizeof(uint32_t), n -= nb;
558 	}
559 }
560 
561 /*
562  * Helper function for events that pass just a single mac address.
563  */
564 static void
565 notify_macaddr(struct ifnet *ifp, int op, const uint8_t mac[IEEE80211_ADDR_LEN])
566 {
567 	struct ieee80211_join_event iev;
568 
569 	CURVNET_SET(ifp->if_vnet);
570 	memset(&iev, 0, sizeof(iev));
571 	IEEE80211_ADDR_COPY(iev.iev_addr, mac);
572 	rt_ieee80211msg(ifp, op, &iev, sizeof(iev));
573 	CURVNET_RESTORE();
574 }
575 
576 void
577 ieee80211_notify_node_join(struct ieee80211_node *ni, int newassoc)
578 {
579 	struct ieee80211vap *vap = ni->ni_vap;
580 	struct ifnet *ifp = vap->iv_ifp;
581 
582 	CURVNET_SET_QUIET(ifp->if_vnet);
583 	IEEE80211_NOTE(vap, IEEE80211_MSG_NODE, ni, "%snode join",
584 	    (ni == vap->iv_bss) ? "bss " : "");
585 
586 	if (ni == vap->iv_bss) {
587 		notify_macaddr(ifp, newassoc ?
588 		    RTM_IEEE80211_ASSOC : RTM_IEEE80211_REASSOC, ni->ni_bssid);
589 		if_link_state_change(ifp, LINK_STATE_UP);
590 	} else {
591 		notify_macaddr(ifp, newassoc ?
592 		    RTM_IEEE80211_JOIN : RTM_IEEE80211_REJOIN, ni->ni_macaddr);
593 	}
594 	CURVNET_RESTORE();
595 }
596 
597 void
598 ieee80211_notify_node_leave(struct ieee80211_node *ni)
599 {
600 	struct ieee80211vap *vap = ni->ni_vap;
601 	struct ifnet *ifp = vap->iv_ifp;
602 
603 	CURVNET_SET_QUIET(ifp->if_vnet);
604 	IEEE80211_NOTE(vap, IEEE80211_MSG_NODE, ni, "%snode leave",
605 	    (ni == vap->iv_bss) ? "bss " : "");
606 
607 	if (ni == vap->iv_bss) {
608 		rt_ieee80211msg(ifp, RTM_IEEE80211_DISASSOC, NULL, 0);
609 		if_link_state_change(ifp, LINK_STATE_DOWN);
610 	} else {
611 		/* fire off wireless event station leaving */
612 		notify_macaddr(ifp, RTM_IEEE80211_LEAVE, ni->ni_macaddr);
613 	}
614 	CURVNET_RESTORE();
615 }
616 
617 void
618 ieee80211_notify_scan_done(struct ieee80211vap *vap)
619 {
620 	struct ifnet *ifp = vap->iv_ifp;
621 
622 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN, "%s\n", "notify scan done");
623 
624 	/* dispatch wireless event indicating scan completed */
625 	CURVNET_SET(ifp->if_vnet);
626 	rt_ieee80211msg(ifp, RTM_IEEE80211_SCAN, NULL, 0);
627 	CURVNET_RESTORE();
628 }
629 
630 void
631 ieee80211_notify_replay_failure(struct ieee80211vap *vap,
632 	const struct ieee80211_frame *wh, const struct ieee80211_key *k,
633 	u_int64_t rsc, int tid)
634 {
635 	struct ifnet *ifp = vap->iv_ifp;
636 
637 	IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_CRYPTO, wh->i_addr2,
638 	    "%s replay detected tid %d <rsc %ju, csc %ju, keyix %u rxkeyix %u>",
639 	    k->wk_cipher->ic_name, tid, (intmax_t) rsc,
640 	    (intmax_t) k->wk_keyrsc[tid],
641 	    k->wk_keyix, k->wk_rxkeyix);
642 
643 	if (ifp != NULL) {		/* NB: for cipher test modules */
644 		struct ieee80211_replay_event iev;
645 
646 		IEEE80211_ADDR_COPY(iev.iev_dst, wh->i_addr1);
647 		IEEE80211_ADDR_COPY(iev.iev_src, wh->i_addr2);
648 		iev.iev_cipher = k->wk_cipher->ic_cipher;
649 		if (k->wk_rxkeyix != IEEE80211_KEYIX_NONE)
650 			iev.iev_keyix = k->wk_rxkeyix;
651 		else
652 			iev.iev_keyix = k->wk_keyix;
653 		iev.iev_keyrsc = k->wk_keyrsc[tid];
654 		iev.iev_rsc = rsc;
655 		CURVNET_SET(ifp->if_vnet);
656 		rt_ieee80211msg(ifp, RTM_IEEE80211_REPLAY, &iev, sizeof(iev));
657 		CURVNET_RESTORE();
658 	}
659 }
660 
661 void
662 ieee80211_notify_michael_failure(struct ieee80211vap *vap,
663 	const struct ieee80211_frame *wh, u_int keyix)
664 {
665 	struct ifnet *ifp = vap->iv_ifp;
666 
667 	IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_CRYPTO, wh->i_addr2,
668 	    "michael MIC verification failed <keyix %u>", keyix);
669 	vap->iv_stats.is_rx_tkipmic++;
670 
671 	if (ifp != NULL) {		/* NB: for cipher test modules */
672 		struct ieee80211_michael_event iev;
673 
674 		IEEE80211_ADDR_COPY(iev.iev_dst, wh->i_addr1);
675 		IEEE80211_ADDR_COPY(iev.iev_src, wh->i_addr2);
676 		iev.iev_cipher = IEEE80211_CIPHER_TKIP;
677 		iev.iev_keyix = keyix;
678 		CURVNET_SET(ifp->if_vnet);
679 		rt_ieee80211msg(ifp, RTM_IEEE80211_MICHAEL, &iev, sizeof(iev));
680 		CURVNET_RESTORE();
681 	}
682 }
683 
684 void
685 ieee80211_notify_wds_discover(struct ieee80211_node *ni)
686 {
687 	struct ieee80211vap *vap = ni->ni_vap;
688 	struct ifnet *ifp = vap->iv_ifp;
689 
690 	notify_macaddr(ifp, RTM_IEEE80211_WDS, ni->ni_macaddr);
691 }
692 
693 void
694 ieee80211_notify_csa(struct ieee80211com *ic,
695 	const struct ieee80211_channel *c, int mode, int count)
696 {
697 	struct ifnet *ifp = ic->ic_ifp;
698 	struct ieee80211_csa_event iev;
699 
700 	memset(&iev, 0, sizeof(iev));
701 	iev.iev_flags = c->ic_flags;
702 	iev.iev_freq = c->ic_freq;
703 	iev.iev_ieee = c->ic_ieee;
704 	iev.iev_mode = mode;
705 	iev.iev_count = count;
706 	CURVNET_SET(ifp->if_vnet);
707 	rt_ieee80211msg(ifp, RTM_IEEE80211_CSA, &iev, sizeof(iev));
708 	CURVNET_RESTORE();
709 }
710 
711 void
712 ieee80211_notify_radar(struct ieee80211com *ic,
713 	const struct ieee80211_channel *c)
714 {
715 	struct ifnet *ifp = ic->ic_ifp;
716 	struct ieee80211_radar_event iev;
717 
718 	memset(&iev, 0, sizeof(iev));
719 	iev.iev_flags = c->ic_flags;
720 	iev.iev_freq = c->ic_freq;
721 	iev.iev_ieee = c->ic_ieee;
722 	CURVNET_SET(ifp->if_vnet);
723 	rt_ieee80211msg(ifp, RTM_IEEE80211_RADAR, &iev, sizeof(iev));
724 	CURVNET_RESTORE();
725 }
726 
727 void
728 ieee80211_notify_cac(struct ieee80211com *ic,
729 	const struct ieee80211_channel *c, enum ieee80211_notify_cac_event type)
730 {
731 	struct ifnet *ifp = ic->ic_ifp;
732 	struct ieee80211_cac_event iev;
733 
734 	memset(&iev, 0, sizeof(iev));
735 	iev.iev_flags = c->ic_flags;
736 	iev.iev_freq = c->ic_freq;
737 	iev.iev_ieee = c->ic_ieee;
738 	iev.iev_type = type;
739 	CURVNET_SET(ifp->if_vnet);
740 	rt_ieee80211msg(ifp, RTM_IEEE80211_CAC, &iev, sizeof(iev));
741 	CURVNET_RESTORE();
742 }
743 
744 void
745 ieee80211_notify_node_deauth(struct ieee80211_node *ni)
746 {
747 	struct ieee80211vap *vap = ni->ni_vap;
748 	struct ifnet *ifp = vap->iv_ifp;
749 
750 	IEEE80211_NOTE(vap, IEEE80211_MSG_NODE, ni, "%s", "node deauth");
751 
752 	notify_macaddr(ifp, RTM_IEEE80211_DEAUTH, ni->ni_macaddr);
753 }
754 
755 void
756 ieee80211_notify_node_auth(struct ieee80211_node *ni)
757 {
758 	struct ieee80211vap *vap = ni->ni_vap;
759 	struct ifnet *ifp = vap->iv_ifp;
760 
761 	IEEE80211_NOTE(vap, IEEE80211_MSG_NODE, ni, "%s", "node auth");
762 
763 	notify_macaddr(ifp, RTM_IEEE80211_AUTH, ni->ni_macaddr);
764 }
765 
766 void
767 ieee80211_notify_country(struct ieee80211vap *vap,
768 	const uint8_t bssid[IEEE80211_ADDR_LEN], const uint8_t cc[2])
769 {
770 	struct ifnet *ifp = vap->iv_ifp;
771 	struct ieee80211_country_event iev;
772 
773 	memset(&iev, 0, sizeof(iev));
774 	IEEE80211_ADDR_COPY(iev.iev_addr, bssid);
775 	iev.iev_cc[0] = cc[0];
776 	iev.iev_cc[1] = cc[1];
777 	CURVNET_SET(ifp->if_vnet);
778 	rt_ieee80211msg(ifp, RTM_IEEE80211_COUNTRY, &iev, sizeof(iev));
779 	CURVNET_RESTORE();
780 }
781 
782 void
783 ieee80211_notify_radio(struct ieee80211com *ic, int state)
784 {
785 	struct ifnet *ifp = ic->ic_ifp;
786 	struct ieee80211_radio_event iev;
787 
788 	memset(&iev, 0, sizeof(iev));
789 	iev.iev_state = state;
790 	CURVNET_SET(ifp->if_vnet);
791 	rt_ieee80211msg(ifp, RTM_IEEE80211_RADIO, &iev, sizeof(iev));
792 	CURVNET_RESTORE();
793 }
794 
795 void
796 ieee80211_load_module(const char *modname)
797 {
798 
799 #ifdef notyet
800 	(void)kern_kldload(curthread, modname, NULL);
801 #else
802 	printf("%s: load the %s module by hand for now.\n", __func__, modname);
803 #endif
804 }
805 
806 static eventhandler_tag wlan_bpfevent;
807 static eventhandler_tag wlan_ifllevent;
808 
809 static void
810 bpf_track(void *arg, struct ifnet *ifp, int dlt, int attach)
811 {
812 	/* NB: identify vap's by if_init */
813 	if (dlt == DLT_IEEE802_11_RADIO &&
814 	    ifp->if_init == ieee80211_init) {
815 		struct ieee80211vap *vap = ifp->if_softc;
816 		/*
817 		 * Track bpf radiotap listener state.  We mark the vap
818 		 * to indicate if any listener is present and the com
819 		 * to indicate if any listener exists on any associated
820 		 * vap.  This flag is used by drivers to prepare radiotap
821 		 * state only when needed.
822 		 */
823 		if (attach) {
824 			ieee80211_syncflag_ext(vap, IEEE80211_FEXT_BPF);
825 			if (vap->iv_opmode == IEEE80211_M_MONITOR)
826 				atomic_add_int(&vap->iv_ic->ic_montaps, 1);
827 		} else if (!bpf_peers_present(vap->iv_rawbpf)) {
828 			ieee80211_syncflag_ext(vap, -IEEE80211_FEXT_BPF);
829 			if (vap->iv_opmode == IEEE80211_M_MONITOR)
830 				atomic_subtract_int(&vap->iv_ic->ic_montaps, 1);
831 		}
832 	}
833 }
834 
835 static void
836 wlan_iflladdr(void *arg __unused, struct ifnet *ifp)
837 {
838 	struct ieee80211com *ic = ifp->if_l2com;
839 	struct ieee80211vap *vap, *next;
840 
841 	if (ifp->if_type != IFT_IEEE80211 || ic == NULL)
842 		return;
843 
844 	IEEE80211_LOCK(ic);
845 	TAILQ_FOREACH_SAFE(vap, &ic->ic_vaps, iv_next, next) {
846 		/*
847 		 * If the MAC address has changed on the parent and it was
848 		 * copied to the vap on creation then re-sync.
849 		 */
850 		if (vap->iv_ic == ic &&
851 		    (vap->iv_flags_ext & IEEE80211_FEXT_UNIQMAC) == 0) {
852 			IEEE80211_ADDR_COPY(vap->iv_myaddr, IF_LLADDR(ifp));
853 			IEEE80211_UNLOCK(ic);
854 			if_setlladdr(vap->iv_ifp, IF_LLADDR(ifp),
855 			    IEEE80211_ADDR_LEN);
856 			IEEE80211_LOCK(ic);
857 		}
858 	}
859 	IEEE80211_UNLOCK(ic);
860 }
861 
862 /*
863  * Module glue.
864  *
865  * NB: the module name is "wlan" for compatibility with NetBSD.
866  */
867 static int
868 wlan_modevent(module_t mod, int type, void *unused)
869 {
870 	switch (type) {
871 	case MOD_LOAD:
872 		if (bootverbose)
873 			printf("wlan: <802.11 Link Layer>\n");
874 		wlan_bpfevent = EVENTHANDLER_REGISTER(bpf_track,
875 		    bpf_track, 0, EVENTHANDLER_PRI_ANY);
876 		if (wlan_bpfevent == NULL)
877 			return ENOMEM;
878 		wlan_ifllevent = EVENTHANDLER_REGISTER(iflladdr_event,
879 		    wlan_iflladdr, NULL, EVENTHANDLER_PRI_ANY);
880 		if (wlan_ifllevent == NULL) {
881 			EVENTHANDLER_DEREGISTER(bpf_track, wlan_bpfevent);
882 			return ENOMEM;
883 		}
884 #if __FreeBSD_version >= 1000020
885 		wlan_cloner = if_clone_simple(wlanname, wlan_clone_create,
886 		    wlan_clone_destroy, 0);
887 #else
888 		if_clone_attach(&wlan_cloner);
889 #endif
890 		if_register_com_alloc(IFT_IEEE80211, wlan_alloc, wlan_free);
891 		return 0;
892 	case MOD_UNLOAD:
893 		if_deregister_com_alloc(IFT_IEEE80211);
894 #if __FreeBSD_version >= 1000020
895 		if_clone_detach(wlan_cloner);
896 #else
897 		if_clone_detach(&wlan_cloner);
898 #endif
899 		EVENTHANDLER_DEREGISTER(bpf_track, wlan_bpfevent);
900 		EVENTHANDLER_DEREGISTER(iflladdr_event, wlan_ifllevent);
901 		return 0;
902 	}
903 	return EINVAL;
904 }
905 
906 static moduledata_t wlan_mod = {
907 #if __FreeBSD_version >= 1000020
908 	wlanname,
909 #else
910 	"wlan",
911 #endif
912 	wlan_modevent,
913 	0
914 };
915 DECLARE_MODULE(wlan, wlan_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
916 MODULE_VERSION(wlan, 1);
917 MODULE_DEPEND(wlan, ether, 1, 1, 1);
918 #ifdef	IEEE80211_ALQ
919 MODULE_DEPEND(wlan, alq, 1, 1, 1);
920 #endif	/* IEEE80211_ALQ */
921 
922