ieee80211_mesh.c revision 205277
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 205277 2010-03-18 11:06:38Z 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_recv_ctl(struct ieee80211_node *, struct mbuf *, int);
86static void	mesh_peer_timeout_setup(struct ieee80211_node *);
87static void	mesh_peer_timeout_backoff(struct ieee80211_node *);
88static void	mesh_peer_timeout_cb(void *);
89static __inline void
90		mesh_peer_timeout_stop(struct ieee80211_node *);
91static int	mesh_verify_meshid(struct ieee80211vap *, const uint8_t *);
92static int	mesh_verify_meshconf(struct ieee80211vap *, const uint8_t *);
93static int	mesh_verify_meshpeer(struct ieee80211vap *, uint8_t,
94    		    const uint8_t *);
95uint32_t	mesh_airtime_calc(struct ieee80211_node *);
96
97/*
98 * Timeout values come from the specification and are in milliseconds.
99 */
100SYSCTL_NODE(_net_wlan, OID_AUTO, mesh, CTLFLAG_RD, 0,
101    "IEEE 802.11s parameters");
102static int ieee80211_mesh_retrytimeout = -1;
103SYSCTL_PROC(_net_wlan_mesh, OID_AUTO, retrytimeout, CTLTYPE_INT | CTLFLAG_RW,
104    &ieee80211_mesh_retrytimeout, 0, ieee80211_sysctl_msecs_ticks, "I",
105    "Retry timeout (msec)");
106static int ieee80211_mesh_holdingtimeout = -1;
107SYSCTL_PROC(_net_wlan_mesh, OID_AUTO, holdingtimeout, CTLTYPE_INT | CTLFLAG_RW,
108    &ieee80211_mesh_holdingtimeout, 0, ieee80211_sysctl_msecs_ticks, "I",
109    "Holding state timeout (msec)");
110static int ieee80211_mesh_confirmtimeout = -1;
111SYSCTL_PROC(_net_wlan_mesh, OID_AUTO, confirmtimeout, CTLTYPE_INT | CTLFLAG_RW,
112    &ieee80211_mesh_confirmtimeout, 0, ieee80211_sysctl_msecs_ticks, "I",
113    "Confirm state timeout (msec)");
114static int ieee80211_mesh_maxretries = 2;
115SYSCTL_INT(_net_wlan_mesh, OID_AUTO, maxretries, CTLTYPE_INT | CTLFLAG_RW,
116    &ieee80211_mesh_maxretries, 0,
117    "Maximum retries during peer link establishment");
118
119static const uint8_t broadcastaddr[IEEE80211_ADDR_LEN] =
120	{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
121
122static	ieee80211_recv_action_func mesh_recv_action_meshpeering_open;
123static	ieee80211_recv_action_func mesh_recv_action_meshpeering_confirm;
124static	ieee80211_recv_action_func mesh_recv_action_meshpeering_close;
125static	ieee80211_recv_action_func mesh_recv_action_meshlmetric_req;
126static	ieee80211_recv_action_func mesh_recv_action_meshlmetric_rep;
127
128static	ieee80211_send_action_func mesh_send_action_meshpeering_open;
129static	ieee80211_send_action_func mesh_send_action_meshpeering_confirm;
130static	ieee80211_send_action_func mesh_send_action_meshpeering_close;
131static	ieee80211_send_action_func mesh_send_action_meshlink_request;
132static	ieee80211_send_action_func mesh_send_action_meshlink_reply;
133
134static const struct ieee80211_mesh_proto_metric mesh_metric_airtime = {
135	.mpm_descr	= "AIRTIME",
136	.mpm_ie		= IEEE80211_MESHCONF_METRIC_AIRTIME,
137	.mpm_metric	= mesh_airtime_calc,
138};
139
140static struct ieee80211_mesh_proto_path		mesh_proto_paths[4];
141static struct ieee80211_mesh_proto_metric	mesh_proto_metrics[4];
142
143#define	MESH_RT_LOCK(ms)	mtx_lock(&(ms)->ms_rt_lock)
144#define	MESH_RT_LOCK_ASSERT(ms)	mtx_assert(&(ms)->ms_rt_lock, MA_OWNED)
145#define	MESH_RT_UNLOCK(ms)	mtx_unlock(&(ms)->ms_rt_lock)
146
147MALLOC_DEFINE(M_80211_MESH_RT, "80211mesh", "802.11s routing table");
148
149/*
150 * Helper functions to manipulate the Mesh routing table.
151 */
152
153static struct ieee80211_mesh_route *
154mesh_rt_find_locked(struct ieee80211_mesh_state *ms,
155    const uint8_t dest[IEEE80211_ADDR_LEN])
156{
157	struct ieee80211_mesh_route *rt;
158
159	MESH_RT_LOCK_ASSERT(ms);
160
161	TAILQ_FOREACH(rt, &ms->ms_routes, rt_next) {
162		if (IEEE80211_ADDR_EQ(dest, rt->rt_dest))
163			return rt;
164	}
165	return NULL;
166}
167
168static struct ieee80211_mesh_route *
169mesh_rt_add_locked(struct ieee80211_mesh_state *ms,
170    const uint8_t dest[IEEE80211_ADDR_LEN])
171{
172	struct ieee80211_mesh_route *rt;
173
174	KASSERT(!IEEE80211_ADDR_EQ(broadcastaddr, dest),
175	    ("%s: adding broadcast to the routing table", __func__));
176
177	MESH_RT_LOCK_ASSERT(ms);
178
179	rt = malloc(ALIGN(sizeof(struct ieee80211_mesh_route)) +
180	    ms->ms_ppath->mpp_privlen, M_80211_MESH_RT, M_NOWAIT | M_ZERO);
181	if (rt != NULL) {
182		IEEE80211_ADDR_COPY(rt->rt_dest, dest);
183		rt->rt_priv = (void *)ALIGN(&rt[1]);
184		rt->rt_crtime = ticks;
185		TAILQ_INSERT_TAIL(&ms->ms_routes, rt, rt_next);
186	}
187	return rt;
188}
189
190struct ieee80211_mesh_route *
191ieee80211_mesh_rt_find(struct ieee80211vap *vap,
192    const uint8_t dest[IEEE80211_ADDR_LEN])
193{
194	struct ieee80211_mesh_state *ms = vap->iv_mesh;
195	struct ieee80211_mesh_route *rt;
196
197	MESH_RT_LOCK(ms);
198	rt = mesh_rt_find_locked(ms, dest);
199	MESH_RT_UNLOCK(ms);
200	return rt;
201}
202
203struct ieee80211_mesh_route *
204ieee80211_mesh_rt_add(struct ieee80211vap *vap,
205    const uint8_t dest[IEEE80211_ADDR_LEN])
206{
207	struct ieee80211_mesh_state *ms = vap->iv_mesh;
208	struct ieee80211_mesh_route *rt;
209
210	KASSERT(ieee80211_mesh_rt_find(vap, dest) == NULL,
211	    ("%s: duplicate entry in the routing table", __func__));
212	KASSERT(!IEEE80211_ADDR_EQ(vap->iv_myaddr, dest),
213	    ("%s: adding self to the routing table", __func__));
214
215	MESH_RT_LOCK(ms);
216	rt = mesh_rt_add_locked(ms, dest);
217	MESH_RT_UNLOCK(ms);
218	return rt;
219}
220
221/*
222 * Add a proxy route (as needed) for the specified destination.
223 */
224void
225ieee80211_mesh_proxy_check(struct ieee80211vap *vap,
226    const uint8_t dest[IEEE80211_ADDR_LEN])
227{
228	struct ieee80211_mesh_state *ms = vap->iv_mesh;
229	struct ieee80211_mesh_route *rt;
230
231	MESH_RT_LOCK(ms);
232	rt = mesh_rt_find_locked(ms, dest);
233	if (rt == NULL) {
234		rt = mesh_rt_add_locked(ms, dest);
235		if (rt == NULL) {
236			IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH, dest,
237			    "%s", "unable to add proxy entry");
238			vap->iv_stats.is_mesh_rtaddfailed++;
239		} else {
240			IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH, dest,
241			    "%s", "add proxy entry");
242			IEEE80211_ADDR_COPY(rt->rt_nexthop, vap->iv_myaddr);
243			rt->rt_flags |= IEEE80211_MESHRT_FLAGS_VALID
244				     |  IEEE80211_MESHRT_FLAGS_PROXY;
245		}
246	/* XXX assert PROXY? */
247	} else if ((rt->rt_flags & IEEE80211_MESHRT_FLAGS_VALID) == 0) {
248		struct ieee80211com *ic = vap->iv_ic;
249		/*
250		 * Fix existing entry created by received frames from
251		 * stations that have some memory of dest.  We also
252		 * flush any frames held on the staging queue; delivering
253		 * them is too much trouble right now.
254		 */
255		IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH, dest,
256		    "%s", "fix proxy entry");
257		IEEE80211_ADDR_COPY(rt->rt_nexthop, vap->iv_myaddr);
258		rt->rt_flags |= IEEE80211_MESHRT_FLAGS_VALID
259			     |  IEEE80211_MESHRT_FLAGS_PROXY;
260		/* XXX belongs in hwmp */
261		ieee80211_ageq_drain_node(&ic->ic_stageq,
262		   (void *)(uintptr_t) ieee80211_mac_hash(ic, dest));
263		/* XXX stat? */
264	}
265	MESH_RT_UNLOCK(ms);
266}
267
268static __inline void
269mesh_rt_del(struct ieee80211_mesh_state *ms, struct ieee80211_mesh_route *rt)
270{
271	TAILQ_REMOVE(&ms->ms_routes, rt, rt_next);
272	free(rt, M_80211_MESH_RT);
273}
274
275void
276ieee80211_mesh_rt_del(struct ieee80211vap *vap,
277    const uint8_t dest[IEEE80211_ADDR_LEN])
278{
279	struct ieee80211_mesh_state *ms = vap->iv_mesh;
280	struct ieee80211_mesh_route *rt, *next;
281
282	MESH_RT_LOCK(ms);
283	TAILQ_FOREACH_SAFE(rt, &ms->ms_routes, rt_next, next) {
284		if (IEEE80211_ADDR_EQ(rt->rt_dest, dest)) {
285			mesh_rt_del(ms, rt);
286			MESH_RT_UNLOCK(ms);
287			return;
288		}
289	}
290	MESH_RT_UNLOCK(ms);
291}
292
293void
294ieee80211_mesh_rt_flush(struct ieee80211vap *vap)
295{
296	struct ieee80211_mesh_state *ms = vap->iv_mesh;
297	struct ieee80211_mesh_route *rt, *next;
298
299	if (ms == NULL)
300		return;
301	MESH_RT_LOCK(ms);
302	TAILQ_FOREACH_SAFE(rt, &ms->ms_routes, rt_next, next)
303		mesh_rt_del(ms, rt);
304	MESH_RT_UNLOCK(ms);
305}
306
307void
308ieee80211_mesh_rt_flush_peer(struct ieee80211vap *vap,
309    const uint8_t peer[IEEE80211_ADDR_LEN])
310{
311	struct ieee80211_mesh_state *ms = vap->iv_mesh;
312	struct ieee80211_mesh_route *rt, *next;
313
314	MESH_RT_LOCK(ms);
315	TAILQ_FOREACH_SAFE(rt, &ms->ms_routes, rt_next, next) {
316		if (IEEE80211_ADDR_EQ(rt->rt_nexthop, peer))
317			mesh_rt_del(ms, rt);
318	}
319	MESH_RT_UNLOCK(ms);
320}
321
322/*
323 * Flush expired routing entries, i.e. those in invalid state for
324 * some time.
325 */
326static void
327mesh_rt_flush_invalid(struct ieee80211vap *vap)
328{
329	struct ieee80211_mesh_state *ms = vap->iv_mesh;
330	struct ieee80211_mesh_route *rt, *next;
331
332	if (ms == NULL)
333		return;
334	MESH_RT_LOCK(ms);
335	TAILQ_FOREACH_SAFE(rt, &ms->ms_routes, rt_next, next) {
336		if ((rt->rt_flags & IEEE80211_MESHRT_FLAGS_VALID) == 0 &&
337		    ticks - rt->rt_crtime >= ms->ms_ppath->mpp_inact)
338			mesh_rt_del(ms, rt);
339	}
340	MESH_RT_UNLOCK(ms);
341}
342
343#define	N(a)	(sizeof(a) / sizeof(a[0]))
344int
345ieee80211_mesh_register_proto_path(const struct ieee80211_mesh_proto_path *mpp)
346{
347	int i, firstempty = -1;
348
349	for (i = 0; i < N(mesh_proto_paths); i++) {
350		if (strncmp(mpp->mpp_descr, mesh_proto_paths[i].mpp_descr,
351		    IEEE80211_MESH_PROTO_DSZ) == 0)
352			return EEXIST;
353		if (!mesh_proto_paths[i].mpp_active && firstempty == -1)
354			firstempty = i;
355	}
356	if (firstempty < 0)
357		return ENOSPC;
358	memcpy(&mesh_proto_paths[firstempty], mpp, sizeof(*mpp));
359	mesh_proto_paths[firstempty].mpp_active = 1;
360	return 0;
361}
362
363int
364ieee80211_mesh_register_proto_metric(const struct
365    ieee80211_mesh_proto_metric *mpm)
366{
367	int i, firstempty = -1;
368
369	for (i = 0; i < N(mesh_proto_metrics); i++) {
370		if (strncmp(mpm->mpm_descr, mesh_proto_metrics[i].mpm_descr,
371		    IEEE80211_MESH_PROTO_DSZ) == 0)
372			return EEXIST;
373		if (!mesh_proto_metrics[i].mpm_active && firstempty == -1)
374			firstempty = i;
375	}
376	if (firstempty < 0)
377		return ENOSPC;
378	memcpy(&mesh_proto_metrics[firstempty], mpm, sizeof(*mpm));
379	mesh_proto_metrics[firstempty].mpm_active = 1;
380	return 0;
381}
382
383static int
384mesh_select_proto_path(struct ieee80211vap *vap, const char *name)
385{
386	struct ieee80211_mesh_state *ms = vap->iv_mesh;
387	int i;
388
389	for (i = 0; i < N(mesh_proto_paths); i++) {
390		if (strcasecmp(mesh_proto_paths[i].mpp_descr, name) == 0) {
391			ms->ms_ppath = &mesh_proto_paths[i];
392			return 0;
393		}
394	}
395	return ENOENT;
396}
397
398static int
399mesh_select_proto_metric(struct ieee80211vap *vap, const char *name)
400{
401	struct ieee80211_mesh_state *ms = vap->iv_mesh;
402	int i;
403
404	for (i = 0; i < N(mesh_proto_metrics); i++) {
405		if (strcasecmp(mesh_proto_metrics[i].mpm_descr, name) == 0) {
406			ms->ms_pmetric = &mesh_proto_metrics[i];
407			return 0;
408		}
409	}
410	return ENOENT;
411}
412#undef	N
413
414static void
415ieee80211_mesh_init(void)
416{
417
418	memset(mesh_proto_paths, 0, sizeof(mesh_proto_paths));
419	memset(mesh_proto_metrics, 0, sizeof(mesh_proto_metrics));
420
421	/*
422	 * Setup mesh parameters that depends on the clock frequency.
423	 */
424	ieee80211_mesh_retrytimeout = msecs_to_ticks(40);
425	ieee80211_mesh_holdingtimeout = msecs_to_ticks(40);
426	ieee80211_mesh_confirmtimeout = msecs_to_ticks(40);
427
428	/*
429	 * Register action frame handlers.
430	 */
431	ieee80211_recv_action_register(IEEE80211_ACTION_CAT_MESHPEERING,
432	    IEEE80211_ACTION_MESHPEERING_OPEN,
433	    mesh_recv_action_meshpeering_open);
434	ieee80211_recv_action_register(IEEE80211_ACTION_CAT_MESHPEERING,
435	    IEEE80211_ACTION_MESHPEERING_CONFIRM,
436	    mesh_recv_action_meshpeering_confirm);
437	ieee80211_recv_action_register(IEEE80211_ACTION_CAT_MESHPEERING,
438	    IEEE80211_ACTION_MESHPEERING_CLOSE,
439	    mesh_recv_action_meshpeering_close);
440	ieee80211_recv_action_register(IEEE80211_ACTION_CAT_MESHLMETRIC,
441	    IEEE80211_ACTION_MESHLMETRIC_REQ, mesh_recv_action_meshlmetric_req);
442	ieee80211_recv_action_register(IEEE80211_ACTION_CAT_MESHLMETRIC,
443	    IEEE80211_ACTION_MESHLMETRIC_REP, mesh_recv_action_meshlmetric_rep);
444
445	ieee80211_send_action_register(IEEE80211_ACTION_CAT_MESHPEERING,
446	    IEEE80211_ACTION_MESHPEERING_OPEN,
447	    mesh_send_action_meshpeering_open);
448	ieee80211_send_action_register(IEEE80211_ACTION_CAT_MESHPEERING,
449	    IEEE80211_ACTION_MESHPEERING_CONFIRM,
450	    mesh_send_action_meshpeering_confirm);
451	ieee80211_send_action_register(IEEE80211_ACTION_CAT_MESHPEERING,
452	    IEEE80211_ACTION_MESHPEERING_CLOSE,
453	    mesh_send_action_meshpeering_close);
454	ieee80211_send_action_register(IEEE80211_ACTION_CAT_MESHLMETRIC,
455	    IEEE80211_ACTION_MESHLMETRIC_REQ,
456	    mesh_send_action_meshlink_request);
457	ieee80211_send_action_register(IEEE80211_ACTION_CAT_MESHLMETRIC,
458	    IEEE80211_ACTION_MESHLMETRIC_REP,
459	    mesh_send_action_meshlink_reply);
460
461	/*
462	 * Register Airtime Link Metric.
463	 */
464	ieee80211_mesh_register_proto_metric(&mesh_metric_airtime);
465
466}
467SYSINIT(wlan_mesh, SI_SUB_DRIVERS, SI_ORDER_FIRST, ieee80211_mesh_init, NULL);
468
469void
470ieee80211_mesh_attach(struct ieee80211com *ic)
471{
472	ic->ic_vattach[IEEE80211_M_MBSS] = mesh_vattach;
473}
474
475void
476ieee80211_mesh_detach(struct ieee80211com *ic)
477{
478}
479
480static void
481mesh_vdetach_peers(void *arg, struct ieee80211_node *ni)
482{
483	struct ieee80211com *ic = ni->ni_ic;
484	uint16_t args[3];
485
486	if (ni->ni_mlstate == IEEE80211_NODE_MESH_ESTABLISHED) {
487		args[0] = ni->ni_mlpid;
488		args[1] = ni->ni_mllid;
489		args[2] = IEEE80211_REASON_PEER_LINK_CANCELED;
490		ieee80211_send_action(ni,
491		    IEEE80211_ACTION_CAT_MESHPEERING,
492		    IEEE80211_ACTION_MESHPEERING_CLOSE,
493		    args);
494	}
495	callout_drain(&ni->ni_mltimer);
496	/* XXX belongs in hwmp */
497	ieee80211_ageq_drain_node(&ic->ic_stageq,
498	   (void *)(uintptr_t) ieee80211_mac_hash(ic, ni->ni_macaddr));
499}
500
501static void
502mesh_vdetach(struct ieee80211vap *vap)
503{
504	struct ieee80211_mesh_state *ms = vap->iv_mesh;
505
506	callout_drain(&ms->ms_cleantimer);
507	ieee80211_iterate_nodes(&vap->iv_ic->ic_sta, mesh_vdetach_peers,
508	    NULL);
509	ieee80211_mesh_rt_flush(vap);
510	mtx_destroy(&ms->ms_rt_lock);
511	ms->ms_ppath->mpp_vdetach(vap);
512	free(vap->iv_mesh, M_80211_VAP);
513	vap->iv_mesh = NULL;
514}
515
516static void
517mesh_vattach(struct ieee80211vap *vap)
518{
519	struct ieee80211_mesh_state *ms;
520	vap->iv_newstate = mesh_newstate;
521	vap->iv_input = mesh_input;
522	vap->iv_opdetach = mesh_vdetach;
523	vap->iv_recv_mgmt = mesh_recv_mgmt;
524	vap->iv_recv_ctl = mesh_recv_ctl;
525	ms = malloc(sizeof(struct ieee80211_mesh_state), M_80211_VAP,
526	    M_NOWAIT | M_ZERO);
527	if (ms == NULL) {
528		printf("%s: couldn't alloc MBSS state\n", __func__);
529		return;
530	}
531	vap->iv_mesh = ms;
532	ms->ms_seq = 0;
533	ms->ms_flags = (IEEE80211_MESHFLAGS_AP | IEEE80211_MESHFLAGS_FWD);
534	ms->ms_ttl = IEEE80211_MESH_DEFAULT_TTL;
535	TAILQ_INIT(&ms->ms_routes);
536	mtx_init(&ms->ms_rt_lock, "MBSS", "802.11s routing table", MTX_DEF);
537	callout_init(&ms->ms_cleantimer, CALLOUT_MPSAFE);
538	mesh_select_proto_metric(vap, "AIRTIME");
539	KASSERT(ms->ms_pmetric, ("ms_pmetric == NULL"));
540	mesh_select_proto_path(vap, "HWMP");
541	KASSERT(ms->ms_ppath, ("ms_ppath == NULL"));
542	ms->ms_ppath->mpp_vattach(vap);
543}
544
545/*
546 * IEEE80211_M_MBSS vap state machine handler.
547 */
548static int
549mesh_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
550{
551	struct ieee80211_mesh_state *ms = vap->iv_mesh;
552	struct ieee80211com *ic = vap->iv_ic;
553	struct ieee80211_node *ni;
554	enum ieee80211_state ostate;
555
556	IEEE80211_LOCK_ASSERT(ic);
557
558	ostate = vap->iv_state;
559	IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE, "%s: %s -> %s (%d)\n",
560	    __func__, ieee80211_state_name[ostate],
561	    ieee80211_state_name[nstate], arg);
562	vap->iv_state = nstate;		/* state transition */
563	if (ostate != IEEE80211_S_SCAN)
564		ieee80211_cancel_scan(vap);	/* background scan */
565	ni = vap->iv_bss;			/* NB: no reference held */
566	if (nstate != IEEE80211_S_RUN && ostate == IEEE80211_S_RUN)
567		callout_drain(&ms->ms_cleantimer);
568	switch (nstate) {
569	case IEEE80211_S_INIT:
570		switch (ostate) {
571		case IEEE80211_S_SCAN:
572			ieee80211_cancel_scan(vap);
573			break;
574		case IEEE80211_S_CAC:
575			ieee80211_dfs_cac_stop(vap);
576			break;
577		case IEEE80211_S_RUN:
578			ieee80211_iterate_nodes(&ic->ic_sta,
579			    mesh_vdetach_peers, NULL);
580			break;
581		default:
582			break;
583		}
584		if (ostate != IEEE80211_S_INIT) {
585			/* NB: optimize INIT -> INIT case */
586			ieee80211_reset_bss(vap);
587			ieee80211_mesh_rt_flush(vap);
588		}
589		break;
590	case IEEE80211_S_SCAN:
591		switch (ostate) {
592		case IEEE80211_S_INIT:
593			if (vap->iv_des_chan != IEEE80211_CHAN_ANYC &&
594			    !IEEE80211_IS_CHAN_RADAR(vap->iv_des_chan) &&
595			    ms->ms_idlen != 0) {
596				/*
597				 * Already have a channel and a mesh ID; bypass
598				 * the scan and startup immediately.
599				 */
600				ieee80211_create_ibss(vap, vap->iv_des_chan);
601				break;
602			}
603			/*
604			 * Initiate a scan.  We can come here as a result
605			 * of an IEEE80211_IOC_SCAN_REQ too in which case
606			 * the vap will be marked with IEEE80211_FEXT_SCANREQ
607			 * and the scan request parameters will be present
608			 * in iv_scanreq.  Otherwise we do the default.
609			*/
610			if (vap->iv_flags_ext & IEEE80211_FEXT_SCANREQ) {
611				ieee80211_check_scan(vap,
612				    vap->iv_scanreq_flags,
613				    vap->iv_scanreq_duration,
614				    vap->iv_scanreq_mindwell,
615				    vap->iv_scanreq_maxdwell,
616				    vap->iv_scanreq_nssid, vap->iv_scanreq_ssid);
617				vap->iv_flags_ext &= ~IEEE80211_FEXT_SCANREQ;
618			} else
619				ieee80211_check_scan_current(vap);
620			break;
621		default:
622			break;
623		}
624		break;
625	case IEEE80211_S_CAC:
626		/*
627		 * Start CAC on a DFS channel.  We come here when starting
628		 * a bss on a DFS channel (see ieee80211_create_ibss).
629		 */
630		ieee80211_dfs_cac_start(vap);
631		break;
632	case IEEE80211_S_RUN:
633		switch (ostate) {
634		case IEEE80211_S_INIT:
635			/*
636			 * Already have a channel; bypass the
637			 * scan and startup immediately.
638			 * Note that ieee80211_create_ibss will call
639			 * back to do a RUN->RUN state change.
640			 */
641			ieee80211_create_ibss(vap,
642			    ieee80211_ht_adjust_channel(ic,
643				ic->ic_curchan, vap->iv_flags_ht));
644			/* NB: iv_bss is changed on return */
645			break;
646		case IEEE80211_S_CAC:
647			/*
648			 * NB: This is the normal state change when CAC
649			 * expires and no radar was detected; no need to
650			 * clear the CAC timer as it's already expired.
651			 */
652			/* fall thru... */
653		case IEEE80211_S_CSA:
654#if 0
655			/*
656			 * Shorten inactivity timer of associated stations
657			 * to weed out sta's that don't follow a CSA.
658			 */
659			ieee80211_iterate_nodes(&ic->ic_sta, sta_csa, vap);
660#endif
661			/*
662			 * Update bss node channel to reflect where
663			 * we landed after CSA.
664			 */
665			ieee80211_node_set_chan(vap->iv_bss,
666			    ieee80211_ht_adjust_channel(ic, ic->ic_curchan,
667				ieee80211_htchanflags(vap->iv_bss->ni_chan)));
668			/* XXX bypass debug msgs */
669			break;
670		case IEEE80211_S_SCAN:
671		case IEEE80211_S_RUN:
672#ifdef IEEE80211_DEBUG
673			if (ieee80211_msg_debug(vap)) {
674				struct ieee80211_node *ni = vap->iv_bss;
675				ieee80211_note(vap,
676				    "synchronized with %s meshid ",
677				    ether_sprintf(ni->ni_meshid));
678				ieee80211_print_essid(ni->ni_meshid,
679				    ni->ni_meshidlen);
680				/* XXX MCS/HT */
681				printf(" channel %d\n",
682				    ieee80211_chan2ieee(ic, ic->ic_curchan));
683			}
684#endif
685			break;
686		default:
687			break;
688		}
689		ieee80211_node_authorize(vap->iv_bss);
690		callout_reset(&ms->ms_cleantimer, ms->ms_ppath->mpp_inact,
691                    mesh_rt_cleanup_cb, vap);
692		break;
693	default:
694		break;
695	}
696	/* NB: ostate not nstate */
697	ms->ms_ppath->mpp_newstate(vap, ostate, arg);
698	return 0;
699}
700
701static void
702mesh_rt_cleanup_cb(void *arg)
703{
704	struct ieee80211vap *vap = arg;
705	struct ieee80211_mesh_state *ms = vap->iv_mesh;
706
707	mesh_rt_flush_invalid(vap);
708	callout_reset(&ms->ms_cleantimer, ms->ms_ppath->mpp_inact,
709	    mesh_rt_cleanup_cb, vap);
710}
711
712
713/*
714 * Helper function to note the Mesh Peer Link FSM change.
715 */
716static void
717mesh_linkchange(struct ieee80211_node *ni, enum ieee80211_mesh_mlstate state)
718{
719	struct ieee80211vap *vap = ni->ni_vap;
720	struct ieee80211_mesh_state *ms = vap->iv_mesh;
721#ifdef IEEE80211_DEBUG
722	static const char *meshlinkstates[] = {
723		[IEEE80211_NODE_MESH_IDLE]		= "IDLE",
724		[IEEE80211_NODE_MESH_OPENSNT]		= "OPEN SENT",
725		[IEEE80211_NODE_MESH_OPENRCV]		= "OPEN RECEIVED",
726		[IEEE80211_NODE_MESH_CONFIRMRCV]	= "CONFIRM RECEIVED",
727		[IEEE80211_NODE_MESH_ESTABLISHED]	= "ESTABLISHED",
728		[IEEE80211_NODE_MESH_HOLDING]		= "HOLDING"
729	};
730#endif
731	IEEE80211_NOTE(vap, IEEE80211_MSG_MESH,
732	    ni, "peer link: %s -> %s",
733	    meshlinkstates[ni->ni_mlstate], meshlinkstates[state]);
734
735	/* track neighbor count */
736	if (state == IEEE80211_NODE_MESH_ESTABLISHED &&
737	    ni->ni_mlstate != IEEE80211_NODE_MESH_ESTABLISHED) {
738		KASSERT(ms->ms_neighbors < 65535, ("neighbor count overflow"));
739		ms->ms_neighbors++;
740		ieee80211_beacon_notify(vap, IEEE80211_BEACON_MESHCONF);
741	} else if (ni->ni_mlstate == IEEE80211_NODE_MESH_ESTABLISHED &&
742	    state != IEEE80211_NODE_MESH_ESTABLISHED) {
743		KASSERT(ms->ms_neighbors > 0, ("neighbor count 0"));
744		ms->ms_neighbors--;
745		ieee80211_beacon_notify(vap, IEEE80211_BEACON_MESHCONF);
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
1480		/* XXX find a better class or define it's own */
1481		IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_INPUT, wh->i_addr2,
1482		    "%s", "recv probe req");
1483		/*
1484		 * Some legacy 11b clients cannot hack a complete
1485		 * probe response frame.  When the request includes
1486		 * only a bare-bones rate set, communicate this to
1487		 * the transmit side.
1488		 */
1489		ieee80211_send_proberesp(vap, wh->i_addr2, 0);
1490		break;
1491	}
1492	case IEEE80211_FC0_SUBTYPE_ACTION:
1493		if (vap->iv_state != IEEE80211_S_RUN) {
1494			vap->iv_stats.is_rx_mgtdiscard++;
1495			break;
1496		}
1497		/*
1498		 * We received an action for an unknown neighbor.
1499		 * XXX: wait for it to beacon or create ieee80211_node?
1500		 */
1501		if (ni == vap->iv_bss) {
1502			IEEE80211_DISCARD(vap, IEEE80211_MSG_MESH,
1503			    wh, NULL, "%s", "unknown node");
1504			vap->iv_stats.is_rx_mgtdiscard++;
1505			break;
1506		}
1507		/*
1508		 * Discard if not for us.
1509		 */
1510		if (!IEEE80211_ADDR_EQ(vap->iv_myaddr, wh->i_addr1) &&
1511		    !IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1512			IEEE80211_DISCARD(vap, IEEE80211_MSG_MESH,
1513			    wh, NULL, "%s", "not for me");
1514			vap->iv_stats.is_rx_mgtdiscard++;
1515			break;
1516		}
1517		/* XXX parse_action is a bit useless now */
1518		if (ieee80211_parse_action(ni, m0) == 0)
1519			ic->ic_recv_action(ni, wh, frm, efrm);
1520		break;
1521	case IEEE80211_FC0_SUBTYPE_AUTH:
1522	case IEEE80211_FC0_SUBTYPE_ASSOC_REQ:
1523	case IEEE80211_FC0_SUBTYPE_REASSOC_REQ:
1524	case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
1525	case IEEE80211_FC0_SUBTYPE_REASSOC_RESP:
1526	case IEEE80211_FC0_SUBTYPE_DEAUTH:
1527	case IEEE80211_FC0_SUBTYPE_DISASSOC:
1528		IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
1529		    wh, NULL, "%s", "not handled");
1530		vap->iv_stats.is_rx_mgtdiscard++;
1531		return;
1532	default:
1533		IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
1534		    wh, "mgt", "subtype 0x%x not handled", subtype);
1535		vap->iv_stats.is_rx_badsubtype++;
1536		break;
1537	}
1538}
1539
1540static void
1541mesh_recv_ctl(struct ieee80211_node *ni, struct mbuf *m, int subtype)
1542{
1543
1544	switch (subtype) {
1545	case IEEE80211_FC0_SUBTYPE_BAR:
1546		ieee80211_recv_bar(ni, m);
1547		break;
1548	}
1549}
1550
1551/*
1552 * Parse meshpeering action ie's for open+confirm frames; the
1553 * important bits are returned in the supplied structure.
1554 */
1555static const struct ieee80211_meshpeer_ie *
1556mesh_parse_meshpeering_action(struct ieee80211_node *ni,
1557	const struct ieee80211_frame *wh,	/* XXX for VERIFY_LENGTH */
1558	const uint8_t *frm, const uint8_t *efrm,
1559	struct ieee80211_meshpeer_ie *mp, uint8_t subtype)
1560{
1561	struct ieee80211vap *vap = ni->ni_vap;
1562	const struct ieee80211_meshpeer_ie *mpie;
1563	const uint8_t *meshid, *meshconf, *meshpeer;
1564
1565	meshid = meshconf = meshpeer = NULL;
1566	while (efrm - frm > 1) {
1567		IEEE80211_VERIFY_LENGTH(efrm - frm, frm[1] + 2, return NULL);
1568		switch (*frm) {
1569		case IEEE80211_ELEMID_MESHID:
1570			meshid = frm;
1571			break;
1572		case IEEE80211_ELEMID_MESHCONF:
1573			meshconf = frm;
1574			break;
1575		case IEEE80211_ELEMID_MESHPEER:
1576			meshpeer = frm;
1577			mpie = (const struct ieee80211_meshpeer_ie *) frm;
1578			memset(mp, 0, sizeof(*mp));
1579			mp->peer_llinkid = LE_READ_2(&mpie->peer_llinkid);
1580			/* NB: peer link ID is optional on these frames */
1581			if (subtype == IEEE80211_MESH_PEER_LINK_CLOSE &&
1582			    mpie->peer_len == 8) {
1583				mp->peer_linkid = 0;
1584				mp->peer_rcode = LE_READ_2(&mpie->peer_linkid);
1585			} else {
1586				mp->peer_linkid = LE_READ_2(&mpie->peer_linkid);
1587				mp->peer_rcode = LE_READ_2(&mpie->peer_rcode);
1588			}
1589			break;
1590		}
1591		frm += frm[1] + 2;
1592	}
1593
1594	/*
1595	 * Verify the contents of the frame. Action frames with
1596	 * close subtype don't have a Mesh Configuration IE.
1597	 * If if fails validation, close the peer link.
1598	 */
1599	KASSERT(meshpeer != NULL &&
1600	    subtype != IEEE80211_ACTION_MESHPEERING_CLOSE,
1601	    ("parsing close action"));
1602
1603	if (mesh_verify_meshid(vap, meshid) ||
1604	    mesh_verify_meshpeer(vap, subtype, meshpeer) ||
1605	    mesh_verify_meshconf(vap, meshconf)) {
1606		uint16_t args[3];
1607
1608		IEEE80211_DISCARD(vap,
1609		    IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH,
1610		    wh, NULL, "%s", "not for our mesh");
1611		vap->iv_stats.is_rx_mgtdiscard++;
1612		switch (ni->ni_mlstate) {
1613		case IEEE80211_NODE_MESH_IDLE:
1614		case IEEE80211_NODE_MESH_ESTABLISHED:
1615		case IEEE80211_NODE_MESH_HOLDING:
1616			/* ignore */
1617			break;
1618		case IEEE80211_NODE_MESH_OPENSNT:
1619		case IEEE80211_NODE_MESH_OPENRCV:
1620		case IEEE80211_NODE_MESH_CONFIRMRCV:
1621			args[0] = ni->ni_mlpid;
1622			args[1] = ni->ni_mllid;
1623			args[2] = IEEE80211_REASON_PEER_LINK_CANCELED;
1624			ieee80211_send_action(ni,
1625			    IEEE80211_ACTION_CAT_MESHPEERING,
1626			    IEEE80211_ACTION_MESHPEERING_CLOSE,
1627			    args);
1628			mesh_linkchange(ni, IEEE80211_NODE_MESH_HOLDING);
1629			mesh_peer_timeout_setup(ni);
1630			break;
1631		}
1632		return NULL;
1633	}
1634	return (const struct ieee80211_meshpeer_ie *) mp;
1635}
1636
1637static int
1638mesh_recv_action_meshpeering_open(struct ieee80211_node *ni,
1639	const struct ieee80211_frame *wh,
1640	const uint8_t *frm, const uint8_t *efrm)
1641{
1642	struct ieee80211vap *vap = ni->ni_vap;
1643	struct ieee80211_meshpeer_ie ie;
1644	const struct ieee80211_meshpeer_ie *meshpeer;
1645	uint16_t args[3];
1646
1647	/* +2+2 for action + code + capabilites */
1648	meshpeer = mesh_parse_meshpeering_action(ni, wh, frm+2+2, efrm, &ie,
1649	    IEEE80211_ACTION_MESHPEERING_OPEN);
1650	if (meshpeer == NULL) {
1651		return 0;
1652	}
1653
1654	/* XXX move up */
1655	IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH, ni,
1656	    "recv PEER OPEN, lid 0x%x", meshpeer->peer_llinkid);
1657
1658	switch (ni->ni_mlstate) {
1659	case IEEE80211_NODE_MESH_IDLE:
1660		mesh_linkchange(ni, IEEE80211_NODE_MESH_OPENRCV);
1661		ni->ni_mllid = meshpeer->peer_llinkid;
1662		ni->ni_mlpid = mesh_generateid(vap);
1663		if (ni->ni_mlpid == 0)
1664			return 0;		/* XXX */
1665		args[0] = ni->ni_mlpid;
1666		/* Announce we're open too... */
1667		ieee80211_send_action(ni,
1668		    IEEE80211_ACTION_CAT_MESHPEERING,
1669		    IEEE80211_ACTION_MESHPEERING_OPEN, args);
1670		/* ...and confirm the link. */
1671		args[0] = ni->ni_mlpid;
1672		args[1] = ni->ni_mllid;
1673		ieee80211_send_action(ni,
1674		    IEEE80211_ACTION_CAT_MESHPEERING,
1675		    IEEE80211_ACTION_MESHPEERING_CONFIRM,
1676		    args);
1677		mesh_peer_timeout_setup(ni);
1678		break;
1679	case IEEE80211_NODE_MESH_OPENRCV:
1680		/* Wrong Link ID */
1681		if (ni->ni_mllid != meshpeer->peer_llinkid) {
1682			args[0] = ni->ni_mllid;
1683			args[1] = ni->ni_mlpid;
1684			args[2] = IEEE80211_REASON_PEER_LINK_CANCELED;
1685			ieee80211_send_action(ni,
1686			    IEEE80211_ACTION_CAT_MESHPEERING,
1687			    IEEE80211_ACTION_MESHPEERING_CLOSE,
1688			    args);
1689			mesh_linkchange(ni, IEEE80211_NODE_MESH_HOLDING);
1690			mesh_peer_timeout_setup(ni);
1691			break;
1692		}
1693		/* Duplicate open, confirm again. */
1694		args[0] = ni->ni_mlpid;
1695		args[1] = ni->ni_mllid;
1696		ieee80211_send_action(ni,
1697		    IEEE80211_ACTION_CAT_MESHPEERING,
1698		    IEEE80211_ACTION_MESHPEERING_CONFIRM,
1699		    args);
1700		break;
1701	case IEEE80211_NODE_MESH_OPENSNT:
1702		ni->ni_mllid = meshpeer->peer_llinkid;
1703		mesh_linkchange(ni, IEEE80211_NODE_MESH_OPENRCV);
1704		args[0] = ni->ni_mlpid;
1705		args[1] = ni->ni_mllid;
1706		ieee80211_send_action(ni,
1707		    IEEE80211_ACTION_CAT_MESHPEERING,
1708		    IEEE80211_ACTION_MESHPEERING_CONFIRM,
1709		    args);
1710		/* NB: don't setup/clear any timeout */
1711		break;
1712	case IEEE80211_NODE_MESH_CONFIRMRCV:
1713		if (ni->ni_mlpid != meshpeer->peer_linkid ||
1714		    ni->ni_mllid != meshpeer->peer_llinkid) {
1715			args[0] = ni->ni_mlpid;
1716			args[1] = ni->ni_mllid;
1717			args[2] = IEEE80211_REASON_PEER_LINK_CANCELED;
1718			ieee80211_send_action(ni,
1719			    IEEE80211_ACTION_CAT_MESHPEERING,
1720			    IEEE80211_ACTION_MESHPEERING_CLOSE,
1721			    args);
1722			mesh_linkchange(ni,
1723			    IEEE80211_NODE_MESH_HOLDING);
1724			mesh_peer_timeout_setup(ni);
1725			break;
1726		}
1727		mesh_linkchange(ni, IEEE80211_NODE_MESH_ESTABLISHED);
1728		ni->ni_mllid = meshpeer->peer_llinkid;
1729		args[0] = ni->ni_mlpid;
1730		args[1] = ni->ni_mllid;
1731		ieee80211_send_action(ni,
1732		    IEEE80211_ACTION_CAT_MESHPEERING,
1733		    IEEE80211_ACTION_MESHPEERING_CONFIRM,
1734		    args);
1735		mesh_peer_timeout_stop(ni);
1736		break;
1737	case IEEE80211_NODE_MESH_ESTABLISHED:
1738		if (ni->ni_mllid != meshpeer->peer_llinkid) {
1739			args[0] = ni->ni_mllid;
1740			args[1] = ni->ni_mlpid;
1741			args[2] = IEEE80211_REASON_PEER_LINK_CANCELED;
1742			ieee80211_send_action(ni,
1743			    IEEE80211_ACTION_CAT_MESHPEERING,
1744			    IEEE80211_ACTION_MESHPEERING_CLOSE,
1745			    args);
1746			mesh_linkchange(ni, IEEE80211_NODE_MESH_HOLDING);
1747			mesh_peer_timeout_setup(ni);
1748			break;
1749		}
1750		args[0] = ni->ni_mlpid;
1751		args[1] = ni->ni_mllid;
1752		ieee80211_send_action(ni,
1753		    IEEE80211_ACTION_CAT_MESHPEERING,
1754		    IEEE80211_ACTION_MESHPEERING_CONFIRM,
1755		    args);
1756		break;
1757	case IEEE80211_NODE_MESH_HOLDING:
1758		args[0] = ni->ni_mlpid;
1759		args[1] = meshpeer->peer_llinkid;
1760		args[2] = IEEE80211_REASON_MESH_MAX_RETRIES;
1761		ieee80211_send_action(ni,
1762		    IEEE80211_ACTION_CAT_MESHPEERING,
1763		    IEEE80211_ACTION_MESHPEERING_CLOSE,
1764		    args);
1765		break;
1766	}
1767	return 0;
1768}
1769
1770static int
1771mesh_recv_action_meshpeering_confirm(struct ieee80211_node *ni,
1772	const struct ieee80211_frame *wh,
1773	const uint8_t *frm, const uint8_t *efrm)
1774{
1775	struct ieee80211vap *vap = ni->ni_vap;
1776	struct ieee80211_meshpeer_ie ie;
1777	const struct ieee80211_meshpeer_ie *meshpeer;
1778	uint16_t args[3];
1779
1780	/* +2+2+2+2 for action + code + capabilites + status code + AID */
1781	meshpeer = mesh_parse_meshpeering_action(ni, wh, frm+2+2+2+2, efrm, &ie,
1782	    IEEE80211_ACTION_MESHPEERING_CONFIRM);
1783	if (meshpeer == NULL) {
1784		return 0;
1785	}
1786
1787	IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH, ni,
1788	    "recv PEER CONFIRM, local id 0x%x, peer id 0x%x",
1789	    meshpeer->peer_llinkid, meshpeer->peer_linkid);
1790
1791	switch (ni->ni_mlstate) {
1792	case IEEE80211_NODE_MESH_OPENRCV:
1793		mesh_linkchange(ni, IEEE80211_NODE_MESH_ESTABLISHED);
1794		mesh_peer_timeout_stop(ni);
1795		break;
1796	case IEEE80211_NODE_MESH_OPENSNT:
1797		mesh_linkchange(ni, IEEE80211_NODE_MESH_CONFIRMRCV);
1798		break;
1799	case IEEE80211_NODE_MESH_HOLDING:
1800		args[0] = ni->ni_mlpid;
1801		args[1] = meshpeer->peer_llinkid;
1802		args[2] = IEEE80211_REASON_MESH_MAX_RETRIES;
1803		ieee80211_send_action(ni,
1804		    IEEE80211_ACTION_CAT_MESHPEERING,
1805		    IEEE80211_ACTION_MESHPEERING_CLOSE,
1806		    args);
1807		break;
1808	case IEEE80211_NODE_MESH_CONFIRMRCV:
1809		if (ni->ni_mllid != meshpeer->peer_llinkid) {
1810			args[0] = ni->ni_mlpid;
1811			args[1] = ni->ni_mllid;
1812			args[2] = IEEE80211_REASON_PEER_LINK_CANCELED;
1813			ieee80211_send_action(ni,
1814			    IEEE80211_ACTION_CAT_MESHPEERING,
1815			    IEEE80211_ACTION_MESHPEERING_CLOSE,
1816			    args);
1817			mesh_linkchange(ni, IEEE80211_NODE_MESH_HOLDING);
1818			mesh_peer_timeout_setup(ni);
1819		}
1820		break;
1821	default:
1822		IEEE80211_DISCARD(vap,
1823		    IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH,
1824		    wh, NULL, "received confirm in invalid state %d",
1825		    ni->ni_mlstate);
1826		vap->iv_stats.is_rx_mgtdiscard++;
1827		break;
1828	}
1829	return 0;
1830}
1831
1832static int
1833mesh_recv_action_meshpeering_close(struct ieee80211_node *ni,
1834	const struct ieee80211_frame *wh,
1835	const uint8_t *frm, const uint8_t *efrm)
1836{
1837	uint16_t args[3];
1838
1839	IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH,
1840	    ni, "%s", "recv PEER CLOSE");
1841
1842	switch (ni->ni_mlstate) {
1843	case IEEE80211_NODE_MESH_IDLE:
1844		/* ignore */
1845		break;
1846	case IEEE80211_NODE_MESH_OPENRCV:
1847	case IEEE80211_NODE_MESH_OPENSNT:
1848	case IEEE80211_NODE_MESH_CONFIRMRCV:
1849	case IEEE80211_NODE_MESH_ESTABLISHED:
1850		args[0] = ni->ni_mlpid;
1851		args[1] = ni->ni_mllid;
1852		args[2] = IEEE80211_REASON_MESH_CLOSE_RCVD;
1853		ieee80211_send_action(ni,
1854		    IEEE80211_ACTION_CAT_MESHPEERING,
1855		    IEEE80211_ACTION_MESHPEERING_CLOSE,
1856		    args);
1857		mesh_linkchange(ni, IEEE80211_NODE_MESH_HOLDING);
1858		mesh_peer_timeout_setup(ni);
1859		break;
1860	case IEEE80211_NODE_MESH_HOLDING:
1861		mesh_linkchange(ni, IEEE80211_NODE_MESH_IDLE);
1862		mesh_peer_timeout_setup(ni);
1863		break;
1864	}
1865	return 0;
1866}
1867
1868/*
1869 * Link Metric handling.
1870 */
1871static int
1872mesh_recv_action_meshlmetric_req(struct ieee80211_node *ni,
1873	const struct ieee80211_frame *wh,
1874	const uint8_t *frm, const uint8_t *efrm)
1875{
1876	uint32_t metric;
1877
1878	metric = mesh_airtime_calc(ni);
1879	ieee80211_send_action(ni,
1880	    IEEE80211_ACTION_CAT_MESHLMETRIC,
1881	    IEEE80211_ACTION_MESHLMETRIC_REP,
1882	    &metric);
1883	return 0;
1884}
1885
1886static int
1887mesh_recv_action_meshlmetric_rep(struct ieee80211_node *ni,
1888	const struct ieee80211_frame *wh,
1889	const uint8_t *frm, const uint8_t *efrm)
1890{
1891	return 0;
1892}
1893
1894static int
1895mesh_send_action(struct ieee80211_node *ni, struct mbuf *m)
1896{
1897	struct ieee80211_bpf_params params;
1898
1899	memset(&params, 0, sizeof(params));
1900	params.ibp_pri = WME_AC_VO;
1901	params.ibp_rate0 = ni->ni_txparms->mgmtrate;
1902	/* XXX ucast/mcast */
1903	params.ibp_try0 = ni->ni_txparms->maxretry;
1904	params.ibp_power = ni->ni_txpower;
1905	return ieee80211_mgmt_output(ni, m, IEEE80211_FC0_SUBTYPE_ACTION,
1906	     &params);
1907}
1908
1909#define	ADDSHORT(frm, v) do {			\
1910	frm[0] = (v) & 0xff;			\
1911	frm[1] = (v) >> 8;			\
1912	frm += 2;				\
1913} while (0)
1914#define	ADDWORD(frm, v) do {			\
1915	frm[0] = (v) & 0xff;			\
1916	frm[1] = ((v) >> 8) & 0xff;		\
1917	frm[2] = ((v) >> 16) & 0xff;		\
1918	frm[3] = ((v) >> 24) & 0xff;		\
1919	frm += 4;				\
1920} while (0)
1921
1922static int
1923mesh_send_action_meshpeering_open(struct ieee80211_node *ni,
1924	int category, int action, void *args0)
1925{
1926	struct ieee80211vap *vap = ni->ni_vap;
1927	struct ieee80211com *ic = ni->ni_ic;
1928	uint16_t *args = args0;
1929	const struct ieee80211_rateset *rs;
1930	struct mbuf *m;
1931	uint8_t *frm;
1932
1933	IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH, ni,
1934	    "send PEER OPEN action: localid 0x%x", args[0]);
1935
1936	IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
1937	    "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", __func__, __LINE__,
1938	    ni, ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)+1);
1939	ieee80211_ref_node(ni);
1940
1941	m = ieee80211_getmgtframe(&frm,
1942	    ic->ic_headroom + sizeof(struct ieee80211_frame),
1943	    sizeof(uint16_t)	/* action+category */
1944	    + sizeof(uint16_t)	/* capabilites */
1945	    + 2 + IEEE80211_RATE_SIZE
1946	    + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
1947	    + 2 + IEEE80211_MESHID_LEN
1948	    + sizeof(struct ieee80211_meshconf_ie)
1949	    + sizeof(struct ieee80211_meshpeer_ie)
1950	);
1951	if (m != NULL) {
1952		/*
1953		 * mesh peer open action frame format:
1954		 *   [1] category
1955		 *   [1] action
1956		 *   [2] capabilities
1957		 *   [tlv] rates
1958		 *   [tlv] xrates
1959		 *   [tlv] mesh id
1960		 *   [tlv] mesh conf
1961		 *   [tlv] mesh peer link mgmt
1962		 */
1963		*frm++ = category;
1964		*frm++ = action;
1965		ADDSHORT(frm, ieee80211_getcapinfo(vap, ni->ni_chan));
1966		rs = ieee80211_get_suprates(ic, ic->ic_curchan);
1967		frm = ieee80211_add_rates(frm, rs);
1968		frm = ieee80211_add_xrates(frm, rs);
1969		frm = ieee80211_add_meshid(frm, vap);
1970		frm = ieee80211_add_meshconf(frm, vap);
1971		frm = ieee80211_add_meshpeer(frm, IEEE80211_MESH_PEER_LINK_OPEN,
1972		    args[0], 0, 0);
1973		m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
1974		return mesh_send_action(ni, m);
1975	} else {
1976		vap->iv_stats.is_tx_nobuf++;
1977		ieee80211_free_node(ni);
1978		return ENOMEM;
1979	}
1980}
1981
1982static int
1983mesh_send_action_meshpeering_confirm(struct ieee80211_node *ni,
1984	int category, int action, void *args0)
1985{
1986	struct ieee80211vap *vap = ni->ni_vap;
1987	struct ieee80211com *ic = ni->ni_ic;
1988	uint16_t *args = args0;
1989	const struct ieee80211_rateset *rs;
1990	struct mbuf *m;
1991	uint8_t *frm;
1992
1993	IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH, ni,
1994	    "send PEER CONFIRM action: localid 0x%x, peerid 0x%x",
1995	    args[0], args[1]);
1996
1997	IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
1998	    "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", __func__, __LINE__,
1999	    ni, ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)+1);
2000	ieee80211_ref_node(ni);
2001
2002	m = ieee80211_getmgtframe(&frm,
2003	    ic->ic_headroom + sizeof(struct ieee80211_frame),
2004	    sizeof(uint16_t)	/* action+category */
2005	    + sizeof(uint16_t)	/* capabilites */
2006	    + sizeof(uint16_t)	/* status code */
2007	    + sizeof(uint16_t)	/* AID */
2008	    + 2 + IEEE80211_RATE_SIZE
2009	    + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
2010	    + 2 + IEEE80211_MESHID_LEN
2011	    + sizeof(struct ieee80211_meshconf_ie)
2012	    + sizeof(struct ieee80211_meshpeer_ie)
2013	);
2014	if (m != NULL) {
2015		/*
2016		 * mesh peer confirm action frame format:
2017		 *   [1] category
2018		 *   [1] action
2019		 *   [2] capabilities
2020		 *   [2] status code
2021		 *   [2] association id (peer ID)
2022		 *   [tlv] rates
2023		 *   [tlv] xrates
2024		 *   [tlv] mesh id
2025		 *   [tlv] mesh conf
2026		 *   [tlv] mesh peer link mgmt
2027		 */
2028		*frm++ = category;
2029		*frm++ = action;
2030		ADDSHORT(frm, ieee80211_getcapinfo(vap, ni->ni_chan));
2031		ADDSHORT(frm, 0);		/* status code */
2032		ADDSHORT(frm, args[1]);		/* AID */
2033		rs = ieee80211_get_suprates(ic, ic->ic_curchan);
2034		frm = ieee80211_add_rates(frm, rs);
2035		frm = ieee80211_add_xrates(frm, rs);
2036		frm = ieee80211_add_meshid(frm, vap);
2037		frm = ieee80211_add_meshconf(frm, vap);
2038		frm = ieee80211_add_meshpeer(frm,
2039		    IEEE80211_MESH_PEER_LINK_CONFIRM,
2040		    args[0], args[1], 0);
2041		m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2042		return mesh_send_action(ni, m);
2043	} else {
2044		vap->iv_stats.is_tx_nobuf++;
2045		ieee80211_free_node(ni);
2046		return ENOMEM;
2047	}
2048}
2049
2050static int
2051mesh_send_action_meshpeering_close(struct ieee80211_node *ni,
2052	int category, int action, void *args0)
2053{
2054	struct ieee80211vap *vap = ni->ni_vap;
2055	struct ieee80211com *ic = ni->ni_ic;
2056	uint16_t *args = args0;
2057	struct mbuf *m;
2058	uint8_t *frm;
2059
2060	IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH, ni,
2061	    "send PEER CLOSE action: localid 0x%x, peerid 0x%x reason %d",
2062	    args[0], args[1], args[2]);
2063
2064	IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
2065	    "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", __func__, __LINE__,
2066	    ni, ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)+1);
2067	ieee80211_ref_node(ni);
2068
2069	m = ieee80211_getmgtframe(&frm,
2070	    ic->ic_headroom + sizeof(struct ieee80211_frame),
2071	    sizeof(uint16_t)	/* action+category */
2072	    + sizeof(uint16_t)	/* reason code */
2073	    + 2 + IEEE80211_MESHID_LEN
2074	    + sizeof(struct ieee80211_meshpeer_ie)
2075	);
2076	if (m != NULL) {
2077		/*
2078		 * mesh peer close action frame format:
2079		 *   [1] category
2080		 *   [1] action
2081		 *   [2] reason code
2082		 *   [tlv] mesh id
2083		 *   [tlv] mesh peer link mgmt
2084		 */
2085		*frm++ = category;
2086		*frm++ = action;
2087		ADDSHORT(frm, args[2]);		/* reason code */
2088		frm = ieee80211_add_meshid(frm, vap);
2089		frm = ieee80211_add_meshpeer(frm,
2090		    IEEE80211_MESH_PEER_LINK_CLOSE,
2091		    args[0], args[1], args[2]);
2092		m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2093		return mesh_send_action(ni, m);
2094	} else {
2095		vap->iv_stats.is_tx_nobuf++;
2096		ieee80211_free_node(ni);
2097		return ENOMEM;
2098	}
2099}
2100
2101static int
2102mesh_send_action_meshlink_request(struct ieee80211_node *ni,
2103	int category, int action, void *arg0)
2104{
2105	struct ieee80211vap *vap = ni->ni_vap;
2106	struct ieee80211com *ic = ni->ni_ic;
2107	struct mbuf *m;
2108	uint8_t *frm;
2109
2110	IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH, ni,
2111	    "%s", "send LINK METRIC REQUEST action");
2112
2113	IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
2114	    "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", __func__, __LINE__,
2115	    ni, ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)+1);
2116	ieee80211_ref_node(ni);
2117
2118	m = ieee80211_getmgtframe(&frm,
2119	    ic->ic_headroom + sizeof(struct ieee80211_frame),
2120	    sizeof(uint16_t)	/* action+category */
2121	);
2122	if (m != NULL) {
2123		/*
2124		 * mesh link metric request
2125		 *   [1] category
2126		 *   [1] action
2127		 */
2128		*frm++ = category;
2129		*frm++ = action;
2130		m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2131		return mesh_send_action(ni, m);
2132	} else {
2133		vap->iv_stats.is_tx_nobuf++;
2134		ieee80211_free_node(ni);
2135		return ENOMEM;
2136	}
2137}
2138
2139static int
2140mesh_send_action_meshlink_reply(struct ieee80211_node *ni,
2141	int category, int action, void *args0)
2142{
2143	struct ieee80211vap *vap = ni->ni_vap;
2144	struct ieee80211com *ic = ni->ni_ic;
2145	uint32_t *metric = args0;
2146	struct mbuf *m;
2147	uint8_t *frm;
2148
2149	IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH, ni,
2150	    "send LINK METRIC REPLY action: metric 0x%x", *metric);
2151
2152	IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
2153	    "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", __func__, __LINE__,
2154	    ni, ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)+1);
2155	ieee80211_ref_node(ni);
2156
2157	m = ieee80211_getmgtframe(&frm,
2158	    ic->ic_headroom + sizeof(struct ieee80211_frame),
2159	    sizeof(uint16_t)	/* action+category */
2160	    + sizeof(struct ieee80211_meshlmetric_ie)
2161	);
2162	if (m != NULL) {
2163		/*
2164		 * mesh link metric reply
2165		 *   [1] category
2166		 *   [1] action
2167		 *   [tlv] mesh link metric
2168		 */
2169		*frm++ = category;
2170		*frm++ = action;
2171		frm = ieee80211_add_meshlmetric(frm, *metric);
2172		m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2173		return mesh_send_action(ni, m);
2174	} else {
2175		vap->iv_stats.is_tx_nobuf++;
2176		ieee80211_free_node(ni);
2177		return ENOMEM;
2178	}
2179}
2180
2181static void
2182mesh_peer_timeout_setup(struct ieee80211_node *ni)
2183{
2184	switch (ni->ni_mlstate) {
2185	case IEEE80211_NODE_MESH_HOLDING:
2186		ni->ni_mltval = ieee80211_mesh_holdingtimeout;
2187		break;
2188	case IEEE80211_NODE_MESH_CONFIRMRCV:
2189		ni->ni_mltval = ieee80211_mesh_confirmtimeout;
2190		break;
2191	case IEEE80211_NODE_MESH_IDLE:
2192		ni->ni_mltval = 0;
2193		break;
2194	default:
2195		ni->ni_mltval = ieee80211_mesh_retrytimeout;
2196		break;
2197	}
2198	if (ni->ni_mltval)
2199		callout_reset(&ni->ni_mltimer, ni->ni_mltval,
2200		    mesh_peer_timeout_cb, ni);
2201}
2202
2203/*
2204 * Same as above but backoffs timer statisically 50%.
2205 */
2206static void
2207mesh_peer_timeout_backoff(struct ieee80211_node *ni)
2208{
2209	uint32_t r;
2210
2211	r = arc4random();
2212	ni->ni_mltval += r % ni->ni_mltval;
2213	callout_reset(&ni->ni_mltimer, ni->ni_mltval, mesh_peer_timeout_cb,
2214	    ni);
2215}
2216
2217static __inline void
2218mesh_peer_timeout_stop(struct ieee80211_node *ni)
2219{
2220	callout_drain(&ni->ni_mltimer);
2221}
2222
2223/*
2224 * Mesh Peer Link Management FSM timeout handling.
2225 */
2226static void
2227mesh_peer_timeout_cb(void *arg)
2228{
2229	struct ieee80211_node *ni = (struct ieee80211_node *)arg;
2230	uint16_t args[3];
2231
2232	IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_MESH,
2233	    ni, "mesh link timeout, state %d, retry counter %d",
2234	    ni->ni_mlstate, ni->ni_mlrcnt);
2235
2236	switch (ni->ni_mlstate) {
2237	case IEEE80211_NODE_MESH_IDLE:
2238	case IEEE80211_NODE_MESH_ESTABLISHED:
2239		break;
2240	case IEEE80211_NODE_MESH_OPENSNT:
2241	case IEEE80211_NODE_MESH_OPENRCV:
2242		if (ni->ni_mlrcnt == ieee80211_mesh_maxretries) {
2243			args[0] = ni->ni_mlpid;
2244			args[2] = IEEE80211_REASON_MESH_MAX_RETRIES;
2245			ieee80211_send_action(ni,
2246			    IEEE80211_ACTION_CAT_MESHPEERING,
2247			    IEEE80211_ACTION_MESHPEERING_CLOSE, args);
2248			ni->ni_mlrcnt = 0;
2249			mesh_linkchange(ni, IEEE80211_NODE_MESH_HOLDING);
2250			mesh_peer_timeout_setup(ni);
2251		} else {
2252			args[0] = ni->ni_mlpid;
2253			ieee80211_send_action(ni,
2254			    IEEE80211_ACTION_CAT_MESHPEERING,
2255			    IEEE80211_ACTION_MESHPEERING_OPEN, args);
2256			ni->ni_mlrcnt++;
2257			mesh_peer_timeout_backoff(ni);
2258		}
2259		break;
2260	case IEEE80211_NODE_MESH_CONFIRMRCV:
2261		if (ni->ni_mlrcnt == ieee80211_mesh_maxretries) {
2262			args[0] = ni->ni_mlpid;
2263			args[2] = IEEE80211_REASON_MESH_CONFIRM_TIMEOUT;
2264			ieee80211_send_action(ni,
2265			    IEEE80211_ACTION_CAT_MESHPEERING,
2266			    IEEE80211_ACTION_MESHPEERING_CLOSE, args);
2267			ni->ni_mlrcnt = 0;
2268			mesh_linkchange(ni, IEEE80211_NODE_MESH_HOLDING);
2269			mesh_peer_timeout_setup(ni);
2270		} else {
2271			ni->ni_mlrcnt++;
2272			mesh_peer_timeout_setup(ni);
2273		}
2274		break;
2275	case IEEE80211_NODE_MESH_HOLDING:
2276		mesh_linkchange(ni, IEEE80211_NODE_MESH_IDLE);
2277		break;
2278	}
2279}
2280
2281static int
2282mesh_verify_meshid(struct ieee80211vap *vap, const uint8_t *ie)
2283{
2284	struct ieee80211_mesh_state *ms = vap->iv_mesh;
2285
2286	if (ie == NULL || ie[1] != ms->ms_idlen)
2287		return 1;
2288	return memcmp(ms->ms_id, ie + 2, ms->ms_idlen);
2289}
2290
2291/*
2292 * Check if we are using the same algorithms for this mesh.
2293 */
2294static int
2295mesh_verify_meshconf(struct ieee80211vap *vap, const uint8_t *ie)
2296{
2297	const struct ieee80211_meshconf_ie *meshconf =
2298	    (const struct ieee80211_meshconf_ie *) ie;
2299	const struct ieee80211_mesh_state *ms = vap->iv_mesh;
2300	uint16_t cap;
2301
2302	if (meshconf == NULL)
2303		return 1;
2304	if (meshconf->conf_pselid != ms->ms_ppath->mpp_ie) {
2305		IEEE80211_DPRINTF(vap, IEEE80211_MSG_MESH,
2306		    "unknown path selection algorithm: 0x%x\n",
2307		    meshconf->conf_pselid);
2308		return 1;
2309	}
2310	if (meshconf->conf_pmetid != ms->ms_pmetric->mpm_ie) {
2311		IEEE80211_DPRINTF(vap, IEEE80211_MSG_MESH,
2312		    "unknown path metric algorithm: 0x%x\n",
2313		    meshconf->conf_pmetid);
2314		return 1;
2315	}
2316	if (meshconf->conf_ccid != 0) {
2317		IEEE80211_DPRINTF(vap, IEEE80211_MSG_MESH,
2318		    "unknown congestion control algorithm: 0x%x\n",
2319		    meshconf->conf_ccid);
2320		return 1;
2321	}
2322	if (meshconf->conf_syncid != IEEE80211_MESHCONF_SYNC_NEIGHOFF) {
2323		IEEE80211_DPRINTF(vap, IEEE80211_MSG_MESH,
2324		    "unknown sync algorithm: 0x%x\n",
2325		    meshconf->conf_syncid);
2326		return 1;
2327	}
2328	if (meshconf->conf_authid != 0) {
2329		IEEE80211_DPRINTF(vap, IEEE80211_MSG_MESH,
2330		    "unknown auth auth algorithm: 0x%x\n",
2331		    meshconf->conf_pselid);
2332		return 1;
2333	}
2334	/* NB: conf_cap is only read correctly here */
2335	cap = LE_READ_2(&meshconf->conf_cap);
2336	/* Not accepting peers */
2337	if (!(cap & IEEE80211_MESHCONF_CAP_AP)) {
2338		IEEE80211_DPRINTF(vap, IEEE80211_MSG_MESH,
2339		    "not accepting peers: 0x%x\n", meshconf->conf_cap);
2340		return 1;
2341	}
2342	return 0;
2343}
2344
2345static int
2346mesh_verify_meshpeer(struct ieee80211vap *vap, uint8_t subtype,
2347    const uint8_t *ie)
2348{
2349	const struct ieee80211_meshpeer_ie *meshpeer =
2350	    (const struct ieee80211_meshpeer_ie *) ie;
2351
2352	if (meshpeer == NULL || meshpeer->peer_len < 6 ||
2353	    meshpeer->peer_len > 10)
2354		return 1;
2355	switch (subtype) {
2356	case IEEE80211_MESH_PEER_LINK_OPEN:
2357		if (meshpeer->peer_len != 6)
2358			return 1;
2359		break;
2360	case IEEE80211_MESH_PEER_LINK_CONFIRM:
2361		if (meshpeer->peer_len != 8)
2362			return 1;
2363		break;
2364	case IEEE80211_MESH_PEER_LINK_CLOSE:
2365		if (meshpeer->peer_len < 8)
2366			return 1;
2367		if (meshpeer->peer_len == 8 && meshpeer->peer_linkid != 0)
2368			return 1;
2369		if (meshpeer->peer_rcode == 0)
2370			return 1;
2371		break;
2372	}
2373	return 0;
2374}
2375
2376/*
2377 * Add a Mesh ID IE to a frame.
2378 */
2379uint8_t *
2380ieee80211_add_meshid(uint8_t *frm, struct ieee80211vap *vap)
2381{
2382	struct ieee80211_mesh_state *ms = vap->iv_mesh;
2383
2384	KASSERT(vap->iv_opmode == IEEE80211_M_MBSS, ("not a mbss vap"));
2385
2386	*frm++ = IEEE80211_ELEMID_MESHID;
2387	*frm++ = ms->ms_idlen;
2388	memcpy(frm, ms->ms_id, ms->ms_idlen);
2389	return frm + ms->ms_idlen;
2390}
2391
2392/*
2393 * Add a Mesh Configuration IE to a frame.
2394 * For now just use HWMP routing, Airtime link metric, Null Congestion
2395 * Signaling, Null Sync Protocol and Null Authentication.
2396 */
2397uint8_t *
2398ieee80211_add_meshconf(uint8_t *frm, struct ieee80211vap *vap)
2399{
2400	const struct ieee80211_mesh_state *ms = vap->iv_mesh;
2401	uint16_t caps;
2402
2403	KASSERT(vap->iv_opmode == IEEE80211_M_MBSS, ("not a MBSS vap"));
2404
2405	*frm++ = IEEE80211_ELEMID_MESHCONF;
2406	*frm++ = sizeof(struct ieee80211_meshconf_ie) - 2;
2407	*frm++ = ms->ms_ppath->mpp_ie;		/* path selection */
2408	*frm++ = ms->ms_pmetric->mpm_ie;	/* link metric */
2409	*frm++ = IEEE80211_MESHCONF_CC_DISABLED;
2410	*frm++ = IEEE80211_MESHCONF_SYNC_NEIGHOFF;
2411	*frm++ = IEEE80211_MESHCONF_AUTH_DISABLED;
2412	/* NB: set the number of neighbors before the rest */
2413	*frm = (ms->ms_neighbors > 15 ? 15 : ms->ms_neighbors) << 1;
2414	if (ms->ms_flags & IEEE80211_MESHFLAGS_PORTAL)
2415		*frm |= IEEE80211_MESHCONF_FORM_MP;
2416	frm += 1;
2417	caps = 0;
2418	if (ms->ms_flags & IEEE80211_MESHFLAGS_AP)
2419		caps |= IEEE80211_MESHCONF_CAP_AP;
2420	if (ms->ms_flags & IEEE80211_MESHFLAGS_FWD)
2421		caps |= IEEE80211_MESHCONF_CAP_FWRD;
2422	ADDSHORT(frm, caps);
2423	return frm;
2424}
2425
2426/*
2427 * Add a Mesh Peer Management IE to a frame.
2428 */
2429uint8_t *
2430ieee80211_add_meshpeer(uint8_t *frm, uint8_t subtype, uint16_t localid,
2431    uint16_t peerid, uint16_t reason)
2432{
2433	/* XXX change for AH */
2434	static const uint8_t meshpeerproto[4] = IEEE80211_MESH_PEER_PROTO;
2435
2436	KASSERT(localid != 0, ("localid == 0"));
2437
2438	*frm++ = IEEE80211_ELEMID_MESHPEER;
2439	switch (subtype) {
2440	case IEEE80211_MESH_PEER_LINK_OPEN:
2441		*frm++ = 6;		/* length */
2442		memcpy(frm, meshpeerproto, 4);
2443		frm += 4;
2444		ADDSHORT(frm, localid);	/* local ID */
2445		break;
2446	case IEEE80211_MESH_PEER_LINK_CONFIRM:
2447		KASSERT(peerid != 0, ("sending peer confirm without peer id"));
2448		*frm++ = 8;		/* length */
2449		memcpy(frm, meshpeerproto, 4);
2450		frm += 4;
2451		ADDSHORT(frm, localid);	/* local ID */
2452		ADDSHORT(frm, peerid);	/* peer ID */
2453		break;
2454	case IEEE80211_MESH_PEER_LINK_CLOSE:
2455		if (peerid)
2456			*frm++ = 10;	/* length */
2457		else
2458			*frm++ = 8;	/* length */
2459		memcpy(frm, meshpeerproto, 4);
2460		frm += 4;
2461		ADDSHORT(frm, localid);	/* local ID */
2462		if (peerid)
2463			ADDSHORT(frm, peerid);	/* peer ID */
2464		ADDSHORT(frm, reason);
2465		break;
2466	}
2467	return frm;
2468}
2469
2470/*
2471 * Compute an Airtime Link Metric for the link with this node.
2472 *
2473 * Based on Draft 3.0 spec (11B.10, p.149).
2474 */
2475/*
2476 * Max 802.11s overhead.
2477 */
2478#define IEEE80211_MESH_MAXOVERHEAD \
2479	(sizeof(struct ieee80211_qosframe_addr4) \
2480	 + sizeof(struct ieee80211_meshcntl_ae11) \
2481	+ sizeof(struct llc) \
2482	+ IEEE80211_ADDR_LEN \
2483	+ IEEE80211_WEP_IVLEN \
2484	+ IEEE80211_WEP_KIDLEN \
2485	+ IEEE80211_WEP_CRCLEN \
2486	+ IEEE80211_WEP_MICLEN \
2487	+ IEEE80211_CRC_LEN)
2488uint32_t
2489mesh_airtime_calc(struct ieee80211_node *ni)
2490{
2491#define M_BITS 8
2492#define S_FACTOR (2 * M_BITS)
2493	struct ieee80211com *ic = ni->ni_ic;
2494	struct ifnet *ifp = ni->ni_vap->iv_ifp;
2495	const static int nbits = 8192 << M_BITS;
2496	uint32_t overhead, rate, errrate;
2497	uint64_t res;
2498
2499	/* Time to transmit a frame */
2500	rate = ni->ni_txrate;
2501	overhead = ieee80211_compute_duration(ic->ic_rt,
2502	    ifp->if_mtu + IEEE80211_MESH_MAXOVERHEAD, rate, 0) << M_BITS;
2503	/* Error rate in percentage */
2504	/* XXX assuming small failures are ok */
2505	errrate = (((ifp->if_oerrors +
2506	    ifp->if_ierrors) / 100) << M_BITS) / 100;
2507	res = (overhead + (nbits / rate)) *
2508	    ((1 << S_FACTOR) / ((1 << M_BITS) - errrate));
2509
2510	return (uint32_t)(res >> S_FACTOR);
2511#undef M_BITS
2512#undef S_FACTOR
2513}
2514
2515/*
2516 * Add a Mesh Link Metric report IE to a frame.
2517 */
2518uint8_t *
2519ieee80211_add_meshlmetric(uint8_t *frm, uint32_t metric)
2520{
2521	*frm++ = IEEE80211_ELEMID_MESHLINK;
2522	*frm++ = 4;
2523	ADDWORD(frm, metric);
2524	return frm;
2525}
2526#undef ADDSHORT
2527#undef ADDWORD
2528
2529/*
2530 * Initialize any mesh-specific node state.
2531 */
2532void
2533ieee80211_mesh_node_init(struct ieee80211vap *vap, struct ieee80211_node *ni)
2534{
2535	ni->ni_flags |= IEEE80211_NODE_QOS;
2536	callout_init(&ni->ni_mltimer, CALLOUT_MPSAFE);
2537}
2538
2539/*
2540 * Cleanup any mesh-specific node state.
2541 */
2542void
2543ieee80211_mesh_node_cleanup(struct ieee80211_node *ni)
2544{
2545	struct ieee80211vap *vap = ni->ni_vap;
2546	struct ieee80211_mesh_state *ms = vap->iv_mesh;
2547
2548	callout_drain(&ni->ni_mltimer);
2549	/* NB: short-circuit callbacks after mesh_vdetach */
2550	if (vap->iv_mesh != NULL)
2551		ms->ms_ppath->mpp_peerdown(ni);
2552}
2553
2554void
2555ieee80211_parse_meshid(struct ieee80211_node *ni, const uint8_t *ie)
2556{
2557	ni->ni_meshidlen = ie[1];
2558	memcpy(ni->ni_meshid, ie + 2, ie[1]);
2559}
2560
2561/*
2562 * Setup mesh-specific node state on neighbor discovery.
2563 */
2564void
2565ieee80211_mesh_init_neighbor(struct ieee80211_node *ni,
2566	const struct ieee80211_frame *wh,
2567	const struct ieee80211_scanparams *sp)
2568{
2569	ieee80211_parse_meshid(ni, sp->meshid);
2570}
2571
2572void
2573ieee80211_mesh_update_beacon(struct ieee80211vap *vap,
2574	struct ieee80211_beacon_offsets *bo)
2575{
2576	KASSERT(vap->iv_opmode == IEEE80211_M_MBSS, ("not a MBSS vap"));
2577
2578	if (isset(bo->bo_flags, IEEE80211_BEACON_MESHCONF)) {
2579		(void)ieee80211_add_meshconf(bo->bo_meshconf, vap);
2580		clrbit(bo->bo_flags, IEEE80211_BEACON_MESHCONF);
2581	}
2582}
2583
2584static int
2585mesh_ioctl_get80211(struct ieee80211vap *vap, struct ieee80211req *ireq)
2586{
2587	struct ieee80211_mesh_state *ms = vap->iv_mesh;
2588	uint8_t tmpmeshid[IEEE80211_NWID_LEN];
2589	struct ieee80211_mesh_route *rt;
2590	struct ieee80211req_mesh_route *imr;
2591	size_t len, off;
2592	uint8_t *p;
2593	int error;
2594
2595	if (vap->iv_opmode != IEEE80211_M_MBSS)
2596		return ENOSYS;
2597
2598	error = 0;
2599	switch (ireq->i_type) {
2600	case IEEE80211_IOC_MESH_ID:
2601		ireq->i_len = ms->ms_idlen;
2602		memcpy(tmpmeshid, ms->ms_id, ireq->i_len);
2603		error = copyout(tmpmeshid, ireq->i_data, ireq->i_len);
2604		break;
2605	case IEEE80211_IOC_MESH_AP:
2606		ireq->i_val = (ms->ms_flags & IEEE80211_MESHFLAGS_AP) != 0;
2607		break;
2608	case IEEE80211_IOC_MESH_FWRD:
2609		ireq->i_val = (ms->ms_flags & IEEE80211_MESHFLAGS_FWD) != 0;
2610		break;
2611	case IEEE80211_IOC_MESH_TTL:
2612		ireq->i_val = ms->ms_ttl;
2613		break;
2614	case IEEE80211_IOC_MESH_RTCMD:
2615		switch (ireq->i_val) {
2616		case IEEE80211_MESH_RTCMD_LIST:
2617			len = 0;
2618			MESH_RT_LOCK(ms);
2619			TAILQ_FOREACH(rt, &ms->ms_routes, rt_next) {
2620				len += sizeof(*imr);
2621			}
2622			MESH_RT_UNLOCK(ms);
2623			if (len > ireq->i_len || ireq->i_len < sizeof(*imr)) {
2624				ireq->i_len = len;
2625				return ENOMEM;
2626			}
2627			ireq->i_len = len;
2628			/* XXX M_WAIT? */
2629			p = malloc(len, M_TEMP, M_NOWAIT | M_ZERO);
2630			if (p == NULL)
2631				return ENOMEM;
2632			off = 0;
2633			MESH_RT_LOCK(ms);
2634			TAILQ_FOREACH(rt, &ms->ms_routes, rt_next) {
2635				if (off >= len)
2636					break;
2637				imr = (struct ieee80211req_mesh_route *)
2638				    (p + off);
2639				imr->imr_flags = rt->rt_flags;
2640				IEEE80211_ADDR_COPY(imr->imr_dest,
2641				    rt->rt_dest);
2642				IEEE80211_ADDR_COPY(imr->imr_nexthop,
2643				    rt->rt_nexthop);
2644				imr->imr_metric = rt->rt_metric;
2645				imr->imr_nhops = rt->rt_nhops;
2646				imr->imr_lifetime = rt->rt_lifetime;
2647				imr->imr_lastmseq = rt->rt_lastmseq;
2648				off += sizeof(*imr);
2649			}
2650			MESH_RT_UNLOCK(ms);
2651			error = copyout(p, (uint8_t *)ireq->i_data,
2652			    ireq->i_len);
2653			free(p, M_TEMP);
2654			break;
2655		case IEEE80211_MESH_RTCMD_FLUSH:
2656		case IEEE80211_MESH_RTCMD_ADD:
2657		case IEEE80211_MESH_RTCMD_DELETE:
2658			return EINVAL;
2659		default:
2660			return ENOSYS;
2661		}
2662		break;
2663	case IEEE80211_IOC_MESH_PR_METRIC:
2664		len = strlen(ms->ms_pmetric->mpm_descr);
2665		if (ireq->i_len < len)
2666			return EINVAL;
2667		ireq->i_len = len;
2668		error = copyout(ms->ms_pmetric->mpm_descr,
2669		    (uint8_t *)ireq->i_data, len);
2670		break;
2671	case IEEE80211_IOC_MESH_PR_PATH:
2672		len = strlen(ms->ms_ppath->mpp_descr);
2673		if (ireq->i_len < len)
2674			return EINVAL;
2675		ireq->i_len = len;
2676		error = copyout(ms->ms_ppath->mpp_descr,
2677		    (uint8_t *)ireq->i_data, len);
2678		break;
2679	default:
2680		return ENOSYS;
2681	}
2682
2683	return error;
2684}
2685IEEE80211_IOCTL_GET(mesh, mesh_ioctl_get80211);
2686
2687static int
2688mesh_ioctl_set80211(struct ieee80211vap *vap, struct ieee80211req *ireq)
2689{
2690	struct ieee80211_mesh_state *ms = vap->iv_mesh;
2691	uint8_t tmpmeshid[IEEE80211_NWID_LEN];
2692	uint8_t tmpaddr[IEEE80211_ADDR_LEN];
2693	char tmpproto[IEEE80211_MESH_PROTO_DSZ];
2694	int error;
2695
2696	if (vap->iv_opmode != IEEE80211_M_MBSS)
2697		return ENOSYS;
2698
2699	error = 0;
2700	switch (ireq->i_type) {
2701	case IEEE80211_IOC_MESH_ID:
2702		if (ireq->i_val != 0 || ireq->i_len > IEEE80211_MESHID_LEN)
2703			return EINVAL;
2704		error = copyin(ireq->i_data, tmpmeshid, ireq->i_len);
2705		if (error != 0)
2706			break;
2707		memset(ms->ms_id, 0, IEEE80211_NWID_LEN);
2708		ms->ms_idlen = ireq->i_len;
2709		memcpy(ms->ms_id, tmpmeshid, ireq->i_len);
2710		error = ENETRESET;
2711		break;
2712	case IEEE80211_IOC_MESH_AP:
2713		if (ireq->i_val)
2714			ms->ms_flags |= IEEE80211_MESHFLAGS_AP;
2715		else
2716			ms->ms_flags &= ~IEEE80211_MESHFLAGS_AP;
2717		error = ENETRESET;
2718		break;
2719	case IEEE80211_IOC_MESH_FWRD:
2720		if (ireq->i_val)
2721			ms->ms_flags |= IEEE80211_MESHFLAGS_FWD;
2722		else
2723			ms->ms_flags &= ~IEEE80211_MESHFLAGS_FWD;
2724		break;
2725	case IEEE80211_IOC_MESH_TTL:
2726		ms->ms_ttl = (uint8_t) ireq->i_val;
2727		break;
2728	case IEEE80211_IOC_MESH_RTCMD:
2729		switch (ireq->i_val) {
2730		case IEEE80211_MESH_RTCMD_LIST:
2731			return EINVAL;
2732		case IEEE80211_MESH_RTCMD_FLUSH:
2733			ieee80211_mesh_rt_flush(vap);
2734			break;
2735		case IEEE80211_MESH_RTCMD_ADD:
2736			if (IEEE80211_ADDR_EQ(vap->iv_myaddr, ireq->i_data) ||
2737			    IEEE80211_ADDR_EQ(broadcastaddr, ireq->i_data))
2738				return EINVAL;
2739			error = copyin(ireq->i_data, &tmpaddr,
2740			    IEEE80211_ADDR_LEN);
2741			if (error == 0)
2742				ieee80211_mesh_discover(vap, tmpaddr, NULL);
2743			break;
2744		case IEEE80211_MESH_RTCMD_DELETE:
2745			ieee80211_mesh_rt_del(vap, ireq->i_data);
2746			break;
2747		default:
2748			return ENOSYS;
2749		}
2750		break;
2751	case IEEE80211_IOC_MESH_PR_METRIC:
2752		error = copyin(ireq->i_data, tmpproto, sizeof(tmpproto));
2753		if (error == 0) {
2754			error = mesh_select_proto_metric(vap, tmpproto);
2755			if (error == 0)
2756				error = ENETRESET;
2757		}
2758		break;
2759	case IEEE80211_IOC_MESH_PR_PATH:
2760		error = copyin(ireq->i_data, tmpproto, sizeof(tmpproto));
2761		if (error == 0) {
2762			error = mesh_select_proto_path(vap, tmpproto);
2763			if (error == 0)
2764				error = ENETRESET;
2765		}
2766		break;
2767	default:
2768		return ENOSYS;
2769	}
2770	return error;
2771}
2772IEEE80211_IOCTL_SET(mesh, mesh_ioctl_set80211);
2773