xref: /freebsd/sys/net80211/ieee80211_mesh.c (revision b5df85a6fd6afa11023863293334d2cf8f788810)
159aa14a9SRui Paulo /*-
259aa14a9SRui Paulo  * Copyright (c) 2009 The FreeBSD Foundation
359aa14a9SRui Paulo  * All rights reserved.
459aa14a9SRui Paulo  *
559aa14a9SRui Paulo  * This software was developed by Rui Paulo under sponsorship from the
659aa14a9SRui Paulo  * FreeBSD Foundation.
759aa14a9SRui Paulo  *
859aa14a9SRui Paulo  * Redistribution and use in source and binary forms, with or without
959aa14a9SRui Paulo  * modification, are permitted provided that the following conditions
1059aa14a9SRui Paulo  * are met:
1159aa14a9SRui Paulo  * 1. Redistributions of source code must retain the above copyright
1259aa14a9SRui Paulo  *    notice, this list of conditions and the following disclaimer.
1359aa14a9SRui Paulo  * 2. Redistributions in binary form must reproduce the above copyright
1459aa14a9SRui Paulo  *    notice, this list of conditions and the following disclaimer in the
1559aa14a9SRui Paulo  *    documentation and/or other materials provided with the distribution.
1659aa14a9SRui Paulo  *
1759aa14a9SRui Paulo  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1859aa14a9SRui Paulo  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1959aa14a9SRui Paulo  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2059aa14a9SRui Paulo  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2159aa14a9SRui Paulo  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2259aa14a9SRui Paulo  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2359aa14a9SRui Paulo  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2459aa14a9SRui Paulo  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2559aa14a9SRui Paulo  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2659aa14a9SRui Paulo  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2759aa14a9SRui Paulo  * SUCH DAMAGE.
2859aa14a9SRui Paulo  */
2959aa14a9SRui Paulo #include <sys/cdefs.h>
3059aa14a9SRui Paulo #ifdef __FreeBSD__
3159aa14a9SRui Paulo __FBSDID("$FreeBSD$");
3259aa14a9SRui Paulo #endif
3359aa14a9SRui Paulo 
3459aa14a9SRui Paulo /*
3559aa14a9SRui Paulo  * IEEE 802.11s Mesh Point (MBSS) support.
3659aa14a9SRui Paulo  *
3759aa14a9SRui Paulo  * Based on March 2009, D3.0 802.11s draft spec.
3859aa14a9SRui Paulo  */
3959aa14a9SRui Paulo #include "opt_inet.h"
4059aa14a9SRui Paulo #include "opt_wlan.h"
4159aa14a9SRui Paulo 
4259aa14a9SRui Paulo #include <sys/param.h>
4359aa14a9SRui Paulo #include <sys/systm.h>
4459aa14a9SRui Paulo #include <sys/mbuf.h>
4559aa14a9SRui Paulo #include <sys/malloc.h>
4659aa14a9SRui Paulo #include <sys/kernel.h>
4759aa14a9SRui Paulo 
4859aa14a9SRui Paulo #include <sys/socket.h>
4959aa14a9SRui Paulo #include <sys/sockio.h>
5059aa14a9SRui Paulo #include <sys/endian.h>
5159aa14a9SRui Paulo #include <sys/errno.h>
5259aa14a9SRui Paulo #include <sys/proc.h>
5359aa14a9SRui Paulo #include <sys/sysctl.h>
5459aa14a9SRui Paulo 
5559aa14a9SRui Paulo #include <net/if.h>
5659aa14a9SRui Paulo #include <net/if_media.h>
5759aa14a9SRui Paulo #include <net/if_llc.h>
5859aa14a9SRui Paulo #include <net/ethernet.h>
5959aa14a9SRui Paulo 
6059aa14a9SRui Paulo #include <net80211/ieee80211_var.h>
6159aa14a9SRui Paulo #include <net80211/ieee80211_action.h>
6259aa14a9SRui Paulo #include <net80211/ieee80211_input.h>
6359aa14a9SRui Paulo #include <net80211/ieee80211_mesh.h>
6459aa14a9SRui Paulo 
65c104cff2SRui Paulo static void	mesh_rt_flush_invalid(struct ieee80211vap *);
6659aa14a9SRui Paulo static int	mesh_select_proto_path(struct ieee80211vap *, const char *);
6759aa14a9SRui Paulo static int	mesh_select_proto_metric(struct ieee80211vap *, const char *);
6859aa14a9SRui Paulo static void	mesh_vattach(struct ieee80211vap *);
6959aa14a9SRui Paulo static int	mesh_newstate(struct ieee80211vap *, enum ieee80211_state, int);
70c104cff2SRui Paulo static void	mesh_rt_cleanup_cb(void *);
71aec0289dSRui Paulo static void	mesh_linkchange(struct ieee80211_node *,
7259aa14a9SRui Paulo 		    enum ieee80211_mesh_mlstate);
7359aa14a9SRui Paulo static void	mesh_checkid(void *, struct ieee80211_node *);
7459aa14a9SRui Paulo static uint32_t	mesh_generateid(struct ieee80211vap *);
7559aa14a9SRui Paulo static int	mesh_checkpseq(struct ieee80211vap *,
7659aa14a9SRui Paulo 		    const uint8_t [IEEE80211_ADDR_LEN], uint32_t);
7759aa14a9SRui Paulo static struct ieee80211_node *
7859aa14a9SRui Paulo 		mesh_find_txnode(struct ieee80211vap *,
7959aa14a9SRui Paulo 		    const uint8_t [IEEE80211_ADDR_LEN]);
8059aa14a9SRui Paulo static void	mesh_forward(struct ieee80211vap *, struct mbuf *,
8159aa14a9SRui Paulo 		    const struct ieee80211_meshcntl *);
8259aa14a9SRui Paulo static int	mesh_input(struct ieee80211_node *, struct mbuf *, int, int);
8359aa14a9SRui Paulo static void	mesh_recv_mgmt(struct ieee80211_node *, struct mbuf *, int,
8459aa14a9SRui Paulo 		    int, int);
850917631fSRui Paulo static void	mesh_recv_ctl(struct ieee80211_node *, struct mbuf *, int);
8659aa14a9SRui Paulo static void	mesh_peer_timeout_setup(struct ieee80211_node *);
8759aa14a9SRui Paulo static void	mesh_peer_timeout_backoff(struct ieee80211_node *);
8859aa14a9SRui Paulo static void	mesh_peer_timeout_cb(void *);
8959aa14a9SRui Paulo static __inline void
9059aa14a9SRui Paulo 		mesh_peer_timeout_stop(struct ieee80211_node *);
9159aa14a9SRui Paulo static int	mesh_verify_meshid(struct ieee80211vap *, const uint8_t *);
9259aa14a9SRui Paulo static int	mesh_verify_meshconf(struct ieee80211vap *, const uint8_t *);
93c77735e2SRui Paulo static int	mesh_verify_meshpeer(struct ieee80211vap *, uint8_t,
94c77735e2SRui Paulo     		    const uint8_t *);
9559aa14a9SRui Paulo uint32_t	mesh_airtime_calc(struct ieee80211_node *);
9659aa14a9SRui Paulo 
9759aa14a9SRui Paulo /*
9859aa14a9SRui Paulo  * Timeout values come from the specification and are in milliseconds.
9959aa14a9SRui Paulo  */
1006472ac3dSEd Schouten static SYSCTL_NODE(_net_wlan, OID_AUTO, mesh, CTLFLAG_RD, 0,
10159aa14a9SRui Paulo     "IEEE 802.11s parameters");
10259aa14a9SRui Paulo static int ieee80211_mesh_retrytimeout = -1;
10359aa14a9SRui Paulo SYSCTL_PROC(_net_wlan_mesh, OID_AUTO, retrytimeout, CTLTYPE_INT | CTLFLAG_RW,
10459aa14a9SRui Paulo     &ieee80211_mesh_retrytimeout, 0, ieee80211_sysctl_msecs_ticks, "I",
10559aa14a9SRui Paulo     "Retry timeout (msec)");
10659aa14a9SRui Paulo static int ieee80211_mesh_holdingtimeout = -1;
10759aa14a9SRui Paulo SYSCTL_PROC(_net_wlan_mesh, OID_AUTO, holdingtimeout, CTLTYPE_INT | CTLFLAG_RW,
10859aa14a9SRui Paulo     &ieee80211_mesh_holdingtimeout, 0, ieee80211_sysctl_msecs_ticks, "I",
10959aa14a9SRui Paulo     "Holding state timeout (msec)");
11059aa14a9SRui Paulo static int ieee80211_mesh_confirmtimeout = -1;
11159aa14a9SRui Paulo SYSCTL_PROC(_net_wlan_mesh, OID_AUTO, confirmtimeout, CTLTYPE_INT | CTLFLAG_RW,
11259aa14a9SRui Paulo     &ieee80211_mesh_confirmtimeout, 0, ieee80211_sysctl_msecs_ticks, "I",
11359aa14a9SRui Paulo     "Confirm state timeout (msec)");
11459aa14a9SRui Paulo static int ieee80211_mesh_maxretries = 2;
11559aa14a9SRui Paulo SYSCTL_INT(_net_wlan_mesh, OID_AUTO, maxretries, CTLTYPE_INT | CTLFLAG_RW,
11659aa14a9SRui Paulo     &ieee80211_mesh_maxretries, 0,
11759aa14a9SRui Paulo     "Maximum retries during peer link establishment");
11859aa14a9SRui Paulo 
11959aa14a9SRui Paulo static const uint8_t broadcastaddr[IEEE80211_ADDR_LEN] =
12059aa14a9SRui Paulo 	{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
12159aa14a9SRui Paulo 
12259aa14a9SRui Paulo static	ieee80211_recv_action_func mesh_recv_action_meshpeering_open;
12359aa14a9SRui Paulo static	ieee80211_recv_action_func mesh_recv_action_meshpeering_confirm;
12459aa14a9SRui Paulo static	ieee80211_recv_action_func mesh_recv_action_meshpeering_close;
125bdd2a076SAdrian Chadd static	ieee80211_recv_action_func mesh_recv_action_meshlmetric;
12659aa14a9SRui Paulo 
12759aa14a9SRui Paulo static	ieee80211_send_action_func mesh_send_action_meshpeering_open;
12859aa14a9SRui Paulo static	ieee80211_send_action_func mesh_send_action_meshpeering_confirm;
12959aa14a9SRui Paulo static	ieee80211_send_action_func mesh_send_action_meshpeering_close;
130bdd2a076SAdrian Chadd static	ieee80211_send_action_func mesh_send_action_meshlmetric;
13159aa14a9SRui Paulo 
13259aa14a9SRui Paulo static const struct ieee80211_mesh_proto_metric mesh_metric_airtime = {
13359aa14a9SRui Paulo 	.mpm_descr	= "AIRTIME",
1346b8c1829SRui Paulo 	.mpm_ie		= IEEE80211_MESHCONF_METRIC_AIRTIME,
13559aa14a9SRui Paulo 	.mpm_metric	= mesh_airtime_calc,
13659aa14a9SRui Paulo };
13759aa14a9SRui Paulo 
13859aa14a9SRui Paulo static struct ieee80211_mesh_proto_path		mesh_proto_paths[4];
13959aa14a9SRui Paulo static struct ieee80211_mesh_proto_metric	mesh_proto_metrics[4];
14059aa14a9SRui Paulo 
141*b5df85a6SMonthadar Al Jaberi #define	RT_ENTRY_LOCK(rt)	mtx_lock(&(rt)->rt_lock)
142*b5df85a6SMonthadar Al Jaberi #define	RT_ENTRY_LOCK_ASSERT(rt) mtx_assert(&(rt)->rt_lock, MA_OWNED)
143*b5df85a6SMonthadar Al Jaberi #define	RT_ENTRY_UNLOCK(rt)	mtx_unlock(&(rt)->rt_lock)
144*b5df85a6SMonthadar Al Jaberi 
14559aa14a9SRui Paulo #define	MESH_RT_LOCK(ms)	mtx_lock(&(ms)->ms_rt_lock)
146c104cff2SRui Paulo #define	MESH_RT_LOCK_ASSERT(ms)	mtx_assert(&(ms)->ms_rt_lock, MA_OWNED)
14759aa14a9SRui Paulo #define	MESH_RT_UNLOCK(ms)	mtx_unlock(&(ms)->ms_rt_lock)
14859aa14a9SRui Paulo 
1491f88a92bSAdrian Chadd MALLOC_DEFINE(M_80211_MESH_PREQ, "80211preq", "802.11 MESH Path Request frame");
1501f88a92bSAdrian Chadd MALLOC_DEFINE(M_80211_MESH_PREP, "80211prep", "802.11 MESH Path Reply frame");
1511f88a92bSAdrian Chadd MALLOC_DEFINE(M_80211_MESH_PERR, "80211perr", "802.11 MESH Path Error frame");
1521f88a92bSAdrian Chadd 
153*b5df85a6SMonthadar Al Jaberi /* The longer one of the lifetime should be stored as new lifetime */
154*b5df85a6SMonthadar Al Jaberi #define MESH_ROUTE_LIFETIME_MAX(a, b)	(a > b ? a : b)
155*b5df85a6SMonthadar Al Jaberi 
15659aa14a9SRui Paulo MALLOC_DEFINE(M_80211_MESH_RT, "80211mesh", "802.11s routing table");
15759aa14a9SRui Paulo 
15859aa14a9SRui Paulo /*
15959aa14a9SRui Paulo  * Helper functions to manipulate the Mesh routing table.
16059aa14a9SRui Paulo  */
161c104cff2SRui Paulo 
162c104cff2SRui Paulo static struct ieee80211_mesh_route *
163c104cff2SRui Paulo mesh_rt_find_locked(struct ieee80211_mesh_state *ms,
164c104cff2SRui Paulo     const uint8_t dest[IEEE80211_ADDR_LEN])
165c104cff2SRui Paulo {
166c104cff2SRui Paulo 	struct ieee80211_mesh_route *rt;
167c104cff2SRui Paulo 
168c104cff2SRui Paulo 	MESH_RT_LOCK_ASSERT(ms);
169c104cff2SRui Paulo 
170c104cff2SRui Paulo 	TAILQ_FOREACH(rt, &ms->ms_routes, rt_next) {
171c104cff2SRui Paulo 		if (IEEE80211_ADDR_EQ(dest, rt->rt_dest))
172c104cff2SRui Paulo 			return rt;
173c104cff2SRui Paulo 	}
174c104cff2SRui Paulo 	return NULL;
175c104cff2SRui Paulo }
176c104cff2SRui Paulo 
177c104cff2SRui Paulo static struct ieee80211_mesh_route *
178c104cff2SRui Paulo mesh_rt_add_locked(struct ieee80211_mesh_state *ms,
179c104cff2SRui Paulo     const uint8_t dest[IEEE80211_ADDR_LEN])
180c104cff2SRui Paulo {
181c104cff2SRui Paulo 	struct ieee80211_mesh_route *rt;
182c104cff2SRui Paulo 
183c104cff2SRui Paulo 	KASSERT(!IEEE80211_ADDR_EQ(broadcastaddr, dest),
184c104cff2SRui Paulo 	    ("%s: adding broadcast to the routing table", __func__));
185c104cff2SRui Paulo 
186c104cff2SRui Paulo 	MESH_RT_LOCK_ASSERT(ms);
187c104cff2SRui Paulo 
188c104cff2SRui Paulo 	rt = malloc(ALIGN(sizeof(struct ieee80211_mesh_route)) +
189c104cff2SRui Paulo 	    ms->ms_ppath->mpp_privlen, M_80211_MESH_RT, M_NOWAIT | M_ZERO);
190c104cff2SRui Paulo 	if (rt != NULL) {
191c104cff2SRui Paulo 		IEEE80211_ADDR_COPY(rt->rt_dest, dest);
192c104cff2SRui Paulo 		rt->rt_priv = (void *)ALIGN(&rt[1]);
193*b5df85a6SMonthadar Al Jaberi 		mtx_init(&rt->rt_lock, "MBSS_RT", "802.11s route entry", MTX_DEF);
194*b5df85a6SMonthadar Al Jaberi 		rt->rt_updtime = ticks;	/* create time */
195c104cff2SRui Paulo 		TAILQ_INSERT_TAIL(&ms->ms_routes, rt, rt_next);
196c104cff2SRui Paulo 	}
197c104cff2SRui Paulo 	return rt;
198c104cff2SRui Paulo }
199c104cff2SRui Paulo 
20059aa14a9SRui Paulo struct ieee80211_mesh_route *
20159aa14a9SRui Paulo ieee80211_mesh_rt_find(struct ieee80211vap *vap,
20259aa14a9SRui Paulo     const uint8_t dest[IEEE80211_ADDR_LEN])
20359aa14a9SRui Paulo {
20459aa14a9SRui Paulo 	struct ieee80211_mesh_state *ms = vap->iv_mesh;
20559aa14a9SRui Paulo 	struct ieee80211_mesh_route *rt;
20659aa14a9SRui Paulo 
20759aa14a9SRui Paulo 	MESH_RT_LOCK(ms);
208c104cff2SRui Paulo 	rt = mesh_rt_find_locked(ms, dest);
20959aa14a9SRui Paulo 	MESH_RT_UNLOCK(ms);
21059aa14a9SRui Paulo 	return rt;
21159aa14a9SRui Paulo }
21259aa14a9SRui Paulo 
21359aa14a9SRui Paulo struct ieee80211_mesh_route *
21459aa14a9SRui Paulo ieee80211_mesh_rt_add(struct ieee80211vap *vap,
21559aa14a9SRui Paulo     const uint8_t dest[IEEE80211_ADDR_LEN])
21659aa14a9SRui Paulo {
21759aa14a9SRui Paulo 	struct ieee80211_mesh_state *ms = vap->iv_mesh;
21859aa14a9SRui Paulo 	struct ieee80211_mesh_route *rt;
21959aa14a9SRui Paulo 
22059aa14a9SRui Paulo 	KASSERT(ieee80211_mesh_rt_find(vap, dest) == NULL,
22159aa14a9SRui Paulo 	    ("%s: duplicate entry in the routing table", __func__));
22259aa14a9SRui Paulo 	KASSERT(!IEEE80211_ADDR_EQ(vap->iv_myaddr, dest),
22359aa14a9SRui Paulo 	    ("%s: adding self to the routing table", __func__));
22459aa14a9SRui Paulo 
22559aa14a9SRui Paulo 	MESH_RT_LOCK(ms);
226c104cff2SRui Paulo 	rt = mesh_rt_add_locked(ms, dest);
22759aa14a9SRui Paulo 	MESH_RT_UNLOCK(ms);
22859aa14a9SRui Paulo 	return rt;
22959aa14a9SRui Paulo }
23059aa14a9SRui Paulo 
231c104cff2SRui Paulo /*
232*b5df85a6SMonthadar Al Jaberi  * Update the route lifetime and returns the updated lifetime.
233*b5df85a6SMonthadar Al Jaberi  * If new_lifetime is zero and route is timedout it will be invalidated.
234*b5df85a6SMonthadar Al Jaberi  * new_lifetime is in msec
235*b5df85a6SMonthadar Al Jaberi  */
236*b5df85a6SMonthadar Al Jaberi int
237*b5df85a6SMonthadar Al Jaberi ieee80211_mesh_rt_update(struct ieee80211_mesh_route *rt, int new_lifetime)
238*b5df85a6SMonthadar Al Jaberi {
239*b5df85a6SMonthadar Al Jaberi 	int timesince, now;
240*b5df85a6SMonthadar Al Jaberi 	uint32_t lifetime = 0;
241*b5df85a6SMonthadar Al Jaberi 
242*b5df85a6SMonthadar Al Jaberi 	now = ticks;
243*b5df85a6SMonthadar Al Jaberi 	RT_ENTRY_LOCK(rt);
244*b5df85a6SMonthadar Al Jaberi 	timesince = ticks_to_msecs(now - rt->rt_updtime);
245*b5df85a6SMonthadar Al Jaberi 	rt->rt_updtime = now;
246*b5df85a6SMonthadar Al Jaberi 	if (timesince >= rt->rt_lifetime) {
247*b5df85a6SMonthadar Al Jaberi 		if (new_lifetime != 0) {
248*b5df85a6SMonthadar Al Jaberi 			rt->rt_lifetime = new_lifetime;
249*b5df85a6SMonthadar Al Jaberi 		}
250*b5df85a6SMonthadar Al Jaberi 		else {
251*b5df85a6SMonthadar Al Jaberi 			rt->rt_flags &= ~IEEE80211_MESHRT_FLAGS_VALID;
252*b5df85a6SMonthadar Al Jaberi 			rt->rt_lifetime = 0;
253*b5df85a6SMonthadar Al Jaberi 		}
254*b5df85a6SMonthadar Al Jaberi 	} else {
255*b5df85a6SMonthadar Al Jaberi 		/* update what is left of lifetime */
256*b5df85a6SMonthadar Al Jaberi 		rt->rt_lifetime = rt->rt_lifetime - timesince;
257*b5df85a6SMonthadar Al Jaberi 		rt->rt_lifetime  = MESH_ROUTE_LIFETIME_MAX(
258*b5df85a6SMonthadar Al Jaberi 			new_lifetime, rt->rt_lifetime);
259*b5df85a6SMonthadar Al Jaberi 	}
260*b5df85a6SMonthadar Al Jaberi 	lifetime = rt->rt_lifetime;
261*b5df85a6SMonthadar Al Jaberi 	RT_ENTRY_UNLOCK(rt);
262*b5df85a6SMonthadar Al Jaberi 
263*b5df85a6SMonthadar Al Jaberi 	return lifetime;
264*b5df85a6SMonthadar Al Jaberi }
265*b5df85a6SMonthadar Al Jaberi 
266*b5df85a6SMonthadar Al Jaberi /*
267c104cff2SRui Paulo  * Add a proxy route (as needed) for the specified destination.
268c104cff2SRui Paulo  */
269c104cff2SRui Paulo void
270c104cff2SRui Paulo ieee80211_mesh_proxy_check(struct ieee80211vap *vap,
271c104cff2SRui Paulo     const uint8_t dest[IEEE80211_ADDR_LEN])
272c104cff2SRui Paulo {
273c104cff2SRui Paulo 	struct ieee80211_mesh_state *ms = vap->iv_mesh;
274c104cff2SRui Paulo 	struct ieee80211_mesh_route *rt;
275c104cff2SRui Paulo 
276c104cff2SRui Paulo 	MESH_RT_LOCK(ms);
277c104cff2SRui Paulo 	rt = mesh_rt_find_locked(ms, dest);
278c104cff2SRui Paulo 	if (rt == NULL) {
279c104cff2SRui Paulo 		rt = mesh_rt_add_locked(ms, dest);
280c104cff2SRui Paulo 		if (rt == NULL) {
281c104cff2SRui Paulo 			IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH, dest,
282c104cff2SRui Paulo 			    "%s", "unable to add proxy entry");
283c104cff2SRui Paulo 			vap->iv_stats.is_mesh_rtaddfailed++;
284c104cff2SRui Paulo 		} else {
2853ca80f0dSRui Paulo 			IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH, dest,
2863ca80f0dSRui Paulo 			    "%s", "add proxy entry");
287c104cff2SRui Paulo 			IEEE80211_ADDR_COPY(rt->rt_nexthop, vap->iv_myaddr);
288c104cff2SRui Paulo 			rt->rt_flags |= IEEE80211_MESHRT_FLAGS_VALID
289c104cff2SRui Paulo 				     |  IEEE80211_MESHRT_FLAGS_PROXY;
290c104cff2SRui Paulo 		}
291c104cff2SRui Paulo 	/* XXX assert PROXY? */
292c104cff2SRui Paulo 	} else if ((rt->rt_flags & IEEE80211_MESHRT_FLAGS_VALID) == 0) {
293c104cff2SRui Paulo 		struct ieee80211com *ic = vap->iv_ic;
294c104cff2SRui Paulo 		/*
295c104cff2SRui Paulo 		 * Fix existing entry created by received frames from
296c104cff2SRui Paulo 		 * stations that have some memory of dest.  We also
297c104cff2SRui Paulo 		 * flush any frames held on the staging queue; delivering
298c104cff2SRui Paulo 		 * them is too much trouble right now.
299c104cff2SRui Paulo 		 */
300c104cff2SRui Paulo 		IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH, dest,
301c104cff2SRui Paulo 		    "%s", "fix proxy entry");
302c104cff2SRui Paulo 		IEEE80211_ADDR_COPY(rt->rt_nexthop, vap->iv_myaddr);
303c104cff2SRui Paulo 		rt->rt_flags |= IEEE80211_MESHRT_FLAGS_VALID
304c104cff2SRui Paulo 			     |  IEEE80211_MESHRT_FLAGS_PROXY;
305c104cff2SRui Paulo 		/* XXX belongs in hwmp */
306c104cff2SRui Paulo 		ieee80211_ageq_drain_node(&ic->ic_stageq,
307c104cff2SRui Paulo 		   (void *)(uintptr_t) ieee80211_mac_hash(ic, dest));
308c104cff2SRui Paulo 		/* XXX stat? */
309c104cff2SRui Paulo 	}
310c104cff2SRui Paulo 	MESH_RT_UNLOCK(ms);
311c104cff2SRui Paulo }
312c104cff2SRui Paulo 
313c104cff2SRui Paulo static __inline void
314c104cff2SRui Paulo mesh_rt_del(struct ieee80211_mesh_state *ms, struct ieee80211_mesh_route *rt)
315c104cff2SRui Paulo {
316c104cff2SRui Paulo 	TAILQ_REMOVE(&ms->ms_routes, rt, rt_next);
317*b5df85a6SMonthadar Al Jaberi 	/*
318*b5df85a6SMonthadar Al Jaberi 	 * Grab the lock before destroying it, to be sure no one else
319*b5df85a6SMonthadar Al Jaberi 	 * is holding the route.
320*b5df85a6SMonthadar Al Jaberi 	 */
321*b5df85a6SMonthadar Al Jaberi 	RT_ENTRY_LOCK(rt);
322*b5df85a6SMonthadar Al Jaberi 	mtx_destroy(&rt->rt_lock);
323c104cff2SRui Paulo 	free(rt, M_80211_MESH_RT);
324c104cff2SRui Paulo }
325c104cff2SRui Paulo 
32659aa14a9SRui Paulo void
32759aa14a9SRui Paulo ieee80211_mesh_rt_del(struct ieee80211vap *vap,
32859aa14a9SRui Paulo     const uint8_t dest[IEEE80211_ADDR_LEN])
32959aa14a9SRui Paulo {
33059aa14a9SRui Paulo 	struct ieee80211_mesh_state *ms = vap->iv_mesh;
33159aa14a9SRui Paulo 	struct ieee80211_mesh_route *rt, *next;
33259aa14a9SRui Paulo 
33359aa14a9SRui Paulo 	MESH_RT_LOCK(ms);
33459aa14a9SRui Paulo 	TAILQ_FOREACH_SAFE(rt, &ms->ms_routes, rt_next, next) {
33559aa14a9SRui Paulo 		if (IEEE80211_ADDR_EQ(rt->rt_dest, dest)) {
336c104cff2SRui Paulo 			mesh_rt_del(ms, rt);
33759aa14a9SRui Paulo 			MESH_RT_UNLOCK(ms);
33859aa14a9SRui Paulo 			return;
33959aa14a9SRui Paulo 		}
34059aa14a9SRui Paulo 	}
34159aa14a9SRui Paulo 	MESH_RT_UNLOCK(ms);
34259aa14a9SRui Paulo }
34359aa14a9SRui Paulo 
34459aa14a9SRui Paulo void
34559aa14a9SRui Paulo ieee80211_mesh_rt_flush(struct ieee80211vap *vap)
34659aa14a9SRui Paulo {
34759aa14a9SRui Paulo 	struct ieee80211_mesh_state *ms = vap->iv_mesh;
34859aa14a9SRui Paulo 	struct ieee80211_mesh_route *rt, *next;
34959aa14a9SRui Paulo 
35059aa14a9SRui Paulo 	if (ms == NULL)
35159aa14a9SRui Paulo 		return;
35259aa14a9SRui Paulo 	MESH_RT_LOCK(ms);
353c104cff2SRui Paulo 	TAILQ_FOREACH_SAFE(rt, &ms->ms_routes, rt_next, next)
354c104cff2SRui Paulo 		mesh_rt_del(ms, rt);
355c104cff2SRui Paulo 	MESH_RT_UNLOCK(ms);
356c104cff2SRui Paulo }
357c104cff2SRui Paulo 
3583ca80f0dSRui Paulo void
3593ca80f0dSRui Paulo ieee80211_mesh_rt_flush_peer(struct ieee80211vap *vap,
3603ca80f0dSRui Paulo     const uint8_t peer[IEEE80211_ADDR_LEN])
3613ca80f0dSRui Paulo {
3623ca80f0dSRui Paulo 	struct ieee80211_mesh_state *ms = vap->iv_mesh;
3633ca80f0dSRui Paulo 	struct ieee80211_mesh_route *rt, *next;
3643ca80f0dSRui Paulo 
3653ca80f0dSRui Paulo 	MESH_RT_LOCK(ms);
3663ca80f0dSRui Paulo 	TAILQ_FOREACH_SAFE(rt, &ms->ms_routes, rt_next, next) {
3673ca80f0dSRui Paulo 		if (IEEE80211_ADDR_EQ(rt->rt_nexthop, peer))
3683ca80f0dSRui Paulo 			mesh_rt_del(ms, rt);
3693ca80f0dSRui Paulo 	}
3703ca80f0dSRui Paulo 	MESH_RT_UNLOCK(ms);
3713ca80f0dSRui Paulo }
3723ca80f0dSRui Paulo 
373c104cff2SRui Paulo /*
374c104cff2SRui Paulo  * Flush expired routing entries, i.e. those in invalid state for
375c104cff2SRui Paulo  * some time.
376c104cff2SRui Paulo  */
377c104cff2SRui Paulo static void
378c104cff2SRui Paulo mesh_rt_flush_invalid(struct ieee80211vap *vap)
379c104cff2SRui Paulo {
380c104cff2SRui Paulo 	struct ieee80211_mesh_state *ms = vap->iv_mesh;
381c104cff2SRui Paulo 	struct ieee80211_mesh_route *rt, *next;
382c104cff2SRui Paulo 
383c104cff2SRui Paulo 	if (ms == NULL)
384c104cff2SRui Paulo 		return;
385c104cff2SRui Paulo 	MESH_RT_LOCK(ms);
38659aa14a9SRui Paulo 	TAILQ_FOREACH_SAFE(rt, &ms->ms_routes, rt_next, next) {
387*b5df85a6SMonthadar Al Jaberi 		ieee80211_mesh_rt_update(rt, 0);
388*b5df85a6SMonthadar Al Jaberi 		/*
389*b5df85a6SMonthadar Al Jaberi 		 * NB: we check for lifetime == 0 so that we give a chance
390*b5df85a6SMonthadar Al Jaberi 		 * for route discovery to complete.
391*b5df85a6SMonthadar Al Jaberi 		 */
392c104cff2SRui Paulo 		if ((rt->rt_flags & IEEE80211_MESHRT_FLAGS_VALID) == 0 &&
393*b5df85a6SMonthadar Al Jaberi 		    rt->rt_lifetime == 0)
394c104cff2SRui Paulo 			mesh_rt_del(ms, rt);
39559aa14a9SRui Paulo 	}
39659aa14a9SRui Paulo 	MESH_RT_UNLOCK(ms);
39759aa14a9SRui Paulo }
39859aa14a9SRui Paulo 
39959aa14a9SRui Paulo #define	N(a)	(sizeof(a) / sizeof(a[0]))
40059aa14a9SRui Paulo int
40159aa14a9SRui Paulo ieee80211_mesh_register_proto_path(const struct ieee80211_mesh_proto_path *mpp)
40259aa14a9SRui Paulo {
40359aa14a9SRui Paulo 	int i, firstempty = -1;
40459aa14a9SRui Paulo 
40559aa14a9SRui Paulo 	for (i = 0; i < N(mesh_proto_paths); i++) {
4066b8c1829SRui Paulo 		if (strncmp(mpp->mpp_descr, mesh_proto_paths[i].mpp_descr,
4076b8c1829SRui Paulo 		    IEEE80211_MESH_PROTO_DSZ) == 0)
40859aa14a9SRui Paulo 			return EEXIST;
4096b8c1829SRui Paulo 		if (!mesh_proto_paths[i].mpp_active && firstempty == -1)
41059aa14a9SRui Paulo 			firstempty = i;
41159aa14a9SRui Paulo 	}
41259aa14a9SRui Paulo 	if (firstempty < 0)
41359aa14a9SRui Paulo 		return ENOSPC;
41459aa14a9SRui Paulo 	memcpy(&mesh_proto_paths[firstempty], mpp, sizeof(*mpp));
4156b8c1829SRui Paulo 	mesh_proto_paths[firstempty].mpp_active = 1;
41659aa14a9SRui Paulo 	return 0;
41759aa14a9SRui Paulo }
41859aa14a9SRui Paulo 
41959aa14a9SRui Paulo int
42059aa14a9SRui Paulo ieee80211_mesh_register_proto_metric(const struct
42159aa14a9SRui Paulo     ieee80211_mesh_proto_metric *mpm)
42259aa14a9SRui Paulo {
42359aa14a9SRui Paulo 	int i, firstempty = -1;
42459aa14a9SRui Paulo 
42559aa14a9SRui Paulo 	for (i = 0; i < N(mesh_proto_metrics); i++) {
4266b8c1829SRui Paulo 		if (strncmp(mpm->mpm_descr, mesh_proto_metrics[i].mpm_descr,
4276b8c1829SRui Paulo 		    IEEE80211_MESH_PROTO_DSZ) == 0)
42859aa14a9SRui Paulo 			return EEXIST;
4296b8c1829SRui Paulo 		if (!mesh_proto_metrics[i].mpm_active && firstempty == -1)
43059aa14a9SRui Paulo 			firstempty = i;
43159aa14a9SRui Paulo 	}
43259aa14a9SRui Paulo 	if (firstempty < 0)
43359aa14a9SRui Paulo 		return ENOSPC;
43459aa14a9SRui Paulo 	memcpy(&mesh_proto_metrics[firstempty], mpm, sizeof(*mpm));
4356b8c1829SRui Paulo 	mesh_proto_metrics[firstempty].mpm_active = 1;
43659aa14a9SRui Paulo 	return 0;
43759aa14a9SRui Paulo }
43859aa14a9SRui Paulo 
43959aa14a9SRui Paulo static int
44059aa14a9SRui Paulo mesh_select_proto_path(struct ieee80211vap *vap, const char *name)
44159aa14a9SRui Paulo {
44259aa14a9SRui Paulo 	struct ieee80211_mesh_state *ms = vap->iv_mesh;
44359aa14a9SRui Paulo 	int i;
44459aa14a9SRui Paulo 
44559aa14a9SRui Paulo 	for (i = 0; i < N(mesh_proto_paths); i++) {
44659aa14a9SRui Paulo 		if (strcasecmp(mesh_proto_paths[i].mpp_descr, name) == 0) {
44759aa14a9SRui Paulo 			ms->ms_ppath = &mesh_proto_paths[i];
44859aa14a9SRui Paulo 			return 0;
44959aa14a9SRui Paulo 		}
45059aa14a9SRui Paulo 	}
45159aa14a9SRui Paulo 	return ENOENT;
45259aa14a9SRui Paulo }
45359aa14a9SRui Paulo 
45459aa14a9SRui Paulo static int
45559aa14a9SRui Paulo mesh_select_proto_metric(struct ieee80211vap *vap, const char *name)
45659aa14a9SRui Paulo {
45759aa14a9SRui Paulo 	struct ieee80211_mesh_state *ms = vap->iv_mesh;
45859aa14a9SRui Paulo 	int i;
45959aa14a9SRui Paulo 
46059aa14a9SRui Paulo 	for (i = 0; i < N(mesh_proto_metrics); i++) {
46159aa14a9SRui Paulo 		if (strcasecmp(mesh_proto_metrics[i].mpm_descr, name) == 0) {
46259aa14a9SRui Paulo 			ms->ms_pmetric = &mesh_proto_metrics[i];
46359aa14a9SRui Paulo 			return 0;
46459aa14a9SRui Paulo 		}
46559aa14a9SRui Paulo 	}
46659aa14a9SRui Paulo 	return ENOENT;
46759aa14a9SRui Paulo }
46859aa14a9SRui Paulo #undef	N
46959aa14a9SRui Paulo 
47059aa14a9SRui Paulo static void
47159aa14a9SRui Paulo ieee80211_mesh_init(void)
47259aa14a9SRui Paulo {
47359aa14a9SRui Paulo 
47459aa14a9SRui Paulo 	memset(mesh_proto_paths, 0, sizeof(mesh_proto_paths));
47559aa14a9SRui Paulo 	memset(mesh_proto_metrics, 0, sizeof(mesh_proto_metrics));
47659aa14a9SRui Paulo 
47759aa14a9SRui Paulo 	/*
47859aa14a9SRui Paulo 	 * Setup mesh parameters that depends on the clock frequency.
47959aa14a9SRui Paulo 	 */
48059aa14a9SRui Paulo 	ieee80211_mesh_retrytimeout = msecs_to_ticks(40);
48159aa14a9SRui Paulo 	ieee80211_mesh_holdingtimeout = msecs_to_ticks(40);
48259aa14a9SRui Paulo 	ieee80211_mesh_confirmtimeout = msecs_to_ticks(40);
48359aa14a9SRui Paulo 
48459aa14a9SRui Paulo 	/*
48559aa14a9SRui Paulo 	 * Register action frame handlers.
48659aa14a9SRui Paulo 	 */
487ebeaa1adSMonthadar Al Jaberi 	ieee80211_recv_action_register(IEEE80211_ACTION_CAT_SELF_PROT,
48859aa14a9SRui Paulo 	    IEEE80211_ACTION_MESHPEERING_OPEN,
48959aa14a9SRui Paulo 	    mesh_recv_action_meshpeering_open);
490ebeaa1adSMonthadar Al Jaberi 	ieee80211_recv_action_register(IEEE80211_ACTION_CAT_SELF_PROT,
49159aa14a9SRui Paulo 	    IEEE80211_ACTION_MESHPEERING_CONFIRM,
49259aa14a9SRui Paulo 	    mesh_recv_action_meshpeering_confirm);
493ebeaa1adSMonthadar Al Jaberi 	ieee80211_recv_action_register(IEEE80211_ACTION_CAT_SELF_PROT,
49459aa14a9SRui Paulo 	    IEEE80211_ACTION_MESHPEERING_CLOSE,
49559aa14a9SRui Paulo 	    mesh_recv_action_meshpeering_close);
496bdd2a076SAdrian Chadd 	ieee80211_recv_action_register(IEEE80211_ACTION_CAT_MESH,
497bdd2a076SAdrian Chadd 	    IEEE80211_ACTION_MESH_LMETRIC, mesh_recv_action_meshlmetric);
49859aa14a9SRui Paulo 
499ebeaa1adSMonthadar Al Jaberi 	ieee80211_send_action_register(IEEE80211_ACTION_CAT_SELF_PROT,
50059aa14a9SRui Paulo 	    IEEE80211_ACTION_MESHPEERING_OPEN,
50159aa14a9SRui Paulo 	    mesh_send_action_meshpeering_open);
502ebeaa1adSMonthadar Al Jaberi 	ieee80211_send_action_register(IEEE80211_ACTION_CAT_SELF_PROT,
50359aa14a9SRui Paulo 	    IEEE80211_ACTION_MESHPEERING_CONFIRM,
50459aa14a9SRui Paulo 	    mesh_send_action_meshpeering_confirm);
505ebeaa1adSMonthadar Al Jaberi 	ieee80211_send_action_register(IEEE80211_ACTION_CAT_SELF_PROT,
50659aa14a9SRui Paulo 	    IEEE80211_ACTION_MESHPEERING_CLOSE,
50759aa14a9SRui Paulo 	    mesh_send_action_meshpeering_close);
508bdd2a076SAdrian Chadd 	ieee80211_send_action_register(IEEE80211_ACTION_CAT_MESH,
509bdd2a076SAdrian Chadd 	    IEEE80211_ACTION_MESH_LMETRIC,
510bdd2a076SAdrian Chadd 	    mesh_send_action_meshlmetric);
51159aa14a9SRui Paulo 
51259aa14a9SRui Paulo 	/*
51359aa14a9SRui Paulo 	 * Register Airtime Link Metric.
51459aa14a9SRui Paulo 	 */
51559aa14a9SRui Paulo 	ieee80211_mesh_register_proto_metric(&mesh_metric_airtime);
51659aa14a9SRui Paulo 
51759aa14a9SRui Paulo }
51859aa14a9SRui Paulo SYSINIT(wlan_mesh, SI_SUB_DRIVERS, SI_ORDER_FIRST, ieee80211_mesh_init, NULL);
51959aa14a9SRui Paulo 
52059aa14a9SRui Paulo void
52159aa14a9SRui Paulo ieee80211_mesh_attach(struct ieee80211com *ic)
52259aa14a9SRui Paulo {
52359aa14a9SRui Paulo 	ic->ic_vattach[IEEE80211_M_MBSS] = mesh_vattach;
52459aa14a9SRui Paulo }
52559aa14a9SRui Paulo 
52659aa14a9SRui Paulo void
52759aa14a9SRui Paulo ieee80211_mesh_detach(struct ieee80211com *ic)
52859aa14a9SRui Paulo {
52959aa14a9SRui Paulo }
53059aa14a9SRui Paulo 
53159aa14a9SRui Paulo static void
53259aa14a9SRui Paulo mesh_vdetach_peers(void *arg, struct ieee80211_node *ni)
53359aa14a9SRui Paulo {
53459aa14a9SRui Paulo 	struct ieee80211com *ic = ni->ni_ic;
53559aa14a9SRui Paulo 	uint16_t args[3];
53659aa14a9SRui Paulo 
53759aa14a9SRui Paulo 	if (ni->ni_mlstate == IEEE80211_NODE_MESH_ESTABLISHED) {
53859aa14a9SRui Paulo 		args[0] = ni->ni_mlpid;
53959aa14a9SRui Paulo 		args[1] = ni->ni_mllid;
54059aa14a9SRui Paulo 		args[2] = IEEE80211_REASON_PEER_LINK_CANCELED;
54159aa14a9SRui Paulo 		ieee80211_send_action(ni,
542ebeaa1adSMonthadar Al Jaberi 		    IEEE80211_ACTION_CAT_SELF_PROT,
54359aa14a9SRui Paulo 		    IEEE80211_ACTION_MESHPEERING_CLOSE,
54459aa14a9SRui Paulo 		    args);
54559aa14a9SRui Paulo 	}
546c104cff2SRui Paulo 	callout_drain(&ni->ni_mltimer);
54759aa14a9SRui Paulo 	/* XXX belongs in hwmp */
54859aa14a9SRui Paulo 	ieee80211_ageq_drain_node(&ic->ic_stageq,
54959aa14a9SRui Paulo 	   (void *)(uintptr_t) ieee80211_mac_hash(ic, ni->ni_macaddr));
55059aa14a9SRui Paulo }
55159aa14a9SRui Paulo 
55259aa14a9SRui Paulo static void
55359aa14a9SRui Paulo mesh_vdetach(struct ieee80211vap *vap)
55459aa14a9SRui Paulo {
55559aa14a9SRui Paulo 	struct ieee80211_mesh_state *ms = vap->iv_mesh;
55659aa14a9SRui Paulo 
557c104cff2SRui Paulo 	callout_drain(&ms->ms_cleantimer);
55859aa14a9SRui Paulo 	ieee80211_iterate_nodes(&vap->iv_ic->ic_sta, mesh_vdetach_peers,
55959aa14a9SRui Paulo 	    NULL);
56059aa14a9SRui Paulo 	ieee80211_mesh_rt_flush(vap);
56159aa14a9SRui Paulo 	mtx_destroy(&ms->ms_rt_lock);
56259aa14a9SRui Paulo 	ms->ms_ppath->mpp_vdetach(vap);
56359aa14a9SRui Paulo 	free(vap->iv_mesh, M_80211_VAP);
56459aa14a9SRui Paulo 	vap->iv_mesh = NULL;
56559aa14a9SRui Paulo }
56659aa14a9SRui Paulo 
56759aa14a9SRui Paulo static void
56859aa14a9SRui Paulo mesh_vattach(struct ieee80211vap *vap)
56959aa14a9SRui Paulo {
57059aa14a9SRui Paulo 	struct ieee80211_mesh_state *ms;
57159aa14a9SRui Paulo 	vap->iv_newstate = mesh_newstate;
57259aa14a9SRui Paulo 	vap->iv_input = mesh_input;
57359aa14a9SRui Paulo 	vap->iv_opdetach = mesh_vdetach;
57459aa14a9SRui Paulo 	vap->iv_recv_mgmt = mesh_recv_mgmt;
5750917631fSRui Paulo 	vap->iv_recv_ctl = mesh_recv_ctl;
57659aa14a9SRui Paulo 	ms = malloc(sizeof(struct ieee80211_mesh_state), M_80211_VAP,
57759aa14a9SRui Paulo 	    M_NOWAIT | M_ZERO);
57859aa14a9SRui Paulo 	if (ms == NULL) {
57959aa14a9SRui Paulo 		printf("%s: couldn't alloc MBSS state\n", __func__);
58059aa14a9SRui Paulo 		return;
58159aa14a9SRui Paulo 	}
58259aa14a9SRui Paulo 	vap->iv_mesh = ms;
58359aa14a9SRui Paulo 	ms->ms_seq = 0;
58459aa14a9SRui Paulo 	ms->ms_flags = (IEEE80211_MESHFLAGS_AP | IEEE80211_MESHFLAGS_FWD);
58559aa14a9SRui Paulo 	ms->ms_ttl = IEEE80211_MESH_DEFAULT_TTL;
58659aa14a9SRui Paulo 	TAILQ_INIT(&ms->ms_routes);
58759aa14a9SRui Paulo 	mtx_init(&ms->ms_rt_lock, "MBSS", "802.11s routing table", MTX_DEF);
588c104cff2SRui Paulo 	callout_init(&ms->ms_cleantimer, CALLOUT_MPSAFE);
58959aa14a9SRui Paulo 	mesh_select_proto_metric(vap, "AIRTIME");
59059aa14a9SRui Paulo 	KASSERT(ms->ms_pmetric, ("ms_pmetric == NULL"));
59159aa14a9SRui Paulo 	mesh_select_proto_path(vap, "HWMP");
59259aa14a9SRui Paulo 	KASSERT(ms->ms_ppath, ("ms_ppath == NULL"));
59359aa14a9SRui Paulo 	ms->ms_ppath->mpp_vattach(vap);
59459aa14a9SRui Paulo }
59559aa14a9SRui Paulo 
59659aa14a9SRui Paulo /*
59759aa14a9SRui Paulo  * IEEE80211_M_MBSS vap state machine handler.
59859aa14a9SRui Paulo  */
59959aa14a9SRui Paulo static int
60059aa14a9SRui Paulo mesh_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
60159aa14a9SRui Paulo {
60259aa14a9SRui Paulo 	struct ieee80211_mesh_state *ms = vap->iv_mesh;
60359aa14a9SRui Paulo 	struct ieee80211com *ic = vap->iv_ic;
60459aa14a9SRui Paulo 	struct ieee80211_node *ni;
60559aa14a9SRui Paulo 	enum ieee80211_state ostate;
60659aa14a9SRui Paulo 
60759aa14a9SRui Paulo 	IEEE80211_LOCK_ASSERT(ic);
60859aa14a9SRui Paulo 
60959aa14a9SRui Paulo 	ostate = vap->iv_state;
61059aa14a9SRui Paulo 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE, "%s: %s -> %s (%d)\n",
61159aa14a9SRui Paulo 	    __func__, ieee80211_state_name[ostate],
61259aa14a9SRui Paulo 	    ieee80211_state_name[nstate], arg);
61359aa14a9SRui Paulo 	vap->iv_state = nstate;		/* state transition */
61459aa14a9SRui Paulo 	if (ostate != IEEE80211_S_SCAN)
61559aa14a9SRui Paulo 		ieee80211_cancel_scan(vap);	/* background scan */
61659aa14a9SRui Paulo 	ni = vap->iv_bss;			/* NB: no reference held */
617c104cff2SRui Paulo 	if (nstate != IEEE80211_S_RUN && ostate == IEEE80211_S_RUN)
618c104cff2SRui Paulo 		callout_drain(&ms->ms_cleantimer);
61959aa14a9SRui Paulo 	switch (nstate) {
62059aa14a9SRui Paulo 	case IEEE80211_S_INIT:
62159aa14a9SRui Paulo 		switch (ostate) {
62259aa14a9SRui Paulo 		case IEEE80211_S_SCAN:
62359aa14a9SRui Paulo 			ieee80211_cancel_scan(vap);
62459aa14a9SRui Paulo 			break;
62559aa14a9SRui Paulo 		case IEEE80211_S_CAC:
62659aa14a9SRui Paulo 			ieee80211_dfs_cac_stop(vap);
62759aa14a9SRui Paulo 			break;
62859aa14a9SRui Paulo 		case IEEE80211_S_RUN:
62959aa14a9SRui Paulo 			ieee80211_iterate_nodes(&ic->ic_sta,
63059aa14a9SRui Paulo 			    mesh_vdetach_peers, NULL);
63159aa14a9SRui Paulo 			break;
63259aa14a9SRui Paulo 		default:
63359aa14a9SRui Paulo 			break;
63459aa14a9SRui Paulo 		}
63559aa14a9SRui Paulo 		if (ostate != IEEE80211_S_INIT) {
63659aa14a9SRui Paulo 			/* NB: optimize INIT -> INIT case */
63759aa14a9SRui Paulo 			ieee80211_reset_bss(vap);
638c104cff2SRui Paulo 			ieee80211_mesh_rt_flush(vap);
63959aa14a9SRui Paulo 		}
64059aa14a9SRui Paulo 		break;
64159aa14a9SRui Paulo 	case IEEE80211_S_SCAN:
64259aa14a9SRui Paulo 		switch (ostate) {
64359aa14a9SRui Paulo 		case IEEE80211_S_INIT:
64459aa14a9SRui Paulo 			if (vap->iv_des_chan != IEEE80211_CHAN_ANYC &&
64559aa14a9SRui Paulo 			    !IEEE80211_IS_CHAN_RADAR(vap->iv_des_chan) &&
64659aa14a9SRui Paulo 			    ms->ms_idlen != 0) {
64759aa14a9SRui Paulo 				/*
64859aa14a9SRui Paulo 				 * Already have a channel and a mesh ID; bypass
64959aa14a9SRui Paulo 				 * the scan and startup immediately.
65059aa14a9SRui Paulo 				 */
65159aa14a9SRui Paulo 				ieee80211_create_ibss(vap, vap->iv_des_chan);
65259aa14a9SRui Paulo 				break;
65359aa14a9SRui Paulo 			}
65459aa14a9SRui Paulo 			/*
65559aa14a9SRui Paulo 			 * Initiate a scan.  We can come here as a result
65659aa14a9SRui Paulo 			 * of an IEEE80211_IOC_SCAN_REQ too in which case
65759aa14a9SRui Paulo 			 * the vap will be marked with IEEE80211_FEXT_SCANREQ
65859aa14a9SRui Paulo 			 * and the scan request parameters will be present
65959aa14a9SRui Paulo 			 * in iv_scanreq.  Otherwise we do the default.
66059aa14a9SRui Paulo 			*/
66159aa14a9SRui Paulo 			if (vap->iv_flags_ext & IEEE80211_FEXT_SCANREQ) {
66259aa14a9SRui Paulo 				ieee80211_check_scan(vap,
66359aa14a9SRui Paulo 				    vap->iv_scanreq_flags,
66459aa14a9SRui Paulo 				    vap->iv_scanreq_duration,
66559aa14a9SRui Paulo 				    vap->iv_scanreq_mindwell,
66659aa14a9SRui Paulo 				    vap->iv_scanreq_maxdwell,
66759aa14a9SRui Paulo 				    vap->iv_scanreq_nssid, vap->iv_scanreq_ssid);
66859aa14a9SRui Paulo 				vap->iv_flags_ext &= ~IEEE80211_FEXT_SCANREQ;
66959aa14a9SRui Paulo 			} else
67059aa14a9SRui Paulo 				ieee80211_check_scan_current(vap);
67159aa14a9SRui Paulo 			break;
67259aa14a9SRui Paulo 		default:
67359aa14a9SRui Paulo 			break;
67459aa14a9SRui Paulo 		}
67559aa14a9SRui Paulo 		break;
67659aa14a9SRui Paulo 	case IEEE80211_S_CAC:
67759aa14a9SRui Paulo 		/*
67859aa14a9SRui Paulo 		 * Start CAC on a DFS channel.  We come here when starting
67959aa14a9SRui Paulo 		 * a bss on a DFS channel (see ieee80211_create_ibss).
68059aa14a9SRui Paulo 		 */
68159aa14a9SRui Paulo 		ieee80211_dfs_cac_start(vap);
68259aa14a9SRui Paulo 		break;
68359aa14a9SRui Paulo 	case IEEE80211_S_RUN:
68459aa14a9SRui Paulo 		switch (ostate) {
68559aa14a9SRui Paulo 		case IEEE80211_S_INIT:
68659aa14a9SRui Paulo 			/*
68759aa14a9SRui Paulo 			 * Already have a channel; bypass the
68859aa14a9SRui Paulo 			 * scan and startup immediately.
68959aa14a9SRui Paulo 			 * Note that ieee80211_create_ibss will call
69059aa14a9SRui Paulo 			 * back to do a RUN->RUN state change.
69159aa14a9SRui Paulo 			 */
69259aa14a9SRui Paulo 			ieee80211_create_ibss(vap,
69359aa14a9SRui Paulo 			    ieee80211_ht_adjust_channel(ic,
69459aa14a9SRui Paulo 				ic->ic_curchan, vap->iv_flags_ht));
69559aa14a9SRui Paulo 			/* NB: iv_bss is changed on return */
69659aa14a9SRui Paulo 			break;
69759aa14a9SRui Paulo 		case IEEE80211_S_CAC:
69859aa14a9SRui Paulo 			/*
69959aa14a9SRui Paulo 			 * NB: This is the normal state change when CAC
70059aa14a9SRui Paulo 			 * expires and no radar was detected; no need to
70159aa14a9SRui Paulo 			 * clear the CAC timer as it's already expired.
70259aa14a9SRui Paulo 			 */
70359aa14a9SRui Paulo 			/* fall thru... */
70459aa14a9SRui Paulo 		case IEEE80211_S_CSA:
70559aa14a9SRui Paulo #if 0
70659aa14a9SRui Paulo 			/*
70759aa14a9SRui Paulo 			 * Shorten inactivity timer of associated stations
70859aa14a9SRui Paulo 			 * to weed out sta's that don't follow a CSA.
70959aa14a9SRui Paulo 			 */
71059aa14a9SRui Paulo 			ieee80211_iterate_nodes(&ic->ic_sta, sta_csa, vap);
71159aa14a9SRui Paulo #endif
71259aa14a9SRui Paulo 			/*
71359aa14a9SRui Paulo 			 * Update bss node channel to reflect where
71459aa14a9SRui Paulo 			 * we landed after CSA.
71559aa14a9SRui Paulo 			 */
71659aa14a9SRui Paulo 			ieee80211_node_set_chan(vap->iv_bss,
71759aa14a9SRui Paulo 			    ieee80211_ht_adjust_channel(ic, ic->ic_curchan,
71859aa14a9SRui Paulo 				ieee80211_htchanflags(vap->iv_bss->ni_chan)));
71959aa14a9SRui Paulo 			/* XXX bypass debug msgs */
72059aa14a9SRui Paulo 			break;
72159aa14a9SRui Paulo 		case IEEE80211_S_SCAN:
72259aa14a9SRui Paulo 		case IEEE80211_S_RUN:
72359aa14a9SRui Paulo #ifdef IEEE80211_DEBUG
72459aa14a9SRui Paulo 			if (ieee80211_msg_debug(vap)) {
72559aa14a9SRui Paulo 				struct ieee80211_node *ni = vap->iv_bss;
72659aa14a9SRui Paulo 				ieee80211_note(vap,
72759aa14a9SRui Paulo 				    "synchronized with %s meshid ",
72859aa14a9SRui Paulo 				    ether_sprintf(ni->ni_meshid));
72959aa14a9SRui Paulo 				ieee80211_print_essid(ni->ni_meshid,
73059aa14a9SRui Paulo 				    ni->ni_meshidlen);
73159aa14a9SRui Paulo 				/* XXX MCS/HT */
73259aa14a9SRui Paulo 				printf(" channel %d\n",
73359aa14a9SRui Paulo 				    ieee80211_chan2ieee(ic, ic->ic_curchan));
73459aa14a9SRui Paulo 			}
73559aa14a9SRui Paulo #endif
73659aa14a9SRui Paulo 			break;
73759aa14a9SRui Paulo 		default:
73859aa14a9SRui Paulo 			break;
73959aa14a9SRui Paulo 		}
74059aa14a9SRui Paulo 		ieee80211_node_authorize(vap->iv_bss);
741e50821abSSam Leffler 		callout_reset(&ms->ms_cleantimer, ms->ms_ppath->mpp_inact,
742c104cff2SRui Paulo                     mesh_rt_cleanup_cb, vap);
74359aa14a9SRui Paulo 		break;
74459aa14a9SRui Paulo 	default:
74559aa14a9SRui Paulo 		break;
74659aa14a9SRui Paulo 	}
74759aa14a9SRui Paulo 	/* NB: ostate not nstate */
74859aa14a9SRui Paulo 	ms->ms_ppath->mpp_newstate(vap, ostate, arg);
74959aa14a9SRui Paulo 	return 0;
75059aa14a9SRui Paulo }
75159aa14a9SRui Paulo 
752c104cff2SRui Paulo static void
753c104cff2SRui Paulo mesh_rt_cleanup_cb(void *arg)
754c104cff2SRui Paulo {
755c104cff2SRui Paulo 	struct ieee80211vap *vap = arg;
756c104cff2SRui Paulo 	struct ieee80211_mesh_state *ms = vap->iv_mesh;
757c104cff2SRui Paulo 
758c104cff2SRui Paulo 	mesh_rt_flush_invalid(vap);
759e50821abSSam Leffler 	callout_reset(&ms->ms_cleantimer, ms->ms_ppath->mpp_inact,
760c104cff2SRui Paulo 	    mesh_rt_cleanup_cb, vap);
761c104cff2SRui Paulo }
762c104cff2SRui Paulo 
763c104cff2SRui Paulo 
76459aa14a9SRui Paulo /*
76559aa14a9SRui Paulo  * Helper function to note the Mesh Peer Link FSM change.
76659aa14a9SRui Paulo  */
76759aa14a9SRui Paulo static void
76859aa14a9SRui Paulo mesh_linkchange(struct ieee80211_node *ni, enum ieee80211_mesh_mlstate state)
76959aa14a9SRui Paulo {
77059aa14a9SRui Paulo 	struct ieee80211vap *vap = ni->ni_vap;
77159aa14a9SRui Paulo 	struct ieee80211_mesh_state *ms = vap->iv_mesh;
77259aa14a9SRui Paulo #ifdef IEEE80211_DEBUG
77359aa14a9SRui Paulo 	static const char *meshlinkstates[] = {
77459aa14a9SRui Paulo 		[IEEE80211_NODE_MESH_IDLE]		= "IDLE",
77559aa14a9SRui Paulo 		[IEEE80211_NODE_MESH_OPENSNT]		= "OPEN SENT",
77659aa14a9SRui Paulo 		[IEEE80211_NODE_MESH_OPENRCV]		= "OPEN RECEIVED",
77759aa14a9SRui Paulo 		[IEEE80211_NODE_MESH_CONFIRMRCV]	= "CONFIRM RECEIVED",
77859aa14a9SRui Paulo 		[IEEE80211_NODE_MESH_ESTABLISHED]	= "ESTABLISHED",
77959aa14a9SRui Paulo 		[IEEE80211_NODE_MESH_HOLDING]		= "HOLDING"
78059aa14a9SRui Paulo 	};
78159aa14a9SRui Paulo #endif
78259aa14a9SRui Paulo 	IEEE80211_NOTE(vap, IEEE80211_MSG_MESH,
78359aa14a9SRui Paulo 	    ni, "peer link: %s -> %s",
78459aa14a9SRui Paulo 	    meshlinkstates[ni->ni_mlstate], meshlinkstates[state]);
78559aa14a9SRui Paulo 
78659aa14a9SRui Paulo 	/* track neighbor count */
78759aa14a9SRui Paulo 	if (state == IEEE80211_NODE_MESH_ESTABLISHED &&
78859aa14a9SRui Paulo 	    ni->ni_mlstate != IEEE80211_NODE_MESH_ESTABLISHED) {
78959aa14a9SRui Paulo 		KASSERT(ms->ms_neighbors < 65535, ("neighbor count overflow"));
79059aa14a9SRui Paulo 		ms->ms_neighbors++;
791d093681cSRui Paulo 		ieee80211_beacon_notify(vap, IEEE80211_BEACON_MESHCONF);
79259aa14a9SRui Paulo 	} else if (ni->ni_mlstate == IEEE80211_NODE_MESH_ESTABLISHED &&
79359aa14a9SRui Paulo 	    state != IEEE80211_NODE_MESH_ESTABLISHED) {
79459aa14a9SRui Paulo 		KASSERT(ms->ms_neighbors > 0, ("neighbor count 0"));
79559aa14a9SRui Paulo 		ms->ms_neighbors--;
796d093681cSRui Paulo 		ieee80211_beacon_notify(vap, IEEE80211_BEACON_MESHCONF);
79759aa14a9SRui Paulo 	}
79859aa14a9SRui Paulo 	ni->ni_mlstate = state;
799c104cff2SRui Paulo 	switch (state) {
800c104cff2SRui Paulo 	case IEEE80211_NODE_MESH_HOLDING:
80159aa14a9SRui Paulo 		ms->ms_ppath->mpp_peerdown(ni);
802c104cff2SRui Paulo 		break;
803c104cff2SRui Paulo 	case IEEE80211_NODE_MESH_ESTABLISHED:
804c104cff2SRui Paulo 		ieee80211_mesh_discover(vap, ni->ni_macaddr, NULL);
805c104cff2SRui Paulo 		break;
806c104cff2SRui Paulo 	default:
807c104cff2SRui Paulo 		break;
808c104cff2SRui Paulo 	}
80959aa14a9SRui Paulo }
81059aa14a9SRui Paulo 
81159aa14a9SRui Paulo /*
81259aa14a9SRui Paulo  * Helper function to generate a unique local ID required for mesh
81359aa14a9SRui Paulo  * peer establishment.
81459aa14a9SRui Paulo  */
81559aa14a9SRui Paulo static void
81659aa14a9SRui Paulo mesh_checkid(void *arg, struct ieee80211_node *ni)
81759aa14a9SRui Paulo {
81859aa14a9SRui Paulo 	uint16_t *r = arg;
81959aa14a9SRui Paulo 
82059aa14a9SRui Paulo 	if (*r == ni->ni_mllid)
82159aa14a9SRui Paulo 		*(uint16_t *)arg = 0;
82259aa14a9SRui Paulo }
82359aa14a9SRui Paulo 
82459aa14a9SRui Paulo static uint32_t
82559aa14a9SRui Paulo mesh_generateid(struct ieee80211vap *vap)
82659aa14a9SRui Paulo {
82759aa14a9SRui Paulo 	int maxiter = 4;
82859aa14a9SRui Paulo 	uint16_t r;
82959aa14a9SRui Paulo 
83059aa14a9SRui Paulo 	do {
83159aa14a9SRui Paulo 		get_random_bytes(&r, 2);
83259aa14a9SRui Paulo 		ieee80211_iterate_nodes(&vap->iv_ic->ic_sta, mesh_checkid, &r);
83359aa14a9SRui Paulo 		maxiter--;
83459aa14a9SRui Paulo 	} while (r == 0 && maxiter > 0);
83559aa14a9SRui Paulo 	return r;
83659aa14a9SRui Paulo }
83759aa14a9SRui Paulo 
83859aa14a9SRui Paulo /*
83959aa14a9SRui Paulo  * Verifies if we already received this packet by checking its
84059aa14a9SRui Paulo  * sequence number.
8413ca80f0dSRui Paulo  * Returns 0 if the frame is to be accepted, 1 otherwise.
84259aa14a9SRui Paulo  */
84359aa14a9SRui Paulo static int
84459aa14a9SRui Paulo mesh_checkpseq(struct ieee80211vap *vap,
84559aa14a9SRui Paulo     const uint8_t source[IEEE80211_ADDR_LEN], uint32_t seq)
84659aa14a9SRui Paulo {
84759aa14a9SRui Paulo 	struct ieee80211_mesh_route *rt;
84859aa14a9SRui Paulo 
84959aa14a9SRui Paulo 	rt = ieee80211_mesh_rt_find(vap, source);
85059aa14a9SRui Paulo 	if (rt == NULL) {
8513ca80f0dSRui Paulo 		rt = ieee80211_mesh_rt_add(vap, source);
8523ca80f0dSRui Paulo 		if (rt == NULL) {
8533ca80f0dSRui Paulo 			IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH, source,
8543ca80f0dSRui Paulo 			    "%s", "add mcast route failed");
8553ca80f0dSRui Paulo 			vap->iv_stats.is_mesh_rtaddfailed++;
8563ca80f0dSRui Paulo 			return 1;
8573ca80f0dSRui Paulo 		}
858c104cff2SRui Paulo 		IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH, source,
859c104cff2SRui Paulo 		    "add mcast route, mesh seqno %d", seq);
86059aa14a9SRui Paulo 		rt->rt_lastmseq = seq;
86159aa14a9SRui Paulo 		return 0;
86259aa14a9SRui Paulo 	}
86359aa14a9SRui Paulo 	if (IEEE80211_MESH_SEQ_GEQ(rt->rt_lastmseq, seq)) {
86459aa14a9SRui Paulo 		return 1;
86559aa14a9SRui Paulo 	} else {
86659aa14a9SRui Paulo 		rt->rt_lastmseq = seq;
86759aa14a9SRui Paulo 		return 0;
86859aa14a9SRui Paulo 	}
86959aa14a9SRui Paulo }
87059aa14a9SRui Paulo 
87159aa14a9SRui Paulo /*
87259aa14a9SRui Paulo  * Iterate the routing table and locate the next hop.
87359aa14a9SRui Paulo  */
87459aa14a9SRui Paulo static struct ieee80211_node *
87559aa14a9SRui Paulo mesh_find_txnode(struct ieee80211vap *vap,
87659aa14a9SRui Paulo     const uint8_t dest[IEEE80211_ADDR_LEN])
87759aa14a9SRui Paulo {
87859aa14a9SRui Paulo 	struct ieee80211_mesh_route *rt;
87959aa14a9SRui Paulo 
88059aa14a9SRui Paulo 	rt = ieee80211_mesh_rt_find(vap, dest);
88159aa14a9SRui Paulo 	if (rt == NULL)
88259aa14a9SRui Paulo 		return NULL;
883c104cff2SRui Paulo 	if ((rt->rt_flags & IEEE80211_MESHRT_FLAGS_VALID) == 0 ||
884c104cff2SRui Paulo 	    (rt->rt_flags & IEEE80211_MESHRT_FLAGS_PROXY)) {
885c104cff2SRui Paulo 		IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH, dest,
886c104cff2SRui Paulo 		    "%s: !valid or proxy, flags 0x%x", __func__, rt->rt_flags);
887c104cff2SRui Paulo 		/* XXX stat */
888c104cff2SRui Paulo 		return NULL;
889c104cff2SRui Paulo 	}
89059aa14a9SRui Paulo 	return ieee80211_find_txnode(vap, rt->rt_nexthop);
89159aa14a9SRui Paulo }
89259aa14a9SRui Paulo 
89359aa14a9SRui Paulo /*
89459aa14a9SRui Paulo  * Forward the specified frame.
89559aa14a9SRui Paulo  * Decrement the TTL and set TA to our MAC address.
89659aa14a9SRui Paulo  */
89759aa14a9SRui Paulo static void
89859aa14a9SRui Paulo mesh_forward(struct ieee80211vap *vap, struct mbuf *m,
89959aa14a9SRui Paulo     const struct ieee80211_meshcntl *mc)
90059aa14a9SRui Paulo {
90159aa14a9SRui Paulo 	struct ieee80211com *ic = vap->iv_ic;
90259aa14a9SRui Paulo 	struct ieee80211_mesh_state *ms = vap->iv_mesh;
90359aa14a9SRui Paulo 	struct ifnet *ifp = vap->iv_ifp;
90459aa14a9SRui Paulo 	struct ifnet *parent = ic->ic_ifp;
90559aa14a9SRui Paulo 	const struct ieee80211_frame *wh =
90659aa14a9SRui Paulo 	    mtod(m, const struct ieee80211_frame *);
90759aa14a9SRui Paulo 	struct mbuf *mcopy;
90859aa14a9SRui Paulo 	struct ieee80211_meshcntl *mccopy;
90959aa14a9SRui Paulo 	struct ieee80211_frame *whcopy;
91059aa14a9SRui Paulo 	struct ieee80211_node *ni;
91159aa14a9SRui Paulo 	int err;
91259aa14a9SRui Paulo 
91359aa14a9SRui Paulo 	if (mc->mc_ttl == 0) {
91459aa14a9SRui Paulo 		IEEE80211_NOTE_FRAME(vap, IEEE80211_MSG_MESH, wh,
91559aa14a9SRui Paulo 		    "%s", "frame not fwd'd, ttl 0");
91659aa14a9SRui Paulo 		vap->iv_stats.is_mesh_fwd_ttl++;
91759aa14a9SRui Paulo 		return;
91859aa14a9SRui Paulo 	}
91959aa14a9SRui Paulo 	if (!(ms->ms_flags & IEEE80211_MESHFLAGS_FWD)) {
92059aa14a9SRui Paulo 		IEEE80211_NOTE_FRAME(vap, IEEE80211_MSG_MESH, wh,
92159aa14a9SRui Paulo 		    "%s", "frame not fwd'd, fwding disabled");
92259aa14a9SRui Paulo 		vap->iv_stats.is_mesh_fwd_disabled++;
92359aa14a9SRui Paulo 		return;
92459aa14a9SRui Paulo 	}
92559aa14a9SRui Paulo 	mcopy = m_dup(m, M_DONTWAIT);
92659aa14a9SRui Paulo 	if (mcopy == NULL) {
92759aa14a9SRui Paulo 		IEEE80211_NOTE_FRAME(vap, IEEE80211_MSG_MESH, wh,
92859aa14a9SRui Paulo 		    "%s", "frame not fwd'd, cannot dup");
92959aa14a9SRui Paulo 		vap->iv_stats.is_mesh_fwd_nobuf++;
93059aa14a9SRui Paulo 		ifp->if_oerrors++;
93159aa14a9SRui Paulo 		return;
93259aa14a9SRui Paulo 	}
93359aa14a9SRui Paulo 	mcopy = m_pullup(mcopy, ieee80211_hdrspace(ic, wh) +
93459aa14a9SRui Paulo 	    sizeof(struct ieee80211_meshcntl));
93559aa14a9SRui Paulo 	if (mcopy == NULL) {
93659aa14a9SRui Paulo 		IEEE80211_NOTE_FRAME(vap, IEEE80211_MSG_MESH, wh,
93759aa14a9SRui Paulo 		    "%s", "frame not fwd'd, too short");
93859aa14a9SRui Paulo 		vap->iv_stats.is_mesh_fwd_tooshort++;
93959aa14a9SRui Paulo 		ifp->if_oerrors++;
94059aa14a9SRui Paulo 		m_freem(mcopy);
94159aa14a9SRui Paulo 		return;
94259aa14a9SRui Paulo 	}
94359aa14a9SRui Paulo 	whcopy = mtod(mcopy, struct ieee80211_frame *);
94459aa14a9SRui Paulo 	mccopy = (struct ieee80211_meshcntl *)
94559aa14a9SRui Paulo 	    (mtod(mcopy, uint8_t *) + ieee80211_hdrspace(ic, wh));
94659aa14a9SRui Paulo 	/* XXX clear other bits? */
94759aa14a9SRui Paulo 	whcopy->i_fc[1] &= ~IEEE80211_FC1_RETRY;
94859aa14a9SRui Paulo 	IEEE80211_ADDR_COPY(whcopy->i_addr2, vap->iv_myaddr);
94959aa14a9SRui Paulo 	if (IEEE80211_IS_MULTICAST(wh->i_addr1)) {
95059aa14a9SRui Paulo 		ni = ieee80211_ref_node(vap->iv_bss);
95159aa14a9SRui Paulo 		mcopy->m_flags |= M_MCAST;
95259aa14a9SRui Paulo 	} else {
95359aa14a9SRui Paulo 		ni = mesh_find_txnode(vap, whcopy->i_addr3);
95459aa14a9SRui Paulo 		if (ni == NULL) {
95559aa14a9SRui Paulo 			IEEE80211_NOTE_FRAME(vap, IEEE80211_MSG_MESH, wh,
95659aa14a9SRui Paulo 			    "%s", "frame not fwd'd, no path");
95759aa14a9SRui Paulo 			vap->iv_stats.is_mesh_fwd_nopath++;
95859aa14a9SRui Paulo 			m_freem(mcopy);
95959aa14a9SRui Paulo 			return;
96059aa14a9SRui Paulo 		}
96159aa14a9SRui Paulo 		IEEE80211_ADDR_COPY(whcopy->i_addr1, ni->ni_macaddr);
96259aa14a9SRui Paulo 	}
96359aa14a9SRui Paulo 	KASSERT(mccopy->mc_ttl > 0, ("%s called with wrong ttl", __func__));
96459aa14a9SRui Paulo 	mccopy->mc_ttl--;
96559aa14a9SRui Paulo 
96659aa14a9SRui Paulo 	/* XXX calculate priority so drivers can find the tx queue */
96759aa14a9SRui Paulo 	M_WME_SETAC(mcopy, WME_AC_BE);
96859aa14a9SRui Paulo 
96959aa14a9SRui Paulo 	/* XXX do we know m_nextpkt is NULL? */
97059aa14a9SRui Paulo 	mcopy->m_pkthdr.rcvif = (void *) ni;
97159aa14a9SRui Paulo 	err = parent->if_transmit(parent, mcopy);
97259aa14a9SRui Paulo 	if (err != 0) {
97359aa14a9SRui Paulo 		/* NB: IFQ_HANDOFF reclaims mbuf */
97459aa14a9SRui Paulo 		ieee80211_free_node(ni);
97559aa14a9SRui Paulo 	} else {
97659aa14a9SRui Paulo 		ifp->if_opackets++;
97759aa14a9SRui Paulo 	}
97859aa14a9SRui Paulo }
97959aa14a9SRui Paulo 
980c104cff2SRui Paulo static struct mbuf *
981c104cff2SRui Paulo mesh_decap(struct ieee80211vap *vap, struct mbuf *m, int hdrlen, int meshdrlen)
982c104cff2SRui Paulo {
983c104cff2SRui Paulo #define	WHDIR(wh) ((wh)->i_fc[1] & IEEE80211_FC1_DIR_MASK)
984c104cff2SRui Paulo 	uint8_t b[sizeof(struct ieee80211_qosframe_addr4) +
985c104cff2SRui Paulo 		  sizeof(struct ieee80211_meshcntl_ae11)];
986c104cff2SRui Paulo 	const struct ieee80211_qosframe_addr4 *wh;
987c104cff2SRui Paulo 	const struct ieee80211_meshcntl_ae10 *mc;
988c104cff2SRui Paulo 	struct ether_header *eh;
989c104cff2SRui Paulo 	struct llc *llc;
990c104cff2SRui Paulo 	int ae;
991c104cff2SRui Paulo 
992c104cff2SRui Paulo 	if (m->m_len < hdrlen + sizeof(*llc) &&
993c104cff2SRui Paulo 	    (m = m_pullup(m, hdrlen + sizeof(*llc))) == NULL) {
994c104cff2SRui Paulo 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_ANY,
995c104cff2SRui Paulo 		    "discard data frame: %s", "m_pullup failed");
996c104cff2SRui Paulo 		vap->iv_stats.is_rx_tooshort++;
997c104cff2SRui Paulo 		return NULL;
998c104cff2SRui Paulo 	}
999c104cff2SRui Paulo 	memcpy(b, mtod(m, caddr_t), hdrlen);
1000c104cff2SRui Paulo 	wh = (const struct ieee80211_qosframe_addr4 *)&b[0];
1001c104cff2SRui Paulo 	mc = (const struct ieee80211_meshcntl_ae10 *)&b[hdrlen - meshdrlen];
1002c104cff2SRui Paulo 	KASSERT(WHDIR(wh) == IEEE80211_FC1_DIR_FROMDS ||
1003c104cff2SRui Paulo 		WHDIR(wh) == IEEE80211_FC1_DIR_DSTODS,
1004c104cff2SRui Paulo 	    ("bogus dir, fc 0x%x:0x%x", wh->i_fc[0], wh->i_fc[1]));
1005c104cff2SRui Paulo 
1006c104cff2SRui Paulo 	llc = (struct llc *)(mtod(m, caddr_t) + hdrlen);
1007c104cff2SRui Paulo 	if (llc->llc_dsap == LLC_SNAP_LSAP && llc->llc_ssap == LLC_SNAP_LSAP &&
1008c104cff2SRui Paulo 	    llc->llc_control == LLC_UI && llc->llc_snap.org_code[0] == 0 &&
1009c104cff2SRui Paulo 	    llc->llc_snap.org_code[1] == 0 && llc->llc_snap.org_code[2] == 0 &&
1010c104cff2SRui Paulo 	    /* NB: preserve AppleTalk frames that have a native SNAP hdr */
1011c104cff2SRui Paulo 	    !(llc->llc_snap.ether_type == htons(ETHERTYPE_AARP) ||
1012c104cff2SRui Paulo 	      llc->llc_snap.ether_type == htons(ETHERTYPE_IPX))) {
1013c104cff2SRui Paulo 		m_adj(m, hdrlen + sizeof(struct llc) - sizeof(*eh));
1014c104cff2SRui Paulo 		llc = NULL;
1015c104cff2SRui Paulo 	} else {
1016c104cff2SRui Paulo 		m_adj(m, hdrlen - sizeof(*eh));
1017c104cff2SRui Paulo 	}
1018c104cff2SRui Paulo 	eh = mtod(m, struct ether_header *);
1019c104cff2SRui Paulo 	ae = mc->mc_flags & 3;
1020c104cff2SRui Paulo 	if (WHDIR(wh) == IEEE80211_FC1_DIR_FROMDS) {
1021c104cff2SRui Paulo 		IEEE80211_ADDR_COPY(eh->ether_dhost, wh->i_addr1);
1022c104cff2SRui Paulo 		if (ae == 0) {
1023c104cff2SRui Paulo 			IEEE80211_ADDR_COPY(eh->ether_shost, wh->i_addr3);
1024c104cff2SRui Paulo 		} else if (ae == 1) {
1025c104cff2SRui Paulo 			IEEE80211_ADDR_COPY(eh->ether_shost, mc->mc_addr4);
1026c104cff2SRui Paulo 		} else {
1027c104cff2SRui Paulo 			IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
1028c104cff2SRui Paulo 			    (const struct ieee80211_frame *)wh, NULL,
1029c104cff2SRui Paulo 			    "bad AE %d", ae);
1030c104cff2SRui Paulo 			vap->iv_stats.is_mesh_badae++;
1031c104cff2SRui Paulo 			m_freem(m);
1032c104cff2SRui Paulo 			return NULL;
1033c104cff2SRui Paulo 		}
1034c104cff2SRui Paulo 	} else {
1035c104cff2SRui Paulo 		if (ae == 0) {
1036c104cff2SRui Paulo 			IEEE80211_ADDR_COPY(eh->ether_dhost, wh->i_addr3);
1037c104cff2SRui Paulo 			IEEE80211_ADDR_COPY(eh->ether_shost, wh->i_addr4);
1038c104cff2SRui Paulo 		} else if (ae == 2) {
1039c104cff2SRui Paulo 			IEEE80211_ADDR_COPY(eh->ether_dhost, mc->mc_addr4);
1040c104cff2SRui Paulo 			IEEE80211_ADDR_COPY(eh->ether_shost, mc->mc_addr5);
1041c104cff2SRui Paulo 		} else {
1042c104cff2SRui Paulo 			IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
1043c104cff2SRui Paulo 			    (const struct ieee80211_frame *)wh, NULL,
1044c104cff2SRui Paulo 			    "bad AE %d", ae);
1045c104cff2SRui Paulo 			vap->iv_stats.is_mesh_badae++;
1046c104cff2SRui Paulo 			m_freem(m);
1047c104cff2SRui Paulo 			return NULL;
1048c104cff2SRui Paulo 		}
1049c104cff2SRui Paulo 	}
1050c104cff2SRui Paulo #ifdef ALIGNED_POINTER
1051c104cff2SRui Paulo 	if (!ALIGNED_POINTER(mtod(m, caddr_t) + sizeof(*eh), uint32_t)) {
1052c104cff2SRui Paulo 		m = ieee80211_realign(vap, m, sizeof(*eh));
1053c104cff2SRui Paulo 		if (m == NULL)
1054c104cff2SRui Paulo 			return NULL;
1055c104cff2SRui Paulo 	}
1056c104cff2SRui Paulo #endif /* ALIGNED_POINTER */
1057c104cff2SRui Paulo 	if (llc != NULL) {
1058c104cff2SRui Paulo 		eh = mtod(m, struct ether_header *);
1059c104cff2SRui Paulo 		eh->ether_type = htons(m->m_pkthdr.len - sizeof(*eh));
1060c104cff2SRui Paulo 	}
1061c104cff2SRui Paulo 	return m;
1062c104cff2SRui Paulo #undef WDIR
1063c104cff2SRui Paulo }
1064c104cff2SRui Paulo 
1065c104cff2SRui Paulo /*
1066c104cff2SRui Paulo  * Return non-zero if the unicast mesh data frame should be processed
1067c104cff2SRui Paulo  * locally.  Frames that are not proxy'd have our address, otherwise
1068c104cff2SRui Paulo  * we need to consult the routing table to look for a proxy entry.
1069c104cff2SRui Paulo  */
1070c104cff2SRui Paulo static __inline int
1071c104cff2SRui Paulo mesh_isucastforme(struct ieee80211vap *vap, const struct ieee80211_frame *wh,
1072c104cff2SRui Paulo     const struct ieee80211_meshcntl *mc)
1073c104cff2SRui Paulo {
1074c104cff2SRui Paulo 	int ae = mc->mc_flags & 3;
1075c104cff2SRui Paulo 
1076c104cff2SRui Paulo 	KASSERT((wh->i_fc[1] & IEEE80211_FC1_DIR_MASK) == IEEE80211_FC1_DIR_DSTODS,
1077c104cff2SRui Paulo 	    ("bad dir 0x%x:0x%x", wh->i_fc[0], wh->i_fc[1]));
1078c104cff2SRui Paulo 	KASSERT(ae == 0 || ae == 2, ("bad AE %d", ae));
1079c104cff2SRui Paulo 	if (ae == 2) {				/* ucast w/ proxy */
1080c104cff2SRui Paulo 		const struct ieee80211_meshcntl_ae10 *mc10 =
1081c104cff2SRui Paulo 		    (const struct ieee80211_meshcntl_ae10 *) mc;
1082c104cff2SRui Paulo 		struct ieee80211_mesh_route *rt =
1083c104cff2SRui Paulo 		    ieee80211_mesh_rt_find(vap, mc10->mc_addr4);
1084c104cff2SRui Paulo 		/* check for proxy route to ourself */
1085c104cff2SRui Paulo 		return (rt != NULL &&
1086c104cff2SRui Paulo 		    (rt->rt_flags & IEEE80211_MESHRT_FLAGS_PROXY));
1087c104cff2SRui Paulo 	} else					/* ucast w/o proxy */
1088c104cff2SRui Paulo 		return IEEE80211_ADDR_EQ(wh->i_addr3, vap->iv_myaddr);
1089c104cff2SRui Paulo }
1090c104cff2SRui Paulo 
109159aa14a9SRui Paulo static int
109259aa14a9SRui Paulo mesh_input(struct ieee80211_node *ni, struct mbuf *m, int rssi, int nf)
109359aa14a9SRui Paulo {
109459aa14a9SRui Paulo #define	HAS_SEQ(type)	((type & 0x4) == 0)
109559aa14a9SRui Paulo 	struct ieee80211vap *vap = ni->ni_vap;
109659aa14a9SRui Paulo 	struct ieee80211com *ic = ni->ni_ic;
109759aa14a9SRui Paulo 	struct ifnet *ifp = vap->iv_ifp;
109859aa14a9SRui Paulo 	struct ieee80211_frame *wh;
109959aa14a9SRui Paulo 	const struct ieee80211_meshcntl *mc;
1100c104cff2SRui Paulo 	int hdrspace, meshdrlen, need_tap;
110191216c71SAdrian Chadd 	uint8_t dir, type, subtype;
110259aa14a9SRui Paulo 	uint32_t seq;
110391216c71SAdrian Chadd 	uint8_t *addr, qos[2];
110459aa14a9SRui Paulo 	ieee80211_seq rxseq;
110559aa14a9SRui Paulo 
110659aa14a9SRui Paulo 	KASSERT(ni != NULL, ("null node"));
110759aa14a9SRui Paulo 	ni->ni_inact = ni->ni_inact_reload;
110859aa14a9SRui Paulo 
110959aa14a9SRui Paulo 	need_tap = 1;			/* mbuf need to be tapped. */
111059aa14a9SRui Paulo 	type = -1;			/* undefined */
111159aa14a9SRui Paulo 
111259aa14a9SRui Paulo 	if (m->m_pkthdr.len < sizeof(struct ieee80211_frame_min)) {
111359aa14a9SRui Paulo 		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
111459aa14a9SRui Paulo 		    ni->ni_macaddr, NULL,
111559aa14a9SRui Paulo 		    "too short (1): len %u", m->m_pkthdr.len);
111659aa14a9SRui Paulo 		vap->iv_stats.is_rx_tooshort++;
111759aa14a9SRui Paulo 		goto out;
111859aa14a9SRui Paulo 	}
111959aa14a9SRui Paulo 	/*
112059aa14a9SRui Paulo 	 * Bit of a cheat here, we use a pointer for a 3-address
112159aa14a9SRui Paulo 	 * frame format but don't reference fields past outside
112259aa14a9SRui Paulo 	 * ieee80211_frame_min w/o first validating the data is
112359aa14a9SRui Paulo 	 * present.
112459aa14a9SRui Paulo 	*/
112559aa14a9SRui Paulo 	wh = mtod(m, struct ieee80211_frame *);
112659aa14a9SRui Paulo 
112759aa14a9SRui Paulo 	if ((wh->i_fc[0] & IEEE80211_FC0_VERSION_MASK) !=
112859aa14a9SRui Paulo 	    IEEE80211_FC0_VERSION_0) {
112959aa14a9SRui Paulo 		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
113059aa14a9SRui Paulo 		    ni->ni_macaddr, NULL, "wrong version %x", wh->i_fc[0]);
113159aa14a9SRui Paulo 		vap->iv_stats.is_rx_badversion++;
113259aa14a9SRui Paulo 		goto err;
113359aa14a9SRui Paulo 	}
113459aa14a9SRui Paulo 	dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK;
113559aa14a9SRui Paulo 	type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
113659aa14a9SRui Paulo 	subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
113759aa14a9SRui Paulo 	if ((ic->ic_flags & IEEE80211_F_SCAN) == 0) {
113859aa14a9SRui Paulo 		IEEE80211_RSSI_LPF(ni->ni_avgrssi, rssi);
113959aa14a9SRui Paulo 		ni->ni_noise = nf;
114059aa14a9SRui Paulo 		if (HAS_SEQ(type)) {
114159aa14a9SRui Paulo 			uint8_t tid = ieee80211_gettid(wh);
114259aa14a9SRui Paulo 
114359aa14a9SRui Paulo 			if (IEEE80211_QOS_HAS_SEQ(wh) &&
114459aa14a9SRui Paulo 			    TID_TO_WME_AC(tid) >= WME_AC_VI)
114559aa14a9SRui Paulo 				ic->ic_wme.wme_hipri_traffic++;
114659aa14a9SRui Paulo 			rxseq = le16toh(*(uint16_t *)wh->i_seq);
1147cd0b8f2dSAdrian Chadd 			if (! ieee80211_check_rxseq(ni, wh)) {
114859aa14a9SRui Paulo 				/* duplicate, discard */
114959aa14a9SRui Paulo 				IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
115059aa14a9SRui Paulo 				    wh->i_addr1, "duplicate",
115159aa14a9SRui Paulo 				    "seqno <%u,%u> fragno <%u,%u> tid %u",
115259aa14a9SRui Paulo 				    rxseq >> IEEE80211_SEQ_SEQ_SHIFT,
115359aa14a9SRui Paulo 				    ni->ni_rxseqs[tid] >>
115459aa14a9SRui Paulo 				    IEEE80211_SEQ_SEQ_SHIFT,
115559aa14a9SRui Paulo 				    rxseq & IEEE80211_SEQ_FRAG_MASK,
115659aa14a9SRui Paulo 				    ni->ni_rxseqs[tid] &
115759aa14a9SRui Paulo 				    IEEE80211_SEQ_FRAG_MASK,
115859aa14a9SRui Paulo 				    tid);
115959aa14a9SRui Paulo 				vap->iv_stats.is_rx_dup++;
116059aa14a9SRui Paulo 				IEEE80211_NODE_STAT(ni, rx_dup);
116159aa14a9SRui Paulo 				goto out;
116259aa14a9SRui Paulo 			}
116359aa14a9SRui Paulo 			ni->ni_rxseqs[tid] = rxseq;
116459aa14a9SRui Paulo 		}
116559aa14a9SRui Paulo 	}
116659aa14a9SRui Paulo #ifdef IEEE80211_DEBUG
116759aa14a9SRui Paulo 	/*
116859aa14a9SRui Paulo 	 * It's easier, but too expensive, to simulate different mesh
116959aa14a9SRui Paulo 	 * topologies by consulting the ACL policy very early, so do this
117059aa14a9SRui Paulo 	 * only under DEBUG.
117159aa14a9SRui Paulo 	 *
117259aa14a9SRui Paulo 	 * NB: this check is also done upon peering link initiation.
117359aa14a9SRui Paulo 	 */
11745a8801b0SBernhard Schmidt 	if (vap->iv_acl != NULL && !vap->iv_acl->iac_check(vap, wh)) {
117559aa14a9SRui Paulo 		IEEE80211_DISCARD(vap, IEEE80211_MSG_ACL,
117659aa14a9SRui Paulo 		    wh, NULL, "%s", "disallowed by ACL");
117759aa14a9SRui Paulo 		vap->iv_stats.is_rx_acl++;
117859aa14a9SRui Paulo 		goto out;
117959aa14a9SRui Paulo 	}
118059aa14a9SRui Paulo #endif
118159aa14a9SRui Paulo 	switch (type) {
118259aa14a9SRui Paulo 	case IEEE80211_FC0_TYPE_DATA:
118359aa14a9SRui Paulo 		if (ni == vap->iv_bss)
118459aa14a9SRui Paulo 			goto out;
118559aa14a9SRui Paulo 		if (ni->ni_mlstate != IEEE80211_NODE_MESH_ESTABLISHED) {
118659aa14a9SRui Paulo 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_MESH,
118759aa14a9SRui Paulo 			    ni->ni_macaddr, NULL,
118859aa14a9SRui Paulo 			    "peer link not yet established (%d)",
118959aa14a9SRui Paulo 			    ni->ni_mlstate);
119059aa14a9SRui Paulo 			vap->iv_stats.is_mesh_nolink++;
119159aa14a9SRui Paulo 			goto out;
119259aa14a9SRui Paulo 		}
119359aa14a9SRui Paulo 		if (dir != IEEE80211_FC1_DIR_FROMDS &&
119459aa14a9SRui Paulo 		    dir != IEEE80211_FC1_DIR_DSTODS) {
119559aa14a9SRui Paulo 			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
119659aa14a9SRui Paulo 			    wh, "data", "incorrect dir 0x%x", dir);
119759aa14a9SRui Paulo 			vap->iv_stats.is_rx_wrongdir++;
119859aa14a9SRui Paulo 			goto err;
119959aa14a9SRui Paulo 		}
120091216c71SAdrian Chadd 
120191216c71SAdrian Chadd 		/* All Mesh data frames are QoS subtype */
120291216c71SAdrian Chadd 		if (!HAS_SEQ(type)) {
120391216c71SAdrian Chadd 			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
120491216c71SAdrian Chadd 			    wh, "data", "incorrect subtype 0x%x", subtype);
120591216c71SAdrian Chadd 			vap->iv_stats.is_rx_badsubtype++;
120691216c71SAdrian Chadd 			goto err;
120791216c71SAdrian Chadd 		}
120891216c71SAdrian Chadd 
120991216c71SAdrian Chadd 		/*
121091216c71SAdrian Chadd 		 * Next up, any fragmentation.
121191216c71SAdrian Chadd 		 * XXX: we defrag before we even try to forward,
121291216c71SAdrian Chadd 		 * Mesh Control field is not present in sub-sequent
121391216c71SAdrian Chadd 		 * fragmented frames. This is in contrast to Draft 4.0.
121491216c71SAdrian Chadd 		 */
121559aa14a9SRui Paulo 		hdrspace = ieee80211_hdrspace(ic, wh);
121691216c71SAdrian Chadd 		if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
121791216c71SAdrian Chadd 			m = ieee80211_defrag(ni, m, hdrspace);
121891216c71SAdrian Chadd 			if (m == NULL) {
121991216c71SAdrian Chadd 				/* Fragment dropped or frame not complete yet */
122091216c71SAdrian Chadd 				goto out;
122191216c71SAdrian Chadd 			}
122291216c71SAdrian Chadd 		}
122391216c71SAdrian Chadd 		wh = mtod(m, struct ieee80211_frame *); /* NB: after defrag */
122491216c71SAdrian Chadd 
122591216c71SAdrian Chadd 		/*
122691216c71SAdrian Chadd 		 * Now we have a complete Mesh Data frame.
122791216c71SAdrian Chadd 		 */
122891216c71SAdrian Chadd 
122991216c71SAdrian Chadd 		/*
123091216c71SAdrian Chadd 		 * Only fromDStoDS data frames use 4 address qos frames
123191216c71SAdrian Chadd 		 * as specified in amendment. Otherwise addr4 is located
123291216c71SAdrian Chadd 		 * in the Mesh Control field and a 3 address qos frame
123391216c71SAdrian Chadd 		 * is used.
123491216c71SAdrian Chadd 		 */
123591216c71SAdrian Chadd 		if (IEEE80211_IS_DSTODS(wh))
123691216c71SAdrian Chadd 			*(uint16_t *)qos = *(uint16_t *)
123791216c71SAdrian Chadd 			    ((struct ieee80211_qosframe_addr4 *)wh)->i_qos;
123891216c71SAdrian Chadd 		else
123991216c71SAdrian Chadd 			*(uint16_t *)qos = *(uint16_t *)
124091216c71SAdrian Chadd 			    ((struct ieee80211_qosframe *)wh)->i_qos;
124191216c71SAdrian Chadd 
124291216c71SAdrian Chadd 		/*
124391216c71SAdrian Chadd 		 * NB: The mesh STA sets the Mesh Control Present
124491216c71SAdrian Chadd 		 * subfield to 1 in the Mesh Data frame containing
124591216c71SAdrian Chadd 		 * an unfragmented MSDU, an A-MSDU, or the first
124691216c71SAdrian Chadd 		 * fragment of an MSDU.
124791216c71SAdrian Chadd 		 * After defrag it should always be present.
124891216c71SAdrian Chadd 		 */
124991216c71SAdrian Chadd 		if (!(qos[1] & IEEE80211_QOS_MC)) {
125091216c71SAdrian Chadd 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_MESH,
125191216c71SAdrian Chadd 			    ni->ni_macaddr, NULL,
125291216c71SAdrian Chadd 			    "%s", "Mesh control field not present");
125391216c71SAdrian Chadd 			vap->iv_stats.is_rx_elem_missing++; /* XXX: kinda */
125491216c71SAdrian Chadd 			goto err;
125591216c71SAdrian Chadd 		}
125691216c71SAdrian Chadd 
125791216c71SAdrian Chadd 		/* pull up enough to get to the mesh control */
125859aa14a9SRui Paulo 		if (m->m_len < hdrspace + sizeof(struct ieee80211_meshcntl) &&
125959aa14a9SRui Paulo 		    (m = m_pullup(m, hdrspace +
126059aa14a9SRui Paulo 		        sizeof(struct ieee80211_meshcntl))) == NULL) {
126159aa14a9SRui Paulo 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
126259aa14a9SRui Paulo 			    ni->ni_macaddr, NULL,
126359aa14a9SRui Paulo 			    "data too short: expecting %u", hdrspace);
126459aa14a9SRui Paulo 			vap->iv_stats.is_rx_tooshort++;
126559aa14a9SRui Paulo 			goto out;		/* XXX */
126659aa14a9SRui Paulo 		}
126759aa14a9SRui Paulo 		/*
126859aa14a9SRui Paulo 		 * Now calculate the full extent of the headers. Note
1269c104cff2SRui Paulo 		 * mesh_decap will pull up anything we didn't get
127059aa14a9SRui Paulo 		 * above when it strips the 802.11 headers.
127159aa14a9SRui Paulo 		 */
127259aa14a9SRui Paulo 		mc = (const struct ieee80211_meshcntl *)
127359aa14a9SRui Paulo 		    (mtod(m, const uint8_t *) + hdrspace);
1274c104cff2SRui Paulo 		meshdrlen = sizeof(struct ieee80211_meshcntl) +
127559aa14a9SRui Paulo 		    (mc->mc_flags & 3) * IEEE80211_ADDR_LEN;
1276c104cff2SRui Paulo 		hdrspace += meshdrlen;
127759aa14a9SRui Paulo 		seq = LE_READ_4(mc->mc_seq);
127859aa14a9SRui Paulo 		if (IEEE80211_IS_MULTICAST(wh->i_addr1))
127959aa14a9SRui Paulo 			addr = wh->i_addr3;
128059aa14a9SRui Paulo 		else
128159aa14a9SRui Paulo 			addr = ((struct ieee80211_qosframe_addr4 *)wh)->i_addr4;
128259aa14a9SRui Paulo 		if (IEEE80211_ADDR_EQ(vap->iv_myaddr, addr)) {
128359aa14a9SRui Paulo 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
128459aa14a9SRui Paulo 			    addr, "data", "%s", "not to me");
128559aa14a9SRui Paulo 			vap->iv_stats.is_rx_wrongbss++;	/* XXX kinda */
128659aa14a9SRui Paulo 			goto out;
128759aa14a9SRui Paulo 		}
128859aa14a9SRui Paulo 		if (mesh_checkpseq(vap, addr, seq) != 0) {
128959aa14a9SRui Paulo 			vap->iv_stats.is_rx_dup++;
129059aa14a9SRui Paulo 			goto out;
129159aa14a9SRui Paulo 		}
129259aa14a9SRui Paulo 
129359aa14a9SRui Paulo 		/*
129459aa14a9SRui Paulo 		 * Potentially forward packet.  See table s36 (p140)
129559aa14a9SRui Paulo 		 * for the rules.  XXX tap fwd'd packets not for us?
129659aa14a9SRui Paulo 		 */
129759aa14a9SRui Paulo 		if (dir == IEEE80211_FC1_DIR_FROMDS ||
1298c104cff2SRui Paulo 		    !mesh_isucastforme(vap, wh, mc)) {
129959aa14a9SRui Paulo 			mesh_forward(vap, m, mc);
130059aa14a9SRui Paulo 			if (dir == IEEE80211_FC1_DIR_DSTODS)
130159aa14a9SRui Paulo 				goto out;
130259aa14a9SRui Paulo 			/* NB: fall thru to deliver mcast frames locally */
130359aa14a9SRui Paulo 		}
130459aa14a9SRui Paulo 
130559aa14a9SRui Paulo 		if (ieee80211_radiotap_active_vap(vap))
130659aa14a9SRui Paulo 			ieee80211_radiotap_rx(vap, m);
130759aa14a9SRui Paulo 		need_tap = 0;
130859aa14a9SRui Paulo 
130959aa14a9SRui Paulo 		/*
131059aa14a9SRui Paulo 		 * Finally, strip the 802.11 header.
131159aa14a9SRui Paulo 		 */
1312c104cff2SRui Paulo 		m = mesh_decap(vap, m, hdrspace, meshdrlen);
131359aa14a9SRui Paulo 		if (m == NULL) {
131459aa14a9SRui Paulo 			/* XXX mask bit to check for both */
131559aa14a9SRui Paulo 			/* don't count Null data frames as errors */
131659aa14a9SRui Paulo 			if (subtype == IEEE80211_FC0_SUBTYPE_NODATA ||
131759aa14a9SRui Paulo 			    subtype == IEEE80211_FC0_SUBTYPE_QOS_NULL)
131859aa14a9SRui Paulo 				goto out;
131959aa14a9SRui Paulo 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
132059aa14a9SRui Paulo 			    ni->ni_macaddr, "data", "%s", "decap error");
132159aa14a9SRui Paulo 			vap->iv_stats.is_rx_decap++;
132259aa14a9SRui Paulo 			IEEE80211_NODE_STAT(ni, rx_decap);
132359aa14a9SRui Paulo 			goto err;
132459aa14a9SRui Paulo 		}
132591216c71SAdrian Chadd 		if (qos[0] & IEEE80211_QOS_AMSDU) {
132659aa14a9SRui Paulo 			m = ieee80211_decap_amsdu(ni, m);
132759aa14a9SRui Paulo 			if (m == NULL)
132859aa14a9SRui Paulo 				return IEEE80211_FC0_TYPE_DATA;
132959aa14a9SRui Paulo 		}
133059aa14a9SRui Paulo 		ieee80211_deliver_data(vap, ni, m);
133159aa14a9SRui Paulo 		return type;
133259aa14a9SRui Paulo 	case IEEE80211_FC0_TYPE_MGT:
133359aa14a9SRui Paulo 		vap->iv_stats.is_rx_mgmt++;
133459aa14a9SRui Paulo 		IEEE80211_NODE_STAT(ni, rx_mgmt);
133559aa14a9SRui Paulo 		if (dir != IEEE80211_FC1_DIR_NODS) {
133659aa14a9SRui Paulo 			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
133759aa14a9SRui Paulo 			    wh, "mgt", "incorrect dir 0x%x", dir);
133859aa14a9SRui Paulo 			vap->iv_stats.is_rx_wrongdir++;
133959aa14a9SRui Paulo 			goto err;
134059aa14a9SRui Paulo 		}
134159aa14a9SRui Paulo 		if (m->m_pkthdr.len < sizeof(struct ieee80211_frame)) {
134259aa14a9SRui Paulo 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
134359aa14a9SRui Paulo 			    ni->ni_macaddr, "mgt", "too short: len %u",
134459aa14a9SRui Paulo 			    m->m_pkthdr.len);
134559aa14a9SRui Paulo 			vap->iv_stats.is_rx_tooshort++;
134659aa14a9SRui Paulo 			goto out;
134759aa14a9SRui Paulo 		}
134859aa14a9SRui Paulo #ifdef IEEE80211_DEBUG
134959aa14a9SRui Paulo 		if ((ieee80211_msg_debug(vap) &&
135059aa14a9SRui Paulo 		    (vap->iv_ic->ic_flags & IEEE80211_F_SCAN)) ||
135159aa14a9SRui Paulo 		    ieee80211_msg_dumppkts(vap)) {
135259aa14a9SRui Paulo 			if_printf(ifp, "received %s from %s rssi %d\n",
135359aa14a9SRui Paulo 			    ieee80211_mgt_subtype_name[subtype >>
135459aa14a9SRui Paulo 			    IEEE80211_FC0_SUBTYPE_SHIFT],
135559aa14a9SRui Paulo 			    ether_sprintf(wh->i_addr2), rssi);
135659aa14a9SRui Paulo 		}
135759aa14a9SRui Paulo #endif
135859aa14a9SRui Paulo 		if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
135959aa14a9SRui Paulo 			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
136059aa14a9SRui Paulo 			    wh, NULL, "%s", "WEP set but not permitted");
136159aa14a9SRui Paulo 			vap->iv_stats.is_rx_mgtdiscard++; /* XXX */
136259aa14a9SRui Paulo 			goto out;
136359aa14a9SRui Paulo 		}
136459aa14a9SRui Paulo 		vap->iv_recv_mgmt(ni, m, subtype, rssi, nf);
136559aa14a9SRui Paulo 		goto out;
136659aa14a9SRui Paulo 	case IEEE80211_FC0_TYPE_CTL:
136759aa14a9SRui Paulo 		vap->iv_stats.is_rx_ctl++;
136859aa14a9SRui Paulo 		IEEE80211_NODE_STAT(ni, rx_ctrl);
136959aa14a9SRui Paulo 		goto out;
137059aa14a9SRui Paulo 	default:
137159aa14a9SRui Paulo 		IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
137259aa14a9SRui Paulo 		    wh, "bad", "frame type 0x%x", type);
137359aa14a9SRui Paulo 		/* should not come here */
137459aa14a9SRui Paulo 		break;
137559aa14a9SRui Paulo 	}
137659aa14a9SRui Paulo err:
137759aa14a9SRui Paulo 	ifp->if_ierrors++;
137859aa14a9SRui Paulo out:
137959aa14a9SRui Paulo 	if (m != NULL) {
138059aa14a9SRui Paulo 		if (need_tap && ieee80211_radiotap_active_vap(vap))
138159aa14a9SRui Paulo 			ieee80211_radiotap_rx(vap, m);
138259aa14a9SRui Paulo 		m_freem(m);
138359aa14a9SRui Paulo 	}
138459aa14a9SRui Paulo 	return type;
138559aa14a9SRui Paulo }
138659aa14a9SRui Paulo 
138759aa14a9SRui Paulo static void
138859aa14a9SRui Paulo mesh_recv_mgmt(struct ieee80211_node *ni, struct mbuf *m0, int subtype,
138959aa14a9SRui Paulo     int rssi, int nf)
139059aa14a9SRui Paulo {
139159aa14a9SRui Paulo 	struct ieee80211vap *vap = ni->ni_vap;
139259aa14a9SRui Paulo 	struct ieee80211_mesh_state *ms = vap->iv_mesh;
139359aa14a9SRui Paulo 	struct ieee80211com *ic = ni->ni_ic;
139459aa14a9SRui Paulo 	struct ieee80211_frame *wh;
1395*b5df85a6SMonthadar Al Jaberi 	struct ieee80211_mesh_route *rt;
139659aa14a9SRui Paulo 	uint8_t *frm, *efrm;
139759aa14a9SRui Paulo 
139859aa14a9SRui Paulo 	wh = mtod(m0, struct ieee80211_frame *);
139959aa14a9SRui Paulo 	frm = (uint8_t *)&wh[1];
140059aa14a9SRui Paulo 	efrm = mtod(m0, uint8_t *) + m0->m_len;
140159aa14a9SRui Paulo 	switch (subtype) {
140259aa14a9SRui Paulo 	case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
140359aa14a9SRui Paulo 	case IEEE80211_FC0_SUBTYPE_BEACON:
140459aa14a9SRui Paulo 	{
140559aa14a9SRui Paulo 		struct ieee80211_scanparams scan;
140659aa14a9SRui Paulo 		/*
140759aa14a9SRui Paulo 		 * We process beacon/probe response
140859aa14a9SRui Paulo 		 * frames to discover neighbors.
140959aa14a9SRui Paulo 		 */
141059aa14a9SRui Paulo 		if (ieee80211_parse_beacon(ni, m0, &scan) != 0)
141159aa14a9SRui Paulo 			return;
141259aa14a9SRui Paulo 		/*
141359aa14a9SRui Paulo 		 * Count frame now that we know it's to be processed.
141459aa14a9SRui Paulo 		 */
141559aa14a9SRui Paulo 		if (subtype == IEEE80211_FC0_SUBTYPE_BEACON) {
141659aa14a9SRui Paulo 			vap->iv_stats.is_rx_beacon++;	/* XXX remove */
141759aa14a9SRui Paulo 			IEEE80211_NODE_STAT(ni, rx_beacons);
141859aa14a9SRui Paulo 		} else
141959aa14a9SRui Paulo 			IEEE80211_NODE_STAT(ni, rx_proberesp);
142059aa14a9SRui Paulo 		/*
142159aa14a9SRui Paulo 		 * If scanning, just pass information to the scan module.
142259aa14a9SRui Paulo 		 */
142359aa14a9SRui Paulo 		if (ic->ic_flags & IEEE80211_F_SCAN) {
142459aa14a9SRui Paulo 			if (ic->ic_flags_ext & IEEE80211_FEXT_PROBECHAN) {
142559aa14a9SRui Paulo 				/*
142659aa14a9SRui Paulo 				 * Actively scanning a channel marked passive;
142759aa14a9SRui Paulo 				 * send a probe request now that we know there
142859aa14a9SRui Paulo 				 * is 802.11 traffic present.
142959aa14a9SRui Paulo 				 *
143059aa14a9SRui Paulo 				 * XXX check if the beacon we recv'd gives
143159aa14a9SRui Paulo 				 * us what we need and suppress the probe req
143259aa14a9SRui Paulo 				 */
143359aa14a9SRui Paulo 				ieee80211_probe_curchan(vap, 1);
143459aa14a9SRui Paulo 				ic->ic_flags_ext &= ~IEEE80211_FEXT_PROBECHAN;
143559aa14a9SRui Paulo 			}
143659aa14a9SRui Paulo 			ieee80211_add_scan(vap, &scan, wh,
143759aa14a9SRui Paulo 			    subtype, rssi, nf);
143859aa14a9SRui Paulo 			return;
143959aa14a9SRui Paulo 		}
144059aa14a9SRui Paulo 
144159aa14a9SRui Paulo 		/* The rest of this code assumes we are running */
144259aa14a9SRui Paulo 		if (vap->iv_state != IEEE80211_S_RUN)
144359aa14a9SRui Paulo 			return;
144459aa14a9SRui Paulo 		/*
144559aa14a9SRui Paulo 		 * Ignore non-mesh STAs.
144659aa14a9SRui Paulo 		 */
144759aa14a9SRui Paulo 		if ((scan.capinfo &
144859aa14a9SRui Paulo 		     (IEEE80211_CAPINFO_ESS|IEEE80211_CAPINFO_IBSS)) ||
144959aa14a9SRui Paulo 		    scan.meshid == NULL || scan.meshconf == NULL) {
145059aa14a9SRui Paulo 			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
145159aa14a9SRui Paulo 			    wh, "beacon", "%s", "not a mesh sta");
145259aa14a9SRui Paulo 			vap->iv_stats.is_mesh_wrongmesh++;
145359aa14a9SRui Paulo 			return;
145459aa14a9SRui Paulo 		}
145559aa14a9SRui Paulo 		/*
145659aa14a9SRui Paulo 		 * Ignore STAs for other mesh networks.
145759aa14a9SRui Paulo 		 */
145859aa14a9SRui Paulo 		if (memcmp(scan.meshid+2, ms->ms_id, ms->ms_idlen) != 0 ||
145959aa14a9SRui Paulo 		    mesh_verify_meshconf(vap, scan.meshconf)) {
146059aa14a9SRui Paulo 			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
146159aa14a9SRui Paulo 			    wh, "beacon", "%s", "not for our mesh");
146259aa14a9SRui Paulo 			vap->iv_stats.is_mesh_wrongmesh++;
146359aa14a9SRui Paulo 			return;
146459aa14a9SRui Paulo 		}
146559aa14a9SRui Paulo 		/*
146659aa14a9SRui Paulo 		 * Peer only based on the current ACL policy.
146759aa14a9SRui Paulo 		 */
14685a8801b0SBernhard Schmidt 		if (vap->iv_acl != NULL && !vap->iv_acl->iac_check(vap, wh)) {
146959aa14a9SRui Paulo 			IEEE80211_DISCARD(vap, IEEE80211_MSG_ACL,
147059aa14a9SRui Paulo 			    wh, NULL, "%s", "disallowed by ACL");
147159aa14a9SRui Paulo 			vap->iv_stats.is_rx_acl++;
147259aa14a9SRui Paulo 			return;
147359aa14a9SRui Paulo 		}
147459aa14a9SRui Paulo 		/*
147559aa14a9SRui Paulo 		 * Do neighbor discovery.
147659aa14a9SRui Paulo 		 */
147759aa14a9SRui Paulo 		if (!IEEE80211_ADDR_EQ(wh->i_addr2, ni->ni_macaddr)) {
147859aa14a9SRui Paulo 			/*
147959aa14a9SRui Paulo 			 * Create a new entry in the neighbor table.
148059aa14a9SRui Paulo 			 */
148159aa14a9SRui Paulo 			ni = ieee80211_add_neighbor(vap, wh, &scan);
148259aa14a9SRui Paulo 		}
148359aa14a9SRui Paulo 		/*
148459aa14a9SRui Paulo 		 * Automatically peer with discovered nodes if possible.
148559aa14a9SRui Paulo 		 * XXX backoff on repeated failure
148659aa14a9SRui Paulo 		 */
148759aa14a9SRui Paulo 		if (ni != vap->iv_bss &&
1488*b5df85a6SMonthadar Al Jaberi 		    (ms->ms_flags & IEEE80211_MESHFLAGS_AP)) {
1489*b5df85a6SMonthadar Al Jaberi 			switch (ni->ni_mlstate) {
1490*b5df85a6SMonthadar Al Jaberi 			case IEEE80211_NODE_MESH_IDLE:
1491*b5df85a6SMonthadar Al Jaberi 			{
149259aa14a9SRui Paulo 				uint16_t args[1];
149359aa14a9SRui Paulo 
149459aa14a9SRui Paulo 				ni->ni_mlpid = mesh_generateid(vap);
149559aa14a9SRui Paulo 				if (ni->ni_mlpid == 0)
149659aa14a9SRui Paulo 					return;
149759aa14a9SRui Paulo 				mesh_linkchange(ni, IEEE80211_NODE_MESH_OPENSNT);
149859aa14a9SRui Paulo 				args[0] = ni->ni_mlpid;
149959aa14a9SRui Paulo 				ieee80211_send_action(ni,
1500ebeaa1adSMonthadar Al Jaberi 				IEEE80211_ACTION_CAT_SELF_PROT,
150159aa14a9SRui Paulo 				IEEE80211_ACTION_MESHPEERING_OPEN, args);
150259aa14a9SRui Paulo 				ni->ni_mlrcnt = 0;
150359aa14a9SRui Paulo 				mesh_peer_timeout_setup(ni);
1504*b5df85a6SMonthadar Al Jaberi 				break;
1505*b5df85a6SMonthadar Al Jaberi 			}
1506*b5df85a6SMonthadar Al Jaberi 			case IEEE80211_NODE_MESH_ESTABLISHED:
1507*b5df85a6SMonthadar Al Jaberi 			{
1508*b5df85a6SMonthadar Al Jaberi 				/*
1509*b5df85a6SMonthadar Al Jaberi 				 * Valid beacon from a peer mesh STA
1510*b5df85a6SMonthadar Al Jaberi 				 * bump TA lifetime
1511*b5df85a6SMonthadar Al Jaberi 				 */
1512*b5df85a6SMonthadar Al Jaberi 				rt = ieee80211_mesh_rt_find(vap, wh->i_addr2);
1513*b5df85a6SMonthadar Al Jaberi 				if(rt != NULL) {
1514*b5df85a6SMonthadar Al Jaberi 					ieee80211_mesh_rt_update(rt,
1515*b5df85a6SMonthadar Al Jaberi 					    ms->ms_ppath->mpp_inact);
1516*b5df85a6SMonthadar Al Jaberi 				}
1517*b5df85a6SMonthadar Al Jaberi 				break;
1518*b5df85a6SMonthadar Al Jaberi 			}
1519*b5df85a6SMonthadar Al Jaberi 			default:
1520*b5df85a6SMonthadar Al Jaberi 				break; /* ignore */
1521*b5df85a6SMonthadar Al Jaberi 			}
152259aa14a9SRui Paulo 		}
152359aa14a9SRui Paulo 		break;
152459aa14a9SRui Paulo 	}
152559aa14a9SRui Paulo 	case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
152659aa14a9SRui Paulo 	{
152759aa14a9SRui Paulo 		uint8_t *ssid, *meshid, *rates, *xrates;
152859aa14a9SRui Paulo 		uint8_t *sfrm;
152959aa14a9SRui Paulo 
153059aa14a9SRui Paulo 		if (vap->iv_state != IEEE80211_S_RUN) {
153159aa14a9SRui Paulo 			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
153259aa14a9SRui Paulo 			    wh, NULL, "wrong state %s",
153359aa14a9SRui Paulo 			    ieee80211_state_name[vap->iv_state]);
153459aa14a9SRui Paulo 			vap->iv_stats.is_rx_mgtdiscard++;
153559aa14a9SRui Paulo 			return;
153659aa14a9SRui Paulo 		}
153759aa14a9SRui Paulo 		if (IEEE80211_IS_MULTICAST(wh->i_addr2)) {
153859aa14a9SRui Paulo 			/* frame must be directed */
153959aa14a9SRui Paulo 			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
154059aa14a9SRui Paulo 			    wh, NULL, "%s", "not unicast");
154159aa14a9SRui Paulo 			vap->iv_stats.is_rx_mgtdiscard++;	/* XXX stat */
154259aa14a9SRui Paulo 			return;
154359aa14a9SRui Paulo 		}
154459aa14a9SRui Paulo 		/*
154559aa14a9SRui Paulo 		 * prreq frame format
154659aa14a9SRui Paulo 		 *      [tlv] ssid
154759aa14a9SRui Paulo 		 *      [tlv] supported rates
154859aa14a9SRui Paulo 		 *      [tlv] extended supported rates
154959aa14a9SRui Paulo 		 *	[tlv] mesh id
155059aa14a9SRui Paulo 		 */
155159aa14a9SRui Paulo 		ssid = meshid = rates = xrates = NULL;
155259aa14a9SRui Paulo 		sfrm = frm;
155359aa14a9SRui Paulo 		while (efrm - frm > 1) {
155459aa14a9SRui Paulo 			IEEE80211_VERIFY_LENGTH(efrm - frm, frm[1] + 2, return);
155559aa14a9SRui Paulo 			switch (*frm) {
155659aa14a9SRui Paulo 			case IEEE80211_ELEMID_SSID:
155759aa14a9SRui Paulo 				ssid = frm;
155859aa14a9SRui Paulo 				break;
155959aa14a9SRui Paulo 			case IEEE80211_ELEMID_RATES:
156059aa14a9SRui Paulo 				rates = frm;
156159aa14a9SRui Paulo 				break;
156259aa14a9SRui Paulo 			case IEEE80211_ELEMID_XRATES:
156359aa14a9SRui Paulo 				xrates = frm;
156459aa14a9SRui Paulo 				break;
156559aa14a9SRui Paulo 			case IEEE80211_ELEMID_MESHID:
156659aa14a9SRui Paulo 				meshid = frm;
156759aa14a9SRui Paulo 				break;
156859aa14a9SRui Paulo 			}
1569d3befdecSBernhard Schmidt 			frm += frm[1] + 2;
157059aa14a9SRui Paulo 		}
157159aa14a9SRui Paulo 		IEEE80211_VERIFY_ELEMENT(ssid, IEEE80211_NWID_LEN, return);
157259aa14a9SRui Paulo 		IEEE80211_VERIFY_ELEMENT(rates, IEEE80211_RATE_MAXSIZE, return);
157359aa14a9SRui Paulo 		if (xrates != NULL)
157459aa14a9SRui Paulo 			IEEE80211_VERIFY_ELEMENT(xrates,
157559aa14a9SRui Paulo 			    IEEE80211_RATE_MAXSIZE - rates[1], return);
15766c946257SRui Paulo 		if (meshid != NULL) {
157759aa14a9SRui Paulo 			IEEE80211_VERIFY_ELEMENT(meshid,
157859aa14a9SRui Paulo 			    IEEE80211_MESHID_LEN, return);
157959aa14a9SRui Paulo 			/* NB: meshid, not ssid */
158059aa14a9SRui Paulo 			IEEE80211_VERIFY_SSID(vap->iv_bss, meshid, return);
15816c946257SRui Paulo 		}
158259aa14a9SRui Paulo 
158359aa14a9SRui Paulo 		/* XXX find a better class or define it's own */
158459aa14a9SRui Paulo 		IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_INPUT, wh->i_addr2,
158559aa14a9SRui Paulo 		    "%s", "recv probe req");
158659aa14a9SRui Paulo 		/*
158759aa14a9SRui Paulo 		 * Some legacy 11b clients cannot hack a complete
158859aa14a9SRui Paulo 		 * probe response frame.  When the request includes
158959aa14a9SRui Paulo 		 * only a bare-bones rate set, communicate this to
159059aa14a9SRui Paulo 		 * the transmit side.
159159aa14a9SRui Paulo 		 */
159259aa14a9SRui Paulo 		ieee80211_send_proberesp(vap, wh->i_addr2, 0);
159359aa14a9SRui Paulo 		break;
159459aa14a9SRui Paulo 	}
159596283082SBernhard Schmidt 
159659aa14a9SRui Paulo 	case IEEE80211_FC0_SUBTYPE_ACTION:
159796283082SBernhard Schmidt 	case IEEE80211_FC0_SUBTYPE_ACTION_NOACK:
159859aa14a9SRui Paulo 		if (ni == vap->iv_bss) {
1599893c4d6eSBernhard Schmidt 			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
160059aa14a9SRui Paulo 			    wh, NULL, "%s", "unknown node");
160159aa14a9SRui Paulo 			vap->iv_stats.is_rx_mgtdiscard++;
1602893c4d6eSBernhard Schmidt 		} else if (!IEEE80211_ADDR_EQ(vap->iv_myaddr, wh->i_addr1) &&
160359aa14a9SRui Paulo 		    !IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1604893c4d6eSBernhard Schmidt 			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
1605893c4d6eSBernhard Schmidt 			    wh, NULL, "%s", "not for us");
160659aa14a9SRui Paulo 			vap->iv_stats.is_rx_mgtdiscard++;
1607893c4d6eSBernhard Schmidt 		} else if (vap->iv_state != IEEE80211_S_RUN) {
160896283082SBernhard Schmidt 			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
160996283082SBernhard Schmidt 			    wh, NULL, "wrong state %s",
161096283082SBernhard Schmidt 			    ieee80211_state_name[vap->iv_state]);
161196283082SBernhard Schmidt 			vap->iv_stats.is_rx_mgtdiscard++;
1612893c4d6eSBernhard Schmidt 		} else {
1613893c4d6eSBernhard Schmidt 			if (ieee80211_parse_action(ni, m0) == 0)
1614893c4d6eSBernhard Schmidt 				(void)ic->ic_recv_action(ni, wh, frm, efrm);
161596283082SBernhard Schmidt 		}
161659aa14a9SRui Paulo 		break;
161796283082SBernhard Schmidt 
161859aa14a9SRui Paulo 	case IEEE80211_FC0_SUBTYPE_ASSOC_REQ:
161959aa14a9SRui Paulo 	case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
162096283082SBernhard Schmidt 	case IEEE80211_FC0_SUBTYPE_REASSOC_REQ:
162159aa14a9SRui Paulo 	case IEEE80211_FC0_SUBTYPE_REASSOC_RESP:
162296283082SBernhard Schmidt 	case IEEE80211_FC0_SUBTYPE_ATIM:
162359aa14a9SRui Paulo 	case IEEE80211_FC0_SUBTYPE_DISASSOC:
162496283082SBernhard Schmidt 	case IEEE80211_FC0_SUBTYPE_AUTH:
162596283082SBernhard Schmidt 	case IEEE80211_FC0_SUBTYPE_DEAUTH:
162659aa14a9SRui Paulo 		IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
162759aa14a9SRui Paulo 		    wh, NULL, "%s", "not handled");
162859aa14a9SRui Paulo 		vap->iv_stats.is_rx_mgtdiscard++;
162996283082SBernhard Schmidt 		break;
163096283082SBernhard Schmidt 
163159aa14a9SRui Paulo 	default:
163259aa14a9SRui Paulo 		IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
163359aa14a9SRui Paulo 		    wh, "mgt", "subtype 0x%x not handled", subtype);
163459aa14a9SRui Paulo 		vap->iv_stats.is_rx_badsubtype++;
163559aa14a9SRui Paulo 		break;
163659aa14a9SRui Paulo 	}
163759aa14a9SRui Paulo }
163859aa14a9SRui Paulo 
16390917631fSRui Paulo static void
16400917631fSRui Paulo mesh_recv_ctl(struct ieee80211_node *ni, struct mbuf *m, int subtype)
16410917631fSRui Paulo {
16420917631fSRui Paulo 
16430917631fSRui Paulo 	switch (subtype) {
16440917631fSRui Paulo 	case IEEE80211_FC0_SUBTYPE_BAR:
16450917631fSRui Paulo 		ieee80211_recv_bar(ni, m);
16460917631fSRui Paulo 		break;
16470917631fSRui Paulo 	}
16480917631fSRui Paulo }
16490917631fSRui Paulo 
165059aa14a9SRui Paulo /*
16516eb9b443SMonthadar Al Jaberi  * Parse meshpeering action ie's for MPM frames
165259aa14a9SRui Paulo  */
165359aa14a9SRui Paulo static const struct ieee80211_meshpeer_ie *
165459aa14a9SRui Paulo mesh_parse_meshpeering_action(struct ieee80211_node *ni,
165559aa14a9SRui Paulo 	const struct ieee80211_frame *wh,	/* XXX for VERIFY_LENGTH */
165659aa14a9SRui Paulo 	const uint8_t *frm, const uint8_t *efrm,
1657c77735e2SRui Paulo 	struct ieee80211_meshpeer_ie *mp, uint8_t subtype)
165859aa14a9SRui Paulo {
165959aa14a9SRui Paulo 	struct ieee80211vap *vap = ni->ni_vap;
166059aa14a9SRui Paulo 	const struct ieee80211_meshpeer_ie *mpie;
16616eb9b443SMonthadar Al Jaberi 	uint16_t args[3];
1662c77735e2SRui Paulo 	const uint8_t *meshid, *meshconf, *meshpeer;
16636eb9b443SMonthadar Al Jaberi 	uint8_t sendclose = 0; /* 1 = MPM frame rejected, close will be sent */
166459aa14a9SRui Paulo 
1665c77735e2SRui Paulo 	meshid = meshconf = meshpeer = NULL;
166659aa14a9SRui Paulo 	while (efrm - frm > 1) {
166759aa14a9SRui Paulo 		IEEE80211_VERIFY_LENGTH(efrm - frm, frm[1] + 2, return NULL);
166859aa14a9SRui Paulo 		switch (*frm) {
166959aa14a9SRui Paulo 		case IEEE80211_ELEMID_MESHID:
167059aa14a9SRui Paulo 			meshid = frm;
167159aa14a9SRui Paulo 			break;
167259aa14a9SRui Paulo 		case IEEE80211_ELEMID_MESHCONF:
167359aa14a9SRui Paulo 			meshconf = frm;
167459aa14a9SRui Paulo 			break;
167559aa14a9SRui Paulo 		case IEEE80211_ELEMID_MESHPEER:
167659aa14a9SRui Paulo 			meshpeer = frm;
167759aa14a9SRui Paulo 			mpie = (const struct ieee80211_meshpeer_ie *) frm;
167859aa14a9SRui Paulo 			memset(mp, 0, sizeof(*mp));
16796eb9b443SMonthadar Al Jaberi 			mp->peer_len = mpie->peer_len;
1680c2042c35SMonthadar Al Jaberi 			mp->peer_proto = LE_READ_2(&mpie->peer_proto);
168159aa14a9SRui Paulo 			mp->peer_llinkid = LE_READ_2(&mpie->peer_llinkid);
1682c2042c35SMonthadar Al Jaberi 			switch (subtype) {
1683c2042c35SMonthadar Al Jaberi 			case IEEE80211_ACTION_MESHPEERING_CONFIRM:
1684c2042c35SMonthadar Al Jaberi 				mp->peer_linkid =
1685c2042c35SMonthadar Al Jaberi 				    LE_READ_2(&mpie->peer_linkid);
1686c2042c35SMonthadar Al Jaberi 				break;
1687c2042c35SMonthadar Al Jaberi 			case IEEE80211_ACTION_MESHPEERING_CLOSE:
1688c2042c35SMonthadar Al Jaberi 				/* NB: peer link ID is optional */
1689c2042c35SMonthadar Al Jaberi 				if (mpie->peer_len ==
1690c2042c35SMonthadar Al Jaberi 				    (IEEE80211_MPM_BASE_SZ + 2)) {
169159aa14a9SRui Paulo 					mp->peer_linkid = 0;
1692c2042c35SMonthadar Al Jaberi 					mp->peer_rcode =
1693c2042c35SMonthadar Al Jaberi 					    LE_READ_2(&mpie->peer_linkid);
169459aa14a9SRui Paulo 				} else {
1695c2042c35SMonthadar Al Jaberi 					mp->peer_linkid =
1696c2042c35SMonthadar Al Jaberi 					    LE_READ_2(&mpie->peer_linkid);
1697c2042c35SMonthadar Al Jaberi 					mp->peer_rcode =
1698c2042c35SMonthadar Al Jaberi 					    LE_READ_2(&mpie->peer_rcode);
1699c2042c35SMonthadar Al Jaberi 				}
1700c2042c35SMonthadar Al Jaberi 				break;
170159aa14a9SRui Paulo 			}
170259aa14a9SRui Paulo 			break;
170359aa14a9SRui Paulo 		}
170459aa14a9SRui Paulo 		frm += frm[1] + 2;
170559aa14a9SRui Paulo 	}
170659aa14a9SRui Paulo 
170759aa14a9SRui Paulo 	/*
17086eb9b443SMonthadar Al Jaberi 	 * Verify the contents of the frame.
17096eb9b443SMonthadar Al Jaberi 	 * If it fails validation, close the peer link.
171059aa14a9SRui Paulo 	 */
17116eb9b443SMonthadar Al Jaberi 	if (mesh_verify_meshpeer(vap, subtype, (const uint8_t *)mp)) {
17126eb9b443SMonthadar Al Jaberi 		sendclose = 1;
17136eb9b443SMonthadar Al Jaberi 		IEEE80211_DISCARD(vap,
17146eb9b443SMonthadar Al Jaberi 		    IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH,
17156eb9b443SMonthadar Al Jaberi 		    wh, NULL, "%s", "MPM validation failed");
17166eb9b443SMonthadar Al Jaberi 	}
171759aa14a9SRui Paulo 
17186eb9b443SMonthadar Al Jaberi 	/* If meshid is not the same reject any frames type. */
17196eb9b443SMonthadar Al Jaberi 	if (sendclose == 0 && mesh_verify_meshid(vap, meshid)) {
17206eb9b443SMonthadar Al Jaberi 		sendclose = 1;
172159aa14a9SRui Paulo 		IEEE80211_DISCARD(vap,
172259aa14a9SRui Paulo 		    IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH,
172359aa14a9SRui Paulo 		    wh, NULL, "%s", "not for our mesh");
17246eb9b443SMonthadar Al Jaberi 		if (subtype == IEEE80211_ACTION_MESHPEERING_CLOSE) {
17256eb9b443SMonthadar Al Jaberi 			/*
17266eb9b443SMonthadar Al Jaberi 			 * Standard not clear about this, if we dont ignore
17276eb9b443SMonthadar Al Jaberi 			 * there will be an endless loop between nodes sending
17286eb9b443SMonthadar Al Jaberi 			 * CLOSE frames between each other with wrong meshid.
17296eb9b443SMonthadar Al Jaberi 			 * Discard and timers will bring FSM to IDLE state.
17306eb9b443SMonthadar Al Jaberi 			 */
17316eb9b443SMonthadar Al Jaberi 			return NULL;
17326eb9b443SMonthadar Al Jaberi 		}
17336eb9b443SMonthadar Al Jaberi 	}
17346eb9b443SMonthadar Al Jaberi 
17356eb9b443SMonthadar Al Jaberi 	/*
17366eb9b443SMonthadar Al Jaberi 	 * Close frames are accepted if meshid is the same.
17376eb9b443SMonthadar Al Jaberi 	 * Verify the other two types.
17386eb9b443SMonthadar Al Jaberi 	 */
17396eb9b443SMonthadar Al Jaberi 	if (sendclose == 0 && subtype != IEEE80211_ACTION_MESHPEERING_CLOSE &&
17406eb9b443SMonthadar Al Jaberi 	    mesh_verify_meshconf(vap, meshconf)) {
17416eb9b443SMonthadar Al Jaberi 		sendclose = 1;
17426eb9b443SMonthadar Al Jaberi 		IEEE80211_DISCARD(vap,
17436eb9b443SMonthadar Al Jaberi 		    IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH,
17446eb9b443SMonthadar Al Jaberi 		    wh, NULL, "%s", "configuration missmatch");
17456eb9b443SMonthadar Al Jaberi 	}
17466eb9b443SMonthadar Al Jaberi 
17476eb9b443SMonthadar Al Jaberi 	if (sendclose) {
174859aa14a9SRui Paulo 		vap->iv_stats.is_rx_mgtdiscard++;
174959aa14a9SRui Paulo 		switch (ni->ni_mlstate) {
175059aa14a9SRui Paulo 		case IEEE80211_NODE_MESH_IDLE:
175159aa14a9SRui Paulo 		case IEEE80211_NODE_MESH_ESTABLISHED:
175259aa14a9SRui Paulo 		case IEEE80211_NODE_MESH_HOLDING:
175359aa14a9SRui Paulo 			/* ignore */
175459aa14a9SRui Paulo 			break;
175559aa14a9SRui Paulo 		case IEEE80211_NODE_MESH_OPENSNT:
175659aa14a9SRui Paulo 		case IEEE80211_NODE_MESH_OPENRCV:
175759aa14a9SRui Paulo 		case IEEE80211_NODE_MESH_CONFIRMRCV:
175859aa14a9SRui Paulo 			args[0] = ni->ni_mlpid;
175959aa14a9SRui Paulo 			args[1] = ni->ni_mllid;
17606eb9b443SMonthadar Al Jaberi 			/* Reason codes for rejection */
17616eb9b443SMonthadar Al Jaberi 			switch (subtype) {
17626eb9b443SMonthadar Al Jaberi 			case IEEE80211_ACTION_MESHPEERING_OPEN:
17636eb9b443SMonthadar Al Jaberi 				args[2] = IEEE80211_REASON_MESH_CPVIOLATION;
17646eb9b443SMonthadar Al Jaberi 				break;
17656eb9b443SMonthadar Al Jaberi 			case IEEE80211_ACTION_MESHPEERING_CONFIRM:
17666eb9b443SMonthadar Al Jaberi 				args[2] = IEEE80211_REASON_MESH_INCONS_PARAMS;
17676eb9b443SMonthadar Al Jaberi 				break;
17686eb9b443SMonthadar Al Jaberi 			}
176959aa14a9SRui Paulo 			ieee80211_send_action(ni,
1770ebeaa1adSMonthadar Al Jaberi 			    IEEE80211_ACTION_CAT_SELF_PROT,
177159aa14a9SRui Paulo 			    IEEE80211_ACTION_MESHPEERING_CLOSE,
177259aa14a9SRui Paulo 			    args);
177359aa14a9SRui Paulo 			mesh_linkchange(ni, IEEE80211_NODE_MESH_HOLDING);
177459aa14a9SRui Paulo 			mesh_peer_timeout_setup(ni);
177559aa14a9SRui Paulo 			break;
177659aa14a9SRui Paulo 		}
177759aa14a9SRui Paulo 		return NULL;
177859aa14a9SRui Paulo 	}
17796eb9b443SMonthadar Al Jaberi 
178059aa14a9SRui Paulo 	return (const struct ieee80211_meshpeer_ie *) mp;
178159aa14a9SRui Paulo }
178259aa14a9SRui Paulo 
178359aa14a9SRui Paulo static int
178459aa14a9SRui Paulo mesh_recv_action_meshpeering_open(struct ieee80211_node *ni,
178559aa14a9SRui Paulo 	const struct ieee80211_frame *wh,
178659aa14a9SRui Paulo 	const uint8_t *frm, const uint8_t *efrm)
178759aa14a9SRui Paulo {
178859aa14a9SRui Paulo 	struct ieee80211vap *vap = ni->ni_vap;
17896eb9b443SMonthadar Al Jaberi 	struct ieee80211_mesh_state *ms = vap->iv_mesh;
179059aa14a9SRui Paulo 	struct ieee80211_meshpeer_ie ie;
179159aa14a9SRui Paulo 	const struct ieee80211_meshpeer_ie *meshpeer;
179259aa14a9SRui Paulo 	uint16_t args[3];
179359aa14a9SRui Paulo 
179459aa14a9SRui Paulo 	/* +2+2 for action + code + capabilites */
1795c77735e2SRui Paulo 	meshpeer = mesh_parse_meshpeering_action(ni, wh, frm+2+2, efrm, &ie,
1796c77735e2SRui Paulo 	    IEEE80211_ACTION_MESHPEERING_OPEN);
179759aa14a9SRui Paulo 	if (meshpeer == NULL) {
179859aa14a9SRui Paulo 		return 0;
179959aa14a9SRui Paulo 	}
180059aa14a9SRui Paulo 
180159aa14a9SRui Paulo 	/* XXX move up */
180259aa14a9SRui Paulo 	IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH, ni,
180359aa14a9SRui Paulo 	    "recv PEER OPEN, lid 0x%x", meshpeer->peer_llinkid);
180459aa14a9SRui Paulo 
180559aa14a9SRui Paulo 	switch (ni->ni_mlstate) {
180659aa14a9SRui Paulo 	case IEEE80211_NODE_MESH_IDLE:
18076eb9b443SMonthadar Al Jaberi 		/* Reject open request if reached our maximum neighbor count */
18086eb9b443SMonthadar Al Jaberi 		if (ms->ms_neighbors >= IEEE80211_MESH_MAX_NEIGHBORS) {
18096eb9b443SMonthadar Al Jaberi 			args[0] = meshpeer->peer_llinkid;
18106eb9b443SMonthadar Al Jaberi 			args[1] = 0;
18116eb9b443SMonthadar Al Jaberi 			args[2] = IEEE80211_REASON_MESH_MAX_PEERS;
18126eb9b443SMonthadar Al Jaberi 			ieee80211_send_action(ni,
18136eb9b443SMonthadar Al Jaberi 			    IEEE80211_ACTION_CAT_SELF_PROT,
18146eb9b443SMonthadar Al Jaberi 			    IEEE80211_ACTION_MESHPEERING_CLOSE,
18156eb9b443SMonthadar Al Jaberi 			    args);
18166eb9b443SMonthadar Al Jaberi 			/* stay in IDLE state */
18176eb9b443SMonthadar Al Jaberi 			return (0);
18186eb9b443SMonthadar Al Jaberi 		}
18196eb9b443SMonthadar Al Jaberi 		/* Open frame accepted */
182059aa14a9SRui Paulo 		mesh_linkchange(ni, IEEE80211_NODE_MESH_OPENRCV);
182159aa14a9SRui Paulo 		ni->ni_mllid = meshpeer->peer_llinkid;
182259aa14a9SRui Paulo 		ni->ni_mlpid = mesh_generateid(vap);
182359aa14a9SRui Paulo 		if (ni->ni_mlpid == 0)
182459aa14a9SRui Paulo 			return 0;		/* XXX */
182559aa14a9SRui Paulo 		args[0] = ni->ni_mlpid;
182659aa14a9SRui Paulo 		/* Announce we're open too... */
182759aa14a9SRui Paulo 		ieee80211_send_action(ni,
1828ebeaa1adSMonthadar Al Jaberi 		    IEEE80211_ACTION_CAT_SELF_PROT,
182959aa14a9SRui Paulo 		    IEEE80211_ACTION_MESHPEERING_OPEN, args);
183059aa14a9SRui Paulo 		/* ...and confirm the link. */
183159aa14a9SRui Paulo 		args[0] = ni->ni_mlpid;
183259aa14a9SRui Paulo 		args[1] = ni->ni_mllid;
183359aa14a9SRui Paulo 		ieee80211_send_action(ni,
1834ebeaa1adSMonthadar Al Jaberi 		    IEEE80211_ACTION_CAT_SELF_PROT,
183559aa14a9SRui Paulo 		    IEEE80211_ACTION_MESHPEERING_CONFIRM,
183659aa14a9SRui Paulo 		    args);
183759aa14a9SRui Paulo 		mesh_peer_timeout_setup(ni);
183859aa14a9SRui Paulo 		break;
183959aa14a9SRui Paulo 	case IEEE80211_NODE_MESH_OPENRCV:
184059aa14a9SRui Paulo 		/* Wrong Link ID */
184159aa14a9SRui Paulo 		if (ni->ni_mllid != meshpeer->peer_llinkid) {
184259aa14a9SRui Paulo 			args[0] = ni->ni_mllid;
184359aa14a9SRui Paulo 			args[1] = ni->ni_mlpid;
184459aa14a9SRui Paulo 			args[2] = IEEE80211_REASON_PEER_LINK_CANCELED;
184559aa14a9SRui Paulo 			ieee80211_send_action(ni,
1846ebeaa1adSMonthadar Al Jaberi 			    IEEE80211_ACTION_CAT_SELF_PROT,
184759aa14a9SRui Paulo 			    IEEE80211_ACTION_MESHPEERING_CLOSE,
184859aa14a9SRui Paulo 			    args);
184959aa14a9SRui Paulo 			mesh_linkchange(ni, IEEE80211_NODE_MESH_HOLDING);
185059aa14a9SRui Paulo 			mesh_peer_timeout_setup(ni);
185159aa14a9SRui Paulo 			break;
185259aa14a9SRui Paulo 		}
185359aa14a9SRui Paulo 		/* Duplicate open, confirm again. */
185459aa14a9SRui Paulo 		args[0] = ni->ni_mlpid;
185559aa14a9SRui Paulo 		args[1] = ni->ni_mllid;
185659aa14a9SRui Paulo 		ieee80211_send_action(ni,
1857ebeaa1adSMonthadar Al Jaberi 		    IEEE80211_ACTION_CAT_SELF_PROT,
185859aa14a9SRui Paulo 		    IEEE80211_ACTION_MESHPEERING_CONFIRM,
185959aa14a9SRui Paulo 		    args);
186059aa14a9SRui Paulo 		break;
186159aa14a9SRui Paulo 	case IEEE80211_NODE_MESH_OPENSNT:
186259aa14a9SRui Paulo 		ni->ni_mllid = meshpeer->peer_llinkid;
186359aa14a9SRui Paulo 		mesh_linkchange(ni, IEEE80211_NODE_MESH_OPENRCV);
186459aa14a9SRui Paulo 		args[0] = ni->ni_mlpid;
186559aa14a9SRui Paulo 		args[1] = ni->ni_mllid;
186659aa14a9SRui Paulo 		ieee80211_send_action(ni,
1867ebeaa1adSMonthadar Al Jaberi 		    IEEE80211_ACTION_CAT_SELF_PROT,
186859aa14a9SRui Paulo 		    IEEE80211_ACTION_MESHPEERING_CONFIRM,
186959aa14a9SRui Paulo 		    args);
187059aa14a9SRui Paulo 		/* NB: don't setup/clear any timeout */
187159aa14a9SRui Paulo 		break;
187259aa14a9SRui Paulo 	case IEEE80211_NODE_MESH_CONFIRMRCV:
187359aa14a9SRui Paulo 		if (ni->ni_mlpid != meshpeer->peer_linkid ||
187459aa14a9SRui Paulo 		    ni->ni_mllid != meshpeer->peer_llinkid) {
187559aa14a9SRui Paulo 			args[0] = ni->ni_mlpid;
187659aa14a9SRui Paulo 			args[1] = ni->ni_mllid;
187759aa14a9SRui Paulo 			args[2] = IEEE80211_REASON_PEER_LINK_CANCELED;
187859aa14a9SRui Paulo 			ieee80211_send_action(ni,
1879ebeaa1adSMonthadar Al Jaberi 			    IEEE80211_ACTION_CAT_SELF_PROT,
188059aa14a9SRui Paulo 			    IEEE80211_ACTION_MESHPEERING_CLOSE,
188159aa14a9SRui Paulo 			    args);
188259aa14a9SRui Paulo 			mesh_linkchange(ni,
188359aa14a9SRui Paulo 			    IEEE80211_NODE_MESH_HOLDING);
188459aa14a9SRui Paulo 			mesh_peer_timeout_setup(ni);
188559aa14a9SRui Paulo 			break;
188659aa14a9SRui Paulo 		}
188759aa14a9SRui Paulo 		mesh_linkchange(ni, IEEE80211_NODE_MESH_ESTABLISHED);
188859aa14a9SRui Paulo 		ni->ni_mllid = meshpeer->peer_llinkid;
188959aa14a9SRui Paulo 		args[0] = ni->ni_mlpid;
189059aa14a9SRui Paulo 		args[1] = ni->ni_mllid;
189159aa14a9SRui Paulo 		ieee80211_send_action(ni,
1892ebeaa1adSMonthadar Al Jaberi 		    IEEE80211_ACTION_CAT_SELF_PROT,
189359aa14a9SRui Paulo 		    IEEE80211_ACTION_MESHPEERING_CONFIRM,
189459aa14a9SRui Paulo 		    args);
189559aa14a9SRui Paulo 		mesh_peer_timeout_stop(ni);
189659aa14a9SRui Paulo 		break;
189759aa14a9SRui Paulo 	case IEEE80211_NODE_MESH_ESTABLISHED:
189859aa14a9SRui Paulo 		if (ni->ni_mllid != meshpeer->peer_llinkid) {
189959aa14a9SRui Paulo 			args[0] = ni->ni_mllid;
190059aa14a9SRui Paulo 			args[1] = ni->ni_mlpid;
190159aa14a9SRui Paulo 			args[2] = IEEE80211_REASON_PEER_LINK_CANCELED;
190259aa14a9SRui Paulo 			ieee80211_send_action(ni,
1903ebeaa1adSMonthadar Al Jaberi 			    IEEE80211_ACTION_CAT_SELF_PROT,
190459aa14a9SRui Paulo 			    IEEE80211_ACTION_MESHPEERING_CLOSE,
190559aa14a9SRui Paulo 			    args);
190659aa14a9SRui Paulo 			mesh_linkchange(ni, IEEE80211_NODE_MESH_HOLDING);
190759aa14a9SRui Paulo 			mesh_peer_timeout_setup(ni);
190859aa14a9SRui Paulo 			break;
190959aa14a9SRui Paulo 		}
191059aa14a9SRui Paulo 		args[0] = ni->ni_mlpid;
191159aa14a9SRui Paulo 		args[1] = ni->ni_mllid;
191259aa14a9SRui Paulo 		ieee80211_send_action(ni,
1913ebeaa1adSMonthadar Al Jaberi 		    IEEE80211_ACTION_CAT_SELF_PROT,
191459aa14a9SRui Paulo 		    IEEE80211_ACTION_MESHPEERING_CONFIRM,
191559aa14a9SRui Paulo 		    args);
191659aa14a9SRui Paulo 		break;
191759aa14a9SRui Paulo 	case IEEE80211_NODE_MESH_HOLDING:
191859aa14a9SRui Paulo 		args[0] = ni->ni_mlpid;
191959aa14a9SRui Paulo 		args[1] = meshpeer->peer_llinkid;
19206eb9b443SMonthadar Al Jaberi 		/* Standard not clear about what the reaason code should be */
19216eb9b443SMonthadar Al Jaberi 		args[2] = IEEE80211_REASON_PEER_LINK_CANCELED;
192259aa14a9SRui Paulo 		ieee80211_send_action(ni,
1923ebeaa1adSMonthadar Al Jaberi 		    IEEE80211_ACTION_CAT_SELF_PROT,
192459aa14a9SRui Paulo 		    IEEE80211_ACTION_MESHPEERING_CLOSE,
192559aa14a9SRui Paulo 		    args);
192659aa14a9SRui Paulo 		break;
192759aa14a9SRui Paulo 	}
192859aa14a9SRui Paulo 	return 0;
192959aa14a9SRui Paulo }
193059aa14a9SRui Paulo 
193159aa14a9SRui Paulo static int
193259aa14a9SRui Paulo mesh_recv_action_meshpeering_confirm(struct ieee80211_node *ni,
193359aa14a9SRui Paulo 	const struct ieee80211_frame *wh,
193459aa14a9SRui Paulo 	const uint8_t *frm, const uint8_t *efrm)
193559aa14a9SRui Paulo {
193659aa14a9SRui Paulo 	struct ieee80211vap *vap = ni->ni_vap;
193759aa14a9SRui Paulo 	struct ieee80211_meshpeer_ie ie;
193859aa14a9SRui Paulo 	const struct ieee80211_meshpeer_ie *meshpeer;
193959aa14a9SRui Paulo 	uint16_t args[3];
194059aa14a9SRui Paulo 
194159aa14a9SRui Paulo 	/* +2+2+2+2 for action + code + capabilites + status code + AID */
1942c77735e2SRui Paulo 	meshpeer = mesh_parse_meshpeering_action(ni, wh, frm+2+2+2+2, efrm, &ie,
1943c77735e2SRui Paulo 	    IEEE80211_ACTION_MESHPEERING_CONFIRM);
194459aa14a9SRui Paulo 	if (meshpeer == NULL) {
194559aa14a9SRui Paulo 		return 0;
194659aa14a9SRui Paulo 	}
194759aa14a9SRui Paulo 
194859aa14a9SRui Paulo 	IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH, ni,
194959aa14a9SRui Paulo 	    "recv PEER CONFIRM, local id 0x%x, peer id 0x%x",
195059aa14a9SRui Paulo 	    meshpeer->peer_llinkid, meshpeer->peer_linkid);
195159aa14a9SRui Paulo 
195259aa14a9SRui Paulo 	switch (ni->ni_mlstate) {
195359aa14a9SRui Paulo 	case IEEE80211_NODE_MESH_OPENRCV:
195459aa14a9SRui Paulo 		mesh_linkchange(ni, IEEE80211_NODE_MESH_ESTABLISHED);
195559aa14a9SRui Paulo 		mesh_peer_timeout_stop(ni);
195659aa14a9SRui Paulo 		break;
195759aa14a9SRui Paulo 	case IEEE80211_NODE_MESH_OPENSNT:
195859aa14a9SRui Paulo 		mesh_linkchange(ni, IEEE80211_NODE_MESH_CONFIRMRCV);
19596eb9b443SMonthadar Al Jaberi 		mesh_peer_timeout_setup(ni);
196059aa14a9SRui Paulo 		break;
196159aa14a9SRui Paulo 	case IEEE80211_NODE_MESH_HOLDING:
196259aa14a9SRui Paulo 		args[0] = ni->ni_mlpid;
196359aa14a9SRui Paulo 		args[1] = meshpeer->peer_llinkid;
19646eb9b443SMonthadar Al Jaberi 		/* Standard not clear about what the reaason code should be */
19656eb9b443SMonthadar Al Jaberi 		args[2] = IEEE80211_REASON_PEER_LINK_CANCELED;
196659aa14a9SRui Paulo 		ieee80211_send_action(ni,
1967ebeaa1adSMonthadar Al Jaberi 		    IEEE80211_ACTION_CAT_SELF_PROT,
196859aa14a9SRui Paulo 		    IEEE80211_ACTION_MESHPEERING_CLOSE,
196959aa14a9SRui Paulo 		    args);
197059aa14a9SRui Paulo 		break;
197159aa14a9SRui Paulo 	case IEEE80211_NODE_MESH_CONFIRMRCV:
197259aa14a9SRui Paulo 		if (ni->ni_mllid != meshpeer->peer_llinkid) {
197359aa14a9SRui Paulo 			args[0] = ni->ni_mlpid;
197459aa14a9SRui Paulo 			args[1] = ni->ni_mllid;
197559aa14a9SRui Paulo 			args[2] = IEEE80211_REASON_PEER_LINK_CANCELED;
197659aa14a9SRui Paulo 			ieee80211_send_action(ni,
1977ebeaa1adSMonthadar Al Jaberi 			    IEEE80211_ACTION_CAT_SELF_PROT,
197859aa14a9SRui Paulo 			    IEEE80211_ACTION_MESHPEERING_CLOSE,
197959aa14a9SRui Paulo 			    args);
198059aa14a9SRui Paulo 			mesh_linkchange(ni, IEEE80211_NODE_MESH_HOLDING);
198159aa14a9SRui Paulo 			mesh_peer_timeout_setup(ni);
198259aa14a9SRui Paulo 		}
198359aa14a9SRui Paulo 		break;
198459aa14a9SRui Paulo 	default:
198559aa14a9SRui Paulo 		IEEE80211_DISCARD(vap,
198659aa14a9SRui Paulo 		    IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH,
198759aa14a9SRui Paulo 		    wh, NULL, "received confirm in invalid state %d",
198859aa14a9SRui Paulo 		    ni->ni_mlstate);
198959aa14a9SRui Paulo 		vap->iv_stats.is_rx_mgtdiscard++;
199059aa14a9SRui Paulo 		break;
199159aa14a9SRui Paulo 	}
199259aa14a9SRui Paulo 	return 0;
199359aa14a9SRui Paulo }
199459aa14a9SRui Paulo 
199559aa14a9SRui Paulo static int
199659aa14a9SRui Paulo mesh_recv_action_meshpeering_close(struct ieee80211_node *ni,
199759aa14a9SRui Paulo 	const struct ieee80211_frame *wh,
199859aa14a9SRui Paulo 	const uint8_t *frm, const uint8_t *efrm)
199959aa14a9SRui Paulo {
20006eb9b443SMonthadar Al Jaberi 	struct ieee80211_meshpeer_ie ie;
20016eb9b443SMonthadar Al Jaberi 	const struct ieee80211_meshpeer_ie *meshpeer;
200259aa14a9SRui Paulo 	uint16_t args[3];
200359aa14a9SRui Paulo 
20046eb9b443SMonthadar Al Jaberi 	/* +2 for action + code */
20056eb9b443SMonthadar Al Jaberi 	meshpeer = mesh_parse_meshpeering_action(ni, wh, frm+2, efrm, &ie,
20066eb9b443SMonthadar Al Jaberi 	    IEEE80211_ACTION_MESHPEERING_CLOSE);
20076eb9b443SMonthadar Al Jaberi 	if (meshpeer == NULL) {
20086eb9b443SMonthadar Al Jaberi 		return 0;
20096eb9b443SMonthadar Al Jaberi 	}
20106eb9b443SMonthadar Al Jaberi 
20116eb9b443SMonthadar Al Jaberi 	/*
20126eb9b443SMonthadar Al Jaberi 	 * XXX: check reason code, for example we could receive
20136eb9b443SMonthadar Al Jaberi 	 * IEEE80211_REASON_MESH_MAX_PEERS then we should not attempt
20146eb9b443SMonthadar Al Jaberi 	 * to peer again.
20156eb9b443SMonthadar Al Jaberi 	 */
20166eb9b443SMonthadar Al Jaberi 
201759aa14a9SRui Paulo 	IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH,
201859aa14a9SRui Paulo 	    ni, "%s", "recv PEER CLOSE");
201959aa14a9SRui Paulo 
202059aa14a9SRui Paulo 	switch (ni->ni_mlstate) {
202159aa14a9SRui Paulo 	case IEEE80211_NODE_MESH_IDLE:
202259aa14a9SRui Paulo 		/* ignore */
202359aa14a9SRui Paulo 		break;
202459aa14a9SRui Paulo 	case IEEE80211_NODE_MESH_OPENRCV:
202559aa14a9SRui Paulo 	case IEEE80211_NODE_MESH_OPENSNT:
202659aa14a9SRui Paulo 	case IEEE80211_NODE_MESH_CONFIRMRCV:
202759aa14a9SRui Paulo 	case IEEE80211_NODE_MESH_ESTABLISHED:
202859aa14a9SRui Paulo 		args[0] = ni->ni_mlpid;
202959aa14a9SRui Paulo 		args[1] = ni->ni_mllid;
203059aa14a9SRui Paulo 		args[2] = IEEE80211_REASON_MESH_CLOSE_RCVD;
203159aa14a9SRui Paulo 		ieee80211_send_action(ni,
2032ebeaa1adSMonthadar Al Jaberi 		    IEEE80211_ACTION_CAT_SELF_PROT,
203359aa14a9SRui Paulo 		    IEEE80211_ACTION_MESHPEERING_CLOSE,
203459aa14a9SRui Paulo 		    args);
203559aa14a9SRui Paulo 		mesh_linkchange(ni, IEEE80211_NODE_MESH_HOLDING);
203659aa14a9SRui Paulo 		mesh_peer_timeout_setup(ni);
203759aa14a9SRui Paulo 		break;
203859aa14a9SRui Paulo 	case IEEE80211_NODE_MESH_HOLDING:
203959aa14a9SRui Paulo 		mesh_linkchange(ni, IEEE80211_NODE_MESH_IDLE);
20406eb9b443SMonthadar Al Jaberi 		mesh_peer_timeout_stop(ni);
204159aa14a9SRui Paulo 		break;
204259aa14a9SRui Paulo 	}
204359aa14a9SRui Paulo 	return 0;
204459aa14a9SRui Paulo }
204559aa14a9SRui Paulo 
204659aa14a9SRui Paulo /*
204759aa14a9SRui Paulo  * Link Metric handling.
204859aa14a9SRui Paulo  */
204959aa14a9SRui Paulo static int
2050bdd2a076SAdrian Chadd mesh_recv_action_meshlmetric(struct ieee80211_node *ni,
205159aa14a9SRui Paulo 	const struct ieee80211_frame *wh,
205259aa14a9SRui Paulo 	const uint8_t *frm, const uint8_t *efrm)
205359aa14a9SRui Paulo {
2054bdd2a076SAdrian Chadd 	const struct ieee80211_meshlmetric_ie *ie =
2055bdd2a076SAdrian Chadd 	    (const struct ieee80211_meshlmetric_ie *)
2056bdd2a076SAdrian Chadd 	    (frm+2); /* action + code */
2057bdd2a076SAdrian Chadd 	struct ieee80211_meshlmetric_ie lm_rep;
205859aa14a9SRui Paulo 
2059bdd2a076SAdrian Chadd 	if (ie->lm_flags & IEEE80211_MESH_LMETRIC_FLAGS_REQ) {
2060bdd2a076SAdrian Chadd 		lm_rep.lm_flags = 0;
2061bdd2a076SAdrian Chadd 		lm_rep.lm_metric = mesh_airtime_calc(ni);
206259aa14a9SRui Paulo 		ieee80211_send_action(ni,
2063bdd2a076SAdrian Chadd 		    IEEE80211_ACTION_CAT_MESH,
2064bdd2a076SAdrian Chadd 		    IEEE80211_ACTION_MESH_LMETRIC,
2065bdd2a076SAdrian Chadd 		    &lm_rep);
206659aa14a9SRui Paulo 	}
2067bdd2a076SAdrian Chadd 	/* XXX: else do nothing for now */
206859aa14a9SRui Paulo 	return 0;
206959aa14a9SRui Paulo }
207059aa14a9SRui Paulo 
207159aa14a9SRui Paulo static int
207259aa14a9SRui Paulo mesh_send_action(struct ieee80211_node *ni, struct mbuf *m)
207359aa14a9SRui Paulo {
207459aa14a9SRui Paulo 	struct ieee80211_bpf_params params;
207559aa14a9SRui Paulo 
207659aa14a9SRui Paulo 	memset(&params, 0, sizeof(params));
207759aa14a9SRui Paulo 	params.ibp_pri = WME_AC_VO;
207859aa14a9SRui Paulo 	params.ibp_rate0 = ni->ni_txparms->mgmtrate;
207959aa14a9SRui Paulo 	/* XXX ucast/mcast */
208059aa14a9SRui Paulo 	params.ibp_try0 = ni->ni_txparms->maxretry;
208159aa14a9SRui Paulo 	params.ibp_power = ni->ni_txpower;
208259aa14a9SRui Paulo 	return ieee80211_mgmt_output(ni, m, IEEE80211_FC0_SUBTYPE_ACTION,
208359aa14a9SRui Paulo 	     &params);
208459aa14a9SRui Paulo }
208559aa14a9SRui Paulo 
208659aa14a9SRui Paulo #define	ADDSHORT(frm, v) do {			\
208759aa14a9SRui Paulo 	frm[0] = (v) & 0xff;			\
208859aa14a9SRui Paulo 	frm[1] = (v) >> 8;			\
208959aa14a9SRui Paulo 	frm += 2;				\
209059aa14a9SRui Paulo } while (0)
209159aa14a9SRui Paulo #define	ADDWORD(frm, v) do {			\
209259aa14a9SRui Paulo 	frm[0] = (v) & 0xff;			\
209359aa14a9SRui Paulo 	frm[1] = ((v) >> 8) & 0xff;		\
209459aa14a9SRui Paulo 	frm[2] = ((v) >> 16) & 0xff;		\
209559aa14a9SRui Paulo 	frm[3] = ((v) >> 24) & 0xff;		\
209659aa14a9SRui Paulo 	frm += 4;				\
209759aa14a9SRui Paulo } while (0)
209859aa14a9SRui Paulo 
209959aa14a9SRui Paulo static int
210059aa14a9SRui Paulo mesh_send_action_meshpeering_open(struct ieee80211_node *ni,
210159aa14a9SRui Paulo 	int category, int action, void *args0)
210259aa14a9SRui Paulo {
210359aa14a9SRui Paulo 	struct ieee80211vap *vap = ni->ni_vap;
210459aa14a9SRui Paulo 	struct ieee80211com *ic = ni->ni_ic;
210559aa14a9SRui Paulo 	uint16_t *args = args0;
210659aa14a9SRui Paulo 	const struct ieee80211_rateset *rs;
210759aa14a9SRui Paulo 	struct mbuf *m;
210859aa14a9SRui Paulo 	uint8_t *frm;
210959aa14a9SRui Paulo 
211059aa14a9SRui Paulo 	IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH, ni,
211159aa14a9SRui Paulo 	    "send PEER OPEN action: localid 0x%x", args[0]);
211259aa14a9SRui Paulo 
211359aa14a9SRui Paulo 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
211459aa14a9SRui Paulo 	    "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", __func__, __LINE__,
211559aa14a9SRui Paulo 	    ni, ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)+1);
211659aa14a9SRui Paulo 	ieee80211_ref_node(ni);
211759aa14a9SRui Paulo 
211859aa14a9SRui Paulo 	m = ieee80211_getmgtframe(&frm,
211959aa14a9SRui Paulo 	    ic->ic_headroom + sizeof(struct ieee80211_frame),
212059aa14a9SRui Paulo 	    sizeof(uint16_t)	/* action+category */
212159aa14a9SRui Paulo 	    + sizeof(uint16_t)	/* capabilites */
212259aa14a9SRui Paulo 	    + 2 + IEEE80211_RATE_SIZE
212359aa14a9SRui Paulo 	    + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
212459aa14a9SRui Paulo 	    + 2 + IEEE80211_MESHID_LEN
212559aa14a9SRui Paulo 	    + sizeof(struct ieee80211_meshconf_ie)
212659aa14a9SRui Paulo 	    + sizeof(struct ieee80211_meshpeer_ie)
212759aa14a9SRui Paulo 	);
212859aa14a9SRui Paulo 	if (m != NULL) {
212959aa14a9SRui Paulo 		/*
213059aa14a9SRui Paulo 		 * mesh peer open action frame format:
213159aa14a9SRui Paulo 		 *   [1] category
213259aa14a9SRui Paulo 		 *   [1] action
213359aa14a9SRui Paulo 		 *   [2] capabilities
213459aa14a9SRui Paulo 		 *   [tlv] rates
213559aa14a9SRui Paulo 		 *   [tlv] xrates
213659aa14a9SRui Paulo 		 *   [tlv] mesh id
213759aa14a9SRui Paulo 		 *   [tlv] mesh conf
213859aa14a9SRui Paulo 		 *   [tlv] mesh peer link mgmt
213959aa14a9SRui Paulo 		 */
214059aa14a9SRui Paulo 		*frm++ = category;
214159aa14a9SRui Paulo 		*frm++ = action;
214259aa14a9SRui Paulo 		ADDSHORT(frm, ieee80211_getcapinfo(vap, ni->ni_chan));
214359aa14a9SRui Paulo 		rs = ieee80211_get_suprates(ic, ic->ic_curchan);
214459aa14a9SRui Paulo 		frm = ieee80211_add_rates(frm, rs);
214559aa14a9SRui Paulo 		frm = ieee80211_add_xrates(frm, rs);
214659aa14a9SRui Paulo 		frm = ieee80211_add_meshid(frm, vap);
214759aa14a9SRui Paulo 		frm = ieee80211_add_meshconf(frm, vap);
2148ebeaa1adSMonthadar Al Jaberi 		frm = ieee80211_add_meshpeer(frm, IEEE80211_ACTION_MESHPEERING_OPEN,
214959aa14a9SRui Paulo 		    args[0], 0, 0);
215059aa14a9SRui Paulo 		m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
215159aa14a9SRui Paulo 		return mesh_send_action(ni, m);
215259aa14a9SRui Paulo 	} else {
215359aa14a9SRui Paulo 		vap->iv_stats.is_tx_nobuf++;
215459aa14a9SRui Paulo 		ieee80211_free_node(ni);
215559aa14a9SRui Paulo 		return ENOMEM;
215659aa14a9SRui Paulo 	}
215759aa14a9SRui Paulo }
215859aa14a9SRui Paulo 
215959aa14a9SRui Paulo static int
216059aa14a9SRui Paulo mesh_send_action_meshpeering_confirm(struct ieee80211_node *ni,
216159aa14a9SRui Paulo 	int category, int action, void *args0)
216259aa14a9SRui Paulo {
216359aa14a9SRui Paulo 	struct ieee80211vap *vap = ni->ni_vap;
216459aa14a9SRui Paulo 	struct ieee80211com *ic = ni->ni_ic;
216559aa14a9SRui Paulo 	uint16_t *args = args0;
216659aa14a9SRui Paulo 	const struct ieee80211_rateset *rs;
216759aa14a9SRui Paulo 	struct mbuf *m;
216859aa14a9SRui Paulo 	uint8_t *frm;
216959aa14a9SRui Paulo 
217059aa14a9SRui Paulo 	IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH, ni,
217159aa14a9SRui Paulo 	    "send PEER CONFIRM action: localid 0x%x, peerid 0x%x",
217259aa14a9SRui Paulo 	    args[0], args[1]);
217359aa14a9SRui Paulo 
217459aa14a9SRui Paulo 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
217559aa14a9SRui Paulo 	    "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", __func__, __LINE__,
217659aa14a9SRui Paulo 	    ni, ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)+1);
217759aa14a9SRui Paulo 	ieee80211_ref_node(ni);
217859aa14a9SRui Paulo 
217959aa14a9SRui Paulo 	m = ieee80211_getmgtframe(&frm,
218059aa14a9SRui Paulo 	    ic->ic_headroom + sizeof(struct ieee80211_frame),
218159aa14a9SRui Paulo 	    sizeof(uint16_t)	/* action+category */
218259aa14a9SRui Paulo 	    + sizeof(uint16_t)	/* capabilites */
218359aa14a9SRui Paulo 	    + sizeof(uint16_t)	/* status code */
218459aa14a9SRui Paulo 	    + sizeof(uint16_t)	/* AID */
218559aa14a9SRui Paulo 	    + 2 + IEEE80211_RATE_SIZE
218659aa14a9SRui Paulo 	    + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
218759aa14a9SRui Paulo 	    + 2 + IEEE80211_MESHID_LEN
218859aa14a9SRui Paulo 	    + sizeof(struct ieee80211_meshconf_ie)
218959aa14a9SRui Paulo 	    + sizeof(struct ieee80211_meshpeer_ie)
219059aa14a9SRui Paulo 	);
219159aa14a9SRui Paulo 	if (m != NULL) {
219259aa14a9SRui Paulo 		/*
219359aa14a9SRui Paulo 		 * mesh peer confirm action frame format:
219459aa14a9SRui Paulo 		 *   [1] category
219559aa14a9SRui Paulo 		 *   [1] action
219659aa14a9SRui Paulo 		 *   [2] capabilities
219759aa14a9SRui Paulo 		 *   [2] status code
219859aa14a9SRui Paulo 		 *   [2] association id (peer ID)
219959aa14a9SRui Paulo 		 *   [tlv] rates
220059aa14a9SRui Paulo 		 *   [tlv] xrates
220159aa14a9SRui Paulo 		 *   [tlv] mesh id
220259aa14a9SRui Paulo 		 *   [tlv] mesh conf
220359aa14a9SRui Paulo 		 *   [tlv] mesh peer link mgmt
220459aa14a9SRui Paulo 		 */
220559aa14a9SRui Paulo 		*frm++ = category;
220659aa14a9SRui Paulo 		*frm++ = action;
220759aa14a9SRui Paulo 		ADDSHORT(frm, ieee80211_getcapinfo(vap, ni->ni_chan));
220859aa14a9SRui Paulo 		ADDSHORT(frm, 0);		/* status code */
220959aa14a9SRui Paulo 		ADDSHORT(frm, args[1]);		/* AID */
221059aa14a9SRui Paulo 		rs = ieee80211_get_suprates(ic, ic->ic_curchan);
221159aa14a9SRui Paulo 		frm = ieee80211_add_rates(frm, rs);
221259aa14a9SRui Paulo 		frm = ieee80211_add_xrates(frm, rs);
221359aa14a9SRui Paulo 		frm = ieee80211_add_meshid(frm, vap);
221459aa14a9SRui Paulo 		frm = ieee80211_add_meshconf(frm, vap);
221559aa14a9SRui Paulo 		frm = ieee80211_add_meshpeer(frm,
2216ebeaa1adSMonthadar Al Jaberi 		    IEEE80211_ACTION_MESHPEERING_CONFIRM,
221759aa14a9SRui Paulo 		    args[0], args[1], 0);
221859aa14a9SRui Paulo 		m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
221959aa14a9SRui Paulo 		return mesh_send_action(ni, m);
222059aa14a9SRui Paulo 	} else {
222159aa14a9SRui Paulo 		vap->iv_stats.is_tx_nobuf++;
222259aa14a9SRui Paulo 		ieee80211_free_node(ni);
222359aa14a9SRui Paulo 		return ENOMEM;
222459aa14a9SRui Paulo 	}
222559aa14a9SRui Paulo }
222659aa14a9SRui Paulo 
222759aa14a9SRui Paulo static int
222859aa14a9SRui Paulo mesh_send_action_meshpeering_close(struct ieee80211_node *ni,
222959aa14a9SRui Paulo 	int category, int action, void *args0)
223059aa14a9SRui Paulo {
223159aa14a9SRui Paulo 	struct ieee80211vap *vap = ni->ni_vap;
223259aa14a9SRui Paulo 	struct ieee80211com *ic = ni->ni_ic;
223359aa14a9SRui Paulo 	uint16_t *args = args0;
223459aa14a9SRui Paulo 	struct mbuf *m;
223559aa14a9SRui Paulo 	uint8_t *frm;
223659aa14a9SRui Paulo 
223759aa14a9SRui Paulo 	IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH, ni,
223859aa14a9SRui Paulo 	    "send PEER CLOSE action: localid 0x%x, peerid 0x%x reason %d",
223959aa14a9SRui Paulo 	    args[0], args[1], args[2]);
224059aa14a9SRui Paulo 
224159aa14a9SRui Paulo 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
224259aa14a9SRui Paulo 	    "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", __func__, __LINE__,
224359aa14a9SRui Paulo 	    ni, ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)+1);
224459aa14a9SRui Paulo 	ieee80211_ref_node(ni);
224559aa14a9SRui Paulo 
224659aa14a9SRui Paulo 	m = ieee80211_getmgtframe(&frm,
224759aa14a9SRui Paulo 	    ic->ic_headroom + sizeof(struct ieee80211_frame),
224859aa14a9SRui Paulo 	    sizeof(uint16_t)	/* action+category */
224959aa14a9SRui Paulo 	    + sizeof(uint16_t)	/* reason code */
225059aa14a9SRui Paulo 	    + 2 + IEEE80211_MESHID_LEN
225159aa14a9SRui Paulo 	    + sizeof(struct ieee80211_meshpeer_ie)
225259aa14a9SRui Paulo 	);
225359aa14a9SRui Paulo 	if (m != NULL) {
225459aa14a9SRui Paulo 		/*
225559aa14a9SRui Paulo 		 * mesh peer close action frame format:
225659aa14a9SRui Paulo 		 *   [1] category
225759aa14a9SRui Paulo 		 *   [1] action
225859aa14a9SRui Paulo 		 *   [tlv] mesh id
225959aa14a9SRui Paulo 		 *   [tlv] mesh peer link mgmt
226059aa14a9SRui Paulo 		 */
226159aa14a9SRui Paulo 		*frm++ = category;
226259aa14a9SRui Paulo 		*frm++ = action;
226359aa14a9SRui Paulo 		frm = ieee80211_add_meshid(frm, vap);
226459aa14a9SRui Paulo 		frm = ieee80211_add_meshpeer(frm,
2265ebeaa1adSMonthadar Al Jaberi 		    IEEE80211_ACTION_MESHPEERING_CLOSE,
226659aa14a9SRui Paulo 		    args[0], args[1], args[2]);
226759aa14a9SRui Paulo 		m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
226859aa14a9SRui Paulo 		return mesh_send_action(ni, m);
226959aa14a9SRui Paulo 	} else {
227059aa14a9SRui Paulo 		vap->iv_stats.is_tx_nobuf++;
227159aa14a9SRui Paulo 		ieee80211_free_node(ni);
227259aa14a9SRui Paulo 		return ENOMEM;
227359aa14a9SRui Paulo 	}
227459aa14a9SRui Paulo }
227559aa14a9SRui Paulo 
227659aa14a9SRui Paulo static int
2277bdd2a076SAdrian Chadd mesh_send_action_meshlmetric(struct ieee80211_node *ni,
227859aa14a9SRui Paulo 	int category, int action, void *arg0)
227959aa14a9SRui Paulo {
228059aa14a9SRui Paulo 	struct ieee80211vap *vap = ni->ni_vap;
228159aa14a9SRui Paulo 	struct ieee80211com *ic = ni->ni_ic;
2282bdd2a076SAdrian Chadd 	struct ieee80211_meshlmetric_ie *ie = arg0;
228359aa14a9SRui Paulo 	struct mbuf *m;
228459aa14a9SRui Paulo 	uint8_t *frm;
228559aa14a9SRui Paulo 
2286bdd2a076SAdrian Chadd 	if (ie->lm_flags & IEEE80211_MESH_LMETRIC_FLAGS_REQ) {
2287bdd2a076SAdrian Chadd 		IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH,
2288bdd2a076SAdrian Chadd 		    ni, "%s", "send LINK METRIC REQUEST action");
228959aa14a9SRui Paulo 	} else {
2290bdd2a076SAdrian Chadd 		IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH,
2291bdd2a076SAdrian Chadd 		    ni, "send LINK METRIC REPLY action: metric 0x%x",
2292bdd2a076SAdrian Chadd 		    ie->lm_metric);
229359aa14a9SRui Paulo 	}
229459aa14a9SRui Paulo 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
229559aa14a9SRui Paulo 	    "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", __func__, __LINE__,
229659aa14a9SRui Paulo 	    ni, ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)+1);
229759aa14a9SRui Paulo 	ieee80211_ref_node(ni);
229859aa14a9SRui Paulo 
229959aa14a9SRui Paulo 	m = ieee80211_getmgtframe(&frm,
230059aa14a9SRui Paulo 	    ic->ic_headroom + sizeof(struct ieee80211_frame),
2301bdd2a076SAdrian Chadd 	    sizeof(uint16_t) +	/* action+category */
2302bdd2a076SAdrian Chadd 	    sizeof(struct ieee80211_meshlmetric_ie)
230359aa14a9SRui Paulo 	);
230459aa14a9SRui Paulo 	if (m != NULL) {
230559aa14a9SRui Paulo 		/*
2306bdd2a076SAdrian Chadd 		 * mesh link metric
230759aa14a9SRui Paulo 		 *   [1] category
230859aa14a9SRui Paulo 		 *   [1] action
230959aa14a9SRui Paulo 		 *   [tlv] mesh link metric
231059aa14a9SRui Paulo 		 */
231159aa14a9SRui Paulo 		*frm++ = category;
231259aa14a9SRui Paulo 		*frm++ = action;
2313bdd2a076SAdrian Chadd 		frm = ieee80211_add_meshlmetric(frm,
2314bdd2a076SAdrian Chadd 		    ie->lm_flags, ie->lm_metric);
231559aa14a9SRui Paulo 		m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
231659aa14a9SRui Paulo 		return mesh_send_action(ni, m);
231759aa14a9SRui Paulo 	} else {
231859aa14a9SRui Paulo 		vap->iv_stats.is_tx_nobuf++;
231959aa14a9SRui Paulo 		ieee80211_free_node(ni);
232059aa14a9SRui Paulo 		return ENOMEM;
232159aa14a9SRui Paulo 	}
232259aa14a9SRui Paulo }
232359aa14a9SRui Paulo 
232459aa14a9SRui Paulo static void
232559aa14a9SRui Paulo mesh_peer_timeout_setup(struct ieee80211_node *ni)
232659aa14a9SRui Paulo {
232759aa14a9SRui Paulo 	switch (ni->ni_mlstate) {
232859aa14a9SRui Paulo 	case IEEE80211_NODE_MESH_HOLDING:
232959aa14a9SRui Paulo 		ni->ni_mltval = ieee80211_mesh_holdingtimeout;
233059aa14a9SRui Paulo 		break;
233159aa14a9SRui Paulo 	case IEEE80211_NODE_MESH_CONFIRMRCV:
233259aa14a9SRui Paulo 		ni->ni_mltval = ieee80211_mesh_confirmtimeout;
233359aa14a9SRui Paulo 		break;
233459aa14a9SRui Paulo 	case IEEE80211_NODE_MESH_IDLE:
233559aa14a9SRui Paulo 		ni->ni_mltval = 0;
233659aa14a9SRui Paulo 		break;
233759aa14a9SRui Paulo 	default:
233859aa14a9SRui Paulo 		ni->ni_mltval = ieee80211_mesh_retrytimeout;
233959aa14a9SRui Paulo 		break;
234059aa14a9SRui Paulo 	}
234159aa14a9SRui Paulo 	if (ni->ni_mltval)
234259aa14a9SRui Paulo 		callout_reset(&ni->ni_mltimer, ni->ni_mltval,
234359aa14a9SRui Paulo 		    mesh_peer_timeout_cb, ni);
234459aa14a9SRui Paulo }
234559aa14a9SRui Paulo 
234659aa14a9SRui Paulo /*
234759aa14a9SRui Paulo  * Same as above but backoffs timer statisically 50%.
234859aa14a9SRui Paulo  */
234959aa14a9SRui Paulo static void
235059aa14a9SRui Paulo mesh_peer_timeout_backoff(struct ieee80211_node *ni)
235159aa14a9SRui Paulo {
235259aa14a9SRui Paulo 	uint32_t r;
235359aa14a9SRui Paulo 
235459aa14a9SRui Paulo 	r = arc4random();
235559aa14a9SRui Paulo 	ni->ni_mltval += r % ni->ni_mltval;
235659aa14a9SRui Paulo 	callout_reset(&ni->ni_mltimer, ni->ni_mltval, mesh_peer_timeout_cb,
235759aa14a9SRui Paulo 	    ni);
235859aa14a9SRui Paulo }
235959aa14a9SRui Paulo 
236059aa14a9SRui Paulo static __inline void
236159aa14a9SRui Paulo mesh_peer_timeout_stop(struct ieee80211_node *ni)
236259aa14a9SRui Paulo {
2363c104cff2SRui Paulo 	callout_drain(&ni->ni_mltimer);
236459aa14a9SRui Paulo }
236559aa14a9SRui Paulo 
236659aa14a9SRui Paulo /*
236759aa14a9SRui Paulo  * Mesh Peer Link Management FSM timeout handling.
236859aa14a9SRui Paulo  */
236959aa14a9SRui Paulo static void
237059aa14a9SRui Paulo mesh_peer_timeout_cb(void *arg)
237159aa14a9SRui Paulo {
237259aa14a9SRui Paulo 	struct ieee80211_node *ni = (struct ieee80211_node *)arg;
237359aa14a9SRui Paulo 	uint16_t args[3];
237459aa14a9SRui Paulo 
237559aa14a9SRui Paulo 	IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_MESH,
237659aa14a9SRui Paulo 	    ni, "mesh link timeout, state %d, retry counter %d",
237759aa14a9SRui Paulo 	    ni->ni_mlstate, ni->ni_mlrcnt);
237859aa14a9SRui Paulo 
237959aa14a9SRui Paulo 	switch (ni->ni_mlstate) {
238059aa14a9SRui Paulo 	case IEEE80211_NODE_MESH_IDLE:
238159aa14a9SRui Paulo 	case IEEE80211_NODE_MESH_ESTABLISHED:
238259aa14a9SRui Paulo 		break;
238359aa14a9SRui Paulo 	case IEEE80211_NODE_MESH_OPENSNT:
238459aa14a9SRui Paulo 	case IEEE80211_NODE_MESH_OPENRCV:
238559aa14a9SRui Paulo 		if (ni->ni_mlrcnt == ieee80211_mesh_maxretries) {
238659aa14a9SRui Paulo 			args[0] = ni->ni_mlpid;
238759aa14a9SRui Paulo 			args[2] = IEEE80211_REASON_MESH_MAX_RETRIES;
238859aa14a9SRui Paulo 			ieee80211_send_action(ni,
2389ebeaa1adSMonthadar Al Jaberi 			    IEEE80211_ACTION_CAT_SELF_PROT,
239059aa14a9SRui Paulo 			    IEEE80211_ACTION_MESHPEERING_CLOSE, args);
239159aa14a9SRui Paulo 			ni->ni_mlrcnt = 0;
239259aa14a9SRui Paulo 			mesh_linkchange(ni, IEEE80211_NODE_MESH_HOLDING);
239359aa14a9SRui Paulo 			mesh_peer_timeout_setup(ni);
239459aa14a9SRui Paulo 		} else {
239559aa14a9SRui Paulo 			args[0] = ni->ni_mlpid;
239659aa14a9SRui Paulo 			ieee80211_send_action(ni,
2397ebeaa1adSMonthadar Al Jaberi 			    IEEE80211_ACTION_CAT_SELF_PROT,
239859aa14a9SRui Paulo 			    IEEE80211_ACTION_MESHPEERING_OPEN, args);
239959aa14a9SRui Paulo 			ni->ni_mlrcnt++;
240059aa14a9SRui Paulo 			mesh_peer_timeout_backoff(ni);
240159aa14a9SRui Paulo 		}
240259aa14a9SRui Paulo 		break;
240359aa14a9SRui Paulo 	case IEEE80211_NODE_MESH_CONFIRMRCV:
240459aa14a9SRui Paulo 		args[0] = ni->ni_mlpid;
240559aa14a9SRui Paulo 		args[2] = IEEE80211_REASON_MESH_CONFIRM_TIMEOUT;
240659aa14a9SRui Paulo 		ieee80211_send_action(ni,
2407ebeaa1adSMonthadar Al Jaberi 		    IEEE80211_ACTION_CAT_SELF_PROT,
240859aa14a9SRui Paulo 		    IEEE80211_ACTION_MESHPEERING_CLOSE, args);
240959aa14a9SRui Paulo 		mesh_linkchange(ni, IEEE80211_NODE_MESH_HOLDING);
241059aa14a9SRui Paulo 		mesh_peer_timeout_setup(ni);
241159aa14a9SRui Paulo 		break;
241259aa14a9SRui Paulo 	case IEEE80211_NODE_MESH_HOLDING:
241359aa14a9SRui Paulo 		mesh_linkchange(ni, IEEE80211_NODE_MESH_IDLE);
241459aa14a9SRui Paulo 		break;
241559aa14a9SRui Paulo 	}
241659aa14a9SRui Paulo }
241759aa14a9SRui Paulo 
2418aec0289dSRui Paulo static int
241959aa14a9SRui Paulo mesh_verify_meshid(struct ieee80211vap *vap, const uint8_t *ie)
242059aa14a9SRui Paulo {
242159aa14a9SRui Paulo 	struct ieee80211_mesh_state *ms = vap->iv_mesh;
242259aa14a9SRui Paulo 
242359aa14a9SRui Paulo 	if (ie == NULL || ie[1] != ms->ms_idlen)
242459aa14a9SRui Paulo 		return 1;
242559aa14a9SRui Paulo 	return memcmp(ms->ms_id, ie + 2, ms->ms_idlen);
242659aa14a9SRui Paulo }
242759aa14a9SRui Paulo 
242859aa14a9SRui Paulo /*
242959aa14a9SRui Paulo  * Check if we are using the same algorithms for this mesh.
243059aa14a9SRui Paulo  */
243159aa14a9SRui Paulo static int
243259aa14a9SRui Paulo mesh_verify_meshconf(struct ieee80211vap *vap, const uint8_t *ie)
243359aa14a9SRui Paulo {
243459aa14a9SRui Paulo 	const struct ieee80211_meshconf_ie *meshconf =
243559aa14a9SRui Paulo 	    (const struct ieee80211_meshconf_ie *) ie;
243659aa14a9SRui Paulo 	const struct ieee80211_mesh_state *ms = vap->iv_mesh;
243759aa14a9SRui Paulo 
243859aa14a9SRui Paulo 	if (meshconf == NULL)
243959aa14a9SRui Paulo 		return 1;
24406b8c1829SRui Paulo 	if (meshconf->conf_pselid != ms->ms_ppath->mpp_ie) {
244159aa14a9SRui Paulo 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_MESH,
24426b8c1829SRui Paulo 		    "unknown path selection algorithm: 0x%x\n",
24436b8c1829SRui Paulo 		    meshconf->conf_pselid);
244459aa14a9SRui Paulo 		return 1;
244559aa14a9SRui Paulo 	}
24466b8c1829SRui Paulo 	if (meshconf->conf_pmetid != ms->ms_pmetric->mpm_ie) {
244759aa14a9SRui Paulo 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_MESH,
24486b8c1829SRui Paulo 		    "unknown path metric algorithm: 0x%x\n",
24496b8c1829SRui Paulo 		    meshconf->conf_pmetid);
245059aa14a9SRui Paulo 		return 1;
245159aa14a9SRui Paulo 	}
24526b8c1829SRui Paulo 	if (meshconf->conf_ccid != 0) {
245359aa14a9SRui Paulo 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_MESH,
24546b8c1829SRui Paulo 		    "unknown congestion control algorithm: 0x%x\n",
24556b8c1829SRui Paulo 		    meshconf->conf_ccid);
245659aa14a9SRui Paulo 		return 1;
245759aa14a9SRui Paulo 	}
24586b8c1829SRui Paulo 	if (meshconf->conf_syncid != IEEE80211_MESHCONF_SYNC_NEIGHOFF) {
245959aa14a9SRui Paulo 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_MESH,
24606b8c1829SRui Paulo 		    "unknown sync algorithm: 0x%x\n",
24616b8c1829SRui Paulo 		    meshconf->conf_syncid);
246259aa14a9SRui Paulo 		return 1;
246359aa14a9SRui Paulo 	}
24646b8c1829SRui Paulo 	if (meshconf->conf_authid != 0) {
246559aa14a9SRui Paulo 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_MESH,
24666b8c1829SRui Paulo 		    "unknown auth auth algorithm: 0x%x\n",
24676b8c1829SRui Paulo 		    meshconf->conf_pselid);
246859aa14a9SRui Paulo 		return 1;
246959aa14a9SRui Paulo 	}
247059aa14a9SRui Paulo 	/* Not accepting peers */
247181b95c2cSAdrian Chadd 	if (!(meshconf->conf_cap & IEEE80211_MESHCONF_CAP_AP)) {
247259aa14a9SRui Paulo 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_MESH,
247359aa14a9SRui Paulo 		    "not accepting peers: 0x%x\n", meshconf->conf_cap);
247459aa14a9SRui Paulo 		return 1;
247559aa14a9SRui Paulo 	}
247659aa14a9SRui Paulo 	return 0;
247759aa14a9SRui Paulo }
247859aa14a9SRui Paulo 
247959aa14a9SRui Paulo static int
2480c77735e2SRui Paulo mesh_verify_meshpeer(struct ieee80211vap *vap, uint8_t subtype,
2481c77735e2SRui Paulo     const uint8_t *ie)
248259aa14a9SRui Paulo {
248359aa14a9SRui Paulo 	const struct ieee80211_meshpeer_ie *meshpeer =
248459aa14a9SRui Paulo 	    (const struct ieee80211_meshpeer_ie *) ie;
248559aa14a9SRui Paulo 
2486c2042c35SMonthadar Al Jaberi 	if (meshpeer == NULL ||
2487c2042c35SMonthadar Al Jaberi 	    meshpeer->peer_len < IEEE80211_MPM_BASE_SZ ||
2488c2042c35SMonthadar Al Jaberi 	    meshpeer->peer_len > IEEE80211_MPM_MAX_SZ)
248959aa14a9SRui Paulo 		return 1;
2490c2042c35SMonthadar Al Jaberi 	if (meshpeer->peer_proto != IEEE80211_MPPID_MPM) {
2491c2042c35SMonthadar Al Jaberi 		IEEE80211_DPRINTF(vap,
2492c2042c35SMonthadar Al Jaberi 		    IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH,
2493c2042c35SMonthadar Al Jaberi 		    "Only MPM protocol is supported (proto: 0x%02X)",
2494c2042c35SMonthadar Al Jaberi 		    meshpeer->peer_proto);
2495c2042c35SMonthadar Al Jaberi 		return 1;
2496c2042c35SMonthadar Al Jaberi 	}
2497c77735e2SRui Paulo 	switch (subtype) {
2498ebeaa1adSMonthadar Al Jaberi 	case IEEE80211_ACTION_MESHPEERING_OPEN:
2499c2042c35SMonthadar Al Jaberi 		if (meshpeer->peer_len != IEEE80211_MPM_BASE_SZ)
250059aa14a9SRui Paulo 			return 1;
250159aa14a9SRui Paulo 		break;
2502ebeaa1adSMonthadar Al Jaberi 	case IEEE80211_ACTION_MESHPEERING_CONFIRM:
2503c2042c35SMonthadar Al Jaberi 		if (meshpeer->peer_len != IEEE80211_MPM_BASE_SZ + 2)
250459aa14a9SRui Paulo 			return 1;
250559aa14a9SRui Paulo 		break;
2506ebeaa1adSMonthadar Al Jaberi 	case IEEE80211_ACTION_MESHPEERING_CLOSE:
2507c2042c35SMonthadar Al Jaberi 		if (meshpeer->peer_len < IEEE80211_MPM_BASE_SZ + 2)
250859aa14a9SRui Paulo 			return 1;
2509c2042c35SMonthadar Al Jaberi 		if (meshpeer->peer_len == (IEEE80211_MPM_BASE_SZ + 2) &&
2510c2042c35SMonthadar Al Jaberi 		    meshpeer->peer_linkid != 0)
251159aa14a9SRui Paulo 			return 1;
251259aa14a9SRui Paulo 		if (meshpeer->peer_rcode == 0)
251359aa14a9SRui Paulo 			return 1;
251459aa14a9SRui Paulo 		break;
251559aa14a9SRui Paulo 	}
251659aa14a9SRui Paulo 	return 0;
251759aa14a9SRui Paulo }
251859aa14a9SRui Paulo 
251959aa14a9SRui Paulo /*
252059aa14a9SRui Paulo  * Add a Mesh ID IE to a frame.
252159aa14a9SRui Paulo  */
252259aa14a9SRui Paulo uint8_t *
252359aa14a9SRui Paulo ieee80211_add_meshid(uint8_t *frm, struct ieee80211vap *vap)
252459aa14a9SRui Paulo {
252559aa14a9SRui Paulo 	struct ieee80211_mesh_state *ms = vap->iv_mesh;
252659aa14a9SRui Paulo 
252759aa14a9SRui Paulo 	KASSERT(vap->iv_opmode == IEEE80211_M_MBSS, ("not a mbss vap"));
252859aa14a9SRui Paulo 
252959aa14a9SRui Paulo 	*frm++ = IEEE80211_ELEMID_MESHID;
253059aa14a9SRui Paulo 	*frm++ = ms->ms_idlen;
253159aa14a9SRui Paulo 	memcpy(frm, ms->ms_id, ms->ms_idlen);
253259aa14a9SRui Paulo 	return frm + ms->ms_idlen;
253359aa14a9SRui Paulo }
253459aa14a9SRui Paulo 
253559aa14a9SRui Paulo /*
253659aa14a9SRui Paulo  * Add a Mesh Configuration IE to a frame.
253759aa14a9SRui Paulo  * For now just use HWMP routing, Airtime link metric, Null Congestion
253859aa14a9SRui Paulo  * Signaling, Null Sync Protocol and Null Authentication.
253959aa14a9SRui Paulo  */
254059aa14a9SRui Paulo uint8_t *
254159aa14a9SRui Paulo ieee80211_add_meshconf(uint8_t *frm, struct ieee80211vap *vap)
254259aa14a9SRui Paulo {
254359aa14a9SRui Paulo 	const struct ieee80211_mesh_state *ms = vap->iv_mesh;
2544651e41a4SRui Paulo 	uint16_t caps;
254559aa14a9SRui Paulo 
254659aa14a9SRui Paulo 	KASSERT(vap->iv_opmode == IEEE80211_M_MBSS, ("not a MBSS vap"));
254759aa14a9SRui Paulo 
254859aa14a9SRui Paulo 	*frm++ = IEEE80211_ELEMID_MESHCONF;
254981b95c2cSAdrian Chadd 	*frm++ = IEEE80211_MESH_CONF_SZ;
25506b8c1829SRui Paulo 	*frm++ = ms->ms_ppath->mpp_ie;		/* path selection */
25516b8c1829SRui Paulo 	*frm++ = ms->ms_pmetric->mpm_ie;	/* link metric */
25526b8c1829SRui Paulo 	*frm++ = IEEE80211_MESHCONF_CC_DISABLED;
25536b8c1829SRui Paulo 	*frm++ = IEEE80211_MESHCONF_SYNC_NEIGHOFF;
25546b8c1829SRui Paulo 	*frm++ = IEEE80211_MESHCONF_AUTH_DISABLED;
255559aa14a9SRui Paulo 	/* NB: set the number of neighbors before the rest */
25566eb9b443SMonthadar Al Jaberi 	*frm = (ms->ms_neighbors > IEEE80211_MESH_MAX_NEIGHBORS ?
25576eb9b443SMonthadar Al Jaberi 	    IEEE80211_MESH_MAX_NEIGHBORS : ms->ms_neighbors) << 1;
255859aa14a9SRui Paulo 	if (ms->ms_flags & IEEE80211_MESHFLAGS_PORTAL)
255959aa14a9SRui Paulo 		*frm |= IEEE80211_MESHCONF_FORM_MP;
256059aa14a9SRui Paulo 	frm += 1;
2561651e41a4SRui Paulo 	caps = 0;
256259aa14a9SRui Paulo 	if (ms->ms_flags & IEEE80211_MESHFLAGS_AP)
2563651e41a4SRui Paulo 		caps |= IEEE80211_MESHCONF_CAP_AP;
256459aa14a9SRui Paulo 	if (ms->ms_flags & IEEE80211_MESHFLAGS_FWD)
2565651e41a4SRui Paulo 		caps |= IEEE80211_MESHCONF_CAP_FWRD;
256681b95c2cSAdrian Chadd 	*frm++ = caps;
256759aa14a9SRui Paulo 	return frm;
256859aa14a9SRui Paulo }
256959aa14a9SRui Paulo 
257059aa14a9SRui Paulo /*
257159aa14a9SRui Paulo  * Add a Mesh Peer Management IE to a frame.
257259aa14a9SRui Paulo  */
257359aa14a9SRui Paulo uint8_t *
257459aa14a9SRui Paulo ieee80211_add_meshpeer(uint8_t *frm, uint8_t subtype, uint16_t localid,
257559aa14a9SRui Paulo     uint16_t peerid, uint16_t reason)
257659aa14a9SRui Paulo {
2577c77735e2SRui Paulo 
257859aa14a9SRui Paulo 	KASSERT(localid != 0, ("localid == 0"));
257959aa14a9SRui Paulo 
258059aa14a9SRui Paulo 	*frm++ = IEEE80211_ELEMID_MESHPEER;
258159aa14a9SRui Paulo 	switch (subtype) {
2582ebeaa1adSMonthadar Al Jaberi 	case IEEE80211_ACTION_MESHPEERING_OPEN:
2583c2042c35SMonthadar Al Jaberi 		*frm++ = IEEE80211_MPM_BASE_SZ;		/* length */
2584c2042c35SMonthadar Al Jaberi 		ADDSHORT(frm, IEEE80211_MPPID_MPM);	/* proto */
258559aa14a9SRui Paulo 		ADDSHORT(frm, localid);			/* local ID */
258659aa14a9SRui Paulo 		break;
2587ebeaa1adSMonthadar Al Jaberi 	case IEEE80211_ACTION_MESHPEERING_CONFIRM:
258859aa14a9SRui Paulo 		KASSERT(peerid != 0, ("sending peer confirm without peer id"));
2589c2042c35SMonthadar Al Jaberi 		*frm++ = IEEE80211_MPM_BASE_SZ + 2;	/* length */
2590c2042c35SMonthadar Al Jaberi 		ADDSHORT(frm, IEEE80211_MPPID_MPM);	/* proto */
259159aa14a9SRui Paulo 		ADDSHORT(frm, localid);			/* local ID */
259259aa14a9SRui Paulo 		ADDSHORT(frm, peerid);			/* peer ID */
259359aa14a9SRui Paulo 		break;
2594ebeaa1adSMonthadar Al Jaberi 	case IEEE80211_ACTION_MESHPEERING_CLOSE:
259559aa14a9SRui Paulo 		if (peerid)
2596c2042c35SMonthadar Al Jaberi 			*frm++ = IEEE80211_MPM_MAX_SZ;	/* length */
259759aa14a9SRui Paulo 		else
2598c2042c35SMonthadar Al Jaberi 			*frm++ = IEEE80211_MPM_BASE_SZ + 2; /* length */
2599c2042c35SMonthadar Al Jaberi 		ADDSHORT(frm, IEEE80211_MPPID_MPM);	/* proto */
260059aa14a9SRui Paulo 		ADDSHORT(frm, localid);	/* local ID */
260159aa14a9SRui Paulo 		if (peerid)
260259aa14a9SRui Paulo 			ADDSHORT(frm, peerid);	/* peer ID */
260359aa14a9SRui Paulo 		ADDSHORT(frm, reason);
260459aa14a9SRui Paulo 		break;
260559aa14a9SRui Paulo 	}
260659aa14a9SRui Paulo 	return frm;
260759aa14a9SRui Paulo }
260859aa14a9SRui Paulo 
260959aa14a9SRui Paulo /*
261059aa14a9SRui Paulo  * Compute an Airtime Link Metric for the link with this node.
261159aa14a9SRui Paulo  *
261259aa14a9SRui Paulo  * Based on Draft 3.0 spec (11B.10, p.149).
261359aa14a9SRui Paulo  */
261459aa14a9SRui Paulo /*
261559aa14a9SRui Paulo  * Max 802.11s overhead.
261659aa14a9SRui Paulo  */
261759aa14a9SRui Paulo #define IEEE80211_MESH_MAXOVERHEAD \
261859aa14a9SRui Paulo 	(sizeof(struct ieee80211_qosframe_addr4) \
261959aa14a9SRui Paulo 	 + sizeof(struct ieee80211_meshcntl_ae11) \
262059aa14a9SRui Paulo 	+ sizeof(struct llc) \
262159aa14a9SRui Paulo 	+ IEEE80211_ADDR_LEN \
262259aa14a9SRui Paulo 	+ IEEE80211_WEP_IVLEN \
262359aa14a9SRui Paulo 	+ IEEE80211_WEP_KIDLEN \
262459aa14a9SRui Paulo 	+ IEEE80211_WEP_CRCLEN \
262559aa14a9SRui Paulo 	+ IEEE80211_WEP_MICLEN \
262659aa14a9SRui Paulo 	+ IEEE80211_CRC_LEN)
262759aa14a9SRui Paulo uint32_t
262859aa14a9SRui Paulo mesh_airtime_calc(struct ieee80211_node *ni)
262959aa14a9SRui Paulo {
263059aa14a9SRui Paulo #define M_BITS 8
263159aa14a9SRui Paulo #define S_FACTOR (2 * M_BITS)
263259aa14a9SRui Paulo 	struct ieee80211com *ic = ni->ni_ic;
263359aa14a9SRui Paulo 	struct ifnet *ifp = ni->ni_vap->iv_ifp;
263459aa14a9SRui Paulo 	const static int nbits = 8192 << M_BITS;
263559aa14a9SRui Paulo 	uint32_t overhead, rate, errrate;
263659aa14a9SRui Paulo 	uint64_t res;
263759aa14a9SRui Paulo 
263859aa14a9SRui Paulo 	/* Time to transmit a frame */
263959aa14a9SRui Paulo 	rate = ni->ni_txrate;
264059aa14a9SRui Paulo 	overhead = ieee80211_compute_duration(ic->ic_rt,
264159aa14a9SRui Paulo 	    ifp->if_mtu + IEEE80211_MESH_MAXOVERHEAD, rate, 0) << M_BITS;
264259aa14a9SRui Paulo 	/* Error rate in percentage */
264359aa14a9SRui Paulo 	/* XXX assuming small failures are ok */
264459aa14a9SRui Paulo 	errrate = (((ifp->if_oerrors +
264559aa14a9SRui Paulo 	    ifp->if_ierrors) / 100) << M_BITS) / 100;
264659aa14a9SRui Paulo 	res = (overhead + (nbits / rate)) *
264759aa14a9SRui Paulo 	    ((1 << S_FACTOR) / ((1 << M_BITS) - errrate));
264859aa14a9SRui Paulo 
264959aa14a9SRui Paulo 	return (uint32_t)(res >> S_FACTOR);
265059aa14a9SRui Paulo #undef M_BITS
265159aa14a9SRui Paulo #undef S_FACTOR
265259aa14a9SRui Paulo }
265359aa14a9SRui Paulo 
265459aa14a9SRui Paulo /*
265559aa14a9SRui Paulo  * Add a Mesh Link Metric report IE to a frame.
265659aa14a9SRui Paulo  */
265759aa14a9SRui Paulo uint8_t *
2658bdd2a076SAdrian Chadd ieee80211_add_meshlmetric(uint8_t *frm, uint8_t flags, uint32_t metric)
265959aa14a9SRui Paulo {
266059aa14a9SRui Paulo 	*frm++ = IEEE80211_ELEMID_MESHLINK;
2661bdd2a076SAdrian Chadd 	*frm++ = 5;
2662bdd2a076SAdrian Chadd 	*frm++ = flags;
266359aa14a9SRui Paulo 	ADDWORD(frm, metric);
266459aa14a9SRui Paulo 	return frm;
266559aa14a9SRui Paulo }
266659aa14a9SRui Paulo #undef ADDSHORT
266759aa14a9SRui Paulo #undef ADDWORD
266859aa14a9SRui Paulo 
266959aa14a9SRui Paulo /*
267059aa14a9SRui Paulo  * Initialize any mesh-specific node state.
267159aa14a9SRui Paulo  */
267259aa14a9SRui Paulo void
267359aa14a9SRui Paulo ieee80211_mesh_node_init(struct ieee80211vap *vap, struct ieee80211_node *ni)
267459aa14a9SRui Paulo {
267559aa14a9SRui Paulo 	ni->ni_flags |= IEEE80211_NODE_QOS;
267659aa14a9SRui Paulo 	callout_init(&ni->ni_mltimer, CALLOUT_MPSAFE);
267759aa14a9SRui Paulo }
267859aa14a9SRui Paulo 
267959aa14a9SRui Paulo /*
268059aa14a9SRui Paulo  * Cleanup any mesh-specific node state.
268159aa14a9SRui Paulo  */
268259aa14a9SRui Paulo void
268359aa14a9SRui Paulo ieee80211_mesh_node_cleanup(struct ieee80211_node *ni)
268459aa14a9SRui Paulo {
268559aa14a9SRui Paulo 	struct ieee80211vap *vap = ni->ni_vap;
268659aa14a9SRui Paulo 	struct ieee80211_mesh_state *ms = vap->iv_mesh;
268759aa14a9SRui Paulo 
268859aa14a9SRui Paulo 	callout_drain(&ni->ni_mltimer);
268959aa14a9SRui Paulo 	/* NB: short-circuit callbacks after mesh_vdetach */
269059aa14a9SRui Paulo 	if (vap->iv_mesh != NULL)
269159aa14a9SRui Paulo 		ms->ms_ppath->mpp_peerdown(ni);
269259aa14a9SRui Paulo }
269359aa14a9SRui Paulo 
269459aa14a9SRui Paulo void
269559aa14a9SRui Paulo ieee80211_parse_meshid(struct ieee80211_node *ni, const uint8_t *ie)
269659aa14a9SRui Paulo {
269759aa14a9SRui Paulo 	ni->ni_meshidlen = ie[1];
269859aa14a9SRui Paulo 	memcpy(ni->ni_meshid, ie + 2, ie[1]);
269959aa14a9SRui Paulo }
270059aa14a9SRui Paulo 
270159aa14a9SRui Paulo /*
270259aa14a9SRui Paulo  * Setup mesh-specific node state on neighbor discovery.
270359aa14a9SRui Paulo  */
270459aa14a9SRui Paulo void
270559aa14a9SRui Paulo ieee80211_mesh_init_neighbor(struct ieee80211_node *ni,
270659aa14a9SRui Paulo 	const struct ieee80211_frame *wh,
270759aa14a9SRui Paulo 	const struct ieee80211_scanparams *sp)
270859aa14a9SRui Paulo {
270959aa14a9SRui Paulo 	ieee80211_parse_meshid(ni, sp->meshid);
271059aa14a9SRui Paulo }
271159aa14a9SRui Paulo 
2712d093681cSRui Paulo void
2713d093681cSRui Paulo ieee80211_mesh_update_beacon(struct ieee80211vap *vap,
2714d093681cSRui Paulo 	struct ieee80211_beacon_offsets *bo)
2715d093681cSRui Paulo {
2716d093681cSRui Paulo 	KASSERT(vap->iv_opmode == IEEE80211_M_MBSS, ("not a MBSS vap"));
2717d093681cSRui Paulo 
2718d093681cSRui Paulo 	if (isset(bo->bo_flags, IEEE80211_BEACON_MESHCONF)) {
2719d093681cSRui Paulo 		(void)ieee80211_add_meshconf(bo->bo_meshconf, vap);
2720d093681cSRui Paulo 		clrbit(bo->bo_flags, IEEE80211_BEACON_MESHCONF);
2721d093681cSRui Paulo 	}
2722d093681cSRui Paulo }
2723d093681cSRui Paulo 
272459aa14a9SRui Paulo static int
272559aa14a9SRui Paulo mesh_ioctl_get80211(struct ieee80211vap *vap, struct ieee80211req *ireq)
272659aa14a9SRui Paulo {
272759aa14a9SRui Paulo 	struct ieee80211_mesh_state *ms = vap->iv_mesh;
272859aa14a9SRui Paulo 	uint8_t tmpmeshid[IEEE80211_NWID_LEN];
272959aa14a9SRui Paulo 	struct ieee80211_mesh_route *rt;
273059aa14a9SRui Paulo 	struct ieee80211req_mesh_route *imr;
273159aa14a9SRui Paulo 	size_t len, off;
273259aa14a9SRui Paulo 	uint8_t *p;
273359aa14a9SRui Paulo 	int error;
273459aa14a9SRui Paulo 
273559aa14a9SRui Paulo 	if (vap->iv_opmode != IEEE80211_M_MBSS)
273659aa14a9SRui Paulo 		return ENOSYS;
273759aa14a9SRui Paulo 
273859aa14a9SRui Paulo 	error = 0;
273959aa14a9SRui Paulo 	switch (ireq->i_type) {
274059aa14a9SRui Paulo 	case IEEE80211_IOC_MESH_ID:
274159aa14a9SRui Paulo 		ireq->i_len = ms->ms_idlen;
274259aa14a9SRui Paulo 		memcpy(tmpmeshid, ms->ms_id, ireq->i_len);
274359aa14a9SRui Paulo 		error = copyout(tmpmeshid, ireq->i_data, ireq->i_len);
274459aa14a9SRui Paulo 		break;
274559aa14a9SRui Paulo 	case IEEE80211_IOC_MESH_AP:
274659aa14a9SRui Paulo 		ireq->i_val = (ms->ms_flags & IEEE80211_MESHFLAGS_AP) != 0;
274759aa14a9SRui Paulo 		break;
274859aa14a9SRui Paulo 	case IEEE80211_IOC_MESH_FWRD:
274959aa14a9SRui Paulo 		ireq->i_val = (ms->ms_flags & IEEE80211_MESHFLAGS_FWD) != 0;
275059aa14a9SRui Paulo 		break;
275159aa14a9SRui Paulo 	case IEEE80211_IOC_MESH_TTL:
275259aa14a9SRui Paulo 		ireq->i_val = ms->ms_ttl;
275359aa14a9SRui Paulo 		break;
275459aa14a9SRui Paulo 	case IEEE80211_IOC_MESH_RTCMD:
275559aa14a9SRui Paulo 		switch (ireq->i_val) {
275659aa14a9SRui Paulo 		case IEEE80211_MESH_RTCMD_LIST:
275759aa14a9SRui Paulo 			len = 0;
275859aa14a9SRui Paulo 			MESH_RT_LOCK(ms);
275959aa14a9SRui Paulo 			TAILQ_FOREACH(rt, &ms->ms_routes, rt_next) {
276059aa14a9SRui Paulo 				len += sizeof(*imr);
276159aa14a9SRui Paulo 			}
276259aa14a9SRui Paulo 			MESH_RT_UNLOCK(ms);
276359aa14a9SRui Paulo 			if (len > ireq->i_len || ireq->i_len < sizeof(*imr)) {
276459aa14a9SRui Paulo 				ireq->i_len = len;
276559aa14a9SRui Paulo 				return ENOMEM;
276659aa14a9SRui Paulo 			}
276759aa14a9SRui Paulo 			ireq->i_len = len;
2768c104cff2SRui Paulo 			/* XXX M_WAIT? */
276959aa14a9SRui Paulo 			p = malloc(len, M_TEMP, M_NOWAIT | M_ZERO);
277059aa14a9SRui Paulo 			if (p == NULL)
277159aa14a9SRui Paulo 				return ENOMEM;
277259aa14a9SRui Paulo 			off = 0;
277359aa14a9SRui Paulo 			MESH_RT_LOCK(ms);
277459aa14a9SRui Paulo 			TAILQ_FOREACH(rt, &ms->ms_routes, rt_next) {
277559aa14a9SRui Paulo 				if (off >= len)
277659aa14a9SRui Paulo 					break;
277759aa14a9SRui Paulo 				imr = (struct ieee80211req_mesh_route *)
277859aa14a9SRui Paulo 				    (p + off);
277959aa14a9SRui Paulo 				IEEE80211_ADDR_COPY(imr->imr_dest,
278059aa14a9SRui Paulo 				    rt->rt_dest);
278159aa14a9SRui Paulo 				IEEE80211_ADDR_COPY(imr->imr_nexthop,
278259aa14a9SRui Paulo 				    rt->rt_nexthop);
278359aa14a9SRui Paulo 				imr->imr_metric = rt->rt_metric;
278459aa14a9SRui Paulo 				imr->imr_nhops = rt->rt_nhops;
2785*b5df85a6SMonthadar Al Jaberi 				imr->imr_lifetime =
2786*b5df85a6SMonthadar Al Jaberi 				    ieee80211_mesh_rt_update(rt, 0);
2787c104cff2SRui Paulo 				imr->imr_lastmseq = rt->rt_lastmseq;
2788*b5df85a6SMonthadar Al Jaberi 				imr->imr_flags = rt->rt_flags; /* last */
278959aa14a9SRui Paulo 				off += sizeof(*imr);
279059aa14a9SRui Paulo 			}
279159aa14a9SRui Paulo 			MESH_RT_UNLOCK(ms);
279259aa14a9SRui Paulo 			error = copyout(p, (uint8_t *)ireq->i_data,
279359aa14a9SRui Paulo 			    ireq->i_len);
279459aa14a9SRui Paulo 			free(p, M_TEMP);
279559aa14a9SRui Paulo 			break;
279659aa14a9SRui Paulo 		case IEEE80211_MESH_RTCMD_FLUSH:
279759aa14a9SRui Paulo 		case IEEE80211_MESH_RTCMD_ADD:
279859aa14a9SRui Paulo 		case IEEE80211_MESH_RTCMD_DELETE:
279959aa14a9SRui Paulo 			return EINVAL;
280059aa14a9SRui Paulo 		default:
280159aa14a9SRui Paulo 			return ENOSYS;
280259aa14a9SRui Paulo 		}
280359aa14a9SRui Paulo 		break;
280459aa14a9SRui Paulo 	case IEEE80211_IOC_MESH_PR_METRIC:
280559aa14a9SRui Paulo 		len = strlen(ms->ms_pmetric->mpm_descr);
280659aa14a9SRui Paulo 		if (ireq->i_len < len)
280759aa14a9SRui Paulo 			return EINVAL;
280859aa14a9SRui Paulo 		ireq->i_len = len;
280959aa14a9SRui Paulo 		error = copyout(ms->ms_pmetric->mpm_descr,
281059aa14a9SRui Paulo 		    (uint8_t *)ireq->i_data, len);
281159aa14a9SRui Paulo 		break;
281259aa14a9SRui Paulo 	case IEEE80211_IOC_MESH_PR_PATH:
281359aa14a9SRui Paulo 		len = strlen(ms->ms_ppath->mpp_descr);
281459aa14a9SRui Paulo 		if (ireq->i_len < len)
281559aa14a9SRui Paulo 			return EINVAL;
281659aa14a9SRui Paulo 		ireq->i_len = len;
281759aa14a9SRui Paulo 		error = copyout(ms->ms_ppath->mpp_descr,
281859aa14a9SRui Paulo 		    (uint8_t *)ireq->i_data, len);
281959aa14a9SRui Paulo 		break;
282059aa14a9SRui Paulo 	default:
282159aa14a9SRui Paulo 		return ENOSYS;
282259aa14a9SRui Paulo 	}
282359aa14a9SRui Paulo 
282459aa14a9SRui Paulo 	return error;
282559aa14a9SRui Paulo }
282659aa14a9SRui Paulo IEEE80211_IOCTL_GET(mesh, mesh_ioctl_get80211);
282759aa14a9SRui Paulo 
282859aa14a9SRui Paulo static int
282959aa14a9SRui Paulo mesh_ioctl_set80211(struct ieee80211vap *vap, struct ieee80211req *ireq)
283059aa14a9SRui Paulo {
283159aa14a9SRui Paulo 	struct ieee80211_mesh_state *ms = vap->iv_mesh;
283259aa14a9SRui Paulo 	uint8_t tmpmeshid[IEEE80211_NWID_LEN];
283359aa14a9SRui Paulo 	uint8_t tmpaddr[IEEE80211_ADDR_LEN];
283459aa14a9SRui Paulo 	char tmpproto[IEEE80211_MESH_PROTO_DSZ];
283559aa14a9SRui Paulo 	int error;
283659aa14a9SRui Paulo 
283759aa14a9SRui Paulo 	if (vap->iv_opmode != IEEE80211_M_MBSS)
283859aa14a9SRui Paulo 		return ENOSYS;
283959aa14a9SRui Paulo 
284059aa14a9SRui Paulo 	error = 0;
284159aa14a9SRui Paulo 	switch (ireq->i_type) {
284259aa14a9SRui Paulo 	case IEEE80211_IOC_MESH_ID:
284359aa14a9SRui Paulo 		if (ireq->i_val != 0 || ireq->i_len > IEEE80211_MESHID_LEN)
284459aa14a9SRui Paulo 			return EINVAL;
284559aa14a9SRui Paulo 		error = copyin(ireq->i_data, tmpmeshid, ireq->i_len);
2846c104cff2SRui Paulo 		if (error != 0)
284759aa14a9SRui Paulo 			break;
284859aa14a9SRui Paulo 		memset(ms->ms_id, 0, IEEE80211_NWID_LEN);
284959aa14a9SRui Paulo 		ms->ms_idlen = ireq->i_len;
285059aa14a9SRui Paulo 		memcpy(ms->ms_id, tmpmeshid, ireq->i_len);
2851c104cff2SRui Paulo 		error = ENETRESET;
285259aa14a9SRui Paulo 		break;
285359aa14a9SRui Paulo 	case IEEE80211_IOC_MESH_AP:
285459aa14a9SRui Paulo 		if (ireq->i_val)
285559aa14a9SRui Paulo 			ms->ms_flags |= IEEE80211_MESHFLAGS_AP;
285659aa14a9SRui Paulo 		else
285759aa14a9SRui Paulo 			ms->ms_flags &= ~IEEE80211_MESHFLAGS_AP;
2858c104cff2SRui Paulo 		error = ENETRESET;
285959aa14a9SRui Paulo 		break;
286059aa14a9SRui Paulo 	case IEEE80211_IOC_MESH_FWRD:
286159aa14a9SRui Paulo 		if (ireq->i_val)
286259aa14a9SRui Paulo 			ms->ms_flags |= IEEE80211_MESHFLAGS_FWD;
286359aa14a9SRui Paulo 		else
286459aa14a9SRui Paulo 			ms->ms_flags &= ~IEEE80211_MESHFLAGS_FWD;
286559aa14a9SRui Paulo 		break;
286659aa14a9SRui Paulo 	case IEEE80211_IOC_MESH_TTL:
286759aa14a9SRui Paulo 		ms->ms_ttl = (uint8_t) ireq->i_val;
286859aa14a9SRui Paulo 		break;
286959aa14a9SRui Paulo 	case IEEE80211_IOC_MESH_RTCMD:
287059aa14a9SRui Paulo 		switch (ireq->i_val) {
287159aa14a9SRui Paulo 		case IEEE80211_MESH_RTCMD_LIST:
287259aa14a9SRui Paulo 			return EINVAL;
287359aa14a9SRui Paulo 		case IEEE80211_MESH_RTCMD_FLUSH:
287459aa14a9SRui Paulo 			ieee80211_mesh_rt_flush(vap);
287559aa14a9SRui Paulo 			break;
287659aa14a9SRui Paulo 		case IEEE80211_MESH_RTCMD_ADD:
287759aa14a9SRui Paulo 			if (IEEE80211_ADDR_EQ(vap->iv_myaddr, ireq->i_data) ||
287859aa14a9SRui Paulo 			    IEEE80211_ADDR_EQ(broadcastaddr, ireq->i_data))
287959aa14a9SRui Paulo 				return EINVAL;
288059aa14a9SRui Paulo 			error = copyin(ireq->i_data, &tmpaddr,
288159aa14a9SRui Paulo 			    IEEE80211_ADDR_LEN);
2882c104cff2SRui Paulo 			if (error == 0)
288359aa14a9SRui Paulo 				ieee80211_mesh_discover(vap, tmpaddr, NULL);
288459aa14a9SRui Paulo 			break;
288559aa14a9SRui Paulo 		case IEEE80211_MESH_RTCMD_DELETE:
288659aa14a9SRui Paulo 			ieee80211_mesh_rt_del(vap, ireq->i_data);
288759aa14a9SRui Paulo 			break;
288859aa14a9SRui Paulo 		default:
288959aa14a9SRui Paulo 			return ENOSYS;
289059aa14a9SRui Paulo 		}
289159aa14a9SRui Paulo 		break;
289259aa14a9SRui Paulo 	case IEEE80211_IOC_MESH_PR_METRIC:
289359aa14a9SRui Paulo 		error = copyin(ireq->i_data, tmpproto, sizeof(tmpproto));
2894c104cff2SRui Paulo 		if (error == 0) {
2895c104cff2SRui Paulo 			error = mesh_select_proto_metric(vap, tmpproto);
2896c104cff2SRui Paulo 			if (error == 0)
2897c104cff2SRui Paulo 				error = ENETRESET;
2898c104cff2SRui Paulo 		}
289959aa14a9SRui Paulo 		break;
290059aa14a9SRui Paulo 	case IEEE80211_IOC_MESH_PR_PATH:
290159aa14a9SRui Paulo 		error = copyin(ireq->i_data, tmpproto, sizeof(tmpproto));
2902c104cff2SRui Paulo 		if (error == 0) {
2903c104cff2SRui Paulo 			error = mesh_select_proto_path(vap, tmpproto);
2904c104cff2SRui Paulo 			if (error == 0)
2905c104cff2SRui Paulo 				error = ENETRESET;
2906c104cff2SRui Paulo 		}
290759aa14a9SRui Paulo 		break;
290859aa14a9SRui Paulo 	default:
290959aa14a9SRui Paulo 		return ENOSYS;
291059aa14a9SRui Paulo 	}
291159aa14a9SRui Paulo 	return error;
291259aa14a9SRui Paulo }
291359aa14a9SRui Paulo IEEE80211_IOCTL_SET(mesh, mesh_ioctl_set80211);
2914