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