2 * Copyright (c) 2009 The FreeBSD Foundation
5 * This software was developed by Rui Paulo under sponsorship from the
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
35 * IEEE 802.11s Mesh Point (MBSS) support.
37 * Based on March 2009, D3.0 802.11s draft spec.
42 #include <sys/param.h>
43 #include <sys/systm.h>
45 #include <sys/malloc.h>
46 #include <sys/kernel.h>
48 #include <sys/socket.h>
49 #include <sys/sockio.h>
50 #include <sys/endian.h>
51 #include <sys/errno.h>
53 #include <sys/sysctl.h>
57 #include <net/if_var.h>
58 #include <net/if_media.h>
59 #include <net/if_llc.h>
60 #include <net/ethernet.h>
62 #include <netproto/802_11/ieee80211_var.h>
63 #include <netproto/802_11/ieee80211_action.h>
64 #ifdef IEEE80211_SUPPORT_SUPERG
65 #include <netproto/802_11/ieee80211_superg.h>
67 #include <netproto/802_11/ieee80211_input.h>
68 #include <netproto/802_11/ieee80211_mesh.h>
70 static void mesh_rt_flush_invalid(struct ieee80211vap *);
71 static int mesh_select_proto_path(struct ieee80211vap *, const char *);
72 static int mesh_select_proto_metric(struct ieee80211vap *, const char *);
73 static void mesh_vattach(struct ieee80211vap *);
74 static int mesh_newstate(struct ieee80211vap *, enum ieee80211_state, int);
75 static void mesh_rt_cleanup_cb(void *);
76 static void mesh_gatemode_setup(struct ieee80211vap *);
77 static void mesh_gatemode_cb(void *);
78 static void mesh_linkchange(struct ieee80211_node *,
79 enum ieee80211_mesh_mlstate);
80 static void mesh_checkid(void *, struct ieee80211_node *);
81 static uint32_t mesh_generateid(struct ieee80211vap *);
82 static int mesh_checkpseq(struct ieee80211vap *,
83 const uint8_t [IEEE80211_ADDR_LEN], uint32_t);
84 static void mesh_transmit_to_gate(struct ieee80211vap *, struct mbuf *,
85 struct ieee80211_mesh_route *);
86 static void mesh_forward(struct ieee80211vap *, struct mbuf *,
87 const struct ieee80211_meshcntl *);
88 static int mesh_input(struct ieee80211_node *, struct mbuf *, int, int);
89 static void mesh_recv_mgmt(struct ieee80211_node *, struct mbuf *, int,
91 static void mesh_recv_ctl(struct ieee80211_node *, struct mbuf *, int);
92 static void mesh_peer_timeout_setup(struct ieee80211_node *);
93 static void mesh_peer_timeout_backoff(struct ieee80211_node *);
94 static void mesh_peer_timeout_cb(void *);
96 mesh_peer_timeout_stop(struct ieee80211_node *);
97 static int mesh_verify_meshid(struct ieee80211vap *, const uint8_t *);
98 static int mesh_verify_meshconf(struct ieee80211vap *, const uint8_t *);
99 static int mesh_verify_meshpeer(struct ieee80211vap *, uint8_t,
101 uint32_t mesh_airtime_calc(struct ieee80211_node *);
104 * Timeout values come from the specification and are in milliseconds.
106 static SYSCTL_NODE(_net_wlan, OID_AUTO, mesh, CTLFLAG_RD, 0,
107 "IEEE 802.11s parameters");
108 static int ieee80211_mesh_gateint = -1;
109 SYSCTL_PROC(_net_wlan_mesh, OID_AUTO, gateint, CTLTYPE_INT | CTLFLAG_RW,
110 &ieee80211_mesh_gateint, 0, ieee80211_sysctl_msecs_ticks, "I",
111 "mesh gate interval (ms)");
112 static int ieee80211_mesh_retrytimeout = -1;
113 SYSCTL_PROC(_net_wlan_mesh, OID_AUTO, retrytimeout, CTLTYPE_INT | CTLFLAG_RW,
114 &ieee80211_mesh_retrytimeout, 0, ieee80211_sysctl_msecs_ticks, "I",
115 "Retry timeout (msec)");
116 static int ieee80211_mesh_holdingtimeout = -1;
118 SYSCTL_PROC(_net_wlan_mesh, OID_AUTO, holdingtimeout, CTLTYPE_INT | CTLFLAG_RW,
119 &ieee80211_mesh_holdingtimeout, 0, ieee80211_sysctl_msecs_ticks, "I",
120 "Holding state timeout (msec)");
121 static int ieee80211_mesh_confirmtimeout = -1;
122 SYSCTL_PROC(_net_wlan_mesh, OID_AUTO, confirmtimeout, CTLTYPE_INT | CTLFLAG_RW,
123 &ieee80211_mesh_confirmtimeout, 0, ieee80211_sysctl_msecs_ticks, "I",
124 "Confirm state timeout (msec)");
125 static int ieee80211_mesh_backofftimeout = -1;
126 SYSCTL_PROC(_net_wlan_mesh, OID_AUTO, backofftimeout, CTLTYPE_INT | CTLFLAG_RW,
127 &ieee80211_mesh_backofftimeout, 0, ieee80211_sysctl_msecs_ticks, "I",
128 "Backoff timeout (msec). This is to throutles peering forever when "
129 "not receiving answer or is rejected by a neighbor");
130 static int ieee80211_mesh_maxretries = 2;
131 SYSCTL_INT(_net_wlan_mesh, OID_AUTO, maxretries, CTLFLAG_RW,
132 &ieee80211_mesh_maxretries, 0,
133 "Maximum retries during peer link establishment");
134 static int ieee80211_mesh_maxholding = 2;
135 SYSCTL_INT(_net_wlan_mesh, OID_AUTO, maxholding, CTLFLAG_RW,
136 &ieee80211_mesh_maxholding, 0,
137 "Maximum times we are allowed to transition to HOLDING state before "
138 "backinoff during peer link establishment");
140 static const uint8_t broadcastaddr[IEEE80211_ADDR_LEN] =
141 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
143 static ieee80211_recv_action_func mesh_recv_action_meshpeering_open;
144 static ieee80211_recv_action_func mesh_recv_action_meshpeering_confirm;
145 static ieee80211_recv_action_func mesh_recv_action_meshpeering_close;
146 static ieee80211_recv_action_func mesh_recv_action_meshlmetric;
147 static ieee80211_recv_action_func mesh_recv_action_meshgate;
149 static ieee80211_send_action_func mesh_send_action_meshpeering_open;
150 static ieee80211_send_action_func mesh_send_action_meshpeering_confirm;
151 static ieee80211_send_action_func mesh_send_action_meshpeering_close;
152 static ieee80211_send_action_func mesh_send_action_meshlmetric;
153 static ieee80211_send_action_func mesh_send_action_meshgate;
155 static const struct ieee80211_mesh_proto_metric mesh_metric_airtime = {
156 .mpm_descr = "AIRTIME",
157 .mpm_ie = IEEE80211_MESHCONF_METRIC_AIRTIME,
158 .mpm_metric = mesh_airtime_calc,
161 static struct ieee80211_mesh_proto_path mesh_proto_paths[4];
162 static struct ieee80211_mesh_proto_metric mesh_proto_metrics[4];
164 MALLOC_DEFINE(M_80211_MESH_PREQ, "80211preq", "802.11 MESH Path Request frame");
165 MALLOC_DEFINE(M_80211_MESH_PREP, "80211prep", "802.11 MESH Path Reply frame");
166 MALLOC_DEFINE(M_80211_MESH_PERR, "80211perr", "802.11 MESH Path Error frame");
168 /* The longer one of the lifetime should be stored as new lifetime */
169 #define MESH_ROUTE_LIFETIME_MAX(a, b) (a > b ? a : b)
171 MALLOC_DEFINE(M_80211_MESH_RT, "80211mesh_rt", "802.11s routing table");
172 MALLOC_DEFINE(M_80211_MESH_GT_RT, "80211mesh_gt", "802.11s known gates table");
175 * Helper functions to manipulate the Mesh routing table.
178 static struct ieee80211_mesh_route *
179 mesh_rt_find_locked(struct ieee80211_mesh_state *ms,
180 const uint8_t dest[IEEE80211_ADDR_LEN])
182 struct ieee80211_mesh_route *rt;
184 MESH_RT_LOCK_ASSERT(ms);
186 TAILQ_FOREACH(rt, &ms->ms_routes, rt_next) {
187 if (IEEE80211_ADDR_EQ(dest, rt->rt_dest))
193 static struct ieee80211_mesh_route *
194 mesh_rt_add_locked(struct ieee80211vap *vap,
195 const uint8_t dest[IEEE80211_ADDR_LEN])
197 struct ieee80211_mesh_state *ms = vap->iv_mesh;
198 struct ieee80211_mesh_route *rt;
200 KASSERT(!IEEE80211_ADDR_EQ(broadcastaddr, dest),
201 ("%s: adding broadcast to the routing table", __func__));
203 MESH_RT_LOCK_ASSERT(ms);
205 rt = kmalloc(ALIGN(sizeof(struct ieee80211_mesh_route)) +
206 ms->ms_ppath->mpp_privlen, M_80211_MESH_RT, M_INTWAIT | M_ZERO);
209 IEEE80211_ADDR_COPY(rt->rt_dest, dest);
210 rt->rt_priv = (void *)ALIGN(&rt[1]);
211 MESH_RT_ENTRY_LOCK_INIT(rt, "MBSS_RT");
212 callout_init_mp(&rt->rt_discovery);
213 rt->rt_updtime = ticks; /* create time */
214 TAILQ_INSERT_TAIL(&ms->ms_routes, rt, rt_next);
219 struct ieee80211_mesh_route *
220 ieee80211_mesh_rt_find(struct ieee80211vap *vap,
221 const uint8_t dest[IEEE80211_ADDR_LEN])
223 struct ieee80211_mesh_state *ms = vap->iv_mesh;
224 struct ieee80211_mesh_route *rt;
227 rt = mesh_rt_find_locked(ms, dest);
232 struct ieee80211_mesh_route *
233 ieee80211_mesh_rt_add(struct ieee80211vap *vap,
234 const uint8_t dest[IEEE80211_ADDR_LEN])
236 struct ieee80211_mesh_state *ms = vap->iv_mesh;
237 struct ieee80211_mesh_route *rt;
239 KASSERT(ieee80211_mesh_rt_find(vap, dest) == NULL,
240 ("%s: duplicate entry in the routing table", __func__));
241 KASSERT(!IEEE80211_ADDR_EQ(vap->iv_myaddr, dest),
242 ("%s: adding self to the routing table", __func__));
245 rt = mesh_rt_add_locked(vap, dest);
251 * Update the route lifetime and returns the updated lifetime.
252 * If new_lifetime is zero and route is timedout it will be invalidated.
253 * new_lifetime is in msec
256 ieee80211_mesh_rt_update(struct ieee80211_mesh_route *rt, int new_lifetime)
259 uint32_t lifetime = 0;
261 KASSERT(rt != NULL, ("route is NULL"));
264 MESH_RT_ENTRY_LOCK(rt);
266 /* dont clobber a proxy entry gated by us */
267 if (rt->rt_flags & IEEE80211_MESHRT_FLAGS_PROXY && rt->rt_nhops == 0) {
268 MESH_RT_ENTRY_UNLOCK(rt);
269 return rt->rt_lifetime;
272 timesince = ticks_to_msecs(now - rt->rt_updtime);
273 rt->rt_updtime = now;
274 if (timesince >= rt->rt_lifetime) {
275 if (new_lifetime != 0) {
276 rt->rt_lifetime = new_lifetime;
279 rt->rt_flags &= ~IEEE80211_MESHRT_FLAGS_VALID;
283 /* update what is left of lifetime */
284 rt->rt_lifetime = rt->rt_lifetime - timesince;
285 rt->rt_lifetime = MESH_ROUTE_LIFETIME_MAX(
286 new_lifetime, rt->rt_lifetime);
288 lifetime = rt->rt_lifetime;
289 MESH_RT_ENTRY_UNLOCK(rt);
295 * Add a proxy route (as needed) for the specified destination.
298 ieee80211_mesh_proxy_check(struct ieee80211vap *vap,
299 const uint8_t dest[IEEE80211_ADDR_LEN])
301 struct ieee80211_mesh_state *ms = vap->iv_mesh;
302 struct ieee80211_mesh_route *rt;
305 rt = mesh_rt_find_locked(ms, dest);
307 rt = mesh_rt_add_locked(vap, dest);
309 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH, dest,
310 "%s", "unable to add proxy entry");
311 vap->iv_stats.is_mesh_rtaddfailed++;
313 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH, dest,
314 "%s", "add proxy entry");
315 IEEE80211_ADDR_COPY(rt->rt_mesh_gate, vap->iv_myaddr);
316 IEEE80211_ADDR_COPY(rt->rt_nexthop, vap->iv_myaddr);
317 rt->rt_flags |= IEEE80211_MESHRT_FLAGS_VALID
318 | IEEE80211_MESHRT_FLAGS_PROXY;
320 } else if ((rt->rt_flags & IEEE80211_MESHRT_FLAGS_VALID) == 0) {
321 KASSERT(rt->rt_flags & IEEE80211_MESHRT_FLAGS_PROXY,
322 ("no proxy flag for poxy entry"));
323 struct ieee80211com *ic = vap->iv_ic;
325 * Fix existing entry created by received frames from
326 * stations that have some memory of dest. We also
327 * flush any frames held on the staging queue; delivering
328 * them is too much trouble right now.
330 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH, dest,
331 "%s", "fix proxy entry");
332 IEEE80211_ADDR_COPY(rt->rt_nexthop, vap->iv_myaddr);
333 rt->rt_flags |= IEEE80211_MESHRT_FLAGS_VALID
334 | IEEE80211_MESHRT_FLAGS_PROXY;
335 /* XXX belongs in hwmp */
336 ieee80211_ageq_drain_node(&ic->ic_stageq,
337 (void *)(uintptr_t) ieee80211_mac_hash(ic, dest));
344 mesh_rt_del(struct ieee80211_mesh_state *ms, struct ieee80211_mesh_route *rt)
346 TAILQ_REMOVE(&ms->ms_routes, rt, rt_next);
348 * Grab the lock before destroying it, to be sure no one else
349 * is holding the route.
351 MESH_RT_ENTRY_LOCK(rt);
352 callout_drain(&rt->rt_discovery);
353 MESH_RT_ENTRY_LOCK_DESTROY(rt);
354 kfree(rt, M_80211_MESH_RT);
358 ieee80211_mesh_rt_del(struct ieee80211vap *vap,
359 const uint8_t dest[IEEE80211_ADDR_LEN])
361 struct ieee80211_mesh_state *ms = vap->iv_mesh;
362 struct ieee80211_mesh_route *rt, *next;
365 TAILQ_FOREACH_SAFE(rt, &ms->ms_routes, rt_next, next) {
366 if (IEEE80211_ADDR_EQ(rt->rt_dest, dest)) {
367 if (rt->rt_flags & IEEE80211_MESHRT_FLAGS_PROXY) {
368 ms->ms_ppath->mpp_senderror(vap, dest, rt,
369 IEEE80211_REASON_MESH_PERR_NO_PROXY);
371 ms->ms_ppath->mpp_senderror(vap, dest, rt,
372 IEEE80211_REASON_MESH_PERR_DEST_UNREACH);
383 ieee80211_mesh_rt_flush(struct ieee80211vap *vap)
385 struct ieee80211_mesh_state *ms = vap->iv_mesh;
386 struct ieee80211_mesh_route *rt, *next;
391 TAILQ_FOREACH_SAFE(rt, &ms->ms_routes, rt_next, next)
397 ieee80211_mesh_rt_flush_peer(struct ieee80211vap *vap,
398 const uint8_t peer[IEEE80211_ADDR_LEN])
400 struct ieee80211_mesh_state *ms = vap->iv_mesh;
401 struct ieee80211_mesh_route *rt, *next;
404 TAILQ_FOREACH_SAFE(rt, &ms->ms_routes, rt_next, next) {
405 if (IEEE80211_ADDR_EQ(rt->rt_nexthop, peer))
412 * Flush expired routing entries, i.e. those in invalid state for
416 mesh_rt_flush_invalid(struct ieee80211vap *vap)
418 struct ieee80211_mesh_state *ms = vap->iv_mesh;
419 struct ieee80211_mesh_route *rt, *next;
424 TAILQ_FOREACH_SAFE(rt, &ms->ms_routes, rt_next, next) {
425 /* Discover paths will be deleted by their own callout */
426 if (rt->rt_flags & IEEE80211_MESHRT_FLAGS_DISCOVER)
428 ieee80211_mesh_rt_update(rt, 0);
429 if ((rt->rt_flags & IEEE80211_MESHRT_FLAGS_VALID) == 0)
435 #define N(a) (sizeof(a) / sizeof(a[0]))
437 ieee80211_mesh_register_proto_path(const struct ieee80211_mesh_proto_path *mpp)
439 int i, firstempty = -1;
441 for (i = 0; i < N(mesh_proto_paths); i++) {
442 if (strncmp(mpp->mpp_descr, mesh_proto_paths[i].mpp_descr,
443 IEEE80211_MESH_PROTO_DSZ) == 0)
445 if (!mesh_proto_paths[i].mpp_active && firstempty == -1)
450 memcpy(&mesh_proto_paths[firstempty], mpp, sizeof(*mpp));
451 mesh_proto_paths[firstempty].mpp_active = 1;
456 ieee80211_mesh_register_proto_metric(const struct
457 ieee80211_mesh_proto_metric *mpm)
459 int i, firstempty = -1;
461 for (i = 0; i < N(mesh_proto_metrics); i++) {
462 if (strncmp(mpm->mpm_descr, mesh_proto_metrics[i].mpm_descr,
463 IEEE80211_MESH_PROTO_DSZ) == 0)
465 if (!mesh_proto_metrics[i].mpm_active && firstempty == -1)
470 memcpy(&mesh_proto_metrics[firstempty], mpm, sizeof(*mpm));
471 mesh_proto_metrics[firstempty].mpm_active = 1;
476 mesh_select_proto_path(struct ieee80211vap *vap, const char *name)
478 struct ieee80211_mesh_state *ms = vap->iv_mesh;
481 for (i = 0; i < N(mesh_proto_paths); i++) {
482 if (strcasecmp(mesh_proto_paths[i].mpp_descr, name) == 0) {
483 ms->ms_ppath = &mesh_proto_paths[i];
491 mesh_select_proto_metric(struct ieee80211vap *vap, const char *name)
493 struct ieee80211_mesh_state *ms = vap->iv_mesh;
496 for (i = 0; i < N(mesh_proto_metrics); i++) {
497 if (strcasecmp(mesh_proto_metrics[i].mpm_descr, name) == 0) {
498 ms->ms_pmetric = &mesh_proto_metrics[i];
507 mesh_gatemode_setup(struct ieee80211vap *vap)
509 struct ieee80211_mesh_state *ms = vap->iv_mesh;
512 * NB: When a mesh gate is running as a ROOT it shall
513 * not send out periodic GANNs but instead mark the
514 * mesh gate flag for the corresponding proactive PREQ
517 if (ms->ms_flags & IEEE80211_MESHFLAGS_ROOT ||
518 (ms->ms_flags & IEEE80211_MESHFLAGS_GATE) == 0) {
519 callout_drain(&ms->ms_gatetimer);
522 callout_reset(&ms->ms_gatetimer, ieee80211_mesh_gateint,
523 mesh_gatemode_cb, vap);
527 mesh_gatemode_cb(void *arg)
529 struct ieee80211vap *vap = (struct ieee80211vap *)arg;
530 struct ieee80211_mesh_state *ms = vap->iv_mesh;
531 struct ieee80211_meshgann_ie gann;
533 gann.gann_flags = 0; /* Reserved */
534 gann.gann_hopcount = 0;
535 gann.gann_ttl = ms->ms_ttl;
536 IEEE80211_ADDR_COPY(gann.gann_addr, vap->iv_myaddr);
537 gann.gann_seq = ms->ms_gateseq++;
538 gann.gann_interval = ieee80211_mesh_gateint;
540 IEEE80211_NOTE(vap, IEEE80211_MSG_MESH, vap->iv_bss,
541 "send broadcast GANN (seq %u)", gann.gann_seq);
543 ieee80211_send_action(vap->iv_bss, IEEE80211_ACTION_CAT_MESH,
544 IEEE80211_ACTION_MESH_GANN, &gann);
545 mesh_gatemode_setup(vap);
549 ieee80211_mesh_init(void)
552 memset(mesh_proto_paths, 0, sizeof(mesh_proto_paths));
553 memset(mesh_proto_metrics, 0, sizeof(mesh_proto_metrics));
556 * Setup mesh parameters that depends on the clock frequency.
558 ieee80211_mesh_gateint = msecs_to_ticks(10000);
559 ieee80211_mesh_retrytimeout = msecs_to_ticks(40);
560 ieee80211_mesh_holdingtimeout = msecs_to_ticks(40);
561 ieee80211_mesh_confirmtimeout = msecs_to_ticks(40);
562 ieee80211_mesh_backofftimeout = msecs_to_ticks(5000);
565 * Register action frame handlers.
567 ieee80211_recv_action_register(IEEE80211_ACTION_CAT_SELF_PROT,
568 IEEE80211_ACTION_MESHPEERING_OPEN,
569 mesh_recv_action_meshpeering_open);
570 ieee80211_recv_action_register(IEEE80211_ACTION_CAT_SELF_PROT,
571 IEEE80211_ACTION_MESHPEERING_CONFIRM,
572 mesh_recv_action_meshpeering_confirm);
573 ieee80211_recv_action_register(IEEE80211_ACTION_CAT_SELF_PROT,
574 IEEE80211_ACTION_MESHPEERING_CLOSE,
575 mesh_recv_action_meshpeering_close);
576 ieee80211_recv_action_register(IEEE80211_ACTION_CAT_MESH,
577 IEEE80211_ACTION_MESH_LMETRIC, mesh_recv_action_meshlmetric);
578 ieee80211_recv_action_register(IEEE80211_ACTION_CAT_MESH,
579 IEEE80211_ACTION_MESH_GANN, mesh_recv_action_meshgate);
581 ieee80211_send_action_register(IEEE80211_ACTION_CAT_SELF_PROT,
582 IEEE80211_ACTION_MESHPEERING_OPEN,
583 mesh_send_action_meshpeering_open);
584 ieee80211_send_action_register(IEEE80211_ACTION_CAT_SELF_PROT,
585 IEEE80211_ACTION_MESHPEERING_CONFIRM,
586 mesh_send_action_meshpeering_confirm);
587 ieee80211_send_action_register(IEEE80211_ACTION_CAT_SELF_PROT,
588 IEEE80211_ACTION_MESHPEERING_CLOSE,
589 mesh_send_action_meshpeering_close);
590 ieee80211_send_action_register(IEEE80211_ACTION_CAT_MESH,
591 IEEE80211_ACTION_MESH_LMETRIC,
592 mesh_send_action_meshlmetric);
593 ieee80211_send_action_register(IEEE80211_ACTION_CAT_MESH,
594 IEEE80211_ACTION_MESH_GANN,
595 mesh_send_action_meshgate);
598 * Register Airtime Link Metric.
600 ieee80211_mesh_register_proto_metric(&mesh_metric_airtime);
603 SYSINIT(wlan_mesh, SI_SUB_DRIVERS, SI_ORDER_FIRST, ieee80211_mesh_init, NULL);
606 ieee80211_mesh_attach(struct ieee80211com *ic)
608 ic->ic_vattach[IEEE80211_M_MBSS] = mesh_vattach;
612 ieee80211_mesh_detach(struct ieee80211com *ic)
617 mesh_vdetach_peers(void *arg, struct ieee80211_node *ni)
619 struct ieee80211com *ic = ni->ni_ic;
622 if (ni->ni_mlstate == IEEE80211_NODE_MESH_ESTABLISHED) {
623 args[0] = ni->ni_mlpid;
624 args[1] = ni->ni_mllid;
625 args[2] = IEEE80211_REASON_PEER_LINK_CANCELED;
626 ieee80211_send_action(ni,
627 IEEE80211_ACTION_CAT_SELF_PROT,
628 IEEE80211_ACTION_MESHPEERING_CLOSE,
631 callout_drain(&ni->ni_mltimer);
632 /* XXX belongs in hwmp */
633 ieee80211_ageq_drain_node(&ic->ic_stageq,
634 (void *)(uintptr_t) ieee80211_mac_hash(ic, ni->ni_macaddr));
638 mesh_vdetach(struct ieee80211vap *vap)
640 struct ieee80211_mesh_state *ms = vap->iv_mesh;
642 callout_drain(&ms->ms_cleantimer);
643 ieee80211_iterate_nodes(&vap->iv_ic->ic_sta, mesh_vdetach_peers,
645 ieee80211_mesh_rt_flush(vap);
646 MESH_RT_LOCK_DESTROY(ms);
647 ms->ms_ppath->mpp_vdetach(vap);
648 kfree(vap->iv_mesh, M_80211_VAP);
653 mesh_vattach(struct ieee80211vap *vap)
655 struct ieee80211_mesh_state *ms;
656 vap->iv_newstate = mesh_newstate;
657 vap->iv_input = mesh_input;
658 vap->iv_opdetach = mesh_vdetach;
659 vap->iv_recv_mgmt = mesh_recv_mgmt;
660 vap->iv_recv_ctl = mesh_recv_ctl;
661 ms = kmalloc(sizeof(struct ieee80211_mesh_state), M_80211_VAP,
664 kprintf("%s: couldn't alloc MBSS state\n", __func__);
669 ms->ms_flags = (IEEE80211_MESHFLAGS_AP | IEEE80211_MESHFLAGS_FWD);
670 ms->ms_ttl = IEEE80211_MESH_DEFAULT_TTL;
671 TAILQ_INIT(&ms->ms_known_gates);
672 TAILQ_INIT(&ms->ms_routes);
673 MESH_RT_LOCK_INIT(ms, "MBSS");
674 callout_init_mp(&ms->ms_cleantimer);
675 callout_init_mp(&ms->ms_gatetimer);
677 mesh_select_proto_metric(vap, "AIRTIME");
678 KASSERT(ms->ms_pmetric, ("ms_pmetric == NULL"));
679 mesh_select_proto_path(vap, "HWMP");
680 KASSERT(ms->ms_ppath, ("ms_ppath == NULL"));
681 ms->ms_ppath->mpp_vattach(vap);
685 * IEEE80211_M_MBSS vap state machine handler.
688 mesh_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
690 struct ieee80211_mesh_state *ms = vap->iv_mesh;
691 struct ieee80211com *ic = vap->iv_ic;
692 struct ieee80211_node *ni;
693 enum ieee80211_state ostate;
695 IEEE80211_LOCK_ASSERT(ic);
697 ostate = vap->iv_state;
698 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE, "%s: %s -> %s (%d)\n",
699 __func__, ieee80211_state_name[ostate],
700 ieee80211_state_name[nstate], arg);
701 vap->iv_state = nstate; /* state transition */
702 if (ostate != IEEE80211_S_SCAN)
703 ieee80211_cancel_scan(vap); /* background scan */
704 ni = vap->iv_bss; /* NB: no reference held */
705 if (nstate != IEEE80211_S_RUN && ostate == IEEE80211_S_RUN) {
706 callout_drain(&ms->ms_cleantimer);
707 callout_drain(&ms->ms_gatetimer);
710 case IEEE80211_S_INIT:
712 case IEEE80211_S_SCAN:
713 ieee80211_cancel_scan(vap);
715 case IEEE80211_S_CAC:
716 ieee80211_dfs_cac_stop(vap);
718 case IEEE80211_S_RUN:
719 ieee80211_iterate_nodes(&ic->ic_sta,
720 mesh_vdetach_peers, NULL);
725 if (ostate != IEEE80211_S_INIT) {
726 /* NB: optimize INIT -> INIT case */
727 ieee80211_reset_bss(vap);
728 ieee80211_mesh_rt_flush(vap);
731 case IEEE80211_S_SCAN:
733 case IEEE80211_S_INIT:
734 if (vap->iv_des_chan != IEEE80211_CHAN_ANYC &&
735 !IEEE80211_IS_CHAN_RADAR(vap->iv_des_chan) &&
738 * Already have a channel and a mesh ID; bypass
739 * the scan and startup immediately.
741 ieee80211_create_ibss(vap, vap->iv_des_chan);
745 * Initiate a scan. We can come here as a result
746 * of an IEEE80211_IOC_SCAN_REQ too in which case
747 * the vap will be marked with IEEE80211_FEXT_SCANREQ
748 * and the scan request parameters will be present
749 * in iv_scanreq. Otherwise we do the default.
751 if (vap->iv_flags_ext & IEEE80211_FEXT_SCANREQ) {
752 ieee80211_check_scan(vap,
753 vap->iv_scanreq_flags,
754 vap->iv_scanreq_duration,
755 vap->iv_scanreq_mindwell,
756 vap->iv_scanreq_maxdwell,
757 vap->iv_scanreq_nssid, vap->iv_scanreq_ssid);
758 vap->iv_flags_ext &= ~IEEE80211_FEXT_SCANREQ;
760 ieee80211_check_scan_current(vap);
766 case IEEE80211_S_CAC:
768 * Start CAC on a DFS channel. We come here when starting
769 * a bss on a DFS channel (see ieee80211_create_ibss).
771 ieee80211_dfs_cac_start(vap);
773 case IEEE80211_S_RUN:
775 case IEEE80211_S_INIT:
777 * Already have a channel; bypass the
778 * scan and startup immediately.
779 * Note that ieee80211_create_ibss will call
780 * back to do a RUN->RUN state change.
782 ieee80211_create_ibss(vap,
783 ieee80211_ht_adjust_channel(ic,
784 ic->ic_curchan, vap->iv_flags_ht));
785 /* NB: iv_bss is changed on return */
787 case IEEE80211_S_CAC:
789 * NB: This is the normal state change when CAC
790 * expires and no radar was detected; no need to
791 * clear the CAC timer as it's already expired.
794 case IEEE80211_S_CSA:
797 * Shorten inactivity timer of associated stations
798 * to weed out sta's that don't follow a CSA.
800 ieee80211_iterate_nodes(&ic->ic_sta, sta_csa, vap);
803 * Update bss node channel to reflect where
804 * we landed after CSA.
806 ieee80211_node_set_chan(vap->iv_bss,
807 ieee80211_ht_adjust_channel(ic, ic->ic_curchan,
808 ieee80211_htchanflags(vap->iv_bss->ni_chan)));
809 /* XXX bypass debug msgs */
811 case IEEE80211_S_SCAN:
812 case IEEE80211_S_RUN:
813 #ifdef IEEE80211_DEBUG
814 if (ieee80211_msg_debug(vap)) {
815 struct ieee80211_node *ni = vap->iv_bss;
817 "synchronized with %s meshid ",
818 ether_sprintf(ni->ni_meshid));
819 ieee80211_print_essid(ni->ni_meshid,
822 kprintf(" channel %d\n",
823 ieee80211_chan2ieee(ic, ic->ic_curchan));
830 ieee80211_node_authorize(vap->iv_bss);
831 callout_reset(&ms->ms_cleantimer, ms->ms_ppath->mpp_inact,
832 mesh_rt_cleanup_cb, vap);
833 mesh_gatemode_setup(vap);
838 /* NB: ostate not nstate */
839 ms->ms_ppath->mpp_newstate(vap, ostate, arg);
844 mesh_rt_cleanup_cb(void *arg)
846 struct ieee80211vap *vap = arg;
847 struct ieee80211_mesh_state *ms = vap->iv_mesh;
849 mesh_rt_flush_invalid(vap);
850 callout_reset(&ms->ms_cleantimer, ms->ms_ppath->mpp_inact,
851 mesh_rt_cleanup_cb, vap);
855 * Mark a mesh STA as gate and return a pointer to it.
856 * If this is first time, we create a new gate route.
857 * Always update the path route to this mesh gate.
859 struct ieee80211_mesh_gate_route *
860 ieee80211_mesh_mark_gate(struct ieee80211vap *vap, const uint8_t *addr,
861 struct ieee80211_mesh_route *rt)
863 struct ieee80211_mesh_state *ms = vap->iv_mesh;
864 struct ieee80211_mesh_gate_route *gr = NULL, *next;
868 TAILQ_FOREACH_SAFE(gr, &ms->ms_known_gates, gr_next, next) {
869 if (IEEE80211_ADDR_EQ(gr->gr_addr, addr)) {
876 /* New mesh gate add it to known table. */
877 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH, addr,
878 "%s", "stored new gate information from pro-PREQ.");
879 gr = kmalloc(ALIGN(sizeof(struct ieee80211_mesh_gate_route)),
880 M_80211_MESH_GT_RT, M_INTWAIT | M_ZERO);
881 IEEE80211_ADDR_COPY(gr->gr_addr, addr);
882 TAILQ_INSERT_TAIL(&ms->ms_known_gates, gr, gr_next);
885 /* TODO: link from path route to gate route */
893 * Helper function to note the Mesh Peer Link FSM change.
896 mesh_linkchange(struct ieee80211_node *ni, enum ieee80211_mesh_mlstate state)
898 struct ieee80211vap *vap = ni->ni_vap;
899 struct ieee80211_mesh_state *ms = vap->iv_mesh;
900 #ifdef IEEE80211_DEBUG
901 static const char *meshlinkstates[] = {
902 [IEEE80211_NODE_MESH_IDLE] = "IDLE",
903 [IEEE80211_NODE_MESH_OPENSNT] = "OPEN SENT",
904 [IEEE80211_NODE_MESH_OPENRCV] = "OPEN RECEIVED",
905 [IEEE80211_NODE_MESH_CONFIRMRCV] = "CONFIRM RECEIVED",
906 [IEEE80211_NODE_MESH_ESTABLISHED] = "ESTABLISHED",
907 [IEEE80211_NODE_MESH_HOLDING] = "HOLDING"
910 IEEE80211_NOTE(vap, IEEE80211_MSG_MESH,
911 ni, "peer link: %s -> %s",
912 meshlinkstates[ni->ni_mlstate], meshlinkstates[state]);
914 /* track neighbor count */
915 if (state == IEEE80211_NODE_MESH_ESTABLISHED &&
916 ni->ni_mlstate != IEEE80211_NODE_MESH_ESTABLISHED) {
917 KASSERT(ms->ms_neighbors < 65535, ("neighbor count overflow"));
919 ieee80211_beacon_notify(vap, IEEE80211_BEACON_MESHCONF);
920 } else if (ni->ni_mlstate == IEEE80211_NODE_MESH_ESTABLISHED &&
921 state != IEEE80211_NODE_MESH_ESTABLISHED) {
922 KASSERT(ms->ms_neighbors > 0, ("neighbor count 0"));
924 ieee80211_beacon_notify(vap, IEEE80211_BEACON_MESHCONF);
926 ni->ni_mlstate = state;
928 case IEEE80211_NODE_MESH_HOLDING:
929 ms->ms_ppath->mpp_peerdown(ni);
931 case IEEE80211_NODE_MESH_ESTABLISHED:
932 ieee80211_mesh_discover(vap, ni->ni_macaddr, NULL);
940 * Helper function to generate a unique local ID required for mesh
941 * peer establishment.
944 mesh_checkid(void *arg, struct ieee80211_node *ni)
948 if (*r == ni->ni_mllid)
949 *(uint16_t *)arg = 0;
953 mesh_generateid(struct ieee80211vap *vap)
959 get_random_bytes(&r, 2);
960 ieee80211_iterate_nodes(&vap->iv_ic->ic_sta, mesh_checkid, &r);
962 } while (r == 0 && maxiter > 0);
967 * Verifies if we already received this packet by checking its
969 * Returns 0 if the frame is to be accepted, 1 otherwise.
972 mesh_checkpseq(struct ieee80211vap *vap,
973 const uint8_t source[IEEE80211_ADDR_LEN], uint32_t seq)
975 struct ieee80211_mesh_route *rt;
977 rt = ieee80211_mesh_rt_find(vap, source);
979 rt = ieee80211_mesh_rt_add(vap, source);
981 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH, source,
982 "%s", "add mcast route failed");
983 vap->iv_stats.is_mesh_rtaddfailed++;
986 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH, source,
987 "add mcast route, mesh seqno %d", seq);
988 rt->rt_lastmseq = seq;
991 if (IEEE80211_MESH_SEQ_GEQ(rt->rt_lastmseq, seq)) {
994 rt->rt_lastmseq = seq;
1000 * Iterate the routing table and locate the next hop.
1002 struct ieee80211_node *
1003 ieee80211_mesh_find_txnode(struct ieee80211vap *vap,
1004 const uint8_t dest[IEEE80211_ADDR_LEN])
1006 struct ieee80211_mesh_route *rt;
1008 rt = ieee80211_mesh_rt_find(vap, dest);
1011 if ((rt->rt_flags & IEEE80211_MESHRT_FLAGS_VALID) == 0) {
1012 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH, dest,
1013 "%s: !valid, flags 0x%x", __func__, rt->rt_flags);
1017 if (rt->rt_flags & IEEE80211_MESHRT_FLAGS_PROXY) {
1018 rt = ieee80211_mesh_rt_find(vap, rt->rt_mesh_gate);
1019 if (rt == NULL) return NULL;
1020 if ((rt->rt_flags & IEEE80211_MESHRT_FLAGS_VALID) == 0) {
1021 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH, dest,
1022 "%s: meshgate !valid, flags 0x%x", __func__,
1028 return ieee80211_find_txnode(vap, rt->rt_nexthop);
1032 mesh_transmit_to_gate(struct ieee80211vap *vap, struct mbuf *m,
1033 struct ieee80211_mesh_route *rt_gate)
1035 struct ifnet *ifp = vap->iv_ifp;
1036 struct ieee80211_node *ni;
1038 IEEE80211_TX_UNLOCK_ASSERT(vap->iv_ic);
1040 ni = ieee80211_mesh_find_txnode(vap, rt_gate->rt_dest);
1042 IFNET_STAT_INC(ifp, oerrors, 1);
1048 * Send through the VAP packet transmit path.
1049 * This consumes the node ref grabbed above and
1050 * the mbuf, regardless of whether there's a problem
1053 (void) ieee80211_vap_pkt_send_dest(vap, m, ni);
1057 * Forward the queued frames to known valid mesh gates.
1058 * Assume destination to be outside the MBSS (i.e. proxy entry),
1059 * If no valid mesh gates are known silently discard queued frames.
1060 * After transmitting frames to all known valid mesh gates, this route
1061 * will be marked invalid, and a new path discovery will happen in the hopes
1062 * that (at least) one of the mesh gates have a new proxy entry for us to use.
1065 ieee80211_mesh_forward_to_gates(struct ieee80211vap *vap,
1066 struct ieee80211_mesh_route *rt_dest)
1068 struct ieee80211com *ic = vap->iv_ic;
1069 struct ieee80211_mesh_state *ms = vap->iv_mesh;
1070 struct ieee80211_mesh_route *rt_gate;
1071 struct ieee80211_mesh_gate_route *gr = NULL, *gr_next;
1072 struct mbuf *m, *mcopy, *next;
1074 IEEE80211_TX_UNLOCK_ASSERT(ic);
1076 KASSERT( rt_dest->rt_flags == IEEE80211_MESHRT_FLAGS_DISCOVER,
1077 ("Route is not marked with IEEE80211_MESHRT_FLAGS_DISCOVER"));
1079 /* XXX: send to more than one valid mash gate */
1082 m = ieee80211_ageq_remove(&ic->ic_stageq,
1083 (struct ieee80211_node *)(uintptr_t)
1084 ieee80211_mac_hash(ic, rt_dest->rt_dest));
1086 TAILQ_FOREACH_SAFE(gr, &ms->ms_known_gates, gr_next, gr_next) {
1087 rt_gate = gr->gr_route;
1088 if (rt_gate == NULL) {
1089 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_HWMP,
1091 "mesh gate with no path %s",
1092 ether_sprintf(gr->gr_addr));
1095 if ((rt_gate->rt_flags & IEEE80211_MESHRT_FLAGS_VALID) == 0)
1097 KASSERT(rt_gate->rt_flags & IEEE80211_MESHRT_FLAGS_GATE,
1098 ("route not marked as a mesh gate"));
1099 KASSERT((rt_gate->rt_flags &
1100 IEEE80211_MESHRT_FLAGS_PROXY) == 0,
1101 ("found mesh gate that is also marked porxy"));
1103 * convert route to a proxy route gated by the current
1104 * mesh gate, this is needed so encap can built data
1105 * frame with correct address.
1107 rt_dest->rt_flags = IEEE80211_MESHRT_FLAGS_PROXY |
1108 IEEE80211_MESHRT_FLAGS_VALID;
1109 rt_dest->rt_ext_seq = 1; /* random value */
1110 IEEE80211_ADDR_COPY(rt_dest->rt_mesh_gate, rt_gate->rt_dest);
1111 IEEE80211_ADDR_COPY(rt_dest->rt_nexthop, rt_gate->rt_nexthop);
1112 rt_dest->rt_metric = rt_gate->rt_metric;
1113 rt_dest->rt_nhops = rt_gate->rt_nhops;
1114 ieee80211_mesh_rt_update(rt_dest, ms->ms_ppath->mpp_inact);
1117 mcopy = m_dup(m, M_NOWAIT);
1118 for (; mcopy != NULL; mcopy = next) {
1119 next = mcopy->m_nextpkt;
1120 mcopy->m_nextpkt = NULL;
1121 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_HWMP,
1123 "flush queued frame %p len %d", mcopy,
1124 mcopy->m_pkthdr.len);
1125 mesh_transmit_to_gate(vap, mcopy, rt_gate);
1129 rt_dest->rt_flags = 0; /* Mark invalid */
1135 * Forward the specified frame.
1136 * Decrement the TTL and set TA to our MAC address.
1139 mesh_forward(struct ieee80211vap *vap, struct mbuf *m,
1140 const struct ieee80211_meshcntl *mc)
1142 struct ieee80211com *ic = vap->iv_ic;
1143 struct ieee80211_mesh_state *ms = vap->iv_mesh;
1144 struct ifnet *ifp = vap->iv_ifp;
1145 const struct ieee80211_frame *wh =
1146 mtod(m, const struct ieee80211_frame *);
1148 struct ieee80211_meshcntl *mccopy;
1149 struct ieee80211_frame *whcopy;
1150 struct ieee80211_node *ni;
1153 /* This is called from the RX path - don't hold this lock */
1154 IEEE80211_TX_UNLOCK_ASSERT(ic);
1157 * mesh ttl of 1 means we are the last one receving it,
1158 * according to amendment we decrement and then check if
1159 * 0, if so we dont forward.
1161 if (mc->mc_ttl < 1) {
1162 IEEE80211_NOTE_FRAME(vap, IEEE80211_MSG_MESH, wh,
1163 "%s", "frame not fwd'd, ttl 1");
1164 vap->iv_stats.is_mesh_fwd_ttl++;
1167 if (!(ms->ms_flags & IEEE80211_MESHFLAGS_FWD)) {
1168 IEEE80211_NOTE_FRAME(vap, IEEE80211_MSG_MESH, wh,
1169 "%s", "frame not fwd'd, fwding disabled");
1170 vap->iv_stats.is_mesh_fwd_disabled++;
1173 mcopy = m_dup(m, M_NOWAIT);
1174 if (mcopy == NULL) {
1175 IEEE80211_NOTE_FRAME(vap, IEEE80211_MSG_MESH, wh,
1176 "%s", "frame not fwd'd, cannot dup");
1177 vap->iv_stats.is_mesh_fwd_nobuf++;
1178 IFNET_STAT_INC(ifp, oerrors, 1);
1181 mcopy = m_pullup(mcopy, ieee80211_hdrspace(ic, wh) +
1182 sizeof(struct ieee80211_meshcntl));
1183 if (mcopy == NULL) {
1184 IEEE80211_NOTE_FRAME(vap, IEEE80211_MSG_MESH, wh,
1185 "%s", "frame not fwd'd, too short");
1186 vap->iv_stats.is_mesh_fwd_tooshort++;
1187 IFNET_STAT_INC(ifp, oerrors, 1);
1191 whcopy = mtod(mcopy, struct ieee80211_frame *);
1192 mccopy = (struct ieee80211_meshcntl *)
1193 (mtod(mcopy, uint8_t *) + ieee80211_hdrspace(ic, wh));
1194 /* XXX clear other bits? */
1195 whcopy->i_fc[1] &= ~IEEE80211_FC1_RETRY;
1196 IEEE80211_ADDR_COPY(whcopy->i_addr2, vap->iv_myaddr);
1197 if (IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1198 ni = ieee80211_ref_node(vap->iv_bss);
1199 mcopy->m_flags |= M_MCAST;
1201 ni = ieee80211_mesh_find_txnode(vap, whcopy->i_addr3);
1204 * [Optional] any of the following three actions:
1205 * o silently discard
1206 * o trigger a path discovery
1207 * o inform TA that meshDA is unknown.
1209 IEEE80211_NOTE_FRAME(vap, IEEE80211_MSG_MESH, wh,
1210 "%s", "frame not fwd'd, no path");
1211 ms->ms_ppath->mpp_senderror(vap, whcopy->i_addr3, NULL,
1212 IEEE80211_REASON_MESH_PERR_NO_FI);
1213 vap->iv_stats.is_mesh_fwd_nopath++;
1217 IEEE80211_ADDR_COPY(whcopy->i_addr1, ni->ni_macaddr);
1219 KASSERT(mccopy->mc_ttl > 0, ("%s called with wrong ttl", __func__));
1222 /* XXX calculate priority so drivers can find the tx queue */
1223 M_WME_SETAC(mcopy, WME_AC_BE);
1225 /* XXX do we know m_nextpkt is NULL? */
1226 mcopy->m_pkthdr.rcvif = (void *) ni;
1229 * XXX this bypasses all of the VAP TX handling; it passes frames
1230 * directly to the parent interface.
1232 * Because of this, there's no TX lock being held as there's no
1233 * encaps state being used.
1235 * Doing a direct parent transmit may not be the correct thing
1236 * to do here; we'll have to re-think this soon.
1238 IEEE80211_TX_LOCK(ic);
1239 err = ieee80211_parent_xmitpkt(ic, mcopy);
1240 IEEE80211_TX_UNLOCK(ic);
1242 /* NB: IFQ_HANDOFF reclaims mbuf */
1243 ieee80211_free_node(ni);
1245 IFNET_STAT_INC(ifp, opackets, 1);
1249 static struct mbuf *
1250 mesh_decap(struct ieee80211vap *vap, struct mbuf *m, int hdrlen, int meshdrlen)
1252 #define WHDIR(wh) ((wh)->i_fc[1] & IEEE80211_FC1_DIR_MASK)
1253 #define MC01(mc) ((const struct ieee80211_meshcntl_ae01 *)mc)
1254 uint8_t b[sizeof(struct ieee80211_qosframe_addr4) +
1255 sizeof(struct ieee80211_meshcntl_ae10)];
1256 const struct ieee80211_qosframe_addr4 *wh;
1257 const struct ieee80211_meshcntl_ae10 *mc;
1258 struct ether_header *eh;
1262 if (m->m_len < hdrlen + sizeof(*llc) &&
1263 (m = m_pullup(m, hdrlen + sizeof(*llc))) == NULL) {
1264 IEEE80211_DPRINTF(vap, IEEE80211_MSG_ANY,
1265 "discard data frame: %s", "m_pullup failed");
1266 vap->iv_stats.is_rx_tooshort++;
1269 memcpy(b, mtod(m, caddr_t), hdrlen);
1270 wh = (const struct ieee80211_qosframe_addr4 *)&b[0];
1271 mc = (const struct ieee80211_meshcntl_ae10 *)&b[hdrlen - meshdrlen];
1272 KASSERT(WHDIR(wh) == IEEE80211_FC1_DIR_FROMDS ||
1273 WHDIR(wh) == IEEE80211_FC1_DIR_DSTODS,
1274 ("bogus dir, fc 0x%x:0x%x", wh->i_fc[0], wh->i_fc[1]));
1276 llc = (struct llc *)(mtod(m, caddr_t) + hdrlen);
1277 if (llc->llc_dsap == LLC_SNAP_LSAP && llc->llc_ssap == LLC_SNAP_LSAP &&
1278 llc->llc_control == LLC_UI && llc->llc_snap.org_code[0] == 0 &&
1279 llc->llc_snap.org_code[1] == 0 && llc->llc_snap.org_code[2] == 0 &&
1280 /* NB: preserve AppleTalk frames that have a native SNAP hdr */
1281 !(llc->llc_snap.ether_type == htons(ETHERTYPE_AARP) ||
1282 llc->llc_snap.ether_type == htons(ETHERTYPE_IPX))) {
1283 m_adj(m, hdrlen + sizeof(struct llc) - sizeof(*eh));
1286 m_adj(m, hdrlen - sizeof(*eh));
1288 eh = mtod(m, struct ether_header *);
1289 ae = mc->mc_flags & IEEE80211_MESH_AE_MASK;
1290 if (WHDIR(wh) == IEEE80211_FC1_DIR_FROMDS) {
1291 IEEE80211_ADDR_COPY(eh->ether_dhost, wh->i_addr1);
1292 if (ae == IEEE80211_MESH_AE_00) {
1293 IEEE80211_ADDR_COPY(eh->ether_shost, wh->i_addr3);
1294 } else if (ae == IEEE80211_MESH_AE_01) {
1295 IEEE80211_ADDR_COPY(eh->ether_shost,
1296 MC01(mc)->mc_addr4);
1298 IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
1299 (const struct ieee80211_frame *)wh, NULL,
1301 vap->iv_stats.is_mesh_badae++;
1306 if (ae == IEEE80211_MESH_AE_00) {
1307 IEEE80211_ADDR_COPY(eh->ether_dhost, wh->i_addr3);
1308 IEEE80211_ADDR_COPY(eh->ether_shost, wh->i_addr4);
1309 } else if (ae == IEEE80211_MESH_AE_10) {
1310 IEEE80211_ADDR_COPY(eh->ether_dhost, mc->mc_addr5);
1311 IEEE80211_ADDR_COPY(eh->ether_shost, mc->mc_addr6);
1313 IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
1314 (const struct ieee80211_frame *)wh, NULL,
1316 vap->iv_stats.is_mesh_badae++;
1321 #ifndef __NO_STRICT_ALIGNMENT
1322 if (!ALIGNED_POINTER(mtod(m, caddr_t) + sizeof(*eh), uint32_t)) {
1323 m = ieee80211_realign(vap, m, sizeof(*eh));
1327 #endif /* !__NO_STRICT_ALIGNMENT */
1329 eh = mtod(m, struct ether_header *);
1330 eh->ether_type = htons(m->m_pkthdr.len - sizeof(*eh));
1338 * Return non-zero if the unicast mesh data frame should be processed
1339 * locally. Frames that are not proxy'd have our address, otherwise
1340 * we need to consult the routing table to look for a proxy entry.
1343 mesh_isucastforme(struct ieee80211vap *vap, const struct ieee80211_frame *wh,
1344 const struct ieee80211_meshcntl *mc)
1346 int ae = mc->mc_flags & 3;
1348 KASSERT((wh->i_fc[1] & IEEE80211_FC1_DIR_MASK) == IEEE80211_FC1_DIR_DSTODS,
1349 ("bad dir 0x%x:0x%x", wh->i_fc[0], wh->i_fc[1]));
1350 KASSERT(ae == IEEE80211_MESH_AE_00 || ae == IEEE80211_MESH_AE_10,
1352 if (ae == IEEE80211_MESH_AE_10) { /* ucast w/ proxy */
1353 const struct ieee80211_meshcntl_ae10 *mc10 =
1354 (const struct ieee80211_meshcntl_ae10 *) mc;
1355 struct ieee80211_mesh_route *rt =
1356 ieee80211_mesh_rt_find(vap, mc10->mc_addr5);
1357 /* check for proxy route to ourself */
1358 return (rt != NULL &&
1359 (rt->rt_flags & IEEE80211_MESHRT_FLAGS_PROXY));
1360 } else /* ucast w/o proxy */
1361 return IEEE80211_ADDR_EQ(wh->i_addr3, vap->iv_myaddr);
1365 * Verifies transmitter, updates lifetime, precursor list and forwards data.
1366 * > 0 means we have forwarded data and no need to process locally
1367 * == 0 means we want to process locally (and we may have forwarded data
1368 * < 0 means there was an error and data should be discarded
1371 mesh_recv_indiv_data_to_fwrd(struct ieee80211vap *vap, struct mbuf *m,
1372 struct ieee80211_frame *wh, const struct ieee80211_meshcntl *mc)
1374 struct ieee80211_qosframe_addr4 *qwh;
1375 struct ieee80211_mesh_state *ms = vap->iv_mesh;
1376 struct ieee80211_mesh_route *rt_meshda, *rt_meshsa;
1378 /* This is called from the RX path - don't hold this lock */
1379 IEEE80211_TX_UNLOCK_ASSERT(vap->iv_ic);
1381 qwh = (struct ieee80211_qosframe_addr4 *)wh;
1385 * o verify addr2 is a legitimate transmitter
1386 * o lifetime of precursor of addr3 (addr2) is max(init, curr)
1387 * o lifetime of precursor of addr4 (nexthop) is max(init, curr)
1390 /* set lifetime of addr3 (meshDA) to initial value */
1391 rt_meshda = ieee80211_mesh_rt_find(vap, qwh->i_addr3);
1392 if (rt_meshda == NULL) {
1393 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH, qwh->i_addr2,
1394 "no route to meshDA(%s)", ether_sprintf(qwh->i_addr3));
1396 * [Optional] any of the following three actions:
1397 * o silently discard [X]
1398 * o trigger a path discovery [ ]
1399 * o inform TA that meshDA is unknown. [ ]
1405 ieee80211_mesh_rt_update(rt_meshda, ticks_to_msecs(
1406 ms->ms_ppath->mpp_inact));
1408 /* set lifetime of addr4 (meshSA) to initial value */
1409 rt_meshsa = ieee80211_mesh_rt_find(vap, qwh->i_addr4);
1410 KASSERT(rt_meshsa != NULL, ("no route"));
1411 ieee80211_mesh_rt_update(rt_meshsa, ticks_to_msecs(
1412 ms->ms_ppath->mpp_inact));
1414 mesh_forward(vap, m, mc);
1415 return (1); /* dont process locally */
1419 * Verifies transmitter, updates lifetime, precursor list and process data
1420 * locally, if data is proxy with AE = 10 it could mean data should go
1421 * on another mesh path or data should be forwarded to the DS.
1423 * > 0 means we have forwarded data and no need to process locally
1424 * == 0 means we want to process locally (and we may have forwarded data
1425 * < 0 means there was an error and data should be discarded
1428 mesh_recv_indiv_data_to_me(struct ieee80211vap *vap, struct mbuf *m,
1429 struct ieee80211_frame *wh, const struct ieee80211_meshcntl *mc)
1431 struct ieee80211_qosframe_addr4 *qwh;
1432 const struct ieee80211_meshcntl_ae10 *mc10;
1433 struct ieee80211_mesh_state *ms = vap->iv_mesh;
1434 struct ieee80211_mesh_route *rt;
1437 /* This is called from the RX path - don't hold this lock */
1438 IEEE80211_TX_UNLOCK_ASSERT(vap->iv_ic);
1440 qwh = (struct ieee80211_qosframe_addr4 *)wh;
1441 mc10 = (const struct ieee80211_meshcntl_ae10 *)mc;
1445 * o verify addr2 is a legitimate transmitter
1446 * o lifetime of precursor entry is max(init, curr)
1449 /* set lifetime of addr4 (meshSA) to initial value */
1450 rt = ieee80211_mesh_rt_find(vap, qwh->i_addr4);
1451 KASSERT(rt != NULL, ("no route"));
1452 ieee80211_mesh_rt_update(rt, ticks_to_msecs(ms->ms_ppath->mpp_inact));
1455 ae = mc10->mc_flags & IEEE80211_MESH_AE_MASK;
1456 KASSERT(ae == IEEE80211_MESH_AE_00 ||
1457 ae == IEEE80211_MESH_AE_10, ("bad AE %d", ae));
1458 if (ae == IEEE80211_MESH_AE_10) {
1459 if (IEEE80211_ADDR_EQ(mc10->mc_addr5, qwh->i_addr3)) {
1460 return (0); /* process locally */
1463 rt = ieee80211_mesh_rt_find(vap, mc10->mc_addr5);
1465 (rt->rt_flags & IEEE80211_MESHRT_FLAGS_VALID) &&
1466 (rt->rt_flags & IEEE80211_MESHRT_FLAGS_PROXY) == 0) {
1468 * Forward on another mesh-path, according to
1469 * amendment as specified in 9.32.4.1
1471 IEEE80211_ADDR_COPY(qwh->i_addr3, mc10->mc_addr5);
1472 mesh_forward(vap, m,
1473 (const struct ieee80211_meshcntl *)mc10);
1474 return (1); /* dont process locally */
1477 * All other cases: forward of MSDUs from the MBSS to DS indiv.
1478 * addressed according to 13.11.3.2.
1480 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_OUTPUT, qwh->i_addr2,
1481 "forward frame to DS, SA(%s) DA(%s)",
1482 ether_sprintf(mc10->mc_addr6),
1483 ether_sprintf(mc10->mc_addr5));
1485 return (0); /* process locally */
1489 * Try to forward the group addressed data on to other mesh STAs, and
1492 * > 0 means we have forwarded data and no need to process locally
1493 * == 0 means we want to process locally (and we may have forwarded data
1494 * < 0 means there was an error and data should be discarded
1497 mesh_recv_group_data(struct ieee80211vap *vap, struct mbuf *m,
1498 struct ieee80211_frame *wh, const struct ieee80211_meshcntl *mc)
1500 #define MC01(mc) ((const struct ieee80211_meshcntl_ae01 *)mc)
1501 struct ieee80211_mesh_state *ms = vap->iv_mesh;
1503 /* This is called from the RX path - don't hold this lock */
1504 IEEE80211_TX_UNLOCK_ASSERT(vap->iv_ic);
1506 mesh_forward(vap, m, mc);
1508 if(mc->mc_ttl > 0) {
1509 if (mc->mc_flags & IEEE80211_MESH_AE_01) {
1511 * Forward of MSDUs from the MBSS to DS group addressed
1512 * (according to 13.11.3.2)
1513 * This happens by delivering the packet, and a bridge
1514 * will sent it on another port member.
1516 if (ms->ms_flags & IEEE80211_MESHFLAGS_GATE &&
1517 ms->ms_flags & IEEE80211_MESHFLAGS_FWD)
1518 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH,
1519 MC01(mc)->mc_addr4, "%s",
1520 "forward from MBSS to the DS");
1523 return (0); /* process locally */
1528 mesh_input(struct ieee80211_node *ni, struct mbuf *m, int rssi, int nf)
1530 #define HAS_SEQ(type) ((type & 0x4) == 0)
1531 #define MC01(mc) ((const struct ieee80211_meshcntl_ae01 *)mc)
1532 #define MC10(mc) ((const struct ieee80211_meshcntl_ae10 *)mc)
1533 struct ieee80211vap *vap = ni->ni_vap;
1534 struct ieee80211com *ic = ni->ni_ic;
1535 struct ifnet *ifp = vap->iv_ifp;
1536 struct ieee80211_frame *wh;
1537 const struct ieee80211_meshcntl *mc;
1538 int hdrspace, meshdrlen, need_tap, error;
1539 uint8_t dir, type, subtype, ae;
1541 const uint8_t *addr;
1543 ieee80211_seq rxseq;
1545 KASSERT(ni != NULL, ("null node"));
1546 ni->ni_inact = ni->ni_inact_reload;
1548 need_tap = 1; /* mbuf need to be tapped. */
1549 type = -1; /* undefined */
1551 /* This is called from the RX path - don't hold this lock */
1552 IEEE80211_TX_UNLOCK_ASSERT(ic);
1554 if (m->m_pkthdr.len < sizeof(struct ieee80211_frame_min)) {
1555 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
1556 ni->ni_macaddr, NULL,
1557 "too short (1): len %u", m->m_pkthdr.len);
1558 vap->iv_stats.is_rx_tooshort++;
1562 * Bit of a cheat here, we use a pointer for a 3-address
1563 * frame format but don't reference fields past outside
1564 * ieee80211_frame_min w/o first validating the data is
1567 wh = mtod(m, struct ieee80211_frame *);
1569 if ((wh->i_fc[0] & IEEE80211_FC0_VERSION_MASK) !=
1570 IEEE80211_FC0_VERSION_0) {
1571 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
1572 ni->ni_macaddr, NULL, "wrong version %x", wh->i_fc[0]);
1573 vap->iv_stats.is_rx_badversion++;
1576 dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK;
1577 type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
1578 subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
1579 if ((ic->ic_flags & IEEE80211_F_SCAN) == 0) {
1580 IEEE80211_RSSI_LPF(ni->ni_avgrssi, rssi);
1582 if (HAS_SEQ(type)) {
1583 uint8_t tid = ieee80211_gettid(wh);
1585 if (IEEE80211_QOS_HAS_SEQ(wh) &&
1586 TID_TO_WME_AC(tid) >= WME_AC_VI)
1587 ic->ic_wme.wme_hipri_traffic++;
1588 rxseq = le16toh(*(uint16_t *)wh->i_seq);
1589 if (! ieee80211_check_rxseq(ni, wh)) {
1590 /* duplicate, discard */
1591 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
1592 wh->i_addr1, "duplicate",
1593 "seqno <%u,%u> fragno <%u,%u> tid %u",
1594 rxseq >> IEEE80211_SEQ_SEQ_SHIFT,
1595 ni->ni_rxseqs[tid] >>
1596 IEEE80211_SEQ_SEQ_SHIFT,
1597 rxseq & IEEE80211_SEQ_FRAG_MASK,
1598 ni->ni_rxseqs[tid] &
1599 IEEE80211_SEQ_FRAG_MASK,
1601 vap->iv_stats.is_rx_dup++;
1602 IEEE80211_NODE_STAT(ni, rx_dup);
1605 ni->ni_rxseqs[tid] = rxseq;
1608 #ifdef IEEE80211_DEBUG
1610 * It's easier, but too expensive, to simulate different mesh
1611 * topologies by consulting the ACL policy very early, so do this
1614 * NB: this check is also done upon peering link initiation.
1616 if (vap->iv_acl != NULL && !vap->iv_acl->iac_check(vap, wh)) {
1617 IEEE80211_DISCARD(vap, IEEE80211_MSG_ACL,
1618 wh, NULL, "%s", "disallowed by ACL");
1619 vap->iv_stats.is_rx_acl++;
1624 case IEEE80211_FC0_TYPE_DATA:
1625 if (ni == vap->iv_bss)
1627 if (ni->ni_mlstate != IEEE80211_NODE_MESH_ESTABLISHED) {
1628 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_MESH,
1629 ni->ni_macaddr, NULL,
1630 "peer link not yet established (%d)",
1632 vap->iv_stats.is_mesh_nolink++;
1635 if (dir != IEEE80211_FC1_DIR_FROMDS &&
1636 dir != IEEE80211_FC1_DIR_DSTODS) {
1637 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
1638 wh, "data", "incorrect dir 0x%x", dir);
1639 vap->iv_stats.is_rx_wrongdir++;
1643 /* All Mesh data frames are QoS subtype */
1644 if (!HAS_SEQ(type)) {
1645 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
1646 wh, "data", "incorrect subtype 0x%x", subtype);
1647 vap->iv_stats.is_rx_badsubtype++;
1652 * Next up, any fragmentation.
1653 * XXX: we defrag before we even try to forward,
1654 * Mesh Control field is not present in sub-sequent
1655 * fragmented frames. This is in contrast to Draft 4.0.
1657 hdrspace = ieee80211_hdrspace(ic, wh);
1658 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1659 m = ieee80211_defrag(ni, m, hdrspace);
1661 /* Fragment dropped or frame not complete yet */
1665 wh = mtod(m, struct ieee80211_frame *); /* NB: after defrag */
1668 * Now we have a complete Mesh Data frame.
1672 * Only fromDStoDS data frames use 4 address qos frames
1673 * as specified in amendment. Otherwise addr4 is located
1674 * in the Mesh Control field and a 3 address qos frame
1677 if (IEEE80211_IS_DSTODS(wh))
1678 *(uint16_t *)qos = *(uint16_t *)
1679 ((struct ieee80211_qosframe_addr4 *)wh)->i_qos;
1681 *(uint16_t *)qos = *(uint16_t *)
1682 ((struct ieee80211_qosframe *)wh)->i_qos;
1685 * NB: The mesh STA sets the Mesh Control Present
1686 * subfield to 1 in the Mesh Data frame containing
1687 * an unfragmented MSDU, an A-MSDU, or the first
1688 * fragment of an MSDU.
1689 * After defrag it should always be present.
1691 if (!(qos[1] & IEEE80211_QOS_MC)) {
1692 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_MESH,
1693 ni->ni_macaddr, NULL,
1694 "%s", "Mesh control field not present");
1695 vap->iv_stats.is_rx_elem_missing++; /* XXX: kinda */
1699 /* pull up enough to get to the mesh control */
1700 if (m->m_len < hdrspace + sizeof(struct ieee80211_meshcntl) &&
1701 (m = m_pullup(m, hdrspace +
1702 sizeof(struct ieee80211_meshcntl))) == NULL) {
1703 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
1704 ni->ni_macaddr, NULL,
1705 "data too short: expecting %u", hdrspace);
1706 vap->iv_stats.is_rx_tooshort++;
1710 * Now calculate the full extent of the headers. Note
1711 * mesh_decap will pull up anything we didn't get
1712 * above when it strips the 802.11 headers.
1714 mc = (const struct ieee80211_meshcntl *)
1715 (mtod(m, const uint8_t *) + hdrspace);
1716 ae = mc->mc_flags & IEEE80211_MESH_AE_MASK;
1717 meshdrlen = sizeof(struct ieee80211_meshcntl) +
1718 ae * IEEE80211_ADDR_LEN;
1719 hdrspace += meshdrlen;
1721 /* pull complete hdrspace = ieee80211_hdrspace + meshcontrol */
1722 if ((meshdrlen > sizeof(struct ieee80211_meshcntl)) &&
1723 (m->m_len < hdrspace) &&
1724 ((m = m_pullup(m, hdrspace)) == NULL)) {
1725 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
1726 ni->ni_macaddr, NULL,
1727 "data too short: expecting %u", hdrspace);
1728 vap->iv_stats.is_rx_tooshort++;
1731 /* XXX: are we sure there is no reallocating after m_pullup? */
1733 seq = LE_READ_4(mc->mc_seq);
1734 if (IEEE80211_IS_MULTICAST(wh->i_addr1))
1736 else if (ae == IEEE80211_MESH_AE_01)
1737 addr = MC01(mc)->mc_addr4;
1739 addr = ((struct ieee80211_qosframe_addr4 *)wh)->i_addr4;
1740 if (IEEE80211_ADDR_EQ(vap->iv_myaddr, addr)) {
1741 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
1742 addr, "data", "%s", "not to me");
1743 vap->iv_stats.is_rx_wrongbss++; /* XXX kinda */
1746 if (mesh_checkpseq(vap, addr, seq) != 0) {
1747 vap->iv_stats.is_rx_dup++;
1751 /* This code "routes" the frame to the right control path */
1752 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1753 if (IEEE80211_ADDR_EQ(vap->iv_myaddr, wh->i_addr3))
1755 mesh_recv_indiv_data_to_me(vap, m, wh, mc);
1756 else if (IEEE80211_IS_MULTICAST(wh->i_addr3))
1757 error = mesh_recv_group_data(vap, m, wh, mc);
1759 error = mesh_recv_indiv_data_to_fwrd(vap, m,
1762 error = mesh_recv_group_data(vap, m, wh, mc);
1768 if (ieee80211_radiotap_active_vap(vap))
1769 ieee80211_radiotap_rx(vap, m);
1773 * Finally, strip the 802.11 header.
1775 m = mesh_decap(vap, m, hdrspace, meshdrlen);
1777 /* XXX mask bit to check for both */
1778 /* don't count Null data frames as errors */
1779 if (subtype == IEEE80211_FC0_SUBTYPE_NODATA ||
1780 subtype == IEEE80211_FC0_SUBTYPE_QOS_NULL)
1782 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
1783 ni->ni_macaddr, "data", "%s", "decap error");
1784 vap->iv_stats.is_rx_decap++;
1785 IEEE80211_NODE_STAT(ni, rx_decap);
1788 if (qos[0] & IEEE80211_QOS_AMSDU) {
1789 m = ieee80211_decap_amsdu(ni, m);
1791 return IEEE80211_FC0_TYPE_DATA;
1793 ieee80211_deliver_data(vap, ni, m);
1795 case IEEE80211_FC0_TYPE_MGT:
1796 vap->iv_stats.is_rx_mgmt++;
1797 IEEE80211_NODE_STAT(ni, rx_mgmt);
1798 if (dir != IEEE80211_FC1_DIR_NODS) {
1799 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
1800 wh, "mgt", "incorrect dir 0x%x", dir);
1801 vap->iv_stats.is_rx_wrongdir++;
1804 if (m->m_pkthdr.len < sizeof(struct ieee80211_frame)) {
1805 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
1806 ni->ni_macaddr, "mgt", "too short: len %u",
1808 vap->iv_stats.is_rx_tooshort++;
1811 #ifdef IEEE80211_DEBUG
1812 if ((ieee80211_msg_debug(vap) &&
1813 (vap->iv_ic->ic_flags & IEEE80211_F_SCAN)) ||
1814 ieee80211_msg_dumppkts(vap)) {
1815 if_printf(ifp, "received %s from %s rssi %d\n",
1816 ieee80211_mgt_subtype_name[subtype >>
1817 IEEE80211_FC0_SUBTYPE_SHIFT],
1818 ether_sprintf(wh->i_addr2), rssi);
1821 if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
1822 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
1823 wh, NULL, "%s", "WEP set but not permitted");
1824 vap->iv_stats.is_rx_mgtdiscard++; /* XXX */
1827 vap->iv_recv_mgmt(ni, m, subtype, rssi, nf);
1829 case IEEE80211_FC0_TYPE_CTL:
1830 vap->iv_stats.is_rx_ctl++;
1831 IEEE80211_NODE_STAT(ni, rx_ctrl);
1834 IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
1835 wh, "bad", "frame type 0x%x", type);
1836 /* should not come here */
1840 IFNET_STAT_INC(ifp, ierrors, 1);
1843 if (need_tap && ieee80211_radiotap_active_vap(vap))
1844 ieee80211_radiotap_rx(vap, m);
1854 mesh_recv_mgmt(struct ieee80211_node *ni, struct mbuf *m0, int subtype,
1857 struct ieee80211vap *vap = ni->ni_vap;
1858 struct ieee80211_mesh_state *ms = vap->iv_mesh;
1859 struct ieee80211com *ic = ni->ni_ic;
1860 struct ieee80211_frame *wh;
1861 struct ieee80211_mesh_route *rt;
1862 uint8_t *frm, *efrm;
1864 wh = mtod(m0, struct ieee80211_frame *);
1865 frm = (uint8_t *)&wh[1];
1866 efrm = mtod(m0, uint8_t *) + m0->m_len;
1868 case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
1869 case IEEE80211_FC0_SUBTYPE_BEACON:
1871 struct ieee80211_scanparams scan;
1873 * We process beacon/probe response
1874 * frames to discover neighbors.
1876 if (ieee80211_parse_beacon(ni, m0, &scan) != 0)
1879 * Count frame now that we know it's to be processed.
1881 if (subtype == IEEE80211_FC0_SUBTYPE_BEACON) {
1882 vap->iv_stats.is_rx_beacon++; /* XXX remove */
1883 IEEE80211_NODE_STAT(ni, rx_beacons);
1885 IEEE80211_NODE_STAT(ni, rx_proberesp);
1887 * If scanning, just pass information to the scan module.
1889 if (ic->ic_flags & IEEE80211_F_SCAN) {
1890 if (ic->ic_flags_ext & IEEE80211_FEXT_PROBECHAN) {
1892 * Actively scanning a channel marked passive;
1893 * send a probe request now that we know there
1894 * is 802.11 traffic present.
1896 * XXX check if the beacon we recv'd gives
1897 * us what we need and suppress the probe req
1899 ieee80211_probe_curchan(vap, 1);
1900 ic->ic_flags_ext &= ~IEEE80211_FEXT_PROBECHAN;
1902 ieee80211_add_scan(vap, &scan, wh,
1907 /* The rest of this code assumes we are running */
1908 if (vap->iv_state != IEEE80211_S_RUN)
1911 * Ignore non-mesh STAs.
1914 (IEEE80211_CAPINFO_ESS|IEEE80211_CAPINFO_IBSS)) ||
1915 scan.meshid == NULL || scan.meshconf == NULL) {
1916 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
1917 wh, "beacon", "%s", "not a mesh sta");
1918 vap->iv_stats.is_mesh_wrongmesh++;
1922 * Ignore STAs for other mesh networks.
1924 if (memcmp(scan.meshid+2, ms->ms_id, ms->ms_idlen) != 0 ||
1925 mesh_verify_meshconf(vap, scan.meshconf)) {
1926 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
1927 wh, "beacon", "%s", "not for our mesh");
1928 vap->iv_stats.is_mesh_wrongmesh++;
1932 * Peer only based on the current ACL policy.
1934 if (vap->iv_acl != NULL && !vap->iv_acl->iac_check(vap, wh)) {
1935 IEEE80211_DISCARD(vap, IEEE80211_MSG_ACL,
1936 wh, NULL, "%s", "disallowed by ACL");
1937 vap->iv_stats.is_rx_acl++;
1941 * Do neighbor discovery.
1943 if (!IEEE80211_ADDR_EQ(wh->i_addr2, ni->ni_macaddr)) {
1945 * Create a new entry in the neighbor table.
1947 ni = ieee80211_add_neighbor(vap, wh, &scan);
1950 * Automatically peer with discovered nodes if possible.
1952 if (ni != vap->iv_bss &&
1953 (ms->ms_flags & IEEE80211_MESHFLAGS_AP)) {
1954 switch (ni->ni_mlstate) {
1955 case IEEE80211_NODE_MESH_IDLE:
1959 /* Wait for backoff callout to reset counter */
1960 if (ni->ni_mlhcnt >= ieee80211_mesh_maxholding)
1963 ni->ni_mlpid = mesh_generateid(vap);
1964 if (ni->ni_mlpid == 0)
1966 mesh_linkchange(ni, IEEE80211_NODE_MESH_OPENSNT);
1967 args[0] = ni->ni_mlpid;
1968 ieee80211_send_action(ni,
1969 IEEE80211_ACTION_CAT_SELF_PROT,
1970 IEEE80211_ACTION_MESHPEERING_OPEN, args);
1972 mesh_peer_timeout_setup(ni);
1975 case IEEE80211_NODE_MESH_ESTABLISHED:
1978 * Valid beacon from a peer mesh STA
1981 rt = ieee80211_mesh_rt_find(vap, wh->i_addr2);
1983 ieee80211_mesh_rt_update(rt,
1985 ms->ms_ppath->mpp_inact));
1995 case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
1997 uint8_t *ssid, *meshid, *rates, *xrates;
2000 if (vap->iv_state != IEEE80211_S_RUN) {
2001 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
2002 wh, NULL, "wrong state %s",
2003 ieee80211_state_name[vap->iv_state]);
2004 vap->iv_stats.is_rx_mgtdiscard++;
2007 if (IEEE80211_IS_MULTICAST(wh->i_addr2)) {
2008 /* frame must be directed */
2009 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
2010 wh, NULL, "%s", "not unicast");
2011 vap->iv_stats.is_rx_mgtdiscard++; /* XXX stat */
2015 * prreq frame format
2017 * [tlv] supported rates
2018 * [tlv] extended supported rates
2021 ssid = meshid = rates = xrates = NULL;
2023 while (efrm - frm > 1) {
2024 IEEE80211_VERIFY_LENGTH(efrm - frm, frm[1] + 2, return);
2026 case IEEE80211_ELEMID_SSID:
2029 case IEEE80211_ELEMID_RATES:
2032 case IEEE80211_ELEMID_XRATES:
2035 case IEEE80211_ELEMID_MESHID:
2041 IEEE80211_VERIFY_ELEMENT(ssid, IEEE80211_NWID_LEN, return);
2042 IEEE80211_VERIFY_ELEMENT(rates, IEEE80211_RATE_MAXSIZE, return);
2044 IEEE80211_VERIFY_ELEMENT(xrates,
2045 IEEE80211_RATE_MAXSIZE - rates[1], return);
2046 if (meshid != NULL) {
2047 IEEE80211_VERIFY_ELEMENT(meshid,
2048 IEEE80211_MESHID_LEN, return);
2049 /* NB: meshid, not ssid */
2050 IEEE80211_VERIFY_SSID(vap->iv_bss, meshid, return);
2053 /* XXX find a better class or define it's own */
2054 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_INPUT, wh->i_addr2,
2055 "%s", "recv probe req");
2057 * Some legacy 11b clients cannot hack a complete
2058 * probe response frame. When the request includes
2059 * only a bare-bones rate set, communicate this to
2060 * the transmit side.
2062 ieee80211_send_proberesp(vap, wh->i_addr2, 0);
2066 case IEEE80211_FC0_SUBTYPE_ACTION:
2067 case IEEE80211_FC0_SUBTYPE_ACTION_NOACK:
2068 if (ni == vap->iv_bss) {
2069 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
2070 wh, NULL, "%s", "unknown node");
2071 vap->iv_stats.is_rx_mgtdiscard++;
2072 } else if (!IEEE80211_ADDR_EQ(vap->iv_myaddr, wh->i_addr1) &&
2073 !IEEE80211_IS_MULTICAST(wh->i_addr1)) {
2074 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
2075 wh, NULL, "%s", "not for us");
2076 vap->iv_stats.is_rx_mgtdiscard++;
2077 } else if (vap->iv_state != IEEE80211_S_RUN) {
2078 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
2079 wh, NULL, "wrong state %s",
2080 ieee80211_state_name[vap->iv_state]);
2081 vap->iv_stats.is_rx_mgtdiscard++;
2083 if (ieee80211_parse_action(ni, m0) == 0)
2084 (void)ic->ic_recv_action(ni, wh, frm, efrm);
2088 case IEEE80211_FC0_SUBTYPE_ASSOC_REQ:
2089 case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
2090 case IEEE80211_FC0_SUBTYPE_REASSOC_REQ:
2091 case IEEE80211_FC0_SUBTYPE_REASSOC_RESP:
2092 case IEEE80211_FC0_SUBTYPE_ATIM:
2093 case IEEE80211_FC0_SUBTYPE_DISASSOC:
2094 case IEEE80211_FC0_SUBTYPE_AUTH:
2095 case IEEE80211_FC0_SUBTYPE_DEAUTH:
2096 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
2097 wh, NULL, "%s", "not handled");
2098 vap->iv_stats.is_rx_mgtdiscard++;
2102 IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
2103 wh, "mgt", "subtype 0x%x not handled", subtype);
2104 vap->iv_stats.is_rx_badsubtype++;
2110 mesh_recv_ctl(struct ieee80211_node *ni, struct mbuf *m, int subtype)
2114 case IEEE80211_FC0_SUBTYPE_BAR:
2115 ieee80211_recv_bar(ni, m);
2121 * Parse meshpeering action ie's for MPM frames
2123 static const struct ieee80211_meshpeer_ie *
2124 mesh_parse_meshpeering_action(struct ieee80211_node *ni,
2125 const struct ieee80211_frame *wh, /* XXX for VERIFY_LENGTH */
2126 const uint8_t *frm, const uint8_t *efrm,
2127 struct ieee80211_meshpeer_ie *mp, uint8_t subtype)
2129 struct ieee80211vap *vap = ni->ni_vap;
2130 const struct ieee80211_meshpeer_ie *mpie;
2132 const uint8_t *meshid, *meshconf, *meshpeer;
2133 uint8_t sendclose = 0; /* 1 = MPM frame rejected, close will be sent */
2135 meshid = meshconf = meshpeer = NULL;
2136 while (efrm - frm > 1) {
2137 IEEE80211_VERIFY_LENGTH(efrm - frm, frm[1] + 2, return NULL);
2139 case IEEE80211_ELEMID_MESHID:
2142 case IEEE80211_ELEMID_MESHCONF:
2145 case IEEE80211_ELEMID_MESHPEER:
2147 mpie = (const struct ieee80211_meshpeer_ie *) frm;
2148 memset(mp, 0, sizeof(*mp));
2149 mp->peer_len = mpie->peer_len;
2150 mp->peer_proto = LE_READ_2(&mpie->peer_proto);
2151 mp->peer_llinkid = LE_READ_2(&mpie->peer_llinkid);
2153 case IEEE80211_ACTION_MESHPEERING_CONFIRM:
2155 LE_READ_2(&mpie->peer_linkid);
2157 case IEEE80211_ACTION_MESHPEERING_CLOSE:
2158 /* NB: peer link ID is optional */
2159 if (mpie->peer_len ==
2160 (IEEE80211_MPM_BASE_SZ + 2)) {
2161 mp->peer_linkid = 0;
2163 LE_READ_2(&mpie->peer_linkid);
2166 LE_READ_2(&mpie->peer_linkid);
2168 LE_READ_2(&mpie->peer_rcode);
2178 * Verify the contents of the frame.
2179 * If it fails validation, close the peer link.
2181 if (mesh_verify_meshpeer(vap, subtype, (const uint8_t *)mp)) {
2183 IEEE80211_DISCARD(vap,
2184 IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH,
2185 wh, NULL, "%s", "MPM validation failed");
2188 /* If meshid is not the same reject any frames type. */
2189 if (sendclose == 0 && mesh_verify_meshid(vap, meshid)) {
2191 IEEE80211_DISCARD(vap,
2192 IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH,
2193 wh, NULL, "%s", "not for our mesh");
2194 if (subtype == IEEE80211_ACTION_MESHPEERING_CLOSE) {
2196 * Standard not clear about this, if we dont ignore
2197 * there will be an endless loop between nodes sending
2198 * CLOSE frames between each other with wrong meshid.
2199 * Discard and timers will bring FSM to IDLE state.
2206 * Close frames are accepted if meshid is the same.
2207 * Verify the other two types.
2209 if (sendclose == 0 && subtype != IEEE80211_ACTION_MESHPEERING_CLOSE &&
2210 mesh_verify_meshconf(vap, meshconf)) {
2212 IEEE80211_DISCARD(vap,
2213 IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH,
2214 wh, NULL, "%s", "configuration missmatch");
2218 vap->iv_stats.is_rx_mgtdiscard++;
2219 switch (ni->ni_mlstate) {
2220 case IEEE80211_NODE_MESH_IDLE:
2221 case IEEE80211_NODE_MESH_ESTABLISHED:
2222 case IEEE80211_NODE_MESH_HOLDING:
2225 case IEEE80211_NODE_MESH_OPENSNT:
2226 case IEEE80211_NODE_MESH_OPENRCV:
2227 case IEEE80211_NODE_MESH_CONFIRMRCV:
2228 args[0] = ni->ni_mlpid;
2229 args[1] = ni->ni_mllid;
2230 /* Reason codes for rejection */
2232 case IEEE80211_ACTION_MESHPEERING_OPEN:
2233 args[2] = IEEE80211_REASON_MESH_CPVIOLATION;
2235 case IEEE80211_ACTION_MESHPEERING_CONFIRM:
2236 args[2] = IEEE80211_REASON_MESH_INCONS_PARAMS;
2239 ieee80211_send_action(ni,
2240 IEEE80211_ACTION_CAT_SELF_PROT,
2241 IEEE80211_ACTION_MESHPEERING_CLOSE,
2243 mesh_linkchange(ni, IEEE80211_NODE_MESH_HOLDING);
2244 mesh_peer_timeout_setup(ni);
2250 return (const struct ieee80211_meshpeer_ie *) mp;
2254 mesh_recv_action_meshpeering_open(struct ieee80211_node *ni,
2255 const struct ieee80211_frame *wh,
2256 const uint8_t *frm, const uint8_t *efrm)
2258 struct ieee80211vap *vap = ni->ni_vap;
2259 struct ieee80211_mesh_state *ms = vap->iv_mesh;
2260 struct ieee80211_meshpeer_ie ie;
2261 const struct ieee80211_meshpeer_ie *meshpeer;
2264 /* +2+2 for action + code + capabilites */
2265 meshpeer = mesh_parse_meshpeering_action(ni, wh, frm+2+2, efrm, &ie,
2266 IEEE80211_ACTION_MESHPEERING_OPEN);
2267 if (meshpeer == NULL) {
2272 IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH, ni,
2273 "recv PEER OPEN, lid 0x%x", meshpeer->peer_llinkid);
2275 switch (ni->ni_mlstate) {
2276 case IEEE80211_NODE_MESH_IDLE:
2277 /* Reject open request if reached our maximum neighbor count */
2278 if (ms->ms_neighbors >= IEEE80211_MESH_MAX_NEIGHBORS) {
2279 args[0] = meshpeer->peer_llinkid;
2281 args[2] = IEEE80211_REASON_MESH_MAX_PEERS;
2282 ieee80211_send_action(ni,
2283 IEEE80211_ACTION_CAT_SELF_PROT,
2284 IEEE80211_ACTION_MESHPEERING_CLOSE,
2286 /* stay in IDLE state */
2289 /* Open frame accepted */
2290 mesh_linkchange(ni, IEEE80211_NODE_MESH_OPENRCV);
2291 ni->ni_mllid = meshpeer->peer_llinkid;
2292 ni->ni_mlpid = mesh_generateid(vap);
2293 if (ni->ni_mlpid == 0)
2295 args[0] = ni->ni_mlpid;
2296 /* Announce we're open too... */
2297 ieee80211_send_action(ni,
2298 IEEE80211_ACTION_CAT_SELF_PROT,
2299 IEEE80211_ACTION_MESHPEERING_OPEN, args);
2300 /* ...and confirm the link. */
2301 args[0] = ni->ni_mlpid;
2302 args[1] = ni->ni_mllid;
2303 ieee80211_send_action(ni,
2304 IEEE80211_ACTION_CAT_SELF_PROT,
2305 IEEE80211_ACTION_MESHPEERING_CONFIRM,
2307 mesh_peer_timeout_setup(ni);
2309 case IEEE80211_NODE_MESH_OPENRCV:
2311 if (ni->ni_mllid != meshpeer->peer_llinkid) {
2312 args[0] = ni->ni_mllid;
2313 args[1] = ni->ni_mlpid;
2314 args[2] = IEEE80211_REASON_PEER_LINK_CANCELED;
2315 ieee80211_send_action(ni,
2316 IEEE80211_ACTION_CAT_SELF_PROT,
2317 IEEE80211_ACTION_MESHPEERING_CLOSE,
2319 mesh_linkchange(ni, IEEE80211_NODE_MESH_HOLDING);
2320 mesh_peer_timeout_setup(ni);
2323 /* Duplicate open, confirm again. */
2324 args[0] = ni->ni_mlpid;
2325 args[1] = ni->ni_mllid;
2326 ieee80211_send_action(ni,
2327 IEEE80211_ACTION_CAT_SELF_PROT,
2328 IEEE80211_ACTION_MESHPEERING_CONFIRM,
2331 case IEEE80211_NODE_MESH_OPENSNT:
2332 ni->ni_mllid = meshpeer->peer_llinkid;
2333 mesh_linkchange(ni, IEEE80211_NODE_MESH_OPENRCV);
2334 args[0] = ni->ni_mlpid;
2335 args[1] = ni->ni_mllid;
2336 ieee80211_send_action(ni,
2337 IEEE80211_ACTION_CAT_SELF_PROT,
2338 IEEE80211_ACTION_MESHPEERING_CONFIRM,
2340 /* NB: don't setup/clear any timeout */
2342 case IEEE80211_NODE_MESH_CONFIRMRCV:
2343 if (ni->ni_mlpid != meshpeer->peer_linkid ||
2344 ni->ni_mllid != meshpeer->peer_llinkid) {
2345 args[0] = ni->ni_mlpid;
2346 args[1] = ni->ni_mllid;
2347 args[2] = IEEE80211_REASON_PEER_LINK_CANCELED;
2348 ieee80211_send_action(ni,
2349 IEEE80211_ACTION_CAT_SELF_PROT,
2350 IEEE80211_ACTION_MESHPEERING_CLOSE,
2353 IEEE80211_NODE_MESH_HOLDING);
2354 mesh_peer_timeout_setup(ni);
2357 mesh_linkchange(ni, IEEE80211_NODE_MESH_ESTABLISHED);
2358 ni->ni_mllid = meshpeer->peer_llinkid;
2359 args[0] = ni->ni_mlpid;
2360 args[1] = ni->ni_mllid;
2361 ieee80211_send_action(ni,
2362 IEEE80211_ACTION_CAT_SELF_PROT,
2363 IEEE80211_ACTION_MESHPEERING_CONFIRM,
2365 mesh_peer_timeout_stop(ni);
2367 case IEEE80211_NODE_MESH_ESTABLISHED:
2368 if (ni->ni_mllid != meshpeer->peer_llinkid) {
2369 args[0] = ni->ni_mllid;
2370 args[1] = ni->ni_mlpid;
2371 args[2] = IEEE80211_REASON_PEER_LINK_CANCELED;
2372 ieee80211_send_action(ni,
2373 IEEE80211_ACTION_CAT_SELF_PROT,
2374 IEEE80211_ACTION_MESHPEERING_CLOSE,
2376 mesh_linkchange(ni, IEEE80211_NODE_MESH_HOLDING);
2377 mesh_peer_timeout_setup(ni);
2380 args[0] = ni->ni_mlpid;
2381 args[1] = ni->ni_mllid;
2382 ieee80211_send_action(ni,
2383 IEEE80211_ACTION_CAT_SELF_PROT,
2384 IEEE80211_ACTION_MESHPEERING_CONFIRM,
2387 case IEEE80211_NODE_MESH_HOLDING:
2388 args[0] = ni->ni_mlpid;
2389 args[1] = meshpeer->peer_llinkid;
2390 /* Standard not clear about what the reaason code should be */
2391 args[2] = IEEE80211_REASON_PEER_LINK_CANCELED;
2392 ieee80211_send_action(ni,
2393 IEEE80211_ACTION_CAT_SELF_PROT,
2394 IEEE80211_ACTION_MESHPEERING_CLOSE,
2402 mesh_recv_action_meshpeering_confirm(struct ieee80211_node *ni,
2403 const struct ieee80211_frame *wh,
2404 const uint8_t *frm, const uint8_t *efrm)
2406 struct ieee80211vap *vap = ni->ni_vap;
2407 struct ieee80211_meshpeer_ie ie;
2408 const struct ieee80211_meshpeer_ie *meshpeer;
2411 /* +2+2+2+2 for action + code + capabilites + status code + AID */
2412 meshpeer = mesh_parse_meshpeering_action(ni, wh, frm+2+2+2+2, efrm, &ie,
2413 IEEE80211_ACTION_MESHPEERING_CONFIRM);
2414 if (meshpeer == NULL) {
2418 IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH, ni,
2419 "recv PEER CONFIRM, local id 0x%x, peer id 0x%x",
2420 meshpeer->peer_llinkid, meshpeer->peer_linkid);
2422 switch (ni->ni_mlstate) {
2423 case IEEE80211_NODE_MESH_OPENRCV:
2424 mesh_linkchange(ni, IEEE80211_NODE_MESH_ESTABLISHED);
2425 mesh_peer_timeout_stop(ni);
2427 case IEEE80211_NODE_MESH_OPENSNT:
2428 mesh_linkchange(ni, IEEE80211_NODE_MESH_CONFIRMRCV);
2429 mesh_peer_timeout_setup(ni);
2431 case IEEE80211_NODE_MESH_HOLDING:
2432 args[0] = ni->ni_mlpid;
2433 args[1] = meshpeer->peer_llinkid;
2434 /* Standard not clear about what the reaason code should be */
2435 args[2] = IEEE80211_REASON_PEER_LINK_CANCELED;
2436 ieee80211_send_action(ni,
2437 IEEE80211_ACTION_CAT_SELF_PROT,
2438 IEEE80211_ACTION_MESHPEERING_CLOSE,
2441 case IEEE80211_NODE_MESH_CONFIRMRCV:
2442 if (ni->ni_mllid != meshpeer->peer_llinkid) {
2443 args[0] = ni->ni_mlpid;
2444 args[1] = ni->ni_mllid;
2445 args[2] = IEEE80211_REASON_PEER_LINK_CANCELED;
2446 ieee80211_send_action(ni,
2447 IEEE80211_ACTION_CAT_SELF_PROT,
2448 IEEE80211_ACTION_MESHPEERING_CLOSE,
2450 mesh_linkchange(ni, IEEE80211_NODE_MESH_HOLDING);
2451 mesh_peer_timeout_setup(ni);
2455 IEEE80211_DISCARD(vap,
2456 IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH,
2457 wh, NULL, "received confirm in invalid state %d",
2459 vap->iv_stats.is_rx_mgtdiscard++;
2466 mesh_recv_action_meshpeering_close(struct ieee80211_node *ni,
2467 const struct ieee80211_frame *wh,
2468 const uint8_t *frm, const uint8_t *efrm)
2470 struct ieee80211_meshpeer_ie ie;
2471 const struct ieee80211_meshpeer_ie *meshpeer;
2474 /* +2 for action + code */
2475 meshpeer = mesh_parse_meshpeering_action(ni, wh, frm+2, efrm, &ie,
2476 IEEE80211_ACTION_MESHPEERING_CLOSE);
2477 if (meshpeer == NULL) {
2482 * XXX: check reason code, for example we could receive
2483 * IEEE80211_REASON_MESH_MAX_PEERS then we should not attempt
2487 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH,
2488 ni, "%s", "recv PEER CLOSE");
2490 switch (ni->ni_mlstate) {
2491 case IEEE80211_NODE_MESH_IDLE:
2494 case IEEE80211_NODE_MESH_OPENRCV:
2495 case IEEE80211_NODE_MESH_OPENSNT:
2496 case IEEE80211_NODE_MESH_CONFIRMRCV:
2497 case IEEE80211_NODE_MESH_ESTABLISHED:
2498 args[0] = ni->ni_mlpid;
2499 args[1] = ni->ni_mllid;
2500 args[2] = IEEE80211_REASON_MESH_CLOSE_RCVD;
2501 ieee80211_send_action(ni,
2502 IEEE80211_ACTION_CAT_SELF_PROT,
2503 IEEE80211_ACTION_MESHPEERING_CLOSE,
2505 mesh_linkchange(ni, IEEE80211_NODE_MESH_HOLDING);
2506 mesh_peer_timeout_setup(ni);
2508 case IEEE80211_NODE_MESH_HOLDING:
2509 mesh_linkchange(ni, IEEE80211_NODE_MESH_IDLE);
2510 mesh_peer_timeout_stop(ni);
2517 * Link Metric handling.
2520 mesh_recv_action_meshlmetric(struct ieee80211_node *ni,
2521 const struct ieee80211_frame *wh,
2522 const uint8_t *frm, const uint8_t *efrm)
2524 const struct ieee80211_meshlmetric_ie *ie =
2525 (const struct ieee80211_meshlmetric_ie *)
2526 (frm+2); /* action + code */
2527 struct ieee80211_meshlmetric_ie lm_rep;
2529 if (ie->lm_flags & IEEE80211_MESH_LMETRIC_FLAGS_REQ) {
2530 lm_rep.lm_flags = 0;
2531 lm_rep.lm_metric = mesh_airtime_calc(ni);
2532 ieee80211_send_action(ni,
2533 IEEE80211_ACTION_CAT_MESH,
2534 IEEE80211_ACTION_MESH_LMETRIC,
2537 /* XXX: else do nothing for now */
2542 * Parse meshgate action ie's for GANN frames.
2543 * Returns -1 if parsing fails, otherwise 0.
2546 mesh_parse_meshgate_action(struct ieee80211_node *ni,
2547 const struct ieee80211_frame *wh, /* XXX for VERIFY_LENGTH */
2548 struct ieee80211_meshgann_ie *ie, const uint8_t *frm, const uint8_t *efrm)
2550 struct ieee80211vap *vap = ni->ni_vap;
2551 const struct ieee80211_meshgann_ie *gannie;
2553 while (efrm - frm > 1) {
2554 IEEE80211_VERIFY_LENGTH(efrm - frm, frm[1] + 2, return -1);
2556 case IEEE80211_ELEMID_MESHGANN:
2557 gannie = (const struct ieee80211_meshgann_ie *) frm;
2558 memset(ie, 0, sizeof(*ie));
2559 ie->gann_ie = gannie->gann_ie;
2560 ie->gann_len = gannie->gann_len;
2561 ie->gann_flags = gannie->gann_flags;
2562 ie->gann_hopcount = gannie->gann_hopcount;
2563 ie->gann_ttl = gannie->gann_ttl;
2564 IEEE80211_ADDR_COPY(ie->gann_addr, gannie->gann_addr);
2565 ie->gann_seq = LE_READ_4(&gannie->gann_seq);
2566 ie->gann_interval = LE_READ_2(&gannie->gann_interval);
2576 * Mesh Gate Announcement handling.
2579 mesh_recv_action_meshgate(struct ieee80211_node *ni,
2580 const struct ieee80211_frame *wh,
2581 const uint8_t *frm, const uint8_t *efrm)
2583 struct ieee80211vap *vap = ni->ni_vap;
2584 struct ieee80211_mesh_state *ms = vap->iv_mesh;
2585 struct ieee80211_mesh_gate_route *gr, *next;
2586 struct ieee80211_mesh_route *rt_gate;
2587 struct ieee80211_meshgann_ie pgann;
2588 struct ieee80211_meshgann_ie ie;
2591 /* +2 for action + code */
2592 if (mesh_parse_meshgate_action(ni, wh, &ie, frm+2, efrm) != 0) {
2593 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_MESH,
2594 ni->ni_macaddr, NULL, "%s",
2595 "GANN parsing failed");
2596 vap->iv_stats.is_rx_mgtdiscard++;
2600 if (IEEE80211_ADDR_EQ(vap->iv_myaddr, ie.gann_addr))
2603 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH, ni->ni_macaddr,
2604 "received GANN, meshgate: %s (seq %u)",
2605 ether_sprintf(ie.gann_addr),
2611 TAILQ_FOREACH_SAFE(gr, &ms->ms_known_gates, gr_next, next) {
2612 if (!IEEE80211_ADDR_EQ(gr->gr_addr, ie.gann_addr))
2614 if (ie.gann_seq <= gr->gr_lastseq) {
2615 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_MESH,
2616 ni->ni_macaddr, NULL,
2617 "GANN old seqno %u <= %u",
2618 ie.gann_seq, gr->gr_lastseq);
2622 /* corresponding mesh gate found & GANN accepted */
2628 /* this GANN is from a new mesh Gate add it to known table. */
2629 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH, ie.gann_addr,
2630 "stored new GANN information, seq %u.", ie.gann_seq);
2631 gr = kmalloc(ALIGN(sizeof(struct ieee80211_mesh_gate_route)),
2632 M_80211_MESH_GT_RT, M_INTWAIT | M_ZERO);
2633 IEEE80211_ADDR_COPY(gr->gr_addr, ie.gann_addr);
2634 TAILQ_INSERT_TAIL(&ms->ms_known_gates, gr, gr_next);
2636 gr->gr_lastseq = ie.gann_seq;
2638 /* check if we have a path to this gate */
2639 rt_gate = mesh_rt_find_locked(ms, gr->gr_addr);
2640 if (rt_gate != NULL &&
2641 rt_gate->rt_flags & IEEE80211_MESHRT_FLAGS_VALID) {
2642 gr->gr_route = rt_gate;
2643 rt_gate->rt_flags |= IEEE80211_MESHRT_FLAGS_GATE;
2648 /* popagate only if decremented ttl >= 1 && forwarding is enabled */
2649 if ((ie.gann_ttl - 1) < 1 && !(ms->ms_flags & IEEE80211_MESHFLAGS_FWD))
2651 pgann.gann_flags = ie.gann_flags; /* Reserved */
2652 pgann.gann_hopcount = ie.gann_hopcount + 1;
2653 pgann.gann_ttl = ie.gann_ttl - 1;
2654 IEEE80211_ADDR_COPY(pgann.gann_addr, ie.gann_addr);
2655 pgann.gann_seq = ie.gann_seq;
2656 pgann.gann_interval = ie.gann_interval;
2658 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH, ie.gann_addr,
2659 "%s", "propagate GANN");
2661 ieee80211_send_action(vap->iv_bss, IEEE80211_ACTION_CAT_MESH,
2662 IEEE80211_ACTION_MESH_GANN, &pgann);
2668 mesh_send_action(struct ieee80211_node *ni,
2669 const uint8_t sa[IEEE80211_ADDR_LEN],
2670 const uint8_t da[IEEE80211_ADDR_LEN],
2673 struct ieee80211vap *vap = ni->ni_vap;
2674 struct ieee80211com *ic = ni->ni_ic;
2675 struct ieee80211_bpf_params params;
2676 struct ieee80211_frame *wh;
2679 KASSERT(ni != NULL, ("null node"));
2681 if (vap->iv_state == IEEE80211_S_CAC) {
2682 IEEE80211_NOTE(vap, IEEE80211_MSG_OUTPUT, ni,
2683 "block %s frame in CAC state", "Mesh action");
2684 vap->iv_stats.is_tx_badstate++;
2685 ieee80211_free_node(ni);
2687 return EIO; /* XXX */
2690 M_PREPEND(m, sizeof(struct ieee80211_frame), M_NOWAIT);
2692 ieee80211_free_node(ni);
2696 IEEE80211_TX_LOCK(ic);
2697 wh = mtod(m, struct ieee80211_frame *);
2698 ieee80211_send_setup(ni, m,
2699 IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_ACTION,
2700 IEEE80211_NONQOS_TID, sa, da, sa);
2701 m->m_flags |= M_ENCAP; /* mark encapsulated */
2703 memset(¶ms, 0, sizeof(params));
2704 params.ibp_pri = WME_AC_VO;
2705 params.ibp_rate0 = ni->ni_txparms->mgmtrate;
2706 if (IEEE80211_IS_MULTICAST(da))
2707 params.ibp_try0 = 1;
2709 params.ibp_try0 = ni->ni_txparms->maxretry;
2710 params.ibp_power = ni->ni_txpower;
2712 IEEE80211_NODE_STAT(ni, tx_mgmt);
2714 ret = ieee80211_raw_output(vap, ni, m, ¶ms);
2715 IEEE80211_TX_UNLOCK(ic);
2719 #define ADDSHORT(frm, v) do { \
2720 frm[0] = (v) & 0xff; \
2721 frm[1] = (v) >> 8; \
2724 #define ADDWORD(frm, v) do { \
2725 frm[0] = (v) & 0xff; \
2726 frm[1] = ((v) >> 8) & 0xff; \
2727 frm[2] = ((v) >> 16) & 0xff; \
2728 frm[3] = ((v) >> 24) & 0xff; \
2733 mesh_send_action_meshpeering_open(struct ieee80211_node *ni,
2734 int category, int action, void *args0)
2736 struct ieee80211vap *vap = ni->ni_vap;
2737 struct ieee80211com *ic = ni->ni_ic;
2738 uint16_t *args = args0;
2739 const struct ieee80211_rateset *rs;
2743 IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH, ni,
2744 "send PEER OPEN action: localid 0x%x", args[0]);
2746 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
2747 "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", __func__, __LINE__,
2748 ni, ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)+1);
2749 ieee80211_ref_node(ni);
2751 m = ieee80211_getmgtframe(&frm,
2752 ic->ic_headroom + sizeof(struct ieee80211_frame),
2753 sizeof(uint16_t) /* action+category */
2754 + sizeof(uint16_t) /* capabilites */
2755 + 2 + IEEE80211_RATE_SIZE
2756 + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
2757 + 2 + IEEE80211_MESHID_LEN
2758 + sizeof(struct ieee80211_meshconf_ie)
2759 + sizeof(struct ieee80211_meshpeer_ie)
2763 * mesh peer open action frame format:
2771 * [tlv] mesh peer link mgmt
2775 ADDSHORT(frm, ieee80211_getcapinfo(vap, ni->ni_chan));
2776 rs = ieee80211_get_suprates(ic, ic->ic_curchan);
2777 frm = ieee80211_add_rates(frm, rs);
2778 frm = ieee80211_add_xrates(frm, rs);
2779 frm = ieee80211_add_meshid(frm, vap);
2780 frm = ieee80211_add_meshconf(frm, vap);
2781 frm = ieee80211_add_meshpeer(frm, IEEE80211_ACTION_MESHPEERING_OPEN,
2783 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2784 return mesh_send_action(ni, vap->iv_myaddr, ni->ni_macaddr, m);
2786 vap->iv_stats.is_tx_nobuf++;
2787 ieee80211_free_node(ni);
2793 mesh_send_action_meshpeering_confirm(struct ieee80211_node *ni,
2794 int category, int action, void *args0)
2796 struct ieee80211vap *vap = ni->ni_vap;
2797 struct ieee80211com *ic = ni->ni_ic;
2798 uint16_t *args = args0;
2799 const struct ieee80211_rateset *rs;
2803 IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH, ni,
2804 "send PEER CONFIRM action: localid 0x%x, peerid 0x%x",
2807 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
2808 "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", __func__, __LINE__,
2809 ni, ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)+1);
2810 ieee80211_ref_node(ni);
2812 m = ieee80211_getmgtframe(&frm,
2813 ic->ic_headroom + sizeof(struct ieee80211_frame),
2814 sizeof(uint16_t) /* action+category */
2815 + sizeof(uint16_t) /* capabilites */
2816 + sizeof(uint16_t) /* status code */
2817 + sizeof(uint16_t) /* AID */
2818 + 2 + IEEE80211_RATE_SIZE
2819 + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
2820 + 2 + IEEE80211_MESHID_LEN
2821 + sizeof(struct ieee80211_meshconf_ie)
2822 + sizeof(struct ieee80211_meshpeer_ie)
2826 * mesh peer confirm action frame format:
2831 * [2] association id (peer ID)
2836 * [tlv] mesh peer link mgmt
2840 ADDSHORT(frm, ieee80211_getcapinfo(vap, ni->ni_chan));
2841 ADDSHORT(frm, 0); /* status code */
2842 ADDSHORT(frm, args[1]); /* AID */
2843 rs = ieee80211_get_suprates(ic, ic->ic_curchan);
2844 frm = ieee80211_add_rates(frm, rs);
2845 frm = ieee80211_add_xrates(frm, rs);
2846 frm = ieee80211_add_meshid(frm, vap);
2847 frm = ieee80211_add_meshconf(frm, vap);
2848 frm = ieee80211_add_meshpeer(frm,
2849 IEEE80211_ACTION_MESHPEERING_CONFIRM,
2850 args[0], args[1], 0);
2851 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2852 return mesh_send_action(ni, vap->iv_myaddr, ni->ni_macaddr, m);
2854 vap->iv_stats.is_tx_nobuf++;
2855 ieee80211_free_node(ni);
2861 mesh_send_action_meshpeering_close(struct ieee80211_node *ni,
2862 int category, int action, void *args0)
2864 struct ieee80211vap *vap = ni->ni_vap;
2865 struct ieee80211com *ic = ni->ni_ic;
2866 uint16_t *args = args0;
2870 IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH, ni,
2871 "send PEER CLOSE action: localid 0x%x, peerid 0x%x reason %d",
2872 args[0], args[1], args[2]);
2874 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
2875 "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", __func__, __LINE__,
2876 ni, ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)+1);
2877 ieee80211_ref_node(ni);
2879 m = ieee80211_getmgtframe(&frm,
2880 ic->ic_headroom + sizeof(struct ieee80211_frame),
2881 sizeof(uint16_t) /* action+category */
2882 + sizeof(uint16_t) /* reason code */
2883 + 2 + IEEE80211_MESHID_LEN
2884 + sizeof(struct ieee80211_meshpeer_ie)
2888 * mesh peer close action frame format:
2892 * [tlv] mesh peer link mgmt
2896 frm = ieee80211_add_meshid(frm, vap);
2897 frm = ieee80211_add_meshpeer(frm,
2898 IEEE80211_ACTION_MESHPEERING_CLOSE,
2899 args[0], args[1], args[2]);
2900 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2901 return mesh_send_action(ni, vap->iv_myaddr, ni->ni_macaddr, m);
2903 vap->iv_stats.is_tx_nobuf++;
2904 ieee80211_free_node(ni);
2910 mesh_send_action_meshlmetric(struct ieee80211_node *ni,
2911 int category, int action, void *arg0)
2913 struct ieee80211vap *vap = ni->ni_vap;
2914 struct ieee80211com *ic = ni->ni_ic;
2915 struct ieee80211_meshlmetric_ie *ie = arg0;
2919 if (ie->lm_flags & IEEE80211_MESH_LMETRIC_FLAGS_REQ) {
2920 IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH,
2921 ni, "%s", "send LINK METRIC REQUEST action");
2923 IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH,
2924 ni, "send LINK METRIC REPLY action: metric 0x%x",
2927 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
2928 "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", __func__, __LINE__,
2929 ni, ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)+1);
2930 ieee80211_ref_node(ni);
2932 m = ieee80211_getmgtframe(&frm,
2933 ic->ic_headroom + sizeof(struct ieee80211_frame),
2934 sizeof(uint16_t) + /* action+category */
2935 sizeof(struct ieee80211_meshlmetric_ie)
2942 * [tlv] mesh link metric
2946 frm = ieee80211_add_meshlmetric(frm,
2947 ie->lm_flags, ie->lm_metric);
2948 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2949 return mesh_send_action(ni, vap->iv_myaddr, ni->ni_macaddr, m);
2951 vap->iv_stats.is_tx_nobuf++;
2952 ieee80211_free_node(ni);
2958 mesh_send_action_meshgate(struct ieee80211_node *ni,
2959 int category, int action, void *arg0)
2961 struct ieee80211vap *vap = ni->ni_vap;
2962 struct ieee80211com *ic = ni->ni_ic;
2963 struct ieee80211_meshgann_ie *ie = arg0;
2967 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
2968 "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", __func__, __LINE__,
2969 ni, ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)+1);
2970 ieee80211_ref_node(ni);
2972 m = ieee80211_getmgtframe(&frm,
2973 ic->ic_headroom + sizeof(struct ieee80211_frame),
2974 sizeof(uint16_t) + /* action+category */
2975 IEEE80211_MESHGANN_BASE_SZ
2982 * [tlv] mesh gate annoucement
2986 frm = ieee80211_add_meshgate(frm, ie);
2987 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2988 return mesh_send_action(ni, vap->iv_myaddr, broadcastaddr, m);
2990 vap->iv_stats.is_tx_nobuf++;
2991 ieee80211_free_node(ni);
2997 mesh_peer_timeout_setup(struct ieee80211_node *ni)
2999 switch (ni->ni_mlstate) {
3000 case IEEE80211_NODE_MESH_HOLDING:
3001 ni->ni_mltval = ieee80211_mesh_holdingtimeout;
3003 case IEEE80211_NODE_MESH_CONFIRMRCV:
3004 ni->ni_mltval = ieee80211_mesh_confirmtimeout;
3006 case IEEE80211_NODE_MESH_IDLE:
3010 ni->ni_mltval = ieee80211_mesh_retrytimeout;
3014 callout_reset(&ni->ni_mltimer, ni->ni_mltval,
3015 mesh_peer_timeout_cb, ni);
3019 * Same as above but backoffs timer statisically 50%.
3022 mesh_peer_timeout_backoff(struct ieee80211_node *ni)
3027 ni->ni_mltval += r % ni->ni_mltval;
3028 callout_reset(&ni->ni_mltimer, ni->ni_mltval, mesh_peer_timeout_cb,
3032 static __inline void
3033 mesh_peer_timeout_stop(struct ieee80211_node *ni)
3035 callout_drain(&ni->ni_mltimer);
3039 mesh_peer_backoff_cb(void *arg)
3041 struct ieee80211_node *ni = (struct ieee80211_node *)arg;
3043 /* After backoff timeout, try to peer automatically again. */
3048 * Mesh Peer Link Management FSM timeout handling.
3051 mesh_peer_timeout_cb(void *arg)
3053 struct ieee80211_node *ni = (struct ieee80211_node *)arg;
3056 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_MESH,
3057 ni, "mesh link timeout, state %d, retry counter %d",
3058 ni->ni_mlstate, ni->ni_mlrcnt);
3060 switch (ni->ni_mlstate) {
3061 case IEEE80211_NODE_MESH_IDLE:
3062 case IEEE80211_NODE_MESH_ESTABLISHED:
3064 case IEEE80211_NODE_MESH_OPENSNT:
3065 case IEEE80211_NODE_MESH_OPENRCV:
3066 if (ni->ni_mlrcnt == ieee80211_mesh_maxretries) {
3067 args[0] = ni->ni_mlpid;
3068 args[2] = IEEE80211_REASON_MESH_MAX_RETRIES;
3069 ieee80211_send_action(ni,
3070 IEEE80211_ACTION_CAT_SELF_PROT,
3071 IEEE80211_ACTION_MESHPEERING_CLOSE, args);
3073 mesh_linkchange(ni, IEEE80211_NODE_MESH_HOLDING);
3074 mesh_peer_timeout_setup(ni);
3076 args[0] = ni->ni_mlpid;
3077 ieee80211_send_action(ni,
3078 IEEE80211_ACTION_CAT_SELF_PROT,
3079 IEEE80211_ACTION_MESHPEERING_OPEN, args);
3081 mesh_peer_timeout_backoff(ni);
3084 case IEEE80211_NODE_MESH_CONFIRMRCV:
3085 args[0] = ni->ni_mlpid;
3086 args[2] = IEEE80211_REASON_MESH_CONFIRM_TIMEOUT;
3087 ieee80211_send_action(ni,
3088 IEEE80211_ACTION_CAT_SELF_PROT,
3089 IEEE80211_ACTION_MESHPEERING_CLOSE, args);
3090 mesh_linkchange(ni, IEEE80211_NODE_MESH_HOLDING);
3091 mesh_peer_timeout_setup(ni);
3093 case IEEE80211_NODE_MESH_HOLDING:
3095 if (ni->ni_mlhcnt >= ieee80211_mesh_maxholding)
3096 callout_reset(&ni->ni_mlhtimer,
3097 ieee80211_mesh_backofftimeout,
3098 mesh_peer_backoff_cb, ni);
3099 mesh_linkchange(ni, IEEE80211_NODE_MESH_IDLE);
3105 mesh_verify_meshid(struct ieee80211vap *vap, const uint8_t *ie)
3107 struct ieee80211_mesh_state *ms = vap->iv_mesh;
3109 if (ie == NULL || ie[1] != ms->ms_idlen)
3111 return memcmp(ms->ms_id, ie + 2, ms->ms_idlen);
3115 * Check if we are using the same algorithms for this mesh.
3118 mesh_verify_meshconf(struct ieee80211vap *vap, const uint8_t *ie)
3120 const struct ieee80211_meshconf_ie *meshconf =
3121 (const struct ieee80211_meshconf_ie *) ie;
3122 const struct ieee80211_mesh_state *ms = vap->iv_mesh;
3124 if (meshconf == NULL)
3126 if (meshconf->conf_pselid != ms->ms_ppath->mpp_ie) {
3127 IEEE80211_DPRINTF(vap, IEEE80211_MSG_MESH,
3128 "unknown path selection algorithm: 0x%x\n",
3129 meshconf->conf_pselid);
3132 if (meshconf->conf_pmetid != ms->ms_pmetric->mpm_ie) {
3133 IEEE80211_DPRINTF(vap, IEEE80211_MSG_MESH,
3134 "unknown path metric algorithm: 0x%x\n",
3135 meshconf->conf_pmetid);
3138 if (meshconf->conf_ccid != 0) {
3139 IEEE80211_DPRINTF(vap, IEEE80211_MSG_MESH,
3140 "unknown congestion control algorithm: 0x%x\n",
3141 meshconf->conf_ccid);
3144 if (meshconf->conf_syncid != IEEE80211_MESHCONF_SYNC_NEIGHOFF) {
3145 IEEE80211_DPRINTF(vap, IEEE80211_MSG_MESH,
3146 "unknown sync algorithm: 0x%x\n",
3147 meshconf->conf_syncid);
3150 if (meshconf->conf_authid != 0) {
3151 IEEE80211_DPRINTF(vap, IEEE80211_MSG_MESH,
3152 "unknown auth auth algorithm: 0x%x\n",
3153 meshconf->conf_pselid);
3156 /* Not accepting peers */
3157 if (!(meshconf->conf_cap & IEEE80211_MESHCONF_CAP_AP)) {
3158 IEEE80211_DPRINTF(vap, IEEE80211_MSG_MESH,
3159 "not accepting peers: 0x%x\n", meshconf->conf_cap);
3166 mesh_verify_meshpeer(struct ieee80211vap *vap, uint8_t subtype,
3169 const struct ieee80211_meshpeer_ie *meshpeer =
3170 (const struct ieee80211_meshpeer_ie *) ie;
3172 if (meshpeer == NULL ||
3173 meshpeer->peer_len < IEEE80211_MPM_BASE_SZ ||
3174 meshpeer->peer_len > IEEE80211_MPM_MAX_SZ)
3176 if (meshpeer->peer_proto != IEEE80211_MPPID_MPM) {
3177 IEEE80211_DPRINTF(vap,
3178 IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH,
3179 "Only MPM protocol is supported (proto: 0x%02X)",
3180 meshpeer->peer_proto);
3184 case IEEE80211_ACTION_MESHPEERING_OPEN:
3185 if (meshpeer->peer_len != IEEE80211_MPM_BASE_SZ)
3188 case IEEE80211_ACTION_MESHPEERING_CONFIRM:
3189 if (meshpeer->peer_len != IEEE80211_MPM_BASE_SZ + 2)
3192 case IEEE80211_ACTION_MESHPEERING_CLOSE:
3193 if (meshpeer->peer_len < IEEE80211_MPM_BASE_SZ + 2)
3195 if (meshpeer->peer_len == (IEEE80211_MPM_BASE_SZ + 2) &&
3196 meshpeer->peer_linkid != 0)
3198 if (meshpeer->peer_rcode == 0)
3206 * Add a Mesh ID IE to a frame.
3209 ieee80211_add_meshid(uint8_t *frm, struct ieee80211vap *vap)
3211 struct ieee80211_mesh_state *ms = vap->iv_mesh;
3213 KASSERT(vap->iv_opmode == IEEE80211_M_MBSS, ("not a mbss vap"));
3215 *frm++ = IEEE80211_ELEMID_MESHID;
3216 *frm++ = ms->ms_idlen;
3217 memcpy(frm, ms->ms_id, ms->ms_idlen);
3218 return frm + ms->ms_idlen;
3222 * Add a Mesh Configuration IE to a frame.
3223 * For now just use HWMP routing, Airtime link metric, Null Congestion
3224 * Signaling, Null Sync Protocol and Null Authentication.
3227 ieee80211_add_meshconf(uint8_t *frm, struct ieee80211vap *vap)
3229 const struct ieee80211_mesh_state *ms = vap->iv_mesh;
3232 KASSERT(vap->iv_opmode == IEEE80211_M_MBSS, ("not a MBSS vap"));
3234 *frm++ = IEEE80211_ELEMID_MESHCONF;
3235 *frm++ = IEEE80211_MESH_CONF_SZ;
3236 *frm++ = ms->ms_ppath->mpp_ie; /* path selection */
3237 *frm++ = ms->ms_pmetric->mpm_ie; /* link metric */
3238 *frm++ = IEEE80211_MESHCONF_CC_DISABLED;
3239 *frm++ = IEEE80211_MESHCONF_SYNC_NEIGHOFF;
3240 *frm++ = IEEE80211_MESHCONF_AUTH_DISABLED;
3241 /* NB: set the number of neighbors before the rest */
3242 *frm = (ms->ms_neighbors > IEEE80211_MESH_MAX_NEIGHBORS ?
3243 IEEE80211_MESH_MAX_NEIGHBORS : ms->ms_neighbors) << 1;
3244 if (ms->ms_flags & IEEE80211_MESHFLAGS_GATE)
3245 *frm |= IEEE80211_MESHCONF_FORM_GATE;
3248 if (ms->ms_flags & IEEE80211_MESHFLAGS_AP)
3249 caps |= IEEE80211_MESHCONF_CAP_AP;
3250 if (ms->ms_flags & IEEE80211_MESHFLAGS_FWD)
3251 caps |= IEEE80211_MESHCONF_CAP_FWRD;
3257 * Add a Mesh Peer Management IE to a frame.
3260 ieee80211_add_meshpeer(uint8_t *frm, uint8_t subtype, uint16_t localid,
3261 uint16_t peerid, uint16_t reason)
3264 KASSERT(localid != 0, ("localid == 0"));
3266 *frm++ = IEEE80211_ELEMID_MESHPEER;
3268 case IEEE80211_ACTION_MESHPEERING_OPEN:
3269 *frm++ = IEEE80211_MPM_BASE_SZ; /* length */
3270 ADDSHORT(frm, IEEE80211_MPPID_MPM); /* proto */
3271 ADDSHORT(frm, localid); /* local ID */
3273 case IEEE80211_ACTION_MESHPEERING_CONFIRM:
3274 KASSERT(peerid != 0, ("sending peer confirm without peer id"));
3275 *frm++ = IEEE80211_MPM_BASE_SZ + 2; /* length */
3276 ADDSHORT(frm, IEEE80211_MPPID_MPM); /* proto */
3277 ADDSHORT(frm, localid); /* local ID */
3278 ADDSHORT(frm, peerid); /* peer ID */
3280 case IEEE80211_ACTION_MESHPEERING_CLOSE:
3282 *frm++ = IEEE80211_MPM_MAX_SZ; /* length */
3284 *frm++ = IEEE80211_MPM_BASE_SZ + 2; /* length */
3285 ADDSHORT(frm, IEEE80211_MPPID_MPM); /* proto */
3286 ADDSHORT(frm, localid); /* local ID */
3288 ADDSHORT(frm, peerid); /* peer ID */
3289 ADDSHORT(frm, reason);
3296 * Compute an Airtime Link Metric for the link with this node.
3298 * Based on Draft 3.0 spec (11B.10, p.149).
3301 * Max 802.11s overhead.
3303 #define IEEE80211_MESH_MAXOVERHEAD \
3304 (sizeof(struct ieee80211_qosframe_addr4) \
3305 + sizeof(struct ieee80211_meshcntl_ae10) \
3306 + sizeof(struct llc) \
3307 + IEEE80211_ADDR_LEN \
3308 + IEEE80211_WEP_IVLEN \
3309 + IEEE80211_WEP_KIDLEN \
3310 + IEEE80211_WEP_CRCLEN \
3311 + IEEE80211_WEP_MICLEN \
3312 + IEEE80211_CRC_LEN)
3314 mesh_airtime_calc(struct ieee80211_node *ni)
3317 #define S_FACTOR (2 * M_BITS)
3318 struct ieee80211com *ic = ni->ni_ic;
3319 struct ifnet *ifp = ni->ni_vap->iv_ifp;
3320 static const int nbits = 8192 << M_BITS;
3321 uint32_t overhead, rate, errrate;
3324 /* Time to transmit a frame */
3325 rate = ni->ni_txrate;
3326 overhead = ieee80211_compute_duration(ic->ic_rt,
3327 ifp->if_mtu + IEEE80211_MESH_MAXOVERHEAD, rate, 0) << M_BITS;
3328 /* Error rate in percentage */
3329 /* XXX assuming small failures are ok */
3330 #if defined(__DragonFly__)
3333 IFNET_STAT_GET(ifp, ierrors, icount);
3334 IFNET_STAT_GET(ifp, oerrors, ocount);
3335 errrate = (((ocount + icount) / 100) << M_BITS)
3338 errrate = (((ifp->if_get_counter(ifp, IFCOUNTER_OERRORS) +
3339 ifp->if_get_counter(ifp, IFCOUNTER_IERRORS)) / 100) << M_BITS)
3342 res = (overhead + (nbits / rate)) *
3343 ((1 << S_FACTOR) / ((1 << M_BITS) - errrate));
3345 return (uint32_t)(res >> S_FACTOR);
3351 * Add a Mesh Link Metric report IE to a frame.
3354 ieee80211_add_meshlmetric(uint8_t *frm, uint8_t flags, uint32_t metric)
3356 *frm++ = IEEE80211_ELEMID_MESHLINK;
3359 ADDWORD(frm, metric);
3364 * Add a Mesh Gate Announcement IE to a frame.
3367 ieee80211_add_meshgate(uint8_t *frm, struct ieee80211_meshgann_ie *ie)
3369 *frm++ = IEEE80211_ELEMID_MESHGANN; /* ie */
3370 *frm++ = IEEE80211_MESHGANN_BASE_SZ; /* len */
3371 *frm++ = ie->gann_flags;
3372 *frm++ = ie->gann_hopcount;
3373 *frm++ = ie->gann_ttl;
3374 IEEE80211_ADDR_COPY(frm, ie->gann_addr);
3376 ADDWORD(frm, ie->gann_seq);
3377 ADDSHORT(frm, ie->gann_interval);
3384 * Initialize any mesh-specific node state.
3387 ieee80211_mesh_node_init(struct ieee80211vap *vap, struct ieee80211_node *ni)
3389 ni->ni_flags |= IEEE80211_NODE_QOS;
3390 callout_init_mp(&ni->ni_mltimer);
3391 callout_init_mp(&ni->ni_mlhtimer);
3395 * Cleanup any mesh-specific node state.
3398 ieee80211_mesh_node_cleanup(struct ieee80211_node *ni)
3400 struct ieee80211vap *vap = ni->ni_vap;
3401 struct ieee80211_mesh_state *ms = vap->iv_mesh;
3403 callout_drain(&ni->ni_mltimer);
3404 callout_drain(&ni->ni_mlhtimer);
3405 /* NB: short-circuit callbacks after mesh_vdetach */
3406 if (vap->iv_mesh != NULL)
3407 ms->ms_ppath->mpp_peerdown(ni);
3411 ieee80211_parse_meshid(struct ieee80211_node *ni, const uint8_t *ie)
3413 ni->ni_meshidlen = ie[1];
3414 memcpy(ni->ni_meshid, ie + 2, ie[1]);
3418 * Setup mesh-specific node state on neighbor discovery.
3421 ieee80211_mesh_init_neighbor(struct ieee80211_node *ni,
3422 const struct ieee80211_frame *wh,
3423 const struct ieee80211_scanparams *sp)
3425 ieee80211_parse_meshid(ni, sp->meshid);
3429 ieee80211_mesh_update_beacon(struct ieee80211vap *vap,
3430 struct ieee80211_beacon_offsets *bo)
3432 KASSERT(vap->iv_opmode == IEEE80211_M_MBSS, ("not a MBSS vap"));
3434 if (isset(bo->bo_flags, IEEE80211_BEACON_MESHCONF)) {
3435 (void)ieee80211_add_meshconf(bo->bo_meshconf, vap);
3436 clrbit(bo->bo_flags, IEEE80211_BEACON_MESHCONF);
3441 mesh_ioctl_get80211(struct ieee80211vap *vap, struct ieee80211req *ireq)
3443 struct ieee80211_mesh_state *ms = vap->iv_mesh;
3444 uint8_t tmpmeshid[IEEE80211_NWID_LEN];
3445 struct ieee80211_mesh_route *rt;
3446 struct ieee80211req_mesh_route *imr;
3451 if (vap->iv_opmode != IEEE80211_M_MBSS)
3455 switch (ireq->i_type) {
3456 case IEEE80211_IOC_MESH_ID:
3457 ireq->i_len = ms->ms_idlen;
3458 memcpy(tmpmeshid, ms->ms_id, ireq->i_len);
3459 error = copyout(tmpmeshid, ireq->i_data, ireq->i_len);
3461 case IEEE80211_IOC_MESH_AP:
3462 ireq->i_val = (ms->ms_flags & IEEE80211_MESHFLAGS_AP) != 0;
3464 case IEEE80211_IOC_MESH_FWRD:
3465 ireq->i_val = (ms->ms_flags & IEEE80211_MESHFLAGS_FWD) != 0;
3467 case IEEE80211_IOC_MESH_GATE:
3468 ireq->i_val = (ms->ms_flags & IEEE80211_MESHFLAGS_GATE) != 0;
3470 case IEEE80211_IOC_MESH_TTL:
3471 ireq->i_val = ms->ms_ttl;
3473 case IEEE80211_IOC_MESH_RTCMD:
3474 switch (ireq->i_val) {
3475 case IEEE80211_MESH_RTCMD_LIST:
3478 TAILQ_FOREACH(rt, &ms->ms_routes, rt_next) {
3479 len += sizeof(*imr);
3482 if (len > ireq->i_len || ireq->i_len < sizeof(*imr)) {
3488 p = kmalloc(len, M_TEMP, M_INTWAIT | M_ZERO);
3493 TAILQ_FOREACH(rt, &ms->ms_routes, rt_next) {
3496 imr = (struct ieee80211req_mesh_route *)
3498 IEEE80211_ADDR_COPY(imr->imr_dest,
3500 IEEE80211_ADDR_COPY(imr->imr_nexthop,
3502 imr->imr_metric = rt->rt_metric;
3503 imr->imr_nhops = rt->rt_nhops;
3505 ieee80211_mesh_rt_update(rt, 0);
3506 imr->imr_lastmseq = rt->rt_lastmseq;
3507 imr->imr_flags = rt->rt_flags; /* last */
3508 off += sizeof(*imr);
3511 error = copyout(p, (uint8_t *)ireq->i_data,
3515 case IEEE80211_MESH_RTCMD_FLUSH:
3516 case IEEE80211_MESH_RTCMD_ADD:
3517 case IEEE80211_MESH_RTCMD_DELETE:
3523 case IEEE80211_IOC_MESH_PR_METRIC:
3524 len = strlen(ms->ms_pmetric->mpm_descr);
3525 if (ireq->i_len < len)
3528 error = copyout(ms->ms_pmetric->mpm_descr,
3529 (uint8_t *)ireq->i_data, len);
3531 case IEEE80211_IOC_MESH_PR_PATH:
3532 len = strlen(ms->ms_ppath->mpp_descr);
3533 if (ireq->i_len < len)
3536 error = copyout(ms->ms_ppath->mpp_descr,
3537 (uint8_t *)ireq->i_data, len);
3545 IEEE80211_IOCTL_GET(mesh, mesh_ioctl_get80211);
3548 mesh_ioctl_set80211(struct ieee80211vap *vap, struct ieee80211req *ireq)
3550 struct ieee80211_mesh_state *ms = vap->iv_mesh;
3551 uint8_t tmpmeshid[IEEE80211_NWID_LEN];
3552 uint8_t tmpaddr[IEEE80211_ADDR_LEN];
3553 char tmpproto[IEEE80211_MESH_PROTO_DSZ];
3556 if (vap->iv_opmode != IEEE80211_M_MBSS)
3560 switch (ireq->i_type) {
3561 case IEEE80211_IOC_MESH_ID:
3562 if (ireq->i_val != 0 || ireq->i_len > IEEE80211_MESHID_LEN)
3564 error = copyin(ireq->i_data, tmpmeshid, ireq->i_len);
3567 memset(ms->ms_id, 0, IEEE80211_NWID_LEN);
3568 ms->ms_idlen = ireq->i_len;
3569 memcpy(ms->ms_id, tmpmeshid, ireq->i_len);
3572 case IEEE80211_IOC_MESH_AP:
3574 ms->ms_flags |= IEEE80211_MESHFLAGS_AP;
3576 ms->ms_flags &= ~IEEE80211_MESHFLAGS_AP;
3579 case IEEE80211_IOC_MESH_FWRD:
3581 ms->ms_flags |= IEEE80211_MESHFLAGS_FWD;
3583 ms->ms_flags &= ~IEEE80211_MESHFLAGS_FWD;
3584 mesh_gatemode_setup(vap);
3586 case IEEE80211_IOC_MESH_GATE:
3588 ms->ms_flags |= IEEE80211_MESHFLAGS_GATE;
3590 ms->ms_flags &= ~IEEE80211_MESHFLAGS_GATE;
3592 case IEEE80211_IOC_MESH_TTL:
3593 ms->ms_ttl = (uint8_t) ireq->i_val;
3595 case IEEE80211_IOC_MESH_RTCMD:
3596 switch (ireq->i_val) {
3597 case IEEE80211_MESH_RTCMD_LIST:
3599 case IEEE80211_MESH_RTCMD_FLUSH:
3600 ieee80211_mesh_rt_flush(vap);
3602 case IEEE80211_MESH_RTCMD_ADD:
3603 if (IEEE80211_ADDR_EQ(vap->iv_myaddr, ireq->i_data) ||
3604 IEEE80211_ADDR_EQ(broadcastaddr, ireq->i_data))
3606 error = copyin(ireq->i_data, &tmpaddr,
3607 IEEE80211_ADDR_LEN);
3609 ieee80211_mesh_discover(vap, tmpaddr, NULL);
3611 case IEEE80211_MESH_RTCMD_DELETE:
3612 ieee80211_mesh_rt_del(vap, ireq->i_data);
3618 case IEEE80211_IOC_MESH_PR_METRIC:
3619 error = copyin(ireq->i_data, tmpproto, sizeof(tmpproto));
3621 error = mesh_select_proto_metric(vap, tmpproto);
3626 case IEEE80211_IOC_MESH_PR_PATH:
3627 error = copyin(ireq->i_data, tmpproto, sizeof(tmpproto));
3629 error = mesh_select_proto_path(vap, tmpproto);
3639 IEEE80211_IOCTL_SET(mesh, mesh_ioctl_set80211);