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