ieee80211_mesh.c revision 234875
1/*-
2 * Copyright (c) 2009 The FreeBSD Foundation
3 * All rights reserved.
4 *
5 * This software was developed by Rui Paulo under sponsorship from the
6 * FreeBSD Foundation.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29#include <sys/cdefs.h>
30#ifdef __FreeBSD__
31__FBSDID("$FreeBSD: head/sys/net80211/ieee80211_mesh.c 234875 2012-05-01 15:39:16Z monthadar $");
32#endif
33
34/*
35 * IEEE 802.11s Mesh Point (MBSS) support.
36 *
37 * Based on March 2009, D3.0 802.11s draft spec.
38 */
39#include "opt_inet.h"
40#include "opt_wlan.h"
41
42#include <sys/param.h>
43#include <sys/systm.h>
44#include <sys/mbuf.h>
45#include <sys/malloc.h>
46#include <sys/kernel.h>
47
48#include <sys/socket.h>
49#include <sys/sockio.h>
50#include <sys/endian.h>
51#include <sys/errno.h>
52#include <sys/proc.h>
53#include <sys/sysctl.h>
54
55#include <net/if.h>
56#include <net/if_media.h>
57#include <net/if_llc.h>
58#include <net/ethernet.h>
59
60#include <net80211/ieee80211_var.h>
61#include <net80211/ieee80211_action.h>
62#include <net80211/ieee80211_input.h>
63#include <net80211/ieee80211_mesh.h>
64
65static void	mesh_rt_flush_invalid(struct ieee80211vap *);
66static int	mesh_select_proto_path(struct ieee80211vap *, const char *);
67static int	mesh_select_proto_metric(struct ieee80211vap *, const char *);
68static void	mesh_vattach(struct ieee80211vap *);
69static int	mesh_newstate(struct ieee80211vap *, enum ieee80211_state, int);
70static void	mesh_rt_cleanup_cb(void *);
71static void	mesh_linkchange(struct ieee80211_node *,
72		    enum ieee80211_mesh_mlstate);
73static void	mesh_checkid(void *, struct ieee80211_node *);
74static uint32_t	mesh_generateid(struct ieee80211vap *);
75static int	mesh_checkpseq(struct ieee80211vap *,
76		    const uint8_t [IEEE80211_ADDR_LEN], uint32_t);
77static struct ieee80211_node *
78		mesh_find_txnode(struct ieee80211vap *,
79		    const uint8_t [IEEE80211_ADDR_LEN]);
80static void	mesh_forward(struct ieee80211vap *, struct mbuf *,
81		    const struct ieee80211_meshcntl *);
82static int	mesh_input(struct ieee80211_node *, struct mbuf *, int, int);
83static void	mesh_recv_mgmt(struct ieee80211_node *, struct mbuf *, int,
84		    int, int);
85static void	mesh_recv_ctl(struct ieee80211_node *, struct mbuf *, int);
86static void	mesh_peer_timeout_setup(struct ieee80211_node *);
87static void	mesh_peer_timeout_backoff(struct ieee80211_node *);
88static void	mesh_peer_timeout_cb(void *);
89static __inline void
90		mesh_peer_timeout_stop(struct ieee80211_node *);
91static int	mesh_verify_meshid(struct ieee80211vap *, const uint8_t *);
92static int	mesh_verify_meshconf(struct ieee80211vap *, const uint8_t *);
93static int	mesh_verify_meshpeer(struct ieee80211vap *, uint8_t,
94    		    const uint8_t *);
95uint32_t	mesh_airtime_calc(struct ieee80211_node *);
96
97/*
98 * Timeout values come from the specification and are in milliseconds.
99 */
100static SYSCTL_NODE(_net_wlan, OID_AUTO, mesh, CTLFLAG_RD, 0,
101    "IEEE 802.11s parameters");
102static int ieee80211_mesh_retrytimeout = -1;
103SYSCTL_PROC(_net_wlan_mesh, OID_AUTO, retrytimeout, CTLTYPE_INT | CTLFLAG_RW,
104    &ieee80211_mesh_retrytimeout, 0, ieee80211_sysctl_msecs_ticks, "I",
105    "Retry timeout (msec)");
106static int ieee80211_mesh_holdingtimeout = -1;
107SYSCTL_PROC(_net_wlan_mesh, OID_AUTO, holdingtimeout, CTLTYPE_INT | CTLFLAG_RW,
108    &ieee80211_mesh_holdingtimeout, 0, ieee80211_sysctl_msecs_ticks, "I",
109    "Holding state timeout (msec)");
110static int ieee80211_mesh_confirmtimeout = -1;
111SYSCTL_PROC(_net_wlan_mesh, OID_AUTO, confirmtimeout, CTLTYPE_INT | CTLFLAG_RW,
112    &ieee80211_mesh_confirmtimeout, 0, ieee80211_sysctl_msecs_ticks, "I",
113    "Confirm state timeout (msec)");
114static int ieee80211_mesh_maxretries = 2;
115SYSCTL_INT(_net_wlan_mesh, OID_AUTO, maxretries, CTLTYPE_INT | CTLFLAG_RW,
116    &ieee80211_mesh_maxretries, 0,
117    "Maximum retries during peer link establishment");
118
119static const uint8_t broadcastaddr[IEEE80211_ADDR_LEN] =
120	{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
121
122static	ieee80211_recv_action_func mesh_recv_action_meshpeering_open;
123static	ieee80211_recv_action_func mesh_recv_action_meshpeering_confirm;
124static	ieee80211_recv_action_func mesh_recv_action_meshpeering_close;
125static	ieee80211_recv_action_func mesh_recv_action_meshlmetric;
126
127static	ieee80211_send_action_func mesh_send_action_meshpeering_open;
128static	ieee80211_send_action_func mesh_send_action_meshpeering_confirm;
129static	ieee80211_send_action_func mesh_send_action_meshpeering_close;
130static	ieee80211_send_action_func mesh_send_action_meshlmetric;
131
132static const struct ieee80211_mesh_proto_metric mesh_metric_airtime = {
133	.mpm_descr	= "AIRTIME",
134	.mpm_ie		= IEEE80211_MESHCONF_METRIC_AIRTIME,
135	.mpm_metric	= mesh_airtime_calc,
136};
137
138static struct ieee80211_mesh_proto_path		mesh_proto_paths[4];
139static struct ieee80211_mesh_proto_metric	mesh_proto_metrics[4];
140
141#define	MESH_RT_LOCK(ms)	mtx_lock(&(ms)->ms_rt_lock)
142#define	MESH_RT_LOCK_ASSERT(ms)	mtx_assert(&(ms)->ms_rt_lock, MA_OWNED)
143#define	MESH_RT_UNLOCK(ms)	mtx_unlock(&(ms)->ms_rt_lock)
144
145MALLOC_DEFINE(M_80211_MESH_PREQ, "80211preq", "802.11 MESH Path Request frame");
146MALLOC_DEFINE(M_80211_MESH_PREP, "80211prep", "802.11 MESH Path Reply frame");
147MALLOC_DEFINE(M_80211_MESH_PERR, "80211perr", "802.11 MESH Path Error frame");
148
149MALLOC_DEFINE(M_80211_MESH_RT, "80211mesh", "802.11s routing table");
150
151/*
152 * Helper functions to manipulate the Mesh routing table.
153 */
154
155static struct ieee80211_mesh_route *
156mesh_rt_find_locked(struct ieee80211_mesh_state *ms,
157    const uint8_t dest[IEEE80211_ADDR_LEN])
158{
159	struct ieee80211_mesh_route *rt;
160
161	MESH_RT_LOCK_ASSERT(ms);
162
163	TAILQ_FOREACH(rt, &ms->ms_routes, rt_next) {
164		if (IEEE80211_ADDR_EQ(dest, rt->rt_dest))
165			return rt;
166	}
167	return NULL;
168}
169
170static struct ieee80211_mesh_route *
171mesh_rt_add_locked(struct ieee80211_mesh_state *ms,
172    const uint8_t dest[IEEE80211_ADDR_LEN])
173{
174	struct ieee80211_mesh_route *rt;
175
176	KASSERT(!IEEE80211_ADDR_EQ(broadcastaddr, dest),
177	    ("%s: adding broadcast to the routing table", __func__));
178
179	MESH_RT_LOCK_ASSERT(ms);
180
181	rt = malloc(ALIGN(sizeof(struct ieee80211_mesh_route)) +
182	    ms->ms_ppath->mpp_privlen, M_80211_MESH_RT, M_NOWAIT | M_ZERO);
183	if (rt != NULL) {
184		IEEE80211_ADDR_COPY(rt->rt_dest, dest);
185		rt->rt_priv = (void *)ALIGN(&rt[1]);
186		rt->rt_crtime = ticks;
187		TAILQ_INSERT_TAIL(&ms->ms_routes, rt, rt_next);
188	}
189	return rt;
190}
191
192struct ieee80211_mesh_route *
193ieee80211_mesh_rt_find(struct ieee80211vap *vap,
194    const uint8_t dest[IEEE80211_ADDR_LEN])
195{
196	struct ieee80211_mesh_state *ms = vap->iv_mesh;
197	struct ieee80211_mesh_route *rt;
198
199	MESH_RT_LOCK(ms);
200	rt = mesh_rt_find_locked(ms, dest);
201	MESH_RT_UNLOCK(ms);
202	return rt;
203}
204
205struct ieee80211_mesh_route *
206ieee80211_mesh_rt_add(struct ieee80211vap *vap,
207    const uint8_t dest[IEEE80211_ADDR_LEN])
208{
209	struct ieee80211_mesh_state *ms = vap->iv_mesh;
210	struct ieee80211_mesh_route *rt;
211
212	KASSERT(ieee80211_mesh_rt_find(vap, dest) == NULL,
213	    ("%s: duplicate entry in the routing table", __func__));
214	KASSERT(!IEEE80211_ADDR_EQ(vap->iv_myaddr, dest),
215	    ("%s: adding self to the routing table", __func__));
216
217	MESH_RT_LOCK(ms);
218	rt = mesh_rt_add_locked(ms, dest);
219	MESH_RT_UNLOCK(ms);
220	return rt;
221}
222
223/*
224 * Add a proxy route (as needed) for the specified destination.
225 */
226void
227ieee80211_mesh_proxy_check(struct ieee80211vap *vap,
228    const uint8_t dest[IEEE80211_ADDR_LEN])
229{
230	struct ieee80211_mesh_state *ms = vap->iv_mesh;
231	struct ieee80211_mesh_route *rt;
232
233	MESH_RT_LOCK(ms);
234	rt = mesh_rt_find_locked(ms, dest);
235	if (rt == NULL) {
236		rt = mesh_rt_add_locked(ms, dest);
237		if (rt == NULL) {
238			IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH, dest,
239			    "%s", "unable to add proxy entry");
240			vap->iv_stats.is_mesh_rtaddfailed++;
241		} else {
242			IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH, dest,
243			    "%s", "add proxy entry");
244			IEEE80211_ADDR_COPY(rt->rt_nexthop, vap->iv_myaddr);
245			rt->rt_flags |= IEEE80211_MESHRT_FLAGS_VALID
246				     |  IEEE80211_MESHRT_FLAGS_PROXY;
247		}
248	/* XXX assert PROXY? */
249	} else if ((rt->rt_flags & IEEE80211_MESHRT_FLAGS_VALID) == 0) {
250		struct ieee80211com *ic = vap->iv_ic;
251		/*
252		 * Fix existing entry created by received frames from
253		 * stations that have some memory of dest.  We also
254		 * flush any frames held on the staging queue; delivering
255		 * them is too much trouble right now.
256		 */
257		IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH, dest,
258		    "%s", "fix proxy entry");
259		IEEE80211_ADDR_COPY(rt->rt_nexthop, vap->iv_myaddr);
260		rt->rt_flags |= IEEE80211_MESHRT_FLAGS_VALID
261			     |  IEEE80211_MESHRT_FLAGS_PROXY;
262		/* XXX belongs in hwmp */
263		ieee80211_ageq_drain_node(&ic->ic_stageq,
264		   (void *)(uintptr_t) ieee80211_mac_hash(ic, dest));
265		/* XXX stat? */
266	}
267	MESH_RT_UNLOCK(ms);
268}
269
270static __inline void
271mesh_rt_del(struct ieee80211_mesh_state *ms, struct ieee80211_mesh_route *rt)
272{
273	TAILQ_REMOVE(&ms->ms_routes, rt, rt_next);
274	free(rt, M_80211_MESH_RT);
275}
276
277void
278ieee80211_mesh_rt_del(struct ieee80211vap *vap,
279    const uint8_t dest[IEEE80211_ADDR_LEN])
280{
281	struct ieee80211_mesh_state *ms = vap->iv_mesh;
282	struct ieee80211_mesh_route *rt, *next;
283
284	MESH_RT_LOCK(ms);
285	TAILQ_FOREACH_SAFE(rt, &ms->ms_routes, rt_next, next) {
286		if (IEEE80211_ADDR_EQ(rt->rt_dest, dest)) {
287			mesh_rt_del(ms, rt);
288			MESH_RT_UNLOCK(ms);
289			return;
290		}
291	}
292	MESH_RT_UNLOCK(ms);
293}
294
295void
296ieee80211_mesh_rt_flush(struct ieee80211vap *vap)
297{
298	struct ieee80211_mesh_state *ms = vap->iv_mesh;
299	struct ieee80211_mesh_route *rt, *next;
300
301	if (ms == NULL)
302		return;
303	MESH_RT_LOCK(ms);
304	TAILQ_FOREACH_SAFE(rt, &ms->ms_routes, rt_next, next)
305		mesh_rt_del(ms, rt);
306	MESH_RT_UNLOCK(ms);
307}
308
309void
310ieee80211_mesh_rt_flush_peer(struct ieee80211vap *vap,
311    const uint8_t peer[IEEE80211_ADDR_LEN])
312{
313	struct ieee80211_mesh_state *ms = vap->iv_mesh;
314	struct ieee80211_mesh_route *rt, *next;
315
316	MESH_RT_LOCK(ms);
317	TAILQ_FOREACH_SAFE(rt, &ms->ms_routes, rt_next, next) {
318		if (IEEE80211_ADDR_EQ(rt->rt_nexthop, peer))
319			mesh_rt_del(ms, rt);
320	}
321	MESH_RT_UNLOCK(ms);
322}
323
324/*
325 * Flush expired routing entries, i.e. those in invalid state for
326 * some time.
327 */
328static void
329mesh_rt_flush_invalid(struct ieee80211vap *vap)
330{
331	struct ieee80211_mesh_state *ms = vap->iv_mesh;
332	struct ieee80211_mesh_route *rt, *next;
333
334	if (ms == NULL)
335		return;
336	MESH_RT_LOCK(ms);
337	TAILQ_FOREACH_SAFE(rt, &ms->ms_routes, rt_next, next) {
338		if ((rt->rt_flags & IEEE80211_MESHRT_FLAGS_VALID) == 0 &&
339		    ticks - rt->rt_crtime >= ms->ms_ppath->mpp_inact)
340			mesh_rt_del(ms, rt);
341	}
342	MESH_RT_UNLOCK(ms);
343}
344
345#define	N(a)	(sizeof(a) / sizeof(a[0]))
346int
347ieee80211_mesh_register_proto_path(const struct ieee80211_mesh_proto_path *mpp)
348{
349	int i, firstempty = -1;
350
351	for (i = 0; i < N(mesh_proto_paths); i++) {
352		if (strncmp(mpp->mpp_descr, mesh_proto_paths[i].mpp_descr,
353		    IEEE80211_MESH_PROTO_DSZ) == 0)
354			return EEXIST;
355		if (!mesh_proto_paths[i].mpp_active && firstempty == -1)
356			firstempty = i;
357	}
358	if (firstempty < 0)
359		return ENOSPC;
360	memcpy(&mesh_proto_paths[firstempty], mpp, sizeof(*mpp));
361	mesh_proto_paths[firstempty].mpp_active = 1;
362	return 0;
363}
364
365int
366ieee80211_mesh_register_proto_metric(const struct
367    ieee80211_mesh_proto_metric *mpm)
368{
369	int i, firstempty = -1;
370
371	for (i = 0; i < N(mesh_proto_metrics); i++) {
372		if (strncmp(mpm->mpm_descr, mesh_proto_metrics[i].mpm_descr,
373		    IEEE80211_MESH_PROTO_DSZ) == 0)
374			return EEXIST;
375		if (!mesh_proto_metrics[i].mpm_active && firstempty == -1)
376			firstempty = i;
377	}
378	if (firstempty < 0)
379		return ENOSPC;
380	memcpy(&mesh_proto_metrics[firstempty], mpm, sizeof(*mpm));
381	mesh_proto_metrics[firstempty].mpm_active = 1;
382	return 0;
383}
384
385static int
386mesh_select_proto_path(struct ieee80211vap *vap, const char *name)
387{
388	struct ieee80211_mesh_state *ms = vap->iv_mesh;
389	int i;
390
391	for (i = 0; i < N(mesh_proto_paths); i++) {
392		if (strcasecmp(mesh_proto_paths[i].mpp_descr, name) == 0) {
393			ms->ms_ppath = &mesh_proto_paths[i];
394			return 0;
395		}
396	}
397	return ENOENT;
398}
399
400static int
401mesh_select_proto_metric(struct ieee80211vap *vap, const char *name)
402{
403	struct ieee80211_mesh_state *ms = vap->iv_mesh;
404	int i;
405
406	for (i = 0; i < N(mesh_proto_metrics); i++) {
407		if (strcasecmp(mesh_proto_metrics[i].mpm_descr, name) == 0) {
408			ms->ms_pmetric = &mesh_proto_metrics[i];
409			return 0;
410		}
411	}
412	return ENOENT;
413}
414#undef	N
415
416static void
417ieee80211_mesh_init(void)
418{
419
420	memset(mesh_proto_paths, 0, sizeof(mesh_proto_paths));
421	memset(mesh_proto_metrics, 0, sizeof(mesh_proto_metrics));
422
423	/*
424	 * Setup mesh parameters that depends on the clock frequency.
425	 */
426	ieee80211_mesh_retrytimeout = msecs_to_ticks(40);
427	ieee80211_mesh_holdingtimeout = msecs_to_ticks(40);
428	ieee80211_mesh_confirmtimeout = msecs_to_ticks(40);
429
430	/*
431	 * Register action frame handlers.
432	 */
433	ieee80211_recv_action_register(IEEE80211_ACTION_CAT_SELF_PROT,
434	    IEEE80211_ACTION_MESHPEERING_OPEN,
435	    mesh_recv_action_meshpeering_open);
436	ieee80211_recv_action_register(IEEE80211_ACTION_CAT_SELF_PROT,
437	    IEEE80211_ACTION_MESHPEERING_CONFIRM,
438	    mesh_recv_action_meshpeering_confirm);
439	ieee80211_recv_action_register(IEEE80211_ACTION_CAT_SELF_PROT,
440	    IEEE80211_ACTION_MESHPEERING_CLOSE,
441	    mesh_recv_action_meshpeering_close);
442	ieee80211_recv_action_register(IEEE80211_ACTION_CAT_MESH,
443	    IEEE80211_ACTION_MESH_LMETRIC, mesh_recv_action_meshlmetric);
444
445	ieee80211_send_action_register(IEEE80211_ACTION_CAT_SELF_PROT,
446	    IEEE80211_ACTION_MESHPEERING_OPEN,
447	    mesh_send_action_meshpeering_open);
448	ieee80211_send_action_register(IEEE80211_ACTION_CAT_SELF_PROT,
449	    IEEE80211_ACTION_MESHPEERING_CONFIRM,
450	    mesh_send_action_meshpeering_confirm);
451	ieee80211_send_action_register(IEEE80211_ACTION_CAT_SELF_PROT,
452	    IEEE80211_ACTION_MESHPEERING_CLOSE,
453	    mesh_send_action_meshpeering_close);
454	ieee80211_send_action_register(IEEE80211_ACTION_CAT_MESH,
455	    IEEE80211_ACTION_MESH_LMETRIC,
456	    mesh_send_action_meshlmetric);
457
458	/*
459	 * Register Airtime Link Metric.
460	 */
461	ieee80211_mesh_register_proto_metric(&mesh_metric_airtime);
462
463}
464SYSINIT(wlan_mesh, SI_SUB_DRIVERS, SI_ORDER_FIRST, ieee80211_mesh_init, NULL);
465
466void
467ieee80211_mesh_attach(struct ieee80211com *ic)
468{
469	ic->ic_vattach[IEEE80211_M_MBSS] = mesh_vattach;
470}
471
472void
473ieee80211_mesh_detach(struct ieee80211com *ic)
474{
475}
476
477static void
478mesh_vdetach_peers(void *arg, struct ieee80211_node *ni)
479{
480	struct ieee80211com *ic = ni->ni_ic;
481	uint16_t args[3];
482
483	if (ni->ni_mlstate == IEEE80211_NODE_MESH_ESTABLISHED) {
484		args[0] = ni->ni_mlpid;
485		args[1] = ni->ni_mllid;
486		args[2] = IEEE80211_REASON_PEER_LINK_CANCELED;
487		ieee80211_send_action(ni,
488		    IEEE80211_ACTION_CAT_SELF_PROT,
489		    IEEE80211_ACTION_MESHPEERING_CLOSE,
490		    args);
491	}
492	callout_drain(&ni->ni_mltimer);
493	/* XXX belongs in hwmp */
494	ieee80211_ageq_drain_node(&ic->ic_stageq,
495	   (void *)(uintptr_t) ieee80211_mac_hash(ic, ni->ni_macaddr));
496}
497
498static void
499mesh_vdetach(struct ieee80211vap *vap)
500{
501	struct ieee80211_mesh_state *ms = vap->iv_mesh;
502
503	callout_drain(&ms->ms_cleantimer);
504	ieee80211_iterate_nodes(&vap->iv_ic->ic_sta, mesh_vdetach_peers,
505	    NULL);
506	ieee80211_mesh_rt_flush(vap);
507	mtx_destroy(&ms->ms_rt_lock);
508	ms->ms_ppath->mpp_vdetach(vap);
509	free(vap->iv_mesh, M_80211_VAP);
510	vap->iv_mesh = NULL;
511}
512
513static void
514mesh_vattach(struct ieee80211vap *vap)
515{
516	struct ieee80211_mesh_state *ms;
517	vap->iv_newstate = mesh_newstate;
518	vap->iv_input = mesh_input;
519	vap->iv_opdetach = mesh_vdetach;
520	vap->iv_recv_mgmt = mesh_recv_mgmt;
521	vap->iv_recv_ctl = mesh_recv_ctl;
522	ms = malloc(sizeof(struct ieee80211_mesh_state), M_80211_VAP,
523	    M_NOWAIT | M_ZERO);
524	if (ms == NULL) {
525		printf("%s: couldn't alloc MBSS state\n", __func__);
526		return;
527	}
528	vap->iv_mesh = ms;
529	ms->ms_seq = 0;
530	ms->ms_flags = (IEEE80211_MESHFLAGS_AP | IEEE80211_MESHFLAGS_FWD);
531	ms->ms_ttl = IEEE80211_MESH_DEFAULT_TTL;
532	TAILQ_INIT(&ms->ms_routes);
533	mtx_init(&ms->ms_rt_lock, "MBSS", "802.11s routing table", MTX_DEF);
534	callout_init(&ms->ms_cleantimer, CALLOUT_MPSAFE);
535	mesh_select_proto_metric(vap, "AIRTIME");
536	KASSERT(ms->ms_pmetric, ("ms_pmetric == NULL"));
537	mesh_select_proto_path(vap, "HWMP");
538	KASSERT(ms->ms_ppath, ("ms_ppath == NULL"));
539	ms->ms_ppath->mpp_vattach(vap);
540}
541
542/*
543 * IEEE80211_M_MBSS vap state machine handler.
544 */
545static int
546mesh_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
547{
548	struct ieee80211_mesh_state *ms = vap->iv_mesh;
549	struct ieee80211com *ic = vap->iv_ic;
550	struct ieee80211_node *ni;
551	enum ieee80211_state ostate;
552
553	IEEE80211_LOCK_ASSERT(ic);
554
555	ostate = vap->iv_state;
556	IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE, "%s: %s -> %s (%d)\n",
557	    __func__, ieee80211_state_name[ostate],
558	    ieee80211_state_name[nstate], arg);
559	vap->iv_state = nstate;		/* state transition */
560	if (ostate != IEEE80211_S_SCAN)
561		ieee80211_cancel_scan(vap);	/* background scan */
562	ni = vap->iv_bss;			/* NB: no reference held */
563	if (nstate != IEEE80211_S_RUN && ostate == IEEE80211_S_RUN)
564		callout_drain(&ms->ms_cleantimer);
565	switch (nstate) {
566	case IEEE80211_S_INIT:
567		switch (ostate) {
568		case IEEE80211_S_SCAN:
569			ieee80211_cancel_scan(vap);
570			break;
571		case IEEE80211_S_CAC:
572			ieee80211_dfs_cac_stop(vap);
573			break;
574		case IEEE80211_S_RUN:
575			ieee80211_iterate_nodes(&ic->ic_sta,
576			    mesh_vdetach_peers, NULL);
577			break;
578		default:
579			break;
580		}
581		if (ostate != IEEE80211_S_INIT) {
582			/* NB: optimize INIT -> INIT case */
583			ieee80211_reset_bss(vap);
584			ieee80211_mesh_rt_flush(vap);
585		}
586		break;
587	case IEEE80211_S_SCAN:
588		switch (ostate) {
589		case IEEE80211_S_INIT:
590			if (vap->iv_des_chan != IEEE80211_CHAN_ANYC &&
591			    !IEEE80211_IS_CHAN_RADAR(vap->iv_des_chan) &&
592			    ms->ms_idlen != 0) {
593				/*
594				 * Already have a channel and a mesh ID; bypass
595				 * the scan and startup immediately.
596				 */
597				ieee80211_create_ibss(vap, vap->iv_des_chan);
598				break;
599			}
600			/*
601			 * Initiate a scan.  We can come here as a result
602			 * of an IEEE80211_IOC_SCAN_REQ too in which case
603			 * the vap will be marked with IEEE80211_FEXT_SCANREQ
604			 * and the scan request parameters will be present
605			 * in iv_scanreq.  Otherwise we do the default.
606			*/
607			if (vap->iv_flags_ext & IEEE80211_FEXT_SCANREQ) {
608				ieee80211_check_scan(vap,
609				    vap->iv_scanreq_flags,
610				    vap->iv_scanreq_duration,
611				    vap->iv_scanreq_mindwell,
612				    vap->iv_scanreq_maxdwell,
613				    vap->iv_scanreq_nssid, vap->iv_scanreq_ssid);
614				vap->iv_flags_ext &= ~IEEE80211_FEXT_SCANREQ;
615			} else
616				ieee80211_check_scan_current(vap);
617			break;
618		default:
619			break;
620		}
621		break;
622	case IEEE80211_S_CAC:
623		/*
624		 * Start CAC on a DFS channel.  We come here when starting
625		 * a bss on a DFS channel (see ieee80211_create_ibss).
626		 */
627		ieee80211_dfs_cac_start(vap);
628		break;
629	case IEEE80211_S_RUN:
630		switch (ostate) {
631		case IEEE80211_S_INIT:
632			/*
633			 * Already have a channel; bypass the
634			 * scan and startup immediately.
635			 * Note that ieee80211_create_ibss will call
636			 * back to do a RUN->RUN state change.
637			 */
638			ieee80211_create_ibss(vap,
639			    ieee80211_ht_adjust_channel(ic,
640				ic->ic_curchan, vap->iv_flags_ht));
641			/* NB: iv_bss is changed on return */
642			break;
643		case IEEE80211_S_CAC:
644			/*
645			 * NB: This is the normal state change when CAC
646			 * expires and no radar was detected; no need to
647			 * clear the CAC timer as it's already expired.
648			 */
649			/* fall thru... */
650		case IEEE80211_S_CSA:
651#if 0
652			/*
653			 * Shorten inactivity timer of associated stations
654			 * to weed out sta's that don't follow a CSA.
655			 */
656			ieee80211_iterate_nodes(&ic->ic_sta, sta_csa, vap);
657#endif
658			/*
659			 * Update bss node channel to reflect where
660			 * we landed after CSA.
661			 */
662			ieee80211_node_set_chan(vap->iv_bss,
663			    ieee80211_ht_adjust_channel(ic, ic->ic_curchan,
664				ieee80211_htchanflags(vap->iv_bss->ni_chan)));
665			/* XXX bypass debug msgs */
666			break;
667		case IEEE80211_S_SCAN:
668		case IEEE80211_S_RUN:
669#ifdef IEEE80211_DEBUG
670			if (ieee80211_msg_debug(vap)) {
671				struct ieee80211_node *ni = vap->iv_bss;
672				ieee80211_note(vap,
673				    "synchronized with %s meshid ",
674				    ether_sprintf(ni->ni_meshid));
675				ieee80211_print_essid(ni->ni_meshid,
676				    ni->ni_meshidlen);
677				/* XXX MCS/HT */
678				printf(" channel %d\n",
679				    ieee80211_chan2ieee(ic, ic->ic_curchan));
680			}
681#endif
682			break;
683		default:
684			break;
685		}
686		ieee80211_node_authorize(vap->iv_bss);
687		callout_reset(&ms->ms_cleantimer, ms->ms_ppath->mpp_inact,
688                    mesh_rt_cleanup_cb, vap);
689		break;
690	default:
691		break;
692	}
693	/* NB: ostate not nstate */
694	ms->ms_ppath->mpp_newstate(vap, ostate, arg);
695	return 0;
696}
697
698static void
699mesh_rt_cleanup_cb(void *arg)
700{
701	struct ieee80211vap *vap = arg;
702	struct ieee80211_mesh_state *ms = vap->iv_mesh;
703
704	mesh_rt_flush_invalid(vap);
705	callout_reset(&ms->ms_cleantimer, ms->ms_ppath->mpp_inact,
706	    mesh_rt_cleanup_cb, vap);
707}
708
709
710/*
711 * Helper function to note the Mesh Peer Link FSM change.
712 */
713static void
714mesh_linkchange(struct ieee80211_node *ni, enum ieee80211_mesh_mlstate state)
715{
716	struct ieee80211vap *vap = ni->ni_vap;
717	struct ieee80211_mesh_state *ms = vap->iv_mesh;
718#ifdef IEEE80211_DEBUG
719	static const char *meshlinkstates[] = {
720		[IEEE80211_NODE_MESH_IDLE]		= "IDLE",
721		[IEEE80211_NODE_MESH_OPENSNT]		= "OPEN SENT",
722		[IEEE80211_NODE_MESH_OPENRCV]		= "OPEN RECEIVED",
723		[IEEE80211_NODE_MESH_CONFIRMRCV]	= "CONFIRM RECEIVED",
724		[IEEE80211_NODE_MESH_ESTABLISHED]	= "ESTABLISHED",
725		[IEEE80211_NODE_MESH_HOLDING]		= "HOLDING"
726	};
727#endif
728	IEEE80211_NOTE(vap, IEEE80211_MSG_MESH,
729	    ni, "peer link: %s -> %s",
730	    meshlinkstates[ni->ni_mlstate], meshlinkstates[state]);
731
732	/* track neighbor count */
733	if (state == IEEE80211_NODE_MESH_ESTABLISHED &&
734	    ni->ni_mlstate != IEEE80211_NODE_MESH_ESTABLISHED) {
735		KASSERT(ms->ms_neighbors < 65535, ("neighbor count overflow"));
736		ms->ms_neighbors++;
737		ieee80211_beacon_notify(vap, IEEE80211_BEACON_MESHCONF);
738	} else if (ni->ni_mlstate == IEEE80211_NODE_MESH_ESTABLISHED &&
739	    state != IEEE80211_NODE_MESH_ESTABLISHED) {
740		KASSERT(ms->ms_neighbors > 0, ("neighbor count 0"));
741		ms->ms_neighbors--;
742		ieee80211_beacon_notify(vap, IEEE80211_BEACON_MESHCONF);
743	}
744	ni->ni_mlstate = state;
745	switch (state) {
746	case IEEE80211_NODE_MESH_HOLDING:
747		ms->ms_ppath->mpp_peerdown(ni);
748		break;
749	case IEEE80211_NODE_MESH_ESTABLISHED:
750		ieee80211_mesh_discover(vap, ni->ni_macaddr, NULL);
751		break;
752	default:
753		break;
754	}
755}
756
757/*
758 * Helper function to generate a unique local ID required for mesh
759 * peer establishment.
760 */
761static void
762mesh_checkid(void *arg, struct ieee80211_node *ni)
763{
764	uint16_t *r = arg;
765
766	if (*r == ni->ni_mllid)
767		*(uint16_t *)arg = 0;
768}
769
770static uint32_t
771mesh_generateid(struct ieee80211vap *vap)
772{
773	int maxiter = 4;
774	uint16_t r;
775
776	do {
777		get_random_bytes(&r, 2);
778		ieee80211_iterate_nodes(&vap->iv_ic->ic_sta, mesh_checkid, &r);
779		maxiter--;
780	} while (r == 0 && maxiter > 0);
781	return r;
782}
783
784/*
785 * Verifies if we already received this packet by checking its
786 * sequence number.
787 * Returns 0 if the frame is to be accepted, 1 otherwise.
788 */
789static int
790mesh_checkpseq(struct ieee80211vap *vap,
791    const uint8_t source[IEEE80211_ADDR_LEN], uint32_t seq)
792{
793	struct ieee80211_mesh_route *rt;
794
795	rt = ieee80211_mesh_rt_find(vap, source);
796	if (rt == NULL) {
797		rt = ieee80211_mesh_rt_add(vap, source);
798		if (rt == NULL) {
799			IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH, source,
800			    "%s", "add mcast route failed");
801			vap->iv_stats.is_mesh_rtaddfailed++;
802			return 1;
803		}
804		IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH, source,
805		    "add mcast route, mesh seqno %d", seq);
806		rt->rt_lastmseq = seq;
807		return 0;
808	}
809	if (IEEE80211_MESH_SEQ_GEQ(rt->rt_lastmseq, seq)) {
810		return 1;
811	} else {
812		rt->rt_lastmseq = seq;
813		return 0;
814	}
815}
816
817/*
818 * Iterate the routing table and locate the next hop.
819 */
820static struct ieee80211_node *
821mesh_find_txnode(struct ieee80211vap *vap,
822    const uint8_t dest[IEEE80211_ADDR_LEN])
823{
824	struct ieee80211_mesh_route *rt;
825
826	rt = ieee80211_mesh_rt_find(vap, dest);
827	if (rt == NULL)
828		return NULL;
829	if ((rt->rt_flags & IEEE80211_MESHRT_FLAGS_VALID) == 0 ||
830	    (rt->rt_flags & IEEE80211_MESHRT_FLAGS_PROXY)) {
831		IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH, dest,
832		    "%s: !valid or proxy, flags 0x%x", __func__, rt->rt_flags);
833		/* XXX stat */
834		return NULL;
835	}
836	return ieee80211_find_txnode(vap, rt->rt_nexthop);
837}
838
839/*
840 * Forward the specified frame.
841 * Decrement the TTL and set TA to our MAC address.
842 */
843static void
844mesh_forward(struct ieee80211vap *vap, struct mbuf *m,
845    const struct ieee80211_meshcntl *mc)
846{
847	struct ieee80211com *ic = vap->iv_ic;
848	struct ieee80211_mesh_state *ms = vap->iv_mesh;
849	struct ifnet *ifp = vap->iv_ifp;
850	struct ifnet *parent = ic->ic_ifp;
851	const struct ieee80211_frame *wh =
852	    mtod(m, const struct ieee80211_frame *);
853	struct mbuf *mcopy;
854	struct ieee80211_meshcntl *mccopy;
855	struct ieee80211_frame *whcopy;
856	struct ieee80211_node *ni;
857	int err;
858
859	if (mc->mc_ttl == 0) {
860		IEEE80211_NOTE_FRAME(vap, IEEE80211_MSG_MESH, wh,
861		    "%s", "frame not fwd'd, ttl 0");
862		vap->iv_stats.is_mesh_fwd_ttl++;
863		return;
864	}
865	if (!(ms->ms_flags & IEEE80211_MESHFLAGS_FWD)) {
866		IEEE80211_NOTE_FRAME(vap, IEEE80211_MSG_MESH, wh,
867		    "%s", "frame not fwd'd, fwding disabled");
868		vap->iv_stats.is_mesh_fwd_disabled++;
869		return;
870	}
871	mcopy = m_dup(m, M_DONTWAIT);
872	if (mcopy == NULL) {
873		IEEE80211_NOTE_FRAME(vap, IEEE80211_MSG_MESH, wh,
874		    "%s", "frame not fwd'd, cannot dup");
875		vap->iv_stats.is_mesh_fwd_nobuf++;
876		ifp->if_oerrors++;
877		return;
878	}
879	mcopy = m_pullup(mcopy, ieee80211_hdrspace(ic, wh) +
880	    sizeof(struct ieee80211_meshcntl));
881	if (mcopy == NULL) {
882		IEEE80211_NOTE_FRAME(vap, IEEE80211_MSG_MESH, wh,
883		    "%s", "frame not fwd'd, too short");
884		vap->iv_stats.is_mesh_fwd_tooshort++;
885		ifp->if_oerrors++;
886		m_freem(mcopy);
887		return;
888	}
889	whcopy = mtod(mcopy, struct ieee80211_frame *);
890	mccopy = (struct ieee80211_meshcntl *)
891	    (mtod(mcopy, uint8_t *) + ieee80211_hdrspace(ic, wh));
892	/* XXX clear other bits? */
893	whcopy->i_fc[1] &= ~IEEE80211_FC1_RETRY;
894	IEEE80211_ADDR_COPY(whcopy->i_addr2, vap->iv_myaddr);
895	if (IEEE80211_IS_MULTICAST(wh->i_addr1)) {
896		ni = ieee80211_ref_node(vap->iv_bss);
897		mcopy->m_flags |= M_MCAST;
898	} else {
899		ni = mesh_find_txnode(vap, whcopy->i_addr3);
900		if (ni == NULL) {
901			IEEE80211_NOTE_FRAME(vap, IEEE80211_MSG_MESH, wh,
902			    "%s", "frame not fwd'd, no path");
903			vap->iv_stats.is_mesh_fwd_nopath++;
904			m_freem(mcopy);
905			return;
906		}
907		IEEE80211_ADDR_COPY(whcopy->i_addr1, ni->ni_macaddr);
908	}
909	KASSERT(mccopy->mc_ttl > 0, ("%s called with wrong ttl", __func__));
910	mccopy->mc_ttl--;
911
912	/* XXX calculate priority so drivers can find the tx queue */
913	M_WME_SETAC(mcopy, WME_AC_BE);
914
915	/* XXX do we know m_nextpkt is NULL? */
916	mcopy->m_pkthdr.rcvif = (void *) ni;
917	err = parent->if_transmit(parent, mcopy);
918	if (err != 0) {
919		/* NB: IFQ_HANDOFF reclaims mbuf */
920		ieee80211_free_node(ni);
921	} else {
922		ifp->if_opackets++;
923	}
924}
925
926static struct mbuf *
927mesh_decap(struct ieee80211vap *vap, struct mbuf *m, int hdrlen, int meshdrlen)
928{
929#define	WHDIR(wh) ((wh)->i_fc[1] & IEEE80211_FC1_DIR_MASK)
930	uint8_t b[sizeof(struct ieee80211_qosframe_addr4) +
931		  sizeof(struct ieee80211_meshcntl_ae11)];
932	const struct ieee80211_qosframe_addr4 *wh;
933	const struct ieee80211_meshcntl_ae10 *mc;
934	struct ether_header *eh;
935	struct llc *llc;
936	int ae;
937
938	if (m->m_len < hdrlen + sizeof(*llc) &&
939	    (m = m_pullup(m, hdrlen + sizeof(*llc))) == NULL) {
940		IEEE80211_DPRINTF(vap, IEEE80211_MSG_ANY,
941		    "discard data frame: %s", "m_pullup failed");
942		vap->iv_stats.is_rx_tooshort++;
943		return NULL;
944	}
945	memcpy(b, mtod(m, caddr_t), hdrlen);
946	wh = (const struct ieee80211_qosframe_addr4 *)&b[0];
947	mc = (const struct ieee80211_meshcntl_ae10 *)&b[hdrlen - meshdrlen];
948	KASSERT(WHDIR(wh) == IEEE80211_FC1_DIR_FROMDS ||
949		WHDIR(wh) == IEEE80211_FC1_DIR_DSTODS,
950	    ("bogus dir, fc 0x%x:0x%x", wh->i_fc[0], wh->i_fc[1]));
951
952	llc = (struct llc *)(mtod(m, caddr_t) + hdrlen);
953	if (llc->llc_dsap == LLC_SNAP_LSAP && llc->llc_ssap == LLC_SNAP_LSAP &&
954	    llc->llc_control == LLC_UI && llc->llc_snap.org_code[0] == 0 &&
955	    llc->llc_snap.org_code[1] == 0 && llc->llc_snap.org_code[2] == 0 &&
956	    /* NB: preserve AppleTalk frames that have a native SNAP hdr */
957	    !(llc->llc_snap.ether_type == htons(ETHERTYPE_AARP) ||
958	      llc->llc_snap.ether_type == htons(ETHERTYPE_IPX))) {
959		m_adj(m, hdrlen + sizeof(struct llc) - sizeof(*eh));
960		llc = NULL;
961	} else {
962		m_adj(m, hdrlen - sizeof(*eh));
963	}
964	eh = mtod(m, struct ether_header *);
965	ae = mc->mc_flags & 3;
966	if (WHDIR(wh) == IEEE80211_FC1_DIR_FROMDS) {
967		IEEE80211_ADDR_COPY(eh->ether_dhost, wh->i_addr1);
968		if (ae == 0) {
969			IEEE80211_ADDR_COPY(eh->ether_shost, wh->i_addr3);
970		} else if (ae == 1) {
971			IEEE80211_ADDR_COPY(eh->ether_shost, mc->mc_addr4);
972		} else {
973			IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
974			    (const struct ieee80211_frame *)wh, NULL,
975			    "bad AE %d", ae);
976			vap->iv_stats.is_mesh_badae++;
977			m_freem(m);
978			return NULL;
979		}
980	} else {
981		if (ae == 0) {
982			IEEE80211_ADDR_COPY(eh->ether_dhost, wh->i_addr3);
983			IEEE80211_ADDR_COPY(eh->ether_shost, wh->i_addr4);
984		} else if (ae == 2) {
985			IEEE80211_ADDR_COPY(eh->ether_dhost, mc->mc_addr4);
986			IEEE80211_ADDR_COPY(eh->ether_shost, mc->mc_addr5);
987		} else {
988			IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
989			    (const struct ieee80211_frame *)wh, NULL,
990			    "bad AE %d", ae);
991			vap->iv_stats.is_mesh_badae++;
992			m_freem(m);
993			return NULL;
994		}
995	}
996#ifdef ALIGNED_POINTER
997	if (!ALIGNED_POINTER(mtod(m, caddr_t) + sizeof(*eh), uint32_t)) {
998		m = ieee80211_realign(vap, m, sizeof(*eh));
999		if (m == NULL)
1000			return NULL;
1001	}
1002#endif /* ALIGNED_POINTER */
1003	if (llc != NULL) {
1004		eh = mtod(m, struct ether_header *);
1005		eh->ether_type = htons(m->m_pkthdr.len - sizeof(*eh));
1006	}
1007	return m;
1008#undef WDIR
1009}
1010
1011/*
1012 * Return non-zero if the unicast mesh data frame should be processed
1013 * locally.  Frames that are not proxy'd have our address, otherwise
1014 * we need to consult the routing table to look for a proxy entry.
1015 */
1016static __inline int
1017mesh_isucastforme(struct ieee80211vap *vap, const struct ieee80211_frame *wh,
1018    const struct ieee80211_meshcntl *mc)
1019{
1020	int ae = mc->mc_flags & 3;
1021
1022	KASSERT((wh->i_fc[1] & IEEE80211_FC1_DIR_MASK) == IEEE80211_FC1_DIR_DSTODS,
1023	    ("bad dir 0x%x:0x%x", wh->i_fc[0], wh->i_fc[1]));
1024	KASSERT(ae == 0 || ae == 2, ("bad AE %d", ae));
1025	if (ae == 2) {				/* ucast w/ proxy */
1026		const struct ieee80211_meshcntl_ae10 *mc10 =
1027		    (const struct ieee80211_meshcntl_ae10 *) mc;
1028		struct ieee80211_mesh_route *rt =
1029		    ieee80211_mesh_rt_find(vap, mc10->mc_addr4);
1030		/* check for proxy route to ourself */
1031		return (rt != NULL &&
1032		    (rt->rt_flags & IEEE80211_MESHRT_FLAGS_PROXY));
1033	} else					/* ucast w/o proxy */
1034		return IEEE80211_ADDR_EQ(wh->i_addr3, vap->iv_myaddr);
1035}
1036
1037static int
1038mesh_input(struct ieee80211_node *ni, struct mbuf *m, int rssi, int nf)
1039{
1040#define	HAS_SEQ(type)	((type & 0x4) == 0)
1041	struct ieee80211vap *vap = ni->ni_vap;
1042	struct ieee80211com *ic = ni->ni_ic;
1043	struct ifnet *ifp = vap->iv_ifp;
1044	struct ieee80211_frame *wh;
1045	const struct ieee80211_meshcntl *mc;
1046	int hdrspace, meshdrlen, need_tap;
1047	uint8_t dir, type, subtype;
1048	uint32_t seq;
1049	uint8_t *addr, qos[2];
1050	ieee80211_seq rxseq;
1051
1052	KASSERT(ni != NULL, ("null node"));
1053	ni->ni_inact = ni->ni_inact_reload;
1054
1055	need_tap = 1;			/* mbuf need to be tapped. */
1056	type = -1;			/* undefined */
1057
1058	if (m->m_pkthdr.len < sizeof(struct ieee80211_frame_min)) {
1059		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
1060		    ni->ni_macaddr, NULL,
1061		    "too short (1): len %u", m->m_pkthdr.len);
1062		vap->iv_stats.is_rx_tooshort++;
1063		goto out;
1064	}
1065	/*
1066	 * Bit of a cheat here, we use a pointer for a 3-address
1067	 * frame format but don't reference fields past outside
1068	 * ieee80211_frame_min w/o first validating the data is
1069	 * present.
1070	*/
1071	wh = mtod(m, struct ieee80211_frame *);
1072
1073	if ((wh->i_fc[0] & IEEE80211_FC0_VERSION_MASK) !=
1074	    IEEE80211_FC0_VERSION_0) {
1075		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
1076		    ni->ni_macaddr, NULL, "wrong version %x", wh->i_fc[0]);
1077		vap->iv_stats.is_rx_badversion++;
1078		goto err;
1079	}
1080	dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK;
1081	type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
1082	subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
1083	if ((ic->ic_flags & IEEE80211_F_SCAN) == 0) {
1084		IEEE80211_RSSI_LPF(ni->ni_avgrssi, rssi);
1085		ni->ni_noise = nf;
1086		if (HAS_SEQ(type)) {
1087			uint8_t tid = ieee80211_gettid(wh);
1088
1089			if (IEEE80211_QOS_HAS_SEQ(wh) &&
1090			    TID_TO_WME_AC(tid) >= WME_AC_VI)
1091				ic->ic_wme.wme_hipri_traffic++;
1092			rxseq = le16toh(*(uint16_t *)wh->i_seq);
1093			if (! ieee80211_check_rxseq(ni, wh)) {
1094				/* duplicate, discard */
1095				IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
1096				    wh->i_addr1, "duplicate",
1097				    "seqno <%u,%u> fragno <%u,%u> tid %u",
1098				    rxseq >> IEEE80211_SEQ_SEQ_SHIFT,
1099				    ni->ni_rxseqs[tid] >>
1100				    IEEE80211_SEQ_SEQ_SHIFT,
1101				    rxseq & IEEE80211_SEQ_FRAG_MASK,
1102				    ni->ni_rxseqs[tid] &
1103				    IEEE80211_SEQ_FRAG_MASK,
1104				    tid);
1105				vap->iv_stats.is_rx_dup++;
1106				IEEE80211_NODE_STAT(ni, rx_dup);
1107				goto out;
1108			}
1109			ni->ni_rxseqs[tid] = rxseq;
1110		}
1111	}
1112#ifdef IEEE80211_DEBUG
1113	/*
1114	 * It's easier, but too expensive, to simulate different mesh
1115	 * topologies by consulting the ACL policy very early, so do this
1116	 * only under DEBUG.
1117	 *
1118	 * NB: this check is also done upon peering link initiation.
1119	 */
1120	if (vap->iv_acl != NULL && !vap->iv_acl->iac_check(vap, wh)) {
1121		IEEE80211_DISCARD(vap, IEEE80211_MSG_ACL,
1122		    wh, NULL, "%s", "disallowed by ACL");
1123		vap->iv_stats.is_rx_acl++;
1124		goto out;
1125	}
1126#endif
1127	switch (type) {
1128	case IEEE80211_FC0_TYPE_DATA:
1129		if (ni == vap->iv_bss)
1130			goto out;
1131		if (ni->ni_mlstate != IEEE80211_NODE_MESH_ESTABLISHED) {
1132			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_MESH,
1133			    ni->ni_macaddr, NULL,
1134			    "peer link not yet established (%d)",
1135			    ni->ni_mlstate);
1136			vap->iv_stats.is_mesh_nolink++;
1137			goto out;
1138		}
1139		if (dir != IEEE80211_FC1_DIR_FROMDS &&
1140		    dir != IEEE80211_FC1_DIR_DSTODS) {
1141			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
1142			    wh, "data", "incorrect dir 0x%x", dir);
1143			vap->iv_stats.is_rx_wrongdir++;
1144			goto err;
1145		}
1146
1147		/* All Mesh data frames are QoS subtype */
1148		if (!HAS_SEQ(type)) {
1149			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
1150			    wh, "data", "incorrect subtype 0x%x", subtype);
1151			vap->iv_stats.is_rx_badsubtype++;
1152			goto err;
1153		}
1154
1155		/*
1156		 * Next up, any fragmentation.
1157		 * XXX: we defrag before we even try to forward,
1158		 * Mesh Control field is not present in sub-sequent
1159		 * fragmented frames. This is in contrast to Draft 4.0.
1160		 */
1161		hdrspace = ieee80211_hdrspace(ic, wh);
1162		if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1163			m = ieee80211_defrag(ni, m, hdrspace);
1164			if (m == NULL) {
1165				/* Fragment dropped or frame not complete yet */
1166				goto out;
1167			}
1168		}
1169		wh = mtod(m, struct ieee80211_frame *); /* NB: after defrag */
1170
1171		/*
1172		 * Now we have a complete Mesh Data frame.
1173		 */
1174
1175		/*
1176		 * Only fromDStoDS data frames use 4 address qos frames
1177		 * as specified in amendment. Otherwise addr4 is located
1178		 * in the Mesh Control field and a 3 address qos frame
1179		 * is used.
1180		 */
1181		if (IEEE80211_IS_DSTODS(wh))
1182			*(uint16_t *)qos = *(uint16_t *)
1183			    ((struct ieee80211_qosframe_addr4 *)wh)->i_qos;
1184		else
1185			*(uint16_t *)qos = *(uint16_t *)
1186			    ((struct ieee80211_qosframe *)wh)->i_qos;
1187
1188		/*
1189		 * NB: The mesh STA sets the Mesh Control Present
1190		 * subfield to 1 in the Mesh Data frame containing
1191		 * an unfragmented MSDU, an A-MSDU, or the first
1192		 * fragment of an MSDU.
1193		 * After defrag it should always be present.
1194		 */
1195		if (!(qos[1] & IEEE80211_QOS_MC)) {
1196			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_MESH,
1197			    ni->ni_macaddr, NULL,
1198			    "%s", "Mesh control field not present");
1199			vap->iv_stats.is_rx_elem_missing++; /* XXX: kinda */
1200			goto err;
1201		}
1202
1203		/* pull up enough to get to the mesh control */
1204		if (m->m_len < hdrspace + sizeof(struct ieee80211_meshcntl) &&
1205		    (m = m_pullup(m, hdrspace +
1206		        sizeof(struct ieee80211_meshcntl))) == NULL) {
1207			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
1208			    ni->ni_macaddr, NULL,
1209			    "data too short: expecting %u", hdrspace);
1210			vap->iv_stats.is_rx_tooshort++;
1211			goto out;		/* XXX */
1212		}
1213		/*
1214		 * Now calculate the full extent of the headers. Note
1215		 * mesh_decap will pull up anything we didn't get
1216		 * above when it strips the 802.11 headers.
1217		 */
1218		mc = (const struct ieee80211_meshcntl *)
1219		    (mtod(m, const uint8_t *) + hdrspace);
1220		meshdrlen = sizeof(struct ieee80211_meshcntl) +
1221		    (mc->mc_flags & 3) * IEEE80211_ADDR_LEN;
1222		hdrspace += meshdrlen;
1223		seq = LE_READ_4(mc->mc_seq);
1224		if (IEEE80211_IS_MULTICAST(wh->i_addr1))
1225			addr = wh->i_addr3;
1226		else
1227			addr = ((struct ieee80211_qosframe_addr4 *)wh)->i_addr4;
1228		if (IEEE80211_ADDR_EQ(vap->iv_myaddr, addr)) {
1229			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
1230			    addr, "data", "%s", "not to me");
1231			vap->iv_stats.is_rx_wrongbss++;	/* XXX kinda */
1232			goto out;
1233		}
1234		if (mesh_checkpseq(vap, addr, seq) != 0) {
1235			vap->iv_stats.is_rx_dup++;
1236			goto out;
1237		}
1238
1239		/*
1240		 * Potentially forward packet.  See table s36 (p140)
1241		 * for the rules.  XXX tap fwd'd packets not for us?
1242		 */
1243		if (dir == IEEE80211_FC1_DIR_FROMDS ||
1244		    !mesh_isucastforme(vap, wh, mc)) {
1245			mesh_forward(vap, m, mc);
1246			if (dir == IEEE80211_FC1_DIR_DSTODS)
1247				goto out;
1248			/* NB: fall thru to deliver mcast frames locally */
1249		}
1250
1251		if (ieee80211_radiotap_active_vap(vap))
1252			ieee80211_radiotap_rx(vap, m);
1253		need_tap = 0;
1254
1255		/*
1256		 * Finally, strip the 802.11 header.
1257		 */
1258		m = mesh_decap(vap, m, hdrspace, meshdrlen);
1259		if (m == NULL) {
1260			/* XXX mask bit to check for both */
1261			/* don't count Null data frames as errors */
1262			if (subtype == IEEE80211_FC0_SUBTYPE_NODATA ||
1263			    subtype == IEEE80211_FC0_SUBTYPE_QOS_NULL)
1264				goto out;
1265			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
1266			    ni->ni_macaddr, "data", "%s", "decap error");
1267			vap->iv_stats.is_rx_decap++;
1268			IEEE80211_NODE_STAT(ni, rx_decap);
1269			goto err;
1270		}
1271		if (qos[0] & IEEE80211_QOS_AMSDU) {
1272			m = ieee80211_decap_amsdu(ni, m);
1273			if (m == NULL)
1274				return IEEE80211_FC0_TYPE_DATA;
1275		}
1276		ieee80211_deliver_data(vap, ni, m);
1277		return type;
1278	case IEEE80211_FC0_TYPE_MGT:
1279		vap->iv_stats.is_rx_mgmt++;
1280		IEEE80211_NODE_STAT(ni, rx_mgmt);
1281		if (dir != IEEE80211_FC1_DIR_NODS) {
1282			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
1283			    wh, "mgt", "incorrect dir 0x%x", dir);
1284			vap->iv_stats.is_rx_wrongdir++;
1285			goto err;
1286		}
1287		if (m->m_pkthdr.len < sizeof(struct ieee80211_frame)) {
1288			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
1289			    ni->ni_macaddr, "mgt", "too short: len %u",
1290			    m->m_pkthdr.len);
1291			vap->iv_stats.is_rx_tooshort++;
1292			goto out;
1293		}
1294#ifdef IEEE80211_DEBUG
1295		if ((ieee80211_msg_debug(vap) &&
1296		    (vap->iv_ic->ic_flags & IEEE80211_F_SCAN)) ||
1297		    ieee80211_msg_dumppkts(vap)) {
1298			if_printf(ifp, "received %s from %s rssi %d\n",
1299			    ieee80211_mgt_subtype_name[subtype >>
1300			    IEEE80211_FC0_SUBTYPE_SHIFT],
1301			    ether_sprintf(wh->i_addr2), rssi);
1302		}
1303#endif
1304		if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
1305			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
1306			    wh, NULL, "%s", "WEP set but not permitted");
1307			vap->iv_stats.is_rx_mgtdiscard++; /* XXX */
1308			goto out;
1309		}
1310		vap->iv_recv_mgmt(ni, m, subtype, rssi, nf);
1311		goto out;
1312	case IEEE80211_FC0_TYPE_CTL:
1313		vap->iv_stats.is_rx_ctl++;
1314		IEEE80211_NODE_STAT(ni, rx_ctrl);
1315		goto out;
1316	default:
1317		IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
1318		    wh, "bad", "frame type 0x%x", type);
1319		/* should not come here */
1320		break;
1321	}
1322err:
1323	ifp->if_ierrors++;
1324out:
1325	if (m != NULL) {
1326		if (need_tap && ieee80211_radiotap_active_vap(vap))
1327			ieee80211_radiotap_rx(vap, m);
1328		m_freem(m);
1329	}
1330	return type;
1331}
1332
1333static void
1334mesh_recv_mgmt(struct ieee80211_node *ni, struct mbuf *m0, int subtype,
1335    int rssi, int nf)
1336{
1337	struct ieee80211vap *vap = ni->ni_vap;
1338	struct ieee80211_mesh_state *ms = vap->iv_mesh;
1339	struct ieee80211com *ic = ni->ni_ic;
1340	struct ieee80211_frame *wh;
1341	uint8_t *frm, *efrm;
1342
1343	wh = mtod(m0, struct ieee80211_frame *);
1344	frm = (uint8_t *)&wh[1];
1345	efrm = mtod(m0, uint8_t *) + m0->m_len;
1346	switch (subtype) {
1347	case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
1348	case IEEE80211_FC0_SUBTYPE_BEACON:
1349	{
1350		struct ieee80211_scanparams scan;
1351		/*
1352		 * We process beacon/probe response
1353		 * frames to discover neighbors.
1354		 */
1355		if (ieee80211_parse_beacon(ni, m0, &scan) != 0)
1356			return;
1357		/*
1358		 * Count frame now that we know it's to be processed.
1359		 */
1360		if (subtype == IEEE80211_FC0_SUBTYPE_BEACON) {
1361			vap->iv_stats.is_rx_beacon++;	/* XXX remove */
1362			IEEE80211_NODE_STAT(ni, rx_beacons);
1363		} else
1364			IEEE80211_NODE_STAT(ni, rx_proberesp);
1365		/*
1366		 * If scanning, just pass information to the scan module.
1367		 */
1368		if (ic->ic_flags & IEEE80211_F_SCAN) {
1369			if (ic->ic_flags_ext & IEEE80211_FEXT_PROBECHAN) {
1370				/*
1371				 * Actively scanning a channel marked passive;
1372				 * send a probe request now that we know there
1373				 * is 802.11 traffic present.
1374				 *
1375				 * XXX check if the beacon we recv'd gives
1376				 * us what we need and suppress the probe req
1377				 */
1378				ieee80211_probe_curchan(vap, 1);
1379				ic->ic_flags_ext &= ~IEEE80211_FEXT_PROBECHAN;
1380			}
1381			ieee80211_add_scan(vap, &scan, wh,
1382			    subtype, rssi, nf);
1383			return;
1384		}
1385
1386		/* The rest of this code assumes we are running */
1387		if (vap->iv_state != IEEE80211_S_RUN)
1388			return;
1389		/*
1390		 * Ignore non-mesh STAs.
1391		 */
1392		if ((scan.capinfo &
1393		     (IEEE80211_CAPINFO_ESS|IEEE80211_CAPINFO_IBSS)) ||
1394		    scan.meshid == NULL || scan.meshconf == NULL) {
1395			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
1396			    wh, "beacon", "%s", "not a mesh sta");
1397			vap->iv_stats.is_mesh_wrongmesh++;
1398			return;
1399		}
1400		/*
1401		 * Ignore STAs for other mesh networks.
1402		 */
1403		if (memcmp(scan.meshid+2, ms->ms_id, ms->ms_idlen) != 0 ||
1404		    mesh_verify_meshconf(vap, scan.meshconf)) {
1405			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
1406			    wh, "beacon", "%s", "not for our mesh");
1407			vap->iv_stats.is_mesh_wrongmesh++;
1408			return;
1409		}
1410		/*
1411		 * Peer only based on the current ACL policy.
1412		 */
1413		if (vap->iv_acl != NULL && !vap->iv_acl->iac_check(vap, wh)) {
1414			IEEE80211_DISCARD(vap, IEEE80211_MSG_ACL,
1415			    wh, NULL, "%s", "disallowed by ACL");
1416			vap->iv_stats.is_rx_acl++;
1417			return;
1418		}
1419		/*
1420		 * Do neighbor discovery.
1421		 */
1422		if (!IEEE80211_ADDR_EQ(wh->i_addr2, ni->ni_macaddr)) {
1423			/*
1424			 * Create a new entry in the neighbor table.
1425			 */
1426			ni = ieee80211_add_neighbor(vap, wh, &scan);
1427		}
1428		/*
1429		 * Automatically peer with discovered nodes if possible.
1430		 * XXX backoff on repeated failure
1431		 */
1432		if (ni != vap->iv_bss &&
1433		    (ms->ms_flags & IEEE80211_MESHFLAGS_AP) &&
1434		    ni->ni_mlstate == IEEE80211_NODE_MESH_IDLE) {
1435			uint16_t args[1];
1436
1437			ni->ni_mlpid = mesh_generateid(vap);
1438			if (ni->ni_mlpid == 0)
1439				return;
1440			mesh_linkchange(ni, IEEE80211_NODE_MESH_OPENSNT);
1441			args[0] = ni->ni_mlpid;
1442			ieee80211_send_action(ni,
1443			    IEEE80211_ACTION_CAT_SELF_PROT,
1444			    IEEE80211_ACTION_MESHPEERING_OPEN, args);
1445			ni->ni_mlrcnt = 0;
1446			mesh_peer_timeout_setup(ni);
1447		}
1448		break;
1449	}
1450	case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
1451	{
1452		uint8_t *ssid, *meshid, *rates, *xrates;
1453		uint8_t *sfrm;
1454
1455		if (vap->iv_state != IEEE80211_S_RUN) {
1456			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
1457			    wh, NULL, "wrong state %s",
1458			    ieee80211_state_name[vap->iv_state]);
1459			vap->iv_stats.is_rx_mgtdiscard++;
1460			return;
1461		}
1462		if (IEEE80211_IS_MULTICAST(wh->i_addr2)) {
1463			/* frame must be directed */
1464			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
1465			    wh, NULL, "%s", "not unicast");
1466			vap->iv_stats.is_rx_mgtdiscard++;	/* XXX stat */
1467			return;
1468		}
1469		/*
1470		 * prreq frame format
1471		 *      [tlv] ssid
1472		 *      [tlv] supported rates
1473		 *      [tlv] extended supported rates
1474		 *	[tlv] mesh id
1475		 */
1476		ssid = meshid = rates = xrates = NULL;
1477		sfrm = frm;
1478		while (efrm - frm > 1) {
1479			IEEE80211_VERIFY_LENGTH(efrm - frm, frm[1] + 2, return);
1480			switch (*frm) {
1481			case IEEE80211_ELEMID_SSID:
1482				ssid = frm;
1483				break;
1484			case IEEE80211_ELEMID_RATES:
1485				rates = frm;
1486				break;
1487			case IEEE80211_ELEMID_XRATES:
1488				xrates = frm;
1489				break;
1490			case IEEE80211_ELEMID_MESHID:
1491				meshid = frm;
1492				break;
1493			}
1494			frm += frm[1] + 2;
1495		}
1496		IEEE80211_VERIFY_ELEMENT(ssid, IEEE80211_NWID_LEN, return);
1497		IEEE80211_VERIFY_ELEMENT(rates, IEEE80211_RATE_MAXSIZE, return);
1498		if (xrates != NULL)
1499			IEEE80211_VERIFY_ELEMENT(xrates,
1500			    IEEE80211_RATE_MAXSIZE - rates[1], return);
1501		if (meshid != NULL) {
1502			IEEE80211_VERIFY_ELEMENT(meshid,
1503			    IEEE80211_MESHID_LEN, return);
1504			/* NB: meshid, not ssid */
1505			IEEE80211_VERIFY_SSID(vap->iv_bss, meshid, return);
1506		}
1507
1508		/* XXX find a better class or define it's own */
1509		IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_INPUT, wh->i_addr2,
1510		    "%s", "recv probe req");
1511		/*
1512		 * Some legacy 11b clients cannot hack a complete
1513		 * probe response frame.  When the request includes
1514		 * only a bare-bones rate set, communicate this to
1515		 * the transmit side.
1516		 */
1517		ieee80211_send_proberesp(vap, wh->i_addr2, 0);
1518		break;
1519	}
1520
1521	case IEEE80211_FC0_SUBTYPE_ACTION:
1522	case IEEE80211_FC0_SUBTYPE_ACTION_NOACK:
1523		if (ni == vap->iv_bss) {
1524			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
1525			    wh, NULL, "%s", "unknown node");
1526			vap->iv_stats.is_rx_mgtdiscard++;
1527		} else if (!IEEE80211_ADDR_EQ(vap->iv_myaddr, wh->i_addr1) &&
1528		    !IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1529			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
1530			    wh, NULL, "%s", "not for us");
1531			vap->iv_stats.is_rx_mgtdiscard++;
1532		} else if (vap->iv_state != IEEE80211_S_RUN) {
1533			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
1534			    wh, NULL, "wrong state %s",
1535			    ieee80211_state_name[vap->iv_state]);
1536			vap->iv_stats.is_rx_mgtdiscard++;
1537		} else {
1538			if (ieee80211_parse_action(ni, m0) == 0)
1539				(void)ic->ic_recv_action(ni, wh, frm, efrm);
1540		}
1541		break;
1542
1543	case IEEE80211_FC0_SUBTYPE_ASSOC_REQ:
1544	case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
1545	case IEEE80211_FC0_SUBTYPE_REASSOC_REQ:
1546	case IEEE80211_FC0_SUBTYPE_REASSOC_RESP:
1547	case IEEE80211_FC0_SUBTYPE_ATIM:
1548	case IEEE80211_FC0_SUBTYPE_DISASSOC:
1549	case IEEE80211_FC0_SUBTYPE_AUTH:
1550	case IEEE80211_FC0_SUBTYPE_DEAUTH:
1551		IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
1552		    wh, NULL, "%s", "not handled");
1553		vap->iv_stats.is_rx_mgtdiscard++;
1554		break;
1555
1556	default:
1557		IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
1558		    wh, "mgt", "subtype 0x%x not handled", subtype);
1559		vap->iv_stats.is_rx_badsubtype++;
1560		break;
1561	}
1562}
1563
1564static void
1565mesh_recv_ctl(struct ieee80211_node *ni, struct mbuf *m, int subtype)
1566{
1567
1568	switch (subtype) {
1569	case IEEE80211_FC0_SUBTYPE_BAR:
1570		ieee80211_recv_bar(ni, m);
1571		break;
1572	}
1573}
1574
1575/*
1576 * Parse meshpeering action ie's for open+confirm frames
1577 */
1578static const struct ieee80211_meshpeer_ie *
1579mesh_parse_meshpeering_action(struct ieee80211_node *ni,
1580	const struct ieee80211_frame *wh,	/* XXX for VERIFY_LENGTH */
1581	const uint8_t *frm, const uint8_t *efrm,
1582	struct ieee80211_meshpeer_ie *mp, uint8_t subtype)
1583{
1584	struct ieee80211vap *vap = ni->ni_vap;
1585	const struct ieee80211_meshpeer_ie *mpie;
1586	const uint8_t *meshid, *meshconf, *meshpeer;
1587
1588	meshid = meshconf = meshpeer = NULL;
1589	while (efrm - frm > 1) {
1590		IEEE80211_VERIFY_LENGTH(efrm - frm, frm[1] + 2, return NULL);
1591		switch (*frm) {
1592		case IEEE80211_ELEMID_MESHID:
1593			meshid = frm;
1594			break;
1595		case IEEE80211_ELEMID_MESHCONF:
1596			meshconf = frm;
1597			break;
1598		case IEEE80211_ELEMID_MESHPEER:
1599			meshpeer = frm;
1600			mpie = (const struct ieee80211_meshpeer_ie *) frm;
1601			memset(mp, 0, sizeof(*mp));
1602			mp->peer_proto = LE_READ_2(&mpie->peer_proto);
1603			mp->peer_llinkid = LE_READ_2(&mpie->peer_llinkid);
1604			switch (subtype) {
1605			case IEEE80211_ACTION_MESHPEERING_CONFIRM:
1606				mp->peer_linkid =
1607				    LE_READ_2(&mpie->peer_linkid);
1608				break;
1609			case IEEE80211_ACTION_MESHPEERING_CLOSE:
1610				/* NB: peer link ID is optional */
1611				if (mpie->peer_len ==
1612				    (IEEE80211_MPM_BASE_SZ + 2)) {
1613					mp->peer_linkid = 0;
1614					mp->peer_rcode =
1615					    LE_READ_2(&mpie->peer_linkid);
1616				} else {
1617					mp->peer_linkid =
1618					    LE_READ_2(&mpie->peer_linkid);
1619					mp->peer_rcode =
1620					    LE_READ_2(&mpie->peer_rcode);
1621				}
1622				break;
1623			}
1624			break;
1625		}
1626		frm += frm[1] + 2;
1627	}
1628
1629	/*
1630	 * Verify the contents of the frame. Action frames with
1631	 * close subtype don't have a Mesh Configuration IE.
1632	 * If if fails validation, close the peer link.
1633	 */
1634	KASSERT(meshpeer != NULL &&
1635	    subtype != IEEE80211_ACTION_MESHPEERING_CLOSE,
1636	    ("parsing close action"));
1637
1638	if (mesh_verify_meshid(vap, meshid) ||
1639	    mesh_verify_meshpeer(vap, subtype, meshpeer) ||
1640	    mesh_verify_meshconf(vap, meshconf)) {
1641		uint16_t args[3];
1642
1643		IEEE80211_DISCARD(vap,
1644		    IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH,
1645		    wh, NULL, "%s", "not for our mesh");
1646		vap->iv_stats.is_rx_mgtdiscard++;
1647		switch (ni->ni_mlstate) {
1648		case IEEE80211_NODE_MESH_IDLE:
1649		case IEEE80211_NODE_MESH_ESTABLISHED:
1650		case IEEE80211_NODE_MESH_HOLDING:
1651			/* ignore */
1652			break;
1653		case IEEE80211_NODE_MESH_OPENSNT:
1654		case IEEE80211_NODE_MESH_OPENRCV:
1655		case IEEE80211_NODE_MESH_CONFIRMRCV:
1656			args[0] = ni->ni_mlpid;
1657			args[1] = ni->ni_mllid;
1658			args[2] = IEEE80211_REASON_PEER_LINK_CANCELED;
1659			ieee80211_send_action(ni,
1660			    IEEE80211_ACTION_CAT_SELF_PROT,
1661			    IEEE80211_ACTION_MESHPEERING_CLOSE,
1662			    args);
1663			mesh_linkchange(ni, IEEE80211_NODE_MESH_HOLDING);
1664			mesh_peer_timeout_setup(ni);
1665			break;
1666		}
1667		return NULL;
1668	}
1669	return (const struct ieee80211_meshpeer_ie *) mp;
1670}
1671
1672static int
1673mesh_recv_action_meshpeering_open(struct ieee80211_node *ni,
1674	const struct ieee80211_frame *wh,
1675	const uint8_t *frm, const uint8_t *efrm)
1676{
1677	struct ieee80211vap *vap = ni->ni_vap;
1678	struct ieee80211_meshpeer_ie ie;
1679	const struct ieee80211_meshpeer_ie *meshpeer;
1680	uint16_t args[3];
1681
1682	/* +2+2 for action + code + capabilites */
1683	meshpeer = mesh_parse_meshpeering_action(ni, wh, frm+2+2, efrm, &ie,
1684	    IEEE80211_ACTION_MESHPEERING_OPEN);
1685	if (meshpeer == NULL) {
1686		return 0;
1687	}
1688
1689	/* XXX move up */
1690	IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH, ni,
1691	    "recv PEER OPEN, lid 0x%x", meshpeer->peer_llinkid);
1692
1693	switch (ni->ni_mlstate) {
1694	case IEEE80211_NODE_MESH_IDLE:
1695		mesh_linkchange(ni, IEEE80211_NODE_MESH_OPENRCV);
1696		ni->ni_mllid = meshpeer->peer_llinkid;
1697		ni->ni_mlpid = mesh_generateid(vap);
1698		if (ni->ni_mlpid == 0)
1699			return 0;		/* XXX */
1700		args[0] = ni->ni_mlpid;
1701		/* Announce we're open too... */
1702		ieee80211_send_action(ni,
1703		    IEEE80211_ACTION_CAT_SELF_PROT,
1704		    IEEE80211_ACTION_MESHPEERING_OPEN, args);
1705		/* ...and confirm the link. */
1706		args[0] = ni->ni_mlpid;
1707		args[1] = ni->ni_mllid;
1708		ieee80211_send_action(ni,
1709		    IEEE80211_ACTION_CAT_SELF_PROT,
1710		    IEEE80211_ACTION_MESHPEERING_CONFIRM,
1711		    args);
1712		mesh_peer_timeout_setup(ni);
1713		break;
1714	case IEEE80211_NODE_MESH_OPENRCV:
1715		/* Wrong Link ID */
1716		if (ni->ni_mllid != meshpeer->peer_llinkid) {
1717			args[0] = ni->ni_mllid;
1718			args[1] = ni->ni_mlpid;
1719			args[2] = IEEE80211_REASON_PEER_LINK_CANCELED;
1720			ieee80211_send_action(ni,
1721			    IEEE80211_ACTION_CAT_SELF_PROT,
1722			    IEEE80211_ACTION_MESHPEERING_CLOSE,
1723			    args);
1724			mesh_linkchange(ni, IEEE80211_NODE_MESH_HOLDING);
1725			mesh_peer_timeout_setup(ni);
1726			break;
1727		}
1728		/* Duplicate open, confirm again. */
1729		args[0] = ni->ni_mlpid;
1730		args[1] = ni->ni_mllid;
1731		ieee80211_send_action(ni,
1732		    IEEE80211_ACTION_CAT_SELF_PROT,
1733		    IEEE80211_ACTION_MESHPEERING_CONFIRM,
1734		    args);
1735		break;
1736	case IEEE80211_NODE_MESH_OPENSNT:
1737		ni->ni_mllid = meshpeer->peer_llinkid;
1738		mesh_linkchange(ni, IEEE80211_NODE_MESH_OPENRCV);
1739		args[0] = ni->ni_mlpid;
1740		args[1] = ni->ni_mllid;
1741		ieee80211_send_action(ni,
1742		    IEEE80211_ACTION_CAT_SELF_PROT,
1743		    IEEE80211_ACTION_MESHPEERING_CONFIRM,
1744		    args);
1745		/* NB: don't setup/clear any timeout */
1746		break;
1747	case IEEE80211_NODE_MESH_CONFIRMRCV:
1748		if (ni->ni_mlpid != meshpeer->peer_linkid ||
1749		    ni->ni_mllid != meshpeer->peer_llinkid) {
1750			args[0] = ni->ni_mlpid;
1751			args[1] = ni->ni_mllid;
1752			args[2] = IEEE80211_REASON_PEER_LINK_CANCELED;
1753			ieee80211_send_action(ni,
1754			    IEEE80211_ACTION_CAT_SELF_PROT,
1755			    IEEE80211_ACTION_MESHPEERING_CLOSE,
1756			    args);
1757			mesh_linkchange(ni,
1758			    IEEE80211_NODE_MESH_HOLDING);
1759			mesh_peer_timeout_setup(ni);
1760			break;
1761		}
1762		mesh_linkchange(ni, IEEE80211_NODE_MESH_ESTABLISHED);
1763		ni->ni_mllid = meshpeer->peer_llinkid;
1764		args[0] = ni->ni_mlpid;
1765		args[1] = ni->ni_mllid;
1766		ieee80211_send_action(ni,
1767		    IEEE80211_ACTION_CAT_SELF_PROT,
1768		    IEEE80211_ACTION_MESHPEERING_CONFIRM,
1769		    args);
1770		mesh_peer_timeout_stop(ni);
1771		break;
1772	case IEEE80211_NODE_MESH_ESTABLISHED:
1773		if (ni->ni_mllid != meshpeer->peer_llinkid) {
1774			args[0] = ni->ni_mllid;
1775			args[1] = ni->ni_mlpid;
1776			args[2] = IEEE80211_REASON_PEER_LINK_CANCELED;
1777			ieee80211_send_action(ni,
1778			    IEEE80211_ACTION_CAT_SELF_PROT,
1779			    IEEE80211_ACTION_MESHPEERING_CLOSE,
1780			    args);
1781			mesh_linkchange(ni, IEEE80211_NODE_MESH_HOLDING);
1782			mesh_peer_timeout_setup(ni);
1783			break;
1784		}
1785		args[0] = ni->ni_mlpid;
1786		args[1] = ni->ni_mllid;
1787		ieee80211_send_action(ni,
1788		    IEEE80211_ACTION_CAT_SELF_PROT,
1789		    IEEE80211_ACTION_MESHPEERING_CONFIRM,
1790		    args);
1791		break;
1792	case IEEE80211_NODE_MESH_HOLDING:
1793		args[0] = ni->ni_mlpid;
1794		args[1] = meshpeer->peer_llinkid;
1795		args[2] = IEEE80211_REASON_MESH_MAX_RETRIES;
1796		ieee80211_send_action(ni,
1797		    IEEE80211_ACTION_CAT_SELF_PROT,
1798		    IEEE80211_ACTION_MESHPEERING_CLOSE,
1799		    args);
1800		break;
1801	}
1802	return 0;
1803}
1804
1805static int
1806mesh_recv_action_meshpeering_confirm(struct ieee80211_node *ni,
1807	const struct ieee80211_frame *wh,
1808	const uint8_t *frm, const uint8_t *efrm)
1809{
1810	struct ieee80211vap *vap = ni->ni_vap;
1811	struct ieee80211_meshpeer_ie ie;
1812	const struct ieee80211_meshpeer_ie *meshpeer;
1813	uint16_t args[3];
1814
1815	/* +2+2+2+2 for action + code + capabilites + status code + AID */
1816	meshpeer = mesh_parse_meshpeering_action(ni, wh, frm+2+2+2+2, efrm, &ie,
1817	    IEEE80211_ACTION_MESHPEERING_CONFIRM);
1818	if (meshpeer == NULL) {
1819		return 0;
1820	}
1821
1822	IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH, ni,
1823	    "recv PEER CONFIRM, local id 0x%x, peer id 0x%x",
1824	    meshpeer->peer_llinkid, meshpeer->peer_linkid);
1825
1826	switch (ni->ni_mlstate) {
1827	case IEEE80211_NODE_MESH_OPENRCV:
1828		mesh_linkchange(ni, IEEE80211_NODE_MESH_ESTABLISHED);
1829		mesh_peer_timeout_stop(ni);
1830		break;
1831	case IEEE80211_NODE_MESH_OPENSNT:
1832		mesh_linkchange(ni, IEEE80211_NODE_MESH_CONFIRMRCV);
1833		break;
1834	case IEEE80211_NODE_MESH_HOLDING:
1835		args[0] = ni->ni_mlpid;
1836		args[1] = meshpeer->peer_llinkid;
1837		args[2] = IEEE80211_REASON_MESH_MAX_RETRIES;
1838		ieee80211_send_action(ni,
1839		    IEEE80211_ACTION_CAT_SELF_PROT,
1840		    IEEE80211_ACTION_MESHPEERING_CLOSE,
1841		    args);
1842		break;
1843	case IEEE80211_NODE_MESH_CONFIRMRCV:
1844		if (ni->ni_mllid != meshpeer->peer_llinkid) {
1845			args[0] = ni->ni_mlpid;
1846			args[1] = ni->ni_mllid;
1847			args[2] = IEEE80211_REASON_PEER_LINK_CANCELED;
1848			ieee80211_send_action(ni,
1849			    IEEE80211_ACTION_CAT_SELF_PROT,
1850			    IEEE80211_ACTION_MESHPEERING_CLOSE,
1851			    args);
1852			mesh_linkchange(ni, IEEE80211_NODE_MESH_HOLDING);
1853			mesh_peer_timeout_setup(ni);
1854		}
1855		break;
1856	default:
1857		IEEE80211_DISCARD(vap,
1858		    IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH,
1859		    wh, NULL, "received confirm in invalid state %d",
1860		    ni->ni_mlstate);
1861		vap->iv_stats.is_rx_mgtdiscard++;
1862		break;
1863	}
1864	return 0;
1865}
1866
1867static int
1868mesh_recv_action_meshpeering_close(struct ieee80211_node *ni,
1869	const struct ieee80211_frame *wh,
1870	const uint8_t *frm, const uint8_t *efrm)
1871{
1872	uint16_t args[3];
1873
1874	IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH,
1875	    ni, "%s", "recv PEER CLOSE");
1876
1877	switch (ni->ni_mlstate) {
1878	case IEEE80211_NODE_MESH_IDLE:
1879		/* ignore */
1880		break;
1881	case IEEE80211_NODE_MESH_OPENRCV:
1882	case IEEE80211_NODE_MESH_OPENSNT:
1883	case IEEE80211_NODE_MESH_CONFIRMRCV:
1884	case IEEE80211_NODE_MESH_ESTABLISHED:
1885		args[0] = ni->ni_mlpid;
1886		args[1] = ni->ni_mllid;
1887		args[2] = IEEE80211_REASON_MESH_CLOSE_RCVD;
1888		ieee80211_send_action(ni,
1889		    IEEE80211_ACTION_CAT_SELF_PROT,
1890		    IEEE80211_ACTION_MESHPEERING_CLOSE,
1891		    args);
1892		mesh_linkchange(ni, IEEE80211_NODE_MESH_HOLDING);
1893		mesh_peer_timeout_setup(ni);
1894		break;
1895	case IEEE80211_NODE_MESH_HOLDING:
1896		mesh_linkchange(ni, IEEE80211_NODE_MESH_IDLE);
1897		mesh_peer_timeout_setup(ni);
1898		break;
1899	}
1900	return 0;
1901}
1902
1903/*
1904 * Link Metric handling.
1905 */
1906static int
1907mesh_recv_action_meshlmetric(struct ieee80211_node *ni,
1908	const struct ieee80211_frame *wh,
1909	const uint8_t *frm, const uint8_t *efrm)
1910{
1911	const struct ieee80211_meshlmetric_ie *ie =
1912	    (const struct ieee80211_meshlmetric_ie *)
1913	    (frm+2); /* action + code */
1914	struct ieee80211_meshlmetric_ie lm_rep;
1915
1916	if (ie->lm_flags & IEEE80211_MESH_LMETRIC_FLAGS_REQ) {
1917		lm_rep.lm_flags = 0;
1918		lm_rep.lm_metric = mesh_airtime_calc(ni);
1919		ieee80211_send_action(ni,
1920		    IEEE80211_ACTION_CAT_MESH,
1921		    IEEE80211_ACTION_MESH_LMETRIC,
1922		    &lm_rep);
1923	}
1924	/* XXX: else do nothing for now */
1925	return 0;
1926}
1927
1928static int
1929mesh_send_action(struct ieee80211_node *ni, struct mbuf *m)
1930{
1931	struct ieee80211_bpf_params params;
1932
1933	memset(&params, 0, sizeof(params));
1934	params.ibp_pri = WME_AC_VO;
1935	params.ibp_rate0 = ni->ni_txparms->mgmtrate;
1936	/* XXX ucast/mcast */
1937	params.ibp_try0 = ni->ni_txparms->maxretry;
1938	params.ibp_power = ni->ni_txpower;
1939	return ieee80211_mgmt_output(ni, m, IEEE80211_FC0_SUBTYPE_ACTION,
1940	     &params);
1941}
1942
1943#define	ADDSHORT(frm, v) do {			\
1944	frm[0] = (v) & 0xff;			\
1945	frm[1] = (v) >> 8;			\
1946	frm += 2;				\
1947} while (0)
1948#define	ADDWORD(frm, v) do {			\
1949	frm[0] = (v) & 0xff;			\
1950	frm[1] = ((v) >> 8) & 0xff;		\
1951	frm[2] = ((v) >> 16) & 0xff;		\
1952	frm[3] = ((v) >> 24) & 0xff;		\
1953	frm += 4;				\
1954} while (0)
1955
1956static int
1957mesh_send_action_meshpeering_open(struct ieee80211_node *ni,
1958	int category, int action, void *args0)
1959{
1960	struct ieee80211vap *vap = ni->ni_vap;
1961	struct ieee80211com *ic = ni->ni_ic;
1962	uint16_t *args = args0;
1963	const struct ieee80211_rateset *rs;
1964	struct mbuf *m;
1965	uint8_t *frm;
1966
1967	IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH, ni,
1968	    "send PEER OPEN action: localid 0x%x", args[0]);
1969
1970	IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
1971	    "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", __func__, __LINE__,
1972	    ni, ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)+1);
1973	ieee80211_ref_node(ni);
1974
1975	m = ieee80211_getmgtframe(&frm,
1976	    ic->ic_headroom + sizeof(struct ieee80211_frame),
1977	    sizeof(uint16_t)	/* action+category */
1978	    + sizeof(uint16_t)	/* capabilites */
1979	    + 2 + IEEE80211_RATE_SIZE
1980	    + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
1981	    + 2 + IEEE80211_MESHID_LEN
1982	    + sizeof(struct ieee80211_meshconf_ie)
1983	    + sizeof(struct ieee80211_meshpeer_ie)
1984	);
1985	if (m != NULL) {
1986		/*
1987		 * mesh peer open action frame format:
1988		 *   [1] category
1989		 *   [1] action
1990		 *   [2] capabilities
1991		 *   [tlv] rates
1992		 *   [tlv] xrates
1993		 *   [tlv] mesh id
1994		 *   [tlv] mesh conf
1995		 *   [tlv] mesh peer link mgmt
1996		 */
1997		*frm++ = category;
1998		*frm++ = action;
1999		ADDSHORT(frm, ieee80211_getcapinfo(vap, ni->ni_chan));
2000		rs = ieee80211_get_suprates(ic, ic->ic_curchan);
2001		frm = ieee80211_add_rates(frm, rs);
2002		frm = ieee80211_add_xrates(frm, rs);
2003		frm = ieee80211_add_meshid(frm, vap);
2004		frm = ieee80211_add_meshconf(frm, vap);
2005		frm = ieee80211_add_meshpeer(frm, IEEE80211_ACTION_MESHPEERING_OPEN,
2006		    args[0], 0, 0);
2007		m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2008		return mesh_send_action(ni, m);
2009	} else {
2010		vap->iv_stats.is_tx_nobuf++;
2011		ieee80211_free_node(ni);
2012		return ENOMEM;
2013	}
2014}
2015
2016static int
2017mesh_send_action_meshpeering_confirm(struct ieee80211_node *ni,
2018	int category, int action, void *args0)
2019{
2020	struct ieee80211vap *vap = ni->ni_vap;
2021	struct ieee80211com *ic = ni->ni_ic;
2022	uint16_t *args = args0;
2023	const struct ieee80211_rateset *rs;
2024	struct mbuf *m;
2025	uint8_t *frm;
2026
2027	IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH, ni,
2028	    "send PEER CONFIRM action: localid 0x%x, peerid 0x%x",
2029	    args[0], args[1]);
2030
2031	IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
2032	    "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", __func__, __LINE__,
2033	    ni, ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)+1);
2034	ieee80211_ref_node(ni);
2035
2036	m = ieee80211_getmgtframe(&frm,
2037	    ic->ic_headroom + sizeof(struct ieee80211_frame),
2038	    sizeof(uint16_t)	/* action+category */
2039	    + sizeof(uint16_t)	/* capabilites */
2040	    + sizeof(uint16_t)	/* status code */
2041	    + sizeof(uint16_t)	/* AID */
2042	    + 2 + IEEE80211_RATE_SIZE
2043	    + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
2044	    + 2 + IEEE80211_MESHID_LEN
2045	    + sizeof(struct ieee80211_meshconf_ie)
2046	    + sizeof(struct ieee80211_meshpeer_ie)
2047	);
2048	if (m != NULL) {
2049		/*
2050		 * mesh peer confirm action frame format:
2051		 *   [1] category
2052		 *   [1] action
2053		 *   [2] capabilities
2054		 *   [2] status code
2055		 *   [2] association id (peer ID)
2056		 *   [tlv] rates
2057		 *   [tlv] xrates
2058		 *   [tlv] mesh id
2059		 *   [tlv] mesh conf
2060		 *   [tlv] mesh peer link mgmt
2061		 */
2062		*frm++ = category;
2063		*frm++ = action;
2064		ADDSHORT(frm, ieee80211_getcapinfo(vap, ni->ni_chan));
2065		ADDSHORT(frm, 0);		/* status code */
2066		ADDSHORT(frm, args[1]);		/* AID */
2067		rs = ieee80211_get_suprates(ic, ic->ic_curchan);
2068		frm = ieee80211_add_rates(frm, rs);
2069		frm = ieee80211_add_xrates(frm, rs);
2070		frm = ieee80211_add_meshid(frm, vap);
2071		frm = ieee80211_add_meshconf(frm, vap);
2072		frm = ieee80211_add_meshpeer(frm,
2073		    IEEE80211_ACTION_MESHPEERING_CONFIRM,
2074		    args[0], args[1], 0);
2075		m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2076		return mesh_send_action(ni, m);
2077	} else {
2078		vap->iv_stats.is_tx_nobuf++;
2079		ieee80211_free_node(ni);
2080		return ENOMEM;
2081	}
2082}
2083
2084static int
2085mesh_send_action_meshpeering_close(struct ieee80211_node *ni,
2086	int category, int action, void *args0)
2087{
2088	struct ieee80211vap *vap = ni->ni_vap;
2089	struct ieee80211com *ic = ni->ni_ic;
2090	uint16_t *args = args0;
2091	struct mbuf *m;
2092	uint8_t *frm;
2093
2094	IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH, ni,
2095	    "send PEER CLOSE action: localid 0x%x, peerid 0x%x reason %d",
2096	    args[0], args[1], args[2]);
2097
2098	IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
2099	    "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", __func__, __LINE__,
2100	    ni, ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)+1);
2101	ieee80211_ref_node(ni);
2102
2103	m = ieee80211_getmgtframe(&frm,
2104	    ic->ic_headroom + sizeof(struct ieee80211_frame),
2105	    sizeof(uint16_t)	/* action+category */
2106	    + sizeof(uint16_t)	/* reason code */
2107	    + 2 + IEEE80211_MESHID_LEN
2108	    + sizeof(struct ieee80211_meshpeer_ie)
2109	);
2110	if (m != NULL) {
2111		/*
2112		 * mesh peer close action frame format:
2113		 *   [1] category
2114		 *   [1] action
2115		 *   [2] reason code
2116		 *   [tlv] mesh id
2117		 *   [tlv] mesh peer link mgmt
2118		 */
2119		*frm++ = category;
2120		*frm++ = action;
2121		ADDSHORT(frm, args[2]);		/* reason code */
2122		frm = ieee80211_add_meshid(frm, vap);
2123		frm = ieee80211_add_meshpeer(frm,
2124		    IEEE80211_ACTION_MESHPEERING_CLOSE,
2125		    args[0], args[1], args[2]);
2126		m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2127		return mesh_send_action(ni, m);
2128	} else {
2129		vap->iv_stats.is_tx_nobuf++;
2130		ieee80211_free_node(ni);
2131		return ENOMEM;
2132	}
2133}
2134
2135static int
2136mesh_send_action_meshlmetric(struct ieee80211_node *ni,
2137	int category, int action, void *arg0)
2138{
2139	struct ieee80211vap *vap = ni->ni_vap;
2140	struct ieee80211com *ic = ni->ni_ic;
2141	struct ieee80211_meshlmetric_ie *ie = arg0;
2142	struct mbuf *m;
2143	uint8_t *frm;
2144
2145	if (ie->lm_flags & IEEE80211_MESH_LMETRIC_FLAGS_REQ) {
2146		IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH,
2147		    ni, "%s", "send LINK METRIC REQUEST action");
2148	} else {
2149		IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH,
2150		    ni, "send LINK METRIC REPLY action: metric 0x%x",
2151		    ie->lm_metric);
2152	}
2153	IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
2154	    "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", __func__, __LINE__,
2155	    ni, ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)+1);
2156	ieee80211_ref_node(ni);
2157
2158	m = ieee80211_getmgtframe(&frm,
2159	    ic->ic_headroom + sizeof(struct ieee80211_frame),
2160	    sizeof(uint16_t) +	/* action+category */
2161	    sizeof(struct ieee80211_meshlmetric_ie)
2162	);
2163	if (m != NULL) {
2164		/*
2165		 * mesh link metric
2166		 *   [1] category
2167		 *   [1] action
2168		 *   [tlv] mesh link metric
2169		 */
2170		*frm++ = category;
2171		*frm++ = action;
2172		frm = ieee80211_add_meshlmetric(frm,
2173		    ie->lm_flags, ie->lm_metric);
2174		m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2175		return mesh_send_action(ni, m);
2176	} else {
2177		vap->iv_stats.is_tx_nobuf++;
2178		ieee80211_free_node(ni);
2179		return ENOMEM;
2180	}
2181}
2182
2183static void
2184mesh_peer_timeout_setup(struct ieee80211_node *ni)
2185{
2186	switch (ni->ni_mlstate) {
2187	case IEEE80211_NODE_MESH_HOLDING:
2188		ni->ni_mltval = ieee80211_mesh_holdingtimeout;
2189		break;
2190	case IEEE80211_NODE_MESH_CONFIRMRCV:
2191		ni->ni_mltval = ieee80211_mesh_confirmtimeout;
2192		break;
2193	case IEEE80211_NODE_MESH_IDLE:
2194		ni->ni_mltval = 0;
2195		break;
2196	default:
2197		ni->ni_mltval = ieee80211_mesh_retrytimeout;
2198		break;
2199	}
2200	if (ni->ni_mltval)
2201		callout_reset(&ni->ni_mltimer, ni->ni_mltval,
2202		    mesh_peer_timeout_cb, ni);
2203}
2204
2205/*
2206 * Same as above but backoffs timer statisically 50%.
2207 */
2208static void
2209mesh_peer_timeout_backoff(struct ieee80211_node *ni)
2210{
2211	uint32_t r;
2212
2213	r = arc4random();
2214	ni->ni_mltval += r % ni->ni_mltval;
2215	callout_reset(&ni->ni_mltimer, ni->ni_mltval, mesh_peer_timeout_cb,
2216	    ni);
2217}
2218
2219static __inline void
2220mesh_peer_timeout_stop(struct ieee80211_node *ni)
2221{
2222	callout_drain(&ni->ni_mltimer);
2223}
2224
2225/*
2226 * Mesh Peer Link Management FSM timeout handling.
2227 */
2228static void
2229mesh_peer_timeout_cb(void *arg)
2230{
2231	struct ieee80211_node *ni = (struct ieee80211_node *)arg;
2232	uint16_t args[3];
2233
2234	IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_MESH,
2235	    ni, "mesh link timeout, state %d, retry counter %d",
2236	    ni->ni_mlstate, ni->ni_mlrcnt);
2237
2238	switch (ni->ni_mlstate) {
2239	case IEEE80211_NODE_MESH_IDLE:
2240	case IEEE80211_NODE_MESH_ESTABLISHED:
2241		break;
2242	case IEEE80211_NODE_MESH_OPENSNT:
2243	case IEEE80211_NODE_MESH_OPENRCV:
2244		if (ni->ni_mlrcnt == ieee80211_mesh_maxretries) {
2245			args[0] = ni->ni_mlpid;
2246			args[2] = IEEE80211_REASON_MESH_MAX_RETRIES;
2247			ieee80211_send_action(ni,
2248			    IEEE80211_ACTION_CAT_SELF_PROT,
2249			    IEEE80211_ACTION_MESHPEERING_CLOSE, args);
2250			ni->ni_mlrcnt = 0;
2251			mesh_linkchange(ni, IEEE80211_NODE_MESH_HOLDING);
2252			mesh_peer_timeout_setup(ni);
2253		} else {
2254			args[0] = ni->ni_mlpid;
2255			ieee80211_send_action(ni,
2256			    IEEE80211_ACTION_CAT_SELF_PROT,
2257			    IEEE80211_ACTION_MESHPEERING_OPEN, args);
2258			ni->ni_mlrcnt++;
2259			mesh_peer_timeout_backoff(ni);
2260		}
2261		break;
2262	case IEEE80211_NODE_MESH_CONFIRMRCV:
2263		if (ni->ni_mlrcnt == ieee80211_mesh_maxretries) {
2264			args[0] = ni->ni_mlpid;
2265			args[2] = IEEE80211_REASON_MESH_CONFIRM_TIMEOUT;
2266			ieee80211_send_action(ni,
2267			    IEEE80211_ACTION_CAT_SELF_PROT,
2268			    IEEE80211_ACTION_MESHPEERING_CLOSE, args);
2269			ni->ni_mlrcnt = 0;
2270			mesh_linkchange(ni, IEEE80211_NODE_MESH_HOLDING);
2271			mesh_peer_timeout_setup(ni);
2272		} else {
2273			ni->ni_mlrcnt++;
2274			mesh_peer_timeout_setup(ni);
2275		}
2276		break;
2277	case IEEE80211_NODE_MESH_HOLDING:
2278		mesh_linkchange(ni, IEEE80211_NODE_MESH_IDLE);
2279		break;
2280	}
2281}
2282
2283static int
2284mesh_verify_meshid(struct ieee80211vap *vap, const uint8_t *ie)
2285{
2286	struct ieee80211_mesh_state *ms = vap->iv_mesh;
2287
2288	if (ie == NULL || ie[1] != ms->ms_idlen)
2289		return 1;
2290	return memcmp(ms->ms_id, ie + 2, ms->ms_idlen);
2291}
2292
2293/*
2294 * Check if we are using the same algorithms for this mesh.
2295 */
2296static int
2297mesh_verify_meshconf(struct ieee80211vap *vap, const uint8_t *ie)
2298{
2299	const struct ieee80211_meshconf_ie *meshconf =
2300	    (const struct ieee80211_meshconf_ie *) ie;
2301	const struct ieee80211_mesh_state *ms = vap->iv_mesh;
2302
2303	if (meshconf == NULL)
2304		return 1;
2305	if (meshconf->conf_pselid != ms->ms_ppath->mpp_ie) {
2306		IEEE80211_DPRINTF(vap, IEEE80211_MSG_MESH,
2307		    "unknown path selection algorithm: 0x%x\n",
2308		    meshconf->conf_pselid);
2309		return 1;
2310	}
2311	if (meshconf->conf_pmetid != ms->ms_pmetric->mpm_ie) {
2312		IEEE80211_DPRINTF(vap, IEEE80211_MSG_MESH,
2313		    "unknown path metric algorithm: 0x%x\n",
2314		    meshconf->conf_pmetid);
2315		return 1;
2316	}
2317	if (meshconf->conf_ccid != 0) {
2318		IEEE80211_DPRINTF(vap, IEEE80211_MSG_MESH,
2319		    "unknown congestion control algorithm: 0x%x\n",
2320		    meshconf->conf_ccid);
2321		return 1;
2322	}
2323	if (meshconf->conf_syncid != IEEE80211_MESHCONF_SYNC_NEIGHOFF) {
2324		IEEE80211_DPRINTF(vap, IEEE80211_MSG_MESH,
2325		    "unknown sync algorithm: 0x%x\n",
2326		    meshconf->conf_syncid);
2327		return 1;
2328	}
2329	if (meshconf->conf_authid != 0) {
2330		IEEE80211_DPRINTF(vap, IEEE80211_MSG_MESH,
2331		    "unknown auth auth algorithm: 0x%x\n",
2332		    meshconf->conf_pselid);
2333		return 1;
2334	}
2335	/* Not accepting peers */
2336	if (!(meshconf->conf_cap & IEEE80211_MESHCONF_CAP_AP)) {
2337		IEEE80211_DPRINTF(vap, IEEE80211_MSG_MESH,
2338		    "not accepting peers: 0x%x\n", meshconf->conf_cap);
2339		return 1;
2340	}
2341	return 0;
2342}
2343
2344static int
2345mesh_verify_meshpeer(struct ieee80211vap *vap, uint8_t subtype,
2346    const uint8_t *ie)
2347{
2348	const struct ieee80211_meshpeer_ie *meshpeer =
2349	    (const struct ieee80211_meshpeer_ie *) ie;
2350
2351	if (meshpeer == NULL ||
2352	    meshpeer->peer_len < IEEE80211_MPM_BASE_SZ ||
2353	    meshpeer->peer_len > IEEE80211_MPM_MAX_SZ)
2354		return 1;
2355	if (meshpeer->peer_proto != IEEE80211_MPPID_MPM) {
2356		IEEE80211_DPRINTF(vap,
2357		    IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH,
2358		    "Only MPM protocol is supported (proto: 0x%02X)",
2359		    meshpeer->peer_proto);
2360		return 1;
2361	}
2362	switch (subtype) {
2363	case IEEE80211_ACTION_MESHPEERING_OPEN:
2364		if (meshpeer->peer_len != IEEE80211_MPM_BASE_SZ)
2365			return 1;
2366		break;
2367	case IEEE80211_ACTION_MESHPEERING_CONFIRM:
2368		if (meshpeer->peer_len != IEEE80211_MPM_BASE_SZ + 2)
2369			return 1;
2370		break;
2371	case IEEE80211_ACTION_MESHPEERING_CLOSE:
2372		if (meshpeer->peer_len < IEEE80211_MPM_BASE_SZ + 2)
2373			return 1;
2374		if (meshpeer->peer_len == (IEEE80211_MPM_BASE_SZ + 2) &&
2375		    meshpeer->peer_linkid != 0)
2376			return 1;
2377		if (meshpeer->peer_rcode == 0)
2378			return 1;
2379		break;
2380	}
2381	return 0;
2382}
2383
2384/*
2385 * Add a Mesh ID IE to a frame.
2386 */
2387uint8_t *
2388ieee80211_add_meshid(uint8_t *frm, struct ieee80211vap *vap)
2389{
2390	struct ieee80211_mesh_state *ms = vap->iv_mesh;
2391
2392	KASSERT(vap->iv_opmode == IEEE80211_M_MBSS, ("not a mbss vap"));
2393
2394	*frm++ = IEEE80211_ELEMID_MESHID;
2395	*frm++ = ms->ms_idlen;
2396	memcpy(frm, ms->ms_id, ms->ms_idlen);
2397	return frm + ms->ms_idlen;
2398}
2399
2400/*
2401 * Add a Mesh Configuration IE to a frame.
2402 * For now just use HWMP routing, Airtime link metric, Null Congestion
2403 * Signaling, Null Sync Protocol and Null Authentication.
2404 */
2405uint8_t *
2406ieee80211_add_meshconf(uint8_t *frm, struct ieee80211vap *vap)
2407{
2408	const struct ieee80211_mesh_state *ms = vap->iv_mesh;
2409	uint16_t caps;
2410
2411	KASSERT(vap->iv_opmode == IEEE80211_M_MBSS, ("not a MBSS vap"));
2412
2413	*frm++ = IEEE80211_ELEMID_MESHCONF;
2414	*frm++ = IEEE80211_MESH_CONF_SZ;
2415	*frm++ = ms->ms_ppath->mpp_ie;		/* path selection */
2416	*frm++ = ms->ms_pmetric->mpm_ie;	/* link metric */
2417	*frm++ = IEEE80211_MESHCONF_CC_DISABLED;
2418	*frm++ = IEEE80211_MESHCONF_SYNC_NEIGHOFF;
2419	*frm++ = IEEE80211_MESHCONF_AUTH_DISABLED;
2420	/* NB: set the number of neighbors before the rest */
2421	*frm = (ms->ms_neighbors > 15 ? 15 : ms->ms_neighbors) << 1;
2422	if (ms->ms_flags & IEEE80211_MESHFLAGS_PORTAL)
2423		*frm |= IEEE80211_MESHCONF_FORM_MP;
2424	frm += 1;
2425	caps = 0;
2426	if (ms->ms_flags & IEEE80211_MESHFLAGS_AP)
2427		caps |= IEEE80211_MESHCONF_CAP_AP;
2428	if (ms->ms_flags & IEEE80211_MESHFLAGS_FWD)
2429		caps |= IEEE80211_MESHCONF_CAP_FWRD;
2430	*frm++ = caps;
2431	return frm;
2432}
2433
2434/*
2435 * Add a Mesh Peer Management IE to a frame.
2436 */
2437uint8_t *
2438ieee80211_add_meshpeer(uint8_t *frm, uint8_t subtype, uint16_t localid,
2439    uint16_t peerid, uint16_t reason)
2440{
2441
2442	KASSERT(localid != 0, ("localid == 0"));
2443
2444	*frm++ = IEEE80211_ELEMID_MESHPEER;
2445	switch (subtype) {
2446	case IEEE80211_ACTION_MESHPEERING_OPEN:
2447		*frm++ = IEEE80211_MPM_BASE_SZ;		/* length */
2448		ADDSHORT(frm, IEEE80211_MPPID_MPM);	/* proto */
2449		ADDSHORT(frm, localid);			/* local ID */
2450		break;
2451	case IEEE80211_ACTION_MESHPEERING_CONFIRM:
2452		KASSERT(peerid != 0, ("sending peer confirm without peer id"));
2453		*frm++ = IEEE80211_MPM_BASE_SZ + 2;	/* length */
2454		ADDSHORT(frm, IEEE80211_MPPID_MPM);	/* proto */
2455		ADDSHORT(frm, localid);			/* local ID */
2456		ADDSHORT(frm, peerid);			/* peer ID */
2457		break;
2458	case IEEE80211_ACTION_MESHPEERING_CLOSE:
2459		if (peerid)
2460			*frm++ = IEEE80211_MPM_MAX_SZ;	/* length */
2461		else
2462			*frm++ = IEEE80211_MPM_BASE_SZ + 2; /* length */
2463		ADDSHORT(frm, IEEE80211_MPPID_MPM);	/* proto */
2464		ADDSHORT(frm, localid);	/* local ID */
2465		if (peerid)
2466			ADDSHORT(frm, peerid);	/* peer ID */
2467		ADDSHORT(frm, reason);
2468		break;
2469	}
2470	return frm;
2471}
2472
2473/*
2474 * Compute an Airtime Link Metric for the link with this node.
2475 *
2476 * Based on Draft 3.0 spec (11B.10, p.149).
2477 */
2478/*
2479 * Max 802.11s overhead.
2480 */
2481#define IEEE80211_MESH_MAXOVERHEAD \
2482	(sizeof(struct ieee80211_qosframe_addr4) \
2483	 + sizeof(struct ieee80211_meshcntl_ae11) \
2484	+ sizeof(struct llc) \
2485	+ IEEE80211_ADDR_LEN \
2486	+ IEEE80211_WEP_IVLEN \
2487	+ IEEE80211_WEP_KIDLEN \
2488	+ IEEE80211_WEP_CRCLEN \
2489	+ IEEE80211_WEP_MICLEN \
2490	+ IEEE80211_CRC_LEN)
2491uint32_t
2492mesh_airtime_calc(struct ieee80211_node *ni)
2493{
2494#define M_BITS 8
2495#define S_FACTOR (2 * M_BITS)
2496	struct ieee80211com *ic = ni->ni_ic;
2497	struct ifnet *ifp = ni->ni_vap->iv_ifp;
2498	const static int nbits = 8192 << M_BITS;
2499	uint32_t overhead, rate, errrate;
2500	uint64_t res;
2501
2502	/* Time to transmit a frame */
2503	rate = ni->ni_txrate;
2504	overhead = ieee80211_compute_duration(ic->ic_rt,
2505	    ifp->if_mtu + IEEE80211_MESH_MAXOVERHEAD, rate, 0) << M_BITS;
2506	/* Error rate in percentage */
2507	/* XXX assuming small failures are ok */
2508	errrate = (((ifp->if_oerrors +
2509	    ifp->if_ierrors) / 100) << M_BITS) / 100;
2510	res = (overhead + (nbits / rate)) *
2511	    ((1 << S_FACTOR) / ((1 << M_BITS) - errrate));
2512
2513	return (uint32_t)(res >> S_FACTOR);
2514#undef M_BITS
2515#undef S_FACTOR
2516}
2517
2518/*
2519 * Add a Mesh Link Metric report IE to a frame.
2520 */
2521uint8_t *
2522ieee80211_add_meshlmetric(uint8_t *frm, uint8_t flags, uint32_t metric)
2523{
2524	*frm++ = IEEE80211_ELEMID_MESHLINK;
2525	*frm++ = 5;
2526	*frm++ = flags;
2527	ADDWORD(frm, metric);
2528	return frm;
2529}
2530#undef ADDSHORT
2531#undef ADDWORD
2532
2533/*
2534 * Initialize any mesh-specific node state.
2535 */
2536void
2537ieee80211_mesh_node_init(struct ieee80211vap *vap, struct ieee80211_node *ni)
2538{
2539	ni->ni_flags |= IEEE80211_NODE_QOS;
2540	callout_init(&ni->ni_mltimer, CALLOUT_MPSAFE);
2541}
2542
2543/*
2544 * Cleanup any mesh-specific node state.
2545 */
2546void
2547ieee80211_mesh_node_cleanup(struct ieee80211_node *ni)
2548{
2549	struct ieee80211vap *vap = ni->ni_vap;
2550	struct ieee80211_mesh_state *ms = vap->iv_mesh;
2551
2552	callout_drain(&ni->ni_mltimer);
2553	/* NB: short-circuit callbacks after mesh_vdetach */
2554	if (vap->iv_mesh != NULL)
2555		ms->ms_ppath->mpp_peerdown(ni);
2556}
2557
2558void
2559ieee80211_parse_meshid(struct ieee80211_node *ni, const uint8_t *ie)
2560{
2561	ni->ni_meshidlen = ie[1];
2562	memcpy(ni->ni_meshid, ie + 2, ie[1]);
2563}
2564
2565/*
2566 * Setup mesh-specific node state on neighbor discovery.
2567 */
2568void
2569ieee80211_mesh_init_neighbor(struct ieee80211_node *ni,
2570	const struct ieee80211_frame *wh,
2571	const struct ieee80211_scanparams *sp)
2572{
2573	ieee80211_parse_meshid(ni, sp->meshid);
2574}
2575
2576void
2577ieee80211_mesh_update_beacon(struct ieee80211vap *vap,
2578	struct ieee80211_beacon_offsets *bo)
2579{
2580	KASSERT(vap->iv_opmode == IEEE80211_M_MBSS, ("not a MBSS vap"));
2581
2582	if (isset(bo->bo_flags, IEEE80211_BEACON_MESHCONF)) {
2583		(void)ieee80211_add_meshconf(bo->bo_meshconf, vap);
2584		clrbit(bo->bo_flags, IEEE80211_BEACON_MESHCONF);
2585	}
2586}
2587
2588static int
2589mesh_ioctl_get80211(struct ieee80211vap *vap, struct ieee80211req *ireq)
2590{
2591	struct ieee80211_mesh_state *ms = vap->iv_mesh;
2592	uint8_t tmpmeshid[IEEE80211_NWID_LEN];
2593	struct ieee80211_mesh_route *rt;
2594	struct ieee80211req_mesh_route *imr;
2595	size_t len, off;
2596	uint8_t *p;
2597	int error;
2598
2599	if (vap->iv_opmode != IEEE80211_M_MBSS)
2600		return ENOSYS;
2601
2602	error = 0;
2603	switch (ireq->i_type) {
2604	case IEEE80211_IOC_MESH_ID:
2605		ireq->i_len = ms->ms_idlen;
2606		memcpy(tmpmeshid, ms->ms_id, ireq->i_len);
2607		error = copyout(tmpmeshid, ireq->i_data, ireq->i_len);
2608		break;
2609	case IEEE80211_IOC_MESH_AP:
2610		ireq->i_val = (ms->ms_flags & IEEE80211_MESHFLAGS_AP) != 0;
2611		break;
2612	case IEEE80211_IOC_MESH_FWRD:
2613		ireq->i_val = (ms->ms_flags & IEEE80211_MESHFLAGS_FWD) != 0;
2614		break;
2615	case IEEE80211_IOC_MESH_TTL:
2616		ireq->i_val = ms->ms_ttl;
2617		break;
2618	case IEEE80211_IOC_MESH_RTCMD:
2619		switch (ireq->i_val) {
2620		case IEEE80211_MESH_RTCMD_LIST:
2621			len = 0;
2622			MESH_RT_LOCK(ms);
2623			TAILQ_FOREACH(rt, &ms->ms_routes, rt_next) {
2624				len += sizeof(*imr);
2625			}
2626			MESH_RT_UNLOCK(ms);
2627			if (len > ireq->i_len || ireq->i_len < sizeof(*imr)) {
2628				ireq->i_len = len;
2629				return ENOMEM;
2630			}
2631			ireq->i_len = len;
2632			/* XXX M_WAIT? */
2633			p = malloc(len, M_TEMP, M_NOWAIT | M_ZERO);
2634			if (p == NULL)
2635				return ENOMEM;
2636			off = 0;
2637			MESH_RT_LOCK(ms);
2638			TAILQ_FOREACH(rt, &ms->ms_routes, rt_next) {
2639				if (off >= len)
2640					break;
2641				imr = (struct ieee80211req_mesh_route *)
2642				    (p + off);
2643				imr->imr_flags = rt->rt_flags;
2644				IEEE80211_ADDR_COPY(imr->imr_dest,
2645				    rt->rt_dest);
2646				IEEE80211_ADDR_COPY(imr->imr_nexthop,
2647				    rt->rt_nexthop);
2648				imr->imr_metric = rt->rt_metric;
2649				imr->imr_nhops = rt->rt_nhops;
2650				imr->imr_lifetime = rt->rt_lifetime;
2651				imr->imr_lastmseq = rt->rt_lastmseq;
2652				off += sizeof(*imr);
2653			}
2654			MESH_RT_UNLOCK(ms);
2655			error = copyout(p, (uint8_t *)ireq->i_data,
2656			    ireq->i_len);
2657			free(p, M_TEMP);
2658			break;
2659		case IEEE80211_MESH_RTCMD_FLUSH:
2660		case IEEE80211_MESH_RTCMD_ADD:
2661		case IEEE80211_MESH_RTCMD_DELETE:
2662			return EINVAL;
2663		default:
2664			return ENOSYS;
2665		}
2666		break;
2667	case IEEE80211_IOC_MESH_PR_METRIC:
2668		len = strlen(ms->ms_pmetric->mpm_descr);
2669		if (ireq->i_len < len)
2670			return EINVAL;
2671		ireq->i_len = len;
2672		error = copyout(ms->ms_pmetric->mpm_descr,
2673		    (uint8_t *)ireq->i_data, len);
2674		break;
2675	case IEEE80211_IOC_MESH_PR_PATH:
2676		len = strlen(ms->ms_ppath->mpp_descr);
2677		if (ireq->i_len < len)
2678			return EINVAL;
2679		ireq->i_len = len;
2680		error = copyout(ms->ms_ppath->mpp_descr,
2681		    (uint8_t *)ireq->i_data, len);
2682		break;
2683	default:
2684		return ENOSYS;
2685	}
2686
2687	return error;
2688}
2689IEEE80211_IOCTL_GET(mesh, mesh_ioctl_get80211);
2690
2691static int
2692mesh_ioctl_set80211(struct ieee80211vap *vap, struct ieee80211req *ireq)
2693{
2694	struct ieee80211_mesh_state *ms = vap->iv_mesh;
2695	uint8_t tmpmeshid[IEEE80211_NWID_LEN];
2696	uint8_t tmpaddr[IEEE80211_ADDR_LEN];
2697	char tmpproto[IEEE80211_MESH_PROTO_DSZ];
2698	int error;
2699
2700	if (vap->iv_opmode != IEEE80211_M_MBSS)
2701		return ENOSYS;
2702
2703	error = 0;
2704	switch (ireq->i_type) {
2705	case IEEE80211_IOC_MESH_ID:
2706		if (ireq->i_val != 0 || ireq->i_len > IEEE80211_MESHID_LEN)
2707			return EINVAL;
2708		error = copyin(ireq->i_data, tmpmeshid, ireq->i_len);
2709		if (error != 0)
2710			break;
2711		memset(ms->ms_id, 0, IEEE80211_NWID_LEN);
2712		ms->ms_idlen = ireq->i_len;
2713		memcpy(ms->ms_id, tmpmeshid, ireq->i_len);
2714		error = ENETRESET;
2715		break;
2716	case IEEE80211_IOC_MESH_AP:
2717		if (ireq->i_val)
2718			ms->ms_flags |= IEEE80211_MESHFLAGS_AP;
2719		else
2720			ms->ms_flags &= ~IEEE80211_MESHFLAGS_AP;
2721		error = ENETRESET;
2722		break;
2723	case IEEE80211_IOC_MESH_FWRD:
2724		if (ireq->i_val)
2725			ms->ms_flags |= IEEE80211_MESHFLAGS_FWD;
2726		else
2727			ms->ms_flags &= ~IEEE80211_MESHFLAGS_FWD;
2728		break;
2729	case IEEE80211_IOC_MESH_TTL:
2730		ms->ms_ttl = (uint8_t) ireq->i_val;
2731		break;
2732	case IEEE80211_IOC_MESH_RTCMD:
2733		switch (ireq->i_val) {
2734		case IEEE80211_MESH_RTCMD_LIST:
2735			return EINVAL;
2736		case IEEE80211_MESH_RTCMD_FLUSH:
2737			ieee80211_mesh_rt_flush(vap);
2738			break;
2739		case IEEE80211_MESH_RTCMD_ADD:
2740			if (IEEE80211_ADDR_EQ(vap->iv_myaddr, ireq->i_data) ||
2741			    IEEE80211_ADDR_EQ(broadcastaddr, ireq->i_data))
2742				return EINVAL;
2743			error = copyin(ireq->i_data, &tmpaddr,
2744			    IEEE80211_ADDR_LEN);
2745			if (error == 0)
2746				ieee80211_mesh_discover(vap, tmpaddr, NULL);
2747			break;
2748		case IEEE80211_MESH_RTCMD_DELETE:
2749			ieee80211_mesh_rt_del(vap, ireq->i_data);
2750			break;
2751		default:
2752			return ENOSYS;
2753		}
2754		break;
2755	case IEEE80211_IOC_MESH_PR_METRIC:
2756		error = copyin(ireq->i_data, tmpproto, sizeof(tmpproto));
2757		if (error == 0) {
2758			error = mesh_select_proto_metric(vap, tmpproto);
2759			if (error == 0)
2760				error = ENETRESET;
2761		}
2762		break;
2763	case IEEE80211_IOC_MESH_PR_PATH:
2764		error = copyin(ireq->i_data, tmpproto, sizeof(tmpproto));
2765		if (error == 0) {
2766			error = mesh_select_proto_path(vap, tmpproto);
2767			if (error == 0)
2768				error = ENETRESET;
2769		}
2770		break;
2771	default:
2772		return ENOSYS;
2773	}
2774	return error;
2775}
2776IEEE80211_IOCTL_SET(mesh, mesh_ioctl_set80211);
2777