Merge branch 'vendor/LESS'
[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_callout(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_callout(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 int
320 ieee80211_mesh_register_proto_path(const struct ieee80211_mesh_proto_path *mpp)
321 {
322         int i, firstempty = -1;
323
324         for (i = 0; i < NELEM(mesh_proto_paths); i++) {
325                 if (strncmp(mpp->mpp_descr, mesh_proto_paths[i].mpp_descr,
326                     IEEE80211_MESH_PROTO_DSZ) == 0)
327                         return EEXIST;
328                 if (!mesh_proto_paths[i].mpp_active && firstempty == -1)
329                         firstempty = i;
330         }
331         if (firstempty < 0)
332                 return ENOSPC;
333         memcpy(&mesh_proto_paths[firstempty], mpp, sizeof(*mpp));
334         mesh_proto_paths[firstempty].mpp_active = 1;
335         return 0;
336 }
337
338 int
339 ieee80211_mesh_register_proto_metric(const struct
340     ieee80211_mesh_proto_metric *mpm)
341 {
342         int i, firstempty = -1;
343
344         for (i = 0; i < NELEM(mesh_proto_metrics); i++) {
345                 if (strncmp(mpm->mpm_descr, mesh_proto_metrics[i].mpm_descr,
346                     IEEE80211_MESH_PROTO_DSZ) == 0)
347                         return EEXIST;
348                 if (!mesh_proto_metrics[i].mpm_active && firstempty == -1)
349                         firstempty = i;
350         }
351         if (firstempty < 0)
352                 return ENOSPC;
353         memcpy(&mesh_proto_metrics[firstempty], mpm, sizeof(*mpm));
354         mesh_proto_metrics[firstempty].mpm_active = 1;
355         return 0;
356 }
357
358 static int
359 mesh_select_proto_path(struct ieee80211vap *vap, const char *name)
360 {
361         struct ieee80211_mesh_state *ms = vap->iv_mesh;
362         int i;
363
364         for (i = 0; i < NELEM(mesh_proto_paths); i++) {
365                 if (strcasecmp(mesh_proto_paths[i].mpp_descr, name) == 0) {
366                         ms->ms_ppath = &mesh_proto_paths[i];
367                         return 0;
368                 }
369         }
370         return ENOENT;
371 }
372
373 static int
374 mesh_select_proto_metric(struct ieee80211vap *vap, const char *name)
375 {
376         struct ieee80211_mesh_state *ms = vap->iv_mesh;
377         int i;
378
379         for (i = 0; i < NELEM(mesh_proto_metrics); i++) {
380                 if (strcasecmp(mesh_proto_metrics[i].mpm_descr, name) == 0) {
381                         ms->ms_pmetric = &mesh_proto_metrics[i];
382                         return 0;
383                 }
384         }
385         return ENOENT;
386 }
387
388 static void
389 ieee80211_mesh_init(void)
390 {
391
392         memset(mesh_proto_paths, 0, sizeof(mesh_proto_paths));
393         memset(mesh_proto_metrics, 0, sizeof(mesh_proto_metrics));
394
395         /*
396          * Setup mesh parameters that depends on the clock frequency.
397          */
398         ieee80211_mesh_retrytimeout = msecs_to_ticks(40);
399         ieee80211_mesh_holdingtimeout = msecs_to_ticks(40);
400         ieee80211_mesh_confirmtimeout = msecs_to_ticks(40);
401
402         /*
403          * Register action frame handlers.
404          */
405         ieee80211_recv_action_register(IEEE80211_ACTION_CAT_MESHPEERING,
406             IEEE80211_ACTION_MESHPEERING_OPEN,
407             mesh_recv_action_meshpeering_open);
408         ieee80211_recv_action_register(IEEE80211_ACTION_CAT_MESHPEERING,
409             IEEE80211_ACTION_MESHPEERING_CONFIRM,
410             mesh_recv_action_meshpeering_confirm);
411         ieee80211_recv_action_register(IEEE80211_ACTION_CAT_MESHPEERING,
412             IEEE80211_ACTION_MESHPEERING_CLOSE,
413             mesh_recv_action_meshpeering_close);
414         ieee80211_recv_action_register(IEEE80211_ACTION_CAT_MESHLMETRIC,
415             IEEE80211_ACTION_MESHLMETRIC_REQ, mesh_recv_action_meshlmetric_req);
416         ieee80211_recv_action_register(IEEE80211_ACTION_CAT_MESHLMETRIC,
417             IEEE80211_ACTION_MESHLMETRIC_REP, mesh_recv_action_meshlmetric_rep);
418
419         ieee80211_send_action_register(IEEE80211_ACTION_CAT_MESHPEERING, 
420             IEEE80211_ACTION_MESHPEERING_OPEN,
421             mesh_send_action_meshpeering_open);
422         ieee80211_send_action_register(IEEE80211_ACTION_CAT_MESHPEERING, 
423             IEEE80211_ACTION_MESHPEERING_CONFIRM,
424             mesh_send_action_meshpeering_confirm);
425         ieee80211_send_action_register(IEEE80211_ACTION_CAT_MESHPEERING, 
426             IEEE80211_ACTION_MESHPEERING_CLOSE,
427             mesh_send_action_meshpeering_close);
428         ieee80211_send_action_register(IEEE80211_ACTION_CAT_MESHLMETRIC, 
429             IEEE80211_ACTION_MESHLMETRIC_REQ,
430             mesh_send_action_meshlink_request);
431         ieee80211_send_action_register(IEEE80211_ACTION_CAT_MESHLMETRIC, 
432             IEEE80211_ACTION_MESHLMETRIC_REP,
433             mesh_send_action_meshlink_reply);
434
435         /*
436          * Register Airtime Link Metric.
437          */
438         ieee80211_mesh_register_proto_metric(&mesh_metric_airtime);
439
440 }
441 SYSINIT(wlan_mesh, SI_SUB_DRIVERS, SI_ORDER_FIRST, ieee80211_mesh_init, NULL);
442
443 void
444 ieee80211_mesh_attach(struct ieee80211com *ic)
445 {
446         ic->ic_vattach[IEEE80211_M_MBSS] = mesh_vattach;
447 }
448
449 void
450 ieee80211_mesh_detach(struct ieee80211com *ic)
451 {
452 }
453
454 static void
455 mesh_vdetach_peers(void *arg, struct ieee80211_node *ni)
456 {
457         struct ieee80211com *ic = ni->ni_ic;
458         uint16_t args[3];
459
460         if (ni->ni_mlstate == IEEE80211_NODE_MESH_ESTABLISHED) {
461                 args[0] = ni->ni_mlpid;
462                 args[1] = ni->ni_mllid;
463                 args[2] = IEEE80211_REASON_PEER_LINK_CANCELED;
464                 ieee80211_send_action(ni,
465                     IEEE80211_ACTION_CAT_MESHPEERING,
466                     IEEE80211_ACTION_MESHPEERING_CLOSE,
467                     args);
468         }
469         callout_stop(&ni->ni_mltimer);
470         /* XXX belongs in hwmp */
471         ieee80211_ageq_drain_node(&ic->ic_stageq,
472            (void *)(uintptr_t) ieee80211_mac_hash(ic, ni->ni_macaddr));
473 }
474
475 static void
476 mesh_vdetach(struct ieee80211vap *vap)
477 {
478         struct ieee80211_mesh_state *ms = vap->iv_mesh;
479
480         callout_stop(&ms->ms_cleantimer);
481         ieee80211_iterate_nodes(&vap->iv_ic->ic_sta, mesh_vdetach_peers,
482             NULL);
483         ieee80211_mesh_rt_flush(vap);
484         ms->ms_ppath->mpp_vdetach(vap);
485         kfree(vap->iv_mesh, M_80211_VAP);
486         vap->iv_mesh = NULL;
487 }
488
489 static void
490 mesh_vattach(struct ieee80211vap *vap)
491 {
492         struct ieee80211_mesh_state *ms;
493         vap->iv_newstate = mesh_newstate;
494         vap->iv_input = mesh_input;
495         vap->iv_opdetach = mesh_vdetach;
496         vap->iv_recv_mgmt = mesh_recv_mgmt;
497         ms = kmalloc(sizeof(struct ieee80211_mesh_state), M_80211_VAP,
498             M_INTWAIT | M_ZERO);
499         if (ms == NULL) {
500                 kprintf("%s: couldn't alloc MBSS state\n", __func__);
501                 return;
502         }
503         vap->iv_mesh = ms;
504         ms->ms_seq = 0;
505         ms->ms_flags = (IEEE80211_MESHFLAGS_AP | IEEE80211_MESHFLAGS_FWD);
506         ms->ms_ttl = IEEE80211_MESH_DEFAULT_TTL;
507         TAILQ_INIT(&ms->ms_routes);
508         callout_init_mp(&ms->ms_cleantimer);
509         mesh_select_proto_metric(vap, "AIRTIME");
510         KASSERT(ms->ms_pmetric, ("ms_pmetric == NULL"));
511         mesh_select_proto_path(vap, "HWMP");
512         KASSERT(ms->ms_ppath, ("ms_ppath == NULL"));
513         ms->ms_ppath->mpp_vattach(vap);
514 }
515
516 /*
517  * IEEE80211_M_MBSS vap state machine handler.
518  */
519 static int
520 mesh_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
521 {
522         struct ieee80211_mesh_state *ms = vap->iv_mesh;
523         struct ieee80211com *ic = vap->iv_ic;
524         struct ieee80211_node *ni;
525         enum ieee80211_state ostate;
526
527         ostate = vap->iv_state;
528         IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE, "%s: %s -> %s (%d)\n",
529             __func__, ieee80211_state_name[ostate],
530             ieee80211_state_name[nstate], arg);
531         vap->iv_state = nstate;         /* state transition */
532         if (ostate != IEEE80211_S_SCAN)
533                 ieee80211_cancel_scan(vap);     /* background scan */
534         ni = vap->iv_bss;                       /* NB: no reference held */
535         if (nstate != IEEE80211_S_RUN && ostate == IEEE80211_S_RUN)
536                 callout_stop(&ms->ms_cleantimer);
537         switch (nstate) {
538         case IEEE80211_S_INIT:
539                 switch (ostate) {
540                 case IEEE80211_S_SCAN:
541                         ieee80211_cancel_scan(vap);
542                         break;
543                 case IEEE80211_S_CAC:
544                         ieee80211_dfs_cac_stop(vap);
545                         break;
546                 case IEEE80211_S_RUN:
547                         ieee80211_iterate_nodes(&ic->ic_sta,
548                             mesh_vdetach_peers, NULL);
549                         break;
550                 default:
551                         break;
552                 }
553                 if (ostate != IEEE80211_S_INIT) {
554                         /* NB: optimize INIT -> INIT case */
555                         ieee80211_reset_bss(vap);
556                         ieee80211_mesh_rt_flush(vap);
557                 }
558                 break;
559         case IEEE80211_S_SCAN:
560                 switch (ostate) {
561                 case IEEE80211_S_INIT:
562                         if (vap->iv_des_chan != IEEE80211_CHAN_ANYC &&
563                             !IEEE80211_IS_CHAN_RADAR(vap->iv_des_chan) &&
564                             ms->ms_idlen != 0) {
565                                 /*
566                                  * Already have a channel and a mesh ID; bypass
567                                  * the scan and startup immediately.
568                                  */
569                                 ieee80211_create_ibss(vap, vap->iv_des_chan);
570                                 break;
571                         }
572                         /*
573                          * Initiate a scan.  We can come here as a result
574                          * of an IEEE80211_IOC_SCAN_REQ too in which case
575                          * the vap will be marked with IEEE80211_FEXT_SCANREQ
576                          * and the scan request parameters will be present
577                          * in iv_scanreq.  Otherwise we do the default.
578                         */
579                         if (vap->iv_flags_ext & IEEE80211_FEXT_SCANREQ) {
580                                 ieee80211_check_scan(vap,
581                                     vap->iv_scanreq_flags,
582                                     vap->iv_scanreq_duration,
583                                     vap->iv_scanreq_mindwell,
584                                     vap->iv_scanreq_maxdwell,
585                                     vap->iv_scanreq_nssid, vap->iv_scanreq_ssid);
586                                 vap->iv_flags_ext &= ~IEEE80211_FEXT_SCANREQ;
587                         } else
588                                 ieee80211_check_scan_current(vap);
589                         break;
590                 default:
591                         break;
592                 }
593                 break;
594         case IEEE80211_S_CAC:
595                 /*
596                  * Start CAC on a DFS channel.  We come here when starting
597                  * a bss on a DFS channel (see ieee80211_create_ibss).
598                  */
599                 ieee80211_dfs_cac_start(vap);
600                 break;
601         case IEEE80211_S_RUN:
602                 switch (ostate) {
603                 case IEEE80211_S_INIT:
604                         /*
605                          * Already have a channel; bypass the
606                          * scan and startup immediately.
607                          * Note that ieee80211_create_ibss will call
608                          * back to do a RUN->RUN state change.
609                          */
610                         ieee80211_create_ibss(vap,
611                             ieee80211_ht_adjust_channel(ic,
612                                 ic->ic_curchan, vap->iv_flags_ht));
613                         /* NB: iv_bss is changed on return */
614                         break;
615                 case IEEE80211_S_CAC:
616                         /*
617                          * NB: This is the normal state change when CAC
618                          * expires and no radar was detected; no need to
619                          * clear the CAC timer as it's already expired.
620                          */
621                         /* fall thru... */
622                 case IEEE80211_S_CSA:
623 #if 0
624                         /*
625                          * Shorten inactivity timer of associated stations
626                          * to weed out sta's that don't follow a CSA.
627                          */
628                         ieee80211_iterate_nodes(&ic->ic_sta, sta_csa, vap);
629 #endif
630                         /*
631                          * Update bss node channel to reflect where
632                          * we landed after CSA.
633                          */
634                         ieee80211_node_set_chan(vap->iv_bss,
635                             ieee80211_ht_adjust_channel(ic, ic->ic_curchan,
636                                 ieee80211_htchanflags(vap->iv_bss->ni_chan)));
637                         /* XXX bypass debug msgs */
638                         break;
639                 case IEEE80211_S_SCAN:
640                 case IEEE80211_S_RUN:
641 #ifdef IEEE80211_DEBUG
642                         if (ieee80211_msg_debug(vap)) {
643                                 struct ieee80211_node *ni = vap->iv_bss;
644                                 ieee80211_note(vap,
645                                     "synchronized with %6D meshid ",
646                                     ni->ni_meshid, ":");
647                                 ieee80211_print_essid(ni->ni_meshid,
648                                     ni->ni_meshidlen);
649                                 /* XXX MCS/HT */
650                                 kprintf(" channel %d\n",
651                                     ieee80211_chan2ieee(ic, ic->ic_curchan));
652                         }
653 #endif
654                         break;
655                 default:
656                         break;
657                 }
658                 ieee80211_node_authorize(vap->iv_bss);
659                 callout_reset(&ms->ms_cleantimer, ms->ms_ppath->mpp_inact,
660                     mesh_rt_cleanup_callout, vap);
661                 break;
662         default:
663                 break;
664         }
665         /* NB: ostate not nstate */
666         ms->ms_ppath->mpp_newstate(vap, ostate, arg);
667         return 0;
668 }
669
670 static void
671 mesh_rt_cleanup_callout(void *arg)
672 {
673         struct ieee80211vap *vap = arg;
674         struct ieee80211_mesh_state *ms = vap->iv_mesh;
675
676         wlan_serialize_enter();
677         mesh_rt_flush_invalid(vap);
678         callout_reset(&ms->ms_cleantimer, ms->ms_ppath->mpp_inact,
679                       mesh_rt_cleanup_callout, vap);
680         wlan_serialize_exit();
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_callout, ni);
2161         }
2162 }
2163
2164 /*
2165  * Same as above but backoffs timer statisically 50%.
2166  */
2167 static void
2168 mesh_peer_timeout_backoff(struct ieee80211_node *ni)
2169 {
2170         uint32_t r;
2171         
2172         r = karc4random();
2173         ni->ni_mltval += r % ni->ni_mltval;
2174         callout_reset(&ni->ni_mltimer, ni->ni_mltval,
2175                       mesh_peer_timeout_callout, ni);
2176 }
2177
2178 static __inline void
2179 mesh_peer_timeout_stop(struct ieee80211_node *ni)
2180 {
2181         callout_stop(&ni->ni_mltimer);
2182 }
2183
2184 /*
2185  * Mesh Peer Link Management FSM timeout handling.
2186  */
2187 static void
2188 mesh_peer_timeout_callout(void *arg)
2189 {
2190         struct ieee80211_node *ni = (struct ieee80211_node *)arg;
2191         uint16_t args[3];
2192
2193         wlan_serialize_enter();
2194         IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_MESH,
2195             ni, "mesh link timeout, state %d, retry counter %d",
2196             ni->ni_mlstate, ni->ni_mlrcnt);
2197         
2198         switch (ni->ni_mlstate) {
2199         case IEEE80211_NODE_MESH_IDLE:
2200         case IEEE80211_NODE_MESH_ESTABLISHED:
2201                 break;
2202         case IEEE80211_NODE_MESH_OPENSNT:
2203         case IEEE80211_NODE_MESH_OPENRCV:
2204                 if (ni->ni_mlrcnt == ieee80211_mesh_maxretries) {
2205                         args[0] = ni->ni_mlpid;
2206                         args[2] = IEEE80211_REASON_MESH_MAX_RETRIES;
2207                         ieee80211_send_action(ni,
2208                             IEEE80211_ACTION_CAT_MESHPEERING,
2209                             IEEE80211_ACTION_MESHPEERING_CLOSE, args);
2210                         ni->ni_mlrcnt = 0;
2211                         mesh_linkchange(ni, IEEE80211_NODE_MESH_HOLDING);
2212                         mesh_peer_timeout_setup(ni);
2213                 } else {
2214                         args[0] = ni->ni_mlpid;
2215                         ieee80211_send_action(ni,
2216                             IEEE80211_ACTION_CAT_MESHPEERING,
2217                             IEEE80211_ACTION_MESHPEERING_OPEN, args);
2218                         ni->ni_mlrcnt++;
2219                         mesh_peer_timeout_backoff(ni);
2220                 }
2221                 break;
2222         case IEEE80211_NODE_MESH_CONFIRMRCV:
2223                 if (ni->ni_mlrcnt == ieee80211_mesh_maxretries) {
2224                         args[0] = ni->ni_mlpid;
2225                         args[2] = IEEE80211_REASON_MESH_CONFIRM_TIMEOUT;
2226                         ieee80211_send_action(ni,
2227                             IEEE80211_ACTION_CAT_MESHPEERING,
2228                             IEEE80211_ACTION_MESHPEERING_CLOSE, args);
2229                         ni->ni_mlrcnt = 0;
2230                         mesh_linkchange(ni, IEEE80211_NODE_MESH_HOLDING);
2231                         mesh_peer_timeout_setup(ni);
2232                 } else {
2233                         ni->ni_mlrcnt++;
2234                         mesh_peer_timeout_setup(ni);
2235                 }
2236                 break;
2237         case IEEE80211_NODE_MESH_HOLDING:
2238                 mesh_linkchange(ni, IEEE80211_NODE_MESH_IDLE);
2239                 break;
2240         }
2241         wlan_serialize_exit();
2242 }
2243
2244 static int
2245 mesh_verify_meshid(struct ieee80211vap *vap, const uint8_t *ie)
2246 {
2247         struct ieee80211_mesh_state *ms = vap->iv_mesh;
2248
2249         if (ie == NULL || ie[1] != ms->ms_idlen)
2250                 return 1;
2251         return memcmp(ms->ms_id, ie + 2, ms->ms_idlen);
2252 }
2253
2254 /*
2255  * Check if we are using the same algorithms for this mesh.
2256  */
2257 static int
2258 mesh_verify_meshconf(struct ieee80211vap *vap, const uint8_t *ie)
2259 {
2260         const struct ieee80211_meshconf_ie *meshconf =
2261             (const struct ieee80211_meshconf_ie *) ie;
2262         const struct ieee80211_mesh_state *ms = vap->iv_mesh;
2263         uint16_t cap;
2264
2265         if (meshconf == NULL)
2266                 return 1;
2267         if (meshconf->conf_pselid != ms->ms_ppath->mpp_ie) {
2268                 IEEE80211_DPRINTF(vap, IEEE80211_MSG_MESH,
2269                     "unknown path selection algorithm: 0x%x\n",
2270                     meshconf->conf_pselid);
2271                 return 1;
2272         }
2273         if (meshconf->conf_pmetid != ms->ms_pmetric->mpm_ie) {
2274                 IEEE80211_DPRINTF(vap, IEEE80211_MSG_MESH,
2275                     "unknown path metric algorithm: 0x%x\n",
2276                     meshconf->conf_pmetid);
2277                 return 1;
2278         }
2279         if (meshconf->conf_ccid != 0) {
2280                 IEEE80211_DPRINTF(vap, IEEE80211_MSG_MESH,
2281                     "unknown congestion control algorithm: 0x%x\n",
2282                     meshconf->conf_ccid);
2283                 return 1;
2284         }
2285         if (meshconf->conf_syncid != IEEE80211_MESHCONF_SYNC_NEIGHOFF) {
2286                 IEEE80211_DPRINTF(vap, IEEE80211_MSG_MESH,
2287                     "unknown sync algorithm: 0x%x\n",
2288                     meshconf->conf_syncid);
2289                 return 1;
2290         }
2291         if (meshconf->conf_authid != 0) {
2292                 IEEE80211_DPRINTF(vap, IEEE80211_MSG_MESH,
2293                     "unknown auth auth algorithm: 0x%x\n",
2294                     meshconf->conf_pselid);
2295                 return 1;
2296         }
2297         /* NB: conf_cap is only read correctly here */
2298         cap = LE_READ_2(&meshconf->conf_cap);
2299         /* Not accepting peers */
2300         if (!(cap & IEEE80211_MESHCONF_CAP_AP)) {
2301                 IEEE80211_DPRINTF(vap, IEEE80211_MSG_MESH,
2302                     "not accepting peers: 0x%x\n", meshconf->conf_cap);
2303                 return 1;
2304         }
2305         return 0;
2306 }
2307
2308 static int
2309 mesh_verify_meshpeer(struct ieee80211vap *vap, uint8_t subtype,
2310     const uint8_t *ie)
2311 {
2312         const struct ieee80211_meshpeer_ie *meshpeer =
2313             (const struct ieee80211_meshpeer_ie *) ie;
2314
2315         if (meshpeer == NULL || meshpeer->peer_len < 6 ||
2316             meshpeer->peer_len > 10)
2317                 return 1;
2318         switch (subtype) {
2319         case IEEE80211_MESH_PEER_LINK_OPEN:
2320                 if (meshpeer->peer_len != 6)
2321                         return 1;
2322                 break;
2323         case IEEE80211_MESH_PEER_LINK_CONFIRM:
2324                 if (meshpeer->peer_len != 8)
2325                         return 1;
2326                 break;
2327         case IEEE80211_MESH_PEER_LINK_CLOSE:
2328                 if (meshpeer->peer_len < 8)
2329                         return 1;
2330                 if (meshpeer->peer_len == 8 && meshpeer->peer_linkid != 0)
2331                         return 1;
2332                 if (meshpeer->peer_rcode == 0)
2333                         return 1;
2334                 break;
2335         }
2336         return 0;
2337 }
2338
2339 /*
2340  * Add a Mesh ID IE to a frame.
2341  */
2342 uint8_t *
2343 ieee80211_add_meshid(uint8_t *frm, struct ieee80211vap *vap)
2344 {
2345         struct ieee80211_mesh_state *ms = vap->iv_mesh;
2346
2347         KASSERT(vap->iv_opmode == IEEE80211_M_MBSS, ("not a mbss vap"));
2348
2349         *frm++ = IEEE80211_ELEMID_MESHID;
2350         *frm++ = ms->ms_idlen;
2351         memcpy(frm, ms->ms_id, ms->ms_idlen);
2352         return frm + ms->ms_idlen;
2353 }
2354
2355 /*
2356  * Add a Mesh Configuration IE to a frame.
2357  * For now just use HWMP routing, Airtime link metric, Null Congestion
2358  * Signaling, Null Sync Protocol and Null Authentication.
2359  */
2360 uint8_t *
2361 ieee80211_add_meshconf(uint8_t *frm, struct ieee80211vap *vap)
2362 {
2363         const struct ieee80211_mesh_state *ms = vap->iv_mesh;
2364         uint16_t caps;
2365
2366         KASSERT(vap->iv_opmode == IEEE80211_M_MBSS, ("not a MBSS vap"));
2367
2368         *frm++ = IEEE80211_ELEMID_MESHCONF;
2369         *frm++ = sizeof(struct ieee80211_meshconf_ie) - 2;
2370         *frm++ = ms->ms_ppath->mpp_ie;          /* path selection */
2371         *frm++ = ms->ms_pmetric->mpm_ie;        /* link metric */
2372         *frm++ = IEEE80211_MESHCONF_CC_DISABLED;
2373         *frm++ = IEEE80211_MESHCONF_SYNC_NEIGHOFF;
2374         *frm++ = IEEE80211_MESHCONF_AUTH_DISABLED;
2375         /* NB: set the number of neighbors before the rest */
2376         *frm = (ms->ms_neighbors > 15 ? 15 : ms->ms_neighbors) << 1;
2377         if (ms->ms_flags & IEEE80211_MESHFLAGS_PORTAL)
2378                 *frm |= IEEE80211_MESHCONF_FORM_MP;
2379         frm += 1;
2380         caps = 0;
2381         if (ms->ms_flags & IEEE80211_MESHFLAGS_AP)
2382                 caps |= IEEE80211_MESHCONF_CAP_AP;
2383         if (ms->ms_flags & IEEE80211_MESHFLAGS_FWD)
2384                 caps |= IEEE80211_MESHCONF_CAP_FWRD;
2385         ADDSHORT(frm, caps);
2386         return frm;
2387 }
2388
2389 /*
2390  * Add a Mesh Peer Management IE to a frame.
2391  */
2392 uint8_t *
2393 ieee80211_add_meshpeer(uint8_t *frm, uint8_t subtype, uint16_t localid,
2394     uint16_t peerid, uint16_t reason)
2395 {
2396         /* XXX change for AH */
2397         static const uint8_t meshpeerproto[4] = IEEE80211_MESH_PEER_PROTO;
2398
2399         KASSERT(localid != 0, ("localid == 0"));
2400
2401         *frm++ = IEEE80211_ELEMID_MESHPEER;
2402         switch (subtype) {
2403         case IEEE80211_MESH_PEER_LINK_OPEN:
2404                 *frm++ = 6;             /* length */
2405                 memcpy(frm, meshpeerproto, 4);
2406                 frm += 4;
2407                 ADDSHORT(frm, localid); /* local ID */
2408                 break;
2409         case IEEE80211_MESH_PEER_LINK_CONFIRM:
2410                 KASSERT(peerid != 0, ("sending peer confirm without peer id"));
2411                 *frm++ = 8;             /* length */
2412                 memcpy(frm, meshpeerproto, 4);
2413                 frm += 4;
2414                 ADDSHORT(frm, localid); /* local ID */
2415                 ADDSHORT(frm, peerid);  /* peer ID */
2416                 break;
2417         case IEEE80211_MESH_PEER_LINK_CLOSE:
2418                 if (peerid)
2419                         *frm++ = 10;    /* length */
2420                 else
2421                         *frm++ = 8;     /* length */
2422                 memcpy(frm, meshpeerproto, 4);
2423                 frm += 4;
2424                 ADDSHORT(frm, localid); /* local ID */
2425                 if (peerid)
2426                         ADDSHORT(frm, peerid);  /* peer ID */
2427                 ADDSHORT(frm, reason);
2428                 break;
2429         }
2430         return frm;
2431 }
2432
2433 /*
2434  * Compute an Airtime Link Metric for the link with this node.
2435  *
2436  * Based on Draft 3.0 spec (11B.10, p.149).
2437  */
2438 /*
2439  * Max 802.11s overhead.
2440  */
2441 #define IEEE80211_MESH_MAXOVERHEAD \
2442         (sizeof(struct ieee80211_qosframe_addr4) \
2443          + sizeof(struct ieee80211_meshcntl_ae11) \
2444         + sizeof(struct llc) \
2445         + IEEE80211_ADDR_LEN \
2446         + IEEE80211_WEP_IVLEN \
2447         + IEEE80211_WEP_KIDLEN \
2448         + IEEE80211_WEP_CRCLEN \
2449         + IEEE80211_WEP_MICLEN \
2450         + IEEE80211_CRC_LEN)
2451 uint32_t
2452 mesh_airtime_calc(struct ieee80211_node *ni)
2453 {
2454 #define M_BITS 8
2455 #define S_FACTOR (2 * M_BITS)
2456         struct ieee80211com *ic = ni->ni_ic;
2457         struct ifnet *ifp = ni->ni_vap->iv_ifp;
2458         const static int nbits = 8192 << M_BITS;
2459         uint32_t overhead, rate, errrate;
2460         uint64_t res;
2461
2462         /* Time to transmit a frame */
2463         rate = ni->ni_txrate;
2464         overhead = ieee80211_compute_duration(ic->ic_rt,
2465             ifp->if_mtu + IEEE80211_MESH_MAXOVERHEAD, rate, 0) << M_BITS;
2466         /* Error rate in percentage */
2467         /* XXX assuming small failures are ok */
2468         errrate = (((ifp->if_oerrors +
2469             ifp->if_ierrors) / 100) << M_BITS) / 100;
2470         res = (overhead + (nbits / rate)) *
2471             ((1 << S_FACTOR) / ((1 << M_BITS) - errrate));
2472
2473         return (uint32_t)(res >> S_FACTOR);
2474 #undef M_BITS
2475 #undef S_FACTOR
2476 }
2477
2478 /*
2479  * Add a Mesh Link Metric report IE to a frame.
2480  */
2481 uint8_t *
2482 ieee80211_add_meshlmetric(uint8_t *frm, uint32_t metric)
2483 {
2484         *frm++ = IEEE80211_ELEMID_MESHLINK;
2485         *frm++ = 4;
2486         ADDWORD(frm, metric);
2487         return frm;
2488 }
2489 #undef ADDSHORT
2490 #undef ADDWORD
2491
2492 /*
2493  * Initialize any mesh-specific node state.
2494  */
2495 void
2496 ieee80211_mesh_node_init(struct ieee80211vap *vap, struct ieee80211_node *ni)
2497 {
2498         ni->ni_flags |= IEEE80211_NODE_QOS;
2499         callout_init_mp(&ni->ni_mltimer);
2500 }
2501
2502 /*
2503  * Cleanup any mesh-specific node state.
2504  */
2505 void
2506 ieee80211_mesh_node_cleanup(struct ieee80211_node *ni)
2507 {
2508         struct ieee80211vap *vap = ni->ni_vap;
2509         struct ieee80211_mesh_state *ms = vap->iv_mesh;
2510
2511         callout_stop(&ni->ni_mltimer);
2512         /* NB: short-circuit callbacks after mesh_vdetach */
2513         if (vap->iv_mesh != NULL)
2514                 ms->ms_ppath->mpp_peerdown(ni);
2515 }
2516
2517 void
2518 ieee80211_parse_meshid(struct ieee80211_node *ni, const uint8_t *ie)
2519 {
2520         ni->ni_meshidlen = ie[1];
2521         memcpy(ni->ni_meshid, ie + 2, ie[1]);
2522 }
2523
2524 /*
2525  * Setup mesh-specific node state on neighbor discovery.
2526  */
2527 void
2528 ieee80211_mesh_init_neighbor(struct ieee80211_node *ni,
2529         const struct ieee80211_frame *wh,
2530         const struct ieee80211_scanparams *sp)
2531 {
2532         ieee80211_parse_meshid(ni, sp->meshid);
2533 }
2534
2535 void
2536 ieee80211_mesh_update_beacon(struct ieee80211vap *vap,
2537         struct ieee80211_beacon_offsets *bo)
2538 {
2539         KASSERT(vap->iv_opmode == IEEE80211_M_MBSS, ("not a MBSS vap"));
2540
2541         if (isset(bo->bo_flags, IEEE80211_BEACON_MESHCONF)) {
2542                 (void)ieee80211_add_meshconf(bo->bo_meshconf, vap);
2543                 clrbit(bo->bo_flags, IEEE80211_BEACON_MESHCONF);
2544         }
2545 }
2546
2547 static int
2548 mesh_ioctl_get80211(struct ieee80211vap *vap, struct ieee80211req *ireq)
2549 {
2550         struct ieee80211_mesh_state *ms = vap->iv_mesh;
2551         uint8_t tmpmeshid[IEEE80211_NWID_LEN];
2552         struct ieee80211_mesh_route *rt;
2553         struct ieee80211req_mesh_route *imr;
2554         size_t len, off;
2555         uint8_t *p;
2556         int error;
2557
2558         if (vap->iv_opmode != IEEE80211_M_MBSS)
2559                 return ENOSYS;
2560
2561         error = 0;
2562         switch (ireq->i_type) {
2563         case IEEE80211_IOC_MESH_ID:
2564                 ireq->i_len = ms->ms_idlen;
2565                 memcpy(tmpmeshid, ms->ms_id, ireq->i_len);
2566                 error = copyout(tmpmeshid, ireq->i_data, ireq->i_len);
2567                 break;
2568         case IEEE80211_IOC_MESH_AP:
2569                 ireq->i_val = (ms->ms_flags & IEEE80211_MESHFLAGS_AP) != 0;
2570                 break;
2571         case IEEE80211_IOC_MESH_FWRD:
2572                 ireq->i_val = (ms->ms_flags & IEEE80211_MESHFLAGS_FWD) != 0;
2573                 break;
2574         case IEEE80211_IOC_MESH_TTL:
2575                 ireq->i_val = ms->ms_ttl;
2576                 break;
2577         case IEEE80211_IOC_MESH_RTCMD:
2578                 switch (ireq->i_val) {
2579                 case IEEE80211_MESH_RTCMD_LIST:
2580                         len = 0;
2581                         TAILQ_FOREACH(rt, &ms->ms_routes, rt_next) {
2582                                 len += sizeof(*imr);
2583                         }
2584                         if (len > ireq->i_len || ireq->i_len < sizeof(*imr)) {
2585                                 ireq->i_len = len;
2586                                 return ENOMEM;
2587                         }
2588                         ireq->i_len = len;
2589                         /* XXX M_WAIT? */
2590                         p = kmalloc(len, M_TEMP, M_INTWAIT | M_ZERO);
2591                         if (p == NULL)
2592                                 return ENOMEM;
2593                         off = 0;
2594                         TAILQ_FOREACH(rt, &ms->ms_routes, rt_next) {
2595                                 if (off >= len)
2596                                         break;
2597                                 imr = (struct ieee80211req_mesh_route *)
2598                                     (p + off);
2599                                 imr->imr_flags = rt->rt_flags;
2600                                 IEEE80211_ADDR_COPY(imr->imr_dest,
2601                                     rt->rt_dest);
2602                                 IEEE80211_ADDR_COPY(imr->imr_nexthop,
2603                                     rt->rt_nexthop);
2604                                 imr->imr_metric = rt->rt_metric;
2605                                 imr->imr_nhops = rt->rt_nhops;
2606                                 imr->imr_lifetime = rt->rt_lifetime;
2607                                 imr->imr_lastmseq = rt->rt_lastmseq;
2608                                 off += sizeof(*imr);
2609                         }
2610                         error = copyout(p, (uint8_t *)ireq->i_data,
2611                             ireq->i_len);
2612                         kfree(p, M_TEMP);
2613                         break;
2614                 case IEEE80211_MESH_RTCMD_FLUSH:
2615                 case IEEE80211_MESH_RTCMD_ADD:
2616                 case IEEE80211_MESH_RTCMD_DELETE:
2617                         return EINVAL;
2618                 default:
2619                         return ENOSYS;
2620                 }
2621                 break;
2622         case IEEE80211_IOC_MESH_PR_METRIC:
2623                 len = strlen(ms->ms_pmetric->mpm_descr);
2624                 if (ireq->i_len < len)
2625                         return EINVAL;
2626                 ireq->i_len = len;
2627                 error = copyout(ms->ms_pmetric->mpm_descr,
2628                     (uint8_t *)ireq->i_data, len);
2629                 break;
2630         case IEEE80211_IOC_MESH_PR_PATH:
2631                 len = strlen(ms->ms_ppath->mpp_descr);
2632                 if (ireq->i_len < len)
2633                         return EINVAL;
2634                 ireq->i_len = len;
2635                 error = copyout(ms->ms_ppath->mpp_descr,
2636                     (uint8_t *)ireq->i_data, len);
2637                 break;
2638         default:
2639                 return ENOSYS;
2640         }
2641
2642         return error;
2643 }
2644 IEEE80211_IOCTL_GET(mesh, mesh_ioctl_get80211);
2645
2646 static int
2647 mesh_ioctl_set80211(struct ieee80211vap *vap, struct ieee80211req *ireq)
2648 {
2649         struct ieee80211_mesh_state *ms = vap->iv_mesh;
2650         uint8_t tmpmeshid[IEEE80211_NWID_LEN];
2651         uint8_t tmpaddr[IEEE80211_ADDR_LEN];
2652         char tmpproto[IEEE80211_MESH_PROTO_DSZ];
2653         int error;
2654
2655         if (vap->iv_opmode != IEEE80211_M_MBSS)
2656                 return ENOSYS;
2657
2658         error = 0;
2659         switch (ireq->i_type) {
2660         case IEEE80211_IOC_MESH_ID:
2661                 if (ireq->i_val != 0 || ireq->i_len > IEEE80211_MESHID_LEN)
2662                         return EINVAL;
2663                 error = copyin(ireq->i_data, tmpmeshid, ireq->i_len);
2664                 if (error != 0)
2665                         break;
2666                 memset(ms->ms_id, 0, IEEE80211_NWID_LEN);
2667                 ms->ms_idlen = ireq->i_len;
2668                 memcpy(ms->ms_id, tmpmeshid, ireq->i_len);
2669                 error = ENETRESET;
2670                 break;
2671         case IEEE80211_IOC_MESH_AP:
2672                 if (ireq->i_val)
2673                         ms->ms_flags |= IEEE80211_MESHFLAGS_AP;
2674                 else
2675                         ms->ms_flags &= ~IEEE80211_MESHFLAGS_AP;
2676                 error = ENETRESET;
2677                 break;
2678         case IEEE80211_IOC_MESH_FWRD:
2679                 if (ireq->i_val)
2680                         ms->ms_flags |= IEEE80211_MESHFLAGS_FWD;
2681                 else
2682                         ms->ms_flags &= ~IEEE80211_MESHFLAGS_FWD;
2683                 break;
2684         case IEEE80211_IOC_MESH_TTL:
2685                 ms->ms_ttl = (uint8_t) ireq->i_val;
2686                 break;
2687         case IEEE80211_IOC_MESH_RTCMD:
2688                 switch (ireq->i_val) {
2689                 case IEEE80211_MESH_RTCMD_LIST:
2690                         return EINVAL;
2691                 case IEEE80211_MESH_RTCMD_FLUSH:
2692                         ieee80211_mesh_rt_flush(vap);
2693                         break;
2694                 case IEEE80211_MESH_RTCMD_ADD:
2695                         if (IEEE80211_ADDR_EQ(vap->iv_myaddr, ireq->i_data) ||
2696                             IEEE80211_ADDR_EQ(broadcastaddr, ireq->i_data))
2697                                 return EINVAL;
2698                         error = copyin(ireq->i_data, &tmpaddr,
2699                             IEEE80211_ADDR_LEN);
2700                         if (error == 0)
2701                                 ieee80211_mesh_discover(vap, tmpaddr, NULL);
2702                         break;
2703                 case IEEE80211_MESH_RTCMD_DELETE:
2704                         ieee80211_mesh_rt_del(vap, ireq->i_data);
2705                         break;
2706                 default:
2707                         return ENOSYS;
2708                 }
2709                 break;
2710         case IEEE80211_IOC_MESH_PR_METRIC:
2711                 error = copyin(ireq->i_data, tmpproto, sizeof(tmpproto));
2712                 if (error == 0) {
2713                         error = mesh_select_proto_metric(vap, tmpproto);
2714                         if (error == 0)
2715                                 error = ENETRESET;
2716                 }
2717                 break;
2718         case IEEE80211_IOC_MESH_PR_PATH:
2719                 error = copyin(ireq->i_data, tmpproto, sizeof(tmpproto));
2720                 if (error == 0) {
2721                         error = mesh_select_proto_path(vap, tmpproto);
2722                         if (error == 0)
2723                                 error = ENETRESET;
2724                 }
2725                 break;
2726         default:
2727                 return ENOSYS;
2728         }
2729         return error;
2730 }
2731 IEEE80211_IOCTL_SET(mesh, mesh_ioctl_set80211);