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 receiving 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 %s",
1130 ether_sprintf(gr->gr_addr));
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(%s)", ether_sprintf(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(%s) DA(%s)",
1520 ether_sprintf(mc10->mc_addr6),
1521 ether_sprintf(mc10->mc_addr5));
1523 return (0); /* process locally */
1527 * Try to forward the group addressed data on to other mesh STAs, and
1530 * > 0 means we have forwarded data and no need to process locally
1531 * == 0 means we want to process locally (and we may have forwarded data
1532 * < 0 means there was an error and data should be discarded
1535 mesh_recv_group_data(struct ieee80211vap *vap, struct mbuf *m,
1536 struct ieee80211_frame *wh, const struct ieee80211_meshcntl *mc)
1538 #define MC01(mc) ((const struct ieee80211_meshcntl_ae01 *)mc)
1539 struct ieee80211_mesh_state *ms = vap->iv_mesh;
1541 /* This is called from the RX path - don't hold this lock */
1542 IEEE80211_TX_UNLOCK_ASSERT(vap->iv_ic);
1544 mesh_forward(vap, m, mc);
1546 if(mc->mc_ttl > 0) {
1547 if (mc->mc_flags & IEEE80211_MESH_AE_01) {
1549 * Forward of MSDUs from the MBSS to DS group addressed
1550 * (according to 13.11.3.2)
1551 * This happens by delivering the packet, and a bridge
1552 * will sent it on another port member.
1554 if (ms->ms_flags & IEEE80211_MESHFLAGS_GATE &&
1555 ms->ms_flags & IEEE80211_MESHFLAGS_FWD)
1556 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH,
1557 MC01(mc)->mc_addr4, "%s",
1558 "forward from MBSS to the DS");
1561 return (0); /* process locally */
1566 mesh_input(struct ieee80211_node *ni, struct mbuf *m, int rssi, int nf)
1568 #define HAS_SEQ(type) ((type & 0x4) == 0)
1569 #define MC01(mc) ((const struct ieee80211_meshcntl_ae01 *)mc)
1570 #define MC10(mc) ((const struct ieee80211_meshcntl_ae10 *)mc)
1571 struct ieee80211vap *vap = ni->ni_vap;
1572 struct ieee80211com *ic = ni->ni_ic;
1573 struct ifnet *ifp = vap->iv_ifp;
1574 struct ieee80211_frame *wh;
1575 const struct ieee80211_meshcntl *mc;
1576 int hdrspace, meshdrlen, need_tap, error;
1577 uint8_t dir, type, subtype, ae;
1579 const uint8_t *addr;
1581 ieee80211_seq rxseq;
1583 KASSERT(ni != NULL, ("null node"));
1584 ni->ni_inact = ni->ni_inact_reload;
1586 need_tap = 1; /* mbuf need to be tapped. */
1587 type = -1; /* undefined */
1589 /* This is called from the RX path - don't hold this lock */
1590 IEEE80211_TX_UNLOCK_ASSERT(ic);
1592 if (m->m_pkthdr.len < sizeof(struct ieee80211_frame_min)) {
1593 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
1594 ni->ni_macaddr, NULL,
1595 "too short (1): len %u", m->m_pkthdr.len);
1596 vap->iv_stats.is_rx_tooshort++;
1600 * Bit of a cheat here, we use a pointer for a 3-address
1601 * frame format but don't reference fields past outside
1602 * ieee80211_frame_min w/o first validating the data is
1605 wh = mtod(m, struct ieee80211_frame *);
1607 if ((wh->i_fc[0] & IEEE80211_FC0_VERSION_MASK) !=
1608 IEEE80211_FC0_VERSION_0) {
1609 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
1610 ni->ni_macaddr, NULL, "wrong version %x", wh->i_fc[0]);
1611 vap->iv_stats.is_rx_badversion++;
1614 dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK;
1615 type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
1616 subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
1617 if ((ic->ic_flags & IEEE80211_F_SCAN) == 0) {
1618 IEEE80211_RSSI_LPF(ni->ni_avgrssi, rssi);
1620 if (HAS_SEQ(type)) {
1621 uint8_t tid = ieee80211_gettid(wh);
1623 if (IEEE80211_QOS_HAS_SEQ(wh) &&
1624 TID_TO_WME_AC(tid) >= WME_AC_VI)
1625 ic->ic_wme.wme_hipri_traffic++;
1626 rxseq = le16toh(*(uint16_t *)wh->i_seq);
1627 if (! ieee80211_check_rxseq(ni, wh)) {
1628 /* duplicate, discard */
1629 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
1630 wh->i_addr1, "duplicate",
1631 "seqno <%u,%u> fragno <%u,%u> tid %u",
1632 rxseq >> IEEE80211_SEQ_SEQ_SHIFT,
1633 ni->ni_rxseqs[tid] >>
1634 IEEE80211_SEQ_SEQ_SHIFT,
1635 rxseq & IEEE80211_SEQ_FRAG_MASK,
1636 ni->ni_rxseqs[tid] &
1637 IEEE80211_SEQ_FRAG_MASK,
1639 vap->iv_stats.is_rx_dup++;
1640 IEEE80211_NODE_STAT(ni, rx_dup);
1643 ni->ni_rxseqs[tid] = rxseq;
1646 #ifdef IEEE80211_DEBUG
1648 * It's easier, but too expensive, to simulate different mesh
1649 * topologies by consulting the ACL policy very early, so do this
1652 * NB: this check is also done upon peering link initiation.
1654 if (vap->iv_acl != NULL && !vap->iv_acl->iac_check(vap, wh)) {
1655 IEEE80211_DISCARD(vap, IEEE80211_MSG_ACL,
1656 wh, NULL, "%s", "disallowed by ACL");
1657 vap->iv_stats.is_rx_acl++;
1662 case IEEE80211_FC0_TYPE_DATA:
1663 if (ni == vap->iv_bss)
1665 if (ni->ni_mlstate != IEEE80211_NODE_MESH_ESTABLISHED) {
1666 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_MESH,
1667 ni->ni_macaddr, NULL,
1668 "peer link not yet established (%d)",
1670 vap->iv_stats.is_mesh_nolink++;
1673 if (dir != IEEE80211_FC1_DIR_FROMDS &&
1674 dir != IEEE80211_FC1_DIR_DSTODS) {
1675 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
1676 wh, "data", "incorrect dir 0x%x", dir);
1677 vap->iv_stats.is_rx_wrongdir++;
1681 /* All Mesh data frames are QoS subtype */
1682 if (!HAS_SEQ(type)) {
1683 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
1684 wh, "data", "incorrect subtype 0x%x", subtype);
1685 vap->iv_stats.is_rx_badsubtype++;
1690 * Next up, any fragmentation.
1691 * XXX: we defrag before we even try to forward,
1692 * Mesh Control field is not present in sub-sequent
1693 * fragmented frames. This is in contrast to Draft 4.0.
1695 hdrspace = ieee80211_hdrspace(ic, wh);
1696 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1697 m = ieee80211_defrag(ni, m, hdrspace);
1699 /* Fragment dropped or frame not complete yet */
1703 wh = mtod(m, struct ieee80211_frame *); /* NB: after defrag */
1706 * Now we have a complete Mesh Data frame.
1710 * Only fromDStoDS data frames use 4 address qos frames
1711 * as specified in amendment. Otherwise addr4 is located
1712 * in the Mesh Control field and a 3 address qos frame
1715 if (IEEE80211_IS_DSTODS(wh))
1716 *(uint16_t *)qos = *(uint16_t *)
1717 ((struct ieee80211_qosframe_addr4 *)wh)->i_qos;
1719 *(uint16_t *)qos = *(uint16_t *)
1720 ((struct ieee80211_qosframe *)wh)->i_qos;
1723 * NB: The mesh STA sets the Mesh Control Present
1724 * subfield to 1 in the Mesh Data frame containing
1725 * an unfragmented MSDU, an A-MSDU, or the first
1726 * fragment of an MSDU.
1727 * After defrag it should always be present.
1729 if (!(qos[1] & IEEE80211_QOS_MC)) {
1730 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_MESH,
1731 ni->ni_macaddr, NULL,
1732 "%s", "Mesh control field not present");
1733 vap->iv_stats.is_rx_elem_missing++; /* XXX: kinda */
1737 /* pull up enough to get to the mesh control */
1738 if (m->m_len < hdrspace + sizeof(struct ieee80211_meshcntl) &&
1739 (m = m_pullup(m, hdrspace +
1740 sizeof(struct ieee80211_meshcntl))) == NULL) {
1741 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
1742 ni->ni_macaddr, NULL,
1743 "data too short: expecting %u", hdrspace);
1744 vap->iv_stats.is_rx_tooshort++;
1748 * Now calculate the full extent of the headers. Note
1749 * mesh_decap will pull up anything we didn't get
1750 * above when it strips the 802.11 headers.
1752 mc = (const struct ieee80211_meshcntl *)
1753 (mtod(m, const uint8_t *) + hdrspace);
1754 ae = mc->mc_flags & IEEE80211_MESH_AE_MASK;
1755 meshdrlen = sizeof(struct ieee80211_meshcntl) +
1756 ae * IEEE80211_ADDR_LEN;
1757 hdrspace += meshdrlen;
1759 /* pull complete hdrspace = ieee80211_hdrspace + meshcontrol */
1760 if ((meshdrlen > sizeof(struct ieee80211_meshcntl)) &&
1761 (m->m_len < hdrspace) &&
1762 ((m = m_pullup(m, hdrspace)) == NULL)) {
1763 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
1764 ni->ni_macaddr, NULL,
1765 "data too short: expecting %u", hdrspace);
1766 vap->iv_stats.is_rx_tooshort++;
1769 /* XXX: are we sure there is no reallocating after m_pullup? */
1771 seq = LE_READ_4(mc->mc_seq);
1772 if (IEEE80211_IS_MULTICAST(wh->i_addr1))
1774 else if (ae == IEEE80211_MESH_AE_01)
1775 addr = MC01(mc)->mc_addr4;
1777 addr = ((struct ieee80211_qosframe_addr4 *)wh)->i_addr4;
1778 if (IEEE80211_ADDR_EQ(vap->iv_myaddr, addr)) {
1779 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
1780 addr, "data", "%s", "not to me");
1781 vap->iv_stats.is_rx_wrongbss++; /* XXX kinda */
1784 if (mesh_checkpseq(vap, addr, seq) != 0) {
1785 vap->iv_stats.is_rx_dup++;
1789 /* This code "routes" the frame to the right control path */
1790 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1791 if (IEEE80211_ADDR_EQ(vap->iv_myaddr, wh->i_addr3))
1793 mesh_recv_indiv_data_to_me(vap, m, wh, mc);
1794 else if (IEEE80211_IS_MULTICAST(wh->i_addr3))
1795 error = mesh_recv_group_data(vap, m, wh, mc);
1797 error = mesh_recv_indiv_data_to_fwrd(vap, m,
1800 error = mesh_recv_group_data(vap, m, wh, mc);
1806 if (ieee80211_radiotap_active_vap(vap))
1807 ieee80211_radiotap_rx(vap, m);
1811 * Finally, strip the 802.11 header.
1813 m = mesh_decap(vap, m, hdrspace, meshdrlen);
1815 /* XXX mask bit to check for both */
1816 /* don't count Null data frames as errors */
1817 if (subtype == IEEE80211_FC0_SUBTYPE_NODATA ||
1818 subtype == IEEE80211_FC0_SUBTYPE_QOS_NULL)
1820 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
1821 ni->ni_macaddr, "data", "%s", "decap error");
1822 vap->iv_stats.is_rx_decap++;
1823 IEEE80211_NODE_STAT(ni, rx_decap);
1826 if (qos[0] & IEEE80211_QOS_AMSDU) {
1827 m = ieee80211_decap_amsdu(ni, m);
1829 return IEEE80211_FC0_TYPE_DATA;
1831 ieee80211_deliver_data(vap, ni, m);
1833 case IEEE80211_FC0_TYPE_MGT:
1834 vap->iv_stats.is_rx_mgmt++;
1835 IEEE80211_NODE_STAT(ni, rx_mgmt);
1836 if (dir != IEEE80211_FC1_DIR_NODS) {
1837 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
1838 wh, "mgt", "incorrect dir 0x%x", dir);
1839 vap->iv_stats.is_rx_wrongdir++;
1842 if (m->m_pkthdr.len < sizeof(struct ieee80211_frame)) {
1843 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
1844 ni->ni_macaddr, "mgt", "too short: len %u",
1846 vap->iv_stats.is_rx_tooshort++;
1849 #ifdef IEEE80211_DEBUG
1850 if ((ieee80211_msg_debug(vap) &&
1851 (vap->iv_ic->ic_flags & IEEE80211_F_SCAN)) ||
1852 ieee80211_msg_dumppkts(vap)) {
1853 if_printf(ifp, "received %s from %s rssi %d\n",
1854 ieee80211_mgt_subtype_name[subtype >>
1855 IEEE80211_FC0_SUBTYPE_SHIFT],
1856 ether_sprintf(wh->i_addr2), rssi);
1859 if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
1860 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
1861 wh, NULL, "%s", "WEP set but not permitted");
1862 vap->iv_stats.is_rx_mgtdiscard++; /* XXX */
1865 vap->iv_recv_mgmt(ni, m, subtype, rssi, nf);
1867 case IEEE80211_FC0_TYPE_CTL:
1868 vap->iv_stats.is_rx_ctl++;
1869 IEEE80211_NODE_STAT(ni, rx_ctrl);
1872 IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
1873 wh, "bad", "frame type 0x%x", type);
1874 /* should not come here */
1878 IFNET_STAT_INC(ifp, ierrors, 1);
1881 if (need_tap && ieee80211_radiotap_active_vap(vap))
1882 ieee80211_radiotap_rx(vap, m);
1892 mesh_recv_mgmt(struct ieee80211_node *ni, struct mbuf *m0, int subtype,
1895 struct ieee80211vap *vap = ni->ni_vap;
1896 struct ieee80211_mesh_state *ms = vap->iv_mesh;
1897 struct ieee80211com *ic = ni->ni_ic;
1898 struct ieee80211_frame *wh;
1899 struct ieee80211_mesh_route *rt;
1900 uint8_t *frm, *efrm;
1902 wh = mtod(m0, struct ieee80211_frame *);
1903 frm = (uint8_t *)&wh[1];
1904 efrm = mtod(m0, uint8_t *) + m0->m_len;
1906 case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
1907 case IEEE80211_FC0_SUBTYPE_BEACON:
1909 struct ieee80211_scanparams scan;
1911 * We process beacon/probe response
1912 * frames to discover neighbors.
1914 if (ieee80211_parse_beacon(ni, m0, &scan) != 0)
1917 * Count frame now that we know it's to be processed.
1919 if (subtype == IEEE80211_FC0_SUBTYPE_BEACON) {
1920 vap->iv_stats.is_rx_beacon++; /* XXX remove */
1921 IEEE80211_NODE_STAT(ni, rx_beacons);
1923 IEEE80211_NODE_STAT(ni, rx_proberesp);
1925 * If scanning, just pass information to the scan module.
1927 if (ic->ic_flags & IEEE80211_F_SCAN) {
1928 if (ic->ic_flags_ext & IEEE80211_FEXT_PROBECHAN) {
1930 * Actively scanning a channel marked passive;
1931 * send a probe request now that we know there
1932 * is 802.11 traffic present.
1934 * XXX check if the beacon we recv'd gives
1935 * us what we need and suppress the probe req
1937 ieee80211_probe_curchan(vap, 1);
1938 ic->ic_flags_ext &= ~IEEE80211_FEXT_PROBECHAN;
1940 ieee80211_add_scan(vap, &scan, wh,
1945 /* The rest of this code assumes we are running */
1946 if (vap->iv_state != IEEE80211_S_RUN)
1949 * Ignore non-mesh STAs.
1952 (IEEE80211_CAPINFO_ESS|IEEE80211_CAPINFO_IBSS)) ||
1953 scan.meshid == NULL || scan.meshconf == NULL) {
1954 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
1955 wh, "beacon", "%s", "not a mesh sta");
1956 vap->iv_stats.is_mesh_wrongmesh++;
1960 * Ignore STAs for other mesh networks.
1962 if (memcmp(scan.meshid+2, ms->ms_id, ms->ms_idlen) != 0 ||
1963 mesh_verify_meshconf(vap, scan.meshconf)) {
1964 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
1965 wh, "beacon", "%s", "not for our mesh");
1966 vap->iv_stats.is_mesh_wrongmesh++;
1970 * Peer only based on the current ACL policy.
1972 if (vap->iv_acl != NULL && !vap->iv_acl->iac_check(vap, wh)) {
1973 IEEE80211_DISCARD(vap, IEEE80211_MSG_ACL,
1974 wh, NULL, "%s", "disallowed by ACL");
1975 vap->iv_stats.is_rx_acl++;
1979 * Do neighbor discovery.
1981 if (!IEEE80211_ADDR_EQ(wh->i_addr2, ni->ni_macaddr)) {
1983 * Create a new entry in the neighbor table.
1985 ni = ieee80211_add_neighbor(vap, wh, &scan);
1988 * Automatically peer with discovered nodes if possible.
1990 if (ni != vap->iv_bss &&
1991 (ms->ms_flags & IEEE80211_MESHFLAGS_AP)) {
1992 switch (ni->ni_mlstate) {
1993 case IEEE80211_NODE_MESH_IDLE:
1997 /* Wait for backoff callout to reset counter */
1998 if (ni->ni_mlhcnt >= ieee80211_mesh_maxholding)
2001 ni->ni_mlpid = mesh_generateid(vap);
2002 if (ni->ni_mlpid == 0)
2004 mesh_linkchange(ni, IEEE80211_NODE_MESH_OPENSNT);
2005 args[0] = ni->ni_mlpid;
2006 ieee80211_send_action(ni,
2007 IEEE80211_ACTION_CAT_SELF_PROT,
2008 IEEE80211_ACTION_MESHPEERING_OPEN, args);
2010 mesh_peer_timeout_setup(ni);
2013 case IEEE80211_NODE_MESH_ESTABLISHED:
2016 * Valid beacon from a peer mesh STA
2019 rt = ieee80211_mesh_rt_find(vap, wh->i_addr2);
2021 ieee80211_mesh_rt_update(rt,
2023 ms->ms_ppath->mpp_inact));
2033 case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
2035 uint8_t *ssid, *meshid, *rates, *xrates;
2038 if (vap->iv_state != IEEE80211_S_RUN) {
2039 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
2040 wh, NULL, "wrong state %s",
2041 ieee80211_state_name[vap->iv_state]);
2042 vap->iv_stats.is_rx_mgtdiscard++;
2045 if (IEEE80211_IS_MULTICAST(wh->i_addr2)) {
2046 /* frame must be directed */
2047 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
2048 wh, NULL, "%s", "not unicast");
2049 vap->iv_stats.is_rx_mgtdiscard++; /* XXX stat */
2053 * prreq frame format
2055 * [tlv] supported rates
2056 * [tlv] extended supported rates
2059 ssid = meshid = rates = xrates = NULL;
2061 while (efrm - frm > 1) {
2062 IEEE80211_VERIFY_LENGTH(efrm - frm, frm[1] + 2, return);
2064 case IEEE80211_ELEMID_SSID:
2067 case IEEE80211_ELEMID_RATES:
2070 case IEEE80211_ELEMID_XRATES:
2073 case IEEE80211_ELEMID_MESHID:
2079 IEEE80211_VERIFY_ELEMENT(ssid, IEEE80211_NWID_LEN, return);
2080 IEEE80211_VERIFY_ELEMENT(rates, IEEE80211_RATE_MAXSIZE, return);
2082 IEEE80211_VERIFY_ELEMENT(xrates,
2083 IEEE80211_RATE_MAXSIZE - rates[1], return);
2084 if (meshid != NULL) {
2085 IEEE80211_VERIFY_ELEMENT(meshid,
2086 IEEE80211_MESHID_LEN, return);
2087 /* NB: meshid, not ssid */
2088 IEEE80211_VERIFY_SSID(vap->iv_bss, meshid, return);
2091 /* XXX find a better class or define it's own */
2092 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_INPUT, wh->i_addr2,
2093 "%s", "recv probe req");
2095 * Some legacy 11b clients cannot hack a complete
2096 * probe response frame. When the request includes
2097 * only a bare-bones rate set, communicate this to
2098 * the transmit side.
2100 ieee80211_send_proberesp(vap, wh->i_addr2, 0);
2104 case IEEE80211_FC0_SUBTYPE_ACTION:
2105 case IEEE80211_FC0_SUBTYPE_ACTION_NOACK:
2106 if (ni == vap->iv_bss) {
2107 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
2108 wh, NULL, "%s", "unknown node");
2109 vap->iv_stats.is_rx_mgtdiscard++;
2110 } else if (!IEEE80211_ADDR_EQ(vap->iv_myaddr, wh->i_addr1) &&
2111 !IEEE80211_IS_MULTICAST(wh->i_addr1)) {
2112 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
2113 wh, NULL, "%s", "not for us");
2114 vap->iv_stats.is_rx_mgtdiscard++;
2115 } else if (vap->iv_state != IEEE80211_S_RUN) {
2116 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
2117 wh, NULL, "wrong state %s",
2118 ieee80211_state_name[vap->iv_state]);
2119 vap->iv_stats.is_rx_mgtdiscard++;
2121 if (ieee80211_parse_action(ni, m0) == 0)
2122 (void)ic->ic_recv_action(ni, wh, frm, efrm);
2126 case IEEE80211_FC0_SUBTYPE_ASSOC_REQ:
2127 case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
2128 case IEEE80211_FC0_SUBTYPE_REASSOC_REQ:
2129 case IEEE80211_FC0_SUBTYPE_REASSOC_RESP:
2130 case IEEE80211_FC0_SUBTYPE_ATIM:
2131 case IEEE80211_FC0_SUBTYPE_DISASSOC:
2132 case IEEE80211_FC0_SUBTYPE_AUTH:
2133 case IEEE80211_FC0_SUBTYPE_DEAUTH:
2134 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
2135 wh, NULL, "%s", "not handled");
2136 vap->iv_stats.is_rx_mgtdiscard++;
2140 IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
2141 wh, "mgt", "subtype 0x%x not handled", subtype);
2142 vap->iv_stats.is_rx_badsubtype++;
2148 mesh_recv_ctl(struct ieee80211_node *ni, struct mbuf *m, int subtype)
2152 case IEEE80211_FC0_SUBTYPE_BAR:
2153 ieee80211_recv_bar(ni, m);
2159 * Parse meshpeering action ie's for MPM frames
2161 static const struct ieee80211_meshpeer_ie *
2162 mesh_parse_meshpeering_action(struct ieee80211_node *ni,
2163 const struct ieee80211_frame *wh, /* XXX for VERIFY_LENGTH */
2164 const uint8_t *frm, const uint8_t *efrm,
2165 struct ieee80211_meshpeer_ie *mp, uint8_t subtype)
2167 struct ieee80211vap *vap = ni->ni_vap;
2168 const struct ieee80211_meshpeer_ie *mpie;
2170 const uint8_t *meshid, *meshconf, *meshpeer;
2171 uint8_t sendclose = 0; /* 1 = MPM frame rejected, close will be sent */
2173 meshid = meshconf = meshpeer = NULL;
2174 while (efrm - frm > 1) {
2175 IEEE80211_VERIFY_LENGTH(efrm - frm, frm[1] + 2, return NULL);
2177 case IEEE80211_ELEMID_MESHID:
2180 case IEEE80211_ELEMID_MESHCONF:
2183 case IEEE80211_ELEMID_MESHPEER:
2185 mpie = (const struct ieee80211_meshpeer_ie *) frm;
2186 memset(mp, 0, sizeof(*mp));
2187 mp->peer_len = mpie->peer_len;
2188 mp->peer_proto = LE_READ_2(&mpie->peer_proto);
2189 mp->peer_llinkid = LE_READ_2(&mpie->peer_llinkid);
2191 case IEEE80211_ACTION_MESHPEERING_CONFIRM:
2193 LE_READ_2(&mpie->peer_linkid);
2195 case IEEE80211_ACTION_MESHPEERING_CLOSE:
2196 /* NB: peer link ID is optional */
2197 if (mpie->peer_len ==
2198 (IEEE80211_MPM_BASE_SZ + 2)) {
2199 mp->peer_linkid = 0;
2201 LE_READ_2(&mpie->peer_linkid);
2204 LE_READ_2(&mpie->peer_linkid);
2206 LE_READ_2(&mpie->peer_rcode);
2216 * Verify the contents of the frame.
2217 * If it fails validation, close the peer link.
2219 if (mesh_verify_meshpeer(vap, subtype, (const uint8_t *)mp)) {
2221 IEEE80211_DISCARD(vap,
2222 IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH,
2223 wh, NULL, "%s", "MPM validation failed");
2226 /* If meshid is not the same reject any frames type. */
2227 if (sendclose == 0 && mesh_verify_meshid(vap, meshid)) {
2229 IEEE80211_DISCARD(vap,
2230 IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH,
2231 wh, NULL, "%s", "not for our mesh");
2232 if (subtype == IEEE80211_ACTION_MESHPEERING_CLOSE) {
2234 * Standard not clear about this, if we dont ignore
2235 * there will be an endless loop between nodes sending
2236 * CLOSE frames between each other with wrong meshid.
2237 * Discard and timers will bring FSM to IDLE state.
2244 * Close frames are accepted if meshid is the same.
2245 * Verify the other two types.
2247 if (sendclose == 0 && subtype != IEEE80211_ACTION_MESHPEERING_CLOSE &&
2248 mesh_verify_meshconf(vap, meshconf)) {
2250 IEEE80211_DISCARD(vap,
2251 IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH,
2252 wh, NULL, "%s", "configuration missmatch");
2256 vap->iv_stats.is_rx_mgtdiscard++;
2257 switch (ni->ni_mlstate) {
2258 case IEEE80211_NODE_MESH_IDLE:
2259 case IEEE80211_NODE_MESH_ESTABLISHED:
2260 case IEEE80211_NODE_MESH_HOLDING:
2263 case IEEE80211_NODE_MESH_OPENSNT:
2264 case IEEE80211_NODE_MESH_OPENRCV:
2265 case IEEE80211_NODE_MESH_CONFIRMRCV:
2266 args[0] = ni->ni_mlpid;
2267 args[1] = ni->ni_mllid;
2268 /* Reason codes for rejection */
2270 case IEEE80211_ACTION_MESHPEERING_OPEN:
2271 args[2] = IEEE80211_REASON_MESH_CPVIOLATION;
2273 case IEEE80211_ACTION_MESHPEERING_CONFIRM:
2274 args[2] = IEEE80211_REASON_MESH_INCONS_PARAMS;
2277 ieee80211_send_action(ni,
2278 IEEE80211_ACTION_CAT_SELF_PROT,
2279 IEEE80211_ACTION_MESHPEERING_CLOSE,
2281 mesh_linkchange(ni, IEEE80211_NODE_MESH_HOLDING);
2282 mesh_peer_timeout_setup(ni);
2288 return (const struct ieee80211_meshpeer_ie *) mp;
2292 mesh_recv_action_meshpeering_open(struct ieee80211_node *ni,
2293 const struct ieee80211_frame *wh,
2294 const uint8_t *frm, const uint8_t *efrm)
2296 struct ieee80211vap *vap = ni->ni_vap;
2297 struct ieee80211_mesh_state *ms = vap->iv_mesh;
2298 struct ieee80211_meshpeer_ie ie;
2299 const struct ieee80211_meshpeer_ie *meshpeer;
2302 /* +2+2 for action + code + capabilites */
2303 meshpeer = mesh_parse_meshpeering_action(ni, wh, frm+2+2, efrm, &ie,
2304 IEEE80211_ACTION_MESHPEERING_OPEN);
2305 if (meshpeer == NULL) {
2310 IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH, ni,
2311 "recv PEER OPEN, lid 0x%x", meshpeer->peer_llinkid);
2313 switch (ni->ni_mlstate) {
2314 case IEEE80211_NODE_MESH_IDLE:
2315 /* Reject open request if reached our maximum neighbor count */
2316 if (ms->ms_neighbors >= IEEE80211_MESH_MAX_NEIGHBORS) {
2317 args[0] = meshpeer->peer_llinkid;
2319 args[2] = IEEE80211_REASON_MESH_MAX_PEERS;
2320 ieee80211_send_action(ni,
2321 IEEE80211_ACTION_CAT_SELF_PROT,
2322 IEEE80211_ACTION_MESHPEERING_CLOSE,
2324 /* stay in IDLE state */
2327 /* Open frame accepted */
2328 mesh_linkchange(ni, IEEE80211_NODE_MESH_OPENRCV);
2329 ni->ni_mllid = meshpeer->peer_llinkid;
2330 ni->ni_mlpid = mesh_generateid(vap);
2331 if (ni->ni_mlpid == 0)
2333 args[0] = ni->ni_mlpid;
2334 /* Announce we're open too... */
2335 ieee80211_send_action(ni,
2336 IEEE80211_ACTION_CAT_SELF_PROT,
2337 IEEE80211_ACTION_MESHPEERING_OPEN, args);
2338 /* ...and confirm the link. */
2339 args[0] = ni->ni_mlpid;
2340 args[1] = ni->ni_mllid;
2341 ieee80211_send_action(ni,
2342 IEEE80211_ACTION_CAT_SELF_PROT,
2343 IEEE80211_ACTION_MESHPEERING_CONFIRM,
2345 mesh_peer_timeout_setup(ni);
2347 case IEEE80211_NODE_MESH_OPENRCV:
2349 if (ni->ni_mllid != meshpeer->peer_llinkid) {
2350 args[0] = ni->ni_mllid;
2351 args[1] = ni->ni_mlpid;
2352 args[2] = IEEE80211_REASON_PEER_LINK_CANCELED;
2353 ieee80211_send_action(ni,
2354 IEEE80211_ACTION_CAT_SELF_PROT,
2355 IEEE80211_ACTION_MESHPEERING_CLOSE,
2357 mesh_linkchange(ni, IEEE80211_NODE_MESH_HOLDING);
2358 mesh_peer_timeout_setup(ni);
2361 /* Duplicate open, confirm again. */
2362 args[0] = ni->ni_mlpid;
2363 args[1] = ni->ni_mllid;
2364 ieee80211_send_action(ni,
2365 IEEE80211_ACTION_CAT_SELF_PROT,
2366 IEEE80211_ACTION_MESHPEERING_CONFIRM,
2369 case IEEE80211_NODE_MESH_OPENSNT:
2370 ni->ni_mllid = meshpeer->peer_llinkid;
2371 mesh_linkchange(ni, IEEE80211_NODE_MESH_OPENRCV);
2372 args[0] = ni->ni_mlpid;
2373 args[1] = ni->ni_mllid;
2374 ieee80211_send_action(ni,
2375 IEEE80211_ACTION_CAT_SELF_PROT,
2376 IEEE80211_ACTION_MESHPEERING_CONFIRM,
2378 /* NB: don't setup/clear any timeout */
2380 case IEEE80211_NODE_MESH_CONFIRMRCV:
2381 if (ni->ni_mlpid != meshpeer->peer_linkid ||
2382 ni->ni_mllid != meshpeer->peer_llinkid) {
2383 args[0] = ni->ni_mlpid;
2384 args[1] = ni->ni_mllid;
2385 args[2] = IEEE80211_REASON_PEER_LINK_CANCELED;
2386 ieee80211_send_action(ni,
2387 IEEE80211_ACTION_CAT_SELF_PROT,
2388 IEEE80211_ACTION_MESHPEERING_CLOSE,
2391 IEEE80211_NODE_MESH_HOLDING);
2392 mesh_peer_timeout_setup(ni);
2395 mesh_linkchange(ni, IEEE80211_NODE_MESH_ESTABLISHED);
2396 ni->ni_mllid = meshpeer->peer_llinkid;
2397 args[0] = ni->ni_mlpid;
2398 args[1] = ni->ni_mllid;
2399 ieee80211_send_action(ni,
2400 IEEE80211_ACTION_CAT_SELF_PROT,
2401 IEEE80211_ACTION_MESHPEERING_CONFIRM,
2403 mesh_peer_timeout_stop(ni);
2405 case IEEE80211_NODE_MESH_ESTABLISHED:
2406 if (ni->ni_mllid != meshpeer->peer_llinkid) {
2407 args[0] = ni->ni_mllid;
2408 args[1] = ni->ni_mlpid;
2409 args[2] = IEEE80211_REASON_PEER_LINK_CANCELED;
2410 ieee80211_send_action(ni,
2411 IEEE80211_ACTION_CAT_SELF_PROT,
2412 IEEE80211_ACTION_MESHPEERING_CLOSE,
2414 mesh_linkchange(ni, IEEE80211_NODE_MESH_HOLDING);
2415 mesh_peer_timeout_setup(ni);
2418 args[0] = ni->ni_mlpid;
2419 args[1] = ni->ni_mllid;
2420 ieee80211_send_action(ni,
2421 IEEE80211_ACTION_CAT_SELF_PROT,
2422 IEEE80211_ACTION_MESHPEERING_CONFIRM,
2425 case IEEE80211_NODE_MESH_HOLDING:
2426 args[0] = ni->ni_mlpid;
2427 args[1] = meshpeer->peer_llinkid;
2428 /* Standard not clear about what the reaason code should be */
2429 args[2] = IEEE80211_REASON_PEER_LINK_CANCELED;
2430 ieee80211_send_action(ni,
2431 IEEE80211_ACTION_CAT_SELF_PROT,
2432 IEEE80211_ACTION_MESHPEERING_CLOSE,
2440 mesh_recv_action_meshpeering_confirm(struct ieee80211_node *ni,
2441 const struct ieee80211_frame *wh,
2442 const uint8_t *frm, const uint8_t *efrm)
2444 struct ieee80211vap *vap = ni->ni_vap;
2445 struct ieee80211_meshpeer_ie ie;
2446 const struct ieee80211_meshpeer_ie *meshpeer;
2449 /* +2+2+2+2 for action + code + capabilites + status code + AID */
2450 meshpeer = mesh_parse_meshpeering_action(ni, wh, frm+2+2+2+2, efrm, &ie,
2451 IEEE80211_ACTION_MESHPEERING_CONFIRM);
2452 if (meshpeer == NULL) {
2456 IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH, ni,
2457 "recv PEER CONFIRM, local id 0x%x, peer id 0x%x",
2458 meshpeer->peer_llinkid, meshpeer->peer_linkid);
2460 switch (ni->ni_mlstate) {
2461 case IEEE80211_NODE_MESH_OPENRCV:
2462 mesh_linkchange(ni, IEEE80211_NODE_MESH_ESTABLISHED);
2463 mesh_peer_timeout_stop(ni);
2465 case IEEE80211_NODE_MESH_OPENSNT:
2466 mesh_linkchange(ni, IEEE80211_NODE_MESH_CONFIRMRCV);
2467 mesh_peer_timeout_setup(ni);
2469 case IEEE80211_NODE_MESH_HOLDING:
2470 args[0] = ni->ni_mlpid;
2471 args[1] = meshpeer->peer_llinkid;
2472 /* Standard not clear about what the reaason code should be */
2473 args[2] = IEEE80211_REASON_PEER_LINK_CANCELED;
2474 ieee80211_send_action(ni,
2475 IEEE80211_ACTION_CAT_SELF_PROT,
2476 IEEE80211_ACTION_MESHPEERING_CLOSE,
2479 case IEEE80211_NODE_MESH_CONFIRMRCV:
2480 if (ni->ni_mllid != meshpeer->peer_llinkid) {
2481 args[0] = ni->ni_mlpid;
2482 args[1] = ni->ni_mllid;
2483 args[2] = IEEE80211_REASON_PEER_LINK_CANCELED;
2484 ieee80211_send_action(ni,
2485 IEEE80211_ACTION_CAT_SELF_PROT,
2486 IEEE80211_ACTION_MESHPEERING_CLOSE,
2488 mesh_linkchange(ni, IEEE80211_NODE_MESH_HOLDING);
2489 mesh_peer_timeout_setup(ni);
2493 IEEE80211_DISCARD(vap,
2494 IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH,
2495 wh, NULL, "received confirm in invalid state %d",
2497 vap->iv_stats.is_rx_mgtdiscard++;
2504 mesh_recv_action_meshpeering_close(struct ieee80211_node *ni,
2505 const struct ieee80211_frame *wh,
2506 const uint8_t *frm, const uint8_t *efrm)
2508 struct ieee80211_meshpeer_ie ie;
2509 const struct ieee80211_meshpeer_ie *meshpeer;
2512 /* +2 for action + code */
2513 meshpeer = mesh_parse_meshpeering_action(ni, wh, frm+2, efrm, &ie,
2514 IEEE80211_ACTION_MESHPEERING_CLOSE);
2515 if (meshpeer == NULL) {
2520 * XXX: check reason code, for example we could receive
2521 * IEEE80211_REASON_MESH_MAX_PEERS then we should not attempt
2525 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH,
2526 ni, "%s", "recv PEER CLOSE");
2528 switch (ni->ni_mlstate) {
2529 case IEEE80211_NODE_MESH_IDLE:
2532 case IEEE80211_NODE_MESH_OPENRCV:
2533 case IEEE80211_NODE_MESH_OPENSNT:
2534 case IEEE80211_NODE_MESH_CONFIRMRCV:
2535 case IEEE80211_NODE_MESH_ESTABLISHED:
2536 args[0] = ni->ni_mlpid;
2537 args[1] = ni->ni_mllid;
2538 args[2] = IEEE80211_REASON_MESH_CLOSE_RCVD;
2539 ieee80211_send_action(ni,
2540 IEEE80211_ACTION_CAT_SELF_PROT,
2541 IEEE80211_ACTION_MESHPEERING_CLOSE,
2543 mesh_linkchange(ni, IEEE80211_NODE_MESH_HOLDING);
2544 mesh_peer_timeout_setup(ni);
2546 case IEEE80211_NODE_MESH_HOLDING:
2547 mesh_linkchange(ni, IEEE80211_NODE_MESH_IDLE);
2548 mesh_peer_timeout_stop(ni);
2555 * Link Metric handling.
2558 mesh_recv_action_meshlmetric(struct ieee80211_node *ni,
2559 const struct ieee80211_frame *wh,
2560 const uint8_t *frm, const uint8_t *efrm)
2562 const struct ieee80211_meshlmetric_ie *ie =
2563 (const struct ieee80211_meshlmetric_ie *)
2564 (frm+2); /* action + code */
2565 struct ieee80211_meshlmetric_ie lm_rep;
2567 if (ie->lm_flags & IEEE80211_MESH_LMETRIC_FLAGS_REQ) {
2568 lm_rep.lm_flags = 0;
2569 lm_rep.lm_metric = mesh_airtime_calc(ni);
2570 ieee80211_send_action(ni,
2571 IEEE80211_ACTION_CAT_MESH,
2572 IEEE80211_ACTION_MESH_LMETRIC,
2575 /* XXX: else do nothing for now */
2580 * Parse meshgate action ie's for GANN frames.
2581 * Returns -1 if parsing fails, otherwise 0.
2584 mesh_parse_meshgate_action(struct ieee80211_node *ni,
2585 const struct ieee80211_frame *wh, /* XXX for VERIFY_LENGTH */
2586 struct ieee80211_meshgann_ie *ie, const uint8_t *frm, const uint8_t *efrm)
2588 struct ieee80211vap *vap = ni->ni_vap;
2589 const struct ieee80211_meshgann_ie *gannie;
2591 while (efrm - frm > 1) {
2592 IEEE80211_VERIFY_LENGTH(efrm - frm, frm[1] + 2, return -1);
2594 case IEEE80211_ELEMID_MESHGANN:
2595 gannie = (const struct ieee80211_meshgann_ie *) frm;
2596 memset(ie, 0, sizeof(*ie));
2597 ie->gann_ie = gannie->gann_ie;
2598 ie->gann_len = gannie->gann_len;
2599 ie->gann_flags = gannie->gann_flags;
2600 ie->gann_hopcount = gannie->gann_hopcount;
2601 ie->gann_ttl = gannie->gann_ttl;
2602 IEEE80211_ADDR_COPY(ie->gann_addr, gannie->gann_addr);
2603 ie->gann_seq = LE_READ_4(&gannie->gann_seq);
2604 ie->gann_interval = LE_READ_2(&gannie->gann_interval);
2614 * Mesh Gate Announcement handling.
2617 mesh_recv_action_meshgate(struct ieee80211_node *ni,
2618 const struct ieee80211_frame *wh,
2619 const uint8_t *frm, const uint8_t *efrm)
2621 struct ieee80211vap *vap = ni->ni_vap;
2622 struct ieee80211_mesh_state *ms = vap->iv_mesh;
2623 struct ieee80211_mesh_gate_route *gr, *next;
2624 struct ieee80211_mesh_route *rt_gate;
2625 struct ieee80211_meshgann_ie pgann;
2626 struct ieee80211_meshgann_ie ie;
2629 /* +2 for action + code */
2630 if (mesh_parse_meshgate_action(ni, wh, &ie, frm+2, efrm) != 0) {
2631 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_MESH,
2632 ni->ni_macaddr, NULL, "%s",
2633 "GANN parsing failed");
2634 vap->iv_stats.is_rx_mgtdiscard++;
2638 if (IEEE80211_ADDR_EQ(vap->iv_myaddr, ie.gann_addr))
2641 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH, ni->ni_macaddr,
2642 "received GANN, meshgate: %s (seq %u)",
2643 ether_sprintf(ie.gann_addr),
2649 TAILQ_FOREACH_SAFE(gr, &ms->ms_known_gates, gr_next, next) {
2650 if (!IEEE80211_ADDR_EQ(gr->gr_addr, ie.gann_addr))
2652 if (ie.gann_seq <= gr->gr_lastseq) {
2653 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_MESH,
2654 ni->ni_macaddr, NULL,
2655 "GANN old seqno %u <= %u",
2656 ie.gann_seq, gr->gr_lastseq);
2660 /* corresponding mesh gate found & GANN accepted */
2666 /* this GANN is from a new mesh Gate add it to known table. */
2667 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH, ie.gann_addr,
2668 "stored new GANN information, seq %u.", ie.gann_seq);
2669 gr = kmalloc(ALIGN(sizeof(struct ieee80211_mesh_gate_route)),
2670 M_80211_MESH_GT_RT, M_INTWAIT | M_ZERO);
2671 IEEE80211_ADDR_COPY(gr->gr_addr, ie.gann_addr);
2672 TAILQ_INSERT_TAIL(&ms->ms_known_gates, gr, gr_next);
2674 gr->gr_lastseq = ie.gann_seq;
2676 /* check if we have a path to this gate */
2677 rt_gate = mesh_rt_find_locked(ms, gr->gr_addr);
2678 if (rt_gate != NULL &&
2679 rt_gate->rt_flags & IEEE80211_MESHRT_FLAGS_VALID) {
2680 gr->gr_route = rt_gate;
2681 rt_gate->rt_flags |= IEEE80211_MESHRT_FLAGS_GATE;
2686 /* popagate only if decremented ttl >= 1 && forwarding is enabled */
2687 if ((ie.gann_ttl - 1) < 1 && !(ms->ms_flags & IEEE80211_MESHFLAGS_FWD))
2689 pgann.gann_flags = ie.gann_flags; /* Reserved */
2690 pgann.gann_hopcount = ie.gann_hopcount + 1;
2691 pgann.gann_ttl = ie.gann_ttl - 1;
2692 IEEE80211_ADDR_COPY(pgann.gann_addr, ie.gann_addr);
2693 pgann.gann_seq = ie.gann_seq;
2694 pgann.gann_interval = ie.gann_interval;
2696 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH, ie.gann_addr,
2697 "%s", "propagate GANN");
2699 ieee80211_send_action(vap->iv_bss, IEEE80211_ACTION_CAT_MESH,
2700 IEEE80211_ACTION_MESH_GANN, &pgann);
2706 mesh_send_action(struct ieee80211_node *ni,
2707 const uint8_t sa[IEEE80211_ADDR_LEN],
2708 const uint8_t da[IEEE80211_ADDR_LEN],
2711 struct ieee80211vap *vap = ni->ni_vap;
2712 struct ieee80211com *ic = ni->ni_ic;
2713 struct ieee80211_bpf_params params;
2714 struct ieee80211_frame *wh;
2717 KASSERT(ni != NULL, ("null node"));
2719 if (vap->iv_state == IEEE80211_S_CAC) {
2720 IEEE80211_NOTE(vap, IEEE80211_MSG_OUTPUT, ni,
2721 "block %s frame in CAC state", "Mesh action");
2722 vap->iv_stats.is_tx_badstate++;
2723 ieee80211_free_node(ni);
2725 return EIO; /* XXX */
2728 M_PREPEND(m, sizeof(struct ieee80211_frame), M_NOWAIT);
2730 ieee80211_free_node(ni);
2734 IEEE80211_TX_LOCK(ic);
2735 wh = mtod(m, struct ieee80211_frame *);
2736 ieee80211_send_setup(ni, m,
2737 IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_ACTION,
2738 IEEE80211_NONQOS_TID, sa, da, sa);
2739 m->m_flags |= M_ENCAP; /* mark encapsulated */
2741 memset(¶ms, 0, sizeof(params));
2742 params.ibp_pri = WME_AC_VO;
2743 params.ibp_rate0 = ni->ni_txparms->mgmtrate;
2744 if (IEEE80211_IS_MULTICAST(da))
2745 params.ibp_try0 = 1;
2747 params.ibp_try0 = ni->ni_txparms->maxretry;
2748 params.ibp_power = ni->ni_txpower;
2750 IEEE80211_NODE_STAT(ni, tx_mgmt);
2752 ret = ieee80211_raw_output(vap, ni, m, ¶ms);
2753 IEEE80211_TX_UNLOCK(ic);
2757 #define ADDSHORT(frm, v) do { \
2758 frm[0] = (v) & 0xff; \
2759 frm[1] = (v) >> 8; \
2762 #define ADDWORD(frm, v) do { \
2763 frm[0] = (v) & 0xff; \
2764 frm[1] = ((v) >> 8) & 0xff; \
2765 frm[2] = ((v) >> 16) & 0xff; \
2766 frm[3] = ((v) >> 24) & 0xff; \
2771 mesh_send_action_meshpeering_open(struct ieee80211_node *ni,
2772 int category, int action, void *args0)
2774 struct ieee80211vap *vap = ni->ni_vap;
2775 struct ieee80211com *ic = ni->ni_ic;
2776 uint16_t *args = args0;
2777 const struct ieee80211_rateset *rs;
2781 IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH, ni,
2782 "send PEER OPEN action: localid 0x%x", args[0]);
2784 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
2785 "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", __func__, __LINE__,
2786 ni, ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)+1);
2787 ieee80211_ref_node(ni);
2789 m = ieee80211_getmgtframe(&frm,
2790 ic->ic_headroom + sizeof(struct ieee80211_frame),
2791 sizeof(uint16_t) /* action+category */
2792 + sizeof(uint16_t) /* capabilites */
2793 + 2 + IEEE80211_RATE_SIZE
2794 + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
2795 + 2 + IEEE80211_MESHID_LEN
2796 + sizeof(struct ieee80211_meshconf_ie)
2797 + sizeof(struct ieee80211_meshpeer_ie)
2801 * mesh peer open action frame format:
2809 * [tlv] mesh peer link mgmt
2813 ADDSHORT(frm, ieee80211_getcapinfo(vap, ni->ni_chan));
2814 rs = ieee80211_get_suprates(ic, ic->ic_curchan);
2815 frm = ieee80211_add_rates(frm, rs);
2816 frm = ieee80211_add_xrates(frm, rs);
2817 frm = ieee80211_add_meshid(frm, vap);
2818 frm = ieee80211_add_meshconf(frm, vap);
2819 frm = ieee80211_add_meshpeer(frm, IEEE80211_ACTION_MESHPEERING_OPEN,
2821 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2822 return mesh_send_action(ni, vap->iv_myaddr, ni->ni_macaddr, m);
2824 vap->iv_stats.is_tx_nobuf++;
2825 ieee80211_free_node(ni);
2831 mesh_send_action_meshpeering_confirm(struct ieee80211_node *ni,
2832 int category, int action, void *args0)
2834 struct ieee80211vap *vap = ni->ni_vap;
2835 struct ieee80211com *ic = ni->ni_ic;
2836 uint16_t *args = args0;
2837 const struct ieee80211_rateset *rs;
2841 IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH, ni,
2842 "send PEER CONFIRM action: localid 0x%x, peerid 0x%x",
2845 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
2846 "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", __func__, __LINE__,
2847 ni, ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)+1);
2848 ieee80211_ref_node(ni);
2850 m = ieee80211_getmgtframe(&frm,
2851 ic->ic_headroom + sizeof(struct ieee80211_frame),
2852 sizeof(uint16_t) /* action+category */
2853 + sizeof(uint16_t) /* capabilites */
2854 + sizeof(uint16_t) /* status code */
2855 + sizeof(uint16_t) /* AID */
2856 + 2 + IEEE80211_RATE_SIZE
2857 + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
2858 + 2 + IEEE80211_MESHID_LEN
2859 + sizeof(struct ieee80211_meshconf_ie)
2860 + sizeof(struct ieee80211_meshpeer_ie)
2864 * mesh peer confirm action frame format:
2869 * [2] association id (peer ID)
2874 * [tlv] mesh peer link mgmt
2878 ADDSHORT(frm, ieee80211_getcapinfo(vap, ni->ni_chan));
2879 ADDSHORT(frm, 0); /* status code */
2880 ADDSHORT(frm, args[1]); /* AID */
2881 rs = ieee80211_get_suprates(ic, ic->ic_curchan);
2882 frm = ieee80211_add_rates(frm, rs);
2883 frm = ieee80211_add_xrates(frm, rs);
2884 frm = ieee80211_add_meshid(frm, vap);
2885 frm = ieee80211_add_meshconf(frm, vap);
2886 frm = ieee80211_add_meshpeer(frm,
2887 IEEE80211_ACTION_MESHPEERING_CONFIRM,
2888 args[0], args[1], 0);
2889 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2890 return mesh_send_action(ni, vap->iv_myaddr, ni->ni_macaddr, m);
2892 vap->iv_stats.is_tx_nobuf++;
2893 ieee80211_free_node(ni);
2899 mesh_send_action_meshpeering_close(struct ieee80211_node *ni,
2900 int category, int action, void *args0)
2902 struct ieee80211vap *vap = ni->ni_vap;
2903 struct ieee80211com *ic = ni->ni_ic;
2904 uint16_t *args = args0;
2908 IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH, ni,
2909 "send PEER CLOSE action: localid 0x%x, peerid 0x%x reason %d",
2910 args[0], args[1], args[2]);
2912 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
2913 "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", __func__, __LINE__,
2914 ni, ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)+1);
2915 ieee80211_ref_node(ni);
2917 m = ieee80211_getmgtframe(&frm,
2918 ic->ic_headroom + sizeof(struct ieee80211_frame),
2919 sizeof(uint16_t) /* action+category */
2920 + sizeof(uint16_t) /* reason code */
2921 + 2 + IEEE80211_MESHID_LEN
2922 + sizeof(struct ieee80211_meshpeer_ie)
2926 * mesh peer close action frame format:
2930 * [tlv] mesh peer link mgmt
2934 frm = ieee80211_add_meshid(frm, vap);
2935 frm = ieee80211_add_meshpeer(frm,
2936 IEEE80211_ACTION_MESHPEERING_CLOSE,
2937 args[0], args[1], args[2]);
2938 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2939 return mesh_send_action(ni, vap->iv_myaddr, ni->ni_macaddr, m);
2941 vap->iv_stats.is_tx_nobuf++;
2942 ieee80211_free_node(ni);
2948 mesh_send_action_meshlmetric(struct ieee80211_node *ni,
2949 int category, int action, void *arg0)
2951 struct ieee80211vap *vap = ni->ni_vap;
2952 struct ieee80211com *ic = ni->ni_ic;
2953 struct ieee80211_meshlmetric_ie *ie = arg0;
2957 if (ie->lm_flags & IEEE80211_MESH_LMETRIC_FLAGS_REQ) {
2958 IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH,
2959 ni, "%s", "send LINK METRIC REQUEST action");
2961 IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH,
2962 ni, "send LINK METRIC REPLY action: metric 0x%x",
2965 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
2966 "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", __func__, __LINE__,
2967 ni, ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)+1);
2968 ieee80211_ref_node(ni);
2970 m = ieee80211_getmgtframe(&frm,
2971 ic->ic_headroom + sizeof(struct ieee80211_frame),
2972 sizeof(uint16_t) + /* action+category */
2973 sizeof(struct ieee80211_meshlmetric_ie)
2980 * [tlv] mesh link metric
2984 frm = ieee80211_add_meshlmetric(frm,
2985 ie->lm_flags, ie->lm_metric);
2986 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2987 return mesh_send_action(ni, vap->iv_myaddr, ni->ni_macaddr, m);
2989 vap->iv_stats.is_tx_nobuf++;
2990 ieee80211_free_node(ni);
2996 mesh_send_action_meshgate(struct ieee80211_node *ni,
2997 int category, int action, void *arg0)
2999 struct ieee80211vap *vap = ni->ni_vap;
3000 struct ieee80211com *ic = ni->ni_ic;
3001 struct ieee80211_meshgann_ie *ie = arg0;
3005 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
3006 "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", __func__, __LINE__,
3007 ni, ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)+1);
3008 ieee80211_ref_node(ni);
3010 m = ieee80211_getmgtframe(&frm,
3011 ic->ic_headroom + sizeof(struct ieee80211_frame),
3012 sizeof(uint16_t) + /* action+category */
3013 IEEE80211_MESHGANN_BASE_SZ
3020 * [tlv] mesh gate annoucement
3024 frm = ieee80211_add_meshgate(frm, ie);
3025 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
3026 return mesh_send_action(ni, vap->iv_myaddr, broadcastaddr, m);
3028 vap->iv_stats.is_tx_nobuf++;
3029 ieee80211_free_node(ni);
3035 mesh_peer_timeout_setup(struct ieee80211_node *ni)
3037 switch (ni->ni_mlstate) {
3038 case IEEE80211_NODE_MESH_HOLDING:
3039 ni->ni_mltval = ieee80211_mesh_holdingtimeout;
3041 case IEEE80211_NODE_MESH_CONFIRMRCV:
3042 ni->ni_mltval = ieee80211_mesh_confirmtimeout;
3044 case IEEE80211_NODE_MESH_IDLE:
3048 ni->ni_mltval = ieee80211_mesh_retrytimeout;
3052 callout_reset(&ni->ni_mltimer, ni->ni_mltval,
3053 mesh_peer_timeout_cb, ni);
3057 * Same as above but backoffs timer statisically 50%.
3060 mesh_peer_timeout_backoff(struct ieee80211_node *ni)
3065 ni->ni_mltval += r % ni->ni_mltval;
3066 callout_reset(&ni->ni_mltimer, ni->ni_mltval, mesh_peer_timeout_cb,
3070 static __inline void
3071 mesh_peer_timeout_stop(struct ieee80211_node *ni)
3073 callout_drain(&ni->ni_mltimer);
3077 mesh_peer_backoff_cb(void *arg)
3079 struct ieee80211_node *ni = (struct ieee80211_node *)arg;
3081 /* After backoff timeout, try to peer automatically again. */
3086 * Mesh Peer Link Management FSM timeout handling.
3089 mesh_peer_timeout_cb(void *arg)
3091 struct ieee80211_node *ni = (struct ieee80211_node *)arg;
3094 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_MESH,
3095 ni, "mesh link timeout, state %d, retry counter %d",
3096 ni->ni_mlstate, ni->ni_mlrcnt);
3098 switch (ni->ni_mlstate) {
3099 case IEEE80211_NODE_MESH_IDLE:
3100 case IEEE80211_NODE_MESH_ESTABLISHED:
3102 case IEEE80211_NODE_MESH_OPENSNT:
3103 case IEEE80211_NODE_MESH_OPENRCV:
3104 if (ni->ni_mlrcnt == ieee80211_mesh_maxretries) {
3105 args[0] = ni->ni_mlpid;
3106 args[2] = IEEE80211_REASON_MESH_MAX_RETRIES;
3107 ieee80211_send_action(ni,
3108 IEEE80211_ACTION_CAT_SELF_PROT,
3109 IEEE80211_ACTION_MESHPEERING_CLOSE, args);
3111 mesh_linkchange(ni, IEEE80211_NODE_MESH_HOLDING);
3112 mesh_peer_timeout_setup(ni);
3114 args[0] = ni->ni_mlpid;
3115 ieee80211_send_action(ni,
3116 IEEE80211_ACTION_CAT_SELF_PROT,
3117 IEEE80211_ACTION_MESHPEERING_OPEN, args);
3119 mesh_peer_timeout_backoff(ni);
3122 case IEEE80211_NODE_MESH_CONFIRMRCV:
3123 args[0] = ni->ni_mlpid;
3124 args[2] = IEEE80211_REASON_MESH_CONFIRM_TIMEOUT;
3125 ieee80211_send_action(ni,
3126 IEEE80211_ACTION_CAT_SELF_PROT,
3127 IEEE80211_ACTION_MESHPEERING_CLOSE, args);
3128 mesh_linkchange(ni, IEEE80211_NODE_MESH_HOLDING);
3129 mesh_peer_timeout_setup(ni);
3131 case IEEE80211_NODE_MESH_HOLDING:
3133 if (ni->ni_mlhcnt >= ieee80211_mesh_maxholding)
3134 callout_reset(&ni->ni_mlhtimer,
3135 ieee80211_mesh_backofftimeout,
3136 mesh_peer_backoff_cb, ni);
3137 mesh_linkchange(ni, IEEE80211_NODE_MESH_IDLE);
3143 mesh_verify_meshid(struct ieee80211vap *vap, const uint8_t *ie)
3145 struct ieee80211_mesh_state *ms = vap->iv_mesh;
3147 if (ie == NULL || ie[1] != ms->ms_idlen)
3149 return memcmp(ms->ms_id, ie + 2, ms->ms_idlen);
3153 * Check if we are using the same algorithms for this mesh.
3156 mesh_verify_meshconf(struct ieee80211vap *vap, const uint8_t *ie)
3158 const struct ieee80211_meshconf_ie *meshconf =
3159 (const struct ieee80211_meshconf_ie *) ie;
3160 const struct ieee80211_mesh_state *ms = vap->iv_mesh;
3162 if (meshconf == NULL)
3164 if (meshconf->conf_pselid != ms->ms_ppath->mpp_ie) {
3165 IEEE80211_DPRINTF(vap, IEEE80211_MSG_MESH,
3166 "unknown path selection algorithm: 0x%x\n",
3167 meshconf->conf_pselid);
3170 if (meshconf->conf_pmetid != ms->ms_pmetric->mpm_ie) {
3171 IEEE80211_DPRINTF(vap, IEEE80211_MSG_MESH,
3172 "unknown path metric algorithm: 0x%x\n",
3173 meshconf->conf_pmetid);
3176 if (meshconf->conf_ccid != 0) {
3177 IEEE80211_DPRINTF(vap, IEEE80211_MSG_MESH,
3178 "unknown congestion control algorithm: 0x%x\n",
3179 meshconf->conf_ccid);
3182 if (meshconf->conf_syncid != IEEE80211_MESHCONF_SYNC_NEIGHOFF) {
3183 IEEE80211_DPRINTF(vap, IEEE80211_MSG_MESH,
3184 "unknown sync algorithm: 0x%x\n",
3185 meshconf->conf_syncid);
3188 if (meshconf->conf_authid != 0) {
3189 IEEE80211_DPRINTF(vap, IEEE80211_MSG_MESH,
3190 "unknown auth auth algorithm: 0x%x\n",
3191 meshconf->conf_pselid);
3194 /* Not accepting peers */
3195 if (!(meshconf->conf_cap & IEEE80211_MESHCONF_CAP_AP)) {
3196 IEEE80211_DPRINTF(vap, IEEE80211_MSG_MESH,
3197 "not accepting peers: 0x%x\n", meshconf->conf_cap);
3204 mesh_verify_meshpeer(struct ieee80211vap *vap, uint8_t subtype,
3207 const struct ieee80211_meshpeer_ie *meshpeer =
3208 (const struct ieee80211_meshpeer_ie *) ie;
3210 if (meshpeer == NULL ||
3211 meshpeer->peer_len < IEEE80211_MPM_BASE_SZ ||
3212 meshpeer->peer_len > IEEE80211_MPM_MAX_SZ)
3214 if (meshpeer->peer_proto != IEEE80211_MPPID_MPM) {
3215 IEEE80211_DPRINTF(vap,
3216 IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH,
3217 "Only MPM protocol is supported (proto: 0x%02X)",
3218 meshpeer->peer_proto);
3222 case IEEE80211_ACTION_MESHPEERING_OPEN:
3223 if (meshpeer->peer_len != IEEE80211_MPM_BASE_SZ)
3226 case IEEE80211_ACTION_MESHPEERING_CONFIRM:
3227 if (meshpeer->peer_len != IEEE80211_MPM_BASE_SZ + 2)
3230 case IEEE80211_ACTION_MESHPEERING_CLOSE:
3231 if (meshpeer->peer_len < IEEE80211_MPM_BASE_SZ + 2)
3233 if (meshpeer->peer_len == (IEEE80211_MPM_BASE_SZ + 2) &&
3234 meshpeer->peer_linkid != 0)
3236 if (meshpeer->peer_rcode == 0)
3244 * Add a Mesh ID IE to a frame.
3247 ieee80211_add_meshid(uint8_t *frm, struct ieee80211vap *vap)
3249 struct ieee80211_mesh_state *ms = vap->iv_mesh;
3251 KASSERT(vap->iv_opmode == IEEE80211_M_MBSS, ("not a mbss vap"));
3253 *frm++ = IEEE80211_ELEMID_MESHID;
3254 *frm++ = ms->ms_idlen;
3255 memcpy(frm, ms->ms_id, ms->ms_idlen);
3256 return frm + ms->ms_idlen;
3260 * Add a Mesh Configuration IE to a frame.
3261 * For now just use HWMP routing, Airtime link metric, Null Congestion
3262 * Signaling, Null Sync Protocol and Null Authentication.
3265 ieee80211_add_meshconf(uint8_t *frm, struct ieee80211vap *vap)
3267 const struct ieee80211_mesh_state *ms = vap->iv_mesh;
3270 KASSERT(vap->iv_opmode == IEEE80211_M_MBSS, ("not a MBSS vap"));
3272 *frm++ = IEEE80211_ELEMID_MESHCONF;
3273 *frm++ = IEEE80211_MESH_CONF_SZ;
3274 *frm++ = ms->ms_ppath->mpp_ie; /* path selection */
3275 *frm++ = ms->ms_pmetric->mpm_ie; /* link metric */
3276 *frm++ = IEEE80211_MESHCONF_CC_DISABLED;
3277 *frm++ = IEEE80211_MESHCONF_SYNC_NEIGHOFF;
3278 *frm++ = IEEE80211_MESHCONF_AUTH_DISABLED;
3279 /* NB: set the number of neighbors before the rest */
3280 *frm = (ms->ms_neighbors > IEEE80211_MESH_MAX_NEIGHBORS ?
3281 IEEE80211_MESH_MAX_NEIGHBORS : ms->ms_neighbors) << 1;
3282 if (ms->ms_flags & IEEE80211_MESHFLAGS_GATE)
3283 *frm |= IEEE80211_MESHCONF_FORM_GATE;
3286 if (ms->ms_flags & IEEE80211_MESHFLAGS_AP)
3287 caps |= IEEE80211_MESHCONF_CAP_AP;
3288 if (ms->ms_flags & IEEE80211_MESHFLAGS_FWD)
3289 caps |= IEEE80211_MESHCONF_CAP_FWRD;
3295 * Add a Mesh Peer Management IE to a frame.
3298 ieee80211_add_meshpeer(uint8_t *frm, uint8_t subtype, uint16_t localid,
3299 uint16_t peerid, uint16_t reason)
3302 KASSERT(localid != 0, ("localid == 0"));
3304 *frm++ = IEEE80211_ELEMID_MESHPEER;
3306 case IEEE80211_ACTION_MESHPEERING_OPEN:
3307 *frm++ = IEEE80211_MPM_BASE_SZ; /* length */
3308 ADDSHORT(frm, IEEE80211_MPPID_MPM); /* proto */
3309 ADDSHORT(frm, localid); /* local ID */
3311 case IEEE80211_ACTION_MESHPEERING_CONFIRM:
3312 KASSERT(peerid != 0, ("sending peer confirm without peer id"));
3313 *frm++ = IEEE80211_MPM_BASE_SZ + 2; /* length */
3314 ADDSHORT(frm, IEEE80211_MPPID_MPM); /* proto */
3315 ADDSHORT(frm, localid); /* local ID */
3316 ADDSHORT(frm, peerid); /* peer ID */
3318 case IEEE80211_ACTION_MESHPEERING_CLOSE:
3320 *frm++ = IEEE80211_MPM_MAX_SZ; /* length */
3322 *frm++ = IEEE80211_MPM_BASE_SZ + 2; /* length */
3323 ADDSHORT(frm, IEEE80211_MPPID_MPM); /* proto */
3324 ADDSHORT(frm, localid); /* local ID */
3326 ADDSHORT(frm, peerid); /* peer ID */
3327 ADDSHORT(frm, reason);
3334 * Compute an Airtime Link Metric for the link with this node.
3336 * Based on Draft 3.0 spec (11B.10, p.149).
3339 * Max 802.11s overhead.
3341 #define IEEE80211_MESH_MAXOVERHEAD \
3342 (sizeof(struct ieee80211_qosframe_addr4) \
3343 + sizeof(struct ieee80211_meshcntl_ae10) \
3344 + sizeof(struct llc) \
3345 + IEEE80211_ADDR_LEN \
3346 + IEEE80211_WEP_IVLEN \
3347 + IEEE80211_WEP_KIDLEN \
3348 + IEEE80211_WEP_CRCLEN \
3349 + IEEE80211_WEP_MICLEN \
3350 + IEEE80211_CRC_LEN)
3352 mesh_airtime_calc(struct ieee80211_node *ni)
3355 #define S_FACTOR (2 * M_BITS)
3356 struct ieee80211com *ic = ni->ni_ic;
3357 struct ifnet *ifp = ni->ni_vap->iv_ifp;
3358 static const int nbits = 8192 << M_BITS;
3359 uint32_t overhead, rate, errrate;
3362 /* Time to transmit a frame */
3363 rate = ni->ni_txrate;
3364 overhead = ieee80211_compute_duration(ic->ic_rt,
3365 ifp->if_mtu + IEEE80211_MESH_MAXOVERHEAD, rate, 0) << M_BITS;
3366 /* Error rate in percentage */
3367 /* XXX assuming small failures are ok */
3368 #if defined(__DragonFly__)
3371 IFNET_STAT_GET(ifp, ierrors, icount);
3372 IFNET_STAT_GET(ifp, oerrors, ocount);
3373 errrate = (((ocount + icount) / 100) << M_BITS)
3376 errrate = (((ifp->if_get_counter(ifp, IFCOUNTER_OERRORS) +
3377 ifp->if_get_counter(ifp, IFCOUNTER_IERRORS)) / 100) << M_BITS)
3380 res = (overhead + (nbits / rate)) *
3381 ((1 << S_FACTOR) / ((1 << M_BITS) - errrate));
3383 return (uint32_t)(res >> S_FACTOR);
3389 * Add a Mesh Link Metric report IE to a frame.
3392 ieee80211_add_meshlmetric(uint8_t *frm, uint8_t flags, uint32_t metric)
3394 *frm++ = IEEE80211_ELEMID_MESHLINK;
3397 ADDWORD(frm, metric);
3402 * Add a Mesh Gate Announcement IE to a frame.
3405 ieee80211_add_meshgate(uint8_t *frm, struct ieee80211_meshgann_ie *ie)
3407 *frm++ = IEEE80211_ELEMID_MESHGANN; /* ie */
3408 *frm++ = IEEE80211_MESHGANN_BASE_SZ; /* len */
3409 *frm++ = ie->gann_flags;
3410 *frm++ = ie->gann_hopcount;
3411 *frm++ = ie->gann_ttl;
3412 IEEE80211_ADDR_COPY(frm, ie->gann_addr);
3414 ADDWORD(frm, ie->gann_seq);
3415 ADDSHORT(frm, ie->gann_interval);
3422 * Initialize any mesh-specific node state.
3425 ieee80211_mesh_node_init(struct ieee80211vap *vap, struct ieee80211_node *ni)
3427 ni->ni_flags |= IEEE80211_NODE_QOS;
3428 callout_init_mp(&ni->ni_mltimer);
3429 callout_init_mp(&ni->ni_mlhtimer);
3433 * Cleanup any mesh-specific node state.
3436 ieee80211_mesh_node_cleanup(struct ieee80211_node *ni)
3438 struct ieee80211vap *vap = ni->ni_vap;
3439 struct ieee80211_mesh_state *ms = vap->iv_mesh;
3441 callout_drain(&ni->ni_mltimer);
3442 callout_drain(&ni->ni_mlhtimer);
3443 /* NB: short-circuit callbacks after mesh_vdetach */
3444 if (vap->iv_mesh != NULL)
3445 ms->ms_ppath->mpp_peerdown(ni);
3449 ieee80211_parse_meshid(struct ieee80211_node *ni, const uint8_t *ie)
3451 ni->ni_meshidlen = ie[1];
3452 memcpy(ni->ni_meshid, ie + 2, ie[1]);
3456 * Setup mesh-specific node state on neighbor discovery.
3459 ieee80211_mesh_init_neighbor(struct ieee80211_node *ni,
3460 const struct ieee80211_frame *wh,
3461 const struct ieee80211_scanparams *sp)
3463 ieee80211_parse_meshid(ni, sp->meshid);
3467 ieee80211_mesh_update_beacon(struct ieee80211vap *vap,
3468 struct ieee80211_beacon_offsets *bo)
3470 KASSERT(vap->iv_opmode == IEEE80211_M_MBSS, ("not a MBSS vap"));
3472 if (isset(bo->bo_flags, IEEE80211_BEACON_MESHCONF)) {
3473 (void)ieee80211_add_meshconf(bo->bo_meshconf, vap);
3474 clrbit(bo->bo_flags, IEEE80211_BEACON_MESHCONF);
3479 mesh_ioctl_get80211(struct ieee80211vap *vap, struct ieee80211req *ireq)
3481 struct ieee80211_mesh_state *ms = vap->iv_mesh;
3482 uint8_t tmpmeshid[IEEE80211_NWID_LEN];
3483 struct ieee80211_mesh_route *rt;
3484 struct ieee80211req_mesh_route *imr;
3489 if (vap->iv_opmode != IEEE80211_M_MBSS)
3493 switch (ireq->i_type) {
3494 case IEEE80211_IOC_MESH_ID:
3495 ireq->i_len = ms->ms_idlen;
3496 memcpy(tmpmeshid, ms->ms_id, ireq->i_len);
3497 error = copyout(tmpmeshid, ireq->i_data, ireq->i_len);
3499 case IEEE80211_IOC_MESH_AP:
3500 ireq->i_val = (ms->ms_flags & IEEE80211_MESHFLAGS_AP) != 0;
3502 case IEEE80211_IOC_MESH_FWRD:
3503 ireq->i_val = (ms->ms_flags & IEEE80211_MESHFLAGS_FWD) != 0;
3505 case IEEE80211_IOC_MESH_GATE:
3506 ireq->i_val = (ms->ms_flags & IEEE80211_MESHFLAGS_GATE) != 0;
3508 case IEEE80211_IOC_MESH_TTL:
3509 ireq->i_val = ms->ms_ttl;
3511 case IEEE80211_IOC_MESH_RTCMD:
3512 switch (ireq->i_val) {
3513 case IEEE80211_MESH_RTCMD_LIST:
3516 TAILQ_FOREACH(rt, &ms->ms_routes, rt_next) {
3517 len += sizeof(*imr);
3520 if (len > ireq->i_len || ireq->i_len < sizeof(*imr)) {
3526 p = kmalloc(len, M_TEMP, M_INTWAIT | M_ZERO);
3531 TAILQ_FOREACH(rt, &ms->ms_routes, rt_next) {
3534 imr = (struct ieee80211req_mesh_route *)
3536 IEEE80211_ADDR_COPY(imr->imr_dest,
3538 IEEE80211_ADDR_COPY(imr->imr_nexthop,
3540 imr->imr_metric = rt->rt_metric;
3541 imr->imr_nhops = rt->rt_nhops;
3543 ieee80211_mesh_rt_update(rt, 0);
3544 imr->imr_lastmseq = rt->rt_lastmseq;
3545 imr->imr_flags = rt->rt_flags; /* last */
3546 off += sizeof(*imr);
3549 error = copyout(p, (uint8_t *)ireq->i_data,
3553 case IEEE80211_MESH_RTCMD_FLUSH:
3554 case IEEE80211_MESH_RTCMD_ADD:
3555 case IEEE80211_MESH_RTCMD_DELETE:
3561 case IEEE80211_IOC_MESH_PR_METRIC:
3562 len = strlen(ms->ms_pmetric->mpm_descr);
3563 if (ireq->i_len < len)
3566 error = copyout(ms->ms_pmetric->mpm_descr,
3567 (uint8_t *)ireq->i_data, len);
3569 case IEEE80211_IOC_MESH_PR_PATH:
3570 len = strlen(ms->ms_ppath->mpp_descr);
3571 if (ireq->i_len < len)
3574 error = copyout(ms->ms_ppath->mpp_descr,
3575 (uint8_t *)ireq->i_data, len);
3583 IEEE80211_IOCTL_GET(mesh, mesh_ioctl_get80211);
3586 mesh_ioctl_set80211(struct ieee80211vap *vap, struct ieee80211req *ireq)
3588 struct ieee80211_mesh_state *ms = vap->iv_mesh;
3589 uint8_t tmpmeshid[IEEE80211_NWID_LEN];
3590 uint8_t tmpaddr[IEEE80211_ADDR_LEN];
3591 char tmpproto[IEEE80211_MESH_PROTO_DSZ];
3594 if (vap->iv_opmode != IEEE80211_M_MBSS)
3598 switch (ireq->i_type) {
3599 case IEEE80211_IOC_MESH_ID:
3600 if (ireq->i_val != 0 || ireq->i_len > IEEE80211_MESHID_LEN)
3602 error = copyin(ireq->i_data, tmpmeshid, ireq->i_len);
3605 memset(ms->ms_id, 0, IEEE80211_NWID_LEN);
3606 ms->ms_idlen = ireq->i_len;
3607 memcpy(ms->ms_id, tmpmeshid, ireq->i_len);
3610 case IEEE80211_IOC_MESH_AP:
3612 ms->ms_flags |= IEEE80211_MESHFLAGS_AP;
3614 ms->ms_flags &= ~IEEE80211_MESHFLAGS_AP;
3617 case IEEE80211_IOC_MESH_FWRD:
3619 ms->ms_flags |= IEEE80211_MESHFLAGS_FWD;
3621 ms->ms_flags &= ~IEEE80211_MESHFLAGS_FWD;
3622 mesh_gatemode_setup(vap);
3624 case IEEE80211_IOC_MESH_GATE:
3626 ms->ms_flags |= IEEE80211_MESHFLAGS_GATE;
3628 ms->ms_flags &= ~IEEE80211_MESHFLAGS_GATE;
3630 case IEEE80211_IOC_MESH_TTL:
3631 ms->ms_ttl = (uint8_t) ireq->i_val;
3633 case IEEE80211_IOC_MESH_RTCMD:
3634 switch (ireq->i_val) {
3635 case IEEE80211_MESH_RTCMD_LIST:
3637 case IEEE80211_MESH_RTCMD_FLUSH:
3638 ieee80211_mesh_rt_flush(vap);
3640 case IEEE80211_MESH_RTCMD_ADD:
3641 if (IEEE80211_ADDR_EQ(vap->iv_myaddr, ireq->i_data) ||
3642 IEEE80211_ADDR_EQ(broadcastaddr, ireq->i_data))
3644 error = copyin(ireq->i_data, &tmpaddr,
3645 IEEE80211_ADDR_LEN);
3647 ieee80211_mesh_discover(vap, tmpaddr, NULL);
3649 case IEEE80211_MESH_RTCMD_DELETE:
3650 ieee80211_mesh_rt_del(vap, ireq->i_data);
3656 case IEEE80211_IOC_MESH_PR_METRIC:
3657 error = copyin(ireq->i_data, tmpproto, sizeof(tmpproto));
3659 error = mesh_select_proto_metric(vap, tmpproto);
3664 case IEEE80211_IOC_MESH_PR_PATH:
3665 error = copyin(ireq->i_data, tmpproto, sizeof(tmpproto));
3667 error = mesh_select_proto_path(vap, tmpproto);
3677 IEEE80211_IOCTL_SET(mesh, mesh_ioctl_set80211);