2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2009 The FreeBSD Foundation
7 * This software was developed by Rui Paulo under sponsorship from the
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
37 * IEEE 802.11s Mesh Point (MBSS) support.
39 * Based on March 2009, D3.0 802.11s draft spec.
44 #include <sys/param.h>
45 #include <sys/systm.h>
47 #include <sys/malloc.h>
48 #include <sys/kernel.h>
50 #include <sys/socket.h>
51 #include <sys/sockio.h>
52 #include <sys/endian.h>
53 #include <sys/errno.h>
55 #include <sys/sysctl.h>
59 #include <net/if_var.h>
60 #include <net/if_media.h>
61 #include <net/if_llc.h>
62 #include <net/ethernet.h>
64 #include <net80211/ieee80211_var.h>
65 #include <net80211/ieee80211_action.h>
66 #ifdef IEEE80211_SUPPORT_SUPERG
67 #include <net80211/ieee80211_superg.h>
69 #include <net80211/ieee80211_input.h>
70 #include <net80211/ieee80211_mesh.h>
72 static void mesh_rt_flush_invalid(struct ieee80211vap *);
73 static int mesh_select_proto_path(struct ieee80211vap *, const char *);
74 static int mesh_select_proto_metric(struct ieee80211vap *, const char *);
75 static void mesh_vattach(struct ieee80211vap *);
76 static int mesh_newstate(struct ieee80211vap *, enum ieee80211_state, int);
77 static void mesh_rt_cleanup_cb(void *);
78 static void mesh_gatemode_setup(struct ieee80211vap *);
79 static void mesh_gatemode_cb(void *);
80 static void mesh_linkchange(struct ieee80211_node *,
81 enum ieee80211_mesh_mlstate);
82 static void mesh_checkid(void *, struct ieee80211_node *);
83 static uint32_t mesh_generateid(struct ieee80211vap *);
84 static int mesh_checkpseq(struct ieee80211vap *,
85 const uint8_t [IEEE80211_ADDR_LEN], uint32_t);
86 static void mesh_transmit_to_gate(struct ieee80211vap *, struct mbuf *,
87 struct ieee80211_mesh_route *);
88 static void mesh_forward(struct ieee80211vap *, struct mbuf *,
89 const struct ieee80211_meshcntl *);
90 static int mesh_input(struct ieee80211_node *, struct mbuf *,
91 const struct ieee80211_rx_stats *rxs, int, int);
92 static void mesh_recv_mgmt(struct ieee80211_node *, struct mbuf *, int,
93 const struct ieee80211_rx_stats *rxs, int, int);
94 static void mesh_recv_ctl(struct ieee80211_node *, struct mbuf *, int);
95 static void mesh_peer_timeout_setup(struct ieee80211_node *);
96 static void mesh_peer_timeout_backoff(struct ieee80211_node *);
97 static void mesh_peer_timeout_cb(void *);
99 mesh_peer_timeout_stop(struct ieee80211_node *);
100 static int mesh_verify_meshid(struct ieee80211vap *, const uint8_t *);
101 static int mesh_verify_meshconf(struct ieee80211vap *, const uint8_t *);
102 static int mesh_verify_meshpeer(struct ieee80211vap *, uint8_t,
104 uint32_t mesh_airtime_calc(struct ieee80211_node *);
107 * Timeout values come from the specification and are in milliseconds.
109 static SYSCTL_NODE(_net_wlan, OID_AUTO, mesh, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
110 "IEEE 802.11s parameters");
111 static int ieee80211_mesh_gateint = -1;
112 SYSCTL_PROC(_net_wlan_mesh, OID_AUTO, gateint,
113 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
114 &ieee80211_mesh_gateint, 0, ieee80211_sysctl_msecs_ticks, "I",
115 "mesh gate interval (ms)");
116 static int ieee80211_mesh_retrytimeout = -1;
117 SYSCTL_PROC(_net_wlan_mesh, OID_AUTO, retrytimeout,
118 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
119 &ieee80211_mesh_retrytimeout, 0, ieee80211_sysctl_msecs_ticks, "I",
120 "Retry timeout (msec)");
121 static int ieee80211_mesh_holdingtimeout = -1;
123 SYSCTL_PROC(_net_wlan_mesh, OID_AUTO, holdingtimeout,
124 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
125 &ieee80211_mesh_holdingtimeout, 0, ieee80211_sysctl_msecs_ticks, "I",
126 "Holding state timeout (msec)");
127 static int ieee80211_mesh_confirmtimeout = -1;
128 SYSCTL_PROC(_net_wlan_mesh, OID_AUTO, confirmtimeout,
129 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
130 &ieee80211_mesh_confirmtimeout, 0, ieee80211_sysctl_msecs_ticks, "I",
131 "Confirm state timeout (msec)");
132 static int ieee80211_mesh_backofftimeout = -1;
133 SYSCTL_PROC(_net_wlan_mesh, OID_AUTO, backofftimeout,
134 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
135 &ieee80211_mesh_backofftimeout, 0, ieee80211_sysctl_msecs_ticks, "I",
136 "Backoff timeout (msec). This is to throutles peering forever when "
137 "not receiving answer or is rejected by a neighbor");
138 static int ieee80211_mesh_maxretries = 2;
139 SYSCTL_INT(_net_wlan_mesh, OID_AUTO, maxretries, CTLFLAG_RW,
140 &ieee80211_mesh_maxretries, 0,
141 "Maximum retries during peer link establishment");
142 static int ieee80211_mesh_maxholding = 2;
143 SYSCTL_INT(_net_wlan_mesh, OID_AUTO, maxholding, CTLFLAG_RW,
144 &ieee80211_mesh_maxholding, 0,
145 "Maximum times we are allowed to transition to HOLDING state before "
146 "backinoff during peer link establishment");
148 static const uint8_t broadcastaddr[IEEE80211_ADDR_LEN] =
149 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
151 static ieee80211_recv_action_func mesh_recv_action_meshpeering_open;
152 static ieee80211_recv_action_func mesh_recv_action_meshpeering_confirm;
153 static ieee80211_recv_action_func mesh_recv_action_meshpeering_close;
154 static ieee80211_recv_action_func mesh_recv_action_meshlmetric;
155 static ieee80211_recv_action_func mesh_recv_action_meshgate;
157 static ieee80211_send_action_func mesh_send_action_meshpeering_open;
158 static ieee80211_send_action_func mesh_send_action_meshpeering_confirm;
159 static ieee80211_send_action_func mesh_send_action_meshpeering_close;
160 static ieee80211_send_action_func mesh_send_action_meshlmetric;
161 static ieee80211_send_action_func mesh_send_action_meshgate;
163 static const struct ieee80211_mesh_proto_metric mesh_metric_airtime = {
164 .mpm_descr = "AIRTIME",
165 .mpm_ie = IEEE80211_MESHCONF_METRIC_AIRTIME,
166 .mpm_metric = mesh_airtime_calc,
169 static struct ieee80211_mesh_proto_path mesh_proto_paths[4];
170 static struct ieee80211_mesh_proto_metric mesh_proto_metrics[4];
172 MALLOC_DEFINE(M_80211_MESH_PREQ, "80211preq", "802.11 MESH Path Request frame");
173 MALLOC_DEFINE(M_80211_MESH_PREP, "80211prep", "802.11 MESH Path Reply frame");
174 MALLOC_DEFINE(M_80211_MESH_PERR, "80211perr", "802.11 MESH Path Error frame");
176 /* The longer one of the lifetime should be stored as new lifetime */
177 #define MESH_ROUTE_LIFETIME_MAX(a, b) (a > b ? a : b)
179 MALLOC_DEFINE(M_80211_MESH_RT, "80211mesh_rt", "802.11s routing table");
180 MALLOC_DEFINE(M_80211_MESH_GT_RT, "80211mesh_gt", "802.11s known gates table");
183 * Helper functions to manipulate the Mesh routing table.
186 static struct ieee80211_mesh_route *
187 mesh_rt_find_locked(struct ieee80211_mesh_state *ms,
188 const uint8_t dest[IEEE80211_ADDR_LEN])
190 struct ieee80211_mesh_route *rt;
192 MESH_RT_LOCK_ASSERT(ms);
194 TAILQ_FOREACH(rt, &ms->ms_routes, rt_next) {
195 if (IEEE80211_ADDR_EQ(dest, rt->rt_dest))
201 static struct ieee80211_mesh_route *
202 mesh_rt_add_locked(struct ieee80211vap *vap,
203 const uint8_t dest[IEEE80211_ADDR_LEN])
205 struct ieee80211_mesh_state *ms = vap->iv_mesh;
206 struct ieee80211_mesh_route *rt;
208 KASSERT(!IEEE80211_ADDR_EQ(broadcastaddr, dest),
209 ("%s: adding broadcast to the routing table", __func__));
211 MESH_RT_LOCK_ASSERT(ms);
213 rt = IEEE80211_MALLOC(ALIGN(sizeof(struct ieee80211_mesh_route)) +
214 ms->ms_ppath->mpp_privlen, M_80211_MESH_RT,
215 IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
218 IEEE80211_ADDR_COPY(rt->rt_dest, dest);
219 rt->rt_priv = (void *)ALIGN(&rt[1]);
220 MESH_RT_ENTRY_LOCK_INIT(rt, "MBSS_RT");
221 callout_init(&rt->rt_discovery, 1);
222 rt->rt_updtime = ticks; /* create time */
223 TAILQ_INSERT_TAIL(&ms->ms_routes, rt, rt_next);
228 struct ieee80211_mesh_route *
229 ieee80211_mesh_rt_find(struct ieee80211vap *vap,
230 const uint8_t dest[IEEE80211_ADDR_LEN])
232 struct ieee80211_mesh_state *ms = vap->iv_mesh;
233 struct ieee80211_mesh_route *rt;
236 rt = mesh_rt_find_locked(ms, dest);
241 struct ieee80211_mesh_route *
242 ieee80211_mesh_rt_add(struct ieee80211vap *vap,
243 const uint8_t dest[IEEE80211_ADDR_LEN])
245 struct ieee80211_mesh_state *ms = vap->iv_mesh;
246 struct ieee80211_mesh_route *rt;
248 KASSERT(ieee80211_mesh_rt_find(vap, dest) == NULL,
249 ("%s: duplicate entry in the routing table", __func__));
250 KASSERT(!IEEE80211_ADDR_EQ(vap->iv_myaddr, dest),
251 ("%s: adding self to the routing table", __func__));
254 rt = mesh_rt_add_locked(vap, dest);
260 * Update the route lifetime and returns the updated lifetime.
261 * If new_lifetime is zero and route is timedout it will be invalidated.
262 * new_lifetime is in msec
265 ieee80211_mesh_rt_update(struct ieee80211_mesh_route *rt, int new_lifetime)
268 uint32_t lifetime = 0;
270 KASSERT(rt != NULL, ("route is NULL"));
273 MESH_RT_ENTRY_LOCK(rt);
275 /* dont clobber a proxy entry gated by us */
276 if (rt->rt_flags & IEEE80211_MESHRT_FLAGS_PROXY && rt->rt_nhops == 0) {
277 MESH_RT_ENTRY_UNLOCK(rt);
278 return rt->rt_lifetime;
281 timesince = ticks_to_msecs(now - rt->rt_updtime);
282 rt->rt_updtime = now;
283 if (timesince >= rt->rt_lifetime) {
284 if (new_lifetime != 0) {
285 rt->rt_lifetime = new_lifetime;
288 rt->rt_flags &= ~IEEE80211_MESHRT_FLAGS_VALID;
292 /* update what is left of lifetime */
293 rt->rt_lifetime = rt->rt_lifetime - timesince;
294 rt->rt_lifetime = MESH_ROUTE_LIFETIME_MAX(
295 new_lifetime, rt->rt_lifetime);
297 lifetime = rt->rt_lifetime;
298 MESH_RT_ENTRY_UNLOCK(rt);
304 * Add a proxy route (as needed) for the specified destination.
307 ieee80211_mesh_proxy_check(struct ieee80211vap *vap,
308 const uint8_t dest[IEEE80211_ADDR_LEN])
310 struct ieee80211_mesh_state *ms = vap->iv_mesh;
311 struct ieee80211_mesh_route *rt;
314 rt = mesh_rt_find_locked(ms, dest);
316 rt = mesh_rt_add_locked(vap, dest);
318 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH, dest,
319 "%s", "unable to add proxy entry");
320 vap->iv_stats.is_mesh_rtaddfailed++;
322 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH, dest,
323 "%s", "add proxy entry");
324 IEEE80211_ADDR_COPY(rt->rt_mesh_gate, vap->iv_myaddr);
325 IEEE80211_ADDR_COPY(rt->rt_nexthop, vap->iv_myaddr);
326 rt->rt_flags |= IEEE80211_MESHRT_FLAGS_VALID
327 | IEEE80211_MESHRT_FLAGS_PROXY;
329 } else if ((rt->rt_flags & IEEE80211_MESHRT_FLAGS_VALID) == 0) {
330 KASSERT(rt->rt_flags & IEEE80211_MESHRT_FLAGS_PROXY,
331 ("no proxy flag for poxy entry"));
332 struct ieee80211com *ic = vap->iv_ic;
334 * Fix existing entry created by received frames from
335 * stations that have some memory of dest. We also
336 * flush any frames held on the staging queue; delivering
337 * them is too much trouble right now.
339 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH, dest,
340 "%s", "fix proxy entry");
341 IEEE80211_ADDR_COPY(rt->rt_nexthop, vap->iv_myaddr);
342 rt->rt_flags |= IEEE80211_MESHRT_FLAGS_VALID
343 | IEEE80211_MESHRT_FLAGS_PROXY;
344 /* XXX belongs in hwmp */
345 ieee80211_ageq_drain_node(&ic->ic_stageq,
346 (void *)(uintptr_t) ieee80211_mac_hash(ic, dest));
353 mesh_rt_del(struct ieee80211_mesh_state *ms, struct ieee80211_mesh_route *rt)
355 TAILQ_REMOVE(&ms->ms_routes, rt, rt_next);
357 * Grab the lock before destroying it, to be sure no one else
358 * is holding the route.
360 MESH_RT_ENTRY_LOCK(rt);
361 callout_drain(&rt->rt_discovery);
362 MESH_RT_ENTRY_LOCK_DESTROY(rt);
363 IEEE80211_FREE(rt, M_80211_MESH_RT);
367 ieee80211_mesh_rt_del(struct ieee80211vap *vap,
368 const uint8_t dest[IEEE80211_ADDR_LEN])
370 struct ieee80211_mesh_state *ms = vap->iv_mesh;
371 struct ieee80211_mesh_route *rt, *next;
374 TAILQ_FOREACH_SAFE(rt, &ms->ms_routes, rt_next, next) {
375 if (IEEE80211_ADDR_EQ(rt->rt_dest, dest)) {
376 if (rt->rt_flags & IEEE80211_MESHRT_FLAGS_PROXY) {
377 ms->ms_ppath->mpp_senderror(vap, dest, rt,
378 IEEE80211_REASON_MESH_PERR_NO_PROXY);
380 ms->ms_ppath->mpp_senderror(vap, dest, rt,
381 IEEE80211_REASON_MESH_PERR_DEST_UNREACH);
392 ieee80211_mesh_rt_flush(struct ieee80211vap *vap)
394 struct ieee80211_mesh_state *ms = vap->iv_mesh;
395 struct ieee80211_mesh_route *rt, *next;
400 TAILQ_FOREACH_SAFE(rt, &ms->ms_routes, rt_next, next)
406 ieee80211_mesh_rt_flush_peer(struct ieee80211vap *vap,
407 const uint8_t peer[IEEE80211_ADDR_LEN])
409 struct ieee80211_mesh_state *ms = vap->iv_mesh;
410 struct ieee80211_mesh_route *rt, *next;
413 TAILQ_FOREACH_SAFE(rt, &ms->ms_routes, rt_next, next) {
414 if (IEEE80211_ADDR_EQ(rt->rt_nexthop, peer))
421 * Flush expired routing entries, i.e. those in invalid state for
425 mesh_rt_flush_invalid(struct ieee80211vap *vap)
427 struct ieee80211_mesh_state *ms = vap->iv_mesh;
428 struct ieee80211_mesh_route *rt, *next;
433 TAILQ_FOREACH_SAFE(rt, &ms->ms_routes, rt_next, next) {
434 /* Discover paths will be deleted by their own callout */
435 if (rt->rt_flags & IEEE80211_MESHRT_FLAGS_DISCOVER)
437 ieee80211_mesh_rt_update(rt, 0);
438 if ((rt->rt_flags & IEEE80211_MESHRT_FLAGS_VALID) == 0)
445 ieee80211_mesh_register_proto_path(const struct ieee80211_mesh_proto_path *mpp)
447 int i, firstempty = -1;
449 for (i = 0; i < nitems(mesh_proto_paths); i++) {
450 if (strncmp(mpp->mpp_descr, mesh_proto_paths[i].mpp_descr,
451 IEEE80211_MESH_PROTO_DSZ) == 0)
453 if (!mesh_proto_paths[i].mpp_active && firstempty == -1)
458 memcpy(&mesh_proto_paths[firstempty], mpp, sizeof(*mpp));
459 mesh_proto_paths[firstempty].mpp_active = 1;
464 ieee80211_mesh_register_proto_metric(const struct
465 ieee80211_mesh_proto_metric *mpm)
467 int i, firstempty = -1;
469 for (i = 0; i < nitems(mesh_proto_metrics); i++) {
470 if (strncmp(mpm->mpm_descr, mesh_proto_metrics[i].mpm_descr,
471 IEEE80211_MESH_PROTO_DSZ) == 0)
473 if (!mesh_proto_metrics[i].mpm_active && firstempty == -1)
478 memcpy(&mesh_proto_metrics[firstempty], mpm, sizeof(*mpm));
479 mesh_proto_metrics[firstempty].mpm_active = 1;
484 mesh_select_proto_path(struct ieee80211vap *vap, const char *name)
486 struct ieee80211_mesh_state *ms = vap->iv_mesh;
489 for (i = 0; i < nitems(mesh_proto_paths); i++) {
490 if (strcasecmp(mesh_proto_paths[i].mpp_descr, name) == 0) {
491 ms->ms_ppath = &mesh_proto_paths[i];
499 mesh_select_proto_metric(struct ieee80211vap *vap, const char *name)
501 struct ieee80211_mesh_state *ms = vap->iv_mesh;
504 for (i = 0; i < nitems(mesh_proto_metrics); i++) {
505 if (strcasecmp(mesh_proto_metrics[i].mpm_descr, name) == 0) {
506 ms->ms_pmetric = &mesh_proto_metrics[i];
514 mesh_gatemode_setup(struct ieee80211vap *vap)
516 struct ieee80211_mesh_state *ms = vap->iv_mesh;
519 * NB: When a mesh gate is running as a ROOT it shall
520 * not send out periodic GANNs but instead mark the
521 * mesh gate flag for the corresponding proactive PREQ
524 if (ms->ms_flags & IEEE80211_MESHFLAGS_ROOT ||
525 (ms->ms_flags & IEEE80211_MESHFLAGS_GATE) == 0) {
526 callout_drain(&ms->ms_gatetimer);
529 callout_reset(&ms->ms_gatetimer, ieee80211_mesh_gateint,
530 mesh_gatemode_cb, vap);
534 mesh_gatemode_cb(void *arg)
536 struct ieee80211vap *vap = (struct ieee80211vap *)arg;
537 struct ieee80211_mesh_state *ms = vap->iv_mesh;
538 struct ieee80211_meshgann_ie gann;
540 gann.gann_flags = 0; /* Reserved */
541 gann.gann_hopcount = 0;
542 gann.gann_ttl = ms->ms_ttl;
543 IEEE80211_ADDR_COPY(gann.gann_addr, vap->iv_myaddr);
544 gann.gann_seq = ms->ms_gateseq++;
545 gann.gann_interval = ieee80211_mesh_gateint;
547 IEEE80211_NOTE(vap, IEEE80211_MSG_MESH, vap->iv_bss,
548 "send broadcast GANN (seq %u)", gann.gann_seq);
550 ieee80211_send_action(vap->iv_bss, IEEE80211_ACTION_CAT_MESH,
551 IEEE80211_ACTION_MESH_GANN, &gann);
552 mesh_gatemode_setup(vap);
556 ieee80211_mesh_init(void)
559 memset(mesh_proto_paths, 0, sizeof(mesh_proto_paths));
560 memset(mesh_proto_metrics, 0, sizeof(mesh_proto_metrics));
563 * Setup mesh parameters that depends on the clock frequency.
565 ieee80211_mesh_gateint = msecs_to_ticks(10000);
566 ieee80211_mesh_retrytimeout = msecs_to_ticks(40);
567 ieee80211_mesh_holdingtimeout = msecs_to_ticks(40);
568 ieee80211_mesh_confirmtimeout = msecs_to_ticks(40);
569 ieee80211_mesh_backofftimeout = msecs_to_ticks(5000);
572 * Register action frame handlers.
574 ieee80211_recv_action_register(IEEE80211_ACTION_CAT_SELF_PROT,
575 IEEE80211_ACTION_MESHPEERING_OPEN,
576 mesh_recv_action_meshpeering_open);
577 ieee80211_recv_action_register(IEEE80211_ACTION_CAT_SELF_PROT,
578 IEEE80211_ACTION_MESHPEERING_CONFIRM,
579 mesh_recv_action_meshpeering_confirm);
580 ieee80211_recv_action_register(IEEE80211_ACTION_CAT_SELF_PROT,
581 IEEE80211_ACTION_MESHPEERING_CLOSE,
582 mesh_recv_action_meshpeering_close);
583 ieee80211_recv_action_register(IEEE80211_ACTION_CAT_MESH,
584 IEEE80211_ACTION_MESH_LMETRIC, mesh_recv_action_meshlmetric);
585 ieee80211_recv_action_register(IEEE80211_ACTION_CAT_MESH,
586 IEEE80211_ACTION_MESH_GANN, mesh_recv_action_meshgate);
588 ieee80211_send_action_register(IEEE80211_ACTION_CAT_SELF_PROT,
589 IEEE80211_ACTION_MESHPEERING_OPEN,
590 mesh_send_action_meshpeering_open);
591 ieee80211_send_action_register(IEEE80211_ACTION_CAT_SELF_PROT,
592 IEEE80211_ACTION_MESHPEERING_CONFIRM,
593 mesh_send_action_meshpeering_confirm);
594 ieee80211_send_action_register(IEEE80211_ACTION_CAT_SELF_PROT,
595 IEEE80211_ACTION_MESHPEERING_CLOSE,
596 mesh_send_action_meshpeering_close);
597 ieee80211_send_action_register(IEEE80211_ACTION_CAT_MESH,
598 IEEE80211_ACTION_MESH_LMETRIC,
599 mesh_send_action_meshlmetric);
600 ieee80211_send_action_register(IEEE80211_ACTION_CAT_MESH,
601 IEEE80211_ACTION_MESH_GANN,
602 mesh_send_action_meshgate);
605 * Register Airtime Link Metric.
607 ieee80211_mesh_register_proto_metric(&mesh_metric_airtime);
610 SYSINIT(wlan_mesh, SI_SUB_DRIVERS, SI_ORDER_FIRST, ieee80211_mesh_init, NULL);
613 ieee80211_mesh_attach(struct ieee80211com *ic)
615 ic->ic_vattach[IEEE80211_M_MBSS] = mesh_vattach;
619 ieee80211_mesh_detach(struct ieee80211com *ic)
624 mesh_vdetach_peers(void *arg, struct ieee80211_node *ni)
626 struct ieee80211com *ic = ni->ni_ic;
629 if (ni->ni_mlstate == IEEE80211_NODE_MESH_ESTABLISHED) {
630 args[0] = ni->ni_mlpid;
631 args[1] = ni->ni_mllid;
632 args[2] = IEEE80211_REASON_PEER_LINK_CANCELED;
633 ieee80211_send_action(ni,
634 IEEE80211_ACTION_CAT_SELF_PROT,
635 IEEE80211_ACTION_MESHPEERING_CLOSE,
638 callout_drain(&ni->ni_mltimer);
639 /* XXX belongs in hwmp */
640 ieee80211_ageq_drain_node(&ic->ic_stageq,
641 (void *)(uintptr_t) ieee80211_mac_hash(ic, ni->ni_macaddr));
645 mesh_vdetach(struct ieee80211vap *vap)
647 struct ieee80211_mesh_state *ms = vap->iv_mesh;
649 callout_drain(&ms->ms_cleantimer);
650 ieee80211_iterate_nodes(&vap->iv_ic->ic_sta, mesh_vdetach_peers,
652 ieee80211_mesh_rt_flush(vap);
653 MESH_RT_LOCK_DESTROY(ms);
654 ms->ms_ppath->mpp_vdetach(vap);
655 IEEE80211_FREE(vap->iv_mesh, M_80211_VAP);
660 mesh_vattach(struct ieee80211vap *vap)
662 struct ieee80211_mesh_state *ms;
663 vap->iv_newstate = mesh_newstate;
664 vap->iv_input = mesh_input;
665 vap->iv_opdetach = mesh_vdetach;
666 vap->iv_recv_mgmt = mesh_recv_mgmt;
667 vap->iv_recv_ctl = mesh_recv_ctl;
668 ms = IEEE80211_MALLOC(sizeof(struct ieee80211_mesh_state), M_80211_VAP,
669 IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
671 printf("%s: couldn't alloc MBSS state\n", __func__);
676 ms->ms_flags = (IEEE80211_MESHFLAGS_AP | IEEE80211_MESHFLAGS_FWD);
677 ms->ms_ttl = IEEE80211_MESH_DEFAULT_TTL;
678 TAILQ_INIT(&ms->ms_known_gates);
679 TAILQ_INIT(&ms->ms_routes);
680 MESH_RT_LOCK_INIT(ms, "MBSS");
681 callout_init(&ms->ms_cleantimer, 1);
682 callout_init(&ms->ms_gatetimer, 1);
684 mesh_select_proto_metric(vap, "AIRTIME");
685 KASSERT(ms->ms_pmetric, ("ms_pmetric == NULL"));
686 mesh_select_proto_path(vap, "HWMP");
687 KASSERT(ms->ms_ppath, ("ms_ppath == NULL"));
688 ms->ms_ppath->mpp_vattach(vap);
692 * IEEE80211_M_MBSS vap state machine handler.
695 mesh_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
697 struct ieee80211_mesh_state *ms = vap->iv_mesh;
698 struct ieee80211com *ic = vap->iv_ic;
699 struct ieee80211_node *ni;
700 enum ieee80211_state ostate;
702 IEEE80211_LOCK_ASSERT(ic);
704 ostate = vap->iv_state;
705 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE, "%s: %s -> %s (%d)\n",
706 __func__, ieee80211_state_name[ostate],
707 ieee80211_state_name[nstate], arg);
708 vap->iv_state = nstate; /* state transition */
709 if (ostate != IEEE80211_S_SCAN)
710 ieee80211_cancel_scan(vap); /* background scan */
711 ni = vap->iv_bss; /* NB: no reference held */
712 if (nstate != IEEE80211_S_RUN && ostate == IEEE80211_S_RUN) {
713 callout_drain(&ms->ms_cleantimer);
714 callout_drain(&ms->ms_gatetimer);
717 case IEEE80211_S_INIT:
719 case IEEE80211_S_SCAN:
720 ieee80211_cancel_scan(vap);
722 case IEEE80211_S_CAC:
723 ieee80211_dfs_cac_stop(vap);
725 case IEEE80211_S_RUN:
726 ieee80211_iterate_nodes(&ic->ic_sta,
727 mesh_vdetach_peers, NULL);
732 if (ostate != IEEE80211_S_INIT) {
733 /* NB: optimize INIT -> INIT case */
734 ieee80211_reset_bss(vap);
735 ieee80211_mesh_rt_flush(vap);
738 case IEEE80211_S_SCAN:
740 case IEEE80211_S_INIT:
741 if (vap->iv_des_chan != IEEE80211_CHAN_ANYC &&
742 !IEEE80211_IS_CHAN_RADAR(vap->iv_des_chan) &&
745 * Already have a channel and a mesh ID; bypass
746 * the scan and startup immediately.
748 ieee80211_create_ibss(vap, vap->iv_des_chan);
752 * Initiate a scan. We can come here as a result
753 * of an IEEE80211_IOC_SCAN_REQ too in which case
754 * the vap will be marked with IEEE80211_FEXT_SCANREQ
755 * and the scan request parameters will be present
756 * in iv_scanreq. Otherwise we do the default.
758 if (vap->iv_flags_ext & IEEE80211_FEXT_SCANREQ) {
759 ieee80211_check_scan(vap,
760 vap->iv_scanreq_flags,
761 vap->iv_scanreq_duration,
762 vap->iv_scanreq_mindwell,
763 vap->iv_scanreq_maxdwell,
764 vap->iv_scanreq_nssid, vap->iv_scanreq_ssid);
765 vap->iv_flags_ext &= ~IEEE80211_FEXT_SCANREQ;
767 ieee80211_check_scan_current(vap);
773 case IEEE80211_S_CAC:
775 * Start CAC on a DFS channel. We come here when starting
776 * a bss on a DFS channel (see ieee80211_create_ibss).
778 ieee80211_dfs_cac_start(vap);
780 case IEEE80211_S_RUN:
782 case IEEE80211_S_INIT:
784 * Already have a channel; bypass the
785 * scan and startup immediately.
786 * Note that ieee80211_create_ibss will call
787 * back to do a RUN->RUN state change.
789 ieee80211_create_ibss(vap,
790 ieee80211_ht_adjust_channel(ic,
791 ic->ic_curchan, vap->iv_flags_ht));
792 /* NB: iv_bss is changed on return */
794 case IEEE80211_S_CAC:
796 * NB: This is the normal state change when CAC
797 * expires and no radar was detected; no need to
798 * clear the CAC timer as it's already expired.
801 case IEEE80211_S_CSA:
804 * Shorten inactivity timer of associated stations
805 * to weed out sta's that don't follow a CSA.
807 ieee80211_iterate_nodes(&ic->ic_sta, sta_csa, vap);
810 * Update bss node channel to reflect where
811 * we landed after CSA.
813 ieee80211_node_set_chan(ni,
814 ieee80211_ht_adjust_channel(ic, ic->ic_curchan,
815 ieee80211_htchanflags(ni->ni_chan)));
816 /* XXX bypass debug msgs */
818 case IEEE80211_S_SCAN:
819 case IEEE80211_S_RUN:
820 #ifdef IEEE80211_DEBUG
821 if (ieee80211_msg_debug(vap)) {
823 "synchronized with %s meshid ",
824 ether_sprintf(ni->ni_meshid));
825 ieee80211_print_essid(ni->ni_meshid,
828 printf(" channel %d\n",
829 ieee80211_chan2ieee(ic, ic->ic_curchan));
836 ieee80211_node_authorize(ni);
837 callout_reset(&ms->ms_cleantimer, ms->ms_ppath->mpp_inact,
838 mesh_rt_cleanup_cb, vap);
839 mesh_gatemode_setup(vap);
844 /* NB: ostate not nstate */
845 ms->ms_ppath->mpp_newstate(vap, ostate, arg);
850 mesh_rt_cleanup_cb(void *arg)
852 struct ieee80211vap *vap = arg;
853 struct ieee80211_mesh_state *ms = vap->iv_mesh;
855 mesh_rt_flush_invalid(vap);
856 callout_reset(&ms->ms_cleantimer, ms->ms_ppath->mpp_inact,
857 mesh_rt_cleanup_cb, vap);
861 * Mark a mesh STA as gate and return a pointer to it.
862 * If this is first time, we create a new gate route.
863 * Always update the path route to this mesh gate.
865 struct ieee80211_mesh_gate_route *
866 ieee80211_mesh_mark_gate(struct ieee80211vap *vap, const uint8_t *addr,
867 struct ieee80211_mesh_route *rt)
869 struct ieee80211_mesh_state *ms = vap->iv_mesh;
870 struct ieee80211_mesh_gate_route *gr = NULL, *next;
874 TAILQ_FOREACH_SAFE(gr, &ms->ms_known_gates, gr_next, next) {
875 if (IEEE80211_ADDR_EQ(gr->gr_addr, addr)) {
882 /* New mesh gate add it to known table. */
883 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH, addr,
884 "%s", "stored new gate information from pro-PREQ.");
885 gr = IEEE80211_MALLOC(ALIGN(sizeof(struct ieee80211_mesh_gate_route)),
887 IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
888 IEEE80211_ADDR_COPY(gr->gr_addr, addr);
889 TAILQ_INSERT_TAIL(&ms->ms_known_gates, gr, gr_next);
892 /* TODO: link from path route to gate route */
900 * Helper function to note the Mesh Peer Link FSM change.
903 mesh_linkchange(struct ieee80211_node *ni, enum ieee80211_mesh_mlstate state)
905 struct ieee80211vap *vap = ni->ni_vap;
906 struct ieee80211_mesh_state *ms = vap->iv_mesh;
907 #ifdef IEEE80211_DEBUG
908 static const char *meshlinkstates[] = {
909 [IEEE80211_NODE_MESH_IDLE] = "IDLE",
910 [IEEE80211_NODE_MESH_OPENSNT] = "OPEN SENT",
911 [IEEE80211_NODE_MESH_OPENRCV] = "OPEN RECEIVED",
912 [IEEE80211_NODE_MESH_CONFIRMRCV] = "CONFIRM RECEIVED",
913 [IEEE80211_NODE_MESH_ESTABLISHED] = "ESTABLISHED",
914 [IEEE80211_NODE_MESH_HOLDING] = "HOLDING"
917 IEEE80211_NOTE(vap, IEEE80211_MSG_MESH,
918 ni, "peer link: %s -> %s",
919 meshlinkstates[ni->ni_mlstate], meshlinkstates[state]);
921 /* track neighbor count */
922 if (state == IEEE80211_NODE_MESH_ESTABLISHED &&
923 ni->ni_mlstate != IEEE80211_NODE_MESH_ESTABLISHED) {
924 KASSERT(ms->ms_neighbors < 65535, ("neighbor count overflow"));
926 ieee80211_beacon_notify(vap, IEEE80211_BEACON_MESHCONF);
927 } else if (ni->ni_mlstate == IEEE80211_NODE_MESH_ESTABLISHED &&
928 state != IEEE80211_NODE_MESH_ESTABLISHED) {
929 KASSERT(ms->ms_neighbors > 0, ("neighbor count 0"));
931 ieee80211_beacon_notify(vap, IEEE80211_BEACON_MESHCONF);
933 ni->ni_mlstate = state;
935 case IEEE80211_NODE_MESH_HOLDING:
936 ms->ms_ppath->mpp_peerdown(ni);
938 case IEEE80211_NODE_MESH_ESTABLISHED:
939 ieee80211_mesh_discover(vap, ni->ni_macaddr, NULL);
947 * Helper function to generate a unique local ID required for mesh
948 * peer establishment.
951 mesh_checkid(void *arg, struct ieee80211_node *ni)
955 if (*r == ni->ni_mllid)
956 *(uint16_t *)arg = 0;
960 mesh_generateid(struct ieee80211vap *vap)
966 get_random_bytes(&r, 2);
967 ieee80211_iterate_nodes(&vap->iv_ic->ic_sta, mesh_checkid, &r);
969 } while (r == 0 && maxiter > 0);
974 * Verifies if we already received this packet by checking its
976 * Returns 0 if the frame is to be accepted, 1 otherwise.
979 mesh_checkpseq(struct ieee80211vap *vap,
980 const uint8_t source[IEEE80211_ADDR_LEN], uint32_t seq)
982 struct ieee80211_mesh_route *rt;
984 rt = ieee80211_mesh_rt_find(vap, source);
986 rt = ieee80211_mesh_rt_add(vap, source);
988 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH, source,
989 "%s", "add mcast route failed");
990 vap->iv_stats.is_mesh_rtaddfailed++;
993 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH, source,
994 "add mcast route, mesh seqno %d", seq);
995 rt->rt_lastmseq = seq;
998 if (IEEE80211_MESH_SEQ_GEQ(rt->rt_lastmseq, seq)) {
1001 rt->rt_lastmseq = seq;
1007 * Iterate the routing table and locate the next hop.
1009 struct ieee80211_node *
1010 ieee80211_mesh_find_txnode(struct ieee80211vap *vap,
1011 const uint8_t dest[IEEE80211_ADDR_LEN])
1013 struct ieee80211_mesh_route *rt;
1015 rt = ieee80211_mesh_rt_find(vap, dest);
1018 if ((rt->rt_flags & IEEE80211_MESHRT_FLAGS_VALID) == 0) {
1019 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH, dest,
1020 "%s: !valid, flags 0x%x", __func__, rt->rt_flags);
1024 if (rt->rt_flags & IEEE80211_MESHRT_FLAGS_PROXY) {
1025 rt = ieee80211_mesh_rt_find(vap, rt->rt_mesh_gate);
1026 if (rt == NULL) return NULL;
1027 if ((rt->rt_flags & IEEE80211_MESHRT_FLAGS_VALID) == 0) {
1028 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH, dest,
1029 "%s: meshgate !valid, flags 0x%x", __func__,
1035 return ieee80211_find_txnode(vap, rt->rt_nexthop);
1039 mesh_transmit_to_gate(struct ieee80211vap *vap, struct mbuf *m,
1040 struct ieee80211_mesh_route *rt_gate)
1042 struct ifnet *ifp = vap->iv_ifp;
1043 struct ieee80211_node *ni;
1045 IEEE80211_TX_UNLOCK_ASSERT(vap->iv_ic);
1047 ni = ieee80211_mesh_find_txnode(vap, rt_gate->rt_dest);
1049 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1055 * Send through the VAP packet transmit path.
1056 * This consumes the node ref grabbed above and
1057 * the mbuf, regardless of whether there's a problem
1060 (void) ieee80211_vap_pkt_send_dest(vap, m, ni);
1064 * Forward the queued frames to known valid mesh gates.
1065 * Assume destination to be outside the MBSS (i.e. proxy entry),
1066 * If no valid mesh gates are known silently discard queued frames.
1067 * After transmitting frames to all known valid mesh gates, this route
1068 * will be marked invalid, and a new path discovery will happen in the hopes
1069 * that (at least) one of the mesh gates have a new proxy entry for us to use.
1072 ieee80211_mesh_forward_to_gates(struct ieee80211vap *vap,
1073 struct ieee80211_mesh_route *rt_dest)
1075 struct ieee80211com *ic = vap->iv_ic;
1076 struct ieee80211_mesh_state *ms = vap->iv_mesh;
1077 struct ieee80211_mesh_route *rt_gate;
1078 struct ieee80211_mesh_gate_route *gr = NULL, *gr_next;
1079 struct mbuf *m, *mcopy, *next;
1081 IEEE80211_TX_UNLOCK_ASSERT(ic);
1083 KASSERT( rt_dest->rt_flags == IEEE80211_MESHRT_FLAGS_DISCOVER,
1084 ("Route is not marked with IEEE80211_MESHRT_FLAGS_DISCOVER"));
1086 /* XXX: send to more than one valid mash gate */
1089 m = ieee80211_ageq_remove(&ic->ic_stageq,
1090 (struct ieee80211_node *)(uintptr_t)
1091 ieee80211_mac_hash(ic, rt_dest->rt_dest));
1093 TAILQ_FOREACH_SAFE(gr, &ms->ms_known_gates, gr_next, gr_next) {
1094 rt_gate = gr->gr_route;
1095 if (rt_gate == NULL) {
1096 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_HWMP,
1098 "mesh gate with no path %6D",
1102 if ((rt_gate->rt_flags & IEEE80211_MESHRT_FLAGS_VALID) == 0)
1104 KASSERT(rt_gate->rt_flags & IEEE80211_MESHRT_FLAGS_GATE,
1105 ("route not marked as a mesh gate"));
1106 KASSERT((rt_gate->rt_flags &
1107 IEEE80211_MESHRT_FLAGS_PROXY) == 0,
1108 ("found mesh gate that is also marked porxy"));
1110 * convert route to a proxy route gated by the current
1111 * mesh gate, this is needed so encap can built data
1112 * frame with correct address.
1114 rt_dest->rt_flags = IEEE80211_MESHRT_FLAGS_PROXY |
1115 IEEE80211_MESHRT_FLAGS_VALID;
1116 rt_dest->rt_ext_seq = 1; /* random value */
1117 IEEE80211_ADDR_COPY(rt_dest->rt_mesh_gate, rt_gate->rt_dest);
1118 IEEE80211_ADDR_COPY(rt_dest->rt_nexthop, rt_gate->rt_nexthop);
1119 rt_dest->rt_metric = rt_gate->rt_metric;
1120 rt_dest->rt_nhops = rt_gate->rt_nhops;
1121 ieee80211_mesh_rt_update(rt_dest, ms->ms_ppath->mpp_inact);
1124 mcopy = m_dup(m, M_NOWAIT);
1125 for (; mcopy != NULL; mcopy = next) {
1126 next = mcopy->m_nextpkt;
1127 mcopy->m_nextpkt = NULL;
1128 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_HWMP,
1130 "flush queued frame %p len %d", mcopy,
1131 mcopy->m_pkthdr.len);
1132 mesh_transmit_to_gate(vap, mcopy, rt_gate);
1136 rt_dest->rt_flags = 0; /* Mark invalid */
1142 * Forward the specified frame.
1143 * Decrement the TTL and set TA to our MAC address.
1146 mesh_forward(struct ieee80211vap *vap, struct mbuf *m,
1147 const struct ieee80211_meshcntl *mc)
1149 struct ieee80211com *ic = vap->iv_ic;
1150 struct ieee80211_mesh_state *ms = vap->iv_mesh;
1151 struct ifnet *ifp = vap->iv_ifp;
1152 const struct ieee80211_frame *wh =
1153 mtod(m, const struct ieee80211_frame *);
1155 struct ieee80211_meshcntl *mccopy;
1156 struct ieee80211_frame *whcopy;
1157 struct ieee80211_node *ni;
1160 /* This is called from the RX path - don't hold this lock */
1161 IEEE80211_TX_UNLOCK_ASSERT(ic);
1164 * mesh ttl of 1 means we are the last one receiving it,
1165 * according to amendment we decrement and then check if
1166 * 0, if so we dont forward.
1168 if (mc->mc_ttl < 1) {
1169 IEEE80211_NOTE_FRAME(vap, IEEE80211_MSG_MESH, wh,
1170 "%s", "frame not fwd'd, ttl 1");
1171 vap->iv_stats.is_mesh_fwd_ttl++;
1174 if (!(ms->ms_flags & IEEE80211_MESHFLAGS_FWD)) {
1175 IEEE80211_NOTE_FRAME(vap, IEEE80211_MSG_MESH, wh,
1176 "%s", "frame not fwd'd, fwding disabled");
1177 vap->iv_stats.is_mesh_fwd_disabled++;
1180 mcopy = m_dup(m, M_NOWAIT);
1181 if (mcopy == NULL) {
1182 IEEE80211_NOTE_FRAME(vap, IEEE80211_MSG_MESH, wh,
1183 "%s", "frame not fwd'd, cannot dup");
1184 vap->iv_stats.is_mesh_fwd_nobuf++;
1185 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1188 mcopy = m_pullup(mcopy, ieee80211_hdrspace(ic, wh) +
1189 sizeof(struct ieee80211_meshcntl));
1190 if (mcopy == NULL) {
1191 IEEE80211_NOTE_FRAME(vap, IEEE80211_MSG_MESH, wh,
1192 "%s", "frame not fwd'd, too short");
1193 vap->iv_stats.is_mesh_fwd_tooshort++;
1194 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1198 whcopy = mtod(mcopy, struct ieee80211_frame *);
1199 mccopy = (struct ieee80211_meshcntl *)
1200 (mtod(mcopy, uint8_t *) + ieee80211_hdrspace(ic, wh));
1201 /* XXX clear other bits? */
1202 whcopy->i_fc[1] &= ~IEEE80211_FC1_RETRY;
1203 IEEE80211_ADDR_COPY(whcopy->i_addr2, vap->iv_myaddr);
1204 if (IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1205 ni = ieee80211_ref_node(vap->iv_bss);
1206 mcopy->m_flags |= M_MCAST;
1208 ni = ieee80211_mesh_find_txnode(vap, whcopy->i_addr3);
1211 * [Optional] any of the following three actions:
1212 * o silently discard
1213 * o trigger a path discovery
1214 * o inform TA that meshDA is unknown.
1216 IEEE80211_NOTE_FRAME(vap, IEEE80211_MSG_MESH, wh,
1217 "%s", "frame not fwd'd, no path");
1218 ms->ms_ppath->mpp_senderror(vap, whcopy->i_addr3, NULL,
1219 IEEE80211_REASON_MESH_PERR_NO_FI);
1220 vap->iv_stats.is_mesh_fwd_nopath++;
1224 IEEE80211_ADDR_COPY(whcopy->i_addr1, ni->ni_macaddr);
1226 KASSERT(mccopy->mc_ttl > 0, ("%s called with wrong ttl", __func__));
1229 /* XXX calculate priority so drivers can find the tx queue */
1230 M_WME_SETAC(mcopy, WME_AC_BE);
1232 /* XXX do we know m_nextpkt is NULL? */
1233 MPASS((mcopy->m_pkthdr.csum_flags & CSUM_SND_TAG) == 0);
1234 mcopy->m_pkthdr.rcvif = (void *) ni;
1237 * XXX this bypasses all of the VAP TX handling; it passes frames
1238 * directly to the parent interface.
1240 * Because of this, there's no TX lock being held as there's no
1241 * encaps state being used.
1243 * Doing a direct parent transmit may not be the correct thing
1244 * to do here; we'll have to re-think this soon.
1246 IEEE80211_TX_LOCK(ic);
1247 err = ieee80211_parent_xmitpkt(ic, mcopy);
1248 IEEE80211_TX_UNLOCK(ic);
1250 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
1253 static struct mbuf *
1254 mesh_decap(struct ieee80211vap *vap, struct mbuf *m, int hdrlen, int meshdrlen)
1256 #define WHDIR(wh) ((wh)->i_fc[1] & IEEE80211_FC1_DIR_MASK)
1257 #define MC01(mc) ((const struct ieee80211_meshcntl_ae01 *)mc)
1258 uint8_t b[sizeof(struct ieee80211_qosframe_addr4) +
1259 sizeof(struct ieee80211_meshcntl_ae10)];
1260 const struct ieee80211_qosframe_addr4 *wh;
1261 const struct ieee80211_meshcntl_ae10 *mc;
1262 struct ether_header *eh;
1266 if (m->m_len < hdrlen + sizeof(*llc) &&
1267 (m = m_pullup(m, hdrlen + sizeof(*llc))) == NULL) {
1268 IEEE80211_DPRINTF(vap, IEEE80211_MSG_ANY,
1269 "discard data frame: %s", "m_pullup failed");
1270 vap->iv_stats.is_rx_tooshort++;
1273 memcpy(b, mtod(m, caddr_t), hdrlen);
1274 wh = (const struct ieee80211_qosframe_addr4 *)&b[0];
1275 mc = (const struct ieee80211_meshcntl_ae10 *)&b[hdrlen - meshdrlen];
1276 KASSERT(WHDIR(wh) == IEEE80211_FC1_DIR_FROMDS ||
1277 WHDIR(wh) == IEEE80211_FC1_DIR_DSTODS,
1278 ("bogus dir, fc 0x%x:0x%x", wh->i_fc[0], wh->i_fc[1]));
1280 llc = (struct llc *)(mtod(m, caddr_t) + hdrlen);
1281 if (llc->llc_dsap == LLC_SNAP_LSAP && llc->llc_ssap == LLC_SNAP_LSAP &&
1282 llc->llc_control == LLC_UI && llc->llc_snap.org_code[0] == 0 &&
1283 llc->llc_snap.org_code[1] == 0 && llc->llc_snap.org_code[2] == 0 &&
1284 /* NB: preserve AppleTalk frames that have a native SNAP hdr */
1285 !(llc->llc_snap.ether_type == htons(ETHERTYPE_AARP) ||
1286 llc->llc_snap.ether_type == htons(ETHERTYPE_IPX))) {
1287 m_adj(m, hdrlen + sizeof(struct llc) - sizeof(*eh));
1290 m_adj(m, hdrlen - sizeof(*eh));
1292 eh = mtod(m, struct ether_header *);
1293 ae = mc->mc_flags & IEEE80211_MESH_AE_MASK;
1294 if (WHDIR(wh) == IEEE80211_FC1_DIR_FROMDS) {
1295 IEEE80211_ADDR_COPY(eh->ether_dhost, wh->i_addr1);
1296 if (ae == IEEE80211_MESH_AE_00) {
1297 IEEE80211_ADDR_COPY(eh->ether_shost, wh->i_addr3);
1298 } else if (ae == IEEE80211_MESH_AE_01) {
1299 IEEE80211_ADDR_COPY(eh->ether_shost,
1300 MC01(mc)->mc_addr4);
1302 IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
1303 (const struct ieee80211_frame *)wh, NULL,
1305 vap->iv_stats.is_mesh_badae++;
1310 if (ae == IEEE80211_MESH_AE_00) {
1311 IEEE80211_ADDR_COPY(eh->ether_dhost, wh->i_addr3);
1312 IEEE80211_ADDR_COPY(eh->ether_shost, wh->i_addr4);
1313 } else if (ae == IEEE80211_MESH_AE_10) {
1314 IEEE80211_ADDR_COPY(eh->ether_dhost, mc->mc_addr5);
1315 IEEE80211_ADDR_COPY(eh->ether_shost, mc->mc_addr6);
1317 IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
1318 (const struct ieee80211_frame *)wh, NULL,
1320 vap->iv_stats.is_mesh_badae++;
1325 #ifndef __NO_STRICT_ALIGNMENT
1326 if (!ALIGNED_POINTER(mtod(m, caddr_t) + sizeof(*eh), uint32_t)) {
1327 m = ieee80211_realign(vap, m, sizeof(*eh));
1331 #endif /* !__NO_STRICT_ALIGNMENT */
1333 eh = mtod(m, struct ether_header *);
1334 eh->ether_type = htons(m->m_pkthdr.len - sizeof(*eh));
1342 * Return non-zero if the unicast mesh data frame should be processed
1343 * locally. Frames that are not proxy'd have our address, otherwise
1344 * we need to consult the routing table to look for a proxy entry.
1347 mesh_isucastforme(struct ieee80211vap *vap, const struct ieee80211_frame *wh,
1348 const struct ieee80211_meshcntl *mc)
1350 int ae = mc->mc_flags & 3;
1352 KASSERT((wh->i_fc[1] & IEEE80211_FC1_DIR_MASK) == IEEE80211_FC1_DIR_DSTODS,
1353 ("bad dir 0x%x:0x%x", wh->i_fc[0], wh->i_fc[1]));
1354 KASSERT(ae == IEEE80211_MESH_AE_00 || ae == IEEE80211_MESH_AE_10,
1356 if (ae == IEEE80211_MESH_AE_10) { /* ucast w/ proxy */
1357 const struct ieee80211_meshcntl_ae10 *mc10 =
1358 (const struct ieee80211_meshcntl_ae10 *) mc;
1359 struct ieee80211_mesh_route *rt =
1360 ieee80211_mesh_rt_find(vap, mc10->mc_addr5);
1361 /* check for proxy route to ourself */
1362 return (rt != NULL &&
1363 (rt->rt_flags & IEEE80211_MESHRT_FLAGS_PROXY));
1364 } else /* ucast w/o proxy */
1365 return IEEE80211_ADDR_EQ(wh->i_addr3, vap->iv_myaddr);
1369 * Verifies transmitter, updates lifetime, precursor list and forwards data.
1370 * > 0 means we have forwarded data and no need to process locally
1371 * == 0 means we want to process locally (and we may have forwarded data
1372 * < 0 means there was an error and data should be discarded
1375 mesh_recv_indiv_data_to_fwrd(struct ieee80211vap *vap, struct mbuf *m,
1376 struct ieee80211_frame *wh, const struct ieee80211_meshcntl *mc)
1378 struct ieee80211_qosframe_addr4 *qwh;
1379 struct ieee80211_mesh_state *ms = vap->iv_mesh;
1380 struct ieee80211_mesh_route *rt_meshda, *rt_meshsa;
1382 /* This is called from the RX path - don't hold this lock */
1383 IEEE80211_TX_UNLOCK_ASSERT(vap->iv_ic);
1385 qwh = (struct ieee80211_qosframe_addr4 *)wh;
1389 * o verify addr2 is a legitimate transmitter
1390 * o lifetime of precursor of addr3 (addr2) is max(init, curr)
1391 * o lifetime of precursor of addr4 (nexthop) is max(init, curr)
1394 /* set lifetime of addr3 (meshDA) to initial value */
1395 rt_meshda = ieee80211_mesh_rt_find(vap, qwh->i_addr3);
1396 if (rt_meshda == NULL) {
1397 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH, qwh->i_addr2,
1398 "no route to meshDA(%6D)", qwh->i_addr3, ":");
1400 * [Optional] any of the following three actions:
1401 * o silently discard [X]
1402 * o trigger a path discovery [ ]
1403 * o inform TA that meshDA is unknown. [ ]
1409 ieee80211_mesh_rt_update(rt_meshda, ticks_to_msecs(
1410 ms->ms_ppath->mpp_inact));
1412 /* set lifetime of addr4 (meshSA) to initial value */
1413 rt_meshsa = ieee80211_mesh_rt_find(vap, qwh->i_addr4);
1414 KASSERT(rt_meshsa != NULL, ("no route"));
1415 ieee80211_mesh_rt_update(rt_meshsa, ticks_to_msecs(
1416 ms->ms_ppath->mpp_inact));
1418 mesh_forward(vap, m, mc);
1419 return (1); /* dont process locally */
1423 * Verifies transmitter, updates lifetime, precursor list and process data
1424 * locally, if data is proxy with AE = 10 it could mean data should go
1425 * on another mesh path or data should be forwarded to the DS.
1427 * > 0 means we have forwarded data and no need to process locally
1428 * == 0 means we want to process locally (and we may have forwarded data
1429 * < 0 means there was an error and data should be discarded
1432 mesh_recv_indiv_data_to_me(struct ieee80211vap *vap, struct mbuf *m,
1433 struct ieee80211_frame *wh, const struct ieee80211_meshcntl *mc)
1435 struct ieee80211_qosframe_addr4 *qwh;
1436 const struct ieee80211_meshcntl_ae10 *mc10;
1437 struct ieee80211_mesh_state *ms = vap->iv_mesh;
1438 struct ieee80211_mesh_route *rt;
1441 /* This is called from the RX path - don't hold this lock */
1442 IEEE80211_TX_UNLOCK_ASSERT(vap->iv_ic);
1444 qwh = (struct ieee80211_qosframe_addr4 *)wh;
1445 mc10 = (const struct ieee80211_meshcntl_ae10 *)mc;
1449 * o verify addr2 is a legitimate transmitter
1450 * o lifetime of precursor entry is max(init, curr)
1453 /* set lifetime of addr4 (meshSA) to initial value */
1454 rt = ieee80211_mesh_rt_find(vap, qwh->i_addr4);
1455 KASSERT(rt != NULL, ("no route"));
1456 ieee80211_mesh_rt_update(rt, ticks_to_msecs(ms->ms_ppath->mpp_inact));
1459 ae = mc10->mc_flags & IEEE80211_MESH_AE_MASK;
1460 KASSERT(ae == IEEE80211_MESH_AE_00 ||
1461 ae == IEEE80211_MESH_AE_10, ("bad AE %d", ae));
1462 if (ae == IEEE80211_MESH_AE_10) {
1463 if (IEEE80211_ADDR_EQ(mc10->mc_addr5, qwh->i_addr3)) {
1464 return (0); /* process locally */
1467 rt = ieee80211_mesh_rt_find(vap, mc10->mc_addr5);
1469 (rt->rt_flags & IEEE80211_MESHRT_FLAGS_VALID) &&
1470 (rt->rt_flags & IEEE80211_MESHRT_FLAGS_PROXY) == 0) {
1472 * Forward on another mesh-path, according to
1473 * amendment as specified in 9.32.4.1
1475 IEEE80211_ADDR_COPY(qwh->i_addr3, mc10->mc_addr5);
1476 mesh_forward(vap, m,
1477 (const struct ieee80211_meshcntl *)mc10);
1478 return (1); /* dont process locally */
1481 * All other cases: forward of MSDUs from the MBSS to DS indiv.
1482 * addressed according to 13.11.3.2.
1484 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_OUTPUT, qwh->i_addr2,
1485 "forward frame to DS, SA(%6D) DA(%6D)",
1486 mc10->mc_addr6, ":", mc10->mc_addr5, ":");
1488 return (0); /* process locally */
1492 * Try to forward the group addressed data on to other mesh STAs, and
1495 * > 0 means we have forwarded data and no need to process locally
1496 * == 0 means we want to process locally (and we may have forwarded data
1497 * < 0 means there was an error and data should be discarded
1500 mesh_recv_group_data(struct ieee80211vap *vap, struct mbuf *m,
1501 struct ieee80211_frame *wh, const struct ieee80211_meshcntl *mc)
1503 #define MC01(mc) ((const struct ieee80211_meshcntl_ae01 *)mc)
1504 struct ieee80211_mesh_state *ms = vap->iv_mesh;
1506 /* This is called from the RX path - don't hold this lock */
1507 IEEE80211_TX_UNLOCK_ASSERT(vap->iv_ic);
1509 mesh_forward(vap, m, mc);
1511 if(mc->mc_ttl > 0) {
1512 if (mc->mc_flags & IEEE80211_MESH_AE_01) {
1514 * Forward of MSDUs from the MBSS to DS group addressed
1515 * (according to 13.11.3.2)
1516 * This happens by delivering the packet, and a bridge
1517 * will sent it on another port member.
1519 if (ms->ms_flags & IEEE80211_MESHFLAGS_GATE &&
1520 ms->ms_flags & IEEE80211_MESHFLAGS_FWD) {
1521 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH,
1522 MC01(mc)->mc_addr4, "%s",
1523 "forward from MBSS to the DS");
1527 return (0); /* process locally */
1532 mesh_input(struct ieee80211_node *ni, struct mbuf *m,
1533 const struct ieee80211_rx_stats *rxs, int rssi, int nf)
1535 #define HAS_SEQ(type) ((type & 0x4) == 0)
1536 #define MC01(mc) ((const struct ieee80211_meshcntl_ae01 *)mc)
1537 struct ieee80211vap *vap = ni->ni_vap;
1538 struct ieee80211com *ic = ni->ni_ic;
1539 struct ifnet *ifp = vap->iv_ifp;
1540 struct ieee80211_frame *wh;
1541 const struct ieee80211_meshcntl *mc;
1542 int hdrspace, meshdrlen, need_tap, error;
1543 uint8_t dir, type, subtype, ae;
1545 const uint8_t *addr;
1548 KASSERT(ni != NULL, ("null node"));
1549 ni->ni_inact = ni->ni_inact_reload;
1551 need_tap = 1; /* mbuf need to be tapped. */
1552 type = -1; /* undefined */
1554 /* This is called from the RX path - don't hold this lock */
1555 IEEE80211_TX_UNLOCK_ASSERT(ic);
1557 if (m->m_pkthdr.len < sizeof(struct ieee80211_frame_min)) {
1558 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
1559 ni->ni_macaddr, NULL,
1560 "too short (1): len %u", m->m_pkthdr.len);
1561 vap->iv_stats.is_rx_tooshort++;
1565 * Bit of a cheat here, we use a pointer for a 3-address
1566 * frame format but don't reference fields past outside
1567 * ieee80211_frame_min w/o first validating the data is
1570 wh = mtod(m, struct ieee80211_frame *);
1572 if ((wh->i_fc[0] & IEEE80211_FC0_VERSION_MASK) !=
1573 IEEE80211_FC0_VERSION_0) {
1574 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
1575 ni->ni_macaddr, NULL, "wrong version %x", wh->i_fc[0]);
1576 vap->iv_stats.is_rx_badversion++;
1579 dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK;
1580 type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
1581 subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
1582 if ((ic->ic_flags & IEEE80211_F_SCAN) == 0) {
1583 IEEE80211_RSSI_LPF(ni->ni_avgrssi, rssi);
1585 if (HAS_SEQ(type)) {
1586 uint8_t tid = ieee80211_gettid(wh);
1588 if (IEEE80211_QOS_HAS_SEQ(wh) &&
1589 TID_TO_WME_AC(tid) >= WME_AC_VI)
1590 ic->ic_wme.wme_hipri_traffic++;
1591 if (! ieee80211_check_rxseq(ni, wh, wh->i_addr1, rxs))
1595 #ifdef IEEE80211_DEBUG
1597 * It's easier, but too expensive, to simulate different mesh
1598 * topologies by consulting the ACL policy very early, so do this
1601 * NB: this check is also done upon peering link initiation.
1603 if (vap->iv_acl != NULL && !vap->iv_acl->iac_check(vap, wh)) {
1604 IEEE80211_DISCARD(vap, IEEE80211_MSG_ACL,
1605 wh, NULL, "%s", "disallowed by ACL");
1606 vap->iv_stats.is_rx_acl++;
1611 case IEEE80211_FC0_TYPE_DATA:
1612 if (ni == vap->iv_bss)
1614 if (ni->ni_mlstate != IEEE80211_NODE_MESH_ESTABLISHED) {
1615 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_MESH,
1616 ni->ni_macaddr, NULL,
1617 "peer link not yet established (%d)",
1619 vap->iv_stats.is_mesh_nolink++;
1622 if (dir != IEEE80211_FC1_DIR_FROMDS &&
1623 dir != IEEE80211_FC1_DIR_DSTODS) {
1624 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
1625 wh, "data", "incorrect dir 0x%x", dir);
1626 vap->iv_stats.is_rx_wrongdir++;
1630 /* All Mesh data frames are QoS subtype */
1631 if (!HAS_SEQ(type)) {
1632 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
1633 wh, "data", "incorrect subtype 0x%x", subtype);
1634 vap->iv_stats.is_rx_badsubtype++;
1639 * Next up, any fragmentation.
1640 * XXX: we defrag before we even try to forward,
1641 * Mesh Control field is not present in sub-sequent
1642 * fragmented frames. This is in contrast to Draft 4.0.
1644 hdrspace = ieee80211_hdrspace(ic, wh);
1645 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1646 m = ieee80211_defrag(ni, m, hdrspace);
1648 /* Fragment dropped or frame not complete yet */
1652 wh = mtod(m, struct ieee80211_frame *); /* NB: after defrag */
1655 * Now we have a complete Mesh Data frame.
1659 * Only fromDStoDS data frames use 4 address qos frames
1660 * as specified in amendment. Otherwise addr4 is located
1661 * in the Mesh Control field and a 3 address qos frame
1664 *(uint16_t *)qos = *(uint16_t *)ieee80211_getqos(wh);
1667 * NB: The mesh STA sets the Mesh Control Present
1668 * subfield to 1 in the Mesh Data frame containing
1669 * an unfragmented MSDU, an A-MSDU, or the first
1670 * fragment of an MSDU.
1671 * After defrag it should always be present.
1673 if (!(qos[1] & IEEE80211_QOS_MC)) {
1674 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_MESH,
1675 ni->ni_macaddr, NULL,
1676 "%s", "Mesh control field not present");
1677 vap->iv_stats.is_rx_elem_missing++; /* XXX: kinda */
1681 /* pull up enough to get to the mesh control */
1682 if (m->m_len < hdrspace + sizeof(struct ieee80211_meshcntl) &&
1683 (m = m_pullup(m, hdrspace +
1684 sizeof(struct ieee80211_meshcntl))) == NULL) {
1685 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
1686 ni->ni_macaddr, NULL,
1687 "data too short: expecting %u", hdrspace);
1688 vap->iv_stats.is_rx_tooshort++;
1692 * Now calculate the full extent of the headers. Note
1693 * mesh_decap will pull up anything we didn't get
1694 * above when it strips the 802.11 headers.
1696 mc = (const struct ieee80211_meshcntl *)
1697 (mtod(m, const uint8_t *) + hdrspace);
1698 ae = mc->mc_flags & IEEE80211_MESH_AE_MASK;
1699 meshdrlen = sizeof(struct ieee80211_meshcntl) +
1700 ae * IEEE80211_ADDR_LEN;
1701 hdrspace += meshdrlen;
1703 /* pull complete hdrspace = ieee80211_hdrspace + meshcontrol */
1704 if ((meshdrlen > sizeof(struct ieee80211_meshcntl)) &&
1705 (m->m_len < hdrspace) &&
1706 ((m = m_pullup(m, hdrspace)) == NULL)) {
1707 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
1708 ni->ni_macaddr, NULL,
1709 "data too short: expecting %u", hdrspace);
1710 vap->iv_stats.is_rx_tooshort++;
1713 /* XXX: are we sure there is no reallocating after m_pullup? */
1715 seq = le32dec(mc->mc_seq);
1716 if (IEEE80211_IS_MULTICAST(wh->i_addr1))
1718 else if (ae == IEEE80211_MESH_AE_01)
1719 addr = MC01(mc)->mc_addr4;
1721 addr = ((struct ieee80211_qosframe_addr4 *)wh)->i_addr4;
1722 if (IEEE80211_ADDR_EQ(vap->iv_myaddr, addr)) {
1723 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
1724 addr, "data", "%s", "not to me");
1725 vap->iv_stats.is_rx_wrongbss++; /* XXX kinda */
1728 if (mesh_checkpseq(vap, addr, seq) != 0) {
1729 vap->iv_stats.is_rx_dup++;
1733 /* This code "routes" the frame to the right control path */
1734 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1735 if (IEEE80211_ADDR_EQ(vap->iv_myaddr, wh->i_addr3))
1737 mesh_recv_indiv_data_to_me(vap, m, wh, mc);
1738 else if (IEEE80211_IS_MULTICAST(wh->i_addr3))
1739 error = mesh_recv_group_data(vap, m, wh, mc);
1741 error = mesh_recv_indiv_data_to_fwrd(vap, m,
1744 error = mesh_recv_group_data(vap, m, wh, mc);
1750 if (ieee80211_radiotap_active_vap(vap))
1751 ieee80211_radiotap_rx(vap, m);
1755 * Finally, strip the 802.11 header.
1757 m = mesh_decap(vap, m, hdrspace, meshdrlen);
1759 /* XXX mask bit to check for both */
1760 /* don't count Null data frames as errors */
1761 if (subtype == IEEE80211_FC0_SUBTYPE_NODATA ||
1762 subtype == IEEE80211_FC0_SUBTYPE_QOS_NULL)
1764 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
1765 ni->ni_macaddr, "data", "%s", "decap error");
1766 vap->iv_stats.is_rx_decap++;
1767 IEEE80211_NODE_STAT(ni, rx_decap);
1770 if (qos[0] & IEEE80211_QOS_AMSDU) {
1771 m = ieee80211_decap_amsdu(ni, m);
1773 return IEEE80211_FC0_TYPE_DATA;
1775 ieee80211_deliver_data(vap, ni, m);
1777 case IEEE80211_FC0_TYPE_MGT:
1778 vap->iv_stats.is_rx_mgmt++;
1779 IEEE80211_NODE_STAT(ni, rx_mgmt);
1780 if (dir != IEEE80211_FC1_DIR_NODS) {
1781 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
1782 wh, "mgt", "incorrect dir 0x%x", dir);
1783 vap->iv_stats.is_rx_wrongdir++;
1786 if (m->m_pkthdr.len < sizeof(struct ieee80211_frame)) {
1787 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
1788 ni->ni_macaddr, "mgt", "too short: len %u",
1790 vap->iv_stats.is_rx_tooshort++;
1793 #ifdef IEEE80211_DEBUG
1794 if ((ieee80211_msg_debug(vap) &&
1795 (vap->iv_ic->ic_flags & IEEE80211_F_SCAN)) ||
1796 ieee80211_msg_dumppkts(vap)) {
1797 if_printf(ifp, "received %s from %s rssi %d\n",
1798 ieee80211_mgt_subtype_name(subtype),
1799 ether_sprintf(wh->i_addr2), rssi);
1802 if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
1803 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
1804 wh, NULL, "%s", "WEP set but not permitted");
1805 vap->iv_stats.is_rx_mgtdiscard++; /* XXX */
1808 vap->iv_recv_mgmt(ni, m, subtype, rxs, rssi, nf);
1810 case IEEE80211_FC0_TYPE_CTL:
1811 vap->iv_stats.is_rx_ctl++;
1812 IEEE80211_NODE_STAT(ni, rx_ctrl);
1815 IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
1816 wh, "bad", "frame type 0x%x", type);
1817 /* should not come here */
1821 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
1824 if (need_tap && ieee80211_radiotap_active_vap(vap))
1825 ieee80211_radiotap_rx(vap, m);
1834 mesh_recv_mgmt(struct ieee80211_node *ni, struct mbuf *m0, int subtype,
1835 const struct ieee80211_rx_stats *rxs, int rssi, int nf)
1837 struct ieee80211vap *vap = ni->ni_vap;
1838 struct ieee80211_mesh_state *ms = vap->iv_mesh;
1839 struct ieee80211com *ic = ni->ni_ic;
1840 struct ieee80211_channel *rxchan = ic->ic_curchan;
1841 struct ieee80211_frame *wh;
1842 struct ieee80211_mesh_route *rt;
1843 uint8_t *frm, *efrm;
1845 wh = mtod(m0, struct ieee80211_frame *);
1846 frm = (uint8_t *)&wh[1];
1847 efrm = mtod(m0, uint8_t *) + m0->m_len;
1849 case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
1850 case IEEE80211_FC0_SUBTYPE_BEACON:
1852 struct ieee80211_scanparams scan;
1853 struct ieee80211_channel *c;
1855 * We process beacon/probe response
1856 * frames to discover neighbors.
1859 c = ieee80211_lookup_channel_rxstatus(vap, rxs);
1863 if (ieee80211_parse_beacon(ni, m0, rxchan, &scan) != 0)
1866 * Count frame now that we know it's to be processed.
1868 if (subtype == IEEE80211_FC0_SUBTYPE_BEACON) {
1869 vap->iv_stats.is_rx_beacon++; /* XXX remove */
1870 IEEE80211_NODE_STAT(ni, rx_beacons);
1872 IEEE80211_NODE_STAT(ni, rx_proberesp);
1874 * If scanning, just pass information to the scan module.
1876 if (ic->ic_flags & IEEE80211_F_SCAN) {
1877 if (ic->ic_flags_ext & IEEE80211_FEXT_PROBECHAN) {
1879 * Actively scanning a channel marked passive;
1880 * send a probe request now that we know there
1881 * is 802.11 traffic present.
1883 * XXX check if the beacon we recv'd gives
1884 * us what we need and suppress the probe req
1886 ieee80211_probe_curchan(vap, 1);
1887 ic->ic_flags_ext &= ~IEEE80211_FEXT_PROBECHAN;
1889 ieee80211_add_scan(vap, rxchan, &scan, wh,
1894 /* The rest of this code assumes we are running */
1895 if (vap->iv_state != IEEE80211_S_RUN)
1898 * Ignore non-mesh STAs.
1901 (IEEE80211_CAPINFO_ESS|IEEE80211_CAPINFO_IBSS)) ||
1902 scan.meshid == NULL || scan.meshconf == NULL) {
1903 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
1904 wh, "beacon", "%s", "not a mesh sta");
1905 vap->iv_stats.is_mesh_wrongmesh++;
1909 * Ignore STAs for other mesh networks.
1911 if (memcmp(scan.meshid+2, ms->ms_id, ms->ms_idlen) != 0 ||
1912 mesh_verify_meshconf(vap, scan.meshconf)) {
1913 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
1914 wh, "beacon", "%s", "not for our mesh");
1915 vap->iv_stats.is_mesh_wrongmesh++;
1919 * Peer only based on the current ACL policy.
1921 if (vap->iv_acl != NULL && !vap->iv_acl->iac_check(vap, wh)) {
1922 IEEE80211_DISCARD(vap, IEEE80211_MSG_ACL,
1923 wh, NULL, "%s", "disallowed by ACL");
1924 vap->iv_stats.is_rx_acl++;
1928 * Do neighbor discovery.
1930 if (!IEEE80211_ADDR_EQ(wh->i_addr2, ni->ni_macaddr)) {
1932 * Create a new entry in the neighbor table.
1934 ni = ieee80211_add_neighbor(vap, wh, &scan);
1937 * Automatically peer with discovered nodes if possible.
1939 if (ni != vap->iv_bss &&
1940 (ms->ms_flags & IEEE80211_MESHFLAGS_AP)) {
1941 switch (ni->ni_mlstate) {
1942 case IEEE80211_NODE_MESH_IDLE:
1946 /* Wait for backoff callout to reset counter */
1947 if (ni->ni_mlhcnt >= ieee80211_mesh_maxholding)
1950 ni->ni_mlpid = mesh_generateid(vap);
1951 if (ni->ni_mlpid == 0)
1953 mesh_linkchange(ni, IEEE80211_NODE_MESH_OPENSNT);
1954 args[0] = ni->ni_mlpid;
1955 ieee80211_send_action(ni,
1956 IEEE80211_ACTION_CAT_SELF_PROT,
1957 IEEE80211_ACTION_MESHPEERING_OPEN, args);
1959 mesh_peer_timeout_setup(ni);
1962 case IEEE80211_NODE_MESH_ESTABLISHED:
1965 * Valid beacon from a peer mesh STA
1968 rt = ieee80211_mesh_rt_find(vap, wh->i_addr2);
1970 ieee80211_mesh_rt_update(rt,
1972 ms->ms_ppath->mpp_inact));
1982 case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
1984 uint8_t *ssid, *meshid, *rates, *xrates;
1986 if (vap->iv_state != IEEE80211_S_RUN) {
1987 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
1988 wh, NULL, "wrong state %s",
1989 ieee80211_state_name[vap->iv_state]);
1990 vap->iv_stats.is_rx_mgtdiscard++;
1993 if (IEEE80211_IS_MULTICAST(wh->i_addr2)) {
1994 /* frame must be directed */
1995 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
1996 wh, NULL, "%s", "not unicast");
1997 vap->iv_stats.is_rx_mgtdiscard++; /* XXX stat */
2001 * prreq frame format
2003 * [tlv] supported rates
2004 * [tlv] extended supported rates
2007 ssid = meshid = rates = xrates = NULL;
2008 while (efrm - frm > 1) {
2009 IEEE80211_VERIFY_LENGTH(efrm - frm, frm[1] + 2, return);
2011 case IEEE80211_ELEMID_SSID:
2014 case IEEE80211_ELEMID_RATES:
2017 case IEEE80211_ELEMID_XRATES:
2020 case IEEE80211_ELEMID_MESHID:
2026 IEEE80211_VERIFY_ELEMENT(ssid, IEEE80211_NWID_LEN, return);
2027 IEEE80211_VERIFY_ELEMENT(rates, IEEE80211_RATE_MAXSIZE, return);
2029 IEEE80211_VERIFY_ELEMENT(xrates,
2030 IEEE80211_RATE_MAXSIZE - rates[1], return);
2031 if (meshid != NULL) {
2032 IEEE80211_VERIFY_ELEMENT(meshid,
2033 IEEE80211_MESHID_LEN, return);
2034 /* NB: meshid, not ssid */
2035 IEEE80211_VERIFY_SSID(vap->iv_bss, meshid, return);
2038 /* XXX find a better class or define it's own */
2039 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_INPUT, wh->i_addr2,
2040 "%s", "recv probe req");
2042 * Some legacy 11b clients cannot hack a complete
2043 * probe response frame. When the request includes
2044 * only a bare-bones rate set, communicate this to
2045 * the transmit side.
2047 ieee80211_send_proberesp(vap, wh->i_addr2, 0);
2051 case IEEE80211_FC0_SUBTYPE_ACTION:
2052 case IEEE80211_FC0_SUBTYPE_ACTION_NOACK:
2053 if (ni == vap->iv_bss) {
2054 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
2055 wh, NULL, "%s", "unknown node");
2056 vap->iv_stats.is_rx_mgtdiscard++;
2057 } else if (!IEEE80211_ADDR_EQ(vap->iv_myaddr, wh->i_addr1) &&
2058 !IEEE80211_IS_MULTICAST(wh->i_addr1)) {
2059 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
2060 wh, NULL, "%s", "not for us");
2061 vap->iv_stats.is_rx_mgtdiscard++;
2062 } else if (vap->iv_state != IEEE80211_S_RUN) {
2063 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
2064 wh, NULL, "wrong state %s",
2065 ieee80211_state_name[vap->iv_state]);
2066 vap->iv_stats.is_rx_mgtdiscard++;
2068 if (ieee80211_parse_action(ni, m0) == 0)
2069 (void)ic->ic_recv_action(ni, wh, frm, efrm);
2073 case IEEE80211_FC0_SUBTYPE_ASSOC_REQ:
2074 case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
2075 case IEEE80211_FC0_SUBTYPE_REASSOC_REQ:
2076 case IEEE80211_FC0_SUBTYPE_REASSOC_RESP:
2077 case IEEE80211_FC0_SUBTYPE_TIMING_ADV:
2078 case IEEE80211_FC0_SUBTYPE_ATIM:
2079 case IEEE80211_FC0_SUBTYPE_DISASSOC:
2080 case IEEE80211_FC0_SUBTYPE_AUTH:
2081 case IEEE80211_FC0_SUBTYPE_DEAUTH:
2082 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
2083 wh, NULL, "%s", "not handled");
2084 vap->iv_stats.is_rx_mgtdiscard++;
2088 IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
2089 wh, "mgt", "subtype 0x%x not handled", subtype);
2090 vap->iv_stats.is_rx_badsubtype++;
2096 mesh_recv_ctl(struct ieee80211_node *ni, struct mbuf *m, int subtype)
2100 case IEEE80211_FC0_SUBTYPE_BAR:
2101 ieee80211_recv_bar(ni, m);
2107 * Parse meshpeering action ie's for MPM frames
2109 static const struct ieee80211_meshpeer_ie *
2110 mesh_parse_meshpeering_action(struct ieee80211_node *ni,
2111 const struct ieee80211_frame *wh, /* XXX for VERIFY_LENGTH */
2112 const uint8_t *frm, const uint8_t *efrm,
2113 struct ieee80211_meshpeer_ie *mp, uint8_t subtype)
2115 struct ieee80211vap *vap = ni->ni_vap;
2116 const struct ieee80211_meshpeer_ie *mpie;
2118 const uint8_t *meshid, *meshconf;
2119 uint8_t sendclose = 0; /* 1 = MPM frame rejected, close will be sent */
2121 meshid = meshconf = NULL;
2122 while (efrm - frm > 1) {
2123 IEEE80211_VERIFY_LENGTH(efrm - frm, frm[1] + 2, return NULL);
2125 case IEEE80211_ELEMID_MESHID:
2128 case IEEE80211_ELEMID_MESHCONF:
2131 case IEEE80211_ELEMID_MESHPEER:
2132 mpie = (const struct ieee80211_meshpeer_ie *) frm;
2133 memset(mp, 0, sizeof(*mp));
2134 mp->peer_len = mpie->peer_len;
2135 mp->peer_proto = le16dec(&mpie->peer_proto);
2136 mp->peer_llinkid = le16dec(&mpie->peer_llinkid);
2138 case IEEE80211_ACTION_MESHPEERING_CONFIRM:
2140 le16dec(&mpie->peer_linkid);
2142 case IEEE80211_ACTION_MESHPEERING_CLOSE:
2143 /* NB: peer link ID is optional */
2144 if (mpie->peer_len ==
2145 (IEEE80211_MPM_BASE_SZ + 2)) {
2146 mp->peer_linkid = 0;
2148 le16dec(&mpie->peer_linkid);
2151 le16dec(&mpie->peer_linkid);
2153 le16dec(&mpie->peer_rcode);
2163 * Verify the contents of the frame.
2164 * If it fails validation, close the peer link.
2166 if (mesh_verify_meshpeer(vap, subtype, (const uint8_t *)mp)) {
2168 IEEE80211_DISCARD(vap,
2169 IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH,
2170 wh, NULL, "%s", "MPM validation failed");
2173 /* If meshid is not the same reject any frames type. */
2174 if (sendclose == 0 && mesh_verify_meshid(vap, meshid)) {
2176 IEEE80211_DISCARD(vap,
2177 IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH,
2178 wh, NULL, "%s", "not for our mesh");
2179 if (subtype == IEEE80211_ACTION_MESHPEERING_CLOSE) {
2181 * Standard not clear about this, if we dont ignore
2182 * there will be an endless loop between nodes sending
2183 * CLOSE frames between each other with wrong meshid.
2184 * Discard and timers will bring FSM to IDLE state.
2191 * Close frames are accepted if meshid is the same.
2192 * Verify the other two types.
2194 if (sendclose == 0 && subtype != IEEE80211_ACTION_MESHPEERING_CLOSE &&
2195 mesh_verify_meshconf(vap, meshconf)) {
2197 IEEE80211_DISCARD(vap,
2198 IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH,
2199 wh, NULL, "%s", "configuration missmatch");
2203 vap->iv_stats.is_rx_mgtdiscard++;
2204 switch (ni->ni_mlstate) {
2205 case IEEE80211_NODE_MESH_IDLE:
2206 case IEEE80211_NODE_MESH_ESTABLISHED:
2207 case IEEE80211_NODE_MESH_HOLDING:
2210 case IEEE80211_NODE_MESH_OPENSNT:
2211 case IEEE80211_NODE_MESH_OPENRCV:
2212 case IEEE80211_NODE_MESH_CONFIRMRCV:
2213 args[0] = ni->ni_mlpid;
2214 args[1] = ni->ni_mllid;
2215 /* Reason codes for rejection */
2217 case IEEE80211_ACTION_MESHPEERING_OPEN:
2218 args[2] = IEEE80211_REASON_MESH_CPVIOLATION;
2220 case IEEE80211_ACTION_MESHPEERING_CONFIRM:
2221 args[2] = IEEE80211_REASON_MESH_INCONS_PARAMS;
2224 ieee80211_send_action(ni,
2225 IEEE80211_ACTION_CAT_SELF_PROT,
2226 IEEE80211_ACTION_MESHPEERING_CLOSE,
2228 mesh_linkchange(ni, IEEE80211_NODE_MESH_HOLDING);
2229 mesh_peer_timeout_setup(ni);
2235 return (const struct ieee80211_meshpeer_ie *) mp;
2239 mesh_recv_action_meshpeering_open(struct ieee80211_node *ni,
2240 const struct ieee80211_frame *wh,
2241 const uint8_t *frm, const uint8_t *efrm)
2243 struct ieee80211vap *vap = ni->ni_vap;
2244 struct ieee80211_mesh_state *ms = vap->iv_mesh;
2245 struct ieee80211_meshpeer_ie ie;
2246 const struct ieee80211_meshpeer_ie *meshpeer;
2249 /* +2+2 for action + code + capabilites */
2250 meshpeer = mesh_parse_meshpeering_action(ni, wh, frm+2+2, efrm, &ie,
2251 IEEE80211_ACTION_MESHPEERING_OPEN);
2252 if (meshpeer == NULL) {
2257 IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH, ni,
2258 "recv PEER OPEN, lid 0x%x", meshpeer->peer_llinkid);
2260 switch (ni->ni_mlstate) {
2261 case IEEE80211_NODE_MESH_IDLE:
2262 /* Reject open request if reached our maximum neighbor count */
2263 if (ms->ms_neighbors >= IEEE80211_MESH_MAX_NEIGHBORS) {
2264 args[0] = meshpeer->peer_llinkid;
2266 args[2] = IEEE80211_REASON_MESH_MAX_PEERS;
2267 ieee80211_send_action(ni,
2268 IEEE80211_ACTION_CAT_SELF_PROT,
2269 IEEE80211_ACTION_MESHPEERING_CLOSE,
2271 /* stay in IDLE state */
2274 /* Open frame accepted */
2275 mesh_linkchange(ni, IEEE80211_NODE_MESH_OPENRCV);
2276 ni->ni_mllid = meshpeer->peer_llinkid;
2277 ni->ni_mlpid = mesh_generateid(vap);
2278 if (ni->ni_mlpid == 0)
2280 args[0] = ni->ni_mlpid;
2281 /* Announce we're open too... */
2282 ieee80211_send_action(ni,
2283 IEEE80211_ACTION_CAT_SELF_PROT,
2284 IEEE80211_ACTION_MESHPEERING_OPEN, args);
2285 /* ...and confirm the link. */
2286 args[0] = ni->ni_mlpid;
2287 args[1] = ni->ni_mllid;
2288 ieee80211_send_action(ni,
2289 IEEE80211_ACTION_CAT_SELF_PROT,
2290 IEEE80211_ACTION_MESHPEERING_CONFIRM,
2292 mesh_peer_timeout_setup(ni);
2294 case IEEE80211_NODE_MESH_OPENRCV:
2296 if (ni->ni_mllid != meshpeer->peer_llinkid) {
2297 args[0] = ni->ni_mllid;
2298 args[1] = ni->ni_mlpid;
2299 args[2] = IEEE80211_REASON_PEER_LINK_CANCELED;
2300 ieee80211_send_action(ni,
2301 IEEE80211_ACTION_CAT_SELF_PROT,
2302 IEEE80211_ACTION_MESHPEERING_CLOSE,
2304 mesh_linkchange(ni, IEEE80211_NODE_MESH_HOLDING);
2305 mesh_peer_timeout_setup(ni);
2308 /* Duplicate open, confirm again. */
2309 args[0] = ni->ni_mlpid;
2310 args[1] = ni->ni_mllid;
2311 ieee80211_send_action(ni,
2312 IEEE80211_ACTION_CAT_SELF_PROT,
2313 IEEE80211_ACTION_MESHPEERING_CONFIRM,
2316 case IEEE80211_NODE_MESH_OPENSNT:
2317 ni->ni_mllid = meshpeer->peer_llinkid;
2318 mesh_linkchange(ni, IEEE80211_NODE_MESH_OPENRCV);
2319 args[0] = ni->ni_mlpid;
2320 args[1] = ni->ni_mllid;
2321 ieee80211_send_action(ni,
2322 IEEE80211_ACTION_CAT_SELF_PROT,
2323 IEEE80211_ACTION_MESHPEERING_CONFIRM,
2325 /* NB: don't setup/clear any timeout */
2327 case IEEE80211_NODE_MESH_CONFIRMRCV:
2328 if (ni->ni_mlpid != meshpeer->peer_linkid ||
2329 ni->ni_mllid != meshpeer->peer_llinkid) {
2330 args[0] = ni->ni_mlpid;
2331 args[1] = ni->ni_mllid;
2332 args[2] = IEEE80211_REASON_PEER_LINK_CANCELED;
2333 ieee80211_send_action(ni,
2334 IEEE80211_ACTION_CAT_SELF_PROT,
2335 IEEE80211_ACTION_MESHPEERING_CLOSE,
2338 IEEE80211_NODE_MESH_HOLDING);
2339 mesh_peer_timeout_setup(ni);
2342 mesh_linkchange(ni, IEEE80211_NODE_MESH_ESTABLISHED);
2343 ni->ni_mllid = meshpeer->peer_llinkid;
2344 args[0] = ni->ni_mlpid;
2345 args[1] = ni->ni_mllid;
2346 ieee80211_send_action(ni,
2347 IEEE80211_ACTION_CAT_SELF_PROT,
2348 IEEE80211_ACTION_MESHPEERING_CONFIRM,
2350 mesh_peer_timeout_stop(ni);
2352 case IEEE80211_NODE_MESH_ESTABLISHED:
2353 if (ni->ni_mllid != meshpeer->peer_llinkid) {
2354 args[0] = ni->ni_mllid;
2355 args[1] = ni->ni_mlpid;
2356 args[2] = IEEE80211_REASON_PEER_LINK_CANCELED;
2357 ieee80211_send_action(ni,
2358 IEEE80211_ACTION_CAT_SELF_PROT,
2359 IEEE80211_ACTION_MESHPEERING_CLOSE,
2361 mesh_linkchange(ni, IEEE80211_NODE_MESH_HOLDING);
2362 mesh_peer_timeout_setup(ni);
2365 args[0] = ni->ni_mlpid;
2366 args[1] = ni->ni_mllid;
2367 ieee80211_send_action(ni,
2368 IEEE80211_ACTION_CAT_SELF_PROT,
2369 IEEE80211_ACTION_MESHPEERING_CONFIRM,
2372 case IEEE80211_NODE_MESH_HOLDING:
2373 args[0] = ni->ni_mlpid;
2374 args[1] = meshpeer->peer_llinkid;
2375 /* Standard not clear about what the reaason code should be */
2376 args[2] = IEEE80211_REASON_PEER_LINK_CANCELED;
2377 ieee80211_send_action(ni,
2378 IEEE80211_ACTION_CAT_SELF_PROT,
2379 IEEE80211_ACTION_MESHPEERING_CLOSE,
2387 mesh_recv_action_meshpeering_confirm(struct ieee80211_node *ni,
2388 const struct ieee80211_frame *wh,
2389 const uint8_t *frm, const uint8_t *efrm)
2391 struct ieee80211vap *vap = ni->ni_vap;
2392 struct ieee80211_meshpeer_ie ie;
2393 const struct ieee80211_meshpeer_ie *meshpeer;
2396 /* +2+2+2+2 for action + code + capabilites + status code + AID */
2397 meshpeer = mesh_parse_meshpeering_action(ni, wh, frm+2+2+2+2, efrm, &ie,
2398 IEEE80211_ACTION_MESHPEERING_CONFIRM);
2399 if (meshpeer == NULL) {
2403 IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH, ni,
2404 "recv PEER CONFIRM, local id 0x%x, peer id 0x%x",
2405 meshpeer->peer_llinkid, meshpeer->peer_linkid);
2407 switch (ni->ni_mlstate) {
2408 case IEEE80211_NODE_MESH_OPENRCV:
2409 mesh_linkchange(ni, IEEE80211_NODE_MESH_ESTABLISHED);
2410 mesh_peer_timeout_stop(ni);
2412 case IEEE80211_NODE_MESH_OPENSNT:
2413 mesh_linkchange(ni, IEEE80211_NODE_MESH_CONFIRMRCV);
2414 mesh_peer_timeout_setup(ni);
2416 case IEEE80211_NODE_MESH_HOLDING:
2417 args[0] = ni->ni_mlpid;
2418 args[1] = meshpeer->peer_llinkid;
2419 /* Standard not clear about what the reaason code should be */
2420 args[2] = IEEE80211_REASON_PEER_LINK_CANCELED;
2421 ieee80211_send_action(ni,
2422 IEEE80211_ACTION_CAT_SELF_PROT,
2423 IEEE80211_ACTION_MESHPEERING_CLOSE,
2426 case IEEE80211_NODE_MESH_CONFIRMRCV:
2427 if (ni->ni_mllid != meshpeer->peer_llinkid) {
2428 args[0] = ni->ni_mlpid;
2429 args[1] = ni->ni_mllid;
2430 args[2] = IEEE80211_REASON_PEER_LINK_CANCELED;
2431 ieee80211_send_action(ni,
2432 IEEE80211_ACTION_CAT_SELF_PROT,
2433 IEEE80211_ACTION_MESHPEERING_CLOSE,
2435 mesh_linkchange(ni, IEEE80211_NODE_MESH_HOLDING);
2436 mesh_peer_timeout_setup(ni);
2440 IEEE80211_DISCARD(vap,
2441 IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH,
2442 wh, NULL, "received confirm in invalid state %d",
2444 vap->iv_stats.is_rx_mgtdiscard++;
2451 mesh_recv_action_meshpeering_close(struct ieee80211_node *ni,
2452 const struct ieee80211_frame *wh,
2453 const uint8_t *frm, const uint8_t *efrm)
2455 struct ieee80211_meshpeer_ie ie;
2456 const struct ieee80211_meshpeer_ie *meshpeer;
2459 /* +2 for action + code */
2460 meshpeer = mesh_parse_meshpeering_action(ni, wh, frm+2, efrm, &ie,
2461 IEEE80211_ACTION_MESHPEERING_CLOSE);
2462 if (meshpeer == NULL) {
2467 * XXX: check reason code, for example we could receive
2468 * IEEE80211_REASON_MESH_MAX_PEERS then we should not attempt
2472 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH,
2473 ni, "%s", "recv PEER CLOSE");
2475 switch (ni->ni_mlstate) {
2476 case IEEE80211_NODE_MESH_IDLE:
2479 case IEEE80211_NODE_MESH_OPENRCV:
2480 case IEEE80211_NODE_MESH_OPENSNT:
2481 case IEEE80211_NODE_MESH_CONFIRMRCV:
2482 case IEEE80211_NODE_MESH_ESTABLISHED:
2483 args[0] = ni->ni_mlpid;
2484 args[1] = ni->ni_mllid;
2485 args[2] = IEEE80211_REASON_MESH_CLOSE_RCVD;
2486 ieee80211_send_action(ni,
2487 IEEE80211_ACTION_CAT_SELF_PROT,
2488 IEEE80211_ACTION_MESHPEERING_CLOSE,
2490 mesh_linkchange(ni, IEEE80211_NODE_MESH_HOLDING);
2491 mesh_peer_timeout_setup(ni);
2493 case IEEE80211_NODE_MESH_HOLDING:
2494 mesh_linkchange(ni, IEEE80211_NODE_MESH_IDLE);
2495 mesh_peer_timeout_stop(ni);
2502 * Link Metric handling.
2505 mesh_recv_action_meshlmetric(struct ieee80211_node *ni,
2506 const struct ieee80211_frame *wh,
2507 const uint8_t *frm, const uint8_t *efrm)
2509 const struct ieee80211_meshlmetric_ie *ie =
2510 (const struct ieee80211_meshlmetric_ie *)
2511 (frm+2); /* action + code */
2512 struct ieee80211_meshlmetric_ie lm_rep;
2514 if (ie->lm_flags & IEEE80211_MESH_LMETRIC_FLAGS_REQ) {
2515 lm_rep.lm_flags = 0;
2516 lm_rep.lm_metric = mesh_airtime_calc(ni);
2517 ieee80211_send_action(ni,
2518 IEEE80211_ACTION_CAT_MESH,
2519 IEEE80211_ACTION_MESH_LMETRIC,
2522 /* XXX: else do nothing for now */
2527 * Parse meshgate action ie's for GANN frames.
2528 * Returns -1 if parsing fails, otherwise 0.
2531 mesh_parse_meshgate_action(struct ieee80211_node *ni,
2532 const struct ieee80211_frame *wh, /* XXX for VERIFY_LENGTH */
2533 struct ieee80211_meshgann_ie *ie, const uint8_t *frm, const uint8_t *efrm)
2535 struct ieee80211vap *vap = ni->ni_vap;
2536 const struct ieee80211_meshgann_ie *gannie;
2538 while (efrm - frm > 1) {
2539 IEEE80211_VERIFY_LENGTH(efrm - frm, frm[1] + 2, return -1);
2541 case IEEE80211_ELEMID_MESHGANN:
2542 gannie = (const struct ieee80211_meshgann_ie *) frm;
2543 memset(ie, 0, sizeof(*ie));
2544 ie->gann_ie = gannie->gann_ie;
2545 ie->gann_len = gannie->gann_len;
2546 ie->gann_flags = gannie->gann_flags;
2547 ie->gann_hopcount = gannie->gann_hopcount;
2548 ie->gann_ttl = gannie->gann_ttl;
2549 IEEE80211_ADDR_COPY(ie->gann_addr, gannie->gann_addr);
2550 ie->gann_seq = le32dec(&gannie->gann_seq);
2551 ie->gann_interval = le16dec(&gannie->gann_interval);
2561 * Mesh Gate Announcement handling.
2564 mesh_recv_action_meshgate(struct ieee80211_node *ni,
2565 const struct ieee80211_frame *wh,
2566 const uint8_t *frm, const uint8_t *efrm)
2568 struct ieee80211vap *vap = ni->ni_vap;
2569 struct ieee80211_mesh_state *ms = vap->iv_mesh;
2570 struct ieee80211_mesh_gate_route *gr, *next;
2571 struct ieee80211_mesh_route *rt_gate;
2572 struct ieee80211_meshgann_ie pgann;
2573 struct ieee80211_meshgann_ie ie;
2576 /* +2 for action + code */
2577 if (mesh_parse_meshgate_action(ni, wh, &ie, frm+2, efrm) != 0) {
2578 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_MESH,
2579 ni->ni_macaddr, NULL, "%s",
2580 "GANN parsing failed");
2581 vap->iv_stats.is_rx_mgtdiscard++;
2585 if (IEEE80211_ADDR_EQ(vap->iv_myaddr, ie.gann_addr))
2588 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH, ni->ni_macaddr,
2589 "received GANN, meshgate: %6D (seq %u)", ie.gann_addr, ":",
2595 TAILQ_FOREACH_SAFE(gr, &ms->ms_known_gates, gr_next, next) {
2596 if (!IEEE80211_ADDR_EQ(gr->gr_addr, ie.gann_addr))
2598 if (ie.gann_seq <= gr->gr_lastseq) {
2599 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_MESH,
2600 ni->ni_macaddr, NULL,
2601 "GANN old seqno %u <= %u",
2602 ie.gann_seq, gr->gr_lastseq);
2606 /* corresponding mesh gate found & GANN accepted */
2612 /* this GANN is from a new mesh Gate add it to known table. */
2613 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH, ie.gann_addr,
2614 "stored new GANN information, seq %u.", ie.gann_seq);
2615 gr = IEEE80211_MALLOC(ALIGN(sizeof(struct ieee80211_mesh_gate_route)),
2617 IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
2618 IEEE80211_ADDR_COPY(gr->gr_addr, ie.gann_addr);
2619 TAILQ_INSERT_TAIL(&ms->ms_known_gates, gr, gr_next);
2621 gr->gr_lastseq = ie.gann_seq;
2623 /* check if we have a path to this gate */
2624 rt_gate = mesh_rt_find_locked(ms, gr->gr_addr);
2625 if (rt_gate != NULL &&
2626 rt_gate->rt_flags & IEEE80211_MESHRT_FLAGS_VALID) {
2627 gr->gr_route = rt_gate;
2628 rt_gate->rt_flags |= IEEE80211_MESHRT_FLAGS_GATE;
2633 /* popagate only if decremented ttl >= 1 && forwarding is enabled */
2634 if ((ie.gann_ttl - 1) < 1 && !(ms->ms_flags & IEEE80211_MESHFLAGS_FWD))
2636 pgann.gann_flags = ie.gann_flags; /* Reserved */
2637 pgann.gann_hopcount = ie.gann_hopcount + 1;
2638 pgann.gann_ttl = ie.gann_ttl - 1;
2639 IEEE80211_ADDR_COPY(pgann.gann_addr, ie.gann_addr);
2640 pgann.gann_seq = ie.gann_seq;
2641 pgann.gann_interval = ie.gann_interval;
2643 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH, ie.gann_addr,
2644 "%s", "propagate GANN");
2646 ieee80211_send_action(vap->iv_bss, IEEE80211_ACTION_CAT_MESH,
2647 IEEE80211_ACTION_MESH_GANN, &pgann);
2653 mesh_send_action(struct ieee80211_node *ni,
2654 const uint8_t sa[IEEE80211_ADDR_LEN],
2655 const uint8_t da[IEEE80211_ADDR_LEN],
2658 struct ieee80211vap *vap = ni->ni_vap;
2659 struct ieee80211com *ic = ni->ni_ic;
2660 struct ieee80211_bpf_params params;
2663 KASSERT(ni != NULL, ("null node"));
2665 if (vap->iv_state == IEEE80211_S_CAC) {
2666 IEEE80211_NOTE(vap, IEEE80211_MSG_OUTPUT, ni,
2667 "block %s frame in CAC state", "Mesh action");
2668 vap->iv_stats.is_tx_badstate++;
2669 ieee80211_free_node(ni);
2671 return EIO; /* XXX */
2674 M_PREPEND(m, sizeof(struct ieee80211_frame), M_NOWAIT);
2676 ieee80211_free_node(ni);
2680 IEEE80211_TX_LOCK(ic);
2681 ieee80211_send_setup(ni, m,
2682 IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_ACTION,
2683 IEEE80211_NONQOS_TID, sa, da, sa);
2684 m->m_flags |= M_ENCAP; /* mark encapsulated */
2686 memset(¶ms, 0, sizeof(params));
2687 params.ibp_pri = WME_AC_VO;
2688 params.ibp_rate0 = ni->ni_txparms->mgmtrate;
2689 if (IEEE80211_IS_MULTICAST(da))
2690 params.ibp_try0 = 1;
2692 params.ibp_try0 = ni->ni_txparms->maxretry;
2693 params.ibp_power = ni->ni_txpower;
2695 IEEE80211_NODE_STAT(ni, tx_mgmt);
2697 ret = ieee80211_raw_output(vap, ni, m, ¶ms);
2698 IEEE80211_TX_UNLOCK(ic);
2702 #define ADDSHORT(frm, v) do { \
2703 frm[0] = (v) & 0xff; \
2704 frm[1] = (v) >> 8; \
2707 #define ADDWORD(frm, v) do { \
2708 frm[0] = (v) & 0xff; \
2709 frm[1] = ((v) >> 8) & 0xff; \
2710 frm[2] = ((v) >> 16) & 0xff; \
2711 frm[3] = ((v) >> 24) & 0xff; \
2716 mesh_send_action_meshpeering_open(struct ieee80211_node *ni,
2717 int category, int action, void *args0)
2719 struct ieee80211vap *vap = ni->ni_vap;
2720 struct ieee80211com *ic = ni->ni_ic;
2721 uint16_t *args = args0;
2722 const struct ieee80211_rateset *rs;
2726 IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH, ni,
2727 "send PEER OPEN action: localid 0x%x", args[0]);
2729 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
2730 "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", __func__, __LINE__,
2731 ni, ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)+1);
2732 ieee80211_ref_node(ni);
2734 m = ieee80211_getmgtframe(&frm,
2735 ic->ic_headroom + sizeof(struct ieee80211_frame),
2736 sizeof(uint16_t) /* action+category */
2737 + sizeof(uint16_t) /* capabilites */
2738 + 2 + IEEE80211_RATE_SIZE
2739 + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
2740 + 2 + IEEE80211_MESHID_LEN
2741 + sizeof(struct ieee80211_meshconf_ie)
2742 + sizeof(struct ieee80211_meshpeer_ie)
2746 * mesh peer open action frame format:
2754 * [tlv] mesh peer link mgmt
2758 ADDSHORT(frm, ieee80211_getcapinfo(vap, ni->ni_chan));
2759 rs = ieee80211_get_suprates(ic, ic->ic_curchan);
2760 frm = ieee80211_add_rates(frm, rs);
2761 frm = ieee80211_add_xrates(frm, rs);
2762 frm = ieee80211_add_meshid(frm, vap);
2763 frm = ieee80211_add_meshconf(frm, vap);
2764 frm = ieee80211_add_meshpeer(frm, IEEE80211_ACTION_MESHPEERING_OPEN,
2766 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2767 return mesh_send_action(ni, vap->iv_myaddr, ni->ni_macaddr, m);
2769 vap->iv_stats.is_tx_nobuf++;
2770 ieee80211_free_node(ni);
2776 mesh_send_action_meshpeering_confirm(struct ieee80211_node *ni,
2777 int category, int action, void *args0)
2779 struct ieee80211vap *vap = ni->ni_vap;
2780 struct ieee80211com *ic = ni->ni_ic;
2781 uint16_t *args = args0;
2782 const struct ieee80211_rateset *rs;
2786 IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH, ni,
2787 "send PEER CONFIRM action: localid 0x%x, peerid 0x%x",
2790 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
2791 "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", __func__, __LINE__,
2792 ni, ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)+1);
2793 ieee80211_ref_node(ni);
2795 m = ieee80211_getmgtframe(&frm,
2796 ic->ic_headroom + sizeof(struct ieee80211_frame),
2797 sizeof(uint16_t) /* action+category */
2798 + sizeof(uint16_t) /* capabilites */
2799 + sizeof(uint16_t) /* status code */
2800 + sizeof(uint16_t) /* AID */
2801 + 2 + IEEE80211_RATE_SIZE
2802 + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
2803 + 2 + IEEE80211_MESHID_LEN
2804 + sizeof(struct ieee80211_meshconf_ie)
2805 + sizeof(struct ieee80211_meshpeer_ie)
2809 * mesh peer confirm action frame format:
2814 * [2] association id (peer ID)
2819 * [tlv] mesh peer link mgmt
2823 ADDSHORT(frm, ieee80211_getcapinfo(vap, ni->ni_chan));
2824 ADDSHORT(frm, 0); /* status code */
2825 ADDSHORT(frm, args[1]); /* AID */
2826 rs = ieee80211_get_suprates(ic, ic->ic_curchan);
2827 frm = ieee80211_add_rates(frm, rs);
2828 frm = ieee80211_add_xrates(frm, rs);
2829 frm = ieee80211_add_meshid(frm, vap);
2830 frm = ieee80211_add_meshconf(frm, vap);
2831 frm = ieee80211_add_meshpeer(frm,
2832 IEEE80211_ACTION_MESHPEERING_CONFIRM,
2833 args[0], args[1], 0);
2834 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2835 return mesh_send_action(ni, vap->iv_myaddr, ni->ni_macaddr, m);
2837 vap->iv_stats.is_tx_nobuf++;
2838 ieee80211_free_node(ni);
2844 mesh_send_action_meshpeering_close(struct ieee80211_node *ni,
2845 int category, int action, void *args0)
2847 struct ieee80211vap *vap = ni->ni_vap;
2848 struct ieee80211com *ic = ni->ni_ic;
2849 uint16_t *args = args0;
2853 IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH, ni,
2854 "send PEER CLOSE action: localid 0x%x, peerid 0x%x reason %d (%s)",
2855 args[0], args[1], args[2], ieee80211_reason_to_string(args[2]));
2857 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
2858 "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", __func__, __LINE__,
2859 ni, ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)+1);
2860 ieee80211_ref_node(ni);
2862 m = ieee80211_getmgtframe(&frm,
2863 ic->ic_headroom + sizeof(struct ieee80211_frame),
2864 sizeof(uint16_t) /* action+category */
2865 + sizeof(uint16_t) /* reason code */
2866 + 2 + IEEE80211_MESHID_LEN
2867 + sizeof(struct ieee80211_meshpeer_ie)
2871 * mesh peer close action frame format:
2875 * [tlv] mesh peer link mgmt
2879 frm = ieee80211_add_meshid(frm, vap);
2880 frm = ieee80211_add_meshpeer(frm,
2881 IEEE80211_ACTION_MESHPEERING_CLOSE,
2882 args[0], args[1], args[2]);
2883 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2884 return mesh_send_action(ni, vap->iv_myaddr, ni->ni_macaddr, m);
2886 vap->iv_stats.is_tx_nobuf++;
2887 ieee80211_free_node(ni);
2893 mesh_send_action_meshlmetric(struct ieee80211_node *ni,
2894 int category, int action, void *arg0)
2896 struct ieee80211vap *vap = ni->ni_vap;
2897 struct ieee80211com *ic = ni->ni_ic;
2898 struct ieee80211_meshlmetric_ie *ie = arg0;
2902 if (ie->lm_flags & IEEE80211_MESH_LMETRIC_FLAGS_REQ) {
2903 IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH,
2904 ni, "%s", "send LINK METRIC REQUEST action");
2906 IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH,
2907 ni, "send LINK METRIC REPLY action: metric 0x%x",
2910 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
2911 "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", __func__, __LINE__,
2912 ni, ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)+1);
2913 ieee80211_ref_node(ni);
2915 m = ieee80211_getmgtframe(&frm,
2916 ic->ic_headroom + sizeof(struct ieee80211_frame),
2917 sizeof(uint16_t) + /* action+category */
2918 sizeof(struct ieee80211_meshlmetric_ie)
2925 * [tlv] mesh link metric
2929 frm = ieee80211_add_meshlmetric(frm,
2930 ie->lm_flags, ie->lm_metric);
2931 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2932 return mesh_send_action(ni, vap->iv_myaddr, ni->ni_macaddr, m);
2934 vap->iv_stats.is_tx_nobuf++;
2935 ieee80211_free_node(ni);
2941 mesh_send_action_meshgate(struct ieee80211_node *ni,
2942 int category, int action, void *arg0)
2944 struct ieee80211vap *vap = ni->ni_vap;
2945 struct ieee80211com *ic = ni->ni_ic;
2946 struct ieee80211_meshgann_ie *ie = arg0;
2950 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
2951 "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", __func__, __LINE__,
2952 ni, ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)+1);
2953 ieee80211_ref_node(ni);
2955 m = ieee80211_getmgtframe(&frm,
2956 ic->ic_headroom + sizeof(struct ieee80211_frame),
2957 sizeof(uint16_t) + /* action+category */
2958 IEEE80211_MESHGANN_BASE_SZ
2965 * [tlv] mesh gate annoucement
2969 frm = ieee80211_add_meshgate(frm, ie);
2970 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2971 return mesh_send_action(ni, vap->iv_myaddr, broadcastaddr, m);
2973 vap->iv_stats.is_tx_nobuf++;
2974 ieee80211_free_node(ni);
2980 mesh_peer_timeout_setup(struct ieee80211_node *ni)
2982 switch (ni->ni_mlstate) {
2983 case IEEE80211_NODE_MESH_HOLDING:
2984 ni->ni_mltval = ieee80211_mesh_holdingtimeout;
2986 case IEEE80211_NODE_MESH_CONFIRMRCV:
2987 ni->ni_mltval = ieee80211_mesh_confirmtimeout;
2989 case IEEE80211_NODE_MESH_IDLE:
2993 ni->ni_mltval = ieee80211_mesh_retrytimeout;
2997 callout_reset(&ni->ni_mltimer, ni->ni_mltval,
2998 mesh_peer_timeout_cb, ni);
3002 * Same as above but backoffs timer statisically 50%.
3005 mesh_peer_timeout_backoff(struct ieee80211_node *ni)
3010 ni->ni_mltval += r % ni->ni_mltval;
3011 callout_reset(&ni->ni_mltimer, ni->ni_mltval, mesh_peer_timeout_cb,
3015 static __inline void
3016 mesh_peer_timeout_stop(struct ieee80211_node *ni)
3018 callout_drain(&ni->ni_mltimer);
3022 mesh_peer_backoff_cb(void *arg)
3024 struct ieee80211_node *ni = (struct ieee80211_node *)arg;
3026 /* After backoff timeout, try to peer automatically again. */
3031 * Mesh Peer Link Management FSM timeout handling.
3034 mesh_peer_timeout_cb(void *arg)
3036 struct ieee80211_node *ni = (struct ieee80211_node *)arg;
3039 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_MESH,
3040 ni, "mesh link timeout, state %d, retry counter %d",
3041 ni->ni_mlstate, ni->ni_mlrcnt);
3043 switch (ni->ni_mlstate) {
3044 case IEEE80211_NODE_MESH_IDLE:
3045 case IEEE80211_NODE_MESH_ESTABLISHED:
3047 case IEEE80211_NODE_MESH_OPENSNT:
3048 case IEEE80211_NODE_MESH_OPENRCV:
3049 if (ni->ni_mlrcnt == ieee80211_mesh_maxretries) {
3050 args[0] = ni->ni_mlpid;
3051 args[2] = IEEE80211_REASON_MESH_MAX_RETRIES;
3052 ieee80211_send_action(ni,
3053 IEEE80211_ACTION_CAT_SELF_PROT,
3054 IEEE80211_ACTION_MESHPEERING_CLOSE, args);
3056 mesh_linkchange(ni, IEEE80211_NODE_MESH_HOLDING);
3057 mesh_peer_timeout_setup(ni);
3059 args[0] = ni->ni_mlpid;
3060 ieee80211_send_action(ni,
3061 IEEE80211_ACTION_CAT_SELF_PROT,
3062 IEEE80211_ACTION_MESHPEERING_OPEN, args);
3064 mesh_peer_timeout_backoff(ni);
3067 case IEEE80211_NODE_MESH_CONFIRMRCV:
3068 args[0] = ni->ni_mlpid;
3069 args[2] = IEEE80211_REASON_MESH_CONFIRM_TIMEOUT;
3070 ieee80211_send_action(ni,
3071 IEEE80211_ACTION_CAT_SELF_PROT,
3072 IEEE80211_ACTION_MESHPEERING_CLOSE, args);
3073 mesh_linkchange(ni, IEEE80211_NODE_MESH_HOLDING);
3074 mesh_peer_timeout_setup(ni);
3076 case IEEE80211_NODE_MESH_HOLDING:
3078 if (ni->ni_mlhcnt >= ieee80211_mesh_maxholding)
3079 callout_reset(&ni->ni_mlhtimer,
3080 ieee80211_mesh_backofftimeout,
3081 mesh_peer_backoff_cb, ni);
3082 mesh_linkchange(ni, IEEE80211_NODE_MESH_IDLE);
3088 mesh_verify_meshid(struct ieee80211vap *vap, const uint8_t *ie)
3090 struct ieee80211_mesh_state *ms = vap->iv_mesh;
3092 if (ie == NULL || ie[1] != ms->ms_idlen)
3094 return memcmp(ms->ms_id, ie + 2, ms->ms_idlen);
3098 * Check if we are using the same algorithms for this mesh.
3101 mesh_verify_meshconf(struct ieee80211vap *vap, const uint8_t *ie)
3103 const struct ieee80211_meshconf_ie *meshconf =
3104 (const struct ieee80211_meshconf_ie *) ie;
3105 const struct ieee80211_mesh_state *ms = vap->iv_mesh;
3107 if (meshconf == NULL)
3109 if (meshconf->conf_pselid != ms->ms_ppath->mpp_ie) {
3110 IEEE80211_DPRINTF(vap, IEEE80211_MSG_MESH,
3111 "unknown path selection algorithm: 0x%x\n",
3112 meshconf->conf_pselid);
3115 if (meshconf->conf_pmetid != ms->ms_pmetric->mpm_ie) {
3116 IEEE80211_DPRINTF(vap, IEEE80211_MSG_MESH,
3117 "unknown path metric algorithm: 0x%x\n",
3118 meshconf->conf_pmetid);
3121 if (meshconf->conf_ccid != 0) {
3122 IEEE80211_DPRINTF(vap, IEEE80211_MSG_MESH,
3123 "unknown congestion control algorithm: 0x%x\n",
3124 meshconf->conf_ccid);
3127 if (meshconf->conf_syncid != IEEE80211_MESHCONF_SYNC_NEIGHOFF) {
3128 IEEE80211_DPRINTF(vap, IEEE80211_MSG_MESH,
3129 "unknown sync algorithm: 0x%x\n",
3130 meshconf->conf_syncid);
3133 if (meshconf->conf_authid != 0) {
3134 IEEE80211_DPRINTF(vap, IEEE80211_MSG_MESH,
3135 "unknown auth auth algorithm: 0x%x\n",
3136 meshconf->conf_pselid);
3139 /* Not accepting peers */
3140 if (!(meshconf->conf_cap & IEEE80211_MESHCONF_CAP_AP)) {
3141 IEEE80211_DPRINTF(vap, IEEE80211_MSG_MESH,
3142 "not accepting peers: 0x%x\n", meshconf->conf_cap);
3149 mesh_verify_meshpeer(struct ieee80211vap *vap, uint8_t subtype,
3152 const struct ieee80211_meshpeer_ie *meshpeer =
3153 (const struct ieee80211_meshpeer_ie *) ie;
3155 if (meshpeer == NULL ||
3156 meshpeer->peer_len < IEEE80211_MPM_BASE_SZ ||
3157 meshpeer->peer_len > IEEE80211_MPM_MAX_SZ)
3159 if (meshpeer->peer_proto != IEEE80211_MPPID_MPM) {
3160 IEEE80211_DPRINTF(vap,
3161 IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH,
3162 "Only MPM protocol is supported (proto: 0x%02X)",
3163 meshpeer->peer_proto);
3167 case IEEE80211_ACTION_MESHPEERING_OPEN:
3168 if (meshpeer->peer_len != IEEE80211_MPM_BASE_SZ)
3171 case IEEE80211_ACTION_MESHPEERING_CONFIRM:
3172 if (meshpeer->peer_len != IEEE80211_MPM_BASE_SZ + 2)
3175 case IEEE80211_ACTION_MESHPEERING_CLOSE:
3176 if (meshpeer->peer_len < IEEE80211_MPM_BASE_SZ + 2)
3178 if (meshpeer->peer_len == (IEEE80211_MPM_BASE_SZ + 2) &&
3179 meshpeer->peer_linkid != 0)
3181 if (meshpeer->peer_rcode == 0)
3189 * Add a Mesh ID IE to a frame.
3192 ieee80211_add_meshid(uint8_t *frm, struct ieee80211vap *vap)
3194 struct ieee80211_mesh_state *ms = vap->iv_mesh;
3196 KASSERT(vap->iv_opmode == IEEE80211_M_MBSS, ("not a mbss vap"));
3198 *frm++ = IEEE80211_ELEMID_MESHID;
3199 *frm++ = ms->ms_idlen;
3200 memcpy(frm, ms->ms_id, ms->ms_idlen);
3201 return frm + ms->ms_idlen;
3205 * Add a Mesh Configuration IE to a frame.
3206 * For now just use HWMP routing, Airtime link metric, Null Congestion
3207 * Signaling, Null Sync Protocol and Null Authentication.
3210 ieee80211_add_meshconf(uint8_t *frm, struct ieee80211vap *vap)
3212 const struct ieee80211_mesh_state *ms = vap->iv_mesh;
3215 KASSERT(vap->iv_opmode == IEEE80211_M_MBSS, ("not a MBSS vap"));
3217 *frm++ = IEEE80211_ELEMID_MESHCONF;
3218 *frm++ = IEEE80211_MESH_CONF_SZ;
3219 *frm++ = ms->ms_ppath->mpp_ie; /* path selection */
3220 *frm++ = ms->ms_pmetric->mpm_ie; /* link metric */
3221 *frm++ = IEEE80211_MESHCONF_CC_DISABLED;
3222 *frm++ = IEEE80211_MESHCONF_SYNC_NEIGHOFF;
3223 *frm++ = IEEE80211_MESHCONF_AUTH_DISABLED;
3224 /* NB: set the number of neighbors before the rest */
3225 *frm = (ms->ms_neighbors > IEEE80211_MESH_MAX_NEIGHBORS ?
3226 IEEE80211_MESH_MAX_NEIGHBORS : ms->ms_neighbors) << 1;
3227 if (ms->ms_flags & IEEE80211_MESHFLAGS_GATE)
3228 *frm |= IEEE80211_MESHCONF_FORM_GATE;
3231 if (ms->ms_flags & IEEE80211_MESHFLAGS_AP)
3232 caps |= IEEE80211_MESHCONF_CAP_AP;
3233 if (ms->ms_flags & IEEE80211_MESHFLAGS_FWD)
3234 caps |= IEEE80211_MESHCONF_CAP_FWRD;
3240 * Add a Mesh Peer Management IE to a frame.
3243 ieee80211_add_meshpeer(uint8_t *frm, uint8_t subtype, uint16_t localid,
3244 uint16_t peerid, uint16_t reason)
3247 KASSERT(localid != 0, ("localid == 0"));
3249 *frm++ = IEEE80211_ELEMID_MESHPEER;
3251 case IEEE80211_ACTION_MESHPEERING_OPEN:
3252 *frm++ = IEEE80211_MPM_BASE_SZ; /* length */
3253 ADDSHORT(frm, IEEE80211_MPPID_MPM); /* proto */
3254 ADDSHORT(frm, localid); /* local ID */
3256 case IEEE80211_ACTION_MESHPEERING_CONFIRM:
3257 KASSERT(peerid != 0, ("sending peer confirm without peer id"));
3258 *frm++ = IEEE80211_MPM_BASE_SZ + 2; /* length */
3259 ADDSHORT(frm, IEEE80211_MPPID_MPM); /* proto */
3260 ADDSHORT(frm, localid); /* local ID */
3261 ADDSHORT(frm, peerid); /* peer ID */
3263 case IEEE80211_ACTION_MESHPEERING_CLOSE:
3265 *frm++ = IEEE80211_MPM_MAX_SZ; /* length */
3267 *frm++ = IEEE80211_MPM_BASE_SZ + 2; /* length */
3268 ADDSHORT(frm, IEEE80211_MPPID_MPM); /* proto */
3269 ADDSHORT(frm, localid); /* local ID */
3271 ADDSHORT(frm, peerid); /* peer ID */
3272 ADDSHORT(frm, reason);
3279 * Compute an Airtime Link Metric for the link with this node.
3281 * Based on Draft 3.0 spec (11B.10, p.149).
3284 * Max 802.11s overhead.
3286 #define IEEE80211_MESH_MAXOVERHEAD \
3287 (sizeof(struct ieee80211_qosframe_addr4) \
3288 + sizeof(struct ieee80211_meshcntl_ae10) \
3289 + sizeof(struct llc) \
3290 + IEEE80211_ADDR_LEN \
3291 + IEEE80211_WEP_IVLEN \
3292 + IEEE80211_WEP_KIDLEN \
3293 + IEEE80211_WEP_CRCLEN \
3294 + IEEE80211_WEP_MICLEN \
3295 + IEEE80211_CRC_LEN)
3297 mesh_airtime_calc(struct ieee80211_node *ni)
3300 #define S_FACTOR (2 * M_BITS)
3301 struct ieee80211com *ic = ni->ni_ic;
3302 struct ifnet *ifp = ni->ni_vap->iv_ifp;
3303 const static int nbits = 8192 << M_BITS;
3304 uint32_t overhead, rate, errrate;
3307 /* Time to transmit a frame */
3308 rate = ni->ni_txrate;
3309 overhead = ieee80211_compute_duration(ic->ic_rt,
3310 ifp->if_mtu + IEEE80211_MESH_MAXOVERHEAD, rate, 0) << M_BITS;
3311 /* Error rate in percentage */
3312 /* XXX assuming small failures are ok */
3313 errrate = (((ifp->if_get_counter(ifp, IFCOUNTER_OERRORS) +
3314 ifp->if_get_counter(ifp, IFCOUNTER_IERRORS)) / 100) << M_BITS)
3316 res = (overhead + (nbits / rate)) *
3317 ((1 << S_FACTOR) / ((1 << M_BITS) - errrate));
3319 return (uint32_t)(res >> S_FACTOR);
3325 * Add a Mesh Link Metric report IE to a frame.
3328 ieee80211_add_meshlmetric(uint8_t *frm, uint8_t flags, uint32_t metric)
3330 *frm++ = IEEE80211_ELEMID_MESHLINK;
3333 ADDWORD(frm, metric);
3338 * Add a Mesh Gate Announcement IE to a frame.
3341 ieee80211_add_meshgate(uint8_t *frm, struct ieee80211_meshgann_ie *ie)
3343 *frm++ = IEEE80211_ELEMID_MESHGANN; /* ie */
3344 *frm++ = IEEE80211_MESHGANN_BASE_SZ; /* len */
3345 *frm++ = ie->gann_flags;
3346 *frm++ = ie->gann_hopcount;
3347 *frm++ = ie->gann_ttl;
3348 IEEE80211_ADDR_COPY(frm, ie->gann_addr);
3350 ADDWORD(frm, ie->gann_seq);
3351 ADDSHORT(frm, ie->gann_interval);
3358 * Initialize any mesh-specific node state.
3361 ieee80211_mesh_node_init(struct ieee80211vap *vap, struct ieee80211_node *ni)
3363 ni->ni_flags |= IEEE80211_NODE_QOS;
3364 callout_init(&ni->ni_mltimer, 1);
3365 callout_init(&ni->ni_mlhtimer, 1);
3369 * Cleanup any mesh-specific node state.
3372 ieee80211_mesh_node_cleanup(struct ieee80211_node *ni)
3374 struct ieee80211vap *vap = ni->ni_vap;
3375 struct ieee80211_mesh_state *ms = vap->iv_mesh;
3377 callout_drain(&ni->ni_mltimer);
3378 callout_drain(&ni->ni_mlhtimer);
3379 /* NB: short-circuit callbacks after mesh_vdetach */
3380 if (vap->iv_mesh != NULL)
3381 ms->ms_ppath->mpp_peerdown(ni);
3385 ieee80211_parse_meshid(struct ieee80211_node *ni, const uint8_t *ie)
3387 ni->ni_meshidlen = ie[1];
3388 memcpy(ni->ni_meshid, ie + 2, ie[1]);
3392 * Setup mesh-specific node state on neighbor discovery.
3395 ieee80211_mesh_init_neighbor(struct ieee80211_node *ni,
3396 const struct ieee80211_frame *wh,
3397 const struct ieee80211_scanparams *sp)
3399 ieee80211_parse_meshid(ni, sp->meshid);
3403 ieee80211_mesh_update_beacon(struct ieee80211vap *vap,
3404 struct ieee80211_beacon_offsets *bo)
3406 KASSERT(vap->iv_opmode == IEEE80211_M_MBSS, ("not a MBSS vap"));
3408 if (isset(bo->bo_flags, IEEE80211_BEACON_MESHCONF)) {
3409 (void)ieee80211_add_meshconf(bo->bo_meshconf, vap);
3410 clrbit(bo->bo_flags, IEEE80211_BEACON_MESHCONF);
3415 mesh_ioctl_get80211(struct ieee80211vap *vap, struct ieee80211req *ireq)
3417 struct ieee80211_mesh_state *ms = vap->iv_mesh;
3418 uint8_t tmpmeshid[IEEE80211_NWID_LEN];
3419 struct ieee80211_mesh_route *rt;
3420 struct ieee80211req_mesh_route *imr;
3425 if (vap->iv_opmode != IEEE80211_M_MBSS)
3429 switch (ireq->i_type) {
3430 case IEEE80211_IOC_MESH_ID:
3431 ireq->i_len = ms->ms_idlen;
3432 memcpy(tmpmeshid, ms->ms_id, ireq->i_len);
3433 error = copyout(tmpmeshid, ireq->i_data, ireq->i_len);
3435 case IEEE80211_IOC_MESH_AP:
3436 ireq->i_val = (ms->ms_flags & IEEE80211_MESHFLAGS_AP) != 0;
3438 case IEEE80211_IOC_MESH_FWRD:
3439 ireq->i_val = (ms->ms_flags & IEEE80211_MESHFLAGS_FWD) != 0;
3441 case IEEE80211_IOC_MESH_GATE:
3442 ireq->i_val = (ms->ms_flags & IEEE80211_MESHFLAGS_GATE) != 0;
3444 case IEEE80211_IOC_MESH_TTL:
3445 ireq->i_val = ms->ms_ttl;
3447 case IEEE80211_IOC_MESH_RTCMD:
3448 switch (ireq->i_val) {
3449 case IEEE80211_MESH_RTCMD_LIST:
3452 TAILQ_FOREACH(rt, &ms->ms_routes, rt_next) {
3453 len += sizeof(*imr);
3456 if (len > ireq->i_len || ireq->i_len < sizeof(*imr)) {
3462 p = IEEE80211_MALLOC(len, M_TEMP,
3463 IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
3468 TAILQ_FOREACH(rt, &ms->ms_routes, rt_next) {
3471 imr = (struct ieee80211req_mesh_route *)
3473 IEEE80211_ADDR_COPY(imr->imr_dest,
3475 IEEE80211_ADDR_COPY(imr->imr_nexthop,
3477 imr->imr_metric = rt->rt_metric;
3478 imr->imr_nhops = rt->rt_nhops;
3480 ieee80211_mesh_rt_update(rt, 0);
3481 imr->imr_lastmseq = rt->rt_lastmseq;
3482 imr->imr_flags = rt->rt_flags; /* last */
3483 off += sizeof(*imr);
3486 error = copyout(p, (uint8_t *)ireq->i_data,
3488 IEEE80211_FREE(p, M_TEMP);
3490 case IEEE80211_MESH_RTCMD_FLUSH:
3491 case IEEE80211_MESH_RTCMD_ADD:
3492 case IEEE80211_MESH_RTCMD_DELETE:
3498 case IEEE80211_IOC_MESH_PR_METRIC:
3499 len = strlen(ms->ms_pmetric->mpm_descr);
3500 if (ireq->i_len < len)
3503 error = copyout(ms->ms_pmetric->mpm_descr,
3504 (uint8_t *)ireq->i_data, len);
3506 case IEEE80211_IOC_MESH_PR_PATH:
3507 len = strlen(ms->ms_ppath->mpp_descr);
3508 if (ireq->i_len < len)
3511 error = copyout(ms->ms_ppath->mpp_descr,
3512 (uint8_t *)ireq->i_data, len);
3520 IEEE80211_IOCTL_GET(mesh, mesh_ioctl_get80211);
3523 mesh_ioctl_set80211(struct ieee80211vap *vap, struct ieee80211req *ireq)
3525 struct ieee80211_mesh_state *ms = vap->iv_mesh;
3526 uint8_t tmpmeshid[IEEE80211_NWID_LEN];
3527 uint8_t tmpaddr[IEEE80211_ADDR_LEN];
3528 char tmpproto[IEEE80211_MESH_PROTO_DSZ];
3531 if (vap->iv_opmode != IEEE80211_M_MBSS)
3535 switch (ireq->i_type) {
3536 case IEEE80211_IOC_MESH_ID:
3537 if (ireq->i_val != 0 || ireq->i_len > IEEE80211_MESHID_LEN)
3539 error = copyin(ireq->i_data, tmpmeshid, ireq->i_len);
3542 memset(ms->ms_id, 0, IEEE80211_NWID_LEN);
3543 ms->ms_idlen = ireq->i_len;
3544 memcpy(ms->ms_id, tmpmeshid, ireq->i_len);
3547 case IEEE80211_IOC_MESH_AP:
3549 ms->ms_flags |= IEEE80211_MESHFLAGS_AP;
3551 ms->ms_flags &= ~IEEE80211_MESHFLAGS_AP;
3554 case IEEE80211_IOC_MESH_FWRD:
3556 ms->ms_flags |= IEEE80211_MESHFLAGS_FWD;
3558 ms->ms_flags &= ~IEEE80211_MESHFLAGS_FWD;
3559 mesh_gatemode_setup(vap);
3561 case IEEE80211_IOC_MESH_GATE:
3563 ms->ms_flags |= IEEE80211_MESHFLAGS_GATE;
3565 ms->ms_flags &= ~IEEE80211_MESHFLAGS_GATE;
3567 case IEEE80211_IOC_MESH_TTL:
3568 ms->ms_ttl = (uint8_t) ireq->i_val;
3570 case IEEE80211_IOC_MESH_RTCMD:
3571 switch (ireq->i_val) {
3572 case IEEE80211_MESH_RTCMD_LIST:
3574 case IEEE80211_MESH_RTCMD_FLUSH:
3575 ieee80211_mesh_rt_flush(vap);
3577 case IEEE80211_MESH_RTCMD_ADD:
3578 error = copyin(ireq->i_data, tmpaddr,
3579 IEEE80211_ADDR_LEN);
3582 if (IEEE80211_ADDR_EQ(vap->iv_myaddr, tmpaddr) ||
3583 IEEE80211_ADDR_EQ(broadcastaddr, tmpaddr))
3585 ieee80211_mesh_discover(vap, tmpaddr, NULL);
3587 case IEEE80211_MESH_RTCMD_DELETE:
3588 error = copyin(ireq->i_data, tmpaddr,
3589 IEEE80211_ADDR_LEN);
3592 ieee80211_mesh_rt_del(vap, tmpaddr);
3598 case IEEE80211_IOC_MESH_PR_METRIC:
3599 error = copyin(ireq->i_data, tmpproto, sizeof(tmpproto));
3601 error = mesh_select_proto_metric(vap, tmpproto);
3606 case IEEE80211_IOC_MESH_PR_PATH:
3607 error = copyin(ireq->i_data, tmpproto, sizeof(tmpproto));
3609 error = mesh_select_proto_path(vap, tmpproto);
3619 IEEE80211_IOCTL_SET(mesh, mesh_ioctl_set80211);