xref: /freebsd/sys/net80211/ieee80211_mesh.c (revision 737a965ec9a74abbed3e30e53a69bbaace4da245)
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 
141b5df85a6SMonthadar Al Jaberi #define	RT_ENTRY_LOCK(rt)	mtx_lock(&(rt)->rt_lock)
142b5df85a6SMonthadar Al Jaberi #define	RT_ENTRY_LOCK_ASSERT(rt) mtx_assert(&(rt)->rt_lock, MA_OWNED)
143b5df85a6SMonthadar Al Jaberi #define	RT_ENTRY_UNLOCK(rt)	mtx_unlock(&(rt)->rt_lock)
144b5df85a6SMonthadar 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 
153b5df85a6SMonthadar Al Jaberi /* The longer one of the lifetime should be stored as new lifetime */
154b5df85a6SMonthadar Al Jaberi #define MESH_ROUTE_LIFETIME_MAX(a, b)	(a > b ? a : b)
155b5df85a6SMonthadar 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]);
193b5df85a6SMonthadar Al Jaberi 		mtx_init(&rt->rt_lock, "MBSS_RT", "802.11s route entry", MTX_DEF);
194b5df85a6SMonthadar 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 /*
232b5df85a6SMonthadar Al Jaberi  * Update the route lifetime and returns the updated lifetime.
233b5df85a6SMonthadar Al Jaberi  * If new_lifetime is zero and route is timedout it will be invalidated.
234b5df85a6SMonthadar Al Jaberi  * new_lifetime is in msec
235b5df85a6SMonthadar Al Jaberi  */
236b5df85a6SMonthadar Al Jaberi int
237b5df85a6SMonthadar Al Jaberi ieee80211_mesh_rt_update(struct ieee80211_mesh_route *rt, int new_lifetime)
238b5df85a6SMonthadar Al Jaberi {
239b5df85a6SMonthadar Al Jaberi 	int timesince, now;
240b5df85a6SMonthadar Al Jaberi 	uint32_t lifetime = 0;
241b5df85a6SMonthadar Al Jaberi 
242b5df85a6SMonthadar Al Jaberi 	now = ticks;
243b5df85a6SMonthadar Al Jaberi 	RT_ENTRY_LOCK(rt);
2444f3a27aeSMonthadar Al Jaberi 
2454f3a27aeSMonthadar Al Jaberi 	/* dont clobber a proxy entry gated by us */
2464f3a27aeSMonthadar Al Jaberi 	if (rt->rt_flags & IEEE80211_MESHRT_FLAGS_PROXY && rt->rt_nhops == 0) {
2474f3a27aeSMonthadar Al Jaberi 		RT_ENTRY_UNLOCK(rt);
2484f3a27aeSMonthadar Al Jaberi 		return rt->rt_lifetime;
2494f3a27aeSMonthadar Al Jaberi 	}
2504f3a27aeSMonthadar Al Jaberi 
251b5df85a6SMonthadar Al Jaberi 	timesince = ticks_to_msecs(now - rt->rt_updtime);
252b5df85a6SMonthadar Al Jaberi 	rt->rt_updtime = now;
253b5df85a6SMonthadar Al Jaberi 	if (timesince >= rt->rt_lifetime) {
254b5df85a6SMonthadar Al Jaberi 		if (new_lifetime != 0) {
255b5df85a6SMonthadar Al Jaberi 			rt->rt_lifetime = new_lifetime;
256b5df85a6SMonthadar Al Jaberi 		}
257b5df85a6SMonthadar Al Jaberi 		else {
258b5df85a6SMonthadar Al Jaberi 			rt->rt_flags &= ~IEEE80211_MESHRT_FLAGS_VALID;
259b5df85a6SMonthadar Al Jaberi 			rt->rt_lifetime = 0;
260b5df85a6SMonthadar Al Jaberi 		}
261b5df85a6SMonthadar Al Jaberi 	} else {
262b5df85a6SMonthadar Al Jaberi 		/* update what is left of lifetime */
263b5df85a6SMonthadar Al Jaberi 		rt->rt_lifetime = rt->rt_lifetime - timesince;
264b5df85a6SMonthadar Al Jaberi 		rt->rt_lifetime  = MESH_ROUTE_LIFETIME_MAX(
265b5df85a6SMonthadar Al Jaberi 			new_lifetime, rt->rt_lifetime);
266b5df85a6SMonthadar Al Jaberi 	}
267b5df85a6SMonthadar Al Jaberi 	lifetime = rt->rt_lifetime;
268b5df85a6SMonthadar Al Jaberi 	RT_ENTRY_UNLOCK(rt);
269b5df85a6SMonthadar Al Jaberi 
270b5df85a6SMonthadar Al Jaberi 	return lifetime;
271b5df85a6SMonthadar Al Jaberi }
272b5df85a6SMonthadar Al Jaberi 
273b5df85a6SMonthadar Al Jaberi /*
274c104cff2SRui Paulo  * Add a proxy route (as needed) for the specified destination.
275c104cff2SRui Paulo  */
276c104cff2SRui Paulo void
277c104cff2SRui Paulo ieee80211_mesh_proxy_check(struct ieee80211vap *vap,
278c104cff2SRui Paulo     const uint8_t dest[IEEE80211_ADDR_LEN])
279c104cff2SRui Paulo {
280c104cff2SRui Paulo 	struct ieee80211_mesh_state *ms = vap->iv_mesh;
281c104cff2SRui Paulo 	struct ieee80211_mesh_route *rt;
282c104cff2SRui Paulo 
283c104cff2SRui Paulo 	MESH_RT_LOCK(ms);
284c104cff2SRui Paulo 	rt = mesh_rt_find_locked(ms, dest);
285c104cff2SRui Paulo 	if (rt == NULL) {
286c104cff2SRui Paulo 		rt = mesh_rt_add_locked(ms, dest);
287c104cff2SRui Paulo 		if (rt == NULL) {
288c104cff2SRui Paulo 			IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH, dest,
289c104cff2SRui Paulo 			    "%s", "unable to add proxy entry");
290c104cff2SRui Paulo 			vap->iv_stats.is_mesh_rtaddfailed++;
291c104cff2SRui Paulo 		} else {
2923ca80f0dSRui Paulo 			IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH, dest,
2933ca80f0dSRui Paulo 			    "%s", "add proxy entry");
2943c314f6dSMonthadar Al Jaberi 			IEEE80211_ADDR_COPY(rt->rt_mesh_gate, vap->iv_myaddr);
295c104cff2SRui Paulo 			IEEE80211_ADDR_COPY(rt->rt_nexthop, vap->iv_myaddr);
296c104cff2SRui Paulo 			rt->rt_flags |= IEEE80211_MESHRT_FLAGS_VALID
297c104cff2SRui Paulo 				     |  IEEE80211_MESHRT_FLAGS_PROXY;
298c104cff2SRui Paulo 		}
299c104cff2SRui Paulo 	} else if ((rt->rt_flags & IEEE80211_MESHRT_FLAGS_VALID) == 0) {
3003c314f6dSMonthadar Al Jaberi 		KASSERT(rt->rt_flags & IEEE80211_MESHRT_FLAGS_PROXY,
3013c314f6dSMonthadar Al Jaberi 		    ("no proxy flag for poxy entry"));
302c104cff2SRui Paulo 		struct ieee80211com *ic = vap->iv_ic;
303c104cff2SRui Paulo 		/*
304c104cff2SRui Paulo 		 * Fix existing entry created by received frames from
305c104cff2SRui Paulo 		 * stations that have some memory of dest.  We also
306c104cff2SRui Paulo 		 * flush any frames held on the staging queue; delivering
307c104cff2SRui Paulo 		 * them is too much trouble right now.
308c104cff2SRui Paulo 		 */
309c104cff2SRui Paulo 		IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH, dest,
310c104cff2SRui Paulo 		    "%s", "fix proxy entry");
311c104cff2SRui Paulo 		IEEE80211_ADDR_COPY(rt->rt_nexthop, vap->iv_myaddr);
312c104cff2SRui Paulo 		rt->rt_flags |= IEEE80211_MESHRT_FLAGS_VALID
313c104cff2SRui Paulo 			     |  IEEE80211_MESHRT_FLAGS_PROXY;
314c104cff2SRui Paulo 		/* XXX belongs in hwmp */
315c104cff2SRui Paulo 		ieee80211_ageq_drain_node(&ic->ic_stageq,
316c104cff2SRui Paulo 		   (void *)(uintptr_t) ieee80211_mac_hash(ic, dest));
317c104cff2SRui Paulo 		/* XXX stat? */
318c104cff2SRui Paulo 	}
319c104cff2SRui Paulo 	MESH_RT_UNLOCK(ms);
320c104cff2SRui Paulo }
321c104cff2SRui Paulo 
322c104cff2SRui Paulo static __inline void
323c104cff2SRui Paulo mesh_rt_del(struct ieee80211_mesh_state *ms, struct ieee80211_mesh_route *rt)
324c104cff2SRui Paulo {
325c104cff2SRui Paulo 	TAILQ_REMOVE(&ms->ms_routes, rt, rt_next);
326b5df85a6SMonthadar Al Jaberi 	/*
327b5df85a6SMonthadar Al Jaberi 	 * Grab the lock before destroying it, to be sure no one else
328b5df85a6SMonthadar Al Jaberi 	 * is holding the route.
329b5df85a6SMonthadar Al Jaberi 	 */
330b5df85a6SMonthadar Al Jaberi 	RT_ENTRY_LOCK(rt);
331b5df85a6SMonthadar Al Jaberi 	mtx_destroy(&rt->rt_lock);
332c104cff2SRui Paulo 	free(rt, M_80211_MESH_RT);
333c104cff2SRui Paulo }
334c104cff2SRui Paulo 
33559aa14a9SRui Paulo void
33659aa14a9SRui Paulo ieee80211_mesh_rt_del(struct ieee80211vap *vap,
33759aa14a9SRui Paulo     const uint8_t dest[IEEE80211_ADDR_LEN])
33859aa14a9SRui Paulo {
33959aa14a9SRui Paulo 	struct ieee80211_mesh_state *ms = vap->iv_mesh;
34059aa14a9SRui Paulo 	struct ieee80211_mesh_route *rt, *next;
34159aa14a9SRui Paulo 
34259aa14a9SRui Paulo 	MESH_RT_LOCK(ms);
34359aa14a9SRui Paulo 	TAILQ_FOREACH_SAFE(rt, &ms->ms_routes, rt_next, next) {
34459aa14a9SRui Paulo 		if (IEEE80211_ADDR_EQ(rt->rt_dest, dest)) {
345c104cff2SRui Paulo 			mesh_rt_del(ms, rt);
34659aa14a9SRui Paulo 			MESH_RT_UNLOCK(ms);
34759aa14a9SRui Paulo 			return;
34859aa14a9SRui Paulo 		}
34959aa14a9SRui Paulo 	}
35059aa14a9SRui Paulo 	MESH_RT_UNLOCK(ms);
35159aa14a9SRui Paulo }
35259aa14a9SRui Paulo 
35359aa14a9SRui Paulo void
35459aa14a9SRui Paulo ieee80211_mesh_rt_flush(struct ieee80211vap *vap)
35559aa14a9SRui Paulo {
35659aa14a9SRui Paulo 	struct ieee80211_mesh_state *ms = vap->iv_mesh;
35759aa14a9SRui Paulo 	struct ieee80211_mesh_route *rt, *next;
35859aa14a9SRui Paulo 
35959aa14a9SRui Paulo 	if (ms == NULL)
36059aa14a9SRui Paulo 		return;
36159aa14a9SRui Paulo 	MESH_RT_LOCK(ms);
362c104cff2SRui Paulo 	TAILQ_FOREACH_SAFE(rt, &ms->ms_routes, rt_next, next)
363c104cff2SRui Paulo 		mesh_rt_del(ms, rt);
364c104cff2SRui Paulo 	MESH_RT_UNLOCK(ms);
365c104cff2SRui Paulo }
366c104cff2SRui Paulo 
3673ca80f0dSRui Paulo void
3683ca80f0dSRui Paulo ieee80211_mesh_rt_flush_peer(struct ieee80211vap *vap,
3693ca80f0dSRui Paulo     const uint8_t peer[IEEE80211_ADDR_LEN])
3703ca80f0dSRui Paulo {
3713ca80f0dSRui Paulo 	struct ieee80211_mesh_state *ms = vap->iv_mesh;
3723ca80f0dSRui Paulo 	struct ieee80211_mesh_route *rt, *next;
3733ca80f0dSRui Paulo 
3743ca80f0dSRui Paulo 	MESH_RT_LOCK(ms);
3753ca80f0dSRui Paulo 	TAILQ_FOREACH_SAFE(rt, &ms->ms_routes, rt_next, next) {
3763ca80f0dSRui Paulo 		if (IEEE80211_ADDR_EQ(rt->rt_nexthop, peer))
3773ca80f0dSRui Paulo 			mesh_rt_del(ms, rt);
3783ca80f0dSRui Paulo 	}
3793ca80f0dSRui Paulo 	MESH_RT_UNLOCK(ms);
3803ca80f0dSRui Paulo }
3813ca80f0dSRui Paulo 
382c104cff2SRui Paulo /*
383c104cff2SRui Paulo  * Flush expired routing entries, i.e. those in invalid state for
384c104cff2SRui Paulo  * some time.
385c104cff2SRui Paulo  */
386c104cff2SRui Paulo static void
387c104cff2SRui Paulo mesh_rt_flush_invalid(struct ieee80211vap *vap)
388c104cff2SRui Paulo {
389c104cff2SRui Paulo 	struct ieee80211_mesh_state *ms = vap->iv_mesh;
390c104cff2SRui Paulo 	struct ieee80211_mesh_route *rt, *next;
391c104cff2SRui Paulo 
392c104cff2SRui Paulo 	if (ms == NULL)
393c104cff2SRui Paulo 		return;
394c104cff2SRui Paulo 	MESH_RT_LOCK(ms);
39559aa14a9SRui Paulo 	TAILQ_FOREACH_SAFE(rt, &ms->ms_routes, rt_next, next) {
396b5df85a6SMonthadar Al Jaberi 		ieee80211_mesh_rt_update(rt, 0);
397b5df85a6SMonthadar Al Jaberi 		/*
398b5df85a6SMonthadar Al Jaberi 		 * NB: we check for lifetime == 0 so that we give a chance
399b5df85a6SMonthadar Al Jaberi 		 * for route discovery to complete.
400b5df85a6SMonthadar Al Jaberi 		 */
401c104cff2SRui Paulo 		if ((rt->rt_flags & IEEE80211_MESHRT_FLAGS_VALID) == 0 &&
402b5df85a6SMonthadar Al Jaberi 		    rt->rt_lifetime == 0)
403c104cff2SRui Paulo 			mesh_rt_del(ms, rt);
40459aa14a9SRui Paulo 	}
40559aa14a9SRui Paulo 	MESH_RT_UNLOCK(ms);
40659aa14a9SRui Paulo }
40759aa14a9SRui Paulo 
40859aa14a9SRui Paulo #define	N(a)	(sizeof(a) / sizeof(a[0]))
40959aa14a9SRui Paulo int
41059aa14a9SRui Paulo ieee80211_mesh_register_proto_path(const struct ieee80211_mesh_proto_path *mpp)
41159aa14a9SRui Paulo {
41259aa14a9SRui Paulo 	int i, firstempty = -1;
41359aa14a9SRui Paulo 
41459aa14a9SRui Paulo 	for (i = 0; i < N(mesh_proto_paths); i++) {
4156b8c1829SRui Paulo 		if (strncmp(mpp->mpp_descr, mesh_proto_paths[i].mpp_descr,
4166b8c1829SRui Paulo 		    IEEE80211_MESH_PROTO_DSZ) == 0)
41759aa14a9SRui Paulo 			return EEXIST;
4186b8c1829SRui Paulo 		if (!mesh_proto_paths[i].mpp_active && firstempty == -1)
41959aa14a9SRui Paulo 			firstempty = i;
42059aa14a9SRui Paulo 	}
42159aa14a9SRui Paulo 	if (firstempty < 0)
42259aa14a9SRui Paulo 		return ENOSPC;
42359aa14a9SRui Paulo 	memcpy(&mesh_proto_paths[firstempty], mpp, sizeof(*mpp));
4246b8c1829SRui Paulo 	mesh_proto_paths[firstempty].mpp_active = 1;
42559aa14a9SRui Paulo 	return 0;
42659aa14a9SRui Paulo }
42759aa14a9SRui Paulo 
42859aa14a9SRui Paulo int
42959aa14a9SRui Paulo ieee80211_mesh_register_proto_metric(const struct
43059aa14a9SRui Paulo     ieee80211_mesh_proto_metric *mpm)
43159aa14a9SRui Paulo {
43259aa14a9SRui Paulo 	int i, firstempty = -1;
43359aa14a9SRui Paulo 
43459aa14a9SRui Paulo 	for (i = 0; i < N(mesh_proto_metrics); i++) {
4356b8c1829SRui Paulo 		if (strncmp(mpm->mpm_descr, mesh_proto_metrics[i].mpm_descr,
4366b8c1829SRui Paulo 		    IEEE80211_MESH_PROTO_DSZ) == 0)
43759aa14a9SRui Paulo 			return EEXIST;
4386b8c1829SRui Paulo 		if (!mesh_proto_metrics[i].mpm_active && firstempty == -1)
43959aa14a9SRui Paulo 			firstempty = i;
44059aa14a9SRui Paulo 	}
44159aa14a9SRui Paulo 	if (firstempty < 0)
44259aa14a9SRui Paulo 		return ENOSPC;
44359aa14a9SRui Paulo 	memcpy(&mesh_proto_metrics[firstempty], mpm, sizeof(*mpm));
4446b8c1829SRui Paulo 	mesh_proto_metrics[firstempty].mpm_active = 1;
44559aa14a9SRui Paulo 	return 0;
44659aa14a9SRui Paulo }
44759aa14a9SRui Paulo 
44859aa14a9SRui Paulo static int
44959aa14a9SRui Paulo mesh_select_proto_path(struct ieee80211vap *vap, const char *name)
45059aa14a9SRui Paulo {
45159aa14a9SRui Paulo 	struct ieee80211_mesh_state *ms = vap->iv_mesh;
45259aa14a9SRui Paulo 	int i;
45359aa14a9SRui Paulo 
45459aa14a9SRui Paulo 	for (i = 0; i < N(mesh_proto_paths); i++) {
45559aa14a9SRui Paulo 		if (strcasecmp(mesh_proto_paths[i].mpp_descr, name) == 0) {
45659aa14a9SRui Paulo 			ms->ms_ppath = &mesh_proto_paths[i];
45759aa14a9SRui Paulo 			return 0;
45859aa14a9SRui Paulo 		}
45959aa14a9SRui Paulo 	}
46059aa14a9SRui Paulo 	return ENOENT;
46159aa14a9SRui Paulo }
46259aa14a9SRui Paulo 
46359aa14a9SRui Paulo static int
46459aa14a9SRui Paulo mesh_select_proto_metric(struct ieee80211vap *vap, const char *name)
46559aa14a9SRui Paulo {
46659aa14a9SRui Paulo 	struct ieee80211_mesh_state *ms = vap->iv_mesh;
46759aa14a9SRui Paulo 	int i;
46859aa14a9SRui Paulo 
46959aa14a9SRui Paulo 	for (i = 0; i < N(mesh_proto_metrics); i++) {
47059aa14a9SRui Paulo 		if (strcasecmp(mesh_proto_metrics[i].mpm_descr, name) == 0) {
47159aa14a9SRui Paulo 			ms->ms_pmetric = &mesh_proto_metrics[i];
47259aa14a9SRui Paulo 			return 0;
47359aa14a9SRui Paulo 		}
47459aa14a9SRui Paulo 	}
47559aa14a9SRui Paulo 	return ENOENT;
47659aa14a9SRui Paulo }
47759aa14a9SRui Paulo #undef	N
47859aa14a9SRui Paulo 
47959aa14a9SRui Paulo static void
48059aa14a9SRui Paulo ieee80211_mesh_init(void)
48159aa14a9SRui Paulo {
48259aa14a9SRui Paulo 
48359aa14a9SRui Paulo 	memset(mesh_proto_paths, 0, sizeof(mesh_proto_paths));
48459aa14a9SRui Paulo 	memset(mesh_proto_metrics, 0, sizeof(mesh_proto_metrics));
48559aa14a9SRui Paulo 
48659aa14a9SRui Paulo 	/*
48759aa14a9SRui Paulo 	 * Setup mesh parameters that depends on the clock frequency.
48859aa14a9SRui Paulo 	 */
48959aa14a9SRui Paulo 	ieee80211_mesh_retrytimeout = msecs_to_ticks(40);
49059aa14a9SRui Paulo 	ieee80211_mesh_holdingtimeout = msecs_to_ticks(40);
49159aa14a9SRui Paulo 	ieee80211_mesh_confirmtimeout = msecs_to_ticks(40);
49259aa14a9SRui Paulo 
49359aa14a9SRui Paulo 	/*
49459aa14a9SRui Paulo 	 * Register action frame handlers.
49559aa14a9SRui Paulo 	 */
496ebeaa1adSMonthadar Al Jaberi 	ieee80211_recv_action_register(IEEE80211_ACTION_CAT_SELF_PROT,
49759aa14a9SRui Paulo 	    IEEE80211_ACTION_MESHPEERING_OPEN,
49859aa14a9SRui Paulo 	    mesh_recv_action_meshpeering_open);
499ebeaa1adSMonthadar Al Jaberi 	ieee80211_recv_action_register(IEEE80211_ACTION_CAT_SELF_PROT,
50059aa14a9SRui Paulo 	    IEEE80211_ACTION_MESHPEERING_CONFIRM,
50159aa14a9SRui Paulo 	    mesh_recv_action_meshpeering_confirm);
502ebeaa1adSMonthadar Al Jaberi 	ieee80211_recv_action_register(IEEE80211_ACTION_CAT_SELF_PROT,
50359aa14a9SRui Paulo 	    IEEE80211_ACTION_MESHPEERING_CLOSE,
50459aa14a9SRui Paulo 	    mesh_recv_action_meshpeering_close);
505bdd2a076SAdrian Chadd 	ieee80211_recv_action_register(IEEE80211_ACTION_CAT_MESH,
506bdd2a076SAdrian Chadd 	    IEEE80211_ACTION_MESH_LMETRIC, mesh_recv_action_meshlmetric);
50759aa14a9SRui Paulo 
508ebeaa1adSMonthadar Al Jaberi 	ieee80211_send_action_register(IEEE80211_ACTION_CAT_SELF_PROT,
50959aa14a9SRui Paulo 	    IEEE80211_ACTION_MESHPEERING_OPEN,
51059aa14a9SRui Paulo 	    mesh_send_action_meshpeering_open);
511ebeaa1adSMonthadar Al Jaberi 	ieee80211_send_action_register(IEEE80211_ACTION_CAT_SELF_PROT,
51259aa14a9SRui Paulo 	    IEEE80211_ACTION_MESHPEERING_CONFIRM,
51359aa14a9SRui Paulo 	    mesh_send_action_meshpeering_confirm);
514ebeaa1adSMonthadar Al Jaberi 	ieee80211_send_action_register(IEEE80211_ACTION_CAT_SELF_PROT,
51559aa14a9SRui Paulo 	    IEEE80211_ACTION_MESHPEERING_CLOSE,
51659aa14a9SRui Paulo 	    mesh_send_action_meshpeering_close);
517bdd2a076SAdrian Chadd 	ieee80211_send_action_register(IEEE80211_ACTION_CAT_MESH,
518bdd2a076SAdrian Chadd 	    IEEE80211_ACTION_MESH_LMETRIC,
519bdd2a076SAdrian Chadd 	    mesh_send_action_meshlmetric);
52059aa14a9SRui Paulo 
52159aa14a9SRui Paulo 	/*
52259aa14a9SRui Paulo 	 * Register Airtime Link Metric.
52359aa14a9SRui Paulo 	 */
52459aa14a9SRui Paulo 	ieee80211_mesh_register_proto_metric(&mesh_metric_airtime);
52559aa14a9SRui Paulo 
52659aa14a9SRui Paulo }
52759aa14a9SRui Paulo SYSINIT(wlan_mesh, SI_SUB_DRIVERS, SI_ORDER_FIRST, ieee80211_mesh_init, NULL);
52859aa14a9SRui Paulo 
52959aa14a9SRui Paulo void
53059aa14a9SRui Paulo ieee80211_mesh_attach(struct ieee80211com *ic)
53159aa14a9SRui Paulo {
53259aa14a9SRui Paulo 	ic->ic_vattach[IEEE80211_M_MBSS] = mesh_vattach;
53359aa14a9SRui Paulo }
53459aa14a9SRui Paulo 
53559aa14a9SRui Paulo void
53659aa14a9SRui Paulo ieee80211_mesh_detach(struct ieee80211com *ic)
53759aa14a9SRui Paulo {
53859aa14a9SRui Paulo }
53959aa14a9SRui Paulo 
54059aa14a9SRui Paulo static void
54159aa14a9SRui Paulo mesh_vdetach_peers(void *arg, struct ieee80211_node *ni)
54259aa14a9SRui Paulo {
54359aa14a9SRui Paulo 	struct ieee80211com *ic = ni->ni_ic;
54459aa14a9SRui Paulo 	uint16_t args[3];
54559aa14a9SRui Paulo 
54659aa14a9SRui Paulo 	if (ni->ni_mlstate == IEEE80211_NODE_MESH_ESTABLISHED) {
54759aa14a9SRui Paulo 		args[0] = ni->ni_mlpid;
54859aa14a9SRui Paulo 		args[1] = ni->ni_mllid;
54959aa14a9SRui Paulo 		args[2] = IEEE80211_REASON_PEER_LINK_CANCELED;
55059aa14a9SRui Paulo 		ieee80211_send_action(ni,
551ebeaa1adSMonthadar Al Jaberi 		    IEEE80211_ACTION_CAT_SELF_PROT,
55259aa14a9SRui Paulo 		    IEEE80211_ACTION_MESHPEERING_CLOSE,
55359aa14a9SRui Paulo 		    args);
55459aa14a9SRui Paulo 	}
555c104cff2SRui Paulo 	callout_drain(&ni->ni_mltimer);
55659aa14a9SRui Paulo 	/* XXX belongs in hwmp */
55759aa14a9SRui Paulo 	ieee80211_ageq_drain_node(&ic->ic_stageq,
55859aa14a9SRui Paulo 	   (void *)(uintptr_t) ieee80211_mac_hash(ic, ni->ni_macaddr));
55959aa14a9SRui Paulo }
56059aa14a9SRui Paulo 
56159aa14a9SRui Paulo static void
56259aa14a9SRui Paulo mesh_vdetach(struct ieee80211vap *vap)
56359aa14a9SRui Paulo {
56459aa14a9SRui Paulo 	struct ieee80211_mesh_state *ms = vap->iv_mesh;
56559aa14a9SRui Paulo 
566c104cff2SRui Paulo 	callout_drain(&ms->ms_cleantimer);
56759aa14a9SRui Paulo 	ieee80211_iterate_nodes(&vap->iv_ic->ic_sta, mesh_vdetach_peers,
56859aa14a9SRui Paulo 	    NULL);
56959aa14a9SRui Paulo 	ieee80211_mesh_rt_flush(vap);
57059aa14a9SRui Paulo 	mtx_destroy(&ms->ms_rt_lock);
57159aa14a9SRui Paulo 	ms->ms_ppath->mpp_vdetach(vap);
57259aa14a9SRui Paulo 	free(vap->iv_mesh, M_80211_VAP);
57359aa14a9SRui Paulo 	vap->iv_mesh = NULL;
57459aa14a9SRui Paulo }
57559aa14a9SRui Paulo 
57659aa14a9SRui Paulo static void
57759aa14a9SRui Paulo mesh_vattach(struct ieee80211vap *vap)
57859aa14a9SRui Paulo {
57959aa14a9SRui Paulo 	struct ieee80211_mesh_state *ms;
58059aa14a9SRui Paulo 	vap->iv_newstate = mesh_newstate;
58159aa14a9SRui Paulo 	vap->iv_input = mesh_input;
58259aa14a9SRui Paulo 	vap->iv_opdetach = mesh_vdetach;
58359aa14a9SRui Paulo 	vap->iv_recv_mgmt = mesh_recv_mgmt;
5840917631fSRui Paulo 	vap->iv_recv_ctl = mesh_recv_ctl;
58559aa14a9SRui Paulo 	ms = malloc(sizeof(struct ieee80211_mesh_state), M_80211_VAP,
58659aa14a9SRui Paulo 	    M_NOWAIT | M_ZERO);
58759aa14a9SRui Paulo 	if (ms == NULL) {
58859aa14a9SRui Paulo 		printf("%s: couldn't alloc MBSS state\n", __func__);
58959aa14a9SRui Paulo 		return;
59059aa14a9SRui Paulo 	}
59159aa14a9SRui Paulo 	vap->iv_mesh = ms;
59259aa14a9SRui Paulo 	ms->ms_seq = 0;
59359aa14a9SRui Paulo 	ms->ms_flags = (IEEE80211_MESHFLAGS_AP | IEEE80211_MESHFLAGS_FWD);
59459aa14a9SRui Paulo 	ms->ms_ttl = IEEE80211_MESH_DEFAULT_TTL;
59559aa14a9SRui Paulo 	TAILQ_INIT(&ms->ms_routes);
59659aa14a9SRui Paulo 	mtx_init(&ms->ms_rt_lock, "MBSS", "802.11s routing table", MTX_DEF);
597c104cff2SRui Paulo 	callout_init(&ms->ms_cleantimer, CALLOUT_MPSAFE);
59859aa14a9SRui Paulo 	mesh_select_proto_metric(vap, "AIRTIME");
59959aa14a9SRui Paulo 	KASSERT(ms->ms_pmetric, ("ms_pmetric == NULL"));
60059aa14a9SRui Paulo 	mesh_select_proto_path(vap, "HWMP");
60159aa14a9SRui Paulo 	KASSERT(ms->ms_ppath, ("ms_ppath == NULL"));
60259aa14a9SRui Paulo 	ms->ms_ppath->mpp_vattach(vap);
60359aa14a9SRui Paulo }
60459aa14a9SRui Paulo 
60559aa14a9SRui Paulo /*
60659aa14a9SRui Paulo  * IEEE80211_M_MBSS vap state machine handler.
60759aa14a9SRui Paulo  */
60859aa14a9SRui Paulo static int
60959aa14a9SRui Paulo mesh_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
61059aa14a9SRui Paulo {
61159aa14a9SRui Paulo 	struct ieee80211_mesh_state *ms = vap->iv_mesh;
61259aa14a9SRui Paulo 	struct ieee80211com *ic = vap->iv_ic;
61359aa14a9SRui Paulo 	struct ieee80211_node *ni;
61459aa14a9SRui Paulo 	enum ieee80211_state ostate;
61559aa14a9SRui Paulo 
61659aa14a9SRui Paulo 	IEEE80211_LOCK_ASSERT(ic);
61759aa14a9SRui Paulo 
61859aa14a9SRui Paulo 	ostate = vap->iv_state;
61959aa14a9SRui Paulo 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE, "%s: %s -> %s (%d)\n",
62059aa14a9SRui Paulo 	    __func__, ieee80211_state_name[ostate],
62159aa14a9SRui Paulo 	    ieee80211_state_name[nstate], arg);
62259aa14a9SRui Paulo 	vap->iv_state = nstate;		/* state transition */
62359aa14a9SRui Paulo 	if (ostate != IEEE80211_S_SCAN)
62459aa14a9SRui Paulo 		ieee80211_cancel_scan(vap);	/* background scan */
62559aa14a9SRui Paulo 	ni = vap->iv_bss;			/* NB: no reference held */
626c104cff2SRui Paulo 	if (nstate != IEEE80211_S_RUN && ostate == IEEE80211_S_RUN)
627c104cff2SRui Paulo 		callout_drain(&ms->ms_cleantimer);
62859aa14a9SRui Paulo 	switch (nstate) {
62959aa14a9SRui Paulo 	case IEEE80211_S_INIT:
63059aa14a9SRui Paulo 		switch (ostate) {
63159aa14a9SRui Paulo 		case IEEE80211_S_SCAN:
63259aa14a9SRui Paulo 			ieee80211_cancel_scan(vap);
63359aa14a9SRui Paulo 			break;
63459aa14a9SRui Paulo 		case IEEE80211_S_CAC:
63559aa14a9SRui Paulo 			ieee80211_dfs_cac_stop(vap);
63659aa14a9SRui Paulo 			break;
63759aa14a9SRui Paulo 		case IEEE80211_S_RUN:
63859aa14a9SRui Paulo 			ieee80211_iterate_nodes(&ic->ic_sta,
63959aa14a9SRui Paulo 			    mesh_vdetach_peers, NULL);
64059aa14a9SRui Paulo 			break;
64159aa14a9SRui Paulo 		default:
64259aa14a9SRui Paulo 			break;
64359aa14a9SRui Paulo 		}
64459aa14a9SRui Paulo 		if (ostate != IEEE80211_S_INIT) {
64559aa14a9SRui Paulo 			/* NB: optimize INIT -> INIT case */
64659aa14a9SRui Paulo 			ieee80211_reset_bss(vap);
647c104cff2SRui Paulo 			ieee80211_mesh_rt_flush(vap);
64859aa14a9SRui Paulo 		}
64959aa14a9SRui Paulo 		break;
65059aa14a9SRui Paulo 	case IEEE80211_S_SCAN:
65159aa14a9SRui Paulo 		switch (ostate) {
65259aa14a9SRui Paulo 		case IEEE80211_S_INIT:
65359aa14a9SRui Paulo 			if (vap->iv_des_chan != IEEE80211_CHAN_ANYC &&
65459aa14a9SRui Paulo 			    !IEEE80211_IS_CHAN_RADAR(vap->iv_des_chan) &&
65559aa14a9SRui Paulo 			    ms->ms_idlen != 0) {
65659aa14a9SRui Paulo 				/*
65759aa14a9SRui Paulo 				 * Already have a channel and a mesh ID; bypass
65859aa14a9SRui Paulo 				 * the scan and startup immediately.
65959aa14a9SRui Paulo 				 */
66059aa14a9SRui Paulo 				ieee80211_create_ibss(vap, vap->iv_des_chan);
66159aa14a9SRui Paulo 				break;
66259aa14a9SRui Paulo 			}
66359aa14a9SRui Paulo 			/*
66459aa14a9SRui Paulo 			 * Initiate a scan.  We can come here as a result
66559aa14a9SRui Paulo 			 * of an IEEE80211_IOC_SCAN_REQ too in which case
66659aa14a9SRui Paulo 			 * the vap will be marked with IEEE80211_FEXT_SCANREQ
66759aa14a9SRui Paulo 			 * and the scan request parameters will be present
66859aa14a9SRui Paulo 			 * in iv_scanreq.  Otherwise we do the default.
66959aa14a9SRui Paulo 			*/
67059aa14a9SRui Paulo 			if (vap->iv_flags_ext & IEEE80211_FEXT_SCANREQ) {
67159aa14a9SRui Paulo 				ieee80211_check_scan(vap,
67259aa14a9SRui Paulo 				    vap->iv_scanreq_flags,
67359aa14a9SRui Paulo 				    vap->iv_scanreq_duration,
67459aa14a9SRui Paulo 				    vap->iv_scanreq_mindwell,
67559aa14a9SRui Paulo 				    vap->iv_scanreq_maxdwell,
67659aa14a9SRui Paulo 				    vap->iv_scanreq_nssid, vap->iv_scanreq_ssid);
67759aa14a9SRui Paulo 				vap->iv_flags_ext &= ~IEEE80211_FEXT_SCANREQ;
67859aa14a9SRui Paulo 			} else
67959aa14a9SRui Paulo 				ieee80211_check_scan_current(vap);
68059aa14a9SRui Paulo 			break;
68159aa14a9SRui Paulo 		default:
68259aa14a9SRui Paulo 			break;
68359aa14a9SRui Paulo 		}
68459aa14a9SRui Paulo 		break;
68559aa14a9SRui Paulo 	case IEEE80211_S_CAC:
68659aa14a9SRui Paulo 		/*
68759aa14a9SRui Paulo 		 * Start CAC on a DFS channel.  We come here when starting
68859aa14a9SRui Paulo 		 * a bss on a DFS channel (see ieee80211_create_ibss).
68959aa14a9SRui Paulo 		 */
69059aa14a9SRui Paulo 		ieee80211_dfs_cac_start(vap);
69159aa14a9SRui Paulo 		break;
69259aa14a9SRui Paulo 	case IEEE80211_S_RUN:
69359aa14a9SRui Paulo 		switch (ostate) {
69459aa14a9SRui Paulo 		case IEEE80211_S_INIT:
69559aa14a9SRui Paulo 			/*
69659aa14a9SRui Paulo 			 * Already have a channel; bypass the
69759aa14a9SRui Paulo 			 * scan and startup immediately.
69859aa14a9SRui Paulo 			 * Note that ieee80211_create_ibss will call
69959aa14a9SRui Paulo 			 * back to do a RUN->RUN state change.
70059aa14a9SRui Paulo 			 */
70159aa14a9SRui Paulo 			ieee80211_create_ibss(vap,
70259aa14a9SRui Paulo 			    ieee80211_ht_adjust_channel(ic,
70359aa14a9SRui Paulo 				ic->ic_curchan, vap->iv_flags_ht));
70459aa14a9SRui Paulo 			/* NB: iv_bss is changed on return */
70559aa14a9SRui Paulo 			break;
70659aa14a9SRui Paulo 		case IEEE80211_S_CAC:
70759aa14a9SRui Paulo 			/*
70859aa14a9SRui Paulo 			 * NB: This is the normal state change when CAC
70959aa14a9SRui Paulo 			 * expires and no radar was detected; no need to
71059aa14a9SRui Paulo 			 * clear the CAC timer as it's already expired.
71159aa14a9SRui Paulo 			 */
71259aa14a9SRui Paulo 			/* fall thru... */
71359aa14a9SRui Paulo 		case IEEE80211_S_CSA:
71459aa14a9SRui Paulo #if 0
71559aa14a9SRui Paulo 			/*
71659aa14a9SRui Paulo 			 * Shorten inactivity timer of associated stations
71759aa14a9SRui Paulo 			 * to weed out sta's that don't follow a CSA.
71859aa14a9SRui Paulo 			 */
71959aa14a9SRui Paulo 			ieee80211_iterate_nodes(&ic->ic_sta, sta_csa, vap);
72059aa14a9SRui Paulo #endif
72159aa14a9SRui Paulo 			/*
72259aa14a9SRui Paulo 			 * Update bss node channel to reflect where
72359aa14a9SRui Paulo 			 * we landed after CSA.
72459aa14a9SRui Paulo 			 */
72559aa14a9SRui Paulo 			ieee80211_node_set_chan(vap->iv_bss,
72659aa14a9SRui Paulo 			    ieee80211_ht_adjust_channel(ic, ic->ic_curchan,
72759aa14a9SRui Paulo 				ieee80211_htchanflags(vap->iv_bss->ni_chan)));
72859aa14a9SRui Paulo 			/* XXX bypass debug msgs */
72959aa14a9SRui Paulo 			break;
73059aa14a9SRui Paulo 		case IEEE80211_S_SCAN:
73159aa14a9SRui Paulo 		case IEEE80211_S_RUN:
73259aa14a9SRui Paulo #ifdef IEEE80211_DEBUG
73359aa14a9SRui Paulo 			if (ieee80211_msg_debug(vap)) {
73459aa14a9SRui Paulo 				struct ieee80211_node *ni = vap->iv_bss;
73559aa14a9SRui Paulo 				ieee80211_note(vap,
73659aa14a9SRui Paulo 				    "synchronized with %s meshid ",
73759aa14a9SRui Paulo 				    ether_sprintf(ni->ni_meshid));
73859aa14a9SRui Paulo 				ieee80211_print_essid(ni->ni_meshid,
73959aa14a9SRui Paulo 				    ni->ni_meshidlen);
74059aa14a9SRui Paulo 				/* XXX MCS/HT */
74159aa14a9SRui Paulo 				printf(" channel %d\n",
74259aa14a9SRui Paulo 				    ieee80211_chan2ieee(ic, ic->ic_curchan));
74359aa14a9SRui Paulo 			}
74459aa14a9SRui Paulo #endif
74559aa14a9SRui Paulo 			break;
74659aa14a9SRui Paulo 		default:
74759aa14a9SRui Paulo 			break;
74859aa14a9SRui Paulo 		}
74959aa14a9SRui Paulo 		ieee80211_node_authorize(vap->iv_bss);
750e50821abSSam Leffler 		callout_reset(&ms->ms_cleantimer, ms->ms_ppath->mpp_inact,
751c104cff2SRui Paulo                     mesh_rt_cleanup_cb, vap);
75259aa14a9SRui Paulo 		break;
75359aa14a9SRui Paulo 	default:
75459aa14a9SRui Paulo 		break;
75559aa14a9SRui Paulo 	}
75659aa14a9SRui Paulo 	/* NB: ostate not nstate */
75759aa14a9SRui Paulo 	ms->ms_ppath->mpp_newstate(vap, ostate, arg);
75859aa14a9SRui Paulo 	return 0;
75959aa14a9SRui Paulo }
76059aa14a9SRui Paulo 
761c104cff2SRui Paulo static void
762c104cff2SRui Paulo mesh_rt_cleanup_cb(void *arg)
763c104cff2SRui Paulo {
764c104cff2SRui Paulo 	struct ieee80211vap *vap = arg;
765c104cff2SRui Paulo 	struct ieee80211_mesh_state *ms = vap->iv_mesh;
766c104cff2SRui Paulo 
767c104cff2SRui Paulo 	mesh_rt_flush_invalid(vap);
768e50821abSSam Leffler 	callout_reset(&ms->ms_cleantimer, ms->ms_ppath->mpp_inact,
769c104cff2SRui Paulo 	    mesh_rt_cleanup_cb, vap);
770c104cff2SRui Paulo }
771c104cff2SRui Paulo 
772c104cff2SRui Paulo 
77359aa14a9SRui Paulo /*
77459aa14a9SRui Paulo  * Helper function to note the Mesh Peer Link FSM change.
77559aa14a9SRui Paulo  */
77659aa14a9SRui Paulo static void
77759aa14a9SRui Paulo mesh_linkchange(struct ieee80211_node *ni, enum ieee80211_mesh_mlstate state)
77859aa14a9SRui Paulo {
77959aa14a9SRui Paulo 	struct ieee80211vap *vap = ni->ni_vap;
78059aa14a9SRui Paulo 	struct ieee80211_mesh_state *ms = vap->iv_mesh;
78159aa14a9SRui Paulo #ifdef IEEE80211_DEBUG
78259aa14a9SRui Paulo 	static const char *meshlinkstates[] = {
78359aa14a9SRui Paulo 		[IEEE80211_NODE_MESH_IDLE]		= "IDLE",
78459aa14a9SRui Paulo 		[IEEE80211_NODE_MESH_OPENSNT]		= "OPEN SENT",
78559aa14a9SRui Paulo 		[IEEE80211_NODE_MESH_OPENRCV]		= "OPEN RECEIVED",
78659aa14a9SRui Paulo 		[IEEE80211_NODE_MESH_CONFIRMRCV]	= "CONFIRM RECEIVED",
78759aa14a9SRui Paulo 		[IEEE80211_NODE_MESH_ESTABLISHED]	= "ESTABLISHED",
78859aa14a9SRui Paulo 		[IEEE80211_NODE_MESH_HOLDING]		= "HOLDING"
78959aa14a9SRui Paulo 	};
79059aa14a9SRui Paulo #endif
79159aa14a9SRui Paulo 	IEEE80211_NOTE(vap, IEEE80211_MSG_MESH,
79259aa14a9SRui Paulo 	    ni, "peer link: %s -> %s",
79359aa14a9SRui Paulo 	    meshlinkstates[ni->ni_mlstate], meshlinkstates[state]);
79459aa14a9SRui Paulo 
79559aa14a9SRui Paulo 	/* track neighbor count */
79659aa14a9SRui Paulo 	if (state == IEEE80211_NODE_MESH_ESTABLISHED &&
79759aa14a9SRui Paulo 	    ni->ni_mlstate != IEEE80211_NODE_MESH_ESTABLISHED) {
79859aa14a9SRui Paulo 		KASSERT(ms->ms_neighbors < 65535, ("neighbor count overflow"));
79959aa14a9SRui Paulo 		ms->ms_neighbors++;
800d093681cSRui Paulo 		ieee80211_beacon_notify(vap, IEEE80211_BEACON_MESHCONF);
80159aa14a9SRui Paulo 	} else if (ni->ni_mlstate == IEEE80211_NODE_MESH_ESTABLISHED &&
80259aa14a9SRui Paulo 	    state != IEEE80211_NODE_MESH_ESTABLISHED) {
80359aa14a9SRui Paulo 		KASSERT(ms->ms_neighbors > 0, ("neighbor count 0"));
80459aa14a9SRui Paulo 		ms->ms_neighbors--;
805d093681cSRui Paulo 		ieee80211_beacon_notify(vap, IEEE80211_BEACON_MESHCONF);
80659aa14a9SRui Paulo 	}
80759aa14a9SRui Paulo 	ni->ni_mlstate = state;
808c104cff2SRui Paulo 	switch (state) {
809c104cff2SRui Paulo 	case IEEE80211_NODE_MESH_HOLDING:
81059aa14a9SRui Paulo 		ms->ms_ppath->mpp_peerdown(ni);
811c104cff2SRui Paulo 		break;
812c104cff2SRui Paulo 	case IEEE80211_NODE_MESH_ESTABLISHED:
813c104cff2SRui Paulo 		ieee80211_mesh_discover(vap, ni->ni_macaddr, NULL);
814c104cff2SRui Paulo 		break;
815c104cff2SRui Paulo 	default:
816c104cff2SRui Paulo 		break;
817c104cff2SRui Paulo 	}
81859aa14a9SRui Paulo }
81959aa14a9SRui Paulo 
82059aa14a9SRui Paulo /*
82159aa14a9SRui Paulo  * Helper function to generate a unique local ID required for mesh
82259aa14a9SRui Paulo  * peer establishment.
82359aa14a9SRui Paulo  */
82459aa14a9SRui Paulo static void
82559aa14a9SRui Paulo mesh_checkid(void *arg, struct ieee80211_node *ni)
82659aa14a9SRui Paulo {
82759aa14a9SRui Paulo 	uint16_t *r = arg;
82859aa14a9SRui Paulo 
82959aa14a9SRui Paulo 	if (*r == ni->ni_mllid)
83059aa14a9SRui Paulo 		*(uint16_t *)arg = 0;
83159aa14a9SRui Paulo }
83259aa14a9SRui Paulo 
83359aa14a9SRui Paulo static uint32_t
83459aa14a9SRui Paulo mesh_generateid(struct ieee80211vap *vap)
83559aa14a9SRui Paulo {
83659aa14a9SRui Paulo 	int maxiter = 4;
83759aa14a9SRui Paulo 	uint16_t r;
83859aa14a9SRui Paulo 
83959aa14a9SRui Paulo 	do {
84059aa14a9SRui Paulo 		get_random_bytes(&r, 2);
84159aa14a9SRui Paulo 		ieee80211_iterate_nodes(&vap->iv_ic->ic_sta, mesh_checkid, &r);
84259aa14a9SRui Paulo 		maxiter--;
84359aa14a9SRui Paulo 	} while (r == 0 && maxiter > 0);
84459aa14a9SRui Paulo 	return r;
84559aa14a9SRui Paulo }
84659aa14a9SRui Paulo 
84759aa14a9SRui Paulo /*
84859aa14a9SRui Paulo  * Verifies if we already received this packet by checking its
84959aa14a9SRui Paulo  * sequence number.
8503ca80f0dSRui Paulo  * Returns 0 if the frame is to be accepted, 1 otherwise.
85159aa14a9SRui Paulo  */
85259aa14a9SRui Paulo static int
85359aa14a9SRui Paulo mesh_checkpseq(struct ieee80211vap *vap,
85459aa14a9SRui Paulo     const uint8_t source[IEEE80211_ADDR_LEN], uint32_t seq)
85559aa14a9SRui Paulo {
85659aa14a9SRui Paulo 	struct ieee80211_mesh_route *rt;
85759aa14a9SRui Paulo 
85859aa14a9SRui Paulo 	rt = ieee80211_mesh_rt_find(vap, source);
85959aa14a9SRui Paulo 	if (rt == NULL) {
8603ca80f0dSRui Paulo 		rt = ieee80211_mesh_rt_add(vap, source);
8613ca80f0dSRui Paulo 		if (rt == NULL) {
8623ca80f0dSRui Paulo 			IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH, source,
8633ca80f0dSRui Paulo 			    "%s", "add mcast route failed");
8643ca80f0dSRui Paulo 			vap->iv_stats.is_mesh_rtaddfailed++;
8653ca80f0dSRui Paulo 			return 1;
8663ca80f0dSRui Paulo 		}
867c104cff2SRui Paulo 		IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH, source,
868c104cff2SRui Paulo 		    "add mcast route, mesh seqno %d", seq);
86959aa14a9SRui Paulo 		rt->rt_lastmseq = seq;
87059aa14a9SRui Paulo 		return 0;
87159aa14a9SRui Paulo 	}
87259aa14a9SRui Paulo 	if (IEEE80211_MESH_SEQ_GEQ(rt->rt_lastmseq, seq)) {
87359aa14a9SRui Paulo 		return 1;
87459aa14a9SRui Paulo 	} else {
87559aa14a9SRui Paulo 		rt->rt_lastmseq = seq;
87659aa14a9SRui Paulo 		return 0;
87759aa14a9SRui Paulo 	}
87859aa14a9SRui Paulo }
87959aa14a9SRui Paulo 
88059aa14a9SRui Paulo /*
88159aa14a9SRui Paulo  * Iterate the routing table and locate the next hop.
88259aa14a9SRui Paulo  */
88359aa14a9SRui Paulo static struct ieee80211_node *
88459aa14a9SRui Paulo mesh_find_txnode(struct ieee80211vap *vap,
88559aa14a9SRui Paulo     const uint8_t dest[IEEE80211_ADDR_LEN])
88659aa14a9SRui Paulo {
88759aa14a9SRui Paulo 	struct ieee80211_mesh_route *rt;
88859aa14a9SRui Paulo 
88959aa14a9SRui Paulo 	rt = ieee80211_mesh_rt_find(vap, dest);
89059aa14a9SRui Paulo 	if (rt == NULL)
89159aa14a9SRui Paulo 		return NULL;
892c104cff2SRui Paulo 	if ((rt->rt_flags & IEEE80211_MESHRT_FLAGS_VALID) == 0 ||
893c104cff2SRui Paulo 	    (rt->rt_flags & IEEE80211_MESHRT_FLAGS_PROXY)) {
894c104cff2SRui Paulo 		IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH, dest,
895c104cff2SRui Paulo 		    "%s: !valid or proxy, flags 0x%x", __func__, rt->rt_flags);
896c104cff2SRui Paulo 		/* XXX stat */
897c104cff2SRui Paulo 		return NULL;
898c104cff2SRui Paulo 	}
89959aa14a9SRui Paulo 	return ieee80211_find_txnode(vap, rt->rt_nexthop);
90059aa14a9SRui Paulo }
90159aa14a9SRui Paulo 
90259aa14a9SRui Paulo /*
90359aa14a9SRui Paulo  * Forward the specified frame.
90459aa14a9SRui Paulo  * Decrement the TTL and set TA to our MAC address.
90559aa14a9SRui Paulo  */
90659aa14a9SRui Paulo static void
90759aa14a9SRui Paulo mesh_forward(struct ieee80211vap *vap, struct mbuf *m,
90859aa14a9SRui Paulo     const struct ieee80211_meshcntl *mc)
90959aa14a9SRui Paulo {
91059aa14a9SRui Paulo 	struct ieee80211com *ic = vap->iv_ic;
91159aa14a9SRui Paulo 	struct ieee80211_mesh_state *ms = vap->iv_mesh;
91259aa14a9SRui Paulo 	struct ifnet *ifp = vap->iv_ifp;
91359aa14a9SRui Paulo 	struct ifnet *parent = ic->ic_ifp;
91459aa14a9SRui Paulo 	const struct ieee80211_frame *wh =
91559aa14a9SRui Paulo 	    mtod(m, const struct ieee80211_frame *);
91659aa14a9SRui Paulo 	struct mbuf *mcopy;
91759aa14a9SRui Paulo 	struct ieee80211_meshcntl *mccopy;
91859aa14a9SRui Paulo 	struct ieee80211_frame *whcopy;
91959aa14a9SRui Paulo 	struct ieee80211_node *ni;
92059aa14a9SRui Paulo 	int err;
92159aa14a9SRui Paulo 
9223c314f6dSMonthadar Al Jaberi 	/*
9233c314f6dSMonthadar Al Jaberi 	 * mesh ttl of 1 means we are the last one receving it,
9243c314f6dSMonthadar Al Jaberi 	 * according to amendment we decrement and then check if
9253c314f6dSMonthadar Al Jaberi 	 * 0, if so we dont forward.
9263c314f6dSMonthadar Al Jaberi 	 */
9273c314f6dSMonthadar Al Jaberi 	if (mc->mc_ttl < 1) {
92859aa14a9SRui Paulo 		IEEE80211_NOTE_FRAME(vap, IEEE80211_MSG_MESH, wh,
9293c314f6dSMonthadar Al Jaberi 		    "%s", "frame not fwd'd, ttl 1");
93059aa14a9SRui Paulo 		vap->iv_stats.is_mesh_fwd_ttl++;
93159aa14a9SRui Paulo 		return;
93259aa14a9SRui Paulo 	}
93359aa14a9SRui Paulo 	if (!(ms->ms_flags & IEEE80211_MESHFLAGS_FWD)) {
93459aa14a9SRui Paulo 		IEEE80211_NOTE_FRAME(vap, IEEE80211_MSG_MESH, wh,
93559aa14a9SRui Paulo 		    "%s", "frame not fwd'd, fwding disabled");
93659aa14a9SRui Paulo 		vap->iv_stats.is_mesh_fwd_disabled++;
93759aa14a9SRui Paulo 		return;
93859aa14a9SRui Paulo 	}
93959aa14a9SRui Paulo 	mcopy = m_dup(m, M_DONTWAIT);
94059aa14a9SRui Paulo 	if (mcopy == NULL) {
94159aa14a9SRui Paulo 		IEEE80211_NOTE_FRAME(vap, IEEE80211_MSG_MESH, wh,
94259aa14a9SRui Paulo 		    "%s", "frame not fwd'd, cannot dup");
94359aa14a9SRui Paulo 		vap->iv_stats.is_mesh_fwd_nobuf++;
94459aa14a9SRui Paulo 		ifp->if_oerrors++;
94559aa14a9SRui Paulo 		return;
94659aa14a9SRui Paulo 	}
94759aa14a9SRui Paulo 	mcopy = m_pullup(mcopy, ieee80211_hdrspace(ic, wh) +
94859aa14a9SRui Paulo 	    sizeof(struct ieee80211_meshcntl));
94959aa14a9SRui Paulo 	if (mcopy == NULL) {
95059aa14a9SRui Paulo 		IEEE80211_NOTE_FRAME(vap, IEEE80211_MSG_MESH, wh,
95159aa14a9SRui Paulo 		    "%s", "frame not fwd'd, too short");
95259aa14a9SRui Paulo 		vap->iv_stats.is_mesh_fwd_tooshort++;
95359aa14a9SRui Paulo 		ifp->if_oerrors++;
95459aa14a9SRui Paulo 		m_freem(mcopy);
95559aa14a9SRui Paulo 		return;
95659aa14a9SRui Paulo 	}
95759aa14a9SRui Paulo 	whcopy = mtod(mcopy, struct ieee80211_frame *);
95859aa14a9SRui Paulo 	mccopy = (struct ieee80211_meshcntl *)
95959aa14a9SRui Paulo 	    (mtod(mcopy, uint8_t *) + ieee80211_hdrspace(ic, wh));
96059aa14a9SRui Paulo 	/* XXX clear other bits? */
96159aa14a9SRui Paulo 	whcopy->i_fc[1] &= ~IEEE80211_FC1_RETRY;
96259aa14a9SRui Paulo 	IEEE80211_ADDR_COPY(whcopy->i_addr2, vap->iv_myaddr);
96359aa14a9SRui Paulo 	if (IEEE80211_IS_MULTICAST(wh->i_addr1)) {
96459aa14a9SRui Paulo 		ni = ieee80211_ref_node(vap->iv_bss);
96559aa14a9SRui Paulo 		mcopy->m_flags |= M_MCAST;
96659aa14a9SRui Paulo 	} else {
96759aa14a9SRui Paulo 		ni = mesh_find_txnode(vap, whcopy->i_addr3);
96859aa14a9SRui Paulo 		if (ni == NULL) {
9693c314f6dSMonthadar Al Jaberi 			/*
9703c314f6dSMonthadar Al Jaberi 			 * [Optional] any of the following three actions:
9713c314f6dSMonthadar Al Jaberi 			 * o silently discard
9723c314f6dSMonthadar Al Jaberi 			 * o trigger a path discovery
9733c314f6dSMonthadar Al Jaberi 			 * o inform TA that meshDA is unreachable.
9743c314f6dSMonthadar Al Jaberi 			 */
97559aa14a9SRui Paulo 			IEEE80211_NOTE_FRAME(vap, IEEE80211_MSG_MESH, wh,
97659aa14a9SRui Paulo 			    "%s", "frame not fwd'd, no path");
97759aa14a9SRui Paulo 			vap->iv_stats.is_mesh_fwd_nopath++;
97859aa14a9SRui Paulo 			m_freem(mcopy);
97959aa14a9SRui Paulo 			return;
98059aa14a9SRui Paulo 		}
98159aa14a9SRui Paulo 		IEEE80211_ADDR_COPY(whcopy->i_addr1, ni->ni_macaddr);
98259aa14a9SRui Paulo 	}
98359aa14a9SRui Paulo 	KASSERT(mccopy->mc_ttl > 0, ("%s called with wrong ttl", __func__));
98459aa14a9SRui Paulo 	mccopy->mc_ttl--;
98559aa14a9SRui Paulo 
98659aa14a9SRui Paulo 	/* XXX calculate priority so drivers can find the tx queue */
98759aa14a9SRui Paulo 	M_WME_SETAC(mcopy, WME_AC_BE);
98859aa14a9SRui Paulo 
98959aa14a9SRui Paulo 	/* XXX do we know m_nextpkt is NULL? */
99059aa14a9SRui Paulo 	mcopy->m_pkthdr.rcvif = (void *) ni;
99159aa14a9SRui Paulo 	err = parent->if_transmit(parent, mcopy);
99259aa14a9SRui Paulo 	if (err != 0) {
99359aa14a9SRui Paulo 		/* NB: IFQ_HANDOFF reclaims mbuf */
99459aa14a9SRui Paulo 		ieee80211_free_node(ni);
99559aa14a9SRui Paulo 	} else {
99659aa14a9SRui Paulo 		ifp->if_opackets++;
99759aa14a9SRui Paulo 	}
99859aa14a9SRui Paulo }
99959aa14a9SRui Paulo 
1000c104cff2SRui Paulo static struct mbuf *
1001c104cff2SRui Paulo mesh_decap(struct ieee80211vap *vap, struct mbuf *m, int hdrlen, int meshdrlen)
1002c104cff2SRui Paulo {
1003c104cff2SRui Paulo #define	WHDIR(wh)	((wh)->i_fc[1] & IEEE80211_FC1_DIR_MASK)
10043c314f6dSMonthadar Al Jaberi #define	MC01(mc)	((const struct ieee80211_meshcntl_ae01 *)mc)
1005c104cff2SRui Paulo 	uint8_t b[sizeof(struct ieee80211_qosframe_addr4) +
10063c314f6dSMonthadar Al Jaberi 		  sizeof(struct ieee80211_meshcntl_ae10)];
1007c104cff2SRui Paulo 	const struct ieee80211_qosframe_addr4 *wh;
1008c104cff2SRui Paulo 	const struct ieee80211_meshcntl_ae10 *mc;
1009c104cff2SRui Paulo 	struct ether_header *eh;
1010c104cff2SRui Paulo 	struct llc *llc;
1011c104cff2SRui Paulo 	int ae;
1012c104cff2SRui Paulo 
1013c104cff2SRui Paulo 	if (m->m_len < hdrlen + sizeof(*llc) &&
1014c104cff2SRui Paulo 	    (m = m_pullup(m, hdrlen + sizeof(*llc))) == NULL) {
1015c104cff2SRui Paulo 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_ANY,
1016c104cff2SRui Paulo 		    "discard data frame: %s", "m_pullup failed");
1017c104cff2SRui Paulo 		vap->iv_stats.is_rx_tooshort++;
1018c104cff2SRui Paulo 		return NULL;
1019c104cff2SRui Paulo 	}
1020c104cff2SRui Paulo 	memcpy(b, mtod(m, caddr_t), hdrlen);
1021c104cff2SRui Paulo 	wh = (const struct ieee80211_qosframe_addr4 *)&b[0];
1022c104cff2SRui Paulo 	mc = (const struct ieee80211_meshcntl_ae10 *)&b[hdrlen - meshdrlen];
1023c104cff2SRui Paulo 	KASSERT(WHDIR(wh) == IEEE80211_FC1_DIR_FROMDS ||
1024c104cff2SRui Paulo 		WHDIR(wh) == IEEE80211_FC1_DIR_DSTODS,
1025c104cff2SRui Paulo 	    ("bogus dir, fc 0x%x:0x%x", wh->i_fc[0], wh->i_fc[1]));
1026c104cff2SRui Paulo 
1027c104cff2SRui Paulo 	llc = (struct llc *)(mtod(m, caddr_t) + hdrlen);
1028c104cff2SRui Paulo 	if (llc->llc_dsap == LLC_SNAP_LSAP && llc->llc_ssap == LLC_SNAP_LSAP &&
1029c104cff2SRui Paulo 	    llc->llc_control == LLC_UI && llc->llc_snap.org_code[0] == 0 &&
1030c104cff2SRui Paulo 	    llc->llc_snap.org_code[1] == 0 && llc->llc_snap.org_code[2] == 0 &&
1031c104cff2SRui Paulo 	    /* NB: preserve AppleTalk frames that have a native SNAP hdr */
1032c104cff2SRui Paulo 	    !(llc->llc_snap.ether_type == htons(ETHERTYPE_AARP) ||
1033c104cff2SRui Paulo 	      llc->llc_snap.ether_type == htons(ETHERTYPE_IPX))) {
1034c104cff2SRui Paulo 		m_adj(m, hdrlen + sizeof(struct llc) - sizeof(*eh));
1035c104cff2SRui Paulo 		llc = NULL;
1036c104cff2SRui Paulo 	} else {
1037c104cff2SRui Paulo 		m_adj(m, hdrlen - sizeof(*eh));
1038c104cff2SRui Paulo 	}
1039c104cff2SRui Paulo 	eh = mtod(m, struct ether_header *);
10403c314f6dSMonthadar Al Jaberi 	ae = mc->mc_flags & IEEE80211_MESH_AE_MASK;
1041c104cff2SRui Paulo 	if (WHDIR(wh) == IEEE80211_FC1_DIR_FROMDS) {
1042c104cff2SRui Paulo 		IEEE80211_ADDR_COPY(eh->ether_dhost, wh->i_addr1);
10433c314f6dSMonthadar Al Jaberi 		if (ae == IEEE80211_MESH_AE_00) {
1044c104cff2SRui Paulo 			IEEE80211_ADDR_COPY(eh->ether_shost, wh->i_addr3);
10453c314f6dSMonthadar Al Jaberi 		} else if (ae == IEEE80211_MESH_AE_01) {
10463c314f6dSMonthadar Al Jaberi 			IEEE80211_ADDR_COPY(eh->ether_shost,
10473c314f6dSMonthadar Al Jaberi 			    MC01(mc)->mc_addr4);
1048c104cff2SRui Paulo 		} else {
1049c104cff2SRui Paulo 			IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
1050c104cff2SRui Paulo 			    (const struct ieee80211_frame *)wh, NULL,
1051c104cff2SRui Paulo 			    "bad AE %d", ae);
1052c104cff2SRui Paulo 			vap->iv_stats.is_mesh_badae++;
1053c104cff2SRui Paulo 			m_freem(m);
1054c104cff2SRui Paulo 			return NULL;
1055c104cff2SRui Paulo 		}
1056c104cff2SRui Paulo 	} else {
10573c314f6dSMonthadar Al Jaberi 		if (ae == IEEE80211_MESH_AE_00) {
1058c104cff2SRui Paulo 			IEEE80211_ADDR_COPY(eh->ether_dhost, wh->i_addr3);
1059c104cff2SRui Paulo 			IEEE80211_ADDR_COPY(eh->ether_shost, wh->i_addr4);
10603c314f6dSMonthadar Al Jaberi 		} else if (ae == IEEE80211_MESH_AE_10) {
10613c314f6dSMonthadar Al Jaberi 			IEEE80211_ADDR_COPY(eh->ether_dhost, mc->mc_addr5);
10623c314f6dSMonthadar Al Jaberi 			IEEE80211_ADDR_COPY(eh->ether_shost, mc->mc_addr6);
1063c104cff2SRui Paulo 		} else {
1064c104cff2SRui Paulo 			IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
1065c104cff2SRui Paulo 			    (const struct ieee80211_frame *)wh, NULL,
1066c104cff2SRui Paulo 			    "bad AE %d", ae);
1067c104cff2SRui Paulo 			vap->iv_stats.is_mesh_badae++;
1068c104cff2SRui Paulo 			m_freem(m);
1069c104cff2SRui Paulo 			return NULL;
1070c104cff2SRui Paulo 		}
1071c104cff2SRui Paulo 	}
1072c104cff2SRui Paulo #ifdef ALIGNED_POINTER
1073c104cff2SRui Paulo 	if (!ALIGNED_POINTER(mtod(m, caddr_t) + sizeof(*eh), uint32_t)) {
1074c104cff2SRui Paulo 		m = ieee80211_realign(vap, m, sizeof(*eh));
1075c104cff2SRui Paulo 		if (m == NULL)
1076c104cff2SRui Paulo 			return NULL;
1077c104cff2SRui Paulo 	}
1078c104cff2SRui Paulo #endif /* ALIGNED_POINTER */
1079c104cff2SRui Paulo 	if (llc != NULL) {
1080c104cff2SRui Paulo 		eh = mtod(m, struct ether_header *);
1081c104cff2SRui Paulo 		eh->ether_type = htons(m->m_pkthdr.len - sizeof(*eh));
1082c104cff2SRui Paulo 	}
1083c104cff2SRui Paulo 	return m;
1084c104cff2SRui Paulo #undef	WDIR
10853c314f6dSMonthadar Al Jaberi #undef	MC01
1086c104cff2SRui Paulo }
1087c104cff2SRui Paulo 
1088c104cff2SRui Paulo /*
1089c104cff2SRui Paulo  * Return non-zero if the unicast mesh data frame should be processed
1090c104cff2SRui Paulo  * locally.  Frames that are not proxy'd have our address, otherwise
1091c104cff2SRui Paulo  * we need to consult the routing table to look for a proxy entry.
1092c104cff2SRui Paulo  */
1093c104cff2SRui Paulo static __inline int
1094c104cff2SRui Paulo mesh_isucastforme(struct ieee80211vap *vap, const struct ieee80211_frame *wh,
1095c104cff2SRui Paulo     const struct ieee80211_meshcntl *mc)
1096c104cff2SRui Paulo {
1097c104cff2SRui Paulo 	int ae = mc->mc_flags & 3;
1098c104cff2SRui Paulo 
1099c104cff2SRui Paulo 	KASSERT((wh->i_fc[1] & IEEE80211_FC1_DIR_MASK) == IEEE80211_FC1_DIR_DSTODS,
1100c104cff2SRui Paulo 	    ("bad dir 0x%x:0x%x", wh->i_fc[0], wh->i_fc[1]));
11013c314f6dSMonthadar Al Jaberi 	KASSERT(ae == IEEE80211_MESH_AE_00 || ae == IEEE80211_MESH_AE_10,
11023c314f6dSMonthadar Al Jaberi 	    ("bad AE %d", ae));
11033c314f6dSMonthadar Al Jaberi 	if (ae == IEEE80211_MESH_AE_10) {	/* ucast w/ proxy */
1104c104cff2SRui Paulo 		const struct ieee80211_meshcntl_ae10 *mc10 =
1105c104cff2SRui Paulo 		    (const struct ieee80211_meshcntl_ae10 *) mc;
1106c104cff2SRui Paulo 		struct ieee80211_mesh_route *rt =
11073c314f6dSMonthadar Al Jaberi 		    ieee80211_mesh_rt_find(vap, mc10->mc_addr5);
1108c104cff2SRui Paulo 		/* check for proxy route to ourself */
1109c104cff2SRui Paulo 		return (rt != NULL &&
1110c104cff2SRui Paulo 		    (rt->rt_flags & IEEE80211_MESHRT_FLAGS_PROXY));
1111c104cff2SRui Paulo 	} else					/* ucast w/o proxy */
1112c104cff2SRui Paulo 		return IEEE80211_ADDR_EQ(wh->i_addr3, vap->iv_myaddr);
1113c104cff2SRui Paulo }
1114c104cff2SRui Paulo 
11153c314f6dSMonthadar Al Jaberi /*
11163c314f6dSMonthadar Al Jaberi  * Verifies transmitter, updates lifetime, precursor list and forwards data.
11173c314f6dSMonthadar Al Jaberi  * > 0 means we have forwarded data and no need to process locally
11183c314f6dSMonthadar Al Jaberi  * == 0 means we want to process locally (and we may have forwarded data
11193c314f6dSMonthadar Al Jaberi  * < 0 means there was an error and data should be discarded
11203c314f6dSMonthadar Al Jaberi  */
11213c314f6dSMonthadar Al Jaberi static int
11223c314f6dSMonthadar Al Jaberi mesh_recv_indiv_data_to_fwrd(struct ieee80211vap *vap, struct mbuf *m,
11233c314f6dSMonthadar Al Jaberi     struct ieee80211_frame *wh, const struct ieee80211_meshcntl *mc)
11243c314f6dSMonthadar Al Jaberi {
11254f3a27aeSMonthadar Al Jaberi 	struct ieee80211_qosframe_addr4 *qwh;
11264f3a27aeSMonthadar Al Jaberi 	struct ieee80211_mesh_state *ms = vap->iv_mesh;
11274f3a27aeSMonthadar Al Jaberi 	struct ieee80211_mesh_route *rt_meshda, *rt_meshsa;
11284f3a27aeSMonthadar Al Jaberi 
11294f3a27aeSMonthadar Al Jaberi 	qwh = (struct ieee80211_qosframe_addr4 *)wh;
11303c314f6dSMonthadar Al Jaberi 
11313c314f6dSMonthadar Al Jaberi 	/*
11323c314f6dSMonthadar Al Jaberi 	 * TODO:
11333c314f6dSMonthadar Al Jaberi 	 * o verify addr2 is  a legitimate transmitter
11343c314f6dSMonthadar Al Jaberi 	 * o lifetime of precursor of addr3 (addr2) is max(init, curr)
11353c314f6dSMonthadar Al Jaberi 	 * o lifetime of precursor of addr4 (nexthop) is max(init, curr)
11363c314f6dSMonthadar Al Jaberi 	 */
11373c314f6dSMonthadar Al Jaberi 
11384f3a27aeSMonthadar Al Jaberi 	/* set lifetime of addr3 (meshDA) to initial value */
11394f3a27aeSMonthadar Al Jaberi 	rt_meshda = ieee80211_mesh_rt_find(vap, qwh->i_addr3);
1140*737a965eSMonthadar Al Jaberi 	if (rt_meshda == NULL) {
1141*737a965eSMonthadar Al Jaberi 		IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH, qwh->i_addr2,
1142*737a965eSMonthadar Al Jaberi 		    "no route to meshDA(%6D)", qwh->i_addr3, ":");
1143*737a965eSMonthadar Al Jaberi 		/*
1144*737a965eSMonthadar Al Jaberi 		 * [Optional] any of the following three actions:
1145*737a965eSMonthadar Al Jaberi 		 * o silently discard 				[X]
1146*737a965eSMonthadar Al Jaberi 		 * o trigger a path discovery			[ ]
1147*737a965eSMonthadar Al Jaberi 		 * o inform TA that meshDA is unknown.		[ ]
1148*737a965eSMonthadar Al Jaberi 		 */
1149*737a965eSMonthadar Al Jaberi 		/* XXX: stats */
1150*737a965eSMonthadar Al Jaberi 		return (-1);
1151*737a965eSMonthadar Al Jaberi 	}
1152*737a965eSMonthadar Al Jaberi 
11534f3a27aeSMonthadar Al Jaberi 	ieee80211_mesh_rt_update(rt_meshda, ticks_to_msecs(
11544f3a27aeSMonthadar Al Jaberi 	    ms->ms_ppath->mpp_inact));
11554f3a27aeSMonthadar Al Jaberi 
11564f3a27aeSMonthadar Al Jaberi 	/* set lifetime of addr4 (meshSA) to initial value */
11574f3a27aeSMonthadar Al Jaberi 	rt_meshsa = ieee80211_mesh_rt_find(vap, qwh->i_addr4);
11584f3a27aeSMonthadar Al Jaberi 	KASSERT(rt_meshsa != NULL, ("no route"));
11594f3a27aeSMonthadar Al Jaberi 	ieee80211_mesh_rt_update(rt_meshsa, ticks_to_msecs(
11604f3a27aeSMonthadar Al Jaberi 	    ms->ms_ppath->mpp_inact));
11614f3a27aeSMonthadar Al Jaberi 
11623c314f6dSMonthadar Al Jaberi 	mesh_forward(vap, m, mc);
11633c314f6dSMonthadar Al Jaberi 	return (1); /* dont process locally */
11643c314f6dSMonthadar Al Jaberi }
11653c314f6dSMonthadar Al Jaberi 
11663c314f6dSMonthadar Al Jaberi /*
11673c314f6dSMonthadar Al Jaberi  * Verifies transmitter, updates lifetime, precursor list and process data
11683c314f6dSMonthadar Al Jaberi  * locally, if data is is proxy with AE = 10 it could mean data should go
11693c314f6dSMonthadar Al Jaberi  * on another mesh path or data should be forwarded to the DS.
11703c314f6dSMonthadar Al Jaberi  *
11713c314f6dSMonthadar Al Jaberi  * > 0 means we have forwarded data and no need to process locally
11723c314f6dSMonthadar Al Jaberi  * == 0 means we want to process locally (and we may have forwarded data
11733c314f6dSMonthadar Al Jaberi  * < 0 means there was an error and data should be discarded
11743c314f6dSMonthadar Al Jaberi  */
11753c314f6dSMonthadar Al Jaberi static int
11763c314f6dSMonthadar Al Jaberi mesh_recv_indiv_data_to_me(struct ieee80211vap *vap, struct mbuf *m,
11773c314f6dSMonthadar Al Jaberi     struct ieee80211_frame *wh, const struct ieee80211_meshcntl *mc)
11783c314f6dSMonthadar Al Jaberi {
11793c314f6dSMonthadar Al Jaberi 	struct ieee80211_qosframe_addr4 *qwh;
11803c314f6dSMonthadar Al Jaberi 	const struct ieee80211_meshcntl_ae10 *mc10;
11814f3a27aeSMonthadar Al Jaberi 	struct ieee80211_mesh_state *ms = vap->iv_mesh;
11823c314f6dSMonthadar Al Jaberi 	struct ieee80211_mesh_route *rt;
11833c314f6dSMonthadar Al Jaberi 	int ae;
11843c314f6dSMonthadar Al Jaberi 
11853c314f6dSMonthadar Al Jaberi 	qwh = (struct ieee80211_qosframe_addr4 *)wh;
11863c314f6dSMonthadar Al Jaberi 	mc10 = (const struct ieee80211_meshcntl_ae10 *)mc;
11873c314f6dSMonthadar Al Jaberi 
11883c314f6dSMonthadar Al Jaberi 	/*
11893c314f6dSMonthadar Al Jaberi 	 * TODO:
11903c314f6dSMonthadar Al Jaberi 	 * o verify addr2 is  a legitimate transmitter
11913c314f6dSMonthadar Al Jaberi 	 * o lifetime of precursor entry is max(init, curr)
11923c314f6dSMonthadar Al Jaberi 	 */
11933c314f6dSMonthadar Al Jaberi 
11944f3a27aeSMonthadar Al Jaberi 	/* set lifetime of addr4 (meshSA) to initial value */
11954f3a27aeSMonthadar Al Jaberi 	rt = ieee80211_mesh_rt_find(vap, qwh->i_addr4);
11964f3a27aeSMonthadar Al Jaberi 	KASSERT(rt != NULL, ("no route"));
11974f3a27aeSMonthadar Al Jaberi 	ieee80211_mesh_rt_update(rt, ticks_to_msecs(ms->ms_ppath->mpp_inact));
11984f3a27aeSMonthadar Al Jaberi 	rt = NULL;
11994f3a27aeSMonthadar Al Jaberi 
12003c314f6dSMonthadar Al Jaberi 	ae = mc10->mc_flags & IEEE80211_MESH_AE_MASK;
12013c314f6dSMonthadar Al Jaberi 	KASSERT(ae == IEEE80211_MESH_AE_00 ||
12023c314f6dSMonthadar Al Jaberi 	    ae == IEEE80211_MESH_AE_10, ("bad AE %d", ae));
12033c314f6dSMonthadar Al Jaberi 	if (ae == IEEE80211_MESH_AE_10) {
12043c314f6dSMonthadar Al Jaberi 		if (IEEE80211_ADDR_EQ(mc10->mc_addr5, qwh->i_addr3)) {
12053c314f6dSMonthadar Al Jaberi 			return (0); /* process locally */
12063c314f6dSMonthadar Al Jaberi 		}
12073c314f6dSMonthadar Al Jaberi 
12083c314f6dSMonthadar Al Jaberi 		rt =  ieee80211_mesh_rt_find(vap, mc10->mc_addr5);
12093c314f6dSMonthadar Al Jaberi 		if (rt != NULL &&
12103c314f6dSMonthadar Al Jaberi 		    (rt->rt_flags & IEEE80211_MESHRT_FLAGS_VALID) &&
12113c314f6dSMonthadar Al Jaberi 		    (rt->rt_flags & IEEE80211_MESHRT_FLAGS_PROXY) == 0) {
12123c314f6dSMonthadar Al Jaberi 			/*
12133c314f6dSMonthadar Al Jaberi 			 * Forward on another mesh-path, according to
12143c314f6dSMonthadar Al Jaberi 			 * amendment as specified in 9.32.4.1
12153c314f6dSMonthadar Al Jaberi 			 */
12163c314f6dSMonthadar Al Jaberi 			IEEE80211_ADDR_COPY(qwh->i_addr3, mc10->mc_addr5);
12173c314f6dSMonthadar Al Jaberi 			mesh_forward(vap, m,
12183c314f6dSMonthadar Al Jaberi 			    (const struct ieee80211_meshcntl *)mc10);
12193c314f6dSMonthadar Al Jaberi 			return (1); /* dont process locally */
12203c314f6dSMonthadar Al Jaberi 		}
12213c314f6dSMonthadar Al Jaberi 		/*
12223c314f6dSMonthadar Al Jaberi 		 * All other cases: forward of MSDUs from the MBSS to DS indiv.
12233c314f6dSMonthadar Al Jaberi 		 * addressed according to 13.11.3.2.
12243c314f6dSMonthadar Al Jaberi 		 */
12253c314f6dSMonthadar Al Jaberi 	}
12263c314f6dSMonthadar Al Jaberi 	return (0); /* process locally */
12273c314f6dSMonthadar Al Jaberi }
12283c314f6dSMonthadar Al Jaberi 
12293c314f6dSMonthadar Al Jaberi /*
12303c314f6dSMonthadar Al Jaberi  * Try to forward the group addressed data on to other mesh STAs, and
12313c314f6dSMonthadar Al Jaberi  * also to the DS.
12323c314f6dSMonthadar Al Jaberi  *
12333c314f6dSMonthadar Al Jaberi  * > 0 means we have forwarded data and no need to process locally
12343c314f6dSMonthadar Al Jaberi  * == 0 means we want to process locally (and we may have forwarded data
12353c314f6dSMonthadar Al Jaberi  * < 0 means there was an error and data should be discarded
12363c314f6dSMonthadar Al Jaberi  */
12373c314f6dSMonthadar Al Jaberi static int
12383c314f6dSMonthadar Al Jaberi mesh_recv_group_data(struct ieee80211vap *vap, struct mbuf *m,
12393c314f6dSMonthadar Al Jaberi     struct ieee80211_frame *wh, const struct ieee80211_meshcntl *mc)
12403c314f6dSMonthadar Al Jaberi {
12413c314f6dSMonthadar Al Jaberi #define	MC01(mc)	((const struct ieee80211_meshcntl_ae01 *)mc)
12423c314f6dSMonthadar Al Jaberi 	struct ieee80211_mesh_state *ms = vap->iv_mesh;
12433c314f6dSMonthadar Al Jaberi 
12443c314f6dSMonthadar Al Jaberi 	mesh_forward(vap, m, mc);
12453c314f6dSMonthadar Al Jaberi 
12463c314f6dSMonthadar Al Jaberi 	if(mc->mc_ttl > 0) {
12473c314f6dSMonthadar Al Jaberi 		if (mc->mc_flags & IEEE80211_MESH_AE_01) {
12483c314f6dSMonthadar Al Jaberi 			/*
12493c314f6dSMonthadar Al Jaberi 			 * Forward of MSDUs from the MBSS to DS group addressed
12503c314f6dSMonthadar Al Jaberi 			 * (according to 13.11.3.2)
12513c314f6dSMonthadar Al Jaberi 			 * This happens by delivering the packet, and a bridge
12523c314f6dSMonthadar Al Jaberi 			 * will sent it on another port member.
12533c314f6dSMonthadar Al Jaberi 			 */
12543c314f6dSMonthadar Al Jaberi 			if (ms->ms_flags & IEEE80211_MESHFLAGS_PORTAL &&
12553c314f6dSMonthadar Al Jaberi 			    ms->ms_flags & IEEE80211_MESHFLAGS_FWD)
12563c314f6dSMonthadar Al Jaberi 				IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH,
12573c314f6dSMonthadar Al Jaberi 				    MC01(mc)->mc_addr4, "%s",
12583c314f6dSMonthadar Al Jaberi 				    "forward from MBSS to the DS");
12593c314f6dSMonthadar Al Jaberi 		}
12603c314f6dSMonthadar Al Jaberi 	}
12613c314f6dSMonthadar Al Jaberi 	return (0); /* process locally */
12623c314f6dSMonthadar Al Jaberi #undef	MC01
12633c314f6dSMonthadar Al Jaberi }
12643c314f6dSMonthadar Al Jaberi 
126559aa14a9SRui Paulo static int
126659aa14a9SRui Paulo mesh_input(struct ieee80211_node *ni, struct mbuf *m, int rssi, int nf)
126759aa14a9SRui Paulo {
126859aa14a9SRui Paulo #define	HAS_SEQ(type)	((type & 0x4) == 0)
12693c314f6dSMonthadar Al Jaberi #define	MC01(mc)	((const struct ieee80211_meshcntl_ae01 *)mc)
12703c314f6dSMonthadar Al Jaberi #define	MC10(mc)	((const struct ieee80211_meshcntl_ae10 *)mc)
127159aa14a9SRui Paulo 	struct ieee80211vap *vap = ni->ni_vap;
127259aa14a9SRui Paulo 	struct ieee80211com *ic = ni->ni_ic;
127359aa14a9SRui Paulo 	struct ifnet *ifp = vap->iv_ifp;
127459aa14a9SRui Paulo 	struct ieee80211_frame *wh;
127559aa14a9SRui Paulo 	const struct ieee80211_meshcntl *mc;
12763c314f6dSMonthadar Al Jaberi 	int hdrspace, meshdrlen, need_tap, error;
12773c314f6dSMonthadar Al Jaberi 	uint8_t dir, type, subtype, ae;
127859aa14a9SRui Paulo 	uint32_t seq;
12793c314f6dSMonthadar Al Jaberi 	const uint8_t *addr;
12803c314f6dSMonthadar Al Jaberi 	uint8_t qos[2];
128159aa14a9SRui Paulo 	ieee80211_seq rxseq;
128259aa14a9SRui Paulo 
128359aa14a9SRui Paulo 	KASSERT(ni != NULL, ("null node"));
128459aa14a9SRui Paulo 	ni->ni_inact = ni->ni_inact_reload;
128559aa14a9SRui Paulo 
128659aa14a9SRui Paulo 	need_tap = 1;			/* mbuf need to be tapped. */
128759aa14a9SRui Paulo 	type = -1;			/* undefined */
128859aa14a9SRui Paulo 
128959aa14a9SRui Paulo 	if (m->m_pkthdr.len < sizeof(struct ieee80211_frame_min)) {
129059aa14a9SRui Paulo 		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
129159aa14a9SRui Paulo 		    ni->ni_macaddr, NULL,
129259aa14a9SRui Paulo 		    "too short (1): len %u", m->m_pkthdr.len);
129359aa14a9SRui Paulo 		vap->iv_stats.is_rx_tooshort++;
129459aa14a9SRui Paulo 		goto out;
129559aa14a9SRui Paulo 	}
129659aa14a9SRui Paulo 	/*
129759aa14a9SRui Paulo 	 * Bit of a cheat here, we use a pointer for a 3-address
129859aa14a9SRui Paulo 	 * frame format but don't reference fields past outside
129959aa14a9SRui Paulo 	 * ieee80211_frame_min w/o first validating the data is
130059aa14a9SRui Paulo 	 * present.
130159aa14a9SRui Paulo 	*/
130259aa14a9SRui Paulo 	wh = mtod(m, struct ieee80211_frame *);
130359aa14a9SRui Paulo 
130459aa14a9SRui Paulo 	if ((wh->i_fc[0] & IEEE80211_FC0_VERSION_MASK) !=
130559aa14a9SRui Paulo 	    IEEE80211_FC0_VERSION_0) {
130659aa14a9SRui Paulo 		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
130759aa14a9SRui Paulo 		    ni->ni_macaddr, NULL, "wrong version %x", wh->i_fc[0]);
130859aa14a9SRui Paulo 		vap->iv_stats.is_rx_badversion++;
130959aa14a9SRui Paulo 		goto err;
131059aa14a9SRui Paulo 	}
131159aa14a9SRui Paulo 	dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK;
131259aa14a9SRui Paulo 	type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
131359aa14a9SRui Paulo 	subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
131459aa14a9SRui Paulo 	if ((ic->ic_flags & IEEE80211_F_SCAN) == 0) {
131559aa14a9SRui Paulo 		IEEE80211_RSSI_LPF(ni->ni_avgrssi, rssi);
131659aa14a9SRui Paulo 		ni->ni_noise = nf;
131759aa14a9SRui Paulo 		if (HAS_SEQ(type)) {
131859aa14a9SRui Paulo 			uint8_t tid = ieee80211_gettid(wh);
131959aa14a9SRui Paulo 
132059aa14a9SRui Paulo 			if (IEEE80211_QOS_HAS_SEQ(wh) &&
132159aa14a9SRui Paulo 			    TID_TO_WME_AC(tid) >= WME_AC_VI)
132259aa14a9SRui Paulo 				ic->ic_wme.wme_hipri_traffic++;
132359aa14a9SRui Paulo 			rxseq = le16toh(*(uint16_t *)wh->i_seq);
1324cd0b8f2dSAdrian Chadd 			if (! ieee80211_check_rxseq(ni, wh)) {
132559aa14a9SRui Paulo 				/* duplicate, discard */
132659aa14a9SRui Paulo 				IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
132759aa14a9SRui Paulo 				    wh->i_addr1, "duplicate",
132859aa14a9SRui Paulo 				    "seqno <%u,%u> fragno <%u,%u> tid %u",
132959aa14a9SRui Paulo 				    rxseq >> IEEE80211_SEQ_SEQ_SHIFT,
133059aa14a9SRui Paulo 				    ni->ni_rxseqs[tid] >>
133159aa14a9SRui Paulo 				    IEEE80211_SEQ_SEQ_SHIFT,
133259aa14a9SRui Paulo 				    rxseq & IEEE80211_SEQ_FRAG_MASK,
133359aa14a9SRui Paulo 				    ni->ni_rxseqs[tid] &
133459aa14a9SRui Paulo 				    IEEE80211_SEQ_FRAG_MASK,
133559aa14a9SRui Paulo 				    tid);
133659aa14a9SRui Paulo 				vap->iv_stats.is_rx_dup++;
133759aa14a9SRui Paulo 				IEEE80211_NODE_STAT(ni, rx_dup);
133859aa14a9SRui Paulo 				goto out;
133959aa14a9SRui Paulo 			}
134059aa14a9SRui Paulo 			ni->ni_rxseqs[tid] = rxseq;
134159aa14a9SRui Paulo 		}
134259aa14a9SRui Paulo 	}
134359aa14a9SRui Paulo #ifdef IEEE80211_DEBUG
134459aa14a9SRui Paulo 	/*
134559aa14a9SRui Paulo 	 * It's easier, but too expensive, to simulate different mesh
134659aa14a9SRui Paulo 	 * topologies by consulting the ACL policy very early, so do this
134759aa14a9SRui Paulo 	 * only under DEBUG.
134859aa14a9SRui Paulo 	 *
134959aa14a9SRui Paulo 	 * NB: this check is also done upon peering link initiation.
135059aa14a9SRui Paulo 	 */
13515a8801b0SBernhard Schmidt 	if (vap->iv_acl != NULL && !vap->iv_acl->iac_check(vap, wh)) {
135259aa14a9SRui Paulo 		IEEE80211_DISCARD(vap, IEEE80211_MSG_ACL,
135359aa14a9SRui Paulo 		    wh, NULL, "%s", "disallowed by ACL");
135459aa14a9SRui Paulo 		vap->iv_stats.is_rx_acl++;
135559aa14a9SRui Paulo 		goto out;
135659aa14a9SRui Paulo 	}
135759aa14a9SRui Paulo #endif
135859aa14a9SRui Paulo 	switch (type) {
135959aa14a9SRui Paulo 	case IEEE80211_FC0_TYPE_DATA:
136059aa14a9SRui Paulo 		if (ni == vap->iv_bss)
136159aa14a9SRui Paulo 			goto out;
136259aa14a9SRui Paulo 		if (ni->ni_mlstate != IEEE80211_NODE_MESH_ESTABLISHED) {
136359aa14a9SRui Paulo 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_MESH,
136459aa14a9SRui Paulo 			    ni->ni_macaddr, NULL,
136559aa14a9SRui Paulo 			    "peer link not yet established (%d)",
136659aa14a9SRui Paulo 			    ni->ni_mlstate);
136759aa14a9SRui Paulo 			vap->iv_stats.is_mesh_nolink++;
136859aa14a9SRui Paulo 			goto out;
136959aa14a9SRui Paulo 		}
137059aa14a9SRui Paulo 		if (dir != IEEE80211_FC1_DIR_FROMDS &&
137159aa14a9SRui Paulo 		    dir != IEEE80211_FC1_DIR_DSTODS) {
137259aa14a9SRui Paulo 			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
137359aa14a9SRui Paulo 			    wh, "data", "incorrect dir 0x%x", dir);
137459aa14a9SRui Paulo 			vap->iv_stats.is_rx_wrongdir++;
137559aa14a9SRui Paulo 			goto err;
137659aa14a9SRui Paulo 		}
137791216c71SAdrian Chadd 
137891216c71SAdrian Chadd 		/* All Mesh data frames are QoS subtype */
137991216c71SAdrian Chadd 		if (!HAS_SEQ(type)) {
138091216c71SAdrian Chadd 			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
138191216c71SAdrian Chadd 			    wh, "data", "incorrect subtype 0x%x", subtype);
138291216c71SAdrian Chadd 			vap->iv_stats.is_rx_badsubtype++;
138391216c71SAdrian Chadd 			goto err;
138491216c71SAdrian Chadd 		}
138591216c71SAdrian Chadd 
138691216c71SAdrian Chadd 		/*
138791216c71SAdrian Chadd 		 * Next up, any fragmentation.
138891216c71SAdrian Chadd 		 * XXX: we defrag before we even try to forward,
138991216c71SAdrian Chadd 		 * Mesh Control field is not present in sub-sequent
139091216c71SAdrian Chadd 		 * fragmented frames. This is in contrast to Draft 4.0.
139191216c71SAdrian Chadd 		 */
139259aa14a9SRui Paulo 		hdrspace = ieee80211_hdrspace(ic, wh);
139391216c71SAdrian Chadd 		if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
139491216c71SAdrian Chadd 			m = ieee80211_defrag(ni, m, hdrspace);
139591216c71SAdrian Chadd 			if (m == NULL) {
139691216c71SAdrian Chadd 				/* Fragment dropped or frame not complete yet */
139791216c71SAdrian Chadd 				goto out;
139891216c71SAdrian Chadd 			}
139991216c71SAdrian Chadd 		}
140091216c71SAdrian Chadd 		wh = mtod(m, struct ieee80211_frame *); /* NB: after defrag */
140191216c71SAdrian Chadd 
140291216c71SAdrian Chadd 		/*
140391216c71SAdrian Chadd 		 * Now we have a complete Mesh Data frame.
140491216c71SAdrian Chadd 		 */
140591216c71SAdrian Chadd 
140691216c71SAdrian Chadd 		/*
140791216c71SAdrian Chadd 		 * Only fromDStoDS data frames use 4 address qos frames
140891216c71SAdrian Chadd 		 * as specified in amendment. Otherwise addr4 is located
140991216c71SAdrian Chadd 		 * in the Mesh Control field and a 3 address qos frame
141091216c71SAdrian Chadd 		 * is used.
141191216c71SAdrian Chadd 		 */
141291216c71SAdrian Chadd 		if (IEEE80211_IS_DSTODS(wh))
141391216c71SAdrian Chadd 			*(uint16_t *)qos = *(uint16_t *)
141491216c71SAdrian Chadd 			    ((struct ieee80211_qosframe_addr4 *)wh)->i_qos;
141591216c71SAdrian Chadd 		else
141691216c71SAdrian Chadd 			*(uint16_t *)qos = *(uint16_t *)
141791216c71SAdrian Chadd 			    ((struct ieee80211_qosframe *)wh)->i_qos;
141891216c71SAdrian Chadd 
141991216c71SAdrian Chadd 		/*
142091216c71SAdrian Chadd 		 * NB: The mesh STA sets the Mesh Control Present
142191216c71SAdrian Chadd 		 * subfield to 1 in the Mesh Data frame containing
142291216c71SAdrian Chadd 		 * an unfragmented MSDU, an A-MSDU, or the first
142391216c71SAdrian Chadd 		 * fragment of an MSDU.
142491216c71SAdrian Chadd 		 * After defrag it should always be present.
142591216c71SAdrian Chadd 		 */
142691216c71SAdrian Chadd 		if (!(qos[1] & IEEE80211_QOS_MC)) {
142791216c71SAdrian Chadd 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_MESH,
142891216c71SAdrian Chadd 			    ni->ni_macaddr, NULL,
142991216c71SAdrian Chadd 			    "%s", "Mesh control field not present");
143091216c71SAdrian Chadd 			vap->iv_stats.is_rx_elem_missing++; /* XXX: kinda */
143191216c71SAdrian Chadd 			goto err;
143291216c71SAdrian Chadd 		}
143391216c71SAdrian Chadd 
143491216c71SAdrian Chadd 		/* pull up enough to get to the mesh control */
143559aa14a9SRui Paulo 		if (m->m_len < hdrspace + sizeof(struct ieee80211_meshcntl) &&
143659aa14a9SRui Paulo 		    (m = m_pullup(m, hdrspace +
143759aa14a9SRui Paulo 		        sizeof(struct ieee80211_meshcntl))) == NULL) {
143859aa14a9SRui Paulo 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
143959aa14a9SRui Paulo 			    ni->ni_macaddr, NULL,
144059aa14a9SRui Paulo 			    "data too short: expecting %u", hdrspace);
144159aa14a9SRui Paulo 			vap->iv_stats.is_rx_tooshort++;
144259aa14a9SRui Paulo 			goto out;		/* XXX */
144359aa14a9SRui Paulo 		}
144459aa14a9SRui Paulo 		/*
144559aa14a9SRui Paulo 		 * Now calculate the full extent of the headers. Note
1446c104cff2SRui Paulo 		 * mesh_decap will pull up anything we didn't get
144759aa14a9SRui Paulo 		 * above when it strips the 802.11 headers.
144859aa14a9SRui Paulo 		 */
144959aa14a9SRui Paulo 		mc = (const struct ieee80211_meshcntl *)
145059aa14a9SRui Paulo 		    (mtod(m, const uint8_t *) + hdrspace);
14513c314f6dSMonthadar Al Jaberi 		ae = mc->mc_flags & IEEE80211_MESH_AE_MASK;
1452c104cff2SRui Paulo 		meshdrlen = sizeof(struct ieee80211_meshcntl) +
14533c314f6dSMonthadar Al Jaberi 		    ae * IEEE80211_ADDR_LEN;
1454c104cff2SRui Paulo 		hdrspace += meshdrlen;
14553c314f6dSMonthadar Al Jaberi 
14563c314f6dSMonthadar Al Jaberi 		/* pull complete hdrspace = ieee80211_hdrspace + meshcontrol */
14573c314f6dSMonthadar Al Jaberi 		if ((meshdrlen > sizeof(struct ieee80211_meshcntl)) &&
14583c314f6dSMonthadar Al Jaberi 		    (m->m_len < hdrspace) &&
14593c314f6dSMonthadar Al Jaberi 		    ((m = m_pullup(m, hdrspace)) == NULL)) {
14603c314f6dSMonthadar Al Jaberi 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
14613c314f6dSMonthadar Al Jaberi 			    ni->ni_macaddr, NULL,
14623c314f6dSMonthadar Al Jaberi 			    "data too short: expecting %u", hdrspace);
14633c314f6dSMonthadar Al Jaberi 			vap->iv_stats.is_rx_tooshort++;
14643c314f6dSMonthadar Al Jaberi 			goto out;		/* XXX */
14653c314f6dSMonthadar Al Jaberi 		}
14663c314f6dSMonthadar Al Jaberi 		/* XXX: are we sure there is no reallocating after m_pullup? */
14673c314f6dSMonthadar Al Jaberi 
146859aa14a9SRui Paulo 		seq = LE_READ_4(mc->mc_seq);
146959aa14a9SRui Paulo 		if (IEEE80211_IS_MULTICAST(wh->i_addr1))
147059aa14a9SRui Paulo 			addr = wh->i_addr3;
14713c314f6dSMonthadar Al Jaberi 		else if (ae == IEEE80211_MESH_AE_01)
14723c314f6dSMonthadar Al Jaberi 			addr = MC01(mc)->mc_addr4;
147359aa14a9SRui Paulo 		else
147459aa14a9SRui Paulo 			addr = ((struct ieee80211_qosframe_addr4 *)wh)->i_addr4;
147559aa14a9SRui Paulo 		if (IEEE80211_ADDR_EQ(vap->iv_myaddr, addr)) {
147659aa14a9SRui Paulo 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
147759aa14a9SRui Paulo 			    addr, "data", "%s", "not to me");
147859aa14a9SRui Paulo 			vap->iv_stats.is_rx_wrongbss++;	/* XXX kinda */
147959aa14a9SRui Paulo 			goto out;
148059aa14a9SRui Paulo 		}
148159aa14a9SRui Paulo 		if (mesh_checkpseq(vap, addr, seq) != 0) {
148259aa14a9SRui Paulo 			vap->iv_stats.is_rx_dup++;
148359aa14a9SRui Paulo 			goto out;
148459aa14a9SRui Paulo 		}
148559aa14a9SRui Paulo 
14863c314f6dSMonthadar Al Jaberi 		/* This code "routes" the frame to the right control path */
14873c314f6dSMonthadar Al Jaberi 		if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
14883c314f6dSMonthadar Al Jaberi 			if (IEEE80211_ADDR_EQ(vap->iv_myaddr, wh->i_addr3))
14893c314f6dSMonthadar Al Jaberi 				error =
14903c314f6dSMonthadar Al Jaberi 				    mesh_recv_indiv_data_to_me(vap, m, wh, mc);
14913c314f6dSMonthadar Al Jaberi 			else if (IEEE80211_IS_MULTICAST(wh->i_addr3))
14923c314f6dSMonthadar Al Jaberi 				error = mesh_recv_group_data(vap, m, wh, mc);
14933c314f6dSMonthadar Al Jaberi 			else
14943c314f6dSMonthadar Al Jaberi 				error = mesh_recv_indiv_data_to_fwrd(vap, m,
14953c314f6dSMonthadar Al Jaberi 				    wh, mc);
14963c314f6dSMonthadar Al Jaberi 		} else
14973c314f6dSMonthadar Al Jaberi 			error = mesh_recv_group_data(vap, m, wh, mc);
14983c314f6dSMonthadar Al Jaberi 		if (error < 0)
14993c314f6dSMonthadar Al Jaberi 			goto err;
15003c314f6dSMonthadar Al Jaberi 		else if (error > 0)
150159aa14a9SRui Paulo 			goto out;
150259aa14a9SRui Paulo 
150359aa14a9SRui Paulo 		if (ieee80211_radiotap_active_vap(vap))
150459aa14a9SRui Paulo 			ieee80211_radiotap_rx(vap, m);
150559aa14a9SRui Paulo 		need_tap = 0;
150659aa14a9SRui Paulo 
150759aa14a9SRui Paulo 		/*
150859aa14a9SRui Paulo 		 * Finally, strip the 802.11 header.
150959aa14a9SRui Paulo 		 */
1510c104cff2SRui Paulo 		m = mesh_decap(vap, m, hdrspace, meshdrlen);
151159aa14a9SRui Paulo 		if (m == NULL) {
151259aa14a9SRui Paulo 			/* XXX mask bit to check for both */
151359aa14a9SRui Paulo 			/* don't count Null data frames as errors */
151459aa14a9SRui Paulo 			if (subtype == IEEE80211_FC0_SUBTYPE_NODATA ||
151559aa14a9SRui Paulo 			    subtype == IEEE80211_FC0_SUBTYPE_QOS_NULL)
151659aa14a9SRui Paulo 				goto out;
151759aa14a9SRui Paulo 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
151859aa14a9SRui Paulo 			    ni->ni_macaddr, "data", "%s", "decap error");
151959aa14a9SRui Paulo 			vap->iv_stats.is_rx_decap++;
152059aa14a9SRui Paulo 			IEEE80211_NODE_STAT(ni, rx_decap);
152159aa14a9SRui Paulo 			goto err;
152259aa14a9SRui Paulo 		}
152391216c71SAdrian Chadd 		if (qos[0] & IEEE80211_QOS_AMSDU) {
152459aa14a9SRui Paulo 			m = ieee80211_decap_amsdu(ni, m);
152559aa14a9SRui Paulo 			if (m == NULL)
152659aa14a9SRui Paulo 				return IEEE80211_FC0_TYPE_DATA;
152759aa14a9SRui Paulo 		}
152859aa14a9SRui Paulo 		ieee80211_deliver_data(vap, ni, m);
152959aa14a9SRui Paulo 		return type;
153059aa14a9SRui Paulo 	case IEEE80211_FC0_TYPE_MGT:
153159aa14a9SRui Paulo 		vap->iv_stats.is_rx_mgmt++;
153259aa14a9SRui Paulo 		IEEE80211_NODE_STAT(ni, rx_mgmt);
153359aa14a9SRui Paulo 		if (dir != IEEE80211_FC1_DIR_NODS) {
153459aa14a9SRui Paulo 			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
153559aa14a9SRui Paulo 			    wh, "mgt", "incorrect dir 0x%x", dir);
153659aa14a9SRui Paulo 			vap->iv_stats.is_rx_wrongdir++;
153759aa14a9SRui Paulo 			goto err;
153859aa14a9SRui Paulo 		}
153959aa14a9SRui Paulo 		if (m->m_pkthdr.len < sizeof(struct ieee80211_frame)) {
154059aa14a9SRui Paulo 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
154159aa14a9SRui Paulo 			    ni->ni_macaddr, "mgt", "too short: len %u",
154259aa14a9SRui Paulo 			    m->m_pkthdr.len);
154359aa14a9SRui Paulo 			vap->iv_stats.is_rx_tooshort++;
154459aa14a9SRui Paulo 			goto out;
154559aa14a9SRui Paulo 		}
154659aa14a9SRui Paulo #ifdef IEEE80211_DEBUG
154759aa14a9SRui Paulo 		if ((ieee80211_msg_debug(vap) &&
154859aa14a9SRui Paulo 		    (vap->iv_ic->ic_flags & IEEE80211_F_SCAN)) ||
154959aa14a9SRui Paulo 		    ieee80211_msg_dumppkts(vap)) {
155059aa14a9SRui Paulo 			if_printf(ifp, "received %s from %s rssi %d\n",
155159aa14a9SRui Paulo 			    ieee80211_mgt_subtype_name[subtype >>
155259aa14a9SRui Paulo 			    IEEE80211_FC0_SUBTYPE_SHIFT],
155359aa14a9SRui Paulo 			    ether_sprintf(wh->i_addr2), rssi);
155459aa14a9SRui Paulo 		}
155559aa14a9SRui Paulo #endif
155659aa14a9SRui Paulo 		if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
155759aa14a9SRui Paulo 			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
155859aa14a9SRui Paulo 			    wh, NULL, "%s", "WEP set but not permitted");
155959aa14a9SRui Paulo 			vap->iv_stats.is_rx_mgtdiscard++; /* XXX */
156059aa14a9SRui Paulo 			goto out;
156159aa14a9SRui Paulo 		}
156259aa14a9SRui Paulo 		vap->iv_recv_mgmt(ni, m, subtype, rssi, nf);
156359aa14a9SRui Paulo 		goto out;
156459aa14a9SRui Paulo 	case IEEE80211_FC0_TYPE_CTL:
156559aa14a9SRui Paulo 		vap->iv_stats.is_rx_ctl++;
156659aa14a9SRui Paulo 		IEEE80211_NODE_STAT(ni, rx_ctrl);
156759aa14a9SRui Paulo 		goto out;
156859aa14a9SRui Paulo 	default:
156959aa14a9SRui Paulo 		IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
157059aa14a9SRui Paulo 		    wh, "bad", "frame type 0x%x", type);
157159aa14a9SRui Paulo 		/* should not come here */
157259aa14a9SRui Paulo 		break;
157359aa14a9SRui Paulo 	}
157459aa14a9SRui Paulo err:
157559aa14a9SRui Paulo 	ifp->if_ierrors++;
157659aa14a9SRui Paulo out:
157759aa14a9SRui Paulo 	if (m != NULL) {
157859aa14a9SRui Paulo 		if (need_tap && ieee80211_radiotap_active_vap(vap))
157959aa14a9SRui Paulo 			ieee80211_radiotap_rx(vap, m);
158059aa14a9SRui Paulo 		m_freem(m);
158159aa14a9SRui Paulo 	}
158259aa14a9SRui Paulo 	return type;
15833c314f6dSMonthadar Al Jaberi #undef	HAS_SEQ
15843c314f6dSMonthadar Al Jaberi #undef	MC01
15853c314f6dSMonthadar Al Jaberi #undef	MC10
158659aa14a9SRui Paulo }
158759aa14a9SRui Paulo 
158859aa14a9SRui Paulo static void
158959aa14a9SRui Paulo mesh_recv_mgmt(struct ieee80211_node *ni, struct mbuf *m0, int subtype,
159059aa14a9SRui Paulo     int rssi, int nf)
159159aa14a9SRui Paulo {
159259aa14a9SRui Paulo 	struct ieee80211vap *vap = ni->ni_vap;
159359aa14a9SRui Paulo 	struct ieee80211_mesh_state *ms = vap->iv_mesh;
159459aa14a9SRui Paulo 	struct ieee80211com *ic = ni->ni_ic;
159559aa14a9SRui Paulo 	struct ieee80211_frame *wh;
1596b5df85a6SMonthadar Al Jaberi 	struct ieee80211_mesh_route *rt;
159759aa14a9SRui Paulo 	uint8_t *frm, *efrm;
159859aa14a9SRui Paulo 
159959aa14a9SRui Paulo 	wh = mtod(m0, struct ieee80211_frame *);
160059aa14a9SRui Paulo 	frm = (uint8_t *)&wh[1];
160159aa14a9SRui Paulo 	efrm = mtod(m0, uint8_t *) + m0->m_len;
160259aa14a9SRui Paulo 	switch (subtype) {
160359aa14a9SRui Paulo 	case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
160459aa14a9SRui Paulo 	case IEEE80211_FC0_SUBTYPE_BEACON:
160559aa14a9SRui Paulo 	{
160659aa14a9SRui Paulo 		struct ieee80211_scanparams scan;
160759aa14a9SRui Paulo 		/*
160859aa14a9SRui Paulo 		 * We process beacon/probe response
160959aa14a9SRui Paulo 		 * frames to discover neighbors.
161059aa14a9SRui Paulo 		 */
161159aa14a9SRui Paulo 		if (ieee80211_parse_beacon(ni, m0, &scan) != 0)
161259aa14a9SRui Paulo 			return;
161359aa14a9SRui Paulo 		/*
161459aa14a9SRui Paulo 		 * Count frame now that we know it's to be processed.
161559aa14a9SRui Paulo 		 */
161659aa14a9SRui Paulo 		if (subtype == IEEE80211_FC0_SUBTYPE_BEACON) {
161759aa14a9SRui Paulo 			vap->iv_stats.is_rx_beacon++;	/* XXX remove */
161859aa14a9SRui Paulo 			IEEE80211_NODE_STAT(ni, rx_beacons);
161959aa14a9SRui Paulo 		} else
162059aa14a9SRui Paulo 			IEEE80211_NODE_STAT(ni, rx_proberesp);
162159aa14a9SRui Paulo 		/*
162259aa14a9SRui Paulo 		 * If scanning, just pass information to the scan module.
162359aa14a9SRui Paulo 		 */
162459aa14a9SRui Paulo 		if (ic->ic_flags & IEEE80211_F_SCAN) {
162559aa14a9SRui Paulo 			if (ic->ic_flags_ext & IEEE80211_FEXT_PROBECHAN) {
162659aa14a9SRui Paulo 				/*
162759aa14a9SRui Paulo 				 * Actively scanning a channel marked passive;
162859aa14a9SRui Paulo 				 * send a probe request now that we know there
162959aa14a9SRui Paulo 				 * is 802.11 traffic present.
163059aa14a9SRui Paulo 				 *
163159aa14a9SRui Paulo 				 * XXX check if the beacon we recv'd gives
163259aa14a9SRui Paulo 				 * us what we need and suppress the probe req
163359aa14a9SRui Paulo 				 */
163459aa14a9SRui Paulo 				ieee80211_probe_curchan(vap, 1);
163559aa14a9SRui Paulo 				ic->ic_flags_ext &= ~IEEE80211_FEXT_PROBECHAN;
163659aa14a9SRui Paulo 			}
163759aa14a9SRui Paulo 			ieee80211_add_scan(vap, &scan, wh,
163859aa14a9SRui Paulo 			    subtype, rssi, nf);
163959aa14a9SRui Paulo 			return;
164059aa14a9SRui Paulo 		}
164159aa14a9SRui Paulo 
164259aa14a9SRui Paulo 		/* The rest of this code assumes we are running */
164359aa14a9SRui Paulo 		if (vap->iv_state != IEEE80211_S_RUN)
164459aa14a9SRui Paulo 			return;
164559aa14a9SRui Paulo 		/*
164659aa14a9SRui Paulo 		 * Ignore non-mesh STAs.
164759aa14a9SRui Paulo 		 */
164859aa14a9SRui Paulo 		if ((scan.capinfo &
164959aa14a9SRui Paulo 		     (IEEE80211_CAPINFO_ESS|IEEE80211_CAPINFO_IBSS)) ||
165059aa14a9SRui Paulo 		    scan.meshid == NULL || scan.meshconf == NULL) {
165159aa14a9SRui Paulo 			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
165259aa14a9SRui Paulo 			    wh, "beacon", "%s", "not a mesh sta");
165359aa14a9SRui Paulo 			vap->iv_stats.is_mesh_wrongmesh++;
165459aa14a9SRui Paulo 			return;
165559aa14a9SRui Paulo 		}
165659aa14a9SRui Paulo 		/*
165759aa14a9SRui Paulo 		 * Ignore STAs for other mesh networks.
165859aa14a9SRui Paulo 		 */
165959aa14a9SRui Paulo 		if (memcmp(scan.meshid+2, ms->ms_id, ms->ms_idlen) != 0 ||
166059aa14a9SRui Paulo 		    mesh_verify_meshconf(vap, scan.meshconf)) {
166159aa14a9SRui Paulo 			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
166259aa14a9SRui Paulo 			    wh, "beacon", "%s", "not for our mesh");
166359aa14a9SRui Paulo 			vap->iv_stats.is_mesh_wrongmesh++;
166459aa14a9SRui Paulo 			return;
166559aa14a9SRui Paulo 		}
166659aa14a9SRui Paulo 		/*
166759aa14a9SRui Paulo 		 * Peer only based on the current ACL policy.
166859aa14a9SRui Paulo 		 */
16695a8801b0SBernhard Schmidt 		if (vap->iv_acl != NULL && !vap->iv_acl->iac_check(vap, wh)) {
167059aa14a9SRui Paulo 			IEEE80211_DISCARD(vap, IEEE80211_MSG_ACL,
167159aa14a9SRui Paulo 			    wh, NULL, "%s", "disallowed by ACL");
167259aa14a9SRui Paulo 			vap->iv_stats.is_rx_acl++;
167359aa14a9SRui Paulo 			return;
167459aa14a9SRui Paulo 		}
167559aa14a9SRui Paulo 		/*
167659aa14a9SRui Paulo 		 * Do neighbor discovery.
167759aa14a9SRui Paulo 		 */
167859aa14a9SRui Paulo 		if (!IEEE80211_ADDR_EQ(wh->i_addr2, ni->ni_macaddr)) {
167959aa14a9SRui Paulo 			/*
168059aa14a9SRui Paulo 			 * Create a new entry in the neighbor table.
168159aa14a9SRui Paulo 			 */
168259aa14a9SRui Paulo 			ni = ieee80211_add_neighbor(vap, wh, &scan);
168359aa14a9SRui Paulo 		}
168459aa14a9SRui Paulo 		/*
168559aa14a9SRui Paulo 		 * Automatically peer with discovered nodes if possible.
168659aa14a9SRui Paulo 		 * XXX backoff on repeated failure
168759aa14a9SRui Paulo 		 */
168859aa14a9SRui Paulo 		if (ni != vap->iv_bss &&
1689b5df85a6SMonthadar Al Jaberi 		    (ms->ms_flags & IEEE80211_MESHFLAGS_AP)) {
1690b5df85a6SMonthadar Al Jaberi 			switch (ni->ni_mlstate) {
1691b5df85a6SMonthadar Al Jaberi 			case IEEE80211_NODE_MESH_IDLE:
1692b5df85a6SMonthadar Al Jaberi 			{
169359aa14a9SRui Paulo 				uint16_t args[1];
169459aa14a9SRui Paulo 
169559aa14a9SRui Paulo 				ni->ni_mlpid = mesh_generateid(vap);
169659aa14a9SRui Paulo 				if (ni->ni_mlpid == 0)
169759aa14a9SRui Paulo 					return;
169859aa14a9SRui Paulo 				mesh_linkchange(ni, IEEE80211_NODE_MESH_OPENSNT);
169959aa14a9SRui Paulo 				args[0] = ni->ni_mlpid;
170059aa14a9SRui Paulo 				ieee80211_send_action(ni,
1701ebeaa1adSMonthadar Al Jaberi 				IEEE80211_ACTION_CAT_SELF_PROT,
170259aa14a9SRui Paulo 				IEEE80211_ACTION_MESHPEERING_OPEN, args);
170359aa14a9SRui Paulo 				ni->ni_mlrcnt = 0;
170459aa14a9SRui Paulo 				mesh_peer_timeout_setup(ni);
1705b5df85a6SMonthadar Al Jaberi 				break;
1706b5df85a6SMonthadar Al Jaberi 			}
1707b5df85a6SMonthadar Al Jaberi 			case IEEE80211_NODE_MESH_ESTABLISHED:
1708b5df85a6SMonthadar Al Jaberi 			{
1709b5df85a6SMonthadar Al Jaberi 				/*
1710b5df85a6SMonthadar Al Jaberi 				 * Valid beacon from a peer mesh STA
1711b5df85a6SMonthadar Al Jaberi 				 * bump TA lifetime
1712b5df85a6SMonthadar Al Jaberi 				 */
1713b5df85a6SMonthadar Al Jaberi 				rt = ieee80211_mesh_rt_find(vap, wh->i_addr2);
1714b5df85a6SMonthadar Al Jaberi 				if(rt != NULL) {
1715b5df85a6SMonthadar Al Jaberi 					ieee80211_mesh_rt_update(rt,
17164f3a27aeSMonthadar Al Jaberi 					    ticks_to_msecs(
17174f3a27aeSMonthadar Al Jaberi 					    ms->ms_ppath->mpp_inact));
1718b5df85a6SMonthadar Al Jaberi 				}
1719b5df85a6SMonthadar Al Jaberi 				break;
1720b5df85a6SMonthadar Al Jaberi 			}
1721b5df85a6SMonthadar Al Jaberi 			default:
1722b5df85a6SMonthadar Al Jaberi 				break; /* ignore */
1723b5df85a6SMonthadar Al Jaberi 			}
172459aa14a9SRui Paulo 		}
172559aa14a9SRui Paulo 		break;
172659aa14a9SRui Paulo 	}
172759aa14a9SRui Paulo 	case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
172859aa14a9SRui Paulo 	{
172959aa14a9SRui Paulo 		uint8_t *ssid, *meshid, *rates, *xrates;
173059aa14a9SRui Paulo 		uint8_t *sfrm;
173159aa14a9SRui Paulo 
173259aa14a9SRui Paulo 		if (vap->iv_state != IEEE80211_S_RUN) {
173359aa14a9SRui Paulo 			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
173459aa14a9SRui Paulo 			    wh, NULL, "wrong state %s",
173559aa14a9SRui Paulo 			    ieee80211_state_name[vap->iv_state]);
173659aa14a9SRui Paulo 			vap->iv_stats.is_rx_mgtdiscard++;
173759aa14a9SRui Paulo 			return;
173859aa14a9SRui Paulo 		}
173959aa14a9SRui Paulo 		if (IEEE80211_IS_MULTICAST(wh->i_addr2)) {
174059aa14a9SRui Paulo 			/* frame must be directed */
174159aa14a9SRui Paulo 			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
174259aa14a9SRui Paulo 			    wh, NULL, "%s", "not unicast");
174359aa14a9SRui Paulo 			vap->iv_stats.is_rx_mgtdiscard++;	/* XXX stat */
174459aa14a9SRui Paulo 			return;
174559aa14a9SRui Paulo 		}
174659aa14a9SRui Paulo 		/*
174759aa14a9SRui Paulo 		 * prreq frame format
174859aa14a9SRui Paulo 		 *      [tlv] ssid
174959aa14a9SRui Paulo 		 *      [tlv] supported rates
175059aa14a9SRui Paulo 		 *      [tlv] extended supported rates
175159aa14a9SRui Paulo 		 *	[tlv] mesh id
175259aa14a9SRui Paulo 		 */
175359aa14a9SRui Paulo 		ssid = meshid = rates = xrates = NULL;
175459aa14a9SRui Paulo 		sfrm = frm;
175559aa14a9SRui Paulo 		while (efrm - frm > 1) {
175659aa14a9SRui Paulo 			IEEE80211_VERIFY_LENGTH(efrm - frm, frm[1] + 2, return);
175759aa14a9SRui Paulo 			switch (*frm) {
175859aa14a9SRui Paulo 			case IEEE80211_ELEMID_SSID:
175959aa14a9SRui Paulo 				ssid = frm;
176059aa14a9SRui Paulo 				break;
176159aa14a9SRui Paulo 			case IEEE80211_ELEMID_RATES:
176259aa14a9SRui Paulo 				rates = frm;
176359aa14a9SRui Paulo 				break;
176459aa14a9SRui Paulo 			case IEEE80211_ELEMID_XRATES:
176559aa14a9SRui Paulo 				xrates = frm;
176659aa14a9SRui Paulo 				break;
176759aa14a9SRui Paulo 			case IEEE80211_ELEMID_MESHID:
176859aa14a9SRui Paulo 				meshid = frm;
176959aa14a9SRui Paulo 				break;
177059aa14a9SRui Paulo 			}
1771d3befdecSBernhard Schmidt 			frm += frm[1] + 2;
177259aa14a9SRui Paulo 		}
177359aa14a9SRui Paulo 		IEEE80211_VERIFY_ELEMENT(ssid, IEEE80211_NWID_LEN, return);
177459aa14a9SRui Paulo 		IEEE80211_VERIFY_ELEMENT(rates, IEEE80211_RATE_MAXSIZE, return);
177559aa14a9SRui Paulo 		if (xrates != NULL)
177659aa14a9SRui Paulo 			IEEE80211_VERIFY_ELEMENT(xrates,
177759aa14a9SRui Paulo 			    IEEE80211_RATE_MAXSIZE - rates[1], return);
17786c946257SRui Paulo 		if (meshid != NULL) {
177959aa14a9SRui Paulo 			IEEE80211_VERIFY_ELEMENT(meshid,
178059aa14a9SRui Paulo 			    IEEE80211_MESHID_LEN, return);
178159aa14a9SRui Paulo 			/* NB: meshid, not ssid */
178259aa14a9SRui Paulo 			IEEE80211_VERIFY_SSID(vap->iv_bss, meshid, return);
17836c946257SRui Paulo 		}
178459aa14a9SRui Paulo 
178559aa14a9SRui Paulo 		/* XXX find a better class or define it's own */
178659aa14a9SRui Paulo 		IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_INPUT, wh->i_addr2,
178759aa14a9SRui Paulo 		    "%s", "recv probe req");
178859aa14a9SRui Paulo 		/*
178959aa14a9SRui Paulo 		 * Some legacy 11b clients cannot hack a complete
179059aa14a9SRui Paulo 		 * probe response frame.  When the request includes
179159aa14a9SRui Paulo 		 * only a bare-bones rate set, communicate this to
179259aa14a9SRui Paulo 		 * the transmit side.
179359aa14a9SRui Paulo 		 */
179459aa14a9SRui Paulo 		ieee80211_send_proberesp(vap, wh->i_addr2, 0);
179559aa14a9SRui Paulo 		break;
179659aa14a9SRui Paulo 	}
179796283082SBernhard Schmidt 
179859aa14a9SRui Paulo 	case IEEE80211_FC0_SUBTYPE_ACTION:
179996283082SBernhard Schmidt 	case IEEE80211_FC0_SUBTYPE_ACTION_NOACK:
180059aa14a9SRui Paulo 		if (ni == vap->iv_bss) {
1801893c4d6eSBernhard Schmidt 			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
180259aa14a9SRui Paulo 			    wh, NULL, "%s", "unknown node");
180359aa14a9SRui Paulo 			vap->iv_stats.is_rx_mgtdiscard++;
1804893c4d6eSBernhard Schmidt 		} else if (!IEEE80211_ADDR_EQ(vap->iv_myaddr, wh->i_addr1) &&
180559aa14a9SRui Paulo 		    !IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1806893c4d6eSBernhard Schmidt 			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
1807893c4d6eSBernhard Schmidt 			    wh, NULL, "%s", "not for us");
180859aa14a9SRui Paulo 			vap->iv_stats.is_rx_mgtdiscard++;
1809893c4d6eSBernhard Schmidt 		} else if (vap->iv_state != IEEE80211_S_RUN) {
181096283082SBernhard Schmidt 			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
181196283082SBernhard Schmidt 			    wh, NULL, "wrong state %s",
181296283082SBernhard Schmidt 			    ieee80211_state_name[vap->iv_state]);
181396283082SBernhard Schmidt 			vap->iv_stats.is_rx_mgtdiscard++;
1814893c4d6eSBernhard Schmidt 		} else {
1815893c4d6eSBernhard Schmidt 			if (ieee80211_parse_action(ni, m0) == 0)
1816893c4d6eSBernhard Schmidt 				(void)ic->ic_recv_action(ni, wh, frm, efrm);
181796283082SBernhard Schmidt 		}
181859aa14a9SRui Paulo 		break;
181996283082SBernhard Schmidt 
182059aa14a9SRui Paulo 	case IEEE80211_FC0_SUBTYPE_ASSOC_REQ:
182159aa14a9SRui Paulo 	case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
182296283082SBernhard Schmidt 	case IEEE80211_FC0_SUBTYPE_REASSOC_REQ:
182359aa14a9SRui Paulo 	case IEEE80211_FC0_SUBTYPE_REASSOC_RESP:
182496283082SBernhard Schmidt 	case IEEE80211_FC0_SUBTYPE_ATIM:
182559aa14a9SRui Paulo 	case IEEE80211_FC0_SUBTYPE_DISASSOC:
182696283082SBernhard Schmidt 	case IEEE80211_FC0_SUBTYPE_AUTH:
182796283082SBernhard Schmidt 	case IEEE80211_FC0_SUBTYPE_DEAUTH:
182859aa14a9SRui Paulo 		IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
182959aa14a9SRui Paulo 		    wh, NULL, "%s", "not handled");
183059aa14a9SRui Paulo 		vap->iv_stats.is_rx_mgtdiscard++;
183196283082SBernhard Schmidt 		break;
183296283082SBernhard Schmidt 
183359aa14a9SRui Paulo 	default:
183459aa14a9SRui Paulo 		IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
183559aa14a9SRui Paulo 		    wh, "mgt", "subtype 0x%x not handled", subtype);
183659aa14a9SRui Paulo 		vap->iv_stats.is_rx_badsubtype++;
183759aa14a9SRui Paulo 		break;
183859aa14a9SRui Paulo 	}
183959aa14a9SRui Paulo }
184059aa14a9SRui Paulo 
18410917631fSRui Paulo static void
18420917631fSRui Paulo mesh_recv_ctl(struct ieee80211_node *ni, struct mbuf *m, int subtype)
18430917631fSRui Paulo {
18440917631fSRui Paulo 
18450917631fSRui Paulo 	switch (subtype) {
18460917631fSRui Paulo 	case IEEE80211_FC0_SUBTYPE_BAR:
18470917631fSRui Paulo 		ieee80211_recv_bar(ni, m);
18480917631fSRui Paulo 		break;
18490917631fSRui Paulo 	}
18500917631fSRui Paulo }
18510917631fSRui Paulo 
185259aa14a9SRui Paulo /*
18536eb9b443SMonthadar Al Jaberi  * Parse meshpeering action ie's for MPM frames
185459aa14a9SRui Paulo  */
185559aa14a9SRui Paulo static const struct ieee80211_meshpeer_ie *
185659aa14a9SRui Paulo mesh_parse_meshpeering_action(struct ieee80211_node *ni,
185759aa14a9SRui Paulo 	const struct ieee80211_frame *wh,	/* XXX for VERIFY_LENGTH */
185859aa14a9SRui Paulo 	const uint8_t *frm, const uint8_t *efrm,
1859c77735e2SRui Paulo 	struct ieee80211_meshpeer_ie *mp, uint8_t subtype)
186059aa14a9SRui Paulo {
186159aa14a9SRui Paulo 	struct ieee80211vap *vap = ni->ni_vap;
186259aa14a9SRui Paulo 	const struct ieee80211_meshpeer_ie *mpie;
18636eb9b443SMonthadar Al Jaberi 	uint16_t args[3];
1864c77735e2SRui Paulo 	const uint8_t *meshid, *meshconf, *meshpeer;
18656eb9b443SMonthadar Al Jaberi 	uint8_t sendclose = 0; /* 1 = MPM frame rejected, close will be sent */
186659aa14a9SRui Paulo 
1867c77735e2SRui Paulo 	meshid = meshconf = meshpeer = NULL;
186859aa14a9SRui Paulo 	while (efrm - frm > 1) {
186959aa14a9SRui Paulo 		IEEE80211_VERIFY_LENGTH(efrm - frm, frm[1] + 2, return NULL);
187059aa14a9SRui Paulo 		switch (*frm) {
187159aa14a9SRui Paulo 		case IEEE80211_ELEMID_MESHID:
187259aa14a9SRui Paulo 			meshid = frm;
187359aa14a9SRui Paulo 			break;
187459aa14a9SRui Paulo 		case IEEE80211_ELEMID_MESHCONF:
187559aa14a9SRui Paulo 			meshconf = frm;
187659aa14a9SRui Paulo 			break;
187759aa14a9SRui Paulo 		case IEEE80211_ELEMID_MESHPEER:
187859aa14a9SRui Paulo 			meshpeer = frm;
187959aa14a9SRui Paulo 			mpie = (const struct ieee80211_meshpeer_ie *) frm;
188059aa14a9SRui Paulo 			memset(mp, 0, sizeof(*mp));
18816eb9b443SMonthadar Al Jaberi 			mp->peer_len = mpie->peer_len;
1882c2042c35SMonthadar Al Jaberi 			mp->peer_proto = LE_READ_2(&mpie->peer_proto);
188359aa14a9SRui Paulo 			mp->peer_llinkid = LE_READ_2(&mpie->peer_llinkid);
1884c2042c35SMonthadar Al Jaberi 			switch (subtype) {
1885c2042c35SMonthadar Al Jaberi 			case IEEE80211_ACTION_MESHPEERING_CONFIRM:
1886c2042c35SMonthadar Al Jaberi 				mp->peer_linkid =
1887c2042c35SMonthadar Al Jaberi 				    LE_READ_2(&mpie->peer_linkid);
1888c2042c35SMonthadar Al Jaberi 				break;
1889c2042c35SMonthadar Al Jaberi 			case IEEE80211_ACTION_MESHPEERING_CLOSE:
1890c2042c35SMonthadar Al Jaberi 				/* NB: peer link ID is optional */
1891c2042c35SMonthadar Al Jaberi 				if (mpie->peer_len ==
1892c2042c35SMonthadar Al Jaberi 				    (IEEE80211_MPM_BASE_SZ + 2)) {
189359aa14a9SRui Paulo 					mp->peer_linkid = 0;
1894c2042c35SMonthadar Al Jaberi 					mp->peer_rcode =
1895c2042c35SMonthadar Al Jaberi 					    LE_READ_2(&mpie->peer_linkid);
189659aa14a9SRui Paulo 				} else {
1897c2042c35SMonthadar Al Jaberi 					mp->peer_linkid =
1898c2042c35SMonthadar Al Jaberi 					    LE_READ_2(&mpie->peer_linkid);
1899c2042c35SMonthadar Al Jaberi 					mp->peer_rcode =
1900c2042c35SMonthadar Al Jaberi 					    LE_READ_2(&mpie->peer_rcode);
1901c2042c35SMonthadar Al Jaberi 				}
1902c2042c35SMonthadar Al Jaberi 				break;
190359aa14a9SRui Paulo 			}
190459aa14a9SRui Paulo 			break;
190559aa14a9SRui Paulo 		}
190659aa14a9SRui Paulo 		frm += frm[1] + 2;
190759aa14a9SRui Paulo 	}
190859aa14a9SRui Paulo 
190959aa14a9SRui Paulo 	/*
19106eb9b443SMonthadar Al Jaberi 	 * Verify the contents of the frame.
19116eb9b443SMonthadar Al Jaberi 	 * If it fails validation, close the peer link.
191259aa14a9SRui Paulo 	 */
19136eb9b443SMonthadar Al Jaberi 	if (mesh_verify_meshpeer(vap, subtype, (const uint8_t *)mp)) {
19146eb9b443SMonthadar Al Jaberi 		sendclose = 1;
19156eb9b443SMonthadar Al Jaberi 		IEEE80211_DISCARD(vap,
19166eb9b443SMonthadar Al Jaberi 		    IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH,
19176eb9b443SMonthadar Al Jaberi 		    wh, NULL, "%s", "MPM validation failed");
19186eb9b443SMonthadar Al Jaberi 	}
191959aa14a9SRui Paulo 
19206eb9b443SMonthadar Al Jaberi 	/* If meshid is not the same reject any frames type. */
19216eb9b443SMonthadar Al Jaberi 	if (sendclose == 0 && mesh_verify_meshid(vap, meshid)) {
19226eb9b443SMonthadar Al Jaberi 		sendclose = 1;
192359aa14a9SRui Paulo 		IEEE80211_DISCARD(vap,
192459aa14a9SRui Paulo 		    IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH,
192559aa14a9SRui Paulo 		    wh, NULL, "%s", "not for our mesh");
19266eb9b443SMonthadar Al Jaberi 		if (subtype == IEEE80211_ACTION_MESHPEERING_CLOSE) {
19276eb9b443SMonthadar Al Jaberi 			/*
19286eb9b443SMonthadar Al Jaberi 			 * Standard not clear about this, if we dont ignore
19296eb9b443SMonthadar Al Jaberi 			 * there will be an endless loop between nodes sending
19306eb9b443SMonthadar Al Jaberi 			 * CLOSE frames between each other with wrong meshid.
19316eb9b443SMonthadar Al Jaberi 			 * Discard and timers will bring FSM to IDLE state.
19326eb9b443SMonthadar Al Jaberi 			 */
19336eb9b443SMonthadar Al Jaberi 			return NULL;
19346eb9b443SMonthadar Al Jaberi 		}
19356eb9b443SMonthadar Al Jaberi 	}
19366eb9b443SMonthadar Al Jaberi 
19376eb9b443SMonthadar Al Jaberi 	/*
19386eb9b443SMonthadar Al Jaberi 	 * Close frames are accepted if meshid is the same.
19396eb9b443SMonthadar Al Jaberi 	 * Verify the other two types.
19406eb9b443SMonthadar Al Jaberi 	 */
19416eb9b443SMonthadar Al Jaberi 	if (sendclose == 0 && subtype != IEEE80211_ACTION_MESHPEERING_CLOSE &&
19426eb9b443SMonthadar Al Jaberi 	    mesh_verify_meshconf(vap, meshconf)) {
19436eb9b443SMonthadar Al Jaberi 		sendclose = 1;
19446eb9b443SMonthadar Al Jaberi 		IEEE80211_DISCARD(vap,
19456eb9b443SMonthadar Al Jaberi 		    IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH,
19466eb9b443SMonthadar Al Jaberi 		    wh, NULL, "%s", "configuration missmatch");
19476eb9b443SMonthadar Al Jaberi 	}
19486eb9b443SMonthadar Al Jaberi 
19496eb9b443SMonthadar Al Jaberi 	if (sendclose) {
195059aa14a9SRui Paulo 		vap->iv_stats.is_rx_mgtdiscard++;
195159aa14a9SRui Paulo 		switch (ni->ni_mlstate) {
195259aa14a9SRui Paulo 		case IEEE80211_NODE_MESH_IDLE:
195359aa14a9SRui Paulo 		case IEEE80211_NODE_MESH_ESTABLISHED:
195459aa14a9SRui Paulo 		case IEEE80211_NODE_MESH_HOLDING:
195559aa14a9SRui Paulo 			/* ignore */
195659aa14a9SRui Paulo 			break;
195759aa14a9SRui Paulo 		case IEEE80211_NODE_MESH_OPENSNT:
195859aa14a9SRui Paulo 		case IEEE80211_NODE_MESH_OPENRCV:
195959aa14a9SRui Paulo 		case IEEE80211_NODE_MESH_CONFIRMRCV:
196059aa14a9SRui Paulo 			args[0] = ni->ni_mlpid;
196159aa14a9SRui Paulo 			args[1] = ni->ni_mllid;
19626eb9b443SMonthadar Al Jaberi 			/* Reason codes for rejection */
19636eb9b443SMonthadar Al Jaberi 			switch (subtype) {
19646eb9b443SMonthadar Al Jaberi 			case IEEE80211_ACTION_MESHPEERING_OPEN:
19656eb9b443SMonthadar Al Jaberi 				args[2] = IEEE80211_REASON_MESH_CPVIOLATION;
19666eb9b443SMonthadar Al Jaberi 				break;
19676eb9b443SMonthadar Al Jaberi 			case IEEE80211_ACTION_MESHPEERING_CONFIRM:
19686eb9b443SMonthadar Al Jaberi 				args[2] = IEEE80211_REASON_MESH_INCONS_PARAMS;
19696eb9b443SMonthadar Al Jaberi 				break;
19706eb9b443SMonthadar Al Jaberi 			}
197159aa14a9SRui Paulo 			ieee80211_send_action(ni,
1972ebeaa1adSMonthadar Al Jaberi 			    IEEE80211_ACTION_CAT_SELF_PROT,
197359aa14a9SRui Paulo 			    IEEE80211_ACTION_MESHPEERING_CLOSE,
197459aa14a9SRui Paulo 			    args);
197559aa14a9SRui Paulo 			mesh_linkchange(ni, IEEE80211_NODE_MESH_HOLDING);
197659aa14a9SRui Paulo 			mesh_peer_timeout_setup(ni);
197759aa14a9SRui Paulo 			break;
197859aa14a9SRui Paulo 		}
197959aa14a9SRui Paulo 		return NULL;
198059aa14a9SRui Paulo 	}
19816eb9b443SMonthadar Al Jaberi 
198259aa14a9SRui Paulo 	return (const struct ieee80211_meshpeer_ie *) mp;
198359aa14a9SRui Paulo }
198459aa14a9SRui Paulo 
198559aa14a9SRui Paulo static int
198659aa14a9SRui Paulo mesh_recv_action_meshpeering_open(struct ieee80211_node *ni,
198759aa14a9SRui Paulo 	const struct ieee80211_frame *wh,
198859aa14a9SRui Paulo 	const uint8_t *frm, const uint8_t *efrm)
198959aa14a9SRui Paulo {
199059aa14a9SRui Paulo 	struct ieee80211vap *vap = ni->ni_vap;
19916eb9b443SMonthadar Al Jaberi 	struct ieee80211_mesh_state *ms = vap->iv_mesh;
199259aa14a9SRui Paulo 	struct ieee80211_meshpeer_ie ie;
199359aa14a9SRui Paulo 	const struct ieee80211_meshpeer_ie *meshpeer;
199459aa14a9SRui Paulo 	uint16_t args[3];
199559aa14a9SRui Paulo 
199659aa14a9SRui Paulo 	/* +2+2 for action + code + capabilites */
1997c77735e2SRui Paulo 	meshpeer = mesh_parse_meshpeering_action(ni, wh, frm+2+2, efrm, &ie,
1998c77735e2SRui Paulo 	    IEEE80211_ACTION_MESHPEERING_OPEN);
199959aa14a9SRui Paulo 	if (meshpeer == NULL) {
200059aa14a9SRui Paulo 		return 0;
200159aa14a9SRui Paulo 	}
200259aa14a9SRui Paulo 
200359aa14a9SRui Paulo 	/* XXX move up */
200459aa14a9SRui Paulo 	IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH, ni,
200559aa14a9SRui Paulo 	    "recv PEER OPEN, lid 0x%x", meshpeer->peer_llinkid);
200659aa14a9SRui Paulo 
200759aa14a9SRui Paulo 	switch (ni->ni_mlstate) {
200859aa14a9SRui Paulo 	case IEEE80211_NODE_MESH_IDLE:
20096eb9b443SMonthadar Al Jaberi 		/* Reject open request if reached our maximum neighbor count */
20106eb9b443SMonthadar Al Jaberi 		if (ms->ms_neighbors >= IEEE80211_MESH_MAX_NEIGHBORS) {
20116eb9b443SMonthadar Al Jaberi 			args[0] = meshpeer->peer_llinkid;
20126eb9b443SMonthadar Al Jaberi 			args[1] = 0;
20136eb9b443SMonthadar Al Jaberi 			args[2] = IEEE80211_REASON_MESH_MAX_PEERS;
20146eb9b443SMonthadar Al Jaberi 			ieee80211_send_action(ni,
20156eb9b443SMonthadar Al Jaberi 			    IEEE80211_ACTION_CAT_SELF_PROT,
20166eb9b443SMonthadar Al Jaberi 			    IEEE80211_ACTION_MESHPEERING_CLOSE,
20176eb9b443SMonthadar Al Jaberi 			    args);
20186eb9b443SMonthadar Al Jaberi 			/* stay in IDLE state */
20196eb9b443SMonthadar Al Jaberi 			return (0);
20206eb9b443SMonthadar Al Jaberi 		}
20216eb9b443SMonthadar Al Jaberi 		/* Open frame accepted */
202259aa14a9SRui Paulo 		mesh_linkchange(ni, IEEE80211_NODE_MESH_OPENRCV);
202359aa14a9SRui Paulo 		ni->ni_mllid = meshpeer->peer_llinkid;
202459aa14a9SRui Paulo 		ni->ni_mlpid = mesh_generateid(vap);
202559aa14a9SRui Paulo 		if (ni->ni_mlpid == 0)
202659aa14a9SRui Paulo 			return 0;		/* XXX */
202759aa14a9SRui Paulo 		args[0] = ni->ni_mlpid;
202859aa14a9SRui Paulo 		/* Announce we're open too... */
202959aa14a9SRui Paulo 		ieee80211_send_action(ni,
2030ebeaa1adSMonthadar Al Jaberi 		    IEEE80211_ACTION_CAT_SELF_PROT,
203159aa14a9SRui Paulo 		    IEEE80211_ACTION_MESHPEERING_OPEN, args);
203259aa14a9SRui Paulo 		/* ...and confirm the link. */
203359aa14a9SRui Paulo 		args[0] = ni->ni_mlpid;
203459aa14a9SRui Paulo 		args[1] = ni->ni_mllid;
203559aa14a9SRui Paulo 		ieee80211_send_action(ni,
2036ebeaa1adSMonthadar Al Jaberi 		    IEEE80211_ACTION_CAT_SELF_PROT,
203759aa14a9SRui Paulo 		    IEEE80211_ACTION_MESHPEERING_CONFIRM,
203859aa14a9SRui Paulo 		    args);
203959aa14a9SRui Paulo 		mesh_peer_timeout_setup(ni);
204059aa14a9SRui Paulo 		break;
204159aa14a9SRui Paulo 	case IEEE80211_NODE_MESH_OPENRCV:
204259aa14a9SRui Paulo 		/* Wrong Link ID */
204359aa14a9SRui Paulo 		if (ni->ni_mllid != meshpeer->peer_llinkid) {
204459aa14a9SRui Paulo 			args[0] = ni->ni_mllid;
204559aa14a9SRui Paulo 			args[1] = ni->ni_mlpid;
204659aa14a9SRui Paulo 			args[2] = IEEE80211_REASON_PEER_LINK_CANCELED;
204759aa14a9SRui Paulo 			ieee80211_send_action(ni,
2048ebeaa1adSMonthadar Al Jaberi 			    IEEE80211_ACTION_CAT_SELF_PROT,
204959aa14a9SRui Paulo 			    IEEE80211_ACTION_MESHPEERING_CLOSE,
205059aa14a9SRui Paulo 			    args);
205159aa14a9SRui Paulo 			mesh_linkchange(ni, IEEE80211_NODE_MESH_HOLDING);
205259aa14a9SRui Paulo 			mesh_peer_timeout_setup(ni);
205359aa14a9SRui Paulo 			break;
205459aa14a9SRui Paulo 		}
205559aa14a9SRui Paulo 		/* Duplicate open, confirm again. */
205659aa14a9SRui Paulo 		args[0] = ni->ni_mlpid;
205759aa14a9SRui Paulo 		args[1] = ni->ni_mllid;
205859aa14a9SRui Paulo 		ieee80211_send_action(ni,
2059ebeaa1adSMonthadar Al Jaberi 		    IEEE80211_ACTION_CAT_SELF_PROT,
206059aa14a9SRui Paulo 		    IEEE80211_ACTION_MESHPEERING_CONFIRM,
206159aa14a9SRui Paulo 		    args);
206259aa14a9SRui Paulo 		break;
206359aa14a9SRui Paulo 	case IEEE80211_NODE_MESH_OPENSNT:
206459aa14a9SRui Paulo 		ni->ni_mllid = meshpeer->peer_llinkid;
206559aa14a9SRui Paulo 		mesh_linkchange(ni, IEEE80211_NODE_MESH_OPENRCV);
206659aa14a9SRui Paulo 		args[0] = ni->ni_mlpid;
206759aa14a9SRui Paulo 		args[1] = ni->ni_mllid;
206859aa14a9SRui Paulo 		ieee80211_send_action(ni,
2069ebeaa1adSMonthadar Al Jaberi 		    IEEE80211_ACTION_CAT_SELF_PROT,
207059aa14a9SRui Paulo 		    IEEE80211_ACTION_MESHPEERING_CONFIRM,
207159aa14a9SRui Paulo 		    args);
207259aa14a9SRui Paulo 		/* NB: don't setup/clear any timeout */
207359aa14a9SRui Paulo 		break;
207459aa14a9SRui Paulo 	case IEEE80211_NODE_MESH_CONFIRMRCV:
207559aa14a9SRui Paulo 		if (ni->ni_mlpid != meshpeer->peer_linkid ||
207659aa14a9SRui Paulo 		    ni->ni_mllid != meshpeer->peer_llinkid) {
207759aa14a9SRui Paulo 			args[0] = ni->ni_mlpid;
207859aa14a9SRui Paulo 			args[1] = ni->ni_mllid;
207959aa14a9SRui Paulo 			args[2] = IEEE80211_REASON_PEER_LINK_CANCELED;
208059aa14a9SRui Paulo 			ieee80211_send_action(ni,
2081ebeaa1adSMonthadar Al Jaberi 			    IEEE80211_ACTION_CAT_SELF_PROT,
208259aa14a9SRui Paulo 			    IEEE80211_ACTION_MESHPEERING_CLOSE,
208359aa14a9SRui Paulo 			    args);
208459aa14a9SRui Paulo 			mesh_linkchange(ni,
208559aa14a9SRui Paulo 			    IEEE80211_NODE_MESH_HOLDING);
208659aa14a9SRui Paulo 			mesh_peer_timeout_setup(ni);
208759aa14a9SRui Paulo 			break;
208859aa14a9SRui Paulo 		}
208959aa14a9SRui Paulo 		mesh_linkchange(ni, IEEE80211_NODE_MESH_ESTABLISHED);
209059aa14a9SRui Paulo 		ni->ni_mllid = meshpeer->peer_llinkid;
209159aa14a9SRui Paulo 		args[0] = ni->ni_mlpid;
209259aa14a9SRui Paulo 		args[1] = ni->ni_mllid;
209359aa14a9SRui Paulo 		ieee80211_send_action(ni,
2094ebeaa1adSMonthadar Al Jaberi 		    IEEE80211_ACTION_CAT_SELF_PROT,
209559aa14a9SRui Paulo 		    IEEE80211_ACTION_MESHPEERING_CONFIRM,
209659aa14a9SRui Paulo 		    args);
209759aa14a9SRui Paulo 		mesh_peer_timeout_stop(ni);
209859aa14a9SRui Paulo 		break;
209959aa14a9SRui Paulo 	case IEEE80211_NODE_MESH_ESTABLISHED:
210059aa14a9SRui Paulo 		if (ni->ni_mllid != meshpeer->peer_llinkid) {
210159aa14a9SRui Paulo 			args[0] = ni->ni_mllid;
210259aa14a9SRui Paulo 			args[1] = ni->ni_mlpid;
210359aa14a9SRui Paulo 			args[2] = IEEE80211_REASON_PEER_LINK_CANCELED;
210459aa14a9SRui Paulo 			ieee80211_send_action(ni,
2105ebeaa1adSMonthadar Al Jaberi 			    IEEE80211_ACTION_CAT_SELF_PROT,
210659aa14a9SRui Paulo 			    IEEE80211_ACTION_MESHPEERING_CLOSE,
210759aa14a9SRui Paulo 			    args);
210859aa14a9SRui Paulo 			mesh_linkchange(ni, IEEE80211_NODE_MESH_HOLDING);
210959aa14a9SRui Paulo 			mesh_peer_timeout_setup(ni);
211059aa14a9SRui Paulo 			break;
211159aa14a9SRui Paulo 		}
211259aa14a9SRui Paulo 		args[0] = ni->ni_mlpid;
211359aa14a9SRui Paulo 		args[1] = ni->ni_mllid;
211459aa14a9SRui Paulo 		ieee80211_send_action(ni,
2115ebeaa1adSMonthadar Al Jaberi 		    IEEE80211_ACTION_CAT_SELF_PROT,
211659aa14a9SRui Paulo 		    IEEE80211_ACTION_MESHPEERING_CONFIRM,
211759aa14a9SRui Paulo 		    args);
211859aa14a9SRui Paulo 		break;
211959aa14a9SRui Paulo 	case IEEE80211_NODE_MESH_HOLDING:
212059aa14a9SRui Paulo 		args[0] = ni->ni_mlpid;
212159aa14a9SRui Paulo 		args[1] = meshpeer->peer_llinkid;
21226eb9b443SMonthadar Al Jaberi 		/* Standard not clear about what the reaason code should be */
21236eb9b443SMonthadar Al Jaberi 		args[2] = IEEE80211_REASON_PEER_LINK_CANCELED;
212459aa14a9SRui Paulo 		ieee80211_send_action(ni,
2125ebeaa1adSMonthadar Al Jaberi 		    IEEE80211_ACTION_CAT_SELF_PROT,
212659aa14a9SRui Paulo 		    IEEE80211_ACTION_MESHPEERING_CLOSE,
212759aa14a9SRui Paulo 		    args);
212859aa14a9SRui Paulo 		break;
212959aa14a9SRui Paulo 	}
213059aa14a9SRui Paulo 	return 0;
213159aa14a9SRui Paulo }
213259aa14a9SRui Paulo 
213359aa14a9SRui Paulo static int
213459aa14a9SRui Paulo mesh_recv_action_meshpeering_confirm(struct ieee80211_node *ni,
213559aa14a9SRui Paulo 	const struct ieee80211_frame *wh,
213659aa14a9SRui Paulo 	const uint8_t *frm, const uint8_t *efrm)
213759aa14a9SRui Paulo {
213859aa14a9SRui Paulo 	struct ieee80211vap *vap = ni->ni_vap;
213959aa14a9SRui Paulo 	struct ieee80211_meshpeer_ie ie;
214059aa14a9SRui Paulo 	const struct ieee80211_meshpeer_ie *meshpeer;
214159aa14a9SRui Paulo 	uint16_t args[3];
214259aa14a9SRui Paulo 
214359aa14a9SRui Paulo 	/* +2+2+2+2 for action + code + capabilites + status code + AID */
2144c77735e2SRui Paulo 	meshpeer = mesh_parse_meshpeering_action(ni, wh, frm+2+2+2+2, efrm, &ie,
2145c77735e2SRui Paulo 	    IEEE80211_ACTION_MESHPEERING_CONFIRM);
214659aa14a9SRui Paulo 	if (meshpeer == NULL) {
214759aa14a9SRui Paulo 		return 0;
214859aa14a9SRui Paulo 	}
214959aa14a9SRui Paulo 
215059aa14a9SRui Paulo 	IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH, ni,
215159aa14a9SRui Paulo 	    "recv PEER CONFIRM, local id 0x%x, peer id 0x%x",
215259aa14a9SRui Paulo 	    meshpeer->peer_llinkid, meshpeer->peer_linkid);
215359aa14a9SRui Paulo 
215459aa14a9SRui Paulo 	switch (ni->ni_mlstate) {
215559aa14a9SRui Paulo 	case IEEE80211_NODE_MESH_OPENRCV:
215659aa14a9SRui Paulo 		mesh_linkchange(ni, IEEE80211_NODE_MESH_ESTABLISHED);
215759aa14a9SRui Paulo 		mesh_peer_timeout_stop(ni);
215859aa14a9SRui Paulo 		break;
215959aa14a9SRui Paulo 	case IEEE80211_NODE_MESH_OPENSNT:
216059aa14a9SRui Paulo 		mesh_linkchange(ni, IEEE80211_NODE_MESH_CONFIRMRCV);
21616eb9b443SMonthadar Al Jaberi 		mesh_peer_timeout_setup(ni);
216259aa14a9SRui Paulo 		break;
216359aa14a9SRui Paulo 	case IEEE80211_NODE_MESH_HOLDING:
216459aa14a9SRui Paulo 		args[0] = ni->ni_mlpid;
216559aa14a9SRui Paulo 		args[1] = meshpeer->peer_llinkid;
21666eb9b443SMonthadar Al Jaberi 		/* Standard not clear about what the reaason code should be */
21676eb9b443SMonthadar Al Jaberi 		args[2] = IEEE80211_REASON_PEER_LINK_CANCELED;
216859aa14a9SRui Paulo 		ieee80211_send_action(ni,
2169ebeaa1adSMonthadar Al Jaberi 		    IEEE80211_ACTION_CAT_SELF_PROT,
217059aa14a9SRui Paulo 		    IEEE80211_ACTION_MESHPEERING_CLOSE,
217159aa14a9SRui Paulo 		    args);
217259aa14a9SRui Paulo 		break;
217359aa14a9SRui Paulo 	case IEEE80211_NODE_MESH_CONFIRMRCV:
217459aa14a9SRui Paulo 		if (ni->ni_mllid != meshpeer->peer_llinkid) {
217559aa14a9SRui Paulo 			args[0] = ni->ni_mlpid;
217659aa14a9SRui Paulo 			args[1] = ni->ni_mllid;
217759aa14a9SRui Paulo 			args[2] = IEEE80211_REASON_PEER_LINK_CANCELED;
217859aa14a9SRui Paulo 			ieee80211_send_action(ni,
2179ebeaa1adSMonthadar Al Jaberi 			    IEEE80211_ACTION_CAT_SELF_PROT,
218059aa14a9SRui Paulo 			    IEEE80211_ACTION_MESHPEERING_CLOSE,
218159aa14a9SRui Paulo 			    args);
218259aa14a9SRui Paulo 			mesh_linkchange(ni, IEEE80211_NODE_MESH_HOLDING);
218359aa14a9SRui Paulo 			mesh_peer_timeout_setup(ni);
218459aa14a9SRui Paulo 		}
218559aa14a9SRui Paulo 		break;
218659aa14a9SRui Paulo 	default:
218759aa14a9SRui Paulo 		IEEE80211_DISCARD(vap,
218859aa14a9SRui Paulo 		    IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH,
218959aa14a9SRui Paulo 		    wh, NULL, "received confirm in invalid state %d",
219059aa14a9SRui Paulo 		    ni->ni_mlstate);
219159aa14a9SRui Paulo 		vap->iv_stats.is_rx_mgtdiscard++;
219259aa14a9SRui Paulo 		break;
219359aa14a9SRui Paulo 	}
219459aa14a9SRui Paulo 	return 0;
219559aa14a9SRui Paulo }
219659aa14a9SRui Paulo 
219759aa14a9SRui Paulo static int
219859aa14a9SRui Paulo mesh_recv_action_meshpeering_close(struct ieee80211_node *ni,
219959aa14a9SRui Paulo 	const struct ieee80211_frame *wh,
220059aa14a9SRui Paulo 	const uint8_t *frm, const uint8_t *efrm)
220159aa14a9SRui Paulo {
22026eb9b443SMonthadar Al Jaberi 	struct ieee80211_meshpeer_ie ie;
22036eb9b443SMonthadar Al Jaberi 	const struct ieee80211_meshpeer_ie *meshpeer;
220459aa14a9SRui Paulo 	uint16_t args[3];
220559aa14a9SRui Paulo 
22066eb9b443SMonthadar Al Jaberi 	/* +2 for action + code */
22076eb9b443SMonthadar Al Jaberi 	meshpeer = mesh_parse_meshpeering_action(ni, wh, frm+2, efrm, &ie,
22086eb9b443SMonthadar Al Jaberi 	    IEEE80211_ACTION_MESHPEERING_CLOSE);
22096eb9b443SMonthadar Al Jaberi 	if (meshpeer == NULL) {
22106eb9b443SMonthadar Al Jaberi 		return 0;
22116eb9b443SMonthadar Al Jaberi 	}
22126eb9b443SMonthadar Al Jaberi 
22136eb9b443SMonthadar Al Jaberi 	/*
22146eb9b443SMonthadar Al Jaberi 	 * XXX: check reason code, for example we could receive
22156eb9b443SMonthadar Al Jaberi 	 * IEEE80211_REASON_MESH_MAX_PEERS then we should not attempt
22166eb9b443SMonthadar Al Jaberi 	 * to peer again.
22176eb9b443SMonthadar Al Jaberi 	 */
22186eb9b443SMonthadar Al Jaberi 
221959aa14a9SRui Paulo 	IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH,
222059aa14a9SRui Paulo 	    ni, "%s", "recv PEER CLOSE");
222159aa14a9SRui Paulo 
222259aa14a9SRui Paulo 	switch (ni->ni_mlstate) {
222359aa14a9SRui Paulo 	case IEEE80211_NODE_MESH_IDLE:
222459aa14a9SRui Paulo 		/* ignore */
222559aa14a9SRui Paulo 		break;
222659aa14a9SRui Paulo 	case IEEE80211_NODE_MESH_OPENRCV:
222759aa14a9SRui Paulo 	case IEEE80211_NODE_MESH_OPENSNT:
222859aa14a9SRui Paulo 	case IEEE80211_NODE_MESH_CONFIRMRCV:
222959aa14a9SRui Paulo 	case IEEE80211_NODE_MESH_ESTABLISHED:
223059aa14a9SRui Paulo 		args[0] = ni->ni_mlpid;
223159aa14a9SRui Paulo 		args[1] = ni->ni_mllid;
223259aa14a9SRui Paulo 		args[2] = IEEE80211_REASON_MESH_CLOSE_RCVD;
223359aa14a9SRui Paulo 		ieee80211_send_action(ni,
2234ebeaa1adSMonthadar Al Jaberi 		    IEEE80211_ACTION_CAT_SELF_PROT,
223559aa14a9SRui Paulo 		    IEEE80211_ACTION_MESHPEERING_CLOSE,
223659aa14a9SRui Paulo 		    args);
223759aa14a9SRui Paulo 		mesh_linkchange(ni, IEEE80211_NODE_MESH_HOLDING);
223859aa14a9SRui Paulo 		mesh_peer_timeout_setup(ni);
223959aa14a9SRui Paulo 		break;
224059aa14a9SRui Paulo 	case IEEE80211_NODE_MESH_HOLDING:
224159aa14a9SRui Paulo 		mesh_linkchange(ni, IEEE80211_NODE_MESH_IDLE);
22426eb9b443SMonthadar Al Jaberi 		mesh_peer_timeout_stop(ni);
224359aa14a9SRui Paulo 		break;
224459aa14a9SRui Paulo 	}
224559aa14a9SRui Paulo 	return 0;
224659aa14a9SRui Paulo }
224759aa14a9SRui Paulo 
224859aa14a9SRui Paulo /*
224959aa14a9SRui Paulo  * Link Metric handling.
225059aa14a9SRui Paulo  */
225159aa14a9SRui Paulo static int
2252bdd2a076SAdrian Chadd mesh_recv_action_meshlmetric(struct ieee80211_node *ni,
225359aa14a9SRui Paulo 	const struct ieee80211_frame *wh,
225459aa14a9SRui Paulo 	const uint8_t *frm, const uint8_t *efrm)
225559aa14a9SRui Paulo {
2256bdd2a076SAdrian Chadd 	const struct ieee80211_meshlmetric_ie *ie =
2257bdd2a076SAdrian Chadd 	    (const struct ieee80211_meshlmetric_ie *)
2258bdd2a076SAdrian Chadd 	    (frm+2); /* action + code */
2259bdd2a076SAdrian Chadd 	struct ieee80211_meshlmetric_ie lm_rep;
226059aa14a9SRui Paulo 
2261bdd2a076SAdrian Chadd 	if (ie->lm_flags & IEEE80211_MESH_LMETRIC_FLAGS_REQ) {
2262bdd2a076SAdrian Chadd 		lm_rep.lm_flags = 0;
2263bdd2a076SAdrian Chadd 		lm_rep.lm_metric = mesh_airtime_calc(ni);
226459aa14a9SRui Paulo 		ieee80211_send_action(ni,
2265bdd2a076SAdrian Chadd 		    IEEE80211_ACTION_CAT_MESH,
2266bdd2a076SAdrian Chadd 		    IEEE80211_ACTION_MESH_LMETRIC,
2267bdd2a076SAdrian Chadd 		    &lm_rep);
226859aa14a9SRui Paulo 	}
2269bdd2a076SAdrian Chadd 	/* XXX: else do nothing for now */
227059aa14a9SRui Paulo 	return 0;
227159aa14a9SRui Paulo }
227259aa14a9SRui Paulo 
227359aa14a9SRui Paulo static int
227459aa14a9SRui Paulo mesh_send_action(struct ieee80211_node *ni, struct mbuf *m)
227559aa14a9SRui Paulo {
227659aa14a9SRui Paulo 	struct ieee80211_bpf_params params;
227759aa14a9SRui Paulo 
227859aa14a9SRui Paulo 	memset(&params, 0, sizeof(params));
227959aa14a9SRui Paulo 	params.ibp_pri = WME_AC_VO;
228059aa14a9SRui Paulo 	params.ibp_rate0 = ni->ni_txparms->mgmtrate;
228159aa14a9SRui Paulo 	/* XXX ucast/mcast */
228259aa14a9SRui Paulo 	params.ibp_try0 = ni->ni_txparms->maxretry;
228359aa14a9SRui Paulo 	params.ibp_power = ni->ni_txpower;
228459aa14a9SRui Paulo 	return ieee80211_mgmt_output(ni, m, IEEE80211_FC0_SUBTYPE_ACTION,
228559aa14a9SRui Paulo 	     &params);
228659aa14a9SRui Paulo }
228759aa14a9SRui Paulo 
228859aa14a9SRui Paulo #define	ADDSHORT(frm, v) do {			\
228959aa14a9SRui Paulo 	frm[0] = (v) & 0xff;			\
229059aa14a9SRui Paulo 	frm[1] = (v) >> 8;			\
229159aa14a9SRui Paulo 	frm += 2;				\
229259aa14a9SRui Paulo } while (0)
229359aa14a9SRui Paulo #define	ADDWORD(frm, v) do {			\
229459aa14a9SRui Paulo 	frm[0] = (v) & 0xff;			\
229559aa14a9SRui Paulo 	frm[1] = ((v) >> 8) & 0xff;		\
229659aa14a9SRui Paulo 	frm[2] = ((v) >> 16) & 0xff;		\
229759aa14a9SRui Paulo 	frm[3] = ((v) >> 24) & 0xff;		\
229859aa14a9SRui Paulo 	frm += 4;				\
229959aa14a9SRui Paulo } while (0)
230059aa14a9SRui Paulo 
230159aa14a9SRui Paulo static int
230259aa14a9SRui Paulo mesh_send_action_meshpeering_open(struct ieee80211_node *ni,
230359aa14a9SRui Paulo 	int category, int action, void *args0)
230459aa14a9SRui Paulo {
230559aa14a9SRui Paulo 	struct ieee80211vap *vap = ni->ni_vap;
230659aa14a9SRui Paulo 	struct ieee80211com *ic = ni->ni_ic;
230759aa14a9SRui Paulo 	uint16_t *args = args0;
230859aa14a9SRui Paulo 	const struct ieee80211_rateset *rs;
230959aa14a9SRui Paulo 	struct mbuf *m;
231059aa14a9SRui Paulo 	uint8_t *frm;
231159aa14a9SRui Paulo 
231259aa14a9SRui Paulo 	IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH, ni,
231359aa14a9SRui Paulo 	    "send PEER OPEN action: localid 0x%x", args[0]);
231459aa14a9SRui Paulo 
231559aa14a9SRui Paulo 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
231659aa14a9SRui Paulo 	    "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", __func__, __LINE__,
231759aa14a9SRui Paulo 	    ni, ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)+1);
231859aa14a9SRui Paulo 	ieee80211_ref_node(ni);
231959aa14a9SRui Paulo 
232059aa14a9SRui Paulo 	m = ieee80211_getmgtframe(&frm,
232159aa14a9SRui Paulo 	    ic->ic_headroom + sizeof(struct ieee80211_frame),
232259aa14a9SRui Paulo 	    sizeof(uint16_t)	/* action+category */
232359aa14a9SRui Paulo 	    + sizeof(uint16_t)	/* capabilites */
232459aa14a9SRui Paulo 	    + 2 + IEEE80211_RATE_SIZE
232559aa14a9SRui Paulo 	    + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
232659aa14a9SRui Paulo 	    + 2 + IEEE80211_MESHID_LEN
232759aa14a9SRui Paulo 	    + sizeof(struct ieee80211_meshconf_ie)
232859aa14a9SRui Paulo 	    + sizeof(struct ieee80211_meshpeer_ie)
232959aa14a9SRui Paulo 	);
233059aa14a9SRui Paulo 	if (m != NULL) {
233159aa14a9SRui Paulo 		/*
233259aa14a9SRui Paulo 		 * mesh peer open action frame format:
233359aa14a9SRui Paulo 		 *   [1] category
233459aa14a9SRui Paulo 		 *   [1] action
233559aa14a9SRui Paulo 		 *   [2] capabilities
233659aa14a9SRui Paulo 		 *   [tlv] rates
233759aa14a9SRui Paulo 		 *   [tlv] xrates
233859aa14a9SRui Paulo 		 *   [tlv] mesh id
233959aa14a9SRui Paulo 		 *   [tlv] mesh conf
234059aa14a9SRui Paulo 		 *   [tlv] mesh peer link mgmt
234159aa14a9SRui Paulo 		 */
234259aa14a9SRui Paulo 		*frm++ = category;
234359aa14a9SRui Paulo 		*frm++ = action;
234459aa14a9SRui Paulo 		ADDSHORT(frm, ieee80211_getcapinfo(vap, ni->ni_chan));
234559aa14a9SRui Paulo 		rs = ieee80211_get_suprates(ic, ic->ic_curchan);
234659aa14a9SRui Paulo 		frm = ieee80211_add_rates(frm, rs);
234759aa14a9SRui Paulo 		frm = ieee80211_add_xrates(frm, rs);
234859aa14a9SRui Paulo 		frm = ieee80211_add_meshid(frm, vap);
234959aa14a9SRui Paulo 		frm = ieee80211_add_meshconf(frm, vap);
2350ebeaa1adSMonthadar Al Jaberi 		frm = ieee80211_add_meshpeer(frm, IEEE80211_ACTION_MESHPEERING_OPEN,
235159aa14a9SRui Paulo 		    args[0], 0, 0);
235259aa14a9SRui Paulo 		m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
235359aa14a9SRui Paulo 		return mesh_send_action(ni, m);
235459aa14a9SRui Paulo 	} else {
235559aa14a9SRui Paulo 		vap->iv_stats.is_tx_nobuf++;
235659aa14a9SRui Paulo 		ieee80211_free_node(ni);
235759aa14a9SRui Paulo 		return ENOMEM;
235859aa14a9SRui Paulo 	}
235959aa14a9SRui Paulo }
236059aa14a9SRui Paulo 
236159aa14a9SRui Paulo static int
236259aa14a9SRui Paulo mesh_send_action_meshpeering_confirm(struct ieee80211_node *ni,
236359aa14a9SRui Paulo 	int category, int action, void *args0)
236459aa14a9SRui Paulo {
236559aa14a9SRui Paulo 	struct ieee80211vap *vap = ni->ni_vap;
236659aa14a9SRui Paulo 	struct ieee80211com *ic = ni->ni_ic;
236759aa14a9SRui Paulo 	uint16_t *args = args0;
236859aa14a9SRui Paulo 	const struct ieee80211_rateset *rs;
236959aa14a9SRui Paulo 	struct mbuf *m;
237059aa14a9SRui Paulo 	uint8_t *frm;
237159aa14a9SRui Paulo 
237259aa14a9SRui Paulo 	IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH, ni,
237359aa14a9SRui Paulo 	    "send PEER CONFIRM action: localid 0x%x, peerid 0x%x",
237459aa14a9SRui Paulo 	    args[0], args[1]);
237559aa14a9SRui Paulo 
237659aa14a9SRui Paulo 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
237759aa14a9SRui Paulo 	    "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", __func__, __LINE__,
237859aa14a9SRui Paulo 	    ni, ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)+1);
237959aa14a9SRui Paulo 	ieee80211_ref_node(ni);
238059aa14a9SRui Paulo 
238159aa14a9SRui Paulo 	m = ieee80211_getmgtframe(&frm,
238259aa14a9SRui Paulo 	    ic->ic_headroom + sizeof(struct ieee80211_frame),
238359aa14a9SRui Paulo 	    sizeof(uint16_t)	/* action+category */
238459aa14a9SRui Paulo 	    + sizeof(uint16_t)	/* capabilites */
238559aa14a9SRui Paulo 	    + sizeof(uint16_t)	/* status code */
238659aa14a9SRui Paulo 	    + sizeof(uint16_t)	/* AID */
238759aa14a9SRui Paulo 	    + 2 + IEEE80211_RATE_SIZE
238859aa14a9SRui Paulo 	    + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
238959aa14a9SRui Paulo 	    + 2 + IEEE80211_MESHID_LEN
239059aa14a9SRui Paulo 	    + sizeof(struct ieee80211_meshconf_ie)
239159aa14a9SRui Paulo 	    + sizeof(struct ieee80211_meshpeer_ie)
239259aa14a9SRui Paulo 	);
239359aa14a9SRui Paulo 	if (m != NULL) {
239459aa14a9SRui Paulo 		/*
239559aa14a9SRui Paulo 		 * mesh peer confirm action frame format:
239659aa14a9SRui Paulo 		 *   [1] category
239759aa14a9SRui Paulo 		 *   [1] action
239859aa14a9SRui Paulo 		 *   [2] capabilities
239959aa14a9SRui Paulo 		 *   [2] status code
240059aa14a9SRui Paulo 		 *   [2] association id (peer ID)
240159aa14a9SRui Paulo 		 *   [tlv] rates
240259aa14a9SRui Paulo 		 *   [tlv] xrates
240359aa14a9SRui Paulo 		 *   [tlv] mesh id
240459aa14a9SRui Paulo 		 *   [tlv] mesh conf
240559aa14a9SRui Paulo 		 *   [tlv] mesh peer link mgmt
240659aa14a9SRui Paulo 		 */
240759aa14a9SRui Paulo 		*frm++ = category;
240859aa14a9SRui Paulo 		*frm++ = action;
240959aa14a9SRui Paulo 		ADDSHORT(frm, ieee80211_getcapinfo(vap, ni->ni_chan));
241059aa14a9SRui Paulo 		ADDSHORT(frm, 0);		/* status code */
241159aa14a9SRui Paulo 		ADDSHORT(frm, args[1]);		/* AID */
241259aa14a9SRui Paulo 		rs = ieee80211_get_suprates(ic, ic->ic_curchan);
241359aa14a9SRui Paulo 		frm = ieee80211_add_rates(frm, rs);
241459aa14a9SRui Paulo 		frm = ieee80211_add_xrates(frm, rs);
241559aa14a9SRui Paulo 		frm = ieee80211_add_meshid(frm, vap);
241659aa14a9SRui Paulo 		frm = ieee80211_add_meshconf(frm, vap);
241759aa14a9SRui Paulo 		frm = ieee80211_add_meshpeer(frm,
2418ebeaa1adSMonthadar Al Jaberi 		    IEEE80211_ACTION_MESHPEERING_CONFIRM,
241959aa14a9SRui Paulo 		    args[0], args[1], 0);
242059aa14a9SRui Paulo 		m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
242159aa14a9SRui Paulo 		return mesh_send_action(ni, m);
242259aa14a9SRui Paulo 	} else {
242359aa14a9SRui Paulo 		vap->iv_stats.is_tx_nobuf++;
242459aa14a9SRui Paulo 		ieee80211_free_node(ni);
242559aa14a9SRui Paulo 		return ENOMEM;
242659aa14a9SRui Paulo 	}
242759aa14a9SRui Paulo }
242859aa14a9SRui Paulo 
242959aa14a9SRui Paulo static int
243059aa14a9SRui Paulo mesh_send_action_meshpeering_close(struct ieee80211_node *ni,
243159aa14a9SRui Paulo 	int category, int action, void *args0)
243259aa14a9SRui Paulo {
243359aa14a9SRui Paulo 	struct ieee80211vap *vap = ni->ni_vap;
243459aa14a9SRui Paulo 	struct ieee80211com *ic = ni->ni_ic;
243559aa14a9SRui Paulo 	uint16_t *args = args0;
243659aa14a9SRui Paulo 	struct mbuf *m;
243759aa14a9SRui Paulo 	uint8_t *frm;
243859aa14a9SRui Paulo 
243959aa14a9SRui Paulo 	IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH, ni,
244059aa14a9SRui Paulo 	    "send PEER CLOSE action: localid 0x%x, peerid 0x%x reason %d",
244159aa14a9SRui Paulo 	    args[0], args[1], args[2]);
244259aa14a9SRui Paulo 
244359aa14a9SRui Paulo 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
244459aa14a9SRui Paulo 	    "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", __func__, __LINE__,
244559aa14a9SRui Paulo 	    ni, ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)+1);
244659aa14a9SRui Paulo 	ieee80211_ref_node(ni);
244759aa14a9SRui Paulo 
244859aa14a9SRui Paulo 	m = ieee80211_getmgtframe(&frm,
244959aa14a9SRui Paulo 	    ic->ic_headroom + sizeof(struct ieee80211_frame),
245059aa14a9SRui Paulo 	    sizeof(uint16_t)	/* action+category */
245159aa14a9SRui Paulo 	    + sizeof(uint16_t)	/* reason code */
245259aa14a9SRui Paulo 	    + 2 + IEEE80211_MESHID_LEN
245359aa14a9SRui Paulo 	    + sizeof(struct ieee80211_meshpeer_ie)
245459aa14a9SRui Paulo 	);
245559aa14a9SRui Paulo 	if (m != NULL) {
245659aa14a9SRui Paulo 		/*
245759aa14a9SRui Paulo 		 * mesh peer close action frame format:
245859aa14a9SRui Paulo 		 *   [1] category
245959aa14a9SRui Paulo 		 *   [1] action
246059aa14a9SRui Paulo 		 *   [tlv] mesh id
246159aa14a9SRui Paulo 		 *   [tlv] mesh peer link mgmt
246259aa14a9SRui Paulo 		 */
246359aa14a9SRui Paulo 		*frm++ = category;
246459aa14a9SRui Paulo 		*frm++ = action;
246559aa14a9SRui Paulo 		frm = ieee80211_add_meshid(frm, vap);
246659aa14a9SRui Paulo 		frm = ieee80211_add_meshpeer(frm,
2467ebeaa1adSMonthadar Al Jaberi 		    IEEE80211_ACTION_MESHPEERING_CLOSE,
246859aa14a9SRui Paulo 		    args[0], args[1], args[2]);
246959aa14a9SRui Paulo 		m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
247059aa14a9SRui Paulo 		return mesh_send_action(ni, m);
247159aa14a9SRui Paulo 	} else {
247259aa14a9SRui Paulo 		vap->iv_stats.is_tx_nobuf++;
247359aa14a9SRui Paulo 		ieee80211_free_node(ni);
247459aa14a9SRui Paulo 		return ENOMEM;
247559aa14a9SRui Paulo 	}
247659aa14a9SRui Paulo }
247759aa14a9SRui Paulo 
247859aa14a9SRui Paulo static int
2479bdd2a076SAdrian Chadd mesh_send_action_meshlmetric(struct ieee80211_node *ni,
248059aa14a9SRui Paulo 	int category, int action, void *arg0)
248159aa14a9SRui Paulo {
248259aa14a9SRui Paulo 	struct ieee80211vap *vap = ni->ni_vap;
248359aa14a9SRui Paulo 	struct ieee80211com *ic = ni->ni_ic;
2484bdd2a076SAdrian Chadd 	struct ieee80211_meshlmetric_ie *ie = arg0;
248559aa14a9SRui Paulo 	struct mbuf *m;
248659aa14a9SRui Paulo 	uint8_t *frm;
248759aa14a9SRui Paulo 
2488bdd2a076SAdrian Chadd 	if (ie->lm_flags & IEEE80211_MESH_LMETRIC_FLAGS_REQ) {
2489bdd2a076SAdrian Chadd 		IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH,
2490bdd2a076SAdrian Chadd 		    ni, "%s", "send LINK METRIC REQUEST action");
249159aa14a9SRui Paulo 	} else {
2492bdd2a076SAdrian Chadd 		IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH,
2493bdd2a076SAdrian Chadd 		    ni, "send LINK METRIC REPLY action: metric 0x%x",
2494bdd2a076SAdrian Chadd 		    ie->lm_metric);
249559aa14a9SRui Paulo 	}
249659aa14a9SRui Paulo 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
249759aa14a9SRui Paulo 	    "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", __func__, __LINE__,
249859aa14a9SRui Paulo 	    ni, ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)+1);
249959aa14a9SRui Paulo 	ieee80211_ref_node(ni);
250059aa14a9SRui Paulo 
250159aa14a9SRui Paulo 	m = ieee80211_getmgtframe(&frm,
250259aa14a9SRui Paulo 	    ic->ic_headroom + sizeof(struct ieee80211_frame),
2503bdd2a076SAdrian Chadd 	    sizeof(uint16_t) +	/* action+category */
2504bdd2a076SAdrian Chadd 	    sizeof(struct ieee80211_meshlmetric_ie)
250559aa14a9SRui Paulo 	);
250659aa14a9SRui Paulo 	if (m != NULL) {
250759aa14a9SRui Paulo 		/*
2508bdd2a076SAdrian Chadd 		 * mesh link metric
250959aa14a9SRui Paulo 		 *   [1] category
251059aa14a9SRui Paulo 		 *   [1] action
251159aa14a9SRui Paulo 		 *   [tlv] mesh link metric
251259aa14a9SRui Paulo 		 */
251359aa14a9SRui Paulo 		*frm++ = category;
251459aa14a9SRui Paulo 		*frm++ = action;
2515bdd2a076SAdrian Chadd 		frm = ieee80211_add_meshlmetric(frm,
2516bdd2a076SAdrian Chadd 		    ie->lm_flags, ie->lm_metric);
251759aa14a9SRui Paulo 		m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
251859aa14a9SRui Paulo 		return mesh_send_action(ni, m);
251959aa14a9SRui Paulo 	} else {
252059aa14a9SRui Paulo 		vap->iv_stats.is_tx_nobuf++;
252159aa14a9SRui Paulo 		ieee80211_free_node(ni);
252259aa14a9SRui Paulo 		return ENOMEM;
252359aa14a9SRui Paulo 	}
252459aa14a9SRui Paulo }
252559aa14a9SRui Paulo 
252659aa14a9SRui Paulo static void
252759aa14a9SRui Paulo mesh_peer_timeout_setup(struct ieee80211_node *ni)
252859aa14a9SRui Paulo {
252959aa14a9SRui Paulo 	switch (ni->ni_mlstate) {
253059aa14a9SRui Paulo 	case IEEE80211_NODE_MESH_HOLDING:
253159aa14a9SRui Paulo 		ni->ni_mltval = ieee80211_mesh_holdingtimeout;
253259aa14a9SRui Paulo 		break;
253359aa14a9SRui Paulo 	case IEEE80211_NODE_MESH_CONFIRMRCV:
253459aa14a9SRui Paulo 		ni->ni_mltval = ieee80211_mesh_confirmtimeout;
253559aa14a9SRui Paulo 		break;
253659aa14a9SRui Paulo 	case IEEE80211_NODE_MESH_IDLE:
253759aa14a9SRui Paulo 		ni->ni_mltval = 0;
253859aa14a9SRui Paulo 		break;
253959aa14a9SRui Paulo 	default:
254059aa14a9SRui Paulo 		ni->ni_mltval = ieee80211_mesh_retrytimeout;
254159aa14a9SRui Paulo 		break;
254259aa14a9SRui Paulo 	}
254359aa14a9SRui Paulo 	if (ni->ni_mltval)
254459aa14a9SRui Paulo 		callout_reset(&ni->ni_mltimer, ni->ni_mltval,
254559aa14a9SRui Paulo 		    mesh_peer_timeout_cb, ni);
254659aa14a9SRui Paulo }
254759aa14a9SRui Paulo 
254859aa14a9SRui Paulo /*
254959aa14a9SRui Paulo  * Same as above but backoffs timer statisically 50%.
255059aa14a9SRui Paulo  */
255159aa14a9SRui Paulo static void
255259aa14a9SRui Paulo mesh_peer_timeout_backoff(struct ieee80211_node *ni)
255359aa14a9SRui Paulo {
255459aa14a9SRui Paulo 	uint32_t r;
255559aa14a9SRui Paulo 
255659aa14a9SRui Paulo 	r = arc4random();
255759aa14a9SRui Paulo 	ni->ni_mltval += r % ni->ni_mltval;
255859aa14a9SRui Paulo 	callout_reset(&ni->ni_mltimer, ni->ni_mltval, mesh_peer_timeout_cb,
255959aa14a9SRui Paulo 	    ni);
256059aa14a9SRui Paulo }
256159aa14a9SRui Paulo 
256259aa14a9SRui Paulo static __inline void
256359aa14a9SRui Paulo mesh_peer_timeout_stop(struct ieee80211_node *ni)
256459aa14a9SRui Paulo {
2565c104cff2SRui Paulo 	callout_drain(&ni->ni_mltimer);
256659aa14a9SRui Paulo }
256759aa14a9SRui Paulo 
256859aa14a9SRui Paulo /*
256959aa14a9SRui Paulo  * Mesh Peer Link Management FSM timeout handling.
257059aa14a9SRui Paulo  */
257159aa14a9SRui Paulo static void
257259aa14a9SRui Paulo mesh_peer_timeout_cb(void *arg)
257359aa14a9SRui Paulo {
257459aa14a9SRui Paulo 	struct ieee80211_node *ni = (struct ieee80211_node *)arg;
257559aa14a9SRui Paulo 	uint16_t args[3];
257659aa14a9SRui Paulo 
257759aa14a9SRui Paulo 	IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_MESH,
257859aa14a9SRui Paulo 	    ni, "mesh link timeout, state %d, retry counter %d",
257959aa14a9SRui Paulo 	    ni->ni_mlstate, ni->ni_mlrcnt);
258059aa14a9SRui Paulo 
258159aa14a9SRui Paulo 	switch (ni->ni_mlstate) {
258259aa14a9SRui Paulo 	case IEEE80211_NODE_MESH_IDLE:
258359aa14a9SRui Paulo 	case IEEE80211_NODE_MESH_ESTABLISHED:
258459aa14a9SRui Paulo 		break;
258559aa14a9SRui Paulo 	case IEEE80211_NODE_MESH_OPENSNT:
258659aa14a9SRui Paulo 	case IEEE80211_NODE_MESH_OPENRCV:
258759aa14a9SRui Paulo 		if (ni->ni_mlrcnt == ieee80211_mesh_maxretries) {
258859aa14a9SRui Paulo 			args[0] = ni->ni_mlpid;
258959aa14a9SRui Paulo 			args[2] = IEEE80211_REASON_MESH_MAX_RETRIES;
259059aa14a9SRui Paulo 			ieee80211_send_action(ni,
2591ebeaa1adSMonthadar Al Jaberi 			    IEEE80211_ACTION_CAT_SELF_PROT,
259259aa14a9SRui Paulo 			    IEEE80211_ACTION_MESHPEERING_CLOSE, args);
259359aa14a9SRui Paulo 			ni->ni_mlrcnt = 0;
259459aa14a9SRui Paulo 			mesh_linkchange(ni, IEEE80211_NODE_MESH_HOLDING);
259559aa14a9SRui Paulo 			mesh_peer_timeout_setup(ni);
259659aa14a9SRui Paulo 		} else {
259759aa14a9SRui Paulo 			args[0] = ni->ni_mlpid;
259859aa14a9SRui Paulo 			ieee80211_send_action(ni,
2599ebeaa1adSMonthadar Al Jaberi 			    IEEE80211_ACTION_CAT_SELF_PROT,
260059aa14a9SRui Paulo 			    IEEE80211_ACTION_MESHPEERING_OPEN, args);
260159aa14a9SRui Paulo 			ni->ni_mlrcnt++;
260259aa14a9SRui Paulo 			mesh_peer_timeout_backoff(ni);
260359aa14a9SRui Paulo 		}
260459aa14a9SRui Paulo 		break;
260559aa14a9SRui Paulo 	case IEEE80211_NODE_MESH_CONFIRMRCV:
260659aa14a9SRui Paulo 		args[0] = ni->ni_mlpid;
260759aa14a9SRui Paulo 		args[2] = IEEE80211_REASON_MESH_CONFIRM_TIMEOUT;
260859aa14a9SRui Paulo 		ieee80211_send_action(ni,
2609ebeaa1adSMonthadar Al Jaberi 		    IEEE80211_ACTION_CAT_SELF_PROT,
261059aa14a9SRui Paulo 		    IEEE80211_ACTION_MESHPEERING_CLOSE, args);
261159aa14a9SRui Paulo 		mesh_linkchange(ni, IEEE80211_NODE_MESH_HOLDING);
261259aa14a9SRui Paulo 		mesh_peer_timeout_setup(ni);
261359aa14a9SRui Paulo 		break;
261459aa14a9SRui Paulo 	case IEEE80211_NODE_MESH_HOLDING:
261559aa14a9SRui Paulo 		mesh_linkchange(ni, IEEE80211_NODE_MESH_IDLE);
261659aa14a9SRui Paulo 		break;
261759aa14a9SRui Paulo 	}
261859aa14a9SRui Paulo }
261959aa14a9SRui Paulo 
2620aec0289dSRui Paulo static int
262159aa14a9SRui Paulo mesh_verify_meshid(struct ieee80211vap *vap, const uint8_t *ie)
262259aa14a9SRui Paulo {
262359aa14a9SRui Paulo 	struct ieee80211_mesh_state *ms = vap->iv_mesh;
262459aa14a9SRui Paulo 
262559aa14a9SRui Paulo 	if (ie == NULL || ie[1] != ms->ms_idlen)
262659aa14a9SRui Paulo 		return 1;
262759aa14a9SRui Paulo 	return memcmp(ms->ms_id, ie + 2, ms->ms_idlen);
262859aa14a9SRui Paulo }
262959aa14a9SRui Paulo 
263059aa14a9SRui Paulo /*
263159aa14a9SRui Paulo  * Check if we are using the same algorithms for this mesh.
263259aa14a9SRui Paulo  */
263359aa14a9SRui Paulo static int
263459aa14a9SRui Paulo mesh_verify_meshconf(struct ieee80211vap *vap, const uint8_t *ie)
263559aa14a9SRui Paulo {
263659aa14a9SRui Paulo 	const struct ieee80211_meshconf_ie *meshconf =
263759aa14a9SRui Paulo 	    (const struct ieee80211_meshconf_ie *) ie;
263859aa14a9SRui Paulo 	const struct ieee80211_mesh_state *ms = vap->iv_mesh;
263959aa14a9SRui Paulo 
264059aa14a9SRui Paulo 	if (meshconf == NULL)
264159aa14a9SRui Paulo 		return 1;
26426b8c1829SRui Paulo 	if (meshconf->conf_pselid != ms->ms_ppath->mpp_ie) {
264359aa14a9SRui Paulo 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_MESH,
26446b8c1829SRui Paulo 		    "unknown path selection algorithm: 0x%x\n",
26456b8c1829SRui Paulo 		    meshconf->conf_pselid);
264659aa14a9SRui Paulo 		return 1;
264759aa14a9SRui Paulo 	}
26486b8c1829SRui Paulo 	if (meshconf->conf_pmetid != ms->ms_pmetric->mpm_ie) {
264959aa14a9SRui Paulo 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_MESH,
26506b8c1829SRui Paulo 		    "unknown path metric algorithm: 0x%x\n",
26516b8c1829SRui Paulo 		    meshconf->conf_pmetid);
265259aa14a9SRui Paulo 		return 1;
265359aa14a9SRui Paulo 	}
26546b8c1829SRui Paulo 	if (meshconf->conf_ccid != 0) {
265559aa14a9SRui Paulo 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_MESH,
26566b8c1829SRui Paulo 		    "unknown congestion control algorithm: 0x%x\n",
26576b8c1829SRui Paulo 		    meshconf->conf_ccid);
265859aa14a9SRui Paulo 		return 1;
265959aa14a9SRui Paulo 	}
26606b8c1829SRui Paulo 	if (meshconf->conf_syncid != IEEE80211_MESHCONF_SYNC_NEIGHOFF) {
266159aa14a9SRui Paulo 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_MESH,
26626b8c1829SRui Paulo 		    "unknown sync algorithm: 0x%x\n",
26636b8c1829SRui Paulo 		    meshconf->conf_syncid);
266459aa14a9SRui Paulo 		return 1;
266559aa14a9SRui Paulo 	}
26666b8c1829SRui Paulo 	if (meshconf->conf_authid != 0) {
266759aa14a9SRui Paulo 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_MESH,
26686b8c1829SRui Paulo 		    "unknown auth auth algorithm: 0x%x\n",
26696b8c1829SRui Paulo 		    meshconf->conf_pselid);
267059aa14a9SRui Paulo 		return 1;
267159aa14a9SRui Paulo 	}
267259aa14a9SRui Paulo 	/* Not accepting peers */
267381b95c2cSAdrian Chadd 	if (!(meshconf->conf_cap & IEEE80211_MESHCONF_CAP_AP)) {
267459aa14a9SRui Paulo 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_MESH,
267559aa14a9SRui Paulo 		    "not accepting peers: 0x%x\n", meshconf->conf_cap);
267659aa14a9SRui Paulo 		return 1;
267759aa14a9SRui Paulo 	}
267859aa14a9SRui Paulo 	return 0;
267959aa14a9SRui Paulo }
268059aa14a9SRui Paulo 
268159aa14a9SRui Paulo static int
2682c77735e2SRui Paulo mesh_verify_meshpeer(struct ieee80211vap *vap, uint8_t subtype,
2683c77735e2SRui Paulo     const uint8_t *ie)
268459aa14a9SRui Paulo {
268559aa14a9SRui Paulo 	const struct ieee80211_meshpeer_ie *meshpeer =
268659aa14a9SRui Paulo 	    (const struct ieee80211_meshpeer_ie *) ie;
268759aa14a9SRui Paulo 
2688c2042c35SMonthadar Al Jaberi 	if (meshpeer == NULL ||
2689c2042c35SMonthadar Al Jaberi 	    meshpeer->peer_len < IEEE80211_MPM_BASE_SZ ||
2690c2042c35SMonthadar Al Jaberi 	    meshpeer->peer_len > IEEE80211_MPM_MAX_SZ)
269159aa14a9SRui Paulo 		return 1;
2692c2042c35SMonthadar Al Jaberi 	if (meshpeer->peer_proto != IEEE80211_MPPID_MPM) {
2693c2042c35SMonthadar Al Jaberi 		IEEE80211_DPRINTF(vap,
2694c2042c35SMonthadar Al Jaberi 		    IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH,
2695c2042c35SMonthadar Al Jaberi 		    "Only MPM protocol is supported (proto: 0x%02X)",
2696c2042c35SMonthadar Al Jaberi 		    meshpeer->peer_proto);
2697c2042c35SMonthadar Al Jaberi 		return 1;
2698c2042c35SMonthadar Al Jaberi 	}
2699c77735e2SRui Paulo 	switch (subtype) {
2700ebeaa1adSMonthadar Al Jaberi 	case IEEE80211_ACTION_MESHPEERING_OPEN:
2701c2042c35SMonthadar Al Jaberi 		if (meshpeer->peer_len != IEEE80211_MPM_BASE_SZ)
270259aa14a9SRui Paulo 			return 1;
270359aa14a9SRui Paulo 		break;
2704ebeaa1adSMonthadar Al Jaberi 	case IEEE80211_ACTION_MESHPEERING_CONFIRM:
2705c2042c35SMonthadar Al Jaberi 		if (meshpeer->peer_len != IEEE80211_MPM_BASE_SZ + 2)
270659aa14a9SRui Paulo 			return 1;
270759aa14a9SRui Paulo 		break;
2708ebeaa1adSMonthadar Al Jaberi 	case IEEE80211_ACTION_MESHPEERING_CLOSE:
2709c2042c35SMonthadar Al Jaberi 		if (meshpeer->peer_len < IEEE80211_MPM_BASE_SZ + 2)
271059aa14a9SRui Paulo 			return 1;
2711c2042c35SMonthadar Al Jaberi 		if (meshpeer->peer_len == (IEEE80211_MPM_BASE_SZ + 2) &&
2712c2042c35SMonthadar Al Jaberi 		    meshpeer->peer_linkid != 0)
271359aa14a9SRui Paulo 			return 1;
271459aa14a9SRui Paulo 		if (meshpeer->peer_rcode == 0)
271559aa14a9SRui Paulo 			return 1;
271659aa14a9SRui Paulo 		break;
271759aa14a9SRui Paulo 	}
271859aa14a9SRui Paulo 	return 0;
271959aa14a9SRui Paulo }
272059aa14a9SRui Paulo 
272159aa14a9SRui Paulo /*
272259aa14a9SRui Paulo  * Add a Mesh ID IE to a frame.
272359aa14a9SRui Paulo  */
272459aa14a9SRui Paulo uint8_t *
272559aa14a9SRui Paulo ieee80211_add_meshid(uint8_t *frm, struct ieee80211vap *vap)
272659aa14a9SRui Paulo {
272759aa14a9SRui Paulo 	struct ieee80211_mesh_state *ms = vap->iv_mesh;
272859aa14a9SRui Paulo 
272959aa14a9SRui Paulo 	KASSERT(vap->iv_opmode == IEEE80211_M_MBSS, ("not a mbss vap"));
273059aa14a9SRui Paulo 
273159aa14a9SRui Paulo 	*frm++ = IEEE80211_ELEMID_MESHID;
273259aa14a9SRui Paulo 	*frm++ = ms->ms_idlen;
273359aa14a9SRui Paulo 	memcpy(frm, ms->ms_id, ms->ms_idlen);
273459aa14a9SRui Paulo 	return frm + ms->ms_idlen;
273559aa14a9SRui Paulo }
273659aa14a9SRui Paulo 
273759aa14a9SRui Paulo /*
273859aa14a9SRui Paulo  * Add a Mesh Configuration IE to a frame.
273959aa14a9SRui Paulo  * For now just use HWMP routing, Airtime link metric, Null Congestion
274059aa14a9SRui Paulo  * Signaling, Null Sync Protocol and Null Authentication.
274159aa14a9SRui Paulo  */
274259aa14a9SRui Paulo uint8_t *
274359aa14a9SRui Paulo ieee80211_add_meshconf(uint8_t *frm, struct ieee80211vap *vap)
274459aa14a9SRui Paulo {
274559aa14a9SRui Paulo 	const struct ieee80211_mesh_state *ms = vap->iv_mesh;
2746651e41a4SRui Paulo 	uint16_t caps;
274759aa14a9SRui Paulo 
274859aa14a9SRui Paulo 	KASSERT(vap->iv_opmode == IEEE80211_M_MBSS, ("not a MBSS vap"));
274959aa14a9SRui Paulo 
275059aa14a9SRui Paulo 	*frm++ = IEEE80211_ELEMID_MESHCONF;
275181b95c2cSAdrian Chadd 	*frm++ = IEEE80211_MESH_CONF_SZ;
27526b8c1829SRui Paulo 	*frm++ = ms->ms_ppath->mpp_ie;		/* path selection */
27536b8c1829SRui Paulo 	*frm++ = ms->ms_pmetric->mpm_ie;	/* link metric */
27546b8c1829SRui Paulo 	*frm++ = IEEE80211_MESHCONF_CC_DISABLED;
27556b8c1829SRui Paulo 	*frm++ = IEEE80211_MESHCONF_SYNC_NEIGHOFF;
27566b8c1829SRui Paulo 	*frm++ = IEEE80211_MESHCONF_AUTH_DISABLED;
275759aa14a9SRui Paulo 	/* NB: set the number of neighbors before the rest */
27586eb9b443SMonthadar Al Jaberi 	*frm = (ms->ms_neighbors > IEEE80211_MESH_MAX_NEIGHBORS ?
27596eb9b443SMonthadar Al Jaberi 	    IEEE80211_MESH_MAX_NEIGHBORS : ms->ms_neighbors) << 1;
276059aa14a9SRui Paulo 	if (ms->ms_flags & IEEE80211_MESHFLAGS_PORTAL)
276159aa14a9SRui Paulo 		*frm |= IEEE80211_MESHCONF_FORM_MP;
276259aa14a9SRui Paulo 	frm += 1;
2763651e41a4SRui Paulo 	caps = 0;
276459aa14a9SRui Paulo 	if (ms->ms_flags & IEEE80211_MESHFLAGS_AP)
2765651e41a4SRui Paulo 		caps |= IEEE80211_MESHCONF_CAP_AP;
276659aa14a9SRui Paulo 	if (ms->ms_flags & IEEE80211_MESHFLAGS_FWD)
2767651e41a4SRui Paulo 		caps |= IEEE80211_MESHCONF_CAP_FWRD;
276881b95c2cSAdrian Chadd 	*frm++ = caps;
276959aa14a9SRui Paulo 	return frm;
277059aa14a9SRui Paulo }
277159aa14a9SRui Paulo 
277259aa14a9SRui Paulo /*
277359aa14a9SRui Paulo  * Add a Mesh Peer Management IE to a frame.
277459aa14a9SRui Paulo  */
277559aa14a9SRui Paulo uint8_t *
277659aa14a9SRui Paulo ieee80211_add_meshpeer(uint8_t *frm, uint8_t subtype, uint16_t localid,
277759aa14a9SRui Paulo     uint16_t peerid, uint16_t reason)
277859aa14a9SRui Paulo {
2779c77735e2SRui Paulo 
278059aa14a9SRui Paulo 	KASSERT(localid != 0, ("localid == 0"));
278159aa14a9SRui Paulo 
278259aa14a9SRui Paulo 	*frm++ = IEEE80211_ELEMID_MESHPEER;
278359aa14a9SRui Paulo 	switch (subtype) {
2784ebeaa1adSMonthadar Al Jaberi 	case IEEE80211_ACTION_MESHPEERING_OPEN:
2785c2042c35SMonthadar Al Jaberi 		*frm++ = IEEE80211_MPM_BASE_SZ;		/* length */
2786c2042c35SMonthadar Al Jaberi 		ADDSHORT(frm, IEEE80211_MPPID_MPM);	/* proto */
278759aa14a9SRui Paulo 		ADDSHORT(frm, localid);			/* local ID */
278859aa14a9SRui Paulo 		break;
2789ebeaa1adSMonthadar Al Jaberi 	case IEEE80211_ACTION_MESHPEERING_CONFIRM:
279059aa14a9SRui Paulo 		KASSERT(peerid != 0, ("sending peer confirm without peer id"));
2791c2042c35SMonthadar Al Jaberi 		*frm++ = IEEE80211_MPM_BASE_SZ + 2;	/* length */
2792c2042c35SMonthadar Al Jaberi 		ADDSHORT(frm, IEEE80211_MPPID_MPM);	/* proto */
279359aa14a9SRui Paulo 		ADDSHORT(frm, localid);			/* local ID */
279459aa14a9SRui Paulo 		ADDSHORT(frm, peerid);			/* peer ID */
279559aa14a9SRui Paulo 		break;
2796ebeaa1adSMonthadar Al Jaberi 	case IEEE80211_ACTION_MESHPEERING_CLOSE:
279759aa14a9SRui Paulo 		if (peerid)
2798c2042c35SMonthadar Al Jaberi 			*frm++ = IEEE80211_MPM_MAX_SZ;	/* length */
279959aa14a9SRui Paulo 		else
2800c2042c35SMonthadar Al Jaberi 			*frm++ = IEEE80211_MPM_BASE_SZ + 2; /* length */
2801c2042c35SMonthadar Al Jaberi 		ADDSHORT(frm, IEEE80211_MPPID_MPM);	/* proto */
280259aa14a9SRui Paulo 		ADDSHORT(frm, localid);	/* local ID */
280359aa14a9SRui Paulo 		if (peerid)
280459aa14a9SRui Paulo 			ADDSHORT(frm, peerid);	/* peer ID */
280559aa14a9SRui Paulo 		ADDSHORT(frm, reason);
280659aa14a9SRui Paulo 		break;
280759aa14a9SRui Paulo 	}
280859aa14a9SRui Paulo 	return frm;
280959aa14a9SRui Paulo }
281059aa14a9SRui Paulo 
281159aa14a9SRui Paulo /*
281259aa14a9SRui Paulo  * Compute an Airtime Link Metric for the link with this node.
281359aa14a9SRui Paulo  *
281459aa14a9SRui Paulo  * Based on Draft 3.0 spec (11B.10, p.149).
281559aa14a9SRui Paulo  */
281659aa14a9SRui Paulo /*
281759aa14a9SRui Paulo  * Max 802.11s overhead.
281859aa14a9SRui Paulo  */
281959aa14a9SRui Paulo #define IEEE80211_MESH_MAXOVERHEAD \
282059aa14a9SRui Paulo 	(sizeof(struct ieee80211_qosframe_addr4) \
28213c314f6dSMonthadar Al Jaberi 	 + sizeof(struct ieee80211_meshcntl_ae10) \
282259aa14a9SRui Paulo 	+ sizeof(struct llc) \
282359aa14a9SRui Paulo 	+ IEEE80211_ADDR_LEN \
282459aa14a9SRui Paulo 	+ IEEE80211_WEP_IVLEN \
282559aa14a9SRui Paulo 	+ IEEE80211_WEP_KIDLEN \
282659aa14a9SRui Paulo 	+ IEEE80211_WEP_CRCLEN \
282759aa14a9SRui Paulo 	+ IEEE80211_WEP_MICLEN \
282859aa14a9SRui Paulo 	+ IEEE80211_CRC_LEN)
282959aa14a9SRui Paulo uint32_t
283059aa14a9SRui Paulo mesh_airtime_calc(struct ieee80211_node *ni)
283159aa14a9SRui Paulo {
283259aa14a9SRui Paulo #define M_BITS 8
283359aa14a9SRui Paulo #define S_FACTOR (2 * M_BITS)
283459aa14a9SRui Paulo 	struct ieee80211com *ic = ni->ni_ic;
283559aa14a9SRui Paulo 	struct ifnet *ifp = ni->ni_vap->iv_ifp;
283659aa14a9SRui Paulo 	const static int nbits = 8192 << M_BITS;
283759aa14a9SRui Paulo 	uint32_t overhead, rate, errrate;
283859aa14a9SRui Paulo 	uint64_t res;
283959aa14a9SRui Paulo 
284059aa14a9SRui Paulo 	/* Time to transmit a frame */
284159aa14a9SRui Paulo 	rate = ni->ni_txrate;
284259aa14a9SRui Paulo 	overhead = ieee80211_compute_duration(ic->ic_rt,
284359aa14a9SRui Paulo 	    ifp->if_mtu + IEEE80211_MESH_MAXOVERHEAD, rate, 0) << M_BITS;
284459aa14a9SRui Paulo 	/* Error rate in percentage */
284559aa14a9SRui Paulo 	/* XXX assuming small failures are ok */
284659aa14a9SRui Paulo 	errrate = (((ifp->if_oerrors +
284759aa14a9SRui Paulo 	    ifp->if_ierrors) / 100) << M_BITS) / 100;
284859aa14a9SRui Paulo 	res = (overhead + (nbits / rate)) *
284959aa14a9SRui Paulo 	    ((1 << S_FACTOR) / ((1 << M_BITS) - errrate));
285059aa14a9SRui Paulo 
285159aa14a9SRui Paulo 	return (uint32_t)(res >> S_FACTOR);
285259aa14a9SRui Paulo #undef M_BITS
285359aa14a9SRui Paulo #undef S_FACTOR
285459aa14a9SRui Paulo }
285559aa14a9SRui Paulo 
285659aa14a9SRui Paulo /*
285759aa14a9SRui Paulo  * Add a Mesh Link Metric report IE to a frame.
285859aa14a9SRui Paulo  */
285959aa14a9SRui Paulo uint8_t *
2860bdd2a076SAdrian Chadd ieee80211_add_meshlmetric(uint8_t *frm, uint8_t flags, uint32_t metric)
286159aa14a9SRui Paulo {
286259aa14a9SRui Paulo 	*frm++ = IEEE80211_ELEMID_MESHLINK;
2863bdd2a076SAdrian Chadd 	*frm++ = 5;
2864bdd2a076SAdrian Chadd 	*frm++ = flags;
286559aa14a9SRui Paulo 	ADDWORD(frm, metric);
286659aa14a9SRui Paulo 	return frm;
286759aa14a9SRui Paulo }
286859aa14a9SRui Paulo #undef ADDSHORT
286959aa14a9SRui Paulo #undef ADDWORD
287059aa14a9SRui Paulo 
287159aa14a9SRui Paulo /*
287259aa14a9SRui Paulo  * Initialize any mesh-specific node state.
287359aa14a9SRui Paulo  */
287459aa14a9SRui Paulo void
287559aa14a9SRui Paulo ieee80211_mesh_node_init(struct ieee80211vap *vap, struct ieee80211_node *ni)
287659aa14a9SRui Paulo {
287759aa14a9SRui Paulo 	ni->ni_flags |= IEEE80211_NODE_QOS;
287859aa14a9SRui Paulo 	callout_init(&ni->ni_mltimer, CALLOUT_MPSAFE);
287959aa14a9SRui Paulo }
288059aa14a9SRui Paulo 
288159aa14a9SRui Paulo /*
288259aa14a9SRui Paulo  * Cleanup any mesh-specific node state.
288359aa14a9SRui Paulo  */
288459aa14a9SRui Paulo void
288559aa14a9SRui Paulo ieee80211_mesh_node_cleanup(struct ieee80211_node *ni)
288659aa14a9SRui Paulo {
288759aa14a9SRui Paulo 	struct ieee80211vap *vap = ni->ni_vap;
288859aa14a9SRui Paulo 	struct ieee80211_mesh_state *ms = vap->iv_mesh;
288959aa14a9SRui Paulo 
289059aa14a9SRui Paulo 	callout_drain(&ni->ni_mltimer);
289159aa14a9SRui Paulo 	/* NB: short-circuit callbacks after mesh_vdetach */
289259aa14a9SRui Paulo 	if (vap->iv_mesh != NULL)
289359aa14a9SRui Paulo 		ms->ms_ppath->mpp_peerdown(ni);
289459aa14a9SRui Paulo }
289559aa14a9SRui Paulo 
289659aa14a9SRui Paulo void
289759aa14a9SRui Paulo ieee80211_parse_meshid(struct ieee80211_node *ni, const uint8_t *ie)
289859aa14a9SRui Paulo {
289959aa14a9SRui Paulo 	ni->ni_meshidlen = ie[1];
290059aa14a9SRui Paulo 	memcpy(ni->ni_meshid, ie + 2, ie[1]);
290159aa14a9SRui Paulo }
290259aa14a9SRui Paulo 
290359aa14a9SRui Paulo /*
290459aa14a9SRui Paulo  * Setup mesh-specific node state on neighbor discovery.
290559aa14a9SRui Paulo  */
290659aa14a9SRui Paulo void
290759aa14a9SRui Paulo ieee80211_mesh_init_neighbor(struct ieee80211_node *ni,
290859aa14a9SRui Paulo 	const struct ieee80211_frame *wh,
290959aa14a9SRui Paulo 	const struct ieee80211_scanparams *sp)
291059aa14a9SRui Paulo {
291159aa14a9SRui Paulo 	ieee80211_parse_meshid(ni, sp->meshid);
291259aa14a9SRui Paulo }
291359aa14a9SRui Paulo 
2914d093681cSRui Paulo void
2915d093681cSRui Paulo ieee80211_mesh_update_beacon(struct ieee80211vap *vap,
2916d093681cSRui Paulo 	struct ieee80211_beacon_offsets *bo)
2917d093681cSRui Paulo {
2918d093681cSRui Paulo 	KASSERT(vap->iv_opmode == IEEE80211_M_MBSS, ("not a MBSS vap"));
2919d093681cSRui Paulo 
2920d093681cSRui Paulo 	if (isset(bo->bo_flags, IEEE80211_BEACON_MESHCONF)) {
2921d093681cSRui Paulo 		(void)ieee80211_add_meshconf(bo->bo_meshconf, vap);
2922d093681cSRui Paulo 		clrbit(bo->bo_flags, IEEE80211_BEACON_MESHCONF);
2923d093681cSRui Paulo 	}
2924d093681cSRui Paulo }
2925d093681cSRui Paulo 
292659aa14a9SRui Paulo static int
292759aa14a9SRui Paulo mesh_ioctl_get80211(struct ieee80211vap *vap, struct ieee80211req *ireq)
292859aa14a9SRui Paulo {
292959aa14a9SRui Paulo 	struct ieee80211_mesh_state *ms = vap->iv_mesh;
293059aa14a9SRui Paulo 	uint8_t tmpmeshid[IEEE80211_NWID_LEN];
293159aa14a9SRui Paulo 	struct ieee80211_mesh_route *rt;
293259aa14a9SRui Paulo 	struct ieee80211req_mesh_route *imr;
293359aa14a9SRui Paulo 	size_t len, off;
293459aa14a9SRui Paulo 	uint8_t *p;
293559aa14a9SRui Paulo 	int error;
293659aa14a9SRui Paulo 
293759aa14a9SRui Paulo 	if (vap->iv_opmode != IEEE80211_M_MBSS)
293859aa14a9SRui Paulo 		return ENOSYS;
293959aa14a9SRui Paulo 
294059aa14a9SRui Paulo 	error = 0;
294159aa14a9SRui Paulo 	switch (ireq->i_type) {
294259aa14a9SRui Paulo 	case IEEE80211_IOC_MESH_ID:
294359aa14a9SRui Paulo 		ireq->i_len = ms->ms_idlen;
294459aa14a9SRui Paulo 		memcpy(tmpmeshid, ms->ms_id, ireq->i_len);
294559aa14a9SRui Paulo 		error = copyout(tmpmeshid, ireq->i_data, ireq->i_len);
294659aa14a9SRui Paulo 		break;
294759aa14a9SRui Paulo 	case IEEE80211_IOC_MESH_AP:
294859aa14a9SRui Paulo 		ireq->i_val = (ms->ms_flags & IEEE80211_MESHFLAGS_AP) != 0;
294959aa14a9SRui Paulo 		break;
295059aa14a9SRui Paulo 	case IEEE80211_IOC_MESH_FWRD:
295159aa14a9SRui Paulo 		ireq->i_val = (ms->ms_flags & IEEE80211_MESHFLAGS_FWD) != 0;
295259aa14a9SRui Paulo 		break;
295359aa14a9SRui Paulo 	case IEEE80211_IOC_MESH_TTL:
295459aa14a9SRui Paulo 		ireq->i_val = ms->ms_ttl;
295559aa14a9SRui Paulo 		break;
295659aa14a9SRui Paulo 	case IEEE80211_IOC_MESH_RTCMD:
295759aa14a9SRui Paulo 		switch (ireq->i_val) {
295859aa14a9SRui Paulo 		case IEEE80211_MESH_RTCMD_LIST:
295959aa14a9SRui Paulo 			len = 0;
296059aa14a9SRui Paulo 			MESH_RT_LOCK(ms);
296159aa14a9SRui Paulo 			TAILQ_FOREACH(rt, &ms->ms_routes, rt_next) {
296259aa14a9SRui Paulo 				len += sizeof(*imr);
296359aa14a9SRui Paulo 			}
296459aa14a9SRui Paulo 			MESH_RT_UNLOCK(ms);
296559aa14a9SRui Paulo 			if (len > ireq->i_len || ireq->i_len < sizeof(*imr)) {
296659aa14a9SRui Paulo 				ireq->i_len = len;
296759aa14a9SRui Paulo 				return ENOMEM;
296859aa14a9SRui Paulo 			}
296959aa14a9SRui Paulo 			ireq->i_len = len;
2970c104cff2SRui Paulo 			/* XXX M_WAIT? */
297159aa14a9SRui Paulo 			p = malloc(len, M_TEMP, M_NOWAIT | M_ZERO);
297259aa14a9SRui Paulo 			if (p == NULL)
297359aa14a9SRui Paulo 				return ENOMEM;
297459aa14a9SRui Paulo 			off = 0;
297559aa14a9SRui Paulo 			MESH_RT_LOCK(ms);
297659aa14a9SRui Paulo 			TAILQ_FOREACH(rt, &ms->ms_routes, rt_next) {
297759aa14a9SRui Paulo 				if (off >= len)
297859aa14a9SRui Paulo 					break;
297959aa14a9SRui Paulo 				imr = (struct ieee80211req_mesh_route *)
298059aa14a9SRui Paulo 				    (p + off);
298159aa14a9SRui Paulo 				IEEE80211_ADDR_COPY(imr->imr_dest,
298259aa14a9SRui Paulo 				    rt->rt_dest);
298359aa14a9SRui Paulo 				IEEE80211_ADDR_COPY(imr->imr_nexthop,
298459aa14a9SRui Paulo 				    rt->rt_nexthop);
298559aa14a9SRui Paulo 				imr->imr_metric = rt->rt_metric;
298659aa14a9SRui Paulo 				imr->imr_nhops = rt->rt_nhops;
2987b5df85a6SMonthadar Al Jaberi 				imr->imr_lifetime =
2988b5df85a6SMonthadar Al Jaberi 				    ieee80211_mesh_rt_update(rt, 0);
2989c104cff2SRui Paulo 				imr->imr_lastmseq = rt->rt_lastmseq;
2990b5df85a6SMonthadar Al Jaberi 				imr->imr_flags = rt->rt_flags; /* last */
299159aa14a9SRui Paulo 				off += sizeof(*imr);
299259aa14a9SRui Paulo 			}
299359aa14a9SRui Paulo 			MESH_RT_UNLOCK(ms);
299459aa14a9SRui Paulo 			error = copyout(p, (uint8_t *)ireq->i_data,
299559aa14a9SRui Paulo 			    ireq->i_len);
299659aa14a9SRui Paulo 			free(p, M_TEMP);
299759aa14a9SRui Paulo 			break;
299859aa14a9SRui Paulo 		case IEEE80211_MESH_RTCMD_FLUSH:
299959aa14a9SRui Paulo 		case IEEE80211_MESH_RTCMD_ADD:
300059aa14a9SRui Paulo 		case IEEE80211_MESH_RTCMD_DELETE:
300159aa14a9SRui Paulo 			return EINVAL;
300259aa14a9SRui Paulo 		default:
300359aa14a9SRui Paulo 			return ENOSYS;
300459aa14a9SRui Paulo 		}
300559aa14a9SRui Paulo 		break;
300659aa14a9SRui Paulo 	case IEEE80211_IOC_MESH_PR_METRIC:
300759aa14a9SRui Paulo 		len = strlen(ms->ms_pmetric->mpm_descr);
300859aa14a9SRui Paulo 		if (ireq->i_len < len)
300959aa14a9SRui Paulo 			return EINVAL;
301059aa14a9SRui Paulo 		ireq->i_len = len;
301159aa14a9SRui Paulo 		error = copyout(ms->ms_pmetric->mpm_descr,
301259aa14a9SRui Paulo 		    (uint8_t *)ireq->i_data, len);
301359aa14a9SRui Paulo 		break;
301459aa14a9SRui Paulo 	case IEEE80211_IOC_MESH_PR_PATH:
301559aa14a9SRui Paulo 		len = strlen(ms->ms_ppath->mpp_descr);
301659aa14a9SRui Paulo 		if (ireq->i_len < len)
301759aa14a9SRui Paulo 			return EINVAL;
301859aa14a9SRui Paulo 		ireq->i_len = len;
301959aa14a9SRui Paulo 		error = copyout(ms->ms_ppath->mpp_descr,
302059aa14a9SRui Paulo 		    (uint8_t *)ireq->i_data, len);
302159aa14a9SRui Paulo 		break;
302259aa14a9SRui Paulo 	default:
302359aa14a9SRui Paulo 		return ENOSYS;
302459aa14a9SRui Paulo 	}
302559aa14a9SRui Paulo 
302659aa14a9SRui Paulo 	return error;
302759aa14a9SRui Paulo }
302859aa14a9SRui Paulo IEEE80211_IOCTL_GET(mesh, mesh_ioctl_get80211);
302959aa14a9SRui Paulo 
303059aa14a9SRui Paulo static int
303159aa14a9SRui Paulo mesh_ioctl_set80211(struct ieee80211vap *vap, struct ieee80211req *ireq)
303259aa14a9SRui Paulo {
303359aa14a9SRui Paulo 	struct ieee80211_mesh_state *ms = vap->iv_mesh;
303459aa14a9SRui Paulo 	uint8_t tmpmeshid[IEEE80211_NWID_LEN];
303559aa14a9SRui Paulo 	uint8_t tmpaddr[IEEE80211_ADDR_LEN];
303659aa14a9SRui Paulo 	char tmpproto[IEEE80211_MESH_PROTO_DSZ];
303759aa14a9SRui Paulo 	int error;
303859aa14a9SRui Paulo 
303959aa14a9SRui Paulo 	if (vap->iv_opmode != IEEE80211_M_MBSS)
304059aa14a9SRui Paulo 		return ENOSYS;
304159aa14a9SRui Paulo 
304259aa14a9SRui Paulo 	error = 0;
304359aa14a9SRui Paulo 	switch (ireq->i_type) {
304459aa14a9SRui Paulo 	case IEEE80211_IOC_MESH_ID:
304559aa14a9SRui Paulo 		if (ireq->i_val != 0 || ireq->i_len > IEEE80211_MESHID_LEN)
304659aa14a9SRui Paulo 			return EINVAL;
304759aa14a9SRui Paulo 		error = copyin(ireq->i_data, tmpmeshid, ireq->i_len);
3048c104cff2SRui Paulo 		if (error != 0)
304959aa14a9SRui Paulo 			break;
305059aa14a9SRui Paulo 		memset(ms->ms_id, 0, IEEE80211_NWID_LEN);
305159aa14a9SRui Paulo 		ms->ms_idlen = ireq->i_len;
305259aa14a9SRui Paulo 		memcpy(ms->ms_id, tmpmeshid, ireq->i_len);
3053c104cff2SRui Paulo 		error = ENETRESET;
305459aa14a9SRui Paulo 		break;
305559aa14a9SRui Paulo 	case IEEE80211_IOC_MESH_AP:
305659aa14a9SRui Paulo 		if (ireq->i_val)
305759aa14a9SRui Paulo 			ms->ms_flags |= IEEE80211_MESHFLAGS_AP;
305859aa14a9SRui Paulo 		else
305959aa14a9SRui Paulo 			ms->ms_flags &= ~IEEE80211_MESHFLAGS_AP;
3060c104cff2SRui Paulo 		error = ENETRESET;
306159aa14a9SRui Paulo 		break;
306259aa14a9SRui Paulo 	case IEEE80211_IOC_MESH_FWRD:
306359aa14a9SRui Paulo 		if (ireq->i_val)
306459aa14a9SRui Paulo 			ms->ms_flags |= IEEE80211_MESHFLAGS_FWD;
306559aa14a9SRui Paulo 		else
306659aa14a9SRui Paulo 			ms->ms_flags &= ~IEEE80211_MESHFLAGS_FWD;
306759aa14a9SRui Paulo 		break;
306859aa14a9SRui Paulo 	case IEEE80211_IOC_MESH_TTL:
306959aa14a9SRui Paulo 		ms->ms_ttl = (uint8_t) ireq->i_val;
307059aa14a9SRui Paulo 		break;
307159aa14a9SRui Paulo 	case IEEE80211_IOC_MESH_RTCMD:
307259aa14a9SRui Paulo 		switch (ireq->i_val) {
307359aa14a9SRui Paulo 		case IEEE80211_MESH_RTCMD_LIST:
307459aa14a9SRui Paulo 			return EINVAL;
307559aa14a9SRui Paulo 		case IEEE80211_MESH_RTCMD_FLUSH:
307659aa14a9SRui Paulo 			ieee80211_mesh_rt_flush(vap);
307759aa14a9SRui Paulo 			break;
307859aa14a9SRui Paulo 		case IEEE80211_MESH_RTCMD_ADD:
307959aa14a9SRui Paulo 			if (IEEE80211_ADDR_EQ(vap->iv_myaddr, ireq->i_data) ||
308059aa14a9SRui Paulo 			    IEEE80211_ADDR_EQ(broadcastaddr, ireq->i_data))
308159aa14a9SRui Paulo 				return EINVAL;
308259aa14a9SRui Paulo 			error = copyin(ireq->i_data, &tmpaddr,
308359aa14a9SRui Paulo 			    IEEE80211_ADDR_LEN);
3084c104cff2SRui Paulo 			if (error == 0)
308559aa14a9SRui Paulo 				ieee80211_mesh_discover(vap, tmpaddr, NULL);
308659aa14a9SRui Paulo 			break;
308759aa14a9SRui Paulo 		case IEEE80211_MESH_RTCMD_DELETE:
308859aa14a9SRui Paulo 			ieee80211_mesh_rt_del(vap, ireq->i_data);
308959aa14a9SRui Paulo 			break;
309059aa14a9SRui Paulo 		default:
309159aa14a9SRui Paulo 			return ENOSYS;
309259aa14a9SRui Paulo 		}
309359aa14a9SRui Paulo 		break;
309459aa14a9SRui Paulo 	case IEEE80211_IOC_MESH_PR_METRIC:
309559aa14a9SRui Paulo 		error = copyin(ireq->i_data, tmpproto, sizeof(tmpproto));
3096c104cff2SRui Paulo 		if (error == 0) {
3097c104cff2SRui Paulo 			error = mesh_select_proto_metric(vap, tmpproto);
3098c104cff2SRui Paulo 			if (error == 0)
3099c104cff2SRui Paulo 				error = ENETRESET;
3100c104cff2SRui Paulo 		}
310159aa14a9SRui Paulo 		break;
310259aa14a9SRui Paulo 	case IEEE80211_IOC_MESH_PR_PATH:
310359aa14a9SRui Paulo 		error = copyin(ireq->i_data, tmpproto, sizeof(tmpproto));
3104c104cff2SRui Paulo 		if (error == 0) {
3105c104cff2SRui Paulo 			error = mesh_select_proto_path(vap, tmpproto);
3106c104cff2SRui Paulo 			if (error == 0)
3107c104cff2SRui Paulo 				error = ENETRESET;
3108c104cff2SRui Paulo 		}
310959aa14a9SRui Paulo 		break;
311059aa14a9SRui Paulo 	default:
311159aa14a9SRui Paulo 		return ENOSYS;
311259aa14a9SRui Paulo 	}
311359aa14a9SRui Paulo 	return error;
311459aa14a9SRui Paulo }
311559aa14a9SRui Paulo IEEE80211_IOCTL_SET(mesh, mesh_ioctl_set80211);
3116