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