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