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