Home | History | Annotate | Line # | Download | only in net80211
ieee80211_netbsd.c revision 1.25
      1 /* $NetBSD: ieee80211_netbsd.c,v 1.25 2014/02/25 18:30:12 pooka Exp $ */
      2 /*-
      3  * Copyright (c) 2003-2005 Sam Leffler, Errno Consulting
      4  * All rights reserved.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  * 1. Redistributions of source code must retain the above copyright
     10  *    notice, this list of conditions and the following disclaimer.
     11  * 2. Redistributions in binary form must reproduce the above copyright
     12  *    notice, this list of conditions and the following disclaimer in the
     13  *    documentation and/or other materials provided with the distribution.
     14  * 3. The name of the author may not be used to endorse or promote products
     15  *    derived from this software without specific prior written permission.
     16  *
     17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     27  */
     28 
     29 #include <sys/cdefs.h>
     30 #ifdef __FreeBSD__
     31 __FBSDID("$FreeBSD: src/sys/net80211/ieee80211_freebsd.c,v 1.8 2005/08/08 18:46:35 sam Exp $");
     32 #else
     33 __KERNEL_RCSID(0, "$NetBSD: ieee80211_netbsd.c,v 1.25 2014/02/25 18:30:12 pooka Exp $");
     34 #endif
     35 
     36 /*
     37  * IEEE 802.11 support (NetBSD-specific code)
     38  */
     39 #include <sys/param.h>
     40 #include <sys/kernel.h>
     41 #include <sys/systm.h>
     42 #include <sys/mbuf.h>
     43 #include <sys/proc.h>
     44 #include <sys/sysctl.h>
     45 #include <sys/once.h>
     46 
     47 #include <sys/socket.h>
     48 
     49 #include <sys/cprng.h>
     50 
     51 #include <net/if.h>
     52 #include <net/if_media.h>
     53 #include <net/if_ether.h>
     54 #include <net/route.h>
     55 
     56 #include <net80211/ieee80211_netbsd.h>
     57 #include <net80211/ieee80211_var.h>
     58 #include <net80211/ieee80211_sysctl.h>
     59 
     60 #define	LOGICALLY_EQUAL(x, y)	(!(x) == !(y))
     61 
     62 static void ieee80211_sysctl_fill_node(struct ieee80211_node *,
     63     struct ieee80211_node_sysctl *, int, const struct ieee80211_channel *,
     64     uint32_t);
     65 static struct ieee80211_node *ieee80211_node_walknext(
     66     struct ieee80211_node_walk *);
     67 static struct ieee80211_node *ieee80211_node_walkfirst(
     68     struct ieee80211_node_walk *, u_short);
     69 static int ieee80211_sysctl_node(SYSCTLFN_ARGS);
     70 
     71 #ifdef IEEE80211_DEBUG
     72 int	ieee80211_debug = 0;
     73 #endif
     74 
     75 typedef void (*ieee80211_setup_func)(void);
     76 
     77 __link_set_decl(ieee80211_funcs, ieee80211_setup_func);
     78 
     79 static int
     80 ieee80211_init0(void)
     81 {
     82 	ieee80211_setup_func * const *ieee80211_setup, f;
     83 
     84 	if (max_linkhdr < ALIGN(sizeof(struct ieee80211_qosframe_addr4))) {
     85 		max_linkhdr = ALIGN(sizeof(struct ieee80211_qosframe_addr4));
     86 	}
     87 
     88         __link_set_foreach(ieee80211_setup, ieee80211_funcs) {
     89 		f = (void*)*ieee80211_setup;
     90 		(*f)();
     91 	}
     92 
     93 	return 0;
     94 }
     95 
     96 void
     97 ieee80211_init(void)
     98 {
     99 	static ONCE_DECL(ieee80211_init_once);
    100 
    101 	RUN_ONCE(&ieee80211_init_once, ieee80211_init0);
    102 }
    103 
    104 static int
    105 ieee80211_sysctl_inact(SYSCTLFN_ARGS)
    106 {
    107 	int error, t;
    108 	struct sysctlnode node;
    109 
    110 	node = *rnode;
    111 	/* sysctl_lookup copies the product from t.  Then, it
    112 	 * copies the new value onto t.
    113 	 */
    114 	t = *(int*)rnode->sysctl_data * IEEE80211_INACT_WAIT;
    115 	node.sysctl_data = &t;
    116 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
    117 	if (error || newp == NULL)
    118 		return (error);
    119 
    120 	/* The new value was in seconds.  Convert to inactivity-wait
    121 	 * intervals.  There are IEEE80211_INACT_WAIT seconds per
    122 	 * interval.
    123 	 */
    124 	*(int*)rnode->sysctl_data = t / IEEE80211_INACT_WAIT;
    125 
    126 	return (0);
    127 }
    128 
    129 static int
    130 ieee80211_sysctl_parent(SYSCTLFN_ARGS)
    131 {
    132 	struct ieee80211com *ic;
    133 	char pname[IFNAMSIZ];
    134 	struct sysctlnode node;
    135 
    136 	node = *rnode;
    137 	ic = node.sysctl_data;
    138 	strncpy(pname, ic->ic_ifp->if_xname, IFNAMSIZ);
    139 	node.sysctl_data = pname;
    140 	return sysctl_lookup(SYSCTLFN_CALL(&node));
    141 }
    142 
    143 /*
    144  * Create or get top of sysctl tree net.link.ieee80211.
    145  */
    146 static const struct sysctlnode *
    147 ieee80211_sysctl_treetop(struct sysctllog **log)
    148 {
    149 	int rc;
    150 	const struct sysctlnode *rnode;
    151 
    152 	if ((rc = sysctl_createv(log, 0, NULL, &rnode,
    153 	    CTLFLAG_PERMANENT, CTLTYPE_NODE, "link",
    154 	    "link-layer statistics and controls",
    155 	    NULL, 0, NULL, 0, CTL_NET, PF_LINK, CTL_EOL)) != 0)
    156 		goto err;
    157 
    158 	if ((rc = sysctl_createv(log, 0, &rnode, &rnode,
    159 	    CTLFLAG_PERMANENT, CTLTYPE_NODE, "ieee80211",
    160 	    "IEEE 802.11 WLAN statistics and controls",
    161 	    NULL, 0, NULL, 0, CTL_CREATE, CTL_EOL)) != 0)
    162 		goto err;
    163 
    164 	return rnode;
    165 err:
    166 	printf("%s: sysctl_createv failed, rc = %d\n", __func__, rc);
    167 	return NULL;
    168 }
    169 
    170 void
    171 ieee80211_sysctl_attach(struct ieee80211com *ic)
    172 {
    173 	int rc;
    174 	const struct sysctlnode *cnode, *rnode;
    175 	char num[sizeof("vap") + 14];		/* sufficient for 32 bits */
    176 
    177 	if ((rnode = ieee80211_sysctl_treetop(NULL)) == NULL)
    178 		return;
    179 
    180 	snprintf(num, sizeof(num), "vap%u", ic->ic_vap);
    181 
    182 	if ((rc = sysctl_createv(&ic->ic_sysctllog, 0, &rnode, &rnode,
    183 	    CTLFLAG_PERMANENT, CTLTYPE_NODE, num, SYSCTL_DESCR("virtual AP"),
    184 	    NULL, 0, NULL, 0, CTL_CREATE, CTL_EOL)) != 0)
    185 		goto err;
    186 
    187 	/* control debugging printfs */
    188 	if ((rc = sysctl_createv(&ic->ic_sysctllog, 0, &rnode, &cnode,
    189 	    CTLFLAG_PERMANENT|CTLFLAG_READONLY, CTLTYPE_STRING,
    190 	    "parent", SYSCTL_DESCR("parent device"),
    191 	    ieee80211_sysctl_parent, 0, (void *)ic, IFNAMSIZ, CTL_CREATE,
    192 	    CTL_EOL)) != 0)
    193 		goto err;
    194 
    195 #ifdef IEEE80211_DEBUG
    196 	/* control debugging printfs */
    197 	if ((rc = sysctl_createv(&ic->ic_sysctllog, 0, &rnode, &cnode,
    198 	    CTLFLAG_PERMANENT|CTLFLAG_READWRITE, CTLTYPE_INT,
    199 	    "debug", SYSCTL_DESCR("control debugging printfs"),
    200 	    NULL, ieee80211_debug, &ic->ic_debug, 0,
    201 	    CTL_CREATE, CTL_EOL)) != 0)
    202 		goto err;
    203 #endif
    204 	/* XXX inherit from tunables */
    205 	if ((rc = sysctl_createv(&ic->ic_sysctllog, 0, &rnode, &cnode,
    206 	    CTLFLAG_PERMANENT|CTLFLAG_READWRITE, CTLTYPE_INT,
    207 	    "inact_run", SYSCTL_DESCR("station inactivity timeout (sec)"),
    208 	    ieee80211_sysctl_inact, 0, &ic->ic_inact_run, 0,
    209 	    CTL_CREATE, CTL_EOL)) != 0)
    210 		goto err;
    211 	if ((rc = sysctl_createv(&ic->ic_sysctllog, 0, &rnode, &cnode,
    212 	    CTLFLAG_PERMANENT|CTLFLAG_READWRITE, CTLTYPE_INT,
    213 	    "inact_probe",
    214 	    SYSCTL_DESCR("station inactivity probe timeout (sec)"),
    215 	    ieee80211_sysctl_inact, 0, &ic->ic_inact_probe, 0,
    216 	    CTL_CREATE, CTL_EOL)) != 0)
    217 		goto err;
    218 	if ((rc = sysctl_createv(&ic->ic_sysctllog, 0, &rnode, &cnode,
    219 	    CTLFLAG_PERMANENT|CTLFLAG_READWRITE, CTLTYPE_INT,
    220 	    "inact_auth",
    221 	    SYSCTL_DESCR("station authentication timeout (sec)"),
    222 	    ieee80211_sysctl_inact, 0, &ic->ic_inact_auth, 0,
    223 	    CTL_CREATE, CTL_EOL)) != 0)
    224 		goto err;
    225 	if ((rc = sysctl_createv(&ic->ic_sysctllog, 0, &rnode, &cnode,
    226 	    CTLFLAG_PERMANENT|CTLFLAG_READWRITE, CTLTYPE_INT,
    227 	    "inact_init",
    228 	    SYSCTL_DESCR("station initial state timeout (sec)"),
    229 	    ieee80211_sysctl_inact, 0, &ic->ic_inact_init, 0,
    230 	    CTL_CREATE, CTL_EOL)) != 0)
    231 		goto err;
    232 	if ((rc = sysctl_createv(&ic->ic_sysctllog, 0, &rnode, &cnode,
    233 	    CTLFLAG_PERMANENT|CTLFLAG_READWRITE, CTLTYPE_INT,
    234 	    "driver_caps", SYSCTL_DESCR("driver capabilities"),
    235 	    NULL, 0, &ic->ic_caps, 0, CTL_CREATE, CTL_EOL)) != 0)
    236 		goto err;
    237 	if ((rc = sysctl_createv(&ic->ic_sysctllog, 0, &rnode, &cnode,
    238 	    CTLFLAG_PERMANENT|CTLFLAG_READWRITE, CTLTYPE_INT,
    239 	    "bmiss_max", SYSCTL_DESCR("consecutive beacon misses before scanning"),
    240 	    NULL, 0, &ic->ic_bmiss_max, 0, CTL_CREATE, CTL_EOL)) != 0)
    241 		goto err;
    242 
    243 	return;
    244 err:
    245 	printf("%s: sysctl_createv failed, rc = %d\n", __func__, rc);
    246 }
    247 
    248 void
    249 ieee80211_sysctl_detach(struct ieee80211com *ic)
    250 {
    251 	sysctl_teardown(&ic->ic_sysctllog);
    252 }
    253 
    254 /*
    255  * Pointers for testing:
    256  *
    257  *	If there are no interfaces, or else no 802.11 interfaces,
    258  *	ieee80211_node_walkfirst must return NULL.
    259  *
    260  *	If there is any single 802.11 interface, ieee80211_node_walkfirst
    261  *	must not return NULL.
    262  */
    263 static struct ieee80211_node *
    264 ieee80211_node_walkfirst(struct ieee80211_node_walk *nw, u_short if_index)
    265 {
    266 	(void)memset(nw, 0, sizeof(*nw));
    267 
    268 	nw->nw_ifindex = if_index;
    269 
    270 	LIST_FOREACH(nw->nw_ic, &ieee80211com_head, ic_list) {
    271 		if (if_index != 0 && nw->nw_ic->ic_ifp->if_index != if_index)
    272 			continue;
    273 		if (!TAILQ_EMPTY(&nw->nw_ic->ic_sta.nt_node))
    274 			nw->nw_nt = &nw->nw_ic->ic_sta;
    275 		else if (!TAILQ_EMPTY(&nw->nw_ic->ic_scan.nt_node))
    276 			nw->nw_nt = &nw->nw_ic->ic_scan;
    277 		else if (nw->nw_ic->ic_bss == NULL)
    278 			continue;
    279 		break;
    280 	}
    281 
    282 	if (nw->nw_ic == NULL)
    283 		return NULL;
    284 
    285 	if (nw->nw_nt == NULL)
    286 		nw->nw_ni = nw->nw_ic->ic_bss;
    287 	else
    288 		nw->nw_ni = TAILQ_FIRST(&nw->nw_nt->nt_node);
    289 
    290 	return nw->nw_ni;
    291 }
    292 
    293 static struct ieee80211_node *
    294 ieee80211_node_walknext(struct ieee80211_node_walk *nw)
    295 {
    296 	if (nw->nw_nt != NULL)
    297 		nw->nw_ni = TAILQ_NEXT(nw->nw_ni, ni_list);
    298 	else
    299 		nw->nw_ni = NULL;
    300 
    301 	while (nw->nw_ni == NULL) {
    302 		if (nw->nw_nt == &nw->nw_ic->ic_sta) {
    303 			nw->nw_nt = &nw->nw_ic->ic_scan;
    304 			nw->nw_ni = TAILQ_FIRST(&nw->nw_nt->nt_node);
    305 			continue;
    306 		} else if (nw->nw_nt == &nw->nw_ic->ic_scan) {
    307 			nw->nw_nt = NULL;
    308 			nw->nw_ni = nw->nw_ic->ic_bss;
    309 			continue;
    310 		}
    311 		KASSERT(nw->nw_nt == NULL);
    312 		if (nw->nw_ifindex != 0)
    313 			return NULL;
    314 
    315 		nw->nw_ic = LIST_NEXT(nw->nw_ic, ic_list);
    316 		if (nw->nw_ic == NULL)
    317 			return NULL;
    318 
    319 		nw->nw_nt = &nw->nw_ic->ic_sta;
    320 		nw->nw_ni = TAILQ_FIRST(&nw->nw_nt->nt_node);
    321 	}
    322 
    323 	return nw->nw_ni;
    324 }
    325 
    326 static void
    327 ieee80211_sysctl_fill_node(struct ieee80211_node *ni,
    328     struct ieee80211_node_sysctl *ns, int ifindex,
    329     const struct ieee80211_channel *chan0, uint32_t flags)
    330 {
    331 	ns->ns_ifindex = ifindex;
    332 	ns->ns_capinfo = ni->ni_capinfo;
    333 	ns->ns_flags = flags;
    334 	(void)memcpy(ns->ns_macaddr, ni->ni_macaddr, sizeof(ns->ns_macaddr));
    335 	(void)memcpy(ns->ns_bssid, ni->ni_bssid, sizeof(ns->ns_bssid));
    336 	if (ni->ni_chan != IEEE80211_CHAN_ANYC) {
    337 		ns->ns_freq = ni->ni_chan->ic_freq;
    338 		ns->ns_chanflags = ni->ni_chan->ic_flags;
    339 		ns->ns_chanidx = ni->ni_chan - chan0;
    340 	} else {
    341 		ns->ns_freq = ns->ns_chanflags = 0;
    342 		ns->ns_chanidx = 0;
    343 	}
    344 	ns->ns_rssi = ni->ni_rssi;
    345 	ns->ns_esslen = ni->ni_esslen;
    346 	(void)memcpy(ns->ns_essid, ni->ni_essid, sizeof(ns->ns_essid));
    347 	ns->ns_erp = ni->ni_erp;
    348 	ns->ns_associd = ni->ni_associd;
    349 	ns->ns_inact = ni->ni_inact * IEEE80211_INACT_WAIT;
    350 	ns->ns_rstamp = ni->ni_rstamp;
    351 	ns->ns_rates = ni->ni_rates;
    352 	ns->ns_txrate = ni->ni_txrate;
    353 	ns->ns_intval = ni->ni_intval;
    354 	(void)memcpy(ns->ns_tstamp, &ni->ni_tstamp, sizeof(ns->ns_tstamp));
    355 	ns->ns_txseq = ni->ni_txseqs[0];
    356 	ns->ns_rxseq = ni->ni_rxseqs[0];
    357 	ns->ns_fhdwell = ni->ni_fhdwell;
    358 	ns->ns_fhindex = ni->ni_fhindex;
    359 	ns->ns_fails = ni->ni_fails;
    360 }
    361 
    362 /* Between two examinations of the sysctl tree, I expect each
    363  * interface to add no more than 5 nodes.
    364  */
    365 #define IEEE80211_SYSCTL_NODE_GROWTH	5
    366 
    367 static int
    368 ieee80211_sysctl_node(SYSCTLFN_ARGS)
    369 {
    370 	struct ieee80211_node_walk nw;
    371 	struct ieee80211_node *ni;
    372 	struct ieee80211_node_sysctl ns;
    373 	char *dp;
    374 	u_int cur_ifindex, ifcount, ifindex, last_ifindex, op, arg, hdr_type;
    375 	uint32_t flags;
    376 	size_t len, needed, eltsize, out_size;
    377 	int error, s, saw_bss = 0, nelt;
    378 
    379 	if (namelen == 1 && name[0] == CTL_QUERY)
    380 		return (sysctl_query(SYSCTLFN_CALL(rnode)));
    381 
    382 	if (namelen != IEEE80211_SYSCTL_NODENAMELEN)
    383 		return (EINVAL);
    384 
    385 	/* ifindex.op.arg.header-type.eltsize.nelt */
    386 	dp = oldp;
    387 	len = (oldp != NULL) ? *oldlenp : 0;
    388 	ifindex = name[IEEE80211_SYSCTL_NODENAME_IF];
    389 	op = name[IEEE80211_SYSCTL_NODENAME_OP];
    390 	arg = name[IEEE80211_SYSCTL_NODENAME_ARG];
    391 	hdr_type = name[IEEE80211_SYSCTL_NODENAME_TYPE];
    392 	eltsize = name[IEEE80211_SYSCTL_NODENAME_ELTSIZE];
    393 	nelt = name[IEEE80211_SYSCTL_NODENAME_ELTCOUNT];
    394 	out_size = MIN(sizeof(ns), eltsize);
    395 
    396 	if (op != IEEE80211_SYSCTL_OP_ALL || arg != 0 ||
    397 	    hdr_type != IEEE80211_SYSCTL_T_NODE || eltsize < 1 || nelt < 0)
    398 		return (EINVAL);
    399 
    400 	error = 0;
    401 	needed = 0;
    402 	ifcount = 0;
    403 	last_ifindex = 0;
    404 
    405 	s = splnet();
    406 
    407 	for (ni = ieee80211_node_walkfirst(&nw, ifindex); ni != NULL;
    408 	     ni = ieee80211_node_walknext(&nw)) {
    409 		struct ieee80211com *ic;
    410 
    411 		ic = nw.nw_ic;
    412 		cur_ifindex = ic->ic_ifp->if_index;
    413 
    414 		if (cur_ifindex != last_ifindex) {
    415 			saw_bss = 0;
    416 			ifcount++;
    417 			last_ifindex = cur_ifindex;
    418 		}
    419 
    420 		if (nelt <= 0)
    421 			continue;
    422 
    423 		if (saw_bss && ni == ic->ic_bss)
    424 			continue;
    425 		else if (ni == ic->ic_bss) {
    426 			saw_bss = 1;
    427 			flags = IEEE80211_NODE_SYSCTL_F_BSS;
    428 		} else
    429 			flags = 0;
    430 		if (ni->ni_table == &ic->ic_scan)
    431 			flags |= IEEE80211_NODE_SYSCTL_F_SCAN;
    432 		else if (ni->ni_table == &ic->ic_sta)
    433 			flags |= IEEE80211_NODE_SYSCTL_F_STA;
    434 		if (len >= eltsize) {
    435 			ieee80211_sysctl_fill_node(ni, &ns, cur_ifindex,
    436 			    &ic->ic_channels[0], flags);
    437 			error = copyout(&ns, dp, out_size);
    438 			if (error)
    439 				goto cleanup;
    440 			dp += eltsize;
    441 			len -= eltsize;
    442 		}
    443 		needed += eltsize;
    444 		if (nelt != INT_MAX)
    445 			nelt--;
    446 	}
    447 cleanup:
    448 	splx(s);
    449 
    450 	*oldlenp = needed;
    451 	if (oldp == NULL)
    452 		*oldlenp += ifcount * IEEE80211_SYSCTL_NODE_GROWTH * eltsize;
    453 
    454 	return (error);
    455 }
    456 
    457 /*
    458  * Setup sysctl(3) MIB, net.ieee80211.*
    459  *
    460  * TBD condition CTLFLAG_PERMANENT on being a module or not
    461  */
    462 SYSCTL_SETUP(sysctl_ieee80211, "sysctl ieee80211 subtree setup")
    463 {
    464 	int rc;
    465 	const struct sysctlnode *cnode, *rnode;
    466 
    467 	if ((rnode = ieee80211_sysctl_treetop(clog)) == NULL)
    468 		return;
    469 
    470 	if ((rc = sysctl_createv(clog, 0, &rnode, NULL,
    471 	    CTLFLAG_PERMANENT, CTLTYPE_NODE, "nodes", "client/peer stations",
    472 	    ieee80211_sysctl_node, 0, NULL, 0, CTL_CREATE, CTL_EOL)) != 0)
    473 		goto err;
    474 
    475 #ifdef IEEE80211_DEBUG
    476 	/* control debugging printfs */
    477 	if ((rc = sysctl_createv(clog, 0, &rnode, &cnode,
    478 	    CTLFLAG_PERMANENT|CTLFLAG_READWRITE, CTLTYPE_INT,
    479 	    "debug", SYSCTL_DESCR("control debugging printfs"),
    480 	    NULL, 0, &ieee80211_debug, 0, CTL_CREATE, CTL_EOL)) != 0)
    481 		goto err;
    482 #endif /* IEEE80211_DEBUG */
    483 
    484 	return;
    485 err:
    486 	printf("%s: sysctl_createv failed (rc = %d)\n", __func__, rc);
    487 }
    488 
    489 int
    490 ieee80211_node_dectestref(struct ieee80211_node *ni)
    491 {
    492 	if (atomic_dec_uint_nv(&ni->ni_refcnt) == 0) {
    493 		atomic_inc_uint(&ni->ni_refcnt);
    494 		return 1;
    495 	} else
    496 		return 0;
    497 }
    498 
    499 void
    500 ieee80211_drain_ifq(struct ifqueue *ifq)
    501 {
    502 	struct ieee80211_node *ni;
    503 	struct mbuf *m;
    504 
    505 	for (;;) {
    506 		IF_DEQUEUE(ifq, m);
    507 		if (m == NULL)
    508 			break;
    509 
    510 		ni = (struct ieee80211_node *)m->m_pkthdr.rcvif;
    511 		KASSERT(ni != NULL);
    512 		ieee80211_free_node(ni);
    513 		m->m_pkthdr.rcvif = NULL;
    514 
    515 		m_freem(m);
    516 	}
    517 }
    518 
    519 
    520 void
    521 if_printf(struct ifnet *ifp, const char *fmt, ...)
    522 {
    523 	va_list ap;
    524 	va_start(ap, fmt);
    525 
    526 	printf("%s: ", ifp->if_xname);
    527 	vprintf(fmt, ap);
    528 
    529 	va_end(ap);
    530 	return;
    531 }
    532 
    533 
    534 /*
    535  * Allocate and setup a management frame of the specified
    536  * size.  We return the mbuf and a pointer to the start
    537  * of the contiguous data area that's been reserved based
    538  * on the packet length.  The data area is forced to 32-bit
    539  * alignment and the buffer length to a multiple of 4 bytes.
    540  * This is done mainly so beacon frames (that require this)
    541  * can use this interface too.
    542  */
    543 struct mbuf *
    544 ieee80211_getmgtframe(u_int8_t **frm, u_int pktlen)
    545 {
    546 	struct mbuf *m;
    547 	u_int len;
    548 
    549 	/*
    550 	 * NB: we know the mbuf routines will align the data area
    551 	 *     so we don't need to do anything special.
    552 	 */
    553 	/* XXX 4-address frame? */
    554 	len = roundup(sizeof(struct ieee80211_frame) + pktlen, 4);
    555 	IASSERT(len <= MCLBYTES, ("802.11 mgt frame too large: %u", len));
    556 	if (len <= MHLEN) {
    557 		m = m_gethdr(M_NOWAIT, MT_HEADER);
    558 		/*
    559 		 * Align the data in case additional headers are added.
    560 		 * This should only happen when a WEP header is added
    561 		 * which only happens for shared key authentication mgt
    562 		 * frames which all fit in MHLEN.
    563 		 */
    564 		if (m != NULL)
    565 			MH_ALIGN(m, len);
    566 	} else
    567 		m = m_getcl(M_NOWAIT, MT_HEADER, M_PKTHDR);
    568 	if (m != NULL) {
    569 		m->m_data += sizeof(struct ieee80211_frame);
    570 		*frm = m->m_data;
    571 		IASSERT((uintptr_t)*frm % 4 == 0, ("bad beacon boundary"));
    572 	}
    573 	return m;
    574 }
    575 
    576 void
    577 get_random_bytes(void *p, size_t n)
    578 {
    579 	cprng_fast(p, n);
    580 }
    581 
    582 void
    583 ieee80211_notify_node_join(struct ieee80211com *ic, struct ieee80211_node *ni, int newassoc)
    584 {
    585 	struct ifnet *ifp = ic->ic_ifp;
    586 	struct ieee80211_join_event iev;
    587 
    588 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE, "%snode %s join\n",
    589 	    (ni == ic->ic_bss) ? "bss " : "",
    590 	    ether_sprintf(ni->ni_macaddr));
    591 
    592 	memset(&iev, 0, sizeof(iev));
    593 	if (ni == ic->ic_bss) {
    594 		IEEE80211_ADDR_COPY(iev.iev_addr, ni->ni_bssid);
    595 		rt_ieee80211msg(ifp, newassoc ?
    596 			RTM_IEEE80211_ASSOC : RTM_IEEE80211_REASSOC,
    597 			&iev, sizeof(iev));
    598 		if_link_state_change(ifp, LINK_STATE_UP);
    599 	} else {
    600 		IEEE80211_ADDR_COPY(iev.iev_addr, ni->ni_macaddr);
    601 		rt_ieee80211msg(ifp, newassoc ?
    602 		    RTM_IEEE80211_JOIN : RTM_IEEE80211_REJOIN,
    603 		    &iev, sizeof(iev));
    604 	}
    605 }
    606 
    607 void
    608 ieee80211_notify_node_leave(struct ieee80211com *ic, struct ieee80211_node *ni)
    609 {
    610 	struct ifnet *ifp = ic->ic_ifp;
    611 	struct ieee80211_leave_event iev;
    612 
    613 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_NODE, "%snode %s leave\n",
    614 	    (ni == ic->ic_bss) ? "bss " : "",
    615 	    ether_sprintf(ni->ni_macaddr));
    616 
    617 	if (ni == ic->ic_bss) {
    618 		rt_ieee80211msg(ifp, RTM_IEEE80211_DISASSOC, NULL, 0);
    619 		if_link_state_change(ifp, LINK_STATE_DOWN);
    620 	} else {
    621 		/* fire off wireless event station leaving */
    622 		memset(&iev, 0, sizeof(iev));
    623 		IEEE80211_ADDR_COPY(iev.iev_addr, ni->ni_macaddr);
    624 		rt_ieee80211msg(ifp, RTM_IEEE80211_LEAVE, &iev, sizeof(iev));
    625 	}
    626 }
    627 
    628 void
    629 ieee80211_notify_scan_done(struct ieee80211com *ic)
    630 {
    631 	struct ifnet *ifp = ic->ic_ifp;
    632 
    633 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_SCAN,
    634 		"%s", "notify scan done\n");
    635 
    636 	/* dispatch wireless event indicating scan completed */
    637 	rt_ieee80211msg(ifp, RTM_IEEE80211_SCAN, NULL, 0);
    638 }
    639 
    640 void
    641 ieee80211_notify_replay_failure(struct ieee80211com *ic,
    642 	const struct ieee80211_frame *wh, const struct ieee80211_key *k,
    643 	u_int64_t rsc)
    644 {
    645 	struct ifnet *ifp = ic->ic_ifp;
    646 
    647 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO,
    648 	    "[%s] %s replay detected <rsc %ju, csc %ju, keyix %u rxkeyix %u>\n",
    649 	    ether_sprintf(wh->i_addr2), k->wk_cipher->ic_name,
    650 	    (intmax_t) rsc, (intmax_t) k->wk_keyrsc,
    651 	    k->wk_keyix, k->wk_rxkeyix);
    652 
    653 	if (ifp != NULL) {		/* NB: for cipher test modules */
    654 		struct ieee80211_replay_event iev;
    655 
    656 		IEEE80211_ADDR_COPY(iev.iev_dst, wh->i_addr1);
    657 		IEEE80211_ADDR_COPY(iev.iev_src, wh->i_addr2);
    658 		iev.iev_cipher = k->wk_cipher->ic_cipher;
    659 		if (k->wk_rxkeyix != IEEE80211_KEYIX_NONE)
    660 			iev.iev_keyix = k->wk_rxkeyix;
    661 		else
    662 			iev.iev_keyix = k->wk_keyix;
    663 		iev.iev_keyrsc = k->wk_keyrsc;
    664 		iev.iev_rsc = rsc;
    665 		rt_ieee80211msg(ifp, RTM_IEEE80211_REPLAY, &iev, sizeof(iev));
    666 	}
    667 }
    668 
    669 void
    670 ieee80211_notify_michael_failure(struct ieee80211com *ic,
    671 	const struct ieee80211_frame *wh, u_int keyix)
    672 {
    673 	struct ifnet *ifp = ic->ic_ifp;
    674 
    675 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_CRYPTO,
    676 		"[%s] michael MIC verification failed <keyix %u>\n",
    677 	       ether_sprintf(wh->i_addr2), keyix);
    678 	ic->ic_stats.is_rx_tkipmic++;
    679 
    680 	if (ifp != NULL) {		/* NB: for cipher test modules */
    681 		struct ieee80211_michael_event iev;
    682 
    683 		IEEE80211_ADDR_COPY(iev.iev_dst, wh->i_addr1);
    684 		IEEE80211_ADDR_COPY(iev.iev_src, wh->i_addr2);
    685 		iev.iev_cipher = IEEE80211_CIPHER_TKIP;
    686 		iev.iev_keyix = keyix;
    687 		rt_ieee80211msg(ifp, RTM_IEEE80211_MICHAEL, &iev, sizeof(iev));
    688 	}
    689 }
    690 
    691 void
    692 ieee80211_load_module(const char *modname)
    693 {
    694 #ifdef notyet
    695 	struct thread *td = curthread;
    696 
    697 	if (suser(td) == 0 && securelevel_gt(td->td_ucred, 0) == 0) {
    698 		mtx_lock(&Giant);
    699 		(void) linker_load_module(modname, NULL, NULL, NULL, NULL);
    700 		mtx_unlock(&Giant);
    701 	}
    702 #else
    703 	printf("%s: load the %s module by hand for now.\n", __func__, modname);
    704 #endif
    705 }
    706