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