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