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