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