1 /*- 2 * Copyright (c) 2009 The FreeBSD Foundation 3 * All rights reserved. 4 * 5 * This software was developed by Rui Paulo under sponsorship from the 6 * FreeBSD Foundation. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 */ 29 #include <sys/cdefs.h> 30 #ifdef __FreeBSD__ 31 __FBSDID("$FreeBSD$"); 32 #endif 33 34 /* 35 * IEEE 802.11s Mesh Point (MBSS) support. 36 * 37 * Based on March 2009, D3.0 802.11s draft spec. 38 */ 39 #include "opt_inet.h" 40 #include "opt_wlan.h" 41 42 #include <sys/param.h> 43 #include <sys/systm.h> 44 #include <sys/mbuf.h> 45 #include <sys/malloc.h> 46 #include <sys/kernel.h> 47 48 #include <sys/socket.h> 49 #include <sys/sockio.h> 50 #include <sys/endian.h> 51 #include <sys/errno.h> 52 #include <sys/proc.h> 53 #include <sys/sysctl.h> 54 55 #include <net/if.h> 56 #include <net/if_media.h> 57 #include <net/if_llc.h> 58 #include <net/ethernet.h> 59 60 #include <net80211/ieee80211_var.h> 61 #include <net80211/ieee80211_action.h> 62 #include <net80211/ieee80211_input.h> 63 #include <net80211/ieee80211_mesh.h> 64 65 static void mesh_rt_flush_invalid(struct ieee80211vap *); 66 static int mesh_select_proto_path(struct ieee80211vap *, const char *); 67 static int mesh_select_proto_metric(struct ieee80211vap *, const char *); 68 static void mesh_vattach(struct ieee80211vap *); 69 static int mesh_newstate(struct ieee80211vap *, enum ieee80211_state, int); 70 static void mesh_rt_cleanup_cb(void *); 71 static void mesh_linkchange(struct ieee80211_node *, 72 enum ieee80211_mesh_mlstate); 73 static void mesh_checkid(void *, struct ieee80211_node *); 74 static uint32_t mesh_generateid(struct ieee80211vap *); 75 static int mesh_checkpseq(struct ieee80211vap *, 76 const uint8_t [IEEE80211_ADDR_LEN], uint32_t); 77 static struct ieee80211_node * 78 mesh_find_txnode(struct ieee80211vap *, 79 const uint8_t [IEEE80211_ADDR_LEN]); 80 static void mesh_forward(struct ieee80211vap *, struct mbuf *, 81 const struct ieee80211_meshcntl *); 82 static int mesh_input(struct ieee80211_node *, struct mbuf *, int, int); 83 static void mesh_recv_mgmt(struct ieee80211_node *, struct mbuf *, int, 84 int, int); 85 static void mesh_recv_ctl(struct ieee80211_node *, struct mbuf *, int); 86 static void mesh_peer_timeout_setup(struct ieee80211_node *); 87 static void mesh_peer_timeout_backoff(struct ieee80211_node *); 88 static void mesh_peer_timeout_cb(void *); 89 static __inline void 90 mesh_peer_timeout_stop(struct ieee80211_node *); 91 static int mesh_verify_meshid(struct ieee80211vap *, const uint8_t *); 92 static int mesh_verify_meshconf(struct ieee80211vap *, const uint8_t *); 93 static int mesh_verify_meshpeer(struct ieee80211vap *, uint8_t, 94 const uint8_t *); 95 uint32_t mesh_airtime_calc(struct ieee80211_node *); 96 97 /* 98 * Timeout values come from the specification and are in milliseconds. 99 */ 100 SYSCTL_NODE(_net_wlan, OID_AUTO, mesh, CTLFLAG_RD, 0, 101 "IEEE 802.11s parameters"); 102 static int ieee80211_mesh_retrytimeout = -1; 103 SYSCTL_PROC(_net_wlan_mesh, OID_AUTO, retrytimeout, CTLTYPE_INT | CTLFLAG_RW, 104 &ieee80211_mesh_retrytimeout, 0, ieee80211_sysctl_msecs_ticks, "I", 105 "Retry timeout (msec)"); 106 static int ieee80211_mesh_holdingtimeout = -1; 107 SYSCTL_PROC(_net_wlan_mesh, OID_AUTO, holdingtimeout, CTLTYPE_INT | CTLFLAG_RW, 108 &ieee80211_mesh_holdingtimeout, 0, ieee80211_sysctl_msecs_ticks, "I", 109 "Holding state timeout (msec)"); 110 static int ieee80211_mesh_confirmtimeout = -1; 111 SYSCTL_PROC(_net_wlan_mesh, OID_AUTO, confirmtimeout, CTLTYPE_INT | CTLFLAG_RW, 112 &ieee80211_mesh_confirmtimeout, 0, ieee80211_sysctl_msecs_ticks, "I", 113 "Confirm state timeout (msec)"); 114 static int ieee80211_mesh_maxretries = 2; 115 SYSCTL_INT(_net_wlan_mesh, OID_AUTO, maxretries, CTLTYPE_INT | CTLFLAG_RW, 116 &ieee80211_mesh_maxretries, 0, 117 "Maximum retries during peer link establishment"); 118 119 static const uint8_t broadcastaddr[IEEE80211_ADDR_LEN] = 120 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; 121 122 static ieee80211_recv_action_func mesh_recv_action_meshpeering_open; 123 static ieee80211_recv_action_func mesh_recv_action_meshpeering_confirm; 124 static ieee80211_recv_action_func mesh_recv_action_meshpeering_close; 125 static ieee80211_recv_action_func mesh_recv_action_meshlmetric_req; 126 static ieee80211_recv_action_func mesh_recv_action_meshlmetric_rep; 127 128 static ieee80211_send_action_func mesh_send_action_meshpeering_open; 129 static ieee80211_send_action_func mesh_send_action_meshpeering_confirm; 130 static ieee80211_send_action_func mesh_send_action_meshpeering_close; 131 static ieee80211_send_action_func mesh_send_action_meshlink_request; 132 static ieee80211_send_action_func mesh_send_action_meshlink_reply; 133 134 static const struct ieee80211_mesh_proto_metric mesh_metric_airtime = { 135 .mpm_descr = "AIRTIME", 136 .mpm_ie = IEEE80211_MESHCONF_METRIC_AIRTIME, 137 .mpm_metric = mesh_airtime_calc, 138 }; 139 140 static struct ieee80211_mesh_proto_path mesh_proto_paths[4]; 141 static struct ieee80211_mesh_proto_metric mesh_proto_metrics[4]; 142 143 #define MESH_RT_LOCK(ms) mtx_lock(&(ms)->ms_rt_lock) 144 #define MESH_RT_LOCK_ASSERT(ms) mtx_assert(&(ms)->ms_rt_lock, MA_OWNED) 145 #define MESH_RT_UNLOCK(ms) mtx_unlock(&(ms)->ms_rt_lock) 146 147 MALLOC_DEFINE(M_80211_MESH_RT, "80211mesh", "802.11s routing table"); 148 149 /* 150 * Helper functions to manipulate the Mesh routing table. 151 */ 152 153 static struct ieee80211_mesh_route * 154 mesh_rt_find_locked(struct ieee80211_mesh_state *ms, 155 const uint8_t dest[IEEE80211_ADDR_LEN]) 156 { 157 struct ieee80211_mesh_route *rt; 158 159 MESH_RT_LOCK_ASSERT(ms); 160 161 TAILQ_FOREACH(rt, &ms->ms_routes, rt_next) { 162 if (IEEE80211_ADDR_EQ(dest, rt->rt_dest)) 163 return rt; 164 } 165 return NULL; 166 } 167 168 static struct ieee80211_mesh_route * 169 mesh_rt_add_locked(struct ieee80211_mesh_state *ms, 170 const uint8_t dest[IEEE80211_ADDR_LEN]) 171 { 172 struct ieee80211_mesh_route *rt; 173 174 KASSERT(!IEEE80211_ADDR_EQ(broadcastaddr, dest), 175 ("%s: adding broadcast to the routing table", __func__)); 176 177 MESH_RT_LOCK_ASSERT(ms); 178 179 rt = malloc(ALIGN(sizeof(struct ieee80211_mesh_route)) + 180 ms->ms_ppath->mpp_privlen, M_80211_MESH_RT, M_NOWAIT | M_ZERO); 181 if (rt != NULL) { 182 IEEE80211_ADDR_COPY(rt->rt_dest, dest); 183 rt->rt_priv = (void *)ALIGN(&rt[1]); 184 rt->rt_crtime = ticks; 185 TAILQ_INSERT_TAIL(&ms->ms_routes, rt, rt_next); 186 } 187 return rt; 188 } 189 190 struct ieee80211_mesh_route * 191 ieee80211_mesh_rt_find(struct ieee80211vap *vap, 192 const uint8_t dest[IEEE80211_ADDR_LEN]) 193 { 194 struct ieee80211_mesh_state *ms = vap->iv_mesh; 195 struct ieee80211_mesh_route *rt; 196 197 MESH_RT_LOCK(ms); 198 rt = mesh_rt_find_locked(ms, dest); 199 MESH_RT_UNLOCK(ms); 200 return rt; 201 } 202 203 struct ieee80211_mesh_route * 204 ieee80211_mesh_rt_add(struct ieee80211vap *vap, 205 const uint8_t dest[IEEE80211_ADDR_LEN]) 206 { 207 struct ieee80211_mesh_state *ms = vap->iv_mesh; 208 struct ieee80211_mesh_route *rt; 209 210 KASSERT(ieee80211_mesh_rt_find(vap, dest) == NULL, 211 ("%s: duplicate entry in the routing table", __func__)); 212 KASSERT(!IEEE80211_ADDR_EQ(vap->iv_myaddr, dest), 213 ("%s: adding self to the routing table", __func__)); 214 215 MESH_RT_LOCK(ms); 216 rt = mesh_rt_add_locked(ms, dest); 217 MESH_RT_UNLOCK(ms); 218 return rt; 219 } 220 221 /* 222 * Add a proxy route (as needed) for the specified destination. 223 */ 224 void 225 ieee80211_mesh_proxy_check(struct ieee80211vap *vap, 226 const uint8_t dest[IEEE80211_ADDR_LEN]) 227 { 228 struct ieee80211_mesh_state *ms = vap->iv_mesh; 229 struct ieee80211_mesh_route *rt; 230 231 MESH_RT_LOCK(ms); 232 rt = mesh_rt_find_locked(ms, dest); 233 if (rt == NULL) { 234 rt = mesh_rt_add_locked(ms, dest); 235 if (rt == NULL) { 236 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH, dest, 237 "%s", "unable to add proxy entry"); 238 vap->iv_stats.is_mesh_rtaddfailed++; 239 } else { 240 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH, dest, 241 "%s", "add proxy entry"); 242 IEEE80211_ADDR_COPY(rt->rt_nexthop, vap->iv_myaddr); 243 rt->rt_flags |= IEEE80211_MESHRT_FLAGS_VALID 244 | IEEE80211_MESHRT_FLAGS_PROXY; 245 } 246 /* XXX assert PROXY? */ 247 } else if ((rt->rt_flags & IEEE80211_MESHRT_FLAGS_VALID) == 0) { 248 struct ieee80211com *ic = vap->iv_ic; 249 /* 250 * Fix existing entry created by received frames from 251 * stations that have some memory of dest. We also 252 * flush any frames held on the staging queue; delivering 253 * them is too much trouble right now. 254 */ 255 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH, dest, 256 "%s", "fix proxy entry"); 257 IEEE80211_ADDR_COPY(rt->rt_nexthop, vap->iv_myaddr); 258 rt->rt_flags |= IEEE80211_MESHRT_FLAGS_VALID 259 | IEEE80211_MESHRT_FLAGS_PROXY; 260 /* XXX belongs in hwmp */ 261 ieee80211_ageq_drain_node(&ic->ic_stageq, 262 (void *)(uintptr_t) ieee80211_mac_hash(ic, dest)); 263 /* XXX stat? */ 264 } 265 MESH_RT_UNLOCK(ms); 266 } 267 268 static __inline void 269 mesh_rt_del(struct ieee80211_mesh_state *ms, struct ieee80211_mesh_route *rt) 270 { 271 TAILQ_REMOVE(&ms->ms_routes, rt, rt_next); 272 free(rt, M_80211_MESH_RT); 273 } 274 275 void 276 ieee80211_mesh_rt_del(struct ieee80211vap *vap, 277 const uint8_t dest[IEEE80211_ADDR_LEN]) 278 { 279 struct ieee80211_mesh_state *ms = vap->iv_mesh; 280 struct ieee80211_mesh_route *rt, *next; 281 282 MESH_RT_LOCK(ms); 283 TAILQ_FOREACH_SAFE(rt, &ms->ms_routes, rt_next, next) { 284 if (IEEE80211_ADDR_EQ(rt->rt_dest, dest)) { 285 mesh_rt_del(ms, rt); 286 MESH_RT_UNLOCK(ms); 287 return; 288 } 289 } 290 MESH_RT_UNLOCK(ms); 291 } 292 293 void 294 ieee80211_mesh_rt_flush(struct ieee80211vap *vap) 295 { 296 struct ieee80211_mesh_state *ms = vap->iv_mesh; 297 struct ieee80211_mesh_route *rt, *next; 298 299 if (ms == NULL) 300 return; 301 MESH_RT_LOCK(ms); 302 TAILQ_FOREACH_SAFE(rt, &ms->ms_routes, rt_next, next) 303 mesh_rt_del(ms, rt); 304 MESH_RT_UNLOCK(ms); 305 } 306 307 void 308 ieee80211_mesh_rt_flush_peer(struct ieee80211vap *vap, 309 const uint8_t peer[IEEE80211_ADDR_LEN]) 310 { 311 struct ieee80211_mesh_state *ms = vap->iv_mesh; 312 struct ieee80211_mesh_route *rt, *next; 313 314 MESH_RT_LOCK(ms); 315 TAILQ_FOREACH_SAFE(rt, &ms->ms_routes, rt_next, next) { 316 if (IEEE80211_ADDR_EQ(rt->rt_nexthop, peer)) 317 mesh_rt_del(ms, rt); 318 } 319 MESH_RT_UNLOCK(ms); 320 } 321 322 /* 323 * Flush expired routing entries, i.e. those in invalid state for 324 * some time. 325 */ 326 static void 327 mesh_rt_flush_invalid(struct ieee80211vap *vap) 328 { 329 struct ieee80211_mesh_state *ms = vap->iv_mesh; 330 struct ieee80211_mesh_route *rt, *next; 331 332 if (ms == NULL) 333 return; 334 MESH_RT_LOCK(ms); 335 TAILQ_FOREACH_SAFE(rt, &ms->ms_routes, rt_next, next) { 336 if ((rt->rt_flags & IEEE80211_MESHRT_FLAGS_VALID) == 0 && 337 ticks - rt->rt_crtime >= ms->ms_ppath->mpp_inact) 338 mesh_rt_del(ms, rt); 339 } 340 MESH_RT_UNLOCK(ms); 341 } 342 343 #define N(a) (sizeof(a) / sizeof(a[0])) 344 int 345 ieee80211_mesh_register_proto_path(const struct ieee80211_mesh_proto_path *mpp) 346 { 347 int i, firstempty = -1; 348 349 for (i = 0; i < N(mesh_proto_paths); i++) { 350 if (strncmp(mpp->mpp_descr, mesh_proto_paths[i].mpp_descr, 351 IEEE80211_MESH_PROTO_DSZ) == 0) 352 return EEXIST; 353 if (!mesh_proto_paths[i].mpp_active && firstempty == -1) 354 firstempty = i; 355 } 356 if (firstempty < 0) 357 return ENOSPC; 358 memcpy(&mesh_proto_paths[firstempty], mpp, sizeof(*mpp)); 359 mesh_proto_paths[firstempty].mpp_active = 1; 360 return 0; 361 } 362 363 int 364 ieee80211_mesh_register_proto_metric(const struct 365 ieee80211_mesh_proto_metric *mpm) 366 { 367 int i, firstempty = -1; 368 369 for (i = 0; i < N(mesh_proto_metrics); i++) { 370 if (strncmp(mpm->mpm_descr, mesh_proto_metrics[i].mpm_descr, 371 IEEE80211_MESH_PROTO_DSZ) == 0) 372 return EEXIST; 373 if (!mesh_proto_metrics[i].mpm_active && firstempty == -1) 374 firstempty = i; 375 } 376 if (firstempty < 0) 377 return ENOSPC; 378 memcpy(&mesh_proto_metrics[firstempty], mpm, sizeof(*mpm)); 379 mesh_proto_metrics[firstempty].mpm_active = 1; 380 return 0; 381 } 382 383 static int 384 mesh_select_proto_path(struct ieee80211vap *vap, const char *name) 385 { 386 struct ieee80211_mesh_state *ms = vap->iv_mesh; 387 int i; 388 389 for (i = 0; i < N(mesh_proto_paths); i++) { 390 if (strcasecmp(mesh_proto_paths[i].mpp_descr, name) == 0) { 391 ms->ms_ppath = &mesh_proto_paths[i]; 392 return 0; 393 } 394 } 395 return ENOENT; 396 } 397 398 static int 399 mesh_select_proto_metric(struct ieee80211vap *vap, const char *name) 400 { 401 struct ieee80211_mesh_state *ms = vap->iv_mesh; 402 int i; 403 404 for (i = 0; i < N(mesh_proto_metrics); i++) { 405 if (strcasecmp(mesh_proto_metrics[i].mpm_descr, name) == 0) { 406 ms->ms_pmetric = &mesh_proto_metrics[i]; 407 return 0; 408 } 409 } 410 return ENOENT; 411 } 412 #undef N 413 414 static void 415 ieee80211_mesh_init(void) 416 { 417 418 memset(mesh_proto_paths, 0, sizeof(mesh_proto_paths)); 419 memset(mesh_proto_metrics, 0, sizeof(mesh_proto_metrics)); 420 421 /* 422 * Setup mesh parameters that depends on the clock frequency. 423 */ 424 ieee80211_mesh_retrytimeout = msecs_to_ticks(40); 425 ieee80211_mesh_holdingtimeout = msecs_to_ticks(40); 426 ieee80211_mesh_confirmtimeout = msecs_to_ticks(40); 427 428 /* 429 * Register action frame handlers. 430 */ 431 ieee80211_recv_action_register(IEEE80211_ACTION_CAT_MESHPEERING, 432 IEEE80211_ACTION_MESHPEERING_OPEN, 433 mesh_recv_action_meshpeering_open); 434 ieee80211_recv_action_register(IEEE80211_ACTION_CAT_MESHPEERING, 435 IEEE80211_ACTION_MESHPEERING_CONFIRM, 436 mesh_recv_action_meshpeering_confirm); 437 ieee80211_recv_action_register(IEEE80211_ACTION_CAT_MESHPEERING, 438 IEEE80211_ACTION_MESHPEERING_CLOSE, 439 mesh_recv_action_meshpeering_close); 440 ieee80211_recv_action_register(IEEE80211_ACTION_CAT_MESHLMETRIC, 441 IEEE80211_ACTION_MESHLMETRIC_REQ, mesh_recv_action_meshlmetric_req); 442 ieee80211_recv_action_register(IEEE80211_ACTION_CAT_MESHLMETRIC, 443 IEEE80211_ACTION_MESHLMETRIC_REP, mesh_recv_action_meshlmetric_rep); 444 445 ieee80211_send_action_register(IEEE80211_ACTION_CAT_MESHPEERING, 446 IEEE80211_ACTION_MESHPEERING_OPEN, 447 mesh_send_action_meshpeering_open); 448 ieee80211_send_action_register(IEEE80211_ACTION_CAT_MESHPEERING, 449 IEEE80211_ACTION_MESHPEERING_CONFIRM, 450 mesh_send_action_meshpeering_confirm); 451 ieee80211_send_action_register(IEEE80211_ACTION_CAT_MESHPEERING, 452 IEEE80211_ACTION_MESHPEERING_CLOSE, 453 mesh_send_action_meshpeering_close); 454 ieee80211_send_action_register(IEEE80211_ACTION_CAT_MESHLMETRIC, 455 IEEE80211_ACTION_MESHLMETRIC_REQ, 456 mesh_send_action_meshlink_request); 457 ieee80211_send_action_register(IEEE80211_ACTION_CAT_MESHLMETRIC, 458 IEEE80211_ACTION_MESHLMETRIC_REP, 459 mesh_send_action_meshlink_reply); 460 461 /* 462 * Register Airtime Link Metric. 463 */ 464 ieee80211_mesh_register_proto_metric(&mesh_metric_airtime); 465 466 } 467 SYSINIT(wlan_mesh, SI_SUB_DRIVERS, SI_ORDER_FIRST, ieee80211_mesh_init, NULL); 468 469 void 470 ieee80211_mesh_attach(struct ieee80211com *ic) 471 { 472 ic->ic_vattach[IEEE80211_M_MBSS] = mesh_vattach; 473 } 474 475 void 476 ieee80211_mesh_detach(struct ieee80211com *ic) 477 { 478 } 479 480 static void 481 mesh_vdetach_peers(void *arg, struct ieee80211_node *ni) 482 { 483 struct ieee80211com *ic = ni->ni_ic; 484 uint16_t args[3]; 485 486 if (ni->ni_mlstate == IEEE80211_NODE_MESH_ESTABLISHED) { 487 args[0] = ni->ni_mlpid; 488 args[1] = ni->ni_mllid; 489 args[2] = IEEE80211_REASON_PEER_LINK_CANCELED; 490 ieee80211_send_action(ni, 491 IEEE80211_ACTION_CAT_MESHPEERING, 492 IEEE80211_ACTION_MESHPEERING_CLOSE, 493 args); 494 } 495 callout_drain(&ni->ni_mltimer); 496 /* XXX belongs in hwmp */ 497 ieee80211_ageq_drain_node(&ic->ic_stageq, 498 (void *)(uintptr_t) ieee80211_mac_hash(ic, ni->ni_macaddr)); 499 } 500 501 static void 502 mesh_vdetach(struct ieee80211vap *vap) 503 { 504 struct ieee80211_mesh_state *ms = vap->iv_mesh; 505 506 callout_drain(&ms->ms_cleantimer); 507 ieee80211_iterate_nodes(&vap->iv_ic->ic_sta, mesh_vdetach_peers, 508 NULL); 509 ieee80211_mesh_rt_flush(vap); 510 mtx_destroy(&ms->ms_rt_lock); 511 ms->ms_ppath->mpp_vdetach(vap); 512 free(vap->iv_mesh, M_80211_VAP); 513 vap->iv_mesh = NULL; 514 } 515 516 static void 517 mesh_vattach(struct ieee80211vap *vap) 518 { 519 struct ieee80211_mesh_state *ms; 520 vap->iv_newstate = mesh_newstate; 521 vap->iv_input = mesh_input; 522 vap->iv_opdetach = mesh_vdetach; 523 vap->iv_recv_mgmt = mesh_recv_mgmt; 524 vap->iv_recv_ctl = mesh_recv_ctl; 525 ms = malloc(sizeof(struct ieee80211_mesh_state), M_80211_VAP, 526 M_NOWAIT | M_ZERO); 527 if (ms == NULL) { 528 printf("%s: couldn't alloc MBSS state\n", __func__); 529 return; 530 } 531 vap->iv_mesh = ms; 532 ms->ms_seq = 0; 533 ms->ms_flags = (IEEE80211_MESHFLAGS_AP | IEEE80211_MESHFLAGS_FWD); 534 ms->ms_ttl = IEEE80211_MESH_DEFAULT_TTL; 535 TAILQ_INIT(&ms->ms_routes); 536 mtx_init(&ms->ms_rt_lock, "MBSS", "802.11s routing table", MTX_DEF); 537 callout_init(&ms->ms_cleantimer, CALLOUT_MPSAFE); 538 mesh_select_proto_metric(vap, "AIRTIME"); 539 KASSERT(ms->ms_pmetric, ("ms_pmetric == NULL")); 540 mesh_select_proto_path(vap, "HWMP"); 541 KASSERT(ms->ms_ppath, ("ms_ppath == NULL")); 542 ms->ms_ppath->mpp_vattach(vap); 543 } 544 545 /* 546 * IEEE80211_M_MBSS vap state machine handler. 547 */ 548 static int 549 mesh_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg) 550 { 551 struct ieee80211_mesh_state *ms = vap->iv_mesh; 552 struct ieee80211com *ic = vap->iv_ic; 553 struct ieee80211_node *ni; 554 enum ieee80211_state ostate; 555 556 IEEE80211_LOCK_ASSERT(ic); 557 558 ostate = vap->iv_state; 559 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE, "%s: %s -> %s (%d)\n", 560 __func__, ieee80211_state_name[ostate], 561 ieee80211_state_name[nstate], arg); 562 vap->iv_state = nstate; /* state transition */ 563 if (ostate != IEEE80211_S_SCAN) 564 ieee80211_cancel_scan(vap); /* background scan */ 565 ni = vap->iv_bss; /* NB: no reference held */ 566 if (nstate != IEEE80211_S_RUN && ostate == IEEE80211_S_RUN) 567 callout_drain(&ms->ms_cleantimer); 568 switch (nstate) { 569 case IEEE80211_S_INIT: 570 switch (ostate) { 571 case IEEE80211_S_SCAN: 572 ieee80211_cancel_scan(vap); 573 break; 574 case IEEE80211_S_CAC: 575 ieee80211_dfs_cac_stop(vap); 576 break; 577 case IEEE80211_S_RUN: 578 ieee80211_iterate_nodes(&ic->ic_sta, 579 mesh_vdetach_peers, NULL); 580 break; 581 default: 582 break; 583 } 584 if (ostate != IEEE80211_S_INIT) { 585 /* NB: optimize INIT -> INIT case */ 586 ieee80211_reset_bss(vap); 587 ieee80211_mesh_rt_flush(vap); 588 } 589 break; 590 case IEEE80211_S_SCAN: 591 switch (ostate) { 592 case IEEE80211_S_INIT: 593 if (vap->iv_des_chan != IEEE80211_CHAN_ANYC && 594 !IEEE80211_IS_CHAN_RADAR(vap->iv_des_chan) && 595 ms->ms_idlen != 0) { 596 /* 597 * Already have a channel and a mesh ID; bypass 598 * the scan and startup immediately. 599 */ 600 ieee80211_create_ibss(vap, vap->iv_des_chan); 601 break; 602 } 603 /* 604 * Initiate a scan. We can come here as a result 605 * of an IEEE80211_IOC_SCAN_REQ too in which case 606 * the vap will be marked with IEEE80211_FEXT_SCANREQ 607 * and the scan request parameters will be present 608 * in iv_scanreq. Otherwise we do the default. 609 */ 610 if (vap->iv_flags_ext & IEEE80211_FEXT_SCANREQ) { 611 ieee80211_check_scan(vap, 612 vap->iv_scanreq_flags, 613 vap->iv_scanreq_duration, 614 vap->iv_scanreq_mindwell, 615 vap->iv_scanreq_maxdwell, 616 vap->iv_scanreq_nssid, vap->iv_scanreq_ssid); 617 vap->iv_flags_ext &= ~IEEE80211_FEXT_SCANREQ; 618 } else 619 ieee80211_check_scan_current(vap); 620 break; 621 default: 622 break; 623 } 624 break; 625 case IEEE80211_S_CAC: 626 /* 627 * Start CAC on a DFS channel. We come here when starting 628 * a bss on a DFS channel (see ieee80211_create_ibss). 629 */ 630 ieee80211_dfs_cac_start(vap); 631 break; 632 case IEEE80211_S_RUN: 633 switch (ostate) { 634 case IEEE80211_S_INIT: 635 /* 636 * Already have a channel; bypass the 637 * scan and startup immediately. 638 * Note that ieee80211_create_ibss will call 639 * back to do a RUN->RUN state change. 640 */ 641 ieee80211_create_ibss(vap, 642 ieee80211_ht_adjust_channel(ic, 643 ic->ic_curchan, vap->iv_flags_ht)); 644 /* NB: iv_bss is changed on return */ 645 break; 646 case IEEE80211_S_CAC: 647 /* 648 * NB: This is the normal state change when CAC 649 * expires and no radar was detected; no need to 650 * clear the CAC timer as it's already expired. 651 */ 652 /* fall thru... */ 653 case IEEE80211_S_CSA: 654 #if 0 655 /* 656 * Shorten inactivity timer of associated stations 657 * to weed out sta's that don't follow a CSA. 658 */ 659 ieee80211_iterate_nodes(&ic->ic_sta, sta_csa, vap); 660 #endif 661 /* 662 * Update bss node channel to reflect where 663 * we landed after CSA. 664 */ 665 ieee80211_node_set_chan(vap->iv_bss, 666 ieee80211_ht_adjust_channel(ic, ic->ic_curchan, 667 ieee80211_htchanflags(vap->iv_bss->ni_chan))); 668 /* XXX bypass debug msgs */ 669 break; 670 case IEEE80211_S_SCAN: 671 case IEEE80211_S_RUN: 672 #ifdef IEEE80211_DEBUG 673 if (ieee80211_msg_debug(vap)) { 674 struct ieee80211_node *ni = vap->iv_bss; 675 ieee80211_note(vap, 676 "synchronized with %s meshid ", 677 ether_sprintf(ni->ni_meshid)); 678 ieee80211_print_essid(ni->ni_meshid, 679 ni->ni_meshidlen); 680 /* XXX MCS/HT */ 681 printf(" channel %d\n", 682 ieee80211_chan2ieee(ic, ic->ic_curchan)); 683 } 684 #endif 685 break; 686 default: 687 break; 688 } 689 ieee80211_node_authorize(vap->iv_bss); 690 callout_reset(&ms->ms_cleantimer, ms->ms_ppath->mpp_inact, 691 mesh_rt_cleanup_cb, vap); 692 break; 693 default: 694 break; 695 } 696 /* NB: ostate not nstate */ 697 ms->ms_ppath->mpp_newstate(vap, ostate, arg); 698 return 0; 699 } 700 701 static void 702 mesh_rt_cleanup_cb(void *arg) 703 { 704 struct ieee80211vap *vap = arg; 705 struct ieee80211_mesh_state *ms = vap->iv_mesh; 706 707 mesh_rt_flush_invalid(vap); 708 callout_reset(&ms->ms_cleantimer, ms->ms_ppath->mpp_inact, 709 mesh_rt_cleanup_cb, vap); 710 } 711 712 713 /* 714 * Helper function to note the Mesh Peer Link FSM change. 715 */ 716 static void 717 mesh_linkchange(struct ieee80211_node *ni, enum ieee80211_mesh_mlstate state) 718 { 719 struct ieee80211vap *vap = ni->ni_vap; 720 struct ieee80211_mesh_state *ms = vap->iv_mesh; 721 #ifdef IEEE80211_DEBUG 722 static const char *meshlinkstates[] = { 723 [IEEE80211_NODE_MESH_IDLE] = "IDLE", 724 [IEEE80211_NODE_MESH_OPENSNT] = "OPEN SENT", 725 [IEEE80211_NODE_MESH_OPENRCV] = "OPEN RECEIVED", 726 [IEEE80211_NODE_MESH_CONFIRMRCV] = "CONFIRM RECEIVED", 727 [IEEE80211_NODE_MESH_ESTABLISHED] = "ESTABLISHED", 728 [IEEE80211_NODE_MESH_HOLDING] = "HOLDING" 729 }; 730 #endif 731 IEEE80211_NOTE(vap, IEEE80211_MSG_MESH, 732 ni, "peer link: %s -> %s", 733 meshlinkstates[ni->ni_mlstate], meshlinkstates[state]); 734 735 /* track neighbor count */ 736 if (state == IEEE80211_NODE_MESH_ESTABLISHED && 737 ni->ni_mlstate != IEEE80211_NODE_MESH_ESTABLISHED) { 738 KASSERT(ms->ms_neighbors < 65535, ("neighbor count overflow")); 739 ms->ms_neighbors++; 740 ieee80211_beacon_notify(vap, IEEE80211_BEACON_MESHCONF); 741 } else if (ni->ni_mlstate == IEEE80211_NODE_MESH_ESTABLISHED && 742 state != IEEE80211_NODE_MESH_ESTABLISHED) { 743 KASSERT(ms->ms_neighbors > 0, ("neighbor count 0")); 744 ms->ms_neighbors--; 745 ieee80211_beacon_notify(vap, IEEE80211_BEACON_MESHCONF); 746 } 747 ni->ni_mlstate = state; 748 switch (state) { 749 case IEEE80211_NODE_MESH_HOLDING: 750 ms->ms_ppath->mpp_peerdown(ni); 751 break; 752 case IEEE80211_NODE_MESH_ESTABLISHED: 753 ieee80211_mesh_discover(vap, ni->ni_macaddr, NULL); 754 break; 755 default: 756 break; 757 } 758 } 759 760 /* 761 * Helper function to generate a unique local ID required for mesh 762 * peer establishment. 763 */ 764 static void 765 mesh_checkid(void *arg, struct ieee80211_node *ni) 766 { 767 uint16_t *r = arg; 768 769 if (*r == ni->ni_mllid) 770 *(uint16_t *)arg = 0; 771 } 772 773 static uint32_t 774 mesh_generateid(struct ieee80211vap *vap) 775 { 776 int maxiter = 4; 777 uint16_t r; 778 779 do { 780 get_random_bytes(&r, 2); 781 ieee80211_iterate_nodes(&vap->iv_ic->ic_sta, mesh_checkid, &r); 782 maxiter--; 783 } while (r == 0 && maxiter > 0); 784 return r; 785 } 786 787 /* 788 * Verifies if we already received this packet by checking its 789 * sequence number. 790 * Returns 0 if the frame is to be accepted, 1 otherwise. 791 */ 792 static int 793 mesh_checkpseq(struct ieee80211vap *vap, 794 const uint8_t source[IEEE80211_ADDR_LEN], uint32_t seq) 795 { 796 struct ieee80211_mesh_route *rt; 797 798 rt = ieee80211_mesh_rt_find(vap, source); 799 if (rt == NULL) { 800 rt = ieee80211_mesh_rt_add(vap, source); 801 if (rt == NULL) { 802 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH, source, 803 "%s", "add mcast route failed"); 804 vap->iv_stats.is_mesh_rtaddfailed++; 805 return 1; 806 } 807 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH, source, 808 "add mcast route, mesh seqno %d", seq); 809 rt->rt_lastmseq = seq; 810 return 0; 811 } 812 if (IEEE80211_MESH_SEQ_GEQ(rt->rt_lastmseq, seq)) { 813 return 1; 814 } else { 815 rt->rt_lastmseq = seq; 816 return 0; 817 } 818 } 819 820 /* 821 * Iterate the routing table and locate the next hop. 822 */ 823 static struct ieee80211_node * 824 mesh_find_txnode(struct ieee80211vap *vap, 825 const uint8_t dest[IEEE80211_ADDR_LEN]) 826 { 827 struct ieee80211_mesh_route *rt; 828 829 rt = ieee80211_mesh_rt_find(vap, dest); 830 if (rt == NULL) 831 return NULL; 832 if ((rt->rt_flags & IEEE80211_MESHRT_FLAGS_VALID) == 0 || 833 (rt->rt_flags & IEEE80211_MESHRT_FLAGS_PROXY)) { 834 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH, dest, 835 "%s: !valid or proxy, flags 0x%x", __func__, rt->rt_flags); 836 /* XXX stat */ 837 return NULL; 838 } 839 return ieee80211_find_txnode(vap, rt->rt_nexthop); 840 } 841 842 /* 843 * Forward the specified frame. 844 * Decrement the TTL and set TA to our MAC address. 845 */ 846 static void 847 mesh_forward(struct ieee80211vap *vap, struct mbuf *m, 848 const struct ieee80211_meshcntl *mc) 849 { 850 struct ieee80211com *ic = vap->iv_ic; 851 struct ieee80211_mesh_state *ms = vap->iv_mesh; 852 struct ifnet *ifp = vap->iv_ifp; 853 struct ifnet *parent = ic->ic_ifp; 854 const struct ieee80211_frame *wh = 855 mtod(m, const struct ieee80211_frame *); 856 struct mbuf *mcopy; 857 struct ieee80211_meshcntl *mccopy; 858 struct ieee80211_frame *whcopy; 859 struct ieee80211_node *ni; 860 int err; 861 862 if (mc->mc_ttl == 0) { 863 IEEE80211_NOTE_FRAME(vap, IEEE80211_MSG_MESH, wh, 864 "%s", "frame not fwd'd, ttl 0"); 865 vap->iv_stats.is_mesh_fwd_ttl++; 866 return; 867 } 868 if (!(ms->ms_flags & IEEE80211_MESHFLAGS_FWD)) { 869 IEEE80211_NOTE_FRAME(vap, IEEE80211_MSG_MESH, wh, 870 "%s", "frame not fwd'd, fwding disabled"); 871 vap->iv_stats.is_mesh_fwd_disabled++; 872 return; 873 } 874 mcopy = m_dup(m, M_DONTWAIT); 875 if (mcopy == NULL) { 876 IEEE80211_NOTE_FRAME(vap, IEEE80211_MSG_MESH, wh, 877 "%s", "frame not fwd'd, cannot dup"); 878 vap->iv_stats.is_mesh_fwd_nobuf++; 879 ifp->if_oerrors++; 880 return; 881 } 882 mcopy = m_pullup(mcopy, ieee80211_hdrspace(ic, wh) + 883 sizeof(struct ieee80211_meshcntl)); 884 if (mcopy == NULL) { 885 IEEE80211_NOTE_FRAME(vap, IEEE80211_MSG_MESH, wh, 886 "%s", "frame not fwd'd, too short"); 887 vap->iv_stats.is_mesh_fwd_tooshort++; 888 ifp->if_oerrors++; 889 m_freem(mcopy); 890 return; 891 } 892 whcopy = mtod(mcopy, struct ieee80211_frame *); 893 mccopy = (struct ieee80211_meshcntl *) 894 (mtod(mcopy, uint8_t *) + ieee80211_hdrspace(ic, wh)); 895 /* XXX clear other bits? */ 896 whcopy->i_fc[1] &= ~IEEE80211_FC1_RETRY; 897 IEEE80211_ADDR_COPY(whcopy->i_addr2, vap->iv_myaddr); 898 if (IEEE80211_IS_MULTICAST(wh->i_addr1)) { 899 ni = ieee80211_ref_node(vap->iv_bss); 900 mcopy->m_flags |= M_MCAST; 901 } else { 902 ni = mesh_find_txnode(vap, whcopy->i_addr3); 903 if (ni == NULL) { 904 IEEE80211_NOTE_FRAME(vap, IEEE80211_MSG_MESH, wh, 905 "%s", "frame not fwd'd, no path"); 906 vap->iv_stats.is_mesh_fwd_nopath++; 907 m_freem(mcopy); 908 return; 909 } 910 IEEE80211_ADDR_COPY(whcopy->i_addr1, ni->ni_macaddr); 911 } 912 KASSERT(mccopy->mc_ttl > 0, ("%s called with wrong ttl", __func__)); 913 mccopy->mc_ttl--; 914 915 /* XXX calculate priority so drivers can find the tx queue */ 916 M_WME_SETAC(mcopy, WME_AC_BE); 917 918 /* XXX do we know m_nextpkt is NULL? */ 919 mcopy->m_pkthdr.rcvif = (void *) ni; 920 err = parent->if_transmit(parent, mcopy); 921 if (err != 0) { 922 /* NB: IFQ_HANDOFF reclaims mbuf */ 923 ieee80211_free_node(ni); 924 } else { 925 ifp->if_opackets++; 926 } 927 } 928 929 static struct mbuf * 930 mesh_decap(struct ieee80211vap *vap, struct mbuf *m, int hdrlen, int meshdrlen) 931 { 932 #define WHDIR(wh) ((wh)->i_fc[1] & IEEE80211_FC1_DIR_MASK) 933 uint8_t b[sizeof(struct ieee80211_qosframe_addr4) + 934 sizeof(struct ieee80211_meshcntl_ae11)]; 935 const struct ieee80211_qosframe_addr4 *wh; 936 const struct ieee80211_meshcntl_ae10 *mc; 937 struct ether_header *eh; 938 struct llc *llc; 939 int ae; 940 941 if (m->m_len < hdrlen + sizeof(*llc) && 942 (m = m_pullup(m, hdrlen + sizeof(*llc))) == NULL) { 943 IEEE80211_DPRINTF(vap, IEEE80211_MSG_ANY, 944 "discard data frame: %s", "m_pullup failed"); 945 vap->iv_stats.is_rx_tooshort++; 946 return NULL; 947 } 948 memcpy(b, mtod(m, caddr_t), hdrlen); 949 wh = (const struct ieee80211_qosframe_addr4 *)&b[0]; 950 mc = (const struct ieee80211_meshcntl_ae10 *)&b[hdrlen - meshdrlen]; 951 KASSERT(WHDIR(wh) == IEEE80211_FC1_DIR_FROMDS || 952 WHDIR(wh) == IEEE80211_FC1_DIR_DSTODS, 953 ("bogus dir, fc 0x%x:0x%x", wh->i_fc[0], wh->i_fc[1])); 954 955 llc = (struct llc *)(mtod(m, caddr_t) + hdrlen); 956 if (llc->llc_dsap == LLC_SNAP_LSAP && llc->llc_ssap == LLC_SNAP_LSAP && 957 llc->llc_control == LLC_UI && llc->llc_snap.org_code[0] == 0 && 958 llc->llc_snap.org_code[1] == 0 && llc->llc_snap.org_code[2] == 0 && 959 /* NB: preserve AppleTalk frames that have a native SNAP hdr */ 960 !(llc->llc_snap.ether_type == htons(ETHERTYPE_AARP) || 961 llc->llc_snap.ether_type == htons(ETHERTYPE_IPX))) { 962 m_adj(m, hdrlen + sizeof(struct llc) - sizeof(*eh)); 963 llc = NULL; 964 } else { 965 m_adj(m, hdrlen - sizeof(*eh)); 966 } 967 eh = mtod(m, struct ether_header *); 968 ae = mc->mc_flags & 3; 969 if (WHDIR(wh) == IEEE80211_FC1_DIR_FROMDS) { 970 IEEE80211_ADDR_COPY(eh->ether_dhost, wh->i_addr1); 971 if (ae == 0) { 972 IEEE80211_ADDR_COPY(eh->ether_shost, wh->i_addr3); 973 } else if (ae == 1) { 974 IEEE80211_ADDR_COPY(eh->ether_shost, mc->mc_addr4); 975 } else { 976 IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY, 977 (const struct ieee80211_frame *)wh, NULL, 978 "bad AE %d", ae); 979 vap->iv_stats.is_mesh_badae++; 980 m_freem(m); 981 return NULL; 982 } 983 } else { 984 if (ae == 0) { 985 IEEE80211_ADDR_COPY(eh->ether_dhost, wh->i_addr3); 986 IEEE80211_ADDR_COPY(eh->ether_shost, wh->i_addr4); 987 } else if (ae == 2) { 988 IEEE80211_ADDR_COPY(eh->ether_dhost, mc->mc_addr4); 989 IEEE80211_ADDR_COPY(eh->ether_shost, mc->mc_addr5); 990 } else { 991 IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY, 992 (const struct ieee80211_frame *)wh, NULL, 993 "bad AE %d", ae); 994 vap->iv_stats.is_mesh_badae++; 995 m_freem(m); 996 return NULL; 997 } 998 } 999 #ifdef ALIGNED_POINTER 1000 if (!ALIGNED_POINTER(mtod(m, caddr_t) + sizeof(*eh), uint32_t)) { 1001 m = ieee80211_realign(vap, m, sizeof(*eh)); 1002 if (m == NULL) 1003 return NULL; 1004 } 1005 #endif /* ALIGNED_POINTER */ 1006 if (llc != NULL) { 1007 eh = mtod(m, struct ether_header *); 1008 eh->ether_type = htons(m->m_pkthdr.len - sizeof(*eh)); 1009 } 1010 return m; 1011 #undef WDIR 1012 } 1013 1014 /* 1015 * Return non-zero if the unicast mesh data frame should be processed 1016 * locally. Frames that are not proxy'd have our address, otherwise 1017 * we need to consult the routing table to look for a proxy entry. 1018 */ 1019 static __inline int 1020 mesh_isucastforme(struct ieee80211vap *vap, const struct ieee80211_frame *wh, 1021 const struct ieee80211_meshcntl *mc) 1022 { 1023 int ae = mc->mc_flags & 3; 1024 1025 KASSERT((wh->i_fc[1] & IEEE80211_FC1_DIR_MASK) == IEEE80211_FC1_DIR_DSTODS, 1026 ("bad dir 0x%x:0x%x", wh->i_fc[0], wh->i_fc[1])); 1027 KASSERT(ae == 0 || ae == 2, ("bad AE %d", ae)); 1028 if (ae == 2) { /* ucast w/ proxy */ 1029 const struct ieee80211_meshcntl_ae10 *mc10 = 1030 (const struct ieee80211_meshcntl_ae10 *) mc; 1031 struct ieee80211_mesh_route *rt = 1032 ieee80211_mesh_rt_find(vap, mc10->mc_addr4); 1033 /* check for proxy route to ourself */ 1034 return (rt != NULL && 1035 (rt->rt_flags & IEEE80211_MESHRT_FLAGS_PROXY)); 1036 } else /* ucast w/o proxy */ 1037 return IEEE80211_ADDR_EQ(wh->i_addr3, vap->iv_myaddr); 1038 } 1039 1040 static int 1041 mesh_input(struct ieee80211_node *ni, struct mbuf *m, int rssi, int nf) 1042 { 1043 #define SEQ_LEQ(a,b) ((int)((a)-(b)) <= 0) 1044 #define HAS_SEQ(type) ((type & 0x4) == 0) 1045 struct ieee80211vap *vap = ni->ni_vap; 1046 struct ieee80211com *ic = ni->ni_ic; 1047 struct ifnet *ifp = vap->iv_ifp; 1048 struct ieee80211_frame *wh; 1049 const struct ieee80211_meshcntl *mc; 1050 int hdrspace, meshdrlen, need_tap; 1051 uint8_t dir, type, subtype, qos; 1052 uint32_t seq; 1053 uint8_t *addr; 1054 ieee80211_seq rxseq; 1055 1056 KASSERT(ni != NULL, ("null node")); 1057 ni->ni_inact = ni->ni_inact_reload; 1058 1059 need_tap = 1; /* mbuf need to be tapped. */ 1060 type = -1; /* undefined */ 1061 1062 if (m->m_pkthdr.len < sizeof(struct ieee80211_frame_min)) { 1063 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY, 1064 ni->ni_macaddr, NULL, 1065 "too short (1): len %u", m->m_pkthdr.len); 1066 vap->iv_stats.is_rx_tooshort++; 1067 goto out; 1068 } 1069 /* 1070 * Bit of a cheat here, we use a pointer for a 3-address 1071 * frame format but don't reference fields past outside 1072 * ieee80211_frame_min w/o first validating the data is 1073 * present. 1074 */ 1075 wh = mtod(m, struct ieee80211_frame *); 1076 1077 if ((wh->i_fc[0] & IEEE80211_FC0_VERSION_MASK) != 1078 IEEE80211_FC0_VERSION_0) { 1079 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY, 1080 ni->ni_macaddr, NULL, "wrong version %x", wh->i_fc[0]); 1081 vap->iv_stats.is_rx_badversion++; 1082 goto err; 1083 } 1084 dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK; 1085 type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK; 1086 subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK; 1087 if ((ic->ic_flags & IEEE80211_F_SCAN) == 0) { 1088 IEEE80211_RSSI_LPF(ni->ni_avgrssi, rssi); 1089 ni->ni_noise = nf; 1090 if (HAS_SEQ(type)) { 1091 uint8_t tid = ieee80211_gettid(wh); 1092 1093 if (IEEE80211_QOS_HAS_SEQ(wh) && 1094 TID_TO_WME_AC(tid) >= WME_AC_VI) 1095 ic->ic_wme.wme_hipri_traffic++; 1096 rxseq = le16toh(*(uint16_t *)wh->i_seq); 1097 if ((ni->ni_flags & IEEE80211_NODE_HT) == 0 && 1098 (wh->i_fc[1] & IEEE80211_FC1_RETRY) && 1099 SEQ_LEQ(rxseq, ni->ni_rxseqs[tid])) { 1100 /* duplicate, discard */ 1101 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT, 1102 wh->i_addr1, "duplicate", 1103 "seqno <%u,%u> fragno <%u,%u> tid %u", 1104 rxseq >> IEEE80211_SEQ_SEQ_SHIFT, 1105 ni->ni_rxseqs[tid] >> 1106 IEEE80211_SEQ_SEQ_SHIFT, 1107 rxseq & IEEE80211_SEQ_FRAG_MASK, 1108 ni->ni_rxseqs[tid] & 1109 IEEE80211_SEQ_FRAG_MASK, 1110 tid); 1111 vap->iv_stats.is_rx_dup++; 1112 IEEE80211_NODE_STAT(ni, rx_dup); 1113 goto out; 1114 } 1115 ni->ni_rxseqs[tid] = rxseq; 1116 } 1117 } 1118 #ifdef IEEE80211_DEBUG 1119 /* 1120 * It's easier, but too expensive, to simulate different mesh 1121 * topologies by consulting the ACL policy very early, so do this 1122 * only under DEBUG. 1123 * 1124 * NB: this check is also done upon peering link initiation. 1125 */ 1126 if (vap->iv_acl != NULL && !vap->iv_acl->iac_check(vap, wh->i_addr2)) { 1127 IEEE80211_DISCARD(vap, IEEE80211_MSG_ACL, 1128 wh, NULL, "%s", "disallowed by ACL"); 1129 vap->iv_stats.is_rx_acl++; 1130 goto out; 1131 } 1132 #endif 1133 switch (type) { 1134 case IEEE80211_FC0_TYPE_DATA: 1135 if (ni == vap->iv_bss) 1136 goto out; 1137 if (ni->ni_mlstate != IEEE80211_NODE_MESH_ESTABLISHED) { 1138 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_MESH, 1139 ni->ni_macaddr, NULL, 1140 "peer link not yet established (%d)", 1141 ni->ni_mlstate); 1142 vap->iv_stats.is_mesh_nolink++; 1143 goto out; 1144 } 1145 if (dir != IEEE80211_FC1_DIR_FROMDS && 1146 dir != IEEE80211_FC1_DIR_DSTODS) { 1147 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT, 1148 wh, "data", "incorrect dir 0x%x", dir); 1149 vap->iv_stats.is_rx_wrongdir++; 1150 goto err; 1151 } 1152 /* pull up enough to get to the mesh control */ 1153 hdrspace = ieee80211_hdrspace(ic, wh); 1154 if (m->m_len < hdrspace + sizeof(struct ieee80211_meshcntl) && 1155 (m = m_pullup(m, hdrspace + 1156 sizeof(struct ieee80211_meshcntl))) == NULL) { 1157 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY, 1158 ni->ni_macaddr, NULL, 1159 "data too short: expecting %u", hdrspace); 1160 vap->iv_stats.is_rx_tooshort++; 1161 goto out; /* XXX */ 1162 } 1163 /* 1164 * Now calculate the full extent of the headers. Note 1165 * mesh_decap will pull up anything we didn't get 1166 * above when it strips the 802.11 headers. 1167 */ 1168 mc = (const struct ieee80211_meshcntl *) 1169 (mtod(m, const uint8_t *) + hdrspace); 1170 meshdrlen = sizeof(struct ieee80211_meshcntl) + 1171 (mc->mc_flags & 3) * IEEE80211_ADDR_LEN; 1172 hdrspace += meshdrlen; 1173 seq = LE_READ_4(mc->mc_seq); 1174 if (IEEE80211_IS_MULTICAST(wh->i_addr1)) 1175 addr = wh->i_addr3; 1176 else 1177 addr = ((struct ieee80211_qosframe_addr4 *)wh)->i_addr4; 1178 if (IEEE80211_ADDR_EQ(vap->iv_myaddr, addr)) { 1179 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT, 1180 addr, "data", "%s", "not to me"); 1181 vap->iv_stats.is_rx_wrongbss++; /* XXX kinda */ 1182 goto out; 1183 } 1184 if (mesh_checkpseq(vap, addr, seq) != 0) { 1185 vap->iv_stats.is_rx_dup++; 1186 goto out; 1187 } 1188 1189 /* 1190 * Potentially forward packet. See table s36 (p140) 1191 * for the rules. XXX tap fwd'd packets not for us? 1192 */ 1193 if (dir == IEEE80211_FC1_DIR_FROMDS || 1194 !mesh_isucastforme(vap, wh, mc)) { 1195 mesh_forward(vap, m, mc); 1196 if (dir == IEEE80211_FC1_DIR_DSTODS) 1197 goto out; 1198 /* NB: fall thru to deliver mcast frames locally */ 1199 } 1200 1201 /* 1202 * Save QoS bits for use below--before we strip the header. 1203 */ 1204 if (subtype == IEEE80211_FC0_SUBTYPE_QOS) { 1205 qos = (dir == IEEE80211_FC1_DIR_DSTODS) ? 1206 ((struct ieee80211_qosframe_addr4 *)wh)->i_qos[0] : 1207 ((struct ieee80211_qosframe *)wh)->i_qos[0]; 1208 } else 1209 qos = 0; 1210 /* 1211 * Next up, any fragmentation. 1212 */ 1213 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) { 1214 m = ieee80211_defrag(ni, m, hdrspace); 1215 if (m == NULL) { 1216 /* Fragment dropped or frame not complete yet */ 1217 goto out; 1218 } 1219 } 1220 wh = NULL; /* no longer valid, catch any uses */ 1221 1222 if (ieee80211_radiotap_active_vap(vap)) 1223 ieee80211_radiotap_rx(vap, m); 1224 need_tap = 0; 1225 1226 /* 1227 * Finally, strip the 802.11 header. 1228 */ 1229 m = mesh_decap(vap, m, hdrspace, meshdrlen); 1230 if (m == NULL) { 1231 /* XXX mask bit to check for both */ 1232 /* don't count Null data frames as errors */ 1233 if (subtype == IEEE80211_FC0_SUBTYPE_NODATA || 1234 subtype == IEEE80211_FC0_SUBTYPE_QOS_NULL) 1235 goto out; 1236 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT, 1237 ni->ni_macaddr, "data", "%s", "decap error"); 1238 vap->iv_stats.is_rx_decap++; 1239 IEEE80211_NODE_STAT(ni, rx_decap); 1240 goto err; 1241 } 1242 if (qos & IEEE80211_QOS_AMSDU) { 1243 m = ieee80211_decap_amsdu(ni, m); 1244 if (m == NULL) 1245 return IEEE80211_FC0_TYPE_DATA; 1246 } 1247 ieee80211_deliver_data(vap, ni, m); 1248 return type; 1249 case IEEE80211_FC0_TYPE_MGT: 1250 vap->iv_stats.is_rx_mgmt++; 1251 IEEE80211_NODE_STAT(ni, rx_mgmt); 1252 if (dir != IEEE80211_FC1_DIR_NODS) { 1253 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT, 1254 wh, "mgt", "incorrect dir 0x%x", dir); 1255 vap->iv_stats.is_rx_wrongdir++; 1256 goto err; 1257 } 1258 if (m->m_pkthdr.len < sizeof(struct ieee80211_frame)) { 1259 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY, 1260 ni->ni_macaddr, "mgt", "too short: len %u", 1261 m->m_pkthdr.len); 1262 vap->iv_stats.is_rx_tooshort++; 1263 goto out; 1264 } 1265 #ifdef IEEE80211_DEBUG 1266 if ((ieee80211_msg_debug(vap) && 1267 (vap->iv_ic->ic_flags & IEEE80211_F_SCAN)) || 1268 ieee80211_msg_dumppkts(vap)) { 1269 if_printf(ifp, "received %s from %s rssi %d\n", 1270 ieee80211_mgt_subtype_name[subtype >> 1271 IEEE80211_FC0_SUBTYPE_SHIFT], 1272 ether_sprintf(wh->i_addr2), rssi); 1273 } 1274 #endif 1275 if (wh->i_fc[1] & IEEE80211_FC1_WEP) { 1276 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT, 1277 wh, NULL, "%s", "WEP set but not permitted"); 1278 vap->iv_stats.is_rx_mgtdiscard++; /* XXX */ 1279 goto out; 1280 } 1281 vap->iv_recv_mgmt(ni, m, subtype, rssi, nf); 1282 goto out; 1283 case IEEE80211_FC0_TYPE_CTL: 1284 vap->iv_stats.is_rx_ctl++; 1285 IEEE80211_NODE_STAT(ni, rx_ctrl); 1286 goto out; 1287 default: 1288 IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY, 1289 wh, "bad", "frame type 0x%x", type); 1290 /* should not come here */ 1291 break; 1292 } 1293 err: 1294 ifp->if_ierrors++; 1295 out: 1296 if (m != NULL) { 1297 if (need_tap && ieee80211_radiotap_active_vap(vap)) 1298 ieee80211_radiotap_rx(vap, m); 1299 m_freem(m); 1300 } 1301 return type; 1302 } 1303 1304 static void 1305 mesh_recv_mgmt(struct ieee80211_node *ni, struct mbuf *m0, int subtype, 1306 int rssi, int nf) 1307 { 1308 struct ieee80211vap *vap = ni->ni_vap; 1309 struct ieee80211_mesh_state *ms = vap->iv_mesh; 1310 struct ieee80211com *ic = ni->ni_ic; 1311 struct ieee80211_frame *wh; 1312 uint8_t *frm, *efrm; 1313 1314 wh = mtod(m0, struct ieee80211_frame *); 1315 frm = (uint8_t *)&wh[1]; 1316 efrm = mtod(m0, uint8_t *) + m0->m_len; 1317 switch (subtype) { 1318 case IEEE80211_FC0_SUBTYPE_PROBE_RESP: 1319 case IEEE80211_FC0_SUBTYPE_BEACON: 1320 { 1321 struct ieee80211_scanparams scan; 1322 /* 1323 * We process beacon/probe response 1324 * frames to discover neighbors. 1325 */ 1326 if (ieee80211_parse_beacon(ni, m0, &scan) != 0) 1327 return; 1328 /* 1329 * Count frame now that we know it's to be processed. 1330 */ 1331 if (subtype == IEEE80211_FC0_SUBTYPE_BEACON) { 1332 vap->iv_stats.is_rx_beacon++; /* XXX remove */ 1333 IEEE80211_NODE_STAT(ni, rx_beacons); 1334 } else 1335 IEEE80211_NODE_STAT(ni, rx_proberesp); 1336 /* 1337 * If scanning, just pass information to the scan module. 1338 */ 1339 if (ic->ic_flags & IEEE80211_F_SCAN) { 1340 if (ic->ic_flags_ext & IEEE80211_FEXT_PROBECHAN) { 1341 /* 1342 * Actively scanning a channel marked passive; 1343 * send a probe request now that we know there 1344 * is 802.11 traffic present. 1345 * 1346 * XXX check if the beacon we recv'd gives 1347 * us what we need and suppress the probe req 1348 */ 1349 ieee80211_probe_curchan(vap, 1); 1350 ic->ic_flags_ext &= ~IEEE80211_FEXT_PROBECHAN; 1351 } 1352 ieee80211_add_scan(vap, &scan, wh, 1353 subtype, rssi, nf); 1354 return; 1355 } 1356 1357 /* The rest of this code assumes we are running */ 1358 if (vap->iv_state != IEEE80211_S_RUN) 1359 return; 1360 /* 1361 * Ignore non-mesh STAs. 1362 */ 1363 if ((scan.capinfo & 1364 (IEEE80211_CAPINFO_ESS|IEEE80211_CAPINFO_IBSS)) || 1365 scan.meshid == NULL || scan.meshconf == NULL) { 1366 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT, 1367 wh, "beacon", "%s", "not a mesh sta"); 1368 vap->iv_stats.is_mesh_wrongmesh++; 1369 return; 1370 } 1371 /* 1372 * Ignore STAs for other mesh networks. 1373 */ 1374 if (memcmp(scan.meshid+2, ms->ms_id, ms->ms_idlen) != 0 || 1375 mesh_verify_meshconf(vap, scan.meshconf)) { 1376 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT, 1377 wh, "beacon", "%s", "not for our mesh"); 1378 vap->iv_stats.is_mesh_wrongmesh++; 1379 return; 1380 } 1381 /* 1382 * Peer only based on the current ACL policy. 1383 */ 1384 if (vap->iv_acl != NULL && 1385 !vap->iv_acl->iac_check(vap, wh->i_addr2)) { 1386 IEEE80211_DISCARD(vap, IEEE80211_MSG_ACL, 1387 wh, NULL, "%s", "disallowed by ACL"); 1388 vap->iv_stats.is_rx_acl++; 1389 return; 1390 } 1391 /* 1392 * Do neighbor discovery. 1393 */ 1394 if (!IEEE80211_ADDR_EQ(wh->i_addr2, ni->ni_macaddr)) { 1395 /* 1396 * Create a new entry in the neighbor table. 1397 */ 1398 ni = ieee80211_add_neighbor(vap, wh, &scan); 1399 } 1400 /* 1401 * Automatically peer with discovered nodes if possible. 1402 * XXX backoff on repeated failure 1403 */ 1404 if (ni != vap->iv_bss && 1405 (ms->ms_flags & IEEE80211_MESHFLAGS_AP) && 1406 ni->ni_mlstate == IEEE80211_NODE_MESH_IDLE) { 1407 uint16_t args[1]; 1408 1409 ni->ni_mlpid = mesh_generateid(vap); 1410 if (ni->ni_mlpid == 0) 1411 return; 1412 mesh_linkchange(ni, IEEE80211_NODE_MESH_OPENSNT); 1413 args[0] = ni->ni_mlpid; 1414 ieee80211_send_action(ni, 1415 IEEE80211_ACTION_CAT_MESHPEERING, 1416 IEEE80211_ACTION_MESHPEERING_OPEN, args); 1417 ni->ni_mlrcnt = 0; 1418 mesh_peer_timeout_setup(ni); 1419 } 1420 break; 1421 } 1422 case IEEE80211_FC0_SUBTYPE_PROBE_REQ: 1423 { 1424 uint8_t *ssid, *meshid, *rates, *xrates; 1425 uint8_t *sfrm; 1426 1427 if (vap->iv_state != IEEE80211_S_RUN) { 1428 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT, 1429 wh, NULL, "wrong state %s", 1430 ieee80211_state_name[vap->iv_state]); 1431 vap->iv_stats.is_rx_mgtdiscard++; 1432 return; 1433 } 1434 if (IEEE80211_IS_MULTICAST(wh->i_addr2)) { 1435 /* frame must be directed */ 1436 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT, 1437 wh, NULL, "%s", "not unicast"); 1438 vap->iv_stats.is_rx_mgtdiscard++; /* XXX stat */ 1439 return; 1440 } 1441 /* 1442 * prreq frame format 1443 * [tlv] ssid 1444 * [tlv] supported rates 1445 * [tlv] extended supported rates 1446 * [tlv] mesh id 1447 */ 1448 ssid = meshid = rates = xrates = NULL; 1449 sfrm = frm; 1450 while (efrm - frm > 1) { 1451 IEEE80211_VERIFY_LENGTH(efrm - frm, frm[1] + 2, return); 1452 switch (*frm) { 1453 case IEEE80211_ELEMID_SSID: 1454 ssid = frm; 1455 break; 1456 case IEEE80211_ELEMID_RATES: 1457 rates = frm; 1458 break; 1459 case IEEE80211_ELEMID_XRATES: 1460 xrates = frm; 1461 break; 1462 case IEEE80211_ELEMID_MESHID: 1463 meshid = frm; 1464 break; 1465 } 1466 frm += frm[1] + 2; 1467 } 1468 IEEE80211_VERIFY_ELEMENT(ssid, IEEE80211_NWID_LEN, return); 1469 IEEE80211_VERIFY_ELEMENT(rates, IEEE80211_RATE_MAXSIZE, return); 1470 if (xrates != NULL) 1471 IEEE80211_VERIFY_ELEMENT(xrates, 1472 IEEE80211_RATE_MAXSIZE - rates[1], return); 1473 if (meshid != NULL) { 1474 IEEE80211_VERIFY_ELEMENT(meshid, 1475 IEEE80211_MESHID_LEN, return); 1476 /* NB: meshid, not ssid */ 1477 IEEE80211_VERIFY_SSID(vap->iv_bss, meshid, return); 1478 } 1479 1480 /* XXX find a better class or define it's own */ 1481 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_INPUT, wh->i_addr2, 1482 "%s", "recv probe req"); 1483 /* 1484 * Some legacy 11b clients cannot hack a complete 1485 * probe response frame. When the request includes 1486 * only a bare-bones rate set, communicate this to 1487 * the transmit side. 1488 */ 1489 ieee80211_send_proberesp(vap, wh->i_addr2, 0); 1490 break; 1491 } 1492 1493 case IEEE80211_FC0_SUBTYPE_ACTION: 1494 case IEEE80211_FC0_SUBTYPE_ACTION_NOACK: 1495 if (ni == vap->iv_bss) { 1496 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT, 1497 wh, NULL, "%s", "unknown node"); 1498 vap->iv_stats.is_rx_mgtdiscard++; 1499 } else if (!IEEE80211_ADDR_EQ(vap->iv_myaddr, wh->i_addr1) && 1500 !IEEE80211_IS_MULTICAST(wh->i_addr1)) { 1501 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT, 1502 wh, NULL, "%s", "not for us"); 1503 vap->iv_stats.is_rx_mgtdiscard++; 1504 } else if (vap->iv_state != IEEE80211_S_RUN) { 1505 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT, 1506 wh, NULL, "wrong state %s", 1507 ieee80211_state_name[vap->iv_state]); 1508 vap->iv_stats.is_rx_mgtdiscard++; 1509 } else { 1510 if (ieee80211_parse_action(ni, m0) == 0) 1511 (void)ic->ic_recv_action(ni, wh, frm, efrm); 1512 } 1513 break; 1514 1515 case IEEE80211_FC0_SUBTYPE_ASSOC_REQ: 1516 case IEEE80211_FC0_SUBTYPE_ASSOC_RESP: 1517 case IEEE80211_FC0_SUBTYPE_REASSOC_REQ: 1518 case IEEE80211_FC0_SUBTYPE_REASSOC_RESP: 1519 case IEEE80211_FC0_SUBTYPE_ATIM: 1520 case IEEE80211_FC0_SUBTYPE_DISASSOC: 1521 case IEEE80211_FC0_SUBTYPE_AUTH: 1522 case IEEE80211_FC0_SUBTYPE_DEAUTH: 1523 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT, 1524 wh, NULL, "%s", "not handled"); 1525 vap->iv_stats.is_rx_mgtdiscard++; 1526 break; 1527 1528 default: 1529 IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY, 1530 wh, "mgt", "subtype 0x%x not handled", subtype); 1531 vap->iv_stats.is_rx_badsubtype++; 1532 break; 1533 } 1534 } 1535 1536 static void 1537 mesh_recv_ctl(struct ieee80211_node *ni, struct mbuf *m, int subtype) 1538 { 1539 1540 switch (subtype) { 1541 case IEEE80211_FC0_SUBTYPE_BAR: 1542 ieee80211_recv_bar(ni, m); 1543 break; 1544 } 1545 } 1546 1547 /* 1548 * Parse meshpeering action ie's for open+confirm frames; the 1549 * important bits are returned in the supplied structure. 1550 */ 1551 static const struct ieee80211_meshpeer_ie * 1552 mesh_parse_meshpeering_action(struct ieee80211_node *ni, 1553 const struct ieee80211_frame *wh, /* XXX for VERIFY_LENGTH */ 1554 const uint8_t *frm, const uint8_t *efrm, 1555 struct ieee80211_meshpeer_ie *mp, uint8_t subtype) 1556 { 1557 struct ieee80211vap *vap = ni->ni_vap; 1558 const struct ieee80211_meshpeer_ie *mpie; 1559 const uint8_t *meshid, *meshconf, *meshpeer; 1560 1561 meshid = meshconf = meshpeer = NULL; 1562 while (efrm - frm > 1) { 1563 IEEE80211_VERIFY_LENGTH(efrm - frm, frm[1] + 2, return NULL); 1564 switch (*frm) { 1565 case IEEE80211_ELEMID_MESHID: 1566 meshid = frm; 1567 break; 1568 case IEEE80211_ELEMID_MESHCONF: 1569 meshconf = frm; 1570 break; 1571 case IEEE80211_ELEMID_MESHPEER: 1572 meshpeer = frm; 1573 mpie = (const struct ieee80211_meshpeer_ie *) frm; 1574 memset(mp, 0, sizeof(*mp)); 1575 mp->peer_llinkid = LE_READ_2(&mpie->peer_llinkid); 1576 /* NB: peer link ID is optional on these frames */ 1577 if (subtype == IEEE80211_MESH_PEER_LINK_CLOSE && 1578 mpie->peer_len == 8) { 1579 mp->peer_linkid = 0; 1580 mp->peer_rcode = LE_READ_2(&mpie->peer_linkid); 1581 } else { 1582 mp->peer_linkid = LE_READ_2(&mpie->peer_linkid); 1583 mp->peer_rcode = LE_READ_2(&mpie->peer_rcode); 1584 } 1585 break; 1586 } 1587 frm += frm[1] + 2; 1588 } 1589 1590 /* 1591 * Verify the contents of the frame. Action frames with 1592 * close subtype don't have a Mesh Configuration IE. 1593 * If if fails validation, close the peer link. 1594 */ 1595 KASSERT(meshpeer != NULL && 1596 subtype != IEEE80211_ACTION_MESHPEERING_CLOSE, 1597 ("parsing close action")); 1598 1599 if (mesh_verify_meshid(vap, meshid) || 1600 mesh_verify_meshpeer(vap, subtype, meshpeer) || 1601 mesh_verify_meshconf(vap, meshconf)) { 1602 uint16_t args[3]; 1603 1604 IEEE80211_DISCARD(vap, 1605 IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH, 1606 wh, NULL, "%s", "not for our mesh"); 1607 vap->iv_stats.is_rx_mgtdiscard++; 1608 switch (ni->ni_mlstate) { 1609 case IEEE80211_NODE_MESH_IDLE: 1610 case IEEE80211_NODE_MESH_ESTABLISHED: 1611 case IEEE80211_NODE_MESH_HOLDING: 1612 /* ignore */ 1613 break; 1614 case IEEE80211_NODE_MESH_OPENSNT: 1615 case IEEE80211_NODE_MESH_OPENRCV: 1616 case IEEE80211_NODE_MESH_CONFIRMRCV: 1617 args[0] = ni->ni_mlpid; 1618 args[1] = ni->ni_mllid; 1619 args[2] = IEEE80211_REASON_PEER_LINK_CANCELED; 1620 ieee80211_send_action(ni, 1621 IEEE80211_ACTION_CAT_MESHPEERING, 1622 IEEE80211_ACTION_MESHPEERING_CLOSE, 1623 args); 1624 mesh_linkchange(ni, IEEE80211_NODE_MESH_HOLDING); 1625 mesh_peer_timeout_setup(ni); 1626 break; 1627 } 1628 return NULL; 1629 } 1630 return (const struct ieee80211_meshpeer_ie *) mp; 1631 } 1632 1633 static int 1634 mesh_recv_action_meshpeering_open(struct ieee80211_node *ni, 1635 const struct ieee80211_frame *wh, 1636 const uint8_t *frm, const uint8_t *efrm) 1637 { 1638 struct ieee80211vap *vap = ni->ni_vap; 1639 struct ieee80211_meshpeer_ie ie; 1640 const struct ieee80211_meshpeer_ie *meshpeer; 1641 uint16_t args[3]; 1642 1643 /* +2+2 for action + code + capabilites */ 1644 meshpeer = mesh_parse_meshpeering_action(ni, wh, frm+2+2, efrm, &ie, 1645 IEEE80211_ACTION_MESHPEERING_OPEN); 1646 if (meshpeer == NULL) { 1647 return 0; 1648 } 1649 1650 /* XXX move up */ 1651 IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH, ni, 1652 "recv PEER OPEN, lid 0x%x", meshpeer->peer_llinkid); 1653 1654 switch (ni->ni_mlstate) { 1655 case IEEE80211_NODE_MESH_IDLE: 1656 mesh_linkchange(ni, IEEE80211_NODE_MESH_OPENRCV); 1657 ni->ni_mllid = meshpeer->peer_llinkid; 1658 ni->ni_mlpid = mesh_generateid(vap); 1659 if (ni->ni_mlpid == 0) 1660 return 0; /* XXX */ 1661 args[0] = ni->ni_mlpid; 1662 /* Announce we're open too... */ 1663 ieee80211_send_action(ni, 1664 IEEE80211_ACTION_CAT_MESHPEERING, 1665 IEEE80211_ACTION_MESHPEERING_OPEN, args); 1666 /* ...and confirm the link. */ 1667 args[0] = ni->ni_mlpid; 1668 args[1] = ni->ni_mllid; 1669 ieee80211_send_action(ni, 1670 IEEE80211_ACTION_CAT_MESHPEERING, 1671 IEEE80211_ACTION_MESHPEERING_CONFIRM, 1672 args); 1673 mesh_peer_timeout_setup(ni); 1674 break; 1675 case IEEE80211_NODE_MESH_OPENRCV: 1676 /* Wrong Link ID */ 1677 if (ni->ni_mllid != meshpeer->peer_llinkid) { 1678 args[0] = ni->ni_mllid; 1679 args[1] = ni->ni_mlpid; 1680 args[2] = IEEE80211_REASON_PEER_LINK_CANCELED; 1681 ieee80211_send_action(ni, 1682 IEEE80211_ACTION_CAT_MESHPEERING, 1683 IEEE80211_ACTION_MESHPEERING_CLOSE, 1684 args); 1685 mesh_linkchange(ni, IEEE80211_NODE_MESH_HOLDING); 1686 mesh_peer_timeout_setup(ni); 1687 break; 1688 } 1689 /* Duplicate open, confirm again. */ 1690 args[0] = ni->ni_mlpid; 1691 args[1] = ni->ni_mllid; 1692 ieee80211_send_action(ni, 1693 IEEE80211_ACTION_CAT_MESHPEERING, 1694 IEEE80211_ACTION_MESHPEERING_CONFIRM, 1695 args); 1696 break; 1697 case IEEE80211_NODE_MESH_OPENSNT: 1698 ni->ni_mllid = meshpeer->peer_llinkid; 1699 mesh_linkchange(ni, IEEE80211_NODE_MESH_OPENRCV); 1700 args[0] = ni->ni_mlpid; 1701 args[1] = ni->ni_mllid; 1702 ieee80211_send_action(ni, 1703 IEEE80211_ACTION_CAT_MESHPEERING, 1704 IEEE80211_ACTION_MESHPEERING_CONFIRM, 1705 args); 1706 /* NB: don't setup/clear any timeout */ 1707 break; 1708 case IEEE80211_NODE_MESH_CONFIRMRCV: 1709 if (ni->ni_mlpid != meshpeer->peer_linkid || 1710 ni->ni_mllid != meshpeer->peer_llinkid) { 1711 args[0] = ni->ni_mlpid; 1712 args[1] = ni->ni_mllid; 1713 args[2] = IEEE80211_REASON_PEER_LINK_CANCELED; 1714 ieee80211_send_action(ni, 1715 IEEE80211_ACTION_CAT_MESHPEERING, 1716 IEEE80211_ACTION_MESHPEERING_CLOSE, 1717 args); 1718 mesh_linkchange(ni, 1719 IEEE80211_NODE_MESH_HOLDING); 1720 mesh_peer_timeout_setup(ni); 1721 break; 1722 } 1723 mesh_linkchange(ni, IEEE80211_NODE_MESH_ESTABLISHED); 1724 ni->ni_mllid = meshpeer->peer_llinkid; 1725 args[0] = ni->ni_mlpid; 1726 args[1] = ni->ni_mllid; 1727 ieee80211_send_action(ni, 1728 IEEE80211_ACTION_CAT_MESHPEERING, 1729 IEEE80211_ACTION_MESHPEERING_CONFIRM, 1730 args); 1731 mesh_peer_timeout_stop(ni); 1732 break; 1733 case IEEE80211_NODE_MESH_ESTABLISHED: 1734 if (ni->ni_mllid != meshpeer->peer_llinkid) { 1735 args[0] = ni->ni_mllid; 1736 args[1] = ni->ni_mlpid; 1737 args[2] = IEEE80211_REASON_PEER_LINK_CANCELED; 1738 ieee80211_send_action(ni, 1739 IEEE80211_ACTION_CAT_MESHPEERING, 1740 IEEE80211_ACTION_MESHPEERING_CLOSE, 1741 args); 1742 mesh_linkchange(ni, IEEE80211_NODE_MESH_HOLDING); 1743 mesh_peer_timeout_setup(ni); 1744 break; 1745 } 1746 args[0] = ni->ni_mlpid; 1747 args[1] = ni->ni_mllid; 1748 ieee80211_send_action(ni, 1749 IEEE80211_ACTION_CAT_MESHPEERING, 1750 IEEE80211_ACTION_MESHPEERING_CONFIRM, 1751 args); 1752 break; 1753 case IEEE80211_NODE_MESH_HOLDING: 1754 args[0] = ni->ni_mlpid; 1755 args[1] = meshpeer->peer_llinkid; 1756 args[2] = IEEE80211_REASON_MESH_MAX_RETRIES; 1757 ieee80211_send_action(ni, 1758 IEEE80211_ACTION_CAT_MESHPEERING, 1759 IEEE80211_ACTION_MESHPEERING_CLOSE, 1760 args); 1761 break; 1762 } 1763 return 0; 1764 } 1765 1766 static int 1767 mesh_recv_action_meshpeering_confirm(struct ieee80211_node *ni, 1768 const struct ieee80211_frame *wh, 1769 const uint8_t *frm, const uint8_t *efrm) 1770 { 1771 struct ieee80211vap *vap = ni->ni_vap; 1772 struct ieee80211_meshpeer_ie ie; 1773 const struct ieee80211_meshpeer_ie *meshpeer; 1774 uint16_t args[3]; 1775 1776 /* +2+2+2+2 for action + code + capabilites + status code + AID */ 1777 meshpeer = mesh_parse_meshpeering_action(ni, wh, frm+2+2+2+2, efrm, &ie, 1778 IEEE80211_ACTION_MESHPEERING_CONFIRM); 1779 if (meshpeer == NULL) { 1780 return 0; 1781 } 1782 1783 IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH, ni, 1784 "recv PEER CONFIRM, local id 0x%x, peer id 0x%x", 1785 meshpeer->peer_llinkid, meshpeer->peer_linkid); 1786 1787 switch (ni->ni_mlstate) { 1788 case IEEE80211_NODE_MESH_OPENRCV: 1789 mesh_linkchange(ni, IEEE80211_NODE_MESH_ESTABLISHED); 1790 mesh_peer_timeout_stop(ni); 1791 break; 1792 case IEEE80211_NODE_MESH_OPENSNT: 1793 mesh_linkchange(ni, IEEE80211_NODE_MESH_CONFIRMRCV); 1794 break; 1795 case IEEE80211_NODE_MESH_HOLDING: 1796 args[0] = ni->ni_mlpid; 1797 args[1] = meshpeer->peer_llinkid; 1798 args[2] = IEEE80211_REASON_MESH_MAX_RETRIES; 1799 ieee80211_send_action(ni, 1800 IEEE80211_ACTION_CAT_MESHPEERING, 1801 IEEE80211_ACTION_MESHPEERING_CLOSE, 1802 args); 1803 break; 1804 case IEEE80211_NODE_MESH_CONFIRMRCV: 1805 if (ni->ni_mllid != meshpeer->peer_llinkid) { 1806 args[0] = ni->ni_mlpid; 1807 args[1] = ni->ni_mllid; 1808 args[2] = IEEE80211_REASON_PEER_LINK_CANCELED; 1809 ieee80211_send_action(ni, 1810 IEEE80211_ACTION_CAT_MESHPEERING, 1811 IEEE80211_ACTION_MESHPEERING_CLOSE, 1812 args); 1813 mesh_linkchange(ni, IEEE80211_NODE_MESH_HOLDING); 1814 mesh_peer_timeout_setup(ni); 1815 } 1816 break; 1817 default: 1818 IEEE80211_DISCARD(vap, 1819 IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH, 1820 wh, NULL, "received confirm in invalid state %d", 1821 ni->ni_mlstate); 1822 vap->iv_stats.is_rx_mgtdiscard++; 1823 break; 1824 } 1825 return 0; 1826 } 1827 1828 static int 1829 mesh_recv_action_meshpeering_close(struct ieee80211_node *ni, 1830 const struct ieee80211_frame *wh, 1831 const uint8_t *frm, const uint8_t *efrm) 1832 { 1833 uint16_t args[3]; 1834 1835 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH, 1836 ni, "%s", "recv PEER CLOSE"); 1837 1838 switch (ni->ni_mlstate) { 1839 case IEEE80211_NODE_MESH_IDLE: 1840 /* ignore */ 1841 break; 1842 case IEEE80211_NODE_MESH_OPENRCV: 1843 case IEEE80211_NODE_MESH_OPENSNT: 1844 case IEEE80211_NODE_MESH_CONFIRMRCV: 1845 case IEEE80211_NODE_MESH_ESTABLISHED: 1846 args[0] = ni->ni_mlpid; 1847 args[1] = ni->ni_mllid; 1848 args[2] = IEEE80211_REASON_MESH_CLOSE_RCVD; 1849 ieee80211_send_action(ni, 1850 IEEE80211_ACTION_CAT_MESHPEERING, 1851 IEEE80211_ACTION_MESHPEERING_CLOSE, 1852 args); 1853 mesh_linkchange(ni, IEEE80211_NODE_MESH_HOLDING); 1854 mesh_peer_timeout_setup(ni); 1855 break; 1856 case IEEE80211_NODE_MESH_HOLDING: 1857 mesh_linkchange(ni, IEEE80211_NODE_MESH_IDLE); 1858 mesh_peer_timeout_setup(ni); 1859 break; 1860 } 1861 return 0; 1862 } 1863 1864 /* 1865 * Link Metric handling. 1866 */ 1867 static int 1868 mesh_recv_action_meshlmetric_req(struct ieee80211_node *ni, 1869 const struct ieee80211_frame *wh, 1870 const uint8_t *frm, const uint8_t *efrm) 1871 { 1872 uint32_t metric; 1873 1874 metric = mesh_airtime_calc(ni); 1875 ieee80211_send_action(ni, 1876 IEEE80211_ACTION_CAT_MESHLMETRIC, 1877 IEEE80211_ACTION_MESHLMETRIC_REP, 1878 &metric); 1879 return 0; 1880 } 1881 1882 static int 1883 mesh_recv_action_meshlmetric_rep(struct ieee80211_node *ni, 1884 const struct ieee80211_frame *wh, 1885 const uint8_t *frm, const uint8_t *efrm) 1886 { 1887 return 0; 1888 } 1889 1890 static int 1891 mesh_send_action(struct ieee80211_node *ni, struct mbuf *m) 1892 { 1893 struct ieee80211_bpf_params params; 1894 1895 memset(¶ms, 0, sizeof(params)); 1896 params.ibp_pri = WME_AC_VO; 1897 params.ibp_rate0 = ni->ni_txparms->mgmtrate; 1898 /* XXX ucast/mcast */ 1899 params.ibp_try0 = ni->ni_txparms->maxretry; 1900 params.ibp_power = ni->ni_txpower; 1901 return ieee80211_mgmt_output(ni, m, IEEE80211_FC0_SUBTYPE_ACTION, 1902 ¶ms); 1903 } 1904 1905 #define ADDSHORT(frm, v) do { \ 1906 frm[0] = (v) & 0xff; \ 1907 frm[1] = (v) >> 8; \ 1908 frm += 2; \ 1909 } while (0) 1910 #define ADDWORD(frm, v) do { \ 1911 frm[0] = (v) & 0xff; \ 1912 frm[1] = ((v) >> 8) & 0xff; \ 1913 frm[2] = ((v) >> 16) & 0xff; \ 1914 frm[3] = ((v) >> 24) & 0xff; \ 1915 frm += 4; \ 1916 } while (0) 1917 1918 static int 1919 mesh_send_action_meshpeering_open(struct ieee80211_node *ni, 1920 int category, int action, void *args0) 1921 { 1922 struct ieee80211vap *vap = ni->ni_vap; 1923 struct ieee80211com *ic = ni->ni_ic; 1924 uint16_t *args = args0; 1925 const struct ieee80211_rateset *rs; 1926 struct mbuf *m; 1927 uint8_t *frm; 1928 1929 IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH, ni, 1930 "send PEER OPEN action: localid 0x%x", args[0]); 1931 1932 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE, 1933 "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", __func__, __LINE__, 1934 ni, ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)+1); 1935 ieee80211_ref_node(ni); 1936 1937 m = ieee80211_getmgtframe(&frm, 1938 ic->ic_headroom + sizeof(struct ieee80211_frame), 1939 sizeof(uint16_t) /* action+category */ 1940 + sizeof(uint16_t) /* capabilites */ 1941 + 2 + IEEE80211_RATE_SIZE 1942 + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE) 1943 + 2 + IEEE80211_MESHID_LEN 1944 + sizeof(struct ieee80211_meshconf_ie) 1945 + sizeof(struct ieee80211_meshpeer_ie) 1946 ); 1947 if (m != NULL) { 1948 /* 1949 * mesh peer open action frame format: 1950 * [1] category 1951 * [1] action 1952 * [2] capabilities 1953 * [tlv] rates 1954 * [tlv] xrates 1955 * [tlv] mesh id 1956 * [tlv] mesh conf 1957 * [tlv] mesh peer link mgmt 1958 */ 1959 *frm++ = category; 1960 *frm++ = action; 1961 ADDSHORT(frm, ieee80211_getcapinfo(vap, ni->ni_chan)); 1962 rs = ieee80211_get_suprates(ic, ic->ic_curchan); 1963 frm = ieee80211_add_rates(frm, rs); 1964 frm = ieee80211_add_xrates(frm, rs); 1965 frm = ieee80211_add_meshid(frm, vap); 1966 frm = ieee80211_add_meshconf(frm, vap); 1967 frm = ieee80211_add_meshpeer(frm, IEEE80211_MESH_PEER_LINK_OPEN, 1968 args[0], 0, 0); 1969 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *); 1970 return mesh_send_action(ni, m); 1971 } else { 1972 vap->iv_stats.is_tx_nobuf++; 1973 ieee80211_free_node(ni); 1974 return ENOMEM; 1975 } 1976 } 1977 1978 static int 1979 mesh_send_action_meshpeering_confirm(struct ieee80211_node *ni, 1980 int category, int action, void *args0) 1981 { 1982 struct ieee80211vap *vap = ni->ni_vap; 1983 struct ieee80211com *ic = ni->ni_ic; 1984 uint16_t *args = args0; 1985 const struct ieee80211_rateset *rs; 1986 struct mbuf *m; 1987 uint8_t *frm; 1988 1989 IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH, ni, 1990 "send PEER CONFIRM action: localid 0x%x, peerid 0x%x", 1991 args[0], args[1]); 1992 1993 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE, 1994 "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", __func__, __LINE__, 1995 ni, ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)+1); 1996 ieee80211_ref_node(ni); 1997 1998 m = ieee80211_getmgtframe(&frm, 1999 ic->ic_headroom + sizeof(struct ieee80211_frame), 2000 sizeof(uint16_t) /* action+category */ 2001 + sizeof(uint16_t) /* capabilites */ 2002 + sizeof(uint16_t) /* status code */ 2003 + sizeof(uint16_t) /* AID */ 2004 + 2 + IEEE80211_RATE_SIZE 2005 + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE) 2006 + 2 + IEEE80211_MESHID_LEN 2007 + sizeof(struct ieee80211_meshconf_ie) 2008 + sizeof(struct ieee80211_meshpeer_ie) 2009 ); 2010 if (m != NULL) { 2011 /* 2012 * mesh peer confirm action frame format: 2013 * [1] category 2014 * [1] action 2015 * [2] capabilities 2016 * [2] status code 2017 * [2] association id (peer ID) 2018 * [tlv] rates 2019 * [tlv] xrates 2020 * [tlv] mesh id 2021 * [tlv] mesh conf 2022 * [tlv] mesh peer link mgmt 2023 */ 2024 *frm++ = category; 2025 *frm++ = action; 2026 ADDSHORT(frm, ieee80211_getcapinfo(vap, ni->ni_chan)); 2027 ADDSHORT(frm, 0); /* status code */ 2028 ADDSHORT(frm, args[1]); /* AID */ 2029 rs = ieee80211_get_suprates(ic, ic->ic_curchan); 2030 frm = ieee80211_add_rates(frm, rs); 2031 frm = ieee80211_add_xrates(frm, rs); 2032 frm = ieee80211_add_meshid(frm, vap); 2033 frm = ieee80211_add_meshconf(frm, vap); 2034 frm = ieee80211_add_meshpeer(frm, 2035 IEEE80211_MESH_PEER_LINK_CONFIRM, 2036 args[0], args[1], 0); 2037 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *); 2038 return mesh_send_action(ni, m); 2039 } else { 2040 vap->iv_stats.is_tx_nobuf++; 2041 ieee80211_free_node(ni); 2042 return ENOMEM; 2043 } 2044 } 2045 2046 static int 2047 mesh_send_action_meshpeering_close(struct ieee80211_node *ni, 2048 int category, int action, void *args0) 2049 { 2050 struct ieee80211vap *vap = ni->ni_vap; 2051 struct ieee80211com *ic = ni->ni_ic; 2052 uint16_t *args = args0; 2053 struct mbuf *m; 2054 uint8_t *frm; 2055 2056 IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH, ni, 2057 "send PEER CLOSE action: localid 0x%x, peerid 0x%x reason %d", 2058 args[0], args[1], args[2]); 2059 2060 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE, 2061 "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", __func__, __LINE__, 2062 ni, ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)+1); 2063 ieee80211_ref_node(ni); 2064 2065 m = ieee80211_getmgtframe(&frm, 2066 ic->ic_headroom + sizeof(struct ieee80211_frame), 2067 sizeof(uint16_t) /* action+category */ 2068 + sizeof(uint16_t) /* reason code */ 2069 + 2 + IEEE80211_MESHID_LEN 2070 + sizeof(struct ieee80211_meshpeer_ie) 2071 ); 2072 if (m != NULL) { 2073 /* 2074 * mesh peer close action frame format: 2075 * [1] category 2076 * [1] action 2077 * [2] reason code 2078 * [tlv] mesh id 2079 * [tlv] mesh peer link mgmt 2080 */ 2081 *frm++ = category; 2082 *frm++ = action; 2083 ADDSHORT(frm, args[2]); /* reason code */ 2084 frm = ieee80211_add_meshid(frm, vap); 2085 frm = ieee80211_add_meshpeer(frm, 2086 IEEE80211_MESH_PEER_LINK_CLOSE, 2087 args[0], args[1], args[2]); 2088 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *); 2089 return mesh_send_action(ni, m); 2090 } else { 2091 vap->iv_stats.is_tx_nobuf++; 2092 ieee80211_free_node(ni); 2093 return ENOMEM; 2094 } 2095 } 2096 2097 static int 2098 mesh_send_action_meshlink_request(struct ieee80211_node *ni, 2099 int category, int action, void *arg0) 2100 { 2101 struct ieee80211vap *vap = ni->ni_vap; 2102 struct ieee80211com *ic = ni->ni_ic; 2103 struct mbuf *m; 2104 uint8_t *frm; 2105 2106 IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH, ni, 2107 "%s", "send LINK METRIC REQUEST action"); 2108 2109 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE, 2110 "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", __func__, __LINE__, 2111 ni, ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)+1); 2112 ieee80211_ref_node(ni); 2113 2114 m = ieee80211_getmgtframe(&frm, 2115 ic->ic_headroom + sizeof(struct ieee80211_frame), 2116 sizeof(uint16_t) /* action+category */ 2117 ); 2118 if (m != NULL) { 2119 /* 2120 * mesh link metric request 2121 * [1] category 2122 * [1] action 2123 */ 2124 *frm++ = category; 2125 *frm++ = action; 2126 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *); 2127 return mesh_send_action(ni, m); 2128 } else { 2129 vap->iv_stats.is_tx_nobuf++; 2130 ieee80211_free_node(ni); 2131 return ENOMEM; 2132 } 2133 } 2134 2135 static int 2136 mesh_send_action_meshlink_reply(struct ieee80211_node *ni, 2137 int category, int action, void *args0) 2138 { 2139 struct ieee80211vap *vap = ni->ni_vap; 2140 struct ieee80211com *ic = ni->ni_ic; 2141 uint32_t *metric = args0; 2142 struct mbuf *m; 2143 uint8_t *frm; 2144 2145 IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH, ni, 2146 "send LINK METRIC REPLY action: metric 0x%x", *metric); 2147 2148 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE, 2149 "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", __func__, __LINE__, 2150 ni, ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)+1); 2151 ieee80211_ref_node(ni); 2152 2153 m = ieee80211_getmgtframe(&frm, 2154 ic->ic_headroom + sizeof(struct ieee80211_frame), 2155 sizeof(uint16_t) /* action+category */ 2156 + sizeof(struct ieee80211_meshlmetric_ie) 2157 ); 2158 if (m != NULL) { 2159 /* 2160 * mesh link metric reply 2161 * [1] category 2162 * [1] action 2163 * [tlv] mesh link metric 2164 */ 2165 *frm++ = category; 2166 *frm++ = action; 2167 frm = ieee80211_add_meshlmetric(frm, *metric); 2168 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *); 2169 return mesh_send_action(ni, m); 2170 } else { 2171 vap->iv_stats.is_tx_nobuf++; 2172 ieee80211_free_node(ni); 2173 return ENOMEM; 2174 } 2175 } 2176 2177 static void 2178 mesh_peer_timeout_setup(struct ieee80211_node *ni) 2179 { 2180 switch (ni->ni_mlstate) { 2181 case IEEE80211_NODE_MESH_HOLDING: 2182 ni->ni_mltval = ieee80211_mesh_holdingtimeout; 2183 break; 2184 case IEEE80211_NODE_MESH_CONFIRMRCV: 2185 ni->ni_mltval = ieee80211_mesh_confirmtimeout; 2186 break; 2187 case IEEE80211_NODE_MESH_IDLE: 2188 ni->ni_mltval = 0; 2189 break; 2190 default: 2191 ni->ni_mltval = ieee80211_mesh_retrytimeout; 2192 break; 2193 } 2194 if (ni->ni_mltval) 2195 callout_reset(&ni->ni_mltimer, ni->ni_mltval, 2196 mesh_peer_timeout_cb, ni); 2197 } 2198 2199 /* 2200 * Same as above but backoffs timer statisically 50%. 2201 */ 2202 static void 2203 mesh_peer_timeout_backoff(struct ieee80211_node *ni) 2204 { 2205 uint32_t r; 2206 2207 r = arc4random(); 2208 ni->ni_mltval += r % ni->ni_mltval; 2209 callout_reset(&ni->ni_mltimer, ni->ni_mltval, mesh_peer_timeout_cb, 2210 ni); 2211 } 2212 2213 static __inline void 2214 mesh_peer_timeout_stop(struct ieee80211_node *ni) 2215 { 2216 callout_drain(&ni->ni_mltimer); 2217 } 2218 2219 /* 2220 * Mesh Peer Link Management FSM timeout handling. 2221 */ 2222 static void 2223 mesh_peer_timeout_cb(void *arg) 2224 { 2225 struct ieee80211_node *ni = (struct ieee80211_node *)arg; 2226 uint16_t args[3]; 2227 2228 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_MESH, 2229 ni, "mesh link timeout, state %d, retry counter %d", 2230 ni->ni_mlstate, ni->ni_mlrcnt); 2231 2232 switch (ni->ni_mlstate) { 2233 case IEEE80211_NODE_MESH_IDLE: 2234 case IEEE80211_NODE_MESH_ESTABLISHED: 2235 break; 2236 case IEEE80211_NODE_MESH_OPENSNT: 2237 case IEEE80211_NODE_MESH_OPENRCV: 2238 if (ni->ni_mlrcnt == ieee80211_mesh_maxretries) { 2239 args[0] = ni->ni_mlpid; 2240 args[2] = IEEE80211_REASON_MESH_MAX_RETRIES; 2241 ieee80211_send_action(ni, 2242 IEEE80211_ACTION_CAT_MESHPEERING, 2243 IEEE80211_ACTION_MESHPEERING_CLOSE, args); 2244 ni->ni_mlrcnt = 0; 2245 mesh_linkchange(ni, IEEE80211_NODE_MESH_HOLDING); 2246 mesh_peer_timeout_setup(ni); 2247 } else { 2248 args[0] = ni->ni_mlpid; 2249 ieee80211_send_action(ni, 2250 IEEE80211_ACTION_CAT_MESHPEERING, 2251 IEEE80211_ACTION_MESHPEERING_OPEN, args); 2252 ni->ni_mlrcnt++; 2253 mesh_peer_timeout_backoff(ni); 2254 } 2255 break; 2256 case IEEE80211_NODE_MESH_CONFIRMRCV: 2257 if (ni->ni_mlrcnt == ieee80211_mesh_maxretries) { 2258 args[0] = ni->ni_mlpid; 2259 args[2] = IEEE80211_REASON_MESH_CONFIRM_TIMEOUT; 2260 ieee80211_send_action(ni, 2261 IEEE80211_ACTION_CAT_MESHPEERING, 2262 IEEE80211_ACTION_MESHPEERING_CLOSE, args); 2263 ni->ni_mlrcnt = 0; 2264 mesh_linkchange(ni, IEEE80211_NODE_MESH_HOLDING); 2265 mesh_peer_timeout_setup(ni); 2266 } else { 2267 ni->ni_mlrcnt++; 2268 mesh_peer_timeout_setup(ni); 2269 } 2270 break; 2271 case IEEE80211_NODE_MESH_HOLDING: 2272 mesh_linkchange(ni, IEEE80211_NODE_MESH_IDLE); 2273 break; 2274 } 2275 } 2276 2277 static int 2278 mesh_verify_meshid(struct ieee80211vap *vap, const uint8_t *ie) 2279 { 2280 struct ieee80211_mesh_state *ms = vap->iv_mesh; 2281 2282 if (ie == NULL || ie[1] != ms->ms_idlen) 2283 return 1; 2284 return memcmp(ms->ms_id, ie + 2, ms->ms_idlen); 2285 } 2286 2287 /* 2288 * Check if we are using the same algorithms for this mesh. 2289 */ 2290 static int 2291 mesh_verify_meshconf(struct ieee80211vap *vap, const uint8_t *ie) 2292 { 2293 const struct ieee80211_meshconf_ie *meshconf = 2294 (const struct ieee80211_meshconf_ie *) ie; 2295 const struct ieee80211_mesh_state *ms = vap->iv_mesh; 2296 uint16_t cap; 2297 2298 if (meshconf == NULL) 2299 return 1; 2300 if (meshconf->conf_pselid != ms->ms_ppath->mpp_ie) { 2301 IEEE80211_DPRINTF(vap, IEEE80211_MSG_MESH, 2302 "unknown path selection algorithm: 0x%x\n", 2303 meshconf->conf_pselid); 2304 return 1; 2305 } 2306 if (meshconf->conf_pmetid != ms->ms_pmetric->mpm_ie) { 2307 IEEE80211_DPRINTF(vap, IEEE80211_MSG_MESH, 2308 "unknown path metric algorithm: 0x%x\n", 2309 meshconf->conf_pmetid); 2310 return 1; 2311 } 2312 if (meshconf->conf_ccid != 0) { 2313 IEEE80211_DPRINTF(vap, IEEE80211_MSG_MESH, 2314 "unknown congestion control algorithm: 0x%x\n", 2315 meshconf->conf_ccid); 2316 return 1; 2317 } 2318 if (meshconf->conf_syncid != IEEE80211_MESHCONF_SYNC_NEIGHOFF) { 2319 IEEE80211_DPRINTF(vap, IEEE80211_MSG_MESH, 2320 "unknown sync algorithm: 0x%x\n", 2321 meshconf->conf_syncid); 2322 return 1; 2323 } 2324 if (meshconf->conf_authid != 0) { 2325 IEEE80211_DPRINTF(vap, IEEE80211_MSG_MESH, 2326 "unknown auth auth algorithm: 0x%x\n", 2327 meshconf->conf_pselid); 2328 return 1; 2329 } 2330 /* NB: conf_cap is only read correctly here */ 2331 cap = LE_READ_2(&meshconf->conf_cap); 2332 /* Not accepting peers */ 2333 if (!(cap & IEEE80211_MESHCONF_CAP_AP)) { 2334 IEEE80211_DPRINTF(vap, IEEE80211_MSG_MESH, 2335 "not accepting peers: 0x%x\n", meshconf->conf_cap); 2336 return 1; 2337 } 2338 return 0; 2339 } 2340 2341 static int 2342 mesh_verify_meshpeer(struct ieee80211vap *vap, uint8_t subtype, 2343 const uint8_t *ie) 2344 { 2345 const struct ieee80211_meshpeer_ie *meshpeer = 2346 (const struct ieee80211_meshpeer_ie *) ie; 2347 2348 if (meshpeer == NULL || meshpeer->peer_len < 6 || 2349 meshpeer->peer_len > 10) 2350 return 1; 2351 switch (subtype) { 2352 case IEEE80211_MESH_PEER_LINK_OPEN: 2353 if (meshpeer->peer_len != 6) 2354 return 1; 2355 break; 2356 case IEEE80211_MESH_PEER_LINK_CONFIRM: 2357 if (meshpeer->peer_len != 8) 2358 return 1; 2359 break; 2360 case IEEE80211_MESH_PEER_LINK_CLOSE: 2361 if (meshpeer->peer_len < 8) 2362 return 1; 2363 if (meshpeer->peer_len == 8 && meshpeer->peer_linkid != 0) 2364 return 1; 2365 if (meshpeer->peer_rcode == 0) 2366 return 1; 2367 break; 2368 } 2369 return 0; 2370 } 2371 2372 /* 2373 * Add a Mesh ID IE to a frame. 2374 */ 2375 uint8_t * 2376 ieee80211_add_meshid(uint8_t *frm, struct ieee80211vap *vap) 2377 { 2378 struct ieee80211_mesh_state *ms = vap->iv_mesh; 2379 2380 KASSERT(vap->iv_opmode == IEEE80211_M_MBSS, ("not a mbss vap")); 2381 2382 *frm++ = IEEE80211_ELEMID_MESHID; 2383 *frm++ = ms->ms_idlen; 2384 memcpy(frm, ms->ms_id, ms->ms_idlen); 2385 return frm + ms->ms_idlen; 2386 } 2387 2388 /* 2389 * Add a Mesh Configuration IE to a frame. 2390 * For now just use HWMP routing, Airtime link metric, Null Congestion 2391 * Signaling, Null Sync Protocol and Null Authentication. 2392 */ 2393 uint8_t * 2394 ieee80211_add_meshconf(uint8_t *frm, struct ieee80211vap *vap) 2395 { 2396 const struct ieee80211_mesh_state *ms = vap->iv_mesh; 2397 uint16_t caps; 2398 2399 KASSERT(vap->iv_opmode == IEEE80211_M_MBSS, ("not a MBSS vap")); 2400 2401 *frm++ = IEEE80211_ELEMID_MESHCONF; 2402 *frm++ = sizeof(struct ieee80211_meshconf_ie) - 2; 2403 *frm++ = ms->ms_ppath->mpp_ie; /* path selection */ 2404 *frm++ = ms->ms_pmetric->mpm_ie; /* link metric */ 2405 *frm++ = IEEE80211_MESHCONF_CC_DISABLED; 2406 *frm++ = IEEE80211_MESHCONF_SYNC_NEIGHOFF; 2407 *frm++ = IEEE80211_MESHCONF_AUTH_DISABLED; 2408 /* NB: set the number of neighbors before the rest */ 2409 *frm = (ms->ms_neighbors > 15 ? 15 : ms->ms_neighbors) << 1; 2410 if (ms->ms_flags & IEEE80211_MESHFLAGS_PORTAL) 2411 *frm |= IEEE80211_MESHCONF_FORM_MP; 2412 frm += 1; 2413 caps = 0; 2414 if (ms->ms_flags & IEEE80211_MESHFLAGS_AP) 2415 caps |= IEEE80211_MESHCONF_CAP_AP; 2416 if (ms->ms_flags & IEEE80211_MESHFLAGS_FWD) 2417 caps |= IEEE80211_MESHCONF_CAP_FWRD; 2418 ADDSHORT(frm, caps); 2419 return frm; 2420 } 2421 2422 /* 2423 * Add a Mesh Peer Management IE to a frame. 2424 */ 2425 uint8_t * 2426 ieee80211_add_meshpeer(uint8_t *frm, uint8_t subtype, uint16_t localid, 2427 uint16_t peerid, uint16_t reason) 2428 { 2429 /* XXX change for AH */ 2430 static const uint8_t meshpeerproto[4] = IEEE80211_MESH_PEER_PROTO; 2431 2432 KASSERT(localid != 0, ("localid == 0")); 2433 2434 *frm++ = IEEE80211_ELEMID_MESHPEER; 2435 switch (subtype) { 2436 case IEEE80211_MESH_PEER_LINK_OPEN: 2437 *frm++ = 6; /* length */ 2438 memcpy(frm, meshpeerproto, 4); 2439 frm += 4; 2440 ADDSHORT(frm, localid); /* local ID */ 2441 break; 2442 case IEEE80211_MESH_PEER_LINK_CONFIRM: 2443 KASSERT(peerid != 0, ("sending peer confirm without peer id")); 2444 *frm++ = 8; /* length */ 2445 memcpy(frm, meshpeerproto, 4); 2446 frm += 4; 2447 ADDSHORT(frm, localid); /* local ID */ 2448 ADDSHORT(frm, peerid); /* peer ID */ 2449 break; 2450 case IEEE80211_MESH_PEER_LINK_CLOSE: 2451 if (peerid) 2452 *frm++ = 10; /* length */ 2453 else 2454 *frm++ = 8; /* length */ 2455 memcpy(frm, meshpeerproto, 4); 2456 frm += 4; 2457 ADDSHORT(frm, localid); /* local ID */ 2458 if (peerid) 2459 ADDSHORT(frm, peerid); /* peer ID */ 2460 ADDSHORT(frm, reason); 2461 break; 2462 } 2463 return frm; 2464 } 2465 2466 /* 2467 * Compute an Airtime Link Metric for the link with this node. 2468 * 2469 * Based on Draft 3.0 spec (11B.10, p.149). 2470 */ 2471 /* 2472 * Max 802.11s overhead. 2473 */ 2474 #define IEEE80211_MESH_MAXOVERHEAD \ 2475 (sizeof(struct ieee80211_qosframe_addr4) \ 2476 + sizeof(struct ieee80211_meshcntl_ae11) \ 2477 + sizeof(struct llc) \ 2478 + IEEE80211_ADDR_LEN \ 2479 + IEEE80211_WEP_IVLEN \ 2480 + IEEE80211_WEP_KIDLEN \ 2481 + IEEE80211_WEP_CRCLEN \ 2482 + IEEE80211_WEP_MICLEN \ 2483 + IEEE80211_CRC_LEN) 2484 uint32_t 2485 mesh_airtime_calc(struct ieee80211_node *ni) 2486 { 2487 #define M_BITS 8 2488 #define S_FACTOR (2 * M_BITS) 2489 struct ieee80211com *ic = ni->ni_ic; 2490 struct ifnet *ifp = ni->ni_vap->iv_ifp; 2491 const static int nbits = 8192 << M_BITS; 2492 uint32_t overhead, rate, errrate; 2493 uint64_t res; 2494 2495 /* Time to transmit a frame */ 2496 rate = ni->ni_txrate; 2497 overhead = ieee80211_compute_duration(ic->ic_rt, 2498 ifp->if_mtu + IEEE80211_MESH_MAXOVERHEAD, rate, 0) << M_BITS; 2499 /* Error rate in percentage */ 2500 /* XXX assuming small failures are ok */ 2501 errrate = (((ifp->if_oerrors + 2502 ifp->if_ierrors) / 100) << M_BITS) / 100; 2503 res = (overhead + (nbits / rate)) * 2504 ((1 << S_FACTOR) / ((1 << M_BITS) - errrate)); 2505 2506 return (uint32_t)(res >> S_FACTOR); 2507 #undef M_BITS 2508 #undef S_FACTOR 2509 } 2510 2511 /* 2512 * Add a Mesh Link Metric report IE to a frame. 2513 */ 2514 uint8_t * 2515 ieee80211_add_meshlmetric(uint8_t *frm, uint32_t metric) 2516 { 2517 *frm++ = IEEE80211_ELEMID_MESHLINK; 2518 *frm++ = 4; 2519 ADDWORD(frm, metric); 2520 return frm; 2521 } 2522 #undef ADDSHORT 2523 #undef ADDWORD 2524 2525 /* 2526 * Initialize any mesh-specific node state. 2527 */ 2528 void 2529 ieee80211_mesh_node_init(struct ieee80211vap *vap, struct ieee80211_node *ni) 2530 { 2531 ni->ni_flags |= IEEE80211_NODE_QOS; 2532 callout_init(&ni->ni_mltimer, CALLOUT_MPSAFE); 2533 } 2534 2535 /* 2536 * Cleanup any mesh-specific node state. 2537 */ 2538 void 2539 ieee80211_mesh_node_cleanup(struct ieee80211_node *ni) 2540 { 2541 struct ieee80211vap *vap = ni->ni_vap; 2542 struct ieee80211_mesh_state *ms = vap->iv_mesh; 2543 2544 callout_drain(&ni->ni_mltimer); 2545 /* NB: short-circuit callbacks after mesh_vdetach */ 2546 if (vap->iv_mesh != NULL) 2547 ms->ms_ppath->mpp_peerdown(ni); 2548 } 2549 2550 void 2551 ieee80211_parse_meshid(struct ieee80211_node *ni, const uint8_t *ie) 2552 { 2553 ni->ni_meshidlen = ie[1]; 2554 memcpy(ni->ni_meshid, ie + 2, ie[1]); 2555 } 2556 2557 /* 2558 * Setup mesh-specific node state on neighbor discovery. 2559 */ 2560 void 2561 ieee80211_mesh_init_neighbor(struct ieee80211_node *ni, 2562 const struct ieee80211_frame *wh, 2563 const struct ieee80211_scanparams *sp) 2564 { 2565 ieee80211_parse_meshid(ni, sp->meshid); 2566 } 2567 2568 void 2569 ieee80211_mesh_update_beacon(struct ieee80211vap *vap, 2570 struct ieee80211_beacon_offsets *bo) 2571 { 2572 KASSERT(vap->iv_opmode == IEEE80211_M_MBSS, ("not a MBSS vap")); 2573 2574 if (isset(bo->bo_flags, IEEE80211_BEACON_MESHCONF)) { 2575 (void)ieee80211_add_meshconf(bo->bo_meshconf, vap); 2576 clrbit(bo->bo_flags, IEEE80211_BEACON_MESHCONF); 2577 } 2578 } 2579 2580 static int 2581 mesh_ioctl_get80211(struct ieee80211vap *vap, struct ieee80211req *ireq) 2582 { 2583 struct ieee80211_mesh_state *ms = vap->iv_mesh; 2584 uint8_t tmpmeshid[IEEE80211_NWID_LEN]; 2585 struct ieee80211_mesh_route *rt; 2586 struct ieee80211req_mesh_route *imr; 2587 size_t len, off; 2588 uint8_t *p; 2589 int error; 2590 2591 if (vap->iv_opmode != IEEE80211_M_MBSS) 2592 return ENOSYS; 2593 2594 error = 0; 2595 switch (ireq->i_type) { 2596 case IEEE80211_IOC_MESH_ID: 2597 ireq->i_len = ms->ms_idlen; 2598 memcpy(tmpmeshid, ms->ms_id, ireq->i_len); 2599 error = copyout(tmpmeshid, ireq->i_data, ireq->i_len); 2600 break; 2601 case IEEE80211_IOC_MESH_AP: 2602 ireq->i_val = (ms->ms_flags & IEEE80211_MESHFLAGS_AP) != 0; 2603 break; 2604 case IEEE80211_IOC_MESH_FWRD: 2605 ireq->i_val = (ms->ms_flags & IEEE80211_MESHFLAGS_FWD) != 0; 2606 break; 2607 case IEEE80211_IOC_MESH_TTL: 2608 ireq->i_val = ms->ms_ttl; 2609 break; 2610 case IEEE80211_IOC_MESH_RTCMD: 2611 switch (ireq->i_val) { 2612 case IEEE80211_MESH_RTCMD_LIST: 2613 len = 0; 2614 MESH_RT_LOCK(ms); 2615 TAILQ_FOREACH(rt, &ms->ms_routes, rt_next) { 2616 len += sizeof(*imr); 2617 } 2618 MESH_RT_UNLOCK(ms); 2619 if (len > ireq->i_len || ireq->i_len < sizeof(*imr)) { 2620 ireq->i_len = len; 2621 return ENOMEM; 2622 } 2623 ireq->i_len = len; 2624 /* XXX M_WAIT? */ 2625 p = malloc(len, M_TEMP, M_NOWAIT | M_ZERO); 2626 if (p == NULL) 2627 return ENOMEM; 2628 off = 0; 2629 MESH_RT_LOCK(ms); 2630 TAILQ_FOREACH(rt, &ms->ms_routes, rt_next) { 2631 if (off >= len) 2632 break; 2633 imr = (struct ieee80211req_mesh_route *) 2634 (p + off); 2635 imr->imr_flags = rt->rt_flags; 2636 IEEE80211_ADDR_COPY(imr->imr_dest, 2637 rt->rt_dest); 2638 IEEE80211_ADDR_COPY(imr->imr_nexthop, 2639 rt->rt_nexthop); 2640 imr->imr_metric = rt->rt_metric; 2641 imr->imr_nhops = rt->rt_nhops; 2642 imr->imr_lifetime = rt->rt_lifetime; 2643 imr->imr_lastmseq = rt->rt_lastmseq; 2644 off += sizeof(*imr); 2645 } 2646 MESH_RT_UNLOCK(ms); 2647 error = copyout(p, (uint8_t *)ireq->i_data, 2648 ireq->i_len); 2649 free(p, M_TEMP); 2650 break; 2651 case IEEE80211_MESH_RTCMD_FLUSH: 2652 case IEEE80211_MESH_RTCMD_ADD: 2653 case IEEE80211_MESH_RTCMD_DELETE: 2654 return EINVAL; 2655 default: 2656 return ENOSYS; 2657 } 2658 break; 2659 case IEEE80211_IOC_MESH_PR_METRIC: 2660 len = strlen(ms->ms_pmetric->mpm_descr); 2661 if (ireq->i_len < len) 2662 return EINVAL; 2663 ireq->i_len = len; 2664 error = copyout(ms->ms_pmetric->mpm_descr, 2665 (uint8_t *)ireq->i_data, len); 2666 break; 2667 case IEEE80211_IOC_MESH_PR_PATH: 2668 len = strlen(ms->ms_ppath->mpp_descr); 2669 if (ireq->i_len < len) 2670 return EINVAL; 2671 ireq->i_len = len; 2672 error = copyout(ms->ms_ppath->mpp_descr, 2673 (uint8_t *)ireq->i_data, len); 2674 break; 2675 default: 2676 return ENOSYS; 2677 } 2678 2679 return error; 2680 } 2681 IEEE80211_IOCTL_GET(mesh, mesh_ioctl_get80211); 2682 2683 static int 2684 mesh_ioctl_set80211(struct ieee80211vap *vap, struct ieee80211req *ireq) 2685 { 2686 struct ieee80211_mesh_state *ms = vap->iv_mesh; 2687 uint8_t tmpmeshid[IEEE80211_NWID_LEN]; 2688 uint8_t tmpaddr[IEEE80211_ADDR_LEN]; 2689 char tmpproto[IEEE80211_MESH_PROTO_DSZ]; 2690 int error; 2691 2692 if (vap->iv_opmode != IEEE80211_M_MBSS) 2693 return ENOSYS; 2694 2695 error = 0; 2696 switch (ireq->i_type) { 2697 case IEEE80211_IOC_MESH_ID: 2698 if (ireq->i_val != 0 || ireq->i_len > IEEE80211_MESHID_LEN) 2699 return EINVAL; 2700 error = copyin(ireq->i_data, tmpmeshid, ireq->i_len); 2701 if (error != 0) 2702 break; 2703 memset(ms->ms_id, 0, IEEE80211_NWID_LEN); 2704 ms->ms_idlen = ireq->i_len; 2705 memcpy(ms->ms_id, tmpmeshid, ireq->i_len); 2706 error = ENETRESET; 2707 break; 2708 case IEEE80211_IOC_MESH_AP: 2709 if (ireq->i_val) 2710 ms->ms_flags |= IEEE80211_MESHFLAGS_AP; 2711 else 2712 ms->ms_flags &= ~IEEE80211_MESHFLAGS_AP; 2713 error = ENETRESET; 2714 break; 2715 case IEEE80211_IOC_MESH_FWRD: 2716 if (ireq->i_val) 2717 ms->ms_flags |= IEEE80211_MESHFLAGS_FWD; 2718 else 2719 ms->ms_flags &= ~IEEE80211_MESHFLAGS_FWD; 2720 break; 2721 case IEEE80211_IOC_MESH_TTL: 2722 ms->ms_ttl = (uint8_t) ireq->i_val; 2723 break; 2724 case IEEE80211_IOC_MESH_RTCMD: 2725 switch (ireq->i_val) { 2726 case IEEE80211_MESH_RTCMD_LIST: 2727 return EINVAL; 2728 case IEEE80211_MESH_RTCMD_FLUSH: 2729 ieee80211_mesh_rt_flush(vap); 2730 break; 2731 case IEEE80211_MESH_RTCMD_ADD: 2732 if (IEEE80211_ADDR_EQ(vap->iv_myaddr, ireq->i_data) || 2733 IEEE80211_ADDR_EQ(broadcastaddr, ireq->i_data)) 2734 return EINVAL; 2735 error = copyin(ireq->i_data, &tmpaddr, 2736 IEEE80211_ADDR_LEN); 2737 if (error == 0) 2738 ieee80211_mesh_discover(vap, tmpaddr, NULL); 2739 break; 2740 case IEEE80211_MESH_RTCMD_DELETE: 2741 ieee80211_mesh_rt_del(vap, ireq->i_data); 2742 break; 2743 default: 2744 return ENOSYS; 2745 } 2746 break; 2747 case IEEE80211_IOC_MESH_PR_METRIC: 2748 error = copyin(ireq->i_data, tmpproto, sizeof(tmpproto)); 2749 if (error == 0) { 2750 error = mesh_select_proto_metric(vap, tmpproto); 2751 if (error == 0) 2752 error = ENETRESET; 2753 } 2754 break; 2755 case IEEE80211_IOC_MESH_PR_PATH: 2756 error = copyin(ireq->i_data, tmpproto, sizeof(tmpproto)); 2757 if (error == 0) { 2758 error = mesh_select_proto_path(vap, tmpproto); 2759 if (error == 0) 2760 error = ENETRESET; 2761 } 2762 break; 2763 default: 2764 return ENOSYS; 2765 } 2766 return error; 2767 } 2768 IEEE80211_IOCTL_SET(mesh, mesh_ioctl_set80211); 2769