xref: /freebsd/sys/net80211/ieee80211_mesh.c (revision c2042c359f8069517ecc5393aec312f1720da4b2)
159aa14a9SRui Paulo /*-
259aa14a9SRui Paulo  * Copyright (c) 2009 The FreeBSD Foundation
359aa14a9SRui Paulo  * All rights reserved.
459aa14a9SRui Paulo  *
559aa14a9SRui Paulo  * This software was developed by Rui Paulo under sponsorship from the
659aa14a9SRui Paulo  * FreeBSD Foundation.
759aa14a9SRui Paulo  *
859aa14a9SRui Paulo  * Redistribution and use in source and binary forms, with or without
959aa14a9SRui Paulo  * modification, are permitted provided that the following conditions
1059aa14a9SRui Paulo  * are met:
1159aa14a9SRui Paulo  * 1. Redistributions of source code must retain the above copyright
1259aa14a9SRui Paulo  *    notice, this list of conditions and the following disclaimer.
1359aa14a9SRui Paulo  * 2. Redistributions in binary form must reproduce the above copyright
1459aa14a9SRui Paulo  *    notice, this list of conditions and the following disclaimer in the
1559aa14a9SRui Paulo  *    documentation and/or other materials provided with the distribution.
1659aa14a9SRui Paulo  *
1759aa14a9SRui Paulo  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1859aa14a9SRui Paulo  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1959aa14a9SRui Paulo  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2059aa14a9SRui Paulo  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2159aa14a9SRui Paulo  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2259aa14a9SRui Paulo  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2359aa14a9SRui Paulo  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2459aa14a9SRui Paulo  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2559aa14a9SRui Paulo  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2659aa14a9SRui Paulo  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2759aa14a9SRui Paulo  * SUCH DAMAGE.
2859aa14a9SRui Paulo  */
2959aa14a9SRui Paulo #include <sys/cdefs.h>
3059aa14a9SRui Paulo #ifdef __FreeBSD__
3159aa14a9SRui Paulo __FBSDID("$FreeBSD$");
3259aa14a9SRui Paulo #endif
3359aa14a9SRui Paulo 
3459aa14a9SRui Paulo /*
3559aa14a9SRui Paulo  * IEEE 802.11s Mesh Point (MBSS) support.
3659aa14a9SRui Paulo  *
3759aa14a9SRui Paulo  * Based on March 2009, D3.0 802.11s draft spec.
3859aa14a9SRui Paulo  */
3959aa14a9SRui Paulo #include "opt_inet.h"
4059aa14a9SRui Paulo #include "opt_wlan.h"
4159aa14a9SRui Paulo 
4259aa14a9SRui Paulo #include <sys/param.h>
4359aa14a9SRui Paulo #include <sys/systm.h>
4459aa14a9SRui Paulo #include <sys/mbuf.h>
4559aa14a9SRui Paulo #include <sys/malloc.h>
4659aa14a9SRui Paulo #include <sys/kernel.h>
4759aa14a9SRui Paulo 
4859aa14a9SRui Paulo #include <sys/socket.h>
4959aa14a9SRui Paulo #include <sys/sockio.h>
5059aa14a9SRui Paulo #include <sys/endian.h>
5159aa14a9SRui Paulo #include <sys/errno.h>
5259aa14a9SRui Paulo #include <sys/proc.h>
5359aa14a9SRui Paulo #include <sys/sysctl.h>
5459aa14a9SRui Paulo 
5559aa14a9SRui Paulo #include <net/if.h>
5659aa14a9SRui Paulo #include <net/if_media.h>
5759aa14a9SRui Paulo #include <net/if_llc.h>
5859aa14a9SRui Paulo #include <net/ethernet.h>
5959aa14a9SRui Paulo 
6059aa14a9SRui Paulo #include <net80211/ieee80211_var.h>
6159aa14a9SRui Paulo #include <net80211/ieee80211_action.h>
6259aa14a9SRui Paulo #include <net80211/ieee80211_input.h>
6359aa14a9SRui Paulo #include <net80211/ieee80211_mesh.h>
6459aa14a9SRui Paulo 
65c104cff2SRui Paulo static void	mesh_rt_flush_invalid(struct ieee80211vap *);
6659aa14a9SRui Paulo static int	mesh_select_proto_path(struct ieee80211vap *, const char *);
6759aa14a9SRui Paulo static int	mesh_select_proto_metric(struct ieee80211vap *, const char *);
6859aa14a9SRui Paulo static void	mesh_vattach(struct ieee80211vap *);
6959aa14a9SRui Paulo static int	mesh_newstate(struct ieee80211vap *, enum ieee80211_state, int);
70c104cff2SRui Paulo static void	mesh_rt_cleanup_cb(void *);
71aec0289dSRui Paulo static void	mesh_linkchange(struct ieee80211_node *,
7259aa14a9SRui Paulo 		    enum ieee80211_mesh_mlstate);
7359aa14a9SRui Paulo static void	mesh_checkid(void *, struct ieee80211_node *);
7459aa14a9SRui Paulo static uint32_t	mesh_generateid(struct ieee80211vap *);
7559aa14a9SRui Paulo static int	mesh_checkpseq(struct ieee80211vap *,
7659aa14a9SRui Paulo 		    const uint8_t [IEEE80211_ADDR_LEN], uint32_t);
7759aa14a9SRui Paulo static struct ieee80211_node *
7859aa14a9SRui Paulo 		mesh_find_txnode(struct ieee80211vap *,
7959aa14a9SRui Paulo 		    const uint8_t [IEEE80211_ADDR_LEN]);
8059aa14a9SRui Paulo static void	mesh_forward(struct ieee80211vap *, struct mbuf *,
8159aa14a9SRui Paulo 		    const struct ieee80211_meshcntl *);
8259aa14a9SRui Paulo static int	mesh_input(struct ieee80211_node *, struct mbuf *, int, int);
8359aa14a9SRui Paulo static void	mesh_recv_mgmt(struct ieee80211_node *, struct mbuf *, int,
8459aa14a9SRui Paulo 		    int, int);
850917631fSRui Paulo static void	mesh_recv_ctl(struct ieee80211_node *, struct mbuf *, int);
8659aa14a9SRui Paulo static void	mesh_peer_timeout_setup(struct ieee80211_node *);
8759aa14a9SRui Paulo static void	mesh_peer_timeout_backoff(struct ieee80211_node *);
8859aa14a9SRui Paulo static void	mesh_peer_timeout_cb(void *);
8959aa14a9SRui Paulo static __inline void
9059aa14a9SRui Paulo 		mesh_peer_timeout_stop(struct ieee80211_node *);
9159aa14a9SRui Paulo static int	mesh_verify_meshid(struct ieee80211vap *, const uint8_t *);
9259aa14a9SRui Paulo static int	mesh_verify_meshconf(struct ieee80211vap *, const uint8_t *);
93c77735e2SRui Paulo static int	mesh_verify_meshpeer(struct ieee80211vap *, uint8_t,
94c77735e2SRui Paulo     		    const uint8_t *);
9559aa14a9SRui Paulo uint32_t	mesh_airtime_calc(struct ieee80211_node *);
9659aa14a9SRui Paulo 
9759aa14a9SRui Paulo /*
9859aa14a9SRui Paulo  * Timeout values come from the specification and are in milliseconds.
9959aa14a9SRui Paulo  */
1006472ac3dSEd Schouten static SYSCTL_NODE(_net_wlan, OID_AUTO, mesh, CTLFLAG_RD, 0,
10159aa14a9SRui Paulo     "IEEE 802.11s parameters");
10259aa14a9SRui Paulo static int ieee80211_mesh_retrytimeout = -1;
10359aa14a9SRui Paulo SYSCTL_PROC(_net_wlan_mesh, OID_AUTO, retrytimeout, CTLTYPE_INT | CTLFLAG_RW,
10459aa14a9SRui Paulo     &ieee80211_mesh_retrytimeout, 0, ieee80211_sysctl_msecs_ticks, "I",
10559aa14a9SRui Paulo     "Retry timeout (msec)");
10659aa14a9SRui Paulo static int ieee80211_mesh_holdingtimeout = -1;
10759aa14a9SRui Paulo SYSCTL_PROC(_net_wlan_mesh, OID_AUTO, holdingtimeout, CTLTYPE_INT | CTLFLAG_RW,
10859aa14a9SRui Paulo     &ieee80211_mesh_holdingtimeout, 0, ieee80211_sysctl_msecs_ticks, "I",
10959aa14a9SRui Paulo     "Holding state timeout (msec)");
11059aa14a9SRui Paulo static int ieee80211_mesh_confirmtimeout = -1;
11159aa14a9SRui Paulo SYSCTL_PROC(_net_wlan_mesh, OID_AUTO, confirmtimeout, CTLTYPE_INT | CTLFLAG_RW,
11259aa14a9SRui Paulo     &ieee80211_mesh_confirmtimeout, 0, ieee80211_sysctl_msecs_ticks, "I",
11359aa14a9SRui Paulo     "Confirm state timeout (msec)");
11459aa14a9SRui Paulo static int ieee80211_mesh_maxretries = 2;
11559aa14a9SRui Paulo SYSCTL_INT(_net_wlan_mesh, OID_AUTO, maxretries, CTLTYPE_INT | CTLFLAG_RW,
11659aa14a9SRui Paulo     &ieee80211_mesh_maxretries, 0,
11759aa14a9SRui Paulo     "Maximum retries during peer link establishment");
11859aa14a9SRui Paulo 
11959aa14a9SRui Paulo static const uint8_t broadcastaddr[IEEE80211_ADDR_LEN] =
12059aa14a9SRui Paulo 	{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
12159aa14a9SRui Paulo 
12259aa14a9SRui Paulo static	ieee80211_recv_action_func mesh_recv_action_meshpeering_open;
12359aa14a9SRui Paulo static	ieee80211_recv_action_func mesh_recv_action_meshpeering_confirm;
12459aa14a9SRui Paulo static	ieee80211_recv_action_func mesh_recv_action_meshpeering_close;
125bdd2a076SAdrian Chadd static	ieee80211_recv_action_func mesh_recv_action_meshlmetric;
12659aa14a9SRui Paulo 
12759aa14a9SRui Paulo static	ieee80211_send_action_func mesh_send_action_meshpeering_open;
12859aa14a9SRui Paulo static	ieee80211_send_action_func mesh_send_action_meshpeering_confirm;
12959aa14a9SRui Paulo static	ieee80211_send_action_func mesh_send_action_meshpeering_close;
130bdd2a076SAdrian Chadd static	ieee80211_send_action_func mesh_send_action_meshlmetric;
13159aa14a9SRui Paulo 
13259aa14a9SRui Paulo static const struct ieee80211_mesh_proto_metric mesh_metric_airtime = {
13359aa14a9SRui Paulo 	.mpm_descr	= "AIRTIME",
1346b8c1829SRui Paulo 	.mpm_ie		= IEEE80211_MESHCONF_METRIC_AIRTIME,
13559aa14a9SRui Paulo 	.mpm_metric	= mesh_airtime_calc,
13659aa14a9SRui Paulo };
13759aa14a9SRui Paulo 
13859aa14a9SRui Paulo static struct ieee80211_mesh_proto_path		mesh_proto_paths[4];
13959aa14a9SRui Paulo static struct ieee80211_mesh_proto_metric	mesh_proto_metrics[4];
14059aa14a9SRui Paulo 
14159aa14a9SRui Paulo #define	MESH_RT_LOCK(ms)	mtx_lock(&(ms)->ms_rt_lock)
142c104cff2SRui Paulo #define	MESH_RT_LOCK_ASSERT(ms)	mtx_assert(&(ms)->ms_rt_lock, MA_OWNED)
14359aa14a9SRui Paulo #define	MESH_RT_UNLOCK(ms)	mtx_unlock(&(ms)->ms_rt_lock)
14459aa14a9SRui Paulo 
1451f88a92bSAdrian Chadd MALLOC_DEFINE(M_80211_MESH_PREQ, "80211preq", "802.11 MESH Path Request frame");
1461f88a92bSAdrian Chadd MALLOC_DEFINE(M_80211_MESH_PREP, "80211prep", "802.11 MESH Path Reply frame");
1471f88a92bSAdrian Chadd MALLOC_DEFINE(M_80211_MESH_PERR, "80211perr", "802.11 MESH Path Error frame");
1481f88a92bSAdrian Chadd 
14959aa14a9SRui Paulo MALLOC_DEFINE(M_80211_MESH_RT, "80211mesh", "802.11s routing table");
15059aa14a9SRui Paulo 
15159aa14a9SRui Paulo /*
15259aa14a9SRui Paulo  * Helper functions to manipulate the Mesh routing table.
15359aa14a9SRui Paulo  */
154c104cff2SRui Paulo 
155c104cff2SRui Paulo static struct ieee80211_mesh_route *
156c104cff2SRui Paulo mesh_rt_find_locked(struct ieee80211_mesh_state *ms,
157c104cff2SRui Paulo     const uint8_t dest[IEEE80211_ADDR_LEN])
158c104cff2SRui Paulo {
159c104cff2SRui Paulo 	struct ieee80211_mesh_route *rt;
160c104cff2SRui Paulo 
161c104cff2SRui Paulo 	MESH_RT_LOCK_ASSERT(ms);
162c104cff2SRui Paulo 
163c104cff2SRui Paulo 	TAILQ_FOREACH(rt, &ms->ms_routes, rt_next) {
164c104cff2SRui Paulo 		if (IEEE80211_ADDR_EQ(dest, rt->rt_dest))
165c104cff2SRui Paulo 			return rt;
166c104cff2SRui Paulo 	}
167c104cff2SRui Paulo 	return NULL;
168c104cff2SRui Paulo }
169c104cff2SRui Paulo 
170c104cff2SRui Paulo static struct ieee80211_mesh_route *
171c104cff2SRui Paulo mesh_rt_add_locked(struct ieee80211_mesh_state *ms,
172c104cff2SRui Paulo     const uint8_t dest[IEEE80211_ADDR_LEN])
173c104cff2SRui Paulo {
174c104cff2SRui Paulo 	struct ieee80211_mesh_route *rt;
175c104cff2SRui Paulo 
176c104cff2SRui Paulo 	KASSERT(!IEEE80211_ADDR_EQ(broadcastaddr, dest),
177c104cff2SRui Paulo 	    ("%s: adding broadcast to the routing table", __func__));
178c104cff2SRui Paulo 
179c104cff2SRui Paulo 	MESH_RT_LOCK_ASSERT(ms);
180c104cff2SRui Paulo 
181c104cff2SRui Paulo 	rt = malloc(ALIGN(sizeof(struct ieee80211_mesh_route)) +
182c104cff2SRui Paulo 	    ms->ms_ppath->mpp_privlen, M_80211_MESH_RT, M_NOWAIT | M_ZERO);
183c104cff2SRui Paulo 	if (rt != NULL) {
184c104cff2SRui Paulo 		IEEE80211_ADDR_COPY(rt->rt_dest, dest);
185c104cff2SRui Paulo 		rt->rt_priv = (void *)ALIGN(&rt[1]);
186e50821abSSam Leffler 		rt->rt_crtime = ticks;
187c104cff2SRui Paulo 		TAILQ_INSERT_TAIL(&ms->ms_routes, rt, rt_next);
188c104cff2SRui Paulo 	}
189c104cff2SRui Paulo 	return rt;
190c104cff2SRui Paulo }
191c104cff2SRui Paulo 
19259aa14a9SRui Paulo struct ieee80211_mesh_route *
19359aa14a9SRui Paulo ieee80211_mesh_rt_find(struct ieee80211vap *vap,
19459aa14a9SRui Paulo     const uint8_t dest[IEEE80211_ADDR_LEN])
19559aa14a9SRui Paulo {
19659aa14a9SRui Paulo 	struct ieee80211_mesh_state *ms = vap->iv_mesh;
19759aa14a9SRui Paulo 	struct ieee80211_mesh_route *rt;
19859aa14a9SRui Paulo 
19959aa14a9SRui Paulo 	MESH_RT_LOCK(ms);
200c104cff2SRui Paulo 	rt = mesh_rt_find_locked(ms, dest);
20159aa14a9SRui Paulo 	MESH_RT_UNLOCK(ms);
20259aa14a9SRui Paulo 	return rt;
20359aa14a9SRui Paulo }
20459aa14a9SRui Paulo 
20559aa14a9SRui Paulo struct ieee80211_mesh_route *
20659aa14a9SRui Paulo ieee80211_mesh_rt_add(struct ieee80211vap *vap,
20759aa14a9SRui Paulo     const uint8_t dest[IEEE80211_ADDR_LEN])
20859aa14a9SRui Paulo {
20959aa14a9SRui Paulo 	struct ieee80211_mesh_state *ms = vap->iv_mesh;
21059aa14a9SRui Paulo 	struct ieee80211_mesh_route *rt;
21159aa14a9SRui Paulo 
21259aa14a9SRui Paulo 	KASSERT(ieee80211_mesh_rt_find(vap, dest) == NULL,
21359aa14a9SRui Paulo 	    ("%s: duplicate entry in the routing table", __func__));
21459aa14a9SRui Paulo 	KASSERT(!IEEE80211_ADDR_EQ(vap->iv_myaddr, dest),
21559aa14a9SRui Paulo 	    ("%s: adding self to the routing table", __func__));
21659aa14a9SRui Paulo 
21759aa14a9SRui Paulo 	MESH_RT_LOCK(ms);
218c104cff2SRui Paulo 	rt = mesh_rt_add_locked(ms, dest);
21959aa14a9SRui Paulo 	MESH_RT_UNLOCK(ms);
22059aa14a9SRui Paulo 	return rt;
22159aa14a9SRui Paulo }
22259aa14a9SRui Paulo 
223c104cff2SRui Paulo /*
224c104cff2SRui Paulo  * Add a proxy route (as needed) for the specified destination.
225c104cff2SRui Paulo  */
226c104cff2SRui Paulo void
227c104cff2SRui Paulo ieee80211_mesh_proxy_check(struct ieee80211vap *vap,
228c104cff2SRui Paulo     const uint8_t dest[IEEE80211_ADDR_LEN])
229c104cff2SRui Paulo {
230c104cff2SRui Paulo 	struct ieee80211_mesh_state *ms = vap->iv_mesh;
231c104cff2SRui Paulo 	struct ieee80211_mesh_route *rt;
232c104cff2SRui Paulo 
233c104cff2SRui Paulo 	MESH_RT_LOCK(ms);
234c104cff2SRui Paulo 	rt = mesh_rt_find_locked(ms, dest);
235c104cff2SRui Paulo 	if (rt == NULL) {
236c104cff2SRui Paulo 		rt = mesh_rt_add_locked(ms, dest);
237c104cff2SRui Paulo 		if (rt == NULL) {
238c104cff2SRui Paulo 			IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH, dest,
239c104cff2SRui Paulo 			    "%s", "unable to add proxy entry");
240c104cff2SRui Paulo 			vap->iv_stats.is_mesh_rtaddfailed++;
241c104cff2SRui Paulo 		} else {
2423ca80f0dSRui Paulo 			IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH, dest,
2433ca80f0dSRui Paulo 			    "%s", "add proxy entry");
244c104cff2SRui Paulo 			IEEE80211_ADDR_COPY(rt->rt_nexthop, vap->iv_myaddr);
245c104cff2SRui Paulo 			rt->rt_flags |= IEEE80211_MESHRT_FLAGS_VALID
246c104cff2SRui Paulo 				     |  IEEE80211_MESHRT_FLAGS_PROXY;
247c104cff2SRui Paulo 		}
248c104cff2SRui Paulo 	/* XXX assert PROXY? */
249c104cff2SRui Paulo 	} else if ((rt->rt_flags & IEEE80211_MESHRT_FLAGS_VALID) == 0) {
250c104cff2SRui Paulo 		struct ieee80211com *ic = vap->iv_ic;
251c104cff2SRui Paulo 		/*
252c104cff2SRui Paulo 		 * Fix existing entry created by received frames from
253c104cff2SRui Paulo 		 * stations that have some memory of dest.  We also
254c104cff2SRui Paulo 		 * flush any frames held on the staging queue; delivering
255c104cff2SRui Paulo 		 * them is too much trouble right now.
256c104cff2SRui Paulo 		 */
257c104cff2SRui Paulo 		IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH, dest,
258c104cff2SRui Paulo 		    "%s", "fix proxy entry");
259c104cff2SRui Paulo 		IEEE80211_ADDR_COPY(rt->rt_nexthop, vap->iv_myaddr);
260c104cff2SRui Paulo 		rt->rt_flags |= IEEE80211_MESHRT_FLAGS_VALID
261c104cff2SRui Paulo 			     |  IEEE80211_MESHRT_FLAGS_PROXY;
262c104cff2SRui Paulo 		/* XXX belongs in hwmp */
263c104cff2SRui Paulo 		ieee80211_ageq_drain_node(&ic->ic_stageq,
264c104cff2SRui Paulo 		   (void *)(uintptr_t) ieee80211_mac_hash(ic, dest));
265c104cff2SRui Paulo 		/* XXX stat? */
266c104cff2SRui Paulo 	}
267c104cff2SRui Paulo 	MESH_RT_UNLOCK(ms);
268c104cff2SRui Paulo }
269c104cff2SRui Paulo 
270c104cff2SRui Paulo static __inline void
271c104cff2SRui Paulo mesh_rt_del(struct ieee80211_mesh_state *ms, struct ieee80211_mesh_route *rt)
272c104cff2SRui Paulo {
273c104cff2SRui Paulo 	TAILQ_REMOVE(&ms->ms_routes, rt, rt_next);
274c104cff2SRui Paulo 	free(rt, M_80211_MESH_RT);
275c104cff2SRui Paulo }
276c104cff2SRui Paulo 
27759aa14a9SRui Paulo void
27859aa14a9SRui Paulo ieee80211_mesh_rt_del(struct ieee80211vap *vap,
27959aa14a9SRui Paulo     const uint8_t dest[IEEE80211_ADDR_LEN])
28059aa14a9SRui Paulo {
28159aa14a9SRui Paulo 	struct ieee80211_mesh_state *ms = vap->iv_mesh;
28259aa14a9SRui Paulo 	struct ieee80211_mesh_route *rt, *next;
28359aa14a9SRui Paulo 
28459aa14a9SRui Paulo 	MESH_RT_LOCK(ms);
28559aa14a9SRui Paulo 	TAILQ_FOREACH_SAFE(rt, &ms->ms_routes, rt_next, next) {
28659aa14a9SRui Paulo 		if (IEEE80211_ADDR_EQ(rt->rt_dest, dest)) {
287c104cff2SRui Paulo 			mesh_rt_del(ms, rt);
28859aa14a9SRui Paulo 			MESH_RT_UNLOCK(ms);
28959aa14a9SRui Paulo 			return;
29059aa14a9SRui Paulo 		}
29159aa14a9SRui Paulo 	}
29259aa14a9SRui Paulo 	MESH_RT_UNLOCK(ms);
29359aa14a9SRui Paulo }
29459aa14a9SRui Paulo 
29559aa14a9SRui Paulo void
29659aa14a9SRui Paulo ieee80211_mesh_rt_flush(struct ieee80211vap *vap)
29759aa14a9SRui Paulo {
29859aa14a9SRui Paulo 	struct ieee80211_mesh_state *ms = vap->iv_mesh;
29959aa14a9SRui Paulo 	struct ieee80211_mesh_route *rt, *next;
30059aa14a9SRui Paulo 
30159aa14a9SRui Paulo 	if (ms == NULL)
30259aa14a9SRui Paulo 		return;
30359aa14a9SRui Paulo 	MESH_RT_LOCK(ms);
304c104cff2SRui Paulo 	TAILQ_FOREACH_SAFE(rt, &ms->ms_routes, rt_next, next)
305c104cff2SRui Paulo 		mesh_rt_del(ms, rt);
306c104cff2SRui Paulo 	MESH_RT_UNLOCK(ms);
307c104cff2SRui Paulo }
308c104cff2SRui Paulo 
3093ca80f0dSRui Paulo void
3103ca80f0dSRui Paulo ieee80211_mesh_rt_flush_peer(struct ieee80211vap *vap,
3113ca80f0dSRui Paulo     const uint8_t peer[IEEE80211_ADDR_LEN])
3123ca80f0dSRui Paulo {
3133ca80f0dSRui Paulo 	struct ieee80211_mesh_state *ms = vap->iv_mesh;
3143ca80f0dSRui Paulo 	struct ieee80211_mesh_route *rt, *next;
3153ca80f0dSRui Paulo 
3163ca80f0dSRui Paulo 	MESH_RT_LOCK(ms);
3173ca80f0dSRui Paulo 	TAILQ_FOREACH_SAFE(rt, &ms->ms_routes, rt_next, next) {
3183ca80f0dSRui Paulo 		if (IEEE80211_ADDR_EQ(rt->rt_nexthop, peer))
3193ca80f0dSRui Paulo 			mesh_rt_del(ms, rt);
3203ca80f0dSRui Paulo 	}
3213ca80f0dSRui Paulo 	MESH_RT_UNLOCK(ms);
3223ca80f0dSRui Paulo }
3233ca80f0dSRui Paulo 
324c104cff2SRui Paulo /*
325c104cff2SRui Paulo  * Flush expired routing entries, i.e. those in invalid state for
326c104cff2SRui Paulo  * some time.
327c104cff2SRui Paulo  */
328c104cff2SRui Paulo static void
329c104cff2SRui Paulo mesh_rt_flush_invalid(struct ieee80211vap *vap)
330c104cff2SRui Paulo {
331c104cff2SRui Paulo 	struct ieee80211_mesh_state *ms = vap->iv_mesh;
332c104cff2SRui Paulo 	struct ieee80211_mesh_route *rt, *next;
333c104cff2SRui Paulo 
334c104cff2SRui Paulo 	if (ms == NULL)
335c104cff2SRui Paulo 		return;
336c104cff2SRui Paulo 	MESH_RT_LOCK(ms);
33759aa14a9SRui Paulo 	TAILQ_FOREACH_SAFE(rt, &ms->ms_routes, rt_next, next) {
338c104cff2SRui Paulo 		if ((rt->rt_flags & IEEE80211_MESHRT_FLAGS_VALID) == 0 &&
339e50821abSSam Leffler 		    ticks - rt->rt_crtime >= ms->ms_ppath->mpp_inact)
340c104cff2SRui Paulo 			mesh_rt_del(ms, rt);
34159aa14a9SRui Paulo 	}
34259aa14a9SRui Paulo 	MESH_RT_UNLOCK(ms);
34359aa14a9SRui Paulo }
34459aa14a9SRui Paulo 
34559aa14a9SRui Paulo #define	N(a)	(sizeof(a) / sizeof(a[0]))
34659aa14a9SRui Paulo int
34759aa14a9SRui Paulo ieee80211_mesh_register_proto_path(const struct ieee80211_mesh_proto_path *mpp)
34859aa14a9SRui Paulo {
34959aa14a9SRui Paulo 	int i, firstempty = -1;
35059aa14a9SRui Paulo 
35159aa14a9SRui Paulo 	for (i = 0; i < N(mesh_proto_paths); i++) {
3526b8c1829SRui Paulo 		if (strncmp(mpp->mpp_descr, mesh_proto_paths[i].mpp_descr,
3536b8c1829SRui Paulo 		    IEEE80211_MESH_PROTO_DSZ) == 0)
35459aa14a9SRui Paulo 			return EEXIST;
3556b8c1829SRui Paulo 		if (!mesh_proto_paths[i].mpp_active && firstempty == -1)
35659aa14a9SRui Paulo 			firstempty = i;
35759aa14a9SRui Paulo 	}
35859aa14a9SRui Paulo 	if (firstempty < 0)
35959aa14a9SRui Paulo 		return ENOSPC;
36059aa14a9SRui Paulo 	memcpy(&mesh_proto_paths[firstempty], mpp, sizeof(*mpp));
3616b8c1829SRui Paulo 	mesh_proto_paths[firstempty].mpp_active = 1;
36259aa14a9SRui Paulo 	return 0;
36359aa14a9SRui Paulo }
36459aa14a9SRui Paulo 
36559aa14a9SRui Paulo int
36659aa14a9SRui Paulo ieee80211_mesh_register_proto_metric(const struct
36759aa14a9SRui Paulo     ieee80211_mesh_proto_metric *mpm)
36859aa14a9SRui Paulo {
36959aa14a9SRui Paulo 	int i, firstempty = -1;
37059aa14a9SRui Paulo 
37159aa14a9SRui Paulo 	for (i = 0; i < N(mesh_proto_metrics); i++) {
3726b8c1829SRui Paulo 		if (strncmp(mpm->mpm_descr, mesh_proto_metrics[i].mpm_descr,
3736b8c1829SRui Paulo 		    IEEE80211_MESH_PROTO_DSZ) == 0)
37459aa14a9SRui Paulo 			return EEXIST;
3756b8c1829SRui Paulo 		if (!mesh_proto_metrics[i].mpm_active && firstempty == -1)
37659aa14a9SRui Paulo 			firstempty = i;
37759aa14a9SRui Paulo 	}
37859aa14a9SRui Paulo 	if (firstempty < 0)
37959aa14a9SRui Paulo 		return ENOSPC;
38059aa14a9SRui Paulo 	memcpy(&mesh_proto_metrics[firstempty], mpm, sizeof(*mpm));
3816b8c1829SRui Paulo 	mesh_proto_metrics[firstempty].mpm_active = 1;
38259aa14a9SRui Paulo 	return 0;
38359aa14a9SRui Paulo }
38459aa14a9SRui Paulo 
38559aa14a9SRui Paulo static int
38659aa14a9SRui Paulo mesh_select_proto_path(struct ieee80211vap *vap, const char *name)
38759aa14a9SRui Paulo {
38859aa14a9SRui Paulo 	struct ieee80211_mesh_state *ms = vap->iv_mesh;
38959aa14a9SRui Paulo 	int i;
39059aa14a9SRui Paulo 
39159aa14a9SRui Paulo 	for (i = 0; i < N(mesh_proto_paths); i++) {
39259aa14a9SRui Paulo 		if (strcasecmp(mesh_proto_paths[i].mpp_descr, name) == 0) {
39359aa14a9SRui Paulo 			ms->ms_ppath = &mesh_proto_paths[i];
39459aa14a9SRui Paulo 			return 0;
39559aa14a9SRui Paulo 		}
39659aa14a9SRui Paulo 	}
39759aa14a9SRui Paulo 	return ENOENT;
39859aa14a9SRui Paulo }
39959aa14a9SRui Paulo 
40059aa14a9SRui Paulo static int
40159aa14a9SRui Paulo mesh_select_proto_metric(struct ieee80211vap *vap, const char *name)
40259aa14a9SRui Paulo {
40359aa14a9SRui Paulo 	struct ieee80211_mesh_state *ms = vap->iv_mesh;
40459aa14a9SRui Paulo 	int i;
40559aa14a9SRui Paulo 
40659aa14a9SRui Paulo 	for (i = 0; i < N(mesh_proto_metrics); i++) {
40759aa14a9SRui Paulo 		if (strcasecmp(mesh_proto_metrics[i].mpm_descr, name) == 0) {
40859aa14a9SRui Paulo 			ms->ms_pmetric = &mesh_proto_metrics[i];
40959aa14a9SRui Paulo 			return 0;
41059aa14a9SRui Paulo 		}
41159aa14a9SRui Paulo 	}
41259aa14a9SRui Paulo 	return ENOENT;
41359aa14a9SRui Paulo }
41459aa14a9SRui Paulo #undef	N
41559aa14a9SRui Paulo 
41659aa14a9SRui Paulo static void
41759aa14a9SRui Paulo ieee80211_mesh_init(void)
41859aa14a9SRui Paulo {
41959aa14a9SRui Paulo 
42059aa14a9SRui Paulo 	memset(mesh_proto_paths, 0, sizeof(mesh_proto_paths));
42159aa14a9SRui Paulo 	memset(mesh_proto_metrics, 0, sizeof(mesh_proto_metrics));
42259aa14a9SRui Paulo 
42359aa14a9SRui Paulo 	/*
42459aa14a9SRui Paulo 	 * Setup mesh parameters that depends on the clock frequency.
42559aa14a9SRui Paulo 	 */
42659aa14a9SRui Paulo 	ieee80211_mesh_retrytimeout = msecs_to_ticks(40);
42759aa14a9SRui Paulo 	ieee80211_mesh_holdingtimeout = msecs_to_ticks(40);
42859aa14a9SRui Paulo 	ieee80211_mesh_confirmtimeout = msecs_to_ticks(40);
42959aa14a9SRui Paulo 
43059aa14a9SRui Paulo 	/*
43159aa14a9SRui Paulo 	 * Register action frame handlers.
43259aa14a9SRui Paulo 	 */
433ebeaa1adSMonthadar Al Jaberi 	ieee80211_recv_action_register(IEEE80211_ACTION_CAT_SELF_PROT,
43459aa14a9SRui Paulo 	    IEEE80211_ACTION_MESHPEERING_OPEN,
43559aa14a9SRui Paulo 	    mesh_recv_action_meshpeering_open);
436ebeaa1adSMonthadar Al Jaberi 	ieee80211_recv_action_register(IEEE80211_ACTION_CAT_SELF_PROT,
43759aa14a9SRui Paulo 	    IEEE80211_ACTION_MESHPEERING_CONFIRM,
43859aa14a9SRui Paulo 	    mesh_recv_action_meshpeering_confirm);
439ebeaa1adSMonthadar Al Jaberi 	ieee80211_recv_action_register(IEEE80211_ACTION_CAT_SELF_PROT,
44059aa14a9SRui Paulo 	    IEEE80211_ACTION_MESHPEERING_CLOSE,
44159aa14a9SRui Paulo 	    mesh_recv_action_meshpeering_close);
442bdd2a076SAdrian Chadd 	ieee80211_recv_action_register(IEEE80211_ACTION_CAT_MESH,
443bdd2a076SAdrian Chadd 	    IEEE80211_ACTION_MESH_LMETRIC, mesh_recv_action_meshlmetric);
44459aa14a9SRui Paulo 
445ebeaa1adSMonthadar Al Jaberi 	ieee80211_send_action_register(IEEE80211_ACTION_CAT_SELF_PROT,
44659aa14a9SRui Paulo 	    IEEE80211_ACTION_MESHPEERING_OPEN,
44759aa14a9SRui Paulo 	    mesh_send_action_meshpeering_open);
448ebeaa1adSMonthadar Al Jaberi 	ieee80211_send_action_register(IEEE80211_ACTION_CAT_SELF_PROT,
44959aa14a9SRui Paulo 	    IEEE80211_ACTION_MESHPEERING_CONFIRM,
45059aa14a9SRui Paulo 	    mesh_send_action_meshpeering_confirm);
451ebeaa1adSMonthadar Al Jaberi 	ieee80211_send_action_register(IEEE80211_ACTION_CAT_SELF_PROT,
45259aa14a9SRui Paulo 	    IEEE80211_ACTION_MESHPEERING_CLOSE,
45359aa14a9SRui Paulo 	    mesh_send_action_meshpeering_close);
454bdd2a076SAdrian Chadd 	ieee80211_send_action_register(IEEE80211_ACTION_CAT_MESH,
455bdd2a076SAdrian Chadd 	    IEEE80211_ACTION_MESH_LMETRIC,
456bdd2a076SAdrian Chadd 	    mesh_send_action_meshlmetric);
45759aa14a9SRui Paulo 
45859aa14a9SRui Paulo 	/*
45959aa14a9SRui Paulo 	 * Register Airtime Link Metric.
46059aa14a9SRui Paulo 	 */
46159aa14a9SRui Paulo 	ieee80211_mesh_register_proto_metric(&mesh_metric_airtime);
46259aa14a9SRui Paulo 
46359aa14a9SRui Paulo }
46459aa14a9SRui Paulo SYSINIT(wlan_mesh, SI_SUB_DRIVERS, SI_ORDER_FIRST, ieee80211_mesh_init, NULL);
46559aa14a9SRui Paulo 
46659aa14a9SRui Paulo void
46759aa14a9SRui Paulo ieee80211_mesh_attach(struct ieee80211com *ic)
46859aa14a9SRui Paulo {
46959aa14a9SRui Paulo 	ic->ic_vattach[IEEE80211_M_MBSS] = mesh_vattach;
47059aa14a9SRui Paulo }
47159aa14a9SRui Paulo 
47259aa14a9SRui Paulo void
47359aa14a9SRui Paulo ieee80211_mesh_detach(struct ieee80211com *ic)
47459aa14a9SRui Paulo {
47559aa14a9SRui Paulo }
47659aa14a9SRui Paulo 
47759aa14a9SRui Paulo static void
47859aa14a9SRui Paulo mesh_vdetach_peers(void *arg, struct ieee80211_node *ni)
47959aa14a9SRui Paulo {
48059aa14a9SRui Paulo 	struct ieee80211com *ic = ni->ni_ic;
48159aa14a9SRui Paulo 	uint16_t args[3];
48259aa14a9SRui Paulo 
48359aa14a9SRui Paulo 	if (ni->ni_mlstate == IEEE80211_NODE_MESH_ESTABLISHED) {
48459aa14a9SRui Paulo 		args[0] = ni->ni_mlpid;
48559aa14a9SRui Paulo 		args[1] = ni->ni_mllid;
48659aa14a9SRui Paulo 		args[2] = IEEE80211_REASON_PEER_LINK_CANCELED;
48759aa14a9SRui Paulo 		ieee80211_send_action(ni,
488ebeaa1adSMonthadar Al Jaberi 		    IEEE80211_ACTION_CAT_SELF_PROT,
48959aa14a9SRui Paulo 		    IEEE80211_ACTION_MESHPEERING_CLOSE,
49059aa14a9SRui Paulo 		    args);
49159aa14a9SRui Paulo 	}
492c104cff2SRui Paulo 	callout_drain(&ni->ni_mltimer);
49359aa14a9SRui Paulo 	/* XXX belongs in hwmp */
49459aa14a9SRui Paulo 	ieee80211_ageq_drain_node(&ic->ic_stageq,
49559aa14a9SRui Paulo 	   (void *)(uintptr_t) ieee80211_mac_hash(ic, ni->ni_macaddr));
49659aa14a9SRui Paulo }
49759aa14a9SRui Paulo 
49859aa14a9SRui Paulo static void
49959aa14a9SRui Paulo mesh_vdetach(struct ieee80211vap *vap)
50059aa14a9SRui Paulo {
50159aa14a9SRui Paulo 	struct ieee80211_mesh_state *ms = vap->iv_mesh;
50259aa14a9SRui Paulo 
503c104cff2SRui Paulo 	callout_drain(&ms->ms_cleantimer);
50459aa14a9SRui Paulo 	ieee80211_iterate_nodes(&vap->iv_ic->ic_sta, mesh_vdetach_peers,
50559aa14a9SRui Paulo 	    NULL);
50659aa14a9SRui Paulo 	ieee80211_mesh_rt_flush(vap);
50759aa14a9SRui Paulo 	mtx_destroy(&ms->ms_rt_lock);
50859aa14a9SRui Paulo 	ms->ms_ppath->mpp_vdetach(vap);
50959aa14a9SRui Paulo 	free(vap->iv_mesh, M_80211_VAP);
51059aa14a9SRui Paulo 	vap->iv_mesh = NULL;
51159aa14a9SRui Paulo }
51259aa14a9SRui Paulo 
51359aa14a9SRui Paulo static void
51459aa14a9SRui Paulo mesh_vattach(struct ieee80211vap *vap)
51559aa14a9SRui Paulo {
51659aa14a9SRui Paulo 	struct ieee80211_mesh_state *ms;
51759aa14a9SRui Paulo 	vap->iv_newstate = mesh_newstate;
51859aa14a9SRui Paulo 	vap->iv_input = mesh_input;
51959aa14a9SRui Paulo 	vap->iv_opdetach = mesh_vdetach;
52059aa14a9SRui Paulo 	vap->iv_recv_mgmt = mesh_recv_mgmt;
5210917631fSRui Paulo 	vap->iv_recv_ctl = mesh_recv_ctl;
52259aa14a9SRui Paulo 	ms = malloc(sizeof(struct ieee80211_mesh_state), M_80211_VAP,
52359aa14a9SRui Paulo 	    M_NOWAIT | M_ZERO);
52459aa14a9SRui Paulo 	if (ms == NULL) {
52559aa14a9SRui Paulo 		printf("%s: couldn't alloc MBSS state\n", __func__);
52659aa14a9SRui Paulo 		return;
52759aa14a9SRui Paulo 	}
52859aa14a9SRui Paulo 	vap->iv_mesh = ms;
52959aa14a9SRui Paulo 	ms->ms_seq = 0;
53059aa14a9SRui Paulo 	ms->ms_flags = (IEEE80211_MESHFLAGS_AP | IEEE80211_MESHFLAGS_FWD);
53159aa14a9SRui Paulo 	ms->ms_ttl = IEEE80211_MESH_DEFAULT_TTL;
53259aa14a9SRui Paulo 	TAILQ_INIT(&ms->ms_routes);
53359aa14a9SRui Paulo 	mtx_init(&ms->ms_rt_lock, "MBSS", "802.11s routing table", MTX_DEF);
534c104cff2SRui Paulo 	callout_init(&ms->ms_cleantimer, CALLOUT_MPSAFE);
53559aa14a9SRui Paulo 	mesh_select_proto_metric(vap, "AIRTIME");
53659aa14a9SRui Paulo 	KASSERT(ms->ms_pmetric, ("ms_pmetric == NULL"));
53759aa14a9SRui Paulo 	mesh_select_proto_path(vap, "HWMP");
53859aa14a9SRui Paulo 	KASSERT(ms->ms_ppath, ("ms_ppath == NULL"));
53959aa14a9SRui Paulo 	ms->ms_ppath->mpp_vattach(vap);
54059aa14a9SRui Paulo }
54159aa14a9SRui Paulo 
54259aa14a9SRui Paulo /*
54359aa14a9SRui Paulo  * IEEE80211_M_MBSS vap state machine handler.
54459aa14a9SRui Paulo  */
54559aa14a9SRui Paulo static int
54659aa14a9SRui Paulo mesh_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
54759aa14a9SRui Paulo {
54859aa14a9SRui Paulo 	struct ieee80211_mesh_state *ms = vap->iv_mesh;
54959aa14a9SRui Paulo 	struct ieee80211com *ic = vap->iv_ic;
55059aa14a9SRui Paulo 	struct ieee80211_node *ni;
55159aa14a9SRui Paulo 	enum ieee80211_state ostate;
55259aa14a9SRui Paulo 
55359aa14a9SRui Paulo 	IEEE80211_LOCK_ASSERT(ic);
55459aa14a9SRui Paulo 
55559aa14a9SRui Paulo 	ostate = vap->iv_state;
55659aa14a9SRui Paulo 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE, "%s: %s -> %s (%d)\n",
55759aa14a9SRui Paulo 	    __func__, ieee80211_state_name[ostate],
55859aa14a9SRui Paulo 	    ieee80211_state_name[nstate], arg);
55959aa14a9SRui Paulo 	vap->iv_state = nstate;		/* state transition */
56059aa14a9SRui Paulo 	if (ostate != IEEE80211_S_SCAN)
56159aa14a9SRui Paulo 		ieee80211_cancel_scan(vap);	/* background scan */
56259aa14a9SRui Paulo 	ni = vap->iv_bss;			/* NB: no reference held */
563c104cff2SRui Paulo 	if (nstate != IEEE80211_S_RUN && ostate == IEEE80211_S_RUN)
564c104cff2SRui Paulo 		callout_drain(&ms->ms_cleantimer);
56559aa14a9SRui Paulo 	switch (nstate) {
56659aa14a9SRui Paulo 	case IEEE80211_S_INIT:
56759aa14a9SRui Paulo 		switch (ostate) {
56859aa14a9SRui Paulo 		case IEEE80211_S_SCAN:
56959aa14a9SRui Paulo 			ieee80211_cancel_scan(vap);
57059aa14a9SRui Paulo 			break;
57159aa14a9SRui Paulo 		case IEEE80211_S_CAC:
57259aa14a9SRui Paulo 			ieee80211_dfs_cac_stop(vap);
57359aa14a9SRui Paulo 			break;
57459aa14a9SRui Paulo 		case IEEE80211_S_RUN:
57559aa14a9SRui Paulo 			ieee80211_iterate_nodes(&ic->ic_sta,
57659aa14a9SRui Paulo 			    mesh_vdetach_peers, NULL);
57759aa14a9SRui Paulo 			break;
57859aa14a9SRui Paulo 		default:
57959aa14a9SRui Paulo 			break;
58059aa14a9SRui Paulo 		}
58159aa14a9SRui Paulo 		if (ostate != IEEE80211_S_INIT) {
58259aa14a9SRui Paulo 			/* NB: optimize INIT -> INIT case */
58359aa14a9SRui Paulo 			ieee80211_reset_bss(vap);
584c104cff2SRui Paulo 			ieee80211_mesh_rt_flush(vap);
58559aa14a9SRui Paulo 		}
58659aa14a9SRui Paulo 		break;
58759aa14a9SRui Paulo 	case IEEE80211_S_SCAN:
58859aa14a9SRui Paulo 		switch (ostate) {
58959aa14a9SRui Paulo 		case IEEE80211_S_INIT:
59059aa14a9SRui Paulo 			if (vap->iv_des_chan != IEEE80211_CHAN_ANYC &&
59159aa14a9SRui Paulo 			    !IEEE80211_IS_CHAN_RADAR(vap->iv_des_chan) &&
59259aa14a9SRui Paulo 			    ms->ms_idlen != 0) {
59359aa14a9SRui Paulo 				/*
59459aa14a9SRui Paulo 				 * Already have a channel and a mesh ID; bypass
59559aa14a9SRui Paulo 				 * the scan and startup immediately.
59659aa14a9SRui Paulo 				 */
59759aa14a9SRui Paulo 				ieee80211_create_ibss(vap, vap->iv_des_chan);
59859aa14a9SRui Paulo 				break;
59959aa14a9SRui Paulo 			}
60059aa14a9SRui Paulo 			/*
60159aa14a9SRui Paulo 			 * Initiate a scan.  We can come here as a result
60259aa14a9SRui Paulo 			 * of an IEEE80211_IOC_SCAN_REQ too in which case
60359aa14a9SRui Paulo 			 * the vap will be marked with IEEE80211_FEXT_SCANREQ
60459aa14a9SRui Paulo 			 * and the scan request parameters will be present
60559aa14a9SRui Paulo 			 * in iv_scanreq.  Otherwise we do the default.
60659aa14a9SRui Paulo 			*/
60759aa14a9SRui Paulo 			if (vap->iv_flags_ext & IEEE80211_FEXT_SCANREQ) {
60859aa14a9SRui Paulo 				ieee80211_check_scan(vap,
60959aa14a9SRui Paulo 				    vap->iv_scanreq_flags,
61059aa14a9SRui Paulo 				    vap->iv_scanreq_duration,
61159aa14a9SRui Paulo 				    vap->iv_scanreq_mindwell,
61259aa14a9SRui Paulo 				    vap->iv_scanreq_maxdwell,
61359aa14a9SRui Paulo 				    vap->iv_scanreq_nssid, vap->iv_scanreq_ssid);
61459aa14a9SRui Paulo 				vap->iv_flags_ext &= ~IEEE80211_FEXT_SCANREQ;
61559aa14a9SRui Paulo 			} else
61659aa14a9SRui Paulo 				ieee80211_check_scan_current(vap);
61759aa14a9SRui Paulo 			break;
61859aa14a9SRui Paulo 		default:
61959aa14a9SRui Paulo 			break;
62059aa14a9SRui Paulo 		}
62159aa14a9SRui Paulo 		break;
62259aa14a9SRui Paulo 	case IEEE80211_S_CAC:
62359aa14a9SRui Paulo 		/*
62459aa14a9SRui Paulo 		 * Start CAC on a DFS channel.  We come here when starting
62559aa14a9SRui Paulo 		 * a bss on a DFS channel (see ieee80211_create_ibss).
62659aa14a9SRui Paulo 		 */
62759aa14a9SRui Paulo 		ieee80211_dfs_cac_start(vap);
62859aa14a9SRui Paulo 		break;
62959aa14a9SRui Paulo 	case IEEE80211_S_RUN:
63059aa14a9SRui Paulo 		switch (ostate) {
63159aa14a9SRui Paulo 		case IEEE80211_S_INIT:
63259aa14a9SRui Paulo 			/*
63359aa14a9SRui Paulo 			 * Already have a channel; bypass the
63459aa14a9SRui Paulo 			 * scan and startup immediately.
63559aa14a9SRui Paulo 			 * Note that ieee80211_create_ibss will call
63659aa14a9SRui Paulo 			 * back to do a RUN->RUN state change.
63759aa14a9SRui Paulo 			 */
63859aa14a9SRui Paulo 			ieee80211_create_ibss(vap,
63959aa14a9SRui Paulo 			    ieee80211_ht_adjust_channel(ic,
64059aa14a9SRui Paulo 				ic->ic_curchan, vap->iv_flags_ht));
64159aa14a9SRui Paulo 			/* NB: iv_bss is changed on return */
64259aa14a9SRui Paulo 			break;
64359aa14a9SRui Paulo 		case IEEE80211_S_CAC:
64459aa14a9SRui Paulo 			/*
64559aa14a9SRui Paulo 			 * NB: This is the normal state change when CAC
64659aa14a9SRui Paulo 			 * expires and no radar was detected; no need to
64759aa14a9SRui Paulo 			 * clear the CAC timer as it's already expired.
64859aa14a9SRui Paulo 			 */
64959aa14a9SRui Paulo 			/* fall thru... */
65059aa14a9SRui Paulo 		case IEEE80211_S_CSA:
65159aa14a9SRui Paulo #if 0
65259aa14a9SRui Paulo 			/*
65359aa14a9SRui Paulo 			 * Shorten inactivity timer of associated stations
65459aa14a9SRui Paulo 			 * to weed out sta's that don't follow a CSA.
65559aa14a9SRui Paulo 			 */
65659aa14a9SRui Paulo 			ieee80211_iterate_nodes(&ic->ic_sta, sta_csa, vap);
65759aa14a9SRui Paulo #endif
65859aa14a9SRui Paulo 			/*
65959aa14a9SRui Paulo 			 * Update bss node channel to reflect where
66059aa14a9SRui Paulo 			 * we landed after CSA.
66159aa14a9SRui Paulo 			 */
66259aa14a9SRui Paulo 			ieee80211_node_set_chan(vap->iv_bss,
66359aa14a9SRui Paulo 			    ieee80211_ht_adjust_channel(ic, ic->ic_curchan,
66459aa14a9SRui Paulo 				ieee80211_htchanflags(vap->iv_bss->ni_chan)));
66559aa14a9SRui Paulo 			/* XXX bypass debug msgs */
66659aa14a9SRui Paulo 			break;
66759aa14a9SRui Paulo 		case IEEE80211_S_SCAN:
66859aa14a9SRui Paulo 		case IEEE80211_S_RUN:
66959aa14a9SRui Paulo #ifdef IEEE80211_DEBUG
67059aa14a9SRui Paulo 			if (ieee80211_msg_debug(vap)) {
67159aa14a9SRui Paulo 				struct ieee80211_node *ni = vap->iv_bss;
67259aa14a9SRui Paulo 				ieee80211_note(vap,
67359aa14a9SRui Paulo 				    "synchronized with %s meshid ",
67459aa14a9SRui Paulo 				    ether_sprintf(ni->ni_meshid));
67559aa14a9SRui Paulo 				ieee80211_print_essid(ni->ni_meshid,
67659aa14a9SRui Paulo 				    ni->ni_meshidlen);
67759aa14a9SRui Paulo 				/* XXX MCS/HT */
67859aa14a9SRui Paulo 				printf(" channel %d\n",
67959aa14a9SRui Paulo 				    ieee80211_chan2ieee(ic, ic->ic_curchan));
68059aa14a9SRui Paulo 			}
68159aa14a9SRui Paulo #endif
68259aa14a9SRui Paulo 			break;
68359aa14a9SRui Paulo 		default:
68459aa14a9SRui Paulo 			break;
68559aa14a9SRui Paulo 		}
68659aa14a9SRui Paulo 		ieee80211_node_authorize(vap->iv_bss);
687e50821abSSam Leffler 		callout_reset(&ms->ms_cleantimer, ms->ms_ppath->mpp_inact,
688c104cff2SRui Paulo                     mesh_rt_cleanup_cb, vap);
68959aa14a9SRui Paulo 		break;
69059aa14a9SRui Paulo 	default:
69159aa14a9SRui Paulo 		break;
69259aa14a9SRui Paulo 	}
69359aa14a9SRui Paulo 	/* NB: ostate not nstate */
69459aa14a9SRui Paulo 	ms->ms_ppath->mpp_newstate(vap, ostate, arg);
69559aa14a9SRui Paulo 	return 0;
69659aa14a9SRui Paulo }
69759aa14a9SRui Paulo 
698c104cff2SRui Paulo static void
699c104cff2SRui Paulo mesh_rt_cleanup_cb(void *arg)
700c104cff2SRui Paulo {
701c104cff2SRui Paulo 	struct ieee80211vap *vap = arg;
702c104cff2SRui Paulo 	struct ieee80211_mesh_state *ms = vap->iv_mesh;
703c104cff2SRui Paulo 
704c104cff2SRui Paulo 	mesh_rt_flush_invalid(vap);
705e50821abSSam Leffler 	callout_reset(&ms->ms_cleantimer, ms->ms_ppath->mpp_inact,
706c104cff2SRui Paulo 	    mesh_rt_cleanup_cb, vap);
707c104cff2SRui Paulo }
708c104cff2SRui Paulo 
709c104cff2SRui Paulo 
71059aa14a9SRui Paulo /*
71159aa14a9SRui Paulo  * Helper function to note the Mesh Peer Link FSM change.
71259aa14a9SRui Paulo  */
71359aa14a9SRui Paulo static void
71459aa14a9SRui Paulo mesh_linkchange(struct ieee80211_node *ni, enum ieee80211_mesh_mlstate state)
71559aa14a9SRui Paulo {
71659aa14a9SRui Paulo 	struct ieee80211vap *vap = ni->ni_vap;
71759aa14a9SRui Paulo 	struct ieee80211_mesh_state *ms = vap->iv_mesh;
71859aa14a9SRui Paulo #ifdef IEEE80211_DEBUG
71959aa14a9SRui Paulo 	static const char *meshlinkstates[] = {
72059aa14a9SRui Paulo 		[IEEE80211_NODE_MESH_IDLE]		= "IDLE",
72159aa14a9SRui Paulo 		[IEEE80211_NODE_MESH_OPENSNT]		= "OPEN SENT",
72259aa14a9SRui Paulo 		[IEEE80211_NODE_MESH_OPENRCV]		= "OPEN RECEIVED",
72359aa14a9SRui Paulo 		[IEEE80211_NODE_MESH_CONFIRMRCV]	= "CONFIRM RECEIVED",
72459aa14a9SRui Paulo 		[IEEE80211_NODE_MESH_ESTABLISHED]	= "ESTABLISHED",
72559aa14a9SRui Paulo 		[IEEE80211_NODE_MESH_HOLDING]		= "HOLDING"
72659aa14a9SRui Paulo 	};
72759aa14a9SRui Paulo #endif
72859aa14a9SRui Paulo 	IEEE80211_NOTE(vap, IEEE80211_MSG_MESH,
72959aa14a9SRui Paulo 	    ni, "peer link: %s -> %s",
73059aa14a9SRui Paulo 	    meshlinkstates[ni->ni_mlstate], meshlinkstates[state]);
73159aa14a9SRui Paulo 
73259aa14a9SRui Paulo 	/* track neighbor count */
73359aa14a9SRui Paulo 	if (state == IEEE80211_NODE_MESH_ESTABLISHED &&
73459aa14a9SRui Paulo 	    ni->ni_mlstate != IEEE80211_NODE_MESH_ESTABLISHED) {
73559aa14a9SRui Paulo 		KASSERT(ms->ms_neighbors < 65535, ("neighbor count overflow"));
73659aa14a9SRui Paulo 		ms->ms_neighbors++;
737d093681cSRui Paulo 		ieee80211_beacon_notify(vap, IEEE80211_BEACON_MESHCONF);
73859aa14a9SRui Paulo 	} else if (ni->ni_mlstate == IEEE80211_NODE_MESH_ESTABLISHED &&
73959aa14a9SRui Paulo 	    state != IEEE80211_NODE_MESH_ESTABLISHED) {
74059aa14a9SRui Paulo 		KASSERT(ms->ms_neighbors > 0, ("neighbor count 0"));
74159aa14a9SRui Paulo 		ms->ms_neighbors--;
742d093681cSRui Paulo 		ieee80211_beacon_notify(vap, IEEE80211_BEACON_MESHCONF);
74359aa14a9SRui Paulo 	}
74459aa14a9SRui Paulo 	ni->ni_mlstate = state;
745c104cff2SRui Paulo 	switch (state) {
746c104cff2SRui Paulo 	case IEEE80211_NODE_MESH_HOLDING:
74759aa14a9SRui Paulo 		ms->ms_ppath->mpp_peerdown(ni);
748c104cff2SRui Paulo 		break;
749c104cff2SRui Paulo 	case IEEE80211_NODE_MESH_ESTABLISHED:
750c104cff2SRui Paulo 		ieee80211_mesh_discover(vap, ni->ni_macaddr, NULL);
751c104cff2SRui Paulo 		break;
752c104cff2SRui Paulo 	default:
753c104cff2SRui Paulo 		break;
754c104cff2SRui Paulo 	}
75559aa14a9SRui Paulo }
75659aa14a9SRui Paulo 
75759aa14a9SRui Paulo /*
75859aa14a9SRui Paulo  * Helper function to generate a unique local ID required for mesh
75959aa14a9SRui Paulo  * peer establishment.
76059aa14a9SRui Paulo  */
76159aa14a9SRui Paulo static void
76259aa14a9SRui Paulo mesh_checkid(void *arg, struct ieee80211_node *ni)
76359aa14a9SRui Paulo {
76459aa14a9SRui Paulo 	uint16_t *r = arg;
76559aa14a9SRui Paulo 
76659aa14a9SRui Paulo 	if (*r == ni->ni_mllid)
76759aa14a9SRui Paulo 		*(uint16_t *)arg = 0;
76859aa14a9SRui Paulo }
76959aa14a9SRui Paulo 
77059aa14a9SRui Paulo static uint32_t
77159aa14a9SRui Paulo mesh_generateid(struct ieee80211vap *vap)
77259aa14a9SRui Paulo {
77359aa14a9SRui Paulo 	int maxiter = 4;
77459aa14a9SRui Paulo 	uint16_t r;
77559aa14a9SRui Paulo 
77659aa14a9SRui Paulo 	do {
77759aa14a9SRui Paulo 		get_random_bytes(&r, 2);
77859aa14a9SRui Paulo 		ieee80211_iterate_nodes(&vap->iv_ic->ic_sta, mesh_checkid, &r);
77959aa14a9SRui Paulo 		maxiter--;
78059aa14a9SRui Paulo 	} while (r == 0 && maxiter > 0);
78159aa14a9SRui Paulo 	return r;
78259aa14a9SRui Paulo }
78359aa14a9SRui Paulo 
78459aa14a9SRui Paulo /*
78559aa14a9SRui Paulo  * Verifies if we already received this packet by checking its
78659aa14a9SRui Paulo  * sequence number.
7873ca80f0dSRui Paulo  * Returns 0 if the frame is to be accepted, 1 otherwise.
78859aa14a9SRui Paulo  */
78959aa14a9SRui Paulo static int
79059aa14a9SRui Paulo mesh_checkpseq(struct ieee80211vap *vap,
79159aa14a9SRui Paulo     const uint8_t source[IEEE80211_ADDR_LEN], uint32_t seq)
79259aa14a9SRui Paulo {
79359aa14a9SRui Paulo 	struct ieee80211_mesh_route *rt;
79459aa14a9SRui Paulo 
79559aa14a9SRui Paulo 	rt = ieee80211_mesh_rt_find(vap, source);
79659aa14a9SRui Paulo 	if (rt == NULL) {
7973ca80f0dSRui Paulo 		rt = ieee80211_mesh_rt_add(vap, source);
7983ca80f0dSRui Paulo 		if (rt == NULL) {
7993ca80f0dSRui Paulo 			IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH, source,
8003ca80f0dSRui Paulo 			    "%s", "add mcast route failed");
8013ca80f0dSRui Paulo 			vap->iv_stats.is_mesh_rtaddfailed++;
8023ca80f0dSRui Paulo 			return 1;
8033ca80f0dSRui Paulo 		}
804c104cff2SRui Paulo 		IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH, source,
805c104cff2SRui Paulo 		    "add mcast route, mesh seqno %d", seq);
80659aa14a9SRui Paulo 		rt->rt_lastmseq = seq;
80759aa14a9SRui Paulo 		return 0;
80859aa14a9SRui Paulo 	}
80959aa14a9SRui Paulo 	if (IEEE80211_MESH_SEQ_GEQ(rt->rt_lastmseq, seq)) {
81059aa14a9SRui Paulo 		return 1;
81159aa14a9SRui Paulo 	} else {
81259aa14a9SRui Paulo 		rt->rt_lastmseq = seq;
81359aa14a9SRui Paulo 		return 0;
81459aa14a9SRui Paulo 	}
81559aa14a9SRui Paulo }
81659aa14a9SRui Paulo 
81759aa14a9SRui Paulo /*
81859aa14a9SRui Paulo  * Iterate the routing table and locate the next hop.
81959aa14a9SRui Paulo  */
82059aa14a9SRui Paulo static struct ieee80211_node *
82159aa14a9SRui Paulo mesh_find_txnode(struct ieee80211vap *vap,
82259aa14a9SRui Paulo     const uint8_t dest[IEEE80211_ADDR_LEN])
82359aa14a9SRui Paulo {
82459aa14a9SRui Paulo 	struct ieee80211_mesh_route *rt;
82559aa14a9SRui Paulo 
82659aa14a9SRui Paulo 	rt = ieee80211_mesh_rt_find(vap, dest);
82759aa14a9SRui Paulo 	if (rt == NULL)
82859aa14a9SRui Paulo 		return NULL;
829c104cff2SRui Paulo 	if ((rt->rt_flags & IEEE80211_MESHRT_FLAGS_VALID) == 0 ||
830c104cff2SRui Paulo 	    (rt->rt_flags & IEEE80211_MESHRT_FLAGS_PROXY)) {
831c104cff2SRui Paulo 		IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH, dest,
832c104cff2SRui Paulo 		    "%s: !valid or proxy, flags 0x%x", __func__, rt->rt_flags);
833c104cff2SRui Paulo 		/* XXX stat */
834c104cff2SRui Paulo 		return NULL;
835c104cff2SRui Paulo 	}
83659aa14a9SRui Paulo 	return ieee80211_find_txnode(vap, rt->rt_nexthop);
83759aa14a9SRui Paulo }
83859aa14a9SRui Paulo 
83959aa14a9SRui Paulo /*
84059aa14a9SRui Paulo  * Forward the specified frame.
84159aa14a9SRui Paulo  * Decrement the TTL and set TA to our MAC address.
84259aa14a9SRui Paulo  */
84359aa14a9SRui Paulo static void
84459aa14a9SRui Paulo mesh_forward(struct ieee80211vap *vap, struct mbuf *m,
84559aa14a9SRui Paulo     const struct ieee80211_meshcntl *mc)
84659aa14a9SRui Paulo {
84759aa14a9SRui Paulo 	struct ieee80211com *ic = vap->iv_ic;
84859aa14a9SRui Paulo 	struct ieee80211_mesh_state *ms = vap->iv_mesh;
84959aa14a9SRui Paulo 	struct ifnet *ifp = vap->iv_ifp;
85059aa14a9SRui Paulo 	struct ifnet *parent = ic->ic_ifp;
85159aa14a9SRui Paulo 	const struct ieee80211_frame *wh =
85259aa14a9SRui Paulo 	    mtod(m, const struct ieee80211_frame *);
85359aa14a9SRui Paulo 	struct mbuf *mcopy;
85459aa14a9SRui Paulo 	struct ieee80211_meshcntl *mccopy;
85559aa14a9SRui Paulo 	struct ieee80211_frame *whcopy;
85659aa14a9SRui Paulo 	struct ieee80211_node *ni;
85759aa14a9SRui Paulo 	int err;
85859aa14a9SRui Paulo 
85959aa14a9SRui Paulo 	if (mc->mc_ttl == 0) {
86059aa14a9SRui Paulo 		IEEE80211_NOTE_FRAME(vap, IEEE80211_MSG_MESH, wh,
86159aa14a9SRui Paulo 		    "%s", "frame not fwd'd, ttl 0");
86259aa14a9SRui Paulo 		vap->iv_stats.is_mesh_fwd_ttl++;
86359aa14a9SRui Paulo 		return;
86459aa14a9SRui Paulo 	}
86559aa14a9SRui Paulo 	if (!(ms->ms_flags & IEEE80211_MESHFLAGS_FWD)) {
86659aa14a9SRui Paulo 		IEEE80211_NOTE_FRAME(vap, IEEE80211_MSG_MESH, wh,
86759aa14a9SRui Paulo 		    "%s", "frame not fwd'd, fwding disabled");
86859aa14a9SRui Paulo 		vap->iv_stats.is_mesh_fwd_disabled++;
86959aa14a9SRui Paulo 		return;
87059aa14a9SRui Paulo 	}
87159aa14a9SRui Paulo 	mcopy = m_dup(m, M_DONTWAIT);
87259aa14a9SRui Paulo 	if (mcopy == NULL) {
87359aa14a9SRui Paulo 		IEEE80211_NOTE_FRAME(vap, IEEE80211_MSG_MESH, wh,
87459aa14a9SRui Paulo 		    "%s", "frame not fwd'd, cannot dup");
87559aa14a9SRui Paulo 		vap->iv_stats.is_mesh_fwd_nobuf++;
87659aa14a9SRui Paulo 		ifp->if_oerrors++;
87759aa14a9SRui Paulo 		return;
87859aa14a9SRui Paulo 	}
87959aa14a9SRui Paulo 	mcopy = m_pullup(mcopy, ieee80211_hdrspace(ic, wh) +
88059aa14a9SRui Paulo 	    sizeof(struct ieee80211_meshcntl));
88159aa14a9SRui Paulo 	if (mcopy == NULL) {
88259aa14a9SRui Paulo 		IEEE80211_NOTE_FRAME(vap, IEEE80211_MSG_MESH, wh,
88359aa14a9SRui Paulo 		    "%s", "frame not fwd'd, too short");
88459aa14a9SRui Paulo 		vap->iv_stats.is_mesh_fwd_tooshort++;
88559aa14a9SRui Paulo 		ifp->if_oerrors++;
88659aa14a9SRui Paulo 		m_freem(mcopy);
88759aa14a9SRui Paulo 		return;
88859aa14a9SRui Paulo 	}
88959aa14a9SRui Paulo 	whcopy = mtod(mcopy, struct ieee80211_frame *);
89059aa14a9SRui Paulo 	mccopy = (struct ieee80211_meshcntl *)
89159aa14a9SRui Paulo 	    (mtod(mcopy, uint8_t *) + ieee80211_hdrspace(ic, wh));
89259aa14a9SRui Paulo 	/* XXX clear other bits? */
89359aa14a9SRui Paulo 	whcopy->i_fc[1] &= ~IEEE80211_FC1_RETRY;
89459aa14a9SRui Paulo 	IEEE80211_ADDR_COPY(whcopy->i_addr2, vap->iv_myaddr);
89559aa14a9SRui Paulo 	if (IEEE80211_IS_MULTICAST(wh->i_addr1)) {
89659aa14a9SRui Paulo 		ni = ieee80211_ref_node(vap->iv_bss);
89759aa14a9SRui Paulo 		mcopy->m_flags |= M_MCAST;
89859aa14a9SRui Paulo 	} else {
89959aa14a9SRui Paulo 		ni = mesh_find_txnode(vap, whcopy->i_addr3);
90059aa14a9SRui Paulo 		if (ni == NULL) {
90159aa14a9SRui Paulo 			IEEE80211_NOTE_FRAME(vap, IEEE80211_MSG_MESH, wh,
90259aa14a9SRui Paulo 			    "%s", "frame not fwd'd, no path");
90359aa14a9SRui Paulo 			vap->iv_stats.is_mesh_fwd_nopath++;
90459aa14a9SRui Paulo 			m_freem(mcopy);
90559aa14a9SRui Paulo 			return;
90659aa14a9SRui Paulo 		}
90759aa14a9SRui Paulo 		IEEE80211_ADDR_COPY(whcopy->i_addr1, ni->ni_macaddr);
90859aa14a9SRui Paulo 	}
90959aa14a9SRui Paulo 	KASSERT(mccopy->mc_ttl > 0, ("%s called with wrong ttl", __func__));
91059aa14a9SRui Paulo 	mccopy->mc_ttl--;
91159aa14a9SRui Paulo 
91259aa14a9SRui Paulo 	/* XXX calculate priority so drivers can find the tx queue */
91359aa14a9SRui Paulo 	M_WME_SETAC(mcopy, WME_AC_BE);
91459aa14a9SRui Paulo 
91559aa14a9SRui Paulo 	/* XXX do we know m_nextpkt is NULL? */
91659aa14a9SRui Paulo 	mcopy->m_pkthdr.rcvif = (void *) ni;
91759aa14a9SRui Paulo 	err = parent->if_transmit(parent, mcopy);
91859aa14a9SRui Paulo 	if (err != 0) {
91959aa14a9SRui Paulo 		/* NB: IFQ_HANDOFF reclaims mbuf */
92059aa14a9SRui Paulo 		ieee80211_free_node(ni);
92159aa14a9SRui Paulo 	} else {
92259aa14a9SRui Paulo 		ifp->if_opackets++;
92359aa14a9SRui Paulo 	}
92459aa14a9SRui Paulo }
92559aa14a9SRui Paulo 
926c104cff2SRui Paulo static struct mbuf *
927c104cff2SRui Paulo mesh_decap(struct ieee80211vap *vap, struct mbuf *m, int hdrlen, int meshdrlen)
928c104cff2SRui Paulo {
929c104cff2SRui Paulo #define	WHDIR(wh) ((wh)->i_fc[1] & IEEE80211_FC1_DIR_MASK)
930c104cff2SRui Paulo 	uint8_t b[sizeof(struct ieee80211_qosframe_addr4) +
931c104cff2SRui Paulo 		  sizeof(struct ieee80211_meshcntl_ae11)];
932c104cff2SRui Paulo 	const struct ieee80211_qosframe_addr4 *wh;
933c104cff2SRui Paulo 	const struct ieee80211_meshcntl_ae10 *mc;
934c104cff2SRui Paulo 	struct ether_header *eh;
935c104cff2SRui Paulo 	struct llc *llc;
936c104cff2SRui Paulo 	int ae;
937c104cff2SRui Paulo 
938c104cff2SRui Paulo 	if (m->m_len < hdrlen + sizeof(*llc) &&
939c104cff2SRui Paulo 	    (m = m_pullup(m, hdrlen + sizeof(*llc))) == NULL) {
940c104cff2SRui Paulo 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_ANY,
941c104cff2SRui Paulo 		    "discard data frame: %s", "m_pullup failed");
942c104cff2SRui Paulo 		vap->iv_stats.is_rx_tooshort++;
943c104cff2SRui Paulo 		return NULL;
944c104cff2SRui Paulo 	}
945c104cff2SRui Paulo 	memcpy(b, mtod(m, caddr_t), hdrlen);
946c104cff2SRui Paulo 	wh = (const struct ieee80211_qosframe_addr4 *)&b[0];
947c104cff2SRui Paulo 	mc = (const struct ieee80211_meshcntl_ae10 *)&b[hdrlen - meshdrlen];
948c104cff2SRui Paulo 	KASSERT(WHDIR(wh) == IEEE80211_FC1_DIR_FROMDS ||
949c104cff2SRui Paulo 		WHDIR(wh) == IEEE80211_FC1_DIR_DSTODS,
950c104cff2SRui Paulo 	    ("bogus dir, fc 0x%x:0x%x", wh->i_fc[0], wh->i_fc[1]));
951c104cff2SRui Paulo 
952c104cff2SRui Paulo 	llc = (struct llc *)(mtod(m, caddr_t) + hdrlen);
953c104cff2SRui Paulo 	if (llc->llc_dsap == LLC_SNAP_LSAP && llc->llc_ssap == LLC_SNAP_LSAP &&
954c104cff2SRui Paulo 	    llc->llc_control == LLC_UI && llc->llc_snap.org_code[0] == 0 &&
955c104cff2SRui Paulo 	    llc->llc_snap.org_code[1] == 0 && llc->llc_snap.org_code[2] == 0 &&
956c104cff2SRui Paulo 	    /* NB: preserve AppleTalk frames that have a native SNAP hdr */
957c104cff2SRui Paulo 	    !(llc->llc_snap.ether_type == htons(ETHERTYPE_AARP) ||
958c104cff2SRui Paulo 	      llc->llc_snap.ether_type == htons(ETHERTYPE_IPX))) {
959c104cff2SRui Paulo 		m_adj(m, hdrlen + sizeof(struct llc) - sizeof(*eh));
960c104cff2SRui Paulo 		llc = NULL;
961c104cff2SRui Paulo 	} else {
962c104cff2SRui Paulo 		m_adj(m, hdrlen - sizeof(*eh));
963c104cff2SRui Paulo 	}
964c104cff2SRui Paulo 	eh = mtod(m, struct ether_header *);
965c104cff2SRui Paulo 	ae = mc->mc_flags & 3;
966c104cff2SRui Paulo 	if (WHDIR(wh) == IEEE80211_FC1_DIR_FROMDS) {
967c104cff2SRui Paulo 		IEEE80211_ADDR_COPY(eh->ether_dhost, wh->i_addr1);
968c104cff2SRui Paulo 		if (ae == 0) {
969c104cff2SRui Paulo 			IEEE80211_ADDR_COPY(eh->ether_shost, wh->i_addr3);
970c104cff2SRui Paulo 		} else if (ae == 1) {
971c104cff2SRui Paulo 			IEEE80211_ADDR_COPY(eh->ether_shost, mc->mc_addr4);
972c104cff2SRui Paulo 		} else {
973c104cff2SRui Paulo 			IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
974c104cff2SRui Paulo 			    (const struct ieee80211_frame *)wh, NULL,
975c104cff2SRui Paulo 			    "bad AE %d", ae);
976c104cff2SRui Paulo 			vap->iv_stats.is_mesh_badae++;
977c104cff2SRui Paulo 			m_freem(m);
978c104cff2SRui Paulo 			return NULL;
979c104cff2SRui Paulo 		}
980c104cff2SRui Paulo 	} else {
981c104cff2SRui Paulo 		if (ae == 0) {
982c104cff2SRui Paulo 			IEEE80211_ADDR_COPY(eh->ether_dhost, wh->i_addr3);
983c104cff2SRui Paulo 			IEEE80211_ADDR_COPY(eh->ether_shost, wh->i_addr4);
984c104cff2SRui Paulo 		} else if (ae == 2) {
985c104cff2SRui Paulo 			IEEE80211_ADDR_COPY(eh->ether_dhost, mc->mc_addr4);
986c104cff2SRui Paulo 			IEEE80211_ADDR_COPY(eh->ether_shost, mc->mc_addr5);
987c104cff2SRui Paulo 		} else {
988c104cff2SRui Paulo 			IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
989c104cff2SRui Paulo 			    (const struct ieee80211_frame *)wh, NULL,
990c104cff2SRui Paulo 			    "bad AE %d", ae);
991c104cff2SRui Paulo 			vap->iv_stats.is_mesh_badae++;
992c104cff2SRui Paulo 			m_freem(m);
993c104cff2SRui Paulo 			return NULL;
994c104cff2SRui Paulo 		}
995c104cff2SRui Paulo 	}
996c104cff2SRui Paulo #ifdef ALIGNED_POINTER
997c104cff2SRui Paulo 	if (!ALIGNED_POINTER(mtod(m, caddr_t) + sizeof(*eh), uint32_t)) {
998c104cff2SRui Paulo 		m = ieee80211_realign(vap, m, sizeof(*eh));
999c104cff2SRui Paulo 		if (m == NULL)
1000c104cff2SRui Paulo 			return NULL;
1001c104cff2SRui Paulo 	}
1002c104cff2SRui Paulo #endif /* ALIGNED_POINTER */
1003c104cff2SRui Paulo 	if (llc != NULL) {
1004c104cff2SRui Paulo 		eh = mtod(m, struct ether_header *);
1005c104cff2SRui Paulo 		eh->ether_type = htons(m->m_pkthdr.len - sizeof(*eh));
1006c104cff2SRui Paulo 	}
1007c104cff2SRui Paulo 	return m;
1008c104cff2SRui Paulo #undef WDIR
1009c104cff2SRui Paulo }
1010c104cff2SRui Paulo 
1011c104cff2SRui Paulo /*
1012c104cff2SRui Paulo  * Return non-zero if the unicast mesh data frame should be processed
1013c104cff2SRui Paulo  * locally.  Frames that are not proxy'd have our address, otherwise
1014c104cff2SRui Paulo  * we need to consult the routing table to look for a proxy entry.
1015c104cff2SRui Paulo  */
1016c104cff2SRui Paulo static __inline int
1017c104cff2SRui Paulo mesh_isucastforme(struct ieee80211vap *vap, const struct ieee80211_frame *wh,
1018c104cff2SRui Paulo     const struct ieee80211_meshcntl *mc)
1019c104cff2SRui Paulo {
1020c104cff2SRui Paulo 	int ae = mc->mc_flags & 3;
1021c104cff2SRui Paulo 
1022c104cff2SRui Paulo 	KASSERT((wh->i_fc[1] & IEEE80211_FC1_DIR_MASK) == IEEE80211_FC1_DIR_DSTODS,
1023c104cff2SRui Paulo 	    ("bad dir 0x%x:0x%x", wh->i_fc[0], wh->i_fc[1]));
1024c104cff2SRui Paulo 	KASSERT(ae == 0 || ae == 2, ("bad AE %d", ae));
1025c104cff2SRui Paulo 	if (ae == 2) {				/* ucast w/ proxy */
1026c104cff2SRui Paulo 		const struct ieee80211_meshcntl_ae10 *mc10 =
1027c104cff2SRui Paulo 		    (const struct ieee80211_meshcntl_ae10 *) mc;
1028c104cff2SRui Paulo 		struct ieee80211_mesh_route *rt =
1029c104cff2SRui Paulo 		    ieee80211_mesh_rt_find(vap, mc10->mc_addr4);
1030c104cff2SRui Paulo 		/* check for proxy route to ourself */
1031c104cff2SRui Paulo 		return (rt != NULL &&
1032c104cff2SRui Paulo 		    (rt->rt_flags & IEEE80211_MESHRT_FLAGS_PROXY));
1033c104cff2SRui Paulo 	} else					/* ucast w/o proxy */
1034c104cff2SRui Paulo 		return IEEE80211_ADDR_EQ(wh->i_addr3, vap->iv_myaddr);
1035c104cff2SRui Paulo }
1036c104cff2SRui Paulo 
103759aa14a9SRui Paulo static int
103859aa14a9SRui Paulo mesh_input(struct ieee80211_node *ni, struct mbuf *m, int rssi, int nf)
103959aa14a9SRui Paulo {
104059aa14a9SRui Paulo #define	HAS_SEQ(type)	((type & 0x4) == 0)
104159aa14a9SRui Paulo 	struct ieee80211vap *vap = ni->ni_vap;
104259aa14a9SRui Paulo 	struct ieee80211com *ic = ni->ni_ic;
104359aa14a9SRui Paulo 	struct ifnet *ifp = vap->iv_ifp;
104459aa14a9SRui Paulo 	struct ieee80211_frame *wh;
104559aa14a9SRui Paulo 	const struct ieee80211_meshcntl *mc;
1046c104cff2SRui Paulo 	int hdrspace, meshdrlen, need_tap;
104791216c71SAdrian Chadd 	uint8_t dir, type, subtype;
104859aa14a9SRui Paulo 	uint32_t seq;
104991216c71SAdrian Chadd 	uint8_t *addr, qos[2];
105059aa14a9SRui Paulo 	ieee80211_seq rxseq;
105159aa14a9SRui Paulo 
105259aa14a9SRui Paulo 	KASSERT(ni != NULL, ("null node"));
105359aa14a9SRui Paulo 	ni->ni_inact = ni->ni_inact_reload;
105459aa14a9SRui Paulo 
105559aa14a9SRui Paulo 	need_tap = 1;			/* mbuf need to be tapped. */
105659aa14a9SRui Paulo 	type = -1;			/* undefined */
105759aa14a9SRui Paulo 
105859aa14a9SRui Paulo 	if (m->m_pkthdr.len < sizeof(struct ieee80211_frame_min)) {
105959aa14a9SRui Paulo 		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
106059aa14a9SRui Paulo 		    ni->ni_macaddr, NULL,
106159aa14a9SRui Paulo 		    "too short (1): len %u", m->m_pkthdr.len);
106259aa14a9SRui Paulo 		vap->iv_stats.is_rx_tooshort++;
106359aa14a9SRui Paulo 		goto out;
106459aa14a9SRui Paulo 	}
106559aa14a9SRui Paulo 	/*
106659aa14a9SRui Paulo 	 * Bit of a cheat here, we use a pointer for a 3-address
106759aa14a9SRui Paulo 	 * frame format but don't reference fields past outside
106859aa14a9SRui Paulo 	 * ieee80211_frame_min w/o first validating the data is
106959aa14a9SRui Paulo 	 * present.
107059aa14a9SRui Paulo 	*/
107159aa14a9SRui Paulo 	wh = mtod(m, struct ieee80211_frame *);
107259aa14a9SRui Paulo 
107359aa14a9SRui Paulo 	if ((wh->i_fc[0] & IEEE80211_FC0_VERSION_MASK) !=
107459aa14a9SRui Paulo 	    IEEE80211_FC0_VERSION_0) {
107559aa14a9SRui Paulo 		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
107659aa14a9SRui Paulo 		    ni->ni_macaddr, NULL, "wrong version %x", wh->i_fc[0]);
107759aa14a9SRui Paulo 		vap->iv_stats.is_rx_badversion++;
107859aa14a9SRui Paulo 		goto err;
107959aa14a9SRui Paulo 	}
108059aa14a9SRui Paulo 	dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK;
108159aa14a9SRui Paulo 	type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
108259aa14a9SRui Paulo 	subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
108359aa14a9SRui Paulo 	if ((ic->ic_flags & IEEE80211_F_SCAN) == 0) {
108459aa14a9SRui Paulo 		IEEE80211_RSSI_LPF(ni->ni_avgrssi, rssi);
108559aa14a9SRui Paulo 		ni->ni_noise = nf;
108659aa14a9SRui Paulo 		if (HAS_SEQ(type)) {
108759aa14a9SRui Paulo 			uint8_t tid = ieee80211_gettid(wh);
108859aa14a9SRui Paulo 
108959aa14a9SRui Paulo 			if (IEEE80211_QOS_HAS_SEQ(wh) &&
109059aa14a9SRui Paulo 			    TID_TO_WME_AC(tid) >= WME_AC_VI)
109159aa14a9SRui Paulo 				ic->ic_wme.wme_hipri_traffic++;
109259aa14a9SRui Paulo 			rxseq = le16toh(*(uint16_t *)wh->i_seq);
1093cd0b8f2dSAdrian Chadd 			if (! ieee80211_check_rxseq(ni, wh)) {
109459aa14a9SRui Paulo 				/* duplicate, discard */
109559aa14a9SRui Paulo 				IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
109659aa14a9SRui Paulo 				    wh->i_addr1, "duplicate",
109759aa14a9SRui Paulo 				    "seqno <%u,%u> fragno <%u,%u> tid %u",
109859aa14a9SRui Paulo 				    rxseq >> IEEE80211_SEQ_SEQ_SHIFT,
109959aa14a9SRui Paulo 				    ni->ni_rxseqs[tid] >>
110059aa14a9SRui Paulo 				    IEEE80211_SEQ_SEQ_SHIFT,
110159aa14a9SRui Paulo 				    rxseq & IEEE80211_SEQ_FRAG_MASK,
110259aa14a9SRui Paulo 				    ni->ni_rxseqs[tid] &
110359aa14a9SRui Paulo 				    IEEE80211_SEQ_FRAG_MASK,
110459aa14a9SRui Paulo 				    tid);
110559aa14a9SRui Paulo 				vap->iv_stats.is_rx_dup++;
110659aa14a9SRui Paulo 				IEEE80211_NODE_STAT(ni, rx_dup);
110759aa14a9SRui Paulo 				goto out;
110859aa14a9SRui Paulo 			}
110959aa14a9SRui Paulo 			ni->ni_rxseqs[tid] = rxseq;
111059aa14a9SRui Paulo 		}
111159aa14a9SRui Paulo 	}
111259aa14a9SRui Paulo #ifdef IEEE80211_DEBUG
111359aa14a9SRui Paulo 	/*
111459aa14a9SRui Paulo 	 * It's easier, but too expensive, to simulate different mesh
111559aa14a9SRui Paulo 	 * topologies by consulting the ACL policy very early, so do this
111659aa14a9SRui Paulo 	 * only under DEBUG.
111759aa14a9SRui Paulo 	 *
111859aa14a9SRui Paulo 	 * NB: this check is also done upon peering link initiation.
111959aa14a9SRui Paulo 	 */
11205a8801b0SBernhard Schmidt 	if (vap->iv_acl != NULL && !vap->iv_acl->iac_check(vap, wh)) {
112159aa14a9SRui Paulo 		IEEE80211_DISCARD(vap, IEEE80211_MSG_ACL,
112259aa14a9SRui Paulo 		    wh, NULL, "%s", "disallowed by ACL");
112359aa14a9SRui Paulo 		vap->iv_stats.is_rx_acl++;
112459aa14a9SRui Paulo 		goto out;
112559aa14a9SRui Paulo 	}
112659aa14a9SRui Paulo #endif
112759aa14a9SRui Paulo 	switch (type) {
112859aa14a9SRui Paulo 	case IEEE80211_FC0_TYPE_DATA:
112959aa14a9SRui Paulo 		if (ni == vap->iv_bss)
113059aa14a9SRui Paulo 			goto out;
113159aa14a9SRui Paulo 		if (ni->ni_mlstate != IEEE80211_NODE_MESH_ESTABLISHED) {
113259aa14a9SRui Paulo 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_MESH,
113359aa14a9SRui Paulo 			    ni->ni_macaddr, NULL,
113459aa14a9SRui Paulo 			    "peer link not yet established (%d)",
113559aa14a9SRui Paulo 			    ni->ni_mlstate);
113659aa14a9SRui Paulo 			vap->iv_stats.is_mesh_nolink++;
113759aa14a9SRui Paulo 			goto out;
113859aa14a9SRui Paulo 		}
113959aa14a9SRui Paulo 		if (dir != IEEE80211_FC1_DIR_FROMDS &&
114059aa14a9SRui Paulo 		    dir != IEEE80211_FC1_DIR_DSTODS) {
114159aa14a9SRui Paulo 			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
114259aa14a9SRui Paulo 			    wh, "data", "incorrect dir 0x%x", dir);
114359aa14a9SRui Paulo 			vap->iv_stats.is_rx_wrongdir++;
114459aa14a9SRui Paulo 			goto err;
114559aa14a9SRui Paulo 		}
114691216c71SAdrian Chadd 
114791216c71SAdrian Chadd 		/* All Mesh data frames are QoS subtype */
114891216c71SAdrian Chadd 		if (!HAS_SEQ(type)) {
114991216c71SAdrian Chadd 			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
115091216c71SAdrian Chadd 			    wh, "data", "incorrect subtype 0x%x", subtype);
115191216c71SAdrian Chadd 			vap->iv_stats.is_rx_badsubtype++;
115291216c71SAdrian Chadd 			goto err;
115391216c71SAdrian Chadd 		}
115491216c71SAdrian Chadd 
115591216c71SAdrian Chadd 		/*
115691216c71SAdrian Chadd 		 * Next up, any fragmentation.
115791216c71SAdrian Chadd 		 * XXX: we defrag before we even try to forward,
115891216c71SAdrian Chadd 		 * Mesh Control field is not present in sub-sequent
115991216c71SAdrian Chadd 		 * fragmented frames. This is in contrast to Draft 4.0.
116091216c71SAdrian Chadd 		 */
116159aa14a9SRui Paulo 		hdrspace = ieee80211_hdrspace(ic, wh);
116291216c71SAdrian Chadd 		if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
116391216c71SAdrian Chadd 			m = ieee80211_defrag(ni, m, hdrspace);
116491216c71SAdrian Chadd 			if (m == NULL) {
116591216c71SAdrian Chadd 				/* Fragment dropped or frame not complete yet */
116691216c71SAdrian Chadd 				goto out;
116791216c71SAdrian Chadd 			}
116891216c71SAdrian Chadd 		}
116991216c71SAdrian Chadd 		wh = mtod(m, struct ieee80211_frame *); /* NB: after defrag */
117091216c71SAdrian Chadd 
117191216c71SAdrian Chadd 		/*
117291216c71SAdrian Chadd 		 * Now we have a complete Mesh Data frame.
117391216c71SAdrian Chadd 		 */
117491216c71SAdrian Chadd 
117591216c71SAdrian Chadd 		/*
117691216c71SAdrian Chadd 		 * Only fromDStoDS data frames use 4 address qos frames
117791216c71SAdrian Chadd 		 * as specified in amendment. Otherwise addr4 is located
117891216c71SAdrian Chadd 		 * in the Mesh Control field and a 3 address qos frame
117991216c71SAdrian Chadd 		 * is used.
118091216c71SAdrian Chadd 		 */
118191216c71SAdrian Chadd 		if (IEEE80211_IS_DSTODS(wh))
118291216c71SAdrian Chadd 			*(uint16_t *)qos = *(uint16_t *)
118391216c71SAdrian Chadd 			    ((struct ieee80211_qosframe_addr4 *)wh)->i_qos;
118491216c71SAdrian Chadd 		else
118591216c71SAdrian Chadd 			*(uint16_t *)qos = *(uint16_t *)
118691216c71SAdrian Chadd 			    ((struct ieee80211_qosframe *)wh)->i_qos;
118791216c71SAdrian Chadd 
118891216c71SAdrian Chadd 		/*
118991216c71SAdrian Chadd 		 * NB: The mesh STA sets the Mesh Control Present
119091216c71SAdrian Chadd 		 * subfield to 1 in the Mesh Data frame containing
119191216c71SAdrian Chadd 		 * an unfragmented MSDU, an A-MSDU, or the first
119291216c71SAdrian Chadd 		 * fragment of an MSDU.
119391216c71SAdrian Chadd 		 * After defrag it should always be present.
119491216c71SAdrian Chadd 		 */
119591216c71SAdrian Chadd 		if (!(qos[1] & IEEE80211_QOS_MC)) {
119691216c71SAdrian Chadd 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_MESH,
119791216c71SAdrian Chadd 			    ni->ni_macaddr, NULL,
119891216c71SAdrian Chadd 			    "%s", "Mesh control field not present");
119991216c71SAdrian Chadd 			vap->iv_stats.is_rx_elem_missing++; /* XXX: kinda */
120091216c71SAdrian Chadd 			goto err;
120191216c71SAdrian Chadd 		}
120291216c71SAdrian Chadd 
120391216c71SAdrian Chadd 		/* pull up enough to get to the mesh control */
120459aa14a9SRui Paulo 		if (m->m_len < hdrspace + sizeof(struct ieee80211_meshcntl) &&
120559aa14a9SRui Paulo 		    (m = m_pullup(m, hdrspace +
120659aa14a9SRui Paulo 		        sizeof(struct ieee80211_meshcntl))) == NULL) {
120759aa14a9SRui Paulo 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
120859aa14a9SRui Paulo 			    ni->ni_macaddr, NULL,
120959aa14a9SRui Paulo 			    "data too short: expecting %u", hdrspace);
121059aa14a9SRui Paulo 			vap->iv_stats.is_rx_tooshort++;
121159aa14a9SRui Paulo 			goto out;		/* XXX */
121259aa14a9SRui Paulo 		}
121359aa14a9SRui Paulo 		/*
121459aa14a9SRui Paulo 		 * Now calculate the full extent of the headers. Note
1215c104cff2SRui Paulo 		 * mesh_decap will pull up anything we didn't get
121659aa14a9SRui Paulo 		 * above when it strips the 802.11 headers.
121759aa14a9SRui Paulo 		 */
121859aa14a9SRui Paulo 		mc = (const struct ieee80211_meshcntl *)
121959aa14a9SRui Paulo 		    (mtod(m, const uint8_t *) + hdrspace);
1220c104cff2SRui Paulo 		meshdrlen = sizeof(struct ieee80211_meshcntl) +
122159aa14a9SRui Paulo 		    (mc->mc_flags & 3) * IEEE80211_ADDR_LEN;
1222c104cff2SRui Paulo 		hdrspace += meshdrlen;
122359aa14a9SRui Paulo 		seq = LE_READ_4(mc->mc_seq);
122459aa14a9SRui Paulo 		if (IEEE80211_IS_MULTICAST(wh->i_addr1))
122559aa14a9SRui Paulo 			addr = wh->i_addr3;
122659aa14a9SRui Paulo 		else
122759aa14a9SRui Paulo 			addr = ((struct ieee80211_qosframe_addr4 *)wh)->i_addr4;
122859aa14a9SRui Paulo 		if (IEEE80211_ADDR_EQ(vap->iv_myaddr, addr)) {
122959aa14a9SRui Paulo 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
123059aa14a9SRui Paulo 			    addr, "data", "%s", "not to me");
123159aa14a9SRui Paulo 			vap->iv_stats.is_rx_wrongbss++;	/* XXX kinda */
123259aa14a9SRui Paulo 			goto out;
123359aa14a9SRui Paulo 		}
123459aa14a9SRui Paulo 		if (mesh_checkpseq(vap, addr, seq) != 0) {
123559aa14a9SRui Paulo 			vap->iv_stats.is_rx_dup++;
123659aa14a9SRui Paulo 			goto out;
123759aa14a9SRui Paulo 		}
123859aa14a9SRui Paulo 
123959aa14a9SRui Paulo 		/*
124059aa14a9SRui Paulo 		 * Potentially forward packet.  See table s36 (p140)
124159aa14a9SRui Paulo 		 * for the rules.  XXX tap fwd'd packets not for us?
124259aa14a9SRui Paulo 		 */
124359aa14a9SRui Paulo 		if (dir == IEEE80211_FC1_DIR_FROMDS ||
1244c104cff2SRui Paulo 		    !mesh_isucastforme(vap, wh, mc)) {
124559aa14a9SRui Paulo 			mesh_forward(vap, m, mc);
124659aa14a9SRui Paulo 			if (dir == IEEE80211_FC1_DIR_DSTODS)
124759aa14a9SRui Paulo 				goto out;
124859aa14a9SRui Paulo 			/* NB: fall thru to deliver mcast frames locally */
124959aa14a9SRui Paulo 		}
125059aa14a9SRui Paulo 
125159aa14a9SRui Paulo 		if (ieee80211_radiotap_active_vap(vap))
125259aa14a9SRui Paulo 			ieee80211_radiotap_rx(vap, m);
125359aa14a9SRui Paulo 		need_tap = 0;
125459aa14a9SRui Paulo 
125559aa14a9SRui Paulo 		/*
125659aa14a9SRui Paulo 		 * Finally, strip the 802.11 header.
125759aa14a9SRui Paulo 		 */
1258c104cff2SRui Paulo 		m = mesh_decap(vap, m, hdrspace, meshdrlen);
125959aa14a9SRui Paulo 		if (m == NULL) {
126059aa14a9SRui Paulo 			/* XXX mask bit to check for both */
126159aa14a9SRui Paulo 			/* don't count Null data frames as errors */
126259aa14a9SRui Paulo 			if (subtype == IEEE80211_FC0_SUBTYPE_NODATA ||
126359aa14a9SRui Paulo 			    subtype == IEEE80211_FC0_SUBTYPE_QOS_NULL)
126459aa14a9SRui Paulo 				goto out;
126559aa14a9SRui Paulo 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
126659aa14a9SRui Paulo 			    ni->ni_macaddr, "data", "%s", "decap error");
126759aa14a9SRui Paulo 			vap->iv_stats.is_rx_decap++;
126859aa14a9SRui Paulo 			IEEE80211_NODE_STAT(ni, rx_decap);
126959aa14a9SRui Paulo 			goto err;
127059aa14a9SRui Paulo 		}
127191216c71SAdrian Chadd 		if (qos[0] & IEEE80211_QOS_AMSDU) {
127259aa14a9SRui Paulo 			m = ieee80211_decap_amsdu(ni, m);
127359aa14a9SRui Paulo 			if (m == NULL)
127459aa14a9SRui Paulo 				return IEEE80211_FC0_TYPE_DATA;
127559aa14a9SRui Paulo 		}
127659aa14a9SRui Paulo 		ieee80211_deliver_data(vap, ni, m);
127759aa14a9SRui Paulo 		return type;
127859aa14a9SRui Paulo 	case IEEE80211_FC0_TYPE_MGT:
127959aa14a9SRui Paulo 		vap->iv_stats.is_rx_mgmt++;
128059aa14a9SRui Paulo 		IEEE80211_NODE_STAT(ni, rx_mgmt);
128159aa14a9SRui Paulo 		if (dir != IEEE80211_FC1_DIR_NODS) {
128259aa14a9SRui Paulo 			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
128359aa14a9SRui Paulo 			    wh, "mgt", "incorrect dir 0x%x", dir);
128459aa14a9SRui Paulo 			vap->iv_stats.is_rx_wrongdir++;
128559aa14a9SRui Paulo 			goto err;
128659aa14a9SRui Paulo 		}
128759aa14a9SRui Paulo 		if (m->m_pkthdr.len < sizeof(struct ieee80211_frame)) {
128859aa14a9SRui Paulo 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
128959aa14a9SRui Paulo 			    ni->ni_macaddr, "mgt", "too short: len %u",
129059aa14a9SRui Paulo 			    m->m_pkthdr.len);
129159aa14a9SRui Paulo 			vap->iv_stats.is_rx_tooshort++;
129259aa14a9SRui Paulo 			goto out;
129359aa14a9SRui Paulo 		}
129459aa14a9SRui Paulo #ifdef IEEE80211_DEBUG
129559aa14a9SRui Paulo 		if ((ieee80211_msg_debug(vap) &&
129659aa14a9SRui Paulo 		    (vap->iv_ic->ic_flags & IEEE80211_F_SCAN)) ||
129759aa14a9SRui Paulo 		    ieee80211_msg_dumppkts(vap)) {
129859aa14a9SRui Paulo 			if_printf(ifp, "received %s from %s rssi %d\n",
129959aa14a9SRui Paulo 			    ieee80211_mgt_subtype_name[subtype >>
130059aa14a9SRui Paulo 			    IEEE80211_FC0_SUBTYPE_SHIFT],
130159aa14a9SRui Paulo 			    ether_sprintf(wh->i_addr2), rssi);
130259aa14a9SRui Paulo 		}
130359aa14a9SRui Paulo #endif
130459aa14a9SRui Paulo 		if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
130559aa14a9SRui Paulo 			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
130659aa14a9SRui Paulo 			    wh, NULL, "%s", "WEP set but not permitted");
130759aa14a9SRui Paulo 			vap->iv_stats.is_rx_mgtdiscard++; /* XXX */
130859aa14a9SRui Paulo 			goto out;
130959aa14a9SRui Paulo 		}
131059aa14a9SRui Paulo 		vap->iv_recv_mgmt(ni, m, subtype, rssi, nf);
131159aa14a9SRui Paulo 		goto out;
131259aa14a9SRui Paulo 	case IEEE80211_FC0_TYPE_CTL:
131359aa14a9SRui Paulo 		vap->iv_stats.is_rx_ctl++;
131459aa14a9SRui Paulo 		IEEE80211_NODE_STAT(ni, rx_ctrl);
131559aa14a9SRui Paulo 		goto out;
131659aa14a9SRui Paulo 	default:
131759aa14a9SRui Paulo 		IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
131859aa14a9SRui Paulo 		    wh, "bad", "frame type 0x%x", type);
131959aa14a9SRui Paulo 		/* should not come here */
132059aa14a9SRui Paulo 		break;
132159aa14a9SRui Paulo 	}
132259aa14a9SRui Paulo err:
132359aa14a9SRui Paulo 	ifp->if_ierrors++;
132459aa14a9SRui Paulo out:
132559aa14a9SRui Paulo 	if (m != NULL) {
132659aa14a9SRui Paulo 		if (need_tap && ieee80211_radiotap_active_vap(vap))
132759aa14a9SRui Paulo 			ieee80211_radiotap_rx(vap, m);
132859aa14a9SRui Paulo 		m_freem(m);
132959aa14a9SRui Paulo 	}
133059aa14a9SRui Paulo 	return type;
133159aa14a9SRui Paulo }
133259aa14a9SRui Paulo 
133359aa14a9SRui Paulo static void
133459aa14a9SRui Paulo mesh_recv_mgmt(struct ieee80211_node *ni, struct mbuf *m0, int subtype,
133559aa14a9SRui Paulo     int rssi, int nf)
133659aa14a9SRui Paulo {
133759aa14a9SRui Paulo 	struct ieee80211vap *vap = ni->ni_vap;
133859aa14a9SRui Paulo 	struct ieee80211_mesh_state *ms = vap->iv_mesh;
133959aa14a9SRui Paulo 	struct ieee80211com *ic = ni->ni_ic;
134059aa14a9SRui Paulo 	struct ieee80211_frame *wh;
134159aa14a9SRui Paulo 	uint8_t *frm, *efrm;
134259aa14a9SRui Paulo 
134359aa14a9SRui Paulo 	wh = mtod(m0, struct ieee80211_frame *);
134459aa14a9SRui Paulo 	frm = (uint8_t *)&wh[1];
134559aa14a9SRui Paulo 	efrm = mtod(m0, uint8_t *) + m0->m_len;
134659aa14a9SRui Paulo 	switch (subtype) {
134759aa14a9SRui Paulo 	case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
134859aa14a9SRui Paulo 	case IEEE80211_FC0_SUBTYPE_BEACON:
134959aa14a9SRui Paulo 	{
135059aa14a9SRui Paulo 		struct ieee80211_scanparams scan;
135159aa14a9SRui Paulo 		/*
135259aa14a9SRui Paulo 		 * We process beacon/probe response
135359aa14a9SRui Paulo 		 * frames to discover neighbors.
135459aa14a9SRui Paulo 		 */
135559aa14a9SRui Paulo 		if (ieee80211_parse_beacon(ni, m0, &scan) != 0)
135659aa14a9SRui Paulo 			return;
135759aa14a9SRui Paulo 		/*
135859aa14a9SRui Paulo 		 * Count frame now that we know it's to be processed.
135959aa14a9SRui Paulo 		 */
136059aa14a9SRui Paulo 		if (subtype == IEEE80211_FC0_SUBTYPE_BEACON) {
136159aa14a9SRui Paulo 			vap->iv_stats.is_rx_beacon++;	/* XXX remove */
136259aa14a9SRui Paulo 			IEEE80211_NODE_STAT(ni, rx_beacons);
136359aa14a9SRui Paulo 		} else
136459aa14a9SRui Paulo 			IEEE80211_NODE_STAT(ni, rx_proberesp);
136559aa14a9SRui Paulo 		/*
136659aa14a9SRui Paulo 		 * If scanning, just pass information to the scan module.
136759aa14a9SRui Paulo 		 */
136859aa14a9SRui Paulo 		if (ic->ic_flags & IEEE80211_F_SCAN) {
136959aa14a9SRui Paulo 			if (ic->ic_flags_ext & IEEE80211_FEXT_PROBECHAN) {
137059aa14a9SRui Paulo 				/*
137159aa14a9SRui Paulo 				 * Actively scanning a channel marked passive;
137259aa14a9SRui Paulo 				 * send a probe request now that we know there
137359aa14a9SRui Paulo 				 * is 802.11 traffic present.
137459aa14a9SRui Paulo 				 *
137559aa14a9SRui Paulo 				 * XXX check if the beacon we recv'd gives
137659aa14a9SRui Paulo 				 * us what we need and suppress the probe req
137759aa14a9SRui Paulo 				 */
137859aa14a9SRui Paulo 				ieee80211_probe_curchan(vap, 1);
137959aa14a9SRui Paulo 				ic->ic_flags_ext &= ~IEEE80211_FEXT_PROBECHAN;
138059aa14a9SRui Paulo 			}
138159aa14a9SRui Paulo 			ieee80211_add_scan(vap, &scan, wh,
138259aa14a9SRui Paulo 			    subtype, rssi, nf);
138359aa14a9SRui Paulo 			return;
138459aa14a9SRui Paulo 		}
138559aa14a9SRui Paulo 
138659aa14a9SRui Paulo 		/* The rest of this code assumes we are running */
138759aa14a9SRui Paulo 		if (vap->iv_state != IEEE80211_S_RUN)
138859aa14a9SRui Paulo 			return;
138959aa14a9SRui Paulo 		/*
139059aa14a9SRui Paulo 		 * Ignore non-mesh STAs.
139159aa14a9SRui Paulo 		 */
139259aa14a9SRui Paulo 		if ((scan.capinfo &
139359aa14a9SRui Paulo 		     (IEEE80211_CAPINFO_ESS|IEEE80211_CAPINFO_IBSS)) ||
139459aa14a9SRui Paulo 		    scan.meshid == NULL || scan.meshconf == NULL) {
139559aa14a9SRui Paulo 			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
139659aa14a9SRui Paulo 			    wh, "beacon", "%s", "not a mesh sta");
139759aa14a9SRui Paulo 			vap->iv_stats.is_mesh_wrongmesh++;
139859aa14a9SRui Paulo 			return;
139959aa14a9SRui Paulo 		}
140059aa14a9SRui Paulo 		/*
140159aa14a9SRui Paulo 		 * Ignore STAs for other mesh networks.
140259aa14a9SRui Paulo 		 */
140359aa14a9SRui Paulo 		if (memcmp(scan.meshid+2, ms->ms_id, ms->ms_idlen) != 0 ||
140459aa14a9SRui Paulo 		    mesh_verify_meshconf(vap, scan.meshconf)) {
140559aa14a9SRui Paulo 			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
140659aa14a9SRui Paulo 			    wh, "beacon", "%s", "not for our mesh");
140759aa14a9SRui Paulo 			vap->iv_stats.is_mesh_wrongmesh++;
140859aa14a9SRui Paulo 			return;
140959aa14a9SRui Paulo 		}
141059aa14a9SRui Paulo 		/*
141159aa14a9SRui Paulo 		 * Peer only based on the current ACL policy.
141259aa14a9SRui Paulo 		 */
14135a8801b0SBernhard Schmidt 		if (vap->iv_acl != NULL && !vap->iv_acl->iac_check(vap, wh)) {
141459aa14a9SRui Paulo 			IEEE80211_DISCARD(vap, IEEE80211_MSG_ACL,
141559aa14a9SRui Paulo 			    wh, NULL, "%s", "disallowed by ACL");
141659aa14a9SRui Paulo 			vap->iv_stats.is_rx_acl++;
141759aa14a9SRui Paulo 			return;
141859aa14a9SRui Paulo 		}
141959aa14a9SRui Paulo 		/*
142059aa14a9SRui Paulo 		 * Do neighbor discovery.
142159aa14a9SRui Paulo 		 */
142259aa14a9SRui Paulo 		if (!IEEE80211_ADDR_EQ(wh->i_addr2, ni->ni_macaddr)) {
142359aa14a9SRui Paulo 			/*
142459aa14a9SRui Paulo 			 * Create a new entry in the neighbor table.
142559aa14a9SRui Paulo 			 */
142659aa14a9SRui Paulo 			ni = ieee80211_add_neighbor(vap, wh, &scan);
142759aa14a9SRui Paulo 		}
142859aa14a9SRui Paulo 		/*
142959aa14a9SRui Paulo 		 * Automatically peer with discovered nodes if possible.
143059aa14a9SRui Paulo 		 * XXX backoff on repeated failure
143159aa14a9SRui Paulo 		 */
143259aa14a9SRui Paulo 		if (ni != vap->iv_bss &&
143359aa14a9SRui Paulo 		    (ms->ms_flags & IEEE80211_MESHFLAGS_AP) &&
143459aa14a9SRui Paulo 		    ni->ni_mlstate == IEEE80211_NODE_MESH_IDLE) {
143559aa14a9SRui Paulo 			uint16_t args[1];
143659aa14a9SRui Paulo 
143759aa14a9SRui Paulo 			ni->ni_mlpid = mesh_generateid(vap);
143859aa14a9SRui Paulo 			if (ni->ni_mlpid == 0)
143959aa14a9SRui Paulo 				return;
144059aa14a9SRui Paulo 			mesh_linkchange(ni, IEEE80211_NODE_MESH_OPENSNT);
144159aa14a9SRui Paulo 			args[0] = ni->ni_mlpid;
144259aa14a9SRui Paulo 			ieee80211_send_action(ni,
1443ebeaa1adSMonthadar Al Jaberi 			    IEEE80211_ACTION_CAT_SELF_PROT,
144459aa14a9SRui Paulo 			    IEEE80211_ACTION_MESHPEERING_OPEN, args);
144559aa14a9SRui Paulo 			ni->ni_mlrcnt = 0;
144659aa14a9SRui Paulo 			mesh_peer_timeout_setup(ni);
144759aa14a9SRui Paulo 		}
144859aa14a9SRui Paulo 		break;
144959aa14a9SRui Paulo 	}
145059aa14a9SRui Paulo 	case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
145159aa14a9SRui Paulo 	{
145259aa14a9SRui Paulo 		uint8_t *ssid, *meshid, *rates, *xrates;
145359aa14a9SRui Paulo 		uint8_t *sfrm;
145459aa14a9SRui Paulo 
145559aa14a9SRui Paulo 		if (vap->iv_state != IEEE80211_S_RUN) {
145659aa14a9SRui Paulo 			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
145759aa14a9SRui Paulo 			    wh, NULL, "wrong state %s",
145859aa14a9SRui Paulo 			    ieee80211_state_name[vap->iv_state]);
145959aa14a9SRui Paulo 			vap->iv_stats.is_rx_mgtdiscard++;
146059aa14a9SRui Paulo 			return;
146159aa14a9SRui Paulo 		}
146259aa14a9SRui Paulo 		if (IEEE80211_IS_MULTICAST(wh->i_addr2)) {
146359aa14a9SRui Paulo 			/* frame must be directed */
146459aa14a9SRui Paulo 			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
146559aa14a9SRui Paulo 			    wh, NULL, "%s", "not unicast");
146659aa14a9SRui Paulo 			vap->iv_stats.is_rx_mgtdiscard++;	/* XXX stat */
146759aa14a9SRui Paulo 			return;
146859aa14a9SRui Paulo 		}
146959aa14a9SRui Paulo 		/*
147059aa14a9SRui Paulo 		 * prreq frame format
147159aa14a9SRui Paulo 		 *      [tlv] ssid
147259aa14a9SRui Paulo 		 *      [tlv] supported rates
147359aa14a9SRui Paulo 		 *      [tlv] extended supported rates
147459aa14a9SRui Paulo 		 *	[tlv] mesh id
147559aa14a9SRui Paulo 		 */
147659aa14a9SRui Paulo 		ssid = meshid = rates = xrates = NULL;
147759aa14a9SRui Paulo 		sfrm = frm;
147859aa14a9SRui Paulo 		while (efrm - frm > 1) {
147959aa14a9SRui Paulo 			IEEE80211_VERIFY_LENGTH(efrm - frm, frm[1] + 2, return);
148059aa14a9SRui Paulo 			switch (*frm) {
148159aa14a9SRui Paulo 			case IEEE80211_ELEMID_SSID:
148259aa14a9SRui Paulo 				ssid = frm;
148359aa14a9SRui Paulo 				break;
148459aa14a9SRui Paulo 			case IEEE80211_ELEMID_RATES:
148559aa14a9SRui Paulo 				rates = frm;
148659aa14a9SRui Paulo 				break;
148759aa14a9SRui Paulo 			case IEEE80211_ELEMID_XRATES:
148859aa14a9SRui Paulo 				xrates = frm;
148959aa14a9SRui Paulo 				break;
149059aa14a9SRui Paulo 			case IEEE80211_ELEMID_MESHID:
149159aa14a9SRui Paulo 				meshid = frm;
149259aa14a9SRui Paulo 				break;
149359aa14a9SRui Paulo 			}
1494d3befdecSBernhard Schmidt 			frm += frm[1] + 2;
149559aa14a9SRui Paulo 		}
149659aa14a9SRui Paulo 		IEEE80211_VERIFY_ELEMENT(ssid, IEEE80211_NWID_LEN, return);
149759aa14a9SRui Paulo 		IEEE80211_VERIFY_ELEMENT(rates, IEEE80211_RATE_MAXSIZE, return);
149859aa14a9SRui Paulo 		if (xrates != NULL)
149959aa14a9SRui Paulo 			IEEE80211_VERIFY_ELEMENT(xrates,
150059aa14a9SRui Paulo 			    IEEE80211_RATE_MAXSIZE - rates[1], return);
15016c946257SRui Paulo 		if (meshid != NULL) {
150259aa14a9SRui Paulo 			IEEE80211_VERIFY_ELEMENT(meshid,
150359aa14a9SRui Paulo 			    IEEE80211_MESHID_LEN, return);
150459aa14a9SRui Paulo 			/* NB: meshid, not ssid */
150559aa14a9SRui Paulo 			IEEE80211_VERIFY_SSID(vap->iv_bss, meshid, return);
15066c946257SRui Paulo 		}
150759aa14a9SRui Paulo 
150859aa14a9SRui Paulo 		/* XXX find a better class or define it's own */
150959aa14a9SRui Paulo 		IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_INPUT, wh->i_addr2,
151059aa14a9SRui Paulo 		    "%s", "recv probe req");
151159aa14a9SRui Paulo 		/*
151259aa14a9SRui Paulo 		 * Some legacy 11b clients cannot hack a complete
151359aa14a9SRui Paulo 		 * probe response frame.  When the request includes
151459aa14a9SRui Paulo 		 * only a bare-bones rate set, communicate this to
151559aa14a9SRui Paulo 		 * the transmit side.
151659aa14a9SRui Paulo 		 */
151759aa14a9SRui Paulo 		ieee80211_send_proberesp(vap, wh->i_addr2, 0);
151859aa14a9SRui Paulo 		break;
151959aa14a9SRui Paulo 	}
152096283082SBernhard Schmidt 
152159aa14a9SRui Paulo 	case IEEE80211_FC0_SUBTYPE_ACTION:
152296283082SBernhard Schmidt 	case IEEE80211_FC0_SUBTYPE_ACTION_NOACK:
152359aa14a9SRui Paulo 		if (ni == vap->iv_bss) {
1524893c4d6eSBernhard Schmidt 			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
152559aa14a9SRui Paulo 			    wh, NULL, "%s", "unknown node");
152659aa14a9SRui Paulo 			vap->iv_stats.is_rx_mgtdiscard++;
1527893c4d6eSBernhard Schmidt 		} else if (!IEEE80211_ADDR_EQ(vap->iv_myaddr, wh->i_addr1) &&
152859aa14a9SRui Paulo 		    !IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1529893c4d6eSBernhard Schmidt 			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
1530893c4d6eSBernhard Schmidt 			    wh, NULL, "%s", "not for us");
153159aa14a9SRui Paulo 			vap->iv_stats.is_rx_mgtdiscard++;
1532893c4d6eSBernhard Schmidt 		} else if (vap->iv_state != IEEE80211_S_RUN) {
153396283082SBernhard Schmidt 			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
153496283082SBernhard Schmidt 			    wh, NULL, "wrong state %s",
153596283082SBernhard Schmidt 			    ieee80211_state_name[vap->iv_state]);
153696283082SBernhard Schmidt 			vap->iv_stats.is_rx_mgtdiscard++;
1537893c4d6eSBernhard Schmidt 		} else {
1538893c4d6eSBernhard Schmidt 			if (ieee80211_parse_action(ni, m0) == 0)
1539893c4d6eSBernhard Schmidt 				(void)ic->ic_recv_action(ni, wh, frm, efrm);
154096283082SBernhard Schmidt 		}
154159aa14a9SRui Paulo 		break;
154296283082SBernhard Schmidt 
154359aa14a9SRui Paulo 	case IEEE80211_FC0_SUBTYPE_ASSOC_REQ:
154459aa14a9SRui Paulo 	case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
154596283082SBernhard Schmidt 	case IEEE80211_FC0_SUBTYPE_REASSOC_REQ:
154659aa14a9SRui Paulo 	case IEEE80211_FC0_SUBTYPE_REASSOC_RESP:
154796283082SBernhard Schmidt 	case IEEE80211_FC0_SUBTYPE_ATIM:
154859aa14a9SRui Paulo 	case IEEE80211_FC0_SUBTYPE_DISASSOC:
154996283082SBernhard Schmidt 	case IEEE80211_FC0_SUBTYPE_AUTH:
155096283082SBernhard Schmidt 	case IEEE80211_FC0_SUBTYPE_DEAUTH:
155159aa14a9SRui Paulo 		IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
155259aa14a9SRui Paulo 		    wh, NULL, "%s", "not handled");
155359aa14a9SRui Paulo 		vap->iv_stats.is_rx_mgtdiscard++;
155496283082SBernhard Schmidt 		break;
155596283082SBernhard Schmidt 
155659aa14a9SRui Paulo 	default:
155759aa14a9SRui Paulo 		IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
155859aa14a9SRui Paulo 		    wh, "mgt", "subtype 0x%x not handled", subtype);
155959aa14a9SRui Paulo 		vap->iv_stats.is_rx_badsubtype++;
156059aa14a9SRui Paulo 		break;
156159aa14a9SRui Paulo 	}
156259aa14a9SRui Paulo }
156359aa14a9SRui Paulo 
15640917631fSRui Paulo static void
15650917631fSRui Paulo mesh_recv_ctl(struct ieee80211_node *ni, struct mbuf *m, int subtype)
15660917631fSRui Paulo {
15670917631fSRui Paulo 
15680917631fSRui Paulo 	switch (subtype) {
15690917631fSRui Paulo 	case IEEE80211_FC0_SUBTYPE_BAR:
15700917631fSRui Paulo 		ieee80211_recv_bar(ni, m);
15710917631fSRui Paulo 		break;
15720917631fSRui Paulo 	}
15730917631fSRui Paulo }
15740917631fSRui Paulo 
157559aa14a9SRui Paulo /*
1576*c2042c35SMonthadar Al Jaberi  * Parse meshpeering action ie's for open+confirm frames
157759aa14a9SRui Paulo  */
157859aa14a9SRui Paulo static const struct ieee80211_meshpeer_ie *
157959aa14a9SRui Paulo mesh_parse_meshpeering_action(struct ieee80211_node *ni,
158059aa14a9SRui Paulo 	const struct ieee80211_frame *wh,	/* XXX for VERIFY_LENGTH */
158159aa14a9SRui Paulo 	const uint8_t *frm, const uint8_t *efrm,
1582c77735e2SRui Paulo 	struct ieee80211_meshpeer_ie *mp, uint8_t subtype)
158359aa14a9SRui Paulo {
158459aa14a9SRui Paulo 	struct ieee80211vap *vap = ni->ni_vap;
158559aa14a9SRui Paulo 	const struct ieee80211_meshpeer_ie *mpie;
1586c77735e2SRui Paulo 	const uint8_t *meshid, *meshconf, *meshpeer;
158759aa14a9SRui Paulo 
1588c77735e2SRui Paulo 	meshid = meshconf = meshpeer = NULL;
158959aa14a9SRui Paulo 	while (efrm - frm > 1) {
159059aa14a9SRui Paulo 		IEEE80211_VERIFY_LENGTH(efrm - frm, frm[1] + 2, return NULL);
159159aa14a9SRui Paulo 		switch (*frm) {
159259aa14a9SRui Paulo 		case IEEE80211_ELEMID_MESHID:
159359aa14a9SRui Paulo 			meshid = frm;
159459aa14a9SRui Paulo 			break;
159559aa14a9SRui Paulo 		case IEEE80211_ELEMID_MESHCONF:
159659aa14a9SRui Paulo 			meshconf = frm;
159759aa14a9SRui Paulo 			break;
159859aa14a9SRui Paulo 		case IEEE80211_ELEMID_MESHPEER:
159959aa14a9SRui Paulo 			meshpeer = frm;
160059aa14a9SRui Paulo 			mpie = (const struct ieee80211_meshpeer_ie *) frm;
160159aa14a9SRui Paulo 			memset(mp, 0, sizeof(*mp));
1602*c2042c35SMonthadar Al Jaberi 			mp->peer_proto = LE_READ_2(&mpie->peer_proto);
160359aa14a9SRui Paulo 			mp->peer_llinkid = LE_READ_2(&mpie->peer_llinkid);
1604*c2042c35SMonthadar Al Jaberi 			switch (subtype) {
1605*c2042c35SMonthadar Al Jaberi 			case IEEE80211_ACTION_MESHPEERING_CONFIRM:
1606*c2042c35SMonthadar Al Jaberi 				mp->peer_linkid =
1607*c2042c35SMonthadar Al Jaberi 				    LE_READ_2(&mpie->peer_linkid);
1608*c2042c35SMonthadar Al Jaberi 				break;
1609*c2042c35SMonthadar Al Jaberi 			case IEEE80211_ACTION_MESHPEERING_CLOSE:
1610*c2042c35SMonthadar Al Jaberi 				/* NB: peer link ID is optional */
1611*c2042c35SMonthadar Al Jaberi 				if (mpie->peer_len ==
1612*c2042c35SMonthadar Al Jaberi 				    (IEEE80211_MPM_BASE_SZ + 2)) {
161359aa14a9SRui Paulo 					mp->peer_linkid = 0;
1614*c2042c35SMonthadar Al Jaberi 					mp->peer_rcode =
1615*c2042c35SMonthadar Al Jaberi 					    LE_READ_2(&mpie->peer_linkid);
161659aa14a9SRui Paulo 				} else {
1617*c2042c35SMonthadar Al Jaberi 					mp->peer_linkid =
1618*c2042c35SMonthadar Al Jaberi 					    LE_READ_2(&mpie->peer_linkid);
1619*c2042c35SMonthadar Al Jaberi 					mp->peer_rcode =
1620*c2042c35SMonthadar Al Jaberi 					    LE_READ_2(&mpie->peer_rcode);
1621*c2042c35SMonthadar Al Jaberi 				}
1622*c2042c35SMonthadar Al Jaberi 				break;
162359aa14a9SRui Paulo 			}
162459aa14a9SRui Paulo 			break;
162559aa14a9SRui Paulo 		}
162659aa14a9SRui Paulo 		frm += frm[1] + 2;
162759aa14a9SRui Paulo 	}
162859aa14a9SRui Paulo 
162959aa14a9SRui Paulo 	/*
163059aa14a9SRui Paulo 	 * Verify the contents of the frame. Action frames with
163159aa14a9SRui Paulo 	 * close subtype don't have a Mesh Configuration IE.
163259aa14a9SRui Paulo 	 * If if fails validation, close the peer link.
163359aa14a9SRui Paulo 	 */
1634c77735e2SRui Paulo 	KASSERT(meshpeer != NULL &&
1635c77735e2SRui Paulo 	    subtype != IEEE80211_ACTION_MESHPEERING_CLOSE,
1636c77735e2SRui Paulo 	    ("parsing close action"));
163759aa14a9SRui Paulo 
1638c77735e2SRui Paulo 	if (mesh_verify_meshid(vap, meshid) ||
1639c77735e2SRui Paulo 	    mesh_verify_meshpeer(vap, subtype, meshpeer) ||
164059aa14a9SRui Paulo 	    mesh_verify_meshconf(vap, meshconf)) {
164159aa14a9SRui Paulo 		uint16_t args[3];
164259aa14a9SRui Paulo 
164359aa14a9SRui Paulo 		IEEE80211_DISCARD(vap,
164459aa14a9SRui Paulo 		    IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH,
164559aa14a9SRui Paulo 		    wh, NULL, "%s", "not for our mesh");
164659aa14a9SRui Paulo 		vap->iv_stats.is_rx_mgtdiscard++;
164759aa14a9SRui Paulo 		switch (ni->ni_mlstate) {
164859aa14a9SRui Paulo 		case IEEE80211_NODE_MESH_IDLE:
164959aa14a9SRui Paulo 		case IEEE80211_NODE_MESH_ESTABLISHED:
165059aa14a9SRui Paulo 		case IEEE80211_NODE_MESH_HOLDING:
165159aa14a9SRui Paulo 			/* ignore */
165259aa14a9SRui Paulo 			break;
165359aa14a9SRui Paulo 		case IEEE80211_NODE_MESH_OPENSNT:
165459aa14a9SRui Paulo 		case IEEE80211_NODE_MESH_OPENRCV:
165559aa14a9SRui Paulo 		case IEEE80211_NODE_MESH_CONFIRMRCV:
165659aa14a9SRui Paulo 			args[0] = ni->ni_mlpid;
165759aa14a9SRui Paulo 			args[1] = ni->ni_mllid;
165859aa14a9SRui Paulo 			args[2] = IEEE80211_REASON_PEER_LINK_CANCELED;
165959aa14a9SRui Paulo 			ieee80211_send_action(ni,
1660ebeaa1adSMonthadar Al Jaberi 			    IEEE80211_ACTION_CAT_SELF_PROT,
166159aa14a9SRui Paulo 			    IEEE80211_ACTION_MESHPEERING_CLOSE,
166259aa14a9SRui Paulo 			    args);
166359aa14a9SRui Paulo 			mesh_linkchange(ni, IEEE80211_NODE_MESH_HOLDING);
166459aa14a9SRui Paulo 			mesh_peer_timeout_setup(ni);
166559aa14a9SRui Paulo 			break;
166659aa14a9SRui Paulo 		}
166759aa14a9SRui Paulo 		return NULL;
166859aa14a9SRui Paulo 	}
166959aa14a9SRui Paulo 	return (const struct ieee80211_meshpeer_ie *) mp;
167059aa14a9SRui Paulo }
167159aa14a9SRui Paulo 
167259aa14a9SRui Paulo static int
167359aa14a9SRui Paulo mesh_recv_action_meshpeering_open(struct ieee80211_node *ni,
167459aa14a9SRui Paulo 	const struct ieee80211_frame *wh,
167559aa14a9SRui Paulo 	const uint8_t *frm, const uint8_t *efrm)
167659aa14a9SRui Paulo {
167759aa14a9SRui Paulo 	struct ieee80211vap *vap = ni->ni_vap;
167859aa14a9SRui Paulo 	struct ieee80211_meshpeer_ie ie;
167959aa14a9SRui Paulo 	const struct ieee80211_meshpeer_ie *meshpeer;
168059aa14a9SRui Paulo 	uint16_t args[3];
168159aa14a9SRui Paulo 
168259aa14a9SRui Paulo 	/* +2+2 for action + code + capabilites */
1683c77735e2SRui Paulo 	meshpeer = mesh_parse_meshpeering_action(ni, wh, frm+2+2, efrm, &ie,
1684c77735e2SRui Paulo 	    IEEE80211_ACTION_MESHPEERING_OPEN);
168559aa14a9SRui Paulo 	if (meshpeer == NULL) {
168659aa14a9SRui Paulo 		return 0;
168759aa14a9SRui Paulo 	}
168859aa14a9SRui Paulo 
168959aa14a9SRui Paulo 	/* XXX move up */
169059aa14a9SRui Paulo 	IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH, ni,
169159aa14a9SRui Paulo 	    "recv PEER OPEN, lid 0x%x", meshpeer->peer_llinkid);
169259aa14a9SRui Paulo 
169359aa14a9SRui Paulo 	switch (ni->ni_mlstate) {
169459aa14a9SRui Paulo 	case IEEE80211_NODE_MESH_IDLE:
169559aa14a9SRui Paulo 		mesh_linkchange(ni, IEEE80211_NODE_MESH_OPENRCV);
169659aa14a9SRui Paulo 		ni->ni_mllid = meshpeer->peer_llinkid;
169759aa14a9SRui Paulo 		ni->ni_mlpid = mesh_generateid(vap);
169859aa14a9SRui Paulo 		if (ni->ni_mlpid == 0)
169959aa14a9SRui Paulo 			return 0;		/* XXX */
170059aa14a9SRui Paulo 		args[0] = ni->ni_mlpid;
170159aa14a9SRui Paulo 		/* Announce we're open too... */
170259aa14a9SRui Paulo 		ieee80211_send_action(ni,
1703ebeaa1adSMonthadar Al Jaberi 		    IEEE80211_ACTION_CAT_SELF_PROT,
170459aa14a9SRui Paulo 		    IEEE80211_ACTION_MESHPEERING_OPEN, args);
170559aa14a9SRui Paulo 		/* ...and confirm the link. */
170659aa14a9SRui Paulo 		args[0] = ni->ni_mlpid;
170759aa14a9SRui Paulo 		args[1] = ni->ni_mllid;
170859aa14a9SRui Paulo 		ieee80211_send_action(ni,
1709ebeaa1adSMonthadar Al Jaberi 		    IEEE80211_ACTION_CAT_SELF_PROT,
171059aa14a9SRui Paulo 		    IEEE80211_ACTION_MESHPEERING_CONFIRM,
171159aa14a9SRui Paulo 		    args);
171259aa14a9SRui Paulo 		mesh_peer_timeout_setup(ni);
171359aa14a9SRui Paulo 		break;
171459aa14a9SRui Paulo 	case IEEE80211_NODE_MESH_OPENRCV:
171559aa14a9SRui Paulo 		/* Wrong Link ID */
171659aa14a9SRui Paulo 		if (ni->ni_mllid != meshpeer->peer_llinkid) {
171759aa14a9SRui Paulo 			args[0] = ni->ni_mllid;
171859aa14a9SRui Paulo 			args[1] = ni->ni_mlpid;
171959aa14a9SRui Paulo 			args[2] = IEEE80211_REASON_PEER_LINK_CANCELED;
172059aa14a9SRui Paulo 			ieee80211_send_action(ni,
1721ebeaa1adSMonthadar Al Jaberi 			    IEEE80211_ACTION_CAT_SELF_PROT,
172259aa14a9SRui Paulo 			    IEEE80211_ACTION_MESHPEERING_CLOSE,
172359aa14a9SRui Paulo 			    args);
172459aa14a9SRui Paulo 			mesh_linkchange(ni, IEEE80211_NODE_MESH_HOLDING);
172559aa14a9SRui Paulo 			mesh_peer_timeout_setup(ni);
172659aa14a9SRui Paulo 			break;
172759aa14a9SRui Paulo 		}
172859aa14a9SRui Paulo 		/* Duplicate open, confirm again. */
172959aa14a9SRui Paulo 		args[0] = ni->ni_mlpid;
173059aa14a9SRui Paulo 		args[1] = ni->ni_mllid;
173159aa14a9SRui Paulo 		ieee80211_send_action(ni,
1732ebeaa1adSMonthadar Al Jaberi 		    IEEE80211_ACTION_CAT_SELF_PROT,
173359aa14a9SRui Paulo 		    IEEE80211_ACTION_MESHPEERING_CONFIRM,
173459aa14a9SRui Paulo 		    args);
173559aa14a9SRui Paulo 		break;
173659aa14a9SRui Paulo 	case IEEE80211_NODE_MESH_OPENSNT:
173759aa14a9SRui Paulo 		ni->ni_mllid = meshpeer->peer_llinkid;
173859aa14a9SRui Paulo 		mesh_linkchange(ni, IEEE80211_NODE_MESH_OPENRCV);
173959aa14a9SRui Paulo 		args[0] = ni->ni_mlpid;
174059aa14a9SRui Paulo 		args[1] = ni->ni_mllid;
174159aa14a9SRui Paulo 		ieee80211_send_action(ni,
1742ebeaa1adSMonthadar Al Jaberi 		    IEEE80211_ACTION_CAT_SELF_PROT,
174359aa14a9SRui Paulo 		    IEEE80211_ACTION_MESHPEERING_CONFIRM,
174459aa14a9SRui Paulo 		    args);
174559aa14a9SRui Paulo 		/* NB: don't setup/clear any timeout */
174659aa14a9SRui Paulo 		break;
174759aa14a9SRui Paulo 	case IEEE80211_NODE_MESH_CONFIRMRCV:
174859aa14a9SRui Paulo 		if (ni->ni_mlpid != meshpeer->peer_linkid ||
174959aa14a9SRui Paulo 		    ni->ni_mllid != meshpeer->peer_llinkid) {
175059aa14a9SRui Paulo 			args[0] = ni->ni_mlpid;
175159aa14a9SRui Paulo 			args[1] = ni->ni_mllid;
175259aa14a9SRui Paulo 			args[2] = IEEE80211_REASON_PEER_LINK_CANCELED;
175359aa14a9SRui Paulo 			ieee80211_send_action(ni,
1754ebeaa1adSMonthadar Al Jaberi 			    IEEE80211_ACTION_CAT_SELF_PROT,
175559aa14a9SRui Paulo 			    IEEE80211_ACTION_MESHPEERING_CLOSE,
175659aa14a9SRui Paulo 			    args);
175759aa14a9SRui Paulo 			mesh_linkchange(ni,
175859aa14a9SRui Paulo 			    IEEE80211_NODE_MESH_HOLDING);
175959aa14a9SRui Paulo 			mesh_peer_timeout_setup(ni);
176059aa14a9SRui Paulo 			break;
176159aa14a9SRui Paulo 		}
176259aa14a9SRui Paulo 		mesh_linkchange(ni, IEEE80211_NODE_MESH_ESTABLISHED);
176359aa14a9SRui Paulo 		ni->ni_mllid = meshpeer->peer_llinkid;
176459aa14a9SRui Paulo 		args[0] = ni->ni_mlpid;
176559aa14a9SRui Paulo 		args[1] = ni->ni_mllid;
176659aa14a9SRui Paulo 		ieee80211_send_action(ni,
1767ebeaa1adSMonthadar Al Jaberi 		    IEEE80211_ACTION_CAT_SELF_PROT,
176859aa14a9SRui Paulo 		    IEEE80211_ACTION_MESHPEERING_CONFIRM,
176959aa14a9SRui Paulo 		    args);
177059aa14a9SRui Paulo 		mesh_peer_timeout_stop(ni);
177159aa14a9SRui Paulo 		break;
177259aa14a9SRui Paulo 	case IEEE80211_NODE_MESH_ESTABLISHED:
177359aa14a9SRui Paulo 		if (ni->ni_mllid != meshpeer->peer_llinkid) {
177459aa14a9SRui Paulo 			args[0] = ni->ni_mllid;
177559aa14a9SRui Paulo 			args[1] = ni->ni_mlpid;
177659aa14a9SRui Paulo 			args[2] = IEEE80211_REASON_PEER_LINK_CANCELED;
177759aa14a9SRui Paulo 			ieee80211_send_action(ni,
1778ebeaa1adSMonthadar Al Jaberi 			    IEEE80211_ACTION_CAT_SELF_PROT,
177959aa14a9SRui Paulo 			    IEEE80211_ACTION_MESHPEERING_CLOSE,
178059aa14a9SRui Paulo 			    args);
178159aa14a9SRui Paulo 			mesh_linkchange(ni, IEEE80211_NODE_MESH_HOLDING);
178259aa14a9SRui Paulo 			mesh_peer_timeout_setup(ni);
178359aa14a9SRui Paulo 			break;
178459aa14a9SRui Paulo 		}
178559aa14a9SRui Paulo 		args[0] = ni->ni_mlpid;
178659aa14a9SRui Paulo 		args[1] = ni->ni_mllid;
178759aa14a9SRui Paulo 		ieee80211_send_action(ni,
1788ebeaa1adSMonthadar Al Jaberi 		    IEEE80211_ACTION_CAT_SELF_PROT,
178959aa14a9SRui Paulo 		    IEEE80211_ACTION_MESHPEERING_CONFIRM,
179059aa14a9SRui Paulo 		    args);
179159aa14a9SRui Paulo 		break;
179259aa14a9SRui Paulo 	case IEEE80211_NODE_MESH_HOLDING:
179359aa14a9SRui Paulo 		args[0] = ni->ni_mlpid;
179459aa14a9SRui Paulo 		args[1] = meshpeer->peer_llinkid;
179559aa14a9SRui Paulo 		args[2] = IEEE80211_REASON_MESH_MAX_RETRIES;
179659aa14a9SRui Paulo 		ieee80211_send_action(ni,
1797ebeaa1adSMonthadar Al Jaberi 		    IEEE80211_ACTION_CAT_SELF_PROT,
179859aa14a9SRui Paulo 		    IEEE80211_ACTION_MESHPEERING_CLOSE,
179959aa14a9SRui Paulo 		    args);
180059aa14a9SRui Paulo 		break;
180159aa14a9SRui Paulo 	}
180259aa14a9SRui Paulo 	return 0;
180359aa14a9SRui Paulo }
180459aa14a9SRui Paulo 
180559aa14a9SRui Paulo static int
180659aa14a9SRui Paulo mesh_recv_action_meshpeering_confirm(struct ieee80211_node *ni,
180759aa14a9SRui Paulo 	const struct ieee80211_frame *wh,
180859aa14a9SRui Paulo 	const uint8_t *frm, const uint8_t *efrm)
180959aa14a9SRui Paulo {
181059aa14a9SRui Paulo 	struct ieee80211vap *vap = ni->ni_vap;
181159aa14a9SRui Paulo 	struct ieee80211_meshpeer_ie ie;
181259aa14a9SRui Paulo 	const struct ieee80211_meshpeer_ie *meshpeer;
181359aa14a9SRui Paulo 	uint16_t args[3];
181459aa14a9SRui Paulo 
181559aa14a9SRui Paulo 	/* +2+2+2+2 for action + code + capabilites + status code + AID */
1816c77735e2SRui Paulo 	meshpeer = mesh_parse_meshpeering_action(ni, wh, frm+2+2+2+2, efrm, &ie,
1817c77735e2SRui Paulo 	    IEEE80211_ACTION_MESHPEERING_CONFIRM);
181859aa14a9SRui Paulo 	if (meshpeer == NULL) {
181959aa14a9SRui Paulo 		return 0;
182059aa14a9SRui Paulo 	}
182159aa14a9SRui Paulo 
182259aa14a9SRui Paulo 	IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH, ni,
182359aa14a9SRui Paulo 	    "recv PEER CONFIRM, local id 0x%x, peer id 0x%x",
182459aa14a9SRui Paulo 	    meshpeer->peer_llinkid, meshpeer->peer_linkid);
182559aa14a9SRui Paulo 
182659aa14a9SRui Paulo 	switch (ni->ni_mlstate) {
182759aa14a9SRui Paulo 	case IEEE80211_NODE_MESH_OPENRCV:
182859aa14a9SRui Paulo 		mesh_linkchange(ni, IEEE80211_NODE_MESH_ESTABLISHED);
182959aa14a9SRui Paulo 		mesh_peer_timeout_stop(ni);
183059aa14a9SRui Paulo 		break;
183159aa14a9SRui Paulo 	case IEEE80211_NODE_MESH_OPENSNT:
183259aa14a9SRui Paulo 		mesh_linkchange(ni, IEEE80211_NODE_MESH_CONFIRMRCV);
183359aa14a9SRui Paulo 		break;
183459aa14a9SRui Paulo 	case IEEE80211_NODE_MESH_HOLDING:
183559aa14a9SRui Paulo 		args[0] = ni->ni_mlpid;
183659aa14a9SRui Paulo 		args[1] = meshpeer->peer_llinkid;
183759aa14a9SRui Paulo 		args[2] = IEEE80211_REASON_MESH_MAX_RETRIES;
183859aa14a9SRui Paulo 		ieee80211_send_action(ni,
1839ebeaa1adSMonthadar Al Jaberi 		    IEEE80211_ACTION_CAT_SELF_PROT,
184059aa14a9SRui Paulo 		    IEEE80211_ACTION_MESHPEERING_CLOSE,
184159aa14a9SRui Paulo 		    args);
184259aa14a9SRui Paulo 		break;
184359aa14a9SRui Paulo 	case IEEE80211_NODE_MESH_CONFIRMRCV:
184459aa14a9SRui Paulo 		if (ni->ni_mllid != meshpeer->peer_llinkid) {
184559aa14a9SRui Paulo 			args[0] = ni->ni_mlpid;
184659aa14a9SRui Paulo 			args[1] = ni->ni_mllid;
184759aa14a9SRui Paulo 			args[2] = IEEE80211_REASON_PEER_LINK_CANCELED;
184859aa14a9SRui Paulo 			ieee80211_send_action(ni,
1849ebeaa1adSMonthadar Al Jaberi 			    IEEE80211_ACTION_CAT_SELF_PROT,
185059aa14a9SRui Paulo 			    IEEE80211_ACTION_MESHPEERING_CLOSE,
185159aa14a9SRui Paulo 			    args);
185259aa14a9SRui Paulo 			mesh_linkchange(ni, IEEE80211_NODE_MESH_HOLDING);
185359aa14a9SRui Paulo 			mesh_peer_timeout_setup(ni);
185459aa14a9SRui Paulo 		}
185559aa14a9SRui Paulo 		break;
185659aa14a9SRui Paulo 	default:
185759aa14a9SRui Paulo 		IEEE80211_DISCARD(vap,
185859aa14a9SRui Paulo 		    IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH,
185959aa14a9SRui Paulo 		    wh, NULL, "received confirm in invalid state %d",
186059aa14a9SRui Paulo 		    ni->ni_mlstate);
186159aa14a9SRui Paulo 		vap->iv_stats.is_rx_mgtdiscard++;
186259aa14a9SRui Paulo 		break;
186359aa14a9SRui Paulo 	}
186459aa14a9SRui Paulo 	return 0;
186559aa14a9SRui Paulo }
186659aa14a9SRui Paulo 
186759aa14a9SRui Paulo static int
186859aa14a9SRui Paulo mesh_recv_action_meshpeering_close(struct ieee80211_node *ni,
186959aa14a9SRui Paulo 	const struct ieee80211_frame *wh,
187059aa14a9SRui Paulo 	const uint8_t *frm, const uint8_t *efrm)
187159aa14a9SRui Paulo {
187259aa14a9SRui Paulo 	uint16_t args[3];
187359aa14a9SRui Paulo 
187459aa14a9SRui Paulo 	IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH,
187559aa14a9SRui Paulo 	    ni, "%s", "recv PEER CLOSE");
187659aa14a9SRui Paulo 
187759aa14a9SRui Paulo 	switch (ni->ni_mlstate) {
187859aa14a9SRui Paulo 	case IEEE80211_NODE_MESH_IDLE:
187959aa14a9SRui Paulo 		/* ignore */
188059aa14a9SRui Paulo 		break;
188159aa14a9SRui Paulo 	case IEEE80211_NODE_MESH_OPENRCV:
188259aa14a9SRui Paulo 	case IEEE80211_NODE_MESH_OPENSNT:
188359aa14a9SRui Paulo 	case IEEE80211_NODE_MESH_CONFIRMRCV:
188459aa14a9SRui Paulo 	case IEEE80211_NODE_MESH_ESTABLISHED:
188559aa14a9SRui Paulo 		args[0] = ni->ni_mlpid;
188659aa14a9SRui Paulo 		args[1] = ni->ni_mllid;
188759aa14a9SRui Paulo 		args[2] = IEEE80211_REASON_MESH_CLOSE_RCVD;
188859aa14a9SRui Paulo 		ieee80211_send_action(ni,
1889ebeaa1adSMonthadar Al Jaberi 		    IEEE80211_ACTION_CAT_SELF_PROT,
189059aa14a9SRui Paulo 		    IEEE80211_ACTION_MESHPEERING_CLOSE,
189159aa14a9SRui Paulo 		    args);
189259aa14a9SRui Paulo 		mesh_linkchange(ni, IEEE80211_NODE_MESH_HOLDING);
189359aa14a9SRui Paulo 		mesh_peer_timeout_setup(ni);
189459aa14a9SRui Paulo 		break;
189559aa14a9SRui Paulo 	case IEEE80211_NODE_MESH_HOLDING:
189659aa14a9SRui Paulo 		mesh_linkchange(ni, IEEE80211_NODE_MESH_IDLE);
189759aa14a9SRui Paulo 		mesh_peer_timeout_setup(ni);
189859aa14a9SRui Paulo 		break;
189959aa14a9SRui Paulo 	}
190059aa14a9SRui Paulo 	return 0;
190159aa14a9SRui Paulo }
190259aa14a9SRui Paulo 
190359aa14a9SRui Paulo /*
190459aa14a9SRui Paulo  * Link Metric handling.
190559aa14a9SRui Paulo  */
190659aa14a9SRui Paulo static int
1907bdd2a076SAdrian Chadd mesh_recv_action_meshlmetric(struct ieee80211_node *ni,
190859aa14a9SRui Paulo 	const struct ieee80211_frame *wh,
190959aa14a9SRui Paulo 	const uint8_t *frm, const uint8_t *efrm)
191059aa14a9SRui Paulo {
1911bdd2a076SAdrian Chadd 	const struct ieee80211_meshlmetric_ie *ie =
1912bdd2a076SAdrian Chadd 	    (const struct ieee80211_meshlmetric_ie *)
1913bdd2a076SAdrian Chadd 	    (frm+2); /* action + code */
1914bdd2a076SAdrian Chadd 	struct ieee80211_meshlmetric_ie lm_rep;
191559aa14a9SRui Paulo 
1916bdd2a076SAdrian Chadd 	if (ie->lm_flags & IEEE80211_MESH_LMETRIC_FLAGS_REQ) {
1917bdd2a076SAdrian Chadd 		lm_rep.lm_flags = 0;
1918bdd2a076SAdrian Chadd 		lm_rep.lm_metric = mesh_airtime_calc(ni);
191959aa14a9SRui Paulo 		ieee80211_send_action(ni,
1920bdd2a076SAdrian Chadd 		    IEEE80211_ACTION_CAT_MESH,
1921bdd2a076SAdrian Chadd 		    IEEE80211_ACTION_MESH_LMETRIC,
1922bdd2a076SAdrian Chadd 		    &lm_rep);
192359aa14a9SRui Paulo 	}
1924bdd2a076SAdrian Chadd 	/* XXX: else do nothing for now */
192559aa14a9SRui Paulo 	return 0;
192659aa14a9SRui Paulo }
192759aa14a9SRui Paulo 
192859aa14a9SRui Paulo static int
192959aa14a9SRui Paulo mesh_send_action(struct ieee80211_node *ni, struct mbuf *m)
193059aa14a9SRui Paulo {
193159aa14a9SRui Paulo 	struct ieee80211_bpf_params params;
193259aa14a9SRui Paulo 
193359aa14a9SRui Paulo 	memset(&params, 0, sizeof(params));
193459aa14a9SRui Paulo 	params.ibp_pri = WME_AC_VO;
193559aa14a9SRui Paulo 	params.ibp_rate0 = ni->ni_txparms->mgmtrate;
193659aa14a9SRui Paulo 	/* XXX ucast/mcast */
193759aa14a9SRui Paulo 	params.ibp_try0 = ni->ni_txparms->maxretry;
193859aa14a9SRui Paulo 	params.ibp_power = ni->ni_txpower;
193959aa14a9SRui Paulo 	return ieee80211_mgmt_output(ni, m, IEEE80211_FC0_SUBTYPE_ACTION,
194059aa14a9SRui Paulo 	     &params);
194159aa14a9SRui Paulo }
194259aa14a9SRui Paulo 
194359aa14a9SRui Paulo #define	ADDSHORT(frm, v) do {			\
194459aa14a9SRui Paulo 	frm[0] = (v) & 0xff;			\
194559aa14a9SRui Paulo 	frm[1] = (v) >> 8;			\
194659aa14a9SRui Paulo 	frm += 2;				\
194759aa14a9SRui Paulo } while (0)
194859aa14a9SRui Paulo #define	ADDWORD(frm, v) do {			\
194959aa14a9SRui Paulo 	frm[0] = (v) & 0xff;			\
195059aa14a9SRui Paulo 	frm[1] = ((v) >> 8) & 0xff;		\
195159aa14a9SRui Paulo 	frm[2] = ((v) >> 16) & 0xff;		\
195259aa14a9SRui Paulo 	frm[3] = ((v) >> 24) & 0xff;		\
195359aa14a9SRui Paulo 	frm += 4;				\
195459aa14a9SRui Paulo } while (0)
195559aa14a9SRui Paulo 
195659aa14a9SRui Paulo static int
195759aa14a9SRui Paulo mesh_send_action_meshpeering_open(struct ieee80211_node *ni,
195859aa14a9SRui Paulo 	int category, int action, void *args0)
195959aa14a9SRui Paulo {
196059aa14a9SRui Paulo 	struct ieee80211vap *vap = ni->ni_vap;
196159aa14a9SRui Paulo 	struct ieee80211com *ic = ni->ni_ic;
196259aa14a9SRui Paulo 	uint16_t *args = args0;
196359aa14a9SRui Paulo 	const struct ieee80211_rateset *rs;
196459aa14a9SRui Paulo 	struct mbuf *m;
196559aa14a9SRui Paulo 	uint8_t *frm;
196659aa14a9SRui Paulo 
196759aa14a9SRui Paulo 	IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH, ni,
196859aa14a9SRui Paulo 	    "send PEER OPEN action: localid 0x%x", args[0]);
196959aa14a9SRui Paulo 
197059aa14a9SRui Paulo 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
197159aa14a9SRui Paulo 	    "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", __func__, __LINE__,
197259aa14a9SRui Paulo 	    ni, ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)+1);
197359aa14a9SRui Paulo 	ieee80211_ref_node(ni);
197459aa14a9SRui Paulo 
197559aa14a9SRui Paulo 	m = ieee80211_getmgtframe(&frm,
197659aa14a9SRui Paulo 	    ic->ic_headroom + sizeof(struct ieee80211_frame),
197759aa14a9SRui Paulo 	    sizeof(uint16_t)	/* action+category */
197859aa14a9SRui Paulo 	    + sizeof(uint16_t)	/* capabilites */
197959aa14a9SRui Paulo 	    + 2 + IEEE80211_RATE_SIZE
198059aa14a9SRui Paulo 	    + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
198159aa14a9SRui Paulo 	    + 2 + IEEE80211_MESHID_LEN
198259aa14a9SRui Paulo 	    + sizeof(struct ieee80211_meshconf_ie)
198359aa14a9SRui Paulo 	    + sizeof(struct ieee80211_meshpeer_ie)
198459aa14a9SRui Paulo 	);
198559aa14a9SRui Paulo 	if (m != NULL) {
198659aa14a9SRui Paulo 		/*
198759aa14a9SRui Paulo 		 * mesh peer open action frame format:
198859aa14a9SRui Paulo 		 *   [1] category
198959aa14a9SRui Paulo 		 *   [1] action
199059aa14a9SRui Paulo 		 *   [2] capabilities
199159aa14a9SRui Paulo 		 *   [tlv] rates
199259aa14a9SRui Paulo 		 *   [tlv] xrates
199359aa14a9SRui Paulo 		 *   [tlv] mesh id
199459aa14a9SRui Paulo 		 *   [tlv] mesh conf
199559aa14a9SRui Paulo 		 *   [tlv] mesh peer link mgmt
199659aa14a9SRui Paulo 		 */
199759aa14a9SRui Paulo 		*frm++ = category;
199859aa14a9SRui Paulo 		*frm++ = action;
199959aa14a9SRui Paulo 		ADDSHORT(frm, ieee80211_getcapinfo(vap, ni->ni_chan));
200059aa14a9SRui Paulo 		rs = ieee80211_get_suprates(ic, ic->ic_curchan);
200159aa14a9SRui Paulo 		frm = ieee80211_add_rates(frm, rs);
200259aa14a9SRui Paulo 		frm = ieee80211_add_xrates(frm, rs);
200359aa14a9SRui Paulo 		frm = ieee80211_add_meshid(frm, vap);
200459aa14a9SRui Paulo 		frm = ieee80211_add_meshconf(frm, vap);
2005ebeaa1adSMonthadar Al Jaberi 		frm = ieee80211_add_meshpeer(frm, IEEE80211_ACTION_MESHPEERING_OPEN,
200659aa14a9SRui Paulo 		    args[0], 0, 0);
200759aa14a9SRui Paulo 		m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
200859aa14a9SRui Paulo 		return mesh_send_action(ni, m);
200959aa14a9SRui Paulo 	} else {
201059aa14a9SRui Paulo 		vap->iv_stats.is_tx_nobuf++;
201159aa14a9SRui Paulo 		ieee80211_free_node(ni);
201259aa14a9SRui Paulo 		return ENOMEM;
201359aa14a9SRui Paulo 	}
201459aa14a9SRui Paulo }
201559aa14a9SRui Paulo 
201659aa14a9SRui Paulo static int
201759aa14a9SRui Paulo mesh_send_action_meshpeering_confirm(struct ieee80211_node *ni,
201859aa14a9SRui Paulo 	int category, int action, void *args0)
201959aa14a9SRui Paulo {
202059aa14a9SRui Paulo 	struct ieee80211vap *vap = ni->ni_vap;
202159aa14a9SRui Paulo 	struct ieee80211com *ic = ni->ni_ic;
202259aa14a9SRui Paulo 	uint16_t *args = args0;
202359aa14a9SRui Paulo 	const struct ieee80211_rateset *rs;
202459aa14a9SRui Paulo 	struct mbuf *m;
202559aa14a9SRui Paulo 	uint8_t *frm;
202659aa14a9SRui Paulo 
202759aa14a9SRui Paulo 	IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH, ni,
202859aa14a9SRui Paulo 	    "send PEER CONFIRM action: localid 0x%x, peerid 0x%x",
202959aa14a9SRui Paulo 	    args[0], args[1]);
203059aa14a9SRui Paulo 
203159aa14a9SRui Paulo 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
203259aa14a9SRui Paulo 	    "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", __func__, __LINE__,
203359aa14a9SRui Paulo 	    ni, ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)+1);
203459aa14a9SRui Paulo 	ieee80211_ref_node(ni);
203559aa14a9SRui Paulo 
203659aa14a9SRui Paulo 	m = ieee80211_getmgtframe(&frm,
203759aa14a9SRui Paulo 	    ic->ic_headroom + sizeof(struct ieee80211_frame),
203859aa14a9SRui Paulo 	    sizeof(uint16_t)	/* action+category */
203959aa14a9SRui Paulo 	    + sizeof(uint16_t)	/* capabilites */
204059aa14a9SRui Paulo 	    + sizeof(uint16_t)	/* status code */
204159aa14a9SRui Paulo 	    + sizeof(uint16_t)	/* AID */
204259aa14a9SRui Paulo 	    + 2 + IEEE80211_RATE_SIZE
204359aa14a9SRui Paulo 	    + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
204459aa14a9SRui Paulo 	    + 2 + IEEE80211_MESHID_LEN
204559aa14a9SRui Paulo 	    + sizeof(struct ieee80211_meshconf_ie)
204659aa14a9SRui Paulo 	    + sizeof(struct ieee80211_meshpeer_ie)
204759aa14a9SRui Paulo 	);
204859aa14a9SRui Paulo 	if (m != NULL) {
204959aa14a9SRui Paulo 		/*
205059aa14a9SRui Paulo 		 * mesh peer confirm action frame format:
205159aa14a9SRui Paulo 		 *   [1] category
205259aa14a9SRui Paulo 		 *   [1] action
205359aa14a9SRui Paulo 		 *   [2] capabilities
205459aa14a9SRui Paulo 		 *   [2] status code
205559aa14a9SRui Paulo 		 *   [2] association id (peer ID)
205659aa14a9SRui Paulo 		 *   [tlv] rates
205759aa14a9SRui Paulo 		 *   [tlv] xrates
205859aa14a9SRui Paulo 		 *   [tlv] mesh id
205959aa14a9SRui Paulo 		 *   [tlv] mesh conf
206059aa14a9SRui Paulo 		 *   [tlv] mesh peer link mgmt
206159aa14a9SRui Paulo 		 */
206259aa14a9SRui Paulo 		*frm++ = category;
206359aa14a9SRui Paulo 		*frm++ = action;
206459aa14a9SRui Paulo 		ADDSHORT(frm, ieee80211_getcapinfo(vap, ni->ni_chan));
206559aa14a9SRui Paulo 		ADDSHORT(frm, 0);		/* status code */
206659aa14a9SRui Paulo 		ADDSHORT(frm, args[1]);		/* AID */
206759aa14a9SRui Paulo 		rs = ieee80211_get_suprates(ic, ic->ic_curchan);
206859aa14a9SRui Paulo 		frm = ieee80211_add_rates(frm, rs);
206959aa14a9SRui Paulo 		frm = ieee80211_add_xrates(frm, rs);
207059aa14a9SRui Paulo 		frm = ieee80211_add_meshid(frm, vap);
207159aa14a9SRui Paulo 		frm = ieee80211_add_meshconf(frm, vap);
207259aa14a9SRui Paulo 		frm = ieee80211_add_meshpeer(frm,
2073ebeaa1adSMonthadar Al Jaberi 		    IEEE80211_ACTION_MESHPEERING_CONFIRM,
207459aa14a9SRui Paulo 		    args[0], args[1], 0);
207559aa14a9SRui Paulo 		m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
207659aa14a9SRui Paulo 		return mesh_send_action(ni, m);
207759aa14a9SRui Paulo 	} else {
207859aa14a9SRui Paulo 		vap->iv_stats.is_tx_nobuf++;
207959aa14a9SRui Paulo 		ieee80211_free_node(ni);
208059aa14a9SRui Paulo 		return ENOMEM;
208159aa14a9SRui Paulo 	}
208259aa14a9SRui Paulo }
208359aa14a9SRui Paulo 
208459aa14a9SRui Paulo static int
208559aa14a9SRui Paulo mesh_send_action_meshpeering_close(struct ieee80211_node *ni,
208659aa14a9SRui Paulo 	int category, int action, void *args0)
208759aa14a9SRui Paulo {
208859aa14a9SRui Paulo 	struct ieee80211vap *vap = ni->ni_vap;
208959aa14a9SRui Paulo 	struct ieee80211com *ic = ni->ni_ic;
209059aa14a9SRui Paulo 	uint16_t *args = args0;
209159aa14a9SRui Paulo 	struct mbuf *m;
209259aa14a9SRui Paulo 	uint8_t *frm;
209359aa14a9SRui Paulo 
209459aa14a9SRui Paulo 	IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH, ni,
209559aa14a9SRui Paulo 	    "send PEER CLOSE action: localid 0x%x, peerid 0x%x reason %d",
209659aa14a9SRui Paulo 	    args[0], args[1], args[2]);
209759aa14a9SRui Paulo 
209859aa14a9SRui Paulo 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
209959aa14a9SRui Paulo 	    "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", __func__, __LINE__,
210059aa14a9SRui Paulo 	    ni, ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)+1);
210159aa14a9SRui Paulo 	ieee80211_ref_node(ni);
210259aa14a9SRui Paulo 
210359aa14a9SRui Paulo 	m = ieee80211_getmgtframe(&frm,
210459aa14a9SRui Paulo 	    ic->ic_headroom + sizeof(struct ieee80211_frame),
210559aa14a9SRui Paulo 	    sizeof(uint16_t)	/* action+category */
210659aa14a9SRui Paulo 	    + sizeof(uint16_t)	/* reason code */
210759aa14a9SRui Paulo 	    + 2 + IEEE80211_MESHID_LEN
210859aa14a9SRui Paulo 	    + sizeof(struct ieee80211_meshpeer_ie)
210959aa14a9SRui Paulo 	);
211059aa14a9SRui Paulo 	if (m != NULL) {
211159aa14a9SRui Paulo 		/*
211259aa14a9SRui Paulo 		 * mesh peer close action frame format:
211359aa14a9SRui Paulo 		 *   [1] category
211459aa14a9SRui Paulo 		 *   [1] action
211559aa14a9SRui Paulo 		 *   [2] reason code
211659aa14a9SRui Paulo 		 *   [tlv] mesh id
211759aa14a9SRui Paulo 		 *   [tlv] mesh peer link mgmt
211859aa14a9SRui Paulo 		 */
211959aa14a9SRui Paulo 		*frm++ = category;
212059aa14a9SRui Paulo 		*frm++ = action;
212159aa14a9SRui Paulo 		ADDSHORT(frm, args[2]);		/* reason code */
212259aa14a9SRui Paulo 		frm = ieee80211_add_meshid(frm, vap);
212359aa14a9SRui Paulo 		frm = ieee80211_add_meshpeer(frm,
2124ebeaa1adSMonthadar Al Jaberi 		    IEEE80211_ACTION_MESHPEERING_CLOSE,
212559aa14a9SRui Paulo 		    args[0], args[1], args[2]);
212659aa14a9SRui Paulo 		m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
212759aa14a9SRui Paulo 		return mesh_send_action(ni, m);
212859aa14a9SRui Paulo 	} else {
212959aa14a9SRui Paulo 		vap->iv_stats.is_tx_nobuf++;
213059aa14a9SRui Paulo 		ieee80211_free_node(ni);
213159aa14a9SRui Paulo 		return ENOMEM;
213259aa14a9SRui Paulo 	}
213359aa14a9SRui Paulo }
213459aa14a9SRui Paulo 
213559aa14a9SRui Paulo static int
2136bdd2a076SAdrian Chadd mesh_send_action_meshlmetric(struct ieee80211_node *ni,
213759aa14a9SRui Paulo 	int category, int action, void *arg0)
213859aa14a9SRui Paulo {
213959aa14a9SRui Paulo 	struct ieee80211vap *vap = ni->ni_vap;
214059aa14a9SRui Paulo 	struct ieee80211com *ic = ni->ni_ic;
2141bdd2a076SAdrian Chadd 	struct ieee80211_meshlmetric_ie *ie = arg0;
214259aa14a9SRui Paulo 	struct mbuf *m;
214359aa14a9SRui Paulo 	uint8_t *frm;
214459aa14a9SRui Paulo 
2145bdd2a076SAdrian Chadd 	if (ie->lm_flags & IEEE80211_MESH_LMETRIC_FLAGS_REQ) {
2146bdd2a076SAdrian Chadd 		IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH,
2147bdd2a076SAdrian Chadd 		    ni, "%s", "send LINK METRIC REQUEST action");
214859aa14a9SRui Paulo 	} else {
2149bdd2a076SAdrian Chadd 		IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH,
2150bdd2a076SAdrian Chadd 		    ni, "send LINK METRIC REPLY action: metric 0x%x",
2151bdd2a076SAdrian Chadd 		    ie->lm_metric);
215259aa14a9SRui Paulo 	}
215359aa14a9SRui Paulo 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
215459aa14a9SRui Paulo 	    "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", __func__, __LINE__,
215559aa14a9SRui Paulo 	    ni, ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)+1);
215659aa14a9SRui Paulo 	ieee80211_ref_node(ni);
215759aa14a9SRui Paulo 
215859aa14a9SRui Paulo 	m = ieee80211_getmgtframe(&frm,
215959aa14a9SRui Paulo 	    ic->ic_headroom + sizeof(struct ieee80211_frame),
2160bdd2a076SAdrian Chadd 	    sizeof(uint16_t) +	/* action+category */
2161bdd2a076SAdrian Chadd 	    sizeof(struct ieee80211_meshlmetric_ie)
216259aa14a9SRui Paulo 	);
216359aa14a9SRui Paulo 	if (m != NULL) {
216459aa14a9SRui Paulo 		/*
2165bdd2a076SAdrian Chadd 		 * mesh link metric
216659aa14a9SRui Paulo 		 *   [1] category
216759aa14a9SRui Paulo 		 *   [1] action
216859aa14a9SRui Paulo 		 *   [tlv] mesh link metric
216959aa14a9SRui Paulo 		 */
217059aa14a9SRui Paulo 		*frm++ = category;
217159aa14a9SRui Paulo 		*frm++ = action;
2172bdd2a076SAdrian Chadd 		frm = ieee80211_add_meshlmetric(frm,
2173bdd2a076SAdrian Chadd 		    ie->lm_flags, ie->lm_metric);
217459aa14a9SRui Paulo 		m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
217559aa14a9SRui Paulo 		return mesh_send_action(ni, m);
217659aa14a9SRui Paulo 	} else {
217759aa14a9SRui Paulo 		vap->iv_stats.is_tx_nobuf++;
217859aa14a9SRui Paulo 		ieee80211_free_node(ni);
217959aa14a9SRui Paulo 		return ENOMEM;
218059aa14a9SRui Paulo 	}
218159aa14a9SRui Paulo }
218259aa14a9SRui Paulo 
218359aa14a9SRui Paulo static void
218459aa14a9SRui Paulo mesh_peer_timeout_setup(struct ieee80211_node *ni)
218559aa14a9SRui Paulo {
218659aa14a9SRui Paulo 	switch (ni->ni_mlstate) {
218759aa14a9SRui Paulo 	case IEEE80211_NODE_MESH_HOLDING:
218859aa14a9SRui Paulo 		ni->ni_mltval = ieee80211_mesh_holdingtimeout;
218959aa14a9SRui Paulo 		break;
219059aa14a9SRui Paulo 	case IEEE80211_NODE_MESH_CONFIRMRCV:
219159aa14a9SRui Paulo 		ni->ni_mltval = ieee80211_mesh_confirmtimeout;
219259aa14a9SRui Paulo 		break;
219359aa14a9SRui Paulo 	case IEEE80211_NODE_MESH_IDLE:
219459aa14a9SRui Paulo 		ni->ni_mltval = 0;
219559aa14a9SRui Paulo 		break;
219659aa14a9SRui Paulo 	default:
219759aa14a9SRui Paulo 		ni->ni_mltval = ieee80211_mesh_retrytimeout;
219859aa14a9SRui Paulo 		break;
219959aa14a9SRui Paulo 	}
220059aa14a9SRui Paulo 	if (ni->ni_mltval)
220159aa14a9SRui Paulo 		callout_reset(&ni->ni_mltimer, ni->ni_mltval,
220259aa14a9SRui Paulo 		    mesh_peer_timeout_cb, ni);
220359aa14a9SRui Paulo }
220459aa14a9SRui Paulo 
220559aa14a9SRui Paulo /*
220659aa14a9SRui Paulo  * Same as above but backoffs timer statisically 50%.
220759aa14a9SRui Paulo  */
220859aa14a9SRui Paulo static void
220959aa14a9SRui Paulo mesh_peer_timeout_backoff(struct ieee80211_node *ni)
221059aa14a9SRui Paulo {
221159aa14a9SRui Paulo 	uint32_t r;
221259aa14a9SRui Paulo 
221359aa14a9SRui Paulo 	r = arc4random();
221459aa14a9SRui Paulo 	ni->ni_mltval += r % ni->ni_mltval;
221559aa14a9SRui Paulo 	callout_reset(&ni->ni_mltimer, ni->ni_mltval, mesh_peer_timeout_cb,
221659aa14a9SRui Paulo 	    ni);
221759aa14a9SRui Paulo }
221859aa14a9SRui Paulo 
221959aa14a9SRui Paulo static __inline void
222059aa14a9SRui Paulo mesh_peer_timeout_stop(struct ieee80211_node *ni)
222159aa14a9SRui Paulo {
2222c104cff2SRui Paulo 	callout_drain(&ni->ni_mltimer);
222359aa14a9SRui Paulo }
222459aa14a9SRui Paulo 
222559aa14a9SRui Paulo /*
222659aa14a9SRui Paulo  * Mesh Peer Link Management FSM timeout handling.
222759aa14a9SRui Paulo  */
222859aa14a9SRui Paulo static void
222959aa14a9SRui Paulo mesh_peer_timeout_cb(void *arg)
223059aa14a9SRui Paulo {
223159aa14a9SRui Paulo 	struct ieee80211_node *ni = (struct ieee80211_node *)arg;
223259aa14a9SRui Paulo 	uint16_t args[3];
223359aa14a9SRui Paulo 
223459aa14a9SRui Paulo 	IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_MESH,
223559aa14a9SRui Paulo 	    ni, "mesh link timeout, state %d, retry counter %d",
223659aa14a9SRui Paulo 	    ni->ni_mlstate, ni->ni_mlrcnt);
223759aa14a9SRui Paulo 
223859aa14a9SRui Paulo 	switch (ni->ni_mlstate) {
223959aa14a9SRui Paulo 	case IEEE80211_NODE_MESH_IDLE:
224059aa14a9SRui Paulo 	case IEEE80211_NODE_MESH_ESTABLISHED:
224159aa14a9SRui Paulo 		break;
224259aa14a9SRui Paulo 	case IEEE80211_NODE_MESH_OPENSNT:
224359aa14a9SRui Paulo 	case IEEE80211_NODE_MESH_OPENRCV:
224459aa14a9SRui Paulo 		if (ni->ni_mlrcnt == ieee80211_mesh_maxretries) {
224559aa14a9SRui Paulo 			args[0] = ni->ni_mlpid;
224659aa14a9SRui Paulo 			args[2] = IEEE80211_REASON_MESH_MAX_RETRIES;
224759aa14a9SRui Paulo 			ieee80211_send_action(ni,
2248ebeaa1adSMonthadar Al Jaberi 			    IEEE80211_ACTION_CAT_SELF_PROT,
224959aa14a9SRui Paulo 			    IEEE80211_ACTION_MESHPEERING_CLOSE, args);
225059aa14a9SRui Paulo 			ni->ni_mlrcnt = 0;
225159aa14a9SRui Paulo 			mesh_linkchange(ni, IEEE80211_NODE_MESH_HOLDING);
225259aa14a9SRui Paulo 			mesh_peer_timeout_setup(ni);
225359aa14a9SRui Paulo 		} else {
225459aa14a9SRui Paulo 			args[0] = ni->ni_mlpid;
225559aa14a9SRui Paulo 			ieee80211_send_action(ni,
2256ebeaa1adSMonthadar Al Jaberi 			    IEEE80211_ACTION_CAT_SELF_PROT,
225759aa14a9SRui Paulo 			    IEEE80211_ACTION_MESHPEERING_OPEN, args);
225859aa14a9SRui Paulo 			ni->ni_mlrcnt++;
225959aa14a9SRui Paulo 			mesh_peer_timeout_backoff(ni);
226059aa14a9SRui Paulo 		}
226159aa14a9SRui Paulo 		break;
226259aa14a9SRui Paulo 	case IEEE80211_NODE_MESH_CONFIRMRCV:
226359aa14a9SRui Paulo 		if (ni->ni_mlrcnt == ieee80211_mesh_maxretries) {
226459aa14a9SRui Paulo 			args[0] = ni->ni_mlpid;
226559aa14a9SRui Paulo 			args[2] = IEEE80211_REASON_MESH_CONFIRM_TIMEOUT;
226659aa14a9SRui Paulo 			ieee80211_send_action(ni,
2267ebeaa1adSMonthadar Al Jaberi 			    IEEE80211_ACTION_CAT_SELF_PROT,
226859aa14a9SRui Paulo 			    IEEE80211_ACTION_MESHPEERING_CLOSE, args);
226959aa14a9SRui Paulo 			ni->ni_mlrcnt = 0;
227059aa14a9SRui Paulo 			mesh_linkchange(ni, IEEE80211_NODE_MESH_HOLDING);
227159aa14a9SRui Paulo 			mesh_peer_timeout_setup(ni);
227259aa14a9SRui Paulo 		} else {
227359aa14a9SRui Paulo 			ni->ni_mlrcnt++;
227459aa14a9SRui Paulo 			mesh_peer_timeout_setup(ni);
227559aa14a9SRui Paulo 		}
227659aa14a9SRui Paulo 		break;
227759aa14a9SRui Paulo 	case IEEE80211_NODE_MESH_HOLDING:
227859aa14a9SRui Paulo 		mesh_linkchange(ni, IEEE80211_NODE_MESH_IDLE);
227959aa14a9SRui Paulo 		break;
228059aa14a9SRui Paulo 	}
228159aa14a9SRui Paulo }
228259aa14a9SRui Paulo 
2283aec0289dSRui Paulo static int
228459aa14a9SRui Paulo mesh_verify_meshid(struct ieee80211vap *vap, const uint8_t *ie)
228559aa14a9SRui Paulo {
228659aa14a9SRui Paulo 	struct ieee80211_mesh_state *ms = vap->iv_mesh;
228759aa14a9SRui Paulo 
228859aa14a9SRui Paulo 	if (ie == NULL || ie[1] != ms->ms_idlen)
228959aa14a9SRui Paulo 		return 1;
229059aa14a9SRui Paulo 	return memcmp(ms->ms_id, ie + 2, ms->ms_idlen);
229159aa14a9SRui Paulo }
229259aa14a9SRui Paulo 
229359aa14a9SRui Paulo /*
229459aa14a9SRui Paulo  * Check if we are using the same algorithms for this mesh.
229559aa14a9SRui Paulo  */
229659aa14a9SRui Paulo static int
229759aa14a9SRui Paulo mesh_verify_meshconf(struct ieee80211vap *vap, const uint8_t *ie)
229859aa14a9SRui Paulo {
229959aa14a9SRui Paulo 	const struct ieee80211_meshconf_ie *meshconf =
230059aa14a9SRui Paulo 	    (const struct ieee80211_meshconf_ie *) ie;
230159aa14a9SRui Paulo 	const struct ieee80211_mesh_state *ms = vap->iv_mesh;
230259aa14a9SRui Paulo 
230359aa14a9SRui Paulo 	if (meshconf == NULL)
230459aa14a9SRui Paulo 		return 1;
23056b8c1829SRui Paulo 	if (meshconf->conf_pselid != ms->ms_ppath->mpp_ie) {
230659aa14a9SRui Paulo 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_MESH,
23076b8c1829SRui Paulo 		    "unknown path selection algorithm: 0x%x\n",
23086b8c1829SRui Paulo 		    meshconf->conf_pselid);
230959aa14a9SRui Paulo 		return 1;
231059aa14a9SRui Paulo 	}
23116b8c1829SRui Paulo 	if (meshconf->conf_pmetid != ms->ms_pmetric->mpm_ie) {
231259aa14a9SRui Paulo 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_MESH,
23136b8c1829SRui Paulo 		    "unknown path metric algorithm: 0x%x\n",
23146b8c1829SRui Paulo 		    meshconf->conf_pmetid);
231559aa14a9SRui Paulo 		return 1;
231659aa14a9SRui Paulo 	}
23176b8c1829SRui Paulo 	if (meshconf->conf_ccid != 0) {
231859aa14a9SRui Paulo 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_MESH,
23196b8c1829SRui Paulo 		    "unknown congestion control algorithm: 0x%x\n",
23206b8c1829SRui Paulo 		    meshconf->conf_ccid);
232159aa14a9SRui Paulo 		return 1;
232259aa14a9SRui Paulo 	}
23236b8c1829SRui Paulo 	if (meshconf->conf_syncid != IEEE80211_MESHCONF_SYNC_NEIGHOFF) {
232459aa14a9SRui Paulo 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_MESH,
23256b8c1829SRui Paulo 		    "unknown sync algorithm: 0x%x\n",
23266b8c1829SRui Paulo 		    meshconf->conf_syncid);
232759aa14a9SRui Paulo 		return 1;
232859aa14a9SRui Paulo 	}
23296b8c1829SRui Paulo 	if (meshconf->conf_authid != 0) {
233059aa14a9SRui Paulo 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_MESH,
23316b8c1829SRui Paulo 		    "unknown auth auth algorithm: 0x%x\n",
23326b8c1829SRui Paulo 		    meshconf->conf_pselid);
233359aa14a9SRui Paulo 		return 1;
233459aa14a9SRui Paulo 	}
233559aa14a9SRui Paulo 	/* Not accepting peers */
233681b95c2cSAdrian Chadd 	if (!(meshconf->conf_cap & IEEE80211_MESHCONF_CAP_AP)) {
233759aa14a9SRui Paulo 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_MESH,
233859aa14a9SRui Paulo 		    "not accepting peers: 0x%x\n", meshconf->conf_cap);
233959aa14a9SRui Paulo 		return 1;
234059aa14a9SRui Paulo 	}
234159aa14a9SRui Paulo 	return 0;
234259aa14a9SRui Paulo }
234359aa14a9SRui Paulo 
234459aa14a9SRui Paulo static int
2345c77735e2SRui Paulo mesh_verify_meshpeer(struct ieee80211vap *vap, uint8_t subtype,
2346c77735e2SRui Paulo     const uint8_t *ie)
234759aa14a9SRui Paulo {
234859aa14a9SRui Paulo 	const struct ieee80211_meshpeer_ie *meshpeer =
234959aa14a9SRui Paulo 	    (const struct ieee80211_meshpeer_ie *) ie;
235059aa14a9SRui Paulo 
2351*c2042c35SMonthadar Al Jaberi 	if (meshpeer == NULL ||
2352*c2042c35SMonthadar Al Jaberi 	    meshpeer->peer_len < IEEE80211_MPM_BASE_SZ ||
2353*c2042c35SMonthadar Al Jaberi 	    meshpeer->peer_len > IEEE80211_MPM_MAX_SZ)
235459aa14a9SRui Paulo 		return 1;
2355*c2042c35SMonthadar Al Jaberi 	if (meshpeer->peer_proto != IEEE80211_MPPID_MPM) {
2356*c2042c35SMonthadar Al Jaberi 		IEEE80211_DPRINTF(vap,
2357*c2042c35SMonthadar Al Jaberi 		    IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH,
2358*c2042c35SMonthadar Al Jaberi 		    "Only MPM protocol is supported (proto: 0x%02X)",
2359*c2042c35SMonthadar Al Jaberi 		    meshpeer->peer_proto);
2360*c2042c35SMonthadar Al Jaberi 		return 1;
2361*c2042c35SMonthadar Al Jaberi 	}
2362c77735e2SRui Paulo 	switch (subtype) {
2363ebeaa1adSMonthadar Al Jaberi 	case IEEE80211_ACTION_MESHPEERING_OPEN:
2364*c2042c35SMonthadar Al Jaberi 		if (meshpeer->peer_len != IEEE80211_MPM_BASE_SZ)
236559aa14a9SRui Paulo 			return 1;
236659aa14a9SRui Paulo 		break;
2367ebeaa1adSMonthadar Al Jaberi 	case IEEE80211_ACTION_MESHPEERING_CONFIRM:
2368*c2042c35SMonthadar Al Jaberi 		if (meshpeer->peer_len != IEEE80211_MPM_BASE_SZ + 2)
236959aa14a9SRui Paulo 			return 1;
237059aa14a9SRui Paulo 		break;
2371ebeaa1adSMonthadar Al Jaberi 	case IEEE80211_ACTION_MESHPEERING_CLOSE:
2372*c2042c35SMonthadar Al Jaberi 		if (meshpeer->peer_len < IEEE80211_MPM_BASE_SZ + 2)
237359aa14a9SRui Paulo 			return 1;
2374*c2042c35SMonthadar Al Jaberi 		if (meshpeer->peer_len == (IEEE80211_MPM_BASE_SZ + 2) &&
2375*c2042c35SMonthadar Al Jaberi 		    meshpeer->peer_linkid != 0)
237659aa14a9SRui Paulo 			return 1;
237759aa14a9SRui Paulo 		if (meshpeer->peer_rcode == 0)
237859aa14a9SRui Paulo 			return 1;
237959aa14a9SRui Paulo 		break;
238059aa14a9SRui Paulo 	}
238159aa14a9SRui Paulo 	return 0;
238259aa14a9SRui Paulo }
238359aa14a9SRui Paulo 
238459aa14a9SRui Paulo /*
238559aa14a9SRui Paulo  * Add a Mesh ID IE to a frame.
238659aa14a9SRui Paulo  */
238759aa14a9SRui Paulo uint8_t *
238859aa14a9SRui Paulo ieee80211_add_meshid(uint8_t *frm, struct ieee80211vap *vap)
238959aa14a9SRui Paulo {
239059aa14a9SRui Paulo 	struct ieee80211_mesh_state *ms = vap->iv_mesh;
239159aa14a9SRui Paulo 
239259aa14a9SRui Paulo 	KASSERT(vap->iv_opmode == IEEE80211_M_MBSS, ("not a mbss vap"));
239359aa14a9SRui Paulo 
239459aa14a9SRui Paulo 	*frm++ = IEEE80211_ELEMID_MESHID;
239559aa14a9SRui Paulo 	*frm++ = ms->ms_idlen;
239659aa14a9SRui Paulo 	memcpy(frm, ms->ms_id, ms->ms_idlen);
239759aa14a9SRui Paulo 	return frm + ms->ms_idlen;
239859aa14a9SRui Paulo }
239959aa14a9SRui Paulo 
240059aa14a9SRui Paulo /*
240159aa14a9SRui Paulo  * Add a Mesh Configuration IE to a frame.
240259aa14a9SRui Paulo  * For now just use HWMP routing, Airtime link metric, Null Congestion
240359aa14a9SRui Paulo  * Signaling, Null Sync Protocol and Null Authentication.
240459aa14a9SRui Paulo  */
240559aa14a9SRui Paulo uint8_t *
240659aa14a9SRui Paulo ieee80211_add_meshconf(uint8_t *frm, struct ieee80211vap *vap)
240759aa14a9SRui Paulo {
240859aa14a9SRui Paulo 	const struct ieee80211_mesh_state *ms = vap->iv_mesh;
2409651e41a4SRui Paulo 	uint16_t caps;
241059aa14a9SRui Paulo 
241159aa14a9SRui Paulo 	KASSERT(vap->iv_opmode == IEEE80211_M_MBSS, ("not a MBSS vap"));
241259aa14a9SRui Paulo 
241359aa14a9SRui Paulo 	*frm++ = IEEE80211_ELEMID_MESHCONF;
241481b95c2cSAdrian Chadd 	*frm++ = IEEE80211_MESH_CONF_SZ;
24156b8c1829SRui Paulo 	*frm++ = ms->ms_ppath->mpp_ie;		/* path selection */
24166b8c1829SRui Paulo 	*frm++ = ms->ms_pmetric->mpm_ie;	/* link metric */
24176b8c1829SRui Paulo 	*frm++ = IEEE80211_MESHCONF_CC_DISABLED;
24186b8c1829SRui Paulo 	*frm++ = IEEE80211_MESHCONF_SYNC_NEIGHOFF;
24196b8c1829SRui Paulo 	*frm++ = IEEE80211_MESHCONF_AUTH_DISABLED;
242059aa14a9SRui Paulo 	/* NB: set the number of neighbors before the rest */
242159aa14a9SRui Paulo 	*frm = (ms->ms_neighbors > 15 ? 15 : ms->ms_neighbors) << 1;
242259aa14a9SRui Paulo 	if (ms->ms_flags & IEEE80211_MESHFLAGS_PORTAL)
242359aa14a9SRui Paulo 		*frm |= IEEE80211_MESHCONF_FORM_MP;
242459aa14a9SRui Paulo 	frm += 1;
2425651e41a4SRui Paulo 	caps = 0;
242659aa14a9SRui Paulo 	if (ms->ms_flags & IEEE80211_MESHFLAGS_AP)
2427651e41a4SRui Paulo 		caps |= IEEE80211_MESHCONF_CAP_AP;
242859aa14a9SRui Paulo 	if (ms->ms_flags & IEEE80211_MESHFLAGS_FWD)
2429651e41a4SRui Paulo 		caps |= IEEE80211_MESHCONF_CAP_FWRD;
243081b95c2cSAdrian Chadd 	*frm++ = caps;
243159aa14a9SRui Paulo 	return frm;
243259aa14a9SRui Paulo }
243359aa14a9SRui Paulo 
243459aa14a9SRui Paulo /*
243559aa14a9SRui Paulo  * Add a Mesh Peer Management IE to a frame.
243659aa14a9SRui Paulo  */
243759aa14a9SRui Paulo uint8_t *
243859aa14a9SRui Paulo ieee80211_add_meshpeer(uint8_t *frm, uint8_t subtype, uint16_t localid,
243959aa14a9SRui Paulo     uint16_t peerid, uint16_t reason)
244059aa14a9SRui Paulo {
2441c77735e2SRui Paulo 
244259aa14a9SRui Paulo 	KASSERT(localid != 0, ("localid == 0"));
244359aa14a9SRui Paulo 
244459aa14a9SRui Paulo 	*frm++ = IEEE80211_ELEMID_MESHPEER;
244559aa14a9SRui Paulo 	switch (subtype) {
2446ebeaa1adSMonthadar Al Jaberi 	case IEEE80211_ACTION_MESHPEERING_OPEN:
2447*c2042c35SMonthadar Al Jaberi 		*frm++ = IEEE80211_MPM_BASE_SZ;		/* length */
2448*c2042c35SMonthadar Al Jaberi 		ADDSHORT(frm, IEEE80211_MPPID_MPM);	/* proto */
244959aa14a9SRui Paulo 		ADDSHORT(frm, localid);			/* local ID */
245059aa14a9SRui Paulo 		break;
2451ebeaa1adSMonthadar Al Jaberi 	case IEEE80211_ACTION_MESHPEERING_CONFIRM:
245259aa14a9SRui Paulo 		KASSERT(peerid != 0, ("sending peer confirm without peer id"));
2453*c2042c35SMonthadar Al Jaberi 		*frm++ = IEEE80211_MPM_BASE_SZ + 2;	/* length */
2454*c2042c35SMonthadar Al Jaberi 		ADDSHORT(frm, IEEE80211_MPPID_MPM);	/* proto */
245559aa14a9SRui Paulo 		ADDSHORT(frm, localid);			/* local ID */
245659aa14a9SRui Paulo 		ADDSHORT(frm, peerid);			/* peer ID */
245759aa14a9SRui Paulo 		break;
2458ebeaa1adSMonthadar Al Jaberi 	case IEEE80211_ACTION_MESHPEERING_CLOSE:
245959aa14a9SRui Paulo 		if (peerid)
2460*c2042c35SMonthadar Al Jaberi 			*frm++ = IEEE80211_MPM_MAX_SZ;	/* length */
246159aa14a9SRui Paulo 		else
2462*c2042c35SMonthadar Al Jaberi 			*frm++ = IEEE80211_MPM_BASE_SZ + 2; /* length */
2463*c2042c35SMonthadar Al Jaberi 		ADDSHORT(frm, IEEE80211_MPPID_MPM);	/* proto */
246459aa14a9SRui Paulo 		ADDSHORT(frm, localid);	/* local ID */
246559aa14a9SRui Paulo 		if (peerid)
246659aa14a9SRui Paulo 			ADDSHORT(frm, peerid);	/* peer ID */
246759aa14a9SRui Paulo 		ADDSHORT(frm, reason);
246859aa14a9SRui Paulo 		break;
246959aa14a9SRui Paulo 	}
247059aa14a9SRui Paulo 	return frm;
247159aa14a9SRui Paulo }
247259aa14a9SRui Paulo 
247359aa14a9SRui Paulo /*
247459aa14a9SRui Paulo  * Compute an Airtime Link Metric for the link with this node.
247559aa14a9SRui Paulo  *
247659aa14a9SRui Paulo  * Based on Draft 3.0 spec (11B.10, p.149).
247759aa14a9SRui Paulo  */
247859aa14a9SRui Paulo /*
247959aa14a9SRui Paulo  * Max 802.11s overhead.
248059aa14a9SRui Paulo  */
248159aa14a9SRui Paulo #define IEEE80211_MESH_MAXOVERHEAD \
248259aa14a9SRui Paulo 	(sizeof(struct ieee80211_qosframe_addr4) \
248359aa14a9SRui Paulo 	 + sizeof(struct ieee80211_meshcntl_ae11) \
248459aa14a9SRui Paulo 	+ sizeof(struct llc) \
248559aa14a9SRui Paulo 	+ IEEE80211_ADDR_LEN \
248659aa14a9SRui Paulo 	+ IEEE80211_WEP_IVLEN \
248759aa14a9SRui Paulo 	+ IEEE80211_WEP_KIDLEN \
248859aa14a9SRui Paulo 	+ IEEE80211_WEP_CRCLEN \
248959aa14a9SRui Paulo 	+ IEEE80211_WEP_MICLEN \
249059aa14a9SRui Paulo 	+ IEEE80211_CRC_LEN)
249159aa14a9SRui Paulo uint32_t
249259aa14a9SRui Paulo mesh_airtime_calc(struct ieee80211_node *ni)
249359aa14a9SRui Paulo {
249459aa14a9SRui Paulo #define M_BITS 8
249559aa14a9SRui Paulo #define S_FACTOR (2 * M_BITS)
249659aa14a9SRui Paulo 	struct ieee80211com *ic = ni->ni_ic;
249759aa14a9SRui Paulo 	struct ifnet *ifp = ni->ni_vap->iv_ifp;
249859aa14a9SRui Paulo 	const static int nbits = 8192 << M_BITS;
249959aa14a9SRui Paulo 	uint32_t overhead, rate, errrate;
250059aa14a9SRui Paulo 	uint64_t res;
250159aa14a9SRui Paulo 
250259aa14a9SRui Paulo 	/* Time to transmit a frame */
250359aa14a9SRui Paulo 	rate = ni->ni_txrate;
250459aa14a9SRui Paulo 	overhead = ieee80211_compute_duration(ic->ic_rt,
250559aa14a9SRui Paulo 	    ifp->if_mtu + IEEE80211_MESH_MAXOVERHEAD, rate, 0) << M_BITS;
250659aa14a9SRui Paulo 	/* Error rate in percentage */
250759aa14a9SRui Paulo 	/* XXX assuming small failures are ok */
250859aa14a9SRui Paulo 	errrate = (((ifp->if_oerrors +
250959aa14a9SRui Paulo 	    ifp->if_ierrors) / 100) << M_BITS) / 100;
251059aa14a9SRui Paulo 	res = (overhead + (nbits / rate)) *
251159aa14a9SRui Paulo 	    ((1 << S_FACTOR) / ((1 << M_BITS) - errrate));
251259aa14a9SRui Paulo 
251359aa14a9SRui Paulo 	return (uint32_t)(res >> S_FACTOR);
251459aa14a9SRui Paulo #undef M_BITS
251559aa14a9SRui Paulo #undef S_FACTOR
251659aa14a9SRui Paulo }
251759aa14a9SRui Paulo 
251859aa14a9SRui Paulo /*
251959aa14a9SRui Paulo  * Add a Mesh Link Metric report IE to a frame.
252059aa14a9SRui Paulo  */
252159aa14a9SRui Paulo uint8_t *
2522bdd2a076SAdrian Chadd ieee80211_add_meshlmetric(uint8_t *frm, uint8_t flags, uint32_t metric)
252359aa14a9SRui Paulo {
252459aa14a9SRui Paulo 	*frm++ = IEEE80211_ELEMID_MESHLINK;
2525bdd2a076SAdrian Chadd 	*frm++ = 5;
2526bdd2a076SAdrian Chadd 	*frm++ = flags;
252759aa14a9SRui Paulo 	ADDWORD(frm, metric);
252859aa14a9SRui Paulo 	return frm;
252959aa14a9SRui Paulo }
253059aa14a9SRui Paulo #undef ADDSHORT
253159aa14a9SRui Paulo #undef ADDWORD
253259aa14a9SRui Paulo 
253359aa14a9SRui Paulo /*
253459aa14a9SRui Paulo  * Initialize any mesh-specific node state.
253559aa14a9SRui Paulo  */
253659aa14a9SRui Paulo void
253759aa14a9SRui Paulo ieee80211_mesh_node_init(struct ieee80211vap *vap, struct ieee80211_node *ni)
253859aa14a9SRui Paulo {
253959aa14a9SRui Paulo 	ni->ni_flags |= IEEE80211_NODE_QOS;
254059aa14a9SRui Paulo 	callout_init(&ni->ni_mltimer, CALLOUT_MPSAFE);
254159aa14a9SRui Paulo }
254259aa14a9SRui Paulo 
254359aa14a9SRui Paulo /*
254459aa14a9SRui Paulo  * Cleanup any mesh-specific node state.
254559aa14a9SRui Paulo  */
254659aa14a9SRui Paulo void
254759aa14a9SRui Paulo ieee80211_mesh_node_cleanup(struct ieee80211_node *ni)
254859aa14a9SRui Paulo {
254959aa14a9SRui Paulo 	struct ieee80211vap *vap = ni->ni_vap;
255059aa14a9SRui Paulo 	struct ieee80211_mesh_state *ms = vap->iv_mesh;
255159aa14a9SRui Paulo 
255259aa14a9SRui Paulo 	callout_drain(&ni->ni_mltimer);
255359aa14a9SRui Paulo 	/* NB: short-circuit callbacks after mesh_vdetach */
255459aa14a9SRui Paulo 	if (vap->iv_mesh != NULL)
255559aa14a9SRui Paulo 		ms->ms_ppath->mpp_peerdown(ni);
255659aa14a9SRui Paulo }
255759aa14a9SRui Paulo 
255859aa14a9SRui Paulo void
255959aa14a9SRui Paulo ieee80211_parse_meshid(struct ieee80211_node *ni, const uint8_t *ie)
256059aa14a9SRui Paulo {
256159aa14a9SRui Paulo 	ni->ni_meshidlen = ie[1];
256259aa14a9SRui Paulo 	memcpy(ni->ni_meshid, ie + 2, ie[1]);
256359aa14a9SRui Paulo }
256459aa14a9SRui Paulo 
256559aa14a9SRui Paulo /*
256659aa14a9SRui Paulo  * Setup mesh-specific node state on neighbor discovery.
256759aa14a9SRui Paulo  */
256859aa14a9SRui Paulo void
256959aa14a9SRui Paulo ieee80211_mesh_init_neighbor(struct ieee80211_node *ni,
257059aa14a9SRui Paulo 	const struct ieee80211_frame *wh,
257159aa14a9SRui Paulo 	const struct ieee80211_scanparams *sp)
257259aa14a9SRui Paulo {
257359aa14a9SRui Paulo 	ieee80211_parse_meshid(ni, sp->meshid);
257459aa14a9SRui Paulo }
257559aa14a9SRui Paulo 
2576d093681cSRui Paulo void
2577d093681cSRui Paulo ieee80211_mesh_update_beacon(struct ieee80211vap *vap,
2578d093681cSRui Paulo 	struct ieee80211_beacon_offsets *bo)
2579d093681cSRui Paulo {
2580d093681cSRui Paulo 	KASSERT(vap->iv_opmode == IEEE80211_M_MBSS, ("not a MBSS vap"));
2581d093681cSRui Paulo 
2582d093681cSRui Paulo 	if (isset(bo->bo_flags, IEEE80211_BEACON_MESHCONF)) {
2583d093681cSRui Paulo 		(void)ieee80211_add_meshconf(bo->bo_meshconf, vap);
2584d093681cSRui Paulo 		clrbit(bo->bo_flags, IEEE80211_BEACON_MESHCONF);
2585d093681cSRui Paulo 	}
2586d093681cSRui Paulo }
2587d093681cSRui Paulo 
258859aa14a9SRui Paulo static int
258959aa14a9SRui Paulo mesh_ioctl_get80211(struct ieee80211vap *vap, struct ieee80211req *ireq)
259059aa14a9SRui Paulo {
259159aa14a9SRui Paulo 	struct ieee80211_mesh_state *ms = vap->iv_mesh;
259259aa14a9SRui Paulo 	uint8_t tmpmeshid[IEEE80211_NWID_LEN];
259359aa14a9SRui Paulo 	struct ieee80211_mesh_route *rt;
259459aa14a9SRui Paulo 	struct ieee80211req_mesh_route *imr;
259559aa14a9SRui Paulo 	size_t len, off;
259659aa14a9SRui Paulo 	uint8_t *p;
259759aa14a9SRui Paulo 	int error;
259859aa14a9SRui Paulo 
259959aa14a9SRui Paulo 	if (vap->iv_opmode != IEEE80211_M_MBSS)
260059aa14a9SRui Paulo 		return ENOSYS;
260159aa14a9SRui Paulo 
260259aa14a9SRui Paulo 	error = 0;
260359aa14a9SRui Paulo 	switch (ireq->i_type) {
260459aa14a9SRui Paulo 	case IEEE80211_IOC_MESH_ID:
260559aa14a9SRui Paulo 		ireq->i_len = ms->ms_idlen;
260659aa14a9SRui Paulo 		memcpy(tmpmeshid, ms->ms_id, ireq->i_len);
260759aa14a9SRui Paulo 		error = copyout(tmpmeshid, ireq->i_data, ireq->i_len);
260859aa14a9SRui Paulo 		break;
260959aa14a9SRui Paulo 	case IEEE80211_IOC_MESH_AP:
261059aa14a9SRui Paulo 		ireq->i_val = (ms->ms_flags & IEEE80211_MESHFLAGS_AP) != 0;
261159aa14a9SRui Paulo 		break;
261259aa14a9SRui Paulo 	case IEEE80211_IOC_MESH_FWRD:
261359aa14a9SRui Paulo 		ireq->i_val = (ms->ms_flags & IEEE80211_MESHFLAGS_FWD) != 0;
261459aa14a9SRui Paulo 		break;
261559aa14a9SRui Paulo 	case IEEE80211_IOC_MESH_TTL:
261659aa14a9SRui Paulo 		ireq->i_val = ms->ms_ttl;
261759aa14a9SRui Paulo 		break;
261859aa14a9SRui Paulo 	case IEEE80211_IOC_MESH_RTCMD:
261959aa14a9SRui Paulo 		switch (ireq->i_val) {
262059aa14a9SRui Paulo 		case IEEE80211_MESH_RTCMD_LIST:
262159aa14a9SRui Paulo 			len = 0;
262259aa14a9SRui Paulo 			MESH_RT_LOCK(ms);
262359aa14a9SRui Paulo 			TAILQ_FOREACH(rt, &ms->ms_routes, rt_next) {
262459aa14a9SRui Paulo 				len += sizeof(*imr);
262559aa14a9SRui Paulo 			}
262659aa14a9SRui Paulo 			MESH_RT_UNLOCK(ms);
262759aa14a9SRui Paulo 			if (len > ireq->i_len || ireq->i_len < sizeof(*imr)) {
262859aa14a9SRui Paulo 				ireq->i_len = len;
262959aa14a9SRui Paulo 				return ENOMEM;
263059aa14a9SRui Paulo 			}
263159aa14a9SRui Paulo 			ireq->i_len = len;
2632c104cff2SRui Paulo 			/* XXX M_WAIT? */
263359aa14a9SRui Paulo 			p = malloc(len, M_TEMP, M_NOWAIT | M_ZERO);
263459aa14a9SRui Paulo 			if (p == NULL)
263559aa14a9SRui Paulo 				return ENOMEM;
263659aa14a9SRui Paulo 			off = 0;
263759aa14a9SRui Paulo 			MESH_RT_LOCK(ms);
263859aa14a9SRui Paulo 			TAILQ_FOREACH(rt, &ms->ms_routes, rt_next) {
263959aa14a9SRui Paulo 				if (off >= len)
264059aa14a9SRui Paulo 					break;
264159aa14a9SRui Paulo 				imr = (struct ieee80211req_mesh_route *)
264259aa14a9SRui Paulo 				    (p + off);
26433ca80f0dSRui Paulo 				imr->imr_flags = rt->rt_flags;
264459aa14a9SRui Paulo 				IEEE80211_ADDR_COPY(imr->imr_dest,
264559aa14a9SRui Paulo 				    rt->rt_dest);
264659aa14a9SRui Paulo 				IEEE80211_ADDR_COPY(imr->imr_nexthop,
264759aa14a9SRui Paulo 				    rt->rt_nexthop);
264859aa14a9SRui Paulo 				imr->imr_metric = rt->rt_metric;
264959aa14a9SRui Paulo 				imr->imr_nhops = rt->rt_nhops;
265059aa14a9SRui Paulo 				imr->imr_lifetime = rt->rt_lifetime;
2651c104cff2SRui Paulo 				imr->imr_lastmseq = rt->rt_lastmseq;
265259aa14a9SRui Paulo 				off += sizeof(*imr);
265359aa14a9SRui Paulo 			}
265459aa14a9SRui Paulo 			MESH_RT_UNLOCK(ms);
265559aa14a9SRui Paulo 			error = copyout(p, (uint8_t *)ireq->i_data,
265659aa14a9SRui Paulo 			    ireq->i_len);
265759aa14a9SRui Paulo 			free(p, M_TEMP);
265859aa14a9SRui Paulo 			break;
265959aa14a9SRui Paulo 		case IEEE80211_MESH_RTCMD_FLUSH:
266059aa14a9SRui Paulo 		case IEEE80211_MESH_RTCMD_ADD:
266159aa14a9SRui Paulo 		case IEEE80211_MESH_RTCMD_DELETE:
266259aa14a9SRui Paulo 			return EINVAL;
266359aa14a9SRui Paulo 		default:
266459aa14a9SRui Paulo 			return ENOSYS;
266559aa14a9SRui Paulo 		}
266659aa14a9SRui Paulo 		break;
266759aa14a9SRui Paulo 	case IEEE80211_IOC_MESH_PR_METRIC:
266859aa14a9SRui Paulo 		len = strlen(ms->ms_pmetric->mpm_descr);
266959aa14a9SRui Paulo 		if (ireq->i_len < len)
267059aa14a9SRui Paulo 			return EINVAL;
267159aa14a9SRui Paulo 		ireq->i_len = len;
267259aa14a9SRui Paulo 		error = copyout(ms->ms_pmetric->mpm_descr,
267359aa14a9SRui Paulo 		    (uint8_t *)ireq->i_data, len);
267459aa14a9SRui Paulo 		break;
267559aa14a9SRui Paulo 	case IEEE80211_IOC_MESH_PR_PATH:
267659aa14a9SRui Paulo 		len = strlen(ms->ms_ppath->mpp_descr);
267759aa14a9SRui Paulo 		if (ireq->i_len < len)
267859aa14a9SRui Paulo 			return EINVAL;
267959aa14a9SRui Paulo 		ireq->i_len = len;
268059aa14a9SRui Paulo 		error = copyout(ms->ms_ppath->mpp_descr,
268159aa14a9SRui Paulo 		    (uint8_t *)ireq->i_data, len);
268259aa14a9SRui Paulo 		break;
268359aa14a9SRui Paulo 	default:
268459aa14a9SRui Paulo 		return ENOSYS;
268559aa14a9SRui Paulo 	}
268659aa14a9SRui Paulo 
268759aa14a9SRui Paulo 	return error;
268859aa14a9SRui Paulo }
268959aa14a9SRui Paulo IEEE80211_IOCTL_GET(mesh, mesh_ioctl_get80211);
269059aa14a9SRui Paulo 
269159aa14a9SRui Paulo static int
269259aa14a9SRui Paulo mesh_ioctl_set80211(struct ieee80211vap *vap, struct ieee80211req *ireq)
269359aa14a9SRui Paulo {
269459aa14a9SRui Paulo 	struct ieee80211_mesh_state *ms = vap->iv_mesh;
269559aa14a9SRui Paulo 	uint8_t tmpmeshid[IEEE80211_NWID_LEN];
269659aa14a9SRui Paulo 	uint8_t tmpaddr[IEEE80211_ADDR_LEN];
269759aa14a9SRui Paulo 	char tmpproto[IEEE80211_MESH_PROTO_DSZ];
269859aa14a9SRui Paulo 	int error;
269959aa14a9SRui Paulo 
270059aa14a9SRui Paulo 	if (vap->iv_opmode != IEEE80211_M_MBSS)
270159aa14a9SRui Paulo 		return ENOSYS;
270259aa14a9SRui Paulo 
270359aa14a9SRui Paulo 	error = 0;
270459aa14a9SRui Paulo 	switch (ireq->i_type) {
270559aa14a9SRui Paulo 	case IEEE80211_IOC_MESH_ID:
270659aa14a9SRui Paulo 		if (ireq->i_val != 0 || ireq->i_len > IEEE80211_MESHID_LEN)
270759aa14a9SRui Paulo 			return EINVAL;
270859aa14a9SRui Paulo 		error = copyin(ireq->i_data, tmpmeshid, ireq->i_len);
2709c104cff2SRui Paulo 		if (error != 0)
271059aa14a9SRui Paulo 			break;
271159aa14a9SRui Paulo 		memset(ms->ms_id, 0, IEEE80211_NWID_LEN);
271259aa14a9SRui Paulo 		ms->ms_idlen = ireq->i_len;
271359aa14a9SRui Paulo 		memcpy(ms->ms_id, tmpmeshid, ireq->i_len);
2714c104cff2SRui Paulo 		error = ENETRESET;
271559aa14a9SRui Paulo 		break;
271659aa14a9SRui Paulo 	case IEEE80211_IOC_MESH_AP:
271759aa14a9SRui Paulo 		if (ireq->i_val)
271859aa14a9SRui Paulo 			ms->ms_flags |= IEEE80211_MESHFLAGS_AP;
271959aa14a9SRui Paulo 		else
272059aa14a9SRui Paulo 			ms->ms_flags &= ~IEEE80211_MESHFLAGS_AP;
2721c104cff2SRui Paulo 		error = ENETRESET;
272259aa14a9SRui Paulo 		break;
272359aa14a9SRui Paulo 	case IEEE80211_IOC_MESH_FWRD:
272459aa14a9SRui Paulo 		if (ireq->i_val)
272559aa14a9SRui Paulo 			ms->ms_flags |= IEEE80211_MESHFLAGS_FWD;
272659aa14a9SRui Paulo 		else
272759aa14a9SRui Paulo 			ms->ms_flags &= ~IEEE80211_MESHFLAGS_FWD;
272859aa14a9SRui Paulo 		break;
272959aa14a9SRui Paulo 	case IEEE80211_IOC_MESH_TTL:
273059aa14a9SRui Paulo 		ms->ms_ttl = (uint8_t) ireq->i_val;
273159aa14a9SRui Paulo 		break;
273259aa14a9SRui Paulo 	case IEEE80211_IOC_MESH_RTCMD:
273359aa14a9SRui Paulo 		switch (ireq->i_val) {
273459aa14a9SRui Paulo 		case IEEE80211_MESH_RTCMD_LIST:
273559aa14a9SRui Paulo 			return EINVAL;
273659aa14a9SRui Paulo 		case IEEE80211_MESH_RTCMD_FLUSH:
273759aa14a9SRui Paulo 			ieee80211_mesh_rt_flush(vap);
273859aa14a9SRui Paulo 			break;
273959aa14a9SRui Paulo 		case IEEE80211_MESH_RTCMD_ADD:
274059aa14a9SRui Paulo 			if (IEEE80211_ADDR_EQ(vap->iv_myaddr, ireq->i_data) ||
274159aa14a9SRui Paulo 			    IEEE80211_ADDR_EQ(broadcastaddr, ireq->i_data))
274259aa14a9SRui Paulo 				return EINVAL;
274359aa14a9SRui Paulo 			error = copyin(ireq->i_data, &tmpaddr,
274459aa14a9SRui Paulo 			    IEEE80211_ADDR_LEN);
2745c104cff2SRui Paulo 			if (error == 0)
274659aa14a9SRui Paulo 				ieee80211_mesh_discover(vap, tmpaddr, NULL);
274759aa14a9SRui Paulo 			break;
274859aa14a9SRui Paulo 		case IEEE80211_MESH_RTCMD_DELETE:
274959aa14a9SRui Paulo 			ieee80211_mesh_rt_del(vap, ireq->i_data);
275059aa14a9SRui Paulo 			break;
275159aa14a9SRui Paulo 		default:
275259aa14a9SRui Paulo 			return ENOSYS;
275359aa14a9SRui Paulo 		}
275459aa14a9SRui Paulo 		break;
275559aa14a9SRui Paulo 	case IEEE80211_IOC_MESH_PR_METRIC:
275659aa14a9SRui Paulo 		error = copyin(ireq->i_data, tmpproto, sizeof(tmpproto));
2757c104cff2SRui Paulo 		if (error == 0) {
2758c104cff2SRui Paulo 			error = mesh_select_proto_metric(vap, tmpproto);
2759c104cff2SRui Paulo 			if (error == 0)
2760c104cff2SRui Paulo 				error = ENETRESET;
2761c104cff2SRui Paulo 		}
276259aa14a9SRui Paulo 		break;
276359aa14a9SRui Paulo 	case IEEE80211_IOC_MESH_PR_PATH:
276459aa14a9SRui Paulo 		error = copyin(ireq->i_data, tmpproto, sizeof(tmpproto));
2765c104cff2SRui Paulo 		if (error == 0) {
2766c104cff2SRui Paulo 			error = mesh_select_proto_path(vap, tmpproto);
2767c104cff2SRui Paulo 			if (error == 0)
2768c104cff2SRui Paulo 				error = ENETRESET;
2769c104cff2SRui Paulo 		}
277059aa14a9SRui Paulo 		break;
277159aa14a9SRui Paulo 	default:
277259aa14a9SRui Paulo 		return ENOSYS;
277359aa14a9SRui Paulo 	}
277459aa14a9SRui Paulo 	return error;
277559aa14a9SRui Paulo }
277659aa14a9SRui Paulo IEEE80211_IOCTL_SET(mesh, mesh_ioctl_set80211);
2777