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