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