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>
56 #include <net/if_media.h>
57 #include <net/if_llc.h>
58 #include <net/ethernet.h>
60 #include <net80211/ieee80211_var.h>
61 #include <net80211/ieee80211_action.h>
62 #include <net80211/ieee80211_input.h>
63 #include <net80211/ieee80211_mesh.h>
65 static void mesh_rt_flush_invalid(struct ieee80211vap *);
66 static int mesh_select_proto_path(struct ieee80211vap *, const char *);
67 static int mesh_select_proto_metric(struct ieee80211vap *, const char *);
68 static void mesh_vattach(struct ieee80211vap *);
69 static int mesh_newstate(struct ieee80211vap *, enum ieee80211_state, int);
70 static void mesh_rt_cleanup_cb(void *);
71 static void mesh_linkchange(struct ieee80211_node *,
72 enum ieee80211_mesh_mlstate);
73 static void mesh_checkid(void *, struct ieee80211_node *);
74 static uint32_t mesh_generateid(struct ieee80211vap *);
75 static int mesh_checkpseq(struct ieee80211vap *,
76 const uint8_t [IEEE80211_ADDR_LEN], uint32_t);
77 static struct ieee80211_node *
78 mesh_find_txnode(struct ieee80211vap *,
79 const uint8_t [IEEE80211_ADDR_LEN]);
80 static void mesh_forward(struct ieee80211vap *, struct mbuf *,
81 const struct ieee80211_meshcntl *);
82 static int mesh_input(struct ieee80211_node *, struct mbuf *, int, int);
83 static void mesh_recv_mgmt(struct ieee80211_node *, struct mbuf *, int,
85 static void mesh_peer_timeout_setup(struct ieee80211_node *);
86 static void mesh_peer_timeout_backoff(struct ieee80211_node *);
87 static void mesh_peer_timeout_cb(void *);
89 mesh_peer_timeout_stop(struct ieee80211_node *);
90 static int mesh_verify_meshpeerver(struct ieee80211vap *, const uint8_t *);
91 static int mesh_verify_meshid(struct ieee80211vap *, const uint8_t *);
92 static int mesh_verify_meshconf(struct ieee80211vap *, const uint8_t *);
93 static int mesh_verify_meshpeer(struct ieee80211vap *, const uint8_t *);
94 uint32_t mesh_airtime_calc(struct ieee80211_node *);
97 * Timeout values come from the specification and are in milliseconds.
99 SYSCTL_NODE(_net_wlan, OID_AUTO, mesh, CTLFLAG_RD, 0,
100 "IEEE 802.11s parameters");
101 static int ieee80211_mesh_retrytimeout = -1;
102 SYSCTL_PROC(_net_wlan_mesh, OID_AUTO, retrytimeout, CTLTYPE_INT | CTLFLAG_RW,
103 &ieee80211_mesh_retrytimeout, 0, ieee80211_sysctl_msecs_ticks, "I",
104 "Retry timeout (msec)");
105 static int ieee80211_mesh_holdingtimeout = -1;
106 SYSCTL_PROC(_net_wlan_mesh, OID_AUTO, holdingtimeout, CTLTYPE_INT | CTLFLAG_RW,
107 &ieee80211_mesh_holdingtimeout, 0, ieee80211_sysctl_msecs_ticks, "I",
108 "Holding state timeout (msec)");
109 static int ieee80211_mesh_confirmtimeout = -1;
110 SYSCTL_PROC(_net_wlan_mesh, OID_AUTO, confirmtimeout, CTLTYPE_INT | CTLFLAG_RW,
111 &ieee80211_mesh_confirmtimeout, 0, ieee80211_sysctl_msecs_ticks, "I",
112 "Confirm state timeout (msec)");
113 static int ieee80211_mesh_maxretries = 2;
114 SYSCTL_INT(_net_wlan_mesh, OID_AUTO, maxretries, CTLTYPE_INT | CTLFLAG_RW,
115 &ieee80211_mesh_maxretries, 0,
116 "Maximum retries during peer link establishment");
118 static const uint8_t broadcastaddr[IEEE80211_ADDR_LEN] =
119 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
121 static ieee80211_recv_action_func mesh_recv_action_meshpeering_open;
122 static ieee80211_recv_action_func mesh_recv_action_meshpeering_confirm;
123 static ieee80211_recv_action_func mesh_recv_action_meshpeering_close;
124 static ieee80211_recv_action_func mesh_recv_action_meshlmetric_req;
125 static ieee80211_recv_action_func mesh_recv_action_meshlmetric_rep;
127 static ieee80211_send_action_func mesh_send_action_meshpeering_open;
128 static ieee80211_send_action_func mesh_send_action_meshpeering_confirm;
129 static ieee80211_send_action_func mesh_send_action_meshpeering_close;
130 static ieee80211_send_action_func mesh_send_action_meshlink_request;
131 static ieee80211_send_action_func mesh_send_action_meshlink_reply;
133 static const struct ieee80211_mesh_proto_metric mesh_metric_airtime = {
134 .mpm_descr = "AIRTIME",
135 .mpm_ie = IEEE80211_MESHCONF_AIRTIME,
136 .mpm_metric = mesh_airtime_calc,
139 static struct ieee80211_mesh_proto_path mesh_proto_paths[4];
140 static struct ieee80211_mesh_proto_metric mesh_proto_metrics[4];
142 #define MESH_RT_LOCK(ms) mtx_lock(&(ms)->ms_rt_lock)
143 #define MESH_RT_LOCK_ASSERT(ms) mtx_assert(&(ms)->ms_rt_lock, MA_OWNED)
144 #define MESH_RT_UNLOCK(ms) mtx_unlock(&(ms)->ms_rt_lock)
146 MALLOC_DEFINE(M_80211_MESH_RT, "80211mesh", "802.11s routing table");
149 * Helper functions to manipulate the Mesh routing table.
152 static struct ieee80211_mesh_route *
153 mesh_rt_find_locked(struct ieee80211_mesh_state *ms,
154 const uint8_t dest[IEEE80211_ADDR_LEN])
156 struct ieee80211_mesh_route *rt;
158 MESH_RT_LOCK_ASSERT(ms);
160 TAILQ_FOREACH(rt, &ms->ms_routes, rt_next) {
161 if (IEEE80211_ADDR_EQ(dest, rt->rt_dest))
167 static struct ieee80211_mesh_route *
168 mesh_rt_add_locked(struct ieee80211_mesh_state *ms,
169 const uint8_t dest[IEEE80211_ADDR_LEN])
171 struct ieee80211_mesh_route *rt;
173 KASSERT(!IEEE80211_ADDR_EQ(broadcastaddr, dest),
174 ("%s: adding broadcast to the routing table", __func__));
176 MESH_RT_LOCK_ASSERT(ms);
178 rt = malloc(ALIGN(sizeof(struct ieee80211_mesh_route)) +
179 ms->ms_ppath->mpp_privlen, M_80211_MESH_RT, M_NOWAIT | M_ZERO);
181 IEEE80211_ADDR_COPY(rt->rt_dest, dest);
182 rt->rt_priv = (void *)ALIGN(&rt[1]);
183 rt->rt_crtime = ticks;
184 TAILQ_INSERT_TAIL(&ms->ms_routes, rt, rt_next);
189 struct ieee80211_mesh_route *
190 ieee80211_mesh_rt_find(struct ieee80211vap *vap,
191 const uint8_t dest[IEEE80211_ADDR_LEN])
193 struct ieee80211_mesh_state *ms = vap->iv_mesh;
194 struct ieee80211_mesh_route *rt;
197 rt = mesh_rt_find_locked(ms, dest);
202 struct ieee80211_mesh_route *
203 ieee80211_mesh_rt_add(struct ieee80211vap *vap,
204 const uint8_t dest[IEEE80211_ADDR_LEN])
206 struct ieee80211_mesh_state *ms = vap->iv_mesh;
207 struct ieee80211_mesh_route *rt;
209 KASSERT(ieee80211_mesh_rt_find(vap, dest) == NULL,
210 ("%s: duplicate entry in the routing table", __func__));
211 KASSERT(!IEEE80211_ADDR_EQ(vap->iv_myaddr, dest),
212 ("%s: adding self to the routing table", __func__));
215 rt = mesh_rt_add_locked(ms, dest);
221 * Add a proxy route (as needed) for the specified destination.
224 ieee80211_mesh_proxy_check(struct ieee80211vap *vap,
225 const uint8_t dest[IEEE80211_ADDR_LEN])
227 struct ieee80211_mesh_state *ms = vap->iv_mesh;
228 struct ieee80211_mesh_route *rt;
231 rt = mesh_rt_find_locked(ms, dest);
233 rt = mesh_rt_add_locked(ms, dest);
235 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH, dest,
236 "%s", "unable to add proxy entry");
237 vap->iv_stats.is_mesh_rtaddfailed++;
239 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH, dest,
240 "%s", "add proxy entry");
241 IEEE80211_ADDR_COPY(rt->rt_nexthop, vap->iv_myaddr);
242 rt->rt_flags |= IEEE80211_MESHRT_FLAGS_VALID
243 | IEEE80211_MESHRT_FLAGS_PROXY;
245 /* XXX assert PROXY? */
246 } else if ((rt->rt_flags & IEEE80211_MESHRT_FLAGS_VALID) == 0) {
247 struct ieee80211com *ic = vap->iv_ic;
249 * Fix existing entry created by received frames from
250 * stations that have some memory of dest. We also
251 * flush any frames held on the staging queue; delivering
252 * them is too much trouble right now.
254 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH, dest,
255 "%s", "fix proxy entry");
256 IEEE80211_ADDR_COPY(rt->rt_nexthop, vap->iv_myaddr);
257 rt->rt_flags |= IEEE80211_MESHRT_FLAGS_VALID
258 | IEEE80211_MESHRT_FLAGS_PROXY;
259 /* XXX belongs in hwmp */
260 ieee80211_ageq_drain_node(&ic->ic_stageq,
261 (void *)(uintptr_t) ieee80211_mac_hash(ic, dest));
268 mesh_rt_del(struct ieee80211_mesh_state *ms, struct ieee80211_mesh_route *rt)
270 TAILQ_REMOVE(&ms->ms_routes, rt, rt_next);
271 free(rt, M_80211_MESH_RT);
275 ieee80211_mesh_rt_del(struct ieee80211vap *vap,
276 const uint8_t dest[IEEE80211_ADDR_LEN])
278 struct ieee80211_mesh_state *ms = vap->iv_mesh;
279 struct ieee80211_mesh_route *rt, *next;
282 TAILQ_FOREACH_SAFE(rt, &ms->ms_routes, rt_next, next) {
283 if (IEEE80211_ADDR_EQ(rt->rt_dest, dest)) {
293 ieee80211_mesh_rt_flush(struct ieee80211vap *vap)
295 struct ieee80211_mesh_state *ms = vap->iv_mesh;
296 struct ieee80211_mesh_route *rt, *next;
301 TAILQ_FOREACH_SAFE(rt, &ms->ms_routes, rt_next, next)
307 ieee80211_mesh_rt_flush_peer(struct ieee80211vap *vap,
308 const uint8_t peer[IEEE80211_ADDR_LEN])
310 struct ieee80211_mesh_state *ms = vap->iv_mesh;
311 struct ieee80211_mesh_route *rt, *next;
314 TAILQ_FOREACH_SAFE(rt, &ms->ms_routes, rt_next, next) {
315 if (IEEE80211_ADDR_EQ(rt->rt_nexthop, peer))
322 * Flush expired routing entries, i.e. those in invalid state for
326 mesh_rt_flush_invalid(struct ieee80211vap *vap)
328 struct ieee80211_mesh_state *ms = vap->iv_mesh;
329 struct ieee80211_mesh_route *rt, *next;
334 TAILQ_FOREACH_SAFE(rt, &ms->ms_routes, rt_next, next) {
335 if ((rt->rt_flags & IEEE80211_MESHRT_FLAGS_VALID) == 0 &&
336 ticks - rt->rt_crtime >= ms->ms_ppath->mpp_inact)
342 #define N(a) (sizeof(a) / sizeof(a[0]))
344 ieee80211_mesh_register_proto_path(const struct ieee80211_mesh_proto_path *mpp)
346 int i, firstempty = -1;
347 static const uint8_t emptyie[4] = { 0, 0, 0, 0 };
349 for (i = 0; i < N(mesh_proto_paths); i++) {
350 if (memcmp(mpp->mpp_ie, mesh_proto_paths[i].mpp_ie, 4) == 0)
352 if (memcmp(mesh_proto_paths[i].mpp_ie, emptyie, 4) == 0 &&
358 memcpy(&mesh_proto_paths[firstempty], mpp, sizeof(*mpp));
363 ieee80211_mesh_register_proto_metric(const struct
364 ieee80211_mesh_proto_metric *mpm)
366 int i, firstempty = -1;
367 static const uint8_t emptyie[4] = { 0, 0, 0, 0 };
369 for (i = 0; i < N(mesh_proto_metrics); i++) {
370 if (memcmp(mpm->mpm_ie, mesh_proto_metrics[i].mpm_ie, 4) == 0)
372 if (memcmp(mesh_proto_metrics[i].mpm_ie, emptyie, 4) == 0 &&
378 memcpy(&mesh_proto_metrics[firstempty], mpm, sizeof(*mpm));
383 mesh_select_proto_path(struct ieee80211vap *vap, const char *name)
385 struct ieee80211_mesh_state *ms = vap->iv_mesh;
388 for (i = 0; i < N(mesh_proto_paths); i++) {
389 if (strcasecmp(mesh_proto_paths[i].mpp_descr, name) == 0) {
390 ms->ms_ppath = &mesh_proto_paths[i];
391 if (vap->iv_state == IEEE80211_S_RUN)
392 vap->iv_newstate(vap, IEEE80211_S_INIT, 0);
400 mesh_select_proto_metric(struct ieee80211vap *vap, const char *name)
402 struct ieee80211_mesh_state *ms = vap->iv_mesh;
405 for (i = 0; i < N(mesh_proto_metrics); i++) {
406 if (strcasecmp(mesh_proto_metrics[i].mpm_descr, name) == 0) {
407 ms->ms_pmetric = &mesh_proto_metrics[i];
408 if (vap->iv_state == IEEE80211_S_RUN)
409 vap->iv_newstate(vap, IEEE80211_S_INIT, 0);
418 ieee80211_mesh_init(void)
421 memset(mesh_proto_paths, 0, sizeof(mesh_proto_paths));
422 memset(mesh_proto_metrics, 0, sizeof(mesh_proto_metrics));
425 * Setup mesh parameters that depends on the clock frequency.
427 ieee80211_mesh_retrytimeout = msecs_to_ticks(40);
428 ieee80211_mesh_holdingtimeout = msecs_to_ticks(40);
429 ieee80211_mesh_confirmtimeout = msecs_to_ticks(40);
432 * Register action frame handlers.
434 ieee80211_recv_action_register(IEEE80211_ACTION_CAT_MESHPEERING,
435 IEEE80211_ACTION_MESHPEERING_OPEN,
436 mesh_recv_action_meshpeering_open);
437 ieee80211_recv_action_register(IEEE80211_ACTION_CAT_MESHPEERING,
438 IEEE80211_ACTION_MESHPEERING_CONFIRM,
439 mesh_recv_action_meshpeering_confirm);
440 ieee80211_recv_action_register(IEEE80211_ACTION_CAT_MESHPEERING,
441 IEEE80211_ACTION_MESHPEERING_CLOSE,
442 mesh_recv_action_meshpeering_close);
443 ieee80211_recv_action_register(IEEE80211_ACTION_CAT_MESHLMETRIC,
444 IEEE80211_ACTION_MESHLMETRIC_REQ, mesh_recv_action_meshlmetric_req);
445 ieee80211_recv_action_register(IEEE80211_ACTION_CAT_MESHLMETRIC,
446 IEEE80211_ACTION_MESHLMETRIC_REP, mesh_recv_action_meshlmetric_rep);
448 ieee80211_send_action_register(IEEE80211_ACTION_CAT_MESHPEERING,
449 IEEE80211_ACTION_MESHPEERING_OPEN,
450 mesh_send_action_meshpeering_open);
451 ieee80211_send_action_register(IEEE80211_ACTION_CAT_MESHPEERING,
452 IEEE80211_ACTION_MESHPEERING_CONFIRM,
453 mesh_send_action_meshpeering_confirm);
454 ieee80211_send_action_register(IEEE80211_ACTION_CAT_MESHPEERING,
455 IEEE80211_ACTION_MESHPEERING_CLOSE,
456 mesh_send_action_meshpeering_close);
457 ieee80211_send_action_register(IEEE80211_ACTION_CAT_MESHLMETRIC,
458 IEEE80211_ACTION_MESHLMETRIC_REQ,
459 mesh_send_action_meshlink_request);
460 ieee80211_send_action_register(IEEE80211_ACTION_CAT_MESHLMETRIC,
461 IEEE80211_ACTION_MESHLMETRIC_REP,
462 mesh_send_action_meshlink_reply);
465 * Register Airtime Link Metric.
467 ieee80211_mesh_register_proto_metric(&mesh_metric_airtime);
470 SYSINIT(wlan_mesh, SI_SUB_DRIVERS, SI_ORDER_FIRST, ieee80211_mesh_init, NULL);
473 ieee80211_mesh_attach(struct ieee80211com *ic)
475 ic->ic_vattach[IEEE80211_M_MBSS] = mesh_vattach;
479 ieee80211_mesh_detach(struct ieee80211com *ic)
484 mesh_vdetach_peers(void *arg, struct ieee80211_node *ni)
486 struct ieee80211com *ic = ni->ni_ic;
489 if (ni->ni_mlstate == IEEE80211_NODE_MESH_ESTABLISHED) {
490 args[0] = ni->ni_mlpid;
491 args[1] = ni->ni_mllid;
492 args[2] = IEEE80211_REASON_PEER_LINK_CANCELED;
493 ieee80211_send_action(ni,
494 IEEE80211_ACTION_CAT_MESHPEERING,
495 IEEE80211_ACTION_MESHPEERING_CLOSE,
498 callout_drain(&ni->ni_mltimer);
499 /* XXX belongs in hwmp */
500 ieee80211_ageq_drain_node(&ic->ic_stageq,
501 (void *)(uintptr_t) ieee80211_mac_hash(ic, ni->ni_macaddr));
505 mesh_vdetach(struct ieee80211vap *vap)
507 struct ieee80211_mesh_state *ms = vap->iv_mesh;
509 callout_drain(&ms->ms_cleantimer);
510 ieee80211_iterate_nodes(&vap->iv_ic->ic_sta, mesh_vdetach_peers,
512 ieee80211_mesh_rt_flush(vap);
513 mtx_destroy(&ms->ms_rt_lock);
514 ms->ms_ppath->mpp_vdetach(vap);
515 free(vap->iv_mesh, M_80211_VAP);
520 mesh_vattach(struct ieee80211vap *vap)
522 struct ieee80211_mesh_state *ms;
523 vap->iv_newstate = mesh_newstate;
524 vap->iv_input = mesh_input;
525 vap->iv_opdetach = mesh_vdetach;
526 vap->iv_recv_mgmt = mesh_recv_mgmt;
527 ms = malloc(sizeof(struct ieee80211_mesh_state), M_80211_VAP,
530 printf("%s: couldn't alloc MBSS state\n", __func__);
535 ms->ms_flags = (IEEE80211_MESHFLAGS_AP | IEEE80211_MESHFLAGS_FWD);
536 ms->ms_ttl = IEEE80211_MESH_DEFAULT_TTL;
537 TAILQ_INIT(&ms->ms_routes);
538 mtx_init(&ms->ms_rt_lock, "MBSS", "802.11s routing table", MTX_DEF);
539 callout_init(&ms->ms_cleantimer, CALLOUT_MPSAFE);
540 mesh_select_proto_metric(vap, "AIRTIME");
541 KASSERT(ms->ms_pmetric, ("ms_pmetric == NULL"));
542 mesh_select_proto_path(vap, "HWMP");
543 KASSERT(ms->ms_ppath, ("ms_ppath == NULL"));
544 ms->ms_ppath->mpp_vattach(vap);
548 * IEEE80211_M_MBSS vap state machine handler.
551 mesh_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
553 struct ieee80211_mesh_state *ms = vap->iv_mesh;
554 struct ieee80211com *ic = vap->iv_ic;
555 struct ieee80211_node *ni;
556 enum ieee80211_state ostate;
558 IEEE80211_LOCK_ASSERT(ic);
560 ostate = vap->iv_state;
561 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE, "%s: %s -> %s (%d)\n",
562 __func__, ieee80211_state_name[ostate],
563 ieee80211_state_name[nstate], arg);
564 vap->iv_state = nstate; /* state transition */
565 if (ostate != IEEE80211_S_SCAN)
566 ieee80211_cancel_scan(vap); /* background scan */
567 ni = vap->iv_bss; /* NB: no reference held */
568 if (nstate != IEEE80211_S_RUN && ostate == IEEE80211_S_RUN)
569 callout_drain(&ms->ms_cleantimer);
571 case IEEE80211_S_INIT:
573 case IEEE80211_S_SCAN:
574 ieee80211_cancel_scan(vap);
576 case IEEE80211_S_CAC:
577 ieee80211_dfs_cac_stop(vap);
579 case IEEE80211_S_RUN:
580 ieee80211_iterate_nodes(&ic->ic_sta,
581 mesh_vdetach_peers, NULL);
586 if (ostate != IEEE80211_S_INIT) {
587 /* NB: optimize INIT -> INIT case */
588 ieee80211_reset_bss(vap);
589 ieee80211_mesh_rt_flush(vap);
592 case IEEE80211_S_SCAN:
594 case IEEE80211_S_INIT:
595 if (vap->iv_des_chan != IEEE80211_CHAN_ANYC &&
596 !IEEE80211_IS_CHAN_RADAR(vap->iv_des_chan) &&
599 * Already have a channel and a mesh ID; bypass
600 * the scan and startup immediately.
602 ieee80211_create_ibss(vap, vap->iv_des_chan);
606 * Initiate a scan. We can come here as a result
607 * of an IEEE80211_IOC_SCAN_REQ too in which case
608 * the vap will be marked with IEEE80211_FEXT_SCANREQ
609 * and the scan request parameters will be present
610 * in iv_scanreq. Otherwise we do the default.
612 if (vap->iv_flags_ext & IEEE80211_FEXT_SCANREQ) {
613 ieee80211_check_scan(vap,
614 vap->iv_scanreq_flags,
615 vap->iv_scanreq_duration,
616 vap->iv_scanreq_mindwell,
617 vap->iv_scanreq_maxdwell,
618 vap->iv_scanreq_nssid, vap->iv_scanreq_ssid);
619 vap->iv_flags_ext &= ~IEEE80211_FEXT_SCANREQ;
621 ieee80211_check_scan_current(vap);
627 case IEEE80211_S_CAC:
629 * Start CAC on a DFS channel. We come here when starting
630 * a bss on a DFS channel (see ieee80211_create_ibss).
632 ieee80211_dfs_cac_start(vap);
634 case IEEE80211_S_RUN:
636 case IEEE80211_S_INIT:
638 * Already have a channel; bypass the
639 * scan and startup immediately.
640 * Note that ieee80211_create_ibss will call
641 * back to do a RUN->RUN state change.
643 ieee80211_create_ibss(vap,
644 ieee80211_ht_adjust_channel(ic,
645 ic->ic_curchan, vap->iv_flags_ht));
646 /* NB: iv_bss is changed on return */
648 case IEEE80211_S_CAC:
650 * NB: This is the normal state change when CAC
651 * expires and no radar was detected; no need to
652 * clear the CAC timer as it's already expired.
655 case IEEE80211_S_CSA:
658 * Shorten inactivity timer of associated stations
659 * to weed out sta's that don't follow a CSA.
661 ieee80211_iterate_nodes(&ic->ic_sta, sta_csa, vap);
664 * Update bss node channel to reflect where
665 * we landed after CSA.
667 ieee80211_node_set_chan(vap->iv_bss,
668 ieee80211_ht_adjust_channel(ic, ic->ic_curchan,
669 ieee80211_htchanflags(vap->iv_bss->ni_chan)));
670 /* XXX bypass debug msgs */
672 case IEEE80211_S_SCAN:
673 case IEEE80211_S_RUN:
674 #ifdef IEEE80211_DEBUG
675 if (ieee80211_msg_debug(vap)) {
676 struct ieee80211_node *ni = vap->iv_bss;
678 "synchronized with %s meshid ",
679 ether_sprintf(ni->ni_meshid));
680 ieee80211_print_essid(ni->ni_meshid,
683 printf(" channel %d\n",
684 ieee80211_chan2ieee(ic, ic->ic_curchan));
691 ieee80211_node_authorize(vap->iv_bss);
692 callout_reset(&ms->ms_cleantimer, ms->ms_ppath->mpp_inact,
693 mesh_rt_cleanup_cb, vap);
698 /* NB: ostate not nstate */
699 ms->ms_ppath->mpp_newstate(vap, ostate, arg);
704 mesh_rt_cleanup_cb(void *arg)
706 struct ieee80211vap *vap = arg;
707 struct ieee80211_mesh_state *ms = vap->iv_mesh;
709 mesh_rt_flush_invalid(vap);
710 callout_reset(&ms->ms_cleantimer, ms->ms_ppath->mpp_inact,
711 mesh_rt_cleanup_cb, vap);
716 * Helper function to note the Mesh Peer Link FSM change.
719 mesh_linkchange(struct ieee80211_node *ni, enum ieee80211_mesh_mlstate state)
721 struct ieee80211vap *vap = ni->ni_vap;
722 struct ieee80211_mesh_state *ms = vap->iv_mesh;
723 #ifdef IEEE80211_DEBUG
724 static const char *meshlinkstates[] = {
725 [IEEE80211_NODE_MESH_IDLE] = "IDLE",
726 [IEEE80211_NODE_MESH_OPENSNT] = "OPEN SENT",
727 [IEEE80211_NODE_MESH_OPENRCV] = "OPEN RECEIVED",
728 [IEEE80211_NODE_MESH_CONFIRMRCV] = "CONFIRM RECEIVED",
729 [IEEE80211_NODE_MESH_ESTABLISHED] = "ESTABLISHED",
730 [IEEE80211_NODE_MESH_HOLDING] = "HOLDING"
733 IEEE80211_NOTE(vap, IEEE80211_MSG_MESH,
734 ni, "peer link: %s -> %s",
735 meshlinkstates[ni->ni_mlstate], meshlinkstates[state]);
737 /* track neighbor count */
738 if (state == IEEE80211_NODE_MESH_ESTABLISHED &&
739 ni->ni_mlstate != IEEE80211_NODE_MESH_ESTABLISHED) {
740 KASSERT(ms->ms_neighbors < 65535, ("neighbor count overflow"));
742 } else if (ni->ni_mlstate == IEEE80211_NODE_MESH_ESTABLISHED &&
743 state != IEEE80211_NODE_MESH_ESTABLISHED) {
744 KASSERT(ms->ms_neighbors > 0, ("neighbor count 0"));
747 ni->ni_mlstate = state;
749 case IEEE80211_NODE_MESH_HOLDING:
750 ms->ms_ppath->mpp_peerdown(ni);
752 case IEEE80211_NODE_MESH_ESTABLISHED:
753 ieee80211_mesh_discover(vap, ni->ni_macaddr, NULL);
761 * Helper function to generate a unique local ID required for mesh
762 * peer establishment.
765 mesh_checkid(void *arg, struct ieee80211_node *ni)
769 if (*r == ni->ni_mllid)
770 *(uint16_t *)arg = 0;
774 mesh_generateid(struct ieee80211vap *vap)
780 get_random_bytes(&r, 2);
781 ieee80211_iterate_nodes(&vap->iv_ic->ic_sta, mesh_checkid, &r);
783 } while (r == 0 && maxiter > 0);
788 * Verifies if we already received this packet by checking its
790 * Returns 0 if the frame is to be accepted, 1 otherwise.
793 mesh_checkpseq(struct ieee80211vap *vap,
794 const uint8_t source[IEEE80211_ADDR_LEN], uint32_t seq)
796 struct ieee80211_mesh_route *rt;
798 rt = ieee80211_mesh_rt_find(vap, source);
800 rt = ieee80211_mesh_rt_add(vap, source);
802 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH, source,
803 "%s", "add mcast route failed");
804 vap->iv_stats.is_mesh_rtaddfailed++;
807 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH, source,
808 "add mcast route, mesh seqno %d", seq);
809 rt->rt_lastmseq = seq;
812 if (IEEE80211_MESH_SEQ_GEQ(rt->rt_lastmseq, seq)) {
815 rt->rt_lastmseq = seq;
821 * Iterate the routing table and locate the next hop.
823 static struct ieee80211_node *
824 mesh_find_txnode(struct ieee80211vap *vap,
825 const uint8_t dest[IEEE80211_ADDR_LEN])
827 struct ieee80211_mesh_route *rt;
829 rt = ieee80211_mesh_rt_find(vap, dest);
832 if ((rt->rt_flags & IEEE80211_MESHRT_FLAGS_VALID) == 0 ||
833 (rt->rt_flags & IEEE80211_MESHRT_FLAGS_PROXY)) {
834 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH, dest,
835 "%s: !valid or proxy, flags 0x%x", __func__, rt->rt_flags);
839 return ieee80211_find_txnode(vap, rt->rt_nexthop);
843 * Forward the specified frame.
844 * Decrement the TTL and set TA to our MAC address.
847 mesh_forward(struct ieee80211vap *vap, struct mbuf *m,
848 const struct ieee80211_meshcntl *mc)
850 struct ieee80211com *ic = vap->iv_ic;
851 struct ieee80211_mesh_state *ms = vap->iv_mesh;
852 struct ifnet *ifp = vap->iv_ifp;
853 struct ifnet *parent = ic->ic_ifp;
854 const struct ieee80211_frame *wh =
855 mtod(m, const struct ieee80211_frame *);
857 struct ieee80211_meshcntl *mccopy;
858 struct ieee80211_frame *whcopy;
859 struct ieee80211_node *ni;
862 if (mc->mc_ttl == 0) {
863 IEEE80211_NOTE_FRAME(vap, IEEE80211_MSG_MESH, wh,
864 "%s", "frame not fwd'd, ttl 0");
865 vap->iv_stats.is_mesh_fwd_ttl++;
868 if (!(ms->ms_flags & IEEE80211_MESHFLAGS_FWD)) {
869 IEEE80211_NOTE_FRAME(vap, IEEE80211_MSG_MESH, wh,
870 "%s", "frame not fwd'd, fwding disabled");
871 vap->iv_stats.is_mesh_fwd_disabled++;
874 mcopy = m_dup(m, M_DONTWAIT);
876 IEEE80211_NOTE_FRAME(vap, IEEE80211_MSG_MESH, wh,
877 "%s", "frame not fwd'd, cannot dup");
878 vap->iv_stats.is_mesh_fwd_nobuf++;
882 mcopy = m_pullup(mcopy, ieee80211_hdrspace(ic, wh) +
883 sizeof(struct ieee80211_meshcntl));
885 IEEE80211_NOTE_FRAME(vap, IEEE80211_MSG_MESH, wh,
886 "%s", "frame not fwd'd, too short");
887 vap->iv_stats.is_mesh_fwd_tooshort++;
892 whcopy = mtod(mcopy, struct ieee80211_frame *);
893 mccopy = (struct ieee80211_meshcntl *)
894 (mtod(mcopy, uint8_t *) + ieee80211_hdrspace(ic, wh));
895 /* XXX clear other bits? */
896 whcopy->i_fc[1] &= ~IEEE80211_FC1_RETRY;
897 IEEE80211_ADDR_COPY(whcopy->i_addr2, vap->iv_myaddr);
898 if (IEEE80211_IS_MULTICAST(wh->i_addr1)) {
899 ni = ieee80211_ref_node(vap->iv_bss);
900 mcopy->m_flags |= M_MCAST;
902 ni = mesh_find_txnode(vap, whcopy->i_addr3);
904 IEEE80211_NOTE_FRAME(vap, IEEE80211_MSG_MESH, wh,
905 "%s", "frame not fwd'd, no path");
906 vap->iv_stats.is_mesh_fwd_nopath++;
910 IEEE80211_ADDR_COPY(whcopy->i_addr1, ni->ni_macaddr);
912 KASSERT(mccopy->mc_ttl > 0, ("%s called with wrong ttl", __func__));
915 /* XXX calculate priority so drivers can find the tx queue */
916 M_WME_SETAC(mcopy, WME_AC_BE);
918 /* XXX do we know m_nextpkt is NULL? */
919 mcopy->m_pkthdr.rcvif = (void *) ni;
920 err = parent->if_transmit(parent, mcopy);
922 /* NB: IFQ_HANDOFF reclaims mbuf */
923 ieee80211_free_node(ni);
930 mesh_decap(struct ieee80211vap *vap, struct mbuf *m, int hdrlen, int meshdrlen)
932 #define WHDIR(wh) ((wh)->i_fc[1] & IEEE80211_FC1_DIR_MASK)
933 uint8_t b[sizeof(struct ieee80211_qosframe_addr4) +
934 sizeof(struct ieee80211_meshcntl_ae11)];
935 const struct ieee80211_qosframe_addr4 *wh;
936 const struct ieee80211_meshcntl_ae10 *mc;
937 struct ether_header *eh;
941 if (m->m_len < hdrlen + sizeof(*llc) &&
942 (m = m_pullup(m, hdrlen + sizeof(*llc))) == NULL) {
943 IEEE80211_DPRINTF(vap, IEEE80211_MSG_ANY,
944 "discard data frame: %s", "m_pullup failed");
945 vap->iv_stats.is_rx_tooshort++;
948 memcpy(b, mtod(m, caddr_t), hdrlen);
949 wh = (const struct ieee80211_qosframe_addr4 *)&b[0];
950 mc = (const struct ieee80211_meshcntl_ae10 *)&b[hdrlen - meshdrlen];
951 KASSERT(WHDIR(wh) == IEEE80211_FC1_DIR_FROMDS ||
952 WHDIR(wh) == IEEE80211_FC1_DIR_DSTODS,
953 ("bogus dir, fc 0x%x:0x%x", wh->i_fc[0], wh->i_fc[1]));
955 llc = (struct llc *)(mtod(m, caddr_t) + hdrlen);
956 if (llc->llc_dsap == LLC_SNAP_LSAP && llc->llc_ssap == LLC_SNAP_LSAP &&
957 llc->llc_control == LLC_UI && llc->llc_snap.org_code[0] == 0 &&
958 llc->llc_snap.org_code[1] == 0 && llc->llc_snap.org_code[2] == 0 &&
959 /* NB: preserve AppleTalk frames that have a native SNAP hdr */
960 !(llc->llc_snap.ether_type == htons(ETHERTYPE_AARP) ||
961 llc->llc_snap.ether_type == htons(ETHERTYPE_IPX))) {
962 m_adj(m, hdrlen + sizeof(struct llc) - sizeof(*eh));
965 m_adj(m, hdrlen - sizeof(*eh));
967 eh = mtod(m, struct ether_header *);
968 ae = mc->mc_flags & 3;
969 if (WHDIR(wh) == IEEE80211_FC1_DIR_FROMDS) {
970 IEEE80211_ADDR_COPY(eh->ether_dhost, wh->i_addr1);
972 IEEE80211_ADDR_COPY(eh->ether_shost, wh->i_addr3);
973 } else if (ae == 1) {
974 IEEE80211_ADDR_COPY(eh->ether_shost, mc->mc_addr4);
976 IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
977 (const struct ieee80211_frame *)wh, NULL,
979 vap->iv_stats.is_mesh_badae++;
985 IEEE80211_ADDR_COPY(eh->ether_dhost, wh->i_addr3);
986 IEEE80211_ADDR_COPY(eh->ether_shost, wh->i_addr4);
987 } else if (ae == 2) {
988 IEEE80211_ADDR_COPY(eh->ether_dhost, mc->mc_addr4);
989 IEEE80211_ADDR_COPY(eh->ether_shost, mc->mc_addr5);
991 IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
992 (const struct ieee80211_frame *)wh, NULL,
994 vap->iv_stats.is_mesh_badae++;
999 #ifdef ALIGNED_POINTER
1000 if (!ALIGNED_POINTER(mtod(m, caddr_t) + sizeof(*eh), uint32_t)) {
1001 m = ieee80211_realign(vap, m, sizeof(*eh));
1005 #endif /* ALIGNED_POINTER */
1007 eh = mtod(m, struct ether_header *);
1008 eh->ether_type = htons(m->m_pkthdr.len - sizeof(*eh));
1015 * Return non-zero if the unicast mesh data frame should be processed
1016 * locally. Frames that are not proxy'd have our address, otherwise
1017 * we need to consult the routing table to look for a proxy entry.
1020 mesh_isucastforme(struct ieee80211vap *vap, const struct ieee80211_frame *wh,
1021 const struct ieee80211_meshcntl *mc)
1023 int ae = mc->mc_flags & 3;
1025 KASSERT((wh->i_fc[1] & IEEE80211_FC1_DIR_MASK) == IEEE80211_FC1_DIR_DSTODS,
1026 ("bad dir 0x%x:0x%x", wh->i_fc[0], wh->i_fc[1]));
1027 KASSERT(ae == 0 || ae == 2, ("bad AE %d", ae));
1028 if (ae == 2) { /* ucast w/ proxy */
1029 const struct ieee80211_meshcntl_ae10 *mc10 =
1030 (const struct ieee80211_meshcntl_ae10 *) mc;
1031 struct ieee80211_mesh_route *rt =
1032 ieee80211_mesh_rt_find(vap, mc10->mc_addr4);
1033 /* check for proxy route to ourself */
1034 return (rt != NULL &&
1035 (rt->rt_flags & IEEE80211_MESHRT_FLAGS_PROXY));
1036 } else /* ucast w/o proxy */
1037 return IEEE80211_ADDR_EQ(wh->i_addr3, vap->iv_myaddr);
1041 mesh_input(struct ieee80211_node *ni, struct mbuf *m, int rssi, int nf)
1043 #define SEQ_LEQ(a,b) ((int)((a)-(b)) <= 0)
1044 #define HAS_SEQ(type) ((type & 0x4) == 0)
1045 struct ieee80211vap *vap = ni->ni_vap;
1046 struct ieee80211com *ic = ni->ni_ic;
1047 struct ifnet *ifp = vap->iv_ifp;
1048 struct ieee80211_frame *wh;
1049 const struct ieee80211_meshcntl *mc;
1050 int hdrspace, meshdrlen, need_tap;
1051 uint8_t dir, type, subtype, qos;
1054 ieee80211_seq rxseq;
1056 KASSERT(ni != NULL, ("null node"));
1057 ni->ni_inact = ni->ni_inact_reload;
1059 need_tap = 1; /* mbuf need to be tapped. */
1060 type = -1; /* undefined */
1062 if (m->m_pkthdr.len < sizeof(struct ieee80211_frame_min)) {
1063 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
1064 ni->ni_macaddr, NULL,
1065 "too short (1): len %u", m->m_pkthdr.len);
1066 vap->iv_stats.is_rx_tooshort++;
1070 * Bit of a cheat here, we use a pointer for a 3-address
1071 * frame format but don't reference fields past outside
1072 * ieee80211_frame_min w/o first validating the data is
1075 wh = mtod(m, struct ieee80211_frame *);
1077 if ((wh->i_fc[0] & IEEE80211_FC0_VERSION_MASK) !=
1078 IEEE80211_FC0_VERSION_0) {
1079 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
1080 ni->ni_macaddr, NULL, "wrong version %x", wh->i_fc[0]);
1081 vap->iv_stats.is_rx_badversion++;
1084 dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK;
1085 type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
1086 subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
1087 if ((ic->ic_flags & IEEE80211_F_SCAN) == 0) {
1088 IEEE80211_RSSI_LPF(ni->ni_avgrssi, rssi);
1090 if (HAS_SEQ(type)) {
1091 uint8_t tid = ieee80211_gettid(wh);
1093 if (IEEE80211_QOS_HAS_SEQ(wh) &&
1094 TID_TO_WME_AC(tid) >= WME_AC_VI)
1095 ic->ic_wme.wme_hipri_traffic++;
1096 rxseq = le16toh(*(uint16_t *)wh->i_seq);
1097 if ((ni->ni_flags & IEEE80211_NODE_HT) == 0 &&
1098 (wh->i_fc[1] & IEEE80211_FC1_RETRY) &&
1099 SEQ_LEQ(rxseq, ni->ni_rxseqs[tid])) {
1100 /* duplicate, discard */
1101 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
1102 wh->i_addr1, "duplicate",
1103 "seqno <%u,%u> fragno <%u,%u> tid %u",
1104 rxseq >> IEEE80211_SEQ_SEQ_SHIFT,
1105 ni->ni_rxseqs[tid] >>
1106 IEEE80211_SEQ_SEQ_SHIFT,
1107 rxseq & IEEE80211_SEQ_FRAG_MASK,
1108 ni->ni_rxseqs[tid] &
1109 IEEE80211_SEQ_FRAG_MASK,
1111 vap->iv_stats.is_rx_dup++;
1112 IEEE80211_NODE_STAT(ni, rx_dup);
1115 ni->ni_rxseqs[tid] = rxseq;
1118 #ifdef IEEE80211_DEBUG
1120 * It's easier, but too expensive, to simulate different mesh
1121 * topologies by consulting the ACL policy very early, so do this
1124 * NB: this check is also done upon peering link initiation.
1126 if (vap->iv_acl != NULL && !vap->iv_acl->iac_check(vap, wh->i_addr2)) {
1127 IEEE80211_DISCARD(vap, IEEE80211_MSG_ACL,
1128 wh, NULL, "%s", "disallowed by ACL");
1129 vap->iv_stats.is_rx_acl++;
1134 case IEEE80211_FC0_TYPE_DATA:
1135 if (ni == vap->iv_bss)
1137 if (ni->ni_mlstate != IEEE80211_NODE_MESH_ESTABLISHED) {
1138 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_MESH,
1139 ni->ni_macaddr, NULL,
1140 "peer link not yet established (%d)",
1142 vap->iv_stats.is_mesh_nolink++;
1145 if (dir != IEEE80211_FC1_DIR_FROMDS &&
1146 dir != IEEE80211_FC1_DIR_DSTODS) {
1147 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
1148 wh, "data", "incorrect dir 0x%x", dir);
1149 vap->iv_stats.is_rx_wrongdir++;
1152 /* pull up enough to get to the mesh control */
1153 hdrspace = ieee80211_hdrspace(ic, wh);
1154 if (m->m_len < hdrspace + sizeof(struct ieee80211_meshcntl) &&
1155 (m = m_pullup(m, hdrspace +
1156 sizeof(struct ieee80211_meshcntl))) == NULL) {
1157 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
1158 ni->ni_macaddr, NULL,
1159 "data too short: expecting %u", hdrspace);
1160 vap->iv_stats.is_rx_tooshort++;
1164 * Now calculate the full extent of the headers. Note
1165 * mesh_decap will pull up anything we didn't get
1166 * above when it strips the 802.11 headers.
1168 mc = (const struct ieee80211_meshcntl *)
1169 (mtod(m, const uint8_t *) + hdrspace);
1170 meshdrlen = sizeof(struct ieee80211_meshcntl) +
1171 (mc->mc_flags & 3) * IEEE80211_ADDR_LEN;
1172 hdrspace += meshdrlen;
1173 seq = LE_READ_4(mc->mc_seq);
1174 if (IEEE80211_IS_MULTICAST(wh->i_addr1))
1177 addr = ((struct ieee80211_qosframe_addr4 *)wh)->i_addr4;
1178 if (IEEE80211_ADDR_EQ(vap->iv_myaddr, addr)) {
1179 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
1180 addr, "data", "%s", "not to me");
1181 vap->iv_stats.is_rx_wrongbss++; /* XXX kinda */
1184 if (mesh_checkpseq(vap, addr, seq) != 0) {
1185 vap->iv_stats.is_rx_dup++;
1190 * Potentially forward packet. See table s36 (p140)
1191 * for the rules. XXX tap fwd'd packets not for us?
1193 if (dir == IEEE80211_FC1_DIR_FROMDS ||
1194 !mesh_isucastforme(vap, wh, mc)) {
1195 mesh_forward(vap, m, mc);
1196 if (dir == IEEE80211_FC1_DIR_DSTODS)
1198 /* NB: fall thru to deliver mcast frames locally */
1202 * Save QoS bits for use below--before we strip the header.
1204 if (subtype == IEEE80211_FC0_SUBTYPE_QOS) {
1205 qos = (dir == IEEE80211_FC1_DIR_DSTODS) ?
1206 ((struct ieee80211_qosframe_addr4 *)wh)->i_qos[0] :
1207 ((struct ieee80211_qosframe *)wh)->i_qos[0];
1211 * Next up, any fragmentation.
1213 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1214 m = ieee80211_defrag(ni, m, hdrspace);
1216 /* Fragment dropped or frame not complete yet */
1220 wh = NULL; /* no longer valid, catch any uses */
1222 if (ieee80211_radiotap_active_vap(vap))
1223 ieee80211_radiotap_rx(vap, m);
1227 * Finally, strip the 802.11 header.
1229 m = mesh_decap(vap, m, hdrspace, meshdrlen);
1231 /* XXX mask bit to check for both */
1232 /* don't count Null data frames as errors */
1233 if (subtype == IEEE80211_FC0_SUBTYPE_NODATA ||
1234 subtype == IEEE80211_FC0_SUBTYPE_QOS_NULL)
1236 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
1237 ni->ni_macaddr, "data", "%s", "decap error");
1238 vap->iv_stats.is_rx_decap++;
1239 IEEE80211_NODE_STAT(ni, rx_decap);
1242 if (qos & IEEE80211_QOS_AMSDU) {
1243 m = ieee80211_decap_amsdu(ni, m);
1245 return IEEE80211_FC0_TYPE_DATA;
1247 ieee80211_deliver_data(vap, ni, m);
1249 case IEEE80211_FC0_TYPE_MGT:
1250 vap->iv_stats.is_rx_mgmt++;
1251 IEEE80211_NODE_STAT(ni, rx_mgmt);
1252 if (dir != IEEE80211_FC1_DIR_NODS) {
1253 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
1254 wh, "mgt", "incorrect dir 0x%x", dir);
1255 vap->iv_stats.is_rx_wrongdir++;
1258 if (m->m_pkthdr.len < sizeof(struct ieee80211_frame)) {
1259 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
1260 ni->ni_macaddr, "mgt", "too short: len %u",
1262 vap->iv_stats.is_rx_tooshort++;
1265 #ifdef IEEE80211_DEBUG
1266 if ((ieee80211_msg_debug(vap) &&
1267 (vap->iv_ic->ic_flags & IEEE80211_F_SCAN)) ||
1268 ieee80211_msg_dumppkts(vap)) {
1269 if_printf(ifp, "received %s from %s rssi %d\n",
1270 ieee80211_mgt_subtype_name[subtype >>
1271 IEEE80211_FC0_SUBTYPE_SHIFT],
1272 ether_sprintf(wh->i_addr2), rssi);
1275 if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
1276 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
1277 wh, NULL, "%s", "WEP set but not permitted");
1278 vap->iv_stats.is_rx_mgtdiscard++; /* XXX */
1281 vap->iv_recv_mgmt(ni, m, subtype, rssi, nf);
1283 case IEEE80211_FC0_TYPE_CTL:
1284 vap->iv_stats.is_rx_ctl++;
1285 IEEE80211_NODE_STAT(ni, rx_ctrl);
1288 IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
1289 wh, "bad", "frame type 0x%x", type);
1290 /* should not come here */
1297 if (need_tap && ieee80211_radiotap_active_vap(vap))
1298 ieee80211_radiotap_rx(vap, m);
1305 mesh_recv_mgmt(struct ieee80211_node *ni, struct mbuf *m0, int subtype,
1308 struct ieee80211vap *vap = ni->ni_vap;
1309 struct ieee80211_mesh_state *ms = vap->iv_mesh;
1310 struct ieee80211com *ic = ni->ni_ic;
1311 struct ieee80211_frame *wh;
1312 uint8_t *frm, *efrm;
1314 wh = mtod(m0, struct ieee80211_frame *);
1315 frm = (uint8_t *)&wh[1];
1316 efrm = mtod(m0, uint8_t *) + m0->m_len;
1318 case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
1319 case IEEE80211_FC0_SUBTYPE_BEACON:
1321 struct ieee80211_scanparams scan;
1323 * We process beacon/probe response
1324 * frames to discover neighbors.
1326 if (ieee80211_parse_beacon(ni, m0, &scan) != 0)
1329 * Count frame now that we know it's to be processed.
1331 if (subtype == IEEE80211_FC0_SUBTYPE_BEACON) {
1332 vap->iv_stats.is_rx_beacon++; /* XXX remove */
1333 IEEE80211_NODE_STAT(ni, rx_beacons);
1335 IEEE80211_NODE_STAT(ni, rx_proberesp);
1337 * If scanning, just pass information to the scan module.
1339 if (ic->ic_flags & IEEE80211_F_SCAN) {
1340 if (ic->ic_flags_ext & IEEE80211_FEXT_PROBECHAN) {
1342 * Actively scanning a channel marked passive;
1343 * send a probe request now that we know there
1344 * is 802.11 traffic present.
1346 * XXX check if the beacon we recv'd gives
1347 * us what we need and suppress the probe req
1349 ieee80211_probe_curchan(vap, 1);
1350 ic->ic_flags_ext &= ~IEEE80211_FEXT_PROBECHAN;
1352 ieee80211_add_scan(vap, &scan, wh,
1357 /* The rest of this code assumes we are running */
1358 if (vap->iv_state != IEEE80211_S_RUN)
1361 * Ignore non-mesh STAs.
1364 (IEEE80211_CAPINFO_ESS|IEEE80211_CAPINFO_IBSS)) ||
1365 scan.meshid == NULL || scan.meshconf == NULL) {
1366 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
1367 wh, "beacon", "%s", "not a mesh sta");
1368 vap->iv_stats.is_mesh_wrongmesh++;
1372 * Ignore STAs for other mesh networks.
1374 if (memcmp(scan.meshid+2, ms->ms_id, ms->ms_idlen) != 0 ||
1375 mesh_verify_meshconf(vap, scan.meshconf)) {
1376 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
1377 wh, "beacon", "%s", "not for our mesh");
1378 vap->iv_stats.is_mesh_wrongmesh++;
1382 * Peer only based on the current ACL policy.
1384 if (vap->iv_acl != NULL &&
1385 !vap->iv_acl->iac_check(vap, wh->i_addr2)) {
1386 IEEE80211_DISCARD(vap, IEEE80211_MSG_ACL,
1387 wh, NULL, "%s", "disallowed by ACL");
1388 vap->iv_stats.is_rx_acl++;
1392 * Do neighbor discovery.
1394 if (!IEEE80211_ADDR_EQ(wh->i_addr2, ni->ni_macaddr)) {
1396 * Create a new entry in the neighbor table.
1398 ni = ieee80211_add_neighbor(vap, wh, &scan);
1401 * Automatically peer with discovered nodes if possible.
1402 * XXX backoff on repeated failure
1404 if (ni != vap->iv_bss &&
1405 (ms->ms_flags & IEEE80211_MESHFLAGS_AP) &&
1406 ni->ni_mlstate == IEEE80211_NODE_MESH_IDLE) {
1409 ni->ni_mlpid = mesh_generateid(vap);
1410 if (ni->ni_mlpid == 0)
1412 mesh_linkchange(ni, IEEE80211_NODE_MESH_OPENSNT);
1413 args[0] = ni->ni_mlpid;
1414 ieee80211_send_action(ni,
1415 IEEE80211_ACTION_CAT_MESHPEERING,
1416 IEEE80211_ACTION_MESHPEERING_OPEN, args);
1418 mesh_peer_timeout_setup(ni);
1422 case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
1424 uint8_t *ssid, *meshid, *rates, *xrates;
1427 if (vap->iv_state != IEEE80211_S_RUN) {
1428 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
1429 wh, NULL, "wrong state %s",
1430 ieee80211_state_name[vap->iv_state]);
1431 vap->iv_stats.is_rx_mgtdiscard++;
1434 if (IEEE80211_IS_MULTICAST(wh->i_addr2)) {
1435 /* frame must be directed */
1436 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
1437 wh, NULL, "%s", "not unicast");
1438 vap->iv_stats.is_rx_mgtdiscard++; /* XXX stat */
1442 * prreq frame format
1444 * [tlv] supported rates
1445 * [tlv] extended supported rates
1448 ssid = meshid = rates = xrates = NULL;
1450 while (efrm - frm > 1) {
1451 IEEE80211_VERIFY_LENGTH(efrm - frm, frm[1] + 2, return);
1453 case IEEE80211_ELEMID_SSID:
1456 case IEEE80211_ELEMID_RATES:
1459 case IEEE80211_ELEMID_XRATES:
1462 case IEEE80211_ELEMID_MESHID:
1468 IEEE80211_VERIFY_ELEMENT(ssid, IEEE80211_NWID_LEN, return);
1469 IEEE80211_VERIFY_ELEMENT(rates, IEEE80211_RATE_MAXSIZE, return);
1471 IEEE80211_VERIFY_ELEMENT(xrates,
1472 IEEE80211_RATE_MAXSIZE - rates[1], return);
1474 IEEE80211_VERIFY_ELEMENT(meshid,
1475 IEEE80211_MESHID_LEN, return);
1476 /* NB: meshid, not ssid */
1477 IEEE80211_VERIFY_SSID(vap->iv_bss, meshid, return);
1479 /* XXX find a better class or define it's own */
1480 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_INPUT, wh->i_addr2,
1481 "%s", "recv probe req");
1483 * Some legacy 11b clients cannot hack a complete
1484 * probe response frame. When the request includes
1485 * only a bare-bones rate set, communicate this to
1486 * the transmit side.
1488 ieee80211_send_proberesp(vap, wh->i_addr2, 0);
1491 case IEEE80211_FC0_SUBTYPE_ACTION:
1492 if (vap->iv_state != IEEE80211_S_RUN) {
1493 vap->iv_stats.is_rx_mgtdiscard++;
1497 * We received an action for an unknown neighbor.
1498 * XXX: wait for it to beacon or create ieee80211_node?
1500 if (ni == vap->iv_bss) {
1501 IEEE80211_DISCARD(vap, IEEE80211_MSG_MESH,
1502 wh, NULL, "%s", "unknown node");
1503 vap->iv_stats.is_rx_mgtdiscard++;
1507 * Discard if not for us.
1509 if (!IEEE80211_ADDR_EQ(vap->iv_myaddr, wh->i_addr1) &&
1510 !IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1511 IEEE80211_DISCARD(vap, IEEE80211_MSG_MESH,
1512 wh, NULL, "%s", "not for me");
1513 vap->iv_stats.is_rx_mgtdiscard++;
1516 /* XXX parse_action is a bit useless now */
1517 if (ieee80211_parse_action(ni, m0) == 0)
1518 ic->ic_recv_action(ni, wh, frm, efrm);
1520 case IEEE80211_FC0_SUBTYPE_AUTH:
1521 case IEEE80211_FC0_SUBTYPE_ASSOC_REQ:
1522 case IEEE80211_FC0_SUBTYPE_REASSOC_REQ:
1523 case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
1524 case IEEE80211_FC0_SUBTYPE_REASSOC_RESP:
1525 case IEEE80211_FC0_SUBTYPE_DEAUTH:
1526 case IEEE80211_FC0_SUBTYPE_DISASSOC:
1527 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
1528 wh, NULL, "%s", "not handled");
1529 vap->iv_stats.is_rx_mgtdiscard++;
1532 IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
1533 wh, "mgt", "subtype 0x%x not handled", subtype);
1534 vap->iv_stats.is_rx_badsubtype++;
1540 * Parse meshpeering action ie's for open+confirm frames; the
1541 * important bits are returned in the supplied structure.
1543 static const struct ieee80211_meshpeer_ie *
1544 mesh_parse_meshpeering_action(struct ieee80211_node *ni,
1545 const struct ieee80211_frame *wh, /* XXX for VERIFY_LENGTH */
1546 const uint8_t *frm, const uint8_t *efrm,
1547 struct ieee80211_meshpeer_ie *mp)
1549 struct ieee80211vap *vap = ni->ni_vap;
1550 const struct ieee80211_meshpeer_ie *mpie;
1551 const uint8_t *meshid, *meshconf, *meshpeerver, *meshpeer;
1553 meshid = meshconf = meshpeerver = meshpeer = NULL;
1554 while (efrm - frm > 1) {
1555 IEEE80211_VERIFY_LENGTH(efrm - frm, frm[1] + 2, return NULL);
1557 case IEEE80211_ELEMID_MESHPEERVER:
1560 case IEEE80211_ELEMID_MESHID:
1563 case IEEE80211_ELEMID_MESHCONF:
1566 case IEEE80211_ELEMID_MESHPEER:
1568 mpie = (const struct ieee80211_meshpeer_ie *) frm;
1569 memset(mp, 0, sizeof(*mp));
1570 mp->peer_subtype = mpie->peer_subtype;
1571 mp->peer_llinkid = LE_READ_2(&mpie->peer_llinkid);
1572 /* NB: peer link ID is optional on these frames */
1573 if (mpie->peer_subtype ==
1574 IEEE80211_MESH_PEER_LINK_CLOSE &&
1575 mpie->peer_len == 5) {
1576 mp->peer_linkid = 0;
1577 mp->peer_rcode = LE_READ_2(&mpie->peer_linkid);
1579 mp->peer_linkid = LE_READ_2(&mpie->peer_linkid);
1580 mp->peer_rcode = LE_READ_2(&mpie->peer_rcode);
1588 * Verify the contents of the frame. Action frames with
1589 * close subtype don't have a Mesh Configuration IE.
1590 * If if fails validation, close the peer link.
1592 KASSERT(meshpeer != NULL && mp->peer_subtype !=
1593 IEEE80211_ACTION_MESHPEERING_CLOSE, ("parsing close action"));
1595 if (mesh_verify_meshpeerver(vap, meshpeerver) ||
1596 mesh_verify_meshid(vap, meshid) ||
1597 mesh_verify_meshpeer(vap, meshpeer) ||
1598 mesh_verify_meshconf(vap, meshconf)) {
1601 IEEE80211_DISCARD(vap,
1602 IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH,
1603 wh, NULL, "%s", "not for our mesh");
1604 vap->iv_stats.is_rx_mgtdiscard++;
1605 switch (ni->ni_mlstate) {
1606 case IEEE80211_NODE_MESH_IDLE:
1607 case IEEE80211_NODE_MESH_ESTABLISHED:
1608 case IEEE80211_NODE_MESH_HOLDING:
1611 case IEEE80211_NODE_MESH_OPENSNT:
1612 case IEEE80211_NODE_MESH_OPENRCV:
1613 case IEEE80211_NODE_MESH_CONFIRMRCV:
1614 args[0] = ni->ni_mlpid;
1615 args[1] = ni->ni_mllid;
1616 args[2] = IEEE80211_REASON_PEER_LINK_CANCELED;
1617 ieee80211_send_action(ni,
1618 IEEE80211_ACTION_CAT_MESHPEERING,
1619 IEEE80211_ACTION_MESHPEERING_CLOSE,
1621 mesh_linkchange(ni, IEEE80211_NODE_MESH_HOLDING);
1622 mesh_peer_timeout_setup(ni);
1627 return (const struct ieee80211_meshpeer_ie *) mp;
1631 mesh_recv_action_meshpeering_open(struct ieee80211_node *ni,
1632 const struct ieee80211_frame *wh,
1633 const uint8_t *frm, const uint8_t *efrm)
1635 struct ieee80211vap *vap = ni->ni_vap;
1636 struct ieee80211_meshpeer_ie ie;
1637 const struct ieee80211_meshpeer_ie *meshpeer;
1640 /* +2+2 for action + code + capabilites */
1641 meshpeer = mesh_parse_meshpeering_action(ni, wh, frm+2+2, efrm, &ie);
1642 if (meshpeer == NULL) {
1647 IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH, ni,
1648 "recv PEER OPEN, lid 0x%x", meshpeer->peer_llinkid);
1650 switch (ni->ni_mlstate) {
1651 case IEEE80211_NODE_MESH_IDLE:
1652 mesh_linkchange(ni, IEEE80211_NODE_MESH_OPENRCV);
1653 ni->ni_mllid = meshpeer->peer_llinkid;
1654 ni->ni_mlpid = mesh_generateid(vap);
1655 if (ni->ni_mlpid == 0)
1657 args[0] = ni->ni_mlpid;
1658 /* Announce we're open too... */
1659 ieee80211_send_action(ni,
1660 IEEE80211_ACTION_CAT_MESHPEERING,
1661 IEEE80211_ACTION_MESHPEERING_OPEN, args);
1662 /* ...and confirm the link. */
1663 args[0] = ni->ni_mlpid;
1664 args[1] = ni->ni_mllid;
1665 ieee80211_send_action(ni,
1666 IEEE80211_ACTION_CAT_MESHPEERING,
1667 IEEE80211_ACTION_MESHPEERING_CONFIRM,
1669 mesh_peer_timeout_setup(ni);
1671 case IEEE80211_NODE_MESH_OPENRCV:
1673 if (ni->ni_mllid != meshpeer->peer_llinkid) {
1674 args[0] = ni->ni_mllid;
1675 args[1] = ni->ni_mlpid;
1676 args[2] = IEEE80211_REASON_PEER_LINK_CANCELED;
1677 ieee80211_send_action(ni,
1678 IEEE80211_ACTION_CAT_MESHPEERING,
1679 IEEE80211_ACTION_MESHPEERING_CLOSE,
1681 mesh_linkchange(ni, IEEE80211_NODE_MESH_HOLDING);
1682 mesh_peer_timeout_setup(ni);
1685 /* Duplicate open, confirm again. */
1686 args[0] = ni->ni_mlpid;
1687 args[1] = ni->ni_mllid;
1688 ieee80211_send_action(ni,
1689 IEEE80211_ACTION_CAT_MESHPEERING,
1690 IEEE80211_ACTION_MESHPEERING_CONFIRM,
1693 case IEEE80211_NODE_MESH_OPENSNT:
1694 ni->ni_mllid = meshpeer->peer_llinkid;
1695 mesh_linkchange(ni, IEEE80211_NODE_MESH_OPENRCV);
1696 args[0] = ni->ni_mlpid;
1697 args[1] = ni->ni_mllid;
1698 ieee80211_send_action(ni,
1699 IEEE80211_ACTION_CAT_MESHPEERING,
1700 IEEE80211_ACTION_MESHPEERING_CONFIRM,
1702 /* NB: don't setup/clear any timeout */
1704 case IEEE80211_NODE_MESH_CONFIRMRCV:
1705 if (ni->ni_mlpid != meshpeer->peer_linkid ||
1706 ni->ni_mllid != meshpeer->peer_llinkid) {
1707 args[0] = ni->ni_mlpid;
1708 args[1] = ni->ni_mllid;
1709 args[2] = IEEE80211_REASON_PEER_LINK_CANCELED;
1710 ieee80211_send_action(ni,
1711 IEEE80211_ACTION_CAT_MESHPEERING,
1712 IEEE80211_ACTION_MESHPEERING_CLOSE,
1715 IEEE80211_NODE_MESH_HOLDING);
1716 mesh_peer_timeout_setup(ni);
1719 mesh_linkchange(ni, IEEE80211_NODE_MESH_ESTABLISHED);
1720 ni->ni_mllid = meshpeer->peer_llinkid;
1721 args[0] = ni->ni_mlpid;
1722 args[1] = ni->ni_mllid;
1723 ieee80211_send_action(ni,
1724 IEEE80211_ACTION_CAT_MESHPEERING,
1725 IEEE80211_ACTION_MESHPEERING_CONFIRM,
1727 mesh_peer_timeout_stop(ni);
1729 case IEEE80211_NODE_MESH_ESTABLISHED:
1730 if (ni->ni_mllid != meshpeer->peer_llinkid) {
1731 args[0] = ni->ni_mllid;
1732 args[1] = ni->ni_mlpid;
1733 args[2] = IEEE80211_REASON_PEER_LINK_CANCELED;
1734 ieee80211_send_action(ni,
1735 IEEE80211_ACTION_CAT_MESHPEERING,
1736 IEEE80211_ACTION_MESHPEERING_CLOSE,
1738 mesh_linkchange(ni, IEEE80211_NODE_MESH_HOLDING);
1739 mesh_peer_timeout_setup(ni);
1742 args[0] = ni->ni_mlpid;
1743 args[1] = ni->ni_mllid;
1744 ieee80211_send_action(ni,
1745 IEEE80211_ACTION_CAT_MESHPEERING,
1746 IEEE80211_ACTION_MESHPEERING_CONFIRM,
1749 case IEEE80211_NODE_MESH_HOLDING:
1750 args[0] = ni->ni_mlpid;
1751 args[1] = meshpeer->peer_llinkid;
1752 args[2] = IEEE80211_REASON_MESH_MAX_RETRIES;
1753 ieee80211_send_action(ni,
1754 IEEE80211_ACTION_CAT_MESHPEERING,
1755 IEEE80211_ACTION_MESHPEERING_CLOSE,
1763 mesh_recv_action_meshpeering_confirm(struct ieee80211_node *ni,
1764 const struct ieee80211_frame *wh,
1765 const uint8_t *frm, const uint8_t *efrm)
1767 struct ieee80211vap *vap = ni->ni_vap;
1768 struct ieee80211_meshpeer_ie ie;
1769 const struct ieee80211_meshpeer_ie *meshpeer;
1772 /* +2+2+2+2 for action + code + capabilites + status code + AID */
1773 meshpeer = mesh_parse_meshpeering_action(ni, wh, frm+2+2+2+2, efrm, &ie);
1774 if (meshpeer == NULL) {
1778 IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH, ni,
1779 "recv PEER CONFIRM, local id 0x%x, peer id 0x%x",
1780 meshpeer->peer_llinkid, meshpeer->peer_linkid);
1782 switch (ni->ni_mlstate) {
1783 case IEEE80211_NODE_MESH_OPENRCV:
1784 mesh_linkchange(ni, IEEE80211_NODE_MESH_ESTABLISHED);
1785 mesh_peer_timeout_stop(ni);
1787 case IEEE80211_NODE_MESH_OPENSNT:
1788 mesh_linkchange(ni, IEEE80211_NODE_MESH_CONFIRMRCV);
1790 case IEEE80211_NODE_MESH_HOLDING:
1791 args[0] = ni->ni_mlpid;
1792 args[1] = meshpeer->peer_llinkid;
1793 args[2] = IEEE80211_REASON_MESH_MAX_RETRIES;
1794 ieee80211_send_action(ni,
1795 IEEE80211_ACTION_CAT_MESHPEERING,
1796 IEEE80211_ACTION_MESHPEERING_CLOSE,
1799 case IEEE80211_NODE_MESH_CONFIRMRCV:
1800 if (ni->ni_mllid != meshpeer->peer_llinkid) {
1801 args[0] = ni->ni_mlpid;
1802 args[1] = ni->ni_mllid;
1803 args[2] = IEEE80211_REASON_PEER_LINK_CANCELED;
1804 ieee80211_send_action(ni,
1805 IEEE80211_ACTION_CAT_MESHPEERING,
1806 IEEE80211_ACTION_MESHPEERING_CLOSE,
1808 mesh_linkchange(ni, IEEE80211_NODE_MESH_HOLDING);
1809 mesh_peer_timeout_setup(ni);
1813 IEEE80211_DISCARD(vap,
1814 IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH,
1815 wh, NULL, "received confirm in invalid state %d",
1817 vap->iv_stats.is_rx_mgtdiscard++;
1824 mesh_recv_action_meshpeering_close(struct ieee80211_node *ni,
1825 const struct ieee80211_frame *wh,
1826 const uint8_t *frm, const uint8_t *efrm)
1830 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH,
1831 ni, "%s", "recv PEER CLOSE");
1833 switch (ni->ni_mlstate) {
1834 case IEEE80211_NODE_MESH_IDLE:
1837 case IEEE80211_NODE_MESH_OPENRCV:
1838 case IEEE80211_NODE_MESH_OPENSNT:
1839 case IEEE80211_NODE_MESH_CONFIRMRCV:
1840 case IEEE80211_NODE_MESH_ESTABLISHED:
1841 args[0] = ni->ni_mlpid;
1842 args[1] = ni->ni_mllid;
1843 args[2] = IEEE80211_REASON_MESH_CLOSE_RCVD;
1844 ieee80211_send_action(ni,
1845 IEEE80211_ACTION_CAT_MESHPEERING,
1846 IEEE80211_ACTION_MESHPEERING_CLOSE,
1848 mesh_linkchange(ni, IEEE80211_NODE_MESH_HOLDING);
1849 mesh_peer_timeout_setup(ni);
1851 case IEEE80211_NODE_MESH_HOLDING:
1852 mesh_linkchange(ni, IEEE80211_NODE_MESH_IDLE);
1853 mesh_peer_timeout_setup(ni);
1860 * Link Metric handling.
1863 mesh_recv_action_meshlmetric_req(struct ieee80211_node *ni,
1864 const struct ieee80211_frame *wh,
1865 const uint8_t *frm, const uint8_t *efrm)
1869 metric = mesh_airtime_calc(ni);
1870 ieee80211_send_action(ni,
1871 IEEE80211_ACTION_CAT_MESHLMETRIC,
1872 IEEE80211_ACTION_MESHLMETRIC_REP,
1878 mesh_recv_action_meshlmetric_rep(struct ieee80211_node *ni,
1879 const struct ieee80211_frame *wh,
1880 const uint8_t *frm, const uint8_t *efrm)
1886 mesh_send_action(struct ieee80211_node *ni, struct mbuf *m)
1888 struct ieee80211_bpf_params params;
1890 memset(¶ms, 0, sizeof(params));
1891 params.ibp_pri = WME_AC_VO;
1892 params.ibp_rate0 = ni->ni_txparms->mgmtrate;
1893 /* XXX ucast/mcast */
1894 params.ibp_try0 = ni->ni_txparms->maxretry;
1895 params.ibp_power = ni->ni_txpower;
1896 return ieee80211_mgmt_output(ni, m, IEEE80211_FC0_SUBTYPE_ACTION,
1900 #define ADDSHORT(frm, v) do { \
1901 frm[0] = (v) & 0xff; \
1902 frm[1] = (v) >> 8; \
1905 #define ADDWORD(frm, v) do { \
1906 frm[0] = (v) & 0xff; \
1907 frm[1] = ((v) >> 8) & 0xff; \
1908 frm[2] = ((v) >> 16) & 0xff; \
1909 frm[3] = ((v) >> 24) & 0xff; \
1914 mesh_send_action_meshpeering_open(struct ieee80211_node *ni,
1915 int category, int action, void *args0)
1917 struct ieee80211vap *vap = ni->ni_vap;
1918 struct ieee80211com *ic = ni->ni_ic;
1919 uint16_t *args = args0;
1920 const struct ieee80211_rateset *rs;
1924 IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH, ni,
1925 "send PEER OPEN action: localid 0x%x", args[0]);
1927 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
1928 "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", __func__, __LINE__,
1929 ni, ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)+1);
1930 ieee80211_ref_node(ni);
1932 m = ieee80211_getmgtframe(&frm,
1933 ic->ic_headroom + sizeof(struct ieee80211_frame),
1934 sizeof(uint16_t) /* action+category */
1935 + sizeof(uint16_t) /* capabilites */
1936 + sizeof(struct ieee80211_meshpeerver_ie)
1937 + 2 + IEEE80211_RATE_SIZE
1938 + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
1939 + 2 + IEEE80211_MESHID_LEN
1940 + sizeof(struct ieee80211_meshconf_ie)
1941 + sizeof(struct ieee80211_meshpeer_ie)
1945 * mesh peer open action frame format:
1949 * [tlv] mesh peer protocol version
1954 * [tlv] mesh peer link mgmt
1958 ADDSHORT(frm, ieee80211_getcapinfo(vap, ni->ni_chan));
1959 frm = ieee80211_add_meshpeerver(frm, vap);
1960 rs = ieee80211_get_suprates(ic, ic->ic_curchan);
1961 frm = ieee80211_add_rates(frm, rs);
1962 frm = ieee80211_add_xrates(frm, rs);
1963 frm = ieee80211_add_meshid(frm, vap);
1964 frm = ieee80211_add_meshconf(frm, vap);
1965 frm = ieee80211_add_meshpeer(frm, IEEE80211_MESH_PEER_LINK_OPEN,
1967 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
1968 return mesh_send_action(ni, m);
1970 vap->iv_stats.is_tx_nobuf++;
1971 ieee80211_free_node(ni);
1977 mesh_send_action_meshpeering_confirm(struct ieee80211_node *ni,
1978 int category, int action, void *args0)
1980 struct ieee80211vap *vap = ni->ni_vap;
1981 struct ieee80211com *ic = ni->ni_ic;
1982 uint16_t *args = args0;
1983 const struct ieee80211_rateset *rs;
1987 IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH, ni,
1988 "send PEER CONFIRM action: localid 0x%x, peerid 0x%x",
1991 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
1992 "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", __func__, __LINE__,
1993 ni, ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)+1);
1994 ieee80211_ref_node(ni);
1996 m = ieee80211_getmgtframe(&frm,
1997 ic->ic_headroom + sizeof(struct ieee80211_frame),
1998 sizeof(uint16_t) /* action+category */
1999 + sizeof(uint16_t) /* capabilites */
2000 + sizeof(uint16_t) /* status code */
2001 + sizeof(uint16_t) /* AID */
2002 + sizeof(struct ieee80211_meshpeerver_ie)
2003 + 2 + IEEE80211_RATE_SIZE
2004 + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
2005 + 2 + IEEE80211_MESHID_LEN
2006 + sizeof(struct ieee80211_meshconf_ie)
2007 + sizeof(struct ieee80211_meshpeer_ie)
2011 * mesh peer confirm action frame format:
2016 * [2] association id (peer ID)
2017 * [tlv] mesh peer protocol version
2022 * [tlv] mesh peer link mgmt
2026 ADDSHORT(frm, ieee80211_getcapinfo(vap, ni->ni_chan));
2027 ADDSHORT(frm, 0); /* status code */
2028 ADDSHORT(frm, args[1]); /* AID */
2029 frm = ieee80211_add_meshpeerver(frm, vap);
2030 rs = ieee80211_get_suprates(ic, ic->ic_curchan);
2031 frm = ieee80211_add_rates(frm, rs);
2032 frm = ieee80211_add_xrates(frm, rs);
2033 frm = ieee80211_add_meshid(frm, vap);
2034 frm = ieee80211_add_meshconf(frm, vap);
2035 frm = ieee80211_add_meshpeer(frm,
2036 IEEE80211_MESH_PEER_LINK_CONFIRM,
2037 args[0], args[1], 0);
2038 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2039 return mesh_send_action(ni, m);
2041 vap->iv_stats.is_tx_nobuf++;
2042 ieee80211_free_node(ni);
2048 mesh_send_action_meshpeering_close(struct ieee80211_node *ni,
2049 int category, int action, void *args0)
2051 struct ieee80211vap *vap = ni->ni_vap;
2052 struct ieee80211com *ic = ni->ni_ic;
2053 uint16_t *args = args0;
2057 IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH, ni,
2058 "send PEER CLOSE action: localid 0x%x, peerid 0x%x reason %d",
2059 args[0], args[1], args[2]);
2061 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
2062 "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", __func__, __LINE__,
2063 ni, ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)+1);
2064 ieee80211_ref_node(ni);
2066 m = ieee80211_getmgtframe(&frm,
2067 ic->ic_headroom + sizeof(struct ieee80211_frame),
2068 sizeof(uint16_t) /* action+category */
2069 + sizeof(uint16_t) /* reason code */
2070 + sizeof(struct ieee80211_meshpeerver_ie)
2071 + 2 + IEEE80211_MESHID_LEN
2072 + sizeof(struct ieee80211_meshpeer_ie)
2076 * mesh peer close action frame format:
2080 * [tlv] mesh peer protocol version
2082 * [tlv] mesh peer link mgmt
2086 ADDSHORT(frm, args[2]); /* reason code */
2087 frm = ieee80211_add_meshpeerver(frm, vap);
2088 frm = ieee80211_add_meshid(frm, vap);
2089 frm = ieee80211_add_meshpeer(frm,
2090 IEEE80211_MESH_PEER_LINK_CLOSE,
2091 args[0], args[1], args[2]);
2092 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2093 return mesh_send_action(ni, m);
2095 vap->iv_stats.is_tx_nobuf++;
2096 ieee80211_free_node(ni);
2102 mesh_send_action_meshlink_request(struct ieee80211_node *ni,
2103 int category, int action, void *arg0)
2105 struct ieee80211vap *vap = ni->ni_vap;
2106 struct ieee80211com *ic = ni->ni_ic;
2110 IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH, ni,
2111 "%s", "send LINK METRIC REQUEST action");
2113 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
2114 "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", __func__, __LINE__,
2115 ni, ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)+1);
2116 ieee80211_ref_node(ni);
2118 m = ieee80211_getmgtframe(&frm,
2119 ic->ic_headroom + sizeof(struct ieee80211_frame),
2120 sizeof(uint16_t) /* action+category */
2124 * mesh link metric request
2130 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2131 return mesh_send_action(ni, m);
2133 vap->iv_stats.is_tx_nobuf++;
2134 ieee80211_free_node(ni);
2140 mesh_send_action_meshlink_reply(struct ieee80211_node *ni,
2141 int category, int action, void *args0)
2143 struct ieee80211vap *vap = ni->ni_vap;
2144 struct ieee80211com *ic = ni->ni_ic;
2145 uint32_t *metric = args0;
2149 IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH, ni,
2150 "send LINK METRIC REPLY action: metric 0x%x", *metric);
2152 IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
2153 "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", __func__, __LINE__,
2154 ni, ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)+1);
2155 ieee80211_ref_node(ni);
2157 m = ieee80211_getmgtframe(&frm,
2158 ic->ic_headroom + sizeof(struct ieee80211_frame),
2159 sizeof(uint16_t) /* action+category */
2160 + sizeof(struct ieee80211_meshlmetric_ie)
2164 * mesh link metric reply
2167 * [tlv] mesh link metric
2171 frm = ieee80211_add_meshlmetric(frm, *metric);
2172 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2173 return mesh_send_action(ni, m);
2175 vap->iv_stats.is_tx_nobuf++;
2176 ieee80211_free_node(ni);
2182 mesh_peer_timeout_setup(struct ieee80211_node *ni)
2184 switch (ni->ni_mlstate) {
2185 case IEEE80211_NODE_MESH_HOLDING:
2186 ni->ni_mltval = ieee80211_mesh_holdingtimeout;
2188 case IEEE80211_NODE_MESH_CONFIRMRCV:
2189 ni->ni_mltval = ieee80211_mesh_confirmtimeout;
2191 case IEEE80211_NODE_MESH_IDLE:
2195 ni->ni_mltval = ieee80211_mesh_retrytimeout;
2199 callout_reset(&ni->ni_mltimer, ni->ni_mltval,
2200 mesh_peer_timeout_cb, ni);
2204 * Same as above but backoffs timer statisically 50%.
2207 mesh_peer_timeout_backoff(struct ieee80211_node *ni)
2212 ni->ni_mltval += r % ni->ni_mltval;
2213 callout_reset(&ni->ni_mltimer, ni->ni_mltval, mesh_peer_timeout_cb,
2217 static __inline void
2218 mesh_peer_timeout_stop(struct ieee80211_node *ni)
2220 callout_drain(&ni->ni_mltimer);
2224 * Mesh Peer Link Management FSM timeout handling.
2227 mesh_peer_timeout_cb(void *arg)
2229 struct ieee80211_node *ni = (struct ieee80211_node *)arg;
2232 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_MESH,
2233 ni, "mesh link timeout, state %d, retry counter %d",
2234 ni->ni_mlstate, ni->ni_mlrcnt);
2236 switch (ni->ni_mlstate) {
2237 case IEEE80211_NODE_MESH_IDLE:
2238 case IEEE80211_NODE_MESH_ESTABLISHED:
2240 case IEEE80211_NODE_MESH_OPENSNT:
2241 case IEEE80211_NODE_MESH_OPENRCV:
2242 if (ni->ni_mlrcnt == ieee80211_mesh_maxretries) {
2243 args[0] = ni->ni_mlpid;
2244 args[2] = IEEE80211_REASON_MESH_MAX_RETRIES;
2245 ieee80211_send_action(ni,
2246 IEEE80211_ACTION_CAT_MESHPEERING,
2247 IEEE80211_ACTION_MESHPEERING_CLOSE, args);
2249 mesh_linkchange(ni, IEEE80211_NODE_MESH_HOLDING);
2250 mesh_peer_timeout_setup(ni);
2252 args[0] = ni->ni_mlpid;
2253 ieee80211_send_action(ni,
2254 IEEE80211_ACTION_CAT_MESHPEERING,
2255 IEEE80211_ACTION_MESHPEERING_OPEN, args);
2257 mesh_peer_timeout_backoff(ni);
2260 case IEEE80211_NODE_MESH_CONFIRMRCV:
2261 if (ni->ni_mlrcnt == ieee80211_mesh_maxretries) {
2262 args[0] = ni->ni_mlpid;
2263 args[2] = IEEE80211_REASON_MESH_CONFIRM_TIMEOUT;
2264 ieee80211_send_action(ni,
2265 IEEE80211_ACTION_CAT_MESHPEERING,
2266 IEEE80211_ACTION_MESHPEERING_CLOSE, args);
2268 mesh_linkchange(ni, IEEE80211_NODE_MESH_HOLDING);
2269 mesh_peer_timeout_setup(ni);
2272 mesh_peer_timeout_setup(ni);
2275 case IEEE80211_NODE_MESH_HOLDING:
2276 mesh_linkchange(ni, IEEE80211_NODE_MESH_IDLE);
2282 mesh_verify_meshpeerver(struct ieee80211vap *vap, const uint8_t *ie)
2284 static const uint8_t peer[4] = IEEE80211_MESHPEERVER_PEER;
2285 const struct ieee80211_meshpeerver_ie *meshpeerver =
2286 (const struct ieee80211_meshpeerver_ie *) ie;
2288 if (meshpeerver->peerver_len !=
2289 sizeof(struct ieee80211_meshpeerver_ie) - 2)
2291 return memcmp(meshpeerver->peerver_proto, peer, 4);
2295 mesh_verify_meshid(struct ieee80211vap *vap, const uint8_t *ie)
2297 struct ieee80211_mesh_state *ms = vap->iv_mesh;
2299 if (ie == NULL || ie[1] != ms->ms_idlen)
2301 return memcmp(ms->ms_id, ie + 2, ms->ms_idlen);
2305 * Check if we are using the same algorithms for this mesh.
2308 mesh_verify_meshconf(struct ieee80211vap *vap, const uint8_t *ie)
2310 static const uint8_t null[4] = IEEE80211_MESHCONF_NULL;
2311 const struct ieee80211_meshconf_ie *meshconf =
2312 (const struct ieee80211_meshconf_ie *) ie;
2313 const struct ieee80211_mesh_state *ms = vap->iv_mesh;
2315 if (meshconf == NULL)
2317 if (meshconf->conf_ver != IEEE80211_MESHCONF_VERSION) {
2318 IEEE80211_DPRINTF(vap, IEEE80211_MSG_MESH,
2319 "wrong mesh conf version: %d\n", meshconf->conf_ver);
2322 if (memcmp(meshconf->conf_pselid, ms->ms_ppath->mpp_ie, 4) != 0) {
2323 IEEE80211_DPRINTF(vap, IEEE80211_MSG_MESH,
2324 "unknown path selection algorithm: 0x%x%x%x%x\n",
2325 meshconf->conf_pselid[0], meshconf->conf_pselid[1],
2326 meshconf->conf_pselid[2], meshconf->conf_pselid[3]);
2329 if (memcmp(meshconf->conf_pmetid, ms->ms_pmetric->mpm_ie, 4) != 0) {
2330 IEEE80211_DPRINTF(vap, IEEE80211_MSG_MESH,
2331 "unknown path metric algorithm: 0x%x%x%x%x\n",
2332 meshconf->conf_pmetid[0], meshconf->conf_pmetid[1],
2333 meshconf->conf_pmetid[2], meshconf->conf_pmetid[3]);
2336 if (memcmp(meshconf->conf_ccid, null, 4) != 0) {
2337 IEEE80211_DPRINTF(vap, IEEE80211_MSG_MESH,
2338 "unknown congestion sig algorithm: 0x%x%x%x%x\n",
2339 meshconf->conf_ccid[0], meshconf->conf_ccid[1],
2340 meshconf->conf_ccid[2], meshconf->conf_ccid[3]);
2343 if (memcmp(meshconf->conf_syncid, null, 4) != 0) {
2344 IEEE80211_DPRINTF(vap, IEEE80211_MSG_MESH,
2345 "unknown sync algorithm: 0x%x%x%x%x\n",
2346 meshconf->conf_syncid[0], meshconf->conf_syncid[1],
2347 meshconf->conf_syncid[2], meshconf->conf_syncid[3]);
2350 if (memcmp(meshconf->conf_authid, null, 4) != 0) {
2351 IEEE80211_DPRINTF(vap, IEEE80211_MSG_MESH,
2352 "unknown auth auth algorithm: 0x%x%x%x%x\n",
2353 meshconf->conf_pselid[0], meshconf->conf_pselid[1],
2354 meshconf->conf_pselid[2], meshconf->conf_pselid[3]);
2357 /* Not accepting peers */
2358 if (!(meshconf->conf_cap & IEEE80211_MESHCONF_CAP_AP)) {
2359 IEEE80211_DPRINTF(vap, IEEE80211_MSG_MESH,
2360 "not accepting peers: 0x%x\n", meshconf->conf_cap);
2367 mesh_verify_meshpeer(struct ieee80211vap *vap, const uint8_t *ie)
2369 const struct ieee80211_meshpeer_ie *meshpeer =
2370 (const struct ieee80211_meshpeer_ie *) ie;
2372 if (meshpeer == NULL)
2374 switch (meshpeer->peer_subtype) {
2375 case IEEE80211_MESH_PEER_LINK_OPEN:
2376 if (meshpeer->peer_len != 3)
2379 case IEEE80211_MESH_PEER_LINK_CONFIRM:
2380 if (meshpeer->peer_len != 5)
2383 case IEEE80211_MESH_PEER_LINK_CLOSE:
2384 if (meshpeer->peer_len < 5)
2386 if (meshpeer->peer_len == 5 && meshpeer->peer_linkid != 0)
2388 if (meshpeer->peer_rcode == 0)
2396 * Add a Mesh ID IE to a frame.
2399 ieee80211_add_meshid(uint8_t *frm, struct ieee80211vap *vap)
2401 struct ieee80211_mesh_state *ms = vap->iv_mesh;
2403 KASSERT(vap->iv_opmode == IEEE80211_M_MBSS, ("not a mbss vap"));
2405 *frm++ = IEEE80211_ELEMID_MESHID;
2406 *frm++ = ms->ms_idlen;
2407 memcpy(frm, ms->ms_id, ms->ms_idlen);
2408 return frm + ms->ms_idlen;
2412 * Add a Mesh Configuration IE to a frame.
2413 * For now just use HWMP routing, Airtime link metric, Null Congestion
2414 * Signaling, Null Sync Protocol and Null Authentication.
2417 ieee80211_add_meshconf(uint8_t *frm, struct ieee80211vap *vap)
2419 const struct ieee80211_mesh_state *ms = vap->iv_mesh;
2420 static const uint8_t null[4] = IEEE80211_MESHCONF_NULL;
2422 KASSERT(vap->iv_opmode == IEEE80211_M_MBSS, ("not a MBSS vap"));
2424 *frm++ = IEEE80211_ELEMID_MESHCONF;
2425 *frm++ = sizeof(struct ieee80211_meshconf_ie) - 2;
2426 *frm++ = IEEE80211_MESHCONF_VERSION;
2427 memcpy(frm, ms->ms_ppath->mpp_ie, 4); /* path selection */
2429 memcpy(frm, ms->ms_pmetric->mpm_ie, 4); /* link metric */
2431 /* XXX null for now */
2432 memcpy(frm, null, 4); /* congestion control */
2434 memcpy(frm, null, 4); /* sync */
2436 memcpy(frm, null, 4); /* auth */
2438 /* NB: set the number of neighbors before the rest */
2439 *frm = (ms->ms_neighbors > 15 ? 15 : ms->ms_neighbors) << 1;
2440 if (ms->ms_flags & IEEE80211_MESHFLAGS_PORTAL)
2441 *frm |= IEEE80211_MESHCONF_FORM_MP;
2443 if (ms->ms_flags & IEEE80211_MESHFLAGS_AP)
2444 *frm |= IEEE80211_MESHCONF_CAP_AP;
2445 if (ms->ms_flags & IEEE80211_MESHFLAGS_FWD)
2446 *frm |= IEEE80211_MESHCONF_CAP_FWRD;
2452 * Add a Mesh Peer Protocol IE to a frame.
2453 * XXX: needs to grow support for Abbreviated Handshake
2456 ieee80211_add_meshpeerver(uint8_t *frm, struct ieee80211vap *vap)
2458 static struct ieee80211_meshpeerver_ie ie = {
2459 .peerver_ie = IEEE80211_ELEMID_MESHPEERVER,
2461 .peerver_proto = IEEE80211_MESHPEERVER_PEER,
2464 KASSERT(vap->iv_opmode == IEEE80211_M_MBSS, ("not a MBSS vap"));
2466 memcpy(frm, &ie, sizeof(ie));
2467 return frm + sizeof(ie);
2471 * Add a Mesh Peer Management IE to a frame.
2474 ieee80211_add_meshpeer(uint8_t *frm, uint8_t subtype, uint16_t localid,
2475 uint16_t peerid, uint16_t reason)
2477 KASSERT(localid != 0, ("localid == 0"));
2479 *frm++ = IEEE80211_ELEMID_MESHPEER;
2481 case IEEE80211_MESH_PEER_LINK_OPEN:
2482 *frm++ = 3; /* length */
2484 ADDSHORT(frm, localid); /* local ID */
2486 case IEEE80211_MESH_PEER_LINK_CONFIRM:
2487 KASSERT(peerid != 0, ("sending peer confirm without peer id"));
2488 *frm++ = 5; /* length */
2490 ADDSHORT(frm, localid); /* local ID */
2491 ADDSHORT(frm, peerid); /* peer ID */
2493 case IEEE80211_MESH_PEER_LINK_CLOSE:
2495 *frm++ = 7; /* length */
2497 *frm++ = 5; /* length */
2499 ADDSHORT(frm, localid); /* local ID */
2501 ADDSHORT(frm, peerid); /* peer ID */
2502 ADDSHORT(frm, reason);
2509 * Compute an Airtime Link Metric for the link with this node.
2511 * Based on Draft 3.0 spec (11B.10, p.149).
2514 * Max 802.11s overhead.
2516 #define IEEE80211_MESH_MAXOVERHEAD \
2517 (sizeof(struct ieee80211_qosframe_addr4) \
2518 + sizeof(struct ieee80211_meshcntl_ae11) \
2519 + sizeof(struct llc) \
2520 + IEEE80211_ADDR_LEN \
2521 + IEEE80211_WEP_IVLEN \
2522 + IEEE80211_WEP_KIDLEN \
2523 + IEEE80211_WEP_CRCLEN \
2524 + IEEE80211_WEP_MICLEN \
2525 + IEEE80211_CRC_LEN)
2527 mesh_airtime_calc(struct ieee80211_node *ni)
2530 #define S_FACTOR (2 * M_BITS)
2531 struct ieee80211com *ic = ni->ni_ic;
2532 struct ifnet *ifp = ni->ni_vap->iv_ifp;
2533 const static int nbits = 8192 << M_BITS;
2534 uint32_t overhead, rate, errrate;
2537 /* Time to transmit a frame */
2538 rate = ni->ni_txrate;
2539 overhead = ieee80211_compute_duration(ic->ic_rt,
2540 ifp->if_mtu + IEEE80211_MESH_MAXOVERHEAD, rate, 0) << M_BITS;
2541 /* Error rate in percentage */
2542 /* XXX assuming small failures are ok */
2543 errrate = (((ifp->if_oerrors +
2544 ifp->if_ierrors) / 100) << M_BITS) / 100;
2545 res = (overhead + (nbits / rate)) *
2546 ((1 << S_FACTOR) / ((1 << M_BITS) - errrate));
2548 return (uint32_t)(res >> S_FACTOR);
2554 * Add a Mesh Link Metric report IE to a frame.
2557 ieee80211_add_meshlmetric(uint8_t *frm, uint32_t metric)
2559 *frm++ = IEEE80211_ELEMID_MESHLINK;
2561 ADDWORD(frm, metric);
2568 * Initialize any mesh-specific node state.
2571 ieee80211_mesh_node_init(struct ieee80211vap *vap, struct ieee80211_node *ni)
2573 ni->ni_flags |= IEEE80211_NODE_QOS;
2574 callout_init(&ni->ni_mltimer, CALLOUT_MPSAFE);
2578 * Cleanup any mesh-specific node state.
2581 ieee80211_mesh_node_cleanup(struct ieee80211_node *ni)
2583 struct ieee80211vap *vap = ni->ni_vap;
2584 struct ieee80211_mesh_state *ms = vap->iv_mesh;
2586 callout_drain(&ni->ni_mltimer);
2587 /* NB: short-circuit callbacks after mesh_vdetach */
2588 if (vap->iv_mesh != NULL)
2589 ms->ms_ppath->mpp_peerdown(ni);
2593 ieee80211_parse_meshid(struct ieee80211_node *ni, const uint8_t *ie)
2595 ni->ni_meshidlen = ie[1];
2596 memcpy(ni->ni_meshid, ie + 2, ie[1]);
2600 * Setup mesh-specific node state on neighbor discovery.
2603 ieee80211_mesh_init_neighbor(struct ieee80211_node *ni,
2604 const struct ieee80211_frame *wh,
2605 const struct ieee80211_scanparams *sp)
2607 ieee80211_parse_meshid(ni, sp->meshid);
2611 mesh_ioctl_get80211(struct ieee80211vap *vap, struct ieee80211req *ireq)
2613 struct ieee80211_mesh_state *ms = vap->iv_mesh;
2614 uint8_t tmpmeshid[IEEE80211_NWID_LEN];
2615 struct ieee80211_mesh_route *rt;
2616 struct ieee80211req_mesh_route *imr;
2621 if (vap->iv_opmode != IEEE80211_M_MBSS)
2625 switch (ireq->i_type) {
2626 case IEEE80211_IOC_MESH_ID:
2627 ireq->i_len = ms->ms_idlen;
2628 memcpy(tmpmeshid, ms->ms_id, ireq->i_len);
2629 error = copyout(tmpmeshid, ireq->i_data, ireq->i_len);
2631 case IEEE80211_IOC_MESH_AP:
2632 ireq->i_val = (ms->ms_flags & IEEE80211_MESHFLAGS_AP) != 0;
2634 case IEEE80211_IOC_MESH_FWRD:
2635 ireq->i_val = (ms->ms_flags & IEEE80211_MESHFLAGS_FWD) != 0;
2637 case IEEE80211_IOC_MESH_TTL:
2638 ireq->i_val = ms->ms_ttl;
2640 case IEEE80211_IOC_MESH_RTCMD:
2641 switch (ireq->i_val) {
2642 case IEEE80211_MESH_RTCMD_LIST:
2645 TAILQ_FOREACH(rt, &ms->ms_routes, rt_next) {
2646 len += sizeof(*imr);
2649 if (len > ireq->i_len || ireq->i_len < sizeof(*imr)) {
2655 p = malloc(len, M_TEMP, M_NOWAIT | M_ZERO);
2660 TAILQ_FOREACH(rt, &ms->ms_routes, rt_next) {
2663 imr = (struct ieee80211req_mesh_route *)
2665 imr->imr_flags = rt->rt_flags;
2666 IEEE80211_ADDR_COPY(imr->imr_dest,
2668 IEEE80211_ADDR_COPY(imr->imr_nexthop,
2670 imr->imr_metric = rt->rt_metric;
2671 imr->imr_nhops = rt->rt_nhops;
2672 imr->imr_lifetime = rt->rt_lifetime;
2673 imr->imr_lastmseq = rt->rt_lastmseq;
2674 off += sizeof(*imr);
2677 error = copyout(p, (uint8_t *)ireq->i_data,
2681 case IEEE80211_MESH_RTCMD_FLUSH:
2682 case IEEE80211_MESH_RTCMD_ADD:
2683 case IEEE80211_MESH_RTCMD_DELETE:
2689 case IEEE80211_IOC_MESH_PR_METRIC:
2690 len = strlen(ms->ms_pmetric->mpm_descr);
2691 if (ireq->i_len < len)
2694 error = copyout(ms->ms_pmetric->mpm_descr,
2695 (uint8_t *)ireq->i_data, len);
2697 case IEEE80211_IOC_MESH_PR_PATH:
2698 len = strlen(ms->ms_ppath->mpp_descr);
2699 if (ireq->i_len < len)
2702 error = copyout(ms->ms_ppath->mpp_descr,
2703 (uint8_t *)ireq->i_data, len);
2711 IEEE80211_IOCTL_GET(mesh, mesh_ioctl_get80211);
2714 mesh_ioctl_set80211(struct ieee80211vap *vap, struct ieee80211req *ireq)
2716 struct ieee80211_mesh_state *ms = vap->iv_mesh;
2717 uint8_t tmpmeshid[IEEE80211_NWID_LEN];
2718 uint8_t tmpaddr[IEEE80211_ADDR_LEN];
2719 char tmpproto[IEEE80211_MESH_PROTO_DSZ];
2722 if (vap->iv_opmode != IEEE80211_M_MBSS)
2726 switch (ireq->i_type) {
2727 case IEEE80211_IOC_MESH_ID:
2728 if (ireq->i_val != 0 || ireq->i_len > IEEE80211_MESHID_LEN)
2730 error = copyin(ireq->i_data, tmpmeshid, ireq->i_len);
2733 memset(ms->ms_id, 0, IEEE80211_NWID_LEN);
2734 ms->ms_idlen = ireq->i_len;
2735 memcpy(ms->ms_id, tmpmeshid, ireq->i_len);
2738 case IEEE80211_IOC_MESH_AP:
2740 ms->ms_flags |= IEEE80211_MESHFLAGS_AP;
2742 ms->ms_flags &= ~IEEE80211_MESHFLAGS_AP;
2745 case IEEE80211_IOC_MESH_FWRD:
2747 ms->ms_flags |= IEEE80211_MESHFLAGS_FWD;
2749 ms->ms_flags &= ~IEEE80211_MESHFLAGS_FWD;
2751 case IEEE80211_IOC_MESH_TTL:
2752 ms->ms_ttl = (uint8_t) ireq->i_val;
2754 case IEEE80211_IOC_MESH_RTCMD:
2755 switch (ireq->i_val) {
2756 case IEEE80211_MESH_RTCMD_LIST:
2758 case IEEE80211_MESH_RTCMD_FLUSH:
2759 ieee80211_mesh_rt_flush(vap);
2761 case IEEE80211_MESH_RTCMD_ADD:
2762 if (IEEE80211_ADDR_EQ(vap->iv_myaddr, ireq->i_data) ||
2763 IEEE80211_ADDR_EQ(broadcastaddr, ireq->i_data))
2765 error = copyin(ireq->i_data, &tmpaddr,
2766 IEEE80211_ADDR_LEN);
2768 ieee80211_mesh_discover(vap, tmpaddr, NULL);
2770 case IEEE80211_MESH_RTCMD_DELETE:
2771 ieee80211_mesh_rt_del(vap, ireq->i_data);
2777 case IEEE80211_IOC_MESH_PR_METRIC:
2778 error = copyin(ireq->i_data, tmpproto, sizeof(tmpproto));
2780 error = mesh_select_proto_metric(vap, tmpproto);
2785 case IEEE80211_IOC_MESH_PR_PATH:
2786 error = copyin(ireq->i_data, tmpproto, sizeof(tmpproto));
2788 error = mesh_select_proto_path(vap, tmpproto);
2798 IEEE80211_IOCTL_SET(mesh, mesh_ioctl_set80211);