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