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