ieee80211_freebsd.c revision 298433
121259Swollman/*-
221259Swollman * Copyright (c) 2003-2009 Sam Leffler, Errno Consulting
321259Swollman * All rights reserved.
421259Swollman *
521259Swollman * Redistribution and use in source and binary forms, with or without
621259Swollman * modification, are permitted provided that the following conditions
721259Swollman * are met:
821259Swollman * 1. Redistributions of source code must retain the above copyright
921259Swollman *    notice, this list of conditions and the following disclaimer.
1021259Swollman * 2. Redistributions in binary form must reproduce the above copyright
1121259Swollman *    notice, this list of conditions and the following disclaimer in the
1221259Swollman *    documentation and/or other materials provided with the distribution.
1321259Swollman *
1421259Swollman * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1521259Swollman * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1621259Swollman * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
1721259Swollman * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
1821259Swollman * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
1921259Swollman * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2021259Swollman * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2121259Swollman * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2221259Swollman * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2321259Swollman * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2421259Swollman */
2521259Swollman
2621259Swollman#include <sys/cdefs.h>
2721259Swollman__FBSDID("$FreeBSD: head/sys/net80211/ieee80211_freebsd.c 298433 2016-04-21 19:57:40Z pfg $");
2821259Swollman
2921259Swollman/*
3021259Swollman * IEEE 802.11 support (FreeBSD-specific code)
3121259Swollman */
3221259Swollman#include "opt_wlan.h"
3321259Swollman
3450477Speter#include <sys/param.h>
3521259Swollman#include <sys/systm.h>
3621259Swollman#include <sys/eventhandler.h>
3721259Swollman#include <sys/kernel.h>
3821259Swollman#include <sys/linker.h>
3921259Swollman#include <sys/malloc.h>
4021259Swollman#include <sys/mbuf.h>
4121259Swollman#include <sys/module.h>
4221259Swollman#include <sys/proc.h>
4321259Swollman#include <sys/sysctl.h>
4421259Swollman
4521259Swollman#include <sys/socket.h>
4621259Swollman
4721259Swollman#include <net/bpf.h>
4821259Swollman#include <net/if.h>
4921259Swollman#include <net/if_var.h>
5021259Swollman#include <net/if_dl.h>
5121259Swollman#include <net/if_clone.h>
5221259Swollman#include <net/if_media.h>
5321259Swollman#include <net/if_types.h>
5421259Swollman#include <net/ethernet.h>
5521259Swollman#include <net/route.h>
5621259Swollman#include <net/vnet.h>
5721259Swollman
5821259Swollman#include <net80211/ieee80211_var.h>
5921259Swollman#include <net80211/ieee80211_input.h>
6021259Swollman
6121259SwollmanSYSCTL_NODE(_net, OID_AUTO, wlan, CTLFLAG_RD, 0, "IEEE 80211 parameters");
6221259Swollman
6321259Swollman#ifdef IEEE80211_DEBUG
6421259Swollmanint	ieee80211_debug = 0;
6521259SwollmanSYSCTL_INT(_net_wlan, OID_AUTO, debug, CTLFLAG_RW, &ieee80211_debug,
6621259Swollman	    0, "debugging printfs");
6721259Swollman#endif
6821259Swollman
6921259Swollmanstatic MALLOC_DEFINE(M_80211_COM, "80211com", "802.11 com state");
7021259Swollman
7121259Swollmanstatic const char wlanname[] = "wlan";
7221259Swollmanstatic struct if_clone *wlan_cloner;
7321259Swollman
7421259Swollmanstatic int
7521259Swollmanwlan_clone_create(struct if_clone *ifc, int unit, caddr_t params)
7621259Swollman{
7721259Swollman	struct ieee80211_clone_params cp;
7869224Sjlemon	struct ieee80211vap *vap;
7969152Sjlemon	struct ieee80211com *ic;
8069224Sjlemon	int error;
8169224Sjlemon
8269152Sjlemon	error = copyin(params, &cp, sizeof(cp));
8360938Sjake	if (error)
8460938Sjake		return error;
8560938Sjake	ic = ieee80211_find_com(cp.icp_parent);
8672084Sphk	if (ic == NULL)
8721259Swollman		return ENXIO;
8821259Swollman	if (cp.icp_opmode >= IEEE80211_OPMODE_MAX) {
8921259Swollman		ic_printf(ic, "%s: invalid opmode %d\n", __func__,
9021259Swollman		    cp.icp_opmode);
9121259Swollman		return EINVAL;
9221259Swollman	}
9321259Swollman	if ((ic->ic_caps & ieee80211_opcap[cp.icp_opmode]) == 0) {
9421259Swollman		ic_printf(ic, "%s mode not supported\n",
9521259Swollman		    ieee80211_opmode_name[cp.icp_opmode]);
9621259Swollman		return EOPNOTSUPP;
9769152Sjlemon	}
9821259Swollman	if ((cp.icp_flags & IEEE80211_CLONE_TDMA) &&
9921259Swollman#ifdef IEEE80211_SUPPORT_TDMA
10021259Swollman	    (ic->ic_caps & IEEE80211_C_TDMA) == 0
10121259Swollman#else
10221259Swollman	    (1)
10321259Swollman#endif
10421259Swollman	) {
10521259Swollman		ic_printf(ic, "TDMA not supported\n");
10621259Swollman		return EOPNOTSUPP;
10721259Swollman	}
10860938Sjake	vap = ic->ic_vap_create(ic, wlanname, unit,
10921259Swollman			cp.icp_opmode, cp.icp_flags, cp.icp_bssid,
11021259Swollman			cp.icp_flags & IEEE80211_CLONE_MACADDR ?
11121259Swollman			    cp.icp_macaddr : ic->ic_macaddr);
11221259Swollman
11321259Swollman	return (vap == NULL ? EIO : 0);
11421259Swollman}
11521259Swollman
11669152Sjlemonstatic void
11721259Swollmanwlan_clone_destroy(struct ifnet *ifp)
11821259Swollman{
11921259Swollman	struct ieee80211vap *vap = ifp->if_softc;
12021259Swollman	struct ieee80211com *ic = vap->iv_ic;
12121404Swollman
12221404Swollman	ic->ic_vap_delete(vap);
12321259Swollman}
12421259Swollman
12521259Swollmanvoid
12621259Swollmanieee80211_vap_destroy(struct ieee80211vap *vap)
12721259Swollman{
12821259Swollman	CURVNET_SET(vap->iv_ifp->if_vnet);
12921259Swollman	if_clone_destroyif(wlan_cloner, vap->iv_ifp);
13021259Swollman	CURVNET_RESTORE();
13121259Swollman}
13236735Sdfr
13321259Swollmanint
13421259Swollmanieee80211_sysctl_msecs_ticks(SYSCTL_HANDLER_ARGS)
13521259Swollman{
13621259Swollman	int msecs = ticks_to_msecs(*(int *)arg1);
13721259Swollman	int error, t;
13821259Swollman
13921259Swollman	error = sysctl_handle_int(oidp, &msecs, 0, req);
14021259Swollman	if (error || !req->newptr)
14121259Swollman		return error;
14221259Swollman	t = msecs_to_ticks(msecs);
14321259Swollman	*(int *)arg1 = (t < 1) ? 1 : t;
14421259Swollman	return 0;
14521404Swollman}
14621404Swollman
14721259Swollmanstatic int
14821259Swollmanieee80211_sysctl_inact(SYSCTL_HANDLER_ARGS)
14952904Sshin{
15021259Swollman	int inact = (*(int *)arg1) * IEEE80211_INACT_WAIT;
15169152Sjlemon	int error;
15221259Swollman
15321259Swollman	error = sysctl_handle_int(oidp, &inact, 0, req);
15421259Swollman	if (error || !req->newptr)
15521259Swollman		return error;
15621259Swollman	*(int *)arg1 = inact / IEEE80211_INACT_WAIT;
15721259Swollman	return 0;
15821259Swollman}
15921259Swollman
16021259Swollmanstatic int
16158698Sjlemonieee80211_sysctl_parent(SYSCTL_HANDLER_ARGS)
16221259Swollman{
16321259Swollman	struct ieee80211com *ic = arg1;
16421259Swollman
16521259Swollman	return SYSCTL_OUT_STR(req, ic->ic_name);
16621259Swollman}
16721259Swollman
16821259Swollmanstatic int
16921259Swollmanieee80211_sysctl_radar(SYSCTL_HANDLER_ARGS)
17021259Swollman{
17121259Swollman	struct ieee80211com *ic = arg1;
17221259Swollman	int t = 0, error;
17321259Swollman
17421259Swollman	error = sysctl_handle_int(oidp, &t, 0, req);
17521259Swollman	if (error || !req->newptr)
17621259Swollman		return error;
17721259Swollman	IEEE80211_LOCK(ic);
17853541Sshin	ieee80211_dfs_notify_radar(ic, ic->ic_curchan);
17953541Sshin	IEEE80211_UNLOCK(ic);
18053541Sshin	return 0;
18153541Sshin}
18221259Swollman
18321259Swollmanvoid
18421259Swollmanieee80211_sysctl_attach(struct ieee80211com *ic)
18521259Swollman{
18621259Swollman}
18721259Swollman
18821259Swollmanvoid
18921259Swollmanieee80211_sysctl_detach(struct ieee80211com *ic)
19021259Swollman{
19121259Swollman}
19221259Swollman
19321259Swollmanvoid
19472200Sbmilekicieee80211_sysctl_vattach(struct ieee80211vap *vap)
19572200Sbmilekic{
19669152Sjlemon	struct ifnet *ifp = vap->iv_ifp;
19769152Sjlemon	struct sysctl_ctx_list *ctx;
19869152Sjlemon	struct sysctl_oid *oid;
19921259Swollman	char num[14];			/* sufficient for 32 bits */
20069152Sjlemon
20169152Sjlemon	ctx = (struct sysctl_ctx_list *) IEEE80211_MALLOC(sizeof(struct sysctl_ctx_list),
20269152Sjlemon		M_DEVBUF, IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
20369152Sjlemon	if (ctx == NULL) {
20469152Sjlemon		if_printf(ifp, "%s: cannot allocate sysctl context!\n",
20569152Sjlemon			__func__);
20669152Sjlemon		return;
20769152Sjlemon	}
20869152Sjlemon	sysctl_ctx_init(ctx);
20969152Sjlemon	snprintf(num, sizeof(num), "%u", ifp->if_dunit);
21069152Sjlemon	oid = SYSCTL_ADD_NODE(ctx, &SYSCTL_NODE_CHILDREN(_net, wlan),
21169152Sjlemon		OID_AUTO, num, CTLFLAG_RD, NULL, "");
21269152Sjlemon	SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
21369152Sjlemon		"%parent", CTLTYPE_STRING | CTLFLAG_RD, vap->iv_ic, 0,
21469152Sjlemon		ieee80211_sysctl_parent, "A", "parent device");
21569152Sjlemon	SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
21669152Sjlemon		"driver_caps", CTLFLAG_RW, &vap->iv_caps, 0,
21769152Sjlemon		"driver capabilities");
21869152Sjlemon#ifdef IEEE80211_DEBUG
21969152Sjlemon	vap->iv_debug = ieee80211_debug;
22069152Sjlemon	SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
22169152Sjlemon		"debug", CTLFLAG_RW, &vap->iv_debug, 0,
22269152Sjlemon		"control debugging printfs");
22369152Sjlemon#endif
22469152Sjlemon	SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
22569152Sjlemon		"bmiss_max", CTLFLAG_RW, &vap->iv_bmiss_max, 0,
22669152Sjlemon		"consecutive beacon misses before scanning");
22769152Sjlemon	/* XXX inherit from tunables */
22869152Sjlemon	SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
22969152Sjlemon		"inact_run", CTLTYPE_INT | CTLFLAG_RW, &vap->iv_inact_run, 0,
23069152Sjlemon		ieee80211_sysctl_inact, "I",
23169152Sjlemon		"station inactivity timeout (sec)");
23269152Sjlemon	SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
23369152Sjlemon		"inact_probe", CTLTYPE_INT | CTLFLAG_RW, &vap->iv_inact_probe, 0,
23469152Sjlemon		ieee80211_sysctl_inact, "I",
23569152Sjlemon		"station inactivity probe timeout (sec)");
23669152Sjlemon	SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
23769152Sjlemon		"inact_auth", CTLTYPE_INT | CTLFLAG_RW, &vap->iv_inact_auth, 0,
23869152Sjlemon		ieee80211_sysctl_inact, "I",
23969152Sjlemon		"station authentication timeout (sec)");
24069152Sjlemon	SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
24169152Sjlemon		"inact_init", CTLTYPE_INT | CTLFLAG_RW, &vap->iv_inact_init, 0,
24269152Sjlemon		ieee80211_sysctl_inact, "I",
24369152Sjlemon		"station initial state timeout (sec)");
24469152Sjlemon	if (vap->iv_htcaps & IEEE80211_HTC_HT) {
24569152Sjlemon		SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
24669152Sjlemon			"ampdu_mintraffic_bk", CTLFLAG_RW,
24769152Sjlemon			&vap->iv_ampdu_mintraffic[WME_AC_BK], 0,
24869152Sjlemon			"BK traffic tx aggr threshold (pps)");
24969152Sjlemon		SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
25069152Sjlemon			"ampdu_mintraffic_be", CTLFLAG_RW,
25169152Sjlemon			&vap->iv_ampdu_mintraffic[WME_AC_BE], 0,
25269152Sjlemon			"BE traffic tx aggr threshold (pps)");
25369152Sjlemon		SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
25469152Sjlemon			"ampdu_mintraffic_vo", CTLFLAG_RW,
25569152Sjlemon			&vap->iv_ampdu_mintraffic[WME_AC_VO], 0,
25669152Sjlemon			"VO traffic tx aggr threshold (pps)");
25769152Sjlemon		SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
25855205Speter			"ampdu_mintraffic_vi", CTLFLAG_RW,
25969152Sjlemon			&vap->iv_ampdu_mintraffic[WME_AC_VI], 0,
26069152Sjlemon			"VI traffic tx aggr threshold (pps)");
26121259Swollman	}
26235210Sbde	if (vap->iv_caps & IEEE80211_C_DFS) {
26369152Sjlemon		SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
26421259Swollman			"radar", CTLTYPE_INT | CTLFLAG_RW, vap->iv_ic, 0,
26569152Sjlemon			ieee80211_sysctl_radar, "I", "simulate radar event");
26621259Swollman	}
26769152Sjlemon	vap->iv_sysctl = ctx;
26869152Sjlemon	vap->iv_oid = oid;
26969152Sjlemon}
27069152Sjlemon
27169152Sjlemonvoid
27269152Sjlemonieee80211_sysctl_vdetach(struct ieee80211vap *vap)
27369152Sjlemon{
27469152Sjlemon
27569152Sjlemon	if (vap->iv_sysctl != NULL) {
27669152Sjlemon		sysctl_ctx_free(vap->iv_sysctl);
27769152Sjlemon		IEEE80211_FREE(vap->iv_sysctl, M_DEVBUF);
27869152Sjlemon		vap->iv_sysctl = NULL;
27969152Sjlemon	}
28069152Sjlemon}
28169152Sjlemon
28269152Sjlemonint
28369152Sjlemonieee80211_node_dectestref(struct ieee80211_node *ni)
28469152Sjlemon{
28569152Sjlemon	/* XXX need equivalent of atomic_dec_and_test */
28669152Sjlemon	atomic_subtract_int(&ni->ni_refcnt, 1);
28769152Sjlemon	return atomic_cmpset_int(&ni->ni_refcnt, 0, 1);
28869152Sjlemon}
28969152Sjlemon
29069152Sjlemonvoid
29169152Sjlemonieee80211_drain_ifq(struct ifqueue *ifq)
29221259Swollman{
29321259Swollman	struct ieee80211_node *ni;
29449459Sbrian	struct mbuf *m;
29549459Sbrian
29649459Sbrian	for (;;) {
29749459Sbrian		IF_DEQUEUE(ifq, m);
29849459Sbrian		if (m == NULL)
29949459Sbrian			break;
30049459Sbrian
30155205Speter		ni = (struct ieee80211_node *)m->m_pkthdr.rcvif;
30221259Swollman		KASSERT(ni != NULL, ("frame w/o node"));
30321259Swollman		ieee80211_free_node(ni);
30421259Swollman		m->m_pkthdr.rcvif = NULL;
30521259Swollman
30621259Swollman		m_freem(m);
30721259Swollman	}
30821259Swollman}
30921259Swollman
31021259Swollmanvoid
31121259Swollmanieee80211_flush_ifq(struct ifqueue *ifq, struct ieee80211vap *vap)
31221259Swollman{
31321259Swollman	struct ieee80211_node *ni;
31467334Sjoe	struct mbuf *m, **mprev;
31521259Swollman
31660938Sjake	IF_LOCK(ifq);
31721259Swollman	mprev = &ifq->ifq_head;
31821259Swollman	while ((m = *mprev) != NULL) {
31921259Swollman		ni = (struct ieee80211_node *)m->m_pkthdr.rcvif;
32047254Spb		if (ni != NULL && ni->ni_vap == vap) {
32121259Swollman			*mprev = m->m_nextpkt;		/* remove from list */
32221259Swollman			ifq->ifq_len--;
32321259Swollman
32421259Swollman			m_freem(m);
32528845Sjulian			ieee80211_free_node(ni);	/* reclaim ref */
32628845Sjulian		} else
32728845Sjulian			mprev = &m->m_nextpkt;
32821259Swollman	}
32921259Swollman	/* recalculate tail ptr */
33021259Swollman	m = ifq->ifq_head;
33153541Sshin	for (; m != NULL && m->m_nextpkt != NULL; m = m->m_nextpkt)
33253541Sshin		;
33353541Sshin	ifq->ifq_tail = m;
33421404Swollman	IF_UNLOCK(ifq);
33552904Sshin}
33652904Sshin
33752904Sshin/*
33853541Sshin * As above, for mbufs allocated with m_gethdr/MGETHDR
33952904Sshin * or initialized by M_COPY_PKTHDR.
34052904Sshin */
34152904Sshin#define	MC_ALIGN(m, len)						\
34252904Sshindo {									\
34360938Sjake	(m)->m_data += rounddown2(MCLBYTES - (len), sizeof(long));	\
34452904Sshin} while (/* CONSTCOND */ 0)
34552904Sshin
34652904Sshin/*
34752904Sshin * Allocate and setup a management frame of the specified
34852904Sshin * size.  We return the mbuf and a pointer to the start
34921404Swollman * of the contiguous data area that's been reserved based
35021404Swollman * on the packet length.  The data area is forced to 32-bit
35121404Swollman * alignment and the buffer length to a multiple of 4 bytes.
35221404Swollman * This is done mainly so beacon frames (that require this)
35321404Swollman * can use this interface too.
35421404Swollman */
35572084Sphkstruct mbuf *
35621434Swollmanieee80211_getmgtframe(uint8_t **frm, int headroom, int pktlen)
35721434Swollman{
35821434Swollman	struct mbuf *m;
35921434Swollman	u_int len;
36021434Swollman
36121404Swollman	/*
36221404Swollman	 * NB: we know the mbuf routines will align the data area
36355205Speter	 *     so we don't need to do anything special.
36421259Swollman	 */
36546568Speter	len = roundup2(headroom + pktlen, 4);
36646568Speter	KASSERT(len <= MCLBYTES, ("802.11 mgt frame too large: %u", len));
36746568Speter	if (len < MINCLSIZE) {
36846568Speter		m = m_gethdr(M_NOWAIT, MT_DATA);
36946568Speter		/*
37046568Speter		 * Align the data in case additional headers are added.
37121259Swollman		 * This should only happen when a WEP header is added
37221259Swollman		 * which only happens for shared key authentication mgt
37352904Sshin		 * frames which all fit in MHLEN.
37421259Swollman		 */
37571791Speter		if (m != NULL)
37621259Swollman			M_ALIGN(m, len);
37721259Swollman	} else {
37821259Swollman		m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
37963090Sarchie		if (m != NULL)
38063090Sarchie			MC_ALIGN(m, len);
38121259Swollman	}
38262143Sarchie	if (m != NULL) {
38321259Swollman		m->m_data += headroom;
38421259Swollman		*frm = m->m_data;
38562143Sarchie	}
38621259Swollman	return m;
38721259Swollman}
38853541Sshin
38921434Swollman#ifndef __NO_STRICT_ALIGNMENT
39021404Swollman/*
39121259Swollman * Re-align the payload in the mbuf.  This is mainly used (right now)
39221404Swollman * to handle IP header alignment requirements on certain architectures.
39345720Speter */
39421259Swollmanstruct mbuf *
39541879Sphkieee80211_realign(struct ieee80211vap *vap, struct mbuf *m, size_t align)
39664651Sarchie{
39741879Sphk	int pktlen, space;
39821259Swollman	struct mbuf *n;
39921259Swollman
40036735Sdfr	pktlen = m->m_pkthdr.len;
40121259Swollman	space = pktlen + align;
40221259Swollman	if (space < MINCLSIZE)
40352904Sshin		n = m_gethdr(M_NOWAIT, MT_DATA);
40421259Swollman	else {
40521259Swollman		n = m_getjcl(M_NOWAIT, MT_DATA, M_PKTHDR,
40621259Swollman		    space <= MCLBYTES ?     MCLBYTES :
40721259Swollman#if MJUMPAGESIZE != MCLBYTES
40821259Swollman		    space <= MJUMPAGESIZE ? MJUMPAGESIZE :
40921259Swollman#endif
41021259Swollman		    space <= MJUM9BYTES ?   MJUM9BYTES : MJUM16BYTES);
41121259Swollman	}
41221259Swollman	if (__predict_true(n != NULL)) {
41321259Swollman		m_move_pkthdr(n, m);
41421259Swollman		n->m_data = (caddr_t)(ALIGN(n->m_data + align) - align);
41521259Swollman		m_copydata(m, 0, pktlen, mtod(n, caddr_t));
41621259Swollman		n->m_len = pktlen;
41721259Swollman	} else {
41821259Swollman		IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
41921259Swollman		    mtod(m, const struct ieee80211_frame *), NULL,
42053541Sshin		    "%s", "no mbuf to realign");
42121434Swollman		vap->iv_stats.is_rx_badalign++;
42260889Sarchie	}
42321434Swollman	m_freem(m);
42455205Speter	return n;
42521259Swollman}
42621259Swollman#endif /* !__NO_STRICT_ALIGNMENT */
427
428int
429ieee80211_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
448int
449ieee80211_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
466int
467ieee80211_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
482void
483ieee80211_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 */
500int
501ieee80211_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
517int
518ieee80211_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 * Transmit a frame to the parent interface.
534 */
535int
536ieee80211_parent_xmitpkt(struct ieee80211com *ic, struct mbuf *m)
537{
538	int error;
539
540	/*
541	 * Assert the IC TX lock is held - this enforces the
542	 * processing -> queuing order is maintained
543	 */
544	IEEE80211_TX_LOCK_ASSERT(ic);
545	error = ic->ic_transmit(ic, m);
546	if (error) {
547		struct ieee80211_node *ni;
548
549		ni = (struct ieee80211_node *)m->m_pkthdr.rcvif;
550
551		/* XXX number of fragments */
552		if_inc_counter(ni->ni_vap->iv_ifp, IFCOUNTER_OERRORS, 1);
553		ieee80211_free_node(ni);
554		ieee80211_free_mbuf(m);
555	}
556	return (error);
557}
558
559/*
560 * Transmit a frame to the VAP interface.
561 */
562int
563ieee80211_vap_xmitpkt(struct ieee80211vap *vap, struct mbuf *m)
564{
565	struct ifnet *ifp = vap->iv_ifp;
566
567	/*
568	 * When transmitting via the VAP, we shouldn't hold
569	 * any IC TX lock as the VAP TX path will acquire it.
570	 */
571	IEEE80211_TX_UNLOCK_ASSERT(vap->iv_ic);
572
573	return (ifp->if_transmit(ifp, m));
574
575}
576
577#include <sys/libkern.h>
578
579void
580get_random_bytes(void *p, size_t n)
581{
582	uint8_t *dp = p;
583
584	while (n > 0) {
585		uint32_t v = arc4random();
586		size_t nb = n > sizeof(uint32_t) ? sizeof(uint32_t) : n;
587		bcopy(&v, dp, n > sizeof(uint32_t) ? sizeof(uint32_t) : n);
588		dp += sizeof(uint32_t), n -= nb;
589	}
590}
591
592/*
593 * Helper function for events that pass just a single mac address.
594 */
595static void
596notify_macaddr(struct ifnet *ifp, int op, const uint8_t mac[IEEE80211_ADDR_LEN])
597{
598	struct ieee80211_join_event iev;
599
600	CURVNET_SET(ifp->if_vnet);
601	memset(&iev, 0, sizeof(iev));
602	IEEE80211_ADDR_COPY(iev.iev_addr, mac);
603	rt_ieee80211msg(ifp, op, &iev, sizeof(iev));
604	CURVNET_RESTORE();
605}
606
607void
608ieee80211_notify_node_join(struct ieee80211_node *ni, int newassoc)
609{
610	struct ieee80211vap *vap = ni->ni_vap;
611	struct ifnet *ifp = vap->iv_ifp;
612
613	CURVNET_SET_QUIET(ifp->if_vnet);
614	IEEE80211_NOTE(vap, IEEE80211_MSG_NODE, ni, "%snode join",
615	    (ni == vap->iv_bss) ? "bss " : "");
616
617	if (ni == vap->iv_bss) {
618		notify_macaddr(ifp, newassoc ?
619		    RTM_IEEE80211_ASSOC : RTM_IEEE80211_REASSOC, ni->ni_bssid);
620		if_link_state_change(ifp, LINK_STATE_UP);
621	} else {
622		notify_macaddr(ifp, newassoc ?
623		    RTM_IEEE80211_JOIN : RTM_IEEE80211_REJOIN, ni->ni_macaddr);
624	}
625	CURVNET_RESTORE();
626}
627
628void
629ieee80211_notify_node_leave(struct ieee80211_node *ni)
630{
631	struct ieee80211vap *vap = ni->ni_vap;
632	struct ifnet *ifp = vap->iv_ifp;
633
634	CURVNET_SET_QUIET(ifp->if_vnet);
635	IEEE80211_NOTE(vap, IEEE80211_MSG_NODE, ni, "%snode leave",
636	    (ni == vap->iv_bss) ? "bss " : "");
637
638	if (ni == vap->iv_bss) {
639		rt_ieee80211msg(ifp, RTM_IEEE80211_DISASSOC, NULL, 0);
640		if_link_state_change(ifp, LINK_STATE_DOWN);
641	} else {
642		/* fire off wireless event station leaving */
643		notify_macaddr(ifp, RTM_IEEE80211_LEAVE, ni->ni_macaddr);
644	}
645	CURVNET_RESTORE();
646}
647
648void
649ieee80211_notify_scan_done(struct ieee80211vap *vap)
650{
651	struct ifnet *ifp = vap->iv_ifp;
652
653	IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN, "%s\n", "notify scan done");
654
655	/* dispatch wireless event indicating scan completed */
656	CURVNET_SET(ifp->if_vnet);
657	rt_ieee80211msg(ifp, RTM_IEEE80211_SCAN, NULL, 0);
658	CURVNET_RESTORE();
659}
660
661void
662ieee80211_notify_replay_failure(struct ieee80211vap *vap,
663	const struct ieee80211_frame *wh, const struct ieee80211_key *k,
664	u_int64_t rsc, int tid)
665{
666	struct ifnet *ifp = vap->iv_ifp;
667
668	IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_CRYPTO, wh->i_addr2,
669	    "%s replay detected tid %d <rsc %ju, csc %ju, keyix %u rxkeyix %u>",
670	    k->wk_cipher->ic_name, tid, (intmax_t) rsc,
671	    (intmax_t) k->wk_keyrsc[tid],
672	    k->wk_keyix, k->wk_rxkeyix);
673
674	if (ifp != NULL) {		/* NB: for cipher test modules */
675		struct ieee80211_replay_event iev;
676
677		IEEE80211_ADDR_COPY(iev.iev_dst, wh->i_addr1);
678		IEEE80211_ADDR_COPY(iev.iev_src, wh->i_addr2);
679		iev.iev_cipher = k->wk_cipher->ic_cipher;
680		if (k->wk_rxkeyix != IEEE80211_KEYIX_NONE)
681			iev.iev_keyix = k->wk_rxkeyix;
682		else
683			iev.iev_keyix = k->wk_keyix;
684		iev.iev_keyrsc = k->wk_keyrsc[tid];
685		iev.iev_rsc = rsc;
686		CURVNET_SET(ifp->if_vnet);
687		rt_ieee80211msg(ifp, RTM_IEEE80211_REPLAY, &iev, sizeof(iev));
688		CURVNET_RESTORE();
689	}
690}
691
692void
693ieee80211_notify_michael_failure(struct ieee80211vap *vap,
694	const struct ieee80211_frame *wh, u_int keyix)
695{
696	struct ifnet *ifp = vap->iv_ifp;
697
698	IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_CRYPTO, wh->i_addr2,
699	    "michael MIC verification failed <keyix %u>", keyix);
700	vap->iv_stats.is_rx_tkipmic++;
701
702	if (ifp != NULL) {		/* NB: for cipher test modules */
703		struct ieee80211_michael_event iev;
704
705		IEEE80211_ADDR_COPY(iev.iev_dst, wh->i_addr1);
706		IEEE80211_ADDR_COPY(iev.iev_src, wh->i_addr2);
707		iev.iev_cipher = IEEE80211_CIPHER_TKIP;
708		iev.iev_keyix = keyix;
709		CURVNET_SET(ifp->if_vnet);
710		rt_ieee80211msg(ifp, RTM_IEEE80211_MICHAEL, &iev, sizeof(iev));
711		CURVNET_RESTORE();
712	}
713}
714
715void
716ieee80211_notify_wds_discover(struct ieee80211_node *ni)
717{
718	struct ieee80211vap *vap = ni->ni_vap;
719	struct ifnet *ifp = vap->iv_ifp;
720
721	notify_macaddr(ifp, RTM_IEEE80211_WDS, ni->ni_macaddr);
722}
723
724void
725ieee80211_notify_csa(struct ieee80211com *ic,
726	const struct ieee80211_channel *c, int mode, int count)
727{
728	struct ieee80211_csa_event iev;
729	struct ieee80211vap *vap;
730	struct ifnet *ifp;
731
732	memset(&iev, 0, sizeof(iev));
733	iev.iev_flags = c->ic_flags;
734	iev.iev_freq = c->ic_freq;
735	iev.iev_ieee = c->ic_ieee;
736	iev.iev_mode = mode;
737	iev.iev_count = count;
738	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
739		ifp = vap->iv_ifp;
740		CURVNET_SET(ifp->if_vnet);
741		rt_ieee80211msg(ifp, RTM_IEEE80211_CSA, &iev, sizeof(iev));
742		CURVNET_RESTORE();
743	}
744}
745
746void
747ieee80211_notify_radar(struct ieee80211com *ic,
748	const struct ieee80211_channel *c)
749{
750	struct ieee80211_radar_event iev;
751	struct ieee80211vap *vap;
752	struct ifnet *ifp;
753
754	memset(&iev, 0, sizeof(iev));
755	iev.iev_flags = c->ic_flags;
756	iev.iev_freq = c->ic_freq;
757	iev.iev_ieee = c->ic_ieee;
758	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
759		ifp = vap->iv_ifp;
760		CURVNET_SET(ifp->if_vnet);
761		rt_ieee80211msg(ifp, RTM_IEEE80211_RADAR, &iev, sizeof(iev));
762		CURVNET_RESTORE();
763	}
764}
765
766void
767ieee80211_notify_cac(struct ieee80211com *ic,
768	const struct ieee80211_channel *c, enum ieee80211_notify_cac_event type)
769{
770	struct ieee80211_cac_event iev;
771	struct ieee80211vap *vap;
772	struct ifnet *ifp;
773
774	memset(&iev, 0, sizeof(iev));
775	iev.iev_flags = c->ic_flags;
776	iev.iev_freq = c->ic_freq;
777	iev.iev_ieee = c->ic_ieee;
778	iev.iev_type = type;
779	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
780		ifp = vap->iv_ifp;
781		CURVNET_SET(ifp->if_vnet);
782		rt_ieee80211msg(ifp, RTM_IEEE80211_CAC, &iev, sizeof(iev));
783		CURVNET_RESTORE();
784	}
785}
786
787void
788ieee80211_notify_node_deauth(struct ieee80211_node *ni)
789{
790	struct ieee80211vap *vap = ni->ni_vap;
791	struct ifnet *ifp = vap->iv_ifp;
792
793	IEEE80211_NOTE(vap, IEEE80211_MSG_NODE, ni, "%s", "node deauth");
794
795	notify_macaddr(ifp, RTM_IEEE80211_DEAUTH, ni->ni_macaddr);
796}
797
798void
799ieee80211_notify_node_auth(struct ieee80211_node *ni)
800{
801	struct ieee80211vap *vap = ni->ni_vap;
802	struct ifnet *ifp = vap->iv_ifp;
803
804	IEEE80211_NOTE(vap, IEEE80211_MSG_NODE, ni, "%s", "node auth");
805
806	notify_macaddr(ifp, RTM_IEEE80211_AUTH, ni->ni_macaddr);
807}
808
809void
810ieee80211_notify_country(struct ieee80211vap *vap,
811	const uint8_t bssid[IEEE80211_ADDR_LEN], const uint8_t cc[2])
812{
813	struct ifnet *ifp = vap->iv_ifp;
814	struct ieee80211_country_event iev;
815
816	memset(&iev, 0, sizeof(iev));
817	IEEE80211_ADDR_COPY(iev.iev_addr, bssid);
818	iev.iev_cc[0] = cc[0];
819	iev.iev_cc[1] = cc[1];
820	CURVNET_SET(ifp->if_vnet);
821	rt_ieee80211msg(ifp, RTM_IEEE80211_COUNTRY, &iev, sizeof(iev));
822	CURVNET_RESTORE();
823}
824
825void
826ieee80211_notify_radio(struct ieee80211com *ic, int state)
827{
828	struct ieee80211_radio_event iev;
829	struct ieee80211vap *vap;
830	struct ifnet *ifp;
831
832	memset(&iev, 0, sizeof(iev));
833	iev.iev_state = state;
834	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
835		ifp = vap->iv_ifp;
836		CURVNET_SET(ifp->if_vnet);
837		rt_ieee80211msg(ifp, RTM_IEEE80211_RADIO, &iev, sizeof(iev));
838		CURVNET_RESTORE();
839	}
840}
841
842void
843ieee80211_load_module(const char *modname)
844{
845
846#ifdef notyet
847	(void)kern_kldload(curthread, modname, NULL);
848#else
849	printf("%s: load the %s module by hand for now.\n", __func__, modname);
850#endif
851}
852
853static eventhandler_tag wlan_bpfevent;
854
855static void
856bpf_track(void *arg, struct ifnet *ifp, int dlt, int attach)
857{
858	/* NB: identify vap's by if_init */
859	if (dlt == DLT_IEEE802_11_RADIO &&
860	    ifp->if_init == ieee80211_init) {
861		struct ieee80211vap *vap = ifp->if_softc;
862		/*
863		 * Track bpf radiotap listener state.  We mark the vap
864		 * to indicate if any listener is present and the com
865		 * to indicate if any listener exists on any associated
866		 * vap.  This flag is used by drivers to prepare radiotap
867		 * state only when needed.
868		 */
869		if (attach) {
870			ieee80211_syncflag_ext(vap, IEEE80211_FEXT_BPF);
871			if (vap->iv_opmode == IEEE80211_M_MONITOR)
872				atomic_add_int(&vap->iv_ic->ic_montaps, 1);
873		} else if (!bpf_peers_present(vap->iv_rawbpf)) {
874			ieee80211_syncflag_ext(vap, -IEEE80211_FEXT_BPF);
875			if (vap->iv_opmode == IEEE80211_M_MONITOR)
876				atomic_subtract_int(&vap->iv_ic->ic_montaps, 1);
877		}
878	}
879}
880
881/*
882 * Module glue.
883 *
884 * NB: the module name is "wlan" for compatibility with NetBSD.
885 */
886static int
887wlan_modevent(module_t mod, int type, void *unused)
888{
889	switch (type) {
890	case MOD_LOAD:
891		if (bootverbose)
892			printf("wlan: <802.11 Link Layer>\n");
893		wlan_bpfevent = EVENTHANDLER_REGISTER(bpf_track,
894		    bpf_track, 0, EVENTHANDLER_PRI_ANY);
895		wlan_cloner = if_clone_simple(wlanname, wlan_clone_create,
896		    wlan_clone_destroy, 0);
897		return 0;
898	case MOD_UNLOAD:
899		if_clone_detach(wlan_cloner);
900		EVENTHANDLER_DEREGISTER(bpf_track, wlan_bpfevent);
901		return 0;
902	}
903	return EINVAL;
904}
905
906static moduledata_t wlan_mod = {
907	wlanname,
908	wlan_modevent,
909	0
910};
911DECLARE_MODULE(wlan, wlan_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
912MODULE_VERSION(wlan, 1);
913MODULE_DEPEND(wlan, ether, 1, 1, 1);
914#ifdef	IEEE80211_ALQ
915MODULE_DEPEND(wlan, alq, 1, 1, 1);
916#endif	/* IEEE80211_ALQ */
917
918