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