xref: /freebsd/sys/net80211/ieee80211_mesh.c (revision 227da7d10ba5626aabd240351bb09d59263f0812)
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)");
114ff17492fSMonthadar Al Jaberi static int ieee80211_mesh_backofftimeout = -1;
115ff17492fSMonthadar Al Jaberi SYSCTL_PROC(_net_wlan_mesh, OID_AUTO, backofftimeout, CTLTYPE_INT | CTLFLAG_RW,
116ff17492fSMonthadar Al Jaberi     &ieee80211_mesh_backofftimeout, 0, ieee80211_sysctl_msecs_ticks, "I",
117ff17492fSMonthadar Al Jaberi     "Backoff timeout (msec). This is to throutles peering forever when "
118ff17492fSMonthadar Al Jaberi     "not receving answer or is rejected by a neighbor");
11959aa14a9SRui Paulo static int ieee80211_mesh_maxretries = 2;
12059aa14a9SRui Paulo SYSCTL_INT(_net_wlan_mesh, OID_AUTO, maxretries, CTLTYPE_INT | CTLFLAG_RW,
12159aa14a9SRui Paulo     &ieee80211_mesh_maxretries, 0,
12259aa14a9SRui Paulo     "Maximum retries during peer link establishment");
123ff17492fSMonthadar Al Jaberi static int ieee80211_mesh_maxholding = 2;
124ff17492fSMonthadar Al Jaberi SYSCTL_INT(_net_wlan_mesh, OID_AUTO, maxholding, CTLTYPE_INT | CTLFLAG_RW,
125ff17492fSMonthadar Al Jaberi     &ieee80211_mesh_maxholding, 0,
126ff17492fSMonthadar Al Jaberi     "Maximum times we are allowed to transition to HOLDING state before "
127ff17492fSMonthadar Al Jaberi     "backinoff during peer link establishment");
12859aa14a9SRui Paulo 
12959aa14a9SRui Paulo static const uint8_t broadcastaddr[IEEE80211_ADDR_LEN] =
13059aa14a9SRui Paulo 	{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
13159aa14a9SRui Paulo 
13259aa14a9SRui Paulo static	ieee80211_recv_action_func mesh_recv_action_meshpeering_open;
13359aa14a9SRui Paulo static	ieee80211_recv_action_func mesh_recv_action_meshpeering_confirm;
13459aa14a9SRui Paulo static	ieee80211_recv_action_func mesh_recv_action_meshpeering_close;
135bdd2a076SAdrian Chadd static	ieee80211_recv_action_func mesh_recv_action_meshlmetric;
13659aa14a9SRui Paulo 
13759aa14a9SRui Paulo static	ieee80211_send_action_func mesh_send_action_meshpeering_open;
13859aa14a9SRui Paulo static	ieee80211_send_action_func mesh_send_action_meshpeering_confirm;
13959aa14a9SRui Paulo static	ieee80211_send_action_func mesh_send_action_meshpeering_close;
140bdd2a076SAdrian Chadd static	ieee80211_send_action_func mesh_send_action_meshlmetric;
14159aa14a9SRui Paulo 
14259aa14a9SRui Paulo static const struct ieee80211_mesh_proto_metric mesh_metric_airtime = {
14359aa14a9SRui Paulo 	.mpm_descr	= "AIRTIME",
1446b8c1829SRui Paulo 	.mpm_ie		= IEEE80211_MESHCONF_METRIC_AIRTIME,
14559aa14a9SRui Paulo 	.mpm_metric	= mesh_airtime_calc,
14659aa14a9SRui Paulo };
14759aa14a9SRui Paulo 
14859aa14a9SRui Paulo static struct ieee80211_mesh_proto_path		mesh_proto_paths[4];
14959aa14a9SRui Paulo static struct ieee80211_mesh_proto_metric	mesh_proto_metrics[4];
15059aa14a9SRui Paulo 
151b5df85a6SMonthadar Al Jaberi #define	RT_ENTRY_LOCK(rt)	mtx_lock(&(rt)->rt_lock)
152b5df85a6SMonthadar Al Jaberi #define	RT_ENTRY_LOCK_ASSERT(rt) mtx_assert(&(rt)->rt_lock, MA_OWNED)
153b5df85a6SMonthadar Al Jaberi #define	RT_ENTRY_UNLOCK(rt)	mtx_unlock(&(rt)->rt_lock)
154b5df85a6SMonthadar Al Jaberi 
15559aa14a9SRui Paulo #define	MESH_RT_LOCK(ms)	mtx_lock(&(ms)->ms_rt_lock)
156c104cff2SRui Paulo #define	MESH_RT_LOCK_ASSERT(ms)	mtx_assert(&(ms)->ms_rt_lock, MA_OWNED)
15759aa14a9SRui Paulo #define	MESH_RT_UNLOCK(ms)	mtx_unlock(&(ms)->ms_rt_lock)
15859aa14a9SRui Paulo 
1591f88a92bSAdrian Chadd MALLOC_DEFINE(M_80211_MESH_PREQ, "80211preq", "802.11 MESH Path Request frame");
1601f88a92bSAdrian Chadd MALLOC_DEFINE(M_80211_MESH_PREP, "80211prep", "802.11 MESH Path Reply frame");
1611f88a92bSAdrian Chadd MALLOC_DEFINE(M_80211_MESH_PERR, "80211perr", "802.11 MESH Path Error frame");
1621f88a92bSAdrian Chadd 
163b5df85a6SMonthadar Al Jaberi /* The longer one of the lifetime should be stored as new lifetime */
164b5df85a6SMonthadar Al Jaberi #define MESH_ROUTE_LIFETIME_MAX(a, b)	(a > b ? a : b)
165b5df85a6SMonthadar Al Jaberi 
16659aa14a9SRui Paulo MALLOC_DEFINE(M_80211_MESH_RT, "80211mesh", "802.11s routing table");
16759aa14a9SRui Paulo 
16859aa14a9SRui Paulo /*
16959aa14a9SRui Paulo  * Helper functions to manipulate the Mesh routing table.
17059aa14a9SRui Paulo  */
171c104cff2SRui Paulo 
172c104cff2SRui Paulo static struct ieee80211_mesh_route *
173c104cff2SRui Paulo mesh_rt_find_locked(struct ieee80211_mesh_state *ms,
174c104cff2SRui Paulo     const uint8_t dest[IEEE80211_ADDR_LEN])
175c104cff2SRui Paulo {
176c104cff2SRui Paulo 	struct ieee80211_mesh_route *rt;
177c104cff2SRui Paulo 
178c104cff2SRui Paulo 	MESH_RT_LOCK_ASSERT(ms);
179c104cff2SRui Paulo 
180c104cff2SRui Paulo 	TAILQ_FOREACH(rt, &ms->ms_routes, rt_next) {
181c104cff2SRui Paulo 		if (IEEE80211_ADDR_EQ(dest, rt->rt_dest))
182c104cff2SRui Paulo 			return rt;
183c104cff2SRui Paulo 	}
184c104cff2SRui Paulo 	return NULL;
185c104cff2SRui Paulo }
186c104cff2SRui Paulo 
187c104cff2SRui Paulo static struct ieee80211_mesh_route *
18847451c66SMonthadar Al Jaberi mesh_rt_add_locked(struct ieee80211vap *vap,
189c104cff2SRui Paulo     const uint8_t dest[IEEE80211_ADDR_LEN])
190c104cff2SRui Paulo {
19147451c66SMonthadar Al Jaberi 	struct ieee80211_mesh_state *ms = vap->iv_mesh;
192c104cff2SRui Paulo 	struct ieee80211_mesh_route *rt;
193c104cff2SRui Paulo 
194c104cff2SRui Paulo 	KASSERT(!IEEE80211_ADDR_EQ(broadcastaddr, dest),
195c104cff2SRui Paulo 	    ("%s: adding broadcast to the routing table", __func__));
196c104cff2SRui Paulo 
197c104cff2SRui Paulo 	MESH_RT_LOCK_ASSERT(ms);
198c104cff2SRui Paulo 
199c104cff2SRui Paulo 	rt = malloc(ALIGN(sizeof(struct ieee80211_mesh_route)) +
200c104cff2SRui Paulo 	    ms->ms_ppath->mpp_privlen, M_80211_MESH_RT, M_NOWAIT | M_ZERO);
201c104cff2SRui Paulo 	if (rt != NULL) {
20247451c66SMonthadar Al Jaberi 		rt->rt_vap = vap;
203c104cff2SRui Paulo 		IEEE80211_ADDR_COPY(rt->rt_dest, dest);
204c104cff2SRui Paulo 		rt->rt_priv = (void *)ALIGN(&rt[1]);
205b5df85a6SMonthadar Al Jaberi 		mtx_init(&rt->rt_lock, "MBSS_RT", "802.11s route entry", MTX_DEF);
20647451c66SMonthadar Al Jaberi 		callout_init(&rt->rt_discovery, CALLOUT_MPSAFE);
207b5df85a6SMonthadar Al Jaberi 		rt->rt_updtime = ticks;	/* create time */
208c104cff2SRui Paulo 		TAILQ_INSERT_TAIL(&ms->ms_routes, rt, rt_next);
209c104cff2SRui Paulo 	}
210c104cff2SRui Paulo 	return rt;
211c104cff2SRui Paulo }
212c104cff2SRui Paulo 
21359aa14a9SRui Paulo struct ieee80211_mesh_route *
21459aa14a9SRui Paulo ieee80211_mesh_rt_find(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 	MESH_RT_LOCK(ms);
221c104cff2SRui Paulo 	rt = mesh_rt_find_locked(ms, dest);
22259aa14a9SRui Paulo 	MESH_RT_UNLOCK(ms);
22359aa14a9SRui Paulo 	return rt;
22459aa14a9SRui Paulo }
22559aa14a9SRui Paulo 
22659aa14a9SRui Paulo struct ieee80211_mesh_route *
22759aa14a9SRui Paulo ieee80211_mesh_rt_add(struct ieee80211vap *vap,
22859aa14a9SRui Paulo     const uint8_t dest[IEEE80211_ADDR_LEN])
22959aa14a9SRui Paulo {
23059aa14a9SRui Paulo 	struct ieee80211_mesh_state *ms = vap->iv_mesh;
23159aa14a9SRui Paulo 	struct ieee80211_mesh_route *rt;
23259aa14a9SRui Paulo 
23359aa14a9SRui Paulo 	KASSERT(ieee80211_mesh_rt_find(vap, dest) == NULL,
23459aa14a9SRui Paulo 	    ("%s: duplicate entry in the routing table", __func__));
23559aa14a9SRui Paulo 	KASSERT(!IEEE80211_ADDR_EQ(vap->iv_myaddr, dest),
23659aa14a9SRui Paulo 	    ("%s: adding self to the routing table", __func__));
23759aa14a9SRui Paulo 
23859aa14a9SRui Paulo 	MESH_RT_LOCK(ms);
23947451c66SMonthadar Al Jaberi 	rt = mesh_rt_add_locked(vap, dest);
24059aa14a9SRui Paulo 	MESH_RT_UNLOCK(ms);
24159aa14a9SRui Paulo 	return rt;
24259aa14a9SRui Paulo }
24359aa14a9SRui Paulo 
244c104cff2SRui Paulo /*
245b5df85a6SMonthadar Al Jaberi  * Update the route lifetime and returns the updated lifetime.
246b5df85a6SMonthadar Al Jaberi  * If new_lifetime is zero and route is timedout it will be invalidated.
247b5df85a6SMonthadar Al Jaberi  * new_lifetime is in msec
248b5df85a6SMonthadar Al Jaberi  */
249b5df85a6SMonthadar Al Jaberi int
250b5df85a6SMonthadar Al Jaberi ieee80211_mesh_rt_update(struct ieee80211_mesh_route *rt, int new_lifetime)
251b5df85a6SMonthadar Al Jaberi {
252b5df85a6SMonthadar Al Jaberi 	int timesince, now;
253b5df85a6SMonthadar Al Jaberi 	uint32_t lifetime = 0;
254b5df85a6SMonthadar Al Jaberi 
255644ccee4SMonthadar Al Jaberi 	KASSERT(rt != NULL, ("route is NULL"));
256644ccee4SMonthadar Al Jaberi 
257b5df85a6SMonthadar Al Jaberi 	now = ticks;
258b5df85a6SMonthadar Al Jaberi 	RT_ENTRY_LOCK(rt);
2594f3a27aeSMonthadar Al Jaberi 
2604f3a27aeSMonthadar Al Jaberi 	/* dont clobber a proxy entry gated by us */
2614f3a27aeSMonthadar Al Jaberi 	if (rt->rt_flags & IEEE80211_MESHRT_FLAGS_PROXY && rt->rt_nhops == 0) {
2624f3a27aeSMonthadar Al Jaberi 		RT_ENTRY_UNLOCK(rt);
2634f3a27aeSMonthadar Al Jaberi 		return rt->rt_lifetime;
2644f3a27aeSMonthadar Al Jaberi 	}
2654f3a27aeSMonthadar Al Jaberi 
266b5df85a6SMonthadar Al Jaberi 	timesince = ticks_to_msecs(now - rt->rt_updtime);
267b5df85a6SMonthadar Al Jaberi 	rt->rt_updtime = now;
268b5df85a6SMonthadar Al Jaberi 	if (timesince >= rt->rt_lifetime) {
269b5df85a6SMonthadar Al Jaberi 		if (new_lifetime != 0) {
270b5df85a6SMonthadar Al Jaberi 			rt->rt_lifetime = new_lifetime;
271b5df85a6SMonthadar Al Jaberi 		}
272b5df85a6SMonthadar Al Jaberi 		else {
273b5df85a6SMonthadar Al Jaberi 			rt->rt_flags &= ~IEEE80211_MESHRT_FLAGS_VALID;
274b5df85a6SMonthadar Al Jaberi 			rt->rt_lifetime = 0;
275b5df85a6SMonthadar Al Jaberi 		}
276b5df85a6SMonthadar Al Jaberi 	} else {
277b5df85a6SMonthadar Al Jaberi 		/* update what is left of lifetime */
278b5df85a6SMonthadar Al Jaberi 		rt->rt_lifetime = rt->rt_lifetime - timesince;
279b5df85a6SMonthadar Al Jaberi 		rt->rt_lifetime  = MESH_ROUTE_LIFETIME_MAX(
280b5df85a6SMonthadar Al Jaberi 			new_lifetime, rt->rt_lifetime);
281b5df85a6SMonthadar Al Jaberi 	}
282b5df85a6SMonthadar Al Jaberi 	lifetime = rt->rt_lifetime;
283b5df85a6SMonthadar Al Jaberi 	RT_ENTRY_UNLOCK(rt);
284b5df85a6SMonthadar Al Jaberi 
285b5df85a6SMonthadar Al Jaberi 	return lifetime;
286b5df85a6SMonthadar Al Jaberi }
287b5df85a6SMonthadar Al Jaberi 
288b5df85a6SMonthadar Al Jaberi /*
289c104cff2SRui Paulo  * Add a proxy route (as needed) for the specified destination.
290c104cff2SRui Paulo  */
291c104cff2SRui Paulo void
292c104cff2SRui Paulo ieee80211_mesh_proxy_check(struct ieee80211vap *vap,
293c104cff2SRui Paulo     const uint8_t dest[IEEE80211_ADDR_LEN])
294c104cff2SRui Paulo {
295c104cff2SRui Paulo 	struct ieee80211_mesh_state *ms = vap->iv_mesh;
296c104cff2SRui Paulo 	struct ieee80211_mesh_route *rt;
297c104cff2SRui Paulo 
298c104cff2SRui Paulo 	MESH_RT_LOCK(ms);
299c104cff2SRui Paulo 	rt = mesh_rt_find_locked(ms, dest);
300c104cff2SRui Paulo 	if (rt == NULL) {
30147451c66SMonthadar Al Jaberi 		rt = mesh_rt_add_locked(vap, dest);
302c104cff2SRui Paulo 		if (rt == NULL) {
303c104cff2SRui Paulo 			IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH, dest,
304c104cff2SRui Paulo 			    "%s", "unable to add proxy entry");
305c104cff2SRui Paulo 			vap->iv_stats.is_mesh_rtaddfailed++;
306c104cff2SRui Paulo 		} else {
3073ca80f0dSRui Paulo 			IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH, dest,
3083ca80f0dSRui Paulo 			    "%s", "add proxy entry");
3093c314f6dSMonthadar Al Jaberi 			IEEE80211_ADDR_COPY(rt->rt_mesh_gate, vap->iv_myaddr);
310c104cff2SRui Paulo 			IEEE80211_ADDR_COPY(rt->rt_nexthop, vap->iv_myaddr);
311c104cff2SRui Paulo 			rt->rt_flags |= IEEE80211_MESHRT_FLAGS_VALID
312c104cff2SRui Paulo 				     |  IEEE80211_MESHRT_FLAGS_PROXY;
313c104cff2SRui Paulo 		}
314c104cff2SRui Paulo 	} else if ((rt->rt_flags & IEEE80211_MESHRT_FLAGS_VALID) == 0) {
3153c314f6dSMonthadar Al Jaberi 		KASSERT(rt->rt_flags & IEEE80211_MESHRT_FLAGS_PROXY,
3163c314f6dSMonthadar Al Jaberi 		    ("no proxy flag for poxy entry"));
317c104cff2SRui Paulo 		struct ieee80211com *ic = vap->iv_ic;
318c104cff2SRui Paulo 		/*
319c104cff2SRui Paulo 		 * Fix existing entry created by received frames from
320c104cff2SRui Paulo 		 * stations that have some memory of dest.  We also
321c104cff2SRui Paulo 		 * flush any frames held on the staging queue; delivering
322c104cff2SRui Paulo 		 * them is too much trouble right now.
323c104cff2SRui Paulo 		 */
324c104cff2SRui Paulo 		IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH, dest,
325c104cff2SRui Paulo 		    "%s", "fix proxy entry");
326c104cff2SRui Paulo 		IEEE80211_ADDR_COPY(rt->rt_nexthop, vap->iv_myaddr);
327c104cff2SRui Paulo 		rt->rt_flags |= IEEE80211_MESHRT_FLAGS_VALID
328c104cff2SRui Paulo 			     |  IEEE80211_MESHRT_FLAGS_PROXY;
329c104cff2SRui Paulo 		/* XXX belongs in hwmp */
330c104cff2SRui Paulo 		ieee80211_ageq_drain_node(&ic->ic_stageq,
331c104cff2SRui Paulo 		   (void *)(uintptr_t) ieee80211_mac_hash(ic, dest));
332c104cff2SRui Paulo 		/* XXX stat? */
333c104cff2SRui Paulo 	}
334c104cff2SRui Paulo 	MESH_RT_UNLOCK(ms);
335c104cff2SRui Paulo }
336c104cff2SRui Paulo 
337c104cff2SRui Paulo static __inline void
338c104cff2SRui Paulo mesh_rt_del(struct ieee80211_mesh_state *ms, struct ieee80211_mesh_route *rt)
339c104cff2SRui Paulo {
340c104cff2SRui Paulo 	TAILQ_REMOVE(&ms->ms_routes, rt, rt_next);
341b5df85a6SMonthadar Al Jaberi 	/*
342b5df85a6SMonthadar Al Jaberi 	 * Grab the lock before destroying it, to be sure no one else
343b5df85a6SMonthadar Al Jaberi 	 * is holding the route.
344b5df85a6SMonthadar Al Jaberi 	 */
345b5df85a6SMonthadar Al Jaberi 	RT_ENTRY_LOCK(rt);
34647451c66SMonthadar Al Jaberi 	callout_drain(&rt->rt_discovery);
347b5df85a6SMonthadar Al Jaberi 	mtx_destroy(&rt->rt_lock);
348c104cff2SRui Paulo 	free(rt, M_80211_MESH_RT);
349c104cff2SRui Paulo }
350c104cff2SRui Paulo 
35159aa14a9SRui Paulo void
35259aa14a9SRui Paulo ieee80211_mesh_rt_del(struct ieee80211vap *vap,
35359aa14a9SRui Paulo     const uint8_t dest[IEEE80211_ADDR_LEN])
35459aa14a9SRui Paulo {
35559aa14a9SRui Paulo 	struct ieee80211_mesh_state *ms = vap->iv_mesh;
35659aa14a9SRui Paulo 	struct ieee80211_mesh_route *rt, *next;
35759aa14a9SRui Paulo 
35859aa14a9SRui Paulo 	MESH_RT_LOCK(ms);
35959aa14a9SRui Paulo 	TAILQ_FOREACH_SAFE(rt, &ms->ms_routes, rt_next, next) {
36059aa14a9SRui Paulo 		if (IEEE80211_ADDR_EQ(rt->rt_dest, dest)) {
3613db82516SMonthadar Al Jaberi 			if (rt->rt_flags & IEEE80211_MESHRT_FLAGS_PROXY) {
3623db82516SMonthadar Al Jaberi 				ms->ms_ppath->mpp_senderror(vap, dest, rt,
3633db82516SMonthadar Al Jaberi 				    IEEE80211_REASON_MESH_PERR_NO_PROXY);
3643db82516SMonthadar Al Jaberi 			} else {
3653db82516SMonthadar Al Jaberi 				ms->ms_ppath->mpp_senderror(vap, dest, rt,
3663db82516SMonthadar Al Jaberi 				    IEEE80211_REASON_MESH_PERR_DEST_UNREACH);
3673db82516SMonthadar Al Jaberi 			}
368c104cff2SRui Paulo 			mesh_rt_del(ms, rt);
36959aa14a9SRui Paulo 			MESH_RT_UNLOCK(ms);
37059aa14a9SRui Paulo 			return;
37159aa14a9SRui Paulo 		}
37259aa14a9SRui Paulo 	}
37359aa14a9SRui Paulo 	MESH_RT_UNLOCK(ms);
37459aa14a9SRui Paulo }
37559aa14a9SRui Paulo 
37659aa14a9SRui Paulo void
37759aa14a9SRui Paulo ieee80211_mesh_rt_flush(struct ieee80211vap *vap)
37859aa14a9SRui Paulo {
37959aa14a9SRui Paulo 	struct ieee80211_mesh_state *ms = vap->iv_mesh;
38059aa14a9SRui Paulo 	struct ieee80211_mesh_route *rt, *next;
38159aa14a9SRui Paulo 
38259aa14a9SRui Paulo 	if (ms == NULL)
38359aa14a9SRui Paulo 		return;
38459aa14a9SRui Paulo 	MESH_RT_LOCK(ms);
385c104cff2SRui Paulo 	TAILQ_FOREACH_SAFE(rt, &ms->ms_routes, rt_next, next)
386c104cff2SRui Paulo 		mesh_rt_del(ms, rt);
387c104cff2SRui Paulo 	MESH_RT_UNLOCK(ms);
388c104cff2SRui Paulo }
389c104cff2SRui Paulo 
3903ca80f0dSRui Paulo void
3913ca80f0dSRui Paulo ieee80211_mesh_rt_flush_peer(struct ieee80211vap *vap,
3923ca80f0dSRui Paulo     const uint8_t peer[IEEE80211_ADDR_LEN])
3933ca80f0dSRui Paulo {
3943ca80f0dSRui Paulo 	struct ieee80211_mesh_state *ms = vap->iv_mesh;
3953ca80f0dSRui Paulo 	struct ieee80211_mesh_route *rt, *next;
3963ca80f0dSRui Paulo 
3973ca80f0dSRui Paulo 	MESH_RT_LOCK(ms);
3983ca80f0dSRui Paulo 	TAILQ_FOREACH_SAFE(rt, &ms->ms_routes, rt_next, next) {
3993ca80f0dSRui Paulo 		if (IEEE80211_ADDR_EQ(rt->rt_nexthop, peer))
4003ca80f0dSRui Paulo 			mesh_rt_del(ms, rt);
4013ca80f0dSRui Paulo 	}
4023ca80f0dSRui Paulo 	MESH_RT_UNLOCK(ms);
4033ca80f0dSRui Paulo }
4043ca80f0dSRui Paulo 
405c104cff2SRui Paulo /*
406c104cff2SRui Paulo  * Flush expired routing entries, i.e. those in invalid state for
407c104cff2SRui Paulo  * some time.
408c104cff2SRui Paulo  */
409c104cff2SRui Paulo static void
410c104cff2SRui Paulo mesh_rt_flush_invalid(struct ieee80211vap *vap)
411c104cff2SRui Paulo {
412c104cff2SRui Paulo 	struct ieee80211_mesh_state *ms = vap->iv_mesh;
413c104cff2SRui Paulo 	struct ieee80211_mesh_route *rt, *next;
414c104cff2SRui Paulo 
415c104cff2SRui Paulo 	if (ms == NULL)
416c104cff2SRui Paulo 		return;
417c104cff2SRui Paulo 	MESH_RT_LOCK(ms);
41859aa14a9SRui Paulo 	TAILQ_FOREACH_SAFE(rt, &ms->ms_routes, rt_next, next) {
41947451c66SMonthadar Al Jaberi 		/* Discover paths will be deleted by their own callout */
42047451c66SMonthadar Al Jaberi 		if (rt->rt_flags & IEEE80211_MESHRT_FLAGS_DISCOVER)
42147451c66SMonthadar Al Jaberi 			continue;
422b5df85a6SMonthadar Al Jaberi 		ieee80211_mesh_rt_update(rt, 0);
42347451c66SMonthadar Al Jaberi 		if ((rt->rt_flags & IEEE80211_MESHRT_FLAGS_VALID) == 0)
424c104cff2SRui Paulo 			mesh_rt_del(ms, rt);
42559aa14a9SRui Paulo 	}
42659aa14a9SRui Paulo 	MESH_RT_UNLOCK(ms);
42759aa14a9SRui Paulo }
42859aa14a9SRui Paulo 
42959aa14a9SRui Paulo #define	N(a)	(sizeof(a) / sizeof(a[0]))
43059aa14a9SRui Paulo int
43159aa14a9SRui Paulo ieee80211_mesh_register_proto_path(const struct ieee80211_mesh_proto_path *mpp)
43259aa14a9SRui Paulo {
43359aa14a9SRui Paulo 	int i, firstempty = -1;
43459aa14a9SRui Paulo 
43559aa14a9SRui Paulo 	for (i = 0; i < N(mesh_proto_paths); i++) {
4366b8c1829SRui Paulo 		if (strncmp(mpp->mpp_descr, mesh_proto_paths[i].mpp_descr,
4376b8c1829SRui Paulo 		    IEEE80211_MESH_PROTO_DSZ) == 0)
43859aa14a9SRui Paulo 			return EEXIST;
4396b8c1829SRui Paulo 		if (!mesh_proto_paths[i].mpp_active && firstempty == -1)
44059aa14a9SRui Paulo 			firstempty = i;
44159aa14a9SRui Paulo 	}
44259aa14a9SRui Paulo 	if (firstempty < 0)
44359aa14a9SRui Paulo 		return ENOSPC;
44459aa14a9SRui Paulo 	memcpy(&mesh_proto_paths[firstempty], mpp, sizeof(*mpp));
4456b8c1829SRui Paulo 	mesh_proto_paths[firstempty].mpp_active = 1;
44659aa14a9SRui Paulo 	return 0;
44759aa14a9SRui Paulo }
44859aa14a9SRui Paulo 
44959aa14a9SRui Paulo int
45059aa14a9SRui Paulo ieee80211_mesh_register_proto_metric(const struct
45159aa14a9SRui Paulo     ieee80211_mesh_proto_metric *mpm)
45259aa14a9SRui Paulo {
45359aa14a9SRui Paulo 	int i, firstempty = -1;
45459aa14a9SRui Paulo 
45559aa14a9SRui Paulo 	for (i = 0; i < N(mesh_proto_metrics); i++) {
4566b8c1829SRui Paulo 		if (strncmp(mpm->mpm_descr, mesh_proto_metrics[i].mpm_descr,
4576b8c1829SRui Paulo 		    IEEE80211_MESH_PROTO_DSZ) == 0)
45859aa14a9SRui Paulo 			return EEXIST;
4596b8c1829SRui Paulo 		if (!mesh_proto_metrics[i].mpm_active && firstempty == -1)
46059aa14a9SRui Paulo 			firstempty = i;
46159aa14a9SRui Paulo 	}
46259aa14a9SRui Paulo 	if (firstempty < 0)
46359aa14a9SRui Paulo 		return ENOSPC;
46459aa14a9SRui Paulo 	memcpy(&mesh_proto_metrics[firstempty], mpm, sizeof(*mpm));
4656b8c1829SRui Paulo 	mesh_proto_metrics[firstempty].mpm_active = 1;
46659aa14a9SRui Paulo 	return 0;
46759aa14a9SRui Paulo }
46859aa14a9SRui Paulo 
46959aa14a9SRui Paulo static int
47059aa14a9SRui Paulo mesh_select_proto_path(struct ieee80211vap *vap, const char *name)
47159aa14a9SRui Paulo {
47259aa14a9SRui Paulo 	struct ieee80211_mesh_state *ms = vap->iv_mesh;
47359aa14a9SRui Paulo 	int i;
47459aa14a9SRui Paulo 
47559aa14a9SRui Paulo 	for (i = 0; i < N(mesh_proto_paths); i++) {
47659aa14a9SRui Paulo 		if (strcasecmp(mesh_proto_paths[i].mpp_descr, name) == 0) {
47759aa14a9SRui Paulo 			ms->ms_ppath = &mesh_proto_paths[i];
47859aa14a9SRui Paulo 			return 0;
47959aa14a9SRui Paulo 		}
48059aa14a9SRui Paulo 	}
48159aa14a9SRui Paulo 	return ENOENT;
48259aa14a9SRui Paulo }
48359aa14a9SRui Paulo 
48459aa14a9SRui Paulo static int
48559aa14a9SRui Paulo mesh_select_proto_metric(struct ieee80211vap *vap, const char *name)
48659aa14a9SRui Paulo {
48759aa14a9SRui Paulo 	struct ieee80211_mesh_state *ms = vap->iv_mesh;
48859aa14a9SRui Paulo 	int i;
48959aa14a9SRui Paulo 
49059aa14a9SRui Paulo 	for (i = 0; i < N(mesh_proto_metrics); i++) {
49159aa14a9SRui Paulo 		if (strcasecmp(mesh_proto_metrics[i].mpm_descr, name) == 0) {
49259aa14a9SRui Paulo 			ms->ms_pmetric = &mesh_proto_metrics[i];
49359aa14a9SRui Paulo 			return 0;
49459aa14a9SRui Paulo 		}
49559aa14a9SRui Paulo 	}
49659aa14a9SRui Paulo 	return ENOENT;
49759aa14a9SRui Paulo }
49859aa14a9SRui Paulo #undef	N
49959aa14a9SRui Paulo 
50059aa14a9SRui Paulo static void
50159aa14a9SRui Paulo ieee80211_mesh_init(void)
50259aa14a9SRui Paulo {
50359aa14a9SRui Paulo 
50459aa14a9SRui Paulo 	memset(mesh_proto_paths, 0, sizeof(mesh_proto_paths));
50559aa14a9SRui Paulo 	memset(mesh_proto_metrics, 0, sizeof(mesh_proto_metrics));
50659aa14a9SRui Paulo 
50759aa14a9SRui Paulo 	/*
50859aa14a9SRui Paulo 	 * Setup mesh parameters that depends on the clock frequency.
50959aa14a9SRui Paulo 	 */
51059aa14a9SRui Paulo 	ieee80211_mesh_retrytimeout = msecs_to_ticks(40);
51159aa14a9SRui Paulo 	ieee80211_mesh_holdingtimeout = msecs_to_ticks(40);
51259aa14a9SRui Paulo 	ieee80211_mesh_confirmtimeout = msecs_to_ticks(40);
513ff17492fSMonthadar Al Jaberi 	ieee80211_mesh_backofftimeout = msecs_to_ticks(5000);
51459aa14a9SRui Paulo 
51559aa14a9SRui Paulo 	/*
51659aa14a9SRui Paulo 	 * Register action frame handlers.
51759aa14a9SRui Paulo 	 */
518ebeaa1adSMonthadar Al Jaberi 	ieee80211_recv_action_register(IEEE80211_ACTION_CAT_SELF_PROT,
51959aa14a9SRui Paulo 	    IEEE80211_ACTION_MESHPEERING_OPEN,
52059aa14a9SRui Paulo 	    mesh_recv_action_meshpeering_open);
521ebeaa1adSMonthadar Al Jaberi 	ieee80211_recv_action_register(IEEE80211_ACTION_CAT_SELF_PROT,
52259aa14a9SRui Paulo 	    IEEE80211_ACTION_MESHPEERING_CONFIRM,
52359aa14a9SRui Paulo 	    mesh_recv_action_meshpeering_confirm);
524ebeaa1adSMonthadar Al Jaberi 	ieee80211_recv_action_register(IEEE80211_ACTION_CAT_SELF_PROT,
52559aa14a9SRui Paulo 	    IEEE80211_ACTION_MESHPEERING_CLOSE,
52659aa14a9SRui Paulo 	    mesh_recv_action_meshpeering_close);
527bdd2a076SAdrian Chadd 	ieee80211_recv_action_register(IEEE80211_ACTION_CAT_MESH,
528bdd2a076SAdrian Chadd 	    IEEE80211_ACTION_MESH_LMETRIC, mesh_recv_action_meshlmetric);
52959aa14a9SRui Paulo 
530ebeaa1adSMonthadar Al Jaberi 	ieee80211_send_action_register(IEEE80211_ACTION_CAT_SELF_PROT,
53159aa14a9SRui Paulo 	    IEEE80211_ACTION_MESHPEERING_OPEN,
53259aa14a9SRui Paulo 	    mesh_send_action_meshpeering_open);
533ebeaa1adSMonthadar Al Jaberi 	ieee80211_send_action_register(IEEE80211_ACTION_CAT_SELF_PROT,
53459aa14a9SRui Paulo 	    IEEE80211_ACTION_MESHPEERING_CONFIRM,
53559aa14a9SRui Paulo 	    mesh_send_action_meshpeering_confirm);
536ebeaa1adSMonthadar Al Jaberi 	ieee80211_send_action_register(IEEE80211_ACTION_CAT_SELF_PROT,
53759aa14a9SRui Paulo 	    IEEE80211_ACTION_MESHPEERING_CLOSE,
53859aa14a9SRui Paulo 	    mesh_send_action_meshpeering_close);
539bdd2a076SAdrian Chadd 	ieee80211_send_action_register(IEEE80211_ACTION_CAT_MESH,
540bdd2a076SAdrian Chadd 	    IEEE80211_ACTION_MESH_LMETRIC,
541bdd2a076SAdrian Chadd 	    mesh_send_action_meshlmetric);
54259aa14a9SRui Paulo 
54359aa14a9SRui Paulo 	/*
54459aa14a9SRui Paulo 	 * Register Airtime Link Metric.
54559aa14a9SRui Paulo 	 */
54659aa14a9SRui Paulo 	ieee80211_mesh_register_proto_metric(&mesh_metric_airtime);
54759aa14a9SRui Paulo 
54859aa14a9SRui Paulo }
54959aa14a9SRui Paulo SYSINIT(wlan_mesh, SI_SUB_DRIVERS, SI_ORDER_FIRST, ieee80211_mesh_init, NULL);
55059aa14a9SRui Paulo 
55159aa14a9SRui Paulo void
55259aa14a9SRui Paulo ieee80211_mesh_attach(struct ieee80211com *ic)
55359aa14a9SRui Paulo {
55459aa14a9SRui Paulo 	ic->ic_vattach[IEEE80211_M_MBSS] = mesh_vattach;
55559aa14a9SRui Paulo }
55659aa14a9SRui Paulo 
55759aa14a9SRui Paulo void
55859aa14a9SRui Paulo ieee80211_mesh_detach(struct ieee80211com *ic)
55959aa14a9SRui Paulo {
56059aa14a9SRui Paulo }
56159aa14a9SRui Paulo 
56259aa14a9SRui Paulo static void
56359aa14a9SRui Paulo mesh_vdetach_peers(void *arg, struct ieee80211_node *ni)
56459aa14a9SRui Paulo {
56559aa14a9SRui Paulo 	struct ieee80211com *ic = ni->ni_ic;
56659aa14a9SRui Paulo 	uint16_t args[3];
56759aa14a9SRui Paulo 
56859aa14a9SRui Paulo 	if (ni->ni_mlstate == IEEE80211_NODE_MESH_ESTABLISHED) {
56959aa14a9SRui Paulo 		args[0] = ni->ni_mlpid;
57059aa14a9SRui Paulo 		args[1] = ni->ni_mllid;
57159aa14a9SRui Paulo 		args[2] = IEEE80211_REASON_PEER_LINK_CANCELED;
57259aa14a9SRui Paulo 		ieee80211_send_action(ni,
573ebeaa1adSMonthadar Al Jaberi 		    IEEE80211_ACTION_CAT_SELF_PROT,
57459aa14a9SRui Paulo 		    IEEE80211_ACTION_MESHPEERING_CLOSE,
57559aa14a9SRui Paulo 		    args);
57659aa14a9SRui Paulo 	}
577c104cff2SRui Paulo 	callout_drain(&ni->ni_mltimer);
57859aa14a9SRui Paulo 	/* XXX belongs in hwmp */
57959aa14a9SRui Paulo 	ieee80211_ageq_drain_node(&ic->ic_stageq,
58059aa14a9SRui Paulo 	   (void *)(uintptr_t) ieee80211_mac_hash(ic, ni->ni_macaddr));
58159aa14a9SRui Paulo }
58259aa14a9SRui Paulo 
58359aa14a9SRui Paulo static void
58459aa14a9SRui Paulo mesh_vdetach(struct ieee80211vap *vap)
58559aa14a9SRui Paulo {
58659aa14a9SRui Paulo 	struct ieee80211_mesh_state *ms = vap->iv_mesh;
58759aa14a9SRui Paulo 
588c104cff2SRui Paulo 	callout_drain(&ms->ms_cleantimer);
58959aa14a9SRui Paulo 	ieee80211_iterate_nodes(&vap->iv_ic->ic_sta, mesh_vdetach_peers,
59059aa14a9SRui Paulo 	    NULL);
59159aa14a9SRui Paulo 	ieee80211_mesh_rt_flush(vap);
59259aa14a9SRui Paulo 	mtx_destroy(&ms->ms_rt_lock);
59359aa14a9SRui Paulo 	ms->ms_ppath->mpp_vdetach(vap);
59459aa14a9SRui Paulo 	free(vap->iv_mesh, M_80211_VAP);
59559aa14a9SRui Paulo 	vap->iv_mesh = NULL;
59659aa14a9SRui Paulo }
59759aa14a9SRui Paulo 
59859aa14a9SRui Paulo static void
59959aa14a9SRui Paulo mesh_vattach(struct ieee80211vap *vap)
60059aa14a9SRui Paulo {
60159aa14a9SRui Paulo 	struct ieee80211_mesh_state *ms;
60259aa14a9SRui Paulo 	vap->iv_newstate = mesh_newstate;
60359aa14a9SRui Paulo 	vap->iv_input = mesh_input;
60459aa14a9SRui Paulo 	vap->iv_opdetach = mesh_vdetach;
60559aa14a9SRui Paulo 	vap->iv_recv_mgmt = mesh_recv_mgmt;
6060917631fSRui Paulo 	vap->iv_recv_ctl = mesh_recv_ctl;
60759aa14a9SRui Paulo 	ms = malloc(sizeof(struct ieee80211_mesh_state), M_80211_VAP,
60859aa14a9SRui Paulo 	    M_NOWAIT | M_ZERO);
60959aa14a9SRui Paulo 	if (ms == NULL) {
61059aa14a9SRui Paulo 		printf("%s: couldn't alloc MBSS state\n", __func__);
61159aa14a9SRui Paulo 		return;
61259aa14a9SRui Paulo 	}
61359aa14a9SRui Paulo 	vap->iv_mesh = ms;
61459aa14a9SRui Paulo 	ms->ms_seq = 0;
61559aa14a9SRui Paulo 	ms->ms_flags = (IEEE80211_MESHFLAGS_AP | IEEE80211_MESHFLAGS_FWD);
61659aa14a9SRui Paulo 	ms->ms_ttl = IEEE80211_MESH_DEFAULT_TTL;
61759aa14a9SRui Paulo 	TAILQ_INIT(&ms->ms_routes);
61859aa14a9SRui Paulo 	mtx_init(&ms->ms_rt_lock, "MBSS", "802.11s routing table", MTX_DEF);
619c104cff2SRui Paulo 	callout_init(&ms->ms_cleantimer, CALLOUT_MPSAFE);
62059aa14a9SRui Paulo 	mesh_select_proto_metric(vap, "AIRTIME");
62159aa14a9SRui Paulo 	KASSERT(ms->ms_pmetric, ("ms_pmetric == NULL"));
62259aa14a9SRui Paulo 	mesh_select_proto_path(vap, "HWMP");
62359aa14a9SRui Paulo 	KASSERT(ms->ms_ppath, ("ms_ppath == NULL"));
62459aa14a9SRui Paulo 	ms->ms_ppath->mpp_vattach(vap);
62559aa14a9SRui Paulo }
62659aa14a9SRui Paulo 
62759aa14a9SRui Paulo /*
62859aa14a9SRui Paulo  * IEEE80211_M_MBSS vap state machine handler.
62959aa14a9SRui Paulo  */
63059aa14a9SRui Paulo static int
63159aa14a9SRui Paulo mesh_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
63259aa14a9SRui Paulo {
63359aa14a9SRui Paulo 	struct ieee80211_mesh_state *ms = vap->iv_mesh;
63459aa14a9SRui Paulo 	struct ieee80211com *ic = vap->iv_ic;
63559aa14a9SRui Paulo 	struct ieee80211_node *ni;
63659aa14a9SRui Paulo 	enum ieee80211_state ostate;
63759aa14a9SRui Paulo 
63859aa14a9SRui Paulo 	IEEE80211_LOCK_ASSERT(ic);
63959aa14a9SRui Paulo 
64059aa14a9SRui Paulo 	ostate = vap->iv_state;
64159aa14a9SRui Paulo 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE, "%s: %s -> %s (%d)\n",
64259aa14a9SRui Paulo 	    __func__, ieee80211_state_name[ostate],
64359aa14a9SRui Paulo 	    ieee80211_state_name[nstate], arg);
64459aa14a9SRui Paulo 	vap->iv_state = nstate;		/* state transition */
64559aa14a9SRui Paulo 	if (ostate != IEEE80211_S_SCAN)
64659aa14a9SRui Paulo 		ieee80211_cancel_scan(vap);	/* background scan */
64759aa14a9SRui Paulo 	ni = vap->iv_bss;			/* NB: no reference held */
648c104cff2SRui Paulo 	if (nstate != IEEE80211_S_RUN && ostate == IEEE80211_S_RUN)
649c104cff2SRui Paulo 		callout_drain(&ms->ms_cleantimer);
65059aa14a9SRui Paulo 	switch (nstate) {
65159aa14a9SRui Paulo 	case IEEE80211_S_INIT:
65259aa14a9SRui Paulo 		switch (ostate) {
65359aa14a9SRui Paulo 		case IEEE80211_S_SCAN:
65459aa14a9SRui Paulo 			ieee80211_cancel_scan(vap);
65559aa14a9SRui Paulo 			break;
65659aa14a9SRui Paulo 		case IEEE80211_S_CAC:
65759aa14a9SRui Paulo 			ieee80211_dfs_cac_stop(vap);
65859aa14a9SRui Paulo 			break;
65959aa14a9SRui Paulo 		case IEEE80211_S_RUN:
66059aa14a9SRui Paulo 			ieee80211_iterate_nodes(&ic->ic_sta,
66159aa14a9SRui Paulo 			    mesh_vdetach_peers, NULL);
66259aa14a9SRui Paulo 			break;
66359aa14a9SRui Paulo 		default:
66459aa14a9SRui Paulo 			break;
66559aa14a9SRui Paulo 		}
66659aa14a9SRui Paulo 		if (ostate != IEEE80211_S_INIT) {
66759aa14a9SRui Paulo 			/* NB: optimize INIT -> INIT case */
66859aa14a9SRui Paulo 			ieee80211_reset_bss(vap);
669c104cff2SRui Paulo 			ieee80211_mesh_rt_flush(vap);
67059aa14a9SRui Paulo 		}
67159aa14a9SRui Paulo 		break;
67259aa14a9SRui Paulo 	case IEEE80211_S_SCAN:
67359aa14a9SRui Paulo 		switch (ostate) {
67459aa14a9SRui Paulo 		case IEEE80211_S_INIT:
67559aa14a9SRui Paulo 			if (vap->iv_des_chan != IEEE80211_CHAN_ANYC &&
67659aa14a9SRui Paulo 			    !IEEE80211_IS_CHAN_RADAR(vap->iv_des_chan) &&
67759aa14a9SRui Paulo 			    ms->ms_idlen != 0) {
67859aa14a9SRui Paulo 				/*
67959aa14a9SRui Paulo 				 * Already have a channel and a mesh ID; bypass
68059aa14a9SRui Paulo 				 * the scan and startup immediately.
68159aa14a9SRui Paulo 				 */
68259aa14a9SRui Paulo 				ieee80211_create_ibss(vap, vap->iv_des_chan);
68359aa14a9SRui Paulo 				break;
68459aa14a9SRui Paulo 			}
68559aa14a9SRui Paulo 			/*
68659aa14a9SRui Paulo 			 * Initiate a scan.  We can come here as a result
68759aa14a9SRui Paulo 			 * of an IEEE80211_IOC_SCAN_REQ too in which case
68859aa14a9SRui Paulo 			 * the vap will be marked with IEEE80211_FEXT_SCANREQ
68959aa14a9SRui Paulo 			 * and the scan request parameters will be present
69059aa14a9SRui Paulo 			 * in iv_scanreq.  Otherwise we do the default.
69159aa14a9SRui Paulo 			*/
69259aa14a9SRui Paulo 			if (vap->iv_flags_ext & IEEE80211_FEXT_SCANREQ) {
69359aa14a9SRui Paulo 				ieee80211_check_scan(vap,
69459aa14a9SRui Paulo 				    vap->iv_scanreq_flags,
69559aa14a9SRui Paulo 				    vap->iv_scanreq_duration,
69659aa14a9SRui Paulo 				    vap->iv_scanreq_mindwell,
69759aa14a9SRui Paulo 				    vap->iv_scanreq_maxdwell,
69859aa14a9SRui Paulo 				    vap->iv_scanreq_nssid, vap->iv_scanreq_ssid);
69959aa14a9SRui Paulo 				vap->iv_flags_ext &= ~IEEE80211_FEXT_SCANREQ;
70059aa14a9SRui Paulo 			} else
70159aa14a9SRui Paulo 				ieee80211_check_scan_current(vap);
70259aa14a9SRui Paulo 			break;
70359aa14a9SRui Paulo 		default:
70459aa14a9SRui Paulo 			break;
70559aa14a9SRui Paulo 		}
70659aa14a9SRui Paulo 		break;
70759aa14a9SRui Paulo 	case IEEE80211_S_CAC:
70859aa14a9SRui Paulo 		/*
70959aa14a9SRui Paulo 		 * Start CAC on a DFS channel.  We come here when starting
71059aa14a9SRui Paulo 		 * a bss on a DFS channel (see ieee80211_create_ibss).
71159aa14a9SRui Paulo 		 */
71259aa14a9SRui Paulo 		ieee80211_dfs_cac_start(vap);
71359aa14a9SRui Paulo 		break;
71459aa14a9SRui Paulo 	case IEEE80211_S_RUN:
71559aa14a9SRui Paulo 		switch (ostate) {
71659aa14a9SRui Paulo 		case IEEE80211_S_INIT:
71759aa14a9SRui Paulo 			/*
71859aa14a9SRui Paulo 			 * Already have a channel; bypass the
71959aa14a9SRui Paulo 			 * scan and startup immediately.
72059aa14a9SRui Paulo 			 * Note that ieee80211_create_ibss will call
72159aa14a9SRui Paulo 			 * back to do a RUN->RUN state change.
72259aa14a9SRui Paulo 			 */
72359aa14a9SRui Paulo 			ieee80211_create_ibss(vap,
72459aa14a9SRui Paulo 			    ieee80211_ht_adjust_channel(ic,
72559aa14a9SRui Paulo 				ic->ic_curchan, vap->iv_flags_ht));
72659aa14a9SRui Paulo 			/* NB: iv_bss is changed on return */
72759aa14a9SRui Paulo 			break;
72859aa14a9SRui Paulo 		case IEEE80211_S_CAC:
72959aa14a9SRui Paulo 			/*
73059aa14a9SRui Paulo 			 * NB: This is the normal state change when CAC
73159aa14a9SRui Paulo 			 * expires and no radar was detected; no need to
73259aa14a9SRui Paulo 			 * clear the CAC timer as it's already expired.
73359aa14a9SRui Paulo 			 */
73459aa14a9SRui Paulo 			/* fall thru... */
73559aa14a9SRui Paulo 		case IEEE80211_S_CSA:
73659aa14a9SRui Paulo #if 0
73759aa14a9SRui Paulo 			/*
73859aa14a9SRui Paulo 			 * Shorten inactivity timer of associated stations
73959aa14a9SRui Paulo 			 * to weed out sta's that don't follow a CSA.
74059aa14a9SRui Paulo 			 */
74159aa14a9SRui Paulo 			ieee80211_iterate_nodes(&ic->ic_sta, sta_csa, vap);
74259aa14a9SRui Paulo #endif
74359aa14a9SRui Paulo 			/*
74459aa14a9SRui Paulo 			 * Update bss node channel to reflect where
74559aa14a9SRui Paulo 			 * we landed after CSA.
74659aa14a9SRui Paulo 			 */
74759aa14a9SRui Paulo 			ieee80211_node_set_chan(vap->iv_bss,
74859aa14a9SRui Paulo 			    ieee80211_ht_adjust_channel(ic, ic->ic_curchan,
74959aa14a9SRui Paulo 				ieee80211_htchanflags(vap->iv_bss->ni_chan)));
75059aa14a9SRui Paulo 			/* XXX bypass debug msgs */
75159aa14a9SRui Paulo 			break;
75259aa14a9SRui Paulo 		case IEEE80211_S_SCAN:
75359aa14a9SRui Paulo 		case IEEE80211_S_RUN:
75459aa14a9SRui Paulo #ifdef IEEE80211_DEBUG
75559aa14a9SRui Paulo 			if (ieee80211_msg_debug(vap)) {
75659aa14a9SRui Paulo 				struct ieee80211_node *ni = vap->iv_bss;
75759aa14a9SRui Paulo 				ieee80211_note(vap,
75859aa14a9SRui Paulo 				    "synchronized with %s meshid ",
75959aa14a9SRui Paulo 				    ether_sprintf(ni->ni_meshid));
76059aa14a9SRui Paulo 				ieee80211_print_essid(ni->ni_meshid,
76159aa14a9SRui Paulo 				    ni->ni_meshidlen);
76259aa14a9SRui Paulo 				/* XXX MCS/HT */
76359aa14a9SRui Paulo 				printf(" channel %d\n",
76459aa14a9SRui Paulo 				    ieee80211_chan2ieee(ic, ic->ic_curchan));
76559aa14a9SRui Paulo 			}
76659aa14a9SRui Paulo #endif
76759aa14a9SRui Paulo 			break;
76859aa14a9SRui Paulo 		default:
76959aa14a9SRui Paulo 			break;
77059aa14a9SRui Paulo 		}
77159aa14a9SRui Paulo 		ieee80211_node_authorize(vap->iv_bss);
772e50821abSSam Leffler 		callout_reset(&ms->ms_cleantimer, ms->ms_ppath->mpp_inact,
773c104cff2SRui Paulo                     mesh_rt_cleanup_cb, vap);
77459aa14a9SRui Paulo 		break;
77559aa14a9SRui Paulo 	default:
77659aa14a9SRui Paulo 		break;
77759aa14a9SRui Paulo 	}
77859aa14a9SRui Paulo 	/* NB: ostate not nstate */
77959aa14a9SRui Paulo 	ms->ms_ppath->mpp_newstate(vap, ostate, arg);
78059aa14a9SRui Paulo 	return 0;
78159aa14a9SRui Paulo }
78259aa14a9SRui Paulo 
783c104cff2SRui Paulo static void
784c104cff2SRui Paulo mesh_rt_cleanup_cb(void *arg)
785c104cff2SRui Paulo {
786c104cff2SRui Paulo 	struct ieee80211vap *vap = arg;
787c104cff2SRui Paulo 	struct ieee80211_mesh_state *ms = vap->iv_mesh;
788c104cff2SRui Paulo 
789c104cff2SRui Paulo 	mesh_rt_flush_invalid(vap);
790e50821abSSam Leffler 	callout_reset(&ms->ms_cleantimer, ms->ms_ppath->mpp_inact,
791c104cff2SRui Paulo 	    mesh_rt_cleanup_cb, vap);
792c104cff2SRui Paulo }
793c104cff2SRui Paulo 
794c104cff2SRui Paulo 
79559aa14a9SRui Paulo /*
79659aa14a9SRui Paulo  * Helper function to note the Mesh Peer Link FSM change.
79759aa14a9SRui Paulo  */
79859aa14a9SRui Paulo static void
79959aa14a9SRui Paulo mesh_linkchange(struct ieee80211_node *ni, enum ieee80211_mesh_mlstate state)
80059aa14a9SRui Paulo {
80159aa14a9SRui Paulo 	struct ieee80211vap *vap = ni->ni_vap;
80259aa14a9SRui Paulo 	struct ieee80211_mesh_state *ms = vap->iv_mesh;
80359aa14a9SRui Paulo #ifdef IEEE80211_DEBUG
80459aa14a9SRui Paulo 	static const char *meshlinkstates[] = {
80559aa14a9SRui Paulo 		[IEEE80211_NODE_MESH_IDLE]		= "IDLE",
80659aa14a9SRui Paulo 		[IEEE80211_NODE_MESH_OPENSNT]		= "OPEN SENT",
80759aa14a9SRui Paulo 		[IEEE80211_NODE_MESH_OPENRCV]		= "OPEN RECEIVED",
80859aa14a9SRui Paulo 		[IEEE80211_NODE_MESH_CONFIRMRCV]	= "CONFIRM RECEIVED",
80959aa14a9SRui Paulo 		[IEEE80211_NODE_MESH_ESTABLISHED]	= "ESTABLISHED",
81059aa14a9SRui Paulo 		[IEEE80211_NODE_MESH_HOLDING]		= "HOLDING"
81159aa14a9SRui Paulo 	};
81259aa14a9SRui Paulo #endif
81359aa14a9SRui Paulo 	IEEE80211_NOTE(vap, IEEE80211_MSG_MESH,
81459aa14a9SRui Paulo 	    ni, "peer link: %s -> %s",
81559aa14a9SRui Paulo 	    meshlinkstates[ni->ni_mlstate], meshlinkstates[state]);
81659aa14a9SRui Paulo 
81759aa14a9SRui Paulo 	/* track neighbor count */
81859aa14a9SRui Paulo 	if (state == IEEE80211_NODE_MESH_ESTABLISHED &&
81959aa14a9SRui Paulo 	    ni->ni_mlstate != IEEE80211_NODE_MESH_ESTABLISHED) {
82059aa14a9SRui Paulo 		KASSERT(ms->ms_neighbors < 65535, ("neighbor count overflow"));
82159aa14a9SRui Paulo 		ms->ms_neighbors++;
822d093681cSRui Paulo 		ieee80211_beacon_notify(vap, IEEE80211_BEACON_MESHCONF);
82359aa14a9SRui Paulo 	} else if (ni->ni_mlstate == IEEE80211_NODE_MESH_ESTABLISHED &&
82459aa14a9SRui Paulo 	    state != IEEE80211_NODE_MESH_ESTABLISHED) {
82559aa14a9SRui Paulo 		KASSERT(ms->ms_neighbors > 0, ("neighbor count 0"));
82659aa14a9SRui Paulo 		ms->ms_neighbors--;
827d093681cSRui Paulo 		ieee80211_beacon_notify(vap, IEEE80211_BEACON_MESHCONF);
82859aa14a9SRui Paulo 	}
82959aa14a9SRui Paulo 	ni->ni_mlstate = state;
830c104cff2SRui Paulo 	switch (state) {
831c104cff2SRui Paulo 	case IEEE80211_NODE_MESH_HOLDING:
83259aa14a9SRui Paulo 		ms->ms_ppath->mpp_peerdown(ni);
833c104cff2SRui Paulo 		break;
834c104cff2SRui Paulo 	case IEEE80211_NODE_MESH_ESTABLISHED:
835c104cff2SRui Paulo 		ieee80211_mesh_discover(vap, ni->ni_macaddr, NULL);
836c104cff2SRui Paulo 		break;
837c104cff2SRui Paulo 	default:
838c104cff2SRui Paulo 		break;
839c104cff2SRui Paulo 	}
84059aa14a9SRui Paulo }
84159aa14a9SRui Paulo 
84259aa14a9SRui Paulo /*
84359aa14a9SRui Paulo  * Helper function to generate a unique local ID required for mesh
84459aa14a9SRui Paulo  * peer establishment.
84559aa14a9SRui Paulo  */
84659aa14a9SRui Paulo static void
84759aa14a9SRui Paulo mesh_checkid(void *arg, struct ieee80211_node *ni)
84859aa14a9SRui Paulo {
84959aa14a9SRui Paulo 	uint16_t *r = arg;
85059aa14a9SRui Paulo 
85159aa14a9SRui Paulo 	if (*r == ni->ni_mllid)
85259aa14a9SRui Paulo 		*(uint16_t *)arg = 0;
85359aa14a9SRui Paulo }
85459aa14a9SRui Paulo 
85559aa14a9SRui Paulo static uint32_t
85659aa14a9SRui Paulo mesh_generateid(struct ieee80211vap *vap)
85759aa14a9SRui Paulo {
85859aa14a9SRui Paulo 	int maxiter = 4;
85959aa14a9SRui Paulo 	uint16_t r;
86059aa14a9SRui Paulo 
86159aa14a9SRui Paulo 	do {
86259aa14a9SRui Paulo 		get_random_bytes(&r, 2);
86359aa14a9SRui Paulo 		ieee80211_iterate_nodes(&vap->iv_ic->ic_sta, mesh_checkid, &r);
86459aa14a9SRui Paulo 		maxiter--;
86559aa14a9SRui Paulo 	} while (r == 0 && maxiter > 0);
86659aa14a9SRui Paulo 	return r;
86759aa14a9SRui Paulo }
86859aa14a9SRui Paulo 
86959aa14a9SRui Paulo /*
87059aa14a9SRui Paulo  * Verifies if we already received this packet by checking its
87159aa14a9SRui Paulo  * sequence number.
8723ca80f0dSRui Paulo  * Returns 0 if the frame is to be accepted, 1 otherwise.
87359aa14a9SRui Paulo  */
87459aa14a9SRui Paulo static int
87559aa14a9SRui Paulo mesh_checkpseq(struct ieee80211vap *vap,
87659aa14a9SRui Paulo     const uint8_t source[IEEE80211_ADDR_LEN], uint32_t seq)
87759aa14a9SRui Paulo {
87859aa14a9SRui Paulo 	struct ieee80211_mesh_route *rt;
87959aa14a9SRui Paulo 
88059aa14a9SRui Paulo 	rt = ieee80211_mesh_rt_find(vap, source);
88159aa14a9SRui Paulo 	if (rt == NULL) {
8823ca80f0dSRui Paulo 		rt = ieee80211_mesh_rt_add(vap, source);
8833ca80f0dSRui Paulo 		if (rt == NULL) {
8843ca80f0dSRui Paulo 			IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH, source,
8853ca80f0dSRui Paulo 			    "%s", "add mcast route failed");
8863ca80f0dSRui Paulo 			vap->iv_stats.is_mesh_rtaddfailed++;
8873ca80f0dSRui Paulo 			return 1;
8883ca80f0dSRui Paulo 		}
889c104cff2SRui Paulo 		IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH, source,
890c104cff2SRui Paulo 		    "add mcast route, mesh seqno %d", seq);
89159aa14a9SRui Paulo 		rt->rt_lastmseq = seq;
89259aa14a9SRui Paulo 		return 0;
89359aa14a9SRui Paulo 	}
89459aa14a9SRui Paulo 	if (IEEE80211_MESH_SEQ_GEQ(rt->rt_lastmseq, seq)) {
89559aa14a9SRui Paulo 		return 1;
89659aa14a9SRui Paulo 	} else {
89759aa14a9SRui Paulo 		rt->rt_lastmseq = seq;
89859aa14a9SRui Paulo 		return 0;
89959aa14a9SRui Paulo 	}
90059aa14a9SRui Paulo }
90159aa14a9SRui Paulo 
90259aa14a9SRui Paulo /*
90359aa14a9SRui Paulo  * Iterate the routing table and locate the next hop.
90459aa14a9SRui Paulo  */
90559aa14a9SRui Paulo static struct ieee80211_node *
90659aa14a9SRui Paulo mesh_find_txnode(struct ieee80211vap *vap,
90759aa14a9SRui Paulo     const uint8_t dest[IEEE80211_ADDR_LEN])
90859aa14a9SRui Paulo {
90959aa14a9SRui Paulo 	struct ieee80211_mesh_route *rt;
91059aa14a9SRui Paulo 
91159aa14a9SRui Paulo 	rt = ieee80211_mesh_rt_find(vap, dest);
91259aa14a9SRui Paulo 	if (rt == NULL)
91359aa14a9SRui Paulo 		return NULL;
914c104cff2SRui Paulo 	if ((rt->rt_flags & IEEE80211_MESHRT_FLAGS_VALID) == 0 ||
915c104cff2SRui Paulo 	    (rt->rt_flags & IEEE80211_MESHRT_FLAGS_PROXY)) {
916c104cff2SRui Paulo 		IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH, dest,
917c104cff2SRui Paulo 		    "%s: !valid or proxy, flags 0x%x", __func__, rt->rt_flags);
918c104cff2SRui Paulo 		/* XXX stat */
919c104cff2SRui Paulo 		return NULL;
920c104cff2SRui Paulo 	}
92159aa14a9SRui Paulo 	return ieee80211_find_txnode(vap, rt->rt_nexthop);
92259aa14a9SRui Paulo }
92359aa14a9SRui Paulo 
92459aa14a9SRui Paulo /*
92559aa14a9SRui Paulo  * Forward the specified frame.
92659aa14a9SRui Paulo  * Decrement the TTL and set TA to our MAC address.
92759aa14a9SRui Paulo  */
92859aa14a9SRui Paulo static void
92959aa14a9SRui Paulo mesh_forward(struct ieee80211vap *vap, struct mbuf *m,
93059aa14a9SRui Paulo     const struct ieee80211_meshcntl *mc)
93159aa14a9SRui Paulo {
93259aa14a9SRui Paulo 	struct ieee80211com *ic = vap->iv_ic;
93359aa14a9SRui Paulo 	struct ieee80211_mesh_state *ms = vap->iv_mesh;
93459aa14a9SRui Paulo 	struct ifnet *ifp = vap->iv_ifp;
93559aa14a9SRui Paulo 	struct ifnet *parent = ic->ic_ifp;
93659aa14a9SRui Paulo 	const struct ieee80211_frame *wh =
93759aa14a9SRui Paulo 	    mtod(m, const struct ieee80211_frame *);
93859aa14a9SRui Paulo 	struct mbuf *mcopy;
93959aa14a9SRui Paulo 	struct ieee80211_meshcntl *mccopy;
94059aa14a9SRui Paulo 	struct ieee80211_frame *whcopy;
94159aa14a9SRui Paulo 	struct ieee80211_node *ni;
94259aa14a9SRui Paulo 	int err;
94359aa14a9SRui Paulo 
9443c314f6dSMonthadar Al Jaberi 	/*
9453c314f6dSMonthadar Al Jaberi 	 * mesh ttl of 1 means we are the last one receving it,
9463c314f6dSMonthadar Al Jaberi 	 * according to amendment we decrement and then check if
9473c314f6dSMonthadar Al Jaberi 	 * 0, if so we dont forward.
9483c314f6dSMonthadar Al Jaberi 	 */
9493c314f6dSMonthadar Al Jaberi 	if (mc->mc_ttl < 1) {
95059aa14a9SRui Paulo 		IEEE80211_NOTE_FRAME(vap, IEEE80211_MSG_MESH, wh,
9513c314f6dSMonthadar Al Jaberi 		    "%s", "frame not fwd'd, ttl 1");
95259aa14a9SRui Paulo 		vap->iv_stats.is_mesh_fwd_ttl++;
95359aa14a9SRui Paulo 		return;
95459aa14a9SRui Paulo 	}
95559aa14a9SRui Paulo 	if (!(ms->ms_flags & IEEE80211_MESHFLAGS_FWD)) {
95659aa14a9SRui Paulo 		IEEE80211_NOTE_FRAME(vap, IEEE80211_MSG_MESH, wh,
95759aa14a9SRui Paulo 		    "%s", "frame not fwd'd, fwding disabled");
95859aa14a9SRui Paulo 		vap->iv_stats.is_mesh_fwd_disabled++;
95959aa14a9SRui Paulo 		return;
96059aa14a9SRui Paulo 	}
961eb1b1807SGleb Smirnoff 	mcopy = m_dup(m, M_NOWAIT);
96259aa14a9SRui Paulo 	if (mcopy == NULL) {
96359aa14a9SRui Paulo 		IEEE80211_NOTE_FRAME(vap, IEEE80211_MSG_MESH, wh,
96459aa14a9SRui Paulo 		    "%s", "frame not fwd'd, cannot dup");
96559aa14a9SRui Paulo 		vap->iv_stats.is_mesh_fwd_nobuf++;
96659aa14a9SRui Paulo 		ifp->if_oerrors++;
96759aa14a9SRui Paulo 		return;
96859aa14a9SRui Paulo 	}
96959aa14a9SRui Paulo 	mcopy = m_pullup(mcopy, ieee80211_hdrspace(ic, wh) +
97059aa14a9SRui Paulo 	    sizeof(struct ieee80211_meshcntl));
97159aa14a9SRui Paulo 	if (mcopy == NULL) {
97259aa14a9SRui Paulo 		IEEE80211_NOTE_FRAME(vap, IEEE80211_MSG_MESH, wh,
97359aa14a9SRui Paulo 		    "%s", "frame not fwd'd, too short");
97459aa14a9SRui Paulo 		vap->iv_stats.is_mesh_fwd_tooshort++;
97559aa14a9SRui Paulo 		ifp->if_oerrors++;
97659aa14a9SRui Paulo 		m_freem(mcopy);
97759aa14a9SRui Paulo 		return;
97859aa14a9SRui Paulo 	}
97959aa14a9SRui Paulo 	whcopy = mtod(mcopy, struct ieee80211_frame *);
98059aa14a9SRui Paulo 	mccopy = (struct ieee80211_meshcntl *)
98159aa14a9SRui Paulo 	    (mtod(mcopy, uint8_t *) + ieee80211_hdrspace(ic, wh));
98259aa14a9SRui Paulo 	/* XXX clear other bits? */
98359aa14a9SRui Paulo 	whcopy->i_fc[1] &= ~IEEE80211_FC1_RETRY;
98459aa14a9SRui Paulo 	IEEE80211_ADDR_COPY(whcopy->i_addr2, vap->iv_myaddr);
98559aa14a9SRui Paulo 	if (IEEE80211_IS_MULTICAST(wh->i_addr1)) {
98659aa14a9SRui Paulo 		ni = ieee80211_ref_node(vap->iv_bss);
98759aa14a9SRui Paulo 		mcopy->m_flags |= M_MCAST;
98859aa14a9SRui Paulo 	} else {
98959aa14a9SRui Paulo 		ni = mesh_find_txnode(vap, whcopy->i_addr3);
99059aa14a9SRui Paulo 		if (ni == NULL) {
9913c314f6dSMonthadar Al Jaberi 			/*
9923c314f6dSMonthadar Al Jaberi 			 * [Optional] any of the following three actions:
9933c314f6dSMonthadar Al Jaberi 			 * o silently discard
9943c314f6dSMonthadar Al Jaberi 			 * o trigger a path discovery
9953db82516SMonthadar Al Jaberi 			 * o inform TA that meshDA is unknown.
9963c314f6dSMonthadar Al Jaberi 			 */
99759aa14a9SRui Paulo 			IEEE80211_NOTE_FRAME(vap, IEEE80211_MSG_MESH, wh,
99859aa14a9SRui Paulo 			    "%s", "frame not fwd'd, no path");
9993db82516SMonthadar Al Jaberi 			ms->ms_ppath->mpp_senderror(vap, whcopy->i_addr3, NULL,
10003db82516SMonthadar Al Jaberi 			    IEEE80211_REASON_MESH_PERR_NO_FI);
100159aa14a9SRui Paulo 			vap->iv_stats.is_mesh_fwd_nopath++;
100259aa14a9SRui Paulo 			m_freem(mcopy);
100359aa14a9SRui Paulo 			return;
100459aa14a9SRui Paulo 		}
100559aa14a9SRui Paulo 		IEEE80211_ADDR_COPY(whcopy->i_addr1, ni->ni_macaddr);
100659aa14a9SRui Paulo 	}
100759aa14a9SRui Paulo 	KASSERT(mccopy->mc_ttl > 0, ("%s called with wrong ttl", __func__));
100859aa14a9SRui Paulo 	mccopy->mc_ttl--;
100959aa14a9SRui Paulo 
101059aa14a9SRui Paulo 	/* XXX calculate priority so drivers can find the tx queue */
101159aa14a9SRui Paulo 	M_WME_SETAC(mcopy, WME_AC_BE);
101259aa14a9SRui Paulo 
101359aa14a9SRui Paulo 	/* XXX do we know m_nextpkt is NULL? */
101459aa14a9SRui Paulo 	mcopy->m_pkthdr.rcvif = (void *) ni;
101559aa14a9SRui Paulo 	err = parent->if_transmit(parent, mcopy);
101659aa14a9SRui Paulo 	if (err != 0) {
101759aa14a9SRui Paulo 		/* NB: IFQ_HANDOFF reclaims mbuf */
101859aa14a9SRui Paulo 		ieee80211_free_node(ni);
101959aa14a9SRui Paulo 	} else {
102059aa14a9SRui Paulo 		ifp->if_opackets++;
102159aa14a9SRui Paulo 	}
102259aa14a9SRui Paulo }
102359aa14a9SRui Paulo 
1024c104cff2SRui Paulo static struct mbuf *
1025c104cff2SRui Paulo mesh_decap(struct ieee80211vap *vap, struct mbuf *m, int hdrlen, int meshdrlen)
1026c104cff2SRui Paulo {
1027c104cff2SRui Paulo #define	WHDIR(wh)	((wh)->i_fc[1] & IEEE80211_FC1_DIR_MASK)
10283c314f6dSMonthadar Al Jaberi #define	MC01(mc)	((const struct ieee80211_meshcntl_ae01 *)mc)
1029c104cff2SRui Paulo 	uint8_t b[sizeof(struct ieee80211_qosframe_addr4) +
10303c314f6dSMonthadar Al Jaberi 		  sizeof(struct ieee80211_meshcntl_ae10)];
1031c104cff2SRui Paulo 	const struct ieee80211_qosframe_addr4 *wh;
1032c104cff2SRui Paulo 	const struct ieee80211_meshcntl_ae10 *mc;
1033c104cff2SRui Paulo 	struct ether_header *eh;
1034c104cff2SRui Paulo 	struct llc *llc;
1035c104cff2SRui Paulo 	int ae;
1036c104cff2SRui Paulo 
1037c104cff2SRui Paulo 	if (m->m_len < hdrlen + sizeof(*llc) &&
1038c104cff2SRui Paulo 	    (m = m_pullup(m, hdrlen + sizeof(*llc))) == NULL) {
1039c104cff2SRui Paulo 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_ANY,
1040c104cff2SRui Paulo 		    "discard data frame: %s", "m_pullup failed");
1041c104cff2SRui Paulo 		vap->iv_stats.is_rx_tooshort++;
1042c104cff2SRui Paulo 		return NULL;
1043c104cff2SRui Paulo 	}
1044c104cff2SRui Paulo 	memcpy(b, mtod(m, caddr_t), hdrlen);
1045c104cff2SRui Paulo 	wh = (const struct ieee80211_qosframe_addr4 *)&b[0];
1046c104cff2SRui Paulo 	mc = (const struct ieee80211_meshcntl_ae10 *)&b[hdrlen - meshdrlen];
1047c104cff2SRui Paulo 	KASSERT(WHDIR(wh) == IEEE80211_FC1_DIR_FROMDS ||
1048c104cff2SRui Paulo 		WHDIR(wh) == IEEE80211_FC1_DIR_DSTODS,
1049c104cff2SRui Paulo 	    ("bogus dir, fc 0x%x:0x%x", wh->i_fc[0], wh->i_fc[1]));
1050c104cff2SRui Paulo 
1051c104cff2SRui Paulo 	llc = (struct llc *)(mtod(m, caddr_t) + hdrlen);
1052c104cff2SRui Paulo 	if (llc->llc_dsap == LLC_SNAP_LSAP && llc->llc_ssap == LLC_SNAP_LSAP &&
1053c104cff2SRui Paulo 	    llc->llc_control == LLC_UI && llc->llc_snap.org_code[0] == 0 &&
1054c104cff2SRui Paulo 	    llc->llc_snap.org_code[1] == 0 && llc->llc_snap.org_code[2] == 0 &&
1055c104cff2SRui Paulo 	    /* NB: preserve AppleTalk frames that have a native SNAP hdr */
1056c104cff2SRui Paulo 	    !(llc->llc_snap.ether_type == htons(ETHERTYPE_AARP) ||
1057c104cff2SRui Paulo 	      llc->llc_snap.ether_type == htons(ETHERTYPE_IPX))) {
1058c104cff2SRui Paulo 		m_adj(m, hdrlen + sizeof(struct llc) - sizeof(*eh));
1059c104cff2SRui Paulo 		llc = NULL;
1060c104cff2SRui Paulo 	} else {
1061c104cff2SRui Paulo 		m_adj(m, hdrlen - sizeof(*eh));
1062c104cff2SRui Paulo 	}
1063c104cff2SRui Paulo 	eh = mtod(m, struct ether_header *);
10643c314f6dSMonthadar Al Jaberi 	ae = mc->mc_flags & IEEE80211_MESH_AE_MASK;
1065c104cff2SRui Paulo 	if (WHDIR(wh) == IEEE80211_FC1_DIR_FROMDS) {
1066c104cff2SRui Paulo 		IEEE80211_ADDR_COPY(eh->ether_dhost, wh->i_addr1);
10673c314f6dSMonthadar Al Jaberi 		if (ae == IEEE80211_MESH_AE_00) {
1068c104cff2SRui Paulo 			IEEE80211_ADDR_COPY(eh->ether_shost, wh->i_addr3);
10693c314f6dSMonthadar Al Jaberi 		} else if (ae == IEEE80211_MESH_AE_01) {
10703c314f6dSMonthadar Al Jaberi 			IEEE80211_ADDR_COPY(eh->ether_shost,
10713c314f6dSMonthadar Al Jaberi 			    MC01(mc)->mc_addr4);
1072c104cff2SRui Paulo 		} else {
1073c104cff2SRui Paulo 			IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
1074c104cff2SRui Paulo 			    (const struct ieee80211_frame *)wh, NULL,
1075c104cff2SRui Paulo 			    "bad AE %d", ae);
1076c104cff2SRui Paulo 			vap->iv_stats.is_mesh_badae++;
1077c104cff2SRui Paulo 			m_freem(m);
1078c104cff2SRui Paulo 			return NULL;
1079c104cff2SRui Paulo 		}
1080c104cff2SRui Paulo 	} else {
10813c314f6dSMonthadar Al Jaberi 		if (ae == IEEE80211_MESH_AE_00) {
1082c104cff2SRui Paulo 			IEEE80211_ADDR_COPY(eh->ether_dhost, wh->i_addr3);
1083c104cff2SRui Paulo 			IEEE80211_ADDR_COPY(eh->ether_shost, wh->i_addr4);
10843c314f6dSMonthadar Al Jaberi 		} else if (ae == IEEE80211_MESH_AE_10) {
10853c314f6dSMonthadar Al Jaberi 			IEEE80211_ADDR_COPY(eh->ether_dhost, mc->mc_addr5);
10863c314f6dSMonthadar Al Jaberi 			IEEE80211_ADDR_COPY(eh->ether_shost, mc->mc_addr6);
1087c104cff2SRui Paulo 		} else {
1088c104cff2SRui Paulo 			IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
1089c104cff2SRui Paulo 			    (const struct ieee80211_frame *)wh, NULL,
1090c104cff2SRui Paulo 			    "bad AE %d", ae);
1091c104cff2SRui Paulo 			vap->iv_stats.is_mesh_badae++;
1092c104cff2SRui Paulo 			m_freem(m);
1093c104cff2SRui Paulo 			return NULL;
1094c104cff2SRui Paulo 		}
1095c104cff2SRui Paulo 	}
1096c104cff2SRui Paulo #ifdef ALIGNED_POINTER
1097c104cff2SRui Paulo 	if (!ALIGNED_POINTER(mtod(m, caddr_t) + sizeof(*eh), uint32_t)) {
1098c104cff2SRui Paulo 		m = ieee80211_realign(vap, m, sizeof(*eh));
1099c104cff2SRui Paulo 		if (m == NULL)
1100c104cff2SRui Paulo 			return NULL;
1101c104cff2SRui Paulo 	}
1102c104cff2SRui Paulo #endif /* ALIGNED_POINTER */
1103c104cff2SRui Paulo 	if (llc != NULL) {
1104c104cff2SRui Paulo 		eh = mtod(m, struct ether_header *);
1105c104cff2SRui Paulo 		eh->ether_type = htons(m->m_pkthdr.len - sizeof(*eh));
1106c104cff2SRui Paulo 	}
1107c104cff2SRui Paulo 	return m;
1108c104cff2SRui Paulo #undef	WDIR
11093c314f6dSMonthadar Al Jaberi #undef	MC01
1110c104cff2SRui Paulo }
1111c104cff2SRui Paulo 
1112c104cff2SRui Paulo /*
1113c104cff2SRui Paulo  * Return non-zero if the unicast mesh data frame should be processed
1114c104cff2SRui Paulo  * locally.  Frames that are not proxy'd have our address, otherwise
1115c104cff2SRui Paulo  * we need to consult the routing table to look for a proxy entry.
1116c104cff2SRui Paulo  */
1117c104cff2SRui Paulo static __inline int
1118c104cff2SRui Paulo mesh_isucastforme(struct ieee80211vap *vap, const struct ieee80211_frame *wh,
1119c104cff2SRui Paulo     const struct ieee80211_meshcntl *mc)
1120c104cff2SRui Paulo {
1121c104cff2SRui Paulo 	int ae = mc->mc_flags & 3;
1122c104cff2SRui Paulo 
1123c104cff2SRui Paulo 	KASSERT((wh->i_fc[1] & IEEE80211_FC1_DIR_MASK) == IEEE80211_FC1_DIR_DSTODS,
1124c104cff2SRui Paulo 	    ("bad dir 0x%x:0x%x", wh->i_fc[0], wh->i_fc[1]));
11253c314f6dSMonthadar Al Jaberi 	KASSERT(ae == IEEE80211_MESH_AE_00 || ae == IEEE80211_MESH_AE_10,
11263c314f6dSMonthadar Al Jaberi 	    ("bad AE %d", ae));
11273c314f6dSMonthadar Al Jaberi 	if (ae == IEEE80211_MESH_AE_10) {	/* ucast w/ proxy */
1128c104cff2SRui Paulo 		const struct ieee80211_meshcntl_ae10 *mc10 =
1129c104cff2SRui Paulo 		    (const struct ieee80211_meshcntl_ae10 *) mc;
1130c104cff2SRui Paulo 		struct ieee80211_mesh_route *rt =
11313c314f6dSMonthadar Al Jaberi 		    ieee80211_mesh_rt_find(vap, mc10->mc_addr5);
1132c104cff2SRui Paulo 		/* check for proxy route to ourself */
1133c104cff2SRui Paulo 		return (rt != NULL &&
1134c104cff2SRui Paulo 		    (rt->rt_flags & IEEE80211_MESHRT_FLAGS_PROXY));
1135c104cff2SRui Paulo 	} else					/* ucast w/o proxy */
1136c104cff2SRui Paulo 		return IEEE80211_ADDR_EQ(wh->i_addr3, vap->iv_myaddr);
1137c104cff2SRui Paulo }
1138c104cff2SRui Paulo 
11393c314f6dSMonthadar Al Jaberi /*
11403c314f6dSMonthadar Al Jaberi  * Verifies transmitter, updates lifetime, precursor list and forwards data.
11413c314f6dSMonthadar Al Jaberi  * > 0 means we have forwarded data and no need to process locally
11423c314f6dSMonthadar Al Jaberi  * == 0 means we want to process locally (and we may have forwarded data
11433c314f6dSMonthadar Al Jaberi  * < 0 means there was an error and data should be discarded
11443c314f6dSMonthadar Al Jaberi  */
11453c314f6dSMonthadar Al Jaberi static int
11463c314f6dSMonthadar Al Jaberi mesh_recv_indiv_data_to_fwrd(struct ieee80211vap *vap, struct mbuf *m,
11473c314f6dSMonthadar Al Jaberi     struct ieee80211_frame *wh, const struct ieee80211_meshcntl *mc)
11483c314f6dSMonthadar Al Jaberi {
11494f3a27aeSMonthadar Al Jaberi 	struct ieee80211_qosframe_addr4 *qwh;
11504f3a27aeSMonthadar Al Jaberi 	struct ieee80211_mesh_state *ms = vap->iv_mesh;
11514f3a27aeSMonthadar Al Jaberi 	struct ieee80211_mesh_route *rt_meshda, *rt_meshsa;
11524f3a27aeSMonthadar Al Jaberi 
11534f3a27aeSMonthadar Al Jaberi 	qwh = (struct ieee80211_qosframe_addr4 *)wh;
11543c314f6dSMonthadar Al Jaberi 
11553c314f6dSMonthadar Al Jaberi 	/*
11563c314f6dSMonthadar Al Jaberi 	 * TODO:
11573c314f6dSMonthadar Al Jaberi 	 * o verify addr2 is  a legitimate transmitter
11583c314f6dSMonthadar Al Jaberi 	 * o lifetime of precursor of addr3 (addr2) is max(init, curr)
11593c314f6dSMonthadar Al Jaberi 	 * o lifetime of precursor of addr4 (nexthop) is max(init, curr)
11603c314f6dSMonthadar Al Jaberi 	 */
11613c314f6dSMonthadar Al Jaberi 
11624f3a27aeSMonthadar Al Jaberi 	/* set lifetime of addr3 (meshDA) to initial value */
11634f3a27aeSMonthadar Al Jaberi 	rt_meshda = ieee80211_mesh_rt_find(vap, qwh->i_addr3);
1164737a965eSMonthadar Al Jaberi 	if (rt_meshda == NULL) {
1165737a965eSMonthadar Al Jaberi 		IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH, qwh->i_addr2,
1166737a965eSMonthadar Al Jaberi 		    "no route to meshDA(%6D)", qwh->i_addr3, ":");
1167737a965eSMonthadar Al Jaberi 		/*
1168737a965eSMonthadar Al Jaberi 		 * [Optional] any of the following three actions:
1169737a965eSMonthadar Al Jaberi 		 * o silently discard 				[X]
1170737a965eSMonthadar Al Jaberi 		 * o trigger a path discovery			[ ]
1171737a965eSMonthadar Al Jaberi 		 * o inform TA that meshDA is unknown.		[ ]
1172737a965eSMonthadar Al Jaberi 		 */
1173737a965eSMonthadar Al Jaberi 		/* XXX: stats */
1174737a965eSMonthadar Al Jaberi 		return (-1);
1175737a965eSMonthadar Al Jaberi 	}
1176737a965eSMonthadar Al Jaberi 
11774f3a27aeSMonthadar Al Jaberi 	ieee80211_mesh_rt_update(rt_meshda, ticks_to_msecs(
11784f3a27aeSMonthadar Al Jaberi 	    ms->ms_ppath->mpp_inact));
11794f3a27aeSMonthadar Al Jaberi 
11804f3a27aeSMonthadar Al Jaberi 	/* set lifetime of addr4 (meshSA) to initial value */
11814f3a27aeSMonthadar Al Jaberi 	rt_meshsa = ieee80211_mesh_rt_find(vap, qwh->i_addr4);
11824f3a27aeSMonthadar Al Jaberi 	KASSERT(rt_meshsa != NULL, ("no route"));
11834f3a27aeSMonthadar Al Jaberi 	ieee80211_mesh_rt_update(rt_meshsa, ticks_to_msecs(
11844f3a27aeSMonthadar Al Jaberi 	    ms->ms_ppath->mpp_inact));
11854f3a27aeSMonthadar Al Jaberi 
11863c314f6dSMonthadar Al Jaberi 	mesh_forward(vap, m, mc);
11873c314f6dSMonthadar Al Jaberi 	return (1); /* dont process locally */
11883c314f6dSMonthadar Al Jaberi }
11893c314f6dSMonthadar Al Jaberi 
11903c314f6dSMonthadar Al Jaberi /*
11913c314f6dSMonthadar Al Jaberi  * Verifies transmitter, updates lifetime, precursor list and process data
11920af1b472SEitan Adler  * locally, if data is proxy with AE = 10 it could mean data should go
11933c314f6dSMonthadar Al Jaberi  * on another mesh path or data should be forwarded to the DS.
11943c314f6dSMonthadar Al Jaberi  *
11953c314f6dSMonthadar Al Jaberi  * > 0 means we have forwarded data and no need to process locally
11963c314f6dSMonthadar Al Jaberi  * == 0 means we want to process locally (and we may have forwarded data
11973c314f6dSMonthadar Al Jaberi  * < 0 means there was an error and data should be discarded
11983c314f6dSMonthadar Al Jaberi  */
11993c314f6dSMonthadar Al Jaberi static int
12003c314f6dSMonthadar Al Jaberi mesh_recv_indiv_data_to_me(struct ieee80211vap *vap, struct mbuf *m,
12013c314f6dSMonthadar Al Jaberi     struct ieee80211_frame *wh, const struct ieee80211_meshcntl *mc)
12023c314f6dSMonthadar Al Jaberi {
12033c314f6dSMonthadar Al Jaberi 	struct ieee80211_qosframe_addr4 *qwh;
12043c314f6dSMonthadar Al Jaberi 	const struct ieee80211_meshcntl_ae10 *mc10;
12054f3a27aeSMonthadar Al Jaberi 	struct ieee80211_mesh_state *ms = vap->iv_mesh;
12063c314f6dSMonthadar Al Jaberi 	struct ieee80211_mesh_route *rt;
12073c314f6dSMonthadar Al Jaberi 	int ae;
12083c314f6dSMonthadar Al Jaberi 
12093c314f6dSMonthadar Al Jaberi 	qwh = (struct ieee80211_qosframe_addr4 *)wh;
12103c314f6dSMonthadar Al Jaberi 	mc10 = (const struct ieee80211_meshcntl_ae10 *)mc;
12113c314f6dSMonthadar Al Jaberi 
12123c314f6dSMonthadar Al Jaberi 	/*
12133c314f6dSMonthadar Al Jaberi 	 * TODO:
12143c314f6dSMonthadar Al Jaberi 	 * o verify addr2 is  a legitimate transmitter
12153c314f6dSMonthadar Al Jaberi 	 * o lifetime of precursor entry is max(init, curr)
12163c314f6dSMonthadar Al Jaberi 	 */
12173c314f6dSMonthadar Al Jaberi 
12184f3a27aeSMonthadar Al Jaberi 	/* set lifetime of addr4 (meshSA) to initial value */
12194f3a27aeSMonthadar Al Jaberi 	rt = ieee80211_mesh_rt_find(vap, qwh->i_addr4);
12204f3a27aeSMonthadar Al Jaberi 	KASSERT(rt != NULL, ("no route"));
12214f3a27aeSMonthadar Al Jaberi 	ieee80211_mesh_rt_update(rt, ticks_to_msecs(ms->ms_ppath->mpp_inact));
12224f3a27aeSMonthadar Al Jaberi 	rt = NULL;
12234f3a27aeSMonthadar Al Jaberi 
12243c314f6dSMonthadar Al Jaberi 	ae = mc10->mc_flags & IEEE80211_MESH_AE_MASK;
12253c314f6dSMonthadar Al Jaberi 	KASSERT(ae == IEEE80211_MESH_AE_00 ||
12263c314f6dSMonthadar Al Jaberi 	    ae == IEEE80211_MESH_AE_10, ("bad AE %d", ae));
12273c314f6dSMonthadar Al Jaberi 	if (ae == IEEE80211_MESH_AE_10) {
12283c314f6dSMonthadar Al Jaberi 		if (IEEE80211_ADDR_EQ(mc10->mc_addr5, qwh->i_addr3)) {
12293c314f6dSMonthadar Al Jaberi 			return (0); /* process locally */
12303c314f6dSMonthadar Al Jaberi 		}
12313c314f6dSMonthadar Al Jaberi 
12323c314f6dSMonthadar Al Jaberi 		rt =  ieee80211_mesh_rt_find(vap, mc10->mc_addr5);
12333c314f6dSMonthadar Al Jaberi 		if (rt != NULL &&
12343c314f6dSMonthadar Al Jaberi 		    (rt->rt_flags & IEEE80211_MESHRT_FLAGS_VALID) &&
12353c314f6dSMonthadar Al Jaberi 		    (rt->rt_flags & IEEE80211_MESHRT_FLAGS_PROXY) == 0) {
12363c314f6dSMonthadar Al Jaberi 			/*
12373c314f6dSMonthadar Al Jaberi 			 * Forward on another mesh-path, according to
12383c314f6dSMonthadar Al Jaberi 			 * amendment as specified in 9.32.4.1
12393c314f6dSMonthadar Al Jaberi 			 */
12403c314f6dSMonthadar Al Jaberi 			IEEE80211_ADDR_COPY(qwh->i_addr3, mc10->mc_addr5);
12413c314f6dSMonthadar Al Jaberi 			mesh_forward(vap, m,
12423c314f6dSMonthadar Al Jaberi 			    (const struct ieee80211_meshcntl *)mc10);
12433c314f6dSMonthadar Al Jaberi 			return (1); /* dont process locally */
12443c314f6dSMonthadar Al Jaberi 		}
12453c314f6dSMonthadar Al Jaberi 		/*
12463c314f6dSMonthadar Al Jaberi 		 * All other cases: forward of MSDUs from the MBSS to DS indiv.
12473c314f6dSMonthadar Al Jaberi 		 * addressed according to 13.11.3.2.
12483c314f6dSMonthadar Al Jaberi 		 */
1249*227da7d1SMonthadar Al Jaberi 		IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_OUTPUT, qwh->i_addr2,
1250*227da7d1SMonthadar Al Jaberi 		    "forward frame to DS, SA(%6D) DA(%6D)",
1251*227da7d1SMonthadar Al Jaberi 		    mc10->mc_addr6, ":", mc10->mc_addr5, ":");
12523c314f6dSMonthadar Al Jaberi 	}
12533c314f6dSMonthadar Al Jaberi 	return (0); /* process locally */
12543c314f6dSMonthadar Al Jaberi }
12553c314f6dSMonthadar Al Jaberi 
12563c314f6dSMonthadar Al Jaberi /*
12573c314f6dSMonthadar Al Jaberi  * Try to forward the group addressed data on to other mesh STAs, and
12583c314f6dSMonthadar Al Jaberi  * also to the DS.
12593c314f6dSMonthadar Al Jaberi  *
12603c314f6dSMonthadar Al Jaberi  * > 0 means we have forwarded data and no need to process locally
12613c314f6dSMonthadar Al Jaberi  * == 0 means we want to process locally (and we may have forwarded data
12623c314f6dSMonthadar Al Jaberi  * < 0 means there was an error and data should be discarded
12633c314f6dSMonthadar Al Jaberi  */
12643c314f6dSMonthadar Al Jaberi static int
12653c314f6dSMonthadar Al Jaberi mesh_recv_group_data(struct ieee80211vap *vap, struct mbuf *m,
12663c314f6dSMonthadar Al Jaberi     struct ieee80211_frame *wh, const struct ieee80211_meshcntl *mc)
12673c314f6dSMonthadar Al Jaberi {
12683c314f6dSMonthadar Al Jaberi #define	MC01(mc)	((const struct ieee80211_meshcntl_ae01 *)mc)
12693c314f6dSMonthadar Al Jaberi 	struct ieee80211_mesh_state *ms = vap->iv_mesh;
12703c314f6dSMonthadar Al Jaberi 
12713c314f6dSMonthadar Al Jaberi 	mesh_forward(vap, m, mc);
12723c314f6dSMonthadar Al Jaberi 
12733c314f6dSMonthadar Al Jaberi 	if(mc->mc_ttl > 0) {
12743c314f6dSMonthadar Al Jaberi 		if (mc->mc_flags & IEEE80211_MESH_AE_01) {
12753c314f6dSMonthadar Al Jaberi 			/*
12763c314f6dSMonthadar Al Jaberi 			 * Forward of MSDUs from the MBSS to DS group addressed
12773c314f6dSMonthadar Al Jaberi 			 * (according to 13.11.3.2)
12783c314f6dSMonthadar Al Jaberi 			 * This happens by delivering the packet, and a bridge
12793c314f6dSMonthadar Al Jaberi 			 * will sent it on another port member.
12803c314f6dSMonthadar Al Jaberi 			 */
12819fc85253SMonthadar Al Jaberi 			if (ms->ms_flags & IEEE80211_MESHFLAGS_GATE &&
12823c314f6dSMonthadar Al Jaberi 			    ms->ms_flags & IEEE80211_MESHFLAGS_FWD)
12833c314f6dSMonthadar Al Jaberi 				IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH,
12843c314f6dSMonthadar Al Jaberi 				    MC01(mc)->mc_addr4, "%s",
12853c314f6dSMonthadar Al Jaberi 				    "forward from MBSS to the DS");
12863c314f6dSMonthadar Al Jaberi 		}
12873c314f6dSMonthadar Al Jaberi 	}
12883c314f6dSMonthadar Al Jaberi 	return (0); /* process locally */
12893c314f6dSMonthadar Al Jaberi #undef	MC01
12903c314f6dSMonthadar Al Jaberi }
12913c314f6dSMonthadar Al Jaberi 
129259aa14a9SRui Paulo static int
129359aa14a9SRui Paulo mesh_input(struct ieee80211_node *ni, struct mbuf *m, int rssi, int nf)
129459aa14a9SRui Paulo {
129559aa14a9SRui Paulo #define	HAS_SEQ(type)	((type & 0x4) == 0)
12963c314f6dSMonthadar Al Jaberi #define	MC01(mc)	((const struct ieee80211_meshcntl_ae01 *)mc)
12973c314f6dSMonthadar Al Jaberi #define	MC10(mc)	((const struct ieee80211_meshcntl_ae10 *)mc)
129859aa14a9SRui Paulo 	struct ieee80211vap *vap = ni->ni_vap;
129959aa14a9SRui Paulo 	struct ieee80211com *ic = ni->ni_ic;
130059aa14a9SRui Paulo 	struct ifnet *ifp = vap->iv_ifp;
130159aa14a9SRui Paulo 	struct ieee80211_frame *wh;
130259aa14a9SRui Paulo 	const struct ieee80211_meshcntl *mc;
13033c314f6dSMonthadar Al Jaberi 	int hdrspace, meshdrlen, need_tap, error;
13043c314f6dSMonthadar Al Jaberi 	uint8_t dir, type, subtype, ae;
130559aa14a9SRui Paulo 	uint32_t seq;
13063c314f6dSMonthadar Al Jaberi 	const uint8_t *addr;
13073c314f6dSMonthadar Al Jaberi 	uint8_t qos[2];
130859aa14a9SRui Paulo 	ieee80211_seq rxseq;
130959aa14a9SRui Paulo 
131059aa14a9SRui Paulo 	KASSERT(ni != NULL, ("null node"));
131159aa14a9SRui Paulo 	ni->ni_inact = ni->ni_inact_reload;
131259aa14a9SRui Paulo 
131359aa14a9SRui Paulo 	need_tap = 1;			/* mbuf need to be tapped. */
131459aa14a9SRui Paulo 	type = -1;			/* undefined */
131559aa14a9SRui Paulo 
131659aa14a9SRui Paulo 	if (m->m_pkthdr.len < sizeof(struct ieee80211_frame_min)) {
131759aa14a9SRui Paulo 		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
131859aa14a9SRui Paulo 		    ni->ni_macaddr, NULL,
131959aa14a9SRui Paulo 		    "too short (1): len %u", m->m_pkthdr.len);
132059aa14a9SRui Paulo 		vap->iv_stats.is_rx_tooshort++;
132159aa14a9SRui Paulo 		goto out;
132259aa14a9SRui Paulo 	}
132359aa14a9SRui Paulo 	/*
132459aa14a9SRui Paulo 	 * Bit of a cheat here, we use a pointer for a 3-address
132559aa14a9SRui Paulo 	 * frame format but don't reference fields past outside
132659aa14a9SRui Paulo 	 * ieee80211_frame_min w/o first validating the data is
132759aa14a9SRui Paulo 	 * present.
132859aa14a9SRui Paulo 	*/
132959aa14a9SRui Paulo 	wh = mtod(m, struct ieee80211_frame *);
133059aa14a9SRui Paulo 
133159aa14a9SRui Paulo 	if ((wh->i_fc[0] & IEEE80211_FC0_VERSION_MASK) !=
133259aa14a9SRui Paulo 	    IEEE80211_FC0_VERSION_0) {
133359aa14a9SRui Paulo 		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
133459aa14a9SRui Paulo 		    ni->ni_macaddr, NULL, "wrong version %x", wh->i_fc[0]);
133559aa14a9SRui Paulo 		vap->iv_stats.is_rx_badversion++;
133659aa14a9SRui Paulo 		goto err;
133759aa14a9SRui Paulo 	}
133859aa14a9SRui Paulo 	dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK;
133959aa14a9SRui Paulo 	type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
134059aa14a9SRui Paulo 	subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
134159aa14a9SRui Paulo 	if ((ic->ic_flags & IEEE80211_F_SCAN) == 0) {
134259aa14a9SRui Paulo 		IEEE80211_RSSI_LPF(ni->ni_avgrssi, rssi);
134359aa14a9SRui Paulo 		ni->ni_noise = nf;
134459aa14a9SRui Paulo 		if (HAS_SEQ(type)) {
134559aa14a9SRui Paulo 			uint8_t tid = ieee80211_gettid(wh);
134659aa14a9SRui Paulo 
134759aa14a9SRui Paulo 			if (IEEE80211_QOS_HAS_SEQ(wh) &&
134859aa14a9SRui Paulo 			    TID_TO_WME_AC(tid) >= WME_AC_VI)
134959aa14a9SRui Paulo 				ic->ic_wme.wme_hipri_traffic++;
135059aa14a9SRui Paulo 			rxseq = le16toh(*(uint16_t *)wh->i_seq);
1351cd0b8f2dSAdrian Chadd 			if (! ieee80211_check_rxseq(ni, wh)) {
135259aa14a9SRui Paulo 				/* duplicate, discard */
135359aa14a9SRui Paulo 				IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
135459aa14a9SRui Paulo 				    wh->i_addr1, "duplicate",
135559aa14a9SRui Paulo 				    "seqno <%u,%u> fragno <%u,%u> tid %u",
135659aa14a9SRui Paulo 				    rxseq >> IEEE80211_SEQ_SEQ_SHIFT,
135759aa14a9SRui Paulo 				    ni->ni_rxseqs[tid] >>
135859aa14a9SRui Paulo 				    IEEE80211_SEQ_SEQ_SHIFT,
135959aa14a9SRui Paulo 				    rxseq & IEEE80211_SEQ_FRAG_MASK,
136059aa14a9SRui Paulo 				    ni->ni_rxseqs[tid] &
136159aa14a9SRui Paulo 				    IEEE80211_SEQ_FRAG_MASK,
136259aa14a9SRui Paulo 				    tid);
136359aa14a9SRui Paulo 				vap->iv_stats.is_rx_dup++;
136459aa14a9SRui Paulo 				IEEE80211_NODE_STAT(ni, rx_dup);
136559aa14a9SRui Paulo 				goto out;
136659aa14a9SRui Paulo 			}
136759aa14a9SRui Paulo 			ni->ni_rxseqs[tid] = rxseq;
136859aa14a9SRui Paulo 		}
136959aa14a9SRui Paulo 	}
137059aa14a9SRui Paulo #ifdef IEEE80211_DEBUG
137159aa14a9SRui Paulo 	/*
137259aa14a9SRui Paulo 	 * It's easier, but too expensive, to simulate different mesh
137359aa14a9SRui Paulo 	 * topologies by consulting the ACL policy very early, so do this
137459aa14a9SRui Paulo 	 * only under DEBUG.
137559aa14a9SRui Paulo 	 *
137659aa14a9SRui Paulo 	 * NB: this check is also done upon peering link initiation.
137759aa14a9SRui Paulo 	 */
13785a8801b0SBernhard Schmidt 	if (vap->iv_acl != NULL && !vap->iv_acl->iac_check(vap, wh)) {
137959aa14a9SRui Paulo 		IEEE80211_DISCARD(vap, IEEE80211_MSG_ACL,
138059aa14a9SRui Paulo 		    wh, NULL, "%s", "disallowed by ACL");
138159aa14a9SRui Paulo 		vap->iv_stats.is_rx_acl++;
138259aa14a9SRui Paulo 		goto out;
138359aa14a9SRui Paulo 	}
138459aa14a9SRui Paulo #endif
138559aa14a9SRui Paulo 	switch (type) {
138659aa14a9SRui Paulo 	case IEEE80211_FC0_TYPE_DATA:
138759aa14a9SRui Paulo 		if (ni == vap->iv_bss)
138859aa14a9SRui Paulo 			goto out;
138959aa14a9SRui Paulo 		if (ni->ni_mlstate != IEEE80211_NODE_MESH_ESTABLISHED) {
139059aa14a9SRui Paulo 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_MESH,
139159aa14a9SRui Paulo 			    ni->ni_macaddr, NULL,
139259aa14a9SRui Paulo 			    "peer link not yet established (%d)",
139359aa14a9SRui Paulo 			    ni->ni_mlstate);
139459aa14a9SRui Paulo 			vap->iv_stats.is_mesh_nolink++;
139559aa14a9SRui Paulo 			goto out;
139659aa14a9SRui Paulo 		}
139759aa14a9SRui Paulo 		if (dir != IEEE80211_FC1_DIR_FROMDS &&
139859aa14a9SRui Paulo 		    dir != IEEE80211_FC1_DIR_DSTODS) {
139959aa14a9SRui Paulo 			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
140059aa14a9SRui Paulo 			    wh, "data", "incorrect dir 0x%x", dir);
140159aa14a9SRui Paulo 			vap->iv_stats.is_rx_wrongdir++;
140259aa14a9SRui Paulo 			goto err;
140359aa14a9SRui Paulo 		}
140491216c71SAdrian Chadd 
140591216c71SAdrian Chadd 		/* All Mesh data frames are QoS subtype */
140691216c71SAdrian Chadd 		if (!HAS_SEQ(type)) {
140791216c71SAdrian Chadd 			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
140891216c71SAdrian Chadd 			    wh, "data", "incorrect subtype 0x%x", subtype);
140991216c71SAdrian Chadd 			vap->iv_stats.is_rx_badsubtype++;
141091216c71SAdrian Chadd 			goto err;
141191216c71SAdrian Chadd 		}
141291216c71SAdrian Chadd 
141391216c71SAdrian Chadd 		/*
141491216c71SAdrian Chadd 		 * Next up, any fragmentation.
141591216c71SAdrian Chadd 		 * XXX: we defrag before we even try to forward,
141691216c71SAdrian Chadd 		 * Mesh Control field is not present in sub-sequent
141791216c71SAdrian Chadd 		 * fragmented frames. This is in contrast to Draft 4.0.
141891216c71SAdrian Chadd 		 */
141959aa14a9SRui Paulo 		hdrspace = ieee80211_hdrspace(ic, wh);
142091216c71SAdrian Chadd 		if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
142191216c71SAdrian Chadd 			m = ieee80211_defrag(ni, m, hdrspace);
142291216c71SAdrian Chadd 			if (m == NULL) {
142391216c71SAdrian Chadd 				/* Fragment dropped or frame not complete yet */
142491216c71SAdrian Chadd 				goto out;
142591216c71SAdrian Chadd 			}
142691216c71SAdrian Chadd 		}
142791216c71SAdrian Chadd 		wh = mtod(m, struct ieee80211_frame *); /* NB: after defrag */
142891216c71SAdrian Chadd 
142991216c71SAdrian Chadd 		/*
143091216c71SAdrian Chadd 		 * Now we have a complete Mesh Data frame.
143191216c71SAdrian Chadd 		 */
143291216c71SAdrian Chadd 
143391216c71SAdrian Chadd 		/*
143491216c71SAdrian Chadd 		 * Only fromDStoDS data frames use 4 address qos frames
143591216c71SAdrian Chadd 		 * as specified in amendment. Otherwise addr4 is located
143691216c71SAdrian Chadd 		 * in the Mesh Control field and a 3 address qos frame
143791216c71SAdrian Chadd 		 * is used.
143891216c71SAdrian Chadd 		 */
143991216c71SAdrian Chadd 		if (IEEE80211_IS_DSTODS(wh))
144091216c71SAdrian Chadd 			*(uint16_t *)qos = *(uint16_t *)
144191216c71SAdrian Chadd 			    ((struct ieee80211_qosframe_addr4 *)wh)->i_qos;
144291216c71SAdrian Chadd 		else
144391216c71SAdrian Chadd 			*(uint16_t *)qos = *(uint16_t *)
144491216c71SAdrian Chadd 			    ((struct ieee80211_qosframe *)wh)->i_qos;
144591216c71SAdrian Chadd 
144691216c71SAdrian Chadd 		/*
144791216c71SAdrian Chadd 		 * NB: The mesh STA sets the Mesh Control Present
144891216c71SAdrian Chadd 		 * subfield to 1 in the Mesh Data frame containing
144991216c71SAdrian Chadd 		 * an unfragmented MSDU, an A-MSDU, or the first
145091216c71SAdrian Chadd 		 * fragment of an MSDU.
145191216c71SAdrian Chadd 		 * After defrag it should always be present.
145291216c71SAdrian Chadd 		 */
145391216c71SAdrian Chadd 		if (!(qos[1] & IEEE80211_QOS_MC)) {
145491216c71SAdrian Chadd 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_MESH,
145591216c71SAdrian Chadd 			    ni->ni_macaddr, NULL,
145691216c71SAdrian Chadd 			    "%s", "Mesh control field not present");
145791216c71SAdrian Chadd 			vap->iv_stats.is_rx_elem_missing++; /* XXX: kinda */
145891216c71SAdrian Chadd 			goto err;
145991216c71SAdrian Chadd 		}
146091216c71SAdrian Chadd 
146191216c71SAdrian Chadd 		/* pull up enough to get to the mesh control */
146259aa14a9SRui Paulo 		if (m->m_len < hdrspace + sizeof(struct ieee80211_meshcntl) &&
146359aa14a9SRui Paulo 		    (m = m_pullup(m, hdrspace +
146459aa14a9SRui Paulo 		        sizeof(struct ieee80211_meshcntl))) == NULL) {
146559aa14a9SRui Paulo 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
146659aa14a9SRui Paulo 			    ni->ni_macaddr, NULL,
146759aa14a9SRui Paulo 			    "data too short: expecting %u", hdrspace);
146859aa14a9SRui Paulo 			vap->iv_stats.is_rx_tooshort++;
146959aa14a9SRui Paulo 			goto out;		/* XXX */
147059aa14a9SRui Paulo 		}
147159aa14a9SRui Paulo 		/*
147259aa14a9SRui Paulo 		 * Now calculate the full extent of the headers. Note
1473c104cff2SRui Paulo 		 * mesh_decap will pull up anything we didn't get
147459aa14a9SRui Paulo 		 * above when it strips the 802.11 headers.
147559aa14a9SRui Paulo 		 */
147659aa14a9SRui Paulo 		mc = (const struct ieee80211_meshcntl *)
147759aa14a9SRui Paulo 		    (mtod(m, const uint8_t *) + hdrspace);
14783c314f6dSMonthadar Al Jaberi 		ae = mc->mc_flags & IEEE80211_MESH_AE_MASK;
1479c104cff2SRui Paulo 		meshdrlen = sizeof(struct ieee80211_meshcntl) +
14803c314f6dSMonthadar Al Jaberi 		    ae * IEEE80211_ADDR_LEN;
1481c104cff2SRui Paulo 		hdrspace += meshdrlen;
14823c314f6dSMonthadar Al Jaberi 
14833c314f6dSMonthadar Al Jaberi 		/* pull complete hdrspace = ieee80211_hdrspace + meshcontrol */
14843c314f6dSMonthadar Al Jaberi 		if ((meshdrlen > sizeof(struct ieee80211_meshcntl)) &&
14853c314f6dSMonthadar Al Jaberi 		    (m->m_len < hdrspace) &&
14863c314f6dSMonthadar Al Jaberi 		    ((m = m_pullup(m, hdrspace)) == NULL)) {
14873c314f6dSMonthadar Al Jaberi 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
14883c314f6dSMonthadar Al Jaberi 			    ni->ni_macaddr, NULL,
14893c314f6dSMonthadar Al Jaberi 			    "data too short: expecting %u", hdrspace);
14903c314f6dSMonthadar Al Jaberi 			vap->iv_stats.is_rx_tooshort++;
14913c314f6dSMonthadar Al Jaberi 			goto out;		/* XXX */
14923c314f6dSMonthadar Al Jaberi 		}
14933c314f6dSMonthadar Al Jaberi 		/* XXX: are we sure there is no reallocating after m_pullup? */
14943c314f6dSMonthadar Al Jaberi 
149559aa14a9SRui Paulo 		seq = LE_READ_4(mc->mc_seq);
149659aa14a9SRui Paulo 		if (IEEE80211_IS_MULTICAST(wh->i_addr1))
149759aa14a9SRui Paulo 			addr = wh->i_addr3;
14983c314f6dSMonthadar Al Jaberi 		else if (ae == IEEE80211_MESH_AE_01)
14993c314f6dSMonthadar Al Jaberi 			addr = MC01(mc)->mc_addr4;
150059aa14a9SRui Paulo 		else
150159aa14a9SRui Paulo 			addr = ((struct ieee80211_qosframe_addr4 *)wh)->i_addr4;
150259aa14a9SRui Paulo 		if (IEEE80211_ADDR_EQ(vap->iv_myaddr, addr)) {
150359aa14a9SRui Paulo 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
150459aa14a9SRui Paulo 			    addr, "data", "%s", "not to me");
150559aa14a9SRui Paulo 			vap->iv_stats.is_rx_wrongbss++;	/* XXX kinda */
150659aa14a9SRui Paulo 			goto out;
150759aa14a9SRui Paulo 		}
150859aa14a9SRui Paulo 		if (mesh_checkpseq(vap, addr, seq) != 0) {
150959aa14a9SRui Paulo 			vap->iv_stats.is_rx_dup++;
151059aa14a9SRui Paulo 			goto out;
151159aa14a9SRui Paulo 		}
151259aa14a9SRui Paulo 
15133c314f6dSMonthadar Al Jaberi 		/* This code "routes" the frame to the right control path */
15143c314f6dSMonthadar Al Jaberi 		if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
15153c314f6dSMonthadar Al Jaberi 			if (IEEE80211_ADDR_EQ(vap->iv_myaddr, wh->i_addr3))
15163c314f6dSMonthadar Al Jaberi 				error =
15173c314f6dSMonthadar Al Jaberi 				    mesh_recv_indiv_data_to_me(vap, m, wh, mc);
15183c314f6dSMonthadar Al Jaberi 			else if (IEEE80211_IS_MULTICAST(wh->i_addr3))
15193c314f6dSMonthadar Al Jaberi 				error = mesh_recv_group_data(vap, m, wh, mc);
15203c314f6dSMonthadar Al Jaberi 			else
15213c314f6dSMonthadar Al Jaberi 				error = mesh_recv_indiv_data_to_fwrd(vap, m,
15223c314f6dSMonthadar Al Jaberi 				    wh, mc);
15233c314f6dSMonthadar Al Jaberi 		} else
15243c314f6dSMonthadar Al Jaberi 			error = mesh_recv_group_data(vap, m, wh, mc);
15253c314f6dSMonthadar Al Jaberi 		if (error < 0)
15263c314f6dSMonthadar Al Jaberi 			goto err;
15273c314f6dSMonthadar Al Jaberi 		else if (error > 0)
152859aa14a9SRui Paulo 			goto out;
152959aa14a9SRui Paulo 
153059aa14a9SRui Paulo 		if (ieee80211_radiotap_active_vap(vap))
153159aa14a9SRui Paulo 			ieee80211_radiotap_rx(vap, m);
153259aa14a9SRui Paulo 		need_tap = 0;
153359aa14a9SRui Paulo 
153459aa14a9SRui Paulo 		/*
153559aa14a9SRui Paulo 		 * Finally, strip the 802.11 header.
153659aa14a9SRui Paulo 		 */
1537c104cff2SRui Paulo 		m = mesh_decap(vap, m, hdrspace, meshdrlen);
153859aa14a9SRui Paulo 		if (m == NULL) {
153959aa14a9SRui Paulo 			/* XXX mask bit to check for both */
154059aa14a9SRui Paulo 			/* don't count Null data frames as errors */
154159aa14a9SRui Paulo 			if (subtype == IEEE80211_FC0_SUBTYPE_NODATA ||
154259aa14a9SRui Paulo 			    subtype == IEEE80211_FC0_SUBTYPE_QOS_NULL)
154359aa14a9SRui Paulo 				goto out;
154459aa14a9SRui Paulo 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
154559aa14a9SRui Paulo 			    ni->ni_macaddr, "data", "%s", "decap error");
154659aa14a9SRui Paulo 			vap->iv_stats.is_rx_decap++;
154759aa14a9SRui Paulo 			IEEE80211_NODE_STAT(ni, rx_decap);
154859aa14a9SRui Paulo 			goto err;
154959aa14a9SRui Paulo 		}
155091216c71SAdrian Chadd 		if (qos[0] & IEEE80211_QOS_AMSDU) {
155159aa14a9SRui Paulo 			m = ieee80211_decap_amsdu(ni, m);
155259aa14a9SRui Paulo 			if (m == NULL)
155359aa14a9SRui Paulo 				return IEEE80211_FC0_TYPE_DATA;
155459aa14a9SRui Paulo 		}
155559aa14a9SRui Paulo 		ieee80211_deliver_data(vap, ni, m);
155659aa14a9SRui Paulo 		return type;
155759aa14a9SRui Paulo 	case IEEE80211_FC0_TYPE_MGT:
155859aa14a9SRui Paulo 		vap->iv_stats.is_rx_mgmt++;
155959aa14a9SRui Paulo 		IEEE80211_NODE_STAT(ni, rx_mgmt);
156059aa14a9SRui Paulo 		if (dir != IEEE80211_FC1_DIR_NODS) {
156159aa14a9SRui Paulo 			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
156259aa14a9SRui Paulo 			    wh, "mgt", "incorrect dir 0x%x", dir);
156359aa14a9SRui Paulo 			vap->iv_stats.is_rx_wrongdir++;
156459aa14a9SRui Paulo 			goto err;
156559aa14a9SRui Paulo 		}
156659aa14a9SRui Paulo 		if (m->m_pkthdr.len < sizeof(struct ieee80211_frame)) {
156759aa14a9SRui Paulo 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
156859aa14a9SRui Paulo 			    ni->ni_macaddr, "mgt", "too short: len %u",
156959aa14a9SRui Paulo 			    m->m_pkthdr.len);
157059aa14a9SRui Paulo 			vap->iv_stats.is_rx_tooshort++;
157159aa14a9SRui Paulo 			goto out;
157259aa14a9SRui Paulo 		}
157359aa14a9SRui Paulo #ifdef IEEE80211_DEBUG
157459aa14a9SRui Paulo 		if ((ieee80211_msg_debug(vap) &&
157559aa14a9SRui Paulo 		    (vap->iv_ic->ic_flags & IEEE80211_F_SCAN)) ||
157659aa14a9SRui Paulo 		    ieee80211_msg_dumppkts(vap)) {
157759aa14a9SRui Paulo 			if_printf(ifp, "received %s from %s rssi %d\n",
157859aa14a9SRui Paulo 			    ieee80211_mgt_subtype_name[subtype >>
157959aa14a9SRui Paulo 			    IEEE80211_FC0_SUBTYPE_SHIFT],
158059aa14a9SRui Paulo 			    ether_sprintf(wh->i_addr2), rssi);
158159aa14a9SRui Paulo 		}
158259aa14a9SRui Paulo #endif
158359aa14a9SRui Paulo 		if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
158459aa14a9SRui Paulo 			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
158559aa14a9SRui Paulo 			    wh, NULL, "%s", "WEP set but not permitted");
158659aa14a9SRui Paulo 			vap->iv_stats.is_rx_mgtdiscard++; /* XXX */
158759aa14a9SRui Paulo 			goto out;
158859aa14a9SRui Paulo 		}
158959aa14a9SRui Paulo 		vap->iv_recv_mgmt(ni, m, subtype, rssi, nf);
159059aa14a9SRui Paulo 		goto out;
159159aa14a9SRui Paulo 	case IEEE80211_FC0_TYPE_CTL:
159259aa14a9SRui Paulo 		vap->iv_stats.is_rx_ctl++;
159359aa14a9SRui Paulo 		IEEE80211_NODE_STAT(ni, rx_ctrl);
159459aa14a9SRui Paulo 		goto out;
159559aa14a9SRui Paulo 	default:
159659aa14a9SRui Paulo 		IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
159759aa14a9SRui Paulo 		    wh, "bad", "frame type 0x%x", type);
159859aa14a9SRui Paulo 		/* should not come here */
159959aa14a9SRui Paulo 		break;
160059aa14a9SRui Paulo 	}
160159aa14a9SRui Paulo err:
160259aa14a9SRui Paulo 	ifp->if_ierrors++;
160359aa14a9SRui Paulo out:
160459aa14a9SRui Paulo 	if (m != NULL) {
160559aa14a9SRui Paulo 		if (need_tap && ieee80211_radiotap_active_vap(vap))
160659aa14a9SRui Paulo 			ieee80211_radiotap_rx(vap, m);
160759aa14a9SRui Paulo 		m_freem(m);
160859aa14a9SRui Paulo 	}
160959aa14a9SRui Paulo 	return type;
16103c314f6dSMonthadar Al Jaberi #undef	HAS_SEQ
16113c314f6dSMonthadar Al Jaberi #undef	MC01
16123c314f6dSMonthadar Al Jaberi #undef	MC10
161359aa14a9SRui Paulo }
161459aa14a9SRui Paulo 
161559aa14a9SRui Paulo static void
161659aa14a9SRui Paulo mesh_recv_mgmt(struct ieee80211_node *ni, struct mbuf *m0, int subtype,
161759aa14a9SRui Paulo     int rssi, int nf)
161859aa14a9SRui Paulo {
161959aa14a9SRui Paulo 	struct ieee80211vap *vap = ni->ni_vap;
162059aa14a9SRui Paulo 	struct ieee80211_mesh_state *ms = vap->iv_mesh;
162159aa14a9SRui Paulo 	struct ieee80211com *ic = ni->ni_ic;
162259aa14a9SRui Paulo 	struct ieee80211_frame *wh;
1623b5df85a6SMonthadar Al Jaberi 	struct ieee80211_mesh_route *rt;
162459aa14a9SRui Paulo 	uint8_t *frm, *efrm;
162559aa14a9SRui Paulo 
162659aa14a9SRui Paulo 	wh = mtod(m0, struct ieee80211_frame *);
162759aa14a9SRui Paulo 	frm = (uint8_t *)&wh[1];
162859aa14a9SRui Paulo 	efrm = mtod(m0, uint8_t *) + m0->m_len;
162959aa14a9SRui Paulo 	switch (subtype) {
163059aa14a9SRui Paulo 	case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
163159aa14a9SRui Paulo 	case IEEE80211_FC0_SUBTYPE_BEACON:
163259aa14a9SRui Paulo 	{
163359aa14a9SRui Paulo 		struct ieee80211_scanparams scan;
163459aa14a9SRui Paulo 		/*
163559aa14a9SRui Paulo 		 * We process beacon/probe response
163659aa14a9SRui Paulo 		 * frames to discover neighbors.
163759aa14a9SRui Paulo 		 */
163859aa14a9SRui Paulo 		if (ieee80211_parse_beacon(ni, m0, &scan) != 0)
163959aa14a9SRui Paulo 			return;
164059aa14a9SRui Paulo 		/*
164159aa14a9SRui Paulo 		 * Count frame now that we know it's to be processed.
164259aa14a9SRui Paulo 		 */
164359aa14a9SRui Paulo 		if (subtype == IEEE80211_FC0_SUBTYPE_BEACON) {
164459aa14a9SRui Paulo 			vap->iv_stats.is_rx_beacon++;	/* XXX remove */
164559aa14a9SRui Paulo 			IEEE80211_NODE_STAT(ni, rx_beacons);
164659aa14a9SRui Paulo 		} else
164759aa14a9SRui Paulo 			IEEE80211_NODE_STAT(ni, rx_proberesp);
164859aa14a9SRui Paulo 		/*
164959aa14a9SRui Paulo 		 * If scanning, just pass information to the scan module.
165059aa14a9SRui Paulo 		 */
165159aa14a9SRui Paulo 		if (ic->ic_flags & IEEE80211_F_SCAN) {
165259aa14a9SRui Paulo 			if (ic->ic_flags_ext & IEEE80211_FEXT_PROBECHAN) {
165359aa14a9SRui Paulo 				/*
165459aa14a9SRui Paulo 				 * Actively scanning a channel marked passive;
165559aa14a9SRui Paulo 				 * send a probe request now that we know there
165659aa14a9SRui Paulo 				 * is 802.11 traffic present.
165759aa14a9SRui Paulo 				 *
165859aa14a9SRui Paulo 				 * XXX check if the beacon we recv'd gives
165959aa14a9SRui Paulo 				 * us what we need and suppress the probe req
166059aa14a9SRui Paulo 				 */
166159aa14a9SRui Paulo 				ieee80211_probe_curchan(vap, 1);
166259aa14a9SRui Paulo 				ic->ic_flags_ext &= ~IEEE80211_FEXT_PROBECHAN;
166359aa14a9SRui Paulo 			}
166459aa14a9SRui Paulo 			ieee80211_add_scan(vap, &scan, wh,
166559aa14a9SRui Paulo 			    subtype, rssi, nf);
166659aa14a9SRui Paulo 			return;
166759aa14a9SRui Paulo 		}
166859aa14a9SRui Paulo 
166959aa14a9SRui Paulo 		/* The rest of this code assumes we are running */
167059aa14a9SRui Paulo 		if (vap->iv_state != IEEE80211_S_RUN)
167159aa14a9SRui Paulo 			return;
167259aa14a9SRui Paulo 		/*
167359aa14a9SRui Paulo 		 * Ignore non-mesh STAs.
167459aa14a9SRui Paulo 		 */
167559aa14a9SRui Paulo 		if ((scan.capinfo &
167659aa14a9SRui Paulo 		     (IEEE80211_CAPINFO_ESS|IEEE80211_CAPINFO_IBSS)) ||
167759aa14a9SRui Paulo 		    scan.meshid == NULL || scan.meshconf == NULL) {
167859aa14a9SRui Paulo 			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
167959aa14a9SRui Paulo 			    wh, "beacon", "%s", "not a mesh sta");
168059aa14a9SRui Paulo 			vap->iv_stats.is_mesh_wrongmesh++;
168159aa14a9SRui Paulo 			return;
168259aa14a9SRui Paulo 		}
168359aa14a9SRui Paulo 		/*
168459aa14a9SRui Paulo 		 * Ignore STAs for other mesh networks.
168559aa14a9SRui Paulo 		 */
168659aa14a9SRui Paulo 		if (memcmp(scan.meshid+2, ms->ms_id, ms->ms_idlen) != 0 ||
168759aa14a9SRui Paulo 		    mesh_verify_meshconf(vap, scan.meshconf)) {
168859aa14a9SRui Paulo 			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
168959aa14a9SRui Paulo 			    wh, "beacon", "%s", "not for our mesh");
169059aa14a9SRui Paulo 			vap->iv_stats.is_mesh_wrongmesh++;
169159aa14a9SRui Paulo 			return;
169259aa14a9SRui Paulo 		}
169359aa14a9SRui Paulo 		/*
169459aa14a9SRui Paulo 		 * Peer only based on the current ACL policy.
169559aa14a9SRui Paulo 		 */
16965a8801b0SBernhard Schmidt 		if (vap->iv_acl != NULL && !vap->iv_acl->iac_check(vap, wh)) {
169759aa14a9SRui Paulo 			IEEE80211_DISCARD(vap, IEEE80211_MSG_ACL,
169859aa14a9SRui Paulo 			    wh, NULL, "%s", "disallowed by ACL");
169959aa14a9SRui Paulo 			vap->iv_stats.is_rx_acl++;
170059aa14a9SRui Paulo 			return;
170159aa14a9SRui Paulo 		}
170259aa14a9SRui Paulo 		/*
170359aa14a9SRui Paulo 		 * Do neighbor discovery.
170459aa14a9SRui Paulo 		 */
170559aa14a9SRui Paulo 		if (!IEEE80211_ADDR_EQ(wh->i_addr2, ni->ni_macaddr)) {
170659aa14a9SRui Paulo 			/*
170759aa14a9SRui Paulo 			 * Create a new entry in the neighbor table.
170859aa14a9SRui Paulo 			 */
170959aa14a9SRui Paulo 			ni = ieee80211_add_neighbor(vap, wh, &scan);
171059aa14a9SRui Paulo 		}
171159aa14a9SRui Paulo 		/*
171259aa14a9SRui Paulo 		 * Automatically peer with discovered nodes if possible.
171359aa14a9SRui Paulo 		 */
171459aa14a9SRui Paulo 		if (ni != vap->iv_bss &&
1715b5df85a6SMonthadar Al Jaberi 		    (ms->ms_flags & IEEE80211_MESHFLAGS_AP)) {
1716b5df85a6SMonthadar Al Jaberi 			switch (ni->ni_mlstate) {
1717b5df85a6SMonthadar Al Jaberi 			case IEEE80211_NODE_MESH_IDLE:
1718b5df85a6SMonthadar Al Jaberi 			{
171959aa14a9SRui Paulo 				uint16_t args[1];
172059aa14a9SRui Paulo 
1721ff17492fSMonthadar Al Jaberi 				/* Wait for backoff callout to reset counter */
1722ff17492fSMonthadar Al Jaberi 				if (ni->ni_mlhcnt >= ieee80211_mesh_maxholding)
1723ff17492fSMonthadar Al Jaberi 					return;
1724ff17492fSMonthadar Al Jaberi 
172559aa14a9SRui Paulo 				ni->ni_mlpid = mesh_generateid(vap);
172659aa14a9SRui Paulo 				if (ni->ni_mlpid == 0)
172759aa14a9SRui Paulo 					return;
172859aa14a9SRui Paulo 				mesh_linkchange(ni, IEEE80211_NODE_MESH_OPENSNT);
172959aa14a9SRui Paulo 				args[0] = ni->ni_mlpid;
173059aa14a9SRui Paulo 				ieee80211_send_action(ni,
1731ebeaa1adSMonthadar Al Jaberi 				IEEE80211_ACTION_CAT_SELF_PROT,
173259aa14a9SRui Paulo 				IEEE80211_ACTION_MESHPEERING_OPEN, args);
173359aa14a9SRui Paulo 				ni->ni_mlrcnt = 0;
173459aa14a9SRui Paulo 				mesh_peer_timeout_setup(ni);
1735b5df85a6SMonthadar Al Jaberi 				break;
1736b5df85a6SMonthadar Al Jaberi 			}
1737b5df85a6SMonthadar Al Jaberi 			case IEEE80211_NODE_MESH_ESTABLISHED:
1738b5df85a6SMonthadar Al Jaberi 			{
1739b5df85a6SMonthadar Al Jaberi 				/*
1740b5df85a6SMonthadar Al Jaberi 				 * Valid beacon from a peer mesh STA
1741b5df85a6SMonthadar Al Jaberi 				 * bump TA lifetime
1742b5df85a6SMonthadar Al Jaberi 				 */
1743b5df85a6SMonthadar Al Jaberi 				rt = ieee80211_mesh_rt_find(vap, wh->i_addr2);
1744b5df85a6SMonthadar Al Jaberi 				if(rt != NULL) {
1745b5df85a6SMonthadar Al Jaberi 					ieee80211_mesh_rt_update(rt,
17464f3a27aeSMonthadar Al Jaberi 					    ticks_to_msecs(
17474f3a27aeSMonthadar Al Jaberi 					    ms->ms_ppath->mpp_inact));
1748b5df85a6SMonthadar Al Jaberi 				}
1749b5df85a6SMonthadar Al Jaberi 				break;
1750b5df85a6SMonthadar Al Jaberi 			}
1751b5df85a6SMonthadar Al Jaberi 			default:
1752b5df85a6SMonthadar Al Jaberi 				break; /* ignore */
1753b5df85a6SMonthadar Al Jaberi 			}
175459aa14a9SRui Paulo 		}
175559aa14a9SRui Paulo 		break;
175659aa14a9SRui Paulo 	}
175759aa14a9SRui Paulo 	case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
175859aa14a9SRui Paulo 	{
175959aa14a9SRui Paulo 		uint8_t *ssid, *meshid, *rates, *xrates;
176059aa14a9SRui Paulo 		uint8_t *sfrm;
176159aa14a9SRui Paulo 
176259aa14a9SRui Paulo 		if (vap->iv_state != IEEE80211_S_RUN) {
176359aa14a9SRui Paulo 			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
176459aa14a9SRui Paulo 			    wh, NULL, "wrong state %s",
176559aa14a9SRui Paulo 			    ieee80211_state_name[vap->iv_state]);
176659aa14a9SRui Paulo 			vap->iv_stats.is_rx_mgtdiscard++;
176759aa14a9SRui Paulo 			return;
176859aa14a9SRui Paulo 		}
176959aa14a9SRui Paulo 		if (IEEE80211_IS_MULTICAST(wh->i_addr2)) {
177059aa14a9SRui Paulo 			/* frame must be directed */
177159aa14a9SRui Paulo 			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
177259aa14a9SRui Paulo 			    wh, NULL, "%s", "not unicast");
177359aa14a9SRui Paulo 			vap->iv_stats.is_rx_mgtdiscard++;	/* XXX stat */
177459aa14a9SRui Paulo 			return;
177559aa14a9SRui Paulo 		}
177659aa14a9SRui Paulo 		/*
177759aa14a9SRui Paulo 		 * prreq frame format
177859aa14a9SRui Paulo 		 *      [tlv] ssid
177959aa14a9SRui Paulo 		 *      [tlv] supported rates
178059aa14a9SRui Paulo 		 *      [tlv] extended supported rates
178159aa14a9SRui Paulo 		 *	[tlv] mesh id
178259aa14a9SRui Paulo 		 */
178359aa14a9SRui Paulo 		ssid = meshid = rates = xrates = NULL;
178459aa14a9SRui Paulo 		sfrm = frm;
178559aa14a9SRui Paulo 		while (efrm - frm > 1) {
178659aa14a9SRui Paulo 			IEEE80211_VERIFY_LENGTH(efrm - frm, frm[1] + 2, return);
178759aa14a9SRui Paulo 			switch (*frm) {
178859aa14a9SRui Paulo 			case IEEE80211_ELEMID_SSID:
178959aa14a9SRui Paulo 				ssid = frm;
179059aa14a9SRui Paulo 				break;
179159aa14a9SRui Paulo 			case IEEE80211_ELEMID_RATES:
179259aa14a9SRui Paulo 				rates = frm;
179359aa14a9SRui Paulo 				break;
179459aa14a9SRui Paulo 			case IEEE80211_ELEMID_XRATES:
179559aa14a9SRui Paulo 				xrates = frm;
179659aa14a9SRui Paulo 				break;
179759aa14a9SRui Paulo 			case IEEE80211_ELEMID_MESHID:
179859aa14a9SRui Paulo 				meshid = frm;
179959aa14a9SRui Paulo 				break;
180059aa14a9SRui Paulo 			}
1801d3befdecSBernhard Schmidt 			frm += frm[1] + 2;
180259aa14a9SRui Paulo 		}
180359aa14a9SRui Paulo 		IEEE80211_VERIFY_ELEMENT(ssid, IEEE80211_NWID_LEN, return);
180459aa14a9SRui Paulo 		IEEE80211_VERIFY_ELEMENT(rates, IEEE80211_RATE_MAXSIZE, return);
180559aa14a9SRui Paulo 		if (xrates != NULL)
180659aa14a9SRui Paulo 			IEEE80211_VERIFY_ELEMENT(xrates,
180759aa14a9SRui Paulo 			    IEEE80211_RATE_MAXSIZE - rates[1], return);
18086c946257SRui Paulo 		if (meshid != NULL) {
180959aa14a9SRui Paulo 			IEEE80211_VERIFY_ELEMENT(meshid,
181059aa14a9SRui Paulo 			    IEEE80211_MESHID_LEN, return);
181159aa14a9SRui Paulo 			/* NB: meshid, not ssid */
181259aa14a9SRui Paulo 			IEEE80211_VERIFY_SSID(vap->iv_bss, meshid, return);
18136c946257SRui Paulo 		}
181459aa14a9SRui Paulo 
181559aa14a9SRui Paulo 		/* XXX find a better class or define it's own */
181659aa14a9SRui Paulo 		IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_INPUT, wh->i_addr2,
181759aa14a9SRui Paulo 		    "%s", "recv probe req");
181859aa14a9SRui Paulo 		/*
181959aa14a9SRui Paulo 		 * Some legacy 11b clients cannot hack a complete
182059aa14a9SRui Paulo 		 * probe response frame.  When the request includes
182159aa14a9SRui Paulo 		 * only a bare-bones rate set, communicate this to
182259aa14a9SRui Paulo 		 * the transmit side.
182359aa14a9SRui Paulo 		 */
182459aa14a9SRui Paulo 		ieee80211_send_proberesp(vap, wh->i_addr2, 0);
182559aa14a9SRui Paulo 		break;
182659aa14a9SRui Paulo 	}
182796283082SBernhard Schmidt 
182859aa14a9SRui Paulo 	case IEEE80211_FC0_SUBTYPE_ACTION:
182996283082SBernhard Schmidt 	case IEEE80211_FC0_SUBTYPE_ACTION_NOACK:
183059aa14a9SRui Paulo 		if (ni == vap->iv_bss) {
1831893c4d6eSBernhard Schmidt 			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
183259aa14a9SRui Paulo 			    wh, NULL, "%s", "unknown node");
183359aa14a9SRui Paulo 			vap->iv_stats.is_rx_mgtdiscard++;
1834893c4d6eSBernhard Schmidt 		} else if (!IEEE80211_ADDR_EQ(vap->iv_myaddr, wh->i_addr1) &&
183559aa14a9SRui Paulo 		    !IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1836893c4d6eSBernhard Schmidt 			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
1837893c4d6eSBernhard Schmidt 			    wh, NULL, "%s", "not for us");
183859aa14a9SRui Paulo 			vap->iv_stats.is_rx_mgtdiscard++;
1839893c4d6eSBernhard Schmidt 		} else if (vap->iv_state != IEEE80211_S_RUN) {
184096283082SBernhard Schmidt 			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
184196283082SBernhard Schmidt 			    wh, NULL, "wrong state %s",
184296283082SBernhard Schmidt 			    ieee80211_state_name[vap->iv_state]);
184396283082SBernhard Schmidt 			vap->iv_stats.is_rx_mgtdiscard++;
1844893c4d6eSBernhard Schmidt 		} else {
1845893c4d6eSBernhard Schmidt 			if (ieee80211_parse_action(ni, m0) == 0)
1846893c4d6eSBernhard Schmidt 				(void)ic->ic_recv_action(ni, wh, frm, efrm);
184796283082SBernhard Schmidt 		}
184859aa14a9SRui Paulo 		break;
184996283082SBernhard Schmidt 
185059aa14a9SRui Paulo 	case IEEE80211_FC0_SUBTYPE_ASSOC_REQ:
185159aa14a9SRui Paulo 	case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
185296283082SBernhard Schmidt 	case IEEE80211_FC0_SUBTYPE_REASSOC_REQ:
185359aa14a9SRui Paulo 	case IEEE80211_FC0_SUBTYPE_REASSOC_RESP:
185496283082SBernhard Schmidt 	case IEEE80211_FC0_SUBTYPE_ATIM:
185559aa14a9SRui Paulo 	case IEEE80211_FC0_SUBTYPE_DISASSOC:
185696283082SBernhard Schmidt 	case IEEE80211_FC0_SUBTYPE_AUTH:
185796283082SBernhard Schmidt 	case IEEE80211_FC0_SUBTYPE_DEAUTH:
185859aa14a9SRui Paulo 		IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
185959aa14a9SRui Paulo 		    wh, NULL, "%s", "not handled");
186059aa14a9SRui Paulo 		vap->iv_stats.is_rx_mgtdiscard++;
186196283082SBernhard Schmidt 		break;
186296283082SBernhard Schmidt 
186359aa14a9SRui Paulo 	default:
186459aa14a9SRui Paulo 		IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
186559aa14a9SRui Paulo 		    wh, "mgt", "subtype 0x%x not handled", subtype);
186659aa14a9SRui Paulo 		vap->iv_stats.is_rx_badsubtype++;
186759aa14a9SRui Paulo 		break;
186859aa14a9SRui Paulo 	}
186959aa14a9SRui Paulo }
187059aa14a9SRui Paulo 
18710917631fSRui Paulo static void
18720917631fSRui Paulo mesh_recv_ctl(struct ieee80211_node *ni, struct mbuf *m, int subtype)
18730917631fSRui Paulo {
18740917631fSRui Paulo 
18750917631fSRui Paulo 	switch (subtype) {
18760917631fSRui Paulo 	case IEEE80211_FC0_SUBTYPE_BAR:
18770917631fSRui Paulo 		ieee80211_recv_bar(ni, m);
18780917631fSRui Paulo 		break;
18790917631fSRui Paulo 	}
18800917631fSRui Paulo }
18810917631fSRui Paulo 
188259aa14a9SRui Paulo /*
18836eb9b443SMonthadar Al Jaberi  * Parse meshpeering action ie's for MPM frames
188459aa14a9SRui Paulo  */
188559aa14a9SRui Paulo static const struct ieee80211_meshpeer_ie *
188659aa14a9SRui Paulo mesh_parse_meshpeering_action(struct ieee80211_node *ni,
188759aa14a9SRui Paulo 	const struct ieee80211_frame *wh,	/* XXX for VERIFY_LENGTH */
188859aa14a9SRui Paulo 	const uint8_t *frm, const uint8_t *efrm,
1889c77735e2SRui Paulo 	struct ieee80211_meshpeer_ie *mp, uint8_t subtype)
189059aa14a9SRui Paulo {
189159aa14a9SRui Paulo 	struct ieee80211vap *vap = ni->ni_vap;
189259aa14a9SRui Paulo 	const struct ieee80211_meshpeer_ie *mpie;
18936eb9b443SMonthadar Al Jaberi 	uint16_t args[3];
1894c77735e2SRui Paulo 	const uint8_t *meshid, *meshconf, *meshpeer;
18956eb9b443SMonthadar Al Jaberi 	uint8_t sendclose = 0; /* 1 = MPM frame rejected, close will be sent */
189659aa14a9SRui Paulo 
1897c77735e2SRui Paulo 	meshid = meshconf = meshpeer = NULL;
189859aa14a9SRui Paulo 	while (efrm - frm > 1) {
189959aa14a9SRui Paulo 		IEEE80211_VERIFY_LENGTH(efrm - frm, frm[1] + 2, return NULL);
190059aa14a9SRui Paulo 		switch (*frm) {
190159aa14a9SRui Paulo 		case IEEE80211_ELEMID_MESHID:
190259aa14a9SRui Paulo 			meshid = frm;
190359aa14a9SRui Paulo 			break;
190459aa14a9SRui Paulo 		case IEEE80211_ELEMID_MESHCONF:
190559aa14a9SRui Paulo 			meshconf = frm;
190659aa14a9SRui Paulo 			break;
190759aa14a9SRui Paulo 		case IEEE80211_ELEMID_MESHPEER:
190859aa14a9SRui Paulo 			meshpeer = frm;
190959aa14a9SRui Paulo 			mpie = (const struct ieee80211_meshpeer_ie *) frm;
191059aa14a9SRui Paulo 			memset(mp, 0, sizeof(*mp));
19116eb9b443SMonthadar Al Jaberi 			mp->peer_len = mpie->peer_len;
1912c2042c35SMonthadar Al Jaberi 			mp->peer_proto = LE_READ_2(&mpie->peer_proto);
191359aa14a9SRui Paulo 			mp->peer_llinkid = LE_READ_2(&mpie->peer_llinkid);
1914c2042c35SMonthadar Al Jaberi 			switch (subtype) {
1915c2042c35SMonthadar Al Jaberi 			case IEEE80211_ACTION_MESHPEERING_CONFIRM:
1916c2042c35SMonthadar Al Jaberi 				mp->peer_linkid =
1917c2042c35SMonthadar Al Jaberi 				    LE_READ_2(&mpie->peer_linkid);
1918c2042c35SMonthadar Al Jaberi 				break;
1919c2042c35SMonthadar Al Jaberi 			case IEEE80211_ACTION_MESHPEERING_CLOSE:
1920c2042c35SMonthadar Al Jaberi 				/* NB: peer link ID is optional */
1921c2042c35SMonthadar Al Jaberi 				if (mpie->peer_len ==
1922c2042c35SMonthadar Al Jaberi 				    (IEEE80211_MPM_BASE_SZ + 2)) {
192359aa14a9SRui Paulo 					mp->peer_linkid = 0;
1924c2042c35SMonthadar Al Jaberi 					mp->peer_rcode =
1925c2042c35SMonthadar Al Jaberi 					    LE_READ_2(&mpie->peer_linkid);
192659aa14a9SRui Paulo 				} else {
1927c2042c35SMonthadar Al Jaberi 					mp->peer_linkid =
1928c2042c35SMonthadar Al Jaberi 					    LE_READ_2(&mpie->peer_linkid);
1929c2042c35SMonthadar Al Jaberi 					mp->peer_rcode =
1930c2042c35SMonthadar Al Jaberi 					    LE_READ_2(&mpie->peer_rcode);
1931c2042c35SMonthadar Al Jaberi 				}
1932c2042c35SMonthadar Al Jaberi 				break;
193359aa14a9SRui Paulo 			}
193459aa14a9SRui Paulo 			break;
193559aa14a9SRui Paulo 		}
193659aa14a9SRui Paulo 		frm += frm[1] + 2;
193759aa14a9SRui Paulo 	}
193859aa14a9SRui Paulo 
193959aa14a9SRui Paulo 	/*
19406eb9b443SMonthadar Al Jaberi 	 * Verify the contents of the frame.
19416eb9b443SMonthadar Al Jaberi 	 * If it fails validation, close the peer link.
194259aa14a9SRui Paulo 	 */
19436eb9b443SMonthadar Al Jaberi 	if (mesh_verify_meshpeer(vap, subtype, (const uint8_t *)mp)) {
19446eb9b443SMonthadar Al Jaberi 		sendclose = 1;
19456eb9b443SMonthadar Al Jaberi 		IEEE80211_DISCARD(vap,
19466eb9b443SMonthadar Al Jaberi 		    IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH,
19476eb9b443SMonthadar Al Jaberi 		    wh, NULL, "%s", "MPM validation failed");
19486eb9b443SMonthadar Al Jaberi 	}
194959aa14a9SRui Paulo 
19506eb9b443SMonthadar Al Jaberi 	/* If meshid is not the same reject any frames type. */
19516eb9b443SMonthadar Al Jaberi 	if (sendclose == 0 && mesh_verify_meshid(vap, meshid)) {
19526eb9b443SMonthadar Al Jaberi 		sendclose = 1;
195359aa14a9SRui Paulo 		IEEE80211_DISCARD(vap,
195459aa14a9SRui Paulo 		    IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH,
195559aa14a9SRui Paulo 		    wh, NULL, "%s", "not for our mesh");
19566eb9b443SMonthadar Al Jaberi 		if (subtype == IEEE80211_ACTION_MESHPEERING_CLOSE) {
19576eb9b443SMonthadar Al Jaberi 			/*
19586eb9b443SMonthadar Al Jaberi 			 * Standard not clear about this, if we dont ignore
19596eb9b443SMonthadar Al Jaberi 			 * there will be an endless loop between nodes sending
19606eb9b443SMonthadar Al Jaberi 			 * CLOSE frames between each other with wrong meshid.
19616eb9b443SMonthadar Al Jaberi 			 * Discard and timers will bring FSM to IDLE state.
19626eb9b443SMonthadar Al Jaberi 			 */
19636eb9b443SMonthadar Al Jaberi 			return NULL;
19646eb9b443SMonthadar Al Jaberi 		}
19656eb9b443SMonthadar Al Jaberi 	}
19666eb9b443SMonthadar Al Jaberi 
19676eb9b443SMonthadar Al Jaberi 	/*
19686eb9b443SMonthadar Al Jaberi 	 * Close frames are accepted if meshid is the same.
19696eb9b443SMonthadar Al Jaberi 	 * Verify the other two types.
19706eb9b443SMonthadar Al Jaberi 	 */
19716eb9b443SMonthadar Al Jaberi 	if (sendclose == 0 && subtype != IEEE80211_ACTION_MESHPEERING_CLOSE &&
19726eb9b443SMonthadar Al Jaberi 	    mesh_verify_meshconf(vap, meshconf)) {
19736eb9b443SMonthadar Al Jaberi 		sendclose = 1;
19746eb9b443SMonthadar Al Jaberi 		IEEE80211_DISCARD(vap,
19756eb9b443SMonthadar Al Jaberi 		    IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH,
19766eb9b443SMonthadar Al Jaberi 		    wh, NULL, "%s", "configuration missmatch");
19776eb9b443SMonthadar Al Jaberi 	}
19786eb9b443SMonthadar Al Jaberi 
19796eb9b443SMonthadar Al Jaberi 	if (sendclose) {
198059aa14a9SRui Paulo 		vap->iv_stats.is_rx_mgtdiscard++;
198159aa14a9SRui Paulo 		switch (ni->ni_mlstate) {
198259aa14a9SRui Paulo 		case IEEE80211_NODE_MESH_IDLE:
198359aa14a9SRui Paulo 		case IEEE80211_NODE_MESH_ESTABLISHED:
198459aa14a9SRui Paulo 		case IEEE80211_NODE_MESH_HOLDING:
198559aa14a9SRui Paulo 			/* ignore */
198659aa14a9SRui Paulo 			break;
198759aa14a9SRui Paulo 		case IEEE80211_NODE_MESH_OPENSNT:
198859aa14a9SRui Paulo 		case IEEE80211_NODE_MESH_OPENRCV:
198959aa14a9SRui Paulo 		case IEEE80211_NODE_MESH_CONFIRMRCV:
199059aa14a9SRui Paulo 			args[0] = ni->ni_mlpid;
199159aa14a9SRui Paulo 			args[1] = ni->ni_mllid;
19926eb9b443SMonthadar Al Jaberi 			/* Reason codes for rejection */
19936eb9b443SMonthadar Al Jaberi 			switch (subtype) {
19946eb9b443SMonthadar Al Jaberi 			case IEEE80211_ACTION_MESHPEERING_OPEN:
19956eb9b443SMonthadar Al Jaberi 				args[2] = IEEE80211_REASON_MESH_CPVIOLATION;
19966eb9b443SMonthadar Al Jaberi 				break;
19976eb9b443SMonthadar Al Jaberi 			case IEEE80211_ACTION_MESHPEERING_CONFIRM:
19986eb9b443SMonthadar Al Jaberi 				args[2] = IEEE80211_REASON_MESH_INCONS_PARAMS;
19996eb9b443SMonthadar Al Jaberi 				break;
20006eb9b443SMonthadar Al Jaberi 			}
200159aa14a9SRui Paulo 			ieee80211_send_action(ni,
2002ebeaa1adSMonthadar Al Jaberi 			    IEEE80211_ACTION_CAT_SELF_PROT,
200359aa14a9SRui Paulo 			    IEEE80211_ACTION_MESHPEERING_CLOSE,
200459aa14a9SRui Paulo 			    args);
200559aa14a9SRui Paulo 			mesh_linkchange(ni, IEEE80211_NODE_MESH_HOLDING);
200659aa14a9SRui Paulo 			mesh_peer_timeout_setup(ni);
200759aa14a9SRui Paulo 			break;
200859aa14a9SRui Paulo 		}
200959aa14a9SRui Paulo 		return NULL;
201059aa14a9SRui Paulo 	}
20116eb9b443SMonthadar Al Jaberi 
201259aa14a9SRui Paulo 	return (const struct ieee80211_meshpeer_ie *) mp;
201359aa14a9SRui Paulo }
201459aa14a9SRui Paulo 
201559aa14a9SRui Paulo static int
201659aa14a9SRui Paulo mesh_recv_action_meshpeering_open(struct ieee80211_node *ni,
201759aa14a9SRui Paulo 	const struct ieee80211_frame *wh,
201859aa14a9SRui Paulo 	const uint8_t *frm, const uint8_t *efrm)
201959aa14a9SRui Paulo {
202059aa14a9SRui Paulo 	struct ieee80211vap *vap = ni->ni_vap;
20216eb9b443SMonthadar Al Jaberi 	struct ieee80211_mesh_state *ms = vap->iv_mesh;
202259aa14a9SRui Paulo 	struct ieee80211_meshpeer_ie ie;
202359aa14a9SRui Paulo 	const struct ieee80211_meshpeer_ie *meshpeer;
202459aa14a9SRui Paulo 	uint16_t args[3];
202559aa14a9SRui Paulo 
202659aa14a9SRui Paulo 	/* +2+2 for action + code + capabilites */
2027c77735e2SRui Paulo 	meshpeer = mesh_parse_meshpeering_action(ni, wh, frm+2+2, efrm, &ie,
2028c77735e2SRui Paulo 	    IEEE80211_ACTION_MESHPEERING_OPEN);
202959aa14a9SRui Paulo 	if (meshpeer == NULL) {
203059aa14a9SRui Paulo 		return 0;
203159aa14a9SRui Paulo 	}
203259aa14a9SRui Paulo 
203359aa14a9SRui Paulo 	/* XXX move up */
203459aa14a9SRui Paulo 	IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH, ni,
203559aa14a9SRui Paulo 	    "recv PEER OPEN, lid 0x%x", meshpeer->peer_llinkid);
203659aa14a9SRui Paulo 
203759aa14a9SRui Paulo 	switch (ni->ni_mlstate) {
203859aa14a9SRui Paulo 	case IEEE80211_NODE_MESH_IDLE:
20396eb9b443SMonthadar Al Jaberi 		/* Reject open request if reached our maximum neighbor count */
20406eb9b443SMonthadar Al Jaberi 		if (ms->ms_neighbors >= IEEE80211_MESH_MAX_NEIGHBORS) {
20416eb9b443SMonthadar Al Jaberi 			args[0] = meshpeer->peer_llinkid;
20426eb9b443SMonthadar Al Jaberi 			args[1] = 0;
20436eb9b443SMonthadar Al Jaberi 			args[2] = IEEE80211_REASON_MESH_MAX_PEERS;
20446eb9b443SMonthadar Al Jaberi 			ieee80211_send_action(ni,
20456eb9b443SMonthadar Al Jaberi 			    IEEE80211_ACTION_CAT_SELF_PROT,
20466eb9b443SMonthadar Al Jaberi 			    IEEE80211_ACTION_MESHPEERING_CLOSE,
20476eb9b443SMonthadar Al Jaberi 			    args);
20486eb9b443SMonthadar Al Jaberi 			/* stay in IDLE state */
20496eb9b443SMonthadar Al Jaberi 			return (0);
20506eb9b443SMonthadar Al Jaberi 		}
20516eb9b443SMonthadar Al Jaberi 		/* Open frame accepted */
205259aa14a9SRui Paulo 		mesh_linkchange(ni, IEEE80211_NODE_MESH_OPENRCV);
205359aa14a9SRui Paulo 		ni->ni_mllid = meshpeer->peer_llinkid;
205459aa14a9SRui Paulo 		ni->ni_mlpid = mesh_generateid(vap);
205559aa14a9SRui Paulo 		if (ni->ni_mlpid == 0)
205659aa14a9SRui Paulo 			return 0;		/* XXX */
205759aa14a9SRui Paulo 		args[0] = ni->ni_mlpid;
205859aa14a9SRui Paulo 		/* Announce we're open too... */
205959aa14a9SRui Paulo 		ieee80211_send_action(ni,
2060ebeaa1adSMonthadar Al Jaberi 		    IEEE80211_ACTION_CAT_SELF_PROT,
206159aa14a9SRui Paulo 		    IEEE80211_ACTION_MESHPEERING_OPEN, args);
206259aa14a9SRui Paulo 		/* ...and confirm the link. */
206359aa14a9SRui Paulo 		args[0] = ni->ni_mlpid;
206459aa14a9SRui Paulo 		args[1] = ni->ni_mllid;
206559aa14a9SRui Paulo 		ieee80211_send_action(ni,
2066ebeaa1adSMonthadar Al Jaberi 		    IEEE80211_ACTION_CAT_SELF_PROT,
206759aa14a9SRui Paulo 		    IEEE80211_ACTION_MESHPEERING_CONFIRM,
206859aa14a9SRui Paulo 		    args);
206959aa14a9SRui Paulo 		mesh_peer_timeout_setup(ni);
207059aa14a9SRui Paulo 		break;
207159aa14a9SRui Paulo 	case IEEE80211_NODE_MESH_OPENRCV:
207259aa14a9SRui Paulo 		/* Wrong Link ID */
207359aa14a9SRui Paulo 		if (ni->ni_mllid != meshpeer->peer_llinkid) {
207459aa14a9SRui Paulo 			args[0] = ni->ni_mllid;
207559aa14a9SRui Paulo 			args[1] = ni->ni_mlpid;
207659aa14a9SRui Paulo 			args[2] = IEEE80211_REASON_PEER_LINK_CANCELED;
207759aa14a9SRui Paulo 			ieee80211_send_action(ni,
2078ebeaa1adSMonthadar Al Jaberi 			    IEEE80211_ACTION_CAT_SELF_PROT,
207959aa14a9SRui Paulo 			    IEEE80211_ACTION_MESHPEERING_CLOSE,
208059aa14a9SRui Paulo 			    args);
208159aa14a9SRui Paulo 			mesh_linkchange(ni, IEEE80211_NODE_MESH_HOLDING);
208259aa14a9SRui Paulo 			mesh_peer_timeout_setup(ni);
208359aa14a9SRui Paulo 			break;
208459aa14a9SRui Paulo 		}
208559aa14a9SRui Paulo 		/* Duplicate open, confirm again. */
208659aa14a9SRui Paulo 		args[0] = ni->ni_mlpid;
208759aa14a9SRui Paulo 		args[1] = ni->ni_mllid;
208859aa14a9SRui Paulo 		ieee80211_send_action(ni,
2089ebeaa1adSMonthadar Al Jaberi 		    IEEE80211_ACTION_CAT_SELF_PROT,
209059aa14a9SRui Paulo 		    IEEE80211_ACTION_MESHPEERING_CONFIRM,
209159aa14a9SRui Paulo 		    args);
209259aa14a9SRui Paulo 		break;
209359aa14a9SRui Paulo 	case IEEE80211_NODE_MESH_OPENSNT:
209459aa14a9SRui Paulo 		ni->ni_mllid = meshpeer->peer_llinkid;
209559aa14a9SRui Paulo 		mesh_linkchange(ni, IEEE80211_NODE_MESH_OPENRCV);
209659aa14a9SRui Paulo 		args[0] = ni->ni_mlpid;
209759aa14a9SRui Paulo 		args[1] = ni->ni_mllid;
209859aa14a9SRui Paulo 		ieee80211_send_action(ni,
2099ebeaa1adSMonthadar Al Jaberi 		    IEEE80211_ACTION_CAT_SELF_PROT,
210059aa14a9SRui Paulo 		    IEEE80211_ACTION_MESHPEERING_CONFIRM,
210159aa14a9SRui Paulo 		    args);
210259aa14a9SRui Paulo 		/* NB: don't setup/clear any timeout */
210359aa14a9SRui Paulo 		break;
210459aa14a9SRui Paulo 	case IEEE80211_NODE_MESH_CONFIRMRCV:
210559aa14a9SRui Paulo 		if (ni->ni_mlpid != meshpeer->peer_linkid ||
210659aa14a9SRui Paulo 		    ni->ni_mllid != meshpeer->peer_llinkid) {
210759aa14a9SRui Paulo 			args[0] = ni->ni_mlpid;
210859aa14a9SRui Paulo 			args[1] = ni->ni_mllid;
210959aa14a9SRui Paulo 			args[2] = IEEE80211_REASON_PEER_LINK_CANCELED;
211059aa14a9SRui Paulo 			ieee80211_send_action(ni,
2111ebeaa1adSMonthadar Al Jaberi 			    IEEE80211_ACTION_CAT_SELF_PROT,
211259aa14a9SRui Paulo 			    IEEE80211_ACTION_MESHPEERING_CLOSE,
211359aa14a9SRui Paulo 			    args);
211459aa14a9SRui Paulo 			mesh_linkchange(ni,
211559aa14a9SRui Paulo 			    IEEE80211_NODE_MESH_HOLDING);
211659aa14a9SRui Paulo 			mesh_peer_timeout_setup(ni);
211759aa14a9SRui Paulo 			break;
211859aa14a9SRui Paulo 		}
211959aa14a9SRui Paulo 		mesh_linkchange(ni, IEEE80211_NODE_MESH_ESTABLISHED);
212059aa14a9SRui Paulo 		ni->ni_mllid = meshpeer->peer_llinkid;
212159aa14a9SRui Paulo 		args[0] = ni->ni_mlpid;
212259aa14a9SRui Paulo 		args[1] = ni->ni_mllid;
212359aa14a9SRui Paulo 		ieee80211_send_action(ni,
2124ebeaa1adSMonthadar Al Jaberi 		    IEEE80211_ACTION_CAT_SELF_PROT,
212559aa14a9SRui Paulo 		    IEEE80211_ACTION_MESHPEERING_CONFIRM,
212659aa14a9SRui Paulo 		    args);
212759aa14a9SRui Paulo 		mesh_peer_timeout_stop(ni);
212859aa14a9SRui Paulo 		break;
212959aa14a9SRui Paulo 	case IEEE80211_NODE_MESH_ESTABLISHED:
213059aa14a9SRui Paulo 		if (ni->ni_mllid != meshpeer->peer_llinkid) {
213159aa14a9SRui Paulo 			args[0] = ni->ni_mllid;
213259aa14a9SRui Paulo 			args[1] = ni->ni_mlpid;
213359aa14a9SRui Paulo 			args[2] = IEEE80211_REASON_PEER_LINK_CANCELED;
213459aa14a9SRui Paulo 			ieee80211_send_action(ni,
2135ebeaa1adSMonthadar Al Jaberi 			    IEEE80211_ACTION_CAT_SELF_PROT,
213659aa14a9SRui Paulo 			    IEEE80211_ACTION_MESHPEERING_CLOSE,
213759aa14a9SRui Paulo 			    args);
213859aa14a9SRui Paulo 			mesh_linkchange(ni, IEEE80211_NODE_MESH_HOLDING);
213959aa14a9SRui Paulo 			mesh_peer_timeout_setup(ni);
214059aa14a9SRui Paulo 			break;
214159aa14a9SRui Paulo 		}
214259aa14a9SRui Paulo 		args[0] = ni->ni_mlpid;
214359aa14a9SRui Paulo 		args[1] = ni->ni_mllid;
214459aa14a9SRui Paulo 		ieee80211_send_action(ni,
2145ebeaa1adSMonthadar Al Jaberi 		    IEEE80211_ACTION_CAT_SELF_PROT,
214659aa14a9SRui Paulo 		    IEEE80211_ACTION_MESHPEERING_CONFIRM,
214759aa14a9SRui Paulo 		    args);
214859aa14a9SRui Paulo 		break;
214959aa14a9SRui Paulo 	case IEEE80211_NODE_MESH_HOLDING:
215059aa14a9SRui Paulo 		args[0] = ni->ni_mlpid;
215159aa14a9SRui Paulo 		args[1] = meshpeer->peer_llinkid;
21526eb9b443SMonthadar Al Jaberi 		/* Standard not clear about what the reaason code should be */
21536eb9b443SMonthadar Al Jaberi 		args[2] = IEEE80211_REASON_PEER_LINK_CANCELED;
215459aa14a9SRui Paulo 		ieee80211_send_action(ni,
2155ebeaa1adSMonthadar Al Jaberi 		    IEEE80211_ACTION_CAT_SELF_PROT,
215659aa14a9SRui Paulo 		    IEEE80211_ACTION_MESHPEERING_CLOSE,
215759aa14a9SRui Paulo 		    args);
215859aa14a9SRui Paulo 		break;
215959aa14a9SRui Paulo 	}
216059aa14a9SRui Paulo 	return 0;
216159aa14a9SRui Paulo }
216259aa14a9SRui Paulo 
216359aa14a9SRui Paulo static int
216459aa14a9SRui Paulo mesh_recv_action_meshpeering_confirm(struct ieee80211_node *ni,
216559aa14a9SRui Paulo 	const struct ieee80211_frame *wh,
216659aa14a9SRui Paulo 	const uint8_t *frm, const uint8_t *efrm)
216759aa14a9SRui Paulo {
216859aa14a9SRui Paulo 	struct ieee80211vap *vap = ni->ni_vap;
216959aa14a9SRui Paulo 	struct ieee80211_meshpeer_ie ie;
217059aa14a9SRui Paulo 	const struct ieee80211_meshpeer_ie *meshpeer;
217159aa14a9SRui Paulo 	uint16_t args[3];
217259aa14a9SRui Paulo 
217359aa14a9SRui Paulo 	/* +2+2+2+2 for action + code + capabilites + status code + AID */
2174c77735e2SRui Paulo 	meshpeer = mesh_parse_meshpeering_action(ni, wh, frm+2+2+2+2, efrm, &ie,
2175c77735e2SRui Paulo 	    IEEE80211_ACTION_MESHPEERING_CONFIRM);
217659aa14a9SRui Paulo 	if (meshpeer == NULL) {
217759aa14a9SRui Paulo 		return 0;
217859aa14a9SRui Paulo 	}
217959aa14a9SRui Paulo 
218059aa14a9SRui Paulo 	IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH, ni,
218159aa14a9SRui Paulo 	    "recv PEER CONFIRM, local id 0x%x, peer id 0x%x",
218259aa14a9SRui Paulo 	    meshpeer->peer_llinkid, meshpeer->peer_linkid);
218359aa14a9SRui Paulo 
218459aa14a9SRui Paulo 	switch (ni->ni_mlstate) {
218559aa14a9SRui Paulo 	case IEEE80211_NODE_MESH_OPENRCV:
218659aa14a9SRui Paulo 		mesh_linkchange(ni, IEEE80211_NODE_MESH_ESTABLISHED);
218759aa14a9SRui Paulo 		mesh_peer_timeout_stop(ni);
218859aa14a9SRui Paulo 		break;
218959aa14a9SRui Paulo 	case IEEE80211_NODE_MESH_OPENSNT:
219059aa14a9SRui Paulo 		mesh_linkchange(ni, IEEE80211_NODE_MESH_CONFIRMRCV);
21916eb9b443SMonthadar Al Jaberi 		mesh_peer_timeout_setup(ni);
219259aa14a9SRui Paulo 		break;
219359aa14a9SRui Paulo 	case IEEE80211_NODE_MESH_HOLDING:
219459aa14a9SRui Paulo 		args[0] = ni->ni_mlpid;
219559aa14a9SRui Paulo 		args[1] = meshpeer->peer_llinkid;
21966eb9b443SMonthadar Al Jaberi 		/* Standard not clear about what the reaason code should be */
21976eb9b443SMonthadar Al Jaberi 		args[2] = IEEE80211_REASON_PEER_LINK_CANCELED;
219859aa14a9SRui Paulo 		ieee80211_send_action(ni,
2199ebeaa1adSMonthadar Al Jaberi 		    IEEE80211_ACTION_CAT_SELF_PROT,
220059aa14a9SRui Paulo 		    IEEE80211_ACTION_MESHPEERING_CLOSE,
220159aa14a9SRui Paulo 		    args);
220259aa14a9SRui Paulo 		break;
220359aa14a9SRui Paulo 	case IEEE80211_NODE_MESH_CONFIRMRCV:
220459aa14a9SRui Paulo 		if (ni->ni_mllid != meshpeer->peer_llinkid) {
220559aa14a9SRui Paulo 			args[0] = ni->ni_mlpid;
220659aa14a9SRui Paulo 			args[1] = ni->ni_mllid;
220759aa14a9SRui Paulo 			args[2] = IEEE80211_REASON_PEER_LINK_CANCELED;
220859aa14a9SRui Paulo 			ieee80211_send_action(ni,
2209ebeaa1adSMonthadar Al Jaberi 			    IEEE80211_ACTION_CAT_SELF_PROT,
221059aa14a9SRui Paulo 			    IEEE80211_ACTION_MESHPEERING_CLOSE,
221159aa14a9SRui Paulo 			    args);
221259aa14a9SRui Paulo 			mesh_linkchange(ni, IEEE80211_NODE_MESH_HOLDING);
221359aa14a9SRui Paulo 			mesh_peer_timeout_setup(ni);
221459aa14a9SRui Paulo 		}
221559aa14a9SRui Paulo 		break;
221659aa14a9SRui Paulo 	default:
221759aa14a9SRui Paulo 		IEEE80211_DISCARD(vap,
221859aa14a9SRui Paulo 		    IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH,
221959aa14a9SRui Paulo 		    wh, NULL, "received confirm in invalid state %d",
222059aa14a9SRui Paulo 		    ni->ni_mlstate);
222159aa14a9SRui Paulo 		vap->iv_stats.is_rx_mgtdiscard++;
222259aa14a9SRui Paulo 		break;
222359aa14a9SRui Paulo 	}
222459aa14a9SRui Paulo 	return 0;
222559aa14a9SRui Paulo }
222659aa14a9SRui Paulo 
222759aa14a9SRui Paulo static int
222859aa14a9SRui Paulo mesh_recv_action_meshpeering_close(struct ieee80211_node *ni,
222959aa14a9SRui Paulo 	const struct ieee80211_frame *wh,
223059aa14a9SRui Paulo 	const uint8_t *frm, const uint8_t *efrm)
223159aa14a9SRui Paulo {
22326eb9b443SMonthadar Al Jaberi 	struct ieee80211_meshpeer_ie ie;
22336eb9b443SMonthadar Al Jaberi 	const struct ieee80211_meshpeer_ie *meshpeer;
223459aa14a9SRui Paulo 	uint16_t args[3];
223559aa14a9SRui Paulo 
22366eb9b443SMonthadar Al Jaberi 	/* +2 for action + code */
22376eb9b443SMonthadar Al Jaberi 	meshpeer = mesh_parse_meshpeering_action(ni, wh, frm+2, efrm, &ie,
22386eb9b443SMonthadar Al Jaberi 	    IEEE80211_ACTION_MESHPEERING_CLOSE);
22396eb9b443SMonthadar Al Jaberi 	if (meshpeer == NULL) {
22406eb9b443SMonthadar Al Jaberi 		return 0;
22416eb9b443SMonthadar Al Jaberi 	}
22426eb9b443SMonthadar Al Jaberi 
22436eb9b443SMonthadar Al Jaberi 	/*
22446eb9b443SMonthadar Al Jaberi 	 * XXX: check reason code, for example we could receive
22456eb9b443SMonthadar Al Jaberi 	 * IEEE80211_REASON_MESH_MAX_PEERS then we should not attempt
22466eb9b443SMonthadar Al Jaberi 	 * to peer again.
22476eb9b443SMonthadar Al Jaberi 	 */
22486eb9b443SMonthadar Al Jaberi 
224959aa14a9SRui Paulo 	IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH,
225059aa14a9SRui Paulo 	    ni, "%s", "recv PEER CLOSE");
225159aa14a9SRui Paulo 
225259aa14a9SRui Paulo 	switch (ni->ni_mlstate) {
225359aa14a9SRui Paulo 	case IEEE80211_NODE_MESH_IDLE:
225459aa14a9SRui Paulo 		/* ignore */
225559aa14a9SRui Paulo 		break;
225659aa14a9SRui Paulo 	case IEEE80211_NODE_MESH_OPENRCV:
225759aa14a9SRui Paulo 	case IEEE80211_NODE_MESH_OPENSNT:
225859aa14a9SRui Paulo 	case IEEE80211_NODE_MESH_CONFIRMRCV:
225959aa14a9SRui Paulo 	case IEEE80211_NODE_MESH_ESTABLISHED:
226059aa14a9SRui Paulo 		args[0] = ni->ni_mlpid;
226159aa14a9SRui Paulo 		args[1] = ni->ni_mllid;
226259aa14a9SRui Paulo 		args[2] = IEEE80211_REASON_MESH_CLOSE_RCVD;
226359aa14a9SRui Paulo 		ieee80211_send_action(ni,
2264ebeaa1adSMonthadar Al Jaberi 		    IEEE80211_ACTION_CAT_SELF_PROT,
226559aa14a9SRui Paulo 		    IEEE80211_ACTION_MESHPEERING_CLOSE,
226659aa14a9SRui Paulo 		    args);
226759aa14a9SRui Paulo 		mesh_linkchange(ni, IEEE80211_NODE_MESH_HOLDING);
226859aa14a9SRui Paulo 		mesh_peer_timeout_setup(ni);
226959aa14a9SRui Paulo 		break;
227059aa14a9SRui Paulo 	case IEEE80211_NODE_MESH_HOLDING:
227159aa14a9SRui Paulo 		mesh_linkchange(ni, IEEE80211_NODE_MESH_IDLE);
22726eb9b443SMonthadar Al Jaberi 		mesh_peer_timeout_stop(ni);
227359aa14a9SRui Paulo 		break;
227459aa14a9SRui Paulo 	}
227559aa14a9SRui Paulo 	return 0;
227659aa14a9SRui Paulo }
227759aa14a9SRui Paulo 
227859aa14a9SRui Paulo /*
227959aa14a9SRui Paulo  * Link Metric handling.
228059aa14a9SRui Paulo  */
228159aa14a9SRui Paulo static int
2282bdd2a076SAdrian Chadd mesh_recv_action_meshlmetric(struct ieee80211_node *ni,
228359aa14a9SRui Paulo 	const struct ieee80211_frame *wh,
228459aa14a9SRui Paulo 	const uint8_t *frm, const uint8_t *efrm)
228559aa14a9SRui Paulo {
2286bdd2a076SAdrian Chadd 	const struct ieee80211_meshlmetric_ie *ie =
2287bdd2a076SAdrian Chadd 	    (const struct ieee80211_meshlmetric_ie *)
2288bdd2a076SAdrian Chadd 	    (frm+2); /* action + code */
2289bdd2a076SAdrian Chadd 	struct ieee80211_meshlmetric_ie lm_rep;
229059aa14a9SRui Paulo 
2291bdd2a076SAdrian Chadd 	if (ie->lm_flags & IEEE80211_MESH_LMETRIC_FLAGS_REQ) {
2292bdd2a076SAdrian Chadd 		lm_rep.lm_flags = 0;
2293bdd2a076SAdrian Chadd 		lm_rep.lm_metric = mesh_airtime_calc(ni);
229459aa14a9SRui Paulo 		ieee80211_send_action(ni,
2295bdd2a076SAdrian Chadd 		    IEEE80211_ACTION_CAT_MESH,
2296bdd2a076SAdrian Chadd 		    IEEE80211_ACTION_MESH_LMETRIC,
2297bdd2a076SAdrian Chadd 		    &lm_rep);
229859aa14a9SRui Paulo 	}
2299bdd2a076SAdrian Chadd 	/* XXX: else do nothing for now */
230059aa14a9SRui Paulo 	return 0;
230159aa14a9SRui Paulo }
230259aa14a9SRui Paulo 
230359aa14a9SRui Paulo static int
230459aa14a9SRui Paulo mesh_send_action(struct ieee80211_node *ni, struct mbuf *m)
230559aa14a9SRui Paulo {
230659aa14a9SRui Paulo 	struct ieee80211_bpf_params params;
230759aa14a9SRui Paulo 
230859aa14a9SRui Paulo 	memset(&params, 0, sizeof(params));
230959aa14a9SRui Paulo 	params.ibp_pri = WME_AC_VO;
231059aa14a9SRui Paulo 	params.ibp_rate0 = ni->ni_txparms->mgmtrate;
231159aa14a9SRui Paulo 	/* XXX ucast/mcast */
231259aa14a9SRui Paulo 	params.ibp_try0 = ni->ni_txparms->maxretry;
231359aa14a9SRui Paulo 	params.ibp_power = ni->ni_txpower;
231459aa14a9SRui Paulo 	return ieee80211_mgmt_output(ni, m, IEEE80211_FC0_SUBTYPE_ACTION,
231559aa14a9SRui Paulo 	     &params);
231659aa14a9SRui Paulo }
231759aa14a9SRui Paulo 
231859aa14a9SRui Paulo #define	ADDSHORT(frm, v) do {			\
231959aa14a9SRui Paulo 	frm[0] = (v) & 0xff;			\
232059aa14a9SRui Paulo 	frm[1] = (v) >> 8;			\
232159aa14a9SRui Paulo 	frm += 2;				\
232259aa14a9SRui Paulo } while (0)
232359aa14a9SRui Paulo #define	ADDWORD(frm, v) do {			\
232459aa14a9SRui Paulo 	frm[0] = (v) & 0xff;			\
232559aa14a9SRui Paulo 	frm[1] = ((v) >> 8) & 0xff;		\
232659aa14a9SRui Paulo 	frm[2] = ((v) >> 16) & 0xff;		\
232759aa14a9SRui Paulo 	frm[3] = ((v) >> 24) & 0xff;		\
232859aa14a9SRui Paulo 	frm += 4;				\
232959aa14a9SRui Paulo } while (0)
233059aa14a9SRui Paulo 
233159aa14a9SRui Paulo static int
233259aa14a9SRui Paulo mesh_send_action_meshpeering_open(struct ieee80211_node *ni,
233359aa14a9SRui Paulo 	int category, int action, void *args0)
233459aa14a9SRui Paulo {
233559aa14a9SRui Paulo 	struct ieee80211vap *vap = ni->ni_vap;
233659aa14a9SRui Paulo 	struct ieee80211com *ic = ni->ni_ic;
233759aa14a9SRui Paulo 	uint16_t *args = args0;
233859aa14a9SRui Paulo 	const struct ieee80211_rateset *rs;
233959aa14a9SRui Paulo 	struct mbuf *m;
234059aa14a9SRui Paulo 	uint8_t *frm;
234159aa14a9SRui Paulo 
234259aa14a9SRui Paulo 	IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH, ni,
234359aa14a9SRui Paulo 	    "send PEER OPEN action: localid 0x%x", args[0]);
234459aa14a9SRui Paulo 
234559aa14a9SRui Paulo 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
234659aa14a9SRui Paulo 	    "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", __func__, __LINE__,
234759aa14a9SRui Paulo 	    ni, ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)+1);
234859aa14a9SRui Paulo 	ieee80211_ref_node(ni);
234959aa14a9SRui Paulo 
235059aa14a9SRui Paulo 	m = ieee80211_getmgtframe(&frm,
235159aa14a9SRui Paulo 	    ic->ic_headroom + sizeof(struct ieee80211_frame),
235259aa14a9SRui Paulo 	    sizeof(uint16_t)	/* action+category */
235359aa14a9SRui Paulo 	    + sizeof(uint16_t)	/* capabilites */
235459aa14a9SRui Paulo 	    + 2 + IEEE80211_RATE_SIZE
235559aa14a9SRui Paulo 	    + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
235659aa14a9SRui Paulo 	    + 2 + IEEE80211_MESHID_LEN
235759aa14a9SRui Paulo 	    + sizeof(struct ieee80211_meshconf_ie)
235859aa14a9SRui Paulo 	    + sizeof(struct ieee80211_meshpeer_ie)
235959aa14a9SRui Paulo 	);
236059aa14a9SRui Paulo 	if (m != NULL) {
236159aa14a9SRui Paulo 		/*
236259aa14a9SRui Paulo 		 * mesh peer open action frame format:
236359aa14a9SRui Paulo 		 *   [1] category
236459aa14a9SRui Paulo 		 *   [1] action
236559aa14a9SRui Paulo 		 *   [2] capabilities
236659aa14a9SRui Paulo 		 *   [tlv] rates
236759aa14a9SRui Paulo 		 *   [tlv] xrates
236859aa14a9SRui Paulo 		 *   [tlv] mesh id
236959aa14a9SRui Paulo 		 *   [tlv] mesh conf
237059aa14a9SRui Paulo 		 *   [tlv] mesh peer link mgmt
237159aa14a9SRui Paulo 		 */
237259aa14a9SRui Paulo 		*frm++ = category;
237359aa14a9SRui Paulo 		*frm++ = action;
237459aa14a9SRui Paulo 		ADDSHORT(frm, ieee80211_getcapinfo(vap, ni->ni_chan));
237559aa14a9SRui Paulo 		rs = ieee80211_get_suprates(ic, ic->ic_curchan);
237659aa14a9SRui Paulo 		frm = ieee80211_add_rates(frm, rs);
237759aa14a9SRui Paulo 		frm = ieee80211_add_xrates(frm, rs);
237859aa14a9SRui Paulo 		frm = ieee80211_add_meshid(frm, vap);
237959aa14a9SRui Paulo 		frm = ieee80211_add_meshconf(frm, vap);
2380ebeaa1adSMonthadar Al Jaberi 		frm = ieee80211_add_meshpeer(frm, IEEE80211_ACTION_MESHPEERING_OPEN,
238159aa14a9SRui Paulo 		    args[0], 0, 0);
238259aa14a9SRui Paulo 		m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
238359aa14a9SRui Paulo 		return mesh_send_action(ni, m);
238459aa14a9SRui Paulo 	} else {
238559aa14a9SRui Paulo 		vap->iv_stats.is_tx_nobuf++;
238659aa14a9SRui Paulo 		ieee80211_free_node(ni);
238759aa14a9SRui Paulo 		return ENOMEM;
238859aa14a9SRui Paulo 	}
238959aa14a9SRui Paulo }
239059aa14a9SRui Paulo 
239159aa14a9SRui Paulo static int
239259aa14a9SRui Paulo mesh_send_action_meshpeering_confirm(struct ieee80211_node *ni,
239359aa14a9SRui Paulo 	int category, int action, void *args0)
239459aa14a9SRui Paulo {
239559aa14a9SRui Paulo 	struct ieee80211vap *vap = ni->ni_vap;
239659aa14a9SRui Paulo 	struct ieee80211com *ic = ni->ni_ic;
239759aa14a9SRui Paulo 	uint16_t *args = args0;
239859aa14a9SRui Paulo 	const struct ieee80211_rateset *rs;
239959aa14a9SRui Paulo 	struct mbuf *m;
240059aa14a9SRui Paulo 	uint8_t *frm;
240159aa14a9SRui Paulo 
240259aa14a9SRui Paulo 	IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH, ni,
240359aa14a9SRui Paulo 	    "send PEER CONFIRM action: localid 0x%x, peerid 0x%x",
240459aa14a9SRui Paulo 	    args[0], args[1]);
240559aa14a9SRui Paulo 
240659aa14a9SRui Paulo 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
240759aa14a9SRui Paulo 	    "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", __func__, __LINE__,
240859aa14a9SRui Paulo 	    ni, ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)+1);
240959aa14a9SRui Paulo 	ieee80211_ref_node(ni);
241059aa14a9SRui Paulo 
241159aa14a9SRui Paulo 	m = ieee80211_getmgtframe(&frm,
241259aa14a9SRui Paulo 	    ic->ic_headroom + sizeof(struct ieee80211_frame),
241359aa14a9SRui Paulo 	    sizeof(uint16_t)	/* action+category */
241459aa14a9SRui Paulo 	    + sizeof(uint16_t)	/* capabilites */
241559aa14a9SRui Paulo 	    + sizeof(uint16_t)	/* status code */
241659aa14a9SRui Paulo 	    + sizeof(uint16_t)	/* AID */
241759aa14a9SRui Paulo 	    + 2 + IEEE80211_RATE_SIZE
241859aa14a9SRui Paulo 	    + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
241959aa14a9SRui Paulo 	    + 2 + IEEE80211_MESHID_LEN
242059aa14a9SRui Paulo 	    + sizeof(struct ieee80211_meshconf_ie)
242159aa14a9SRui Paulo 	    + sizeof(struct ieee80211_meshpeer_ie)
242259aa14a9SRui Paulo 	);
242359aa14a9SRui Paulo 	if (m != NULL) {
242459aa14a9SRui Paulo 		/*
242559aa14a9SRui Paulo 		 * mesh peer confirm action frame format:
242659aa14a9SRui Paulo 		 *   [1] category
242759aa14a9SRui Paulo 		 *   [1] action
242859aa14a9SRui Paulo 		 *   [2] capabilities
242959aa14a9SRui Paulo 		 *   [2] status code
243059aa14a9SRui Paulo 		 *   [2] association id (peer ID)
243159aa14a9SRui Paulo 		 *   [tlv] rates
243259aa14a9SRui Paulo 		 *   [tlv] xrates
243359aa14a9SRui Paulo 		 *   [tlv] mesh id
243459aa14a9SRui Paulo 		 *   [tlv] mesh conf
243559aa14a9SRui Paulo 		 *   [tlv] mesh peer link mgmt
243659aa14a9SRui Paulo 		 */
243759aa14a9SRui Paulo 		*frm++ = category;
243859aa14a9SRui Paulo 		*frm++ = action;
243959aa14a9SRui Paulo 		ADDSHORT(frm, ieee80211_getcapinfo(vap, ni->ni_chan));
244059aa14a9SRui Paulo 		ADDSHORT(frm, 0);		/* status code */
244159aa14a9SRui Paulo 		ADDSHORT(frm, args[1]);		/* AID */
244259aa14a9SRui Paulo 		rs = ieee80211_get_suprates(ic, ic->ic_curchan);
244359aa14a9SRui Paulo 		frm = ieee80211_add_rates(frm, rs);
244459aa14a9SRui Paulo 		frm = ieee80211_add_xrates(frm, rs);
244559aa14a9SRui Paulo 		frm = ieee80211_add_meshid(frm, vap);
244659aa14a9SRui Paulo 		frm = ieee80211_add_meshconf(frm, vap);
244759aa14a9SRui Paulo 		frm = ieee80211_add_meshpeer(frm,
2448ebeaa1adSMonthadar Al Jaberi 		    IEEE80211_ACTION_MESHPEERING_CONFIRM,
244959aa14a9SRui Paulo 		    args[0], args[1], 0);
245059aa14a9SRui Paulo 		m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
245159aa14a9SRui Paulo 		return mesh_send_action(ni, m);
245259aa14a9SRui Paulo 	} else {
245359aa14a9SRui Paulo 		vap->iv_stats.is_tx_nobuf++;
245459aa14a9SRui Paulo 		ieee80211_free_node(ni);
245559aa14a9SRui Paulo 		return ENOMEM;
245659aa14a9SRui Paulo 	}
245759aa14a9SRui Paulo }
245859aa14a9SRui Paulo 
245959aa14a9SRui Paulo static int
246059aa14a9SRui Paulo mesh_send_action_meshpeering_close(struct ieee80211_node *ni,
246159aa14a9SRui Paulo 	int category, int action, void *args0)
246259aa14a9SRui Paulo {
246359aa14a9SRui Paulo 	struct ieee80211vap *vap = ni->ni_vap;
246459aa14a9SRui Paulo 	struct ieee80211com *ic = ni->ni_ic;
246559aa14a9SRui Paulo 	uint16_t *args = args0;
246659aa14a9SRui Paulo 	struct mbuf *m;
246759aa14a9SRui Paulo 	uint8_t *frm;
246859aa14a9SRui Paulo 
246959aa14a9SRui Paulo 	IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH, ni,
247059aa14a9SRui Paulo 	    "send PEER CLOSE action: localid 0x%x, peerid 0x%x reason %d",
247159aa14a9SRui Paulo 	    args[0], args[1], args[2]);
247259aa14a9SRui Paulo 
247359aa14a9SRui Paulo 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
247459aa14a9SRui Paulo 	    "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", __func__, __LINE__,
247559aa14a9SRui Paulo 	    ni, ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)+1);
247659aa14a9SRui Paulo 	ieee80211_ref_node(ni);
247759aa14a9SRui Paulo 
247859aa14a9SRui Paulo 	m = ieee80211_getmgtframe(&frm,
247959aa14a9SRui Paulo 	    ic->ic_headroom + sizeof(struct ieee80211_frame),
248059aa14a9SRui Paulo 	    sizeof(uint16_t)	/* action+category */
248159aa14a9SRui Paulo 	    + sizeof(uint16_t)	/* reason code */
248259aa14a9SRui Paulo 	    + 2 + IEEE80211_MESHID_LEN
248359aa14a9SRui Paulo 	    + sizeof(struct ieee80211_meshpeer_ie)
248459aa14a9SRui Paulo 	);
248559aa14a9SRui Paulo 	if (m != NULL) {
248659aa14a9SRui Paulo 		/*
248759aa14a9SRui Paulo 		 * mesh peer close action frame format:
248859aa14a9SRui Paulo 		 *   [1] category
248959aa14a9SRui Paulo 		 *   [1] action
249059aa14a9SRui Paulo 		 *   [tlv] mesh id
249159aa14a9SRui Paulo 		 *   [tlv] mesh peer link mgmt
249259aa14a9SRui Paulo 		 */
249359aa14a9SRui Paulo 		*frm++ = category;
249459aa14a9SRui Paulo 		*frm++ = action;
249559aa14a9SRui Paulo 		frm = ieee80211_add_meshid(frm, vap);
249659aa14a9SRui Paulo 		frm = ieee80211_add_meshpeer(frm,
2497ebeaa1adSMonthadar Al Jaberi 		    IEEE80211_ACTION_MESHPEERING_CLOSE,
249859aa14a9SRui Paulo 		    args[0], args[1], args[2]);
249959aa14a9SRui Paulo 		m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
250059aa14a9SRui Paulo 		return mesh_send_action(ni, m);
250159aa14a9SRui Paulo 	} else {
250259aa14a9SRui Paulo 		vap->iv_stats.is_tx_nobuf++;
250359aa14a9SRui Paulo 		ieee80211_free_node(ni);
250459aa14a9SRui Paulo 		return ENOMEM;
250559aa14a9SRui Paulo 	}
250659aa14a9SRui Paulo }
250759aa14a9SRui Paulo 
250859aa14a9SRui Paulo static int
2509bdd2a076SAdrian Chadd mesh_send_action_meshlmetric(struct ieee80211_node *ni,
251059aa14a9SRui Paulo 	int category, int action, void *arg0)
251159aa14a9SRui Paulo {
251259aa14a9SRui Paulo 	struct ieee80211vap *vap = ni->ni_vap;
251359aa14a9SRui Paulo 	struct ieee80211com *ic = ni->ni_ic;
2514bdd2a076SAdrian Chadd 	struct ieee80211_meshlmetric_ie *ie = arg0;
251559aa14a9SRui Paulo 	struct mbuf *m;
251659aa14a9SRui Paulo 	uint8_t *frm;
251759aa14a9SRui Paulo 
2518bdd2a076SAdrian Chadd 	if (ie->lm_flags & IEEE80211_MESH_LMETRIC_FLAGS_REQ) {
2519bdd2a076SAdrian Chadd 		IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH,
2520bdd2a076SAdrian Chadd 		    ni, "%s", "send LINK METRIC REQUEST action");
252159aa14a9SRui Paulo 	} else {
2522bdd2a076SAdrian Chadd 		IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH,
2523bdd2a076SAdrian Chadd 		    ni, "send LINK METRIC REPLY action: metric 0x%x",
2524bdd2a076SAdrian Chadd 		    ie->lm_metric);
252559aa14a9SRui Paulo 	}
252659aa14a9SRui Paulo 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
252759aa14a9SRui Paulo 	    "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", __func__, __LINE__,
252859aa14a9SRui Paulo 	    ni, ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)+1);
252959aa14a9SRui Paulo 	ieee80211_ref_node(ni);
253059aa14a9SRui Paulo 
253159aa14a9SRui Paulo 	m = ieee80211_getmgtframe(&frm,
253259aa14a9SRui Paulo 	    ic->ic_headroom + sizeof(struct ieee80211_frame),
2533bdd2a076SAdrian Chadd 	    sizeof(uint16_t) +	/* action+category */
2534bdd2a076SAdrian Chadd 	    sizeof(struct ieee80211_meshlmetric_ie)
253559aa14a9SRui Paulo 	);
253659aa14a9SRui Paulo 	if (m != NULL) {
253759aa14a9SRui Paulo 		/*
2538bdd2a076SAdrian Chadd 		 * mesh link metric
253959aa14a9SRui Paulo 		 *   [1] category
254059aa14a9SRui Paulo 		 *   [1] action
254159aa14a9SRui Paulo 		 *   [tlv] mesh link metric
254259aa14a9SRui Paulo 		 */
254359aa14a9SRui Paulo 		*frm++ = category;
254459aa14a9SRui Paulo 		*frm++ = action;
2545bdd2a076SAdrian Chadd 		frm = ieee80211_add_meshlmetric(frm,
2546bdd2a076SAdrian Chadd 		    ie->lm_flags, ie->lm_metric);
254759aa14a9SRui Paulo 		m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
254859aa14a9SRui Paulo 		return mesh_send_action(ni, m);
254959aa14a9SRui Paulo 	} else {
255059aa14a9SRui Paulo 		vap->iv_stats.is_tx_nobuf++;
255159aa14a9SRui Paulo 		ieee80211_free_node(ni);
255259aa14a9SRui Paulo 		return ENOMEM;
255359aa14a9SRui Paulo 	}
255459aa14a9SRui Paulo }
255559aa14a9SRui Paulo 
255659aa14a9SRui Paulo static void
255759aa14a9SRui Paulo mesh_peer_timeout_setup(struct ieee80211_node *ni)
255859aa14a9SRui Paulo {
255959aa14a9SRui Paulo 	switch (ni->ni_mlstate) {
256059aa14a9SRui Paulo 	case IEEE80211_NODE_MESH_HOLDING:
256159aa14a9SRui Paulo 		ni->ni_mltval = ieee80211_mesh_holdingtimeout;
256259aa14a9SRui Paulo 		break;
256359aa14a9SRui Paulo 	case IEEE80211_NODE_MESH_CONFIRMRCV:
256459aa14a9SRui Paulo 		ni->ni_mltval = ieee80211_mesh_confirmtimeout;
256559aa14a9SRui Paulo 		break;
256659aa14a9SRui Paulo 	case IEEE80211_NODE_MESH_IDLE:
256759aa14a9SRui Paulo 		ni->ni_mltval = 0;
256859aa14a9SRui Paulo 		break;
256959aa14a9SRui Paulo 	default:
257059aa14a9SRui Paulo 		ni->ni_mltval = ieee80211_mesh_retrytimeout;
257159aa14a9SRui Paulo 		break;
257259aa14a9SRui Paulo 	}
257359aa14a9SRui Paulo 	if (ni->ni_mltval)
257459aa14a9SRui Paulo 		callout_reset(&ni->ni_mltimer, ni->ni_mltval,
257559aa14a9SRui Paulo 		    mesh_peer_timeout_cb, ni);
257659aa14a9SRui Paulo }
257759aa14a9SRui Paulo 
257859aa14a9SRui Paulo /*
257959aa14a9SRui Paulo  * Same as above but backoffs timer statisically 50%.
258059aa14a9SRui Paulo  */
258159aa14a9SRui Paulo static void
258259aa14a9SRui Paulo mesh_peer_timeout_backoff(struct ieee80211_node *ni)
258359aa14a9SRui Paulo {
258459aa14a9SRui Paulo 	uint32_t r;
258559aa14a9SRui Paulo 
258659aa14a9SRui Paulo 	r = arc4random();
258759aa14a9SRui Paulo 	ni->ni_mltval += r % ni->ni_mltval;
258859aa14a9SRui Paulo 	callout_reset(&ni->ni_mltimer, ni->ni_mltval, mesh_peer_timeout_cb,
258959aa14a9SRui Paulo 	    ni);
259059aa14a9SRui Paulo }
259159aa14a9SRui Paulo 
259259aa14a9SRui Paulo static __inline void
259359aa14a9SRui Paulo mesh_peer_timeout_stop(struct ieee80211_node *ni)
259459aa14a9SRui Paulo {
2595c104cff2SRui Paulo 	callout_drain(&ni->ni_mltimer);
259659aa14a9SRui Paulo }
259759aa14a9SRui Paulo 
2598ff17492fSMonthadar Al Jaberi static void
2599ff17492fSMonthadar Al Jaberi mesh_peer_backoff_cb(void *arg)
2600ff17492fSMonthadar Al Jaberi {
2601ff17492fSMonthadar Al Jaberi 	struct ieee80211_node *ni = (struct ieee80211_node *)arg;
2602ff17492fSMonthadar Al Jaberi 
2603ff17492fSMonthadar Al Jaberi 	/* After backoff timeout, try to peer automatically again. */
2604ff17492fSMonthadar Al Jaberi 	ni->ni_mlhcnt = 0;
2605ff17492fSMonthadar Al Jaberi }
2606ff17492fSMonthadar Al Jaberi 
260759aa14a9SRui Paulo /*
260859aa14a9SRui Paulo  * Mesh Peer Link Management FSM timeout handling.
260959aa14a9SRui Paulo  */
261059aa14a9SRui Paulo static void
261159aa14a9SRui Paulo mesh_peer_timeout_cb(void *arg)
261259aa14a9SRui Paulo {
261359aa14a9SRui Paulo 	struct ieee80211_node *ni = (struct ieee80211_node *)arg;
261459aa14a9SRui Paulo 	uint16_t args[3];
261559aa14a9SRui Paulo 
261659aa14a9SRui Paulo 	IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_MESH,
261759aa14a9SRui Paulo 	    ni, "mesh link timeout, state %d, retry counter %d",
261859aa14a9SRui Paulo 	    ni->ni_mlstate, ni->ni_mlrcnt);
261959aa14a9SRui Paulo 
262059aa14a9SRui Paulo 	switch (ni->ni_mlstate) {
262159aa14a9SRui Paulo 	case IEEE80211_NODE_MESH_IDLE:
262259aa14a9SRui Paulo 	case IEEE80211_NODE_MESH_ESTABLISHED:
262359aa14a9SRui Paulo 		break;
262459aa14a9SRui Paulo 	case IEEE80211_NODE_MESH_OPENSNT:
262559aa14a9SRui Paulo 	case IEEE80211_NODE_MESH_OPENRCV:
262659aa14a9SRui Paulo 		if (ni->ni_mlrcnt == ieee80211_mesh_maxretries) {
262759aa14a9SRui Paulo 			args[0] = ni->ni_mlpid;
262859aa14a9SRui Paulo 			args[2] = IEEE80211_REASON_MESH_MAX_RETRIES;
262959aa14a9SRui Paulo 			ieee80211_send_action(ni,
2630ebeaa1adSMonthadar Al Jaberi 			    IEEE80211_ACTION_CAT_SELF_PROT,
263159aa14a9SRui Paulo 			    IEEE80211_ACTION_MESHPEERING_CLOSE, args);
263259aa14a9SRui Paulo 			ni->ni_mlrcnt = 0;
263359aa14a9SRui Paulo 			mesh_linkchange(ni, IEEE80211_NODE_MESH_HOLDING);
263459aa14a9SRui Paulo 			mesh_peer_timeout_setup(ni);
263559aa14a9SRui Paulo 		} else {
263659aa14a9SRui Paulo 			args[0] = ni->ni_mlpid;
263759aa14a9SRui Paulo 			ieee80211_send_action(ni,
2638ebeaa1adSMonthadar Al Jaberi 			    IEEE80211_ACTION_CAT_SELF_PROT,
263959aa14a9SRui Paulo 			    IEEE80211_ACTION_MESHPEERING_OPEN, args);
264059aa14a9SRui Paulo 			ni->ni_mlrcnt++;
264159aa14a9SRui Paulo 			mesh_peer_timeout_backoff(ni);
264259aa14a9SRui Paulo 		}
264359aa14a9SRui Paulo 		break;
264459aa14a9SRui Paulo 	case IEEE80211_NODE_MESH_CONFIRMRCV:
264559aa14a9SRui Paulo 		args[0] = ni->ni_mlpid;
264659aa14a9SRui Paulo 		args[2] = IEEE80211_REASON_MESH_CONFIRM_TIMEOUT;
264759aa14a9SRui Paulo 		ieee80211_send_action(ni,
2648ebeaa1adSMonthadar Al Jaberi 		    IEEE80211_ACTION_CAT_SELF_PROT,
264959aa14a9SRui Paulo 		    IEEE80211_ACTION_MESHPEERING_CLOSE, args);
265059aa14a9SRui Paulo 		mesh_linkchange(ni, IEEE80211_NODE_MESH_HOLDING);
265159aa14a9SRui Paulo 		mesh_peer_timeout_setup(ni);
265259aa14a9SRui Paulo 		break;
265359aa14a9SRui Paulo 	case IEEE80211_NODE_MESH_HOLDING:
2654ff17492fSMonthadar Al Jaberi 		ni->ni_mlhcnt++;
2655ff17492fSMonthadar Al Jaberi 		if (ni->ni_mlhcnt >= ieee80211_mesh_maxholding)
2656ff17492fSMonthadar Al Jaberi 			callout_reset(&ni->ni_mlhtimer,
2657ff17492fSMonthadar Al Jaberi 			    ieee80211_mesh_backofftimeout,
2658ff17492fSMonthadar Al Jaberi 			    mesh_peer_backoff_cb, ni);
265959aa14a9SRui Paulo 		mesh_linkchange(ni, IEEE80211_NODE_MESH_IDLE);
266059aa14a9SRui Paulo 		break;
266159aa14a9SRui Paulo 	}
266259aa14a9SRui Paulo }
266359aa14a9SRui Paulo 
2664aec0289dSRui Paulo static int
266559aa14a9SRui Paulo mesh_verify_meshid(struct ieee80211vap *vap, const uint8_t *ie)
266659aa14a9SRui Paulo {
266759aa14a9SRui Paulo 	struct ieee80211_mesh_state *ms = vap->iv_mesh;
266859aa14a9SRui Paulo 
266959aa14a9SRui Paulo 	if (ie == NULL || ie[1] != ms->ms_idlen)
267059aa14a9SRui Paulo 		return 1;
267159aa14a9SRui Paulo 	return memcmp(ms->ms_id, ie + 2, ms->ms_idlen);
267259aa14a9SRui Paulo }
267359aa14a9SRui Paulo 
267459aa14a9SRui Paulo /*
267559aa14a9SRui Paulo  * Check if we are using the same algorithms for this mesh.
267659aa14a9SRui Paulo  */
267759aa14a9SRui Paulo static int
267859aa14a9SRui Paulo mesh_verify_meshconf(struct ieee80211vap *vap, const uint8_t *ie)
267959aa14a9SRui Paulo {
268059aa14a9SRui Paulo 	const struct ieee80211_meshconf_ie *meshconf =
268159aa14a9SRui Paulo 	    (const struct ieee80211_meshconf_ie *) ie;
268259aa14a9SRui Paulo 	const struct ieee80211_mesh_state *ms = vap->iv_mesh;
268359aa14a9SRui Paulo 
268459aa14a9SRui Paulo 	if (meshconf == NULL)
268559aa14a9SRui Paulo 		return 1;
26866b8c1829SRui Paulo 	if (meshconf->conf_pselid != ms->ms_ppath->mpp_ie) {
268759aa14a9SRui Paulo 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_MESH,
26886b8c1829SRui Paulo 		    "unknown path selection algorithm: 0x%x\n",
26896b8c1829SRui Paulo 		    meshconf->conf_pselid);
269059aa14a9SRui Paulo 		return 1;
269159aa14a9SRui Paulo 	}
26926b8c1829SRui Paulo 	if (meshconf->conf_pmetid != ms->ms_pmetric->mpm_ie) {
269359aa14a9SRui Paulo 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_MESH,
26946b8c1829SRui Paulo 		    "unknown path metric algorithm: 0x%x\n",
26956b8c1829SRui Paulo 		    meshconf->conf_pmetid);
269659aa14a9SRui Paulo 		return 1;
269759aa14a9SRui Paulo 	}
26986b8c1829SRui Paulo 	if (meshconf->conf_ccid != 0) {
269959aa14a9SRui Paulo 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_MESH,
27006b8c1829SRui Paulo 		    "unknown congestion control algorithm: 0x%x\n",
27016b8c1829SRui Paulo 		    meshconf->conf_ccid);
270259aa14a9SRui Paulo 		return 1;
270359aa14a9SRui Paulo 	}
27046b8c1829SRui Paulo 	if (meshconf->conf_syncid != IEEE80211_MESHCONF_SYNC_NEIGHOFF) {
270559aa14a9SRui Paulo 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_MESH,
27066b8c1829SRui Paulo 		    "unknown sync algorithm: 0x%x\n",
27076b8c1829SRui Paulo 		    meshconf->conf_syncid);
270859aa14a9SRui Paulo 		return 1;
270959aa14a9SRui Paulo 	}
27106b8c1829SRui Paulo 	if (meshconf->conf_authid != 0) {
271159aa14a9SRui Paulo 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_MESH,
27126b8c1829SRui Paulo 		    "unknown auth auth algorithm: 0x%x\n",
27136b8c1829SRui Paulo 		    meshconf->conf_pselid);
271459aa14a9SRui Paulo 		return 1;
271559aa14a9SRui Paulo 	}
271659aa14a9SRui Paulo 	/* Not accepting peers */
271781b95c2cSAdrian Chadd 	if (!(meshconf->conf_cap & IEEE80211_MESHCONF_CAP_AP)) {
271859aa14a9SRui Paulo 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_MESH,
271959aa14a9SRui Paulo 		    "not accepting peers: 0x%x\n", meshconf->conf_cap);
272059aa14a9SRui Paulo 		return 1;
272159aa14a9SRui Paulo 	}
272259aa14a9SRui Paulo 	return 0;
272359aa14a9SRui Paulo }
272459aa14a9SRui Paulo 
272559aa14a9SRui Paulo static int
2726c77735e2SRui Paulo mesh_verify_meshpeer(struct ieee80211vap *vap, uint8_t subtype,
2727c77735e2SRui Paulo     const uint8_t *ie)
272859aa14a9SRui Paulo {
272959aa14a9SRui Paulo 	const struct ieee80211_meshpeer_ie *meshpeer =
273059aa14a9SRui Paulo 	    (const struct ieee80211_meshpeer_ie *) ie;
273159aa14a9SRui Paulo 
2732c2042c35SMonthadar Al Jaberi 	if (meshpeer == NULL ||
2733c2042c35SMonthadar Al Jaberi 	    meshpeer->peer_len < IEEE80211_MPM_BASE_SZ ||
2734c2042c35SMonthadar Al Jaberi 	    meshpeer->peer_len > IEEE80211_MPM_MAX_SZ)
273559aa14a9SRui Paulo 		return 1;
2736c2042c35SMonthadar Al Jaberi 	if (meshpeer->peer_proto != IEEE80211_MPPID_MPM) {
2737c2042c35SMonthadar Al Jaberi 		IEEE80211_DPRINTF(vap,
2738c2042c35SMonthadar Al Jaberi 		    IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH,
2739c2042c35SMonthadar Al Jaberi 		    "Only MPM protocol is supported (proto: 0x%02X)",
2740c2042c35SMonthadar Al Jaberi 		    meshpeer->peer_proto);
2741c2042c35SMonthadar Al Jaberi 		return 1;
2742c2042c35SMonthadar Al Jaberi 	}
2743c77735e2SRui Paulo 	switch (subtype) {
2744ebeaa1adSMonthadar Al Jaberi 	case IEEE80211_ACTION_MESHPEERING_OPEN:
2745c2042c35SMonthadar Al Jaberi 		if (meshpeer->peer_len != IEEE80211_MPM_BASE_SZ)
274659aa14a9SRui Paulo 			return 1;
274759aa14a9SRui Paulo 		break;
2748ebeaa1adSMonthadar Al Jaberi 	case IEEE80211_ACTION_MESHPEERING_CONFIRM:
2749c2042c35SMonthadar Al Jaberi 		if (meshpeer->peer_len != IEEE80211_MPM_BASE_SZ + 2)
275059aa14a9SRui Paulo 			return 1;
275159aa14a9SRui Paulo 		break;
2752ebeaa1adSMonthadar Al Jaberi 	case IEEE80211_ACTION_MESHPEERING_CLOSE:
2753c2042c35SMonthadar Al Jaberi 		if (meshpeer->peer_len < IEEE80211_MPM_BASE_SZ + 2)
275459aa14a9SRui Paulo 			return 1;
2755c2042c35SMonthadar Al Jaberi 		if (meshpeer->peer_len == (IEEE80211_MPM_BASE_SZ + 2) &&
2756c2042c35SMonthadar Al Jaberi 		    meshpeer->peer_linkid != 0)
275759aa14a9SRui Paulo 			return 1;
275859aa14a9SRui Paulo 		if (meshpeer->peer_rcode == 0)
275959aa14a9SRui Paulo 			return 1;
276059aa14a9SRui Paulo 		break;
276159aa14a9SRui Paulo 	}
276259aa14a9SRui Paulo 	return 0;
276359aa14a9SRui Paulo }
276459aa14a9SRui Paulo 
276559aa14a9SRui Paulo /*
276659aa14a9SRui Paulo  * Add a Mesh ID IE to a frame.
276759aa14a9SRui Paulo  */
276859aa14a9SRui Paulo uint8_t *
276959aa14a9SRui Paulo ieee80211_add_meshid(uint8_t *frm, struct ieee80211vap *vap)
277059aa14a9SRui Paulo {
277159aa14a9SRui Paulo 	struct ieee80211_mesh_state *ms = vap->iv_mesh;
277259aa14a9SRui Paulo 
277359aa14a9SRui Paulo 	KASSERT(vap->iv_opmode == IEEE80211_M_MBSS, ("not a mbss vap"));
277459aa14a9SRui Paulo 
277559aa14a9SRui Paulo 	*frm++ = IEEE80211_ELEMID_MESHID;
277659aa14a9SRui Paulo 	*frm++ = ms->ms_idlen;
277759aa14a9SRui Paulo 	memcpy(frm, ms->ms_id, ms->ms_idlen);
277859aa14a9SRui Paulo 	return frm + ms->ms_idlen;
277959aa14a9SRui Paulo }
278059aa14a9SRui Paulo 
278159aa14a9SRui Paulo /*
278259aa14a9SRui Paulo  * Add a Mesh Configuration IE to a frame.
278359aa14a9SRui Paulo  * For now just use HWMP routing, Airtime link metric, Null Congestion
278459aa14a9SRui Paulo  * Signaling, Null Sync Protocol and Null Authentication.
278559aa14a9SRui Paulo  */
278659aa14a9SRui Paulo uint8_t *
278759aa14a9SRui Paulo ieee80211_add_meshconf(uint8_t *frm, struct ieee80211vap *vap)
278859aa14a9SRui Paulo {
278959aa14a9SRui Paulo 	const struct ieee80211_mesh_state *ms = vap->iv_mesh;
2790651e41a4SRui Paulo 	uint16_t caps;
279159aa14a9SRui Paulo 
279259aa14a9SRui Paulo 	KASSERT(vap->iv_opmode == IEEE80211_M_MBSS, ("not a MBSS vap"));
279359aa14a9SRui Paulo 
279459aa14a9SRui Paulo 	*frm++ = IEEE80211_ELEMID_MESHCONF;
279581b95c2cSAdrian Chadd 	*frm++ = IEEE80211_MESH_CONF_SZ;
27966b8c1829SRui Paulo 	*frm++ = ms->ms_ppath->mpp_ie;		/* path selection */
27976b8c1829SRui Paulo 	*frm++ = ms->ms_pmetric->mpm_ie;	/* link metric */
27986b8c1829SRui Paulo 	*frm++ = IEEE80211_MESHCONF_CC_DISABLED;
27996b8c1829SRui Paulo 	*frm++ = IEEE80211_MESHCONF_SYNC_NEIGHOFF;
28006b8c1829SRui Paulo 	*frm++ = IEEE80211_MESHCONF_AUTH_DISABLED;
280159aa14a9SRui Paulo 	/* NB: set the number of neighbors before the rest */
28026eb9b443SMonthadar Al Jaberi 	*frm = (ms->ms_neighbors > IEEE80211_MESH_MAX_NEIGHBORS ?
28036eb9b443SMonthadar Al Jaberi 	    IEEE80211_MESH_MAX_NEIGHBORS : ms->ms_neighbors) << 1;
28049fc85253SMonthadar Al Jaberi 	if (ms->ms_flags & IEEE80211_MESHFLAGS_GATE)
28059fc85253SMonthadar Al Jaberi 		*frm |= IEEE80211_MESHCONF_FORM_GATE;
280659aa14a9SRui Paulo 	frm += 1;
2807651e41a4SRui Paulo 	caps = 0;
280859aa14a9SRui Paulo 	if (ms->ms_flags & IEEE80211_MESHFLAGS_AP)
2809651e41a4SRui Paulo 		caps |= IEEE80211_MESHCONF_CAP_AP;
281059aa14a9SRui Paulo 	if (ms->ms_flags & IEEE80211_MESHFLAGS_FWD)
2811651e41a4SRui Paulo 		caps |= IEEE80211_MESHCONF_CAP_FWRD;
281281b95c2cSAdrian Chadd 	*frm++ = caps;
281359aa14a9SRui Paulo 	return frm;
281459aa14a9SRui Paulo }
281559aa14a9SRui Paulo 
281659aa14a9SRui Paulo /*
281759aa14a9SRui Paulo  * Add a Mesh Peer Management IE to a frame.
281859aa14a9SRui Paulo  */
281959aa14a9SRui Paulo uint8_t *
282059aa14a9SRui Paulo ieee80211_add_meshpeer(uint8_t *frm, uint8_t subtype, uint16_t localid,
282159aa14a9SRui Paulo     uint16_t peerid, uint16_t reason)
282259aa14a9SRui Paulo {
2823c77735e2SRui Paulo 
282459aa14a9SRui Paulo 	KASSERT(localid != 0, ("localid == 0"));
282559aa14a9SRui Paulo 
282659aa14a9SRui Paulo 	*frm++ = IEEE80211_ELEMID_MESHPEER;
282759aa14a9SRui Paulo 	switch (subtype) {
2828ebeaa1adSMonthadar Al Jaberi 	case IEEE80211_ACTION_MESHPEERING_OPEN:
2829c2042c35SMonthadar Al Jaberi 		*frm++ = IEEE80211_MPM_BASE_SZ;		/* length */
2830c2042c35SMonthadar Al Jaberi 		ADDSHORT(frm, IEEE80211_MPPID_MPM);	/* proto */
283159aa14a9SRui Paulo 		ADDSHORT(frm, localid);			/* local ID */
283259aa14a9SRui Paulo 		break;
2833ebeaa1adSMonthadar Al Jaberi 	case IEEE80211_ACTION_MESHPEERING_CONFIRM:
283459aa14a9SRui Paulo 		KASSERT(peerid != 0, ("sending peer confirm without peer id"));
2835c2042c35SMonthadar Al Jaberi 		*frm++ = IEEE80211_MPM_BASE_SZ + 2;	/* length */
2836c2042c35SMonthadar Al Jaberi 		ADDSHORT(frm, IEEE80211_MPPID_MPM);	/* proto */
283759aa14a9SRui Paulo 		ADDSHORT(frm, localid);			/* local ID */
283859aa14a9SRui Paulo 		ADDSHORT(frm, peerid);			/* peer ID */
283959aa14a9SRui Paulo 		break;
2840ebeaa1adSMonthadar Al Jaberi 	case IEEE80211_ACTION_MESHPEERING_CLOSE:
284159aa14a9SRui Paulo 		if (peerid)
2842c2042c35SMonthadar Al Jaberi 			*frm++ = IEEE80211_MPM_MAX_SZ;	/* length */
284359aa14a9SRui Paulo 		else
2844c2042c35SMonthadar Al Jaberi 			*frm++ = IEEE80211_MPM_BASE_SZ + 2; /* length */
2845c2042c35SMonthadar Al Jaberi 		ADDSHORT(frm, IEEE80211_MPPID_MPM);	/* proto */
284659aa14a9SRui Paulo 		ADDSHORT(frm, localid);	/* local ID */
284759aa14a9SRui Paulo 		if (peerid)
284859aa14a9SRui Paulo 			ADDSHORT(frm, peerid);	/* peer ID */
284959aa14a9SRui Paulo 		ADDSHORT(frm, reason);
285059aa14a9SRui Paulo 		break;
285159aa14a9SRui Paulo 	}
285259aa14a9SRui Paulo 	return frm;
285359aa14a9SRui Paulo }
285459aa14a9SRui Paulo 
285559aa14a9SRui Paulo /*
285659aa14a9SRui Paulo  * Compute an Airtime Link Metric for the link with this node.
285759aa14a9SRui Paulo  *
285859aa14a9SRui Paulo  * Based on Draft 3.0 spec (11B.10, p.149).
285959aa14a9SRui Paulo  */
286059aa14a9SRui Paulo /*
286159aa14a9SRui Paulo  * Max 802.11s overhead.
286259aa14a9SRui Paulo  */
286359aa14a9SRui Paulo #define IEEE80211_MESH_MAXOVERHEAD \
286459aa14a9SRui Paulo 	(sizeof(struct ieee80211_qosframe_addr4) \
28653c314f6dSMonthadar Al Jaberi 	 + sizeof(struct ieee80211_meshcntl_ae10) \
286659aa14a9SRui Paulo 	+ sizeof(struct llc) \
286759aa14a9SRui Paulo 	+ IEEE80211_ADDR_LEN \
286859aa14a9SRui Paulo 	+ IEEE80211_WEP_IVLEN \
286959aa14a9SRui Paulo 	+ IEEE80211_WEP_KIDLEN \
287059aa14a9SRui Paulo 	+ IEEE80211_WEP_CRCLEN \
287159aa14a9SRui Paulo 	+ IEEE80211_WEP_MICLEN \
287259aa14a9SRui Paulo 	+ IEEE80211_CRC_LEN)
287359aa14a9SRui Paulo uint32_t
287459aa14a9SRui Paulo mesh_airtime_calc(struct ieee80211_node *ni)
287559aa14a9SRui Paulo {
287659aa14a9SRui Paulo #define M_BITS 8
287759aa14a9SRui Paulo #define S_FACTOR (2 * M_BITS)
287859aa14a9SRui Paulo 	struct ieee80211com *ic = ni->ni_ic;
287959aa14a9SRui Paulo 	struct ifnet *ifp = ni->ni_vap->iv_ifp;
288059aa14a9SRui Paulo 	const static int nbits = 8192 << M_BITS;
288159aa14a9SRui Paulo 	uint32_t overhead, rate, errrate;
288259aa14a9SRui Paulo 	uint64_t res;
288359aa14a9SRui Paulo 
288459aa14a9SRui Paulo 	/* Time to transmit a frame */
288559aa14a9SRui Paulo 	rate = ni->ni_txrate;
288659aa14a9SRui Paulo 	overhead = ieee80211_compute_duration(ic->ic_rt,
288759aa14a9SRui Paulo 	    ifp->if_mtu + IEEE80211_MESH_MAXOVERHEAD, rate, 0) << M_BITS;
288859aa14a9SRui Paulo 	/* Error rate in percentage */
288959aa14a9SRui Paulo 	/* XXX assuming small failures are ok */
289059aa14a9SRui Paulo 	errrate = (((ifp->if_oerrors +
289159aa14a9SRui Paulo 	    ifp->if_ierrors) / 100) << M_BITS) / 100;
289259aa14a9SRui Paulo 	res = (overhead + (nbits / rate)) *
289359aa14a9SRui Paulo 	    ((1 << S_FACTOR) / ((1 << M_BITS) - errrate));
289459aa14a9SRui Paulo 
289559aa14a9SRui Paulo 	return (uint32_t)(res >> S_FACTOR);
289659aa14a9SRui Paulo #undef M_BITS
289759aa14a9SRui Paulo #undef S_FACTOR
289859aa14a9SRui Paulo }
289959aa14a9SRui Paulo 
290059aa14a9SRui Paulo /*
290159aa14a9SRui Paulo  * Add a Mesh Link Metric report IE to a frame.
290259aa14a9SRui Paulo  */
290359aa14a9SRui Paulo uint8_t *
2904bdd2a076SAdrian Chadd ieee80211_add_meshlmetric(uint8_t *frm, uint8_t flags, uint32_t metric)
290559aa14a9SRui Paulo {
290659aa14a9SRui Paulo 	*frm++ = IEEE80211_ELEMID_MESHLINK;
2907bdd2a076SAdrian Chadd 	*frm++ = 5;
2908bdd2a076SAdrian Chadd 	*frm++ = flags;
290959aa14a9SRui Paulo 	ADDWORD(frm, metric);
291059aa14a9SRui Paulo 	return frm;
291159aa14a9SRui Paulo }
291259aa14a9SRui Paulo #undef ADDSHORT
291359aa14a9SRui Paulo #undef ADDWORD
291459aa14a9SRui Paulo 
291559aa14a9SRui Paulo /*
291659aa14a9SRui Paulo  * Initialize any mesh-specific node state.
291759aa14a9SRui Paulo  */
291859aa14a9SRui Paulo void
291959aa14a9SRui Paulo ieee80211_mesh_node_init(struct ieee80211vap *vap, struct ieee80211_node *ni)
292059aa14a9SRui Paulo {
292159aa14a9SRui Paulo 	ni->ni_flags |= IEEE80211_NODE_QOS;
292259aa14a9SRui Paulo 	callout_init(&ni->ni_mltimer, CALLOUT_MPSAFE);
2923ff17492fSMonthadar Al Jaberi 	callout_init(&ni->ni_mlhtimer, CALLOUT_MPSAFE);
292459aa14a9SRui Paulo }
292559aa14a9SRui Paulo 
292659aa14a9SRui Paulo /*
292759aa14a9SRui Paulo  * Cleanup any mesh-specific node state.
292859aa14a9SRui Paulo  */
292959aa14a9SRui Paulo void
293059aa14a9SRui Paulo ieee80211_mesh_node_cleanup(struct ieee80211_node *ni)
293159aa14a9SRui Paulo {
293259aa14a9SRui Paulo 	struct ieee80211vap *vap = ni->ni_vap;
293359aa14a9SRui Paulo 	struct ieee80211_mesh_state *ms = vap->iv_mesh;
293459aa14a9SRui Paulo 
293559aa14a9SRui Paulo 	callout_drain(&ni->ni_mltimer);
2936ff17492fSMonthadar Al Jaberi 	callout_drain(&ni->ni_mlhtimer);
293759aa14a9SRui Paulo 	/* NB: short-circuit callbacks after mesh_vdetach */
293859aa14a9SRui Paulo 	if (vap->iv_mesh != NULL)
293959aa14a9SRui Paulo 		ms->ms_ppath->mpp_peerdown(ni);
294059aa14a9SRui Paulo }
294159aa14a9SRui Paulo 
294259aa14a9SRui Paulo void
294359aa14a9SRui Paulo ieee80211_parse_meshid(struct ieee80211_node *ni, const uint8_t *ie)
294459aa14a9SRui Paulo {
294559aa14a9SRui Paulo 	ni->ni_meshidlen = ie[1];
294659aa14a9SRui Paulo 	memcpy(ni->ni_meshid, ie + 2, ie[1]);
294759aa14a9SRui Paulo }
294859aa14a9SRui Paulo 
294959aa14a9SRui Paulo /*
295059aa14a9SRui Paulo  * Setup mesh-specific node state on neighbor discovery.
295159aa14a9SRui Paulo  */
295259aa14a9SRui Paulo void
295359aa14a9SRui Paulo ieee80211_mesh_init_neighbor(struct ieee80211_node *ni,
295459aa14a9SRui Paulo 	const struct ieee80211_frame *wh,
295559aa14a9SRui Paulo 	const struct ieee80211_scanparams *sp)
295659aa14a9SRui Paulo {
295759aa14a9SRui Paulo 	ieee80211_parse_meshid(ni, sp->meshid);
295859aa14a9SRui Paulo }
295959aa14a9SRui Paulo 
2960d093681cSRui Paulo void
2961d093681cSRui Paulo ieee80211_mesh_update_beacon(struct ieee80211vap *vap,
2962d093681cSRui Paulo 	struct ieee80211_beacon_offsets *bo)
2963d093681cSRui Paulo {
2964d093681cSRui Paulo 	KASSERT(vap->iv_opmode == IEEE80211_M_MBSS, ("not a MBSS vap"));
2965d093681cSRui Paulo 
2966d093681cSRui Paulo 	if (isset(bo->bo_flags, IEEE80211_BEACON_MESHCONF)) {
2967d093681cSRui Paulo 		(void)ieee80211_add_meshconf(bo->bo_meshconf, vap);
2968d093681cSRui Paulo 		clrbit(bo->bo_flags, IEEE80211_BEACON_MESHCONF);
2969d093681cSRui Paulo 	}
2970d093681cSRui Paulo }
2971d093681cSRui Paulo 
297259aa14a9SRui Paulo static int
297359aa14a9SRui Paulo mesh_ioctl_get80211(struct ieee80211vap *vap, struct ieee80211req *ireq)
297459aa14a9SRui Paulo {
297559aa14a9SRui Paulo 	struct ieee80211_mesh_state *ms = vap->iv_mesh;
297659aa14a9SRui Paulo 	uint8_t tmpmeshid[IEEE80211_NWID_LEN];
297759aa14a9SRui Paulo 	struct ieee80211_mesh_route *rt;
297859aa14a9SRui Paulo 	struct ieee80211req_mesh_route *imr;
297959aa14a9SRui Paulo 	size_t len, off;
298059aa14a9SRui Paulo 	uint8_t *p;
298159aa14a9SRui Paulo 	int error;
298259aa14a9SRui Paulo 
298359aa14a9SRui Paulo 	if (vap->iv_opmode != IEEE80211_M_MBSS)
298459aa14a9SRui Paulo 		return ENOSYS;
298559aa14a9SRui Paulo 
298659aa14a9SRui Paulo 	error = 0;
298759aa14a9SRui Paulo 	switch (ireq->i_type) {
298859aa14a9SRui Paulo 	case IEEE80211_IOC_MESH_ID:
298959aa14a9SRui Paulo 		ireq->i_len = ms->ms_idlen;
299059aa14a9SRui Paulo 		memcpy(tmpmeshid, ms->ms_id, ireq->i_len);
299159aa14a9SRui Paulo 		error = copyout(tmpmeshid, ireq->i_data, ireq->i_len);
299259aa14a9SRui Paulo 		break;
299359aa14a9SRui Paulo 	case IEEE80211_IOC_MESH_AP:
299459aa14a9SRui Paulo 		ireq->i_val = (ms->ms_flags & IEEE80211_MESHFLAGS_AP) != 0;
299559aa14a9SRui Paulo 		break;
299659aa14a9SRui Paulo 	case IEEE80211_IOC_MESH_FWRD:
299759aa14a9SRui Paulo 		ireq->i_val = (ms->ms_flags & IEEE80211_MESHFLAGS_FWD) != 0;
299859aa14a9SRui Paulo 		break;
29999fc85253SMonthadar Al Jaberi 	case IEEE80211_IOC_MESH_GATE:
30009fc85253SMonthadar Al Jaberi 		ireq->i_val = (ms->ms_flags & IEEE80211_MESHFLAGS_GATE) != 0;
30019fc85253SMonthadar Al Jaberi 		break;
300259aa14a9SRui Paulo 	case IEEE80211_IOC_MESH_TTL:
300359aa14a9SRui Paulo 		ireq->i_val = ms->ms_ttl;
300459aa14a9SRui Paulo 		break;
300559aa14a9SRui Paulo 	case IEEE80211_IOC_MESH_RTCMD:
300659aa14a9SRui Paulo 		switch (ireq->i_val) {
300759aa14a9SRui Paulo 		case IEEE80211_MESH_RTCMD_LIST:
300859aa14a9SRui Paulo 			len = 0;
300959aa14a9SRui Paulo 			MESH_RT_LOCK(ms);
301059aa14a9SRui Paulo 			TAILQ_FOREACH(rt, &ms->ms_routes, rt_next) {
301159aa14a9SRui Paulo 				len += sizeof(*imr);
301259aa14a9SRui Paulo 			}
301359aa14a9SRui Paulo 			MESH_RT_UNLOCK(ms);
301459aa14a9SRui Paulo 			if (len > ireq->i_len || ireq->i_len < sizeof(*imr)) {
301559aa14a9SRui Paulo 				ireq->i_len = len;
301659aa14a9SRui Paulo 				return ENOMEM;
301759aa14a9SRui Paulo 			}
301859aa14a9SRui Paulo 			ireq->i_len = len;
3019c104cff2SRui Paulo 			/* XXX M_WAIT? */
302059aa14a9SRui Paulo 			p = malloc(len, M_TEMP, M_NOWAIT | M_ZERO);
302159aa14a9SRui Paulo 			if (p == NULL)
302259aa14a9SRui Paulo 				return ENOMEM;
302359aa14a9SRui Paulo 			off = 0;
302459aa14a9SRui Paulo 			MESH_RT_LOCK(ms);
302559aa14a9SRui Paulo 			TAILQ_FOREACH(rt, &ms->ms_routes, rt_next) {
302659aa14a9SRui Paulo 				if (off >= len)
302759aa14a9SRui Paulo 					break;
302859aa14a9SRui Paulo 				imr = (struct ieee80211req_mesh_route *)
302959aa14a9SRui Paulo 				    (p + off);
303059aa14a9SRui Paulo 				IEEE80211_ADDR_COPY(imr->imr_dest,
303159aa14a9SRui Paulo 				    rt->rt_dest);
303259aa14a9SRui Paulo 				IEEE80211_ADDR_COPY(imr->imr_nexthop,
303359aa14a9SRui Paulo 				    rt->rt_nexthop);
303459aa14a9SRui Paulo 				imr->imr_metric = rt->rt_metric;
303559aa14a9SRui Paulo 				imr->imr_nhops = rt->rt_nhops;
3036b5df85a6SMonthadar Al Jaberi 				imr->imr_lifetime =
3037b5df85a6SMonthadar Al Jaberi 				    ieee80211_mesh_rt_update(rt, 0);
3038c104cff2SRui Paulo 				imr->imr_lastmseq = rt->rt_lastmseq;
3039b5df85a6SMonthadar Al Jaberi 				imr->imr_flags = rt->rt_flags; /* last */
304059aa14a9SRui Paulo 				off += sizeof(*imr);
304159aa14a9SRui Paulo 			}
304259aa14a9SRui Paulo 			MESH_RT_UNLOCK(ms);
304359aa14a9SRui Paulo 			error = copyout(p, (uint8_t *)ireq->i_data,
304459aa14a9SRui Paulo 			    ireq->i_len);
304559aa14a9SRui Paulo 			free(p, M_TEMP);
304659aa14a9SRui Paulo 			break;
304759aa14a9SRui Paulo 		case IEEE80211_MESH_RTCMD_FLUSH:
304859aa14a9SRui Paulo 		case IEEE80211_MESH_RTCMD_ADD:
304959aa14a9SRui Paulo 		case IEEE80211_MESH_RTCMD_DELETE:
305059aa14a9SRui Paulo 			return EINVAL;
305159aa14a9SRui Paulo 		default:
305259aa14a9SRui Paulo 			return ENOSYS;
305359aa14a9SRui Paulo 		}
305459aa14a9SRui Paulo 		break;
305559aa14a9SRui Paulo 	case IEEE80211_IOC_MESH_PR_METRIC:
305659aa14a9SRui Paulo 		len = strlen(ms->ms_pmetric->mpm_descr);
305759aa14a9SRui Paulo 		if (ireq->i_len < len)
305859aa14a9SRui Paulo 			return EINVAL;
305959aa14a9SRui Paulo 		ireq->i_len = len;
306059aa14a9SRui Paulo 		error = copyout(ms->ms_pmetric->mpm_descr,
306159aa14a9SRui Paulo 		    (uint8_t *)ireq->i_data, len);
306259aa14a9SRui Paulo 		break;
306359aa14a9SRui Paulo 	case IEEE80211_IOC_MESH_PR_PATH:
306459aa14a9SRui Paulo 		len = strlen(ms->ms_ppath->mpp_descr);
306559aa14a9SRui Paulo 		if (ireq->i_len < len)
306659aa14a9SRui Paulo 			return EINVAL;
306759aa14a9SRui Paulo 		ireq->i_len = len;
306859aa14a9SRui Paulo 		error = copyout(ms->ms_ppath->mpp_descr,
306959aa14a9SRui Paulo 		    (uint8_t *)ireq->i_data, len);
307059aa14a9SRui Paulo 		break;
307159aa14a9SRui Paulo 	default:
307259aa14a9SRui Paulo 		return ENOSYS;
307359aa14a9SRui Paulo 	}
307459aa14a9SRui Paulo 
307559aa14a9SRui Paulo 	return error;
307659aa14a9SRui Paulo }
307759aa14a9SRui Paulo IEEE80211_IOCTL_GET(mesh, mesh_ioctl_get80211);
307859aa14a9SRui Paulo 
307959aa14a9SRui Paulo static int
308059aa14a9SRui Paulo mesh_ioctl_set80211(struct ieee80211vap *vap, struct ieee80211req *ireq)
308159aa14a9SRui Paulo {
308259aa14a9SRui Paulo 	struct ieee80211_mesh_state *ms = vap->iv_mesh;
308359aa14a9SRui Paulo 	uint8_t tmpmeshid[IEEE80211_NWID_LEN];
308459aa14a9SRui Paulo 	uint8_t tmpaddr[IEEE80211_ADDR_LEN];
308559aa14a9SRui Paulo 	char tmpproto[IEEE80211_MESH_PROTO_DSZ];
308659aa14a9SRui Paulo 	int error;
308759aa14a9SRui Paulo 
308859aa14a9SRui Paulo 	if (vap->iv_opmode != IEEE80211_M_MBSS)
308959aa14a9SRui Paulo 		return ENOSYS;
309059aa14a9SRui Paulo 
309159aa14a9SRui Paulo 	error = 0;
309259aa14a9SRui Paulo 	switch (ireq->i_type) {
309359aa14a9SRui Paulo 	case IEEE80211_IOC_MESH_ID:
309459aa14a9SRui Paulo 		if (ireq->i_val != 0 || ireq->i_len > IEEE80211_MESHID_LEN)
309559aa14a9SRui Paulo 			return EINVAL;
309659aa14a9SRui Paulo 		error = copyin(ireq->i_data, tmpmeshid, ireq->i_len);
3097c104cff2SRui Paulo 		if (error != 0)
309859aa14a9SRui Paulo 			break;
309959aa14a9SRui Paulo 		memset(ms->ms_id, 0, IEEE80211_NWID_LEN);
310059aa14a9SRui Paulo 		ms->ms_idlen = ireq->i_len;
310159aa14a9SRui Paulo 		memcpy(ms->ms_id, tmpmeshid, ireq->i_len);
3102c104cff2SRui Paulo 		error = ENETRESET;
310359aa14a9SRui Paulo 		break;
310459aa14a9SRui Paulo 	case IEEE80211_IOC_MESH_AP:
310559aa14a9SRui Paulo 		if (ireq->i_val)
310659aa14a9SRui Paulo 			ms->ms_flags |= IEEE80211_MESHFLAGS_AP;
310759aa14a9SRui Paulo 		else
310859aa14a9SRui Paulo 			ms->ms_flags &= ~IEEE80211_MESHFLAGS_AP;
3109c104cff2SRui Paulo 		error = ENETRESET;
311059aa14a9SRui Paulo 		break;
311159aa14a9SRui Paulo 	case IEEE80211_IOC_MESH_FWRD:
311259aa14a9SRui Paulo 		if (ireq->i_val)
311359aa14a9SRui Paulo 			ms->ms_flags |= IEEE80211_MESHFLAGS_FWD;
311459aa14a9SRui Paulo 		else
311559aa14a9SRui Paulo 			ms->ms_flags &= ~IEEE80211_MESHFLAGS_FWD;
311659aa14a9SRui Paulo 		break;
31179fc85253SMonthadar Al Jaberi 	case IEEE80211_IOC_MESH_GATE:
31189fc85253SMonthadar Al Jaberi 		if (ireq->i_val)
31199fc85253SMonthadar Al Jaberi 			ms->ms_flags |= IEEE80211_MESHFLAGS_GATE;
31209fc85253SMonthadar Al Jaberi 		else
31219fc85253SMonthadar Al Jaberi 			ms->ms_flags &= ~IEEE80211_MESHFLAGS_GATE;
31229fc85253SMonthadar Al Jaberi 		break;
312359aa14a9SRui Paulo 	case IEEE80211_IOC_MESH_TTL:
312459aa14a9SRui Paulo 		ms->ms_ttl = (uint8_t) ireq->i_val;
312559aa14a9SRui Paulo 		break;
312659aa14a9SRui Paulo 	case IEEE80211_IOC_MESH_RTCMD:
312759aa14a9SRui Paulo 		switch (ireq->i_val) {
312859aa14a9SRui Paulo 		case IEEE80211_MESH_RTCMD_LIST:
312959aa14a9SRui Paulo 			return EINVAL;
313059aa14a9SRui Paulo 		case IEEE80211_MESH_RTCMD_FLUSH:
313159aa14a9SRui Paulo 			ieee80211_mesh_rt_flush(vap);
313259aa14a9SRui Paulo 			break;
313359aa14a9SRui Paulo 		case IEEE80211_MESH_RTCMD_ADD:
313459aa14a9SRui Paulo 			if (IEEE80211_ADDR_EQ(vap->iv_myaddr, ireq->i_data) ||
313559aa14a9SRui Paulo 			    IEEE80211_ADDR_EQ(broadcastaddr, ireq->i_data))
313659aa14a9SRui Paulo 				return EINVAL;
313759aa14a9SRui Paulo 			error = copyin(ireq->i_data, &tmpaddr,
313859aa14a9SRui Paulo 			    IEEE80211_ADDR_LEN);
3139c104cff2SRui Paulo 			if (error == 0)
314059aa14a9SRui Paulo 				ieee80211_mesh_discover(vap, tmpaddr, NULL);
314159aa14a9SRui Paulo 			break;
314259aa14a9SRui Paulo 		case IEEE80211_MESH_RTCMD_DELETE:
314359aa14a9SRui Paulo 			ieee80211_mesh_rt_del(vap, ireq->i_data);
314459aa14a9SRui Paulo 			break;
314559aa14a9SRui Paulo 		default:
314659aa14a9SRui Paulo 			return ENOSYS;
314759aa14a9SRui Paulo 		}
314859aa14a9SRui Paulo 		break;
314959aa14a9SRui Paulo 	case IEEE80211_IOC_MESH_PR_METRIC:
315059aa14a9SRui Paulo 		error = copyin(ireq->i_data, tmpproto, sizeof(tmpproto));
3151c104cff2SRui Paulo 		if (error == 0) {
3152c104cff2SRui Paulo 			error = mesh_select_proto_metric(vap, tmpproto);
3153c104cff2SRui Paulo 			if (error == 0)
3154c104cff2SRui Paulo 				error = ENETRESET;
3155c104cff2SRui Paulo 		}
315659aa14a9SRui Paulo 		break;
315759aa14a9SRui Paulo 	case IEEE80211_IOC_MESH_PR_PATH:
315859aa14a9SRui Paulo 		error = copyin(ireq->i_data, tmpproto, sizeof(tmpproto));
3159c104cff2SRui Paulo 		if (error == 0) {
3160c104cff2SRui Paulo 			error = mesh_select_proto_path(vap, tmpproto);
3161c104cff2SRui Paulo 			if (error == 0)
3162c104cff2SRui Paulo 				error = ENETRESET;
3163c104cff2SRui Paulo 		}
316459aa14a9SRui Paulo 		break;
316559aa14a9SRui Paulo 	default:
316659aa14a9SRui Paulo 		return ENOSYS;
316759aa14a9SRui Paulo 	}
316859aa14a9SRui Paulo 	return error;
316959aa14a9SRui Paulo }
317059aa14a9SRui Paulo IEEE80211_IOCTL_SET(mesh, mesh_ioctl_set80211);
3171