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 <netproto/802_11/ieee80211_var.h>
63 #include <netproto/802_11/ieee80211_action.h>
64 #ifdef IEEE80211_SUPPORT_SUPERG
65 #include <netproto/802_11/ieee80211_superg.h>
67 #include <netproto/802_11/ieee80211_input.h>
68 #include <netproto/802_11/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 *, int, int);
89 static void mesh_recv_mgmt(struct ieee80211_node *, struct mbuf *, int,
91 static void mesh_recv_ctl(struct ieee80211_node *, struct mbuf *, int);
92 static void mesh_peer_timeout_setup(struct ieee80211_node *);
93 static void mesh_peer_timeout_backoff(struct ieee80211_node *);
94 static void mesh_peer_timeout_cb(void *);
96 mesh_peer_timeout_stop(struct ieee80211_node *);
97 static int mesh_verify_meshid(struct ieee80211vap *, const uint8_t *);
98 static int mesh_verify_meshconf(struct ieee80211vap *, const uint8_t *);
99 static int mesh_verify_meshpeer(struct ieee80211vap *, uint8_t,
101 uint32_t mesh_airtime_calc(struct ieee80211_node *);
104 * Timeout values come from the specification and are in milliseconds.
106 static SYSCTL_NODE(_net_wlan, OID_AUTO, mesh, CTLFLAG_RD, 0,
107 "IEEE 802.11s parameters");
108 static int ieee80211_mesh_gateint = -1;
109 SYSCTL_PROC(_net_wlan_mesh, OID_AUTO, gateint, CTLTYPE_INT | CTLFLAG_RW,
110 &ieee80211_mesh_gateint, 0, ieee80211_sysctl_msecs_ticks, "I",
111 "mesh gate interval (ms)");
112 static int ieee80211_mesh_retrytimeout = -1;
113 SYSCTL_PROC(_net_wlan_mesh, OID_AUTO, retrytimeout, CTLTYPE_INT | CTLFLAG_RW,
114 &ieee80211_mesh_retrytimeout, 0, ieee80211_sysctl_msecs_ticks, "I",
115 "Retry timeout (msec)");
116 static int ieee80211_mesh_holdingtimeout = -1;
118 SYSCTL_PROC(_net_wlan_mesh, OID_AUTO, holdingtimeout, CTLTYPE_INT | CTLFLAG_RW,
119 &ieee80211_mesh_holdingtimeout, 0, ieee80211_sysctl_msecs_ticks, "I",
120 "Holding state timeout (msec)");
121 static int ieee80211_mesh_confirmtimeout = -1;
122 SYSCTL_PROC(_net_wlan_mesh, OID_AUTO, confirmtimeout, CTLTYPE_INT | CTLFLAG_RW,
123 &ieee80211_mesh_confirmtimeout, 0, ieee80211_sysctl_msecs_ticks, "I",
124 "Confirm state timeout (msec)");
125 static int ieee80211_mesh_backofftimeout = -1;
126 SYSCTL_PROC(_net_wlan_mesh, OID_AUTO, backofftimeout, CTLTYPE_INT | CTLFLAG_RW,
127 &ieee80211_mesh_backofftimeout, 0, ieee80211_sysctl_msecs_ticks, "I",
128 "Backoff timeout (msec). This is to throutles peering forever when "
129 "not receving answer or is rejected by a neighbor");
130 static int ieee80211_mesh_maxretries = 2;
131 SYSCTL_INT(_net_wlan_mesh, OID_AUTO, maxretries, CTLFLAG_RW,
132 &ieee80211_mesh_maxretries, 0,
133 "Maximum retries during peer link establishment");
134 static int ieee80211_mesh_maxholding = 2;
135 SYSCTL_INT(_net_wlan_mesh, OID_AUTO, maxholding, CTLFLAG_RW,
136 &ieee80211_mesh_maxholding, 0,
137 "Maximum times we are allowed to transition to HOLDING state before "
138 "backinoff during peer link establishment");
140 static const uint8_t broadcastaddr[IEEE80211_ADDR_LEN] =
141 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
143 static ieee80211_recv_action_func mesh_recv_action_meshpeering_open;
144 static ieee80211_recv_action_func mesh_recv_action_meshpeering_confirm;
145 static ieee80211_recv_action_func mesh_recv_action_meshpeering_close;
146 static ieee80211_recv_action_func mesh_recv_action_meshlmetric;
147 static ieee80211_recv_action_func mesh_recv_action_meshgate;
149 static ieee80211_send_action_func mesh_send_action_meshpeering_open;
150 static ieee80211_send_action_func mesh_send_action_meshpeering_confirm;
151 static ieee80211_send_action_func mesh_send_action_meshpeering_close;
152 static ieee80211_send_action_func mesh_send_action_meshlmetric;
153 static ieee80211_send_action_func mesh_send_action_meshgate;
155 static const struct ieee80211_mesh_proto_metric mesh_metric_airtime = {
156 .mpm_descr = "AIRTIME",
157 .mpm_ie = IEEE80211_MESHCONF_METRIC_AIRTIME,
158 .mpm_metric = mesh_airtime_calc,
161 static struct ieee80211_mesh_proto_path mesh_proto_paths[4];
162 static struct ieee80211_mesh_proto_metric mesh_proto_metrics[4];
164 #if defined(__DragonFly__)
166 #define RT_ENTRY_LOCK(rt) lockmgr(&(rt)->rt_lock, LK_EXCLUSIVE)
167 #define RT_ENTRY_LOCK_ASSERT(rt) KKASSERT(lockstatus(&(rt)->rt_lock, curthread) == LK_EXCLUSIVE)
168 #define RT_ENTRY_UNLOCK(rt) lockmgr(&(rt)->rt_lock, LK_RELEASE)
170 #define MESH_RT_LOCK(ms) lockmgr(&(ms)->ms_rt_lock, LK_EXCLUSIVE)
171 #define MESH_RT_LOCK_ASSERT(ms) KKASSERT(lockstatus(&(ms)->ms_rt_lock, curthread) == LK_EXCLUSIVE)
172 #define MESH_RT_UNLOCK(ms) lockmgr(&(ms)->ms_rt_lock, LK_RELEASE)
176 #define RT_ENTRY_LOCK(rt) mtx_lock(&(rt)->rt_lock)
177 #define RT_ENTRY_LOCK_ASSERT(rt) mtx_assert(&(rt)->rt_lock, MA_OWNED)
178 #define RT_ENTRY_UNLOCK(rt) mtx_unlock(&(rt)->rt_lock)
180 #define MESH_RT_LOCK(ms) mtx_lock(&(ms)->ms_rt_lock)
181 #define MESH_RT_LOCK_ASSERT(ms) mtx_assert(&(ms)->ms_rt_lock, MA_OWNED)
182 #define MESH_RT_UNLOCK(ms) mtx_unlock(&(ms)->ms_rt_lock)
186 MALLOC_DEFINE(M_80211_MESH_PREQ, "80211preq", "802.11 MESH Path Request frame");
187 MALLOC_DEFINE(M_80211_MESH_PREP, "80211prep", "802.11 MESH Path Reply frame");
188 MALLOC_DEFINE(M_80211_MESH_PERR, "80211perr", "802.11 MESH Path Error frame");
190 /* The longer one of the lifetime should be stored as new lifetime */
191 #define MESH_ROUTE_LIFETIME_MAX(a, b) (a > b ? a : b)
193 MALLOC_DEFINE(M_80211_MESH_RT, "80211mesh_rt", "802.11s routing table");
194 MALLOC_DEFINE(M_80211_MESH_GT_RT, "80211mesh_gt", "802.11s known gates table");
197 * Helper functions to manipulate the Mesh routing table.
200 static struct ieee80211_mesh_route *
201 mesh_rt_find_locked(struct ieee80211_mesh_state *ms,
202 const uint8_t dest[IEEE80211_ADDR_LEN])
204 struct ieee80211_mesh_route *rt;
206 MESH_RT_LOCK_ASSERT(ms);
208 TAILQ_FOREACH(rt, &ms->ms_routes, rt_next) {
209 if (IEEE80211_ADDR_EQ(dest, rt->rt_dest))
215 static struct ieee80211_mesh_route *
216 mesh_rt_add_locked(struct ieee80211vap *vap,
217 const uint8_t dest[IEEE80211_ADDR_LEN])
219 struct ieee80211_mesh_state *ms = vap->iv_mesh;
220 struct ieee80211_mesh_route *rt;
222 KASSERT(!IEEE80211_ADDR_EQ(broadcastaddr, dest),
223 ("%s: adding broadcast to the routing table", __func__));
225 MESH_RT_LOCK_ASSERT(ms);
227 rt = kmalloc(ALIGN(sizeof(struct ieee80211_mesh_route)) +
228 ms->ms_ppath->mpp_privlen, M_80211_MESH_RT, M_INTWAIT | M_ZERO);
231 IEEE80211_ADDR_COPY(rt->rt_dest, dest);
232 rt->rt_priv = (void *)ALIGN(&rt[1]);
233 #if defined(__DragonFly__)
234 lockinit(&rt->rt_lock, "MBSS_RT", 0, 0);
236 mtx_init(&rt->rt_lock, "MBSS_RT", "802.11s route entry", MTX_DEF);
238 callout_init_mp(&rt->rt_discovery);
239 rt->rt_updtime = ticks; /* create time */
240 TAILQ_INSERT_TAIL(&ms->ms_routes, rt, rt_next);
245 struct ieee80211_mesh_route *
246 ieee80211_mesh_rt_find(struct ieee80211vap *vap,
247 const uint8_t dest[IEEE80211_ADDR_LEN])
249 struct ieee80211_mesh_state *ms = vap->iv_mesh;
250 struct ieee80211_mesh_route *rt;
253 rt = mesh_rt_find_locked(ms, dest);
258 struct ieee80211_mesh_route *
259 ieee80211_mesh_rt_add(struct ieee80211vap *vap,
260 const uint8_t dest[IEEE80211_ADDR_LEN])
262 struct ieee80211_mesh_state *ms = vap->iv_mesh;
263 struct ieee80211_mesh_route *rt;
265 KASSERT(ieee80211_mesh_rt_find(vap, dest) == NULL,
266 ("%s: duplicate entry in the routing table", __func__));
267 KASSERT(!IEEE80211_ADDR_EQ(vap->iv_myaddr, dest),
268 ("%s: adding self to the routing table", __func__));
271 rt = mesh_rt_add_locked(vap, dest);
277 * Update the route lifetime and returns the updated lifetime.
278 * If new_lifetime is zero and route is timedout it will be invalidated.
279 * new_lifetime is in msec
282 ieee80211_mesh_rt_update(struct ieee80211_mesh_route *rt, int new_lifetime)
285 uint32_t lifetime = 0;
287 KASSERT(rt != NULL, ("route is NULL"));
292 /* dont clobber a proxy entry gated by us */
293 if (rt->rt_flags & IEEE80211_MESHRT_FLAGS_PROXY && rt->rt_nhops == 0) {
295 return rt->rt_lifetime;
298 timesince = ticks_to_msecs(now - rt->rt_updtime);
299 rt->rt_updtime = now;
300 if (timesince >= rt->rt_lifetime) {
301 if (new_lifetime != 0) {
302 rt->rt_lifetime = new_lifetime;
305 rt->rt_flags &= ~IEEE80211_MESHRT_FLAGS_VALID;
309 /* update what is left of lifetime */
310 rt->rt_lifetime = rt->rt_lifetime - timesince;
311 rt->rt_lifetime = MESH_ROUTE_LIFETIME_MAX(
312 new_lifetime, rt->rt_lifetime);
314 lifetime = rt->rt_lifetime;
321 * Add a proxy route (as needed) for the specified destination.
324 ieee80211_mesh_proxy_check(struct ieee80211vap *vap,
325 const uint8_t dest[IEEE80211_ADDR_LEN])
327 struct ieee80211_mesh_state *ms = vap->iv_mesh;
328 struct ieee80211_mesh_route *rt;
331 rt = mesh_rt_find_locked(ms, dest);
333 rt = mesh_rt_add_locked(vap, dest);
335 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH, dest,
336 "%s", "unable to add proxy entry");
337 vap->iv_stats.is_mesh_rtaddfailed++;
339 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH, dest,
340 "%s", "add proxy entry");
341 IEEE80211_ADDR_COPY(rt->rt_mesh_gate, vap->iv_myaddr);
342 IEEE80211_ADDR_COPY(rt->rt_nexthop, vap->iv_myaddr);
343 rt->rt_flags |= IEEE80211_MESHRT_FLAGS_VALID
344 | IEEE80211_MESHRT_FLAGS_PROXY;
346 } else if ((rt->rt_flags & IEEE80211_MESHRT_FLAGS_VALID) == 0) {
347 KASSERT(rt->rt_flags & IEEE80211_MESHRT_FLAGS_PROXY,
348 ("no proxy flag for poxy entry"));
349 struct ieee80211com *ic = vap->iv_ic;
351 * Fix existing entry created by received frames from
352 * stations that have some memory of dest. We also
353 * flush any frames held on the staging queue; delivering
354 * them is too much trouble right now.
356 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH, dest,
357 "%s", "fix proxy entry");
358 IEEE80211_ADDR_COPY(rt->rt_nexthop, vap->iv_myaddr);
359 rt->rt_flags |= IEEE80211_MESHRT_FLAGS_VALID
360 | IEEE80211_MESHRT_FLAGS_PROXY;
361 /* XXX belongs in hwmp */
362 ieee80211_ageq_drain_node(&ic->ic_stageq,
363 (void *)(uintptr_t) ieee80211_mac_hash(ic, dest));
370 mesh_rt_del(struct ieee80211_mesh_state *ms, struct ieee80211_mesh_route *rt)
372 TAILQ_REMOVE(&ms->ms_routes, rt, rt_next);
374 * Grab the lock before destroying it, to be sure no one else
375 * is holding the route.
378 callout_drain(&rt->rt_discovery);
379 #if defined(__DragonFly__)
380 lockuninit(&rt->rt_lock);
382 mtx_destroy(&rt->rt_lock);
384 kfree(rt, M_80211_MESH_RT);
388 ieee80211_mesh_rt_del(struct ieee80211vap *vap,
389 const uint8_t dest[IEEE80211_ADDR_LEN])
391 struct ieee80211_mesh_state *ms = vap->iv_mesh;
392 struct ieee80211_mesh_route *rt, *next;
395 TAILQ_FOREACH_SAFE(rt, &ms->ms_routes, rt_next, next) {
396 if (IEEE80211_ADDR_EQ(rt->rt_dest, dest)) {
397 if (rt->rt_flags & IEEE80211_MESHRT_FLAGS_PROXY) {
398 ms->ms_ppath->mpp_senderror(vap, dest, rt,
399 IEEE80211_REASON_MESH_PERR_NO_PROXY);
401 ms->ms_ppath->mpp_senderror(vap, dest, rt,
402 IEEE80211_REASON_MESH_PERR_DEST_UNREACH);
413 ieee80211_mesh_rt_flush(struct ieee80211vap *vap)
415 struct ieee80211_mesh_state *ms = vap->iv_mesh;
416 struct ieee80211_mesh_route *rt, *next;
421 TAILQ_FOREACH_SAFE(rt, &ms->ms_routes, rt_next, next)
427 ieee80211_mesh_rt_flush_peer(struct ieee80211vap *vap,
428 const uint8_t peer[IEEE80211_ADDR_LEN])
430 struct ieee80211_mesh_state *ms = vap->iv_mesh;
431 struct ieee80211_mesh_route *rt, *next;
434 TAILQ_FOREACH_SAFE(rt, &ms->ms_routes, rt_next, next) {
435 if (IEEE80211_ADDR_EQ(rt->rt_nexthop, peer))
442 * Flush expired routing entries, i.e. those in invalid state for
446 mesh_rt_flush_invalid(struct ieee80211vap *vap)
448 struct ieee80211_mesh_state *ms = vap->iv_mesh;
449 struct ieee80211_mesh_route *rt, *next;
454 TAILQ_FOREACH_SAFE(rt, &ms->ms_routes, rt_next, next) {
455 /* Discover paths will be deleted by their own callout */
456 if (rt->rt_flags & IEEE80211_MESHRT_FLAGS_DISCOVER)
458 ieee80211_mesh_rt_update(rt, 0);
459 if ((rt->rt_flags & IEEE80211_MESHRT_FLAGS_VALID) == 0)
465 #define N(a) (sizeof(a) / sizeof(a[0]))
467 ieee80211_mesh_register_proto_path(const struct ieee80211_mesh_proto_path *mpp)
469 int i, firstempty = -1;
471 for (i = 0; i < N(mesh_proto_paths); i++) {
472 if (strncmp(mpp->mpp_descr, mesh_proto_paths[i].mpp_descr,
473 IEEE80211_MESH_PROTO_DSZ) == 0)
475 if (!mesh_proto_paths[i].mpp_active && firstempty == -1)
480 memcpy(&mesh_proto_paths[firstempty], mpp, sizeof(*mpp));
481 mesh_proto_paths[firstempty].mpp_active = 1;
486 ieee80211_mesh_register_proto_metric(const struct
487 ieee80211_mesh_proto_metric *mpm)
489 int i, firstempty = -1;
491 for (i = 0; i < N(mesh_proto_metrics); i++) {
492 if (strncmp(mpm->mpm_descr, mesh_proto_metrics[i].mpm_descr,
493 IEEE80211_MESH_PROTO_DSZ) == 0)
495 if (!mesh_proto_metrics[i].mpm_active && firstempty == -1)
500 memcpy(&mesh_proto_metrics[firstempty], mpm, sizeof(*mpm));
501 mesh_proto_metrics[firstempty].mpm_active = 1;
506 mesh_select_proto_path(struct ieee80211vap *vap, const char *name)
508 struct ieee80211_mesh_state *ms = vap->iv_mesh;
511 for (i = 0; i < N(mesh_proto_paths); i++) {
512 if (strcasecmp(mesh_proto_paths[i].mpp_descr, name) == 0) {
513 ms->ms_ppath = &mesh_proto_paths[i];
521 mesh_select_proto_metric(struct ieee80211vap *vap, const char *name)
523 struct ieee80211_mesh_state *ms = vap->iv_mesh;
526 for (i = 0; i < N(mesh_proto_metrics); i++) {
527 if (strcasecmp(mesh_proto_metrics[i].mpm_descr, name) == 0) {
528 ms->ms_pmetric = &mesh_proto_metrics[i];
537 mesh_gatemode_setup(struct ieee80211vap *vap)
539 struct ieee80211_mesh_state *ms = vap->iv_mesh;
542 * NB: When a mesh gate is running as a ROOT it shall
543 * not send out periodic GANNs but instead mark the
544 * mesh gate flag for the corresponding proactive PREQ
547 if (ms->ms_flags & IEEE80211_MESHFLAGS_ROOT ||
548 (ms->ms_flags & IEEE80211_MESHFLAGS_GATE) == 0) {
549 callout_drain(&ms->ms_gatetimer);
552 callout_reset(&ms->ms_gatetimer, ieee80211_mesh_gateint,
553 mesh_gatemode_cb, vap);
557 mesh_gatemode_cb(void *arg)
559 struct ieee80211vap *vap = (struct ieee80211vap *)arg;
560 struct ieee80211_mesh_state *ms = vap->iv_mesh;
561 struct ieee80211_meshgann_ie gann;
563 gann.gann_flags = 0; /* Reserved */
564 gann.gann_hopcount = 0;
565 gann.gann_ttl = ms->ms_ttl;
566 IEEE80211_ADDR_COPY(gann.gann_addr, vap->iv_myaddr);
567 gann.gann_seq = ms->ms_gateseq++;
568 gann.gann_interval = ieee80211_mesh_gateint;
570 IEEE80211_NOTE(vap, IEEE80211_MSG_MESH, vap->iv_bss,
571 "send broadcast GANN (seq %u)", gann.gann_seq);
573 ieee80211_send_action(vap->iv_bss, IEEE80211_ACTION_CAT_MESH,
574 IEEE80211_ACTION_MESH_GANN, &gann);
575 mesh_gatemode_setup(vap);
579 ieee80211_mesh_init(void)
582 memset(mesh_proto_paths, 0, sizeof(mesh_proto_paths));
583 memset(mesh_proto_metrics, 0, sizeof(mesh_proto_metrics));
586 * Setup mesh parameters that depends on the clock frequency.
588 ieee80211_mesh_gateint = msecs_to_ticks(10000);
589 ieee80211_mesh_retrytimeout = msecs_to_ticks(40);
590 ieee80211_mesh_holdingtimeout = msecs_to_ticks(40);
591 ieee80211_mesh_confirmtimeout = msecs_to_ticks(40);
592 ieee80211_mesh_backofftimeout = msecs_to_ticks(5000);
595 * Register action frame handlers.
597 ieee80211_recv_action_register(IEEE80211_ACTION_CAT_SELF_PROT,
598 IEEE80211_ACTION_MESHPEERING_OPEN,
599 mesh_recv_action_meshpeering_open);
600 ieee80211_recv_action_register(IEEE80211_ACTION_CAT_SELF_PROT,
601 IEEE80211_ACTION_MESHPEERING_CONFIRM,
602 mesh_recv_action_meshpeering_confirm);
603 ieee80211_recv_action_register(IEEE80211_ACTION_CAT_SELF_PROT,
604 IEEE80211_ACTION_MESHPEERING_CLOSE,
605 mesh_recv_action_meshpeering_close);
606 ieee80211_recv_action_register(IEEE80211_ACTION_CAT_MESH,
607 IEEE80211_ACTION_MESH_LMETRIC, mesh_recv_action_meshlmetric);
608 ieee80211_recv_action_register(IEEE80211_ACTION_CAT_MESH,
609 IEEE80211_ACTION_MESH_GANN, mesh_recv_action_meshgate);
611 ieee80211_send_action_register(IEEE80211_ACTION_CAT_SELF_PROT,
612 IEEE80211_ACTION_MESHPEERING_OPEN,
613 mesh_send_action_meshpeering_open);
614 ieee80211_send_action_register(IEEE80211_ACTION_CAT_SELF_PROT,
615 IEEE80211_ACTION_MESHPEERING_CONFIRM,
616 mesh_send_action_meshpeering_confirm);
617 ieee80211_send_action_register(IEEE80211_ACTION_CAT_SELF_PROT,
618 IEEE80211_ACTION_MESHPEERING_CLOSE,
619 mesh_send_action_meshpeering_close);
620 ieee80211_send_action_register(IEEE80211_ACTION_CAT_MESH,
621 IEEE80211_ACTION_MESH_LMETRIC,
622 mesh_send_action_meshlmetric);
623 ieee80211_send_action_register(IEEE80211_ACTION_CAT_MESH,
624 IEEE80211_ACTION_MESH_GANN,
625 mesh_send_action_meshgate);
628 * Register Airtime Link Metric.
630 ieee80211_mesh_register_proto_metric(&mesh_metric_airtime);
633 SYSINIT(wlan_mesh, SI_SUB_DRIVERS, SI_ORDER_FIRST, ieee80211_mesh_init, NULL);
636 ieee80211_mesh_attach(struct ieee80211com *ic)
638 ic->ic_vattach[IEEE80211_M_MBSS] = mesh_vattach;
642 ieee80211_mesh_detach(struct ieee80211com *ic)
647 mesh_vdetach_peers(void *arg, struct ieee80211_node *ni)
649 struct ieee80211com *ic = ni->ni_ic;
652 if (ni->ni_mlstate == IEEE80211_NODE_MESH_ESTABLISHED) {
653 args[0] = ni->ni_mlpid;
654 args[1] = ni->ni_mllid;
655 args[2] = IEEE80211_REASON_PEER_LINK_CANCELED;
656 ieee80211_send_action(ni,
657 IEEE80211_ACTION_CAT_SELF_PROT,
658 IEEE80211_ACTION_MESHPEERING_CLOSE,
661 callout_drain(&ni->ni_mltimer);
662 /* XXX belongs in hwmp */
663 ieee80211_ageq_drain_node(&ic->ic_stageq,
664 (void *)(uintptr_t) ieee80211_mac_hash(ic, ni->ni_macaddr));
668 mesh_vdetach(struct ieee80211vap *vap)
670 struct ieee80211_mesh_state *ms = vap->iv_mesh;
672 callout_drain(&ms->ms_cleantimer);
673 ieee80211_iterate_nodes(&vap->iv_ic->ic_sta, mesh_vdetach_peers,
675 ieee80211_mesh_rt_flush(vap);
676 #if defined(__DragonFly__)
677 lockuninit(&ms->ms_rt_lock);
679 mtx_destroy(&ms->ms_rt_lock);
681 ms->ms_ppath->mpp_vdetach(vap);
682 kfree(vap->iv_mesh, M_80211_VAP);
687 mesh_vattach(struct ieee80211vap *vap)
689 struct ieee80211_mesh_state *ms;
690 vap->iv_newstate = mesh_newstate;
691 vap->iv_input = mesh_input;
692 vap->iv_opdetach = mesh_vdetach;
693 vap->iv_recv_mgmt = mesh_recv_mgmt;
694 vap->iv_recv_ctl = mesh_recv_ctl;
695 ms = kmalloc(sizeof(struct ieee80211_mesh_state), M_80211_VAP,
698 kprintf("%s: couldn't alloc MBSS state\n", __func__);
703 ms->ms_flags = (IEEE80211_MESHFLAGS_AP | IEEE80211_MESHFLAGS_FWD);
704 ms->ms_ttl = IEEE80211_MESH_DEFAULT_TTL;
705 TAILQ_INIT(&ms->ms_known_gates);
706 TAILQ_INIT(&ms->ms_routes);
707 #if defined(__DragonFly__)
708 lockinit(&ms->ms_rt_lock, "MBSS", 0, 0);
710 mtx_init(&ms->ms_rt_lock, "MBSS", "802.11s routing table", MTX_DEF);
712 callout_init_mp(&ms->ms_cleantimer);
713 callout_init_mp(&ms->ms_gatetimer);
715 mesh_select_proto_metric(vap, "AIRTIME");
716 KASSERT(ms->ms_pmetric, ("ms_pmetric == NULL"));
717 mesh_select_proto_path(vap, "HWMP");
718 KASSERT(ms->ms_ppath, ("ms_ppath == NULL"));
719 ms->ms_ppath->mpp_vattach(vap);
723 * IEEE80211_M_MBSS vap state machine handler.
726 mesh_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
728 struct ieee80211_mesh_state *ms = vap->iv_mesh;
729 struct ieee80211com *ic = vap->iv_ic;
730 struct ieee80211_node *ni;
731 enum ieee80211_state ostate;
733 IEEE80211_LOCK_ASSERT(ic);
735 ostate = vap->iv_state;
736 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE, "%s: %s -> %s (%d)\n",
737 __func__, ieee80211_state_name[ostate],
738 ieee80211_state_name[nstate], arg);
739 vap->iv_state = nstate; /* state transition */
740 if (ostate != IEEE80211_S_SCAN)
741 ieee80211_cancel_scan(vap); /* background scan */
742 ni = vap->iv_bss; /* NB: no reference held */
743 if (nstate != IEEE80211_S_RUN && ostate == IEEE80211_S_RUN) {
744 callout_drain(&ms->ms_cleantimer);
745 callout_drain(&ms->ms_gatetimer);
748 case IEEE80211_S_INIT:
750 case IEEE80211_S_SCAN:
751 ieee80211_cancel_scan(vap);
753 case IEEE80211_S_CAC:
754 ieee80211_dfs_cac_stop(vap);
756 case IEEE80211_S_RUN:
757 ieee80211_iterate_nodes(&ic->ic_sta,
758 mesh_vdetach_peers, NULL);
763 if (ostate != IEEE80211_S_INIT) {
764 /* NB: optimize INIT -> INIT case */
765 ieee80211_reset_bss(vap);
766 ieee80211_mesh_rt_flush(vap);
769 case IEEE80211_S_SCAN:
771 case IEEE80211_S_INIT:
772 if (vap->iv_des_chan != IEEE80211_CHAN_ANYC &&
773 !IEEE80211_IS_CHAN_RADAR(vap->iv_des_chan) &&
776 * Already have a channel and a mesh ID; bypass
777 * the scan and startup immediately.
779 ieee80211_create_ibss(vap, vap->iv_des_chan);
783 * Initiate a scan. We can come here as a result
784 * of an IEEE80211_IOC_SCAN_REQ too in which case
785 * the vap will be marked with IEEE80211_FEXT_SCANREQ
786 * and the scan request parameters will be present
787 * in iv_scanreq. Otherwise we do the default.
789 if (vap->iv_flags_ext & IEEE80211_FEXT_SCANREQ) {
790 ieee80211_check_scan(vap,
791 vap->iv_scanreq_flags,
792 vap->iv_scanreq_duration,
793 vap->iv_scanreq_mindwell,
794 vap->iv_scanreq_maxdwell,
795 vap->iv_scanreq_nssid, vap->iv_scanreq_ssid);
796 vap->iv_flags_ext &= ~IEEE80211_FEXT_SCANREQ;
798 ieee80211_check_scan_current(vap);
804 case IEEE80211_S_CAC:
806 * Start CAC on a DFS channel. We come here when starting
807 * a bss on a DFS channel (see ieee80211_create_ibss).
809 ieee80211_dfs_cac_start(vap);
811 case IEEE80211_S_RUN:
813 case IEEE80211_S_INIT:
815 * Already have a channel; bypass the
816 * scan and startup immediately.
817 * Note that ieee80211_create_ibss will call
818 * back to do a RUN->RUN state change.
820 ieee80211_create_ibss(vap,
821 ieee80211_ht_adjust_channel(ic,
822 ic->ic_curchan, vap->iv_flags_ht));
823 /* NB: iv_bss is changed on return */
825 case IEEE80211_S_CAC:
827 * NB: This is the normal state change when CAC
828 * expires and no radar was detected; no need to
829 * clear the CAC timer as it's already expired.
832 case IEEE80211_S_CSA:
835 * Shorten inactivity timer of associated stations
836 * to weed out sta's that don't follow a CSA.
838 ieee80211_iterate_nodes(&ic->ic_sta, sta_csa, vap);
841 * Update bss node channel to reflect where
842 * we landed after CSA.
844 ieee80211_node_set_chan(vap->iv_bss,
845 ieee80211_ht_adjust_channel(ic, ic->ic_curchan,
846 ieee80211_htchanflags(vap->iv_bss->ni_chan)));
847 /* XXX bypass debug msgs */
849 case IEEE80211_S_SCAN:
850 case IEEE80211_S_RUN:
851 #ifdef IEEE80211_DEBUG
852 if (ieee80211_msg_debug(vap)) {
853 struct ieee80211_node *ni = vap->iv_bss;
855 "synchronized with %s meshid ",
856 ether_sprintf(ni->ni_meshid));
857 ieee80211_print_essid(ni->ni_meshid,
860 kprintf(" channel %d\n",
861 ieee80211_chan2ieee(ic, ic->ic_curchan));
868 ieee80211_node_authorize(vap->iv_bss);
869 callout_reset(&ms->ms_cleantimer, ms->ms_ppath->mpp_inact,
870 mesh_rt_cleanup_cb, vap);
871 mesh_gatemode_setup(vap);
876 /* NB: ostate not nstate */
877 ms->ms_ppath->mpp_newstate(vap, ostate, arg);
882 mesh_rt_cleanup_cb(void *arg)
884 struct ieee80211vap *vap = arg;
885 struct ieee80211_mesh_state *ms = vap->iv_mesh;
887 mesh_rt_flush_invalid(vap);
888 callout_reset(&ms->ms_cleantimer, ms->ms_ppath->mpp_inact,
889 mesh_rt_cleanup_cb, vap);
893 * Mark a mesh STA as gate and return a pointer to it.
894 * If this is first time, we create a new gate route.
895 * Always update the path route to this mesh gate.
897 struct ieee80211_mesh_gate_route *
898 ieee80211_mesh_mark_gate(struct ieee80211vap *vap, const uint8_t *addr,
899 struct ieee80211_mesh_route *rt)
901 struct ieee80211_mesh_state *ms = vap->iv_mesh;
902 struct ieee80211_mesh_gate_route *gr = NULL, *next;
906 TAILQ_FOREACH_SAFE(gr, &ms->ms_known_gates, gr_next, next) {
907 if (IEEE80211_ADDR_EQ(gr->gr_addr, addr)) {
914 /* New mesh gate add it to known table. */
915 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH, addr,
916 "%s", "stored new gate information from pro-PREQ.");
917 gr = kmalloc(ALIGN(sizeof(struct ieee80211_mesh_gate_route)),
918 M_80211_MESH_GT_RT, M_INTWAIT | M_ZERO);
919 IEEE80211_ADDR_COPY(gr->gr_addr, addr);
920 TAILQ_INSERT_TAIL(&ms->ms_known_gates, gr, gr_next);
923 /* TODO: link from path route to gate route */
931 * Helper function to note the Mesh Peer Link FSM change.
934 mesh_linkchange(struct ieee80211_node *ni, enum ieee80211_mesh_mlstate state)
936 struct ieee80211vap *vap = ni->ni_vap;
937 struct ieee80211_mesh_state *ms = vap->iv_mesh;
938 #ifdef IEEE80211_DEBUG
939 static const char *meshlinkstates[] = {
940 [IEEE80211_NODE_MESH_IDLE] = "IDLE",
941 [IEEE80211_NODE_MESH_OPENSNT] = "OPEN SENT",
942 [IEEE80211_NODE_MESH_OPENRCV] = "OPEN RECEIVED",
943 [IEEE80211_NODE_MESH_CONFIRMRCV] = "CONFIRM RECEIVED",
944 [IEEE80211_NODE_MESH_ESTABLISHED] = "ESTABLISHED",
945 [IEEE80211_NODE_MESH_HOLDING] = "HOLDING"
948 IEEE80211_NOTE(vap, IEEE80211_MSG_MESH,
949 ni, "peer link: %s -> %s",
950 meshlinkstates[ni->ni_mlstate], meshlinkstates[state]);
952 /* track neighbor count */
953 if (state == IEEE80211_NODE_MESH_ESTABLISHED &&
954 ni->ni_mlstate != IEEE80211_NODE_MESH_ESTABLISHED) {
955 KASSERT(ms->ms_neighbors < 65535, ("neighbor count overflow"));
957 ieee80211_beacon_notify(vap, IEEE80211_BEACON_MESHCONF);
958 } else if (ni->ni_mlstate == IEEE80211_NODE_MESH_ESTABLISHED &&
959 state != IEEE80211_NODE_MESH_ESTABLISHED) {
960 KASSERT(ms->ms_neighbors > 0, ("neighbor count 0"));
962 ieee80211_beacon_notify(vap, IEEE80211_BEACON_MESHCONF);
964 ni->ni_mlstate = state;
966 case IEEE80211_NODE_MESH_HOLDING:
967 ms->ms_ppath->mpp_peerdown(ni);
969 case IEEE80211_NODE_MESH_ESTABLISHED:
970 ieee80211_mesh_discover(vap, ni->ni_macaddr, NULL);
978 * Helper function to generate a unique local ID required for mesh
979 * peer establishment.
982 mesh_checkid(void *arg, struct ieee80211_node *ni)
986 if (*r == ni->ni_mllid)
987 *(uint16_t *)arg = 0;
991 mesh_generateid(struct ieee80211vap *vap)
997 get_random_bytes(&r, 2);
998 ieee80211_iterate_nodes(&vap->iv_ic->ic_sta, mesh_checkid, &r);
1000 } while (r == 0 && maxiter > 0);
1005 * Verifies if we already received this packet by checking its
1007 * Returns 0 if the frame is to be accepted, 1 otherwise.
1010 mesh_checkpseq(struct ieee80211vap *vap,
1011 const uint8_t source[IEEE80211_ADDR_LEN], uint32_t seq)
1013 struct ieee80211_mesh_route *rt;
1015 rt = ieee80211_mesh_rt_find(vap, source);
1017 rt = ieee80211_mesh_rt_add(vap, source);
1019 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH, source,
1020 "%s", "add mcast route failed");
1021 vap->iv_stats.is_mesh_rtaddfailed++;
1024 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH, source,
1025 "add mcast route, mesh seqno %d", seq);
1026 rt->rt_lastmseq = seq;
1029 if (IEEE80211_MESH_SEQ_GEQ(rt->rt_lastmseq, seq)) {
1032 rt->rt_lastmseq = seq;
1038 * Iterate the routing table and locate the next hop.
1040 struct ieee80211_node *
1041 ieee80211_mesh_find_txnode(struct ieee80211vap *vap,
1042 const uint8_t dest[IEEE80211_ADDR_LEN])
1044 struct ieee80211_mesh_route *rt;
1046 rt = ieee80211_mesh_rt_find(vap, dest);
1049 if ((rt->rt_flags & IEEE80211_MESHRT_FLAGS_VALID) == 0) {
1050 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH, dest,
1051 "%s: !valid, flags 0x%x", __func__, rt->rt_flags);
1055 if (rt->rt_flags & IEEE80211_MESHRT_FLAGS_PROXY) {
1056 rt = ieee80211_mesh_rt_find(vap, rt->rt_mesh_gate);
1057 if (rt == NULL) return NULL;
1058 if ((rt->rt_flags & IEEE80211_MESHRT_FLAGS_VALID) == 0) {
1059 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH, dest,
1060 "%s: meshgate !valid, flags 0x%x", __func__,
1066 return ieee80211_find_txnode(vap, rt->rt_nexthop);
1070 mesh_transmit_to_gate(struct ieee80211vap *vap, struct mbuf *m,
1071 struct ieee80211_mesh_route *rt_gate)
1073 struct ifnet *ifp = vap->iv_ifp;
1074 struct ieee80211_node *ni;
1076 IEEE80211_TX_UNLOCK_ASSERT(vap->iv_ic);
1078 ni = ieee80211_mesh_find_txnode(vap, rt_gate->rt_dest);
1080 IFNET_STAT_INC(ifp, oerrors, 1);
1086 * Send through the VAP packet transmit path.
1087 * This consumes the node ref grabbed above and
1088 * the mbuf, regardless of whether there's a problem
1091 (void) ieee80211_vap_pkt_send_dest(vap, m, ni);
1095 * Forward the queued frames to known valid mesh gates.
1096 * Assume destination to be outside the MBSS (i.e. proxy entry),
1097 * If no valid mesh gates are known silently discard queued frames.
1098 * After transmitting frames to all known valid mesh gates, this route
1099 * will be marked invalid, and a new path discovery will happen in the hopes
1100 * that (at least) one of the mesh gates have a new proxy entry for us to use.
1103 ieee80211_mesh_forward_to_gates(struct ieee80211vap *vap,
1104 struct ieee80211_mesh_route *rt_dest)
1106 struct ieee80211com *ic = vap->iv_ic;
1107 struct ieee80211_mesh_state *ms = vap->iv_mesh;
1108 struct ieee80211_mesh_route *rt_gate;
1109 struct ieee80211_mesh_gate_route *gr = NULL, *gr_next;
1110 struct mbuf *m, *mcopy, *next;
1112 IEEE80211_TX_UNLOCK_ASSERT(ic);
1114 KASSERT( rt_dest->rt_flags == IEEE80211_MESHRT_FLAGS_DISCOVER,
1115 ("Route is not marked with IEEE80211_MESHRT_FLAGS_DISCOVER"));
1117 /* XXX: send to more than one valid mash gate */
1120 m = ieee80211_ageq_remove(&ic->ic_stageq,
1121 (struct ieee80211_node *)(uintptr_t)
1122 ieee80211_mac_hash(ic, rt_dest->rt_dest));
1124 TAILQ_FOREACH_SAFE(gr, &ms->ms_known_gates, gr_next, gr_next) {
1125 rt_gate = gr->gr_route;
1126 if (rt_gate == NULL) {
1127 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_HWMP,
1129 "mesh gate with no path %6D",
1133 if ((rt_gate->rt_flags & IEEE80211_MESHRT_FLAGS_VALID) == 0)
1135 KASSERT(rt_gate->rt_flags & IEEE80211_MESHRT_FLAGS_GATE,
1136 ("route not marked as a mesh gate"));
1137 KASSERT((rt_gate->rt_flags &
1138 IEEE80211_MESHRT_FLAGS_PROXY) == 0,
1139 ("found mesh gate that is also marked porxy"));
1141 * convert route to a proxy route gated by the current
1142 * mesh gate, this is needed so encap can built data
1143 * frame with correct address.
1145 rt_dest->rt_flags = IEEE80211_MESHRT_FLAGS_PROXY |
1146 IEEE80211_MESHRT_FLAGS_VALID;
1147 rt_dest->rt_ext_seq = 1; /* random value */
1148 IEEE80211_ADDR_COPY(rt_dest->rt_mesh_gate, rt_gate->rt_dest);
1149 IEEE80211_ADDR_COPY(rt_dest->rt_nexthop, rt_gate->rt_nexthop);
1150 rt_dest->rt_metric = rt_gate->rt_metric;
1151 rt_dest->rt_nhops = rt_gate->rt_nhops;
1152 ieee80211_mesh_rt_update(rt_dest, ms->ms_ppath->mpp_inact);
1155 mcopy = m_dup(m, M_NOWAIT);
1156 for (; mcopy != NULL; mcopy = next) {
1157 next = mcopy->m_nextpkt;
1158 mcopy->m_nextpkt = NULL;
1159 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_HWMP,
1161 "flush queued frame %p len %d", mcopy,
1162 mcopy->m_pkthdr.len);
1163 mesh_transmit_to_gate(vap, mcopy, rt_gate);
1167 rt_dest->rt_flags = 0; /* Mark invalid */
1173 * Forward the specified frame.
1174 * Decrement the TTL and set TA to our MAC address.
1177 mesh_forward(struct ieee80211vap *vap, struct mbuf *m,
1178 const struct ieee80211_meshcntl *mc)
1180 struct ieee80211com *ic = vap->iv_ic;
1181 struct ieee80211_mesh_state *ms = vap->iv_mesh;
1182 struct ifnet *ifp = vap->iv_ifp;
1183 const struct ieee80211_frame *wh =
1184 mtod(m, const struct ieee80211_frame *);
1186 struct ieee80211_meshcntl *mccopy;
1187 struct ieee80211_frame *whcopy;
1188 struct ieee80211_node *ni;
1191 /* This is called from the RX path - don't hold this lock */
1192 IEEE80211_TX_UNLOCK_ASSERT(ic);
1195 * mesh ttl of 1 means we are the last one receving it,
1196 * according to amendment we decrement and then check if
1197 * 0, if so we dont forward.
1199 if (mc->mc_ttl < 1) {
1200 IEEE80211_NOTE_FRAME(vap, IEEE80211_MSG_MESH, wh,
1201 "%s", "frame not fwd'd, ttl 1");
1202 vap->iv_stats.is_mesh_fwd_ttl++;
1205 if (!(ms->ms_flags & IEEE80211_MESHFLAGS_FWD)) {
1206 IEEE80211_NOTE_FRAME(vap, IEEE80211_MSG_MESH, wh,
1207 "%s", "frame not fwd'd, fwding disabled");
1208 vap->iv_stats.is_mesh_fwd_disabled++;
1211 mcopy = m_dup(m, M_NOWAIT);
1212 if (mcopy == NULL) {
1213 IEEE80211_NOTE_FRAME(vap, IEEE80211_MSG_MESH, wh,
1214 "%s", "frame not fwd'd, cannot dup");
1215 vap->iv_stats.is_mesh_fwd_nobuf++;
1216 IFNET_STAT_INC(ifp, oerrors, 1);
1219 mcopy = m_pullup(mcopy, ieee80211_hdrspace(ic, wh) +
1220 sizeof(struct ieee80211_meshcntl));
1221 if (mcopy == NULL) {
1222 IEEE80211_NOTE_FRAME(vap, IEEE80211_MSG_MESH, wh,
1223 "%s", "frame not fwd'd, too short");
1224 vap->iv_stats.is_mesh_fwd_tooshort++;
1225 IFNET_STAT_INC(ifp, oerrors, 1);
1229 whcopy = mtod(mcopy, struct ieee80211_frame *);
1230 mccopy = (struct ieee80211_meshcntl *)
1231 (mtod(mcopy, uint8_t *) + ieee80211_hdrspace(ic, wh));
1232 /* XXX clear other bits? */
1233 whcopy->i_fc[1] &= ~IEEE80211_FC1_RETRY;
1234 IEEE80211_ADDR_COPY(whcopy->i_addr2, vap->iv_myaddr);
1235 if (IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1236 ni = ieee80211_ref_node(vap->iv_bss);
1237 mcopy->m_flags |= M_MCAST;
1239 ni = ieee80211_mesh_find_txnode(vap, whcopy->i_addr3);
1242 * [Optional] any of the following three actions:
1243 * o silently discard
1244 * o trigger a path discovery
1245 * o inform TA that meshDA is unknown.
1247 IEEE80211_NOTE_FRAME(vap, IEEE80211_MSG_MESH, wh,
1248 "%s", "frame not fwd'd, no path");
1249 ms->ms_ppath->mpp_senderror(vap, whcopy->i_addr3, NULL,
1250 IEEE80211_REASON_MESH_PERR_NO_FI);
1251 vap->iv_stats.is_mesh_fwd_nopath++;
1255 IEEE80211_ADDR_COPY(whcopy->i_addr1, ni->ni_macaddr);
1257 KASSERT(mccopy->mc_ttl > 0, ("%s called with wrong ttl", __func__));
1260 /* XXX calculate priority so drivers can find the tx queue */
1261 M_WME_SETAC(mcopy, WME_AC_BE);
1263 /* XXX do we know m_nextpkt is NULL? */
1264 mcopy->m_pkthdr.rcvif = (void *) ni;
1267 * XXX this bypasses all of the VAP TX handling; it passes frames
1268 * directly to the parent interface.
1270 * Because of this, there's no TX lock being held as there's no
1271 * encaps state being used.
1273 * Doing a direct parent transmit may not be the correct thing
1274 * to do here; we'll have to re-think this soon.
1276 IEEE80211_TX_LOCK(ic);
1277 err = ieee80211_parent_xmitpkt(ic, mcopy);
1278 IEEE80211_TX_UNLOCK(ic);
1280 /* NB: IFQ_HANDOFF reclaims mbuf */
1281 ieee80211_free_node(ni);
1283 IFNET_STAT_INC(ifp, opackets, 1);
1287 static struct mbuf *
1288 mesh_decap(struct ieee80211vap *vap, struct mbuf *m, int hdrlen, int meshdrlen)
1290 #define WHDIR(wh) ((wh)->i_fc[1] & IEEE80211_FC1_DIR_MASK)
1291 #define MC01(mc) ((const struct ieee80211_meshcntl_ae01 *)mc)
1292 uint8_t b[sizeof(struct ieee80211_qosframe_addr4) +
1293 sizeof(struct ieee80211_meshcntl_ae10)];
1294 const struct ieee80211_qosframe_addr4 *wh;
1295 const struct ieee80211_meshcntl_ae10 *mc;
1296 struct ether_header *eh;
1300 if (m->m_len < hdrlen + sizeof(*llc) &&
1301 (m = m_pullup(m, hdrlen + sizeof(*llc))) == NULL) {
1302 IEEE80211_DPRINTF(vap, IEEE80211_MSG_ANY,
1303 "discard data frame: %s", "m_pullup failed");
1304 vap->iv_stats.is_rx_tooshort++;
1307 memcpy(b, mtod(m, caddr_t), hdrlen);
1308 wh = (const struct ieee80211_qosframe_addr4 *)&b[0];
1309 mc = (const struct ieee80211_meshcntl_ae10 *)&b[hdrlen - meshdrlen];
1310 KASSERT(WHDIR(wh) == IEEE80211_FC1_DIR_FROMDS ||
1311 WHDIR(wh) == IEEE80211_FC1_DIR_DSTODS,
1312 ("bogus dir, fc 0x%x:0x%x", wh->i_fc[0], wh->i_fc[1]));
1314 llc = (struct llc *)(mtod(m, caddr_t) + hdrlen);
1315 if (llc->llc_dsap == LLC_SNAP_LSAP && llc->llc_ssap == LLC_SNAP_LSAP &&
1316 llc->llc_control == LLC_UI && llc->llc_snap.org_code[0] == 0 &&
1317 llc->llc_snap.org_code[1] == 0 && llc->llc_snap.org_code[2] == 0 &&
1318 /* NB: preserve AppleTalk frames that have a native SNAP hdr */
1319 !(llc->llc_snap.ether_type == htons(ETHERTYPE_AARP) ||
1320 llc->llc_snap.ether_type == htons(ETHERTYPE_IPX))) {
1321 m_adj(m, hdrlen + sizeof(struct llc) - sizeof(*eh));
1324 m_adj(m, hdrlen - sizeof(*eh));
1326 eh = mtod(m, struct ether_header *);
1327 ae = mc->mc_flags & IEEE80211_MESH_AE_MASK;
1328 if (WHDIR(wh) == IEEE80211_FC1_DIR_FROMDS) {
1329 IEEE80211_ADDR_COPY(eh->ether_dhost, wh->i_addr1);
1330 if (ae == IEEE80211_MESH_AE_00) {
1331 IEEE80211_ADDR_COPY(eh->ether_shost, wh->i_addr3);
1332 } else if (ae == IEEE80211_MESH_AE_01) {
1333 IEEE80211_ADDR_COPY(eh->ether_shost,
1334 MC01(mc)->mc_addr4);
1336 IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
1337 (const struct ieee80211_frame *)wh, NULL,
1339 vap->iv_stats.is_mesh_badae++;
1344 if (ae == IEEE80211_MESH_AE_00) {
1345 IEEE80211_ADDR_COPY(eh->ether_dhost, wh->i_addr3);
1346 IEEE80211_ADDR_COPY(eh->ether_shost, wh->i_addr4);
1347 } else if (ae == IEEE80211_MESH_AE_10) {
1348 IEEE80211_ADDR_COPY(eh->ether_dhost, mc->mc_addr5);
1349 IEEE80211_ADDR_COPY(eh->ether_shost, mc->mc_addr6);
1351 IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
1352 (const struct ieee80211_frame *)wh, NULL,
1354 vap->iv_stats.is_mesh_badae++;
1359 #ifndef __NO_STRICT_ALIGNMENT
1360 if (!ALIGNED_POINTER(mtod(m, caddr_t) + sizeof(*eh), uint32_t)) {
1361 m = ieee80211_realign(vap, m, sizeof(*eh));
1365 #endif /* !__NO_STRICT_ALIGNMENT */
1367 eh = mtod(m, struct ether_header *);
1368 eh->ether_type = htons(m->m_pkthdr.len - sizeof(*eh));
1376 * Return non-zero if the unicast mesh data frame should be processed
1377 * locally. Frames that are not proxy'd have our address, otherwise
1378 * we need to consult the routing table to look for a proxy entry.
1381 mesh_isucastforme(struct ieee80211vap *vap, const struct ieee80211_frame *wh,
1382 const struct ieee80211_meshcntl *mc)
1384 int ae = mc->mc_flags & 3;
1386 KASSERT((wh->i_fc[1] & IEEE80211_FC1_DIR_MASK) == IEEE80211_FC1_DIR_DSTODS,
1387 ("bad dir 0x%x:0x%x", wh->i_fc[0], wh->i_fc[1]));
1388 KASSERT(ae == IEEE80211_MESH_AE_00 || ae == IEEE80211_MESH_AE_10,
1390 if (ae == IEEE80211_MESH_AE_10) { /* ucast w/ proxy */
1391 const struct ieee80211_meshcntl_ae10 *mc10 =
1392 (const struct ieee80211_meshcntl_ae10 *) mc;
1393 struct ieee80211_mesh_route *rt =
1394 ieee80211_mesh_rt_find(vap, mc10->mc_addr5);
1395 /* check for proxy route to ourself */
1396 return (rt != NULL &&
1397 (rt->rt_flags & IEEE80211_MESHRT_FLAGS_PROXY));
1398 } else /* ucast w/o proxy */
1399 return IEEE80211_ADDR_EQ(wh->i_addr3, vap->iv_myaddr);
1403 * Verifies transmitter, updates lifetime, precursor list and forwards data.
1404 * > 0 means we have forwarded data and no need to process locally
1405 * == 0 means we want to process locally (and we may have forwarded data
1406 * < 0 means there was an error and data should be discarded
1409 mesh_recv_indiv_data_to_fwrd(struct ieee80211vap *vap, struct mbuf *m,
1410 struct ieee80211_frame *wh, const struct ieee80211_meshcntl *mc)
1412 struct ieee80211_qosframe_addr4 *qwh;
1413 struct ieee80211_mesh_state *ms = vap->iv_mesh;
1414 struct ieee80211_mesh_route *rt_meshda, *rt_meshsa;
1416 /* This is called from the RX path - don't hold this lock */
1417 IEEE80211_TX_UNLOCK_ASSERT(vap->iv_ic);
1419 qwh = (struct ieee80211_qosframe_addr4 *)wh;
1423 * o verify addr2 is a legitimate transmitter
1424 * o lifetime of precursor of addr3 (addr2) is max(init, curr)
1425 * o lifetime of precursor of addr4 (nexthop) is max(init, curr)
1428 /* set lifetime of addr3 (meshDA) to initial value */
1429 rt_meshda = ieee80211_mesh_rt_find(vap, qwh->i_addr3);
1430 if (rt_meshda == NULL) {
1431 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH, qwh->i_addr2,
1432 "no route to meshDA(%6D)", qwh->i_addr3, ":");
1434 * [Optional] any of the following three actions:
1435 * o silently discard [X]
1436 * o trigger a path discovery [ ]
1437 * o inform TA that meshDA is unknown. [ ]
1443 ieee80211_mesh_rt_update(rt_meshda, ticks_to_msecs(
1444 ms->ms_ppath->mpp_inact));
1446 /* set lifetime of addr4 (meshSA) to initial value */
1447 rt_meshsa = ieee80211_mesh_rt_find(vap, qwh->i_addr4);
1448 KASSERT(rt_meshsa != NULL, ("no route"));
1449 ieee80211_mesh_rt_update(rt_meshsa, ticks_to_msecs(
1450 ms->ms_ppath->mpp_inact));
1452 mesh_forward(vap, m, mc);
1453 return (1); /* dont process locally */
1457 * Verifies transmitter, updates lifetime, precursor list and process data
1458 * locally, if data is proxy with AE = 10 it could mean data should go
1459 * on another mesh path or data should be forwarded to the DS.
1461 * > 0 means we have forwarded data and no need to process locally
1462 * == 0 means we want to process locally (and we may have forwarded data
1463 * < 0 means there was an error and data should be discarded
1466 mesh_recv_indiv_data_to_me(struct ieee80211vap *vap, struct mbuf *m,
1467 struct ieee80211_frame *wh, const struct ieee80211_meshcntl *mc)
1469 struct ieee80211_qosframe_addr4 *qwh;
1470 const struct ieee80211_meshcntl_ae10 *mc10;
1471 struct ieee80211_mesh_state *ms = vap->iv_mesh;
1472 struct ieee80211_mesh_route *rt;
1475 /* This is called from the RX path - don't hold this lock */
1476 IEEE80211_TX_UNLOCK_ASSERT(vap->iv_ic);
1478 qwh = (struct ieee80211_qosframe_addr4 *)wh;
1479 mc10 = (const struct ieee80211_meshcntl_ae10 *)mc;
1483 * o verify addr2 is a legitimate transmitter
1484 * o lifetime of precursor entry is max(init, curr)
1487 /* set lifetime of addr4 (meshSA) to initial value */
1488 rt = ieee80211_mesh_rt_find(vap, qwh->i_addr4);
1489 KASSERT(rt != NULL, ("no route"));
1490 ieee80211_mesh_rt_update(rt, ticks_to_msecs(ms->ms_ppath->mpp_inact));
1493 ae = mc10->mc_flags & IEEE80211_MESH_AE_MASK;
1494 KASSERT(ae == IEEE80211_MESH_AE_00 ||
1495 ae == IEEE80211_MESH_AE_10, ("bad AE %d", ae));
1496 if (ae == IEEE80211_MESH_AE_10) {
1497 if (IEEE80211_ADDR_EQ(mc10->mc_addr5, qwh->i_addr3)) {
1498 return (0); /* process locally */
1501 rt = ieee80211_mesh_rt_find(vap, mc10->mc_addr5);
1503 (rt->rt_flags & IEEE80211_MESHRT_FLAGS_VALID) &&
1504 (rt->rt_flags & IEEE80211_MESHRT_FLAGS_PROXY) == 0) {
1506 * Forward on another mesh-path, according to
1507 * amendment as specified in 9.32.4.1
1509 IEEE80211_ADDR_COPY(qwh->i_addr3, mc10->mc_addr5);
1510 mesh_forward(vap, m,
1511 (const struct ieee80211_meshcntl *)mc10);
1512 return (1); /* dont process locally */
1515 * All other cases: forward of MSDUs from the MBSS to DS indiv.
1516 * addressed according to 13.11.3.2.
1518 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_OUTPUT, qwh->i_addr2,
1519 "forward frame to DS, SA(%6D) DA(%6D)",
1520 mc10->mc_addr6, ":", mc10->mc_addr5, ":");
1522 return (0); /* process locally */
1526 * Try to forward the group addressed data on to other mesh STAs, and
1529 * > 0 means we have forwarded data and no need to process locally
1530 * == 0 means we want to process locally (and we may have forwarded data
1531 * < 0 means there was an error and data should be discarded
1534 mesh_recv_group_data(struct ieee80211vap *vap, struct mbuf *m,
1535 struct ieee80211_frame *wh, const struct ieee80211_meshcntl *mc)
1537 #define MC01(mc) ((const struct ieee80211_meshcntl_ae01 *)mc)
1538 struct ieee80211_mesh_state *ms = vap->iv_mesh;
1540 /* This is called from the RX path - don't hold this lock */
1541 IEEE80211_TX_UNLOCK_ASSERT(vap->iv_ic);
1543 mesh_forward(vap, m, mc);
1545 if(mc->mc_ttl > 0) {
1546 if (mc->mc_flags & IEEE80211_MESH_AE_01) {
1548 * Forward of MSDUs from the MBSS to DS group addressed
1549 * (according to 13.11.3.2)
1550 * This happens by delivering the packet, and a bridge
1551 * will sent it on another port member.
1553 if (ms->ms_flags & IEEE80211_MESHFLAGS_GATE &&
1554 ms->ms_flags & IEEE80211_MESHFLAGS_FWD)
1555 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH,
1556 MC01(mc)->mc_addr4, "%s",
1557 "forward from MBSS to the DS");
1560 return (0); /* process locally */
1565 mesh_input(struct ieee80211_node *ni, struct mbuf *m, int rssi, int nf)
1567 #define HAS_SEQ(type) ((type & 0x4) == 0)
1568 #define MC01(mc) ((const struct ieee80211_meshcntl_ae01 *)mc)
1569 #define MC10(mc) ((const struct ieee80211_meshcntl_ae10 *)mc)
1570 struct ieee80211vap *vap = ni->ni_vap;
1571 struct ieee80211com *ic = ni->ni_ic;
1572 struct ifnet *ifp = vap->iv_ifp;
1573 struct ieee80211_frame *wh;
1574 const struct ieee80211_meshcntl *mc;
1575 int hdrspace, meshdrlen, need_tap, error;
1576 uint8_t dir, type, subtype, ae;
1578 const uint8_t *addr;
1580 ieee80211_seq rxseq;
1582 KASSERT(ni != NULL, ("null node"));
1583 ni->ni_inact = ni->ni_inact_reload;
1585 need_tap = 1; /* mbuf need to be tapped. */
1586 type = -1; /* undefined */
1588 /* This is called from the RX path - don't hold this lock */
1589 IEEE80211_TX_UNLOCK_ASSERT(ic);
1591 if (m->m_pkthdr.len < sizeof(struct ieee80211_frame_min)) {
1592 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
1593 ni->ni_macaddr, NULL,
1594 "too short (1): len %u", m->m_pkthdr.len);
1595 vap->iv_stats.is_rx_tooshort++;
1599 * Bit of a cheat here, we use a pointer for a 3-address
1600 * frame format but don't reference fields past outside
1601 * ieee80211_frame_min w/o first validating the data is
1604 wh = mtod(m, struct ieee80211_frame *);
1606 if ((wh->i_fc[0] & IEEE80211_FC0_VERSION_MASK) !=
1607 IEEE80211_FC0_VERSION_0) {
1608 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
1609 ni->ni_macaddr, NULL, "wrong version %x", wh->i_fc[0]);
1610 vap->iv_stats.is_rx_badversion++;
1613 dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK;
1614 type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
1615 subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
1616 if ((ic->ic_flags & IEEE80211_F_SCAN) == 0) {
1617 IEEE80211_RSSI_LPF(ni->ni_avgrssi, rssi);
1619 if (HAS_SEQ(type)) {
1620 uint8_t tid = ieee80211_gettid(wh);
1622 if (IEEE80211_QOS_HAS_SEQ(wh) &&
1623 TID_TO_WME_AC(tid) >= WME_AC_VI)
1624 ic->ic_wme.wme_hipri_traffic++;
1625 rxseq = le16toh(*(uint16_t *)wh->i_seq);
1626 if (! ieee80211_check_rxseq(ni, wh)) {
1627 /* duplicate, discard */
1628 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
1629 wh->i_addr1, "duplicate",
1630 "seqno <%u,%u> fragno <%u,%u> tid %u",
1631 rxseq >> IEEE80211_SEQ_SEQ_SHIFT,
1632 ni->ni_rxseqs[tid] >>
1633 IEEE80211_SEQ_SEQ_SHIFT,
1634 rxseq & IEEE80211_SEQ_FRAG_MASK,
1635 ni->ni_rxseqs[tid] &
1636 IEEE80211_SEQ_FRAG_MASK,
1638 vap->iv_stats.is_rx_dup++;
1639 IEEE80211_NODE_STAT(ni, rx_dup);
1642 ni->ni_rxseqs[tid] = rxseq;
1645 #ifdef IEEE80211_DEBUG
1647 * It's easier, but too expensive, to simulate different mesh
1648 * topologies by consulting the ACL policy very early, so do this
1651 * NB: this check is also done upon peering link initiation.
1653 if (vap->iv_acl != NULL && !vap->iv_acl->iac_check(vap, wh)) {
1654 IEEE80211_DISCARD(vap, IEEE80211_MSG_ACL,
1655 wh, NULL, "%s", "disallowed by ACL");
1656 vap->iv_stats.is_rx_acl++;
1661 case IEEE80211_FC0_TYPE_DATA:
1662 if (ni == vap->iv_bss)
1664 if (ni->ni_mlstate != IEEE80211_NODE_MESH_ESTABLISHED) {
1665 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_MESH,
1666 ni->ni_macaddr, NULL,
1667 "peer link not yet established (%d)",
1669 vap->iv_stats.is_mesh_nolink++;
1672 if (dir != IEEE80211_FC1_DIR_FROMDS &&
1673 dir != IEEE80211_FC1_DIR_DSTODS) {
1674 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
1675 wh, "data", "incorrect dir 0x%x", dir);
1676 vap->iv_stats.is_rx_wrongdir++;
1680 /* All Mesh data frames are QoS subtype */
1681 if (!HAS_SEQ(type)) {
1682 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
1683 wh, "data", "incorrect subtype 0x%x", subtype);
1684 vap->iv_stats.is_rx_badsubtype++;
1689 * Next up, any fragmentation.
1690 * XXX: we defrag before we even try to forward,
1691 * Mesh Control field is not present in sub-sequent
1692 * fragmented frames. This is in contrast to Draft 4.0.
1694 hdrspace = ieee80211_hdrspace(ic, wh);
1695 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1696 m = ieee80211_defrag(ni, m, hdrspace);
1698 /* Fragment dropped or frame not complete yet */
1702 wh = mtod(m, struct ieee80211_frame *); /* NB: after defrag */
1705 * Now we have a complete Mesh Data frame.
1709 * Only fromDStoDS data frames use 4 address qos frames
1710 * as specified in amendment. Otherwise addr4 is located
1711 * in the Mesh Control field and a 3 address qos frame
1714 if (IEEE80211_IS_DSTODS(wh))
1715 *(uint16_t *)qos = *(uint16_t *)
1716 ((struct ieee80211_qosframe_addr4 *)wh)->i_qos;
1718 *(uint16_t *)qos = *(uint16_t *)
1719 ((struct ieee80211_qosframe *)wh)->i_qos;
1722 * NB: The mesh STA sets the Mesh Control Present
1723 * subfield to 1 in the Mesh Data frame containing
1724 * an unfragmented MSDU, an A-MSDU, or the first
1725 * fragment of an MSDU.
1726 * After defrag it should always be present.
1728 if (!(qos[1] & IEEE80211_QOS_MC)) {
1729 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_MESH,
1730 ni->ni_macaddr, NULL,
1731 "%s", "Mesh control field not present");
1732 vap->iv_stats.is_rx_elem_missing++; /* XXX: kinda */
1736 /* pull up enough to get to the mesh control */
1737 if (m->m_len < hdrspace + sizeof(struct ieee80211_meshcntl) &&
1738 (m = m_pullup(m, hdrspace +
1739 sizeof(struct ieee80211_meshcntl))) == NULL) {
1740 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
1741 ni->ni_macaddr, NULL,
1742 "data too short: expecting %u", hdrspace);
1743 vap->iv_stats.is_rx_tooshort++;
1747 * Now calculate the full extent of the headers. Note
1748 * mesh_decap will pull up anything we didn't get
1749 * above when it strips the 802.11 headers.
1751 mc = (const struct ieee80211_meshcntl *)
1752 (mtod(m, const uint8_t *) + hdrspace);
1753 ae = mc->mc_flags & IEEE80211_MESH_AE_MASK;
1754 meshdrlen = sizeof(struct ieee80211_meshcntl) +
1755 ae * IEEE80211_ADDR_LEN;
1756 hdrspace += meshdrlen;
1758 /* pull complete hdrspace = ieee80211_hdrspace + meshcontrol */
1759 if ((meshdrlen > sizeof(struct ieee80211_meshcntl)) &&
1760 (m->m_len < hdrspace) &&
1761 ((m = m_pullup(m, hdrspace)) == NULL)) {
1762 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
1763 ni->ni_macaddr, NULL,
1764 "data too short: expecting %u", hdrspace);
1765 vap->iv_stats.is_rx_tooshort++;
1768 /* XXX: are we sure there is no reallocating after m_pullup? */
1770 seq = LE_READ_4(mc->mc_seq);
1771 if (IEEE80211_IS_MULTICAST(wh->i_addr1))
1773 else if (ae == IEEE80211_MESH_AE_01)
1774 addr = MC01(mc)->mc_addr4;
1776 addr = ((struct ieee80211_qosframe_addr4 *)wh)->i_addr4;
1777 if (IEEE80211_ADDR_EQ(vap->iv_myaddr, addr)) {
1778 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
1779 addr, "data", "%s", "not to me");
1780 vap->iv_stats.is_rx_wrongbss++; /* XXX kinda */
1783 if (mesh_checkpseq(vap, addr, seq) != 0) {
1784 vap->iv_stats.is_rx_dup++;
1788 /* This code "routes" the frame to the right control path */
1789 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1790 if (IEEE80211_ADDR_EQ(vap->iv_myaddr, wh->i_addr3))
1792 mesh_recv_indiv_data_to_me(vap, m, wh, mc);
1793 else if (IEEE80211_IS_MULTICAST(wh->i_addr3))
1794 error = mesh_recv_group_data(vap, m, wh, mc);
1796 error = mesh_recv_indiv_data_to_fwrd(vap, m,
1799 error = mesh_recv_group_data(vap, m, wh, mc);
1805 if (ieee80211_radiotap_active_vap(vap))
1806 ieee80211_radiotap_rx(vap, m);
1810 * Finally, strip the 802.11 header.
1812 m = mesh_decap(vap, m, hdrspace, meshdrlen);
1814 /* XXX mask bit to check for both */
1815 /* don't count Null data frames as errors */
1816 if (subtype == IEEE80211_FC0_SUBTYPE_NODATA ||
1817 subtype == IEEE80211_FC0_SUBTYPE_QOS_NULL)
1819 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
1820 ni->ni_macaddr, "data", "%s", "decap error");
1821 vap->iv_stats.is_rx_decap++;
1822 IEEE80211_NODE_STAT(ni, rx_decap);
1825 if (qos[0] & IEEE80211_QOS_AMSDU) {
1826 m = ieee80211_decap_amsdu(ni, m);
1828 return IEEE80211_FC0_TYPE_DATA;
1830 ieee80211_deliver_data(vap, ni, m);
1832 case IEEE80211_FC0_TYPE_MGT:
1833 vap->iv_stats.is_rx_mgmt++;
1834 IEEE80211_NODE_STAT(ni, rx_mgmt);
1835 if (dir != IEEE80211_FC1_DIR_NODS) {
1836 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
1837 wh, "mgt", "incorrect dir 0x%x", dir);
1838 vap->iv_stats.is_rx_wrongdir++;
1841 if (m->m_pkthdr.len < sizeof(struct ieee80211_frame)) {
1842 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
1843 ni->ni_macaddr, "mgt", "too short: len %u",
1845 vap->iv_stats.is_rx_tooshort++;
1848 #ifdef IEEE80211_DEBUG
1849 if ((ieee80211_msg_debug(vap) &&
1850 (vap->iv_ic->ic_flags & IEEE80211_F_SCAN)) ||
1851 ieee80211_msg_dumppkts(vap)) {
1852 if_printf(ifp, "received %s from %s rssi %d\n",
1853 ieee80211_mgt_subtype_name[subtype >>
1854 IEEE80211_FC0_SUBTYPE_SHIFT],
1855 ether_sprintf(wh->i_addr2), rssi);
1858 if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
1859 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
1860 wh, NULL, "%s", "WEP set but not permitted");
1861 vap->iv_stats.is_rx_mgtdiscard++; /* XXX */
1864 vap->iv_recv_mgmt(ni, m, subtype, rssi, nf);
1866 case IEEE80211_FC0_TYPE_CTL:
1867 vap->iv_stats.is_rx_ctl++;
1868 IEEE80211_NODE_STAT(ni, rx_ctrl);
1871 IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
1872 wh, "bad", "frame type 0x%x", type);
1873 /* should not come here */
1877 IFNET_STAT_INC(ifp, ierrors, 1);
1880 if (need_tap && ieee80211_radiotap_active_vap(vap))
1881 ieee80211_radiotap_rx(vap, m);
1891 mesh_recv_mgmt(struct ieee80211_node *ni, struct mbuf *m0, int subtype,
1894 struct ieee80211vap *vap = ni->ni_vap;
1895 struct ieee80211_mesh_state *ms = vap->iv_mesh;
1896 struct ieee80211com *ic = ni->ni_ic;
1897 struct ieee80211_frame *wh;
1898 struct ieee80211_mesh_route *rt;
1899 uint8_t *frm, *efrm;
1901 wh = mtod(m0, struct ieee80211_frame *);
1902 frm = (uint8_t *)&wh[1];
1903 efrm = mtod(m0, uint8_t *) + m0->m_len;
1905 case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
1906 case IEEE80211_FC0_SUBTYPE_BEACON:
1908 struct ieee80211_scanparams scan;
1910 * We process beacon/probe response
1911 * frames to discover neighbors.
1913 if (ieee80211_parse_beacon(ni, m0, &scan) != 0)
1916 * Count frame now that we know it's to be processed.
1918 if (subtype == IEEE80211_FC0_SUBTYPE_BEACON) {
1919 vap->iv_stats.is_rx_beacon++; /* XXX remove */
1920 IEEE80211_NODE_STAT(ni, rx_beacons);
1922 IEEE80211_NODE_STAT(ni, rx_proberesp);
1924 * If scanning, just pass information to the scan module.
1926 if (ic->ic_flags & IEEE80211_F_SCAN) {
1927 if (ic->ic_flags_ext & IEEE80211_FEXT_PROBECHAN) {
1929 * Actively scanning a channel marked passive;
1930 * send a probe request now that we know there
1931 * is 802.11 traffic present.
1933 * XXX check if the beacon we recv'd gives
1934 * us what we need and suppress the probe req
1936 ieee80211_probe_curchan(vap, 1);
1937 ic->ic_flags_ext &= ~IEEE80211_FEXT_PROBECHAN;
1939 ieee80211_add_scan(vap, &scan, wh,
1944 /* The rest of this code assumes we are running */
1945 if (vap->iv_state != IEEE80211_S_RUN)
1948 * Ignore non-mesh STAs.
1951 (IEEE80211_CAPINFO_ESS|IEEE80211_CAPINFO_IBSS)) ||
1952 scan.meshid == NULL || scan.meshconf == NULL) {
1953 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
1954 wh, "beacon", "%s", "not a mesh sta");
1955 vap->iv_stats.is_mesh_wrongmesh++;
1959 * Ignore STAs for other mesh networks.
1961 if (memcmp(scan.meshid+2, ms->ms_id, ms->ms_idlen) != 0 ||
1962 mesh_verify_meshconf(vap, scan.meshconf)) {
1963 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
1964 wh, "beacon", "%s", "not for our mesh");
1965 vap->iv_stats.is_mesh_wrongmesh++;
1969 * Peer only based on the current ACL policy.
1971 if (vap->iv_acl != NULL && !vap->iv_acl->iac_check(vap, wh)) {
1972 IEEE80211_DISCARD(vap, IEEE80211_MSG_ACL,
1973 wh, NULL, "%s", "disallowed by ACL");
1974 vap->iv_stats.is_rx_acl++;
1978 * Do neighbor discovery.
1980 if (!IEEE80211_ADDR_EQ(wh->i_addr2, ni->ni_macaddr)) {
1982 * Create a new entry in the neighbor table.
1984 ni = ieee80211_add_neighbor(vap, wh, &scan);
1987 * Automatically peer with discovered nodes if possible.
1989 if (ni != vap->iv_bss &&
1990 (ms->ms_flags & IEEE80211_MESHFLAGS_AP)) {
1991 switch (ni->ni_mlstate) {
1992 case IEEE80211_NODE_MESH_IDLE:
1996 /* Wait for backoff callout to reset counter */
1997 if (ni->ni_mlhcnt >= ieee80211_mesh_maxholding)
2000 ni->ni_mlpid = mesh_generateid(vap);
2001 if (ni->ni_mlpid == 0)
2003 mesh_linkchange(ni, IEEE80211_NODE_MESH_OPENSNT);
2004 args[0] = ni->ni_mlpid;
2005 ieee80211_send_action(ni,
2006 IEEE80211_ACTION_CAT_SELF_PROT,
2007 IEEE80211_ACTION_MESHPEERING_OPEN, args);
2009 mesh_peer_timeout_setup(ni);
2012 case IEEE80211_NODE_MESH_ESTABLISHED:
2015 * Valid beacon from a peer mesh STA
2018 rt = ieee80211_mesh_rt_find(vap, wh->i_addr2);
2020 ieee80211_mesh_rt_update(rt,
2022 ms->ms_ppath->mpp_inact));
2032 case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
2034 uint8_t *ssid, *meshid, *rates, *xrates;
2037 if (vap->iv_state != IEEE80211_S_RUN) {
2038 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
2039 wh, NULL, "wrong state %s",
2040 ieee80211_state_name[vap->iv_state]);
2041 vap->iv_stats.is_rx_mgtdiscard++;
2044 if (IEEE80211_IS_MULTICAST(wh->i_addr2)) {
2045 /* frame must be directed */
2046 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
2047 wh, NULL, "%s", "not unicast");
2048 vap->iv_stats.is_rx_mgtdiscard++; /* XXX stat */
2052 * prreq frame format
2054 * [tlv] supported rates
2055 * [tlv] extended supported rates
2058 ssid = meshid = rates = xrates = NULL;
2060 while (efrm - frm > 1) {
2061 IEEE80211_VERIFY_LENGTH(efrm - frm, frm[1] + 2, return);
2063 case IEEE80211_ELEMID_SSID:
2066 case IEEE80211_ELEMID_RATES:
2069 case IEEE80211_ELEMID_XRATES:
2072 case IEEE80211_ELEMID_MESHID:
2078 IEEE80211_VERIFY_ELEMENT(ssid, IEEE80211_NWID_LEN, return);
2079 IEEE80211_VERIFY_ELEMENT(rates, IEEE80211_RATE_MAXSIZE, return);
2081 IEEE80211_VERIFY_ELEMENT(xrates,
2082 IEEE80211_RATE_MAXSIZE - rates[1], return);
2083 if (meshid != NULL) {
2084 IEEE80211_VERIFY_ELEMENT(meshid,
2085 IEEE80211_MESHID_LEN, return);
2086 /* NB: meshid, not ssid */
2087 IEEE80211_VERIFY_SSID(vap->iv_bss, meshid, return);
2090 /* XXX find a better class or define it's own */
2091 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_INPUT, wh->i_addr2,
2092 "%s", "recv probe req");
2094 * Some legacy 11b clients cannot hack a complete
2095 * probe response frame. When the request includes
2096 * only a bare-bones rate set, communicate this to
2097 * the transmit side.
2099 ieee80211_send_proberesp(vap, wh->i_addr2, 0);
2103 case IEEE80211_FC0_SUBTYPE_ACTION:
2104 case IEEE80211_FC0_SUBTYPE_ACTION_NOACK:
2105 if (ni == vap->iv_bss) {
2106 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
2107 wh, NULL, "%s", "unknown node");
2108 vap->iv_stats.is_rx_mgtdiscard++;
2109 } else if (!IEEE80211_ADDR_EQ(vap->iv_myaddr, wh->i_addr1) &&
2110 !IEEE80211_IS_MULTICAST(wh->i_addr1)) {
2111 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
2112 wh, NULL, "%s", "not for us");
2113 vap->iv_stats.is_rx_mgtdiscard++;
2114 } else if (vap->iv_state != IEEE80211_S_RUN) {
2115 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
2116 wh, NULL, "wrong state %s",
2117 ieee80211_state_name[vap->iv_state]);
2118 vap->iv_stats.is_rx_mgtdiscard++;
2120 if (ieee80211_parse_action(ni, m0) == 0)
2121 (void)ic->ic_recv_action(ni, wh, frm, efrm);
2125 case IEEE80211_FC0_SUBTYPE_ASSOC_REQ:
2126 case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
2127 case IEEE80211_FC0_SUBTYPE_REASSOC_REQ:
2128 case IEEE80211_FC0_SUBTYPE_REASSOC_RESP:
2129 case IEEE80211_FC0_SUBTYPE_ATIM:
2130 case IEEE80211_FC0_SUBTYPE_DISASSOC:
2131 case IEEE80211_FC0_SUBTYPE_AUTH:
2132 case IEEE80211_FC0_SUBTYPE_DEAUTH:
2133 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
2134 wh, NULL, "%s", "not handled");
2135 vap->iv_stats.is_rx_mgtdiscard++;
2139 IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
2140 wh, "mgt", "subtype 0x%x not handled", subtype);
2141 vap->iv_stats.is_rx_badsubtype++;
2147 mesh_recv_ctl(struct ieee80211_node *ni, struct mbuf *m, int subtype)
2151 case IEEE80211_FC0_SUBTYPE_BAR:
2152 ieee80211_recv_bar(ni, m);
2158 * Parse meshpeering action ie's for MPM frames
2160 static const struct ieee80211_meshpeer_ie *
2161 mesh_parse_meshpeering_action(struct ieee80211_node *ni,
2162 const struct ieee80211_frame *wh, /* XXX for VERIFY_LENGTH */
2163 const uint8_t *frm, const uint8_t *efrm,
2164 struct ieee80211_meshpeer_ie *mp, uint8_t subtype)
2166 struct ieee80211vap *vap = ni->ni_vap;
2167 const struct ieee80211_meshpeer_ie *mpie;
2169 const uint8_t *meshid, *meshconf, *meshpeer;
2170 uint8_t sendclose = 0; /* 1 = MPM frame rejected, close will be sent */
2172 meshid = meshconf = meshpeer = NULL;
2173 while (efrm - frm > 1) {
2174 IEEE80211_VERIFY_LENGTH(efrm - frm, frm[1] + 2, return NULL);
2176 case IEEE80211_ELEMID_MESHID:
2179 case IEEE80211_ELEMID_MESHCONF:
2182 case IEEE80211_ELEMID_MESHPEER:
2184 mpie = (const struct ieee80211_meshpeer_ie *) frm;
2185 memset(mp, 0, sizeof(*mp));
2186 mp->peer_len = mpie->peer_len;
2187 mp->peer_proto = LE_READ_2(&mpie->peer_proto);
2188 mp->peer_llinkid = LE_READ_2(&mpie->peer_llinkid);
2190 case IEEE80211_ACTION_MESHPEERING_CONFIRM:
2192 LE_READ_2(&mpie->peer_linkid);
2194 case IEEE80211_ACTION_MESHPEERING_CLOSE:
2195 /* NB: peer link ID is optional */
2196 if (mpie->peer_len ==
2197 (IEEE80211_MPM_BASE_SZ + 2)) {
2198 mp->peer_linkid = 0;
2200 LE_READ_2(&mpie->peer_linkid);
2203 LE_READ_2(&mpie->peer_linkid);
2205 LE_READ_2(&mpie->peer_rcode);
2215 * Verify the contents of the frame.
2216 * If it fails validation, close the peer link.
2218 if (mesh_verify_meshpeer(vap, subtype, (const uint8_t *)mp)) {
2220 IEEE80211_DISCARD(vap,
2221 IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH,
2222 wh, NULL, "%s", "MPM validation failed");
2225 /* If meshid is not the same reject any frames type. */
2226 if (sendclose == 0 && mesh_verify_meshid(vap, meshid)) {
2228 IEEE80211_DISCARD(vap,
2229 IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH,
2230 wh, NULL, "%s", "not for our mesh");
2231 if (subtype == IEEE80211_ACTION_MESHPEERING_CLOSE) {
2233 * Standard not clear about this, if we dont ignore
2234 * there will be an endless loop between nodes sending
2235 * CLOSE frames between each other with wrong meshid.
2236 * Discard and timers will bring FSM to IDLE state.
2243 * Close frames are accepted if meshid is the same.
2244 * Verify the other two types.
2246 if (sendclose == 0 && subtype != IEEE80211_ACTION_MESHPEERING_CLOSE &&
2247 mesh_verify_meshconf(vap, meshconf)) {
2249 IEEE80211_DISCARD(vap,
2250 IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH,
2251 wh, NULL, "%s", "configuration missmatch");
2255 vap->iv_stats.is_rx_mgtdiscard++;
2256 switch (ni->ni_mlstate) {
2257 case IEEE80211_NODE_MESH_IDLE:
2258 case IEEE80211_NODE_MESH_ESTABLISHED:
2259 case IEEE80211_NODE_MESH_HOLDING:
2262 case IEEE80211_NODE_MESH_OPENSNT:
2263 case IEEE80211_NODE_MESH_OPENRCV:
2264 case IEEE80211_NODE_MESH_CONFIRMRCV:
2265 args[0] = ni->ni_mlpid;
2266 args[1] = ni->ni_mllid;
2267 /* Reason codes for rejection */
2269 case IEEE80211_ACTION_MESHPEERING_OPEN:
2270 args[2] = IEEE80211_REASON_MESH_CPVIOLATION;
2272 case IEEE80211_ACTION_MESHPEERING_CONFIRM:
2273 args[2] = IEEE80211_REASON_MESH_INCONS_PARAMS;
2276 ieee80211_send_action(ni,
2277 IEEE80211_ACTION_CAT_SELF_PROT,
2278 IEEE80211_ACTION_MESHPEERING_CLOSE,
2280 mesh_linkchange(ni, IEEE80211_NODE_MESH_HOLDING);
2281 mesh_peer_timeout_setup(ni);
2287 return (const struct ieee80211_meshpeer_ie *) mp;
2291 mesh_recv_action_meshpeering_open(struct ieee80211_node *ni,
2292 const struct ieee80211_frame *wh,
2293 const uint8_t *frm, const uint8_t *efrm)
2295 struct ieee80211vap *vap = ni->ni_vap;
2296 struct ieee80211_mesh_state *ms = vap->iv_mesh;
2297 struct ieee80211_meshpeer_ie ie;
2298 const struct ieee80211_meshpeer_ie *meshpeer;
2301 /* +2+2 for action + code + capabilites */
2302 meshpeer = mesh_parse_meshpeering_action(ni, wh, frm+2+2, efrm, &ie,
2303 IEEE80211_ACTION_MESHPEERING_OPEN);
2304 if (meshpeer == NULL) {
2309 IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH, ni,
2310 "recv PEER OPEN, lid 0x%x", meshpeer->peer_llinkid);
2312 switch (ni->ni_mlstate) {
2313 case IEEE80211_NODE_MESH_IDLE:
2314 /* Reject open request if reached our maximum neighbor count */
2315 if (ms->ms_neighbors >= IEEE80211_MESH_MAX_NEIGHBORS) {
2316 args[0] = meshpeer->peer_llinkid;
2318 args[2] = IEEE80211_REASON_MESH_MAX_PEERS;
2319 ieee80211_send_action(ni,
2320 IEEE80211_ACTION_CAT_SELF_PROT,
2321 IEEE80211_ACTION_MESHPEERING_CLOSE,
2323 /* stay in IDLE state */
2326 /* Open frame accepted */
2327 mesh_linkchange(ni, IEEE80211_NODE_MESH_OPENRCV);
2328 ni->ni_mllid = meshpeer->peer_llinkid;
2329 ni->ni_mlpid = mesh_generateid(vap);
2330 if (ni->ni_mlpid == 0)
2332 args[0] = ni->ni_mlpid;
2333 /* Announce we're open too... */
2334 ieee80211_send_action(ni,
2335 IEEE80211_ACTION_CAT_SELF_PROT,
2336 IEEE80211_ACTION_MESHPEERING_OPEN, args);
2337 /* ...and confirm the link. */
2338 args[0] = ni->ni_mlpid;
2339 args[1] = ni->ni_mllid;
2340 ieee80211_send_action(ni,
2341 IEEE80211_ACTION_CAT_SELF_PROT,
2342 IEEE80211_ACTION_MESHPEERING_CONFIRM,
2344 mesh_peer_timeout_setup(ni);
2346 case IEEE80211_NODE_MESH_OPENRCV:
2348 if (ni->ni_mllid != meshpeer->peer_llinkid) {
2349 args[0] = ni->ni_mllid;
2350 args[1] = ni->ni_mlpid;
2351 args[2] = IEEE80211_REASON_PEER_LINK_CANCELED;
2352 ieee80211_send_action(ni,
2353 IEEE80211_ACTION_CAT_SELF_PROT,
2354 IEEE80211_ACTION_MESHPEERING_CLOSE,
2356 mesh_linkchange(ni, IEEE80211_NODE_MESH_HOLDING);
2357 mesh_peer_timeout_setup(ni);
2360 /* Duplicate open, confirm again. */
2361 args[0] = ni->ni_mlpid;
2362 args[1] = ni->ni_mllid;
2363 ieee80211_send_action(ni,
2364 IEEE80211_ACTION_CAT_SELF_PROT,
2365 IEEE80211_ACTION_MESHPEERING_CONFIRM,
2368 case IEEE80211_NODE_MESH_OPENSNT:
2369 ni->ni_mllid = meshpeer->peer_llinkid;
2370 mesh_linkchange(ni, IEEE80211_NODE_MESH_OPENRCV);
2371 args[0] = ni->ni_mlpid;
2372 args[1] = ni->ni_mllid;
2373 ieee80211_send_action(ni,
2374 IEEE80211_ACTION_CAT_SELF_PROT,
2375 IEEE80211_ACTION_MESHPEERING_CONFIRM,
2377 /* NB: don't setup/clear any timeout */
2379 case IEEE80211_NODE_MESH_CONFIRMRCV:
2380 if (ni->ni_mlpid != meshpeer->peer_linkid ||
2381 ni->ni_mllid != meshpeer->peer_llinkid) {
2382 args[0] = ni->ni_mlpid;
2383 args[1] = ni->ni_mllid;
2384 args[2] = IEEE80211_REASON_PEER_LINK_CANCELED;
2385 ieee80211_send_action(ni,
2386 IEEE80211_ACTION_CAT_SELF_PROT,
2387 IEEE80211_ACTION_MESHPEERING_CLOSE,
2390 IEEE80211_NODE_MESH_HOLDING);
2391 mesh_peer_timeout_setup(ni);
2394 mesh_linkchange(ni, IEEE80211_NODE_MESH_ESTABLISHED);
2395 ni->ni_mllid = meshpeer->peer_llinkid;
2396 args[0] = ni->ni_mlpid;
2397 args[1] = ni->ni_mllid;
2398 ieee80211_send_action(ni,
2399 IEEE80211_ACTION_CAT_SELF_PROT,
2400 IEEE80211_ACTION_MESHPEERING_CONFIRM,
2402 mesh_peer_timeout_stop(ni);
2404 case IEEE80211_NODE_MESH_ESTABLISHED:
2405 if (ni->ni_mllid != meshpeer->peer_llinkid) {
2406 args[0] = ni->ni_mllid;
2407 args[1] = ni->ni_mlpid;
2408 args[2] = IEEE80211_REASON_PEER_LINK_CANCELED;
2409 ieee80211_send_action(ni,
2410 IEEE80211_ACTION_CAT_SELF_PROT,
2411 IEEE80211_ACTION_MESHPEERING_CLOSE,
2413 mesh_linkchange(ni, IEEE80211_NODE_MESH_HOLDING);
2414 mesh_peer_timeout_setup(ni);
2417 args[0] = ni->ni_mlpid;
2418 args[1] = ni->ni_mllid;
2419 ieee80211_send_action(ni,
2420 IEEE80211_ACTION_CAT_SELF_PROT,
2421 IEEE80211_ACTION_MESHPEERING_CONFIRM,
2424 case IEEE80211_NODE_MESH_HOLDING:
2425 args[0] = ni->ni_mlpid;
2426 args[1] = meshpeer->peer_llinkid;
2427 /* Standard not clear about what the reaason code should be */
2428 args[2] = IEEE80211_REASON_PEER_LINK_CANCELED;
2429 ieee80211_send_action(ni,
2430 IEEE80211_ACTION_CAT_SELF_PROT,
2431 IEEE80211_ACTION_MESHPEERING_CLOSE,
2439 mesh_recv_action_meshpeering_confirm(struct ieee80211_node *ni,
2440 const struct ieee80211_frame *wh,
2441 const uint8_t *frm, const uint8_t *efrm)
2443 struct ieee80211vap *vap = ni->ni_vap;
2444 struct ieee80211_meshpeer_ie ie;
2445 const struct ieee80211_meshpeer_ie *meshpeer;
2448 /* +2+2+2+2 for action + code + capabilites + status code + AID */
2449 meshpeer = mesh_parse_meshpeering_action(ni, wh, frm+2+2+2+2, efrm, &ie,
2450 IEEE80211_ACTION_MESHPEERING_CONFIRM);
2451 if (meshpeer == NULL) {
2455 IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH, ni,
2456 "recv PEER CONFIRM, local id 0x%x, peer id 0x%x",
2457 meshpeer->peer_llinkid, meshpeer->peer_linkid);
2459 switch (ni->ni_mlstate) {
2460 case IEEE80211_NODE_MESH_OPENRCV:
2461 mesh_linkchange(ni, IEEE80211_NODE_MESH_ESTABLISHED);
2462 mesh_peer_timeout_stop(ni);
2464 case IEEE80211_NODE_MESH_OPENSNT:
2465 mesh_linkchange(ni, IEEE80211_NODE_MESH_CONFIRMRCV);
2466 mesh_peer_timeout_setup(ni);
2468 case IEEE80211_NODE_MESH_HOLDING:
2469 args[0] = ni->ni_mlpid;
2470 args[1] = meshpeer->peer_llinkid;
2471 /* Standard not clear about what the reaason code should be */
2472 args[2] = IEEE80211_REASON_PEER_LINK_CANCELED;
2473 ieee80211_send_action(ni,
2474 IEEE80211_ACTION_CAT_SELF_PROT,
2475 IEEE80211_ACTION_MESHPEERING_CLOSE,
2478 case IEEE80211_NODE_MESH_CONFIRMRCV:
2479 if (ni->ni_mllid != meshpeer->peer_llinkid) {
2480 args[0] = ni->ni_mlpid;
2481 args[1] = ni->ni_mllid;
2482 args[2] = IEEE80211_REASON_PEER_LINK_CANCELED;
2483 ieee80211_send_action(ni,
2484 IEEE80211_ACTION_CAT_SELF_PROT,
2485 IEEE80211_ACTION_MESHPEERING_CLOSE,
2487 mesh_linkchange(ni, IEEE80211_NODE_MESH_HOLDING);
2488 mesh_peer_timeout_setup(ni);
2492 IEEE80211_DISCARD(vap,
2493 IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH,
2494 wh, NULL, "received confirm in invalid state %d",
2496 vap->iv_stats.is_rx_mgtdiscard++;
2503 mesh_recv_action_meshpeering_close(struct ieee80211_node *ni,
2504 const struct ieee80211_frame *wh,
2505 const uint8_t *frm, const uint8_t *efrm)
2507 struct ieee80211_meshpeer_ie ie;
2508 const struct ieee80211_meshpeer_ie *meshpeer;
2511 /* +2 for action + code */
2512 meshpeer = mesh_parse_meshpeering_action(ni, wh, frm+2, efrm, &ie,
2513 IEEE80211_ACTION_MESHPEERING_CLOSE);
2514 if (meshpeer == NULL) {
2519 * XXX: check reason code, for example we could receive
2520 * IEEE80211_REASON_MESH_MAX_PEERS then we should not attempt
2524 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH,
2525 ni, "%s", "recv PEER CLOSE");
2527 switch (ni->ni_mlstate) {
2528 case IEEE80211_NODE_MESH_IDLE:
2531 case IEEE80211_NODE_MESH_OPENRCV:
2532 case IEEE80211_NODE_MESH_OPENSNT:
2533 case IEEE80211_NODE_MESH_CONFIRMRCV:
2534 case IEEE80211_NODE_MESH_ESTABLISHED:
2535 args[0] = ni->ni_mlpid;
2536 args[1] = ni->ni_mllid;
2537 args[2] = IEEE80211_REASON_MESH_CLOSE_RCVD;
2538 ieee80211_send_action(ni,
2539 IEEE80211_ACTION_CAT_SELF_PROT,
2540 IEEE80211_ACTION_MESHPEERING_CLOSE,
2542 mesh_linkchange(ni, IEEE80211_NODE_MESH_HOLDING);
2543 mesh_peer_timeout_setup(ni);
2545 case IEEE80211_NODE_MESH_HOLDING:
2546 mesh_linkchange(ni, IEEE80211_NODE_MESH_IDLE);
2547 mesh_peer_timeout_stop(ni);
2554 * Link Metric handling.
2557 mesh_recv_action_meshlmetric(struct ieee80211_node *ni,
2558 const struct ieee80211_frame *wh,
2559 const uint8_t *frm, const uint8_t *efrm)
2561 const struct ieee80211_meshlmetric_ie *ie =
2562 (const struct ieee80211_meshlmetric_ie *)
2563 (frm+2); /* action + code */
2564 struct ieee80211_meshlmetric_ie lm_rep;
2566 if (ie->lm_flags & IEEE80211_MESH_LMETRIC_FLAGS_REQ) {
2567 lm_rep.lm_flags = 0;
2568 lm_rep.lm_metric = mesh_airtime_calc(ni);
2569 ieee80211_send_action(ni,
2570 IEEE80211_ACTION_CAT_MESH,
2571 IEEE80211_ACTION_MESH_LMETRIC,
2574 /* XXX: else do nothing for now */
2579 * Parse meshgate action ie's for GANN frames.
2580 * Returns -1 if parsing fails, otherwise 0.
2583 mesh_parse_meshgate_action(struct ieee80211_node *ni,
2584 const struct ieee80211_frame *wh, /* XXX for VERIFY_LENGTH */
2585 struct ieee80211_meshgann_ie *ie, const uint8_t *frm, const uint8_t *efrm)
2587 struct ieee80211vap *vap = ni->ni_vap;
2588 const struct ieee80211_meshgann_ie *gannie;
2590 while (efrm - frm > 1) {
2591 IEEE80211_VERIFY_LENGTH(efrm - frm, frm[1] + 2, return -1);
2593 case IEEE80211_ELEMID_MESHGANN:
2594 gannie = (const struct ieee80211_meshgann_ie *) frm;
2595 memset(ie, 0, sizeof(*ie));
2596 ie->gann_ie = gannie->gann_ie;
2597 ie->gann_len = gannie->gann_len;
2598 ie->gann_flags = gannie->gann_flags;
2599 ie->gann_hopcount = gannie->gann_hopcount;
2600 ie->gann_ttl = gannie->gann_ttl;
2601 IEEE80211_ADDR_COPY(ie->gann_addr, gannie->gann_addr);
2602 ie->gann_seq = LE_READ_4(&gannie->gann_seq);
2603 ie->gann_interval = LE_READ_2(&gannie->gann_interval);
2613 * Mesh Gate Announcement handling.
2616 mesh_recv_action_meshgate(struct ieee80211_node *ni,
2617 const struct ieee80211_frame *wh,
2618 const uint8_t *frm, const uint8_t *efrm)
2620 struct ieee80211vap *vap = ni->ni_vap;
2621 struct ieee80211_mesh_state *ms = vap->iv_mesh;
2622 struct ieee80211_mesh_gate_route *gr, *next;
2623 struct ieee80211_mesh_route *rt_gate;
2624 struct ieee80211_meshgann_ie pgann;
2625 struct ieee80211_meshgann_ie ie;
2628 /* +2 for action + code */
2629 if (mesh_parse_meshgate_action(ni, wh, &ie, frm+2, efrm) != 0) {
2630 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_MESH,
2631 ni->ni_macaddr, NULL, "%s",
2632 "GANN parsing failed");
2633 vap->iv_stats.is_rx_mgtdiscard++;
2637 if (IEEE80211_ADDR_EQ(vap->iv_myaddr, ie.gann_addr))
2640 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH, ni->ni_macaddr,
2641 "received GANN, meshgate: %6D (seq %u)", ie.gann_addr, ":",
2647 TAILQ_FOREACH_SAFE(gr, &ms->ms_known_gates, gr_next, next) {
2648 if (!IEEE80211_ADDR_EQ(gr->gr_addr, ie.gann_addr))
2650 if (ie.gann_seq <= gr->gr_lastseq) {
2651 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_MESH,
2652 ni->ni_macaddr, NULL,
2653 "GANN old seqno %u <= %u",
2654 ie.gann_seq, gr->gr_lastseq);
2658 /* corresponding mesh gate found & GANN accepted */
2664 /* this GANN is from a new mesh Gate add it to known table. */
2665 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH, ie.gann_addr,
2666 "stored new GANN information, seq %u.", ie.gann_seq);
2667 gr = kmalloc(ALIGN(sizeof(struct ieee80211_mesh_gate_route)),
2668 M_80211_MESH_GT_RT, M_INTWAIT | M_ZERO);
2669 IEEE80211_ADDR_COPY(gr->gr_addr, ie.gann_addr);
2670 TAILQ_INSERT_TAIL(&ms->ms_known_gates, gr, gr_next);
2672 gr->gr_lastseq = ie.gann_seq;
2674 /* check if we have a path to this gate */
2675 rt_gate = mesh_rt_find_locked(ms, gr->gr_addr);
2676 if (rt_gate != NULL &&
2677 rt_gate->rt_flags & IEEE80211_MESHRT_FLAGS_VALID) {
2678 gr->gr_route = rt_gate;
2679 rt_gate->rt_flags |= IEEE80211_MESHRT_FLAGS_GATE;
2684 /* popagate only if decremented ttl >= 1 && forwarding is enabled */
2685 if ((ie.gann_ttl - 1) < 1 && !(ms->ms_flags & IEEE80211_MESHFLAGS_FWD))
2687 pgann.gann_flags = ie.gann_flags; /* Reserved */
2688 pgann.gann_hopcount = ie.gann_hopcount + 1;
2689 pgann.gann_ttl = ie.gann_ttl - 1;
2690 IEEE80211_ADDR_COPY(pgann.gann_addr, ie.gann_addr);
2691 pgann.gann_seq = ie.gann_seq;
2692 pgann.gann_interval = ie.gann_interval;
2694 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH, ie.gann_addr,
2695 "%s", "propagate GANN");
2697 ieee80211_send_action(vap->iv_bss, IEEE80211_ACTION_CAT_MESH,
2698 IEEE80211_ACTION_MESH_GANN, &pgann);
2704 mesh_send_action(struct ieee80211_node *ni,
2705 const uint8_t sa[IEEE80211_ADDR_LEN],
2706 const uint8_t da[IEEE80211_ADDR_LEN],
2709 struct ieee80211vap *vap = ni->ni_vap;
2710 struct ieee80211com *ic = ni->ni_ic;
2711 struct ieee80211_bpf_params params;
2712 struct ieee80211_frame *wh;
2715 KASSERT(ni != NULL, ("null node"));
2717 if (vap->iv_state == IEEE80211_S_CAC) {
2718 IEEE80211_NOTE(vap, IEEE80211_MSG_OUTPUT, ni,
2719 "block %s frame in CAC state", "Mesh action");
2720 vap->iv_stats.is_tx_badstate++;
2721 ieee80211_free_node(ni);
2723 return EIO; /* XXX */
2726 M_PREPEND(m, sizeof(struct ieee80211_frame), M_NOWAIT);
2728 ieee80211_free_node(ni);
2732 IEEE80211_TX_LOCK(ic);
2733 wh = mtod(m, struct ieee80211_frame *);
2734 ieee80211_send_setup(ni, m,
2735 IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_ACTION,
2736 IEEE80211_NONQOS_TID, sa, da, sa);
2737 m->m_flags |= M_ENCAP; /* mark encapsulated */
2739 memset(¶ms, 0, sizeof(params));
2740 params.ibp_pri = WME_AC_VO;
2741 params.ibp_rate0 = ni->ni_txparms->mgmtrate;
2742 if (IEEE80211_IS_MULTICAST(da))
2743 params.ibp_try0 = 1;
2745 params.ibp_try0 = ni->ni_txparms->maxretry;
2746 params.ibp_power = ni->ni_txpower;
2748 IEEE80211_NODE_STAT(ni, tx_mgmt);
2750 ret = ieee80211_raw_output(vap, ni, m, ¶ms);
2751 IEEE80211_TX_UNLOCK(ic);
2755 #define ADDSHORT(frm, v) do { \
2756 frm[0] = (v) & 0xff; \
2757 frm[1] = (v) >> 8; \
2760 #define ADDWORD(frm, v) do { \
2761 frm[0] = (v) & 0xff; \
2762 frm[1] = ((v) >> 8) & 0xff; \
2763 frm[2] = ((v) >> 16) & 0xff; \
2764 frm[3] = ((v) >> 24) & 0xff; \
2769 mesh_send_action_meshpeering_open(struct ieee80211_node *ni,
2770 int category, int action, void *args0)
2772 struct ieee80211vap *vap = ni->ni_vap;
2773 struct ieee80211com *ic = ni->ni_ic;
2774 uint16_t *args = args0;
2775 const struct ieee80211_rateset *rs;
2779 IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH, ni,
2780 "send PEER OPEN action: localid 0x%x", args[0]);
2782 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
2783 "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", __func__, __LINE__,
2784 ni, ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)+1);
2785 ieee80211_ref_node(ni);
2787 m = ieee80211_getmgtframe(&frm,
2788 ic->ic_headroom + sizeof(struct ieee80211_frame),
2789 sizeof(uint16_t) /* action+category */
2790 + sizeof(uint16_t) /* capabilites */
2791 + 2 + IEEE80211_RATE_SIZE
2792 + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
2793 + 2 + IEEE80211_MESHID_LEN
2794 + sizeof(struct ieee80211_meshconf_ie)
2795 + sizeof(struct ieee80211_meshpeer_ie)
2799 * mesh peer open action frame format:
2807 * [tlv] mesh peer link mgmt
2811 ADDSHORT(frm, ieee80211_getcapinfo(vap, ni->ni_chan));
2812 rs = ieee80211_get_suprates(ic, ic->ic_curchan);
2813 frm = ieee80211_add_rates(frm, rs);
2814 frm = ieee80211_add_xrates(frm, rs);
2815 frm = ieee80211_add_meshid(frm, vap);
2816 frm = ieee80211_add_meshconf(frm, vap);
2817 frm = ieee80211_add_meshpeer(frm, IEEE80211_ACTION_MESHPEERING_OPEN,
2819 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2820 return mesh_send_action(ni, vap->iv_myaddr, ni->ni_macaddr, m);
2822 vap->iv_stats.is_tx_nobuf++;
2823 ieee80211_free_node(ni);
2829 mesh_send_action_meshpeering_confirm(struct ieee80211_node *ni,
2830 int category, int action, void *args0)
2832 struct ieee80211vap *vap = ni->ni_vap;
2833 struct ieee80211com *ic = ni->ni_ic;
2834 uint16_t *args = args0;
2835 const struct ieee80211_rateset *rs;
2839 IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH, ni,
2840 "send PEER CONFIRM action: localid 0x%x, peerid 0x%x",
2843 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
2844 "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", __func__, __LINE__,
2845 ni, ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)+1);
2846 ieee80211_ref_node(ni);
2848 m = ieee80211_getmgtframe(&frm,
2849 ic->ic_headroom + sizeof(struct ieee80211_frame),
2850 sizeof(uint16_t) /* action+category */
2851 + sizeof(uint16_t) /* capabilites */
2852 + sizeof(uint16_t) /* status code */
2853 + sizeof(uint16_t) /* AID */
2854 + 2 + IEEE80211_RATE_SIZE
2855 + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
2856 + 2 + IEEE80211_MESHID_LEN
2857 + sizeof(struct ieee80211_meshconf_ie)
2858 + sizeof(struct ieee80211_meshpeer_ie)
2862 * mesh peer confirm action frame format:
2867 * [2] association id (peer ID)
2872 * [tlv] mesh peer link mgmt
2876 ADDSHORT(frm, ieee80211_getcapinfo(vap, ni->ni_chan));
2877 ADDSHORT(frm, 0); /* status code */
2878 ADDSHORT(frm, args[1]); /* AID */
2879 rs = ieee80211_get_suprates(ic, ic->ic_curchan);
2880 frm = ieee80211_add_rates(frm, rs);
2881 frm = ieee80211_add_xrates(frm, rs);
2882 frm = ieee80211_add_meshid(frm, vap);
2883 frm = ieee80211_add_meshconf(frm, vap);
2884 frm = ieee80211_add_meshpeer(frm,
2885 IEEE80211_ACTION_MESHPEERING_CONFIRM,
2886 args[0], args[1], 0);
2887 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2888 return mesh_send_action(ni, vap->iv_myaddr, ni->ni_macaddr, m);
2890 vap->iv_stats.is_tx_nobuf++;
2891 ieee80211_free_node(ni);
2897 mesh_send_action_meshpeering_close(struct ieee80211_node *ni,
2898 int category, int action, void *args0)
2900 struct ieee80211vap *vap = ni->ni_vap;
2901 struct ieee80211com *ic = ni->ni_ic;
2902 uint16_t *args = args0;
2906 IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH, ni,
2907 "send PEER CLOSE action: localid 0x%x, peerid 0x%x reason %d",
2908 args[0], args[1], args[2]);
2910 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
2911 "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", __func__, __LINE__,
2912 ni, ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)+1);
2913 ieee80211_ref_node(ni);
2915 m = ieee80211_getmgtframe(&frm,
2916 ic->ic_headroom + sizeof(struct ieee80211_frame),
2917 sizeof(uint16_t) /* action+category */
2918 + sizeof(uint16_t) /* reason code */
2919 + 2 + IEEE80211_MESHID_LEN
2920 + sizeof(struct ieee80211_meshpeer_ie)
2924 * mesh peer close action frame format:
2928 * [tlv] mesh peer link mgmt
2932 frm = ieee80211_add_meshid(frm, vap);
2933 frm = ieee80211_add_meshpeer(frm,
2934 IEEE80211_ACTION_MESHPEERING_CLOSE,
2935 args[0], args[1], args[2]);
2936 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2937 return mesh_send_action(ni, vap->iv_myaddr, ni->ni_macaddr, m);
2939 vap->iv_stats.is_tx_nobuf++;
2940 ieee80211_free_node(ni);
2946 mesh_send_action_meshlmetric(struct ieee80211_node *ni,
2947 int category, int action, void *arg0)
2949 struct ieee80211vap *vap = ni->ni_vap;
2950 struct ieee80211com *ic = ni->ni_ic;
2951 struct ieee80211_meshlmetric_ie *ie = arg0;
2955 if (ie->lm_flags & IEEE80211_MESH_LMETRIC_FLAGS_REQ) {
2956 IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH,
2957 ni, "%s", "send LINK METRIC REQUEST action");
2959 IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH,
2960 ni, "send LINK METRIC REPLY action: metric 0x%x",
2963 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
2964 "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", __func__, __LINE__,
2965 ni, ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)+1);
2966 ieee80211_ref_node(ni);
2968 m = ieee80211_getmgtframe(&frm,
2969 ic->ic_headroom + sizeof(struct ieee80211_frame),
2970 sizeof(uint16_t) + /* action+category */
2971 sizeof(struct ieee80211_meshlmetric_ie)
2978 * [tlv] mesh link metric
2982 frm = ieee80211_add_meshlmetric(frm,
2983 ie->lm_flags, ie->lm_metric);
2984 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2985 return mesh_send_action(ni, vap->iv_myaddr, ni->ni_macaddr, m);
2987 vap->iv_stats.is_tx_nobuf++;
2988 ieee80211_free_node(ni);
2994 mesh_send_action_meshgate(struct ieee80211_node *ni,
2995 int category, int action, void *arg0)
2997 struct ieee80211vap *vap = ni->ni_vap;
2998 struct ieee80211com *ic = ni->ni_ic;
2999 struct ieee80211_meshgann_ie *ie = arg0;
3003 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
3004 "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", __func__, __LINE__,
3005 ni, ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)+1);
3006 ieee80211_ref_node(ni);
3008 m = ieee80211_getmgtframe(&frm,
3009 ic->ic_headroom + sizeof(struct ieee80211_frame),
3010 sizeof(uint16_t) + /* action+category */
3011 IEEE80211_MESHGANN_BASE_SZ
3018 * [tlv] mesh gate annoucement
3022 frm = ieee80211_add_meshgate(frm, ie);
3023 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
3024 return mesh_send_action(ni, vap->iv_myaddr, broadcastaddr, m);
3026 vap->iv_stats.is_tx_nobuf++;
3027 ieee80211_free_node(ni);
3033 mesh_peer_timeout_setup(struct ieee80211_node *ni)
3035 switch (ni->ni_mlstate) {
3036 case IEEE80211_NODE_MESH_HOLDING:
3037 ni->ni_mltval = ieee80211_mesh_holdingtimeout;
3039 case IEEE80211_NODE_MESH_CONFIRMRCV:
3040 ni->ni_mltval = ieee80211_mesh_confirmtimeout;
3042 case IEEE80211_NODE_MESH_IDLE:
3046 ni->ni_mltval = ieee80211_mesh_retrytimeout;
3050 callout_reset(&ni->ni_mltimer, ni->ni_mltval,
3051 mesh_peer_timeout_cb, ni);
3055 * Same as above but backoffs timer statisically 50%.
3058 mesh_peer_timeout_backoff(struct ieee80211_node *ni)
3063 ni->ni_mltval += r % ni->ni_mltval;
3064 callout_reset(&ni->ni_mltimer, ni->ni_mltval, mesh_peer_timeout_cb,
3068 static __inline void
3069 mesh_peer_timeout_stop(struct ieee80211_node *ni)
3071 callout_drain(&ni->ni_mltimer);
3075 mesh_peer_backoff_cb(void *arg)
3077 struct ieee80211_node *ni = (struct ieee80211_node *)arg;
3079 /* After backoff timeout, try to peer automatically again. */
3084 * Mesh Peer Link Management FSM timeout handling.
3087 mesh_peer_timeout_cb(void *arg)
3089 struct ieee80211_node *ni = (struct ieee80211_node *)arg;
3092 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_MESH,
3093 ni, "mesh link timeout, state %d, retry counter %d",
3094 ni->ni_mlstate, ni->ni_mlrcnt);
3096 switch (ni->ni_mlstate) {
3097 case IEEE80211_NODE_MESH_IDLE:
3098 case IEEE80211_NODE_MESH_ESTABLISHED:
3100 case IEEE80211_NODE_MESH_OPENSNT:
3101 case IEEE80211_NODE_MESH_OPENRCV:
3102 if (ni->ni_mlrcnt == ieee80211_mesh_maxretries) {
3103 args[0] = ni->ni_mlpid;
3104 args[2] = IEEE80211_REASON_MESH_MAX_RETRIES;
3105 ieee80211_send_action(ni,
3106 IEEE80211_ACTION_CAT_SELF_PROT,
3107 IEEE80211_ACTION_MESHPEERING_CLOSE, args);
3109 mesh_linkchange(ni, IEEE80211_NODE_MESH_HOLDING);
3110 mesh_peer_timeout_setup(ni);
3112 args[0] = ni->ni_mlpid;
3113 ieee80211_send_action(ni,
3114 IEEE80211_ACTION_CAT_SELF_PROT,
3115 IEEE80211_ACTION_MESHPEERING_OPEN, args);
3117 mesh_peer_timeout_backoff(ni);
3120 case IEEE80211_NODE_MESH_CONFIRMRCV:
3121 args[0] = ni->ni_mlpid;
3122 args[2] = IEEE80211_REASON_MESH_CONFIRM_TIMEOUT;
3123 ieee80211_send_action(ni,
3124 IEEE80211_ACTION_CAT_SELF_PROT,
3125 IEEE80211_ACTION_MESHPEERING_CLOSE, args);
3126 mesh_linkchange(ni, IEEE80211_NODE_MESH_HOLDING);
3127 mesh_peer_timeout_setup(ni);
3129 case IEEE80211_NODE_MESH_HOLDING:
3131 if (ni->ni_mlhcnt >= ieee80211_mesh_maxholding)
3132 callout_reset(&ni->ni_mlhtimer,
3133 ieee80211_mesh_backofftimeout,
3134 mesh_peer_backoff_cb, ni);
3135 mesh_linkchange(ni, IEEE80211_NODE_MESH_IDLE);
3141 mesh_verify_meshid(struct ieee80211vap *vap, const uint8_t *ie)
3143 struct ieee80211_mesh_state *ms = vap->iv_mesh;
3145 if (ie == NULL || ie[1] != ms->ms_idlen)
3147 return memcmp(ms->ms_id, ie + 2, ms->ms_idlen);
3151 * Check if we are using the same algorithms for this mesh.
3154 mesh_verify_meshconf(struct ieee80211vap *vap, const uint8_t *ie)
3156 const struct ieee80211_meshconf_ie *meshconf =
3157 (const struct ieee80211_meshconf_ie *) ie;
3158 const struct ieee80211_mesh_state *ms = vap->iv_mesh;
3160 if (meshconf == NULL)
3162 if (meshconf->conf_pselid != ms->ms_ppath->mpp_ie) {
3163 IEEE80211_DPRINTF(vap, IEEE80211_MSG_MESH,
3164 "unknown path selection algorithm: 0x%x\n",
3165 meshconf->conf_pselid);
3168 if (meshconf->conf_pmetid != ms->ms_pmetric->mpm_ie) {
3169 IEEE80211_DPRINTF(vap, IEEE80211_MSG_MESH,
3170 "unknown path metric algorithm: 0x%x\n",
3171 meshconf->conf_pmetid);
3174 if (meshconf->conf_ccid != 0) {
3175 IEEE80211_DPRINTF(vap, IEEE80211_MSG_MESH,
3176 "unknown congestion control algorithm: 0x%x\n",
3177 meshconf->conf_ccid);
3180 if (meshconf->conf_syncid != IEEE80211_MESHCONF_SYNC_NEIGHOFF) {
3181 IEEE80211_DPRINTF(vap, IEEE80211_MSG_MESH,
3182 "unknown sync algorithm: 0x%x\n",
3183 meshconf->conf_syncid);
3186 if (meshconf->conf_authid != 0) {
3187 IEEE80211_DPRINTF(vap, IEEE80211_MSG_MESH,
3188 "unknown auth auth algorithm: 0x%x\n",
3189 meshconf->conf_pselid);
3192 /* Not accepting peers */
3193 if (!(meshconf->conf_cap & IEEE80211_MESHCONF_CAP_AP)) {
3194 IEEE80211_DPRINTF(vap, IEEE80211_MSG_MESH,
3195 "not accepting peers: 0x%x\n", meshconf->conf_cap);
3202 mesh_verify_meshpeer(struct ieee80211vap *vap, uint8_t subtype,
3205 const struct ieee80211_meshpeer_ie *meshpeer =
3206 (const struct ieee80211_meshpeer_ie *) ie;
3208 if (meshpeer == NULL ||
3209 meshpeer->peer_len < IEEE80211_MPM_BASE_SZ ||
3210 meshpeer->peer_len > IEEE80211_MPM_MAX_SZ)
3212 if (meshpeer->peer_proto != IEEE80211_MPPID_MPM) {
3213 IEEE80211_DPRINTF(vap,
3214 IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH,
3215 "Only MPM protocol is supported (proto: 0x%02X)",
3216 meshpeer->peer_proto);
3220 case IEEE80211_ACTION_MESHPEERING_OPEN:
3221 if (meshpeer->peer_len != IEEE80211_MPM_BASE_SZ)
3224 case IEEE80211_ACTION_MESHPEERING_CONFIRM:
3225 if (meshpeer->peer_len != IEEE80211_MPM_BASE_SZ + 2)
3228 case IEEE80211_ACTION_MESHPEERING_CLOSE:
3229 if (meshpeer->peer_len < IEEE80211_MPM_BASE_SZ + 2)
3231 if (meshpeer->peer_len == (IEEE80211_MPM_BASE_SZ + 2) &&
3232 meshpeer->peer_linkid != 0)
3234 if (meshpeer->peer_rcode == 0)
3242 * Add a Mesh ID IE to a frame.
3245 ieee80211_add_meshid(uint8_t *frm, struct ieee80211vap *vap)
3247 struct ieee80211_mesh_state *ms = vap->iv_mesh;
3249 KASSERT(vap->iv_opmode == IEEE80211_M_MBSS, ("not a mbss vap"));
3251 *frm++ = IEEE80211_ELEMID_MESHID;
3252 *frm++ = ms->ms_idlen;
3253 memcpy(frm, ms->ms_id, ms->ms_idlen);
3254 return frm + ms->ms_idlen;
3258 * Add a Mesh Configuration IE to a frame.
3259 * For now just use HWMP routing, Airtime link metric, Null Congestion
3260 * Signaling, Null Sync Protocol and Null Authentication.
3263 ieee80211_add_meshconf(uint8_t *frm, struct ieee80211vap *vap)
3265 const struct ieee80211_mesh_state *ms = vap->iv_mesh;
3268 KASSERT(vap->iv_opmode == IEEE80211_M_MBSS, ("not a MBSS vap"));
3270 *frm++ = IEEE80211_ELEMID_MESHCONF;
3271 *frm++ = IEEE80211_MESH_CONF_SZ;
3272 *frm++ = ms->ms_ppath->mpp_ie; /* path selection */
3273 *frm++ = ms->ms_pmetric->mpm_ie; /* link metric */
3274 *frm++ = IEEE80211_MESHCONF_CC_DISABLED;
3275 *frm++ = IEEE80211_MESHCONF_SYNC_NEIGHOFF;
3276 *frm++ = IEEE80211_MESHCONF_AUTH_DISABLED;
3277 /* NB: set the number of neighbors before the rest */
3278 *frm = (ms->ms_neighbors > IEEE80211_MESH_MAX_NEIGHBORS ?
3279 IEEE80211_MESH_MAX_NEIGHBORS : ms->ms_neighbors) << 1;
3280 if (ms->ms_flags & IEEE80211_MESHFLAGS_GATE)
3281 *frm |= IEEE80211_MESHCONF_FORM_GATE;
3284 if (ms->ms_flags & IEEE80211_MESHFLAGS_AP)
3285 caps |= IEEE80211_MESHCONF_CAP_AP;
3286 if (ms->ms_flags & IEEE80211_MESHFLAGS_FWD)
3287 caps |= IEEE80211_MESHCONF_CAP_FWRD;
3293 * Add a Mesh Peer Management IE to a frame.
3296 ieee80211_add_meshpeer(uint8_t *frm, uint8_t subtype, uint16_t localid,
3297 uint16_t peerid, uint16_t reason)
3300 KASSERT(localid != 0, ("localid == 0"));
3302 *frm++ = IEEE80211_ELEMID_MESHPEER;
3304 case IEEE80211_ACTION_MESHPEERING_OPEN:
3305 *frm++ = IEEE80211_MPM_BASE_SZ; /* length */
3306 ADDSHORT(frm, IEEE80211_MPPID_MPM); /* proto */
3307 ADDSHORT(frm, localid); /* local ID */
3309 case IEEE80211_ACTION_MESHPEERING_CONFIRM:
3310 KASSERT(peerid != 0, ("sending peer confirm without peer id"));
3311 *frm++ = IEEE80211_MPM_BASE_SZ + 2; /* length */
3312 ADDSHORT(frm, IEEE80211_MPPID_MPM); /* proto */
3313 ADDSHORT(frm, localid); /* local ID */
3314 ADDSHORT(frm, peerid); /* peer ID */
3316 case IEEE80211_ACTION_MESHPEERING_CLOSE:
3318 *frm++ = IEEE80211_MPM_MAX_SZ; /* length */
3320 *frm++ = IEEE80211_MPM_BASE_SZ + 2; /* length */
3321 ADDSHORT(frm, IEEE80211_MPPID_MPM); /* proto */
3322 ADDSHORT(frm, localid); /* local ID */
3324 ADDSHORT(frm, peerid); /* peer ID */
3325 ADDSHORT(frm, reason);
3332 * Compute an Airtime Link Metric for the link with this node.
3334 * Based on Draft 3.0 spec (11B.10, p.149).
3337 * Max 802.11s overhead.
3339 #define IEEE80211_MESH_MAXOVERHEAD \
3340 (sizeof(struct ieee80211_qosframe_addr4) \
3341 + sizeof(struct ieee80211_meshcntl_ae10) \
3342 + sizeof(struct llc) \
3343 + IEEE80211_ADDR_LEN \
3344 + IEEE80211_WEP_IVLEN \
3345 + IEEE80211_WEP_KIDLEN \
3346 + IEEE80211_WEP_CRCLEN \
3347 + IEEE80211_WEP_MICLEN \
3348 + IEEE80211_CRC_LEN)
3350 mesh_airtime_calc(struct ieee80211_node *ni)
3353 #define S_FACTOR (2 * M_BITS)
3354 struct ieee80211com *ic = ni->ni_ic;
3355 struct ifnet *ifp = ni->ni_vap->iv_ifp;
3356 static const int nbits = 8192 << M_BITS;
3357 uint32_t overhead, rate, errrate;
3360 /* Time to transmit a frame */
3361 rate = ni->ni_txrate;
3362 overhead = ieee80211_compute_duration(ic->ic_rt,
3363 ifp->if_mtu + IEEE80211_MESH_MAXOVERHEAD, rate, 0) << M_BITS;
3364 /* Error rate in percentage */
3365 /* XXX assuming small failures are ok */
3366 #if defined(__DragonFly__)
3369 IFNET_STAT_GET(ifp, ierrors, icount);
3370 IFNET_STAT_GET(ifp, oerrors, ocount);
3371 errrate = (((ocount + icount) / 100) << M_BITS)
3374 errrate = (((ifp->if_get_counter(ifp, IFCOUNTER_OERRORS) +
3375 ifp->if_get_counter(ifp, IFCOUNTER_IERRORS)) / 100) << M_BITS)
3378 res = (overhead + (nbits / rate)) *
3379 ((1 << S_FACTOR) / ((1 << M_BITS) - errrate));
3381 return (uint32_t)(res >> S_FACTOR);
3387 * Add a Mesh Link Metric report IE to a frame.
3390 ieee80211_add_meshlmetric(uint8_t *frm, uint8_t flags, uint32_t metric)
3392 *frm++ = IEEE80211_ELEMID_MESHLINK;
3395 ADDWORD(frm, metric);
3400 * Add a Mesh Gate Announcement IE to a frame.
3403 ieee80211_add_meshgate(uint8_t *frm, struct ieee80211_meshgann_ie *ie)
3405 *frm++ = IEEE80211_ELEMID_MESHGANN; /* ie */
3406 *frm++ = IEEE80211_MESHGANN_BASE_SZ; /* len */
3407 *frm++ = ie->gann_flags;
3408 *frm++ = ie->gann_hopcount;
3409 *frm++ = ie->gann_ttl;
3410 IEEE80211_ADDR_COPY(frm, ie->gann_addr);
3412 ADDWORD(frm, ie->gann_seq);
3413 ADDSHORT(frm, ie->gann_interval);
3420 * Initialize any mesh-specific node state.
3423 ieee80211_mesh_node_init(struct ieee80211vap *vap, struct ieee80211_node *ni)
3425 ni->ni_flags |= IEEE80211_NODE_QOS;
3426 callout_init_mp(&ni->ni_mltimer);
3427 callout_init_mp(&ni->ni_mlhtimer);
3431 * Cleanup any mesh-specific node state.
3434 ieee80211_mesh_node_cleanup(struct ieee80211_node *ni)
3436 struct ieee80211vap *vap = ni->ni_vap;
3437 struct ieee80211_mesh_state *ms = vap->iv_mesh;
3439 callout_drain(&ni->ni_mltimer);
3440 callout_drain(&ni->ni_mlhtimer);
3441 /* NB: short-circuit callbacks after mesh_vdetach */
3442 if (vap->iv_mesh != NULL)
3443 ms->ms_ppath->mpp_peerdown(ni);
3447 ieee80211_parse_meshid(struct ieee80211_node *ni, const uint8_t *ie)
3449 ni->ni_meshidlen = ie[1];
3450 memcpy(ni->ni_meshid, ie + 2, ie[1]);
3454 * Setup mesh-specific node state on neighbor discovery.
3457 ieee80211_mesh_init_neighbor(struct ieee80211_node *ni,
3458 const struct ieee80211_frame *wh,
3459 const struct ieee80211_scanparams *sp)
3461 ieee80211_parse_meshid(ni, sp->meshid);
3465 ieee80211_mesh_update_beacon(struct ieee80211vap *vap,
3466 struct ieee80211_beacon_offsets *bo)
3468 KASSERT(vap->iv_opmode == IEEE80211_M_MBSS, ("not a MBSS vap"));
3470 if (isset(bo->bo_flags, IEEE80211_BEACON_MESHCONF)) {
3471 (void)ieee80211_add_meshconf(bo->bo_meshconf, vap);
3472 clrbit(bo->bo_flags, IEEE80211_BEACON_MESHCONF);
3477 mesh_ioctl_get80211(struct ieee80211vap *vap, struct ieee80211req *ireq)
3479 struct ieee80211_mesh_state *ms = vap->iv_mesh;
3480 uint8_t tmpmeshid[IEEE80211_NWID_LEN];
3481 struct ieee80211_mesh_route *rt;
3482 struct ieee80211req_mesh_route *imr;
3487 if (vap->iv_opmode != IEEE80211_M_MBSS)
3491 switch (ireq->i_type) {
3492 case IEEE80211_IOC_MESH_ID:
3493 ireq->i_len = ms->ms_idlen;
3494 memcpy(tmpmeshid, ms->ms_id, ireq->i_len);
3495 error = copyout(tmpmeshid, ireq->i_data, ireq->i_len);
3497 case IEEE80211_IOC_MESH_AP:
3498 ireq->i_val = (ms->ms_flags & IEEE80211_MESHFLAGS_AP) != 0;
3500 case IEEE80211_IOC_MESH_FWRD:
3501 ireq->i_val = (ms->ms_flags & IEEE80211_MESHFLAGS_FWD) != 0;
3503 case IEEE80211_IOC_MESH_GATE:
3504 ireq->i_val = (ms->ms_flags & IEEE80211_MESHFLAGS_GATE) != 0;
3506 case IEEE80211_IOC_MESH_TTL:
3507 ireq->i_val = ms->ms_ttl;
3509 case IEEE80211_IOC_MESH_RTCMD:
3510 switch (ireq->i_val) {
3511 case IEEE80211_MESH_RTCMD_LIST:
3514 TAILQ_FOREACH(rt, &ms->ms_routes, rt_next) {
3515 len += sizeof(*imr);
3518 if (len > ireq->i_len || ireq->i_len < sizeof(*imr)) {
3524 p = kmalloc(len, M_TEMP, M_INTWAIT | M_ZERO);
3529 TAILQ_FOREACH(rt, &ms->ms_routes, rt_next) {
3532 imr = (struct ieee80211req_mesh_route *)
3534 IEEE80211_ADDR_COPY(imr->imr_dest,
3536 IEEE80211_ADDR_COPY(imr->imr_nexthop,
3538 imr->imr_metric = rt->rt_metric;
3539 imr->imr_nhops = rt->rt_nhops;
3541 ieee80211_mesh_rt_update(rt, 0);
3542 imr->imr_lastmseq = rt->rt_lastmseq;
3543 imr->imr_flags = rt->rt_flags; /* last */
3544 off += sizeof(*imr);
3547 error = copyout(p, (uint8_t *)ireq->i_data,
3551 case IEEE80211_MESH_RTCMD_FLUSH:
3552 case IEEE80211_MESH_RTCMD_ADD:
3553 case IEEE80211_MESH_RTCMD_DELETE:
3559 case IEEE80211_IOC_MESH_PR_METRIC:
3560 len = strlen(ms->ms_pmetric->mpm_descr);
3561 if (ireq->i_len < len)
3564 error = copyout(ms->ms_pmetric->mpm_descr,
3565 (uint8_t *)ireq->i_data, len);
3567 case IEEE80211_IOC_MESH_PR_PATH:
3568 len = strlen(ms->ms_ppath->mpp_descr);
3569 if (ireq->i_len < len)
3572 error = copyout(ms->ms_ppath->mpp_descr,
3573 (uint8_t *)ireq->i_data, len);
3581 IEEE80211_IOCTL_GET(mesh, mesh_ioctl_get80211);
3584 mesh_ioctl_set80211(struct ieee80211vap *vap, struct ieee80211req *ireq)
3586 struct ieee80211_mesh_state *ms = vap->iv_mesh;
3587 uint8_t tmpmeshid[IEEE80211_NWID_LEN];
3588 uint8_t tmpaddr[IEEE80211_ADDR_LEN];
3589 char tmpproto[IEEE80211_MESH_PROTO_DSZ];
3592 if (vap->iv_opmode != IEEE80211_M_MBSS)
3596 switch (ireq->i_type) {
3597 case IEEE80211_IOC_MESH_ID:
3598 if (ireq->i_val != 0 || ireq->i_len > IEEE80211_MESHID_LEN)
3600 error = copyin(ireq->i_data, tmpmeshid, ireq->i_len);
3603 memset(ms->ms_id, 0, IEEE80211_NWID_LEN);
3604 ms->ms_idlen = ireq->i_len;
3605 memcpy(ms->ms_id, tmpmeshid, ireq->i_len);
3608 case IEEE80211_IOC_MESH_AP:
3610 ms->ms_flags |= IEEE80211_MESHFLAGS_AP;
3612 ms->ms_flags &= ~IEEE80211_MESHFLAGS_AP;
3615 case IEEE80211_IOC_MESH_FWRD:
3617 ms->ms_flags |= IEEE80211_MESHFLAGS_FWD;
3619 ms->ms_flags &= ~IEEE80211_MESHFLAGS_FWD;
3620 mesh_gatemode_setup(vap);
3622 case IEEE80211_IOC_MESH_GATE:
3624 ms->ms_flags |= IEEE80211_MESHFLAGS_GATE;
3626 ms->ms_flags &= ~IEEE80211_MESHFLAGS_GATE;
3628 case IEEE80211_IOC_MESH_TTL:
3629 ms->ms_ttl = (uint8_t) ireq->i_val;
3631 case IEEE80211_IOC_MESH_RTCMD:
3632 switch (ireq->i_val) {
3633 case IEEE80211_MESH_RTCMD_LIST:
3635 case IEEE80211_MESH_RTCMD_FLUSH:
3636 ieee80211_mesh_rt_flush(vap);
3638 case IEEE80211_MESH_RTCMD_ADD:
3639 if (IEEE80211_ADDR_EQ(vap->iv_myaddr, ireq->i_data) ||
3640 IEEE80211_ADDR_EQ(broadcastaddr, ireq->i_data))
3642 error = copyin(ireq->i_data, &tmpaddr,
3643 IEEE80211_ADDR_LEN);
3645 ieee80211_mesh_discover(vap, tmpaddr, NULL);
3647 case IEEE80211_MESH_RTCMD_DELETE:
3648 ieee80211_mesh_rt_del(vap, ireq->i_data);
3654 case IEEE80211_IOC_MESH_PR_METRIC:
3655 error = copyin(ireq->i_data, tmpproto, sizeof(tmpproto));
3657 error = mesh_select_proto_metric(vap, tmpproto);
3662 case IEEE80211_IOC_MESH_PR_PATH:
3663 error = copyin(ireq->i_data, tmpproto, sizeof(tmpproto));
3665 error = mesh_select_proto_path(vap, tmpproto);
3675 IEEE80211_IOCTL_SET(mesh, mesh_ioctl_set80211);