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