Home | History | Annotate | Line # | Download | only in net80211
ieee80211_netbsd.c revision 1.31.2.2
      1  1.31.2.2      phil /*	$NetBSD: ieee80211_netbsd.c,v 1.31.2.2 2018/07/12 16:35:34 phil Exp $ */
      2  1.31.2.2      phil 
      3  1.31.2.1      phil /*-
      4  1.31.2.1      phil  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
      5  1.31.2.1      phil  *
      6  1.31.2.1      phil  * Copyright (c) 2003-2009 Sam Leffler, Errno Consulting
      7       1.1    dyoung  * All rights reserved.
      8       1.1    dyoung  *
      9       1.1    dyoung  * Redistribution and use in source and binary forms, with or without
     10       1.1    dyoung  * modification, are permitted provided that the following conditions
     11       1.1    dyoung  * are met:
     12       1.1    dyoung  * 1. Redistributions of source code must retain the above copyright
     13       1.1    dyoung  *    notice, this list of conditions and the following disclaimer.
     14       1.1    dyoung  * 2. Redistributions in binary form must reproduce the above copyright
     15       1.1    dyoung  *    notice, this list of conditions and the following disclaimer in the
     16       1.1    dyoung  *    documentation and/or other materials provided with the distribution.
     17       1.1    dyoung  *
     18       1.1    dyoung  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     19       1.1    dyoung  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     20       1.1    dyoung  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     21       1.1    dyoung  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     22       1.1    dyoung  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     23       1.1    dyoung  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     24       1.1    dyoung  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     25       1.1    dyoung  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     26       1.1    dyoung  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     27       1.1    dyoung  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     28       1.1    dyoung  */
     29       1.1    dyoung 
     30       1.1    dyoung #include <sys/cdefs.h>
     31  1.31.2.2      phil /*  __FBSDID("$FreeBSD$");  */
     32  1.31.2.2      phil __KERNEL_RCSID(0, "$NetBSD: ieee80211_netbsd.c,v 1.31.2.2 2018/07/12 16:35:34 phil Exp $");
     33       1.1    dyoung 
     34       1.1    dyoung /*
     35  1.31.2.2      phil  * IEEE 802.11 support (NetBSD-specific code)
     36       1.1    dyoung  */
     37  1.31.2.2      phil 
     38  1.31.2.1      phil #include "opt_wlan.h"
     39  1.31.2.1      phil 
     40  1.31.2.2      phil #include <sys/atomic.h>
     41       1.1    dyoung #include <sys/param.h>
     42  1.31.2.1      phil #include <sys/systm.h>
     43       1.1    dyoung #include <sys/kernel.h>
     44  1.31.2.1      phil #include <sys/malloc.h>
     45  1.31.2.1      phil #include <sys/mbuf.h>
     46  1.31.2.1      phil #include <sys/module.h>
     47       1.1    dyoung #include <sys/proc.h>
     48       1.1    dyoung #include <sys/sysctl.h>
     49  1.31.2.2      phil #include <sys/syslog.h>
     50       1.1    dyoung 
     51       1.1    dyoung #include <sys/socket.h>
     52       1.1    dyoung 
     53  1.31.2.1      phil #include <net/bpf.h>
     54       1.1    dyoung #include <net/if.h>
     55  1.31.2.1      phil #include <net/if_dl.h>
     56  1.31.2.2      phil #include <net/if_ether.h>
     57       1.1    dyoung #include <net/if_media.h>
     58  1.31.2.1      phil #include <net/if_types.h>
     59       1.1    dyoung #include <net/route.h>
     60       1.1    dyoung 
     61       1.1    dyoung #include <net80211/ieee80211_var.h>
     62  1.31.2.1      phil #include <net80211/ieee80211_input.h>
     63       1.1    dyoung 
     64  1.31.2.2      phil #ifdef notyet
     65  1.31.2.1      phil SYSCTL_NODE(_net, OID_AUTO, wlan, CTLFLAG_RD, 0, "IEEE 80211 parameters");
     66      1.26     pooka 
     67       1.1    dyoung #ifdef IEEE80211_DEBUG
     68  1.31.2.1      phil static int	ieee80211_debug = 0;
     69  1.31.2.1      phil SYSCTL_INT(_net_wlan, OID_AUTO, debug, CTLFLAG_RW, &ieee80211_debug,
     70  1.31.2.1      phil 	    0, "debugging printfs");
     71       1.1    dyoung #endif
     72  1.31.2.2      phil #endif /* notyet */
     73       1.1    dyoung 
     74  1.31.2.2      phil /* static MALLOC_DEFINE(M_80211_COM, "80211com", "802.11 com state"); NNN */
     75       1.8     skrll 
     76  1.31.2.2      phil #ifdef notyet
     77  1.31.2.1      phil static const char wlanname[] = "wlan";
     78  1.31.2.1      phil static struct if_clone *wlan_cloner;
     79       1.8     skrll 
     80      1.12      yamt static int
     81  1.31.2.1      phil wlan_clone_create(struct if_clone *ifc, int unit, caddr_t params)
     82       1.8     skrll {
     83  1.31.2.1      phil 	struct ieee80211_clone_params cp;
     84  1.31.2.1      phil 	struct ieee80211vap *vap;
     85  1.31.2.1      phil 	struct ieee80211com *ic;
     86  1.31.2.1      phil 	int error;
     87       1.8     skrll 
     88  1.31.2.1      phil 	error = copyin(params, &cp, sizeof(cp));
     89  1.31.2.1      phil 	if (error)
     90  1.31.2.1      phil 		return error;
     91  1.31.2.1      phil 	ic = ieee80211_find_com(cp.icp_parent);
     92  1.31.2.1      phil 	if (ic == NULL)
     93  1.31.2.1      phil 		return ENXIO;
     94  1.31.2.1      phil 	if (cp.icp_opmode >= IEEE80211_OPMODE_MAX) {
     95  1.31.2.1      phil 		ic_printf(ic, "%s: invalid opmode %d\n", __func__,
     96  1.31.2.1      phil 		    cp.icp_opmode);
     97  1.31.2.1      phil 		return EINVAL;
     98  1.31.2.1      phil 	}
     99  1.31.2.1      phil 	if ((ic->ic_caps & ieee80211_opcap[cp.icp_opmode]) == 0) {
    100  1.31.2.1      phil 		ic_printf(ic, "%s mode not supported\n",
    101  1.31.2.1      phil 		    ieee80211_opmode_name[cp.icp_opmode]);
    102  1.31.2.1      phil 		return EOPNOTSUPP;
    103  1.31.2.1      phil 	}
    104  1.31.2.1      phil 	if ((cp.icp_flags & IEEE80211_CLONE_TDMA) &&
    105  1.31.2.1      phil #ifdef IEEE80211_SUPPORT_TDMA
    106  1.31.2.1      phil 	    (ic->ic_caps & IEEE80211_C_TDMA) == 0
    107  1.31.2.1      phil #else
    108  1.31.2.1      phil 	    (1)
    109  1.31.2.1      phil #endif
    110  1.31.2.1      phil 	) {
    111  1.31.2.1      phil 		ic_printf(ic, "TDMA not supported\n");
    112  1.31.2.1      phil 		return EOPNOTSUPP;
    113  1.31.2.1      phil 	}
    114  1.31.2.1      phil 	vap = ic->ic_vap_create(ic, wlanname, unit,
    115  1.31.2.1      phil 			cp.icp_opmode, cp.icp_flags, cp.icp_bssid,
    116  1.31.2.1      phil 			cp.icp_flags & IEEE80211_CLONE_MACADDR ?
    117  1.31.2.1      phil 			    cp.icp_macaddr : ic->ic_macaddr);
    118      1.26     pooka 
    119  1.31.2.1      phil 	return (vap == NULL ? EIO : 0);
    120  1.31.2.1      phil }
    121      1.22      matt 
    122  1.31.2.1      phil static void
    123  1.31.2.1      phil wlan_clone_destroy(struct ifnet *ifp)
    124  1.31.2.1      phil {
    125  1.31.2.1      phil 	struct ieee80211vap *vap = ifp->if_softc;
    126  1.31.2.1      phil 	struct ieee80211com *ic = vap->iv_ic;
    127      1.12      yamt 
    128  1.31.2.1      phil 	ic->ic_vap_delete(vap);
    129       1.8     skrll }
    130  1.31.2.2      phil #endif
    131       1.8     skrll 
    132      1.10   thorpej void
    133  1.31.2.1      phil ieee80211_vap_destroy(struct ieee80211vap *vap)
    134      1.10   thorpej {
    135  1.31.2.2      phil #ifdef notyet
    136  1.31.2.1      phil 	CURVNET_SET(vap->iv_ifp->if_vnet);
    137  1.31.2.1      phil 	if_clone_destroyif(wlan_cloner, vap->iv_ifp);
    138  1.31.2.1      phil 	CURVNET_RESTORE();
    139  1.31.2.2      phil #else
    140  1.31.2.2      phil 	panic("ieee80211_vap_destroy");
    141  1.31.2.2      phil #endif
    142      1.10   thorpej }
    143      1.10   thorpej 
    144  1.31.2.2      phil #ifdef notyet
    145  1.31.2.1      phil int
    146  1.31.2.1      phil ieee80211_sysctl_msecs_ticks(SYSCTL_HANDLER_ARGS)
    147       1.1    dyoung {
    148  1.31.2.1      phil 	int msecs = ticks_to_msecs(*(int *)arg1);
    149       1.2    dyoung 	int error, t;
    150      1.30      maxv 
    151  1.31.2.1      phil 	error = sysctl_handle_int(oidp, &msecs, 0, req);
    152  1.31.2.1      phil 	if (error || !req->newptr)
    153      1.30      maxv 		return error;
    154  1.31.2.1      phil 	t = msecs_to_ticks(msecs);
    155  1.31.2.1      phil 	*(int *)arg1 = (t < 1) ? 1 : t;
    156      1.30      maxv 	return 0;
    157       1.1    dyoung }
    158       1.1    dyoung 
    159       1.1    dyoung static int
    160  1.31.2.1      phil ieee80211_sysctl_inact(SYSCTL_HANDLER_ARGS)
    161       1.1    dyoung {
    162  1.31.2.1      phil 	int inact = (*(int *)arg1) * IEEE80211_INACT_WAIT;
    163  1.31.2.1      phil 	int error;
    164       1.2    dyoung 
    165  1.31.2.1      phil 	error = sysctl_handle_int(oidp, &inact, 0, req);
    166  1.31.2.1      phil 	if (error || !req->newptr)
    167  1.31.2.1      phil 		return error;
    168  1.31.2.1      phil 	*(int *)arg1 = inact / IEEE80211_INACT_WAIT;
    169  1.31.2.1      phil 	return 0;
    170       1.2    dyoung }
    171       1.2    dyoung 
    172  1.31.2.1      phil static int
    173  1.31.2.1      phil ieee80211_sysctl_parent(SYSCTL_HANDLER_ARGS)
    174       1.2    dyoung {
    175  1.31.2.1      phil 	struct ieee80211com *ic = arg1;
    176       1.2    dyoung 
    177  1.31.2.1      phil 	return SYSCTL_OUT_STR(req, ic->ic_name);
    178       1.1    dyoung }
    179       1.1    dyoung 
    180  1.31.2.1      phil static int
    181  1.31.2.1      phil ieee80211_sysctl_radar(SYSCTL_HANDLER_ARGS)
    182       1.1    dyoung {
    183  1.31.2.1      phil 	struct ieee80211com *ic = arg1;
    184  1.31.2.1      phil 	int t = 0, error;
    185       1.1    dyoung 
    186  1.31.2.1      phil 	error = sysctl_handle_int(oidp, &t, 0, req);
    187  1.31.2.1      phil 	if (error || !req->newptr)
    188  1.31.2.1      phil 		return error;
    189  1.31.2.1      phil 	IEEE80211_LOCK(ic);
    190  1.31.2.1      phil 	ieee80211_dfs_notify_radar(ic, ic->ic_curchan);
    191  1.31.2.1      phil 	IEEE80211_UNLOCK(ic);
    192  1.31.2.1      phil 	return 0;
    193       1.2    dyoung }
    194       1.2    dyoung 
    195       1.2    dyoung /*
    196  1.31.2.1      phil  * For now, just restart everything.
    197       1.2    dyoung  *
    198  1.31.2.1      phil  * Later on, it'd be nice to have a separate VAP restart to
    199  1.31.2.1      phil  * full-device restart.
    200      1.30      maxv  */
    201  1.31.2.1      phil static int
    202  1.31.2.1      phil ieee80211_sysctl_vap_restart(SYSCTL_HANDLER_ARGS)
    203       1.2    dyoung {
    204  1.31.2.1      phil 	struct ieee80211vap *vap = arg1;
    205  1.31.2.1      phil 	int t = 0, error;
    206       1.1    dyoung 
    207  1.31.2.1      phil 	error = sysctl_handle_int(oidp, &t, 0, req);
    208  1.31.2.1      phil 	if (error || !req->newptr)
    209  1.31.2.1      phil 		return error;
    210       1.2    dyoung 
    211  1.31.2.1      phil 	ieee80211_restart_all(vap->iv_ic);
    212  1.31.2.1      phil 	return 0;
    213       1.2    dyoung }
    214  1.31.2.2      phil #endif /* notyet */
    215       1.2    dyoung 
    216  1.31.2.1      phil void
    217  1.31.2.1      phil ieee80211_sysctl_attach(struct ieee80211com *ic)
    218  1.31.2.1      phil {
    219       1.2    dyoung }
    220       1.2    dyoung 
    221  1.31.2.1      phil void
    222  1.31.2.1      phil ieee80211_sysctl_detach(struct ieee80211com *ic)
    223       1.2    dyoung {
    224       1.2    dyoung }
    225       1.2    dyoung 
    226  1.31.2.1      phil void
    227  1.31.2.1      phil ieee80211_sysctl_vattach(struct ieee80211vap *vap)
    228       1.2    dyoung {
    229  1.31.2.2      phil #ifdef notyet
    230  1.31.2.1      phil 	struct ifnet *ifp = vap->iv_ifp;
    231  1.31.2.1      phil 	struct sysctl_ctx_list *ctx;
    232  1.31.2.1      phil 	struct sysctl_oid *oid;
    233  1.31.2.1      phil 	char num[14];			/* sufficient for 32 bits */
    234  1.31.2.1      phil 
    235  1.31.2.1      phil 	ctx = (struct sysctl_ctx_list *) IEEE80211_MALLOC(sizeof(struct sysctl_ctx_list),
    236  1.31.2.1      phil 		M_DEVBUF, IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
    237  1.31.2.1      phil 	if (ctx == NULL) {
    238  1.31.2.1      phil 		if_printf(ifp, "%s: cannot allocate sysctl context!\n",
    239  1.31.2.1      phil 			__func__);
    240       1.2    dyoung 		return;
    241  1.31.2.1      phil 	}
    242  1.31.2.1      phil 	sysctl_ctx_init(ctx);
    243  1.31.2.1      phil 	snprintf(num, sizeof(num), "%u", ifp->if_dunit);
    244  1.31.2.1      phil 	oid = SYSCTL_ADD_NODE(ctx, &SYSCTL_NODE_CHILDREN(_net, wlan),
    245  1.31.2.1      phil 		OID_AUTO, num, CTLFLAG_RD, NULL, "");
    246  1.31.2.1      phil 	SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
    247  1.31.2.1      phil 		"%parent", CTLTYPE_STRING | CTLFLAG_RD, vap->iv_ic, 0,
    248  1.31.2.1      phil 		ieee80211_sysctl_parent, "A", "parent device");
    249  1.31.2.1      phil 	SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
    250  1.31.2.1      phil 		"driver_caps", CTLFLAG_RW, &vap->iv_caps, 0,
    251  1.31.2.1      phil 		"driver capabilities");
    252       1.2    dyoung #ifdef IEEE80211_DEBUG
    253  1.31.2.1      phil 	vap->iv_debug = ieee80211_debug;
    254  1.31.2.1      phil 	SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
    255  1.31.2.1      phil 		"debug", CTLFLAG_RW, &vap->iv_debug, 0,
    256  1.31.2.1      phil 		"control debugging printfs");
    257      1.30      maxv #endif
    258  1.31.2.1      phil 	SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
    259  1.31.2.1      phil 		"bmiss_max", CTLFLAG_RW, &vap->iv_bmiss_max, 0,
    260  1.31.2.1      phil 		"consecutive beacon misses before scanning");
    261  1.31.2.1      phil 	/* XXX inherit from tunables */
    262  1.31.2.1      phil 	SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
    263  1.31.2.1      phil 		"inact_run", CTLTYPE_INT | CTLFLAG_RW, &vap->iv_inact_run, 0,
    264  1.31.2.1      phil 		ieee80211_sysctl_inact, "I",
    265  1.31.2.1      phil 		"station inactivity timeout (sec)");
    266  1.31.2.1      phil 	SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
    267  1.31.2.1      phil 		"inact_probe", CTLTYPE_INT | CTLFLAG_RW, &vap->iv_inact_probe, 0,
    268  1.31.2.1      phil 		ieee80211_sysctl_inact, "I",
    269  1.31.2.1      phil 		"station inactivity probe timeout (sec)");
    270  1.31.2.1      phil 	SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
    271  1.31.2.1      phil 		"inact_auth", CTLTYPE_INT | CTLFLAG_RW, &vap->iv_inact_auth, 0,
    272  1.31.2.1      phil 		ieee80211_sysctl_inact, "I",
    273  1.31.2.1      phil 		"station authentication timeout (sec)");
    274  1.31.2.1      phil 	SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
    275  1.31.2.1      phil 		"inact_init", CTLTYPE_INT | CTLFLAG_RW, &vap->iv_inact_init, 0,
    276  1.31.2.1      phil 		ieee80211_sysctl_inact, "I",
    277  1.31.2.1      phil 		"station initial state timeout (sec)");
    278  1.31.2.1      phil 	if (vap->iv_htcaps & IEEE80211_HTC_HT) {
    279  1.31.2.1      phil 		SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
    280  1.31.2.1      phil 			"ampdu_mintraffic_bk", CTLFLAG_RW,
    281  1.31.2.1      phil 			&vap->iv_ampdu_mintraffic[WME_AC_BK], 0,
    282  1.31.2.1      phil 			"BK traffic tx aggr threshold (pps)");
    283  1.31.2.1      phil 		SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
    284  1.31.2.1      phil 			"ampdu_mintraffic_be", CTLFLAG_RW,
    285  1.31.2.1      phil 			&vap->iv_ampdu_mintraffic[WME_AC_BE], 0,
    286  1.31.2.1      phil 			"BE traffic tx aggr threshold (pps)");
    287  1.31.2.1      phil 		SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
    288  1.31.2.1      phil 			"ampdu_mintraffic_vo", CTLFLAG_RW,
    289  1.31.2.1      phil 			&vap->iv_ampdu_mintraffic[WME_AC_VO], 0,
    290  1.31.2.1      phil 			"VO traffic tx aggr threshold (pps)");
    291  1.31.2.1      phil 		SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
    292  1.31.2.1      phil 			"ampdu_mintraffic_vi", CTLFLAG_RW,
    293  1.31.2.1      phil 			&vap->iv_ampdu_mintraffic[WME_AC_VI], 0,
    294  1.31.2.1      phil 			"VI traffic tx aggr threshold (pps)");
    295  1.31.2.1      phil 	}
    296  1.31.2.1      phil 
    297  1.31.2.1      phil 	SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
    298  1.31.2.1      phil 		"force_restart", CTLTYPE_INT | CTLFLAG_RW, vap, 0,
    299  1.31.2.1      phil 		ieee80211_sysctl_vap_restart, "I",
    300  1.31.2.1      phil 		"force a VAP restart");
    301  1.31.2.1      phil 
    302  1.31.2.1      phil 	if (vap->iv_caps & IEEE80211_C_DFS) {
    303  1.31.2.1      phil 		SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
    304  1.31.2.1      phil 			"radar", CTLTYPE_INT | CTLFLAG_RW, vap->iv_ic, 0,
    305  1.31.2.1      phil 			ieee80211_sysctl_radar, "I", "simulate radar event");
    306  1.31.2.1      phil 	}
    307  1.31.2.1      phil 	vap->iv_sysctl = ctx;
    308  1.31.2.1      phil 	vap->iv_oid = oid;
    309  1.31.2.2      phil #endif
    310  1.31.2.1      phil }
    311       1.2    dyoung 
    312  1.31.2.1      phil void
    313  1.31.2.1      phil ieee80211_sysctl_vdetach(struct ieee80211vap *vap)
    314  1.31.2.1      phil {
    315  1.31.2.2      phil #ifdef notyet
    316  1.31.2.1      phil 	if (vap->iv_sysctl != NULL) {
    317  1.31.2.1      phil 		sysctl_ctx_free(vap->iv_sysctl);
    318  1.31.2.1      phil 		IEEE80211_FREE(vap->iv_sysctl, M_DEVBUF);
    319  1.31.2.1      phil 		vap->iv_sysctl = NULL;
    320  1.31.2.1      phil 	}
    321  1.31.2.2      phil #endif
    322       1.1    dyoung }
    323       1.1    dyoung 
    324  1.31.2.2      phil 
    325       1.1    dyoung int
    326       1.1    dyoung ieee80211_node_dectestref(struct ieee80211_node *ni)
    327       1.1    dyoung {
    328  1.31.2.1      phil 	/* XXX need equivalent of atomic_dec_and_test */
    329  1.31.2.1      phil 	atomic_subtract_int(&ni->ni_refcnt, 1);
    330  1.31.2.2      phil 	return atomic_cas_uint(&ni->ni_refcnt, 0, 1) == 0;
    331       1.2    dyoung }
    332       1.2    dyoung 
    333       1.2    dyoung void
    334      1.15  degroote ieee80211_drain_ifq(struct ifqueue *ifq)
    335      1.15  degroote {
    336      1.15  degroote 	struct ieee80211_node *ni;
    337      1.15  degroote 	struct mbuf *m;
    338      1.15  degroote 
    339      1.15  degroote 	for (;;) {
    340      1.15  degroote 		IF_DEQUEUE(ifq, m);
    341      1.15  degroote 		if (m == NULL)
    342      1.15  degroote 			break;
    343      1.15  degroote 
    344  1.31.2.2      phil 		ni = (struct ieee80211_node *)m_get_rcvif_NOMPSAFE(m);
    345  1.31.2.2      phil 		FBSDKASSERT(ni != NULL, ("frame w/o node"));
    346      1.15  degroote 		ieee80211_free_node(ni);
    347  1.31.2.2      phil 		ieee80211_free_mbuf(m);
    348      1.15  degroote 	}
    349      1.15  degroote }
    350      1.15  degroote 
    351      1.15  degroote void
    352  1.31.2.1      phil ieee80211_flush_ifq(struct ifqueue *ifq, struct ieee80211vap *vap)
    353       1.2    dyoung {
    354  1.31.2.1      phil 	struct ieee80211_node *ni;
    355  1.31.2.1      phil 	struct mbuf *m, **mprev;
    356       1.2    dyoung 
    357  1.31.2.2      phil 	IFQ_LOCK(ifq);
    358  1.31.2.1      phil 	mprev = &ifq->ifq_head;
    359  1.31.2.1      phil 	while ((m = *mprev) != NULL) {
    360  1.31.2.2      phil 		ni = (struct ieee80211_node *)m_get_rcvif_NOMPSAFE(m);
    361  1.31.2.1      phil 		if (ni != NULL && ni->ni_vap == vap) {
    362  1.31.2.1      phil 			*mprev = m->m_nextpkt;		/* remove from list */
    363  1.31.2.1      phil 			ifq->ifq_len--;
    364       1.2    dyoung 
    365  1.31.2.1      phil 			ieee80211_free_node(ni);	/* reclaim ref */
    366  1.31.2.2      phil 			ieee80211_free_mbuf(m);
    367  1.31.2.1      phil 		} else
    368  1.31.2.1      phil 			mprev = &m->m_nextpkt;
    369  1.31.2.1      phil 	}
    370  1.31.2.1      phil 	/* recalculate tail ptr */
    371  1.31.2.1      phil 	m = ifq->ifq_head;
    372  1.31.2.1      phil 	for (; m != NULL && m->m_nextpkt != NULL; m = m->m_nextpkt)
    373  1.31.2.1      phil 		;
    374  1.31.2.1      phil 	ifq->ifq_tail = m;
    375  1.31.2.2      phil 	IFQ_UNLOCK(ifq);
    376       1.2    dyoung }
    377       1.2    dyoung 
    378       1.1    dyoung /*
    379  1.31.2.1      phil  * As above, for mbufs allocated with m_gethdr/MGETHDR
    380  1.31.2.1      phil  * or initialized by M_COPY_PKTHDR.
    381  1.31.2.1      phil  */
    382  1.31.2.1      phil #define	MC_ALIGN(m, len)						\
    383  1.31.2.1      phil do {									\
    384  1.31.2.1      phil 	(m)->m_data += rounddown2(MCLBYTES - (len), sizeof(long));	\
    385  1.31.2.1      phil } while (/* CONSTCOND */ 0)
    386  1.31.2.1      phil 
    387  1.31.2.1      phil /*
    388       1.1    dyoung  * Allocate and setup a management frame of the specified
    389       1.1    dyoung  * size.  We return the mbuf and a pointer to the start
    390       1.1    dyoung  * of the contiguous data area that's been reserved based
    391       1.1    dyoung  * on the packet length.  The data area is forced to 32-bit
    392       1.1    dyoung  * alignment and the buffer length to a multiple of 4 bytes.
    393       1.1    dyoung  * This is done mainly so beacon frames (that require this)
    394       1.1    dyoung  * can use this interface too.
    395       1.1    dyoung  */
    396       1.1    dyoung struct mbuf *
    397  1.31.2.1      phil ieee80211_getmgtframe(uint8_t **frm, int headroom, int pktlen)
    398       1.1    dyoung {
    399       1.1    dyoung 	struct mbuf *m;
    400       1.1    dyoung 	u_int len;
    401       1.1    dyoung 
    402       1.1    dyoung 	/*
    403       1.1    dyoung 	 * NB: we know the mbuf routines will align the data area
    404       1.1    dyoung 	 *     so we don't need to do anything special.
    405       1.1    dyoung 	 */
    406  1.31.2.1      phil 	len = roundup2(headroom + pktlen, 4);
    407  1.31.2.2      phil 	FBSDKASSERT(len <= MCLBYTES, ("802.11 mgt frame too large: %u", len));
    408  1.31.2.1      phil 	if (len < MINCLSIZE) {
    409  1.31.2.1      phil 		m = m_gethdr(M_NOWAIT, MT_DATA);
    410       1.1    dyoung 		/*
    411       1.1    dyoung 		 * Align the data in case additional headers are added.
    412       1.1    dyoung 		 * This should only happen when a WEP header is added
    413       1.1    dyoung 		 * which only happens for shared key authentication mgt
    414       1.1    dyoung 		 * frames which all fit in MHLEN.
    415       1.1    dyoung 		 */
    416       1.1    dyoung 		if (m != NULL)
    417  1.31.2.1      phil 			M_ALIGN(m, len);
    418      1.30      maxv 	} else {
    419  1.31.2.1      phil 		m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
    420  1.31.2.1      phil 		if (m != NULL)
    421  1.31.2.1      phil 			MC_ALIGN(m, len);
    422      1.30      maxv 	}
    423       1.1    dyoung 	if (m != NULL) {
    424  1.31.2.1      phil 		m->m_data += headroom;
    425       1.1    dyoung 		*frm = m->m_data;
    426       1.1    dyoung 	}
    427       1.1    dyoung 	return m;
    428       1.1    dyoung }
    429       1.1    dyoung 
    430  1.31.2.1      phil #ifndef __NO_STRICT_ALIGNMENT
    431  1.31.2.1      phil /*
    432  1.31.2.1      phil  * Re-align the payload in the mbuf.  This is mainly used (right now)
    433  1.31.2.1      phil  * to handle IP header alignment requirements on certain architectures.
    434  1.31.2.1      phil  */
    435  1.31.2.1      phil struct mbuf *
    436  1.31.2.1      phil ieee80211_realign(struct ieee80211vap *vap, struct mbuf *m, size_t align)
    437  1.31.2.1      phil {
    438  1.31.2.1      phil 	int pktlen, space;
    439  1.31.2.1      phil 	struct mbuf *n;
    440  1.31.2.1      phil 
    441  1.31.2.1      phil 	pktlen = m->m_pkthdr.len;
    442  1.31.2.1      phil 	space = pktlen + align;
    443  1.31.2.1      phil 	if (space < MINCLSIZE)
    444  1.31.2.1      phil 		n = m_gethdr(M_NOWAIT, MT_DATA);
    445  1.31.2.1      phil 	else {
    446  1.31.2.1      phil 		n = m_getjcl(M_NOWAIT, MT_DATA, M_PKTHDR,
    447  1.31.2.1      phil 		    space <= MCLBYTES ?     MCLBYTES :
    448  1.31.2.1      phil #if MJUMPAGESIZE != MCLBYTES
    449  1.31.2.1      phil 		    space <= MJUMPAGESIZE ? MJUMPAGESIZE :
    450  1.31.2.1      phil #endif
    451  1.31.2.1      phil 		    space <= MJUM9BYTES ?   MJUM9BYTES : MJUM16BYTES);
    452  1.31.2.1      phil 	}
    453  1.31.2.1      phil 	if (__predict_true(n != NULL)) {
    454  1.31.2.1      phil 		m_move_pkthdr(n, m);
    455  1.31.2.1      phil 		n->m_data = (caddr_t)(ALIGN(n->m_data + align) - align);
    456  1.31.2.1      phil 		m_copydata(m, 0, pktlen, mtod(n, caddr_t));
    457  1.31.2.1      phil 		n->m_len = pktlen;
    458  1.31.2.1      phil 	} else {
    459  1.31.2.1      phil 		IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
    460  1.31.2.1      phil 		    mtod(m, const struct ieee80211_frame *), NULL,
    461  1.31.2.1      phil 		    "%s", "no mbuf to realign");
    462  1.31.2.1      phil 		vap->iv_stats.is_rx_badalign++;
    463  1.31.2.1      phil 	}
    464  1.31.2.1      phil 	m_freem(m);
    465  1.31.2.1      phil 	return n;
    466  1.31.2.1      phil }
    467  1.31.2.1      phil #endif /* !__NO_STRICT_ALIGNMENT */
    468  1.31.2.1      phil 
    469  1.31.2.1      phil int
    470  1.31.2.1      phil ieee80211_add_callback(struct mbuf *m,
    471  1.31.2.1      phil 	void (*func)(struct ieee80211_node *, void *, int), void *arg)
    472  1.31.2.1      phil {
    473  1.31.2.1      phil 	struct m_tag *mtag;
    474  1.31.2.1      phil 	struct ieee80211_cb *cb;
    475  1.31.2.1      phil 
    476  1.31.2.2      phil 	mtag = m_tag_get(/*MTAG_ABI_NET80211*/ NET80211_TAG_CALLBACK,
    477  1.31.2.1      phil 			sizeof(struct ieee80211_cb), M_NOWAIT);
    478  1.31.2.1      phil 	if (mtag == NULL)
    479  1.31.2.1      phil 		return 0;
    480  1.31.2.1      phil 
    481  1.31.2.1      phil 	cb = (struct ieee80211_cb *)(mtag+1);
    482  1.31.2.1      phil 	cb->func = func;
    483  1.31.2.1      phil 	cb->arg = arg;
    484  1.31.2.1      phil 	m_tag_prepend(m, mtag);
    485  1.31.2.1      phil 	m->m_flags |= M_TXCB;
    486  1.31.2.1      phil 	return 1;
    487  1.31.2.1      phil }
    488  1.31.2.1      phil 
    489  1.31.2.1      phil int
    490  1.31.2.1      phil ieee80211_add_xmit_params(struct mbuf *m,
    491  1.31.2.1      phil     const struct ieee80211_bpf_params *params)
    492  1.31.2.1      phil {
    493  1.31.2.1      phil 	struct m_tag *mtag;
    494  1.31.2.1      phil 	struct ieee80211_tx_params *tx;
    495  1.31.2.1      phil 
    496  1.31.2.2      phil 	mtag = m_tag_get(/*MTAG_ABI_NET80211*/ NET80211_TAG_XMIT_PARAMS,
    497  1.31.2.1      phil 	    sizeof(struct ieee80211_tx_params), M_NOWAIT);
    498  1.31.2.1      phil 	if (mtag == NULL)
    499  1.31.2.1      phil 		return (0);
    500  1.31.2.1      phil 
    501  1.31.2.1      phil 	tx = (struct ieee80211_tx_params *)(mtag+1);
    502  1.31.2.1      phil 	memcpy(&tx->params, params, sizeof(struct ieee80211_bpf_params));
    503  1.31.2.1      phil 	m_tag_prepend(m, mtag);
    504  1.31.2.1      phil 	return (1);
    505  1.31.2.1      phil }
    506  1.31.2.1      phil 
    507  1.31.2.1      phil int
    508  1.31.2.1      phil ieee80211_get_xmit_params(struct mbuf *m,
    509  1.31.2.1      phil     struct ieee80211_bpf_params *params)
    510  1.31.2.1      phil {
    511  1.31.2.1      phil 	struct m_tag *mtag;
    512  1.31.2.1      phil 	struct ieee80211_tx_params *tx;
    513  1.31.2.1      phil 
    514  1.31.2.2      phil 	mtag = m_tag_find(m, /*MTAG_ABI_NET80211,*/ NET80211_TAG_XMIT_PARAMS,
    515  1.31.2.1      phil 	    NULL);
    516  1.31.2.1      phil 	if (mtag == NULL)
    517  1.31.2.1      phil 		return (-1);
    518  1.31.2.1      phil 	tx = (struct ieee80211_tx_params *)(mtag + 1);
    519  1.31.2.1      phil 	memcpy(params, &tx->params, sizeof(struct ieee80211_bpf_params));
    520  1.31.2.1      phil 	return (0);
    521  1.31.2.1      phil }
    522  1.31.2.1      phil 
    523       1.1    dyoung void
    524  1.31.2.1      phil ieee80211_process_callback(struct ieee80211_node *ni,
    525  1.31.2.1      phil 	struct mbuf *m, int status)
    526  1.31.2.1      phil {
    527  1.31.2.1      phil 	struct m_tag *mtag;
    528  1.31.2.1      phil 
    529  1.31.2.2      phil 	mtag = m_tag_find(m, /*MTAG_ABI_NET80211,*/ NET80211_TAG_CALLBACK, NULL);
    530  1.31.2.1      phil 	if (mtag != NULL) {
    531  1.31.2.1      phil 		struct ieee80211_cb *cb = (struct ieee80211_cb *)(mtag+1);
    532  1.31.2.1      phil 		cb->func(ni, cb->arg, status);
    533  1.31.2.1      phil 	}
    534  1.31.2.1      phil }
    535  1.31.2.1      phil 
    536  1.31.2.1      phil /*
    537  1.31.2.1      phil  * Add RX parameters to the given mbuf.
    538  1.31.2.1      phil  *
    539  1.31.2.1      phil  * Returns 1 if OK, 0 on error.
    540  1.31.2.1      phil  */
    541  1.31.2.1      phil int
    542  1.31.2.1      phil ieee80211_add_rx_params(struct mbuf *m, const struct ieee80211_rx_stats *rxs)
    543  1.31.2.1      phil {
    544  1.31.2.1      phil 	struct m_tag *mtag;
    545  1.31.2.1      phil 	struct ieee80211_rx_params *rx;
    546  1.31.2.1      phil 
    547  1.31.2.2      phil 	mtag = m_tag_get(/*MTAG_ABI_NET80211,*/ NET80211_TAG_RECV_PARAMS,
    548  1.31.2.1      phil 	    sizeof(struct ieee80211_rx_stats), M_NOWAIT);
    549  1.31.2.1      phil 	if (mtag == NULL)
    550  1.31.2.1      phil 		return (0);
    551  1.31.2.1      phil 
    552  1.31.2.1      phil 	rx = (struct ieee80211_rx_params *)(mtag + 1);
    553  1.31.2.1      phil 	memcpy(&rx->params, rxs, sizeof(*rxs));
    554  1.31.2.1      phil 	m_tag_prepend(m, mtag);
    555  1.31.2.1      phil 	return (1);
    556  1.31.2.1      phil }
    557  1.31.2.1      phil 
    558  1.31.2.1      phil int
    559  1.31.2.1      phil ieee80211_get_rx_params(struct mbuf *m, struct ieee80211_rx_stats *rxs)
    560  1.31.2.1      phil {
    561  1.31.2.1      phil 	struct m_tag *mtag;
    562  1.31.2.1      phil 	struct ieee80211_rx_params *rx;
    563  1.31.2.1      phil 
    564  1.31.2.2      phil 	mtag = m_tag_find(m, /*MTAG_ABI_NET80211,*/ NET80211_TAG_RECV_PARAMS,
    565  1.31.2.1      phil 	    NULL);
    566  1.31.2.1      phil 	if (mtag == NULL)
    567  1.31.2.1      phil 		return (-1);
    568  1.31.2.1      phil 	rx = (struct ieee80211_rx_params *)(mtag + 1);
    569  1.31.2.1      phil 	memcpy(rxs, &rx->params, sizeof(*rxs));
    570  1.31.2.1      phil 	return (0);
    571  1.31.2.1      phil }
    572  1.31.2.1      phil 
    573  1.31.2.1      phil const struct ieee80211_rx_stats *
    574  1.31.2.1      phil ieee80211_get_rx_params_ptr(struct mbuf *m)
    575  1.31.2.1      phil {
    576  1.31.2.1      phil 	struct m_tag *mtag;
    577  1.31.2.1      phil 	struct ieee80211_rx_params *rx;
    578  1.31.2.1      phil 
    579  1.31.2.2      phil 	mtag = m_tag_find(m, /*MTAG_ABI_NET80211,*/ NET80211_TAG_RECV_PARAMS,
    580  1.31.2.1      phil 	    NULL);
    581  1.31.2.1      phil 	if (mtag == NULL)
    582  1.31.2.1      phil 		return (NULL);
    583  1.31.2.1      phil 	rx = (struct ieee80211_rx_params *)(mtag + 1);
    584  1.31.2.1      phil 	return (&rx->params);
    585  1.31.2.1      phil }
    586  1.31.2.1      phil 
    587  1.31.2.1      phil 
    588  1.31.2.1      phil /*
    589  1.31.2.1      phil  * Add TOA parameters to the given mbuf.
    590  1.31.2.1      phil  */
    591  1.31.2.1      phil int
    592  1.31.2.1      phil ieee80211_add_toa_params(struct mbuf *m, const struct ieee80211_toa_params *p)
    593  1.31.2.1      phil {
    594  1.31.2.1      phil 	struct m_tag *mtag;
    595  1.31.2.1      phil 	struct ieee80211_toa_params *rp;
    596  1.31.2.1      phil 
    597  1.31.2.2      phil 	mtag = m_tag_get(/*MTAG_ABI_NET80211,*/ NET80211_TAG_TOA_PARAMS,
    598  1.31.2.1      phil 	    sizeof(struct ieee80211_toa_params), M_NOWAIT);
    599  1.31.2.1      phil 	if (mtag == NULL)
    600  1.31.2.1      phil 		return (0);
    601  1.31.2.1      phil 
    602  1.31.2.1      phil 	rp = (struct ieee80211_toa_params *)(mtag + 1);
    603  1.31.2.1      phil 	memcpy(rp, p, sizeof(*rp));
    604  1.31.2.1      phil 	m_tag_prepend(m, mtag);
    605  1.31.2.1      phil 	return (1);
    606  1.31.2.1      phil }
    607  1.31.2.1      phil 
    608  1.31.2.1      phil int
    609  1.31.2.1      phil ieee80211_get_toa_params(struct mbuf *m, struct ieee80211_toa_params *p)
    610  1.31.2.1      phil {
    611  1.31.2.1      phil 	struct m_tag *mtag;
    612  1.31.2.1      phil 	struct ieee80211_toa_params *rp;
    613  1.31.2.1      phil 
    614  1.31.2.2      phil 	mtag = m_tag_find(m, /*MTAG_ABI_NET80211,*/ NET80211_TAG_TOA_PARAMS,
    615  1.31.2.1      phil 	    NULL);
    616  1.31.2.1      phil 	if (mtag == NULL)
    617  1.31.2.1      phil 		return (0);
    618  1.31.2.1      phil 	rp = (struct ieee80211_toa_params *)(mtag + 1);
    619  1.31.2.1      phil 	if (p != NULL)
    620  1.31.2.1      phil 		memcpy(p, rp, sizeof(*p));
    621  1.31.2.1      phil 	return (1);
    622  1.31.2.1      phil }
    623  1.31.2.1      phil 
    624  1.31.2.1      phil /*
    625  1.31.2.1      phil  * Transmit a frame to the parent interface.
    626  1.31.2.1      phil  */
    627  1.31.2.1      phil int
    628  1.31.2.1      phil ieee80211_parent_xmitpkt(struct ieee80211com *ic, struct mbuf *m)
    629       1.1    dyoung {
    630  1.31.2.1      phil 	int error;
    631  1.31.2.1      phil 
    632  1.31.2.1      phil 	/*
    633  1.31.2.1      phil 	 * Assert the IC TX lock is held - this enforces the
    634  1.31.2.1      phil 	 * processing -> queuing order is maintained
    635  1.31.2.1      phil 	 */
    636  1.31.2.1      phil 	IEEE80211_TX_LOCK_ASSERT(ic);
    637  1.31.2.1      phil 	error = ic->ic_transmit(ic, m);
    638  1.31.2.1      phil 	if (error) {
    639  1.31.2.1      phil 		struct ieee80211_node *ni;
    640  1.31.2.1      phil 
    641  1.31.2.2      phil 		ni = (struct ieee80211_node *)m_get_rcvif_NOMPSAFE(m);
    642  1.31.2.1      phil 
    643  1.31.2.1      phil 		/* XXX number of fragments */
    644  1.31.2.1      phil 		if_inc_counter(ni->ni_vap->iv_ifp, IFCOUNTER_OERRORS, 1);
    645  1.31.2.1      phil 		ieee80211_free_node(ni);
    646  1.31.2.1      phil 		ieee80211_free_mbuf(m);
    647  1.31.2.1      phil 	}
    648  1.31.2.1      phil 	return (error);
    649       1.1    dyoung }
    650       1.1    dyoung 
    651  1.31.2.1      phil /*
    652  1.31.2.1      phil  * Transmit a frame to the VAP interface.
    653  1.31.2.1      phil  */
    654  1.31.2.1      phil int
    655  1.31.2.1      phil ieee80211_vap_xmitpkt(struct ieee80211vap *vap, struct mbuf *m)
    656  1.31.2.1      phil {
    657  1.31.2.1      phil 	struct ifnet *ifp = vap->iv_ifp;
    658  1.31.2.1      phil 
    659  1.31.2.1      phil 	/*
    660  1.31.2.1      phil 	 * When transmitting via the VAP, we shouldn't hold
    661  1.31.2.1      phil 	 * any IC TX lock as the VAP TX path will acquire it.
    662  1.31.2.1      phil 	 */
    663  1.31.2.1      phil 	IEEE80211_TX_UNLOCK_ASSERT(vap->iv_ic);
    664  1.31.2.1      phil 
    665  1.31.2.1      phil 	return (ifp->if_transmit(ifp, m));
    666  1.31.2.1      phil 
    667  1.31.2.1      phil }
    668  1.31.2.1      phil 
    669       1.1    dyoung void
    670  1.31.2.1      phil get_random_bytes(void *p, size_t n)
    671       1.1    dyoung {
    672  1.31.2.1      phil 	uint8_t *dp = p;
    673       1.1    dyoung 
    674  1.31.2.1      phil 	while (n > 0) {
    675  1.31.2.1      phil 		uint32_t v = arc4random();
    676  1.31.2.1      phil 		size_t nb = n > sizeof(uint32_t) ? sizeof(uint32_t) : n;
    677  1.31.2.1      phil 		bcopy(&v, dp, n > sizeof(uint32_t) ? sizeof(uint32_t) : n);
    678  1.31.2.1      phil 		dp += sizeof(uint32_t), n -= nb;
    679  1.31.2.1      phil 	}
    680  1.31.2.1      phil }
    681       1.3    dyoung 
    682  1.31.2.1      phil /*
    683  1.31.2.1      phil  * Helper function for events that pass just a single mac address.
    684  1.31.2.1      phil  */
    685  1.31.2.1      phil static void
    686  1.31.2.1      phil notify_macaddr(struct ifnet *ifp, int op, const uint8_t mac[IEEE80211_ADDR_LEN])
    687  1.31.2.1      phil {
    688  1.31.2.1      phil 	struct ieee80211_join_event iev;
    689  1.31.2.1      phil 
    690  1.31.2.1      phil 	CURVNET_SET(ifp->if_vnet);
    691       1.7    dyoung 	memset(&iev, 0, sizeof(iev));
    692  1.31.2.1      phil 	IEEE80211_ADDR_COPY(iev.iev_addr, mac);
    693  1.31.2.1      phil 	rt_ieee80211msg(ifp, op, &iev, sizeof(iev));
    694  1.31.2.1      phil 	CURVNET_RESTORE();
    695  1.31.2.1      phil }
    696  1.31.2.1      phil 
    697  1.31.2.1      phil void
    698  1.31.2.1      phil ieee80211_notify_node_join(struct ieee80211_node *ni, int newassoc)
    699  1.31.2.1      phil {
    700  1.31.2.1      phil 	struct ieee80211vap *vap = ni->ni_vap;
    701  1.31.2.1      phil 	struct ifnet *ifp = vap->iv_ifp;
    702  1.31.2.1      phil 
    703  1.31.2.1      phil 	CURVNET_SET_QUIET(ifp->if_vnet);
    704  1.31.2.1      phil 	IEEE80211_NOTE(vap, IEEE80211_MSG_NODE, ni, "%snode join",
    705  1.31.2.1      phil 	    (ni == vap->iv_bss) ? "bss " : "");
    706  1.31.2.1      phil 
    707  1.31.2.1      phil 	if (ni == vap->iv_bss) {
    708  1.31.2.1      phil 		notify_macaddr(ifp, newassoc ?
    709  1.31.2.1      phil 		    RTM_IEEE80211_ASSOC : RTM_IEEE80211_REASSOC, ni->ni_bssid);
    710       1.1    dyoung 		if_link_state_change(ifp, LINK_STATE_UP);
    711      1.16  christos 	} else {
    712  1.31.2.1      phil 		notify_macaddr(ifp, newassoc ?
    713  1.31.2.1      phil 		    RTM_IEEE80211_JOIN : RTM_IEEE80211_REJOIN, ni->ni_macaddr);
    714       1.1    dyoung 	}
    715  1.31.2.1      phil 	CURVNET_RESTORE();
    716       1.1    dyoung }
    717       1.1    dyoung 
    718       1.1    dyoung void
    719  1.31.2.1      phil ieee80211_notify_node_leave(struct ieee80211_node *ni)
    720       1.1    dyoung {
    721  1.31.2.1      phil 	struct ieee80211vap *vap = ni->ni_vap;
    722  1.31.2.1      phil 	struct ifnet *ifp = vap->iv_ifp;
    723       1.1    dyoung 
    724  1.31.2.1      phil 	CURVNET_SET_QUIET(ifp->if_vnet);
    725  1.31.2.1      phil 	IEEE80211_NOTE(vap, IEEE80211_MSG_NODE, ni, "%snode leave",
    726  1.31.2.1      phil 	    (ni == vap->iv_bss) ? "bss " : "");
    727       1.3    dyoung 
    728  1.31.2.1      phil 	if (ni == vap->iv_bss) {
    729       1.1    dyoung 		rt_ieee80211msg(ifp, RTM_IEEE80211_DISASSOC, NULL, 0);
    730       1.1    dyoung 		if_link_state_change(ifp, LINK_STATE_DOWN);
    731       1.1    dyoung 	} else {
    732       1.1    dyoung 		/* fire off wireless event station leaving */
    733  1.31.2.1      phil 		notify_macaddr(ifp, RTM_IEEE80211_LEAVE, ni->ni_macaddr);
    734       1.1    dyoung 	}
    735  1.31.2.1      phil 	CURVNET_RESTORE();
    736       1.1    dyoung }
    737       1.1    dyoung 
    738       1.1    dyoung void
    739  1.31.2.1      phil ieee80211_notify_scan_done(struct ieee80211vap *vap)
    740       1.1    dyoung {
    741  1.31.2.1      phil 	struct ifnet *ifp = vap->iv_ifp;
    742       1.1    dyoung 
    743  1.31.2.1      phil 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN, "%s\n", "notify scan done");
    744       1.1    dyoung 
    745       1.1    dyoung 	/* dispatch wireless event indicating scan completed */
    746  1.31.2.1      phil 	CURVNET_SET(ifp->if_vnet);
    747       1.1    dyoung 	rt_ieee80211msg(ifp, RTM_IEEE80211_SCAN, NULL, 0);
    748  1.31.2.1      phil 	CURVNET_RESTORE();
    749       1.1    dyoung }
    750       1.1    dyoung 
    751       1.1    dyoung void
    752  1.31.2.1      phil ieee80211_notify_replay_failure(struct ieee80211vap *vap,
    753       1.1    dyoung 	const struct ieee80211_frame *wh, const struct ieee80211_key *k,
    754  1.31.2.1      phil 	u_int64_t rsc, int tid)
    755       1.1    dyoung {
    756  1.31.2.1      phil 	struct ifnet *ifp = vap->iv_ifp;
    757       1.1    dyoung 
    758  1.31.2.1      phil 	IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_CRYPTO, wh->i_addr2,
    759  1.31.2.1      phil 	    "%s replay detected tid %d <rsc %ju, csc %ju, keyix %u rxkeyix %u>",
    760  1.31.2.1      phil 	    k->wk_cipher->ic_name, tid, (intmax_t) rsc,
    761  1.31.2.1      phil 	    (intmax_t) k->wk_keyrsc[tid],
    762       1.8     skrll 	    k->wk_keyix, k->wk_rxkeyix);
    763       1.1    dyoung 
    764       1.1    dyoung 	if (ifp != NULL) {		/* NB: for cipher test modules */
    765       1.1    dyoung 		struct ieee80211_replay_event iev;
    766       1.1    dyoung 
    767       1.1    dyoung 		IEEE80211_ADDR_COPY(iev.iev_dst, wh->i_addr1);
    768       1.1    dyoung 		IEEE80211_ADDR_COPY(iev.iev_src, wh->i_addr2);
    769       1.1    dyoung 		iev.iev_cipher = k->wk_cipher->ic_cipher;
    770       1.8     skrll 		if (k->wk_rxkeyix != IEEE80211_KEYIX_NONE)
    771       1.8     skrll 			iev.iev_keyix = k->wk_rxkeyix;
    772       1.8     skrll 		else
    773       1.8     skrll 			iev.iev_keyix = k->wk_keyix;
    774  1.31.2.1      phil 		iev.iev_keyrsc = k->wk_keyrsc[tid];
    775       1.1    dyoung 		iev.iev_rsc = rsc;
    776  1.31.2.1      phil 		CURVNET_SET(ifp->if_vnet);
    777       1.1    dyoung 		rt_ieee80211msg(ifp, RTM_IEEE80211_REPLAY, &iev, sizeof(iev));
    778  1.31.2.1      phil 		CURVNET_RESTORE();
    779       1.1    dyoung 	}
    780       1.1    dyoung }
    781       1.1    dyoung 
    782       1.1    dyoung void
    783  1.31.2.1      phil ieee80211_notify_michael_failure(struct ieee80211vap *vap,
    784       1.1    dyoung 	const struct ieee80211_frame *wh, u_int keyix)
    785       1.1    dyoung {
    786  1.31.2.1      phil 	struct ifnet *ifp = vap->iv_ifp;
    787       1.1    dyoung 
    788  1.31.2.1      phil 	IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_CRYPTO, wh->i_addr2,
    789  1.31.2.1      phil 	    "michael MIC verification failed <keyix %u>", keyix);
    790  1.31.2.1      phil 	vap->iv_stats.is_rx_tkipmic++;
    791       1.1    dyoung 
    792       1.1    dyoung 	if (ifp != NULL) {		/* NB: for cipher test modules */
    793       1.1    dyoung 		struct ieee80211_michael_event iev;
    794       1.1    dyoung 
    795       1.1    dyoung 		IEEE80211_ADDR_COPY(iev.iev_dst, wh->i_addr1);
    796       1.1    dyoung 		IEEE80211_ADDR_COPY(iev.iev_src, wh->i_addr2);
    797       1.1    dyoung 		iev.iev_cipher = IEEE80211_CIPHER_TKIP;
    798       1.1    dyoung 		iev.iev_keyix = keyix;
    799  1.31.2.1      phil 		CURVNET_SET(ifp->if_vnet);
    800       1.1    dyoung 		rt_ieee80211msg(ifp, RTM_IEEE80211_MICHAEL, &iev, sizeof(iev));
    801  1.31.2.1      phil 		CURVNET_RESTORE();
    802       1.1    dyoung 	}
    803       1.1    dyoung }
    804       1.1    dyoung 
    805       1.1    dyoung void
    806  1.31.2.1      phil ieee80211_notify_wds_discover(struct ieee80211_node *ni)
    807       1.1    dyoung {
    808  1.31.2.1      phil 	struct ieee80211vap *vap = ni->ni_vap;
    809  1.31.2.1      phil 	struct ifnet *ifp = vap->iv_ifp;
    810  1.31.2.1      phil 
    811  1.31.2.1      phil 	notify_macaddr(ifp, RTM_IEEE80211_WDS, ni->ni_macaddr);
    812  1.31.2.1      phil }
    813  1.31.2.1      phil 
    814  1.31.2.1      phil void
    815  1.31.2.1      phil ieee80211_notify_csa(struct ieee80211com *ic,
    816  1.31.2.1      phil 	const struct ieee80211_channel *c, int mode, int count)
    817  1.31.2.1      phil {
    818  1.31.2.1      phil 	struct ieee80211_csa_event iev;
    819  1.31.2.1      phil 	struct ieee80211vap *vap;
    820  1.31.2.1      phil 	struct ifnet *ifp;
    821       1.1    dyoung 
    822  1.31.2.1      phil 	memset(&iev, 0, sizeof(iev));
    823  1.31.2.1      phil 	iev.iev_flags = c->ic_flags;
    824  1.31.2.1      phil 	iev.iev_freq = c->ic_freq;
    825  1.31.2.1      phil 	iev.iev_ieee = c->ic_ieee;
    826  1.31.2.1      phil 	iev.iev_mode = mode;
    827  1.31.2.1      phil 	iev.iev_count = count;
    828  1.31.2.1      phil 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
    829  1.31.2.1      phil 		ifp = vap->iv_ifp;
    830  1.31.2.1      phil 		CURVNET_SET(ifp->if_vnet);
    831  1.31.2.1      phil 		rt_ieee80211msg(ifp, RTM_IEEE80211_CSA, &iev, sizeof(iev));
    832  1.31.2.1      phil 		CURVNET_RESTORE();
    833  1.31.2.1      phil 	}
    834  1.31.2.1      phil }
    835  1.31.2.1      phil 
    836  1.31.2.1      phil void
    837  1.31.2.1      phil ieee80211_notify_radar(struct ieee80211com *ic,
    838  1.31.2.1      phil 	const struct ieee80211_channel *c)
    839  1.31.2.1      phil {
    840  1.31.2.1      phil 	struct ieee80211_radar_event iev;
    841  1.31.2.1      phil 	struct ieee80211vap *vap;
    842  1.31.2.1      phil 	struct ifnet *ifp;
    843  1.31.2.1      phil 
    844  1.31.2.1      phil 	memset(&iev, 0, sizeof(iev));
    845  1.31.2.1      phil 	iev.iev_flags = c->ic_flags;
    846  1.31.2.1      phil 	iev.iev_freq = c->ic_freq;
    847  1.31.2.1      phil 	iev.iev_ieee = c->ic_ieee;
    848  1.31.2.1      phil 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
    849  1.31.2.1      phil 		ifp = vap->iv_ifp;
    850  1.31.2.1      phil 		CURVNET_SET(ifp->if_vnet);
    851  1.31.2.1      phil 		rt_ieee80211msg(ifp, RTM_IEEE80211_RADAR, &iev, sizeof(iev));
    852  1.31.2.1      phil 		CURVNET_RESTORE();
    853  1.31.2.1      phil 	}
    854  1.31.2.1      phil }
    855  1.31.2.1      phil 
    856  1.31.2.1      phil void
    857  1.31.2.1      phil ieee80211_notify_cac(struct ieee80211com *ic,
    858  1.31.2.1      phil 	const struct ieee80211_channel *c, enum ieee80211_notify_cac_event type)
    859  1.31.2.1      phil {
    860  1.31.2.1      phil 	struct ieee80211_cac_event iev;
    861  1.31.2.1      phil 	struct ieee80211vap *vap;
    862  1.31.2.1      phil 	struct ifnet *ifp;
    863  1.31.2.1      phil 
    864  1.31.2.1      phil 	memset(&iev, 0, sizeof(iev));
    865  1.31.2.1      phil 	iev.iev_flags = c->ic_flags;
    866  1.31.2.1      phil 	iev.iev_freq = c->ic_freq;
    867  1.31.2.1      phil 	iev.iev_ieee = c->ic_ieee;
    868  1.31.2.1      phil 	iev.iev_type = type;
    869  1.31.2.1      phil 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
    870  1.31.2.1      phil 		ifp = vap->iv_ifp;
    871  1.31.2.1      phil 		CURVNET_SET(ifp->if_vnet);
    872  1.31.2.1      phil 		rt_ieee80211msg(ifp, RTM_IEEE80211_CAC, &iev, sizeof(iev));
    873  1.31.2.1      phil 		CURVNET_RESTORE();
    874  1.31.2.1      phil 	}
    875  1.31.2.1      phil }
    876  1.31.2.1      phil 
    877  1.31.2.1      phil void
    878  1.31.2.1      phil ieee80211_notify_node_deauth(struct ieee80211_node *ni)
    879  1.31.2.1      phil {
    880  1.31.2.1      phil 	struct ieee80211vap *vap = ni->ni_vap;
    881  1.31.2.1      phil 	struct ifnet *ifp = vap->iv_ifp;
    882  1.31.2.1      phil 
    883  1.31.2.1      phil 	IEEE80211_NOTE(vap, IEEE80211_MSG_NODE, ni, "%s", "node deauth");
    884  1.31.2.1      phil 
    885  1.31.2.1      phil 	notify_macaddr(ifp, RTM_IEEE80211_DEAUTH, ni->ni_macaddr);
    886  1.31.2.1      phil }
    887  1.31.2.1      phil 
    888  1.31.2.1      phil void
    889  1.31.2.1      phil ieee80211_notify_node_auth(struct ieee80211_node *ni)
    890  1.31.2.1      phil {
    891  1.31.2.1      phil 	struct ieee80211vap *vap = ni->ni_vap;
    892  1.31.2.1      phil 	struct ifnet *ifp = vap->iv_ifp;
    893  1.31.2.1      phil 
    894  1.31.2.1      phil 	IEEE80211_NOTE(vap, IEEE80211_MSG_NODE, ni, "%s", "node auth");
    895  1.31.2.1      phil 
    896  1.31.2.1      phil 	notify_macaddr(ifp, RTM_IEEE80211_AUTH, ni->ni_macaddr);
    897  1.31.2.1      phil }
    898  1.31.2.1      phil 
    899  1.31.2.1      phil void
    900  1.31.2.1      phil ieee80211_notify_country(struct ieee80211vap *vap,
    901  1.31.2.1      phil 	const uint8_t bssid[IEEE80211_ADDR_LEN], const uint8_t cc[2])
    902  1.31.2.1      phil {
    903  1.31.2.1      phil 	struct ifnet *ifp = vap->iv_ifp;
    904  1.31.2.1      phil 	struct ieee80211_country_event iev;
    905  1.31.2.1      phil 
    906  1.31.2.1      phil 	memset(&iev, 0, sizeof(iev));
    907  1.31.2.1      phil 	IEEE80211_ADDR_COPY(iev.iev_addr, bssid);
    908  1.31.2.1      phil 	iev.iev_cc[0] = cc[0];
    909  1.31.2.1      phil 	iev.iev_cc[1] = cc[1];
    910  1.31.2.1      phil 	CURVNET_SET(ifp->if_vnet);
    911  1.31.2.1      phil 	rt_ieee80211msg(ifp, RTM_IEEE80211_COUNTRY, &iev, sizeof(iev));
    912  1.31.2.1      phil 	CURVNET_RESTORE();
    913  1.31.2.1      phil }
    914  1.31.2.1      phil 
    915  1.31.2.1      phil void
    916  1.31.2.1      phil ieee80211_notify_radio(struct ieee80211com *ic, int state)
    917  1.31.2.1      phil {
    918  1.31.2.1      phil 	struct ieee80211_radio_event iev;
    919  1.31.2.1      phil 	struct ieee80211vap *vap;
    920  1.31.2.1      phil 	struct ifnet *ifp;
    921  1.31.2.1      phil 
    922  1.31.2.1      phil 	memset(&iev, 0, sizeof(iev));
    923  1.31.2.1      phil 	iev.iev_state = state;
    924  1.31.2.1      phil 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
    925  1.31.2.1      phil 		ifp = vap->iv_ifp;
    926  1.31.2.1      phil 		CURVNET_SET(ifp->if_vnet);
    927  1.31.2.1      phil 		rt_ieee80211msg(ifp, RTM_IEEE80211_RADIO, &iev, sizeof(iev));
    928  1.31.2.1      phil 		CURVNET_RESTORE();
    929       1.1    dyoung 	}
    930  1.31.2.1      phil }
    931  1.31.2.1      phil 
    932  1.31.2.1      phil void
    933  1.31.2.1      phil ieee80211_load_module(const char *modname)
    934  1.31.2.1      phil {
    935  1.31.2.1      phil 
    936  1.31.2.1      phil #ifdef notyet
    937  1.31.2.2      phil 	struct thread *td = curthread;
    938  1.31.2.2      phil 
    939  1.31.2.2      phil 	if (suser(td) == 0 && securelevel_gt(td->td_ucred, 0) == 0) {
    940  1.31.2.2      phil 		mtx_lock(&Giant);
    941  1.31.2.2      phil 		(void) linker_load_module(modname, NULL, NULL, NULL, NULL);
    942  1.31.2.2      phil 		mtx_unlock(&Giant);
    943  1.31.2.2      phil 	}
    944       1.1    dyoung #else
    945       1.1    dyoung 	printf("%s: load the %s module by hand for now.\n", __func__, modname);
    946       1.1    dyoung #endif
    947       1.1    dyoung }
    948      1.31      maxv 
    949  1.31.2.2      phil #ifdef notyet
    950  1.31.2.1      phil static eventhandler_tag wlan_bpfevent;
    951  1.31.2.1      phil static eventhandler_tag wlan_ifllevent;
    952  1.31.2.1      phil 
    953  1.31.2.1      phil static void
    954  1.31.2.1      phil bpf_track(void *arg, struct ifnet *ifp, int dlt, int attach)
    955  1.31.2.1      phil {
    956  1.31.2.1      phil 	/* NB: identify vap's by if_init */
    957  1.31.2.1      phil 	if (dlt == DLT_IEEE802_11_RADIO &&
    958  1.31.2.1      phil 	    ifp->if_init == ieee80211_init) {
    959  1.31.2.1      phil 		struct ieee80211vap *vap = ifp->if_softc;
    960  1.31.2.1      phil 		/*
    961  1.31.2.1      phil 		 * Track bpf radiotap listener state.  We mark the vap
    962  1.31.2.1      phil 		 * to indicate if any listener is present and the com
    963  1.31.2.1      phil 		 * to indicate if any listener exists on any associated
    964  1.31.2.1      phil 		 * vap.  This flag is used by drivers to prepare radiotap
    965  1.31.2.1      phil 		 * state only when needed.
    966  1.31.2.1      phil 		 */
    967  1.31.2.1      phil 		if (attach) {
    968  1.31.2.1      phil 			ieee80211_syncflag_ext(vap, IEEE80211_FEXT_BPF);
    969  1.31.2.1      phil 			if (vap->iv_opmode == IEEE80211_M_MONITOR)
    970  1.31.2.1      phil 				atomic_add_int(&vap->iv_ic->ic_montaps, 1);
    971  1.31.2.1      phil 		} else if (!bpf_peers_present(vap->iv_rawbpf)) {
    972  1.31.2.1      phil 			ieee80211_syncflag_ext(vap, -IEEE80211_FEXT_BPF);
    973  1.31.2.1      phil 			if (vap->iv_opmode == IEEE80211_M_MONITOR)
    974  1.31.2.1      phil 				atomic_subtract_int(&vap->iv_ic->ic_montaps, 1);
    975  1.31.2.1      phil 		}
    976  1.31.2.1      phil 	}
    977  1.31.2.1      phil }
    978      1.31      maxv 
    979      1.31      maxv /*
    980  1.31.2.1      phil  * Change MAC address on the vap (if was not started).
    981      1.31      maxv  */
    982  1.31.2.1      phil static void
    983  1.31.2.1      phil wlan_iflladdr(void *arg __unused, struct ifnet *ifp)
    984      1.31      maxv {
    985  1.31.2.1      phil 	/* NB: identify vap's by if_init */
    986  1.31.2.1      phil 	if (ifp->if_init == ieee80211_init &&
    987  1.31.2.1      phil 	    (ifp->if_flags & IFF_UP) == 0) {
    988  1.31.2.1      phil 		struct ieee80211vap *vap = ifp->if_softc;
    989      1.31      maxv 
    990  1.31.2.1      phil 		IEEE80211_ADDR_COPY(vap->iv_myaddr, IF_LLADDR(ifp));
    991  1.31.2.1      phil 	}
    992      1.31      maxv }
    993  1.31.2.2      phil #endif
    994  1.31.2.2      phil 
    995  1.31.2.2      phil void
    996  1.31.2.2      phil if_inc_counter(struct ifnet *ifp, ift_counter ifc, int64_t value)
    997  1.31.2.2      phil {
    998  1.31.2.2      phil 	switch (ifc) {
    999  1.31.2.2      phil 	case IFCOUNTER_IPACKETS:
   1000  1.31.2.2      phil 		ifp->if_data.ifi_ipackets += value;
   1001  1.31.2.2      phil 		break;
   1002  1.31.2.2      phil 	case IFCOUNTER_IERRORS:
   1003  1.31.2.2      phil 		ifp->if_data.ifi_ierrors += value;
   1004  1.31.2.2      phil 		break;
   1005  1.31.2.2      phil 	case IFCOUNTER_OPACKETS:
   1006  1.31.2.2      phil 		ifp->if_data.ifi_opackets += value;
   1007  1.31.2.2      phil 		break;
   1008  1.31.2.2      phil 	case IFCOUNTER_OERRORS:
   1009  1.31.2.2      phil 		ifp->if_data.ifi_oerrors += value;
   1010  1.31.2.2      phil 		break;
   1011  1.31.2.2      phil         case IFCOUNTER_COLLISIONS:
   1012  1.31.2.2      phil 		ifp->if_data.ifi_collisions += value;
   1013  1.31.2.2      phil 		break;
   1014  1.31.2.2      phil         case IFCOUNTER_IBYTES:
   1015  1.31.2.2      phil 		ifp->if_data.ifi_ibytes += value;
   1016  1.31.2.2      phil 		break;
   1017  1.31.2.2      phil         case IFCOUNTER_OBYTES:
   1018  1.31.2.2      phil 		ifp->if_data.ifi_obytes += value;
   1019  1.31.2.2      phil 		break;
   1020  1.31.2.2      phil         case IFCOUNTER_IMCASTS:
   1021  1.31.2.2      phil 		ifp->if_data.ifi_imcasts += value;
   1022  1.31.2.2      phil 		break;
   1023  1.31.2.2      phil         case IFCOUNTER_OMCASTS:
   1024  1.31.2.2      phil 		ifp->if_data.ifi_omcasts += value;
   1025  1.31.2.2      phil 		break;
   1026  1.31.2.2      phil         case IFCOUNTER_IQDROPS:
   1027  1.31.2.2      phil 		ifp->if_data.ifi_iqdrops += value;
   1028  1.31.2.2      phil 		break;
   1029  1.31.2.2      phil         case IFCOUNTER_OQDROPS:
   1030  1.31.2.2      phil 		/* ifp->if_data.ifi_oqdrops += value; No such field, just ignore it q*/
   1031  1.31.2.2      phil 		break;
   1032  1.31.2.2      phil         case IFCOUNTER_NOPROTO:
   1033  1.31.2.2      phil 		ifp->if_data.ifi_noproto += value;
   1034  1.31.2.2      phil 		break;
   1035  1.31.2.2      phil 	default:
   1036  1.31.2.2      phil 		panic("if_inc_counter: non-existant counter");
   1037  1.31.2.2      phil 	}
   1038  1.31.2.2      phil }
   1039  1.31.2.2      phil 
   1040      1.31      maxv 
   1041  1.31.2.2      phil #ifdef notyet
   1042      1.31      maxv /*
   1043  1.31.2.1      phil  * Module glue.
   1044      1.31      maxv  *
   1045  1.31.2.1      phil  * NB: the module name is "wlan" for compatibility with NetBSD.
   1046      1.31      maxv  */
   1047  1.31.2.1      phil static int
   1048  1.31.2.1      phil wlan_modevent(module_t mod, int type, void *unused)
   1049      1.31      maxv {
   1050  1.31.2.1      phil 	switch (type) {
   1051  1.31.2.1      phil 	case MOD_LOAD:
   1052  1.31.2.1      phil 		if (bootverbose)
   1053  1.31.2.1      phil 			printf("wlan: <802.11 Link Layer>\n");
   1054  1.31.2.1      phil 		wlan_bpfevent = EVENTHANDLER_REGISTER(bpf_track,
   1055  1.31.2.1      phil 		    bpf_track, 0, EVENTHANDLER_PRI_ANY);
   1056  1.31.2.1      phil 		wlan_ifllevent = EVENTHANDLER_REGISTER(iflladdr_event,
   1057  1.31.2.1      phil 		    wlan_iflladdr, NULL, EVENTHANDLER_PRI_ANY);
   1058  1.31.2.1      phil 		wlan_cloner = if_clone_simple(wlanname, wlan_clone_create,
   1059  1.31.2.1      phil 		    wlan_clone_destroy, 0);
   1060  1.31.2.1      phil 		return 0;
   1061  1.31.2.1      phil 	case MOD_UNLOAD:
   1062  1.31.2.1      phil 		if_clone_detach(wlan_cloner);
   1063  1.31.2.1      phil 		EVENTHANDLER_DEREGISTER(bpf_track, wlan_bpfevent);
   1064  1.31.2.1      phil 		EVENTHANDLER_DEREGISTER(iflladdr_event, wlan_ifllevent);
   1065  1.31.2.1      phil 		return 0;
   1066      1.31      maxv 	}
   1067  1.31.2.1      phil 	return EINVAL;
   1068      1.31      maxv }
   1069  1.31.2.1      phil 
   1070  1.31.2.1      phil static moduledata_t wlan_mod = {
   1071  1.31.2.1      phil 	wlanname,
   1072  1.31.2.1      phil 	wlan_modevent,
   1073  1.31.2.1      phil 	0
   1074  1.31.2.1      phil };
   1075  1.31.2.1      phil DECLARE_MODULE(wlan, wlan_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
   1076  1.31.2.1      phil MODULE_VERSION(wlan, 1);
   1077  1.31.2.1      phil MODULE_DEPEND(wlan, ether, 1, 1, 1);
   1078  1.31.2.2      phil #endif
   1079  1.31.2.2      phil 
   1080  1.31.2.1      phil #ifdef	IEEE80211_ALQ
   1081  1.31.2.1      phil MODULE_DEPEND(wlan, alq, 1, 1, 1);
   1082  1.31.2.1      phil #endif	/* IEEE80211_ALQ */
   1083  1.31.2.1      phil 
   1084  1.31.2.2      phil /* Missing support for if_printf in NetBSD ... */
   1085  1.31.2.2      phil int
   1086  1.31.2.2      phil if_printf(struct ifnet *ifp, const char *fmt, ...)
   1087  1.31.2.2      phil {
   1088  1.31.2.2      phil         char if_fmt[256];
   1089  1.31.2.2      phil         va_list ap;
   1090  1.31.2.2      phil 
   1091  1.31.2.2      phil         snprintf(if_fmt, sizeof(if_fmt), "%s: %s", ifp->if_xname, fmt);
   1092  1.31.2.2      phil         va_start(ap, fmt);
   1093  1.31.2.2      phil         vlog(LOG_INFO, if_fmt, ap);
   1094  1.31.2.2      phil         va_end(ap);
   1095  1.31.2.2      phil         return (0);
   1096  1.31.2.2      phil }
   1097  1.31.2.2      phil 
   1098  1.31.2.2      phil /*
   1099  1.31.2.2      phil  * Set the m_data pointer of a newly-allocated mbuf
   1100  1.31.2.2      phil  * to place an object of the specified size at the
   1101  1.31.2.2      phil  * end of the mbuf, longword aligned.
   1102  1.31.2.2      phil  */
   1103  1.31.2.2      phil void
   1104  1.31.2.2      phil m_align(struct mbuf *m, int len)
   1105  1.31.2.2      phil {
   1106  1.31.2.2      phil 	int adjust;
   1107  1.31.2.2      phil 
   1108  1.31.2.2      phil 	KASSERT(len != M_COPYALL);
   1109  1.31.2.2      phil 
   1110  1.31.2.2      phil 	if (m->m_flags & M_EXT)
   1111  1.31.2.2      phil 		adjust = m->m_ext.ext_size - len;
   1112  1.31.2.2      phil 	else if (m->m_flags & M_PKTHDR)
   1113  1.31.2.2      phil 		adjust = MHLEN - len;
   1114  1.31.2.2      phil 	else
   1115  1.31.2.2      phil 		adjust = MLEN - len;
   1116  1.31.2.2      phil 	m->m_data += adjust &~ (sizeof(long)-1);
   1117  1.31.2.2      phil }
   1118  1.31.2.2      phil 
   1119  1.31.2.2      phil /*
   1120  1.31.2.2      phil  * Append the specified data to the indicated mbuf chain,
   1121  1.31.2.2      phil  * Extend the mbuf chain if the new data does not fit in
   1122  1.31.2.2      phil  * existing space.
   1123  1.31.2.2      phil  *
   1124  1.31.2.2      phil  * Return 1 if able to complete the job; otherwise 0.
   1125  1.31.2.2      phil  */
   1126  1.31.2.2      phil int
   1127  1.31.2.2      phil m_append(struct mbuf *m0, int len, const void *cpv)
   1128  1.31.2.2      phil {
   1129  1.31.2.2      phil 	struct mbuf *m, *n;
   1130  1.31.2.2      phil 	int remainder, space;
   1131  1.31.2.2      phil 	const char *cp = cpv;
   1132  1.31.2.2      phil 
   1133  1.31.2.2      phil 	KASSERT(len != M_COPYALL);
   1134  1.31.2.2      phil 	for (m = m0; m->m_next != NULL; m = m->m_next)
   1135  1.31.2.2      phil 		continue;
   1136  1.31.2.2      phil 	remainder = len;
   1137  1.31.2.2      phil 	space = M_TRAILINGSPACE(m);
   1138  1.31.2.2      phil 	if (space > 0) {
   1139  1.31.2.2      phil 		/*
   1140  1.31.2.2      phil 		 * Copy into available space.
   1141  1.31.2.2      phil 		 */
   1142  1.31.2.2      phil 		if (space > remainder)
   1143  1.31.2.2      phil 			space = remainder;
   1144  1.31.2.2      phil 		memmove(mtod(m, char *) + m->m_len, cp, space);
   1145  1.31.2.2      phil 		m->m_len += space;
   1146  1.31.2.2      phil 		cp = cp + space, remainder -= space;
   1147  1.31.2.2      phil 	}
   1148  1.31.2.2      phil 	while (remainder > 0) {
   1149  1.31.2.2      phil 		/*
   1150  1.31.2.2      phil 		 * Allocate a new mbuf; could check space
   1151  1.31.2.2      phil 		 * and allocate a cluster instead.
   1152  1.31.2.2      phil 		 */
   1153  1.31.2.2      phil 		n = m_get(M_DONTWAIT, m->m_type);
   1154  1.31.2.2      phil 		if (n == NULL)
   1155  1.31.2.2      phil 			break;
   1156  1.31.2.2      phil 		n->m_len = min(MLEN, remainder);
   1157  1.31.2.2      phil 		memmove(mtod(n, void *), cp, n->m_len);
   1158  1.31.2.2      phil 		cp += n->m_len, remainder -= n->m_len;
   1159  1.31.2.2      phil 		m->m_next = n;
   1160  1.31.2.2      phil 		m = n;
   1161  1.31.2.2      phil 	}
   1162  1.31.2.2      phil 	if (m0->m_flags & M_PKTHDR)
   1163  1.31.2.2      phil 		m0->m_pkthdr.len += len - remainder;
   1164  1.31.2.2      phil 	return (remainder == 0);
   1165  1.31.2.2      phil }
   1166  1.31.2.2      phil 
   1167  1.31.2.2      phil /*
   1168  1.31.2.2      phil  * Create a writable copy of the mbuf chain.  While doing this
   1169  1.31.2.2      phil  * we compact the chain with a goal of producing a chain with
   1170  1.31.2.2      phil  * at most two mbufs.  The second mbuf in this chain is likely
   1171  1.31.2.2      phil  * to be a cluster.  The primary purpose of this work is to create
   1172  1.31.2.2      phil  * a writable packet for encryption, compression, etc.  The
   1173  1.31.2.2      phil  * secondary goal is to linearize the data so the data can be
   1174  1.31.2.2      phil  * passed to crypto hardware in the most efficient manner possible.
   1175  1.31.2.2      phil  */
   1176  1.31.2.2      phil struct mbuf *
   1177  1.31.2.2      phil m_unshare(struct mbuf *m0, int how)
   1178  1.31.2.2      phil {
   1179  1.31.2.2      phil 	struct mbuf *m, *mprev;
   1180  1.31.2.2      phil 	struct mbuf *n, *mfirst, *mlast;
   1181  1.31.2.2      phil 	int len, off;
   1182  1.31.2.2      phil 
   1183  1.31.2.2      phil 	mprev = NULL;
   1184  1.31.2.2      phil 	for (m = m0; m != NULL; m = mprev->m_next) {
   1185  1.31.2.2      phil 		/*
   1186  1.31.2.2      phil 		 * Regular mbufs are ignored unless there's a cluster
   1187  1.31.2.2      phil 		 * in front of it that we can use to coalesce.  We do
   1188  1.31.2.2      phil 		 * the latter mainly so later clusters can be coalesced
   1189  1.31.2.2      phil 		 * also w/o having to handle them specially (i.e. convert
   1190  1.31.2.2      phil 		 * mbuf+cluster -> cluster).  This optimization is heavily
   1191  1.31.2.2      phil 		 * influenced by the assumption that we're running over
   1192  1.31.2.2      phil 		 * Ethernet where MCLBYTES is large enough that the max
   1193  1.31.2.2      phil 		 * packet size will permit lots of coalescing into a
   1194  1.31.2.2      phil 		 * single cluster.  This in turn permits efficient
   1195  1.31.2.2      phil 		 * crypto operations, especially when using hardware.
   1196  1.31.2.2      phil 		 */
   1197  1.31.2.2      phil 		if ((m->m_flags & M_EXT) == 0) {
   1198  1.31.2.2      phil 			if (mprev && (mprev->m_flags & M_EXT) &&
   1199  1.31.2.2      phil 			    m->m_len <= M_TRAILINGSPACE(mprev)) {
   1200  1.31.2.2      phil 				/* XXX: this ignores mbuf types */
   1201  1.31.2.2      phil 				memcpy(mtod(mprev, caddr_t) + mprev->m_len,
   1202  1.31.2.2      phil 				    mtod(m, caddr_t), m->m_len);
   1203  1.31.2.2      phil 				mprev->m_len += m->m_len;
   1204  1.31.2.2      phil 				mprev->m_next = m->m_next;	/* unlink from chain */
   1205  1.31.2.2      phil 				m_free(m);			/* reclaim mbuf */
   1206  1.31.2.2      phil 			} else {
   1207  1.31.2.2      phil 				mprev = m;
   1208  1.31.2.2      phil 			}
   1209  1.31.2.2      phil 			continue;
   1210  1.31.2.2      phil 		}
   1211  1.31.2.2      phil 		/*
   1212  1.31.2.2      phil 		 * Writable mbufs are left alone (for now).
   1213  1.31.2.2      phil 		 */
   1214  1.31.2.2      phil 		if (!M_READONLY(m)) {
   1215  1.31.2.2      phil 			mprev = m;
   1216  1.31.2.2      phil 			continue;
   1217  1.31.2.2      phil 		}
   1218  1.31.2.2      phil 
   1219  1.31.2.2      phil 		/*
   1220  1.31.2.2      phil 		 * Not writable, replace with a copy or coalesce with
   1221  1.31.2.2      phil 		 * the previous mbuf if possible (since we have to copy
   1222  1.31.2.2      phil 		 * it anyway, we try to reduce the number of mbufs and
   1223  1.31.2.2      phil 		 * clusters so that future work is easier).
   1224  1.31.2.2      phil 		 */
   1225  1.31.2.2      phil 		FBSDKASSERT(m->m_flags & M_EXT, ("m_flags 0x%x", m->m_flags));
   1226  1.31.2.2      phil 		/* NB: we only coalesce into a cluster or larger */
   1227  1.31.2.2      phil 		if (mprev != NULL && (mprev->m_flags & M_EXT) &&
   1228  1.31.2.2      phil 		    m->m_len <= M_TRAILINGSPACE(mprev)) {
   1229  1.31.2.2      phil 			/* XXX: this ignores mbuf types */
   1230  1.31.2.2      phil 			memcpy(mtod(mprev, caddr_t) + mprev->m_len,
   1231  1.31.2.2      phil 			    mtod(m, caddr_t), m->m_len);
   1232  1.31.2.2      phil 			mprev->m_len += m->m_len;
   1233  1.31.2.2      phil 			mprev->m_next = m->m_next;	/* unlink from chain */
   1234  1.31.2.2      phil 			m_free(m);			/* reclaim mbuf */
   1235  1.31.2.2      phil 			continue;
   1236  1.31.2.2      phil 		}
   1237  1.31.2.2      phil 
   1238  1.31.2.2      phil 		/*
   1239  1.31.2.2      phil 		 * Allocate new space to hold the copy and copy the data.
   1240  1.31.2.2      phil 		 * We deal with jumbo mbufs (i.e. m_len > MCLBYTES) by
   1241  1.31.2.2      phil 		 * splitting them into clusters.  We could just malloc a
   1242  1.31.2.2      phil 		 * buffer and make it external but too many device drivers
   1243  1.31.2.2      phil 		 * don't know how to break up the non-contiguous memory when
   1244  1.31.2.2      phil 		 * doing DMA.
   1245  1.31.2.2      phil 		 */
   1246  1.31.2.2      phil 		n = m_getcl(how, m->m_type, m->m_flags & M_COPYFLAGS);
   1247  1.31.2.2      phil 		if (n == NULL) {
   1248  1.31.2.2      phil 			m_freem(m0);
   1249  1.31.2.2      phil 			return (NULL);
   1250  1.31.2.2      phil 		}
   1251  1.31.2.2      phil 		if (m->m_flags & M_PKTHDR) {
   1252  1.31.2.2      phil 			FBSDKASSERT(mprev == NULL, ("%s: m0 %p, m %p has M_PKTHDR",
   1253  1.31.2.2      phil 			    __func__, m0, m));
   1254  1.31.2.2      phil 			m_move_pkthdr(n, m);
   1255  1.31.2.2      phil 		}
   1256  1.31.2.2      phil 		len = m->m_len;
   1257  1.31.2.2      phil 		off = 0;
   1258  1.31.2.2      phil 		mfirst = n;
   1259  1.31.2.2      phil 		mlast = NULL;
   1260  1.31.2.2      phil 		for (;;) {
   1261  1.31.2.2      phil 			int cc = min(len, MCLBYTES);
   1262  1.31.2.2      phil 			memcpy(mtod(n, caddr_t), mtod(m, caddr_t) + off, cc);
   1263  1.31.2.2      phil 			n->m_len = cc;
   1264  1.31.2.2      phil 			if (mlast != NULL)
   1265  1.31.2.2      phil 				mlast->m_next = n;
   1266  1.31.2.2      phil 			mlast = n;
   1267  1.31.2.2      phil #if 0
   1268  1.31.2.2      phil 			newipsecstat.ips_clcopied++;
   1269  1.31.2.2      phil #endif
   1270  1.31.2.2      phil 
   1271  1.31.2.2      phil 			len -= cc;
   1272  1.31.2.2      phil 			if (len <= 0)
   1273  1.31.2.2      phil 				break;
   1274  1.31.2.2      phil 			off += cc;
   1275  1.31.2.2      phil 
   1276  1.31.2.2      phil 			n = m_getcl(how, m->m_type, m->m_flags & M_COPYFLAGS);
   1277  1.31.2.2      phil 			if (n == NULL) {
   1278  1.31.2.2      phil 				m_freem(mfirst);
   1279  1.31.2.2      phil 				m_freem(m0);
   1280  1.31.2.2      phil 				return (NULL);
   1281  1.31.2.2      phil 			}
   1282  1.31.2.2      phil 		}
   1283  1.31.2.2      phil 		n->m_next = m->m_next;
   1284  1.31.2.2      phil 		if (mprev == NULL)
   1285  1.31.2.2      phil 			m0 = mfirst;		/* new head of chain */
   1286  1.31.2.2      phil 		else
   1287  1.31.2.2      phil 			mprev->m_next = mfirst;	/* replace old mbuf */
   1288  1.31.2.2      phil 		m_free(m);			/* release old mbuf */
   1289  1.31.2.2      phil 		mprev = mfirst;
   1290  1.31.2.2      phil 	}
   1291  1.31.2.2      phil 	return (m0);
   1292  1.31.2.2      phil }
   1293