Fix remaining M_NOWAIT and use ieee80211_handoff instead of if_transmit.
[dragonfly.git] / sys / netproto / 802_11 / wlan / ieee80211_mesh.c
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. 
28  *
29  * $FreeBSD: head/sys/net80211/ieee80211_mesh.c 203423 2010-02-03 10:12:49Z rpaulo $
30  * $DragonFly$
31  */ 
32
33 /*
34  * IEEE 802.11s Mesh Point (MBSS) support.
35  *
36  * Based on March 2009, D3.0 802.11s draft spec.
37  */
38 #include "opt_inet.h"
39 #include "opt_wlan.h"
40
41 #include <sys/param.h>
42 #include <sys/systm.h> 
43 #include <sys/mbuf.h>   
44 #include <sys/malloc.h>
45 #include <sys/kernel.h>
46
47 #include <sys/socket.h>
48 #include <sys/sockio.h>
49 #include <sys/endian.h>
50 #include <sys/errno.h>
51 #include <sys/proc.h>
52 #include <sys/sysctl.h>
53
54 #include <net/if.h>
55 #include <net/if_media.h>
56 #include <net/if_llc.h>
57 #include <net/ethernet.h>
58 #include <net/route.h>
59
60 #include <netproto/802_11/ieee80211_var.h>
61 #include <netproto/802_11/ieee80211_action.h>
62 #include <netproto/802_11/ieee80211_input.h>
63 #include <netproto/802_11/ieee80211_mesh.h>
64
65 static void     mesh_rt_flush_invalid(struct ieee80211vap *);
66 static int      mesh_select_proto_path(struct ieee80211vap *, const char *);
67 static int      mesh_select_proto_metric(struct ieee80211vap *, const char *);
68 static void     mesh_vattach(struct ieee80211vap *);
69 static int      mesh_newstate(struct ieee80211vap *, enum ieee80211_state, int);
70 static void     mesh_rt_cleanup_cb(void *);
71 static void     mesh_linkchange(struct ieee80211_node *,
72                     enum ieee80211_mesh_mlstate);
73 static void     mesh_checkid(void *, struct ieee80211_node *);
74 static uint32_t mesh_generateid(struct ieee80211vap *);
75 static int      mesh_checkpseq(struct ieee80211vap *,
76                     const uint8_t [IEEE80211_ADDR_LEN], uint32_t);
77 static struct ieee80211_node *
78                 mesh_find_txnode(struct ieee80211vap *,
79                     const uint8_t [IEEE80211_ADDR_LEN]);
80 static void     mesh_forward(struct ieee80211vap *, struct mbuf *,
81                     const struct ieee80211_meshcntl *);
82 static int      mesh_input(struct ieee80211_node *, struct mbuf *, int, int);
83 static void     mesh_recv_mgmt(struct ieee80211_node *, struct mbuf *, int,
84                     int, int);
85 static void     mesh_peer_timeout_setup(struct ieee80211_node *);
86 static void     mesh_peer_timeout_backoff(struct ieee80211_node *);
87 static void     mesh_peer_timeout_cb(void *);
88 static __inline void
89                 mesh_peer_timeout_stop(struct ieee80211_node *);
90 static int      mesh_verify_meshid(struct ieee80211vap *, const uint8_t *);
91 static int      mesh_verify_meshconf(struct ieee80211vap *, const uint8_t *);
92 static int      mesh_verify_meshpeer(struct ieee80211vap *, uint8_t,
93                     const uint8_t *);
94 uint32_t        mesh_airtime_calc(struct ieee80211_node *);
95
96 /*
97  * Timeout values come from the specification and are in milliseconds.
98  */
99 SYSCTL_NODE(_net_wlan, OID_AUTO, mesh, CTLFLAG_RD, 0,
100     "IEEE 802.11s parameters");
101 static int ieee80211_mesh_retrytimeout = -1;
102 SYSCTL_PROC(_net_wlan_mesh, OID_AUTO, retrytimeout, CTLTYPE_INT | CTLFLAG_RW,
103     &ieee80211_mesh_retrytimeout, 0, ieee80211_sysctl_msecs_ticks, "I",
104     "Retry timeout (msec)");
105 static int ieee80211_mesh_holdingtimeout = -1;
106 SYSCTL_PROC(_net_wlan_mesh, OID_AUTO, holdingtimeout, CTLTYPE_INT | CTLFLAG_RW,
107     &ieee80211_mesh_holdingtimeout, 0, ieee80211_sysctl_msecs_ticks, "I",
108     "Holding state timeout (msec)");
109 static int ieee80211_mesh_confirmtimeout = -1;
110 SYSCTL_PROC(_net_wlan_mesh, OID_AUTO, confirmtimeout, CTLTYPE_INT | CTLFLAG_RW,
111     &ieee80211_mesh_confirmtimeout, 0, ieee80211_sysctl_msecs_ticks, "I",
112     "Confirm state timeout (msec)");
113 static int ieee80211_mesh_maxretries = 2;
114 SYSCTL_INT(_net_wlan_mesh, OID_AUTO, maxretries, CTLTYPE_INT | CTLFLAG_RW,
115     &ieee80211_mesh_maxretries, 0,
116     "Maximum retries during peer link establishment");
117
118 static const uint8_t broadcastaddr[IEEE80211_ADDR_LEN] =
119         { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
120
121 static  ieee80211_recv_action_func mesh_recv_action_meshpeering_open;
122 static  ieee80211_recv_action_func mesh_recv_action_meshpeering_confirm;
123 static  ieee80211_recv_action_func mesh_recv_action_meshpeering_close;
124 static  ieee80211_recv_action_func mesh_recv_action_meshlmetric_req;
125 static  ieee80211_recv_action_func mesh_recv_action_meshlmetric_rep;
126
127 static  ieee80211_send_action_func mesh_send_action_meshpeering_open;
128 static  ieee80211_send_action_func mesh_send_action_meshpeering_confirm;
129 static  ieee80211_send_action_func mesh_send_action_meshpeering_close;
130 static  ieee80211_send_action_func mesh_send_action_meshlink_request;
131 static  ieee80211_send_action_func mesh_send_action_meshlink_reply;
132
133 static const struct ieee80211_mesh_proto_metric mesh_metric_airtime = {
134         .mpm_descr      = "AIRTIME",
135         .mpm_ie         = IEEE80211_MESHCONF_METRIC_AIRTIME,
136         .mpm_metric     = mesh_airtime_calc,
137 };
138
139 static struct ieee80211_mesh_proto_path         mesh_proto_paths[4];
140 static struct ieee80211_mesh_proto_metric       mesh_proto_metrics[4];
141
142 #define MESH_RT_LOCK(ms)        lockmgr(&(ms)->ms_rt_lock, LK_EXCLUSIVE)
143 #define MESH_RT_LOCK_ASSERT(ms) \
144         KKASSERT(lockstatus(&(ms)->ms_rt_lock, curthread) != 0)
145 #define MESH_RT_UNLOCK(ms)      lockmgr(&(ms)->ms_rt_lock, LK_RELEASE)
146
147 MALLOC_DEFINE(M_80211_MESH_RT, "80211mesh", "802.11s routing table");
148
149 /*
150  * Helper functions to manipulate the Mesh routing table.
151  */
152
153 static struct ieee80211_mesh_route *
154 mesh_rt_find_locked(struct ieee80211_mesh_state *ms,
155     const uint8_t dest[IEEE80211_ADDR_LEN])
156 {
157         struct ieee80211_mesh_route *rt;
158
159         MESH_RT_LOCK_ASSERT(ms);
160
161         TAILQ_FOREACH(rt, &ms->ms_routes, rt_next) {
162                 if (IEEE80211_ADDR_EQ(dest, rt->rt_dest))
163                         return rt;
164         }
165         return NULL;
166 }
167
168 static struct ieee80211_mesh_route *
169 mesh_rt_add_locked(struct ieee80211_mesh_state *ms,
170     const uint8_t dest[IEEE80211_ADDR_LEN])
171 {
172         struct ieee80211_mesh_route *rt;
173
174         KASSERT(!IEEE80211_ADDR_EQ(broadcastaddr, dest),
175             ("%s: adding broadcast to the routing table", __func__));
176
177         MESH_RT_LOCK_ASSERT(ms);
178
179         rt = kmalloc(ALIGN(sizeof(struct ieee80211_mesh_route)) +
180             ms->ms_ppath->mpp_privlen, M_80211_MESH_RT, M_INTWAIT | M_ZERO);
181         if (rt != NULL) {
182                 IEEE80211_ADDR_COPY(rt->rt_dest, dest);
183                 rt->rt_priv = (void *)ALIGN(&rt[1]);
184                 rt->rt_crtime = ticks;
185                 TAILQ_INSERT_TAIL(&ms->ms_routes, rt, rt_next);
186         }
187         return rt;
188 }
189
190 struct ieee80211_mesh_route *
191 ieee80211_mesh_rt_find(struct ieee80211vap *vap,
192     const uint8_t dest[IEEE80211_ADDR_LEN])
193 {
194         struct ieee80211_mesh_state *ms = vap->iv_mesh;
195         struct ieee80211_mesh_route *rt;
196
197         MESH_RT_LOCK(ms);
198         rt = mesh_rt_find_locked(ms, dest);
199         MESH_RT_UNLOCK(ms);
200         return rt;
201 }
202
203 struct ieee80211_mesh_route *
204 ieee80211_mesh_rt_add(struct ieee80211vap *vap,
205     const uint8_t dest[IEEE80211_ADDR_LEN])
206 {
207         struct ieee80211_mesh_state *ms = vap->iv_mesh;
208         struct ieee80211_mesh_route *rt;
209
210         KASSERT(ieee80211_mesh_rt_find(vap, dest) == NULL,
211             ("%s: duplicate entry in the routing table", __func__));
212         KASSERT(!IEEE80211_ADDR_EQ(vap->iv_myaddr, dest),
213             ("%s: adding self to the routing table", __func__));
214
215         MESH_RT_LOCK(ms);
216         rt = mesh_rt_add_locked(ms, dest);
217         MESH_RT_UNLOCK(ms);
218         return rt;
219 }
220
221 /*
222  * Add a proxy route (as needed) for the specified destination.
223  */
224 void
225 ieee80211_mesh_proxy_check(struct ieee80211vap *vap,
226     const uint8_t dest[IEEE80211_ADDR_LEN])
227 {
228         struct ieee80211_mesh_state *ms = vap->iv_mesh;
229         struct ieee80211_mesh_route *rt;
230
231         MESH_RT_LOCK(ms);
232         rt = mesh_rt_find_locked(ms, dest);
233         if (rt == NULL) {
234                 rt = mesh_rt_add_locked(ms, dest);
235                 if (rt == NULL) {
236                         IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH, dest,
237                             "%s", "unable to add proxy entry");
238                         vap->iv_stats.is_mesh_rtaddfailed++;
239                 } else {
240                         IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH, dest,
241                             "%s", "add proxy entry");
242                         IEEE80211_ADDR_COPY(rt->rt_nexthop, vap->iv_myaddr);
243                         rt->rt_flags |= IEEE80211_MESHRT_FLAGS_VALID
244                                      |  IEEE80211_MESHRT_FLAGS_PROXY;
245                 }
246         /* XXX assert PROXY? */
247         } else if ((rt->rt_flags & IEEE80211_MESHRT_FLAGS_VALID) == 0) {
248                 struct ieee80211com *ic = vap->iv_ic;
249                 /*
250                  * Fix existing entry created by received frames from
251                  * stations that have some memory of dest.  We also
252                  * flush any frames held on the staging queue; delivering
253                  * them is too much trouble right now.
254                  */
255                 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH, dest,
256                     "%s", "fix proxy entry");
257                 IEEE80211_ADDR_COPY(rt->rt_nexthop, vap->iv_myaddr);
258                 rt->rt_flags |= IEEE80211_MESHRT_FLAGS_VALID
259                              |  IEEE80211_MESHRT_FLAGS_PROXY;
260                 /* XXX belongs in hwmp */
261                 ieee80211_ageq_drain_node(&ic->ic_stageq,
262                    (void *)(uintptr_t) ieee80211_mac_hash(ic, dest));
263                 /* XXX stat? */
264         }
265         MESH_RT_UNLOCK(ms);
266 }
267
268 static __inline void
269 mesh_rt_del(struct ieee80211_mesh_state *ms, struct ieee80211_mesh_route *rt)
270 {
271         TAILQ_REMOVE(&ms->ms_routes, rt, rt_next);
272         kfree(rt, M_80211_MESH_RT);
273 }
274
275 void
276 ieee80211_mesh_rt_del(struct ieee80211vap *vap,
277     const uint8_t dest[IEEE80211_ADDR_LEN])
278 {
279         struct ieee80211_mesh_state *ms = vap->iv_mesh;
280         struct ieee80211_mesh_route *rt, *next;
281
282         MESH_RT_LOCK(ms);
283         TAILQ_FOREACH_MUTABLE(rt, &ms->ms_routes, rt_next, next) {
284                 if (IEEE80211_ADDR_EQ(rt->rt_dest, dest)) {
285                         mesh_rt_del(ms, rt);
286                         MESH_RT_UNLOCK(ms);
287                         return;
288                 }
289         }
290         MESH_RT_UNLOCK(ms);
291 }
292
293 void
294 ieee80211_mesh_rt_flush(struct ieee80211vap *vap)
295 {
296         struct ieee80211_mesh_state *ms = vap->iv_mesh;
297         struct ieee80211_mesh_route *rt, *next;
298
299         if (ms == NULL)
300                 return;
301         MESH_RT_LOCK(ms);
302         TAILQ_FOREACH_MUTABLE(rt, &ms->ms_routes, rt_next, next)
303                 mesh_rt_del(ms, rt);
304         MESH_RT_UNLOCK(ms);
305 }
306
307 void
308 ieee80211_mesh_rt_flush_peer(struct ieee80211vap *vap,
309     const uint8_t peer[IEEE80211_ADDR_LEN])
310 {
311         struct ieee80211_mesh_state *ms = vap->iv_mesh;
312         struct ieee80211_mesh_route *rt, *next;
313
314         MESH_RT_LOCK(ms);
315         TAILQ_FOREACH_MUTABLE(rt, &ms->ms_routes, rt_next, next) {
316                 if (IEEE80211_ADDR_EQ(rt->rt_nexthop, peer))
317                         mesh_rt_del(ms, rt);
318         }
319         MESH_RT_UNLOCK(ms);
320 }
321
322 /*
323  * Flush expired routing entries, i.e. those in invalid state for
324  * some time.
325  */
326 static void
327 mesh_rt_flush_invalid(struct ieee80211vap *vap)
328 {
329         struct ieee80211_mesh_state *ms = vap->iv_mesh;
330         struct ieee80211_mesh_route *rt, *next;
331
332         if (ms == NULL)
333                 return;
334         MESH_RT_LOCK(ms);
335         TAILQ_FOREACH_MUTABLE(rt, &ms->ms_routes, rt_next, next) {
336                 if ((rt->rt_flags & IEEE80211_MESHRT_FLAGS_VALID) == 0 &&
337                     ticks - rt->rt_crtime >= ms->ms_ppath->mpp_inact)
338                         mesh_rt_del(ms, rt);
339         }
340         MESH_RT_UNLOCK(ms);
341 }
342
343 #define N(a)    (sizeof(a) / sizeof(a[0]))
344 int
345 ieee80211_mesh_register_proto_path(const struct ieee80211_mesh_proto_path *mpp)
346 {
347         int i, firstempty = -1;
348
349         for (i = 0; i < N(mesh_proto_paths); i++) {
350                 if (strncmp(mpp->mpp_descr, mesh_proto_paths[i].mpp_descr,
351                     IEEE80211_MESH_PROTO_DSZ) == 0)
352                         return EEXIST;
353                 if (!mesh_proto_paths[i].mpp_active && firstempty == -1)
354                         firstempty = i;
355         }
356         if (firstempty < 0)
357                 return ENOSPC;
358         memcpy(&mesh_proto_paths[firstempty], mpp, sizeof(*mpp));
359         mesh_proto_paths[firstempty].mpp_active = 1;
360         return 0;
361 }
362
363 int
364 ieee80211_mesh_register_proto_metric(const struct
365     ieee80211_mesh_proto_metric *mpm)
366 {
367         int i, firstempty = -1;
368
369         for (i = 0; i < N(mesh_proto_metrics); i++) {
370                 if (strncmp(mpm->mpm_descr, mesh_proto_metrics[i].mpm_descr,
371                     IEEE80211_MESH_PROTO_DSZ) == 0)
372                         return EEXIST;
373                 if (!mesh_proto_metrics[i].mpm_active && firstempty == -1)
374                         firstempty = i;
375         }
376         if (firstempty < 0)
377                 return ENOSPC;
378         memcpy(&mesh_proto_metrics[firstempty], mpm, sizeof(*mpm));
379         mesh_proto_metrics[firstempty].mpm_active = 1;
380         return 0;
381 }
382
383 static int
384 mesh_select_proto_path(struct ieee80211vap *vap, const char *name)
385 {
386         struct ieee80211_mesh_state *ms = vap->iv_mesh;
387         int i;
388
389         for (i = 0; i < N(mesh_proto_paths); i++) {
390                 if (strcasecmp(mesh_proto_paths[i].mpp_descr, name) == 0) {
391                         ms->ms_ppath = &mesh_proto_paths[i];
392                         return 0;
393                 }
394         }
395         return ENOENT;
396 }
397
398 static int
399 mesh_select_proto_metric(struct ieee80211vap *vap, const char *name)
400 {
401         struct ieee80211_mesh_state *ms = vap->iv_mesh;
402         int i;
403
404         for (i = 0; i < N(mesh_proto_metrics); i++) {
405                 if (strcasecmp(mesh_proto_metrics[i].mpm_descr, name) == 0) {
406                         ms->ms_pmetric = &mesh_proto_metrics[i];
407                         return 0;
408                 }
409         }
410         return ENOENT;
411 }
412 #undef  N
413
414 static void
415 ieee80211_mesh_init(void)
416 {
417
418         memset(mesh_proto_paths, 0, sizeof(mesh_proto_paths));
419         memset(mesh_proto_metrics, 0, sizeof(mesh_proto_metrics));
420
421         /*
422          * Setup mesh parameters that depends on the clock frequency.
423          */
424         ieee80211_mesh_retrytimeout = msecs_to_ticks(40);
425         ieee80211_mesh_holdingtimeout = msecs_to_ticks(40);
426         ieee80211_mesh_confirmtimeout = msecs_to_ticks(40);
427
428         /*
429          * Register action frame handlers.
430          */
431         ieee80211_recv_action_register(IEEE80211_ACTION_CAT_MESHPEERING,
432             IEEE80211_ACTION_MESHPEERING_OPEN,
433             mesh_recv_action_meshpeering_open);
434         ieee80211_recv_action_register(IEEE80211_ACTION_CAT_MESHPEERING,
435             IEEE80211_ACTION_MESHPEERING_CONFIRM,
436             mesh_recv_action_meshpeering_confirm);
437         ieee80211_recv_action_register(IEEE80211_ACTION_CAT_MESHPEERING,
438             IEEE80211_ACTION_MESHPEERING_CLOSE,
439             mesh_recv_action_meshpeering_close);
440         ieee80211_recv_action_register(IEEE80211_ACTION_CAT_MESHLMETRIC,
441             IEEE80211_ACTION_MESHLMETRIC_REQ, mesh_recv_action_meshlmetric_req);
442         ieee80211_recv_action_register(IEEE80211_ACTION_CAT_MESHLMETRIC,
443             IEEE80211_ACTION_MESHLMETRIC_REP, mesh_recv_action_meshlmetric_rep);
444
445         ieee80211_send_action_register(IEEE80211_ACTION_CAT_MESHPEERING, 
446             IEEE80211_ACTION_MESHPEERING_OPEN,
447             mesh_send_action_meshpeering_open);
448         ieee80211_send_action_register(IEEE80211_ACTION_CAT_MESHPEERING, 
449             IEEE80211_ACTION_MESHPEERING_CONFIRM,
450             mesh_send_action_meshpeering_confirm);
451         ieee80211_send_action_register(IEEE80211_ACTION_CAT_MESHPEERING, 
452             IEEE80211_ACTION_MESHPEERING_CLOSE,
453             mesh_send_action_meshpeering_close);
454         ieee80211_send_action_register(IEEE80211_ACTION_CAT_MESHLMETRIC, 
455             IEEE80211_ACTION_MESHLMETRIC_REQ,
456             mesh_send_action_meshlink_request);
457         ieee80211_send_action_register(IEEE80211_ACTION_CAT_MESHLMETRIC, 
458             IEEE80211_ACTION_MESHLMETRIC_REP,
459             mesh_send_action_meshlink_reply);
460
461         /*
462          * Register Airtime Link Metric.
463          */
464         ieee80211_mesh_register_proto_metric(&mesh_metric_airtime);
465
466 }
467 SYSINIT(wlan_mesh, SI_SUB_DRIVERS, SI_ORDER_FIRST, ieee80211_mesh_init, NULL);
468
469 void
470 ieee80211_mesh_attach(struct ieee80211com *ic)
471 {
472         ic->ic_vattach[IEEE80211_M_MBSS] = mesh_vattach;
473 }
474
475 void
476 ieee80211_mesh_detach(struct ieee80211com *ic)
477 {
478 }
479
480 static void
481 mesh_vdetach_peers(void *arg, struct ieee80211_node *ni)
482 {
483         struct ieee80211com *ic = ni->ni_ic;
484         uint16_t args[3];
485
486         if (ni->ni_mlstate == IEEE80211_NODE_MESH_ESTABLISHED) {
487                 args[0] = ni->ni_mlpid;
488                 args[1] = ni->ni_mllid;
489                 args[2] = IEEE80211_REASON_PEER_LINK_CANCELED;
490                 ieee80211_send_action(ni,
491                     IEEE80211_ACTION_CAT_MESHPEERING,
492                     IEEE80211_ACTION_MESHPEERING_CLOSE,
493                     args);
494         }
495         callout_stop(&ni->ni_mltimer);
496         /* XXX belongs in hwmp */
497         ieee80211_ageq_drain_node(&ic->ic_stageq,
498            (void *)(uintptr_t) ieee80211_mac_hash(ic, ni->ni_macaddr));
499 }
500
501 static void
502 mesh_vdetach(struct ieee80211vap *vap)
503 {
504         struct ieee80211_mesh_state *ms = vap->iv_mesh;
505
506         callout_stop(&ms->ms_cleantimer);
507         ieee80211_iterate_nodes(&vap->iv_ic->ic_sta, mesh_vdetach_peers,
508             NULL);
509         ieee80211_mesh_rt_flush(vap);
510         lockuninit(&ms->ms_rt_lock);
511         ms->ms_ppath->mpp_vdetach(vap);
512         kfree(vap->iv_mesh, M_80211_VAP);
513         vap->iv_mesh = NULL;
514 }
515
516 static void
517 mesh_vattach(struct ieee80211vap *vap)
518 {
519         struct ieee80211_mesh_state *ms;
520         vap->iv_newstate = mesh_newstate;
521         vap->iv_input = mesh_input;
522         vap->iv_opdetach = mesh_vdetach;
523         vap->iv_recv_mgmt = mesh_recv_mgmt;
524         ms = kmalloc(sizeof(struct ieee80211_mesh_state), M_80211_VAP,
525             M_INTWAIT | M_ZERO);
526         if (ms == NULL) {
527                 kprintf("%s: couldn't alloc MBSS state\n", __func__);
528                 return;
529         }
530         vap->iv_mesh = ms;
531         ms->ms_seq = 0;
532         ms->ms_flags = (IEEE80211_MESHFLAGS_AP | IEEE80211_MESHFLAGS_FWD);
533         ms->ms_ttl = IEEE80211_MESH_DEFAULT_TTL;
534         TAILQ_INIT(&ms->ms_routes);
535         lockinit(&ms->ms_rt_lock, "MBSS", 0, 0);
536         callout_init_mp(&ms->ms_cleantimer);
537         mesh_select_proto_metric(vap, "AIRTIME");
538         KASSERT(ms->ms_pmetric, ("ms_pmetric == NULL"));
539         mesh_select_proto_path(vap, "HWMP");
540         KASSERT(ms->ms_ppath, ("ms_ppath == NULL"));
541         ms->ms_ppath->mpp_vattach(vap);
542 }
543
544 /*
545  * IEEE80211_M_MBSS vap state machine handler.
546  */
547 static int
548 mesh_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
549 {
550         struct ieee80211_mesh_state *ms = vap->iv_mesh;
551         struct ieee80211com *ic = vap->iv_ic;
552         struct ieee80211_node *ni;
553         enum ieee80211_state ostate;
554
555         IEEE80211_LOCK_ASSERT(ic);
556
557         ostate = vap->iv_state;
558         IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE, "%s: %s -> %s (%d)\n",
559             __func__, ieee80211_state_name[ostate],
560             ieee80211_state_name[nstate], arg);
561         vap->iv_state = nstate;         /* state transition */
562         if (ostate != IEEE80211_S_SCAN)
563                 ieee80211_cancel_scan(vap);     /* background scan */
564         ni = vap->iv_bss;                       /* NB: no reference held */
565         if (nstate != IEEE80211_S_RUN && ostate == IEEE80211_S_RUN)
566                 callout_stop(&ms->ms_cleantimer);
567         switch (nstate) {
568         case IEEE80211_S_INIT:
569                 switch (ostate) {
570                 case IEEE80211_S_SCAN:
571                         ieee80211_cancel_scan(vap);
572                         break;
573                 case IEEE80211_S_CAC:
574                         ieee80211_dfs_cac_stop(vap);
575                         break;
576                 case IEEE80211_S_RUN:
577                         ieee80211_iterate_nodes(&ic->ic_sta,
578                             mesh_vdetach_peers, NULL);
579                         break;
580                 default:
581                         break;
582                 }
583                 if (ostate != IEEE80211_S_INIT) {
584                         /* NB: optimize INIT -> INIT case */
585                         ieee80211_reset_bss(vap);
586                         ieee80211_mesh_rt_flush(vap);
587                 }
588                 break;
589         case IEEE80211_S_SCAN:
590                 switch (ostate) {
591                 case IEEE80211_S_INIT:
592                         if (vap->iv_des_chan != IEEE80211_CHAN_ANYC &&
593                             !IEEE80211_IS_CHAN_RADAR(vap->iv_des_chan) &&
594                             ms->ms_idlen != 0) {
595                                 /*
596                                  * Already have a channel and a mesh ID; bypass
597                                  * the scan and startup immediately.
598                                  */
599                                 ieee80211_create_ibss(vap, vap->iv_des_chan);
600                                 break;
601                         }
602                         /*
603                          * Initiate a scan.  We can come here as a result
604                          * of an IEEE80211_IOC_SCAN_REQ too in which case
605                          * the vap will be marked with IEEE80211_FEXT_SCANREQ
606                          * and the scan request parameters will be present
607                          * in iv_scanreq.  Otherwise we do the default.
608                         */
609                         if (vap->iv_flags_ext & IEEE80211_FEXT_SCANREQ) {
610                                 ieee80211_check_scan(vap,
611                                     vap->iv_scanreq_flags,
612                                     vap->iv_scanreq_duration,
613                                     vap->iv_scanreq_mindwell,
614                                     vap->iv_scanreq_maxdwell,
615                                     vap->iv_scanreq_nssid, vap->iv_scanreq_ssid);
616                                 vap->iv_flags_ext &= ~IEEE80211_FEXT_SCANREQ;
617                         } else
618                                 ieee80211_check_scan_current(vap);
619                         break;
620                 default:
621                         break;
622                 }
623                 break;
624         case IEEE80211_S_CAC:
625                 /*
626                  * Start CAC on a DFS channel.  We come here when starting
627                  * a bss on a DFS channel (see ieee80211_create_ibss).
628                  */
629                 ieee80211_dfs_cac_start(vap);
630                 break;
631         case IEEE80211_S_RUN:
632                 switch (ostate) {
633                 case IEEE80211_S_INIT:
634                         /*
635                          * Already have a channel; bypass the
636                          * scan and startup immediately.
637                          * Note that ieee80211_create_ibss will call
638                          * back to do a RUN->RUN state change.
639                          */
640                         ieee80211_create_ibss(vap,
641                             ieee80211_ht_adjust_channel(ic,
642                                 ic->ic_curchan, vap->iv_flags_ht));
643                         /* NB: iv_bss is changed on return */
644                         break;
645                 case IEEE80211_S_CAC:
646                         /*
647                          * NB: This is the normal state change when CAC
648                          * expires and no radar was detected; no need to
649                          * clear the CAC timer as it's already expired.
650                          */
651                         /* fall thru... */
652                 case IEEE80211_S_CSA:
653 #if 0
654                         /*
655                          * Shorten inactivity timer of associated stations
656                          * to weed out sta's that don't follow a CSA.
657                          */
658                         ieee80211_iterate_nodes(&ic->ic_sta, sta_csa, vap);
659 #endif
660                         /*
661                          * Update bss node channel to reflect where
662                          * we landed after CSA.
663                          */
664                         ieee80211_node_set_chan(vap->iv_bss,
665                             ieee80211_ht_adjust_channel(ic, ic->ic_curchan,
666                                 ieee80211_htchanflags(vap->iv_bss->ni_chan)));
667                         /* XXX bypass debug msgs */
668                         break;
669                 case IEEE80211_S_SCAN:
670                 case IEEE80211_S_RUN:
671 #ifdef IEEE80211_DEBUG
672                         if (ieee80211_msg_debug(vap)) {
673                                 struct ieee80211_node *ni = vap->iv_bss;
674                                 ieee80211_note(vap,
675                                     "synchronized with %6D meshid ",
676                                     ni->ni_meshid, ":");
677                                 ieee80211_print_essid(ni->ni_meshid,
678                                     ni->ni_meshidlen);
679                                 /* XXX MCS/HT */
680                                 kprintf(" channel %d\n",
681                                     ieee80211_chan2ieee(ic, ic->ic_curchan));
682                         }
683 #endif
684                         break;
685                 default:
686                         break;
687                 }
688                 ieee80211_node_authorize(vap->iv_bss);
689                 callout_reset(&ms->ms_cleantimer, ms->ms_ppath->mpp_inact,
690                     mesh_rt_cleanup_cb, vap);
691                 break;
692         default:
693                 break;
694         }
695         /* NB: ostate not nstate */
696         ms->ms_ppath->mpp_newstate(vap, ostate, arg);
697         return 0;
698 }
699
700 static void
701 mesh_rt_cleanup_cb(void *arg)
702 {
703         struct ieee80211vap *vap = arg;
704         struct ieee80211_mesh_state *ms = vap->iv_mesh;
705
706         mesh_rt_flush_invalid(vap);
707         callout_reset(&ms->ms_cleantimer, ms->ms_ppath->mpp_inact,
708             mesh_rt_cleanup_cb, vap);
709 }
710
711
712 /*
713  * Helper function to note the Mesh Peer Link FSM change.
714  */
715 static void
716 mesh_linkchange(struct ieee80211_node *ni, enum ieee80211_mesh_mlstate state)
717 {
718         struct ieee80211vap *vap = ni->ni_vap;
719         struct ieee80211_mesh_state *ms = vap->iv_mesh;
720 #ifdef IEEE80211_DEBUG
721         static const char *meshlinkstates[] = {
722                 [IEEE80211_NODE_MESH_IDLE]              = "IDLE",
723                 [IEEE80211_NODE_MESH_OPENSNT]           = "OPEN SENT",
724                 [IEEE80211_NODE_MESH_OPENRCV]           = "OPEN RECEIVED",
725                 [IEEE80211_NODE_MESH_CONFIRMRCV]        = "CONFIRM RECEIVED",
726                 [IEEE80211_NODE_MESH_ESTABLISHED]       = "ESTABLISHED",
727                 [IEEE80211_NODE_MESH_HOLDING]           = "HOLDING"
728         };
729 #endif
730         IEEE80211_NOTE(vap, IEEE80211_MSG_MESH,
731             ni, "peer link: %s -> %s",
732             meshlinkstates[ni->ni_mlstate], meshlinkstates[state]);
733
734         /* track neighbor count */
735         if (state == IEEE80211_NODE_MESH_ESTABLISHED &&
736             ni->ni_mlstate != IEEE80211_NODE_MESH_ESTABLISHED) {
737                 KASSERT(ms->ms_neighbors < 65535, ("neighbor count overflow"));
738                 ms->ms_neighbors++;
739                 ieee80211_beacon_notify(vap, IEEE80211_BEACON_MESHCONF);
740         } else if (ni->ni_mlstate == IEEE80211_NODE_MESH_ESTABLISHED &&
741             state != IEEE80211_NODE_MESH_ESTABLISHED) {
742                 KASSERT(ms->ms_neighbors > 0, ("neighbor count 0"));
743                 ms->ms_neighbors--;
744                 ieee80211_beacon_notify(vap, IEEE80211_BEACON_MESHCONF);
745         }
746         ni->ni_mlstate = state;
747         switch (state) {
748         case IEEE80211_NODE_MESH_HOLDING:
749                 ms->ms_ppath->mpp_peerdown(ni);
750                 break;
751         case IEEE80211_NODE_MESH_ESTABLISHED:
752                 ieee80211_mesh_discover(vap, ni->ni_macaddr, NULL);
753                 break;
754         default:
755                 break;
756         }
757 }
758
759 /*
760  * Helper function to generate a unique local ID required for mesh
761  * peer establishment.
762  */
763 static void
764 mesh_checkid(void *arg, struct ieee80211_node *ni)
765 {
766         uint16_t *r = arg;
767         
768         if (*r == ni->ni_mllid)
769                 *(uint16_t *)arg = 0;
770 }
771
772 static uint32_t
773 mesh_generateid(struct ieee80211vap *vap)
774 {
775         int maxiter = 4;
776         uint16_t r;
777
778         do {
779                 get_random_bytes(&r, 2);
780                 ieee80211_iterate_nodes(&vap->iv_ic->ic_sta, mesh_checkid, &r);
781                 maxiter--;
782         } while (r == 0 && maxiter > 0);
783         return r;
784 }
785
786 /*
787  * Verifies if we already received this packet by checking its
788  * sequence number.
789  * Returns 0 if the frame is to be accepted, 1 otherwise.
790  */
791 static int
792 mesh_checkpseq(struct ieee80211vap *vap,
793     const uint8_t source[IEEE80211_ADDR_LEN], uint32_t seq)
794 {
795         struct ieee80211_mesh_route *rt;
796
797         rt = ieee80211_mesh_rt_find(vap, source);
798         if (rt == NULL) {
799                 rt = ieee80211_mesh_rt_add(vap, source);
800                 if (rt == NULL) {
801                         IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH, source,
802                             "%s", "add mcast route failed");
803                         vap->iv_stats.is_mesh_rtaddfailed++;
804                         return 1;
805                 }
806                 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH, source,
807                     "add mcast route, mesh seqno %d", seq);
808                 rt->rt_lastmseq = seq;
809                 return 0;
810         }
811         if (IEEE80211_MESH_SEQ_GEQ(rt->rt_lastmseq, seq)) {
812                 return 1;
813         } else {
814                 rt->rt_lastmseq = seq;
815                 return 0;
816         }
817 }
818
819 /*
820  * Iterate the routing table and locate the next hop.
821  */
822 static struct ieee80211_node *
823 mesh_find_txnode(struct ieee80211vap *vap,
824     const uint8_t dest[IEEE80211_ADDR_LEN])
825 {
826         struct ieee80211_mesh_route *rt;
827
828         rt = ieee80211_mesh_rt_find(vap, dest);
829         if (rt == NULL)
830                 return NULL;
831         if ((rt->rt_flags & IEEE80211_MESHRT_FLAGS_VALID) == 0 ||
832             (rt->rt_flags & IEEE80211_MESHRT_FLAGS_PROXY)) {
833                 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_MESH, dest,
834                     "%s: !valid or proxy, flags 0x%x", __func__, rt->rt_flags);
835                 /* XXX stat */
836                 return NULL;
837         }
838         return ieee80211_find_txnode(vap, rt->rt_nexthop);
839 }
840
841 /*
842  * Forward the specified frame.
843  * Decrement the TTL and set TA to our MAC address.
844  */
845 static void
846 mesh_forward(struct ieee80211vap *vap, struct mbuf *m,
847     const struct ieee80211_meshcntl *mc)
848 {
849         struct ieee80211com *ic = vap->iv_ic;
850         struct ieee80211_mesh_state *ms = vap->iv_mesh;
851         struct ifnet *ifp = vap->iv_ifp;
852         struct ifnet *parent = ic->ic_ifp;
853         const struct ieee80211_frame *wh =
854             mtod(m, const struct ieee80211_frame *);
855         struct mbuf *mcopy;
856         struct ieee80211_meshcntl *mccopy;
857         struct ieee80211_frame *whcopy;
858         struct ieee80211_node *ni;
859         int err;
860
861         if (mc->mc_ttl == 0) {
862                 IEEE80211_NOTE_FRAME(vap, IEEE80211_MSG_MESH, wh,
863                     "%s", "frame not fwd'd, ttl 0");
864                 vap->iv_stats.is_mesh_fwd_ttl++;
865                 return;
866         }
867         if (!(ms->ms_flags & IEEE80211_MESHFLAGS_FWD)) {
868                 IEEE80211_NOTE_FRAME(vap, IEEE80211_MSG_MESH, wh,
869                     "%s", "frame not fwd'd, fwding disabled");
870                 vap->iv_stats.is_mesh_fwd_disabled++;
871                 return;
872         }
873         mcopy = m_dup(m, MB_DONTWAIT);
874         if (mcopy == NULL) {
875                 IEEE80211_NOTE_FRAME(vap, IEEE80211_MSG_MESH, wh,
876                     "%s", "frame not fwd'd, cannot dup");
877                 vap->iv_stats.is_mesh_fwd_nobuf++;
878                 ifp->if_oerrors++;
879                 return;
880         }
881         mcopy = m_pullup(mcopy, ieee80211_hdrspace(ic, wh) +
882             sizeof(struct ieee80211_meshcntl));
883         if (mcopy == NULL) {
884                 IEEE80211_NOTE_FRAME(vap, IEEE80211_MSG_MESH, wh,
885                     "%s", "frame not fwd'd, too short");
886                 vap->iv_stats.is_mesh_fwd_tooshort++;
887                 ifp->if_oerrors++;
888                 m_freem(mcopy);
889                 return;
890         }
891         whcopy = mtod(mcopy, struct ieee80211_frame *);
892         mccopy = (struct ieee80211_meshcntl *)
893             (mtod(mcopy, uint8_t *) + ieee80211_hdrspace(ic, wh));
894         /* XXX clear other bits? */
895         whcopy->i_fc[1] &= ~IEEE80211_FC1_RETRY;
896         IEEE80211_ADDR_COPY(whcopy->i_addr2, vap->iv_myaddr);
897         if (IEEE80211_IS_MULTICAST(wh->i_addr1)) {
898                 ni = ieee80211_ref_node(vap->iv_bss);
899                 mcopy->m_flags |= M_MCAST;
900         } else {
901                 ni = mesh_find_txnode(vap, whcopy->i_addr3);
902                 if (ni == NULL) {
903                         IEEE80211_NOTE_FRAME(vap, IEEE80211_MSG_MESH, wh,
904                             "%s", "frame not fwd'd, no path");
905                         vap->iv_stats.is_mesh_fwd_nopath++;
906                         m_freem(mcopy);
907                         return;
908                 }
909                 IEEE80211_ADDR_COPY(whcopy->i_addr1, ni->ni_macaddr);
910         }
911         KASSERT(mccopy->mc_ttl > 0, ("%s called with wrong ttl", __func__));
912         mccopy->mc_ttl--;
913
914         /* XXX calculate priority so drivers can find the tx queue */
915         M_WME_SETAC(mcopy, WME_AC_BE);
916
917         /* XXX do we know m_nextpkt is NULL? */
918         mcopy->m_pkthdr.rcvif = (void *) ni;
919         err = ieee80211_handoff(parent, mcopy);
920         if (err != 0) {
921                 /* NB: IFQ_HANDOFF reclaims mbuf */
922                 ieee80211_free_node(ni);
923         } else {
924                 ifp->if_opackets++;
925         }
926 }
927
928 static struct mbuf *
929 mesh_decap(struct ieee80211vap *vap, struct mbuf *m, int hdrlen, int meshdrlen)
930 {
931 #define WHDIR(wh) ((wh)->i_fc[1] & IEEE80211_FC1_DIR_MASK)
932         uint8_t b[sizeof(struct ieee80211_qosframe_addr4) +
933                   sizeof(struct ieee80211_meshcntl_ae11)];
934         const struct ieee80211_qosframe_addr4 *wh;
935         const struct ieee80211_meshcntl_ae10 *mc;
936         struct ether_header *eh;
937         struct llc *llc;
938         int ae;
939
940         if (m->m_len < hdrlen + sizeof(*llc) &&
941             (m = m_pullup(m, hdrlen + sizeof(*llc))) == NULL) {
942                 IEEE80211_DPRINTF(vap, IEEE80211_MSG_ANY,
943                     "discard data frame: %s", "m_pullup failed");
944                 vap->iv_stats.is_rx_tooshort++;
945                 return NULL;
946         }
947         memcpy(b, mtod(m, caddr_t), hdrlen);
948         wh = (const struct ieee80211_qosframe_addr4 *)&b[0];
949         mc = (const struct ieee80211_meshcntl_ae10 *)&b[hdrlen - meshdrlen];
950         KASSERT(WHDIR(wh) == IEEE80211_FC1_DIR_FROMDS ||
951                 WHDIR(wh) == IEEE80211_FC1_DIR_DSTODS,
952             ("bogus dir, fc 0x%x:0x%x", wh->i_fc[0], wh->i_fc[1]));
953
954         llc = (struct llc *)(mtod(m, caddr_t) + hdrlen);
955         if (llc->llc_dsap == LLC_SNAP_LSAP && llc->llc_ssap == LLC_SNAP_LSAP &&
956             llc->llc_control == LLC_UI && llc->llc_snap.org_code[0] == 0 &&
957             llc->llc_snap.org_code[1] == 0 && llc->llc_snap.org_code[2] == 0 &&
958             /* NB: preserve AppleTalk frames that have a native SNAP hdr */
959             !(llc->llc_snap.ether_type == htons(ETHERTYPE_AARP) ||
960               llc->llc_snap.ether_type == htons(ETHERTYPE_IPX))) {
961                 m_adj(m, hdrlen + sizeof(struct llc) - sizeof(*eh));
962                 llc = NULL;
963         } else {
964                 m_adj(m, hdrlen - sizeof(*eh));
965         }
966         eh = mtod(m, struct ether_header *);
967         ae = mc->mc_flags & 3;
968         if (WHDIR(wh) == IEEE80211_FC1_DIR_FROMDS) {
969                 IEEE80211_ADDR_COPY(eh->ether_dhost, wh->i_addr1);
970                 if (ae == 0) {
971                         IEEE80211_ADDR_COPY(eh->ether_shost, wh->i_addr3);
972                 } else if (ae == 1) {
973                         IEEE80211_ADDR_COPY(eh->ether_shost, mc->mc_addr4);
974                 } else {
975                         IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
976                             (const struct ieee80211_frame *)wh, NULL,
977                             "bad AE %d", ae);
978                         vap->iv_stats.is_mesh_badae++;
979                         m_freem(m);
980                         return NULL;
981                 }
982         } else {
983                 if (ae == 0) {
984                         IEEE80211_ADDR_COPY(eh->ether_dhost, wh->i_addr3);
985                         IEEE80211_ADDR_COPY(eh->ether_shost, wh->i_addr4);
986                 } else if (ae == 2) {
987                         IEEE80211_ADDR_COPY(eh->ether_dhost, mc->mc_addr4);
988                         IEEE80211_ADDR_COPY(eh->ether_shost, mc->mc_addr5);
989                 } else {
990                         IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
991                             (const struct ieee80211_frame *)wh, NULL,
992                             "bad AE %d", ae);
993                         vap->iv_stats.is_mesh_badae++;
994                         m_freem(m);
995                         return NULL;
996                 }
997         }
998 #ifdef ALIGNED_POINTER
999         if (!ALIGNED_POINTER(mtod(m, caddr_t) + sizeof(*eh), uint32_t)) {
1000                 m = ieee80211_realign(vap, m, sizeof(*eh));
1001                 if (m == NULL)
1002                         return NULL;
1003         }
1004 #endif /* ALIGNED_POINTER */
1005         if (llc != NULL) {
1006                 eh = mtod(m, struct ether_header *);
1007                 eh->ether_type = htons(m->m_pkthdr.len - sizeof(*eh));
1008         }
1009         return m;
1010 #undef WDIR
1011 }
1012
1013 /*
1014  * Return non-zero if the unicast mesh data frame should be processed
1015  * locally.  Frames that are not proxy'd have our address, otherwise
1016  * we need to consult the routing table to look for a proxy entry.
1017  */
1018 static __inline int
1019 mesh_isucastforme(struct ieee80211vap *vap, const struct ieee80211_frame *wh,
1020     const struct ieee80211_meshcntl *mc)
1021 {
1022         int ae = mc->mc_flags & 3;
1023
1024         KASSERT((wh->i_fc[1] & IEEE80211_FC1_DIR_MASK) == IEEE80211_FC1_DIR_DSTODS,
1025             ("bad dir 0x%x:0x%x", wh->i_fc[0], wh->i_fc[1]));
1026         KASSERT(ae == 0 || ae == 2, ("bad AE %d", ae));
1027         if (ae == 2) {                          /* ucast w/ proxy */
1028                 const struct ieee80211_meshcntl_ae10 *mc10 =
1029                     (const struct ieee80211_meshcntl_ae10 *) mc;
1030                 struct ieee80211_mesh_route *rt =
1031                     ieee80211_mesh_rt_find(vap, mc10->mc_addr4);
1032                 /* check for proxy route to ourself */
1033                 return (rt != NULL &&
1034                     (rt->rt_flags & IEEE80211_MESHRT_FLAGS_PROXY));
1035         } else                                  /* ucast w/o proxy */
1036                 return IEEE80211_ADDR_EQ(wh->i_addr3, vap->iv_myaddr);
1037 }
1038
1039 static int
1040 mesh_input(struct ieee80211_node *ni, struct mbuf *m, int rssi, int nf)
1041 {
1042 #define SEQ_LEQ(a,b)    ((int)((a)-(b)) <= 0)
1043 #define HAS_SEQ(type)   ((type & 0x4) == 0)
1044         struct ieee80211vap *vap = ni->ni_vap;
1045         struct ieee80211com *ic = ni->ni_ic;
1046         struct ifnet *ifp = vap->iv_ifp;
1047         struct ieee80211_frame *wh;
1048         const struct ieee80211_meshcntl *mc;
1049         int hdrspace, meshdrlen, need_tap;
1050         uint8_t dir, type, subtype, qos;
1051         uint32_t seq;
1052         uint8_t *addr;
1053         ieee80211_seq rxseq;
1054
1055         KASSERT(ni != NULL, ("null node"));
1056         ni->ni_inact = ni->ni_inact_reload;
1057
1058         need_tap = 1;                   /* mbuf need to be tapped. */
1059         type = -1;                      /* undefined */
1060
1061         if (m->m_pkthdr.len < sizeof(struct ieee80211_frame_min)) {
1062                 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
1063                     ni->ni_macaddr, NULL,
1064                     "too short (1): len %u", m->m_pkthdr.len);
1065                 vap->iv_stats.is_rx_tooshort++;
1066                 goto out;
1067         }
1068         /*
1069          * Bit of a cheat here, we use a pointer for a 3-address
1070          * frame format but don't reference fields past outside
1071          * ieee80211_frame_min w/o first validating the data is
1072          * present.
1073         */
1074         wh = mtod(m, struct ieee80211_frame *);
1075
1076         if ((wh->i_fc[0] & IEEE80211_FC0_VERSION_MASK) !=
1077             IEEE80211_FC0_VERSION_0) {
1078                 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
1079                     ni->ni_macaddr, NULL, "wrong version %x", wh->i_fc[0]);
1080                 vap->iv_stats.is_rx_badversion++;
1081                 goto err;
1082         }
1083         dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK;
1084         type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
1085         subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
1086         if ((ic->ic_flags & IEEE80211_F_SCAN) == 0) {
1087                 IEEE80211_RSSI_LPF(ni->ni_avgrssi, rssi);
1088                 ni->ni_noise = nf;
1089                 if (HAS_SEQ(type)) {
1090                         uint8_t tid = ieee80211_gettid(wh);
1091
1092                         if (IEEE80211_QOS_HAS_SEQ(wh) &&
1093                             TID_TO_WME_AC(tid) >= WME_AC_VI)
1094                                 ic->ic_wme.wme_hipri_traffic++;
1095                         rxseq = le16toh(*(uint16_t *)wh->i_seq);
1096                         if ((ni->ni_flags & IEEE80211_NODE_HT) == 0 &&
1097                             (wh->i_fc[1] & IEEE80211_FC1_RETRY) &&
1098                             SEQ_LEQ(rxseq, ni->ni_rxseqs[tid])) {
1099                                 /* duplicate, discard */
1100                                 IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
1101                                     wh->i_addr1, "duplicate",
1102                                     "seqno <%u,%u> fragno <%u,%u> tid %u",
1103                                     rxseq >> IEEE80211_SEQ_SEQ_SHIFT,
1104                                     ni->ni_rxseqs[tid] >>
1105                                     IEEE80211_SEQ_SEQ_SHIFT,
1106                                     rxseq & IEEE80211_SEQ_FRAG_MASK,
1107                                     ni->ni_rxseqs[tid] &
1108                                     IEEE80211_SEQ_FRAG_MASK,
1109                                     tid);
1110                                 vap->iv_stats.is_rx_dup++;
1111                                 IEEE80211_NODE_STAT(ni, rx_dup);
1112                                 goto out;
1113                         }
1114                         ni->ni_rxseqs[tid] = rxseq;
1115                 }
1116         }
1117 #ifdef IEEE80211_DEBUG
1118         /*
1119          * It's easier, but too expensive, to simulate different mesh
1120          * topologies by consulting the ACL policy very early, so do this
1121          * only under DEBUG.
1122          *
1123          * NB: this check is also done upon peering link initiation.
1124          */
1125         if (vap->iv_acl != NULL && !vap->iv_acl->iac_check(vap, wh->i_addr2)) {
1126                 IEEE80211_DISCARD(vap, IEEE80211_MSG_ACL,
1127                     wh, NULL, "%s", "disallowed by ACL");
1128                 vap->iv_stats.is_rx_acl++;
1129                 goto out;
1130         }
1131 #endif
1132         switch (type) {
1133         case IEEE80211_FC0_TYPE_DATA:
1134                 if (ni == vap->iv_bss)
1135                         goto out;
1136                 if (ni->ni_mlstate != IEEE80211_NODE_MESH_ESTABLISHED) {
1137                         IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_MESH,
1138                             ni->ni_macaddr, NULL,
1139                             "peer link not yet established (%d)",
1140                             ni->ni_mlstate);
1141                         vap->iv_stats.is_mesh_nolink++;
1142                         goto out;
1143                 }       
1144                 if (dir != IEEE80211_FC1_DIR_FROMDS &&
1145                     dir != IEEE80211_FC1_DIR_DSTODS) {
1146                         IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
1147                             wh, "data", "incorrect dir 0x%x", dir);
1148                         vap->iv_stats.is_rx_wrongdir++;
1149                         goto err;
1150                 }
1151                 /* pull up enough to get to the mesh control */
1152                 hdrspace = ieee80211_hdrspace(ic, wh);
1153                 if (m->m_len < hdrspace + sizeof(struct ieee80211_meshcntl) &&
1154                     (m = m_pullup(m, hdrspace +
1155                         sizeof(struct ieee80211_meshcntl))) == NULL) {
1156                         IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
1157                             ni->ni_macaddr, NULL,
1158                             "data too short: expecting %u", hdrspace);
1159                         vap->iv_stats.is_rx_tooshort++;
1160                         goto out;               /* XXX */
1161                 }
1162                 /*
1163                  * Now calculate the full extent of the headers. Note
1164                  * mesh_decap will pull up anything we didn't get
1165                  * above when it strips the 802.11 headers.
1166                  */
1167                 mc = (const struct ieee80211_meshcntl *)
1168                     (mtod(m, const uint8_t *) + hdrspace);
1169                 meshdrlen = sizeof(struct ieee80211_meshcntl) +
1170                     (mc->mc_flags & 3) * IEEE80211_ADDR_LEN;
1171                 hdrspace += meshdrlen;
1172                 seq = LE_READ_4(mc->mc_seq);
1173                 if (IEEE80211_IS_MULTICAST(wh->i_addr1))
1174                         addr = wh->i_addr3;
1175                 else
1176                         addr = ((struct ieee80211_qosframe_addr4 *)wh)->i_addr4;
1177                 if (IEEE80211_ADDR_EQ(vap->iv_myaddr, addr)) {
1178                         IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
1179                             addr, "data", "%s", "not to me");
1180                         vap->iv_stats.is_rx_wrongbss++; /* XXX kinda */
1181                         goto out;
1182                 }
1183                 if (mesh_checkpseq(vap, addr, seq) != 0) {
1184                         vap->iv_stats.is_rx_dup++;
1185                         goto out;
1186                 }
1187
1188                 /*
1189                  * Potentially forward packet.  See table s36 (p140)
1190                  * for the rules.  XXX tap fwd'd packets not for us?
1191                  */
1192                 if (dir == IEEE80211_FC1_DIR_FROMDS ||
1193                     !mesh_isucastforme(vap, wh, mc)) {
1194                         mesh_forward(vap, m, mc);
1195                         if (dir == IEEE80211_FC1_DIR_DSTODS)
1196                                 goto out;
1197                         /* NB: fall thru to deliver mcast frames locally */
1198                 }
1199
1200                 /*
1201                  * Save QoS bits for use below--before we strip the header.
1202                  */
1203                 if (subtype == IEEE80211_FC0_SUBTYPE_QOS) {
1204                         qos = (dir == IEEE80211_FC1_DIR_DSTODS) ?
1205                             ((struct ieee80211_qosframe_addr4 *)wh)->i_qos[0] :
1206                             ((struct ieee80211_qosframe *)wh)->i_qos[0];
1207                 } else
1208                         qos = 0;
1209                 /*
1210                  * Next up, any fragmentation.
1211                  */
1212                 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1213                         m = ieee80211_defrag(ni, m, hdrspace);
1214                         if (m == NULL) {
1215                                 /* Fragment dropped or frame not complete yet */
1216                                 goto out;
1217                         }
1218                 }
1219                 wh = NULL;              /* no longer valid, catch any uses */
1220
1221                 if (ieee80211_radiotap_active_vap(vap))
1222                         ieee80211_radiotap_rx(vap, m);
1223                 need_tap = 0;
1224
1225                 /*
1226                  * Finally, strip the 802.11 header.
1227                  */
1228                 m = mesh_decap(vap, m, hdrspace, meshdrlen);
1229                 if (m == NULL) {
1230                         /* XXX mask bit to check for both */
1231                         /* don't count Null data frames as errors */
1232                         if (subtype == IEEE80211_FC0_SUBTYPE_NODATA ||
1233                             subtype == IEEE80211_FC0_SUBTYPE_QOS_NULL)
1234                                 goto out;
1235                         IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_INPUT,
1236                             ni->ni_macaddr, "data", "%s", "decap error");
1237                         vap->iv_stats.is_rx_decap++;
1238                         IEEE80211_NODE_STAT(ni, rx_decap);
1239                         goto err;
1240                 }
1241                 if (qos & IEEE80211_QOS_AMSDU) {
1242                         m = ieee80211_decap_amsdu(ni, m);
1243                         if (m == NULL)
1244                                 return IEEE80211_FC0_TYPE_DATA;
1245                 }
1246                 ieee80211_deliver_data(vap, ni, m);
1247                 return type;
1248         case IEEE80211_FC0_TYPE_MGT:
1249                 vap->iv_stats.is_rx_mgmt++;
1250                 IEEE80211_NODE_STAT(ni, rx_mgmt);
1251                 if (dir != IEEE80211_FC1_DIR_NODS) {
1252                         IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
1253                             wh, "mgt", "incorrect dir 0x%x", dir);
1254                         vap->iv_stats.is_rx_wrongdir++;
1255                         goto err;
1256                 }
1257                 if (m->m_pkthdr.len < sizeof(struct ieee80211_frame)) {
1258                         IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
1259                             ni->ni_macaddr, "mgt", "too short: len %u",
1260                             m->m_pkthdr.len);
1261                         vap->iv_stats.is_rx_tooshort++;
1262                         goto out;
1263                 }
1264 #ifdef IEEE80211_DEBUG
1265                 if ((ieee80211_msg_debug(vap) && 
1266                     (vap->iv_ic->ic_flags & IEEE80211_F_SCAN)) ||
1267                     ieee80211_msg_dumppkts(vap)) {
1268                         if_printf(ifp, "received %s from %6D rssi %d\n",
1269                             ieee80211_mgt_subtype_name[subtype >>
1270                             IEEE80211_FC0_SUBTYPE_SHIFT],
1271                             wh->i_addr2, ":", rssi);
1272                 }
1273 #endif
1274                 if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
1275                         IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
1276                             wh, NULL, "%s", "WEP set but not permitted");
1277                         vap->iv_stats.is_rx_mgtdiscard++; /* XXX */
1278                         goto out;
1279                 }
1280                 vap->iv_recv_mgmt(ni, m, subtype, rssi, nf);
1281                 goto out;
1282         case IEEE80211_FC0_TYPE_CTL:
1283                 vap->iv_stats.is_rx_ctl++;
1284                 IEEE80211_NODE_STAT(ni, rx_ctrl);
1285                 goto out;
1286         default:
1287                 IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
1288                     wh, "bad", "frame type 0x%x", type);
1289                 /* should not come here */
1290                 break;
1291         }
1292 err:
1293         ifp->if_ierrors++;
1294 out:
1295         if (m != NULL) {
1296                 if (need_tap && ieee80211_radiotap_active_vap(vap))
1297                         ieee80211_radiotap_rx(vap, m);
1298                 m_freem(m);
1299         }
1300         return type;
1301 }
1302
1303 static void
1304 mesh_recv_mgmt(struct ieee80211_node *ni, struct mbuf *m0, int subtype,
1305     int rssi, int nf)
1306 {
1307         struct ieee80211vap *vap = ni->ni_vap;
1308         struct ieee80211_mesh_state *ms = vap->iv_mesh;
1309         struct ieee80211com *ic = ni->ni_ic;
1310         struct ieee80211_frame *wh;
1311         uint8_t *frm, *efrm;
1312
1313         wh = mtod(m0, struct ieee80211_frame *);
1314         frm = (uint8_t *)&wh[1];
1315         efrm = mtod(m0, uint8_t *) + m0->m_len;
1316         switch (subtype) {
1317         case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
1318         case IEEE80211_FC0_SUBTYPE_BEACON:
1319         {
1320                 struct ieee80211_scanparams scan;
1321                 /*
1322                  * We process beacon/probe response
1323                  * frames to discover neighbors.
1324                  */
1325                 if (ieee80211_parse_beacon(ni, m0, &scan) != 0)
1326                         return;
1327                 /*
1328                  * Count frame now that we know it's to be processed.
1329                  */
1330                 if (subtype == IEEE80211_FC0_SUBTYPE_BEACON) {
1331                         vap->iv_stats.is_rx_beacon++;   /* XXX remove */
1332                         IEEE80211_NODE_STAT(ni, rx_beacons);
1333                 } else
1334                         IEEE80211_NODE_STAT(ni, rx_proberesp);
1335                 /*
1336                  * If scanning, just pass information to the scan module.
1337                  */
1338                 if (ic->ic_flags & IEEE80211_F_SCAN) {
1339                         if (ic->ic_flags_ext & IEEE80211_FEXT_PROBECHAN) {
1340                                 /*
1341                                  * Actively scanning a channel marked passive;
1342                                  * send a probe request now that we know there
1343                                  * is 802.11 traffic present.
1344                                  *
1345                                  * XXX check if the beacon we recv'd gives
1346                                  * us what we need and suppress the probe req
1347                                  */
1348                                 ieee80211_probe_curchan(vap, 1);
1349                                 ic->ic_flags_ext &= ~IEEE80211_FEXT_PROBECHAN;
1350                         }
1351                         ieee80211_add_scan(vap, &scan, wh,
1352                             subtype, rssi, nf);
1353                         return;
1354                 }
1355
1356                 /* The rest of this code assumes we are running */
1357                 if (vap->iv_state != IEEE80211_S_RUN)
1358                         return;
1359                 /*
1360                  * Ignore non-mesh STAs.
1361                  */
1362                 if ((scan.capinfo &
1363                      (IEEE80211_CAPINFO_ESS|IEEE80211_CAPINFO_IBSS)) ||
1364                     scan.meshid == NULL || scan.meshconf == NULL) {
1365                         IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
1366                             wh, "beacon", "%s", "not a mesh sta");
1367                         vap->iv_stats.is_mesh_wrongmesh++;
1368                         return;
1369                 }
1370                 /*
1371                  * Ignore STAs for other mesh networks.
1372                  */
1373                 if (memcmp(scan.meshid+2, ms->ms_id, ms->ms_idlen) != 0 ||
1374                     mesh_verify_meshconf(vap, scan.meshconf)) {
1375                         IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
1376                             wh, "beacon", "%s", "not for our mesh");
1377                         vap->iv_stats.is_mesh_wrongmesh++;
1378                         return;
1379                 }
1380                 /*
1381                  * Peer only based on the current ACL policy.
1382                  */
1383                 if (vap->iv_acl != NULL &&
1384                     !vap->iv_acl->iac_check(vap, wh->i_addr2)) {
1385                         IEEE80211_DISCARD(vap, IEEE80211_MSG_ACL,
1386                             wh, NULL, "%s", "disallowed by ACL");
1387                         vap->iv_stats.is_rx_acl++;
1388                         return;
1389                 }
1390                 /*
1391                  * Do neighbor discovery.
1392                  */
1393                 if (!IEEE80211_ADDR_EQ(wh->i_addr2, ni->ni_macaddr)) {
1394                         /*
1395                          * Create a new entry in the neighbor table.
1396                          */
1397                         ni = ieee80211_add_neighbor(vap, wh, &scan);
1398                 }
1399                 /*
1400                  * Automatically peer with discovered nodes if possible.
1401                  * XXX backoff on repeated failure
1402                  */
1403                 if (ni != vap->iv_bss &&
1404                     (ms->ms_flags & IEEE80211_MESHFLAGS_AP) &&
1405                     ni->ni_mlstate == IEEE80211_NODE_MESH_IDLE) {
1406                         uint16_t args[1];
1407
1408                         ni->ni_mlpid = mesh_generateid(vap);
1409                         if (ni->ni_mlpid == 0)
1410                                 return;
1411                         mesh_linkchange(ni, IEEE80211_NODE_MESH_OPENSNT);
1412                         args[0] = ni->ni_mlpid;
1413                         ieee80211_send_action(ni,
1414                             IEEE80211_ACTION_CAT_MESHPEERING,
1415                             IEEE80211_ACTION_MESHPEERING_OPEN, args);
1416                         ni->ni_mlrcnt = 0;
1417                         mesh_peer_timeout_setup(ni);
1418                 }
1419                 break;
1420         }
1421         case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
1422         {
1423                 uint8_t *ssid, *meshid, *rates, *xrates;
1424                 uint8_t *sfrm;
1425
1426                 if (vap->iv_state != IEEE80211_S_RUN) {
1427                         IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
1428                             wh, NULL, "wrong state %s",
1429                             ieee80211_state_name[vap->iv_state]);
1430                         vap->iv_stats.is_rx_mgtdiscard++;
1431                         return;
1432                 }
1433                 if (IEEE80211_IS_MULTICAST(wh->i_addr2)) {
1434                         /* frame must be directed */
1435                         IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
1436                             wh, NULL, "%s", "not unicast");
1437                         vap->iv_stats.is_rx_mgtdiscard++;       /* XXX stat */
1438                         return;
1439                 }
1440                 /*
1441                  * prreq frame format
1442                  *      [tlv] ssid
1443                  *      [tlv] supported rates
1444                  *      [tlv] extended supported rates
1445                  *      [tlv] mesh id
1446                  */
1447                 ssid = meshid = rates = xrates = NULL;
1448                 sfrm = frm;
1449                 while (efrm - frm > 1) {
1450                         IEEE80211_VERIFY_LENGTH(efrm - frm, frm[1] + 2, return);
1451                         switch (*frm) {
1452                         case IEEE80211_ELEMID_SSID:
1453                                 ssid = frm;
1454                                 break;
1455                         case IEEE80211_ELEMID_RATES:
1456                                 rates = frm;
1457                                 break;
1458                         case IEEE80211_ELEMID_XRATES:
1459                                 xrates = frm;
1460                                 break;
1461                         case IEEE80211_ELEMID_MESHID:
1462                                 meshid = frm;
1463                                 break;
1464                         }
1465                         frm += frm[2] + 2;
1466                 }
1467                 IEEE80211_VERIFY_ELEMENT(ssid, IEEE80211_NWID_LEN, return);
1468                 IEEE80211_VERIFY_ELEMENT(rates, IEEE80211_RATE_MAXSIZE, return);
1469                 if (xrates != NULL)
1470                         IEEE80211_VERIFY_ELEMENT(xrates,
1471                             IEEE80211_RATE_MAXSIZE - rates[1], return);
1472                 if (meshid != NULL) {
1473                         IEEE80211_VERIFY_ELEMENT(meshid,
1474                             IEEE80211_MESHID_LEN, return);
1475                         /* NB: meshid, not ssid */
1476                         IEEE80211_VERIFY_SSID(vap->iv_bss, meshid, return);
1477                 }
1478
1479                 /* XXX find a better class or define it's own */
1480                 IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_INPUT, wh->i_addr2,
1481                     "%s", "recv probe req");
1482                 /*
1483                  * Some legacy 11b clients cannot hack a complete
1484                  * probe response frame.  When the request includes
1485                  * only a bare-bones rate set, communicate this to
1486                  * the transmit side.
1487                  */
1488                 ieee80211_send_proberesp(vap, wh->i_addr2, 0);
1489                 break;
1490         }
1491         case IEEE80211_FC0_SUBTYPE_ACTION:
1492                 if (vap->iv_state != IEEE80211_S_RUN) {
1493                         vap->iv_stats.is_rx_mgtdiscard++;
1494                         break;
1495                 }
1496                 /*
1497                  * We received an action for an unknown neighbor.
1498                  * XXX: wait for it to beacon or create ieee80211_node?
1499                  */
1500                 if (ni == vap->iv_bss) {
1501                         IEEE80211_DISCARD(vap, IEEE80211_MSG_MESH,
1502                             wh, NULL, "%s", "unknown node");
1503                         vap->iv_stats.is_rx_mgtdiscard++;
1504                         break;
1505                 }
1506                 /*
1507                  * Discard if not for us.
1508                  */
1509                 if (!IEEE80211_ADDR_EQ(vap->iv_myaddr, wh->i_addr1) &&
1510                     !IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1511                         IEEE80211_DISCARD(vap, IEEE80211_MSG_MESH,
1512                             wh, NULL, "%s", "not for me");
1513                         vap->iv_stats.is_rx_mgtdiscard++;
1514                         break;
1515                 }
1516                 /* XXX parse_action is a bit useless now */
1517                 if (ieee80211_parse_action(ni, m0) == 0)
1518                         ic->ic_recv_action(ni, wh, frm, efrm);
1519                 break;
1520         case IEEE80211_FC0_SUBTYPE_AUTH:
1521         case IEEE80211_FC0_SUBTYPE_ASSOC_REQ:
1522         case IEEE80211_FC0_SUBTYPE_REASSOC_REQ:
1523         case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
1524         case IEEE80211_FC0_SUBTYPE_REASSOC_RESP:
1525         case IEEE80211_FC0_SUBTYPE_DEAUTH:
1526         case IEEE80211_FC0_SUBTYPE_DISASSOC:
1527                 IEEE80211_DISCARD(vap, IEEE80211_MSG_INPUT,
1528                     wh, NULL, "%s", "not handled");
1529                 vap->iv_stats.is_rx_mgtdiscard++;
1530                 return;
1531         default:
1532                 IEEE80211_DISCARD(vap, IEEE80211_MSG_ANY,
1533                     wh, "mgt", "subtype 0x%x not handled", subtype);
1534                 vap->iv_stats.is_rx_badsubtype++;
1535                 break;
1536         }
1537 }
1538
1539 /*
1540  * Parse meshpeering action ie's for open+confirm frames; the
1541  * important bits are returned in the supplied structure.
1542  */
1543 static const struct ieee80211_meshpeer_ie *
1544 mesh_parse_meshpeering_action(struct ieee80211_node *ni,
1545         const struct ieee80211_frame *wh,       /* XXX for VERIFY_LENGTH */
1546         const uint8_t *frm, const uint8_t *efrm,
1547         struct ieee80211_meshpeer_ie *mp, uint8_t subtype)
1548 {
1549         struct ieee80211vap *vap = ni->ni_vap;
1550         const struct ieee80211_meshpeer_ie *mpie;
1551         const uint8_t *meshid, *meshconf, *meshpeer;
1552
1553         meshid = meshconf = meshpeer = NULL;
1554         while (efrm - frm > 1) {
1555                 IEEE80211_VERIFY_LENGTH(efrm - frm, frm[1] + 2, return NULL);
1556                 switch (*frm) {
1557                 case IEEE80211_ELEMID_MESHID:
1558                         meshid = frm;
1559                         break;
1560                 case IEEE80211_ELEMID_MESHCONF:
1561                         meshconf = frm;
1562                         break;
1563                 case IEEE80211_ELEMID_MESHPEER:
1564                         meshpeer = frm;
1565                         mpie = (const struct ieee80211_meshpeer_ie *) frm;
1566                         memset(mp, 0, sizeof(*mp));
1567                         mp->peer_llinkid = LE_READ_2(&mpie->peer_llinkid);
1568                         /* NB: peer link ID is optional on these frames */
1569                         if (subtype == IEEE80211_MESH_PEER_LINK_CLOSE &&
1570                             mpie->peer_len == 8) {
1571                                 mp->peer_linkid = 0;
1572                                 mp->peer_rcode = LE_READ_2(&mpie->peer_linkid);
1573                         } else {
1574                                 mp->peer_linkid = LE_READ_2(&mpie->peer_linkid);
1575                                 mp->peer_rcode = LE_READ_2(&mpie->peer_rcode);
1576                         }
1577                         break;
1578                 }
1579                 frm += frm[1] + 2;
1580         }
1581
1582         /*
1583          * Verify the contents of the frame. Action frames with
1584          * close subtype don't have a Mesh Configuration IE.
1585          * If if fails validation, close the peer link.
1586          */
1587         KASSERT(meshpeer != NULL &&
1588             subtype != IEEE80211_ACTION_MESHPEERING_CLOSE,
1589             ("parsing close action"));
1590
1591         if (mesh_verify_meshid(vap, meshid) ||
1592             mesh_verify_meshpeer(vap, subtype, meshpeer) ||
1593             mesh_verify_meshconf(vap, meshconf)) {
1594                 uint16_t args[3];
1595
1596                 IEEE80211_DISCARD(vap,
1597                     IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH,
1598                     wh, NULL, "%s", "not for our mesh");
1599                 vap->iv_stats.is_rx_mgtdiscard++;
1600                 switch (ni->ni_mlstate) {
1601                 case IEEE80211_NODE_MESH_IDLE:
1602                 case IEEE80211_NODE_MESH_ESTABLISHED:
1603                 case IEEE80211_NODE_MESH_HOLDING:
1604                         /* ignore */
1605                         break;
1606                 case IEEE80211_NODE_MESH_OPENSNT:
1607                 case IEEE80211_NODE_MESH_OPENRCV:
1608                 case IEEE80211_NODE_MESH_CONFIRMRCV:
1609                         args[0] = ni->ni_mlpid;
1610                         args[1] = ni->ni_mllid;
1611                         args[2] = IEEE80211_REASON_PEER_LINK_CANCELED;
1612                         ieee80211_send_action(ni,
1613                             IEEE80211_ACTION_CAT_MESHPEERING,
1614                             IEEE80211_ACTION_MESHPEERING_CLOSE,
1615                             args);
1616                         mesh_linkchange(ni, IEEE80211_NODE_MESH_HOLDING);
1617                         mesh_peer_timeout_setup(ni);
1618                         break;
1619                 }
1620                 return NULL;
1621         }
1622         return (const struct ieee80211_meshpeer_ie *) mp;
1623 }
1624
1625 static int
1626 mesh_recv_action_meshpeering_open(struct ieee80211_node *ni,
1627         const struct ieee80211_frame *wh,
1628         const uint8_t *frm, const uint8_t *efrm)
1629 {
1630         struct ieee80211vap *vap = ni->ni_vap;
1631         struct ieee80211_meshpeer_ie ie;
1632         const struct ieee80211_meshpeer_ie *meshpeer;
1633         uint16_t args[3];
1634
1635         /* +2+2 for action + code + capabilites */
1636         meshpeer = mesh_parse_meshpeering_action(ni, wh, frm+2+2, efrm, &ie,
1637             IEEE80211_ACTION_MESHPEERING_OPEN);
1638         if (meshpeer == NULL) {
1639                 return 0;
1640         }
1641
1642         /* XXX move up */
1643         IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH, ni,
1644             "recv PEER OPEN, lid 0x%x", meshpeer->peer_llinkid);
1645
1646         switch (ni->ni_mlstate) {
1647         case IEEE80211_NODE_MESH_IDLE:
1648                 mesh_linkchange(ni, IEEE80211_NODE_MESH_OPENRCV);
1649                 ni->ni_mllid = meshpeer->peer_llinkid;
1650                 ni->ni_mlpid = mesh_generateid(vap);
1651                 if (ni->ni_mlpid == 0)
1652                         return 0;               /* XXX */
1653                 args[0] = ni->ni_mlpid;
1654                 /* Announce we're open too... */
1655                 ieee80211_send_action(ni,
1656                     IEEE80211_ACTION_CAT_MESHPEERING,
1657                     IEEE80211_ACTION_MESHPEERING_OPEN, args);
1658                 /* ...and confirm the link. */
1659                 args[0] = ni->ni_mlpid;
1660                 args[1] = ni->ni_mllid;
1661                 ieee80211_send_action(ni,
1662                     IEEE80211_ACTION_CAT_MESHPEERING,
1663                     IEEE80211_ACTION_MESHPEERING_CONFIRM,
1664                     args);
1665                 mesh_peer_timeout_setup(ni);
1666                 break;
1667         case IEEE80211_NODE_MESH_OPENRCV:
1668                 /* Wrong Link ID */
1669                 if (ni->ni_mllid != meshpeer->peer_llinkid) {
1670                         args[0] = ni->ni_mllid;
1671                         args[1] = ni->ni_mlpid;
1672                         args[2] = IEEE80211_REASON_PEER_LINK_CANCELED;
1673                         ieee80211_send_action(ni,
1674                             IEEE80211_ACTION_CAT_MESHPEERING,
1675                             IEEE80211_ACTION_MESHPEERING_CLOSE,
1676                             args);
1677                         mesh_linkchange(ni, IEEE80211_NODE_MESH_HOLDING);
1678                         mesh_peer_timeout_setup(ni);
1679                         break;
1680                 }
1681                 /* Duplicate open, confirm again. */
1682                 args[0] = ni->ni_mlpid;
1683                 args[1] = ni->ni_mllid;
1684                 ieee80211_send_action(ni,
1685                     IEEE80211_ACTION_CAT_MESHPEERING,
1686                     IEEE80211_ACTION_MESHPEERING_CONFIRM,
1687                     args);
1688                 break;
1689         case IEEE80211_NODE_MESH_OPENSNT:
1690                 ni->ni_mllid = meshpeer->peer_llinkid;
1691                 mesh_linkchange(ni, IEEE80211_NODE_MESH_OPENRCV);
1692                 args[0] = ni->ni_mlpid;
1693                 args[1] = ni->ni_mllid;
1694                 ieee80211_send_action(ni,
1695                     IEEE80211_ACTION_CAT_MESHPEERING,
1696                     IEEE80211_ACTION_MESHPEERING_CONFIRM,
1697                     args);
1698                 /* NB: don't setup/clear any timeout */
1699                 break;
1700         case IEEE80211_NODE_MESH_CONFIRMRCV:
1701                 if (ni->ni_mlpid != meshpeer->peer_linkid ||
1702                     ni->ni_mllid != meshpeer->peer_llinkid) {
1703                         args[0] = ni->ni_mlpid;
1704                         args[1] = ni->ni_mllid;
1705                         args[2] = IEEE80211_REASON_PEER_LINK_CANCELED;
1706                         ieee80211_send_action(ni,
1707                             IEEE80211_ACTION_CAT_MESHPEERING,
1708                             IEEE80211_ACTION_MESHPEERING_CLOSE,
1709                             args);
1710                         mesh_linkchange(ni,
1711                             IEEE80211_NODE_MESH_HOLDING);
1712                         mesh_peer_timeout_setup(ni);
1713                         break;
1714                 }
1715                 mesh_linkchange(ni, IEEE80211_NODE_MESH_ESTABLISHED);
1716                 ni->ni_mllid = meshpeer->peer_llinkid;
1717                 args[0] = ni->ni_mlpid;
1718                 args[1] = ni->ni_mllid;
1719                 ieee80211_send_action(ni,
1720                     IEEE80211_ACTION_CAT_MESHPEERING,
1721                     IEEE80211_ACTION_MESHPEERING_CONFIRM,
1722                     args);
1723                 mesh_peer_timeout_stop(ni);
1724                 break;
1725         case IEEE80211_NODE_MESH_ESTABLISHED:
1726                 if (ni->ni_mllid != meshpeer->peer_llinkid) {
1727                         args[0] = ni->ni_mllid;
1728                         args[1] = ni->ni_mlpid;
1729                         args[2] = IEEE80211_REASON_PEER_LINK_CANCELED;
1730                         ieee80211_send_action(ni,
1731                             IEEE80211_ACTION_CAT_MESHPEERING,
1732                             IEEE80211_ACTION_MESHPEERING_CLOSE,
1733                             args);
1734                         mesh_linkchange(ni, IEEE80211_NODE_MESH_HOLDING);
1735                         mesh_peer_timeout_setup(ni);
1736                         break;
1737                 }
1738                 args[0] = ni->ni_mlpid;
1739                 args[1] = ni->ni_mllid;
1740                 ieee80211_send_action(ni,
1741                     IEEE80211_ACTION_CAT_MESHPEERING,
1742                     IEEE80211_ACTION_MESHPEERING_CONFIRM,
1743                     args);
1744                 break;
1745         case IEEE80211_NODE_MESH_HOLDING:
1746                 args[0] = ni->ni_mlpid;
1747                 args[1] = meshpeer->peer_llinkid;
1748                 args[2] = IEEE80211_REASON_MESH_MAX_RETRIES;
1749                 ieee80211_send_action(ni,
1750                     IEEE80211_ACTION_CAT_MESHPEERING,
1751                     IEEE80211_ACTION_MESHPEERING_CLOSE,
1752                     args);
1753                 break;
1754         }
1755         return 0;
1756 }
1757
1758 static int
1759 mesh_recv_action_meshpeering_confirm(struct ieee80211_node *ni,
1760         const struct ieee80211_frame *wh,
1761         const uint8_t *frm, const uint8_t *efrm)
1762 {
1763         struct ieee80211vap *vap = ni->ni_vap;
1764         struct ieee80211_meshpeer_ie ie;
1765         const struct ieee80211_meshpeer_ie *meshpeer;
1766         uint16_t args[3];
1767
1768         /* +2+2+2+2 for action + code + capabilites + status code + AID */
1769         meshpeer = mesh_parse_meshpeering_action(ni, wh, frm+2+2+2+2, efrm, &ie,
1770             IEEE80211_ACTION_MESHPEERING_CONFIRM);
1771         if (meshpeer == NULL) {
1772                 return 0;
1773         }
1774
1775         IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH, ni,
1776             "recv PEER CONFIRM, local id 0x%x, peer id 0x%x",
1777             meshpeer->peer_llinkid, meshpeer->peer_linkid);
1778
1779         switch (ni->ni_mlstate) {
1780         case IEEE80211_NODE_MESH_OPENRCV:
1781                 mesh_linkchange(ni, IEEE80211_NODE_MESH_ESTABLISHED);
1782                 mesh_peer_timeout_stop(ni);
1783                 break;
1784         case IEEE80211_NODE_MESH_OPENSNT:
1785                 mesh_linkchange(ni, IEEE80211_NODE_MESH_CONFIRMRCV);
1786                 break;
1787         case IEEE80211_NODE_MESH_HOLDING:
1788                 args[0] = ni->ni_mlpid;
1789                 args[1] = meshpeer->peer_llinkid;
1790                 args[2] = IEEE80211_REASON_MESH_MAX_RETRIES;
1791                 ieee80211_send_action(ni,
1792                     IEEE80211_ACTION_CAT_MESHPEERING,
1793                     IEEE80211_ACTION_MESHPEERING_CLOSE,
1794                     args);
1795                 break;
1796         case IEEE80211_NODE_MESH_CONFIRMRCV:
1797                 if (ni->ni_mllid != meshpeer->peer_llinkid) {
1798                         args[0] = ni->ni_mlpid;
1799                         args[1] = ni->ni_mllid;
1800                         args[2] = IEEE80211_REASON_PEER_LINK_CANCELED;
1801                         ieee80211_send_action(ni,
1802                             IEEE80211_ACTION_CAT_MESHPEERING,
1803                             IEEE80211_ACTION_MESHPEERING_CLOSE,
1804                             args);
1805                         mesh_linkchange(ni, IEEE80211_NODE_MESH_HOLDING);
1806                         mesh_peer_timeout_setup(ni);
1807                 }
1808                 break;
1809         default:
1810                 IEEE80211_DISCARD(vap,
1811                     IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH,
1812                     wh, NULL, "received confirm in invalid state %d",
1813                     ni->ni_mlstate);
1814                 vap->iv_stats.is_rx_mgtdiscard++;
1815                 break;
1816         }
1817         return 0;
1818 }
1819
1820 static int
1821 mesh_recv_action_meshpeering_close(struct ieee80211_node *ni,
1822         const struct ieee80211_frame *wh,
1823         const uint8_t *frm, const uint8_t *efrm)
1824 {
1825         uint16_t args[3];
1826
1827         IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH,
1828             ni, "%s", "recv PEER CLOSE");
1829
1830         switch (ni->ni_mlstate) {
1831         case IEEE80211_NODE_MESH_IDLE:
1832                 /* ignore */
1833                 break;
1834         case IEEE80211_NODE_MESH_OPENRCV:
1835         case IEEE80211_NODE_MESH_OPENSNT:
1836         case IEEE80211_NODE_MESH_CONFIRMRCV:
1837         case IEEE80211_NODE_MESH_ESTABLISHED:
1838                 args[0] = ni->ni_mlpid;
1839                 args[1] = ni->ni_mllid;
1840                 args[2] = IEEE80211_REASON_MESH_CLOSE_RCVD;
1841                 ieee80211_send_action(ni,
1842                     IEEE80211_ACTION_CAT_MESHPEERING,
1843                     IEEE80211_ACTION_MESHPEERING_CLOSE,
1844                     args);
1845                 mesh_linkchange(ni, IEEE80211_NODE_MESH_HOLDING);
1846                 mesh_peer_timeout_setup(ni);
1847                 break;
1848         case IEEE80211_NODE_MESH_HOLDING:
1849                 mesh_linkchange(ni, IEEE80211_NODE_MESH_IDLE);
1850                 mesh_peer_timeout_setup(ni);
1851                 break;
1852         }
1853         return 0;
1854 }
1855
1856 /*
1857  * Link Metric handling.
1858  */
1859 static int
1860 mesh_recv_action_meshlmetric_req(struct ieee80211_node *ni,
1861         const struct ieee80211_frame *wh,
1862         const uint8_t *frm, const uint8_t *efrm)
1863 {
1864         uint32_t metric;
1865
1866         metric = mesh_airtime_calc(ni);
1867         ieee80211_send_action(ni,
1868             IEEE80211_ACTION_CAT_MESHLMETRIC,
1869             IEEE80211_ACTION_MESHLMETRIC_REP,
1870             &metric);
1871         return 0;
1872 }
1873
1874 static int
1875 mesh_recv_action_meshlmetric_rep(struct ieee80211_node *ni,
1876         const struct ieee80211_frame *wh,
1877         const uint8_t *frm, const uint8_t *efrm)
1878 {
1879         return 0;
1880 }
1881
1882 static int
1883 mesh_send_action(struct ieee80211_node *ni, struct mbuf *m)
1884 {
1885         struct ieee80211_bpf_params params;
1886
1887         memset(&params, 0, sizeof(params));
1888         params.ibp_pri = WME_AC_VO;
1889         params.ibp_rate0 = ni->ni_txparms->mgmtrate;
1890         /* XXX ucast/mcast */
1891         params.ibp_try0 = ni->ni_txparms->maxretry;
1892         params.ibp_power = ni->ni_txpower;
1893         return ieee80211_mgmt_output(ni, m, IEEE80211_FC0_SUBTYPE_ACTION,
1894              &params);
1895 }
1896
1897 #define ADDSHORT(frm, v) do {                   \
1898         frm[0] = (v) & 0xff;                    \
1899         frm[1] = (v) >> 8;                      \
1900         frm += 2;                               \
1901 } while (0)
1902 #define ADDWORD(frm, v) do {                    \
1903         frm[0] = (v) & 0xff;                    \
1904         frm[1] = ((v) >> 8) & 0xff;             \
1905         frm[2] = ((v) >> 16) & 0xff;            \
1906         frm[3] = ((v) >> 24) & 0xff;            \
1907         frm += 4;                               \
1908 } while (0)
1909
1910 static int
1911 mesh_send_action_meshpeering_open(struct ieee80211_node *ni,
1912         int category, int action, void *args0)
1913 {
1914         struct ieee80211vap *vap = ni->ni_vap;
1915         struct ieee80211com *ic = ni->ni_ic;
1916         uint16_t *args = args0;
1917         const struct ieee80211_rateset *rs;
1918         struct mbuf *m;
1919         uint8_t *frm;
1920
1921         IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH, ni,
1922             "send PEER OPEN action: localid 0x%x", args[0]);
1923
1924         IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
1925             "ieee80211_ref_node (%s:%u) %p<%6D> refcnt %d\n", __func__, __LINE__,
1926             ni, ni->ni_macaddr, ":", ieee80211_node_refcnt(ni)+1);
1927         ieee80211_ref_node(ni);
1928
1929         m = ieee80211_getmgtframe(&frm,
1930             ic->ic_headroom + sizeof(struct ieee80211_frame),
1931             sizeof(uint16_t)    /* action+category */
1932             + sizeof(uint16_t)  /* capabilites */
1933             + 2 + IEEE80211_RATE_SIZE    
1934             + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)         
1935             + 2 + IEEE80211_MESHID_LEN
1936             + sizeof(struct ieee80211_meshconf_ie)       
1937             + sizeof(struct ieee80211_meshpeer_ie)
1938         );
1939         if (m != NULL) {
1940                 /*
1941                  * mesh peer open action frame format:
1942                  *   [1] category
1943                  *   [1] action
1944                  *   [2] capabilities
1945                  *   [tlv] rates
1946                  *   [tlv] xrates
1947                  *   [tlv] mesh id
1948                  *   [tlv] mesh conf
1949                  *   [tlv] mesh peer link mgmt
1950                  */
1951                 *frm++ = category;
1952                 *frm++ = action;
1953                 ADDSHORT(frm, ieee80211_getcapinfo(vap, ni->ni_chan));
1954                 rs = ieee80211_get_suprates(ic, ic->ic_curchan);
1955                 frm = ieee80211_add_rates(frm, rs);
1956                 frm = ieee80211_add_xrates(frm, rs);
1957                 frm = ieee80211_add_meshid(frm, vap);
1958                 frm = ieee80211_add_meshconf(frm, vap);
1959                 frm = ieee80211_add_meshpeer(frm, IEEE80211_MESH_PEER_LINK_OPEN,
1960                     args[0], 0, 0);
1961                 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
1962                 return mesh_send_action(ni, m);
1963         } else {
1964                 vap->iv_stats.is_tx_nobuf++;
1965                 ieee80211_free_node(ni);
1966                 return ENOMEM;
1967         }
1968 }
1969
1970 static int
1971 mesh_send_action_meshpeering_confirm(struct ieee80211_node *ni,
1972         int category, int action, void *args0)
1973 {
1974         struct ieee80211vap *vap = ni->ni_vap;
1975         struct ieee80211com *ic = ni->ni_ic;
1976         uint16_t *args = args0;
1977         const struct ieee80211_rateset *rs;
1978         struct mbuf *m;
1979         uint8_t *frm;
1980
1981         IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH, ni,
1982             "send PEER CONFIRM action: localid 0x%x, peerid 0x%x",
1983             args[0], args[1]);
1984
1985         IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
1986             "ieee80211_ref_node (%s:%u) %p<%6D> refcnt %d\n", __func__, __LINE__,
1987             ni, ni->ni_macaddr, ":", ieee80211_node_refcnt(ni)+1);
1988         ieee80211_ref_node(ni);
1989
1990         m = ieee80211_getmgtframe(&frm,
1991             ic->ic_headroom + sizeof(struct ieee80211_frame),
1992             sizeof(uint16_t)    /* action+category */
1993             + sizeof(uint16_t)  /* capabilites */
1994             + sizeof(uint16_t)  /* status code */
1995             + sizeof(uint16_t)  /* AID */
1996             + 2 + IEEE80211_RATE_SIZE    
1997             + 2 + (IEEE80211_RATE_MAXSIZE - IEEE80211_RATE_SIZE)         
1998             + 2 + IEEE80211_MESHID_LEN
1999             + sizeof(struct ieee80211_meshconf_ie)       
2000             + sizeof(struct ieee80211_meshpeer_ie)
2001         );
2002         if (m != NULL) {
2003                 /*
2004                  * mesh peer confirm action frame format:
2005                  *   [1] category
2006                  *   [1] action
2007                  *   [2] capabilities
2008                  *   [2] status code
2009                  *   [2] association id (peer ID)
2010                  *   [tlv] rates
2011                  *   [tlv] xrates
2012                  *   [tlv] mesh id
2013                  *   [tlv] mesh conf
2014                  *   [tlv] mesh peer link mgmt
2015                  */
2016                 *frm++ = category;
2017                 *frm++ = action;
2018                 ADDSHORT(frm, ieee80211_getcapinfo(vap, ni->ni_chan));
2019                 ADDSHORT(frm, 0);               /* status code */
2020                 ADDSHORT(frm, args[1]);         /* AID */
2021                 rs = ieee80211_get_suprates(ic, ic->ic_curchan);
2022                 frm = ieee80211_add_rates(frm, rs);
2023                 frm = ieee80211_add_xrates(frm, rs);
2024                 frm = ieee80211_add_meshid(frm, vap);
2025                 frm = ieee80211_add_meshconf(frm, vap);
2026                 frm = ieee80211_add_meshpeer(frm,
2027                     IEEE80211_MESH_PEER_LINK_CONFIRM,
2028                     args[0], args[1], 0);
2029                 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2030                 return mesh_send_action(ni, m);
2031         } else {
2032                 vap->iv_stats.is_tx_nobuf++;
2033                 ieee80211_free_node(ni);
2034                 return ENOMEM;
2035         }
2036 }
2037
2038 static int
2039 mesh_send_action_meshpeering_close(struct ieee80211_node *ni,
2040         int category, int action, void *args0)
2041 {
2042         struct ieee80211vap *vap = ni->ni_vap;
2043         struct ieee80211com *ic = ni->ni_ic;
2044         uint16_t *args = args0;
2045         struct mbuf *m;
2046         uint8_t *frm;
2047
2048         IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH, ni,
2049             "send PEER CLOSE action: localid 0x%x, peerid 0x%x reason %d",
2050             args[0], args[1], args[2]);
2051
2052         IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
2053             "ieee80211_ref_node (%s:%u) %p<%6D> refcnt %d\n", __func__, __LINE__,
2054             ni, ni->ni_macaddr, ieee80211_node_refcnt(ni)+1);
2055         ieee80211_ref_node(ni);
2056
2057         m = ieee80211_getmgtframe(&frm,
2058             ic->ic_headroom + sizeof(struct ieee80211_frame),
2059             sizeof(uint16_t)    /* action+category */
2060             + sizeof(uint16_t)  /* reason code */
2061             + 2 + IEEE80211_MESHID_LEN
2062             + sizeof(struct ieee80211_meshpeer_ie) 
2063         );
2064         if (m != NULL) {
2065                 /*
2066                  * mesh peer close action frame format:
2067                  *   [1] category
2068                  *   [1] action
2069                  *   [2] reason code
2070                  *   [tlv] mesh id
2071                  *   [tlv] mesh peer link mgmt
2072                  */
2073                 *frm++ = category;
2074                 *frm++ = action;
2075                 ADDSHORT(frm, args[2]);         /* reason code */
2076                 frm = ieee80211_add_meshid(frm, vap);
2077                 frm = ieee80211_add_meshpeer(frm,
2078                     IEEE80211_MESH_PEER_LINK_CLOSE,
2079                     args[0], args[1], args[2]);
2080                 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2081                 return mesh_send_action(ni, m);
2082         } else {
2083                 vap->iv_stats.is_tx_nobuf++;
2084                 ieee80211_free_node(ni);
2085                 return ENOMEM;
2086         }
2087 }
2088
2089 static int
2090 mesh_send_action_meshlink_request(struct ieee80211_node *ni,
2091         int category, int action, void *arg0)
2092 {
2093         struct ieee80211vap *vap = ni->ni_vap;
2094         struct ieee80211com *ic = ni->ni_ic;
2095         struct mbuf *m;
2096         uint8_t *frm;
2097
2098         IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH, ni,
2099             "%s", "send LINK METRIC REQUEST action");
2100
2101         IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
2102             "ieee80211_ref_node (%s:%u) %p<%6D> refcnt %d\n", __func__, __LINE__,
2103             ni, ni->ni_macaddr, ":", ieee80211_node_refcnt(ni)+1);
2104         ieee80211_ref_node(ni);
2105
2106         m = ieee80211_getmgtframe(&frm,
2107             ic->ic_headroom + sizeof(struct ieee80211_frame),
2108             sizeof(uint16_t)    /* action+category */
2109         );
2110         if (m != NULL) {
2111                 /*
2112                  * mesh link metric request
2113                  *   [1] category
2114                  *   [1] action
2115                  */
2116                 *frm++ = category;
2117                 *frm++ = action;
2118                 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2119                 return mesh_send_action(ni, m);
2120         } else {
2121                 vap->iv_stats.is_tx_nobuf++;
2122                 ieee80211_free_node(ni);
2123                 return ENOMEM;
2124         }
2125 }
2126
2127 static int
2128 mesh_send_action_meshlink_reply(struct ieee80211_node *ni,
2129         int category, int action, void *args0)
2130 {
2131         struct ieee80211vap *vap = ni->ni_vap;
2132         struct ieee80211com *ic = ni->ni_ic;
2133         uint32_t *metric = args0;
2134         struct mbuf *m;
2135         uint8_t *frm;
2136
2137         IEEE80211_NOTE(vap, IEEE80211_MSG_ACTION | IEEE80211_MSG_MESH, ni,
2138             "send LINK METRIC REPLY action: metric 0x%x", *metric);
2139
2140         IEEE80211_DPRINTF(vap, IEEE80211_MSG_NODE,
2141             "ieee80211_ref_node (%s:%u) %p<%6D> refcnt %d\n", __func__, __LINE__,
2142             ni, ni->ni_macaddr, ":", ieee80211_node_refcnt(ni)+1);
2143         ieee80211_ref_node(ni);
2144
2145         m = ieee80211_getmgtframe(&frm,
2146             ic->ic_headroom + sizeof(struct ieee80211_frame),
2147             sizeof(uint16_t)    /* action+category */
2148             + sizeof(struct ieee80211_meshlmetric_ie)
2149         );
2150         if (m != NULL) {
2151                 /*
2152                  * mesh link metric reply
2153                  *   [1] category
2154                  *   [1] action
2155                  *   [tlv] mesh link metric
2156                  */
2157                 *frm++ = category;
2158                 *frm++ = action;
2159                 frm = ieee80211_add_meshlmetric(frm, *metric);
2160                 m->m_pkthdr.len = m->m_len = frm - mtod(m, uint8_t *);
2161                 return mesh_send_action(ni, m);
2162         } else {
2163                 vap->iv_stats.is_tx_nobuf++;
2164                 ieee80211_free_node(ni);
2165                 return ENOMEM;
2166         }
2167 }
2168
2169 static void
2170 mesh_peer_timeout_setup(struct ieee80211_node *ni)
2171 {
2172         switch (ni->ni_mlstate) {
2173         case IEEE80211_NODE_MESH_HOLDING:
2174                 ni->ni_mltval = ieee80211_mesh_holdingtimeout;
2175                 break;
2176         case IEEE80211_NODE_MESH_CONFIRMRCV:
2177                 ni->ni_mltval = ieee80211_mesh_confirmtimeout;
2178                 break;
2179         case IEEE80211_NODE_MESH_IDLE:
2180                 ni->ni_mltval = 0;
2181                 break;
2182         default:
2183                 ni->ni_mltval = ieee80211_mesh_retrytimeout;
2184                 break;
2185         }
2186         if (ni->ni_mltval)
2187                 callout_reset(&ni->ni_mltimer, ni->ni_mltval,
2188                     mesh_peer_timeout_cb, ni);
2189 }
2190
2191 /*
2192  * Same as above but backoffs timer statisically 50%.
2193  */
2194 static void
2195 mesh_peer_timeout_backoff(struct ieee80211_node *ni)
2196 {
2197         uint32_t r;
2198         
2199         r = karc4random();
2200         ni->ni_mltval += r % ni->ni_mltval;
2201         callout_reset(&ni->ni_mltimer, ni->ni_mltval, mesh_peer_timeout_cb,
2202             ni);
2203 }
2204
2205 static __inline void
2206 mesh_peer_timeout_stop(struct ieee80211_node *ni)
2207 {
2208         callout_stop(&ni->ni_mltimer);
2209 }
2210
2211 /*
2212  * Mesh Peer Link Management FSM timeout handling.
2213  */
2214 static void
2215 mesh_peer_timeout_cb(void *arg)
2216 {
2217         struct ieee80211_node *ni = (struct ieee80211_node *)arg;
2218         uint16_t args[3];
2219
2220         IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_MESH,
2221             ni, "mesh link timeout, state %d, retry counter %d",
2222             ni->ni_mlstate, ni->ni_mlrcnt);
2223         
2224         switch (ni->ni_mlstate) {
2225         case IEEE80211_NODE_MESH_IDLE:
2226         case IEEE80211_NODE_MESH_ESTABLISHED:
2227                 break;
2228         case IEEE80211_NODE_MESH_OPENSNT:
2229         case IEEE80211_NODE_MESH_OPENRCV:
2230                 if (ni->ni_mlrcnt == ieee80211_mesh_maxretries) {
2231                         args[0] = ni->ni_mlpid;
2232                         args[2] = IEEE80211_REASON_MESH_MAX_RETRIES;
2233                         ieee80211_send_action(ni,
2234                             IEEE80211_ACTION_CAT_MESHPEERING,
2235                             IEEE80211_ACTION_MESHPEERING_CLOSE, args);
2236                         ni->ni_mlrcnt = 0;
2237                         mesh_linkchange(ni, IEEE80211_NODE_MESH_HOLDING);
2238                         mesh_peer_timeout_setup(ni);
2239                 } else {
2240                         args[0] = ni->ni_mlpid;
2241                         ieee80211_send_action(ni,
2242                             IEEE80211_ACTION_CAT_MESHPEERING,
2243                             IEEE80211_ACTION_MESHPEERING_OPEN, args);
2244                         ni->ni_mlrcnt++;
2245                         mesh_peer_timeout_backoff(ni);
2246                 }
2247                 break;
2248         case IEEE80211_NODE_MESH_CONFIRMRCV:
2249                 if (ni->ni_mlrcnt == ieee80211_mesh_maxretries) {
2250                         args[0] = ni->ni_mlpid;
2251                         args[2] = IEEE80211_REASON_MESH_CONFIRM_TIMEOUT;
2252                         ieee80211_send_action(ni,
2253                             IEEE80211_ACTION_CAT_MESHPEERING,
2254                             IEEE80211_ACTION_MESHPEERING_CLOSE, args);
2255                         ni->ni_mlrcnt = 0;
2256                         mesh_linkchange(ni, IEEE80211_NODE_MESH_HOLDING);
2257                         mesh_peer_timeout_setup(ni);
2258                 } else {
2259                         ni->ni_mlrcnt++;
2260                         mesh_peer_timeout_setup(ni);
2261                 }
2262                 break;
2263         case IEEE80211_NODE_MESH_HOLDING:
2264                 mesh_linkchange(ni, IEEE80211_NODE_MESH_IDLE);
2265                 break;
2266         }
2267 }
2268
2269 static int
2270 mesh_verify_meshid(struct ieee80211vap *vap, const uint8_t *ie)
2271 {
2272         struct ieee80211_mesh_state *ms = vap->iv_mesh;
2273
2274         if (ie == NULL || ie[1] != ms->ms_idlen)
2275                 return 1;
2276         return memcmp(ms->ms_id, ie + 2, ms->ms_idlen);
2277 }
2278
2279 /*
2280  * Check if we are using the same algorithms for this mesh.
2281  */
2282 static int
2283 mesh_verify_meshconf(struct ieee80211vap *vap, const uint8_t *ie)
2284 {
2285         const struct ieee80211_meshconf_ie *meshconf =
2286             (const struct ieee80211_meshconf_ie *) ie;
2287         const struct ieee80211_mesh_state *ms = vap->iv_mesh;
2288         uint16_t cap;
2289
2290         if (meshconf == NULL)
2291                 return 1;
2292         if (meshconf->conf_pselid != ms->ms_ppath->mpp_ie) {
2293                 IEEE80211_DPRINTF(vap, IEEE80211_MSG_MESH,
2294                     "unknown path selection algorithm: 0x%x\n",
2295                     meshconf->conf_pselid);
2296                 return 1;
2297         }
2298         if (meshconf->conf_pmetid != ms->ms_pmetric->mpm_ie) {
2299                 IEEE80211_DPRINTF(vap, IEEE80211_MSG_MESH,
2300                     "unknown path metric algorithm: 0x%x\n",
2301                     meshconf->conf_pmetid);
2302                 return 1;
2303         }
2304         if (meshconf->conf_ccid != 0) {
2305                 IEEE80211_DPRINTF(vap, IEEE80211_MSG_MESH,
2306                     "unknown congestion control algorithm: 0x%x\n",
2307                     meshconf->conf_ccid);
2308                 return 1;
2309         }
2310         if (meshconf->conf_syncid != IEEE80211_MESHCONF_SYNC_NEIGHOFF) {
2311                 IEEE80211_DPRINTF(vap, IEEE80211_MSG_MESH,
2312                     "unknown sync algorithm: 0x%x\n",
2313                     meshconf->conf_syncid);
2314                 return 1;
2315         }
2316         if (meshconf->conf_authid != 0) {
2317                 IEEE80211_DPRINTF(vap, IEEE80211_MSG_MESH,
2318                     "unknown auth auth algorithm: 0x%x\n",
2319                     meshconf->conf_pselid);
2320                 return 1;
2321         }
2322         /* NB: conf_cap is only read correctly here */
2323         cap = LE_READ_2(&meshconf->conf_cap);
2324         /* Not accepting peers */
2325         if (!(cap & IEEE80211_MESHCONF_CAP_AP)) {
2326                 IEEE80211_DPRINTF(vap, IEEE80211_MSG_MESH,
2327                     "not accepting peers: 0x%x\n", meshconf->conf_cap);
2328                 return 1;
2329         }
2330         return 0;
2331 }
2332
2333 static int
2334 mesh_verify_meshpeer(struct ieee80211vap *vap, uint8_t subtype,
2335     const uint8_t *ie)
2336 {
2337         const struct ieee80211_meshpeer_ie *meshpeer =
2338             (const struct ieee80211_meshpeer_ie *) ie;
2339
2340         if (meshpeer == NULL || meshpeer->peer_len < 6 ||
2341             meshpeer->peer_len > 10)
2342                 return 1;
2343         switch (subtype) {
2344         case IEEE80211_MESH_PEER_LINK_OPEN:
2345                 if (meshpeer->peer_len != 6)
2346                         return 1;
2347                 break;
2348         case IEEE80211_MESH_PEER_LINK_CONFIRM:
2349                 if (meshpeer->peer_len != 8)
2350                         return 1;
2351                 break;
2352         case IEEE80211_MESH_PEER_LINK_CLOSE:
2353                 if (meshpeer->peer_len < 8)
2354                         return 1;
2355                 if (meshpeer->peer_len == 8 && meshpeer->peer_linkid != 0)
2356                         return 1;
2357                 if (meshpeer->peer_rcode == 0)
2358                         return 1;
2359                 break;
2360         }
2361         return 0;
2362 }
2363
2364 /*
2365  * Add a Mesh ID IE to a frame.
2366  */
2367 uint8_t *
2368 ieee80211_add_meshid(uint8_t *frm, struct ieee80211vap *vap)
2369 {
2370         struct ieee80211_mesh_state *ms = vap->iv_mesh;
2371
2372         KASSERT(vap->iv_opmode == IEEE80211_M_MBSS, ("not a mbss vap"));
2373
2374         *frm++ = IEEE80211_ELEMID_MESHID;
2375         *frm++ = ms->ms_idlen;
2376         memcpy(frm, ms->ms_id, ms->ms_idlen);
2377         return frm + ms->ms_idlen;
2378 }
2379
2380 /*
2381  * Add a Mesh Configuration IE to a frame.
2382  * For now just use HWMP routing, Airtime link metric, Null Congestion
2383  * Signaling, Null Sync Protocol and Null Authentication.
2384  */
2385 uint8_t *
2386 ieee80211_add_meshconf(uint8_t *frm, struct ieee80211vap *vap)
2387 {
2388         const struct ieee80211_mesh_state *ms = vap->iv_mesh;
2389         uint16_t caps;
2390
2391         KASSERT(vap->iv_opmode == IEEE80211_M_MBSS, ("not a MBSS vap"));
2392
2393         *frm++ = IEEE80211_ELEMID_MESHCONF;
2394         *frm++ = sizeof(struct ieee80211_meshconf_ie) - 2;
2395         *frm++ = ms->ms_ppath->mpp_ie;          /* path selection */
2396         *frm++ = ms->ms_pmetric->mpm_ie;        /* link metric */
2397         *frm++ = IEEE80211_MESHCONF_CC_DISABLED;
2398         *frm++ = IEEE80211_MESHCONF_SYNC_NEIGHOFF;
2399         *frm++ = IEEE80211_MESHCONF_AUTH_DISABLED;
2400         /* NB: set the number of neighbors before the rest */
2401         *frm = (ms->ms_neighbors > 15 ? 15 : ms->ms_neighbors) << 1;
2402         if (ms->ms_flags & IEEE80211_MESHFLAGS_PORTAL)
2403                 *frm |= IEEE80211_MESHCONF_FORM_MP;
2404         frm += 1;
2405         caps = 0;
2406         if (ms->ms_flags & IEEE80211_MESHFLAGS_AP)
2407                 caps |= IEEE80211_MESHCONF_CAP_AP;
2408         if (ms->ms_flags & IEEE80211_MESHFLAGS_FWD)
2409                 caps |= IEEE80211_MESHCONF_CAP_FWRD;
2410         ADDSHORT(frm, caps);
2411         return frm;
2412 }
2413
2414 /*
2415  * Add a Mesh Peer Management IE to a frame.
2416  */
2417 uint8_t *
2418 ieee80211_add_meshpeer(uint8_t *frm, uint8_t subtype, uint16_t localid,
2419     uint16_t peerid, uint16_t reason)
2420 {
2421         /* XXX change for AH */
2422         static const uint8_t meshpeerproto[4] = IEEE80211_MESH_PEER_PROTO;
2423
2424         KASSERT(localid != 0, ("localid == 0"));
2425
2426         *frm++ = IEEE80211_ELEMID_MESHPEER;
2427         switch (subtype) {
2428         case IEEE80211_MESH_PEER_LINK_OPEN:
2429                 *frm++ = 6;             /* length */
2430                 memcpy(frm, meshpeerproto, 4);
2431                 frm += 4;
2432                 ADDSHORT(frm, localid); /* local ID */
2433                 break;
2434         case IEEE80211_MESH_PEER_LINK_CONFIRM:
2435                 KASSERT(peerid != 0, ("sending peer confirm without peer id"));
2436                 *frm++ = 8;             /* length */
2437                 memcpy(frm, meshpeerproto, 4);
2438                 frm += 4;
2439                 ADDSHORT(frm, localid); /* local ID */
2440                 ADDSHORT(frm, peerid);  /* peer ID */
2441                 break;
2442         case IEEE80211_MESH_PEER_LINK_CLOSE:
2443                 if (peerid)
2444                         *frm++ = 10;    /* length */
2445                 else
2446                         *frm++ = 8;     /* length */
2447                 memcpy(frm, meshpeerproto, 4);
2448                 frm += 4;
2449                 ADDSHORT(frm, localid); /* local ID */
2450                 if (peerid)
2451                         ADDSHORT(frm, peerid);  /* peer ID */
2452                 ADDSHORT(frm, reason);
2453                 break;
2454         }
2455         return frm;
2456 }
2457
2458 /*
2459  * Compute an Airtime Link Metric for the link with this node.
2460  *
2461  * Based on Draft 3.0 spec (11B.10, p.149).
2462  */
2463 /*
2464  * Max 802.11s overhead.
2465  */
2466 #define IEEE80211_MESH_MAXOVERHEAD \
2467         (sizeof(struct ieee80211_qosframe_addr4) \
2468          + sizeof(struct ieee80211_meshcntl_ae11) \
2469         + sizeof(struct llc) \
2470         + IEEE80211_ADDR_LEN \
2471         + IEEE80211_WEP_IVLEN \
2472         + IEEE80211_WEP_KIDLEN \
2473         + IEEE80211_WEP_CRCLEN \
2474         + IEEE80211_WEP_MICLEN \
2475         + IEEE80211_CRC_LEN)
2476 uint32_t
2477 mesh_airtime_calc(struct ieee80211_node *ni)
2478 {
2479 #define M_BITS 8
2480 #define S_FACTOR (2 * M_BITS)
2481         struct ieee80211com *ic = ni->ni_ic;
2482         struct ifnet *ifp = ni->ni_vap->iv_ifp;
2483         const static int nbits = 8192 << M_BITS;
2484         uint32_t overhead, rate, errrate;
2485         uint64_t res;
2486
2487         /* Time to transmit a frame */
2488         rate = ni->ni_txrate;
2489         overhead = ieee80211_compute_duration(ic->ic_rt,
2490             ifp->if_mtu + IEEE80211_MESH_MAXOVERHEAD, rate, 0) << M_BITS;
2491         /* Error rate in percentage */
2492         /* XXX assuming small failures are ok */
2493         errrate = (((ifp->if_oerrors +
2494             ifp->if_ierrors) / 100) << M_BITS) / 100;
2495         res = (overhead + (nbits / rate)) *
2496             ((1 << S_FACTOR) / ((1 << M_BITS) - errrate));
2497
2498         return (uint32_t)(res >> S_FACTOR);
2499 #undef M_BITS
2500 #undef S_FACTOR
2501 }
2502
2503 /*
2504  * Add a Mesh Link Metric report IE to a frame.
2505  */
2506 uint8_t *
2507 ieee80211_add_meshlmetric(uint8_t *frm, uint32_t metric)
2508 {
2509         *frm++ = IEEE80211_ELEMID_MESHLINK;
2510         *frm++ = 4;
2511         ADDWORD(frm, metric);
2512         return frm;
2513 }
2514 #undef ADDSHORT
2515 #undef ADDWORD
2516
2517 /*
2518  * Initialize any mesh-specific node state.
2519  */
2520 void
2521 ieee80211_mesh_node_init(struct ieee80211vap *vap, struct ieee80211_node *ni)
2522 {
2523         ni->ni_flags |= IEEE80211_NODE_QOS;
2524         callout_init_mp(&ni->ni_mltimer);
2525 }
2526
2527 /*
2528  * Cleanup any mesh-specific node state.
2529  */
2530 void
2531 ieee80211_mesh_node_cleanup(struct ieee80211_node *ni)
2532 {
2533         struct ieee80211vap *vap = ni->ni_vap;
2534         struct ieee80211_mesh_state *ms = vap->iv_mesh;
2535
2536         callout_stop(&ni->ni_mltimer);
2537         /* NB: short-circuit callbacks after mesh_vdetach */
2538         if (vap->iv_mesh != NULL)
2539                 ms->ms_ppath->mpp_peerdown(ni);
2540 }
2541
2542 void
2543 ieee80211_parse_meshid(struct ieee80211_node *ni, const uint8_t *ie)
2544 {
2545         ni->ni_meshidlen = ie[1];
2546         memcpy(ni->ni_meshid, ie + 2, ie[1]);
2547 }
2548
2549 /*
2550  * Setup mesh-specific node state on neighbor discovery.
2551  */
2552 void
2553 ieee80211_mesh_init_neighbor(struct ieee80211_node *ni,
2554         const struct ieee80211_frame *wh,
2555         const struct ieee80211_scanparams *sp)
2556 {
2557         ieee80211_parse_meshid(ni, sp->meshid);
2558 }
2559
2560 void
2561 ieee80211_mesh_update_beacon(struct ieee80211vap *vap,
2562         struct ieee80211_beacon_offsets *bo)
2563 {
2564         KASSERT(vap->iv_opmode == IEEE80211_M_MBSS, ("not a MBSS vap"));
2565
2566         if (isset(bo->bo_flags, IEEE80211_BEACON_MESHCONF)) {
2567                 (void)ieee80211_add_meshconf(bo->bo_meshconf, vap);
2568                 clrbit(bo->bo_flags, IEEE80211_BEACON_MESHCONF);
2569         }
2570 }
2571
2572 static int
2573 mesh_ioctl_get80211(struct ieee80211vap *vap, struct ieee80211req *ireq)
2574 {
2575         struct ieee80211_mesh_state *ms = vap->iv_mesh;
2576         uint8_t tmpmeshid[IEEE80211_NWID_LEN];
2577         struct ieee80211_mesh_route *rt;
2578         struct ieee80211req_mesh_route *imr;
2579         size_t len, off;
2580         uint8_t *p;
2581         int error;
2582
2583         if (vap->iv_opmode != IEEE80211_M_MBSS)
2584                 return ENOSYS;
2585
2586         error = 0;
2587         switch (ireq->i_type) {
2588         case IEEE80211_IOC_MESH_ID:
2589                 ireq->i_len = ms->ms_idlen;
2590                 memcpy(tmpmeshid, ms->ms_id, ireq->i_len);
2591                 error = copyout(tmpmeshid, ireq->i_data, ireq->i_len);
2592                 break;
2593         case IEEE80211_IOC_MESH_AP:
2594                 ireq->i_val = (ms->ms_flags & IEEE80211_MESHFLAGS_AP) != 0;
2595                 break;
2596         case IEEE80211_IOC_MESH_FWRD:
2597                 ireq->i_val = (ms->ms_flags & IEEE80211_MESHFLAGS_FWD) != 0;
2598                 break;
2599         case IEEE80211_IOC_MESH_TTL:
2600                 ireq->i_val = ms->ms_ttl;
2601                 break;
2602         case IEEE80211_IOC_MESH_RTCMD:
2603                 switch (ireq->i_val) {
2604                 case IEEE80211_MESH_RTCMD_LIST:
2605                         len = 0;
2606                         MESH_RT_LOCK(ms);
2607                         TAILQ_FOREACH(rt, &ms->ms_routes, rt_next) {
2608                                 len += sizeof(*imr);
2609                         }
2610                         MESH_RT_UNLOCK(ms);
2611                         if (len > ireq->i_len || ireq->i_len < sizeof(*imr)) {
2612                                 ireq->i_len = len;
2613                                 return ENOMEM;
2614                         }
2615                         ireq->i_len = len;
2616                         /* XXX M_WAIT? */
2617                         p = kmalloc(len, M_TEMP, M_INTWAIT | M_ZERO);
2618                         if (p == NULL)
2619                                 return ENOMEM;
2620                         off = 0;
2621                         MESH_RT_LOCK(ms);
2622                         TAILQ_FOREACH(rt, &ms->ms_routes, rt_next) {
2623                                 if (off >= len)
2624                                         break;
2625                                 imr = (struct ieee80211req_mesh_route *)
2626                                     (p + off);
2627                                 imr->imr_flags = rt->rt_flags;
2628                                 IEEE80211_ADDR_COPY(imr->imr_dest,
2629                                     rt->rt_dest);
2630                                 IEEE80211_ADDR_COPY(imr->imr_nexthop,
2631                                     rt->rt_nexthop);
2632                                 imr->imr_metric = rt->rt_metric;
2633                                 imr->imr_nhops = rt->rt_nhops;
2634                                 imr->imr_lifetime = rt->rt_lifetime;
2635                                 imr->imr_lastmseq = rt->rt_lastmseq;
2636                                 off += sizeof(*imr);
2637                         }
2638                         MESH_RT_UNLOCK(ms);
2639                         error = copyout(p, (uint8_t *)ireq->i_data,
2640                             ireq->i_len);
2641                         kfree(p, M_TEMP);
2642                         break;
2643                 case IEEE80211_MESH_RTCMD_FLUSH:
2644                 case IEEE80211_MESH_RTCMD_ADD:
2645                 case IEEE80211_MESH_RTCMD_DELETE:
2646                         return EINVAL;
2647                 default:
2648                         return ENOSYS;
2649                 }
2650                 break;
2651         case IEEE80211_IOC_MESH_PR_METRIC:
2652                 len = strlen(ms->ms_pmetric->mpm_descr);
2653                 if (ireq->i_len < len)
2654                         return EINVAL;
2655                 ireq->i_len = len;
2656                 error = copyout(ms->ms_pmetric->mpm_descr,
2657                     (uint8_t *)ireq->i_data, len);
2658                 break;
2659         case IEEE80211_IOC_MESH_PR_PATH:
2660                 len = strlen(ms->ms_ppath->mpp_descr);
2661                 if (ireq->i_len < len)
2662                         return EINVAL;
2663                 ireq->i_len = len;
2664                 error = copyout(ms->ms_ppath->mpp_descr,
2665                     (uint8_t *)ireq->i_data, len);
2666                 break;
2667         default:
2668                 return ENOSYS;
2669         }
2670
2671         return error;
2672 }
2673 IEEE80211_IOCTL_GET(mesh, mesh_ioctl_get80211);
2674
2675 static int
2676 mesh_ioctl_set80211(struct ieee80211vap *vap, struct ieee80211req *ireq)
2677 {
2678         struct ieee80211_mesh_state *ms = vap->iv_mesh;
2679         uint8_t tmpmeshid[IEEE80211_NWID_LEN];
2680         uint8_t tmpaddr[IEEE80211_ADDR_LEN];
2681         char tmpproto[IEEE80211_MESH_PROTO_DSZ];
2682         int error;
2683
2684         if (vap->iv_opmode != IEEE80211_M_MBSS)
2685                 return ENOSYS;
2686
2687         error = 0;
2688         switch (ireq->i_type) {
2689         case IEEE80211_IOC_MESH_ID:
2690                 if (ireq->i_val != 0 || ireq->i_len > IEEE80211_MESHID_LEN)
2691                         return EINVAL;
2692                 error = copyin(ireq->i_data, tmpmeshid, ireq->i_len);
2693                 if (error != 0)
2694                         break;
2695                 memset(ms->ms_id, 0, IEEE80211_NWID_LEN);
2696                 ms->ms_idlen = ireq->i_len;
2697                 memcpy(ms->ms_id, tmpmeshid, ireq->i_len);
2698                 error = ENETRESET;
2699                 break;
2700         case IEEE80211_IOC_MESH_AP:
2701                 if (ireq->i_val)
2702                         ms->ms_flags |= IEEE80211_MESHFLAGS_AP;
2703                 else
2704                         ms->ms_flags &= ~IEEE80211_MESHFLAGS_AP;
2705                 error = ENETRESET;
2706                 break;
2707         case IEEE80211_IOC_MESH_FWRD:
2708                 if (ireq->i_val)
2709                         ms->ms_flags |= IEEE80211_MESHFLAGS_FWD;
2710                 else
2711                         ms->ms_flags &= ~IEEE80211_MESHFLAGS_FWD;
2712                 break;
2713         case IEEE80211_IOC_MESH_TTL:
2714                 ms->ms_ttl = (uint8_t) ireq->i_val;
2715                 break;
2716         case IEEE80211_IOC_MESH_RTCMD:
2717                 switch (ireq->i_val) {
2718                 case IEEE80211_MESH_RTCMD_LIST:
2719                         return EINVAL;
2720                 case IEEE80211_MESH_RTCMD_FLUSH:
2721                         ieee80211_mesh_rt_flush(vap);
2722                         break;
2723                 case IEEE80211_MESH_RTCMD_ADD:
2724                         if (IEEE80211_ADDR_EQ(vap->iv_myaddr, ireq->i_data) ||
2725                             IEEE80211_ADDR_EQ(broadcastaddr, ireq->i_data))
2726                                 return EINVAL;
2727                         error = copyin(ireq->i_data, &tmpaddr,
2728                             IEEE80211_ADDR_LEN);
2729                         if (error == 0)
2730                                 ieee80211_mesh_discover(vap, tmpaddr, NULL);
2731                         break;
2732                 case IEEE80211_MESH_RTCMD_DELETE:
2733                         ieee80211_mesh_rt_del(vap, ireq->i_data);
2734                         break;
2735                 default:
2736                         return ENOSYS;
2737                 }
2738                 break;
2739         case IEEE80211_IOC_MESH_PR_METRIC:
2740                 error = copyin(ireq->i_data, tmpproto, sizeof(tmpproto));
2741                 if (error == 0) {
2742                         error = mesh_select_proto_metric(vap, tmpproto);
2743                         if (error == 0)
2744                                 error = ENETRESET;
2745                 }
2746                 break;
2747         case IEEE80211_IOC_MESH_PR_PATH:
2748                 error = copyin(ireq->i_data, tmpproto, sizeof(tmpproto));
2749                 if (error == 0) {
2750                         error = mesh_select_proto_path(vap, tmpproto);
2751                         if (error == 0)
2752                                 error = ENETRESET;
2753                 }
2754                 break;
2755         default:
2756                 return ENOSYS;
2757         }
2758         return error;
2759 }
2760 IEEE80211_IOCTL_SET(mesh, mesh_ioctl_set80211);