1 /*- 2 * Copyright (c) 2009 The FreeBSD Foundation 3 * All rights reserved. 4 * 5 * This software was developed by Rui Paulo under sponsorship from the 6 * FreeBSD Foundation. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 */ 29 #include <sys/cdefs.h> 30 #ifdef __FreeBSD__ 31 __FBSDID("$FreeBSD$"); 32 #endif 33 34 /* 35 * IEEE 802.11s Hybrid Wireless Mesh Protocol, HWMP. 36 * 37 * Based on March 2009, D3.0 802.11s draft spec. 38 */ 39 #include "opt_inet.h" 40 #include "opt_wlan.h" 41 42 #include <sys/param.h> 43 #include <sys/systm.h> 44 #include <sys/mbuf.h> 45 #include <sys/malloc.h> 46 #include <sys/kernel.h> 47 48 #include <sys/socket.h> 49 #include <sys/sockio.h> 50 #include <sys/endian.h> 51 #include <sys/errno.h> 52 #include <sys/proc.h> 53 #include <sys/sysctl.h> 54 55 #include <net/if.h> 56 #include <net/if_media.h> 57 #include <net/if_llc.h> 58 #include <net/ethernet.h> 59 60 #include <net/bpf.h> 61 62 #include <net80211/ieee80211_var.h> 63 #include <net80211/ieee80211_action.h> 64 #include <net80211/ieee80211_input.h> 65 #include <net80211/ieee80211_mesh.h> 66 67 static void hwmp_vattach(struct ieee80211vap *); 68 static void hwmp_vdetach(struct ieee80211vap *); 69 static int hwmp_newstate(struct ieee80211vap *, 70 enum ieee80211_state, int); 71 static int hwmp_send_action(struct ieee80211vap *, 72 const uint8_t [IEEE80211_ADDR_LEN], 73 uint8_t *, size_t); 74 static uint8_t * hwmp_add_meshpreq(uint8_t *, 75 const struct ieee80211_meshpreq_ie *); 76 static uint8_t * hwmp_add_meshprep(uint8_t *, 77 const struct ieee80211_meshprep_ie *); 78 static uint8_t * hwmp_add_meshperr(uint8_t *, 79 const struct ieee80211_meshperr_ie *); 80 static uint8_t * hwmp_add_meshrann(uint8_t *, 81 const struct ieee80211_meshrann_ie *); 82 static void hwmp_rootmode_setup(struct ieee80211vap *); 83 static void hwmp_rootmode_cb(void *); 84 static void hwmp_rootmode_rann_cb(void *); 85 static void hwmp_recv_preq(struct ieee80211vap *, struct ieee80211_node *, 86 const struct ieee80211_frame *, 87 const struct ieee80211_meshpreq_ie *); 88 static int hwmp_send_preq(struct ieee80211vap *, 89 const uint8_t [IEEE80211_ADDR_LEN], 90 struct ieee80211_meshpreq_ie *, 91 struct timeval *, struct timeval *); 92 static void hwmp_recv_prep(struct ieee80211vap *, struct ieee80211_node *, 93 const struct ieee80211_frame *, 94 const struct ieee80211_meshprep_ie *); 95 static int hwmp_send_prep(struct ieee80211vap *, 96 const uint8_t [IEEE80211_ADDR_LEN], 97 struct ieee80211_meshprep_ie *); 98 static void hwmp_recv_perr(struct ieee80211vap *, struct ieee80211_node *, 99 const struct ieee80211_frame *, 100 const struct ieee80211_meshperr_ie *); 101 static int hwmp_send_perr(struct ieee80211vap *, 102 const uint8_t [IEEE80211_ADDR_LEN], 103 struct ieee80211_meshperr_ie *); 104 static void hwmp_senderror(struct ieee80211vap *, 105 const uint8_t [IEEE80211_ADDR_LEN], 106 struct ieee80211_mesh_route *, int); 107 static void hwmp_recv_rann(struct ieee80211vap *, struct ieee80211_node *, 108 const struct ieee80211_frame *, 109 const struct ieee80211_meshrann_ie *); 110 static int hwmp_send_rann(struct ieee80211vap *, 111 const uint8_t [IEEE80211_ADDR_LEN], 112 struct ieee80211_meshrann_ie *); 113 static struct ieee80211_node * 114 hwmp_discover(struct ieee80211vap *, 115 const uint8_t [IEEE80211_ADDR_LEN], struct mbuf *); 116 static void hwmp_peerdown(struct ieee80211_node *); 117 118 static struct timeval ieee80211_hwmp_preqminint = { 0, 100000 }; 119 static struct timeval ieee80211_hwmp_perrminint = { 0, 100000 }; 120 121 /* unalligned little endian access */ 122 #define LE_WRITE_2(p, v) do { \ 123 ((uint8_t *)(p))[0] = (v) & 0xff; \ 124 ((uint8_t *)(p))[1] = ((v) >> 8) & 0xff; \ 125 } while (0) 126 #define LE_WRITE_4(p, v) do { \ 127 ((uint8_t *)(p))[0] = (v) & 0xff; \ 128 ((uint8_t *)(p))[1] = ((v) >> 8) & 0xff; \ 129 ((uint8_t *)(p))[2] = ((v) >> 16) & 0xff; \ 130 ((uint8_t *)(p))[3] = ((v) >> 24) & 0xff; \ 131 } while (0) 132 133 134 /* NB: the Target Address set in a Proactive PREQ is the broadcast address. */ 135 static const uint8_t broadcastaddr[IEEE80211_ADDR_LEN] = 136 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; 137 138 typedef uint32_t ieee80211_hwmp_seq; 139 #define HWMP_SEQ_LT(a, b) ((int32_t)((a)-(b)) < 0) 140 #define HWMP_SEQ_LEQ(a, b) ((int32_t)((a)-(b)) <= 0) 141 #define HWMP_SEQ_EQ(a, b) ((int32_t)((a)-(b)) == 0) 142 #define HWMP_SEQ_GT(a, b) ((int32_t)((a)-(b)) > 0) 143 #define HWMP_SEQ_GEQ(a, b) ((int32_t)((a)-(b)) >= 0) 144 145 #define HWMP_SEQ_MAX(a, b) (a > b ? a : b) 146 147 /* 148 * Private extension of ieee80211_mesh_route. 149 */ 150 struct ieee80211_hwmp_route { 151 ieee80211_hwmp_seq hr_seq; /* last HWMP seq seen from dst*/ 152 ieee80211_hwmp_seq hr_preqid; /* last PREQ ID seen from dst */ 153 ieee80211_hwmp_seq hr_origseq; /* seq. no. on our latest PREQ*/ 154 struct timeval hr_lastpreq; /* last time we sent a PREQ */ 155 struct timeval hr_lastrootconf; /* last sent PREQ root conf */ 156 int hr_preqretries; /* number of discoveries */ 157 int hr_lastdiscovery; /* last discovery in ticks */ 158 }; 159 struct ieee80211_hwmp_state { 160 ieee80211_hwmp_seq hs_seq; /* next seq to be used */ 161 ieee80211_hwmp_seq hs_preqid; /* next PREQ ID to be used */ 162 int hs_rootmode; /* proactive HWMP */ 163 struct timeval hs_lastperr; /* last time we sent a PERR */ 164 struct callout hs_roottimer; 165 uint8_t hs_maxhops; /* max hop count */ 166 }; 167 168 static SYSCTL_NODE(_net_wlan, OID_AUTO, hwmp, CTLFLAG_RD, 0, 169 "IEEE 802.11s HWMP parameters"); 170 static int ieee80211_hwmp_targetonly = 0; 171 SYSCTL_INT(_net_wlan_hwmp, OID_AUTO, targetonly, CTLFLAG_RW, 172 &ieee80211_hwmp_targetonly, 0, "Set TO bit on generated PREQs"); 173 static int ieee80211_hwmp_pathtimeout = -1; 174 SYSCTL_PROC(_net_wlan_hwmp, OID_AUTO, pathlifetime, CTLTYPE_INT | CTLFLAG_RW, 175 &ieee80211_hwmp_pathtimeout, 0, ieee80211_sysctl_msecs_ticks, "I", 176 "path entry lifetime (ms)"); 177 static int ieee80211_hwmp_maxpreq_retries = -1; 178 SYSCTL_PROC(_net_wlan_hwmp, OID_AUTO, maxpreq_retries, CTLTYPE_INT | CTLFLAG_RW, 179 &ieee80211_hwmp_maxpreq_retries, 0, ieee80211_sysctl_msecs_ticks, "I", 180 "maximum number of preq retries"); 181 static int ieee80211_hwmp_net_diameter_traversaltime = -1; 182 SYSCTL_PROC(_net_wlan_hwmp, OID_AUTO, net_diameter_traversal_time, 183 CTLTYPE_INT | CTLFLAG_RW, &ieee80211_hwmp_net_diameter_traversaltime, 0, 184 ieee80211_sysctl_msecs_ticks, "I", 185 "estimate travelse time across the MBSS (ms)"); 186 static int ieee80211_hwmp_roottimeout = -1; 187 SYSCTL_PROC(_net_wlan_hwmp, OID_AUTO, roottimeout, CTLTYPE_INT | CTLFLAG_RW, 188 &ieee80211_hwmp_roottimeout, 0, ieee80211_sysctl_msecs_ticks, "I", 189 "root PREQ timeout (ms)"); 190 static int ieee80211_hwmp_rootint = -1; 191 SYSCTL_PROC(_net_wlan_hwmp, OID_AUTO, rootint, CTLTYPE_INT | CTLFLAG_RW, 192 &ieee80211_hwmp_rootint, 0, ieee80211_sysctl_msecs_ticks, "I", 193 "root interval (ms)"); 194 static int ieee80211_hwmp_rannint = -1; 195 SYSCTL_PROC(_net_wlan_hwmp, OID_AUTO, rannint, CTLTYPE_INT | CTLFLAG_RW, 196 &ieee80211_hwmp_rannint, 0, ieee80211_sysctl_msecs_ticks, "I", 197 "root announcement interval (ms)"); 198 static struct timeval ieee80211_hwmp_rootconfint = { 0, 0 }; 199 static int ieee80211_hwmp_rootconfint_internal = -1; 200 SYSCTL_PROC(_net_wlan_hwmp, OID_AUTO, rootconfint, CTLTYPE_INT | CTLFLAG_RD, 201 &ieee80211_hwmp_rootconfint_internal, 0, ieee80211_sysctl_msecs_ticks, "I", 202 "root confirmation interval (ms) (read-only)"); 203 204 #define IEEE80211_HWMP_DEFAULT_MAXHOPS 31 205 206 static ieee80211_recv_action_func hwmp_recv_action_meshpath; 207 208 static struct ieee80211_mesh_proto_path mesh_proto_hwmp = { 209 .mpp_descr = "HWMP", 210 .mpp_ie = IEEE80211_MESHCONF_PATH_HWMP, 211 .mpp_discover = hwmp_discover, 212 .mpp_peerdown = hwmp_peerdown, 213 .mpp_senderror = hwmp_senderror, 214 .mpp_vattach = hwmp_vattach, 215 .mpp_vdetach = hwmp_vdetach, 216 .mpp_newstate = hwmp_newstate, 217 .mpp_privlen = sizeof(struct ieee80211_hwmp_route), 218 }; 219 SYSCTL_PROC(_net_wlan_hwmp, OID_AUTO, inact, CTLTYPE_INT | CTLFLAG_RW, 220 &mesh_proto_hwmp.mpp_inact, 0, ieee80211_sysctl_msecs_ticks, "I", 221 "mesh route inactivity timeout (ms)"); 222 223 224 static void 225 ieee80211_hwmp_init(void) 226 { 227 /* Default values as per amendment */ 228 ieee80211_hwmp_pathtimeout = msecs_to_ticks(5*1000); 229 ieee80211_hwmp_roottimeout = msecs_to_ticks(5*1000); 230 ieee80211_hwmp_rootint = msecs_to_ticks(2*1000); 231 ieee80211_hwmp_rannint = msecs_to_ticks(1*1000); 232 ieee80211_hwmp_rootconfint_internal = msecs_to_ticks(2*1000); 233 ieee80211_hwmp_maxpreq_retries = 3; 234 /* 235 * (TU): A measurement of time equal to 1024 μs, 236 * 500 TU is 512 ms. 237 */ 238 ieee80211_hwmp_net_diameter_traversaltime = msecs_to_ticks(512); 239 240 /* 241 * NB: I dont know how to make SYSCTL_PROC that calls ms to ticks 242 * and return a struct timeval... 243 */ 244 ieee80211_hwmp_rootconfint.tv_usec = 245 ieee80211_hwmp_rootconfint_internal * 1000; 246 247 /* 248 * Register action frame handler. 249 */ 250 ieee80211_recv_action_register(IEEE80211_ACTION_CAT_MESH, 251 IEEE80211_ACTION_MESH_HWMP, hwmp_recv_action_meshpath); 252 253 /* NB: default is 5 secs per spec */ 254 mesh_proto_hwmp.mpp_inact = msecs_to_ticks(5*1000); 255 256 /* 257 * Register HWMP. 258 */ 259 ieee80211_mesh_register_proto_path(&mesh_proto_hwmp); 260 } 261 SYSINIT(wlan_hwmp, SI_SUB_DRIVERS, SI_ORDER_SECOND, ieee80211_hwmp_init, NULL); 262 263 static void 264 hwmp_vattach(struct ieee80211vap *vap) 265 { 266 struct ieee80211_hwmp_state *hs; 267 268 KASSERT(vap->iv_opmode == IEEE80211_M_MBSS, 269 ("not a mesh vap, opmode %d", vap->iv_opmode)); 270 271 hs = IEEE80211_MALLOC(sizeof(struct ieee80211_hwmp_state), M_80211_VAP, 272 IEEE80211_M_NOWAIT | IEEE80211_M_ZERO); 273 if (hs == NULL) { 274 printf("%s: couldn't alloc HWMP state\n", __func__); 275 return; 276 } 277 hs->hs_maxhops = IEEE80211_HWMP_DEFAULT_MAXHOPS; 278 callout_init(&hs->hs_roottimer, 1); 279 vap->iv_hwmp = hs; 280 } 281 282 static void 283 hwmp_vdetach(struct ieee80211vap *vap) 284 { 285 struct ieee80211_hwmp_state *hs = vap->iv_hwmp; 286 287 callout_drain(&hs->hs_roottimer); 288 IEEE80211_FREE(vap->iv_hwmp, M_80211_VAP); 289 vap->iv_hwmp = NULL; 290 } 291 292 static int 293 hwmp_newstate(struct ieee80211vap *vap, enum ieee80211_state ostate, int arg) 294 { 295 enum ieee80211_state nstate = vap->iv_state; 296 struct ieee80211_hwmp_state *hs = vap->iv_hwmp; 297 298 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE, "%s: %s -> %s (%d)\n", 299 __func__, ieee80211_state_name[ostate], 300 ieee80211_state_name[nstate], arg); 301 302 if (nstate != IEEE80211_S_RUN && ostate == IEEE80211_S_RUN) 303 callout_drain(&hs->hs_roottimer); 304 if (nstate == IEEE80211_S_RUN) 305 hwmp_rootmode_setup(vap); 306 return 0; 307 } 308 309 /* 310 * Verify the length of an HWMP PREQ and return the number 311 * of destinations >= 1, if verification fails -1 is returned. 312 */ 313 static int 314 verify_mesh_preq_len(struct ieee80211vap *vap, 315 const struct ieee80211_frame *wh, const uint8_t *iefrm) 316 { 317 int alloc_sz = -1; 318 int ndest = -1; 319 if (iefrm[2] & IEEE80211_MESHPREQ_FLAGS_AE) { 320 /* Originator External Address present */ 321 alloc_sz = IEEE80211_MESHPREQ_BASE_SZ_AE; 322 ndest = iefrm[IEEE80211_MESHPREQ_TCNT_OFFSET_AE]; 323 } else { 324 /* w/o Originator External Address */ 325 alloc_sz = IEEE80211_MESHPREQ_BASE_SZ; 326 ndest = iefrm[IEEE80211_MESHPREQ_TCNT_OFFSET]; 327 } 328 alloc_sz += ndest * IEEE80211_MESHPREQ_TRGT_SZ; 329 330 if(iefrm[1] != (alloc_sz)) { 331 IEEE80211_DISCARD(vap, 332 IEEE80211_MSG_ACTION | IEEE80211_MSG_HWMP, 333 wh, NULL, "PREQ (AE=%s) with wrong len", 334 iefrm[2] & IEEE80211_MESHPREQ_FLAGS_AE ? "1" : "0"); 335 return (-1); 336 } 337 return ndest; 338 } 339 340 /* 341 * Verify the length of an HWMP PREP and returns 1 on success, 342 * otherwise -1. 343 */ 344 static int 345 verify_mesh_prep_len(struct ieee80211vap *vap, 346 const struct ieee80211_frame *wh, const uint8_t *iefrm) 347 { 348 int alloc_sz = -1; 349 if (iefrm[2] & IEEE80211_MESHPREP_FLAGS_AE) { 350 if (iefrm[1] == IEEE80211_MESHPREP_BASE_SZ_AE) 351 alloc_sz = IEEE80211_MESHPREP_BASE_SZ_AE; 352 } else if (iefrm[1] == IEEE80211_MESHPREP_BASE_SZ) 353 alloc_sz = IEEE80211_MESHPREP_BASE_SZ; 354 if(alloc_sz < 0) { 355 IEEE80211_DISCARD(vap, 356 IEEE80211_MSG_ACTION | IEEE80211_MSG_HWMP, 357 wh, NULL, "PREP (AE=%s) with wrong len", 358 iefrm[2] & IEEE80211_MESHPREP_FLAGS_AE ? "1" : "0"); 359 return (-1); 360 } 361 return (1); 362 } 363 364 /* 365 * Verify the length of an HWMP PERR and return the number 366 * of destinations >= 1, if verification fails -1 is returned. 367 */ 368 static int 369 verify_mesh_perr_len(struct ieee80211vap *vap, 370 const struct ieee80211_frame *wh, const uint8_t *iefrm) 371 { 372 int alloc_sz = -1; 373 const uint8_t *iefrm_t = iefrm; 374 uint8_t ndest = iefrm_t[IEEE80211_MESHPERR_NDEST_OFFSET]; 375 int i; 376 377 if(ndest > IEEE80211_MESHPERR_MAXDEST) { 378 IEEE80211_DISCARD(vap, 379 IEEE80211_MSG_ACTION | IEEE80211_MSG_HWMP, 380 wh, NULL, "PERR with wrong number of destionat (>19), %u", 381 ndest); 382 return (-1); 383 } 384 385 iefrm_t += IEEE80211_MESHPERR_NDEST_OFFSET + 1; /* flag is next field */ 386 /* We need to check each destionation flag to know size */ 387 for(i = 0; i<ndest; i++) { 388 if ((*iefrm_t) & IEEE80211_MESHPERR_FLAGS_AE) 389 iefrm_t += IEEE80211_MESHPERR_DEST_SZ_AE; 390 else 391 iefrm_t += IEEE80211_MESHPERR_DEST_SZ; 392 } 393 394 alloc_sz = (iefrm_t - iefrm) - 2; /* action + code */ 395 if(alloc_sz != iefrm[1]) { 396 IEEE80211_DISCARD(vap, 397 IEEE80211_MSG_ACTION | IEEE80211_MSG_HWMP, 398 wh, NULL, "%s", "PERR with wrong len"); 399 return (-1); 400 } 401 return ndest; 402 } 403 404 static int 405 hwmp_recv_action_meshpath(struct ieee80211_node *ni, 406 const struct ieee80211_frame *wh, 407 const uint8_t *frm, const uint8_t *efrm) 408 { 409 struct ieee80211vap *vap = ni->ni_vap; 410 struct ieee80211_meshpreq_ie *preq; 411 struct ieee80211_meshprep_ie *prep; 412 struct ieee80211_meshperr_ie *perr; 413 struct ieee80211_meshrann_ie rann; 414 const uint8_t *iefrm = frm + 2; /* action + code */ 415 const uint8_t *iefrm_t = iefrm; /* temporary pointer */ 416 int ndest = -1; 417 int found = 0; 418 419 while (efrm - iefrm > 1) { 420 IEEE80211_VERIFY_LENGTH(efrm - iefrm, iefrm[1] + 2, return 0); 421 switch (*iefrm) { 422 case IEEE80211_ELEMID_MESHPREQ: 423 { 424 int i = 0; 425 426 iefrm_t = iefrm; 427 ndest = verify_mesh_preq_len(vap, wh, iefrm_t); 428 if (ndest < 0) { 429 vap->iv_stats.is_rx_mgtdiscard++; 430 break; 431 } 432 preq = IEEE80211_MALLOC(sizeof(*preq) + 433 (ndest - 1) * sizeof(*preq->preq_targets), 434 M_80211_MESH_PREQ, 435 IEEE80211_M_NOWAIT | IEEE80211_M_ZERO); 436 KASSERT(preq != NULL, ("preq == NULL")); 437 438 preq->preq_ie = *iefrm_t++; 439 preq->preq_len = *iefrm_t++; 440 preq->preq_flags = *iefrm_t++; 441 preq->preq_hopcount = *iefrm_t++; 442 preq->preq_ttl = *iefrm_t++; 443 preq->preq_id = LE_READ_4(iefrm_t); iefrm_t += 4; 444 IEEE80211_ADDR_COPY(preq->preq_origaddr, iefrm_t); 445 iefrm_t += 6; 446 preq->preq_origseq = LE_READ_4(iefrm_t); iefrm_t += 4; 447 /* NB: may have Originator Proxied Address */ 448 if (preq->preq_flags & IEEE80211_MESHPREQ_FLAGS_AE) { 449 IEEE80211_ADDR_COPY( 450 preq->preq_orig_ext_addr, iefrm_t); 451 iefrm_t += 6; 452 } 453 preq->preq_lifetime = LE_READ_4(iefrm_t); iefrm_t += 4; 454 preq->preq_metric = LE_READ_4(iefrm_t); iefrm_t += 4; 455 preq->preq_tcount = *iefrm_t++; 456 457 for (i = 0; i < preq->preq_tcount; i++) { 458 preq->preq_targets[i].target_flags = *iefrm_t++; 459 IEEE80211_ADDR_COPY( 460 preq->preq_targets[i].target_addr, iefrm_t); 461 iefrm_t += 6; 462 preq->preq_targets[i].target_seq = 463 LE_READ_4(iefrm_t); 464 iefrm_t += 4; 465 } 466 467 hwmp_recv_preq(vap, ni, wh, preq); 468 IEEE80211_FREE(preq, M_80211_MESH_PREQ); 469 found++; 470 break; 471 } 472 case IEEE80211_ELEMID_MESHPREP: 473 { 474 iefrm_t = iefrm; 475 ndest = verify_mesh_prep_len(vap, wh, iefrm_t); 476 if (ndest < 0) { 477 vap->iv_stats.is_rx_mgtdiscard++; 478 break; 479 } 480 prep = IEEE80211_MALLOC(sizeof(*prep), 481 M_80211_MESH_PREP, 482 IEEE80211_M_NOWAIT | IEEE80211_M_ZERO); 483 KASSERT(prep != NULL, ("prep == NULL")); 484 485 prep->prep_ie = *iefrm_t++; 486 prep->prep_len = *iefrm_t++; 487 prep->prep_flags = *iefrm_t++; 488 prep->prep_hopcount = *iefrm_t++; 489 prep->prep_ttl = *iefrm_t++; 490 IEEE80211_ADDR_COPY(prep->prep_targetaddr, iefrm_t); 491 iefrm_t += 6; 492 prep->prep_targetseq = LE_READ_4(iefrm_t); iefrm_t += 4; 493 /* NB: May have Target Proxied Address */ 494 if (prep->prep_flags & IEEE80211_MESHPREP_FLAGS_AE) { 495 IEEE80211_ADDR_COPY( 496 prep->prep_target_ext_addr, iefrm_t); 497 iefrm_t += 6; 498 } 499 prep->prep_lifetime = LE_READ_4(iefrm_t); iefrm_t += 4; 500 prep->prep_metric = LE_READ_4(iefrm_t); iefrm_t += 4; 501 IEEE80211_ADDR_COPY(prep->prep_origaddr, iefrm_t); 502 iefrm_t += 6; 503 prep->prep_origseq = LE_READ_4(iefrm_t); iefrm_t += 4; 504 505 hwmp_recv_prep(vap, ni, wh, prep); 506 IEEE80211_FREE(prep, M_80211_MESH_PREP); 507 found++; 508 break; 509 } 510 case IEEE80211_ELEMID_MESHPERR: 511 { 512 int i = 0; 513 514 iefrm_t = iefrm; 515 ndest = verify_mesh_perr_len(vap, wh, iefrm_t); 516 if (ndest < 0) { 517 vap->iv_stats.is_rx_mgtdiscard++; 518 break; 519 } 520 perr = IEEE80211_MALLOC(sizeof(*perr) + 521 (ndest - 1) * sizeof(*perr->perr_dests), 522 M_80211_MESH_PERR, 523 IEEE80211_M_NOWAIT | IEEE80211_M_ZERO); 524 KASSERT(perr != NULL, ("perr == NULL")); 525 526 perr->perr_ie = *iefrm_t++; 527 perr->perr_len = *iefrm_t++; 528 perr->perr_ttl = *iefrm_t++; 529 perr->perr_ndests = *iefrm_t++; 530 531 for (i = 0; i<perr->perr_ndests; i++) { 532 perr->perr_dests[i].dest_flags = *iefrm_t++; 533 IEEE80211_ADDR_COPY( 534 perr->perr_dests[i].dest_addr, iefrm_t); 535 iefrm_t += 6; 536 perr->perr_dests[i].dest_seq = LE_READ_4(iefrm_t); 537 iefrm_t += 4; 538 /* NB: May have Target Proxied Address */ 539 if (perr->perr_dests[i].dest_flags & 540 IEEE80211_MESHPERR_FLAGS_AE) { 541 IEEE80211_ADDR_COPY( 542 perr->perr_dests[i].dest_ext_addr, 543 iefrm_t); 544 iefrm_t += 6; 545 } 546 perr->perr_dests[i].dest_rcode = 547 LE_READ_2(iefrm_t); 548 iefrm_t += 2; 549 } 550 551 hwmp_recv_perr(vap, ni, wh, perr); 552 IEEE80211_FREE(perr, M_80211_MESH_PERR); 553 found++; 554 break; 555 } 556 case IEEE80211_ELEMID_MESHRANN: 557 { 558 const struct ieee80211_meshrann_ie *mrann = 559 (const struct ieee80211_meshrann_ie *) iefrm; 560 if (mrann->rann_len != 561 sizeof(struct ieee80211_meshrann_ie) - 2) { 562 IEEE80211_DISCARD(vap, 563 IEEE80211_MSG_ACTION | IEEE80211_MSG_HWMP, 564 wh, NULL, "%s", "RAN with wrong len"); 565 vap->iv_stats.is_rx_mgtdiscard++; 566 return 1; 567 } 568 memcpy(&rann, mrann, sizeof(rann)); 569 rann.rann_seq = LE_READ_4(&mrann->rann_seq); 570 rann.rann_interval = LE_READ_4(&mrann->rann_interval); 571 rann.rann_metric = LE_READ_4(&mrann->rann_metric); 572 hwmp_recv_rann(vap, ni, wh, &rann); 573 found++; 574 break; 575 } 576 } 577 iefrm += iefrm[1] + 2; 578 } 579 if (!found) { 580 IEEE80211_DISCARD(vap, 581 IEEE80211_MSG_ACTION | IEEE80211_MSG_HWMP, 582 wh, NULL, "%s", "PATH SEL action without IE"); 583 vap->iv_stats.is_rx_mgtdiscard++; 584 } 585 return 0; 586 } 587 588 static int 589 hwmp_send_action(struct ieee80211vap *vap, 590 const uint8_t da[IEEE80211_ADDR_LEN], 591 uint8_t *ie, size_t len) 592 { 593 struct ieee80211_node *ni; 594 struct ieee80211com *ic; 595 struct ieee80211_bpf_params params; 596 struct mbuf *m; 597 uint8_t *frm; 598 int ret; 599 600 if (IEEE80211_IS_MULTICAST(da)) { 601 ni = ieee80211_ref_node(vap->iv_bss); 602 #ifdef IEEE80211_DEBUG_REFCNT 603 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE, 604 "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", 605 __func__, __LINE__, 606 ni, ether_sprintf(ni->ni_macaddr), 607 ieee80211_node_refcnt(ni)+1); 608 #endif 609 ieee80211_ref_node(ni); 610 } 611 else 612 ni = ieee80211_mesh_find_txnode(vap, da); 613 614 if (vap->iv_state == IEEE80211_S_CAC) { 615 IEEE80211_NOTE(vap, IEEE80211_MSG_OUTPUT, ni, 616 "block %s frame in CAC state", "HWMP action"); 617 vap->iv_stats.is_tx_badstate++; 618 return EIO; /* XXX */ 619 } 620 621 KASSERT(ni != NULL, ("null node")); 622 ic = ni->ni_ic; 623 624 m = ieee80211_getmgtframe(&frm, 625 ic->ic_headroom + sizeof(struct ieee80211_frame), 626 sizeof(struct ieee80211_action) + len 627 ); 628 if (m == NULL) { 629 ieee80211_free_node(ni); 630 vap->iv_stats.is_tx_nobuf++; 631 return ENOMEM; 632 } 633 *frm++ = IEEE80211_ACTION_CAT_MESH; 634 *frm++ = IEEE80211_ACTION_MESH_HWMP; 635 switch (*ie) { 636 case IEEE80211_ELEMID_MESHPREQ: 637 frm = hwmp_add_meshpreq(frm, 638 (struct ieee80211_meshpreq_ie *)ie); 639 break; 640 case IEEE80211_ELEMID_MESHPREP: 641 frm = hwmp_add_meshprep(frm, 642 (struct ieee80211_meshprep_ie *)ie); 643 break; 644 case IEEE80211_ELEMID_MESHPERR: 645 frm = hwmp_add_meshperr(frm, 646 (struct ieee80211_meshperr_ie *)ie); 647 break; 648 case IEEE80211_ELEMID_MESHRANN: 649 frm = hwmp_add_meshrann(frm, 650 (struct ieee80211_meshrann_ie *)ie); 651 break; 652 } 653 654 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *); 655 M_PREPEND(m, sizeof(struct ieee80211_frame), M_NOWAIT); 656 if (m == NULL) { 657 ieee80211_free_node(ni); 658 vap->iv_stats.is_tx_nobuf++; 659 return ENOMEM; 660 } 661 662 IEEE80211_TX_LOCK(ic); 663 664 ieee80211_send_setup(ni, m, 665 IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_ACTION, 666 IEEE80211_NONQOS_TID, vap->iv_myaddr, da, vap->iv_myaddr); 667 668 m->m_flags |= M_ENCAP; /* mark encapsulated */ 669 IEEE80211_NODE_STAT(ni, tx_mgmt); 670 671 memset(¶ms, 0, sizeof(params)); 672 params.ibp_pri = WME_AC_VO; 673 params.ibp_rate0 = ni->ni_txparms->mgmtrate; 674 if (IEEE80211_IS_MULTICAST(da)) 675 params.ibp_try0 = 1; 676 else 677 params.ibp_try0 = ni->ni_txparms->maxretry; 678 params.ibp_power = ni->ni_txpower; 679 ret = ieee80211_raw_output(vap, ni, m, ¶ms); 680 IEEE80211_TX_UNLOCK(ic); 681 return (ret); 682 } 683 684 #define ADDSHORT(frm, v) do { \ 685 frm[0] = (v) & 0xff; \ 686 frm[1] = (v) >> 8; \ 687 frm += 2; \ 688 } while (0) 689 #define ADDWORD(frm, v) do { \ 690 LE_WRITE_4(frm, v); \ 691 frm += 4; \ 692 } while (0) 693 /* 694 * Add a Mesh Path Request IE to a frame. 695 */ 696 #define PREQ_TFLAGS(n) preq->preq_targets[n].target_flags 697 #define PREQ_TADDR(n) preq->preq_targets[n].target_addr 698 #define PREQ_TSEQ(n) preq->preq_targets[n].target_seq 699 static uint8_t * 700 hwmp_add_meshpreq(uint8_t *frm, const struct ieee80211_meshpreq_ie *preq) 701 { 702 int i; 703 704 *frm++ = IEEE80211_ELEMID_MESHPREQ; 705 *frm++ = preq->preq_len; /* len already calculated */ 706 *frm++ = preq->preq_flags; 707 *frm++ = preq->preq_hopcount; 708 *frm++ = preq->preq_ttl; 709 ADDWORD(frm, preq->preq_id); 710 IEEE80211_ADDR_COPY(frm, preq->preq_origaddr); frm += 6; 711 ADDWORD(frm, preq->preq_origseq); 712 if (preq->preq_flags & IEEE80211_MESHPREQ_FLAGS_AE) { 713 IEEE80211_ADDR_COPY(frm, preq->preq_orig_ext_addr); 714 frm += 6; 715 } 716 ADDWORD(frm, preq->preq_lifetime); 717 ADDWORD(frm, preq->preq_metric); 718 *frm++ = preq->preq_tcount; 719 for (i = 0; i < preq->preq_tcount; i++) { 720 *frm++ = PREQ_TFLAGS(i); 721 IEEE80211_ADDR_COPY(frm, PREQ_TADDR(i)); 722 frm += 6; 723 ADDWORD(frm, PREQ_TSEQ(i)); 724 } 725 return frm; 726 } 727 #undef PREQ_TFLAGS 728 #undef PREQ_TADDR 729 #undef PREQ_TSEQ 730 731 /* 732 * Add a Mesh Path Reply IE to a frame. 733 */ 734 static uint8_t * 735 hwmp_add_meshprep(uint8_t *frm, const struct ieee80211_meshprep_ie *prep) 736 { 737 *frm++ = IEEE80211_ELEMID_MESHPREP; 738 *frm++ = prep->prep_len; /* len already calculated */ 739 *frm++ = prep->prep_flags; 740 *frm++ = prep->prep_hopcount; 741 *frm++ = prep->prep_ttl; 742 IEEE80211_ADDR_COPY(frm, prep->prep_targetaddr); frm += 6; 743 ADDWORD(frm, prep->prep_targetseq); 744 if (prep->prep_flags & IEEE80211_MESHPREP_FLAGS_AE) { 745 IEEE80211_ADDR_COPY(frm, prep->prep_target_ext_addr); 746 frm += 6; 747 } 748 ADDWORD(frm, prep->prep_lifetime); 749 ADDWORD(frm, prep->prep_metric); 750 IEEE80211_ADDR_COPY(frm, prep->prep_origaddr); frm += 6; 751 ADDWORD(frm, prep->prep_origseq); 752 return frm; 753 } 754 755 /* 756 * Add a Mesh Path Error IE to a frame. 757 */ 758 #define PERR_DFLAGS(n) perr->perr_dests[n].dest_flags 759 #define PERR_DADDR(n) perr->perr_dests[n].dest_addr 760 #define PERR_DSEQ(n) perr->perr_dests[n].dest_seq 761 #define PERR_EXTADDR(n) perr->perr_dests[n].dest_ext_addr 762 #define PERR_DRCODE(n) perr->perr_dests[n].dest_rcode 763 static uint8_t * 764 hwmp_add_meshperr(uint8_t *frm, const struct ieee80211_meshperr_ie *perr) 765 { 766 int i; 767 768 *frm++ = IEEE80211_ELEMID_MESHPERR; 769 *frm++ = perr->perr_len; /* len already calculated */ 770 *frm++ = perr->perr_ttl; 771 *frm++ = perr->perr_ndests; 772 for (i = 0; i < perr->perr_ndests; i++) { 773 *frm++ = PERR_DFLAGS(i); 774 IEEE80211_ADDR_COPY(frm, PERR_DADDR(i)); 775 frm += 6; 776 ADDWORD(frm, PERR_DSEQ(i)); 777 if (PERR_DFLAGS(i) & IEEE80211_MESHPERR_FLAGS_AE) { 778 IEEE80211_ADDR_COPY(frm, PERR_EXTADDR(i)); 779 frm += 6; 780 } 781 ADDSHORT(frm, PERR_DRCODE(i)); 782 } 783 return frm; 784 } 785 #undef PERR_DFLAGS 786 #undef PERR_DADDR 787 #undef PERR_DSEQ 788 #undef PERR_EXTADDR 789 #undef PERR_DRCODE 790 791 /* 792 * Add a Root Annoucement IE to a frame. 793 */ 794 static uint8_t * 795 hwmp_add_meshrann(uint8_t *frm, const struct ieee80211_meshrann_ie *rann) 796 { 797 *frm++ = IEEE80211_ELEMID_MESHRANN; 798 *frm++ = rann->rann_len; 799 *frm++ = rann->rann_flags; 800 *frm++ = rann->rann_hopcount; 801 *frm++ = rann->rann_ttl; 802 IEEE80211_ADDR_COPY(frm, rann->rann_addr); frm += 6; 803 ADDWORD(frm, rann->rann_seq); 804 ADDWORD(frm, rann->rann_interval); 805 ADDWORD(frm, rann->rann_metric); 806 return frm; 807 } 808 809 static void 810 hwmp_rootmode_setup(struct ieee80211vap *vap) 811 { 812 struct ieee80211_hwmp_state *hs = vap->iv_hwmp; 813 struct ieee80211_mesh_state *ms = vap->iv_mesh; 814 815 switch (hs->hs_rootmode) { 816 case IEEE80211_HWMP_ROOTMODE_DISABLED: 817 callout_drain(&hs->hs_roottimer); 818 ms->ms_flags &= ~IEEE80211_MESHFLAGS_ROOT; 819 break; 820 case IEEE80211_HWMP_ROOTMODE_NORMAL: 821 case IEEE80211_HWMP_ROOTMODE_PROACTIVE: 822 callout_reset(&hs->hs_roottimer, ieee80211_hwmp_rootint, 823 hwmp_rootmode_cb, vap); 824 ms->ms_flags |= IEEE80211_MESHFLAGS_ROOT; 825 break; 826 case IEEE80211_HWMP_ROOTMODE_RANN: 827 callout_reset(&hs->hs_roottimer, ieee80211_hwmp_rannint, 828 hwmp_rootmode_rann_cb, vap); 829 ms->ms_flags |= IEEE80211_MESHFLAGS_ROOT; 830 break; 831 } 832 } 833 834 /* 835 * Send a broadcast Path Request to find all nodes on the mesh. We are 836 * called when the vap is configured as a HWMP root node. 837 */ 838 #define PREQ_TFLAGS(n) preq.preq_targets[n].target_flags 839 #define PREQ_TADDR(n) preq.preq_targets[n].target_addr 840 #define PREQ_TSEQ(n) preq.preq_targets[n].target_seq 841 static void 842 hwmp_rootmode_cb(void *arg) 843 { 844 struct ieee80211vap *vap = (struct ieee80211vap *)arg; 845 struct ieee80211_hwmp_state *hs = vap->iv_hwmp; 846 struct ieee80211_mesh_state *ms = vap->iv_mesh; 847 struct ieee80211_meshpreq_ie preq; 848 849 IEEE80211_NOTE(vap, IEEE80211_MSG_HWMP, vap->iv_bss, 850 "%s", "send broadcast PREQ"); 851 852 preq.preq_flags = 0; 853 if (ms->ms_flags & IEEE80211_MESHFLAGS_GATE) 854 preq.preq_flags |= IEEE80211_MESHPREQ_FLAGS_GATE; 855 if (hs->hs_rootmode == IEEE80211_HWMP_ROOTMODE_PROACTIVE) 856 preq.preq_flags |= IEEE80211_MESHPREQ_FLAGS_PP; 857 preq.preq_hopcount = 0; 858 preq.preq_ttl = ms->ms_ttl; 859 preq.preq_id = ++hs->hs_preqid; 860 IEEE80211_ADDR_COPY(preq.preq_origaddr, vap->iv_myaddr); 861 preq.preq_origseq = ++hs->hs_seq; 862 preq.preq_lifetime = ticks_to_msecs(ieee80211_hwmp_roottimeout); 863 preq.preq_metric = IEEE80211_MESHLMETRIC_INITIALVAL; 864 preq.preq_tcount = 1; 865 IEEE80211_ADDR_COPY(PREQ_TADDR(0), broadcastaddr); 866 PREQ_TFLAGS(0) = IEEE80211_MESHPREQ_TFLAGS_TO | 867 IEEE80211_MESHPREQ_TFLAGS_USN; 868 PREQ_TSEQ(0) = 0; 869 vap->iv_stats.is_hwmp_rootreqs++; 870 /* NB: we enforce rate check ourself */ 871 hwmp_send_preq(vap, broadcastaddr, &preq, NULL, NULL); 872 hwmp_rootmode_setup(vap); 873 } 874 #undef PREQ_TFLAGS 875 #undef PREQ_TADDR 876 #undef PREQ_TSEQ 877 878 /* 879 * Send a Root Annoucement (RANN) to find all the nodes on the mesh. We are 880 * called when the vap is configured as a HWMP RANN root node. 881 */ 882 static void 883 hwmp_rootmode_rann_cb(void *arg) 884 { 885 struct ieee80211vap *vap = (struct ieee80211vap *)arg; 886 struct ieee80211_hwmp_state *hs = vap->iv_hwmp; 887 struct ieee80211_mesh_state *ms = vap->iv_mesh; 888 struct ieee80211_meshrann_ie rann; 889 890 IEEE80211_NOTE(vap, IEEE80211_MSG_HWMP, vap->iv_bss, 891 "%s", "send broadcast RANN"); 892 893 rann.rann_flags = 0; 894 if (ms->ms_flags & IEEE80211_MESHFLAGS_GATE) 895 rann.rann_flags |= IEEE80211_MESHFLAGS_GATE; 896 rann.rann_hopcount = 0; 897 rann.rann_ttl = ms->ms_ttl; 898 IEEE80211_ADDR_COPY(rann.rann_addr, vap->iv_myaddr); 899 rann.rann_seq = ++hs->hs_seq; 900 rann.rann_interval = ieee80211_hwmp_rannint; 901 rann.rann_metric = IEEE80211_MESHLMETRIC_INITIALVAL; 902 903 vap->iv_stats.is_hwmp_rootrann++; 904 hwmp_send_rann(vap, broadcastaddr, &rann); 905 hwmp_rootmode_setup(vap); 906 } 907 908 /* 909 * Update forwarding information to TA if metric improves. 910 */ 911 static void 912 hwmp_update_transmitter(struct ieee80211vap *vap, struct ieee80211_node *ni, 913 const char *hwmp_frame) 914 { 915 struct ieee80211_mesh_state *ms = vap->iv_mesh; 916 struct ieee80211_mesh_route *rttran = NULL; /* Transmitter */ 917 int metric = 0; 918 919 rttran = ieee80211_mesh_rt_find(vap, ni->ni_macaddr); 920 if (rttran == NULL) { 921 rttran = ieee80211_mesh_rt_add(vap, ni->ni_macaddr); 922 if (rttran == NULL) { 923 IEEE80211_NOTE(vap, IEEE80211_MSG_HWMP, ni, 924 "unable to add path to transmitter %6D of %s", 925 ni->ni_macaddr, ":", hwmp_frame); 926 vap->iv_stats.is_mesh_rtaddfailed++; 927 return; 928 } 929 } 930 metric = ms->ms_pmetric->mpm_metric(ni); 931 if (!(rttran->rt_flags & IEEE80211_MESHRT_FLAGS_VALID) || 932 rttran->rt_metric > metric) 933 { 934 IEEE80211_NOTE(vap, IEEE80211_MSG_HWMP, ni, 935 "%s path to transmiter %6D of %s, metric %d:%d", 936 rttran->rt_flags & IEEE80211_MESHRT_FLAGS_VALID ? 937 "prefer" : "update", ni->ni_macaddr, ":", hwmp_frame, 938 rttran->rt_metric, metric); 939 IEEE80211_ADDR_COPY(rttran->rt_nexthop, ni->ni_macaddr); 940 rttran->rt_metric = metric; 941 rttran->rt_nhops = 1; 942 ieee80211_mesh_rt_update(rttran, ms->ms_ppath->mpp_inact); 943 rttran->rt_flags = IEEE80211_MESHRT_FLAGS_VALID; 944 } 945 } 946 947 #define PREQ_TFLAGS(n) preq->preq_targets[n].target_flags 948 #define PREQ_TADDR(n) preq->preq_targets[n].target_addr 949 #define PREQ_TSEQ(n) preq->preq_targets[n].target_seq 950 static void 951 hwmp_recv_preq(struct ieee80211vap *vap, struct ieee80211_node *ni, 952 const struct ieee80211_frame *wh, const struct ieee80211_meshpreq_ie *preq) 953 { 954 struct ieee80211_mesh_state *ms = vap->iv_mesh; 955 struct ieee80211_mesh_route *rtorig = NULL; 956 struct ieee80211_mesh_route *rtorig_ext = NULL; 957 struct ieee80211_mesh_route *rttarg = NULL; 958 struct ieee80211_hwmp_route *hrorig = NULL; 959 struct ieee80211_hwmp_route *hrtarg = NULL; 960 struct ieee80211_hwmp_state *hs = vap->iv_hwmp; 961 struct ieee80211_meshprep_ie prep; 962 ieee80211_hwmp_seq preqid; /* last seen preqid for orig */ 963 uint32_t metric = 0; 964 965 /* 966 * Ignore PREQs from us. Could happen because someone forward it 967 * back to us. 968 */ 969 if (IEEE80211_ADDR_EQ(vap->iv_myaddr, preq->preq_origaddr)) 970 return; 971 972 IEEE80211_NOTE(vap, IEEE80211_MSG_HWMP, ni, 973 "received PREQ, orig %6D, targ(0) %6D", preq->preq_origaddr, ":", 974 PREQ_TADDR(0), ":"); 975 976 /* 977 * Acceptance criteria: (if the PREQ is not for us or not broadcast, 978 * or an external mac address not proxied by us), 979 * AND forwarding is disabled, discard this PREQ. 980 */ 981 rttarg = ieee80211_mesh_rt_find(vap, PREQ_TADDR(0)); 982 if (!(ms->ms_flags & IEEE80211_MESHFLAGS_FWD) && 983 (!IEEE80211_ADDR_EQ(vap->iv_myaddr, PREQ_TADDR(0)) || 984 !IEEE80211_IS_MULTICAST(PREQ_TADDR(0)) || 985 (rttarg != NULL && 986 rttarg->rt_flags & IEEE80211_MESHRT_FLAGS_PROXY && 987 IEEE80211_ADDR_EQ(vap->iv_myaddr, rttarg->rt_mesh_gate)))) { 988 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_HWMP, 989 preq->preq_origaddr, NULL, "%s", "not accepting PREQ"); 990 return; 991 } 992 /* 993 * Acceptance criteria: if unicast addressed 994 * AND no valid forwarding for Target of PREQ, discard this PREQ. 995 */ 996 if(rttarg != NULL) 997 hrtarg = IEEE80211_MESH_ROUTE_PRIV(rttarg, 998 struct ieee80211_hwmp_route); 999 /* Address mode: ucast */ 1000 if(preq->preq_flags & IEEE80211_MESHPREQ_FLAGS_AM && 1001 rttarg == NULL && 1002 !IEEE80211_ADDR_EQ(vap->iv_myaddr, PREQ_TADDR(0))) { 1003 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_HWMP, 1004 preq->preq_origaddr, NULL, 1005 "unicast addressed PREQ of unknown target %6D", 1006 PREQ_TADDR(0), ":"); 1007 return; 1008 } 1009 1010 /* PREQ ACCEPTED */ 1011 1012 rtorig = ieee80211_mesh_rt_find(vap, preq->preq_origaddr); 1013 if (rtorig == NULL) { 1014 rtorig = ieee80211_mesh_rt_add(vap, preq->preq_origaddr); 1015 if (rtorig == NULL) { 1016 IEEE80211_NOTE(vap, IEEE80211_MSG_HWMP, ni, 1017 "unable to add orig path to %6D", 1018 preq->preq_origaddr, ":"); 1019 vap->iv_stats.is_mesh_rtaddfailed++; 1020 return; 1021 } 1022 IEEE80211_NOTE(vap, IEEE80211_MSG_HWMP, ni, 1023 "adding originator %6D", preq->preq_origaddr, ":"); 1024 } 1025 hrorig = IEEE80211_MESH_ROUTE_PRIV(rtorig, struct ieee80211_hwmp_route); 1026 1027 /* record last seen preqid */ 1028 preqid = hrorig->hr_preqid; 1029 hrorig->hr_preqid = HWMP_SEQ_MAX(hrorig->hr_preqid, preq->preq_id); 1030 1031 /* Data creation and update of forwarding information 1032 * according to Table 11C-8 for originator mesh STA. 1033 */ 1034 metric = preq->preq_metric + ms->ms_pmetric->mpm_metric(ni); 1035 if (HWMP_SEQ_GT(preq->preq_origseq, hrorig->hr_seq) || 1036 (HWMP_SEQ_EQ(preq->preq_origseq, hrorig->hr_seq) && 1037 metric < rtorig->rt_metric)) { 1038 hrorig->hr_seq = preq->preq_origseq; 1039 IEEE80211_ADDR_COPY(rtorig->rt_nexthop, wh->i_addr2); 1040 rtorig->rt_metric = metric; 1041 rtorig->rt_nhops = preq->preq_hopcount + 1; 1042 ieee80211_mesh_rt_update(rtorig, preq->preq_lifetime); 1043 /* Path to orig is valid now. 1044 * NB: we know it can't be Proxy, and if it is GATE 1045 * it will be marked below. 1046 */ 1047 rtorig->rt_flags = IEEE80211_MESHRT_FLAGS_VALID; 1048 } else if ((hrtarg != NULL && 1049 !HWMP_SEQ_EQ(hrtarg->hr_seq, PREQ_TSEQ(0))) || 1050 (rtorig->rt_flags & IEEE80211_MESHRT_FLAGS_VALID && 1051 preqid >= preq->preq_id)) { 1052 IEEE80211_NOTE(vap, IEEE80211_MSG_HWMP, ni, 1053 "discard PREQ from %6D, old seqno %u <= %u," 1054 " or old preqid %u < %u", 1055 preq->preq_origaddr, ":", 1056 preq->preq_origseq, hrorig->hr_seq, 1057 preq->preq_id, preqid); 1058 return; 1059 } 1060 1061 /* Update forwarding information to TA if metric improves. */ 1062 hwmp_update_transmitter(vap, ni, "PREQ"); 1063 1064 /* 1065 * Check if the PREQ is addressed to us. 1066 * or a Proxy currently gated by us. 1067 */ 1068 if (IEEE80211_ADDR_EQ(vap->iv_myaddr, PREQ_TADDR(0)) || 1069 (ms->ms_flags & IEEE80211_MESHFLAGS_GATE && 1070 rttarg != NULL && 1071 IEEE80211_ADDR_EQ(vap->iv_myaddr, rttarg->rt_mesh_gate) && 1072 rttarg->rt_flags & IEEE80211_MESHRT_FLAGS_PROXY && 1073 rttarg->rt_flags & IEEE80211_MESHRT_FLAGS_VALID)) { 1074 /* 1075 * When we are the target we shall update our own HWMP seq 1076 * number with max of (current and preq->seq) + 1 1077 */ 1078 hs->hs_seq = HWMP_SEQ_MAX(hs->hs_seq, PREQ_TSEQ(0)) + 1; 1079 1080 prep.prep_flags = 0; 1081 prep.prep_hopcount = 0; 1082 prep.prep_metric = IEEE80211_MESHLMETRIC_INITIALVAL; 1083 IEEE80211_ADDR_COPY(prep.prep_targetaddr, vap->iv_myaddr); 1084 if (rttarg != NULL && /* if NULL it means we are the target */ 1085 rttarg->rt_flags & IEEE80211_MESHRT_FLAGS_PROXY) { 1086 IEEE80211_NOTE(vap, IEEE80211_MSG_HWMP, ni, 1087 "reply for proxy %6D", rttarg->rt_dest, ":"); 1088 prep.prep_flags |= IEEE80211_MESHPREP_FLAGS_AE; 1089 IEEE80211_ADDR_COPY(prep.prep_target_ext_addr, 1090 rttarg->rt_dest); 1091 /* update proxy seqno to HWMP seqno */ 1092 rttarg->rt_ext_seq = hs->hs_seq; 1093 prep.prep_hopcount = rttarg->rt_nhops; 1094 prep.prep_metric = rttarg->rt_metric; 1095 IEEE80211_ADDR_COPY(prep.prep_targetaddr, rttarg->rt_mesh_gate); 1096 } 1097 /* 1098 * Build and send a PREP frame. 1099 */ 1100 prep.prep_ttl = ms->ms_ttl; 1101 prep.prep_targetseq = hs->hs_seq; 1102 prep.prep_lifetime = preq->preq_lifetime; 1103 IEEE80211_ADDR_COPY(prep.prep_origaddr, preq->preq_origaddr); 1104 prep.prep_origseq = preq->preq_origseq; 1105 1106 IEEE80211_NOTE(vap, IEEE80211_MSG_HWMP, ni, 1107 "reply to %6D", preq->preq_origaddr, ":"); 1108 hwmp_send_prep(vap, wh->i_addr2, &prep); 1109 return; 1110 } 1111 /* we may update our proxy information for the orig external */ 1112 else if (preq->preq_flags & IEEE80211_MESHPREQ_FLAGS_AE) { 1113 rtorig_ext = 1114 ieee80211_mesh_rt_find(vap, preq->preq_orig_ext_addr); 1115 if (rtorig_ext == NULL) { 1116 rtorig_ext = ieee80211_mesh_rt_add(vap, 1117 preq->preq_orig_ext_addr); 1118 if (rtorig_ext == NULL) { 1119 IEEE80211_NOTE(vap, IEEE80211_MSG_HWMP, ni, 1120 "unable to add orig ext proxy to %6D", 1121 preq->preq_orig_ext_addr, ":"); 1122 vap->iv_stats.is_mesh_rtaddfailed++; 1123 return; 1124 } 1125 IEEE80211_ADDR_COPY(rtorig_ext->rt_mesh_gate, 1126 preq->preq_origaddr); 1127 } 1128 rtorig_ext->rt_ext_seq = preq->preq_origseq; 1129 ieee80211_mesh_rt_update(rtorig_ext, preq->preq_lifetime); 1130 } 1131 /* 1132 * Proactive PREQ: reply with a proactive PREP to the 1133 * root STA if requested. 1134 */ 1135 if (IEEE80211_ADDR_EQ(PREQ_TADDR(0), broadcastaddr) && 1136 (PREQ_TFLAGS(0) & IEEE80211_MESHPREQ_TFLAGS_TO)) { 1137 IEEE80211_NOTE(vap, IEEE80211_MSG_HWMP, ni, 1138 "root mesh station @ %6D", preq->preq_origaddr, ":"); 1139 1140 /* Check if root is a mesh gate, mark it */ 1141 if (preq->preq_flags & IEEE80211_MESHPREQ_FLAGS_GATE) { 1142 struct ieee80211_mesh_gate_route *gr; 1143 1144 rtorig->rt_flags |= IEEE80211_MESHRT_FLAGS_GATE; 1145 gr = ieee80211_mesh_mark_gate(vap, preq->preq_origaddr, 1146 rtorig); 1147 gr->gr_lastseq = 0; /* NOT GANN */ 1148 } 1149 1150 /* 1151 * Reply with a PREP if we don't have a path to the root 1152 * or if the root sent us a proactive PREQ. 1153 */ 1154 if ((rtorig->rt_flags & IEEE80211_MESHRT_FLAGS_VALID) == 0 || 1155 (preq->preq_flags & IEEE80211_MESHPREQ_FLAGS_PP)) { 1156 prep.prep_flags = 0; 1157 prep.prep_hopcount = 0; 1158 prep.prep_ttl = ms->ms_ttl; 1159 IEEE80211_ADDR_COPY(prep.prep_origaddr, 1160 preq->preq_origaddr); 1161 prep.prep_origseq = preq->preq_origseq; 1162 prep.prep_lifetime = preq->preq_lifetime; 1163 prep.prep_metric = IEEE80211_MESHLMETRIC_INITIALVAL; 1164 IEEE80211_ADDR_COPY(prep.prep_targetaddr, 1165 vap->iv_myaddr); 1166 prep.prep_targetseq = ++hs->hs_seq; 1167 hwmp_send_prep(vap, rtorig->rt_nexthop, &prep); 1168 } 1169 } 1170 1171 /* 1172 * Forwarding and Intermediate reply for PREQs with 1 target. 1173 */ 1174 if ((preq->preq_tcount == 1) && (preq->preq_ttl > 1) && 1175 (ms->ms_flags & IEEE80211_MESHFLAGS_FWD)) { 1176 struct ieee80211_meshpreq_ie ppreq; /* propagated PREQ */ 1177 1178 memcpy(&ppreq, preq, sizeof(ppreq)); 1179 1180 /* 1181 * We have a valid route to this node. 1182 * NB: if target is proxy dont reply. 1183 */ 1184 if (rttarg != NULL && 1185 rttarg->rt_flags & IEEE80211_MESHRT_FLAGS_VALID && 1186 !(rttarg->rt_flags & IEEE80211_MESHRT_FLAGS_PROXY)) { 1187 /* 1188 * Check if we can send an intermediate Path Reply, 1189 * i.e., Target Only bit is not set and target is not 1190 * the MAC broadcast address. 1191 */ 1192 if (!(PREQ_TFLAGS(0) & IEEE80211_MESHPREQ_TFLAGS_TO) && 1193 !IEEE80211_ADDR_EQ(PREQ_TADDR(0), broadcastaddr)) { 1194 struct ieee80211_meshprep_ie prep; 1195 1196 IEEE80211_NOTE(vap, IEEE80211_MSG_HWMP, ni, 1197 "intermediate reply for PREQ from %6D", 1198 preq->preq_origaddr, ":"); 1199 prep.prep_flags = 0; 1200 prep.prep_hopcount = rttarg->rt_nhops; 1201 prep.prep_ttl = ms->ms_ttl; 1202 IEEE80211_ADDR_COPY(&prep.prep_targetaddr, 1203 PREQ_TADDR(0)); 1204 prep.prep_targetseq = hrtarg->hr_seq; 1205 prep.prep_lifetime = preq->preq_lifetime; 1206 prep.prep_metric =rttarg->rt_metric; 1207 IEEE80211_ADDR_COPY(&prep.prep_origaddr, 1208 preq->preq_origaddr); 1209 prep.prep_origseq = hrorig->hr_seq; 1210 hwmp_send_prep(vap, rtorig->rt_nexthop, &prep); 1211 1212 /* 1213 * Set TO and unset RF bits because we have 1214 * sent a PREP. 1215 */ 1216 ppreq.preq_targets[0].target_flags |= 1217 IEEE80211_MESHPREQ_TFLAGS_TO; 1218 } 1219 } 1220 1221 IEEE80211_NOTE(vap, IEEE80211_MSG_HWMP, ni, 1222 "forward PREQ from %6D", 1223 preq->preq_origaddr, ":"); 1224 ppreq.preq_hopcount += 1; 1225 ppreq.preq_ttl -= 1; 1226 ppreq.preq_metric += ms->ms_pmetric->mpm_metric(ni); 1227 1228 /* don't do PREQ ratecheck when we propagate */ 1229 hwmp_send_preq(vap, broadcastaddr, &ppreq, NULL, NULL); 1230 } 1231 } 1232 #undef PREQ_TFLAGS 1233 #undef PREQ_TADDR 1234 #undef PREQ_TSEQ 1235 1236 static int 1237 hwmp_send_preq(struct ieee80211vap *vap, 1238 const uint8_t da[IEEE80211_ADDR_LEN], 1239 struct ieee80211_meshpreq_ie *preq, 1240 struct timeval *last, struct timeval *minint) 1241 { 1242 1243 /* 1244 * Enforce PREQ interval. 1245 * NB: Proactive ROOT PREQs rate is handled by cb task. 1246 */ 1247 if (last != NULL && minint != NULL) { 1248 if (ratecheck(last, minint) == 0) 1249 return EALREADY; /* XXX: we should postpone */ 1250 getmicrouptime(last); 1251 } 1252 1253 /* 1254 * mesh preq action frame format 1255 * [6] da 1256 * [6] sa 1257 * [6] addr3 = sa 1258 * [1] action 1259 * [1] category 1260 * [tlv] mesh path request 1261 */ 1262 preq->preq_ie = IEEE80211_ELEMID_MESHPREQ; 1263 preq->preq_len = (preq->preq_flags & IEEE80211_MESHPREQ_FLAGS_AE ? 1264 IEEE80211_MESHPREQ_BASE_SZ_AE : IEEE80211_MESHPREQ_BASE_SZ) + 1265 preq->preq_tcount * IEEE80211_MESHPREQ_TRGT_SZ; 1266 return hwmp_send_action(vap, da, (uint8_t *)preq, preq->preq_len+2); 1267 } 1268 1269 static void 1270 hwmp_recv_prep(struct ieee80211vap *vap, struct ieee80211_node *ni, 1271 const struct ieee80211_frame *wh, const struct ieee80211_meshprep_ie *prep) 1272 { 1273 #define IS_PROXY(rt) (rt->rt_flags & IEEE80211_MESHRT_FLAGS_PROXY) 1274 #define PROXIED_BY_US(rt) \ 1275 (IEEE80211_ADDR_EQ(vap->iv_myaddr, rt->rt_mesh_gate)) 1276 struct ieee80211_mesh_state *ms = vap->iv_mesh; 1277 struct ieee80211_hwmp_state *hs = vap->iv_hwmp; 1278 struct ieee80211_mesh_route *rt = NULL; 1279 struct ieee80211_mesh_route *rtorig = NULL; 1280 struct ieee80211_mesh_route *rtext = NULL; 1281 struct ieee80211_hwmp_route *hr; 1282 struct ieee80211com *ic = vap->iv_ic; 1283 struct mbuf *m, *next; 1284 uint32_t metric = 0; 1285 const uint8_t *addr; 1286 1287 IEEE80211_NOTE(vap, IEEE80211_MSG_HWMP, ni, 1288 "received PREP, orig %6D, targ %6D", prep->prep_origaddr, ":", 1289 prep->prep_targetaddr, ":"); 1290 1291 /* 1292 * Acceptance criteria: (If the corresponding PREP was not generated 1293 * by us OR not generated by an external mac that is not proxied by us) 1294 * AND forwarding is disabled, discard this PREP. 1295 */ 1296 rtorig = ieee80211_mesh_rt_find(vap, prep->prep_origaddr); 1297 if ((!IEEE80211_ADDR_EQ(vap->iv_myaddr, prep->prep_origaddr) || 1298 (rtorig != NULL && IS_PROXY(rtorig) && !PROXIED_BY_US(rtorig))) && 1299 !(ms->ms_flags & IEEE80211_MESHFLAGS_FWD)){ 1300 IEEE80211_NOTE(vap, IEEE80211_MSG_HWMP, ni, 1301 "discard PREP, orig(%6D) not proxied or generated by us", 1302 prep->prep_origaddr, ":"); 1303 return; 1304 } 1305 1306 /* PREP ACCEPTED */ 1307 1308 /* 1309 * If accepted shall create or update the active forwarding information 1310 * it maintains for the target mesh STA of the PREP (according to the 1311 * rules defined in 13.10.8.4). If the conditions for creating or 1312 * updating the forwarding information have not been met in those 1313 * rules, no further steps are applied to the PREP. 1314 */ 1315 rt = ieee80211_mesh_rt_find(vap, prep->prep_targetaddr); 1316 if (rt == NULL) { 1317 rt = ieee80211_mesh_rt_add(vap, prep->prep_targetaddr); 1318 if (rt == NULL) { 1319 IEEE80211_NOTE(vap, IEEE80211_MSG_HWMP, ni, 1320 "unable to add PREP path to %6D", 1321 prep->prep_targetaddr, ":"); 1322 vap->iv_stats.is_mesh_rtaddfailed++; 1323 return; 1324 } 1325 IEEE80211_NOTE(vap, IEEE80211_MSG_HWMP, ni, 1326 "adding target %6D", prep->prep_targetaddr, ":"); 1327 } 1328 hr = IEEE80211_MESH_ROUTE_PRIV(rt, struct ieee80211_hwmp_route); 1329 /* update path metric */ 1330 metric = prep->prep_metric + ms->ms_pmetric->mpm_metric(ni); 1331 if ((rt->rt_flags & IEEE80211_MESHRT_FLAGS_VALID)) { 1332 if (HWMP_SEQ_LT(prep->prep_targetseq, hr->hr_seq)) { 1333 IEEE80211_NOTE(vap, IEEE80211_MSG_HWMP, ni, 1334 "discard PREP from %6D, old seq no %u < %u", 1335 prep->prep_targetaddr, ":", 1336 prep->prep_targetseq, hr->hr_seq); 1337 return; 1338 } else if (HWMP_SEQ_LEQ(prep->prep_targetseq, hr->hr_seq) && 1339 metric > rt->rt_metric) { 1340 IEEE80211_NOTE(vap, IEEE80211_MSG_HWMP, ni, 1341 "discard PREP from %6D, new metric %u > %u", 1342 prep->prep_targetaddr, ":", 1343 metric, rt->rt_metric); 1344 return; 1345 } 1346 } 1347 1348 IEEE80211_NOTE(vap, IEEE80211_MSG_HWMP, ni, 1349 "%s path to %6D, hopcount %d:%d metric %d:%d", 1350 rt->rt_flags & IEEE80211_MESHRT_FLAGS_VALID ? 1351 "prefer" : "update", 1352 prep->prep_targetaddr, ":", 1353 rt->rt_nhops, prep->prep_hopcount + 1, 1354 rt->rt_metric, metric); 1355 1356 hr->hr_seq = prep->prep_targetseq; 1357 hr->hr_preqretries = 0; 1358 IEEE80211_ADDR_COPY(rt->rt_nexthop, ni->ni_macaddr); 1359 rt->rt_metric = metric; 1360 rt->rt_nhops = prep->prep_hopcount + 1; 1361 ieee80211_mesh_rt_update(rt, prep->prep_lifetime); 1362 if (rt->rt_flags & IEEE80211_MESHRT_FLAGS_DISCOVER) { 1363 /* discovery complete */ 1364 rt->rt_flags &= ~IEEE80211_MESHRT_FLAGS_DISCOVER; 1365 } 1366 rt->rt_flags |= IEEE80211_MESHRT_FLAGS_VALID; /* mark valid */ 1367 1368 /* Update forwarding information to TA if metric improves */ 1369 hwmp_update_transmitter(vap, ni, "PREP"); 1370 1371 /* 1372 * If it's NOT for us, propagate the PREP 1373 */ 1374 if (!IEEE80211_ADDR_EQ(vap->iv_myaddr, prep->prep_origaddr) && 1375 prep->prep_ttl > 1 && 1376 prep->prep_hopcount < hs->hs_maxhops) { 1377 struct ieee80211_meshprep_ie pprep; /* propagated PREP */ 1378 /* 1379 * NB: We should already have setup the path to orig 1380 * mesh STA when we propagated PREQ to target mesh STA, 1381 * no PREP is generated without a corresponding PREQ. 1382 * XXX: for now just ignore. 1383 */ 1384 if (rtorig == NULL) { 1385 IEEE80211_NOTE(vap, IEEE80211_MSG_HWMP, ni, 1386 "received PREP for an unknown orig(%6D)", 1387 prep->prep_origaddr, ":"); 1388 return; 1389 } 1390 1391 IEEE80211_NOTE(vap, IEEE80211_MSG_HWMP, ni, 1392 "propagate PREP from %6D", 1393 prep->prep_targetaddr, ":"); 1394 1395 memcpy(&pprep, prep, sizeof(pprep)); 1396 pprep.prep_hopcount += 1; 1397 pprep.prep_ttl -= 1; 1398 pprep.prep_metric += ms->ms_pmetric->mpm_metric(ni); 1399 hwmp_send_prep(vap, rtorig->rt_nexthop, &pprep); 1400 1401 /* precursor list for the Target Mesh STA Address is updated */ 1402 } 1403 1404 /* 1405 * Check if we received a PREP w/ AE and store target external address. 1406 * We may store target external address if recevied PREP w/ AE 1407 * and we are not final destination 1408 */ 1409 if (prep->prep_flags & IEEE80211_MESHPREP_FLAGS_AE) { 1410 rtext = ieee80211_mesh_rt_find(vap, 1411 prep->prep_target_ext_addr); 1412 if (rtext == NULL) { 1413 rtext = ieee80211_mesh_rt_add(vap, 1414 prep->prep_target_ext_addr); 1415 if (rtext == NULL) { 1416 IEEE80211_NOTE(vap, IEEE80211_MSG_HWMP, ni, 1417 "unable to add PREP path to proxy %6D", 1418 prep->prep_targetaddr, ":"); 1419 vap->iv_stats.is_mesh_rtaddfailed++; 1420 return; 1421 } 1422 } 1423 IEEE80211_NOTE(vap, IEEE80211_MSG_HWMP, ni, 1424 "%s path to %6D, hopcount %d:%d metric %d:%d", 1425 rtext->rt_flags & IEEE80211_MESHRT_FLAGS_VALID ? 1426 "prefer" : "update", 1427 prep->prep_target_ext_addr, ":", 1428 rtext->rt_nhops, prep->prep_hopcount + 1, 1429 rtext->rt_metric, metric); 1430 1431 rtext->rt_flags = IEEE80211_MESHRT_FLAGS_PROXY | 1432 IEEE80211_MESHRT_FLAGS_VALID; 1433 IEEE80211_ADDR_COPY(rtext->rt_dest, 1434 prep->prep_target_ext_addr); 1435 IEEE80211_ADDR_COPY(rtext->rt_mesh_gate, 1436 prep->prep_targetaddr); 1437 IEEE80211_ADDR_COPY(rtext->rt_nexthop, wh->i_addr2); 1438 rtext->rt_metric = metric; 1439 rtext->rt_lifetime = prep->prep_lifetime; 1440 rtext->rt_nhops = prep->prep_hopcount + 1; 1441 rtext->rt_ext_seq = prep->prep_origseq; /* new proxy seq */ 1442 /* 1443 * XXX: proxy entries have no HWMP priv data, 1444 * nullify them to be sure? 1445 */ 1446 } 1447 /* 1448 * Check for frames queued awaiting path discovery. 1449 * XXX probably can tell exactly and avoid remove call 1450 * NB: hash may have false matches, if so they will get 1451 * stuck back on the stageq because there won't be 1452 * a path. 1453 */ 1454 addr = prep->prep_flags & IEEE80211_MESHPREP_FLAGS_AE ? 1455 prep->prep_target_ext_addr : prep->prep_targetaddr; 1456 m = ieee80211_ageq_remove(&ic->ic_stageq, 1457 (struct ieee80211_node *)(uintptr_t) 1458 ieee80211_mac_hash(ic, addr)); /* either dest or ext_dest */ 1459 1460 /* 1461 * All frames in the stageq here should be non-M_ENCAP; or things 1462 * will get very unhappy. 1463 */ 1464 for (; m != NULL; m = next) { 1465 next = m->m_nextpkt; 1466 m->m_nextpkt = NULL; 1467 IEEE80211_NOTE(vap, IEEE80211_MSG_HWMP, ni, 1468 "flush queued frame %p len %d", m, m->m_pkthdr.len); 1469 /* 1470 * If the mbuf has M_ENCAP set, ensure we free it. 1471 * Note that after if_transmit() is called, m is invalid. 1472 */ 1473 (void) ieee80211_vap_xmitpkt(vap, m); 1474 } 1475 #undef IS_PROXY 1476 #undef PROXIED_BY_US 1477 } 1478 1479 static int 1480 hwmp_send_prep(struct ieee80211vap *vap, 1481 const uint8_t da[IEEE80211_ADDR_LEN], 1482 struct ieee80211_meshprep_ie *prep) 1483 { 1484 /* NB: there's no PREP minimum interval. */ 1485 1486 /* 1487 * mesh prep action frame format 1488 * [6] da 1489 * [6] sa 1490 * [6] addr3 = sa 1491 * [1] action 1492 * [1] category 1493 * [tlv] mesh path reply 1494 */ 1495 prep->prep_ie = IEEE80211_ELEMID_MESHPREP; 1496 prep->prep_len = prep->prep_flags & IEEE80211_MESHPREP_FLAGS_AE ? 1497 IEEE80211_MESHPREP_BASE_SZ_AE : IEEE80211_MESHPREP_BASE_SZ; 1498 return hwmp_send_action(vap, da, (uint8_t *)prep, prep->prep_len + 2); 1499 } 1500 1501 #define PERR_DFLAGS(n) perr.perr_dests[n].dest_flags 1502 #define PERR_DADDR(n) perr.perr_dests[n].dest_addr 1503 #define PERR_DSEQ(n) perr.perr_dests[n].dest_seq 1504 #define PERR_DRCODE(n) perr.perr_dests[n].dest_rcode 1505 static void 1506 hwmp_peerdown(struct ieee80211_node *ni) 1507 { 1508 struct ieee80211vap *vap = ni->ni_vap; 1509 struct ieee80211_mesh_state *ms = vap->iv_mesh; 1510 struct ieee80211_meshperr_ie perr; 1511 struct ieee80211_mesh_route *rt; 1512 struct ieee80211_hwmp_route *hr; 1513 1514 rt = ieee80211_mesh_rt_find(vap, ni->ni_macaddr); 1515 if (rt == NULL) 1516 return; 1517 hr = IEEE80211_MESH_ROUTE_PRIV(rt, struct ieee80211_hwmp_route); 1518 IEEE80211_NOTE(vap, IEEE80211_MSG_HWMP, ni, 1519 "%s", "delete route entry"); 1520 perr.perr_ttl = ms->ms_ttl; 1521 perr.perr_ndests = 1; 1522 PERR_DFLAGS(0) = 0; 1523 if (hr->hr_seq == 0) 1524 PERR_DFLAGS(0) |= IEEE80211_MESHPERR_DFLAGS_USN; 1525 PERR_DFLAGS(0) |= IEEE80211_MESHPERR_DFLAGS_RC; 1526 IEEE80211_ADDR_COPY(PERR_DADDR(0), rt->rt_dest); 1527 PERR_DSEQ(0) = ++hr->hr_seq; 1528 PERR_DRCODE(0) = IEEE80211_REASON_MESH_PERR_DEST_UNREACH; 1529 /* NB: flush everything passing through peer */ 1530 ieee80211_mesh_rt_flush_peer(vap, ni->ni_macaddr); 1531 hwmp_send_perr(vap, broadcastaddr, &perr); 1532 } 1533 #undef PERR_DFLAGS 1534 #undef PERR_DADDR 1535 #undef PERR_DSEQ 1536 #undef PERR_DRCODE 1537 1538 #define PERR_DFLAGS(n) perr->perr_dests[n].dest_flags 1539 #define PERR_DADDR(n) perr->perr_dests[n].dest_addr 1540 #define PERR_DSEQ(n) perr->perr_dests[n].dest_seq 1541 #define PERR_DEXTADDR(n) perr->perr_dests[n].dest_ext_addr 1542 #define PERR_DRCODE(n) perr->perr_dests[n].dest_rcode 1543 static void 1544 hwmp_recv_perr(struct ieee80211vap *vap, struct ieee80211_node *ni, 1545 const struct ieee80211_frame *wh, const struct ieee80211_meshperr_ie *perr) 1546 { 1547 struct ieee80211_mesh_state *ms = vap->iv_mesh; 1548 struct ieee80211_mesh_route *rt = NULL; 1549 struct ieee80211_mesh_route *rt_ext = NULL; 1550 struct ieee80211_hwmp_route *hr; 1551 struct ieee80211_meshperr_ie *pperr = NULL; 1552 int i, j = 0, forward = 0; 1553 1554 IEEE80211_NOTE(vap, IEEE80211_MSG_HWMP, ni, 1555 "received PERR from %6D", wh->i_addr2, ":"); 1556 1557 /* 1558 * if forwarding is true, prepare pperr 1559 */ 1560 if (ms->ms_flags & IEEE80211_MESHFLAGS_FWD) { 1561 forward = 1; 1562 pperr = IEEE80211_MALLOC(sizeof(*perr) + 31*sizeof(*perr->perr_dests), 1563 M_80211_MESH_PERR, IEEE80211_M_NOWAIT); /* XXX: magic number, 32 err dests */ 1564 } 1565 1566 /* 1567 * Acceptance criteria: check if we have forwarding information 1568 * stored about destination, and that nexthop == TA of this PERR. 1569 * NB: we also build a new PERR to propagate in case we should forward. 1570 */ 1571 for (i = 0; i < perr->perr_ndests; i++) { 1572 rt = ieee80211_mesh_rt_find(vap, PERR_DADDR(i)); 1573 if (rt == NULL) 1574 continue; 1575 if (!IEEE80211_ADDR_EQ(rt->rt_nexthop, wh->i_addr2)) 1576 continue; 1577 1578 /* found and accepted a PERR ndest element, process it... */ 1579 if (forward) 1580 memcpy(&pperr->perr_dests[j], &perr->perr_dests[i], 1581 sizeof(*perr->perr_dests)); 1582 hr = IEEE80211_MESH_ROUTE_PRIV(rt, struct ieee80211_hwmp_route); 1583 switch(PERR_DFLAGS(i)) { 1584 case (IEEE80211_REASON_MESH_PERR_NO_FI): 1585 if (PERR_DSEQ(i) == 0) { 1586 hr->hr_seq++; 1587 if (forward) { 1588 pperr->perr_dests[j].dest_seq = 1589 hr->hr_seq; 1590 } 1591 } else { 1592 hr->hr_seq = PERR_DSEQ(i); 1593 } 1594 rt->rt_flags &= ~IEEE80211_MESHRT_FLAGS_VALID; 1595 j++; 1596 break; 1597 case (IEEE80211_REASON_MESH_PERR_DEST_UNREACH): 1598 if(HWMP_SEQ_GT(PERR_DSEQ(i), hr->hr_seq)) { 1599 hr->hr_seq = PERR_DSEQ(i); 1600 rt->rt_flags &= ~IEEE80211_MESHRT_FLAGS_VALID; 1601 j++; 1602 } 1603 break; 1604 case (IEEE80211_REASON_MESH_PERR_NO_PROXY): 1605 rt_ext = ieee80211_mesh_rt_find(vap, PERR_DEXTADDR(i)); 1606 if (rt_ext != NULL) { 1607 rt_ext->rt_flags &= 1608 ~IEEE80211_MESHRT_FLAGS_VALID; 1609 j++; 1610 } 1611 break; 1612 default: 1613 IEEE80211_DISCARD(vap, IEEE80211_MSG_HWMP, wh, NULL, 1614 "PERR, unknown reason code %u\n", PERR_DFLAGS(i)); 1615 goto done; /* XXX: stats?? */ 1616 } 1617 ieee80211_mesh_rt_flush_peer(vap, PERR_DADDR(i)); 1618 KASSERT(j < 32, ("PERR, error ndest >= 32 (%u)", j)); 1619 } 1620 if (j == 0) { 1621 IEEE80211_DISCARD(vap, IEEE80211_MSG_HWMP, wh, NULL, "%s", 1622 "PERR not accepted"); 1623 goto done; /* XXX: stats?? */ 1624 } 1625 1626 /* 1627 * Propagate the PERR if we previously found it on our routing table. 1628 */ 1629 if (forward && perr->perr_ttl > 1) { 1630 IEEE80211_NOTE(vap, IEEE80211_MSG_HWMP, ni, 1631 "propagate PERR from %6D", wh->i_addr2, ":"); 1632 pperr->perr_ndests = j; 1633 pperr->perr_ttl--; 1634 hwmp_send_perr(vap, broadcastaddr, pperr); 1635 } 1636 done: 1637 if (pperr != NULL) 1638 IEEE80211_FREE(pperr, M_80211_MESH_PERR); 1639 } 1640 #undef PERR_DFLAGS 1641 #undef PERR_DADDR 1642 #undef PERR_DSEQ 1643 #undef PERR_DEXTADDR 1644 #undef PERR_DRCODE 1645 1646 static int 1647 hwmp_send_perr(struct ieee80211vap *vap, 1648 const uint8_t da[IEEE80211_ADDR_LEN], 1649 struct ieee80211_meshperr_ie *perr) 1650 { 1651 struct ieee80211_hwmp_state *hs = vap->iv_hwmp; 1652 int i; 1653 uint8_t length = 0; 1654 1655 /* 1656 * Enforce PERR interval. 1657 */ 1658 if (ratecheck(&hs->hs_lastperr, &ieee80211_hwmp_perrminint) == 0) 1659 return EALREADY; 1660 getmicrouptime(&hs->hs_lastperr); 1661 1662 /* 1663 * mesh perr action frame format 1664 * [6] da 1665 * [6] sa 1666 * [6] addr3 = sa 1667 * [1] action 1668 * [1] category 1669 * [tlv] mesh path error 1670 */ 1671 perr->perr_ie = IEEE80211_ELEMID_MESHPERR; 1672 length = IEEE80211_MESHPERR_BASE_SZ; 1673 for (i = 0; i<perr->perr_ndests; i++) { 1674 if (perr->perr_dests[i].dest_flags & 1675 IEEE80211_MESHPERR_FLAGS_AE) { 1676 length += IEEE80211_MESHPERR_DEST_SZ_AE; 1677 continue ; 1678 } 1679 length += IEEE80211_MESHPERR_DEST_SZ; 1680 } 1681 perr->perr_len =length; 1682 return hwmp_send_action(vap, da, (uint8_t *)perr, perr->perr_len+2); 1683 } 1684 1685 /* 1686 * Called from the rest of the net80211 code (mesh code for example). 1687 * NB: IEEE80211_REASON_MESH_PERR_DEST_UNREACH can be trigger by the fact that 1688 * a mesh STA is unable to forward an MSDU/MMPDU to a next-hop mesh STA. 1689 */ 1690 #define PERR_DFLAGS(n) perr.perr_dests[n].dest_flags 1691 #define PERR_DADDR(n) perr.perr_dests[n].dest_addr 1692 #define PERR_DSEQ(n) perr.perr_dests[n].dest_seq 1693 #define PERR_DEXTADDR(n) perr.perr_dests[n].dest_ext_addr 1694 #define PERR_DRCODE(n) perr.perr_dests[n].dest_rcode 1695 static void 1696 hwmp_senderror(struct ieee80211vap *vap, 1697 const uint8_t addr[IEEE80211_ADDR_LEN], 1698 struct ieee80211_mesh_route *rt, int rcode) 1699 { 1700 struct ieee80211_mesh_state *ms = vap->iv_mesh; 1701 struct ieee80211_hwmp_route *hr = NULL; 1702 struct ieee80211_meshperr_ie perr; 1703 1704 if (rt != NULL) 1705 hr = IEEE80211_MESH_ROUTE_PRIV(rt, 1706 struct ieee80211_hwmp_route); 1707 1708 perr.perr_ndests = 1; 1709 perr.perr_ttl = ms->ms_ttl; 1710 PERR_DFLAGS(0) = 0; 1711 PERR_DRCODE(0) = rcode; 1712 1713 switch (rcode) { 1714 case IEEE80211_REASON_MESH_PERR_NO_FI: 1715 IEEE80211_ADDR_COPY(PERR_DADDR(0), addr); 1716 PERR_DSEQ(0) = 0; /* reserved */ 1717 break; 1718 case IEEE80211_REASON_MESH_PERR_NO_PROXY: 1719 KASSERT(rt != NULL, ("no proxy info for sending PERR")); 1720 KASSERT(rt->rt_flags & IEEE80211_MESHRT_FLAGS_PROXY, 1721 ("route is not marked proxy")); 1722 PERR_DFLAGS(0) |= IEEE80211_MESHPERR_FLAGS_AE; 1723 IEEE80211_ADDR_COPY(PERR_DADDR(0), vap->iv_myaddr); 1724 PERR_DSEQ(0) = rt->rt_ext_seq; 1725 IEEE80211_ADDR_COPY(PERR_DEXTADDR(0), addr); 1726 break; 1727 case IEEE80211_REASON_MESH_PERR_DEST_UNREACH: 1728 KASSERT(rt != NULL, ("no route info for sending PERR")); 1729 IEEE80211_ADDR_COPY(PERR_DADDR(0), addr); 1730 PERR_DSEQ(0) = hr->hr_seq; 1731 break; 1732 default: 1733 KASSERT(0, ("unknown reason code for HWMP PERR (%u)", rcode)); 1734 } 1735 hwmp_send_perr(vap, broadcastaddr, &perr); 1736 } 1737 #undef PERR_DFLAGS 1738 #undef PEER_DADDR 1739 #undef PERR_DSEQ 1740 #undef PERR_DEXTADDR 1741 #undef PERR_DRCODE 1742 1743 static void 1744 hwmp_recv_rann(struct ieee80211vap *vap, struct ieee80211_node *ni, 1745 const struct ieee80211_frame *wh, const struct ieee80211_meshrann_ie *rann) 1746 { 1747 struct ieee80211_mesh_state *ms = vap->iv_mesh; 1748 struct ieee80211_hwmp_state *hs = vap->iv_hwmp; 1749 struct ieee80211_mesh_route *rt = NULL; 1750 struct ieee80211_hwmp_route *hr; 1751 struct ieee80211_meshpreq_ie preq; 1752 struct ieee80211_meshrann_ie prann; 1753 uint32_t metric = 0; 1754 1755 if (IEEE80211_ADDR_EQ(rann->rann_addr, vap->iv_myaddr)) 1756 return; 1757 1758 rt = ieee80211_mesh_rt_find(vap, rann->rann_addr); 1759 if (rt != NULL && rt->rt_flags & IEEE80211_MESHRT_FLAGS_VALID) { 1760 hr = IEEE80211_MESH_ROUTE_PRIV(rt, struct ieee80211_hwmp_route); 1761 1762 /* Acceptance criteria: if RANN.seq < stored seq, discard RANN */ 1763 if (HWMP_SEQ_LT(rann->rann_seq, hr->hr_seq)) { 1764 IEEE80211_DISCARD(vap, IEEE80211_MSG_HWMP, wh, NULL, 1765 "RANN seq %u < %u", rann->rann_seq, hr->hr_seq); 1766 return; 1767 } 1768 1769 /* Acceptance criteria: if RANN.seq == stored seq AND 1770 * RANN.metric > stored metric, discard RANN */ 1771 if (HWMP_SEQ_EQ(rann->rann_seq, hr->hr_seq) && 1772 rann->rann_metric > rt->rt_metric) { 1773 IEEE80211_DISCARD(vap, IEEE80211_MSG_HWMP, wh, NULL, 1774 "RANN metric %u > %u", rann->rann_metric, rt->rt_metric); 1775 return; 1776 } 1777 } 1778 1779 /* RANN ACCEPTED */ 1780 1781 ieee80211_hwmp_rannint = rann->rann_interval; /* XXX: mtx lock? */ 1782 metric = rann->rann_metric + ms->ms_pmetric->mpm_metric(ni); 1783 1784 if (rt == NULL) { 1785 rt = ieee80211_mesh_rt_add(vap, rann->rann_addr); 1786 if (rt == NULL) { 1787 IEEE80211_DISCARD(vap, IEEE80211_MSG_HWMP, wh, NULL, 1788 "unable to add mac for RANN root %6D", 1789 rann->rann_addr, ":"); 1790 vap->iv_stats.is_mesh_rtaddfailed++; 1791 return; 1792 } 1793 } 1794 hr = IEEE80211_MESH_ROUTE_PRIV(rt, struct ieee80211_hwmp_route); 1795 /* Check if root is a mesh gate, mark it */ 1796 if (rann->rann_flags & IEEE80211_MESHRANN_FLAGS_GATE) { 1797 struct ieee80211_mesh_gate_route *gr; 1798 1799 rt->rt_flags |= IEEE80211_MESHRT_FLAGS_GATE; 1800 gr = ieee80211_mesh_mark_gate(vap, rann->rann_addr, 1801 rt); 1802 gr->gr_lastseq = 0; /* NOT GANN */ 1803 } 1804 /* discovery timeout */ 1805 ieee80211_mesh_rt_update(rt, 1806 ticks_to_msecs(ieee80211_hwmp_roottimeout)); 1807 1808 preq.preq_flags = IEEE80211_MESHPREQ_FLAGS_AM; 1809 preq.preq_hopcount = 0; 1810 preq.preq_ttl = ms->ms_ttl; 1811 preq.preq_id = 0; /* reserved */ 1812 IEEE80211_ADDR_COPY(preq.preq_origaddr, vap->iv_myaddr); 1813 preq.preq_origseq = ++hs->hs_seq; 1814 preq.preq_lifetime = ieee80211_hwmp_roottimeout; 1815 preq.preq_metric = IEEE80211_MESHLMETRIC_INITIALVAL; 1816 preq.preq_tcount = 1; 1817 preq.preq_targets[0].target_flags = IEEE80211_MESHPREQ_TFLAGS_TO; 1818 /* NB: IEEE80211_MESHPREQ_TFLAGS_USN = 0 implicitly implied */ 1819 IEEE80211_ADDR_COPY(preq.preq_targets[0].target_addr, rann->rann_addr); 1820 preq.preq_targets[0].target_seq = rann->rann_seq; 1821 /* XXX: if rootconfint have not passed, we built this preq in vain */ 1822 hwmp_send_preq(vap, wh->i_addr2, &preq, &hr->hr_lastrootconf, 1823 &ieee80211_hwmp_rootconfint); 1824 1825 /* propagate a RANN */ 1826 if (rt->rt_flags & IEEE80211_MESHRT_FLAGS_VALID && 1827 rann->rann_ttl > 1 && 1828 ms->ms_flags & IEEE80211_MESHFLAGS_FWD) { 1829 hr->hr_seq = rann->rann_seq; 1830 memcpy(&prann, rann, sizeof(prann)); 1831 prann.rann_hopcount += 1; 1832 prann.rann_ttl -= 1; 1833 prann.rann_metric += ms->ms_pmetric->mpm_metric(ni); 1834 hwmp_send_rann(vap, broadcastaddr, &prann); 1835 } 1836 } 1837 1838 static int 1839 hwmp_send_rann(struct ieee80211vap *vap, 1840 const uint8_t da[IEEE80211_ADDR_LEN], 1841 struct ieee80211_meshrann_ie *rann) 1842 { 1843 /* 1844 * mesh rann action frame format 1845 * [6] da 1846 * [6] sa 1847 * [6] addr3 = sa 1848 * [1] action 1849 * [1] category 1850 * [tlv] root annoucement 1851 */ 1852 rann->rann_ie = IEEE80211_ELEMID_MESHRANN; 1853 rann->rann_len = IEEE80211_MESHRANN_BASE_SZ; 1854 return hwmp_send_action(vap, da, (uint8_t *)rann, rann->rann_len + 2); 1855 } 1856 1857 #define PREQ_TFLAGS(n) preq.preq_targets[n].target_flags 1858 #define PREQ_TADDR(n) preq.preq_targets[n].target_addr 1859 #define PREQ_TSEQ(n) preq.preq_targets[n].target_seq 1860 static void 1861 hwmp_rediscover_cb(void *arg) 1862 { 1863 struct ieee80211_mesh_route *rt = arg; 1864 struct ieee80211vap *vap = rt->rt_vap; 1865 struct ieee80211_hwmp_state *hs = vap->iv_hwmp; 1866 struct ieee80211_mesh_state *ms = vap->iv_mesh; 1867 struct ieee80211_hwmp_route *hr; 1868 struct ieee80211_meshpreq_ie preq; /* Optimize: storing first preq? */ 1869 1870 if ((rt->rt_flags & IEEE80211_MESHRT_FLAGS_VALID)) 1871 return ; /* nothing to do */ 1872 1873 hr = IEEE80211_MESH_ROUTE_PRIV(rt, struct ieee80211_hwmp_route); 1874 if (hr->hr_preqretries >= 1875 ieee80211_hwmp_maxpreq_retries) { 1876 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_ANY, 1877 rt->rt_dest, "%s", 1878 "max number of discovery, send queued frames to GATE"); 1879 ieee80211_mesh_forward_to_gates(vap, rt); 1880 vap->iv_stats.is_mesh_fwd_nopath++; 1881 return ; /* XXX: flush queue? */ 1882 } 1883 1884 hr->hr_preqretries++; 1885 1886 1887 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_HWMP, rt->rt_dest, 1888 "start path rediscovery , target seq %u", hr->hr_seq); 1889 /* 1890 * Try to discover the path for this node. 1891 * Group addressed PREQ Case A 1892 */ 1893 preq.preq_flags = 0; 1894 preq.preq_hopcount = 0; 1895 preq.preq_ttl = ms->ms_ttl; 1896 preq.preq_id = ++hs->hs_preqid; 1897 IEEE80211_ADDR_COPY(preq.preq_origaddr, vap->iv_myaddr); 1898 preq.preq_origseq = hr->hr_origseq; 1899 preq.preq_lifetime = ticks_to_msecs(ieee80211_hwmp_pathtimeout); 1900 preq.preq_metric = IEEE80211_MESHLMETRIC_INITIALVAL; 1901 preq.preq_tcount = 1; 1902 IEEE80211_ADDR_COPY(PREQ_TADDR(0), rt->rt_dest); 1903 PREQ_TFLAGS(0) = 0; 1904 if (ieee80211_hwmp_targetonly) 1905 PREQ_TFLAGS(0) |= IEEE80211_MESHPREQ_TFLAGS_TO; 1906 PREQ_TFLAGS(0) |= IEEE80211_MESHPREQ_TFLAGS_USN; 1907 PREQ_TSEQ(0) = 0; /* RESERVED when USN flag is set */ 1908 /* XXX check return value */ 1909 hwmp_send_preq(vap, broadcastaddr, &preq, &hr->hr_lastpreq, 1910 &ieee80211_hwmp_preqminint); 1911 callout_reset(&rt->rt_discovery, 1912 ieee80211_hwmp_net_diameter_traversaltime * 2, 1913 hwmp_rediscover_cb, rt); 1914 } 1915 1916 static struct ieee80211_node * 1917 hwmp_discover(struct ieee80211vap *vap, 1918 const uint8_t dest[IEEE80211_ADDR_LEN], struct mbuf *m) 1919 { 1920 struct ieee80211_hwmp_state *hs = vap->iv_hwmp; 1921 struct ieee80211_mesh_state *ms = vap->iv_mesh; 1922 struct ieee80211_mesh_route *rt = NULL; 1923 struct ieee80211_hwmp_route *hr; 1924 struct ieee80211_meshpreq_ie preq; 1925 struct ieee80211_node *ni; 1926 int sendpreq = 0; 1927 1928 KASSERT(vap->iv_opmode == IEEE80211_M_MBSS, 1929 ("not a mesh vap, opmode %d", vap->iv_opmode)); 1930 1931 KASSERT(!IEEE80211_ADDR_EQ(vap->iv_myaddr, dest), 1932 ("%s: discovering self!", __func__)); 1933 1934 ni = NULL; 1935 if (!IEEE80211_IS_MULTICAST(dest)) { 1936 rt = ieee80211_mesh_rt_find(vap, dest); 1937 if (rt == NULL) { 1938 rt = ieee80211_mesh_rt_add(vap, dest); 1939 if (rt == NULL) { 1940 IEEE80211_NOTE(vap, IEEE80211_MSG_HWMP, 1941 ni, "unable to add discovery path to %6D", 1942 dest, ":"); 1943 vap->iv_stats.is_mesh_rtaddfailed++; 1944 goto done; 1945 } 1946 } 1947 hr = IEEE80211_MESH_ROUTE_PRIV(rt, 1948 struct ieee80211_hwmp_route); 1949 if (rt->rt_flags & IEEE80211_MESHRT_FLAGS_DISCOVER) { 1950 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_HWMP, dest, 1951 "%s", "already discovering queue frame until path found"); 1952 sendpreq = 1; 1953 goto done; 1954 } 1955 if ((rt->rt_flags & IEEE80211_MESHRT_FLAGS_VALID) == 0) { 1956 if (hr->hr_lastdiscovery != 0 && 1957 (ticks - hr->hr_lastdiscovery < 1958 (ieee80211_hwmp_net_diameter_traversaltime * 2))) { 1959 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY, 1960 dest, NULL, "%s", 1961 "too frequent discovery requeust"); 1962 sendpreq = 1; 1963 goto done; 1964 } 1965 hr->hr_lastdiscovery = ticks; 1966 if (hr->hr_preqretries >= 1967 ieee80211_hwmp_maxpreq_retries) { 1968 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY, 1969 dest, NULL, "%s", 1970 "no valid path , max number of discovery"); 1971 vap->iv_stats.is_mesh_fwd_nopath++; 1972 goto done; 1973 } 1974 rt->rt_flags = IEEE80211_MESHRT_FLAGS_DISCOVER; 1975 hr->hr_preqretries++; 1976 if (hr->hr_origseq == 0) 1977 hr->hr_origseq = ++hs->hs_seq; 1978 rt->rt_metric = IEEE80211_MESHLMETRIC_INITIALVAL; 1979 sendpreq = 1; 1980 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_HWMP, dest, 1981 "start path discovery (src %s), target seq %u", 1982 m == NULL ? "<none>" : ether_sprintf( 1983 mtod(m, struct ether_header *)->ether_shost), 1984 hr->hr_seq); 1985 /* 1986 * Try to discover the path for this node. 1987 * Group addressed PREQ Case A 1988 */ 1989 preq.preq_flags = 0; 1990 preq.preq_hopcount = 0; 1991 preq.preq_ttl = ms->ms_ttl; 1992 preq.preq_id = ++hs->hs_preqid; 1993 IEEE80211_ADDR_COPY(preq.preq_origaddr, vap->iv_myaddr); 1994 preq.preq_origseq = hr->hr_origseq; 1995 preq.preq_lifetime = 1996 ticks_to_msecs(ieee80211_hwmp_pathtimeout); 1997 preq.preq_metric = IEEE80211_MESHLMETRIC_INITIALVAL; 1998 preq.preq_tcount = 1; 1999 IEEE80211_ADDR_COPY(PREQ_TADDR(0), dest); 2000 PREQ_TFLAGS(0) = 0; 2001 if (ieee80211_hwmp_targetonly) 2002 PREQ_TFLAGS(0) |= IEEE80211_MESHPREQ_TFLAGS_TO; 2003 PREQ_TFLAGS(0) |= IEEE80211_MESHPREQ_TFLAGS_USN; 2004 PREQ_TSEQ(0) = 0; /* RESERVED when USN flag is set */ 2005 /* XXX check return value */ 2006 hwmp_send_preq(vap, broadcastaddr, &preq, 2007 &hr->hr_lastpreq, &ieee80211_hwmp_preqminint); 2008 callout_reset(&rt->rt_discovery, 2009 ieee80211_hwmp_net_diameter_traversaltime * 2, 2010 hwmp_rediscover_cb, rt); 2011 } 2012 if (rt->rt_flags & IEEE80211_MESHRT_FLAGS_VALID) 2013 ni = ieee80211_find_txnode(vap, rt->rt_nexthop); 2014 } else { 2015 ni = ieee80211_find_txnode(vap, dest); 2016 /* NB: if null then we leak mbuf */ 2017 KASSERT(ni != NULL, ("leak mcast frame")); 2018 return ni; 2019 } 2020 done: 2021 if (ni == NULL && m != NULL) { 2022 if (sendpreq) { 2023 struct ieee80211com *ic = vap->iv_ic; 2024 /* 2025 * Queue packet for transmit when path discovery 2026 * completes. If discovery never completes the 2027 * frame will be flushed by way of the aging timer. 2028 */ 2029 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_HWMP, dest, 2030 "%s", "queue frame until path found"); 2031 m->m_pkthdr.rcvif = (void *)(uintptr_t) 2032 ieee80211_mac_hash(ic, dest); 2033 /* XXX age chosen randomly */ 2034 ieee80211_ageq_append(&ic->ic_stageq, m, 2035 IEEE80211_INACT_WAIT); 2036 } else { 2037 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_HWMP, 2038 dest, NULL, "%s", "no valid path to this node"); 2039 m_freem(m); 2040 } 2041 } 2042 return ni; 2043 } 2044 #undef PREQ_TFLAGS 2045 #undef PREQ_TADDR 2046 #undef PREQ_TSEQ 2047 2048 static int 2049 hwmp_ioctl_get80211(struct ieee80211vap *vap, struct ieee80211req *ireq) 2050 { 2051 struct ieee80211_hwmp_state *hs = vap->iv_hwmp; 2052 int error; 2053 2054 if (vap->iv_opmode != IEEE80211_M_MBSS) 2055 return ENOSYS; 2056 error = 0; 2057 switch (ireq->i_type) { 2058 case IEEE80211_IOC_HWMP_ROOTMODE: 2059 ireq->i_val = hs->hs_rootmode; 2060 break; 2061 case IEEE80211_IOC_HWMP_MAXHOPS: 2062 ireq->i_val = hs->hs_maxhops; 2063 break; 2064 default: 2065 return ENOSYS; 2066 } 2067 return error; 2068 } 2069 IEEE80211_IOCTL_GET(hwmp, hwmp_ioctl_get80211); 2070 2071 static int 2072 hwmp_ioctl_set80211(struct ieee80211vap *vap, struct ieee80211req *ireq) 2073 { 2074 struct ieee80211_hwmp_state *hs = vap->iv_hwmp; 2075 int error; 2076 2077 if (vap->iv_opmode != IEEE80211_M_MBSS) 2078 return ENOSYS; 2079 error = 0; 2080 switch (ireq->i_type) { 2081 case IEEE80211_IOC_HWMP_ROOTMODE: 2082 if (ireq->i_val < 0 || ireq->i_val > 3) 2083 return EINVAL; 2084 hs->hs_rootmode = ireq->i_val; 2085 hwmp_rootmode_setup(vap); 2086 break; 2087 case IEEE80211_IOC_HWMP_MAXHOPS: 2088 if (ireq->i_val <= 0 || ireq->i_val > 255) 2089 return EINVAL; 2090 hs->hs_maxhops = ireq->i_val; 2091 break; 2092 default: 2093 return ENOSYS; 2094 } 2095 return error; 2096 } 2097 IEEE80211_IOCTL_SET(hwmp, hwmp_ioctl_set80211); 2098