blob: c3a0b0a4f97ff4631a4a8be5f5873eb55fafa4eb [file] [log] [blame]
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +01001/*
Rui Paulo264d9b72009-11-09 23:46:58 +00002 * Copyright (c) 2008, 2009 open80211s Ltd.
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +01003 * 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 */
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09009#include <linux/gfp.h>
Johannes Berg902acc72008-02-23 15:17:19 +010010#include <linux/kernel.h>
11#include <linux/random.h>
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +010012#include "ieee80211_i.h"
Johannes Berg2c8dccc2008-04-08 15:14:40 -040013#include "rate.h"
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +010014#include "mesh.h"
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +010015
16#ifdef CONFIG_MAC80211_VERBOSE_MPL_DEBUG
17#define mpl_dbg(fmt, args...) printk(KERN_DEBUG fmt, ##args)
18#else
19#define mpl_dbg(fmt, args...) do { (void)(0); } while (0)
20#endif
21
Thomas Pedersen8db09852011-08-12 20:01:00 -070022#define PLINK_GET_LLID(p) (p + 2)
23#define PLINK_GET_PLID(p) (p + 4)
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +010024
25#define mod_plink_timer(s, t) (mod_timer(&s->plink_timer, \
26 jiffies + HZ * t / 1000))
27
Johannes Berg472dbc42008-09-11 00:01:49 +020028#define dot11MeshMaxRetries(s) (s->u.mesh.mshcfg.dot11MeshMaxRetries)
29#define dot11MeshRetryTimeout(s) (s->u.mesh.mshcfg.dot11MeshRetryTimeout)
30#define dot11MeshConfirmTimeout(s) (s->u.mesh.mshcfg.dot11MeshConfirmTimeout)
31#define dot11MeshHoldingTimeout(s) (s->u.mesh.mshcfg.dot11MeshHoldingTimeout)
32#define dot11MeshMaxPeerLinks(s) (s->u.mesh.mshcfg.dot11MeshMaxPeerLinks)
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +010033
Ashok Nagarajan3d4f9692012-03-06 12:48:30 -080034/* We only need a valid sta if user configured a minimum rssi_threshold. */
35#define rssi_threshold_check(sta, sdata) \
Ashok Nagarajan55335132012-02-28 17:04:08 -080036 (sdata->u.mesh.mshcfg.rssi_threshold == 0 ||\
Ashok Nagarajan3d4f9692012-03-06 12:48:30 -080037 (sta && (s8) -ewma_read(&sta->avg_signal) > \
38 sdata->u.mesh.mshcfg.rssi_threshold))
Ashok Nagarajan55335132012-02-28 17:04:08 -080039
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +010040enum plink_event {
41 PLINK_UNDEFINED,
42 OPN_ACPT,
43 OPN_RJCT,
44 OPN_IGNR,
45 CNF_ACPT,
46 CNF_RJCT,
47 CNF_IGNR,
48 CLS_ACPT,
49 CLS_IGNR
50};
51
Thomas Pedersenba4a14e2011-09-20 13:43:32 -070052static int mesh_plink_frame_tx(struct ieee80211_sub_if_data *sdata,
53 enum ieee80211_self_protected_actioncode action,
54 u8 *da, __le16 llid, __le16 plid, __le16 reason);
55
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +010056static inline
57void mesh_plink_inc_estab_count(struct ieee80211_sub_if_data *sdata)
58{
Johannes Berg472dbc42008-09-11 00:01:49 +020059 atomic_inc(&sdata->u.mesh.mshstats.estab_plinks);
Johannes Bergd0709a62008-02-25 16:27:46 +010060 mesh_accept_plinks_update(sdata);
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +010061}
62
63static inline
64void mesh_plink_dec_estab_count(struct ieee80211_sub_if_data *sdata)
65{
Johannes Berg472dbc42008-09-11 00:01:49 +020066 atomic_dec(&sdata->u.mesh.mshstats.estab_plinks);
Johannes Bergd0709a62008-02-25 16:27:46 +010067 mesh_accept_plinks_update(sdata);
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +010068}
69
70/**
71 * mesh_plink_fsm_restart - restart a mesh peer link finite state machine
72 *
Rui Paulo23c7a292009-11-09 23:46:42 +000073 * @sta: mesh peer link to restart
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +010074 *
Johannes Berg07346f812008-05-03 01:02:02 +020075 * Locking: this function must be called holding sta->lock
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +010076 */
77static inline void mesh_plink_fsm_restart(struct sta_info *sta)
78{
Javier Cardona57cf8042011-05-13 10:45:43 -070079 sta->plink_state = NL80211_PLINK_LISTEN;
Luis Carlos Cobo37659ff2008-02-29 12:13:38 -080080 sta->llid = sta->plid = sta->reason = 0;
81 sta->plink_retries = 0;
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +010082}
83
Johannes Berg93e5deb2008-04-01 15:21:00 +020084/*
Thomas Pedersen54ab1ff2012-04-18 19:23:42 -070085 * Allocate mesh sta entry and insert into station table
Johannes Berg93e5deb2008-04-01 15:21:00 +020086 */
Johannes Berg03e44972008-02-27 09:56:40 +010087static struct sta_info *mesh_plink_alloc(struct ieee80211_sub_if_data *sdata,
Thomas Pedersen54ab1ff2012-04-18 19:23:42 -070088 u8 *hw_addr)
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +010089{
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +010090 struct sta_info *sta;
91
Thomas Pedersen54ab1ff2012-04-18 19:23:42 -070092 if (sdata->local->num_sta >= MESH_MAX_PLINKS)
Johannes Berg73651ee2008-02-25 16:27:47 +010093 return NULL;
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +010094
Johannes Berg34e89502010-02-03 13:59:58 +010095 sta = sta_info_alloc(sdata, hw_addr, GFP_KERNEL);
Johannes Berg73651ee2008-02-25 16:27:47 +010096 if (!sta)
97 return NULL;
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +010098
Johannes Berg83d5cc02012-01-12 09:31:10 +010099 sta_info_pre_move_state(sta, IEEE80211_STA_AUTH);
100 sta_info_pre_move_state(sta, IEEE80211_STA_ASSOC);
101 sta_info_pre_move_state(sta, IEEE80211_STA_AUTHORIZED);
Johannes Bergd9a7ddb2011-12-14 12:35:30 +0100102
Johannes Bergc2c98fd2011-09-29 16:04:36 +0200103 set_sta_flag(sta, WLAN_STA_WME);
Johannes Bergd9a7ddb2011-12-14 12:35:30 +0100104
Thomas Pedersen54ab1ff2012-04-18 19:23:42 -0700105 if (sta_info_insert(sta))
106 return NULL;
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100107
108 return sta;
109}
110
111/**
John W. Linvillec9370192010-06-21 17:14:07 -0400112 * __mesh_plink_deactivate - deactivate mesh peer link
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100113 *
114 * @sta: mesh peer link to deactivate
115 *
116 * All mesh paths with this peer as next hop will be flushed
117 *
Johannes Berg07346f812008-05-03 01:02:02 +0200118 * Locking: the caller must hold sta->lock
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100119 */
John W. Linvillec9370192010-06-21 17:14:07 -0400120static bool __mesh_plink_deactivate(struct sta_info *sta)
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100121{
Johannes Bergd0709a62008-02-25 16:27:46 +0100122 struct ieee80211_sub_if_data *sdata = sta->sdata;
John W. Linvillec9370192010-06-21 17:14:07 -0400123 bool deactivated = false;
Johannes Bergd0709a62008-02-25 16:27:46 +0100124
Javier Cardona57cf8042011-05-13 10:45:43 -0700125 if (sta->plink_state == NL80211_PLINK_ESTAB) {
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100126 mesh_plink_dec_estab_count(sdata);
John W. Linvillec9370192010-06-21 17:14:07 -0400127 deactivated = true;
128 }
Javier Cardona57cf8042011-05-13 10:45:43 -0700129 sta->plink_state = NL80211_PLINK_BLOCKED;
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100130 mesh_path_flush_by_nexthop(sta);
John W. Linvillec9370192010-06-21 17:14:07 -0400131
132 return deactivated;
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100133}
134
Johannes Berg902acc72008-02-23 15:17:19 +0100135/**
John W. Linvillec9370192010-06-21 17:14:07 -0400136 * mesh_plink_deactivate - deactivate mesh peer link
Johannes Berg902acc72008-02-23 15:17:19 +0100137 *
138 * @sta: mesh peer link to deactivate
139 *
140 * All mesh paths with this peer as next hop will be flushed
141 */
142void mesh_plink_deactivate(struct sta_info *sta)
143{
John W. Linvillec9370192010-06-21 17:14:07 -0400144 struct ieee80211_sub_if_data *sdata = sta->sdata;
145 bool deactivated;
146
Johannes Berg07346f812008-05-03 01:02:02 +0200147 spin_lock_bh(&sta->lock);
John W. Linvillec9370192010-06-21 17:14:07 -0400148 deactivated = __mesh_plink_deactivate(sta);
Thomas Pedersenba4a14e2011-09-20 13:43:32 -0700149 sta->reason = cpu_to_le16(WLAN_REASON_MESH_PEER_CANCELED);
150 mesh_plink_frame_tx(sdata, WLAN_SP_MESH_PEERING_CLOSE,
151 sta->sta.addr, sta->llid, sta->plid,
152 sta->reason);
Johannes Berg07346f812008-05-03 01:02:02 +0200153 spin_unlock_bh(&sta->lock);
John W. Linvillec9370192010-06-21 17:14:07 -0400154
155 if (deactivated)
156 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BEACON);
Johannes Berg902acc72008-02-23 15:17:19 +0100157}
158
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200159static int mesh_plink_frame_tx(struct ieee80211_sub_if_data *sdata,
Thomas Pedersen54ef6562011-08-11 19:35:12 -0700160 enum ieee80211_self_protected_actioncode action,
161 u8 *da, __le16 llid, __le16 plid, __le16 reason) {
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200162 struct ieee80211_local *local = sdata->local;
Thomas Pedersen3b69a9c2011-10-26 14:47:25 -0700163 struct sk_buff *skb;
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100164 struct ieee80211_mgmt *mgmt;
165 bool include_plid = false;
Thomas Pedersen8db09852011-08-12 20:01:00 -0700166 u16 peering_proto = 0;
Thomas Pedersen3b69a9c2011-10-26 14:47:25 -0700167 u8 *pos, ie_len = 4;
168 int hdr_len = offsetof(struct ieee80211_mgmt, u.action.u.self_prot) +
169 sizeof(mgmt->u.action.u.self_prot);
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100170
Javier Cardona65e8b0c2012-01-17 18:17:46 -0800171 skb = dev_alloc_skb(local->tx_headroom +
Thomas Pedersen3b69a9c2011-10-26 14:47:25 -0700172 hdr_len +
173 2 + /* capability info */
174 2 + /* AID */
175 2 + 8 + /* supported rates */
176 2 + (IEEE80211_MAX_SUPP_RATES - 8) +
177 2 + sdata->u.mesh.mesh_id_len +
178 2 + sizeof(struct ieee80211_meshconf_ie) +
Thomas Pedersen176f3602011-10-26 14:47:27 -0700179 2 + sizeof(struct ieee80211_ht_cap) +
Johannes Berg074d46d2012-03-15 19:45:16 +0100180 2 + sizeof(struct ieee80211_ht_operation) +
Thomas Pedersen3b69a9c2011-10-26 14:47:25 -0700181 2 + 8 + /* peering IE */
182 sdata->u.mesh.ie_len);
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100183 if (!skb)
184 return -1;
Javier Cardona65e8b0c2012-01-17 18:17:46 -0800185 skb_reserve(skb, local->tx_headroom);
Thomas Pedersen3b69a9c2011-10-26 14:47:25 -0700186 mgmt = (struct ieee80211_mgmt *) skb_put(skb, hdr_len);
187 memset(mgmt, 0, hdr_len);
Harvey Harrisone7827a72008-07-15 18:44:13 -0700188 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
189 IEEE80211_STYPE_ACTION);
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100190 memcpy(mgmt->da, da, ETH_ALEN);
Johannes Berg47846c92009-11-25 17:46:19 +0100191 memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
Javier Cardona915b5c52011-05-03 16:57:10 -0700192 memcpy(mgmt->bssid, sdata->vif.addr, ETH_ALEN);
Thomas Pedersen8db09852011-08-12 20:01:00 -0700193 mgmt->u.action.category = WLAN_CATEGORY_SELF_PROTECTED;
194 mgmt->u.action.u.self_prot.action_code = action;
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100195
Thomas Pedersen8db09852011-08-12 20:01:00 -0700196 if (action != WLAN_SP_MESH_PEERING_CLOSE) {
197 /* capability info */
198 pos = skb_put(skb, 2);
199 memset(pos, 0, 2);
Thomas Pedersen54ef6562011-08-11 19:35:12 -0700200 if (action == WLAN_SP_MESH_PEERING_CONFIRM) {
Thomas Pedersen8db09852011-08-12 20:01:00 -0700201 /* AID */
202 pos = skb_put(skb, 2);
Rui Paulo77fa76b2009-11-09 23:46:52 +0000203 memcpy(pos + 2, &plid, 2);
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100204 }
Ashok Nagarajan657c3e02012-04-02 21:21:20 -0700205 if (ieee80211_add_srates_ie(&sdata->vif, skb, true) ||
206 ieee80211_add_ext_srates_ie(&sdata->vif, skb, true) ||
Thomas Pedersen082ebb02011-08-11 19:35:10 -0700207 mesh_add_rsn_ie(skb, sdata) ||
208 mesh_add_meshid_ie(skb, sdata) ||
209 mesh_add_meshconf_ie(skb, sdata))
210 return -1;
Thomas Pedersen8db09852011-08-12 20:01:00 -0700211 } else { /* WLAN_SP_MESH_PEERING_CLOSE */
212 if (mesh_add_meshid_ie(skb, sdata))
213 return -1;
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100214 }
215
Thomas Pedersen8db09852011-08-12 20:01:00 -0700216 /* Add Mesh Peering Management element */
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100217 switch (action) {
Thomas Pedersen54ef6562011-08-11 19:35:12 -0700218 case WLAN_SP_MESH_PEERING_OPEN:
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100219 break;
Thomas Pedersen54ef6562011-08-11 19:35:12 -0700220 case WLAN_SP_MESH_PEERING_CONFIRM:
Thomas Pedersen8db09852011-08-12 20:01:00 -0700221 ie_len += 2;
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100222 include_plid = true;
223 break;
Thomas Pedersen54ef6562011-08-11 19:35:12 -0700224 case WLAN_SP_MESH_PEERING_CLOSE:
Thomas Pedersen8db09852011-08-12 20:01:00 -0700225 if (plid) {
226 ie_len += 2;
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100227 include_plid = true;
228 }
Thomas Pedersen8db09852011-08-12 20:01:00 -0700229 ie_len += 2; /* reason code */
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100230 break;
Thomas Pedersen8db09852011-08-12 20:01:00 -0700231 default:
232 return -EINVAL;
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100233 }
234
Thomas Pedersen8db09852011-08-12 20:01:00 -0700235 if (WARN_ON(skb_tailroom(skb) < 2 + ie_len))
236 return -ENOMEM;
237
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100238 pos = skb_put(skb, 2 + ie_len);
Thomas Pedersen8db09852011-08-12 20:01:00 -0700239 *pos++ = WLAN_EID_PEER_MGMT;
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100240 *pos++ = ie_len;
Thomas Pedersen8db09852011-08-12 20:01:00 -0700241 memcpy(pos, &peering_proto, 2);
242 pos += 2;
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100243 memcpy(pos, &llid, 2);
Thomas Pedersen8db09852011-08-12 20:01:00 -0700244 pos += 2;
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100245 if (include_plid) {
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100246 memcpy(pos, &plid, 2);
Thomas Pedersen8db09852011-08-12 20:01:00 -0700247 pos += 2;
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100248 }
Thomas Pedersen54ef6562011-08-11 19:35:12 -0700249 if (action == WLAN_SP_MESH_PEERING_CLOSE) {
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100250 memcpy(pos, &reason, 2);
Thomas Pedersen8db09852011-08-12 20:01:00 -0700251 pos += 2;
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100252 }
Thomas Pedersen176f3602011-10-26 14:47:27 -0700253
254 if (action != WLAN_SP_MESH_PEERING_CLOSE) {
255 if (mesh_add_ht_cap_ie(skb, sdata) ||
Johannes Berg074d46d2012-03-15 19:45:16 +0100256 mesh_add_ht_oper_ie(skb, sdata))
Thomas Pedersen176f3602011-10-26 14:47:27 -0700257 return -1;
258 }
259
Thomas Pedersen8db09852011-08-12 20:01:00 -0700260 if (mesh_add_vendor_ies(skb, sdata))
261 return -1;
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100262
Johannes Berg62ae67b2009-11-18 18:42:05 +0100263 ieee80211_tx_skb(sdata, skb);
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100264 return 0;
265}
266
Thomas Pedersen54ab1ff2012-04-18 19:23:42 -0700267/* mesh_peer_init - initialize new mesh peer and return resulting sta_info
268 *
269 * @sdata: local meshif
270 * @addr: peer's address
271 * @rates: station's supported rates
272 * @elems: IEs from beacon or mesh peering frame
273 *
274 * call under RCU
275 */
276static struct sta_info *mesh_peer_init(struct ieee80211_sub_if_data *sdata,
277 u8 *addr, u32 rates,
278 struct ieee802_11_elems *elems)
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100279{
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200280 struct ieee80211_local *local = sdata->local;
Thomas Pedersen54ab1ff2012-04-18 19:23:42 -0700281 struct ieee80211_supported_band *sband;
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100282 struct sta_info *sta;
283
Thomas Pedersen54ab1ff2012-04-18 19:23:42 -0700284 sband = local->hw.wiphy->bands[local->oper_channel->band];
Johannes Bergd0709a62008-02-25 16:27:46 +0100285
Thomas Pedersen54ab1ff2012-04-18 19:23:42 -0700286 sta = sta_info_get(sdata, addr);
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100287 if (!sta) {
Thomas Pedersen54ab1ff2012-04-18 19:23:42 -0700288 sta = mesh_plink_alloc(sdata, addr);
Johannes Berg34e89502010-02-03 13:59:58 +0100289 if (!sta)
Thomas Pedersen54ab1ff2012-04-18 19:23:42 -0700290 return NULL;
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100291 }
292
Thomas Pedersen54ab1ff2012-04-18 19:23:42 -0700293 spin_lock_bh(&sta->lock);
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100294 sta->last_rx = jiffies;
Johannes Berg323ce792008-09-11 02:45:11 +0200295 sta->sta.supp_rates[local->hw.conf.channel->band] = rates;
Thomas Pedersen54ab1ff2012-04-18 19:23:42 -0700296 if (elems->ht_cap_elem)
297 ieee80211_ht_cap_ie_to_sta_ht_cap(sdata, sband,
298 elems->ht_cap_elem,
299 &sta->sta.ht_cap);
300 else
301 memset(&sta->sta.ht_cap, 0, sizeof(sta->sta.ht_cap));
302
303 rate_control_rate_init(sta);
304 spin_unlock_bh(&sta->lock);
305
306 return sta;
307}
308
309void mesh_neighbour_update(u8 *hw_addr, u32 rates,
310 struct ieee80211_sub_if_data *sdata,
311 struct ieee802_11_elems *elems)
312{
313 struct sta_info *sta;
314
315 /* Userspace handles peer allocation when security is enabled */
316 if (sdata->u.mesh.security & IEEE80211_MESH_SEC_AUTHED) {
317 cfg80211_notify_new_peer_candidate(sdata->dev, hw_addr,
318 elems->ie_start,
319 elems->total_len,
320 GFP_KERNEL);
321 return;
322 }
323
324 rcu_read_lock();
325 sta = mesh_peer_init(sdata, hw_addr, rates, elems);
326 if (!sta)
327 goto out;
328
Javier Cardona1570ca52011-04-07 15:08:35 -0700329 if (mesh_peer_accepts_plinks(elems) &&
Thomas Pedersen54ab1ff2012-04-18 19:23:42 -0700330 sta->plink_state == NL80211_PLINK_LISTEN &&
331 sdata->u.mesh.accepting_plinks &&
332 sdata->u.mesh.mshcfg.auto_open_plinks &&
333 rssi_threshold_check(sta, sdata))
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100334 mesh_plink_open(sta);
335
Thomas Pedersen54ab1ff2012-04-18 19:23:42 -0700336out:
Johannes Bergd0709a62008-02-25 16:27:46 +0100337 rcu_read_unlock();
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100338}
339
340static void mesh_plink_timer(unsigned long data)
341{
342 struct sta_info *sta;
343 __le16 llid, plid, reason;
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100344 struct ieee80211_sub_if_data *sdata;
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100345
Johannes Bergd0709a62008-02-25 16:27:46 +0100346 /*
347 * This STA is valid because sta_info_destroy() will
348 * del_timer_sync() this timer after having made sure
349 * it cannot be readded (by deleting the plink.)
350 */
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100351 sta = (struct sta_info *) data;
352
Johannes Berg5bb644a2009-05-17 11:40:42 +0200353 if (sta->sdata->local->quiescing) {
354 sta->plink_timer_was_running = true;
355 return;
356 }
357
Johannes Berg07346f812008-05-03 01:02:02 +0200358 spin_lock_bh(&sta->lock);
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100359 if (sta->ignore_plink_timer) {
360 sta->ignore_plink_timer = false;
Johannes Berg07346f812008-05-03 01:02:02 +0200361 spin_unlock_bh(&sta->lock);
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100362 return;
363 }
Johannes Berg0c68ae262008-10-27 15:56:10 -0700364 mpl_dbg("Mesh plink timer for %pM fired on state %d\n",
365 sta->sta.addr, sta->plink_state);
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100366 reason = 0;
367 llid = sta->llid;
368 plid = sta->plid;
Johannes Bergd0709a62008-02-25 16:27:46 +0100369 sdata = sta->sdata;
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100370
371 switch (sta->plink_state) {
Javier Cardona57cf8042011-05-13 10:45:43 -0700372 case NL80211_PLINK_OPN_RCVD:
373 case NL80211_PLINK_OPN_SNT:
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100374 /* retry timer */
375 if (sta->plink_retries < dot11MeshMaxRetries(sdata)) {
376 u32 rand;
Johannes Berg0c68ae262008-10-27 15:56:10 -0700377 mpl_dbg("Mesh plink for %pM (retry, timeout): %d %d\n",
378 sta->sta.addr, sta->plink_retries,
379 sta->plink_timeout);
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100380 get_random_bytes(&rand, sizeof(u32));
381 sta->plink_timeout = sta->plink_timeout +
382 rand % sta->plink_timeout;
383 ++sta->plink_retries;
Johannes Bergd0709a62008-02-25 16:27:46 +0100384 mod_plink_timer(sta, sta->plink_timeout);
Johannes Berg07346f812008-05-03 01:02:02 +0200385 spin_unlock_bh(&sta->lock);
Thomas Pedersen54ef6562011-08-11 19:35:12 -0700386 mesh_plink_frame_tx(sdata, WLAN_SP_MESH_PEERING_OPEN,
387 sta->sta.addr, llid, 0, 0);
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100388 break;
389 }
Thomas Pedersen54ef6562011-08-11 19:35:12 -0700390 reason = cpu_to_le16(WLAN_REASON_MESH_MAX_RETRIES);
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100391 /* fall through on else */
Javier Cardona57cf8042011-05-13 10:45:43 -0700392 case NL80211_PLINK_CNF_RCVD:
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100393 /* confirm timer */
394 if (!reason)
Thomas Pedersen54ef6562011-08-11 19:35:12 -0700395 reason = cpu_to_le16(WLAN_REASON_MESH_CONFIRM_TIMEOUT);
Javier Cardona57cf8042011-05-13 10:45:43 -0700396 sta->plink_state = NL80211_PLINK_HOLDING;
Johannes Bergd0709a62008-02-25 16:27:46 +0100397 mod_plink_timer(sta, dot11MeshHoldingTimeout(sdata));
Johannes Berg07346f812008-05-03 01:02:02 +0200398 spin_unlock_bh(&sta->lock);
Thomas Pedersen54ef6562011-08-11 19:35:12 -0700399 mesh_plink_frame_tx(sdata, WLAN_SP_MESH_PEERING_CLOSE,
400 sta->sta.addr, llid, plid, reason);
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100401 break;
Javier Cardona57cf8042011-05-13 10:45:43 -0700402 case NL80211_PLINK_HOLDING:
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100403 /* holding timer */
Johannes Bergd0709a62008-02-25 16:27:46 +0100404 del_timer(&sta->plink_timer);
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100405 mesh_plink_fsm_restart(sta);
Johannes Berg07346f812008-05-03 01:02:02 +0200406 spin_unlock_bh(&sta->lock);
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100407 break;
408 default:
Johannes Berg07346f812008-05-03 01:02:02 +0200409 spin_unlock_bh(&sta->lock);
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100410 break;
411 }
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100412}
413
Johannes Berg5bb644a2009-05-17 11:40:42 +0200414#ifdef CONFIG_PM
415void mesh_plink_quiesce(struct sta_info *sta)
416{
417 if (del_timer_sync(&sta->plink_timer))
418 sta->plink_timer_was_running = true;
419}
420
421void mesh_plink_restart(struct sta_info *sta)
422{
423 if (sta->plink_timer_was_running) {
424 add_timer(&sta->plink_timer);
425 sta->plink_timer_was_running = false;
426 }
427}
428#endif
429
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100430static inline void mesh_plink_timer_set(struct sta_info *sta, int timeout)
431{
432 sta->plink_timer.expires = jiffies + (HZ * timeout / 1000);
433 sta->plink_timer.data = (unsigned long) sta;
434 sta->plink_timer.function = mesh_plink_timer;
435 sta->plink_timeout = timeout;
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100436 add_timer(&sta->plink_timer);
437}
438
439int mesh_plink_open(struct sta_info *sta)
440{
441 __le16 llid;
Johannes Bergd0709a62008-02-25 16:27:46 +0100442 struct ieee80211_sub_if_data *sdata = sta->sdata;
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100443
Johannes Bergc2c98fd2011-09-29 16:04:36 +0200444 if (!test_sta_flag(sta, WLAN_STA_AUTH))
Javier Cardona53e80512011-04-07 15:08:32 -0700445 return -EPERM;
446
Johannes Berg07346f812008-05-03 01:02:02 +0200447 spin_lock_bh(&sta->lock);
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100448 get_random_bytes(&llid, 2);
449 sta->llid = llid;
Javier Cardona57cf8042011-05-13 10:45:43 -0700450 if (sta->plink_state != NL80211_PLINK_LISTEN) {
Johannes Berg07346f812008-05-03 01:02:02 +0200451 spin_unlock_bh(&sta->lock);
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100452 return -EBUSY;
453 }
Javier Cardona57cf8042011-05-13 10:45:43 -0700454 sta->plink_state = NL80211_PLINK_OPN_SNT;
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100455 mesh_plink_timer_set(sta, dot11MeshRetryTimeout(sdata));
Johannes Berg07346f812008-05-03 01:02:02 +0200456 spin_unlock_bh(&sta->lock);
Johannes Berg0c68ae262008-10-27 15:56:10 -0700457 mpl_dbg("Mesh plink: starting establishment with %pM\n",
458 sta->sta.addr);
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100459
Thomas Pedersen54ef6562011-08-11 19:35:12 -0700460 return mesh_plink_frame_tx(sdata, WLAN_SP_MESH_PEERING_OPEN,
Johannes Berg17741cd2008-09-11 00:02:02 +0200461 sta->sta.addr, llid, 0, 0);
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100462}
463
464void mesh_plink_block(struct sta_info *sta)
465{
John W. Linvillec9370192010-06-21 17:14:07 -0400466 struct ieee80211_sub_if_data *sdata = sta->sdata;
467 bool deactivated;
468
Johannes Berg07346f812008-05-03 01:02:02 +0200469 spin_lock_bh(&sta->lock);
John W. Linvillec9370192010-06-21 17:14:07 -0400470 deactivated = __mesh_plink_deactivate(sta);
Javier Cardona57cf8042011-05-13 10:45:43 -0700471 sta->plink_state = NL80211_PLINK_BLOCKED;
Johannes Berg07346f812008-05-03 01:02:02 +0200472 spin_unlock_bh(&sta->lock);
John W. Linvillec9370192010-06-21 17:14:07 -0400473
474 if (deactivated)
475 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BEACON);
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100476}
477
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100478
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200479void mesh_rx_plink_frame(struct ieee80211_sub_if_data *sdata, struct ieee80211_mgmt *mgmt,
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100480 size_t len, struct ieee80211_rx_status *rx_status)
481{
Johannes Bergd0709a62008-02-25 16:27:46 +0100482 struct ieee80211_local *local = sdata->local;
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100483 struct ieee802_11_elems elems;
484 struct sta_info *sta;
485 enum plink_event event;
Thomas Pedersen54ef6562011-08-11 19:35:12 -0700486 enum ieee80211_self_protected_actioncode ftype;
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100487 size_t baselen;
Christian Lamparterd12c7452010-10-08 22:27:07 +0200488 bool deactivated, matches_local = true;
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100489 u8 ie_len;
490 u8 *baseaddr;
Ashok Nagarajan9ebb61a2012-04-02 21:21:21 -0700491 u32 rates, basic_rates = 0;
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100492 __le16 plid, llid, reason;
Rui Paulo1460dd12009-11-09 23:46:48 +0000493#ifdef CONFIG_MAC80211_VERBOSE_MPL_DEBUG
494 static const char *mplstates[] = {
Javier Cardona57cf8042011-05-13 10:45:43 -0700495 [NL80211_PLINK_LISTEN] = "LISTEN",
496 [NL80211_PLINK_OPN_SNT] = "OPN-SNT",
497 [NL80211_PLINK_OPN_RCVD] = "OPN-RCVD",
498 [NL80211_PLINK_CNF_RCVD] = "CNF_RCVD",
499 [NL80211_PLINK_ESTAB] = "ESTAB",
500 [NL80211_PLINK_HOLDING] = "HOLDING",
501 [NL80211_PLINK_BLOCKED] = "BLOCKED"
Rui Paulo1460dd12009-11-09 23:46:48 +0000502 };
503#endif
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100504
Johannes Berg9c80d3d2008-09-08 15:41:59 +0200505 /* need action_code, aux */
506 if (len < IEEE80211_MIN_ACTION_SIZE + 3)
507 return;
508
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100509 if (is_multicast_ether_addr(mgmt->da)) {
510 mpl_dbg("Mesh plink: ignore frame from multicast address");
511 return;
512 }
513
Thomas Pedersen8db09852011-08-12 20:01:00 -0700514 baseaddr = mgmt->u.action.u.self_prot.variable;
515 baselen = (u8 *) mgmt->u.action.u.self_prot.variable - (u8 *) mgmt;
516 if (mgmt->u.action.u.self_prot.action_code ==
Thomas Pedersen54ef6562011-08-11 19:35:12 -0700517 WLAN_SP_MESH_PEERING_CONFIRM) {
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100518 baseaddr += 4;
David Woo70bdb6b2009-08-12 11:03:44 -0700519 baselen += 4;
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100520 }
521 ieee802_11_parse_elems(baseaddr, len - baselen, &elems);
Thomas Pedersen8db09852011-08-12 20:01:00 -0700522 if (!elems.peering) {
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100523 mpl_dbg("Mesh plink: missing necessary peer link ie\n");
524 return;
525 }
Javier Cardonab130e5c2011-05-03 16:57:07 -0700526 if (elems.rsn_len &&
527 sdata->u.mesh.security == IEEE80211_MESH_SEC_NONE) {
Javier Cardona5cff5e02011-04-07 15:08:29 -0700528 mpl_dbg("Mesh plink: can't establish link with secure peer\n");
529 return;
530 }
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100531
Thomas Pedersen8db09852011-08-12 20:01:00 -0700532 ftype = mgmt->u.action.u.self_prot.action_code;
533 ie_len = elems.peering_len;
534 if ((ftype == WLAN_SP_MESH_PEERING_OPEN && ie_len != 4) ||
535 (ftype == WLAN_SP_MESH_PEERING_CONFIRM && ie_len != 6) ||
536 (ftype == WLAN_SP_MESH_PEERING_CLOSE && ie_len != 6
537 && ie_len != 8)) {
Rui Paulo09383932009-11-09 23:46:43 +0000538 mpl_dbg("Mesh plink: incorrect plink ie length %d %d\n",
539 ftype, ie_len);
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100540 return;
541 }
542
Thomas Pedersen54ef6562011-08-11 19:35:12 -0700543 if (ftype != WLAN_SP_MESH_PEERING_CLOSE &&
544 (!elems.mesh_id || !elems.mesh_config)) {
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100545 mpl_dbg("Mesh plink: missing necessary ie\n");
546 return;
547 }
548 /* Note the lines below are correct, the llid in the frame is the plid
549 * from the point of view of this host.
550 */
Thomas Pedersen8db09852011-08-12 20:01:00 -0700551 memcpy(&plid, PLINK_GET_LLID(elems.peering), 2);
Thomas Pedersen54ef6562011-08-11 19:35:12 -0700552 if (ftype == WLAN_SP_MESH_PEERING_CONFIRM ||
Thomas Pedersen8db09852011-08-12 20:01:00 -0700553 (ftype == WLAN_SP_MESH_PEERING_CLOSE && ie_len == 8))
554 memcpy(&llid, PLINK_GET_PLID(elems.peering), 2);
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100555
Johannes Bergd0709a62008-02-25 16:27:46 +0100556 rcu_read_lock();
557
Johannes Bergabe60632009-11-25 17:46:18 +0100558 sta = sta_info_get(sdata, mgmt->sa);
Thomas Pedersen54ef6562011-08-11 19:35:12 -0700559 if (!sta && ftype != WLAN_SP_MESH_PEERING_OPEN) {
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100560 mpl_dbg("Mesh plink: cls or cnf from unknown peer\n");
Johannes Bergd0709a62008-02-25 16:27:46 +0100561 rcu_read_unlock();
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100562 return;
563 }
564
Ashok Nagarajan55335132012-02-28 17:04:08 -0800565 if (ftype == WLAN_SP_MESH_PEERING_OPEN &&
Ashok Nagarajan3d4f9692012-03-06 12:48:30 -0800566 !rssi_threshold_check(sta, sdata)) {
Ashok Nagarajan55335132012-02-28 17:04:08 -0800567 mpl_dbg("Mesh plink: %pM does not meet rssi threshold\n",
Ashok Nagarajan3d4f9692012-03-06 12:48:30 -0800568 mgmt->sa);
Ashok Nagarajan55335132012-02-28 17:04:08 -0800569 rcu_read_unlock();
570 return;
571 }
572
Johannes Bergc2c98fd2011-09-29 16:04:36 +0200573 if (sta && !test_sta_flag(sta, WLAN_STA_AUTH)) {
Javier Cardona53e80512011-04-07 15:08:32 -0700574 mpl_dbg("Mesh plink: Action frame from non-authed peer\n");
575 rcu_read_unlock();
576 return;
577 }
578
Javier Cardona57cf8042011-05-13 10:45:43 -0700579 if (sta && sta->plink_state == NL80211_PLINK_BLOCKED) {
Johannes Bergd0709a62008-02-25 16:27:46 +0100580 rcu_read_unlock();
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100581 return;
582 }
583
584 /* Now we will figure out the appropriate event... */
585 event = PLINK_UNDEFINED;
Ashok Nagarajan9ebb61a2012-04-02 21:21:21 -0700586 rates = ieee80211_sta_get_rates(local, &elems,
587 rx_status->band, &basic_rates);
588
Thomas Pedersen54ef6562011-08-11 19:35:12 -0700589 if (ftype != WLAN_SP_MESH_PEERING_CLOSE &&
Ashok Nagarajanfe40cb62012-04-02 21:21:22 -0700590 (!mesh_matches_local(&elems, sdata, basic_rates))) {
Christian Lamparterd12c7452010-10-08 22:27:07 +0200591 matches_local = false;
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100592 switch (ftype) {
Thomas Pedersen54ef6562011-08-11 19:35:12 -0700593 case WLAN_SP_MESH_PEERING_OPEN:
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100594 event = OPN_RJCT;
595 break;
Thomas Pedersen54ef6562011-08-11 19:35:12 -0700596 case WLAN_SP_MESH_PEERING_CONFIRM:
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100597 event = CNF_RJCT;
598 break;
Thomas Pedersen54ef6562011-08-11 19:35:12 -0700599 default:
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100600 break;
601 }
Christian Lamparterd12c7452010-10-08 22:27:07 +0200602 }
603
604 if (!sta && !matches_local) {
605 rcu_read_unlock();
Thomas Pedersen54ef6562011-08-11 19:35:12 -0700606 reason = cpu_to_le16(WLAN_REASON_MESH_CONFIG);
Christian Lamparterd12c7452010-10-08 22:27:07 +0200607 llid = 0;
Thomas Pedersen54ef6562011-08-11 19:35:12 -0700608 mesh_plink_frame_tx(sdata, WLAN_SP_MESH_PEERING_CLOSE,
609 mgmt->sa, llid, plid, reason);
Christian Lamparterd12c7452010-10-08 22:27:07 +0200610 return;
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100611 } else if (!sta) {
Thomas Pedersen54ef6562011-08-11 19:35:12 -0700612 /* ftype == WLAN_SP_MESH_PEERING_OPEN */
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100613 if (!mesh_plink_free_count(sdata)) {
614 mpl_dbg("Mesh plink error: no more free plinks\n");
Johannes Berg73651ee2008-02-25 16:27:47 +0100615 rcu_read_unlock();
616 return;
617 }
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100618 event = OPN_ACPT;
Christian Lamparterd12c7452010-10-08 22:27:07 +0200619 } else if (matches_local) {
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100620 switch (ftype) {
Thomas Pedersen54ef6562011-08-11 19:35:12 -0700621 case WLAN_SP_MESH_PEERING_OPEN:
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100622 if (!mesh_plink_free_count(sdata) ||
Johannes Bergd0709a62008-02-25 16:27:46 +0100623 (sta->plid && sta->plid != plid))
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100624 event = OPN_IGNR;
625 else
626 event = OPN_ACPT;
627 break;
Thomas Pedersen54ef6562011-08-11 19:35:12 -0700628 case WLAN_SP_MESH_PEERING_CONFIRM:
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100629 if (!mesh_plink_free_count(sdata) ||
Johannes Bergd0709a62008-02-25 16:27:46 +0100630 (sta->llid != llid || sta->plid != plid))
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100631 event = CNF_IGNR;
632 else
633 event = CNF_ACPT;
634 break;
Thomas Pedersen54ef6562011-08-11 19:35:12 -0700635 case WLAN_SP_MESH_PEERING_CLOSE:
Javier Cardona57cf8042011-05-13 10:45:43 -0700636 if (sta->plink_state == NL80211_PLINK_ESTAB)
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100637 /* Do not check for llid or plid. This does not
638 * follow the standard but since multiple plinks
639 * per sta are not supported, it is necessary in
640 * order to avoid a livelock when MP A sees an
641 * establish peer link to MP B but MP B does not
642 * see it. This can be caused by a timeout in
643 * B's peer link establishment or B beign
644 * restarted.
645 */
646 event = CLS_ACPT;
647 else if (sta->plid != plid)
648 event = CLS_IGNR;
649 else if (ie_len == 7 && sta->llid != llid)
650 event = CLS_IGNR;
651 else
652 event = CLS_ACPT;
653 break;
654 default:
655 mpl_dbg("Mesh plink: unknown frame subtype\n");
Johannes Bergd0709a62008-02-25 16:27:46 +0100656 rcu_read_unlock();
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100657 return;
658 }
Thomas Pedersen54ab1ff2012-04-18 19:23:42 -0700659 }
660
661 if (event == OPN_ACPT) {
662 /* allocate sta entry if necessary and update info */
663 sta = mesh_peer_init(sdata, mgmt->sa, rates, &elems);
664 if (!sta) {
665 mpl_dbg("Mesh plink: failed to init peer!\n");
666 rcu_read_unlock();
667 return;
668 }
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100669 }
670
Rui Paulo1460dd12009-11-09 23:46:48 +0000671 mpl_dbg("Mesh plink (peer, state, llid, plid, event): %pM %s %d %d %d\n",
672 mgmt->sa, mplstates[sta->plink_state],
Johannes Berg0c68ae262008-10-27 15:56:10 -0700673 le16_to_cpu(sta->llid), le16_to_cpu(sta->plid),
674 event);
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100675 reason = 0;
Thomas Pedersen54ab1ff2012-04-18 19:23:42 -0700676 spin_lock_bh(&sta->lock);
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100677 switch (sta->plink_state) {
678 /* spin_unlock as soon as state is updated at each case */
Javier Cardona57cf8042011-05-13 10:45:43 -0700679 case NL80211_PLINK_LISTEN:
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100680 switch (event) {
681 case CLS_ACPT:
682 mesh_plink_fsm_restart(sta);
Johannes Berg07346f812008-05-03 01:02:02 +0200683 spin_unlock_bh(&sta->lock);
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100684 break;
685 case OPN_ACPT:
Javier Cardona57cf8042011-05-13 10:45:43 -0700686 sta->plink_state = NL80211_PLINK_OPN_RCVD;
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100687 sta->plid = plid;
688 get_random_bytes(&llid, 2);
689 sta->llid = llid;
690 mesh_plink_timer_set(sta, dot11MeshRetryTimeout(sdata));
Johannes Berg07346f812008-05-03 01:02:02 +0200691 spin_unlock_bh(&sta->lock);
Thomas Pedersen54ef6562011-08-11 19:35:12 -0700692 mesh_plink_frame_tx(sdata,
693 WLAN_SP_MESH_PEERING_OPEN,
694 sta->sta.addr, llid, 0, 0);
695 mesh_plink_frame_tx(sdata,
696 WLAN_SP_MESH_PEERING_CONFIRM,
697 sta->sta.addr, llid, plid, 0);
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100698 break;
699 default:
Johannes Berg07346f812008-05-03 01:02:02 +0200700 spin_unlock_bh(&sta->lock);
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100701 break;
702 }
703 break;
704
Javier Cardona57cf8042011-05-13 10:45:43 -0700705 case NL80211_PLINK_OPN_SNT:
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100706 switch (event) {
707 case OPN_RJCT:
708 case CNF_RJCT:
Thomas Pedersen54ef6562011-08-11 19:35:12 -0700709 reason = cpu_to_le16(WLAN_REASON_MESH_CONFIG);
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100710 case CLS_ACPT:
711 if (!reason)
Thomas Pedersen54ef6562011-08-11 19:35:12 -0700712 reason = cpu_to_le16(WLAN_REASON_MESH_CLOSE);
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100713 sta->reason = reason;
Javier Cardona57cf8042011-05-13 10:45:43 -0700714 sta->plink_state = NL80211_PLINK_HOLDING;
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100715 if (!mod_plink_timer(sta,
716 dot11MeshHoldingTimeout(sdata)))
717 sta->ignore_plink_timer = true;
718
719 llid = sta->llid;
Johannes Berg07346f812008-05-03 01:02:02 +0200720 spin_unlock_bh(&sta->lock);
Thomas Pedersen54ef6562011-08-11 19:35:12 -0700721 mesh_plink_frame_tx(sdata,
722 WLAN_SP_MESH_PEERING_CLOSE,
723 sta->sta.addr, llid, plid, reason);
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100724 break;
725 case OPN_ACPT:
726 /* retry timer is left untouched */
Javier Cardona57cf8042011-05-13 10:45:43 -0700727 sta->plink_state = NL80211_PLINK_OPN_RCVD;
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100728 sta->plid = plid;
729 llid = sta->llid;
Johannes Berg07346f812008-05-03 01:02:02 +0200730 spin_unlock_bh(&sta->lock);
Thomas Pedersen54ef6562011-08-11 19:35:12 -0700731 mesh_plink_frame_tx(sdata,
732 WLAN_SP_MESH_PEERING_CONFIRM,
733 sta->sta.addr, llid, plid, 0);
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100734 break;
735 case CNF_ACPT:
Javier Cardona57cf8042011-05-13 10:45:43 -0700736 sta->plink_state = NL80211_PLINK_CNF_RCVD;
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100737 if (!mod_plink_timer(sta,
738 dot11MeshConfirmTimeout(sdata)))
739 sta->ignore_plink_timer = true;
740
Johannes Berg07346f812008-05-03 01:02:02 +0200741 spin_unlock_bh(&sta->lock);
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100742 break;
743 default:
Johannes Berg07346f812008-05-03 01:02:02 +0200744 spin_unlock_bh(&sta->lock);
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100745 break;
746 }
747 break;
748
Javier Cardona57cf8042011-05-13 10:45:43 -0700749 case NL80211_PLINK_OPN_RCVD:
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100750 switch (event) {
751 case OPN_RJCT:
752 case CNF_RJCT:
Thomas Pedersen54ef6562011-08-11 19:35:12 -0700753 reason = cpu_to_le16(WLAN_REASON_MESH_CONFIG);
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100754 case CLS_ACPT:
755 if (!reason)
Thomas Pedersen54ef6562011-08-11 19:35:12 -0700756 reason = cpu_to_le16(WLAN_REASON_MESH_CLOSE);
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100757 sta->reason = reason;
Javier Cardona57cf8042011-05-13 10:45:43 -0700758 sta->plink_state = NL80211_PLINK_HOLDING;
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100759 if (!mod_plink_timer(sta,
760 dot11MeshHoldingTimeout(sdata)))
761 sta->ignore_plink_timer = true;
762
763 llid = sta->llid;
Johannes Berg07346f812008-05-03 01:02:02 +0200764 spin_unlock_bh(&sta->lock);
Thomas Pedersen54ef6562011-08-11 19:35:12 -0700765 mesh_plink_frame_tx(sdata, WLAN_SP_MESH_PEERING_CLOSE,
766 sta->sta.addr, llid, plid, reason);
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100767 break;
768 case OPN_ACPT:
769 llid = sta->llid;
Johannes Berg07346f812008-05-03 01:02:02 +0200770 spin_unlock_bh(&sta->lock);
Thomas Pedersen54ef6562011-08-11 19:35:12 -0700771 mesh_plink_frame_tx(sdata,
772 WLAN_SP_MESH_PEERING_CONFIRM,
773 sta->sta.addr, llid, plid, 0);
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100774 break;
775 case CNF_ACPT:
Johannes Bergd0709a62008-02-25 16:27:46 +0100776 del_timer(&sta->plink_timer);
Javier Cardona57cf8042011-05-13 10:45:43 -0700777 sta->plink_state = NL80211_PLINK_ESTAB;
Johannes Berg07346f812008-05-03 01:02:02 +0200778 spin_unlock_bh(&sta->lock);
John W. Linvillec9370192010-06-21 17:14:07 -0400779 mesh_plink_inc_estab_count(sdata);
780 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BEACON);
Johannes Berg0c68ae262008-10-27 15:56:10 -0700781 mpl_dbg("Mesh plink with %pM ESTABLISHED\n",
782 sta->sta.addr);
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100783 break;
784 default:
Johannes Berg07346f812008-05-03 01:02:02 +0200785 spin_unlock_bh(&sta->lock);
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100786 break;
787 }
788 break;
789
Javier Cardona57cf8042011-05-13 10:45:43 -0700790 case NL80211_PLINK_CNF_RCVD:
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100791 switch (event) {
792 case OPN_RJCT:
793 case CNF_RJCT:
Thomas Pedersen54ef6562011-08-11 19:35:12 -0700794 reason = cpu_to_le16(WLAN_REASON_MESH_CONFIG);
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100795 case CLS_ACPT:
796 if (!reason)
Thomas Pedersen54ef6562011-08-11 19:35:12 -0700797 reason = cpu_to_le16(WLAN_REASON_MESH_CLOSE);
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100798 sta->reason = reason;
Javier Cardona57cf8042011-05-13 10:45:43 -0700799 sta->plink_state = NL80211_PLINK_HOLDING;
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100800 if (!mod_plink_timer(sta,
801 dot11MeshHoldingTimeout(sdata)))
802 sta->ignore_plink_timer = true;
803
804 llid = sta->llid;
Johannes Berg07346f812008-05-03 01:02:02 +0200805 spin_unlock_bh(&sta->lock);
Thomas Pedersen54ef6562011-08-11 19:35:12 -0700806 mesh_plink_frame_tx(sdata,
807 WLAN_SP_MESH_PEERING_CLOSE,
808 sta->sta.addr, llid, plid, reason);
Johannes Bergff59dc72008-02-25 10:11:50 +0100809 break;
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100810 case OPN_ACPT:
Johannes Bergd0709a62008-02-25 16:27:46 +0100811 del_timer(&sta->plink_timer);
Javier Cardona57cf8042011-05-13 10:45:43 -0700812 sta->plink_state = NL80211_PLINK_ESTAB;
Johannes Berg07346f812008-05-03 01:02:02 +0200813 spin_unlock_bh(&sta->lock);
John W. Linvillec9370192010-06-21 17:14:07 -0400814 mesh_plink_inc_estab_count(sdata);
815 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BEACON);
Johannes Berg0c68ae262008-10-27 15:56:10 -0700816 mpl_dbg("Mesh plink with %pM ESTABLISHED\n",
817 sta->sta.addr);
Thomas Pedersen54ef6562011-08-11 19:35:12 -0700818 mesh_plink_frame_tx(sdata,
819 WLAN_SP_MESH_PEERING_CONFIRM,
820 sta->sta.addr, llid, plid, 0);
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100821 break;
822 default:
Johannes Berg07346f812008-05-03 01:02:02 +0200823 spin_unlock_bh(&sta->lock);
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100824 break;
825 }
826 break;
827
Javier Cardona57cf8042011-05-13 10:45:43 -0700828 case NL80211_PLINK_ESTAB:
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100829 switch (event) {
830 case CLS_ACPT:
Thomas Pedersen54ef6562011-08-11 19:35:12 -0700831 reason = cpu_to_le16(WLAN_REASON_MESH_CLOSE);
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100832 sta->reason = reason;
John W. Linvillec9370192010-06-21 17:14:07 -0400833 deactivated = __mesh_plink_deactivate(sta);
Javier Cardona57cf8042011-05-13 10:45:43 -0700834 sta->plink_state = NL80211_PLINK_HOLDING;
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100835 llid = sta->llid;
Johannes Bergd0709a62008-02-25 16:27:46 +0100836 mod_plink_timer(sta, dot11MeshHoldingTimeout(sdata));
Johannes Berg07346f812008-05-03 01:02:02 +0200837 spin_unlock_bh(&sta->lock);
John W. Linvillec9370192010-06-21 17:14:07 -0400838 if (deactivated)
839 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BEACON);
Thomas Pedersen54ef6562011-08-11 19:35:12 -0700840 mesh_plink_frame_tx(sdata, WLAN_SP_MESH_PEERING_CLOSE,
841 sta->sta.addr, llid, plid, reason);
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100842 break;
843 case OPN_ACPT:
844 llid = sta->llid;
Johannes Berg07346f812008-05-03 01:02:02 +0200845 spin_unlock_bh(&sta->lock);
Thomas Pedersen54ef6562011-08-11 19:35:12 -0700846 mesh_plink_frame_tx(sdata,
847 WLAN_SP_MESH_PEERING_CONFIRM,
848 sta->sta.addr, llid, plid, 0);
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100849 break;
850 default:
Johannes Berg07346f812008-05-03 01:02:02 +0200851 spin_unlock_bh(&sta->lock);
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100852 break;
853 }
854 break;
Javier Cardona57cf8042011-05-13 10:45:43 -0700855 case NL80211_PLINK_HOLDING:
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100856 switch (event) {
857 case CLS_ACPT:
Johannes Bergd0709a62008-02-25 16:27:46 +0100858 if (del_timer(&sta->plink_timer))
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100859 sta->ignore_plink_timer = 1;
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100860 mesh_plink_fsm_restart(sta);
Johannes Berg07346f812008-05-03 01:02:02 +0200861 spin_unlock_bh(&sta->lock);
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100862 break;
863 case OPN_ACPT:
864 case CNF_ACPT:
865 case OPN_RJCT:
866 case CNF_RJCT:
867 llid = sta->llid;
868 reason = sta->reason;
Johannes Berg07346f812008-05-03 01:02:02 +0200869 spin_unlock_bh(&sta->lock);
Thomas Pedersen54ef6562011-08-11 19:35:12 -0700870 mesh_plink_frame_tx(sdata, WLAN_SP_MESH_PEERING_CLOSE,
871 sta->sta.addr, llid, plid, reason);
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100872 break;
873 default:
Johannes Berg07346f812008-05-03 01:02:02 +0200874 spin_unlock_bh(&sta->lock);
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100875 }
876 break;
877 default:
Luis Carlos Cobob4e08ea2008-02-29 15:46:08 -0800878 /* should not get here, PLINK_BLOCKED is dealt with at the
Daniel Mack3ad2f3f2010-02-03 08:01:28 +0800879 * beginning of the function
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100880 */
Johannes Berg07346f812008-05-03 01:02:02 +0200881 spin_unlock_bh(&sta->lock);
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100882 break;
883 }
Johannes Bergd0709a62008-02-25 16:27:46 +0100884
885 rcu_read_unlock();
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100886}