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