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_var.h>
58 #include <net/if_media.h>
59 #include <net/if_llc.h>
60 #include <net/ethernet.h>
62 #include <net80211/ieee80211_var.h>
63 #include <net80211/ieee80211_action.h>
64 #ifdef IEEE80211_SUPPORT_SUPERG
65 #include <net80211/ieee80211_superg.h>
67 #include <net80211/ieee80211_input.h>
68 #include <net80211/ieee80211_mesh.h>
70 static void mesh_rt_flush_invalid(struct ieee80211vap *);
71 static int mesh_select_proto_path(struct ieee80211vap *, const char *);
72 static int mesh_select_proto_metric(struct ieee80211vap *, const char *);
73 static void mesh_vattach(struct ieee80211vap *);
74 static int mesh_newstate(struct ieee80211vap *, enum ieee80211_state, int);
75 static void mesh_rt_cleanup_cb(void *);
76 static void mesh_gatemode_setup(struct ieee80211vap *);
77 static void mesh_gatemode_cb(void *);
78 static void mesh_linkchange(struct ieee80211_node *,
79 enum ieee80211_mesh_mlstate);
80 static void mesh_checkid(void *, struct ieee80211_node *);
81 static uint32_t mesh_generateid(struct ieee80211vap *);
82 static int mesh_checkpseq(struct ieee80211vap *,
83 const uint8_t [IEEE80211_ADDR_LEN], uint32_t);
84 static void mesh_transmit_to_gate(struct ieee80211vap *, struct mbuf *,
85 struct ieee80211_mesh_route *);
86 static void mesh_forward(struct ieee80211vap *, struct mbuf *,
87 const struct ieee80211_meshcntl *);
88 static int mesh_input(struct ieee80211_node *, struct mbuf *,
89 const struct ieee80211_rx_stats *rxs, int, int);
90 static void mesh_recv_mgmt(struct ieee80211_node *, struct mbuf *, int,
91 const struct ieee80211_rx_stats *rxs, int, int);
92 static void mesh_recv_ctl(struct ieee80211_node *, struct mbuf *, int);
93 static void mesh_peer_timeout_setup(struct ieee80211_node *);
94 static void mesh_peer_timeout_backoff(struct ieee80211_node *);
95 static void mesh_peer_timeout_cb(void *);
97 mesh_peer_timeout_stop(struct ieee80211_node *);
98 static int mesh_verify_meshid(struct ieee80211vap *, const uint8_t *);
99 static int mesh_verify_meshconf(struct ieee80211vap *, const uint8_t *);
100 static int mesh_verify_meshpeer(struct ieee80211vap *, uint8_t,
102 uint32_t mesh_airtime_calc(struct ieee80211_node *);
105 * Timeout values come from the specification and are in milliseconds.
107 static SYSCTL_NODE(_net_wlan, OID_AUTO, mesh, CTLFLAG_RD, 0,
108 "IEEE 802.11s parameters");
109 static int ieee80211_mesh_gateint = -1;
110 SYSCTL_PROC(_net_wlan_mesh, OID_AUTO, gateint, CTLTYPE_INT | CTLFLAG_RW,
111 &ieee80211_mesh_gateint, 0, ieee80211_sysctl_msecs_ticks, "I",
112 "mesh gate interval (ms)");
113 static int ieee80211_mesh_retrytimeout = -1;
114 SYSCTL_PROC(_net_wlan_mesh, OID_AUTO, retrytimeout, CTLTYPE_INT | CTLFLAG_RW,
115 &ieee80211_mesh_retrytimeout, 0, ieee80211_sysctl_msecs_ticks, "I",
116 "Retry timeout (msec)");
117 static int ieee80211_mesh_holdingtimeout = -1;
119 SYSCTL_PROC(_net_wlan_mesh, OID_AUTO, holdingtimeout, CTLTYPE_INT | CTLFLAG_RW,
120 &ieee80211_mesh_holdingtimeout, 0, ieee80211_sysctl_msecs_ticks, "I",
121 "Holding state timeout (msec)");
122 static int ieee80211_mesh_confirmtimeout = -1;
123 SYSCTL_PROC(_net_wlan_mesh, OID_AUTO, confirmtimeout, CTLTYPE_INT | CTLFLAG_RW,
124 &ieee80211_mesh_confirmtimeout, 0, ieee80211_sysctl_msecs_ticks, "I",
125 "Confirm state timeout (msec)");
126 static int ieee80211_mesh_backofftimeout = -1;
127 SYSCTL_PROC(_net_wlan_mesh, OID_AUTO, backofftimeout, CTLTYPE_INT | CTLFLAG_RW,
128 &ieee80211_mesh_backofftimeout, 0, ieee80211_sysctl_msecs_ticks, "I",
129 "Backoff timeout (msec). This is to throutles peering forever when "
130 "not receiving answer or is rejected by a neighbor");
131 static int ieee80211_mesh_maxretries = 2;
132 SYSCTL_INT(_net_wlan_mesh, OID_AUTO, maxretries, CTLFLAG_RW,
133 &ieee80211_mesh_maxretries, 0,
134 "Maximum retries during peer link establishment");
135 static int ieee80211_mesh_maxholding = 2;
136 SYSCTL_INT(_net_wlan_mesh, OID_AUTO, maxholding, CTLFLAG_RW,
137 &ieee80211_mesh_maxholding, 0,
138 "Maximum times we are allowed to transition to HOLDING state before "
139 "backinoff during peer link establishment");
141 static const uint8_t broadcastaddr[IEEE80211_ADDR_LEN] =
142 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
144 static ieee80211_recv_action_func mesh_recv_action_meshpeering_open;
145 static ieee80211_recv_action_func mesh_recv_action_meshpeering_confirm;
146 static ieee80211_recv_action_func mesh_recv_action_meshpeering_close;
147 static ieee80211_recv_action_func mesh_recv_action_meshlmetric;
148 static ieee80211_recv_action_func mesh_recv_action_meshgate;
150 static ieee80211_send_action_func mesh_send_action_meshpeering_open;
151 static ieee80211_send_action_func mesh_send_action_meshpeering_confirm;
152 static ieee80211_send_action_func mesh_send_action_meshpeering_close;
153 static ieee80211_send_action_func mesh_send_action_meshlmetric;
154 static ieee80211_send_action_func mesh_send_action_meshgate;
156 static const struct ieee80211_mesh_proto_metric mesh_metric_airtime = {
157 .mpm_descr = "AIRTIME",
158 .mpm_ie = IEEE80211_MESHCONF_METRIC_AIRTIME,
159 .mpm_metric = mesh_airtime_calc,
162 static struct ieee80211_mesh_proto_path mesh_proto_paths[4];
163 static struct ieee80211_mesh_proto_metric mesh_proto_metrics[4];
165 MALLOC_DEFINE(M_80211_MESH_PREQ, "80211preq", "802.11 MESH Path Request frame");
166 MALLOC_DEFINE(M_80211_MESH_PREP, "80211prep", "802.11 MESH Path Reply frame");
167 MALLOC_DEFINE(M_80211_MESH_PERR, "80211perr", "802.11 MESH Path Error frame");
169 /* The longer one of the lifetime should be stored as new lifetime */
170 #define MESH_ROUTE_LIFETIME_MAX(a, b) (a > b ? a : b)
172 MALLOC_DEFINE(M_80211_MESH_RT, "80211mesh_rt", "802.11s routing table");
173 MALLOC_DEFINE(M_80211_MESH_GT_RT, "80211mesh_gt", "802.11s known gates table");
176 * Helper functions to manipulate the Mesh routing table.
179 static struct ieee80211_mesh_route *
180 mesh_rt_find_locked(struct ieee80211_mesh_state *ms,
181 const uint8_t dest[IEEE80211_ADDR_LEN])
183 struct ieee80211_mesh_route *rt;
185 MESH_RT_LOCK_ASSERT(ms);
187 TAILQ_FOREACH(rt, &ms->ms_routes, rt_next) {
188 if (IEEE80211_ADDR_EQ(dest, rt->rt_dest))
194 static struct ieee80211_mesh_route *
195 mesh_rt_add_locked(struct ieee80211vap *vap,
196 const uint8_t dest[IEEE80211_ADDR_LEN])
198 struct ieee80211_mesh_state *ms = vap->iv_mesh;
199 struct ieee80211_mesh_route *rt;
201 KASSERT(!IEEE80211_ADDR_EQ(broadcastaddr, dest),
202 ("%s: adding broadcast to the routing table", __func__));
204 MESH_RT_LOCK_ASSERT(ms);
206 rt = IEEE80211_MALLOC(ALIGN(sizeof(struct ieee80211_mesh_route)) +
207 ms->ms_ppath->mpp_privlen, M_80211_MESH_RT,
208 IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
211 IEEE80211_ADDR_COPY(rt->rt_dest, dest);
212 rt->rt_priv = (void *)ALIGN(&rt[1]);
213 MESH_RT_ENTRY_LOCK_INIT(rt, "MBSS_RT");
214 callout_init(&rt->rt_discovery, 1);
215 rt->rt_updtime = ticks; /* create time */
216 TAILQ_INSERT_TAIL(&ms->ms_routes, rt, rt_next);
221 struct ieee80211_mesh_route *
222 ieee80211_mesh_rt_find(struct ieee80211vap *vap,
223 const uint8_t dest[IEEE80211_ADDR_LEN])
225 struct ieee80211_mesh_state *ms = vap->iv_mesh;
226 struct ieee80211_mesh_route *rt;
229 rt = mesh_rt_find_locked(ms, dest);
234 struct ieee80211_mesh_route *
235 ieee80211_mesh_rt_add(struct ieee80211vap *vap,
236 const uint8_t dest[IEEE80211_ADDR_LEN])
238 struct ieee80211_mesh_state *ms = vap->iv_mesh;
239 struct ieee80211_mesh_route *rt;
241 KASSERT(ieee80211_mesh_rt_find(vap, dest) == NULL,
242 ("%s: duplicate entry in the routing table", __func__));
243 KASSERT(!IEEE80211_ADDR_EQ(vap->iv_myaddr, dest),
244 ("%s: adding self to the routing table", __func__));
247 rt = mesh_rt_add_locked(vap, dest);
253 * Update the route lifetime and returns the updated lifetime.
254 * If new_lifetime is zero and route is timedout it will be invalidated.
255 * new_lifetime is in msec
258 ieee80211_mesh_rt_update(struct ieee80211_mesh_route *rt, int new_lifetime)
261 uint32_t lifetime = 0;
263 KASSERT(rt != NULL, ("route is NULL"));
266 MESH_RT_ENTRY_LOCK(rt);
268 /* dont clobber a proxy entry gated by us */
269 if (rt->rt_flags & IEEE80211_MESHRT_FLAGS_PROXY && rt->rt_nhops == 0) {
270 MESH_RT_ENTRY_UNLOCK(rt);
271 return rt->rt_lifetime;
274 timesince = ticks_to_msecs(now - rt->rt_updtime);
275 rt->rt_updtime = now;
276 if (timesince >= rt->rt_lifetime) {
277 if (new_lifetime != 0) {
278 rt->rt_lifetime = new_lifetime;
281 rt->rt_flags &= ~IEEE80211_MESHRT_FLAGS_VALID;
285 /* update what is left of lifetime */
286 rt->rt_lifetime = rt->rt_lifetime - timesince;
287 rt->rt_lifetime = MESH_ROUTE_LIFETIME_MAX(
288 new_lifetime, rt->rt_lifetime);
290 lifetime = rt->rt_lifetime;
291 MESH_RT_ENTRY_UNLOCK(rt);
297 * Add a proxy route (as needed) for the specified destination.
300 ieee80211_mesh_proxy_check(struct ieee80211vap *vap,
301 const uint8_t dest[IEEE80211_ADDR_LEN])
303 struct ieee80211_mesh_state *ms = vap->iv_mesh;
304 struct ieee80211_mesh_route *rt;
307 rt = mesh_rt_find_locked(ms, dest);
309 rt = mesh_rt_add_locked(vap, dest);
311 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH, dest,
312 "%s", "unable to add proxy entry");
313 vap->iv_stats.is_mesh_rtaddfailed++;
315 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH, dest,
316 "%s", "add proxy entry");
317 IEEE80211_ADDR_COPY(rt->rt_mesh_gate, vap->iv_myaddr);
318 IEEE80211_ADDR_COPY(rt->rt_nexthop, vap->iv_myaddr);
319 rt->rt_flags |= IEEE80211_MESHRT_FLAGS_VALID
320 | IEEE80211_MESHRT_FLAGS_PROXY;
322 } else if ((rt->rt_flags & IEEE80211_MESHRT_FLAGS_VALID) == 0) {
323 KASSERT(rt->rt_flags & IEEE80211_MESHRT_FLAGS_PROXY,
324 ("no proxy flag for poxy entry"));
325 struct ieee80211com *ic = vap->iv_ic;
327 * Fix existing entry created by received frames from
328 * stations that have some memory of dest. We also
329 * flush any frames held on the staging queue; delivering
330 * them is too much trouble right now.
332 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH, dest,
333 "%s", "fix proxy entry");
334 IEEE80211_ADDR_COPY(rt->rt_nexthop, vap->iv_myaddr);
335 rt->rt_flags |= IEEE80211_MESHRT_FLAGS_VALID
336 | IEEE80211_MESHRT_FLAGS_PROXY;
337 /* XXX belongs in hwmp */
338 ieee80211_ageq_drain_node(&ic->ic_stageq,
339 (void *)(uintptr_t) ieee80211_mac_hash(ic, dest));
346 mesh_rt_del(struct ieee80211_mesh_state *ms, struct ieee80211_mesh_route *rt)
348 TAILQ_REMOVE(&ms->ms_routes, rt, rt_next);
350 * Grab the lock before destroying it, to be sure no one else
351 * is holding the route.
353 MESH_RT_ENTRY_LOCK(rt);
354 callout_drain(&rt->rt_discovery);
355 MESH_RT_ENTRY_LOCK_DESTROY(rt);
356 IEEE80211_FREE(rt, M_80211_MESH_RT);
360 ieee80211_mesh_rt_del(struct ieee80211vap *vap,
361 const uint8_t dest[IEEE80211_ADDR_LEN])
363 struct ieee80211_mesh_state *ms = vap->iv_mesh;
364 struct ieee80211_mesh_route *rt, *next;
367 TAILQ_FOREACH_SAFE(rt, &ms->ms_routes, rt_next, next) {
368 if (IEEE80211_ADDR_EQ(rt->rt_dest, dest)) {
369 if (rt->rt_flags & IEEE80211_MESHRT_FLAGS_PROXY) {
370 ms->ms_ppath->mpp_senderror(vap, dest, rt,
371 IEEE80211_REASON_MESH_PERR_NO_PROXY);
373 ms->ms_ppath->mpp_senderror(vap, dest, rt,
374 IEEE80211_REASON_MESH_PERR_DEST_UNREACH);
385 ieee80211_mesh_rt_flush(struct ieee80211vap *vap)
387 struct ieee80211_mesh_state *ms = vap->iv_mesh;
388 struct ieee80211_mesh_route *rt, *next;
393 TAILQ_FOREACH_SAFE(rt, &ms->ms_routes, rt_next, next)
399 ieee80211_mesh_rt_flush_peer(struct ieee80211vap *vap,
400 const uint8_t peer[IEEE80211_ADDR_LEN])
402 struct ieee80211_mesh_state *ms = vap->iv_mesh;
403 struct ieee80211_mesh_route *rt, *next;
406 TAILQ_FOREACH_SAFE(rt, &ms->ms_routes, rt_next, next) {
407 if (IEEE80211_ADDR_EQ(rt->rt_nexthop, peer))
414 * Flush expired routing entries, i.e. those in invalid state for
418 mesh_rt_flush_invalid(struct ieee80211vap *vap)
420 struct ieee80211_mesh_state *ms = vap->iv_mesh;
421 struct ieee80211_mesh_route *rt, *next;
426 TAILQ_FOREACH_SAFE(rt, &ms->ms_routes, rt_next, next) {
427 /* Discover paths will be deleted by their own callout */
428 if (rt->rt_flags & IEEE80211_MESHRT_FLAGS_DISCOVER)
430 ieee80211_mesh_rt_update(rt, 0);
431 if ((rt->rt_flags & IEEE80211_MESHRT_FLAGS_VALID) == 0)
437 #define N(a) (sizeof(a) / sizeof(a[0]))
439 ieee80211_mesh_register_proto_path(const struct ieee80211_mesh_proto_path *mpp)
441 int i, firstempty = -1;
443 for (i = 0; i < N(mesh_proto_paths); i++) {
444 if (strncmp(mpp->mpp_descr, mesh_proto_paths[i].mpp_descr,
445 IEEE80211_MESH_PROTO_DSZ) == 0)
447 if (!mesh_proto_paths[i].mpp_active && firstempty == -1)
452 memcpy(&mesh_proto_paths[firstempty], mpp, sizeof(*mpp));
453 mesh_proto_paths[firstempty].mpp_active = 1;
458 ieee80211_mesh_register_proto_metric(const struct
459 ieee80211_mesh_proto_metric *mpm)
461 int i, firstempty = -1;
463 for (i = 0; i < N(mesh_proto_metrics); i++) {
464 if (strncmp(mpm->mpm_descr, mesh_proto_metrics[i].mpm_descr,
465 IEEE80211_MESH_PROTO_DSZ) == 0)
467 if (!mesh_proto_metrics[i].mpm_active && firstempty == -1)
472 memcpy(&mesh_proto_metrics[firstempty], mpm, sizeof(*mpm));
473 mesh_proto_metrics[firstempty].mpm_active = 1;
478 mesh_select_proto_path(struct ieee80211vap *vap, const char *name)
480 struct ieee80211_mesh_state *ms = vap->iv_mesh;
483 for (i = 0; i < N(mesh_proto_paths); i++) {
484 if (strcasecmp(mesh_proto_paths[i].mpp_descr, name) == 0) {
485 ms->ms_ppath = &mesh_proto_paths[i];
493 mesh_select_proto_metric(struct ieee80211vap *vap, const char *name)
495 struct ieee80211_mesh_state *ms = vap->iv_mesh;
498 for (i = 0; i < N(mesh_proto_metrics); i++) {
499 if (strcasecmp(mesh_proto_metrics[i].mpm_descr, name) == 0) {
500 ms->ms_pmetric = &mesh_proto_metrics[i];
509 mesh_gatemode_setup(struct ieee80211vap *vap)
511 struct ieee80211_mesh_state *ms = vap->iv_mesh;
514 * NB: When a mesh gate is running as a ROOT it shall
515 * not send out periodic GANNs but instead mark the
516 * mesh gate flag for the corresponding proactive PREQ
519 if (ms->ms_flags & IEEE80211_MESHFLAGS_ROOT ||
520 (ms->ms_flags & IEEE80211_MESHFLAGS_GATE) == 0) {
521 callout_drain(&ms->ms_gatetimer);
524 callout_reset(&ms->ms_gatetimer, ieee80211_mesh_gateint,
525 mesh_gatemode_cb, vap);
529 mesh_gatemode_cb(void *arg)
531 struct ieee80211vap *vap = (struct ieee80211vap *)arg;
532 struct ieee80211_mesh_state *ms = vap->iv_mesh;
533 struct ieee80211_meshgann_ie gann;
535 gann.gann_flags = 0; /* Reserved */
536 gann.gann_hopcount = 0;
537 gann.gann_ttl = ms->ms_ttl;
538 IEEE80211_ADDR_COPY(gann.gann_addr, vap->iv_myaddr);
539 gann.gann_seq = ms->ms_gateseq++;
540 gann.gann_interval = ieee80211_mesh_gateint;
542 IEEE80211_NOTE(vap, IEEE80211_MSG_MESH, vap->iv_bss,
543 "send broadcast GANN (seq %u)", gann.gann_seq);
545 ieee80211_send_action(vap->iv_bss, IEEE80211_ACTION_CAT_MESH,
546 IEEE80211_ACTION_MESH_GANN, &gann);
547 mesh_gatemode_setup(vap);
551 ieee80211_mesh_init(void)
554 memset(mesh_proto_paths, 0, sizeof(mesh_proto_paths));
555 memset(mesh_proto_metrics, 0, sizeof(mesh_proto_metrics));
558 * Setup mesh parameters that depends on the clock frequency.
560 ieee80211_mesh_gateint = msecs_to_ticks(10000);
561 ieee80211_mesh_retrytimeout = msecs_to_ticks(40);
562 ieee80211_mesh_holdingtimeout = msecs_to_ticks(40);
563 ieee80211_mesh_confirmtimeout = msecs_to_ticks(40);
564 ieee80211_mesh_backofftimeout = msecs_to_ticks(5000);
567 * Register action frame handlers.
569 ieee80211_recv_action_register(IEEE80211_ACTION_CAT_SELF_PROT,
570 IEEE80211_ACTION_MESHPEERING_OPEN,
571 mesh_recv_action_meshpeering_open);
572 ieee80211_recv_action_register(IEEE80211_ACTION_CAT_SELF_PROT,
573 IEEE80211_ACTION_MESHPEERING_CONFIRM,
574 mesh_recv_action_meshpeering_confirm);
575 ieee80211_recv_action_register(IEEE80211_ACTION_CAT_SELF_PROT,
576 IEEE80211_ACTION_MESHPEERING_CLOSE,
577 mesh_recv_action_meshpeering_close);
578 ieee80211_recv_action_register(IEEE80211_ACTION_CAT_MESH,
579 IEEE80211_ACTION_MESH_LMETRIC, mesh_recv_action_meshlmetric);
580 ieee80211_recv_action_register(IEEE80211_ACTION_CAT_MESH,
581 IEEE80211_ACTION_MESH_GANN, mesh_recv_action_meshgate);
583 ieee80211_send_action_register(IEEE80211_ACTION_CAT_SELF_PROT,
584 IEEE80211_ACTION_MESHPEERING_OPEN,
585 mesh_send_action_meshpeering_open);
586 ieee80211_send_action_register(IEEE80211_ACTION_CAT_SELF_PROT,
587 IEEE80211_ACTION_MESHPEERING_CONFIRM,
588 mesh_send_action_meshpeering_confirm);
589 ieee80211_send_action_register(IEEE80211_ACTION_CAT_SELF_PROT,
590 IEEE80211_ACTION_MESHPEERING_CLOSE,
591 mesh_send_action_meshpeering_close);
592 ieee80211_send_action_register(IEEE80211_ACTION_CAT_MESH,
593 IEEE80211_ACTION_MESH_LMETRIC,
594 mesh_send_action_meshlmetric);
595 ieee80211_send_action_register(IEEE80211_ACTION_CAT_MESH,
596 IEEE80211_ACTION_MESH_GANN,
597 mesh_send_action_meshgate);
600 * Register Airtime Link Metric.
602 ieee80211_mesh_register_proto_metric(&mesh_metric_airtime);
605 SYSINIT(wlan_mesh, SI_SUB_DRIVERS, SI_ORDER_FIRST, ieee80211_mesh_init, NULL);
608 ieee80211_mesh_attach(struct ieee80211com *ic)
610 ic->ic_vattach[IEEE80211_M_MBSS] = mesh_vattach;
614 ieee80211_mesh_detach(struct ieee80211com *ic)
619 mesh_vdetach_peers(void *arg, struct ieee80211_node *ni)
621 struct ieee80211com *ic = ni->ni_ic;
624 if (ni->ni_mlstate == IEEE80211_NODE_MESH_ESTABLISHED) {
625 args[0] = ni->ni_mlpid;
626 args[1] = ni->ni_mllid;
627 args[2] = IEEE80211_REASON_PEER_LINK_CANCELED;
628 ieee80211_send_action(ni,
629 IEEE80211_ACTION_CAT_SELF_PROT,
630 IEEE80211_ACTION_MESHPEERING_CLOSE,
633 callout_drain(&ni->ni_mltimer);
634 /* XXX belongs in hwmp */
635 ieee80211_ageq_drain_node(&ic->ic_stageq,
636 (void *)(uintptr_t) ieee80211_mac_hash(ic, ni->ni_macaddr));
640 mesh_vdetach(struct ieee80211vap *vap)
642 struct ieee80211_mesh_state *ms = vap->iv_mesh;
644 callout_drain(&ms->ms_cleantimer);
645 ieee80211_iterate_nodes(&vap->iv_ic->ic_sta, mesh_vdetach_peers,
647 ieee80211_mesh_rt_flush(vap);
648 MESH_RT_LOCK_DESTROY(ms);
649 ms->ms_ppath->mpp_vdetach(vap);
650 IEEE80211_FREE(vap->iv_mesh, M_80211_VAP);
655 mesh_vattach(struct ieee80211vap *vap)
657 struct ieee80211_mesh_state *ms;
658 vap->iv_newstate = mesh_newstate;
659 vap->iv_input = mesh_input;
660 vap->iv_opdetach = mesh_vdetach;
661 vap->iv_recv_mgmt = mesh_recv_mgmt;
662 vap->iv_recv_ctl = mesh_recv_ctl;
663 ms = IEEE80211_MALLOC(sizeof(struct ieee80211_mesh_state), M_80211_VAP,
664 IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
666 printf("%s: couldn't alloc MBSS state\n", __func__);
671 ms->ms_flags = (IEEE80211_MESHFLAGS_AP | IEEE80211_MESHFLAGS_FWD);
672 ms->ms_ttl = IEEE80211_MESH_DEFAULT_TTL;
673 TAILQ_INIT(&ms->ms_known_gates);
674 TAILQ_INIT(&ms->ms_routes);
675 MESH_RT_LOCK_INIT(ms, "MBSS");
676 callout_init(&ms->ms_cleantimer, 1);
677 callout_init(&ms->ms_gatetimer, 1);
679 mesh_select_proto_metric(vap, "AIRTIME");
680 KASSERT(ms->ms_pmetric, ("ms_pmetric == NULL"));
681 mesh_select_proto_path(vap, "HWMP");
682 KASSERT(ms->ms_ppath, ("ms_ppath == NULL"));
683 ms->ms_ppath->mpp_vattach(vap);
687 * IEEE80211_M_MBSS vap state machine handler.
690 mesh_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
692 struct ieee80211_mesh_state *ms = vap->iv_mesh;
693 struct ieee80211com *ic = vap->iv_ic;
694 struct ieee80211_node *ni;
695 enum ieee80211_state ostate;
697 IEEE80211_LOCK_ASSERT(ic);
699 ostate = vap->iv_state;
700 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE, "%s: %s -> %s (%d)\n",
701 __func__, ieee80211_state_name[ostate],
702 ieee80211_state_name[nstate], arg);
703 vap->iv_state = nstate; /* state transition */
704 if (ostate != IEEE80211_S_SCAN)
705 ieee80211_cancel_scan(vap); /* background scan */
706 ni = vap->iv_bss; /* NB: no reference held */
707 if (nstate != IEEE80211_S_RUN && ostate == IEEE80211_S_RUN) {
708 callout_drain(&ms->ms_cleantimer);
709 callout_drain(&ms->ms_gatetimer);
712 case IEEE80211_S_INIT:
714 case IEEE80211_S_SCAN:
715 ieee80211_cancel_scan(vap);
717 case IEEE80211_S_CAC:
718 ieee80211_dfs_cac_stop(vap);
720 case IEEE80211_S_RUN:
721 ieee80211_iterate_nodes(&ic->ic_sta,
722 mesh_vdetach_peers, NULL);
727 if (ostate != IEEE80211_S_INIT) {
728 /* NB: optimize INIT -> INIT case */
729 ieee80211_reset_bss(vap);
730 ieee80211_mesh_rt_flush(vap);
733 case IEEE80211_S_SCAN:
735 case IEEE80211_S_INIT:
736 if (vap->iv_des_chan != IEEE80211_CHAN_ANYC &&
737 !IEEE80211_IS_CHAN_RADAR(vap->iv_des_chan) &&
740 * Already have a channel and a mesh ID; bypass
741 * the scan and startup immediately.
743 ieee80211_create_ibss(vap, vap->iv_des_chan);
747 * Initiate a scan. We can come here as a result
748 * of an IEEE80211_IOC_SCAN_REQ too in which case
749 * the vap will be marked with IEEE80211_FEXT_SCANREQ
750 * and the scan request parameters will be present
751 * in iv_scanreq. Otherwise we do the default.
753 if (vap->iv_flags_ext & IEEE80211_FEXT_SCANREQ) {
754 ieee80211_check_scan(vap,
755 vap->iv_scanreq_flags,
756 vap->iv_scanreq_duration,
757 vap->iv_scanreq_mindwell,
758 vap->iv_scanreq_maxdwell,
759 vap->iv_scanreq_nssid, vap->iv_scanreq_ssid);
760 vap->iv_flags_ext &= ~IEEE80211_FEXT_SCANREQ;
762 ieee80211_check_scan_current(vap);
768 case IEEE80211_S_CAC:
770 * Start CAC on a DFS channel. We come here when starting
771 * a bss on a DFS channel (see ieee80211_create_ibss).
773 ieee80211_dfs_cac_start(vap);
775 case IEEE80211_S_RUN:
777 case IEEE80211_S_INIT:
779 * Already have a channel; bypass the
780 * scan and startup immediately.
781 * Note that ieee80211_create_ibss will call
782 * back to do a RUN->RUN state change.
784 ieee80211_create_ibss(vap,
785 ieee80211_ht_adjust_channel(ic,
786 ic->ic_curchan, vap->iv_flags_ht));
787 /* NB: iv_bss is changed on return */
789 case IEEE80211_S_CAC:
791 * NB: This is the normal state change when CAC
792 * expires and no radar was detected; no need to
793 * clear the CAC timer as it's already expired.
796 case IEEE80211_S_CSA:
799 * Shorten inactivity timer of associated stations
800 * to weed out sta's that don't follow a CSA.
802 ieee80211_iterate_nodes(&ic->ic_sta, sta_csa, vap);
805 * Update bss node channel to reflect where
806 * we landed after CSA.
808 ieee80211_node_set_chan(vap->iv_bss,
809 ieee80211_ht_adjust_channel(ic, ic->ic_curchan,
810 ieee80211_htchanflags(vap->iv_bss->ni_chan)));
811 /* XXX bypass debug msgs */
813 case IEEE80211_S_SCAN:
814 case IEEE80211_S_RUN:
815 #ifdef IEEE80211_DEBUG
816 if (ieee80211_msg_debug(vap)) {
817 struct ieee80211_node *ni = vap->iv_bss;
819 "synchronized with %s meshid ",
820 ether_sprintf(ni->ni_meshid));
821 ieee80211_print_essid(ni->ni_meshid,
824 printf(" channel %d\n",
825 ieee80211_chan2ieee(ic, ic->ic_curchan));
832 ieee80211_node_authorize(vap->iv_bss);
833 callout_reset(&ms->ms_cleantimer, ms->ms_ppath->mpp_inact,
834 mesh_rt_cleanup_cb, vap);
835 mesh_gatemode_setup(vap);
840 /* NB: ostate not nstate */
841 ms->ms_ppath->mpp_newstate(vap, ostate, arg);
846 mesh_rt_cleanup_cb(void *arg)
848 struct ieee80211vap *vap = arg;
849 struct ieee80211_mesh_state *ms = vap->iv_mesh;
851 mesh_rt_flush_invalid(vap);
852 callout_reset(&ms->ms_cleantimer, ms->ms_ppath->mpp_inact,
853 mesh_rt_cleanup_cb, vap);
857 * Mark a mesh STA as gate and return a pointer to it.
858 * If this is first time, we create a new gate route.
859 * Always update the path route to this mesh gate.
861 struct ieee80211_mesh_gate_route *
862 ieee80211_mesh_mark_gate(struct ieee80211vap *vap, const uint8_t *addr,
863 struct ieee80211_mesh_route *rt)
865 struct ieee80211_mesh_state *ms = vap->iv_mesh;
866 struct ieee80211_mesh_gate_route *gr = NULL, *next;
870 TAILQ_FOREACH_SAFE(gr, &ms->ms_known_gates, gr_next, next) {
871 if (IEEE80211_ADDR_EQ(gr->gr_addr, addr)) {
878 /* New mesh gate add it to known table. */
879 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH, addr,
880 "%s", "stored new gate information from pro-PREQ.");
881 gr = IEEE80211_MALLOC(ALIGN(sizeof(struct ieee80211_mesh_gate_route)),
883 IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
884 IEEE80211_ADDR_COPY(gr->gr_addr, addr);
885 TAILQ_INSERT_TAIL(&ms->ms_known_gates, gr, gr_next);
888 /* TODO: link from path route to gate route */
896 * Helper function to note the Mesh Peer Link FSM change.
899 mesh_linkchange(struct ieee80211_node *ni, enum ieee80211_mesh_mlstate state)
901 struct ieee80211vap *vap = ni->ni_vap;
902 struct ieee80211_mesh_state *ms = vap->iv_mesh;
903 #ifdef IEEE80211_DEBUG
904 static const char *meshlinkstates[] = {
905 [IEEE80211_NODE_MESH_IDLE] = "IDLE",
906 [IEEE80211_NODE_MESH_OPENSNT] = "OPEN SENT",
907 [IEEE80211_NODE_MESH_OPENRCV] = "OPEN RECEIVED",
908 [IEEE80211_NODE_MESH_CONFIRMRCV] = "CONFIRM RECEIVED",
909 [IEEE80211_NODE_MESH_ESTABLISHED] = "ESTABLISHED",
910 [IEEE80211_NODE_MESH_HOLDING] = "HOLDING"
913 IEEE80211_NOTE(vap, IEEE80211_MSG_MESH,
914 ni, "peer link: %s -> %s",
915 meshlinkstates[ni->ni_mlstate], meshlinkstates[state]);
917 /* track neighbor count */
918 if (state == IEEE80211_NODE_MESH_ESTABLISHED &&
919 ni->ni_mlstate != IEEE80211_NODE_MESH_ESTABLISHED) {
920 KASSERT(ms->ms_neighbors < 65535, ("neighbor count overflow"));
922 ieee80211_beacon_notify(vap, IEEE80211_BEACON_MESHCONF);
923 } else if (ni->ni_mlstate == IEEE80211_NODE_MESH_ESTABLISHED &&
924 state != IEEE80211_NODE_MESH_ESTABLISHED) {
925 KASSERT(ms->ms_neighbors > 0, ("neighbor count 0"));
927 ieee80211_beacon_notify(vap, IEEE80211_BEACON_MESHCONF);
929 ni->ni_mlstate = state;
931 case IEEE80211_NODE_MESH_HOLDING:
932 ms->ms_ppath->mpp_peerdown(ni);
934 case IEEE80211_NODE_MESH_ESTABLISHED:
935 ieee80211_mesh_discover(vap, ni->ni_macaddr, NULL);
943 * Helper function to generate a unique local ID required for mesh
944 * peer establishment.
947 mesh_checkid(void *arg, struct ieee80211_node *ni)
951 if (*r == ni->ni_mllid)
952 *(uint16_t *)arg = 0;
956 mesh_generateid(struct ieee80211vap *vap)
962 get_random_bytes(&r, 2);
963 ieee80211_iterate_nodes(&vap->iv_ic->ic_sta, mesh_checkid, &r);
965 } while (r == 0 && maxiter > 0);
970 * Verifies if we already received this packet by checking its
972 * Returns 0 if the frame is to be accepted, 1 otherwise.
975 mesh_checkpseq(struct ieee80211vap *vap,
976 const uint8_t source[IEEE80211_ADDR_LEN], uint32_t seq)
978 struct ieee80211_mesh_route *rt;
980 rt = ieee80211_mesh_rt_find(vap, source);
982 rt = ieee80211_mesh_rt_add(vap, source);
984 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH, source,
985 "%s", "add mcast route failed");
986 vap->iv_stats.is_mesh_rtaddfailed++;
989 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH, source,
990 "add mcast route, mesh seqno %d", seq);
991 rt->rt_lastmseq = seq;
994 if (IEEE80211_MESH_SEQ_GEQ(rt->rt_lastmseq, seq)) {
997 rt->rt_lastmseq = seq;
1003 * Iterate the routing table and locate the next hop.
1005 struct ieee80211_node *
1006 ieee80211_mesh_find_txnode(struct ieee80211vap *vap,
1007 const uint8_t dest[IEEE80211_ADDR_LEN])
1009 struct ieee80211_mesh_route *rt;
1011 rt = ieee80211_mesh_rt_find(vap, dest);
1014 if ((rt->rt_flags & IEEE80211_MESHRT_FLAGS_VALID) == 0) {
1015 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH, dest,
1016 "%s: !valid, flags 0x%x", __func__, rt->rt_flags);
1020 if (rt->rt_flags & IEEE80211_MESHRT_FLAGS_PROXY) {
1021 rt = ieee80211_mesh_rt_find(vap, rt->rt_mesh_gate);
1022 if (rt == NULL) return NULL;
1023 if ((rt->rt_flags & IEEE80211_MESHRT_FLAGS_VALID) == 0) {
1024 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH, dest,
1025 "%s: meshgate !valid, flags 0x%x", __func__,
1031 return ieee80211_find_txnode(vap, rt->rt_nexthop);
1035 mesh_transmit_to_gate(struct ieee80211vap *vap, struct mbuf *m,
1036 struct ieee80211_mesh_route *rt_gate)
1038 struct ifnet *ifp = vap->iv_ifp;
1039 struct ieee80211_node *ni;
1041 IEEE80211_TX_UNLOCK_ASSERT(vap->iv_ic);
1043 ni = ieee80211_mesh_find_txnode(vap, rt_gate->rt_dest);
1045 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1051 * Send through the VAP packet transmit path.
1052 * This consumes the node ref grabbed above and
1053 * the mbuf, regardless of whether there's a problem
1056 (void) ieee80211_vap_pkt_send_dest(vap, m, ni);
1060 * Forward the queued frames to known valid mesh gates.
1061 * Assume destination to be outside the MBSS (i.e. proxy entry),
1062 * If no valid mesh gates are known silently discard queued frames.
1063 * After transmitting frames to all known valid mesh gates, this route
1064 * will be marked invalid, and a new path discovery will happen in the hopes
1065 * that (at least) one of the mesh gates have a new proxy entry for us to use.
1068 ieee80211_mesh_forward_to_gates(struct ieee80211vap *vap,
1069 struct ieee80211_mesh_route *rt_dest)
1071 struct ieee80211com *ic = vap->iv_ic;
1072 struct ieee80211_mesh_state *ms = vap->iv_mesh;
1073 struct ieee80211_mesh_route *rt_gate;
1074 struct ieee80211_mesh_gate_route *gr = NULL, *gr_next;
1075 struct mbuf *m, *mcopy, *next;
1077 IEEE80211_TX_UNLOCK_ASSERT(ic);
1079 KASSERT( rt_dest->rt_flags == IEEE80211_MESHRT_FLAGS_DISCOVER,
1080 ("Route is not marked with IEEE80211_MESHRT_FLAGS_DISCOVER"));
1082 /* XXX: send to more than one valid mash gate */
1085 m = ieee80211_ageq_remove(&ic->ic_stageq,
1086 (struct ieee80211_node *)(uintptr_t)
1087 ieee80211_mac_hash(ic, rt_dest->rt_dest));
1089 TAILQ_FOREACH_SAFE(gr, &ms->ms_known_gates, gr_next, gr_next) {
1090 rt_gate = gr->gr_route;
1091 if (rt_gate == NULL) {
1092 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_HWMP,
1094 "mesh gate with no path %6D",
1098 if ((rt_gate->rt_flags & IEEE80211_MESHRT_FLAGS_VALID) == 0)
1100 KASSERT(rt_gate->rt_flags & IEEE80211_MESHRT_FLAGS_GATE,
1101 ("route not marked as a mesh gate"));
1102 KASSERT((rt_gate->rt_flags &
1103 IEEE80211_MESHRT_FLAGS_PROXY) == 0,
1104 ("found mesh gate that is also marked porxy"));
1106 * convert route to a proxy route gated by the current
1107 * mesh gate, this is needed so encap can built data
1108 * frame with correct address.
1110 rt_dest->rt_flags = IEEE80211_MESHRT_FLAGS_PROXY |
1111 IEEE80211_MESHRT_FLAGS_VALID;
1112 rt_dest->rt_ext_seq = 1; /* random value */
1113 IEEE80211_ADDR_COPY(rt_dest->rt_mesh_gate, rt_gate->rt_dest);
1114 IEEE80211_ADDR_COPY(rt_dest->rt_nexthop, rt_gate->rt_nexthop);
1115 rt_dest->rt_metric = rt_gate->rt_metric;
1116 rt_dest->rt_nhops = rt_gate->rt_nhops;
1117 ieee80211_mesh_rt_update(rt_dest, ms->ms_ppath->mpp_inact);
1120 mcopy = m_dup(m, M_NOWAIT);
1121 for (; mcopy != NULL; mcopy = next) {
1122 next = mcopy->m_nextpkt;
1123 mcopy->m_nextpkt = NULL;
1124 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_HWMP,
1126 "flush queued frame %p len %d", mcopy,
1127 mcopy->m_pkthdr.len);
1128 mesh_transmit_to_gate(vap, mcopy, rt_gate);
1132 rt_dest->rt_flags = 0; /* Mark invalid */
1138 * Forward the specified frame.
1139 * Decrement the TTL and set TA to our MAC address.
1142 mesh_forward(struct ieee80211vap *vap, struct mbuf *m,
1143 const struct ieee80211_meshcntl *mc)
1145 struct ieee80211com *ic = vap->iv_ic;
1146 struct ieee80211_mesh_state *ms = vap->iv_mesh;
1147 struct ifnet *ifp = vap->iv_ifp;
1148 const struct ieee80211_frame *wh =
1149 mtod(m, const struct ieee80211_frame *);
1151 struct ieee80211_meshcntl *mccopy;
1152 struct ieee80211_frame *whcopy;
1153 struct ieee80211_node *ni;
1156 /* This is called from the RX path - don't hold this lock */
1157 IEEE80211_TX_UNLOCK_ASSERT(ic);
1160 * mesh ttl of 1 means we are the last one receving it,
1161 * according to amendment we decrement and then check if
1162 * 0, if so we dont forward.
1164 if (mc->mc_ttl < 1) {
1165 IEEE80211_NOTE_FRAME(vap, IEEE80211_MSG_MESH, wh,
1166 "%s", "frame not fwd'd, ttl 1");
1167 vap->iv_stats.is_mesh_fwd_ttl++;
1170 if (!(ms->ms_flags & IEEE80211_MESHFLAGS_FWD)) {
1171 IEEE80211_NOTE_FRAME(vap, IEEE80211_MSG_MESH, wh,
1172 "%s", "frame not fwd'd, fwding disabled");
1173 vap->iv_stats.is_mesh_fwd_disabled++;
1176 mcopy = m_dup(m, M_NOWAIT);
1177 if (mcopy == NULL) {
1178 IEEE80211_NOTE_FRAME(vap, IEEE80211_MSG_MESH, wh,
1179 "%s", "frame not fwd'd, cannot dup");
1180 vap->iv_stats.is_mesh_fwd_nobuf++;
1181 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1184 mcopy = m_pullup(mcopy, ieee80211_hdrspace(ic, wh) +
1185 sizeof(struct ieee80211_meshcntl));
1186 if (mcopy == NULL) {
1187 IEEE80211_NOTE_FRAME(vap, IEEE80211_MSG_MESH, wh,
1188 "%s", "frame not fwd'd, too short");
1189 vap->iv_stats.is_mesh_fwd_tooshort++;
1190 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1194 whcopy = mtod(mcopy, struct ieee80211_frame *);
1195 mccopy = (struct ieee80211_meshcntl *)
1196 (mtod(mcopy, uint8_t *) + ieee80211_hdrspace(ic, wh));
1197 /* XXX clear other bits? */
1198 whcopy->i_fc[1] &= ~IEEE80211_FC1_RETRY;
1199 IEEE80211_ADDR_COPY(whcopy->i_addr2, vap->iv_myaddr);
1200 if (IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1201 ni = ieee80211_ref_node(vap->iv_bss);
1202 mcopy->m_flags |= M_MCAST;
1204 ni = ieee80211_mesh_find_txnode(vap, whcopy->i_addr3);
1207 * [Optional] any of the following three actions:
1208 * o silently discard
1209 * o trigger a path discovery
1210 * o inform TA that meshDA is unknown.
1212 IEEE80211_NOTE_FRAME(vap, IEEE80211_MSG_MESH, wh,
1213 "%s", "frame not fwd'd, no path");
1214 ms->ms_ppath->mpp_senderror(vap, whcopy->i_addr3, NULL,
1215 IEEE80211_REASON_MESH_PERR_NO_FI);
1216 vap->iv_stats.is_mesh_fwd_nopath++;
1220 IEEE80211_ADDR_COPY(whcopy->i_addr1, ni->ni_macaddr);
1222 KASSERT(mccopy->mc_ttl > 0, ("%s called with wrong ttl", __func__));
1225 /* XXX calculate priority so drivers can find the tx queue */
1226 M_WME_SETAC(mcopy, WME_AC_BE);
1228 /* XXX do we know m_nextpkt is NULL? */
1229 mcopy->m_pkthdr.rcvif = (void *) ni;
1232 * XXX this bypasses all of the VAP TX handling; it passes frames
1233 * directly to the parent interface.
1235 * Because of this, there's no TX lock being held as there's no
1236 * encaps state being used.
1238 * Doing a direct parent transmit may not be the correct thing
1239 * to do here; we'll have to re-think this soon.
1241 IEEE80211_TX_LOCK(ic);
1242 err = ieee80211_parent_xmitpkt(ic, mcopy);
1243 IEEE80211_TX_UNLOCK(ic);
1245 /* NB: IFQ_HANDOFF reclaims mbuf */
1246 ieee80211_free_node(ni);
1248 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
1252 static struct mbuf *
1253 mesh_decap(struct ieee80211vap *vap, struct mbuf *m, int hdrlen, int meshdrlen)
1255 #define WHDIR(wh) ((wh)->i_fc[1] & IEEE80211_FC1_DIR_MASK)
1256 #define MC01(mc) ((const struct ieee80211_meshcntl_ae01 *)mc)
1257 uint8_t b[sizeof(struct ieee80211_qosframe_addr4) +
1258 sizeof(struct ieee80211_meshcntl_ae10)];
1259 const struct ieee80211_qosframe_addr4 *wh;
1260 const struct ieee80211_meshcntl_ae10 *mc;
1261 struct ether_header *eh;
1265 if (m->m_len < hdrlen + sizeof(*llc) &&
1266 (m = m_pullup(m, hdrlen + sizeof(*llc))) == NULL) {
1267 IEEE80211_DPRINTF(vap, IEEE80211_MSG_ANY,
1268 "discard data frame: %s", "m_pullup failed");
1269 vap->iv_stats.is_rx_tooshort++;
1272 memcpy(b, mtod(m, caddr_t), hdrlen);
1273 wh = (const struct ieee80211_qosframe_addr4 *)&b[0];
1274 mc = (const struct ieee80211_meshcntl_ae10 *)&b[hdrlen - meshdrlen];
1275 KASSERT(WHDIR(wh) == IEEE80211_FC1_DIR_FROMDS ||
1276 WHDIR(wh) == IEEE80211_FC1_DIR_DSTODS,
1277 ("bogus dir, fc 0x%x:0x%x", wh->i_fc[0], wh->i_fc[1]));
1279 llc = (struct llc *)(mtod(m, caddr_t) + hdrlen);
1280 if (llc->llc_dsap == LLC_SNAP_LSAP && llc->llc_ssap == LLC_SNAP_LSAP &&
1281 llc->llc_control == LLC_UI && llc->llc_snap.org_code[0] == 0 &&
1282 llc->llc_snap.org_code[1] == 0 && llc->llc_snap.org_code[2] == 0 &&
1283 /* NB: preserve AppleTalk frames that have a native SNAP hdr */
1284 !(llc->llc_snap.ether_type == htons(ETHERTYPE_AARP) ||
1285 llc->llc_snap.ether_type == htons(ETHERTYPE_IPX))) {
1286 m_adj(m, hdrlen + sizeof(struct llc) - sizeof(*eh));
1289 m_adj(m, hdrlen - sizeof(*eh));
1291 eh = mtod(m, struct ether_header *);
1292 ae = mc->mc_flags & IEEE80211_MESH_AE_MASK;
1293 if (WHDIR(wh) == IEEE80211_FC1_DIR_FROMDS) {
1294 IEEE80211_ADDR_COPY(eh->ether_dhost, wh->i_addr1);
1295 if (ae == IEEE80211_MESH_AE_00) {
1296 IEEE80211_ADDR_COPY(eh->ether_shost, wh->i_addr3);
1297 } else if (ae == IEEE80211_MESH_AE_01) {
1298 IEEE80211_ADDR_COPY(eh->ether_shost,
1299 MC01(mc)->mc_addr4);
1301 IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
1302 (const struct ieee80211_frame *)wh, NULL,
1304 vap->iv_stats.is_mesh_badae++;
1309 if (ae == IEEE80211_MESH_AE_00) {
1310 IEEE80211_ADDR_COPY(eh->ether_dhost, wh->i_addr3);
1311 IEEE80211_ADDR_COPY(eh->ether_shost, wh->i_addr4);
1312 } else if (ae == IEEE80211_MESH_AE_10) {
1313 IEEE80211_ADDR_COPY(eh->ether_dhost, mc->mc_addr5);
1314 IEEE80211_ADDR_COPY(eh->ether_shost, mc->mc_addr6);
1316 IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
1317 (const struct ieee80211_frame *)wh, NULL,
1319 vap->iv_stats.is_mesh_badae++;
1324 #ifndef __NO_STRICT_ALIGNMENT
1325 if (!ALIGNED_POINTER(mtod(m, caddr_t) + sizeof(*eh), uint32_t)) {
1326 m = ieee80211_realign(vap, m, sizeof(*eh));
1330 #endif /* !__NO_STRICT_ALIGNMENT */
1332 eh = mtod(m, struct ether_header *);
1333 eh->ether_type = htons(m->m_pkthdr.len - sizeof(*eh));
1341 * Return non-zero if the unicast mesh data frame should be processed
1342 * locally. Frames that are not proxy'd have our address, otherwise
1343 * we need to consult the routing table to look for a proxy entry.
1346 mesh_isucastforme(struct ieee80211vap *vap, const struct ieee80211_frame *wh,
1347 const struct ieee80211_meshcntl *mc)
1349 int ae = mc->mc_flags & 3;
1351 KASSERT((wh->i_fc[1] & IEEE80211_FC1_DIR_MASK) == IEEE80211_FC1_DIR_DSTODS,
1352 ("bad dir 0x%x:0x%x", wh->i_fc[0], wh->i_fc[1]));
1353 KASSERT(ae == IEEE80211_MESH_AE_00 || ae == IEEE80211_MESH_AE_10,
1355 if (ae == IEEE80211_MESH_AE_10) { /* ucast w/ proxy */
1356 const struct ieee80211_meshcntl_ae10 *mc10 =
1357 (const struct ieee80211_meshcntl_ae10 *) mc;
1358 struct ieee80211_mesh_route *rt =
1359 ieee80211_mesh_rt_find(vap, mc10->mc_addr5);
1360 /* check for proxy route to ourself */
1361 return (rt != NULL &&
1362 (rt->rt_flags & IEEE80211_MESHRT_FLAGS_PROXY));
1363 } else /* ucast w/o proxy */
1364 return IEEE80211_ADDR_EQ(wh->i_addr3, vap->iv_myaddr);
1368 * Verifies transmitter, updates lifetime, precursor list and forwards data.
1369 * > 0 means we have forwarded data and no need to process locally
1370 * == 0 means we want to process locally (and we may have forwarded data
1371 * < 0 means there was an error and data should be discarded
1374 mesh_recv_indiv_data_to_fwrd(struct ieee80211vap *vap, struct mbuf *m,
1375 struct ieee80211_frame *wh, const struct ieee80211_meshcntl *mc)
1377 struct ieee80211_qosframe_addr4 *qwh;
1378 struct ieee80211_mesh_state *ms = vap->iv_mesh;
1379 struct ieee80211_mesh_route *rt_meshda, *rt_meshsa;
1381 /* This is called from the RX path - don't hold this lock */
1382 IEEE80211_TX_UNLOCK_ASSERT(vap->iv_ic);
1384 qwh = (struct ieee80211_qosframe_addr4 *)wh;
1388 * o verify addr2 is a legitimate transmitter
1389 * o lifetime of precursor of addr3 (addr2) is max(init, curr)
1390 * o lifetime of precursor of addr4 (nexthop) is max(init, curr)
1393 /* set lifetime of addr3 (meshDA) to initial value */
1394 rt_meshda = ieee80211_mesh_rt_find(vap, qwh->i_addr3);
1395 if (rt_meshda == NULL) {
1396 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH, qwh->i_addr2,
1397 "no route to meshDA(%6D)", qwh->i_addr3, ":");
1399 * [Optional] any of the following three actions:
1400 * o silently discard [X]
1401 * o trigger a path discovery [ ]
1402 * o inform TA that meshDA is unknown. [ ]
1408 ieee80211_mesh_rt_update(rt_meshda, ticks_to_msecs(
1409 ms->ms_ppath->mpp_inact));
1411 /* set lifetime of addr4 (meshSA) to initial value */
1412 rt_meshsa = ieee80211_mesh_rt_find(vap, qwh->i_addr4);
1413 KASSERT(rt_meshsa != NULL, ("no route"));
1414 ieee80211_mesh_rt_update(rt_meshsa, ticks_to_msecs(
1415 ms->ms_ppath->mpp_inact));
1417 mesh_forward(vap, m, mc);
1418 return (1); /* dont process locally */
1422 * Verifies transmitter, updates lifetime, precursor list and process data
1423 * locally, if data is proxy with AE = 10 it could mean data should go
1424 * on another mesh path or data should be forwarded to the DS.
1426 * > 0 means we have forwarded data and no need to process locally
1427 * == 0 means we want to process locally (and we may have forwarded data
1428 * < 0 means there was an error and data should be discarded
1431 mesh_recv_indiv_data_to_me(struct ieee80211vap *vap, struct mbuf *m,
1432 struct ieee80211_frame *wh, const struct ieee80211_meshcntl *mc)
1434 struct ieee80211_qosframe_addr4 *qwh;
1435 const struct ieee80211_meshcntl_ae10 *mc10;
1436 struct ieee80211_mesh_state *ms = vap->iv_mesh;
1437 struct ieee80211_mesh_route *rt;
1440 /* This is called from the RX path - don't hold this lock */
1441 IEEE80211_TX_UNLOCK_ASSERT(vap->iv_ic);
1443 qwh = (struct ieee80211_qosframe_addr4 *)wh;
1444 mc10 = (const struct ieee80211_meshcntl_ae10 *)mc;
1448 * o verify addr2 is a legitimate transmitter
1449 * o lifetime of precursor entry is max(init, curr)
1452 /* set lifetime of addr4 (meshSA) to initial value */
1453 rt = ieee80211_mesh_rt_find(vap, qwh->i_addr4);
1454 KASSERT(rt != NULL, ("no route"));
1455 ieee80211_mesh_rt_update(rt, ticks_to_msecs(ms->ms_ppath->mpp_inact));
1458 ae = mc10->mc_flags & IEEE80211_MESH_AE_MASK;
1459 KASSERT(ae == IEEE80211_MESH_AE_00 ||
1460 ae == IEEE80211_MESH_AE_10, ("bad AE %d", ae));
1461 if (ae == IEEE80211_MESH_AE_10) {
1462 if (IEEE80211_ADDR_EQ(mc10->mc_addr5, qwh->i_addr3)) {
1463 return (0); /* process locally */
1466 rt = ieee80211_mesh_rt_find(vap, mc10->mc_addr5);
1468 (rt->rt_flags & IEEE80211_MESHRT_FLAGS_VALID) &&
1469 (rt->rt_flags & IEEE80211_MESHRT_FLAGS_PROXY) == 0) {
1471 * Forward on another mesh-path, according to
1472 * amendment as specified in 9.32.4.1
1474 IEEE80211_ADDR_COPY(qwh->i_addr3, mc10->mc_addr5);
1475 mesh_forward(vap, m,
1476 (const struct ieee80211_meshcntl *)mc10);
1477 return (1); /* dont process locally */
1480 * All other cases: forward of MSDUs from the MBSS to DS indiv.
1481 * addressed according to 13.11.3.2.
1483 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_OUTPUT, qwh->i_addr2,
1484 "forward frame to DS, SA(%6D) DA(%6D)",
1485 mc10->mc_addr6, ":", mc10->mc_addr5, ":");
1487 return (0); /* process locally */
1491 * Try to forward the group addressed data on to other mesh STAs, and
1494 * > 0 means we have forwarded data and no need to process locally
1495 * == 0 means we want to process locally (and we may have forwarded data
1496 * < 0 means there was an error and data should be discarded
1499 mesh_recv_group_data(struct ieee80211vap *vap, struct mbuf *m,
1500 struct ieee80211_frame *wh, const struct ieee80211_meshcntl *mc)
1502 #define MC01(mc) ((const struct ieee80211_meshcntl_ae01 *)mc)
1503 struct ieee80211_mesh_state *ms = vap->iv_mesh;
1505 /* This is called from the RX path - don't hold this lock */
1506 IEEE80211_TX_UNLOCK_ASSERT(vap->iv_ic);
1508 mesh_forward(vap, m, mc);
1510 if(mc->mc_ttl > 0) {
1511 if (mc->mc_flags & IEEE80211_MESH_AE_01) {
1513 * Forward of MSDUs from the MBSS to DS group addressed
1514 * (according to 13.11.3.2)
1515 * This happens by delivering the packet, and a bridge
1516 * will sent it on another port member.
1518 if (ms->ms_flags & IEEE80211_MESHFLAGS_GATE &&
1519 ms->ms_flags & IEEE80211_MESHFLAGS_FWD)
1520 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH,
1521 MC01(mc)->mc_addr4, "%s",
1522 "forward from MBSS to the DS");
1525 return (0); /* process locally */
1530 mesh_input(struct ieee80211_node *ni, struct mbuf *m,
1531 const struct ieee80211_rx_stats *rxs, int rssi, int nf)
1533 #define HAS_SEQ(type) ((type & 0x4) == 0)
1534 #define MC01(mc) ((const struct ieee80211_meshcntl_ae01 *)mc)
1535 #define MC10(mc) ((const struct ieee80211_meshcntl_ae10 *)mc)
1536 struct ieee80211vap *vap = ni->ni_vap;
1537 struct ieee80211com *ic = ni->ni_ic;
1538 struct ifnet *ifp = vap->iv_ifp;
1539 struct ieee80211_frame *wh;
1540 const struct ieee80211_meshcntl *mc;
1541 int hdrspace, meshdrlen, need_tap, error;
1542 uint8_t dir, type, subtype, ae;
1544 const uint8_t *addr;
1546 ieee80211_seq rxseq;
1548 KASSERT(ni != NULL, ("null node"));
1549 ni->ni_inact = ni->ni_inact_reload;
1551 need_tap = 1; /* mbuf need to be tapped. */
1552 type = -1; /* undefined */
1554 /* This is called from the RX path - don't hold this lock */
1555 IEEE80211_TX_UNLOCK_ASSERT(ic);
1557 if (m->m_pkthdr.len < sizeof(struct ieee80211_frame_min)) {
1558 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
1559 ni->ni_macaddr, NULL,
1560 "too short (1): len %u", m->m_pkthdr.len);
1561 vap->iv_stats.is_rx_tooshort++;
1565 * Bit of a cheat here, we use a pointer for a 3-address
1566 * frame format but don't reference fields past outside
1567 * ieee80211_frame_min w/o first validating the data is
1570 wh = mtod(m, struct ieee80211_frame *);
1572 if ((wh->i_fc[0] & IEEE80211_FC0_VERSION_MASK) !=
1573 IEEE80211_FC0_VERSION_0) {
1574 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
1575 ni->ni_macaddr, NULL, "wrong version %x", wh->i_fc[0]);
1576 vap->iv_stats.is_rx_badversion++;
1579 dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK;
1580 type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
1581 subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
1582 if ((ic->ic_flags & IEEE80211_F_SCAN) == 0) {
1583 IEEE80211_RSSI_LPF(ni->ni_avgrssi, rssi);
1585 if (HAS_SEQ(type)) {
1586 uint8_t tid = ieee80211_gettid(wh);
1588 if (IEEE80211_QOS_HAS_SEQ(wh) &&
1589 TID_TO_WME_AC(tid) >= WME_AC_VI)
1590 ic->ic_wme.wme_hipri_traffic++;
1591 rxseq = le16toh(*(uint16_t *)wh->i_seq);
1592 if (! ieee80211_check_rxseq(ni, wh)) {
1593 /* duplicate, discard */
1594 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
1595 wh->i_addr1, "duplicate",
1596 "seqno <%u,%u> fragno <%u,%u> tid %u",
1597 rxseq >> IEEE80211_SEQ_SEQ_SHIFT,
1598 ni->ni_rxseqs[tid] >>
1599 IEEE80211_SEQ_SEQ_SHIFT,
1600 rxseq & IEEE80211_SEQ_FRAG_MASK,
1601 ni->ni_rxseqs[tid] &
1602 IEEE80211_SEQ_FRAG_MASK,
1604 vap->iv_stats.is_rx_dup++;
1605 IEEE80211_NODE_STAT(ni, rx_dup);
1608 ni->ni_rxseqs[tid] = rxseq;
1611 #ifdef IEEE80211_DEBUG
1613 * It's easier, but too expensive, to simulate different mesh
1614 * topologies by consulting the ACL policy very early, so do this
1617 * NB: this check is also done upon peering link initiation.
1619 if (vap->iv_acl != NULL && !vap->iv_acl->iac_check(vap, wh)) {
1620 IEEE80211_DISCARD(vap, IEEE80211_MSG_ACL,
1621 wh, NULL, "%s", "disallowed by ACL");
1622 vap->iv_stats.is_rx_acl++;
1627 case IEEE80211_FC0_TYPE_DATA:
1628 if (ni == vap->iv_bss)
1630 if (ni->ni_mlstate != IEEE80211_NODE_MESH_ESTABLISHED) {
1631 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_MESH,
1632 ni->ni_macaddr, NULL,
1633 "peer link not yet established (%d)",
1635 vap->iv_stats.is_mesh_nolink++;
1638 if (dir != IEEE80211_FC1_DIR_FROMDS &&
1639 dir != IEEE80211_FC1_DIR_DSTODS) {
1640 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
1641 wh, "data", "incorrect dir 0x%x", dir);
1642 vap->iv_stats.is_rx_wrongdir++;
1646 /* All Mesh data frames are QoS subtype */
1647 if (!HAS_SEQ(type)) {
1648 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
1649 wh, "data", "incorrect subtype 0x%x", subtype);
1650 vap->iv_stats.is_rx_badsubtype++;
1655 * Next up, any fragmentation.
1656 * XXX: we defrag before we even try to forward,
1657 * Mesh Control field is not present in sub-sequent
1658 * fragmented frames. This is in contrast to Draft 4.0.
1660 hdrspace = ieee80211_hdrspace(ic, wh);
1661 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1662 m = ieee80211_defrag(ni, m, hdrspace);
1664 /* Fragment dropped or frame not complete yet */
1668 wh = mtod(m, struct ieee80211_frame *); /* NB: after defrag */
1671 * Now we have a complete Mesh Data frame.
1675 * Only fromDStoDS data frames use 4 address qos frames
1676 * as specified in amendment. Otherwise addr4 is located
1677 * in the Mesh Control field and a 3 address qos frame
1680 if (IEEE80211_IS_DSTODS(wh))
1681 *(uint16_t *)qos = *(uint16_t *)
1682 ((struct ieee80211_qosframe_addr4 *)wh)->i_qos;
1684 *(uint16_t *)qos = *(uint16_t *)
1685 ((struct ieee80211_qosframe *)wh)->i_qos;
1688 * NB: The mesh STA sets the Mesh Control Present
1689 * subfield to 1 in the Mesh Data frame containing
1690 * an unfragmented MSDU, an A-MSDU, or the first
1691 * fragment of an MSDU.
1692 * After defrag it should always be present.
1694 if (!(qos[1] & IEEE80211_QOS_MC)) {
1695 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_MESH,
1696 ni->ni_macaddr, NULL,
1697 "%s", "Mesh control field not present");
1698 vap->iv_stats.is_rx_elem_missing++; /* XXX: kinda */
1702 /* pull up enough to get to the mesh control */
1703 if (m->m_len < hdrspace + sizeof(struct ieee80211_meshcntl) &&
1704 (m = m_pullup(m, hdrspace +
1705 sizeof(struct ieee80211_meshcntl))) == NULL) {
1706 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
1707 ni->ni_macaddr, NULL,
1708 "data too short: expecting %u", hdrspace);
1709 vap->iv_stats.is_rx_tooshort++;
1713 * Now calculate the full extent of the headers. Note
1714 * mesh_decap will pull up anything we didn't get
1715 * above when it strips the 802.11 headers.
1717 mc = (const struct ieee80211_meshcntl *)
1718 (mtod(m, const uint8_t *) + hdrspace);
1719 ae = mc->mc_flags & IEEE80211_MESH_AE_MASK;
1720 meshdrlen = sizeof(struct ieee80211_meshcntl) +
1721 ae * IEEE80211_ADDR_LEN;
1722 hdrspace += meshdrlen;
1724 /* pull complete hdrspace = ieee80211_hdrspace + meshcontrol */
1725 if ((meshdrlen > sizeof(struct ieee80211_meshcntl)) &&
1726 (m->m_len < hdrspace) &&
1727 ((m = m_pullup(m, hdrspace)) == NULL)) {
1728 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
1729 ni->ni_macaddr, NULL,
1730 "data too short: expecting %u", hdrspace);
1731 vap->iv_stats.is_rx_tooshort++;
1734 /* XXX: are we sure there is no reallocating after m_pullup? */
1736 seq = LE_READ_4(mc->mc_seq);
1737 if (IEEE80211_IS_MULTICAST(wh->i_addr1))
1739 else if (ae == IEEE80211_MESH_AE_01)
1740 addr = MC01(mc)->mc_addr4;
1742 addr = ((struct ieee80211_qosframe_addr4 *)wh)->i_addr4;
1743 if (IEEE80211_ADDR_EQ(vap->iv_myaddr, addr)) {
1744 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
1745 addr, "data", "%s", "not to me");
1746 vap->iv_stats.is_rx_wrongbss++; /* XXX kinda */
1749 if (mesh_checkpseq(vap, addr, seq) != 0) {
1750 vap->iv_stats.is_rx_dup++;
1754 /* This code "routes" the frame to the right control path */
1755 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1756 if (IEEE80211_ADDR_EQ(vap->iv_myaddr, wh->i_addr3))
1758 mesh_recv_indiv_data_to_me(vap, m, wh, mc);
1759 else if (IEEE80211_IS_MULTICAST(wh->i_addr3))
1760 error = mesh_recv_group_data(vap, m, wh, mc);
1762 error = mesh_recv_indiv_data_to_fwrd(vap, m,
1765 error = mesh_recv_group_data(vap, m, wh, mc);
1771 if (ieee80211_radiotap_active_vap(vap))
1772 ieee80211_radiotap_rx(vap, m);
1776 * Finally, strip the 802.11 header.
1778 m = mesh_decap(vap, m, hdrspace, meshdrlen);
1780 /* XXX mask bit to check for both */
1781 /* don't count Null data frames as errors */
1782 if (subtype == IEEE80211_FC0_SUBTYPE_NODATA ||
1783 subtype == IEEE80211_FC0_SUBTYPE_QOS_NULL)
1785 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
1786 ni->ni_macaddr, "data", "%s", "decap error");
1787 vap->iv_stats.is_rx_decap++;
1788 IEEE80211_NODE_STAT(ni, rx_decap);
1791 if (qos[0] & IEEE80211_QOS_AMSDU) {
1792 m = ieee80211_decap_amsdu(ni, m);
1794 return IEEE80211_FC0_TYPE_DATA;
1796 ieee80211_deliver_data(vap, ni, m);
1798 case IEEE80211_FC0_TYPE_MGT:
1799 vap->iv_stats.is_rx_mgmt++;
1800 IEEE80211_NODE_STAT(ni, rx_mgmt);
1801 if (dir != IEEE80211_FC1_DIR_NODS) {
1802 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
1803 wh, "mgt", "incorrect dir 0x%x", dir);
1804 vap->iv_stats.is_rx_wrongdir++;
1807 if (m->m_pkthdr.len < sizeof(struct ieee80211_frame)) {
1808 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
1809 ni->ni_macaddr, "mgt", "too short: len %u",
1811 vap->iv_stats.is_rx_tooshort++;
1814 #ifdef IEEE80211_DEBUG
1815 if ((ieee80211_msg_debug(vap) &&
1816 (vap->iv_ic->ic_flags & IEEE80211_F_SCAN)) ||
1817 ieee80211_msg_dumppkts(vap)) {
1818 if_printf(ifp, "received %s from %s rssi %d\n",
1819 ieee80211_mgt_subtype_name[subtype >>
1820 IEEE80211_FC0_SUBTYPE_SHIFT],
1821 ether_sprintf(wh->i_addr2), rssi);
1824 if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
1825 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
1826 wh, NULL, "%s", "WEP set but not permitted");
1827 vap->iv_stats.is_rx_mgtdiscard++; /* XXX */
1830 vap->iv_recv_mgmt(ni, m, subtype, rxs, rssi, nf);
1832 case IEEE80211_FC0_TYPE_CTL:
1833 vap->iv_stats.is_rx_ctl++;
1834 IEEE80211_NODE_STAT(ni, rx_ctrl);
1837 IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
1838 wh, "bad", "frame type 0x%x", type);
1839 /* should not come here */
1843 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
1846 if (need_tap && ieee80211_radiotap_active_vap(vap))
1847 ieee80211_radiotap_rx(vap, m);
1857 mesh_recv_mgmt(struct ieee80211_node *ni, struct mbuf *m0, int subtype,
1858 const struct ieee80211_rx_stats *rxs, int rssi, int nf)
1860 struct ieee80211vap *vap = ni->ni_vap;
1861 struct ieee80211_mesh_state *ms = vap->iv_mesh;
1862 struct ieee80211com *ic = ni->ni_ic;
1863 struct ieee80211_channel *rxchan = ic->ic_curchan;
1864 struct ieee80211_frame *wh;
1865 struct ieee80211_mesh_route *rt;
1866 uint8_t *frm, *efrm;
1868 wh = mtod(m0, struct ieee80211_frame *);
1869 frm = (uint8_t *)&wh[1];
1870 efrm = mtod(m0, uint8_t *) + m0->m_len;
1872 case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
1873 case IEEE80211_FC0_SUBTYPE_BEACON:
1875 struct ieee80211_scanparams scan;
1876 struct ieee80211_channel *c;
1878 * We process beacon/probe response
1879 * frames to discover neighbors.
1882 c = ieee80211_lookup_channel_rxstatus(vap, rxs);
1886 if (ieee80211_parse_beacon(ni, m0, rxchan, &scan) != 0)
1889 * Count frame now that we know it's to be processed.
1891 if (subtype == IEEE80211_FC0_SUBTYPE_BEACON) {
1892 vap->iv_stats.is_rx_beacon++; /* XXX remove */
1893 IEEE80211_NODE_STAT(ni, rx_beacons);
1895 IEEE80211_NODE_STAT(ni, rx_proberesp);
1897 * If scanning, just pass information to the scan module.
1899 if (ic->ic_flags & IEEE80211_F_SCAN) {
1900 if (ic->ic_flags_ext & IEEE80211_FEXT_PROBECHAN) {
1902 * Actively scanning a channel marked passive;
1903 * send a probe request now that we know there
1904 * is 802.11 traffic present.
1906 * XXX check if the beacon we recv'd gives
1907 * us what we need and suppress the probe req
1909 ieee80211_probe_curchan(vap, 1);
1910 ic->ic_flags_ext &= ~IEEE80211_FEXT_PROBECHAN;
1912 ieee80211_add_scan(vap, rxchan, &scan, wh,
1917 /* The rest of this code assumes we are running */
1918 if (vap->iv_state != IEEE80211_S_RUN)
1921 * Ignore non-mesh STAs.
1924 (IEEE80211_CAPINFO_ESS|IEEE80211_CAPINFO_IBSS)) ||
1925 scan.meshid == NULL || scan.meshconf == NULL) {
1926 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
1927 wh, "beacon", "%s", "not a mesh sta");
1928 vap->iv_stats.is_mesh_wrongmesh++;
1932 * Ignore STAs for other mesh networks.
1934 if (memcmp(scan.meshid+2, ms->ms_id, ms->ms_idlen) != 0 ||
1935 mesh_verify_meshconf(vap, scan.meshconf)) {
1936 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
1937 wh, "beacon", "%s", "not for our mesh");
1938 vap->iv_stats.is_mesh_wrongmesh++;
1942 * Peer only based on the current ACL policy.
1944 if (vap->iv_acl != NULL && !vap->iv_acl->iac_check(vap, wh)) {
1945 IEEE80211_DISCARD(vap, IEEE80211_MSG_ACL,
1946 wh, NULL, "%s", "disallowed by ACL");
1947 vap->iv_stats.is_rx_acl++;
1951 * Do neighbor discovery.
1953 if (!IEEE80211_ADDR_EQ(wh->i_addr2, ni->ni_macaddr)) {
1955 * Create a new entry in the neighbor table.
1957 ni = ieee80211_add_neighbor(vap, wh, &scan);
1960 * Automatically peer with discovered nodes if possible.
1962 if (ni != vap->iv_bss &&
1963 (ms->ms_flags & IEEE80211_MESHFLAGS_AP)) {
1964 switch (ni->ni_mlstate) {
1965 case IEEE80211_NODE_MESH_IDLE:
1969 /* Wait for backoff callout to reset counter */
1970 if (ni->ni_mlhcnt >= ieee80211_mesh_maxholding)
1973 ni->ni_mlpid = mesh_generateid(vap);
1974 if (ni->ni_mlpid == 0)
1976 mesh_linkchange(ni, IEEE80211_NODE_MESH_OPENSNT);
1977 args[0] = ni->ni_mlpid;
1978 ieee80211_send_action(ni,
1979 IEEE80211_ACTION_CAT_SELF_PROT,
1980 IEEE80211_ACTION_MESHPEERING_OPEN, args);
1982 mesh_peer_timeout_setup(ni);
1985 case IEEE80211_NODE_MESH_ESTABLISHED:
1988 * Valid beacon from a peer mesh STA
1991 rt = ieee80211_mesh_rt_find(vap, wh->i_addr2);
1993 ieee80211_mesh_rt_update(rt,
1995 ms->ms_ppath->mpp_inact));
2005 case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
2007 uint8_t *ssid, *meshid, *rates, *xrates;
2010 if (vap->iv_state != IEEE80211_S_RUN) {
2011 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
2012 wh, NULL, "wrong state %s",
2013 ieee80211_state_name[vap->iv_state]);
2014 vap->iv_stats.is_rx_mgtdiscard++;
2017 if (IEEE80211_IS_MULTICAST(wh->i_addr2)) {
2018 /* frame must be directed */
2019 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
2020 wh, NULL, "%s", "not unicast");
2021 vap->iv_stats.is_rx_mgtdiscard++; /* XXX stat */
2025 * prreq frame format
2027 * [tlv] supported rates
2028 * [tlv] extended supported rates
2031 ssid = meshid = rates = xrates = NULL;
2033 while (efrm - frm > 1) {
2034 IEEE80211_VERIFY_LENGTH(efrm - frm, frm[1] + 2, return);
2036 case IEEE80211_ELEMID_SSID:
2039 case IEEE80211_ELEMID_RATES:
2042 case IEEE80211_ELEMID_XRATES:
2045 case IEEE80211_ELEMID_MESHID:
2051 IEEE80211_VERIFY_ELEMENT(ssid, IEEE80211_NWID_LEN, return);
2052 IEEE80211_VERIFY_ELEMENT(rates, IEEE80211_RATE_MAXSIZE, return);
2054 IEEE80211_VERIFY_ELEMENT(xrates,
2055 IEEE80211_RATE_MAXSIZE - rates[1], return);
2056 if (meshid != NULL) {
2057 IEEE80211_VERIFY_ELEMENT(meshid,
2058 IEEE80211_MESHID_LEN, return);
2059 /* NB: meshid, not ssid */
2060 IEEE80211_VERIFY_SSID(vap->iv_bss, meshid, return);
2063 /* XXX find a better class or define it's own */
2064 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_INPUT, wh->i_addr2,
2065 "%s", "recv probe req");
2067 * Some legacy 11b clients cannot hack a complete
2068 * probe response frame. When the request includes
2069 * only a bare-bones rate set, communicate this to
2070 * the transmit side.
2072 ieee80211_send_proberesp(vap, wh->i_addr2, 0);
2076 case IEEE80211_FC0_SUBTYPE_ACTION:
2077 case IEEE80211_FC0_SUBTYPE_ACTION_NOACK:
2078 if (ni == vap->iv_bss) {
2079 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
2080 wh, NULL, "%s", "unknown node");
2081 vap->iv_stats.is_rx_mgtdiscard++;
2082 } else if (!IEEE80211_ADDR_EQ(vap->iv_myaddr, wh->i_addr1) &&
2083 !IEEE80211_IS_MULTICAST(wh->i_addr1)) {
2084 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
2085 wh, NULL, "%s", "not for us");
2086 vap->iv_stats.is_rx_mgtdiscard++;
2087 } else if (vap->iv_state != IEEE80211_S_RUN) {
2088 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
2089 wh, NULL, "wrong state %s",
2090 ieee80211_state_name[vap->iv_state]);
2091 vap->iv_stats.is_rx_mgtdiscard++;
2093 if (ieee80211_parse_action(ni, m0) == 0)
2094 (void)ic->ic_recv_action(ni, wh, frm, efrm);
2098 case IEEE80211_FC0_SUBTYPE_ASSOC_REQ:
2099 case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
2100 case IEEE80211_FC0_SUBTYPE_REASSOC_REQ:
2101 case IEEE80211_FC0_SUBTYPE_REASSOC_RESP:
2102 case IEEE80211_FC0_SUBTYPE_ATIM:
2103 case IEEE80211_FC0_SUBTYPE_DISASSOC:
2104 case IEEE80211_FC0_SUBTYPE_AUTH:
2105 case IEEE80211_FC0_SUBTYPE_DEAUTH:
2106 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
2107 wh, NULL, "%s", "not handled");
2108 vap->iv_stats.is_rx_mgtdiscard++;
2112 IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
2113 wh, "mgt", "subtype 0x%x not handled", subtype);
2114 vap->iv_stats.is_rx_badsubtype++;
2120 mesh_recv_ctl(struct ieee80211_node *ni, struct mbuf *m, int subtype)
2124 case IEEE80211_FC0_SUBTYPE_BAR:
2125 ieee80211_recv_bar(ni, m);
2131 * Parse meshpeering action ie's for MPM frames
2133 static const struct ieee80211_meshpeer_ie *
2134 mesh_parse_meshpeering_action(struct ieee80211_node *ni,
2135 const struct ieee80211_frame *wh, /* XXX for VERIFY_LENGTH */
2136 const uint8_t *frm, const uint8_t *efrm,
2137 struct ieee80211_meshpeer_ie *mp, uint8_t subtype)
2139 struct ieee80211vap *vap = ni->ni_vap;
2140 const struct ieee80211_meshpeer_ie *mpie;
2142 const uint8_t *meshid, *meshconf, *meshpeer;
2143 uint8_t sendclose = 0; /* 1 = MPM frame rejected, close will be sent */
2145 meshid = meshconf = meshpeer = NULL;
2146 while (efrm - frm > 1) {
2147 IEEE80211_VERIFY_LENGTH(efrm - frm, frm[1] + 2, return NULL);
2149 case IEEE80211_ELEMID_MESHID:
2152 case IEEE80211_ELEMID_MESHCONF:
2155 case IEEE80211_ELEMID_MESHPEER:
2157 mpie = (const struct ieee80211_meshpeer_ie *) frm;
2158 memset(mp, 0, sizeof(*mp));
2159 mp->peer_len = mpie->peer_len;
2160 mp->peer_proto = LE_READ_2(&mpie->peer_proto);
2161 mp->peer_llinkid = LE_READ_2(&mpie->peer_llinkid);
2163 case IEEE80211_ACTION_MESHPEERING_CONFIRM:
2165 LE_READ_2(&mpie->peer_linkid);
2167 case IEEE80211_ACTION_MESHPEERING_CLOSE:
2168 /* NB: peer link ID is optional */
2169 if (mpie->peer_len ==
2170 (IEEE80211_MPM_BASE_SZ + 2)) {
2171 mp->peer_linkid = 0;
2173 LE_READ_2(&mpie->peer_linkid);
2176 LE_READ_2(&mpie->peer_linkid);
2178 LE_READ_2(&mpie->peer_rcode);
2188 * Verify the contents of the frame.
2189 * If it fails validation, close the peer link.
2191 if (mesh_verify_meshpeer(vap, subtype, (const uint8_t *)mp)) {
2193 IEEE80211_DISCARD(vap,
2194 IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH,
2195 wh, NULL, "%s", "MPM validation failed");
2198 /* If meshid is not the same reject any frames type. */
2199 if (sendclose == 0 && mesh_verify_meshid(vap, meshid)) {
2201 IEEE80211_DISCARD(vap,
2202 IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH,
2203 wh, NULL, "%s", "not for our mesh");
2204 if (subtype == IEEE80211_ACTION_MESHPEERING_CLOSE) {
2206 * Standard not clear about this, if we dont ignore
2207 * there will be an endless loop between nodes sending
2208 * CLOSE frames between each other with wrong meshid.
2209 * Discard and timers will bring FSM to IDLE state.
2216 * Close frames are accepted if meshid is the same.
2217 * Verify the other two types.
2219 if (sendclose == 0 && subtype != IEEE80211_ACTION_MESHPEERING_CLOSE &&
2220 mesh_verify_meshconf(vap, meshconf)) {
2222 IEEE80211_DISCARD(vap,
2223 IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH,
2224 wh, NULL, "%s", "configuration missmatch");
2228 vap->iv_stats.is_rx_mgtdiscard++;
2229 switch (ni->ni_mlstate) {
2230 case IEEE80211_NODE_MESH_IDLE:
2231 case IEEE80211_NODE_MESH_ESTABLISHED:
2232 case IEEE80211_NODE_MESH_HOLDING:
2235 case IEEE80211_NODE_MESH_OPENSNT:
2236 case IEEE80211_NODE_MESH_OPENRCV:
2237 case IEEE80211_NODE_MESH_CONFIRMRCV:
2238 args[0] = ni->ni_mlpid;
2239 args[1] = ni->ni_mllid;
2240 /* Reason codes for rejection */
2242 case IEEE80211_ACTION_MESHPEERING_OPEN:
2243 args[2] = IEEE80211_REASON_MESH_CPVIOLATION;
2245 case IEEE80211_ACTION_MESHPEERING_CONFIRM:
2246 args[2] = IEEE80211_REASON_MESH_INCONS_PARAMS;
2249 ieee80211_send_action(ni,
2250 IEEE80211_ACTION_CAT_SELF_PROT,
2251 IEEE80211_ACTION_MESHPEERING_CLOSE,
2253 mesh_linkchange(ni, IEEE80211_NODE_MESH_HOLDING);
2254 mesh_peer_timeout_setup(ni);
2260 return (const struct ieee80211_meshpeer_ie *) mp;
2264 mesh_recv_action_meshpeering_open(struct ieee80211_node *ni,
2265 const struct ieee80211_frame *wh,
2266 const uint8_t *frm, const uint8_t *efrm)
2268 struct ieee80211vap *vap = ni->ni_vap;
2269 struct ieee80211_mesh_state *ms = vap->iv_mesh;
2270 struct ieee80211_meshpeer_ie ie;
2271 const struct ieee80211_meshpeer_ie *meshpeer;
2274 /* +2+2 for action + code + capabilites */
2275 meshpeer = mesh_parse_meshpeering_action(ni, wh, frm+2+2, efrm, &ie,
2276 IEEE80211_ACTION_MESHPEERING_OPEN);
2277 if (meshpeer == NULL) {
2282 IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH, ni,
2283 "recv PEER OPEN, lid 0x%x", meshpeer->peer_llinkid);
2285 switch (ni->ni_mlstate) {
2286 case IEEE80211_NODE_MESH_IDLE:
2287 /* Reject open request if reached our maximum neighbor count */
2288 if (ms->ms_neighbors >= IEEE80211_MESH_MAX_NEIGHBORS) {
2289 args[0] = meshpeer->peer_llinkid;
2291 args[2] = IEEE80211_REASON_MESH_MAX_PEERS;
2292 ieee80211_send_action(ni,
2293 IEEE80211_ACTION_CAT_SELF_PROT,
2294 IEEE80211_ACTION_MESHPEERING_CLOSE,
2296 /* stay in IDLE state */
2299 /* Open frame accepted */
2300 mesh_linkchange(ni, IEEE80211_NODE_MESH_OPENRCV);
2301 ni->ni_mllid = meshpeer->peer_llinkid;
2302 ni->ni_mlpid = mesh_generateid(vap);
2303 if (ni->ni_mlpid == 0)
2305 args[0] = ni->ni_mlpid;
2306 /* Announce we're open too... */
2307 ieee80211_send_action(ni,
2308 IEEE80211_ACTION_CAT_SELF_PROT,
2309 IEEE80211_ACTION_MESHPEERING_OPEN, args);
2310 /* ...and confirm the link. */
2311 args[0] = ni->ni_mlpid;
2312 args[1] = ni->ni_mllid;
2313 ieee80211_send_action(ni,
2314 IEEE80211_ACTION_CAT_SELF_PROT,
2315 IEEE80211_ACTION_MESHPEERING_CONFIRM,
2317 mesh_peer_timeout_setup(ni);
2319 case IEEE80211_NODE_MESH_OPENRCV:
2321 if (ni->ni_mllid != meshpeer->peer_llinkid) {
2322 args[0] = ni->ni_mllid;
2323 args[1] = ni->ni_mlpid;
2324 args[2] = IEEE80211_REASON_PEER_LINK_CANCELED;
2325 ieee80211_send_action(ni,
2326 IEEE80211_ACTION_CAT_SELF_PROT,
2327 IEEE80211_ACTION_MESHPEERING_CLOSE,
2329 mesh_linkchange(ni, IEEE80211_NODE_MESH_HOLDING);
2330 mesh_peer_timeout_setup(ni);
2333 /* Duplicate open, confirm again. */
2334 args[0] = ni->ni_mlpid;
2335 args[1] = ni->ni_mllid;
2336 ieee80211_send_action(ni,
2337 IEEE80211_ACTION_CAT_SELF_PROT,
2338 IEEE80211_ACTION_MESHPEERING_CONFIRM,
2341 case IEEE80211_NODE_MESH_OPENSNT:
2342 ni->ni_mllid = meshpeer->peer_llinkid;
2343 mesh_linkchange(ni, IEEE80211_NODE_MESH_OPENRCV);
2344 args[0] = ni->ni_mlpid;
2345 args[1] = ni->ni_mllid;
2346 ieee80211_send_action(ni,
2347 IEEE80211_ACTION_CAT_SELF_PROT,
2348 IEEE80211_ACTION_MESHPEERING_CONFIRM,
2350 /* NB: don't setup/clear any timeout */
2352 case IEEE80211_NODE_MESH_CONFIRMRCV:
2353 if (ni->ni_mlpid != meshpeer->peer_linkid ||
2354 ni->ni_mllid != meshpeer->peer_llinkid) {
2355 args[0] = ni->ni_mlpid;
2356 args[1] = ni->ni_mllid;
2357 args[2] = IEEE80211_REASON_PEER_LINK_CANCELED;
2358 ieee80211_send_action(ni,
2359 IEEE80211_ACTION_CAT_SELF_PROT,
2360 IEEE80211_ACTION_MESHPEERING_CLOSE,
2363 IEEE80211_NODE_MESH_HOLDING);
2364 mesh_peer_timeout_setup(ni);
2367 mesh_linkchange(ni, IEEE80211_NODE_MESH_ESTABLISHED);
2368 ni->ni_mllid = meshpeer->peer_llinkid;
2369 args[0] = ni->ni_mlpid;
2370 args[1] = ni->ni_mllid;
2371 ieee80211_send_action(ni,
2372 IEEE80211_ACTION_CAT_SELF_PROT,
2373 IEEE80211_ACTION_MESHPEERING_CONFIRM,
2375 mesh_peer_timeout_stop(ni);
2377 case IEEE80211_NODE_MESH_ESTABLISHED:
2378 if (ni->ni_mllid != meshpeer->peer_llinkid) {
2379 args[0] = ni->ni_mllid;
2380 args[1] = ni->ni_mlpid;
2381 args[2] = IEEE80211_REASON_PEER_LINK_CANCELED;
2382 ieee80211_send_action(ni,
2383 IEEE80211_ACTION_CAT_SELF_PROT,
2384 IEEE80211_ACTION_MESHPEERING_CLOSE,
2386 mesh_linkchange(ni, IEEE80211_NODE_MESH_HOLDING);
2387 mesh_peer_timeout_setup(ni);
2390 args[0] = ni->ni_mlpid;
2391 args[1] = ni->ni_mllid;
2392 ieee80211_send_action(ni,
2393 IEEE80211_ACTION_CAT_SELF_PROT,
2394 IEEE80211_ACTION_MESHPEERING_CONFIRM,
2397 case IEEE80211_NODE_MESH_HOLDING:
2398 args[0] = ni->ni_mlpid;
2399 args[1] = meshpeer->peer_llinkid;
2400 /* Standard not clear about what the reaason code should be */
2401 args[2] = IEEE80211_REASON_PEER_LINK_CANCELED;
2402 ieee80211_send_action(ni,
2403 IEEE80211_ACTION_CAT_SELF_PROT,
2404 IEEE80211_ACTION_MESHPEERING_CLOSE,
2412 mesh_recv_action_meshpeering_confirm(struct ieee80211_node *ni,
2413 const struct ieee80211_frame *wh,
2414 const uint8_t *frm, const uint8_t *efrm)
2416 struct ieee80211vap *vap = ni->ni_vap;
2417 struct ieee80211_meshpeer_ie ie;
2418 const struct ieee80211_meshpeer_ie *meshpeer;
2421 /* +2+2+2+2 for action + code + capabilites + status code + AID */
2422 meshpeer = mesh_parse_meshpeering_action(ni, wh, frm+2+2+2+2, efrm, &ie,
2423 IEEE80211_ACTION_MESHPEERING_CONFIRM);
2424 if (meshpeer == NULL) {
2428 IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH, ni,
2429 "recv PEER CONFIRM, local id 0x%x, peer id 0x%x",
2430 meshpeer->peer_llinkid, meshpeer->peer_linkid);
2432 switch (ni->ni_mlstate) {
2433 case IEEE80211_NODE_MESH_OPENRCV:
2434 mesh_linkchange(ni, IEEE80211_NODE_MESH_ESTABLISHED);
2435 mesh_peer_timeout_stop(ni);
2437 case IEEE80211_NODE_MESH_OPENSNT:
2438 mesh_linkchange(ni, IEEE80211_NODE_MESH_CONFIRMRCV);
2439 mesh_peer_timeout_setup(ni);
2441 case IEEE80211_NODE_MESH_HOLDING:
2442 args[0] = ni->ni_mlpid;
2443 args[1] = meshpeer->peer_llinkid;
2444 /* Standard not clear about what the reaason code should be */
2445 args[2] = IEEE80211_REASON_PEER_LINK_CANCELED;
2446 ieee80211_send_action(ni,
2447 IEEE80211_ACTION_CAT_SELF_PROT,
2448 IEEE80211_ACTION_MESHPEERING_CLOSE,
2451 case IEEE80211_NODE_MESH_CONFIRMRCV:
2452 if (ni->ni_mllid != meshpeer->peer_llinkid) {
2453 args[0] = ni->ni_mlpid;
2454 args[1] = ni->ni_mllid;
2455 args[2] = IEEE80211_REASON_PEER_LINK_CANCELED;
2456 ieee80211_send_action(ni,
2457 IEEE80211_ACTION_CAT_SELF_PROT,
2458 IEEE80211_ACTION_MESHPEERING_CLOSE,
2460 mesh_linkchange(ni, IEEE80211_NODE_MESH_HOLDING);
2461 mesh_peer_timeout_setup(ni);
2465 IEEE80211_DISCARD(vap,
2466 IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH,
2467 wh, NULL, "received confirm in invalid state %d",
2469 vap->iv_stats.is_rx_mgtdiscard++;
2476 mesh_recv_action_meshpeering_close(struct ieee80211_node *ni,
2477 const struct ieee80211_frame *wh,
2478 const uint8_t *frm, const uint8_t *efrm)
2480 struct ieee80211_meshpeer_ie ie;
2481 const struct ieee80211_meshpeer_ie *meshpeer;
2484 /* +2 for action + code */
2485 meshpeer = mesh_parse_meshpeering_action(ni, wh, frm+2, efrm, &ie,
2486 IEEE80211_ACTION_MESHPEERING_CLOSE);
2487 if (meshpeer == NULL) {
2492 * XXX: check reason code, for example we could receive
2493 * IEEE80211_REASON_MESH_MAX_PEERS then we should not attempt
2497 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH,
2498 ni, "%s", "recv PEER CLOSE");
2500 switch (ni->ni_mlstate) {
2501 case IEEE80211_NODE_MESH_IDLE:
2504 case IEEE80211_NODE_MESH_OPENRCV:
2505 case IEEE80211_NODE_MESH_OPENSNT:
2506 case IEEE80211_NODE_MESH_CONFIRMRCV:
2507 case IEEE80211_NODE_MESH_ESTABLISHED:
2508 args[0] = ni->ni_mlpid;
2509 args[1] = ni->ni_mllid;
2510 args[2] = IEEE80211_REASON_MESH_CLOSE_RCVD;
2511 ieee80211_send_action(ni,
2512 IEEE80211_ACTION_CAT_SELF_PROT,
2513 IEEE80211_ACTION_MESHPEERING_CLOSE,
2515 mesh_linkchange(ni, IEEE80211_NODE_MESH_HOLDING);
2516 mesh_peer_timeout_setup(ni);
2518 case IEEE80211_NODE_MESH_HOLDING:
2519 mesh_linkchange(ni, IEEE80211_NODE_MESH_IDLE);
2520 mesh_peer_timeout_stop(ni);
2527 * Link Metric handling.
2530 mesh_recv_action_meshlmetric(struct ieee80211_node *ni,
2531 const struct ieee80211_frame *wh,
2532 const uint8_t *frm, const uint8_t *efrm)
2534 const struct ieee80211_meshlmetric_ie *ie =
2535 (const struct ieee80211_meshlmetric_ie *)
2536 (frm+2); /* action + code */
2537 struct ieee80211_meshlmetric_ie lm_rep;
2539 if (ie->lm_flags & IEEE80211_MESH_LMETRIC_FLAGS_REQ) {
2540 lm_rep.lm_flags = 0;
2541 lm_rep.lm_metric = mesh_airtime_calc(ni);
2542 ieee80211_send_action(ni,
2543 IEEE80211_ACTION_CAT_MESH,
2544 IEEE80211_ACTION_MESH_LMETRIC,
2547 /* XXX: else do nothing for now */
2552 * Parse meshgate action ie's for GANN frames.
2553 * Returns -1 if parsing fails, otherwise 0.
2556 mesh_parse_meshgate_action(struct ieee80211_node *ni,
2557 const struct ieee80211_frame *wh, /* XXX for VERIFY_LENGTH */
2558 struct ieee80211_meshgann_ie *ie, const uint8_t *frm, const uint8_t *efrm)
2560 struct ieee80211vap *vap = ni->ni_vap;
2561 const struct ieee80211_meshgann_ie *gannie;
2563 while (efrm - frm > 1) {
2564 IEEE80211_VERIFY_LENGTH(efrm - frm, frm[1] + 2, return -1);
2566 case IEEE80211_ELEMID_MESHGANN:
2567 gannie = (const struct ieee80211_meshgann_ie *) frm;
2568 memset(ie, 0, sizeof(*ie));
2569 ie->gann_ie = gannie->gann_ie;
2570 ie->gann_len = gannie->gann_len;
2571 ie->gann_flags = gannie->gann_flags;
2572 ie->gann_hopcount = gannie->gann_hopcount;
2573 ie->gann_ttl = gannie->gann_ttl;
2574 IEEE80211_ADDR_COPY(ie->gann_addr, gannie->gann_addr);
2575 ie->gann_seq = LE_READ_4(&gannie->gann_seq);
2576 ie->gann_interval = LE_READ_2(&gannie->gann_interval);
2586 * Mesh Gate Announcement handling.
2589 mesh_recv_action_meshgate(struct ieee80211_node *ni,
2590 const struct ieee80211_frame *wh,
2591 const uint8_t *frm, const uint8_t *efrm)
2593 struct ieee80211vap *vap = ni->ni_vap;
2594 struct ieee80211_mesh_state *ms = vap->iv_mesh;
2595 struct ieee80211_mesh_gate_route *gr, *next;
2596 struct ieee80211_mesh_route *rt_gate;
2597 struct ieee80211_meshgann_ie pgann;
2598 struct ieee80211_meshgann_ie ie;
2601 /* +2 for action + code */
2602 if (mesh_parse_meshgate_action(ni, wh, &ie, frm+2, efrm) != 0) {
2603 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_MESH,
2604 ni->ni_macaddr, NULL, "%s",
2605 "GANN parsing failed");
2606 vap->iv_stats.is_rx_mgtdiscard++;
2610 if (IEEE80211_ADDR_EQ(vap->iv_myaddr, ie.gann_addr))
2613 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH, ni->ni_macaddr,
2614 "received GANN, meshgate: %6D (seq %u)", ie.gann_addr, ":",
2620 TAILQ_FOREACH_SAFE(gr, &ms->ms_known_gates, gr_next, next) {
2621 if (!IEEE80211_ADDR_EQ(gr->gr_addr, ie.gann_addr))
2623 if (ie.gann_seq <= gr->gr_lastseq) {
2624 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_MESH,
2625 ni->ni_macaddr, NULL,
2626 "GANN old seqno %u <= %u",
2627 ie.gann_seq, gr->gr_lastseq);
2631 /* corresponding mesh gate found & GANN accepted */
2637 /* this GANN is from a new mesh Gate add it to known table. */
2638 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH, ie.gann_addr,
2639 "stored new GANN information, seq %u.", ie.gann_seq);
2640 gr = IEEE80211_MALLOC(ALIGN(sizeof(struct ieee80211_mesh_gate_route)),
2642 IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
2643 IEEE80211_ADDR_COPY(gr->gr_addr, ie.gann_addr);
2644 TAILQ_INSERT_TAIL(&ms->ms_known_gates, gr, gr_next);
2646 gr->gr_lastseq = ie.gann_seq;
2648 /* check if we have a path to this gate */
2649 rt_gate = mesh_rt_find_locked(ms, gr->gr_addr);
2650 if (rt_gate != NULL &&
2651 rt_gate->rt_flags & IEEE80211_MESHRT_FLAGS_VALID) {
2652 gr->gr_route = rt_gate;
2653 rt_gate->rt_flags |= IEEE80211_MESHRT_FLAGS_GATE;
2658 /* popagate only if decremented ttl >= 1 && forwarding is enabled */
2659 if ((ie.gann_ttl - 1) < 1 && !(ms->ms_flags & IEEE80211_MESHFLAGS_FWD))
2661 pgann.gann_flags = ie.gann_flags; /* Reserved */
2662 pgann.gann_hopcount = ie.gann_hopcount + 1;
2663 pgann.gann_ttl = ie.gann_ttl - 1;
2664 IEEE80211_ADDR_COPY(pgann.gann_addr, ie.gann_addr);
2665 pgann.gann_seq = ie.gann_seq;
2666 pgann.gann_interval = ie.gann_interval;
2668 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH, ie.gann_addr,
2669 "%s", "propagate GANN");
2671 ieee80211_send_action(vap->iv_bss, IEEE80211_ACTION_CAT_MESH,
2672 IEEE80211_ACTION_MESH_GANN, &pgann);
2678 mesh_send_action(struct ieee80211_node *ni,
2679 const uint8_t sa[IEEE80211_ADDR_LEN],
2680 const uint8_t da[IEEE80211_ADDR_LEN],
2683 struct ieee80211vap *vap = ni->ni_vap;
2684 struct ieee80211com *ic = ni->ni_ic;
2685 struct ieee80211_bpf_params params;
2686 struct ieee80211_frame *wh;
2689 KASSERT(ni != NULL, ("null node"));
2691 if (vap->iv_state == IEEE80211_S_CAC) {
2692 IEEE80211_NOTE(vap, IEEE80211_MSG_OUTPUT, ni,
2693 "block %s frame in CAC state", "Mesh action");
2694 vap->iv_stats.is_tx_badstate++;
2695 ieee80211_free_node(ni);
2697 return EIO; /* XXX */
2700 M_PREPEND(m, sizeof(struct ieee80211_frame), M_NOWAIT);
2702 ieee80211_free_node(ni);
2706 IEEE80211_TX_LOCK(ic);
2707 wh = mtod(m, struct ieee80211_frame *);
2708 ieee80211_send_setup(ni, m,
2709 IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_ACTION,
2710 IEEE80211_NONQOS_TID, sa, da, sa);
2711 m->m_flags |= M_ENCAP; /* mark encapsulated */
2713 memset(¶ms, 0, sizeof(params));
2714 params.ibp_pri = WME_AC_VO;
2715 params.ibp_rate0 = ni->ni_txparms->mgmtrate;
2716 if (IEEE80211_IS_MULTICAST(da))
2717 params.ibp_try0 = 1;
2719 params.ibp_try0 = ni->ni_txparms->maxretry;
2720 params.ibp_power = ni->ni_txpower;
2722 IEEE80211_NODE_STAT(ni, tx_mgmt);
2724 ret = ieee80211_raw_output(vap, ni, m, ¶ms);
2725 IEEE80211_TX_UNLOCK(ic);
2729 #define ADDSHORT(frm, v) do { \
2730 frm[0] = (v) & 0xff; \
2731 frm[1] = (v) >> 8; \
2734 #define ADDWORD(frm, v) do { \
2735 frm[0] = (v) & 0xff; \
2736 frm[1] = ((v) >> 8) & 0xff; \
2737 frm[2] = ((v) >> 16) & 0xff; \
2738 frm[3] = ((v) >> 24) & 0xff; \
2743 mesh_send_action_meshpeering_open(struct ieee80211_node *ni,
2744 int category, int action, void *args0)
2746 struct ieee80211vap *vap = ni->ni_vap;
2747 struct ieee80211com *ic = ni->ni_ic;
2748 uint16_t *args = args0;
2749 const struct ieee80211_rateset *rs;
2753 IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH, ni,
2754 "send PEER OPEN action: localid 0x%x", args[0]);
2756 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
2757 "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", __func__, __LINE__,
2758 ni, ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)+1);
2759 ieee80211_ref_node(ni);
2761 m = ieee80211_getmgtframe(&frm,
2762 ic->ic_headroom + sizeof(struct ieee80211_frame),
2763 sizeof(uint16_t) /* action+category */
2764 + sizeof(uint16_t) /* capabilites */
2765 + 2 + IEEE80211_RATE_SIZE
2766 + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
2767 + 2 + IEEE80211_MESHID_LEN
2768 + sizeof(struct ieee80211_meshconf_ie)
2769 + sizeof(struct ieee80211_meshpeer_ie)
2773 * mesh peer open action frame format:
2781 * [tlv] mesh peer link mgmt
2785 ADDSHORT(frm, ieee80211_getcapinfo(vap, ni->ni_chan));
2786 rs = ieee80211_get_suprates(ic, ic->ic_curchan);
2787 frm = ieee80211_add_rates(frm, rs);
2788 frm = ieee80211_add_xrates(frm, rs);
2789 frm = ieee80211_add_meshid(frm, vap);
2790 frm = ieee80211_add_meshconf(frm, vap);
2791 frm = ieee80211_add_meshpeer(frm, IEEE80211_ACTION_MESHPEERING_OPEN,
2793 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2794 return mesh_send_action(ni, vap->iv_myaddr, ni->ni_macaddr, m);
2796 vap->iv_stats.is_tx_nobuf++;
2797 ieee80211_free_node(ni);
2803 mesh_send_action_meshpeering_confirm(struct ieee80211_node *ni,
2804 int category, int action, void *args0)
2806 struct ieee80211vap *vap = ni->ni_vap;
2807 struct ieee80211com *ic = ni->ni_ic;
2808 uint16_t *args = args0;
2809 const struct ieee80211_rateset *rs;
2813 IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH, ni,
2814 "send PEER CONFIRM action: localid 0x%x, peerid 0x%x",
2817 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
2818 "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", __func__, __LINE__,
2819 ni, ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)+1);
2820 ieee80211_ref_node(ni);
2822 m = ieee80211_getmgtframe(&frm,
2823 ic->ic_headroom + sizeof(struct ieee80211_frame),
2824 sizeof(uint16_t) /* action+category */
2825 + sizeof(uint16_t) /* capabilites */
2826 + sizeof(uint16_t) /* status code */
2827 + sizeof(uint16_t) /* AID */
2828 + 2 + IEEE80211_RATE_SIZE
2829 + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
2830 + 2 + IEEE80211_MESHID_LEN
2831 + sizeof(struct ieee80211_meshconf_ie)
2832 + sizeof(struct ieee80211_meshpeer_ie)
2836 * mesh peer confirm action frame format:
2841 * [2] association id (peer ID)
2846 * [tlv] mesh peer link mgmt
2850 ADDSHORT(frm, ieee80211_getcapinfo(vap, ni->ni_chan));
2851 ADDSHORT(frm, 0); /* status code */
2852 ADDSHORT(frm, args[1]); /* AID */
2853 rs = ieee80211_get_suprates(ic, ic->ic_curchan);
2854 frm = ieee80211_add_rates(frm, rs);
2855 frm = ieee80211_add_xrates(frm, rs);
2856 frm = ieee80211_add_meshid(frm, vap);
2857 frm = ieee80211_add_meshconf(frm, vap);
2858 frm = ieee80211_add_meshpeer(frm,
2859 IEEE80211_ACTION_MESHPEERING_CONFIRM,
2860 args[0], args[1], 0);
2861 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2862 return mesh_send_action(ni, vap->iv_myaddr, ni->ni_macaddr, m);
2864 vap->iv_stats.is_tx_nobuf++;
2865 ieee80211_free_node(ni);
2871 mesh_send_action_meshpeering_close(struct ieee80211_node *ni,
2872 int category, int action, void *args0)
2874 struct ieee80211vap *vap = ni->ni_vap;
2875 struct ieee80211com *ic = ni->ni_ic;
2876 uint16_t *args = args0;
2880 IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH, ni,
2881 "send PEER CLOSE action: localid 0x%x, peerid 0x%x reason %d",
2882 args[0], args[1], args[2]);
2884 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
2885 "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", __func__, __LINE__,
2886 ni, ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)+1);
2887 ieee80211_ref_node(ni);
2889 m = ieee80211_getmgtframe(&frm,
2890 ic->ic_headroom + sizeof(struct ieee80211_frame),
2891 sizeof(uint16_t) /* action+category */
2892 + sizeof(uint16_t) /* reason code */
2893 + 2 + IEEE80211_MESHID_LEN
2894 + sizeof(struct ieee80211_meshpeer_ie)
2898 * mesh peer close action frame format:
2902 * [tlv] mesh peer link mgmt
2906 frm = ieee80211_add_meshid(frm, vap);
2907 frm = ieee80211_add_meshpeer(frm,
2908 IEEE80211_ACTION_MESHPEERING_CLOSE,
2909 args[0], args[1], args[2]);
2910 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2911 return mesh_send_action(ni, vap->iv_myaddr, ni->ni_macaddr, m);
2913 vap->iv_stats.is_tx_nobuf++;
2914 ieee80211_free_node(ni);
2920 mesh_send_action_meshlmetric(struct ieee80211_node *ni,
2921 int category, int action, void *arg0)
2923 struct ieee80211vap *vap = ni->ni_vap;
2924 struct ieee80211com *ic = ni->ni_ic;
2925 struct ieee80211_meshlmetric_ie *ie = arg0;
2929 if (ie->lm_flags & IEEE80211_MESH_LMETRIC_FLAGS_REQ) {
2930 IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH,
2931 ni, "%s", "send LINK METRIC REQUEST action");
2933 IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH,
2934 ni, "send LINK METRIC REPLY action: metric 0x%x",
2937 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
2938 "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", __func__, __LINE__,
2939 ni, ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)+1);
2940 ieee80211_ref_node(ni);
2942 m = ieee80211_getmgtframe(&frm,
2943 ic->ic_headroom + sizeof(struct ieee80211_frame),
2944 sizeof(uint16_t) + /* action+category */
2945 sizeof(struct ieee80211_meshlmetric_ie)
2952 * [tlv] mesh link metric
2956 frm = ieee80211_add_meshlmetric(frm,
2957 ie->lm_flags, ie->lm_metric);
2958 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2959 return mesh_send_action(ni, vap->iv_myaddr, ni->ni_macaddr, m);
2961 vap->iv_stats.is_tx_nobuf++;
2962 ieee80211_free_node(ni);
2968 mesh_send_action_meshgate(struct ieee80211_node *ni,
2969 int category, int action, void *arg0)
2971 struct ieee80211vap *vap = ni->ni_vap;
2972 struct ieee80211com *ic = ni->ni_ic;
2973 struct ieee80211_meshgann_ie *ie = arg0;
2977 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
2978 "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", __func__, __LINE__,
2979 ni, ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)+1);
2980 ieee80211_ref_node(ni);
2982 m = ieee80211_getmgtframe(&frm,
2983 ic->ic_headroom + sizeof(struct ieee80211_frame),
2984 sizeof(uint16_t) + /* action+category */
2985 IEEE80211_MESHGANN_BASE_SZ
2992 * [tlv] mesh gate annoucement
2996 frm = ieee80211_add_meshgate(frm, ie);
2997 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2998 return mesh_send_action(ni, vap->iv_myaddr, broadcastaddr, m);
3000 vap->iv_stats.is_tx_nobuf++;
3001 ieee80211_free_node(ni);
3007 mesh_peer_timeout_setup(struct ieee80211_node *ni)
3009 switch (ni->ni_mlstate) {
3010 case IEEE80211_NODE_MESH_HOLDING:
3011 ni->ni_mltval = ieee80211_mesh_holdingtimeout;
3013 case IEEE80211_NODE_MESH_CONFIRMRCV:
3014 ni->ni_mltval = ieee80211_mesh_confirmtimeout;
3016 case IEEE80211_NODE_MESH_IDLE:
3020 ni->ni_mltval = ieee80211_mesh_retrytimeout;
3024 callout_reset(&ni->ni_mltimer, ni->ni_mltval,
3025 mesh_peer_timeout_cb, ni);
3029 * Same as above but backoffs timer statisically 50%.
3032 mesh_peer_timeout_backoff(struct ieee80211_node *ni)
3037 ni->ni_mltval += r % ni->ni_mltval;
3038 callout_reset(&ni->ni_mltimer, ni->ni_mltval, mesh_peer_timeout_cb,
3042 static __inline void
3043 mesh_peer_timeout_stop(struct ieee80211_node *ni)
3045 callout_drain(&ni->ni_mltimer);
3049 mesh_peer_backoff_cb(void *arg)
3051 struct ieee80211_node *ni = (struct ieee80211_node *)arg;
3053 /* After backoff timeout, try to peer automatically again. */
3058 * Mesh Peer Link Management FSM timeout handling.
3061 mesh_peer_timeout_cb(void *arg)
3063 struct ieee80211_node *ni = (struct ieee80211_node *)arg;
3066 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_MESH,
3067 ni, "mesh link timeout, state %d, retry counter %d",
3068 ni->ni_mlstate, ni->ni_mlrcnt);
3070 switch (ni->ni_mlstate) {
3071 case IEEE80211_NODE_MESH_IDLE:
3072 case IEEE80211_NODE_MESH_ESTABLISHED:
3074 case IEEE80211_NODE_MESH_OPENSNT:
3075 case IEEE80211_NODE_MESH_OPENRCV:
3076 if (ni->ni_mlrcnt == ieee80211_mesh_maxretries) {
3077 args[0] = ni->ni_mlpid;
3078 args[2] = IEEE80211_REASON_MESH_MAX_RETRIES;
3079 ieee80211_send_action(ni,
3080 IEEE80211_ACTION_CAT_SELF_PROT,
3081 IEEE80211_ACTION_MESHPEERING_CLOSE, args);
3083 mesh_linkchange(ni, IEEE80211_NODE_MESH_HOLDING);
3084 mesh_peer_timeout_setup(ni);
3086 args[0] = ni->ni_mlpid;
3087 ieee80211_send_action(ni,
3088 IEEE80211_ACTION_CAT_SELF_PROT,
3089 IEEE80211_ACTION_MESHPEERING_OPEN, args);
3091 mesh_peer_timeout_backoff(ni);
3094 case IEEE80211_NODE_MESH_CONFIRMRCV:
3095 args[0] = ni->ni_mlpid;
3096 args[2] = IEEE80211_REASON_MESH_CONFIRM_TIMEOUT;
3097 ieee80211_send_action(ni,
3098 IEEE80211_ACTION_CAT_SELF_PROT,
3099 IEEE80211_ACTION_MESHPEERING_CLOSE, args);
3100 mesh_linkchange(ni, IEEE80211_NODE_MESH_HOLDING);
3101 mesh_peer_timeout_setup(ni);
3103 case IEEE80211_NODE_MESH_HOLDING:
3105 if (ni->ni_mlhcnt >= ieee80211_mesh_maxholding)
3106 callout_reset(&ni->ni_mlhtimer,
3107 ieee80211_mesh_backofftimeout,
3108 mesh_peer_backoff_cb, ni);
3109 mesh_linkchange(ni, IEEE80211_NODE_MESH_IDLE);
3115 mesh_verify_meshid(struct ieee80211vap *vap, const uint8_t *ie)
3117 struct ieee80211_mesh_state *ms = vap->iv_mesh;
3119 if (ie == NULL || ie[1] != ms->ms_idlen)
3121 return memcmp(ms->ms_id, ie + 2, ms->ms_idlen);
3125 * Check if we are using the same algorithms for this mesh.
3128 mesh_verify_meshconf(struct ieee80211vap *vap, const uint8_t *ie)
3130 const struct ieee80211_meshconf_ie *meshconf =
3131 (const struct ieee80211_meshconf_ie *) ie;
3132 const struct ieee80211_mesh_state *ms = vap->iv_mesh;
3134 if (meshconf == NULL)
3136 if (meshconf->conf_pselid != ms->ms_ppath->mpp_ie) {
3137 IEEE80211_DPRINTF(vap, IEEE80211_MSG_MESH,
3138 "unknown path selection algorithm: 0x%x\n",
3139 meshconf->conf_pselid);
3142 if (meshconf->conf_pmetid != ms->ms_pmetric->mpm_ie) {
3143 IEEE80211_DPRINTF(vap, IEEE80211_MSG_MESH,
3144 "unknown path metric algorithm: 0x%x\n",
3145 meshconf->conf_pmetid);
3148 if (meshconf->conf_ccid != 0) {
3149 IEEE80211_DPRINTF(vap, IEEE80211_MSG_MESH,
3150 "unknown congestion control algorithm: 0x%x\n",
3151 meshconf->conf_ccid);
3154 if (meshconf->conf_syncid != IEEE80211_MESHCONF_SYNC_NEIGHOFF) {
3155 IEEE80211_DPRINTF(vap, IEEE80211_MSG_MESH,
3156 "unknown sync algorithm: 0x%x\n",
3157 meshconf->conf_syncid);
3160 if (meshconf->conf_authid != 0) {
3161 IEEE80211_DPRINTF(vap, IEEE80211_MSG_MESH,
3162 "unknown auth auth algorithm: 0x%x\n",
3163 meshconf->conf_pselid);
3166 /* Not accepting peers */
3167 if (!(meshconf->conf_cap & IEEE80211_MESHCONF_CAP_AP)) {
3168 IEEE80211_DPRINTF(vap, IEEE80211_MSG_MESH,
3169 "not accepting peers: 0x%x\n", meshconf->conf_cap);
3176 mesh_verify_meshpeer(struct ieee80211vap *vap, uint8_t subtype,
3179 const struct ieee80211_meshpeer_ie *meshpeer =
3180 (const struct ieee80211_meshpeer_ie *) ie;
3182 if (meshpeer == NULL ||
3183 meshpeer->peer_len < IEEE80211_MPM_BASE_SZ ||
3184 meshpeer->peer_len > IEEE80211_MPM_MAX_SZ)
3186 if (meshpeer->peer_proto != IEEE80211_MPPID_MPM) {
3187 IEEE80211_DPRINTF(vap,
3188 IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH,
3189 "Only MPM protocol is supported (proto: 0x%02X)",
3190 meshpeer->peer_proto);
3194 case IEEE80211_ACTION_MESHPEERING_OPEN:
3195 if (meshpeer->peer_len != IEEE80211_MPM_BASE_SZ)
3198 case IEEE80211_ACTION_MESHPEERING_CONFIRM:
3199 if (meshpeer->peer_len != IEEE80211_MPM_BASE_SZ + 2)
3202 case IEEE80211_ACTION_MESHPEERING_CLOSE:
3203 if (meshpeer->peer_len < IEEE80211_MPM_BASE_SZ + 2)
3205 if (meshpeer->peer_len == (IEEE80211_MPM_BASE_SZ + 2) &&
3206 meshpeer->peer_linkid != 0)
3208 if (meshpeer->peer_rcode == 0)
3216 * Add a Mesh ID IE to a frame.
3219 ieee80211_add_meshid(uint8_t *frm, struct ieee80211vap *vap)
3221 struct ieee80211_mesh_state *ms = vap->iv_mesh;
3223 KASSERT(vap->iv_opmode == IEEE80211_M_MBSS, ("not a mbss vap"));
3225 *frm++ = IEEE80211_ELEMID_MESHID;
3226 *frm++ = ms->ms_idlen;
3227 memcpy(frm, ms->ms_id, ms->ms_idlen);
3228 return frm + ms->ms_idlen;
3232 * Add a Mesh Configuration IE to a frame.
3233 * For now just use HWMP routing, Airtime link metric, Null Congestion
3234 * Signaling, Null Sync Protocol and Null Authentication.
3237 ieee80211_add_meshconf(uint8_t *frm, struct ieee80211vap *vap)
3239 const struct ieee80211_mesh_state *ms = vap->iv_mesh;
3242 KASSERT(vap->iv_opmode == IEEE80211_M_MBSS, ("not a MBSS vap"));
3244 *frm++ = IEEE80211_ELEMID_MESHCONF;
3245 *frm++ = IEEE80211_MESH_CONF_SZ;
3246 *frm++ = ms->ms_ppath->mpp_ie; /* path selection */
3247 *frm++ = ms->ms_pmetric->mpm_ie; /* link metric */
3248 *frm++ = IEEE80211_MESHCONF_CC_DISABLED;
3249 *frm++ = IEEE80211_MESHCONF_SYNC_NEIGHOFF;
3250 *frm++ = IEEE80211_MESHCONF_AUTH_DISABLED;
3251 /* NB: set the number of neighbors before the rest */
3252 *frm = (ms->ms_neighbors > IEEE80211_MESH_MAX_NEIGHBORS ?
3253 IEEE80211_MESH_MAX_NEIGHBORS : ms->ms_neighbors) << 1;
3254 if (ms->ms_flags & IEEE80211_MESHFLAGS_GATE)
3255 *frm |= IEEE80211_MESHCONF_FORM_GATE;
3258 if (ms->ms_flags & IEEE80211_MESHFLAGS_AP)
3259 caps |= IEEE80211_MESHCONF_CAP_AP;
3260 if (ms->ms_flags & IEEE80211_MESHFLAGS_FWD)
3261 caps |= IEEE80211_MESHCONF_CAP_FWRD;
3267 * Add a Mesh Peer Management IE to a frame.
3270 ieee80211_add_meshpeer(uint8_t *frm, uint8_t subtype, uint16_t localid,
3271 uint16_t peerid, uint16_t reason)
3274 KASSERT(localid != 0, ("localid == 0"));
3276 *frm++ = IEEE80211_ELEMID_MESHPEER;
3278 case IEEE80211_ACTION_MESHPEERING_OPEN:
3279 *frm++ = IEEE80211_MPM_BASE_SZ; /* length */
3280 ADDSHORT(frm, IEEE80211_MPPID_MPM); /* proto */
3281 ADDSHORT(frm, localid); /* local ID */
3283 case IEEE80211_ACTION_MESHPEERING_CONFIRM:
3284 KASSERT(peerid != 0, ("sending peer confirm without peer id"));
3285 *frm++ = IEEE80211_MPM_BASE_SZ + 2; /* length */
3286 ADDSHORT(frm, IEEE80211_MPPID_MPM); /* proto */
3287 ADDSHORT(frm, localid); /* local ID */
3288 ADDSHORT(frm, peerid); /* peer ID */
3290 case IEEE80211_ACTION_MESHPEERING_CLOSE:
3292 *frm++ = IEEE80211_MPM_MAX_SZ; /* length */
3294 *frm++ = IEEE80211_MPM_BASE_SZ + 2; /* length */
3295 ADDSHORT(frm, IEEE80211_MPPID_MPM); /* proto */
3296 ADDSHORT(frm, localid); /* local ID */
3298 ADDSHORT(frm, peerid); /* peer ID */
3299 ADDSHORT(frm, reason);
3306 * Compute an Airtime Link Metric for the link with this node.
3308 * Based on Draft 3.0 spec (11B.10, p.149).
3311 * Max 802.11s overhead.
3313 #define IEEE80211_MESH_MAXOVERHEAD \
3314 (sizeof(struct ieee80211_qosframe_addr4) \
3315 + sizeof(struct ieee80211_meshcntl_ae10) \
3316 + sizeof(struct llc) \
3317 + IEEE80211_ADDR_LEN \
3318 + IEEE80211_WEP_IVLEN \
3319 + IEEE80211_WEP_KIDLEN \
3320 + IEEE80211_WEP_CRCLEN \
3321 + IEEE80211_WEP_MICLEN \
3322 + IEEE80211_CRC_LEN)
3324 mesh_airtime_calc(struct ieee80211_node *ni)
3327 #define S_FACTOR (2 * M_BITS)
3328 struct ieee80211com *ic = ni->ni_ic;
3329 struct ifnet *ifp = ni->ni_vap->iv_ifp;
3330 const static int nbits = 8192 << M_BITS;
3331 uint32_t overhead, rate, errrate;
3334 /* Time to transmit a frame */
3335 rate = ni->ni_txrate;
3336 overhead = ieee80211_compute_duration(ic->ic_rt,
3337 ifp->if_mtu + IEEE80211_MESH_MAXOVERHEAD, rate, 0) << M_BITS;
3338 /* Error rate in percentage */
3339 /* XXX assuming small failures are ok */
3340 errrate = (((ifp->if_get_counter(ifp, IFCOUNTER_OERRORS) +
3341 ifp->if_get_counter(ifp, IFCOUNTER_IERRORS)) / 100) << M_BITS)
3343 res = (overhead + (nbits / rate)) *
3344 ((1 << S_FACTOR) / ((1 << M_BITS) - errrate));
3346 return (uint32_t)(res >> S_FACTOR);
3352 * Add a Mesh Link Metric report IE to a frame.
3355 ieee80211_add_meshlmetric(uint8_t *frm, uint8_t flags, uint32_t metric)
3357 *frm++ = IEEE80211_ELEMID_MESHLINK;
3360 ADDWORD(frm, metric);
3365 * Add a Mesh Gate Announcement IE to a frame.
3368 ieee80211_add_meshgate(uint8_t *frm, struct ieee80211_meshgann_ie *ie)
3370 *frm++ = IEEE80211_ELEMID_MESHGANN; /* ie */
3371 *frm++ = IEEE80211_MESHGANN_BASE_SZ; /* len */
3372 *frm++ = ie->gann_flags;
3373 *frm++ = ie->gann_hopcount;
3374 *frm++ = ie->gann_ttl;
3375 IEEE80211_ADDR_COPY(frm, ie->gann_addr);
3377 ADDWORD(frm, ie->gann_seq);
3378 ADDSHORT(frm, ie->gann_interval);
3385 * Initialize any mesh-specific node state.
3388 ieee80211_mesh_node_init(struct ieee80211vap *vap, struct ieee80211_node *ni)
3390 ni->ni_flags |= IEEE80211_NODE_QOS;
3391 callout_init(&ni->ni_mltimer, 1);
3392 callout_init(&ni->ni_mlhtimer, 1);
3396 * Cleanup any mesh-specific node state.
3399 ieee80211_mesh_node_cleanup(struct ieee80211_node *ni)
3401 struct ieee80211vap *vap = ni->ni_vap;
3402 struct ieee80211_mesh_state *ms = vap->iv_mesh;
3404 callout_drain(&ni->ni_mltimer);
3405 callout_drain(&ni->ni_mlhtimer);
3406 /* NB: short-circuit callbacks after mesh_vdetach */
3407 if (vap->iv_mesh != NULL)
3408 ms->ms_ppath->mpp_peerdown(ni);
3412 ieee80211_parse_meshid(struct ieee80211_node *ni, const uint8_t *ie)
3414 ni->ni_meshidlen = ie[1];
3415 memcpy(ni->ni_meshid, ie + 2, ie[1]);
3419 * Setup mesh-specific node state on neighbor discovery.
3422 ieee80211_mesh_init_neighbor(struct ieee80211_node *ni,
3423 const struct ieee80211_frame *wh,
3424 const struct ieee80211_scanparams *sp)
3426 ieee80211_parse_meshid(ni, sp->meshid);
3430 ieee80211_mesh_update_beacon(struct ieee80211vap *vap,
3431 struct ieee80211_beacon_offsets *bo)
3433 KASSERT(vap->iv_opmode == IEEE80211_M_MBSS, ("not a MBSS vap"));
3435 if (isset(bo->bo_flags, IEEE80211_BEACON_MESHCONF)) {
3436 (void)ieee80211_add_meshconf(bo->bo_meshconf, vap);
3437 clrbit(bo->bo_flags, IEEE80211_BEACON_MESHCONF);
3442 mesh_ioctl_get80211(struct ieee80211vap *vap, struct ieee80211req *ireq)
3444 struct ieee80211_mesh_state *ms = vap->iv_mesh;
3445 uint8_t tmpmeshid[IEEE80211_NWID_LEN];
3446 struct ieee80211_mesh_route *rt;
3447 struct ieee80211req_mesh_route *imr;
3452 if (vap->iv_opmode != IEEE80211_M_MBSS)
3456 switch (ireq->i_type) {
3457 case IEEE80211_IOC_MESH_ID:
3458 ireq->i_len = ms->ms_idlen;
3459 memcpy(tmpmeshid, ms->ms_id, ireq->i_len);
3460 error = copyout(tmpmeshid, ireq->i_data, ireq->i_len);
3462 case IEEE80211_IOC_MESH_AP:
3463 ireq->i_val = (ms->ms_flags & IEEE80211_MESHFLAGS_AP) != 0;
3465 case IEEE80211_IOC_MESH_FWRD:
3466 ireq->i_val = (ms->ms_flags & IEEE80211_MESHFLAGS_FWD) != 0;
3468 case IEEE80211_IOC_MESH_GATE:
3469 ireq->i_val = (ms->ms_flags & IEEE80211_MESHFLAGS_GATE) != 0;
3471 case IEEE80211_IOC_MESH_TTL:
3472 ireq->i_val = ms->ms_ttl;
3474 case IEEE80211_IOC_MESH_RTCMD:
3475 switch (ireq->i_val) {
3476 case IEEE80211_MESH_RTCMD_LIST:
3479 TAILQ_FOREACH(rt, &ms->ms_routes, rt_next) {
3480 len += sizeof(*imr);
3483 if (len > ireq->i_len || ireq->i_len < sizeof(*imr)) {
3489 p = IEEE80211_MALLOC(len, M_TEMP,
3490 IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
3495 TAILQ_FOREACH(rt, &ms->ms_routes, rt_next) {
3498 imr = (struct ieee80211req_mesh_route *)
3500 IEEE80211_ADDR_COPY(imr->imr_dest,
3502 IEEE80211_ADDR_COPY(imr->imr_nexthop,
3504 imr->imr_metric = rt->rt_metric;
3505 imr->imr_nhops = rt->rt_nhops;
3507 ieee80211_mesh_rt_update(rt, 0);
3508 imr->imr_lastmseq = rt->rt_lastmseq;
3509 imr->imr_flags = rt->rt_flags; /* last */
3510 off += sizeof(*imr);
3513 error = copyout(p, (uint8_t *)ireq->i_data,
3515 IEEE80211_FREE(p, M_TEMP);
3517 case IEEE80211_MESH_RTCMD_FLUSH:
3518 case IEEE80211_MESH_RTCMD_ADD:
3519 case IEEE80211_MESH_RTCMD_DELETE:
3525 case IEEE80211_IOC_MESH_PR_METRIC:
3526 len = strlen(ms->ms_pmetric->mpm_descr);
3527 if (ireq->i_len < len)
3530 error = copyout(ms->ms_pmetric->mpm_descr,
3531 (uint8_t *)ireq->i_data, len);
3533 case IEEE80211_IOC_MESH_PR_PATH:
3534 len = strlen(ms->ms_ppath->mpp_descr);
3535 if (ireq->i_len < len)
3538 error = copyout(ms->ms_ppath->mpp_descr,
3539 (uint8_t *)ireq->i_data, len);
3547 IEEE80211_IOCTL_GET(mesh, mesh_ioctl_get80211);
3550 mesh_ioctl_set80211(struct ieee80211vap *vap, struct ieee80211req *ireq)
3552 struct ieee80211_mesh_state *ms = vap->iv_mesh;
3553 uint8_t tmpmeshid[IEEE80211_NWID_LEN];
3554 uint8_t tmpaddr[IEEE80211_ADDR_LEN];
3555 char tmpproto[IEEE80211_MESH_PROTO_DSZ];
3558 if (vap->iv_opmode != IEEE80211_M_MBSS)
3562 switch (ireq->i_type) {
3563 case IEEE80211_IOC_MESH_ID:
3564 if (ireq->i_val != 0 || ireq->i_len > IEEE80211_MESHID_LEN)
3566 error = copyin(ireq->i_data, tmpmeshid, ireq->i_len);
3569 memset(ms->ms_id, 0, IEEE80211_NWID_LEN);
3570 ms->ms_idlen = ireq->i_len;
3571 memcpy(ms->ms_id, tmpmeshid, ireq->i_len);
3574 case IEEE80211_IOC_MESH_AP:
3576 ms->ms_flags |= IEEE80211_MESHFLAGS_AP;
3578 ms->ms_flags &= ~IEEE80211_MESHFLAGS_AP;
3581 case IEEE80211_IOC_MESH_FWRD:
3583 ms->ms_flags |= IEEE80211_MESHFLAGS_FWD;
3585 ms->ms_flags &= ~IEEE80211_MESHFLAGS_FWD;
3586 mesh_gatemode_setup(vap);
3588 case IEEE80211_IOC_MESH_GATE:
3590 ms->ms_flags |= IEEE80211_MESHFLAGS_GATE;
3592 ms->ms_flags &= ~IEEE80211_MESHFLAGS_GATE;
3594 case IEEE80211_IOC_MESH_TTL:
3595 ms->ms_ttl = (uint8_t) ireq->i_val;
3597 case IEEE80211_IOC_MESH_RTCMD:
3598 switch (ireq->i_val) {
3599 case IEEE80211_MESH_RTCMD_LIST:
3601 case IEEE80211_MESH_RTCMD_FLUSH:
3602 ieee80211_mesh_rt_flush(vap);
3604 case IEEE80211_MESH_RTCMD_ADD:
3605 if (IEEE80211_ADDR_EQ(vap->iv_myaddr, ireq->i_data) ||
3606 IEEE80211_ADDR_EQ(broadcastaddr, ireq->i_data))
3608 error = copyin(ireq->i_data, &tmpaddr,
3609 IEEE80211_ADDR_LEN);
3611 ieee80211_mesh_discover(vap, tmpaddr, NULL);
3613 case IEEE80211_MESH_RTCMD_DELETE:
3614 ieee80211_mesh_rt_del(vap, ireq->i_data);
3620 case IEEE80211_IOC_MESH_PR_METRIC:
3621 error = copyin(ireq->i_data, tmpproto, sizeof(tmpproto));
3623 error = mesh_select_proto_metric(vap, tmpproto);
3628 case IEEE80211_IOC_MESH_PR_PATH:
3629 error = copyin(ireq->i_data, tmpproto, sizeof(tmpproto));
3631 error = mesh_select_proto_path(vap, tmpproto);
3641 IEEE80211_IOCTL_SET(mesh, mesh_ioctl_set80211);