xref: /freebsd/sys/net80211/ieee80211_mesh.c (revision a4dc509f723944821bcfcc52005ff87c9a5dee5b)
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/bpf.h>
56 #include <net/if.h>
57 #include <net/if_var.h>
58 #include <net/if_media.h>
59 #include <net/if_llc.h>
60 #include <net/ethernet.h>
61 
62 #include <net80211/ieee80211_var.h>
63 #include <net80211/ieee80211_action.h>
64 #ifdef IEEE80211_SUPPORT_SUPERG
65 #include <net80211/ieee80211_superg.h>
66 #endif
67 #include <net80211/ieee80211_input.h>
68 #include <net80211/ieee80211_mesh.h>
69 
70 static void	mesh_rt_flush_invalid(struct ieee80211vap *);
71 static int	mesh_select_proto_path(struct ieee80211vap *, const char *);
72 static int	mesh_select_proto_metric(struct ieee80211vap *, const char *);
73 static void	mesh_vattach(struct ieee80211vap *);
74 static int	mesh_newstate(struct ieee80211vap *, enum ieee80211_state, int);
75 static void	mesh_rt_cleanup_cb(void *);
76 static void	mesh_gatemode_setup(struct ieee80211vap *);
77 static void	mesh_gatemode_cb(void *);
78 static void	mesh_linkchange(struct ieee80211_node *,
79 		    enum ieee80211_mesh_mlstate);
80 static void	mesh_checkid(void *, struct ieee80211_node *);
81 static uint32_t	mesh_generateid(struct ieee80211vap *);
82 static int	mesh_checkpseq(struct ieee80211vap *,
83 		    const uint8_t [IEEE80211_ADDR_LEN], uint32_t);
84 static void	mesh_transmit_to_gate(struct ieee80211vap *, struct mbuf *,
85 		    struct ieee80211_mesh_route *);
86 static void	mesh_forward(struct ieee80211vap *, struct mbuf *,
87 		    const struct ieee80211_meshcntl *);
88 static int	mesh_input(struct ieee80211_node *, struct mbuf *,
89 		    const struct ieee80211_rx_stats *rxs, int, int);
90 static void	mesh_recv_mgmt(struct ieee80211_node *, struct mbuf *, int,
91 		    const struct ieee80211_rx_stats *rxs, int, int);
92 static void	mesh_recv_ctl(struct ieee80211_node *, struct mbuf *, int);
93 static void	mesh_peer_timeout_setup(struct ieee80211_node *);
94 static void	mesh_peer_timeout_backoff(struct ieee80211_node *);
95 static void	mesh_peer_timeout_cb(void *);
96 static __inline void
97 		mesh_peer_timeout_stop(struct ieee80211_node *);
98 static int	mesh_verify_meshid(struct ieee80211vap *, const uint8_t *);
99 static int	mesh_verify_meshconf(struct ieee80211vap *, const uint8_t *);
100 static int	mesh_verify_meshpeer(struct ieee80211vap *, uint8_t,
101     		    const uint8_t *);
102 uint32_t	mesh_airtime_calc(struct ieee80211_node *);
103 
104 /*
105  * Timeout values come from the specification and are in milliseconds.
106  */
107 static SYSCTL_NODE(_net_wlan, OID_AUTO, mesh, CTLFLAG_RD, 0,
108     "IEEE 802.11s parameters");
109 static int	ieee80211_mesh_gateint = -1;
110 SYSCTL_PROC(_net_wlan_mesh, OID_AUTO, gateint, CTLTYPE_INT | CTLFLAG_RW,
111     &ieee80211_mesh_gateint, 0, ieee80211_sysctl_msecs_ticks, "I",
112     "mesh gate interval (ms)");
113 static int ieee80211_mesh_retrytimeout = -1;
114 SYSCTL_PROC(_net_wlan_mesh, OID_AUTO, retrytimeout, CTLTYPE_INT | CTLFLAG_RW,
115     &ieee80211_mesh_retrytimeout, 0, ieee80211_sysctl_msecs_ticks, "I",
116     "Retry timeout (msec)");
117 static int ieee80211_mesh_holdingtimeout = -1;
118 
119 SYSCTL_PROC(_net_wlan_mesh, OID_AUTO, holdingtimeout, CTLTYPE_INT | CTLFLAG_RW,
120     &ieee80211_mesh_holdingtimeout, 0, ieee80211_sysctl_msecs_ticks, "I",
121     "Holding state timeout (msec)");
122 static int ieee80211_mesh_confirmtimeout = -1;
123 SYSCTL_PROC(_net_wlan_mesh, OID_AUTO, confirmtimeout, CTLTYPE_INT | CTLFLAG_RW,
124     &ieee80211_mesh_confirmtimeout, 0, ieee80211_sysctl_msecs_ticks, "I",
125     "Confirm state timeout (msec)");
126 static int ieee80211_mesh_backofftimeout = -1;
127 SYSCTL_PROC(_net_wlan_mesh, OID_AUTO, backofftimeout, CTLTYPE_INT | CTLFLAG_RW,
128     &ieee80211_mesh_backofftimeout, 0, ieee80211_sysctl_msecs_ticks, "I",
129     "Backoff timeout (msec). This is to throutles peering forever when "
130     "not receiving answer or is rejected by a neighbor");
131 static int ieee80211_mesh_maxretries = 2;
132 SYSCTL_INT(_net_wlan_mesh, OID_AUTO, maxretries, CTLFLAG_RW,
133     &ieee80211_mesh_maxretries, 0,
134     "Maximum retries during peer link establishment");
135 static int ieee80211_mesh_maxholding = 2;
136 SYSCTL_INT(_net_wlan_mesh, OID_AUTO, maxholding, CTLFLAG_RW,
137     &ieee80211_mesh_maxholding, 0,
138     "Maximum times we are allowed to transition to HOLDING state before "
139     "backinoff during peer link establishment");
140 
141 static const uint8_t broadcastaddr[IEEE80211_ADDR_LEN] =
142 	{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
143 
144 static	ieee80211_recv_action_func mesh_recv_action_meshpeering_open;
145 static	ieee80211_recv_action_func mesh_recv_action_meshpeering_confirm;
146 static	ieee80211_recv_action_func mesh_recv_action_meshpeering_close;
147 static	ieee80211_recv_action_func mesh_recv_action_meshlmetric;
148 static	ieee80211_recv_action_func mesh_recv_action_meshgate;
149 
150 static	ieee80211_send_action_func mesh_send_action_meshpeering_open;
151 static	ieee80211_send_action_func mesh_send_action_meshpeering_confirm;
152 static	ieee80211_send_action_func mesh_send_action_meshpeering_close;
153 static	ieee80211_send_action_func mesh_send_action_meshlmetric;
154 static	ieee80211_send_action_func mesh_send_action_meshgate;
155 
156 static const struct ieee80211_mesh_proto_metric mesh_metric_airtime = {
157 	.mpm_descr	= "AIRTIME",
158 	.mpm_ie		= IEEE80211_MESHCONF_METRIC_AIRTIME,
159 	.mpm_metric	= mesh_airtime_calc,
160 };
161 
162 static struct ieee80211_mesh_proto_path		mesh_proto_paths[4];
163 static struct ieee80211_mesh_proto_metric	mesh_proto_metrics[4];
164 
165 MALLOC_DEFINE(M_80211_MESH_PREQ, "80211preq", "802.11 MESH Path Request frame");
166 MALLOC_DEFINE(M_80211_MESH_PREP, "80211prep", "802.11 MESH Path Reply frame");
167 MALLOC_DEFINE(M_80211_MESH_PERR, "80211perr", "802.11 MESH Path Error frame");
168 
169 /* The longer one of the lifetime should be stored as new lifetime */
170 #define MESH_ROUTE_LIFETIME_MAX(a, b)	(a > b ? a : b)
171 
172 MALLOC_DEFINE(M_80211_MESH_RT, "80211mesh_rt", "802.11s routing table");
173 MALLOC_DEFINE(M_80211_MESH_GT_RT, "80211mesh_gt", "802.11s known gates table");
174 
175 /*
176  * Helper functions to manipulate the Mesh routing table.
177  */
178 
179 static struct ieee80211_mesh_route *
180 mesh_rt_find_locked(struct ieee80211_mesh_state *ms,
181     const uint8_t dest[IEEE80211_ADDR_LEN])
182 {
183 	struct ieee80211_mesh_route *rt;
184 
185 	MESH_RT_LOCK_ASSERT(ms);
186 
187 	TAILQ_FOREACH(rt, &ms->ms_routes, rt_next) {
188 		if (IEEE80211_ADDR_EQ(dest, rt->rt_dest))
189 			return rt;
190 	}
191 	return NULL;
192 }
193 
194 static struct ieee80211_mesh_route *
195 mesh_rt_add_locked(struct ieee80211vap *vap,
196     const uint8_t dest[IEEE80211_ADDR_LEN])
197 {
198 	struct ieee80211_mesh_state *ms = vap->iv_mesh;
199 	struct ieee80211_mesh_route *rt;
200 
201 	KASSERT(!IEEE80211_ADDR_EQ(broadcastaddr, dest),
202 	    ("%s: adding broadcast to the routing table", __func__));
203 
204 	MESH_RT_LOCK_ASSERT(ms);
205 
206 	rt = IEEE80211_MALLOC(ALIGN(sizeof(struct ieee80211_mesh_route)) +
207 	    ms->ms_ppath->mpp_privlen, M_80211_MESH_RT,
208 	    IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
209 	if (rt != NULL) {
210 		rt->rt_vap = vap;
211 		IEEE80211_ADDR_COPY(rt->rt_dest, dest);
212 		rt->rt_priv = (void *)ALIGN(&rt[1]);
213 		MESH_RT_ENTRY_LOCK_INIT(rt, "MBSS_RT");
214 		callout_init(&rt->rt_discovery, 1);
215 		rt->rt_updtime = ticks;	/* create time */
216 		TAILQ_INSERT_TAIL(&ms->ms_routes, rt, rt_next);
217 	}
218 	return rt;
219 }
220 
221 struct ieee80211_mesh_route *
222 ieee80211_mesh_rt_find(struct ieee80211vap *vap,
223     const uint8_t dest[IEEE80211_ADDR_LEN])
224 {
225 	struct ieee80211_mesh_state *ms = vap->iv_mesh;
226 	struct ieee80211_mesh_route *rt;
227 
228 	MESH_RT_LOCK(ms);
229 	rt = mesh_rt_find_locked(ms, dest);
230 	MESH_RT_UNLOCK(ms);
231 	return rt;
232 }
233 
234 struct ieee80211_mesh_route *
235 ieee80211_mesh_rt_add(struct ieee80211vap *vap,
236     const uint8_t dest[IEEE80211_ADDR_LEN])
237 {
238 	struct ieee80211_mesh_state *ms = vap->iv_mesh;
239 	struct ieee80211_mesh_route *rt;
240 
241 	KASSERT(ieee80211_mesh_rt_find(vap, dest) == NULL,
242 	    ("%s: duplicate entry in the routing table", __func__));
243 	KASSERT(!IEEE80211_ADDR_EQ(vap->iv_myaddr, dest),
244 	    ("%s: adding self to the routing table", __func__));
245 
246 	MESH_RT_LOCK(ms);
247 	rt = mesh_rt_add_locked(vap, dest);
248 	MESH_RT_UNLOCK(ms);
249 	return rt;
250 }
251 
252 /*
253  * Update the route lifetime and returns the updated lifetime.
254  * If new_lifetime is zero and route is timedout it will be invalidated.
255  * new_lifetime is in msec
256  */
257 int
258 ieee80211_mesh_rt_update(struct ieee80211_mesh_route *rt, int new_lifetime)
259 {
260 	int timesince, now;
261 	uint32_t lifetime = 0;
262 
263 	KASSERT(rt != NULL, ("route is NULL"));
264 
265 	now = ticks;
266 	MESH_RT_ENTRY_LOCK(rt);
267 
268 	/* dont clobber a proxy entry gated by us */
269 	if (rt->rt_flags & IEEE80211_MESHRT_FLAGS_PROXY && rt->rt_nhops == 0) {
270 		MESH_RT_ENTRY_UNLOCK(rt);
271 		return rt->rt_lifetime;
272 	}
273 
274 	timesince = ticks_to_msecs(now - rt->rt_updtime);
275 	rt->rt_updtime = now;
276 	if (timesince >= rt->rt_lifetime) {
277 		if (new_lifetime != 0) {
278 			rt->rt_lifetime = new_lifetime;
279 		}
280 		else {
281 			rt->rt_flags &= ~IEEE80211_MESHRT_FLAGS_VALID;
282 			rt->rt_lifetime = 0;
283 		}
284 	} else {
285 		/* update what is left of lifetime */
286 		rt->rt_lifetime = rt->rt_lifetime - timesince;
287 		rt->rt_lifetime  = MESH_ROUTE_LIFETIME_MAX(
288 			new_lifetime, rt->rt_lifetime);
289 	}
290 	lifetime = rt->rt_lifetime;
291 	MESH_RT_ENTRY_UNLOCK(rt);
292 
293 	return lifetime;
294 }
295 
296 /*
297  * Add a proxy route (as needed) for the specified destination.
298  */
299 void
300 ieee80211_mesh_proxy_check(struct ieee80211vap *vap,
301     const uint8_t dest[IEEE80211_ADDR_LEN])
302 {
303 	struct ieee80211_mesh_state *ms = vap->iv_mesh;
304 	struct ieee80211_mesh_route *rt;
305 
306 	MESH_RT_LOCK(ms);
307 	rt = mesh_rt_find_locked(ms, dest);
308 	if (rt == NULL) {
309 		rt = mesh_rt_add_locked(vap, dest);
310 		if (rt == NULL) {
311 			IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH, dest,
312 			    "%s", "unable to add proxy entry");
313 			vap->iv_stats.is_mesh_rtaddfailed++;
314 		} else {
315 			IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH, dest,
316 			    "%s", "add proxy entry");
317 			IEEE80211_ADDR_COPY(rt->rt_mesh_gate, vap->iv_myaddr);
318 			IEEE80211_ADDR_COPY(rt->rt_nexthop, vap->iv_myaddr);
319 			rt->rt_flags |= IEEE80211_MESHRT_FLAGS_VALID
320 				     |  IEEE80211_MESHRT_FLAGS_PROXY;
321 		}
322 	} else if ((rt->rt_flags & IEEE80211_MESHRT_FLAGS_VALID) == 0) {
323 		KASSERT(rt->rt_flags & IEEE80211_MESHRT_FLAGS_PROXY,
324 		    ("no proxy flag for poxy entry"));
325 		struct ieee80211com *ic = vap->iv_ic;
326 		/*
327 		 * Fix existing entry created by received frames from
328 		 * stations that have some memory of dest.  We also
329 		 * flush any frames held on the staging queue; delivering
330 		 * them is too much trouble right now.
331 		 */
332 		IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH, dest,
333 		    "%s", "fix proxy entry");
334 		IEEE80211_ADDR_COPY(rt->rt_nexthop, vap->iv_myaddr);
335 		rt->rt_flags |= IEEE80211_MESHRT_FLAGS_VALID
336 			     |  IEEE80211_MESHRT_FLAGS_PROXY;
337 		/* XXX belongs in hwmp */
338 		ieee80211_ageq_drain_node(&ic->ic_stageq,
339 		   (void *)(uintptr_t) ieee80211_mac_hash(ic, dest));
340 		/* XXX stat? */
341 	}
342 	MESH_RT_UNLOCK(ms);
343 }
344 
345 static __inline void
346 mesh_rt_del(struct ieee80211_mesh_state *ms, struct ieee80211_mesh_route *rt)
347 {
348 	TAILQ_REMOVE(&ms->ms_routes, rt, rt_next);
349 	/*
350 	 * Grab the lock before destroying it, to be sure no one else
351 	 * is holding the route.
352 	 */
353 	MESH_RT_ENTRY_LOCK(rt);
354 	callout_drain(&rt->rt_discovery);
355 	MESH_RT_ENTRY_LOCK_DESTROY(rt);
356 	IEEE80211_FREE(rt, M_80211_MESH_RT);
357 }
358 
359 void
360 ieee80211_mesh_rt_del(struct ieee80211vap *vap,
361     const uint8_t dest[IEEE80211_ADDR_LEN])
362 {
363 	struct ieee80211_mesh_state *ms = vap->iv_mesh;
364 	struct ieee80211_mesh_route *rt, *next;
365 
366 	MESH_RT_LOCK(ms);
367 	TAILQ_FOREACH_SAFE(rt, &ms->ms_routes, rt_next, next) {
368 		if (IEEE80211_ADDR_EQ(rt->rt_dest, dest)) {
369 			if (rt->rt_flags & IEEE80211_MESHRT_FLAGS_PROXY) {
370 				ms->ms_ppath->mpp_senderror(vap, dest, rt,
371 				    IEEE80211_REASON_MESH_PERR_NO_PROXY);
372 			} else {
373 				ms->ms_ppath->mpp_senderror(vap, dest, rt,
374 				    IEEE80211_REASON_MESH_PERR_DEST_UNREACH);
375 			}
376 			mesh_rt_del(ms, rt);
377 			MESH_RT_UNLOCK(ms);
378 			return;
379 		}
380 	}
381 	MESH_RT_UNLOCK(ms);
382 }
383 
384 void
385 ieee80211_mesh_rt_flush(struct ieee80211vap *vap)
386 {
387 	struct ieee80211_mesh_state *ms = vap->iv_mesh;
388 	struct ieee80211_mesh_route *rt, *next;
389 
390 	if (ms == NULL)
391 		return;
392 	MESH_RT_LOCK(ms);
393 	TAILQ_FOREACH_SAFE(rt, &ms->ms_routes, rt_next, next)
394 		mesh_rt_del(ms, rt);
395 	MESH_RT_UNLOCK(ms);
396 }
397 
398 void
399 ieee80211_mesh_rt_flush_peer(struct ieee80211vap *vap,
400     const uint8_t peer[IEEE80211_ADDR_LEN])
401 {
402 	struct ieee80211_mesh_state *ms = vap->iv_mesh;
403 	struct ieee80211_mesh_route *rt, *next;
404 
405 	MESH_RT_LOCK(ms);
406 	TAILQ_FOREACH_SAFE(rt, &ms->ms_routes, rt_next, next) {
407 		if (IEEE80211_ADDR_EQ(rt->rt_nexthop, peer))
408 			mesh_rt_del(ms, rt);
409 	}
410 	MESH_RT_UNLOCK(ms);
411 }
412 
413 /*
414  * Flush expired routing entries, i.e. those in invalid state for
415  * some time.
416  */
417 static void
418 mesh_rt_flush_invalid(struct ieee80211vap *vap)
419 {
420 	struct ieee80211_mesh_state *ms = vap->iv_mesh;
421 	struct ieee80211_mesh_route *rt, *next;
422 
423 	if (ms == NULL)
424 		return;
425 	MESH_RT_LOCK(ms);
426 	TAILQ_FOREACH_SAFE(rt, &ms->ms_routes, rt_next, next) {
427 		/* Discover paths will be deleted by their own callout */
428 		if (rt->rt_flags & IEEE80211_MESHRT_FLAGS_DISCOVER)
429 			continue;
430 		ieee80211_mesh_rt_update(rt, 0);
431 		if ((rt->rt_flags & IEEE80211_MESHRT_FLAGS_VALID) == 0)
432 			mesh_rt_del(ms, rt);
433 	}
434 	MESH_RT_UNLOCK(ms);
435 }
436 
437 int
438 ieee80211_mesh_register_proto_path(const struct ieee80211_mesh_proto_path *mpp)
439 {
440 	int i, firstempty = -1;
441 
442 	for (i = 0; i < nitems(mesh_proto_paths); i++) {
443 		if (strncmp(mpp->mpp_descr, mesh_proto_paths[i].mpp_descr,
444 		    IEEE80211_MESH_PROTO_DSZ) == 0)
445 			return EEXIST;
446 		if (!mesh_proto_paths[i].mpp_active && firstempty == -1)
447 			firstempty = i;
448 	}
449 	if (firstempty < 0)
450 		return ENOSPC;
451 	memcpy(&mesh_proto_paths[firstempty], mpp, sizeof(*mpp));
452 	mesh_proto_paths[firstempty].mpp_active = 1;
453 	return 0;
454 }
455 
456 int
457 ieee80211_mesh_register_proto_metric(const struct
458     ieee80211_mesh_proto_metric *mpm)
459 {
460 	int i, firstempty = -1;
461 
462 	for (i = 0; i < nitems(mesh_proto_metrics); i++) {
463 		if (strncmp(mpm->mpm_descr, mesh_proto_metrics[i].mpm_descr,
464 		    IEEE80211_MESH_PROTO_DSZ) == 0)
465 			return EEXIST;
466 		if (!mesh_proto_metrics[i].mpm_active && firstempty == -1)
467 			firstempty = i;
468 	}
469 	if (firstempty < 0)
470 		return ENOSPC;
471 	memcpy(&mesh_proto_metrics[firstempty], mpm, sizeof(*mpm));
472 	mesh_proto_metrics[firstempty].mpm_active = 1;
473 	return 0;
474 }
475 
476 static int
477 mesh_select_proto_path(struct ieee80211vap *vap, const char *name)
478 {
479 	struct ieee80211_mesh_state *ms = vap->iv_mesh;
480 	int i;
481 
482 	for (i = 0; i < nitems(mesh_proto_paths); i++) {
483 		if (strcasecmp(mesh_proto_paths[i].mpp_descr, name) == 0) {
484 			ms->ms_ppath = &mesh_proto_paths[i];
485 			return 0;
486 		}
487 	}
488 	return ENOENT;
489 }
490 
491 static int
492 mesh_select_proto_metric(struct ieee80211vap *vap, const char *name)
493 {
494 	struct ieee80211_mesh_state *ms = vap->iv_mesh;
495 	int i;
496 
497 	for (i = 0; i < nitems(mesh_proto_metrics); i++) {
498 		if (strcasecmp(mesh_proto_metrics[i].mpm_descr, name) == 0) {
499 			ms->ms_pmetric = &mesh_proto_metrics[i];
500 			return 0;
501 		}
502 	}
503 	return ENOENT;
504 }
505 
506 static void
507 mesh_gatemode_setup(struct ieee80211vap *vap)
508 {
509 	struct ieee80211_mesh_state *ms = vap->iv_mesh;
510 
511 	/*
512 	 * NB: When a mesh gate is running as a ROOT it shall
513 	 * not send out periodic GANNs but instead mark the
514 	 * mesh gate flag for the corresponding proactive PREQ
515 	 * and RANN frames.
516 	 */
517 	if (ms->ms_flags & IEEE80211_MESHFLAGS_ROOT ||
518 	    (ms->ms_flags & IEEE80211_MESHFLAGS_GATE) == 0) {
519 		callout_drain(&ms->ms_gatetimer);
520 		return ;
521 	}
522 	callout_reset(&ms->ms_gatetimer, ieee80211_mesh_gateint,
523 	    mesh_gatemode_cb, vap);
524 }
525 
526 static void
527 mesh_gatemode_cb(void *arg)
528 {
529 	struct ieee80211vap *vap = (struct ieee80211vap *)arg;
530 	struct ieee80211_mesh_state *ms = vap->iv_mesh;
531 	struct ieee80211_meshgann_ie gann;
532 
533 	gann.gann_flags = 0; /* Reserved */
534 	gann.gann_hopcount = 0;
535 	gann.gann_ttl = ms->ms_ttl;
536 	IEEE80211_ADDR_COPY(gann.gann_addr, vap->iv_myaddr);
537 	gann.gann_seq = ms->ms_gateseq++;
538 	gann.gann_interval = ieee80211_mesh_gateint;
539 
540 	IEEE80211_NOTE(vap, IEEE80211_MSG_MESH, vap->iv_bss,
541 	    "send broadcast GANN (seq %u)", gann.gann_seq);
542 
543 	ieee80211_send_action(vap->iv_bss, IEEE80211_ACTION_CAT_MESH,
544 	    IEEE80211_ACTION_MESH_GANN, &gann);
545 	mesh_gatemode_setup(vap);
546 }
547 
548 static void
549 ieee80211_mesh_init(void)
550 {
551 
552 	memset(mesh_proto_paths, 0, sizeof(mesh_proto_paths));
553 	memset(mesh_proto_metrics, 0, sizeof(mesh_proto_metrics));
554 
555 	/*
556 	 * Setup mesh parameters that depends on the clock frequency.
557 	 */
558 	ieee80211_mesh_gateint = msecs_to_ticks(10000);
559 	ieee80211_mesh_retrytimeout = msecs_to_ticks(40);
560 	ieee80211_mesh_holdingtimeout = msecs_to_ticks(40);
561 	ieee80211_mesh_confirmtimeout = msecs_to_ticks(40);
562 	ieee80211_mesh_backofftimeout = msecs_to_ticks(5000);
563 
564 	/*
565 	 * Register action frame handlers.
566 	 */
567 	ieee80211_recv_action_register(IEEE80211_ACTION_CAT_SELF_PROT,
568 	    IEEE80211_ACTION_MESHPEERING_OPEN,
569 	    mesh_recv_action_meshpeering_open);
570 	ieee80211_recv_action_register(IEEE80211_ACTION_CAT_SELF_PROT,
571 	    IEEE80211_ACTION_MESHPEERING_CONFIRM,
572 	    mesh_recv_action_meshpeering_confirm);
573 	ieee80211_recv_action_register(IEEE80211_ACTION_CAT_SELF_PROT,
574 	    IEEE80211_ACTION_MESHPEERING_CLOSE,
575 	    mesh_recv_action_meshpeering_close);
576 	ieee80211_recv_action_register(IEEE80211_ACTION_CAT_MESH,
577 	    IEEE80211_ACTION_MESH_LMETRIC, mesh_recv_action_meshlmetric);
578 	ieee80211_recv_action_register(IEEE80211_ACTION_CAT_MESH,
579 	    IEEE80211_ACTION_MESH_GANN, mesh_recv_action_meshgate);
580 
581 	ieee80211_send_action_register(IEEE80211_ACTION_CAT_SELF_PROT,
582 	    IEEE80211_ACTION_MESHPEERING_OPEN,
583 	    mesh_send_action_meshpeering_open);
584 	ieee80211_send_action_register(IEEE80211_ACTION_CAT_SELF_PROT,
585 	    IEEE80211_ACTION_MESHPEERING_CONFIRM,
586 	    mesh_send_action_meshpeering_confirm);
587 	ieee80211_send_action_register(IEEE80211_ACTION_CAT_SELF_PROT,
588 	    IEEE80211_ACTION_MESHPEERING_CLOSE,
589 	    mesh_send_action_meshpeering_close);
590 	ieee80211_send_action_register(IEEE80211_ACTION_CAT_MESH,
591 	    IEEE80211_ACTION_MESH_LMETRIC,
592 	    mesh_send_action_meshlmetric);
593 	ieee80211_send_action_register(IEEE80211_ACTION_CAT_MESH,
594 	    IEEE80211_ACTION_MESH_GANN,
595 	    mesh_send_action_meshgate);
596 
597 	/*
598 	 * Register Airtime Link Metric.
599 	 */
600 	ieee80211_mesh_register_proto_metric(&mesh_metric_airtime);
601 
602 }
603 SYSINIT(wlan_mesh, SI_SUB_DRIVERS, SI_ORDER_FIRST, ieee80211_mesh_init, NULL);
604 
605 void
606 ieee80211_mesh_attach(struct ieee80211com *ic)
607 {
608 	ic->ic_vattach[IEEE80211_M_MBSS] = mesh_vattach;
609 }
610 
611 void
612 ieee80211_mesh_detach(struct ieee80211com *ic)
613 {
614 }
615 
616 static void
617 mesh_vdetach_peers(void *arg, struct ieee80211_node *ni)
618 {
619 	struct ieee80211com *ic = ni->ni_ic;
620 	uint16_t args[3];
621 
622 	if (ni->ni_mlstate == IEEE80211_NODE_MESH_ESTABLISHED) {
623 		args[0] = ni->ni_mlpid;
624 		args[1] = ni->ni_mllid;
625 		args[2] = IEEE80211_REASON_PEER_LINK_CANCELED;
626 		ieee80211_send_action(ni,
627 		    IEEE80211_ACTION_CAT_SELF_PROT,
628 		    IEEE80211_ACTION_MESHPEERING_CLOSE,
629 		    args);
630 	}
631 	callout_drain(&ni->ni_mltimer);
632 	/* XXX belongs in hwmp */
633 	ieee80211_ageq_drain_node(&ic->ic_stageq,
634 	   (void *)(uintptr_t) ieee80211_mac_hash(ic, ni->ni_macaddr));
635 }
636 
637 static void
638 mesh_vdetach(struct ieee80211vap *vap)
639 {
640 	struct ieee80211_mesh_state *ms = vap->iv_mesh;
641 
642 	callout_drain(&ms->ms_cleantimer);
643 	ieee80211_iterate_nodes(&vap->iv_ic->ic_sta, mesh_vdetach_peers,
644 	    NULL);
645 	ieee80211_mesh_rt_flush(vap);
646 	MESH_RT_LOCK_DESTROY(ms);
647 	ms->ms_ppath->mpp_vdetach(vap);
648 	IEEE80211_FREE(vap->iv_mesh, M_80211_VAP);
649 	vap->iv_mesh = NULL;
650 }
651 
652 static void
653 mesh_vattach(struct ieee80211vap *vap)
654 {
655 	struct ieee80211_mesh_state *ms;
656 	vap->iv_newstate = mesh_newstate;
657 	vap->iv_input = mesh_input;
658 	vap->iv_opdetach = mesh_vdetach;
659 	vap->iv_recv_mgmt = mesh_recv_mgmt;
660 	vap->iv_recv_ctl = mesh_recv_ctl;
661 	ms = IEEE80211_MALLOC(sizeof(struct ieee80211_mesh_state), M_80211_VAP,
662 	    IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
663 	if (ms == NULL) {
664 		printf("%s: couldn't alloc MBSS state\n", __func__);
665 		return;
666 	}
667 	vap->iv_mesh = ms;
668 	ms->ms_seq = 0;
669 	ms->ms_flags = (IEEE80211_MESHFLAGS_AP | IEEE80211_MESHFLAGS_FWD);
670 	ms->ms_ttl = IEEE80211_MESH_DEFAULT_TTL;
671 	TAILQ_INIT(&ms->ms_known_gates);
672 	TAILQ_INIT(&ms->ms_routes);
673 	MESH_RT_LOCK_INIT(ms, "MBSS");
674 	callout_init(&ms->ms_cleantimer, 1);
675 	callout_init(&ms->ms_gatetimer, 1);
676 	ms->ms_gateseq = 0;
677 	mesh_select_proto_metric(vap, "AIRTIME");
678 	KASSERT(ms->ms_pmetric, ("ms_pmetric == NULL"));
679 	mesh_select_proto_path(vap, "HWMP");
680 	KASSERT(ms->ms_ppath, ("ms_ppath == NULL"));
681 	ms->ms_ppath->mpp_vattach(vap);
682 }
683 
684 /*
685  * IEEE80211_M_MBSS vap state machine handler.
686  */
687 static int
688 mesh_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
689 {
690 	struct ieee80211_mesh_state *ms = vap->iv_mesh;
691 	struct ieee80211com *ic = vap->iv_ic;
692 	struct ieee80211_node *ni;
693 	enum ieee80211_state ostate;
694 
695 	IEEE80211_LOCK_ASSERT(ic);
696 
697 	ostate = vap->iv_state;
698 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE, "%s: %s -> %s (%d)\n",
699 	    __func__, ieee80211_state_name[ostate],
700 	    ieee80211_state_name[nstate], arg);
701 	vap->iv_state = nstate;		/* state transition */
702 	if (ostate != IEEE80211_S_SCAN)
703 		ieee80211_cancel_scan(vap);	/* background scan */
704 	ni = vap->iv_bss;			/* NB: no reference held */
705 	if (nstate != IEEE80211_S_RUN && ostate == IEEE80211_S_RUN) {
706 		callout_drain(&ms->ms_cleantimer);
707 		callout_drain(&ms->ms_gatetimer);
708 	}
709 	switch (nstate) {
710 	case IEEE80211_S_INIT:
711 		switch (ostate) {
712 		case IEEE80211_S_SCAN:
713 			ieee80211_cancel_scan(vap);
714 			break;
715 		case IEEE80211_S_CAC:
716 			ieee80211_dfs_cac_stop(vap);
717 			break;
718 		case IEEE80211_S_RUN:
719 			ieee80211_iterate_nodes(&ic->ic_sta,
720 			    mesh_vdetach_peers, NULL);
721 			break;
722 		default:
723 			break;
724 		}
725 		if (ostate != IEEE80211_S_INIT) {
726 			/* NB: optimize INIT -> INIT case */
727 			ieee80211_reset_bss(vap);
728 			ieee80211_mesh_rt_flush(vap);
729 		}
730 		break;
731 	case IEEE80211_S_SCAN:
732 		switch (ostate) {
733 		case IEEE80211_S_INIT:
734 			if (vap->iv_des_chan != IEEE80211_CHAN_ANYC &&
735 			    !IEEE80211_IS_CHAN_RADAR(vap->iv_des_chan) &&
736 			    ms->ms_idlen != 0) {
737 				/*
738 				 * Already have a channel and a mesh ID; bypass
739 				 * the scan and startup immediately.
740 				 */
741 				ieee80211_create_ibss(vap, vap->iv_des_chan);
742 				break;
743 			}
744 			/*
745 			 * Initiate a scan.  We can come here as a result
746 			 * of an IEEE80211_IOC_SCAN_REQ too in which case
747 			 * the vap will be marked with IEEE80211_FEXT_SCANREQ
748 			 * and the scan request parameters will be present
749 			 * in iv_scanreq.  Otherwise we do the default.
750 			*/
751 			if (vap->iv_flags_ext & IEEE80211_FEXT_SCANREQ) {
752 				ieee80211_check_scan(vap,
753 				    vap->iv_scanreq_flags,
754 				    vap->iv_scanreq_duration,
755 				    vap->iv_scanreq_mindwell,
756 				    vap->iv_scanreq_maxdwell,
757 				    vap->iv_scanreq_nssid, vap->iv_scanreq_ssid);
758 				vap->iv_flags_ext &= ~IEEE80211_FEXT_SCANREQ;
759 			} else
760 				ieee80211_check_scan_current(vap);
761 			break;
762 		default:
763 			break;
764 		}
765 		break;
766 	case IEEE80211_S_CAC:
767 		/*
768 		 * Start CAC on a DFS channel.  We come here when starting
769 		 * a bss on a DFS channel (see ieee80211_create_ibss).
770 		 */
771 		ieee80211_dfs_cac_start(vap);
772 		break;
773 	case IEEE80211_S_RUN:
774 		switch (ostate) {
775 		case IEEE80211_S_INIT:
776 			/*
777 			 * Already have a channel; bypass the
778 			 * scan and startup immediately.
779 			 * Note that ieee80211_create_ibss will call
780 			 * back to do a RUN->RUN state change.
781 			 */
782 			ieee80211_create_ibss(vap,
783 			    ieee80211_ht_adjust_channel(ic,
784 				ic->ic_curchan, vap->iv_flags_ht));
785 			/* NB: iv_bss is changed on return */
786 			break;
787 		case IEEE80211_S_CAC:
788 			/*
789 			 * NB: This is the normal state change when CAC
790 			 * expires and no radar was detected; no need to
791 			 * clear the CAC timer as it's already expired.
792 			 */
793 			/* fall thru... */
794 		case IEEE80211_S_CSA:
795 #if 0
796 			/*
797 			 * Shorten inactivity timer of associated stations
798 			 * to weed out sta's that don't follow a CSA.
799 			 */
800 			ieee80211_iterate_nodes(&ic->ic_sta, sta_csa, vap);
801 #endif
802 			/*
803 			 * Update bss node channel to reflect where
804 			 * we landed after CSA.
805 			 */
806 			ieee80211_node_set_chan(vap->iv_bss,
807 			    ieee80211_ht_adjust_channel(ic, ic->ic_curchan,
808 				ieee80211_htchanflags(vap->iv_bss->ni_chan)));
809 			/* XXX bypass debug msgs */
810 			break;
811 		case IEEE80211_S_SCAN:
812 		case IEEE80211_S_RUN:
813 #ifdef IEEE80211_DEBUG
814 			if (ieee80211_msg_debug(vap)) {
815 				struct ieee80211_node *ni = vap->iv_bss;
816 				ieee80211_note(vap,
817 				    "synchronized with %s meshid ",
818 				    ether_sprintf(ni->ni_meshid));
819 				ieee80211_print_essid(ni->ni_meshid,
820 				    ni->ni_meshidlen);
821 				/* XXX MCS/HT */
822 				printf(" channel %d\n",
823 				    ieee80211_chan2ieee(ic, ic->ic_curchan));
824 			}
825 #endif
826 			break;
827 		default:
828 			break;
829 		}
830 		ieee80211_node_authorize(vap->iv_bss);
831 		callout_reset(&ms->ms_cleantimer, ms->ms_ppath->mpp_inact,
832                     mesh_rt_cleanup_cb, vap);
833 		mesh_gatemode_setup(vap);
834 		break;
835 	default:
836 		break;
837 	}
838 	/* NB: ostate not nstate */
839 	ms->ms_ppath->mpp_newstate(vap, ostate, arg);
840 	return 0;
841 }
842 
843 static void
844 mesh_rt_cleanup_cb(void *arg)
845 {
846 	struct ieee80211vap *vap = arg;
847 	struct ieee80211_mesh_state *ms = vap->iv_mesh;
848 
849 	mesh_rt_flush_invalid(vap);
850 	callout_reset(&ms->ms_cleantimer, ms->ms_ppath->mpp_inact,
851 	    mesh_rt_cleanup_cb, vap);
852 }
853 
854 /*
855  * Mark a mesh STA as gate and return a pointer to it.
856  * If this is first time, we create a new gate route.
857  * Always update the path route to this mesh gate.
858  */
859 struct ieee80211_mesh_gate_route *
860 ieee80211_mesh_mark_gate(struct ieee80211vap *vap, const uint8_t *addr,
861     struct ieee80211_mesh_route *rt)
862 {
863 	struct ieee80211_mesh_state *ms = vap->iv_mesh;
864 	struct ieee80211_mesh_gate_route *gr = NULL, *next;
865 	int found = 0;
866 
867 	MESH_RT_LOCK(ms);
868 	TAILQ_FOREACH_SAFE(gr, &ms->ms_known_gates, gr_next, next) {
869 		if (IEEE80211_ADDR_EQ(gr->gr_addr, addr)) {
870 			found = 1;
871 			break;
872 		}
873 	}
874 
875 	if (!found) {
876 		/* New mesh gate add it to known table. */
877 		IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH, addr,
878 		    "%s", "stored new gate information from pro-PREQ.");
879 		gr = IEEE80211_MALLOC(ALIGN(sizeof(struct ieee80211_mesh_gate_route)),
880 		    M_80211_MESH_GT_RT,
881 		    IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
882 		IEEE80211_ADDR_COPY(gr->gr_addr, addr);
883 		TAILQ_INSERT_TAIL(&ms->ms_known_gates, gr, gr_next);
884 	}
885 	gr->gr_route = rt;
886 	/* TODO: link from path route to gate route */
887 	MESH_RT_UNLOCK(ms);
888 
889 	return gr;
890 }
891 
892 
893 /*
894  * Helper function to note the Mesh Peer Link FSM change.
895  */
896 static void
897 mesh_linkchange(struct ieee80211_node *ni, enum ieee80211_mesh_mlstate state)
898 {
899 	struct ieee80211vap *vap = ni->ni_vap;
900 	struct ieee80211_mesh_state *ms = vap->iv_mesh;
901 #ifdef IEEE80211_DEBUG
902 	static const char *meshlinkstates[] = {
903 		[IEEE80211_NODE_MESH_IDLE]		= "IDLE",
904 		[IEEE80211_NODE_MESH_OPENSNT]		= "OPEN SENT",
905 		[IEEE80211_NODE_MESH_OPENRCV]		= "OPEN RECEIVED",
906 		[IEEE80211_NODE_MESH_CONFIRMRCV]	= "CONFIRM RECEIVED",
907 		[IEEE80211_NODE_MESH_ESTABLISHED]	= "ESTABLISHED",
908 		[IEEE80211_NODE_MESH_HOLDING]		= "HOLDING"
909 	};
910 #endif
911 	IEEE80211_NOTE(vap, IEEE80211_MSG_MESH,
912 	    ni, "peer link: %s -> %s",
913 	    meshlinkstates[ni->ni_mlstate], meshlinkstates[state]);
914 
915 	/* track neighbor count */
916 	if (state == IEEE80211_NODE_MESH_ESTABLISHED &&
917 	    ni->ni_mlstate != IEEE80211_NODE_MESH_ESTABLISHED) {
918 		KASSERT(ms->ms_neighbors < 65535, ("neighbor count overflow"));
919 		ms->ms_neighbors++;
920 		ieee80211_beacon_notify(vap, IEEE80211_BEACON_MESHCONF);
921 	} else if (ni->ni_mlstate == IEEE80211_NODE_MESH_ESTABLISHED &&
922 	    state != IEEE80211_NODE_MESH_ESTABLISHED) {
923 		KASSERT(ms->ms_neighbors > 0, ("neighbor count 0"));
924 		ms->ms_neighbors--;
925 		ieee80211_beacon_notify(vap, IEEE80211_BEACON_MESHCONF);
926 	}
927 	ni->ni_mlstate = state;
928 	switch (state) {
929 	case IEEE80211_NODE_MESH_HOLDING:
930 		ms->ms_ppath->mpp_peerdown(ni);
931 		break;
932 	case IEEE80211_NODE_MESH_ESTABLISHED:
933 		ieee80211_mesh_discover(vap, ni->ni_macaddr, NULL);
934 		break;
935 	default:
936 		break;
937 	}
938 }
939 
940 /*
941  * Helper function to generate a unique local ID required for mesh
942  * peer establishment.
943  */
944 static void
945 mesh_checkid(void *arg, struct ieee80211_node *ni)
946 {
947 	uint16_t *r = arg;
948 
949 	if (*r == ni->ni_mllid)
950 		*(uint16_t *)arg = 0;
951 }
952 
953 static uint32_t
954 mesh_generateid(struct ieee80211vap *vap)
955 {
956 	int maxiter = 4;
957 	uint16_t r;
958 
959 	do {
960 		get_random_bytes(&r, 2);
961 		ieee80211_iterate_nodes(&vap->iv_ic->ic_sta, mesh_checkid, &r);
962 		maxiter--;
963 	} while (r == 0 && maxiter > 0);
964 	return r;
965 }
966 
967 /*
968  * Verifies if we already received this packet by checking its
969  * sequence number.
970  * Returns 0 if the frame is to be accepted, 1 otherwise.
971  */
972 static int
973 mesh_checkpseq(struct ieee80211vap *vap,
974     const uint8_t source[IEEE80211_ADDR_LEN], uint32_t seq)
975 {
976 	struct ieee80211_mesh_route *rt;
977 
978 	rt = ieee80211_mesh_rt_find(vap, source);
979 	if (rt == NULL) {
980 		rt = ieee80211_mesh_rt_add(vap, source);
981 		if (rt == NULL) {
982 			IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH, source,
983 			    "%s", "add mcast route failed");
984 			vap->iv_stats.is_mesh_rtaddfailed++;
985 			return 1;
986 		}
987 		IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH, source,
988 		    "add mcast route, mesh seqno %d", seq);
989 		rt->rt_lastmseq = seq;
990 		return 0;
991 	}
992 	if (IEEE80211_MESH_SEQ_GEQ(rt->rt_lastmseq, seq)) {
993 		return 1;
994 	} else {
995 		rt->rt_lastmseq = seq;
996 		return 0;
997 	}
998 }
999 
1000 /*
1001  * Iterate the routing table and locate the next hop.
1002  */
1003 struct ieee80211_node *
1004 ieee80211_mesh_find_txnode(struct ieee80211vap *vap,
1005     const uint8_t dest[IEEE80211_ADDR_LEN])
1006 {
1007 	struct ieee80211_mesh_route *rt;
1008 
1009 	rt = ieee80211_mesh_rt_find(vap, dest);
1010 	if (rt == NULL)
1011 		return NULL;
1012 	if ((rt->rt_flags & IEEE80211_MESHRT_FLAGS_VALID) == 0) {
1013 		IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH, dest,
1014 		    "%s: !valid, flags 0x%x", __func__, rt->rt_flags);
1015 		/* XXX stat */
1016 		return NULL;
1017 	}
1018 	if (rt->rt_flags & IEEE80211_MESHRT_FLAGS_PROXY) {
1019 		rt = ieee80211_mesh_rt_find(vap, rt->rt_mesh_gate);
1020 		if (rt == NULL) return NULL;
1021 		if ((rt->rt_flags & IEEE80211_MESHRT_FLAGS_VALID) == 0) {
1022 			IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH, dest,
1023 			    "%s: meshgate !valid, flags 0x%x", __func__,
1024 			    rt->rt_flags);
1025 			/* XXX stat */
1026 			return NULL;
1027 		}
1028 	}
1029 	return ieee80211_find_txnode(vap, rt->rt_nexthop);
1030 }
1031 
1032 static void
1033 mesh_transmit_to_gate(struct ieee80211vap *vap, struct mbuf *m,
1034     struct ieee80211_mesh_route *rt_gate)
1035 {
1036 	struct ifnet *ifp = vap->iv_ifp;
1037 	struct ieee80211_node *ni;
1038 
1039 	IEEE80211_TX_UNLOCK_ASSERT(vap->iv_ic);
1040 
1041 	ni = ieee80211_mesh_find_txnode(vap, rt_gate->rt_dest);
1042 	if (ni == NULL) {
1043 		if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1044 		m_freem(m);
1045 		return;
1046 	}
1047 
1048 	/*
1049 	 * Send through the VAP packet transmit path.
1050 	 * This consumes the node ref grabbed above and
1051 	 * the mbuf, regardless of whether there's a problem
1052 	 * or not.
1053 	 */
1054 	(void) ieee80211_vap_pkt_send_dest(vap, m, ni);
1055 }
1056 
1057 /*
1058  * Forward the queued frames to known valid mesh gates.
1059  * Assume destination to be outside the MBSS (i.e. proxy entry),
1060  * If no valid mesh gates are known silently discard queued frames.
1061  * After transmitting frames to all known valid mesh gates, this route
1062  * will be marked invalid, and a new path discovery will happen in the hopes
1063  * that (at least) one of the mesh gates have a new proxy entry for us to use.
1064  */
1065 void
1066 ieee80211_mesh_forward_to_gates(struct ieee80211vap *vap,
1067     struct ieee80211_mesh_route *rt_dest)
1068 {
1069 	struct ieee80211com *ic = vap->iv_ic;
1070 	struct ieee80211_mesh_state *ms = vap->iv_mesh;
1071 	struct ieee80211_mesh_route *rt_gate;
1072 	struct ieee80211_mesh_gate_route *gr = NULL, *gr_next;
1073 	struct mbuf *m, *mcopy, *next;
1074 
1075 	IEEE80211_TX_UNLOCK_ASSERT(ic);
1076 
1077 	KASSERT( rt_dest->rt_flags == IEEE80211_MESHRT_FLAGS_DISCOVER,
1078 	    ("Route is not marked with IEEE80211_MESHRT_FLAGS_DISCOVER"));
1079 
1080 	/* XXX: send to more than one valid mash gate */
1081 	MESH_RT_LOCK(ms);
1082 
1083 	m = ieee80211_ageq_remove(&ic->ic_stageq,
1084 	    (struct ieee80211_node *)(uintptr_t)
1085 	    ieee80211_mac_hash(ic, rt_dest->rt_dest));
1086 
1087 	TAILQ_FOREACH_SAFE(gr, &ms->ms_known_gates, gr_next, gr_next) {
1088 		rt_gate = gr->gr_route;
1089 		if (rt_gate == NULL) {
1090 			IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_HWMP,
1091 				rt_dest->rt_dest,
1092 				"mesh gate with no path %6D",
1093 				gr->gr_addr, ":");
1094 			continue;
1095 		}
1096 		if ((rt_gate->rt_flags & IEEE80211_MESHRT_FLAGS_VALID) == 0)
1097 			continue;
1098 		KASSERT(rt_gate->rt_flags & IEEE80211_MESHRT_FLAGS_GATE,
1099 		    ("route not marked as a mesh gate"));
1100 		KASSERT((rt_gate->rt_flags &
1101 			IEEE80211_MESHRT_FLAGS_PROXY) == 0,
1102 			("found mesh gate that is also marked porxy"));
1103 		/*
1104 		 * convert route to a proxy route gated by the current
1105 		 * mesh gate, this is needed so encap can built data
1106 		 * frame with correct address.
1107 		 */
1108 		rt_dest->rt_flags = IEEE80211_MESHRT_FLAGS_PROXY |
1109 			IEEE80211_MESHRT_FLAGS_VALID;
1110 		rt_dest->rt_ext_seq = 1; /* random value */
1111 		IEEE80211_ADDR_COPY(rt_dest->rt_mesh_gate, rt_gate->rt_dest);
1112 		IEEE80211_ADDR_COPY(rt_dest->rt_nexthop, rt_gate->rt_nexthop);
1113 		rt_dest->rt_metric = rt_gate->rt_metric;
1114 		rt_dest->rt_nhops = rt_gate->rt_nhops;
1115 		ieee80211_mesh_rt_update(rt_dest, ms->ms_ppath->mpp_inact);
1116 		MESH_RT_UNLOCK(ms);
1117 		/* XXX: lock?? */
1118 		mcopy = m_dup(m, M_NOWAIT);
1119 		for (; mcopy != NULL; mcopy = next) {
1120 			next = mcopy->m_nextpkt;
1121 			mcopy->m_nextpkt = NULL;
1122 			IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_HWMP,
1123 			    rt_dest->rt_dest,
1124 			    "flush queued frame %p len %d", mcopy,
1125 			    mcopy->m_pkthdr.len);
1126 			mesh_transmit_to_gate(vap, mcopy, rt_gate);
1127 		}
1128 		MESH_RT_LOCK(ms);
1129 	}
1130 	rt_dest->rt_flags = 0; /* Mark invalid */
1131 	m_freem(m);
1132 	MESH_RT_UNLOCK(ms);
1133 }
1134 
1135 /*
1136  * Forward the specified frame.
1137  * Decrement the TTL and set TA to our MAC address.
1138  */
1139 static void
1140 mesh_forward(struct ieee80211vap *vap, struct mbuf *m,
1141     const struct ieee80211_meshcntl *mc)
1142 {
1143 	struct ieee80211com *ic = vap->iv_ic;
1144 	struct ieee80211_mesh_state *ms = vap->iv_mesh;
1145 	struct ifnet *ifp = vap->iv_ifp;
1146 	const struct ieee80211_frame *wh =
1147 	    mtod(m, const struct ieee80211_frame *);
1148 	struct mbuf *mcopy;
1149 	struct ieee80211_meshcntl *mccopy;
1150 	struct ieee80211_frame *whcopy;
1151 	struct ieee80211_node *ni;
1152 	int err;
1153 
1154 	/* This is called from the RX path - don't hold this lock */
1155 	IEEE80211_TX_UNLOCK_ASSERT(ic);
1156 
1157 	/*
1158 	 * mesh ttl of 1 means we are the last one receving it,
1159 	 * according to amendment we decrement and then check if
1160 	 * 0, if so we dont forward.
1161 	 */
1162 	if (mc->mc_ttl < 1) {
1163 		IEEE80211_NOTE_FRAME(vap, IEEE80211_MSG_MESH, wh,
1164 		    "%s", "frame not fwd'd, ttl 1");
1165 		vap->iv_stats.is_mesh_fwd_ttl++;
1166 		return;
1167 	}
1168 	if (!(ms->ms_flags & IEEE80211_MESHFLAGS_FWD)) {
1169 		IEEE80211_NOTE_FRAME(vap, IEEE80211_MSG_MESH, wh,
1170 		    "%s", "frame not fwd'd, fwding disabled");
1171 		vap->iv_stats.is_mesh_fwd_disabled++;
1172 		return;
1173 	}
1174 	mcopy = m_dup(m, M_NOWAIT);
1175 	if (mcopy == NULL) {
1176 		IEEE80211_NOTE_FRAME(vap, IEEE80211_MSG_MESH, wh,
1177 		    "%s", "frame not fwd'd, cannot dup");
1178 		vap->iv_stats.is_mesh_fwd_nobuf++;
1179 		if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1180 		return;
1181 	}
1182 	mcopy = m_pullup(mcopy, ieee80211_hdrspace(ic, wh) +
1183 	    sizeof(struct ieee80211_meshcntl));
1184 	if (mcopy == NULL) {
1185 		IEEE80211_NOTE_FRAME(vap, IEEE80211_MSG_MESH, wh,
1186 		    "%s", "frame not fwd'd, too short");
1187 		vap->iv_stats.is_mesh_fwd_tooshort++;
1188 		if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1189 		m_freem(mcopy);
1190 		return;
1191 	}
1192 	whcopy = mtod(mcopy, struct ieee80211_frame *);
1193 	mccopy = (struct ieee80211_meshcntl *)
1194 	    (mtod(mcopy, uint8_t *) + ieee80211_hdrspace(ic, wh));
1195 	/* XXX clear other bits? */
1196 	whcopy->i_fc[1] &= ~IEEE80211_FC1_RETRY;
1197 	IEEE80211_ADDR_COPY(whcopy->i_addr2, vap->iv_myaddr);
1198 	if (IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1199 		ni = ieee80211_ref_node(vap->iv_bss);
1200 		mcopy->m_flags |= M_MCAST;
1201 	} else {
1202 		ni = ieee80211_mesh_find_txnode(vap, whcopy->i_addr3);
1203 		if (ni == NULL) {
1204 			/*
1205 			 * [Optional] any of the following three actions:
1206 			 * o silently discard
1207 			 * o trigger a path discovery
1208 			 * o inform TA that meshDA is unknown.
1209 			 */
1210 			IEEE80211_NOTE_FRAME(vap, IEEE80211_MSG_MESH, wh,
1211 			    "%s", "frame not fwd'd, no path");
1212 			ms->ms_ppath->mpp_senderror(vap, whcopy->i_addr3, NULL,
1213 			    IEEE80211_REASON_MESH_PERR_NO_FI);
1214 			vap->iv_stats.is_mesh_fwd_nopath++;
1215 			m_freem(mcopy);
1216 			return;
1217 		}
1218 		IEEE80211_ADDR_COPY(whcopy->i_addr1, ni->ni_macaddr);
1219 	}
1220 	KASSERT(mccopy->mc_ttl > 0, ("%s called with wrong ttl", __func__));
1221 	mccopy->mc_ttl--;
1222 
1223 	/* XXX calculate priority so drivers can find the tx queue */
1224 	M_WME_SETAC(mcopy, WME_AC_BE);
1225 
1226 	/* XXX do we know m_nextpkt is NULL? */
1227 	mcopy->m_pkthdr.rcvif = (void *) ni;
1228 
1229 	/*
1230 	 * XXX this bypasses all of the VAP TX handling; it passes frames
1231 	 * directly to the parent interface.
1232 	 *
1233 	 * Because of this, there's no TX lock being held as there's no
1234 	 * encaps state being used.
1235 	 *
1236 	 * Doing a direct parent transmit may not be the correct thing
1237 	 * to do here; we'll have to re-think this soon.
1238 	 */
1239 	IEEE80211_TX_LOCK(ic);
1240 	err = ieee80211_parent_xmitpkt(ic, mcopy);
1241 	IEEE80211_TX_UNLOCK(ic);
1242 	if (!err)
1243 		if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
1244 }
1245 
1246 static struct mbuf *
1247 mesh_decap(struct ieee80211vap *vap, struct mbuf *m, int hdrlen, int meshdrlen)
1248 {
1249 #define	WHDIR(wh)	((wh)->i_fc[1] & IEEE80211_FC1_DIR_MASK)
1250 #define	MC01(mc)	((const struct ieee80211_meshcntl_ae01 *)mc)
1251 	uint8_t b[sizeof(struct ieee80211_qosframe_addr4) +
1252 		  sizeof(struct ieee80211_meshcntl_ae10)];
1253 	const struct ieee80211_qosframe_addr4 *wh;
1254 	const struct ieee80211_meshcntl_ae10 *mc;
1255 	struct ether_header *eh;
1256 	struct llc *llc;
1257 	int ae;
1258 
1259 	if (m->m_len < hdrlen + sizeof(*llc) &&
1260 	    (m = m_pullup(m, hdrlen + sizeof(*llc))) == NULL) {
1261 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_ANY,
1262 		    "discard data frame: %s", "m_pullup failed");
1263 		vap->iv_stats.is_rx_tooshort++;
1264 		return NULL;
1265 	}
1266 	memcpy(b, mtod(m, caddr_t), hdrlen);
1267 	wh = (const struct ieee80211_qosframe_addr4 *)&b[0];
1268 	mc = (const struct ieee80211_meshcntl_ae10 *)&b[hdrlen - meshdrlen];
1269 	KASSERT(WHDIR(wh) == IEEE80211_FC1_DIR_FROMDS ||
1270 		WHDIR(wh) == IEEE80211_FC1_DIR_DSTODS,
1271 	    ("bogus dir, fc 0x%x:0x%x", wh->i_fc[0], wh->i_fc[1]));
1272 
1273 	llc = (struct llc *)(mtod(m, caddr_t) + hdrlen);
1274 	if (llc->llc_dsap == LLC_SNAP_LSAP && llc->llc_ssap == LLC_SNAP_LSAP &&
1275 	    llc->llc_control == LLC_UI && llc->llc_snap.org_code[0] == 0 &&
1276 	    llc->llc_snap.org_code[1] == 0 && llc->llc_snap.org_code[2] == 0 &&
1277 	    /* NB: preserve AppleTalk frames that have a native SNAP hdr */
1278 	    !(llc->llc_snap.ether_type == htons(ETHERTYPE_AARP) ||
1279 	      llc->llc_snap.ether_type == htons(ETHERTYPE_IPX))) {
1280 		m_adj(m, hdrlen + sizeof(struct llc) - sizeof(*eh));
1281 		llc = NULL;
1282 	} else {
1283 		m_adj(m, hdrlen - sizeof(*eh));
1284 	}
1285 	eh = mtod(m, struct ether_header *);
1286 	ae = mc->mc_flags & IEEE80211_MESH_AE_MASK;
1287 	if (WHDIR(wh) == IEEE80211_FC1_DIR_FROMDS) {
1288 		IEEE80211_ADDR_COPY(eh->ether_dhost, wh->i_addr1);
1289 		if (ae == IEEE80211_MESH_AE_00) {
1290 			IEEE80211_ADDR_COPY(eh->ether_shost, wh->i_addr3);
1291 		} else if (ae == IEEE80211_MESH_AE_01) {
1292 			IEEE80211_ADDR_COPY(eh->ether_shost,
1293 			    MC01(mc)->mc_addr4);
1294 		} else {
1295 			IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
1296 			    (const struct ieee80211_frame *)wh, NULL,
1297 			    "bad AE %d", ae);
1298 			vap->iv_stats.is_mesh_badae++;
1299 			m_freem(m);
1300 			return NULL;
1301 		}
1302 	} else {
1303 		if (ae == IEEE80211_MESH_AE_00) {
1304 			IEEE80211_ADDR_COPY(eh->ether_dhost, wh->i_addr3);
1305 			IEEE80211_ADDR_COPY(eh->ether_shost, wh->i_addr4);
1306 		} else if (ae == IEEE80211_MESH_AE_10) {
1307 			IEEE80211_ADDR_COPY(eh->ether_dhost, mc->mc_addr5);
1308 			IEEE80211_ADDR_COPY(eh->ether_shost, mc->mc_addr6);
1309 		} else {
1310 			IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
1311 			    (const struct ieee80211_frame *)wh, NULL,
1312 			    "bad AE %d", ae);
1313 			vap->iv_stats.is_mesh_badae++;
1314 			m_freem(m);
1315 			return NULL;
1316 		}
1317 	}
1318 #ifndef __NO_STRICT_ALIGNMENT
1319 	if (!ALIGNED_POINTER(mtod(m, caddr_t) + sizeof(*eh), uint32_t)) {
1320 		m = ieee80211_realign(vap, m, sizeof(*eh));
1321 		if (m == NULL)
1322 			return NULL;
1323 	}
1324 #endif /* !__NO_STRICT_ALIGNMENT */
1325 	if (llc != NULL) {
1326 		eh = mtod(m, struct ether_header *);
1327 		eh->ether_type = htons(m->m_pkthdr.len - sizeof(*eh));
1328 	}
1329 	return m;
1330 #undef	WDIR
1331 #undef	MC01
1332 }
1333 
1334 /*
1335  * Return non-zero if the unicast mesh data frame should be processed
1336  * locally.  Frames that are not proxy'd have our address, otherwise
1337  * we need to consult the routing table to look for a proxy entry.
1338  */
1339 static __inline int
1340 mesh_isucastforme(struct ieee80211vap *vap, const struct ieee80211_frame *wh,
1341     const struct ieee80211_meshcntl *mc)
1342 {
1343 	int ae = mc->mc_flags & 3;
1344 
1345 	KASSERT((wh->i_fc[1] & IEEE80211_FC1_DIR_MASK) == IEEE80211_FC1_DIR_DSTODS,
1346 	    ("bad dir 0x%x:0x%x", wh->i_fc[0], wh->i_fc[1]));
1347 	KASSERT(ae == IEEE80211_MESH_AE_00 || ae == IEEE80211_MESH_AE_10,
1348 	    ("bad AE %d", ae));
1349 	if (ae == IEEE80211_MESH_AE_10) {	/* ucast w/ proxy */
1350 		const struct ieee80211_meshcntl_ae10 *mc10 =
1351 		    (const struct ieee80211_meshcntl_ae10 *) mc;
1352 		struct ieee80211_mesh_route *rt =
1353 		    ieee80211_mesh_rt_find(vap, mc10->mc_addr5);
1354 		/* check for proxy route to ourself */
1355 		return (rt != NULL &&
1356 		    (rt->rt_flags & IEEE80211_MESHRT_FLAGS_PROXY));
1357 	} else					/* ucast w/o proxy */
1358 		return IEEE80211_ADDR_EQ(wh->i_addr3, vap->iv_myaddr);
1359 }
1360 
1361 /*
1362  * Verifies transmitter, updates lifetime, precursor list and forwards data.
1363  * > 0 means we have forwarded data and no need to process locally
1364  * == 0 means we want to process locally (and we may have forwarded data
1365  * < 0 means there was an error and data should be discarded
1366  */
1367 static int
1368 mesh_recv_indiv_data_to_fwrd(struct ieee80211vap *vap, struct mbuf *m,
1369     struct ieee80211_frame *wh, const struct ieee80211_meshcntl *mc)
1370 {
1371 	struct ieee80211_qosframe_addr4 *qwh;
1372 	struct ieee80211_mesh_state *ms = vap->iv_mesh;
1373 	struct ieee80211_mesh_route *rt_meshda, *rt_meshsa;
1374 
1375 	/* This is called from the RX path - don't hold this lock */
1376 	IEEE80211_TX_UNLOCK_ASSERT(vap->iv_ic);
1377 
1378 	qwh = (struct ieee80211_qosframe_addr4 *)wh;
1379 
1380 	/*
1381 	 * TODO:
1382 	 * o verify addr2 is  a legitimate transmitter
1383 	 * o lifetime of precursor of addr3 (addr2) is max(init, curr)
1384 	 * o lifetime of precursor of addr4 (nexthop) is max(init, curr)
1385 	 */
1386 
1387 	/* set lifetime of addr3 (meshDA) to initial value */
1388 	rt_meshda = ieee80211_mesh_rt_find(vap, qwh->i_addr3);
1389 	if (rt_meshda == NULL) {
1390 		IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH, qwh->i_addr2,
1391 		    "no route to meshDA(%6D)", qwh->i_addr3, ":");
1392 		/*
1393 		 * [Optional] any of the following three actions:
1394 		 * o silently discard 				[X]
1395 		 * o trigger a path discovery			[ ]
1396 		 * o inform TA that meshDA is unknown.		[ ]
1397 		 */
1398 		/* XXX: stats */
1399 		return (-1);
1400 	}
1401 
1402 	ieee80211_mesh_rt_update(rt_meshda, ticks_to_msecs(
1403 	    ms->ms_ppath->mpp_inact));
1404 
1405 	/* set lifetime of addr4 (meshSA) to initial value */
1406 	rt_meshsa = ieee80211_mesh_rt_find(vap, qwh->i_addr4);
1407 	KASSERT(rt_meshsa != NULL, ("no route"));
1408 	ieee80211_mesh_rt_update(rt_meshsa, ticks_to_msecs(
1409 	    ms->ms_ppath->mpp_inact));
1410 
1411 	mesh_forward(vap, m, mc);
1412 	return (1); /* dont process locally */
1413 }
1414 
1415 /*
1416  * Verifies transmitter, updates lifetime, precursor list and process data
1417  * locally, if data is proxy with AE = 10 it could mean data should go
1418  * on another mesh path or data should be forwarded to the DS.
1419  *
1420  * > 0 means we have forwarded data and no need to process locally
1421  * == 0 means we want to process locally (and we may have forwarded data
1422  * < 0 means there was an error and data should be discarded
1423  */
1424 static int
1425 mesh_recv_indiv_data_to_me(struct ieee80211vap *vap, struct mbuf *m,
1426     struct ieee80211_frame *wh, const struct ieee80211_meshcntl *mc)
1427 {
1428 	struct ieee80211_qosframe_addr4 *qwh;
1429 	const struct ieee80211_meshcntl_ae10 *mc10;
1430 	struct ieee80211_mesh_state *ms = vap->iv_mesh;
1431 	struct ieee80211_mesh_route *rt;
1432 	int ae;
1433 
1434 	/* This is called from the RX path - don't hold this lock */
1435 	IEEE80211_TX_UNLOCK_ASSERT(vap->iv_ic);
1436 
1437 	qwh = (struct ieee80211_qosframe_addr4 *)wh;
1438 	mc10 = (const struct ieee80211_meshcntl_ae10 *)mc;
1439 
1440 	/*
1441 	 * TODO:
1442 	 * o verify addr2 is  a legitimate transmitter
1443 	 * o lifetime of precursor entry is max(init, curr)
1444 	 */
1445 
1446 	/* set lifetime of addr4 (meshSA) to initial value */
1447 	rt = ieee80211_mesh_rt_find(vap, qwh->i_addr4);
1448 	KASSERT(rt != NULL, ("no route"));
1449 	ieee80211_mesh_rt_update(rt, ticks_to_msecs(ms->ms_ppath->mpp_inact));
1450 	rt = NULL;
1451 
1452 	ae = mc10->mc_flags & IEEE80211_MESH_AE_MASK;
1453 	KASSERT(ae == IEEE80211_MESH_AE_00 ||
1454 	    ae == IEEE80211_MESH_AE_10, ("bad AE %d", ae));
1455 	if (ae == IEEE80211_MESH_AE_10) {
1456 		if (IEEE80211_ADDR_EQ(mc10->mc_addr5, qwh->i_addr3)) {
1457 			return (0); /* process locally */
1458 		}
1459 
1460 		rt =  ieee80211_mesh_rt_find(vap, mc10->mc_addr5);
1461 		if (rt != NULL &&
1462 		    (rt->rt_flags & IEEE80211_MESHRT_FLAGS_VALID) &&
1463 		    (rt->rt_flags & IEEE80211_MESHRT_FLAGS_PROXY) == 0) {
1464 			/*
1465 			 * Forward on another mesh-path, according to
1466 			 * amendment as specified in 9.32.4.1
1467 			 */
1468 			IEEE80211_ADDR_COPY(qwh->i_addr3, mc10->mc_addr5);
1469 			mesh_forward(vap, m,
1470 			    (const struct ieee80211_meshcntl *)mc10);
1471 			return (1); /* dont process locally */
1472 		}
1473 		/*
1474 		 * All other cases: forward of MSDUs from the MBSS to DS indiv.
1475 		 * addressed according to 13.11.3.2.
1476 		 */
1477 		IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_OUTPUT, qwh->i_addr2,
1478 		    "forward frame to DS, SA(%6D) DA(%6D)",
1479 		    mc10->mc_addr6, ":", mc10->mc_addr5, ":");
1480 	}
1481 	return (0); /* process locally */
1482 }
1483 
1484 /*
1485  * Try to forward the group addressed data on to other mesh STAs, and
1486  * also to the DS.
1487  *
1488  * > 0 means we have forwarded data and no need to process locally
1489  * == 0 means we want to process locally (and we may have forwarded data
1490  * < 0 means there was an error and data should be discarded
1491  */
1492 static int
1493 mesh_recv_group_data(struct ieee80211vap *vap, struct mbuf *m,
1494     struct ieee80211_frame *wh, const struct ieee80211_meshcntl *mc)
1495 {
1496 #define	MC01(mc)	((const struct ieee80211_meshcntl_ae01 *)mc)
1497 	struct ieee80211_mesh_state *ms = vap->iv_mesh;
1498 
1499 	/* This is called from the RX path - don't hold this lock */
1500 	IEEE80211_TX_UNLOCK_ASSERT(vap->iv_ic);
1501 
1502 	mesh_forward(vap, m, mc);
1503 
1504 	if(mc->mc_ttl > 0) {
1505 		if (mc->mc_flags & IEEE80211_MESH_AE_01) {
1506 			/*
1507 			 * Forward of MSDUs from the MBSS to DS group addressed
1508 			 * (according to 13.11.3.2)
1509 			 * This happens by delivering the packet, and a bridge
1510 			 * will sent it on another port member.
1511 			 */
1512 			if (ms->ms_flags & IEEE80211_MESHFLAGS_GATE &&
1513 			    ms->ms_flags & IEEE80211_MESHFLAGS_FWD)
1514 				IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH,
1515 				    MC01(mc)->mc_addr4, "%s",
1516 				    "forward from MBSS to the DS");
1517 		}
1518 	}
1519 	return (0); /* process locally */
1520 #undef	MC01
1521 }
1522 
1523 static int
1524 mesh_input(struct ieee80211_node *ni, struct mbuf *m,
1525     const struct ieee80211_rx_stats *rxs, int rssi, int nf)
1526 {
1527 #define	HAS_SEQ(type)	((type & 0x4) == 0)
1528 #define	MC01(mc)	((const struct ieee80211_meshcntl_ae01 *)mc)
1529 #define	MC10(mc)	((const struct ieee80211_meshcntl_ae10 *)mc)
1530 	struct ieee80211vap *vap = ni->ni_vap;
1531 	struct ieee80211com *ic = ni->ni_ic;
1532 	struct ifnet *ifp = vap->iv_ifp;
1533 	struct ieee80211_frame *wh;
1534 	const struct ieee80211_meshcntl *mc;
1535 	int hdrspace, meshdrlen, need_tap, error;
1536 	uint8_t dir, type, subtype, ae;
1537 	uint32_t seq;
1538 	const uint8_t *addr;
1539 	uint8_t qos[2];
1540 	ieee80211_seq rxseq;
1541 
1542 	KASSERT(ni != NULL, ("null node"));
1543 	ni->ni_inact = ni->ni_inact_reload;
1544 
1545 	need_tap = 1;			/* mbuf need to be tapped. */
1546 	type = -1;			/* undefined */
1547 
1548 	/* This is called from the RX path - don't hold this lock */
1549 	IEEE80211_TX_UNLOCK_ASSERT(ic);
1550 
1551 	if (m->m_pkthdr.len < sizeof(struct ieee80211_frame_min)) {
1552 		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
1553 		    ni->ni_macaddr, NULL,
1554 		    "too short (1): len %u", m->m_pkthdr.len);
1555 		vap->iv_stats.is_rx_tooshort++;
1556 		goto out;
1557 	}
1558 	/*
1559 	 * Bit of a cheat here, we use a pointer for a 3-address
1560 	 * frame format but don't reference fields past outside
1561 	 * ieee80211_frame_min w/o first validating the data is
1562 	 * present.
1563 	*/
1564 	wh = mtod(m, struct ieee80211_frame *);
1565 
1566 	if ((wh->i_fc[0] & IEEE80211_FC0_VERSION_MASK) !=
1567 	    IEEE80211_FC0_VERSION_0) {
1568 		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
1569 		    ni->ni_macaddr, NULL, "wrong version %x", wh->i_fc[0]);
1570 		vap->iv_stats.is_rx_badversion++;
1571 		goto err;
1572 	}
1573 	dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK;
1574 	type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
1575 	subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
1576 	if ((ic->ic_flags & IEEE80211_F_SCAN) == 0) {
1577 		IEEE80211_RSSI_LPF(ni->ni_avgrssi, rssi);
1578 		ni->ni_noise = nf;
1579 		if (HAS_SEQ(type)) {
1580 			uint8_t tid = ieee80211_gettid(wh);
1581 
1582 			if (IEEE80211_QOS_HAS_SEQ(wh) &&
1583 			    TID_TO_WME_AC(tid) >= WME_AC_VI)
1584 				ic->ic_wme.wme_hipri_traffic++;
1585 			rxseq = le16toh(*(uint16_t *)wh->i_seq);
1586 			if (! ieee80211_check_rxseq(ni, wh)) {
1587 				/* duplicate, discard */
1588 				IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
1589 				    wh->i_addr1, "duplicate",
1590 				    "seqno <%u,%u> fragno <%u,%u> tid %u",
1591 				    rxseq >> IEEE80211_SEQ_SEQ_SHIFT,
1592 				    ni->ni_rxseqs[tid] >>
1593 				    IEEE80211_SEQ_SEQ_SHIFT,
1594 				    rxseq & IEEE80211_SEQ_FRAG_MASK,
1595 				    ni->ni_rxseqs[tid] &
1596 				    IEEE80211_SEQ_FRAG_MASK,
1597 				    tid);
1598 				vap->iv_stats.is_rx_dup++;
1599 				IEEE80211_NODE_STAT(ni, rx_dup);
1600 				goto out;
1601 			}
1602 			ni->ni_rxseqs[tid] = rxseq;
1603 		}
1604 	}
1605 #ifdef IEEE80211_DEBUG
1606 	/*
1607 	 * It's easier, but too expensive, to simulate different mesh
1608 	 * topologies by consulting the ACL policy very early, so do this
1609 	 * only under DEBUG.
1610 	 *
1611 	 * NB: this check is also done upon peering link initiation.
1612 	 */
1613 	if (vap->iv_acl != NULL && !vap->iv_acl->iac_check(vap, wh)) {
1614 		IEEE80211_DISCARD(vap, IEEE80211_MSG_ACL,
1615 		    wh, NULL, "%s", "disallowed by ACL");
1616 		vap->iv_stats.is_rx_acl++;
1617 		goto out;
1618 	}
1619 #endif
1620 	switch (type) {
1621 	case IEEE80211_FC0_TYPE_DATA:
1622 		if (ni == vap->iv_bss)
1623 			goto out;
1624 		if (ni->ni_mlstate != IEEE80211_NODE_MESH_ESTABLISHED) {
1625 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_MESH,
1626 			    ni->ni_macaddr, NULL,
1627 			    "peer link not yet established (%d)",
1628 			    ni->ni_mlstate);
1629 			vap->iv_stats.is_mesh_nolink++;
1630 			goto out;
1631 		}
1632 		if (dir != IEEE80211_FC1_DIR_FROMDS &&
1633 		    dir != IEEE80211_FC1_DIR_DSTODS) {
1634 			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
1635 			    wh, "data", "incorrect dir 0x%x", dir);
1636 			vap->iv_stats.is_rx_wrongdir++;
1637 			goto err;
1638 		}
1639 
1640 		/* All Mesh data frames are QoS subtype */
1641 		if (!HAS_SEQ(type)) {
1642 			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
1643 			    wh, "data", "incorrect subtype 0x%x", subtype);
1644 			vap->iv_stats.is_rx_badsubtype++;
1645 			goto err;
1646 		}
1647 
1648 		/*
1649 		 * Next up, any fragmentation.
1650 		 * XXX: we defrag before we even try to forward,
1651 		 * Mesh Control field is not present in sub-sequent
1652 		 * fragmented frames. This is in contrast to Draft 4.0.
1653 		 */
1654 		hdrspace = ieee80211_hdrspace(ic, wh);
1655 		if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1656 			m = ieee80211_defrag(ni, m, hdrspace);
1657 			if (m == NULL) {
1658 				/* Fragment dropped or frame not complete yet */
1659 				goto out;
1660 			}
1661 		}
1662 		wh = mtod(m, struct ieee80211_frame *); /* NB: after defrag */
1663 
1664 		/*
1665 		 * Now we have a complete Mesh Data frame.
1666 		 */
1667 
1668 		/*
1669 		 * Only fromDStoDS data frames use 4 address qos frames
1670 		 * as specified in amendment. Otherwise addr4 is located
1671 		 * in the Mesh Control field and a 3 address qos frame
1672 		 * is used.
1673 		 */
1674 		if (IEEE80211_IS_DSTODS(wh))
1675 			*(uint16_t *)qos = *(uint16_t *)
1676 			    ((struct ieee80211_qosframe_addr4 *)wh)->i_qos;
1677 		else
1678 			*(uint16_t *)qos = *(uint16_t *)
1679 			    ((struct ieee80211_qosframe *)wh)->i_qos;
1680 
1681 		/*
1682 		 * NB: The mesh STA sets the Mesh Control Present
1683 		 * subfield to 1 in the Mesh Data frame containing
1684 		 * an unfragmented MSDU, an A-MSDU, or the first
1685 		 * fragment of an MSDU.
1686 		 * After defrag it should always be present.
1687 		 */
1688 		if (!(qos[1] & IEEE80211_QOS_MC)) {
1689 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_MESH,
1690 			    ni->ni_macaddr, NULL,
1691 			    "%s", "Mesh control field not present");
1692 			vap->iv_stats.is_rx_elem_missing++; /* XXX: kinda */
1693 			goto err;
1694 		}
1695 
1696 		/* pull up enough to get to the mesh control */
1697 		if (m->m_len < hdrspace + sizeof(struct ieee80211_meshcntl) &&
1698 		    (m = m_pullup(m, hdrspace +
1699 		        sizeof(struct ieee80211_meshcntl))) == NULL) {
1700 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
1701 			    ni->ni_macaddr, NULL,
1702 			    "data too short: expecting %u", hdrspace);
1703 			vap->iv_stats.is_rx_tooshort++;
1704 			goto out;		/* XXX */
1705 		}
1706 		/*
1707 		 * Now calculate the full extent of the headers. Note
1708 		 * mesh_decap will pull up anything we didn't get
1709 		 * above when it strips the 802.11 headers.
1710 		 */
1711 		mc = (const struct ieee80211_meshcntl *)
1712 		    (mtod(m, const uint8_t *) + hdrspace);
1713 		ae = mc->mc_flags & IEEE80211_MESH_AE_MASK;
1714 		meshdrlen = sizeof(struct ieee80211_meshcntl) +
1715 		    ae * IEEE80211_ADDR_LEN;
1716 		hdrspace += meshdrlen;
1717 
1718 		/* pull complete hdrspace = ieee80211_hdrspace + meshcontrol */
1719 		if ((meshdrlen > sizeof(struct ieee80211_meshcntl)) &&
1720 		    (m->m_len < hdrspace) &&
1721 		    ((m = m_pullup(m, hdrspace)) == NULL)) {
1722 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
1723 			    ni->ni_macaddr, NULL,
1724 			    "data too short: expecting %u", hdrspace);
1725 			vap->iv_stats.is_rx_tooshort++;
1726 			goto out;		/* XXX */
1727 		}
1728 		/* XXX: are we sure there is no reallocating after m_pullup? */
1729 
1730 		seq = LE_READ_4(mc->mc_seq);
1731 		if (IEEE80211_IS_MULTICAST(wh->i_addr1))
1732 			addr = wh->i_addr3;
1733 		else if (ae == IEEE80211_MESH_AE_01)
1734 			addr = MC01(mc)->mc_addr4;
1735 		else
1736 			addr = ((struct ieee80211_qosframe_addr4 *)wh)->i_addr4;
1737 		if (IEEE80211_ADDR_EQ(vap->iv_myaddr, addr)) {
1738 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
1739 			    addr, "data", "%s", "not to me");
1740 			vap->iv_stats.is_rx_wrongbss++;	/* XXX kinda */
1741 			goto out;
1742 		}
1743 		if (mesh_checkpseq(vap, addr, seq) != 0) {
1744 			vap->iv_stats.is_rx_dup++;
1745 			goto out;
1746 		}
1747 
1748 		/* This code "routes" the frame to the right control path */
1749 		if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1750 			if (IEEE80211_ADDR_EQ(vap->iv_myaddr, wh->i_addr3))
1751 				error =
1752 				    mesh_recv_indiv_data_to_me(vap, m, wh, mc);
1753 			else if (IEEE80211_IS_MULTICAST(wh->i_addr3))
1754 				error = mesh_recv_group_data(vap, m, wh, mc);
1755 			else
1756 				error = mesh_recv_indiv_data_to_fwrd(vap, m,
1757 				    wh, mc);
1758 		} else
1759 			error = mesh_recv_group_data(vap, m, wh, mc);
1760 		if (error < 0)
1761 			goto err;
1762 		else if (error > 0)
1763 			goto out;
1764 
1765 		if (ieee80211_radiotap_active_vap(vap))
1766 			ieee80211_radiotap_rx(vap, m);
1767 		need_tap = 0;
1768 
1769 		/*
1770 		 * Finally, strip the 802.11 header.
1771 		 */
1772 		m = mesh_decap(vap, m, hdrspace, meshdrlen);
1773 		if (m == NULL) {
1774 			/* XXX mask bit to check for both */
1775 			/* don't count Null data frames as errors */
1776 			if (subtype == IEEE80211_FC0_SUBTYPE_NODATA ||
1777 			    subtype == IEEE80211_FC0_SUBTYPE_QOS_NULL)
1778 				goto out;
1779 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
1780 			    ni->ni_macaddr, "data", "%s", "decap error");
1781 			vap->iv_stats.is_rx_decap++;
1782 			IEEE80211_NODE_STAT(ni, rx_decap);
1783 			goto err;
1784 		}
1785 		if (qos[0] & IEEE80211_QOS_AMSDU) {
1786 			m = ieee80211_decap_amsdu(ni, m);
1787 			if (m == NULL)
1788 				return IEEE80211_FC0_TYPE_DATA;
1789 		}
1790 		ieee80211_deliver_data(vap, ni, m);
1791 		return type;
1792 	case IEEE80211_FC0_TYPE_MGT:
1793 		vap->iv_stats.is_rx_mgmt++;
1794 		IEEE80211_NODE_STAT(ni, rx_mgmt);
1795 		if (dir != IEEE80211_FC1_DIR_NODS) {
1796 			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
1797 			    wh, "mgt", "incorrect dir 0x%x", dir);
1798 			vap->iv_stats.is_rx_wrongdir++;
1799 			goto err;
1800 		}
1801 		if (m->m_pkthdr.len < sizeof(struct ieee80211_frame)) {
1802 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
1803 			    ni->ni_macaddr, "mgt", "too short: len %u",
1804 			    m->m_pkthdr.len);
1805 			vap->iv_stats.is_rx_tooshort++;
1806 			goto out;
1807 		}
1808 #ifdef IEEE80211_DEBUG
1809 		if ((ieee80211_msg_debug(vap) &&
1810 		    (vap->iv_ic->ic_flags & IEEE80211_F_SCAN)) ||
1811 		    ieee80211_msg_dumppkts(vap)) {
1812 			if_printf(ifp, "received %s from %s rssi %d\n",
1813 			    ieee80211_mgt_subtype_name[subtype >>
1814 			    IEEE80211_FC0_SUBTYPE_SHIFT],
1815 			    ether_sprintf(wh->i_addr2), rssi);
1816 		}
1817 #endif
1818 		if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
1819 			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
1820 			    wh, NULL, "%s", "WEP set but not permitted");
1821 			vap->iv_stats.is_rx_mgtdiscard++; /* XXX */
1822 			goto out;
1823 		}
1824 		vap->iv_recv_mgmt(ni, m, subtype, rxs, rssi, nf);
1825 		goto out;
1826 	case IEEE80211_FC0_TYPE_CTL:
1827 		vap->iv_stats.is_rx_ctl++;
1828 		IEEE80211_NODE_STAT(ni, rx_ctrl);
1829 		goto out;
1830 	default:
1831 		IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
1832 		    wh, "bad", "frame type 0x%x", type);
1833 		/* should not come here */
1834 		break;
1835 	}
1836 err:
1837 	if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
1838 out:
1839 	if (m != NULL) {
1840 		if (need_tap && ieee80211_radiotap_active_vap(vap))
1841 			ieee80211_radiotap_rx(vap, m);
1842 		m_freem(m);
1843 	}
1844 	return type;
1845 #undef	HAS_SEQ
1846 #undef	MC01
1847 #undef	MC10
1848 }
1849 
1850 static void
1851 mesh_recv_mgmt(struct ieee80211_node *ni, struct mbuf *m0, int subtype,
1852     const struct ieee80211_rx_stats *rxs, int rssi, int nf)
1853 {
1854 	struct ieee80211vap *vap = ni->ni_vap;
1855 	struct ieee80211_mesh_state *ms = vap->iv_mesh;
1856 	struct ieee80211com *ic = ni->ni_ic;
1857 	struct ieee80211_channel *rxchan = ic->ic_curchan;
1858 	struct ieee80211_frame *wh;
1859 	struct ieee80211_mesh_route *rt;
1860 	uint8_t *frm, *efrm;
1861 
1862 	wh = mtod(m0, struct ieee80211_frame *);
1863 	frm = (uint8_t *)&wh[1];
1864 	efrm = mtod(m0, uint8_t *) + m0->m_len;
1865 	switch (subtype) {
1866 	case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
1867 	case IEEE80211_FC0_SUBTYPE_BEACON:
1868 	{
1869 		struct ieee80211_scanparams scan;
1870 		struct ieee80211_channel *c;
1871 		/*
1872 		 * We process beacon/probe response
1873 		 * frames to discover neighbors.
1874 		 */
1875 		if (rxs != NULL) {
1876 			c = ieee80211_lookup_channel_rxstatus(vap, rxs);
1877 			if (c != NULL)
1878 				rxchan = c;
1879 		}
1880 		if (ieee80211_parse_beacon(ni, m0, rxchan, &scan) != 0)
1881 			return;
1882 		/*
1883 		 * Count frame now that we know it's to be processed.
1884 		 */
1885 		if (subtype == IEEE80211_FC0_SUBTYPE_BEACON) {
1886 			vap->iv_stats.is_rx_beacon++;	/* XXX remove */
1887 			IEEE80211_NODE_STAT(ni, rx_beacons);
1888 		} else
1889 			IEEE80211_NODE_STAT(ni, rx_proberesp);
1890 		/*
1891 		 * If scanning, just pass information to the scan module.
1892 		 */
1893 		if (ic->ic_flags & IEEE80211_F_SCAN) {
1894 			if (ic->ic_flags_ext & IEEE80211_FEXT_PROBECHAN) {
1895 				/*
1896 				 * Actively scanning a channel marked passive;
1897 				 * send a probe request now that we know there
1898 				 * is 802.11 traffic present.
1899 				 *
1900 				 * XXX check if the beacon we recv'd gives
1901 				 * us what we need and suppress the probe req
1902 				 */
1903 				ieee80211_probe_curchan(vap, 1);
1904 				ic->ic_flags_ext &= ~IEEE80211_FEXT_PROBECHAN;
1905 			}
1906 			ieee80211_add_scan(vap, rxchan, &scan, wh,
1907 			    subtype, rssi, nf);
1908 			return;
1909 		}
1910 
1911 		/* The rest of this code assumes we are running */
1912 		if (vap->iv_state != IEEE80211_S_RUN)
1913 			return;
1914 		/*
1915 		 * Ignore non-mesh STAs.
1916 		 */
1917 		if ((scan.capinfo &
1918 		     (IEEE80211_CAPINFO_ESS|IEEE80211_CAPINFO_IBSS)) ||
1919 		    scan.meshid == NULL || scan.meshconf == NULL) {
1920 			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
1921 			    wh, "beacon", "%s", "not a mesh sta");
1922 			vap->iv_stats.is_mesh_wrongmesh++;
1923 			return;
1924 		}
1925 		/*
1926 		 * Ignore STAs for other mesh networks.
1927 		 */
1928 		if (memcmp(scan.meshid+2, ms->ms_id, ms->ms_idlen) != 0 ||
1929 		    mesh_verify_meshconf(vap, scan.meshconf)) {
1930 			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
1931 			    wh, "beacon", "%s", "not for our mesh");
1932 			vap->iv_stats.is_mesh_wrongmesh++;
1933 			return;
1934 		}
1935 		/*
1936 		 * Peer only based on the current ACL policy.
1937 		 */
1938 		if (vap->iv_acl != NULL && !vap->iv_acl->iac_check(vap, wh)) {
1939 			IEEE80211_DISCARD(vap, IEEE80211_MSG_ACL,
1940 			    wh, NULL, "%s", "disallowed by ACL");
1941 			vap->iv_stats.is_rx_acl++;
1942 			return;
1943 		}
1944 		/*
1945 		 * Do neighbor discovery.
1946 		 */
1947 		if (!IEEE80211_ADDR_EQ(wh->i_addr2, ni->ni_macaddr)) {
1948 			/*
1949 			 * Create a new entry in the neighbor table.
1950 			 */
1951 			ni = ieee80211_add_neighbor(vap, wh, &scan);
1952 		}
1953 		/*
1954 		 * Automatically peer with discovered nodes if possible.
1955 		 */
1956 		if (ni != vap->iv_bss &&
1957 		    (ms->ms_flags & IEEE80211_MESHFLAGS_AP)) {
1958 			switch (ni->ni_mlstate) {
1959 			case IEEE80211_NODE_MESH_IDLE:
1960 			{
1961 				uint16_t args[1];
1962 
1963 				/* Wait for backoff callout to reset counter */
1964 				if (ni->ni_mlhcnt >= ieee80211_mesh_maxholding)
1965 					return;
1966 
1967 				ni->ni_mlpid = mesh_generateid(vap);
1968 				if (ni->ni_mlpid == 0)
1969 					return;
1970 				mesh_linkchange(ni, IEEE80211_NODE_MESH_OPENSNT);
1971 				args[0] = ni->ni_mlpid;
1972 				ieee80211_send_action(ni,
1973 				IEEE80211_ACTION_CAT_SELF_PROT,
1974 				IEEE80211_ACTION_MESHPEERING_OPEN, args);
1975 				ni->ni_mlrcnt = 0;
1976 				mesh_peer_timeout_setup(ni);
1977 				break;
1978 			}
1979 			case IEEE80211_NODE_MESH_ESTABLISHED:
1980 			{
1981 				/*
1982 				 * Valid beacon from a peer mesh STA
1983 				 * bump TA lifetime
1984 				 */
1985 				rt = ieee80211_mesh_rt_find(vap, wh->i_addr2);
1986 				if(rt != NULL) {
1987 					ieee80211_mesh_rt_update(rt,
1988 					    ticks_to_msecs(
1989 					    ms->ms_ppath->mpp_inact));
1990 				}
1991 				break;
1992 			}
1993 			default:
1994 				break; /* ignore */
1995 			}
1996 		}
1997 		break;
1998 	}
1999 	case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
2000 	{
2001 		uint8_t *ssid, *meshid, *rates, *xrates;
2002 		uint8_t *sfrm;
2003 
2004 		if (vap->iv_state != IEEE80211_S_RUN) {
2005 			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
2006 			    wh, NULL, "wrong state %s",
2007 			    ieee80211_state_name[vap->iv_state]);
2008 			vap->iv_stats.is_rx_mgtdiscard++;
2009 			return;
2010 		}
2011 		if (IEEE80211_IS_MULTICAST(wh->i_addr2)) {
2012 			/* frame must be directed */
2013 			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
2014 			    wh, NULL, "%s", "not unicast");
2015 			vap->iv_stats.is_rx_mgtdiscard++;	/* XXX stat */
2016 			return;
2017 		}
2018 		/*
2019 		 * prreq frame format
2020 		 *      [tlv] ssid
2021 		 *      [tlv] supported rates
2022 		 *      [tlv] extended supported rates
2023 		 *	[tlv] mesh id
2024 		 */
2025 		ssid = meshid = rates = xrates = NULL;
2026 		sfrm = frm;
2027 		while (efrm - frm > 1) {
2028 			IEEE80211_VERIFY_LENGTH(efrm - frm, frm[1] + 2, return);
2029 			switch (*frm) {
2030 			case IEEE80211_ELEMID_SSID:
2031 				ssid = frm;
2032 				break;
2033 			case IEEE80211_ELEMID_RATES:
2034 				rates = frm;
2035 				break;
2036 			case IEEE80211_ELEMID_XRATES:
2037 				xrates = frm;
2038 				break;
2039 			case IEEE80211_ELEMID_MESHID:
2040 				meshid = frm;
2041 				break;
2042 			}
2043 			frm += frm[1] + 2;
2044 		}
2045 		IEEE80211_VERIFY_ELEMENT(ssid, IEEE80211_NWID_LEN, return);
2046 		IEEE80211_VERIFY_ELEMENT(rates, IEEE80211_RATE_MAXSIZE, return);
2047 		if (xrates != NULL)
2048 			IEEE80211_VERIFY_ELEMENT(xrates,
2049 			    IEEE80211_RATE_MAXSIZE - rates[1], return);
2050 		if (meshid != NULL) {
2051 			IEEE80211_VERIFY_ELEMENT(meshid,
2052 			    IEEE80211_MESHID_LEN, return);
2053 			/* NB: meshid, not ssid */
2054 			IEEE80211_VERIFY_SSID(vap->iv_bss, meshid, return);
2055 		}
2056 
2057 		/* XXX find a better class or define it's own */
2058 		IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_INPUT, wh->i_addr2,
2059 		    "%s", "recv probe req");
2060 		/*
2061 		 * Some legacy 11b clients cannot hack a complete
2062 		 * probe response frame.  When the request includes
2063 		 * only a bare-bones rate set, communicate this to
2064 		 * the transmit side.
2065 		 */
2066 		ieee80211_send_proberesp(vap, wh->i_addr2, 0);
2067 		break;
2068 	}
2069 
2070 	case IEEE80211_FC0_SUBTYPE_ACTION:
2071 	case IEEE80211_FC0_SUBTYPE_ACTION_NOACK:
2072 		if (ni == vap->iv_bss) {
2073 			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
2074 			    wh, NULL, "%s", "unknown node");
2075 			vap->iv_stats.is_rx_mgtdiscard++;
2076 		} else if (!IEEE80211_ADDR_EQ(vap->iv_myaddr, wh->i_addr1) &&
2077 		    !IEEE80211_IS_MULTICAST(wh->i_addr1)) {
2078 			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
2079 			    wh, NULL, "%s", "not for us");
2080 			vap->iv_stats.is_rx_mgtdiscard++;
2081 		} else if (vap->iv_state != IEEE80211_S_RUN) {
2082 			IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
2083 			    wh, NULL, "wrong state %s",
2084 			    ieee80211_state_name[vap->iv_state]);
2085 			vap->iv_stats.is_rx_mgtdiscard++;
2086 		} else {
2087 			if (ieee80211_parse_action(ni, m0) == 0)
2088 				(void)ic->ic_recv_action(ni, wh, frm, efrm);
2089 		}
2090 		break;
2091 
2092 	case IEEE80211_FC0_SUBTYPE_ASSOC_REQ:
2093 	case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
2094 	case IEEE80211_FC0_SUBTYPE_REASSOC_REQ:
2095 	case IEEE80211_FC0_SUBTYPE_REASSOC_RESP:
2096 	case IEEE80211_FC0_SUBTYPE_ATIM:
2097 	case IEEE80211_FC0_SUBTYPE_DISASSOC:
2098 	case IEEE80211_FC0_SUBTYPE_AUTH:
2099 	case IEEE80211_FC0_SUBTYPE_DEAUTH:
2100 		IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
2101 		    wh, NULL, "%s", "not handled");
2102 		vap->iv_stats.is_rx_mgtdiscard++;
2103 		break;
2104 
2105 	default:
2106 		IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
2107 		    wh, "mgt", "subtype 0x%x not handled", subtype);
2108 		vap->iv_stats.is_rx_badsubtype++;
2109 		break;
2110 	}
2111 }
2112 
2113 static void
2114 mesh_recv_ctl(struct ieee80211_node *ni, struct mbuf *m, int subtype)
2115 {
2116 
2117 	switch (subtype) {
2118 	case IEEE80211_FC0_SUBTYPE_BAR:
2119 		ieee80211_recv_bar(ni, m);
2120 		break;
2121 	}
2122 }
2123 
2124 /*
2125  * Parse meshpeering action ie's for MPM frames
2126  */
2127 static const struct ieee80211_meshpeer_ie *
2128 mesh_parse_meshpeering_action(struct ieee80211_node *ni,
2129 	const struct ieee80211_frame *wh,	/* XXX for VERIFY_LENGTH */
2130 	const uint8_t *frm, const uint8_t *efrm,
2131 	struct ieee80211_meshpeer_ie *mp, uint8_t subtype)
2132 {
2133 	struct ieee80211vap *vap = ni->ni_vap;
2134 	const struct ieee80211_meshpeer_ie *mpie;
2135 	uint16_t args[3];
2136 	const uint8_t *meshid, *meshconf, *meshpeer;
2137 	uint8_t sendclose = 0; /* 1 = MPM frame rejected, close will be sent */
2138 
2139 	meshid = meshconf = meshpeer = NULL;
2140 	while (efrm - frm > 1) {
2141 		IEEE80211_VERIFY_LENGTH(efrm - frm, frm[1] + 2, return NULL);
2142 		switch (*frm) {
2143 		case IEEE80211_ELEMID_MESHID:
2144 			meshid = frm;
2145 			break;
2146 		case IEEE80211_ELEMID_MESHCONF:
2147 			meshconf = frm;
2148 			break;
2149 		case IEEE80211_ELEMID_MESHPEER:
2150 			meshpeer = frm;
2151 			mpie = (const struct ieee80211_meshpeer_ie *) frm;
2152 			memset(mp, 0, sizeof(*mp));
2153 			mp->peer_len = mpie->peer_len;
2154 			mp->peer_proto = LE_READ_2(&mpie->peer_proto);
2155 			mp->peer_llinkid = LE_READ_2(&mpie->peer_llinkid);
2156 			switch (subtype) {
2157 			case IEEE80211_ACTION_MESHPEERING_CONFIRM:
2158 				mp->peer_linkid =
2159 				    LE_READ_2(&mpie->peer_linkid);
2160 				break;
2161 			case IEEE80211_ACTION_MESHPEERING_CLOSE:
2162 				/* NB: peer link ID is optional */
2163 				if (mpie->peer_len ==
2164 				    (IEEE80211_MPM_BASE_SZ + 2)) {
2165 					mp->peer_linkid = 0;
2166 					mp->peer_rcode =
2167 					    LE_READ_2(&mpie->peer_linkid);
2168 				} else {
2169 					mp->peer_linkid =
2170 					    LE_READ_2(&mpie->peer_linkid);
2171 					mp->peer_rcode =
2172 					    LE_READ_2(&mpie->peer_rcode);
2173 				}
2174 				break;
2175 			}
2176 			break;
2177 		}
2178 		frm += frm[1] + 2;
2179 	}
2180 
2181 	/*
2182 	 * Verify the contents of the frame.
2183 	 * If it fails validation, close the peer link.
2184 	 */
2185 	if (mesh_verify_meshpeer(vap, subtype, (const uint8_t *)mp)) {
2186 		sendclose = 1;
2187 		IEEE80211_DISCARD(vap,
2188 		    IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH,
2189 		    wh, NULL, "%s", "MPM validation failed");
2190 	}
2191 
2192 	/* If meshid is not the same reject any frames type. */
2193 	if (sendclose == 0 && mesh_verify_meshid(vap, meshid)) {
2194 		sendclose = 1;
2195 		IEEE80211_DISCARD(vap,
2196 		    IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH,
2197 		    wh, NULL, "%s", "not for our mesh");
2198 		if (subtype == IEEE80211_ACTION_MESHPEERING_CLOSE) {
2199 			/*
2200 			 * Standard not clear about this, if we dont ignore
2201 			 * there will be an endless loop between nodes sending
2202 			 * CLOSE frames between each other with wrong meshid.
2203 			 * Discard and timers will bring FSM to IDLE state.
2204 			 */
2205 			return NULL;
2206 		}
2207 	}
2208 
2209 	/*
2210 	 * Close frames are accepted if meshid is the same.
2211 	 * Verify the other two types.
2212 	 */
2213 	if (sendclose == 0 && subtype != IEEE80211_ACTION_MESHPEERING_CLOSE &&
2214 	    mesh_verify_meshconf(vap, meshconf)) {
2215 		sendclose = 1;
2216 		IEEE80211_DISCARD(vap,
2217 		    IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH,
2218 		    wh, NULL, "%s", "configuration missmatch");
2219 	}
2220 
2221 	if (sendclose) {
2222 		vap->iv_stats.is_rx_mgtdiscard++;
2223 		switch (ni->ni_mlstate) {
2224 		case IEEE80211_NODE_MESH_IDLE:
2225 		case IEEE80211_NODE_MESH_ESTABLISHED:
2226 		case IEEE80211_NODE_MESH_HOLDING:
2227 			/* ignore */
2228 			break;
2229 		case IEEE80211_NODE_MESH_OPENSNT:
2230 		case IEEE80211_NODE_MESH_OPENRCV:
2231 		case IEEE80211_NODE_MESH_CONFIRMRCV:
2232 			args[0] = ni->ni_mlpid;
2233 			args[1] = ni->ni_mllid;
2234 			/* Reason codes for rejection */
2235 			switch (subtype) {
2236 			case IEEE80211_ACTION_MESHPEERING_OPEN:
2237 				args[2] = IEEE80211_REASON_MESH_CPVIOLATION;
2238 				break;
2239 			case IEEE80211_ACTION_MESHPEERING_CONFIRM:
2240 				args[2] = IEEE80211_REASON_MESH_INCONS_PARAMS;
2241 				break;
2242 			}
2243 			ieee80211_send_action(ni,
2244 			    IEEE80211_ACTION_CAT_SELF_PROT,
2245 			    IEEE80211_ACTION_MESHPEERING_CLOSE,
2246 			    args);
2247 			mesh_linkchange(ni, IEEE80211_NODE_MESH_HOLDING);
2248 			mesh_peer_timeout_setup(ni);
2249 			break;
2250 		}
2251 		return NULL;
2252 	}
2253 
2254 	return (const struct ieee80211_meshpeer_ie *) mp;
2255 }
2256 
2257 static int
2258 mesh_recv_action_meshpeering_open(struct ieee80211_node *ni,
2259 	const struct ieee80211_frame *wh,
2260 	const uint8_t *frm, const uint8_t *efrm)
2261 {
2262 	struct ieee80211vap *vap = ni->ni_vap;
2263 	struct ieee80211_mesh_state *ms = vap->iv_mesh;
2264 	struct ieee80211_meshpeer_ie ie;
2265 	const struct ieee80211_meshpeer_ie *meshpeer;
2266 	uint16_t args[3];
2267 
2268 	/* +2+2 for action + code + capabilites */
2269 	meshpeer = mesh_parse_meshpeering_action(ni, wh, frm+2+2, efrm, &ie,
2270 	    IEEE80211_ACTION_MESHPEERING_OPEN);
2271 	if (meshpeer == NULL) {
2272 		return 0;
2273 	}
2274 
2275 	/* XXX move up */
2276 	IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH, ni,
2277 	    "recv PEER OPEN, lid 0x%x", meshpeer->peer_llinkid);
2278 
2279 	switch (ni->ni_mlstate) {
2280 	case IEEE80211_NODE_MESH_IDLE:
2281 		/* Reject open request if reached our maximum neighbor count */
2282 		if (ms->ms_neighbors >= IEEE80211_MESH_MAX_NEIGHBORS) {
2283 			args[0] = meshpeer->peer_llinkid;
2284 			args[1] = 0;
2285 			args[2] = IEEE80211_REASON_MESH_MAX_PEERS;
2286 			ieee80211_send_action(ni,
2287 			    IEEE80211_ACTION_CAT_SELF_PROT,
2288 			    IEEE80211_ACTION_MESHPEERING_CLOSE,
2289 			    args);
2290 			/* stay in IDLE state */
2291 			return (0);
2292 		}
2293 		/* Open frame accepted */
2294 		mesh_linkchange(ni, IEEE80211_NODE_MESH_OPENRCV);
2295 		ni->ni_mllid = meshpeer->peer_llinkid;
2296 		ni->ni_mlpid = mesh_generateid(vap);
2297 		if (ni->ni_mlpid == 0)
2298 			return 0;		/* XXX */
2299 		args[0] = ni->ni_mlpid;
2300 		/* Announce we're open too... */
2301 		ieee80211_send_action(ni,
2302 		    IEEE80211_ACTION_CAT_SELF_PROT,
2303 		    IEEE80211_ACTION_MESHPEERING_OPEN, args);
2304 		/* ...and confirm the link. */
2305 		args[0] = ni->ni_mlpid;
2306 		args[1] = ni->ni_mllid;
2307 		ieee80211_send_action(ni,
2308 		    IEEE80211_ACTION_CAT_SELF_PROT,
2309 		    IEEE80211_ACTION_MESHPEERING_CONFIRM,
2310 		    args);
2311 		mesh_peer_timeout_setup(ni);
2312 		break;
2313 	case IEEE80211_NODE_MESH_OPENRCV:
2314 		/* Wrong Link ID */
2315 		if (ni->ni_mllid != meshpeer->peer_llinkid) {
2316 			args[0] = ni->ni_mllid;
2317 			args[1] = ni->ni_mlpid;
2318 			args[2] = IEEE80211_REASON_PEER_LINK_CANCELED;
2319 			ieee80211_send_action(ni,
2320 			    IEEE80211_ACTION_CAT_SELF_PROT,
2321 			    IEEE80211_ACTION_MESHPEERING_CLOSE,
2322 			    args);
2323 			mesh_linkchange(ni, IEEE80211_NODE_MESH_HOLDING);
2324 			mesh_peer_timeout_setup(ni);
2325 			break;
2326 		}
2327 		/* Duplicate open, confirm again. */
2328 		args[0] = ni->ni_mlpid;
2329 		args[1] = ni->ni_mllid;
2330 		ieee80211_send_action(ni,
2331 		    IEEE80211_ACTION_CAT_SELF_PROT,
2332 		    IEEE80211_ACTION_MESHPEERING_CONFIRM,
2333 		    args);
2334 		break;
2335 	case IEEE80211_NODE_MESH_OPENSNT:
2336 		ni->ni_mllid = meshpeer->peer_llinkid;
2337 		mesh_linkchange(ni, IEEE80211_NODE_MESH_OPENRCV);
2338 		args[0] = ni->ni_mlpid;
2339 		args[1] = ni->ni_mllid;
2340 		ieee80211_send_action(ni,
2341 		    IEEE80211_ACTION_CAT_SELF_PROT,
2342 		    IEEE80211_ACTION_MESHPEERING_CONFIRM,
2343 		    args);
2344 		/* NB: don't setup/clear any timeout */
2345 		break;
2346 	case IEEE80211_NODE_MESH_CONFIRMRCV:
2347 		if (ni->ni_mlpid != meshpeer->peer_linkid ||
2348 		    ni->ni_mllid != meshpeer->peer_llinkid) {
2349 			args[0] = ni->ni_mlpid;
2350 			args[1] = ni->ni_mllid;
2351 			args[2] = IEEE80211_REASON_PEER_LINK_CANCELED;
2352 			ieee80211_send_action(ni,
2353 			    IEEE80211_ACTION_CAT_SELF_PROT,
2354 			    IEEE80211_ACTION_MESHPEERING_CLOSE,
2355 			    args);
2356 			mesh_linkchange(ni,
2357 			    IEEE80211_NODE_MESH_HOLDING);
2358 			mesh_peer_timeout_setup(ni);
2359 			break;
2360 		}
2361 		mesh_linkchange(ni, IEEE80211_NODE_MESH_ESTABLISHED);
2362 		ni->ni_mllid = meshpeer->peer_llinkid;
2363 		args[0] = ni->ni_mlpid;
2364 		args[1] = ni->ni_mllid;
2365 		ieee80211_send_action(ni,
2366 		    IEEE80211_ACTION_CAT_SELF_PROT,
2367 		    IEEE80211_ACTION_MESHPEERING_CONFIRM,
2368 		    args);
2369 		mesh_peer_timeout_stop(ni);
2370 		break;
2371 	case IEEE80211_NODE_MESH_ESTABLISHED:
2372 		if (ni->ni_mllid != meshpeer->peer_llinkid) {
2373 			args[0] = ni->ni_mllid;
2374 			args[1] = ni->ni_mlpid;
2375 			args[2] = IEEE80211_REASON_PEER_LINK_CANCELED;
2376 			ieee80211_send_action(ni,
2377 			    IEEE80211_ACTION_CAT_SELF_PROT,
2378 			    IEEE80211_ACTION_MESHPEERING_CLOSE,
2379 			    args);
2380 			mesh_linkchange(ni, IEEE80211_NODE_MESH_HOLDING);
2381 			mesh_peer_timeout_setup(ni);
2382 			break;
2383 		}
2384 		args[0] = ni->ni_mlpid;
2385 		args[1] = ni->ni_mllid;
2386 		ieee80211_send_action(ni,
2387 		    IEEE80211_ACTION_CAT_SELF_PROT,
2388 		    IEEE80211_ACTION_MESHPEERING_CONFIRM,
2389 		    args);
2390 		break;
2391 	case IEEE80211_NODE_MESH_HOLDING:
2392 		args[0] = ni->ni_mlpid;
2393 		args[1] = meshpeer->peer_llinkid;
2394 		/* Standard not clear about what the reaason code should be */
2395 		args[2] = IEEE80211_REASON_PEER_LINK_CANCELED;
2396 		ieee80211_send_action(ni,
2397 		    IEEE80211_ACTION_CAT_SELF_PROT,
2398 		    IEEE80211_ACTION_MESHPEERING_CLOSE,
2399 		    args);
2400 		break;
2401 	}
2402 	return 0;
2403 }
2404 
2405 static int
2406 mesh_recv_action_meshpeering_confirm(struct ieee80211_node *ni,
2407 	const struct ieee80211_frame *wh,
2408 	const uint8_t *frm, const uint8_t *efrm)
2409 {
2410 	struct ieee80211vap *vap = ni->ni_vap;
2411 	struct ieee80211_meshpeer_ie ie;
2412 	const struct ieee80211_meshpeer_ie *meshpeer;
2413 	uint16_t args[3];
2414 
2415 	/* +2+2+2+2 for action + code + capabilites + status code + AID */
2416 	meshpeer = mesh_parse_meshpeering_action(ni, wh, frm+2+2+2+2, efrm, &ie,
2417 	    IEEE80211_ACTION_MESHPEERING_CONFIRM);
2418 	if (meshpeer == NULL) {
2419 		return 0;
2420 	}
2421 
2422 	IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH, ni,
2423 	    "recv PEER CONFIRM, local id 0x%x, peer id 0x%x",
2424 	    meshpeer->peer_llinkid, meshpeer->peer_linkid);
2425 
2426 	switch (ni->ni_mlstate) {
2427 	case IEEE80211_NODE_MESH_OPENRCV:
2428 		mesh_linkchange(ni, IEEE80211_NODE_MESH_ESTABLISHED);
2429 		mesh_peer_timeout_stop(ni);
2430 		break;
2431 	case IEEE80211_NODE_MESH_OPENSNT:
2432 		mesh_linkchange(ni, IEEE80211_NODE_MESH_CONFIRMRCV);
2433 		mesh_peer_timeout_setup(ni);
2434 		break;
2435 	case IEEE80211_NODE_MESH_HOLDING:
2436 		args[0] = ni->ni_mlpid;
2437 		args[1] = meshpeer->peer_llinkid;
2438 		/* Standard not clear about what the reaason code should be */
2439 		args[2] = IEEE80211_REASON_PEER_LINK_CANCELED;
2440 		ieee80211_send_action(ni,
2441 		    IEEE80211_ACTION_CAT_SELF_PROT,
2442 		    IEEE80211_ACTION_MESHPEERING_CLOSE,
2443 		    args);
2444 		break;
2445 	case IEEE80211_NODE_MESH_CONFIRMRCV:
2446 		if (ni->ni_mllid != meshpeer->peer_llinkid) {
2447 			args[0] = ni->ni_mlpid;
2448 			args[1] = ni->ni_mllid;
2449 			args[2] = IEEE80211_REASON_PEER_LINK_CANCELED;
2450 			ieee80211_send_action(ni,
2451 			    IEEE80211_ACTION_CAT_SELF_PROT,
2452 			    IEEE80211_ACTION_MESHPEERING_CLOSE,
2453 			    args);
2454 			mesh_linkchange(ni, IEEE80211_NODE_MESH_HOLDING);
2455 			mesh_peer_timeout_setup(ni);
2456 		}
2457 		break;
2458 	default:
2459 		IEEE80211_DISCARD(vap,
2460 		    IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH,
2461 		    wh, NULL, "received confirm in invalid state %d",
2462 		    ni->ni_mlstate);
2463 		vap->iv_stats.is_rx_mgtdiscard++;
2464 		break;
2465 	}
2466 	return 0;
2467 }
2468 
2469 static int
2470 mesh_recv_action_meshpeering_close(struct ieee80211_node *ni,
2471 	const struct ieee80211_frame *wh,
2472 	const uint8_t *frm, const uint8_t *efrm)
2473 {
2474 	struct ieee80211_meshpeer_ie ie;
2475 	const struct ieee80211_meshpeer_ie *meshpeer;
2476 	uint16_t args[3];
2477 
2478 	/* +2 for action + code */
2479 	meshpeer = mesh_parse_meshpeering_action(ni, wh, frm+2, efrm, &ie,
2480 	    IEEE80211_ACTION_MESHPEERING_CLOSE);
2481 	if (meshpeer == NULL) {
2482 		return 0;
2483 	}
2484 
2485 	/*
2486 	 * XXX: check reason code, for example we could receive
2487 	 * IEEE80211_REASON_MESH_MAX_PEERS then we should not attempt
2488 	 * to peer again.
2489 	 */
2490 
2491 	IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH,
2492 	    ni, "%s", "recv PEER CLOSE");
2493 
2494 	switch (ni->ni_mlstate) {
2495 	case IEEE80211_NODE_MESH_IDLE:
2496 		/* ignore */
2497 		break;
2498 	case IEEE80211_NODE_MESH_OPENRCV:
2499 	case IEEE80211_NODE_MESH_OPENSNT:
2500 	case IEEE80211_NODE_MESH_CONFIRMRCV:
2501 	case IEEE80211_NODE_MESH_ESTABLISHED:
2502 		args[0] = ni->ni_mlpid;
2503 		args[1] = ni->ni_mllid;
2504 		args[2] = IEEE80211_REASON_MESH_CLOSE_RCVD;
2505 		ieee80211_send_action(ni,
2506 		    IEEE80211_ACTION_CAT_SELF_PROT,
2507 		    IEEE80211_ACTION_MESHPEERING_CLOSE,
2508 		    args);
2509 		mesh_linkchange(ni, IEEE80211_NODE_MESH_HOLDING);
2510 		mesh_peer_timeout_setup(ni);
2511 		break;
2512 	case IEEE80211_NODE_MESH_HOLDING:
2513 		mesh_linkchange(ni, IEEE80211_NODE_MESH_IDLE);
2514 		mesh_peer_timeout_stop(ni);
2515 		break;
2516 	}
2517 	return 0;
2518 }
2519 
2520 /*
2521  * Link Metric handling.
2522  */
2523 static int
2524 mesh_recv_action_meshlmetric(struct ieee80211_node *ni,
2525 	const struct ieee80211_frame *wh,
2526 	const uint8_t *frm, const uint8_t *efrm)
2527 {
2528 	const struct ieee80211_meshlmetric_ie *ie =
2529 	    (const struct ieee80211_meshlmetric_ie *)
2530 	    (frm+2); /* action + code */
2531 	struct ieee80211_meshlmetric_ie lm_rep;
2532 
2533 	if (ie->lm_flags & IEEE80211_MESH_LMETRIC_FLAGS_REQ) {
2534 		lm_rep.lm_flags = 0;
2535 		lm_rep.lm_metric = mesh_airtime_calc(ni);
2536 		ieee80211_send_action(ni,
2537 		    IEEE80211_ACTION_CAT_MESH,
2538 		    IEEE80211_ACTION_MESH_LMETRIC,
2539 		    &lm_rep);
2540 	}
2541 	/* XXX: else do nothing for now */
2542 	return 0;
2543 }
2544 
2545 /*
2546  * Parse meshgate action ie's for GANN frames.
2547  * Returns -1 if parsing fails, otherwise 0.
2548  */
2549 static int
2550 mesh_parse_meshgate_action(struct ieee80211_node *ni,
2551     const struct ieee80211_frame *wh,	/* XXX for VERIFY_LENGTH */
2552     struct ieee80211_meshgann_ie *ie, const uint8_t *frm, const uint8_t *efrm)
2553 {
2554 	struct ieee80211vap *vap = ni->ni_vap;
2555 	const struct ieee80211_meshgann_ie *gannie;
2556 
2557 	while (efrm - frm > 1) {
2558 		IEEE80211_VERIFY_LENGTH(efrm - frm, frm[1] + 2, return -1);
2559 		switch (*frm) {
2560 		case IEEE80211_ELEMID_MESHGANN:
2561 			gannie = (const struct ieee80211_meshgann_ie *) frm;
2562 			memset(ie, 0, sizeof(*ie));
2563 			ie->gann_ie = gannie->gann_ie;
2564 			ie->gann_len = gannie->gann_len;
2565 			ie->gann_flags = gannie->gann_flags;
2566 			ie->gann_hopcount = gannie->gann_hopcount;
2567 			ie->gann_ttl = gannie->gann_ttl;
2568 			IEEE80211_ADDR_COPY(ie->gann_addr, gannie->gann_addr);
2569 			ie->gann_seq = LE_READ_4(&gannie->gann_seq);
2570 			ie->gann_interval = LE_READ_2(&gannie->gann_interval);
2571 			break;
2572 		}
2573 		frm += frm[1] + 2;
2574 	}
2575 
2576 	return 0;
2577 }
2578 
2579 /*
2580  * Mesh Gate Announcement handling.
2581  */
2582 static int
2583 mesh_recv_action_meshgate(struct ieee80211_node *ni,
2584 	const struct ieee80211_frame *wh,
2585 	const uint8_t *frm, const uint8_t *efrm)
2586 {
2587 	struct ieee80211vap *vap = ni->ni_vap;
2588 	struct ieee80211_mesh_state *ms = vap->iv_mesh;
2589 	struct ieee80211_mesh_gate_route *gr, *next;
2590 	struct ieee80211_mesh_route *rt_gate;
2591 	struct ieee80211_meshgann_ie pgann;
2592 	struct ieee80211_meshgann_ie ie;
2593 	int found = 0;
2594 
2595 	/* +2 for action + code */
2596 	if (mesh_parse_meshgate_action(ni, wh, &ie, frm+2, efrm) != 0) {
2597 		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_MESH,
2598 		    ni->ni_macaddr, NULL, "%s",
2599 		    "GANN parsing failed");
2600 		vap->iv_stats.is_rx_mgtdiscard++;
2601 		return (0);
2602 	}
2603 
2604 	if (IEEE80211_ADDR_EQ(vap->iv_myaddr, ie.gann_addr))
2605 		return 0;
2606 
2607 	IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH, ni->ni_macaddr,
2608 	    "received GANN, meshgate: %6D (seq %u)", ie.gann_addr, ":",
2609 	    ie.gann_seq);
2610 
2611 	if (ms == NULL)
2612 		return (0);
2613 	MESH_RT_LOCK(ms);
2614 	TAILQ_FOREACH_SAFE(gr, &ms->ms_known_gates, gr_next, next) {
2615 		if (!IEEE80211_ADDR_EQ(gr->gr_addr, ie.gann_addr))
2616 			continue;
2617 		if (ie.gann_seq <= gr->gr_lastseq) {
2618 			IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_MESH,
2619 			    ni->ni_macaddr, NULL,
2620 			    "GANN old seqno %u <= %u",
2621 			    ie.gann_seq, gr->gr_lastseq);
2622 			MESH_RT_UNLOCK(ms);
2623 			return (0);
2624 		}
2625 		/* corresponding mesh gate found & GANN accepted */
2626 		found = 1;
2627 		break;
2628 
2629 	}
2630 	if (found == 0) {
2631 		/* this GANN is from a new mesh Gate add it to known table. */
2632 		IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH, ie.gann_addr,
2633 		    "stored new GANN information, seq %u.", ie.gann_seq);
2634 		gr = IEEE80211_MALLOC(ALIGN(sizeof(struct ieee80211_mesh_gate_route)),
2635 		    M_80211_MESH_GT_RT,
2636 		    IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
2637 		IEEE80211_ADDR_COPY(gr->gr_addr, ie.gann_addr);
2638 		TAILQ_INSERT_TAIL(&ms->ms_known_gates, gr, gr_next);
2639 	}
2640 	gr->gr_lastseq = ie.gann_seq;
2641 
2642 	/* check if we have a path to this gate */
2643 	rt_gate = mesh_rt_find_locked(ms, gr->gr_addr);
2644 	if (rt_gate != NULL &&
2645 	    rt_gate->rt_flags & IEEE80211_MESHRT_FLAGS_VALID) {
2646 		gr->gr_route = rt_gate;
2647 		rt_gate->rt_flags |= IEEE80211_MESHRT_FLAGS_GATE;
2648 	}
2649 
2650 	MESH_RT_UNLOCK(ms);
2651 
2652 	/* popagate only if decremented ttl >= 1 && forwarding is enabled */
2653 	if ((ie.gann_ttl - 1) < 1 && !(ms->ms_flags & IEEE80211_MESHFLAGS_FWD))
2654 		return 0;
2655 		pgann.gann_flags = ie.gann_flags; /* Reserved */
2656 	pgann.gann_hopcount = ie.gann_hopcount + 1;
2657 	pgann.gann_ttl = ie.gann_ttl - 1;
2658 	IEEE80211_ADDR_COPY(pgann.gann_addr, ie.gann_addr);
2659 	pgann.gann_seq = ie.gann_seq;
2660 	pgann.gann_interval = ie.gann_interval;
2661 
2662 	IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH, ie.gann_addr,
2663 	    "%s", "propagate GANN");
2664 
2665 	ieee80211_send_action(vap->iv_bss, IEEE80211_ACTION_CAT_MESH,
2666 	    IEEE80211_ACTION_MESH_GANN, &pgann);
2667 
2668 	return 0;
2669 }
2670 
2671 static int
2672 mesh_send_action(struct ieee80211_node *ni,
2673     const uint8_t sa[IEEE80211_ADDR_LEN],
2674     const uint8_t da[IEEE80211_ADDR_LEN],
2675     struct mbuf *m)
2676 {
2677 	struct ieee80211vap *vap = ni->ni_vap;
2678 	struct ieee80211com *ic = ni->ni_ic;
2679 	struct ieee80211_bpf_params params;
2680 	struct ieee80211_frame *wh;
2681 	int ret;
2682 
2683 	KASSERT(ni != NULL, ("null node"));
2684 
2685 	if (vap->iv_state == IEEE80211_S_CAC) {
2686 		IEEE80211_NOTE(vap, IEEE80211_MSG_OUTPUT, ni,
2687 		    "block %s frame in CAC state", "Mesh action");
2688 		vap->iv_stats.is_tx_badstate++;
2689 		ieee80211_free_node(ni);
2690 		m_freem(m);
2691 		return EIO;		/* XXX */
2692 	}
2693 
2694 	M_PREPEND(m, sizeof(struct ieee80211_frame), M_NOWAIT);
2695 	if (m == NULL) {
2696 		ieee80211_free_node(ni);
2697 		return ENOMEM;
2698 	}
2699 
2700 	IEEE80211_TX_LOCK(ic);
2701 	wh = mtod(m, struct ieee80211_frame *);
2702 	ieee80211_send_setup(ni, m,
2703 	     IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_ACTION,
2704 	     IEEE80211_NONQOS_TID, sa, da, sa);
2705 	m->m_flags |= M_ENCAP;		/* mark encapsulated */
2706 
2707 	memset(&params, 0, sizeof(params));
2708 	params.ibp_pri = WME_AC_VO;
2709 	params.ibp_rate0 = ni->ni_txparms->mgmtrate;
2710 	if (IEEE80211_IS_MULTICAST(da))
2711 		params.ibp_try0 = 1;
2712 	else
2713 		params.ibp_try0 = ni->ni_txparms->maxretry;
2714 	params.ibp_power = ni->ni_txpower;
2715 
2716 	IEEE80211_NODE_STAT(ni, tx_mgmt);
2717 
2718 	ret = ieee80211_raw_output(vap, ni, m, &params);
2719 	IEEE80211_TX_UNLOCK(ic);
2720 	return (ret);
2721 }
2722 
2723 #define	ADDSHORT(frm, v) do {			\
2724 	frm[0] = (v) & 0xff;			\
2725 	frm[1] = (v) >> 8;			\
2726 	frm += 2;				\
2727 } while (0)
2728 #define	ADDWORD(frm, v) do {			\
2729 	frm[0] = (v) & 0xff;			\
2730 	frm[1] = ((v) >> 8) & 0xff;		\
2731 	frm[2] = ((v) >> 16) & 0xff;		\
2732 	frm[3] = ((v) >> 24) & 0xff;		\
2733 	frm += 4;				\
2734 } while (0)
2735 
2736 static int
2737 mesh_send_action_meshpeering_open(struct ieee80211_node *ni,
2738 	int category, int action, void *args0)
2739 {
2740 	struct ieee80211vap *vap = ni->ni_vap;
2741 	struct ieee80211com *ic = ni->ni_ic;
2742 	uint16_t *args = args0;
2743 	const struct ieee80211_rateset *rs;
2744 	struct mbuf *m;
2745 	uint8_t *frm;
2746 
2747 	IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH, ni,
2748 	    "send PEER OPEN action: localid 0x%x", args[0]);
2749 
2750 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
2751 	    "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", __func__, __LINE__,
2752 	    ni, ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)+1);
2753 	ieee80211_ref_node(ni);
2754 
2755 	m = ieee80211_getmgtframe(&frm,
2756 	    ic->ic_headroom + sizeof(struct ieee80211_frame),
2757 	    sizeof(uint16_t)	/* action+category */
2758 	    + sizeof(uint16_t)	/* capabilites */
2759 	    + 2 + IEEE80211_RATE_SIZE
2760 	    + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
2761 	    + 2 + IEEE80211_MESHID_LEN
2762 	    + sizeof(struct ieee80211_meshconf_ie)
2763 	    + sizeof(struct ieee80211_meshpeer_ie)
2764 	);
2765 	if (m != NULL) {
2766 		/*
2767 		 * mesh peer open action frame format:
2768 		 *   [1] category
2769 		 *   [1] action
2770 		 *   [2] capabilities
2771 		 *   [tlv] rates
2772 		 *   [tlv] xrates
2773 		 *   [tlv] mesh id
2774 		 *   [tlv] mesh conf
2775 		 *   [tlv] mesh peer link mgmt
2776 		 */
2777 		*frm++ = category;
2778 		*frm++ = action;
2779 		ADDSHORT(frm, ieee80211_getcapinfo(vap, ni->ni_chan));
2780 		rs = ieee80211_get_suprates(ic, ic->ic_curchan);
2781 		frm = ieee80211_add_rates(frm, rs);
2782 		frm = ieee80211_add_xrates(frm, rs);
2783 		frm = ieee80211_add_meshid(frm, vap);
2784 		frm = ieee80211_add_meshconf(frm, vap);
2785 		frm = ieee80211_add_meshpeer(frm, IEEE80211_ACTION_MESHPEERING_OPEN,
2786 		    args[0], 0, 0);
2787 		m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2788 		return mesh_send_action(ni, vap->iv_myaddr, ni->ni_macaddr, m);
2789 	} else {
2790 		vap->iv_stats.is_tx_nobuf++;
2791 		ieee80211_free_node(ni);
2792 		return ENOMEM;
2793 	}
2794 }
2795 
2796 static int
2797 mesh_send_action_meshpeering_confirm(struct ieee80211_node *ni,
2798 	int category, int action, void *args0)
2799 {
2800 	struct ieee80211vap *vap = ni->ni_vap;
2801 	struct ieee80211com *ic = ni->ni_ic;
2802 	uint16_t *args = args0;
2803 	const struct ieee80211_rateset *rs;
2804 	struct mbuf *m;
2805 	uint8_t *frm;
2806 
2807 	IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH, ni,
2808 	    "send PEER CONFIRM action: localid 0x%x, peerid 0x%x",
2809 	    args[0], args[1]);
2810 
2811 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
2812 	    "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", __func__, __LINE__,
2813 	    ni, ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)+1);
2814 	ieee80211_ref_node(ni);
2815 
2816 	m = ieee80211_getmgtframe(&frm,
2817 	    ic->ic_headroom + sizeof(struct ieee80211_frame),
2818 	    sizeof(uint16_t)	/* action+category */
2819 	    + sizeof(uint16_t)	/* capabilites */
2820 	    + sizeof(uint16_t)	/* status code */
2821 	    + sizeof(uint16_t)	/* AID */
2822 	    + 2 + IEEE80211_RATE_SIZE
2823 	    + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)
2824 	    + 2 + IEEE80211_MESHID_LEN
2825 	    + sizeof(struct ieee80211_meshconf_ie)
2826 	    + sizeof(struct ieee80211_meshpeer_ie)
2827 	);
2828 	if (m != NULL) {
2829 		/*
2830 		 * mesh peer confirm action frame format:
2831 		 *   [1] category
2832 		 *   [1] action
2833 		 *   [2] capabilities
2834 		 *   [2] status code
2835 		 *   [2] association id (peer ID)
2836 		 *   [tlv] rates
2837 		 *   [tlv] xrates
2838 		 *   [tlv] mesh id
2839 		 *   [tlv] mesh conf
2840 		 *   [tlv] mesh peer link mgmt
2841 		 */
2842 		*frm++ = category;
2843 		*frm++ = action;
2844 		ADDSHORT(frm, ieee80211_getcapinfo(vap, ni->ni_chan));
2845 		ADDSHORT(frm, 0);		/* status code */
2846 		ADDSHORT(frm, args[1]);		/* AID */
2847 		rs = ieee80211_get_suprates(ic, ic->ic_curchan);
2848 		frm = ieee80211_add_rates(frm, rs);
2849 		frm = ieee80211_add_xrates(frm, rs);
2850 		frm = ieee80211_add_meshid(frm, vap);
2851 		frm = ieee80211_add_meshconf(frm, vap);
2852 		frm = ieee80211_add_meshpeer(frm,
2853 		    IEEE80211_ACTION_MESHPEERING_CONFIRM,
2854 		    args[0], args[1], 0);
2855 		m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2856 		return mesh_send_action(ni, vap->iv_myaddr, ni->ni_macaddr, m);
2857 	} else {
2858 		vap->iv_stats.is_tx_nobuf++;
2859 		ieee80211_free_node(ni);
2860 		return ENOMEM;
2861 	}
2862 }
2863 
2864 static int
2865 mesh_send_action_meshpeering_close(struct ieee80211_node *ni,
2866 	int category, int action, void *args0)
2867 {
2868 	struct ieee80211vap *vap = ni->ni_vap;
2869 	struct ieee80211com *ic = ni->ni_ic;
2870 	uint16_t *args = args0;
2871 	struct mbuf *m;
2872 	uint8_t *frm;
2873 
2874 	IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH, ni,
2875 	    "send PEER CLOSE action: localid 0x%x, peerid 0x%x reason %d",
2876 	    args[0], args[1], args[2]);
2877 
2878 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
2879 	    "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", __func__, __LINE__,
2880 	    ni, ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)+1);
2881 	ieee80211_ref_node(ni);
2882 
2883 	m = ieee80211_getmgtframe(&frm,
2884 	    ic->ic_headroom + sizeof(struct ieee80211_frame),
2885 	    sizeof(uint16_t)	/* action+category */
2886 	    + sizeof(uint16_t)	/* reason code */
2887 	    + 2 + IEEE80211_MESHID_LEN
2888 	    + sizeof(struct ieee80211_meshpeer_ie)
2889 	);
2890 	if (m != NULL) {
2891 		/*
2892 		 * mesh peer close action frame format:
2893 		 *   [1] category
2894 		 *   [1] action
2895 		 *   [tlv] mesh id
2896 		 *   [tlv] mesh peer link mgmt
2897 		 */
2898 		*frm++ = category;
2899 		*frm++ = action;
2900 		frm = ieee80211_add_meshid(frm, vap);
2901 		frm = ieee80211_add_meshpeer(frm,
2902 		    IEEE80211_ACTION_MESHPEERING_CLOSE,
2903 		    args[0], args[1], args[2]);
2904 		m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2905 		return mesh_send_action(ni, vap->iv_myaddr, ni->ni_macaddr, m);
2906 	} else {
2907 		vap->iv_stats.is_tx_nobuf++;
2908 		ieee80211_free_node(ni);
2909 		return ENOMEM;
2910 	}
2911 }
2912 
2913 static int
2914 mesh_send_action_meshlmetric(struct ieee80211_node *ni,
2915 	int category, int action, void *arg0)
2916 {
2917 	struct ieee80211vap *vap = ni->ni_vap;
2918 	struct ieee80211com *ic = ni->ni_ic;
2919 	struct ieee80211_meshlmetric_ie *ie = arg0;
2920 	struct mbuf *m;
2921 	uint8_t *frm;
2922 
2923 	if (ie->lm_flags & IEEE80211_MESH_LMETRIC_FLAGS_REQ) {
2924 		IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH,
2925 		    ni, "%s", "send LINK METRIC REQUEST action");
2926 	} else {
2927 		IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH,
2928 		    ni, "send LINK METRIC REPLY action: metric 0x%x",
2929 		    ie->lm_metric);
2930 	}
2931 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
2932 	    "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", __func__, __LINE__,
2933 	    ni, ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)+1);
2934 	ieee80211_ref_node(ni);
2935 
2936 	m = ieee80211_getmgtframe(&frm,
2937 	    ic->ic_headroom + sizeof(struct ieee80211_frame),
2938 	    sizeof(uint16_t) +	/* action+category */
2939 	    sizeof(struct ieee80211_meshlmetric_ie)
2940 	);
2941 	if (m != NULL) {
2942 		/*
2943 		 * mesh link metric
2944 		 *   [1] category
2945 		 *   [1] action
2946 		 *   [tlv] mesh link metric
2947 		 */
2948 		*frm++ = category;
2949 		*frm++ = action;
2950 		frm = ieee80211_add_meshlmetric(frm,
2951 		    ie->lm_flags, ie->lm_metric);
2952 		m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2953 		return mesh_send_action(ni, vap->iv_myaddr, ni->ni_macaddr, m);
2954 	} else {
2955 		vap->iv_stats.is_tx_nobuf++;
2956 		ieee80211_free_node(ni);
2957 		return ENOMEM;
2958 	}
2959 }
2960 
2961 static int
2962 mesh_send_action_meshgate(struct ieee80211_node *ni,
2963 	int category, int action, void *arg0)
2964 {
2965 	struct ieee80211vap *vap = ni->ni_vap;
2966 	struct ieee80211com *ic = ni->ni_ic;
2967 	struct ieee80211_meshgann_ie *ie = arg0;
2968 	struct mbuf *m;
2969 	uint8_t *frm;
2970 
2971 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
2972 	    "ieee80211_ref_node (%s:%u) %p<%s> refcnt %d\n", __func__, __LINE__,
2973 	    ni, ether_sprintf(ni->ni_macaddr), ieee80211_node_refcnt(ni)+1);
2974 	ieee80211_ref_node(ni);
2975 
2976 	m = ieee80211_getmgtframe(&frm,
2977 	    ic->ic_headroom + sizeof(struct ieee80211_frame),
2978 	    sizeof(uint16_t) +	/* action+category */
2979 	    IEEE80211_MESHGANN_BASE_SZ
2980 	);
2981 	if (m != NULL) {
2982 		/*
2983 		 * mesh link metric
2984 		 *   [1] category
2985 		 *   [1] action
2986 		 *   [tlv] mesh gate annoucement
2987 		 */
2988 		*frm++ = category;
2989 		*frm++ = action;
2990 		frm = ieee80211_add_meshgate(frm, ie);
2991 		m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2992 		return mesh_send_action(ni, vap->iv_myaddr, broadcastaddr, m);
2993 	} else {
2994 		vap->iv_stats.is_tx_nobuf++;
2995 		ieee80211_free_node(ni);
2996 		return ENOMEM;
2997 	}
2998 }
2999 
3000 static void
3001 mesh_peer_timeout_setup(struct ieee80211_node *ni)
3002 {
3003 	switch (ni->ni_mlstate) {
3004 	case IEEE80211_NODE_MESH_HOLDING:
3005 		ni->ni_mltval = ieee80211_mesh_holdingtimeout;
3006 		break;
3007 	case IEEE80211_NODE_MESH_CONFIRMRCV:
3008 		ni->ni_mltval = ieee80211_mesh_confirmtimeout;
3009 		break;
3010 	case IEEE80211_NODE_MESH_IDLE:
3011 		ni->ni_mltval = 0;
3012 		break;
3013 	default:
3014 		ni->ni_mltval = ieee80211_mesh_retrytimeout;
3015 		break;
3016 	}
3017 	if (ni->ni_mltval)
3018 		callout_reset(&ni->ni_mltimer, ni->ni_mltval,
3019 		    mesh_peer_timeout_cb, ni);
3020 }
3021 
3022 /*
3023  * Same as above but backoffs timer statisically 50%.
3024  */
3025 static void
3026 mesh_peer_timeout_backoff(struct ieee80211_node *ni)
3027 {
3028 	uint32_t r;
3029 
3030 	r = arc4random();
3031 	ni->ni_mltval += r % ni->ni_mltval;
3032 	callout_reset(&ni->ni_mltimer, ni->ni_mltval, mesh_peer_timeout_cb,
3033 	    ni);
3034 }
3035 
3036 static __inline void
3037 mesh_peer_timeout_stop(struct ieee80211_node *ni)
3038 {
3039 	callout_drain(&ni->ni_mltimer);
3040 }
3041 
3042 static void
3043 mesh_peer_backoff_cb(void *arg)
3044 {
3045 	struct ieee80211_node *ni = (struct ieee80211_node *)arg;
3046 
3047 	/* After backoff timeout, try to peer automatically again. */
3048 	ni->ni_mlhcnt = 0;
3049 }
3050 
3051 /*
3052  * Mesh Peer Link Management FSM timeout handling.
3053  */
3054 static void
3055 mesh_peer_timeout_cb(void *arg)
3056 {
3057 	struct ieee80211_node *ni = (struct ieee80211_node *)arg;
3058 	uint16_t args[3];
3059 
3060 	IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_MESH,
3061 	    ni, "mesh link timeout, state %d, retry counter %d",
3062 	    ni->ni_mlstate, ni->ni_mlrcnt);
3063 
3064 	switch (ni->ni_mlstate) {
3065 	case IEEE80211_NODE_MESH_IDLE:
3066 	case IEEE80211_NODE_MESH_ESTABLISHED:
3067 		break;
3068 	case IEEE80211_NODE_MESH_OPENSNT:
3069 	case IEEE80211_NODE_MESH_OPENRCV:
3070 		if (ni->ni_mlrcnt == ieee80211_mesh_maxretries) {
3071 			args[0] = ni->ni_mlpid;
3072 			args[2] = IEEE80211_REASON_MESH_MAX_RETRIES;
3073 			ieee80211_send_action(ni,
3074 			    IEEE80211_ACTION_CAT_SELF_PROT,
3075 			    IEEE80211_ACTION_MESHPEERING_CLOSE, args);
3076 			ni->ni_mlrcnt = 0;
3077 			mesh_linkchange(ni, IEEE80211_NODE_MESH_HOLDING);
3078 			mesh_peer_timeout_setup(ni);
3079 		} else {
3080 			args[0] = ni->ni_mlpid;
3081 			ieee80211_send_action(ni,
3082 			    IEEE80211_ACTION_CAT_SELF_PROT,
3083 			    IEEE80211_ACTION_MESHPEERING_OPEN, args);
3084 			ni->ni_mlrcnt++;
3085 			mesh_peer_timeout_backoff(ni);
3086 		}
3087 		break;
3088 	case IEEE80211_NODE_MESH_CONFIRMRCV:
3089 		args[0] = ni->ni_mlpid;
3090 		args[2] = IEEE80211_REASON_MESH_CONFIRM_TIMEOUT;
3091 		ieee80211_send_action(ni,
3092 		    IEEE80211_ACTION_CAT_SELF_PROT,
3093 		    IEEE80211_ACTION_MESHPEERING_CLOSE, args);
3094 		mesh_linkchange(ni, IEEE80211_NODE_MESH_HOLDING);
3095 		mesh_peer_timeout_setup(ni);
3096 		break;
3097 	case IEEE80211_NODE_MESH_HOLDING:
3098 		ni->ni_mlhcnt++;
3099 		if (ni->ni_mlhcnt >= ieee80211_mesh_maxholding)
3100 			callout_reset(&ni->ni_mlhtimer,
3101 			    ieee80211_mesh_backofftimeout,
3102 			    mesh_peer_backoff_cb, ni);
3103 		mesh_linkchange(ni, IEEE80211_NODE_MESH_IDLE);
3104 		break;
3105 	}
3106 }
3107 
3108 static int
3109 mesh_verify_meshid(struct ieee80211vap *vap, const uint8_t *ie)
3110 {
3111 	struct ieee80211_mesh_state *ms = vap->iv_mesh;
3112 
3113 	if (ie == NULL || ie[1] != ms->ms_idlen)
3114 		return 1;
3115 	return memcmp(ms->ms_id, ie + 2, ms->ms_idlen);
3116 }
3117 
3118 /*
3119  * Check if we are using the same algorithms for this mesh.
3120  */
3121 static int
3122 mesh_verify_meshconf(struct ieee80211vap *vap, const uint8_t *ie)
3123 {
3124 	const struct ieee80211_meshconf_ie *meshconf =
3125 	    (const struct ieee80211_meshconf_ie *) ie;
3126 	const struct ieee80211_mesh_state *ms = vap->iv_mesh;
3127 
3128 	if (meshconf == NULL)
3129 		return 1;
3130 	if (meshconf->conf_pselid != ms->ms_ppath->mpp_ie) {
3131 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_MESH,
3132 		    "unknown path selection algorithm: 0x%x\n",
3133 		    meshconf->conf_pselid);
3134 		return 1;
3135 	}
3136 	if (meshconf->conf_pmetid != ms->ms_pmetric->mpm_ie) {
3137 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_MESH,
3138 		    "unknown path metric algorithm: 0x%x\n",
3139 		    meshconf->conf_pmetid);
3140 		return 1;
3141 	}
3142 	if (meshconf->conf_ccid != 0) {
3143 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_MESH,
3144 		    "unknown congestion control algorithm: 0x%x\n",
3145 		    meshconf->conf_ccid);
3146 		return 1;
3147 	}
3148 	if (meshconf->conf_syncid != IEEE80211_MESHCONF_SYNC_NEIGHOFF) {
3149 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_MESH,
3150 		    "unknown sync algorithm: 0x%x\n",
3151 		    meshconf->conf_syncid);
3152 		return 1;
3153 	}
3154 	if (meshconf->conf_authid != 0) {
3155 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_MESH,
3156 		    "unknown auth auth algorithm: 0x%x\n",
3157 		    meshconf->conf_pselid);
3158 		return 1;
3159 	}
3160 	/* Not accepting peers */
3161 	if (!(meshconf->conf_cap & IEEE80211_MESHCONF_CAP_AP)) {
3162 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_MESH,
3163 		    "not accepting peers: 0x%x\n", meshconf->conf_cap);
3164 		return 1;
3165 	}
3166 	return 0;
3167 }
3168 
3169 static int
3170 mesh_verify_meshpeer(struct ieee80211vap *vap, uint8_t subtype,
3171     const uint8_t *ie)
3172 {
3173 	const struct ieee80211_meshpeer_ie *meshpeer =
3174 	    (const struct ieee80211_meshpeer_ie *) ie;
3175 
3176 	if (meshpeer == NULL ||
3177 	    meshpeer->peer_len < IEEE80211_MPM_BASE_SZ ||
3178 	    meshpeer->peer_len > IEEE80211_MPM_MAX_SZ)
3179 		return 1;
3180 	if (meshpeer->peer_proto != IEEE80211_MPPID_MPM) {
3181 		IEEE80211_DPRINTF(vap,
3182 		    IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH,
3183 		    "Only MPM protocol is supported (proto: 0x%02X)",
3184 		    meshpeer->peer_proto);
3185 		return 1;
3186 	}
3187 	switch (subtype) {
3188 	case IEEE80211_ACTION_MESHPEERING_OPEN:
3189 		if (meshpeer->peer_len != IEEE80211_MPM_BASE_SZ)
3190 			return 1;
3191 		break;
3192 	case IEEE80211_ACTION_MESHPEERING_CONFIRM:
3193 		if (meshpeer->peer_len != IEEE80211_MPM_BASE_SZ + 2)
3194 			return 1;
3195 		break;
3196 	case IEEE80211_ACTION_MESHPEERING_CLOSE:
3197 		if (meshpeer->peer_len < IEEE80211_MPM_BASE_SZ + 2)
3198 			return 1;
3199 		if (meshpeer->peer_len == (IEEE80211_MPM_BASE_SZ + 2) &&
3200 		    meshpeer->peer_linkid != 0)
3201 			return 1;
3202 		if (meshpeer->peer_rcode == 0)
3203 			return 1;
3204 		break;
3205 	}
3206 	return 0;
3207 }
3208 
3209 /*
3210  * Add a Mesh ID IE to a frame.
3211  */
3212 uint8_t *
3213 ieee80211_add_meshid(uint8_t *frm, struct ieee80211vap *vap)
3214 {
3215 	struct ieee80211_mesh_state *ms = vap->iv_mesh;
3216 
3217 	KASSERT(vap->iv_opmode == IEEE80211_M_MBSS, ("not a mbss vap"));
3218 
3219 	*frm++ = IEEE80211_ELEMID_MESHID;
3220 	*frm++ = ms->ms_idlen;
3221 	memcpy(frm, ms->ms_id, ms->ms_idlen);
3222 	return frm + ms->ms_idlen;
3223 }
3224 
3225 /*
3226  * Add a Mesh Configuration IE to a frame.
3227  * For now just use HWMP routing, Airtime link metric, Null Congestion
3228  * Signaling, Null Sync Protocol and Null Authentication.
3229  */
3230 uint8_t *
3231 ieee80211_add_meshconf(uint8_t *frm, struct ieee80211vap *vap)
3232 {
3233 	const struct ieee80211_mesh_state *ms = vap->iv_mesh;
3234 	uint16_t caps;
3235 
3236 	KASSERT(vap->iv_opmode == IEEE80211_M_MBSS, ("not a MBSS vap"));
3237 
3238 	*frm++ = IEEE80211_ELEMID_MESHCONF;
3239 	*frm++ = IEEE80211_MESH_CONF_SZ;
3240 	*frm++ = ms->ms_ppath->mpp_ie;		/* path selection */
3241 	*frm++ = ms->ms_pmetric->mpm_ie;	/* link metric */
3242 	*frm++ = IEEE80211_MESHCONF_CC_DISABLED;
3243 	*frm++ = IEEE80211_MESHCONF_SYNC_NEIGHOFF;
3244 	*frm++ = IEEE80211_MESHCONF_AUTH_DISABLED;
3245 	/* NB: set the number of neighbors before the rest */
3246 	*frm = (ms->ms_neighbors > IEEE80211_MESH_MAX_NEIGHBORS ?
3247 	    IEEE80211_MESH_MAX_NEIGHBORS : ms->ms_neighbors) << 1;
3248 	if (ms->ms_flags & IEEE80211_MESHFLAGS_GATE)
3249 		*frm |= IEEE80211_MESHCONF_FORM_GATE;
3250 	frm += 1;
3251 	caps = 0;
3252 	if (ms->ms_flags & IEEE80211_MESHFLAGS_AP)
3253 		caps |= IEEE80211_MESHCONF_CAP_AP;
3254 	if (ms->ms_flags & IEEE80211_MESHFLAGS_FWD)
3255 		caps |= IEEE80211_MESHCONF_CAP_FWRD;
3256 	*frm++ = caps;
3257 	return frm;
3258 }
3259 
3260 /*
3261  * Add a Mesh Peer Management IE to a frame.
3262  */
3263 uint8_t *
3264 ieee80211_add_meshpeer(uint8_t *frm, uint8_t subtype, uint16_t localid,
3265     uint16_t peerid, uint16_t reason)
3266 {
3267 
3268 	KASSERT(localid != 0, ("localid == 0"));
3269 
3270 	*frm++ = IEEE80211_ELEMID_MESHPEER;
3271 	switch (subtype) {
3272 	case IEEE80211_ACTION_MESHPEERING_OPEN:
3273 		*frm++ = IEEE80211_MPM_BASE_SZ;		/* length */
3274 		ADDSHORT(frm, IEEE80211_MPPID_MPM);	/* proto */
3275 		ADDSHORT(frm, localid);			/* local ID */
3276 		break;
3277 	case IEEE80211_ACTION_MESHPEERING_CONFIRM:
3278 		KASSERT(peerid != 0, ("sending peer confirm without peer id"));
3279 		*frm++ = IEEE80211_MPM_BASE_SZ + 2;	/* length */
3280 		ADDSHORT(frm, IEEE80211_MPPID_MPM);	/* proto */
3281 		ADDSHORT(frm, localid);			/* local ID */
3282 		ADDSHORT(frm, peerid);			/* peer ID */
3283 		break;
3284 	case IEEE80211_ACTION_MESHPEERING_CLOSE:
3285 		if (peerid)
3286 			*frm++ = IEEE80211_MPM_MAX_SZ;	/* length */
3287 		else
3288 			*frm++ = IEEE80211_MPM_BASE_SZ + 2; /* length */
3289 		ADDSHORT(frm, IEEE80211_MPPID_MPM);	/* proto */
3290 		ADDSHORT(frm, localid);	/* local ID */
3291 		if (peerid)
3292 			ADDSHORT(frm, peerid);	/* peer ID */
3293 		ADDSHORT(frm, reason);
3294 		break;
3295 	}
3296 	return frm;
3297 }
3298 
3299 /*
3300  * Compute an Airtime Link Metric for the link with this node.
3301  *
3302  * Based on Draft 3.0 spec (11B.10, p.149).
3303  */
3304 /*
3305  * Max 802.11s overhead.
3306  */
3307 #define IEEE80211_MESH_MAXOVERHEAD \
3308 	(sizeof(struct ieee80211_qosframe_addr4) \
3309 	 + sizeof(struct ieee80211_meshcntl_ae10) \
3310 	+ sizeof(struct llc) \
3311 	+ IEEE80211_ADDR_LEN \
3312 	+ IEEE80211_WEP_IVLEN \
3313 	+ IEEE80211_WEP_KIDLEN \
3314 	+ IEEE80211_WEP_CRCLEN \
3315 	+ IEEE80211_WEP_MICLEN \
3316 	+ IEEE80211_CRC_LEN)
3317 uint32_t
3318 mesh_airtime_calc(struct ieee80211_node *ni)
3319 {
3320 #define M_BITS 8
3321 #define S_FACTOR (2 * M_BITS)
3322 	struct ieee80211com *ic = ni->ni_ic;
3323 	struct ifnet *ifp = ni->ni_vap->iv_ifp;
3324 	const static int nbits = 8192 << M_BITS;
3325 	uint32_t overhead, rate, errrate;
3326 	uint64_t res;
3327 
3328 	/* Time to transmit a frame */
3329 	rate = ni->ni_txrate;
3330 	overhead = ieee80211_compute_duration(ic->ic_rt,
3331 	    ifp->if_mtu + IEEE80211_MESH_MAXOVERHEAD, rate, 0) << M_BITS;
3332 	/* Error rate in percentage */
3333 	/* XXX assuming small failures are ok */
3334 	errrate = (((ifp->if_get_counter(ifp, IFCOUNTER_OERRORS) +
3335 	    ifp->if_get_counter(ifp, IFCOUNTER_IERRORS)) / 100) << M_BITS)
3336 	    / 100;
3337 	res = (overhead + (nbits / rate)) *
3338 	    ((1 << S_FACTOR) / ((1 << M_BITS) - errrate));
3339 
3340 	return (uint32_t)(res >> S_FACTOR);
3341 #undef M_BITS
3342 #undef S_FACTOR
3343 }
3344 
3345 /*
3346  * Add a Mesh Link Metric report IE to a frame.
3347  */
3348 uint8_t *
3349 ieee80211_add_meshlmetric(uint8_t *frm, uint8_t flags, uint32_t metric)
3350 {
3351 	*frm++ = IEEE80211_ELEMID_MESHLINK;
3352 	*frm++ = 5;
3353 	*frm++ = flags;
3354 	ADDWORD(frm, metric);
3355 	return frm;
3356 }
3357 
3358 /*
3359  * Add a Mesh Gate Announcement IE to a frame.
3360  */
3361 uint8_t *
3362 ieee80211_add_meshgate(uint8_t *frm, struct ieee80211_meshgann_ie *ie)
3363 {
3364 	*frm++ = IEEE80211_ELEMID_MESHGANN; /* ie */
3365 	*frm++ = IEEE80211_MESHGANN_BASE_SZ; /* len */
3366 	*frm++ = ie->gann_flags;
3367 	*frm++ = ie->gann_hopcount;
3368 	*frm++ = ie->gann_ttl;
3369 	IEEE80211_ADDR_COPY(frm, ie->gann_addr);
3370 	frm += 6;
3371 	ADDWORD(frm, ie->gann_seq);
3372 	ADDSHORT(frm, ie->gann_interval);
3373 	return frm;
3374 }
3375 #undef ADDSHORT
3376 #undef ADDWORD
3377 
3378 /*
3379  * Initialize any mesh-specific node state.
3380  */
3381 void
3382 ieee80211_mesh_node_init(struct ieee80211vap *vap, struct ieee80211_node *ni)
3383 {
3384 	ni->ni_flags |= IEEE80211_NODE_QOS;
3385 	callout_init(&ni->ni_mltimer, 1);
3386 	callout_init(&ni->ni_mlhtimer, 1);
3387 }
3388 
3389 /*
3390  * Cleanup any mesh-specific node state.
3391  */
3392 void
3393 ieee80211_mesh_node_cleanup(struct ieee80211_node *ni)
3394 {
3395 	struct ieee80211vap *vap = ni->ni_vap;
3396 	struct ieee80211_mesh_state *ms = vap->iv_mesh;
3397 
3398 	callout_drain(&ni->ni_mltimer);
3399 	callout_drain(&ni->ni_mlhtimer);
3400 	/* NB: short-circuit callbacks after mesh_vdetach */
3401 	if (vap->iv_mesh != NULL)
3402 		ms->ms_ppath->mpp_peerdown(ni);
3403 }
3404 
3405 void
3406 ieee80211_parse_meshid(struct ieee80211_node *ni, const uint8_t *ie)
3407 {
3408 	ni->ni_meshidlen = ie[1];
3409 	memcpy(ni->ni_meshid, ie + 2, ie[1]);
3410 }
3411 
3412 /*
3413  * Setup mesh-specific node state on neighbor discovery.
3414  */
3415 void
3416 ieee80211_mesh_init_neighbor(struct ieee80211_node *ni,
3417 	const struct ieee80211_frame *wh,
3418 	const struct ieee80211_scanparams *sp)
3419 {
3420 	ieee80211_parse_meshid(ni, sp->meshid);
3421 }
3422 
3423 void
3424 ieee80211_mesh_update_beacon(struct ieee80211vap *vap,
3425 	struct ieee80211_beacon_offsets *bo)
3426 {
3427 	KASSERT(vap->iv_opmode == IEEE80211_M_MBSS, ("not a MBSS vap"));
3428 
3429 	if (isset(bo->bo_flags, IEEE80211_BEACON_MESHCONF)) {
3430 		(void)ieee80211_add_meshconf(bo->bo_meshconf, vap);
3431 		clrbit(bo->bo_flags, IEEE80211_BEACON_MESHCONF);
3432 	}
3433 }
3434 
3435 static int
3436 mesh_ioctl_get80211(struct ieee80211vap *vap, struct ieee80211req *ireq)
3437 {
3438 	struct ieee80211_mesh_state *ms = vap->iv_mesh;
3439 	uint8_t tmpmeshid[IEEE80211_NWID_LEN];
3440 	struct ieee80211_mesh_route *rt;
3441 	struct ieee80211req_mesh_route *imr;
3442 	size_t len, off;
3443 	uint8_t *p;
3444 	int error;
3445 
3446 	if (vap->iv_opmode != IEEE80211_M_MBSS)
3447 		return ENOSYS;
3448 
3449 	error = 0;
3450 	switch (ireq->i_type) {
3451 	case IEEE80211_IOC_MESH_ID:
3452 		ireq->i_len = ms->ms_idlen;
3453 		memcpy(tmpmeshid, ms->ms_id, ireq->i_len);
3454 		error = copyout(tmpmeshid, ireq->i_data, ireq->i_len);
3455 		break;
3456 	case IEEE80211_IOC_MESH_AP:
3457 		ireq->i_val = (ms->ms_flags & IEEE80211_MESHFLAGS_AP) != 0;
3458 		break;
3459 	case IEEE80211_IOC_MESH_FWRD:
3460 		ireq->i_val = (ms->ms_flags & IEEE80211_MESHFLAGS_FWD) != 0;
3461 		break;
3462 	case IEEE80211_IOC_MESH_GATE:
3463 		ireq->i_val = (ms->ms_flags & IEEE80211_MESHFLAGS_GATE) != 0;
3464 		break;
3465 	case IEEE80211_IOC_MESH_TTL:
3466 		ireq->i_val = ms->ms_ttl;
3467 		break;
3468 	case IEEE80211_IOC_MESH_RTCMD:
3469 		switch (ireq->i_val) {
3470 		case IEEE80211_MESH_RTCMD_LIST:
3471 			len = 0;
3472 			MESH_RT_LOCK(ms);
3473 			TAILQ_FOREACH(rt, &ms->ms_routes, rt_next) {
3474 				len += sizeof(*imr);
3475 			}
3476 			MESH_RT_UNLOCK(ms);
3477 			if (len > ireq->i_len || ireq->i_len < sizeof(*imr)) {
3478 				ireq->i_len = len;
3479 				return ENOMEM;
3480 			}
3481 			ireq->i_len = len;
3482 			/* XXX M_WAIT? */
3483 			p = IEEE80211_MALLOC(len, M_TEMP,
3484 			    IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
3485 			if (p == NULL)
3486 				return ENOMEM;
3487 			off = 0;
3488 			MESH_RT_LOCK(ms);
3489 			TAILQ_FOREACH(rt, &ms->ms_routes, rt_next) {
3490 				if (off >= len)
3491 					break;
3492 				imr = (struct ieee80211req_mesh_route *)
3493 				    (p + off);
3494 				IEEE80211_ADDR_COPY(imr->imr_dest,
3495 				    rt->rt_dest);
3496 				IEEE80211_ADDR_COPY(imr->imr_nexthop,
3497 				    rt->rt_nexthop);
3498 				imr->imr_metric = rt->rt_metric;
3499 				imr->imr_nhops = rt->rt_nhops;
3500 				imr->imr_lifetime =
3501 				    ieee80211_mesh_rt_update(rt, 0);
3502 				imr->imr_lastmseq = rt->rt_lastmseq;
3503 				imr->imr_flags = rt->rt_flags; /* last */
3504 				off += sizeof(*imr);
3505 			}
3506 			MESH_RT_UNLOCK(ms);
3507 			error = copyout(p, (uint8_t *)ireq->i_data,
3508 			    ireq->i_len);
3509 			IEEE80211_FREE(p, M_TEMP);
3510 			break;
3511 		case IEEE80211_MESH_RTCMD_FLUSH:
3512 		case IEEE80211_MESH_RTCMD_ADD:
3513 		case IEEE80211_MESH_RTCMD_DELETE:
3514 			return EINVAL;
3515 		default:
3516 			return ENOSYS;
3517 		}
3518 		break;
3519 	case IEEE80211_IOC_MESH_PR_METRIC:
3520 		len = strlen(ms->ms_pmetric->mpm_descr);
3521 		if (ireq->i_len < len)
3522 			return EINVAL;
3523 		ireq->i_len = len;
3524 		error = copyout(ms->ms_pmetric->mpm_descr,
3525 		    (uint8_t *)ireq->i_data, len);
3526 		break;
3527 	case IEEE80211_IOC_MESH_PR_PATH:
3528 		len = strlen(ms->ms_ppath->mpp_descr);
3529 		if (ireq->i_len < len)
3530 			return EINVAL;
3531 		ireq->i_len = len;
3532 		error = copyout(ms->ms_ppath->mpp_descr,
3533 		    (uint8_t *)ireq->i_data, len);
3534 		break;
3535 	default:
3536 		return ENOSYS;
3537 	}
3538 
3539 	return error;
3540 }
3541 IEEE80211_IOCTL_GET(mesh, mesh_ioctl_get80211);
3542 
3543 static int
3544 mesh_ioctl_set80211(struct ieee80211vap *vap, struct ieee80211req *ireq)
3545 {
3546 	struct ieee80211_mesh_state *ms = vap->iv_mesh;
3547 	uint8_t tmpmeshid[IEEE80211_NWID_LEN];
3548 	uint8_t tmpaddr[IEEE80211_ADDR_LEN];
3549 	char tmpproto[IEEE80211_MESH_PROTO_DSZ];
3550 	int error;
3551 
3552 	if (vap->iv_opmode != IEEE80211_M_MBSS)
3553 		return ENOSYS;
3554 
3555 	error = 0;
3556 	switch (ireq->i_type) {
3557 	case IEEE80211_IOC_MESH_ID:
3558 		if (ireq->i_val != 0 || ireq->i_len > IEEE80211_MESHID_LEN)
3559 			return EINVAL;
3560 		error = copyin(ireq->i_data, tmpmeshid, ireq->i_len);
3561 		if (error != 0)
3562 			break;
3563 		memset(ms->ms_id, 0, IEEE80211_NWID_LEN);
3564 		ms->ms_idlen = ireq->i_len;
3565 		memcpy(ms->ms_id, tmpmeshid, ireq->i_len);
3566 		error = ENETRESET;
3567 		break;
3568 	case IEEE80211_IOC_MESH_AP:
3569 		if (ireq->i_val)
3570 			ms->ms_flags |= IEEE80211_MESHFLAGS_AP;
3571 		else
3572 			ms->ms_flags &= ~IEEE80211_MESHFLAGS_AP;
3573 		error = ENETRESET;
3574 		break;
3575 	case IEEE80211_IOC_MESH_FWRD:
3576 		if (ireq->i_val)
3577 			ms->ms_flags |= IEEE80211_MESHFLAGS_FWD;
3578 		else
3579 			ms->ms_flags &= ~IEEE80211_MESHFLAGS_FWD;
3580 		mesh_gatemode_setup(vap);
3581 		break;
3582 	case IEEE80211_IOC_MESH_GATE:
3583 		if (ireq->i_val)
3584 			ms->ms_flags |= IEEE80211_MESHFLAGS_GATE;
3585 		else
3586 			ms->ms_flags &= ~IEEE80211_MESHFLAGS_GATE;
3587 		break;
3588 	case IEEE80211_IOC_MESH_TTL:
3589 		ms->ms_ttl = (uint8_t) ireq->i_val;
3590 		break;
3591 	case IEEE80211_IOC_MESH_RTCMD:
3592 		switch (ireq->i_val) {
3593 		case IEEE80211_MESH_RTCMD_LIST:
3594 			return EINVAL;
3595 		case IEEE80211_MESH_RTCMD_FLUSH:
3596 			ieee80211_mesh_rt_flush(vap);
3597 			break;
3598 		case IEEE80211_MESH_RTCMD_ADD:
3599 			if (IEEE80211_ADDR_EQ(vap->iv_myaddr, ireq->i_data) ||
3600 			    IEEE80211_ADDR_EQ(broadcastaddr, ireq->i_data))
3601 				return EINVAL;
3602 			error = copyin(ireq->i_data, &tmpaddr,
3603 			    IEEE80211_ADDR_LEN);
3604 			if (error == 0)
3605 				ieee80211_mesh_discover(vap, tmpaddr, NULL);
3606 			break;
3607 		case IEEE80211_MESH_RTCMD_DELETE:
3608 			ieee80211_mesh_rt_del(vap, ireq->i_data);
3609 			break;
3610 		default:
3611 			return ENOSYS;
3612 		}
3613 		break;
3614 	case IEEE80211_IOC_MESH_PR_METRIC:
3615 		error = copyin(ireq->i_data, tmpproto, sizeof(tmpproto));
3616 		if (error == 0) {
3617 			error = mesh_select_proto_metric(vap, tmpproto);
3618 			if (error == 0)
3619 				error = ENETRESET;
3620 		}
3621 		break;
3622 	case IEEE80211_IOC_MESH_PR_PATH:
3623 		error = copyin(ireq->i_data, tmpproto, sizeof(tmpproto));
3624 		if (error == 0) {
3625 			error = mesh_select_proto_path(vap, tmpproto);
3626 			if (error == 0)
3627 				error = ENETRESET;
3628 		}
3629 		break;
3630 	default:
3631 		return ENOSYS;
3632 	}
3633 	return error;
3634 }
3635 IEEE80211_IOCTL_SET(mesh, mesh_ioctl_set80211);
3636