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