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