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