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