2 * Copyright (c) 2009 The FreeBSD Foundation
5 * This software was developed by Rui Paulo under sponsorship from the
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
35 * IEEE 802.11s Mesh Point (MBSS) support.
37 * Based on March 2009, D3.0 802.11s draft spec.
42 #include <sys/param.h>
43 #include <sys/systm.h>
45 #include <sys/malloc.h>
46 #include <sys/kernel.h>
48 #include <sys/socket.h>
49 #include <sys/sockio.h>
50 #include <sys/endian.h>
51 #include <sys/errno.h>
53 #include <sys/sysctl.h>
57 #include <net/if_media.h>
58 #include <net/if_llc.h>
59 #include <net/ethernet.h>
61 #include <net80211/ieee80211_var.h>
62 #include <net80211/ieee80211_action.h>
63 #ifdef IEEE80211_SUPPORT_SUPERG
64 #include <net80211/ieee80211_superg.h>
66 #include <net80211/ieee80211_input.h>
67 #include <net80211/ieee80211_mesh.h>
69 static void mesh_rt_flush_invalid(struct ieee80211vap *);
70 static int mesh_select_proto_path(struct ieee80211vap *, const char *);
71 static int mesh_select_proto_metric(struct ieee80211vap *, const char *);
72 static void mesh_vattach(struct ieee80211vap *);
73 static int mesh_newstate(struct ieee80211vap *, enum ieee80211_state, int);
74 static void mesh_rt_cleanup_cb(void *);
75 static void mesh_gatemode_setup(struct ieee80211vap *);
76 static void mesh_gatemode_cb(void *);
77 static void mesh_linkchange(struct ieee80211_node *,
78 enum ieee80211_mesh_mlstate);
79 static void mesh_checkid(void *, struct ieee80211_node *);
80 static uint32_t mesh_generateid(struct ieee80211vap *);
81 static int mesh_checkpseq(struct ieee80211vap *,
82 const uint8_t [IEEE80211_ADDR_LEN], uint32_t);
83 static void mesh_transmit_to_gate(struct ieee80211vap *, struct mbuf *,
84 struct ieee80211_mesh_route *);
85 static void mesh_forward(struct ieee80211vap *, struct mbuf *,
86 const struct ieee80211_meshcntl *);
87 static int mesh_input(struct ieee80211_node *, struct mbuf *, int, int);
88 static void mesh_recv_mgmt(struct ieee80211_node *, struct mbuf *, int,
90 static void mesh_recv_ctl(struct ieee80211_node *, struct mbuf *, int);
91 static void mesh_peer_timeout_setup(struct ieee80211_node *);
92 static void mesh_peer_timeout_backoff(struct ieee80211_node *);
93 static void mesh_peer_timeout_cb(void *);
95 mesh_peer_timeout_stop(struct ieee80211_node *);
96 static int mesh_verify_meshid(struct ieee80211vap *, const uint8_t *);
97 static int mesh_verify_meshconf(struct ieee80211vap *, const uint8_t *);
98 static int mesh_verify_meshpeer(struct ieee80211vap *, uint8_t,
100 uint32_t mesh_airtime_calc(struct ieee80211_node *);
103 * Timeout values come from the specification and are in milliseconds.
105 static SYSCTL_NODE(_net_wlan, OID_AUTO, mesh, CTLFLAG_RD, 0,
106 "IEEE 802.11s parameters");
107 static int ieee80211_mesh_gateint = -1;
108 SYSCTL_PROC(_net_wlan_mesh, OID_AUTO, gateint, CTLTYPE_INT | CTLFLAG_RW,
109 &ieee80211_mesh_gateint, 0, ieee80211_sysctl_msecs_ticks, "I",
110 "mesh gate interval (ms)");
111 static int ieee80211_mesh_retrytimeout = -1;
112 SYSCTL_PROC(_net_wlan_mesh, OID_AUTO, retrytimeout, CTLTYPE_INT | CTLFLAG_RW,
113 &ieee80211_mesh_retrytimeout, 0, ieee80211_sysctl_msecs_ticks, "I",
114 "Retry timeout (msec)");
115 static int ieee80211_mesh_holdingtimeout = -1;
117 SYSCTL_PROC(_net_wlan_mesh, OID_AUTO, holdingtimeout, CTLTYPE_INT | CTLFLAG_RW,
118 &ieee80211_mesh_holdingtimeout, 0, ieee80211_sysctl_msecs_ticks, "I",
119 "Holding state timeout (msec)");
120 static int ieee80211_mesh_confirmtimeout = -1;
121 SYSCTL_PROC(_net_wlan_mesh, OID_AUTO, confirmtimeout, CTLTYPE_INT | CTLFLAG_RW,
122 &ieee80211_mesh_confirmtimeout, 0, ieee80211_sysctl_msecs_ticks, "I",
123 "Confirm state timeout (msec)");
124 static int ieee80211_mesh_backofftimeout = -1;
125 SYSCTL_PROC(_net_wlan_mesh, OID_AUTO, backofftimeout, CTLTYPE_INT | CTLFLAG_RW,
126 &ieee80211_mesh_backofftimeout, 0, ieee80211_sysctl_msecs_ticks, "I",
127 "Backoff timeout (msec). This is to throutles peering forever when "
128 "not receving answer or is rejected by a neighbor");
129 static int ieee80211_mesh_maxretries = 2;
130 SYSCTL_INT(_net_wlan_mesh, OID_AUTO, maxretries, CTLTYPE_INT | CTLFLAG_RW,
131 &ieee80211_mesh_maxretries, 0,
132 "Maximum retries during peer link establishment");
133 static int ieee80211_mesh_maxholding = 2;
134 SYSCTL_INT(_net_wlan_mesh, OID_AUTO, maxholding, CTLTYPE_INT | CTLFLAG_RW,
135 &ieee80211_mesh_maxholding, 0,
136 "Maximum times we are allowed to transition to HOLDING state before "
137 "backinoff during peer link establishment");
139 static const uint8_t broadcastaddr[IEEE80211_ADDR_LEN] =
140 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
142 static ieee80211_recv_action_func mesh_recv_action_meshpeering_open;
143 static ieee80211_recv_action_func mesh_recv_action_meshpeering_confirm;
144 static ieee80211_recv_action_func mesh_recv_action_meshpeering_close;
145 static ieee80211_recv_action_func mesh_recv_action_meshlmetric;
146 static ieee80211_recv_action_func mesh_recv_action_meshgate;
148 static ieee80211_send_action_func mesh_send_action_meshpeering_open;
149 static ieee80211_send_action_func mesh_send_action_meshpeering_confirm;
150 static ieee80211_send_action_func mesh_send_action_meshpeering_close;
151 static ieee80211_send_action_func mesh_send_action_meshlmetric;
152 static ieee80211_send_action_func mesh_send_action_meshgate;
154 static const struct ieee80211_mesh_proto_metric mesh_metric_airtime = {
155 .mpm_descr = "AIRTIME",
156 .mpm_ie = IEEE80211_MESHCONF_METRIC_AIRTIME,
157 .mpm_metric = mesh_airtime_calc,
160 static struct ieee80211_mesh_proto_path mesh_proto_paths[4];
161 static struct ieee80211_mesh_proto_metric mesh_proto_metrics[4];
163 #define RT_ENTRY_LOCK(rt) mtx_lock(&(rt)->rt_lock)
164 #define RT_ENTRY_LOCK_ASSERT(rt) mtx_assert(&(rt)->rt_lock, MA_OWNED)
165 #define RT_ENTRY_UNLOCK(rt) mtx_unlock(&(rt)->rt_lock)
167 #define MESH_RT_LOCK(ms) mtx_lock(&(ms)->ms_rt_lock)
168 #define MESH_RT_LOCK_ASSERT(ms) mtx_assert(&(ms)->ms_rt_lock, MA_OWNED)
169 #define MESH_RT_UNLOCK(ms) mtx_unlock(&(ms)->ms_rt_lock)
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 = malloc(ALIGN(sizeof(struct ieee80211_mesh_route)) +
213 ms->ms_ppath->mpp_privlen, M_80211_MESH_RT, M_NOWAIT | M_ZERO);
216 IEEE80211_ADDR_COPY(rt->rt_dest, dest);
217 rt->rt_priv = (void *)ALIGN(&rt[1]);
218 mtx_init(&rt->rt_lock, "MBSS_RT", "802.11s route entry", MTX_DEF);
219 callout_init(&rt->rt_discovery, CALLOUT_MPSAFE);
220 rt->rt_updtime = ticks; /* create time */
221 TAILQ_INSERT_TAIL(&ms->ms_routes, rt, rt_next);
226 struct ieee80211_mesh_route *
227 ieee80211_mesh_rt_find(struct ieee80211vap *vap,
228 const uint8_t dest[IEEE80211_ADDR_LEN])
230 struct ieee80211_mesh_state *ms = vap->iv_mesh;
231 struct ieee80211_mesh_route *rt;
234 rt = mesh_rt_find_locked(ms, dest);
239 struct ieee80211_mesh_route *
240 ieee80211_mesh_rt_add(struct ieee80211vap *vap,
241 const uint8_t dest[IEEE80211_ADDR_LEN])
243 struct ieee80211_mesh_state *ms = vap->iv_mesh;
244 struct ieee80211_mesh_route *rt;
246 KASSERT(ieee80211_mesh_rt_find(vap, dest) == NULL,
247 ("%s: duplicate entry in the routing table", __func__));
248 KASSERT(!IEEE80211_ADDR_EQ(vap->iv_myaddr, dest),
249 ("%s: adding self to the routing table", __func__));
252 rt = mesh_rt_add_locked(vap, dest);
258 * Update the route lifetime and returns the updated lifetime.
259 * If new_lifetime is zero and route is timedout it will be invalidated.
260 * new_lifetime is in msec
263 ieee80211_mesh_rt_update(struct ieee80211_mesh_route *rt, int new_lifetime)
266 uint32_t lifetime = 0;
268 KASSERT(rt != NULL, ("route is NULL"));
273 /* dont clobber a proxy entry gated by us */
274 if (rt->rt_flags & IEEE80211_MESHRT_FLAGS_PROXY && rt->rt_nhops == 0) {
276 return rt->rt_lifetime;
279 timesince = ticks_to_msecs(now - rt->rt_updtime);
280 rt->rt_updtime = now;
281 if (timesince >= rt->rt_lifetime) {
282 if (new_lifetime != 0) {
283 rt->rt_lifetime = new_lifetime;
286 rt->rt_flags &= ~IEEE80211_MESHRT_FLAGS_VALID;
290 /* update what is left of lifetime */
291 rt->rt_lifetime = rt->rt_lifetime - timesince;
292 rt->rt_lifetime = MESH_ROUTE_LIFETIME_MAX(
293 new_lifetime, rt->rt_lifetime);
295 lifetime = rt->rt_lifetime;
302 * Add a proxy route (as needed) for the specified destination.
305 ieee80211_mesh_proxy_check(struct ieee80211vap *vap,
306 const uint8_t dest[IEEE80211_ADDR_LEN])
308 struct ieee80211_mesh_state *ms = vap->iv_mesh;
309 struct ieee80211_mesh_route *rt;
312 rt = mesh_rt_find_locked(ms, dest);
314 rt = mesh_rt_add_locked(vap, dest);
316 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH, dest,
317 "%s", "unable to add proxy entry");
318 vap->iv_stats.is_mesh_rtaddfailed++;
320 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH, dest,
321 "%s", "add proxy entry");
322 IEEE80211_ADDR_COPY(rt->rt_mesh_gate, vap->iv_myaddr);
323 IEEE80211_ADDR_COPY(rt->rt_nexthop, vap->iv_myaddr);
324 rt->rt_flags |= IEEE80211_MESHRT_FLAGS_VALID
325 | IEEE80211_MESHRT_FLAGS_PROXY;
327 } else if ((rt->rt_flags & IEEE80211_MESHRT_FLAGS_VALID) == 0) {
328 KASSERT(rt->rt_flags & IEEE80211_MESHRT_FLAGS_PROXY,
329 ("no proxy flag for poxy entry"));
330 struct ieee80211com *ic = vap->iv_ic;
332 * Fix existing entry created by received frames from
333 * stations that have some memory of dest. We also
334 * flush any frames held on the staging queue; delivering
335 * them is too much trouble right now.
337 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH, dest,
338 "%s", "fix proxy entry");
339 IEEE80211_ADDR_COPY(rt->rt_nexthop, vap->iv_myaddr);
340 rt->rt_flags |= IEEE80211_MESHRT_FLAGS_VALID
341 | IEEE80211_MESHRT_FLAGS_PROXY;
342 /* XXX belongs in hwmp */
343 ieee80211_ageq_drain_node(&ic->ic_stageq,
344 (void *)(uintptr_t) ieee80211_mac_hash(ic, dest));
351 mesh_rt_del(struct ieee80211_mesh_state *ms, struct ieee80211_mesh_route *rt)
353 TAILQ_REMOVE(&ms->ms_routes, rt, rt_next);
355 * Grab the lock before destroying it, to be sure no one else
356 * is holding the route.
359 callout_drain(&rt->rt_discovery);
360 mtx_destroy(&rt->rt_lock);
361 free(rt, M_80211_MESH_RT);
365 ieee80211_mesh_rt_del(struct ieee80211vap *vap,
366 const uint8_t dest[IEEE80211_ADDR_LEN])
368 struct ieee80211_mesh_state *ms = vap->iv_mesh;
369 struct ieee80211_mesh_route *rt, *next;
372 TAILQ_FOREACH_SAFE(rt, &ms->ms_routes, rt_next, next) {
373 if (IEEE80211_ADDR_EQ(rt->rt_dest, dest)) {
374 if (rt->rt_flags & IEEE80211_MESHRT_FLAGS_PROXY) {
375 ms->ms_ppath->mpp_senderror(vap, dest, rt,
376 IEEE80211_REASON_MESH_PERR_NO_PROXY);
378 ms->ms_ppath->mpp_senderror(vap, dest, rt,
379 IEEE80211_REASON_MESH_PERR_DEST_UNREACH);
390 ieee80211_mesh_rt_flush(struct ieee80211vap *vap)
392 struct ieee80211_mesh_state *ms = vap->iv_mesh;
393 struct ieee80211_mesh_route *rt, *next;
398 TAILQ_FOREACH_SAFE(rt, &ms->ms_routes, rt_next, next)
404 ieee80211_mesh_rt_flush_peer(struct ieee80211vap *vap,
405 const uint8_t peer[IEEE80211_ADDR_LEN])
407 struct ieee80211_mesh_state *ms = vap->iv_mesh;
408 struct ieee80211_mesh_route *rt, *next;
411 TAILQ_FOREACH_SAFE(rt, &ms->ms_routes, rt_next, next) {
412 if (IEEE80211_ADDR_EQ(rt->rt_nexthop, peer))
419 * Flush expired routing entries, i.e. those in invalid state for
423 mesh_rt_flush_invalid(struct ieee80211vap *vap)
425 struct ieee80211_mesh_state *ms = vap->iv_mesh;
426 struct ieee80211_mesh_route *rt, *next;
431 TAILQ_FOREACH_SAFE(rt, &ms->ms_routes, rt_next, next) {
432 /* Discover paths will be deleted by their own callout */
433 if (rt->rt_flags & IEEE80211_MESHRT_FLAGS_DISCOVER)
435 ieee80211_mesh_rt_update(rt, 0);
436 if ((rt->rt_flags & IEEE80211_MESHRT_FLAGS_VALID) == 0)
442 #define N(a) (sizeof(a) / sizeof(a[0]))
444 ieee80211_mesh_register_proto_path(const struct ieee80211_mesh_proto_path *mpp)
446 int i, firstempty = -1;
448 for (i = 0; i < N(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 < N(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 < N(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 < N(mesh_proto_metrics); i++) {
504 if (strcasecmp(mesh_proto_metrics[i].mpm_descr, name) == 0) {
505 ms->ms_pmetric = &mesh_proto_metrics[i];
514 mesh_gatemode_setup(struct ieee80211vap *vap)
516 struct ieee80211_mesh_state *ms = vap->iv_mesh;
519 * NB: When a mesh gate is running as a ROOT it shall
520 * not send out periodic GANNs but instead mark the
521 * mesh gate flag for the corresponding proactive PREQ
524 if (ms->ms_flags & IEEE80211_MESHFLAGS_ROOT ||
525 (ms->ms_flags & IEEE80211_MESHFLAGS_GATE) == 0) {
526 callout_drain(&ms->ms_gatetimer);
529 callout_reset(&ms->ms_gatetimer, ieee80211_mesh_gateint,
530 mesh_gatemode_cb, vap);
534 mesh_gatemode_cb(void *arg)
536 struct ieee80211vap *vap = (struct ieee80211vap *)arg;
537 struct ieee80211_mesh_state *ms = vap->iv_mesh;
538 struct ieee80211_meshgann_ie gann;
540 gann.gann_flags = 0; /* Reserved */
541 gann.gann_hopcount = 0;
542 gann.gann_ttl = ms->ms_ttl;
543 IEEE80211_ADDR_COPY(gann.gann_addr, vap->iv_myaddr);
544 gann.gann_seq = ms->ms_gateseq++;
545 gann.gann_interval = ieee80211_mesh_gateint;
547 IEEE80211_NOTE(vap, IEEE80211_MSG_MESH, vap->iv_bss,
548 "send broadcast GANN (seq %u)", gann.gann_seq);
550 ieee80211_send_action(vap->iv_bss, IEEE80211_ACTION_CAT_MESH,
551 IEEE80211_ACTION_MESH_GANN, &gann);
552 mesh_gatemode_setup(vap);
556 ieee80211_mesh_init(void)
559 memset(mesh_proto_paths, 0, sizeof(mesh_proto_paths));
560 memset(mesh_proto_metrics, 0, sizeof(mesh_proto_metrics));
563 * Setup mesh parameters that depends on the clock frequency.
565 ieee80211_mesh_gateint = msecs_to_ticks(10000);
566 ieee80211_mesh_retrytimeout = msecs_to_ticks(40);
567 ieee80211_mesh_holdingtimeout = msecs_to_ticks(40);
568 ieee80211_mesh_confirmtimeout = msecs_to_ticks(40);
569 ieee80211_mesh_backofftimeout = msecs_to_ticks(5000);
572 * Register action frame handlers.
574 ieee80211_recv_action_register(IEEE80211_ACTION_CAT_SELF_PROT,
575 IEEE80211_ACTION_MESHPEERING_OPEN,
576 mesh_recv_action_meshpeering_open);
577 ieee80211_recv_action_register(IEEE80211_ACTION_CAT_SELF_PROT,
578 IEEE80211_ACTION_MESHPEERING_CONFIRM,
579 mesh_recv_action_meshpeering_confirm);
580 ieee80211_recv_action_register(IEEE80211_ACTION_CAT_SELF_PROT,
581 IEEE80211_ACTION_MESHPEERING_CLOSE,
582 mesh_recv_action_meshpeering_close);
583 ieee80211_recv_action_register(IEEE80211_ACTION_CAT_MESH,
584 IEEE80211_ACTION_MESH_LMETRIC, mesh_recv_action_meshlmetric);
585 ieee80211_recv_action_register(IEEE80211_ACTION_CAT_MESH,
586 IEEE80211_ACTION_MESH_GANN, mesh_recv_action_meshgate);
588 ieee80211_send_action_register(IEEE80211_ACTION_CAT_SELF_PROT,
589 IEEE80211_ACTION_MESHPEERING_OPEN,
590 mesh_send_action_meshpeering_open);
591 ieee80211_send_action_register(IEEE80211_ACTION_CAT_SELF_PROT,
592 IEEE80211_ACTION_MESHPEERING_CONFIRM,
593 mesh_send_action_meshpeering_confirm);
594 ieee80211_send_action_register(IEEE80211_ACTION_CAT_SELF_PROT,
595 IEEE80211_ACTION_MESHPEERING_CLOSE,
596 mesh_send_action_meshpeering_close);
597 ieee80211_send_action_register(IEEE80211_ACTION_CAT_MESH,
598 IEEE80211_ACTION_MESH_LMETRIC,
599 mesh_send_action_meshlmetric);
600 ieee80211_send_action_register(IEEE80211_ACTION_CAT_MESH,
601 IEEE80211_ACTION_MESH_GANN,
602 mesh_send_action_meshgate);
605 * Register Airtime Link Metric.
607 ieee80211_mesh_register_proto_metric(&mesh_metric_airtime);
610 SYSINIT(wlan_mesh, SI_SUB_DRIVERS, SI_ORDER_FIRST, ieee80211_mesh_init, NULL);
613 ieee80211_mesh_attach(struct ieee80211com *ic)
615 ic->ic_vattach[IEEE80211_M_MBSS] = mesh_vattach;
619 ieee80211_mesh_detach(struct ieee80211com *ic)
624 mesh_vdetach_peers(void *arg, struct ieee80211_node *ni)
626 struct ieee80211com *ic = ni->ni_ic;
629 if (ni->ni_mlstate == IEEE80211_NODE_MESH_ESTABLISHED) {
630 args[0] = ni->ni_mlpid;
631 args[1] = ni->ni_mllid;
632 args[2] = IEEE80211_REASON_PEER_LINK_CANCELED;
633 ieee80211_send_action(ni,
634 IEEE80211_ACTION_CAT_SELF_PROT,
635 IEEE80211_ACTION_MESHPEERING_CLOSE,
638 callout_drain(&ni->ni_mltimer);
639 /* XXX belongs in hwmp */
640 ieee80211_ageq_drain_node(&ic->ic_stageq,
641 (void *)(uintptr_t) ieee80211_mac_hash(ic, ni->ni_macaddr));
645 mesh_vdetach(struct ieee80211vap *vap)
647 struct ieee80211_mesh_state *ms = vap->iv_mesh;
649 callout_drain(&ms->ms_cleantimer);
650 ieee80211_iterate_nodes(&vap->iv_ic->ic_sta, mesh_vdetach_peers,
652 ieee80211_mesh_rt_flush(vap);
653 mtx_destroy(&ms->ms_rt_lock);
654 ms->ms_ppath->mpp_vdetach(vap);
655 free(vap->iv_mesh, M_80211_VAP);
660 mesh_vattach(struct ieee80211vap *vap)
662 struct ieee80211_mesh_state *ms;
663 vap->iv_newstate = mesh_newstate;
664 vap->iv_input = mesh_input;
665 vap->iv_opdetach = mesh_vdetach;
666 vap->iv_recv_mgmt = mesh_recv_mgmt;
667 vap->iv_recv_ctl = mesh_recv_ctl;
668 ms = malloc(sizeof(struct ieee80211_mesh_state), M_80211_VAP,
671 printf("%s: couldn't alloc MBSS state\n", __func__);
676 ms->ms_flags = (IEEE80211_MESHFLAGS_AP | IEEE80211_MESHFLAGS_FWD);
677 ms->ms_ttl = IEEE80211_MESH_DEFAULT_TTL;
678 TAILQ_INIT(&ms->ms_known_gates);
679 TAILQ_INIT(&ms->ms_routes);
680 mtx_init(&ms->ms_rt_lock, "MBSS", "802.11s routing table", MTX_DEF);
681 callout_init(&ms->ms_cleantimer, CALLOUT_MPSAFE);
682 callout_init(&ms->ms_gatetimer, CALLOUT_MPSAFE);
684 mesh_select_proto_metric(vap, "AIRTIME");
685 KASSERT(ms->ms_pmetric, ("ms_pmetric == NULL"));
686 mesh_select_proto_path(vap, "HWMP");
687 KASSERT(ms->ms_ppath, ("ms_ppath == NULL"));
688 ms->ms_ppath->mpp_vattach(vap);
692 * IEEE80211_M_MBSS vap state machine handler.
695 mesh_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
697 struct ieee80211_mesh_state *ms = vap->iv_mesh;
698 struct ieee80211com *ic = vap->iv_ic;
699 struct ieee80211_node *ni;
700 enum ieee80211_state ostate;
702 IEEE80211_LOCK_ASSERT(ic);
704 ostate = vap->iv_state;
705 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE, "%s: %s -> %s (%d)\n",
706 __func__, ieee80211_state_name[ostate],
707 ieee80211_state_name[nstate], arg);
708 vap->iv_state = nstate; /* state transition */
709 if (ostate != IEEE80211_S_SCAN)
710 ieee80211_cancel_scan(vap); /* background scan */
711 ni = vap->iv_bss; /* NB: no reference held */
712 if (nstate != IEEE80211_S_RUN && ostate == IEEE80211_S_RUN) {
713 callout_drain(&ms->ms_cleantimer);
714 callout_drain(&ms->ms_gatetimer);
717 case IEEE80211_S_INIT:
719 case IEEE80211_S_SCAN:
720 ieee80211_cancel_scan(vap);
722 case IEEE80211_S_CAC:
723 ieee80211_dfs_cac_stop(vap);
725 case IEEE80211_S_RUN:
726 ieee80211_iterate_nodes(&ic->ic_sta,
727 mesh_vdetach_peers, NULL);
732 if (ostate != IEEE80211_S_INIT) {
733 /* NB: optimize INIT -> INIT case */
734 ieee80211_reset_bss(vap);
735 ieee80211_mesh_rt_flush(vap);
738 case IEEE80211_S_SCAN:
740 case IEEE80211_S_INIT:
741 if (vap->iv_des_chan != IEEE80211_CHAN_ANYC &&
742 !IEEE80211_IS_CHAN_RADAR(vap->iv_des_chan) &&
745 * Already have a channel and a mesh ID; bypass
746 * the scan and startup immediately.
748 ieee80211_create_ibss(vap, vap->iv_des_chan);
752 * Initiate a scan. We can come here as a result
753 * of an IEEE80211_IOC_SCAN_REQ too in which case
754 * the vap will be marked with IEEE80211_FEXT_SCANREQ
755 * and the scan request parameters will be present
756 * in iv_scanreq. Otherwise we do the default.
758 if (vap->iv_flags_ext & IEEE80211_FEXT_SCANREQ) {
759 ieee80211_check_scan(vap,
760 vap->iv_scanreq_flags,
761 vap->iv_scanreq_duration,
762 vap->iv_scanreq_mindwell,
763 vap->iv_scanreq_maxdwell,
764 vap->iv_scanreq_nssid, vap->iv_scanreq_ssid);
765 vap->iv_flags_ext &= ~IEEE80211_FEXT_SCANREQ;
767 ieee80211_check_scan_current(vap);
773 case IEEE80211_S_CAC:
775 * Start CAC on a DFS channel. We come here when starting
776 * a bss on a DFS channel (see ieee80211_create_ibss).
778 ieee80211_dfs_cac_start(vap);
780 case IEEE80211_S_RUN:
782 case IEEE80211_S_INIT:
784 * Already have a channel; bypass the
785 * scan and startup immediately.
786 * Note that ieee80211_create_ibss will call
787 * back to do a RUN->RUN state change.
789 ieee80211_create_ibss(vap,
790 ieee80211_ht_adjust_channel(ic,
791 ic->ic_curchan, vap->iv_flags_ht));
792 /* NB: iv_bss is changed on return */
794 case IEEE80211_S_CAC:
796 * NB: This is the normal state change when CAC
797 * expires and no radar was detected; no need to
798 * clear the CAC timer as it's already expired.
801 case IEEE80211_S_CSA:
804 * Shorten inactivity timer of associated stations
805 * to weed out sta's that don't follow a CSA.
807 ieee80211_iterate_nodes(&ic->ic_sta, sta_csa, vap);
810 * Update bss node channel to reflect where
811 * we landed after CSA.
813 ieee80211_node_set_chan(vap->iv_bss,
814 ieee80211_ht_adjust_channel(ic, ic->ic_curchan,
815 ieee80211_htchanflags(vap->iv_bss->ni_chan)));
816 /* XXX bypass debug msgs */
818 case IEEE80211_S_SCAN:
819 case IEEE80211_S_RUN:
820 #ifdef IEEE80211_DEBUG
821 if (ieee80211_msg_debug(vap)) {
822 struct ieee80211_node *ni = vap->iv_bss;
824 "synchronized with %s meshid ",
825 ether_sprintf(ni->ni_meshid));
826 ieee80211_print_essid(ni->ni_meshid,
829 printf(" channel %d\n",
830 ieee80211_chan2ieee(ic, ic->ic_curchan));
837 ieee80211_node_authorize(vap->iv_bss);
838 callout_reset(&ms->ms_cleantimer, ms->ms_ppath->mpp_inact,
839 mesh_rt_cleanup_cb, vap);
840 mesh_gatemode_setup(vap);
845 /* NB: ostate not nstate */
846 ms->ms_ppath->mpp_newstate(vap, ostate, arg);
851 mesh_rt_cleanup_cb(void *arg)
853 struct ieee80211vap *vap = arg;
854 struct ieee80211_mesh_state *ms = vap->iv_mesh;
856 mesh_rt_flush_invalid(vap);
857 callout_reset(&ms->ms_cleantimer, ms->ms_ppath->mpp_inact,
858 mesh_rt_cleanup_cb, vap);
862 * Mark a mesh STA as gate and return a pointer to it.
863 * If this is first time, we create a new gate route.
864 * Always update the path route to this mesh gate.
866 struct ieee80211_mesh_gate_route *
867 ieee80211_mesh_mark_gate(struct ieee80211vap *vap, const uint8_t *addr,
868 struct ieee80211_mesh_route *rt)
870 struct ieee80211_mesh_state *ms = vap->iv_mesh;
871 struct ieee80211_mesh_gate_route *gr = NULL, *next;
875 TAILQ_FOREACH_SAFE(gr, &ms->ms_known_gates, gr_next, next) {
876 if (IEEE80211_ADDR_EQ(gr->gr_addr, addr)) {
883 /* New mesh gate add it to known table. */
884 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH, addr,
885 "%s", "stored new gate information from pro-PREQ.");
886 gr = malloc(ALIGN(sizeof(struct ieee80211_mesh_gate_route)),
887 M_80211_MESH_GT_RT, M_NOWAIT | M_ZERO);
888 IEEE80211_ADDR_COPY(gr->gr_addr, addr);
889 TAILQ_INSERT_TAIL(&ms->ms_known_gates, gr, gr_next);
892 /* TODO: link from path route to gate route */
900 * Helper function to note the Mesh Peer Link FSM change.
903 mesh_linkchange(struct ieee80211_node *ni, enum ieee80211_mesh_mlstate state)
905 struct ieee80211vap *vap = ni->ni_vap;
906 struct ieee80211_mesh_state *ms = vap->iv_mesh;
907 #ifdef IEEE80211_DEBUG
908 static const char *meshlinkstates[] = {
909 [IEEE80211_NODE_MESH_IDLE] = "IDLE",
910 [IEEE80211_NODE_MESH_OPENSNT] = "OPEN SENT",
911 [IEEE80211_NODE_MESH_OPENRCV] = "OPEN RECEIVED",
912 [IEEE80211_NODE_MESH_CONFIRMRCV] = "CONFIRM RECEIVED",
913 [IEEE80211_NODE_MESH_ESTABLISHED] = "ESTABLISHED",
914 [IEEE80211_NODE_MESH_HOLDING] = "HOLDING"
917 IEEE80211_NOTE(vap, IEEE80211_MSG_MESH,
918 ni, "peer link: %s -> %s",
919 meshlinkstates[ni->ni_mlstate], meshlinkstates[state]);
921 /* track neighbor count */
922 if (state == IEEE80211_NODE_MESH_ESTABLISHED &&
923 ni->ni_mlstate != IEEE80211_NODE_MESH_ESTABLISHED) {
924 KASSERT(ms->ms_neighbors < 65535, ("neighbor count overflow"));
926 ieee80211_beacon_notify(vap, IEEE80211_BEACON_MESHCONF);
927 } else if (ni->ni_mlstate == IEEE80211_NODE_MESH_ESTABLISHED &&
928 state != IEEE80211_NODE_MESH_ESTABLISHED) {
929 KASSERT(ms->ms_neighbors > 0, ("neighbor count 0"));
931 ieee80211_beacon_notify(vap, IEEE80211_BEACON_MESHCONF);
933 ni->ni_mlstate = state;
935 case IEEE80211_NODE_MESH_HOLDING:
936 ms->ms_ppath->mpp_peerdown(ni);
938 case IEEE80211_NODE_MESH_ESTABLISHED:
939 ieee80211_mesh_discover(vap, ni->ni_macaddr, NULL);
947 * Helper function to generate a unique local ID required for mesh
948 * peer establishment.
951 mesh_checkid(void *arg, struct ieee80211_node *ni)
955 if (*r == ni->ni_mllid)
956 *(uint16_t *)arg = 0;
960 mesh_generateid(struct ieee80211vap *vap)
966 get_random_bytes(&r, 2);
967 ieee80211_iterate_nodes(&vap->iv_ic->ic_sta, mesh_checkid, &r);
969 } while (r == 0 && maxiter > 0);
974 * Verifies if we already received this packet by checking its
976 * Returns 0 if the frame is to be accepted, 1 otherwise.
979 mesh_checkpseq(struct ieee80211vap *vap,
980 const uint8_t source[IEEE80211_ADDR_LEN], uint32_t seq)
982 struct ieee80211_mesh_route *rt;
984 rt = ieee80211_mesh_rt_find(vap, source);
986 rt = ieee80211_mesh_rt_add(vap, source);
988 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH, source,
989 "%s", "add mcast route failed");
990 vap->iv_stats.is_mesh_rtaddfailed++;
993 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH, source,
994 "add mcast route, mesh seqno %d", seq);
995 rt->rt_lastmseq = seq;
998 if (IEEE80211_MESH_SEQ_GEQ(rt->rt_lastmseq, seq)) {
1001 rt->rt_lastmseq = seq;
1007 * Iterate the routing table and locate the next hop.
1009 struct ieee80211_node *
1010 ieee80211_mesh_find_txnode(struct ieee80211vap *vap,
1011 const uint8_t dest[IEEE80211_ADDR_LEN])
1013 struct ieee80211_mesh_route *rt;
1015 rt = ieee80211_mesh_rt_find(vap, dest);
1018 if ((rt->rt_flags & IEEE80211_MESHRT_FLAGS_VALID) == 0) {
1019 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH, dest,
1020 "%s: !valid, flags 0x%x", __func__, rt->rt_flags);
1024 if (rt->rt_flags & IEEE80211_MESHRT_FLAGS_PROXY) {
1025 rt = ieee80211_mesh_rt_find(vap, rt->rt_mesh_gate);
1026 if (rt == NULL) return NULL;
1027 if ((rt->rt_flags & IEEE80211_MESHRT_FLAGS_VALID) == 0) {
1028 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH, dest,
1029 "%s: meshgate !valid, flags 0x%x", __func__,
1035 return ieee80211_find_txnode(vap, rt->rt_nexthop);
1039 mesh_transmit_to_gate(struct ieee80211vap *vap, struct mbuf *m,
1040 struct ieee80211_mesh_route *rt_gate)
1042 struct ifnet *ifp = vap->iv_ifp;
1043 struct ieee80211com *ic = vap->iv_ic;
1044 struct ifnet *parent = ic->ic_ifp;
1045 struct ieee80211_node *ni;
1046 struct ether_header *eh;
1049 eh = mtod(m, struct ether_header *);
1050 ni = ieee80211_mesh_find_txnode(vap, rt_gate->rt_dest);
1057 if ((ni->ni_flags & IEEE80211_NODE_PWR_MGT) &&
1058 (m->m_flags & M_PWR_SAV) == 0) {
1060 * Station in power save mode; pass the frame
1061 * to the 802.11 layer and continue. We'll get
1062 * the frame back when the time is right.
1063 * XXX lose WDS vap linkage?
1065 (void) ieee80211_pwrsave(ni, m);
1066 ieee80211_free_node(ni);
1070 /* calculate priority so drivers can find the tx queue */
1071 if (ieee80211_classify(ni, m)) {
1072 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_OUTPUT,
1073 eh->ether_dhost, NULL,
1074 "%s", "classification failure");
1075 vap->iv_stats.is_tx_classify++;
1078 ieee80211_free_node(ni);
1082 * Stash the node pointer. Note that we do this after
1083 * any call to ieee80211_dwds_mcast because that code
1084 * uses any existing value for rcvif to identify the
1085 * interface it (might have been) received on.
1087 m->m_pkthdr.rcvif = (void *)ni;
1089 BPF_MTAP(ifp, m); /* 802.3 tx */
1092 * Check if A-MPDU tx aggregation is setup or if we
1093 * should try to enable it. The sta must be associated
1094 * with HT and A-MPDU enabled for use. When the policy
1095 * routine decides we should enable A-MPDU we issue an
1096 * ADDBA request and wait for a reply. The frame being
1097 * encapsulated will go out w/o using A-MPDU, or possibly
1098 * it might be collected by the driver and held/retransmit.
1099 * The default ic_ampdu_enable routine handles staggering
1100 * ADDBA requests in case the receiver NAK's us or we are
1101 * otherwise unable to establish a BA stream.
1103 if ((ni->ni_flags & IEEE80211_NODE_AMPDU_TX) &&
1104 (vap->iv_flags_ht & IEEE80211_FHT_AMPDU_TX) &&
1105 (m->m_flags & M_EAPOL) == 0) {
1106 int tid = WME_AC_TO_TID(M_WME_GETAC(m));
1107 struct ieee80211_tx_ampdu *tap = &ni->ni_tx_ampdu[tid];
1109 ieee80211_txampdu_count_packet(tap);
1110 if (IEEE80211_AMPDU_RUNNING(tap)) {
1112 * Operational, mark frame for aggregation.
1114 * XXX do tx aggregation here
1116 m->m_flags |= M_AMPDU_MPDU;
1117 } else if (!IEEE80211_AMPDU_REQUESTED(tap) &&
1118 ic->ic_ampdu_enable(ni, tap)) {
1120 * Not negotiated yet, request service.
1122 ieee80211_ampdu_request(ni, tap);
1123 /* XXX hold frame for reply? */
1126 #ifdef IEEE80211_SUPPORT_SUPERG
1127 else if (IEEE80211_ATH_CAP(vap, ni, IEEE80211_NODE_FF)) {
1128 m = ieee80211_ff_check(ni, m);
1130 /* NB: any ni ref held on stageq */
1134 #endif /* IEEE80211_SUPPORT_SUPERG */
1135 if (__predict_true((vap->iv_caps & IEEE80211_C_8023ENCAP) == 0)) {
1137 * Encapsulate the packet in prep for transmission.
1139 m = ieee80211_encap(vap, ni, m);
1141 /* NB: stat+msg handled in ieee80211_encap */
1142 ieee80211_free_node(ni);
1146 error = parent->if_transmit(parent, m);
1149 ieee80211_free_node(ni);
1153 ic->ic_lastdata = ticks;
1157 * Forward the queued frames to known valid mesh gates.
1158 * Assume destination to be outside the MBSS (i.e. proxy entry),
1159 * If no valid mesh gates are known silently discard queued frames.
1160 * After transmitting frames to all known valid mesh gates, this route
1161 * will be marked invalid, and a new path discovery will happen in the hopes
1162 * that (at least) one of the mesh gates have a new proxy entry for us to use.
1165 ieee80211_mesh_forward_to_gates(struct ieee80211vap *vap,
1166 struct ieee80211_mesh_route *rt_dest)
1168 struct ieee80211com *ic = vap->iv_ic;
1169 struct ieee80211_mesh_state *ms = vap->iv_mesh;
1170 struct ieee80211_mesh_route *rt_gate;
1171 struct ieee80211_mesh_gate_route *gr = NULL, *gr_next;
1172 struct mbuf *m, *mcopy, *next;
1174 KASSERT( rt_dest->rt_flags == IEEE80211_MESHRT_FLAGS_DISCOVER,
1175 ("Route is not marked with IEEE80211_MESHRT_FLAGS_DISCOVER"));
1177 /* XXX: send to more than one valid mash gate */
1180 m = ieee80211_ageq_remove(&ic->ic_stageq,
1181 (struct ieee80211_node *)(uintptr_t)
1182 ieee80211_mac_hash(ic, rt_dest->rt_dest));
1184 TAILQ_FOREACH_SAFE(gr, &ms->ms_known_gates, gr_next, gr_next) {
1185 rt_gate = gr->gr_route;
1186 if (rt_gate == NULL) {
1187 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_HWMP,
1189 "mesh gate with no path %6D",
1193 if ((rt_gate->rt_flags & IEEE80211_MESHRT_FLAGS_VALID) == 0)
1195 KASSERT(rt_gate->rt_flags & IEEE80211_MESHRT_FLAGS_GATE,
1196 ("route not marked as a mesh gate"));
1197 KASSERT((rt_gate->rt_flags &
1198 IEEE80211_MESHRT_FLAGS_PROXY) == 0,
1199 ("found mesh gate that is also marked porxy"));
1201 * convert route to a proxy route gated by the current
1202 * mesh gate, this is needed so encap can built data
1203 * frame with correct address.
1205 rt_dest->rt_flags = IEEE80211_MESHRT_FLAGS_PROXY |
1206 IEEE80211_MESHRT_FLAGS_VALID;
1207 rt_dest->rt_ext_seq = 1; /* random value */
1208 IEEE80211_ADDR_COPY(rt_dest->rt_mesh_gate, rt_gate->rt_dest);
1209 IEEE80211_ADDR_COPY(rt_dest->rt_nexthop, rt_gate->rt_nexthop);
1210 rt_dest->rt_metric = rt_gate->rt_metric;
1211 rt_dest->rt_nhops = rt_gate->rt_nhops;
1212 ieee80211_mesh_rt_update(rt_dest, ms->ms_ppath->mpp_inact);
1215 mcopy = m_dup(m, M_NOWAIT);
1216 for (; mcopy != NULL; mcopy = next) {
1217 next = mcopy->m_nextpkt;
1218 mcopy->m_nextpkt = NULL;
1219 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_HWMP,
1221 "flush queued frame %p len %d", mcopy,
1222 mcopy->m_pkthdr.len);
1223 mesh_transmit_to_gate(vap, mcopy, rt_gate);
1227 rt_dest->rt_flags = 0; /* Mark invalid */
1233 * Forward the specified frame.
1234 * Decrement the TTL and set TA to our MAC address.
1237 mesh_forward(struct ieee80211vap *vap, struct mbuf *m,
1238 const struct ieee80211_meshcntl *mc)
1240 struct ieee80211com *ic = vap->iv_ic;
1241 struct ieee80211_mesh_state *ms = vap->iv_mesh;
1242 struct ifnet *ifp = vap->iv_ifp;
1243 struct ifnet *parent = ic->ic_ifp;
1244 const struct ieee80211_frame *wh =
1245 mtod(m, const struct ieee80211_frame *);
1247 struct ieee80211_meshcntl *mccopy;
1248 struct ieee80211_frame *whcopy;
1249 struct ieee80211_node *ni;
1253 * mesh ttl of 1 means we are the last one receving it,
1254 * according to amendment we decrement and then check if
1255 * 0, if so we dont forward.
1257 if (mc->mc_ttl < 1) {
1258 IEEE80211_NOTE_FRAME(vap, IEEE80211_MSG_MESH, wh,
1259 "%s", "frame not fwd'd, ttl 1");
1260 vap->iv_stats.is_mesh_fwd_ttl++;
1263 if (!(ms->ms_flags & IEEE80211_MESHFLAGS_FWD)) {
1264 IEEE80211_NOTE_FRAME(vap, IEEE80211_MSG_MESH, wh,
1265 "%s", "frame not fwd'd, fwding disabled");
1266 vap->iv_stats.is_mesh_fwd_disabled++;
1269 mcopy = m_dup(m, M_NOWAIT);
1270 if (mcopy == NULL) {
1271 IEEE80211_NOTE_FRAME(vap, IEEE80211_MSG_MESH, wh,
1272 "%s", "frame not fwd'd, cannot dup");
1273 vap->iv_stats.is_mesh_fwd_nobuf++;
1277 mcopy = m_pullup(mcopy, ieee80211_hdrspace(ic, wh) +
1278 sizeof(struct ieee80211_meshcntl));
1279 if (mcopy == NULL) {
1280 IEEE80211_NOTE_FRAME(vap, IEEE80211_MSG_MESH, wh,
1281 "%s", "frame not fwd'd, too short");
1282 vap->iv_stats.is_mesh_fwd_tooshort++;
1287 whcopy = mtod(mcopy, struct ieee80211_frame *);
1288 mccopy = (struct ieee80211_meshcntl *)
1289 (mtod(mcopy, uint8_t *) + ieee80211_hdrspace(ic, wh));
1290 /* XXX clear other bits? */
1291 whcopy->i_fc[1] &= ~IEEE80211_FC1_RETRY;
1292 IEEE80211_ADDR_COPY(whcopy->i_addr2, vap->iv_myaddr);
1293 if (IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1294 ni = ieee80211_ref_node(vap->iv_bss);
1295 mcopy->m_flags |= M_MCAST;
1297 ni = ieee80211_mesh_find_txnode(vap, whcopy->i_addr3);
1300 * [Optional] any of the following three actions:
1301 * o silently discard
1302 * o trigger a path discovery
1303 * o inform TA that meshDA is unknown.
1305 IEEE80211_NOTE_FRAME(vap, IEEE80211_MSG_MESH, wh,
1306 "%s", "frame not fwd'd, no path");
1307 ms->ms_ppath->mpp_senderror(vap, whcopy->i_addr3, NULL,
1308 IEEE80211_REASON_MESH_PERR_NO_FI);
1309 vap->iv_stats.is_mesh_fwd_nopath++;
1313 IEEE80211_ADDR_COPY(whcopy->i_addr1, ni->ni_macaddr);
1315 KASSERT(mccopy->mc_ttl > 0, ("%s called with wrong ttl", __func__));
1318 /* XXX calculate priority so drivers can find the tx queue */
1319 M_WME_SETAC(mcopy, WME_AC_BE);
1321 /* XXX do we know m_nextpkt is NULL? */
1322 mcopy->m_pkthdr.rcvif = (void *) ni;
1323 err = parent->if_transmit(parent, mcopy);
1325 /* NB: IFQ_HANDOFF reclaims mbuf */
1326 ieee80211_free_node(ni);
1332 static struct mbuf *
1333 mesh_decap(struct ieee80211vap *vap, struct mbuf *m, int hdrlen, int meshdrlen)
1335 #define WHDIR(wh) ((wh)->i_fc[1] & IEEE80211_FC1_DIR_MASK)
1336 #define MC01(mc) ((const struct ieee80211_meshcntl_ae01 *)mc)
1337 uint8_t b[sizeof(struct ieee80211_qosframe_addr4) +
1338 sizeof(struct ieee80211_meshcntl_ae10)];
1339 const struct ieee80211_qosframe_addr4 *wh;
1340 const struct ieee80211_meshcntl_ae10 *mc;
1341 struct ether_header *eh;
1345 if (m->m_len < hdrlen + sizeof(*llc) &&
1346 (m = m_pullup(m, hdrlen + sizeof(*llc))) == NULL) {
1347 IEEE80211_DPRINTF(vap, IEEE80211_MSG_ANY,
1348 "discard data frame: %s", "m_pullup failed");
1349 vap->iv_stats.is_rx_tooshort++;
1352 memcpy(b, mtod(m, caddr_t), hdrlen);
1353 wh = (const struct ieee80211_qosframe_addr4 *)&b[0];
1354 mc = (const struct ieee80211_meshcntl_ae10 *)&b[hdrlen - meshdrlen];
1355 KASSERT(WHDIR(wh) == IEEE80211_FC1_DIR_FROMDS ||
1356 WHDIR(wh) == IEEE80211_FC1_DIR_DSTODS,
1357 ("bogus dir, fc 0x%x:0x%x", wh->i_fc[0], wh->i_fc[1]));
1359 llc = (struct llc *)(mtod(m, caddr_t) + hdrlen);
1360 if (llc->llc_dsap == LLC_SNAP_LSAP && llc->llc_ssap == LLC_SNAP_LSAP &&
1361 llc->llc_control == LLC_UI && llc->llc_snap.org_code[0] == 0 &&
1362 llc->llc_snap.org_code[1] == 0 && llc->llc_snap.org_code[2] == 0 &&
1363 /* NB: preserve AppleTalk frames that have a native SNAP hdr */
1364 !(llc->llc_snap.ether_type == htons(ETHERTYPE_AARP) ||
1365 llc->llc_snap.ether_type == htons(ETHERTYPE_IPX))) {
1366 m_adj(m, hdrlen + sizeof(struct llc) - sizeof(*eh));
1369 m_adj(m, hdrlen - sizeof(*eh));
1371 eh = mtod(m, struct ether_header *);
1372 ae = mc->mc_flags & IEEE80211_MESH_AE_MASK;
1373 if (WHDIR(wh) == IEEE80211_FC1_DIR_FROMDS) {
1374 IEEE80211_ADDR_COPY(eh->ether_dhost, wh->i_addr1);
1375 if (ae == IEEE80211_MESH_AE_00) {
1376 IEEE80211_ADDR_COPY(eh->ether_shost, wh->i_addr3);
1377 } else if (ae == IEEE80211_MESH_AE_01) {
1378 IEEE80211_ADDR_COPY(eh->ether_shost,
1379 MC01(mc)->mc_addr4);
1381 IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
1382 (const struct ieee80211_frame *)wh, NULL,
1384 vap->iv_stats.is_mesh_badae++;
1389 if (ae == IEEE80211_MESH_AE_00) {
1390 IEEE80211_ADDR_COPY(eh->ether_dhost, wh->i_addr3);
1391 IEEE80211_ADDR_COPY(eh->ether_shost, wh->i_addr4);
1392 } else if (ae == IEEE80211_MESH_AE_10) {
1393 IEEE80211_ADDR_COPY(eh->ether_dhost, mc->mc_addr5);
1394 IEEE80211_ADDR_COPY(eh->ether_shost, mc->mc_addr6);
1396 IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
1397 (const struct ieee80211_frame *)wh, NULL,
1399 vap->iv_stats.is_mesh_badae++;
1404 #ifdef ALIGNED_POINTER
1405 if (!ALIGNED_POINTER(mtod(m, caddr_t) + sizeof(*eh), uint32_t)) {
1406 m = ieee80211_realign(vap, m, sizeof(*eh));
1410 #endif /* ALIGNED_POINTER */
1412 eh = mtod(m, struct ether_header *);
1413 eh->ether_type = htons(m->m_pkthdr.len - sizeof(*eh));
1421 * Return non-zero if the unicast mesh data frame should be processed
1422 * locally. Frames that are not proxy'd have our address, otherwise
1423 * we need to consult the routing table to look for a proxy entry.
1426 mesh_isucastforme(struct ieee80211vap *vap, const struct ieee80211_frame *wh,
1427 const struct ieee80211_meshcntl *mc)
1429 int ae = mc->mc_flags & 3;
1431 KASSERT((wh->i_fc[1] & IEEE80211_FC1_DIR_MASK) == IEEE80211_FC1_DIR_DSTODS,
1432 ("bad dir 0x%x:0x%x", wh->i_fc[0], wh->i_fc[1]));
1433 KASSERT(ae == IEEE80211_MESH_AE_00 || ae == IEEE80211_MESH_AE_10,
1435 if (ae == IEEE80211_MESH_AE_10) { /* ucast w/ proxy */
1436 const struct ieee80211_meshcntl_ae10 *mc10 =
1437 (const struct ieee80211_meshcntl_ae10 *) mc;
1438 struct ieee80211_mesh_route *rt =
1439 ieee80211_mesh_rt_find(vap, mc10->mc_addr5);
1440 /* check for proxy route to ourself */
1441 return (rt != NULL &&
1442 (rt->rt_flags & IEEE80211_MESHRT_FLAGS_PROXY));
1443 } else /* ucast w/o proxy */
1444 return IEEE80211_ADDR_EQ(wh->i_addr3, vap->iv_myaddr);
1448 * Verifies transmitter, updates lifetime, precursor list and forwards data.
1449 * > 0 means we have forwarded data and no need to process locally
1450 * == 0 means we want to process locally (and we may have forwarded data
1451 * < 0 means there was an error and data should be discarded
1454 mesh_recv_indiv_data_to_fwrd(struct ieee80211vap *vap, struct mbuf *m,
1455 struct ieee80211_frame *wh, const struct ieee80211_meshcntl *mc)
1457 struct ieee80211_qosframe_addr4 *qwh;
1458 struct ieee80211_mesh_state *ms = vap->iv_mesh;
1459 struct ieee80211_mesh_route *rt_meshda, *rt_meshsa;
1461 qwh = (struct ieee80211_qosframe_addr4 *)wh;
1465 * o verify addr2 is a legitimate transmitter
1466 * o lifetime of precursor of addr3 (addr2) is max(init, curr)
1467 * o lifetime of precursor of addr4 (nexthop) is max(init, curr)
1470 /* set lifetime of addr3 (meshDA) to initial value */
1471 rt_meshda = ieee80211_mesh_rt_find(vap, qwh->i_addr3);
1472 if (rt_meshda == NULL) {
1473 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH, qwh->i_addr2,
1474 "no route to meshDA(%6D)", qwh->i_addr3, ":");
1476 * [Optional] any of the following three actions:
1477 * o silently discard [X]
1478 * o trigger a path discovery [ ]
1479 * o inform TA that meshDA is unknown. [ ]
1485 ieee80211_mesh_rt_update(rt_meshda, ticks_to_msecs(
1486 ms->ms_ppath->mpp_inact));
1488 /* set lifetime of addr4 (meshSA) to initial value */
1489 rt_meshsa = ieee80211_mesh_rt_find(vap, qwh->i_addr4);
1490 KASSERT(rt_meshsa != NULL, ("no route"));
1491 ieee80211_mesh_rt_update(rt_meshsa, ticks_to_msecs(
1492 ms->ms_ppath->mpp_inact));
1494 mesh_forward(vap, m, mc);
1495 return (1); /* dont process locally */
1499 * Verifies transmitter, updates lifetime, precursor list and process data
1500 * locally, if data is proxy with AE = 10 it could mean data should go
1501 * on another mesh path or data should be forwarded to the DS.
1503 * > 0 means we have forwarded data and no need to process locally
1504 * == 0 means we want to process locally (and we may have forwarded data
1505 * < 0 means there was an error and data should be discarded
1508 mesh_recv_indiv_data_to_me(struct ieee80211vap *vap, struct mbuf *m,
1509 struct ieee80211_frame *wh, const struct ieee80211_meshcntl *mc)
1511 struct ieee80211_qosframe_addr4 *qwh;
1512 const struct ieee80211_meshcntl_ae10 *mc10;
1513 struct ieee80211_mesh_state *ms = vap->iv_mesh;
1514 struct ieee80211_mesh_route *rt;
1517 qwh = (struct ieee80211_qosframe_addr4 *)wh;
1518 mc10 = (const struct ieee80211_meshcntl_ae10 *)mc;
1522 * o verify addr2 is a legitimate transmitter
1523 * o lifetime of precursor entry is max(init, curr)
1526 /* set lifetime of addr4 (meshSA) to initial value */
1527 rt = ieee80211_mesh_rt_find(vap, qwh->i_addr4);
1528 KASSERT(rt != NULL, ("no route"));
1529 ieee80211_mesh_rt_update(rt, ticks_to_msecs(ms->ms_ppath->mpp_inact));
1532 ae = mc10->mc_flags & IEEE80211_MESH_AE_MASK;
1533 KASSERT(ae == IEEE80211_MESH_AE_00 ||
1534 ae == IEEE80211_MESH_AE_10, ("bad AE %d", ae));
1535 if (ae == IEEE80211_MESH_AE_10) {
1536 if (IEEE80211_ADDR_EQ(mc10->mc_addr5, qwh->i_addr3)) {
1537 return (0); /* process locally */
1540 rt = ieee80211_mesh_rt_find(vap, mc10->mc_addr5);
1542 (rt->rt_flags & IEEE80211_MESHRT_FLAGS_VALID) &&
1543 (rt->rt_flags & IEEE80211_MESHRT_FLAGS_PROXY) == 0) {
1545 * Forward on another mesh-path, according to
1546 * amendment as specified in 9.32.4.1
1548 IEEE80211_ADDR_COPY(qwh->i_addr3, mc10->mc_addr5);
1549 mesh_forward(vap, m,
1550 (const struct ieee80211_meshcntl *)mc10);
1551 return (1); /* dont process locally */
1554 * All other cases: forward of MSDUs from the MBSS to DS indiv.
1555 * addressed according to 13.11.3.2.
1557 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_OUTPUT, qwh->i_addr2,
1558 "forward frame to DS, SA(%6D) DA(%6D)",
1559 mc10->mc_addr6, ":", mc10->mc_addr5, ":");
1561 return (0); /* process locally */
1565 * Try to forward the group addressed data on to other mesh STAs, and
1568 * > 0 means we have forwarded data and no need to process locally
1569 * == 0 means we want to process locally (and we may have forwarded data
1570 * < 0 means there was an error and data should be discarded
1573 mesh_recv_group_data(struct ieee80211vap *vap, struct mbuf *m,
1574 struct ieee80211_frame *wh, const struct ieee80211_meshcntl *mc)
1576 #define MC01(mc) ((const struct ieee80211_meshcntl_ae01 *)mc)
1577 struct ieee80211_mesh_state *ms = vap->iv_mesh;
1579 mesh_forward(vap, m, mc);
1581 if(mc->mc_ttl > 0) {
1582 if (mc->mc_flags & IEEE80211_MESH_AE_01) {
1584 * Forward of MSDUs from the MBSS to DS group addressed
1585 * (according to 13.11.3.2)
1586 * This happens by delivering the packet, and a bridge
1587 * will sent it on another port member.
1589 if (ms->ms_flags & IEEE80211_MESHFLAGS_GATE &&
1590 ms->ms_flags & IEEE80211_MESHFLAGS_FWD)
1591 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH,
1592 MC01(mc)->mc_addr4, "%s",
1593 "forward from MBSS to the DS");
1596 return (0); /* process locally */
1601 mesh_input(struct ieee80211_node *ni, struct mbuf *m, int rssi, int nf)
1603 #define HAS_SEQ(type) ((type & 0x4) == 0)
1604 #define MC01(mc) ((const struct ieee80211_meshcntl_ae01 *)mc)
1605 #define MC10(mc) ((const struct ieee80211_meshcntl_ae10 *)mc)
1606 struct ieee80211vap *vap = ni->ni_vap;
1607 struct ieee80211com *ic = ni->ni_ic;
1608 struct ifnet *ifp = vap->iv_ifp;
1609 struct ieee80211_frame *wh;
1610 const struct ieee80211_meshcntl *mc;
1611 int hdrspace, meshdrlen, need_tap, error;
1612 uint8_t dir, type, subtype, ae;
1614 const uint8_t *addr;
1616 ieee80211_seq rxseq;
1618 KASSERT(ni != NULL, ("null node"));
1619 ni->ni_inact = ni->ni_inact_reload;
1621 need_tap = 1; /* mbuf need to be tapped. */
1622 type = -1; /* undefined */
1624 if (m->m_pkthdr.len < sizeof(struct ieee80211_frame_min)) {
1625 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
1626 ni->ni_macaddr, NULL,
1627 "too short (1): len %u", m->m_pkthdr.len);
1628 vap->iv_stats.is_rx_tooshort++;
1632 * Bit of a cheat here, we use a pointer for a 3-address
1633 * frame format but don't reference fields past outside
1634 * ieee80211_frame_min w/o first validating the data is
1637 wh = mtod(m, struct ieee80211_frame *);
1639 if ((wh->i_fc[0] & IEEE80211_FC0_VERSION_MASK) !=
1640 IEEE80211_FC0_VERSION_0) {
1641 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
1642 ni->ni_macaddr, NULL, "wrong version %x", wh->i_fc[0]);
1643 vap->iv_stats.is_rx_badversion++;
1646 dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK;
1647 type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
1648 subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
1649 if ((ic->ic_flags & IEEE80211_F_SCAN) == 0) {
1650 IEEE80211_RSSI_LPF(ni->ni_avgrssi, rssi);
1652 if (HAS_SEQ(type)) {
1653 uint8_t tid = ieee80211_gettid(wh);
1655 if (IEEE80211_QOS_HAS_SEQ(wh) &&
1656 TID_TO_WME_AC(tid) >= WME_AC_VI)
1657 ic->ic_wme.wme_hipri_traffic++;
1658 rxseq = le16toh(*(uint16_t *)wh->i_seq);
1659 if (! ieee80211_check_rxseq(ni, wh)) {
1660 /* duplicate, discard */
1661 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
1662 wh->i_addr1, "duplicate",
1663 "seqno <%u,%u> fragno <%u,%u> tid %u",
1664 rxseq >> IEEE80211_SEQ_SEQ_SHIFT,
1665 ni->ni_rxseqs[tid] >>
1666 IEEE80211_SEQ_SEQ_SHIFT,
1667 rxseq & IEEE80211_SEQ_FRAG_MASK,
1668 ni->ni_rxseqs[tid] &
1669 IEEE80211_SEQ_FRAG_MASK,
1671 vap->iv_stats.is_rx_dup++;
1672 IEEE80211_NODE_STAT(ni, rx_dup);
1675 ni->ni_rxseqs[tid] = rxseq;
1678 #ifdef IEEE80211_DEBUG
1680 * It's easier, but too expensive, to simulate different mesh
1681 * topologies by consulting the ACL policy very early, so do this
1684 * NB: this check is also done upon peering link initiation.
1686 if (vap->iv_acl != NULL && !vap->iv_acl->iac_check(vap, wh)) {
1687 IEEE80211_DISCARD(vap, IEEE80211_MSG_ACL,
1688 wh, NULL, "%s", "disallowed by ACL");
1689 vap->iv_stats.is_rx_acl++;
1694 case IEEE80211_FC0_TYPE_DATA:
1695 if (ni == vap->iv_bss)
1697 if (ni->ni_mlstate != IEEE80211_NODE_MESH_ESTABLISHED) {
1698 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_MESH,
1699 ni->ni_macaddr, NULL,
1700 "peer link not yet established (%d)",
1702 vap->iv_stats.is_mesh_nolink++;
1705 if (dir != IEEE80211_FC1_DIR_FROMDS &&
1706 dir != IEEE80211_FC1_DIR_DSTODS) {
1707 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
1708 wh, "data", "incorrect dir 0x%x", dir);
1709 vap->iv_stats.is_rx_wrongdir++;
1713 /* All Mesh data frames are QoS subtype */
1714 if (!HAS_SEQ(type)) {
1715 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
1716 wh, "data", "incorrect subtype 0x%x", subtype);
1717 vap->iv_stats.is_rx_badsubtype++;
1722 * Next up, any fragmentation.
1723 * XXX: we defrag before we even try to forward,
1724 * Mesh Control field is not present in sub-sequent
1725 * fragmented frames. This is in contrast to Draft 4.0.
1727 hdrspace = ieee80211_hdrspace(ic, wh);
1728 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1729 m = ieee80211_defrag(ni, m, hdrspace);
1731 /* Fragment dropped or frame not complete yet */
1735 wh = mtod(m, struct ieee80211_frame *); /* NB: after defrag */
1738 * Now we have a complete Mesh Data frame.
1742 * Only fromDStoDS data frames use 4 address qos frames
1743 * as specified in amendment. Otherwise addr4 is located
1744 * in the Mesh Control field and a 3 address qos frame
1747 if (IEEE80211_IS_DSTODS(wh))
1748 *(uint16_t *)qos = *(uint16_t *)
1749 ((struct ieee80211_qosframe_addr4 *)wh)->i_qos;
1751 *(uint16_t *)qos = *(uint16_t *)
1752 ((struct ieee80211_qosframe *)wh)->i_qos;
1755 * NB: The mesh STA sets the Mesh Control Present
1756 * subfield to 1 in the Mesh Data frame containing
1757 * an unfragmented MSDU, an A-MSDU, or the first
1758 * fragment of an MSDU.
1759 * After defrag it should always be present.
1761 if (!(qos[1] & IEEE80211_QOS_MC)) {
1762 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_MESH,
1763 ni->ni_macaddr, NULL,
1764 "%s", "Mesh control field not present");
1765 vap->iv_stats.is_rx_elem_missing++; /* XXX: kinda */
1769 /* pull up enough to get to the mesh control */
1770 if (m->m_len < hdrspace + sizeof(struct ieee80211_meshcntl) &&
1771 (m = m_pullup(m, hdrspace +
1772 sizeof(struct ieee80211_meshcntl))) == NULL) {
1773 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
1774 ni->ni_macaddr, NULL,
1775 "data too short: expecting %u", hdrspace);
1776 vap->iv_stats.is_rx_tooshort++;
1780 * Now calculate the full extent of the headers. Note
1781 * mesh_decap will pull up anything we didn't get
1782 * above when it strips the 802.11 headers.
1784 mc = (const struct ieee80211_meshcntl *)
1785 (mtod(m, const uint8_t *) + hdrspace);
1786 ae = mc->mc_flags & IEEE80211_MESH_AE_MASK;
1787 meshdrlen = sizeof(struct ieee80211_meshcntl) +
1788 ae * IEEE80211_ADDR_LEN;
1789 hdrspace += meshdrlen;
1791 /* pull complete hdrspace = ieee80211_hdrspace + meshcontrol */
1792 if ((meshdrlen > sizeof(struct ieee80211_meshcntl)) &&
1793 (m->m_len < hdrspace) &&
1794 ((m = m_pullup(m, hdrspace)) == NULL)) {
1795 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
1796 ni->ni_macaddr, NULL,
1797 "data too short: expecting %u", hdrspace);
1798 vap->iv_stats.is_rx_tooshort++;
1801 /* XXX: are we sure there is no reallocating after m_pullup? */
1803 seq = LE_READ_4(mc->mc_seq);
1804 if (IEEE80211_IS_MULTICAST(wh->i_addr1))
1806 else if (ae == IEEE80211_MESH_AE_01)
1807 addr = MC01(mc)->mc_addr4;
1809 addr = ((struct ieee80211_qosframe_addr4 *)wh)->i_addr4;
1810 if (IEEE80211_ADDR_EQ(vap->iv_myaddr, addr)) {
1811 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
1812 addr, "data", "%s", "not to me");
1813 vap->iv_stats.is_rx_wrongbss++; /* XXX kinda */
1816 if (mesh_checkpseq(vap, addr, seq) != 0) {
1817 vap->iv_stats.is_rx_dup++;
1821 /* This code "routes" the frame to the right control path */
1822 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1823 if (IEEE80211_ADDR_EQ(vap->iv_myaddr, wh->i_addr3))
1825 mesh_recv_indiv_data_to_me(vap, m, wh, mc);
1826 else if (IEEE80211_IS_MULTICAST(wh->i_addr3))
1827 error = mesh_recv_group_data(vap, m, wh, mc);
1829 error = mesh_recv_indiv_data_to_fwrd(vap, m,
1832 error = mesh_recv_group_data(vap, m, wh, mc);
1838 if (ieee80211_radiotap_active_vap(vap))
1839 ieee80211_radiotap_rx(vap, m);
1843 * Finally, strip the 802.11 header.
1845 m = mesh_decap(vap, m, hdrspace, meshdrlen);
1847 /* XXX mask bit to check for both */
1848 /* don't count Null data frames as errors */
1849 if (subtype == IEEE80211_FC0_SUBTYPE_NODATA ||
1850 subtype == IEEE80211_FC0_SUBTYPE_QOS_NULL)
1852 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
1853 ni->ni_macaddr, "data", "%s", "decap error");
1854 vap->iv_stats.is_rx_decap++;
1855 IEEE80211_NODE_STAT(ni, rx_decap);
1858 if (qos[0] & IEEE80211_QOS_AMSDU) {
1859 m = ieee80211_decap_amsdu(ni, m);
1861 return IEEE80211_FC0_TYPE_DATA;
1863 ieee80211_deliver_data(vap, ni, m);
1865 case IEEE80211_FC0_TYPE_MGT:
1866 vap->iv_stats.is_rx_mgmt++;
1867 IEEE80211_NODE_STAT(ni, rx_mgmt);
1868 if (dir != IEEE80211_FC1_DIR_NODS) {
1869 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
1870 wh, "mgt", "incorrect dir 0x%x", dir);
1871 vap->iv_stats.is_rx_wrongdir++;
1874 if (m->m_pkthdr.len < sizeof(struct ieee80211_frame)) {
1875 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
1876 ni->ni_macaddr, "mgt", "too short: len %u",
1878 vap->iv_stats.is_rx_tooshort++;
1881 #ifdef IEEE80211_DEBUG
1882 if ((ieee80211_msg_debug(vap) &&
1883 (vap->iv_ic->ic_flags & IEEE80211_F_SCAN)) ||
1884 ieee80211_msg_dumppkts(vap)) {
1885 if_printf(ifp, "received %s from %s rssi %d\n",
1886 ieee80211_mgt_subtype_name[subtype >>
1887 IEEE80211_FC0_SUBTYPE_SHIFT],
1888 ether_sprintf(wh->i_addr2), rssi);
1891 if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
1892 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
1893 wh, NULL, "%s", "WEP set but not permitted");
1894 vap->iv_stats.is_rx_mgtdiscard++; /* XXX */
1897 vap->iv_recv_mgmt(ni, m, subtype, rssi, nf);
1899 case IEEE80211_FC0_TYPE_CTL:
1900 vap->iv_stats.is_rx_ctl++;
1901 IEEE80211_NODE_STAT(ni, rx_ctrl);
1904 IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
1905 wh, "bad", "frame type 0x%x", type);
1906 /* should not come here */
1913 if (need_tap && ieee80211_radiotap_active_vap(vap))
1914 ieee80211_radiotap_rx(vap, m);
1924 mesh_recv_mgmt(struct ieee80211_node *ni, struct mbuf *m0, int subtype,
1927 struct ieee80211vap *vap = ni->ni_vap;
1928 struct ieee80211_mesh_state *ms = vap->iv_mesh;
1929 struct ieee80211com *ic = ni->ni_ic;
1930 struct ieee80211_frame *wh;
1931 struct ieee80211_mesh_route *rt;
1932 uint8_t *frm, *efrm;
1934 wh = mtod(m0, struct ieee80211_frame *);
1935 frm = (uint8_t *)&wh[1];
1936 efrm = mtod(m0, uint8_t *) + m0->m_len;
1938 case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
1939 case IEEE80211_FC0_SUBTYPE_BEACON:
1941 struct ieee80211_scanparams scan;
1943 * We process beacon/probe response
1944 * frames to discover neighbors.
1946 if (ieee80211_parse_beacon(ni, m0, &scan) != 0)
1949 * Count frame now that we know it's to be processed.
1951 if (subtype == IEEE80211_FC0_SUBTYPE_BEACON) {
1952 vap->iv_stats.is_rx_beacon++; /* XXX remove */
1953 IEEE80211_NODE_STAT(ni, rx_beacons);
1955 IEEE80211_NODE_STAT(ni, rx_proberesp);
1957 * If scanning, just pass information to the scan module.
1959 if (ic->ic_flags & IEEE80211_F_SCAN) {
1960 if (ic->ic_flags_ext & IEEE80211_FEXT_PROBECHAN) {
1962 * Actively scanning a channel marked passive;
1963 * send a probe request now that we know there
1964 * is 802.11 traffic present.
1966 * XXX check if the beacon we recv'd gives
1967 * us what we need and suppress the probe req
1969 ieee80211_probe_curchan(vap, 1);
1970 ic->ic_flags_ext &= ~IEEE80211_FEXT_PROBECHAN;
1972 ieee80211_add_scan(vap, &scan, wh,
1977 /* The rest of this code assumes we are running */
1978 if (vap->iv_state != IEEE80211_S_RUN)
1981 * Ignore non-mesh STAs.
1984 (IEEE80211_CAPINFO_ESS|IEEE80211_CAPINFO_IBSS)) ||
1985 scan.meshid == NULL || scan.meshconf == NULL) {
1986 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
1987 wh, "beacon", "%s", "not a mesh sta");
1988 vap->iv_stats.is_mesh_wrongmesh++;
1992 * Ignore STAs for other mesh networks.
1994 if (memcmp(scan.meshid+2, ms->ms_id, ms->ms_idlen) != 0 ||
1995 mesh_verify_meshconf(vap, scan.meshconf)) {
1996 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
1997 wh, "beacon", "%s", "not for our mesh");
1998 vap->iv_stats.is_mesh_wrongmesh++;
2002 * Peer only based on the current ACL policy.
2004 if (vap->iv_acl != NULL && !vap->iv_acl->iac_check(vap, wh)) {
2005 IEEE80211_DISCARD(vap, IEEE80211_MSG_ACL,
2006 wh, NULL, "%s", "disallowed by ACL");
2007 vap->iv_stats.is_rx_acl++;
2011 * Do neighbor discovery.
2013 if (!IEEE80211_ADDR_EQ(wh->i_addr2, ni->ni_macaddr)) {
2015 * Create a new entry in the neighbor table.
2017 ni = ieee80211_add_neighbor(vap, wh, &scan);
2020 * Automatically peer with discovered nodes if possible.
2022 if (ni != vap->iv_bss &&
2023 (ms->ms_flags & IEEE80211_MESHFLAGS_AP)) {
2024 switch (ni->ni_mlstate) {
2025 case IEEE80211_NODE_MESH_IDLE:
2029 /* Wait for backoff callout to reset counter */
2030 if (ni->ni_mlhcnt >= ieee80211_mesh_maxholding)
2033 ni->ni_mlpid = mesh_generateid(vap);
2034 if (ni->ni_mlpid == 0)
2036 mesh_linkchange(ni, IEEE80211_NODE_MESH_OPENSNT);
2037 args[0] = ni->ni_mlpid;
2038 ieee80211_send_action(ni,
2039 IEEE80211_ACTION_CAT_SELF_PROT,
2040 IEEE80211_ACTION_MESHPEERING_OPEN, args);
2042 mesh_peer_timeout_setup(ni);
2045 case IEEE80211_NODE_MESH_ESTABLISHED:
2048 * Valid beacon from a peer mesh STA
2051 rt = ieee80211_mesh_rt_find(vap, wh->i_addr2);
2053 ieee80211_mesh_rt_update(rt,
2055 ms->ms_ppath->mpp_inact));
2065 case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
2067 uint8_t *ssid, *meshid, *rates, *xrates;
2070 if (vap->iv_state != IEEE80211_S_RUN) {
2071 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
2072 wh, NULL, "wrong state %s",
2073 ieee80211_state_name[vap->iv_state]);
2074 vap->iv_stats.is_rx_mgtdiscard++;
2077 if (IEEE80211_IS_MULTICAST(wh->i_addr2)) {
2078 /* frame must be directed */
2079 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
2080 wh, NULL, "%s", "not unicast");
2081 vap->iv_stats.is_rx_mgtdiscard++; /* XXX stat */
2085 * prreq frame format
2087 * [tlv] supported rates
2088 * [tlv] extended supported rates
2091 ssid = meshid = rates = xrates = NULL;
2093 while (efrm - frm > 1) {
2094 IEEE80211_VERIFY_LENGTH(efrm - frm, frm[1] + 2, return);
2096 case IEEE80211_ELEMID_SSID:
2099 case IEEE80211_ELEMID_RATES:
2102 case IEEE80211_ELEMID_XRATES:
2105 case IEEE80211_ELEMID_MESHID:
2111 IEEE80211_VERIFY_ELEMENT(ssid, IEEE80211_NWID_LEN, return);
2112 IEEE80211_VERIFY_ELEMENT(rates, IEEE80211_RATE_MAXSIZE, return);
2114 IEEE80211_VERIFY_ELEMENT(xrates,
2115 IEEE80211_RATE_MAXSIZE - rates[1], return);
2116 if (meshid != NULL) {
2117 IEEE80211_VERIFY_ELEMENT(meshid,
2118 IEEE80211_MESHID_LEN, return);
2119 /* NB: meshid, not ssid */
2120 IEEE80211_VERIFY_SSID(vap->iv_bss, meshid, return);
2123 /* XXX find a better class or define it's own */
2124 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_INPUT, wh->i_addr2,
2125 "%s", "recv probe req");
2127 * Some legacy 11b clients cannot hack a complete
2128 * probe response frame. When the request includes
2129 * only a bare-bones rate set, communicate this to
2130 * the transmit side.
2132 ieee80211_send_proberesp(vap, wh->i_addr2, 0);
2136 case IEEE80211_FC0_SUBTYPE_ACTION:
2137 case IEEE80211_FC0_SUBTYPE_ACTION_NOACK:
2138 if (ni == vap->iv_bss) {
2139 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
2140 wh, NULL, "%s", "unknown node");
2141 vap->iv_stats.is_rx_mgtdiscard++;
2142 } else if (!IEEE80211_ADDR_EQ(vap->iv_myaddr, wh->i_addr1) &&
2143 !IEEE80211_IS_MULTICAST(wh->i_addr1)) {
2144 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
2145 wh, NULL, "%s", "not for us");
2146 vap->iv_stats.is_rx_mgtdiscard++;
2147 } else if (vap->iv_state != IEEE80211_S_RUN) {
2148 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
2149 wh, NULL, "wrong state %s",
2150 ieee80211_state_name[vap->iv_state]);
2151 vap->iv_stats.is_rx_mgtdiscard++;
2153 if (ieee80211_parse_action(ni, m0) == 0)
2154 (void)ic->ic_recv_action(ni, wh, frm, efrm);
2158 case IEEE80211_FC0_SUBTYPE_ASSOC_REQ:
2159 case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
2160 case IEEE80211_FC0_SUBTYPE_REASSOC_REQ:
2161 case IEEE80211_FC0_SUBTYPE_REASSOC_RESP:
2162 case IEEE80211_FC0_SUBTYPE_ATIM:
2163 case IEEE80211_FC0_SUBTYPE_DISASSOC:
2164 case IEEE80211_FC0_SUBTYPE_AUTH:
2165 case IEEE80211_FC0_SUBTYPE_DEAUTH:
2166 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
2167 wh, NULL, "%s", "not handled");
2168 vap->iv_stats.is_rx_mgtdiscard++;
2172 IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
2173 wh, "mgt", "subtype 0x%x not handled", subtype);
2174 vap->iv_stats.is_rx_badsubtype++;
2180 mesh_recv_ctl(struct ieee80211_node *ni, struct mbuf *m, int subtype)
2184 case IEEE80211_FC0_SUBTYPE_BAR:
2185 ieee80211_recv_bar(ni, m);
2191 * Parse meshpeering action ie's for MPM frames
2193 static const struct ieee80211_meshpeer_ie *
2194 mesh_parse_meshpeering_action(struct ieee80211_node *ni,
2195 const struct ieee80211_frame *wh, /* XXX for VERIFY_LENGTH */
2196 const uint8_t *frm, const uint8_t *efrm,
2197 struct ieee80211_meshpeer_ie *mp, uint8_t subtype)
2199 struct ieee80211vap *vap = ni->ni_vap;
2200 const struct ieee80211_meshpeer_ie *mpie;
2202 const uint8_t *meshid, *meshconf, *meshpeer;
2203 uint8_t sendclose = 0; /* 1 = MPM frame rejected, close will be sent */
2205 meshid = meshconf = meshpeer = NULL;
2206 while (efrm - frm > 1) {
2207 IEEE80211_VERIFY_LENGTH(efrm - frm, frm[1] + 2, return NULL);
2209 case IEEE80211_ELEMID_MESHID:
2212 case IEEE80211_ELEMID_MESHCONF:
2215 case IEEE80211_ELEMID_MESHPEER:
2217 mpie = (const struct ieee80211_meshpeer_ie *) frm;
2218 memset(mp, 0, sizeof(*mp));
2219 mp->peer_len = mpie->peer_len;
2220 mp->peer_proto = LE_READ_2(&mpie->peer_proto);
2221 mp->peer_llinkid = LE_READ_2(&mpie->peer_llinkid);
2223 case IEEE80211_ACTION_MESHPEERING_CONFIRM:
2225 LE_READ_2(&mpie->peer_linkid);
2227 case IEEE80211_ACTION_MESHPEERING_CLOSE:
2228 /* NB: peer link ID is optional */
2229 if (mpie->peer_len ==
2230 (IEEE80211_MPM_BASE_SZ + 2)) {
2231 mp->peer_linkid = 0;
2233 LE_READ_2(&mpie->peer_linkid);
2236 LE_READ_2(&mpie->peer_linkid);
2238 LE_READ_2(&mpie->peer_rcode);
2248 * Verify the contents of the frame.
2249 * If it fails validation, close the peer link.
2251 if (mesh_verify_meshpeer(vap, subtype, (const uint8_t *)mp)) {
2253 IEEE80211_DISCARD(vap,
2254 IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH,
2255 wh, NULL, "%s", "MPM validation failed");
2258 /* If meshid is not the same reject any frames type. */
2259 if (sendclose == 0 && mesh_verify_meshid(vap, meshid)) {
2261 IEEE80211_DISCARD(vap,
2262 IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH,
2263 wh, NULL, "%s", "not for our mesh");
2264 if (subtype == IEEE80211_ACTION_MESHPEERING_CLOSE) {
2266 * Standard not clear about this, if we dont ignore
2267 * there will be an endless loop between nodes sending
2268 * CLOSE frames between each other with wrong meshid.
2269 * Discard and timers will bring FSM to IDLE state.
2276 * Close frames are accepted if meshid is the same.
2277 * Verify the other two types.
2279 if (sendclose == 0 && subtype != IEEE80211_ACTION_MESHPEERING_CLOSE &&
2280 mesh_verify_meshconf(vap, meshconf)) {
2282 IEEE80211_DISCARD(vap,
2283 IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH,
2284 wh, NULL, "%s", "configuration missmatch");
2288 vap->iv_stats.is_rx_mgtdiscard++;
2289 switch (ni->ni_mlstate) {
2290 case IEEE80211_NODE_MESH_IDLE:
2291 case IEEE80211_NODE_MESH_ESTABLISHED:
2292 case IEEE80211_NODE_MESH_HOLDING:
2295 case IEEE80211_NODE_MESH_OPENSNT:
2296 case IEEE80211_NODE_MESH_OPENRCV:
2297 case IEEE80211_NODE_MESH_CONFIRMRCV:
2298 args[0] = ni->ni_mlpid;
2299 args[1] = ni->ni_mllid;
2300 /* Reason codes for rejection */
2302 case IEEE80211_ACTION_MESHPEERING_OPEN:
2303 args[2] = IEEE80211_REASON_MESH_CPVIOLATION;
2305 case IEEE80211_ACTION_MESHPEERING_CONFIRM:
2306 args[2] = IEEE80211_REASON_MESH_INCONS_PARAMS;
2309 ieee80211_send_action(ni,
2310 IEEE80211_ACTION_CAT_SELF_PROT,
2311 IEEE80211_ACTION_MESHPEERING_CLOSE,
2313 mesh_linkchange(ni, IEEE80211_NODE_MESH_HOLDING);
2314 mesh_peer_timeout_setup(ni);
2320 return (const struct ieee80211_meshpeer_ie *) mp;
2324 mesh_recv_action_meshpeering_open(struct ieee80211_node *ni,
2325 const struct ieee80211_frame *wh,
2326 const uint8_t *frm, const uint8_t *efrm)
2328 struct ieee80211vap *vap = ni->ni_vap;
2329 struct ieee80211_mesh_state *ms = vap->iv_mesh;
2330 struct ieee80211_meshpeer_ie ie;
2331 const struct ieee80211_meshpeer_ie *meshpeer;
2334 /* +2+2 for action + code + capabilites */
2335 meshpeer = mesh_parse_meshpeering_action(ni, wh, frm+2+2, efrm, &ie,
2336 IEEE80211_ACTION_MESHPEERING_OPEN);
2337 if (meshpeer == NULL) {
2342 IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH, ni,
2343 "recv PEER OPEN, lid 0x%x", meshpeer->peer_llinkid);
2345 switch (ni->ni_mlstate) {
2346 case IEEE80211_NODE_MESH_IDLE:
2347 /* Reject open request if reached our maximum neighbor count */
2348 if (ms->ms_neighbors >= IEEE80211_MESH_MAX_NEIGHBORS) {
2349 args[0] = meshpeer->peer_llinkid;
2351 args[2] = IEEE80211_REASON_MESH_MAX_PEERS;
2352 ieee80211_send_action(ni,
2353 IEEE80211_ACTION_CAT_SELF_PROT,
2354 IEEE80211_ACTION_MESHPEERING_CLOSE,
2356 /* stay in IDLE state */
2359 /* Open frame accepted */
2360 mesh_linkchange(ni, IEEE80211_NODE_MESH_OPENRCV);
2361 ni->ni_mllid = meshpeer->peer_llinkid;
2362 ni->ni_mlpid = mesh_generateid(vap);
2363 if (ni->ni_mlpid == 0)
2365 args[0] = ni->ni_mlpid;
2366 /* Announce we're open too... */
2367 ieee80211_send_action(ni,
2368 IEEE80211_ACTION_CAT_SELF_PROT,
2369 IEEE80211_ACTION_MESHPEERING_OPEN, args);
2370 /* ...and confirm the link. */
2371 args[0] = ni->ni_mlpid;
2372 args[1] = ni->ni_mllid;
2373 ieee80211_send_action(ni,
2374 IEEE80211_ACTION_CAT_SELF_PROT,
2375 IEEE80211_ACTION_MESHPEERING_CONFIRM,
2377 mesh_peer_timeout_setup(ni);
2379 case IEEE80211_NODE_MESH_OPENRCV:
2381 if (ni->ni_mllid != meshpeer->peer_llinkid) {
2382 args[0] = ni->ni_mllid;
2383 args[1] = ni->ni_mlpid;
2384 args[2] = IEEE80211_REASON_PEER_LINK_CANCELED;
2385 ieee80211_send_action(ni,
2386 IEEE80211_ACTION_CAT_SELF_PROT,
2387 IEEE80211_ACTION_MESHPEERING_CLOSE,
2389 mesh_linkchange(ni, IEEE80211_NODE_MESH_HOLDING);
2390 mesh_peer_timeout_setup(ni);
2393 /* Duplicate open, confirm again. */
2394 args[0] = ni->ni_mlpid;
2395 args[1] = ni->ni_mllid;
2396 ieee80211_send_action(ni,
2397 IEEE80211_ACTION_CAT_SELF_PROT,
2398 IEEE80211_ACTION_MESHPEERING_CONFIRM,
2401 case IEEE80211_NODE_MESH_OPENSNT:
2402 ni->ni_mllid = meshpeer->peer_llinkid;
2403 mesh_linkchange(ni, IEEE80211_NODE_MESH_OPENRCV);
2404 args[0] = ni->ni_mlpid;
2405 args[1] = ni->ni_mllid;
2406 ieee80211_send_action(ni,
2407 IEEE80211_ACTION_CAT_SELF_PROT,
2408 IEEE80211_ACTION_MESHPEERING_CONFIRM,
2410 /* NB: don't setup/clear any timeout */
2412 case IEEE80211_NODE_MESH_CONFIRMRCV:
2413 if (ni->ni_mlpid != meshpeer->peer_linkid ||
2414 ni->ni_mllid != meshpeer->peer_llinkid) {
2415 args[0] = ni->ni_mlpid;
2416 args[1] = ni->ni_mllid;
2417 args[2] = IEEE80211_REASON_PEER_LINK_CANCELED;
2418 ieee80211_send_action(ni,
2419 IEEE80211_ACTION_CAT_SELF_PROT,
2420 IEEE80211_ACTION_MESHPEERING_CLOSE,
2423 IEEE80211_NODE_MESH_HOLDING);
2424 mesh_peer_timeout_setup(ni);
2427 mesh_linkchange(ni, IEEE80211_NODE_MESH_ESTABLISHED);
2428 ni->ni_mllid = meshpeer->peer_llinkid;
2429 args[0] = ni->ni_mlpid;
2430 args[1] = ni->ni_mllid;
2431 ieee80211_send_action(ni,
2432 IEEE80211_ACTION_CAT_SELF_PROT,
2433 IEEE80211_ACTION_MESHPEERING_CONFIRM,
2435 mesh_peer_timeout_stop(ni);
2437 case IEEE80211_NODE_MESH_ESTABLISHED:
2438 if (ni->ni_mllid != meshpeer->peer_llinkid) {
2439 args[0] = ni->ni_mllid;
2440 args[1] = ni->ni_mlpid;
2441 args[2] = IEEE80211_REASON_PEER_LINK_CANCELED;
2442 ieee80211_send_action(ni,
2443 IEEE80211_ACTION_CAT_SELF_PROT,
2444 IEEE80211_ACTION_MESHPEERING_CLOSE,
2446 mesh_linkchange(ni, IEEE80211_NODE_MESH_HOLDING);
2447 mesh_peer_timeout_setup(ni);
2450 args[0] = ni->ni_mlpid;
2451 args[1] = ni->ni_mllid;
2452 ieee80211_send_action(ni,
2453 IEEE80211_ACTION_CAT_SELF_PROT,
2454 IEEE80211_ACTION_MESHPEERING_CONFIRM,
2457 case IEEE80211_NODE_MESH_HOLDING:
2458 args[0] = ni->ni_mlpid;
2459 args[1] = meshpeer->peer_llinkid;
2460 /* Standard not clear about what the reaason code should be */
2461 args[2] = IEEE80211_REASON_PEER_LINK_CANCELED;
2462 ieee80211_send_action(ni,
2463 IEEE80211_ACTION_CAT_SELF_PROT,
2464 IEEE80211_ACTION_MESHPEERING_CLOSE,
2472 mesh_recv_action_meshpeering_confirm(struct ieee80211_node *ni,
2473 const struct ieee80211_frame *wh,
2474 const uint8_t *frm, const uint8_t *efrm)
2476 struct ieee80211vap *vap = ni->ni_vap;
2477 struct ieee80211_meshpeer_ie ie;
2478 const struct ieee80211_meshpeer_ie *meshpeer;
2481 /* +2+2+2+2 for action + code + capabilites + status code + AID */
2482 meshpeer = mesh_parse_meshpeering_action(ni, wh, frm+2+2+2+2, efrm, &ie,
2483 IEEE80211_ACTION_MESHPEERING_CONFIRM);
2484 if (meshpeer == NULL) {
2488 IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH, ni,
2489 "recv PEER CONFIRM, local id 0x%x, peer id 0x%x",
2490 meshpeer->peer_llinkid, meshpeer->peer_linkid);
2492 switch (ni->ni_mlstate) {
2493 case IEEE80211_NODE_MESH_OPENRCV:
2494 mesh_linkchange(ni, IEEE80211_NODE_MESH_ESTABLISHED);
2495 mesh_peer_timeout_stop(ni);
2497 case IEEE80211_NODE_MESH_OPENSNT:
2498 mesh_linkchange(ni, IEEE80211_NODE_MESH_CONFIRMRCV);
2499 mesh_peer_timeout_setup(ni);
2501 case IEEE80211_NODE_MESH_HOLDING:
2502 args[0] = ni->ni_mlpid;
2503 args[1] = meshpeer->peer_llinkid;
2504 /* Standard not clear about what the reaason code should be */
2505 args[2] = IEEE80211_REASON_PEER_LINK_CANCELED;
2506 ieee80211_send_action(ni,
2507 IEEE80211_ACTION_CAT_SELF_PROT,
2508 IEEE80211_ACTION_MESHPEERING_CLOSE,
2511 case IEEE80211_NODE_MESH_CONFIRMRCV:
2512 if (ni->ni_mllid != meshpeer->peer_llinkid) {
2513 args[0] = ni->ni_mlpid;
2514 args[1] = ni->ni_mllid;
2515 args[2] = IEEE80211_REASON_PEER_LINK_CANCELED;
2516 ieee80211_send_action(ni,
2517 IEEE80211_ACTION_CAT_SELF_PROT,
2518 IEEE80211_ACTION_MESHPEERING_CLOSE,
2520 mesh_linkchange(ni, IEEE80211_NODE_MESH_HOLDING);
2521 mesh_peer_timeout_setup(ni);
2525 IEEE80211_DISCARD(vap,
2526 IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH,
2527 wh, NULL, "received confirm in invalid state %d",
2529 vap->iv_stats.is_rx_mgtdiscard++;
2536 mesh_recv_action_meshpeering_close(struct ieee80211_node *ni,
2537 const struct ieee80211_frame *wh,
2538 const uint8_t *frm, const uint8_t *efrm)
2540 struct ieee80211_meshpeer_ie ie;
2541 const struct ieee80211_meshpeer_ie *meshpeer;
2544 /* +2 for action + code */
2545 meshpeer = mesh_parse_meshpeering_action(ni, wh, frm+2, efrm, &ie,
2546 IEEE80211_ACTION_MESHPEERING_CLOSE);
2547 if (meshpeer == NULL) {
2552 * XXX: check reason code, for example we could receive
2553 * IEEE80211_REASON_MESH_MAX_PEERS then we should not attempt
2557 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH,
2558 ni, "%s", "recv PEER CLOSE");
2560 switch (ni->ni_mlstate) {
2561 case IEEE80211_NODE_MESH_IDLE:
2564 case IEEE80211_NODE_MESH_OPENRCV:
2565 case IEEE80211_NODE_MESH_OPENSNT:
2566 case IEEE80211_NODE_MESH_CONFIRMRCV:
2567 case IEEE80211_NODE_MESH_ESTABLISHED:
2568 args[0] = ni->ni_mlpid;
2569 args[1] = ni->ni_mllid;
2570 args[2] = IEEE80211_REASON_MESH_CLOSE_RCVD;
2571 ieee80211_send_action(ni,
2572 IEEE80211_ACTION_CAT_SELF_PROT,
2573 IEEE80211_ACTION_MESHPEERING_CLOSE,
2575 mesh_linkchange(ni, IEEE80211_NODE_MESH_HOLDING);
2576 mesh_peer_timeout_setup(ni);
2578 case IEEE80211_NODE_MESH_HOLDING:
2579 mesh_linkchange(ni, IEEE80211_NODE_MESH_IDLE);
2580 mesh_peer_timeout_stop(ni);
2587 * Link Metric handling.
2590 mesh_recv_action_meshlmetric(struct ieee80211_node *ni,
2591 const struct ieee80211_frame *wh,
2592 const uint8_t *frm, const uint8_t *efrm)
2594 const struct ieee80211_meshlmetric_ie *ie =
2595 (const struct ieee80211_meshlmetric_ie *)
2596 (frm+2); /* action + code */
2597 struct ieee80211_meshlmetric_ie lm_rep;
2599 if (ie->lm_flags & IEEE80211_MESH_LMETRIC_FLAGS_REQ) {
2600 lm_rep.lm_flags = 0;
2601 lm_rep.lm_metric = mesh_airtime_calc(ni);
2602 ieee80211_send_action(ni,
2603 IEEE80211_ACTION_CAT_MESH,
2604 IEEE80211_ACTION_MESH_LMETRIC,
2607 /* XXX: else do nothing for now */
2612 * Parse meshgate action ie's for GANN frames.
2613 * Returns -1 if parsing fails, otherwise 0.
2616 mesh_parse_meshgate_action(struct ieee80211_node *ni,
2617 const struct ieee80211_frame *wh, /* XXX for VERIFY_LENGTH */
2618 struct ieee80211_meshgann_ie *ie, const uint8_t *frm, const uint8_t *efrm)
2620 struct ieee80211vap *vap = ni->ni_vap;
2621 const struct ieee80211_meshgann_ie *gannie;
2623 while (efrm - frm > 1) {
2624 IEEE80211_VERIFY_LENGTH(efrm - frm, frm[1] + 2, return -1);
2626 case IEEE80211_ELEMID_MESHGANN:
2627 gannie = (const struct ieee80211_meshgann_ie *) frm;
2628 memset(ie, 0, sizeof(ie));
2629 ie->gann_ie = gannie->gann_ie;
2630 ie->gann_len = gannie->gann_len;
2631 ie->gann_flags = gannie->gann_flags;
2632 ie->gann_hopcount = gannie->gann_hopcount;
2633 ie->gann_ttl = gannie->gann_ttl;
2634 IEEE80211_ADDR_COPY(ie->gann_addr, gannie->gann_addr);
2635 ie->gann_seq = LE_READ_4(&gannie->gann_seq);
2636 ie->gann_interval = LE_READ_2(&gannie->gann_interval);
2646 * Mesh Gate Announcement handling.
2649 mesh_recv_action_meshgate(struct ieee80211_node *ni,
2650 const struct ieee80211_frame *wh,
2651 const uint8_t *frm, const uint8_t *efrm)
2653 struct ieee80211vap *vap = ni->ni_vap;
2654 struct ieee80211_mesh_state *ms = vap->iv_mesh;
2655 struct ieee80211_mesh_gate_route *gr, *next;
2656 struct ieee80211_mesh_route *rt_gate;
2657 struct ieee80211_meshgann_ie pgann;
2658 struct ieee80211_meshgann_ie ie;
2661 /* +2 for action + code */
2662 if (mesh_parse_meshgate_action(ni, wh, &ie, frm+2, efrm) != 0) {
2663 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_MESH,
2664 ni->ni_macaddr, NULL, "%s",
2665 "GANN parsing failed");
2666 vap->iv_stats.is_rx_mgtdiscard++;
2670 if (IEEE80211_ADDR_EQ(vap->iv_myaddr, ie.gann_addr))
2673 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH, ni->ni_macaddr,
2674 "received GANN, meshgate: %6D (seq %u)", ie.gann_addr, ":",
2680 TAILQ_FOREACH_SAFE(gr, &ms->ms_known_gates, gr_next, next) {
2681 if (!IEEE80211_ADDR_EQ(gr->gr_addr, ie.gann_addr))
2683 if (ie.gann_seq <= gr->gr_lastseq) {
2684 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_MESH,
2685 ni->ni_macaddr, NULL,
2686 "GANN old seqno %u <= %u",
2687 ie.gann_seq, gr->gr_lastseq);
2691 /* corresponding mesh gate found & GANN accepted */
2697 /* this GANN is from a new mesh Gate add it to known table. */
2698 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH, ie.gann_addr,
2699 "stored new GANN information, seq %u.", ie.gann_seq);
2700 gr = malloc(ALIGN(sizeof(struct ieee80211_mesh_gate_route)),
2701 M_80211_MESH_GT_RT, M_NOWAIT | M_ZERO);
2702 IEEE80211_ADDR_COPY(gr->gr_addr, ie.gann_addr);
2703 TAILQ_INSERT_TAIL(&ms->ms_known_gates, gr, gr_next);
2705 gr->gr_lastseq = ie.gann_seq;
2707 /* check if we have a path to this gate */
2708 rt_gate = mesh_rt_find_locked(ms, gr->gr_addr);
2709 if (rt_gate != NULL &&
2710 rt_gate->rt_flags & IEEE80211_MESHRT_FLAGS_VALID) {
2711 gr->gr_route = rt_gate;
2712 rt_gate->rt_flags |= IEEE80211_MESHRT_FLAGS_GATE;
2717 /* popagate only if decremented ttl >= 1 && forwarding is enabled */
2718 if ((ie.gann_ttl - 1) < 1 && !(ms->ms_flags & IEEE80211_MESHFLAGS_FWD))
2720 pgann.gann_flags = ie.gann_flags; /* Reserved */
2721 pgann.gann_hopcount = ie.gann_hopcount + 1;
2722 pgann.gann_ttl = ie.gann_ttl - 1;
2723 IEEE80211_ADDR_COPY(pgann.gann_addr, ie.gann_addr);
2724 pgann.gann_seq = ie.gann_seq;
2725 pgann.gann_interval = ie.gann_interval;
2727 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH, ie.gann_addr,
2728 "%s", "propagate GANN");
2730 ieee80211_send_action(vap->iv_bss, IEEE80211_ACTION_CAT_MESH,
2731 IEEE80211_ACTION_MESH_GANN, &pgann);
2737 mesh_send_action(struct ieee80211_node *ni,
2738 const uint8_t sa[IEEE80211_ADDR_LEN],
2739 const uint8_t da[IEEE80211_ADDR_LEN],
2742 struct ieee80211vap *vap = ni->ni_vap;
2743 struct ieee80211com *ic = ni->ni_ic;
2744 struct ieee80211_bpf_params params;
2745 struct ieee80211_frame *wh;
2747 KASSERT(ni != NULL, ("null node"));
2749 if (vap->iv_state == IEEE80211_S_CAC) {
2750 IEEE80211_NOTE(vap, IEEE80211_MSG_OUTPUT, ni,
2751 "block %s frame in CAC state", "Mesh action");
2752 vap->iv_stats.is_tx_badstate++;
2753 ieee80211_free_node(ni);
2755 return EIO; /* XXX */
2758 M_PREPEND(m, sizeof(struct ieee80211_frame), M_DONTWAIT);
2760 ieee80211_free_node(ni);
2764 wh = mtod(m, struct ieee80211_frame *);
2765 ieee80211_send_setup(ni, m,
2766 IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_ACTION,
2767 IEEE80211_NONQOS_TID, sa, da, sa);
2768 m->m_flags |= M_ENCAP; /* mark encapsulated */
2770 memset(¶ms, 0, sizeof(params));
2771 params.ibp_pri = WME_AC_VO;
2772 params.ibp_rate0 = ni->ni_txparms->mgmtrate;
2773 if (IEEE80211_IS_MULTICAST(da))
2774 params.ibp_try0 = 1;
2776 params.ibp_try0 = ni->ni_txparms->maxretry;
2777 params.ibp_power = ni->ni_txpower;
2779 IEEE80211_NODE_STAT(ni, tx_mgmt);
2781 return ic->ic_raw_xmit(ni, m, ¶ms);
2784 #define ADDSHORT(frm, v) do { \
2785 frm[0] = (v) & 0xff; \
2786 frm[1] = (v) >> 8; \
2789 #define ADDWORD(frm, v) do { \
2790 frm[0] = (v) & 0xff; \
2791 frm[1] = ((v) >> 8) & 0xff; \
2792 frm[2] = ((v) >> 16) & 0xff; \
2793 frm[3] = ((v) >> 24) & 0xff; \
2798 mesh_send_action_meshpeering_open(struct ieee80211_node *ni,
2799 int category, int action, void *args0)
2801 struct ieee80211vap *vap = ni->ni_vap;
2802 struct ieee80211com *ic = ni->ni_ic;
2803 uint16_t *args = args0;
2804 const struct ieee80211_rateset *rs;
2808 IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH, ni,
2809 "send PEER OPEN action: localid 0x%x", args[0]);
2811 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
2812 "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", __func__, __LINE__,
2813 ni, ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)+1);
2814 ieee80211_ref_node(ni);
2816 m = ieee80211_getmgtframe(&frm,
2817 ic->ic_headroom + sizeof(struct ieee80211_frame),
2818 sizeof(uint16_t) /* action+category */
2819 + sizeof(uint16_t) /* capabilites */
2820 + 2 + IEEE80211_RATE_SIZE
2821 + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
2822 + 2 + IEEE80211_MESHID_LEN
2823 + sizeof(struct ieee80211_meshconf_ie)
2824 + sizeof(struct ieee80211_meshpeer_ie)
2828 * mesh peer open action frame format:
2836 * [tlv] mesh peer link mgmt
2840 ADDSHORT(frm, ieee80211_getcapinfo(vap, ni->ni_chan));
2841 rs = ieee80211_get_suprates(ic, ic->ic_curchan);
2842 frm = ieee80211_add_rates(frm, rs);
2843 frm = ieee80211_add_xrates(frm, rs);
2844 frm = ieee80211_add_meshid(frm, vap);
2845 frm = ieee80211_add_meshconf(frm, vap);
2846 frm = ieee80211_add_meshpeer(frm, IEEE80211_ACTION_MESHPEERING_OPEN,
2848 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2849 return mesh_send_action(ni, vap->iv_myaddr, ni->ni_macaddr, m);
2851 vap->iv_stats.is_tx_nobuf++;
2852 ieee80211_free_node(ni);
2858 mesh_send_action_meshpeering_confirm(struct ieee80211_node *ni,
2859 int category, int action, void *args0)
2861 struct ieee80211vap *vap = ni->ni_vap;
2862 struct ieee80211com *ic = ni->ni_ic;
2863 uint16_t *args = args0;
2864 const struct ieee80211_rateset *rs;
2868 IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH, ni,
2869 "send PEER CONFIRM action: localid 0x%x, peerid 0x%x",
2872 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
2873 "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", __func__, __LINE__,
2874 ni, ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)+1);
2875 ieee80211_ref_node(ni);
2877 m = ieee80211_getmgtframe(&frm,
2878 ic->ic_headroom + sizeof(struct ieee80211_frame),
2879 sizeof(uint16_t) /* action+category */
2880 + sizeof(uint16_t) /* capabilites */
2881 + sizeof(uint16_t) /* status code */
2882 + sizeof(uint16_t) /* AID */
2883 + 2 + IEEE80211_RATE_SIZE
2884 + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
2885 + 2 + IEEE80211_MESHID_LEN
2886 + sizeof(struct ieee80211_meshconf_ie)
2887 + sizeof(struct ieee80211_meshpeer_ie)
2891 * mesh peer confirm action frame format:
2896 * [2] association id (peer ID)
2901 * [tlv] mesh peer link mgmt
2905 ADDSHORT(frm, ieee80211_getcapinfo(vap, ni->ni_chan));
2906 ADDSHORT(frm, 0); /* status code */
2907 ADDSHORT(frm, args[1]); /* AID */
2908 rs = ieee80211_get_suprates(ic, ic->ic_curchan);
2909 frm = ieee80211_add_rates(frm, rs);
2910 frm = ieee80211_add_xrates(frm, rs);
2911 frm = ieee80211_add_meshid(frm, vap);
2912 frm = ieee80211_add_meshconf(frm, vap);
2913 frm = ieee80211_add_meshpeer(frm,
2914 IEEE80211_ACTION_MESHPEERING_CONFIRM,
2915 args[0], args[1], 0);
2916 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2917 return mesh_send_action(ni, vap->iv_myaddr, ni->ni_macaddr, m);
2919 vap->iv_stats.is_tx_nobuf++;
2920 ieee80211_free_node(ni);
2926 mesh_send_action_meshpeering_close(struct ieee80211_node *ni,
2927 int category, int action, void *args0)
2929 struct ieee80211vap *vap = ni->ni_vap;
2930 struct ieee80211com *ic = ni->ni_ic;
2931 uint16_t *args = args0;
2935 IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH, ni,
2936 "send PEER CLOSE action: localid 0x%x, peerid 0x%x reason %d",
2937 args[0], args[1], args[2]);
2939 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
2940 "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", __func__, __LINE__,
2941 ni, ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)+1);
2942 ieee80211_ref_node(ni);
2944 m = ieee80211_getmgtframe(&frm,
2945 ic->ic_headroom + sizeof(struct ieee80211_frame),
2946 sizeof(uint16_t) /* action+category */
2947 + sizeof(uint16_t) /* reason code */
2948 + 2 + IEEE80211_MESHID_LEN
2949 + sizeof(struct ieee80211_meshpeer_ie)
2953 * mesh peer close action frame format:
2957 * [tlv] mesh peer link mgmt
2961 frm = ieee80211_add_meshid(frm, vap);
2962 frm = ieee80211_add_meshpeer(frm,
2963 IEEE80211_ACTION_MESHPEERING_CLOSE,
2964 args[0], args[1], args[2]);
2965 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2966 return mesh_send_action(ni, vap->iv_myaddr, ni->ni_macaddr, m);
2968 vap->iv_stats.is_tx_nobuf++;
2969 ieee80211_free_node(ni);
2975 mesh_send_action_meshlmetric(struct ieee80211_node *ni,
2976 int category, int action, void *arg0)
2978 struct ieee80211vap *vap = ni->ni_vap;
2979 struct ieee80211com *ic = ni->ni_ic;
2980 struct ieee80211_meshlmetric_ie *ie = arg0;
2984 if (ie->lm_flags & IEEE80211_MESH_LMETRIC_FLAGS_REQ) {
2985 IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH,
2986 ni, "%s", "send LINK METRIC REQUEST action");
2988 IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH,
2989 ni, "send LINK METRIC REPLY action: metric 0x%x",
2992 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
2993 "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", __func__, __LINE__,
2994 ni, ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)+1);
2995 ieee80211_ref_node(ni);
2997 m = ieee80211_getmgtframe(&frm,
2998 ic->ic_headroom + sizeof(struct ieee80211_frame),
2999 sizeof(uint16_t) + /* action+category */
3000 sizeof(struct ieee80211_meshlmetric_ie)
3007 * [tlv] mesh link metric
3011 frm = ieee80211_add_meshlmetric(frm,
3012 ie->lm_flags, ie->lm_metric);
3013 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
3014 return mesh_send_action(ni, vap->iv_myaddr, ni->ni_macaddr, m);
3016 vap->iv_stats.is_tx_nobuf++;
3017 ieee80211_free_node(ni);
3023 mesh_send_action_meshgate(struct ieee80211_node *ni,
3024 int category, int action, void *arg0)
3026 struct ieee80211vap *vap = ni->ni_vap;
3027 struct ieee80211com *ic = ni->ni_ic;
3028 struct ieee80211_meshgann_ie *ie = arg0;
3032 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
3033 "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", __func__, __LINE__,
3034 ni, ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)+1);
3035 ieee80211_ref_node(ni);
3037 m = ieee80211_getmgtframe(&frm,
3038 ic->ic_headroom + sizeof(struct ieee80211_frame),
3039 sizeof(uint16_t) + /* action+category */
3040 IEEE80211_MESHGANN_BASE_SZ
3047 * [tlv] mesh gate annoucement
3051 frm = ieee80211_add_meshgate(frm, ie);
3052 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
3053 return mesh_send_action(ni, vap->iv_myaddr, broadcastaddr, m);
3055 vap->iv_stats.is_tx_nobuf++;
3056 ieee80211_free_node(ni);
3062 mesh_peer_timeout_setup(struct ieee80211_node *ni)
3064 switch (ni->ni_mlstate) {
3065 case IEEE80211_NODE_MESH_HOLDING:
3066 ni->ni_mltval = ieee80211_mesh_holdingtimeout;
3068 case IEEE80211_NODE_MESH_CONFIRMRCV:
3069 ni->ni_mltval = ieee80211_mesh_confirmtimeout;
3071 case IEEE80211_NODE_MESH_IDLE:
3075 ni->ni_mltval = ieee80211_mesh_retrytimeout;
3079 callout_reset(&ni->ni_mltimer, ni->ni_mltval,
3080 mesh_peer_timeout_cb, ni);
3084 * Same as above but backoffs timer statisically 50%.
3087 mesh_peer_timeout_backoff(struct ieee80211_node *ni)
3092 ni->ni_mltval += r % ni->ni_mltval;
3093 callout_reset(&ni->ni_mltimer, ni->ni_mltval, mesh_peer_timeout_cb,
3097 static __inline void
3098 mesh_peer_timeout_stop(struct ieee80211_node *ni)
3100 callout_drain(&ni->ni_mltimer);
3104 mesh_peer_backoff_cb(void *arg)
3106 struct ieee80211_node *ni = (struct ieee80211_node *)arg;
3108 /* After backoff timeout, try to peer automatically again. */
3113 * Mesh Peer Link Management FSM timeout handling.
3116 mesh_peer_timeout_cb(void *arg)
3118 struct ieee80211_node *ni = (struct ieee80211_node *)arg;
3121 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_MESH,
3122 ni, "mesh link timeout, state %d, retry counter %d",
3123 ni->ni_mlstate, ni->ni_mlrcnt);
3125 switch (ni->ni_mlstate) {
3126 case IEEE80211_NODE_MESH_IDLE:
3127 case IEEE80211_NODE_MESH_ESTABLISHED:
3129 case IEEE80211_NODE_MESH_OPENSNT:
3130 case IEEE80211_NODE_MESH_OPENRCV:
3131 if (ni->ni_mlrcnt == ieee80211_mesh_maxretries) {
3132 args[0] = ni->ni_mlpid;
3133 args[2] = IEEE80211_REASON_MESH_MAX_RETRIES;
3134 ieee80211_send_action(ni,
3135 IEEE80211_ACTION_CAT_SELF_PROT,
3136 IEEE80211_ACTION_MESHPEERING_CLOSE, args);
3138 mesh_linkchange(ni, IEEE80211_NODE_MESH_HOLDING);
3139 mesh_peer_timeout_setup(ni);
3141 args[0] = ni->ni_mlpid;
3142 ieee80211_send_action(ni,
3143 IEEE80211_ACTION_CAT_SELF_PROT,
3144 IEEE80211_ACTION_MESHPEERING_OPEN, args);
3146 mesh_peer_timeout_backoff(ni);
3149 case IEEE80211_NODE_MESH_CONFIRMRCV:
3150 args[0] = ni->ni_mlpid;
3151 args[2] = IEEE80211_REASON_MESH_CONFIRM_TIMEOUT;
3152 ieee80211_send_action(ni,
3153 IEEE80211_ACTION_CAT_SELF_PROT,
3154 IEEE80211_ACTION_MESHPEERING_CLOSE, args);
3155 mesh_linkchange(ni, IEEE80211_NODE_MESH_HOLDING);
3156 mesh_peer_timeout_setup(ni);
3158 case IEEE80211_NODE_MESH_HOLDING:
3160 if (ni->ni_mlhcnt >= ieee80211_mesh_maxholding)
3161 callout_reset(&ni->ni_mlhtimer,
3162 ieee80211_mesh_backofftimeout,
3163 mesh_peer_backoff_cb, ni);
3164 mesh_linkchange(ni, IEEE80211_NODE_MESH_IDLE);
3170 mesh_verify_meshid(struct ieee80211vap *vap, const uint8_t *ie)
3172 struct ieee80211_mesh_state *ms = vap->iv_mesh;
3174 if (ie == NULL || ie[1] != ms->ms_idlen)
3176 return memcmp(ms->ms_id, ie + 2, ms->ms_idlen);
3180 * Check if we are using the same algorithms for this mesh.
3183 mesh_verify_meshconf(struct ieee80211vap *vap, const uint8_t *ie)
3185 const struct ieee80211_meshconf_ie *meshconf =
3186 (const struct ieee80211_meshconf_ie *) ie;
3187 const struct ieee80211_mesh_state *ms = vap->iv_mesh;
3189 if (meshconf == NULL)
3191 if (meshconf->conf_pselid != ms->ms_ppath->mpp_ie) {
3192 IEEE80211_DPRINTF(vap, IEEE80211_MSG_MESH,
3193 "unknown path selection algorithm: 0x%x\n",
3194 meshconf->conf_pselid);
3197 if (meshconf->conf_pmetid != ms->ms_pmetric->mpm_ie) {
3198 IEEE80211_DPRINTF(vap, IEEE80211_MSG_MESH,
3199 "unknown path metric algorithm: 0x%x\n",
3200 meshconf->conf_pmetid);
3203 if (meshconf->conf_ccid != 0) {
3204 IEEE80211_DPRINTF(vap, IEEE80211_MSG_MESH,
3205 "unknown congestion control algorithm: 0x%x\n",
3206 meshconf->conf_ccid);
3209 if (meshconf->conf_syncid != IEEE80211_MESHCONF_SYNC_NEIGHOFF) {
3210 IEEE80211_DPRINTF(vap, IEEE80211_MSG_MESH,
3211 "unknown sync algorithm: 0x%x\n",
3212 meshconf->conf_syncid);
3215 if (meshconf->conf_authid != 0) {
3216 IEEE80211_DPRINTF(vap, IEEE80211_MSG_MESH,
3217 "unknown auth auth algorithm: 0x%x\n",
3218 meshconf->conf_pselid);
3221 /* Not accepting peers */
3222 if (!(meshconf->conf_cap & IEEE80211_MESHCONF_CAP_AP)) {
3223 IEEE80211_DPRINTF(vap, IEEE80211_MSG_MESH,
3224 "not accepting peers: 0x%x\n", meshconf->conf_cap);
3231 mesh_verify_meshpeer(struct ieee80211vap *vap, uint8_t subtype,
3234 const struct ieee80211_meshpeer_ie *meshpeer =
3235 (const struct ieee80211_meshpeer_ie *) ie;
3237 if (meshpeer == NULL ||
3238 meshpeer->peer_len < IEEE80211_MPM_BASE_SZ ||
3239 meshpeer->peer_len > IEEE80211_MPM_MAX_SZ)
3241 if (meshpeer->peer_proto != IEEE80211_MPPID_MPM) {
3242 IEEE80211_DPRINTF(vap,
3243 IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH,
3244 "Only MPM protocol is supported (proto: 0x%02X)",
3245 meshpeer->peer_proto);
3249 case IEEE80211_ACTION_MESHPEERING_OPEN:
3250 if (meshpeer->peer_len != IEEE80211_MPM_BASE_SZ)
3253 case IEEE80211_ACTION_MESHPEERING_CONFIRM:
3254 if (meshpeer->peer_len != IEEE80211_MPM_BASE_SZ + 2)
3257 case IEEE80211_ACTION_MESHPEERING_CLOSE:
3258 if (meshpeer->peer_len < IEEE80211_MPM_BASE_SZ + 2)
3260 if (meshpeer->peer_len == (IEEE80211_MPM_BASE_SZ + 2) &&
3261 meshpeer->peer_linkid != 0)
3263 if (meshpeer->peer_rcode == 0)
3271 * Add a Mesh ID IE to a frame.
3274 ieee80211_add_meshid(uint8_t *frm, struct ieee80211vap *vap)
3276 struct ieee80211_mesh_state *ms = vap->iv_mesh;
3278 KASSERT(vap->iv_opmode == IEEE80211_M_MBSS, ("not a mbss vap"));
3280 *frm++ = IEEE80211_ELEMID_MESHID;
3281 *frm++ = ms->ms_idlen;
3282 memcpy(frm, ms->ms_id, ms->ms_idlen);
3283 return frm + ms->ms_idlen;
3287 * Add a Mesh Configuration IE to a frame.
3288 * For now just use HWMP routing, Airtime link metric, Null Congestion
3289 * Signaling, Null Sync Protocol and Null Authentication.
3292 ieee80211_add_meshconf(uint8_t *frm, struct ieee80211vap *vap)
3294 const struct ieee80211_mesh_state *ms = vap->iv_mesh;
3297 KASSERT(vap->iv_opmode == IEEE80211_M_MBSS, ("not a MBSS vap"));
3299 *frm++ = IEEE80211_ELEMID_MESHCONF;
3300 *frm++ = IEEE80211_MESH_CONF_SZ;
3301 *frm++ = ms->ms_ppath->mpp_ie; /* path selection */
3302 *frm++ = ms->ms_pmetric->mpm_ie; /* link metric */
3303 *frm++ = IEEE80211_MESHCONF_CC_DISABLED;
3304 *frm++ = IEEE80211_MESHCONF_SYNC_NEIGHOFF;
3305 *frm++ = IEEE80211_MESHCONF_AUTH_DISABLED;
3306 /* NB: set the number of neighbors before the rest */
3307 *frm = (ms->ms_neighbors > IEEE80211_MESH_MAX_NEIGHBORS ?
3308 IEEE80211_MESH_MAX_NEIGHBORS : ms->ms_neighbors) << 1;
3309 if (ms->ms_flags & IEEE80211_MESHFLAGS_GATE)
3310 *frm |= IEEE80211_MESHCONF_FORM_GATE;
3313 if (ms->ms_flags & IEEE80211_MESHFLAGS_AP)
3314 caps |= IEEE80211_MESHCONF_CAP_AP;
3315 if (ms->ms_flags & IEEE80211_MESHFLAGS_FWD)
3316 caps |= IEEE80211_MESHCONF_CAP_FWRD;
3322 * Add a Mesh Peer Management IE to a frame.
3325 ieee80211_add_meshpeer(uint8_t *frm, uint8_t subtype, uint16_t localid,
3326 uint16_t peerid, uint16_t reason)
3329 KASSERT(localid != 0, ("localid == 0"));
3331 *frm++ = IEEE80211_ELEMID_MESHPEER;
3333 case IEEE80211_ACTION_MESHPEERING_OPEN:
3334 *frm++ = IEEE80211_MPM_BASE_SZ; /* length */
3335 ADDSHORT(frm, IEEE80211_MPPID_MPM); /* proto */
3336 ADDSHORT(frm, localid); /* local ID */
3338 case IEEE80211_ACTION_MESHPEERING_CONFIRM:
3339 KASSERT(peerid != 0, ("sending peer confirm without peer id"));
3340 *frm++ = IEEE80211_MPM_BASE_SZ + 2; /* length */
3341 ADDSHORT(frm, IEEE80211_MPPID_MPM); /* proto */
3342 ADDSHORT(frm, localid); /* local ID */
3343 ADDSHORT(frm, peerid); /* peer ID */
3345 case IEEE80211_ACTION_MESHPEERING_CLOSE:
3347 *frm++ = IEEE80211_MPM_MAX_SZ; /* length */
3349 *frm++ = IEEE80211_MPM_BASE_SZ + 2; /* length */
3350 ADDSHORT(frm, IEEE80211_MPPID_MPM); /* proto */
3351 ADDSHORT(frm, localid); /* local ID */
3353 ADDSHORT(frm, peerid); /* peer ID */
3354 ADDSHORT(frm, reason);
3361 * Compute an Airtime Link Metric for the link with this node.
3363 * Based on Draft 3.0 spec (11B.10, p.149).
3366 * Max 802.11s overhead.
3368 #define IEEE80211_MESH_MAXOVERHEAD \
3369 (sizeof(struct ieee80211_qosframe_addr4) \
3370 + sizeof(struct ieee80211_meshcntl_ae10) \
3371 + sizeof(struct llc) \
3372 + IEEE80211_ADDR_LEN \
3373 + IEEE80211_WEP_IVLEN \
3374 + IEEE80211_WEP_KIDLEN \
3375 + IEEE80211_WEP_CRCLEN \
3376 + IEEE80211_WEP_MICLEN \
3377 + IEEE80211_CRC_LEN)
3379 mesh_airtime_calc(struct ieee80211_node *ni)
3382 #define S_FACTOR (2 * M_BITS)
3383 struct ieee80211com *ic = ni->ni_ic;
3384 struct ifnet *ifp = ni->ni_vap->iv_ifp;
3385 const static int nbits = 8192 << M_BITS;
3386 uint32_t overhead, rate, errrate;
3389 /* Time to transmit a frame */
3390 rate = ni->ni_txrate;
3391 overhead = ieee80211_compute_duration(ic->ic_rt,
3392 ifp->if_mtu + IEEE80211_MESH_MAXOVERHEAD, rate, 0) << M_BITS;
3393 /* Error rate in percentage */
3394 /* XXX assuming small failures are ok */
3395 errrate = (((ifp->if_oerrors +
3396 ifp->if_ierrors) / 100) << M_BITS) / 100;
3397 res = (overhead + (nbits / rate)) *
3398 ((1 << S_FACTOR) / ((1 << M_BITS) - errrate));
3400 return (uint32_t)(res >> S_FACTOR);
3406 * Add a Mesh Link Metric report IE to a frame.
3409 ieee80211_add_meshlmetric(uint8_t *frm, uint8_t flags, uint32_t metric)
3411 *frm++ = IEEE80211_ELEMID_MESHLINK;
3414 ADDWORD(frm, metric);
3419 * Add a Mesh Gate Announcement IE to a frame.
3422 ieee80211_add_meshgate(uint8_t *frm, struct ieee80211_meshgann_ie *ie)
3424 *frm++ = IEEE80211_ELEMID_MESHGANN; /* ie */
3425 *frm++ = IEEE80211_MESHGANN_BASE_SZ; /* len */
3426 *frm++ = ie->gann_flags;
3427 *frm++ = ie->gann_hopcount;
3428 *frm++ = ie->gann_ttl;
3429 IEEE80211_ADDR_COPY(frm, ie->gann_addr);
3431 ADDWORD(frm, ie->gann_seq);
3432 ADDSHORT(frm, ie->gann_interval);
3439 * Initialize any mesh-specific node state.
3442 ieee80211_mesh_node_init(struct ieee80211vap *vap, struct ieee80211_node *ni)
3444 ni->ni_flags |= IEEE80211_NODE_QOS;
3445 callout_init(&ni->ni_mltimer, CALLOUT_MPSAFE);
3446 callout_init(&ni->ni_mlhtimer, CALLOUT_MPSAFE);
3450 * Cleanup any mesh-specific node state.
3453 ieee80211_mesh_node_cleanup(struct ieee80211_node *ni)
3455 struct ieee80211vap *vap = ni->ni_vap;
3456 struct ieee80211_mesh_state *ms = vap->iv_mesh;
3458 callout_drain(&ni->ni_mltimer);
3459 callout_drain(&ni->ni_mlhtimer);
3460 /* NB: short-circuit callbacks after mesh_vdetach */
3461 if (vap->iv_mesh != NULL)
3462 ms->ms_ppath->mpp_peerdown(ni);
3466 ieee80211_parse_meshid(struct ieee80211_node *ni, const uint8_t *ie)
3468 ni->ni_meshidlen = ie[1];
3469 memcpy(ni->ni_meshid, ie + 2, ie[1]);
3473 * Setup mesh-specific node state on neighbor discovery.
3476 ieee80211_mesh_init_neighbor(struct ieee80211_node *ni,
3477 const struct ieee80211_frame *wh,
3478 const struct ieee80211_scanparams *sp)
3480 ieee80211_parse_meshid(ni, sp->meshid);
3484 ieee80211_mesh_update_beacon(struct ieee80211vap *vap,
3485 struct ieee80211_beacon_offsets *bo)
3487 KASSERT(vap->iv_opmode == IEEE80211_M_MBSS, ("not a MBSS vap"));
3489 if (isset(bo->bo_flags, IEEE80211_BEACON_MESHCONF)) {
3490 (void)ieee80211_add_meshconf(bo->bo_meshconf, vap);
3491 clrbit(bo->bo_flags, IEEE80211_BEACON_MESHCONF);
3496 mesh_ioctl_get80211(struct ieee80211vap *vap, struct ieee80211req *ireq)
3498 struct ieee80211_mesh_state *ms = vap->iv_mesh;
3499 uint8_t tmpmeshid[IEEE80211_NWID_LEN];
3500 struct ieee80211_mesh_route *rt;
3501 struct ieee80211req_mesh_route *imr;
3506 if (vap->iv_opmode != IEEE80211_M_MBSS)
3510 switch (ireq->i_type) {
3511 case IEEE80211_IOC_MESH_ID:
3512 ireq->i_len = ms->ms_idlen;
3513 memcpy(tmpmeshid, ms->ms_id, ireq->i_len);
3514 error = copyout(tmpmeshid, ireq->i_data, ireq->i_len);
3516 case IEEE80211_IOC_MESH_AP:
3517 ireq->i_val = (ms->ms_flags & IEEE80211_MESHFLAGS_AP) != 0;
3519 case IEEE80211_IOC_MESH_FWRD:
3520 ireq->i_val = (ms->ms_flags & IEEE80211_MESHFLAGS_FWD) != 0;
3522 case IEEE80211_IOC_MESH_GATE:
3523 ireq->i_val = (ms->ms_flags & IEEE80211_MESHFLAGS_GATE) != 0;
3525 case IEEE80211_IOC_MESH_TTL:
3526 ireq->i_val = ms->ms_ttl;
3528 case IEEE80211_IOC_MESH_RTCMD:
3529 switch (ireq->i_val) {
3530 case IEEE80211_MESH_RTCMD_LIST:
3533 TAILQ_FOREACH(rt, &ms->ms_routes, rt_next) {
3534 len += sizeof(*imr);
3537 if (len > ireq->i_len || ireq->i_len < sizeof(*imr)) {
3543 p = malloc(len, M_TEMP, M_NOWAIT | M_ZERO);
3548 TAILQ_FOREACH(rt, &ms->ms_routes, rt_next) {
3551 imr = (struct ieee80211req_mesh_route *)
3553 IEEE80211_ADDR_COPY(imr->imr_dest,
3555 IEEE80211_ADDR_COPY(imr->imr_nexthop,
3557 imr->imr_metric = rt->rt_metric;
3558 imr->imr_nhops = rt->rt_nhops;
3560 ieee80211_mesh_rt_update(rt, 0);
3561 imr->imr_lastmseq = rt->rt_lastmseq;
3562 imr->imr_flags = rt->rt_flags; /* last */
3563 off += sizeof(*imr);
3566 error = copyout(p, (uint8_t *)ireq->i_data,
3570 case IEEE80211_MESH_RTCMD_FLUSH:
3571 case IEEE80211_MESH_RTCMD_ADD:
3572 case IEEE80211_MESH_RTCMD_DELETE:
3578 case IEEE80211_IOC_MESH_PR_METRIC:
3579 len = strlen(ms->ms_pmetric->mpm_descr);
3580 if (ireq->i_len < len)
3583 error = copyout(ms->ms_pmetric->mpm_descr,
3584 (uint8_t *)ireq->i_data, len);
3586 case IEEE80211_IOC_MESH_PR_PATH:
3587 len = strlen(ms->ms_ppath->mpp_descr);
3588 if (ireq->i_len < len)
3591 error = copyout(ms->ms_ppath->mpp_descr,
3592 (uint8_t *)ireq->i_data, len);
3600 IEEE80211_IOCTL_GET(mesh, mesh_ioctl_get80211);
3603 mesh_ioctl_set80211(struct ieee80211vap *vap, struct ieee80211req *ireq)
3605 struct ieee80211_mesh_state *ms = vap->iv_mesh;
3606 uint8_t tmpmeshid[IEEE80211_NWID_LEN];
3607 uint8_t tmpaddr[IEEE80211_ADDR_LEN];
3608 char tmpproto[IEEE80211_MESH_PROTO_DSZ];
3611 if (vap->iv_opmode != IEEE80211_M_MBSS)
3615 switch (ireq->i_type) {
3616 case IEEE80211_IOC_MESH_ID:
3617 if (ireq->i_val != 0 || ireq->i_len > IEEE80211_MESHID_LEN)
3619 error = copyin(ireq->i_data, tmpmeshid, ireq->i_len);
3622 memset(ms->ms_id, 0, IEEE80211_NWID_LEN);
3623 ms->ms_idlen = ireq->i_len;
3624 memcpy(ms->ms_id, tmpmeshid, ireq->i_len);
3627 case IEEE80211_IOC_MESH_AP:
3629 ms->ms_flags |= IEEE80211_MESHFLAGS_AP;
3631 ms->ms_flags &= ~IEEE80211_MESHFLAGS_AP;
3634 case IEEE80211_IOC_MESH_FWRD:
3636 ms->ms_flags |= IEEE80211_MESHFLAGS_FWD;
3638 ms->ms_flags &= ~IEEE80211_MESHFLAGS_FWD;
3639 mesh_gatemode_setup(vap);
3641 case IEEE80211_IOC_MESH_GATE:
3643 ms->ms_flags |= IEEE80211_MESHFLAGS_GATE;
3645 ms->ms_flags &= ~IEEE80211_MESHFLAGS_GATE;
3647 case IEEE80211_IOC_MESH_TTL:
3648 ms->ms_ttl = (uint8_t) ireq->i_val;
3650 case IEEE80211_IOC_MESH_RTCMD:
3651 switch (ireq->i_val) {
3652 case IEEE80211_MESH_RTCMD_LIST:
3654 case IEEE80211_MESH_RTCMD_FLUSH:
3655 ieee80211_mesh_rt_flush(vap);
3657 case IEEE80211_MESH_RTCMD_ADD:
3658 if (IEEE80211_ADDR_EQ(vap->iv_myaddr, ireq->i_data) ||
3659 IEEE80211_ADDR_EQ(broadcastaddr, ireq->i_data))
3661 error = copyin(ireq->i_data, &tmpaddr,
3662 IEEE80211_ADDR_LEN);
3664 ieee80211_mesh_discover(vap, tmpaddr, NULL);
3666 case IEEE80211_MESH_RTCMD_DELETE:
3667 ieee80211_mesh_rt_del(vap, ireq->i_data);
3673 case IEEE80211_IOC_MESH_PR_METRIC:
3674 error = copyin(ireq->i_data, tmpproto, sizeof(tmpproto));
3676 error = mesh_select_proto_metric(vap, tmpproto);
3681 case IEEE80211_IOC_MESH_PR_PATH:
3682 error = copyin(ireq->i_data, tmpproto, sizeof(tmpproto));
3684 error = mesh_select_proto_path(vap, tmpproto);
3694 IEEE80211_IOCTL_SET(mesh, mesh_ioctl_set80211);