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 Mesh Point (MBSS) support.
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>
58 #include <net/if_var.h>
59 #include <net/if_media.h>
60 #include <net/if_llc.h>
61 #include <net/ethernet.h>
63 #include <net80211/ieee80211_var.h>
64 #include <net80211/ieee80211_action.h>
65 #ifdef IEEE80211_SUPPORT_SUPERG
66 #include <net80211/ieee80211_superg.h>
68 #include <net80211/ieee80211_input.h>
69 #include <net80211/ieee80211_mesh.h>
71 static void mesh_rt_flush_invalid(struct ieee80211vap *);
72 static int mesh_select_proto_path(struct ieee80211vap *, const char *);
73 static int mesh_select_proto_metric(struct ieee80211vap *, const char *);
74 static void mesh_vattach(struct ieee80211vap *);
75 static int mesh_newstate(struct ieee80211vap *, enum ieee80211_state, int);
76 static void mesh_rt_cleanup_cb(void *);
77 static void mesh_gatemode_setup(struct ieee80211vap *);
78 static void mesh_gatemode_cb(void *);
79 static void mesh_linkchange(struct ieee80211_node *,
80 enum ieee80211_mesh_mlstate);
81 static void mesh_checkid(void *, struct ieee80211_node *);
82 static uint32_t mesh_generateid(struct ieee80211vap *);
83 static int mesh_checkpseq(struct ieee80211vap *,
84 const uint8_t [IEEE80211_ADDR_LEN], uint32_t);
85 static void mesh_transmit_to_gate(struct ieee80211vap *, struct mbuf *,
86 struct ieee80211_mesh_route *);
87 static void mesh_forward(struct ieee80211vap *, struct mbuf *,
88 const struct ieee80211_meshcntl *);
89 static int mesh_input(struct ieee80211_node *, struct mbuf *,
90 const struct ieee80211_rx_stats *rxs, int, int);
91 static void mesh_recv_mgmt(struct ieee80211_node *, struct mbuf *, int,
92 const struct ieee80211_rx_stats *rxs, int, int);
93 static void mesh_recv_ctl(struct ieee80211_node *, struct mbuf *, int);
94 static void mesh_peer_timeout_setup(struct ieee80211_node *);
95 static void mesh_peer_timeout_backoff(struct ieee80211_node *);
96 static void mesh_peer_timeout_cb(void *);
98 mesh_peer_timeout_stop(struct ieee80211_node *);
99 static int mesh_verify_meshid(struct ieee80211vap *, const uint8_t *);
100 static int mesh_verify_meshconf(struct ieee80211vap *, const uint8_t *);
101 static int mesh_verify_meshpeer(struct ieee80211vap *, uint8_t,
103 uint32_t mesh_airtime_calc(struct ieee80211_node *);
106 * Timeout values come from the specification and are in milliseconds.
108 static SYSCTL_NODE(_net_wlan, OID_AUTO, mesh, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
109 "IEEE 802.11s parameters");
110 static int ieee80211_mesh_gateint = -1;
111 SYSCTL_PROC(_net_wlan_mesh, OID_AUTO, gateint,
112 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
113 &ieee80211_mesh_gateint, 0, ieee80211_sysctl_msecs_ticks, "I",
114 "mesh gate interval (ms)");
115 static int ieee80211_mesh_retrytimeout = -1;
116 SYSCTL_PROC(_net_wlan_mesh, OID_AUTO, retrytimeout,
117 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
118 &ieee80211_mesh_retrytimeout, 0, ieee80211_sysctl_msecs_ticks, "I",
119 "Retry timeout (msec)");
120 static int ieee80211_mesh_holdingtimeout = -1;
122 SYSCTL_PROC(_net_wlan_mesh, OID_AUTO, holdingtimeout,
123 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
124 &ieee80211_mesh_holdingtimeout, 0, ieee80211_sysctl_msecs_ticks, "I",
125 "Holding state timeout (msec)");
126 static int ieee80211_mesh_confirmtimeout = -1;
127 SYSCTL_PROC(_net_wlan_mesh, OID_AUTO, confirmtimeout,
128 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
129 &ieee80211_mesh_confirmtimeout, 0, ieee80211_sysctl_msecs_ticks, "I",
130 "Confirm state timeout (msec)");
131 static int ieee80211_mesh_backofftimeout = -1;
132 SYSCTL_PROC(_net_wlan_mesh, OID_AUTO, backofftimeout,
133 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
134 &ieee80211_mesh_backofftimeout, 0, ieee80211_sysctl_msecs_ticks, "I",
135 "Backoff timeout (msec). This is to throutles peering forever when "
136 "not receiving answer or is rejected by a neighbor");
137 static int ieee80211_mesh_maxretries = 2;
138 SYSCTL_INT(_net_wlan_mesh, OID_AUTO, maxretries, CTLFLAG_RW,
139 &ieee80211_mesh_maxretries, 0,
140 "Maximum retries during peer link establishment");
141 static int ieee80211_mesh_maxholding = 2;
142 SYSCTL_INT(_net_wlan_mesh, OID_AUTO, maxholding, CTLFLAG_RW,
143 &ieee80211_mesh_maxholding, 0,
144 "Maximum times we are allowed to transition to HOLDING state before "
145 "backinoff during peer link establishment");
147 static const uint8_t broadcastaddr[IEEE80211_ADDR_LEN] =
148 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
150 static ieee80211_recv_action_func mesh_recv_action_meshpeering_open;
151 static ieee80211_recv_action_func mesh_recv_action_meshpeering_confirm;
152 static ieee80211_recv_action_func mesh_recv_action_meshpeering_close;
153 static ieee80211_recv_action_func mesh_recv_action_meshlmetric;
154 static ieee80211_recv_action_func mesh_recv_action_meshgate;
156 static ieee80211_send_action_func mesh_send_action_meshpeering_open;
157 static ieee80211_send_action_func mesh_send_action_meshpeering_confirm;
158 static ieee80211_send_action_func mesh_send_action_meshpeering_close;
159 static ieee80211_send_action_func mesh_send_action_meshlmetric;
160 static ieee80211_send_action_func mesh_send_action_meshgate;
162 static const struct ieee80211_mesh_proto_metric mesh_metric_airtime = {
163 .mpm_descr = "AIRTIME",
164 .mpm_ie = IEEE80211_MESHCONF_METRIC_AIRTIME,
165 .mpm_metric = mesh_airtime_calc,
168 static struct ieee80211_mesh_proto_path mesh_proto_paths[4];
169 static struct ieee80211_mesh_proto_metric mesh_proto_metrics[4];
171 MALLOC_DEFINE(M_80211_MESH_PREQ, "80211preq", "802.11 MESH Path Request frame");
172 MALLOC_DEFINE(M_80211_MESH_PREP, "80211prep", "802.11 MESH Path Reply frame");
173 MALLOC_DEFINE(M_80211_MESH_PERR, "80211perr", "802.11 MESH Path Error frame");
175 /* The longer one of the lifetime should be stored as new lifetime */
176 #define MESH_ROUTE_LIFETIME_MAX(a, b) (a > b ? a : b)
178 MALLOC_DEFINE(M_80211_MESH_RT, "80211mesh_rt", "802.11s routing table");
179 MALLOC_DEFINE(M_80211_MESH_GT_RT, "80211mesh_gt", "802.11s known gates table");
182 * Helper functions to manipulate the Mesh routing table.
185 static struct ieee80211_mesh_route *
186 mesh_rt_find_locked(struct ieee80211_mesh_state *ms,
187 const uint8_t dest[IEEE80211_ADDR_LEN])
189 struct ieee80211_mesh_route *rt;
191 MESH_RT_LOCK_ASSERT(ms);
193 TAILQ_FOREACH(rt, &ms->ms_routes, rt_next) {
194 if (IEEE80211_ADDR_EQ(dest, rt->rt_dest))
200 static struct ieee80211_mesh_route *
201 mesh_rt_add_locked(struct ieee80211vap *vap,
202 const uint8_t dest[IEEE80211_ADDR_LEN])
204 struct ieee80211_mesh_state *ms = vap->iv_mesh;
205 struct ieee80211_mesh_route *rt;
207 KASSERT(!IEEE80211_ADDR_EQ(broadcastaddr, dest),
208 ("%s: adding broadcast to the routing table", __func__));
210 MESH_RT_LOCK_ASSERT(ms);
212 rt = IEEE80211_MALLOC(ALIGN(sizeof(struct ieee80211_mesh_route)) +
213 ms->ms_ppath->mpp_privlen, M_80211_MESH_RT,
214 IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
217 IEEE80211_ADDR_COPY(rt->rt_dest, dest);
218 rt->rt_priv = (void *)ALIGN(&rt[1]);
219 MESH_RT_ENTRY_LOCK_INIT(rt, "MBSS_RT");
220 callout_init(&rt->rt_discovery, 1);
221 rt->rt_updtime = ticks; /* create time */
222 TAILQ_INSERT_TAIL(&ms->ms_routes, rt, rt_next);
227 struct ieee80211_mesh_route *
228 ieee80211_mesh_rt_find(struct ieee80211vap *vap,
229 const uint8_t dest[IEEE80211_ADDR_LEN])
231 struct ieee80211_mesh_state *ms = vap->iv_mesh;
232 struct ieee80211_mesh_route *rt;
235 rt = mesh_rt_find_locked(ms, dest);
240 struct ieee80211_mesh_route *
241 ieee80211_mesh_rt_add(struct ieee80211vap *vap,
242 const uint8_t dest[IEEE80211_ADDR_LEN])
244 struct ieee80211_mesh_state *ms = vap->iv_mesh;
245 struct ieee80211_mesh_route *rt;
247 KASSERT(ieee80211_mesh_rt_find(vap, dest) == NULL,
248 ("%s: duplicate entry in the routing table", __func__));
249 KASSERT(!IEEE80211_ADDR_EQ(vap->iv_myaddr, dest),
250 ("%s: adding self to the routing table", __func__));
253 rt = mesh_rt_add_locked(vap, dest);
259 * Update the route lifetime and returns the updated lifetime.
260 * If new_lifetime is zero and route is timedout it will be invalidated.
261 * new_lifetime is in msec
264 ieee80211_mesh_rt_update(struct ieee80211_mesh_route *rt, int new_lifetime)
267 uint32_t lifetime = 0;
269 KASSERT(rt != NULL, ("route is NULL"));
272 MESH_RT_ENTRY_LOCK(rt);
274 /* dont clobber a proxy entry gated by us */
275 if (rt->rt_flags & IEEE80211_MESHRT_FLAGS_PROXY && rt->rt_nhops == 0) {
276 MESH_RT_ENTRY_UNLOCK(rt);
277 return rt->rt_lifetime;
280 timesince = ticks_to_msecs(now - rt->rt_updtime);
281 rt->rt_updtime = now;
282 if (timesince >= rt->rt_lifetime) {
283 if (new_lifetime != 0) {
284 rt->rt_lifetime = new_lifetime;
287 rt->rt_flags &= ~IEEE80211_MESHRT_FLAGS_VALID;
291 /* update what is left of lifetime */
292 rt->rt_lifetime = rt->rt_lifetime - timesince;
293 rt->rt_lifetime = MESH_ROUTE_LIFETIME_MAX(
294 new_lifetime, rt->rt_lifetime);
296 lifetime = rt->rt_lifetime;
297 MESH_RT_ENTRY_UNLOCK(rt);
303 * Add a proxy route (as needed) for the specified destination.
306 ieee80211_mesh_proxy_check(struct ieee80211vap *vap,
307 const uint8_t dest[IEEE80211_ADDR_LEN])
309 struct ieee80211_mesh_state *ms = vap->iv_mesh;
310 struct ieee80211_mesh_route *rt;
313 rt = mesh_rt_find_locked(ms, dest);
315 rt = mesh_rt_add_locked(vap, dest);
317 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH, dest,
318 "%s", "unable to add proxy entry");
319 vap->iv_stats.is_mesh_rtaddfailed++;
321 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH, dest,
322 "%s", "add proxy entry");
323 IEEE80211_ADDR_COPY(rt->rt_mesh_gate, vap->iv_myaddr);
324 IEEE80211_ADDR_COPY(rt->rt_nexthop, vap->iv_myaddr);
325 rt->rt_flags |= IEEE80211_MESHRT_FLAGS_VALID
326 | IEEE80211_MESHRT_FLAGS_PROXY;
328 } else if ((rt->rt_flags & IEEE80211_MESHRT_FLAGS_VALID) == 0) {
329 KASSERT(rt->rt_flags & IEEE80211_MESHRT_FLAGS_PROXY,
330 ("no proxy flag for poxy entry"));
331 struct ieee80211com *ic = vap->iv_ic;
333 * Fix existing entry created by received frames from
334 * stations that have some memory of dest. We also
335 * flush any frames held on the staging queue; delivering
336 * them is too much trouble right now.
338 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH, dest,
339 "%s", "fix proxy entry");
340 IEEE80211_ADDR_COPY(rt->rt_nexthop, vap->iv_myaddr);
341 rt->rt_flags |= IEEE80211_MESHRT_FLAGS_VALID
342 | IEEE80211_MESHRT_FLAGS_PROXY;
343 /* XXX belongs in hwmp */
344 ieee80211_ageq_drain_node(&ic->ic_stageq,
345 (void *)(uintptr_t) ieee80211_mac_hash(ic, dest));
352 mesh_rt_del(struct ieee80211_mesh_state *ms, struct ieee80211_mesh_route *rt)
354 TAILQ_REMOVE(&ms->ms_routes, rt, rt_next);
356 * Grab the lock before destroying it, to be sure no one else
357 * is holding the route.
359 MESH_RT_ENTRY_LOCK(rt);
360 callout_drain(&rt->rt_discovery);
361 MESH_RT_ENTRY_LOCK_DESTROY(rt);
362 IEEE80211_FREE(rt, M_80211_MESH_RT);
366 ieee80211_mesh_rt_del(struct ieee80211vap *vap,
367 const uint8_t dest[IEEE80211_ADDR_LEN])
369 struct ieee80211_mesh_state *ms = vap->iv_mesh;
370 struct ieee80211_mesh_route *rt, *next;
373 TAILQ_FOREACH_SAFE(rt, &ms->ms_routes, rt_next, next) {
374 if (IEEE80211_ADDR_EQ(rt->rt_dest, dest)) {
375 if (rt->rt_flags & IEEE80211_MESHRT_FLAGS_PROXY) {
376 ms->ms_ppath->mpp_senderror(vap, dest, rt,
377 IEEE80211_REASON_MESH_PERR_NO_PROXY);
379 ms->ms_ppath->mpp_senderror(vap, dest, rt,
380 IEEE80211_REASON_MESH_PERR_DEST_UNREACH);
391 ieee80211_mesh_rt_flush(struct ieee80211vap *vap)
393 struct ieee80211_mesh_state *ms = vap->iv_mesh;
394 struct ieee80211_mesh_route *rt, *next;
399 TAILQ_FOREACH_SAFE(rt, &ms->ms_routes, rt_next, next)
405 ieee80211_mesh_rt_flush_peer(struct ieee80211vap *vap,
406 const uint8_t peer[IEEE80211_ADDR_LEN])
408 struct ieee80211_mesh_state *ms = vap->iv_mesh;
409 struct ieee80211_mesh_route *rt, *next;
412 TAILQ_FOREACH_SAFE(rt, &ms->ms_routes, rt_next, next) {
413 if (IEEE80211_ADDR_EQ(rt->rt_nexthop, peer))
420 * Flush expired routing entries, i.e. those in invalid state for
424 mesh_rt_flush_invalid(struct ieee80211vap *vap)
426 struct ieee80211_mesh_state *ms = vap->iv_mesh;
427 struct ieee80211_mesh_route *rt, *next;
432 TAILQ_FOREACH_SAFE(rt, &ms->ms_routes, rt_next, next) {
433 /* Discover paths will be deleted by their own callout */
434 if (rt->rt_flags & IEEE80211_MESHRT_FLAGS_DISCOVER)
436 ieee80211_mesh_rt_update(rt, 0);
437 if ((rt->rt_flags & IEEE80211_MESHRT_FLAGS_VALID) == 0)
444 ieee80211_mesh_register_proto_path(const struct ieee80211_mesh_proto_path *mpp)
446 int i, firstempty = -1;
448 for (i = 0; i < nitems(mesh_proto_paths); i++) {
449 if (strncmp(mpp->mpp_descr, mesh_proto_paths[i].mpp_descr,
450 IEEE80211_MESH_PROTO_DSZ) == 0)
452 if (!mesh_proto_paths[i].mpp_active && firstempty == -1)
457 memcpy(&mesh_proto_paths[firstempty], mpp, sizeof(*mpp));
458 mesh_proto_paths[firstempty].mpp_active = 1;
463 ieee80211_mesh_register_proto_metric(const struct
464 ieee80211_mesh_proto_metric *mpm)
466 int i, firstempty = -1;
468 for (i = 0; i < nitems(mesh_proto_metrics); i++) {
469 if (strncmp(mpm->mpm_descr, mesh_proto_metrics[i].mpm_descr,
470 IEEE80211_MESH_PROTO_DSZ) == 0)
472 if (!mesh_proto_metrics[i].mpm_active && firstempty == -1)
477 memcpy(&mesh_proto_metrics[firstempty], mpm, sizeof(*mpm));
478 mesh_proto_metrics[firstempty].mpm_active = 1;
483 mesh_select_proto_path(struct ieee80211vap *vap, const char *name)
485 struct ieee80211_mesh_state *ms = vap->iv_mesh;
488 for (i = 0; i < nitems(mesh_proto_paths); i++) {
489 if (strcasecmp(mesh_proto_paths[i].mpp_descr, name) == 0) {
490 ms->ms_ppath = &mesh_proto_paths[i];
498 mesh_select_proto_metric(struct ieee80211vap *vap, const char *name)
500 struct ieee80211_mesh_state *ms = vap->iv_mesh;
503 for (i = 0; i < nitems(mesh_proto_metrics); i++) {
504 if (strcasecmp(mesh_proto_metrics[i].mpm_descr, name) == 0) {
505 ms->ms_pmetric = &mesh_proto_metrics[i];
513 mesh_gatemode_setup(struct ieee80211vap *vap)
515 struct ieee80211_mesh_state *ms = vap->iv_mesh;
518 * NB: When a mesh gate is running as a ROOT it shall
519 * not send out periodic GANNs but instead mark the
520 * mesh gate flag for the corresponding proactive PREQ
523 if (ms->ms_flags & IEEE80211_MESHFLAGS_ROOT ||
524 (ms->ms_flags & IEEE80211_MESHFLAGS_GATE) == 0) {
525 callout_drain(&ms->ms_gatetimer);
528 callout_reset(&ms->ms_gatetimer, ieee80211_mesh_gateint,
529 mesh_gatemode_cb, vap);
533 mesh_gatemode_cb(void *arg)
535 struct ieee80211vap *vap = (struct ieee80211vap *)arg;
536 struct ieee80211_mesh_state *ms = vap->iv_mesh;
537 struct ieee80211_meshgann_ie gann;
539 gann.gann_flags = 0; /* Reserved */
540 gann.gann_hopcount = 0;
541 gann.gann_ttl = ms->ms_ttl;
542 IEEE80211_ADDR_COPY(gann.gann_addr, vap->iv_myaddr);
543 gann.gann_seq = ms->ms_gateseq++;
544 gann.gann_interval = ieee80211_mesh_gateint;
546 IEEE80211_NOTE(vap, IEEE80211_MSG_MESH, vap->iv_bss,
547 "send broadcast GANN (seq %u)", gann.gann_seq);
549 ieee80211_send_action(vap->iv_bss, IEEE80211_ACTION_CAT_MESH,
550 IEEE80211_ACTION_MESH_GANN, &gann);
551 mesh_gatemode_setup(vap);
555 ieee80211_mesh_init(void)
558 memset(mesh_proto_paths, 0, sizeof(mesh_proto_paths));
559 memset(mesh_proto_metrics, 0, sizeof(mesh_proto_metrics));
562 * Setup mesh parameters that depends on the clock frequency.
564 ieee80211_mesh_gateint = msecs_to_ticks(10000);
565 ieee80211_mesh_retrytimeout = msecs_to_ticks(40);
566 ieee80211_mesh_holdingtimeout = msecs_to_ticks(40);
567 ieee80211_mesh_confirmtimeout = msecs_to_ticks(40);
568 ieee80211_mesh_backofftimeout = msecs_to_ticks(5000);
571 * Register action frame handlers.
573 ieee80211_recv_action_register(IEEE80211_ACTION_CAT_SELF_PROT,
574 IEEE80211_ACTION_MESHPEERING_OPEN,
575 mesh_recv_action_meshpeering_open);
576 ieee80211_recv_action_register(IEEE80211_ACTION_CAT_SELF_PROT,
577 IEEE80211_ACTION_MESHPEERING_CONFIRM,
578 mesh_recv_action_meshpeering_confirm);
579 ieee80211_recv_action_register(IEEE80211_ACTION_CAT_SELF_PROT,
580 IEEE80211_ACTION_MESHPEERING_CLOSE,
581 mesh_recv_action_meshpeering_close);
582 ieee80211_recv_action_register(IEEE80211_ACTION_CAT_MESH,
583 IEEE80211_ACTION_MESH_LMETRIC, mesh_recv_action_meshlmetric);
584 ieee80211_recv_action_register(IEEE80211_ACTION_CAT_MESH,
585 IEEE80211_ACTION_MESH_GANN, mesh_recv_action_meshgate);
587 ieee80211_send_action_register(IEEE80211_ACTION_CAT_SELF_PROT,
588 IEEE80211_ACTION_MESHPEERING_OPEN,
589 mesh_send_action_meshpeering_open);
590 ieee80211_send_action_register(IEEE80211_ACTION_CAT_SELF_PROT,
591 IEEE80211_ACTION_MESHPEERING_CONFIRM,
592 mesh_send_action_meshpeering_confirm);
593 ieee80211_send_action_register(IEEE80211_ACTION_CAT_SELF_PROT,
594 IEEE80211_ACTION_MESHPEERING_CLOSE,
595 mesh_send_action_meshpeering_close);
596 ieee80211_send_action_register(IEEE80211_ACTION_CAT_MESH,
597 IEEE80211_ACTION_MESH_LMETRIC,
598 mesh_send_action_meshlmetric);
599 ieee80211_send_action_register(IEEE80211_ACTION_CAT_MESH,
600 IEEE80211_ACTION_MESH_GANN,
601 mesh_send_action_meshgate);
604 * Register Airtime Link Metric.
606 ieee80211_mesh_register_proto_metric(&mesh_metric_airtime);
609 SYSINIT(wlan_mesh, SI_SUB_DRIVERS, SI_ORDER_FIRST, ieee80211_mesh_init, NULL);
612 ieee80211_mesh_attach(struct ieee80211com *ic)
614 ic->ic_vattach[IEEE80211_M_MBSS] = mesh_vattach;
618 ieee80211_mesh_detach(struct ieee80211com *ic)
623 mesh_vdetach_peers(void *arg, struct ieee80211_node *ni)
625 struct ieee80211com *ic = ni->ni_ic;
628 if (ni->ni_mlstate == IEEE80211_NODE_MESH_ESTABLISHED) {
629 args[0] = ni->ni_mlpid;
630 args[1] = ni->ni_mllid;
631 args[2] = IEEE80211_REASON_PEER_LINK_CANCELED;
632 ieee80211_send_action(ni,
633 IEEE80211_ACTION_CAT_SELF_PROT,
634 IEEE80211_ACTION_MESHPEERING_CLOSE,
637 callout_drain(&ni->ni_mltimer);
638 /* XXX belongs in hwmp */
639 ieee80211_ageq_drain_node(&ic->ic_stageq,
640 (void *)(uintptr_t) ieee80211_mac_hash(ic, ni->ni_macaddr));
644 mesh_vdetach(struct ieee80211vap *vap)
646 struct ieee80211_mesh_state *ms = vap->iv_mesh;
648 callout_drain(&ms->ms_cleantimer);
649 ieee80211_iterate_nodes(&vap->iv_ic->ic_sta, mesh_vdetach_peers,
651 ieee80211_mesh_rt_flush(vap);
652 MESH_RT_LOCK_DESTROY(ms);
653 ms->ms_ppath->mpp_vdetach(vap);
654 IEEE80211_FREE(vap->iv_mesh, M_80211_VAP);
659 mesh_vattach(struct ieee80211vap *vap)
661 struct ieee80211_mesh_state *ms;
662 vap->iv_newstate = mesh_newstate;
663 vap->iv_input = mesh_input;
664 vap->iv_opdetach = mesh_vdetach;
665 vap->iv_recv_mgmt = mesh_recv_mgmt;
666 vap->iv_recv_ctl = mesh_recv_ctl;
667 ms = IEEE80211_MALLOC(sizeof(struct ieee80211_mesh_state), M_80211_VAP,
668 IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
670 printf("%s: couldn't alloc MBSS state\n", __func__);
675 ms->ms_flags = (IEEE80211_MESHFLAGS_AP | IEEE80211_MESHFLAGS_FWD);
676 ms->ms_ttl = IEEE80211_MESH_DEFAULT_TTL;
677 TAILQ_INIT(&ms->ms_known_gates);
678 TAILQ_INIT(&ms->ms_routes);
679 MESH_RT_LOCK_INIT(ms, "MBSS");
680 callout_init(&ms->ms_cleantimer, 1);
681 callout_init(&ms->ms_gatetimer, 1);
683 mesh_select_proto_metric(vap, "AIRTIME");
684 KASSERT(ms->ms_pmetric, ("ms_pmetric == NULL"));
685 mesh_select_proto_path(vap, "HWMP");
686 KASSERT(ms->ms_ppath, ("ms_ppath == NULL"));
687 ms->ms_ppath->mpp_vattach(vap);
691 * IEEE80211_M_MBSS vap state machine handler.
694 mesh_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
696 struct ieee80211_mesh_state *ms = vap->iv_mesh;
697 struct ieee80211com *ic = vap->iv_ic;
698 struct ieee80211_node *ni;
699 enum ieee80211_state ostate;
701 IEEE80211_LOCK_ASSERT(ic);
703 ostate = vap->iv_state;
704 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE, "%s: %s -> %s (%d)\n",
705 __func__, ieee80211_state_name[ostate],
706 ieee80211_state_name[nstate], arg);
707 vap->iv_state = nstate; /* state transition */
708 if (ostate != IEEE80211_S_SCAN)
709 ieee80211_cancel_scan(vap); /* background scan */
710 ni = vap->iv_bss; /* NB: no reference held */
711 if (nstate != IEEE80211_S_RUN && ostate == IEEE80211_S_RUN) {
712 callout_drain(&ms->ms_cleantimer);
713 callout_drain(&ms->ms_gatetimer);
716 case IEEE80211_S_INIT:
718 case IEEE80211_S_SCAN:
719 ieee80211_cancel_scan(vap);
721 case IEEE80211_S_CAC:
722 ieee80211_dfs_cac_stop(vap);
724 case IEEE80211_S_RUN:
725 ieee80211_iterate_nodes(&ic->ic_sta,
726 mesh_vdetach_peers, NULL);
731 if (ostate != IEEE80211_S_INIT) {
732 /* NB: optimize INIT -> INIT case */
733 ieee80211_reset_bss(vap);
734 ieee80211_mesh_rt_flush(vap);
737 case IEEE80211_S_SCAN:
739 case IEEE80211_S_INIT:
740 if (vap->iv_des_chan != IEEE80211_CHAN_ANYC &&
741 !IEEE80211_IS_CHAN_RADAR(vap->iv_des_chan) &&
744 * Already have a channel and a mesh ID; bypass
745 * the scan and startup immediately.
747 ieee80211_create_ibss(vap, vap->iv_des_chan);
751 * Initiate a scan. We can come here as a result
752 * of an IEEE80211_IOC_SCAN_REQ too in which case
753 * the vap will be marked with IEEE80211_FEXT_SCANREQ
754 * and the scan request parameters will be present
755 * in iv_scanreq. Otherwise we do the default.
757 if (vap->iv_flags_ext & IEEE80211_FEXT_SCANREQ) {
758 ieee80211_check_scan(vap,
759 vap->iv_scanreq_flags,
760 vap->iv_scanreq_duration,
761 vap->iv_scanreq_mindwell,
762 vap->iv_scanreq_maxdwell,
763 vap->iv_scanreq_nssid, vap->iv_scanreq_ssid);
764 vap->iv_flags_ext &= ~IEEE80211_FEXT_SCANREQ;
766 ieee80211_check_scan_current(vap);
772 case IEEE80211_S_CAC:
774 * Start CAC on a DFS channel. We come here when starting
775 * a bss on a DFS channel (see ieee80211_create_ibss).
777 ieee80211_dfs_cac_start(vap);
779 case IEEE80211_S_RUN:
781 case IEEE80211_S_INIT:
783 * Already have a channel; bypass the
784 * scan and startup immediately.
785 * Note that ieee80211_create_ibss will call
786 * back to do a RUN->RUN state change.
788 ieee80211_create_ibss(vap,
789 ieee80211_ht_adjust_channel(ic,
790 ic->ic_curchan, vap->iv_flags_ht));
791 /* NB: iv_bss is changed on return */
793 case IEEE80211_S_CAC:
795 * NB: This is the normal state change when CAC
796 * expires and no radar was detected; no need to
797 * clear the CAC timer as it's already expired.
800 case IEEE80211_S_CSA:
803 * Shorten inactivity timer of associated stations
804 * to weed out sta's that don't follow a CSA.
806 ieee80211_iterate_nodes(&ic->ic_sta, sta_csa, vap);
809 * Update bss node channel to reflect where
810 * we landed after CSA.
812 ieee80211_node_set_chan(ni,
813 ieee80211_ht_adjust_channel(ic, ic->ic_curchan,
814 ieee80211_htchanflags(ni->ni_chan)));
815 /* XXX bypass debug msgs */
817 case IEEE80211_S_SCAN:
818 case IEEE80211_S_RUN:
819 #ifdef IEEE80211_DEBUG
820 if (ieee80211_msg_debug(vap)) {
822 "synchronized with %s meshid ",
823 ether_sprintf(ni->ni_meshid));
824 ieee80211_print_essid(ni->ni_meshid,
827 printf(" channel %d\n",
828 ieee80211_chan2ieee(ic, ic->ic_curchan));
835 ieee80211_node_authorize(ni);
836 callout_reset(&ms->ms_cleantimer, ms->ms_ppath->mpp_inact,
837 mesh_rt_cleanup_cb, vap);
838 mesh_gatemode_setup(vap);
843 /* NB: ostate not nstate */
844 ms->ms_ppath->mpp_newstate(vap, ostate, arg);
849 mesh_rt_cleanup_cb(void *arg)
851 struct ieee80211vap *vap = arg;
852 struct ieee80211_mesh_state *ms = vap->iv_mesh;
854 mesh_rt_flush_invalid(vap);
855 callout_reset(&ms->ms_cleantimer, ms->ms_ppath->mpp_inact,
856 mesh_rt_cleanup_cb, vap);
860 * Mark a mesh STA as gate and return a pointer to it.
861 * If this is first time, we create a new gate route.
862 * Always update the path route to this mesh gate.
864 struct ieee80211_mesh_gate_route *
865 ieee80211_mesh_mark_gate(struct ieee80211vap *vap, const uint8_t *addr,
866 struct ieee80211_mesh_route *rt)
868 struct ieee80211_mesh_state *ms = vap->iv_mesh;
869 struct ieee80211_mesh_gate_route *gr = NULL, *next;
873 TAILQ_FOREACH_SAFE(gr, &ms->ms_known_gates, gr_next, next) {
874 if (IEEE80211_ADDR_EQ(gr->gr_addr, addr)) {
881 /* New mesh gate add it to known table. */
882 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH, addr,
883 "%s", "stored new gate information from pro-PREQ.");
884 gr = IEEE80211_MALLOC(ALIGN(sizeof(struct ieee80211_mesh_gate_route)),
886 IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
887 IEEE80211_ADDR_COPY(gr->gr_addr, addr);
888 TAILQ_INSERT_TAIL(&ms->ms_known_gates, gr, gr_next);
891 /* TODO: link from path route to gate route */
898 * Helper function to note the Mesh Peer Link FSM change.
901 mesh_linkchange(struct ieee80211_node *ni, enum ieee80211_mesh_mlstate state)
903 struct ieee80211vap *vap = ni->ni_vap;
904 struct ieee80211_mesh_state *ms = vap->iv_mesh;
905 #ifdef IEEE80211_DEBUG
906 static const char *meshlinkstates[] = {
907 [IEEE80211_NODE_MESH_IDLE] = "IDLE",
908 [IEEE80211_NODE_MESH_OPENSNT] = "OPEN SENT",
909 [IEEE80211_NODE_MESH_OPENRCV] = "OPEN RECEIVED",
910 [IEEE80211_NODE_MESH_CONFIRMRCV] = "CONFIRM RECEIVED",
911 [IEEE80211_NODE_MESH_ESTABLISHED] = "ESTABLISHED",
912 [IEEE80211_NODE_MESH_HOLDING] = "HOLDING"
915 IEEE80211_NOTE(vap, IEEE80211_MSG_MESH,
916 ni, "peer link: %s -> %s",
917 meshlinkstates[ni->ni_mlstate], meshlinkstates[state]);
919 /* track neighbor count */
920 if (state == IEEE80211_NODE_MESH_ESTABLISHED &&
921 ni->ni_mlstate != IEEE80211_NODE_MESH_ESTABLISHED) {
922 KASSERT(ms->ms_neighbors < 65535, ("neighbor count overflow"));
924 ieee80211_beacon_notify(vap, IEEE80211_BEACON_MESHCONF);
925 } else if (ni->ni_mlstate == IEEE80211_NODE_MESH_ESTABLISHED &&
926 state != IEEE80211_NODE_MESH_ESTABLISHED) {
927 KASSERT(ms->ms_neighbors > 0, ("neighbor count 0"));
929 ieee80211_beacon_notify(vap, IEEE80211_BEACON_MESHCONF);
931 ni->ni_mlstate = state;
933 case IEEE80211_NODE_MESH_HOLDING:
934 ms->ms_ppath->mpp_peerdown(ni);
936 case IEEE80211_NODE_MESH_ESTABLISHED:
937 ieee80211_mesh_discover(vap, ni->ni_macaddr, NULL);
945 * Helper function to generate a unique local ID required for mesh
946 * peer establishment.
949 mesh_checkid(void *arg, struct ieee80211_node *ni)
953 if (*r == ni->ni_mllid)
954 *(uint16_t *)arg = 0;
958 mesh_generateid(struct ieee80211vap *vap)
964 net80211_get_random_bytes(&r, 2);
965 ieee80211_iterate_nodes(&vap->iv_ic->ic_sta, mesh_checkid, &r);
967 } while (r == 0 && maxiter > 0);
972 * Verifies if we already received this packet by checking its
974 * Returns 0 if the frame is to be accepted, 1 otherwise.
977 mesh_checkpseq(struct ieee80211vap *vap,
978 const uint8_t source[IEEE80211_ADDR_LEN], uint32_t seq)
980 struct ieee80211_mesh_route *rt;
982 rt = ieee80211_mesh_rt_find(vap, source);
984 rt = ieee80211_mesh_rt_add(vap, source);
986 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH, source,
987 "%s", "add mcast route failed");
988 vap->iv_stats.is_mesh_rtaddfailed++;
991 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH, source,
992 "add mcast route, mesh seqno %d", seq);
993 rt->rt_lastmseq = seq;
996 if (IEEE80211_MESH_SEQ_GEQ(rt->rt_lastmseq, seq)) {
999 rt->rt_lastmseq = seq;
1005 * Iterate the routing table and locate the next hop.
1007 struct ieee80211_node *
1008 ieee80211_mesh_find_txnode(struct ieee80211vap *vap,
1009 const uint8_t dest[IEEE80211_ADDR_LEN])
1011 struct ieee80211_mesh_route *rt;
1013 rt = ieee80211_mesh_rt_find(vap, dest);
1016 if ((rt->rt_flags & IEEE80211_MESHRT_FLAGS_VALID) == 0) {
1017 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH, dest,
1018 "%s: !valid, flags 0x%x", __func__, rt->rt_flags);
1022 if (rt->rt_flags & IEEE80211_MESHRT_FLAGS_PROXY) {
1023 rt = ieee80211_mesh_rt_find(vap, rt->rt_mesh_gate);
1024 if (rt == NULL) return NULL;
1025 if ((rt->rt_flags & IEEE80211_MESHRT_FLAGS_VALID) == 0) {
1026 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH, dest,
1027 "%s: meshgate !valid, flags 0x%x", __func__,
1033 return ieee80211_find_txnode(vap, rt->rt_nexthop);
1037 mesh_transmit_to_gate(struct ieee80211vap *vap, struct mbuf *m,
1038 struct ieee80211_mesh_route *rt_gate)
1040 struct ifnet *ifp = vap->iv_ifp;
1041 struct ieee80211_node *ni;
1043 IEEE80211_TX_UNLOCK_ASSERT(vap->iv_ic);
1045 ni = ieee80211_mesh_find_txnode(vap, rt_gate->rt_dest);
1047 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1053 * Send through the VAP packet transmit path.
1054 * This consumes the node ref grabbed above and
1055 * the mbuf, regardless of whether there's a problem
1058 (void) ieee80211_vap_pkt_send_dest(vap, m, ni);
1062 * Forward the queued frames to known valid mesh gates.
1063 * Assume destination to be outside the MBSS (i.e. proxy entry),
1064 * If no valid mesh gates are known silently discard queued frames.
1065 * After transmitting frames to all known valid mesh gates, this route
1066 * will be marked invalid, and a new path discovery will happen in the hopes
1067 * that (at least) one of the mesh gates have a new proxy entry for us to use.
1070 ieee80211_mesh_forward_to_gates(struct ieee80211vap *vap,
1071 struct ieee80211_mesh_route *rt_dest)
1073 struct ieee80211com *ic = vap->iv_ic;
1074 struct ieee80211_mesh_state *ms = vap->iv_mesh;
1075 struct ieee80211_mesh_route *rt_gate;
1076 struct ieee80211_mesh_gate_route *gr = NULL, *gr_next;
1077 struct mbuf *m, *mcopy, *next;
1079 IEEE80211_TX_UNLOCK_ASSERT(ic);
1081 KASSERT( rt_dest->rt_flags == IEEE80211_MESHRT_FLAGS_DISCOVER,
1082 ("Route is not marked with IEEE80211_MESHRT_FLAGS_DISCOVER"));
1084 /* XXX: send to more than one valid mash gate */
1087 m = ieee80211_ageq_remove(&ic->ic_stageq,
1088 (struct ieee80211_node *)(uintptr_t)
1089 ieee80211_mac_hash(ic, rt_dest->rt_dest));
1091 TAILQ_FOREACH_SAFE(gr, &ms->ms_known_gates, gr_next, gr_next) {
1092 rt_gate = gr->gr_route;
1093 if (rt_gate == NULL) {
1094 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_HWMP,
1096 "mesh gate with no path %6D",
1100 if ((rt_gate->rt_flags & IEEE80211_MESHRT_FLAGS_VALID) == 0)
1102 KASSERT(rt_gate->rt_flags & IEEE80211_MESHRT_FLAGS_GATE,
1103 ("route not marked as a mesh gate"));
1104 KASSERT((rt_gate->rt_flags &
1105 IEEE80211_MESHRT_FLAGS_PROXY) == 0,
1106 ("found mesh gate that is also marked porxy"));
1108 * convert route to a proxy route gated by the current
1109 * mesh gate, this is needed so encap can built data
1110 * frame with correct address.
1112 rt_dest->rt_flags = IEEE80211_MESHRT_FLAGS_PROXY |
1113 IEEE80211_MESHRT_FLAGS_VALID;
1114 rt_dest->rt_ext_seq = 1; /* random value */
1115 IEEE80211_ADDR_COPY(rt_dest->rt_mesh_gate, rt_gate->rt_dest);
1116 IEEE80211_ADDR_COPY(rt_dest->rt_nexthop, rt_gate->rt_nexthop);
1117 rt_dest->rt_metric = rt_gate->rt_metric;
1118 rt_dest->rt_nhops = rt_gate->rt_nhops;
1119 ieee80211_mesh_rt_update(rt_dest, ms->ms_ppath->mpp_inact);
1122 mcopy = m_dup(m, IEEE80211_M_NOWAIT);
1123 for (; mcopy != NULL; mcopy = next) {
1124 next = mcopy->m_nextpkt;
1125 mcopy->m_nextpkt = NULL;
1126 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_HWMP,
1128 "flush queued frame %p len %d", mcopy,
1129 mcopy->m_pkthdr.len);
1130 mesh_transmit_to_gate(vap, mcopy, rt_gate);
1134 rt_dest->rt_flags = 0; /* Mark invalid */
1140 * Forward the specified frame.
1141 * Decrement the TTL and set TA to our MAC address.
1144 mesh_forward(struct ieee80211vap *vap, struct mbuf *m,
1145 const struct ieee80211_meshcntl *mc)
1147 struct ieee80211com *ic = vap->iv_ic;
1148 struct ieee80211_mesh_state *ms = vap->iv_mesh;
1149 struct ifnet *ifp = vap->iv_ifp;
1150 const struct ieee80211_frame *wh =
1151 mtod(m, const struct ieee80211_frame *);
1153 struct ieee80211_meshcntl *mccopy;
1154 struct ieee80211_frame *whcopy;
1155 struct ieee80211_node *ni;
1158 /* This is called from the RX path - don't hold this lock */
1159 IEEE80211_TX_UNLOCK_ASSERT(ic);
1162 * mesh ttl of 1 means we are the last one receiving it,
1163 * according to amendment we decrement and then check if
1164 * 0, if so we dont forward.
1166 if (mc->mc_ttl < 1) {
1167 IEEE80211_NOTE_FRAME(vap, IEEE80211_MSG_MESH, wh,
1168 "%s", "frame not fwd'd, ttl 1");
1169 vap->iv_stats.is_mesh_fwd_ttl++;
1172 if (!(ms->ms_flags & IEEE80211_MESHFLAGS_FWD)) {
1173 IEEE80211_NOTE_FRAME(vap, IEEE80211_MSG_MESH, wh,
1174 "%s", "frame not fwd'd, fwding disabled");
1175 vap->iv_stats.is_mesh_fwd_disabled++;
1178 mcopy = m_dup(m, IEEE80211_M_NOWAIT);
1179 if (mcopy == NULL) {
1180 IEEE80211_NOTE_FRAME(vap, IEEE80211_MSG_MESH, wh,
1181 "%s", "frame not fwd'd, cannot dup");
1182 vap->iv_stats.is_mesh_fwd_nobuf++;
1183 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1186 mcopy = m_pullup(mcopy, ieee80211_hdrspace(ic, wh) +
1187 sizeof(struct ieee80211_meshcntl));
1188 if (mcopy == NULL) {
1189 IEEE80211_NOTE_FRAME(vap, IEEE80211_MSG_MESH, wh,
1190 "%s", "frame not fwd'd, too short");
1191 vap->iv_stats.is_mesh_fwd_tooshort++;
1192 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1196 whcopy = mtod(mcopy, struct ieee80211_frame *);
1197 mccopy = (struct ieee80211_meshcntl *)
1198 (mtod(mcopy, uint8_t *) + ieee80211_hdrspace(ic, wh));
1199 /* XXX clear other bits? */
1200 whcopy->i_fc[1] &= ~IEEE80211_FC1_RETRY;
1201 IEEE80211_ADDR_COPY(whcopy->i_addr2, vap->iv_myaddr);
1202 if (IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1203 ni = ieee80211_ref_node(vap->iv_bss);
1204 mcopy->m_flags |= M_MCAST;
1206 ni = ieee80211_mesh_find_txnode(vap, whcopy->i_addr3);
1209 * [Optional] any of the following three actions:
1210 * o silently discard
1211 * o trigger a path discovery
1212 * o inform TA that meshDA is unknown.
1214 IEEE80211_NOTE_FRAME(vap, IEEE80211_MSG_MESH, wh,
1215 "%s", "frame not fwd'd, no path");
1216 ms->ms_ppath->mpp_senderror(vap, whcopy->i_addr3, NULL,
1217 IEEE80211_REASON_MESH_PERR_NO_FI);
1218 vap->iv_stats.is_mesh_fwd_nopath++;
1222 IEEE80211_ADDR_COPY(whcopy->i_addr1, ni->ni_macaddr);
1224 KASSERT(mccopy->mc_ttl > 0, ("%s called with wrong ttl", __func__));
1227 /* XXX calculate priority so drivers can find the tx queue */
1228 M_WME_SETAC(mcopy, WME_AC_BE);
1230 /* XXX do we know m_nextpkt is NULL? */
1231 MPASS((mcopy->m_pkthdr.csum_flags & CSUM_SND_TAG) == 0);
1232 mcopy->m_pkthdr.rcvif = (void *) ni;
1235 * XXX this bypasses all of the VAP TX handling; it passes frames
1236 * directly to the parent interface.
1238 * Because of this, there's no TX lock being held as there's no
1239 * encaps state being used.
1241 * Doing a direct parent transmit may not be the correct thing
1242 * to do here; we'll have to re-think this soon.
1244 IEEE80211_TX_LOCK(ic);
1245 err = ieee80211_parent_xmitpkt(ic, mcopy);
1246 IEEE80211_TX_UNLOCK(ic);
1248 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
1251 static struct mbuf *
1252 mesh_decap(struct ieee80211vap *vap, struct mbuf *m, int hdrlen, int meshdrlen)
1254 #define WHDIR(wh) ((wh)->i_fc[1] & IEEE80211_FC1_DIR_MASK)
1255 #define MC01(mc) ((const struct ieee80211_meshcntl_ae01 *)mc)
1256 uint8_t b[sizeof(struct ieee80211_qosframe_addr4) +
1257 sizeof(struct ieee80211_meshcntl_ae10)];
1258 const struct ieee80211_qosframe_addr4 *wh;
1259 const struct ieee80211_meshcntl_ae10 *mc;
1260 struct ether_header *eh;
1264 if (m->m_len < hdrlen + sizeof(*llc) &&
1265 (m = m_pullup(m, hdrlen + sizeof(*llc))) == NULL) {
1266 IEEE80211_DPRINTF(vap, IEEE80211_MSG_ANY,
1267 "discard data frame: %s", "m_pullup failed");
1268 vap->iv_stats.is_rx_tooshort++;
1271 memcpy(b, mtod(m, caddr_t), hdrlen);
1272 wh = (const struct ieee80211_qosframe_addr4 *)&b[0];
1273 mc = (const struct ieee80211_meshcntl_ae10 *)&b[hdrlen - meshdrlen];
1274 KASSERT(WHDIR(wh) == IEEE80211_FC1_DIR_FROMDS ||
1275 WHDIR(wh) == IEEE80211_FC1_DIR_DSTODS,
1276 ("bogus dir, fc 0x%x:0x%x", wh->i_fc[0], wh->i_fc[1]));
1278 llc = (struct llc *)(mtod(m, caddr_t) + hdrlen);
1279 if (llc->llc_dsap == LLC_SNAP_LSAP && llc->llc_ssap == LLC_SNAP_LSAP &&
1280 llc->llc_control == LLC_UI && llc->llc_snap.org_code[0] == 0 &&
1281 llc->llc_snap.org_code[1] == 0 && llc->llc_snap.org_code[2] == 0 &&
1282 /* NB: preserve AppleTalk frames that have a native SNAP hdr */
1283 !(llc->llc_snap.ether_type == htons(ETHERTYPE_AARP) ||
1284 llc->llc_snap.ether_type == htons(ETHERTYPE_IPX))) {
1285 m_adj(m, hdrlen + sizeof(struct llc) - sizeof(*eh));
1288 m_adj(m, hdrlen - sizeof(*eh));
1290 eh = mtod(m, struct ether_header *);
1291 ae = mc->mc_flags & IEEE80211_MESH_AE_MASK;
1292 if (WHDIR(wh) == IEEE80211_FC1_DIR_FROMDS) {
1293 IEEE80211_ADDR_COPY(eh->ether_dhost, wh->i_addr1);
1294 if (ae == IEEE80211_MESH_AE_00) {
1295 IEEE80211_ADDR_COPY(eh->ether_shost, wh->i_addr3);
1296 } else if (ae == IEEE80211_MESH_AE_01) {
1297 IEEE80211_ADDR_COPY(eh->ether_shost,
1298 MC01(mc)->mc_addr4);
1300 IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
1301 (const struct ieee80211_frame *)wh, NULL,
1303 vap->iv_stats.is_mesh_badae++;
1308 if (ae == IEEE80211_MESH_AE_00) {
1309 IEEE80211_ADDR_COPY(eh->ether_dhost, wh->i_addr3);
1310 IEEE80211_ADDR_COPY(eh->ether_shost, wh->i_addr4);
1311 } else if (ae == IEEE80211_MESH_AE_10) {
1312 IEEE80211_ADDR_COPY(eh->ether_dhost, mc->mc_addr5);
1313 IEEE80211_ADDR_COPY(eh->ether_shost, mc->mc_addr6);
1315 IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
1316 (const struct ieee80211_frame *)wh, NULL,
1318 vap->iv_stats.is_mesh_badae++;
1323 #ifndef __NO_STRICT_ALIGNMENT
1324 if (!ALIGNED_POINTER(mtod(m, caddr_t) + sizeof(*eh), uint32_t)) {
1325 m = ieee80211_realign(vap, m, sizeof(*eh));
1329 #endif /* !__NO_STRICT_ALIGNMENT */
1331 eh = mtod(m, struct ether_header *);
1332 eh->ether_type = htons(m->m_pkthdr.len - sizeof(*eh));
1340 * Return non-zero if the unicast mesh data frame should be processed
1341 * locally. Frames that are not proxy'd have our address, otherwise
1342 * we need to consult the routing table to look for a proxy entry.
1345 mesh_isucastforme(struct ieee80211vap *vap, const struct ieee80211_frame *wh,
1346 const struct ieee80211_meshcntl *mc)
1348 int ae = mc->mc_flags & 3;
1350 KASSERT((wh->i_fc[1] & IEEE80211_FC1_DIR_MASK) == IEEE80211_FC1_DIR_DSTODS,
1351 ("bad dir 0x%x:0x%x", wh->i_fc[0], wh->i_fc[1]));
1352 KASSERT(ae == IEEE80211_MESH_AE_00 || ae == IEEE80211_MESH_AE_10,
1354 if (ae == IEEE80211_MESH_AE_10) { /* ucast w/ proxy */
1355 const struct ieee80211_meshcntl_ae10 *mc10 =
1356 (const struct ieee80211_meshcntl_ae10 *) mc;
1357 struct ieee80211_mesh_route *rt =
1358 ieee80211_mesh_rt_find(vap, mc10->mc_addr5);
1359 /* check for proxy route to ourself */
1360 return (rt != NULL &&
1361 (rt->rt_flags & IEEE80211_MESHRT_FLAGS_PROXY));
1362 } else /* ucast w/o proxy */
1363 return IEEE80211_ADDR_EQ(wh->i_addr3, vap->iv_myaddr);
1367 * Verifies transmitter, updates lifetime, precursor list and forwards data.
1368 * > 0 means we have forwarded data and no need to process locally
1369 * == 0 means we want to process locally (and we may have forwarded data
1370 * < 0 means there was an error and data should be discarded
1373 mesh_recv_indiv_data_to_fwrd(struct ieee80211vap *vap, struct mbuf *m,
1374 struct ieee80211_frame *wh, const struct ieee80211_meshcntl *mc)
1376 struct ieee80211_qosframe_addr4 *qwh;
1377 struct ieee80211_mesh_state *ms = vap->iv_mesh;
1378 struct ieee80211_mesh_route *rt_meshda, *rt_meshsa;
1380 /* This is called from the RX path - don't hold this lock */
1381 IEEE80211_TX_UNLOCK_ASSERT(vap->iv_ic);
1383 qwh = (struct ieee80211_qosframe_addr4 *)wh;
1387 * o verify addr2 is a legitimate transmitter
1388 * o lifetime of precursor of addr3 (addr2) is max(init, curr)
1389 * o lifetime of precursor of addr4 (nexthop) is max(init, curr)
1392 /* set lifetime of addr3 (meshDA) to initial value */
1393 rt_meshda = ieee80211_mesh_rt_find(vap, qwh->i_addr3);
1394 if (rt_meshda == NULL) {
1395 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH, qwh->i_addr2,
1396 "no route to meshDA(%6D)", qwh->i_addr3, ":");
1398 * [Optional] any of the following three actions:
1399 * o silently discard [X]
1400 * o trigger a path discovery [ ]
1401 * o inform TA that meshDA is unknown. [ ]
1407 ieee80211_mesh_rt_update(rt_meshda, ticks_to_msecs(
1408 ms->ms_ppath->mpp_inact));
1410 /* set lifetime of addr4 (meshSA) to initial value */
1411 rt_meshsa = ieee80211_mesh_rt_find(vap, qwh->i_addr4);
1412 KASSERT(rt_meshsa != NULL, ("no route"));
1413 ieee80211_mesh_rt_update(rt_meshsa, ticks_to_msecs(
1414 ms->ms_ppath->mpp_inact));
1416 mesh_forward(vap, m, mc);
1417 return (1); /* dont process locally */
1421 * Verifies transmitter, updates lifetime, precursor list and process data
1422 * locally, if data is proxy with AE = 10 it could mean data should go
1423 * on another mesh path or data should be forwarded to the DS.
1425 * > 0 means we have forwarded data and no need to process locally
1426 * == 0 means we want to process locally (and we may have forwarded data
1427 * < 0 means there was an error and data should be discarded
1430 mesh_recv_indiv_data_to_me(struct ieee80211vap *vap, struct mbuf *m,
1431 struct ieee80211_frame *wh, const struct ieee80211_meshcntl *mc)
1433 struct ieee80211_qosframe_addr4 *qwh;
1434 const struct ieee80211_meshcntl_ae10 *mc10;
1435 struct ieee80211_mesh_state *ms = vap->iv_mesh;
1436 struct ieee80211_mesh_route *rt;
1439 /* This is called from the RX path - don't hold this lock */
1440 IEEE80211_TX_UNLOCK_ASSERT(vap->iv_ic);
1442 qwh = (struct ieee80211_qosframe_addr4 *)wh;
1443 mc10 = (const struct ieee80211_meshcntl_ae10 *)mc;
1447 * o verify addr2 is a legitimate transmitter
1448 * o lifetime of precursor entry is max(init, curr)
1451 /* set lifetime of addr4 (meshSA) to initial value */
1452 rt = ieee80211_mesh_rt_find(vap, qwh->i_addr4);
1453 KASSERT(rt != NULL, ("no route"));
1454 ieee80211_mesh_rt_update(rt, ticks_to_msecs(ms->ms_ppath->mpp_inact));
1457 ae = mc10->mc_flags & IEEE80211_MESH_AE_MASK;
1458 KASSERT(ae == IEEE80211_MESH_AE_00 ||
1459 ae == IEEE80211_MESH_AE_10, ("bad AE %d", ae));
1460 if (ae == IEEE80211_MESH_AE_10) {
1461 if (IEEE80211_ADDR_EQ(mc10->mc_addr5, qwh->i_addr3)) {
1462 return (0); /* process locally */
1465 rt = ieee80211_mesh_rt_find(vap, mc10->mc_addr5);
1467 (rt->rt_flags & IEEE80211_MESHRT_FLAGS_VALID) &&
1468 (rt->rt_flags & IEEE80211_MESHRT_FLAGS_PROXY) == 0) {
1470 * Forward on another mesh-path, according to
1471 * amendment as specified in 9.32.4.1
1473 IEEE80211_ADDR_COPY(qwh->i_addr3, mc10->mc_addr5);
1474 mesh_forward(vap, m,
1475 (const struct ieee80211_meshcntl *)mc10);
1476 return (1); /* dont process locally */
1479 * All other cases: forward of MSDUs from the MBSS to DS indiv.
1480 * addressed according to 13.11.3.2.
1482 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_OUTPUT, qwh->i_addr2,
1483 "forward frame to DS, SA(%6D) DA(%6D)",
1484 mc10->mc_addr6, ":", mc10->mc_addr5, ":");
1486 return (0); /* process locally */
1490 * Try to forward the group addressed data on to other mesh STAs, and
1493 * > 0 means we have forwarded data and no need to process locally
1494 * == 0 means we want to process locally (and we may have forwarded data
1495 * < 0 means there was an error and data should be discarded
1498 mesh_recv_group_data(struct ieee80211vap *vap, struct mbuf *m,
1499 struct ieee80211_frame *wh, const struct ieee80211_meshcntl *mc)
1501 #define MC01(mc) ((const struct ieee80211_meshcntl_ae01 *)mc)
1502 struct ieee80211_mesh_state *ms = vap->iv_mesh;
1504 /* This is called from the RX path - don't hold this lock */
1505 IEEE80211_TX_UNLOCK_ASSERT(vap->iv_ic);
1507 mesh_forward(vap, m, mc);
1509 if(mc->mc_ttl > 0) {
1510 if (mc->mc_flags & IEEE80211_MESH_AE_01) {
1512 * Forward of MSDUs from the MBSS to DS group addressed
1513 * (according to 13.11.3.2)
1514 * This happens by delivering the packet, and a bridge
1515 * will sent it on another port member.
1517 if (ms->ms_flags & IEEE80211_MESHFLAGS_GATE &&
1518 ms->ms_flags & IEEE80211_MESHFLAGS_FWD) {
1519 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH,
1520 MC01(mc)->mc_addr4, "%s",
1521 "forward from MBSS to the DS");
1525 return (0); /* process locally */
1530 mesh_input(struct ieee80211_node *ni, struct mbuf *m,
1531 const struct ieee80211_rx_stats *rxs, int rssi, int nf)
1533 #define HAS_SEQ(type) ((type & 0x4) == 0)
1534 #define MC01(mc) ((const struct ieee80211_meshcntl_ae01 *)mc)
1535 struct ieee80211vap *vap = ni->ni_vap;
1536 struct ieee80211com *ic = ni->ni_ic;
1537 struct ifnet *ifp = vap->iv_ifp;
1538 struct ieee80211_frame *wh;
1539 const struct ieee80211_meshcntl *mc;
1540 int hdrspace, meshdrlen, need_tap, error;
1541 uint8_t dir, type, subtype, ae;
1543 const uint8_t *addr;
1546 KASSERT(ni != NULL, ("null node"));
1547 ni->ni_inact = ni->ni_inact_reload;
1549 need_tap = 1; /* mbuf need to be tapped. */
1550 type = -1; /* undefined */
1552 /* This is called from the RX path - don't hold this lock */
1553 IEEE80211_TX_UNLOCK_ASSERT(ic);
1555 if (m->m_pkthdr.len < sizeof(struct ieee80211_frame_min)) {
1556 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
1557 ni->ni_macaddr, NULL,
1558 "too short (1): len %u", m->m_pkthdr.len);
1559 vap->iv_stats.is_rx_tooshort++;
1563 * Bit of a cheat here, we use a pointer for a 3-address
1564 * frame format but don't reference fields past outside
1565 * ieee80211_frame_min w/o first validating the data is
1568 wh = mtod(m, struct ieee80211_frame *);
1570 if ((wh->i_fc[0] & IEEE80211_FC0_VERSION_MASK) !=
1571 IEEE80211_FC0_VERSION_0) {
1572 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
1573 ni->ni_macaddr, NULL, "wrong version %x", wh->i_fc[0]);
1574 vap->iv_stats.is_rx_badversion++;
1577 dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK;
1578 type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
1579 subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
1580 if ((ic->ic_flags & IEEE80211_F_SCAN) == 0) {
1581 IEEE80211_RSSI_LPF(ni->ni_avgrssi, rssi);
1583 if (HAS_SEQ(type)) {
1584 uint8_t tid = ieee80211_gettid(wh);
1586 if (IEEE80211_QOS_HAS_SEQ(wh) &&
1587 TID_TO_WME_AC(tid) >= WME_AC_VI)
1588 ic->ic_wme.wme_hipri_traffic++;
1589 if (! ieee80211_check_rxseq(ni, wh, wh->i_addr1, rxs))
1593 #ifdef IEEE80211_DEBUG
1595 * It's easier, but too expensive, to simulate different mesh
1596 * topologies by consulting the ACL policy very early, so do this
1599 * NB: this check is also done upon peering link initiation.
1601 if (vap->iv_acl != NULL && !vap->iv_acl->iac_check(vap, wh)) {
1602 IEEE80211_DISCARD(vap, IEEE80211_MSG_ACL,
1603 wh, NULL, "%s", "disallowed by ACL");
1604 vap->iv_stats.is_rx_acl++;
1609 case IEEE80211_FC0_TYPE_DATA:
1610 if (ni == vap->iv_bss)
1612 if (ni->ni_mlstate != IEEE80211_NODE_MESH_ESTABLISHED) {
1613 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_MESH,
1614 ni->ni_macaddr, NULL,
1615 "peer link not yet established (%d)",
1617 vap->iv_stats.is_mesh_nolink++;
1620 if (dir != IEEE80211_FC1_DIR_FROMDS &&
1621 dir != IEEE80211_FC1_DIR_DSTODS) {
1622 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
1623 wh, "data", "incorrect dir 0x%x", dir);
1624 vap->iv_stats.is_rx_wrongdir++;
1628 /* All Mesh data frames are QoS subtype */
1629 if (!HAS_SEQ(type)) {
1630 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
1631 wh, "data", "incorrect subtype 0x%x", subtype);
1632 vap->iv_stats.is_rx_badsubtype++;
1637 * Next up, any fragmentation.
1638 * XXX: we defrag before we even try to forward,
1639 * Mesh Control field is not present in sub-sequent
1640 * fragmented frames. This is in contrast to Draft 4.0.
1642 hdrspace = ieee80211_hdrspace(ic, wh);
1643 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1644 m = ieee80211_defrag(ni, m, hdrspace, 0);
1646 /* Fragment dropped or frame not complete yet */
1650 wh = mtod(m, struct ieee80211_frame *); /* NB: after defrag */
1653 * Now we have a complete Mesh Data frame.
1657 * Only fromDStoDS data frames use 4 address qos frames
1658 * as specified in amendment. Otherwise addr4 is located
1659 * in the Mesh Control field and a 3 address qos frame
1662 *(uint16_t *)qos = *(uint16_t *)ieee80211_getqos(wh);
1665 * NB: The mesh STA sets the Mesh Control Present
1666 * subfield to 1 in the Mesh Data frame containing
1667 * an unfragmented MSDU, an A-MSDU, or the first
1668 * fragment of an MSDU.
1669 * After defrag it should always be present.
1671 if (!(qos[1] & IEEE80211_QOS_MC)) {
1672 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_MESH,
1673 ni->ni_macaddr, NULL,
1674 "%s", "Mesh control field not present");
1675 vap->iv_stats.is_rx_elem_missing++; /* XXX: kinda */
1679 /* pull up enough to get to the mesh control */
1680 if (m->m_len < hdrspace + sizeof(struct ieee80211_meshcntl) &&
1681 (m = m_pullup(m, hdrspace +
1682 sizeof(struct ieee80211_meshcntl))) == NULL) {
1683 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
1684 ni->ni_macaddr, NULL,
1685 "data too short: expecting %u", hdrspace);
1686 vap->iv_stats.is_rx_tooshort++;
1690 * Now calculate the full extent of the headers. Note
1691 * mesh_decap will pull up anything we didn't get
1692 * above when it strips the 802.11 headers.
1694 mc = (const struct ieee80211_meshcntl *)
1695 (mtod(m, const uint8_t *) + hdrspace);
1696 ae = mc->mc_flags & IEEE80211_MESH_AE_MASK;
1697 meshdrlen = sizeof(struct ieee80211_meshcntl) +
1698 ae * IEEE80211_ADDR_LEN;
1699 hdrspace += meshdrlen;
1701 /* pull complete hdrspace = ieee80211_hdrspace + meshcontrol */
1702 if ((meshdrlen > sizeof(struct ieee80211_meshcntl)) &&
1703 (m->m_len < hdrspace) &&
1704 ((m = m_pullup(m, hdrspace)) == NULL)) {
1705 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
1706 ni->ni_macaddr, NULL,
1707 "data too short: expecting %u", hdrspace);
1708 vap->iv_stats.is_rx_tooshort++;
1711 /* XXX: are we sure there is no reallocating after m_pullup? */
1713 seq = le32dec(mc->mc_seq);
1714 if (IEEE80211_IS_MULTICAST(wh->i_addr1))
1716 else if (ae == IEEE80211_MESH_AE_01)
1717 addr = MC01(mc)->mc_addr4;
1719 addr = ((struct ieee80211_qosframe_addr4 *)wh)->i_addr4;
1720 if (IEEE80211_ADDR_EQ(vap->iv_myaddr, addr)) {
1721 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
1722 addr, "data", "%s", "not to me");
1723 vap->iv_stats.is_rx_wrongbss++; /* XXX kinda */
1726 if (mesh_checkpseq(vap, addr, seq) != 0) {
1727 vap->iv_stats.is_rx_dup++;
1731 /* This code "routes" the frame to the right control path */
1732 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1733 if (IEEE80211_ADDR_EQ(vap->iv_myaddr, wh->i_addr3))
1735 mesh_recv_indiv_data_to_me(vap, m, wh, mc);
1736 else if (IEEE80211_IS_MULTICAST(wh->i_addr3))
1737 error = mesh_recv_group_data(vap, m, wh, mc);
1739 error = mesh_recv_indiv_data_to_fwrd(vap, m,
1742 error = mesh_recv_group_data(vap, m, wh, mc);
1748 if (ieee80211_radiotap_active_vap(vap))
1749 ieee80211_radiotap_rx(vap, m);
1753 * Finally, strip the 802.11 header.
1755 m = mesh_decap(vap, m, hdrspace, meshdrlen);
1757 /* XXX mask bit to check for both */
1758 /* don't count Null data frames as errors */
1759 if (subtype == IEEE80211_FC0_SUBTYPE_NODATA ||
1760 subtype == IEEE80211_FC0_SUBTYPE_QOS_NULL)
1762 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
1763 ni->ni_macaddr, "data", "%s", "decap error");
1764 vap->iv_stats.is_rx_decap++;
1765 IEEE80211_NODE_STAT(ni, rx_decap);
1768 if (qos[0] & IEEE80211_QOS_AMSDU) {
1769 m = ieee80211_decap_amsdu(ni, m);
1771 return IEEE80211_FC0_TYPE_DATA;
1773 ieee80211_deliver_data(vap, ni, m);
1775 case IEEE80211_FC0_TYPE_MGT:
1776 vap->iv_stats.is_rx_mgmt++;
1777 IEEE80211_NODE_STAT(ni, rx_mgmt);
1778 if (dir != IEEE80211_FC1_DIR_NODS) {
1779 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
1780 wh, "mgt", "incorrect dir 0x%x", dir);
1781 vap->iv_stats.is_rx_wrongdir++;
1784 if (m->m_pkthdr.len < sizeof(struct ieee80211_frame)) {
1785 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
1786 ni->ni_macaddr, "mgt", "too short: len %u",
1788 vap->iv_stats.is_rx_tooshort++;
1791 #ifdef IEEE80211_DEBUG
1792 if ((ieee80211_msg_debug(vap) &&
1793 (vap->iv_ic->ic_flags & IEEE80211_F_SCAN)) ||
1794 ieee80211_msg_dumppkts(vap)) {
1795 if_printf(ifp, "received %s from %s rssi %d\n",
1796 ieee80211_mgt_subtype_name(subtype),
1797 ether_sprintf(wh->i_addr2), rssi);
1800 if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
1801 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
1802 wh, NULL, "%s", "WEP set but not permitted");
1803 vap->iv_stats.is_rx_mgtdiscard++; /* XXX */
1806 vap->iv_recv_mgmt(ni, m, subtype, rxs, rssi, nf);
1808 case IEEE80211_FC0_TYPE_CTL:
1809 vap->iv_stats.is_rx_ctl++;
1810 IEEE80211_NODE_STAT(ni, rx_ctrl);
1813 IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
1814 wh, "bad", "frame type 0x%x", type);
1815 /* should not come here */
1819 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
1822 if (need_tap && ieee80211_radiotap_active_vap(vap))
1823 ieee80211_radiotap_rx(vap, m);
1832 mesh_recv_mgmt(struct ieee80211_node *ni, struct mbuf *m0, int subtype,
1833 const struct ieee80211_rx_stats *rxs, int rssi, int nf)
1835 struct ieee80211vap *vap = ni->ni_vap;
1836 struct ieee80211_mesh_state *ms = vap->iv_mesh;
1837 struct ieee80211com *ic = ni->ni_ic;
1838 struct ieee80211_channel *rxchan = ic->ic_curchan;
1839 struct ieee80211_frame *wh;
1840 struct ieee80211_mesh_route *rt;
1841 uint8_t *frm, *efrm;
1843 wh = mtod(m0, struct ieee80211_frame *);
1844 frm = (uint8_t *)&wh[1];
1845 efrm = mtod(m0, uint8_t *) + m0->m_len;
1847 case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
1848 case IEEE80211_FC0_SUBTYPE_BEACON:
1850 struct ieee80211_scanparams scan;
1851 struct ieee80211_channel *c;
1853 * We process beacon/probe response
1854 * frames to discover neighbors.
1857 c = ieee80211_lookup_channel_rxstatus(vap, rxs);
1861 if (ieee80211_parse_beacon(ni, m0, rxchan, &scan) != 0)
1864 * Count frame now that we know it's to be processed.
1866 if (subtype == IEEE80211_FC0_SUBTYPE_BEACON) {
1867 vap->iv_stats.is_rx_beacon++; /* XXX remove */
1868 IEEE80211_NODE_STAT(ni, rx_beacons);
1870 IEEE80211_NODE_STAT(ni, rx_proberesp);
1872 * If scanning, just pass information to the scan module.
1874 if (ic->ic_flags & IEEE80211_F_SCAN) {
1875 if (ic->ic_flags_ext & IEEE80211_FEXT_PROBECHAN) {
1877 * Actively scanning a channel marked passive;
1878 * send a probe request now that we know there
1879 * is 802.11 traffic present.
1881 * XXX check if the beacon we recv'd gives
1882 * us what we need and suppress the probe req
1884 ieee80211_probe_curchan(vap, 1);
1885 ic->ic_flags_ext &= ~IEEE80211_FEXT_PROBECHAN;
1887 ieee80211_add_scan(vap, rxchan, &scan, wh,
1892 /* The rest of this code assumes we are running */
1893 if (vap->iv_state != IEEE80211_S_RUN)
1896 * Ignore non-mesh STAs.
1899 (IEEE80211_CAPINFO_ESS|IEEE80211_CAPINFO_IBSS)) ||
1900 scan.meshid == NULL || scan.meshconf == NULL) {
1901 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
1902 wh, "beacon", "%s", "not a mesh sta");
1903 vap->iv_stats.is_mesh_wrongmesh++;
1907 * Ignore STAs for other mesh networks.
1909 if (memcmp(scan.meshid+2, ms->ms_id, ms->ms_idlen) != 0 ||
1910 mesh_verify_meshconf(vap, scan.meshconf)) {
1911 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
1912 wh, "beacon", "%s", "not for our mesh");
1913 vap->iv_stats.is_mesh_wrongmesh++;
1917 * Peer only based on the current ACL policy.
1919 if (vap->iv_acl != NULL && !vap->iv_acl->iac_check(vap, wh)) {
1920 IEEE80211_DISCARD(vap, IEEE80211_MSG_ACL,
1921 wh, NULL, "%s", "disallowed by ACL");
1922 vap->iv_stats.is_rx_acl++;
1926 * Do neighbor discovery.
1928 if (!IEEE80211_ADDR_EQ(wh->i_addr2, ni->ni_macaddr)) {
1930 * Create a new entry in the neighbor table.
1932 ni = ieee80211_add_neighbor(vap, wh, &scan);
1935 * Automatically peer with discovered nodes if possible.
1937 if (ni != vap->iv_bss &&
1938 (ms->ms_flags & IEEE80211_MESHFLAGS_AP)) {
1939 switch (ni->ni_mlstate) {
1940 case IEEE80211_NODE_MESH_IDLE:
1944 /* Wait for backoff callout to reset counter */
1945 if (ni->ni_mlhcnt >= ieee80211_mesh_maxholding)
1948 ni->ni_mlpid = mesh_generateid(vap);
1949 if (ni->ni_mlpid == 0)
1951 mesh_linkchange(ni, IEEE80211_NODE_MESH_OPENSNT);
1952 args[0] = ni->ni_mlpid;
1953 ieee80211_send_action(ni,
1954 IEEE80211_ACTION_CAT_SELF_PROT,
1955 IEEE80211_ACTION_MESHPEERING_OPEN, args);
1957 mesh_peer_timeout_setup(ni);
1960 case IEEE80211_NODE_MESH_ESTABLISHED:
1963 * Valid beacon from a peer mesh STA
1966 rt = ieee80211_mesh_rt_find(vap, wh->i_addr2);
1968 ieee80211_mesh_rt_update(rt,
1970 ms->ms_ppath->mpp_inact));
1980 case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
1982 uint8_t *ssid, *meshid, *rates, *xrates;
1984 if (vap->iv_state != IEEE80211_S_RUN) {
1985 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
1986 wh, NULL, "wrong state %s",
1987 ieee80211_state_name[vap->iv_state]);
1988 vap->iv_stats.is_rx_mgtdiscard++;
1991 if (IEEE80211_IS_MULTICAST(wh->i_addr2)) {
1992 /* frame must be directed */
1993 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
1994 wh, NULL, "%s", "not unicast");
1995 vap->iv_stats.is_rx_mgtdiscard++; /* XXX stat */
1999 * prreq frame format
2001 * [tlv] supported rates
2002 * [tlv] extended supported rates
2005 ssid = meshid = rates = xrates = NULL;
2006 while (efrm - frm > 1) {
2007 IEEE80211_VERIFY_LENGTH(efrm - frm, frm[1] + 2, return);
2009 case IEEE80211_ELEMID_SSID:
2012 case IEEE80211_ELEMID_RATES:
2015 case IEEE80211_ELEMID_XRATES:
2018 case IEEE80211_ELEMID_MESHID:
2024 IEEE80211_VERIFY_ELEMENT(ssid, IEEE80211_NWID_LEN, return);
2025 IEEE80211_VERIFY_ELEMENT(rates, IEEE80211_RATE_MAXSIZE, return);
2027 IEEE80211_VERIFY_ELEMENT(xrates,
2028 IEEE80211_RATE_MAXSIZE - rates[1], return);
2029 if (meshid != NULL) {
2030 IEEE80211_VERIFY_ELEMENT(meshid,
2031 IEEE80211_MESHID_LEN, return);
2032 /* NB: meshid, not ssid */
2033 IEEE80211_VERIFY_SSID(vap->iv_bss, meshid, return);
2036 /* XXX find a better class or define it's own */
2037 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_INPUT, wh->i_addr2,
2038 "%s", "recv probe req");
2040 * Some legacy 11b clients cannot hack a complete
2041 * probe response frame. When the request includes
2042 * only a bare-bones rate set, communicate this to
2043 * the transmit side.
2045 ieee80211_send_proberesp(vap, wh->i_addr2, 0);
2049 case IEEE80211_FC0_SUBTYPE_ACTION:
2050 case IEEE80211_FC0_SUBTYPE_ACTION_NOACK:
2051 if (ni == vap->iv_bss) {
2052 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
2053 wh, NULL, "%s", "unknown node");
2054 vap->iv_stats.is_rx_mgtdiscard++;
2055 } else if (!IEEE80211_ADDR_EQ(vap->iv_myaddr, wh->i_addr1) &&
2056 !IEEE80211_IS_MULTICAST(wh->i_addr1)) {
2057 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
2058 wh, NULL, "%s", "not for us");
2059 vap->iv_stats.is_rx_mgtdiscard++;
2060 } else if (vap->iv_state != IEEE80211_S_RUN) {
2061 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
2062 wh, NULL, "wrong state %s",
2063 ieee80211_state_name[vap->iv_state]);
2064 vap->iv_stats.is_rx_mgtdiscard++;
2066 if (ieee80211_parse_action(ni, m0) == 0)
2067 (void)ic->ic_recv_action(ni, wh, frm, efrm);
2071 case IEEE80211_FC0_SUBTYPE_ASSOC_REQ:
2072 case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
2073 case IEEE80211_FC0_SUBTYPE_REASSOC_REQ:
2074 case IEEE80211_FC0_SUBTYPE_REASSOC_RESP:
2075 case IEEE80211_FC0_SUBTYPE_TIMING_ADV:
2076 case IEEE80211_FC0_SUBTYPE_ATIM:
2077 case IEEE80211_FC0_SUBTYPE_DISASSOC:
2078 case IEEE80211_FC0_SUBTYPE_AUTH:
2079 case IEEE80211_FC0_SUBTYPE_DEAUTH:
2080 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
2081 wh, NULL, "%s", "not handled");
2082 vap->iv_stats.is_rx_mgtdiscard++;
2086 IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
2087 wh, "mgt", "subtype 0x%x not handled", subtype);
2088 vap->iv_stats.is_rx_badsubtype++;
2094 mesh_recv_ctl(struct ieee80211_node *ni, struct mbuf *m, int subtype)
2098 case IEEE80211_FC0_SUBTYPE_BAR:
2099 ieee80211_recv_bar(ni, m);
2105 * Parse meshpeering action ie's for MPM frames
2107 static const struct ieee80211_meshpeer_ie *
2108 mesh_parse_meshpeering_action(struct ieee80211_node *ni,
2109 const struct ieee80211_frame *wh, /* XXX for VERIFY_LENGTH */
2110 const uint8_t *frm, const uint8_t *efrm,
2111 struct ieee80211_meshpeer_ie *mp, uint8_t subtype)
2113 struct ieee80211vap *vap = ni->ni_vap;
2114 const struct ieee80211_meshpeer_ie *mpie;
2116 const uint8_t *meshid, *meshconf;
2117 uint8_t sendclose = 0; /* 1 = MPM frame rejected, close will be sent */
2119 meshid = meshconf = NULL;
2120 while (efrm - frm > 1) {
2121 IEEE80211_VERIFY_LENGTH(efrm - frm, frm[1] + 2, return NULL);
2123 case IEEE80211_ELEMID_MESHID:
2126 case IEEE80211_ELEMID_MESHCONF:
2129 case IEEE80211_ELEMID_MESHPEER:
2130 mpie = (const struct ieee80211_meshpeer_ie *) frm;
2131 memset(mp, 0, sizeof(*mp));
2132 mp->peer_len = mpie->peer_len;
2133 mp->peer_proto = le16dec(&mpie->peer_proto);
2134 mp->peer_llinkid = le16dec(&mpie->peer_llinkid);
2136 case IEEE80211_ACTION_MESHPEERING_CONFIRM:
2138 le16dec(&mpie->peer_linkid);
2140 case IEEE80211_ACTION_MESHPEERING_CLOSE:
2141 /* NB: peer link ID is optional */
2142 if (mpie->peer_len ==
2143 (IEEE80211_MPM_BASE_SZ + 2)) {
2144 mp->peer_linkid = 0;
2146 le16dec(&mpie->peer_linkid);
2149 le16dec(&mpie->peer_linkid);
2151 le16dec(&mpie->peer_rcode);
2161 * Verify the contents of the frame.
2162 * If it fails validation, close the peer link.
2164 if (mesh_verify_meshpeer(vap, subtype, (const uint8_t *)mp)) {
2166 IEEE80211_DISCARD(vap,
2167 IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH,
2168 wh, NULL, "%s", "MPM validation failed");
2171 /* If meshid is not the same reject any frames type. */
2172 if (sendclose == 0 && mesh_verify_meshid(vap, meshid)) {
2174 IEEE80211_DISCARD(vap,
2175 IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH,
2176 wh, NULL, "%s", "not for our mesh");
2177 if (subtype == IEEE80211_ACTION_MESHPEERING_CLOSE) {
2179 * Standard not clear about this, if we dont ignore
2180 * there will be an endless loop between nodes sending
2181 * CLOSE frames between each other with wrong meshid.
2182 * Discard and timers will bring FSM to IDLE state.
2189 * Close frames are accepted if meshid is the same.
2190 * Verify the other two types.
2192 if (sendclose == 0 && subtype != IEEE80211_ACTION_MESHPEERING_CLOSE &&
2193 mesh_verify_meshconf(vap, meshconf)) {
2195 IEEE80211_DISCARD(vap,
2196 IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH,
2197 wh, NULL, "%s", "configuration mismatch");
2201 vap->iv_stats.is_rx_mgtdiscard++;
2202 switch (ni->ni_mlstate) {
2203 case IEEE80211_NODE_MESH_IDLE:
2204 case IEEE80211_NODE_MESH_ESTABLISHED:
2205 case IEEE80211_NODE_MESH_HOLDING:
2208 case IEEE80211_NODE_MESH_OPENSNT:
2209 case IEEE80211_NODE_MESH_OPENRCV:
2210 case IEEE80211_NODE_MESH_CONFIRMRCV:
2211 args[0] = ni->ni_mlpid;
2212 args[1] = ni->ni_mllid;
2213 /* Reason codes for rejection */
2215 case IEEE80211_ACTION_MESHPEERING_OPEN:
2216 args[2] = IEEE80211_REASON_MESH_CPVIOLATION;
2218 case IEEE80211_ACTION_MESHPEERING_CONFIRM:
2219 args[2] = IEEE80211_REASON_MESH_INCONS_PARAMS;
2222 ieee80211_send_action(ni,
2223 IEEE80211_ACTION_CAT_SELF_PROT,
2224 IEEE80211_ACTION_MESHPEERING_CLOSE,
2226 mesh_linkchange(ni, IEEE80211_NODE_MESH_HOLDING);
2227 mesh_peer_timeout_setup(ni);
2233 return (const struct ieee80211_meshpeer_ie *) mp;
2237 mesh_recv_action_meshpeering_open(struct ieee80211_node *ni,
2238 const struct ieee80211_frame *wh,
2239 const uint8_t *frm, const uint8_t *efrm)
2241 struct ieee80211vap *vap = ni->ni_vap;
2242 struct ieee80211_mesh_state *ms = vap->iv_mesh;
2243 struct ieee80211_meshpeer_ie ie;
2244 const struct ieee80211_meshpeer_ie *meshpeer;
2247 /* +2+2 for action + code + capabilites */
2248 meshpeer = mesh_parse_meshpeering_action(ni, wh, frm+2+2, efrm, &ie,
2249 IEEE80211_ACTION_MESHPEERING_OPEN);
2250 if (meshpeer == NULL) {
2255 IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH, ni,
2256 "recv PEER OPEN, lid 0x%x", meshpeer->peer_llinkid);
2258 switch (ni->ni_mlstate) {
2259 case IEEE80211_NODE_MESH_IDLE:
2260 /* Reject open request if reached our maximum neighbor count */
2261 if (ms->ms_neighbors >= IEEE80211_MESH_MAX_NEIGHBORS) {
2262 args[0] = meshpeer->peer_llinkid;
2264 args[2] = IEEE80211_REASON_MESH_MAX_PEERS;
2265 ieee80211_send_action(ni,
2266 IEEE80211_ACTION_CAT_SELF_PROT,
2267 IEEE80211_ACTION_MESHPEERING_CLOSE,
2269 /* stay in IDLE state */
2272 /* Open frame accepted */
2273 mesh_linkchange(ni, IEEE80211_NODE_MESH_OPENRCV);
2274 ni->ni_mllid = meshpeer->peer_llinkid;
2275 ni->ni_mlpid = mesh_generateid(vap);
2276 if (ni->ni_mlpid == 0)
2278 args[0] = ni->ni_mlpid;
2279 /* Announce we're open too... */
2280 ieee80211_send_action(ni,
2281 IEEE80211_ACTION_CAT_SELF_PROT,
2282 IEEE80211_ACTION_MESHPEERING_OPEN, args);
2283 /* ...and confirm the link. */
2284 args[0] = ni->ni_mlpid;
2285 args[1] = ni->ni_mllid;
2286 ieee80211_send_action(ni,
2287 IEEE80211_ACTION_CAT_SELF_PROT,
2288 IEEE80211_ACTION_MESHPEERING_CONFIRM,
2290 mesh_peer_timeout_setup(ni);
2292 case IEEE80211_NODE_MESH_OPENRCV:
2294 if (ni->ni_mllid != meshpeer->peer_llinkid) {
2295 args[0] = ni->ni_mllid;
2296 args[1] = ni->ni_mlpid;
2297 args[2] = IEEE80211_REASON_PEER_LINK_CANCELED;
2298 ieee80211_send_action(ni,
2299 IEEE80211_ACTION_CAT_SELF_PROT,
2300 IEEE80211_ACTION_MESHPEERING_CLOSE,
2302 mesh_linkchange(ni, IEEE80211_NODE_MESH_HOLDING);
2303 mesh_peer_timeout_setup(ni);
2306 /* Duplicate open, confirm again. */
2307 args[0] = ni->ni_mlpid;
2308 args[1] = ni->ni_mllid;
2309 ieee80211_send_action(ni,
2310 IEEE80211_ACTION_CAT_SELF_PROT,
2311 IEEE80211_ACTION_MESHPEERING_CONFIRM,
2314 case IEEE80211_NODE_MESH_OPENSNT:
2315 ni->ni_mllid = meshpeer->peer_llinkid;
2316 mesh_linkchange(ni, IEEE80211_NODE_MESH_OPENRCV);
2317 args[0] = ni->ni_mlpid;
2318 args[1] = ni->ni_mllid;
2319 ieee80211_send_action(ni,
2320 IEEE80211_ACTION_CAT_SELF_PROT,
2321 IEEE80211_ACTION_MESHPEERING_CONFIRM,
2323 /* NB: don't setup/clear any timeout */
2325 case IEEE80211_NODE_MESH_CONFIRMRCV:
2326 if (ni->ni_mlpid != meshpeer->peer_linkid ||
2327 ni->ni_mllid != meshpeer->peer_llinkid) {
2328 args[0] = ni->ni_mlpid;
2329 args[1] = ni->ni_mllid;
2330 args[2] = IEEE80211_REASON_PEER_LINK_CANCELED;
2331 ieee80211_send_action(ni,
2332 IEEE80211_ACTION_CAT_SELF_PROT,
2333 IEEE80211_ACTION_MESHPEERING_CLOSE,
2336 IEEE80211_NODE_MESH_HOLDING);
2337 mesh_peer_timeout_setup(ni);
2340 mesh_linkchange(ni, IEEE80211_NODE_MESH_ESTABLISHED);
2341 ni->ni_mllid = meshpeer->peer_llinkid;
2342 args[0] = ni->ni_mlpid;
2343 args[1] = ni->ni_mllid;
2344 ieee80211_send_action(ni,
2345 IEEE80211_ACTION_CAT_SELF_PROT,
2346 IEEE80211_ACTION_MESHPEERING_CONFIRM,
2348 mesh_peer_timeout_stop(ni);
2350 case IEEE80211_NODE_MESH_ESTABLISHED:
2351 if (ni->ni_mllid != meshpeer->peer_llinkid) {
2352 args[0] = ni->ni_mllid;
2353 args[1] = ni->ni_mlpid;
2354 args[2] = IEEE80211_REASON_PEER_LINK_CANCELED;
2355 ieee80211_send_action(ni,
2356 IEEE80211_ACTION_CAT_SELF_PROT,
2357 IEEE80211_ACTION_MESHPEERING_CLOSE,
2359 mesh_linkchange(ni, IEEE80211_NODE_MESH_HOLDING);
2360 mesh_peer_timeout_setup(ni);
2363 args[0] = ni->ni_mlpid;
2364 args[1] = ni->ni_mllid;
2365 ieee80211_send_action(ni,
2366 IEEE80211_ACTION_CAT_SELF_PROT,
2367 IEEE80211_ACTION_MESHPEERING_CONFIRM,
2370 case IEEE80211_NODE_MESH_HOLDING:
2371 args[0] = ni->ni_mlpid;
2372 args[1] = meshpeer->peer_llinkid;
2373 /* Standard not clear about what the reaason code should be */
2374 args[2] = IEEE80211_REASON_PEER_LINK_CANCELED;
2375 ieee80211_send_action(ni,
2376 IEEE80211_ACTION_CAT_SELF_PROT,
2377 IEEE80211_ACTION_MESHPEERING_CLOSE,
2385 mesh_recv_action_meshpeering_confirm(struct ieee80211_node *ni,
2386 const struct ieee80211_frame *wh,
2387 const uint8_t *frm, const uint8_t *efrm)
2389 struct ieee80211vap *vap = ni->ni_vap;
2390 struct ieee80211_meshpeer_ie ie;
2391 const struct ieee80211_meshpeer_ie *meshpeer;
2394 /* +2+2+2+2 for action + code + capabilites + status code + AID */
2395 meshpeer = mesh_parse_meshpeering_action(ni, wh, frm+2+2+2+2, efrm, &ie,
2396 IEEE80211_ACTION_MESHPEERING_CONFIRM);
2397 if (meshpeer == NULL) {
2401 IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH, ni,
2402 "recv PEER CONFIRM, local id 0x%x, peer id 0x%x",
2403 meshpeer->peer_llinkid, meshpeer->peer_linkid);
2405 switch (ni->ni_mlstate) {
2406 case IEEE80211_NODE_MESH_OPENRCV:
2407 mesh_linkchange(ni, IEEE80211_NODE_MESH_ESTABLISHED);
2408 mesh_peer_timeout_stop(ni);
2410 case IEEE80211_NODE_MESH_OPENSNT:
2411 mesh_linkchange(ni, IEEE80211_NODE_MESH_CONFIRMRCV);
2412 mesh_peer_timeout_setup(ni);
2414 case IEEE80211_NODE_MESH_HOLDING:
2415 args[0] = ni->ni_mlpid;
2416 args[1] = meshpeer->peer_llinkid;
2417 /* Standard not clear about what the reaason code should be */
2418 args[2] = IEEE80211_REASON_PEER_LINK_CANCELED;
2419 ieee80211_send_action(ni,
2420 IEEE80211_ACTION_CAT_SELF_PROT,
2421 IEEE80211_ACTION_MESHPEERING_CLOSE,
2424 case IEEE80211_NODE_MESH_CONFIRMRCV:
2425 if (ni->ni_mllid != meshpeer->peer_llinkid) {
2426 args[0] = ni->ni_mlpid;
2427 args[1] = ni->ni_mllid;
2428 args[2] = IEEE80211_REASON_PEER_LINK_CANCELED;
2429 ieee80211_send_action(ni,
2430 IEEE80211_ACTION_CAT_SELF_PROT,
2431 IEEE80211_ACTION_MESHPEERING_CLOSE,
2433 mesh_linkchange(ni, IEEE80211_NODE_MESH_HOLDING);
2434 mesh_peer_timeout_setup(ni);
2438 IEEE80211_DISCARD(vap,
2439 IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH,
2440 wh, NULL, "received confirm in invalid state %d",
2442 vap->iv_stats.is_rx_mgtdiscard++;
2449 mesh_recv_action_meshpeering_close(struct ieee80211_node *ni,
2450 const struct ieee80211_frame *wh,
2451 const uint8_t *frm, const uint8_t *efrm)
2453 struct ieee80211_meshpeer_ie ie;
2454 const struct ieee80211_meshpeer_ie *meshpeer;
2457 /* +2 for action + code */
2458 meshpeer = mesh_parse_meshpeering_action(ni, wh, frm+2, efrm, &ie,
2459 IEEE80211_ACTION_MESHPEERING_CLOSE);
2460 if (meshpeer == NULL) {
2465 * XXX: check reason code, for example we could receive
2466 * IEEE80211_REASON_MESH_MAX_PEERS then we should not attempt
2470 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH,
2471 ni, "%s", "recv PEER CLOSE");
2473 switch (ni->ni_mlstate) {
2474 case IEEE80211_NODE_MESH_IDLE:
2477 case IEEE80211_NODE_MESH_OPENRCV:
2478 case IEEE80211_NODE_MESH_OPENSNT:
2479 case IEEE80211_NODE_MESH_CONFIRMRCV:
2480 case IEEE80211_NODE_MESH_ESTABLISHED:
2481 args[0] = ni->ni_mlpid;
2482 args[1] = ni->ni_mllid;
2483 args[2] = IEEE80211_REASON_MESH_CLOSE_RCVD;
2484 ieee80211_send_action(ni,
2485 IEEE80211_ACTION_CAT_SELF_PROT,
2486 IEEE80211_ACTION_MESHPEERING_CLOSE,
2488 mesh_linkchange(ni, IEEE80211_NODE_MESH_HOLDING);
2489 mesh_peer_timeout_setup(ni);
2491 case IEEE80211_NODE_MESH_HOLDING:
2492 mesh_linkchange(ni, IEEE80211_NODE_MESH_IDLE);
2493 mesh_peer_timeout_stop(ni);
2500 * Link Metric handling.
2503 mesh_recv_action_meshlmetric(struct ieee80211_node *ni,
2504 const struct ieee80211_frame *wh,
2505 const uint8_t *frm, const uint8_t *efrm)
2507 const struct ieee80211_meshlmetric_ie *ie =
2508 (const struct ieee80211_meshlmetric_ie *)
2509 (frm+2); /* action + code */
2510 struct ieee80211_meshlmetric_ie lm_rep;
2512 if (ie->lm_flags & IEEE80211_MESH_LMETRIC_FLAGS_REQ) {
2513 lm_rep.lm_flags = 0;
2514 lm_rep.lm_metric = mesh_airtime_calc(ni);
2515 ieee80211_send_action(ni,
2516 IEEE80211_ACTION_CAT_MESH,
2517 IEEE80211_ACTION_MESH_LMETRIC,
2520 /* XXX: else do nothing for now */
2525 * Parse meshgate action ie's for GANN frames.
2526 * Returns -1 if parsing fails, otherwise 0.
2529 mesh_parse_meshgate_action(struct ieee80211_node *ni,
2530 const struct ieee80211_frame *wh, /* XXX for VERIFY_LENGTH */
2531 struct ieee80211_meshgann_ie *ie, const uint8_t *frm, const uint8_t *efrm)
2533 struct ieee80211vap *vap = ni->ni_vap;
2534 const struct ieee80211_meshgann_ie *gannie;
2536 while (efrm - frm > 1) {
2537 IEEE80211_VERIFY_LENGTH(efrm - frm, frm[1] + 2, return -1);
2539 case IEEE80211_ELEMID_MESHGANN:
2540 gannie = (const struct ieee80211_meshgann_ie *) frm;
2541 memset(ie, 0, sizeof(*ie));
2542 ie->gann_ie = gannie->gann_ie;
2543 ie->gann_len = gannie->gann_len;
2544 ie->gann_flags = gannie->gann_flags;
2545 ie->gann_hopcount = gannie->gann_hopcount;
2546 ie->gann_ttl = gannie->gann_ttl;
2547 IEEE80211_ADDR_COPY(ie->gann_addr, gannie->gann_addr);
2548 ie->gann_seq = le32dec(&gannie->gann_seq);
2549 ie->gann_interval = le16dec(&gannie->gann_interval);
2559 * Mesh Gate Announcement handling.
2562 mesh_recv_action_meshgate(struct ieee80211_node *ni,
2563 const struct ieee80211_frame *wh,
2564 const uint8_t *frm, const uint8_t *efrm)
2566 struct ieee80211vap *vap = ni->ni_vap;
2567 struct ieee80211_mesh_state *ms = vap->iv_mesh;
2568 struct ieee80211_mesh_gate_route *gr, *next;
2569 struct ieee80211_mesh_route *rt_gate;
2570 struct ieee80211_meshgann_ie pgann;
2571 struct ieee80211_meshgann_ie ie;
2574 /* +2 for action + code */
2575 if (mesh_parse_meshgate_action(ni, wh, &ie, frm+2, efrm) != 0) {
2576 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_MESH,
2577 ni->ni_macaddr, NULL, "%s",
2578 "GANN parsing failed");
2579 vap->iv_stats.is_rx_mgtdiscard++;
2583 if (IEEE80211_ADDR_EQ(vap->iv_myaddr, ie.gann_addr))
2586 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH, ni->ni_macaddr,
2587 "received GANN, meshgate: %6D (seq %u)", ie.gann_addr, ":",
2593 TAILQ_FOREACH_SAFE(gr, &ms->ms_known_gates, gr_next, next) {
2594 if (!IEEE80211_ADDR_EQ(gr->gr_addr, ie.gann_addr))
2596 if (ie.gann_seq <= gr->gr_lastseq) {
2597 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_MESH,
2598 ni->ni_macaddr, NULL,
2599 "GANN old seqno %u <= %u",
2600 ie.gann_seq, gr->gr_lastseq);
2604 /* corresponding mesh gate found & GANN accepted */
2609 /* this GANN is from a new mesh Gate add it to known table. */
2610 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH, ie.gann_addr,
2611 "stored new GANN information, seq %u.", ie.gann_seq);
2612 gr = IEEE80211_MALLOC(ALIGN(sizeof(struct ieee80211_mesh_gate_route)),
2614 IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
2615 IEEE80211_ADDR_COPY(gr->gr_addr, ie.gann_addr);
2616 TAILQ_INSERT_TAIL(&ms->ms_known_gates, gr, gr_next);
2618 gr->gr_lastseq = ie.gann_seq;
2620 /* check if we have a path to this gate */
2621 rt_gate = mesh_rt_find_locked(ms, gr->gr_addr);
2622 if (rt_gate != NULL &&
2623 rt_gate->rt_flags & IEEE80211_MESHRT_FLAGS_VALID) {
2624 gr->gr_route = rt_gate;
2625 rt_gate->rt_flags |= IEEE80211_MESHRT_FLAGS_GATE;
2630 /* popagate only if decremented ttl >= 1 && forwarding is enabled */
2631 if ((ie.gann_ttl - 1) < 1 && !(ms->ms_flags & IEEE80211_MESHFLAGS_FWD))
2633 pgann.gann_flags = ie.gann_flags; /* Reserved */
2634 pgann.gann_hopcount = ie.gann_hopcount + 1;
2635 pgann.gann_ttl = ie.gann_ttl - 1;
2636 IEEE80211_ADDR_COPY(pgann.gann_addr, ie.gann_addr);
2637 pgann.gann_seq = ie.gann_seq;
2638 pgann.gann_interval = ie.gann_interval;
2640 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH, ie.gann_addr,
2641 "%s", "propagate GANN");
2643 ieee80211_send_action(vap->iv_bss, IEEE80211_ACTION_CAT_MESH,
2644 IEEE80211_ACTION_MESH_GANN, &pgann);
2650 mesh_send_action(struct ieee80211_node *ni,
2651 const uint8_t sa[IEEE80211_ADDR_LEN],
2652 const uint8_t da[IEEE80211_ADDR_LEN],
2655 struct ieee80211vap *vap = ni->ni_vap;
2656 struct ieee80211com *ic = ni->ni_ic;
2657 struct ieee80211_bpf_params params;
2660 KASSERT(ni != NULL, ("null node"));
2662 if (vap->iv_state == IEEE80211_S_CAC) {
2663 IEEE80211_NOTE(vap, IEEE80211_MSG_OUTPUT, ni,
2664 "block %s frame in CAC state", "Mesh action");
2665 vap->iv_stats.is_tx_badstate++;
2666 ieee80211_free_node(ni);
2668 return EIO; /* XXX */
2671 M_PREPEND(m, sizeof(struct ieee80211_frame), IEEE80211_M_NOWAIT);
2673 ieee80211_free_node(ni);
2677 IEEE80211_TX_LOCK(ic);
2678 ieee80211_send_setup(ni, m,
2679 IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_ACTION,
2680 IEEE80211_NONQOS_TID, sa, da, sa);
2681 m->m_flags |= M_ENCAP; /* mark encapsulated */
2683 memset(¶ms, 0, sizeof(params));
2684 params.ibp_pri = WME_AC_VO;
2685 params.ibp_rate0 = ni->ni_txparms->mgmtrate;
2686 if (IEEE80211_IS_MULTICAST(da))
2687 params.ibp_try0 = 1;
2689 params.ibp_try0 = ni->ni_txparms->maxretry;
2690 params.ibp_power = ni->ni_txpower;
2692 IEEE80211_NODE_STAT(ni, tx_mgmt);
2694 ret = ieee80211_raw_output(vap, ni, m, ¶ms);
2695 IEEE80211_TX_UNLOCK(ic);
2699 #define ADDSHORT(frm, v) do { \
2700 frm[0] = (v) & 0xff; \
2701 frm[1] = (v) >> 8; \
2704 #define ADDWORD(frm, v) do { \
2705 frm[0] = (v) & 0xff; \
2706 frm[1] = ((v) >> 8) & 0xff; \
2707 frm[2] = ((v) >> 16) & 0xff; \
2708 frm[3] = ((v) >> 24) & 0xff; \
2713 mesh_send_action_meshpeering_open(struct ieee80211_node *ni,
2714 int category, int action, void *args0)
2716 struct ieee80211vap *vap = ni->ni_vap;
2717 struct ieee80211com *ic = ni->ni_ic;
2718 uint16_t *args = args0;
2719 const struct ieee80211_rateset *rs;
2723 IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH, ni,
2724 "send PEER OPEN action: localid 0x%x", args[0]);
2726 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
2727 "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", __func__, __LINE__,
2728 ni, ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)+1);
2729 ieee80211_ref_node(ni);
2731 m = ieee80211_getmgtframe(&frm,
2732 ic->ic_headroom + sizeof(struct ieee80211_frame),
2733 sizeof(uint16_t) /* action+category */
2734 + sizeof(uint16_t) /* capabilites */
2735 + 2 + IEEE80211_RATE_SIZE
2736 + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
2737 + 2 + IEEE80211_MESHID_LEN
2738 + sizeof(struct ieee80211_meshconf_ie)
2739 + sizeof(struct ieee80211_meshpeer_ie)
2743 * mesh peer open action frame format:
2751 * [tlv] mesh peer link mgmt
2755 ADDSHORT(frm, ieee80211_getcapinfo(vap, ni->ni_chan));
2756 rs = ieee80211_get_suprates(ic, ic->ic_curchan);
2757 frm = ieee80211_add_rates(frm, rs);
2758 frm = ieee80211_add_xrates(frm, rs);
2759 frm = ieee80211_add_meshid(frm, vap);
2760 frm = ieee80211_add_meshconf(frm, vap);
2761 frm = ieee80211_add_meshpeer(frm, IEEE80211_ACTION_MESHPEERING_OPEN,
2763 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2764 return mesh_send_action(ni, vap->iv_myaddr, ni->ni_macaddr, m);
2766 vap->iv_stats.is_tx_nobuf++;
2767 ieee80211_free_node(ni);
2773 mesh_send_action_meshpeering_confirm(struct ieee80211_node *ni,
2774 int category, int action, void *args0)
2776 struct ieee80211vap *vap = ni->ni_vap;
2777 struct ieee80211com *ic = ni->ni_ic;
2778 uint16_t *args = args0;
2779 const struct ieee80211_rateset *rs;
2783 IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH, ni,
2784 "send PEER CONFIRM action: localid 0x%x, peerid 0x%x",
2787 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
2788 "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", __func__, __LINE__,
2789 ni, ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)+1);
2790 ieee80211_ref_node(ni);
2792 m = ieee80211_getmgtframe(&frm,
2793 ic->ic_headroom + sizeof(struct ieee80211_frame),
2794 sizeof(uint16_t) /* action+category */
2795 + sizeof(uint16_t) /* capabilites */
2796 + sizeof(uint16_t) /* status code */
2797 + sizeof(uint16_t) /* AID */
2798 + 2 + IEEE80211_RATE_SIZE
2799 + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
2800 + 2 + IEEE80211_MESHID_LEN
2801 + sizeof(struct ieee80211_meshconf_ie)
2802 + sizeof(struct ieee80211_meshpeer_ie)
2806 * mesh peer confirm action frame format:
2811 * [2] association id (peer ID)
2816 * [tlv] mesh peer link mgmt
2820 ADDSHORT(frm, ieee80211_getcapinfo(vap, ni->ni_chan));
2821 ADDSHORT(frm, 0); /* status code */
2822 ADDSHORT(frm, args[1]); /* AID */
2823 rs = ieee80211_get_suprates(ic, ic->ic_curchan);
2824 frm = ieee80211_add_rates(frm, rs);
2825 frm = ieee80211_add_xrates(frm, rs);
2826 frm = ieee80211_add_meshid(frm, vap);
2827 frm = ieee80211_add_meshconf(frm, vap);
2828 frm = ieee80211_add_meshpeer(frm,
2829 IEEE80211_ACTION_MESHPEERING_CONFIRM,
2830 args[0], args[1], 0);
2831 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2832 return mesh_send_action(ni, vap->iv_myaddr, ni->ni_macaddr, m);
2834 vap->iv_stats.is_tx_nobuf++;
2835 ieee80211_free_node(ni);
2841 mesh_send_action_meshpeering_close(struct ieee80211_node *ni,
2842 int category, int action, void *args0)
2844 struct ieee80211vap *vap = ni->ni_vap;
2845 struct ieee80211com *ic = ni->ni_ic;
2846 uint16_t *args = args0;
2850 IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH, ni,
2851 "send PEER CLOSE action: localid 0x%x, peerid 0x%x reason %d (%s)",
2852 args[0], args[1], args[2], ieee80211_reason_to_string(args[2]));
2854 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
2855 "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", __func__, __LINE__,
2856 ni, ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)+1);
2857 ieee80211_ref_node(ni);
2859 m = ieee80211_getmgtframe(&frm,
2860 ic->ic_headroom + sizeof(struct ieee80211_frame),
2861 sizeof(uint16_t) /* action+category */
2862 + sizeof(uint16_t) /* reason code */
2863 + 2 + IEEE80211_MESHID_LEN
2864 + sizeof(struct ieee80211_meshpeer_ie)
2868 * mesh peer close action frame format:
2872 * [tlv] mesh peer link mgmt
2876 frm = ieee80211_add_meshid(frm, vap);
2877 frm = ieee80211_add_meshpeer(frm,
2878 IEEE80211_ACTION_MESHPEERING_CLOSE,
2879 args[0], args[1], args[2]);
2880 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2881 return mesh_send_action(ni, vap->iv_myaddr, ni->ni_macaddr, m);
2883 vap->iv_stats.is_tx_nobuf++;
2884 ieee80211_free_node(ni);
2890 mesh_send_action_meshlmetric(struct ieee80211_node *ni,
2891 int category, int action, void *arg0)
2893 struct ieee80211vap *vap = ni->ni_vap;
2894 struct ieee80211com *ic = ni->ni_ic;
2895 struct ieee80211_meshlmetric_ie *ie = arg0;
2899 if (ie->lm_flags & IEEE80211_MESH_LMETRIC_FLAGS_REQ) {
2900 IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH,
2901 ni, "%s", "send LINK METRIC REQUEST action");
2903 IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH,
2904 ni, "send LINK METRIC REPLY action: metric 0x%x",
2907 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
2908 "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", __func__, __LINE__,
2909 ni, ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)+1);
2910 ieee80211_ref_node(ni);
2912 m = ieee80211_getmgtframe(&frm,
2913 ic->ic_headroom + sizeof(struct ieee80211_frame),
2914 sizeof(uint16_t) + /* action+category */
2915 sizeof(struct ieee80211_meshlmetric_ie)
2922 * [tlv] mesh link metric
2926 frm = ieee80211_add_meshlmetric(frm,
2927 ie->lm_flags, ie->lm_metric);
2928 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2929 return mesh_send_action(ni, vap->iv_myaddr, ni->ni_macaddr, m);
2931 vap->iv_stats.is_tx_nobuf++;
2932 ieee80211_free_node(ni);
2938 mesh_send_action_meshgate(struct ieee80211_node *ni,
2939 int category, int action, void *arg0)
2941 struct ieee80211vap *vap = ni->ni_vap;
2942 struct ieee80211com *ic = ni->ni_ic;
2943 struct ieee80211_meshgann_ie *ie = arg0;
2947 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
2948 "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", __func__, __LINE__,
2949 ni, ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)+1);
2950 ieee80211_ref_node(ni);
2952 m = ieee80211_getmgtframe(&frm,
2953 ic->ic_headroom + sizeof(struct ieee80211_frame),
2954 sizeof(uint16_t) + /* action+category */
2955 IEEE80211_MESHGANN_BASE_SZ
2962 * [tlv] mesh gate announcement
2966 frm = ieee80211_add_meshgate(frm, ie);
2967 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2968 return mesh_send_action(ni, vap->iv_myaddr, broadcastaddr, m);
2970 vap->iv_stats.is_tx_nobuf++;
2971 ieee80211_free_node(ni);
2977 mesh_peer_timeout_setup(struct ieee80211_node *ni)
2979 switch (ni->ni_mlstate) {
2980 case IEEE80211_NODE_MESH_HOLDING:
2981 ni->ni_mltval = ieee80211_mesh_holdingtimeout;
2983 case IEEE80211_NODE_MESH_CONFIRMRCV:
2984 ni->ni_mltval = ieee80211_mesh_confirmtimeout;
2986 case IEEE80211_NODE_MESH_IDLE:
2990 ni->ni_mltval = ieee80211_mesh_retrytimeout;
2994 callout_reset(&ni->ni_mltimer, ni->ni_mltval,
2995 mesh_peer_timeout_cb, ni);
2999 * Same as above but backoffs timer statisically 50%.
3002 mesh_peer_timeout_backoff(struct ieee80211_node *ni)
3007 ni->ni_mltval += r % ni->ni_mltval;
3008 callout_reset(&ni->ni_mltimer, ni->ni_mltval, mesh_peer_timeout_cb,
3012 static __inline void
3013 mesh_peer_timeout_stop(struct ieee80211_node *ni)
3015 callout_drain(&ni->ni_mltimer);
3019 mesh_peer_backoff_cb(void *arg)
3021 struct ieee80211_node *ni = (struct ieee80211_node *)arg;
3023 /* After backoff timeout, try to peer automatically again. */
3028 * Mesh Peer Link Management FSM timeout handling.
3031 mesh_peer_timeout_cb(void *arg)
3033 struct ieee80211_node *ni = (struct ieee80211_node *)arg;
3036 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_MESH,
3037 ni, "mesh link timeout, state %d, retry counter %d",
3038 ni->ni_mlstate, ni->ni_mlrcnt);
3040 switch (ni->ni_mlstate) {
3041 case IEEE80211_NODE_MESH_IDLE:
3042 case IEEE80211_NODE_MESH_ESTABLISHED:
3044 case IEEE80211_NODE_MESH_OPENSNT:
3045 case IEEE80211_NODE_MESH_OPENRCV:
3046 if (ni->ni_mlrcnt == ieee80211_mesh_maxretries) {
3047 args[0] = ni->ni_mlpid;
3048 args[2] = IEEE80211_REASON_MESH_MAX_RETRIES;
3049 ieee80211_send_action(ni,
3050 IEEE80211_ACTION_CAT_SELF_PROT,
3051 IEEE80211_ACTION_MESHPEERING_CLOSE, args);
3053 mesh_linkchange(ni, IEEE80211_NODE_MESH_HOLDING);
3054 mesh_peer_timeout_setup(ni);
3056 args[0] = ni->ni_mlpid;
3057 ieee80211_send_action(ni,
3058 IEEE80211_ACTION_CAT_SELF_PROT,
3059 IEEE80211_ACTION_MESHPEERING_OPEN, args);
3061 mesh_peer_timeout_backoff(ni);
3064 case IEEE80211_NODE_MESH_CONFIRMRCV:
3065 args[0] = ni->ni_mlpid;
3066 args[2] = IEEE80211_REASON_MESH_CONFIRM_TIMEOUT;
3067 ieee80211_send_action(ni,
3068 IEEE80211_ACTION_CAT_SELF_PROT,
3069 IEEE80211_ACTION_MESHPEERING_CLOSE, args);
3070 mesh_linkchange(ni, IEEE80211_NODE_MESH_HOLDING);
3071 mesh_peer_timeout_setup(ni);
3073 case IEEE80211_NODE_MESH_HOLDING:
3075 if (ni->ni_mlhcnt >= ieee80211_mesh_maxholding)
3076 callout_reset(&ni->ni_mlhtimer,
3077 ieee80211_mesh_backofftimeout,
3078 mesh_peer_backoff_cb, ni);
3079 mesh_linkchange(ni, IEEE80211_NODE_MESH_IDLE);
3085 mesh_verify_meshid(struct ieee80211vap *vap, const uint8_t *ie)
3087 struct ieee80211_mesh_state *ms = vap->iv_mesh;
3089 if (ie == NULL || ie[1] != ms->ms_idlen)
3091 return memcmp(ms->ms_id, ie + 2, ms->ms_idlen);
3095 * Check if we are using the same algorithms for this mesh.
3098 mesh_verify_meshconf(struct ieee80211vap *vap, const uint8_t *ie)
3100 const struct ieee80211_meshconf_ie *meshconf =
3101 (const struct ieee80211_meshconf_ie *) ie;
3102 const struct ieee80211_mesh_state *ms = vap->iv_mesh;
3104 if (meshconf == NULL)
3106 if (meshconf->conf_pselid != ms->ms_ppath->mpp_ie) {
3107 IEEE80211_DPRINTF(vap, IEEE80211_MSG_MESH,
3108 "unknown path selection algorithm: 0x%x\n",
3109 meshconf->conf_pselid);
3112 if (meshconf->conf_pmetid != ms->ms_pmetric->mpm_ie) {
3113 IEEE80211_DPRINTF(vap, IEEE80211_MSG_MESH,
3114 "unknown path metric algorithm: 0x%x\n",
3115 meshconf->conf_pmetid);
3118 if (meshconf->conf_ccid != 0) {
3119 IEEE80211_DPRINTF(vap, IEEE80211_MSG_MESH,
3120 "unknown congestion control algorithm: 0x%x\n",
3121 meshconf->conf_ccid);
3124 if (meshconf->conf_syncid != IEEE80211_MESHCONF_SYNC_NEIGHOFF) {
3125 IEEE80211_DPRINTF(vap, IEEE80211_MSG_MESH,
3126 "unknown sync algorithm: 0x%x\n",
3127 meshconf->conf_syncid);
3130 if (meshconf->conf_authid != 0) {
3131 IEEE80211_DPRINTF(vap, IEEE80211_MSG_MESH,
3132 "unknown auth auth algorithm: 0x%x\n",
3133 meshconf->conf_pselid);
3136 /* Not accepting peers */
3137 if (!(meshconf->conf_cap & IEEE80211_MESHCONF_CAP_AP)) {
3138 IEEE80211_DPRINTF(vap, IEEE80211_MSG_MESH,
3139 "not accepting peers: 0x%x\n", meshconf->conf_cap);
3146 mesh_verify_meshpeer(struct ieee80211vap *vap, uint8_t subtype,
3149 const struct ieee80211_meshpeer_ie *meshpeer =
3150 (const struct ieee80211_meshpeer_ie *) ie;
3152 if (meshpeer == NULL ||
3153 meshpeer->peer_len < IEEE80211_MPM_BASE_SZ ||
3154 meshpeer->peer_len > IEEE80211_MPM_MAX_SZ)
3156 if (meshpeer->peer_proto != IEEE80211_MPPID_MPM) {
3157 IEEE80211_DPRINTF(vap,
3158 IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH,
3159 "Only MPM protocol is supported (proto: 0x%02X)",
3160 meshpeer->peer_proto);
3164 case IEEE80211_ACTION_MESHPEERING_OPEN:
3165 if (meshpeer->peer_len != IEEE80211_MPM_BASE_SZ)
3168 case IEEE80211_ACTION_MESHPEERING_CONFIRM:
3169 if (meshpeer->peer_len != IEEE80211_MPM_BASE_SZ + 2)
3172 case IEEE80211_ACTION_MESHPEERING_CLOSE:
3173 if (meshpeer->peer_len < IEEE80211_MPM_BASE_SZ + 2)
3175 if (meshpeer->peer_len == (IEEE80211_MPM_BASE_SZ + 2) &&
3176 meshpeer->peer_linkid != 0)
3178 if (meshpeer->peer_rcode == 0)
3186 * Add a Mesh ID IE to a frame.
3189 ieee80211_add_meshid(uint8_t *frm, struct ieee80211vap *vap)
3191 struct ieee80211_mesh_state *ms = vap->iv_mesh;
3193 KASSERT(vap->iv_opmode == IEEE80211_M_MBSS, ("not a mbss vap"));
3195 *frm++ = IEEE80211_ELEMID_MESHID;
3196 *frm++ = ms->ms_idlen;
3197 memcpy(frm, ms->ms_id, ms->ms_idlen);
3198 return frm + ms->ms_idlen;
3202 * Add a Mesh Configuration IE to a frame.
3203 * For now just use HWMP routing, Airtime link metric, Null Congestion
3204 * Signaling, Null Sync Protocol and Null Authentication.
3207 ieee80211_add_meshconf(uint8_t *frm, struct ieee80211vap *vap)
3209 const struct ieee80211_mesh_state *ms = vap->iv_mesh;
3212 KASSERT(vap->iv_opmode == IEEE80211_M_MBSS, ("not a MBSS vap"));
3214 *frm++ = IEEE80211_ELEMID_MESHCONF;
3215 *frm++ = IEEE80211_MESH_CONF_SZ;
3216 *frm++ = ms->ms_ppath->mpp_ie; /* path selection */
3217 *frm++ = ms->ms_pmetric->mpm_ie; /* link metric */
3218 *frm++ = IEEE80211_MESHCONF_CC_DISABLED;
3219 *frm++ = IEEE80211_MESHCONF_SYNC_NEIGHOFF;
3220 *frm++ = IEEE80211_MESHCONF_AUTH_DISABLED;
3221 /* NB: set the number of neighbors before the rest */
3222 *frm = (ms->ms_neighbors > IEEE80211_MESH_MAX_NEIGHBORS ?
3223 IEEE80211_MESH_MAX_NEIGHBORS : ms->ms_neighbors) << 1;
3224 if (ms->ms_flags & IEEE80211_MESHFLAGS_GATE)
3225 *frm |= IEEE80211_MESHCONF_FORM_GATE;
3228 if (ms->ms_flags & IEEE80211_MESHFLAGS_AP)
3229 caps |= IEEE80211_MESHCONF_CAP_AP;
3230 if (ms->ms_flags & IEEE80211_MESHFLAGS_FWD)
3231 caps |= IEEE80211_MESHCONF_CAP_FWRD;
3237 * Add a Mesh Peer Management IE to a frame.
3240 ieee80211_add_meshpeer(uint8_t *frm, uint8_t subtype, uint16_t localid,
3241 uint16_t peerid, uint16_t reason)
3244 KASSERT(localid != 0, ("localid == 0"));
3246 *frm++ = IEEE80211_ELEMID_MESHPEER;
3248 case IEEE80211_ACTION_MESHPEERING_OPEN:
3249 *frm++ = IEEE80211_MPM_BASE_SZ; /* length */
3250 ADDSHORT(frm, IEEE80211_MPPID_MPM); /* proto */
3251 ADDSHORT(frm, localid); /* local ID */
3253 case IEEE80211_ACTION_MESHPEERING_CONFIRM:
3254 KASSERT(peerid != 0, ("sending peer confirm without peer id"));
3255 *frm++ = IEEE80211_MPM_BASE_SZ + 2; /* length */
3256 ADDSHORT(frm, IEEE80211_MPPID_MPM); /* proto */
3257 ADDSHORT(frm, localid); /* local ID */
3258 ADDSHORT(frm, peerid); /* peer ID */
3260 case IEEE80211_ACTION_MESHPEERING_CLOSE:
3262 *frm++ = IEEE80211_MPM_MAX_SZ; /* length */
3264 *frm++ = IEEE80211_MPM_BASE_SZ + 2; /* length */
3265 ADDSHORT(frm, IEEE80211_MPPID_MPM); /* proto */
3266 ADDSHORT(frm, localid); /* local ID */
3268 ADDSHORT(frm, peerid); /* peer ID */
3269 ADDSHORT(frm, reason);
3276 * Compute an Airtime Link Metric for the link with this node.
3278 * Based on Draft 3.0 spec (11B.10, p.149).
3281 * Max 802.11s overhead.
3283 #define IEEE80211_MESH_MAXOVERHEAD \
3284 (sizeof(struct ieee80211_qosframe_addr4) \
3285 + sizeof(struct ieee80211_meshcntl_ae10) \
3286 + sizeof(struct llc) \
3287 + IEEE80211_ADDR_LEN \
3288 + IEEE80211_WEP_IVLEN \
3289 + IEEE80211_WEP_KIDLEN \
3290 + IEEE80211_WEP_CRCLEN \
3291 + IEEE80211_WEP_MICLEN \
3292 + IEEE80211_CRC_LEN)
3294 mesh_airtime_calc(struct ieee80211_node *ni)
3297 #define S_FACTOR (2 * M_BITS)
3298 struct ieee80211com *ic = ni->ni_ic;
3299 struct ifnet *ifp = ni->ni_vap->iv_ifp;
3300 const static int nbits = 8192 << M_BITS;
3301 uint32_t overhead, rate, errrate;
3304 /* Time to transmit a frame */
3305 rate = ni->ni_txrate;
3306 overhead = ieee80211_compute_duration(ic->ic_rt,
3307 ifp->if_mtu + IEEE80211_MESH_MAXOVERHEAD, rate, 0) << M_BITS;
3308 /* Error rate in percentage */
3309 /* XXX assuming small failures are ok */
3310 errrate = (((ifp->if_get_counter(ifp, IFCOUNTER_OERRORS) +
3311 ifp->if_get_counter(ifp, IFCOUNTER_IERRORS)) / 100) << M_BITS)
3313 res = (overhead + (nbits / rate)) *
3314 ((1 << S_FACTOR) / ((1 << M_BITS) - errrate));
3316 return (uint32_t)(res >> S_FACTOR);
3322 * Add a Mesh Link Metric report IE to a frame.
3325 ieee80211_add_meshlmetric(uint8_t *frm, uint8_t flags, uint32_t metric)
3327 *frm++ = IEEE80211_ELEMID_MESHLINK;
3330 ADDWORD(frm, metric);
3335 * Add a Mesh Gate Announcement IE to a frame.
3338 ieee80211_add_meshgate(uint8_t *frm, struct ieee80211_meshgann_ie *ie)
3340 *frm++ = IEEE80211_ELEMID_MESHGANN; /* ie */
3341 *frm++ = IEEE80211_MESHGANN_BASE_SZ; /* len */
3342 *frm++ = ie->gann_flags;
3343 *frm++ = ie->gann_hopcount;
3344 *frm++ = ie->gann_ttl;
3345 IEEE80211_ADDR_COPY(frm, ie->gann_addr);
3347 ADDWORD(frm, ie->gann_seq);
3348 ADDSHORT(frm, ie->gann_interval);
3355 * Initialize any mesh-specific node state.
3358 ieee80211_mesh_node_init(struct ieee80211vap *vap, struct ieee80211_node *ni)
3360 ni->ni_flags |= IEEE80211_NODE_QOS;
3361 callout_init(&ni->ni_mltimer, 1);
3362 callout_init(&ni->ni_mlhtimer, 1);
3366 * Cleanup any mesh-specific node state.
3369 ieee80211_mesh_node_cleanup(struct ieee80211_node *ni)
3371 struct ieee80211vap *vap = ni->ni_vap;
3372 struct ieee80211_mesh_state *ms = vap->iv_mesh;
3374 callout_drain(&ni->ni_mltimer);
3375 callout_drain(&ni->ni_mlhtimer);
3376 /* NB: short-circuit callbacks after mesh_vdetach */
3377 if (vap->iv_mesh != NULL)
3378 ms->ms_ppath->mpp_peerdown(ni);
3382 ieee80211_parse_meshid(struct ieee80211_node *ni, const uint8_t *ie)
3384 ni->ni_meshidlen = ie[1];
3385 memcpy(ni->ni_meshid, ie + 2, ie[1]);
3389 * Setup mesh-specific node state on neighbor discovery.
3392 ieee80211_mesh_init_neighbor(struct ieee80211_node *ni,
3393 const struct ieee80211_frame *wh,
3394 const struct ieee80211_scanparams *sp)
3396 ieee80211_parse_meshid(ni, sp->meshid);
3400 ieee80211_mesh_update_beacon(struct ieee80211vap *vap,
3401 struct ieee80211_beacon_offsets *bo)
3403 KASSERT(vap->iv_opmode == IEEE80211_M_MBSS, ("not a MBSS vap"));
3405 if (isset(bo->bo_flags, IEEE80211_BEACON_MESHCONF)) {
3406 (void)ieee80211_add_meshconf(bo->bo_meshconf, vap);
3407 clrbit(bo->bo_flags, IEEE80211_BEACON_MESHCONF);
3412 mesh_ioctl_get80211(struct ieee80211vap *vap, struct ieee80211req *ireq)
3414 struct ieee80211_mesh_state *ms = vap->iv_mesh;
3415 uint8_t tmpmeshid[IEEE80211_NWID_LEN];
3416 struct ieee80211_mesh_route *rt;
3417 struct ieee80211req_mesh_route *imr;
3422 if (vap->iv_opmode != IEEE80211_M_MBSS)
3426 switch (ireq->i_type) {
3427 case IEEE80211_IOC_MESH_ID:
3428 ireq->i_len = ms->ms_idlen;
3429 memcpy(tmpmeshid, ms->ms_id, ireq->i_len);
3430 error = copyout(tmpmeshid, ireq->i_data, ireq->i_len);
3432 case IEEE80211_IOC_MESH_AP:
3433 ireq->i_val = (ms->ms_flags & IEEE80211_MESHFLAGS_AP) != 0;
3435 case IEEE80211_IOC_MESH_FWRD:
3436 ireq->i_val = (ms->ms_flags & IEEE80211_MESHFLAGS_FWD) != 0;
3438 case IEEE80211_IOC_MESH_GATE:
3439 ireq->i_val = (ms->ms_flags & IEEE80211_MESHFLAGS_GATE) != 0;
3441 case IEEE80211_IOC_MESH_TTL:
3442 ireq->i_val = ms->ms_ttl;
3444 case IEEE80211_IOC_MESH_RTCMD:
3445 switch (ireq->i_val) {
3446 case IEEE80211_MESH_RTCMD_LIST:
3449 TAILQ_FOREACH(rt, &ms->ms_routes, rt_next) {
3450 len += sizeof(*imr);
3453 if (len > ireq->i_len || ireq->i_len < sizeof(*imr)) {
3459 p = IEEE80211_MALLOC(len, M_TEMP,
3460 IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
3465 TAILQ_FOREACH(rt, &ms->ms_routes, rt_next) {
3468 imr = (struct ieee80211req_mesh_route *)
3470 IEEE80211_ADDR_COPY(imr->imr_dest,
3472 IEEE80211_ADDR_COPY(imr->imr_nexthop,
3474 imr->imr_metric = rt->rt_metric;
3475 imr->imr_nhops = rt->rt_nhops;
3477 ieee80211_mesh_rt_update(rt, 0);
3478 imr->imr_lastmseq = rt->rt_lastmseq;
3479 imr->imr_flags = rt->rt_flags; /* last */
3480 off += sizeof(*imr);
3483 error = copyout(p, (uint8_t *)ireq->i_data,
3485 IEEE80211_FREE(p, M_TEMP);
3487 case IEEE80211_MESH_RTCMD_FLUSH:
3488 case IEEE80211_MESH_RTCMD_ADD:
3489 case IEEE80211_MESH_RTCMD_DELETE:
3495 case IEEE80211_IOC_MESH_PR_METRIC:
3496 len = strlen(ms->ms_pmetric->mpm_descr);
3497 if (ireq->i_len < len)
3500 error = copyout(ms->ms_pmetric->mpm_descr,
3501 (uint8_t *)ireq->i_data, len);
3503 case IEEE80211_IOC_MESH_PR_PATH:
3504 len = strlen(ms->ms_ppath->mpp_descr);
3505 if (ireq->i_len < len)
3508 error = copyout(ms->ms_ppath->mpp_descr,
3509 (uint8_t *)ireq->i_data, len);
3517 IEEE80211_IOCTL_GET(mesh, mesh_ioctl_get80211);
3520 mesh_ioctl_set80211(struct ieee80211vap *vap, struct ieee80211req *ireq)
3522 struct ieee80211_mesh_state *ms = vap->iv_mesh;
3523 uint8_t tmpmeshid[IEEE80211_NWID_LEN];
3524 uint8_t tmpaddr[IEEE80211_ADDR_LEN];
3525 char tmpproto[IEEE80211_MESH_PROTO_DSZ];
3528 if (vap->iv_opmode != IEEE80211_M_MBSS)
3532 switch (ireq->i_type) {
3533 case IEEE80211_IOC_MESH_ID:
3534 if (ireq->i_val != 0 || ireq->i_len > IEEE80211_MESHID_LEN)
3536 error = copyin(ireq->i_data, tmpmeshid, ireq->i_len);
3539 memset(ms->ms_id, 0, IEEE80211_NWID_LEN);
3540 ms->ms_idlen = ireq->i_len;
3541 memcpy(ms->ms_id, tmpmeshid, ireq->i_len);
3544 case IEEE80211_IOC_MESH_AP:
3546 ms->ms_flags |= IEEE80211_MESHFLAGS_AP;
3548 ms->ms_flags &= ~IEEE80211_MESHFLAGS_AP;
3551 case IEEE80211_IOC_MESH_FWRD:
3553 ms->ms_flags |= IEEE80211_MESHFLAGS_FWD;
3555 ms->ms_flags &= ~IEEE80211_MESHFLAGS_FWD;
3556 mesh_gatemode_setup(vap);
3558 case IEEE80211_IOC_MESH_GATE:
3560 ms->ms_flags |= IEEE80211_MESHFLAGS_GATE;
3562 ms->ms_flags &= ~IEEE80211_MESHFLAGS_GATE;
3564 case IEEE80211_IOC_MESH_TTL:
3565 ms->ms_ttl = (uint8_t) ireq->i_val;
3567 case IEEE80211_IOC_MESH_RTCMD:
3568 switch (ireq->i_val) {
3569 case IEEE80211_MESH_RTCMD_LIST:
3571 case IEEE80211_MESH_RTCMD_FLUSH:
3572 ieee80211_mesh_rt_flush(vap);
3574 case IEEE80211_MESH_RTCMD_ADD:
3575 error = copyin(ireq->i_data, tmpaddr,
3576 IEEE80211_ADDR_LEN);
3579 if (IEEE80211_ADDR_EQ(vap->iv_myaddr, tmpaddr) ||
3580 IEEE80211_ADDR_EQ(broadcastaddr, tmpaddr))
3582 ieee80211_mesh_discover(vap, tmpaddr, NULL);
3584 case IEEE80211_MESH_RTCMD_DELETE:
3585 error = copyin(ireq->i_data, tmpaddr,
3586 IEEE80211_ADDR_LEN);
3589 ieee80211_mesh_rt_del(vap, tmpaddr);
3595 case IEEE80211_IOC_MESH_PR_METRIC:
3596 error = copyin(ireq->i_data, tmpproto, sizeof(tmpproto));
3598 error = mesh_select_proto_metric(vap, tmpproto);
3603 case IEEE80211_IOC_MESH_PR_PATH:
3604 error = copyin(ireq->i_data, tmpproto, sizeof(tmpproto));
3606 error = mesh_select_proto_path(vap, tmpproto);
3616 IEEE80211_IOCTL_SET(mesh, mesh_ioctl_set80211);