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