Commit | Line | Data |
---|---|---|
c3896d2c | 1 | /* |
264d9b7d | 2 | * Copyright (c) 2008, 2009 open80211s Ltd. |
c3896d2c LCC |
3 | * Author: Luis Carlos Cobo <luisca@cozybit.com> |
4 | * | |
5 | * This program is free software; you can redistribute it and/or modify | |
6 | * it under the terms of the GNU General Public License version 2 as | |
7 | * published by the Free Software Foundation. | |
8 | */ | |
902acc78 JB |
9 | #include <linux/kernel.h> |
10 | #include <linux/random.h> | |
c3896d2c | 11 | #include "ieee80211_i.h" |
2c8dccc7 | 12 | #include "rate.h" |
c3896d2c | 13 | #include "mesh.h" |
c3896d2c LCC |
14 | |
15 | #ifdef CONFIG_MAC80211_VERBOSE_MPL_DEBUG | |
16 | #define mpl_dbg(fmt, args...) printk(KERN_DEBUG fmt, ##args) | |
17 | #else | |
18 | #define mpl_dbg(fmt, args...) do { (void)(0); } while (0) | |
19 | #endif | |
20 | ||
0938393f RP |
21 | #define PLINK_GET_LLID(p) (p + 4) |
22 | #define PLINK_GET_PLID(p) (p + 6) | |
c3896d2c LCC |
23 | |
24 | #define mod_plink_timer(s, t) (mod_timer(&s->plink_timer, \ | |
25 | jiffies + HZ * t / 1000)) | |
26 | ||
27 | /* Peer link cancel reasons, all subject to ANA approval */ | |
28 | #define MESH_LINK_CANCELLED 2 | |
29 | #define MESH_MAX_NEIGHBORS 3 | |
30 | #define MESH_CAPABILITY_POLICY_VIOLATION 4 | |
31 | #define MESH_CLOSE_RCVD 5 | |
32 | #define MESH_MAX_RETRIES 6 | |
33 | #define MESH_CONFIRM_TIMEOUT 7 | |
34 | #define MESH_SECURITY_ROLE_NEGOTIATION_DIFFERS 8 | |
35 | #define MESH_SECURITY_AUTHENTICATION_IMPOSSIBLE 9 | |
36 | #define MESH_SECURITY_FAILED_VERIFICATION 10 | |
37 | ||
472dbc45 JB |
38 | #define dot11MeshMaxRetries(s) (s->u.mesh.mshcfg.dot11MeshMaxRetries) |
39 | #define dot11MeshRetryTimeout(s) (s->u.mesh.mshcfg.dot11MeshRetryTimeout) | |
40 | #define dot11MeshConfirmTimeout(s) (s->u.mesh.mshcfg.dot11MeshConfirmTimeout) | |
41 | #define dot11MeshHoldingTimeout(s) (s->u.mesh.mshcfg.dot11MeshHoldingTimeout) | |
42 | #define dot11MeshMaxPeerLinks(s) (s->u.mesh.mshcfg.dot11MeshMaxPeerLinks) | |
c3896d2c LCC |
43 | |
44 | enum plink_frame_type { | |
45 | PLINK_OPEN = 0, | |
46 | PLINK_CONFIRM, | |
47 | PLINK_CLOSE | |
48 | }; | |
49 | ||
50 | enum plink_event { | |
51 | PLINK_UNDEFINED, | |
52 | OPN_ACPT, | |
53 | OPN_RJCT, | |
54 | OPN_IGNR, | |
55 | CNF_ACPT, | |
56 | CNF_RJCT, | |
57 | CNF_IGNR, | |
58 | CLS_ACPT, | |
59 | CLS_IGNR | |
60 | }; | |
61 | ||
62 | static inline | |
63 | void mesh_plink_inc_estab_count(struct ieee80211_sub_if_data *sdata) | |
64 | { | |
472dbc45 | 65 | atomic_inc(&sdata->u.mesh.mshstats.estab_plinks); |
d0709a65 | 66 | mesh_accept_plinks_update(sdata); |
8f2fda95 | 67 | ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BEACON); |
c3896d2c LCC |
68 | } |
69 | ||
70 | static inline | |
71 | void mesh_plink_dec_estab_count(struct ieee80211_sub_if_data *sdata) | |
72 | { | |
472dbc45 | 73 | atomic_dec(&sdata->u.mesh.mshstats.estab_plinks); |
d0709a65 | 74 | mesh_accept_plinks_update(sdata); |
8f2fda95 | 75 | ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BEACON); |
c3896d2c LCC |
76 | } |
77 | ||
78 | /** | |
79 | * mesh_plink_fsm_restart - restart a mesh peer link finite state machine | |
80 | * | |
23c7a29c | 81 | * @sta: mesh peer link to restart |
c3896d2c | 82 | * |
07346f81 | 83 | * Locking: this function must be called holding sta->lock |
c3896d2c LCC |
84 | */ |
85 | static inline void mesh_plink_fsm_restart(struct sta_info *sta) | |
86 | { | |
b4e08ea1 | 87 | sta->plink_state = PLINK_LISTEN; |
37659ff8 LCC |
88 | sta->llid = sta->plid = sta->reason = 0; |
89 | sta->plink_retries = 0; | |
c3896d2c LCC |
90 | } |
91 | ||
93e5deb1 JB |
92 | /* |
93 | * NOTE: This is just an alias for sta_info_alloc(), see notes | |
94 | * on it in the lifecycle management section! | |
95 | */ | |
03e4497e | 96 | static struct sta_info *mesh_plink_alloc(struct ieee80211_sub_if_data *sdata, |
881d948c | 97 | u8 *hw_addr, u32 rates) |
c3896d2c | 98 | { |
d0709a65 | 99 | struct ieee80211_local *local = sdata->local; |
c3896d2c LCC |
100 | struct sta_info *sta; |
101 | ||
c3896d2c | 102 | if (local->num_sta >= MESH_MAX_PLINKS) |
73651ee6 | 103 | return NULL; |
c3896d2c | 104 | |
03e4497e | 105 | sta = sta_info_alloc(sdata, hw_addr, GFP_ATOMIC); |
73651ee6 JB |
106 | if (!sta) |
107 | return NULL; | |
c3896d2c | 108 | |
07346f81 | 109 | sta->flags = WLAN_STA_AUTHORIZED; |
323ce79a | 110 | sta->sta.supp_rates[local->hw.conf.channel->band] = rates; |
b973c31a | 111 | rate_control_rate_init(sta); |
c3896d2c LCC |
112 | |
113 | return sta; | |
114 | } | |
115 | ||
116 | /** | |
42096b63 | 117 | * mesh_plink_deactivate - deactivate mesh peer link |
c3896d2c LCC |
118 | * |
119 | * @sta: mesh peer link to deactivate | |
120 | * | |
121 | * All mesh paths with this peer as next hop will be flushed | |
122 | * | |
07346f81 | 123 | * Locking: the caller must hold sta->lock |
c3896d2c | 124 | */ |
902acc78 | 125 | static void __mesh_plink_deactivate(struct sta_info *sta) |
c3896d2c | 126 | { |
d0709a65 JB |
127 | struct ieee80211_sub_if_data *sdata = sta->sdata; |
128 | ||
b4e08ea1 | 129 | if (sta->plink_state == PLINK_ESTAB) |
c3896d2c | 130 | mesh_plink_dec_estab_count(sdata); |
b4e08ea1 | 131 | sta->plink_state = PLINK_BLOCKED; |
c3896d2c LCC |
132 | mesh_path_flush_by_nexthop(sta); |
133 | } | |
134 | ||
902acc78 JB |
135 | /** |
136 | * __mesh_plink_deactivate - deactivate mesh peer link | |
137 | * | |
138 | * @sta: mesh peer link to deactivate | |
139 | * | |
140 | * All mesh paths with this peer as next hop will be flushed | |
141 | */ | |
142 | void mesh_plink_deactivate(struct sta_info *sta) | |
143 | { | |
07346f81 | 144 | spin_lock_bh(&sta->lock); |
902acc78 | 145 | __mesh_plink_deactivate(sta); |
07346f81 | 146 | spin_unlock_bh(&sta->lock); |
902acc78 JB |
147 | } |
148 | ||
f698d856 | 149 | static int mesh_plink_frame_tx(struct ieee80211_sub_if_data *sdata, |
c3896d2c LCC |
150 | enum plink_frame_type action, u8 *da, __le16 llid, __le16 plid, |
151 | __le16 reason) { | |
f698d856 | 152 | struct ieee80211_local *local = sdata->local; |
c3896d2c LCC |
153 | struct sk_buff *skb = dev_alloc_skb(local->hw.extra_tx_headroom + 400); |
154 | struct ieee80211_mgmt *mgmt; | |
155 | bool include_plid = false; | |
0938393f | 156 | static const u8 meshpeeringproto[] = { 0x00, 0x0F, 0xAC, 0x2A }; |
c3896d2c LCC |
157 | u8 *pos; |
158 | int ie_len; | |
159 | ||
160 | if (!skb) | |
161 | return -1; | |
162 | skb_reserve(skb, local->hw.extra_tx_headroom); | |
163 | /* 25 is the size of the common mgmt part (24) plus the size of the | |
164 | * common action part (1) | |
165 | */ | |
166 | mgmt = (struct ieee80211_mgmt *) | |
167 | skb_put(skb, 25 + sizeof(mgmt->u.action.u.plink_action)); | |
168 | memset(mgmt, 0, 25 + sizeof(mgmt->u.action.u.plink_action)); | |
e7827a70 HH |
169 | mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT | |
170 | IEEE80211_STYPE_ACTION); | |
c3896d2c | 171 | memcpy(mgmt->da, da, ETH_ALEN); |
f698d856 | 172 | memcpy(mgmt->sa, sdata->dev->dev_addr, ETH_ALEN); |
c3896d2c | 173 | /* BSSID is left zeroed, wildcard value */ |
0938393f | 174 | mgmt->u.action.category = MESH_PLINK_CATEGORY; |
c3896d2c LCC |
175 | mgmt->u.action.u.plink_action.action_code = action; |
176 | ||
177 | if (action == PLINK_CLOSE) | |
178 | mgmt->u.action.u.plink_action.aux = reason; | |
179 | else { | |
180 | mgmt->u.action.u.plink_action.aux = cpu_to_le16(0x0); | |
181 | if (action == PLINK_CONFIRM) { | |
182 | pos = skb_put(skb, 4); | |
183 | /* two-byte status code followed by two-byte AID */ | |
77fa76bb RP |
184 | memset(pos, 0, 2); |
185 | memcpy(pos + 2, &plid, 2); | |
c3896d2c | 186 | } |
f698d856 | 187 | mesh_mgmt_ies_add(skb, sdata); |
c3896d2c LCC |
188 | } |
189 | ||
190 | /* Add Peer Link Management element */ | |
191 | switch (action) { | |
192 | case PLINK_OPEN: | |
0938393f | 193 | ie_len = 6; |
c3896d2c LCC |
194 | break; |
195 | case PLINK_CONFIRM: | |
0938393f | 196 | ie_len = 8; |
c3896d2c LCC |
197 | include_plid = true; |
198 | break; | |
199 | case PLINK_CLOSE: | |
200 | default: | |
201 | if (!plid) | |
0938393f | 202 | ie_len = 8; |
c3896d2c | 203 | else { |
0938393f | 204 | ie_len = 10; |
c3896d2c LCC |
205 | include_plid = true; |
206 | } | |
207 | break; | |
208 | } | |
209 | ||
210 | pos = skb_put(skb, 2 + ie_len); | |
211 | *pos++ = WLAN_EID_PEER_LINK; | |
212 | *pos++ = ie_len; | |
0938393f RP |
213 | memcpy(pos, meshpeeringproto, sizeof(meshpeeringproto)); |
214 | pos += 4; | |
c3896d2c LCC |
215 | memcpy(pos, &llid, 2); |
216 | if (include_plid) { | |
217 | pos += 2; | |
218 | memcpy(pos, &plid, 2); | |
219 | } | |
220 | if (action == PLINK_CLOSE) { | |
221 | pos += 2; | |
222 | memcpy(pos, &reason, 2); | |
223 | } | |
224 | ||
62ae67be | 225 | ieee80211_tx_skb(sdata, skb); |
c3896d2c LCC |
226 | return 0; |
227 | } | |
228 | ||
881d948c | 229 | void mesh_neighbour_update(u8 *hw_addr, u32 rates, struct ieee80211_sub_if_data *sdata, |
c3896d2c LCC |
230 | bool peer_accepting_plinks) |
231 | { | |
f698d856 | 232 | struct ieee80211_local *local = sdata->local; |
c3896d2c LCC |
233 | struct sta_info *sta; |
234 | ||
d0709a65 JB |
235 | rcu_read_lock(); |
236 | ||
c3896d2c LCC |
237 | sta = sta_info_get(local, hw_addr); |
238 | if (!sta) { | |
03e4497e | 239 | sta = mesh_plink_alloc(sdata, hw_addr, rates); |
73651ee6 JB |
240 | if (!sta) { |
241 | rcu_read_unlock(); | |
242 | return; | |
243 | } | |
244 | if (sta_info_insert(sta)) { | |
d0709a65 | 245 | rcu_read_unlock(); |
c3896d2c | 246 | return; |
d0709a65 | 247 | } |
c3896d2c LCC |
248 | } |
249 | ||
250 | sta->last_rx = jiffies; | |
323ce79a | 251 | sta->sta.supp_rates[local->hw.conf.channel->band] = rates; |
b4e08ea1 | 252 | if (peer_accepting_plinks && sta->plink_state == PLINK_LISTEN && |
472dbc45 JB |
253 | sdata->u.mesh.accepting_plinks && |
254 | sdata->u.mesh.mshcfg.auto_open_plinks) | |
c3896d2c LCC |
255 | mesh_plink_open(sta); |
256 | ||
d0709a65 | 257 | rcu_read_unlock(); |
c3896d2c LCC |
258 | } |
259 | ||
260 | static void mesh_plink_timer(unsigned long data) | |
261 | { | |
262 | struct sta_info *sta; | |
263 | __le16 llid, plid, reason; | |
c3896d2c | 264 | struct ieee80211_sub_if_data *sdata; |
c3896d2c | 265 | |
d0709a65 JB |
266 | /* |
267 | * This STA is valid because sta_info_destroy() will | |
268 | * del_timer_sync() this timer after having made sure | |
269 | * it cannot be readded (by deleting the plink.) | |
270 | */ | |
c3896d2c LCC |
271 | sta = (struct sta_info *) data; |
272 | ||
5bb644a0 JB |
273 | if (sta->sdata->local->quiescing) { |
274 | sta->plink_timer_was_running = true; | |
275 | return; | |
276 | } | |
277 | ||
07346f81 | 278 | spin_lock_bh(&sta->lock); |
c3896d2c LCC |
279 | if (sta->ignore_plink_timer) { |
280 | sta->ignore_plink_timer = false; | |
07346f81 | 281 | spin_unlock_bh(&sta->lock); |
c3896d2c LCC |
282 | return; |
283 | } | |
0c68ae26 JB |
284 | mpl_dbg("Mesh plink timer for %pM fired on state %d\n", |
285 | sta->sta.addr, sta->plink_state); | |
c3896d2c LCC |
286 | reason = 0; |
287 | llid = sta->llid; | |
288 | plid = sta->plid; | |
d0709a65 | 289 | sdata = sta->sdata; |
c3896d2c LCC |
290 | |
291 | switch (sta->plink_state) { | |
b4e08ea1 LCC |
292 | case PLINK_OPN_RCVD: |
293 | case PLINK_OPN_SNT: | |
c3896d2c LCC |
294 | /* retry timer */ |
295 | if (sta->plink_retries < dot11MeshMaxRetries(sdata)) { | |
296 | u32 rand; | |
0c68ae26 JB |
297 | mpl_dbg("Mesh plink for %pM (retry, timeout): %d %d\n", |
298 | sta->sta.addr, sta->plink_retries, | |
299 | sta->plink_timeout); | |
c3896d2c LCC |
300 | get_random_bytes(&rand, sizeof(u32)); |
301 | sta->plink_timeout = sta->plink_timeout + | |
302 | rand % sta->plink_timeout; | |
303 | ++sta->plink_retries; | |
d0709a65 | 304 | mod_plink_timer(sta, sta->plink_timeout); |
07346f81 | 305 | spin_unlock_bh(&sta->lock); |
17741cdc | 306 | mesh_plink_frame_tx(sdata, PLINK_OPEN, sta->sta.addr, llid, |
c3896d2c LCC |
307 | 0, 0); |
308 | break; | |
309 | } | |
310 | reason = cpu_to_le16(MESH_MAX_RETRIES); | |
311 | /* fall through on else */ | |
b4e08ea1 | 312 | case PLINK_CNF_RCVD: |
c3896d2c LCC |
313 | /* confirm timer */ |
314 | if (!reason) | |
315 | reason = cpu_to_le16(MESH_CONFIRM_TIMEOUT); | |
b4e08ea1 | 316 | sta->plink_state = PLINK_HOLDING; |
d0709a65 | 317 | mod_plink_timer(sta, dot11MeshHoldingTimeout(sdata)); |
07346f81 | 318 | spin_unlock_bh(&sta->lock); |
17741cdc | 319 | mesh_plink_frame_tx(sdata, PLINK_CLOSE, sta->sta.addr, llid, plid, |
c3896d2c LCC |
320 | reason); |
321 | break; | |
b4e08ea1 | 322 | case PLINK_HOLDING: |
c3896d2c | 323 | /* holding timer */ |
d0709a65 | 324 | del_timer(&sta->plink_timer); |
c3896d2c | 325 | mesh_plink_fsm_restart(sta); |
07346f81 | 326 | spin_unlock_bh(&sta->lock); |
c3896d2c LCC |
327 | break; |
328 | default: | |
07346f81 | 329 | spin_unlock_bh(&sta->lock); |
c3896d2c LCC |
330 | break; |
331 | } | |
c3896d2c LCC |
332 | } |
333 | ||
5bb644a0 JB |
334 | #ifdef CONFIG_PM |
335 | void mesh_plink_quiesce(struct sta_info *sta) | |
336 | { | |
337 | if (del_timer_sync(&sta->plink_timer)) | |
338 | sta->plink_timer_was_running = true; | |
339 | } | |
340 | ||
341 | void mesh_plink_restart(struct sta_info *sta) | |
342 | { | |
343 | if (sta->plink_timer_was_running) { | |
344 | add_timer(&sta->plink_timer); | |
345 | sta->plink_timer_was_running = false; | |
346 | } | |
347 | } | |
348 | #endif | |
349 | ||
c3896d2c LCC |
350 | static inline void mesh_plink_timer_set(struct sta_info *sta, int timeout) |
351 | { | |
352 | sta->plink_timer.expires = jiffies + (HZ * timeout / 1000); | |
353 | sta->plink_timer.data = (unsigned long) sta; | |
354 | sta->plink_timer.function = mesh_plink_timer; | |
355 | sta->plink_timeout = timeout; | |
c3896d2c LCC |
356 | add_timer(&sta->plink_timer); |
357 | } | |
358 | ||
359 | int mesh_plink_open(struct sta_info *sta) | |
360 | { | |
361 | __le16 llid; | |
d0709a65 | 362 | struct ieee80211_sub_if_data *sdata = sta->sdata; |
c3896d2c | 363 | |
07346f81 | 364 | spin_lock_bh(&sta->lock); |
c3896d2c LCC |
365 | get_random_bytes(&llid, 2); |
366 | sta->llid = llid; | |
b4e08ea1 | 367 | if (sta->plink_state != PLINK_LISTEN) { |
07346f81 | 368 | spin_unlock_bh(&sta->lock); |
c3896d2c LCC |
369 | return -EBUSY; |
370 | } | |
b4e08ea1 | 371 | sta->plink_state = PLINK_OPN_SNT; |
c3896d2c | 372 | mesh_plink_timer_set(sta, dot11MeshRetryTimeout(sdata)); |
07346f81 | 373 | spin_unlock_bh(&sta->lock); |
0c68ae26 JB |
374 | mpl_dbg("Mesh plink: starting establishment with %pM\n", |
375 | sta->sta.addr); | |
c3896d2c | 376 | |
f698d856 | 377 | return mesh_plink_frame_tx(sdata, PLINK_OPEN, |
17741cdc | 378 | sta->sta.addr, llid, 0, 0); |
c3896d2c LCC |
379 | } |
380 | ||
381 | void mesh_plink_block(struct sta_info *sta) | |
382 | { | |
07346f81 | 383 | spin_lock_bh(&sta->lock); |
902acc78 | 384 | __mesh_plink_deactivate(sta); |
b4e08ea1 | 385 | sta->plink_state = PLINK_BLOCKED; |
07346f81 | 386 | spin_unlock_bh(&sta->lock); |
c3896d2c LCC |
387 | } |
388 | ||
c3896d2c | 389 | |
f698d856 | 390 | void mesh_rx_plink_frame(struct ieee80211_sub_if_data *sdata, struct ieee80211_mgmt *mgmt, |
c3896d2c LCC |
391 | size_t len, struct ieee80211_rx_status *rx_status) |
392 | { | |
d0709a65 | 393 | struct ieee80211_local *local = sdata->local; |
c3896d2c LCC |
394 | struct ieee802_11_elems elems; |
395 | struct sta_info *sta; | |
396 | enum plink_event event; | |
397 | enum plink_frame_type ftype; | |
398 | size_t baselen; | |
399 | u8 ie_len; | |
400 | u8 *baseaddr; | |
401 | __le16 plid, llid, reason; | |
1460dd15 RP |
402 | #ifdef CONFIG_MAC80211_VERBOSE_MPL_DEBUG |
403 | static const char *mplstates[] = { | |
404 | [PLINK_LISTEN] = "LISTEN", | |
405 | [PLINK_OPN_SNT] = "OPN-SNT", | |
406 | [PLINK_OPN_RCVD] = "OPN-RCVD", | |
407 | [PLINK_CNF_RCVD] = "CNF_RCVD", | |
408 | [PLINK_ESTAB] = "ESTAB", | |
409 | [PLINK_HOLDING] = "HOLDING", | |
410 | [PLINK_BLOCKED] = "BLOCKED" | |
411 | }; | |
412 | #endif | |
c3896d2c | 413 | |
9c80d3dc JB |
414 | /* need action_code, aux */ |
415 | if (len < IEEE80211_MIN_ACTION_SIZE + 3) | |
416 | return; | |
417 | ||
c3896d2c LCC |
418 | if (is_multicast_ether_addr(mgmt->da)) { |
419 | mpl_dbg("Mesh plink: ignore frame from multicast address"); | |
420 | return; | |
421 | } | |
422 | ||
423 | baseaddr = mgmt->u.action.u.plink_action.variable; | |
424 | baselen = (u8 *) mgmt->u.action.u.plink_action.variable - (u8 *) mgmt; | |
425 | if (mgmt->u.action.u.plink_action.action_code == PLINK_CONFIRM) { | |
426 | baseaddr += 4; | |
70bdb6b2 | 427 | baselen += 4; |
c3896d2c LCC |
428 | } |
429 | ieee802_11_parse_elems(baseaddr, len - baselen, &elems); | |
430 | if (!elems.peer_link) { | |
431 | mpl_dbg("Mesh plink: missing necessary peer link ie\n"); | |
432 | return; | |
433 | } | |
434 | ||
0938393f | 435 | ftype = mgmt->u.action.u.plink_action.action_code; |
c3896d2c | 436 | ie_len = elems.peer_link_len; |
0938393f RP |
437 | if ((ftype == PLINK_OPEN && ie_len != 6) || |
438 | (ftype == PLINK_CONFIRM && ie_len != 8) || | |
439 | (ftype == PLINK_CLOSE && ie_len != 8 && ie_len != 10)) { | |
440 | mpl_dbg("Mesh plink: incorrect plink ie length %d %d\n", | |
441 | ftype, ie_len); | |
c3896d2c LCC |
442 | return; |
443 | } | |
444 | ||
445 | if (ftype != PLINK_CLOSE && (!elems.mesh_id || !elems.mesh_config)) { | |
446 | mpl_dbg("Mesh plink: missing necessary ie\n"); | |
447 | return; | |
448 | } | |
449 | /* Note the lines below are correct, the llid in the frame is the plid | |
450 | * from the point of view of this host. | |
451 | */ | |
452 | memcpy(&plid, PLINK_GET_LLID(elems.peer_link), 2); | |
0938393f | 453 | if (ftype == PLINK_CONFIRM || (ftype == PLINK_CLOSE && ie_len == 10)) |
c3896d2c LCC |
454 | memcpy(&llid, PLINK_GET_PLID(elems.peer_link), 2); |
455 | ||
d0709a65 JB |
456 | rcu_read_lock(); |
457 | ||
c3896d2c LCC |
458 | sta = sta_info_get(local, mgmt->sa); |
459 | if (!sta && ftype != PLINK_OPEN) { | |
460 | mpl_dbg("Mesh plink: cls or cnf from unknown peer\n"); | |
d0709a65 | 461 | rcu_read_unlock(); |
c3896d2c LCC |
462 | return; |
463 | } | |
464 | ||
b4e08ea1 | 465 | if (sta && sta->plink_state == PLINK_BLOCKED) { |
d0709a65 | 466 | rcu_read_unlock(); |
c3896d2c LCC |
467 | return; |
468 | } | |
469 | ||
470 | /* Now we will figure out the appropriate event... */ | |
471 | event = PLINK_UNDEFINED; | |
f698d856 | 472 | if (ftype != PLINK_CLOSE && (!mesh_matches_local(&elems, sdata))) { |
c3896d2c LCC |
473 | switch (ftype) { |
474 | case PLINK_OPEN: | |
475 | event = OPN_RJCT; | |
476 | break; | |
477 | case PLINK_CONFIRM: | |
478 | event = CNF_RJCT; | |
479 | break; | |
480 | case PLINK_CLOSE: | |
481 | /* avoid warning */ | |
482 | break; | |
483 | } | |
07346f81 | 484 | spin_lock_bh(&sta->lock); |
c3896d2c LCC |
485 | } else if (!sta) { |
486 | /* ftype == PLINK_OPEN */ | |
881d948c | 487 | u32 rates; |
c3896d2c LCC |
488 | if (!mesh_plink_free_count(sdata)) { |
489 | mpl_dbg("Mesh plink error: no more free plinks\n"); | |
d0709a65 | 490 | rcu_read_unlock(); |
c3896d2c LCC |
491 | return; |
492 | } | |
493 | ||
494 | rates = ieee80211_sta_get_rates(local, &elems, rx_status->band); | |
03e4497e | 495 | sta = mesh_plink_alloc(sdata, mgmt->sa, rates); |
73651ee6 | 496 | if (!sta) { |
c3896d2c | 497 | mpl_dbg("Mesh plink error: plink table full\n"); |
d0709a65 | 498 | rcu_read_unlock(); |
c3896d2c LCC |
499 | return; |
500 | } | |
73651ee6 | 501 | if (sta_info_insert(sta)) { |
73651ee6 JB |
502 | rcu_read_unlock(); |
503 | return; | |
504 | } | |
c3896d2c | 505 | event = OPN_ACPT; |
07346f81 | 506 | spin_lock_bh(&sta->lock); |
c3896d2c | 507 | } else { |
07346f81 | 508 | spin_lock_bh(&sta->lock); |
c3896d2c LCC |
509 | switch (ftype) { |
510 | case PLINK_OPEN: | |
511 | if (!mesh_plink_free_count(sdata) || | |
d0709a65 | 512 | (sta->plid && sta->plid != plid)) |
c3896d2c LCC |
513 | event = OPN_IGNR; |
514 | else | |
515 | event = OPN_ACPT; | |
516 | break; | |
517 | case PLINK_CONFIRM: | |
518 | if (!mesh_plink_free_count(sdata) || | |
d0709a65 | 519 | (sta->llid != llid || sta->plid != plid)) |
c3896d2c LCC |
520 | event = CNF_IGNR; |
521 | else | |
522 | event = CNF_ACPT; | |
523 | break; | |
524 | case PLINK_CLOSE: | |
b4e08ea1 | 525 | if (sta->plink_state == PLINK_ESTAB) |
c3896d2c LCC |
526 | /* Do not check for llid or plid. This does not |
527 | * follow the standard but since multiple plinks | |
528 | * per sta are not supported, it is necessary in | |
529 | * order to avoid a livelock when MP A sees an | |
530 | * establish peer link to MP B but MP B does not | |
531 | * see it. This can be caused by a timeout in | |
532 | * B's peer link establishment or B beign | |
533 | * restarted. | |
534 | */ | |
535 | event = CLS_ACPT; | |
536 | else if (sta->plid != plid) | |
537 | event = CLS_IGNR; | |
538 | else if (ie_len == 7 && sta->llid != llid) | |
539 | event = CLS_IGNR; | |
540 | else | |
541 | event = CLS_ACPT; | |
542 | break; | |
543 | default: | |
544 | mpl_dbg("Mesh plink: unknown frame subtype\n"); | |
07346f81 | 545 | spin_unlock_bh(&sta->lock); |
d0709a65 | 546 | rcu_read_unlock(); |
c3896d2c LCC |
547 | return; |
548 | } | |
549 | } | |
550 | ||
1460dd15 RP |
551 | mpl_dbg("Mesh plink (peer, state, llid, plid, event): %pM %s %d %d %d\n", |
552 | mgmt->sa, mplstates[sta->plink_state], | |
0c68ae26 JB |
553 | le16_to_cpu(sta->llid), le16_to_cpu(sta->plid), |
554 | event); | |
c3896d2c LCC |
555 | reason = 0; |
556 | switch (sta->plink_state) { | |
557 | /* spin_unlock as soon as state is updated at each case */ | |
b4e08ea1 | 558 | case PLINK_LISTEN: |
c3896d2c LCC |
559 | switch (event) { |
560 | case CLS_ACPT: | |
561 | mesh_plink_fsm_restart(sta); | |
07346f81 | 562 | spin_unlock_bh(&sta->lock); |
c3896d2c LCC |
563 | break; |
564 | case OPN_ACPT: | |
b4e08ea1 | 565 | sta->plink_state = PLINK_OPN_RCVD; |
c3896d2c LCC |
566 | sta->plid = plid; |
567 | get_random_bytes(&llid, 2); | |
568 | sta->llid = llid; | |
569 | mesh_plink_timer_set(sta, dot11MeshRetryTimeout(sdata)); | |
07346f81 | 570 | spin_unlock_bh(&sta->lock); |
17741cdc | 571 | mesh_plink_frame_tx(sdata, PLINK_OPEN, sta->sta.addr, llid, |
c3896d2c | 572 | 0, 0); |
17741cdc | 573 | mesh_plink_frame_tx(sdata, PLINK_CONFIRM, sta->sta.addr, |
c3896d2c LCC |
574 | llid, plid, 0); |
575 | break; | |
576 | default: | |
07346f81 | 577 | spin_unlock_bh(&sta->lock); |
c3896d2c LCC |
578 | break; |
579 | } | |
580 | break; | |
581 | ||
b4e08ea1 | 582 | case PLINK_OPN_SNT: |
c3896d2c LCC |
583 | switch (event) { |
584 | case OPN_RJCT: | |
585 | case CNF_RJCT: | |
586 | reason = cpu_to_le16(MESH_CAPABILITY_POLICY_VIOLATION); | |
587 | case CLS_ACPT: | |
588 | if (!reason) | |
589 | reason = cpu_to_le16(MESH_CLOSE_RCVD); | |
590 | sta->reason = reason; | |
b4e08ea1 | 591 | sta->plink_state = PLINK_HOLDING; |
c3896d2c LCC |
592 | if (!mod_plink_timer(sta, |
593 | dot11MeshHoldingTimeout(sdata))) | |
594 | sta->ignore_plink_timer = true; | |
595 | ||
596 | llid = sta->llid; | |
07346f81 | 597 | spin_unlock_bh(&sta->lock); |
17741cdc | 598 | mesh_plink_frame_tx(sdata, PLINK_CLOSE, sta->sta.addr, llid, |
c3896d2c LCC |
599 | plid, reason); |
600 | break; | |
601 | case OPN_ACPT: | |
602 | /* retry timer is left untouched */ | |
b4e08ea1 | 603 | sta->plink_state = PLINK_OPN_RCVD; |
c3896d2c LCC |
604 | sta->plid = plid; |
605 | llid = sta->llid; | |
07346f81 | 606 | spin_unlock_bh(&sta->lock); |
17741cdc | 607 | mesh_plink_frame_tx(sdata, PLINK_CONFIRM, sta->sta.addr, llid, |
c3896d2c LCC |
608 | plid, 0); |
609 | break; | |
610 | case CNF_ACPT: | |
b4e08ea1 | 611 | sta->plink_state = PLINK_CNF_RCVD; |
c3896d2c LCC |
612 | if (!mod_plink_timer(sta, |
613 | dot11MeshConfirmTimeout(sdata))) | |
614 | sta->ignore_plink_timer = true; | |
615 | ||
07346f81 | 616 | spin_unlock_bh(&sta->lock); |
c3896d2c LCC |
617 | break; |
618 | default: | |
07346f81 | 619 | spin_unlock_bh(&sta->lock); |
c3896d2c LCC |
620 | break; |
621 | } | |
622 | break; | |
623 | ||
b4e08ea1 | 624 | case PLINK_OPN_RCVD: |
c3896d2c LCC |
625 | switch (event) { |
626 | case OPN_RJCT: | |
627 | case CNF_RJCT: | |
628 | reason = cpu_to_le16(MESH_CAPABILITY_POLICY_VIOLATION); | |
629 | case CLS_ACPT: | |
630 | if (!reason) | |
631 | reason = cpu_to_le16(MESH_CLOSE_RCVD); | |
632 | sta->reason = reason; | |
b4e08ea1 | 633 | sta->plink_state = PLINK_HOLDING; |
c3896d2c LCC |
634 | if (!mod_plink_timer(sta, |
635 | dot11MeshHoldingTimeout(sdata))) | |
636 | sta->ignore_plink_timer = true; | |
637 | ||
638 | llid = sta->llid; | |
07346f81 | 639 | spin_unlock_bh(&sta->lock); |
17741cdc | 640 | mesh_plink_frame_tx(sdata, PLINK_CLOSE, sta->sta.addr, llid, |
c3896d2c LCC |
641 | plid, reason); |
642 | break; | |
643 | case OPN_ACPT: | |
644 | llid = sta->llid; | |
07346f81 | 645 | spin_unlock_bh(&sta->lock); |
17741cdc | 646 | mesh_plink_frame_tx(sdata, PLINK_CONFIRM, sta->sta.addr, llid, |
c3896d2c LCC |
647 | plid, 0); |
648 | break; | |
649 | case CNF_ACPT: | |
d0709a65 | 650 | del_timer(&sta->plink_timer); |
b4e08ea1 | 651 | sta->plink_state = PLINK_ESTAB; |
c3896d2c | 652 | mesh_plink_inc_estab_count(sdata); |
07346f81 | 653 | spin_unlock_bh(&sta->lock); |
0c68ae26 JB |
654 | mpl_dbg("Mesh plink with %pM ESTABLISHED\n", |
655 | sta->sta.addr); | |
c3896d2c LCC |
656 | break; |
657 | default: | |
07346f81 | 658 | spin_unlock_bh(&sta->lock); |
c3896d2c LCC |
659 | break; |
660 | } | |
661 | break; | |
662 | ||
b4e08ea1 | 663 | case PLINK_CNF_RCVD: |
c3896d2c LCC |
664 | switch (event) { |
665 | case OPN_RJCT: | |
666 | case CNF_RJCT: | |
667 | reason = cpu_to_le16(MESH_CAPABILITY_POLICY_VIOLATION); | |
668 | case CLS_ACPT: | |
669 | if (!reason) | |
670 | reason = cpu_to_le16(MESH_CLOSE_RCVD); | |
671 | sta->reason = reason; | |
b4e08ea1 | 672 | sta->plink_state = PLINK_HOLDING; |
c3896d2c LCC |
673 | if (!mod_plink_timer(sta, |
674 | dot11MeshHoldingTimeout(sdata))) | |
675 | sta->ignore_plink_timer = true; | |
676 | ||
677 | llid = sta->llid; | |
07346f81 | 678 | spin_unlock_bh(&sta->lock); |
17741cdc | 679 | mesh_plink_frame_tx(sdata, PLINK_CLOSE, sta->sta.addr, llid, |
c3896d2c | 680 | plid, reason); |
ff59dc76 | 681 | break; |
c3896d2c | 682 | case OPN_ACPT: |
d0709a65 | 683 | del_timer(&sta->plink_timer); |
b4e08ea1 | 684 | sta->plink_state = PLINK_ESTAB; |
c3896d2c | 685 | mesh_plink_inc_estab_count(sdata); |
07346f81 | 686 | spin_unlock_bh(&sta->lock); |
0c68ae26 JB |
687 | mpl_dbg("Mesh plink with %pM ESTABLISHED\n", |
688 | sta->sta.addr); | |
17741cdc | 689 | mesh_plink_frame_tx(sdata, PLINK_CONFIRM, sta->sta.addr, llid, |
c3896d2c LCC |
690 | plid, 0); |
691 | break; | |
692 | default: | |
07346f81 | 693 | spin_unlock_bh(&sta->lock); |
c3896d2c LCC |
694 | break; |
695 | } | |
696 | break; | |
697 | ||
b4e08ea1 | 698 | case PLINK_ESTAB: |
c3896d2c LCC |
699 | switch (event) { |
700 | case CLS_ACPT: | |
701 | reason = cpu_to_le16(MESH_CLOSE_RCVD); | |
702 | sta->reason = reason; | |
902acc78 | 703 | __mesh_plink_deactivate(sta); |
b4e08ea1 | 704 | sta->plink_state = PLINK_HOLDING; |
c3896d2c | 705 | llid = sta->llid; |
d0709a65 | 706 | mod_plink_timer(sta, dot11MeshHoldingTimeout(sdata)); |
07346f81 | 707 | spin_unlock_bh(&sta->lock); |
17741cdc | 708 | mesh_plink_frame_tx(sdata, PLINK_CLOSE, sta->sta.addr, llid, |
c3896d2c LCC |
709 | plid, reason); |
710 | break; | |
711 | case OPN_ACPT: | |
712 | llid = sta->llid; | |
07346f81 | 713 | spin_unlock_bh(&sta->lock); |
17741cdc | 714 | mesh_plink_frame_tx(sdata, PLINK_CONFIRM, sta->sta.addr, llid, |
c3896d2c LCC |
715 | plid, 0); |
716 | break; | |
717 | default: | |
07346f81 | 718 | spin_unlock_bh(&sta->lock); |
c3896d2c LCC |
719 | break; |
720 | } | |
721 | break; | |
b4e08ea1 | 722 | case PLINK_HOLDING: |
c3896d2c LCC |
723 | switch (event) { |
724 | case CLS_ACPT: | |
d0709a65 | 725 | if (del_timer(&sta->plink_timer)) |
c3896d2c | 726 | sta->ignore_plink_timer = 1; |
c3896d2c | 727 | mesh_plink_fsm_restart(sta); |
07346f81 | 728 | spin_unlock_bh(&sta->lock); |
c3896d2c LCC |
729 | break; |
730 | case OPN_ACPT: | |
731 | case CNF_ACPT: | |
732 | case OPN_RJCT: | |
733 | case CNF_RJCT: | |
734 | llid = sta->llid; | |
735 | reason = sta->reason; | |
07346f81 | 736 | spin_unlock_bh(&sta->lock); |
17741cdc JB |
737 | mesh_plink_frame_tx(sdata, PLINK_CLOSE, sta->sta.addr, |
738 | llid, plid, reason); | |
c3896d2c LCC |
739 | break; |
740 | default: | |
07346f81 | 741 | spin_unlock_bh(&sta->lock); |
c3896d2c LCC |
742 | } |
743 | break; | |
744 | default: | |
b4e08ea1 LCC |
745 | /* should not get here, PLINK_BLOCKED is dealt with at the |
746 | * beggining of the function | |
c3896d2c | 747 | */ |
07346f81 | 748 | spin_unlock_bh(&sta->lock); |
c3896d2c LCC |
749 | break; |
750 | } | |
d0709a65 JB |
751 | |
752 | rcu_read_unlock(); | |
c3896d2c | 753 | } |