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