2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2009 The FreeBSD Foundation
6 * This software was developed by Rui Paulo under sponsorship from the
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
36 * IEEE 802.11s Hybrid Wireless Mesh Protocol, HWMP.
38 * Based on March 2009, D3.0 802.11s draft spec.
43 #include <sys/param.h>
44 #include <sys/systm.h>
46 #include <sys/malloc.h>
47 #include <sys/kernel.h>
49 #include <sys/socket.h>
50 #include <sys/sockio.h>
51 #include <sys/endian.h>
52 #include <sys/errno.h>
54 #include <sys/sysctl.h>
57 #include <net/if_media.h>
58 #include <net/if_llc.h>
59 #include <net/ethernet.h>
63 #include <net80211/ieee80211_var.h>
64 #include <net80211/ieee80211_action.h>
65 #include <net80211/ieee80211_input.h>
66 #include <net80211/ieee80211_mesh.h>
68 static void hwmp_vattach(struct ieee80211vap *);
69 static void hwmp_vdetach(struct ieee80211vap *);
70 static int hwmp_newstate(struct ieee80211vap *,
71 enum ieee80211_state, int);
72 static int hwmp_send_action(struct ieee80211vap *,
73 const uint8_t [IEEE80211_ADDR_LEN],
75 static uint8_t * hwmp_add_meshpreq(uint8_t *,
76 const struct ieee80211_meshpreq_ie *);
77 static uint8_t * hwmp_add_meshprep(uint8_t *,
78 const struct ieee80211_meshprep_ie *);
79 static uint8_t * hwmp_add_meshperr(uint8_t *,
80 const struct ieee80211_meshperr_ie *);
81 static uint8_t * hwmp_add_meshrann(uint8_t *,
82 const struct ieee80211_meshrann_ie *);
83 static void hwmp_rootmode_setup(struct ieee80211vap *);
84 static void hwmp_rootmode_cb(void *);
85 static void hwmp_rootmode_rann_cb(void *);
86 static void hwmp_recv_preq(struct ieee80211vap *, struct ieee80211_node *,
87 const struct ieee80211_frame *,
88 const struct ieee80211_meshpreq_ie *);
89 static int hwmp_send_preq(struct ieee80211vap *,
90 const uint8_t [IEEE80211_ADDR_LEN],
91 struct ieee80211_meshpreq_ie *,
92 struct timeval *, struct timeval *);
93 static void hwmp_recv_prep(struct ieee80211vap *, struct ieee80211_node *,
94 const struct ieee80211_frame *,
95 const struct ieee80211_meshprep_ie *);
96 static int hwmp_send_prep(struct ieee80211vap *,
97 const uint8_t [IEEE80211_ADDR_LEN],
98 struct ieee80211_meshprep_ie *);
99 static void hwmp_recv_perr(struct ieee80211vap *, struct ieee80211_node *,
100 const struct ieee80211_frame *,
101 const struct ieee80211_meshperr_ie *);
102 static int hwmp_send_perr(struct ieee80211vap *,
103 const uint8_t [IEEE80211_ADDR_LEN],
104 struct ieee80211_meshperr_ie *);
105 static void hwmp_senderror(struct ieee80211vap *,
106 const uint8_t [IEEE80211_ADDR_LEN],
107 struct ieee80211_mesh_route *, int);
108 static void hwmp_recv_rann(struct ieee80211vap *, struct ieee80211_node *,
109 const struct ieee80211_frame *,
110 const struct ieee80211_meshrann_ie *);
111 static int hwmp_send_rann(struct ieee80211vap *,
112 const uint8_t [IEEE80211_ADDR_LEN],
113 struct ieee80211_meshrann_ie *);
114 static struct ieee80211_node *
115 hwmp_discover(struct ieee80211vap *,
116 const uint8_t [IEEE80211_ADDR_LEN], struct mbuf *);
117 static void hwmp_peerdown(struct ieee80211_node *);
119 static struct timeval ieee80211_hwmp_preqminint = { 0, 100000 };
120 static struct timeval ieee80211_hwmp_perrminint = { 0, 100000 };
122 /* NB: the Target Address set in a Proactive PREQ is the broadcast address. */
123 static const uint8_t broadcastaddr[IEEE80211_ADDR_LEN] =
124 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
126 typedef uint32_t ieee80211_hwmp_seq;
127 #define HWMP_SEQ_LT(a, b) ((int32_t)((a)-(b)) < 0)
128 #define HWMP_SEQ_LEQ(a, b) ((int32_t)((a)-(b)) <= 0)
129 #define HWMP_SEQ_EQ(a, b) ((int32_t)((a)-(b)) == 0)
130 #define HWMP_SEQ_GT(a, b) ((int32_t)((a)-(b)) > 0)
132 #define HWMP_SEQ_MAX(a, b) (a > b ? a : b)
135 * Private extension of ieee80211_mesh_route.
137 struct ieee80211_hwmp_route {
138 ieee80211_hwmp_seq hr_seq; /* last HWMP seq seen from dst*/
139 ieee80211_hwmp_seq hr_preqid; /* last PREQ ID seen from dst */
140 ieee80211_hwmp_seq hr_origseq; /* seq. no. on our latest PREQ*/
141 struct timeval hr_lastpreq; /* last time we sent a PREQ */
142 struct timeval hr_lastrootconf; /* last sent PREQ root conf */
143 int hr_preqretries; /* number of discoveries */
144 int hr_lastdiscovery; /* last discovery in ticks */
146 struct ieee80211_hwmp_state {
147 ieee80211_hwmp_seq hs_seq; /* next seq to be used */
148 ieee80211_hwmp_seq hs_preqid; /* next PREQ ID to be used */
149 int hs_rootmode; /* proactive HWMP */
150 struct timeval hs_lastperr; /* last time we sent a PERR */
151 struct callout hs_roottimer;
152 uint8_t hs_maxhops; /* max hop count */
155 static SYSCTL_NODE(_net_wlan, OID_AUTO, hwmp, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
156 "IEEE 802.11s HWMP parameters");
157 static int ieee80211_hwmp_targetonly = 0;
158 SYSCTL_INT(_net_wlan_hwmp, OID_AUTO, targetonly, CTLFLAG_RW,
159 &ieee80211_hwmp_targetonly, 0, "Set TO bit on generated PREQs");
160 static int ieee80211_hwmp_pathtimeout = -1;
161 SYSCTL_PROC(_net_wlan_hwmp, OID_AUTO, pathlifetime,
162 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
163 &ieee80211_hwmp_pathtimeout, 0, ieee80211_sysctl_msecs_ticks, "I",
164 "path entry lifetime (ms)");
165 static int ieee80211_hwmp_maxpreq_retries = -1;
166 SYSCTL_PROC(_net_wlan_hwmp, OID_AUTO, maxpreq_retries,
167 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
168 &ieee80211_hwmp_maxpreq_retries, 0, ieee80211_sysctl_msecs_ticks, "I",
169 "maximum number of preq retries");
170 static int ieee80211_hwmp_net_diameter_traversaltime = -1;
171 SYSCTL_PROC(_net_wlan_hwmp, OID_AUTO, net_diameter_traversal_time,
172 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
173 &ieee80211_hwmp_net_diameter_traversaltime, 0,
174 ieee80211_sysctl_msecs_ticks, "I",
175 "estimate traversal time across the MBSS (ms)");
176 static int ieee80211_hwmp_roottimeout = -1;
177 SYSCTL_PROC(_net_wlan_hwmp, OID_AUTO, roottimeout,
178 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
179 &ieee80211_hwmp_roottimeout, 0, ieee80211_sysctl_msecs_ticks, "I",
180 "root PREQ timeout (ms)");
181 static int ieee80211_hwmp_rootint = -1;
182 SYSCTL_PROC(_net_wlan_hwmp, OID_AUTO, rootint,
183 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
184 &ieee80211_hwmp_rootint, 0, ieee80211_sysctl_msecs_ticks, "I",
185 "root interval (ms)");
186 static int ieee80211_hwmp_rannint = -1;
187 SYSCTL_PROC(_net_wlan_hwmp, OID_AUTO, rannint,
188 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
189 &ieee80211_hwmp_rannint, 0, ieee80211_sysctl_msecs_ticks, "I",
190 "root announcement interval (ms)");
191 static struct timeval ieee80211_hwmp_rootconfint = { 0, 0 };
192 static int ieee80211_hwmp_rootconfint_internal = -1;
193 SYSCTL_PROC(_net_wlan_hwmp, OID_AUTO, rootconfint,
194 CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE,
195 &ieee80211_hwmp_rootconfint_internal, 0, ieee80211_sysctl_msecs_ticks, "I",
196 "root confirmation interval (ms) (read-only)");
198 #define IEEE80211_HWMP_DEFAULT_MAXHOPS 31
200 static ieee80211_recv_action_func hwmp_recv_action_meshpath;
202 static struct ieee80211_mesh_proto_path mesh_proto_hwmp = {
204 .mpp_ie = IEEE80211_MESHCONF_PATH_HWMP,
205 .mpp_discover = hwmp_discover,
206 .mpp_peerdown = hwmp_peerdown,
207 .mpp_senderror = hwmp_senderror,
208 .mpp_vattach = hwmp_vattach,
209 .mpp_vdetach = hwmp_vdetach,
210 .mpp_newstate = hwmp_newstate,
211 .mpp_privlen = sizeof(struct ieee80211_hwmp_route),
213 SYSCTL_PROC(_net_wlan_hwmp, OID_AUTO, inact,
214 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
215 &mesh_proto_hwmp.mpp_inact, 0, ieee80211_sysctl_msecs_ticks, "I",
216 "mesh route inactivity timeout (ms)");
219 ieee80211_hwmp_init(void)
221 /* Default values as per amendment */
222 ieee80211_hwmp_pathtimeout = msecs_to_ticks(5*1000);
223 ieee80211_hwmp_roottimeout = msecs_to_ticks(5*1000);
224 ieee80211_hwmp_rootint = msecs_to_ticks(2*1000);
225 ieee80211_hwmp_rannint = msecs_to_ticks(1*1000);
226 ieee80211_hwmp_rootconfint_internal = msecs_to_ticks(2*1000);
227 ieee80211_hwmp_maxpreq_retries = 3;
229 * (TU): A measurement of time equal to 1024 μs,
232 ieee80211_hwmp_net_diameter_traversaltime = msecs_to_ticks(512);
235 * NB: I dont know how to make SYSCTL_PROC that calls ms to ticks
236 * and return a struct timeval...
238 ieee80211_hwmp_rootconfint.tv_usec =
239 ieee80211_hwmp_rootconfint_internal * 1000;
242 * Register action frame handler.
244 ieee80211_recv_action_register(IEEE80211_ACTION_CAT_MESH,
245 IEEE80211_ACTION_MESH_HWMP, hwmp_recv_action_meshpath);
247 /* NB: default is 5 secs per spec */
248 mesh_proto_hwmp.mpp_inact = msecs_to_ticks(5*1000);
253 ieee80211_mesh_register_proto_path(&mesh_proto_hwmp);
255 SYSINIT(wlan_hwmp, SI_SUB_DRIVERS, SI_ORDER_SECOND, ieee80211_hwmp_init, NULL);
258 hwmp_vattach(struct ieee80211vap *vap)
260 struct ieee80211_hwmp_state *hs;
262 KASSERT(vap->iv_opmode == IEEE80211_M_MBSS,
263 ("not a mesh vap, opmode %d", vap->iv_opmode));
265 hs = IEEE80211_MALLOC(sizeof(struct ieee80211_hwmp_state), M_80211_VAP,
266 IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
268 printf("%s: couldn't alloc HWMP state\n", __func__);
271 hs->hs_maxhops = IEEE80211_HWMP_DEFAULT_MAXHOPS;
272 callout_init(&hs->hs_roottimer, 1);
277 hwmp_vdetach(struct ieee80211vap *vap)
279 struct ieee80211_hwmp_state *hs = vap->iv_hwmp;
281 callout_drain(&hs->hs_roottimer);
282 IEEE80211_FREE(vap->iv_hwmp, M_80211_VAP);
287 hwmp_newstate(struct ieee80211vap *vap, enum ieee80211_state ostate, int arg)
289 enum ieee80211_state nstate = vap->iv_state;
290 struct ieee80211_hwmp_state *hs = vap->iv_hwmp;
292 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE, "%s: %s -> %s (%d)\n",
293 __func__, ieee80211_state_name[ostate],
294 ieee80211_state_name[nstate], arg);
296 if (nstate != IEEE80211_S_RUN && ostate == IEEE80211_S_RUN)
297 callout_drain(&hs->hs_roottimer);
298 if (nstate == IEEE80211_S_RUN)
299 hwmp_rootmode_setup(vap);
304 * Verify the length of an HWMP PREQ and return the number
305 * of destinations >= 1, if verification fails -1 is returned.
308 verify_mesh_preq_len(struct ieee80211vap *vap,
309 const struct ieee80211_frame *wh, const uint8_t *iefrm)
313 if (iefrm[2] & IEEE80211_MESHPREQ_FLAGS_AE) {
314 /* Originator External Address present */
315 alloc_sz = IEEE80211_MESHPREQ_BASE_SZ_AE;
316 ndest = iefrm[IEEE80211_MESHPREQ_TCNT_OFFSET_AE];
318 /* w/o Originator External Address */
319 alloc_sz = IEEE80211_MESHPREQ_BASE_SZ;
320 ndest = iefrm[IEEE80211_MESHPREQ_TCNT_OFFSET];
322 alloc_sz += ndest * IEEE80211_MESHPREQ_TRGT_SZ;
324 if(iefrm[1] != (alloc_sz)) {
325 IEEE80211_DISCARD(vap,
326 IEEE80211_MSG_ACTION | IEEE80211_MSG_HWMP,
327 wh, NULL, "PREQ (AE=%s) with wrong len",
328 iefrm[2] & IEEE80211_MESHPREQ_FLAGS_AE ? "1" : "0");
335 * Verify the length of an HWMP PREP and returns 1 on success,
339 verify_mesh_prep_len(struct ieee80211vap *vap,
340 const struct ieee80211_frame *wh, const uint8_t *iefrm)
343 if (iefrm[2] & IEEE80211_MESHPREP_FLAGS_AE) {
344 if (iefrm[1] == IEEE80211_MESHPREP_BASE_SZ_AE)
345 alloc_sz = IEEE80211_MESHPREP_BASE_SZ_AE;
346 } else if (iefrm[1] == IEEE80211_MESHPREP_BASE_SZ)
347 alloc_sz = IEEE80211_MESHPREP_BASE_SZ;
349 IEEE80211_DISCARD(vap,
350 IEEE80211_MSG_ACTION | IEEE80211_MSG_HWMP,
351 wh, NULL, "PREP (AE=%s) with wrong len",
352 iefrm[2] & IEEE80211_MESHPREP_FLAGS_AE ? "1" : "0");
359 * Verify the length of an HWMP PERR and return the number
360 * of destinations >= 1, if verification fails -1 is returned.
363 verify_mesh_perr_len(struct ieee80211vap *vap,
364 const struct ieee80211_frame *wh, const uint8_t *iefrm)
367 const uint8_t *iefrm_t = iefrm;
368 uint8_t ndest = iefrm_t[IEEE80211_MESHPERR_NDEST_OFFSET];
371 if(ndest > IEEE80211_MESHPERR_MAXDEST) {
372 IEEE80211_DISCARD(vap,
373 IEEE80211_MSG_ACTION | IEEE80211_MSG_HWMP,
374 wh, NULL, "PERR with wrong number of destionat (>19), %u",
379 iefrm_t += IEEE80211_MESHPERR_NDEST_OFFSET + 1; /* flag is next field */
380 /* We need to check each destination flag to know size */
381 for(i = 0; i<ndest; i++) {
382 if ((*iefrm_t) & IEEE80211_MESHPERR_FLAGS_AE)
383 iefrm_t += IEEE80211_MESHPERR_DEST_SZ_AE;
385 iefrm_t += IEEE80211_MESHPERR_DEST_SZ;
388 alloc_sz = (iefrm_t - iefrm) - 2; /* action + code */
389 if(alloc_sz != iefrm[1]) {
390 IEEE80211_DISCARD(vap,
391 IEEE80211_MSG_ACTION | IEEE80211_MSG_HWMP,
392 wh, NULL, "%s", "PERR with wrong len");
399 hwmp_recv_action_meshpath(struct ieee80211_node *ni,
400 const struct ieee80211_frame *wh,
401 const uint8_t *frm, const uint8_t *efrm)
403 struct ieee80211vap *vap = ni->ni_vap;
404 struct ieee80211_meshpreq_ie *preq;
405 struct ieee80211_meshprep_ie *prep;
406 struct ieee80211_meshperr_ie *perr;
407 struct ieee80211_meshrann_ie rann;
408 const uint8_t *iefrm = frm + 2; /* action + code */
409 const uint8_t *iefrm_t = iefrm; /* temporary pointer */
413 while (efrm - iefrm > 1) {
414 IEEE80211_VERIFY_LENGTH(efrm - iefrm, iefrm[1] + 2, return 0);
416 case IEEE80211_ELEMID_MESHPREQ:
421 ndest = verify_mesh_preq_len(vap, wh, iefrm_t);
423 vap->iv_stats.is_rx_mgtdiscard++;
426 preq = IEEE80211_MALLOC(sizeof(*preq) +
427 (ndest - 1) * sizeof(*preq->preq_targets),
429 IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
430 KASSERT(preq != NULL, ("preq == NULL"));
432 preq->preq_ie = *iefrm_t++;
433 preq->preq_len = *iefrm_t++;
434 preq->preq_flags = *iefrm_t++;
435 preq->preq_hopcount = *iefrm_t++;
436 preq->preq_ttl = *iefrm_t++;
437 preq->preq_id = le32dec(iefrm_t); iefrm_t += 4;
438 IEEE80211_ADDR_COPY(preq->preq_origaddr, iefrm_t);
440 preq->preq_origseq = le32dec(iefrm_t); iefrm_t += 4;
441 /* NB: may have Originator Proxied Address */
442 if (preq->preq_flags & IEEE80211_MESHPREQ_FLAGS_AE) {
444 preq->preq_orig_ext_addr, iefrm_t);
447 preq->preq_lifetime = le32dec(iefrm_t); iefrm_t += 4;
448 preq->preq_metric = le32dec(iefrm_t); iefrm_t += 4;
449 preq->preq_tcount = *iefrm_t++;
451 for (i = 0; i < preq->preq_tcount; i++) {
452 preq->preq_targets[i].target_flags = *iefrm_t++;
454 preq->preq_targets[i].target_addr, iefrm_t);
456 preq->preq_targets[i].target_seq =
461 hwmp_recv_preq(vap, ni, wh, preq);
462 IEEE80211_FREE(preq, M_80211_MESH_PREQ);
466 case IEEE80211_ELEMID_MESHPREP:
469 ndest = verify_mesh_prep_len(vap, wh, iefrm_t);
471 vap->iv_stats.is_rx_mgtdiscard++;
474 prep = IEEE80211_MALLOC(sizeof(*prep),
476 IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
477 KASSERT(prep != NULL, ("prep == NULL"));
479 prep->prep_ie = *iefrm_t++;
480 prep->prep_len = *iefrm_t++;
481 prep->prep_flags = *iefrm_t++;
482 prep->prep_hopcount = *iefrm_t++;
483 prep->prep_ttl = *iefrm_t++;
484 IEEE80211_ADDR_COPY(prep->prep_targetaddr, iefrm_t);
486 prep->prep_targetseq = le32dec(iefrm_t); iefrm_t += 4;
487 /* NB: May have Target Proxied Address */
488 if (prep->prep_flags & IEEE80211_MESHPREP_FLAGS_AE) {
490 prep->prep_target_ext_addr, iefrm_t);
493 prep->prep_lifetime = le32dec(iefrm_t); iefrm_t += 4;
494 prep->prep_metric = le32dec(iefrm_t); iefrm_t += 4;
495 IEEE80211_ADDR_COPY(prep->prep_origaddr, iefrm_t);
497 prep->prep_origseq = le32dec(iefrm_t); iefrm_t += 4;
499 hwmp_recv_prep(vap, ni, wh, prep);
500 IEEE80211_FREE(prep, M_80211_MESH_PREP);
504 case IEEE80211_ELEMID_MESHPERR:
509 ndest = verify_mesh_perr_len(vap, wh, iefrm_t);
511 vap->iv_stats.is_rx_mgtdiscard++;
514 perr = IEEE80211_MALLOC(sizeof(*perr) +
515 (ndest - 1) * sizeof(*perr->perr_dests),
517 IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
518 KASSERT(perr != NULL, ("perr == NULL"));
520 perr->perr_ie = *iefrm_t++;
521 perr->perr_len = *iefrm_t++;
522 perr->perr_ttl = *iefrm_t++;
523 perr->perr_ndests = *iefrm_t++;
525 for (i = 0; i<perr->perr_ndests; i++) {
526 perr->perr_dests[i].dest_flags = *iefrm_t++;
528 perr->perr_dests[i].dest_addr, iefrm_t);
530 perr->perr_dests[i].dest_seq = le32dec(iefrm_t);
532 /* NB: May have Target Proxied Address */
533 if (perr->perr_dests[i].dest_flags &
534 IEEE80211_MESHPERR_FLAGS_AE) {
536 perr->perr_dests[i].dest_ext_addr,
540 perr->perr_dests[i].dest_rcode =
545 hwmp_recv_perr(vap, ni, wh, perr);
546 IEEE80211_FREE(perr, M_80211_MESH_PERR);
550 case IEEE80211_ELEMID_MESHRANN:
552 const struct ieee80211_meshrann_ie *mrann =
553 (const struct ieee80211_meshrann_ie *) iefrm;
554 if (mrann->rann_len !=
555 sizeof(struct ieee80211_meshrann_ie) - 2) {
556 IEEE80211_DISCARD(vap,
557 IEEE80211_MSG_ACTION | IEEE80211_MSG_HWMP,
558 wh, NULL, "%s", "RAN with wrong len");
559 vap->iv_stats.is_rx_mgtdiscard++;
562 memcpy(&rann, mrann, sizeof(rann));
563 rann.rann_seq = le32dec(&mrann->rann_seq);
564 rann.rann_interval = le32dec(&mrann->rann_interval);
565 rann.rann_metric = le32dec(&mrann->rann_metric);
566 hwmp_recv_rann(vap, ni, wh, &rann);
571 iefrm += iefrm[1] + 2;
574 IEEE80211_DISCARD(vap,
575 IEEE80211_MSG_ACTION | IEEE80211_MSG_HWMP,
576 wh, NULL, "%s", "PATH SEL action without IE");
577 vap->iv_stats.is_rx_mgtdiscard++;
583 hwmp_send_action(struct ieee80211vap *vap,
584 const uint8_t da[IEEE80211_ADDR_LEN],
585 uint8_t *ie, size_t len)
587 struct ieee80211_node *ni;
588 struct ieee80211com *ic;
589 struct ieee80211_bpf_params params;
594 if (IEEE80211_IS_MULTICAST(da)) {
595 ni = ieee80211_ref_node(vap->iv_bss);
596 #ifdef IEEE80211_DEBUG_REFCNT
597 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
598 "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n",
600 ni, ether_sprintf(ni->ni_macaddr),
601 ieee80211_node_refcnt(ni)+1);
603 ieee80211_ref_node(ni);
606 ni = ieee80211_mesh_find_txnode(vap, da);
608 if (vap->iv_state == IEEE80211_S_CAC) {
609 IEEE80211_NOTE(vap, IEEE80211_MSG_OUTPUT, ni,
610 "block %s frame in CAC state", "HWMP action");
611 vap->iv_stats.is_tx_badstate++;
612 return EIO; /* XXX */
615 KASSERT(ni != NULL, ("null node"));
618 m = ieee80211_getmgtframe(&frm,
619 ic->ic_headroom + sizeof(struct ieee80211_frame),
620 sizeof(struct ieee80211_action) + len
623 ieee80211_free_node(ni);
624 vap->iv_stats.is_tx_nobuf++;
627 *frm++ = IEEE80211_ACTION_CAT_MESH;
628 *frm++ = IEEE80211_ACTION_MESH_HWMP;
630 case IEEE80211_ELEMID_MESHPREQ:
631 frm = hwmp_add_meshpreq(frm,
632 (struct ieee80211_meshpreq_ie *)ie);
634 case IEEE80211_ELEMID_MESHPREP:
635 frm = hwmp_add_meshprep(frm,
636 (struct ieee80211_meshprep_ie *)ie);
638 case IEEE80211_ELEMID_MESHPERR:
639 frm = hwmp_add_meshperr(frm,
640 (struct ieee80211_meshperr_ie *)ie);
642 case IEEE80211_ELEMID_MESHRANN:
643 frm = hwmp_add_meshrann(frm,
644 (struct ieee80211_meshrann_ie *)ie);
648 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
649 M_PREPEND(m, sizeof(struct ieee80211_frame), IEEE80211_M_NOWAIT);
651 ieee80211_free_node(ni);
652 vap->iv_stats.is_tx_nobuf++;
656 IEEE80211_TX_LOCK(ic);
658 ieee80211_send_setup(ni, m,
659 IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_ACTION,
660 IEEE80211_NONQOS_TID, vap->iv_myaddr, da, vap->iv_myaddr);
662 m->m_flags |= M_ENCAP; /* mark encapsulated */
663 IEEE80211_NODE_STAT(ni, tx_mgmt);
665 memset(¶ms, 0, sizeof(params));
666 params.ibp_pri = WME_AC_VO;
667 params.ibp_rate0 = ni->ni_txparms->mgmtrate;
668 if (IEEE80211_IS_MULTICAST(da))
671 params.ibp_try0 = ni->ni_txparms->maxretry;
672 params.ibp_power = ni->ni_txpower;
673 ret = ieee80211_raw_output(vap, ni, m, ¶ms);
674 IEEE80211_TX_UNLOCK(ic);
678 #define ADDSHORT(frm, v) do { \
682 #define ADDWORD(frm, v) do { \
687 * Add a Mesh Path Request IE to a frame.
689 #define PREQ_TFLAGS(n) preq->preq_targets[n].target_flags
690 #define PREQ_TADDR(n) preq->preq_targets[n].target_addr
691 #define PREQ_TSEQ(n) preq->preq_targets[n].target_seq
693 hwmp_add_meshpreq(uint8_t *frm, const struct ieee80211_meshpreq_ie *preq)
697 *frm++ = IEEE80211_ELEMID_MESHPREQ;
698 *frm++ = preq->preq_len; /* len already calculated */
699 *frm++ = preq->preq_flags;
700 *frm++ = preq->preq_hopcount;
701 *frm++ = preq->preq_ttl;
702 ADDWORD(frm, preq->preq_id);
703 IEEE80211_ADDR_COPY(frm, preq->preq_origaddr); frm += 6;
704 ADDWORD(frm, preq->preq_origseq);
705 if (preq->preq_flags & IEEE80211_MESHPREQ_FLAGS_AE) {
706 IEEE80211_ADDR_COPY(frm, preq->preq_orig_ext_addr);
709 ADDWORD(frm, preq->preq_lifetime);
710 ADDWORD(frm, preq->preq_metric);
711 *frm++ = preq->preq_tcount;
712 for (i = 0; i < preq->preq_tcount; i++) {
713 *frm++ = PREQ_TFLAGS(i);
714 IEEE80211_ADDR_COPY(frm, PREQ_TADDR(i));
716 ADDWORD(frm, PREQ_TSEQ(i));
725 * Add a Mesh Path Reply IE to a frame.
728 hwmp_add_meshprep(uint8_t *frm, const struct ieee80211_meshprep_ie *prep)
730 *frm++ = IEEE80211_ELEMID_MESHPREP;
731 *frm++ = prep->prep_len; /* len already calculated */
732 *frm++ = prep->prep_flags;
733 *frm++ = prep->prep_hopcount;
734 *frm++ = prep->prep_ttl;
735 IEEE80211_ADDR_COPY(frm, prep->prep_targetaddr); frm += 6;
736 ADDWORD(frm, prep->prep_targetseq);
737 if (prep->prep_flags & IEEE80211_MESHPREP_FLAGS_AE) {
738 IEEE80211_ADDR_COPY(frm, prep->prep_target_ext_addr);
741 ADDWORD(frm, prep->prep_lifetime);
742 ADDWORD(frm, prep->prep_metric);
743 IEEE80211_ADDR_COPY(frm, prep->prep_origaddr); frm += 6;
744 ADDWORD(frm, prep->prep_origseq);
749 * Add a Mesh Path Error IE to a frame.
751 #define PERR_DFLAGS(n) perr->perr_dests[n].dest_flags
752 #define PERR_DADDR(n) perr->perr_dests[n].dest_addr
753 #define PERR_DSEQ(n) perr->perr_dests[n].dest_seq
754 #define PERR_EXTADDR(n) perr->perr_dests[n].dest_ext_addr
755 #define PERR_DRCODE(n) perr->perr_dests[n].dest_rcode
757 hwmp_add_meshperr(uint8_t *frm, const struct ieee80211_meshperr_ie *perr)
761 *frm++ = IEEE80211_ELEMID_MESHPERR;
762 *frm++ = perr->perr_len; /* len already calculated */
763 *frm++ = perr->perr_ttl;
764 *frm++ = perr->perr_ndests;
765 for (i = 0; i < perr->perr_ndests; i++) {
766 *frm++ = PERR_DFLAGS(i);
767 IEEE80211_ADDR_COPY(frm, PERR_DADDR(i));
769 ADDWORD(frm, PERR_DSEQ(i));
770 if (PERR_DFLAGS(i) & IEEE80211_MESHPERR_FLAGS_AE) {
771 IEEE80211_ADDR_COPY(frm, PERR_EXTADDR(i));
774 ADDSHORT(frm, PERR_DRCODE(i));
785 * Add a Root Annoucement IE to a frame.
788 hwmp_add_meshrann(uint8_t *frm, const struct ieee80211_meshrann_ie *rann)
790 *frm++ = IEEE80211_ELEMID_MESHRANN;
791 *frm++ = rann->rann_len;
792 *frm++ = rann->rann_flags;
793 *frm++ = rann->rann_hopcount;
794 *frm++ = rann->rann_ttl;
795 IEEE80211_ADDR_COPY(frm, rann->rann_addr); frm += 6;
796 ADDWORD(frm, rann->rann_seq);
797 ADDWORD(frm, rann->rann_interval);
798 ADDWORD(frm, rann->rann_metric);
803 hwmp_rootmode_setup(struct ieee80211vap *vap)
805 struct ieee80211_hwmp_state *hs = vap->iv_hwmp;
806 struct ieee80211_mesh_state *ms = vap->iv_mesh;
808 switch (hs->hs_rootmode) {
809 case IEEE80211_HWMP_ROOTMODE_DISABLED:
810 callout_drain(&hs->hs_roottimer);
811 ms->ms_flags &= ~IEEE80211_MESHFLAGS_ROOT;
813 case IEEE80211_HWMP_ROOTMODE_NORMAL:
814 case IEEE80211_HWMP_ROOTMODE_PROACTIVE:
815 callout_reset(&hs->hs_roottimer, ieee80211_hwmp_rootint,
816 hwmp_rootmode_cb, vap);
817 ms->ms_flags |= IEEE80211_MESHFLAGS_ROOT;
819 case IEEE80211_HWMP_ROOTMODE_RANN:
820 callout_reset(&hs->hs_roottimer, ieee80211_hwmp_rannint,
821 hwmp_rootmode_rann_cb, vap);
822 ms->ms_flags |= IEEE80211_MESHFLAGS_ROOT;
828 * Send a broadcast Path Request to find all nodes on the mesh. We are
829 * called when the vap is configured as a HWMP root node.
831 #define PREQ_TFLAGS(n) preq.preq_targets[n].target_flags
832 #define PREQ_TADDR(n) preq.preq_targets[n].target_addr
833 #define PREQ_TSEQ(n) preq.preq_targets[n].target_seq
835 hwmp_rootmode_cb(void *arg)
837 struct ieee80211vap *vap = (struct ieee80211vap *)arg;
838 struct ieee80211_hwmp_state *hs = vap->iv_hwmp;
839 struct ieee80211_mesh_state *ms = vap->iv_mesh;
840 struct ieee80211_meshpreq_ie preq;
842 IEEE80211_NOTE(vap, IEEE80211_MSG_HWMP, vap->iv_bss,
843 "%s", "send broadcast PREQ");
846 if (ms->ms_flags & IEEE80211_MESHFLAGS_GATE)
847 preq.preq_flags |= IEEE80211_MESHPREQ_FLAGS_GATE;
848 if (hs->hs_rootmode == IEEE80211_HWMP_ROOTMODE_PROACTIVE)
849 preq.preq_flags |= IEEE80211_MESHPREQ_FLAGS_PP;
850 preq.preq_hopcount = 0;
851 preq.preq_ttl = ms->ms_ttl;
852 preq.preq_id = ++hs->hs_preqid;
853 IEEE80211_ADDR_COPY(preq.preq_origaddr, vap->iv_myaddr);
854 preq.preq_origseq = ++hs->hs_seq;
855 preq.preq_lifetime = ticks_to_msecs(ieee80211_hwmp_roottimeout);
856 preq.preq_metric = IEEE80211_MESHLMETRIC_INITIALVAL;
857 preq.preq_tcount = 1;
858 IEEE80211_ADDR_COPY(PREQ_TADDR(0), broadcastaddr);
859 PREQ_TFLAGS(0) = IEEE80211_MESHPREQ_TFLAGS_TO |
860 IEEE80211_MESHPREQ_TFLAGS_USN;
862 vap->iv_stats.is_hwmp_rootreqs++;
863 /* NB: we enforce rate check ourself */
864 hwmp_send_preq(vap, broadcastaddr, &preq, NULL, NULL);
865 hwmp_rootmode_setup(vap);
872 * Send a Root Annoucement (RANN) to find all the nodes on the mesh. We are
873 * called when the vap is configured as a HWMP RANN root node.
876 hwmp_rootmode_rann_cb(void *arg)
878 struct ieee80211vap *vap = (struct ieee80211vap *)arg;
879 struct ieee80211_hwmp_state *hs = vap->iv_hwmp;
880 struct ieee80211_mesh_state *ms = vap->iv_mesh;
881 struct ieee80211_meshrann_ie rann;
883 IEEE80211_NOTE(vap, IEEE80211_MSG_HWMP, vap->iv_bss,
884 "%s", "send broadcast RANN");
887 if (ms->ms_flags & IEEE80211_MESHFLAGS_GATE)
888 rann.rann_flags |= IEEE80211_MESHFLAGS_GATE;
889 rann.rann_hopcount = 0;
890 rann.rann_ttl = ms->ms_ttl;
891 IEEE80211_ADDR_COPY(rann.rann_addr, vap->iv_myaddr);
892 rann.rann_seq = ++hs->hs_seq;
893 rann.rann_interval = ieee80211_hwmp_rannint;
894 rann.rann_metric = IEEE80211_MESHLMETRIC_INITIALVAL;
896 vap->iv_stats.is_hwmp_rootrann++;
897 hwmp_send_rann(vap, broadcastaddr, &rann);
898 hwmp_rootmode_setup(vap);
902 * Update forwarding information to TA if metric improves.
905 hwmp_update_transmitter(struct ieee80211vap *vap, struct ieee80211_node *ni,
906 const char *hwmp_frame)
908 struct ieee80211_mesh_state *ms = vap->iv_mesh;
909 struct ieee80211_mesh_route *rttran = NULL; /* Transmitter */
912 rttran = ieee80211_mesh_rt_find(vap, ni->ni_macaddr);
913 if (rttran == NULL) {
914 rttran = ieee80211_mesh_rt_add(vap, ni->ni_macaddr);
915 if (rttran == NULL) {
916 IEEE80211_NOTE(vap, IEEE80211_MSG_HWMP, ni,
917 "unable to add path to transmitter %6D of %s",
918 ni->ni_macaddr, ":", hwmp_frame);
919 vap->iv_stats.is_mesh_rtaddfailed++;
923 metric = ms->ms_pmetric->mpm_metric(ni);
924 if (!(rttran->rt_flags & IEEE80211_MESHRT_FLAGS_VALID) ||
925 rttran->rt_metric > metric)
927 IEEE80211_NOTE(vap, IEEE80211_MSG_HWMP, ni,
928 "%s path to transmitter %6D of %s, metric %d:%d",
929 rttran->rt_flags & IEEE80211_MESHRT_FLAGS_VALID ?
930 "prefer" : "update", ni->ni_macaddr, ":", hwmp_frame,
931 rttran->rt_metric, metric);
932 IEEE80211_ADDR_COPY(rttran->rt_nexthop, ni->ni_macaddr);
933 rttran->rt_metric = metric;
934 rttran->rt_nhops = 1;
935 ieee80211_mesh_rt_update(rttran, ms->ms_ppath->mpp_inact);
936 rttran->rt_flags = IEEE80211_MESHRT_FLAGS_VALID;
940 #define PREQ_TFLAGS(n) preq->preq_targets[n].target_flags
941 #define PREQ_TADDR(n) preq->preq_targets[n].target_addr
942 #define PREQ_TSEQ(n) preq->preq_targets[n].target_seq
944 hwmp_recv_preq(struct ieee80211vap *vap, struct ieee80211_node *ni,
945 const struct ieee80211_frame *wh, const struct ieee80211_meshpreq_ie *preq)
947 struct ieee80211_mesh_state *ms = vap->iv_mesh;
948 struct ieee80211_mesh_route *rtorig = NULL;
949 struct ieee80211_mesh_route *rtorig_ext = NULL;
950 struct ieee80211_mesh_route *rttarg = NULL;
951 struct ieee80211_hwmp_route *hrorig = NULL;
952 struct ieee80211_hwmp_route *hrtarg = NULL;
953 struct ieee80211_hwmp_state *hs = vap->iv_hwmp;
954 ieee80211_hwmp_seq preqid; /* last seen preqid for orig */
958 * Ignore PREQs from us. Could happen because someone forward it
961 if (IEEE80211_ADDR_EQ(vap->iv_myaddr, preq->preq_origaddr))
964 IEEE80211_NOTE(vap, IEEE80211_MSG_HWMP, ni,
965 "received PREQ, orig %6D, targ(0) %6D", preq->preq_origaddr, ":",
969 * Acceptance criteria: (if the PREQ is not for us or not broadcast,
970 * or an external mac address not proxied by us),
971 * AND forwarding is disabled, discard this PREQ.
973 rttarg = ieee80211_mesh_rt_find(vap, PREQ_TADDR(0));
974 if (!(ms->ms_flags & IEEE80211_MESHFLAGS_FWD) &&
975 (!IEEE80211_ADDR_EQ(vap->iv_myaddr, PREQ_TADDR(0)) ||
976 !IEEE80211_IS_MULTICAST(PREQ_TADDR(0)) ||
978 rttarg->rt_flags & IEEE80211_MESHRT_FLAGS_PROXY &&
979 IEEE80211_ADDR_EQ(vap->iv_myaddr, rttarg->rt_mesh_gate)))) {
980 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_HWMP,
981 preq->preq_origaddr, NULL, "%s", "not accepting PREQ");
985 * Acceptance criteria: if unicast addressed
986 * AND no valid forwarding for Target of PREQ, discard this PREQ.
989 hrtarg = IEEE80211_MESH_ROUTE_PRIV(rttarg,
990 struct ieee80211_hwmp_route);
991 /* Address mode: ucast */
992 if(preq->preq_flags & IEEE80211_MESHPREQ_FLAGS_AM &&
994 !IEEE80211_ADDR_EQ(vap->iv_myaddr, PREQ_TADDR(0))) {
995 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_HWMP,
996 preq->preq_origaddr, NULL,
997 "unicast addressed PREQ of unknown target %6D",
1004 rtorig = ieee80211_mesh_rt_find(vap, preq->preq_origaddr);
1005 if (rtorig == NULL) {
1006 rtorig = ieee80211_mesh_rt_add(vap, preq->preq_origaddr);
1007 if (rtorig == NULL) {
1008 IEEE80211_NOTE(vap, IEEE80211_MSG_HWMP, ni,
1009 "unable to add orig path to %6D",
1010 preq->preq_origaddr, ":");
1011 vap->iv_stats.is_mesh_rtaddfailed++;
1014 IEEE80211_NOTE(vap, IEEE80211_MSG_HWMP, ni,
1015 "adding originator %6D", preq->preq_origaddr, ":");
1017 hrorig = IEEE80211_MESH_ROUTE_PRIV(rtorig, struct ieee80211_hwmp_route);
1019 /* record last seen preqid */
1020 preqid = hrorig->hr_preqid;
1021 hrorig->hr_preqid = HWMP_SEQ_MAX(hrorig->hr_preqid, preq->preq_id);
1023 /* Data creation and update of forwarding information
1024 * according to Table 11C-8 for originator mesh STA.
1026 metric = preq->preq_metric + ms->ms_pmetric->mpm_metric(ni);
1027 if (HWMP_SEQ_GT(preq->preq_origseq, hrorig->hr_seq) ||
1028 (HWMP_SEQ_EQ(preq->preq_origseq, hrorig->hr_seq) &&
1029 metric < rtorig->rt_metric)) {
1030 hrorig->hr_seq = preq->preq_origseq;
1031 IEEE80211_ADDR_COPY(rtorig->rt_nexthop, wh->i_addr2);
1032 rtorig->rt_metric = metric;
1033 rtorig->rt_nhops = preq->preq_hopcount + 1;
1034 ieee80211_mesh_rt_update(rtorig, preq->preq_lifetime);
1035 /* Path to orig is valid now.
1036 * NB: we know it can't be Proxy, and if it is GATE
1037 * it will be marked below.
1039 rtorig->rt_flags = IEEE80211_MESHRT_FLAGS_VALID;
1040 } else if ((hrtarg != NULL &&
1041 !HWMP_SEQ_EQ(hrtarg->hr_seq, PREQ_TSEQ(0))) ||
1042 (rtorig->rt_flags & IEEE80211_MESHRT_FLAGS_VALID &&
1043 preqid >= preq->preq_id)) {
1044 IEEE80211_NOTE(vap, IEEE80211_MSG_HWMP, ni,
1045 "discard PREQ from %6D, old seqno %u <= %u,"
1046 " or old preqid %u < %u",
1047 preq->preq_origaddr, ":",
1048 preq->preq_origseq, hrorig->hr_seq,
1049 preq->preq_id, preqid);
1053 /* Update forwarding information to TA if metric improves. */
1054 hwmp_update_transmitter(vap, ni, "PREQ");
1057 * Check if the PREQ is addressed to us.
1058 * or a Proxy currently gated by us.
1060 if (IEEE80211_ADDR_EQ(vap->iv_myaddr, PREQ_TADDR(0)) ||
1061 (ms->ms_flags & IEEE80211_MESHFLAGS_GATE &&
1063 IEEE80211_ADDR_EQ(vap->iv_myaddr, rttarg->rt_mesh_gate) &&
1064 rttarg->rt_flags & IEEE80211_MESHRT_FLAGS_PROXY &&
1065 rttarg->rt_flags & IEEE80211_MESHRT_FLAGS_VALID)) {
1066 struct ieee80211_meshprep_ie prep;
1069 * When we are the target we shall update our own HWMP seq
1070 * number with max of (current and preq->seq) + 1
1072 hs->hs_seq = HWMP_SEQ_MAX(hs->hs_seq, PREQ_TSEQ(0)) + 1;
1074 prep.prep_flags = 0;
1075 prep.prep_hopcount = 0;
1076 prep.prep_metric = IEEE80211_MESHLMETRIC_INITIALVAL;
1077 IEEE80211_ADDR_COPY(prep.prep_targetaddr, vap->iv_myaddr);
1078 if (rttarg != NULL && /* if NULL it means we are the target */
1079 rttarg->rt_flags & IEEE80211_MESHRT_FLAGS_PROXY) {
1080 IEEE80211_NOTE(vap, IEEE80211_MSG_HWMP, ni,
1081 "reply for proxy %6D", rttarg->rt_dest, ":");
1082 prep.prep_flags |= IEEE80211_MESHPREP_FLAGS_AE;
1083 IEEE80211_ADDR_COPY(prep.prep_target_ext_addr,
1085 /* update proxy seqno to HWMP seqno */
1086 rttarg->rt_ext_seq = hs->hs_seq;
1087 prep.prep_hopcount = rttarg->rt_nhops;
1088 prep.prep_metric = rttarg->rt_metric;
1089 IEEE80211_ADDR_COPY(prep.prep_targetaddr, rttarg->rt_mesh_gate);
1092 * Build and send a PREP frame.
1094 prep.prep_ttl = ms->ms_ttl;
1095 prep.prep_targetseq = hs->hs_seq;
1096 prep.prep_lifetime = preq->preq_lifetime;
1097 IEEE80211_ADDR_COPY(prep.prep_origaddr, preq->preq_origaddr);
1098 prep.prep_origseq = preq->preq_origseq;
1100 IEEE80211_NOTE(vap, IEEE80211_MSG_HWMP, ni,
1101 "reply to %6D", preq->preq_origaddr, ":");
1102 hwmp_send_prep(vap, wh->i_addr2, &prep);
1105 /* we may update our proxy information for the orig external */
1106 else if (preq->preq_flags & IEEE80211_MESHPREQ_FLAGS_AE) {
1108 ieee80211_mesh_rt_find(vap, preq->preq_orig_ext_addr);
1109 if (rtorig_ext == NULL) {
1110 rtorig_ext = ieee80211_mesh_rt_add(vap,
1111 preq->preq_orig_ext_addr);
1112 if (rtorig_ext == NULL) {
1113 IEEE80211_NOTE(vap, IEEE80211_MSG_HWMP, ni,
1114 "unable to add orig ext proxy to %6D",
1115 preq->preq_orig_ext_addr, ":");
1116 vap->iv_stats.is_mesh_rtaddfailed++;
1119 IEEE80211_ADDR_COPY(rtorig_ext->rt_mesh_gate,
1120 preq->preq_origaddr);
1122 rtorig_ext->rt_ext_seq = preq->preq_origseq;
1123 ieee80211_mesh_rt_update(rtorig_ext, preq->preq_lifetime);
1126 * Proactive PREQ: reply with a proactive PREP to the
1127 * root STA if requested.
1129 if (IEEE80211_ADDR_EQ(PREQ_TADDR(0), broadcastaddr) &&
1130 (PREQ_TFLAGS(0) & IEEE80211_MESHPREQ_TFLAGS_TO)) {
1131 IEEE80211_NOTE(vap, IEEE80211_MSG_HWMP, ni,
1132 "root mesh station @ %6D", preq->preq_origaddr, ":");
1134 /* Check if root is a mesh gate, mark it */
1135 if (preq->preq_flags & IEEE80211_MESHPREQ_FLAGS_GATE) {
1136 struct ieee80211_mesh_gate_route *gr;
1138 rtorig->rt_flags |= IEEE80211_MESHRT_FLAGS_GATE;
1139 gr = ieee80211_mesh_mark_gate(vap, preq->preq_origaddr,
1141 gr->gr_lastseq = 0; /* NOT GANN */
1145 * Reply with a PREP if we don't have a path to the root
1146 * or if the root sent us a proactive PREQ.
1148 if ((rtorig->rt_flags & IEEE80211_MESHRT_FLAGS_VALID) == 0 ||
1149 (preq->preq_flags & IEEE80211_MESHPREQ_FLAGS_PP)) {
1150 struct ieee80211_meshprep_ie prep;
1152 prep.prep_flags = 0;
1153 prep.prep_hopcount = 0;
1154 prep.prep_ttl = ms->ms_ttl;
1155 IEEE80211_ADDR_COPY(prep.prep_origaddr,
1156 preq->preq_origaddr);
1157 prep.prep_origseq = preq->preq_origseq;
1158 prep.prep_lifetime = preq->preq_lifetime;
1159 prep.prep_metric = IEEE80211_MESHLMETRIC_INITIALVAL;
1160 IEEE80211_ADDR_COPY(prep.prep_targetaddr,
1162 prep.prep_targetseq = ++hs->hs_seq;
1163 hwmp_send_prep(vap, rtorig->rt_nexthop, &prep);
1168 * Forwarding and Intermediate reply for PREQs with 1 target.
1170 if ((preq->preq_tcount == 1) && (preq->preq_ttl > 1) &&
1171 (ms->ms_flags & IEEE80211_MESHFLAGS_FWD)) {
1172 struct ieee80211_meshpreq_ie ppreq; /* propagated PREQ */
1174 memcpy(&ppreq, preq, sizeof(ppreq));
1177 * We have a valid route to this node.
1178 * NB: if target is proxy dont reply.
1180 if (rttarg != NULL &&
1181 rttarg->rt_flags & IEEE80211_MESHRT_FLAGS_VALID &&
1182 !(rttarg->rt_flags & IEEE80211_MESHRT_FLAGS_PROXY)) {
1184 * Check if we can send an intermediate Path Reply,
1185 * i.e., Target Only bit is not set and target is not
1186 * the MAC broadcast address.
1188 if (!(PREQ_TFLAGS(0) & IEEE80211_MESHPREQ_TFLAGS_TO) &&
1189 !IEEE80211_ADDR_EQ(PREQ_TADDR(0), broadcastaddr)) {
1190 struct ieee80211_meshprep_ie prep;
1192 IEEE80211_NOTE(vap, IEEE80211_MSG_HWMP, ni,
1193 "intermediate reply for PREQ from %6D",
1194 preq->preq_origaddr, ":");
1195 prep.prep_flags = 0;
1196 prep.prep_hopcount = rttarg->rt_nhops;
1197 prep.prep_ttl = ms->ms_ttl;
1198 IEEE80211_ADDR_COPY(&prep.prep_targetaddr,
1200 prep.prep_targetseq = hrtarg->hr_seq;
1201 prep.prep_lifetime = preq->preq_lifetime;
1202 prep.prep_metric =rttarg->rt_metric;
1203 IEEE80211_ADDR_COPY(&prep.prep_origaddr,
1204 preq->preq_origaddr);
1205 prep.prep_origseq = hrorig->hr_seq;
1206 hwmp_send_prep(vap, rtorig->rt_nexthop, &prep);
1209 * Set TO and unset RF bits because we have
1212 ppreq.preq_targets[0].target_flags |=
1213 IEEE80211_MESHPREQ_TFLAGS_TO;
1217 IEEE80211_NOTE(vap, IEEE80211_MSG_HWMP, ni,
1218 "forward PREQ from %6D",
1219 preq->preq_origaddr, ":");
1220 ppreq.preq_hopcount += 1;
1221 ppreq.preq_ttl -= 1;
1222 ppreq.preq_metric += ms->ms_pmetric->mpm_metric(ni);
1224 /* don't do PREQ ratecheck when we propagate */
1225 hwmp_send_preq(vap, broadcastaddr, &ppreq, NULL, NULL);
1233 hwmp_send_preq(struct ieee80211vap *vap,
1234 const uint8_t da[IEEE80211_ADDR_LEN],
1235 struct ieee80211_meshpreq_ie *preq,
1236 struct timeval *last, struct timeval *minint)
1240 * Enforce PREQ interval.
1241 * NB: Proactive ROOT PREQs rate is handled by cb task.
1243 if (last != NULL && minint != NULL) {
1244 if (ratecheck(last, minint) == 0)
1245 return EALREADY; /* XXX: we should postpone */
1246 getmicrouptime(last);
1250 * mesh preq action frame format
1256 * [tlv] mesh path request
1258 preq->preq_ie = IEEE80211_ELEMID_MESHPREQ;
1259 preq->preq_len = (preq->preq_flags & IEEE80211_MESHPREQ_FLAGS_AE ?
1260 IEEE80211_MESHPREQ_BASE_SZ_AE : IEEE80211_MESHPREQ_BASE_SZ) +
1261 preq->preq_tcount * IEEE80211_MESHPREQ_TRGT_SZ;
1262 return hwmp_send_action(vap, da, (uint8_t *)preq, preq->preq_len+2);
1266 hwmp_recv_prep(struct ieee80211vap *vap, struct ieee80211_node *ni,
1267 const struct ieee80211_frame *wh, const struct ieee80211_meshprep_ie *prep)
1269 #define IS_PROXY(rt) (rt->rt_flags & IEEE80211_MESHRT_FLAGS_PROXY)
1270 #define PROXIED_BY_US(rt) \
1271 (IEEE80211_ADDR_EQ(vap->iv_myaddr, rt->rt_mesh_gate))
1272 struct ieee80211_mesh_state *ms = vap->iv_mesh;
1273 struct ieee80211_hwmp_state *hs = vap->iv_hwmp;
1274 struct ieee80211_mesh_route *rt = NULL;
1275 struct ieee80211_mesh_route *rtorig = NULL;
1276 struct ieee80211_mesh_route *rtext = NULL;
1277 struct ieee80211_hwmp_route *hr;
1278 struct ieee80211com *ic = vap->iv_ic;
1279 struct mbuf *m, *next;
1280 uint32_t metric = 0;
1281 const uint8_t *addr;
1283 IEEE80211_NOTE(vap, IEEE80211_MSG_HWMP, ni,
1284 "received PREP, orig %6D, targ %6D", prep->prep_origaddr, ":",
1285 prep->prep_targetaddr, ":");
1288 * Acceptance criteria: (If the corresponding PREP was not generated
1289 * by us OR not generated by an external mac that is not proxied by us)
1290 * AND forwarding is disabled, discard this PREP.
1292 rtorig = ieee80211_mesh_rt_find(vap, prep->prep_origaddr);
1293 if ((!IEEE80211_ADDR_EQ(vap->iv_myaddr, prep->prep_origaddr) ||
1294 (rtorig != NULL && IS_PROXY(rtorig) && !PROXIED_BY_US(rtorig))) &&
1295 !(ms->ms_flags & IEEE80211_MESHFLAGS_FWD)){
1296 IEEE80211_NOTE(vap, IEEE80211_MSG_HWMP, ni,
1297 "discard PREP, orig(%6D) not proxied or generated by us",
1298 prep->prep_origaddr, ":");
1305 * If accepted shall create or update the active forwarding information
1306 * it maintains for the target mesh STA of the PREP (according to the
1307 * rules defined in 13.10.8.4). If the conditions for creating or
1308 * updating the forwarding information have not been met in those
1309 * rules, no further steps are applied to the PREP.
1311 rt = ieee80211_mesh_rt_find(vap, prep->prep_targetaddr);
1313 rt = ieee80211_mesh_rt_add(vap, prep->prep_targetaddr);
1315 IEEE80211_NOTE(vap, IEEE80211_MSG_HWMP, ni,
1316 "unable to add PREP path to %6D",
1317 prep->prep_targetaddr, ":");
1318 vap->iv_stats.is_mesh_rtaddfailed++;
1321 IEEE80211_NOTE(vap, IEEE80211_MSG_HWMP, ni,
1322 "adding target %6D", prep->prep_targetaddr, ":");
1324 hr = IEEE80211_MESH_ROUTE_PRIV(rt, struct ieee80211_hwmp_route);
1325 /* update path metric */
1326 metric = prep->prep_metric + ms->ms_pmetric->mpm_metric(ni);
1327 if ((rt->rt_flags & IEEE80211_MESHRT_FLAGS_VALID)) {
1328 if (HWMP_SEQ_LT(prep->prep_targetseq, hr->hr_seq)) {
1329 IEEE80211_NOTE(vap, IEEE80211_MSG_HWMP, ni,
1330 "discard PREP from %6D, old seq no %u < %u",
1331 prep->prep_targetaddr, ":",
1332 prep->prep_targetseq, hr->hr_seq);
1334 } else if (HWMP_SEQ_LEQ(prep->prep_targetseq, hr->hr_seq) &&
1335 metric > rt->rt_metric) {
1336 IEEE80211_NOTE(vap, IEEE80211_MSG_HWMP, ni,
1337 "discard PREP from %6D, new metric %u > %u",
1338 prep->prep_targetaddr, ":",
1339 metric, rt->rt_metric);
1344 IEEE80211_NOTE(vap, IEEE80211_MSG_HWMP, ni,
1345 "%s path to %6D, hopcount %d:%d metric %d:%d",
1346 rt->rt_flags & IEEE80211_MESHRT_FLAGS_VALID ?
1347 "prefer" : "update",
1348 prep->prep_targetaddr, ":",
1349 rt->rt_nhops, prep->prep_hopcount + 1,
1350 rt->rt_metric, metric);
1352 hr->hr_seq = prep->prep_targetseq;
1353 hr->hr_preqretries = 0;
1354 IEEE80211_ADDR_COPY(rt->rt_nexthop, ni->ni_macaddr);
1355 rt->rt_metric = metric;
1356 rt->rt_nhops = prep->prep_hopcount + 1;
1357 ieee80211_mesh_rt_update(rt, prep->prep_lifetime);
1358 if (rt->rt_flags & IEEE80211_MESHRT_FLAGS_DISCOVER) {
1359 /* discovery complete */
1360 rt->rt_flags &= ~IEEE80211_MESHRT_FLAGS_DISCOVER;
1362 rt->rt_flags |= IEEE80211_MESHRT_FLAGS_VALID; /* mark valid */
1364 /* Update forwarding information to TA if metric improves */
1365 hwmp_update_transmitter(vap, ni, "PREP");
1368 * If it's NOT for us, propagate the PREP
1370 if (!IEEE80211_ADDR_EQ(vap->iv_myaddr, prep->prep_origaddr) &&
1371 prep->prep_ttl > 1 &&
1372 prep->prep_hopcount < hs->hs_maxhops) {
1373 struct ieee80211_meshprep_ie pprep; /* propagated PREP */
1375 * NB: We should already have setup the path to orig
1376 * mesh STA when we propagated PREQ to target mesh STA,
1377 * no PREP is generated without a corresponding PREQ.
1378 * XXX: for now just ignore.
1380 if (rtorig == NULL) {
1381 IEEE80211_NOTE(vap, IEEE80211_MSG_HWMP, ni,
1382 "received PREP for an unknown orig(%6D)",
1383 prep->prep_origaddr, ":");
1387 IEEE80211_NOTE(vap, IEEE80211_MSG_HWMP, ni,
1388 "propagate PREP from %6D",
1389 prep->prep_targetaddr, ":");
1391 memcpy(&pprep, prep, sizeof(pprep));
1392 pprep.prep_hopcount += 1;
1393 pprep.prep_ttl -= 1;
1394 pprep.prep_metric += ms->ms_pmetric->mpm_metric(ni);
1395 hwmp_send_prep(vap, rtorig->rt_nexthop, &pprep);
1397 /* precursor list for the Target Mesh STA Address is updated */
1401 * Check if we received a PREP w/ AE and store target external address.
1402 * We may store target external address if recevied PREP w/ AE
1403 * and we are not final destination
1405 if (prep->prep_flags & IEEE80211_MESHPREP_FLAGS_AE) {
1406 rtext = ieee80211_mesh_rt_find(vap,
1407 prep->prep_target_ext_addr);
1408 if (rtext == NULL) {
1409 rtext = ieee80211_mesh_rt_add(vap,
1410 prep->prep_target_ext_addr);
1411 if (rtext == NULL) {
1412 IEEE80211_NOTE(vap, IEEE80211_MSG_HWMP, ni,
1413 "unable to add PREP path to proxy %6D",
1414 prep->prep_targetaddr, ":");
1415 vap->iv_stats.is_mesh_rtaddfailed++;
1419 IEEE80211_NOTE(vap, IEEE80211_MSG_HWMP, ni,
1420 "%s path to %6D, hopcount %d:%d metric %d:%d",
1421 rtext->rt_flags & IEEE80211_MESHRT_FLAGS_VALID ?
1422 "prefer" : "update",
1423 prep->prep_target_ext_addr, ":",
1424 rtext->rt_nhops, prep->prep_hopcount + 1,
1425 rtext->rt_metric, metric);
1427 rtext->rt_flags = IEEE80211_MESHRT_FLAGS_PROXY |
1428 IEEE80211_MESHRT_FLAGS_VALID;
1429 IEEE80211_ADDR_COPY(rtext->rt_dest,
1430 prep->prep_target_ext_addr);
1431 IEEE80211_ADDR_COPY(rtext->rt_mesh_gate,
1432 prep->prep_targetaddr);
1433 IEEE80211_ADDR_COPY(rtext->rt_nexthop, wh->i_addr2);
1434 rtext->rt_metric = metric;
1435 rtext->rt_lifetime = prep->prep_lifetime;
1436 rtext->rt_nhops = prep->prep_hopcount + 1;
1437 rtext->rt_ext_seq = prep->prep_origseq; /* new proxy seq */
1439 * XXX: proxy entries have no HWMP priv data,
1440 * nullify them to be sure?
1444 * Check for frames queued awaiting path discovery.
1445 * XXX probably can tell exactly and avoid remove call
1446 * NB: hash may have false matches, if so they will get
1447 * stuck back on the stageq because there won't be
1450 addr = prep->prep_flags & IEEE80211_MESHPREP_FLAGS_AE ?
1451 prep->prep_target_ext_addr : prep->prep_targetaddr;
1452 m = ieee80211_ageq_remove(&ic->ic_stageq,
1453 (struct ieee80211_node *)(uintptr_t)
1454 ieee80211_mac_hash(ic, addr)); /* either dest or ext_dest */
1457 * All frames in the stageq here should be non-M_ENCAP; or things
1458 * will get very unhappy.
1460 for (; m != NULL; m = next) {
1461 next = m->m_nextpkt;
1462 m->m_nextpkt = NULL;
1463 IEEE80211_NOTE(vap, IEEE80211_MSG_HWMP, ni,
1464 "flush queued frame %p len %d", m, m->m_pkthdr.len);
1466 * If the mbuf has M_ENCAP set, ensure we free it.
1467 * Note that after if_transmit() is called, m is invalid.
1469 (void) ieee80211_vap_xmitpkt(vap, m);
1472 #undef PROXIED_BY_US
1476 hwmp_send_prep(struct ieee80211vap *vap,
1477 const uint8_t da[IEEE80211_ADDR_LEN],
1478 struct ieee80211_meshprep_ie *prep)
1480 /* NB: there's no PREP minimum interval. */
1483 * mesh prep action frame format
1489 * [tlv] mesh path reply
1491 prep->prep_ie = IEEE80211_ELEMID_MESHPREP;
1492 prep->prep_len = prep->prep_flags & IEEE80211_MESHPREP_FLAGS_AE ?
1493 IEEE80211_MESHPREP_BASE_SZ_AE : IEEE80211_MESHPREP_BASE_SZ;
1494 return hwmp_send_action(vap, da, (uint8_t *)prep, prep->prep_len + 2);
1497 #define PERR_DFLAGS(n) perr.perr_dests[n].dest_flags
1498 #define PERR_DADDR(n) perr.perr_dests[n].dest_addr
1499 #define PERR_DSEQ(n) perr.perr_dests[n].dest_seq
1500 #define PERR_DRCODE(n) perr.perr_dests[n].dest_rcode
1502 hwmp_peerdown(struct ieee80211_node *ni)
1504 struct ieee80211vap *vap = ni->ni_vap;
1505 struct ieee80211_mesh_state *ms = vap->iv_mesh;
1506 struct ieee80211_meshperr_ie perr;
1507 struct ieee80211_mesh_route *rt;
1508 struct ieee80211_hwmp_route *hr;
1510 rt = ieee80211_mesh_rt_find(vap, ni->ni_macaddr);
1513 hr = IEEE80211_MESH_ROUTE_PRIV(rt, struct ieee80211_hwmp_route);
1514 IEEE80211_NOTE(vap, IEEE80211_MSG_HWMP, ni,
1515 "%s", "delete route entry");
1516 perr.perr_ttl = ms->ms_ttl;
1517 perr.perr_ndests = 1;
1519 if (hr->hr_seq == 0)
1520 PERR_DFLAGS(0) |= IEEE80211_MESHPERR_DFLAGS_USN;
1521 PERR_DFLAGS(0) |= IEEE80211_MESHPERR_DFLAGS_RC;
1522 IEEE80211_ADDR_COPY(PERR_DADDR(0), rt->rt_dest);
1523 PERR_DSEQ(0) = ++hr->hr_seq;
1524 PERR_DRCODE(0) = IEEE80211_REASON_MESH_PERR_DEST_UNREACH;
1525 /* NB: flush everything passing through peer */
1526 ieee80211_mesh_rt_flush_peer(vap, ni->ni_macaddr);
1527 hwmp_send_perr(vap, broadcastaddr, &perr);
1534 #define PERR_DFLAGS(n) perr->perr_dests[n].dest_flags
1535 #define PERR_DADDR(n) perr->perr_dests[n].dest_addr
1536 #define PERR_DSEQ(n) perr->perr_dests[n].dest_seq
1537 #define PERR_DEXTADDR(n) perr->perr_dests[n].dest_ext_addr
1539 hwmp_recv_perr(struct ieee80211vap *vap, struct ieee80211_node *ni,
1540 const struct ieee80211_frame *wh, const struct ieee80211_meshperr_ie *perr)
1542 struct ieee80211_mesh_state *ms = vap->iv_mesh;
1543 struct ieee80211_mesh_route *rt = NULL;
1544 struct ieee80211_mesh_route *rt_ext = NULL;
1545 struct ieee80211_hwmp_route *hr;
1546 struct ieee80211_meshperr_ie *pperr = NULL;
1547 int i, j = 0, forward = 0;
1549 IEEE80211_NOTE(vap, IEEE80211_MSG_HWMP, ni,
1550 "received PERR from %6D", wh->i_addr2, ":");
1553 * if forwarding is true, prepare pperr
1555 if (ms->ms_flags & IEEE80211_MESHFLAGS_FWD) {
1557 pperr = IEEE80211_MALLOC(sizeof(*perr) + 31*sizeof(*perr->perr_dests),
1558 M_80211_MESH_PERR, IEEE80211_M_NOWAIT); /* XXX: magic number, 32 err dests */
1562 * Acceptance criteria: check if we have forwarding information
1563 * stored about destination, and that nexthop == TA of this PERR.
1564 * NB: we also build a new PERR to propagate in case we should forward.
1566 for (i = 0; i < perr->perr_ndests; i++) {
1567 rt = ieee80211_mesh_rt_find(vap, PERR_DADDR(i));
1570 if (!IEEE80211_ADDR_EQ(rt->rt_nexthop, wh->i_addr2))
1573 /* found and accepted a PERR ndest element, process it... */
1575 memcpy(&pperr->perr_dests[j], &perr->perr_dests[i],
1576 sizeof(*perr->perr_dests));
1577 hr = IEEE80211_MESH_ROUTE_PRIV(rt, struct ieee80211_hwmp_route);
1578 switch(PERR_DFLAGS(i)) {
1579 case (IEEE80211_REASON_MESH_PERR_NO_FI):
1580 if (PERR_DSEQ(i) == 0) {
1583 pperr->perr_dests[j].dest_seq =
1587 hr->hr_seq = PERR_DSEQ(i);
1589 rt->rt_flags &= ~IEEE80211_MESHRT_FLAGS_VALID;
1592 case (IEEE80211_REASON_MESH_PERR_DEST_UNREACH):
1593 if(HWMP_SEQ_GT(PERR_DSEQ(i), hr->hr_seq)) {
1594 hr->hr_seq = PERR_DSEQ(i);
1595 rt->rt_flags &= ~IEEE80211_MESHRT_FLAGS_VALID;
1599 case (IEEE80211_REASON_MESH_PERR_NO_PROXY):
1600 rt_ext = ieee80211_mesh_rt_find(vap, PERR_DEXTADDR(i));
1601 if (rt_ext != NULL) {
1603 ~IEEE80211_MESHRT_FLAGS_VALID;
1608 IEEE80211_DISCARD(vap, IEEE80211_MSG_HWMP, wh, NULL,
1609 "PERR, unknown reason code %u\n", PERR_DFLAGS(i));
1610 goto done; /* XXX: stats?? */
1612 ieee80211_mesh_rt_flush_peer(vap, PERR_DADDR(i));
1613 KASSERT(j < 32, ("PERR, error ndest >= 32 (%u)", j));
1616 IEEE80211_DISCARD(vap, IEEE80211_MSG_HWMP, wh, NULL, "%s",
1617 "PERR not accepted");
1618 goto done; /* XXX: stats?? */
1622 * Propagate the PERR if we previously found it on our routing table.
1624 if (forward && perr->perr_ttl > 1) {
1625 IEEE80211_NOTE(vap, IEEE80211_MSG_HWMP, ni,
1626 "propagate PERR from %6D", wh->i_addr2, ":");
1627 pperr->perr_ndests = j;
1629 hwmp_send_perr(vap, broadcastaddr, pperr);
1633 IEEE80211_FREE(pperr, M_80211_MESH_PERR);
1638 #undef PERR_DEXTADDR
1641 hwmp_send_perr(struct ieee80211vap *vap,
1642 const uint8_t da[IEEE80211_ADDR_LEN],
1643 struct ieee80211_meshperr_ie *perr)
1645 struct ieee80211_hwmp_state *hs = vap->iv_hwmp;
1650 * Enforce PERR interval.
1652 if (ratecheck(&hs->hs_lastperr, &ieee80211_hwmp_perrminint) == 0)
1654 getmicrouptime(&hs->hs_lastperr);
1657 * mesh perr action frame format
1663 * [tlv] mesh path error
1665 perr->perr_ie = IEEE80211_ELEMID_MESHPERR;
1666 length = IEEE80211_MESHPERR_BASE_SZ;
1667 for (i = 0; i<perr->perr_ndests; i++) {
1668 if (perr->perr_dests[i].dest_flags &
1669 IEEE80211_MESHPERR_FLAGS_AE) {
1670 length += IEEE80211_MESHPERR_DEST_SZ_AE;
1673 length += IEEE80211_MESHPERR_DEST_SZ;
1675 perr->perr_len =length;
1676 return hwmp_send_action(vap, da, (uint8_t *)perr, perr->perr_len+2);
1680 * Called from the rest of the net80211 code (mesh code for example).
1681 * NB: IEEE80211_REASON_MESH_PERR_DEST_UNREACH can be trigger by the fact that
1682 * a mesh STA is unable to forward an MSDU/MMPDU to a next-hop mesh STA.
1684 #define PERR_DFLAGS(n) perr.perr_dests[n].dest_flags
1685 #define PERR_DADDR(n) perr.perr_dests[n].dest_addr
1686 #define PERR_DSEQ(n) perr.perr_dests[n].dest_seq
1687 #define PERR_DEXTADDR(n) perr.perr_dests[n].dest_ext_addr
1688 #define PERR_DRCODE(n) perr.perr_dests[n].dest_rcode
1690 hwmp_senderror(struct ieee80211vap *vap,
1691 const uint8_t addr[IEEE80211_ADDR_LEN],
1692 struct ieee80211_mesh_route *rt, int rcode)
1694 struct ieee80211_mesh_state *ms = vap->iv_mesh;
1695 struct ieee80211_hwmp_route *hr = NULL;
1696 struct ieee80211_meshperr_ie perr;
1699 hr = IEEE80211_MESH_ROUTE_PRIV(rt,
1700 struct ieee80211_hwmp_route);
1702 perr.perr_ndests = 1;
1703 perr.perr_ttl = ms->ms_ttl;
1705 PERR_DRCODE(0) = rcode;
1708 case IEEE80211_REASON_MESH_PERR_NO_FI:
1709 IEEE80211_ADDR_COPY(PERR_DADDR(0), addr);
1710 PERR_DSEQ(0) = 0; /* reserved */
1712 case IEEE80211_REASON_MESH_PERR_NO_PROXY:
1713 KASSERT(rt != NULL, ("no proxy info for sending PERR"));
1714 KASSERT(rt->rt_flags & IEEE80211_MESHRT_FLAGS_PROXY,
1715 ("route is not marked proxy"));
1716 PERR_DFLAGS(0) |= IEEE80211_MESHPERR_FLAGS_AE;
1717 IEEE80211_ADDR_COPY(PERR_DADDR(0), vap->iv_myaddr);
1718 PERR_DSEQ(0) = rt->rt_ext_seq;
1719 IEEE80211_ADDR_COPY(PERR_DEXTADDR(0), addr);
1721 case IEEE80211_REASON_MESH_PERR_DEST_UNREACH:
1722 KASSERT(rt != NULL, ("no route info for sending PERR"));
1723 IEEE80211_ADDR_COPY(PERR_DADDR(0), addr);
1724 PERR_DSEQ(0) = hr->hr_seq;
1727 KASSERT(0, ("unknown reason code for HWMP PERR (%u)", rcode));
1729 hwmp_send_perr(vap, broadcastaddr, &perr);
1734 #undef PERR_DEXTADDR
1738 hwmp_recv_rann(struct ieee80211vap *vap, struct ieee80211_node *ni,
1739 const struct ieee80211_frame *wh, const struct ieee80211_meshrann_ie *rann)
1741 struct ieee80211_mesh_state *ms = vap->iv_mesh;
1742 struct ieee80211_hwmp_state *hs = vap->iv_hwmp;
1743 struct ieee80211_mesh_route *rt = NULL;
1744 struct ieee80211_hwmp_route *hr;
1745 struct ieee80211_meshpreq_ie preq;
1746 struct ieee80211_meshrann_ie prann;
1748 if (IEEE80211_ADDR_EQ(rann->rann_addr, vap->iv_myaddr))
1751 rt = ieee80211_mesh_rt_find(vap, rann->rann_addr);
1752 if (rt != NULL && rt->rt_flags & IEEE80211_MESHRT_FLAGS_VALID) {
1753 hr = IEEE80211_MESH_ROUTE_PRIV(rt, struct ieee80211_hwmp_route);
1755 /* Acceptance criteria: if RANN.seq < stored seq, discard RANN */
1756 if (HWMP_SEQ_LT(rann->rann_seq, hr->hr_seq)) {
1757 IEEE80211_DISCARD(vap, IEEE80211_MSG_HWMP, wh, NULL,
1758 "RANN seq %u < %u", rann->rann_seq, hr->hr_seq);
1762 /* Acceptance criteria: if RANN.seq == stored seq AND
1763 * RANN.metric > stored metric, discard RANN */
1764 if (HWMP_SEQ_EQ(rann->rann_seq, hr->hr_seq) &&
1765 rann->rann_metric > rt->rt_metric) {
1766 IEEE80211_DISCARD(vap, IEEE80211_MSG_HWMP, wh, NULL,
1767 "RANN metric %u > %u", rann->rann_metric, rt->rt_metric);
1774 ieee80211_hwmp_rannint = rann->rann_interval; /* XXX: mtx lock? */
1777 rt = ieee80211_mesh_rt_add(vap, rann->rann_addr);
1779 IEEE80211_DISCARD(vap, IEEE80211_MSG_HWMP, wh, NULL,
1780 "unable to add mac for RANN root %6D",
1781 rann->rann_addr, ":");
1782 vap->iv_stats.is_mesh_rtaddfailed++;
1786 hr = IEEE80211_MESH_ROUTE_PRIV(rt, struct ieee80211_hwmp_route);
1787 /* Check if root is a mesh gate, mark it */
1788 if (rann->rann_flags & IEEE80211_MESHRANN_FLAGS_GATE) {
1789 struct ieee80211_mesh_gate_route *gr;
1791 rt->rt_flags |= IEEE80211_MESHRT_FLAGS_GATE;
1792 gr = ieee80211_mesh_mark_gate(vap, rann->rann_addr,
1794 gr->gr_lastseq = 0; /* NOT GANN */
1796 /* discovery timeout */
1797 ieee80211_mesh_rt_update(rt,
1798 ticks_to_msecs(ieee80211_hwmp_roottimeout));
1800 preq.preq_flags = IEEE80211_MESHPREQ_FLAGS_AM;
1801 preq.preq_hopcount = 0;
1802 preq.preq_ttl = ms->ms_ttl;
1803 preq.preq_id = 0; /* reserved */
1804 IEEE80211_ADDR_COPY(preq.preq_origaddr, vap->iv_myaddr);
1805 preq.preq_origseq = ++hs->hs_seq;
1806 preq.preq_lifetime = ieee80211_hwmp_roottimeout;
1807 preq.preq_metric = IEEE80211_MESHLMETRIC_INITIALVAL;
1808 preq.preq_tcount = 1;
1809 preq.preq_targets[0].target_flags = IEEE80211_MESHPREQ_TFLAGS_TO;
1810 /* NB: IEEE80211_MESHPREQ_TFLAGS_USN = 0 implicitly implied */
1811 IEEE80211_ADDR_COPY(preq.preq_targets[0].target_addr, rann->rann_addr);
1812 preq.preq_targets[0].target_seq = rann->rann_seq;
1813 /* XXX: if rootconfint have not passed, we built this preq in vain */
1814 hwmp_send_preq(vap, wh->i_addr2, &preq, &hr->hr_lastrootconf,
1815 &ieee80211_hwmp_rootconfint);
1817 /* propagate a RANN */
1818 if (rt->rt_flags & IEEE80211_MESHRT_FLAGS_VALID &&
1819 rann->rann_ttl > 1 &&
1820 ms->ms_flags & IEEE80211_MESHFLAGS_FWD) {
1821 hr->hr_seq = rann->rann_seq;
1822 memcpy(&prann, rann, sizeof(prann));
1823 prann.rann_hopcount += 1;
1824 prann.rann_ttl -= 1;
1825 prann.rann_metric += ms->ms_pmetric->mpm_metric(ni);
1826 hwmp_send_rann(vap, broadcastaddr, &prann);
1831 hwmp_send_rann(struct ieee80211vap *vap,
1832 const uint8_t da[IEEE80211_ADDR_LEN],
1833 struct ieee80211_meshrann_ie *rann)
1836 * mesh rann action frame format
1842 * [tlv] root announcement
1844 rann->rann_ie = IEEE80211_ELEMID_MESHRANN;
1845 rann->rann_len = IEEE80211_MESHRANN_BASE_SZ;
1846 return hwmp_send_action(vap, da, (uint8_t *)rann, rann->rann_len + 2);
1849 #define PREQ_TFLAGS(n) preq.preq_targets[n].target_flags
1850 #define PREQ_TADDR(n) preq.preq_targets[n].target_addr
1851 #define PREQ_TSEQ(n) preq.preq_targets[n].target_seq
1853 hwmp_rediscover_cb(void *arg)
1855 struct ieee80211_mesh_route *rt = arg;
1856 struct ieee80211vap *vap = rt->rt_vap;
1857 struct ieee80211_hwmp_state *hs = vap->iv_hwmp;
1858 struct ieee80211_mesh_state *ms = vap->iv_mesh;
1859 struct ieee80211_hwmp_route *hr;
1860 struct ieee80211_meshpreq_ie preq; /* Optimize: storing first preq? */
1862 if ((rt->rt_flags & IEEE80211_MESHRT_FLAGS_VALID))
1863 return ; /* nothing to do */
1865 hr = IEEE80211_MESH_ROUTE_PRIV(rt, struct ieee80211_hwmp_route);
1866 if (hr->hr_preqretries >=
1867 ieee80211_hwmp_maxpreq_retries) {
1868 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_ANY,
1870 "max number of discovery, send queued frames to GATE");
1871 ieee80211_mesh_forward_to_gates(vap, rt);
1872 vap->iv_stats.is_mesh_fwd_nopath++;
1873 return ; /* XXX: flush queue? */
1876 hr->hr_preqretries++;
1878 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_HWMP, rt->rt_dest,
1879 "start path rediscovery , target seq %u", hr->hr_seq);
1881 * Try to discover the path for this node.
1882 * Group addressed PREQ Case A
1884 preq.preq_flags = 0;
1885 preq.preq_hopcount = 0;
1886 preq.preq_ttl = ms->ms_ttl;
1887 preq.preq_id = ++hs->hs_preqid;
1888 IEEE80211_ADDR_COPY(preq.preq_origaddr, vap->iv_myaddr);
1889 preq.preq_origseq = hr->hr_origseq;
1890 preq.preq_lifetime = ticks_to_msecs(ieee80211_hwmp_pathtimeout);
1891 preq.preq_metric = IEEE80211_MESHLMETRIC_INITIALVAL;
1892 preq.preq_tcount = 1;
1893 IEEE80211_ADDR_COPY(PREQ_TADDR(0), rt->rt_dest);
1895 if (ieee80211_hwmp_targetonly)
1896 PREQ_TFLAGS(0) |= IEEE80211_MESHPREQ_TFLAGS_TO;
1897 PREQ_TFLAGS(0) |= IEEE80211_MESHPREQ_TFLAGS_USN;
1898 PREQ_TSEQ(0) = 0; /* RESERVED when USN flag is set */
1899 /* XXX check return value */
1900 hwmp_send_preq(vap, broadcastaddr, &preq, &hr->hr_lastpreq,
1901 &ieee80211_hwmp_preqminint);
1902 callout_reset(&rt->rt_discovery,
1903 ieee80211_hwmp_net_diameter_traversaltime * 2,
1904 hwmp_rediscover_cb, rt);
1907 static struct ieee80211_node *
1908 hwmp_discover(struct ieee80211vap *vap,
1909 const uint8_t dest[IEEE80211_ADDR_LEN], struct mbuf *m)
1911 struct ieee80211_hwmp_state *hs = vap->iv_hwmp;
1912 struct ieee80211_mesh_state *ms = vap->iv_mesh;
1913 struct ieee80211_mesh_route *rt = NULL;
1914 struct ieee80211_hwmp_route *hr;
1915 struct ieee80211_meshpreq_ie preq;
1916 struct ieee80211_node *ni;
1919 KASSERT(vap->iv_opmode == IEEE80211_M_MBSS,
1920 ("not a mesh vap, opmode %d", vap->iv_opmode));
1922 KASSERT(!IEEE80211_ADDR_EQ(vap->iv_myaddr, dest),
1923 ("%s: discovering self!", __func__));
1926 if (!IEEE80211_IS_MULTICAST(dest)) {
1927 rt = ieee80211_mesh_rt_find(vap, dest);
1929 rt = ieee80211_mesh_rt_add(vap, dest);
1931 IEEE80211_NOTE(vap, IEEE80211_MSG_HWMP,
1932 ni, "unable to add discovery path to %6D",
1934 vap->iv_stats.is_mesh_rtaddfailed++;
1938 hr = IEEE80211_MESH_ROUTE_PRIV(rt,
1939 struct ieee80211_hwmp_route);
1940 if (rt->rt_flags & IEEE80211_MESHRT_FLAGS_DISCOVER) {
1941 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_HWMP, dest,
1942 "%s", "already discovering queue frame until path found");
1946 if ((rt->rt_flags & IEEE80211_MESHRT_FLAGS_VALID) == 0) {
1947 if (hr->hr_lastdiscovery != 0 &&
1948 (ticks - hr->hr_lastdiscovery <
1949 (ieee80211_hwmp_net_diameter_traversaltime * 2))) {
1950 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
1952 "too frequent discovery requeust");
1956 hr->hr_lastdiscovery = ticks;
1957 if (hr->hr_preqretries >=
1958 ieee80211_hwmp_maxpreq_retries) {
1959 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
1961 "no valid path , max number of discovery");
1962 vap->iv_stats.is_mesh_fwd_nopath++;
1965 rt->rt_flags = IEEE80211_MESHRT_FLAGS_DISCOVER;
1966 hr->hr_preqretries++;
1967 if (hr->hr_origseq == 0)
1968 hr->hr_origseq = ++hs->hs_seq;
1969 rt->rt_metric = IEEE80211_MESHLMETRIC_INITIALVAL;
1971 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_HWMP, dest,
1972 "start path discovery (src %s), target seq %u",
1973 m == NULL ? "<none>" : ether_sprintf(
1974 mtod(m, struct ether_header *)->ether_shost),
1977 * Try to discover the path for this node.
1978 * Group addressed PREQ Case A
1980 preq.preq_flags = 0;
1981 preq.preq_hopcount = 0;
1982 preq.preq_ttl = ms->ms_ttl;
1983 preq.preq_id = ++hs->hs_preqid;
1984 IEEE80211_ADDR_COPY(preq.preq_origaddr, vap->iv_myaddr);
1985 preq.preq_origseq = hr->hr_origseq;
1986 preq.preq_lifetime =
1987 ticks_to_msecs(ieee80211_hwmp_pathtimeout);
1988 preq.preq_metric = IEEE80211_MESHLMETRIC_INITIALVAL;
1989 preq.preq_tcount = 1;
1990 IEEE80211_ADDR_COPY(PREQ_TADDR(0), dest);
1992 if (ieee80211_hwmp_targetonly)
1993 PREQ_TFLAGS(0) |= IEEE80211_MESHPREQ_TFLAGS_TO;
1994 PREQ_TFLAGS(0) |= IEEE80211_MESHPREQ_TFLAGS_USN;
1995 PREQ_TSEQ(0) = 0; /* RESERVED when USN flag is set */
1996 /* XXX check return value */
1997 hwmp_send_preq(vap, broadcastaddr, &preq,
1998 &hr->hr_lastpreq, &ieee80211_hwmp_preqminint);
1999 callout_reset(&rt->rt_discovery,
2000 ieee80211_hwmp_net_diameter_traversaltime * 2,
2001 hwmp_rediscover_cb, rt);
2003 if (rt->rt_flags & IEEE80211_MESHRT_FLAGS_VALID)
2004 ni = ieee80211_find_txnode(vap, rt->rt_nexthop);
2006 ni = ieee80211_find_txnode(vap, dest);
2007 /* NB: if null then we leak mbuf */
2008 KASSERT(ni != NULL, ("leak mcast frame"));
2012 if (ni == NULL && m != NULL) {
2014 struct ieee80211com *ic = vap->iv_ic;
2016 * Queue packet for transmit when path discovery
2017 * completes. If discovery never completes the
2018 * frame will be flushed by way of the aging timer.
2020 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_HWMP, dest,
2021 "%s", "queue frame until path found");
2022 MPASS((m->m_pkthdr.csum_flags & CSUM_SND_TAG) == 0);
2023 m->m_pkthdr.rcvif = (void *)(uintptr_t)
2024 ieee80211_mac_hash(ic, dest);
2025 /* XXX age chosen randomly */
2026 ieee80211_ageq_append(&ic->ic_stageq, m,
2027 IEEE80211_INACT_WAIT);
2029 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_HWMP,
2030 dest, NULL, "%s", "no valid path to this node");
2041 hwmp_ioctl_get80211(struct ieee80211vap *vap, struct ieee80211req *ireq)
2043 struct ieee80211_hwmp_state *hs = vap->iv_hwmp;
2046 if (vap->iv_opmode != IEEE80211_M_MBSS)
2049 switch (ireq->i_type) {
2050 case IEEE80211_IOC_HWMP_ROOTMODE:
2051 ireq->i_val = hs->hs_rootmode;
2053 case IEEE80211_IOC_HWMP_MAXHOPS:
2054 ireq->i_val = hs->hs_maxhops;
2061 IEEE80211_IOCTL_GET(hwmp, hwmp_ioctl_get80211);
2064 hwmp_ioctl_set80211(struct ieee80211vap *vap, struct ieee80211req *ireq)
2066 struct ieee80211_hwmp_state *hs = vap->iv_hwmp;
2069 if (vap->iv_opmode != IEEE80211_M_MBSS)
2072 switch (ireq->i_type) {
2073 case IEEE80211_IOC_HWMP_ROOTMODE:
2074 if (ireq->i_val < 0 || ireq->i_val > 3)
2076 hs->hs_rootmode = ireq->i_val;
2077 hwmp_rootmode_setup(vap);
2079 case IEEE80211_IOC_HWMP_MAXHOPS:
2080 if (ireq->i_val <= 0 || ireq->i_val > 255)
2082 hs->hs_maxhops = ireq->i_val;
2089 IEEE80211_IOCTL_SET(hwmp, hwmp_ioctl_set80211);