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/bpf.h> 56 #include <net/if.h> 57 #include <net/if_var.h> 58 #include <net/if_media.h> 59 #include <net/if_llc.h> 60 #include <net/ethernet.h> 61 62 #include <net80211/ieee80211_var.h> 63 #include <net80211/ieee80211_action.h> 64 #ifdef IEEE80211_SUPPORT_SUPERG 65 #include <net80211/ieee80211_superg.h> 66 #endif 67 #include <net80211/ieee80211_input.h> 68 #include <net80211/ieee80211_mesh.h> 69 70 static void mesh_rt_flush_invalid(struct ieee80211vap *); 71 static int mesh_select_proto_path(struct ieee80211vap *, const char *); 72 static int mesh_select_proto_metric(struct ieee80211vap *, const char *); 73 static void mesh_vattach(struct ieee80211vap *); 74 static int mesh_newstate(struct ieee80211vap *, enum ieee80211_state, int); 75 static void mesh_rt_cleanup_cb(void *); 76 static void mesh_gatemode_setup(struct ieee80211vap *); 77 static void mesh_gatemode_cb(void *); 78 static void mesh_linkchange(struct ieee80211_node *, 79 enum ieee80211_mesh_mlstate); 80 static void mesh_checkid(void *, struct ieee80211_node *); 81 static uint32_t mesh_generateid(struct ieee80211vap *); 82 static int mesh_checkpseq(struct ieee80211vap *, 83 const uint8_t [IEEE80211_ADDR_LEN], uint32_t); 84 static void mesh_transmit_to_gate(struct ieee80211vap *, struct mbuf *, 85 struct ieee80211_mesh_route *); 86 static void mesh_forward(struct ieee80211vap *, struct mbuf *, 87 const struct ieee80211_meshcntl *); 88 static int mesh_input(struct ieee80211_node *, struct mbuf *, 89 const struct ieee80211_rx_stats *rxs, int, int); 90 static void mesh_recv_mgmt(struct ieee80211_node *, struct mbuf *, int, 91 const struct ieee80211_rx_stats *rxs, int, int); 92 static void mesh_recv_ctl(struct ieee80211_node *, struct mbuf *, int); 93 static void mesh_peer_timeout_setup(struct ieee80211_node *); 94 static void mesh_peer_timeout_backoff(struct ieee80211_node *); 95 static void mesh_peer_timeout_cb(void *); 96 static __inline void 97 mesh_peer_timeout_stop(struct ieee80211_node *); 98 static int mesh_verify_meshid(struct ieee80211vap *, const uint8_t *); 99 static int mesh_verify_meshconf(struct ieee80211vap *, const uint8_t *); 100 static int mesh_verify_meshpeer(struct ieee80211vap *, uint8_t, 101 const uint8_t *); 102 uint32_t mesh_airtime_calc(struct ieee80211_node *); 103 104 /* 105 * Timeout values come from the specification and are in milliseconds. 106 */ 107 static SYSCTL_NODE(_net_wlan, OID_AUTO, mesh, CTLFLAG_RD, 0, 108 "IEEE 802.11s parameters"); 109 static int ieee80211_mesh_gateint = -1; 110 SYSCTL_PROC(_net_wlan_mesh, OID_AUTO, gateint, CTLTYPE_INT | CTLFLAG_RW, 111 &ieee80211_mesh_gateint, 0, ieee80211_sysctl_msecs_ticks, "I", 112 "mesh gate interval (ms)"); 113 static int ieee80211_mesh_retrytimeout = -1; 114 SYSCTL_PROC(_net_wlan_mesh, OID_AUTO, retrytimeout, CTLTYPE_INT | CTLFLAG_RW, 115 &ieee80211_mesh_retrytimeout, 0, ieee80211_sysctl_msecs_ticks, "I", 116 "Retry timeout (msec)"); 117 static int ieee80211_mesh_holdingtimeout = -1; 118 119 SYSCTL_PROC(_net_wlan_mesh, OID_AUTO, holdingtimeout, CTLTYPE_INT | CTLFLAG_RW, 120 &ieee80211_mesh_holdingtimeout, 0, ieee80211_sysctl_msecs_ticks, "I", 121 "Holding state timeout (msec)"); 122 static int ieee80211_mesh_confirmtimeout = -1; 123 SYSCTL_PROC(_net_wlan_mesh, OID_AUTO, confirmtimeout, CTLTYPE_INT | CTLFLAG_RW, 124 &ieee80211_mesh_confirmtimeout, 0, ieee80211_sysctl_msecs_ticks, "I", 125 "Confirm state timeout (msec)"); 126 static int ieee80211_mesh_backofftimeout = -1; 127 SYSCTL_PROC(_net_wlan_mesh, OID_AUTO, backofftimeout, CTLTYPE_INT | CTLFLAG_RW, 128 &ieee80211_mesh_backofftimeout, 0, ieee80211_sysctl_msecs_ticks, "I", 129 "Backoff timeout (msec). This is to throutles peering forever when " 130 "not receiving answer or is rejected by a neighbor"); 131 static int ieee80211_mesh_maxretries = 2; 132 SYSCTL_INT(_net_wlan_mesh, OID_AUTO, maxretries, CTLFLAG_RW, 133 &ieee80211_mesh_maxretries, 0, 134 "Maximum retries during peer link establishment"); 135 static int ieee80211_mesh_maxholding = 2; 136 SYSCTL_INT(_net_wlan_mesh, OID_AUTO, maxholding, CTLFLAG_RW, 137 &ieee80211_mesh_maxholding, 0, 138 "Maximum times we are allowed to transition to HOLDING state before " 139 "backinoff during peer link establishment"); 140 141 static const uint8_t broadcastaddr[IEEE80211_ADDR_LEN] = 142 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; 143 144 static ieee80211_recv_action_func mesh_recv_action_meshpeering_open; 145 static ieee80211_recv_action_func mesh_recv_action_meshpeering_confirm; 146 static ieee80211_recv_action_func mesh_recv_action_meshpeering_close; 147 static ieee80211_recv_action_func mesh_recv_action_meshlmetric; 148 static ieee80211_recv_action_func mesh_recv_action_meshgate; 149 150 static ieee80211_send_action_func mesh_send_action_meshpeering_open; 151 static ieee80211_send_action_func mesh_send_action_meshpeering_confirm; 152 static ieee80211_send_action_func mesh_send_action_meshpeering_close; 153 static ieee80211_send_action_func mesh_send_action_meshlmetric; 154 static ieee80211_send_action_func mesh_send_action_meshgate; 155 156 static const struct ieee80211_mesh_proto_metric mesh_metric_airtime = { 157 .mpm_descr = "AIRTIME", 158 .mpm_ie = IEEE80211_MESHCONF_METRIC_AIRTIME, 159 .mpm_metric = mesh_airtime_calc, 160 }; 161 162 static struct ieee80211_mesh_proto_path mesh_proto_paths[4]; 163 static struct ieee80211_mesh_proto_metric mesh_proto_metrics[4]; 164 165 MALLOC_DEFINE(M_80211_MESH_PREQ, "80211preq", "802.11 MESH Path Request frame"); 166 MALLOC_DEFINE(M_80211_MESH_PREP, "80211prep", "802.11 MESH Path Reply frame"); 167 MALLOC_DEFINE(M_80211_MESH_PERR, "80211perr", "802.11 MESH Path Error frame"); 168 169 /* The longer one of the lifetime should be stored as new lifetime */ 170 #define MESH_ROUTE_LIFETIME_MAX(a, b) (a > b ? a : b) 171 172 MALLOC_DEFINE(M_80211_MESH_RT, "80211mesh_rt", "802.11s routing table"); 173 MALLOC_DEFINE(M_80211_MESH_GT_RT, "80211mesh_gt", "802.11s known gates table"); 174 175 /* 176 * Helper functions to manipulate the Mesh routing table. 177 */ 178 179 static struct ieee80211_mesh_route * 180 mesh_rt_find_locked(struct ieee80211_mesh_state *ms, 181 const uint8_t dest[IEEE80211_ADDR_LEN]) 182 { 183 struct ieee80211_mesh_route *rt; 184 185 MESH_RT_LOCK_ASSERT(ms); 186 187 TAILQ_FOREACH(rt, &ms->ms_routes, rt_next) { 188 if (IEEE80211_ADDR_EQ(dest, rt->rt_dest)) 189 return rt; 190 } 191 return NULL; 192 } 193 194 static struct ieee80211_mesh_route * 195 mesh_rt_add_locked(struct ieee80211vap *vap, 196 const uint8_t dest[IEEE80211_ADDR_LEN]) 197 { 198 struct ieee80211_mesh_state *ms = vap->iv_mesh; 199 struct ieee80211_mesh_route *rt; 200 201 KASSERT(!IEEE80211_ADDR_EQ(broadcastaddr, dest), 202 ("%s: adding broadcast to the routing table", __func__)); 203 204 MESH_RT_LOCK_ASSERT(ms); 205 206 rt = IEEE80211_MALLOC(ALIGN(sizeof(struct ieee80211_mesh_route)) + 207 ms->ms_ppath->mpp_privlen, M_80211_MESH_RT, 208 IEEE80211_M_NOWAIT | IEEE80211_M_ZERO); 209 if (rt != NULL) { 210 rt->rt_vap = vap; 211 IEEE80211_ADDR_COPY(rt->rt_dest, dest); 212 rt->rt_priv = (void *)ALIGN(&rt[1]); 213 MESH_RT_ENTRY_LOCK_INIT(rt, "MBSS_RT"); 214 callout_init(&rt->rt_discovery, 1); 215 rt->rt_updtime = ticks; /* create time */ 216 TAILQ_INSERT_TAIL(&ms->ms_routes, rt, rt_next); 217 } 218 return rt; 219 } 220 221 struct ieee80211_mesh_route * 222 ieee80211_mesh_rt_find(struct ieee80211vap *vap, 223 const uint8_t dest[IEEE80211_ADDR_LEN]) 224 { 225 struct ieee80211_mesh_state *ms = vap->iv_mesh; 226 struct ieee80211_mesh_route *rt; 227 228 MESH_RT_LOCK(ms); 229 rt = mesh_rt_find_locked(ms, dest); 230 MESH_RT_UNLOCK(ms); 231 return rt; 232 } 233 234 struct ieee80211_mesh_route * 235 ieee80211_mesh_rt_add(struct ieee80211vap *vap, 236 const uint8_t dest[IEEE80211_ADDR_LEN]) 237 { 238 struct ieee80211_mesh_state *ms = vap->iv_mesh; 239 struct ieee80211_mesh_route *rt; 240 241 KASSERT(ieee80211_mesh_rt_find(vap, dest) == NULL, 242 ("%s: duplicate entry in the routing table", __func__)); 243 KASSERT(!IEEE80211_ADDR_EQ(vap->iv_myaddr, dest), 244 ("%s: adding self to the routing table", __func__)); 245 246 MESH_RT_LOCK(ms); 247 rt = mesh_rt_add_locked(vap, dest); 248 MESH_RT_UNLOCK(ms); 249 return rt; 250 } 251 252 /* 253 * Update the route lifetime and returns the updated lifetime. 254 * If new_lifetime is zero and route is timedout it will be invalidated. 255 * new_lifetime is in msec 256 */ 257 int 258 ieee80211_mesh_rt_update(struct ieee80211_mesh_route *rt, int new_lifetime) 259 { 260 int timesince, now; 261 uint32_t lifetime = 0; 262 263 KASSERT(rt != NULL, ("route is NULL")); 264 265 now = ticks; 266 MESH_RT_ENTRY_LOCK(rt); 267 268 /* dont clobber a proxy entry gated by us */ 269 if (rt->rt_flags & IEEE80211_MESHRT_FLAGS_PROXY && rt->rt_nhops == 0) { 270 MESH_RT_ENTRY_UNLOCK(rt); 271 return rt->rt_lifetime; 272 } 273 274 timesince = ticks_to_msecs(now - rt->rt_updtime); 275 rt->rt_updtime = now; 276 if (timesince >= rt->rt_lifetime) { 277 if (new_lifetime != 0) { 278 rt->rt_lifetime = new_lifetime; 279 } 280 else { 281 rt->rt_flags &= ~IEEE80211_MESHRT_FLAGS_VALID; 282 rt->rt_lifetime = 0; 283 } 284 } else { 285 /* update what is left of lifetime */ 286 rt->rt_lifetime = rt->rt_lifetime - timesince; 287 rt->rt_lifetime = MESH_ROUTE_LIFETIME_MAX( 288 new_lifetime, rt->rt_lifetime); 289 } 290 lifetime = rt->rt_lifetime; 291 MESH_RT_ENTRY_UNLOCK(rt); 292 293 return lifetime; 294 } 295 296 /* 297 * Add a proxy route (as needed) for the specified destination. 298 */ 299 void 300 ieee80211_mesh_proxy_check(struct ieee80211vap *vap, 301 const uint8_t dest[IEEE80211_ADDR_LEN]) 302 { 303 struct ieee80211_mesh_state *ms = vap->iv_mesh; 304 struct ieee80211_mesh_route *rt; 305 306 MESH_RT_LOCK(ms); 307 rt = mesh_rt_find_locked(ms, dest); 308 if (rt == NULL) { 309 rt = mesh_rt_add_locked(vap, dest); 310 if (rt == NULL) { 311 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH, dest, 312 "%s", "unable to add proxy entry"); 313 vap->iv_stats.is_mesh_rtaddfailed++; 314 } else { 315 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH, dest, 316 "%s", "add proxy entry"); 317 IEEE80211_ADDR_COPY(rt->rt_mesh_gate, vap->iv_myaddr); 318 IEEE80211_ADDR_COPY(rt->rt_nexthop, vap->iv_myaddr); 319 rt->rt_flags |= IEEE80211_MESHRT_FLAGS_VALID 320 | IEEE80211_MESHRT_FLAGS_PROXY; 321 } 322 } else if ((rt->rt_flags & IEEE80211_MESHRT_FLAGS_VALID) == 0) { 323 KASSERT(rt->rt_flags & IEEE80211_MESHRT_FLAGS_PROXY, 324 ("no proxy flag for poxy entry")); 325 struct ieee80211com *ic = vap->iv_ic; 326 /* 327 * Fix existing entry created by received frames from 328 * stations that have some memory of dest. We also 329 * flush any frames held on the staging queue; delivering 330 * them is too much trouble right now. 331 */ 332 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH, dest, 333 "%s", "fix proxy entry"); 334 IEEE80211_ADDR_COPY(rt->rt_nexthop, vap->iv_myaddr); 335 rt->rt_flags |= IEEE80211_MESHRT_FLAGS_VALID 336 | IEEE80211_MESHRT_FLAGS_PROXY; 337 /* XXX belongs in hwmp */ 338 ieee80211_ageq_drain_node(&ic->ic_stageq, 339 (void *)(uintptr_t) ieee80211_mac_hash(ic, dest)); 340 /* XXX stat? */ 341 } 342 MESH_RT_UNLOCK(ms); 343 } 344 345 static __inline void 346 mesh_rt_del(struct ieee80211_mesh_state *ms, struct ieee80211_mesh_route *rt) 347 { 348 TAILQ_REMOVE(&ms->ms_routes, rt, rt_next); 349 /* 350 * Grab the lock before destroying it, to be sure no one else 351 * is holding the route. 352 */ 353 MESH_RT_ENTRY_LOCK(rt); 354 callout_drain(&rt->rt_discovery); 355 MESH_RT_ENTRY_LOCK_DESTROY(rt); 356 IEEE80211_FREE(rt, M_80211_MESH_RT); 357 } 358 359 void 360 ieee80211_mesh_rt_del(struct ieee80211vap *vap, 361 const uint8_t dest[IEEE80211_ADDR_LEN]) 362 { 363 struct ieee80211_mesh_state *ms = vap->iv_mesh; 364 struct ieee80211_mesh_route *rt, *next; 365 366 MESH_RT_LOCK(ms); 367 TAILQ_FOREACH_SAFE(rt, &ms->ms_routes, rt_next, next) { 368 if (IEEE80211_ADDR_EQ(rt->rt_dest, dest)) { 369 if (rt->rt_flags & IEEE80211_MESHRT_FLAGS_PROXY) { 370 ms->ms_ppath->mpp_senderror(vap, dest, rt, 371 IEEE80211_REASON_MESH_PERR_NO_PROXY); 372 } else { 373 ms->ms_ppath->mpp_senderror(vap, dest, rt, 374 IEEE80211_REASON_MESH_PERR_DEST_UNREACH); 375 } 376 mesh_rt_del(ms, rt); 377 MESH_RT_UNLOCK(ms); 378 return; 379 } 380 } 381 MESH_RT_UNLOCK(ms); 382 } 383 384 void 385 ieee80211_mesh_rt_flush(struct ieee80211vap *vap) 386 { 387 struct ieee80211_mesh_state *ms = vap->iv_mesh; 388 struct ieee80211_mesh_route *rt, *next; 389 390 if (ms == NULL) 391 return; 392 MESH_RT_LOCK(ms); 393 TAILQ_FOREACH_SAFE(rt, &ms->ms_routes, rt_next, next) 394 mesh_rt_del(ms, rt); 395 MESH_RT_UNLOCK(ms); 396 } 397 398 void 399 ieee80211_mesh_rt_flush_peer(struct ieee80211vap *vap, 400 const uint8_t peer[IEEE80211_ADDR_LEN]) 401 { 402 struct ieee80211_mesh_state *ms = vap->iv_mesh; 403 struct ieee80211_mesh_route *rt, *next; 404 405 MESH_RT_LOCK(ms); 406 TAILQ_FOREACH_SAFE(rt, &ms->ms_routes, rt_next, next) { 407 if (IEEE80211_ADDR_EQ(rt->rt_nexthop, peer)) 408 mesh_rt_del(ms, rt); 409 } 410 MESH_RT_UNLOCK(ms); 411 } 412 413 /* 414 * Flush expired routing entries, i.e. those in invalid state for 415 * some time. 416 */ 417 static void 418 mesh_rt_flush_invalid(struct ieee80211vap *vap) 419 { 420 struct ieee80211_mesh_state *ms = vap->iv_mesh; 421 struct ieee80211_mesh_route *rt, *next; 422 423 if (ms == NULL) 424 return; 425 MESH_RT_LOCK(ms); 426 TAILQ_FOREACH_SAFE(rt, &ms->ms_routes, rt_next, next) { 427 /* Discover paths will be deleted by their own callout */ 428 if (rt->rt_flags & IEEE80211_MESHRT_FLAGS_DISCOVER) 429 continue; 430 ieee80211_mesh_rt_update(rt, 0); 431 if ((rt->rt_flags & IEEE80211_MESHRT_FLAGS_VALID) == 0) 432 mesh_rt_del(ms, rt); 433 } 434 MESH_RT_UNLOCK(ms); 435 } 436 437 #define N(a) (sizeof(a) / sizeof(a[0])) 438 int 439 ieee80211_mesh_register_proto_path(const struct ieee80211_mesh_proto_path *mpp) 440 { 441 int i, firstempty = -1; 442 443 for (i = 0; i < N(mesh_proto_paths); i++) { 444 if (strncmp(mpp->mpp_descr, mesh_proto_paths[i].mpp_descr, 445 IEEE80211_MESH_PROTO_DSZ) == 0) 446 return EEXIST; 447 if (!mesh_proto_paths[i].mpp_active && firstempty == -1) 448 firstempty = i; 449 } 450 if (firstempty < 0) 451 return ENOSPC; 452 memcpy(&mesh_proto_paths[firstempty], mpp, sizeof(*mpp)); 453 mesh_proto_paths[firstempty].mpp_active = 1; 454 return 0; 455 } 456 457 int 458 ieee80211_mesh_register_proto_metric(const struct 459 ieee80211_mesh_proto_metric *mpm) 460 { 461 int i, firstempty = -1; 462 463 for (i = 0; i < N(mesh_proto_metrics); i++) { 464 if (strncmp(mpm->mpm_descr, mesh_proto_metrics[i].mpm_descr, 465 IEEE80211_MESH_PROTO_DSZ) == 0) 466 return EEXIST; 467 if (!mesh_proto_metrics[i].mpm_active && firstempty == -1) 468 firstempty = i; 469 } 470 if (firstempty < 0) 471 return ENOSPC; 472 memcpy(&mesh_proto_metrics[firstempty], mpm, sizeof(*mpm)); 473 mesh_proto_metrics[firstempty].mpm_active = 1; 474 return 0; 475 } 476 477 static int 478 mesh_select_proto_path(struct ieee80211vap *vap, const char *name) 479 { 480 struct ieee80211_mesh_state *ms = vap->iv_mesh; 481 int i; 482 483 for (i = 0; i < N(mesh_proto_paths); i++) { 484 if (strcasecmp(mesh_proto_paths[i].mpp_descr, name) == 0) { 485 ms->ms_ppath = &mesh_proto_paths[i]; 486 return 0; 487 } 488 } 489 return ENOENT; 490 } 491 492 static int 493 mesh_select_proto_metric(struct ieee80211vap *vap, const char *name) 494 { 495 struct ieee80211_mesh_state *ms = vap->iv_mesh; 496 int i; 497 498 for (i = 0; i < N(mesh_proto_metrics); i++) { 499 if (strcasecmp(mesh_proto_metrics[i].mpm_descr, name) == 0) { 500 ms->ms_pmetric = &mesh_proto_metrics[i]; 501 return 0; 502 } 503 } 504 return ENOENT; 505 } 506 #undef N 507 508 static void 509 mesh_gatemode_setup(struct ieee80211vap *vap) 510 { 511 struct ieee80211_mesh_state *ms = vap->iv_mesh; 512 513 /* 514 * NB: When a mesh gate is running as a ROOT it shall 515 * not send out periodic GANNs but instead mark the 516 * mesh gate flag for the corresponding proactive PREQ 517 * and RANN frames. 518 */ 519 if (ms->ms_flags & IEEE80211_MESHFLAGS_ROOT || 520 (ms->ms_flags & IEEE80211_MESHFLAGS_GATE) == 0) { 521 callout_drain(&ms->ms_gatetimer); 522 return ; 523 } 524 callout_reset(&ms->ms_gatetimer, ieee80211_mesh_gateint, 525 mesh_gatemode_cb, vap); 526 } 527 528 static void 529 mesh_gatemode_cb(void *arg) 530 { 531 struct ieee80211vap *vap = (struct ieee80211vap *)arg; 532 struct ieee80211_mesh_state *ms = vap->iv_mesh; 533 struct ieee80211_meshgann_ie gann; 534 535 gann.gann_flags = 0; /* Reserved */ 536 gann.gann_hopcount = 0; 537 gann.gann_ttl = ms->ms_ttl; 538 IEEE80211_ADDR_COPY(gann.gann_addr, vap->iv_myaddr); 539 gann.gann_seq = ms->ms_gateseq++; 540 gann.gann_interval = ieee80211_mesh_gateint; 541 542 IEEE80211_NOTE(vap, IEEE80211_MSG_MESH, vap->iv_bss, 543 "send broadcast GANN (seq %u)", gann.gann_seq); 544 545 ieee80211_send_action(vap->iv_bss, IEEE80211_ACTION_CAT_MESH, 546 IEEE80211_ACTION_MESH_GANN, &gann); 547 mesh_gatemode_setup(vap); 548 } 549 550 static void 551 ieee80211_mesh_init(void) 552 { 553 554 memset(mesh_proto_paths, 0, sizeof(mesh_proto_paths)); 555 memset(mesh_proto_metrics, 0, sizeof(mesh_proto_metrics)); 556 557 /* 558 * Setup mesh parameters that depends on the clock frequency. 559 */ 560 ieee80211_mesh_gateint = msecs_to_ticks(10000); 561 ieee80211_mesh_retrytimeout = msecs_to_ticks(40); 562 ieee80211_mesh_holdingtimeout = msecs_to_ticks(40); 563 ieee80211_mesh_confirmtimeout = msecs_to_ticks(40); 564 ieee80211_mesh_backofftimeout = msecs_to_ticks(5000); 565 566 /* 567 * Register action frame handlers. 568 */ 569 ieee80211_recv_action_register(IEEE80211_ACTION_CAT_SELF_PROT, 570 IEEE80211_ACTION_MESHPEERING_OPEN, 571 mesh_recv_action_meshpeering_open); 572 ieee80211_recv_action_register(IEEE80211_ACTION_CAT_SELF_PROT, 573 IEEE80211_ACTION_MESHPEERING_CONFIRM, 574 mesh_recv_action_meshpeering_confirm); 575 ieee80211_recv_action_register(IEEE80211_ACTION_CAT_SELF_PROT, 576 IEEE80211_ACTION_MESHPEERING_CLOSE, 577 mesh_recv_action_meshpeering_close); 578 ieee80211_recv_action_register(IEEE80211_ACTION_CAT_MESH, 579 IEEE80211_ACTION_MESH_LMETRIC, mesh_recv_action_meshlmetric); 580 ieee80211_recv_action_register(IEEE80211_ACTION_CAT_MESH, 581 IEEE80211_ACTION_MESH_GANN, mesh_recv_action_meshgate); 582 583 ieee80211_send_action_register(IEEE80211_ACTION_CAT_SELF_PROT, 584 IEEE80211_ACTION_MESHPEERING_OPEN, 585 mesh_send_action_meshpeering_open); 586 ieee80211_send_action_register(IEEE80211_ACTION_CAT_SELF_PROT, 587 IEEE80211_ACTION_MESHPEERING_CONFIRM, 588 mesh_send_action_meshpeering_confirm); 589 ieee80211_send_action_register(IEEE80211_ACTION_CAT_SELF_PROT, 590 IEEE80211_ACTION_MESHPEERING_CLOSE, 591 mesh_send_action_meshpeering_close); 592 ieee80211_send_action_register(IEEE80211_ACTION_CAT_MESH, 593 IEEE80211_ACTION_MESH_LMETRIC, 594 mesh_send_action_meshlmetric); 595 ieee80211_send_action_register(IEEE80211_ACTION_CAT_MESH, 596 IEEE80211_ACTION_MESH_GANN, 597 mesh_send_action_meshgate); 598 599 /* 600 * Register Airtime Link Metric. 601 */ 602 ieee80211_mesh_register_proto_metric(&mesh_metric_airtime); 603 604 } 605 SYSINIT(wlan_mesh, SI_SUB_DRIVERS, SI_ORDER_FIRST, ieee80211_mesh_init, NULL); 606 607 void 608 ieee80211_mesh_attach(struct ieee80211com *ic) 609 { 610 ic->ic_vattach[IEEE80211_M_MBSS] = mesh_vattach; 611 } 612 613 void 614 ieee80211_mesh_detach(struct ieee80211com *ic) 615 { 616 } 617 618 static void 619 mesh_vdetach_peers(void *arg, struct ieee80211_node *ni) 620 { 621 struct ieee80211com *ic = ni->ni_ic; 622 uint16_t args[3]; 623 624 if (ni->ni_mlstate == IEEE80211_NODE_MESH_ESTABLISHED) { 625 args[0] = ni->ni_mlpid; 626 args[1] = ni->ni_mllid; 627 args[2] = IEEE80211_REASON_PEER_LINK_CANCELED; 628 ieee80211_send_action(ni, 629 IEEE80211_ACTION_CAT_SELF_PROT, 630 IEEE80211_ACTION_MESHPEERING_CLOSE, 631 args); 632 } 633 callout_drain(&ni->ni_mltimer); 634 /* XXX belongs in hwmp */ 635 ieee80211_ageq_drain_node(&ic->ic_stageq, 636 (void *)(uintptr_t) ieee80211_mac_hash(ic, ni->ni_macaddr)); 637 } 638 639 static void 640 mesh_vdetach(struct ieee80211vap *vap) 641 { 642 struct ieee80211_mesh_state *ms = vap->iv_mesh; 643 644 callout_drain(&ms->ms_cleantimer); 645 ieee80211_iterate_nodes(&vap->iv_ic->ic_sta, mesh_vdetach_peers, 646 NULL); 647 ieee80211_mesh_rt_flush(vap); 648 MESH_RT_LOCK_DESTROY(ms); 649 ms->ms_ppath->mpp_vdetach(vap); 650 IEEE80211_FREE(vap->iv_mesh, M_80211_VAP); 651 vap->iv_mesh = NULL; 652 } 653 654 static void 655 mesh_vattach(struct ieee80211vap *vap) 656 { 657 struct ieee80211_mesh_state *ms; 658 vap->iv_newstate = mesh_newstate; 659 vap->iv_input = mesh_input; 660 vap->iv_opdetach = mesh_vdetach; 661 vap->iv_recv_mgmt = mesh_recv_mgmt; 662 vap->iv_recv_ctl = mesh_recv_ctl; 663 ms = IEEE80211_MALLOC(sizeof(struct ieee80211_mesh_state), M_80211_VAP, 664 IEEE80211_M_NOWAIT | IEEE80211_M_ZERO); 665 if (ms == NULL) { 666 printf("%s: couldn't alloc MBSS state\n", __func__); 667 return; 668 } 669 vap->iv_mesh = ms; 670 ms->ms_seq = 0; 671 ms->ms_flags = (IEEE80211_MESHFLAGS_AP | IEEE80211_MESHFLAGS_FWD); 672 ms->ms_ttl = IEEE80211_MESH_DEFAULT_TTL; 673 TAILQ_INIT(&ms->ms_known_gates); 674 TAILQ_INIT(&ms->ms_routes); 675 MESH_RT_LOCK_INIT(ms, "MBSS"); 676 callout_init(&ms->ms_cleantimer, 1); 677 callout_init(&ms->ms_gatetimer, 1); 678 ms->ms_gateseq = 0; 679 mesh_select_proto_metric(vap, "AIRTIME"); 680 KASSERT(ms->ms_pmetric, ("ms_pmetric == NULL")); 681 mesh_select_proto_path(vap, "HWMP"); 682 KASSERT(ms->ms_ppath, ("ms_ppath == NULL")); 683 ms->ms_ppath->mpp_vattach(vap); 684 } 685 686 /* 687 * IEEE80211_M_MBSS vap state machine handler. 688 */ 689 static int 690 mesh_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg) 691 { 692 struct ieee80211_mesh_state *ms = vap->iv_mesh; 693 struct ieee80211com *ic = vap->iv_ic; 694 struct ieee80211_node *ni; 695 enum ieee80211_state ostate; 696 697 IEEE80211_LOCK_ASSERT(ic); 698 699 ostate = vap->iv_state; 700 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE, "%s: %s -> %s (%d)\n", 701 __func__, ieee80211_state_name[ostate], 702 ieee80211_state_name[nstate], arg); 703 vap->iv_state = nstate; /* state transition */ 704 if (ostate != IEEE80211_S_SCAN) 705 ieee80211_cancel_scan(vap); /* background scan */ 706 ni = vap->iv_bss; /* NB: no reference held */ 707 if (nstate != IEEE80211_S_RUN && ostate == IEEE80211_S_RUN) { 708 callout_drain(&ms->ms_cleantimer); 709 callout_drain(&ms->ms_gatetimer); 710 } 711 switch (nstate) { 712 case IEEE80211_S_INIT: 713 switch (ostate) { 714 case IEEE80211_S_SCAN: 715 ieee80211_cancel_scan(vap); 716 break; 717 case IEEE80211_S_CAC: 718 ieee80211_dfs_cac_stop(vap); 719 break; 720 case IEEE80211_S_RUN: 721 ieee80211_iterate_nodes(&ic->ic_sta, 722 mesh_vdetach_peers, NULL); 723 break; 724 default: 725 break; 726 } 727 if (ostate != IEEE80211_S_INIT) { 728 /* NB: optimize INIT -> INIT case */ 729 ieee80211_reset_bss(vap); 730 ieee80211_mesh_rt_flush(vap); 731 } 732 break; 733 case IEEE80211_S_SCAN: 734 switch (ostate) { 735 case IEEE80211_S_INIT: 736 if (vap->iv_des_chan != IEEE80211_CHAN_ANYC && 737 !IEEE80211_IS_CHAN_RADAR(vap->iv_des_chan) && 738 ms->ms_idlen != 0) { 739 /* 740 * Already have a channel and a mesh ID; bypass 741 * the scan and startup immediately. 742 */ 743 ieee80211_create_ibss(vap, vap->iv_des_chan); 744 break; 745 } 746 /* 747 * Initiate a scan. We can come here as a result 748 * of an IEEE80211_IOC_SCAN_REQ too in which case 749 * the vap will be marked with IEEE80211_FEXT_SCANREQ 750 * and the scan request parameters will be present 751 * in iv_scanreq. Otherwise we do the default. 752 */ 753 if (vap->iv_flags_ext & IEEE80211_FEXT_SCANREQ) { 754 ieee80211_check_scan(vap, 755 vap->iv_scanreq_flags, 756 vap->iv_scanreq_duration, 757 vap->iv_scanreq_mindwell, 758 vap->iv_scanreq_maxdwell, 759 vap->iv_scanreq_nssid, vap->iv_scanreq_ssid); 760 vap->iv_flags_ext &= ~IEEE80211_FEXT_SCANREQ; 761 } else 762 ieee80211_check_scan_current(vap); 763 break; 764 default: 765 break; 766 } 767 break; 768 case IEEE80211_S_CAC: 769 /* 770 * Start CAC on a DFS channel. We come here when starting 771 * a bss on a DFS channel (see ieee80211_create_ibss). 772 */ 773 ieee80211_dfs_cac_start(vap); 774 break; 775 case IEEE80211_S_RUN: 776 switch (ostate) { 777 case IEEE80211_S_INIT: 778 /* 779 * Already have a channel; bypass the 780 * scan and startup immediately. 781 * Note that ieee80211_create_ibss will call 782 * back to do a RUN->RUN state change. 783 */ 784 ieee80211_create_ibss(vap, 785 ieee80211_ht_adjust_channel(ic, 786 ic->ic_curchan, vap->iv_flags_ht)); 787 /* NB: iv_bss is changed on return */ 788 break; 789 case IEEE80211_S_CAC: 790 /* 791 * NB: This is the normal state change when CAC 792 * expires and no radar was detected; no need to 793 * clear the CAC timer as it's already expired. 794 */ 795 /* fall thru... */ 796 case IEEE80211_S_CSA: 797 #if 0 798 /* 799 * Shorten inactivity timer of associated stations 800 * to weed out sta's that don't follow a CSA. 801 */ 802 ieee80211_iterate_nodes(&ic->ic_sta, sta_csa, vap); 803 #endif 804 /* 805 * Update bss node channel to reflect where 806 * we landed after CSA. 807 */ 808 ieee80211_node_set_chan(vap->iv_bss, 809 ieee80211_ht_adjust_channel(ic, ic->ic_curchan, 810 ieee80211_htchanflags(vap->iv_bss->ni_chan))); 811 /* XXX bypass debug msgs */ 812 break; 813 case IEEE80211_S_SCAN: 814 case IEEE80211_S_RUN: 815 #ifdef IEEE80211_DEBUG 816 if (ieee80211_msg_debug(vap)) { 817 struct ieee80211_node *ni = vap->iv_bss; 818 ieee80211_note(vap, 819 "synchronized with %s meshid ", 820 ether_sprintf(ni->ni_meshid)); 821 ieee80211_print_essid(ni->ni_meshid, 822 ni->ni_meshidlen); 823 /* XXX MCS/HT */ 824 printf(" channel %d\n", 825 ieee80211_chan2ieee(ic, ic->ic_curchan)); 826 } 827 #endif 828 break; 829 default: 830 break; 831 } 832 ieee80211_node_authorize(vap->iv_bss); 833 callout_reset(&ms->ms_cleantimer, ms->ms_ppath->mpp_inact, 834 mesh_rt_cleanup_cb, vap); 835 mesh_gatemode_setup(vap); 836 break; 837 default: 838 break; 839 } 840 /* NB: ostate not nstate */ 841 ms->ms_ppath->mpp_newstate(vap, ostate, arg); 842 return 0; 843 } 844 845 static void 846 mesh_rt_cleanup_cb(void *arg) 847 { 848 struct ieee80211vap *vap = arg; 849 struct ieee80211_mesh_state *ms = vap->iv_mesh; 850 851 mesh_rt_flush_invalid(vap); 852 callout_reset(&ms->ms_cleantimer, ms->ms_ppath->mpp_inact, 853 mesh_rt_cleanup_cb, vap); 854 } 855 856 /* 857 * Mark a mesh STA as gate and return a pointer to it. 858 * If this is first time, we create a new gate route. 859 * Always update the path route to this mesh gate. 860 */ 861 struct ieee80211_mesh_gate_route * 862 ieee80211_mesh_mark_gate(struct ieee80211vap *vap, const uint8_t *addr, 863 struct ieee80211_mesh_route *rt) 864 { 865 struct ieee80211_mesh_state *ms = vap->iv_mesh; 866 struct ieee80211_mesh_gate_route *gr = NULL, *next; 867 int found = 0; 868 869 MESH_RT_LOCK(ms); 870 TAILQ_FOREACH_SAFE(gr, &ms->ms_known_gates, gr_next, next) { 871 if (IEEE80211_ADDR_EQ(gr->gr_addr, addr)) { 872 found = 1; 873 break; 874 } 875 } 876 877 if (!found) { 878 /* New mesh gate add it to known table. */ 879 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH, addr, 880 "%s", "stored new gate information from pro-PREQ."); 881 gr = IEEE80211_MALLOC(ALIGN(sizeof(struct ieee80211_mesh_gate_route)), 882 M_80211_MESH_GT_RT, 883 IEEE80211_M_NOWAIT | IEEE80211_M_ZERO); 884 IEEE80211_ADDR_COPY(gr->gr_addr, addr); 885 TAILQ_INSERT_TAIL(&ms->ms_known_gates, gr, gr_next); 886 } 887 gr->gr_route = rt; 888 /* TODO: link from path route to gate route */ 889 MESH_RT_UNLOCK(ms); 890 891 return gr; 892 } 893 894 895 /* 896 * Helper function to note the Mesh Peer Link FSM change. 897 */ 898 static void 899 mesh_linkchange(struct ieee80211_node *ni, enum ieee80211_mesh_mlstate state) 900 { 901 struct ieee80211vap *vap = ni->ni_vap; 902 struct ieee80211_mesh_state *ms = vap->iv_mesh; 903 #ifdef IEEE80211_DEBUG 904 static const char *meshlinkstates[] = { 905 [IEEE80211_NODE_MESH_IDLE] = "IDLE", 906 [IEEE80211_NODE_MESH_OPENSNT] = "OPEN SENT", 907 [IEEE80211_NODE_MESH_OPENRCV] = "OPEN RECEIVED", 908 [IEEE80211_NODE_MESH_CONFIRMRCV] = "CONFIRM RECEIVED", 909 [IEEE80211_NODE_MESH_ESTABLISHED] = "ESTABLISHED", 910 [IEEE80211_NODE_MESH_HOLDING] = "HOLDING" 911 }; 912 #endif 913 IEEE80211_NOTE(vap, IEEE80211_MSG_MESH, 914 ni, "peer link: %s -> %s", 915 meshlinkstates[ni->ni_mlstate], meshlinkstates[state]); 916 917 /* track neighbor count */ 918 if (state == IEEE80211_NODE_MESH_ESTABLISHED && 919 ni->ni_mlstate != IEEE80211_NODE_MESH_ESTABLISHED) { 920 KASSERT(ms->ms_neighbors < 65535, ("neighbor count overflow")); 921 ms->ms_neighbors++; 922 ieee80211_beacon_notify(vap, IEEE80211_BEACON_MESHCONF); 923 } else if (ni->ni_mlstate == IEEE80211_NODE_MESH_ESTABLISHED && 924 state != IEEE80211_NODE_MESH_ESTABLISHED) { 925 KASSERT(ms->ms_neighbors > 0, ("neighbor count 0")); 926 ms->ms_neighbors--; 927 ieee80211_beacon_notify(vap, IEEE80211_BEACON_MESHCONF); 928 } 929 ni->ni_mlstate = state; 930 switch (state) { 931 case IEEE80211_NODE_MESH_HOLDING: 932 ms->ms_ppath->mpp_peerdown(ni); 933 break; 934 case IEEE80211_NODE_MESH_ESTABLISHED: 935 ieee80211_mesh_discover(vap, ni->ni_macaddr, NULL); 936 break; 937 default: 938 break; 939 } 940 } 941 942 /* 943 * Helper function to generate a unique local ID required for mesh 944 * peer establishment. 945 */ 946 static void 947 mesh_checkid(void *arg, struct ieee80211_node *ni) 948 { 949 uint16_t *r = arg; 950 951 if (*r == ni->ni_mllid) 952 *(uint16_t *)arg = 0; 953 } 954 955 static uint32_t 956 mesh_generateid(struct ieee80211vap *vap) 957 { 958 int maxiter = 4; 959 uint16_t r; 960 961 do { 962 get_random_bytes(&r, 2); 963 ieee80211_iterate_nodes(&vap->iv_ic->ic_sta, mesh_checkid, &r); 964 maxiter--; 965 } while (r == 0 && maxiter > 0); 966 return r; 967 } 968 969 /* 970 * Verifies if we already received this packet by checking its 971 * sequence number. 972 * Returns 0 if the frame is to be accepted, 1 otherwise. 973 */ 974 static int 975 mesh_checkpseq(struct ieee80211vap *vap, 976 const uint8_t source[IEEE80211_ADDR_LEN], uint32_t seq) 977 { 978 struct ieee80211_mesh_route *rt; 979 980 rt = ieee80211_mesh_rt_find(vap, source); 981 if (rt == NULL) { 982 rt = ieee80211_mesh_rt_add(vap, source); 983 if (rt == NULL) { 984 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH, source, 985 "%s", "add mcast route failed"); 986 vap->iv_stats.is_mesh_rtaddfailed++; 987 return 1; 988 } 989 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH, source, 990 "add mcast route, mesh seqno %d", seq); 991 rt->rt_lastmseq = seq; 992 return 0; 993 } 994 if (IEEE80211_MESH_SEQ_GEQ(rt->rt_lastmseq, seq)) { 995 return 1; 996 } else { 997 rt->rt_lastmseq = seq; 998 return 0; 999 } 1000 } 1001 1002 /* 1003 * Iterate the routing table and locate the next hop. 1004 */ 1005 struct ieee80211_node * 1006 ieee80211_mesh_find_txnode(struct ieee80211vap *vap, 1007 const uint8_t dest[IEEE80211_ADDR_LEN]) 1008 { 1009 struct ieee80211_mesh_route *rt; 1010 1011 rt = ieee80211_mesh_rt_find(vap, dest); 1012 if (rt == NULL) 1013 return NULL; 1014 if ((rt->rt_flags & IEEE80211_MESHRT_FLAGS_VALID) == 0) { 1015 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH, dest, 1016 "%s: !valid, flags 0x%x", __func__, rt->rt_flags); 1017 /* XXX stat */ 1018 return NULL; 1019 } 1020 if (rt->rt_flags & IEEE80211_MESHRT_FLAGS_PROXY) { 1021 rt = ieee80211_mesh_rt_find(vap, rt->rt_mesh_gate); 1022 if (rt == NULL) return NULL; 1023 if ((rt->rt_flags & IEEE80211_MESHRT_FLAGS_VALID) == 0) { 1024 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH, dest, 1025 "%s: meshgate !valid, flags 0x%x", __func__, 1026 rt->rt_flags); 1027 /* XXX stat */ 1028 return NULL; 1029 } 1030 } 1031 return ieee80211_find_txnode(vap, rt->rt_nexthop); 1032 } 1033 1034 static void 1035 mesh_transmit_to_gate(struct ieee80211vap *vap, struct mbuf *m, 1036 struct ieee80211_mesh_route *rt_gate) 1037 { 1038 struct ifnet *ifp = vap->iv_ifp; 1039 struct ieee80211_node *ni; 1040 1041 IEEE80211_TX_UNLOCK_ASSERT(vap->iv_ic); 1042 1043 ni = ieee80211_mesh_find_txnode(vap, rt_gate->rt_dest); 1044 if (ni == NULL) { 1045 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); 1046 m_freem(m); 1047 return; 1048 } 1049 1050 /* 1051 * Send through the VAP packet transmit path. 1052 * This consumes the node ref grabbed above and 1053 * the mbuf, regardless of whether there's a problem 1054 * or not. 1055 */ 1056 (void) ieee80211_vap_pkt_send_dest(vap, m, ni); 1057 } 1058 1059 /* 1060 * Forward the queued frames to known valid mesh gates. 1061 * Assume destination to be outside the MBSS (i.e. proxy entry), 1062 * If no valid mesh gates are known silently discard queued frames. 1063 * After transmitting frames to all known valid mesh gates, this route 1064 * will be marked invalid, and a new path discovery will happen in the hopes 1065 * that (at least) one of the mesh gates have a new proxy entry for us to use. 1066 */ 1067 void 1068 ieee80211_mesh_forward_to_gates(struct ieee80211vap *vap, 1069 struct ieee80211_mesh_route *rt_dest) 1070 { 1071 struct ieee80211com *ic = vap->iv_ic; 1072 struct ieee80211_mesh_state *ms = vap->iv_mesh; 1073 struct ieee80211_mesh_route *rt_gate; 1074 struct ieee80211_mesh_gate_route *gr = NULL, *gr_next; 1075 struct mbuf *m, *mcopy, *next; 1076 1077 IEEE80211_TX_UNLOCK_ASSERT(ic); 1078 1079 KASSERT( rt_dest->rt_flags == IEEE80211_MESHRT_FLAGS_DISCOVER, 1080 ("Route is not marked with IEEE80211_MESHRT_FLAGS_DISCOVER")); 1081 1082 /* XXX: send to more than one valid mash gate */ 1083 MESH_RT_LOCK(ms); 1084 1085 m = ieee80211_ageq_remove(&ic->ic_stageq, 1086 (struct ieee80211_node *)(uintptr_t) 1087 ieee80211_mac_hash(ic, rt_dest->rt_dest)); 1088 1089 TAILQ_FOREACH_SAFE(gr, &ms->ms_known_gates, gr_next, gr_next) { 1090 rt_gate = gr->gr_route; 1091 if (rt_gate == NULL) { 1092 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_HWMP, 1093 rt_dest->rt_dest, 1094 "mesh gate with no path %6D", 1095 gr->gr_addr, ":"); 1096 continue; 1097 } 1098 if ((rt_gate->rt_flags & IEEE80211_MESHRT_FLAGS_VALID) == 0) 1099 continue; 1100 KASSERT(rt_gate->rt_flags & IEEE80211_MESHRT_FLAGS_GATE, 1101 ("route not marked as a mesh gate")); 1102 KASSERT((rt_gate->rt_flags & 1103 IEEE80211_MESHRT_FLAGS_PROXY) == 0, 1104 ("found mesh gate that is also marked porxy")); 1105 /* 1106 * convert route to a proxy route gated by the current 1107 * mesh gate, this is needed so encap can built data 1108 * frame with correct address. 1109 */ 1110 rt_dest->rt_flags = IEEE80211_MESHRT_FLAGS_PROXY | 1111 IEEE80211_MESHRT_FLAGS_VALID; 1112 rt_dest->rt_ext_seq = 1; /* random value */ 1113 IEEE80211_ADDR_COPY(rt_dest->rt_mesh_gate, rt_gate->rt_dest); 1114 IEEE80211_ADDR_COPY(rt_dest->rt_nexthop, rt_gate->rt_nexthop); 1115 rt_dest->rt_metric = rt_gate->rt_metric; 1116 rt_dest->rt_nhops = rt_gate->rt_nhops; 1117 ieee80211_mesh_rt_update(rt_dest, ms->ms_ppath->mpp_inact); 1118 MESH_RT_UNLOCK(ms); 1119 /* XXX: lock?? */ 1120 mcopy = m_dup(m, M_NOWAIT); 1121 for (; mcopy != NULL; mcopy = next) { 1122 next = mcopy->m_nextpkt; 1123 mcopy->m_nextpkt = NULL; 1124 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_HWMP, 1125 rt_dest->rt_dest, 1126 "flush queued frame %p len %d", mcopy, 1127 mcopy->m_pkthdr.len); 1128 mesh_transmit_to_gate(vap, mcopy, rt_gate); 1129 } 1130 MESH_RT_LOCK(ms); 1131 } 1132 rt_dest->rt_flags = 0; /* Mark invalid */ 1133 m_freem(m); 1134 MESH_RT_UNLOCK(ms); 1135 } 1136 1137 /* 1138 * Forward the specified frame. 1139 * Decrement the TTL and set TA to our MAC address. 1140 */ 1141 static void 1142 mesh_forward(struct ieee80211vap *vap, struct mbuf *m, 1143 const struct ieee80211_meshcntl *mc) 1144 { 1145 struct ieee80211com *ic = vap->iv_ic; 1146 struct ieee80211_mesh_state *ms = vap->iv_mesh; 1147 struct ifnet *ifp = vap->iv_ifp; 1148 const struct ieee80211_frame *wh = 1149 mtod(m, const struct ieee80211_frame *); 1150 struct mbuf *mcopy; 1151 struct ieee80211_meshcntl *mccopy; 1152 struct ieee80211_frame *whcopy; 1153 struct ieee80211_node *ni; 1154 int err; 1155 1156 /* This is called from the RX path - don't hold this lock */ 1157 IEEE80211_TX_UNLOCK_ASSERT(ic); 1158 1159 /* 1160 * mesh ttl of 1 means we are the last one receving it, 1161 * according to amendment we decrement and then check if 1162 * 0, if so we dont forward. 1163 */ 1164 if (mc->mc_ttl < 1) { 1165 IEEE80211_NOTE_FRAME(vap, IEEE80211_MSG_MESH, wh, 1166 "%s", "frame not fwd'd, ttl 1"); 1167 vap->iv_stats.is_mesh_fwd_ttl++; 1168 return; 1169 } 1170 if (!(ms->ms_flags & IEEE80211_MESHFLAGS_FWD)) { 1171 IEEE80211_NOTE_FRAME(vap, IEEE80211_MSG_MESH, wh, 1172 "%s", "frame not fwd'd, fwding disabled"); 1173 vap->iv_stats.is_mesh_fwd_disabled++; 1174 return; 1175 } 1176 mcopy = m_dup(m, M_NOWAIT); 1177 if (mcopy == NULL) { 1178 IEEE80211_NOTE_FRAME(vap, IEEE80211_MSG_MESH, wh, 1179 "%s", "frame not fwd'd, cannot dup"); 1180 vap->iv_stats.is_mesh_fwd_nobuf++; 1181 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); 1182 return; 1183 } 1184 mcopy = m_pullup(mcopy, ieee80211_hdrspace(ic, wh) + 1185 sizeof(struct ieee80211_meshcntl)); 1186 if (mcopy == NULL) { 1187 IEEE80211_NOTE_FRAME(vap, IEEE80211_MSG_MESH, wh, 1188 "%s", "frame not fwd'd, too short"); 1189 vap->iv_stats.is_mesh_fwd_tooshort++; 1190 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); 1191 m_freem(mcopy); 1192 return; 1193 } 1194 whcopy = mtod(mcopy, struct ieee80211_frame *); 1195 mccopy = (struct ieee80211_meshcntl *) 1196 (mtod(mcopy, uint8_t *) + ieee80211_hdrspace(ic, wh)); 1197 /* XXX clear other bits? */ 1198 whcopy->i_fc[1] &= ~IEEE80211_FC1_RETRY; 1199 IEEE80211_ADDR_COPY(whcopy->i_addr2, vap->iv_myaddr); 1200 if (IEEE80211_IS_MULTICAST(wh->i_addr1)) { 1201 ni = ieee80211_ref_node(vap->iv_bss); 1202 mcopy->m_flags |= M_MCAST; 1203 } else { 1204 ni = ieee80211_mesh_find_txnode(vap, whcopy->i_addr3); 1205 if (ni == NULL) { 1206 /* 1207 * [Optional] any of the following three actions: 1208 * o silently discard 1209 * o trigger a path discovery 1210 * o inform TA that meshDA is unknown. 1211 */ 1212 IEEE80211_NOTE_FRAME(vap, IEEE80211_MSG_MESH, wh, 1213 "%s", "frame not fwd'd, no path"); 1214 ms->ms_ppath->mpp_senderror(vap, whcopy->i_addr3, NULL, 1215 IEEE80211_REASON_MESH_PERR_NO_FI); 1216 vap->iv_stats.is_mesh_fwd_nopath++; 1217 m_freem(mcopy); 1218 return; 1219 } 1220 IEEE80211_ADDR_COPY(whcopy->i_addr1, ni->ni_macaddr); 1221 } 1222 KASSERT(mccopy->mc_ttl > 0, ("%s called with wrong ttl", __func__)); 1223 mccopy->mc_ttl--; 1224 1225 /* XXX calculate priority so drivers can find the tx queue */ 1226 M_WME_SETAC(mcopy, WME_AC_BE); 1227 1228 /* XXX do we know m_nextpkt is NULL? */ 1229 mcopy->m_pkthdr.rcvif = (void *) ni; 1230 1231 /* 1232 * XXX this bypasses all of the VAP TX handling; it passes frames 1233 * directly to the parent interface. 1234 * 1235 * Because of this, there's no TX lock being held as there's no 1236 * encaps state being used. 1237 * 1238 * Doing a direct parent transmit may not be the correct thing 1239 * to do here; we'll have to re-think this soon. 1240 */ 1241 IEEE80211_TX_LOCK(ic); 1242 err = ieee80211_parent_xmitpkt(ic, mcopy); 1243 IEEE80211_TX_UNLOCK(ic); 1244 if (err != 0) { 1245 /* NB: IFQ_HANDOFF reclaims mbuf */ 1246 ieee80211_free_node(ni); 1247 } else { 1248 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1); 1249 } 1250 } 1251 1252 static struct mbuf * 1253 mesh_decap(struct ieee80211vap *vap, struct mbuf *m, int hdrlen, int meshdrlen) 1254 { 1255 #define WHDIR(wh) ((wh)->i_fc[1] & IEEE80211_FC1_DIR_MASK) 1256 #define MC01(mc) ((const struct ieee80211_meshcntl_ae01 *)mc) 1257 uint8_t b[sizeof(struct ieee80211_qosframe_addr4) + 1258 sizeof(struct ieee80211_meshcntl_ae10)]; 1259 const struct ieee80211_qosframe_addr4 *wh; 1260 const struct ieee80211_meshcntl_ae10 *mc; 1261 struct ether_header *eh; 1262 struct llc *llc; 1263 int ae; 1264 1265 if (m->m_len < hdrlen + sizeof(*llc) && 1266 (m = m_pullup(m, hdrlen + sizeof(*llc))) == NULL) { 1267 IEEE80211_DPRINTF(vap, IEEE80211_MSG_ANY, 1268 "discard data frame: %s", "m_pullup failed"); 1269 vap->iv_stats.is_rx_tooshort++; 1270 return NULL; 1271 } 1272 memcpy(b, mtod(m, caddr_t), hdrlen); 1273 wh = (const struct ieee80211_qosframe_addr4 *)&b[0]; 1274 mc = (const struct ieee80211_meshcntl_ae10 *)&b[hdrlen - meshdrlen]; 1275 KASSERT(WHDIR(wh) == IEEE80211_FC1_DIR_FROMDS || 1276 WHDIR(wh) == IEEE80211_FC1_DIR_DSTODS, 1277 ("bogus dir, fc 0x%x:0x%x", wh->i_fc[0], wh->i_fc[1])); 1278 1279 llc = (struct llc *)(mtod(m, caddr_t) + hdrlen); 1280 if (llc->llc_dsap == LLC_SNAP_LSAP && llc->llc_ssap == LLC_SNAP_LSAP && 1281 llc->llc_control == LLC_UI && llc->llc_snap.org_code[0] == 0 && 1282 llc->llc_snap.org_code[1] == 0 && llc->llc_snap.org_code[2] == 0 && 1283 /* NB: preserve AppleTalk frames that have a native SNAP hdr */ 1284 !(llc->llc_snap.ether_type == htons(ETHERTYPE_AARP) || 1285 llc->llc_snap.ether_type == htons(ETHERTYPE_IPX))) { 1286 m_adj(m, hdrlen + sizeof(struct llc) - sizeof(*eh)); 1287 llc = NULL; 1288 } else { 1289 m_adj(m, hdrlen - sizeof(*eh)); 1290 } 1291 eh = mtod(m, struct ether_header *); 1292 ae = mc->mc_flags & IEEE80211_MESH_AE_MASK; 1293 if (WHDIR(wh) == IEEE80211_FC1_DIR_FROMDS) { 1294 IEEE80211_ADDR_COPY(eh->ether_dhost, wh->i_addr1); 1295 if (ae == IEEE80211_MESH_AE_00) { 1296 IEEE80211_ADDR_COPY(eh->ether_shost, wh->i_addr3); 1297 } else if (ae == IEEE80211_MESH_AE_01) { 1298 IEEE80211_ADDR_COPY(eh->ether_shost, 1299 MC01(mc)->mc_addr4); 1300 } else { 1301 IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY, 1302 (const struct ieee80211_frame *)wh, NULL, 1303 "bad AE %d", ae); 1304 vap->iv_stats.is_mesh_badae++; 1305 m_freem(m); 1306 return NULL; 1307 } 1308 } else { 1309 if (ae == IEEE80211_MESH_AE_00) { 1310 IEEE80211_ADDR_COPY(eh->ether_dhost, wh->i_addr3); 1311 IEEE80211_ADDR_COPY(eh->ether_shost, wh->i_addr4); 1312 } else if (ae == IEEE80211_MESH_AE_10) { 1313 IEEE80211_ADDR_COPY(eh->ether_dhost, mc->mc_addr5); 1314 IEEE80211_ADDR_COPY(eh->ether_shost, mc->mc_addr6); 1315 } else { 1316 IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY, 1317 (const struct ieee80211_frame *)wh, NULL, 1318 "bad AE %d", ae); 1319 vap->iv_stats.is_mesh_badae++; 1320 m_freem(m); 1321 return NULL; 1322 } 1323 } 1324 #ifndef __NO_STRICT_ALIGNMENT 1325 if (!ALIGNED_POINTER(mtod(m, caddr_t) + sizeof(*eh), uint32_t)) { 1326 m = ieee80211_realign(vap, m, sizeof(*eh)); 1327 if (m == NULL) 1328 return NULL; 1329 } 1330 #endif /* !__NO_STRICT_ALIGNMENT */ 1331 if (llc != NULL) { 1332 eh = mtod(m, struct ether_header *); 1333 eh->ether_type = htons(m->m_pkthdr.len - sizeof(*eh)); 1334 } 1335 return m; 1336 #undef WDIR 1337 #undef MC01 1338 } 1339 1340 /* 1341 * Return non-zero if the unicast mesh data frame should be processed 1342 * locally. Frames that are not proxy'd have our address, otherwise 1343 * we need to consult the routing table to look for a proxy entry. 1344 */ 1345 static __inline int 1346 mesh_isucastforme(struct ieee80211vap *vap, const struct ieee80211_frame *wh, 1347 const struct ieee80211_meshcntl *mc) 1348 { 1349 int ae = mc->mc_flags & 3; 1350 1351 KASSERT((wh->i_fc[1] & IEEE80211_FC1_DIR_MASK) == IEEE80211_FC1_DIR_DSTODS, 1352 ("bad dir 0x%x:0x%x", wh->i_fc[0], wh->i_fc[1])); 1353 KASSERT(ae == IEEE80211_MESH_AE_00 || ae == IEEE80211_MESH_AE_10, 1354 ("bad AE %d", ae)); 1355 if (ae == IEEE80211_MESH_AE_10) { /* ucast w/ proxy */ 1356 const struct ieee80211_meshcntl_ae10 *mc10 = 1357 (const struct ieee80211_meshcntl_ae10 *) mc; 1358 struct ieee80211_mesh_route *rt = 1359 ieee80211_mesh_rt_find(vap, mc10->mc_addr5); 1360 /* check for proxy route to ourself */ 1361 return (rt != NULL && 1362 (rt->rt_flags & IEEE80211_MESHRT_FLAGS_PROXY)); 1363 } else /* ucast w/o proxy */ 1364 return IEEE80211_ADDR_EQ(wh->i_addr3, vap->iv_myaddr); 1365 } 1366 1367 /* 1368 * Verifies transmitter, updates lifetime, precursor list and forwards data. 1369 * > 0 means we have forwarded data and no need to process locally 1370 * == 0 means we want to process locally (and we may have forwarded data 1371 * < 0 means there was an error and data should be discarded 1372 */ 1373 static int 1374 mesh_recv_indiv_data_to_fwrd(struct ieee80211vap *vap, struct mbuf *m, 1375 struct ieee80211_frame *wh, const struct ieee80211_meshcntl *mc) 1376 { 1377 struct ieee80211_qosframe_addr4 *qwh; 1378 struct ieee80211_mesh_state *ms = vap->iv_mesh; 1379 struct ieee80211_mesh_route *rt_meshda, *rt_meshsa; 1380 1381 /* This is called from the RX path - don't hold this lock */ 1382 IEEE80211_TX_UNLOCK_ASSERT(vap->iv_ic); 1383 1384 qwh = (struct ieee80211_qosframe_addr4 *)wh; 1385 1386 /* 1387 * TODO: 1388 * o verify addr2 is a legitimate transmitter 1389 * o lifetime of precursor of addr3 (addr2) is max(init, curr) 1390 * o lifetime of precursor of addr4 (nexthop) is max(init, curr) 1391 */ 1392 1393 /* set lifetime of addr3 (meshDA) to initial value */ 1394 rt_meshda = ieee80211_mesh_rt_find(vap, qwh->i_addr3); 1395 if (rt_meshda == NULL) { 1396 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH, qwh->i_addr2, 1397 "no route to meshDA(%6D)", qwh->i_addr3, ":"); 1398 /* 1399 * [Optional] any of the following three actions: 1400 * o silently discard [X] 1401 * o trigger a path discovery [ ] 1402 * o inform TA that meshDA is unknown. [ ] 1403 */ 1404 /* XXX: stats */ 1405 return (-1); 1406 } 1407 1408 ieee80211_mesh_rt_update(rt_meshda, ticks_to_msecs( 1409 ms->ms_ppath->mpp_inact)); 1410 1411 /* set lifetime of addr4 (meshSA) to initial value */ 1412 rt_meshsa = ieee80211_mesh_rt_find(vap, qwh->i_addr4); 1413 KASSERT(rt_meshsa != NULL, ("no route")); 1414 ieee80211_mesh_rt_update(rt_meshsa, ticks_to_msecs( 1415 ms->ms_ppath->mpp_inact)); 1416 1417 mesh_forward(vap, m, mc); 1418 return (1); /* dont process locally */ 1419 } 1420 1421 /* 1422 * Verifies transmitter, updates lifetime, precursor list and process data 1423 * locally, if data is proxy with AE = 10 it could mean data should go 1424 * on another mesh path or data should be forwarded to the DS. 1425 * 1426 * > 0 means we have forwarded data and no need to process locally 1427 * == 0 means we want to process locally (and we may have forwarded data 1428 * < 0 means there was an error and data should be discarded 1429 */ 1430 static int 1431 mesh_recv_indiv_data_to_me(struct ieee80211vap *vap, struct mbuf *m, 1432 struct ieee80211_frame *wh, const struct ieee80211_meshcntl *mc) 1433 { 1434 struct ieee80211_qosframe_addr4 *qwh; 1435 const struct ieee80211_meshcntl_ae10 *mc10; 1436 struct ieee80211_mesh_state *ms = vap->iv_mesh; 1437 struct ieee80211_mesh_route *rt; 1438 int ae; 1439 1440 /* This is called from the RX path - don't hold this lock */ 1441 IEEE80211_TX_UNLOCK_ASSERT(vap->iv_ic); 1442 1443 qwh = (struct ieee80211_qosframe_addr4 *)wh; 1444 mc10 = (const struct ieee80211_meshcntl_ae10 *)mc; 1445 1446 /* 1447 * TODO: 1448 * o verify addr2 is a legitimate transmitter 1449 * o lifetime of precursor entry is max(init, curr) 1450 */ 1451 1452 /* set lifetime of addr4 (meshSA) to initial value */ 1453 rt = ieee80211_mesh_rt_find(vap, qwh->i_addr4); 1454 KASSERT(rt != NULL, ("no route")); 1455 ieee80211_mesh_rt_update(rt, ticks_to_msecs(ms->ms_ppath->mpp_inact)); 1456 rt = NULL; 1457 1458 ae = mc10->mc_flags & IEEE80211_MESH_AE_MASK; 1459 KASSERT(ae == IEEE80211_MESH_AE_00 || 1460 ae == IEEE80211_MESH_AE_10, ("bad AE %d", ae)); 1461 if (ae == IEEE80211_MESH_AE_10) { 1462 if (IEEE80211_ADDR_EQ(mc10->mc_addr5, qwh->i_addr3)) { 1463 return (0); /* process locally */ 1464 } 1465 1466 rt = ieee80211_mesh_rt_find(vap, mc10->mc_addr5); 1467 if (rt != NULL && 1468 (rt->rt_flags & IEEE80211_MESHRT_FLAGS_VALID) && 1469 (rt->rt_flags & IEEE80211_MESHRT_FLAGS_PROXY) == 0) { 1470 /* 1471 * Forward on another mesh-path, according to 1472 * amendment as specified in 9.32.4.1 1473 */ 1474 IEEE80211_ADDR_COPY(qwh->i_addr3, mc10->mc_addr5); 1475 mesh_forward(vap, m, 1476 (const struct ieee80211_meshcntl *)mc10); 1477 return (1); /* dont process locally */ 1478 } 1479 /* 1480 * All other cases: forward of MSDUs from the MBSS to DS indiv. 1481 * addressed according to 13.11.3.2. 1482 */ 1483 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_OUTPUT, qwh->i_addr2, 1484 "forward frame to DS, SA(%6D) DA(%6D)", 1485 mc10->mc_addr6, ":", mc10->mc_addr5, ":"); 1486 } 1487 return (0); /* process locally */ 1488 } 1489 1490 /* 1491 * Try to forward the group addressed data on to other mesh STAs, and 1492 * also to the DS. 1493 * 1494 * > 0 means we have forwarded data and no need to process locally 1495 * == 0 means we want to process locally (and we may have forwarded data 1496 * < 0 means there was an error and data should be discarded 1497 */ 1498 static int 1499 mesh_recv_group_data(struct ieee80211vap *vap, struct mbuf *m, 1500 struct ieee80211_frame *wh, const struct ieee80211_meshcntl *mc) 1501 { 1502 #define MC01(mc) ((const struct ieee80211_meshcntl_ae01 *)mc) 1503 struct ieee80211_mesh_state *ms = vap->iv_mesh; 1504 1505 /* This is called from the RX path - don't hold this lock */ 1506 IEEE80211_TX_UNLOCK_ASSERT(vap->iv_ic); 1507 1508 mesh_forward(vap, m, mc); 1509 1510 if(mc->mc_ttl > 0) { 1511 if (mc->mc_flags & IEEE80211_MESH_AE_01) { 1512 /* 1513 * Forward of MSDUs from the MBSS to DS group addressed 1514 * (according to 13.11.3.2) 1515 * This happens by delivering the packet, and a bridge 1516 * will sent it on another port member. 1517 */ 1518 if (ms->ms_flags & IEEE80211_MESHFLAGS_GATE && 1519 ms->ms_flags & IEEE80211_MESHFLAGS_FWD) 1520 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH, 1521 MC01(mc)->mc_addr4, "%s", 1522 "forward from MBSS to the DS"); 1523 } 1524 } 1525 return (0); /* process locally */ 1526 #undef MC01 1527 } 1528 1529 static int 1530 mesh_input(struct ieee80211_node *ni, struct mbuf *m, 1531 const struct ieee80211_rx_stats *rxs, int rssi, int nf) 1532 { 1533 #define HAS_SEQ(type) ((type & 0x4) == 0) 1534 #define MC01(mc) ((const struct ieee80211_meshcntl_ae01 *)mc) 1535 #define MC10(mc) ((const struct ieee80211_meshcntl_ae10 *)mc) 1536 struct ieee80211vap *vap = ni->ni_vap; 1537 struct ieee80211com *ic = ni->ni_ic; 1538 struct ifnet *ifp = vap->iv_ifp; 1539 struct ieee80211_frame *wh; 1540 const struct ieee80211_meshcntl *mc; 1541 int hdrspace, meshdrlen, need_tap, error; 1542 uint8_t dir, type, subtype, ae; 1543 uint32_t seq; 1544 const uint8_t *addr; 1545 uint8_t qos[2]; 1546 ieee80211_seq rxseq; 1547 1548 KASSERT(ni != NULL, ("null node")); 1549 ni->ni_inact = ni->ni_inact_reload; 1550 1551 need_tap = 1; /* mbuf need to be tapped. */ 1552 type = -1; /* undefined */ 1553 1554 /* This is called from the RX path - don't hold this lock */ 1555 IEEE80211_TX_UNLOCK_ASSERT(ic); 1556 1557 if (m->m_pkthdr.len < sizeof(struct ieee80211_frame_min)) { 1558 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY, 1559 ni->ni_macaddr, NULL, 1560 "too short (1): len %u", m->m_pkthdr.len); 1561 vap->iv_stats.is_rx_tooshort++; 1562 goto out; 1563 } 1564 /* 1565 * Bit of a cheat here, we use a pointer for a 3-address 1566 * frame format but don't reference fields past outside 1567 * ieee80211_frame_min w/o first validating the data is 1568 * present. 1569 */ 1570 wh = mtod(m, struct ieee80211_frame *); 1571 1572 if ((wh->i_fc[0] & IEEE80211_FC0_VERSION_MASK) != 1573 IEEE80211_FC0_VERSION_0) { 1574 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY, 1575 ni->ni_macaddr, NULL, "wrong version %x", wh->i_fc[0]); 1576 vap->iv_stats.is_rx_badversion++; 1577 goto err; 1578 } 1579 dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK; 1580 type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK; 1581 subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK; 1582 if ((ic->ic_flags & IEEE80211_F_SCAN) == 0) { 1583 IEEE80211_RSSI_LPF(ni->ni_avgrssi, rssi); 1584 ni->ni_noise = nf; 1585 if (HAS_SEQ(type)) { 1586 uint8_t tid = ieee80211_gettid(wh); 1587 1588 if (IEEE80211_QOS_HAS_SEQ(wh) && 1589 TID_TO_WME_AC(tid) >= WME_AC_VI) 1590 ic->ic_wme.wme_hipri_traffic++; 1591 rxseq = le16toh(*(uint16_t *)wh->i_seq); 1592 if (! ieee80211_check_rxseq(ni, wh)) { 1593 /* duplicate, discard */ 1594 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT, 1595 wh->i_addr1, "duplicate", 1596 "seqno <%u,%u> fragno <%u,%u> tid %u", 1597 rxseq >> IEEE80211_SEQ_SEQ_SHIFT, 1598 ni->ni_rxseqs[tid] >> 1599 IEEE80211_SEQ_SEQ_SHIFT, 1600 rxseq & IEEE80211_SEQ_FRAG_MASK, 1601 ni->ni_rxseqs[tid] & 1602 IEEE80211_SEQ_FRAG_MASK, 1603 tid); 1604 vap->iv_stats.is_rx_dup++; 1605 IEEE80211_NODE_STAT(ni, rx_dup); 1606 goto out; 1607 } 1608 ni->ni_rxseqs[tid] = rxseq; 1609 } 1610 } 1611 #ifdef IEEE80211_DEBUG 1612 /* 1613 * It's easier, but too expensive, to simulate different mesh 1614 * topologies by consulting the ACL policy very early, so do this 1615 * only under DEBUG. 1616 * 1617 * NB: this check is also done upon peering link initiation. 1618 */ 1619 if (vap->iv_acl != NULL && !vap->iv_acl->iac_check(vap, wh)) { 1620 IEEE80211_DISCARD(vap, IEEE80211_MSG_ACL, 1621 wh, NULL, "%s", "disallowed by ACL"); 1622 vap->iv_stats.is_rx_acl++; 1623 goto out; 1624 } 1625 #endif 1626 switch (type) { 1627 case IEEE80211_FC0_TYPE_DATA: 1628 if (ni == vap->iv_bss) 1629 goto out; 1630 if (ni->ni_mlstate != IEEE80211_NODE_MESH_ESTABLISHED) { 1631 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_MESH, 1632 ni->ni_macaddr, NULL, 1633 "peer link not yet established (%d)", 1634 ni->ni_mlstate); 1635 vap->iv_stats.is_mesh_nolink++; 1636 goto out; 1637 } 1638 if (dir != IEEE80211_FC1_DIR_FROMDS && 1639 dir != IEEE80211_FC1_DIR_DSTODS) { 1640 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT, 1641 wh, "data", "incorrect dir 0x%x", dir); 1642 vap->iv_stats.is_rx_wrongdir++; 1643 goto err; 1644 } 1645 1646 /* All Mesh data frames are QoS subtype */ 1647 if (!HAS_SEQ(type)) { 1648 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT, 1649 wh, "data", "incorrect subtype 0x%x", subtype); 1650 vap->iv_stats.is_rx_badsubtype++; 1651 goto err; 1652 } 1653 1654 /* 1655 * Next up, any fragmentation. 1656 * XXX: we defrag before we even try to forward, 1657 * Mesh Control field is not present in sub-sequent 1658 * fragmented frames. This is in contrast to Draft 4.0. 1659 */ 1660 hdrspace = ieee80211_hdrspace(ic, wh); 1661 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) { 1662 m = ieee80211_defrag(ni, m, hdrspace); 1663 if (m == NULL) { 1664 /* Fragment dropped or frame not complete yet */ 1665 goto out; 1666 } 1667 } 1668 wh = mtod(m, struct ieee80211_frame *); /* NB: after defrag */ 1669 1670 /* 1671 * Now we have a complete Mesh Data frame. 1672 */ 1673 1674 /* 1675 * Only fromDStoDS data frames use 4 address qos frames 1676 * as specified in amendment. Otherwise addr4 is located 1677 * in the Mesh Control field and a 3 address qos frame 1678 * is used. 1679 */ 1680 if (IEEE80211_IS_DSTODS(wh)) 1681 *(uint16_t *)qos = *(uint16_t *) 1682 ((struct ieee80211_qosframe_addr4 *)wh)->i_qos; 1683 else 1684 *(uint16_t *)qos = *(uint16_t *) 1685 ((struct ieee80211_qosframe *)wh)->i_qos; 1686 1687 /* 1688 * NB: The mesh STA sets the Mesh Control Present 1689 * subfield to 1 in the Mesh Data frame containing 1690 * an unfragmented MSDU, an A-MSDU, or the first 1691 * fragment of an MSDU. 1692 * After defrag it should always be present. 1693 */ 1694 if (!(qos[1] & IEEE80211_QOS_MC)) { 1695 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_MESH, 1696 ni->ni_macaddr, NULL, 1697 "%s", "Mesh control field not present"); 1698 vap->iv_stats.is_rx_elem_missing++; /* XXX: kinda */ 1699 goto err; 1700 } 1701 1702 /* pull up enough to get to the mesh control */ 1703 if (m->m_len < hdrspace + sizeof(struct ieee80211_meshcntl) && 1704 (m = m_pullup(m, hdrspace + 1705 sizeof(struct ieee80211_meshcntl))) == NULL) { 1706 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY, 1707 ni->ni_macaddr, NULL, 1708 "data too short: expecting %u", hdrspace); 1709 vap->iv_stats.is_rx_tooshort++; 1710 goto out; /* XXX */ 1711 } 1712 /* 1713 * Now calculate the full extent of the headers. Note 1714 * mesh_decap will pull up anything we didn't get 1715 * above when it strips the 802.11 headers. 1716 */ 1717 mc = (const struct ieee80211_meshcntl *) 1718 (mtod(m, const uint8_t *) + hdrspace); 1719 ae = mc->mc_flags & IEEE80211_MESH_AE_MASK; 1720 meshdrlen = sizeof(struct ieee80211_meshcntl) + 1721 ae * IEEE80211_ADDR_LEN; 1722 hdrspace += meshdrlen; 1723 1724 /* pull complete hdrspace = ieee80211_hdrspace + meshcontrol */ 1725 if ((meshdrlen > sizeof(struct ieee80211_meshcntl)) && 1726 (m->m_len < hdrspace) && 1727 ((m = m_pullup(m, hdrspace)) == NULL)) { 1728 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY, 1729 ni->ni_macaddr, NULL, 1730 "data too short: expecting %u", hdrspace); 1731 vap->iv_stats.is_rx_tooshort++; 1732 goto out; /* XXX */ 1733 } 1734 /* XXX: are we sure there is no reallocating after m_pullup? */ 1735 1736 seq = LE_READ_4(mc->mc_seq); 1737 if (IEEE80211_IS_MULTICAST(wh->i_addr1)) 1738 addr = wh->i_addr3; 1739 else if (ae == IEEE80211_MESH_AE_01) 1740 addr = MC01(mc)->mc_addr4; 1741 else 1742 addr = ((struct ieee80211_qosframe_addr4 *)wh)->i_addr4; 1743 if (IEEE80211_ADDR_EQ(vap->iv_myaddr, addr)) { 1744 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT, 1745 addr, "data", "%s", "not to me"); 1746 vap->iv_stats.is_rx_wrongbss++; /* XXX kinda */ 1747 goto out; 1748 } 1749 if (mesh_checkpseq(vap, addr, seq) != 0) { 1750 vap->iv_stats.is_rx_dup++; 1751 goto out; 1752 } 1753 1754 /* This code "routes" the frame to the right control path */ 1755 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) { 1756 if (IEEE80211_ADDR_EQ(vap->iv_myaddr, wh->i_addr3)) 1757 error = 1758 mesh_recv_indiv_data_to_me(vap, m, wh, mc); 1759 else if (IEEE80211_IS_MULTICAST(wh->i_addr3)) 1760 error = mesh_recv_group_data(vap, m, wh, mc); 1761 else 1762 error = mesh_recv_indiv_data_to_fwrd(vap, m, 1763 wh, mc); 1764 } else 1765 error = mesh_recv_group_data(vap, m, wh, mc); 1766 if (error < 0) 1767 goto err; 1768 else if (error > 0) 1769 goto out; 1770 1771 if (ieee80211_radiotap_active_vap(vap)) 1772 ieee80211_radiotap_rx(vap, m); 1773 need_tap = 0; 1774 1775 /* 1776 * Finally, strip the 802.11 header. 1777 */ 1778 m = mesh_decap(vap, m, hdrspace, meshdrlen); 1779 if (m == NULL) { 1780 /* XXX mask bit to check for both */ 1781 /* don't count Null data frames as errors */ 1782 if (subtype == IEEE80211_FC0_SUBTYPE_NODATA || 1783 subtype == IEEE80211_FC0_SUBTYPE_QOS_NULL) 1784 goto out; 1785 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT, 1786 ni->ni_macaddr, "data", "%s", "decap error"); 1787 vap->iv_stats.is_rx_decap++; 1788 IEEE80211_NODE_STAT(ni, rx_decap); 1789 goto err; 1790 } 1791 if (qos[0] & IEEE80211_QOS_AMSDU) { 1792 m = ieee80211_decap_amsdu(ni, m); 1793 if (m == NULL) 1794 return IEEE80211_FC0_TYPE_DATA; 1795 } 1796 ieee80211_deliver_data(vap, ni, m); 1797 return type; 1798 case IEEE80211_FC0_TYPE_MGT: 1799 vap->iv_stats.is_rx_mgmt++; 1800 IEEE80211_NODE_STAT(ni, rx_mgmt); 1801 if (dir != IEEE80211_FC1_DIR_NODS) { 1802 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT, 1803 wh, "mgt", "incorrect dir 0x%x", dir); 1804 vap->iv_stats.is_rx_wrongdir++; 1805 goto err; 1806 } 1807 if (m->m_pkthdr.len < sizeof(struct ieee80211_frame)) { 1808 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY, 1809 ni->ni_macaddr, "mgt", "too short: len %u", 1810 m->m_pkthdr.len); 1811 vap->iv_stats.is_rx_tooshort++; 1812 goto out; 1813 } 1814 #ifdef IEEE80211_DEBUG 1815 if ((ieee80211_msg_debug(vap) && 1816 (vap->iv_ic->ic_flags & IEEE80211_F_SCAN)) || 1817 ieee80211_msg_dumppkts(vap)) { 1818 if_printf(ifp, "received %s from %s rssi %d\n", 1819 ieee80211_mgt_subtype_name[subtype >> 1820 IEEE80211_FC0_SUBTYPE_SHIFT], 1821 ether_sprintf(wh->i_addr2), rssi); 1822 } 1823 #endif 1824 if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) { 1825 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT, 1826 wh, NULL, "%s", "WEP set but not permitted"); 1827 vap->iv_stats.is_rx_mgtdiscard++; /* XXX */ 1828 goto out; 1829 } 1830 vap->iv_recv_mgmt(ni, m, subtype, rxs, rssi, nf); 1831 goto out; 1832 case IEEE80211_FC0_TYPE_CTL: 1833 vap->iv_stats.is_rx_ctl++; 1834 IEEE80211_NODE_STAT(ni, rx_ctrl); 1835 goto out; 1836 default: 1837 IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY, 1838 wh, "bad", "frame type 0x%x", type); 1839 /* should not come here */ 1840 break; 1841 } 1842 err: 1843 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1); 1844 out: 1845 if (m != NULL) { 1846 if (need_tap && ieee80211_radiotap_active_vap(vap)) 1847 ieee80211_radiotap_rx(vap, m); 1848 m_freem(m); 1849 } 1850 return type; 1851 #undef HAS_SEQ 1852 #undef MC01 1853 #undef MC10 1854 } 1855 1856 static void 1857 mesh_recv_mgmt(struct ieee80211_node *ni, struct mbuf *m0, int subtype, 1858 const struct ieee80211_rx_stats *rxs, int rssi, int nf) 1859 { 1860 struct ieee80211vap *vap = ni->ni_vap; 1861 struct ieee80211_mesh_state *ms = vap->iv_mesh; 1862 struct ieee80211com *ic = ni->ni_ic; 1863 struct ieee80211_channel *rxchan = ic->ic_curchan; 1864 struct ieee80211_frame *wh; 1865 struct ieee80211_mesh_route *rt; 1866 uint8_t *frm, *efrm; 1867 1868 wh = mtod(m0, struct ieee80211_frame *); 1869 frm = (uint8_t *)&wh[1]; 1870 efrm = mtod(m0, uint8_t *) + m0->m_len; 1871 switch (subtype) { 1872 case IEEE80211_FC0_SUBTYPE_PROBE_RESP: 1873 case IEEE80211_FC0_SUBTYPE_BEACON: 1874 { 1875 struct ieee80211_scanparams scan; 1876 struct ieee80211_channel *c; 1877 /* 1878 * We process beacon/probe response 1879 * frames to discover neighbors. 1880 */ 1881 if (rxs != NULL) { 1882 c = ieee80211_lookup_channel_rxstatus(vap, rxs); 1883 if (c != NULL) 1884 rxchan = c; 1885 } 1886 if (ieee80211_parse_beacon(ni, m0, rxchan, &scan) != 0) 1887 return; 1888 /* 1889 * Count frame now that we know it's to be processed. 1890 */ 1891 if (subtype == IEEE80211_FC0_SUBTYPE_BEACON) { 1892 vap->iv_stats.is_rx_beacon++; /* XXX remove */ 1893 IEEE80211_NODE_STAT(ni, rx_beacons); 1894 } else 1895 IEEE80211_NODE_STAT(ni, rx_proberesp); 1896 /* 1897 * If scanning, just pass information to the scan module. 1898 */ 1899 if (ic->ic_flags & IEEE80211_F_SCAN) { 1900 if (ic->ic_flags_ext & IEEE80211_FEXT_PROBECHAN) { 1901 /* 1902 * Actively scanning a channel marked passive; 1903 * send a probe request now that we know there 1904 * is 802.11 traffic present. 1905 * 1906 * XXX check if the beacon we recv'd gives 1907 * us what we need and suppress the probe req 1908 */ 1909 ieee80211_probe_curchan(vap, 1); 1910 ic->ic_flags_ext &= ~IEEE80211_FEXT_PROBECHAN; 1911 } 1912 ieee80211_add_scan(vap, rxchan, &scan, wh, 1913 subtype, rssi, nf); 1914 return; 1915 } 1916 1917 /* The rest of this code assumes we are running */ 1918 if (vap->iv_state != IEEE80211_S_RUN) 1919 return; 1920 /* 1921 * Ignore non-mesh STAs. 1922 */ 1923 if ((scan.capinfo & 1924 (IEEE80211_CAPINFO_ESS|IEEE80211_CAPINFO_IBSS)) || 1925 scan.meshid == NULL || scan.meshconf == NULL) { 1926 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT, 1927 wh, "beacon", "%s", "not a mesh sta"); 1928 vap->iv_stats.is_mesh_wrongmesh++; 1929 return; 1930 } 1931 /* 1932 * Ignore STAs for other mesh networks. 1933 */ 1934 if (memcmp(scan.meshid+2, ms->ms_id, ms->ms_idlen) != 0 || 1935 mesh_verify_meshconf(vap, scan.meshconf)) { 1936 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT, 1937 wh, "beacon", "%s", "not for our mesh"); 1938 vap->iv_stats.is_mesh_wrongmesh++; 1939 return; 1940 } 1941 /* 1942 * Peer only based on the current ACL policy. 1943 */ 1944 if (vap->iv_acl != NULL && !vap->iv_acl->iac_check(vap, wh)) { 1945 IEEE80211_DISCARD(vap, IEEE80211_MSG_ACL, 1946 wh, NULL, "%s", "disallowed by ACL"); 1947 vap->iv_stats.is_rx_acl++; 1948 return; 1949 } 1950 /* 1951 * Do neighbor discovery. 1952 */ 1953 if (!IEEE80211_ADDR_EQ(wh->i_addr2, ni->ni_macaddr)) { 1954 /* 1955 * Create a new entry in the neighbor table. 1956 */ 1957 ni = ieee80211_add_neighbor(vap, wh, &scan); 1958 } 1959 /* 1960 * Automatically peer with discovered nodes if possible. 1961 */ 1962 if (ni != vap->iv_bss && 1963 (ms->ms_flags & IEEE80211_MESHFLAGS_AP)) { 1964 switch (ni->ni_mlstate) { 1965 case IEEE80211_NODE_MESH_IDLE: 1966 { 1967 uint16_t args[1]; 1968 1969 /* Wait for backoff callout to reset counter */ 1970 if (ni->ni_mlhcnt >= ieee80211_mesh_maxholding) 1971 return; 1972 1973 ni->ni_mlpid = mesh_generateid(vap); 1974 if (ni->ni_mlpid == 0) 1975 return; 1976 mesh_linkchange(ni, IEEE80211_NODE_MESH_OPENSNT); 1977 args[0] = ni->ni_mlpid; 1978 ieee80211_send_action(ni, 1979 IEEE80211_ACTION_CAT_SELF_PROT, 1980 IEEE80211_ACTION_MESHPEERING_OPEN, args); 1981 ni->ni_mlrcnt = 0; 1982 mesh_peer_timeout_setup(ni); 1983 break; 1984 } 1985 case IEEE80211_NODE_MESH_ESTABLISHED: 1986 { 1987 /* 1988 * Valid beacon from a peer mesh STA 1989 * bump TA lifetime 1990 */ 1991 rt = ieee80211_mesh_rt_find(vap, wh->i_addr2); 1992 if(rt != NULL) { 1993 ieee80211_mesh_rt_update(rt, 1994 ticks_to_msecs( 1995 ms->ms_ppath->mpp_inact)); 1996 } 1997 break; 1998 } 1999 default: 2000 break; /* ignore */ 2001 } 2002 } 2003 break; 2004 } 2005 case IEEE80211_FC0_SUBTYPE_PROBE_REQ: 2006 { 2007 uint8_t *ssid, *meshid, *rates, *xrates; 2008 uint8_t *sfrm; 2009 2010 if (vap->iv_state != IEEE80211_S_RUN) { 2011 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT, 2012 wh, NULL, "wrong state %s", 2013 ieee80211_state_name[vap->iv_state]); 2014 vap->iv_stats.is_rx_mgtdiscard++; 2015 return; 2016 } 2017 if (IEEE80211_IS_MULTICAST(wh->i_addr2)) { 2018 /* frame must be directed */ 2019 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT, 2020 wh, NULL, "%s", "not unicast"); 2021 vap->iv_stats.is_rx_mgtdiscard++; /* XXX stat */ 2022 return; 2023 } 2024 /* 2025 * prreq frame format 2026 * [tlv] ssid 2027 * [tlv] supported rates 2028 * [tlv] extended supported rates 2029 * [tlv] mesh id 2030 */ 2031 ssid = meshid = rates = xrates = NULL; 2032 sfrm = frm; 2033 while (efrm - frm > 1) { 2034 IEEE80211_VERIFY_LENGTH(efrm - frm, frm[1] + 2, return); 2035 switch (*frm) { 2036 case IEEE80211_ELEMID_SSID: 2037 ssid = frm; 2038 break; 2039 case IEEE80211_ELEMID_RATES: 2040 rates = frm; 2041 break; 2042 case IEEE80211_ELEMID_XRATES: 2043 xrates = frm; 2044 break; 2045 case IEEE80211_ELEMID_MESHID: 2046 meshid = frm; 2047 break; 2048 } 2049 frm += frm[1] + 2; 2050 } 2051 IEEE80211_VERIFY_ELEMENT(ssid, IEEE80211_NWID_LEN, return); 2052 IEEE80211_VERIFY_ELEMENT(rates, IEEE80211_RATE_MAXSIZE, return); 2053 if (xrates != NULL) 2054 IEEE80211_VERIFY_ELEMENT(xrates, 2055 IEEE80211_RATE_MAXSIZE - rates[1], return); 2056 if (meshid != NULL) { 2057 IEEE80211_VERIFY_ELEMENT(meshid, 2058 IEEE80211_MESHID_LEN, return); 2059 /* NB: meshid, not ssid */ 2060 IEEE80211_VERIFY_SSID(vap->iv_bss, meshid, return); 2061 } 2062 2063 /* XXX find a better class or define it's own */ 2064 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_INPUT, wh->i_addr2, 2065 "%s", "recv probe req"); 2066 /* 2067 * Some legacy 11b clients cannot hack a complete 2068 * probe response frame. When the request includes 2069 * only a bare-bones rate set, communicate this to 2070 * the transmit side. 2071 */ 2072 ieee80211_send_proberesp(vap, wh->i_addr2, 0); 2073 break; 2074 } 2075 2076 case IEEE80211_FC0_SUBTYPE_ACTION: 2077 case IEEE80211_FC0_SUBTYPE_ACTION_NOACK: 2078 if (ni == vap->iv_bss) { 2079 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT, 2080 wh, NULL, "%s", "unknown node"); 2081 vap->iv_stats.is_rx_mgtdiscard++; 2082 } else if (!IEEE80211_ADDR_EQ(vap->iv_myaddr, wh->i_addr1) && 2083 !IEEE80211_IS_MULTICAST(wh->i_addr1)) { 2084 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT, 2085 wh, NULL, "%s", "not for us"); 2086 vap->iv_stats.is_rx_mgtdiscard++; 2087 } else if (vap->iv_state != IEEE80211_S_RUN) { 2088 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT, 2089 wh, NULL, "wrong state %s", 2090 ieee80211_state_name[vap->iv_state]); 2091 vap->iv_stats.is_rx_mgtdiscard++; 2092 } else { 2093 if (ieee80211_parse_action(ni, m0) == 0) 2094 (void)ic->ic_recv_action(ni, wh, frm, efrm); 2095 } 2096 break; 2097 2098 case IEEE80211_FC0_SUBTYPE_ASSOC_REQ: 2099 case IEEE80211_FC0_SUBTYPE_ASSOC_RESP: 2100 case IEEE80211_FC0_SUBTYPE_REASSOC_REQ: 2101 case IEEE80211_FC0_SUBTYPE_REASSOC_RESP: 2102 case IEEE80211_FC0_SUBTYPE_ATIM: 2103 case IEEE80211_FC0_SUBTYPE_DISASSOC: 2104 case IEEE80211_FC0_SUBTYPE_AUTH: 2105 case IEEE80211_FC0_SUBTYPE_DEAUTH: 2106 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT, 2107 wh, NULL, "%s", "not handled"); 2108 vap->iv_stats.is_rx_mgtdiscard++; 2109 break; 2110 2111 default: 2112 IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY, 2113 wh, "mgt", "subtype 0x%x not handled", subtype); 2114 vap->iv_stats.is_rx_badsubtype++; 2115 break; 2116 } 2117 } 2118 2119 static void 2120 mesh_recv_ctl(struct ieee80211_node *ni, struct mbuf *m, int subtype) 2121 { 2122 2123 switch (subtype) { 2124 case IEEE80211_FC0_SUBTYPE_BAR: 2125 ieee80211_recv_bar(ni, m); 2126 break; 2127 } 2128 } 2129 2130 /* 2131 * Parse meshpeering action ie's for MPM frames 2132 */ 2133 static const struct ieee80211_meshpeer_ie * 2134 mesh_parse_meshpeering_action(struct ieee80211_node *ni, 2135 const struct ieee80211_frame *wh, /* XXX for VERIFY_LENGTH */ 2136 const uint8_t *frm, const uint8_t *efrm, 2137 struct ieee80211_meshpeer_ie *mp, uint8_t subtype) 2138 { 2139 struct ieee80211vap *vap = ni->ni_vap; 2140 const struct ieee80211_meshpeer_ie *mpie; 2141 uint16_t args[3]; 2142 const uint8_t *meshid, *meshconf, *meshpeer; 2143 uint8_t sendclose = 0; /* 1 = MPM frame rejected, close will be sent */ 2144 2145 meshid = meshconf = meshpeer = NULL; 2146 while (efrm - frm > 1) { 2147 IEEE80211_VERIFY_LENGTH(efrm - frm, frm[1] + 2, return NULL); 2148 switch (*frm) { 2149 case IEEE80211_ELEMID_MESHID: 2150 meshid = frm; 2151 break; 2152 case IEEE80211_ELEMID_MESHCONF: 2153 meshconf = frm; 2154 break; 2155 case IEEE80211_ELEMID_MESHPEER: 2156 meshpeer = frm; 2157 mpie = (const struct ieee80211_meshpeer_ie *) frm; 2158 memset(mp, 0, sizeof(*mp)); 2159 mp->peer_len = mpie->peer_len; 2160 mp->peer_proto = LE_READ_2(&mpie->peer_proto); 2161 mp->peer_llinkid = LE_READ_2(&mpie->peer_llinkid); 2162 switch (subtype) { 2163 case IEEE80211_ACTION_MESHPEERING_CONFIRM: 2164 mp->peer_linkid = 2165 LE_READ_2(&mpie->peer_linkid); 2166 break; 2167 case IEEE80211_ACTION_MESHPEERING_CLOSE: 2168 /* NB: peer link ID is optional */ 2169 if (mpie->peer_len == 2170 (IEEE80211_MPM_BASE_SZ + 2)) { 2171 mp->peer_linkid = 0; 2172 mp->peer_rcode = 2173 LE_READ_2(&mpie->peer_linkid); 2174 } else { 2175 mp->peer_linkid = 2176 LE_READ_2(&mpie->peer_linkid); 2177 mp->peer_rcode = 2178 LE_READ_2(&mpie->peer_rcode); 2179 } 2180 break; 2181 } 2182 break; 2183 } 2184 frm += frm[1] + 2; 2185 } 2186 2187 /* 2188 * Verify the contents of the frame. 2189 * If it fails validation, close the peer link. 2190 */ 2191 if (mesh_verify_meshpeer(vap, subtype, (const uint8_t *)mp)) { 2192 sendclose = 1; 2193 IEEE80211_DISCARD(vap, 2194 IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH, 2195 wh, NULL, "%s", "MPM validation failed"); 2196 } 2197 2198 /* If meshid is not the same reject any frames type. */ 2199 if (sendclose == 0 && mesh_verify_meshid(vap, meshid)) { 2200 sendclose = 1; 2201 IEEE80211_DISCARD(vap, 2202 IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH, 2203 wh, NULL, "%s", "not for our mesh"); 2204 if (subtype == IEEE80211_ACTION_MESHPEERING_CLOSE) { 2205 /* 2206 * Standard not clear about this, if we dont ignore 2207 * there will be an endless loop between nodes sending 2208 * CLOSE frames between each other with wrong meshid. 2209 * Discard and timers will bring FSM to IDLE state. 2210 */ 2211 return NULL; 2212 } 2213 } 2214 2215 /* 2216 * Close frames are accepted if meshid is the same. 2217 * Verify the other two types. 2218 */ 2219 if (sendclose == 0 && subtype != IEEE80211_ACTION_MESHPEERING_CLOSE && 2220 mesh_verify_meshconf(vap, meshconf)) { 2221 sendclose = 1; 2222 IEEE80211_DISCARD(vap, 2223 IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH, 2224 wh, NULL, "%s", "configuration missmatch"); 2225 } 2226 2227 if (sendclose) { 2228 vap->iv_stats.is_rx_mgtdiscard++; 2229 switch (ni->ni_mlstate) { 2230 case IEEE80211_NODE_MESH_IDLE: 2231 case IEEE80211_NODE_MESH_ESTABLISHED: 2232 case IEEE80211_NODE_MESH_HOLDING: 2233 /* ignore */ 2234 break; 2235 case IEEE80211_NODE_MESH_OPENSNT: 2236 case IEEE80211_NODE_MESH_OPENRCV: 2237 case IEEE80211_NODE_MESH_CONFIRMRCV: 2238 args[0] = ni->ni_mlpid; 2239 args[1] = ni->ni_mllid; 2240 /* Reason codes for rejection */ 2241 switch (subtype) { 2242 case IEEE80211_ACTION_MESHPEERING_OPEN: 2243 args[2] = IEEE80211_REASON_MESH_CPVIOLATION; 2244 break; 2245 case IEEE80211_ACTION_MESHPEERING_CONFIRM: 2246 args[2] = IEEE80211_REASON_MESH_INCONS_PARAMS; 2247 break; 2248 } 2249 ieee80211_send_action(ni, 2250 IEEE80211_ACTION_CAT_SELF_PROT, 2251 IEEE80211_ACTION_MESHPEERING_CLOSE, 2252 args); 2253 mesh_linkchange(ni, IEEE80211_NODE_MESH_HOLDING); 2254 mesh_peer_timeout_setup(ni); 2255 break; 2256 } 2257 return NULL; 2258 } 2259 2260 return (const struct ieee80211_meshpeer_ie *) mp; 2261 } 2262 2263 static int 2264 mesh_recv_action_meshpeering_open(struct ieee80211_node *ni, 2265 const struct ieee80211_frame *wh, 2266 const uint8_t *frm, const uint8_t *efrm) 2267 { 2268 struct ieee80211vap *vap = ni->ni_vap; 2269 struct ieee80211_mesh_state *ms = vap->iv_mesh; 2270 struct ieee80211_meshpeer_ie ie; 2271 const struct ieee80211_meshpeer_ie *meshpeer; 2272 uint16_t args[3]; 2273 2274 /* +2+2 for action + code + capabilites */ 2275 meshpeer = mesh_parse_meshpeering_action(ni, wh, frm+2+2, efrm, &ie, 2276 IEEE80211_ACTION_MESHPEERING_OPEN); 2277 if (meshpeer == NULL) { 2278 return 0; 2279 } 2280 2281 /* XXX move up */ 2282 IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH, ni, 2283 "recv PEER OPEN, lid 0x%x", meshpeer->peer_llinkid); 2284 2285 switch (ni->ni_mlstate) { 2286 case IEEE80211_NODE_MESH_IDLE: 2287 /* Reject open request if reached our maximum neighbor count */ 2288 if (ms->ms_neighbors >= IEEE80211_MESH_MAX_NEIGHBORS) { 2289 args[0] = meshpeer->peer_llinkid; 2290 args[1] = 0; 2291 args[2] = IEEE80211_REASON_MESH_MAX_PEERS; 2292 ieee80211_send_action(ni, 2293 IEEE80211_ACTION_CAT_SELF_PROT, 2294 IEEE80211_ACTION_MESHPEERING_CLOSE, 2295 args); 2296 /* stay in IDLE state */ 2297 return (0); 2298 } 2299 /* Open frame accepted */ 2300 mesh_linkchange(ni, IEEE80211_NODE_MESH_OPENRCV); 2301 ni->ni_mllid = meshpeer->peer_llinkid; 2302 ni->ni_mlpid = mesh_generateid(vap); 2303 if (ni->ni_mlpid == 0) 2304 return 0; /* XXX */ 2305 args[0] = ni->ni_mlpid; 2306 /* Announce we're open too... */ 2307 ieee80211_send_action(ni, 2308 IEEE80211_ACTION_CAT_SELF_PROT, 2309 IEEE80211_ACTION_MESHPEERING_OPEN, args); 2310 /* ...and confirm the link. */ 2311 args[0] = ni->ni_mlpid; 2312 args[1] = ni->ni_mllid; 2313 ieee80211_send_action(ni, 2314 IEEE80211_ACTION_CAT_SELF_PROT, 2315 IEEE80211_ACTION_MESHPEERING_CONFIRM, 2316 args); 2317 mesh_peer_timeout_setup(ni); 2318 break; 2319 case IEEE80211_NODE_MESH_OPENRCV: 2320 /* Wrong Link ID */ 2321 if (ni->ni_mllid != meshpeer->peer_llinkid) { 2322 args[0] = ni->ni_mllid; 2323 args[1] = ni->ni_mlpid; 2324 args[2] = IEEE80211_REASON_PEER_LINK_CANCELED; 2325 ieee80211_send_action(ni, 2326 IEEE80211_ACTION_CAT_SELF_PROT, 2327 IEEE80211_ACTION_MESHPEERING_CLOSE, 2328 args); 2329 mesh_linkchange(ni, IEEE80211_NODE_MESH_HOLDING); 2330 mesh_peer_timeout_setup(ni); 2331 break; 2332 } 2333 /* Duplicate open, confirm again. */ 2334 args[0] = ni->ni_mlpid; 2335 args[1] = ni->ni_mllid; 2336 ieee80211_send_action(ni, 2337 IEEE80211_ACTION_CAT_SELF_PROT, 2338 IEEE80211_ACTION_MESHPEERING_CONFIRM, 2339 args); 2340 break; 2341 case IEEE80211_NODE_MESH_OPENSNT: 2342 ni->ni_mllid = meshpeer->peer_llinkid; 2343 mesh_linkchange(ni, IEEE80211_NODE_MESH_OPENRCV); 2344 args[0] = ni->ni_mlpid; 2345 args[1] = ni->ni_mllid; 2346 ieee80211_send_action(ni, 2347 IEEE80211_ACTION_CAT_SELF_PROT, 2348 IEEE80211_ACTION_MESHPEERING_CONFIRM, 2349 args); 2350 /* NB: don't setup/clear any timeout */ 2351 break; 2352 case IEEE80211_NODE_MESH_CONFIRMRCV: 2353 if (ni->ni_mlpid != meshpeer->peer_linkid || 2354 ni->ni_mllid != meshpeer->peer_llinkid) { 2355 args[0] = ni->ni_mlpid; 2356 args[1] = ni->ni_mllid; 2357 args[2] = IEEE80211_REASON_PEER_LINK_CANCELED; 2358 ieee80211_send_action(ni, 2359 IEEE80211_ACTION_CAT_SELF_PROT, 2360 IEEE80211_ACTION_MESHPEERING_CLOSE, 2361 args); 2362 mesh_linkchange(ni, 2363 IEEE80211_NODE_MESH_HOLDING); 2364 mesh_peer_timeout_setup(ni); 2365 break; 2366 } 2367 mesh_linkchange(ni, IEEE80211_NODE_MESH_ESTABLISHED); 2368 ni->ni_mllid = meshpeer->peer_llinkid; 2369 args[0] = ni->ni_mlpid; 2370 args[1] = ni->ni_mllid; 2371 ieee80211_send_action(ni, 2372 IEEE80211_ACTION_CAT_SELF_PROT, 2373 IEEE80211_ACTION_MESHPEERING_CONFIRM, 2374 args); 2375 mesh_peer_timeout_stop(ni); 2376 break; 2377 case IEEE80211_NODE_MESH_ESTABLISHED: 2378 if (ni->ni_mllid != meshpeer->peer_llinkid) { 2379 args[0] = ni->ni_mllid; 2380 args[1] = ni->ni_mlpid; 2381 args[2] = IEEE80211_REASON_PEER_LINK_CANCELED; 2382 ieee80211_send_action(ni, 2383 IEEE80211_ACTION_CAT_SELF_PROT, 2384 IEEE80211_ACTION_MESHPEERING_CLOSE, 2385 args); 2386 mesh_linkchange(ni, IEEE80211_NODE_MESH_HOLDING); 2387 mesh_peer_timeout_setup(ni); 2388 break; 2389 } 2390 args[0] = ni->ni_mlpid; 2391 args[1] = ni->ni_mllid; 2392 ieee80211_send_action(ni, 2393 IEEE80211_ACTION_CAT_SELF_PROT, 2394 IEEE80211_ACTION_MESHPEERING_CONFIRM, 2395 args); 2396 break; 2397 case IEEE80211_NODE_MESH_HOLDING: 2398 args[0] = ni->ni_mlpid; 2399 args[1] = meshpeer->peer_llinkid; 2400 /* Standard not clear about what the reaason code should be */ 2401 args[2] = IEEE80211_REASON_PEER_LINK_CANCELED; 2402 ieee80211_send_action(ni, 2403 IEEE80211_ACTION_CAT_SELF_PROT, 2404 IEEE80211_ACTION_MESHPEERING_CLOSE, 2405 args); 2406 break; 2407 } 2408 return 0; 2409 } 2410 2411 static int 2412 mesh_recv_action_meshpeering_confirm(struct ieee80211_node *ni, 2413 const struct ieee80211_frame *wh, 2414 const uint8_t *frm, const uint8_t *efrm) 2415 { 2416 struct ieee80211vap *vap = ni->ni_vap; 2417 struct ieee80211_meshpeer_ie ie; 2418 const struct ieee80211_meshpeer_ie *meshpeer; 2419 uint16_t args[3]; 2420 2421 /* +2+2+2+2 for action + code + capabilites + status code + AID */ 2422 meshpeer = mesh_parse_meshpeering_action(ni, wh, frm+2+2+2+2, efrm, &ie, 2423 IEEE80211_ACTION_MESHPEERING_CONFIRM); 2424 if (meshpeer == NULL) { 2425 return 0; 2426 } 2427 2428 IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH, ni, 2429 "recv PEER CONFIRM, local id 0x%x, peer id 0x%x", 2430 meshpeer->peer_llinkid, meshpeer->peer_linkid); 2431 2432 switch (ni->ni_mlstate) { 2433 case IEEE80211_NODE_MESH_OPENRCV: 2434 mesh_linkchange(ni, IEEE80211_NODE_MESH_ESTABLISHED); 2435 mesh_peer_timeout_stop(ni); 2436 break; 2437 case IEEE80211_NODE_MESH_OPENSNT: 2438 mesh_linkchange(ni, IEEE80211_NODE_MESH_CONFIRMRCV); 2439 mesh_peer_timeout_setup(ni); 2440 break; 2441 case IEEE80211_NODE_MESH_HOLDING: 2442 args[0] = ni->ni_mlpid; 2443 args[1] = meshpeer->peer_llinkid; 2444 /* Standard not clear about what the reaason code should be */ 2445 args[2] = IEEE80211_REASON_PEER_LINK_CANCELED; 2446 ieee80211_send_action(ni, 2447 IEEE80211_ACTION_CAT_SELF_PROT, 2448 IEEE80211_ACTION_MESHPEERING_CLOSE, 2449 args); 2450 break; 2451 case IEEE80211_NODE_MESH_CONFIRMRCV: 2452 if (ni->ni_mllid != meshpeer->peer_llinkid) { 2453 args[0] = ni->ni_mlpid; 2454 args[1] = ni->ni_mllid; 2455 args[2] = IEEE80211_REASON_PEER_LINK_CANCELED; 2456 ieee80211_send_action(ni, 2457 IEEE80211_ACTION_CAT_SELF_PROT, 2458 IEEE80211_ACTION_MESHPEERING_CLOSE, 2459 args); 2460 mesh_linkchange(ni, IEEE80211_NODE_MESH_HOLDING); 2461 mesh_peer_timeout_setup(ni); 2462 } 2463 break; 2464 default: 2465 IEEE80211_DISCARD(vap, 2466 IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH, 2467 wh, NULL, "received confirm in invalid state %d", 2468 ni->ni_mlstate); 2469 vap->iv_stats.is_rx_mgtdiscard++; 2470 break; 2471 } 2472 return 0; 2473 } 2474 2475 static int 2476 mesh_recv_action_meshpeering_close(struct ieee80211_node *ni, 2477 const struct ieee80211_frame *wh, 2478 const uint8_t *frm, const uint8_t *efrm) 2479 { 2480 struct ieee80211_meshpeer_ie ie; 2481 const struct ieee80211_meshpeer_ie *meshpeer; 2482 uint16_t args[3]; 2483 2484 /* +2 for action + code */ 2485 meshpeer = mesh_parse_meshpeering_action(ni, wh, frm+2, efrm, &ie, 2486 IEEE80211_ACTION_MESHPEERING_CLOSE); 2487 if (meshpeer == NULL) { 2488 return 0; 2489 } 2490 2491 /* 2492 * XXX: check reason code, for example we could receive 2493 * IEEE80211_REASON_MESH_MAX_PEERS then we should not attempt 2494 * to peer again. 2495 */ 2496 2497 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH, 2498 ni, "%s", "recv PEER CLOSE"); 2499 2500 switch (ni->ni_mlstate) { 2501 case IEEE80211_NODE_MESH_IDLE: 2502 /* ignore */ 2503 break; 2504 case IEEE80211_NODE_MESH_OPENRCV: 2505 case IEEE80211_NODE_MESH_OPENSNT: 2506 case IEEE80211_NODE_MESH_CONFIRMRCV: 2507 case IEEE80211_NODE_MESH_ESTABLISHED: 2508 args[0] = ni->ni_mlpid; 2509 args[1] = ni->ni_mllid; 2510 args[2] = IEEE80211_REASON_MESH_CLOSE_RCVD; 2511 ieee80211_send_action(ni, 2512 IEEE80211_ACTION_CAT_SELF_PROT, 2513 IEEE80211_ACTION_MESHPEERING_CLOSE, 2514 args); 2515 mesh_linkchange(ni, IEEE80211_NODE_MESH_HOLDING); 2516 mesh_peer_timeout_setup(ni); 2517 break; 2518 case IEEE80211_NODE_MESH_HOLDING: 2519 mesh_linkchange(ni, IEEE80211_NODE_MESH_IDLE); 2520 mesh_peer_timeout_stop(ni); 2521 break; 2522 } 2523 return 0; 2524 } 2525 2526 /* 2527 * Link Metric handling. 2528 */ 2529 static int 2530 mesh_recv_action_meshlmetric(struct ieee80211_node *ni, 2531 const struct ieee80211_frame *wh, 2532 const uint8_t *frm, const uint8_t *efrm) 2533 { 2534 const struct ieee80211_meshlmetric_ie *ie = 2535 (const struct ieee80211_meshlmetric_ie *) 2536 (frm+2); /* action + code */ 2537 struct ieee80211_meshlmetric_ie lm_rep; 2538 2539 if (ie->lm_flags & IEEE80211_MESH_LMETRIC_FLAGS_REQ) { 2540 lm_rep.lm_flags = 0; 2541 lm_rep.lm_metric = mesh_airtime_calc(ni); 2542 ieee80211_send_action(ni, 2543 IEEE80211_ACTION_CAT_MESH, 2544 IEEE80211_ACTION_MESH_LMETRIC, 2545 &lm_rep); 2546 } 2547 /* XXX: else do nothing for now */ 2548 return 0; 2549 } 2550 2551 /* 2552 * Parse meshgate action ie's for GANN frames. 2553 * Returns -1 if parsing fails, otherwise 0. 2554 */ 2555 static int 2556 mesh_parse_meshgate_action(struct ieee80211_node *ni, 2557 const struct ieee80211_frame *wh, /* XXX for VERIFY_LENGTH */ 2558 struct ieee80211_meshgann_ie *ie, const uint8_t *frm, const uint8_t *efrm) 2559 { 2560 struct ieee80211vap *vap = ni->ni_vap; 2561 const struct ieee80211_meshgann_ie *gannie; 2562 2563 while (efrm - frm > 1) { 2564 IEEE80211_VERIFY_LENGTH(efrm - frm, frm[1] + 2, return -1); 2565 switch (*frm) { 2566 case IEEE80211_ELEMID_MESHGANN: 2567 gannie = (const struct ieee80211_meshgann_ie *) frm; 2568 memset(ie, 0, sizeof(*ie)); 2569 ie->gann_ie = gannie->gann_ie; 2570 ie->gann_len = gannie->gann_len; 2571 ie->gann_flags = gannie->gann_flags; 2572 ie->gann_hopcount = gannie->gann_hopcount; 2573 ie->gann_ttl = gannie->gann_ttl; 2574 IEEE80211_ADDR_COPY(ie->gann_addr, gannie->gann_addr); 2575 ie->gann_seq = LE_READ_4(&gannie->gann_seq); 2576 ie->gann_interval = LE_READ_2(&gannie->gann_interval); 2577 break; 2578 } 2579 frm += frm[1] + 2; 2580 } 2581 2582 return 0; 2583 } 2584 2585 /* 2586 * Mesh Gate Announcement handling. 2587 */ 2588 static int 2589 mesh_recv_action_meshgate(struct ieee80211_node *ni, 2590 const struct ieee80211_frame *wh, 2591 const uint8_t *frm, const uint8_t *efrm) 2592 { 2593 struct ieee80211vap *vap = ni->ni_vap; 2594 struct ieee80211_mesh_state *ms = vap->iv_mesh; 2595 struct ieee80211_mesh_gate_route *gr, *next; 2596 struct ieee80211_mesh_route *rt_gate; 2597 struct ieee80211_meshgann_ie pgann; 2598 struct ieee80211_meshgann_ie ie; 2599 int found = 0; 2600 2601 /* +2 for action + code */ 2602 if (mesh_parse_meshgate_action(ni, wh, &ie, frm+2, efrm) != 0) { 2603 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_MESH, 2604 ni->ni_macaddr, NULL, "%s", 2605 "GANN parsing failed"); 2606 vap->iv_stats.is_rx_mgtdiscard++; 2607 return (0); 2608 } 2609 2610 if (IEEE80211_ADDR_EQ(vap->iv_myaddr, ie.gann_addr)) 2611 return 0; 2612 2613 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH, ni->ni_macaddr, 2614 "received GANN, meshgate: %6D (seq %u)", ie.gann_addr, ":", 2615 ie.gann_seq); 2616 2617 if (ms == NULL) 2618 return (0); 2619 MESH_RT_LOCK(ms); 2620 TAILQ_FOREACH_SAFE(gr, &ms->ms_known_gates, gr_next, next) { 2621 if (!IEEE80211_ADDR_EQ(gr->gr_addr, ie.gann_addr)) 2622 continue; 2623 if (ie.gann_seq <= gr->gr_lastseq) { 2624 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_MESH, 2625 ni->ni_macaddr, NULL, 2626 "GANN old seqno %u <= %u", 2627 ie.gann_seq, gr->gr_lastseq); 2628 MESH_RT_UNLOCK(ms); 2629 return (0); 2630 } 2631 /* corresponding mesh gate found & GANN accepted */ 2632 found = 1; 2633 break; 2634 2635 } 2636 if (found == 0) { 2637 /* this GANN is from a new mesh Gate add it to known table. */ 2638 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH, ie.gann_addr, 2639 "stored new GANN information, seq %u.", ie.gann_seq); 2640 gr = IEEE80211_MALLOC(ALIGN(sizeof(struct ieee80211_mesh_gate_route)), 2641 M_80211_MESH_GT_RT, 2642 IEEE80211_M_NOWAIT | IEEE80211_M_ZERO); 2643 IEEE80211_ADDR_COPY(gr->gr_addr, ie.gann_addr); 2644 TAILQ_INSERT_TAIL(&ms->ms_known_gates, gr, gr_next); 2645 } 2646 gr->gr_lastseq = ie.gann_seq; 2647 2648 /* check if we have a path to this gate */ 2649 rt_gate = mesh_rt_find_locked(ms, gr->gr_addr); 2650 if (rt_gate != NULL && 2651 rt_gate->rt_flags & IEEE80211_MESHRT_FLAGS_VALID) { 2652 gr->gr_route = rt_gate; 2653 rt_gate->rt_flags |= IEEE80211_MESHRT_FLAGS_GATE; 2654 } 2655 2656 MESH_RT_UNLOCK(ms); 2657 2658 /* popagate only if decremented ttl >= 1 && forwarding is enabled */ 2659 if ((ie.gann_ttl - 1) < 1 && !(ms->ms_flags & IEEE80211_MESHFLAGS_FWD)) 2660 return 0; 2661 pgann.gann_flags = ie.gann_flags; /* Reserved */ 2662 pgann.gann_hopcount = ie.gann_hopcount + 1; 2663 pgann.gann_ttl = ie.gann_ttl - 1; 2664 IEEE80211_ADDR_COPY(pgann.gann_addr, ie.gann_addr); 2665 pgann.gann_seq = ie.gann_seq; 2666 pgann.gann_interval = ie.gann_interval; 2667 2668 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH, ie.gann_addr, 2669 "%s", "propagate GANN"); 2670 2671 ieee80211_send_action(vap->iv_bss, IEEE80211_ACTION_CAT_MESH, 2672 IEEE80211_ACTION_MESH_GANN, &pgann); 2673 2674 return 0; 2675 } 2676 2677 static int 2678 mesh_send_action(struct ieee80211_node *ni, 2679 const uint8_t sa[IEEE80211_ADDR_LEN], 2680 const uint8_t da[IEEE80211_ADDR_LEN], 2681 struct mbuf *m) 2682 { 2683 struct ieee80211vap *vap = ni->ni_vap; 2684 struct ieee80211com *ic = ni->ni_ic; 2685 struct ieee80211_bpf_params params; 2686 struct ieee80211_frame *wh; 2687 int ret; 2688 2689 KASSERT(ni != NULL, ("null node")); 2690 2691 if (vap->iv_state == IEEE80211_S_CAC) { 2692 IEEE80211_NOTE(vap, IEEE80211_MSG_OUTPUT, ni, 2693 "block %s frame in CAC state", "Mesh action"); 2694 vap->iv_stats.is_tx_badstate++; 2695 ieee80211_free_node(ni); 2696 m_freem(m); 2697 return EIO; /* XXX */ 2698 } 2699 2700 M_PREPEND(m, sizeof(struct ieee80211_frame), M_NOWAIT); 2701 if (m == NULL) { 2702 ieee80211_free_node(ni); 2703 return ENOMEM; 2704 } 2705 2706 IEEE80211_TX_LOCK(ic); 2707 wh = mtod(m, struct ieee80211_frame *); 2708 ieee80211_send_setup(ni, m, 2709 IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_ACTION, 2710 IEEE80211_NONQOS_TID, sa, da, sa); 2711 m->m_flags |= M_ENCAP; /* mark encapsulated */ 2712 2713 memset(¶ms, 0, sizeof(params)); 2714 params.ibp_pri = WME_AC_VO; 2715 params.ibp_rate0 = ni->ni_txparms->mgmtrate; 2716 if (IEEE80211_IS_MULTICAST(da)) 2717 params.ibp_try0 = 1; 2718 else 2719 params.ibp_try0 = ni->ni_txparms->maxretry; 2720 params.ibp_power = ni->ni_txpower; 2721 2722 IEEE80211_NODE_STAT(ni, tx_mgmt); 2723 2724 ret = ieee80211_raw_output(vap, ni, m, ¶ms); 2725 IEEE80211_TX_UNLOCK(ic); 2726 return (ret); 2727 } 2728 2729 #define ADDSHORT(frm, v) do { \ 2730 frm[0] = (v) & 0xff; \ 2731 frm[1] = (v) >> 8; \ 2732 frm += 2; \ 2733 } while (0) 2734 #define ADDWORD(frm, v) do { \ 2735 frm[0] = (v) & 0xff; \ 2736 frm[1] = ((v) >> 8) & 0xff; \ 2737 frm[2] = ((v) >> 16) & 0xff; \ 2738 frm[3] = ((v) >> 24) & 0xff; \ 2739 frm += 4; \ 2740 } while (0) 2741 2742 static int 2743 mesh_send_action_meshpeering_open(struct ieee80211_node *ni, 2744 int category, int action, void *args0) 2745 { 2746 struct ieee80211vap *vap = ni->ni_vap; 2747 struct ieee80211com *ic = ni->ni_ic; 2748 uint16_t *args = args0; 2749 const struct ieee80211_rateset *rs; 2750 struct mbuf *m; 2751 uint8_t *frm; 2752 2753 IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH, ni, 2754 "send PEER OPEN action: localid 0x%x", args[0]); 2755 2756 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE, 2757 "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", __func__, __LINE__, 2758 ni, ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)+1); 2759 ieee80211_ref_node(ni); 2760 2761 m = ieee80211_getmgtframe(&frm, 2762 ic->ic_headroom + sizeof(struct ieee80211_frame), 2763 sizeof(uint16_t) /* action+category */ 2764 + sizeof(uint16_t) /* capabilites */ 2765 + 2 + IEEE80211_RATE_SIZE 2766 + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE) 2767 + 2 + IEEE80211_MESHID_LEN 2768 + sizeof(struct ieee80211_meshconf_ie) 2769 + sizeof(struct ieee80211_meshpeer_ie) 2770 ); 2771 if (m != NULL) { 2772 /* 2773 * mesh peer open action frame format: 2774 * [1] category 2775 * [1] action 2776 * [2] capabilities 2777 * [tlv] rates 2778 * [tlv] xrates 2779 * [tlv] mesh id 2780 * [tlv] mesh conf 2781 * [tlv] mesh peer link mgmt 2782 */ 2783 *frm++ = category; 2784 *frm++ = action; 2785 ADDSHORT(frm, ieee80211_getcapinfo(vap, ni->ni_chan)); 2786 rs = ieee80211_get_suprates(ic, ic->ic_curchan); 2787 frm = ieee80211_add_rates(frm, rs); 2788 frm = ieee80211_add_xrates(frm, rs); 2789 frm = ieee80211_add_meshid(frm, vap); 2790 frm = ieee80211_add_meshconf(frm, vap); 2791 frm = ieee80211_add_meshpeer(frm, IEEE80211_ACTION_MESHPEERING_OPEN, 2792 args[0], 0, 0); 2793 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *); 2794 return mesh_send_action(ni, vap->iv_myaddr, ni->ni_macaddr, m); 2795 } else { 2796 vap->iv_stats.is_tx_nobuf++; 2797 ieee80211_free_node(ni); 2798 return ENOMEM; 2799 } 2800 } 2801 2802 static int 2803 mesh_send_action_meshpeering_confirm(struct ieee80211_node *ni, 2804 int category, int action, void *args0) 2805 { 2806 struct ieee80211vap *vap = ni->ni_vap; 2807 struct ieee80211com *ic = ni->ni_ic; 2808 uint16_t *args = args0; 2809 const struct ieee80211_rateset *rs; 2810 struct mbuf *m; 2811 uint8_t *frm; 2812 2813 IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH, ni, 2814 "send PEER CONFIRM action: localid 0x%x, peerid 0x%x", 2815 args[0], args[1]); 2816 2817 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE, 2818 "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", __func__, __LINE__, 2819 ni, ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)+1); 2820 ieee80211_ref_node(ni); 2821 2822 m = ieee80211_getmgtframe(&frm, 2823 ic->ic_headroom + sizeof(struct ieee80211_frame), 2824 sizeof(uint16_t) /* action+category */ 2825 + sizeof(uint16_t) /* capabilites */ 2826 + sizeof(uint16_t) /* status code */ 2827 + sizeof(uint16_t) /* AID */ 2828 + 2 + IEEE80211_RATE_SIZE 2829 + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE) 2830 + 2 + IEEE80211_MESHID_LEN 2831 + sizeof(struct ieee80211_meshconf_ie) 2832 + sizeof(struct ieee80211_meshpeer_ie) 2833 ); 2834 if (m != NULL) { 2835 /* 2836 * mesh peer confirm action frame format: 2837 * [1] category 2838 * [1] action 2839 * [2] capabilities 2840 * [2] status code 2841 * [2] association id (peer ID) 2842 * [tlv] rates 2843 * [tlv] xrates 2844 * [tlv] mesh id 2845 * [tlv] mesh conf 2846 * [tlv] mesh peer link mgmt 2847 */ 2848 *frm++ = category; 2849 *frm++ = action; 2850 ADDSHORT(frm, ieee80211_getcapinfo(vap, ni->ni_chan)); 2851 ADDSHORT(frm, 0); /* status code */ 2852 ADDSHORT(frm, args[1]); /* AID */ 2853 rs = ieee80211_get_suprates(ic, ic->ic_curchan); 2854 frm = ieee80211_add_rates(frm, rs); 2855 frm = ieee80211_add_xrates(frm, rs); 2856 frm = ieee80211_add_meshid(frm, vap); 2857 frm = ieee80211_add_meshconf(frm, vap); 2858 frm = ieee80211_add_meshpeer(frm, 2859 IEEE80211_ACTION_MESHPEERING_CONFIRM, 2860 args[0], args[1], 0); 2861 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *); 2862 return mesh_send_action(ni, vap->iv_myaddr, ni->ni_macaddr, m); 2863 } else { 2864 vap->iv_stats.is_tx_nobuf++; 2865 ieee80211_free_node(ni); 2866 return ENOMEM; 2867 } 2868 } 2869 2870 static int 2871 mesh_send_action_meshpeering_close(struct ieee80211_node *ni, 2872 int category, int action, void *args0) 2873 { 2874 struct ieee80211vap *vap = ni->ni_vap; 2875 struct ieee80211com *ic = ni->ni_ic; 2876 uint16_t *args = args0; 2877 struct mbuf *m; 2878 uint8_t *frm; 2879 2880 IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH, ni, 2881 "send PEER CLOSE action: localid 0x%x, peerid 0x%x reason %d", 2882 args[0], args[1], args[2]); 2883 2884 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE, 2885 "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", __func__, __LINE__, 2886 ni, ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)+1); 2887 ieee80211_ref_node(ni); 2888 2889 m = ieee80211_getmgtframe(&frm, 2890 ic->ic_headroom + sizeof(struct ieee80211_frame), 2891 sizeof(uint16_t) /* action+category */ 2892 + sizeof(uint16_t) /* reason code */ 2893 + 2 + IEEE80211_MESHID_LEN 2894 + sizeof(struct ieee80211_meshpeer_ie) 2895 ); 2896 if (m != NULL) { 2897 /* 2898 * mesh peer close action frame format: 2899 * [1] category 2900 * [1] action 2901 * [tlv] mesh id 2902 * [tlv] mesh peer link mgmt 2903 */ 2904 *frm++ = category; 2905 *frm++ = action; 2906 frm = ieee80211_add_meshid(frm, vap); 2907 frm = ieee80211_add_meshpeer(frm, 2908 IEEE80211_ACTION_MESHPEERING_CLOSE, 2909 args[0], args[1], args[2]); 2910 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *); 2911 return mesh_send_action(ni, vap->iv_myaddr, ni->ni_macaddr, m); 2912 } else { 2913 vap->iv_stats.is_tx_nobuf++; 2914 ieee80211_free_node(ni); 2915 return ENOMEM; 2916 } 2917 } 2918 2919 static int 2920 mesh_send_action_meshlmetric(struct ieee80211_node *ni, 2921 int category, int action, void *arg0) 2922 { 2923 struct ieee80211vap *vap = ni->ni_vap; 2924 struct ieee80211com *ic = ni->ni_ic; 2925 struct ieee80211_meshlmetric_ie *ie = arg0; 2926 struct mbuf *m; 2927 uint8_t *frm; 2928 2929 if (ie->lm_flags & IEEE80211_MESH_LMETRIC_FLAGS_REQ) { 2930 IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH, 2931 ni, "%s", "send LINK METRIC REQUEST action"); 2932 } else { 2933 IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH, 2934 ni, "send LINK METRIC REPLY action: metric 0x%x", 2935 ie->lm_metric); 2936 } 2937 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE, 2938 "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", __func__, __LINE__, 2939 ni, ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)+1); 2940 ieee80211_ref_node(ni); 2941 2942 m = ieee80211_getmgtframe(&frm, 2943 ic->ic_headroom + sizeof(struct ieee80211_frame), 2944 sizeof(uint16_t) + /* action+category */ 2945 sizeof(struct ieee80211_meshlmetric_ie) 2946 ); 2947 if (m != NULL) { 2948 /* 2949 * mesh link metric 2950 * [1] category 2951 * [1] action 2952 * [tlv] mesh link metric 2953 */ 2954 *frm++ = category; 2955 *frm++ = action; 2956 frm = ieee80211_add_meshlmetric(frm, 2957 ie->lm_flags, ie->lm_metric); 2958 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *); 2959 return mesh_send_action(ni, vap->iv_myaddr, ni->ni_macaddr, m); 2960 } else { 2961 vap->iv_stats.is_tx_nobuf++; 2962 ieee80211_free_node(ni); 2963 return ENOMEM; 2964 } 2965 } 2966 2967 static int 2968 mesh_send_action_meshgate(struct ieee80211_node *ni, 2969 int category, int action, void *arg0) 2970 { 2971 struct ieee80211vap *vap = ni->ni_vap; 2972 struct ieee80211com *ic = ni->ni_ic; 2973 struct ieee80211_meshgann_ie *ie = arg0; 2974 struct mbuf *m; 2975 uint8_t *frm; 2976 2977 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE, 2978 "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", __func__, __LINE__, 2979 ni, ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)+1); 2980 ieee80211_ref_node(ni); 2981 2982 m = ieee80211_getmgtframe(&frm, 2983 ic->ic_headroom + sizeof(struct ieee80211_frame), 2984 sizeof(uint16_t) + /* action+category */ 2985 IEEE80211_MESHGANN_BASE_SZ 2986 ); 2987 if (m != NULL) { 2988 /* 2989 * mesh link metric 2990 * [1] category 2991 * [1] action 2992 * [tlv] mesh gate annoucement 2993 */ 2994 *frm++ = category; 2995 *frm++ = action; 2996 frm = ieee80211_add_meshgate(frm, ie); 2997 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *); 2998 return mesh_send_action(ni, vap->iv_myaddr, broadcastaddr, m); 2999 } else { 3000 vap->iv_stats.is_tx_nobuf++; 3001 ieee80211_free_node(ni); 3002 return ENOMEM; 3003 } 3004 } 3005 3006 static void 3007 mesh_peer_timeout_setup(struct ieee80211_node *ni) 3008 { 3009 switch (ni->ni_mlstate) { 3010 case IEEE80211_NODE_MESH_HOLDING: 3011 ni->ni_mltval = ieee80211_mesh_holdingtimeout; 3012 break; 3013 case IEEE80211_NODE_MESH_CONFIRMRCV: 3014 ni->ni_mltval = ieee80211_mesh_confirmtimeout; 3015 break; 3016 case IEEE80211_NODE_MESH_IDLE: 3017 ni->ni_mltval = 0; 3018 break; 3019 default: 3020 ni->ni_mltval = ieee80211_mesh_retrytimeout; 3021 break; 3022 } 3023 if (ni->ni_mltval) 3024 callout_reset(&ni->ni_mltimer, ni->ni_mltval, 3025 mesh_peer_timeout_cb, ni); 3026 } 3027 3028 /* 3029 * Same as above but backoffs timer statisically 50%. 3030 */ 3031 static void 3032 mesh_peer_timeout_backoff(struct ieee80211_node *ni) 3033 { 3034 uint32_t r; 3035 3036 r = arc4random(); 3037 ni->ni_mltval += r % ni->ni_mltval; 3038 callout_reset(&ni->ni_mltimer, ni->ni_mltval, mesh_peer_timeout_cb, 3039 ni); 3040 } 3041 3042 static __inline void 3043 mesh_peer_timeout_stop(struct ieee80211_node *ni) 3044 { 3045 callout_drain(&ni->ni_mltimer); 3046 } 3047 3048 static void 3049 mesh_peer_backoff_cb(void *arg) 3050 { 3051 struct ieee80211_node *ni = (struct ieee80211_node *)arg; 3052 3053 /* After backoff timeout, try to peer automatically again. */ 3054 ni->ni_mlhcnt = 0; 3055 } 3056 3057 /* 3058 * Mesh Peer Link Management FSM timeout handling. 3059 */ 3060 static void 3061 mesh_peer_timeout_cb(void *arg) 3062 { 3063 struct ieee80211_node *ni = (struct ieee80211_node *)arg; 3064 uint16_t args[3]; 3065 3066 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_MESH, 3067 ni, "mesh link timeout, state %d, retry counter %d", 3068 ni->ni_mlstate, ni->ni_mlrcnt); 3069 3070 switch (ni->ni_mlstate) { 3071 case IEEE80211_NODE_MESH_IDLE: 3072 case IEEE80211_NODE_MESH_ESTABLISHED: 3073 break; 3074 case IEEE80211_NODE_MESH_OPENSNT: 3075 case IEEE80211_NODE_MESH_OPENRCV: 3076 if (ni->ni_mlrcnt == ieee80211_mesh_maxretries) { 3077 args[0] = ni->ni_mlpid; 3078 args[2] = IEEE80211_REASON_MESH_MAX_RETRIES; 3079 ieee80211_send_action(ni, 3080 IEEE80211_ACTION_CAT_SELF_PROT, 3081 IEEE80211_ACTION_MESHPEERING_CLOSE, args); 3082 ni->ni_mlrcnt = 0; 3083 mesh_linkchange(ni, IEEE80211_NODE_MESH_HOLDING); 3084 mesh_peer_timeout_setup(ni); 3085 } else { 3086 args[0] = ni->ni_mlpid; 3087 ieee80211_send_action(ni, 3088 IEEE80211_ACTION_CAT_SELF_PROT, 3089 IEEE80211_ACTION_MESHPEERING_OPEN, args); 3090 ni->ni_mlrcnt++; 3091 mesh_peer_timeout_backoff(ni); 3092 } 3093 break; 3094 case IEEE80211_NODE_MESH_CONFIRMRCV: 3095 args[0] = ni->ni_mlpid; 3096 args[2] = IEEE80211_REASON_MESH_CONFIRM_TIMEOUT; 3097 ieee80211_send_action(ni, 3098 IEEE80211_ACTION_CAT_SELF_PROT, 3099 IEEE80211_ACTION_MESHPEERING_CLOSE, args); 3100 mesh_linkchange(ni, IEEE80211_NODE_MESH_HOLDING); 3101 mesh_peer_timeout_setup(ni); 3102 break; 3103 case IEEE80211_NODE_MESH_HOLDING: 3104 ni->ni_mlhcnt++; 3105 if (ni->ni_mlhcnt >= ieee80211_mesh_maxholding) 3106 callout_reset(&ni->ni_mlhtimer, 3107 ieee80211_mesh_backofftimeout, 3108 mesh_peer_backoff_cb, ni); 3109 mesh_linkchange(ni, IEEE80211_NODE_MESH_IDLE); 3110 break; 3111 } 3112 } 3113 3114 static int 3115 mesh_verify_meshid(struct ieee80211vap *vap, const uint8_t *ie) 3116 { 3117 struct ieee80211_mesh_state *ms = vap->iv_mesh; 3118 3119 if (ie == NULL || ie[1] != ms->ms_idlen) 3120 return 1; 3121 return memcmp(ms->ms_id, ie + 2, ms->ms_idlen); 3122 } 3123 3124 /* 3125 * Check if we are using the same algorithms for this mesh. 3126 */ 3127 static int 3128 mesh_verify_meshconf(struct ieee80211vap *vap, const uint8_t *ie) 3129 { 3130 const struct ieee80211_meshconf_ie *meshconf = 3131 (const struct ieee80211_meshconf_ie *) ie; 3132 const struct ieee80211_mesh_state *ms = vap->iv_mesh; 3133 3134 if (meshconf == NULL) 3135 return 1; 3136 if (meshconf->conf_pselid != ms->ms_ppath->mpp_ie) { 3137 IEEE80211_DPRINTF(vap, IEEE80211_MSG_MESH, 3138 "unknown path selection algorithm: 0x%x\n", 3139 meshconf->conf_pselid); 3140 return 1; 3141 } 3142 if (meshconf->conf_pmetid != ms->ms_pmetric->mpm_ie) { 3143 IEEE80211_DPRINTF(vap, IEEE80211_MSG_MESH, 3144 "unknown path metric algorithm: 0x%x\n", 3145 meshconf->conf_pmetid); 3146 return 1; 3147 } 3148 if (meshconf->conf_ccid != 0) { 3149 IEEE80211_DPRINTF(vap, IEEE80211_MSG_MESH, 3150 "unknown congestion control algorithm: 0x%x\n", 3151 meshconf->conf_ccid); 3152 return 1; 3153 } 3154 if (meshconf->conf_syncid != IEEE80211_MESHCONF_SYNC_NEIGHOFF) { 3155 IEEE80211_DPRINTF(vap, IEEE80211_MSG_MESH, 3156 "unknown sync algorithm: 0x%x\n", 3157 meshconf->conf_syncid); 3158 return 1; 3159 } 3160 if (meshconf->conf_authid != 0) { 3161 IEEE80211_DPRINTF(vap, IEEE80211_MSG_MESH, 3162 "unknown auth auth algorithm: 0x%x\n", 3163 meshconf->conf_pselid); 3164 return 1; 3165 } 3166 /* Not accepting peers */ 3167 if (!(meshconf->conf_cap & IEEE80211_MESHCONF_CAP_AP)) { 3168 IEEE80211_DPRINTF(vap, IEEE80211_MSG_MESH, 3169 "not accepting peers: 0x%x\n", meshconf->conf_cap); 3170 return 1; 3171 } 3172 return 0; 3173 } 3174 3175 static int 3176 mesh_verify_meshpeer(struct ieee80211vap *vap, uint8_t subtype, 3177 const uint8_t *ie) 3178 { 3179 const struct ieee80211_meshpeer_ie *meshpeer = 3180 (const struct ieee80211_meshpeer_ie *) ie; 3181 3182 if (meshpeer == NULL || 3183 meshpeer->peer_len < IEEE80211_MPM_BASE_SZ || 3184 meshpeer->peer_len > IEEE80211_MPM_MAX_SZ) 3185 return 1; 3186 if (meshpeer->peer_proto != IEEE80211_MPPID_MPM) { 3187 IEEE80211_DPRINTF(vap, 3188 IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH, 3189 "Only MPM protocol is supported (proto: 0x%02X)", 3190 meshpeer->peer_proto); 3191 return 1; 3192 } 3193 switch (subtype) { 3194 case IEEE80211_ACTION_MESHPEERING_OPEN: 3195 if (meshpeer->peer_len != IEEE80211_MPM_BASE_SZ) 3196 return 1; 3197 break; 3198 case IEEE80211_ACTION_MESHPEERING_CONFIRM: 3199 if (meshpeer->peer_len != IEEE80211_MPM_BASE_SZ + 2) 3200 return 1; 3201 break; 3202 case IEEE80211_ACTION_MESHPEERING_CLOSE: 3203 if (meshpeer->peer_len < IEEE80211_MPM_BASE_SZ + 2) 3204 return 1; 3205 if (meshpeer->peer_len == (IEEE80211_MPM_BASE_SZ + 2) && 3206 meshpeer->peer_linkid != 0) 3207 return 1; 3208 if (meshpeer->peer_rcode == 0) 3209 return 1; 3210 break; 3211 } 3212 return 0; 3213 } 3214 3215 /* 3216 * Add a Mesh ID IE to a frame. 3217 */ 3218 uint8_t * 3219 ieee80211_add_meshid(uint8_t *frm, struct ieee80211vap *vap) 3220 { 3221 struct ieee80211_mesh_state *ms = vap->iv_mesh; 3222 3223 KASSERT(vap->iv_opmode == IEEE80211_M_MBSS, ("not a mbss vap")); 3224 3225 *frm++ = IEEE80211_ELEMID_MESHID; 3226 *frm++ = ms->ms_idlen; 3227 memcpy(frm, ms->ms_id, ms->ms_idlen); 3228 return frm + ms->ms_idlen; 3229 } 3230 3231 /* 3232 * Add a Mesh Configuration IE to a frame. 3233 * For now just use HWMP routing, Airtime link metric, Null Congestion 3234 * Signaling, Null Sync Protocol and Null Authentication. 3235 */ 3236 uint8_t * 3237 ieee80211_add_meshconf(uint8_t *frm, struct ieee80211vap *vap) 3238 { 3239 const struct ieee80211_mesh_state *ms = vap->iv_mesh; 3240 uint16_t caps; 3241 3242 KASSERT(vap->iv_opmode == IEEE80211_M_MBSS, ("not a MBSS vap")); 3243 3244 *frm++ = IEEE80211_ELEMID_MESHCONF; 3245 *frm++ = IEEE80211_MESH_CONF_SZ; 3246 *frm++ = ms->ms_ppath->mpp_ie; /* path selection */ 3247 *frm++ = ms->ms_pmetric->mpm_ie; /* link metric */ 3248 *frm++ = IEEE80211_MESHCONF_CC_DISABLED; 3249 *frm++ = IEEE80211_MESHCONF_SYNC_NEIGHOFF; 3250 *frm++ = IEEE80211_MESHCONF_AUTH_DISABLED; 3251 /* NB: set the number of neighbors before the rest */ 3252 *frm = (ms->ms_neighbors > IEEE80211_MESH_MAX_NEIGHBORS ? 3253 IEEE80211_MESH_MAX_NEIGHBORS : ms->ms_neighbors) << 1; 3254 if (ms->ms_flags & IEEE80211_MESHFLAGS_GATE) 3255 *frm |= IEEE80211_MESHCONF_FORM_GATE; 3256 frm += 1; 3257 caps = 0; 3258 if (ms->ms_flags & IEEE80211_MESHFLAGS_AP) 3259 caps |= IEEE80211_MESHCONF_CAP_AP; 3260 if (ms->ms_flags & IEEE80211_MESHFLAGS_FWD) 3261 caps |= IEEE80211_MESHCONF_CAP_FWRD; 3262 *frm++ = caps; 3263 return frm; 3264 } 3265 3266 /* 3267 * Add a Mesh Peer Management IE to a frame. 3268 */ 3269 uint8_t * 3270 ieee80211_add_meshpeer(uint8_t *frm, uint8_t subtype, uint16_t localid, 3271 uint16_t peerid, uint16_t reason) 3272 { 3273 3274 KASSERT(localid != 0, ("localid == 0")); 3275 3276 *frm++ = IEEE80211_ELEMID_MESHPEER; 3277 switch (subtype) { 3278 case IEEE80211_ACTION_MESHPEERING_OPEN: 3279 *frm++ = IEEE80211_MPM_BASE_SZ; /* length */ 3280 ADDSHORT(frm, IEEE80211_MPPID_MPM); /* proto */ 3281 ADDSHORT(frm, localid); /* local ID */ 3282 break; 3283 case IEEE80211_ACTION_MESHPEERING_CONFIRM: 3284 KASSERT(peerid != 0, ("sending peer confirm without peer id")); 3285 *frm++ = IEEE80211_MPM_BASE_SZ + 2; /* length */ 3286 ADDSHORT(frm, IEEE80211_MPPID_MPM); /* proto */ 3287 ADDSHORT(frm, localid); /* local ID */ 3288 ADDSHORT(frm, peerid); /* peer ID */ 3289 break; 3290 case IEEE80211_ACTION_MESHPEERING_CLOSE: 3291 if (peerid) 3292 *frm++ = IEEE80211_MPM_MAX_SZ; /* length */ 3293 else 3294 *frm++ = IEEE80211_MPM_BASE_SZ + 2; /* length */ 3295 ADDSHORT(frm, IEEE80211_MPPID_MPM); /* proto */ 3296 ADDSHORT(frm, localid); /* local ID */ 3297 if (peerid) 3298 ADDSHORT(frm, peerid); /* peer ID */ 3299 ADDSHORT(frm, reason); 3300 break; 3301 } 3302 return frm; 3303 } 3304 3305 /* 3306 * Compute an Airtime Link Metric for the link with this node. 3307 * 3308 * Based on Draft 3.0 spec (11B.10, p.149). 3309 */ 3310 /* 3311 * Max 802.11s overhead. 3312 */ 3313 #define IEEE80211_MESH_MAXOVERHEAD \ 3314 (sizeof(struct ieee80211_qosframe_addr4) \ 3315 + sizeof(struct ieee80211_meshcntl_ae10) \ 3316 + sizeof(struct llc) \ 3317 + IEEE80211_ADDR_LEN \ 3318 + IEEE80211_WEP_IVLEN \ 3319 + IEEE80211_WEP_KIDLEN \ 3320 + IEEE80211_WEP_CRCLEN \ 3321 + IEEE80211_WEP_MICLEN \ 3322 + IEEE80211_CRC_LEN) 3323 uint32_t 3324 mesh_airtime_calc(struct ieee80211_node *ni) 3325 { 3326 #define M_BITS 8 3327 #define S_FACTOR (2 * M_BITS) 3328 struct ieee80211com *ic = ni->ni_ic; 3329 struct ifnet *ifp = ni->ni_vap->iv_ifp; 3330 const static int nbits = 8192 << M_BITS; 3331 uint32_t overhead, rate, errrate; 3332 uint64_t res; 3333 3334 /* Time to transmit a frame */ 3335 rate = ni->ni_txrate; 3336 overhead = ieee80211_compute_duration(ic->ic_rt, 3337 ifp->if_mtu + IEEE80211_MESH_MAXOVERHEAD, rate, 0) << M_BITS; 3338 /* Error rate in percentage */ 3339 /* XXX assuming small failures are ok */ 3340 errrate = (((ifp->if_get_counter(ifp, IFCOUNTER_OERRORS) + 3341 ifp->if_get_counter(ifp, IFCOUNTER_IERRORS)) / 100) << M_BITS) 3342 / 100; 3343 res = (overhead + (nbits / rate)) * 3344 ((1 << S_FACTOR) / ((1 << M_BITS) - errrate)); 3345 3346 return (uint32_t)(res >> S_FACTOR); 3347 #undef M_BITS 3348 #undef S_FACTOR 3349 } 3350 3351 /* 3352 * Add a Mesh Link Metric report IE to a frame. 3353 */ 3354 uint8_t * 3355 ieee80211_add_meshlmetric(uint8_t *frm, uint8_t flags, uint32_t metric) 3356 { 3357 *frm++ = IEEE80211_ELEMID_MESHLINK; 3358 *frm++ = 5; 3359 *frm++ = flags; 3360 ADDWORD(frm, metric); 3361 return frm; 3362 } 3363 3364 /* 3365 * Add a Mesh Gate Announcement IE to a frame. 3366 */ 3367 uint8_t * 3368 ieee80211_add_meshgate(uint8_t *frm, struct ieee80211_meshgann_ie *ie) 3369 { 3370 *frm++ = IEEE80211_ELEMID_MESHGANN; /* ie */ 3371 *frm++ = IEEE80211_MESHGANN_BASE_SZ; /* len */ 3372 *frm++ = ie->gann_flags; 3373 *frm++ = ie->gann_hopcount; 3374 *frm++ = ie->gann_ttl; 3375 IEEE80211_ADDR_COPY(frm, ie->gann_addr); 3376 frm += 6; 3377 ADDWORD(frm, ie->gann_seq); 3378 ADDSHORT(frm, ie->gann_interval); 3379 return frm; 3380 } 3381 #undef ADDSHORT 3382 #undef ADDWORD 3383 3384 /* 3385 * Initialize any mesh-specific node state. 3386 */ 3387 void 3388 ieee80211_mesh_node_init(struct ieee80211vap *vap, struct ieee80211_node *ni) 3389 { 3390 ni->ni_flags |= IEEE80211_NODE_QOS; 3391 callout_init(&ni->ni_mltimer, 1); 3392 callout_init(&ni->ni_mlhtimer, 1); 3393 } 3394 3395 /* 3396 * Cleanup any mesh-specific node state. 3397 */ 3398 void 3399 ieee80211_mesh_node_cleanup(struct ieee80211_node *ni) 3400 { 3401 struct ieee80211vap *vap = ni->ni_vap; 3402 struct ieee80211_mesh_state *ms = vap->iv_mesh; 3403 3404 callout_drain(&ni->ni_mltimer); 3405 callout_drain(&ni->ni_mlhtimer); 3406 /* NB: short-circuit callbacks after mesh_vdetach */ 3407 if (vap->iv_mesh != NULL) 3408 ms->ms_ppath->mpp_peerdown(ni); 3409 } 3410 3411 void 3412 ieee80211_parse_meshid(struct ieee80211_node *ni, const uint8_t *ie) 3413 { 3414 ni->ni_meshidlen = ie[1]; 3415 memcpy(ni->ni_meshid, ie + 2, ie[1]); 3416 } 3417 3418 /* 3419 * Setup mesh-specific node state on neighbor discovery. 3420 */ 3421 void 3422 ieee80211_mesh_init_neighbor(struct ieee80211_node *ni, 3423 const struct ieee80211_frame *wh, 3424 const struct ieee80211_scanparams *sp) 3425 { 3426 ieee80211_parse_meshid(ni, sp->meshid); 3427 } 3428 3429 void 3430 ieee80211_mesh_update_beacon(struct ieee80211vap *vap, 3431 struct ieee80211_beacon_offsets *bo) 3432 { 3433 KASSERT(vap->iv_opmode == IEEE80211_M_MBSS, ("not a MBSS vap")); 3434 3435 if (isset(bo->bo_flags, IEEE80211_BEACON_MESHCONF)) { 3436 (void)ieee80211_add_meshconf(bo->bo_meshconf, vap); 3437 clrbit(bo->bo_flags, IEEE80211_BEACON_MESHCONF); 3438 } 3439 } 3440 3441 static int 3442 mesh_ioctl_get80211(struct ieee80211vap *vap, struct ieee80211req *ireq) 3443 { 3444 struct ieee80211_mesh_state *ms = vap->iv_mesh; 3445 uint8_t tmpmeshid[IEEE80211_NWID_LEN]; 3446 struct ieee80211_mesh_route *rt; 3447 struct ieee80211req_mesh_route *imr; 3448 size_t len, off; 3449 uint8_t *p; 3450 int error; 3451 3452 if (vap->iv_opmode != IEEE80211_M_MBSS) 3453 return ENOSYS; 3454 3455 error = 0; 3456 switch (ireq->i_type) { 3457 case IEEE80211_IOC_MESH_ID: 3458 ireq->i_len = ms->ms_idlen; 3459 memcpy(tmpmeshid, ms->ms_id, ireq->i_len); 3460 error = copyout(tmpmeshid, ireq->i_data, ireq->i_len); 3461 break; 3462 case IEEE80211_IOC_MESH_AP: 3463 ireq->i_val = (ms->ms_flags & IEEE80211_MESHFLAGS_AP) != 0; 3464 break; 3465 case IEEE80211_IOC_MESH_FWRD: 3466 ireq->i_val = (ms->ms_flags & IEEE80211_MESHFLAGS_FWD) != 0; 3467 break; 3468 case IEEE80211_IOC_MESH_GATE: 3469 ireq->i_val = (ms->ms_flags & IEEE80211_MESHFLAGS_GATE) != 0; 3470 break; 3471 case IEEE80211_IOC_MESH_TTL: 3472 ireq->i_val = ms->ms_ttl; 3473 break; 3474 case IEEE80211_IOC_MESH_RTCMD: 3475 switch (ireq->i_val) { 3476 case IEEE80211_MESH_RTCMD_LIST: 3477 len = 0; 3478 MESH_RT_LOCK(ms); 3479 TAILQ_FOREACH(rt, &ms->ms_routes, rt_next) { 3480 len += sizeof(*imr); 3481 } 3482 MESH_RT_UNLOCK(ms); 3483 if (len > ireq->i_len || ireq->i_len < sizeof(*imr)) { 3484 ireq->i_len = len; 3485 return ENOMEM; 3486 } 3487 ireq->i_len = len; 3488 /* XXX M_WAIT? */ 3489 p = IEEE80211_MALLOC(len, M_TEMP, 3490 IEEE80211_M_NOWAIT | IEEE80211_M_ZERO); 3491 if (p == NULL) 3492 return ENOMEM; 3493 off = 0; 3494 MESH_RT_LOCK(ms); 3495 TAILQ_FOREACH(rt, &ms->ms_routes, rt_next) { 3496 if (off >= len) 3497 break; 3498 imr = (struct ieee80211req_mesh_route *) 3499 (p + off); 3500 IEEE80211_ADDR_COPY(imr->imr_dest, 3501 rt->rt_dest); 3502 IEEE80211_ADDR_COPY(imr->imr_nexthop, 3503 rt->rt_nexthop); 3504 imr->imr_metric = rt->rt_metric; 3505 imr->imr_nhops = rt->rt_nhops; 3506 imr->imr_lifetime = 3507 ieee80211_mesh_rt_update(rt, 0); 3508 imr->imr_lastmseq = rt->rt_lastmseq; 3509 imr->imr_flags = rt->rt_flags; /* last */ 3510 off += sizeof(*imr); 3511 } 3512 MESH_RT_UNLOCK(ms); 3513 error = copyout(p, (uint8_t *)ireq->i_data, 3514 ireq->i_len); 3515 IEEE80211_FREE(p, M_TEMP); 3516 break; 3517 case IEEE80211_MESH_RTCMD_FLUSH: 3518 case IEEE80211_MESH_RTCMD_ADD: 3519 case IEEE80211_MESH_RTCMD_DELETE: 3520 return EINVAL; 3521 default: 3522 return ENOSYS; 3523 } 3524 break; 3525 case IEEE80211_IOC_MESH_PR_METRIC: 3526 len = strlen(ms->ms_pmetric->mpm_descr); 3527 if (ireq->i_len < len) 3528 return EINVAL; 3529 ireq->i_len = len; 3530 error = copyout(ms->ms_pmetric->mpm_descr, 3531 (uint8_t *)ireq->i_data, len); 3532 break; 3533 case IEEE80211_IOC_MESH_PR_PATH: 3534 len = strlen(ms->ms_ppath->mpp_descr); 3535 if (ireq->i_len < len) 3536 return EINVAL; 3537 ireq->i_len = len; 3538 error = copyout(ms->ms_ppath->mpp_descr, 3539 (uint8_t *)ireq->i_data, len); 3540 break; 3541 default: 3542 return ENOSYS; 3543 } 3544 3545 return error; 3546 } 3547 IEEE80211_IOCTL_GET(mesh, mesh_ioctl_get80211); 3548 3549 static int 3550 mesh_ioctl_set80211(struct ieee80211vap *vap, struct ieee80211req *ireq) 3551 { 3552 struct ieee80211_mesh_state *ms = vap->iv_mesh; 3553 uint8_t tmpmeshid[IEEE80211_NWID_LEN]; 3554 uint8_t tmpaddr[IEEE80211_ADDR_LEN]; 3555 char tmpproto[IEEE80211_MESH_PROTO_DSZ]; 3556 int error; 3557 3558 if (vap->iv_opmode != IEEE80211_M_MBSS) 3559 return ENOSYS; 3560 3561 error = 0; 3562 switch (ireq->i_type) { 3563 case IEEE80211_IOC_MESH_ID: 3564 if (ireq->i_val != 0 || ireq->i_len > IEEE80211_MESHID_LEN) 3565 return EINVAL; 3566 error = copyin(ireq->i_data, tmpmeshid, ireq->i_len); 3567 if (error != 0) 3568 break; 3569 memset(ms->ms_id, 0, IEEE80211_NWID_LEN); 3570 ms->ms_idlen = ireq->i_len; 3571 memcpy(ms->ms_id, tmpmeshid, ireq->i_len); 3572 error = ENETRESET; 3573 break; 3574 case IEEE80211_IOC_MESH_AP: 3575 if (ireq->i_val) 3576 ms->ms_flags |= IEEE80211_MESHFLAGS_AP; 3577 else 3578 ms->ms_flags &= ~IEEE80211_MESHFLAGS_AP; 3579 error = ENETRESET; 3580 break; 3581 case IEEE80211_IOC_MESH_FWRD: 3582 if (ireq->i_val) 3583 ms->ms_flags |= IEEE80211_MESHFLAGS_FWD; 3584 else 3585 ms->ms_flags &= ~IEEE80211_MESHFLAGS_FWD; 3586 mesh_gatemode_setup(vap); 3587 break; 3588 case IEEE80211_IOC_MESH_GATE: 3589 if (ireq->i_val) 3590 ms->ms_flags |= IEEE80211_MESHFLAGS_GATE; 3591 else 3592 ms->ms_flags &= ~IEEE80211_MESHFLAGS_GATE; 3593 break; 3594 case IEEE80211_IOC_MESH_TTL: 3595 ms->ms_ttl = (uint8_t) ireq->i_val; 3596 break; 3597 case IEEE80211_IOC_MESH_RTCMD: 3598 switch (ireq->i_val) { 3599 case IEEE80211_MESH_RTCMD_LIST: 3600 return EINVAL; 3601 case IEEE80211_MESH_RTCMD_FLUSH: 3602 ieee80211_mesh_rt_flush(vap); 3603 break; 3604 case IEEE80211_MESH_RTCMD_ADD: 3605 if (IEEE80211_ADDR_EQ(vap->iv_myaddr, ireq->i_data) || 3606 IEEE80211_ADDR_EQ(broadcastaddr, ireq->i_data)) 3607 return EINVAL; 3608 error = copyin(ireq->i_data, &tmpaddr, 3609 IEEE80211_ADDR_LEN); 3610 if (error == 0) 3611 ieee80211_mesh_discover(vap, tmpaddr, NULL); 3612 break; 3613 case IEEE80211_MESH_RTCMD_DELETE: 3614 ieee80211_mesh_rt_del(vap, ireq->i_data); 3615 break; 3616 default: 3617 return ENOSYS; 3618 } 3619 break; 3620 case IEEE80211_IOC_MESH_PR_METRIC: 3621 error = copyin(ireq->i_data, tmpproto, sizeof(tmpproto)); 3622 if (error == 0) { 3623 error = mesh_select_proto_metric(vap, tmpproto); 3624 if (error == 0) 3625 error = ENETRESET; 3626 } 3627 break; 3628 case IEEE80211_IOC_MESH_PR_PATH: 3629 error = copyin(ireq->i_data, tmpproto, sizeof(tmpproto)); 3630 if (error == 0) { 3631 error = mesh_select_proto_path(vap, tmpproto); 3632 if (error == 0) 3633 error = ENETRESET; 3634 } 3635 break; 3636 default: 3637 return ENOSYS; 3638 } 3639 return error; 3640 } 3641 IEEE80211_IOCTL_SET(mesh, mesh_ioctl_set80211); 3642