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