ieee80211_mesh.c revision 230926
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 230926 2012-02-03 02:06: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_recv_ctl(struct ieee80211_node *, struct mbuf *, int);
86static void	mesh_peer_timeout_setup(struct ieee80211_node *);
87static void	mesh_peer_timeout_backoff(struct ieee80211_node *);
88static void	mesh_peer_timeout_cb(void *);
89static __inline void
90		mesh_peer_timeout_stop(struct ieee80211_node *);
91static int	mesh_verify_meshid(struct ieee80211vap *, const uint8_t *);
92static int	mesh_verify_meshconf(struct ieee80211vap *, const uint8_t *);
93static int	mesh_verify_meshpeer(struct ieee80211vap *, uint8_t,
94    		    const uint8_t *);
95uint32_t	mesh_airtime_calc(struct ieee80211_node *);
96
97/*
98 * Timeout values come from the specification and are in milliseconds.
99 */
100static SYSCTL_NODE(_net_wlan, OID_AUTO, mesh, CTLFLAG_RD, 0,
101    "IEEE 802.11s parameters");
102static int ieee80211_mesh_retrytimeout = -1;
103SYSCTL_PROC(_net_wlan_mesh, OID_AUTO, retrytimeout, CTLTYPE_INT | CTLFLAG_RW,
104    &ieee80211_mesh_retrytimeout, 0, ieee80211_sysctl_msecs_ticks, "I",
105    "Retry timeout (msec)");
106static int ieee80211_mesh_holdingtimeout = -1;
107SYSCTL_PROC(_net_wlan_mesh, OID_AUTO, holdingtimeout, CTLTYPE_INT | CTLFLAG_RW,
108    &ieee80211_mesh_holdingtimeout, 0, ieee80211_sysctl_msecs_ticks, "I",
109    "Holding state timeout (msec)");
110static int ieee80211_mesh_confirmtimeout = -1;
111SYSCTL_PROC(_net_wlan_mesh, OID_AUTO, confirmtimeout, CTLTYPE_INT | CTLFLAG_RW,
112    &ieee80211_mesh_confirmtimeout, 0, ieee80211_sysctl_msecs_ticks, "I",
113    "Confirm state timeout (msec)");
114static int ieee80211_mesh_maxretries = 2;
115SYSCTL_INT(_net_wlan_mesh, OID_AUTO, maxretries, CTLTYPE_INT | CTLFLAG_RW,
116    &ieee80211_mesh_maxretries, 0,
117    "Maximum retries during peer link establishment");
118
119static const uint8_t broadcastaddr[IEEE80211_ADDR_LEN] =
120	{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
121
122static	ieee80211_recv_action_func mesh_recv_action_meshpeering_open;
123static	ieee80211_recv_action_func mesh_recv_action_meshpeering_confirm;
124static	ieee80211_recv_action_func mesh_recv_action_meshpeering_close;
125static	ieee80211_recv_action_func mesh_recv_action_meshlmetric_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	HAS_SEQ(type)	((type & 0x4) == 0)
1044	struct ieee80211vap *vap = ni->ni_vap;
1045	struct ieee80211com *ic = ni->ni_ic;
1046	struct ifnet *ifp = vap->iv_ifp;
1047	struct ieee80211_frame *wh;
1048	const struct ieee80211_meshcntl *mc;
1049	int hdrspace, meshdrlen, need_tap;
1050	uint8_t dir, type, subtype, qos;
1051	uint32_t seq;
1052	uint8_t *addr;
1053	ieee80211_seq rxseq;
1054
1055	KASSERT(ni != NULL, ("null node"));
1056	ni->ni_inact = ni->ni_inact_reload;
1057
1058	need_tap = 1;			/* mbuf need to be tapped. */
1059	type = -1;			/* undefined */
1060
1061	if (m->m_pkthdr.len < sizeof(struct ieee80211_frame_min)) {
1062		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
1063		    ni->ni_macaddr, NULL,
1064		    "too short (1): len %u", m->m_pkthdr.len);
1065		vap->iv_stats.is_rx_tooshort++;
1066		goto out;
1067	}
1068	/*
1069	 * Bit of a cheat here, we use a pointer for a 3-address
1070	 * frame format but don't reference fields past outside
1071	 * ieee80211_frame_min w/o first validating the data is
1072	 * present.
1073	*/
1074	wh = mtod(m, struct ieee80211_frame *);
1075
1076	if ((wh->i_fc[0] & IEEE80211_FC0_VERSION_MASK) !=
1077	    IEEE80211_FC0_VERSION_0) {
1078		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
1079		    ni->ni_macaddr, NULL, "wrong version %x", wh->i_fc[0]);
1080		vap->iv_stats.is_rx_badversion++;
1081		goto err;
1082	}
1083	dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK;
1084	type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
1085	subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
1086	if ((ic->ic_flags & IEEE80211_F_SCAN) == 0) {
1087		IEEE80211_RSSI_LPF(ni->ni_avgrssi, rssi);
1088		ni->ni_noise = nf;
1089		if (HAS_SEQ(type)) {
1090			uint8_t tid = ieee80211_gettid(wh);
1091
1092			if (IEEE80211_QOS_HAS_SEQ(wh) &&
1093			    TID_TO_WME_AC(tid) >= WME_AC_VI)
1094				ic->ic_wme.wme_hipri_traffic++;
1095			rxseq = le16toh(*(uint16_t *)wh->i_seq);
1096			if (! ieee80211_check_rxseq(ni, wh)) {
1097				/* duplicate, discard */
1098				IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
1099				    wh->i_addr1, "duplicate",
1100				    "seqno <%u,%u> fragno <%u,%u> tid %u",
1101				    rxseq >> IEEE80211_SEQ_SEQ_SHIFT,
1102				    ni->ni_rxseqs[tid] >>
1103				    IEEE80211_SEQ_SEQ_SHIFT,
1104				    rxseq & IEEE80211_SEQ_FRAG_MASK,
1105				    ni->ni_rxseqs[tid] &
1106				    IEEE80211_SEQ_FRAG_MASK,
1107				    tid);
1108				vap->iv_stats.is_rx_dup++;
1109				IEEE80211_NODE_STAT(ni, rx_dup);
1110				goto out;
1111			}
1112			ni->ni_rxseqs[tid] = rxseq;
1113		}
1114	}
1115#ifdef IEEE80211_DEBUG
1116	/*
1117	 * It's easier, but too expensive, to simulate different mesh
1118	 * topologies by consulting the ACL policy very early, so do this
1119	 * only under DEBUG.
1120	 *
1121	 * NB: this check is also done upon peering link initiation.
1122	 */
1123	if (vap->iv_acl != NULL && !vap->iv_acl->iac_check(vap, wh)) {
1124		IEEE80211_DISCARD(vap, IEEE80211_MSG_ACL,
1125		    wh, NULL, "%s", "disallowed by ACL");
1126		vap->iv_stats.is_rx_acl++;
1127		goto out;
1128	}
1129#endif
1130	switch (type) {
1131	case IEEE80211_FC0_TYPE_DATA:
1132		if (ni == vap->iv_bss)
1133			goto out;
1134		if (ni->ni_mlstate != IEEE80211_NODE_MESH_ESTABLISHED) {
1135			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_MESH,
1136			    ni->ni_macaddr, NULL,
1137			    "peer link not yet established (%d)",
1138			    ni->ni_mlstate);
1139			vap->iv_stats.is_mesh_nolink++;
1140			goto out;
1141		}
1142		if (dir != IEEE80211_FC1_DIR_FROMDS &&
1143		    dir != IEEE80211_FC1_DIR_DSTODS) {
1144			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
1145			    wh, "data", "incorrect dir 0x%x", dir);
1146			vap->iv_stats.is_rx_wrongdir++;
1147			goto err;
1148		}
1149		/* pull up enough to get to the mesh control */
1150		hdrspace = ieee80211_hdrspace(ic, wh);
1151		if (m->m_len < hdrspace + sizeof(struct ieee80211_meshcntl) &&
1152		    (m = m_pullup(m, hdrspace +
1153		        sizeof(struct ieee80211_meshcntl))) == NULL) {
1154			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
1155			    ni->ni_macaddr, NULL,
1156			    "data too short: expecting %u", hdrspace);
1157			vap->iv_stats.is_rx_tooshort++;
1158			goto out;		/* XXX */
1159		}
1160		/*
1161		 * Now calculate the full extent of the headers. Note
1162		 * mesh_decap will pull up anything we didn't get
1163		 * above when it strips the 802.11 headers.
1164		 */
1165		mc = (const struct ieee80211_meshcntl *)
1166		    (mtod(m, const uint8_t *) + hdrspace);
1167		meshdrlen = sizeof(struct ieee80211_meshcntl) +
1168		    (mc->mc_flags & 3) * IEEE80211_ADDR_LEN;
1169		hdrspace += meshdrlen;
1170		seq = LE_READ_4(mc->mc_seq);
1171		if (IEEE80211_IS_MULTICAST(wh->i_addr1))
1172			addr = wh->i_addr3;
1173		else
1174			addr = ((struct ieee80211_qosframe_addr4 *)wh)->i_addr4;
1175		if (IEEE80211_ADDR_EQ(vap->iv_myaddr, addr)) {
1176			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
1177			    addr, "data", "%s", "not to me");
1178			vap->iv_stats.is_rx_wrongbss++;	/* XXX kinda */
1179			goto out;
1180		}
1181		if (mesh_checkpseq(vap, addr, seq) != 0) {
1182			vap->iv_stats.is_rx_dup++;
1183			goto out;
1184		}
1185
1186		/*
1187		 * Potentially forward packet.  See table s36 (p140)
1188		 * for the rules.  XXX tap fwd'd packets not for us?
1189		 */
1190		if (dir == IEEE80211_FC1_DIR_FROMDS ||
1191		    !mesh_isucastforme(vap, wh, mc)) {
1192			mesh_forward(vap, m, mc);
1193			if (dir == IEEE80211_FC1_DIR_DSTODS)
1194				goto out;
1195			/* NB: fall thru to deliver mcast frames locally */
1196		}
1197
1198		/*
1199		 * Save QoS bits for use below--before we strip the header.
1200		 */
1201		if (subtype == IEEE80211_FC0_SUBTYPE_QOS) {
1202			qos = (dir == IEEE80211_FC1_DIR_DSTODS) ?
1203			    ((struct ieee80211_qosframe_addr4 *)wh)->i_qos[0] :
1204			    ((struct ieee80211_qosframe *)wh)->i_qos[0];
1205		} else
1206			qos = 0;
1207		/*
1208		 * Next up, any fragmentation.
1209		 */
1210		if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1211			m = ieee80211_defrag(ni, m, hdrspace);
1212			if (m == NULL) {
1213				/* Fragment dropped or frame not complete yet */
1214				goto out;
1215			}
1216		}
1217		wh = NULL;		/* no longer valid, catch any uses */
1218
1219		if (ieee80211_radiotap_active_vap(vap))
1220			ieee80211_radiotap_rx(vap, m);
1221		need_tap = 0;
1222
1223		/*
1224		 * Finally, strip the 802.11 header.
1225		 */
1226		m = mesh_decap(vap, m, hdrspace, meshdrlen);
1227		if (m == NULL) {
1228			/* XXX mask bit to check for both */
1229			/* don't count Null data frames as errors */
1230			if (subtype == IEEE80211_FC0_SUBTYPE_NODATA ||
1231			    subtype == IEEE80211_FC0_SUBTYPE_QOS_NULL)
1232				goto out;
1233			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
1234			    ni->ni_macaddr, "data", "%s", "decap error");
1235			vap->iv_stats.is_rx_decap++;
1236			IEEE80211_NODE_STAT(ni, rx_decap);
1237			goto err;
1238		}
1239		if (qos & IEEE80211_QOS_AMSDU) {
1240			m = ieee80211_decap_amsdu(ni, m);
1241			if (m == NULL)
1242				return IEEE80211_FC0_TYPE_DATA;
1243		}
1244		ieee80211_deliver_data(vap, ni, m);
1245		return type;
1246	case IEEE80211_FC0_TYPE_MGT:
1247		vap->iv_stats.is_rx_mgmt++;
1248		IEEE80211_NODE_STAT(ni, rx_mgmt);
1249		if (dir != IEEE80211_FC1_DIR_NODS) {
1250			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
1251			    wh, "mgt", "incorrect dir 0x%x", dir);
1252			vap->iv_stats.is_rx_wrongdir++;
1253			goto err;
1254		}
1255		if (m->m_pkthdr.len < sizeof(struct ieee80211_frame)) {
1256			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
1257			    ni->ni_macaddr, "mgt", "too short: len %u",
1258			    m->m_pkthdr.len);
1259			vap->iv_stats.is_rx_tooshort++;
1260			goto out;
1261		}
1262#ifdef IEEE80211_DEBUG
1263		if ((ieee80211_msg_debug(vap) &&
1264		    (vap->iv_ic->ic_flags & IEEE80211_F_SCAN)) ||
1265		    ieee80211_msg_dumppkts(vap)) {
1266			if_printf(ifp, "received %s from %s rssi %d\n",
1267			    ieee80211_mgt_subtype_name[subtype >>
1268			    IEEE80211_FC0_SUBTYPE_SHIFT],
1269			    ether_sprintf(wh->i_addr2), rssi);
1270		}
1271#endif
1272		if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
1273			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
1274			    wh, NULL, "%s", "WEP set but not permitted");
1275			vap->iv_stats.is_rx_mgtdiscard++; /* XXX */
1276			goto out;
1277		}
1278		vap->iv_recv_mgmt(ni, m, subtype, rssi, nf);
1279		goto out;
1280	case IEEE80211_FC0_TYPE_CTL:
1281		vap->iv_stats.is_rx_ctl++;
1282		IEEE80211_NODE_STAT(ni, rx_ctrl);
1283		goto out;
1284	default:
1285		IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
1286		    wh, "bad", "frame type 0x%x", type);
1287		/* should not come here */
1288		break;
1289	}
1290err:
1291	ifp->if_ierrors++;
1292out:
1293	if (m != NULL) {
1294		if (need_tap && ieee80211_radiotap_active_vap(vap))
1295			ieee80211_radiotap_rx(vap, m);
1296		m_freem(m);
1297	}
1298	return type;
1299}
1300
1301static void
1302mesh_recv_mgmt(struct ieee80211_node *ni, struct mbuf *m0, int subtype,
1303    int rssi, int nf)
1304{
1305	struct ieee80211vap *vap = ni->ni_vap;
1306	struct ieee80211_mesh_state *ms = vap->iv_mesh;
1307	struct ieee80211com *ic = ni->ni_ic;
1308	struct ieee80211_frame *wh;
1309	uint8_t *frm, *efrm;
1310
1311	wh = mtod(m0, struct ieee80211_frame *);
1312	frm = (uint8_t *)&wh[1];
1313	efrm = mtod(m0, uint8_t *) + m0->m_len;
1314	switch (subtype) {
1315	case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
1316	case IEEE80211_FC0_SUBTYPE_BEACON:
1317	{
1318		struct ieee80211_scanparams scan;
1319		/*
1320		 * We process beacon/probe response
1321		 * frames to discover neighbors.
1322		 */
1323		if (ieee80211_parse_beacon(ni, m0, &scan) != 0)
1324			return;
1325		/*
1326		 * Count frame now that we know it's to be processed.
1327		 */
1328		if (subtype == IEEE80211_FC0_SUBTYPE_BEACON) {
1329			vap->iv_stats.is_rx_beacon++;	/* XXX remove */
1330			IEEE80211_NODE_STAT(ni, rx_beacons);
1331		} else
1332			IEEE80211_NODE_STAT(ni, rx_proberesp);
1333		/*
1334		 * If scanning, just pass information to the scan module.
1335		 */
1336		if (ic->ic_flags & IEEE80211_F_SCAN) {
1337			if (ic->ic_flags_ext & IEEE80211_FEXT_PROBECHAN) {
1338				/*
1339				 * Actively scanning a channel marked passive;
1340				 * send a probe request now that we know there
1341				 * is 802.11 traffic present.
1342				 *
1343				 * XXX check if the beacon we recv'd gives
1344				 * us what we need and suppress the probe req
1345				 */
1346				ieee80211_probe_curchan(vap, 1);
1347				ic->ic_flags_ext &= ~IEEE80211_FEXT_PROBECHAN;
1348			}
1349			ieee80211_add_scan(vap, &scan, wh,
1350			    subtype, rssi, nf);
1351			return;
1352		}
1353
1354		/* The rest of this code assumes we are running */
1355		if (vap->iv_state != IEEE80211_S_RUN)
1356			return;
1357		/*
1358		 * Ignore non-mesh STAs.
1359		 */
1360		if ((scan.capinfo &
1361		     (IEEE80211_CAPINFO_ESS|IEEE80211_CAPINFO_IBSS)) ||
1362		    scan.meshid == NULL || scan.meshconf == NULL) {
1363			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
1364			    wh, "beacon", "%s", "not a mesh sta");
1365			vap->iv_stats.is_mesh_wrongmesh++;
1366			return;
1367		}
1368		/*
1369		 * Ignore STAs for other mesh networks.
1370		 */
1371		if (memcmp(scan.meshid+2, ms->ms_id, ms->ms_idlen) != 0 ||
1372		    mesh_verify_meshconf(vap, scan.meshconf)) {
1373			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
1374			    wh, "beacon", "%s", "not for our mesh");
1375			vap->iv_stats.is_mesh_wrongmesh++;
1376			return;
1377		}
1378		/*
1379		 * Peer only based on the current ACL policy.
1380		 */
1381		if (vap->iv_acl != NULL && !vap->iv_acl->iac_check(vap, wh)) {
1382			IEEE80211_DISCARD(vap, IEEE80211_MSG_ACL,
1383			    wh, NULL, "%s", "disallowed by ACL");
1384			vap->iv_stats.is_rx_acl++;
1385			return;
1386		}
1387		/*
1388		 * Do neighbor discovery.
1389		 */
1390		if (!IEEE80211_ADDR_EQ(wh->i_addr2, ni->ni_macaddr)) {
1391			/*
1392			 * Create a new entry in the neighbor table.
1393			 */
1394			ni = ieee80211_add_neighbor(vap, wh, &scan);
1395		}
1396		/*
1397		 * Automatically peer with discovered nodes if possible.
1398		 * XXX backoff on repeated failure
1399		 */
1400		if (ni != vap->iv_bss &&
1401		    (ms->ms_flags & IEEE80211_MESHFLAGS_AP) &&
1402		    ni->ni_mlstate == IEEE80211_NODE_MESH_IDLE) {
1403			uint16_t args[1];
1404
1405			ni->ni_mlpid = mesh_generateid(vap);
1406			if (ni->ni_mlpid == 0)
1407				return;
1408			mesh_linkchange(ni, IEEE80211_NODE_MESH_OPENSNT);
1409			args[0] = ni->ni_mlpid;
1410			ieee80211_send_action(ni,
1411			    IEEE80211_ACTION_CAT_MESHPEERING,
1412			    IEEE80211_ACTION_MESHPEERING_OPEN, args);
1413			ni->ni_mlrcnt = 0;
1414			mesh_peer_timeout_setup(ni);
1415		}
1416		break;
1417	}
1418	case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
1419	{
1420		uint8_t *ssid, *meshid, *rates, *xrates;
1421		uint8_t *sfrm;
1422
1423		if (vap->iv_state != IEEE80211_S_RUN) {
1424			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
1425			    wh, NULL, "wrong state %s",
1426			    ieee80211_state_name[vap->iv_state]);
1427			vap->iv_stats.is_rx_mgtdiscard++;
1428			return;
1429		}
1430		if (IEEE80211_IS_MULTICAST(wh->i_addr2)) {
1431			/* frame must be directed */
1432			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
1433			    wh, NULL, "%s", "not unicast");
1434			vap->iv_stats.is_rx_mgtdiscard++;	/* XXX stat */
1435			return;
1436		}
1437		/*
1438		 * prreq frame format
1439		 *      [tlv] ssid
1440		 *      [tlv] supported rates
1441		 *      [tlv] extended supported rates
1442		 *	[tlv] mesh id
1443		 */
1444		ssid = meshid = rates = xrates = NULL;
1445		sfrm = frm;
1446		while (efrm - frm > 1) {
1447			IEEE80211_VERIFY_LENGTH(efrm - frm, frm[1] + 2, return);
1448			switch (*frm) {
1449			case IEEE80211_ELEMID_SSID:
1450				ssid = frm;
1451				break;
1452			case IEEE80211_ELEMID_RATES:
1453				rates = frm;
1454				break;
1455			case IEEE80211_ELEMID_XRATES:
1456				xrates = frm;
1457				break;
1458			case IEEE80211_ELEMID_MESHID:
1459				meshid = frm;
1460				break;
1461			}
1462			frm += frm[1] + 2;
1463		}
1464		IEEE80211_VERIFY_ELEMENT(ssid, IEEE80211_NWID_LEN, return);
1465		IEEE80211_VERIFY_ELEMENT(rates, IEEE80211_RATE_MAXSIZE, return);
1466		if (xrates != NULL)
1467			IEEE80211_VERIFY_ELEMENT(xrates,
1468			    IEEE80211_RATE_MAXSIZE - rates[1], return);
1469		if (meshid != NULL) {
1470			IEEE80211_VERIFY_ELEMENT(meshid,
1471			    IEEE80211_MESHID_LEN, return);
1472			/* NB: meshid, not ssid */
1473			IEEE80211_VERIFY_SSID(vap->iv_bss, meshid, return);
1474		}
1475
1476		/* XXX find a better class or define it's own */
1477		IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_INPUT, wh->i_addr2,
1478		    "%s", "recv probe req");
1479		/*
1480		 * Some legacy 11b clients cannot hack a complete
1481		 * probe response frame.  When the request includes
1482		 * only a bare-bones rate set, communicate this to
1483		 * the transmit side.
1484		 */
1485		ieee80211_send_proberesp(vap, wh->i_addr2, 0);
1486		break;
1487	}
1488
1489	case IEEE80211_FC0_SUBTYPE_ACTION:
1490	case IEEE80211_FC0_SUBTYPE_ACTION_NOACK:
1491		if (ni == vap->iv_bss) {
1492			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
1493			    wh, NULL, "%s", "unknown node");
1494			vap->iv_stats.is_rx_mgtdiscard++;
1495		} else if (!IEEE80211_ADDR_EQ(vap->iv_myaddr, wh->i_addr1) &&
1496		    !IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1497			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
1498			    wh, NULL, "%s", "not for us");
1499			vap->iv_stats.is_rx_mgtdiscard++;
1500		} else if (vap->iv_state != IEEE80211_S_RUN) {
1501			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
1502			    wh, NULL, "wrong state %s",
1503			    ieee80211_state_name[vap->iv_state]);
1504			vap->iv_stats.is_rx_mgtdiscard++;
1505		} else {
1506			if (ieee80211_parse_action(ni, m0) == 0)
1507				(void)ic->ic_recv_action(ni, wh, frm, efrm);
1508		}
1509		break;
1510
1511	case IEEE80211_FC0_SUBTYPE_ASSOC_REQ:
1512	case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
1513	case IEEE80211_FC0_SUBTYPE_REASSOC_REQ:
1514	case IEEE80211_FC0_SUBTYPE_REASSOC_RESP:
1515	case IEEE80211_FC0_SUBTYPE_ATIM:
1516	case IEEE80211_FC0_SUBTYPE_DISASSOC:
1517	case IEEE80211_FC0_SUBTYPE_AUTH:
1518	case IEEE80211_FC0_SUBTYPE_DEAUTH:
1519		IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
1520		    wh, NULL, "%s", "not handled");
1521		vap->iv_stats.is_rx_mgtdiscard++;
1522		break;
1523
1524	default:
1525		IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
1526		    wh, "mgt", "subtype 0x%x not handled", subtype);
1527		vap->iv_stats.is_rx_badsubtype++;
1528		break;
1529	}
1530}
1531
1532static void
1533mesh_recv_ctl(struct ieee80211_node *ni, struct mbuf *m, int subtype)
1534{
1535
1536	switch (subtype) {
1537	case IEEE80211_FC0_SUBTYPE_BAR:
1538		ieee80211_recv_bar(ni, m);
1539		break;
1540	}
1541}
1542
1543/*
1544 * Parse meshpeering action ie's for open+confirm frames; the
1545 * important bits are returned in the supplied structure.
1546 */
1547static const struct ieee80211_meshpeer_ie *
1548mesh_parse_meshpeering_action(struct ieee80211_node *ni,
1549	const struct ieee80211_frame *wh,	/* XXX for VERIFY_LENGTH */
1550	const uint8_t *frm, const uint8_t *efrm,
1551	struct ieee80211_meshpeer_ie *mp, uint8_t subtype)
1552{
1553	struct ieee80211vap *vap = ni->ni_vap;
1554	const struct ieee80211_meshpeer_ie *mpie;
1555	const uint8_t *meshid, *meshconf, *meshpeer;
1556
1557	meshid = meshconf = meshpeer = NULL;
1558	while (efrm - frm > 1) {
1559		IEEE80211_VERIFY_LENGTH(efrm - frm, frm[1] + 2, return NULL);
1560		switch (*frm) {
1561		case IEEE80211_ELEMID_MESHID:
1562			meshid = frm;
1563			break;
1564		case IEEE80211_ELEMID_MESHCONF:
1565			meshconf = frm;
1566			break;
1567		case IEEE80211_ELEMID_MESHPEER:
1568			meshpeer = frm;
1569			mpie = (const struct ieee80211_meshpeer_ie *) frm;
1570			memset(mp, 0, sizeof(*mp));
1571			mp->peer_llinkid = LE_READ_2(&mpie->peer_llinkid);
1572			/* NB: peer link ID is optional on these frames */
1573			if (subtype == IEEE80211_MESH_PEER_LINK_CLOSE &&
1574			    mpie->peer_len == 8) {
1575				mp->peer_linkid = 0;
1576				mp->peer_rcode = LE_READ_2(&mpie->peer_linkid);
1577			} else {
1578				mp->peer_linkid = LE_READ_2(&mpie->peer_linkid);
1579				mp->peer_rcode = LE_READ_2(&mpie->peer_rcode);
1580			}
1581			break;
1582		}
1583		frm += frm[1] + 2;
1584	}
1585
1586	/*
1587	 * Verify the contents of the frame. Action frames with
1588	 * close subtype don't have a Mesh Configuration IE.
1589	 * If if fails validation, close the peer link.
1590	 */
1591	KASSERT(meshpeer != NULL &&
1592	    subtype != IEEE80211_ACTION_MESHPEERING_CLOSE,
1593	    ("parsing close action"));
1594
1595	if (mesh_verify_meshid(vap, meshid) ||
1596	    mesh_verify_meshpeer(vap, subtype, meshpeer) ||
1597	    mesh_verify_meshconf(vap, meshconf)) {
1598		uint16_t args[3];
1599
1600		IEEE80211_DISCARD(vap,
1601		    IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH,
1602		    wh, NULL, "%s", "not for our mesh");
1603		vap->iv_stats.is_rx_mgtdiscard++;
1604		switch (ni->ni_mlstate) {
1605		case IEEE80211_NODE_MESH_IDLE:
1606		case IEEE80211_NODE_MESH_ESTABLISHED:
1607		case IEEE80211_NODE_MESH_HOLDING:
1608			/* ignore */
1609			break;
1610		case IEEE80211_NODE_MESH_OPENSNT:
1611		case IEEE80211_NODE_MESH_OPENRCV:
1612		case IEEE80211_NODE_MESH_CONFIRMRCV:
1613			args[0] = ni->ni_mlpid;
1614			args[1] = ni->ni_mllid;
1615			args[2] = IEEE80211_REASON_PEER_LINK_CANCELED;
1616			ieee80211_send_action(ni,
1617			    IEEE80211_ACTION_CAT_MESHPEERING,
1618			    IEEE80211_ACTION_MESHPEERING_CLOSE,
1619			    args);
1620			mesh_linkchange(ni, IEEE80211_NODE_MESH_HOLDING);
1621			mesh_peer_timeout_setup(ni);
1622			break;
1623		}
1624		return NULL;
1625	}
1626	return (const struct ieee80211_meshpeer_ie *) mp;
1627}
1628
1629static int
1630mesh_recv_action_meshpeering_open(struct ieee80211_node *ni,
1631	const struct ieee80211_frame *wh,
1632	const uint8_t *frm, const uint8_t *efrm)
1633{
1634	struct ieee80211vap *vap = ni->ni_vap;
1635	struct ieee80211_meshpeer_ie ie;
1636	const struct ieee80211_meshpeer_ie *meshpeer;
1637	uint16_t args[3];
1638
1639	/* +2+2 for action + code + capabilites */
1640	meshpeer = mesh_parse_meshpeering_action(ni, wh, frm+2+2, efrm, &ie,
1641	    IEEE80211_ACTION_MESHPEERING_OPEN);
1642	if (meshpeer == NULL) {
1643		return 0;
1644	}
1645
1646	/* XXX move up */
1647	IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH, ni,
1648	    "recv PEER OPEN, lid 0x%x", meshpeer->peer_llinkid);
1649
1650	switch (ni->ni_mlstate) {
1651	case IEEE80211_NODE_MESH_IDLE:
1652		mesh_linkchange(ni, IEEE80211_NODE_MESH_OPENRCV);
1653		ni->ni_mllid = meshpeer->peer_llinkid;
1654		ni->ni_mlpid = mesh_generateid(vap);
1655		if (ni->ni_mlpid == 0)
1656			return 0;		/* XXX */
1657		args[0] = ni->ni_mlpid;
1658		/* Announce we're open too... */
1659		ieee80211_send_action(ni,
1660		    IEEE80211_ACTION_CAT_MESHPEERING,
1661		    IEEE80211_ACTION_MESHPEERING_OPEN, args);
1662		/* ...and confirm the link. */
1663		args[0] = ni->ni_mlpid;
1664		args[1] = ni->ni_mllid;
1665		ieee80211_send_action(ni,
1666		    IEEE80211_ACTION_CAT_MESHPEERING,
1667		    IEEE80211_ACTION_MESHPEERING_CONFIRM,
1668		    args);
1669		mesh_peer_timeout_setup(ni);
1670		break;
1671	case IEEE80211_NODE_MESH_OPENRCV:
1672		/* Wrong Link ID */
1673		if (ni->ni_mllid != meshpeer->peer_llinkid) {
1674			args[0] = ni->ni_mllid;
1675			args[1] = ni->ni_mlpid;
1676			args[2] = IEEE80211_REASON_PEER_LINK_CANCELED;
1677			ieee80211_send_action(ni,
1678			    IEEE80211_ACTION_CAT_MESHPEERING,
1679			    IEEE80211_ACTION_MESHPEERING_CLOSE,
1680			    args);
1681			mesh_linkchange(ni, IEEE80211_NODE_MESH_HOLDING);
1682			mesh_peer_timeout_setup(ni);
1683			break;
1684		}
1685		/* Duplicate open, confirm again. */
1686		args[0] = ni->ni_mlpid;
1687		args[1] = ni->ni_mllid;
1688		ieee80211_send_action(ni,
1689		    IEEE80211_ACTION_CAT_MESHPEERING,
1690		    IEEE80211_ACTION_MESHPEERING_CONFIRM,
1691		    args);
1692		break;
1693	case IEEE80211_NODE_MESH_OPENSNT:
1694		ni->ni_mllid = meshpeer->peer_llinkid;
1695		mesh_linkchange(ni, IEEE80211_NODE_MESH_OPENRCV);
1696		args[0] = ni->ni_mlpid;
1697		args[1] = ni->ni_mllid;
1698		ieee80211_send_action(ni,
1699		    IEEE80211_ACTION_CAT_MESHPEERING,
1700		    IEEE80211_ACTION_MESHPEERING_CONFIRM,
1701		    args);
1702		/* NB: don't setup/clear any timeout */
1703		break;
1704	case IEEE80211_NODE_MESH_CONFIRMRCV:
1705		if (ni->ni_mlpid != meshpeer->peer_linkid ||
1706		    ni->ni_mllid != meshpeer->peer_llinkid) {
1707			args[0] = ni->ni_mlpid;
1708			args[1] = ni->ni_mllid;
1709			args[2] = IEEE80211_REASON_PEER_LINK_CANCELED;
1710			ieee80211_send_action(ni,
1711			    IEEE80211_ACTION_CAT_MESHPEERING,
1712			    IEEE80211_ACTION_MESHPEERING_CLOSE,
1713			    args);
1714			mesh_linkchange(ni,
1715			    IEEE80211_NODE_MESH_HOLDING);
1716			mesh_peer_timeout_setup(ni);
1717			break;
1718		}
1719		mesh_linkchange(ni, IEEE80211_NODE_MESH_ESTABLISHED);
1720		ni->ni_mllid = meshpeer->peer_llinkid;
1721		args[0] = ni->ni_mlpid;
1722		args[1] = ni->ni_mllid;
1723		ieee80211_send_action(ni,
1724		    IEEE80211_ACTION_CAT_MESHPEERING,
1725		    IEEE80211_ACTION_MESHPEERING_CONFIRM,
1726		    args);
1727		mesh_peer_timeout_stop(ni);
1728		break;
1729	case IEEE80211_NODE_MESH_ESTABLISHED:
1730		if (ni->ni_mllid != meshpeer->peer_llinkid) {
1731			args[0] = ni->ni_mllid;
1732			args[1] = ni->ni_mlpid;
1733			args[2] = IEEE80211_REASON_PEER_LINK_CANCELED;
1734			ieee80211_send_action(ni,
1735			    IEEE80211_ACTION_CAT_MESHPEERING,
1736			    IEEE80211_ACTION_MESHPEERING_CLOSE,
1737			    args);
1738			mesh_linkchange(ni, IEEE80211_NODE_MESH_HOLDING);
1739			mesh_peer_timeout_setup(ni);
1740			break;
1741		}
1742		args[0] = ni->ni_mlpid;
1743		args[1] = ni->ni_mllid;
1744		ieee80211_send_action(ni,
1745		    IEEE80211_ACTION_CAT_MESHPEERING,
1746		    IEEE80211_ACTION_MESHPEERING_CONFIRM,
1747		    args);
1748		break;
1749	case IEEE80211_NODE_MESH_HOLDING:
1750		args[0] = ni->ni_mlpid;
1751		args[1] = meshpeer->peer_llinkid;
1752		args[2] = IEEE80211_REASON_MESH_MAX_RETRIES;
1753		ieee80211_send_action(ni,
1754		    IEEE80211_ACTION_CAT_MESHPEERING,
1755		    IEEE80211_ACTION_MESHPEERING_CLOSE,
1756		    args);
1757		break;
1758	}
1759	return 0;
1760}
1761
1762static int
1763mesh_recv_action_meshpeering_confirm(struct ieee80211_node *ni,
1764	const struct ieee80211_frame *wh,
1765	const uint8_t *frm, const uint8_t *efrm)
1766{
1767	struct ieee80211vap *vap = ni->ni_vap;
1768	struct ieee80211_meshpeer_ie ie;
1769	const struct ieee80211_meshpeer_ie *meshpeer;
1770	uint16_t args[3];
1771
1772	/* +2+2+2+2 for action + code + capabilites + status code + AID */
1773	meshpeer = mesh_parse_meshpeering_action(ni, wh, frm+2+2+2+2, efrm, &ie,
1774	    IEEE80211_ACTION_MESHPEERING_CONFIRM);
1775	if (meshpeer == NULL) {
1776		return 0;
1777	}
1778
1779	IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH, ni,
1780	    "recv PEER CONFIRM, local id 0x%x, peer id 0x%x",
1781	    meshpeer->peer_llinkid, meshpeer->peer_linkid);
1782
1783	switch (ni->ni_mlstate) {
1784	case IEEE80211_NODE_MESH_OPENRCV:
1785		mesh_linkchange(ni, IEEE80211_NODE_MESH_ESTABLISHED);
1786		mesh_peer_timeout_stop(ni);
1787		break;
1788	case IEEE80211_NODE_MESH_OPENSNT:
1789		mesh_linkchange(ni, IEEE80211_NODE_MESH_CONFIRMRCV);
1790		break;
1791	case IEEE80211_NODE_MESH_HOLDING:
1792		args[0] = ni->ni_mlpid;
1793		args[1] = meshpeer->peer_llinkid;
1794		args[2] = IEEE80211_REASON_MESH_MAX_RETRIES;
1795		ieee80211_send_action(ni,
1796		    IEEE80211_ACTION_CAT_MESHPEERING,
1797		    IEEE80211_ACTION_MESHPEERING_CLOSE,
1798		    args);
1799		break;
1800	case IEEE80211_NODE_MESH_CONFIRMRCV:
1801		if (ni->ni_mllid != meshpeer->peer_llinkid) {
1802			args[0] = ni->ni_mlpid;
1803			args[1] = ni->ni_mllid;
1804			args[2] = IEEE80211_REASON_PEER_LINK_CANCELED;
1805			ieee80211_send_action(ni,
1806			    IEEE80211_ACTION_CAT_MESHPEERING,
1807			    IEEE80211_ACTION_MESHPEERING_CLOSE,
1808			    args);
1809			mesh_linkchange(ni, IEEE80211_NODE_MESH_HOLDING);
1810			mesh_peer_timeout_setup(ni);
1811		}
1812		break;
1813	default:
1814		IEEE80211_DISCARD(vap,
1815		    IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH,
1816		    wh, NULL, "received confirm in invalid state %d",
1817		    ni->ni_mlstate);
1818		vap->iv_stats.is_rx_mgtdiscard++;
1819		break;
1820	}
1821	return 0;
1822}
1823
1824static int
1825mesh_recv_action_meshpeering_close(struct ieee80211_node *ni,
1826	const struct ieee80211_frame *wh,
1827	const uint8_t *frm, const uint8_t *efrm)
1828{
1829	uint16_t args[3];
1830
1831	IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH,
1832	    ni, "%s", "recv PEER CLOSE");
1833
1834	switch (ni->ni_mlstate) {
1835	case IEEE80211_NODE_MESH_IDLE:
1836		/* ignore */
1837		break;
1838	case IEEE80211_NODE_MESH_OPENRCV:
1839	case IEEE80211_NODE_MESH_OPENSNT:
1840	case IEEE80211_NODE_MESH_CONFIRMRCV:
1841	case IEEE80211_NODE_MESH_ESTABLISHED:
1842		args[0] = ni->ni_mlpid;
1843		args[1] = ni->ni_mllid;
1844		args[2] = IEEE80211_REASON_MESH_CLOSE_RCVD;
1845		ieee80211_send_action(ni,
1846		    IEEE80211_ACTION_CAT_MESHPEERING,
1847		    IEEE80211_ACTION_MESHPEERING_CLOSE,
1848		    args);
1849		mesh_linkchange(ni, IEEE80211_NODE_MESH_HOLDING);
1850		mesh_peer_timeout_setup(ni);
1851		break;
1852	case IEEE80211_NODE_MESH_HOLDING:
1853		mesh_linkchange(ni, IEEE80211_NODE_MESH_IDLE);
1854		mesh_peer_timeout_setup(ni);
1855		break;
1856	}
1857	return 0;
1858}
1859
1860/*
1861 * Link Metric handling.
1862 */
1863static int
1864mesh_recv_action_meshlmetric_req(struct ieee80211_node *ni,
1865	const struct ieee80211_frame *wh,
1866	const uint8_t *frm, const uint8_t *efrm)
1867{
1868	uint32_t metric;
1869
1870	metric = mesh_airtime_calc(ni);
1871	ieee80211_send_action(ni,
1872	    IEEE80211_ACTION_CAT_MESHLMETRIC,
1873	    IEEE80211_ACTION_MESHLMETRIC_REP,
1874	    &metric);
1875	return 0;
1876}
1877
1878static int
1879mesh_recv_action_meshlmetric_rep(struct ieee80211_node *ni,
1880	const struct ieee80211_frame *wh,
1881	const uint8_t *frm, const uint8_t *efrm)
1882{
1883	return 0;
1884}
1885
1886static int
1887mesh_send_action(struct ieee80211_node *ni, struct mbuf *m)
1888{
1889	struct ieee80211_bpf_params params;
1890
1891	memset(&params, 0, sizeof(params));
1892	params.ibp_pri = WME_AC_VO;
1893	params.ibp_rate0 = ni->ni_txparms->mgmtrate;
1894	/* XXX ucast/mcast */
1895	params.ibp_try0 = ni->ni_txparms->maxretry;
1896	params.ibp_power = ni->ni_txpower;
1897	return ieee80211_mgmt_output(ni, m, IEEE80211_FC0_SUBTYPE_ACTION,
1898	     &params);
1899}
1900
1901#define	ADDSHORT(frm, v) do {			\
1902	frm[0] = (v) & 0xff;			\
1903	frm[1] = (v) >> 8;			\
1904	frm += 2;				\
1905} while (0)
1906#define	ADDWORD(frm, v) do {			\
1907	frm[0] = (v) & 0xff;			\
1908	frm[1] = ((v) >> 8) & 0xff;		\
1909	frm[2] = ((v) >> 16) & 0xff;		\
1910	frm[3] = ((v) >> 24) & 0xff;		\
1911	frm += 4;				\
1912} while (0)
1913
1914static int
1915mesh_send_action_meshpeering_open(struct ieee80211_node *ni,
1916	int category, int action, void *args0)
1917{
1918	struct ieee80211vap *vap = ni->ni_vap;
1919	struct ieee80211com *ic = ni->ni_ic;
1920	uint16_t *args = args0;
1921	const struct ieee80211_rateset *rs;
1922	struct mbuf *m;
1923	uint8_t *frm;
1924
1925	IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH, ni,
1926	    "send PEER OPEN action: localid 0x%x", args[0]);
1927
1928	IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
1929	    "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", __func__, __LINE__,
1930	    ni, ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)+1);
1931	ieee80211_ref_node(ni);
1932
1933	m = ieee80211_getmgtframe(&frm,
1934	    ic->ic_headroom + sizeof(struct ieee80211_frame),
1935	    sizeof(uint16_t)	/* action+category */
1936	    + sizeof(uint16_t)	/* capabilites */
1937	    + 2 + IEEE80211_RATE_SIZE
1938	    + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
1939	    + 2 + IEEE80211_MESHID_LEN
1940	    + sizeof(struct ieee80211_meshconf_ie)
1941	    + sizeof(struct ieee80211_meshpeer_ie)
1942	);
1943	if (m != NULL) {
1944		/*
1945		 * mesh peer open action frame format:
1946		 *   [1] category
1947		 *   [1] action
1948		 *   [2] capabilities
1949		 *   [tlv] rates
1950		 *   [tlv] xrates
1951		 *   [tlv] mesh id
1952		 *   [tlv] mesh conf
1953		 *   [tlv] mesh peer link mgmt
1954		 */
1955		*frm++ = category;
1956		*frm++ = action;
1957		ADDSHORT(frm, ieee80211_getcapinfo(vap, ni->ni_chan));
1958		rs = ieee80211_get_suprates(ic, ic->ic_curchan);
1959		frm = ieee80211_add_rates(frm, rs);
1960		frm = ieee80211_add_xrates(frm, rs);
1961		frm = ieee80211_add_meshid(frm, vap);
1962		frm = ieee80211_add_meshconf(frm, vap);
1963		frm = ieee80211_add_meshpeer(frm, IEEE80211_MESH_PEER_LINK_OPEN,
1964		    args[0], 0, 0);
1965		m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
1966		return mesh_send_action(ni, m);
1967	} else {
1968		vap->iv_stats.is_tx_nobuf++;
1969		ieee80211_free_node(ni);
1970		return ENOMEM;
1971	}
1972}
1973
1974static int
1975mesh_send_action_meshpeering_confirm(struct ieee80211_node *ni,
1976	int category, int action, void *args0)
1977{
1978	struct ieee80211vap *vap = ni->ni_vap;
1979	struct ieee80211com *ic = ni->ni_ic;
1980	uint16_t *args = args0;
1981	const struct ieee80211_rateset *rs;
1982	struct mbuf *m;
1983	uint8_t *frm;
1984
1985	IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH, ni,
1986	    "send PEER CONFIRM action: localid 0x%x, peerid 0x%x",
1987	    args[0], args[1]);
1988
1989	IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
1990	    "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", __func__, __LINE__,
1991	    ni, ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)+1);
1992	ieee80211_ref_node(ni);
1993
1994	m = ieee80211_getmgtframe(&frm,
1995	    ic->ic_headroom + sizeof(struct ieee80211_frame),
1996	    sizeof(uint16_t)	/* action+category */
1997	    + sizeof(uint16_t)	/* capabilites */
1998	    + sizeof(uint16_t)	/* status code */
1999	    + sizeof(uint16_t)	/* AID */
2000	    + 2 + IEEE80211_RATE_SIZE
2001	    + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
2002	    + 2 + IEEE80211_MESHID_LEN
2003	    + sizeof(struct ieee80211_meshconf_ie)
2004	    + sizeof(struct ieee80211_meshpeer_ie)
2005	);
2006	if (m != NULL) {
2007		/*
2008		 * mesh peer confirm action frame format:
2009		 *   [1] category
2010		 *   [1] action
2011		 *   [2] capabilities
2012		 *   [2] status code
2013		 *   [2] association id (peer ID)
2014		 *   [tlv] rates
2015		 *   [tlv] xrates
2016		 *   [tlv] mesh id
2017		 *   [tlv] mesh conf
2018		 *   [tlv] mesh peer link mgmt
2019		 */
2020		*frm++ = category;
2021		*frm++ = action;
2022		ADDSHORT(frm, ieee80211_getcapinfo(vap, ni->ni_chan));
2023		ADDSHORT(frm, 0);		/* status code */
2024		ADDSHORT(frm, args[1]);		/* AID */
2025		rs = ieee80211_get_suprates(ic, ic->ic_curchan);
2026		frm = ieee80211_add_rates(frm, rs);
2027		frm = ieee80211_add_xrates(frm, rs);
2028		frm = ieee80211_add_meshid(frm, vap);
2029		frm = ieee80211_add_meshconf(frm, vap);
2030		frm = ieee80211_add_meshpeer(frm,
2031		    IEEE80211_MESH_PEER_LINK_CONFIRM,
2032		    args[0], args[1], 0);
2033		m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2034		return mesh_send_action(ni, m);
2035	} else {
2036		vap->iv_stats.is_tx_nobuf++;
2037		ieee80211_free_node(ni);
2038		return ENOMEM;
2039	}
2040}
2041
2042static int
2043mesh_send_action_meshpeering_close(struct ieee80211_node *ni,
2044	int category, int action, void *args0)
2045{
2046	struct ieee80211vap *vap = ni->ni_vap;
2047	struct ieee80211com *ic = ni->ni_ic;
2048	uint16_t *args = args0;
2049	struct mbuf *m;
2050	uint8_t *frm;
2051
2052	IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH, ni,
2053	    "send PEER CLOSE action: localid 0x%x, peerid 0x%x reason %d",
2054	    args[0], args[1], args[2]);
2055
2056	IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
2057	    "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", __func__, __LINE__,
2058	    ni, ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)+1);
2059	ieee80211_ref_node(ni);
2060
2061	m = ieee80211_getmgtframe(&frm,
2062	    ic->ic_headroom + sizeof(struct ieee80211_frame),
2063	    sizeof(uint16_t)	/* action+category */
2064	    + sizeof(uint16_t)	/* reason code */
2065	    + 2 + IEEE80211_MESHID_LEN
2066	    + sizeof(struct ieee80211_meshpeer_ie)
2067	);
2068	if (m != NULL) {
2069		/*
2070		 * mesh peer close action frame format:
2071		 *   [1] category
2072		 *   [1] action
2073		 *   [2] reason code
2074		 *   [tlv] mesh id
2075		 *   [tlv] mesh peer link mgmt
2076		 */
2077		*frm++ = category;
2078		*frm++ = action;
2079		ADDSHORT(frm, args[2]);		/* reason code */
2080		frm = ieee80211_add_meshid(frm, vap);
2081		frm = ieee80211_add_meshpeer(frm,
2082		    IEEE80211_MESH_PEER_LINK_CLOSE,
2083		    args[0], args[1], args[2]);
2084		m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2085		return mesh_send_action(ni, m);
2086	} else {
2087		vap->iv_stats.is_tx_nobuf++;
2088		ieee80211_free_node(ni);
2089		return ENOMEM;
2090	}
2091}
2092
2093static int
2094mesh_send_action_meshlink_request(struct ieee80211_node *ni,
2095	int category, int action, void *arg0)
2096{
2097	struct ieee80211vap *vap = ni->ni_vap;
2098	struct ieee80211com *ic = ni->ni_ic;
2099	struct mbuf *m;
2100	uint8_t *frm;
2101
2102	IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH, ni,
2103	    "%s", "send LINK METRIC REQUEST action");
2104
2105	IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
2106	    "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", __func__, __LINE__,
2107	    ni, ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)+1);
2108	ieee80211_ref_node(ni);
2109
2110	m = ieee80211_getmgtframe(&frm,
2111	    ic->ic_headroom + sizeof(struct ieee80211_frame),
2112	    sizeof(uint16_t)	/* action+category */
2113	);
2114	if (m != NULL) {
2115		/*
2116		 * mesh link metric request
2117		 *   [1] category
2118		 *   [1] action
2119		 */
2120		*frm++ = category;
2121		*frm++ = action;
2122		m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2123		return mesh_send_action(ni, m);
2124	} else {
2125		vap->iv_stats.is_tx_nobuf++;
2126		ieee80211_free_node(ni);
2127		return ENOMEM;
2128	}
2129}
2130
2131static int
2132mesh_send_action_meshlink_reply(struct ieee80211_node *ni,
2133	int category, int action, void *args0)
2134{
2135	struct ieee80211vap *vap = ni->ni_vap;
2136	struct ieee80211com *ic = ni->ni_ic;
2137	uint32_t *metric = args0;
2138	struct mbuf *m;
2139	uint8_t *frm;
2140
2141	IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH, ni,
2142	    "send LINK METRIC REPLY action: metric 0x%x", *metric);
2143
2144	IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
2145	    "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", __func__, __LINE__,
2146	    ni, ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)+1);
2147	ieee80211_ref_node(ni);
2148
2149	m = ieee80211_getmgtframe(&frm,
2150	    ic->ic_headroom + sizeof(struct ieee80211_frame),
2151	    sizeof(uint16_t)	/* action+category */
2152	    + sizeof(struct ieee80211_meshlmetric_ie)
2153	);
2154	if (m != NULL) {
2155		/*
2156		 * mesh link metric reply
2157		 *   [1] category
2158		 *   [1] action
2159		 *   [tlv] mesh link metric
2160		 */
2161		*frm++ = category;
2162		*frm++ = action;
2163		frm = ieee80211_add_meshlmetric(frm, *metric);
2164		m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2165		return mesh_send_action(ni, m);
2166	} else {
2167		vap->iv_stats.is_tx_nobuf++;
2168		ieee80211_free_node(ni);
2169		return ENOMEM;
2170	}
2171}
2172
2173static void
2174mesh_peer_timeout_setup(struct ieee80211_node *ni)
2175{
2176	switch (ni->ni_mlstate) {
2177	case IEEE80211_NODE_MESH_HOLDING:
2178		ni->ni_mltval = ieee80211_mesh_holdingtimeout;
2179		break;
2180	case IEEE80211_NODE_MESH_CONFIRMRCV:
2181		ni->ni_mltval = ieee80211_mesh_confirmtimeout;
2182		break;
2183	case IEEE80211_NODE_MESH_IDLE:
2184		ni->ni_mltval = 0;
2185		break;
2186	default:
2187		ni->ni_mltval = ieee80211_mesh_retrytimeout;
2188		break;
2189	}
2190	if (ni->ni_mltval)
2191		callout_reset(&ni->ni_mltimer, ni->ni_mltval,
2192		    mesh_peer_timeout_cb, ni);
2193}
2194
2195/*
2196 * Same as above but backoffs timer statisically 50%.
2197 */
2198static void
2199mesh_peer_timeout_backoff(struct ieee80211_node *ni)
2200{
2201	uint32_t r;
2202
2203	r = arc4random();
2204	ni->ni_mltval += r % ni->ni_mltval;
2205	callout_reset(&ni->ni_mltimer, ni->ni_mltval, mesh_peer_timeout_cb,
2206	    ni);
2207}
2208
2209static __inline void
2210mesh_peer_timeout_stop(struct ieee80211_node *ni)
2211{
2212	callout_drain(&ni->ni_mltimer);
2213}
2214
2215/*
2216 * Mesh Peer Link Management FSM timeout handling.
2217 */
2218static void
2219mesh_peer_timeout_cb(void *arg)
2220{
2221	struct ieee80211_node *ni = (struct ieee80211_node *)arg;
2222	uint16_t args[3];
2223
2224	IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_MESH,
2225	    ni, "mesh link timeout, state %d, retry counter %d",
2226	    ni->ni_mlstate, ni->ni_mlrcnt);
2227
2228	switch (ni->ni_mlstate) {
2229	case IEEE80211_NODE_MESH_IDLE:
2230	case IEEE80211_NODE_MESH_ESTABLISHED:
2231		break;
2232	case IEEE80211_NODE_MESH_OPENSNT:
2233	case IEEE80211_NODE_MESH_OPENRCV:
2234		if (ni->ni_mlrcnt == ieee80211_mesh_maxretries) {
2235			args[0] = ni->ni_mlpid;
2236			args[2] = IEEE80211_REASON_MESH_MAX_RETRIES;
2237			ieee80211_send_action(ni,
2238			    IEEE80211_ACTION_CAT_MESHPEERING,
2239			    IEEE80211_ACTION_MESHPEERING_CLOSE, args);
2240			ni->ni_mlrcnt = 0;
2241			mesh_linkchange(ni, IEEE80211_NODE_MESH_HOLDING);
2242			mesh_peer_timeout_setup(ni);
2243		} else {
2244			args[0] = ni->ni_mlpid;
2245			ieee80211_send_action(ni,
2246			    IEEE80211_ACTION_CAT_MESHPEERING,
2247			    IEEE80211_ACTION_MESHPEERING_OPEN, args);
2248			ni->ni_mlrcnt++;
2249			mesh_peer_timeout_backoff(ni);
2250		}
2251		break;
2252	case IEEE80211_NODE_MESH_CONFIRMRCV:
2253		if (ni->ni_mlrcnt == ieee80211_mesh_maxretries) {
2254			args[0] = ni->ni_mlpid;
2255			args[2] = IEEE80211_REASON_MESH_CONFIRM_TIMEOUT;
2256			ieee80211_send_action(ni,
2257			    IEEE80211_ACTION_CAT_MESHPEERING,
2258			    IEEE80211_ACTION_MESHPEERING_CLOSE, args);
2259			ni->ni_mlrcnt = 0;
2260			mesh_linkchange(ni, IEEE80211_NODE_MESH_HOLDING);
2261			mesh_peer_timeout_setup(ni);
2262		} else {
2263			ni->ni_mlrcnt++;
2264			mesh_peer_timeout_setup(ni);
2265		}
2266		break;
2267	case IEEE80211_NODE_MESH_HOLDING:
2268		mesh_linkchange(ni, IEEE80211_NODE_MESH_IDLE);
2269		break;
2270	}
2271}
2272
2273static int
2274mesh_verify_meshid(struct ieee80211vap *vap, const uint8_t *ie)
2275{
2276	struct ieee80211_mesh_state *ms = vap->iv_mesh;
2277
2278	if (ie == NULL || ie[1] != ms->ms_idlen)
2279		return 1;
2280	return memcmp(ms->ms_id, ie + 2, ms->ms_idlen);
2281}
2282
2283/*
2284 * Check if we are using the same algorithms for this mesh.
2285 */
2286static int
2287mesh_verify_meshconf(struct ieee80211vap *vap, const uint8_t *ie)
2288{
2289	const struct ieee80211_meshconf_ie *meshconf =
2290	    (const struct ieee80211_meshconf_ie *) ie;
2291	const struct ieee80211_mesh_state *ms = vap->iv_mesh;
2292	uint16_t cap;
2293
2294	if (meshconf == NULL)
2295		return 1;
2296	if (meshconf->conf_pselid != ms->ms_ppath->mpp_ie) {
2297		IEEE80211_DPRINTF(vap, IEEE80211_MSG_MESH,
2298		    "unknown path selection algorithm: 0x%x\n",
2299		    meshconf->conf_pselid);
2300		return 1;
2301	}
2302	if (meshconf->conf_pmetid != ms->ms_pmetric->mpm_ie) {
2303		IEEE80211_DPRINTF(vap, IEEE80211_MSG_MESH,
2304		    "unknown path metric algorithm: 0x%x\n",
2305		    meshconf->conf_pmetid);
2306		return 1;
2307	}
2308	if (meshconf->conf_ccid != 0) {
2309		IEEE80211_DPRINTF(vap, IEEE80211_MSG_MESH,
2310		    "unknown congestion control algorithm: 0x%x\n",
2311		    meshconf->conf_ccid);
2312		return 1;
2313	}
2314	if (meshconf->conf_syncid != IEEE80211_MESHCONF_SYNC_NEIGHOFF) {
2315		IEEE80211_DPRINTF(vap, IEEE80211_MSG_MESH,
2316		    "unknown sync algorithm: 0x%x\n",
2317		    meshconf->conf_syncid);
2318		return 1;
2319	}
2320	if (meshconf->conf_authid != 0) {
2321		IEEE80211_DPRINTF(vap, IEEE80211_MSG_MESH,
2322		    "unknown auth auth algorithm: 0x%x\n",
2323		    meshconf->conf_pselid);
2324		return 1;
2325	}
2326	/* NB: conf_cap is only read correctly here */
2327	cap = LE_READ_2(&meshconf->conf_cap);
2328	/* Not accepting peers */
2329	if (!(cap & IEEE80211_MESHCONF_CAP_AP)) {
2330		IEEE80211_DPRINTF(vap, IEEE80211_MSG_MESH,
2331		    "not accepting peers: 0x%x\n", meshconf->conf_cap);
2332		return 1;
2333	}
2334	return 0;
2335}
2336
2337static int
2338mesh_verify_meshpeer(struct ieee80211vap *vap, uint8_t subtype,
2339    const uint8_t *ie)
2340{
2341	const struct ieee80211_meshpeer_ie *meshpeer =
2342	    (const struct ieee80211_meshpeer_ie *) ie;
2343
2344	if (meshpeer == NULL || meshpeer->peer_len < 6 ||
2345	    meshpeer->peer_len > 10)
2346		return 1;
2347	switch (subtype) {
2348	case IEEE80211_MESH_PEER_LINK_OPEN:
2349		if (meshpeer->peer_len != 6)
2350			return 1;
2351		break;
2352	case IEEE80211_MESH_PEER_LINK_CONFIRM:
2353		if (meshpeer->peer_len != 8)
2354			return 1;
2355		break;
2356	case IEEE80211_MESH_PEER_LINK_CLOSE:
2357		if (meshpeer->peer_len < 8)
2358			return 1;
2359		if (meshpeer->peer_len == 8 && meshpeer->peer_linkid != 0)
2360			return 1;
2361		if (meshpeer->peer_rcode == 0)
2362			return 1;
2363		break;
2364	}
2365	return 0;
2366}
2367
2368/*
2369 * Add a Mesh ID IE to a frame.
2370 */
2371uint8_t *
2372ieee80211_add_meshid(uint8_t *frm, struct ieee80211vap *vap)
2373{
2374	struct ieee80211_mesh_state *ms = vap->iv_mesh;
2375
2376	KASSERT(vap->iv_opmode == IEEE80211_M_MBSS, ("not a mbss vap"));
2377
2378	*frm++ = IEEE80211_ELEMID_MESHID;
2379	*frm++ = ms->ms_idlen;
2380	memcpy(frm, ms->ms_id, ms->ms_idlen);
2381	return frm + ms->ms_idlen;
2382}
2383
2384/*
2385 * Add a Mesh Configuration IE to a frame.
2386 * For now just use HWMP routing, Airtime link metric, Null Congestion
2387 * Signaling, Null Sync Protocol and Null Authentication.
2388 */
2389uint8_t *
2390ieee80211_add_meshconf(uint8_t *frm, struct ieee80211vap *vap)
2391{
2392	const struct ieee80211_mesh_state *ms = vap->iv_mesh;
2393	uint16_t caps;
2394
2395	KASSERT(vap->iv_opmode == IEEE80211_M_MBSS, ("not a MBSS vap"));
2396
2397	*frm++ = IEEE80211_ELEMID_MESHCONF;
2398	*frm++ = sizeof(struct ieee80211_meshconf_ie) - 2;
2399	*frm++ = ms->ms_ppath->mpp_ie;		/* path selection */
2400	*frm++ = ms->ms_pmetric->mpm_ie;	/* link metric */
2401	*frm++ = IEEE80211_MESHCONF_CC_DISABLED;
2402	*frm++ = IEEE80211_MESHCONF_SYNC_NEIGHOFF;
2403	*frm++ = IEEE80211_MESHCONF_AUTH_DISABLED;
2404	/* NB: set the number of neighbors before the rest */
2405	*frm = (ms->ms_neighbors > 15 ? 15 : ms->ms_neighbors) << 1;
2406	if (ms->ms_flags & IEEE80211_MESHFLAGS_PORTAL)
2407		*frm |= IEEE80211_MESHCONF_FORM_MP;
2408	frm += 1;
2409	caps = 0;
2410	if (ms->ms_flags & IEEE80211_MESHFLAGS_AP)
2411		caps |= IEEE80211_MESHCONF_CAP_AP;
2412	if (ms->ms_flags & IEEE80211_MESHFLAGS_FWD)
2413		caps |= IEEE80211_MESHCONF_CAP_FWRD;
2414	ADDSHORT(frm, caps);
2415	return frm;
2416}
2417
2418/*
2419 * Add a Mesh Peer Management IE to a frame.
2420 */
2421uint8_t *
2422ieee80211_add_meshpeer(uint8_t *frm, uint8_t subtype, uint16_t localid,
2423    uint16_t peerid, uint16_t reason)
2424{
2425	/* XXX change for AH */
2426	static const uint8_t meshpeerproto[4] = IEEE80211_MESH_PEER_PROTO;
2427
2428	KASSERT(localid != 0, ("localid == 0"));
2429
2430	*frm++ = IEEE80211_ELEMID_MESHPEER;
2431	switch (subtype) {
2432	case IEEE80211_MESH_PEER_LINK_OPEN:
2433		*frm++ = 6;		/* length */
2434		memcpy(frm, meshpeerproto, 4);
2435		frm += 4;
2436		ADDSHORT(frm, localid);	/* local ID */
2437		break;
2438	case IEEE80211_MESH_PEER_LINK_CONFIRM:
2439		KASSERT(peerid != 0, ("sending peer confirm without peer id"));
2440		*frm++ = 8;		/* length */
2441		memcpy(frm, meshpeerproto, 4);
2442		frm += 4;
2443		ADDSHORT(frm, localid);	/* local ID */
2444		ADDSHORT(frm, peerid);	/* peer ID */
2445		break;
2446	case IEEE80211_MESH_PEER_LINK_CLOSE:
2447		if (peerid)
2448			*frm++ = 10;	/* length */
2449		else
2450			*frm++ = 8;	/* length */
2451		memcpy(frm, meshpeerproto, 4);
2452		frm += 4;
2453		ADDSHORT(frm, localid);	/* local ID */
2454		if (peerid)
2455			ADDSHORT(frm, peerid);	/* peer ID */
2456		ADDSHORT(frm, reason);
2457		break;
2458	}
2459	return frm;
2460}
2461
2462/*
2463 * Compute an Airtime Link Metric for the link with this node.
2464 *
2465 * Based on Draft 3.0 spec (11B.10, p.149).
2466 */
2467/*
2468 * Max 802.11s overhead.
2469 */
2470#define IEEE80211_MESH_MAXOVERHEAD \
2471	(sizeof(struct ieee80211_qosframe_addr4) \
2472	 + sizeof(struct ieee80211_meshcntl_ae11) \
2473	+ sizeof(struct llc) \
2474	+ IEEE80211_ADDR_LEN \
2475	+ IEEE80211_WEP_IVLEN \
2476	+ IEEE80211_WEP_KIDLEN \
2477	+ IEEE80211_WEP_CRCLEN \
2478	+ IEEE80211_WEP_MICLEN \
2479	+ IEEE80211_CRC_LEN)
2480uint32_t
2481mesh_airtime_calc(struct ieee80211_node *ni)
2482{
2483#define M_BITS 8
2484#define S_FACTOR (2 * M_BITS)
2485	struct ieee80211com *ic = ni->ni_ic;
2486	struct ifnet *ifp = ni->ni_vap->iv_ifp;
2487	const static int nbits = 8192 << M_BITS;
2488	uint32_t overhead, rate, errrate;
2489	uint64_t res;
2490
2491	/* Time to transmit a frame */
2492	rate = ni->ni_txrate;
2493	overhead = ieee80211_compute_duration(ic->ic_rt,
2494	    ifp->if_mtu + IEEE80211_MESH_MAXOVERHEAD, rate, 0) << M_BITS;
2495	/* Error rate in percentage */
2496	/* XXX assuming small failures are ok */
2497	errrate = (((ifp->if_oerrors +
2498	    ifp->if_ierrors) / 100) << M_BITS) / 100;
2499	res = (overhead + (nbits / rate)) *
2500	    ((1 << S_FACTOR) / ((1 << M_BITS) - errrate));
2501
2502	return (uint32_t)(res >> S_FACTOR);
2503#undef M_BITS
2504#undef S_FACTOR
2505}
2506
2507/*
2508 * Add a Mesh Link Metric report IE to a frame.
2509 */
2510uint8_t *
2511ieee80211_add_meshlmetric(uint8_t *frm, uint32_t metric)
2512{
2513	*frm++ = IEEE80211_ELEMID_MESHLINK;
2514	*frm++ = 4;
2515	ADDWORD(frm, metric);
2516	return frm;
2517}
2518#undef ADDSHORT
2519#undef ADDWORD
2520
2521/*
2522 * Initialize any mesh-specific node state.
2523 */
2524void
2525ieee80211_mesh_node_init(struct ieee80211vap *vap, struct ieee80211_node *ni)
2526{
2527	ni->ni_flags |= IEEE80211_NODE_QOS;
2528	callout_init(&ni->ni_mltimer, CALLOUT_MPSAFE);
2529}
2530
2531/*
2532 * Cleanup any mesh-specific node state.
2533 */
2534void
2535ieee80211_mesh_node_cleanup(struct ieee80211_node *ni)
2536{
2537	struct ieee80211vap *vap = ni->ni_vap;
2538	struct ieee80211_mesh_state *ms = vap->iv_mesh;
2539
2540	callout_drain(&ni->ni_mltimer);
2541	/* NB: short-circuit callbacks after mesh_vdetach */
2542	if (vap->iv_mesh != NULL)
2543		ms->ms_ppath->mpp_peerdown(ni);
2544}
2545
2546void
2547ieee80211_parse_meshid(struct ieee80211_node *ni, const uint8_t *ie)
2548{
2549	ni->ni_meshidlen = ie[1];
2550	memcpy(ni->ni_meshid, ie + 2, ie[1]);
2551}
2552
2553/*
2554 * Setup mesh-specific node state on neighbor discovery.
2555 */
2556void
2557ieee80211_mesh_init_neighbor(struct ieee80211_node *ni,
2558	const struct ieee80211_frame *wh,
2559	const struct ieee80211_scanparams *sp)
2560{
2561	ieee80211_parse_meshid(ni, sp->meshid);
2562}
2563
2564void
2565ieee80211_mesh_update_beacon(struct ieee80211vap *vap,
2566	struct ieee80211_beacon_offsets *bo)
2567{
2568	KASSERT(vap->iv_opmode == IEEE80211_M_MBSS, ("not a MBSS vap"));
2569
2570	if (isset(bo->bo_flags, IEEE80211_BEACON_MESHCONF)) {
2571		(void)ieee80211_add_meshconf(bo->bo_meshconf, vap);
2572		clrbit(bo->bo_flags, IEEE80211_BEACON_MESHCONF);
2573	}
2574}
2575
2576static int
2577mesh_ioctl_get80211(struct ieee80211vap *vap, struct ieee80211req *ireq)
2578{
2579	struct ieee80211_mesh_state *ms = vap->iv_mesh;
2580	uint8_t tmpmeshid[IEEE80211_NWID_LEN];
2581	struct ieee80211_mesh_route *rt;
2582	struct ieee80211req_mesh_route *imr;
2583	size_t len, off;
2584	uint8_t *p;
2585	int error;
2586
2587	if (vap->iv_opmode != IEEE80211_M_MBSS)
2588		return ENOSYS;
2589
2590	error = 0;
2591	switch (ireq->i_type) {
2592	case IEEE80211_IOC_MESH_ID:
2593		ireq->i_len = ms->ms_idlen;
2594		memcpy(tmpmeshid, ms->ms_id, ireq->i_len);
2595		error = copyout(tmpmeshid, ireq->i_data, ireq->i_len);
2596		break;
2597	case IEEE80211_IOC_MESH_AP:
2598		ireq->i_val = (ms->ms_flags & IEEE80211_MESHFLAGS_AP) != 0;
2599		break;
2600	case IEEE80211_IOC_MESH_FWRD:
2601		ireq->i_val = (ms->ms_flags & IEEE80211_MESHFLAGS_FWD) != 0;
2602		break;
2603	case IEEE80211_IOC_MESH_TTL:
2604		ireq->i_val = ms->ms_ttl;
2605		break;
2606	case IEEE80211_IOC_MESH_RTCMD:
2607		switch (ireq->i_val) {
2608		case IEEE80211_MESH_RTCMD_LIST:
2609			len = 0;
2610			MESH_RT_LOCK(ms);
2611			TAILQ_FOREACH(rt, &ms->ms_routes, rt_next) {
2612				len += sizeof(*imr);
2613			}
2614			MESH_RT_UNLOCK(ms);
2615			if (len > ireq->i_len || ireq->i_len < sizeof(*imr)) {
2616				ireq->i_len = len;
2617				return ENOMEM;
2618			}
2619			ireq->i_len = len;
2620			/* XXX M_WAIT? */
2621			p = malloc(len, M_TEMP, M_NOWAIT | M_ZERO);
2622			if (p == NULL)
2623				return ENOMEM;
2624			off = 0;
2625			MESH_RT_LOCK(ms);
2626			TAILQ_FOREACH(rt, &ms->ms_routes, rt_next) {
2627				if (off >= len)
2628					break;
2629				imr = (struct ieee80211req_mesh_route *)
2630				    (p + off);
2631				imr->imr_flags = rt->rt_flags;
2632				IEEE80211_ADDR_COPY(imr->imr_dest,
2633				    rt->rt_dest);
2634				IEEE80211_ADDR_COPY(imr->imr_nexthop,
2635				    rt->rt_nexthop);
2636				imr->imr_metric = rt->rt_metric;
2637				imr->imr_nhops = rt->rt_nhops;
2638				imr->imr_lifetime = rt->rt_lifetime;
2639				imr->imr_lastmseq = rt->rt_lastmseq;
2640				off += sizeof(*imr);
2641			}
2642			MESH_RT_UNLOCK(ms);
2643			error = copyout(p, (uint8_t *)ireq->i_data,
2644			    ireq->i_len);
2645			free(p, M_TEMP);
2646			break;
2647		case IEEE80211_MESH_RTCMD_FLUSH:
2648		case IEEE80211_MESH_RTCMD_ADD:
2649		case IEEE80211_MESH_RTCMD_DELETE:
2650			return EINVAL;
2651		default:
2652			return ENOSYS;
2653		}
2654		break;
2655	case IEEE80211_IOC_MESH_PR_METRIC:
2656		len = strlen(ms->ms_pmetric->mpm_descr);
2657		if (ireq->i_len < len)
2658			return EINVAL;
2659		ireq->i_len = len;
2660		error = copyout(ms->ms_pmetric->mpm_descr,
2661		    (uint8_t *)ireq->i_data, len);
2662		break;
2663	case IEEE80211_IOC_MESH_PR_PATH:
2664		len = strlen(ms->ms_ppath->mpp_descr);
2665		if (ireq->i_len < len)
2666			return EINVAL;
2667		ireq->i_len = len;
2668		error = copyout(ms->ms_ppath->mpp_descr,
2669		    (uint8_t *)ireq->i_data, len);
2670		break;
2671	default:
2672		return ENOSYS;
2673	}
2674
2675	return error;
2676}
2677IEEE80211_IOCTL_GET(mesh, mesh_ioctl_get80211);
2678
2679static int
2680mesh_ioctl_set80211(struct ieee80211vap *vap, struct ieee80211req *ireq)
2681{
2682	struct ieee80211_mesh_state *ms = vap->iv_mesh;
2683	uint8_t tmpmeshid[IEEE80211_NWID_LEN];
2684	uint8_t tmpaddr[IEEE80211_ADDR_LEN];
2685	char tmpproto[IEEE80211_MESH_PROTO_DSZ];
2686	int error;
2687
2688	if (vap->iv_opmode != IEEE80211_M_MBSS)
2689		return ENOSYS;
2690
2691	error = 0;
2692	switch (ireq->i_type) {
2693	case IEEE80211_IOC_MESH_ID:
2694		if (ireq->i_val != 0 || ireq->i_len > IEEE80211_MESHID_LEN)
2695			return EINVAL;
2696		error = copyin(ireq->i_data, tmpmeshid, ireq->i_len);
2697		if (error != 0)
2698			break;
2699		memset(ms->ms_id, 0, IEEE80211_NWID_LEN);
2700		ms->ms_idlen = ireq->i_len;
2701		memcpy(ms->ms_id, tmpmeshid, ireq->i_len);
2702		error = ENETRESET;
2703		break;
2704	case IEEE80211_IOC_MESH_AP:
2705		if (ireq->i_val)
2706			ms->ms_flags |= IEEE80211_MESHFLAGS_AP;
2707		else
2708			ms->ms_flags &= ~IEEE80211_MESHFLAGS_AP;
2709		error = ENETRESET;
2710		break;
2711	case IEEE80211_IOC_MESH_FWRD:
2712		if (ireq->i_val)
2713			ms->ms_flags |= IEEE80211_MESHFLAGS_FWD;
2714		else
2715			ms->ms_flags &= ~IEEE80211_MESHFLAGS_FWD;
2716		break;
2717	case IEEE80211_IOC_MESH_TTL:
2718		ms->ms_ttl = (uint8_t) ireq->i_val;
2719		break;
2720	case IEEE80211_IOC_MESH_RTCMD:
2721		switch (ireq->i_val) {
2722		case IEEE80211_MESH_RTCMD_LIST:
2723			return EINVAL;
2724		case IEEE80211_MESH_RTCMD_FLUSH:
2725			ieee80211_mesh_rt_flush(vap);
2726			break;
2727		case IEEE80211_MESH_RTCMD_ADD:
2728			if (IEEE80211_ADDR_EQ(vap->iv_myaddr, ireq->i_data) ||
2729			    IEEE80211_ADDR_EQ(broadcastaddr, ireq->i_data))
2730				return EINVAL;
2731			error = copyin(ireq->i_data, &tmpaddr,
2732			    IEEE80211_ADDR_LEN);
2733			if (error == 0)
2734				ieee80211_mesh_discover(vap, tmpaddr, NULL);
2735			break;
2736		case IEEE80211_MESH_RTCMD_DELETE:
2737			ieee80211_mesh_rt_del(vap, ireq->i_data);
2738			break;
2739		default:
2740			return ENOSYS;
2741		}
2742		break;
2743	case IEEE80211_IOC_MESH_PR_METRIC:
2744		error = copyin(ireq->i_data, tmpproto, sizeof(tmpproto));
2745		if (error == 0) {
2746			error = mesh_select_proto_metric(vap, tmpproto);
2747			if (error == 0)
2748				error = ENETRESET;
2749		}
2750		break;
2751	case IEEE80211_IOC_MESH_PR_PATH:
2752		error = copyin(ireq->i_data, tmpproto, sizeof(tmpproto));
2753		if (error == 0) {
2754			error = mesh_select_proto_path(vap, tmpproto);
2755			if (error == 0)
2756				error = ENETRESET;
2757		}
2758		break;
2759	default:
2760		return ENOSYS;
2761	}
2762	return error;
2763}
2764IEEE80211_IOCTL_SET(mesh, mesh_ioctl_set80211);
2765