blob: 1ff2a5c63e43444f80ed6b19ebfc3806bacf6722 [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
Thomas Pedersen54ab1ff2012-04-18 19:23:42 -0700271 * @elems: IEs from beacon or mesh peering frame
272 *
273 * call under RCU
274 */
275static struct sta_info *mesh_peer_init(struct ieee80211_sub_if_data *sdata,
Thomas Pedersenf743ff42012-04-18 19:23:43 -0700276 u8 *addr,
Thomas Pedersen54ab1ff2012-04-18 19:23:42 -0700277 struct ieee802_11_elems *elems)
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100278{
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200279 struct ieee80211_local *local = sdata->local;
Thomas Pedersenf743ff42012-04-18 19:23:43 -0700280 enum ieee80211_band band = local->oper_channel->band;
Thomas Pedersen54ab1ff2012-04-18 19:23:42 -0700281 struct ieee80211_supported_band *sband;
Thomas Pedersenf743ff42012-04-18 19:23:43 -0700282 u32 rates, basic_rates = 0;
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100283 struct sta_info *sta;
284
Thomas Pedersenf743ff42012-04-18 19:23:43 -0700285 sband = local->hw.wiphy->bands[band];
286 rates = ieee80211_sta_get_rates(local, elems, band, &basic_rates);
Johannes Bergd0709a62008-02-25 16:27:46 +0100287
Thomas Pedersen54ab1ff2012-04-18 19:23:42 -0700288 sta = sta_info_get(sdata, addr);
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100289 if (!sta) {
Thomas Pedersen54ab1ff2012-04-18 19:23:42 -0700290 sta = mesh_plink_alloc(sdata, addr);
Johannes Berg34e89502010-02-03 13:59:58 +0100291 if (!sta)
Thomas Pedersen54ab1ff2012-04-18 19:23:42 -0700292 return NULL;
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100293 }
294
Thomas Pedersen54ab1ff2012-04-18 19:23:42 -0700295 spin_lock_bh(&sta->lock);
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100296 sta->last_rx = jiffies;
Thomas Pedersenf743ff42012-04-18 19:23:43 -0700297 sta->sta.supp_rates[band] = rates;
Thomas Pedersene76781e2012-04-18 19:24:13 -0700298 if (elems->ht_cap_elem &&
299 sdata->local->_oper_channel_type != NL80211_CHAN_NO_HT)
Thomas Pedersen54ab1ff2012-04-18 19:23:42 -0700300 ieee80211_ht_cap_ie_to_sta_ht_cap(sdata, sband,
301 elems->ht_cap_elem,
302 &sta->sta.ht_cap);
303 else
304 memset(&sta->sta.ht_cap, 0, sizeof(sta->sta.ht_cap));
305
306 rate_control_rate_init(sta);
307 spin_unlock_bh(&sta->lock);
308
309 return sta;
310}
311
Thomas Pedersenf743ff42012-04-18 19:23:43 -0700312void mesh_neighbour_update(struct ieee80211_sub_if_data *sdata,
313 u8 *hw_addr,
Thomas Pedersen54ab1ff2012-04-18 19:23:42 -0700314 struct ieee802_11_elems *elems)
315{
316 struct sta_info *sta;
317
318 /* Userspace handles peer allocation when security is enabled */
319 if (sdata->u.mesh.security & IEEE80211_MESH_SEC_AUTHED) {
320 cfg80211_notify_new_peer_candidate(sdata->dev, hw_addr,
321 elems->ie_start,
322 elems->total_len,
323 GFP_KERNEL);
324 return;
325 }
326
327 rcu_read_lock();
Thomas Pedersenf743ff42012-04-18 19:23:43 -0700328 sta = mesh_peer_init(sdata, hw_addr, elems);
Thomas Pedersen54ab1ff2012-04-18 19:23:42 -0700329 if (!sta)
330 goto out;
331
Javier Cardona1570ca52011-04-07 15:08:35 -0700332 if (mesh_peer_accepts_plinks(elems) &&
Thomas Pedersen54ab1ff2012-04-18 19:23:42 -0700333 sta->plink_state == NL80211_PLINK_LISTEN &&
334 sdata->u.mesh.accepting_plinks &&
335 sdata->u.mesh.mshcfg.auto_open_plinks &&
336 rssi_threshold_check(sta, sdata))
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100337 mesh_plink_open(sta);
338
Thomas Pedersen54ab1ff2012-04-18 19:23:42 -0700339out:
Johannes Bergd0709a62008-02-25 16:27:46 +0100340 rcu_read_unlock();
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100341}
342
343static void mesh_plink_timer(unsigned long data)
344{
345 struct sta_info *sta;
346 __le16 llid, plid, reason;
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100347 struct ieee80211_sub_if_data *sdata;
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100348
Johannes Bergd0709a62008-02-25 16:27:46 +0100349 /*
350 * This STA is valid because sta_info_destroy() will
351 * del_timer_sync() this timer after having made sure
352 * it cannot be readded (by deleting the plink.)
353 */
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100354 sta = (struct sta_info *) data;
355
Johannes Berg5bb644a2009-05-17 11:40:42 +0200356 if (sta->sdata->local->quiescing) {
357 sta->plink_timer_was_running = true;
358 return;
359 }
360
Johannes Berg07346f812008-05-03 01:02:02 +0200361 spin_lock_bh(&sta->lock);
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100362 if (sta->ignore_plink_timer) {
363 sta->ignore_plink_timer = false;
Johannes Berg07346f812008-05-03 01:02:02 +0200364 spin_unlock_bh(&sta->lock);
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100365 return;
366 }
Johannes Berg0c68ae262008-10-27 15:56:10 -0700367 mpl_dbg("Mesh plink timer for %pM fired on state %d\n",
368 sta->sta.addr, sta->plink_state);
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100369 reason = 0;
370 llid = sta->llid;
371 plid = sta->plid;
Johannes Bergd0709a62008-02-25 16:27:46 +0100372 sdata = sta->sdata;
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100373
374 switch (sta->plink_state) {
Javier Cardona57cf8042011-05-13 10:45:43 -0700375 case NL80211_PLINK_OPN_RCVD:
376 case NL80211_PLINK_OPN_SNT:
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100377 /* retry timer */
378 if (sta->plink_retries < dot11MeshMaxRetries(sdata)) {
379 u32 rand;
Johannes Berg0c68ae262008-10-27 15:56:10 -0700380 mpl_dbg("Mesh plink for %pM (retry, timeout): %d %d\n",
381 sta->sta.addr, sta->plink_retries,
382 sta->plink_timeout);
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100383 get_random_bytes(&rand, sizeof(u32));
384 sta->plink_timeout = sta->plink_timeout +
385 rand % sta->plink_timeout;
386 ++sta->plink_retries;
Johannes Bergd0709a62008-02-25 16:27:46 +0100387 mod_plink_timer(sta, sta->plink_timeout);
Johannes Berg07346f812008-05-03 01:02:02 +0200388 spin_unlock_bh(&sta->lock);
Thomas Pedersen54ef6562011-08-11 19:35:12 -0700389 mesh_plink_frame_tx(sdata, WLAN_SP_MESH_PEERING_OPEN,
390 sta->sta.addr, llid, 0, 0);
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100391 break;
392 }
Thomas Pedersen54ef6562011-08-11 19:35:12 -0700393 reason = cpu_to_le16(WLAN_REASON_MESH_MAX_RETRIES);
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100394 /* fall through on else */
Javier Cardona57cf8042011-05-13 10:45:43 -0700395 case NL80211_PLINK_CNF_RCVD:
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100396 /* confirm timer */
397 if (!reason)
Thomas Pedersen54ef6562011-08-11 19:35:12 -0700398 reason = cpu_to_le16(WLAN_REASON_MESH_CONFIRM_TIMEOUT);
Javier Cardona57cf8042011-05-13 10:45:43 -0700399 sta->plink_state = NL80211_PLINK_HOLDING;
Johannes Bergd0709a62008-02-25 16:27:46 +0100400 mod_plink_timer(sta, dot11MeshHoldingTimeout(sdata));
Johannes Berg07346f812008-05-03 01:02:02 +0200401 spin_unlock_bh(&sta->lock);
Thomas Pedersen54ef6562011-08-11 19:35:12 -0700402 mesh_plink_frame_tx(sdata, WLAN_SP_MESH_PEERING_CLOSE,
403 sta->sta.addr, llid, plid, reason);
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100404 break;
Javier Cardona57cf8042011-05-13 10:45:43 -0700405 case NL80211_PLINK_HOLDING:
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100406 /* holding timer */
Johannes Bergd0709a62008-02-25 16:27:46 +0100407 del_timer(&sta->plink_timer);
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100408 mesh_plink_fsm_restart(sta);
Johannes Berg07346f812008-05-03 01:02:02 +0200409 spin_unlock_bh(&sta->lock);
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100410 break;
411 default:
Johannes Berg07346f812008-05-03 01:02:02 +0200412 spin_unlock_bh(&sta->lock);
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100413 break;
414 }
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100415}
416
Johannes Berg5bb644a2009-05-17 11:40:42 +0200417#ifdef CONFIG_PM
418void mesh_plink_quiesce(struct sta_info *sta)
419{
420 if (del_timer_sync(&sta->plink_timer))
421 sta->plink_timer_was_running = true;
422}
423
424void mesh_plink_restart(struct sta_info *sta)
425{
426 if (sta->plink_timer_was_running) {
427 add_timer(&sta->plink_timer);
428 sta->plink_timer_was_running = false;
429 }
430}
431#endif
432
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100433static inline void mesh_plink_timer_set(struct sta_info *sta, int timeout)
434{
435 sta->plink_timer.expires = jiffies + (HZ * timeout / 1000);
436 sta->plink_timer.data = (unsigned long) sta;
437 sta->plink_timer.function = mesh_plink_timer;
438 sta->plink_timeout = timeout;
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100439 add_timer(&sta->plink_timer);
440}
441
442int mesh_plink_open(struct sta_info *sta)
443{
444 __le16 llid;
Johannes Bergd0709a62008-02-25 16:27:46 +0100445 struct ieee80211_sub_if_data *sdata = sta->sdata;
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100446
Johannes Bergc2c98fd2011-09-29 16:04:36 +0200447 if (!test_sta_flag(sta, WLAN_STA_AUTH))
Javier Cardona53e80512011-04-07 15:08:32 -0700448 return -EPERM;
449
Johannes Berg07346f812008-05-03 01:02:02 +0200450 spin_lock_bh(&sta->lock);
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100451 get_random_bytes(&llid, 2);
452 sta->llid = llid;
Javier Cardona57cf8042011-05-13 10:45:43 -0700453 if (sta->plink_state != NL80211_PLINK_LISTEN) {
Johannes Berg07346f812008-05-03 01:02:02 +0200454 spin_unlock_bh(&sta->lock);
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100455 return -EBUSY;
456 }
Javier Cardona57cf8042011-05-13 10:45:43 -0700457 sta->plink_state = NL80211_PLINK_OPN_SNT;
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100458 mesh_plink_timer_set(sta, dot11MeshRetryTimeout(sdata));
Johannes Berg07346f812008-05-03 01:02:02 +0200459 spin_unlock_bh(&sta->lock);
Johannes Berg0c68ae262008-10-27 15:56:10 -0700460 mpl_dbg("Mesh plink: starting establishment with %pM\n",
461 sta->sta.addr);
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100462
Thomas Pedersen54ef6562011-08-11 19:35:12 -0700463 return mesh_plink_frame_tx(sdata, WLAN_SP_MESH_PEERING_OPEN,
Johannes Berg17741cd2008-09-11 00:02:02 +0200464 sta->sta.addr, llid, 0, 0);
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100465}
466
467void mesh_plink_block(struct sta_info *sta)
468{
John W. Linvillec9370192010-06-21 17:14:07 -0400469 struct ieee80211_sub_if_data *sdata = sta->sdata;
470 bool deactivated;
471
Johannes Berg07346f812008-05-03 01:02:02 +0200472 spin_lock_bh(&sta->lock);
John W. Linvillec9370192010-06-21 17:14:07 -0400473 deactivated = __mesh_plink_deactivate(sta);
Javier Cardona57cf8042011-05-13 10:45:43 -0700474 sta->plink_state = NL80211_PLINK_BLOCKED;
Johannes Berg07346f812008-05-03 01:02:02 +0200475 spin_unlock_bh(&sta->lock);
John W. Linvillec9370192010-06-21 17:14:07 -0400476
477 if (deactivated)
478 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BEACON);
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100479}
480
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100481
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200482void mesh_rx_plink_frame(struct ieee80211_sub_if_data *sdata, struct ieee80211_mgmt *mgmt,
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100483 size_t len, struct ieee80211_rx_status *rx_status)
484{
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100485 struct ieee802_11_elems elems;
486 struct sta_info *sta;
487 enum plink_event event;
Thomas Pedersen54ef6562011-08-11 19:35:12 -0700488 enum ieee80211_self_protected_actioncode ftype;
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100489 size_t baselen;
Christian Lamparterd12c7452010-10-08 22:27:07 +0200490 bool deactivated, matches_local = true;
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100491 u8 ie_len;
492 u8 *baseaddr;
493 __le16 plid, llid, reason;
Rui Paulo1460dd12009-11-09 23:46:48 +0000494#ifdef CONFIG_MAC80211_VERBOSE_MPL_DEBUG
495 static const char *mplstates[] = {
Javier Cardona57cf8042011-05-13 10:45:43 -0700496 [NL80211_PLINK_LISTEN] = "LISTEN",
497 [NL80211_PLINK_OPN_SNT] = "OPN-SNT",
498 [NL80211_PLINK_OPN_RCVD] = "OPN-RCVD",
499 [NL80211_PLINK_CNF_RCVD] = "CNF_RCVD",
500 [NL80211_PLINK_ESTAB] = "ESTAB",
501 [NL80211_PLINK_HOLDING] = "HOLDING",
502 [NL80211_PLINK_BLOCKED] = "BLOCKED"
Rui Paulo1460dd12009-11-09 23:46:48 +0000503 };
504#endif
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100505
Johannes Berg9c80d3d2008-09-08 15:41:59 +0200506 /* need action_code, aux */
507 if (len < IEEE80211_MIN_ACTION_SIZE + 3)
508 return;
509
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100510 if (is_multicast_ether_addr(mgmt->da)) {
511 mpl_dbg("Mesh plink: ignore frame from multicast address");
512 return;
513 }
514
Thomas Pedersen8db09852011-08-12 20:01:00 -0700515 baseaddr = mgmt->u.action.u.self_prot.variable;
516 baselen = (u8 *) mgmt->u.action.u.self_prot.variable - (u8 *) mgmt;
517 if (mgmt->u.action.u.self_prot.action_code ==
Thomas Pedersen54ef6562011-08-11 19:35:12 -0700518 WLAN_SP_MESH_PEERING_CONFIRM) {
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100519 baseaddr += 4;
David Woo70bdb6b2009-08-12 11:03:44 -0700520 baselen += 4;
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100521 }
522 ieee802_11_parse_elems(baseaddr, len - baselen, &elems);
Thomas Pedersen8db09852011-08-12 20:01:00 -0700523 if (!elems.peering) {
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100524 mpl_dbg("Mesh plink: missing necessary peer link ie\n");
525 return;
526 }
Javier Cardonab130e5c2011-05-03 16:57:07 -0700527 if (elems.rsn_len &&
528 sdata->u.mesh.security == IEEE80211_MESH_SEC_NONE) {
Javier Cardona5cff5e02011-04-07 15:08:29 -0700529 mpl_dbg("Mesh plink: can't establish link with secure peer\n");
530 return;
531 }
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100532
Thomas Pedersen8db09852011-08-12 20:01:00 -0700533 ftype = mgmt->u.action.u.self_prot.action_code;
534 ie_len = elems.peering_len;
535 if ((ftype == WLAN_SP_MESH_PEERING_OPEN && ie_len != 4) ||
536 (ftype == WLAN_SP_MESH_PEERING_CONFIRM && ie_len != 6) ||
537 (ftype == WLAN_SP_MESH_PEERING_CLOSE && ie_len != 6
538 && ie_len != 8)) {
Rui Paulo09383932009-11-09 23:46:43 +0000539 mpl_dbg("Mesh plink: incorrect plink ie length %d %d\n",
540 ftype, ie_len);
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100541 return;
542 }
543
Thomas Pedersen54ef6562011-08-11 19:35:12 -0700544 if (ftype != WLAN_SP_MESH_PEERING_CLOSE &&
545 (!elems.mesh_id || !elems.mesh_config)) {
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100546 mpl_dbg("Mesh plink: missing necessary ie\n");
547 return;
548 }
549 /* Note the lines below are correct, the llid in the frame is the plid
550 * from the point of view of this host.
551 */
Thomas Pedersen8db09852011-08-12 20:01:00 -0700552 memcpy(&plid, PLINK_GET_LLID(elems.peering), 2);
Thomas Pedersen54ef6562011-08-11 19:35:12 -0700553 if (ftype == WLAN_SP_MESH_PEERING_CONFIRM ||
Thomas Pedersen8db09852011-08-12 20:01:00 -0700554 (ftype == WLAN_SP_MESH_PEERING_CLOSE && ie_len == 8))
555 memcpy(&llid, PLINK_GET_PLID(elems.peering), 2);
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100556
Johannes Bergd0709a62008-02-25 16:27:46 +0100557 rcu_read_lock();
558
Johannes Bergabe60632009-11-25 17:46:18 +0100559 sta = sta_info_get(sdata, mgmt->sa);
Thomas Pedersen54ef6562011-08-11 19:35:12 -0700560 if (!sta && ftype != WLAN_SP_MESH_PEERING_OPEN) {
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100561 mpl_dbg("Mesh plink: cls or cnf from unknown peer\n");
Johannes Bergd0709a62008-02-25 16:27:46 +0100562 rcu_read_unlock();
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100563 return;
564 }
565
Ashok Nagarajan55335132012-02-28 17:04:08 -0800566 if (ftype == WLAN_SP_MESH_PEERING_OPEN &&
Ashok Nagarajan3d4f9692012-03-06 12:48:30 -0800567 !rssi_threshold_check(sta, sdata)) {
Ashok Nagarajan55335132012-02-28 17:04:08 -0800568 mpl_dbg("Mesh plink: %pM does not meet rssi threshold\n",
Ashok Nagarajan3d4f9692012-03-06 12:48:30 -0800569 mgmt->sa);
Ashok Nagarajan55335132012-02-28 17:04:08 -0800570 rcu_read_unlock();
571 return;
572 }
573
Johannes Bergc2c98fd2011-09-29 16:04:36 +0200574 if (sta && !test_sta_flag(sta, WLAN_STA_AUTH)) {
Javier Cardona53e80512011-04-07 15:08:32 -0700575 mpl_dbg("Mesh plink: Action frame from non-authed peer\n");
576 rcu_read_unlock();
577 return;
578 }
579
Javier Cardona57cf8042011-05-13 10:45:43 -0700580 if (sta && sta->plink_state == NL80211_PLINK_BLOCKED) {
Johannes Bergd0709a62008-02-25 16:27:46 +0100581 rcu_read_unlock();
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100582 return;
583 }
584
585 /* Now we will figure out the appropriate event... */
586 event = PLINK_UNDEFINED;
Thomas Pedersen54ef6562011-08-11 19:35:12 -0700587 if (ftype != WLAN_SP_MESH_PEERING_CLOSE &&
Thomas Pedersenf743ff42012-04-18 19:23:43 -0700588 !mesh_matches_local(sdata, &elems)) {
Christian Lamparterd12c7452010-10-08 22:27:07 +0200589 matches_local = false;
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100590 switch (ftype) {
Thomas Pedersen54ef6562011-08-11 19:35:12 -0700591 case WLAN_SP_MESH_PEERING_OPEN:
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100592 event = OPN_RJCT;
593 break;
Thomas Pedersen54ef6562011-08-11 19:35:12 -0700594 case WLAN_SP_MESH_PEERING_CONFIRM:
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100595 event = CNF_RJCT;
596 break;
Thomas Pedersen54ef6562011-08-11 19:35:12 -0700597 default:
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100598 break;
599 }
Christian Lamparterd12c7452010-10-08 22:27:07 +0200600 }
601
602 if (!sta && !matches_local) {
603 rcu_read_unlock();
Thomas Pedersen54ef6562011-08-11 19:35:12 -0700604 reason = cpu_to_le16(WLAN_REASON_MESH_CONFIG);
Christian Lamparterd12c7452010-10-08 22:27:07 +0200605 llid = 0;
Thomas Pedersen54ef6562011-08-11 19:35:12 -0700606 mesh_plink_frame_tx(sdata, WLAN_SP_MESH_PEERING_CLOSE,
607 mgmt->sa, llid, plid, reason);
Christian Lamparterd12c7452010-10-08 22:27:07 +0200608 return;
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100609 } else if (!sta) {
Thomas Pedersen54ef6562011-08-11 19:35:12 -0700610 /* ftype == WLAN_SP_MESH_PEERING_OPEN */
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100611 if (!mesh_plink_free_count(sdata)) {
612 mpl_dbg("Mesh plink error: no more free plinks\n");
Johannes Berg73651ee2008-02-25 16:27:47 +0100613 rcu_read_unlock();
614 return;
615 }
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100616 event = OPN_ACPT;
Christian Lamparterd12c7452010-10-08 22:27:07 +0200617 } else if (matches_local) {
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100618 switch (ftype) {
Thomas Pedersen54ef6562011-08-11 19:35:12 -0700619 case WLAN_SP_MESH_PEERING_OPEN:
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100620 if (!mesh_plink_free_count(sdata) ||
Johannes Bergd0709a62008-02-25 16:27:46 +0100621 (sta->plid && sta->plid != plid))
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100622 event = OPN_IGNR;
623 else
624 event = OPN_ACPT;
625 break;
Thomas Pedersen54ef6562011-08-11 19:35:12 -0700626 case WLAN_SP_MESH_PEERING_CONFIRM:
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100627 if (!mesh_plink_free_count(sdata) ||
Johannes Bergd0709a62008-02-25 16:27:46 +0100628 (sta->llid != llid || sta->plid != plid))
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100629 event = CNF_IGNR;
630 else
631 event = CNF_ACPT;
632 break;
Thomas Pedersen54ef6562011-08-11 19:35:12 -0700633 case WLAN_SP_MESH_PEERING_CLOSE:
Javier Cardona57cf8042011-05-13 10:45:43 -0700634 if (sta->plink_state == NL80211_PLINK_ESTAB)
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100635 /* Do not check for llid or plid. This does not
636 * follow the standard but since multiple plinks
637 * per sta are not supported, it is necessary in
638 * order to avoid a livelock when MP A sees an
639 * establish peer link to MP B but MP B does not
640 * see it. This can be caused by a timeout in
641 * B's peer link establishment or B beign
642 * restarted.
643 */
644 event = CLS_ACPT;
645 else if (sta->plid != plid)
646 event = CLS_IGNR;
647 else if (ie_len == 7 && sta->llid != llid)
648 event = CLS_IGNR;
649 else
650 event = CLS_ACPT;
651 break;
652 default:
653 mpl_dbg("Mesh plink: unknown frame subtype\n");
Johannes Bergd0709a62008-02-25 16:27:46 +0100654 rcu_read_unlock();
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100655 return;
656 }
Thomas Pedersen54ab1ff2012-04-18 19:23:42 -0700657 }
658
659 if (event == OPN_ACPT) {
660 /* allocate sta entry if necessary and update info */
Thomas Pedersenf743ff42012-04-18 19:23:43 -0700661 sta = mesh_peer_init(sdata, mgmt->sa, &elems);
Thomas Pedersen54ab1ff2012-04-18 19:23:42 -0700662 if (!sta) {
663 mpl_dbg("Mesh plink: failed to init peer!\n");
664 rcu_read_unlock();
665 return;
666 }
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100667 }
668
Rui Paulo1460dd12009-11-09 23:46:48 +0000669 mpl_dbg("Mesh plink (peer, state, llid, plid, event): %pM %s %d %d %d\n",
670 mgmt->sa, mplstates[sta->plink_state],
Johannes Berg0c68ae262008-10-27 15:56:10 -0700671 le16_to_cpu(sta->llid), le16_to_cpu(sta->plid),
672 event);
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100673 reason = 0;
Thomas Pedersen54ab1ff2012-04-18 19:23:42 -0700674 spin_lock_bh(&sta->lock);
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100675 switch (sta->plink_state) {
676 /* spin_unlock as soon as state is updated at each case */
Javier Cardona57cf8042011-05-13 10:45:43 -0700677 case NL80211_PLINK_LISTEN:
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100678 switch (event) {
679 case CLS_ACPT:
680 mesh_plink_fsm_restart(sta);
Johannes Berg07346f812008-05-03 01:02:02 +0200681 spin_unlock_bh(&sta->lock);
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100682 break;
683 case OPN_ACPT:
Javier Cardona57cf8042011-05-13 10:45:43 -0700684 sta->plink_state = NL80211_PLINK_OPN_RCVD;
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100685 sta->plid = plid;
686 get_random_bytes(&llid, 2);
687 sta->llid = llid;
688 mesh_plink_timer_set(sta, dot11MeshRetryTimeout(sdata));
Johannes Berg07346f812008-05-03 01:02:02 +0200689 spin_unlock_bh(&sta->lock);
Thomas Pedersen54ef6562011-08-11 19:35:12 -0700690 mesh_plink_frame_tx(sdata,
691 WLAN_SP_MESH_PEERING_OPEN,
692 sta->sta.addr, llid, 0, 0);
693 mesh_plink_frame_tx(sdata,
694 WLAN_SP_MESH_PEERING_CONFIRM,
695 sta->sta.addr, llid, plid, 0);
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100696 break;
697 default:
Johannes Berg07346f812008-05-03 01:02:02 +0200698 spin_unlock_bh(&sta->lock);
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100699 break;
700 }
701 break;
702
Javier Cardona57cf8042011-05-13 10:45:43 -0700703 case NL80211_PLINK_OPN_SNT:
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100704 switch (event) {
705 case OPN_RJCT:
706 case CNF_RJCT:
Thomas Pedersen54ef6562011-08-11 19:35:12 -0700707 reason = cpu_to_le16(WLAN_REASON_MESH_CONFIG);
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100708 case CLS_ACPT:
709 if (!reason)
Thomas Pedersen54ef6562011-08-11 19:35:12 -0700710 reason = cpu_to_le16(WLAN_REASON_MESH_CLOSE);
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100711 sta->reason = reason;
Javier Cardona57cf8042011-05-13 10:45:43 -0700712 sta->plink_state = NL80211_PLINK_HOLDING;
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100713 if (!mod_plink_timer(sta,
714 dot11MeshHoldingTimeout(sdata)))
715 sta->ignore_plink_timer = true;
716
717 llid = sta->llid;
Johannes Berg07346f812008-05-03 01:02:02 +0200718 spin_unlock_bh(&sta->lock);
Thomas Pedersen54ef6562011-08-11 19:35:12 -0700719 mesh_plink_frame_tx(sdata,
720 WLAN_SP_MESH_PEERING_CLOSE,
721 sta->sta.addr, llid, plid, reason);
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100722 break;
723 case OPN_ACPT:
724 /* retry timer is left untouched */
Javier Cardona57cf8042011-05-13 10:45:43 -0700725 sta->plink_state = NL80211_PLINK_OPN_RCVD;
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100726 sta->plid = plid;
727 llid = sta->llid;
Johannes Berg07346f812008-05-03 01:02:02 +0200728 spin_unlock_bh(&sta->lock);
Thomas Pedersen54ef6562011-08-11 19:35:12 -0700729 mesh_plink_frame_tx(sdata,
730 WLAN_SP_MESH_PEERING_CONFIRM,
731 sta->sta.addr, llid, plid, 0);
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100732 break;
733 case CNF_ACPT:
Javier Cardona57cf8042011-05-13 10:45:43 -0700734 sta->plink_state = NL80211_PLINK_CNF_RCVD;
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100735 if (!mod_plink_timer(sta,
736 dot11MeshConfirmTimeout(sdata)))
737 sta->ignore_plink_timer = true;
738
Johannes Berg07346f812008-05-03 01:02:02 +0200739 spin_unlock_bh(&sta->lock);
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100740 break;
741 default:
Johannes Berg07346f812008-05-03 01:02:02 +0200742 spin_unlock_bh(&sta->lock);
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100743 break;
744 }
745 break;
746
Javier Cardona57cf8042011-05-13 10:45:43 -0700747 case NL80211_PLINK_OPN_RCVD:
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100748 switch (event) {
749 case OPN_RJCT:
750 case CNF_RJCT:
Thomas Pedersen54ef6562011-08-11 19:35:12 -0700751 reason = cpu_to_le16(WLAN_REASON_MESH_CONFIG);
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100752 case CLS_ACPT:
753 if (!reason)
Thomas Pedersen54ef6562011-08-11 19:35:12 -0700754 reason = cpu_to_le16(WLAN_REASON_MESH_CLOSE);
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100755 sta->reason = reason;
Javier Cardona57cf8042011-05-13 10:45:43 -0700756 sta->plink_state = NL80211_PLINK_HOLDING;
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100757 if (!mod_plink_timer(sta,
758 dot11MeshHoldingTimeout(sdata)))
759 sta->ignore_plink_timer = true;
760
761 llid = sta->llid;
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, WLAN_SP_MESH_PEERING_CLOSE,
764 sta->sta.addr, llid, plid, reason);
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100765 break;
766 case OPN_ACPT:
767 llid = sta->llid;
Johannes Berg07346f812008-05-03 01:02:02 +0200768 spin_unlock_bh(&sta->lock);
Thomas Pedersen54ef6562011-08-11 19:35:12 -0700769 mesh_plink_frame_tx(sdata,
770 WLAN_SP_MESH_PEERING_CONFIRM,
771 sta->sta.addr, llid, plid, 0);
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100772 break;
773 case CNF_ACPT:
Johannes Bergd0709a62008-02-25 16:27:46 +0100774 del_timer(&sta->plink_timer);
Javier Cardona57cf8042011-05-13 10:45:43 -0700775 sta->plink_state = NL80211_PLINK_ESTAB;
Johannes Berg07346f812008-05-03 01:02:02 +0200776 spin_unlock_bh(&sta->lock);
John W. Linvillec9370192010-06-21 17:14:07 -0400777 mesh_plink_inc_estab_count(sdata);
778 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BEACON);
Johannes Berg0c68ae262008-10-27 15:56:10 -0700779 mpl_dbg("Mesh plink with %pM ESTABLISHED\n",
780 sta->sta.addr);
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100781 break;
782 default:
Johannes Berg07346f812008-05-03 01:02:02 +0200783 spin_unlock_bh(&sta->lock);
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100784 break;
785 }
786 break;
787
Javier Cardona57cf8042011-05-13 10:45:43 -0700788 case NL80211_PLINK_CNF_RCVD:
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100789 switch (event) {
790 case OPN_RJCT:
791 case CNF_RJCT:
Thomas Pedersen54ef6562011-08-11 19:35:12 -0700792 reason = cpu_to_le16(WLAN_REASON_MESH_CONFIG);
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100793 case CLS_ACPT:
794 if (!reason)
Thomas Pedersen54ef6562011-08-11 19:35:12 -0700795 reason = cpu_to_le16(WLAN_REASON_MESH_CLOSE);
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100796 sta->reason = reason;
Javier Cardona57cf8042011-05-13 10:45:43 -0700797 sta->plink_state = NL80211_PLINK_HOLDING;
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100798 if (!mod_plink_timer(sta,
799 dot11MeshHoldingTimeout(sdata)))
800 sta->ignore_plink_timer = true;
801
802 llid = sta->llid;
Johannes Berg07346f812008-05-03 01:02:02 +0200803 spin_unlock_bh(&sta->lock);
Thomas Pedersen54ef6562011-08-11 19:35:12 -0700804 mesh_plink_frame_tx(sdata,
805 WLAN_SP_MESH_PEERING_CLOSE,
806 sta->sta.addr, llid, plid, reason);
Johannes Bergff59dc72008-02-25 10:11:50 +0100807 break;
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100808 case OPN_ACPT:
Johannes Bergd0709a62008-02-25 16:27:46 +0100809 del_timer(&sta->plink_timer);
Javier Cardona57cf8042011-05-13 10:45:43 -0700810 sta->plink_state = NL80211_PLINK_ESTAB;
Johannes Berg07346f812008-05-03 01:02:02 +0200811 spin_unlock_bh(&sta->lock);
John W. Linvillec9370192010-06-21 17:14:07 -0400812 mesh_plink_inc_estab_count(sdata);
813 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BEACON);
Johannes Berg0c68ae262008-10-27 15:56:10 -0700814 mpl_dbg("Mesh plink with %pM ESTABLISHED\n",
815 sta->sta.addr);
Thomas Pedersen54ef6562011-08-11 19:35:12 -0700816 mesh_plink_frame_tx(sdata,
817 WLAN_SP_MESH_PEERING_CONFIRM,
818 sta->sta.addr, llid, plid, 0);
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100819 break;
820 default:
Johannes Berg07346f812008-05-03 01:02:02 +0200821 spin_unlock_bh(&sta->lock);
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100822 break;
823 }
824 break;
825
Javier Cardona57cf8042011-05-13 10:45:43 -0700826 case NL80211_PLINK_ESTAB:
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100827 switch (event) {
828 case CLS_ACPT:
Thomas Pedersen54ef6562011-08-11 19:35:12 -0700829 reason = cpu_to_le16(WLAN_REASON_MESH_CLOSE);
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100830 sta->reason = reason;
John W. Linvillec9370192010-06-21 17:14:07 -0400831 deactivated = __mesh_plink_deactivate(sta);
Javier Cardona57cf8042011-05-13 10:45:43 -0700832 sta->plink_state = NL80211_PLINK_HOLDING;
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100833 llid = sta->llid;
Johannes Bergd0709a62008-02-25 16:27:46 +0100834 mod_plink_timer(sta, dot11MeshHoldingTimeout(sdata));
Johannes Berg07346f812008-05-03 01:02:02 +0200835 spin_unlock_bh(&sta->lock);
John W. Linvillec9370192010-06-21 17:14:07 -0400836 if (deactivated)
837 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BEACON);
Thomas Pedersen54ef6562011-08-11 19:35:12 -0700838 mesh_plink_frame_tx(sdata, WLAN_SP_MESH_PEERING_CLOSE,
839 sta->sta.addr, llid, plid, reason);
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100840 break;
841 case OPN_ACPT:
842 llid = sta->llid;
Johannes Berg07346f812008-05-03 01:02:02 +0200843 spin_unlock_bh(&sta->lock);
Thomas Pedersen54ef6562011-08-11 19:35:12 -0700844 mesh_plink_frame_tx(sdata,
845 WLAN_SP_MESH_PEERING_CONFIRM,
846 sta->sta.addr, llid, plid, 0);
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100847 break;
848 default:
Johannes Berg07346f812008-05-03 01:02:02 +0200849 spin_unlock_bh(&sta->lock);
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100850 break;
851 }
852 break;
Javier Cardona57cf8042011-05-13 10:45:43 -0700853 case NL80211_PLINK_HOLDING:
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100854 switch (event) {
855 case CLS_ACPT:
Johannes Bergd0709a62008-02-25 16:27:46 +0100856 if (del_timer(&sta->plink_timer))
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100857 sta->ignore_plink_timer = 1;
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100858 mesh_plink_fsm_restart(sta);
Johannes Berg07346f812008-05-03 01:02:02 +0200859 spin_unlock_bh(&sta->lock);
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100860 break;
861 case OPN_ACPT:
862 case CNF_ACPT:
863 case OPN_RJCT:
864 case CNF_RJCT:
865 llid = sta->llid;
866 reason = sta->reason;
Johannes Berg07346f812008-05-03 01:02:02 +0200867 spin_unlock_bh(&sta->lock);
Thomas Pedersen54ef6562011-08-11 19:35:12 -0700868 mesh_plink_frame_tx(sdata, WLAN_SP_MESH_PEERING_CLOSE,
869 sta->sta.addr, llid, plid, reason);
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100870 break;
871 default:
Johannes Berg07346f812008-05-03 01:02:02 +0200872 spin_unlock_bh(&sta->lock);
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100873 }
874 break;
875 default:
Luis Carlos Cobob4e08ea2008-02-29 15:46:08 -0800876 /* should not get here, PLINK_BLOCKED is dealt with at the
Daniel Mack3ad2f3f2010-02-03 08:01:28 +0800877 * beginning of the function
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100878 */
Johannes Berg07346f812008-05-03 01:02:02 +0200879 spin_unlock_bh(&sta->lock);
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100880 break;
881 }
Johannes Bergd0709a62008-02-25 16:27:46 +0100882
883 rcu_read_unlock();
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100884}