xref: /haiku/src/libs/compat/freebsd_wlan/net80211/ieee80211_mesh.c (revision da3ca31854ae1f34c46d4d29e44837da03d8cafa)
1753c7e08SAugustin Cavalier /*-
286021fd4SAugustin Cavalier  * SPDX-License-Identifier: BSD-2-Clause
38244a9baSAugustin Cavalier  *
4753c7e08SAugustin Cavalier  * Copyright (c) 2009 The FreeBSD Foundation
5753c7e08SAugustin Cavalier  *
6753c7e08SAugustin Cavalier  * This software was developed by Rui Paulo under sponsorship from the
7753c7e08SAugustin Cavalier  * FreeBSD Foundation.
8753c7e08SAugustin Cavalier  *
9753c7e08SAugustin Cavalier  * Redistribution and use in source and binary forms, with or without
10753c7e08SAugustin Cavalier  * modification, are permitted provided that the following conditions
11753c7e08SAugustin Cavalier  * are met:
12753c7e08SAugustin Cavalier  * 1. Redistributions of source code must retain the above copyright
13753c7e08SAugustin Cavalier  *    notice, this list of conditions and the following disclaimer.
14753c7e08SAugustin Cavalier  * 2. Redistributions in binary form must reproduce the above copyright
15753c7e08SAugustin Cavalier  *    notice, this list of conditions and the following disclaimer in the
16753c7e08SAugustin Cavalier  *    documentation and/or other materials provided with the distribution.
17753c7e08SAugustin Cavalier  *
18753c7e08SAugustin Cavalier  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19753c7e08SAugustin Cavalier  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20753c7e08SAugustin Cavalier  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21753c7e08SAugustin Cavalier  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22753c7e08SAugustin Cavalier  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23753c7e08SAugustin Cavalier  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24753c7e08SAugustin Cavalier  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25753c7e08SAugustin Cavalier  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26753c7e08SAugustin Cavalier  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27753c7e08SAugustin Cavalier  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28753c7e08SAugustin Cavalier  * SUCH DAMAGE.
29753c7e08SAugustin Cavalier  */
30753c7e08SAugustin Cavalier 
31753c7e08SAugustin Cavalier /*
32753c7e08SAugustin Cavalier  * IEEE 802.11s Mesh Point (MBSS) support.
33753c7e08SAugustin Cavalier  *
34753c7e08SAugustin Cavalier  * Based on March 2009, D3.0 802.11s draft spec.
35753c7e08SAugustin Cavalier  */
36753c7e08SAugustin Cavalier #include "opt_inet.h"
37753c7e08SAugustin Cavalier #include "opt_wlan.h"
38753c7e08SAugustin Cavalier 
39753c7e08SAugustin Cavalier #include <sys/param.h>
40753c7e08SAugustin Cavalier #include <sys/systm.h>
41753c7e08SAugustin Cavalier #include <sys/mbuf.h>
42753c7e08SAugustin Cavalier #include <sys/malloc.h>
43753c7e08SAugustin Cavalier #include <sys/kernel.h>
44753c7e08SAugustin Cavalier 
45753c7e08SAugustin Cavalier #include <sys/socket.h>
46753c7e08SAugustin Cavalier #include <sys/sockio.h>
47753c7e08SAugustin Cavalier #include <sys/endian.h>
48753c7e08SAugustin Cavalier #include <sys/errno.h>
49753c7e08SAugustin Cavalier #include <sys/proc.h>
50753c7e08SAugustin Cavalier #include <sys/sysctl.h>
51753c7e08SAugustin Cavalier 
52753c7e08SAugustin Cavalier #include <net/bpf.h>
53753c7e08SAugustin Cavalier #include <net/if.h>
54753c7e08SAugustin Cavalier #include <net/if_var.h>
55753c7e08SAugustin Cavalier #include <net/if_media.h>
56753c7e08SAugustin Cavalier #include <net/if_llc.h>
5786021fd4SAugustin Cavalier #include <net/if_private.h>
58753c7e08SAugustin Cavalier #include <net/ethernet.h>
59753c7e08SAugustin Cavalier 
60753c7e08SAugustin Cavalier #include <net80211/ieee80211_var.h>
61753c7e08SAugustin Cavalier #include <net80211/ieee80211_action.h>
62753c7e08SAugustin Cavalier #ifdef IEEE80211_SUPPORT_SUPERG
63753c7e08SAugustin Cavalier #include <net80211/ieee80211_superg.h>
64753c7e08SAugustin Cavalier #endif
65753c7e08SAugustin Cavalier #include <net80211/ieee80211_input.h>
66753c7e08SAugustin Cavalier #include <net80211/ieee80211_mesh.h>
67753c7e08SAugustin Cavalier 
68753c7e08SAugustin Cavalier static void	mesh_rt_flush_invalid(struct ieee80211vap *);
69753c7e08SAugustin Cavalier static int	mesh_select_proto_path(struct ieee80211vap *, const char *);
70753c7e08SAugustin Cavalier static int	mesh_select_proto_metric(struct ieee80211vap *, const char *);
71753c7e08SAugustin Cavalier static void	mesh_vattach(struct ieee80211vap *);
72753c7e08SAugustin Cavalier static int	mesh_newstate(struct ieee80211vap *, enum ieee80211_state, int);
73753c7e08SAugustin Cavalier static void	mesh_rt_cleanup_cb(void *);
74753c7e08SAugustin Cavalier static void	mesh_gatemode_setup(struct ieee80211vap *);
75753c7e08SAugustin Cavalier static void	mesh_gatemode_cb(void *);
76753c7e08SAugustin Cavalier static void	mesh_linkchange(struct ieee80211_node *,
77753c7e08SAugustin Cavalier 		    enum ieee80211_mesh_mlstate);
78753c7e08SAugustin Cavalier static void	mesh_checkid(void *, struct ieee80211_node *);
79753c7e08SAugustin Cavalier static uint32_t	mesh_generateid(struct ieee80211vap *);
80753c7e08SAugustin Cavalier static int	mesh_checkpseq(struct ieee80211vap *,
81753c7e08SAugustin Cavalier 		    const uint8_t [IEEE80211_ADDR_LEN], uint32_t);
82753c7e08SAugustin Cavalier static void	mesh_transmit_to_gate(struct ieee80211vap *, struct mbuf *,
83753c7e08SAugustin Cavalier 		    struct ieee80211_mesh_route *);
84753c7e08SAugustin Cavalier static void	mesh_forward(struct ieee80211vap *, struct mbuf *,
85753c7e08SAugustin Cavalier 		    const struct ieee80211_meshcntl *);
86753c7e08SAugustin Cavalier static int	mesh_input(struct ieee80211_node *, struct mbuf *,
87753c7e08SAugustin Cavalier 		    const struct ieee80211_rx_stats *rxs, int, int);
88753c7e08SAugustin Cavalier static void	mesh_recv_mgmt(struct ieee80211_node *, struct mbuf *, int,
89753c7e08SAugustin Cavalier 		    const struct ieee80211_rx_stats *rxs, int, int);
90753c7e08SAugustin Cavalier static void	mesh_recv_ctl(struct ieee80211_node *, struct mbuf *, int);
91753c7e08SAugustin Cavalier static void	mesh_peer_timeout_setup(struct ieee80211_node *);
92753c7e08SAugustin Cavalier static void	mesh_peer_timeout_backoff(struct ieee80211_node *);
93753c7e08SAugustin Cavalier static void	mesh_peer_timeout_cb(void *);
94753c7e08SAugustin Cavalier static __inline void
95753c7e08SAugustin Cavalier 		mesh_peer_timeout_stop(struct ieee80211_node *);
96753c7e08SAugustin Cavalier static int	mesh_verify_meshid(struct ieee80211vap *, const uint8_t *);
97753c7e08SAugustin Cavalier static int	mesh_verify_meshconf(struct ieee80211vap *, const uint8_t *);
98753c7e08SAugustin Cavalier static int	mesh_verify_meshpeer(struct ieee80211vap *, uint8_t,
99753c7e08SAugustin Cavalier     		    const uint8_t *);
100753c7e08SAugustin Cavalier uint32_t	mesh_airtime_calc(struct ieee80211_node *);
101753c7e08SAugustin Cavalier 
102753c7e08SAugustin Cavalier /*
103753c7e08SAugustin Cavalier  * Timeout values come from the specification and are in milliseconds.
104753c7e08SAugustin Cavalier  */
1055cad57c4SJérôme Duval static SYSCTL_NODE(_net_wlan, OID_AUTO, mesh, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
106753c7e08SAugustin Cavalier     "IEEE 802.11s parameters");
107753c7e08SAugustin Cavalier static int	ieee80211_mesh_gateint = -1;
1085cad57c4SJérôme Duval SYSCTL_PROC(_net_wlan_mesh, OID_AUTO, gateint,
1095cad57c4SJérôme Duval     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
110753c7e08SAugustin Cavalier     &ieee80211_mesh_gateint, 0, ieee80211_sysctl_msecs_ticks, "I",
111753c7e08SAugustin Cavalier     "mesh gate interval (ms)");
112753c7e08SAugustin Cavalier static int ieee80211_mesh_retrytimeout = -1;
1135cad57c4SJérôme Duval SYSCTL_PROC(_net_wlan_mesh, OID_AUTO, retrytimeout,
1145cad57c4SJérôme Duval     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
115753c7e08SAugustin Cavalier     &ieee80211_mesh_retrytimeout, 0, ieee80211_sysctl_msecs_ticks, "I",
116753c7e08SAugustin Cavalier     "Retry timeout (msec)");
117753c7e08SAugustin Cavalier static int ieee80211_mesh_holdingtimeout = -1;
118753c7e08SAugustin Cavalier 
1195cad57c4SJérôme Duval SYSCTL_PROC(_net_wlan_mesh, OID_AUTO, holdingtimeout,
1205cad57c4SJérôme Duval     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
121753c7e08SAugustin Cavalier     &ieee80211_mesh_holdingtimeout, 0, ieee80211_sysctl_msecs_ticks, "I",
122753c7e08SAugustin Cavalier     "Holding state timeout (msec)");
123753c7e08SAugustin Cavalier static int ieee80211_mesh_confirmtimeout = -1;
1245cad57c4SJérôme Duval SYSCTL_PROC(_net_wlan_mesh, OID_AUTO, confirmtimeout,
1255cad57c4SJérôme Duval     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
126753c7e08SAugustin Cavalier     &ieee80211_mesh_confirmtimeout, 0, ieee80211_sysctl_msecs_ticks, "I",
127753c7e08SAugustin Cavalier     "Confirm state timeout (msec)");
128753c7e08SAugustin Cavalier static int ieee80211_mesh_backofftimeout = -1;
1295cad57c4SJérôme Duval SYSCTL_PROC(_net_wlan_mesh, OID_AUTO, backofftimeout,
1305cad57c4SJérôme Duval     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
131753c7e08SAugustin Cavalier     &ieee80211_mesh_backofftimeout, 0, ieee80211_sysctl_msecs_ticks, "I",
132753c7e08SAugustin Cavalier     "Backoff timeout (msec). This is to throutles peering forever when "
133753c7e08SAugustin Cavalier     "not receiving answer or is rejected by a neighbor");
134753c7e08SAugustin Cavalier static int ieee80211_mesh_maxretries = 2;
135753c7e08SAugustin Cavalier SYSCTL_INT(_net_wlan_mesh, OID_AUTO, maxretries, CTLFLAG_RW,
136753c7e08SAugustin Cavalier     &ieee80211_mesh_maxretries, 0,
137753c7e08SAugustin Cavalier     "Maximum retries during peer link establishment");
138753c7e08SAugustin Cavalier static int ieee80211_mesh_maxholding = 2;
139753c7e08SAugustin Cavalier SYSCTL_INT(_net_wlan_mesh, OID_AUTO, maxholding, CTLFLAG_RW,
140753c7e08SAugustin Cavalier     &ieee80211_mesh_maxholding, 0,
141753c7e08SAugustin Cavalier     "Maximum times we are allowed to transition to HOLDING state before "
142753c7e08SAugustin Cavalier     "backinoff during peer link establishment");
143753c7e08SAugustin Cavalier 
144753c7e08SAugustin Cavalier static const uint8_t broadcastaddr[IEEE80211_ADDR_LEN] =
145753c7e08SAugustin Cavalier 	{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
146753c7e08SAugustin Cavalier 
147753c7e08SAugustin Cavalier static	ieee80211_recv_action_func mesh_recv_action_meshpeering_open;
148753c7e08SAugustin Cavalier static	ieee80211_recv_action_func mesh_recv_action_meshpeering_confirm;
149753c7e08SAugustin Cavalier static	ieee80211_recv_action_func mesh_recv_action_meshpeering_close;
150753c7e08SAugustin Cavalier static	ieee80211_recv_action_func mesh_recv_action_meshlmetric;
151753c7e08SAugustin Cavalier static	ieee80211_recv_action_func mesh_recv_action_meshgate;
152753c7e08SAugustin Cavalier 
153753c7e08SAugustin Cavalier static	ieee80211_send_action_func mesh_send_action_meshpeering_open;
154753c7e08SAugustin Cavalier static	ieee80211_send_action_func mesh_send_action_meshpeering_confirm;
155753c7e08SAugustin Cavalier static	ieee80211_send_action_func mesh_send_action_meshpeering_close;
156753c7e08SAugustin Cavalier static	ieee80211_send_action_func mesh_send_action_meshlmetric;
157753c7e08SAugustin Cavalier static	ieee80211_send_action_func mesh_send_action_meshgate;
158753c7e08SAugustin Cavalier 
159753c7e08SAugustin Cavalier static const struct ieee80211_mesh_proto_metric mesh_metric_airtime = {
160753c7e08SAugustin Cavalier 	.mpm_descr	= "AIRTIME",
161753c7e08SAugustin Cavalier 	.mpm_ie		= IEEE80211_MESHCONF_METRIC_AIRTIME,
162753c7e08SAugustin Cavalier 	.mpm_metric	= mesh_airtime_calc,
163753c7e08SAugustin Cavalier };
164753c7e08SAugustin Cavalier 
165753c7e08SAugustin Cavalier static struct ieee80211_mesh_proto_path		mesh_proto_paths[4];
166753c7e08SAugustin Cavalier static struct ieee80211_mesh_proto_metric	mesh_proto_metrics[4];
167753c7e08SAugustin Cavalier 
168753c7e08SAugustin Cavalier MALLOC_DEFINE(M_80211_MESH_PREQ, "80211preq", "802.11 MESH Path Request frame");
169753c7e08SAugustin Cavalier MALLOC_DEFINE(M_80211_MESH_PREP, "80211prep", "802.11 MESH Path Reply frame");
170753c7e08SAugustin Cavalier MALLOC_DEFINE(M_80211_MESH_PERR, "80211perr", "802.11 MESH Path Error frame");
171753c7e08SAugustin Cavalier 
172753c7e08SAugustin Cavalier /* The longer one of the lifetime should be stored as new lifetime */
173753c7e08SAugustin Cavalier #define MESH_ROUTE_LIFETIME_MAX(a, b)	(a > b ? a : b)
174753c7e08SAugustin Cavalier 
175753c7e08SAugustin Cavalier MALLOC_DEFINE(M_80211_MESH_RT, "80211mesh_rt", "802.11s routing table");
176753c7e08SAugustin Cavalier MALLOC_DEFINE(M_80211_MESH_GT_RT, "80211mesh_gt", "802.11s known gates table");
177753c7e08SAugustin Cavalier 
178753c7e08SAugustin Cavalier /*
179753c7e08SAugustin Cavalier  * Helper functions to manipulate the Mesh routing table.
180753c7e08SAugustin Cavalier  */
181753c7e08SAugustin Cavalier 
182753c7e08SAugustin Cavalier static struct ieee80211_mesh_route *
mesh_rt_find_locked(struct ieee80211_mesh_state * ms,const uint8_t dest[IEEE80211_ADDR_LEN])183753c7e08SAugustin Cavalier mesh_rt_find_locked(struct ieee80211_mesh_state *ms,
184753c7e08SAugustin Cavalier     const uint8_t dest[IEEE80211_ADDR_LEN])
185753c7e08SAugustin Cavalier {
186753c7e08SAugustin Cavalier 	struct ieee80211_mesh_route *rt;
187753c7e08SAugustin Cavalier 
188753c7e08SAugustin Cavalier 	MESH_RT_LOCK_ASSERT(ms);
189753c7e08SAugustin Cavalier 
190753c7e08SAugustin Cavalier 	TAILQ_FOREACH(rt, &ms->ms_routes, rt_next) {
191753c7e08SAugustin Cavalier 		if (IEEE80211_ADDR_EQ(dest, rt->rt_dest))
192753c7e08SAugustin Cavalier 			return rt;
193753c7e08SAugustin Cavalier 	}
194753c7e08SAugustin Cavalier 	return NULL;
195753c7e08SAugustin Cavalier }
196753c7e08SAugustin Cavalier 
197753c7e08SAugustin Cavalier static struct ieee80211_mesh_route *
mesh_rt_add_locked(struct ieee80211vap * vap,const uint8_t dest[IEEE80211_ADDR_LEN])198753c7e08SAugustin Cavalier mesh_rt_add_locked(struct ieee80211vap *vap,
199753c7e08SAugustin Cavalier     const uint8_t dest[IEEE80211_ADDR_LEN])
200753c7e08SAugustin Cavalier {
201753c7e08SAugustin Cavalier 	struct ieee80211_mesh_state *ms = vap->iv_mesh;
202753c7e08SAugustin Cavalier 	struct ieee80211_mesh_route *rt;
203753c7e08SAugustin Cavalier 
204753c7e08SAugustin Cavalier 	KASSERT(!IEEE80211_ADDR_EQ(broadcastaddr, dest),
205753c7e08SAugustin Cavalier 	    ("%s: adding broadcast to the routing table", __func__));
206753c7e08SAugustin Cavalier 
207753c7e08SAugustin Cavalier 	MESH_RT_LOCK_ASSERT(ms);
208753c7e08SAugustin Cavalier 
209753c7e08SAugustin Cavalier 	rt = IEEE80211_MALLOC(ALIGN(sizeof(struct ieee80211_mesh_route)) +
210753c7e08SAugustin Cavalier 	    ms->ms_ppath->mpp_privlen, M_80211_MESH_RT,
211753c7e08SAugustin Cavalier 	    IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
212753c7e08SAugustin Cavalier 	if (rt != NULL) {
213753c7e08SAugustin Cavalier 		rt->rt_vap = vap;
214753c7e08SAugustin Cavalier 		IEEE80211_ADDR_COPY(rt->rt_dest, dest);
215753c7e08SAugustin Cavalier 		rt->rt_priv = (void *)ALIGN(&rt[1]);
216753c7e08SAugustin Cavalier 		MESH_RT_ENTRY_LOCK_INIT(rt, "MBSS_RT");
217753c7e08SAugustin Cavalier 		callout_init(&rt->rt_discovery, 1);
218753c7e08SAugustin Cavalier 		rt->rt_updtime = ticks;	/* create time */
219753c7e08SAugustin Cavalier 		TAILQ_INSERT_TAIL(&ms->ms_routes, rt, rt_next);
220753c7e08SAugustin Cavalier 	}
221753c7e08SAugustin Cavalier 	return rt;
222753c7e08SAugustin Cavalier }
223753c7e08SAugustin Cavalier 
224753c7e08SAugustin Cavalier struct ieee80211_mesh_route *
ieee80211_mesh_rt_find(struct ieee80211vap * vap,const uint8_t dest[IEEE80211_ADDR_LEN])225753c7e08SAugustin Cavalier ieee80211_mesh_rt_find(struct ieee80211vap *vap,
226753c7e08SAugustin Cavalier     const uint8_t dest[IEEE80211_ADDR_LEN])
227753c7e08SAugustin Cavalier {
228753c7e08SAugustin Cavalier 	struct ieee80211_mesh_state *ms = vap->iv_mesh;
229753c7e08SAugustin Cavalier 	struct ieee80211_mesh_route *rt;
230753c7e08SAugustin Cavalier 
231753c7e08SAugustin Cavalier 	MESH_RT_LOCK(ms);
232753c7e08SAugustin Cavalier 	rt = mesh_rt_find_locked(ms, dest);
233753c7e08SAugustin Cavalier 	MESH_RT_UNLOCK(ms);
234753c7e08SAugustin Cavalier 	return rt;
235753c7e08SAugustin Cavalier }
236753c7e08SAugustin Cavalier 
237753c7e08SAugustin Cavalier struct ieee80211_mesh_route *
ieee80211_mesh_rt_add(struct ieee80211vap * vap,const uint8_t dest[IEEE80211_ADDR_LEN])238753c7e08SAugustin Cavalier ieee80211_mesh_rt_add(struct ieee80211vap *vap,
239753c7e08SAugustin Cavalier     const uint8_t dest[IEEE80211_ADDR_LEN])
240753c7e08SAugustin Cavalier {
241753c7e08SAugustin Cavalier 	struct ieee80211_mesh_state *ms = vap->iv_mesh;
242753c7e08SAugustin Cavalier 	struct ieee80211_mesh_route *rt;
243753c7e08SAugustin Cavalier 
244753c7e08SAugustin Cavalier 	KASSERT(ieee80211_mesh_rt_find(vap, dest) == NULL,
245753c7e08SAugustin Cavalier 	    ("%s: duplicate entry in the routing table", __func__));
246753c7e08SAugustin Cavalier 	KASSERT(!IEEE80211_ADDR_EQ(vap->iv_myaddr, dest),
247753c7e08SAugustin Cavalier 	    ("%s: adding self to the routing table", __func__));
248753c7e08SAugustin Cavalier 
249753c7e08SAugustin Cavalier 	MESH_RT_LOCK(ms);
250753c7e08SAugustin Cavalier 	rt = mesh_rt_add_locked(vap, dest);
251753c7e08SAugustin Cavalier 	MESH_RT_UNLOCK(ms);
252753c7e08SAugustin Cavalier 	return rt;
253753c7e08SAugustin Cavalier }
254753c7e08SAugustin Cavalier 
255753c7e08SAugustin Cavalier /*
256753c7e08SAugustin Cavalier  * Update the route lifetime and returns the updated lifetime.
257753c7e08SAugustin Cavalier  * If new_lifetime is zero and route is timedout it will be invalidated.
258753c7e08SAugustin Cavalier  * new_lifetime is in msec
259753c7e08SAugustin Cavalier  */
260753c7e08SAugustin Cavalier int
ieee80211_mesh_rt_update(struct ieee80211_mesh_route * rt,int new_lifetime)261753c7e08SAugustin Cavalier ieee80211_mesh_rt_update(struct ieee80211_mesh_route *rt, int new_lifetime)
262753c7e08SAugustin Cavalier {
263753c7e08SAugustin Cavalier 	int timesince, now;
264753c7e08SAugustin Cavalier 	uint32_t lifetime = 0;
265753c7e08SAugustin Cavalier 
266753c7e08SAugustin Cavalier 	KASSERT(rt != NULL, ("route is NULL"));
267753c7e08SAugustin Cavalier 
268753c7e08SAugustin Cavalier 	now = ticks;
269753c7e08SAugustin Cavalier 	MESH_RT_ENTRY_LOCK(rt);
270753c7e08SAugustin Cavalier 
271753c7e08SAugustin Cavalier 	/* dont clobber a proxy entry gated by us */
272753c7e08SAugustin Cavalier 	if (rt->rt_flags & IEEE80211_MESHRT_FLAGS_PROXY && rt->rt_nhops == 0) {
273753c7e08SAugustin Cavalier 		MESH_RT_ENTRY_UNLOCK(rt);
274753c7e08SAugustin Cavalier 		return rt->rt_lifetime;
275753c7e08SAugustin Cavalier 	}
276753c7e08SAugustin Cavalier 
277753c7e08SAugustin Cavalier 	timesince = ticks_to_msecs(now - rt->rt_updtime);
278753c7e08SAugustin Cavalier 	rt->rt_updtime = now;
279753c7e08SAugustin Cavalier 	if (timesince >= rt->rt_lifetime) {
280753c7e08SAugustin Cavalier 		if (new_lifetime != 0) {
281753c7e08SAugustin Cavalier 			rt->rt_lifetime = new_lifetime;
282753c7e08SAugustin Cavalier 		}
283753c7e08SAugustin Cavalier 		else {
284753c7e08SAugustin Cavalier 			rt->rt_flags &= ~IEEE80211_MESHRT_FLAGS_VALID;
285753c7e08SAugustin Cavalier 			rt->rt_lifetime = 0;
286753c7e08SAugustin Cavalier 		}
287753c7e08SAugustin Cavalier 	} else {
288753c7e08SAugustin Cavalier 		/* update what is left of lifetime */
289753c7e08SAugustin Cavalier 		rt->rt_lifetime = rt->rt_lifetime - timesince;
290753c7e08SAugustin Cavalier 		rt->rt_lifetime  = MESH_ROUTE_LIFETIME_MAX(
291753c7e08SAugustin Cavalier 			new_lifetime, rt->rt_lifetime);
292753c7e08SAugustin Cavalier 	}
293753c7e08SAugustin Cavalier 	lifetime = rt->rt_lifetime;
294753c7e08SAugustin Cavalier 	MESH_RT_ENTRY_UNLOCK(rt);
295753c7e08SAugustin Cavalier 
296753c7e08SAugustin Cavalier 	return lifetime;
297753c7e08SAugustin Cavalier }
298753c7e08SAugustin Cavalier 
299753c7e08SAugustin Cavalier /*
300753c7e08SAugustin Cavalier  * Add a proxy route (as needed) for the specified destination.
301753c7e08SAugustin Cavalier  */
302753c7e08SAugustin Cavalier void
ieee80211_mesh_proxy_check(struct ieee80211vap * vap,const uint8_t dest[IEEE80211_ADDR_LEN])303753c7e08SAugustin Cavalier ieee80211_mesh_proxy_check(struct ieee80211vap *vap,
304753c7e08SAugustin Cavalier     const uint8_t dest[IEEE80211_ADDR_LEN])
305753c7e08SAugustin Cavalier {
306753c7e08SAugustin Cavalier 	struct ieee80211_mesh_state *ms = vap->iv_mesh;
307753c7e08SAugustin Cavalier 	struct ieee80211_mesh_route *rt;
308753c7e08SAugustin Cavalier 
309753c7e08SAugustin Cavalier 	MESH_RT_LOCK(ms);
310753c7e08SAugustin Cavalier 	rt = mesh_rt_find_locked(ms, dest);
311753c7e08SAugustin Cavalier 	if (rt == NULL) {
312753c7e08SAugustin Cavalier 		rt = mesh_rt_add_locked(vap, dest);
313753c7e08SAugustin Cavalier 		if (rt == NULL) {
314753c7e08SAugustin Cavalier 			IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH, dest,
315753c7e08SAugustin Cavalier 			    "%s", "unable to add proxy entry");
316753c7e08SAugustin Cavalier 			vap->iv_stats.is_mesh_rtaddfailed++;
317753c7e08SAugustin Cavalier 		} else {
318753c7e08SAugustin Cavalier 			IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH, dest,
319753c7e08SAugustin Cavalier 			    "%s", "add proxy entry");
320753c7e08SAugustin Cavalier 			IEEE80211_ADDR_COPY(rt->rt_mesh_gate, vap->iv_myaddr);
321753c7e08SAugustin Cavalier 			IEEE80211_ADDR_COPY(rt->rt_nexthop, vap->iv_myaddr);
322753c7e08SAugustin Cavalier 			rt->rt_flags |= IEEE80211_MESHRT_FLAGS_VALID
323753c7e08SAugustin Cavalier 				     |  IEEE80211_MESHRT_FLAGS_PROXY;
324753c7e08SAugustin Cavalier 		}
325753c7e08SAugustin Cavalier 	} else if ((rt->rt_flags & IEEE80211_MESHRT_FLAGS_VALID) == 0) {
326753c7e08SAugustin Cavalier 		KASSERT(rt->rt_flags & IEEE80211_MESHRT_FLAGS_PROXY,
327753c7e08SAugustin Cavalier 		    ("no proxy flag for poxy entry"));
328753c7e08SAugustin Cavalier 		struct ieee80211com *ic = vap->iv_ic;
329753c7e08SAugustin Cavalier 		/*
330753c7e08SAugustin Cavalier 		 * Fix existing entry created by received frames from
331753c7e08SAugustin Cavalier 		 * stations that have some memory of dest.  We also
332753c7e08SAugustin Cavalier 		 * flush any frames held on the staging queue; delivering
333753c7e08SAugustin Cavalier 		 * them is too much trouble right now.
334753c7e08SAugustin Cavalier 		 */
335753c7e08SAugustin Cavalier 		IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH, dest,
336753c7e08SAugustin Cavalier 		    "%s", "fix proxy entry");
337753c7e08SAugustin Cavalier 		IEEE80211_ADDR_COPY(rt->rt_nexthop, vap->iv_myaddr);
338753c7e08SAugustin Cavalier 		rt->rt_flags |= IEEE80211_MESHRT_FLAGS_VALID
339753c7e08SAugustin Cavalier 			     |  IEEE80211_MESHRT_FLAGS_PROXY;
340753c7e08SAugustin Cavalier 		/* XXX belongs in hwmp */
341753c7e08SAugustin Cavalier 		ieee80211_ageq_drain_node(&ic->ic_stageq,
342753c7e08SAugustin Cavalier 		   (void *)(uintptr_t) ieee80211_mac_hash(ic, dest));
343753c7e08SAugustin Cavalier 		/* XXX stat? */
344753c7e08SAugustin Cavalier 	}
345753c7e08SAugustin Cavalier 	MESH_RT_UNLOCK(ms);
346753c7e08SAugustin Cavalier }
347753c7e08SAugustin Cavalier 
348753c7e08SAugustin Cavalier static __inline void
mesh_rt_del(struct ieee80211_mesh_state * ms,struct ieee80211_mesh_route * rt)349753c7e08SAugustin Cavalier mesh_rt_del(struct ieee80211_mesh_state *ms, struct ieee80211_mesh_route *rt)
350753c7e08SAugustin Cavalier {
351753c7e08SAugustin Cavalier 	TAILQ_REMOVE(&ms->ms_routes, rt, rt_next);
352753c7e08SAugustin Cavalier 	/*
353753c7e08SAugustin Cavalier 	 * Grab the lock before destroying it, to be sure no one else
354753c7e08SAugustin Cavalier 	 * is holding the route.
355753c7e08SAugustin Cavalier 	 */
356753c7e08SAugustin Cavalier 	MESH_RT_ENTRY_LOCK(rt);
357753c7e08SAugustin Cavalier 	callout_drain(&rt->rt_discovery);
358753c7e08SAugustin Cavalier 	MESH_RT_ENTRY_LOCK_DESTROY(rt);
359753c7e08SAugustin Cavalier 	IEEE80211_FREE(rt, M_80211_MESH_RT);
360753c7e08SAugustin Cavalier }
361753c7e08SAugustin Cavalier 
362753c7e08SAugustin Cavalier void
ieee80211_mesh_rt_del(struct ieee80211vap * vap,const uint8_t dest[IEEE80211_ADDR_LEN])363753c7e08SAugustin Cavalier ieee80211_mesh_rt_del(struct ieee80211vap *vap,
364753c7e08SAugustin Cavalier     const uint8_t dest[IEEE80211_ADDR_LEN])
365753c7e08SAugustin Cavalier {
366753c7e08SAugustin Cavalier 	struct ieee80211_mesh_state *ms = vap->iv_mesh;
367753c7e08SAugustin Cavalier 	struct ieee80211_mesh_route *rt, *next;
368753c7e08SAugustin Cavalier 
369753c7e08SAugustin Cavalier 	MESH_RT_LOCK(ms);
370753c7e08SAugustin Cavalier 	TAILQ_FOREACH_SAFE(rt, &ms->ms_routes, rt_next, next) {
371753c7e08SAugustin Cavalier 		if (IEEE80211_ADDR_EQ(rt->rt_dest, dest)) {
372753c7e08SAugustin Cavalier 			if (rt->rt_flags & IEEE80211_MESHRT_FLAGS_PROXY) {
373753c7e08SAugustin Cavalier 				ms->ms_ppath->mpp_senderror(vap, dest, rt,
374753c7e08SAugustin Cavalier 				    IEEE80211_REASON_MESH_PERR_NO_PROXY);
375753c7e08SAugustin Cavalier 			} else {
376753c7e08SAugustin Cavalier 				ms->ms_ppath->mpp_senderror(vap, dest, rt,
377753c7e08SAugustin Cavalier 				    IEEE80211_REASON_MESH_PERR_DEST_UNREACH);
378753c7e08SAugustin Cavalier 			}
379753c7e08SAugustin Cavalier 			mesh_rt_del(ms, rt);
380753c7e08SAugustin Cavalier 			MESH_RT_UNLOCK(ms);
381753c7e08SAugustin Cavalier 			return;
382753c7e08SAugustin Cavalier 		}
383753c7e08SAugustin Cavalier 	}
384753c7e08SAugustin Cavalier 	MESH_RT_UNLOCK(ms);
385753c7e08SAugustin Cavalier }
386753c7e08SAugustin Cavalier 
387753c7e08SAugustin Cavalier void
ieee80211_mesh_rt_flush(struct ieee80211vap * vap)388753c7e08SAugustin Cavalier ieee80211_mesh_rt_flush(struct ieee80211vap *vap)
389753c7e08SAugustin Cavalier {
390753c7e08SAugustin Cavalier 	struct ieee80211_mesh_state *ms = vap->iv_mesh;
391753c7e08SAugustin Cavalier 	struct ieee80211_mesh_route *rt, *next;
392753c7e08SAugustin Cavalier 
393753c7e08SAugustin Cavalier 	if (ms == NULL)
394753c7e08SAugustin Cavalier 		return;
395753c7e08SAugustin Cavalier 	MESH_RT_LOCK(ms);
396753c7e08SAugustin Cavalier 	TAILQ_FOREACH_SAFE(rt, &ms->ms_routes, rt_next, next)
397753c7e08SAugustin Cavalier 		mesh_rt_del(ms, rt);
398753c7e08SAugustin Cavalier 	MESH_RT_UNLOCK(ms);
399753c7e08SAugustin Cavalier }
400753c7e08SAugustin Cavalier 
401753c7e08SAugustin Cavalier void
ieee80211_mesh_rt_flush_peer(struct ieee80211vap * vap,const uint8_t peer[IEEE80211_ADDR_LEN])402753c7e08SAugustin Cavalier ieee80211_mesh_rt_flush_peer(struct ieee80211vap *vap,
403753c7e08SAugustin Cavalier     const uint8_t peer[IEEE80211_ADDR_LEN])
404753c7e08SAugustin Cavalier {
405753c7e08SAugustin Cavalier 	struct ieee80211_mesh_state *ms = vap->iv_mesh;
406753c7e08SAugustin Cavalier 	struct ieee80211_mesh_route *rt, *next;
407753c7e08SAugustin Cavalier 
408753c7e08SAugustin Cavalier 	MESH_RT_LOCK(ms);
409753c7e08SAugustin Cavalier 	TAILQ_FOREACH_SAFE(rt, &ms->ms_routes, rt_next, next) {
410753c7e08SAugustin Cavalier 		if (IEEE80211_ADDR_EQ(rt->rt_nexthop, peer))
411753c7e08SAugustin Cavalier 			mesh_rt_del(ms, rt);
412753c7e08SAugustin Cavalier 	}
413753c7e08SAugustin Cavalier 	MESH_RT_UNLOCK(ms);
414753c7e08SAugustin Cavalier }
415753c7e08SAugustin Cavalier 
416753c7e08SAugustin Cavalier /*
417753c7e08SAugustin Cavalier  * Flush expired routing entries, i.e. those in invalid state for
418753c7e08SAugustin Cavalier  * some time.
419753c7e08SAugustin Cavalier  */
420753c7e08SAugustin Cavalier static void
mesh_rt_flush_invalid(struct ieee80211vap * vap)421753c7e08SAugustin Cavalier mesh_rt_flush_invalid(struct ieee80211vap *vap)
422753c7e08SAugustin Cavalier {
423753c7e08SAugustin Cavalier 	struct ieee80211_mesh_state *ms = vap->iv_mesh;
424753c7e08SAugustin Cavalier 	struct ieee80211_mesh_route *rt, *next;
425753c7e08SAugustin Cavalier 
426753c7e08SAugustin Cavalier 	if (ms == NULL)
427753c7e08SAugustin Cavalier 		return;
428753c7e08SAugustin Cavalier 	MESH_RT_LOCK(ms);
429753c7e08SAugustin Cavalier 	TAILQ_FOREACH_SAFE(rt, &ms->ms_routes, rt_next, next) {
430753c7e08SAugustin Cavalier 		/* Discover paths will be deleted by their own callout */
431753c7e08SAugustin Cavalier 		if (rt->rt_flags & IEEE80211_MESHRT_FLAGS_DISCOVER)
432753c7e08SAugustin Cavalier 			continue;
433753c7e08SAugustin Cavalier 		ieee80211_mesh_rt_update(rt, 0);
434753c7e08SAugustin Cavalier 		if ((rt->rt_flags & IEEE80211_MESHRT_FLAGS_VALID) == 0)
435753c7e08SAugustin Cavalier 			mesh_rt_del(ms, rt);
436753c7e08SAugustin Cavalier 	}
437753c7e08SAugustin Cavalier 	MESH_RT_UNLOCK(ms);
438753c7e08SAugustin Cavalier }
439753c7e08SAugustin Cavalier 
440753c7e08SAugustin Cavalier int
ieee80211_mesh_register_proto_path(const struct ieee80211_mesh_proto_path * mpp)441753c7e08SAugustin Cavalier ieee80211_mesh_register_proto_path(const struct ieee80211_mesh_proto_path *mpp)
442753c7e08SAugustin Cavalier {
443753c7e08SAugustin Cavalier 	int i, firstempty = -1;
444753c7e08SAugustin Cavalier 
445753c7e08SAugustin Cavalier 	for (i = 0; i < nitems(mesh_proto_paths); i++) {
446753c7e08SAugustin Cavalier 		if (strncmp(mpp->mpp_descr, mesh_proto_paths[i].mpp_descr,
447753c7e08SAugustin Cavalier 		    IEEE80211_MESH_PROTO_DSZ) == 0)
448753c7e08SAugustin Cavalier 			return EEXIST;
449753c7e08SAugustin Cavalier 		if (!mesh_proto_paths[i].mpp_active && firstempty == -1)
450753c7e08SAugustin Cavalier 			firstempty = i;
451753c7e08SAugustin Cavalier 	}
452753c7e08SAugustin Cavalier 	if (firstempty < 0)
453753c7e08SAugustin Cavalier 		return ENOSPC;
454753c7e08SAugustin Cavalier 	memcpy(&mesh_proto_paths[firstempty], mpp, sizeof(*mpp));
455753c7e08SAugustin Cavalier 	mesh_proto_paths[firstempty].mpp_active = 1;
456753c7e08SAugustin Cavalier 	return 0;
457753c7e08SAugustin Cavalier }
458753c7e08SAugustin Cavalier 
459753c7e08SAugustin Cavalier int
ieee80211_mesh_register_proto_metric(const struct ieee80211_mesh_proto_metric * mpm)460753c7e08SAugustin Cavalier ieee80211_mesh_register_proto_metric(const struct
461753c7e08SAugustin Cavalier     ieee80211_mesh_proto_metric *mpm)
462753c7e08SAugustin Cavalier {
463753c7e08SAugustin Cavalier 	int i, firstempty = -1;
464753c7e08SAugustin Cavalier 
465753c7e08SAugustin Cavalier 	for (i = 0; i < nitems(mesh_proto_metrics); i++) {
466753c7e08SAugustin Cavalier 		if (strncmp(mpm->mpm_descr, mesh_proto_metrics[i].mpm_descr,
467753c7e08SAugustin Cavalier 		    IEEE80211_MESH_PROTO_DSZ) == 0)
468753c7e08SAugustin Cavalier 			return EEXIST;
469753c7e08SAugustin Cavalier 		if (!mesh_proto_metrics[i].mpm_active && firstempty == -1)
470753c7e08SAugustin Cavalier 			firstempty = i;
471753c7e08SAugustin Cavalier 	}
472753c7e08SAugustin Cavalier 	if (firstempty < 0)
473753c7e08SAugustin Cavalier 		return ENOSPC;
474753c7e08SAugustin Cavalier 	memcpy(&mesh_proto_metrics[firstempty], mpm, sizeof(*mpm));
475753c7e08SAugustin Cavalier 	mesh_proto_metrics[firstempty].mpm_active = 1;
476753c7e08SAugustin Cavalier 	return 0;
477753c7e08SAugustin Cavalier }
478753c7e08SAugustin Cavalier 
479753c7e08SAugustin Cavalier static int
mesh_select_proto_path(struct ieee80211vap * vap,const char * name)480753c7e08SAugustin Cavalier mesh_select_proto_path(struct ieee80211vap *vap, const char *name)
481753c7e08SAugustin Cavalier {
482753c7e08SAugustin Cavalier 	struct ieee80211_mesh_state *ms = vap->iv_mesh;
483753c7e08SAugustin Cavalier 	int i;
484753c7e08SAugustin Cavalier 
485753c7e08SAugustin Cavalier 	for (i = 0; i < nitems(mesh_proto_paths); i++) {
486753c7e08SAugustin Cavalier 		if (strcasecmp(mesh_proto_paths[i].mpp_descr, name) == 0) {
487753c7e08SAugustin Cavalier 			ms->ms_ppath = &mesh_proto_paths[i];
488753c7e08SAugustin Cavalier 			return 0;
489753c7e08SAugustin Cavalier 		}
490753c7e08SAugustin Cavalier 	}
491753c7e08SAugustin Cavalier 	return ENOENT;
492753c7e08SAugustin Cavalier }
493753c7e08SAugustin Cavalier 
494753c7e08SAugustin Cavalier static int
mesh_select_proto_metric(struct ieee80211vap * vap,const char * name)495753c7e08SAugustin Cavalier mesh_select_proto_metric(struct ieee80211vap *vap, const char *name)
496753c7e08SAugustin Cavalier {
497753c7e08SAugustin Cavalier 	struct ieee80211_mesh_state *ms = vap->iv_mesh;
498753c7e08SAugustin Cavalier 	int i;
499753c7e08SAugustin Cavalier 
500753c7e08SAugustin Cavalier 	for (i = 0; i < nitems(mesh_proto_metrics); i++) {
501753c7e08SAugustin Cavalier 		if (strcasecmp(mesh_proto_metrics[i].mpm_descr, name) == 0) {
502753c7e08SAugustin Cavalier 			ms->ms_pmetric = &mesh_proto_metrics[i];
503753c7e08SAugustin Cavalier 			return 0;
504753c7e08SAugustin Cavalier 		}
505753c7e08SAugustin Cavalier 	}
506753c7e08SAugustin Cavalier 	return ENOENT;
507753c7e08SAugustin Cavalier }
508753c7e08SAugustin Cavalier 
509753c7e08SAugustin Cavalier static void
mesh_gatemode_setup(struct ieee80211vap * vap)510753c7e08SAugustin Cavalier mesh_gatemode_setup(struct ieee80211vap *vap)
511753c7e08SAugustin Cavalier {
512753c7e08SAugustin Cavalier 	struct ieee80211_mesh_state *ms = vap->iv_mesh;
513753c7e08SAugustin Cavalier 
514753c7e08SAugustin Cavalier 	/*
515753c7e08SAugustin Cavalier 	 * NB: When a mesh gate is running as a ROOT it shall
516753c7e08SAugustin Cavalier 	 * not send out periodic GANNs but instead mark the
517753c7e08SAugustin Cavalier 	 * mesh gate flag for the corresponding proactive PREQ
518753c7e08SAugustin Cavalier 	 * and RANN frames.
519753c7e08SAugustin Cavalier 	 */
520753c7e08SAugustin Cavalier 	if (ms->ms_flags & IEEE80211_MESHFLAGS_ROOT ||
521753c7e08SAugustin Cavalier 	    (ms->ms_flags & IEEE80211_MESHFLAGS_GATE) == 0) {
522753c7e08SAugustin Cavalier 		callout_drain(&ms->ms_gatetimer);
523753c7e08SAugustin Cavalier 		return ;
524753c7e08SAugustin Cavalier 	}
525753c7e08SAugustin Cavalier 	callout_reset(&ms->ms_gatetimer, ieee80211_mesh_gateint,
526753c7e08SAugustin Cavalier 	    mesh_gatemode_cb, vap);
527753c7e08SAugustin Cavalier }
528753c7e08SAugustin Cavalier 
529753c7e08SAugustin Cavalier static void
mesh_gatemode_cb(void * arg)530753c7e08SAugustin Cavalier mesh_gatemode_cb(void *arg)
531753c7e08SAugustin Cavalier {
532753c7e08SAugustin Cavalier 	struct ieee80211vap *vap = (struct ieee80211vap *)arg;
533753c7e08SAugustin Cavalier 	struct ieee80211_mesh_state *ms = vap->iv_mesh;
534753c7e08SAugustin Cavalier 	struct ieee80211_meshgann_ie gann;
535753c7e08SAugustin Cavalier 
536753c7e08SAugustin Cavalier 	gann.gann_flags = 0; /* Reserved */
537753c7e08SAugustin Cavalier 	gann.gann_hopcount = 0;
538753c7e08SAugustin Cavalier 	gann.gann_ttl = ms->ms_ttl;
539753c7e08SAugustin Cavalier 	IEEE80211_ADDR_COPY(gann.gann_addr, vap->iv_myaddr);
540753c7e08SAugustin Cavalier 	gann.gann_seq = ms->ms_gateseq++;
541753c7e08SAugustin Cavalier 	gann.gann_interval = ieee80211_mesh_gateint;
542753c7e08SAugustin Cavalier 
543753c7e08SAugustin Cavalier 	IEEE80211_NOTE(vap, IEEE80211_MSG_MESH, vap->iv_bss,
544753c7e08SAugustin Cavalier 	    "send broadcast GANN (seq %u)", gann.gann_seq);
545753c7e08SAugustin Cavalier 
546753c7e08SAugustin Cavalier 	ieee80211_send_action(vap->iv_bss, IEEE80211_ACTION_CAT_MESH,
547753c7e08SAugustin Cavalier 	    IEEE80211_ACTION_MESH_GANN, &gann);
548753c7e08SAugustin Cavalier 	mesh_gatemode_setup(vap);
549753c7e08SAugustin Cavalier }
550753c7e08SAugustin Cavalier 
5518244a9baSAugustin Cavalier static void
ieee80211_mesh_init(void)552753c7e08SAugustin Cavalier ieee80211_mesh_init(void)
553753c7e08SAugustin Cavalier {
554753c7e08SAugustin Cavalier 
555753c7e08SAugustin Cavalier 	memset(mesh_proto_paths, 0, sizeof(mesh_proto_paths));
556753c7e08SAugustin Cavalier 	memset(mesh_proto_metrics, 0, sizeof(mesh_proto_metrics));
557753c7e08SAugustin Cavalier 
558753c7e08SAugustin Cavalier 	/*
559753c7e08SAugustin Cavalier 	 * Setup mesh parameters that depends on the clock frequency.
560753c7e08SAugustin Cavalier 	 */
561753c7e08SAugustin Cavalier 	ieee80211_mesh_gateint = msecs_to_ticks(10000);
562753c7e08SAugustin Cavalier 	ieee80211_mesh_retrytimeout = msecs_to_ticks(40);
563753c7e08SAugustin Cavalier 	ieee80211_mesh_holdingtimeout = msecs_to_ticks(40);
564753c7e08SAugustin Cavalier 	ieee80211_mesh_confirmtimeout = msecs_to_ticks(40);
565753c7e08SAugustin Cavalier 	ieee80211_mesh_backofftimeout = msecs_to_ticks(5000);
566753c7e08SAugustin Cavalier 
567753c7e08SAugustin Cavalier 	/*
568753c7e08SAugustin Cavalier 	 * Register action frame handlers.
569753c7e08SAugustin Cavalier 	 */
570753c7e08SAugustin Cavalier 	ieee80211_recv_action_register(IEEE80211_ACTION_CAT_SELF_PROT,
571753c7e08SAugustin Cavalier 	    IEEE80211_ACTION_MESHPEERING_OPEN,
572753c7e08SAugustin Cavalier 	    mesh_recv_action_meshpeering_open);
573753c7e08SAugustin Cavalier 	ieee80211_recv_action_register(IEEE80211_ACTION_CAT_SELF_PROT,
574753c7e08SAugustin Cavalier 	    IEEE80211_ACTION_MESHPEERING_CONFIRM,
575753c7e08SAugustin Cavalier 	    mesh_recv_action_meshpeering_confirm);
576753c7e08SAugustin Cavalier 	ieee80211_recv_action_register(IEEE80211_ACTION_CAT_SELF_PROT,
577753c7e08SAugustin Cavalier 	    IEEE80211_ACTION_MESHPEERING_CLOSE,
578753c7e08SAugustin Cavalier 	    mesh_recv_action_meshpeering_close);
579753c7e08SAugustin Cavalier 	ieee80211_recv_action_register(IEEE80211_ACTION_CAT_MESH,
580753c7e08SAugustin Cavalier 	    IEEE80211_ACTION_MESH_LMETRIC, mesh_recv_action_meshlmetric);
581753c7e08SAugustin Cavalier 	ieee80211_recv_action_register(IEEE80211_ACTION_CAT_MESH,
582753c7e08SAugustin Cavalier 	    IEEE80211_ACTION_MESH_GANN, mesh_recv_action_meshgate);
583753c7e08SAugustin Cavalier 
584753c7e08SAugustin Cavalier 	ieee80211_send_action_register(IEEE80211_ACTION_CAT_SELF_PROT,
585753c7e08SAugustin Cavalier 	    IEEE80211_ACTION_MESHPEERING_OPEN,
586753c7e08SAugustin Cavalier 	    mesh_send_action_meshpeering_open);
587753c7e08SAugustin Cavalier 	ieee80211_send_action_register(IEEE80211_ACTION_CAT_SELF_PROT,
588753c7e08SAugustin Cavalier 	    IEEE80211_ACTION_MESHPEERING_CONFIRM,
589753c7e08SAugustin Cavalier 	    mesh_send_action_meshpeering_confirm);
590753c7e08SAugustin Cavalier 	ieee80211_send_action_register(IEEE80211_ACTION_CAT_SELF_PROT,
591753c7e08SAugustin Cavalier 	    IEEE80211_ACTION_MESHPEERING_CLOSE,
592753c7e08SAugustin Cavalier 	    mesh_send_action_meshpeering_close);
593753c7e08SAugustin Cavalier 	ieee80211_send_action_register(IEEE80211_ACTION_CAT_MESH,
594753c7e08SAugustin Cavalier 	    IEEE80211_ACTION_MESH_LMETRIC,
595753c7e08SAugustin Cavalier 	    mesh_send_action_meshlmetric);
596753c7e08SAugustin Cavalier 	ieee80211_send_action_register(IEEE80211_ACTION_CAT_MESH,
597753c7e08SAugustin Cavalier 	    IEEE80211_ACTION_MESH_GANN,
598753c7e08SAugustin Cavalier 	    mesh_send_action_meshgate);
599753c7e08SAugustin Cavalier 
600753c7e08SAugustin Cavalier 	/*
601753c7e08SAugustin Cavalier 	 * Register Airtime Link Metric.
602753c7e08SAugustin Cavalier 	 */
603753c7e08SAugustin Cavalier 	ieee80211_mesh_register_proto_metric(&mesh_metric_airtime);
604753c7e08SAugustin Cavalier 
605753c7e08SAugustin Cavalier }
606753c7e08SAugustin Cavalier SYSINIT(wlan_mesh, SI_SUB_DRIVERS, SI_ORDER_FIRST, ieee80211_mesh_init, NULL);
607753c7e08SAugustin Cavalier 
608753c7e08SAugustin Cavalier void
ieee80211_mesh_attach(struct ieee80211com * ic)609753c7e08SAugustin Cavalier ieee80211_mesh_attach(struct ieee80211com *ic)
610753c7e08SAugustin Cavalier {
611753c7e08SAugustin Cavalier 	ic->ic_vattach[IEEE80211_M_MBSS] = mesh_vattach;
612753c7e08SAugustin Cavalier }
613753c7e08SAugustin Cavalier 
614753c7e08SAugustin Cavalier void
ieee80211_mesh_detach(struct ieee80211com * ic)615753c7e08SAugustin Cavalier ieee80211_mesh_detach(struct ieee80211com *ic)
616753c7e08SAugustin Cavalier {
617753c7e08SAugustin Cavalier }
618753c7e08SAugustin Cavalier 
619753c7e08SAugustin Cavalier static void
mesh_vdetach_peers(void * arg,struct ieee80211_node * ni)620753c7e08SAugustin Cavalier mesh_vdetach_peers(void *arg, struct ieee80211_node *ni)
621753c7e08SAugustin Cavalier {
622753c7e08SAugustin Cavalier 	struct ieee80211com *ic = ni->ni_ic;
623753c7e08SAugustin Cavalier 	uint16_t args[3];
624753c7e08SAugustin Cavalier 
625753c7e08SAugustin Cavalier 	if (ni->ni_mlstate == IEEE80211_NODE_MESH_ESTABLISHED) {
626753c7e08SAugustin Cavalier 		args[0] = ni->ni_mlpid;
627753c7e08SAugustin Cavalier 		args[1] = ni->ni_mllid;
628753c7e08SAugustin Cavalier 		args[2] = IEEE80211_REASON_PEER_LINK_CANCELED;
629753c7e08SAugustin Cavalier 		ieee80211_send_action(ni,
630753c7e08SAugustin Cavalier 		    IEEE80211_ACTION_CAT_SELF_PROT,
631753c7e08SAugustin Cavalier 		    IEEE80211_ACTION_MESHPEERING_CLOSE,
632753c7e08SAugustin Cavalier 		    args);
633753c7e08SAugustin Cavalier 	}
634753c7e08SAugustin Cavalier 	callout_drain(&ni->ni_mltimer);
635753c7e08SAugustin Cavalier 	/* XXX belongs in hwmp */
636753c7e08SAugustin Cavalier 	ieee80211_ageq_drain_node(&ic->ic_stageq,
637753c7e08SAugustin Cavalier 	   (void *)(uintptr_t) ieee80211_mac_hash(ic, ni->ni_macaddr));
638753c7e08SAugustin Cavalier }
639753c7e08SAugustin Cavalier 
640753c7e08SAugustin Cavalier static void
mesh_vdetach(struct ieee80211vap * vap)641753c7e08SAugustin Cavalier mesh_vdetach(struct ieee80211vap *vap)
642753c7e08SAugustin Cavalier {
643753c7e08SAugustin Cavalier 	struct ieee80211_mesh_state *ms = vap->iv_mesh;
644753c7e08SAugustin Cavalier 
645753c7e08SAugustin Cavalier 	callout_drain(&ms->ms_cleantimer);
646753c7e08SAugustin Cavalier 	ieee80211_iterate_nodes(&vap->iv_ic->ic_sta, mesh_vdetach_peers,
647753c7e08SAugustin Cavalier 	    NULL);
648753c7e08SAugustin Cavalier 	ieee80211_mesh_rt_flush(vap);
649753c7e08SAugustin Cavalier 	MESH_RT_LOCK_DESTROY(ms);
650753c7e08SAugustin Cavalier 	ms->ms_ppath->mpp_vdetach(vap);
651753c7e08SAugustin Cavalier 	IEEE80211_FREE(vap->iv_mesh, M_80211_VAP);
652753c7e08SAugustin Cavalier 	vap->iv_mesh = NULL;
653753c7e08SAugustin Cavalier }
654753c7e08SAugustin Cavalier 
655753c7e08SAugustin Cavalier static void
mesh_vattach(struct ieee80211vap * vap)656753c7e08SAugustin Cavalier mesh_vattach(struct ieee80211vap *vap)
657753c7e08SAugustin Cavalier {
658753c7e08SAugustin Cavalier 	struct ieee80211_mesh_state *ms;
659753c7e08SAugustin Cavalier 	vap->iv_newstate = mesh_newstate;
660753c7e08SAugustin Cavalier 	vap->iv_input = mesh_input;
661753c7e08SAugustin Cavalier 	vap->iv_opdetach = mesh_vdetach;
662753c7e08SAugustin Cavalier 	vap->iv_recv_mgmt = mesh_recv_mgmt;
663753c7e08SAugustin Cavalier 	vap->iv_recv_ctl = mesh_recv_ctl;
664753c7e08SAugustin Cavalier 	ms = IEEE80211_MALLOC(sizeof(struct ieee80211_mesh_state), M_80211_VAP,
665753c7e08SAugustin Cavalier 	    IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
666753c7e08SAugustin Cavalier 	if (ms == NULL) {
667753c7e08SAugustin Cavalier 		printf("%s: couldn't alloc MBSS state\n", __func__);
668753c7e08SAugustin Cavalier 		return;
669753c7e08SAugustin Cavalier 	}
670753c7e08SAugustin Cavalier 	vap->iv_mesh = ms;
671753c7e08SAugustin Cavalier 	ms->ms_seq = 0;
672753c7e08SAugustin Cavalier 	ms->ms_flags = (IEEE80211_MESHFLAGS_AP | IEEE80211_MESHFLAGS_FWD);
673753c7e08SAugustin Cavalier 	ms->ms_ttl = IEEE80211_MESH_DEFAULT_TTL;
674753c7e08SAugustin Cavalier 	TAILQ_INIT(&ms->ms_known_gates);
675753c7e08SAugustin Cavalier 	TAILQ_INIT(&ms->ms_routes);
676753c7e08SAugustin Cavalier 	MESH_RT_LOCK_INIT(ms, "MBSS");
677753c7e08SAugustin Cavalier 	callout_init(&ms->ms_cleantimer, 1);
678753c7e08SAugustin Cavalier 	callout_init(&ms->ms_gatetimer, 1);
679753c7e08SAugustin Cavalier 	ms->ms_gateseq = 0;
680753c7e08SAugustin Cavalier 	mesh_select_proto_metric(vap, "AIRTIME");
681753c7e08SAugustin Cavalier 	KASSERT(ms->ms_pmetric, ("ms_pmetric == NULL"));
682753c7e08SAugustin Cavalier 	mesh_select_proto_path(vap, "HWMP");
683753c7e08SAugustin Cavalier 	KASSERT(ms->ms_ppath, ("ms_ppath == NULL"));
684753c7e08SAugustin Cavalier 	ms->ms_ppath->mpp_vattach(vap);
685753c7e08SAugustin Cavalier }
686753c7e08SAugustin Cavalier 
687753c7e08SAugustin Cavalier /*
688753c7e08SAugustin Cavalier  * IEEE80211_M_MBSS vap state machine handler.
689753c7e08SAugustin Cavalier  */
690753c7e08SAugustin Cavalier static int
mesh_newstate(struct ieee80211vap * vap,enum ieee80211_state nstate,int arg)691753c7e08SAugustin Cavalier mesh_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
692753c7e08SAugustin Cavalier {
693753c7e08SAugustin Cavalier 	struct ieee80211_mesh_state *ms = vap->iv_mesh;
694753c7e08SAugustin Cavalier 	struct ieee80211com *ic = vap->iv_ic;
695753c7e08SAugustin Cavalier 	struct ieee80211_node *ni;
696753c7e08SAugustin Cavalier 	enum ieee80211_state ostate;
697753c7e08SAugustin Cavalier 
698753c7e08SAugustin Cavalier 	IEEE80211_LOCK_ASSERT(ic);
699753c7e08SAugustin Cavalier 
700753c7e08SAugustin Cavalier 	ostate = vap->iv_state;
701753c7e08SAugustin Cavalier 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE, "%s: %s -> %s (%d)\n",
702753c7e08SAugustin Cavalier 	    __func__, ieee80211_state_name[ostate],
703753c7e08SAugustin Cavalier 	    ieee80211_state_name[nstate], arg);
704753c7e08SAugustin Cavalier 	vap->iv_state = nstate;		/* state transition */
705753c7e08SAugustin Cavalier 	if (ostate != IEEE80211_S_SCAN)
706753c7e08SAugustin Cavalier 		ieee80211_cancel_scan(vap);	/* background scan */
707753c7e08SAugustin Cavalier 	ni = vap->iv_bss;			/* NB: no reference held */
708753c7e08SAugustin Cavalier 	if (nstate != IEEE80211_S_RUN && ostate == IEEE80211_S_RUN) {
709753c7e08SAugustin Cavalier 		callout_drain(&ms->ms_cleantimer);
710753c7e08SAugustin Cavalier 		callout_drain(&ms->ms_gatetimer);
711753c7e08SAugustin Cavalier 	}
712753c7e08SAugustin Cavalier 	switch (nstate) {
713753c7e08SAugustin Cavalier 	case IEEE80211_S_INIT:
714753c7e08SAugustin Cavalier 		switch (ostate) {
715753c7e08SAugustin Cavalier 		case IEEE80211_S_SCAN:
716753c7e08SAugustin Cavalier 			ieee80211_cancel_scan(vap);
717753c7e08SAugustin Cavalier 			break;
718753c7e08SAugustin Cavalier 		case IEEE80211_S_CAC:
719753c7e08SAugustin Cavalier 			ieee80211_dfs_cac_stop(vap);
720753c7e08SAugustin Cavalier 			break;
721753c7e08SAugustin Cavalier 		case IEEE80211_S_RUN:
722753c7e08SAugustin Cavalier 			ieee80211_iterate_nodes(&ic->ic_sta,
723753c7e08SAugustin Cavalier 			    mesh_vdetach_peers, NULL);
724753c7e08SAugustin Cavalier 			break;
725753c7e08SAugustin Cavalier 		default:
726753c7e08SAugustin Cavalier 			break;
727753c7e08SAugustin Cavalier 		}
728753c7e08SAugustin Cavalier 		if (ostate != IEEE80211_S_INIT) {
729753c7e08SAugustin Cavalier 			/* NB: optimize INIT -> INIT case */
730753c7e08SAugustin Cavalier 			ieee80211_reset_bss(vap);
731753c7e08SAugustin Cavalier 			ieee80211_mesh_rt_flush(vap);
732753c7e08SAugustin Cavalier 		}
733753c7e08SAugustin Cavalier 		break;
734753c7e08SAugustin Cavalier 	case IEEE80211_S_SCAN:
735753c7e08SAugustin Cavalier 		switch (ostate) {
736753c7e08SAugustin Cavalier 		case IEEE80211_S_INIT:
737753c7e08SAugustin Cavalier 			if (vap->iv_des_chan != IEEE80211_CHAN_ANYC &&
738753c7e08SAugustin Cavalier 			    !IEEE80211_IS_CHAN_RADAR(vap->iv_des_chan) &&
739753c7e08SAugustin Cavalier 			    ms->ms_idlen != 0) {
740753c7e08SAugustin Cavalier 				/*
741753c7e08SAugustin Cavalier 				 * Already have a channel and a mesh ID; bypass
742753c7e08SAugustin Cavalier 				 * the scan and startup immediately.
743753c7e08SAugustin Cavalier 				 */
744753c7e08SAugustin Cavalier 				ieee80211_create_ibss(vap, vap->iv_des_chan);
745753c7e08SAugustin Cavalier 				break;
746753c7e08SAugustin Cavalier 			}
747753c7e08SAugustin Cavalier 			/*
748753c7e08SAugustin Cavalier 			 * Initiate a scan.  We can come here as a result
749753c7e08SAugustin Cavalier 			 * of an IEEE80211_IOC_SCAN_REQ too in which case
750753c7e08SAugustin Cavalier 			 * the vap will be marked with IEEE80211_FEXT_SCANREQ
751753c7e08SAugustin Cavalier 			 * and the scan request parameters will be present
752753c7e08SAugustin Cavalier 			 * in iv_scanreq.  Otherwise we do the default.
753753c7e08SAugustin Cavalier 			*/
754753c7e08SAugustin Cavalier 			if (vap->iv_flags_ext & IEEE80211_FEXT_SCANREQ) {
755753c7e08SAugustin Cavalier 				ieee80211_check_scan(vap,
756753c7e08SAugustin Cavalier 				    vap->iv_scanreq_flags,
757753c7e08SAugustin Cavalier 				    vap->iv_scanreq_duration,
758753c7e08SAugustin Cavalier 				    vap->iv_scanreq_mindwell,
759753c7e08SAugustin Cavalier 				    vap->iv_scanreq_maxdwell,
760753c7e08SAugustin Cavalier 				    vap->iv_scanreq_nssid, vap->iv_scanreq_ssid);
761753c7e08SAugustin Cavalier 				vap->iv_flags_ext &= ~IEEE80211_FEXT_SCANREQ;
762753c7e08SAugustin Cavalier 			} else
763753c7e08SAugustin Cavalier 				ieee80211_check_scan_current(vap);
764753c7e08SAugustin Cavalier 			break;
765753c7e08SAugustin Cavalier 		default:
766753c7e08SAugustin Cavalier 			break;
767753c7e08SAugustin Cavalier 		}
768753c7e08SAugustin Cavalier 		break;
769753c7e08SAugustin Cavalier 	case IEEE80211_S_CAC:
770753c7e08SAugustin Cavalier 		/*
771753c7e08SAugustin Cavalier 		 * Start CAC on a DFS channel.  We come here when starting
772753c7e08SAugustin Cavalier 		 * a bss on a DFS channel (see ieee80211_create_ibss).
773753c7e08SAugustin Cavalier 		 */
774753c7e08SAugustin Cavalier 		ieee80211_dfs_cac_start(vap);
775753c7e08SAugustin Cavalier 		break;
776753c7e08SAugustin Cavalier 	case IEEE80211_S_RUN:
777753c7e08SAugustin Cavalier 		switch (ostate) {
778753c7e08SAugustin Cavalier 		case IEEE80211_S_INIT:
779753c7e08SAugustin Cavalier 			/*
780753c7e08SAugustin Cavalier 			 * Already have a channel; bypass the
781753c7e08SAugustin Cavalier 			 * scan and startup immediately.
782753c7e08SAugustin Cavalier 			 * Note that ieee80211_create_ibss will call
783753c7e08SAugustin Cavalier 			 * back to do a RUN->RUN state change.
784753c7e08SAugustin Cavalier 			 */
785753c7e08SAugustin Cavalier 			ieee80211_create_ibss(vap,
786753c7e08SAugustin Cavalier 			    ieee80211_ht_adjust_channel(ic,
787753c7e08SAugustin Cavalier 				ic->ic_curchan, vap->iv_flags_ht));
788753c7e08SAugustin Cavalier 			/* NB: iv_bss is changed on return */
789753c7e08SAugustin Cavalier 			break;
790753c7e08SAugustin Cavalier 		case IEEE80211_S_CAC:
791753c7e08SAugustin Cavalier 			/*
792753c7e08SAugustin Cavalier 			 * NB: This is the normal state change when CAC
793753c7e08SAugustin Cavalier 			 * expires and no radar was detected; no need to
794753c7e08SAugustin Cavalier 			 * clear the CAC timer as it's already expired.
795753c7e08SAugustin Cavalier 			 */
796753c7e08SAugustin Cavalier 			/* fall thru... */
797753c7e08SAugustin Cavalier 		case IEEE80211_S_CSA:
798753c7e08SAugustin Cavalier #if 0
799753c7e08SAugustin Cavalier 			/*
800753c7e08SAugustin Cavalier 			 * Shorten inactivity timer of associated stations
801753c7e08SAugustin Cavalier 			 * to weed out sta's that don't follow a CSA.
802753c7e08SAugustin Cavalier 			 */
803753c7e08SAugustin Cavalier 			ieee80211_iterate_nodes(&ic->ic_sta, sta_csa, vap);
804753c7e08SAugustin Cavalier #endif
805753c7e08SAugustin Cavalier 			/*
806753c7e08SAugustin Cavalier 			 * Update bss node channel to reflect where
807753c7e08SAugustin Cavalier 			 * we landed after CSA.
808753c7e08SAugustin Cavalier 			 */
809753c7e08SAugustin Cavalier 			ieee80211_node_set_chan(ni,
810753c7e08SAugustin Cavalier 			    ieee80211_ht_adjust_channel(ic, ic->ic_curchan,
811753c7e08SAugustin Cavalier 				ieee80211_htchanflags(ni->ni_chan)));
812753c7e08SAugustin Cavalier 			/* XXX bypass debug msgs */
813753c7e08SAugustin Cavalier 			break;
814753c7e08SAugustin Cavalier 		case IEEE80211_S_SCAN:
815753c7e08SAugustin Cavalier 		case IEEE80211_S_RUN:
816753c7e08SAugustin Cavalier #ifdef IEEE80211_DEBUG
817753c7e08SAugustin Cavalier 			if (ieee80211_msg_debug(vap)) {
818753c7e08SAugustin Cavalier 				ieee80211_note(vap,
819753c7e08SAugustin Cavalier 				    "synchronized with %s meshid ",
820753c7e08SAugustin Cavalier 				    ether_sprintf(ni->ni_meshid));
821753c7e08SAugustin Cavalier 				ieee80211_print_essid(ni->ni_meshid,
822753c7e08SAugustin Cavalier 				    ni->ni_meshidlen);
823753c7e08SAugustin Cavalier 				/* XXX MCS/HT */
824753c7e08SAugustin Cavalier 				printf(" channel %d\n",
825753c7e08SAugustin Cavalier 				    ieee80211_chan2ieee(ic, ic->ic_curchan));
826753c7e08SAugustin Cavalier 			}
827753c7e08SAugustin Cavalier #endif
828753c7e08SAugustin Cavalier 			break;
829753c7e08SAugustin Cavalier 		default:
830753c7e08SAugustin Cavalier 			break;
831753c7e08SAugustin Cavalier 		}
832753c7e08SAugustin Cavalier 		ieee80211_node_authorize(ni);
833753c7e08SAugustin Cavalier 		callout_reset(&ms->ms_cleantimer, ms->ms_ppath->mpp_inact,
834753c7e08SAugustin Cavalier                     mesh_rt_cleanup_cb, vap);
835753c7e08SAugustin Cavalier 		mesh_gatemode_setup(vap);
836753c7e08SAugustin Cavalier 		break;
837753c7e08SAugustin Cavalier 	default:
838753c7e08SAugustin Cavalier 		break;
839753c7e08SAugustin Cavalier 	}
840753c7e08SAugustin Cavalier 	/* NB: ostate not nstate */
841753c7e08SAugustin Cavalier 	ms->ms_ppath->mpp_newstate(vap, ostate, arg);
842753c7e08SAugustin Cavalier 	return 0;
843753c7e08SAugustin Cavalier }
844753c7e08SAugustin Cavalier 
845753c7e08SAugustin Cavalier static void
mesh_rt_cleanup_cb(void * arg)846753c7e08SAugustin Cavalier mesh_rt_cleanup_cb(void *arg)
847753c7e08SAugustin Cavalier {
848753c7e08SAugustin Cavalier 	struct ieee80211vap *vap = arg;
849753c7e08SAugustin Cavalier 	struct ieee80211_mesh_state *ms = vap->iv_mesh;
850753c7e08SAugustin Cavalier 
851753c7e08SAugustin Cavalier 	mesh_rt_flush_invalid(vap);
852753c7e08SAugustin Cavalier 	callout_reset(&ms->ms_cleantimer, ms->ms_ppath->mpp_inact,
853753c7e08SAugustin Cavalier 	    mesh_rt_cleanup_cb, vap);
854753c7e08SAugustin Cavalier }
855753c7e08SAugustin Cavalier 
856753c7e08SAugustin Cavalier /*
857753c7e08SAugustin Cavalier  * Mark a mesh STA as gate and return a pointer to it.
858753c7e08SAugustin Cavalier  * If this is first time, we create a new gate route.
859753c7e08SAugustin Cavalier  * Always update the path route to this mesh gate.
860753c7e08SAugustin Cavalier  */
861753c7e08SAugustin Cavalier struct ieee80211_mesh_gate_route *
ieee80211_mesh_mark_gate(struct ieee80211vap * vap,const uint8_t * addr,struct ieee80211_mesh_route * rt)862753c7e08SAugustin Cavalier ieee80211_mesh_mark_gate(struct ieee80211vap *vap, const uint8_t *addr,
863753c7e08SAugustin Cavalier     struct ieee80211_mesh_route *rt)
864753c7e08SAugustin Cavalier {
865753c7e08SAugustin Cavalier 	struct ieee80211_mesh_state *ms = vap->iv_mesh;
866753c7e08SAugustin Cavalier 	struct ieee80211_mesh_gate_route *gr = NULL, *next;
867753c7e08SAugustin Cavalier 	int found = 0;
868753c7e08SAugustin Cavalier 
869753c7e08SAugustin Cavalier 	MESH_RT_LOCK(ms);
870753c7e08SAugustin Cavalier 	TAILQ_FOREACH_SAFE(gr, &ms->ms_known_gates, gr_next, next) {
871753c7e08SAugustin Cavalier 		if (IEEE80211_ADDR_EQ(gr->gr_addr, addr)) {
872753c7e08SAugustin Cavalier 			found = 1;
873753c7e08SAugustin Cavalier 			break;
874753c7e08SAugustin Cavalier 		}
875753c7e08SAugustin Cavalier 	}
876753c7e08SAugustin Cavalier 
877753c7e08SAugustin Cavalier 	if (!found) {
878753c7e08SAugustin Cavalier 		/* New mesh gate add it to known table. */
879753c7e08SAugustin Cavalier 		IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH, addr,
880753c7e08SAugustin Cavalier 		    "%s", "stored new gate information from pro-PREQ.");
881753c7e08SAugustin Cavalier 		gr = IEEE80211_MALLOC(ALIGN(sizeof(struct ieee80211_mesh_gate_route)),
882753c7e08SAugustin Cavalier 		    M_80211_MESH_GT_RT,
883753c7e08SAugustin Cavalier 		    IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
884753c7e08SAugustin Cavalier 		IEEE80211_ADDR_COPY(gr->gr_addr, addr);
885753c7e08SAugustin Cavalier 		TAILQ_INSERT_TAIL(&ms->ms_known_gates, gr, gr_next);
886753c7e08SAugustin Cavalier 	}
887753c7e08SAugustin Cavalier 	gr->gr_route = rt;
888753c7e08SAugustin Cavalier 	/* TODO: link from path route to gate route */
889753c7e08SAugustin Cavalier 	MESH_RT_UNLOCK(ms);
890753c7e08SAugustin Cavalier 
891753c7e08SAugustin Cavalier 	return gr;
892753c7e08SAugustin Cavalier }
893753c7e08SAugustin Cavalier 
894753c7e08SAugustin Cavalier /*
895753c7e08SAugustin Cavalier  * Helper function to note the Mesh Peer Link FSM change.
896753c7e08SAugustin Cavalier  */
897753c7e08SAugustin Cavalier static void
mesh_linkchange(struct ieee80211_node * ni,enum ieee80211_mesh_mlstate state)898753c7e08SAugustin Cavalier mesh_linkchange(struct ieee80211_node *ni, enum ieee80211_mesh_mlstate state)
899753c7e08SAugustin Cavalier {
900753c7e08SAugustin Cavalier 	struct ieee80211vap *vap = ni->ni_vap;
901753c7e08SAugustin Cavalier 	struct ieee80211_mesh_state *ms = vap->iv_mesh;
902753c7e08SAugustin Cavalier #ifdef IEEE80211_DEBUG
903753c7e08SAugustin Cavalier 	static const char *meshlinkstates[] = {
904753c7e08SAugustin Cavalier 		[IEEE80211_NODE_MESH_IDLE]		= "IDLE",
905753c7e08SAugustin Cavalier 		[IEEE80211_NODE_MESH_OPENSNT]		= "OPEN SENT",
906753c7e08SAugustin Cavalier 		[IEEE80211_NODE_MESH_OPENRCV]		= "OPEN RECEIVED",
907753c7e08SAugustin Cavalier 		[IEEE80211_NODE_MESH_CONFIRMRCV]	= "CONFIRM RECEIVED",
908753c7e08SAugustin Cavalier 		[IEEE80211_NODE_MESH_ESTABLISHED]	= "ESTABLISHED",
909753c7e08SAugustin Cavalier 		[IEEE80211_NODE_MESH_HOLDING]		= "HOLDING"
910753c7e08SAugustin Cavalier 	};
911753c7e08SAugustin Cavalier #endif
912753c7e08SAugustin Cavalier 	IEEE80211_NOTE(vap, IEEE80211_MSG_MESH,
913753c7e08SAugustin Cavalier 	    ni, "peer link: %s -> %s",
914753c7e08SAugustin Cavalier 	    meshlinkstates[ni->ni_mlstate], meshlinkstates[state]);
915753c7e08SAugustin Cavalier 
916753c7e08SAugustin Cavalier 	/* track neighbor count */
917753c7e08SAugustin Cavalier 	if (state == IEEE80211_NODE_MESH_ESTABLISHED &&
918753c7e08SAugustin Cavalier 	    ni->ni_mlstate != IEEE80211_NODE_MESH_ESTABLISHED) {
919753c7e08SAugustin Cavalier 		KASSERT(ms->ms_neighbors < 65535, ("neighbor count overflow"));
920753c7e08SAugustin Cavalier 		ms->ms_neighbors++;
921753c7e08SAugustin Cavalier 		ieee80211_beacon_notify(vap, IEEE80211_BEACON_MESHCONF);
922753c7e08SAugustin Cavalier 	} else if (ni->ni_mlstate == IEEE80211_NODE_MESH_ESTABLISHED &&
923753c7e08SAugustin Cavalier 	    state != IEEE80211_NODE_MESH_ESTABLISHED) {
924753c7e08SAugustin Cavalier 		KASSERT(ms->ms_neighbors > 0, ("neighbor count 0"));
925753c7e08SAugustin Cavalier 		ms->ms_neighbors--;
926753c7e08SAugustin Cavalier 		ieee80211_beacon_notify(vap, IEEE80211_BEACON_MESHCONF);
927753c7e08SAugustin Cavalier 	}
928753c7e08SAugustin Cavalier 	ni->ni_mlstate = state;
929753c7e08SAugustin Cavalier 	switch (state) {
930753c7e08SAugustin Cavalier 	case IEEE80211_NODE_MESH_HOLDING:
931753c7e08SAugustin Cavalier 		ms->ms_ppath->mpp_peerdown(ni);
932753c7e08SAugustin Cavalier 		break;
933753c7e08SAugustin Cavalier 	case IEEE80211_NODE_MESH_ESTABLISHED:
934753c7e08SAugustin Cavalier 		ieee80211_mesh_discover(vap, ni->ni_macaddr, NULL);
935753c7e08SAugustin Cavalier 		break;
936753c7e08SAugustin Cavalier 	default:
937753c7e08SAugustin Cavalier 		break;
938753c7e08SAugustin Cavalier 	}
939753c7e08SAugustin Cavalier }
940753c7e08SAugustin Cavalier 
941753c7e08SAugustin Cavalier /*
942753c7e08SAugustin Cavalier  * Helper function to generate a unique local ID required for mesh
943753c7e08SAugustin Cavalier  * peer establishment.
944753c7e08SAugustin Cavalier  */
945753c7e08SAugustin Cavalier static void
mesh_checkid(void * arg,struct ieee80211_node * ni)946753c7e08SAugustin Cavalier mesh_checkid(void *arg, struct ieee80211_node *ni)
947753c7e08SAugustin Cavalier {
948753c7e08SAugustin Cavalier 	uint16_t *r = arg;
949753c7e08SAugustin Cavalier 
950753c7e08SAugustin Cavalier 	if (*r == ni->ni_mllid)
951753c7e08SAugustin Cavalier 		*(uint16_t *)arg = 0;
952753c7e08SAugustin Cavalier }
953753c7e08SAugustin Cavalier 
954753c7e08SAugustin Cavalier static uint32_t
mesh_generateid(struct ieee80211vap * vap)955753c7e08SAugustin Cavalier mesh_generateid(struct ieee80211vap *vap)
956753c7e08SAugustin Cavalier {
957753c7e08SAugustin Cavalier 	int maxiter = 4;
958753c7e08SAugustin Cavalier 	uint16_t r;
959753c7e08SAugustin Cavalier 
960753c7e08SAugustin Cavalier 	do {
96186021fd4SAugustin Cavalier 		net80211_get_random_bytes(&r, 2);
962753c7e08SAugustin Cavalier 		ieee80211_iterate_nodes(&vap->iv_ic->ic_sta, mesh_checkid, &r);
963753c7e08SAugustin Cavalier 		maxiter--;
964753c7e08SAugustin Cavalier 	} while (r == 0 && maxiter > 0);
965753c7e08SAugustin Cavalier 	return r;
966753c7e08SAugustin Cavalier }
967753c7e08SAugustin Cavalier 
968753c7e08SAugustin Cavalier /*
969753c7e08SAugustin Cavalier  * Verifies if we already received this packet by checking its
970753c7e08SAugustin Cavalier  * sequence number.
971753c7e08SAugustin Cavalier  * Returns 0 if the frame is to be accepted, 1 otherwise.
972753c7e08SAugustin Cavalier  */
973753c7e08SAugustin Cavalier static int
mesh_checkpseq(struct ieee80211vap * vap,const uint8_t source[IEEE80211_ADDR_LEN],uint32_t seq)974753c7e08SAugustin Cavalier mesh_checkpseq(struct ieee80211vap *vap,
975753c7e08SAugustin Cavalier     const uint8_t source[IEEE80211_ADDR_LEN], uint32_t seq)
976753c7e08SAugustin Cavalier {
977753c7e08SAugustin Cavalier 	struct ieee80211_mesh_route *rt;
978753c7e08SAugustin Cavalier 
979753c7e08SAugustin Cavalier 	rt = ieee80211_mesh_rt_find(vap, source);
980753c7e08SAugustin Cavalier 	if (rt == NULL) {
981753c7e08SAugustin Cavalier 		rt = ieee80211_mesh_rt_add(vap, source);
982753c7e08SAugustin Cavalier 		if (rt == NULL) {
983753c7e08SAugustin Cavalier 			IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH, source,
984753c7e08SAugustin Cavalier 			    "%s", "add mcast route failed");
985753c7e08SAugustin Cavalier 			vap->iv_stats.is_mesh_rtaddfailed++;
986753c7e08SAugustin Cavalier 			return 1;
987753c7e08SAugustin Cavalier 		}
988753c7e08SAugustin Cavalier 		IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH, source,
989753c7e08SAugustin Cavalier 		    "add mcast route, mesh seqno %d", seq);
990753c7e08SAugustin Cavalier 		rt->rt_lastmseq = seq;
991753c7e08SAugustin Cavalier 		return 0;
992753c7e08SAugustin Cavalier 	}
993753c7e08SAugustin Cavalier 	if (IEEE80211_MESH_SEQ_GEQ(rt->rt_lastmseq, seq)) {
994753c7e08SAugustin Cavalier 		return 1;
995753c7e08SAugustin Cavalier 	} else {
996753c7e08SAugustin Cavalier 		rt->rt_lastmseq = seq;
997753c7e08SAugustin Cavalier 		return 0;
998753c7e08SAugustin Cavalier 	}
999753c7e08SAugustin Cavalier }
1000753c7e08SAugustin Cavalier 
1001753c7e08SAugustin Cavalier /*
1002753c7e08SAugustin Cavalier  * Iterate the routing table and locate the next hop.
1003753c7e08SAugustin Cavalier  */
1004753c7e08SAugustin Cavalier struct ieee80211_node *
ieee80211_mesh_find_txnode(struct ieee80211vap * vap,const uint8_t dest[IEEE80211_ADDR_LEN])1005753c7e08SAugustin Cavalier ieee80211_mesh_find_txnode(struct ieee80211vap *vap,
1006753c7e08SAugustin Cavalier     const uint8_t dest[IEEE80211_ADDR_LEN])
1007753c7e08SAugustin Cavalier {
1008753c7e08SAugustin Cavalier 	struct ieee80211_mesh_route *rt;
1009753c7e08SAugustin Cavalier 
1010753c7e08SAugustin Cavalier 	rt = ieee80211_mesh_rt_find(vap, dest);
1011753c7e08SAugustin Cavalier 	if (rt == NULL)
1012753c7e08SAugustin Cavalier 		return NULL;
1013753c7e08SAugustin Cavalier 	if ((rt->rt_flags & IEEE80211_MESHRT_FLAGS_VALID) == 0) {
1014753c7e08SAugustin Cavalier 		IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH, dest,
1015753c7e08SAugustin Cavalier 		    "%s: !valid, flags 0x%x", __func__, rt->rt_flags);
1016753c7e08SAugustin Cavalier 		/* XXX stat */
1017753c7e08SAugustin Cavalier 		return NULL;
1018753c7e08SAugustin Cavalier 	}
1019753c7e08SAugustin Cavalier 	if (rt->rt_flags & IEEE80211_MESHRT_FLAGS_PROXY) {
1020753c7e08SAugustin Cavalier 		rt = ieee80211_mesh_rt_find(vap, rt->rt_mesh_gate);
1021753c7e08SAugustin Cavalier 		if (rt == NULL) return NULL;
1022753c7e08SAugustin Cavalier 		if ((rt->rt_flags & IEEE80211_MESHRT_FLAGS_VALID) == 0) {
1023753c7e08SAugustin Cavalier 			IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH, dest,
1024753c7e08SAugustin Cavalier 			    "%s: meshgate !valid, flags 0x%x", __func__,
1025753c7e08SAugustin Cavalier 			    rt->rt_flags);
1026753c7e08SAugustin Cavalier 			/* XXX stat */
1027753c7e08SAugustin Cavalier 			return NULL;
1028753c7e08SAugustin Cavalier 		}
1029753c7e08SAugustin Cavalier 	}
1030753c7e08SAugustin Cavalier 	return ieee80211_find_txnode(vap, rt->rt_nexthop);
1031753c7e08SAugustin Cavalier }
1032753c7e08SAugustin Cavalier 
1033753c7e08SAugustin Cavalier static void
mesh_transmit_to_gate(struct ieee80211vap * vap,struct mbuf * m,struct ieee80211_mesh_route * rt_gate)1034753c7e08SAugustin Cavalier mesh_transmit_to_gate(struct ieee80211vap *vap, struct mbuf *m,
1035753c7e08SAugustin Cavalier     struct ieee80211_mesh_route *rt_gate)
1036753c7e08SAugustin Cavalier {
1037753c7e08SAugustin Cavalier 	struct ifnet *ifp = vap->iv_ifp;
1038753c7e08SAugustin Cavalier 	struct ieee80211_node *ni;
1039753c7e08SAugustin Cavalier 
1040753c7e08SAugustin Cavalier 	IEEE80211_TX_UNLOCK_ASSERT(vap->iv_ic);
1041753c7e08SAugustin Cavalier 
1042753c7e08SAugustin Cavalier 	ni = ieee80211_mesh_find_txnode(vap, rt_gate->rt_dest);
1043753c7e08SAugustin Cavalier 	if (ni == NULL) {
1044753c7e08SAugustin Cavalier 		if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1045753c7e08SAugustin Cavalier 		m_freem(m);
1046753c7e08SAugustin Cavalier 		return;
1047753c7e08SAugustin Cavalier 	}
1048753c7e08SAugustin Cavalier 
1049753c7e08SAugustin Cavalier 	/*
1050753c7e08SAugustin Cavalier 	 * Send through the VAP packet transmit path.
1051753c7e08SAugustin Cavalier 	 * This consumes the node ref grabbed above and
1052753c7e08SAugustin Cavalier 	 * the mbuf, regardless of whether there's a problem
1053753c7e08SAugustin Cavalier 	 * or not.
1054753c7e08SAugustin Cavalier 	 */
1055753c7e08SAugustin Cavalier 	(void) ieee80211_vap_pkt_send_dest(vap, m, ni);
1056753c7e08SAugustin Cavalier }
1057753c7e08SAugustin Cavalier 
1058753c7e08SAugustin Cavalier /*
1059753c7e08SAugustin Cavalier  * Forward the queued frames to known valid mesh gates.
1060753c7e08SAugustin Cavalier  * Assume destination to be outside the MBSS (i.e. proxy entry),
1061753c7e08SAugustin Cavalier  * If no valid mesh gates are known silently discard queued frames.
1062753c7e08SAugustin Cavalier  * After transmitting frames to all known valid mesh gates, this route
1063753c7e08SAugustin Cavalier  * will be marked invalid, and a new path discovery will happen in the hopes
1064753c7e08SAugustin Cavalier  * that (at least) one of the mesh gates have a new proxy entry for us to use.
1065753c7e08SAugustin Cavalier  */
1066753c7e08SAugustin Cavalier void
ieee80211_mesh_forward_to_gates(struct ieee80211vap * vap,struct ieee80211_mesh_route * rt_dest)1067753c7e08SAugustin Cavalier ieee80211_mesh_forward_to_gates(struct ieee80211vap *vap,
1068753c7e08SAugustin Cavalier     struct ieee80211_mesh_route *rt_dest)
1069753c7e08SAugustin Cavalier {
1070753c7e08SAugustin Cavalier 	struct ieee80211com *ic = vap->iv_ic;
1071753c7e08SAugustin Cavalier 	struct ieee80211_mesh_state *ms = vap->iv_mesh;
1072753c7e08SAugustin Cavalier 	struct ieee80211_mesh_route *rt_gate;
1073753c7e08SAugustin Cavalier 	struct ieee80211_mesh_gate_route *gr = NULL, *gr_next;
1074753c7e08SAugustin Cavalier 	struct mbuf *m, *mcopy, *next;
1075753c7e08SAugustin Cavalier 
1076753c7e08SAugustin Cavalier 	IEEE80211_TX_UNLOCK_ASSERT(ic);
1077753c7e08SAugustin Cavalier 
1078753c7e08SAugustin Cavalier 	KASSERT( rt_dest->rt_flags == IEEE80211_MESHRT_FLAGS_DISCOVER,
1079753c7e08SAugustin Cavalier 	    ("Route is not marked with IEEE80211_MESHRT_FLAGS_DISCOVER"));
1080753c7e08SAugustin Cavalier 
1081753c7e08SAugustin Cavalier 	/* XXX: send to more than one valid mash gate */
1082753c7e08SAugustin Cavalier 	MESH_RT_LOCK(ms);
1083753c7e08SAugustin Cavalier 
1084753c7e08SAugustin Cavalier 	m = ieee80211_ageq_remove(&ic->ic_stageq,
1085753c7e08SAugustin Cavalier 	    (struct ieee80211_node *)(uintptr_t)
1086753c7e08SAugustin Cavalier 	    ieee80211_mac_hash(ic, rt_dest->rt_dest));
1087753c7e08SAugustin Cavalier 
1088753c7e08SAugustin Cavalier 	TAILQ_FOREACH_SAFE(gr, &ms->ms_known_gates, gr_next, gr_next) {
1089753c7e08SAugustin Cavalier 		rt_gate = gr->gr_route;
1090753c7e08SAugustin Cavalier 		if (rt_gate == NULL) {
1091753c7e08SAugustin Cavalier 			IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_HWMP,
1092753c7e08SAugustin Cavalier 				rt_dest->rt_dest,
1093753c7e08SAugustin Cavalier 				"mesh gate with no path %6D",
1094753c7e08SAugustin Cavalier 				gr->gr_addr, ":");
1095753c7e08SAugustin Cavalier 			continue;
1096753c7e08SAugustin Cavalier 		}
1097753c7e08SAugustin Cavalier 		if ((rt_gate->rt_flags & IEEE80211_MESHRT_FLAGS_VALID) == 0)
1098753c7e08SAugustin Cavalier 			continue;
1099753c7e08SAugustin Cavalier 		KASSERT(rt_gate->rt_flags & IEEE80211_MESHRT_FLAGS_GATE,
1100753c7e08SAugustin Cavalier 		    ("route not marked as a mesh gate"));
1101753c7e08SAugustin Cavalier 		KASSERT((rt_gate->rt_flags &
1102753c7e08SAugustin Cavalier 			IEEE80211_MESHRT_FLAGS_PROXY) == 0,
1103753c7e08SAugustin Cavalier 			("found mesh gate that is also marked porxy"));
1104753c7e08SAugustin Cavalier 		/*
1105753c7e08SAugustin Cavalier 		 * convert route to a proxy route gated by the current
1106753c7e08SAugustin Cavalier 		 * mesh gate, this is needed so encap can built data
1107753c7e08SAugustin Cavalier 		 * frame with correct address.
1108753c7e08SAugustin Cavalier 		 */
1109753c7e08SAugustin Cavalier 		rt_dest->rt_flags = IEEE80211_MESHRT_FLAGS_PROXY |
1110753c7e08SAugustin Cavalier 			IEEE80211_MESHRT_FLAGS_VALID;
1111753c7e08SAugustin Cavalier 		rt_dest->rt_ext_seq = 1; /* random value */
1112753c7e08SAugustin Cavalier 		IEEE80211_ADDR_COPY(rt_dest->rt_mesh_gate, rt_gate->rt_dest);
1113753c7e08SAugustin Cavalier 		IEEE80211_ADDR_COPY(rt_dest->rt_nexthop, rt_gate->rt_nexthop);
1114753c7e08SAugustin Cavalier 		rt_dest->rt_metric = rt_gate->rt_metric;
1115753c7e08SAugustin Cavalier 		rt_dest->rt_nhops = rt_gate->rt_nhops;
1116753c7e08SAugustin Cavalier 		ieee80211_mesh_rt_update(rt_dest, ms->ms_ppath->mpp_inact);
1117753c7e08SAugustin Cavalier 		MESH_RT_UNLOCK(ms);
1118753c7e08SAugustin Cavalier 		/* XXX: lock?? */
111986021fd4SAugustin Cavalier 		mcopy = m_dup(m, IEEE80211_M_NOWAIT);
1120753c7e08SAugustin Cavalier 		for (; mcopy != NULL; mcopy = next) {
1121753c7e08SAugustin Cavalier 			next = mcopy->m_nextpkt;
1122753c7e08SAugustin Cavalier 			mcopy->m_nextpkt = NULL;
1123753c7e08SAugustin Cavalier 			IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_HWMP,
1124753c7e08SAugustin Cavalier 			    rt_dest->rt_dest,
1125753c7e08SAugustin Cavalier 			    "flush queued frame %p len %d", mcopy,
1126753c7e08SAugustin Cavalier 			    mcopy->m_pkthdr.len);
1127753c7e08SAugustin Cavalier 			mesh_transmit_to_gate(vap, mcopy, rt_gate);
1128753c7e08SAugustin Cavalier 		}
1129753c7e08SAugustin Cavalier 		MESH_RT_LOCK(ms);
1130753c7e08SAugustin Cavalier 	}
1131753c7e08SAugustin Cavalier 	rt_dest->rt_flags = 0; /* Mark invalid */
1132753c7e08SAugustin Cavalier 	m_freem(m);
1133753c7e08SAugustin Cavalier 	MESH_RT_UNLOCK(ms);
1134753c7e08SAugustin Cavalier }
1135753c7e08SAugustin Cavalier 
1136753c7e08SAugustin Cavalier /*
1137753c7e08SAugustin Cavalier  * Forward the specified frame.
1138753c7e08SAugustin Cavalier  * Decrement the TTL and set TA to our MAC address.
1139753c7e08SAugustin Cavalier  */
1140753c7e08SAugustin Cavalier static void
mesh_forward(struct ieee80211vap * vap,struct mbuf * m,const struct ieee80211_meshcntl * mc)1141753c7e08SAugustin Cavalier mesh_forward(struct ieee80211vap *vap, struct mbuf *m,
1142753c7e08SAugustin Cavalier     const struct ieee80211_meshcntl *mc)
1143753c7e08SAugustin Cavalier {
1144753c7e08SAugustin Cavalier 	struct ieee80211com *ic = vap->iv_ic;
1145753c7e08SAugustin Cavalier 	struct ieee80211_mesh_state *ms = vap->iv_mesh;
1146753c7e08SAugustin Cavalier 	struct ifnet *ifp = vap->iv_ifp;
1147753c7e08SAugustin Cavalier 	const struct ieee80211_frame *wh =
1148753c7e08SAugustin Cavalier 	    mtod(m, const struct ieee80211_frame *);
1149753c7e08SAugustin Cavalier 	struct mbuf *mcopy;
1150753c7e08SAugustin Cavalier 	struct ieee80211_meshcntl *mccopy;
1151753c7e08SAugustin Cavalier 	struct ieee80211_frame *whcopy;
1152753c7e08SAugustin Cavalier 	struct ieee80211_node *ni;
1153753c7e08SAugustin Cavalier 	int err;
1154753c7e08SAugustin Cavalier 
1155753c7e08SAugustin Cavalier 	/* This is called from the RX path - don't hold this lock */
1156753c7e08SAugustin Cavalier 	IEEE80211_TX_UNLOCK_ASSERT(ic);
1157753c7e08SAugustin Cavalier 
1158753c7e08SAugustin Cavalier 	/*
1159753c7e08SAugustin Cavalier 	 * mesh ttl of 1 means we are the last one receiving it,
1160753c7e08SAugustin Cavalier 	 * according to amendment we decrement and then check if
1161753c7e08SAugustin Cavalier 	 * 0, if so we dont forward.
1162753c7e08SAugustin Cavalier 	 */
1163753c7e08SAugustin Cavalier 	if (mc->mc_ttl < 1) {
1164753c7e08SAugustin Cavalier 		IEEE80211_NOTE_FRAME(vap, IEEE80211_MSG_MESH, wh,
1165753c7e08SAugustin Cavalier 		    "%s", "frame not fwd'd, ttl 1");
1166753c7e08SAugustin Cavalier 		vap->iv_stats.is_mesh_fwd_ttl++;
1167753c7e08SAugustin Cavalier 		return;
1168753c7e08SAugustin Cavalier 	}
1169753c7e08SAugustin Cavalier 	if (!(ms->ms_flags & IEEE80211_MESHFLAGS_FWD)) {
1170753c7e08SAugustin Cavalier 		IEEE80211_NOTE_FRAME(vap, IEEE80211_MSG_MESH, wh,
1171753c7e08SAugustin Cavalier 		    "%s", "frame not fwd'd, fwding disabled");
1172753c7e08SAugustin Cavalier 		vap->iv_stats.is_mesh_fwd_disabled++;
1173753c7e08SAugustin Cavalier 		return;
1174753c7e08SAugustin Cavalier 	}
117586021fd4SAugustin Cavalier 	mcopy = m_dup(m, IEEE80211_M_NOWAIT);
1176753c7e08SAugustin Cavalier 	if (mcopy == NULL) {
1177753c7e08SAugustin Cavalier 		IEEE80211_NOTE_FRAME(vap, IEEE80211_MSG_MESH, wh,
1178753c7e08SAugustin Cavalier 		    "%s", "frame not fwd'd, cannot dup");
1179753c7e08SAugustin Cavalier 		vap->iv_stats.is_mesh_fwd_nobuf++;
1180753c7e08SAugustin Cavalier 		if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1181753c7e08SAugustin Cavalier 		return;
1182753c7e08SAugustin Cavalier 	}
1183753c7e08SAugustin Cavalier 	mcopy = m_pullup(mcopy, ieee80211_hdrspace(ic, wh) +
1184753c7e08SAugustin Cavalier 	    sizeof(struct ieee80211_meshcntl));
1185753c7e08SAugustin Cavalier 	if (mcopy == NULL) {
1186753c7e08SAugustin Cavalier 		IEEE80211_NOTE_FRAME(vap, IEEE80211_MSG_MESH, wh,
1187753c7e08SAugustin Cavalier 		    "%s", "frame not fwd'd, too short");
1188753c7e08SAugustin Cavalier 		vap->iv_stats.is_mesh_fwd_tooshort++;
1189753c7e08SAugustin Cavalier 		if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1190753c7e08SAugustin Cavalier 		m_freem(mcopy);
1191753c7e08SAugustin Cavalier 		return;
1192753c7e08SAugustin Cavalier 	}
1193753c7e08SAugustin Cavalier 	whcopy = mtod(mcopy, struct ieee80211_frame *);
1194753c7e08SAugustin Cavalier 	mccopy = (struct ieee80211_meshcntl *)
1195753c7e08SAugustin Cavalier 	    (mtod(mcopy, uint8_t *) + ieee80211_hdrspace(ic, wh));
1196753c7e08SAugustin Cavalier 	/* XXX clear other bits? */
1197753c7e08SAugustin Cavalier 	whcopy->i_fc[1] &= ~IEEE80211_FC1_RETRY;
1198753c7e08SAugustin Cavalier 	IEEE80211_ADDR_COPY(whcopy->i_addr2, vap->iv_myaddr);
1199753c7e08SAugustin Cavalier 	if (IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1200753c7e08SAugustin Cavalier 		ni = ieee80211_ref_node(vap->iv_bss);
1201753c7e08SAugustin Cavalier 		mcopy->m_flags |= M_MCAST;
1202753c7e08SAugustin Cavalier 	} else {
1203753c7e08SAugustin Cavalier 		ni = ieee80211_mesh_find_txnode(vap, whcopy->i_addr3);
1204753c7e08SAugustin Cavalier 		if (ni == NULL) {
1205753c7e08SAugustin Cavalier 			/*
1206753c7e08SAugustin Cavalier 			 * [Optional] any of the following three actions:
1207753c7e08SAugustin Cavalier 			 * o silently discard
1208753c7e08SAugustin Cavalier 			 * o trigger a path discovery
1209753c7e08SAugustin Cavalier 			 * o inform TA that meshDA is unknown.
1210753c7e08SAugustin Cavalier 			 */
1211753c7e08SAugustin Cavalier 			IEEE80211_NOTE_FRAME(vap, IEEE80211_MSG_MESH, wh,
1212753c7e08SAugustin Cavalier 			    "%s", "frame not fwd'd, no path");
1213753c7e08SAugustin Cavalier 			ms->ms_ppath->mpp_senderror(vap, whcopy->i_addr3, NULL,
1214753c7e08SAugustin Cavalier 			    IEEE80211_REASON_MESH_PERR_NO_FI);
1215753c7e08SAugustin Cavalier 			vap->iv_stats.is_mesh_fwd_nopath++;
1216753c7e08SAugustin Cavalier 			m_freem(mcopy);
1217753c7e08SAugustin Cavalier 			return;
1218753c7e08SAugustin Cavalier 		}
1219753c7e08SAugustin Cavalier 		IEEE80211_ADDR_COPY(whcopy->i_addr1, ni->ni_macaddr);
1220753c7e08SAugustin Cavalier 	}
1221753c7e08SAugustin Cavalier 	KASSERT(mccopy->mc_ttl > 0, ("%s called with wrong ttl", __func__));
1222753c7e08SAugustin Cavalier 	mccopy->mc_ttl--;
1223753c7e08SAugustin Cavalier 
1224753c7e08SAugustin Cavalier 	/* XXX calculate priority so drivers can find the tx queue */
1225753c7e08SAugustin Cavalier 	M_WME_SETAC(mcopy, WME_AC_BE);
1226753c7e08SAugustin Cavalier 
1227753c7e08SAugustin Cavalier 	/* XXX do we know m_nextpkt is NULL? */
122886021fd4SAugustin Cavalier 	MPASS((mcopy->m_pkthdr.csum_flags & CSUM_SND_TAG) == 0);
1229753c7e08SAugustin Cavalier 	mcopy->m_pkthdr.rcvif = (void *) ni;
1230753c7e08SAugustin Cavalier 
1231753c7e08SAugustin Cavalier 	/*
1232753c7e08SAugustin Cavalier 	 * XXX this bypasses all of the VAP TX handling; it passes frames
1233753c7e08SAugustin Cavalier 	 * directly to the parent interface.
1234753c7e08SAugustin Cavalier 	 *
1235753c7e08SAugustin Cavalier 	 * Because of this, there's no TX lock being held as there's no
1236753c7e08SAugustin Cavalier 	 * encaps state being used.
1237753c7e08SAugustin Cavalier 	 *
1238753c7e08SAugustin Cavalier 	 * Doing a direct parent transmit may not be the correct thing
1239753c7e08SAugustin Cavalier 	 * to do here; we'll have to re-think this soon.
1240753c7e08SAugustin Cavalier 	 */
1241753c7e08SAugustin Cavalier 	IEEE80211_TX_LOCK(ic);
1242753c7e08SAugustin Cavalier 	err = ieee80211_parent_xmitpkt(ic, mcopy);
1243753c7e08SAugustin Cavalier 	IEEE80211_TX_UNLOCK(ic);
1244753c7e08SAugustin Cavalier 	if (!err)
1245753c7e08SAugustin Cavalier 		if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
1246753c7e08SAugustin Cavalier }
1247753c7e08SAugustin Cavalier 
1248753c7e08SAugustin Cavalier static struct mbuf *
mesh_decap(struct ieee80211vap * vap,struct mbuf * m,int hdrlen,int meshdrlen)1249753c7e08SAugustin Cavalier mesh_decap(struct ieee80211vap *vap, struct mbuf *m, int hdrlen, int meshdrlen)
1250753c7e08SAugustin Cavalier {
1251753c7e08SAugustin Cavalier #define	WHDIR(wh)	((wh)->i_fc[1] & IEEE80211_FC1_DIR_MASK)
1252753c7e08SAugustin Cavalier #define	MC01(mc)	((const struct ieee80211_meshcntl_ae01 *)mc)
1253753c7e08SAugustin Cavalier 	uint8_t b[sizeof(struct ieee80211_qosframe_addr4) +
1254753c7e08SAugustin Cavalier 		  sizeof(struct ieee80211_meshcntl_ae10)];
1255753c7e08SAugustin Cavalier 	const struct ieee80211_qosframe_addr4 *wh;
1256753c7e08SAugustin Cavalier 	const struct ieee80211_meshcntl_ae10 *mc;
1257753c7e08SAugustin Cavalier 	struct ether_header *eh;
1258753c7e08SAugustin Cavalier 	struct llc *llc;
1259753c7e08SAugustin Cavalier 	int ae;
1260753c7e08SAugustin Cavalier 
1261753c7e08SAugustin Cavalier 	if (m->m_len < hdrlen + sizeof(*llc) &&
1262753c7e08SAugustin Cavalier 	    (m = m_pullup(m, hdrlen + sizeof(*llc))) == NULL) {
1263753c7e08SAugustin Cavalier 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_ANY,
1264753c7e08SAugustin Cavalier 		    "discard data frame: %s", "m_pullup failed");
1265753c7e08SAugustin Cavalier 		vap->iv_stats.is_rx_tooshort++;
1266753c7e08SAugustin Cavalier 		return NULL;
1267753c7e08SAugustin Cavalier 	}
1268753c7e08SAugustin Cavalier 	memcpy(b, mtod(m, caddr_t), hdrlen);
1269753c7e08SAugustin Cavalier 	wh = (const struct ieee80211_qosframe_addr4 *)&b[0];
1270753c7e08SAugustin Cavalier 	mc = (const struct ieee80211_meshcntl_ae10 *)&b[hdrlen - meshdrlen];
1271753c7e08SAugustin Cavalier 	KASSERT(WHDIR(wh) == IEEE80211_FC1_DIR_FROMDS ||
1272753c7e08SAugustin Cavalier 		WHDIR(wh) == IEEE80211_FC1_DIR_DSTODS,
1273753c7e08SAugustin Cavalier 	    ("bogus dir, fc 0x%x:0x%x", wh->i_fc[0], wh->i_fc[1]));
1274753c7e08SAugustin Cavalier 
1275753c7e08SAugustin Cavalier 	llc = (struct llc *)(mtod(m, caddr_t) + hdrlen);
1276753c7e08SAugustin Cavalier 	if (llc->llc_dsap == LLC_SNAP_LSAP && llc->llc_ssap == LLC_SNAP_LSAP &&
1277753c7e08SAugustin Cavalier 	    llc->llc_control == LLC_UI && llc->llc_snap.org_code[0] == 0 &&
1278753c7e08SAugustin Cavalier 	    llc->llc_snap.org_code[1] == 0 && llc->llc_snap.org_code[2] == 0 &&
1279753c7e08SAugustin Cavalier 	    /* NB: preserve AppleTalk frames that have a native SNAP hdr */
1280753c7e08SAugustin Cavalier 	    !(llc->llc_snap.ether_type == htons(ETHERTYPE_AARP) ||
1281753c7e08SAugustin Cavalier 	      llc->llc_snap.ether_type == htons(ETHERTYPE_IPX))) {
1282753c7e08SAugustin Cavalier 		m_adj(m, hdrlen + sizeof(struct llc) - sizeof(*eh));
1283753c7e08SAugustin Cavalier 		llc = NULL;
1284753c7e08SAugustin Cavalier 	} else {
1285753c7e08SAugustin Cavalier 		m_adj(m, hdrlen - sizeof(*eh));
1286753c7e08SAugustin Cavalier 	}
1287753c7e08SAugustin Cavalier 	eh = mtod(m, struct ether_header *);
1288753c7e08SAugustin Cavalier 	ae = mc->mc_flags & IEEE80211_MESH_AE_MASK;
1289753c7e08SAugustin Cavalier 	if (WHDIR(wh) == IEEE80211_FC1_DIR_FROMDS) {
1290753c7e08SAugustin Cavalier 		IEEE80211_ADDR_COPY(eh->ether_dhost, wh->i_addr1);
1291753c7e08SAugustin Cavalier 		if (ae == IEEE80211_MESH_AE_00) {
1292753c7e08SAugustin Cavalier 			IEEE80211_ADDR_COPY(eh->ether_shost, wh->i_addr3);
1293753c7e08SAugustin Cavalier 		} else if (ae == IEEE80211_MESH_AE_01) {
1294753c7e08SAugustin Cavalier 			IEEE80211_ADDR_COPY(eh->ether_shost,
1295753c7e08SAugustin Cavalier 			    MC01(mc)->mc_addr4);
1296753c7e08SAugustin Cavalier 		} else {
1297753c7e08SAugustin Cavalier 			IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
1298753c7e08SAugustin Cavalier 			    (const struct ieee80211_frame *)wh, NULL,
1299753c7e08SAugustin Cavalier 			    "bad AE %d", ae);
1300753c7e08SAugustin Cavalier 			vap->iv_stats.is_mesh_badae++;
1301753c7e08SAugustin Cavalier 			m_freem(m);
1302753c7e08SAugustin Cavalier 			return NULL;
1303753c7e08SAugustin Cavalier 		}
1304753c7e08SAugustin Cavalier 	} else {
1305753c7e08SAugustin Cavalier 		if (ae == IEEE80211_MESH_AE_00) {
1306753c7e08SAugustin Cavalier 			IEEE80211_ADDR_COPY(eh->ether_dhost, wh->i_addr3);
1307753c7e08SAugustin Cavalier 			IEEE80211_ADDR_COPY(eh->ether_shost, wh->i_addr4);
1308753c7e08SAugustin Cavalier 		} else if (ae == IEEE80211_MESH_AE_10) {
1309753c7e08SAugustin Cavalier 			IEEE80211_ADDR_COPY(eh->ether_dhost, mc->mc_addr5);
1310753c7e08SAugustin Cavalier 			IEEE80211_ADDR_COPY(eh->ether_shost, mc->mc_addr6);
1311753c7e08SAugustin Cavalier 		} else {
1312753c7e08SAugustin Cavalier 			IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
1313753c7e08SAugustin Cavalier 			    (const struct ieee80211_frame *)wh, NULL,
1314753c7e08SAugustin Cavalier 			    "bad AE %d", ae);
1315753c7e08SAugustin Cavalier 			vap->iv_stats.is_mesh_badae++;
1316753c7e08SAugustin Cavalier 			m_freem(m);
1317753c7e08SAugustin Cavalier 			return NULL;
1318753c7e08SAugustin Cavalier 		}
1319753c7e08SAugustin Cavalier 	}
1320753c7e08SAugustin Cavalier #ifndef __NO_STRICT_ALIGNMENT
1321753c7e08SAugustin Cavalier 	if (!ALIGNED_POINTER(mtod(m, caddr_t) + sizeof(*eh), uint32_t)) {
1322753c7e08SAugustin Cavalier 		m = ieee80211_realign(vap, m, sizeof(*eh));
1323753c7e08SAugustin Cavalier 		if (m == NULL)
1324753c7e08SAugustin Cavalier 			return NULL;
1325753c7e08SAugustin Cavalier 	}
1326753c7e08SAugustin Cavalier #endif /* !__NO_STRICT_ALIGNMENT */
1327753c7e08SAugustin Cavalier 	if (llc != NULL) {
1328753c7e08SAugustin Cavalier 		eh = mtod(m, struct ether_header *);
1329753c7e08SAugustin Cavalier 		eh->ether_type = htons(m->m_pkthdr.len - sizeof(*eh));
1330753c7e08SAugustin Cavalier 	}
1331753c7e08SAugustin Cavalier 	return m;
1332753c7e08SAugustin Cavalier #undef	WDIR
1333753c7e08SAugustin Cavalier #undef	MC01
1334753c7e08SAugustin Cavalier }
1335753c7e08SAugustin Cavalier 
1336753c7e08SAugustin Cavalier /*
1337753c7e08SAugustin Cavalier  * Return non-zero if the unicast mesh data frame should be processed
1338753c7e08SAugustin Cavalier  * locally.  Frames that are not proxy'd have our address, otherwise
1339753c7e08SAugustin Cavalier  * we need to consult the routing table to look for a proxy entry.
1340753c7e08SAugustin Cavalier  */
1341753c7e08SAugustin Cavalier static __inline int
mesh_isucastforme(struct ieee80211vap * vap,const struct ieee80211_frame * wh,const struct ieee80211_meshcntl * mc)1342753c7e08SAugustin Cavalier mesh_isucastforme(struct ieee80211vap *vap, const struct ieee80211_frame *wh,
1343753c7e08SAugustin Cavalier     const struct ieee80211_meshcntl *mc)
1344753c7e08SAugustin Cavalier {
1345753c7e08SAugustin Cavalier 	int ae = mc->mc_flags & 3;
1346753c7e08SAugustin Cavalier 
1347753c7e08SAugustin Cavalier 	KASSERT((wh->i_fc[1] & IEEE80211_FC1_DIR_MASK) == IEEE80211_FC1_DIR_DSTODS,
1348753c7e08SAugustin Cavalier 	    ("bad dir 0x%x:0x%x", wh->i_fc[0], wh->i_fc[1]));
1349753c7e08SAugustin Cavalier 	KASSERT(ae == IEEE80211_MESH_AE_00 || ae == IEEE80211_MESH_AE_10,
1350753c7e08SAugustin Cavalier 	    ("bad AE %d", ae));
1351753c7e08SAugustin Cavalier 	if (ae == IEEE80211_MESH_AE_10) {	/* ucast w/ proxy */
1352753c7e08SAugustin Cavalier 		const struct ieee80211_meshcntl_ae10 *mc10 =
1353753c7e08SAugustin Cavalier 		    (const struct ieee80211_meshcntl_ae10 *) mc;
1354753c7e08SAugustin Cavalier 		struct ieee80211_mesh_route *rt =
1355753c7e08SAugustin Cavalier 		    ieee80211_mesh_rt_find(vap, mc10->mc_addr5);
1356753c7e08SAugustin Cavalier 		/* check for proxy route to ourself */
1357753c7e08SAugustin Cavalier 		return (rt != NULL &&
1358753c7e08SAugustin Cavalier 		    (rt->rt_flags & IEEE80211_MESHRT_FLAGS_PROXY));
1359753c7e08SAugustin Cavalier 	} else					/* ucast w/o proxy */
1360753c7e08SAugustin Cavalier 		return IEEE80211_ADDR_EQ(wh->i_addr3, vap->iv_myaddr);
1361753c7e08SAugustin Cavalier }
1362753c7e08SAugustin Cavalier 
1363753c7e08SAugustin Cavalier /*
1364753c7e08SAugustin Cavalier  * Verifies transmitter, updates lifetime, precursor list and forwards data.
1365753c7e08SAugustin Cavalier  * > 0 means we have forwarded data and no need to process locally
1366753c7e08SAugustin Cavalier  * == 0 means we want to process locally (and we may have forwarded data
1367753c7e08SAugustin Cavalier  * < 0 means there was an error and data should be discarded
1368753c7e08SAugustin Cavalier  */
1369753c7e08SAugustin Cavalier static int
mesh_recv_indiv_data_to_fwrd(struct ieee80211vap * vap,struct mbuf * m,struct ieee80211_frame * wh,const struct ieee80211_meshcntl * mc)1370753c7e08SAugustin Cavalier mesh_recv_indiv_data_to_fwrd(struct ieee80211vap *vap, struct mbuf *m,
1371753c7e08SAugustin Cavalier     struct ieee80211_frame *wh, const struct ieee80211_meshcntl *mc)
1372753c7e08SAugustin Cavalier {
1373753c7e08SAugustin Cavalier 	struct ieee80211_qosframe_addr4 *qwh;
1374753c7e08SAugustin Cavalier 	struct ieee80211_mesh_state *ms = vap->iv_mesh;
1375753c7e08SAugustin Cavalier 	struct ieee80211_mesh_route *rt_meshda, *rt_meshsa;
1376753c7e08SAugustin Cavalier 
1377753c7e08SAugustin Cavalier 	/* This is called from the RX path - don't hold this lock */
1378753c7e08SAugustin Cavalier 	IEEE80211_TX_UNLOCK_ASSERT(vap->iv_ic);
1379753c7e08SAugustin Cavalier 
1380753c7e08SAugustin Cavalier 	qwh = (struct ieee80211_qosframe_addr4 *)wh;
1381753c7e08SAugustin Cavalier 
1382753c7e08SAugustin Cavalier 	/*
1383753c7e08SAugustin Cavalier 	 * TODO:
1384753c7e08SAugustin Cavalier 	 * o verify addr2 is  a legitimate transmitter
1385753c7e08SAugustin Cavalier 	 * o lifetime of precursor of addr3 (addr2) is max(init, curr)
1386753c7e08SAugustin Cavalier 	 * o lifetime of precursor of addr4 (nexthop) is max(init, curr)
1387753c7e08SAugustin Cavalier 	 */
1388753c7e08SAugustin Cavalier 
1389753c7e08SAugustin Cavalier 	/* set lifetime of addr3 (meshDA) to initial value */
1390753c7e08SAugustin Cavalier 	rt_meshda = ieee80211_mesh_rt_find(vap, qwh->i_addr3);
1391753c7e08SAugustin Cavalier 	if (rt_meshda == NULL) {
1392753c7e08SAugustin Cavalier 		IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH, qwh->i_addr2,
1393753c7e08SAugustin Cavalier 		    "no route to meshDA(%6D)", qwh->i_addr3, ":");
1394753c7e08SAugustin Cavalier 		/*
1395753c7e08SAugustin Cavalier 		 * [Optional] any of the following three actions:
1396753c7e08SAugustin Cavalier 		 * o silently discard 				[X]
1397753c7e08SAugustin Cavalier 		 * o trigger a path discovery			[ ]
1398753c7e08SAugustin Cavalier 		 * o inform TA that meshDA is unknown.		[ ]
1399753c7e08SAugustin Cavalier 		 */
1400753c7e08SAugustin Cavalier 		/* XXX: stats */
1401753c7e08SAugustin Cavalier 		return (-1);
1402753c7e08SAugustin Cavalier 	}
1403753c7e08SAugustin Cavalier 
1404753c7e08SAugustin Cavalier 	ieee80211_mesh_rt_update(rt_meshda, ticks_to_msecs(
1405753c7e08SAugustin Cavalier 	    ms->ms_ppath->mpp_inact));
1406753c7e08SAugustin Cavalier 
1407753c7e08SAugustin Cavalier 	/* set lifetime of addr4 (meshSA) to initial value */
1408753c7e08SAugustin Cavalier 	rt_meshsa = ieee80211_mesh_rt_find(vap, qwh->i_addr4);
1409753c7e08SAugustin Cavalier 	KASSERT(rt_meshsa != NULL, ("no route"));
1410753c7e08SAugustin Cavalier 	ieee80211_mesh_rt_update(rt_meshsa, ticks_to_msecs(
1411753c7e08SAugustin Cavalier 	    ms->ms_ppath->mpp_inact));
1412753c7e08SAugustin Cavalier 
1413753c7e08SAugustin Cavalier 	mesh_forward(vap, m, mc);
1414753c7e08SAugustin Cavalier 	return (1); /* dont process locally */
1415753c7e08SAugustin Cavalier }
1416753c7e08SAugustin Cavalier 
1417753c7e08SAugustin Cavalier /*
1418753c7e08SAugustin Cavalier  * Verifies transmitter, updates lifetime, precursor list and process data
1419753c7e08SAugustin Cavalier  * locally, if data is proxy with AE = 10 it could mean data should go
1420753c7e08SAugustin Cavalier  * on another mesh path or data should be forwarded to the DS.
1421753c7e08SAugustin Cavalier  *
1422753c7e08SAugustin Cavalier  * > 0 means we have forwarded data and no need to process locally
1423753c7e08SAugustin Cavalier  * == 0 means we want to process locally (and we may have forwarded data
1424753c7e08SAugustin Cavalier  * < 0 means there was an error and data should be discarded
1425753c7e08SAugustin Cavalier  */
1426753c7e08SAugustin Cavalier static int
mesh_recv_indiv_data_to_me(struct ieee80211vap * vap,struct mbuf * m,struct ieee80211_frame * wh,const struct ieee80211_meshcntl * mc)1427753c7e08SAugustin Cavalier mesh_recv_indiv_data_to_me(struct ieee80211vap *vap, struct mbuf *m,
1428753c7e08SAugustin Cavalier     struct ieee80211_frame *wh, const struct ieee80211_meshcntl *mc)
1429753c7e08SAugustin Cavalier {
1430753c7e08SAugustin Cavalier 	struct ieee80211_qosframe_addr4 *qwh;
1431753c7e08SAugustin Cavalier 	const struct ieee80211_meshcntl_ae10 *mc10;
1432753c7e08SAugustin Cavalier 	struct ieee80211_mesh_state *ms = vap->iv_mesh;
1433753c7e08SAugustin Cavalier 	struct ieee80211_mesh_route *rt;
1434753c7e08SAugustin Cavalier 	int ae;
1435753c7e08SAugustin Cavalier 
1436753c7e08SAugustin Cavalier 	/* This is called from the RX path - don't hold this lock */
1437753c7e08SAugustin Cavalier 	IEEE80211_TX_UNLOCK_ASSERT(vap->iv_ic);
1438753c7e08SAugustin Cavalier 
1439753c7e08SAugustin Cavalier 	qwh = (struct ieee80211_qosframe_addr4 *)wh;
1440753c7e08SAugustin Cavalier 	mc10 = (const struct ieee80211_meshcntl_ae10 *)mc;
1441753c7e08SAugustin Cavalier 
1442753c7e08SAugustin Cavalier 	/*
1443753c7e08SAugustin Cavalier 	 * TODO:
1444753c7e08SAugustin Cavalier 	 * o verify addr2 is  a legitimate transmitter
1445753c7e08SAugustin Cavalier 	 * o lifetime of precursor entry is max(init, curr)
1446753c7e08SAugustin Cavalier 	 */
1447753c7e08SAugustin Cavalier 
1448753c7e08SAugustin Cavalier 	/* set lifetime of addr4 (meshSA) to initial value */
1449753c7e08SAugustin Cavalier 	rt = ieee80211_mesh_rt_find(vap, qwh->i_addr4);
1450753c7e08SAugustin Cavalier 	KASSERT(rt != NULL, ("no route"));
1451753c7e08SAugustin Cavalier 	ieee80211_mesh_rt_update(rt, ticks_to_msecs(ms->ms_ppath->mpp_inact));
1452753c7e08SAugustin Cavalier 	rt = NULL;
1453753c7e08SAugustin Cavalier 
1454753c7e08SAugustin Cavalier 	ae = mc10->mc_flags & IEEE80211_MESH_AE_MASK;
1455753c7e08SAugustin Cavalier 	KASSERT(ae == IEEE80211_MESH_AE_00 ||
1456753c7e08SAugustin Cavalier 	    ae == IEEE80211_MESH_AE_10, ("bad AE %d", ae));
1457753c7e08SAugustin Cavalier 	if (ae == IEEE80211_MESH_AE_10) {
1458753c7e08SAugustin Cavalier 		if (IEEE80211_ADDR_EQ(mc10->mc_addr5, qwh->i_addr3)) {
1459753c7e08SAugustin Cavalier 			return (0); /* process locally */
1460753c7e08SAugustin Cavalier 		}
1461753c7e08SAugustin Cavalier 
1462753c7e08SAugustin Cavalier 		rt =  ieee80211_mesh_rt_find(vap, mc10->mc_addr5);
1463753c7e08SAugustin Cavalier 		if (rt != NULL &&
1464753c7e08SAugustin Cavalier 		    (rt->rt_flags & IEEE80211_MESHRT_FLAGS_VALID) &&
1465753c7e08SAugustin Cavalier 		    (rt->rt_flags & IEEE80211_MESHRT_FLAGS_PROXY) == 0) {
1466753c7e08SAugustin Cavalier 			/*
1467753c7e08SAugustin Cavalier 			 * Forward on another mesh-path, according to
1468753c7e08SAugustin Cavalier 			 * amendment as specified in 9.32.4.1
1469753c7e08SAugustin Cavalier 			 */
1470753c7e08SAugustin Cavalier 			IEEE80211_ADDR_COPY(qwh->i_addr3, mc10->mc_addr5);
1471753c7e08SAugustin Cavalier 			mesh_forward(vap, m,
1472753c7e08SAugustin Cavalier 			    (const struct ieee80211_meshcntl *)mc10);
1473753c7e08SAugustin Cavalier 			return (1); /* dont process locally */
1474753c7e08SAugustin Cavalier 		}
1475753c7e08SAugustin Cavalier 		/*
1476753c7e08SAugustin Cavalier 		 * All other cases: forward of MSDUs from the MBSS to DS indiv.
1477753c7e08SAugustin Cavalier 		 * addressed according to 13.11.3.2.
1478753c7e08SAugustin Cavalier 		 */
1479753c7e08SAugustin Cavalier 		IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_OUTPUT, qwh->i_addr2,
1480753c7e08SAugustin Cavalier 		    "forward frame to DS, SA(%6D) DA(%6D)",
1481753c7e08SAugustin Cavalier 		    mc10->mc_addr6, ":", mc10->mc_addr5, ":");
1482753c7e08SAugustin Cavalier 	}
1483753c7e08SAugustin Cavalier 	return (0); /* process locally */
1484753c7e08SAugustin Cavalier }
1485753c7e08SAugustin Cavalier 
1486753c7e08SAugustin Cavalier /*
1487753c7e08SAugustin Cavalier  * Try to forward the group addressed data on to other mesh STAs, and
1488753c7e08SAugustin Cavalier  * also to the DS.
1489753c7e08SAugustin Cavalier  *
1490753c7e08SAugustin Cavalier  * > 0 means we have forwarded data and no need to process locally
1491753c7e08SAugustin Cavalier  * == 0 means we want to process locally (and we may have forwarded data
1492753c7e08SAugustin Cavalier  * < 0 means there was an error and data should be discarded
1493753c7e08SAugustin Cavalier  */
1494753c7e08SAugustin Cavalier static int
mesh_recv_group_data(struct ieee80211vap * vap,struct mbuf * m,struct ieee80211_frame * wh,const struct ieee80211_meshcntl * mc)1495753c7e08SAugustin Cavalier mesh_recv_group_data(struct ieee80211vap *vap, struct mbuf *m,
1496753c7e08SAugustin Cavalier     struct ieee80211_frame *wh, const struct ieee80211_meshcntl *mc)
1497753c7e08SAugustin Cavalier {
1498753c7e08SAugustin Cavalier #define	MC01(mc)	((const struct ieee80211_meshcntl_ae01 *)mc)
1499753c7e08SAugustin Cavalier 	struct ieee80211_mesh_state *ms = vap->iv_mesh;
1500753c7e08SAugustin Cavalier 
1501753c7e08SAugustin Cavalier 	/* This is called from the RX path - don't hold this lock */
1502753c7e08SAugustin Cavalier 	IEEE80211_TX_UNLOCK_ASSERT(vap->iv_ic);
1503753c7e08SAugustin Cavalier 
1504753c7e08SAugustin Cavalier 	mesh_forward(vap, m, mc);
1505753c7e08SAugustin Cavalier 
1506753c7e08SAugustin Cavalier 	if(mc->mc_ttl > 0) {
1507753c7e08SAugustin Cavalier 		if (mc->mc_flags & IEEE80211_MESH_AE_01) {
1508753c7e08SAugustin Cavalier 			/*
1509753c7e08SAugustin Cavalier 			 * Forward of MSDUs from the MBSS to DS group addressed
1510753c7e08SAugustin Cavalier 			 * (according to 13.11.3.2)
1511753c7e08SAugustin Cavalier 			 * This happens by delivering the packet, and a bridge
1512753c7e08SAugustin Cavalier 			 * will sent it on another port member.
1513753c7e08SAugustin Cavalier 			 */
1514753c7e08SAugustin Cavalier 			if (ms->ms_flags & IEEE80211_MESHFLAGS_GATE &&
1515753c7e08SAugustin Cavalier 			    ms->ms_flags & IEEE80211_MESHFLAGS_FWD) {
1516753c7e08SAugustin Cavalier 				IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH,
1517753c7e08SAugustin Cavalier 				    MC01(mc)->mc_addr4, "%s",
1518753c7e08SAugustin Cavalier 				    "forward from MBSS to the DS");
1519753c7e08SAugustin Cavalier 			}
1520753c7e08SAugustin Cavalier 		}
1521753c7e08SAugustin Cavalier 	}
1522753c7e08SAugustin Cavalier 	return (0); /* process locally */
1523753c7e08SAugustin Cavalier #undef	MC01
1524753c7e08SAugustin Cavalier }
1525753c7e08SAugustin Cavalier 
1526753c7e08SAugustin Cavalier static int
mesh_input(struct ieee80211_node * ni,struct mbuf * m,const struct ieee80211_rx_stats * rxs,int rssi,int nf)1527753c7e08SAugustin Cavalier mesh_input(struct ieee80211_node *ni, struct mbuf *m,
1528753c7e08SAugustin Cavalier     const struct ieee80211_rx_stats *rxs, int rssi, int nf)
1529753c7e08SAugustin Cavalier {
1530753c7e08SAugustin Cavalier #define	HAS_SEQ(type)	((type & 0x4) == 0)
1531753c7e08SAugustin Cavalier #define	MC01(mc)	((const struct ieee80211_meshcntl_ae01 *)mc)
1532753c7e08SAugustin Cavalier 	struct ieee80211vap *vap = ni->ni_vap;
1533753c7e08SAugustin Cavalier 	struct ieee80211com *ic = ni->ni_ic;
1534753c7e08SAugustin Cavalier 	struct ifnet *ifp = vap->iv_ifp;
1535753c7e08SAugustin Cavalier 	struct ieee80211_frame *wh;
1536753c7e08SAugustin Cavalier 	const struct ieee80211_meshcntl *mc;
1537753c7e08SAugustin Cavalier 	int hdrspace, meshdrlen, need_tap, error;
1538753c7e08SAugustin Cavalier 	uint8_t dir, type, subtype, ae;
1539753c7e08SAugustin Cavalier 	uint32_t seq;
1540753c7e08SAugustin Cavalier 	const uint8_t *addr;
1541753c7e08SAugustin Cavalier 	uint8_t qos[2];
1542753c7e08SAugustin Cavalier 
1543753c7e08SAugustin Cavalier 	KASSERT(ni != NULL, ("null node"));
1544753c7e08SAugustin Cavalier 	ni->ni_inact = ni->ni_inact_reload;
1545753c7e08SAugustin Cavalier 
1546753c7e08SAugustin Cavalier 	need_tap = 1;			/* mbuf need to be tapped. */
1547753c7e08SAugustin Cavalier 	type = -1;			/* undefined */
1548753c7e08SAugustin Cavalier 
1549753c7e08SAugustin Cavalier 	/* This is called from the RX path - don't hold this lock */
1550753c7e08SAugustin Cavalier 	IEEE80211_TX_UNLOCK_ASSERT(ic);
1551753c7e08SAugustin Cavalier 
1552753c7e08SAugustin Cavalier 	if (m->m_pkthdr.len < sizeof(struct ieee80211_frame_min)) {
1553753c7e08SAugustin Cavalier 		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
1554753c7e08SAugustin Cavalier 		    ni->ni_macaddr, NULL,
1555753c7e08SAugustin Cavalier 		    "too short (1): len %u", m->m_pkthdr.len);
1556753c7e08SAugustin Cavalier 		vap->iv_stats.is_rx_tooshort++;
1557753c7e08SAugustin Cavalier 		goto out;
1558753c7e08SAugustin Cavalier 	}
1559753c7e08SAugustin Cavalier 	/*
1560753c7e08SAugustin Cavalier 	 * Bit of a cheat here, we use a pointer for a 3-address
1561753c7e08SAugustin Cavalier 	 * frame format but don't reference fields past outside
1562753c7e08SAugustin Cavalier 	 * ieee80211_frame_min w/o first validating the data is
1563753c7e08SAugustin Cavalier 	 * present.
1564753c7e08SAugustin Cavalier 	*/
1565753c7e08SAugustin Cavalier 	wh = mtod(m, struct ieee80211_frame *);
1566753c7e08SAugustin Cavalier 
1567*da3ca318SAugustin Cavalier 	if (!IEEE80211_IS_FC0_CHECK_VER(wh, IEEE80211_FC0_VERSION_0)) {
1568753c7e08SAugustin Cavalier 		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
1569753c7e08SAugustin Cavalier 		    ni->ni_macaddr, NULL, "wrong version %x", wh->i_fc[0]);
1570753c7e08SAugustin Cavalier 		vap->iv_stats.is_rx_badversion++;
1571753c7e08SAugustin Cavalier 		goto err;
1572753c7e08SAugustin Cavalier 	}
1573753c7e08SAugustin Cavalier 	dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK;
1574753c7e08SAugustin Cavalier 	type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
1575753c7e08SAugustin Cavalier 	subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
1576753c7e08SAugustin Cavalier 	if ((ic->ic_flags & IEEE80211_F_SCAN) == 0) {
1577753c7e08SAugustin Cavalier 		IEEE80211_RSSI_LPF(ni->ni_avgrssi, rssi);
1578753c7e08SAugustin Cavalier 		ni->ni_noise = nf;
1579753c7e08SAugustin Cavalier 		if (HAS_SEQ(type)) {
1580753c7e08SAugustin Cavalier 			uint8_t tid = ieee80211_gettid(wh);
1581753c7e08SAugustin Cavalier 
1582753c7e08SAugustin Cavalier 			if (IEEE80211_QOS_HAS_SEQ(wh) &&
1583753c7e08SAugustin Cavalier 			    TID_TO_WME_AC(tid) >= WME_AC_VI)
1584753c7e08SAugustin Cavalier 				ic->ic_wme.wme_hipri_traffic++;
15858244a9baSAugustin Cavalier 			if (! ieee80211_check_rxseq(ni, wh, wh->i_addr1, rxs))
1586753c7e08SAugustin Cavalier 				goto out;
1587753c7e08SAugustin Cavalier 		}
1588753c7e08SAugustin Cavalier 	}
1589753c7e08SAugustin Cavalier #ifdef IEEE80211_DEBUG
1590753c7e08SAugustin Cavalier 	/*
1591753c7e08SAugustin Cavalier 	 * It's easier, but too expensive, to simulate different mesh
1592753c7e08SAugustin Cavalier 	 * topologies by consulting the ACL policy very early, so do this
1593753c7e08SAugustin Cavalier 	 * only under DEBUG.
1594753c7e08SAugustin Cavalier 	 *
1595753c7e08SAugustin Cavalier 	 * NB: this check is also done upon peering link initiation.
1596753c7e08SAugustin Cavalier 	 */
1597753c7e08SAugustin Cavalier 	if (vap->iv_acl != NULL && !vap->iv_acl->iac_check(vap, wh)) {
1598753c7e08SAugustin Cavalier 		IEEE80211_DISCARD(vap, IEEE80211_MSG_ACL,
1599753c7e08SAugustin Cavalier 		    wh, NULL, "%s", "disallowed by ACL");
1600753c7e08SAugustin Cavalier 		vap->iv_stats.is_rx_acl++;
1601753c7e08SAugustin Cavalier 		goto out;
1602753c7e08SAugustin Cavalier 	}
1603753c7e08SAugustin Cavalier #endif
1604753c7e08SAugustin Cavalier 	switch (type) {
1605753c7e08SAugustin Cavalier 	case IEEE80211_FC0_TYPE_DATA:
1606753c7e08SAugustin Cavalier 		if (ni == vap->iv_bss)
1607753c7e08SAugustin Cavalier 			goto out;
1608753c7e08SAugustin Cavalier 		if (ni->ni_mlstate != IEEE80211_NODE_MESH_ESTABLISHED) {
1609753c7e08SAugustin Cavalier 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_MESH,
1610753c7e08SAugustin Cavalier 			    ni->ni_macaddr, NULL,
1611753c7e08SAugustin Cavalier 			    "peer link not yet established (%d)",
1612753c7e08SAugustin Cavalier 			    ni->ni_mlstate);
1613753c7e08SAugustin Cavalier 			vap->iv_stats.is_mesh_nolink++;
1614753c7e08SAugustin Cavalier 			goto out;
1615753c7e08SAugustin Cavalier 		}
1616753c7e08SAugustin Cavalier 		if (dir != IEEE80211_FC1_DIR_FROMDS &&
1617753c7e08SAugustin Cavalier 		    dir != IEEE80211_FC1_DIR_DSTODS) {
1618753c7e08SAugustin Cavalier 			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
1619753c7e08SAugustin Cavalier 			    wh, "data", "incorrect dir 0x%x", dir);
1620753c7e08SAugustin Cavalier 			vap->iv_stats.is_rx_wrongdir++;
1621753c7e08SAugustin Cavalier 			goto err;
1622753c7e08SAugustin Cavalier 		}
1623753c7e08SAugustin Cavalier 
1624753c7e08SAugustin Cavalier 		/* All Mesh data frames are QoS subtype */
1625753c7e08SAugustin Cavalier 		if (!HAS_SEQ(type)) {
1626753c7e08SAugustin Cavalier 			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
1627753c7e08SAugustin Cavalier 			    wh, "data", "incorrect subtype 0x%x", subtype);
1628753c7e08SAugustin Cavalier 			vap->iv_stats.is_rx_badsubtype++;
1629753c7e08SAugustin Cavalier 			goto err;
1630753c7e08SAugustin Cavalier 		}
1631753c7e08SAugustin Cavalier 
1632753c7e08SAugustin Cavalier 		/*
1633753c7e08SAugustin Cavalier 		 * Next up, any fragmentation.
1634753c7e08SAugustin Cavalier 		 * XXX: we defrag before we even try to forward,
1635753c7e08SAugustin Cavalier 		 * Mesh Control field is not present in sub-sequent
1636753c7e08SAugustin Cavalier 		 * fragmented frames. This is in contrast to Draft 4.0.
1637753c7e08SAugustin Cavalier 		 */
1638753c7e08SAugustin Cavalier 		hdrspace = ieee80211_hdrspace(ic, wh);
1639753c7e08SAugustin Cavalier 		if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
164086021fd4SAugustin Cavalier 			m = ieee80211_defrag(ni, m, hdrspace, 0);
1641753c7e08SAugustin Cavalier 			if (m == NULL) {
1642753c7e08SAugustin Cavalier 				/* Fragment dropped or frame not complete yet */
1643753c7e08SAugustin Cavalier 				goto out;
1644753c7e08SAugustin Cavalier 			}
1645753c7e08SAugustin Cavalier 		}
1646753c7e08SAugustin Cavalier 		wh = mtod(m, struct ieee80211_frame *); /* NB: after defrag */
1647753c7e08SAugustin Cavalier 
1648753c7e08SAugustin Cavalier 		/*
1649753c7e08SAugustin Cavalier 		 * Now we have a complete Mesh Data frame.
1650753c7e08SAugustin Cavalier 		 */
1651753c7e08SAugustin Cavalier 
1652753c7e08SAugustin Cavalier 		/*
1653753c7e08SAugustin Cavalier 		 * Only fromDStoDS data frames use 4 address qos frames
1654753c7e08SAugustin Cavalier 		 * as specified in amendment. Otherwise addr4 is located
1655753c7e08SAugustin Cavalier 		 * in the Mesh Control field and a 3 address qos frame
1656753c7e08SAugustin Cavalier 		 * is used.
1657753c7e08SAugustin Cavalier 		 */
16585cad57c4SJérôme Duval 		*(uint16_t *)qos = *(uint16_t *)ieee80211_getqos(wh);
1659753c7e08SAugustin Cavalier 
1660753c7e08SAugustin Cavalier 		/*
1661753c7e08SAugustin Cavalier 		 * NB: The mesh STA sets the Mesh Control Present
1662753c7e08SAugustin Cavalier 		 * subfield to 1 in the Mesh Data frame containing
1663753c7e08SAugustin Cavalier 		 * an unfragmented MSDU, an A-MSDU, or the first
1664753c7e08SAugustin Cavalier 		 * fragment of an MSDU.
1665753c7e08SAugustin Cavalier 		 * After defrag it should always be present.
1666753c7e08SAugustin Cavalier 		 */
1667753c7e08SAugustin Cavalier 		if (!(qos[1] & IEEE80211_QOS_MC)) {
1668753c7e08SAugustin Cavalier 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_MESH,
1669753c7e08SAugustin Cavalier 			    ni->ni_macaddr, NULL,
1670753c7e08SAugustin Cavalier 			    "%s", "Mesh control field not present");
1671753c7e08SAugustin Cavalier 			vap->iv_stats.is_rx_elem_missing++; /* XXX: kinda */
1672753c7e08SAugustin Cavalier 			goto err;
1673753c7e08SAugustin Cavalier 		}
1674753c7e08SAugustin Cavalier 
1675753c7e08SAugustin Cavalier 		/* pull up enough to get to the mesh control */
1676753c7e08SAugustin Cavalier 		if (m->m_len < hdrspace + sizeof(struct ieee80211_meshcntl) &&
1677753c7e08SAugustin Cavalier 		    (m = m_pullup(m, hdrspace +
1678753c7e08SAugustin Cavalier 		        sizeof(struct ieee80211_meshcntl))) == NULL) {
1679753c7e08SAugustin Cavalier 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
1680753c7e08SAugustin Cavalier 			    ni->ni_macaddr, NULL,
1681753c7e08SAugustin Cavalier 			    "data too short: expecting %u", hdrspace);
1682753c7e08SAugustin Cavalier 			vap->iv_stats.is_rx_tooshort++;
1683753c7e08SAugustin Cavalier 			goto out;		/* XXX */
1684753c7e08SAugustin Cavalier 		}
1685753c7e08SAugustin Cavalier 		/*
1686753c7e08SAugustin Cavalier 		 * Now calculate the full extent of the headers. Note
1687753c7e08SAugustin Cavalier 		 * mesh_decap will pull up anything we didn't get
1688753c7e08SAugustin Cavalier 		 * above when it strips the 802.11 headers.
1689753c7e08SAugustin Cavalier 		 */
1690753c7e08SAugustin Cavalier 		mc = (const struct ieee80211_meshcntl *)
1691753c7e08SAugustin Cavalier 		    (mtod(m, const uint8_t *) + hdrspace);
1692753c7e08SAugustin Cavalier 		ae = mc->mc_flags & IEEE80211_MESH_AE_MASK;
1693753c7e08SAugustin Cavalier 		meshdrlen = sizeof(struct ieee80211_meshcntl) +
1694753c7e08SAugustin Cavalier 		    ae * IEEE80211_ADDR_LEN;
1695753c7e08SAugustin Cavalier 		hdrspace += meshdrlen;
1696753c7e08SAugustin Cavalier 
1697753c7e08SAugustin Cavalier 		/* pull complete hdrspace = ieee80211_hdrspace + meshcontrol */
1698753c7e08SAugustin Cavalier 		if ((meshdrlen > sizeof(struct ieee80211_meshcntl)) &&
1699753c7e08SAugustin Cavalier 		    (m->m_len < hdrspace) &&
1700753c7e08SAugustin Cavalier 		    ((m = m_pullup(m, hdrspace)) == NULL)) {
1701753c7e08SAugustin Cavalier 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
1702753c7e08SAugustin Cavalier 			    ni->ni_macaddr, NULL,
1703753c7e08SAugustin Cavalier 			    "data too short: expecting %u", hdrspace);
1704753c7e08SAugustin Cavalier 			vap->iv_stats.is_rx_tooshort++;
1705753c7e08SAugustin Cavalier 			goto out;		/* XXX */
1706753c7e08SAugustin Cavalier 		}
1707753c7e08SAugustin Cavalier 		/* XXX: are we sure there is no reallocating after m_pullup? */
1708753c7e08SAugustin Cavalier 
1709753c7e08SAugustin Cavalier 		seq = le32dec(mc->mc_seq);
1710753c7e08SAugustin Cavalier 		if (IEEE80211_IS_MULTICAST(wh->i_addr1))
1711753c7e08SAugustin Cavalier 			addr = wh->i_addr3;
1712753c7e08SAugustin Cavalier 		else if (ae == IEEE80211_MESH_AE_01)
1713753c7e08SAugustin Cavalier 			addr = MC01(mc)->mc_addr4;
1714753c7e08SAugustin Cavalier 		else
1715753c7e08SAugustin Cavalier 			addr = ((struct ieee80211_qosframe_addr4 *)wh)->i_addr4;
1716753c7e08SAugustin Cavalier 		if (IEEE80211_ADDR_EQ(vap->iv_myaddr, addr)) {
1717753c7e08SAugustin Cavalier 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
1718753c7e08SAugustin Cavalier 			    addr, "data", "%s", "not to me");
1719753c7e08SAugustin Cavalier 			vap->iv_stats.is_rx_wrongbss++;	/* XXX kinda */
1720753c7e08SAugustin Cavalier 			goto out;
1721753c7e08SAugustin Cavalier 		}
1722753c7e08SAugustin Cavalier 		if (mesh_checkpseq(vap, addr, seq) != 0) {
1723753c7e08SAugustin Cavalier 			vap->iv_stats.is_rx_dup++;
1724753c7e08SAugustin Cavalier 			goto out;
1725753c7e08SAugustin Cavalier 		}
1726753c7e08SAugustin Cavalier 
1727753c7e08SAugustin Cavalier 		/* This code "routes" the frame to the right control path */
1728753c7e08SAugustin Cavalier 		if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1729753c7e08SAugustin Cavalier 			if (IEEE80211_ADDR_EQ(vap->iv_myaddr, wh->i_addr3))
1730753c7e08SAugustin Cavalier 				error =
1731753c7e08SAugustin Cavalier 				    mesh_recv_indiv_data_to_me(vap, m, wh, mc);
1732753c7e08SAugustin Cavalier 			else if (IEEE80211_IS_MULTICAST(wh->i_addr3))
1733753c7e08SAugustin Cavalier 				error = mesh_recv_group_data(vap, m, wh, mc);
1734753c7e08SAugustin Cavalier 			else
1735753c7e08SAugustin Cavalier 				error = mesh_recv_indiv_data_to_fwrd(vap, m,
1736753c7e08SAugustin Cavalier 				    wh, mc);
1737753c7e08SAugustin Cavalier 		} else
1738753c7e08SAugustin Cavalier 			error = mesh_recv_group_data(vap, m, wh, mc);
1739753c7e08SAugustin Cavalier 		if (error < 0)
1740753c7e08SAugustin Cavalier 			goto err;
1741753c7e08SAugustin Cavalier 		else if (error > 0)
1742753c7e08SAugustin Cavalier 			goto out;
1743753c7e08SAugustin Cavalier 
1744753c7e08SAugustin Cavalier 		if (ieee80211_radiotap_active_vap(vap))
1745753c7e08SAugustin Cavalier 			ieee80211_radiotap_rx(vap, m);
1746753c7e08SAugustin Cavalier 		need_tap = 0;
1747753c7e08SAugustin Cavalier 
1748753c7e08SAugustin Cavalier 		/*
1749753c7e08SAugustin Cavalier 		 * Finally, strip the 802.11 header.
1750753c7e08SAugustin Cavalier 		 */
1751753c7e08SAugustin Cavalier 		m = mesh_decap(vap, m, hdrspace, meshdrlen);
1752753c7e08SAugustin Cavalier 		if (m == NULL) {
1753753c7e08SAugustin Cavalier 			/* XXX mask bit to check for both */
1754753c7e08SAugustin Cavalier 			/* don't count Null data frames as errors */
1755753c7e08SAugustin Cavalier 			if (subtype == IEEE80211_FC0_SUBTYPE_NODATA ||
1756753c7e08SAugustin Cavalier 			    subtype == IEEE80211_FC0_SUBTYPE_QOS_NULL)
1757753c7e08SAugustin Cavalier 				goto out;
1758753c7e08SAugustin Cavalier 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
1759753c7e08SAugustin Cavalier 			    ni->ni_macaddr, "data", "%s", "decap error");
1760753c7e08SAugustin Cavalier 			vap->iv_stats.is_rx_decap++;
1761753c7e08SAugustin Cavalier 			IEEE80211_NODE_STAT(ni, rx_decap);
1762753c7e08SAugustin Cavalier 			goto err;
1763753c7e08SAugustin Cavalier 		}
1764753c7e08SAugustin Cavalier 		if (qos[0] & IEEE80211_QOS_AMSDU) {
1765753c7e08SAugustin Cavalier 			m = ieee80211_decap_amsdu(ni, m);
1766753c7e08SAugustin Cavalier 			if (m == NULL)
1767753c7e08SAugustin Cavalier 				return IEEE80211_FC0_TYPE_DATA;
1768753c7e08SAugustin Cavalier 		}
1769753c7e08SAugustin Cavalier 		ieee80211_deliver_data(vap, ni, m);
1770753c7e08SAugustin Cavalier 		return type;
1771753c7e08SAugustin Cavalier 	case IEEE80211_FC0_TYPE_MGT:
1772753c7e08SAugustin Cavalier 		vap->iv_stats.is_rx_mgmt++;
1773753c7e08SAugustin Cavalier 		IEEE80211_NODE_STAT(ni, rx_mgmt);
1774753c7e08SAugustin Cavalier 		if (dir != IEEE80211_FC1_DIR_NODS) {
1775753c7e08SAugustin Cavalier 			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
1776753c7e08SAugustin Cavalier 			    wh, "mgt", "incorrect dir 0x%x", dir);
1777753c7e08SAugustin Cavalier 			vap->iv_stats.is_rx_wrongdir++;
1778753c7e08SAugustin Cavalier 			goto err;
1779753c7e08SAugustin Cavalier 		}
1780753c7e08SAugustin Cavalier 		if (m->m_pkthdr.len < sizeof(struct ieee80211_frame)) {
1781753c7e08SAugustin Cavalier 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
1782753c7e08SAugustin Cavalier 			    ni->ni_macaddr, "mgt", "too short: len %u",
1783753c7e08SAugustin Cavalier 			    m->m_pkthdr.len);
1784753c7e08SAugustin Cavalier 			vap->iv_stats.is_rx_tooshort++;
1785753c7e08SAugustin Cavalier 			goto out;
1786753c7e08SAugustin Cavalier 		}
1787753c7e08SAugustin Cavalier #ifdef IEEE80211_DEBUG
1788753c7e08SAugustin Cavalier 		if ((ieee80211_msg_debug(vap) &&
1789753c7e08SAugustin Cavalier 		    (vap->iv_ic->ic_flags & IEEE80211_F_SCAN)) ||
1790753c7e08SAugustin Cavalier 		    ieee80211_msg_dumppkts(vap)) {
1791753c7e08SAugustin Cavalier 			if_printf(ifp, "received %s from %s rssi %d\n",
1792753c7e08SAugustin Cavalier 			    ieee80211_mgt_subtype_name(subtype),
1793753c7e08SAugustin Cavalier 			    ether_sprintf(wh->i_addr2), rssi);
1794753c7e08SAugustin Cavalier 		}
1795753c7e08SAugustin Cavalier #endif
179686021fd4SAugustin Cavalier 		if (IEEE80211_IS_PROTECTED(wh)) {
1797753c7e08SAugustin Cavalier 			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
1798753c7e08SAugustin Cavalier 			    wh, NULL, "%s", "WEP set but not permitted");
1799753c7e08SAugustin Cavalier 			vap->iv_stats.is_rx_mgtdiscard++; /* XXX */
1800753c7e08SAugustin Cavalier 			goto out;
1801753c7e08SAugustin Cavalier 		}
1802753c7e08SAugustin Cavalier 		vap->iv_recv_mgmt(ni, m, subtype, rxs, rssi, nf);
1803753c7e08SAugustin Cavalier 		goto out;
1804753c7e08SAugustin Cavalier 	case IEEE80211_FC0_TYPE_CTL:
1805753c7e08SAugustin Cavalier 		vap->iv_stats.is_rx_ctl++;
1806753c7e08SAugustin Cavalier 		IEEE80211_NODE_STAT(ni, rx_ctrl);
1807753c7e08SAugustin Cavalier 		goto out;
1808753c7e08SAugustin Cavalier 	default:
1809753c7e08SAugustin Cavalier 		IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
1810753c7e08SAugustin Cavalier 		    wh, "bad", "frame type 0x%x", type);
1811753c7e08SAugustin Cavalier 		/* should not come here */
1812753c7e08SAugustin Cavalier 		break;
1813753c7e08SAugustin Cavalier 	}
1814753c7e08SAugustin Cavalier err:
1815753c7e08SAugustin Cavalier 	if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
1816753c7e08SAugustin Cavalier out:
1817753c7e08SAugustin Cavalier 	if (m != NULL) {
1818753c7e08SAugustin Cavalier 		if (need_tap && ieee80211_radiotap_active_vap(vap))
1819753c7e08SAugustin Cavalier 			ieee80211_radiotap_rx(vap, m);
1820753c7e08SAugustin Cavalier 		m_freem(m);
1821753c7e08SAugustin Cavalier 	}
1822753c7e08SAugustin Cavalier 	return type;
1823753c7e08SAugustin Cavalier #undef	HAS_SEQ
1824753c7e08SAugustin Cavalier #undef	MC01
1825753c7e08SAugustin Cavalier }
1826753c7e08SAugustin Cavalier 
1827753c7e08SAugustin Cavalier static void
mesh_recv_mgmt(struct ieee80211_node * ni,struct mbuf * m0,int subtype,const struct ieee80211_rx_stats * rxs,int rssi,int nf)1828753c7e08SAugustin Cavalier mesh_recv_mgmt(struct ieee80211_node *ni, struct mbuf *m0, int subtype,
1829753c7e08SAugustin Cavalier     const struct ieee80211_rx_stats *rxs, int rssi, int nf)
1830753c7e08SAugustin Cavalier {
1831753c7e08SAugustin Cavalier 	struct ieee80211vap *vap = ni->ni_vap;
1832753c7e08SAugustin Cavalier 	struct ieee80211_mesh_state *ms = vap->iv_mesh;
1833753c7e08SAugustin Cavalier 	struct ieee80211com *ic = ni->ni_ic;
1834753c7e08SAugustin Cavalier 	struct ieee80211_channel *rxchan = ic->ic_curchan;
1835753c7e08SAugustin Cavalier 	struct ieee80211_frame *wh;
1836753c7e08SAugustin Cavalier 	struct ieee80211_mesh_route *rt;
1837753c7e08SAugustin Cavalier 	uint8_t *frm, *efrm;
1838753c7e08SAugustin Cavalier 
1839753c7e08SAugustin Cavalier 	wh = mtod(m0, struct ieee80211_frame *);
1840753c7e08SAugustin Cavalier 	frm = (uint8_t *)&wh[1];
1841753c7e08SAugustin Cavalier 	efrm = mtod(m0, uint8_t *) + m0->m_len;
1842753c7e08SAugustin Cavalier 	switch (subtype) {
1843753c7e08SAugustin Cavalier 	case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
1844753c7e08SAugustin Cavalier 	case IEEE80211_FC0_SUBTYPE_BEACON:
1845753c7e08SAugustin Cavalier 	{
1846753c7e08SAugustin Cavalier 		struct ieee80211_scanparams scan;
1847753c7e08SAugustin Cavalier 		struct ieee80211_channel *c;
1848753c7e08SAugustin Cavalier 		/*
1849753c7e08SAugustin Cavalier 		 * We process beacon/probe response
1850753c7e08SAugustin Cavalier 		 * frames to discover neighbors.
1851753c7e08SAugustin Cavalier 		 */
1852753c7e08SAugustin Cavalier 		if (rxs != NULL) {
1853753c7e08SAugustin Cavalier 			c = ieee80211_lookup_channel_rxstatus(vap, rxs);
1854753c7e08SAugustin Cavalier 			if (c != NULL)
1855753c7e08SAugustin Cavalier 				rxchan = c;
1856753c7e08SAugustin Cavalier 		}
1857753c7e08SAugustin Cavalier 		if (ieee80211_parse_beacon(ni, m0, rxchan, &scan) != 0)
1858753c7e08SAugustin Cavalier 			return;
1859753c7e08SAugustin Cavalier 		/*
1860753c7e08SAugustin Cavalier 		 * Count frame now that we know it's to be processed.
1861753c7e08SAugustin Cavalier 		 */
1862753c7e08SAugustin Cavalier 		if (subtype == IEEE80211_FC0_SUBTYPE_BEACON) {
1863753c7e08SAugustin Cavalier 			vap->iv_stats.is_rx_beacon++;	/* XXX remove */
1864753c7e08SAugustin Cavalier 			IEEE80211_NODE_STAT(ni, rx_beacons);
1865753c7e08SAugustin Cavalier 		} else
1866753c7e08SAugustin Cavalier 			IEEE80211_NODE_STAT(ni, rx_proberesp);
1867753c7e08SAugustin Cavalier 		/*
1868753c7e08SAugustin Cavalier 		 * If scanning, just pass information to the scan module.
1869753c7e08SAugustin Cavalier 		 */
1870753c7e08SAugustin Cavalier 		if (ic->ic_flags & IEEE80211_F_SCAN) {
1871753c7e08SAugustin Cavalier 			if (ic->ic_flags_ext & IEEE80211_FEXT_PROBECHAN) {
1872753c7e08SAugustin Cavalier 				/*
1873753c7e08SAugustin Cavalier 				 * Actively scanning a channel marked passive;
1874753c7e08SAugustin Cavalier 				 * send a probe request now that we know there
1875753c7e08SAugustin Cavalier 				 * is 802.11 traffic present.
1876753c7e08SAugustin Cavalier 				 *
1877753c7e08SAugustin Cavalier 				 * XXX check if the beacon we recv'd gives
1878753c7e08SAugustin Cavalier 				 * us what we need and suppress the probe req
1879753c7e08SAugustin Cavalier 				 */
1880*da3ca318SAugustin Cavalier 				ieee80211_probe_curchan(vap, true);
1881753c7e08SAugustin Cavalier 				ic->ic_flags_ext &= ~IEEE80211_FEXT_PROBECHAN;
1882753c7e08SAugustin Cavalier 			}
1883753c7e08SAugustin Cavalier 			ieee80211_add_scan(vap, rxchan, &scan, wh,
1884753c7e08SAugustin Cavalier 			    subtype, rssi, nf);
1885753c7e08SAugustin Cavalier 			return;
1886753c7e08SAugustin Cavalier 		}
1887753c7e08SAugustin Cavalier 
1888753c7e08SAugustin Cavalier 		/* The rest of this code assumes we are running */
1889753c7e08SAugustin Cavalier 		if (vap->iv_state != IEEE80211_S_RUN)
1890753c7e08SAugustin Cavalier 			return;
1891753c7e08SAugustin Cavalier 		/*
1892753c7e08SAugustin Cavalier 		 * Ignore non-mesh STAs.
1893753c7e08SAugustin Cavalier 		 */
1894753c7e08SAugustin Cavalier 		if ((scan.capinfo &
1895753c7e08SAugustin Cavalier 		     (IEEE80211_CAPINFO_ESS|IEEE80211_CAPINFO_IBSS)) ||
1896753c7e08SAugustin Cavalier 		    scan.meshid == NULL || scan.meshconf == NULL) {
1897753c7e08SAugustin Cavalier 			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
1898753c7e08SAugustin Cavalier 			    wh, "beacon", "%s", "not a mesh sta");
1899753c7e08SAugustin Cavalier 			vap->iv_stats.is_mesh_wrongmesh++;
1900753c7e08SAugustin Cavalier 			return;
1901753c7e08SAugustin Cavalier 		}
1902753c7e08SAugustin Cavalier 		/*
1903753c7e08SAugustin Cavalier 		 * Ignore STAs for other mesh networks.
1904753c7e08SAugustin Cavalier 		 */
1905753c7e08SAugustin Cavalier 		if (memcmp(scan.meshid+2, ms->ms_id, ms->ms_idlen) != 0 ||
1906753c7e08SAugustin Cavalier 		    mesh_verify_meshconf(vap, scan.meshconf)) {
1907753c7e08SAugustin Cavalier 			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
1908753c7e08SAugustin Cavalier 			    wh, "beacon", "%s", "not for our mesh");
1909753c7e08SAugustin Cavalier 			vap->iv_stats.is_mesh_wrongmesh++;
1910753c7e08SAugustin Cavalier 			return;
1911753c7e08SAugustin Cavalier 		}
1912753c7e08SAugustin Cavalier 		/*
1913753c7e08SAugustin Cavalier 		 * Peer only based on the current ACL policy.
1914753c7e08SAugustin Cavalier 		 */
1915753c7e08SAugustin Cavalier 		if (vap->iv_acl != NULL && !vap->iv_acl->iac_check(vap, wh)) {
1916753c7e08SAugustin Cavalier 			IEEE80211_DISCARD(vap, IEEE80211_MSG_ACL,
1917753c7e08SAugustin Cavalier 			    wh, NULL, "%s", "disallowed by ACL");
1918753c7e08SAugustin Cavalier 			vap->iv_stats.is_rx_acl++;
1919753c7e08SAugustin Cavalier 			return;
1920753c7e08SAugustin Cavalier 		}
1921753c7e08SAugustin Cavalier 		/*
1922753c7e08SAugustin Cavalier 		 * Do neighbor discovery.
1923753c7e08SAugustin Cavalier 		 */
1924753c7e08SAugustin Cavalier 		if (!IEEE80211_ADDR_EQ(wh->i_addr2, ni->ni_macaddr)) {
1925753c7e08SAugustin Cavalier 			/*
1926753c7e08SAugustin Cavalier 			 * Create a new entry in the neighbor table.
1927753c7e08SAugustin Cavalier 			 */
1928753c7e08SAugustin Cavalier 			ni = ieee80211_add_neighbor(vap, wh, &scan);
1929753c7e08SAugustin Cavalier 		}
1930753c7e08SAugustin Cavalier 		/*
1931753c7e08SAugustin Cavalier 		 * Automatically peer with discovered nodes if possible.
1932753c7e08SAugustin Cavalier 		 */
1933753c7e08SAugustin Cavalier 		if (ni != vap->iv_bss &&
1934753c7e08SAugustin Cavalier 		    (ms->ms_flags & IEEE80211_MESHFLAGS_AP)) {
1935753c7e08SAugustin Cavalier 			switch (ni->ni_mlstate) {
1936753c7e08SAugustin Cavalier 			case IEEE80211_NODE_MESH_IDLE:
1937753c7e08SAugustin Cavalier 			{
1938753c7e08SAugustin Cavalier 				uint16_t args[1];
1939753c7e08SAugustin Cavalier 
1940753c7e08SAugustin Cavalier 				/* Wait for backoff callout to reset counter */
1941753c7e08SAugustin Cavalier 				if (ni->ni_mlhcnt >= ieee80211_mesh_maxholding)
1942753c7e08SAugustin Cavalier 					return;
1943753c7e08SAugustin Cavalier 
1944753c7e08SAugustin Cavalier 				ni->ni_mlpid = mesh_generateid(vap);
1945753c7e08SAugustin Cavalier 				if (ni->ni_mlpid == 0)
1946753c7e08SAugustin Cavalier 					return;
1947753c7e08SAugustin Cavalier 				mesh_linkchange(ni, IEEE80211_NODE_MESH_OPENSNT);
1948753c7e08SAugustin Cavalier 				args[0] = ni->ni_mlpid;
1949753c7e08SAugustin Cavalier 				ieee80211_send_action(ni,
1950753c7e08SAugustin Cavalier 				IEEE80211_ACTION_CAT_SELF_PROT,
1951753c7e08SAugustin Cavalier 				IEEE80211_ACTION_MESHPEERING_OPEN, args);
1952753c7e08SAugustin Cavalier 				ni->ni_mlrcnt = 0;
1953753c7e08SAugustin Cavalier 				mesh_peer_timeout_setup(ni);
1954753c7e08SAugustin Cavalier 				break;
1955753c7e08SAugustin Cavalier 			}
1956753c7e08SAugustin Cavalier 			case IEEE80211_NODE_MESH_ESTABLISHED:
1957753c7e08SAugustin Cavalier 			{
1958753c7e08SAugustin Cavalier 				/*
1959753c7e08SAugustin Cavalier 				 * Valid beacon from a peer mesh STA
1960753c7e08SAugustin Cavalier 				 * bump TA lifetime
1961753c7e08SAugustin Cavalier 				 */
1962753c7e08SAugustin Cavalier 				rt = ieee80211_mesh_rt_find(vap, wh->i_addr2);
1963753c7e08SAugustin Cavalier 				if(rt != NULL) {
1964753c7e08SAugustin Cavalier 					ieee80211_mesh_rt_update(rt,
1965753c7e08SAugustin Cavalier 					    ticks_to_msecs(
1966753c7e08SAugustin Cavalier 					    ms->ms_ppath->mpp_inact));
1967753c7e08SAugustin Cavalier 				}
1968753c7e08SAugustin Cavalier 				break;
1969753c7e08SAugustin Cavalier 			}
1970753c7e08SAugustin Cavalier 			default:
1971753c7e08SAugustin Cavalier 				break; /* ignore */
1972753c7e08SAugustin Cavalier 			}
1973753c7e08SAugustin Cavalier 		}
1974753c7e08SAugustin Cavalier 		break;
1975753c7e08SAugustin Cavalier 	}
1976753c7e08SAugustin Cavalier 	case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
1977753c7e08SAugustin Cavalier 	{
1978753c7e08SAugustin Cavalier 		uint8_t *ssid, *meshid, *rates, *xrates;
1979753c7e08SAugustin Cavalier 
1980753c7e08SAugustin Cavalier 		if (vap->iv_state != IEEE80211_S_RUN) {
1981753c7e08SAugustin Cavalier 			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
1982753c7e08SAugustin Cavalier 			    wh, NULL, "wrong state %s",
1983753c7e08SAugustin Cavalier 			    ieee80211_state_name[vap->iv_state]);
1984753c7e08SAugustin Cavalier 			vap->iv_stats.is_rx_mgtdiscard++;
1985753c7e08SAugustin Cavalier 			return;
1986753c7e08SAugustin Cavalier 		}
1987753c7e08SAugustin Cavalier 		if (IEEE80211_IS_MULTICAST(wh->i_addr2)) {
1988753c7e08SAugustin Cavalier 			/* frame must be directed */
1989753c7e08SAugustin Cavalier 			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
1990753c7e08SAugustin Cavalier 			    wh, NULL, "%s", "not unicast");
1991753c7e08SAugustin Cavalier 			vap->iv_stats.is_rx_mgtdiscard++;	/* XXX stat */
1992753c7e08SAugustin Cavalier 			return;
1993753c7e08SAugustin Cavalier 		}
1994753c7e08SAugustin Cavalier 		/*
1995753c7e08SAugustin Cavalier 		 * prreq frame format
1996753c7e08SAugustin Cavalier 		 *      [tlv] ssid
1997753c7e08SAugustin Cavalier 		 *      [tlv] supported rates
1998753c7e08SAugustin Cavalier 		 *      [tlv] extended supported rates
1999753c7e08SAugustin Cavalier 		 *	[tlv] mesh id
2000753c7e08SAugustin Cavalier 		 */
2001753c7e08SAugustin Cavalier 		ssid = meshid = rates = xrates = NULL;
2002753c7e08SAugustin Cavalier 		while (efrm - frm > 1) {
2003753c7e08SAugustin Cavalier 			IEEE80211_VERIFY_LENGTH(efrm - frm, frm[1] + 2, return);
2004753c7e08SAugustin Cavalier 			switch (*frm) {
2005753c7e08SAugustin Cavalier 			case IEEE80211_ELEMID_SSID:
2006753c7e08SAugustin Cavalier 				ssid = frm;
2007753c7e08SAugustin Cavalier 				break;
2008753c7e08SAugustin Cavalier 			case IEEE80211_ELEMID_RATES:
2009753c7e08SAugustin Cavalier 				rates = frm;
2010753c7e08SAugustin Cavalier 				break;
2011753c7e08SAugustin Cavalier 			case IEEE80211_ELEMID_XRATES:
2012753c7e08SAugustin Cavalier 				xrates = frm;
2013753c7e08SAugustin Cavalier 				break;
2014753c7e08SAugustin Cavalier 			case IEEE80211_ELEMID_MESHID:
2015753c7e08SAugustin Cavalier 				meshid = frm;
2016753c7e08SAugustin Cavalier 				break;
2017753c7e08SAugustin Cavalier 			}
2018753c7e08SAugustin Cavalier 			frm += frm[1] + 2;
2019753c7e08SAugustin Cavalier 		}
2020753c7e08SAugustin Cavalier 		IEEE80211_VERIFY_ELEMENT(ssid, IEEE80211_NWID_LEN, return);
2021753c7e08SAugustin Cavalier 		IEEE80211_VERIFY_ELEMENT(rates, IEEE80211_RATE_MAXSIZE, return);
2022753c7e08SAugustin Cavalier 		if (xrates != NULL)
2023753c7e08SAugustin Cavalier 			IEEE80211_VERIFY_ELEMENT(xrates,
2024753c7e08SAugustin Cavalier 			    IEEE80211_RATE_MAXSIZE - rates[1], return);
2025753c7e08SAugustin Cavalier 		if (meshid != NULL) {
2026753c7e08SAugustin Cavalier 			IEEE80211_VERIFY_ELEMENT(meshid,
2027753c7e08SAugustin Cavalier 			    IEEE80211_MESHID_LEN, return);
2028753c7e08SAugustin Cavalier 			/* NB: meshid, not ssid */
2029753c7e08SAugustin Cavalier 			IEEE80211_VERIFY_SSID(vap->iv_bss, meshid, return);
2030753c7e08SAugustin Cavalier 		}
2031753c7e08SAugustin Cavalier 
2032753c7e08SAugustin Cavalier 		/* XXX find a better class or define it's own */
2033753c7e08SAugustin Cavalier 		IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_INPUT, wh->i_addr2,
2034753c7e08SAugustin Cavalier 		    "%s", "recv probe req");
2035753c7e08SAugustin Cavalier 		/*
2036753c7e08SAugustin Cavalier 		 * Some legacy 11b clients cannot hack a complete
2037753c7e08SAugustin Cavalier 		 * probe response frame.  When the request includes
2038753c7e08SAugustin Cavalier 		 * only a bare-bones rate set, communicate this to
2039753c7e08SAugustin Cavalier 		 * the transmit side.
2040753c7e08SAugustin Cavalier 		 */
2041753c7e08SAugustin Cavalier 		ieee80211_send_proberesp(vap, wh->i_addr2, 0);
2042753c7e08SAugustin Cavalier 		break;
2043753c7e08SAugustin Cavalier 	}
2044753c7e08SAugustin Cavalier 
2045753c7e08SAugustin Cavalier 	case IEEE80211_FC0_SUBTYPE_ACTION:
2046753c7e08SAugustin Cavalier 	case IEEE80211_FC0_SUBTYPE_ACTION_NOACK:
2047753c7e08SAugustin Cavalier 		if (ni == vap->iv_bss) {
2048753c7e08SAugustin Cavalier 			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
2049753c7e08SAugustin Cavalier 			    wh, NULL, "%s", "unknown node");
2050753c7e08SAugustin Cavalier 			vap->iv_stats.is_rx_mgtdiscard++;
2051753c7e08SAugustin Cavalier 		} else if (!IEEE80211_ADDR_EQ(vap->iv_myaddr, wh->i_addr1) &&
2052753c7e08SAugustin Cavalier 		    !IEEE80211_IS_MULTICAST(wh->i_addr1)) {
2053753c7e08SAugustin Cavalier 			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
2054753c7e08SAugustin Cavalier 			    wh, NULL, "%s", "not for us");
2055753c7e08SAugustin Cavalier 			vap->iv_stats.is_rx_mgtdiscard++;
2056753c7e08SAugustin Cavalier 		} else if (vap->iv_state != IEEE80211_S_RUN) {
2057753c7e08SAugustin Cavalier 			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
2058753c7e08SAugustin Cavalier 			    wh, NULL, "wrong state %s",
2059753c7e08SAugustin Cavalier 			    ieee80211_state_name[vap->iv_state]);
2060753c7e08SAugustin Cavalier 			vap->iv_stats.is_rx_mgtdiscard++;
2061753c7e08SAugustin Cavalier 		} else {
2062753c7e08SAugustin Cavalier 			if (ieee80211_parse_action(ni, m0) == 0)
2063753c7e08SAugustin Cavalier 				(void)ic->ic_recv_action(ni, wh, frm, efrm);
2064753c7e08SAugustin Cavalier 		}
2065753c7e08SAugustin Cavalier 		break;
2066753c7e08SAugustin Cavalier 
2067753c7e08SAugustin Cavalier 	case IEEE80211_FC0_SUBTYPE_ASSOC_REQ:
2068753c7e08SAugustin Cavalier 	case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
2069753c7e08SAugustin Cavalier 	case IEEE80211_FC0_SUBTYPE_REASSOC_REQ:
2070753c7e08SAugustin Cavalier 	case IEEE80211_FC0_SUBTYPE_REASSOC_RESP:
2071753c7e08SAugustin Cavalier 	case IEEE80211_FC0_SUBTYPE_TIMING_ADV:
2072753c7e08SAugustin Cavalier 	case IEEE80211_FC0_SUBTYPE_ATIM:
2073753c7e08SAugustin Cavalier 	case IEEE80211_FC0_SUBTYPE_DISASSOC:
2074753c7e08SAugustin Cavalier 	case IEEE80211_FC0_SUBTYPE_AUTH:
2075753c7e08SAugustin Cavalier 	case IEEE80211_FC0_SUBTYPE_DEAUTH:
2076753c7e08SAugustin Cavalier 		IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
2077753c7e08SAugustin Cavalier 		    wh, NULL, "%s", "not handled");
2078753c7e08SAugustin Cavalier 		vap->iv_stats.is_rx_mgtdiscard++;
2079753c7e08SAugustin Cavalier 		break;
2080753c7e08SAugustin Cavalier 
2081753c7e08SAugustin Cavalier 	default:
2082753c7e08SAugustin Cavalier 		IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
2083753c7e08SAugustin Cavalier 		    wh, "mgt", "subtype 0x%x not handled", subtype);
2084753c7e08SAugustin Cavalier 		vap->iv_stats.is_rx_badsubtype++;
2085753c7e08SAugustin Cavalier 		break;
2086753c7e08SAugustin Cavalier 	}
2087753c7e08SAugustin Cavalier }
2088753c7e08SAugustin Cavalier 
2089753c7e08SAugustin Cavalier static void
mesh_recv_ctl(struct ieee80211_node * ni,struct mbuf * m,int subtype)2090753c7e08SAugustin Cavalier mesh_recv_ctl(struct ieee80211_node *ni, struct mbuf *m, int subtype)
2091753c7e08SAugustin Cavalier {
2092753c7e08SAugustin Cavalier 
2093753c7e08SAugustin Cavalier 	switch (subtype) {
2094753c7e08SAugustin Cavalier 	case IEEE80211_FC0_SUBTYPE_BAR:
2095753c7e08SAugustin Cavalier 		ieee80211_recv_bar(ni, m);
2096753c7e08SAugustin Cavalier 		break;
2097753c7e08SAugustin Cavalier 	}
2098753c7e08SAugustin Cavalier }
2099753c7e08SAugustin Cavalier 
2100753c7e08SAugustin Cavalier /*
2101753c7e08SAugustin Cavalier  * Parse meshpeering action ie's for MPM frames
2102753c7e08SAugustin Cavalier  */
2103753c7e08SAugustin Cavalier static const struct ieee80211_meshpeer_ie *
mesh_parse_meshpeering_action(struct ieee80211_node * ni,const struct ieee80211_frame * wh,const uint8_t * frm,const uint8_t * efrm,struct ieee80211_meshpeer_ie * mp,uint8_t subtype)2104753c7e08SAugustin Cavalier mesh_parse_meshpeering_action(struct ieee80211_node *ni,
2105753c7e08SAugustin Cavalier 	const struct ieee80211_frame *wh,	/* XXX for VERIFY_LENGTH */
2106753c7e08SAugustin Cavalier 	const uint8_t *frm, const uint8_t *efrm,
2107753c7e08SAugustin Cavalier 	struct ieee80211_meshpeer_ie *mp, uint8_t subtype)
2108753c7e08SAugustin Cavalier {
2109753c7e08SAugustin Cavalier 	struct ieee80211vap *vap = ni->ni_vap;
2110753c7e08SAugustin Cavalier 	const struct ieee80211_meshpeer_ie *mpie;
2111753c7e08SAugustin Cavalier 	uint16_t args[3];
2112753c7e08SAugustin Cavalier 	const uint8_t *meshid, *meshconf;
2113753c7e08SAugustin Cavalier 	uint8_t sendclose = 0; /* 1 = MPM frame rejected, close will be sent */
2114753c7e08SAugustin Cavalier 
2115753c7e08SAugustin Cavalier 	meshid = meshconf = NULL;
2116753c7e08SAugustin Cavalier 	while (efrm - frm > 1) {
2117753c7e08SAugustin Cavalier 		IEEE80211_VERIFY_LENGTH(efrm - frm, frm[1] + 2, return NULL);
2118753c7e08SAugustin Cavalier 		switch (*frm) {
2119753c7e08SAugustin Cavalier 		case IEEE80211_ELEMID_MESHID:
2120753c7e08SAugustin Cavalier 			meshid = frm;
2121753c7e08SAugustin Cavalier 			break;
2122753c7e08SAugustin Cavalier 		case IEEE80211_ELEMID_MESHCONF:
2123753c7e08SAugustin Cavalier 			meshconf = frm;
2124753c7e08SAugustin Cavalier 			break;
2125753c7e08SAugustin Cavalier 		case IEEE80211_ELEMID_MESHPEER:
2126753c7e08SAugustin Cavalier 			mpie = (const struct ieee80211_meshpeer_ie *) frm;
2127753c7e08SAugustin Cavalier 			memset(mp, 0, sizeof(*mp));
2128753c7e08SAugustin Cavalier 			mp->peer_len = mpie->peer_len;
2129753c7e08SAugustin Cavalier 			mp->peer_proto = le16dec(&mpie->peer_proto);
2130753c7e08SAugustin Cavalier 			mp->peer_llinkid = le16dec(&mpie->peer_llinkid);
2131753c7e08SAugustin Cavalier 			switch (subtype) {
2132753c7e08SAugustin Cavalier 			case IEEE80211_ACTION_MESHPEERING_CONFIRM:
2133753c7e08SAugustin Cavalier 				mp->peer_linkid =
2134753c7e08SAugustin Cavalier 				    le16dec(&mpie->peer_linkid);
2135753c7e08SAugustin Cavalier 				break;
2136753c7e08SAugustin Cavalier 			case IEEE80211_ACTION_MESHPEERING_CLOSE:
2137753c7e08SAugustin Cavalier 				/* NB: peer link ID is optional */
2138753c7e08SAugustin Cavalier 				if (mpie->peer_len ==
2139753c7e08SAugustin Cavalier 				    (IEEE80211_MPM_BASE_SZ + 2)) {
2140753c7e08SAugustin Cavalier 					mp->peer_linkid = 0;
2141753c7e08SAugustin Cavalier 					mp->peer_rcode =
2142753c7e08SAugustin Cavalier 					    le16dec(&mpie->peer_linkid);
2143753c7e08SAugustin Cavalier 				} else {
2144753c7e08SAugustin Cavalier 					mp->peer_linkid =
2145753c7e08SAugustin Cavalier 					    le16dec(&mpie->peer_linkid);
2146753c7e08SAugustin Cavalier 					mp->peer_rcode =
2147753c7e08SAugustin Cavalier 					    le16dec(&mpie->peer_rcode);
2148753c7e08SAugustin Cavalier 				}
2149753c7e08SAugustin Cavalier 				break;
2150753c7e08SAugustin Cavalier 			}
2151753c7e08SAugustin Cavalier 			break;
2152753c7e08SAugustin Cavalier 		}
2153753c7e08SAugustin Cavalier 		frm += frm[1] + 2;
2154753c7e08SAugustin Cavalier 	}
2155753c7e08SAugustin Cavalier 
2156753c7e08SAugustin Cavalier 	/*
2157753c7e08SAugustin Cavalier 	 * Verify the contents of the frame.
2158753c7e08SAugustin Cavalier 	 * If it fails validation, close the peer link.
2159753c7e08SAugustin Cavalier 	 */
2160753c7e08SAugustin Cavalier 	if (mesh_verify_meshpeer(vap, subtype, (const uint8_t *)mp)) {
2161753c7e08SAugustin Cavalier 		sendclose = 1;
2162753c7e08SAugustin Cavalier 		IEEE80211_DISCARD(vap,
2163753c7e08SAugustin Cavalier 		    IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH,
2164753c7e08SAugustin Cavalier 		    wh, NULL, "%s", "MPM validation failed");
2165753c7e08SAugustin Cavalier 	}
2166753c7e08SAugustin Cavalier 
2167753c7e08SAugustin Cavalier 	/* If meshid is not the same reject any frames type. */
2168753c7e08SAugustin Cavalier 	if (sendclose == 0 && mesh_verify_meshid(vap, meshid)) {
2169753c7e08SAugustin Cavalier 		sendclose = 1;
2170753c7e08SAugustin Cavalier 		IEEE80211_DISCARD(vap,
2171753c7e08SAugustin Cavalier 		    IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH,
2172753c7e08SAugustin Cavalier 		    wh, NULL, "%s", "not for our mesh");
2173753c7e08SAugustin Cavalier 		if (subtype == IEEE80211_ACTION_MESHPEERING_CLOSE) {
2174753c7e08SAugustin Cavalier 			/*
2175753c7e08SAugustin Cavalier 			 * Standard not clear about this, if we dont ignore
2176753c7e08SAugustin Cavalier 			 * there will be an endless loop between nodes sending
2177753c7e08SAugustin Cavalier 			 * CLOSE frames between each other with wrong meshid.
2178753c7e08SAugustin Cavalier 			 * Discard and timers will bring FSM to IDLE state.
2179753c7e08SAugustin Cavalier 			 */
2180753c7e08SAugustin Cavalier 			return NULL;
2181753c7e08SAugustin Cavalier 		}
2182753c7e08SAugustin Cavalier 	}
2183753c7e08SAugustin Cavalier 
2184753c7e08SAugustin Cavalier 	/*
2185753c7e08SAugustin Cavalier 	 * Close frames are accepted if meshid is the same.
2186753c7e08SAugustin Cavalier 	 * Verify the other two types.
2187753c7e08SAugustin Cavalier 	 */
2188753c7e08SAugustin Cavalier 	if (sendclose == 0 && subtype != IEEE80211_ACTION_MESHPEERING_CLOSE &&
2189753c7e08SAugustin Cavalier 	    mesh_verify_meshconf(vap, meshconf)) {
2190753c7e08SAugustin Cavalier 		sendclose = 1;
2191753c7e08SAugustin Cavalier 		IEEE80211_DISCARD(vap,
2192753c7e08SAugustin Cavalier 		    IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH,
219386021fd4SAugustin Cavalier 		    wh, NULL, "%s", "configuration mismatch");
2194753c7e08SAugustin Cavalier 	}
2195753c7e08SAugustin Cavalier 
2196753c7e08SAugustin Cavalier 	if (sendclose) {
2197753c7e08SAugustin Cavalier 		vap->iv_stats.is_rx_mgtdiscard++;
2198753c7e08SAugustin Cavalier 		switch (ni->ni_mlstate) {
2199753c7e08SAugustin Cavalier 		case IEEE80211_NODE_MESH_IDLE:
2200753c7e08SAugustin Cavalier 		case IEEE80211_NODE_MESH_ESTABLISHED:
2201753c7e08SAugustin Cavalier 		case IEEE80211_NODE_MESH_HOLDING:
2202753c7e08SAugustin Cavalier 			/* ignore */
2203753c7e08SAugustin Cavalier 			break;
2204753c7e08SAugustin Cavalier 		case IEEE80211_NODE_MESH_OPENSNT:
2205753c7e08SAugustin Cavalier 		case IEEE80211_NODE_MESH_OPENRCV:
2206753c7e08SAugustin Cavalier 		case IEEE80211_NODE_MESH_CONFIRMRCV:
2207753c7e08SAugustin Cavalier 			args[0] = ni->ni_mlpid;
2208753c7e08SAugustin Cavalier 			args[1] = ni->ni_mllid;
2209753c7e08SAugustin Cavalier 			/* Reason codes for rejection */
2210753c7e08SAugustin Cavalier 			switch (subtype) {
2211753c7e08SAugustin Cavalier 			case IEEE80211_ACTION_MESHPEERING_OPEN:
2212753c7e08SAugustin Cavalier 				args[2] = IEEE80211_REASON_MESH_CPVIOLATION;
2213753c7e08SAugustin Cavalier 				break;
2214753c7e08SAugustin Cavalier 			case IEEE80211_ACTION_MESHPEERING_CONFIRM:
2215753c7e08SAugustin Cavalier 				args[2] = IEEE80211_REASON_MESH_INCONS_PARAMS;
2216753c7e08SAugustin Cavalier 				break;
2217753c7e08SAugustin Cavalier 			}
2218753c7e08SAugustin Cavalier 			ieee80211_send_action(ni,
2219753c7e08SAugustin Cavalier 			    IEEE80211_ACTION_CAT_SELF_PROT,
2220753c7e08SAugustin Cavalier 			    IEEE80211_ACTION_MESHPEERING_CLOSE,
2221753c7e08SAugustin Cavalier 			    args);
2222753c7e08SAugustin Cavalier 			mesh_linkchange(ni, IEEE80211_NODE_MESH_HOLDING);
2223753c7e08SAugustin Cavalier 			mesh_peer_timeout_setup(ni);
2224753c7e08SAugustin Cavalier 			break;
2225753c7e08SAugustin Cavalier 		}
2226753c7e08SAugustin Cavalier 		return NULL;
2227753c7e08SAugustin Cavalier 	}
2228753c7e08SAugustin Cavalier 
2229753c7e08SAugustin Cavalier 	return (const struct ieee80211_meshpeer_ie *) mp;
2230753c7e08SAugustin Cavalier }
2231753c7e08SAugustin Cavalier 
2232753c7e08SAugustin Cavalier static int
mesh_recv_action_meshpeering_open(struct ieee80211_node * ni,const struct ieee80211_frame * wh,const uint8_t * frm,const uint8_t * efrm)2233753c7e08SAugustin Cavalier mesh_recv_action_meshpeering_open(struct ieee80211_node *ni,
2234753c7e08SAugustin Cavalier 	const struct ieee80211_frame *wh,
2235753c7e08SAugustin Cavalier 	const uint8_t *frm, const uint8_t *efrm)
2236753c7e08SAugustin Cavalier {
2237753c7e08SAugustin Cavalier 	struct ieee80211vap *vap = ni->ni_vap;
2238753c7e08SAugustin Cavalier 	struct ieee80211_mesh_state *ms = vap->iv_mesh;
2239753c7e08SAugustin Cavalier 	struct ieee80211_meshpeer_ie ie;
2240753c7e08SAugustin Cavalier 	const struct ieee80211_meshpeer_ie *meshpeer;
2241753c7e08SAugustin Cavalier 	uint16_t args[3];
2242753c7e08SAugustin Cavalier 
2243753c7e08SAugustin Cavalier 	/* +2+2 for action + code + capabilites */
2244753c7e08SAugustin Cavalier 	meshpeer = mesh_parse_meshpeering_action(ni, wh, frm+2+2, efrm, &ie,
2245753c7e08SAugustin Cavalier 	    IEEE80211_ACTION_MESHPEERING_OPEN);
2246753c7e08SAugustin Cavalier 	if (meshpeer == NULL) {
2247753c7e08SAugustin Cavalier 		return 0;
2248753c7e08SAugustin Cavalier 	}
2249753c7e08SAugustin Cavalier 
2250753c7e08SAugustin Cavalier 	/* XXX move up */
2251753c7e08SAugustin Cavalier 	IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH, ni,
2252753c7e08SAugustin Cavalier 	    "recv PEER OPEN, lid 0x%x", meshpeer->peer_llinkid);
2253753c7e08SAugustin Cavalier 
2254753c7e08SAugustin Cavalier 	switch (ni->ni_mlstate) {
2255753c7e08SAugustin Cavalier 	case IEEE80211_NODE_MESH_IDLE:
2256753c7e08SAugustin Cavalier 		/* Reject open request if reached our maximum neighbor count */
2257753c7e08SAugustin Cavalier 		if (ms->ms_neighbors >= IEEE80211_MESH_MAX_NEIGHBORS) {
2258753c7e08SAugustin Cavalier 			args[0] = meshpeer->peer_llinkid;
2259753c7e08SAugustin Cavalier 			args[1] = 0;
2260753c7e08SAugustin Cavalier 			args[2] = IEEE80211_REASON_MESH_MAX_PEERS;
2261753c7e08SAugustin Cavalier 			ieee80211_send_action(ni,
2262753c7e08SAugustin Cavalier 			    IEEE80211_ACTION_CAT_SELF_PROT,
2263753c7e08SAugustin Cavalier 			    IEEE80211_ACTION_MESHPEERING_CLOSE,
2264753c7e08SAugustin Cavalier 			    args);
2265753c7e08SAugustin Cavalier 			/* stay in IDLE state */
2266753c7e08SAugustin Cavalier 			return (0);
2267753c7e08SAugustin Cavalier 		}
2268753c7e08SAugustin Cavalier 		/* Open frame accepted */
2269753c7e08SAugustin Cavalier 		mesh_linkchange(ni, IEEE80211_NODE_MESH_OPENRCV);
2270753c7e08SAugustin Cavalier 		ni->ni_mllid = meshpeer->peer_llinkid;
2271753c7e08SAugustin Cavalier 		ni->ni_mlpid = mesh_generateid(vap);
2272753c7e08SAugustin Cavalier 		if (ni->ni_mlpid == 0)
2273753c7e08SAugustin Cavalier 			return 0;		/* XXX */
2274753c7e08SAugustin Cavalier 		args[0] = ni->ni_mlpid;
2275753c7e08SAugustin Cavalier 		/* Announce we're open too... */
2276753c7e08SAugustin Cavalier 		ieee80211_send_action(ni,
2277753c7e08SAugustin Cavalier 		    IEEE80211_ACTION_CAT_SELF_PROT,
2278753c7e08SAugustin Cavalier 		    IEEE80211_ACTION_MESHPEERING_OPEN, args);
2279753c7e08SAugustin Cavalier 		/* ...and confirm the link. */
2280753c7e08SAugustin Cavalier 		args[0] = ni->ni_mlpid;
2281753c7e08SAugustin Cavalier 		args[1] = ni->ni_mllid;
2282753c7e08SAugustin Cavalier 		ieee80211_send_action(ni,
2283753c7e08SAugustin Cavalier 		    IEEE80211_ACTION_CAT_SELF_PROT,
2284753c7e08SAugustin Cavalier 		    IEEE80211_ACTION_MESHPEERING_CONFIRM,
2285753c7e08SAugustin Cavalier 		    args);
2286753c7e08SAugustin Cavalier 		mesh_peer_timeout_setup(ni);
2287753c7e08SAugustin Cavalier 		break;
2288753c7e08SAugustin Cavalier 	case IEEE80211_NODE_MESH_OPENRCV:
2289753c7e08SAugustin Cavalier 		/* Wrong Link ID */
2290753c7e08SAugustin Cavalier 		if (ni->ni_mllid != meshpeer->peer_llinkid) {
2291753c7e08SAugustin Cavalier 			args[0] = ni->ni_mllid;
2292753c7e08SAugustin Cavalier 			args[1] = ni->ni_mlpid;
2293753c7e08SAugustin Cavalier 			args[2] = IEEE80211_REASON_PEER_LINK_CANCELED;
2294753c7e08SAugustin Cavalier 			ieee80211_send_action(ni,
2295753c7e08SAugustin Cavalier 			    IEEE80211_ACTION_CAT_SELF_PROT,
2296753c7e08SAugustin Cavalier 			    IEEE80211_ACTION_MESHPEERING_CLOSE,
2297753c7e08SAugustin Cavalier 			    args);
2298753c7e08SAugustin Cavalier 			mesh_linkchange(ni, IEEE80211_NODE_MESH_HOLDING);
2299753c7e08SAugustin Cavalier 			mesh_peer_timeout_setup(ni);
2300753c7e08SAugustin Cavalier 			break;
2301753c7e08SAugustin Cavalier 		}
2302753c7e08SAugustin Cavalier 		/* Duplicate open, confirm again. */
2303753c7e08SAugustin Cavalier 		args[0] = ni->ni_mlpid;
2304753c7e08SAugustin Cavalier 		args[1] = ni->ni_mllid;
2305753c7e08SAugustin Cavalier 		ieee80211_send_action(ni,
2306753c7e08SAugustin Cavalier 		    IEEE80211_ACTION_CAT_SELF_PROT,
2307753c7e08SAugustin Cavalier 		    IEEE80211_ACTION_MESHPEERING_CONFIRM,
2308753c7e08SAugustin Cavalier 		    args);
2309753c7e08SAugustin Cavalier 		break;
2310753c7e08SAugustin Cavalier 	case IEEE80211_NODE_MESH_OPENSNT:
2311753c7e08SAugustin Cavalier 		ni->ni_mllid = meshpeer->peer_llinkid;
2312753c7e08SAugustin Cavalier 		mesh_linkchange(ni, IEEE80211_NODE_MESH_OPENRCV);
2313753c7e08SAugustin Cavalier 		args[0] = ni->ni_mlpid;
2314753c7e08SAugustin Cavalier 		args[1] = ni->ni_mllid;
2315753c7e08SAugustin Cavalier 		ieee80211_send_action(ni,
2316753c7e08SAugustin Cavalier 		    IEEE80211_ACTION_CAT_SELF_PROT,
2317753c7e08SAugustin Cavalier 		    IEEE80211_ACTION_MESHPEERING_CONFIRM,
2318753c7e08SAugustin Cavalier 		    args);
2319753c7e08SAugustin Cavalier 		/* NB: don't setup/clear any timeout */
2320753c7e08SAugustin Cavalier 		break;
2321753c7e08SAugustin Cavalier 	case IEEE80211_NODE_MESH_CONFIRMRCV:
2322753c7e08SAugustin Cavalier 		if (ni->ni_mlpid != meshpeer->peer_linkid ||
2323753c7e08SAugustin Cavalier 		    ni->ni_mllid != meshpeer->peer_llinkid) {
2324753c7e08SAugustin Cavalier 			args[0] = ni->ni_mlpid;
2325753c7e08SAugustin Cavalier 			args[1] = ni->ni_mllid;
2326753c7e08SAugustin Cavalier 			args[2] = IEEE80211_REASON_PEER_LINK_CANCELED;
2327753c7e08SAugustin Cavalier 			ieee80211_send_action(ni,
2328753c7e08SAugustin Cavalier 			    IEEE80211_ACTION_CAT_SELF_PROT,
2329753c7e08SAugustin Cavalier 			    IEEE80211_ACTION_MESHPEERING_CLOSE,
2330753c7e08SAugustin Cavalier 			    args);
2331753c7e08SAugustin Cavalier 			mesh_linkchange(ni,
2332753c7e08SAugustin Cavalier 			    IEEE80211_NODE_MESH_HOLDING);
2333753c7e08SAugustin Cavalier 			mesh_peer_timeout_setup(ni);
2334753c7e08SAugustin Cavalier 			break;
2335753c7e08SAugustin Cavalier 		}
2336753c7e08SAugustin Cavalier 		mesh_linkchange(ni, IEEE80211_NODE_MESH_ESTABLISHED);
2337753c7e08SAugustin Cavalier 		ni->ni_mllid = meshpeer->peer_llinkid;
2338753c7e08SAugustin Cavalier 		args[0] = ni->ni_mlpid;
2339753c7e08SAugustin Cavalier 		args[1] = ni->ni_mllid;
2340753c7e08SAugustin Cavalier 		ieee80211_send_action(ni,
2341753c7e08SAugustin Cavalier 		    IEEE80211_ACTION_CAT_SELF_PROT,
2342753c7e08SAugustin Cavalier 		    IEEE80211_ACTION_MESHPEERING_CONFIRM,
2343753c7e08SAugustin Cavalier 		    args);
2344753c7e08SAugustin Cavalier 		mesh_peer_timeout_stop(ni);
2345753c7e08SAugustin Cavalier 		break;
2346753c7e08SAugustin Cavalier 	case IEEE80211_NODE_MESH_ESTABLISHED:
2347753c7e08SAugustin Cavalier 		if (ni->ni_mllid != meshpeer->peer_llinkid) {
2348753c7e08SAugustin Cavalier 			args[0] = ni->ni_mllid;
2349753c7e08SAugustin Cavalier 			args[1] = ni->ni_mlpid;
2350753c7e08SAugustin Cavalier 			args[2] = IEEE80211_REASON_PEER_LINK_CANCELED;
2351753c7e08SAugustin Cavalier 			ieee80211_send_action(ni,
2352753c7e08SAugustin Cavalier 			    IEEE80211_ACTION_CAT_SELF_PROT,
2353753c7e08SAugustin Cavalier 			    IEEE80211_ACTION_MESHPEERING_CLOSE,
2354753c7e08SAugustin Cavalier 			    args);
2355753c7e08SAugustin Cavalier 			mesh_linkchange(ni, IEEE80211_NODE_MESH_HOLDING);
2356753c7e08SAugustin Cavalier 			mesh_peer_timeout_setup(ni);
2357753c7e08SAugustin Cavalier 			break;
2358753c7e08SAugustin Cavalier 		}
2359753c7e08SAugustin Cavalier 		args[0] = ni->ni_mlpid;
2360753c7e08SAugustin Cavalier 		args[1] = ni->ni_mllid;
2361753c7e08SAugustin Cavalier 		ieee80211_send_action(ni,
2362753c7e08SAugustin Cavalier 		    IEEE80211_ACTION_CAT_SELF_PROT,
2363753c7e08SAugustin Cavalier 		    IEEE80211_ACTION_MESHPEERING_CONFIRM,
2364753c7e08SAugustin Cavalier 		    args);
2365753c7e08SAugustin Cavalier 		break;
2366753c7e08SAugustin Cavalier 	case IEEE80211_NODE_MESH_HOLDING:
2367753c7e08SAugustin Cavalier 		args[0] = ni->ni_mlpid;
2368753c7e08SAugustin Cavalier 		args[1] = meshpeer->peer_llinkid;
2369753c7e08SAugustin Cavalier 		/* Standard not clear about what the reaason code should be */
2370753c7e08SAugustin Cavalier 		args[2] = IEEE80211_REASON_PEER_LINK_CANCELED;
2371753c7e08SAugustin Cavalier 		ieee80211_send_action(ni,
2372753c7e08SAugustin Cavalier 		    IEEE80211_ACTION_CAT_SELF_PROT,
2373753c7e08SAugustin Cavalier 		    IEEE80211_ACTION_MESHPEERING_CLOSE,
2374753c7e08SAugustin Cavalier 		    args);
2375753c7e08SAugustin Cavalier 		break;
2376753c7e08SAugustin Cavalier 	}
2377753c7e08SAugustin Cavalier 	return 0;
2378753c7e08SAugustin Cavalier }
2379753c7e08SAugustin Cavalier 
2380753c7e08SAugustin Cavalier static int
mesh_recv_action_meshpeering_confirm(struct ieee80211_node * ni,const struct ieee80211_frame * wh,const uint8_t * frm,const uint8_t * efrm)2381753c7e08SAugustin Cavalier mesh_recv_action_meshpeering_confirm(struct ieee80211_node *ni,
2382753c7e08SAugustin Cavalier 	const struct ieee80211_frame *wh,
2383753c7e08SAugustin Cavalier 	const uint8_t *frm, const uint8_t *efrm)
2384753c7e08SAugustin Cavalier {
2385753c7e08SAugustin Cavalier 	struct ieee80211vap *vap = ni->ni_vap;
2386753c7e08SAugustin Cavalier 	struct ieee80211_meshpeer_ie ie;
2387753c7e08SAugustin Cavalier 	const struct ieee80211_meshpeer_ie *meshpeer;
2388753c7e08SAugustin Cavalier 	uint16_t args[3];
2389753c7e08SAugustin Cavalier 
2390753c7e08SAugustin Cavalier 	/* +2+2+2+2 for action + code + capabilites + status code + AID */
2391753c7e08SAugustin Cavalier 	meshpeer = mesh_parse_meshpeering_action(ni, wh, frm+2+2+2+2, efrm, &ie,
2392753c7e08SAugustin Cavalier 	    IEEE80211_ACTION_MESHPEERING_CONFIRM);
2393753c7e08SAugustin Cavalier 	if (meshpeer == NULL) {
2394753c7e08SAugustin Cavalier 		return 0;
2395753c7e08SAugustin Cavalier 	}
2396753c7e08SAugustin Cavalier 
2397753c7e08SAugustin Cavalier 	IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH, ni,
2398753c7e08SAugustin Cavalier 	    "recv PEER CONFIRM, local id 0x%x, peer id 0x%x",
2399753c7e08SAugustin Cavalier 	    meshpeer->peer_llinkid, meshpeer->peer_linkid);
2400753c7e08SAugustin Cavalier 
2401753c7e08SAugustin Cavalier 	switch (ni->ni_mlstate) {
2402753c7e08SAugustin Cavalier 	case IEEE80211_NODE_MESH_OPENRCV:
2403753c7e08SAugustin Cavalier 		mesh_linkchange(ni, IEEE80211_NODE_MESH_ESTABLISHED);
2404753c7e08SAugustin Cavalier 		mesh_peer_timeout_stop(ni);
2405753c7e08SAugustin Cavalier 		break;
2406753c7e08SAugustin Cavalier 	case IEEE80211_NODE_MESH_OPENSNT:
2407753c7e08SAugustin Cavalier 		mesh_linkchange(ni, IEEE80211_NODE_MESH_CONFIRMRCV);
2408753c7e08SAugustin Cavalier 		mesh_peer_timeout_setup(ni);
2409753c7e08SAugustin Cavalier 		break;
2410753c7e08SAugustin Cavalier 	case IEEE80211_NODE_MESH_HOLDING:
2411753c7e08SAugustin Cavalier 		args[0] = ni->ni_mlpid;
2412753c7e08SAugustin Cavalier 		args[1] = meshpeer->peer_llinkid;
2413753c7e08SAugustin Cavalier 		/* Standard not clear about what the reaason code should be */
2414753c7e08SAugustin Cavalier 		args[2] = IEEE80211_REASON_PEER_LINK_CANCELED;
2415753c7e08SAugustin Cavalier 		ieee80211_send_action(ni,
2416753c7e08SAugustin Cavalier 		    IEEE80211_ACTION_CAT_SELF_PROT,
2417753c7e08SAugustin Cavalier 		    IEEE80211_ACTION_MESHPEERING_CLOSE,
2418753c7e08SAugustin Cavalier 		    args);
2419753c7e08SAugustin Cavalier 		break;
2420753c7e08SAugustin Cavalier 	case IEEE80211_NODE_MESH_CONFIRMRCV:
2421753c7e08SAugustin Cavalier 		if (ni->ni_mllid != meshpeer->peer_llinkid) {
2422753c7e08SAugustin Cavalier 			args[0] = ni->ni_mlpid;
2423753c7e08SAugustin Cavalier 			args[1] = ni->ni_mllid;
2424753c7e08SAugustin Cavalier 			args[2] = IEEE80211_REASON_PEER_LINK_CANCELED;
2425753c7e08SAugustin Cavalier 			ieee80211_send_action(ni,
2426753c7e08SAugustin Cavalier 			    IEEE80211_ACTION_CAT_SELF_PROT,
2427753c7e08SAugustin Cavalier 			    IEEE80211_ACTION_MESHPEERING_CLOSE,
2428753c7e08SAugustin Cavalier 			    args);
2429753c7e08SAugustin Cavalier 			mesh_linkchange(ni, IEEE80211_NODE_MESH_HOLDING);
2430753c7e08SAugustin Cavalier 			mesh_peer_timeout_setup(ni);
2431753c7e08SAugustin Cavalier 		}
2432753c7e08SAugustin Cavalier 		break;
2433753c7e08SAugustin Cavalier 	default:
2434753c7e08SAugustin Cavalier 		IEEE80211_DISCARD(vap,
2435753c7e08SAugustin Cavalier 		    IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH,
2436753c7e08SAugustin Cavalier 		    wh, NULL, "received confirm in invalid state %d",
2437753c7e08SAugustin Cavalier 		    ni->ni_mlstate);
2438753c7e08SAugustin Cavalier 		vap->iv_stats.is_rx_mgtdiscard++;
2439753c7e08SAugustin Cavalier 		break;
2440753c7e08SAugustin Cavalier 	}
2441753c7e08SAugustin Cavalier 	return 0;
2442753c7e08SAugustin Cavalier }
2443753c7e08SAugustin Cavalier 
2444753c7e08SAugustin Cavalier static int
mesh_recv_action_meshpeering_close(struct ieee80211_node * ni,const struct ieee80211_frame * wh,const uint8_t * frm,const uint8_t * efrm)2445753c7e08SAugustin Cavalier mesh_recv_action_meshpeering_close(struct ieee80211_node *ni,
2446753c7e08SAugustin Cavalier 	const struct ieee80211_frame *wh,
2447753c7e08SAugustin Cavalier 	const uint8_t *frm, const uint8_t *efrm)
2448753c7e08SAugustin Cavalier {
2449753c7e08SAugustin Cavalier 	struct ieee80211_meshpeer_ie ie;
2450753c7e08SAugustin Cavalier 	const struct ieee80211_meshpeer_ie *meshpeer;
2451753c7e08SAugustin Cavalier 	uint16_t args[3];
2452753c7e08SAugustin Cavalier 
2453753c7e08SAugustin Cavalier 	/* +2 for action + code */
2454753c7e08SAugustin Cavalier 	meshpeer = mesh_parse_meshpeering_action(ni, wh, frm+2, efrm, &ie,
2455753c7e08SAugustin Cavalier 	    IEEE80211_ACTION_MESHPEERING_CLOSE);
2456753c7e08SAugustin Cavalier 	if (meshpeer == NULL) {
2457753c7e08SAugustin Cavalier 		return 0;
2458753c7e08SAugustin Cavalier 	}
2459753c7e08SAugustin Cavalier 
2460753c7e08SAugustin Cavalier 	/*
2461753c7e08SAugustin Cavalier 	 * XXX: check reason code, for example we could receive
2462753c7e08SAugustin Cavalier 	 * IEEE80211_REASON_MESH_MAX_PEERS then we should not attempt
2463753c7e08SAugustin Cavalier 	 * to peer again.
2464753c7e08SAugustin Cavalier 	 */
2465753c7e08SAugustin Cavalier 
2466753c7e08SAugustin Cavalier 	IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH,
2467753c7e08SAugustin Cavalier 	    ni, "%s", "recv PEER CLOSE");
2468753c7e08SAugustin Cavalier 
2469753c7e08SAugustin Cavalier 	switch (ni->ni_mlstate) {
2470753c7e08SAugustin Cavalier 	case IEEE80211_NODE_MESH_IDLE:
2471753c7e08SAugustin Cavalier 		/* ignore */
2472753c7e08SAugustin Cavalier 		break;
2473753c7e08SAugustin Cavalier 	case IEEE80211_NODE_MESH_OPENRCV:
2474753c7e08SAugustin Cavalier 	case IEEE80211_NODE_MESH_OPENSNT:
2475753c7e08SAugustin Cavalier 	case IEEE80211_NODE_MESH_CONFIRMRCV:
2476753c7e08SAugustin Cavalier 	case IEEE80211_NODE_MESH_ESTABLISHED:
2477753c7e08SAugustin Cavalier 		args[0] = ni->ni_mlpid;
2478753c7e08SAugustin Cavalier 		args[1] = ni->ni_mllid;
2479753c7e08SAugustin Cavalier 		args[2] = IEEE80211_REASON_MESH_CLOSE_RCVD;
2480753c7e08SAugustin Cavalier 		ieee80211_send_action(ni,
2481753c7e08SAugustin Cavalier 		    IEEE80211_ACTION_CAT_SELF_PROT,
2482753c7e08SAugustin Cavalier 		    IEEE80211_ACTION_MESHPEERING_CLOSE,
2483753c7e08SAugustin Cavalier 		    args);
2484753c7e08SAugustin Cavalier 		mesh_linkchange(ni, IEEE80211_NODE_MESH_HOLDING);
2485753c7e08SAugustin Cavalier 		mesh_peer_timeout_setup(ni);
2486753c7e08SAugustin Cavalier 		break;
2487753c7e08SAugustin Cavalier 	case IEEE80211_NODE_MESH_HOLDING:
2488753c7e08SAugustin Cavalier 		mesh_linkchange(ni, IEEE80211_NODE_MESH_IDLE);
2489753c7e08SAugustin Cavalier 		mesh_peer_timeout_stop(ni);
2490753c7e08SAugustin Cavalier 		break;
2491753c7e08SAugustin Cavalier 	}
2492753c7e08SAugustin Cavalier 	return 0;
2493753c7e08SAugustin Cavalier }
2494753c7e08SAugustin Cavalier 
2495753c7e08SAugustin Cavalier /*
2496753c7e08SAugustin Cavalier  * Link Metric handling.
2497753c7e08SAugustin Cavalier  */
2498753c7e08SAugustin Cavalier static int
mesh_recv_action_meshlmetric(struct ieee80211_node * ni,const struct ieee80211_frame * wh,const uint8_t * frm,const uint8_t * efrm)2499753c7e08SAugustin Cavalier mesh_recv_action_meshlmetric(struct ieee80211_node *ni,
2500753c7e08SAugustin Cavalier 	const struct ieee80211_frame *wh,
2501753c7e08SAugustin Cavalier 	const uint8_t *frm, const uint8_t *efrm)
2502753c7e08SAugustin Cavalier {
2503753c7e08SAugustin Cavalier 	const struct ieee80211_meshlmetric_ie *ie =
2504753c7e08SAugustin Cavalier 	    (const struct ieee80211_meshlmetric_ie *)
2505753c7e08SAugustin Cavalier 	    (frm+2); /* action + code */
2506753c7e08SAugustin Cavalier 	struct ieee80211_meshlmetric_ie lm_rep;
2507753c7e08SAugustin Cavalier 
2508753c7e08SAugustin Cavalier 	if (ie->lm_flags & IEEE80211_MESH_LMETRIC_FLAGS_REQ) {
2509753c7e08SAugustin Cavalier 		lm_rep.lm_flags = 0;
2510753c7e08SAugustin Cavalier 		lm_rep.lm_metric = mesh_airtime_calc(ni);
2511753c7e08SAugustin Cavalier 		ieee80211_send_action(ni,
2512753c7e08SAugustin Cavalier 		    IEEE80211_ACTION_CAT_MESH,
2513753c7e08SAugustin Cavalier 		    IEEE80211_ACTION_MESH_LMETRIC,
2514753c7e08SAugustin Cavalier 		    &lm_rep);
2515753c7e08SAugustin Cavalier 	}
2516753c7e08SAugustin Cavalier 	/* XXX: else do nothing for now */
2517753c7e08SAugustin Cavalier 	return 0;
2518753c7e08SAugustin Cavalier }
2519753c7e08SAugustin Cavalier 
2520753c7e08SAugustin Cavalier /*
2521753c7e08SAugustin Cavalier  * Parse meshgate action ie's for GANN frames.
2522753c7e08SAugustin Cavalier  * Returns -1 if parsing fails, otherwise 0.
2523753c7e08SAugustin Cavalier  */
2524753c7e08SAugustin Cavalier static int
mesh_parse_meshgate_action(struct ieee80211_node * ni,const struct ieee80211_frame * wh,struct ieee80211_meshgann_ie * ie,const uint8_t * frm,const uint8_t * efrm)2525753c7e08SAugustin Cavalier mesh_parse_meshgate_action(struct ieee80211_node *ni,
2526753c7e08SAugustin Cavalier     const struct ieee80211_frame *wh,	/* XXX for VERIFY_LENGTH */
2527753c7e08SAugustin Cavalier     struct ieee80211_meshgann_ie *ie, const uint8_t *frm, const uint8_t *efrm)
2528753c7e08SAugustin Cavalier {
2529753c7e08SAugustin Cavalier 	struct ieee80211vap *vap = ni->ni_vap;
2530753c7e08SAugustin Cavalier 	const struct ieee80211_meshgann_ie *gannie;
2531753c7e08SAugustin Cavalier 
2532753c7e08SAugustin Cavalier 	while (efrm - frm > 1) {
2533753c7e08SAugustin Cavalier 		IEEE80211_VERIFY_LENGTH(efrm - frm, frm[1] + 2, return -1);
2534753c7e08SAugustin Cavalier 		switch (*frm) {
2535753c7e08SAugustin Cavalier 		case IEEE80211_ELEMID_MESHGANN:
2536753c7e08SAugustin Cavalier 			gannie = (const struct ieee80211_meshgann_ie *) frm;
2537753c7e08SAugustin Cavalier 			memset(ie, 0, sizeof(*ie));
2538753c7e08SAugustin Cavalier 			ie->gann_ie = gannie->gann_ie;
2539753c7e08SAugustin Cavalier 			ie->gann_len = gannie->gann_len;
2540753c7e08SAugustin Cavalier 			ie->gann_flags = gannie->gann_flags;
2541753c7e08SAugustin Cavalier 			ie->gann_hopcount = gannie->gann_hopcount;
2542753c7e08SAugustin Cavalier 			ie->gann_ttl = gannie->gann_ttl;
2543753c7e08SAugustin Cavalier 			IEEE80211_ADDR_COPY(ie->gann_addr, gannie->gann_addr);
2544753c7e08SAugustin Cavalier 			ie->gann_seq = le32dec(&gannie->gann_seq);
2545753c7e08SAugustin Cavalier 			ie->gann_interval = le16dec(&gannie->gann_interval);
2546753c7e08SAugustin Cavalier 			break;
2547753c7e08SAugustin Cavalier 		}
2548753c7e08SAugustin Cavalier 		frm += frm[1] + 2;
2549753c7e08SAugustin Cavalier 	}
2550753c7e08SAugustin Cavalier 
2551753c7e08SAugustin Cavalier 	return 0;
2552753c7e08SAugustin Cavalier }
2553753c7e08SAugustin Cavalier 
2554753c7e08SAugustin Cavalier /*
2555753c7e08SAugustin Cavalier  * Mesh Gate Announcement handling.
2556753c7e08SAugustin Cavalier  */
2557753c7e08SAugustin Cavalier static int
mesh_recv_action_meshgate(struct ieee80211_node * ni,const struct ieee80211_frame * wh,const uint8_t * frm,const uint8_t * efrm)2558753c7e08SAugustin Cavalier mesh_recv_action_meshgate(struct ieee80211_node *ni,
2559753c7e08SAugustin Cavalier 	const struct ieee80211_frame *wh,
2560753c7e08SAugustin Cavalier 	const uint8_t *frm, const uint8_t *efrm)
2561753c7e08SAugustin Cavalier {
2562753c7e08SAugustin Cavalier 	struct ieee80211vap *vap = ni->ni_vap;
2563753c7e08SAugustin Cavalier 	struct ieee80211_mesh_state *ms = vap->iv_mesh;
2564753c7e08SAugustin Cavalier 	struct ieee80211_mesh_gate_route *gr, *next;
2565753c7e08SAugustin Cavalier 	struct ieee80211_mesh_route *rt_gate;
2566753c7e08SAugustin Cavalier 	struct ieee80211_meshgann_ie pgann;
2567753c7e08SAugustin Cavalier 	struct ieee80211_meshgann_ie ie;
2568753c7e08SAugustin Cavalier 	int found = 0;
2569753c7e08SAugustin Cavalier 
2570753c7e08SAugustin Cavalier 	/* +2 for action + code */
2571753c7e08SAugustin Cavalier 	if (mesh_parse_meshgate_action(ni, wh, &ie, frm+2, efrm) != 0) {
2572753c7e08SAugustin Cavalier 		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_MESH,
2573753c7e08SAugustin Cavalier 		    ni->ni_macaddr, NULL, "%s",
2574753c7e08SAugustin Cavalier 		    "GANN parsing failed");
2575753c7e08SAugustin Cavalier 		vap->iv_stats.is_rx_mgtdiscard++;
2576753c7e08SAugustin Cavalier 		return (0);
2577753c7e08SAugustin Cavalier 	}
2578753c7e08SAugustin Cavalier 
2579753c7e08SAugustin Cavalier 	if (IEEE80211_ADDR_EQ(vap->iv_myaddr, ie.gann_addr))
2580753c7e08SAugustin Cavalier 		return 0;
2581753c7e08SAugustin Cavalier 
2582753c7e08SAugustin Cavalier 	IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH, ni->ni_macaddr,
2583753c7e08SAugustin Cavalier 	    "received GANN, meshgate: %6D (seq %u)", ie.gann_addr, ":",
2584753c7e08SAugustin Cavalier 	    ie.gann_seq);
2585753c7e08SAugustin Cavalier 
2586753c7e08SAugustin Cavalier 	if (ms == NULL)
2587753c7e08SAugustin Cavalier 		return (0);
2588753c7e08SAugustin Cavalier 	MESH_RT_LOCK(ms);
2589753c7e08SAugustin Cavalier 	TAILQ_FOREACH_SAFE(gr, &ms->ms_known_gates, gr_next, next) {
2590753c7e08SAugustin Cavalier 		if (!IEEE80211_ADDR_EQ(gr->gr_addr, ie.gann_addr))
2591753c7e08SAugustin Cavalier 			continue;
2592753c7e08SAugustin Cavalier 		if (ie.gann_seq <= gr->gr_lastseq) {
2593753c7e08SAugustin Cavalier 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_MESH,
2594753c7e08SAugustin Cavalier 			    ni->ni_macaddr, NULL,
2595753c7e08SAugustin Cavalier 			    "GANN old seqno %u <= %u",
2596753c7e08SAugustin Cavalier 			    ie.gann_seq, gr->gr_lastseq);
2597753c7e08SAugustin Cavalier 			MESH_RT_UNLOCK(ms);
2598753c7e08SAugustin Cavalier 			return (0);
2599753c7e08SAugustin Cavalier 		}
2600753c7e08SAugustin Cavalier 		/* corresponding mesh gate found & GANN accepted */
2601753c7e08SAugustin Cavalier 		found = 1;
2602753c7e08SAugustin Cavalier 		break;
2603753c7e08SAugustin Cavalier 	}
2604753c7e08SAugustin Cavalier 	if (found == 0) {
2605753c7e08SAugustin Cavalier 		/* this GANN is from a new mesh Gate add it to known table. */
2606753c7e08SAugustin Cavalier 		IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH, ie.gann_addr,
2607753c7e08SAugustin Cavalier 		    "stored new GANN information, seq %u.", ie.gann_seq);
2608753c7e08SAugustin Cavalier 		gr = IEEE80211_MALLOC(ALIGN(sizeof(struct ieee80211_mesh_gate_route)),
2609753c7e08SAugustin Cavalier 		    M_80211_MESH_GT_RT,
2610753c7e08SAugustin Cavalier 		    IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
2611753c7e08SAugustin Cavalier 		IEEE80211_ADDR_COPY(gr->gr_addr, ie.gann_addr);
2612753c7e08SAugustin Cavalier 		TAILQ_INSERT_TAIL(&ms->ms_known_gates, gr, gr_next);
2613753c7e08SAugustin Cavalier 	}
2614753c7e08SAugustin Cavalier 	gr->gr_lastseq = ie.gann_seq;
2615753c7e08SAugustin Cavalier 
2616753c7e08SAugustin Cavalier 	/* check if we have a path to this gate */
2617753c7e08SAugustin Cavalier 	rt_gate = mesh_rt_find_locked(ms, gr->gr_addr);
2618753c7e08SAugustin Cavalier 	if (rt_gate != NULL &&
2619753c7e08SAugustin Cavalier 	    rt_gate->rt_flags & IEEE80211_MESHRT_FLAGS_VALID) {
2620753c7e08SAugustin Cavalier 		gr->gr_route = rt_gate;
2621753c7e08SAugustin Cavalier 		rt_gate->rt_flags |= IEEE80211_MESHRT_FLAGS_GATE;
2622753c7e08SAugustin Cavalier 	}
2623753c7e08SAugustin Cavalier 
2624753c7e08SAugustin Cavalier 	MESH_RT_UNLOCK(ms);
2625753c7e08SAugustin Cavalier 
2626753c7e08SAugustin Cavalier 	/* popagate only if decremented ttl >= 1 && forwarding is enabled */
2627753c7e08SAugustin Cavalier 	if ((ie.gann_ttl - 1) < 1 && !(ms->ms_flags & IEEE80211_MESHFLAGS_FWD))
2628753c7e08SAugustin Cavalier 		return 0;
2629753c7e08SAugustin Cavalier 	pgann.gann_flags = ie.gann_flags; /* Reserved */
2630753c7e08SAugustin Cavalier 	pgann.gann_hopcount = ie.gann_hopcount + 1;
2631753c7e08SAugustin Cavalier 	pgann.gann_ttl = ie.gann_ttl - 1;
2632753c7e08SAugustin Cavalier 	IEEE80211_ADDR_COPY(pgann.gann_addr, ie.gann_addr);
2633753c7e08SAugustin Cavalier 	pgann.gann_seq = ie.gann_seq;
2634753c7e08SAugustin Cavalier 	pgann.gann_interval = ie.gann_interval;
2635753c7e08SAugustin Cavalier 
2636753c7e08SAugustin Cavalier 	IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH, ie.gann_addr,
2637753c7e08SAugustin Cavalier 	    "%s", "propagate GANN");
2638753c7e08SAugustin Cavalier 
2639753c7e08SAugustin Cavalier 	ieee80211_send_action(vap->iv_bss, IEEE80211_ACTION_CAT_MESH,
2640753c7e08SAugustin Cavalier 	    IEEE80211_ACTION_MESH_GANN, &pgann);
2641753c7e08SAugustin Cavalier 
2642753c7e08SAugustin Cavalier 	return 0;
2643753c7e08SAugustin Cavalier }
2644753c7e08SAugustin Cavalier 
2645753c7e08SAugustin Cavalier static int
mesh_send_action(struct ieee80211_node * ni,const uint8_t sa[IEEE80211_ADDR_LEN],const uint8_t da[IEEE80211_ADDR_LEN],struct mbuf * m)2646753c7e08SAugustin Cavalier mesh_send_action(struct ieee80211_node *ni,
2647753c7e08SAugustin Cavalier     const uint8_t sa[IEEE80211_ADDR_LEN],
2648753c7e08SAugustin Cavalier     const uint8_t da[IEEE80211_ADDR_LEN],
2649753c7e08SAugustin Cavalier     struct mbuf *m)
2650753c7e08SAugustin Cavalier {
2651753c7e08SAugustin Cavalier 	struct ieee80211vap *vap = ni->ni_vap;
2652753c7e08SAugustin Cavalier 	struct ieee80211com *ic = ni->ni_ic;
2653753c7e08SAugustin Cavalier 	struct ieee80211_bpf_params params;
2654753c7e08SAugustin Cavalier 	int ret;
2655753c7e08SAugustin Cavalier 
2656753c7e08SAugustin Cavalier 	KASSERT(ni != NULL, ("null node"));
2657753c7e08SAugustin Cavalier 
2658753c7e08SAugustin Cavalier 	if (vap->iv_state == IEEE80211_S_CAC) {
2659753c7e08SAugustin Cavalier 		IEEE80211_NOTE(vap, IEEE80211_MSG_OUTPUT, ni,
2660753c7e08SAugustin Cavalier 		    "block %s frame in CAC state", "Mesh action");
2661753c7e08SAugustin Cavalier 		vap->iv_stats.is_tx_badstate++;
2662753c7e08SAugustin Cavalier 		ieee80211_free_node(ni);
2663753c7e08SAugustin Cavalier 		m_freem(m);
2664753c7e08SAugustin Cavalier 		return EIO;		/* XXX */
2665753c7e08SAugustin Cavalier 	}
2666753c7e08SAugustin Cavalier 
266786021fd4SAugustin Cavalier 	M_PREPEND(m, sizeof(struct ieee80211_frame), IEEE80211_M_NOWAIT);
2668753c7e08SAugustin Cavalier 	if (m == NULL) {
2669753c7e08SAugustin Cavalier 		ieee80211_free_node(ni);
2670753c7e08SAugustin Cavalier 		return ENOMEM;
2671753c7e08SAugustin Cavalier 	}
2672753c7e08SAugustin Cavalier 
2673753c7e08SAugustin Cavalier 	IEEE80211_TX_LOCK(ic);
2674753c7e08SAugustin Cavalier 	ieee80211_send_setup(ni, m,
2675753c7e08SAugustin Cavalier 	     IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_ACTION,
2676753c7e08SAugustin Cavalier 	     IEEE80211_NONQOS_TID, sa, da, sa);
2677753c7e08SAugustin Cavalier 	m->m_flags |= M_ENCAP;		/* mark encapsulated */
2678753c7e08SAugustin Cavalier 
2679753c7e08SAugustin Cavalier 	memset(&params, 0, sizeof(params));
2680753c7e08SAugustin Cavalier 	params.ibp_pri = WME_AC_VO;
2681753c7e08SAugustin Cavalier 	params.ibp_rate0 = ni->ni_txparms->mgmtrate;
2682753c7e08SAugustin Cavalier 	if (IEEE80211_IS_MULTICAST(da))
2683753c7e08SAugustin Cavalier 		params.ibp_try0 = 1;
2684753c7e08SAugustin Cavalier 	else
2685753c7e08SAugustin Cavalier 		params.ibp_try0 = ni->ni_txparms->maxretry;
2686753c7e08SAugustin Cavalier 	params.ibp_power = ni->ni_txpower;
2687753c7e08SAugustin Cavalier 
2688753c7e08SAugustin Cavalier 	IEEE80211_NODE_STAT(ni, tx_mgmt);
2689753c7e08SAugustin Cavalier 
2690753c7e08SAugustin Cavalier 	ret = ieee80211_raw_output(vap, ni, m, &params);
2691753c7e08SAugustin Cavalier 	IEEE80211_TX_UNLOCK(ic);
2692753c7e08SAugustin Cavalier 	return (ret);
2693753c7e08SAugustin Cavalier }
2694753c7e08SAugustin Cavalier 
2695753c7e08SAugustin Cavalier #define	ADDSHORT(frm, v) do {			\
2696753c7e08SAugustin Cavalier 	frm[0] = (v) & 0xff;			\
2697753c7e08SAugustin Cavalier 	frm[1] = (v) >> 8;			\
2698753c7e08SAugustin Cavalier 	frm += 2;				\
2699753c7e08SAugustin Cavalier } while (0)
2700753c7e08SAugustin Cavalier #define	ADDWORD(frm, v) do {			\
2701753c7e08SAugustin Cavalier 	frm[0] = (v) & 0xff;			\
2702753c7e08SAugustin Cavalier 	frm[1] = ((v) >> 8) & 0xff;		\
2703753c7e08SAugustin Cavalier 	frm[2] = ((v) >> 16) & 0xff;		\
2704753c7e08SAugustin Cavalier 	frm[3] = ((v) >> 24) & 0xff;		\
2705753c7e08SAugustin Cavalier 	frm += 4;				\
2706753c7e08SAugustin Cavalier } while (0)
2707753c7e08SAugustin Cavalier 
2708753c7e08SAugustin Cavalier static int
mesh_send_action_meshpeering_open(struct ieee80211_node * ni,int category,int action,void * args0)2709753c7e08SAugustin Cavalier mesh_send_action_meshpeering_open(struct ieee80211_node *ni,
2710753c7e08SAugustin Cavalier 	int category, int action, void *args0)
2711753c7e08SAugustin Cavalier {
2712753c7e08SAugustin Cavalier 	struct ieee80211vap *vap = ni->ni_vap;
2713753c7e08SAugustin Cavalier 	struct ieee80211com *ic = ni->ni_ic;
2714753c7e08SAugustin Cavalier 	uint16_t *args = args0;
2715753c7e08SAugustin Cavalier 	const struct ieee80211_rateset *rs;
2716753c7e08SAugustin Cavalier 	struct mbuf *m;
2717753c7e08SAugustin Cavalier 	uint8_t *frm;
2718753c7e08SAugustin Cavalier 
2719753c7e08SAugustin Cavalier 	IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH, ni,
2720753c7e08SAugustin Cavalier 	    "send PEER OPEN action: localid 0x%x", args[0]);
2721753c7e08SAugustin Cavalier 
2722753c7e08SAugustin Cavalier 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
2723753c7e08SAugustin Cavalier 	    "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", __func__, __LINE__,
2724753c7e08SAugustin Cavalier 	    ni, ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)+1);
2725753c7e08SAugustin Cavalier 	ieee80211_ref_node(ni);
2726753c7e08SAugustin Cavalier 
2727753c7e08SAugustin Cavalier 	m = ieee80211_getmgtframe(&frm,
2728753c7e08SAugustin Cavalier 	    ic->ic_headroom + sizeof(struct ieee80211_frame),
2729753c7e08SAugustin Cavalier 	    sizeof(uint16_t)	/* action+category */
2730753c7e08SAugustin Cavalier 	    + sizeof(uint16_t)	/* capabilites */
2731753c7e08SAugustin Cavalier 	    + 2 + IEEE80211_RATE_SIZE
2732753c7e08SAugustin Cavalier 	    + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
2733753c7e08SAugustin Cavalier 	    + 2 + IEEE80211_MESHID_LEN
2734753c7e08SAugustin Cavalier 	    + sizeof(struct ieee80211_meshconf_ie)
2735753c7e08SAugustin Cavalier 	    + sizeof(struct ieee80211_meshpeer_ie)
2736753c7e08SAugustin Cavalier 	);
2737753c7e08SAugustin Cavalier 	if (m != NULL) {
2738753c7e08SAugustin Cavalier 		/*
2739753c7e08SAugustin Cavalier 		 * mesh peer open action frame format:
2740753c7e08SAugustin Cavalier 		 *   [1] category
2741753c7e08SAugustin Cavalier 		 *   [1] action
2742753c7e08SAugustin Cavalier 		 *   [2] capabilities
2743753c7e08SAugustin Cavalier 		 *   [tlv] rates
2744753c7e08SAugustin Cavalier 		 *   [tlv] xrates
2745753c7e08SAugustin Cavalier 		 *   [tlv] mesh id
2746753c7e08SAugustin Cavalier 		 *   [tlv] mesh conf
2747753c7e08SAugustin Cavalier 		 *   [tlv] mesh peer link mgmt
2748753c7e08SAugustin Cavalier 		 */
2749753c7e08SAugustin Cavalier 		*frm++ = category;
2750753c7e08SAugustin Cavalier 		*frm++ = action;
2751753c7e08SAugustin Cavalier 		ADDSHORT(frm, ieee80211_getcapinfo(vap, ni->ni_chan));
2752753c7e08SAugustin Cavalier 		rs = ieee80211_get_suprates(ic, ic->ic_curchan);
2753753c7e08SAugustin Cavalier 		frm = ieee80211_add_rates(frm, rs);
2754753c7e08SAugustin Cavalier 		frm = ieee80211_add_xrates(frm, rs);
2755753c7e08SAugustin Cavalier 		frm = ieee80211_add_meshid(frm, vap);
2756753c7e08SAugustin Cavalier 		frm = ieee80211_add_meshconf(frm, vap);
2757753c7e08SAugustin Cavalier 		frm = ieee80211_add_meshpeer(frm, IEEE80211_ACTION_MESHPEERING_OPEN,
2758753c7e08SAugustin Cavalier 		    args[0], 0, 0);
2759753c7e08SAugustin Cavalier 		m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2760753c7e08SAugustin Cavalier 		return mesh_send_action(ni, vap->iv_myaddr, ni->ni_macaddr, m);
2761753c7e08SAugustin Cavalier 	} else {
2762753c7e08SAugustin Cavalier 		vap->iv_stats.is_tx_nobuf++;
2763753c7e08SAugustin Cavalier 		ieee80211_free_node(ni);
2764753c7e08SAugustin Cavalier 		return ENOMEM;
2765753c7e08SAugustin Cavalier 	}
2766753c7e08SAugustin Cavalier }
2767753c7e08SAugustin Cavalier 
2768753c7e08SAugustin Cavalier static int
mesh_send_action_meshpeering_confirm(struct ieee80211_node * ni,int category,int action,void * args0)2769753c7e08SAugustin Cavalier mesh_send_action_meshpeering_confirm(struct ieee80211_node *ni,
2770753c7e08SAugustin Cavalier 	int category, int action, void *args0)
2771753c7e08SAugustin Cavalier {
2772753c7e08SAugustin Cavalier 	struct ieee80211vap *vap = ni->ni_vap;
2773753c7e08SAugustin Cavalier 	struct ieee80211com *ic = ni->ni_ic;
2774753c7e08SAugustin Cavalier 	uint16_t *args = args0;
2775753c7e08SAugustin Cavalier 	const struct ieee80211_rateset *rs;
2776753c7e08SAugustin Cavalier 	struct mbuf *m;
2777753c7e08SAugustin Cavalier 	uint8_t *frm;
2778753c7e08SAugustin Cavalier 
2779753c7e08SAugustin Cavalier 	IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH, ni,
2780753c7e08SAugustin Cavalier 	    "send PEER CONFIRM action: localid 0x%x, peerid 0x%x",
2781753c7e08SAugustin Cavalier 	    args[0], args[1]);
2782753c7e08SAugustin Cavalier 
2783753c7e08SAugustin Cavalier 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
2784753c7e08SAugustin Cavalier 	    "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", __func__, __LINE__,
2785753c7e08SAugustin Cavalier 	    ni, ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)+1);
2786753c7e08SAugustin Cavalier 	ieee80211_ref_node(ni);
2787753c7e08SAugustin Cavalier 
2788753c7e08SAugustin Cavalier 	m = ieee80211_getmgtframe(&frm,
2789753c7e08SAugustin Cavalier 	    ic->ic_headroom + sizeof(struct ieee80211_frame),
2790753c7e08SAugustin Cavalier 	    sizeof(uint16_t)	/* action+category */
2791753c7e08SAugustin Cavalier 	    + sizeof(uint16_t)	/* capabilites */
2792753c7e08SAugustin Cavalier 	    + sizeof(uint16_t)	/* status code */
2793753c7e08SAugustin Cavalier 	    + sizeof(uint16_t)	/* AID */
2794753c7e08SAugustin Cavalier 	    + 2 + IEEE80211_RATE_SIZE
2795753c7e08SAugustin Cavalier 	    + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
2796753c7e08SAugustin Cavalier 	    + 2 + IEEE80211_MESHID_LEN
2797753c7e08SAugustin Cavalier 	    + sizeof(struct ieee80211_meshconf_ie)
2798753c7e08SAugustin Cavalier 	    + sizeof(struct ieee80211_meshpeer_ie)
2799753c7e08SAugustin Cavalier 	);
2800753c7e08SAugustin Cavalier 	if (m != NULL) {
2801753c7e08SAugustin Cavalier 		/*
2802753c7e08SAugustin Cavalier 		 * mesh peer confirm action frame format:
2803753c7e08SAugustin Cavalier 		 *   [1] category
2804753c7e08SAugustin Cavalier 		 *   [1] action
2805753c7e08SAugustin Cavalier 		 *   [2] capabilities
2806753c7e08SAugustin Cavalier 		 *   [2] status code
2807753c7e08SAugustin Cavalier 		 *   [2] association id (peer ID)
2808753c7e08SAugustin Cavalier 		 *   [tlv] rates
2809753c7e08SAugustin Cavalier 		 *   [tlv] xrates
2810753c7e08SAugustin Cavalier 		 *   [tlv] mesh id
2811753c7e08SAugustin Cavalier 		 *   [tlv] mesh conf
2812753c7e08SAugustin Cavalier 		 *   [tlv] mesh peer link mgmt
2813753c7e08SAugustin Cavalier 		 */
2814753c7e08SAugustin Cavalier 		*frm++ = category;
2815753c7e08SAugustin Cavalier 		*frm++ = action;
2816753c7e08SAugustin Cavalier 		ADDSHORT(frm, ieee80211_getcapinfo(vap, ni->ni_chan));
2817753c7e08SAugustin Cavalier 		ADDSHORT(frm, 0);		/* status code */
2818753c7e08SAugustin Cavalier 		ADDSHORT(frm, args[1]);		/* AID */
2819753c7e08SAugustin Cavalier 		rs = ieee80211_get_suprates(ic, ic->ic_curchan);
2820753c7e08SAugustin Cavalier 		frm = ieee80211_add_rates(frm, rs);
2821753c7e08SAugustin Cavalier 		frm = ieee80211_add_xrates(frm, rs);
2822753c7e08SAugustin Cavalier 		frm = ieee80211_add_meshid(frm, vap);
2823753c7e08SAugustin Cavalier 		frm = ieee80211_add_meshconf(frm, vap);
2824753c7e08SAugustin Cavalier 		frm = ieee80211_add_meshpeer(frm,
2825753c7e08SAugustin Cavalier 		    IEEE80211_ACTION_MESHPEERING_CONFIRM,
2826753c7e08SAugustin Cavalier 		    args[0], args[1], 0);
2827753c7e08SAugustin Cavalier 		m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2828753c7e08SAugustin Cavalier 		return mesh_send_action(ni, vap->iv_myaddr, ni->ni_macaddr, m);
2829753c7e08SAugustin Cavalier 	} else {
2830753c7e08SAugustin Cavalier 		vap->iv_stats.is_tx_nobuf++;
2831753c7e08SAugustin Cavalier 		ieee80211_free_node(ni);
2832753c7e08SAugustin Cavalier 		return ENOMEM;
2833753c7e08SAugustin Cavalier 	}
2834753c7e08SAugustin Cavalier }
2835753c7e08SAugustin Cavalier 
2836753c7e08SAugustin Cavalier static int
mesh_send_action_meshpeering_close(struct ieee80211_node * ni,int category,int action,void * args0)2837753c7e08SAugustin Cavalier mesh_send_action_meshpeering_close(struct ieee80211_node *ni,
2838753c7e08SAugustin Cavalier 	int category, int action, void *args0)
2839753c7e08SAugustin Cavalier {
2840753c7e08SAugustin Cavalier 	struct ieee80211vap *vap = ni->ni_vap;
2841753c7e08SAugustin Cavalier 	struct ieee80211com *ic = ni->ni_ic;
2842753c7e08SAugustin Cavalier 	uint16_t *args = args0;
2843753c7e08SAugustin Cavalier 	struct mbuf *m;
2844753c7e08SAugustin Cavalier 	uint8_t *frm;
2845753c7e08SAugustin Cavalier 
2846753c7e08SAugustin Cavalier 	IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH, ni,
2847753c7e08SAugustin Cavalier 	    "send PEER CLOSE action: localid 0x%x, peerid 0x%x reason %d (%s)",
2848753c7e08SAugustin Cavalier 	    args[0], args[1], args[2], ieee80211_reason_to_string(args[2]));
2849753c7e08SAugustin Cavalier 
2850753c7e08SAugustin Cavalier 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
2851753c7e08SAugustin Cavalier 	    "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", __func__, __LINE__,
2852753c7e08SAugustin Cavalier 	    ni, ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)+1);
2853753c7e08SAugustin Cavalier 	ieee80211_ref_node(ni);
2854753c7e08SAugustin Cavalier 
2855753c7e08SAugustin Cavalier 	m = ieee80211_getmgtframe(&frm,
2856753c7e08SAugustin Cavalier 	    ic->ic_headroom + sizeof(struct ieee80211_frame),
2857753c7e08SAugustin Cavalier 	    sizeof(uint16_t)	/* action+category */
2858753c7e08SAugustin Cavalier 	    + sizeof(uint16_t)	/* reason code */
2859753c7e08SAugustin Cavalier 	    + 2 + IEEE80211_MESHID_LEN
2860753c7e08SAugustin Cavalier 	    + sizeof(struct ieee80211_meshpeer_ie)
2861753c7e08SAugustin Cavalier 	);
2862753c7e08SAugustin Cavalier 	if (m != NULL) {
2863753c7e08SAugustin Cavalier 		/*
2864753c7e08SAugustin Cavalier 		 * mesh peer close action frame format:
2865753c7e08SAugustin Cavalier 		 *   [1] category
2866753c7e08SAugustin Cavalier 		 *   [1] action
2867753c7e08SAugustin Cavalier 		 *   [tlv] mesh id
2868753c7e08SAugustin Cavalier 		 *   [tlv] mesh peer link mgmt
2869753c7e08SAugustin Cavalier 		 */
2870753c7e08SAugustin Cavalier 		*frm++ = category;
2871753c7e08SAugustin Cavalier 		*frm++ = action;
2872753c7e08SAugustin Cavalier 		frm = ieee80211_add_meshid(frm, vap);
2873753c7e08SAugustin Cavalier 		frm = ieee80211_add_meshpeer(frm,
2874753c7e08SAugustin Cavalier 		    IEEE80211_ACTION_MESHPEERING_CLOSE,
2875753c7e08SAugustin Cavalier 		    args[0], args[1], args[2]);
2876753c7e08SAugustin Cavalier 		m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2877753c7e08SAugustin Cavalier 		return mesh_send_action(ni, vap->iv_myaddr, ni->ni_macaddr, m);
2878753c7e08SAugustin Cavalier 	} else {
2879753c7e08SAugustin Cavalier 		vap->iv_stats.is_tx_nobuf++;
2880753c7e08SAugustin Cavalier 		ieee80211_free_node(ni);
2881753c7e08SAugustin Cavalier 		return ENOMEM;
2882753c7e08SAugustin Cavalier 	}
2883753c7e08SAugustin Cavalier }
2884753c7e08SAugustin Cavalier 
2885753c7e08SAugustin Cavalier static int
mesh_send_action_meshlmetric(struct ieee80211_node * ni,int category,int action,void * arg0)2886753c7e08SAugustin Cavalier mesh_send_action_meshlmetric(struct ieee80211_node *ni,
2887753c7e08SAugustin Cavalier 	int category, int action, void *arg0)
2888753c7e08SAugustin Cavalier {
2889753c7e08SAugustin Cavalier 	struct ieee80211vap *vap = ni->ni_vap;
2890753c7e08SAugustin Cavalier 	struct ieee80211com *ic = ni->ni_ic;
2891753c7e08SAugustin Cavalier 	struct ieee80211_meshlmetric_ie *ie = arg0;
2892753c7e08SAugustin Cavalier 	struct mbuf *m;
2893753c7e08SAugustin Cavalier 	uint8_t *frm;
2894753c7e08SAugustin Cavalier 
2895753c7e08SAugustin Cavalier 	if (ie->lm_flags & IEEE80211_MESH_LMETRIC_FLAGS_REQ) {
2896753c7e08SAugustin Cavalier 		IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH,
2897753c7e08SAugustin Cavalier 		    ni, "%s", "send LINK METRIC REQUEST action");
2898753c7e08SAugustin Cavalier 	} else {
2899753c7e08SAugustin Cavalier 		IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH,
2900753c7e08SAugustin Cavalier 		    ni, "send LINK METRIC REPLY action: metric 0x%x",
2901753c7e08SAugustin Cavalier 		    ie->lm_metric);
2902753c7e08SAugustin Cavalier 	}
2903753c7e08SAugustin Cavalier 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
2904753c7e08SAugustin Cavalier 	    "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", __func__, __LINE__,
2905753c7e08SAugustin Cavalier 	    ni, ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)+1);
2906753c7e08SAugustin Cavalier 	ieee80211_ref_node(ni);
2907753c7e08SAugustin Cavalier 
2908753c7e08SAugustin Cavalier 	m = ieee80211_getmgtframe(&frm,
2909753c7e08SAugustin Cavalier 	    ic->ic_headroom + sizeof(struct ieee80211_frame),
2910753c7e08SAugustin Cavalier 	    sizeof(uint16_t) +	/* action+category */
2911753c7e08SAugustin Cavalier 	    sizeof(struct ieee80211_meshlmetric_ie)
2912753c7e08SAugustin Cavalier 	);
2913753c7e08SAugustin Cavalier 	if (m != NULL) {
2914753c7e08SAugustin Cavalier 		/*
2915753c7e08SAugustin Cavalier 		 * mesh link metric
2916753c7e08SAugustin Cavalier 		 *   [1] category
2917753c7e08SAugustin Cavalier 		 *   [1] action
2918753c7e08SAugustin Cavalier 		 *   [tlv] mesh link metric
2919753c7e08SAugustin Cavalier 		 */
2920753c7e08SAugustin Cavalier 		*frm++ = category;
2921753c7e08SAugustin Cavalier 		*frm++ = action;
2922753c7e08SAugustin Cavalier 		frm = ieee80211_add_meshlmetric(frm,
2923753c7e08SAugustin Cavalier 		    ie->lm_flags, ie->lm_metric);
2924753c7e08SAugustin Cavalier 		m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2925753c7e08SAugustin Cavalier 		return mesh_send_action(ni, vap->iv_myaddr, ni->ni_macaddr, m);
2926753c7e08SAugustin Cavalier 	} else {
2927753c7e08SAugustin Cavalier 		vap->iv_stats.is_tx_nobuf++;
2928753c7e08SAugustin Cavalier 		ieee80211_free_node(ni);
2929753c7e08SAugustin Cavalier 		return ENOMEM;
2930753c7e08SAugustin Cavalier 	}
2931753c7e08SAugustin Cavalier }
2932753c7e08SAugustin Cavalier 
2933753c7e08SAugustin Cavalier static int
mesh_send_action_meshgate(struct ieee80211_node * ni,int category,int action,void * arg0)2934753c7e08SAugustin Cavalier mesh_send_action_meshgate(struct ieee80211_node *ni,
2935753c7e08SAugustin Cavalier 	int category, int action, void *arg0)
2936753c7e08SAugustin Cavalier {
2937753c7e08SAugustin Cavalier 	struct ieee80211vap *vap = ni->ni_vap;
2938753c7e08SAugustin Cavalier 	struct ieee80211com *ic = ni->ni_ic;
2939753c7e08SAugustin Cavalier 	struct ieee80211_meshgann_ie *ie = arg0;
2940753c7e08SAugustin Cavalier 	struct mbuf *m;
2941753c7e08SAugustin Cavalier 	uint8_t *frm;
2942753c7e08SAugustin Cavalier 
2943753c7e08SAugustin Cavalier 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
2944753c7e08SAugustin Cavalier 	    "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", __func__, __LINE__,
2945753c7e08SAugustin Cavalier 	    ni, ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)+1);
2946753c7e08SAugustin Cavalier 	ieee80211_ref_node(ni);
2947753c7e08SAugustin Cavalier 
2948753c7e08SAugustin Cavalier 	m = ieee80211_getmgtframe(&frm,
2949753c7e08SAugustin Cavalier 	    ic->ic_headroom + sizeof(struct ieee80211_frame),
2950753c7e08SAugustin Cavalier 	    sizeof(uint16_t) +	/* action+category */
2951753c7e08SAugustin Cavalier 	    IEEE80211_MESHGANN_BASE_SZ
2952753c7e08SAugustin Cavalier 	);
2953753c7e08SAugustin Cavalier 	if (m != NULL) {
2954753c7e08SAugustin Cavalier 		/*
2955753c7e08SAugustin Cavalier 		 * mesh link metric
2956753c7e08SAugustin Cavalier 		 *   [1] category
2957753c7e08SAugustin Cavalier 		 *   [1] action
295886021fd4SAugustin Cavalier 		 *   [tlv] mesh gate announcement
2959753c7e08SAugustin Cavalier 		 */
2960753c7e08SAugustin Cavalier 		*frm++ = category;
2961753c7e08SAugustin Cavalier 		*frm++ = action;
2962753c7e08SAugustin Cavalier 		frm = ieee80211_add_meshgate(frm, ie);
2963753c7e08SAugustin Cavalier 		m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2964753c7e08SAugustin Cavalier 		return mesh_send_action(ni, vap->iv_myaddr, broadcastaddr, m);
2965753c7e08SAugustin Cavalier 	} else {
2966753c7e08SAugustin Cavalier 		vap->iv_stats.is_tx_nobuf++;
2967753c7e08SAugustin Cavalier 		ieee80211_free_node(ni);
2968753c7e08SAugustin Cavalier 		return ENOMEM;
2969753c7e08SAugustin Cavalier 	}
2970753c7e08SAugustin Cavalier }
2971753c7e08SAugustin Cavalier 
2972753c7e08SAugustin Cavalier static void
mesh_peer_timeout_setup(struct ieee80211_node * ni)2973753c7e08SAugustin Cavalier mesh_peer_timeout_setup(struct ieee80211_node *ni)
2974753c7e08SAugustin Cavalier {
2975753c7e08SAugustin Cavalier 	switch (ni->ni_mlstate) {
2976753c7e08SAugustin Cavalier 	case IEEE80211_NODE_MESH_HOLDING:
2977753c7e08SAugustin Cavalier 		ni->ni_mltval = ieee80211_mesh_holdingtimeout;
2978753c7e08SAugustin Cavalier 		break;
2979753c7e08SAugustin Cavalier 	case IEEE80211_NODE_MESH_CONFIRMRCV:
2980753c7e08SAugustin Cavalier 		ni->ni_mltval = ieee80211_mesh_confirmtimeout;
2981753c7e08SAugustin Cavalier 		break;
2982753c7e08SAugustin Cavalier 	case IEEE80211_NODE_MESH_IDLE:
2983753c7e08SAugustin Cavalier 		ni->ni_mltval = 0;
2984753c7e08SAugustin Cavalier 		break;
2985753c7e08SAugustin Cavalier 	default:
2986753c7e08SAugustin Cavalier 		ni->ni_mltval = ieee80211_mesh_retrytimeout;
2987753c7e08SAugustin Cavalier 		break;
2988753c7e08SAugustin Cavalier 	}
2989753c7e08SAugustin Cavalier 	if (ni->ni_mltval)
2990753c7e08SAugustin Cavalier 		callout_reset(&ni->ni_mltimer, ni->ni_mltval,
2991753c7e08SAugustin Cavalier 		    mesh_peer_timeout_cb, ni);
2992753c7e08SAugustin Cavalier }
2993753c7e08SAugustin Cavalier 
2994753c7e08SAugustin Cavalier /*
2995753c7e08SAugustin Cavalier  * Same as above but backoffs timer statisically 50%.
2996753c7e08SAugustin Cavalier  */
2997753c7e08SAugustin Cavalier static void
mesh_peer_timeout_backoff(struct ieee80211_node * ni)2998753c7e08SAugustin Cavalier mesh_peer_timeout_backoff(struct ieee80211_node *ni)
2999753c7e08SAugustin Cavalier {
3000753c7e08SAugustin Cavalier 	uint32_t r;
3001753c7e08SAugustin Cavalier 
3002753c7e08SAugustin Cavalier 	r = arc4random();
3003753c7e08SAugustin Cavalier 	ni->ni_mltval += r % ni->ni_mltval;
3004753c7e08SAugustin Cavalier 	callout_reset(&ni->ni_mltimer, ni->ni_mltval, mesh_peer_timeout_cb,
3005753c7e08SAugustin Cavalier 	    ni);
3006753c7e08SAugustin Cavalier }
3007753c7e08SAugustin Cavalier 
3008753c7e08SAugustin Cavalier static __inline void
mesh_peer_timeout_stop(struct ieee80211_node * ni)3009753c7e08SAugustin Cavalier mesh_peer_timeout_stop(struct ieee80211_node *ni)
3010753c7e08SAugustin Cavalier {
3011753c7e08SAugustin Cavalier 	callout_drain(&ni->ni_mltimer);
3012753c7e08SAugustin Cavalier }
3013753c7e08SAugustin Cavalier 
3014753c7e08SAugustin Cavalier static void
mesh_peer_backoff_cb(void * arg)3015753c7e08SAugustin Cavalier mesh_peer_backoff_cb(void *arg)
3016753c7e08SAugustin Cavalier {
3017753c7e08SAugustin Cavalier 	struct ieee80211_node *ni = (struct ieee80211_node *)arg;
3018753c7e08SAugustin Cavalier 
3019753c7e08SAugustin Cavalier 	/* After backoff timeout, try to peer automatically again. */
3020753c7e08SAugustin Cavalier 	ni->ni_mlhcnt = 0;
3021753c7e08SAugustin Cavalier }
3022753c7e08SAugustin Cavalier 
3023753c7e08SAugustin Cavalier /*
3024753c7e08SAugustin Cavalier  * Mesh Peer Link Management FSM timeout handling.
3025753c7e08SAugustin Cavalier  */
3026753c7e08SAugustin Cavalier static void
mesh_peer_timeout_cb(void * arg)3027753c7e08SAugustin Cavalier mesh_peer_timeout_cb(void *arg)
3028753c7e08SAugustin Cavalier {
3029753c7e08SAugustin Cavalier 	struct ieee80211_node *ni = (struct ieee80211_node *)arg;
3030753c7e08SAugustin Cavalier 	uint16_t args[3];
3031753c7e08SAugustin Cavalier 
3032753c7e08SAugustin Cavalier 	IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_MESH,
3033753c7e08SAugustin Cavalier 	    ni, "mesh link timeout, state %d, retry counter %d",
3034753c7e08SAugustin Cavalier 	    ni->ni_mlstate, ni->ni_mlrcnt);
3035753c7e08SAugustin Cavalier 
3036753c7e08SAugustin Cavalier 	switch (ni->ni_mlstate) {
3037753c7e08SAugustin Cavalier 	case IEEE80211_NODE_MESH_IDLE:
3038753c7e08SAugustin Cavalier 	case IEEE80211_NODE_MESH_ESTABLISHED:
3039753c7e08SAugustin Cavalier 		break;
3040753c7e08SAugustin Cavalier 	case IEEE80211_NODE_MESH_OPENSNT:
3041753c7e08SAugustin Cavalier 	case IEEE80211_NODE_MESH_OPENRCV:
3042753c7e08SAugustin Cavalier 		if (ni->ni_mlrcnt == ieee80211_mesh_maxretries) {
3043753c7e08SAugustin Cavalier 			args[0] = ni->ni_mlpid;
3044753c7e08SAugustin Cavalier 			args[2] = IEEE80211_REASON_MESH_MAX_RETRIES;
3045753c7e08SAugustin Cavalier 			ieee80211_send_action(ni,
3046753c7e08SAugustin Cavalier 			    IEEE80211_ACTION_CAT_SELF_PROT,
3047753c7e08SAugustin Cavalier 			    IEEE80211_ACTION_MESHPEERING_CLOSE, args);
3048753c7e08SAugustin Cavalier 			ni->ni_mlrcnt = 0;
3049753c7e08SAugustin Cavalier 			mesh_linkchange(ni, IEEE80211_NODE_MESH_HOLDING);
3050753c7e08SAugustin Cavalier 			mesh_peer_timeout_setup(ni);
3051753c7e08SAugustin Cavalier 		} else {
3052753c7e08SAugustin Cavalier 			args[0] = ni->ni_mlpid;
3053753c7e08SAugustin Cavalier 			ieee80211_send_action(ni,
3054753c7e08SAugustin Cavalier 			    IEEE80211_ACTION_CAT_SELF_PROT,
3055753c7e08SAugustin Cavalier 			    IEEE80211_ACTION_MESHPEERING_OPEN, args);
3056753c7e08SAugustin Cavalier 			ni->ni_mlrcnt++;
3057753c7e08SAugustin Cavalier 			mesh_peer_timeout_backoff(ni);
3058753c7e08SAugustin Cavalier 		}
3059753c7e08SAugustin Cavalier 		break;
3060753c7e08SAugustin Cavalier 	case IEEE80211_NODE_MESH_CONFIRMRCV:
3061753c7e08SAugustin Cavalier 		args[0] = ni->ni_mlpid;
3062753c7e08SAugustin Cavalier 		args[2] = IEEE80211_REASON_MESH_CONFIRM_TIMEOUT;
3063753c7e08SAugustin Cavalier 		ieee80211_send_action(ni,
3064753c7e08SAugustin Cavalier 		    IEEE80211_ACTION_CAT_SELF_PROT,
3065753c7e08SAugustin Cavalier 		    IEEE80211_ACTION_MESHPEERING_CLOSE, args);
3066753c7e08SAugustin Cavalier 		mesh_linkchange(ni, IEEE80211_NODE_MESH_HOLDING);
3067753c7e08SAugustin Cavalier 		mesh_peer_timeout_setup(ni);
3068753c7e08SAugustin Cavalier 		break;
3069753c7e08SAugustin Cavalier 	case IEEE80211_NODE_MESH_HOLDING:
3070753c7e08SAugustin Cavalier 		ni->ni_mlhcnt++;
3071753c7e08SAugustin Cavalier 		if (ni->ni_mlhcnt >= ieee80211_mesh_maxholding)
3072753c7e08SAugustin Cavalier 			callout_reset(&ni->ni_mlhtimer,
3073753c7e08SAugustin Cavalier 			    ieee80211_mesh_backofftimeout,
3074753c7e08SAugustin Cavalier 			    mesh_peer_backoff_cb, ni);
3075753c7e08SAugustin Cavalier 		mesh_linkchange(ni, IEEE80211_NODE_MESH_IDLE);
3076753c7e08SAugustin Cavalier 		break;
3077753c7e08SAugustin Cavalier 	}
3078753c7e08SAugustin Cavalier }
3079753c7e08SAugustin Cavalier 
3080753c7e08SAugustin Cavalier static int
mesh_verify_meshid(struct ieee80211vap * vap,const uint8_t * ie)3081753c7e08SAugustin Cavalier mesh_verify_meshid(struct ieee80211vap *vap, const uint8_t *ie)
3082753c7e08SAugustin Cavalier {
3083753c7e08SAugustin Cavalier 	struct ieee80211_mesh_state *ms = vap->iv_mesh;
3084753c7e08SAugustin Cavalier 
3085753c7e08SAugustin Cavalier 	if (ie == NULL || ie[1] != ms->ms_idlen)
3086753c7e08SAugustin Cavalier 		return 1;
3087753c7e08SAugustin Cavalier 	return memcmp(ms->ms_id, ie + 2, ms->ms_idlen);
3088753c7e08SAugustin Cavalier }
3089753c7e08SAugustin Cavalier 
3090753c7e08SAugustin Cavalier /*
3091753c7e08SAugustin Cavalier  * Check if we are using the same algorithms for this mesh.
3092753c7e08SAugustin Cavalier  */
3093753c7e08SAugustin Cavalier static int
mesh_verify_meshconf(struct ieee80211vap * vap,const uint8_t * ie)3094753c7e08SAugustin Cavalier mesh_verify_meshconf(struct ieee80211vap *vap, const uint8_t *ie)
3095753c7e08SAugustin Cavalier {
3096753c7e08SAugustin Cavalier 	const struct ieee80211_meshconf_ie *meshconf =
3097753c7e08SAugustin Cavalier 	    (const struct ieee80211_meshconf_ie *) ie;
3098753c7e08SAugustin Cavalier 	const struct ieee80211_mesh_state *ms = vap->iv_mesh;
3099753c7e08SAugustin Cavalier 
3100753c7e08SAugustin Cavalier 	if (meshconf == NULL)
3101753c7e08SAugustin Cavalier 		return 1;
3102753c7e08SAugustin Cavalier 	if (meshconf->conf_pselid != ms->ms_ppath->mpp_ie) {
3103753c7e08SAugustin Cavalier 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_MESH,
3104753c7e08SAugustin Cavalier 		    "unknown path selection algorithm: 0x%x\n",
3105753c7e08SAugustin Cavalier 		    meshconf->conf_pselid);
3106753c7e08SAugustin Cavalier 		return 1;
3107753c7e08SAugustin Cavalier 	}
3108753c7e08SAugustin Cavalier 	if (meshconf->conf_pmetid != ms->ms_pmetric->mpm_ie) {
3109753c7e08SAugustin Cavalier 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_MESH,
3110753c7e08SAugustin Cavalier 		    "unknown path metric algorithm: 0x%x\n",
3111753c7e08SAugustin Cavalier 		    meshconf->conf_pmetid);
3112753c7e08SAugustin Cavalier 		return 1;
3113753c7e08SAugustin Cavalier 	}
3114753c7e08SAugustin Cavalier 	if (meshconf->conf_ccid != 0) {
3115753c7e08SAugustin Cavalier 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_MESH,
3116753c7e08SAugustin Cavalier 		    "unknown congestion control algorithm: 0x%x\n",
3117753c7e08SAugustin Cavalier 		    meshconf->conf_ccid);
3118753c7e08SAugustin Cavalier 		return 1;
3119753c7e08SAugustin Cavalier 	}
3120753c7e08SAugustin Cavalier 	if (meshconf->conf_syncid != IEEE80211_MESHCONF_SYNC_NEIGHOFF) {
3121753c7e08SAugustin Cavalier 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_MESH,
3122753c7e08SAugustin Cavalier 		    "unknown sync algorithm: 0x%x\n",
3123753c7e08SAugustin Cavalier 		    meshconf->conf_syncid);
3124753c7e08SAugustin Cavalier 		return 1;
3125753c7e08SAugustin Cavalier 	}
3126753c7e08SAugustin Cavalier 	if (meshconf->conf_authid != 0) {
3127753c7e08SAugustin Cavalier 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_MESH,
3128753c7e08SAugustin Cavalier 		    "unknown auth auth algorithm: 0x%x\n",
3129753c7e08SAugustin Cavalier 		    meshconf->conf_pselid);
3130753c7e08SAugustin Cavalier 		return 1;
3131753c7e08SAugustin Cavalier 	}
3132753c7e08SAugustin Cavalier 	/* Not accepting peers */
3133753c7e08SAugustin Cavalier 	if (!(meshconf->conf_cap & IEEE80211_MESHCONF_CAP_AP)) {
3134753c7e08SAugustin Cavalier 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_MESH,
3135753c7e08SAugustin Cavalier 		    "not accepting peers: 0x%x\n", meshconf->conf_cap);
3136753c7e08SAugustin Cavalier 		return 1;
3137753c7e08SAugustin Cavalier 	}
3138753c7e08SAugustin Cavalier 	return 0;
3139753c7e08SAugustin Cavalier }
3140753c7e08SAugustin Cavalier 
3141753c7e08SAugustin Cavalier static int
mesh_verify_meshpeer(struct ieee80211vap * vap,uint8_t subtype,const uint8_t * ie)3142753c7e08SAugustin Cavalier mesh_verify_meshpeer(struct ieee80211vap *vap, uint8_t subtype,
3143753c7e08SAugustin Cavalier     const uint8_t *ie)
3144753c7e08SAugustin Cavalier {
3145753c7e08SAugustin Cavalier 	const struct ieee80211_meshpeer_ie *meshpeer =
3146753c7e08SAugustin Cavalier 	    (const struct ieee80211_meshpeer_ie *) ie;
3147753c7e08SAugustin Cavalier 
3148753c7e08SAugustin Cavalier 	if (meshpeer == NULL ||
3149753c7e08SAugustin Cavalier 	    meshpeer->peer_len < IEEE80211_MPM_BASE_SZ ||
3150753c7e08SAugustin Cavalier 	    meshpeer->peer_len > IEEE80211_MPM_MAX_SZ)
3151753c7e08SAugustin Cavalier 		return 1;
3152753c7e08SAugustin Cavalier 	if (meshpeer->peer_proto != IEEE80211_MPPID_MPM) {
3153753c7e08SAugustin Cavalier 		IEEE80211_DPRINTF(vap,
3154753c7e08SAugustin Cavalier 		    IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH,
3155753c7e08SAugustin Cavalier 		    "Only MPM protocol is supported (proto: 0x%02X)",
3156753c7e08SAugustin Cavalier 		    meshpeer->peer_proto);
3157753c7e08SAugustin Cavalier 		return 1;
3158753c7e08SAugustin Cavalier 	}
3159753c7e08SAugustin Cavalier 	switch (subtype) {
3160753c7e08SAugustin Cavalier 	case IEEE80211_ACTION_MESHPEERING_OPEN:
3161753c7e08SAugustin Cavalier 		if (meshpeer->peer_len != IEEE80211_MPM_BASE_SZ)
3162753c7e08SAugustin Cavalier 			return 1;
3163753c7e08SAugustin Cavalier 		break;
3164753c7e08SAugustin Cavalier 	case IEEE80211_ACTION_MESHPEERING_CONFIRM:
3165753c7e08SAugustin Cavalier 		if (meshpeer->peer_len != IEEE80211_MPM_BASE_SZ + 2)
3166753c7e08SAugustin Cavalier 			return 1;
3167753c7e08SAugustin Cavalier 		break;
3168753c7e08SAugustin Cavalier 	case IEEE80211_ACTION_MESHPEERING_CLOSE:
3169753c7e08SAugustin Cavalier 		if (meshpeer->peer_len < IEEE80211_MPM_BASE_SZ + 2)
3170753c7e08SAugustin Cavalier 			return 1;
3171753c7e08SAugustin Cavalier 		if (meshpeer->peer_len == (IEEE80211_MPM_BASE_SZ + 2) &&
3172753c7e08SAugustin Cavalier 		    meshpeer->peer_linkid != 0)
3173753c7e08SAugustin Cavalier 			return 1;
3174753c7e08SAugustin Cavalier 		if (meshpeer->peer_rcode == 0)
3175753c7e08SAugustin Cavalier 			return 1;
3176753c7e08SAugustin Cavalier 		break;
3177753c7e08SAugustin Cavalier 	}
3178753c7e08SAugustin Cavalier 	return 0;
3179753c7e08SAugustin Cavalier }
3180753c7e08SAugustin Cavalier 
3181753c7e08SAugustin Cavalier /*
3182753c7e08SAugustin Cavalier  * Add a Mesh ID IE to a frame.
3183753c7e08SAugustin Cavalier  */
3184753c7e08SAugustin Cavalier uint8_t *
ieee80211_add_meshid(uint8_t * frm,struct ieee80211vap * vap)3185753c7e08SAugustin Cavalier ieee80211_add_meshid(uint8_t *frm, struct ieee80211vap *vap)
3186753c7e08SAugustin Cavalier {
3187753c7e08SAugustin Cavalier 	struct ieee80211_mesh_state *ms = vap->iv_mesh;
3188753c7e08SAugustin Cavalier 
3189753c7e08SAugustin Cavalier 	KASSERT(vap->iv_opmode == IEEE80211_M_MBSS, ("not a mbss vap"));
3190753c7e08SAugustin Cavalier 
3191753c7e08SAugustin Cavalier 	*frm++ = IEEE80211_ELEMID_MESHID;
3192753c7e08SAugustin Cavalier 	*frm++ = ms->ms_idlen;
3193753c7e08SAugustin Cavalier 	memcpy(frm, ms->ms_id, ms->ms_idlen);
3194753c7e08SAugustin Cavalier 	return frm + ms->ms_idlen;
3195753c7e08SAugustin Cavalier }
3196753c7e08SAugustin Cavalier 
3197753c7e08SAugustin Cavalier /*
3198753c7e08SAugustin Cavalier  * Add a Mesh Configuration IE to a frame.
3199753c7e08SAugustin Cavalier  * For now just use HWMP routing, Airtime link metric, Null Congestion
3200753c7e08SAugustin Cavalier  * Signaling, Null Sync Protocol and Null Authentication.
3201753c7e08SAugustin Cavalier  */
3202753c7e08SAugustin Cavalier uint8_t *
ieee80211_add_meshconf(uint8_t * frm,struct ieee80211vap * vap)3203753c7e08SAugustin Cavalier ieee80211_add_meshconf(uint8_t *frm, struct ieee80211vap *vap)
3204753c7e08SAugustin Cavalier {
3205753c7e08SAugustin Cavalier 	const struct ieee80211_mesh_state *ms = vap->iv_mesh;
3206753c7e08SAugustin Cavalier 	uint16_t caps;
3207753c7e08SAugustin Cavalier 
3208753c7e08SAugustin Cavalier 	KASSERT(vap->iv_opmode == IEEE80211_M_MBSS, ("not a MBSS vap"));
3209753c7e08SAugustin Cavalier 
3210753c7e08SAugustin Cavalier 	*frm++ = IEEE80211_ELEMID_MESHCONF;
3211753c7e08SAugustin Cavalier 	*frm++ = IEEE80211_MESH_CONF_SZ;
3212753c7e08SAugustin Cavalier 	*frm++ = ms->ms_ppath->mpp_ie;		/* path selection */
3213753c7e08SAugustin Cavalier 	*frm++ = ms->ms_pmetric->mpm_ie;	/* link metric */
3214753c7e08SAugustin Cavalier 	*frm++ = IEEE80211_MESHCONF_CC_DISABLED;
3215753c7e08SAugustin Cavalier 	*frm++ = IEEE80211_MESHCONF_SYNC_NEIGHOFF;
3216753c7e08SAugustin Cavalier 	*frm++ = IEEE80211_MESHCONF_AUTH_DISABLED;
3217753c7e08SAugustin Cavalier 	/* NB: set the number of neighbors before the rest */
3218753c7e08SAugustin Cavalier 	*frm = (ms->ms_neighbors > IEEE80211_MESH_MAX_NEIGHBORS ?
3219753c7e08SAugustin Cavalier 	    IEEE80211_MESH_MAX_NEIGHBORS : ms->ms_neighbors) << 1;
3220753c7e08SAugustin Cavalier 	if (ms->ms_flags & IEEE80211_MESHFLAGS_GATE)
3221753c7e08SAugustin Cavalier 		*frm |= IEEE80211_MESHCONF_FORM_GATE;
3222753c7e08SAugustin Cavalier 	frm += 1;
3223753c7e08SAugustin Cavalier 	caps = 0;
3224753c7e08SAugustin Cavalier 	if (ms->ms_flags & IEEE80211_MESHFLAGS_AP)
3225753c7e08SAugustin Cavalier 		caps |= IEEE80211_MESHCONF_CAP_AP;
3226753c7e08SAugustin Cavalier 	if (ms->ms_flags & IEEE80211_MESHFLAGS_FWD)
3227753c7e08SAugustin Cavalier 		caps |= IEEE80211_MESHCONF_CAP_FWRD;
3228753c7e08SAugustin Cavalier 	*frm++ = caps;
3229753c7e08SAugustin Cavalier 	return frm;
3230753c7e08SAugustin Cavalier }
3231753c7e08SAugustin Cavalier 
3232753c7e08SAugustin Cavalier /*
3233753c7e08SAugustin Cavalier  * Add a Mesh Peer Management IE to a frame.
3234753c7e08SAugustin Cavalier  */
3235753c7e08SAugustin Cavalier uint8_t *
ieee80211_add_meshpeer(uint8_t * frm,uint8_t subtype,uint16_t localid,uint16_t peerid,uint16_t reason)3236753c7e08SAugustin Cavalier ieee80211_add_meshpeer(uint8_t *frm, uint8_t subtype, uint16_t localid,
3237753c7e08SAugustin Cavalier     uint16_t peerid, uint16_t reason)
3238753c7e08SAugustin Cavalier {
3239753c7e08SAugustin Cavalier 
3240753c7e08SAugustin Cavalier 	KASSERT(localid != 0, ("localid == 0"));
3241753c7e08SAugustin Cavalier 
3242753c7e08SAugustin Cavalier 	*frm++ = IEEE80211_ELEMID_MESHPEER;
3243753c7e08SAugustin Cavalier 	switch (subtype) {
3244753c7e08SAugustin Cavalier 	case IEEE80211_ACTION_MESHPEERING_OPEN:
3245753c7e08SAugustin Cavalier 		*frm++ = IEEE80211_MPM_BASE_SZ;		/* length */
3246753c7e08SAugustin Cavalier 		ADDSHORT(frm, IEEE80211_MPPID_MPM);	/* proto */
3247753c7e08SAugustin Cavalier 		ADDSHORT(frm, localid);			/* local ID */
3248753c7e08SAugustin Cavalier 		break;
3249753c7e08SAugustin Cavalier 	case IEEE80211_ACTION_MESHPEERING_CONFIRM:
3250753c7e08SAugustin Cavalier 		KASSERT(peerid != 0, ("sending peer confirm without peer id"));
3251753c7e08SAugustin Cavalier 		*frm++ = IEEE80211_MPM_BASE_SZ + 2;	/* length */
3252753c7e08SAugustin Cavalier 		ADDSHORT(frm, IEEE80211_MPPID_MPM);	/* proto */
3253753c7e08SAugustin Cavalier 		ADDSHORT(frm, localid);			/* local ID */
3254753c7e08SAugustin Cavalier 		ADDSHORT(frm, peerid);			/* peer ID */
3255753c7e08SAugustin Cavalier 		break;
3256753c7e08SAugustin Cavalier 	case IEEE80211_ACTION_MESHPEERING_CLOSE:
3257753c7e08SAugustin Cavalier 		if (peerid)
3258753c7e08SAugustin Cavalier 			*frm++ = IEEE80211_MPM_MAX_SZ;	/* length */
3259753c7e08SAugustin Cavalier 		else
3260753c7e08SAugustin Cavalier 			*frm++ = IEEE80211_MPM_BASE_SZ + 2; /* length */
3261753c7e08SAugustin Cavalier 		ADDSHORT(frm, IEEE80211_MPPID_MPM);	/* proto */
3262753c7e08SAugustin Cavalier 		ADDSHORT(frm, localid);	/* local ID */
3263753c7e08SAugustin Cavalier 		if (peerid)
3264753c7e08SAugustin Cavalier 			ADDSHORT(frm, peerid);	/* peer ID */
3265753c7e08SAugustin Cavalier 		ADDSHORT(frm, reason);
3266753c7e08SAugustin Cavalier 		break;
3267753c7e08SAugustin Cavalier 	}
3268753c7e08SAugustin Cavalier 	return frm;
3269753c7e08SAugustin Cavalier }
3270753c7e08SAugustin Cavalier 
3271753c7e08SAugustin Cavalier /*
3272753c7e08SAugustin Cavalier  * Compute an Airtime Link Metric for the link with this node.
3273753c7e08SAugustin Cavalier  *
3274753c7e08SAugustin Cavalier  * Based on Draft 3.0 spec (11B.10, p.149).
3275753c7e08SAugustin Cavalier  */
3276753c7e08SAugustin Cavalier /*
3277753c7e08SAugustin Cavalier  * Max 802.11s overhead.
3278753c7e08SAugustin Cavalier  */
3279753c7e08SAugustin Cavalier #define IEEE80211_MESH_MAXOVERHEAD \
3280753c7e08SAugustin Cavalier 	(sizeof(struct ieee80211_qosframe_addr4) \
3281753c7e08SAugustin Cavalier 	 + sizeof(struct ieee80211_meshcntl_ae10) \
3282753c7e08SAugustin Cavalier 	+ sizeof(struct llc) \
3283753c7e08SAugustin Cavalier 	+ IEEE80211_ADDR_LEN \
3284753c7e08SAugustin Cavalier 	+ IEEE80211_WEP_IVLEN \
3285753c7e08SAugustin Cavalier 	+ IEEE80211_WEP_KIDLEN \
3286753c7e08SAugustin Cavalier 	+ IEEE80211_WEP_CRCLEN \
3287753c7e08SAugustin Cavalier 	+ IEEE80211_WEP_MICLEN \
3288753c7e08SAugustin Cavalier 	+ IEEE80211_CRC_LEN)
3289753c7e08SAugustin Cavalier uint32_t
mesh_airtime_calc(struct ieee80211_node * ni)3290753c7e08SAugustin Cavalier mesh_airtime_calc(struct ieee80211_node *ni)
3291753c7e08SAugustin Cavalier {
3292753c7e08SAugustin Cavalier #define M_BITS 8
3293753c7e08SAugustin Cavalier #define S_FACTOR (2 * M_BITS)
3294753c7e08SAugustin Cavalier 	struct ieee80211com *ic = ni->ni_ic;
3295753c7e08SAugustin Cavalier 	struct ifnet *ifp = ni->ni_vap->iv_ifp;
3296753c7e08SAugustin Cavalier 	const static int nbits = 8192 << M_BITS;
3297753c7e08SAugustin Cavalier 	uint32_t overhead, rate, errrate;
3298753c7e08SAugustin Cavalier 	uint64_t res;
3299753c7e08SAugustin Cavalier 
3300753c7e08SAugustin Cavalier 	/* Time to transmit a frame */
3301753c7e08SAugustin Cavalier 	rate = ni->ni_txrate;
3302753c7e08SAugustin Cavalier 	overhead = ieee80211_compute_duration(ic->ic_rt,
3303753c7e08SAugustin Cavalier 	    ifp->if_mtu + IEEE80211_MESH_MAXOVERHEAD, rate, 0) << M_BITS;
3304753c7e08SAugustin Cavalier 	/* Error rate in percentage */
3305753c7e08SAugustin Cavalier 	/* XXX assuming small failures are ok */
3306753c7e08SAugustin Cavalier 	errrate = (((ifp->if_get_counter(ifp, IFCOUNTER_OERRORS) +
3307753c7e08SAugustin Cavalier 	    ifp->if_get_counter(ifp, IFCOUNTER_IERRORS)) / 100) << M_BITS)
3308753c7e08SAugustin Cavalier 	    / 100;
3309753c7e08SAugustin Cavalier 	res = (overhead + (nbits / rate)) *
3310753c7e08SAugustin Cavalier 	    ((1 << S_FACTOR) / ((1 << M_BITS) - errrate));
3311753c7e08SAugustin Cavalier 
3312753c7e08SAugustin Cavalier 	return (uint32_t)(res >> S_FACTOR);
3313753c7e08SAugustin Cavalier #undef M_BITS
3314753c7e08SAugustin Cavalier #undef S_FACTOR
3315753c7e08SAugustin Cavalier }
3316753c7e08SAugustin Cavalier 
3317753c7e08SAugustin Cavalier /*
3318753c7e08SAugustin Cavalier  * Add a Mesh Link Metric report IE to a frame.
3319753c7e08SAugustin Cavalier  */
3320753c7e08SAugustin Cavalier uint8_t *
ieee80211_add_meshlmetric(uint8_t * frm,uint8_t flags,uint32_t metric)3321753c7e08SAugustin Cavalier ieee80211_add_meshlmetric(uint8_t *frm, uint8_t flags, uint32_t metric)
3322753c7e08SAugustin Cavalier {
3323753c7e08SAugustin Cavalier 	*frm++ = IEEE80211_ELEMID_MESHLINK;
3324753c7e08SAugustin Cavalier 	*frm++ = 5;
3325753c7e08SAugustin Cavalier 	*frm++ = flags;
3326753c7e08SAugustin Cavalier 	ADDWORD(frm, metric);
3327753c7e08SAugustin Cavalier 	return frm;
3328753c7e08SAugustin Cavalier }
3329753c7e08SAugustin Cavalier 
3330753c7e08SAugustin Cavalier /*
3331753c7e08SAugustin Cavalier  * Add a Mesh Gate Announcement IE to a frame.
3332753c7e08SAugustin Cavalier  */
3333753c7e08SAugustin Cavalier uint8_t *
ieee80211_add_meshgate(uint8_t * frm,struct ieee80211_meshgann_ie * ie)3334753c7e08SAugustin Cavalier ieee80211_add_meshgate(uint8_t *frm, struct ieee80211_meshgann_ie *ie)
3335753c7e08SAugustin Cavalier {
3336753c7e08SAugustin Cavalier 	*frm++ = IEEE80211_ELEMID_MESHGANN; /* ie */
3337753c7e08SAugustin Cavalier 	*frm++ = IEEE80211_MESHGANN_BASE_SZ; /* len */
3338753c7e08SAugustin Cavalier 	*frm++ = ie->gann_flags;
3339753c7e08SAugustin Cavalier 	*frm++ = ie->gann_hopcount;
3340753c7e08SAugustin Cavalier 	*frm++ = ie->gann_ttl;
3341753c7e08SAugustin Cavalier 	IEEE80211_ADDR_COPY(frm, ie->gann_addr);
3342753c7e08SAugustin Cavalier 	frm += 6;
3343753c7e08SAugustin Cavalier 	ADDWORD(frm, ie->gann_seq);
3344753c7e08SAugustin Cavalier 	ADDSHORT(frm, ie->gann_interval);
3345753c7e08SAugustin Cavalier 	return frm;
3346753c7e08SAugustin Cavalier }
3347753c7e08SAugustin Cavalier #undef ADDSHORT
3348753c7e08SAugustin Cavalier #undef ADDWORD
3349753c7e08SAugustin Cavalier 
3350753c7e08SAugustin Cavalier /*
3351753c7e08SAugustin Cavalier  * Initialize any mesh-specific node state.
3352753c7e08SAugustin Cavalier  */
3353753c7e08SAugustin Cavalier void
ieee80211_mesh_node_init(struct ieee80211vap * vap,struct ieee80211_node * ni)3354753c7e08SAugustin Cavalier ieee80211_mesh_node_init(struct ieee80211vap *vap, struct ieee80211_node *ni)
3355753c7e08SAugustin Cavalier {
3356753c7e08SAugustin Cavalier 	ni->ni_flags |= IEEE80211_NODE_QOS;
3357753c7e08SAugustin Cavalier 	callout_init(&ni->ni_mltimer, 1);
3358753c7e08SAugustin Cavalier 	callout_init(&ni->ni_mlhtimer, 1);
3359753c7e08SAugustin Cavalier }
3360753c7e08SAugustin Cavalier 
3361753c7e08SAugustin Cavalier /*
3362753c7e08SAugustin Cavalier  * Cleanup any mesh-specific node state.
3363753c7e08SAugustin Cavalier  */
3364753c7e08SAugustin Cavalier void
ieee80211_mesh_node_cleanup(struct ieee80211_node * ni)3365753c7e08SAugustin Cavalier ieee80211_mesh_node_cleanup(struct ieee80211_node *ni)
3366753c7e08SAugustin Cavalier {
3367753c7e08SAugustin Cavalier 	struct ieee80211vap *vap = ni->ni_vap;
3368753c7e08SAugustin Cavalier 	struct ieee80211_mesh_state *ms = vap->iv_mesh;
3369753c7e08SAugustin Cavalier 
3370753c7e08SAugustin Cavalier 	callout_drain(&ni->ni_mltimer);
3371753c7e08SAugustin Cavalier 	callout_drain(&ni->ni_mlhtimer);
3372753c7e08SAugustin Cavalier 	/* NB: short-circuit callbacks after mesh_vdetach */
3373753c7e08SAugustin Cavalier 	if (vap->iv_mesh != NULL)
3374753c7e08SAugustin Cavalier 		ms->ms_ppath->mpp_peerdown(ni);
3375753c7e08SAugustin Cavalier }
3376753c7e08SAugustin Cavalier 
3377753c7e08SAugustin Cavalier void
ieee80211_parse_meshid(struct ieee80211_node * ni,const uint8_t * ie)3378753c7e08SAugustin Cavalier ieee80211_parse_meshid(struct ieee80211_node *ni, const uint8_t *ie)
3379753c7e08SAugustin Cavalier {
3380753c7e08SAugustin Cavalier 	ni->ni_meshidlen = ie[1];
3381753c7e08SAugustin Cavalier 	memcpy(ni->ni_meshid, ie + 2, ie[1]);
3382753c7e08SAugustin Cavalier }
3383753c7e08SAugustin Cavalier 
3384753c7e08SAugustin Cavalier /*
3385753c7e08SAugustin Cavalier  * Setup mesh-specific node state on neighbor discovery.
3386753c7e08SAugustin Cavalier  */
3387753c7e08SAugustin Cavalier void
ieee80211_mesh_init_neighbor(struct ieee80211_node * ni,const struct ieee80211_frame * wh,const struct ieee80211_scanparams * sp)3388753c7e08SAugustin Cavalier ieee80211_mesh_init_neighbor(struct ieee80211_node *ni,
3389753c7e08SAugustin Cavalier 	const struct ieee80211_frame *wh,
3390753c7e08SAugustin Cavalier 	const struct ieee80211_scanparams *sp)
3391753c7e08SAugustin Cavalier {
3392753c7e08SAugustin Cavalier 	ieee80211_parse_meshid(ni, sp->meshid);
3393753c7e08SAugustin Cavalier }
3394753c7e08SAugustin Cavalier 
3395753c7e08SAugustin Cavalier void
ieee80211_mesh_update_beacon(struct ieee80211vap * vap,struct ieee80211_beacon_offsets * bo)3396753c7e08SAugustin Cavalier ieee80211_mesh_update_beacon(struct ieee80211vap *vap,
3397753c7e08SAugustin Cavalier 	struct ieee80211_beacon_offsets *bo)
3398753c7e08SAugustin Cavalier {
3399753c7e08SAugustin Cavalier 	KASSERT(vap->iv_opmode == IEEE80211_M_MBSS, ("not a MBSS vap"));
3400753c7e08SAugustin Cavalier 
3401753c7e08SAugustin Cavalier 	if (isset(bo->bo_flags, IEEE80211_BEACON_MESHCONF)) {
3402753c7e08SAugustin Cavalier 		(void)ieee80211_add_meshconf(bo->bo_meshconf, vap);
3403753c7e08SAugustin Cavalier 		clrbit(bo->bo_flags, IEEE80211_BEACON_MESHCONF);
3404753c7e08SAugustin Cavalier 	}
3405753c7e08SAugustin Cavalier }
3406753c7e08SAugustin Cavalier 
3407753c7e08SAugustin Cavalier static int
mesh_ioctl_get80211(struct ieee80211vap * vap,struct ieee80211req * ireq)3408753c7e08SAugustin Cavalier mesh_ioctl_get80211(struct ieee80211vap *vap, struct ieee80211req *ireq)
3409753c7e08SAugustin Cavalier {
3410753c7e08SAugustin Cavalier 	struct ieee80211_mesh_state *ms = vap->iv_mesh;
3411753c7e08SAugustin Cavalier 	uint8_t tmpmeshid[IEEE80211_NWID_LEN];
3412753c7e08SAugustin Cavalier 	struct ieee80211_mesh_route *rt;
3413753c7e08SAugustin Cavalier 	struct ieee80211req_mesh_route *imr;
3414753c7e08SAugustin Cavalier 	size_t len, off;
3415753c7e08SAugustin Cavalier 	uint8_t *p;
3416753c7e08SAugustin Cavalier 	int error;
3417753c7e08SAugustin Cavalier 
3418753c7e08SAugustin Cavalier 	if (vap->iv_opmode != IEEE80211_M_MBSS)
3419753c7e08SAugustin Cavalier 		return ENOSYS;
3420753c7e08SAugustin Cavalier 
3421753c7e08SAugustin Cavalier 	error = 0;
3422753c7e08SAugustin Cavalier 	switch (ireq->i_type) {
3423753c7e08SAugustin Cavalier 	case IEEE80211_IOC_MESH_ID:
3424753c7e08SAugustin Cavalier 		ireq->i_len = ms->ms_idlen;
3425753c7e08SAugustin Cavalier 		memcpy(tmpmeshid, ms->ms_id, ireq->i_len);
3426753c7e08SAugustin Cavalier 		error = copyout(tmpmeshid, ireq->i_data, ireq->i_len);
3427753c7e08SAugustin Cavalier 		break;
3428753c7e08SAugustin Cavalier 	case IEEE80211_IOC_MESH_AP:
3429753c7e08SAugustin Cavalier 		ireq->i_val = (ms->ms_flags & IEEE80211_MESHFLAGS_AP) != 0;
3430753c7e08SAugustin Cavalier 		break;
3431753c7e08SAugustin Cavalier 	case IEEE80211_IOC_MESH_FWRD:
3432753c7e08SAugustin Cavalier 		ireq->i_val = (ms->ms_flags & IEEE80211_MESHFLAGS_FWD) != 0;
3433753c7e08SAugustin Cavalier 		break;
3434753c7e08SAugustin Cavalier 	case IEEE80211_IOC_MESH_GATE:
3435753c7e08SAugustin Cavalier 		ireq->i_val = (ms->ms_flags & IEEE80211_MESHFLAGS_GATE) != 0;
3436753c7e08SAugustin Cavalier 		break;
3437753c7e08SAugustin Cavalier 	case IEEE80211_IOC_MESH_TTL:
3438753c7e08SAugustin Cavalier 		ireq->i_val = ms->ms_ttl;
3439753c7e08SAugustin Cavalier 		break;
3440753c7e08SAugustin Cavalier 	case IEEE80211_IOC_MESH_RTCMD:
3441753c7e08SAugustin Cavalier 		switch (ireq->i_val) {
3442753c7e08SAugustin Cavalier 		case IEEE80211_MESH_RTCMD_LIST:
3443753c7e08SAugustin Cavalier 			len = 0;
3444753c7e08SAugustin Cavalier 			MESH_RT_LOCK(ms);
3445753c7e08SAugustin Cavalier 			TAILQ_FOREACH(rt, &ms->ms_routes, rt_next) {
3446753c7e08SAugustin Cavalier 				len += sizeof(*imr);
3447753c7e08SAugustin Cavalier 			}
3448753c7e08SAugustin Cavalier 			MESH_RT_UNLOCK(ms);
3449753c7e08SAugustin Cavalier 			if (len > ireq->i_len || ireq->i_len < sizeof(*imr)) {
3450753c7e08SAugustin Cavalier 				ireq->i_len = len;
3451753c7e08SAugustin Cavalier 				return ENOMEM;
3452753c7e08SAugustin Cavalier 			}
3453753c7e08SAugustin Cavalier 			ireq->i_len = len;
3454753c7e08SAugustin Cavalier 			/* XXX M_WAIT? */
3455753c7e08SAugustin Cavalier 			p = IEEE80211_MALLOC(len, M_TEMP,
3456753c7e08SAugustin Cavalier 			    IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
3457753c7e08SAugustin Cavalier 			if (p == NULL)
3458753c7e08SAugustin Cavalier 				return ENOMEM;
3459753c7e08SAugustin Cavalier 			off = 0;
3460753c7e08SAugustin Cavalier 			MESH_RT_LOCK(ms);
3461753c7e08SAugustin Cavalier 			TAILQ_FOREACH(rt, &ms->ms_routes, rt_next) {
3462753c7e08SAugustin Cavalier 				if (off >= len)
3463753c7e08SAugustin Cavalier 					break;
3464753c7e08SAugustin Cavalier 				imr = (struct ieee80211req_mesh_route *)
3465753c7e08SAugustin Cavalier 				    (p + off);
3466753c7e08SAugustin Cavalier 				IEEE80211_ADDR_COPY(imr->imr_dest,
3467753c7e08SAugustin Cavalier 				    rt->rt_dest);
3468753c7e08SAugustin Cavalier 				IEEE80211_ADDR_COPY(imr->imr_nexthop,
3469753c7e08SAugustin Cavalier 				    rt->rt_nexthop);
3470753c7e08SAugustin Cavalier 				imr->imr_metric = rt->rt_metric;
3471753c7e08SAugustin Cavalier 				imr->imr_nhops = rt->rt_nhops;
3472753c7e08SAugustin Cavalier 				imr->imr_lifetime =
3473753c7e08SAugustin Cavalier 				    ieee80211_mesh_rt_update(rt, 0);
3474753c7e08SAugustin Cavalier 				imr->imr_lastmseq = rt->rt_lastmseq;
3475753c7e08SAugustin Cavalier 				imr->imr_flags = rt->rt_flags; /* last */
3476753c7e08SAugustin Cavalier 				off += sizeof(*imr);
3477753c7e08SAugustin Cavalier 			}
3478753c7e08SAugustin Cavalier 			MESH_RT_UNLOCK(ms);
3479753c7e08SAugustin Cavalier 			error = copyout(p, (uint8_t *)ireq->i_data,
3480753c7e08SAugustin Cavalier 			    ireq->i_len);
3481753c7e08SAugustin Cavalier 			IEEE80211_FREE(p, M_TEMP);
3482753c7e08SAugustin Cavalier 			break;
3483753c7e08SAugustin Cavalier 		case IEEE80211_MESH_RTCMD_FLUSH:
3484753c7e08SAugustin Cavalier 		case IEEE80211_MESH_RTCMD_ADD:
3485753c7e08SAugustin Cavalier 		case IEEE80211_MESH_RTCMD_DELETE:
3486753c7e08SAugustin Cavalier 			return EINVAL;
3487753c7e08SAugustin Cavalier 		default:
3488753c7e08SAugustin Cavalier 			return ENOSYS;
3489753c7e08SAugustin Cavalier 		}
3490753c7e08SAugustin Cavalier 		break;
3491753c7e08SAugustin Cavalier 	case IEEE80211_IOC_MESH_PR_METRIC:
3492753c7e08SAugustin Cavalier 		len = strlen(ms->ms_pmetric->mpm_descr);
3493753c7e08SAugustin Cavalier 		if (ireq->i_len < len)
3494753c7e08SAugustin Cavalier 			return EINVAL;
3495753c7e08SAugustin Cavalier 		ireq->i_len = len;
3496753c7e08SAugustin Cavalier 		error = copyout(ms->ms_pmetric->mpm_descr,
3497753c7e08SAugustin Cavalier 		    (uint8_t *)ireq->i_data, len);
3498753c7e08SAugustin Cavalier 		break;
3499753c7e08SAugustin Cavalier 	case IEEE80211_IOC_MESH_PR_PATH:
3500753c7e08SAugustin Cavalier 		len = strlen(ms->ms_ppath->mpp_descr);
3501753c7e08SAugustin Cavalier 		if (ireq->i_len < len)
3502753c7e08SAugustin Cavalier 			return EINVAL;
3503753c7e08SAugustin Cavalier 		ireq->i_len = len;
3504753c7e08SAugustin Cavalier 		error = copyout(ms->ms_ppath->mpp_descr,
3505753c7e08SAugustin Cavalier 		    (uint8_t *)ireq->i_data, len);
3506753c7e08SAugustin Cavalier 		break;
3507753c7e08SAugustin Cavalier 	default:
3508753c7e08SAugustin Cavalier 		return ENOSYS;
3509753c7e08SAugustin Cavalier 	}
3510753c7e08SAugustin Cavalier 
3511753c7e08SAugustin Cavalier 	return error;
3512753c7e08SAugustin Cavalier }
3513753c7e08SAugustin Cavalier IEEE80211_IOCTL_GET(mesh, mesh_ioctl_get80211);
3514753c7e08SAugustin Cavalier 
3515753c7e08SAugustin Cavalier static int
mesh_ioctl_set80211(struct ieee80211vap * vap,struct ieee80211req * ireq)3516753c7e08SAugustin Cavalier mesh_ioctl_set80211(struct ieee80211vap *vap, struct ieee80211req *ireq)
3517753c7e08SAugustin Cavalier {
3518753c7e08SAugustin Cavalier 	struct ieee80211_mesh_state *ms = vap->iv_mesh;
3519753c7e08SAugustin Cavalier 	uint8_t tmpmeshid[IEEE80211_NWID_LEN];
3520753c7e08SAugustin Cavalier 	uint8_t tmpaddr[IEEE80211_ADDR_LEN];
3521753c7e08SAugustin Cavalier 	char tmpproto[IEEE80211_MESH_PROTO_DSZ];
3522753c7e08SAugustin Cavalier 	int error;
3523753c7e08SAugustin Cavalier 
3524753c7e08SAugustin Cavalier 	if (vap->iv_opmode != IEEE80211_M_MBSS)
3525753c7e08SAugustin Cavalier 		return ENOSYS;
3526753c7e08SAugustin Cavalier 
3527753c7e08SAugustin Cavalier 	error = 0;
3528753c7e08SAugustin Cavalier 	switch (ireq->i_type) {
3529753c7e08SAugustin Cavalier 	case IEEE80211_IOC_MESH_ID:
3530753c7e08SAugustin Cavalier 		if (ireq->i_val != 0 || ireq->i_len > IEEE80211_MESHID_LEN)
3531753c7e08SAugustin Cavalier 			return EINVAL;
3532753c7e08SAugustin Cavalier 		error = copyin(ireq->i_data, tmpmeshid, ireq->i_len);
3533753c7e08SAugustin Cavalier 		if (error != 0)
3534753c7e08SAugustin Cavalier 			break;
3535753c7e08SAugustin Cavalier 		memset(ms->ms_id, 0, IEEE80211_NWID_LEN);
3536753c7e08SAugustin Cavalier 		ms->ms_idlen = ireq->i_len;
3537753c7e08SAugustin Cavalier 		memcpy(ms->ms_id, tmpmeshid, ireq->i_len);
3538753c7e08SAugustin Cavalier 		error = ENETRESET;
3539753c7e08SAugustin Cavalier 		break;
3540753c7e08SAugustin Cavalier 	case IEEE80211_IOC_MESH_AP:
3541753c7e08SAugustin Cavalier 		if (ireq->i_val)
3542753c7e08SAugustin Cavalier 			ms->ms_flags |= IEEE80211_MESHFLAGS_AP;
3543753c7e08SAugustin Cavalier 		else
3544753c7e08SAugustin Cavalier 			ms->ms_flags &= ~IEEE80211_MESHFLAGS_AP;
3545753c7e08SAugustin Cavalier 		error = ENETRESET;
3546753c7e08SAugustin Cavalier 		break;
3547753c7e08SAugustin Cavalier 	case IEEE80211_IOC_MESH_FWRD:
3548753c7e08SAugustin Cavalier 		if (ireq->i_val)
3549753c7e08SAugustin Cavalier 			ms->ms_flags |= IEEE80211_MESHFLAGS_FWD;
3550753c7e08SAugustin Cavalier 		else
3551753c7e08SAugustin Cavalier 			ms->ms_flags &= ~IEEE80211_MESHFLAGS_FWD;
3552753c7e08SAugustin Cavalier 		mesh_gatemode_setup(vap);
3553753c7e08SAugustin Cavalier 		break;
3554753c7e08SAugustin Cavalier 	case IEEE80211_IOC_MESH_GATE:
3555753c7e08SAugustin Cavalier 		if (ireq->i_val)
3556753c7e08SAugustin Cavalier 			ms->ms_flags |= IEEE80211_MESHFLAGS_GATE;
3557753c7e08SAugustin Cavalier 		else
3558753c7e08SAugustin Cavalier 			ms->ms_flags &= ~IEEE80211_MESHFLAGS_GATE;
3559753c7e08SAugustin Cavalier 		break;
3560753c7e08SAugustin Cavalier 	case IEEE80211_IOC_MESH_TTL:
3561753c7e08SAugustin Cavalier 		ms->ms_ttl = (uint8_t) ireq->i_val;
3562753c7e08SAugustin Cavalier 		break;
3563753c7e08SAugustin Cavalier 	case IEEE80211_IOC_MESH_RTCMD:
3564753c7e08SAugustin Cavalier 		switch (ireq->i_val) {
3565753c7e08SAugustin Cavalier 		case IEEE80211_MESH_RTCMD_LIST:
3566753c7e08SAugustin Cavalier 			return EINVAL;
3567753c7e08SAugustin Cavalier 		case IEEE80211_MESH_RTCMD_FLUSH:
3568753c7e08SAugustin Cavalier 			ieee80211_mesh_rt_flush(vap);
3569753c7e08SAugustin Cavalier 			break;
3570753c7e08SAugustin Cavalier 		case IEEE80211_MESH_RTCMD_ADD:
35715cad57c4SJérôme Duval 			error = copyin(ireq->i_data, tmpaddr,
357200ba3fd0SJérôme Duval 			    IEEE80211_ADDR_LEN);
35735cad57c4SJérôme Duval 			if (error != 0)
35745cad57c4SJérôme Duval 				break;
35755cad57c4SJérôme Duval 			if (IEEE80211_ADDR_EQ(vap->iv_myaddr, tmpaddr) ||
35765cad57c4SJérôme Duval 			    IEEE80211_ADDR_EQ(broadcastaddr, tmpaddr))
35775cad57c4SJérôme Duval 				return EINVAL;
3578753c7e08SAugustin Cavalier 			ieee80211_mesh_discover(vap, tmpaddr, NULL);
3579753c7e08SAugustin Cavalier 			break;
3580753c7e08SAugustin Cavalier 		case IEEE80211_MESH_RTCMD_DELETE:
35815cad57c4SJérôme Duval 			error = copyin(ireq->i_data, tmpaddr,
35825cad57c4SJérôme Duval 			    IEEE80211_ADDR_LEN);
35835cad57c4SJérôme Duval 			if (error != 0)
35845cad57c4SJérôme Duval 				break;
35855cad57c4SJérôme Duval 			ieee80211_mesh_rt_del(vap, tmpaddr);
3586753c7e08SAugustin Cavalier 			break;
3587753c7e08SAugustin Cavalier 		default:
3588753c7e08SAugustin Cavalier 			return ENOSYS;
3589753c7e08SAugustin Cavalier 		}
3590753c7e08SAugustin Cavalier 		break;
3591753c7e08SAugustin Cavalier 	case IEEE80211_IOC_MESH_PR_METRIC:
3592753c7e08SAugustin Cavalier 		error = copyin(ireq->i_data, tmpproto, sizeof(tmpproto));
3593753c7e08SAugustin Cavalier 		if (error == 0) {
3594753c7e08SAugustin Cavalier 			error = mesh_select_proto_metric(vap, tmpproto);
3595753c7e08SAugustin Cavalier 			if (error == 0)
3596753c7e08SAugustin Cavalier 				error = ENETRESET;
3597753c7e08SAugustin Cavalier 		}
3598753c7e08SAugustin Cavalier 		break;
3599753c7e08SAugustin Cavalier 	case IEEE80211_IOC_MESH_PR_PATH:
3600753c7e08SAugustin Cavalier 		error = copyin(ireq->i_data, tmpproto, sizeof(tmpproto));
3601753c7e08SAugustin Cavalier 		if (error == 0) {
3602753c7e08SAugustin Cavalier 			error = mesh_select_proto_path(vap, tmpproto);
3603753c7e08SAugustin Cavalier 			if (error == 0)
3604753c7e08SAugustin Cavalier 				error = ENETRESET;
3605753c7e08SAugustin Cavalier 		}
3606753c7e08SAugustin Cavalier 		break;
3607753c7e08SAugustin Cavalier 	default:
3608753c7e08SAugustin Cavalier 		return ENOSYS;
3609753c7e08SAugustin Cavalier 	}
3610753c7e08SAugustin Cavalier 	return error;
3611753c7e08SAugustin Cavalier }
3612753c7e08SAugustin Cavalier IEEE80211_IOCTL_SET(mesh, mesh_ioctl_set80211);
3613