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