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