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