blob: 8cc8461b48a0a037dd129c2f0491093960d410f1 [file] [log] [blame]
Luis Carlos Coboc3896d22008-02-23 15:17:13 +01001/*
Rui Paulo264d9b72009-11-09 23:46:58 +00002 * Copyright (c) 2008, 2009 open80211s Ltd.
Luis Carlos Coboc3896d22008-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 Coboc3896d22008-02-23 15:17:13 +010012#include "ieee80211_i.h"
Johannes Berg2c8dccc2008-04-08 15:14:40 -040013#include "rate.h"
Luis Carlos Coboc3896d22008-02-23 15:17:13 +010014#include "mesh.h"
Luis Carlos Coboc3896d22008-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 Coboc3896d22008-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 Coboc3896d22008-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 Coboc3896d22008-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 Coboc3896d22008-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 Coboc3896d22008-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 Coboc3896d22008-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 Coboc3896d22008-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 Coboc3896d22008-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 Coboc3896d22008-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 Coboc3896d22008-02-23 15:17:13 +010089{
Luis Carlos Coboc3896d22008-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 Coboc3896d22008-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 Coboc3896d22008-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
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100105 return sta;
106}
107
Ashok Nagarajan57aac7c2012-04-30 14:20:30 -0700108/** mesh_set_ht_prot_mode - set correct HT protection mode
109 *
110 * Section 9.23.3.5 of IEEE 80211s standard describes the protection rules for
111 * HT mesh STA in a MBSS. Three HT protection modes are supported for now,
112 * non-HT mixed mode, 20MHz-protection and no-protection mode. non-HT mixed
113 * mode is selected if any non-HT peers are present in our MBSS.
114 * 20MHz-protection mode is selected if all peers in our 20/40MHz MBSS support
115 * HT and atleast one HT20 peer is present. Otherwise no-protection mode is
116 * selected.
117 */
118static u32 mesh_set_ht_prot_mode(struct ieee80211_sub_if_data *sdata)
119{
120 struct ieee80211_local *local = sdata->local;
121 struct sta_info *sta;
122 u32 changed = 0;
123 u16 ht_opmode;
124 bool non_ht_sta = false, ht20_sta = false;
125
126 if (local->_oper_channel_type == NL80211_CHAN_NO_HT)
127 return 0;
128
129 rcu_read_lock();
130 list_for_each_entry_rcu(sta, &local->sta_list, list) {
131 if (sdata == sta->sdata &&
132 sta->plink_state == NL80211_PLINK_ESTAB) {
133 switch (sta->ch_type) {
134 case NL80211_CHAN_NO_HT:
135 mpl_dbg("mesh_plink %pM: nonHT sta (%pM) is present",
136 sdata->vif.addr, sta->sta.addr);
137 non_ht_sta = true;
138 goto out;
139 case NL80211_CHAN_HT20:
140 mpl_dbg("mesh_plink %pM: HT20 sta (%pM) is present",
141 sdata->vif.addr, sta->sta.addr);
142 ht20_sta = true;
143 default:
144 break;
145 }
146 }
147 }
148out:
149 rcu_read_unlock();
150
151 if (non_ht_sta)
152 ht_opmode = IEEE80211_HT_OP_MODE_PROTECTION_NONHT_MIXED;
153 else if (ht20_sta && local->_oper_channel_type > NL80211_CHAN_HT20)
154 ht_opmode = IEEE80211_HT_OP_MODE_PROTECTION_20MHZ;
155 else
156 ht_opmode = IEEE80211_HT_OP_MODE_PROTECTION_NONE;
157
158 if (sdata->vif.bss_conf.ht_operation_mode != ht_opmode) {
159 sdata->vif.bss_conf.ht_operation_mode = ht_opmode;
Ashok Nagarajan70c33ea2012-04-30 14:20:32 -0700160 sdata->u.mesh.mshcfg.ht_opmode = ht_opmode;
Ashok Nagarajan57aac7c2012-04-30 14:20:30 -0700161 changed = BSS_CHANGED_HT;
162 mpl_dbg("mesh_plink %pM: protection mode changed to %d",
163 sdata->vif.addr, ht_opmode);
164 }
165
166 return changed;
167}
168
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100169/**
John W. Linvillec9370192010-06-21 17:14:07 -0400170 * __mesh_plink_deactivate - deactivate mesh peer link
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100171 *
172 * @sta: mesh peer link to deactivate
173 *
174 * All mesh paths with this peer as next hop will be flushed
175 *
Johannes Berg07346f812008-05-03 01:02:02 +0200176 * Locking: the caller must hold sta->lock
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100177 */
John W. Linvillec9370192010-06-21 17:14:07 -0400178static bool __mesh_plink_deactivate(struct sta_info *sta)
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100179{
Johannes Bergd0709a62008-02-25 16:27:46 +0100180 struct ieee80211_sub_if_data *sdata = sta->sdata;
John W. Linvillec9370192010-06-21 17:14:07 -0400181 bool deactivated = false;
Johannes Bergd0709a62008-02-25 16:27:46 +0100182
Javier Cardona57cf8042011-05-13 10:45:43 -0700183 if (sta->plink_state == NL80211_PLINK_ESTAB) {
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100184 mesh_plink_dec_estab_count(sdata);
John W. Linvillec9370192010-06-21 17:14:07 -0400185 deactivated = true;
186 }
Javier Cardona57cf8042011-05-13 10:45:43 -0700187 sta->plink_state = NL80211_PLINK_BLOCKED;
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100188 mesh_path_flush_by_nexthop(sta);
John W. Linvillec9370192010-06-21 17:14:07 -0400189
190 return deactivated;
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100191}
192
Johannes Berg902acc72008-02-23 15:17:19 +0100193/**
John W. Linvillec9370192010-06-21 17:14:07 -0400194 * mesh_plink_deactivate - deactivate mesh peer link
Johannes Berg902acc72008-02-23 15:17:19 +0100195 *
196 * @sta: mesh peer link to deactivate
197 *
198 * All mesh paths with this peer as next hop will be flushed
199 */
200void mesh_plink_deactivate(struct sta_info *sta)
201{
John W. Linvillec9370192010-06-21 17:14:07 -0400202 struct ieee80211_sub_if_data *sdata = sta->sdata;
203 bool deactivated;
204
Johannes Berg07346f812008-05-03 01:02:02 +0200205 spin_lock_bh(&sta->lock);
John W. Linvillec9370192010-06-21 17:14:07 -0400206 deactivated = __mesh_plink_deactivate(sta);
Thomas Pedersenba4a14e2011-09-20 13:43:32 -0700207 sta->reason = cpu_to_le16(WLAN_REASON_MESH_PEER_CANCELED);
208 mesh_plink_frame_tx(sdata, WLAN_SP_MESH_PEERING_CLOSE,
209 sta->sta.addr, sta->llid, sta->plid,
210 sta->reason);
Johannes Berg07346f812008-05-03 01:02:02 +0200211 spin_unlock_bh(&sta->lock);
John W. Linvillec9370192010-06-21 17:14:07 -0400212
213 if (deactivated)
214 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BEACON);
Johannes Berg902acc72008-02-23 15:17:19 +0100215}
216
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200217static int mesh_plink_frame_tx(struct ieee80211_sub_if_data *sdata,
Thomas Pedersen54ef6562011-08-11 19:35:12 -0700218 enum ieee80211_self_protected_actioncode action,
219 u8 *da, __le16 llid, __le16 plid, __le16 reason) {
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200220 struct ieee80211_local *local = sdata->local;
Thomas Pedersen3b69a9c2011-10-26 14:47:25 -0700221 struct sk_buff *skb;
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100222 struct ieee80211_mgmt *mgmt;
223 bool include_plid = false;
Thomas Pedersen8db09852011-08-12 20:01:00 -0700224 u16 peering_proto = 0;
Thomas Pedersen3b69a9c2011-10-26 14:47:25 -0700225 u8 *pos, ie_len = 4;
226 int hdr_len = offsetof(struct ieee80211_mgmt, u.action.u.self_prot) +
227 sizeof(mgmt->u.action.u.self_prot);
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100228
Javier Cardona65e8b0c2012-01-17 18:17:46 -0800229 skb = dev_alloc_skb(local->tx_headroom +
Thomas Pedersen3b69a9c2011-10-26 14:47:25 -0700230 hdr_len +
231 2 + /* capability info */
232 2 + /* AID */
233 2 + 8 + /* supported rates */
234 2 + (IEEE80211_MAX_SUPP_RATES - 8) +
235 2 + sdata->u.mesh.mesh_id_len +
236 2 + sizeof(struct ieee80211_meshconf_ie) +
Thomas Pedersen176f3602011-10-26 14:47:27 -0700237 2 + sizeof(struct ieee80211_ht_cap) +
Johannes Berg074d46d2012-03-15 19:45:16 +0100238 2 + sizeof(struct ieee80211_ht_operation) +
Thomas Pedersen3b69a9c2011-10-26 14:47:25 -0700239 2 + 8 + /* peering IE */
240 sdata->u.mesh.ie_len);
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100241 if (!skb)
242 return -1;
Javier Cardona65e8b0c2012-01-17 18:17:46 -0800243 skb_reserve(skb, local->tx_headroom);
Thomas Pedersen3b69a9c2011-10-26 14:47:25 -0700244 mgmt = (struct ieee80211_mgmt *) skb_put(skb, hdr_len);
245 memset(mgmt, 0, hdr_len);
Harvey Harrisone7827a72008-07-15 18:44:13 -0700246 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
247 IEEE80211_STYPE_ACTION);
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100248 memcpy(mgmt->da, da, ETH_ALEN);
Johannes Berg47846c92009-11-25 17:46:19 +0100249 memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
Javier Cardona915b5c52011-05-03 16:57:10 -0700250 memcpy(mgmt->bssid, sdata->vif.addr, ETH_ALEN);
Thomas Pedersen8db09852011-08-12 20:01:00 -0700251 mgmt->u.action.category = WLAN_CATEGORY_SELF_PROTECTED;
252 mgmt->u.action.u.self_prot.action_code = action;
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100253
Thomas Pedersen8db09852011-08-12 20:01:00 -0700254 if (action != WLAN_SP_MESH_PEERING_CLOSE) {
255 /* capability info */
256 pos = skb_put(skb, 2);
257 memset(pos, 0, 2);
Thomas Pedersen54ef6562011-08-11 19:35:12 -0700258 if (action == WLAN_SP_MESH_PEERING_CONFIRM) {
Thomas Pedersen8db09852011-08-12 20:01:00 -0700259 /* AID */
260 pos = skb_put(skb, 2);
Rui Paulo77fa76b2009-11-09 23:46:52 +0000261 memcpy(pos + 2, &plid, 2);
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100262 }
Ashok Nagarajan657c3e02012-04-02 21:21:20 -0700263 if (ieee80211_add_srates_ie(&sdata->vif, skb, true) ||
264 ieee80211_add_ext_srates_ie(&sdata->vif, skb, true) ||
Thomas Pedersen082ebb02011-08-11 19:35:10 -0700265 mesh_add_rsn_ie(skb, sdata) ||
266 mesh_add_meshid_ie(skb, sdata) ||
267 mesh_add_meshconf_ie(skb, sdata))
268 return -1;
Thomas Pedersen8db09852011-08-12 20:01:00 -0700269 } else { /* WLAN_SP_MESH_PEERING_CLOSE */
270 if (mesh_add_meshid_ie(skb, sdata))
271 return -1;
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100272 }
273
Thomas Pedersen8db09852011-08-12 20:01:00 -0700274 /* Add Mesh Peering Management element */
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100275 switch (action) {
Thomas Pedersen54ef6562011-08-11 19:35:12 -0700276 case WLAN_SP_MESH_PEERING_OPEN:
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100277 break;
Thomas Pedersen54ef6562011-08-11 19:35:12 -0700278 case WLAN_SP_MESH_PEERING_CONFIRM:
Thomas Pedersen8db09852011-08-12 20:01:00 -0700279 ie_len += 2;
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100280 include_plid = true;
281 break;
Thomas Pedersen54ef6562011-08-11 19:35:12 -0700282 case WLAN_SP_MESH_PEERING_CLOSE:
Thomas Pedersen8db09852011-08-12 20:01:00 -0700283 if (plid) {
284 ie_len += 2;
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100285 include_plid = true;
286 }
Thomas Pedersen8db09852011-08-12 20:01:00 -0700287 ie_len += 2; /* reason code */
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100288 break;
Thomas Pedersen8db09852011-08-12 20:01:00 -0700289 default:
290 return -EINVAL;
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100291 }
292
Thomas Pedersen8db09852011-08-12 20:01:00 -0700293 if (WARN_ON(skb_tailroom(skb) < 2 + ie_len))
294 return -ENOMEM;
295
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100296 pos = skb_put(skb, 2 + ie_len);
Thomas Pedersen8db09852011-08-12 20:01:00 -0700297 *pos++ = WLAN_EID_PEER_MGMT;
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100298 *pos++ = ie_len;
Thomas Pedersen8db09852011-08-12 20:01:00 -0700299 memcpy(pos, &peering_proto, 2);
300 pos += 2;
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100301 memcpy(pos, &llid, 2);
Thomas Pedersen8db09852011-08-12 20:01:00 -0700302 pos += 2;
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100303 if (include_plid) {
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100304 memcpy(pos, &plid, 2);
Thomas Pedersen8db09852011-08-12 20:01:00 -0700305 pos += 2;
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100306 }
Thomas Pedersen54ef6562011-08-11 19:35:12 -0700307 if (action == WLAN_SP_MESH_PEERING_CLOSE) {
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100308 memcpy(pos, &reason, 2);
Thomas Pedersen8db09852011-08-12 20:01:00 -0700309 pos += 2;
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100310 }
Thomas Pedersen176f3602011-10-26 14:47:27 -0700311
312 if (action != WLAN_SP_MESH_PEERING_CLOSE) {
313 if (mesh_add_ht_cap_ie(skb, sdata) ||
Johannes Berg074d46d2012-03-15 19:45:16 +0100314 mesh_add_ht_oper_ie(skb, sdata))
Thomas Pedersen176f3602011-10-26 14:47:27 -0700315 return -1;
316 }
317
Thomas Pedersen8db09852011-08-12 20:01:00 -0700318 if (mesh_add_vendor_ies(skb, sdata))
319 return -1;
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100320
Johannes Berg62ae67b2009-11-18 18:42:05 +0100321 ieee80211_tx_skb(sdata, skb);
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100322 return 0;
323}
324
Thomas Pedersen54ab1ff2012-04-18 19:23:42 -0700325/* mesh_peer_init - initialize new mesh peer and return resulting sta_info
326 *
327 * @sdata: local meshif
328 * @addr: peer's address
Thomas Pedersen54ab1ff2012-04-18 19:23:42 -0700329 * @elems: IEs from beacon or mesh peering frame
330 *
331 * call under RCU
332 */
333static struct sta_info *mesh_peer_init(struct ieee80211_sub_if_data *sdata,
Thomas Pedersenf743ff42012-04-18 19:23:43 -0700334 u8 *addr,
Thomas Pedersen54ab1ff2012-04-18 19:23:42 -0700335 struct ieee802_11_elems *elems)
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100336{
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200337 struct ieee80211_local *local = sdata->local;
Thomas Pedersenf743ff42012-04-18 19:23:43 -0700338 enum ieee80211_band band = local->oper_channel->band;
Thomas Pedersen54ab1ff2012-04-18 19:23:42 -0700339 struct ieee80211_supported_band *sband;
Thomas Pedersenf743ff42012-04-18 19:23:43 -0700340 u32 rates, basic_rates = 0;
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100341 struct sta_info *sta;
Thomas Pedersene87278e2012-04-26 15:01:06 -0700342 bool insert = false;
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100343
Thomas Pedersenf743ff42012-04-18 19:23:43 -0700344 sband = local->hw.wiphy->bands[band];
345 rates = ieee80211_sta_get_rates(local, elems, band, &basic_rates);
Johannes Bergd0709a62008-02-25 16:27:46 +0100346
Thomas Pedersen54ab1ff2012-04-18 19:23:42 -0700347 sta = sta_info_get(sdata, addr);
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100348 if (!sta) {
Thomas Pedersen54ab1ff2012-04-18 19:23:42 -0700349 sta = mesh_plink_alloc(sdata, addr);
Johannes Berg34e89502010-02-03 13:59:58 +0100350 if (!sta)
Thomas Pedersen54ab1ff2012-04-18 19:23:42 -0700351 return NULL;
Thomas Pedersene87278e2012-04-26 15:01:06 -0700352 insert = true;
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100353 }
354
Thomas Pedersen54ab1ff2012-04-18 19:23:42 -0700355 spin_lock_bh(&sta->lock);
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100356 sta->last_rx = jiffies;
Thomas Pedersenf743ff42012-04-18 19:23:43 -0700357 sta->sta.supp_rates[band] = rates;
Thomas Pedersene76781e2012-04-18 19:24:13 -0700358 if (elems->ht_cap_elem &&
359 sdata->local->_oper_channel_type != NL80211_CHAN_NO_HT)
Thomas Pedersen54ab1ff2012-04-18 19:23:42 -0700360 ieee80211_ht_cap_ie_to_sta_ht_cap(sdata, sband,
361 elems->ht_cap_elem,
362 &sta->sta.ht_cap);
363 else
364 memset(&sta->sta.ht_cap, 0, sizeof(sta->sta.ht_cap));
365
Ashok Nagarajan57aac7c2012-04-30 14:20:30 -0700366 if (elems->ht_operation) {
Thomas Pedersenc7d25822012-04-26 15:01:07 -0700367 if (!(elems->ht_operation->ht_param &
368 IEEE80211_HT_PARAM_CHAN_WIDTH_ANY))
369 sta->sta.ht_cap.cap &=
370 ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
Ashok Nagarajan57aac7c2012-04-30 14:20:30 -0700371 sta->ch_type =
372 ieee80211_ht_oper_to_channel_type(elems->ht_operation);
373 }
Thomas Pedersenc7d25822012-04-26 15:01:07 -0700374
Thomas Pedersen54ab1ff2012-04-18 19:23:42 -0700375 rate_control_rate_init(sta);
376 spin_unlock_bh(&sta->lock);
377
Thomas Pedersene87278e2012-04-26 15:01:06 -0700378 if (insert && sta_info_insert(sta))
379 return NULL;
380
Thomas Pedersen54ab1ff2012-04-18 19:23:42 -0700381 return sta;
382}
383
Thomas Pedersenf743ff42012-04-18 19:23:43 -0700384void mesh_neighbour_update(struct ieee80211_sub_if_data *sdata,
385 u8 *hw_addr,
Thomas Pedersen54ab1ff2012-04-18 19:23:42 -0700386 struct ieee802_11_elems *elems)
387{
388 struct sta_info *sta;
389
390 /* Userspace handles peer allocation when security is enabled */
391 if (sdata->u.mesh.security & IEEE80211_MESH_SEC_AUTHED) {
392 cfg80211_notify_new_peer_candidate(sdata->dev, hw_addr,
393 elems->ie_start,
394 elems->total_len,
395 GFP_KERNEL);
396 return;
397 }
398
399 rcu_read_lock();
Thomas Pedersenf743ff42012-04-18 19:23:43 -0700400 sta = mesh_peer_init(sdata, hw_addr, elems);
Thomas Pedersen54ab1ff2012-04-18 19:23:42 -0700401 if (!sta)
402 goto out;
403
Javier Cardona1570ca52011-04-07 15:08:35 -0700404 if (mesh_peer_accepts_plinks(elems) &&
Thomas Pedersen54ab1ff2012-04-18 19:23:42 -0700405 sta->plink_state == NL80211_PLINK_LISTEN &&
406 sdata->u.mesh.accepting_plinks &&
407 sdata->u.mesh.mshcfg.auto_open_plinks &&
408 rssi_threshold_check(sta, sdata))
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100409 mesh_plink_open(sta);
410
Thomas Pedersen54ab1ff2012-04-18 19:23:42 -0700411out:
Johannes Bergd0709a62008-02-25 16:27:46 +0100412 rcu_read_unlock();
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100413}
414
415static void mesh_plink_timer(unsigned long data)
416{
417 struct sta_info *sta;
418 __le16 llid, plid, reason;
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100419 struct ieee80211_sub_if_data *sdata;
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100420
Johannes Bergd0709a62008-02-25 16:27:46 +0100421 /*
422 * This STA is valid because sta_info_destroy() will
423 * del_timer_sync() this timer after having made sure
424 * it cannot be readded (by deleting the plink.)
425 */
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100426 sta = (struct sta_info *) data;
427
Johannes Berg5bb644a2009-05-17 11:40:42 +0200428 if (sta->sdata->local->quiescing) {
429 sta->plink_timer_was_running = true;
430 return;
431 }
432
Johannes Berg07346f812008-05-03 01:02:02 +0200433 spin_lock_bh(&sta->lock);
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100434 if (sta->ignore_plink_timer) {
435 sta->ignore_plink_timer = false;
Johannes Berg07346f812008-05-03 01:02:02 +0200436 spin_unlock_bh(&sta->lock);
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100437 return;
438 }
Johannes Berg0c68ae262008-10-27 15:56:10 -0700439 mpl_dbg("Mesh plink timer for %pM fired on state %d\n",
440 sta->sta.addr, sta->plink_state);
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100441 reason = 0;
442 llid = sta->llid;
443 plid = sta->plid;
Johannes Bergd0709a62008-02-25 16:27:46 +0100444 sdata = sta->sdata;
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100445
446 switch (sta->plink_state) {
Javier Cardona57cf8042011-05-13 10:45:43 -0700447 case NL80211_PLINK_OPN_RCVD:
448 case NL80211_PLINK_OPN_SNT:
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100449 /* retry timer */
450 if (sta->plink_retries < dot11MeshMaxRetries(sdata)) {
451 u32 rand;
Johannes Berg0c68ae262008-10-27 15:56:10 -0700452 mpl_dbg("Mesh plink for %pM (retry, timeout): %d %d\n",
453 sta->sta.addr, sta->plink_retries,
454 sta->plink_timeout);
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100455 get_random_bytes(&rand, sizeof(u32));
456 sta->plink_timeout = sta->plink_timeout +
457 rand % sta->plink_timeout;
458 ++sta->plink_retries;
Johannes Bergd0709a62008-02-25 16:27:46 +0100459 mod_plink_timer(sta, sta->plink_timeout);
Johannes Berg07346f812008-05-03 01:02:02 +0200460 spin_unlock_bh(&sta->lock);
Thomas Pedersen54ef6562011-08-11 19:35:12 -0700461 mesh_plink_frame_tx(sdata, WLAN_SP_MESH_PEERING_OPEN,
462 sta->sta.addr, llid, 0, 0);
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100463 break;
464 }
Thomas Pedersen54ef6562011-08-11 19:35:12 -0700465 reason = cpu_to_le16(WLAN_REASON_MESH_MAX_RETRIES);
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100466 /* fall through on else */
Javier Cardona57cf8042011-05-13 10:45:43 -0700467 case NL80211_PLINK_CNF_RCVD:
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100468 /* confirm timer */
469 if (!reason)
Thomas Pedersen54ef6562011-08-11 19:35:12 -0700470 reason = cpu_to_le16(WLAN_REASON_MESH_CONFIRM_TIMEOUT);
Javier Cardona57cf8042011-05-13 10:45:43 -0700471 sta->plink_state = NL80211_PLINK_HOLDING;
Johannes Bergd0709a62008-02-25 16:27:46 +0100472 mod_plink_timer(sta, dot11MeshHoldingTimeout(sdata));
Johannes Berg07346f812008-05-03 01:02:02 +0200473 spin_unlock_bh(&sta->lock);
Thomas Pedersen54ef6562011-08-11 19:35:12 -0700474 mesh_plink_frame_tx(sdata, WLAN_SP_MESH_PEERING_CLOSE,
475 sta->sta.addr, llid, plid, reason);
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100476 break;
Javier Cardona57cf8042011-05-13 10:45:43 -0700477 case NL80211_PLINK_HOLDING:
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100478 /* holding timer */
Johannes Bergd0709a62008-02-25 16:27:46 +0100479 del_timer(&sta->plink_timer);
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100480 mesh_plink_fsm_restart(sta);
Johannes Berg07346f812008-05-03 01:02:02 +0200481 spin_unlock_bh(&sta->lock);
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100482 break;
483 default:
Johannes Berg07346f812008-05-03 01:02:02 +0200484 spin_unlock_bh(&sta->lock);
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100485 break;
486 }
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100487}
488
Johannes Berg5bb644a2009-05-17 11:40:42 +0200489#ifdef CONFIG_PM
490void mesh_plink_quiesce(struct sta_info *sta)
491{
492 if (del_timer_sync(&sta->plink_timer))
493 sta->plink_timer_was_running = true;
494}
495
496void mesh_plink_restart(struct sta_info *sta)
497{
498 if (sta->plink_timer_was_running) {
499 add_timer(&sta->plink_timer);
500 sta->plink_timer_was_running = false;
501 }
502}
503#endif
504
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100505static inline void mesh_plink_timer_set(struct sta_info *sta, int timeout)
506{
507 sta->plink_timer.expires = jiffies + (HZ * timeout / 1000);
508 sta->plink_timer.data = (unsigned long) sta;
509 sta->plink_timer.function = mesh_plink_timer;
510 sta->plink_timeout = timeout;
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100511 add_timer(&sta->plink_timer);
512}
513
514int mesh_plink_open(struct sta_info *sta)
515{
516 __le16 llid;
Johannes Bergd0709a62008-02-25 16:27:46 +0100517 struct ieee80211_sub_if_data *sdata = sta->sdata;
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100518
Johannes Bergc2c98fd2011-09-29 16:04:36 +0200519 if (!test_sta_flag(sta, WLAN_STA_AUTH))
Javier Cardona53e80512011-04-07 15:08:32 -0700520 return -EPERM;
521
Johannes Berg07346f812008-05-03 01:02:02 +0200522 spin_lock_bh(&sta->lock);
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100523 get_random_bytes(&llid, 2);
524 sta->llid = llid;
Javier Cardona57cf8042011-05-13 10:45:43 -0700525 if (sta->plink_state != NL80211_PLINK_LISTEN) {
Johannes Berg07346f812008-05-03 01:02:02 +0200526 spin_unlock_bh(&sta->lock);
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100527 return -EBUSY;
528 }
Javier Cardona57cf8042011-05-13 10:45:43 -0700529 sta->plink_state = NL80211_PLINK_OPN_SNT;
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100530 mesh_plink_timer_set(sta, dot11MeshRetryTimeout(sdata));
Johannes Berg07346f812008-05-03 01:02:02 +0200531 spin_unlock_bh(&sta->lock);
Johannes Berg0c68ae262008-10-27 15:56:10 -0700532 mpl_dbg("Mesh plink: starting establishment with %pM\n",
533 sta->sta.addr);
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100534
Thomas Pedersen54ef6562011-08-11 19:35:12 -0700535 return mesh_plink_frame_tx(sdata, WLAN_SP_MESH_PEERING_OPEN,
Johannes Berg17741cd2008-09-11 00:02:02 +0200536 sta->sta.addr, llid, 0, 0);
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100537}
538
539void mesh_plink_block(struct sta_info *sta)
540{
John W. Linvillec9370192010-06-21 17:14:07 -0400541 struct ieee80211_sub_if_data *sdata = sta->sdata;
542 bool deactivated;
543
Johannes Berg07346f812008-05-03 01:02:02 +0200544 spin_lock_bh(&sta->lock);
John W. Linvillec9370192010-06-21 17:14:07 -0400545 deactivated = __mesh_plink_deactivate(sta);
Javier Cardona57cf8042011-05-13 10:45:43 -0700546 sta->plink_state = NL80211_PLINK_BLOCKED;
Johannes Berg07346f812008-05-03 01:02:02 +0200547 spin_unlock_bh(&sta->lock);
John W. Linvillec9370192010-06-21 17:14:07 -0400548
549 if (deactivated)
550 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BEACON);
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100551}
552
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100553
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200554void mesh_rx_plink_frame(struct ieee80211_sub_if_data *sdata, struct ieee80211_mgmt *mgmt,
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100555 size_t len, struct ieee80211_rx_status *rx_status)
556{
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100557 struct ieee802_11_elems elems;
558 struct sta_info *sta;
559 enum plink_event event;
Thomas Pedersen54ef6562011-08-11 19:35:12 -0700560 enum ieee80211_self_protected_actioncode ftype;
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100561 size_t baselen;
Ashok Nagarajan57aac7c2012-04-30 14:20:30 -0700562 bool matches_local = true;
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100563 u8 ie_len;
564 u8 *baseaddr;
Ashok Nagarajan57aac7c2012-04-30 14:20:30 -0700565 u32 changed = 0;
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100566 __le16 plid, llid, reason;
Rui Paulo1460dd12009-11-09 23:46:48 +0000567#ifdef CONFIG_MAC80211_VERBOSE_MPL_DEBUG
568 static const char *mplstates[] = {
Javier Cardona57cf8042011-05-13 10:45:43 -0700569 [NL80211_PLINK_LISTEN] = "LISTEN",
570 [NL80211_PLINK_OPN_SNT] = "OPN-SNT",
571 [NL80211_PLINK_OPN_RCVD] = "OPN-RCVD",
572 [NL80211_PLINK_CNF_RCVD] = "CNF_RCVD",
573 [NL80211_PLINK_ESTAB] = "ESTAB",
574 [NL80211_PLINK_HOLDING] = "HOLDING",
575 [NL80211_PLINK_BLOCKED] = "BLOCKED"
Rui Paulo1460dd12009-11-09 23:46:48 +0000576 };
577#endif
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100578
Johannes Berg9c80d3d2008-09-08 15:41:59 +0200579 /* need action_code, aux */
580 if (len < IEEE80211_MIN_ACTION_SIZE + 3)
581 return;
582
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100583 if (is_multicast_ether_addr(mgmt->da)) {
584 mpl_dbg("Mesh plink: ignore frame from multicast address");
585 return;
586 }
587
Thomas Pedersen8db09852011-08-12 20:01:00 -0700588 baseaddr = mgmt->u.action.u.self_prot.variable;
589 baselen = (u8 *) mgmt->u.action.u.self_prot.variable - (u8 *) mgmt;
590 if (mgmt->u.action.u.self_prot.action_code ==
Thomas Pedersen54ef6562011-08-11 19:35:12 -0700591 WLAN_SP_MESH_PEERING_CONFIRM) {
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100592 baseaddr += 4;
David Woo70bdb6b2009-08-12 11:03:44 -0700593 baselen += 4;
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100594 }
595 ieee802_11_parse_elems(baseaddr, len - baselen, &elems);
Thomas Pedersen8db09852011-08-12 20:01:00 -0700596 if (!elems.peering) {
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100597 mpl_dbg("Mesh plink: missing necessary peer link ie\n");
598 return;
599 }
Javier Cardonab130e5c2011-05-03 16:57:07 -0700600 if (elems.rsn_len &&
601 sdata->u.mesh.security == IEEE80211_MESH_SEC_NONE) {
Javier Cardona5cff5e02011-04-07 15:08:29 -0700602 mpl_dbg("Mesh plink: can't establish link with secure peer\n");
603 return;
604 }
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100605
Thomas Pedersen8db09852011-08-12 20:01:00 -0700606 ftype = mgmt->u.action.u.self_prot.action_code;
607 ie_len = elems.peering_len;
608 if ((ftype == WLAN_SP_MESH_PEERING_OPEN && ie_len != 4) ||
609 (ftype == WLAN_SP_MESH_PEERING_CONFIRM && ie_len != 6) ||
610 (ftype == WLAN_SP_MESH_PEERING_CLOSE && ie_len != 6
611 && ie_len != 8)) {
Rui Paulo09383932009-11-09 23:46:43 +0000612 mpl_dbg("Mesh plink: incorrect plink ie length %d %d\n",
613 ftype, ie_len);
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100614 return;
615 }
616
Thomas Pedersen54ef6562011-08-11 19:35:12 -0700617 if (ftype != WLAN_SP_MESH_PEERING_CLOSE &&
618 (!elems.mesh_id || !elems.mesh_config)) {
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100619 mpl_dbg("Mesh plink: missing necessary ie\n");
620 return;
621 }
622 /* Note the lines below are correct, the llid in the frame is the plid
623 * from the point of view of this host.
624 */
Thomas Pedersen8db09852011-08-12 20:01:00 -0700625 memcpy(&plid, PLINK_GET_LLID(elems.peering), 2);
Thomas Pedersen54ef6562011-08-11 19:35:12 -0700626 if (ftype == WLAN_SP_MESH_PEERING_CONFIRM ||
Thomas Pedersen8db09852011-08-12 20:01:00 -0700627 (ftype == WLAN_SP_MESH_PEERING_CLOSE && ie_len == 8))
628 memcpy(&llid, PLINK_GET_PLID(elems.peering), 2);
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100629
Johannes Bergd0709a62008-02-25 16:27:46 +0100630 rcu_read_lock();
631
Johannes Bergabe60632009-11-25 17:46:18 +0100632 sta = sta_info_get(sdata, mgmt->sa);
Thomas Pedersen54ef6562011-08-11 19:35:12 -0700633 if (!sta && ftype != WLAN_SP_MESH_PEERING_OPEN) {
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100634 mpl_dbg("Mesh plink: cls or cnf from unknown peer\n");
Johannes Bergd0709a62008-02-25 16:27:46 +0100635 rcu_read_unlock();
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100636 return;
637 }
638
Ashok Nagarajan55335132012-02-28 17:04:08 -0800639 if (ftype == WLAN_SP_MESH_PEERING_OPEN &&
Ashok Nagarajan3d4f9692012-03-06 12:48:30 -0800640 !rssi_threshold_check(sta, sdata)) {
Ashok Nagarajan55335132012-02-28 17:04:08 -0800641 mpl_dbg("Mesh plink: %pM does not meet rssi threshold\n",
Ashok Nagarajan3d4f9692012-03-06 12:48:30 -0800642 mgmt->sa);
Ashok Nagarajan55335132012-02-28 17:04:08 -0800643 rcu_read_unlock();
644 return;
645 }
646
Johannes Bergc2c98fd2011-09-29 16:04:36 +0200647 if (sta && !test_sta_flag(sta, WLAN_STA_AUTH)) {
Javier Cardona53e80512011-04-07 15:08:32 -0700648 mpl_dbg("Mesh plink: Action frame from non-authed peer\n");
649 rcu_read_unlock();
650 return;
651 }
652
Javier Cardona57cf8042011-05-13 10:45:43 -0700653 if (sta && sta->plink_state == NL80211_PLINK_BLOCKED) {
Johannes Bergd0709a62008-02-25 16:27:46 +0100654 rcu_read_unlock();
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100655 return;
656 }
657
658 /* Now we will figure out the appropriate event... */
659 event = PLINK_UNDEFINED;
Thomas Pedersen54ef6562011-08-11 19:35:12 -0700660 if (ftype != WLAN_SP_MESH_PEERING_CLOSE &&
Thomas Pedersenf743ff42012-04-18 19:23:43 -0700661 !mesh_matches_local(sdata, &elems)) {
Christian Lamparterd12c7452010-10-08 22:27:07 +0200662 matches_local = false;
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100663 switch (ftype) {
Thomas Pedersen54ef6562011-08-11 19:35:12 -0700664 case WLAN_SP_MESH_PEERING_OPEN:
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100665 event = OPN_RJCT;
666 break;
Thomas Pedersen54ef6562011-08-11 19:35:12 -0700667 case WLAN_SP_MESH_PEERING_CONFIRM:
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100668 event = CNF_RJCT;
669 break;
Thomas Pedersen54ef6562011-08-11 19:35:12 -0700670 default:
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100671 break;
672 }
Christian Lamparterd12c7452010-10-08 22:27:07 +0200673 }
674
675 if (!sta && !matches_local) {
676 rcu_read_unlock();
Thomas Pedersen54ef6562011-08-11 19:35:12 -0700677 reason = cpu_to_le16(WLAN_REASON_MESH_CONFIG);
Christian Lamparterd12c7452010-10-08 22:27:07 +0200678 llid = 0;
Thomas Pedersen54ef6562011-08-11 19:35:12 -0700679 mesh_plink_frame_tx(sdata, WLAN_SP_MESH_PEERING_CLOSE,
680 mgmt->sa, llid, plid, reason);
Christian Lamparterd12c7452010-10-08 22:27:07 +0200681 return;
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100682 } else if (!sta) {
Thomas Pedersen54ef6562011-08-11 19:35:12 -0700683 /* ftype == WLAN_SP_MESH_PEERING_OPEN */
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100684 if (!mesh_plink_free_count(sdata)) {
685 mpl_dbg("Mesh plink error: no more free plinks\n");
Johannes Berg73651ee2008-02-25 16:27:47 +0100686 rcu_read_unlock();
687 return;
688 }
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100689 event = OPN_ACPT;
Christian Lamparterd12c7452010-10-08 22:27:07 +0200690 } else if (matches_local) {
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100691 switch (ftype) {
Thomas Pedersen54ef6562011-08-11 19:35:12 -0700692 case WLAN_SP_MESH_PEERING_OPEN:
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100693 if (!mesh_plink_free_count(sdata) ||
Johannes Bergd0709a62008-02-25 16:27:46 +0100694 (sta->plid && sta->plid != plid))
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100695 event = OPN_IGNR;
696 else
697 event = OPN_ACPT;
698 break;
Thomas Pedersen54ef6562011-08-11 19:35:12 -0700699 case WLAN_SP_MESH_PEERING_CONFIRM:
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100700 if (!mesh_plink_free_count(sdata) ||
Johannes Bergd0709a62008-02-25 16:27:46 +0100701 (sta->llid != llid || sta->plid != plid))
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100702 event = CNF_IGNR;
703 else
704 event = CNF_ACPT;
705 break;
Thomas Pedersen54ef6562011-08-11 19:35:12 -0700706 case WLAN_SP_MESH_PEERING_CLOSE:
Javier Cardona57cf8042011-05-13 10:45:43 -0700707 if (sta->plink_state == NL80211_PLINK_ESTAB)
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100708 /* Do not check for llid or plid. This does not
709 * follow the standard but since multiple plinks
710 * per sta are not supported, it is necessary in
711 * order to avoid a livelock when MP A sees an
712 * establish peer link to MP B but MP B does not
713 * see it. This can be caused by a timeout in
714 * B's peer link establishment or B beign
715 * restarted.
716 */
717 event = CLS_ACPT;
718 else if (sta->plid != plid)
719 event = CLS_IGNR;
720 else if (ie_len == 7 && sta->llid != llid)
721 event = CLS_IGNR;
722 else
723 event = CLS_ACPT;
724 break;
725 default:
726 mpl_dbg("Mesh plink: unknown frame subtype\n");
Johannes Bergd0709a62008-02-25 16:27:46 +0100727 rcu_read_unlock();
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100728 return;
729 }
Thomas Pedersen54ab1ff2012-04-18 19:23:42 -0700730 }
731
732 if (event == OPN_ACPT) {
733 /* allocate sta entry if necessary and update info */
Thomas Pedersenf743ff42012-04-18 19:23:43 -0700734 sta = mesh_peer_init(sdata, mgmt->sa, &elems);
Thomas Pedersen54ab1ff2012-04-18 19:23:42 -0700735 if (!sta) {
736 mpl_dbg("Mesh plink: failed to init peer!\n");
737 rcu_read_unlock();
738 return;
739 }
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100740 }
741
Rui Paulo1460dd12009-11-09 23:46:48 +0000742 mpl_dbg("Mesh plink (peer, state, llid, plid, event): %pM %s %d %d %d\n",
743 mgmt->sa, mplstates[sta->plink_state],
Johannes Berg0c68ae262008-10-27 15:56:10 -0700744 le16_to_cpu(sta->llid), le16_to_cpu(sta->plid),
745 event);
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100746 reason = 0;
Thomas Pedersen54ab1ff2012-04-18 19:23:42 -0700747 spin_lock_bh(&sta->lock);
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100748 switch (sta->plink_state) {
749 /* spin_unlock as soon as state is updated at each case */
Javier Cardona57cf8042011-05-13 10:45:43 -0700750 case NL80211_PLINK_LISTEN:
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100751 switch (event) {
752 case CLS_ACPT:
753 mesh_plink_fsm_restart(sta);
Johannes Berg07346f812008-05-03 01:02:02 +0200754 spin_unlock_bh(&sta->lock);
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100755 break;
756 case OPN_ACPT:
Javier Cardona57cf8042011-05-13 10:45:43 -0700757 sta->plink_state = NL80211_PLINK_OPN_RCVD;
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100758 sta->plid = plid;
759 get_random_bytes(&llid, 2);
760 sta->llid = llid;
761 mesh_plink_timer_set(sta, dot11MeshRetryTimeout(sdata));
Johannes Berg07346f812008-05-03 01:02:02 +0200762 spin_unlock_bh(&sta->lock);
Thomas Pedersen54ef6562011-08-11 19:35:12 -0700763 mesh_plink_frame_tx(sdata,
764 WLAN_SP_MESH_PEERING_OPEN,
765 sta->sta.addr, llid, 0, 0);
766 mesh_plink_frame_tx(sdata,
767 WLAN_SP_MESH_PEERING_CONFIRM,
768 sta->sta.addr, llid, plid, 0);
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100769 break;
770 default:
Johannes Berg07346f812008-05-03 01:02:02 +0200771 spin_unlock_bh(&sta->lock);
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100772 break;
773 }
774 break;
775
Javier Cardona57cf8042011-05-13 10:45:43 -0700776 case NL80211_PLINK_OPN_SNT:
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100777 switch (event) {
778 case OPN_RJCT:
779 case CNF_RJCT:
Thomas Pedersen54ef6562011-08-11 19:35:12 -0700780 reason = cpu_to_le16(WLAN_REASON_MESH_CONFIG);
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100781 case CLS_ACPT:
782 if (!reason)
Thomas Pedersen54ef6562011-08-11 19:35:12 -0700783 reason = cpu_to_le16(WLAN_REASON_MESH_CLOSE);
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100784 sta->reason = reason;
Javier Cardona57cf8042011-05-13 10:45:43 -0700785 sta->plink_state = NL80211_PLINK_HOLDING;
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100786 if (!mod_plink_timer(sta,
787 dot11MeshHoldingTimeout(sdata)))
788 sta->ignore_plink_timer = true;
789
790 llid = sta->llid;
Johannes Berg07346f812008-05-03 01:02:02 +0200791 spin_unlock_bh(&sta->lock);
Thomas Pedersen54ef6562011-08-11 19:35:12 -0700792 mesh_plink_frame_tx(sdata,
793 WLAN_SP_MESH_PEERING_CLOSE,
794 sta->sta.addr, llid, plid, reason);
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100795 break;
796 case OPN_ACPT:
797 /* retry timer is left untouched */
Javier Cardona57cf8042011-05-13 10:45:43 -0700798 sta->plink_state = NL80211_PLINK_OPN_RCVD;
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100799 sta->plid = plid;
800 llid = sta->llid;
Johannes Berg07346f812008-05-03 01:02:02 +0200801 spin_unlock_bh(&sta->lock);
Thomas Pedersen54ef6562011-08-11 19:35:12 -0700802 mesh_plink_frame_tx(sdata,
803 WLAN_SP_MESH_PEERING_CONFIRM,
804 sta->sta.addr, llid, plid, 0);
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100805 break;
806 case CNF_ACPT:
Javier Cardona57cf8042011-05-13 10:45:43 -0700807 sta->plink_state = NL80211_PLINK_CNF_RCVD;
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100808 if (!mod_plink_timer(sta,
809 dot11MeshConfirmTimeout(sdata)))
810 sta->ignore_plink_timer = true;
811
Johannes Berg07346f812008-05-03 01:02:02 +0200812 spin_unlock_bh(&sta->lock);
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100813 break;
814 default:
Johannes Berg07346f812008-05-03 01:02:02 +0200815 spin_unlock_bh(&sta->lock);
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100816 break;
817 }
818 break;
819
Javier Cardona57cf8042011-05-13 10:45:43 -0700820 case NL80211_PLINK_OPN_RCVD:
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100821 switch (event) {
822 case OPN_RJCT:
823 case CNF_RJCT:
Thomas Pedersen54ef6562011-08-11 19:35:12 -0700824 reason = cpu_to_le16(WLAN_REASON_MESH_CONFIG);
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100825 case CLS_ACPT:
826 if (!reason)
Thomas Pedersen54ef6562011-08-11 19:35:12 -0700827 reason = cpu_to_le16(WLAN_REASON_MESH_CLOSE);
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100828 sta->reason = reason;
Javier Cardona57cf8042011-05-13 10:45:43 -0700829 sta->plink_state = NL80211_PLINK_HOLDING;
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100830 if (!mod_plink_timer(sta,
831 dot11MeshHoldingTimeout(sdata)))
832 sta->ignore_plink_timer = true;
833
834 llid = sta->llid;
Johannes Berg07346f812008-05-03 01:02:02 +0200835 spin_unlock_bh(&sta->lock);
Thomas Pedersen54ef6562011-08-11 19:35:12 -0700836 mesh_plink_frame_tx(sdata, WLAN_SP_MESH_PEERING_CLOSE,
837 sta->sta.addr, llid, plid, reason);
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100838 break;
839 case OPN_ACPT:
840 llid = sta->llid;
Johannes Berg07346f812008-05-03 01:02:02 +0200841 spin_unlock_bh(&sta->lock);
Thomas Pedersen54ef6562011-08-11 19:35:12 -0700842 mesh_plink_frame_tx(sdata,
843 WLAN_SP_MESH_PEERING_CONFIRM,
844 sta->sta.addr, llid, plid, 0);
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100845 break;
846 case CNF_ACPT:
Johannes Bergd0709a62008-02-25 16:27:46 +0100847 del_timer(&sta->plink_timer);
Javier Cardona57cf8042011-05-13 10:45:43 -0700848 sta->plink_state = NL80211_PLINK_ESTAB;
Johannes Berg07346f812008-05-03 01:02:02 +0200849 spin_unlock_bh(&sta->lock);
John W. Linvillec9370192010-06-21 17:14:07 -0400850 mesh_plink_inc_estab_count(sdata);
Ashok Nagarajan57aac7c2012-04-30 14:20:30 -0700851 changed |= mesh_set_ht_prot_mode(sdata);
852 changed |= BSS_CHANGED_BEACON;
Johannes Berg0c68ae262008-10-27 15:56:10 -0700853 mpl_dbg("Mesh plink with %pM ESTABLISHED\n",
854 sta->sta.addr);
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100855 break;
856 default:
Johannes Berg07346f812008-05-03 01:02:02 +0200857 spin_unlock_bh(&sta->lock);
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100858 break;
859 }
860 break;
861
Javier Cardona57cf8042011-05-13 10:45:43 -0700862 case NL80211_PLINK_CNF_RCVD:
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100863 switch (event) {
864 case OPN_RJCT:
865 case CNF_RJCT:
Thomas Pedersen54ef6562011-08-11 19:35:12 -0700866 reason = cpu_to_le16(WLAN_REASON_MESH_CONFIG);
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100867 case CLS_ACPT:
868 if (!reason)
Thomas Pedersen54ef6562011-08-11 19:35:12 -0700869 reason = cpu_to_le16(WLAN_REASON_MESH_CLOSE);
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100870 sta->reason = reason;
Javier Cardona57cf8042011-05-13 10:45:43 -0700871 sta->plink_state = NL80211_PLINK_HOLDING;
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100872 if (!mod_plink_timer(sta,
873 dot11MeshHoldingTimeout(sdata)))
874 sta->ignore_plink_timer = true;
875
876 llid = sta->llid;
Johannes Berg07346f812008-05-03 01:02:02 +0200877 spin_unlock_bh(&sta->lock);
Thomas Pedersen54ef6562011-08-11 19:35:12 -0700878 mesh_plink_frame_tx(sdata,
879 WLAN_SP_MESH_PEERING_CLOSE,
880 sta->sta.addr, llid, plid, reason);
Johannes Bergff59dc72008-02-25 10:11:50 +0100881 break;
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100882 case OPN_ACPT:
Johannes Bergd0709a62008-02-25 16:27:46 +0100883 del_timer(&sta->plink_timer);
Javier Cardona57cf8042011-05-13 10:45:43 -0700884 sta->plink_state = NL80211_PLINK_ESTAB;
Johannes Berg07346f812008-05-03 01:02:02 +0200885 spin_unlock_bh(&sta->lock);
John W. Linvillec9370192010-06-21 17:14:07 -0400886 mesh_plink_inc_estab_count(sdata);
Ashok Nagarajan57aac7c2012-04-30 14:20:30 -0700887 changed |= mesh_set_ht_prot_mode(sdata);
888 changed |= BSS_CHANGED_BEACON;
Johannes Berg0c68ae262008-10-27 15:56:10 -0700889 mpl_dbg("Mesh plink with %pM ESTABLISHED\n",
890 sta->sta.addr);
Thomas Pedersen54ef6562011-08-11 19:35:12 -0700891 mesh_plink_frame_tx(sdata,
892 WLAN_SP_MESH_PEERING_CONFIRM,
893 sta->sta.addr, llid, plid, 0);
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100894 break;
895 default:
Johannes Berg07346f812008-05-03 01:02:02 +0200896 spin_unlock_bh(&sta->lock);
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100897 break;
898 }
899 break;
900
Javier Cardona57cf8042011-05-13 10:45:43 -0700901 case NL80211_PLINK_ESTAB:
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100902 switch (event) {
903 case CLS_ACPT:
Thomas Pedersen54ef6562011-08-11 19:35:12 -0700904 reason = cpu_to_le16(WLAN_REASON_MESH_CLOSE);
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100905 sta->reason = reason;
Ashok Nagarajan57aac7c2012-04-30 14:20:30 -0700906 __mesh_plink_deactivate(sta);
Javier Cardona57cf8042011-05-13 10:45:43 -0700907 sta->plink_state = NL80211_PLINK_HOLDING;
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100908 llid = sta->llid;
Johannes Bergd0709a62008-02-25 16:27:46 +0100909 mod_plink_timer(sta, dot11MeshHoldingTimeout(sdata));
Johannes Berg07346f812008-05-03 01:02:02 +0200910 spin_unlock_bh(&sta->lock);
Ashok Nagarajan57aac7c2012-04-30 14:20:30 -0700911 changed |= mesh_set_ht_prot_mode(sdata);
912 changed |= BSS_CHANGED_BEACON;
Thomas Pedersen54ef6562011-08-11 19:35:12 -0700913 mesh_plink_frame_tx(sdata, WLAN_SP_MESH_PEERING_CLOSE,
914 sta->sta.addr, llid, plid, reason);
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100915 break;
916 case OPN_ACPT:
917 llid = sta->llid;
Johannes Berg07346f812008-05-03 01:02:02 +0200918 spin_unlock_bh(&sta->lock);
Thomas Pedersen54ef6562011-08-11 19:35:12 -0700919 mesh_plink_frame_tx(sdata,
920 WLAN_SP_MESH_PEERING_CONFIRM,
921 sta->sta.addr, llid, plid, 0);
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100922 break;
923 default:
Johannes Berg07346f812008-05-03 01:02:02 +0200924 spin_unlock_bh(&sta->lock);
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100925 break;
926 }
927 break;
Javier Cardona57cf8042011-05-13 10:45:43 -0700928 case NL80211_PLINK_HOLDING:
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100929 switch (event) {
930 case CLS_ACPT:
Johannes Bergd0709a62008-02-25 16:27:46 +0100931 if (del_timer(&sta->plink_timer))
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100932 sta->ignore_plink_timer = 1;
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100933 mesh_plink_fsm_restart(sta);
Johannes Berg07346f812008-05-03 01:02:02 +0200934 spin_unlock_bh(&sta->lock);
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100935 break;
936 case OPN_ACPT:
937 case CNF_ACPT:
938 case OPN_RJCT:
939 case CNF_RJCT:
940 llid = sta->llid;
941 reason = sta->reason;
Johannes Berg07346f812008-05-03 01:02:02 +0200942 spin_unlock_bh(&sta->lock);
Thomas Pedersen54ef6562011-08-11 19:35:12 -0700943 mesh_plink_frame_tx(sdata, WLAN_SP_MESH_PEERING_CLOSE,
944 sta->sta.addr, llid, plid, reason);
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100945 break;
946 default:
Johannes Berg07346f812008-05-03 01:02:02 +0200947 spin_unlock_bh(&sta->lock);
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100948 }
949 break;
950 default:
Luis Carlos Cobob4e08ea2008-02-29 15:46:08 -0800951 /* should not get here, PLINK_BLOCKED is dealt with at the
Daniel Mack3ad2f3f2010-02-03 08:01:28 +0800952 * beginning of the function
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100953 */
Johannes Berg07346f812008-05-03 01:02:02 +0200954 spin_unlock_bh(&sta->lock);
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100955 break;
956 }
Johannes Bergd0709a62008-02-25 16:27:46 +0100957
958 rcu_read_unlock();
Ashok Nagarajan57aac7c2012-04-30 14:20:30 -0700959
960 if (changed)
961 ieee80211_bss_info_change_notify(sdata, changed);
Luis Carlos Coboc3896d22008-02-23 15:17:13 +0100962}