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