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