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