xref: /freebsd/sys/net80211/ieee80211_freebsd.c (revision 0d48d1ffe0446cd2f87ce02555e3d17772ae7284)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2003-2009 Sam Leffler, Errno Consulting
5  * 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  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #include <sys/cdefs.h>
29 /*
30  * IEEE 802.11 support (FreeBSD-specific code)
31  */
32 #include "opt_wlan.h"
33 
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/eventhandler.h>
37 #include <sys/kernel.h>
38 #include <sys/linker.h>
39 #include <sys/malloc.h>
40 #include <sys/mbuf.h>
41 #include <sys/module.h>
42 #include <sys/priv.h>
43 #include <sys/proc.h>
44 #include <sys/sysctl.h>
45 
46 #include <sys/socket.h>
47 
48 #include <net/bpf.h>
49 #include <net/debugnet.h>
50 #include <net/if.h>
51 #include <net/if_var.h>
52 #include <net/if_dl.h>
53 #include <net/if_clone.h>
54 #include <net/if_media.h>
55 #include <net/if_private.h>
56 #include <net/if_types.h>
57 #include <net/ethernet.h>
58 #include <net/route.h>
59 #include <net/vnet.h>
60 
61 #include <net80211/ieee80211_var.h>
62 #include <net80211/ieee80211_input.h>
63 
64 DEBUGNET_DEFINE(ieee80211);
65 SYSCTL_NODE(_net, OID_AUTO, wlan, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
66     "IEEE 80211 parameters");
67 
68 #ifdef IEEE80211_DEBUG
69 static int	ieee80211_debug = 0;
70 SYSCTL_INT(_net_wlan, OID_AUTO, debug, CTLFLAG_RW, &ieee80211_debug,
71 	    0, "debugging printfs");
72 #endif
73 
74 static const char wlanname[] = "wlan";
75 static struct if_clone *wlan_cloner;
76 
77 /*
78  * priv(9) NET80211 checks.
79  * Return 0 if operation is allowed, E* (usually EPERM) otherwise.
80  */
81 int
82 ieee80211_priv_check_vap_getkey(u_long cmd __unused,
83      struct ieee80211vap *vap __unused, struct ifnet *ifp __unused)
84 {
85 
86 	return (priv_check(curthread, PRIV_NET80211_VAP_GETKEY));
87 }
88 
89 int
90 ieee80211_priv_check_vap_manage(u_long cmd __unused,
91      struct ieee80211vap *vap __unused, struct ifnet *ifp __unused)
92 {
93 
94 	return (priv_check(curthread, PRIV_NET80211_VAP_MANAGE));
95 }
96 
97 int
98 ieee80211_priv_check_vap_setmac(u_long cmd __unused,
99      struct ieee80211vap *vap __unused, struct ifnet *ifp __unused)
100 {
101 
102 	return (priv_check(curthread, PRIV_NET80211_VAP_SETMAC));
103 }
104 
105 int
106 ieee80211_priv_check_create_vap(u_long cmd __unused,
107     struct ieee80211vap *vap __unused, struct ifnet *ifp __unused)
108 {
109 
110 	return (priv_check(curthread, PRIV_NET80211_CREATE_VAP));
111 }
112 
113 static int
114 wlan_clone_create(struct if_clone *ifc, char *name, size_t len,
115     struct ifc_data *ifd, struct ifnet **ifpp)
116 {
117 	struct ieee80211_clone_params cp;
118 	struct ieee80211vap *vap;
119 	struct ieee80211com *ic;
120 	int error;
121 
122 	error = ieee80211_priv_check_create_vap(0, NULL, NULL);
123 	if (error)
124 		return error;
125 
126 	error = ifc_copyin(ifd, &cp, sizeof(cp));
127 	if (error)
128 		return error;
129 	ic = ieee80211_find_com(cp.icp_parent);
130 	if (ic == NULL)
131 		return ENXIO;
132 	if (cp.icp_opmode >= IEEE80211_OPMODE_MAX) {
133 		ic_printf(ic, "%s: invalid opmode %d\n", __func__,
134 		    cp.icp_opmode);
135 		return EINVAL;
136 	}
137 	if ((ic->ic_caps & ieee80211_opcap[cp.icp_opmode]) == 0) {
138 		ic_printf(ic, "%s mode not supported\n",
139 		    ieee80211_opmode_name[cp.icp_opmode]);
140 		return EOPNOTSUPP;
141 	}
142 	if ((cp.icp_flags & IEEE80211_CLONE_TDMA) &&
143 #ifdef IEEE80211_SUPPORT_TDMA
144 	    (ic->ic_caps & IEEE80211_C_TDMA) == 0
145 #else
146 	    (1)
147 #endif
148 	) {
149 		ic_printf(ic, "TDMA not supported\n");
150 		return EOPNOTSUPP;
151 	}
152 	vap = ic->ic_vap_create(ic, wlanname, ifd->unit,
153 			cp.icp_opmode, cp.icp_flags, cp.icp_bssid,
154 			cp.icp_flags & IEEE80211_CLONE_MACADDR ?
155 			    cp.icp_macaddr : ic->ic_macaddr);
156 
157 	if (vap == NULL)
158 		return (EIO);
159 
160 #ifdef DEBUGNET
161 	if (ic->ic_debugnet_meth != NULL)
162 		DEBUGNET_SET(vap->iv_ifp, ieee80211);
163 #endif
164 	*ifpp = vap->iv_ifp;
165 
166 	return (0);
167 }
168 
169 static int
170 wlan_clone_destroy(struct if_clone *ifc, struct ifnet *ifp, uint32_t flags)
171 {
172 	struct ieee80211vap *vap = ifp->if_softc;
173 	struct ieee80211com *ic = vap->iv_ic;
174 
175 	ic->ic_vap_delete(vap);
176 
177 	return (0);
178 }
179 
180 void
181 ieee80211_vap_destroy(struct ieee80211vap *vap)
182 {
183 	CURVNET_SET(vap->iv_ifp->if_vnet);
184 	if_clone_destroyif(wlan_cloner, vap->iv_ifp);
185 	CURVNET_RESTORE();
186 }
187 
188 int
189 ieee80211_sysctl_msecs_ticks(SYSCTL_HANDLER_ARGS)
190 {
191 	int msecs = ticks_to_msecs(*(int *)arg1);
192 	int error;
193 
194 	error = sysctl_handle_int(oidp, &msecs, 0, req);
195 	if (error || !req->newptr)
196 		return error;
197 	*(int *)arg1 = msecs_to_ticks(msecs);
198 	return 0;
199 }
200 
201 static int
202 ieee80211_sysctl_inact(SYSCTL_HANDLER_ARGS)
203 {
204 	int inact = (*(int *)arg1) * IEEE80211_INACT_WAIT;
205 	int error;
206 
207 	error = sysctl_handle_int(oidp, &inact, 0, req);
208 	if (error || !req->newptr)
209 		return error;
210 	*(int *)arg1 = inact / IEEE80211_INACT_WAIT;
211 	return 0;
212 }
213 
214 static int
215 ieee80211_sysctl_parent(SYSCTL_HANDLER_ARGS)
216 {
217 	struct ieee80211com *ic = arg1;
218 
219 	return SYSCTL_OUT_STR(req, ic->ic_name);
220 }
221 
222 static int
223 ieee80211_sysctl_radar(SYSCTL_HANDLER_ARGS)
224 {
225 	struct ieee80211com *ic = arg1;
226 	int t = 0, error;
227 
228 	error = sysctl_handle_int(oidp, &t, 0, req);
229 	if (error || !req->newptr)
230 		return error;
231 	IEEE80211_LOCK(ic);
232 	ieee80211_dfs_notify_radar(ic, ic->ic_curchan);
233 	IEEE80211_UNLOCK(ic);
234 	return 0;
235 }
236 
237 /*
238  * For now, just restart everything.
239  *
240  * Later on, it'd be nice to have a separate VAP restart to
241  * full-device restart.
242  */
243 static int
244 ieee80211_sysctl_vap_restart(SYSCTL_HANDLER_ARGS)
245 {
246 	struct ieee80211vap *vap = arg1;
247 	int t = 0, error;
248 
249 	error = sysctl_handle_int(oidp, &t, 0, req);
250 	if (error || !req->newptr)
251 		return error;
252 
253 	ieee80211_restart_all(vap->iv_ic);
254 	return 0;
255 }
256 
257 void
258 ieee80211_sysctl_attach(struct ieee80211com *ic)
259 {
260 }
261 
262 void
263 ieee80211_sysctl_detach(struct ieee80211com *ic)
264 {
265 }
266 
267 void
268 ieee80211_sysctl_vattach(struct ieee80211vap *vap)
269 {
270 	struct ifnet *ifp = vap->iv_ifp;
271 	struct sysctl_ctx_list *ctx;
272 	struct sysctl_oid *oid;
273 	char num[14];			/* sufficient for 32 bits */
274 
275 	ctx = (struct sysctl_ctx_list *) IEEE80211_MALLOC(sizeof(struct sysctl_ctx_list),
276 		M_DEVBUF, IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
277 	if (ctx == NULL) {
278 		if_printf(ifp, "%s: cannot allocate sysctl context!\n",
279 			__func__);
280 		return;
281 	}
282 	sysctl_ctx_init(ctx);
283 	snprintf(num, sizeof(num), "%u", ifp->if_dunit);
284 	oid = SYSCTL_ADD_NODE(ctx, &SYSCTL_NODE_CHILDREN(_net, wlan),
285 	    OID_AUTO, num, CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "");
286 	SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
287 	    "%parent", CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_NEEDGIANT,
288 	    vap->iv_ic, 0, ieee80211_sysctl_parent, "A", "parent device");
289 	SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
290 		"driver_caps", CTLFLAG_RW, &vap->iv_caps, 0,
291 		"driver capabilities");
292 #ifdef IEEE80211_DEBUG
293 	vap->iv_debug = ieee80211_debug;
294 	SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
295 		"debug", CTLFLAG_RW, &vap->iv_debug, 0,
296 		"control debugging printfs");
297 #endif
298 	SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
299 		"bmiss_max", CTLFLAG_RW, &vap->iv_bmiss_max, 0,
300 		"consecutive beacon misses before scanning");
301 	/* XXX inherit from tunables */
302 	SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
303 	    "inact_run", CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
304 	    &vap->iv_inact_run, 0, ieee80211_sysctl_inact, "I",
305 	    "station inactivity timeout (sec)");
306 	SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
307 	    "inact_probe", CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
308 	    &vap->iv_inact_probe, 0, ieee80211_sysctl_inact, "I",
309 	    "station inactivity probe timeout (sec)");
310 	SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
311 	    "inact_auth", CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
312 	    &vap->iv_inact_auth, 0, ieee80211_sysctl_inact, "I",
313 	    "station authentication timeout (sec)");
314 	SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
315 	    "inact_init", CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
316 	    &vap->iv_inact_init, 0, ieee80211_sysctl_inact, "I",
317 	    "station initial state timeout (sec)");
318 	if (vap->iv_htcaps & IEEE80211_HTC_HT) {
319 		SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
320 			"ampdu_mintraffic_bk", CTLFLAG_RW,
321 			&vap->iv_ampdu_mintraffic[WME_AC_BK], 0,
322 			"BK traffic tx aggr threshold (pps)");
323 		SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
324 			"ampdu_mintraffic_be", CTLFLAG_RW,
325 			&vap->iv_ampdu_mintraffic[WME_AC_BE], 0,
326 			"BE traffic tx aggr threshold (pps)");
327 		SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
328 			"ampdu_mintraffic_vo", CTLFLAG_RW,
329 			&vap->iv_ampdu_mintraffic[WME_AC_VO], 0,
330 			"VO traffic tx aggr threshold (pps)");
331 		SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
332 			"ampdu_mintraffic_vi", CTLFLAG_RW,
333 			&vap->iv_ampdu_mintraffic[WME_AC_VI], 0,
334 			"VI traffic tx aggr threshold (pps)");
335 	}
336 
337 	SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
338 	    "force_restart", CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
339 	    vap, 0, ieee80211_sysctl_vap_restart, "I", "force a VAP restart");
340 
341 	if (vap->iv_caps & IEEE80211_C_DFS) {
342 		SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
343 		    "radar", CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
344 		    vap->iv_ic, 0, ieee80211_sysctl_radar, "I",
345 		    "simulate radar event");
346 	}
347 	vap->iv_sysctl = ctx;
348 	vap->iv_oid = oid;
349 }
350 
351 void
352 ieee80211_sysctl_vdetach(struct ieee80211vap *vap)
353 {
354 
355 	if (vap->iv_sysctl != NULL) {
356 		sysctl_ctx_free(vap->iv_sysctl);
357 		IEEE80211_FREE(vap->iv_sysctl, M_DEVBUF);
358 		vap->iv_sysctl = NULL;
359 	}
360 }
361 
362 int
363 ieee80211_com_vincref(struct ieee80211vap *vap)
364 {
365 	uint32_t ostate;
366 
367 	ostate = atomic_fetchadd_32(&vap->iv_com_state, IEEE80211_COM_REF_ADD);
368 
369 	if (ostate & IEEE80211_COM_DETACHED) {
370 		atomic_subtract_32(&vap->iv_com_state, IEEE80211_COM_REF_ADD);
371 		return (ENETDOWN);
372 	}
373 
374 	if (_IEEE80211_MASKSHIFT(ostate, IEEE80211_COM_REF) ==
375 	    IEEE80211_COM_REF_MAX) {
376 		atomic_subtract_32(&vap->iv_com_state, IEEE80211_COM_REF_ADD);
377 		return (EOVERFLOW);
378 	}
379 
380 	return (0);
381 }
382 
383 void
384 ieee80211_com_vdecref(struct ieee80211vap *vap)
385 {
386 	uint32_t ostate;
387 
388 	ostate = atomic_fetchadd_32(&vap->iv_com_state, -IEEE80211_COM_REF_ADD);
389 
390 	KASSERT(_IEEE80211_MASKSHIFT(ostate, IEEE80211_COM_REF) != 0,
391 	    ("com reference counter underflow"));
392 
393 	(void) ostate;
394 }
395 
396 void
397 ieee80211_com_vdetach(struct ieee80211vap *vap)
398 {
399 	int sleep_time;
400 
401 	sleep_time = msecs_to_ticks(250);
402 	atomic_set_32(&vap->iv_com_state, IEEE80211_COM_DETACHED);
403 	while (_IEEE80211_MASKSHIFT(atomic_load_32(&vap->iv_com_state),
404 	    IEEE80211_COM_REF) != 0)
405 		pause("comref", sleep_time);
406 }
407 
408 int
409 ieee80211_node_dectestref(struct ieee80211_node *ni)
410 {
411 	/* XXX need equivalent of atomic_dec_and_test */
412 	atomic_subtract_int(&ni->ni_refcnt, 1);
413 	return atomic_cmpset_int(&ni->ni_refcnt, 0, 1);
414 }
415 
416 void
417 ieee80211_drain_ifq(struct ifqueue *ifq)
418 {
419 	struct ieee80211_node *ni;
420 	struct mbuf *m;
421 
422 	for (;;) {
423 		IF_DEQUEUE(ifq, m);
424 		if (m == NULL)
425 			break;
426 
427 		ni = (struct ieee80211_node *)m->m_pkthdr.rcvif;
428 		KASSERT(ni != NULL, ("frame w/o node"));
429 		ieee80211_free_node(ni);
430 		m->m_pkthdr.rcvif = NULL;
431 
432 		m_freem(m);
433 	}
434 }
435 
436 void
437 ieee80211_flush_ifq(struct ifqueue *ifq, struct ieee80211vap *vap)
438 {
439 	struct ieee80211_node *ni;
440 	struct mbuf *m, **mprev;
441 
442 	IF_LOCK(ifq);
443 	mprev = &ifq->ifq_head;
444 	while ((m = *mprev) != NULL) {
445 		ni = (struct ieee80211_node *)m->m_pkthdr.rcvif;
446 		if (ni != NULL && ni->ni_vap == vap) {
447 			*mprev = m->m_nextpkt;		/* remove from list */
448 			ifq->ifq_len--;
449 
450 			m_freem(m);
451 			ieee80211_free_node(ni);	/* reclaim ref */
452 		} else
453 			mprev = &m->m_nextpkt;
454 	}
455 	/* recalculate tail ptr */
456 	m = ifq->ifq_head;
457 	for (; m != NULL && m->m_nextpkt != NULL; m = m->m_nextpkt)
458 		;
459 	ifq->ifq_tail = m;
460 	IF_UNLOCK(ifq);
461 }
462 
463 /*
464  * As above, for mbufs allocated with m_gethdr/MGETHDR
465  * or initialized by M_COPY_PKTHDR.
466  */
467 #define	MC_ALIGN(m, len)						\
468 do {									\
469 	(m)->m_data += rounddown2(MCLBYTES - (len), sizeof(long));	\
470 } while (/* CONSTCOND */ 0)
471 
472 /*
473  * Allocate and setup a management frame of the specified
474  * size.  We return the mbuf and a pointer to the start
475  * of the contiguous data area that's been reserved based
476  * on the packet length.  The data area is forced to 32-bit
477  * alignment and the buffer length to a multiple of 4 bytes.
478  * This is done mainly so beacon frames (that require this)
479  * can use this interface too.
480  */
481 struct mbuf *
482 ieee80211_getmgtframe(uint8_t **frm, int headroom, int pktlen)
483 {
484 	struct mbuf *m;
485 	u_int len;
486 
487 	/*
488 	 * NB: we know the mbuf routines will align the data area
489 	 *     so we don't need to do anything special.
490 	 */
491 	len = roundup2(headroom + pktlen, 4);
492 	KASSERT(len <= MCLBYTES, ("802.11 mgt frame too large: %u", len));
493 	if (len < MINCLSIZE) {
494 		m = m_gethdr(IEEE80211_M_NOWAIT, MT_DATA);
495 		/*
496 		 * Align the data in case additional headers are added.
497 		 * This should only happen when a WEP header is added
498 		 * which only happens for shared key authentication mgt
499 		 * frames which all fit in MHLEN.
500 		 */
501 		if (m != NULL)
502 			M_ALIGN(m, len);
503 	} else {
504 		m = m_getcl(IEEE80211_M_NOWAIT, MT_DATA, M_PKTHDR);
505 		if (m != NULL)
506 			MC_ALIGN(m, len);
507 	}
508 	if (m != NULL) {
509 		m->m_data += headroom;
510 		*frm = m->m_data;
511 	}
512 	return m;
513 }
514 
515 #ifndef __NO_STRICT_ALIGNMENT
516 /*
517  * Re-align the payload in the mbuf.  This is mainly used (right now)
518  * to handle IP header alignment requirements on certain architectures.
519  */
520 struct mbuf *
521 ieee80211_realign(struct ieee80211vap *vap, struct mbuf *m, size_t align)
522 {
523 	int pktlen, space;
524 	struct mbuf *n;
525 
526 	pktlen = m->m_pkthdr.len;
527 	space = pktlen + align;
528 	if (space < MINCLSIZE)
529 		n = m_gethdr(IEEE80211_M_NOWAIT, MT_DATA);
530 	else {
531 		n = m_getjcl(IEEE80211_M_NOWAIT, MT_DATA, M_PKTHDR,
532 		    space <= MCLBYTES ?     MCLBYTES :
533 #if MJUMPAGESIZE != MCLBYTES
534 		    space <= MJUMPAGESIZE ? MJUMPAGESIZE :
535 #endif
536 		    space <= MJUM9BYTES ?   MJUM9BYTES : MJUM16BYTES);
537 	}
538 	if (__predict_true(n != NULL)) {
539 		m_move_pkthdr(n, m);
540 		n->m_data = (caddr_t)(ALIGN(n->m_data + align) - align);
541 		m_copydata(m, 0, pktlen, mtod(n, caddr_t));
542 		n->m_len = pktlen;
543 	} else {
544 		IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
545 		    mtod(m, const struct ieee80211_frame *), NULL,
546 		    "%s", "no mbuf to realign");
547 		vap->iv_stats.is_rx_badalign++;
548 	}
549 	m_freem(m);
550 	return n;
551 }
552 #endif /* !__NO_STRICT_ALIGNMENT */
553 
554 int
555 ieee80211_add_callback(struct mbuf *m,
556 	void (*func)(struct ieee80211_node *, void *, int), void *arg)
557 {
558 	struct m_tag *mtag;
559 	struct ieee80211_cb *cb;
560 
561 	mtag = m_tag_alloc(MTAG_ABI_NET80211, NET80211_TAG_CALLBACK,
562 			sizeof(struct ieee80211_cb), IEEE80211_M_NOWAIT);
563 	if (mtag == NULL)
564 		return 0;
565 
566 	cb = (struct ieee80211_cb *)(mtag+1);
567 	cb->func = func;
568 	cb->arg = arg;
569 	m_tag_prepend(m, mtag);
570 	m->m_flags |= M_TXCB;
571 	return 1;
572 }
573 
574 int
575 ieee80211_add_xmit_params(struct mbuf *m,
576     const struct ieee80211_bpf_params *params)
577 {
578 	struct m_tag *mtag;
579 	struct ieee80211_tx_params *tx;
580 
581 	mtag = m_tag_alloc(MTAG_ABI_NET80211, NET80211_TAG_XMIT_PARAMS,
582 	    sizeof(struct ieee80211_tx_params), IEEE80211_M_NOWAIT);
583 	if (mtag == NULL)
584 		return (0);
585 
586 	tx = (struct ieee80211_tx_params *)(mtag+1);
587 	memcpy(&tx->params, params, sizeof(struct ieee80211_bpf_params));
588 	m_tag_prepend(m, mtag);
589 	return (1);
590 }
591 
592 int
593 ieee80211_get_xmit_params(struct mbuf *m,
594     struct ieee80211_bpf_params *params)
595 {
596 	struct m_tag *mtag;
597 	struct ieee80211_tx_params *tx;
598 
599 	mtag = m_tag_locate(m, MTAG_ABI_NET80211, NET80211_TAG_XMIT_PARAMS,
600 	    NULL);
601 	if (mtag == NULL)
602 		return (-1);
603 	tx = (struct ieee80211_tx_params *)(mtag + 1);
604 	memcpy(params, &tx->params, sizeof(struct ieee80211_bpf_params));
605 	return (0);
606 }
607 
608 void
609 ieee80211_process_callback(struct ieee80211_node *ni,
610 	struct mbuf *m, int status)
611 {
612 	struct m_tag *mtag;
613 
614 	mtag = m_tag_locate(m, MTAG_ABI_NET80211, NET80211_TAG_CALLBACK, NULL);
615 	if (mtag != NULL) {
616 		struct ieee80211_cb *cb = (struct ieee80211_cb *)(mtag+1);
617 		cb->func(ni, cb->arg, status);
618 	}
619 }
620 
621 /*
622  * Add RX parameters to the given mbuf.
623  *
624  * Returns 1 if OK, 0 on error.
625  */
626 int
627 ieee80211_add_rx_params(struct mbuf *m, const struct ieee80211_rx_stats *rxs)
628 {
629 	struct m_tag *mtag;
630 	struct ieee80211_rx_params *rx;
631 
632 	mtag = m_tag_alloc(MTAG_ABI_NET80211, NET80211_TAG_RECV_PARAMS,
633 	    sizeof(struct ieee80211_rx_stats), IEEE80211_M_NOWAIT);
634 	if (mtag == NULL)
635 		return (0);
636 
637 	rx = (struct ieee80211_rx_params *)(mtag + 1);
638 	memcpy(&rx->params, rxs, sizeof(*rxs));
639 	m_tag_prepend(m, mtag);
640 	return (1);
641 }
642 
643 int
644 ieee80211_get_rx_params(struct mbuf *m, struct ieee80211_rx_stats *rxs)
645 {
646 	struct m_tag *mtag;
647 	struct ieee80211_rx_params *rx;
648 
649 	mtag = m_tag_locate(m, MTAG_ABI_NET80211, NET80211_TAG_RECV_PARAMS,
650 	    NULL);
651 	if (mtag == NULL)
652 		return (-1);
653 	rx = (struct ieee80211_rx_params *)(mtag + 1);
654 	memcpy(rxs, &rx->params, sizeof(*rxs));
655 	return (0);
656 }
657 
658 const struct ieee80211_rx_stats *
659 ieee80211_get_rx_params_ptr(struct mbuf *m)
660 {
661 	struct m_tag *mtag;
662 	struct ieee80211_rx_params *rx;
663 
664 	mtag = m_tag_locate(m, MTAG_ABI_NET80211, NET80211_TAG_RECV_PARAMS,
665 	    NULL);
666 	if (mtag == NULL)
667 		return (NULL);
668 	rx = (struct ieee80211_rx_params *)(mtag + 1);
669 	return (&rx->params);
670 }
671 
672 /*
673  * Add TOA parameters to the given mbuf.
674  */
675 int
676 ieee80211_add_toa_params(struct mbuf *m, const struct ieee80211_toa_params *p)
677 {
678 	struct m_tag *mtag;
679 	struct ieee80211_toa_params *rp;
680 
681 	mtag = m_tag_alloc(MTAG_ABI_NET80211, NET80211_TAG_TOA_PARAMS,
682 	    sizeof(struct ieee80211_toa_params), IEEE80211_M_NOWAIT);
683 	if (mtag == NULL)
684 		return (0);
685 
686 	rp = (struct ieee80211_toa_params *)(mtag + 1);
687 	memcpy(rp, p, sizeof(*rp));
688 	m_tag_prepend(m, mtag);
689 	return (1);
690 }
691 
692 int
693 ieee80211_get_toa_params(struct mbuf *m, struct ieee80211_toa_params *p)
694 {
695 	struct m_tag *mtag;
696 	struct ieee80211_toa_params *rp;
697 
698 	mtag = m_tag_locate(m, MTAG_ABI_NET80211, NET80211_TAG_TOA_PARAMS,
699 	    NULL);
700 	if (mtag == NULL)
701 		return (0);
702 	rp = (struct ieee80211_toa_params *)(mtag + 1);
703 	if (p != NULL)
704 		memcpy(p, rp, sizeof(*p));
705 	return (1);
706 }
707 
708 /*
709  * @brief Transmit a frame to the parent interface.
710  *
711  * Transmit an 802.11 or 802.3 frame to the parent interface.
712  *
713  * This is called as part of 802.11 processing to enqueue a frame
714  * from net80211 into the device for transmit.
715  *
716  * If the interface is marked as 802.3 via IEEE80211_C_8023ENCAP
717  * (ie, doing offload), then an 802.3 frame will be sent and the
718  * driver will need to understand what to do.
719  *
720  * If the interface is marked as 802.11 (ie, no offload), then
721  * an encapsulated 802.11 frame will be queued.  In the case
722  * of an 802.11 fragmented frame this will be a list of frames
723  * representing the fragments making up the 802.11 frame, linked
724  * via m_nextpkt.
725  *
726  * A fragmented frame list will consist of:
727  * + only the first frame with M_SEQNO_SET() assigned the sequence number;
728  * + only the first frame with the node reference and node in rcvif;
729  * + all frames will have the sequence + fragment number populated in
730  *   the 802.11 header.
731  *
732  * The driver must ensure it doesn't try releasing a node reference
733  * for each fragment in the list.
734  *
735  * The provided mbuf/list is consumed both upon success and error.
736  *
737  * @param ic	struct ieee80211com device to enqueue frame to
738  * @param m	struct mbuf chain / packet list to enqueue
739  * @returns	0 if successful, errno if error.
740  */
741 int
742 ieee80211_parent_xmitpkt(struct ieee80211com *ic, struct mbuf *m)
743 {
744 	int error;
745 
746 	/*
747 	 * Assert the IC TX lock is held - this enforces the
748 	 * processing -> queuing order is maintained
749 	 */
750 	IEEE80211_TX_LOCK_ASSERT(ic);
751 	error = ic->ic_transmit(ic, m);
752 	if (error) {
753 		struct ieee80211_node *ni;
754 
755 		ni = (struct ieee80211_node *)m->m_pkthdr.rcvif;
756 
757 		/* XXX number of fragments */
758 		if_inc_counter(ni->ni_vap->iv_ifp, IFCOUNTER_OERRORS, 1);
759 
760 		/* Note: there's only one node reference for a fragment list */
761 		ieee80211_free_node(ni);
762 		ieee80211_free_mbuf(m);
763 	}
764 	return (error);
765 }
766 
767 /*
768  * @brief Transmit an 802.3 frame to the VAP interface.
769  *
770  * This is the entry point for the wifi stack to enqueue 802.3
771  * encapsulated frames for transmit to the given vap/ifnet instance.
772  * This is used in paths where 802.3 frames have been received
773  * or queued, and need to be pushed through the VAP encapsulation
774  * and transmit processing pipeline.
775  *
776  * The provided mbuf/list is consumed both upon success and error.
777  *
778  * @param vap	struct ieee80211vap instance to transmit frame to
779  * @param m	mbuf to transmit
780  * @returns	0 if OK, errno if error
781  */
782 int
783 ieee80211_vap_xmitpkt(struct ieee80211vap *vap, struct mbuf *m)
784 {
785 	struct ifnet *ifp = vap->iv_ifp;
786 
787 	/*
788 	 * When transmitting via the VAP, we shouldn't hold
789 	 * any IC TX lock as the VAP TX path will acquire it.
790 	 */
791 	IEEE80211_TX_UNLOCK_ASSERT(vap->iv_ic);
792 
793 	return (ifp->if_transmit(ifp, m));
794 
795 }
796 
797 #include <sys/libkern.h>
798 
799 void
800 net80211_get_random_bytes(void *p, size_t n)
801 {
802 	uint8_t *dp = p;
803 
804 	while (n > 0) {
805 		uint32_t v = arc4random();
806 		size_t nb = n > sizeof(uint32_t) ? sizeof(uint32_t) : n;
807 		bcopy(&v, dp, n > sizeof(uint32_t) ? sizeof(uint32_t) : n);
808 		dp += sizeof(uint32_t), n -= nb;
809 	}
810 }
811 
812 /*
813  * Helper function for events that pass just a single mac address.
814  */
815 static void
816 notify_macaddr(struct ifnet *ifp, int op, const uint8_t mac[IEEE80211_ADDR_LEN])
817 {
818 	struct ieee80211_join_event iev;
819 
820 	CURVNET_SET(ifp->if_vnet);
821 	memset(&iev, 0, sizeof(iev));
822 	IEEE80211_ADDR_COPY(iev.iev_addr, mac);
823 	rt_ieee80211msg(ifp, op, &iev, sizeof(iev));
824 	CURVNET_RESTORE();
825 }
826 
827 void
828 ieee80211_notify_node_join(struct ieee80211_node *ni, int newassoc)
829 {
830 	struct ieee80211vap *vap = ni->ni_vap;
831 	struct ifnet *ifp = vap->iv_ifp;
832 
833 	CURVNET_SET_QUIET(ifp->if_vnet);
834 	IEEE80211_NOTE(vap, IEEE80211_MSG_NODE, ni, "%snode join",
835 	    (ni == vap->iv_bss) ? "bss " : "");
836 
837 	if (ni == vap->iv_bss) {
838 		notify_macaddr(ifp, newassoc ?
839 		    RTM_IEEE80211_ASSOC : RTM_IEEE80211_REASSOC, ni->ni_bssid);
840 		if_link_state_change(ifp, LINK_STATE_UP);
841 	} else {
842 		notify_macaddr(ifp, newassoc ?
843 		    RTM_IEEE80211_JOIN : RTM_IEEE80211_REJOIN, ni->ni_macaddr);
844 	}
845 	CURVNET_RESTORE();
846 }
847 
848 void
849 ieee80211_notify_node_leave(struct ieee80211_node *ni)
850 {
851 	struct ieee80211vap *vap = ni->ni_vap;
852 	struct ifnet *ifp = vap->iv_ifp;
853 
854 	CURVNET_SET_QUIET(ifp->if_vnet);
855 	IEEE80211_NOTE(vap, IEEE80211_MSG_NODE, ni, "%snode leave",
856 	    (ni == vap->iv_bss) ? "bss " : "");
857 
858 	if (ni == vap->iv_bss) {
859 		rt_ieee80211msg(ifp, RTM_IEEE80211_DISASSOC, NULL, 0);
860 		if_link_state_change(ifp, LINK_STATE_DOWN);
861 	} else {
862 		/* fire off wireless event station leaving */
863 		notify_macaddr(ifp, RTM_IEEE80211_LEAVE, ni->ni_macaddr);
864 	}
865 	CURVNET_RESTORE();
866 }
867 
868 void
869 ieee80211_notify_scan_done(struct ieee80211vap *vap)
870 {
871 	struct ifnet *ifp = vap->iv_ifp;
872 
873 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN, "%s\n", "notify scan done");
874 
875 	/* dispatch wireless event indicating scan completed */
876 	CURVNET_SET(ifp->if_vnet);
877 	rt_ieee80211msg(ifp, RTM_IEEE80211_SCAN, NULL, 0);
878 	CURVNET_RESTORE();
879 }
880 
881 void
882 ieee80211_notify_replay_failure(struct ieee80211vap *vap,
883 	const struct ieee80211_frame *wh, const struct ieee80211_key *k,
884 	u_int64_t rsc, int tid)
885 {
886 	struct ifnet *ifp = vap->iv_ifp;
887 
888 	IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_CRYPTO, wh->i_addr2,
889 	    "%s replay detected tid %d <rsc %ju (%jx), csc %ju (%jx), keyix %u rxkeyix %u>",
890 	    k->wk_cipher->ic_name, tid,
891 	    (intmax_t) rsc,
892 	    (intmax_t) rsc,
893 	    (intmax_t) k->wk_keyrsc[tid],
894 	    (intmax_t) k->wk_keyrsc[tid],
895 	    k->wk_keyix, k->wk_rxkeyix);
896 
897 	if (ifp != NULL) {		/* NB: for cipher test modules */
898 		struct ieee80211_replay_event iev;
899 
900 		IEEE80211_ADDR_COPY(iev.iev_dst, wh->i_addr1);
901 		IEEE80211_ADDR_COPY(iev.iev_src, wh->i_addr2);
902 		iev.iev_cipher = k->wk_cipher->ic_cipher;
903 		if (k->wk_rxkeyix != IEEE80211_KEYIX_NONE)
904 			iev.iev_keyix = k->wk_rxkeyix;
905 		else
906 			iev.iev_keyix = k->wk_keyix;
907 		iev.iev_keyrsc = k->wk_keyrsc[tid];
908 		iev.iev_rsc = rsc;
909 		CURVNET_SET(ifp->if_vnet);
910 		rt_ieee80211msg(ifp, RTM_IEEE80211_REPLAY, &iev, sizeof(iev));
911 		CURVNET_RESTORE();
912 	}
913 }
914 
915 void
916 ieee80211_notify_michael_failure(struct ieee80211vap *vap,
917 	const struct ieee80211_frame *wh, ieee80211_keyix keyix)
918 {
919 	struct ifnet *ifp = vap->iv_ifp;
920 
921 	IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_CRYPTO, wh->i_addr2,
922 	    "michael MIC verification failed <keyix %u>", keyix);
923 	vap->iv_stats.is_rx_tkipmic++;
924 
925 	if (ifp != NULL) {		/* NB: for cipher test modules */
926 		struct ieee80211_michael_event iev;
927 
928 		IEEE80211_ADDR_COPY(iev.iev_dst, wh->i_addr1);
929 		IEEE80211_ADDR_COPY(iev.iev_src, wh->i_addr2);
930 		iev.iev_cipher = IEEE80211_CIPHER_TKIP;
931 		iev.iev_keyix = keyix;
932 		CURVNET_SET(ifp->if_vnet);
933 		rt_ieee80211msg(ifp, RTM_IEEE80211_MICHAEL, &iev, sizeof(iev));
934 		CURVNET_RESTORE();
935 	}
936 }
937 
938 void
939 ieee80211_notify_wds_discover(struct ieee80211_node *ni)
940 {
941 	struct ieee80211vap *vap = ni->ni_vap;
942 	struct ifnet *ifp = vap->iv_ifp;
943 
944 	notify_macaddr(ifp, RTM_IEEE80211_WDS, ni->ni_macaddr);
945 }
946 
947 void
948 ieee80211_notify_csa(struct ieee80211com *ic,
949 	const struct ieee80211_channel *c, int mode, int count)
950 {
951 	struct ieee80211_csa_event iev;
952 	struct ieee80211vap *vap;
953 	struct ifnet *ifp;
954 
955 	memset(&iev, 0, sizeof(iev));
956 	iev.iev_flags = c->ic_flags;
957 	iev.iev_freq = c->ic_freq;
958 	iev.iev_ieee = c->ic_ieee;
959 	iev.iev_mode = mode;
960 	iev.iev_count = count;
961 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
962 		ifp = vap->iv_ifp;
963 		CURVNET_SET(ifp->if_vnet);
964 		rt_ieee80211msg(ifp, RTM_IEEE80211_CSA, &iev, sizeof(iev));
965 		CURVNET_RESTORE();
966 	}
967 }
968 
969 void
970 ieee80211_notify_radar(struct ieee80211com *ic,
971 	const struct ieee80211_channel *c)
972 {
973 	struct ieee80211_radar_event iev;
974 	struct ieee80211vap *vap;
975 	struct ifnet *ifp;
976 
977 	memset(&iev, 0, sizeof(iev));
978 	iev.iev_flags = c->ic_flags;
979 	iev.iev_freq = c->ic_freq;
980 	iev.iev_ieee = c->ic_ieee;
981 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
982 		ifp = vap->iv_ifp;
983 		CURVNET_SET(ifp->if_vnet);
984 		rt_ieee80211msg(ifp, RTM_IEEE80211_RADAR, &iev, sizeof(iev));
985 		CURVNET_RESTORE();
986 	}
987 }
988 
989 void
990 ieee80211_notify_cac(struct ieee80211com *ic,
991 	const struct ieee80211_channel *c, enum ieee80211_notify_cac_event type)
992 {
993 	struct ieee80211_cac_event iev;
994 	struct ieee80211vap *vap;
995 	struct ifnet *ifp;
996 
997 	memset(&iev, 0, sizeof(iev));
998 	iev.iev_flags = c->ic_flags;
999 	iev.iev_freq = c->ic_freq;
1000 	iev.iev_ieee = c->ic_ieee;
1001 	iev.iev_type = type;
1002 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
1003 		ifp = vap->iv_ifp;
1004 		CURVNET_SET(ifp->if_vnet);
1005 		rt_ieee80211msg(ifp, RTM_IEEE80211_CAC, &iev, sizeof(iev));
1006 		CURVNET_RESTORE();
1007 	}
1008 }
1009 
1010 void
1011 ieee80211_notify_node_deauth(struct ieee80211_node *ni)
1012 {
1013 	struct ieee80211vap *vap = ni->ni_vap;
1014 	struct ifnet *ifp = vap->iv_ifp;
1015 
1016 	IEEE80211_NOTE(vap, IEEE80211_MSG_NODE, ni, "%s", "node deauth");
1017 
1018 	notify_macaddr(ifp, RTM_IEEE80211_DEAUTH, ni->ni_macaddr);
1019 }
1020 
1021 void
1022 ieee80211_notify_node_auth(struct ieee80211_node *ni)
1023 {
1024 	struct ieee80211vap *vap = ni->ni_vap;
1025 	struct ifnet *ifp = vap->iv_ifp;
1026 
1027 	IEEE80211_NOTE(vap, IEEE80211_MSG_NODE, ni, "%s", "node auth");
1028 
1029 	notify_macaddr(ifp, RTM_IEEE80211_AUTH, ni->ni_macaddr);
1030 }
1031 
1032 void
1033 ieee80211_notify_country(struct ieee80211vap *vap,
1034 	const uint8_t bssid[IEEE80211_ADDR_LEN], const uint8_t cc[2])
1035 {
1036 	struct ifnet *ifp = vap->iv_ifp;
1037 	struct ieee80211_country_event iev;
1038 
1039 	memset(&iev, 0, sizeof(iev));
1040 	IEEE80211_ADDR_COPY(iev.iev_addr, bssid);
1041 	iev.iev_cc[0] = cc[0];
1042 	iev.iev_cc[1] = cc[1];
1043 	CURVNET_SET(ifp->if_vnet);
1044 	rt_ieee80211msg(ifp, RTM_IEEE80211_COUNTRY, &iev, sizeof(iev));
1045 	CURVNET_RESTORE();
1046 }
1047 
1048 void
1049 ieee80211_notify_radio(struct ieee80211com *ic, int state)
1050 {
1051 	struct ieee80211_radio_event iev;
1052 	struct ieee80211vap *vap;
1053 	struct ifnet *ifp;
1054 
1055 	memset(&iev, 0, sizeof(iev));
1056 	iev.iev_state = state;
1057 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
1058 		ifp = vap->iv_ifp;
1059 		CURVNET_SET(ifp->if_vnet);
1060 		rt_ieee80211msg(ifp, RTM_IEEE80211_RADIO, &iev, sizeof(iev));
1061 		CURVNET_RESTORE();
1062 	}
1063 }
1064 
1065 void
1066 ieee80211_notify_ifnet_change(struct ieee80211vap *vap, int if_flags_mask)
1067 {
1068 	struct ifnet *ifp = vap->iv_ifp;
1069 
1070 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_DEBUG, "%s\n",
1071 	    "interface state change");
1072 
1073 	CURVNET_SET(ifp->if_vnet);
1074 	rt_ifmsg(ifp, if_flags_mask);
1075 	CURVNET_RESTORE();
1076 }
1077 
1078 void
1079 ieee80211_load_module(const char *modname)
1080 {
1081 
1082 #ifdef notyet
1083 	(void)kern_kldload(curthread, modname, NULL);
1084 #else
1085 	printf("%s: load the %s module by hand for now.\n", __func__, modname);
1086 #endif
1087 }
1088 
1089 static eventhandler_tag wlan_bpfevent;
1090 static eventhandler_tag wlan_ifllevent;
1091 
1092 static void
1093 bpf_track(void *arg, struct ifnet *ifp, int dlt, int attach)
1094 {
1095 	/* NB: identify vap's by if_init */
1096 	if (dlt == DLT_IEEE802_11_RADIO &&
1097 	    ifp->if_init == ieee80211_init) {
1098 		struct ieee80211vap *vap = ifp->if_softc;
1099 		/*
1100 		 * Track bpf radiotap listener state.  We mark the vap
1101 		 * to indicate if any listener is present and the com
1102 		 * to indicate if any listener exists on any associated
1103 		 * vap.  This flag is used by drivers to prepare radiotap
1104 		 * state only when needed.
1105 		 */
1106 		if (attach) {
1107 			ieee80211_syncflag_ext(vap, IEEE80211_FEXT_BPF);
1108 			if (vap->iv_opmode == IEEE80211_M_MONITOR)
1109 				atomic_add_int(&vap->iv_ic->ic_montaps, 1);
1110 		} else if (!bpf_peers_present(vap->iv_rawbpf)) {
1111 			ieee80211_syncflag_ext(vap, -IEEE80211_FEXT_BPF);
1112 			if (vap->iv_opmode == IEEE80211_M_MONITOR)
1113 				atomic_subtract_int(&vap->iv_ic->ic_montaps, 1);
1114 		}
1115 	}
1116 }
1117 
1118 /*
1119  * Change MAC address on the vap (if was not started).
1120  */
1121 static void
1122 wlan_iflladdr(void *arg __unused, struct ifnet *ifp)
1123 {
1124 	/* NB: identify vap's by if_init */
1125 	if (ifp->if_init == ieee80211_init &&
1126 	    (ifp->if_flags & IFF_UP) == 0) {
1127 		struct ieee80211vap *vap = ifp->if_softc;
1128 
1129 		IEEE80211_ADDR_COPY(vap->iv_myaddr, IF_LLADDR(ifp));
1130 	}
1131 }
1132 
1133 /*
1134  * Fetch the VAP name.
1135  *
1136  * This returns a const char pointer suitable for debugging,
1137  * but don't expect it to stick around for much longer.
1138  */
1139 const char *
1140 ieee80211_get_vap_ifname(struct ieee80211vap *vap)
1141 {
1142 	if (vap->iv_ifp == NULL)
1143 		return "(none)";
1144 	return (if_name(vap->iv_ifp));
1145 }
1146 
1147 #ifdef DEBUGNET
1148 static void
1149 ieee80211_debugnet_init(struct ifnet *ifp, int *nrxr, int *ncl, int *clsize)
1150 {
1151 	struct ieee80211vap *vap;
1152 	struct ieee80211com *ic;
1153 
1154 	vap = if_getsoftc(ifp);
1155 	ic = vap->iv_ic;
1156 
1157 	IEEE80211_LOCK(ic);
1158 	ic->ic_debugnet_meth->dn8_init(ic, nrxr, ncl, clsize);
1159 	IEEE80211_UNLOCK(ic);
1160 }
1161 
1162 static void
1163 ieee80211_debugnet_event(struct ifnet *ifp, enum debugnet_ev ev)
1164 {
1165 	struct ieee80211vap *vap;
1166 	struct ieee80211com *ic;
1167 
1168 	vap = if_getsoftc(ifp);
1169 	ic = vap->iv_ic;
1170 
1171 	IEEE80211_LOCK(ic);
1172 	ic->ic_debugnet_meth->dn8_event(ic, ev);
1173 	IEEE80211_UNLOCK(ic);
1174 }
1175 
1176 static int
1177 ieee80211_debugnet_transmit(struct ifnet *ifp, struct mbuf *m)
1178 {
1179 	return (ieee80211_vap_transmit(ifp, m));
1180 }
1181 
1182 static int
1183 ieee80211_debugnet_poll(struct ifnet *ifp, int count)
1184 {
1185 	struct ieee80211vap *vap;
1186 	struct ieee80211com *ic;
1187 
1188 	vap = if_getsoftc(ifp);
1189 	ic = vap->iv_ic;
1190 
1191 	return (ic->ic_debugnet_meth->dn8_poll(ic, count));
1192 }
1193 #endif
1194 
1195 /**
1196  * @brief Check if the MAC address was changed by the upper layer.
1197  *
1198  * This is specifically to handle cases like the MAC address
1199  * being changed via an ioctl (eg SIOCSIFLLADDR).
1200  *
1201  * @param vap	VAP to sync MAC address for
1202  */
1203 void
1204 ieee80211_vap_sync_mac_address(struct ieee80211vap *vap)
1205 {
1206 	struct epoch_tracker et;
1207 	const struct ifnet *ifp = vap->iv_ifp;
1208 
1209 	/*
1210 	 * Check if the MAC address was changed
1211 	 * via SIOCSIFLLADDR ioctl.
1212 	 *
1213 	 * NB: device may be detached during initialization;
1214 	 * use if_ioctl for existence check.
1215 	 */
1216 	NET_EPOCH_ENTER(et);
1217 	if (ifp->if_ioctl == ieee80211_ioctl &&
1218 	    (ifp->if_flags & IFF_UP) == 0 &&
1219 	    !IEEE80211_ADDR_EQ(vap->iv_myaddr, IF_LLADDR(ifp)))
1220 		IEEE80211_ADDR_COPY(vap->iv_myaddr, IF_LLADDR(ifp));
1221 	NET_EPOCH_EXIT(et);
1222 }
1223 
1224 /**
1225  * @brief Initial MAC address setup for a VAP.
1226  *
1227  * @param vap	VAP to sync MAC address for
1228  */
1229 void
1230 ieee80211_vap_copy_mac_address(struct ieee80211vap *vap)
1231 {
1232 	struct epoch_tracker et;
1233 
1234 	NET_EPOCH_ENTER(et);
1235 	IEEE80211_ADDR_COPY(vap->iv_myaddr, IF_LLADDR(vap->iv_ifp));
1236 	NET_EPOCH_EXIT(et);
1237 }
1238 
1239 /**
1240  * @brief Deliver data into the upper ifp of the VAP interface
1241  *
1242  * This delivers an 802.3 frame from net80211 up to the operating
1243  * system network interface layer.
1244  *
1245  * @param vap	the current VAP
1246  * @param m	the 802.3 frame to pass up to the VAP interface
1247  *
1248  * Note: this API consumes the mbuf.
1249  */
1250 void
1251 ieee80211_vap_deliver_data(struct ieee80211vap *vap, struct mbuf *m)
1252 {
1253 	struct epoch_tracker et;
1254 
1255 	NET_EPOCH_ENTER(et);
1256 	if_input(vap->iv_ifp, m);
1257 	NET_EPOCH_EXIT(et);
1258 }
1259 
1260 /**
1261  * @brief Return whether the VAP is configured with monitor mode
1262  *
1263  * This checks the operating system layer for whether monitor mode
1264  * is enabled.
1265  *
1266  * @param vap	the current VAP
1267  * @retval true if the underlying interface is in MONITOR mode, false otherwise
1268  */
1269 bool
1270 ieee80211_vap_ifp_check_is_monitor(struct ieee80211vap *vap)
1271 {
1272 	return ((if_getflags(vap->iv_ifp) & IFF_MONITOR) != 0);
1273 }
1274 
1275 /**
1276  * @brief Return whether the VAP is configured in simplex mode.
1277  *
1278  * This checks the operating system layer for whether simplex mode
1279  * is enabled.
1280  *
1281  * @param vap	the current VAP
1282  * @retval true if the underlying interface is in SIMPLEX mode, false otherwise
1283  */
1284 bool
1285 ieee80211_vap_ifp_check_is_simplex(struct ieee80211vap *vap)
1286 {
1287 	return ((if_getflags(vap->iv_ifp) & IFF_SIMPLEX) != 0);
1288 }
1289 
1290 /**
1291  * @brief Return if the VAP underlying network interface is running
1292  *
1293  * @param vap	the current VAP
1294  * @retval true if the underlying interface is running; false otherwise
1295  */
1296 bool
1297 ieee80211_vap_ifp_check_is_running(struct ieee80211vap *vap)
1298 {
1299 	return ((if_getdrvflags(vap->iv_ifp) & IFF_DRV_RUNNING) != 0);
1300 }
1301 
1302 /**
1303  * @brief Change the VAP underlying network interface state
1304  *
1305  * @param vap	the current VAP
1306  * @param state	true to mark the interface as RUNNING, false to clear
1307  */
1308 void
1309 ieee80211_vap_ifp_set_running_state(struct ieee80211vap *vap, bool state)
1310 {
1311 	if (state)
1312 		if_setdrvflagbits(vap->iv_ifp, IFF_DRV_RUNNING, 0);
1313 	else
1314 		if_setdrvflagbits(vap->iv_ifp, 0, IFF_DRV_RUNNING);
1315 }
1316 
1317 /**
1318  * @brief Return the broadcast MAC address.
1319  *
1320  * @param vap	The current VAP
1321  * @retval a uint8_t array representing the ethernet broadcast address
1322  */
1323 const uint8_t *
1324 ieee80211_vap_get_broadcast_address(struct ieee80211vap *vap)
1325 {
1326 	return (if_getbroadcastaddr(vap->iv_ifp));
1327 }
1328 
1329 /*
1330  * Module glue.
1331  *
1332  * NB: the module name is "wlan" for compatibility with NetBSD.
1333  */
1334 static int
1335 wlan_modevent(module_t mod, int type, void *unused)
1336 {
1337 	switch (type) {
1338 	case MOD_LOAD:
1339 		if (bootverbose)
1340 			printf("wlan: <802.11 Link Layer>\n");
1341 		wlan_bpfevent = EVENTHANDLER_REGISTER(bpf_track,
1342 		    bpf_track, 0, EVENTHANDLER_PRI_ANY);
1343 		wlan_ifllevent = EVENTHANDLER_REGISTER(iflladdr_event,
1344 		    wlan_iflladdr, NULL, EVENTHANDLER_PRI_ANY);
1345 		struct if_clone_addreq req = {
1346 			.create_f = wlan_clone_create,
1347 			.destroy_f = wlan_clone_destroy,
1348 			.flags = IFC_F_AUTOUNIT,
1349 		};
1350 		wlan_cloner = ifc_attach_cloner(wlanname, &req);
1351 		return 0;
1352 	case MOD_UNLOAD:
1353 		ifc_detach_cloner(wlan_cloner);
1354 		EVENTHANDLER_DEREGISTER(bpf_track, wlan_bpfevent);
1355 		EVENTHANDLER_DEREGISTER(iflladdr_event, wlan_ifllevent);
1356 		return 0;
1357 	}
1358 	return EINVAL;
1359 }
1360 
1361 static moduledata_t wlan_mod = {
1362 	wlanname,
1363 	wlan_modevent,
1364 	0
1365 };
1366 DECLARE_MODULE(wlan, wlan_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
1367 MODULE_VERSION(wlan, 1);
1368 MODULE_DEPEND(wlan, ether, 1, 1, 1);
1369 #ifdef	IEEE80211_ALQ
1370 MODULE_DEPEND(wlan, alq, 1, 1, 1);
1371 #endif	/* IEEE80211_ALQ */
1372