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