blob: 6209327840f7c3a1e7cc7e56656cae3e750d2a2a [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
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100105 return sta;
106}
107
108/**
John W. Linvillec9370192010-06-21 17:14:07 -0400109 * __mesh_plink_deactivate - deactivate mesh peer link
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100110 *
111 * @sta: mesh peer link to deactivate
112 *
113 * All mesh paths with this peer as next hop will be flushed
114 *
Johannes Berg07346f812008-05-03 01:02:02 +0200115 * Locking: the caller must hold sta->lock
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100116 */
John W. Linvillec9370192010-06-21 17:14:07 -0400117static bool __mesh_plink_deactivate(struct sta_info *sta)
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100118{
Johannes Bergd0709a62008-02-25 16:27:46 +0100119 struct ieee80211_sub_if_data *sdata = sta->sdata;
John W. Linvillec9370192010-06-21 17:14:07 -0400120 bool deactivated = false;
Johannes Bergd0709a62008-02-25 16:27:46 +0100121
Javier Cardona57cf8042011-05-13 10:45:43 -0700122 if (sta->plink_state == NL80211_PLINK_ESTAB) {
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100123 mesh_plink_dec_estab_count(sdata);
John W. Linvillec9370192010-06-21 17:14:07 -0400124 deactivated = true;
125 }
Javier Cardona57cf8042011-05-13 10:45:43 -0700126 sta->plink_state = NL80211_PLINK_BLOCKED;
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100127 mesh_path_flush_by_nexthop(sta);
John W. Linvillec9370192010-06-21 17:14:07 -0400128
129 return deactivated;
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100130}
131
Johannes Berg902acc72008-02-23 15:17:19 +0100132/**
John W. Linvillec9370192010-06-21 17:14:07 -0400133 * mesh_plink_deactivate - deactivate mesh peer link
Johannes Berg902acc72008-02-23 15:17:19 +0100134 *
135 * @sta: mesh peer link to deactivate
136 *
137 * All mesh paths with this peer as next hop will be flushed
138 */
139void mesh_plink_deactivate(struct sta_info *sta)
140{
John W. Linvillec9370192010-06-21 17:14:07 -0400141 struct ieee80211_sub_if_data *sdata = sta->sdata;
142 bool deactivated;
143
Johannes Berg07346f812008-05-03 01:02:02 +0200144 spin_lock_bh(&sta->lock);
John W. Linvillec9370192010-06-21 17:14:07 -0400145 deactivated = __mesh_plink_deactivate(sta);
Thomas Pedersenba4a14e2011-09-20 13:43:32 -0700146 sta->reason = cpu_to_le16(WLAN_REASON_MESH_PEER_CANCELED);
147 mesh_plink_frame_tx(sdata, WLAN_SP_MESH_PEERING_CLOSE,
148 sta->sta.addr, sta->llid, sta->plid,
149 sta->reason);
Johannes Berg07346f812008-05-03 01:02:02 +0200150 spin_unlock_bh(&sta->lock);
John W. Linvillec9370192010-06-21 17:14:07 -0400151
152 if (deactivated)
153 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BEACON);
Johannes Berg902acc72008-02-23 15:17:19 +0100154}
155
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200156static int mesh_plink_frame_tx(struct ieee80211_sub_if_data *sdata,
Thomas Pedersen54ef6562011-08-11 19:35:12 -0700157 enum ieee80211_self_protected_actioncode action,
158 u8 *da, __le16 llid, __le16 plid, __le16 reason) {
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200159 struct ieee80211_local *local = sdata->local;
Thomas Pedersen3b69a9c2011-10-26 14:47:25 -0700160 struct sk_buff *skb;
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100161 struct ieee80211_mgmt *mgmt;
162 bool include_plid = false;
Thomas Pedersen8db09852011-08-12 20:01:00 -0700163 u16 peering_proto = 0;
Thomas Pedersen3b69a9c2011-10-26 14:47:25 -0700164 u8 *pos, ie_len = 4;
165 int hdr_len = offsetof(struct ieee80211_mgmt, u.action.u.self_prot) +
166 sizeof(mgmt->u.action.u.self_prot);
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100167
Javier Cardona65e8b0c2012-01-17 18:17:46 -0800168 skb = dev_alloc_skb(local->tx_headroom +
Thomas Pedersen3b69a9c2011-10-26 14:47:25 -0700169 hdr_len +
170 2 + /* capability info */
171 2 + /* AID */
172 2 + 8 + /* supported rates */
173 2 + (IEEE80211_MAX_SUPP_RATES - 8) +
174 2 + sdata->u.mesh.mesh_id_len +
175 2 + sizeof(struct ieee80211_meshconf_ie) +
Thomas Pedersen176f3602011-10-26 14:47:27 -0700176 2 + sizeof(struct ieee80211_ht_cap) +
Johannes Berg074d46d2012-03-15 19:45:16 +0100177 2 + sizeof(struct ieee80211_ht_operation) +
Thomas Pedersen3b69a9c2011-10-26 14:47:25 -0700178 2 + 8 + /* peering IE */
179 sdata->u.mesh.ie_len);
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100180 if (!skb)
181 return -1;
Javier Cardona65e8b0c2012-01-17 18:17:46 -0800182 skb_reserve(skb, local->tx_headroom);
Thomas Pedersen3b69a9c2011-10-26 14:47:25 -0700183 mgmt = (struct ieee80211_mgmt *) skb_put(skb, hdr_len);
184 memset(mgmt, 0, hdr_len);
Harvey Harrisone7827a72008-07-15 18:44:13 -0700185 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
186 IEEE80211_STYPE_ACTION);
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100187 memcpy(mgmt->da, da, ETH_ALEN);
Johannes Berg47846c92009-11-25 17:46:19 +0100188 memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
Javier Cardona915b5c52011-05-03 16:57:10 -0700189 memcpy(mgmt->bssid, sdata->vif.addr, ETH_ALEN);
Thomas Pedersen8db09852011-08-12 20:01:00 -0700190 mgmt->u.action.category = WLAN_CATEGORY_SELF_PROTECTED;
191 mgmt->u.action.u.self_prot.action_code = action;
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100192
Thomas Pedersen8db09852011-08-12 20:01:00 -0700193 if (action != WLAN_SP_MESH_PEERING_CLOSE) {
194 /* capability info */
195 pos = skb_put(skb, 2);
196 memset(pos, 0, 2);
Thomas Pedersen54ef6562011-08-11 19:35:12 -0700197 if (action == WLAN_SP_MESH_PEERING_CONFIRM) {
Thomas Pedersen8db09852011-08-12 20:01:00 -0700198 /* AID */
199 pos = skb_put(skb, 2);
Rui Paulo77fa76b2009-11-09 23:46:52 +0000200 memcpy(pos + 2, &plid, 2);
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100201 }
Ashok Nagarajan657c3e02012-04-02 21:21:20 -0700202 if (ieee80211_add_srates_ie(&sdata->vif, skb, true) ||
203 ieee80211_add_ext_srates_ie(&sdata->vif, skb, true) ||
Thomas Pedersen082ebb02011-08-11 19:35:10 -0700204 mesh_add_rsn_ie(skb, sdata) ||
205 mesh_add_meshid_ie(skb, sdata) ||
206 mesh_add_meshconf_ie(skb, sdata))
207 return -1;
Thomas Pedersen8db09852011-08-12 20:01:00 -0700208 } else { /* WLAN_SP_MESH_PEERING_CLOSE */
209 if (mesh_add_meshid_ie(skb, sdata))
210 return -1;
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100211 }
212
Thomas Pedersen8db09852011-08-12 20:01:00 -0700213 /* Add Mesh Peering Management element */
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100214 switch (action) {
Thomas Pedersen54ef6562011-08-11 19:35:12 -0700215 case WLAN_SP_MESH_PEERING_OPEN:
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100216 break;
Thomas Pedersen54ef6562011-08-11 19:35:12 -0700217 case WLAN_SP_MESH_PEERING_CONFIRM:
Thomas Pedersen8db09852011-08-12 20:01:00 -0700218 ie_len += 2;
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100219 include_plid = true;
220 break;
Thomas Pedersen54ef6562011-08-11 19:35:12 -0700221 case WLAN_SP_MESH_PEERING_CLOSE:
Thomas Pedersen8db09852011-08-12 20:01:00 -0700222 if (plid) {
223 ie_len += 2;
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100224 include_plid = true;
225 }
Thomas Pedersen8db09852011-08-12 20:01:00 -0700226 ie_len += 2; /* reason code */
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100227 break;
Thomas Pedersen8db09852011-08-12 20:01:00 -0700228 default:
229 return -EINVAL;
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100230 }
231
Thomas Pedersen8db09852011-08-12 20:01:00 -0700232 if (WARN_ON(skb_tailroom(skb) < 2 + ie_len))
233 return -ENOMEM;
234
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100235 pos = skb_put(skb, 2 + ie_len);
Thomas Pedersen8db09852011-08-12 20:01:00 -0700236 *pos++ = WLAN_EID_PEER_MGMT;
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100237 *pos++ = ie_len;
Thomas Pedersen8db09852011-08-12 20:01:00 -0700238 memcpy(pos, &peering_proto, 2);
239 pos += 2;
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100240 memcpy(pos, &llid, 2);
Thomas Pedersen8db09852011-08-12 20:01:00 -0700241 pos += 2;
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100242 if (include_plid) {
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100243 memcpy(pos, &plid, 2);
Thomas Pedersen8db09852011-08-12 20:01:00 -0700244 pos += 2;
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100245 }
Thomas Pedersen54ef6562011-08-11 19:35:12 -0700246 if (action == WLAN_SP_MESH_PEERING_CLOSE) {
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100247 memcpy(pos, &reason, 2);
Thomas Pedersen8db09852011-08-12 20:01:00 -0700248 pos += 2;
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100249 }
Thomas Pedersen176f3602011-10-26 14:47:27 -0700250
251 if (action != WLAN_SP_MESH_PEERING_CLOSE) {
252 if (mesh_add_ht_cap_ie(skb, sdata) ||
Johannes Berg074d46d2012-03-15 19:45:16 +0100253 mesh_add_ht_oper_ie(skb, sdata))
Thomas Pedersen176f3602011-10-26 14:47:27 -0700254 return -1;
255 }
256
Thomas Pedersen8db09852011-08-12 20:01:00 -0700257 if (mesh_add_vendor_ies(skb, sdata))
258 return -1;
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100259
Johannes Berg62ae67b2009-11-18 18:42:05 +0100260 ieee80211_tx_skb(sdata, skb);
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100261 return 0;
262}
263
Thomas Pedersen54ab1ff2012-04-18 19:23:42 -0700264/* mesh_peer_init - initialize new mesh peer and return resulting sta_info
265 *
266 * @sdata: local meshif
267 * @addr: peer's address
Thomas Pedersen54ab1ff2012-04-18 19:23:42 -0700268 * @elems: IEs from beacon or mesh peering frame
269 *
270 * call under RCU
271 */
272static struct sta_info *mesh_peer_init(struct ieee80211_sub_if_data *sdata,
Thomas Pedersenf743ff42012-04-18 19:23:43 -0700273 u8 *addr,
Thomas Pedersen54ab1ff2012-04-18 19:23:42 -0700274 struct ieee802_11_elems *elems)
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100275{
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200276 struct ieee80211_local *local = sdata->local;
Thomas Pedersenf743ff42012-04-18 19:23:43 -0700277 enum ieee80211_band band = local->oper_channel->band;
Thomas Pedersen54ab1ff2012-04-18 19:23:42 -0700278 struct ieee80211_supported_band *sband;
Thomas Pedersenf743ff42012-04-18 19:23:43 -0700279 u32 rates, basic_rates = 0;
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100280 struct sta_info *sta;
Thomas Pedersene87278e2012-04-26 15:01:06 -0700281 bool insert = false;
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100282
Thomas Pedersenf743ff42012-04-18 19:23:43 -0700283 sband = local->hw.wiphy->bands[band];
284 rates = ieee80211_sta_get_rates(local, elems, band, &basic_rates);
Johannes Bergd0709a62008-02-25 16:27:46 +0100285
Thomas Pedersen54ab1ff2012-04-18 19:23:42 -0700286 sta = sta_info_get(sdata, addr);
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100287 if (!sta) {
Thomas Pedersen54ab1ff2012-04-18 19:23:42 -0700288 sta = mesh_plink_alloc(sdata, addr);
Johannes Berg34e89502010-02-03 13:59:58 +0100289 if (!sta)
Thomas Pedersen54ab1ff2012-04-18 19:23:42 -0700290 return NULL;
Thomas Pedersene87278e2012-04-26 15:01:06 -0700291 insert = true;
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100292 }
293
Thomas Pedersen54ab1ff2012-04-18 19:23:42 -0700294 spin_lock_bh(&sta->lock);
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100295 sta->last_rx = jiffies;
Thomas Pedersenf743ff42012-04-18 19:23:43 -0700296 sta->sta.supp_rates[band] = rates;
Thomas Pedersene76781e2012-04-18 19:24:13 -0700297 if (elems->ht_cap_elem &&
298 sdata->local->_oper_channel_type != NL80211_CHAN_NO_HT)
Thomas Pedersen54ab1ff2012-04-18 19:23:42 -0700299 ieee80211_ht_cap_ie_to_sta_ht_cap(sdata, sband,
300 elems->ht_cap_elem,
301 &sta->sta.ht_cap);
302 else
303 memset(&sta->sta.ht_cap, 0, sizeof(sta->sta.ht_cap));
304
Thomas Pedersenc7d25822012-04-26 15:01:07 -0700305 if (elems->ht_operation)
306 if (!(elems->ht_operation->ht_param &
307 IEEE80211_HT_PARAM_CHAN_WIDTH_ANY))
308 sta->sta.ht_cap.cap &=
309 ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
310
Thomas Pedersen54ab1ff2012-04-18 19:23:42 -0700311 rate_control_rate_init(sta);
312 spin_unlock_bh(&sta->lock);
313
Thomas Pedersene87278e2012-04-26 15:01:06 -0700314 if (insert && sta_info_insert(sta))
315 return NULL;
316
Thomas Pedersen54ab1ff2012-04-18 19:23:42 -0700317 return sta;
318}
319
Thomas Pedersenf743ff42012-04-18 19:23:43 -0700320void mesh_neighbour_update(struct ieee80211_sub_if_data *sdata,
321 u8 *hw_addr,
Thomas Pedersen54ab1ff2012-04-18 19:23:42 -0700322 struct ieee802_11_elems *elems)
323{
324 struct sta_info *sta;
325
326 /* Userspace handles peer allocation when security is enabled */
327 if (sdata->u.mesh.security & IEEE80211_MESH_SEC_AUTHED) {
328 cfg80211_notify_new_peer_candidate(sdata->dev, hw_addr,
329 elems->ie_start,
330 elems->total_len,
331 GFP_KERNEL);
332 return;
333 }
334
335 rcu_read_lock();
Thomas Pedersenf743ff42012-04-18 19:23:43 -0700336 sta = mesh_peer_init(sdata, hw_addr, elems);
Thomas Pedersen54ab1ff2012-04-18 19:23:42 -0700337 if (!sta)
338 goto out;
339
Javier Cardona1570ca52011-04-07 15:08:35 -0700340 if (mesh_peer_accepts_plinks(elems) &&
Thomas Pedersen54ab1ff2012-04-18 19:23:42 -0700341 sta->plink_state == NL80211_PLINK_LISTEN &&
342 sdata->u.mesh.accepting_plinks &&
343 sdata->u.mesh.mshcfg.auto_open_plinks &&
344 rssi_threshold_check(sta, sdata))
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100345 mesh_plink_open(sta);
346
Thomas Pedersen54ab1ff2012-04-18 19:23:42 -0700347out:
Johannes Bergd0709a62008-02-25 16:27:46 +0100348 rcu_read_unlock();
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100349}
350
351static void mesh_plink_timer(unsigned long data)
352{
353 struct sta_info *sta;
354 __le16 llid, plid, reason;
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100355 struct ieee80211_sub_if_data *sdata;
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100356
Johannes Bergd0709a62008-02-25 16:27:46 +0100357 /*
358 * This STA is valid because sta_info_destroy() will
359 * del_timer_sync() this timer after having made sure
360 * it cannot be readded (by deleting the plink.)
361 */
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100362 sta = (struct sta_info *) data;
363
Johannes Berg5bb644a2009-05-17 11:40:42 +0200364 if (sta->sdata->local->quiescing) {
365 sta->plink_timer_was_running = true;
366 return;
367 }
368
Johannes Berg07346f812008-05-03 01:02:02 +0200369 spin_lock_bh(&sta->lock);
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100370 if (sta->ignore_plink_timer) {
371 sta->ignore_plink_timer = false;
Johannes Berg07346f812008-05-03 01:02:02 +0200372 spin_unlock_bh(&sta->lock);
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100373 return;
374 }
Johannes Berg0c68ae262008-10-27 15:56:10 -0700375 mpl_dbg("Mesh plink timer for %pM fired on state %d\n",
376 sta->sta.addr, sta->plink_state);
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100377 reason = 0;
378 llid = sta->llid;
379 plid = sta->plid;
Johannes Bergd0709a62008-02-25 16:27:46 +0100380 sdata = sta->sdata;
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100381
382 switch (sta->plink_state) {
Javier Cardona57cf8042011-05-13 10:45:43 -0700383 case NL80211_PLINK_OPN_RCVD:
384 case NL80211_PLINK_OPN_SNT:
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100385 /* retry timer */
386 if (sta->plink_retries < dot11MeshMaxRetries(sdata)) {
387 u32 rand;
Johannes Berg0c68ae262008-10-27 15:56:10 -0700388 mpl_dbg("Mesh plink for %pM (retry, timeout): %d %d\n",
389 sta->sta.addr, sta->plink_retries,
390 sta->plink_timeout);
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100391 get_random_bytes(&rand, sizeof(u32));
392 sta->plink_timeout = sta->plink_timeout +
393 rand % sta->plink_timeout;
394 ++sta->plink_retries;
Johannes Bergd0709a62008-02-25 16:27:46 +0100395 mod_plink_timer(sta, sta->plink_timeout);
Johannes Berg07346f812008-05-03 01:02:02 +0200396 spin_unlock_bh(&sta->lock);
Thomas Pedersen54ef6562011-08-11 19:35:12 -0700397 mesh_plink_frame_tx(sdata, WLAN_SP_MESH_PEERING_OPEN,
398 sta->sta.addr, llid, 0, 0);
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100399 break;
400 }
Thomas Pedersen54ef6562011-08-11 19:35:12 -0700401 reason = cpu_to_le16(WLAN_REASON_MESH_MAX_RETRIES);
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100402 /* fall through on else */
Javier Cardona57cf8042011-05-13 10:45:43 -0700403 case NL80211_PLINK_CNF_RCVD:
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100404 /* confirm timer */
405 if (!reason)
Thomas Pedersen54ef6562011-08-11 19:35:12 -0700406 reason = cpu_to_le16(WLAN_REASON_MESH_CONFIRM_TIMEOUT);
Javier Cardona57cf8042011-05-13 10:45:43 -0700407 sta->plink_state = NL80211_PLINK_HOLDING;
Johannes Bergd0709a62008-02-25 16:27:46 +0100408 mod_plink_timer(sta, dot11MeshHoldingTimeout(sdata));
Johannes Berg07346f812008-05-03 01:02:02 +0200409 spin_unlock_bh(&sta->lock);
Thomas Pedersen54ef6562011-08-11 19:35:12 -0700410 mesh_plink_frame_tx(sdata, WLAN_SP_MESH_PEERING_CLOSE,
411 sta->sta.addr, llid, plid, reason);
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100412 break;
Javier Cardona57cf8042011-05-13 10:45:43 -0700413 case NL80211_PLINK_HOLDING:
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100414 /* holding timer */
Johannes Bergd0709a62008-02-25 16:27:46 +0100415 del_timer(&sta->plink_timer);
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100416 mesh_plink_fsm_restart(sta);
Johannes Berg07346f812008-05-03 01:02:02 +0200417 spin_unlock_bh(&sta->lock);
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100418 break;
419 default:
Johannes Berg07346f812008-05-03 01:02:02 +0200420 spin_unlock_bh(&sta->lock);
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100421 break;
422 }
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100423}
424
Johannes Berg5bb644a2009-05-17 11:40:42 +0200425#ifdef CONFIG_PM
426void mesh_plink_quiesce(struct sta_info *sta)
427{
428 if (del_timer_sync(&sta->plink_timer))
429 sta->plink_timer_was_running = true;
430}
431
432void mesh_plink_restart(struct sta_info *sta)
433{
434 if (sta->plink_timer_was_running) {
435 add_timer(&sta->plink_timer);
436 sta->plink_timer_was_running = false;
437 }
438}
439#endif
440
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100441static inline void mesh_plink_timer_set(struct sta_info *sta, int timeout)
442{
443 sta->plink_timer.expires = jiffies + (HZ * timeout / 1000);
444 sta->plink_timer.data = (unsigned long) sta;
445 sta->plink_timer.function = mesh_plink_timer;
446 sta->plink_timeout = timeout;
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100447 add_timer(&sta->plink_timer);
448}
449
450int mesh_plink_open(struct sta_info *sta)
451{
452 __le16 llid;
Johannes Bergd0709a62008-02-25 16:27:46 +0100453 struct ieee80211_sub_if_data *sdata = sta->sdata;
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100454
Johannes Bergc2c98fd2011-09-29 16:04:36 +0200455 if (!test_sta_flag(sta, WLAN_STA_AUTH))
Javier Cardona53e80512011-04-07 15:08:32 -0700456 return -EPERM;
457
Johannes Berg07346f812008-05-03 01:02:02 +0200458 spin_lock_bh(&sta->lock);
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100459 get_random_bytes(&llid, 2);
460 sta->llid = llid;
Javier Cardona57cf8042011-05-13 10:45:43 -0700461 if (sta->plink_state != NL80211_PLINK_LISTEN) {
Johannes Berg07346f812008-05-03 01:02:02 +0200462 spin_unlock_bh(&sta->lock);
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100463 return -EBUSY;
464 }
Javier Cardona57cf8042011-05-13 10:45:43 -0700465 sta->plink_state = NL80211_PLINK_OPN_SNT;
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100466 mesh_plink_timer_set(sta, dot11MeshRetryTimeout(sdata));
Johannes Berg07346f812008-05-03 01:02:02 +0200467 spin_unlock_bh(&sta->lock);
Johannes Berg0c68ae262008-10-27 15:56:10 -0700468 mpl_dbg("Mesh plink: starting establishment with %pM\n",
469 sta->sta.addr);
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100470
Thomas Pedersen54ef6562011-08-11 19:35:12 -0700471 return mesh_plink_frame_tx(sdata, WLAN_SP_MESH_PEERING_OPEN,
Johannes Berg17741cd2008-09-11 00:02:02 +0200472 sta->sta.addr, llid, 0, 0);
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100473}
474
475void mesh_plink_block(struct sta_info *sta)
476{
John W. Linvillec9370192010-06-21 17:14:07 -0400477 struct ieee80211_sub_if_data *sdata = sta->sdata;
478 bool deactivated;
479
Johannes Berg07346f812008-05-03 01:02:02 +0200480 spin_lock_bh(&sta->lock);
John W. Linvillec9370192010-06-21 17:14:07 -0400481 deactivated = __mesh_plink_deactivate(sta);
Javier Cardona57cf8042011-05-13 10:45:43 -0700482 sta->plink_state = NL80211_PLINK_BLOCKED;
Johannes Berg07346f812008-05-03 01:02:02 +0200483 spin_unlock_bh(&sta->lock);
John W. Linvillec9370192010-06-21 17:14:07 -0400484
485 if (deactivated)
486 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BEACON);
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100487}
488
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100489
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200490void mesh_rx_plink_frame(struct ieee80211_sub_if_data *sdata, struct ieee80211_mgmt *mgmt,
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100491 size_t len, struct ieee80211_rx_status *rx_status)
492{
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100493 struct ieee802_11_elems elems;
494 struct sta_info *sta;
495 enum plink_event event;
Thomas Pedersen54ef6562011-08-11 19:35:12 -0700496 enum ieee80211_self_protected_actioncode ftype;
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100497 size_t baselen;
Christian Lamparterd12c7452010-10-08 22:27:07 +0200498 bool deactivated, matches_local = true;
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100499 u8 ie_len;
500 u8 *baseaddr;
501 __le16 plid, llid, reason;
Rui Paulo1460dd12009-11-09 23:46:48 +0000502#ifdef CONFIG_MAC80211_VERBOSE_MPL_DEBUG
503 static const char *mplstates[] = {
Javier Cardona57cf8042011-05-13 10:45:43 -0700504 [NL80211_PLINK_LISTEN] = "LISTEN",
505 [NL80211_PLINK_OPN_SNT] = "OPN-SNT",
506 [NL80211_PLINK_OPN_RCVD] = "OPN-RCVD",
507 [NL80211_PLINK_CNF_RCVD] = "CNF_RCVD",
508 [NL80211_PLINK_ESTAB] = "ESTAB",
509 [NL80211_PLINK_HOLDING] = "HOLDING",
510 [NL80211_PLINK_BLOCKED] = "BLOCKED"
Rui Paulo1460dd12009-11-09 23:46:48 +0000511 };
512#endif
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100513
Johannes Berg9c80d3d2008-09-08 15:41:59 +0200514 /* need action_code, aux */
515 if (len < IEEE80211_MIN_ACTION_SIZE + 3)
516 return;
517
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100518 if (is_multicast_ether_addr(mgmt->da)) {
519 mpl_dbg("Mesh plink: ignore frame from multicast address");
520 return;
521 }
522
Thomas Pedersen8db09852011-08-12 20:01:00 -0700523 baseaddr = mgmt->u.action.u.self_prot.variable;
524 baselen = (u8 *) mgmt->u.action.u.self_prot.variable - (u8 *) mgmt;
525 if (mgmt->u.action.u.self_prot.action_code ==
Thomas Pedersen54ef6562011-08-11 19:35:12 -0700526 WLAN_SP_MESH_PEERING_CONFIRM) {
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100527 baseaddr += 4;
David Woo70bdb6b2009-08-12 11:03:44 -0700528 baselen += 4;
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100529 }
530 ieee802_11_parse_elems(baseaddr, len - baselen, &elems);
Thomas Pedersen8db09852011-08-12 20:01:00 -0700531 if (!elems.peering) {
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100532 mpl_dbg("Mesh plink: missing necessary peer link ie\n");
533 return;
534 }
Javier Cardonab130e5c2011-05-03 16:57:07 -0700535 if (elems.rsn_len &&
536 sdata->u.mesh.security == IEEE80211_MESH_SEC_NONE) {
Javier Cardona5cff5e02011-04-07 15:08:29 -0700537 mpl_dbg("Mesh plink: can't establish link with secure peer\n");
538 return;
539 }
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100540
Thomas Pedersen8db09852011-08-12 20:01:00 -0700541 ftype = mgmt->u.action.u.self_prot.action_code;
542 ie_len = elems.peering_len;
543 if ((ftype == WLAN_SP_MESH_PEERING_OPEN && ie_len != 4) ||
544 (ftype == WLAN_SP_MESH_PEERING_CONFIRM && ie_len != 6) ||
545 (ftype == WLAN_SP_MESH_PEERING_CLOSE && ie_len != 6
546 && ie_len != 8)) {
Rui Paulo09383932009-11-09 23:46:43 +0000547 mpl_dbg("Mesh plink: incorrect plink ie length %d %d\n",
548 ftype, ie_len);
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100549 return;
550 }
551
Thomas Pedersen54ef6562011-08-11 19:35:12 -0700552 if (ftype != WLAN_SP_MESH_PEERING_CLOSE &&
553 (!elems.mesh_id || !elems.mesh_config)) {
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100554 mpl_dbg("Mesh plink: missing necessary ie\n");
555 return;
556 }
557 /* Note the lines below are correct, the llid in the frame is the plid
558 * from the point of view of this host.
559 */
Thomas Pedersen8db09852011-08-12 20:01:00 -0700560 memcpy(&plid, PLINK_GET_LLID(elems.peering), 2);
Thomas Pedersen54ef6562011-08-11 19:35:12 -0700561 if (ftype == WLAN_SP_MESH_PEERING_CONFIRM ||
Thomas Pedersen8db09852011-08-12 20:01:00 -0700562 (ftype == WLAN_SP_MESH_PEERING_CLOSE && ie_len == 8))
563 memcpy(&llid, PLINK_GET_PLID(elems.peering), 2);
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100564
Johannes Bergd0709a62008-02-25 16:27:46 +0100565 rcu_read_lock();
566
Johannes Bergabe60632009-11-25 17:46:18 +0100567 sta = sta_info_get(sdata, mgmt->sa);
Thomas Pedersen54ef6562011-08-11 19:35:12 -0700568 if (!sta && ftype != WLAN_SP_MESH_PEERING_OPEN) {
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100569 mpl_dbg("Mesh plink: cls or cnf from unknown peer\n");
Johannes Bergd0709a62008-02-25 16:27:46 +0100570 rcu_read_unlock();
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100571 return;
572 }
573
Ashok Nagarajan55335132012-02-28 17:04:08 -0800574 if (ftype == WLAN_SP_MESH_PEERING_OPEN &&
Ashok Nagarajan3d4f9692012-03-06 12:48:30 -0800575 !rssi_threshold_check(sta, sdata)) {
Ashok Nagarajan55335132012-02-28 17:04:08 -0800576 mpl_dbg("Mesh plink: %pM does not meet rssi threshold\n",
Ashok Nagarajan3d4f9692012-03-06 12:48:30 -0800577 mgmt->sa);
Ashok Nagarajan55335132012-02-28 17:04:08 -0800578 rcu_read_unlock();
579 return;
580 }
581
Johannes Bergc2c98fd2011-09-29 16:04:36 +0200582 if (sta && !test_sta_flag(sta, WLAN_STA_AUTH)) {
Javier Cardona53e80512011-04-07 15:08:32 -0700583 mpl_dbg("Mesh plink: Action frame from non-authed peer\n");
584 rcu_read_unlock();
585 return;
586 }
587
Javier Cardona57cf8042011-05-13 10:45:43 -0700588 if (sta && sta->plink_state == NL80211_PLINK_BLOCKED) {
Johannes Bergd0709a62008-02-25 16:27:46 +0100589 rcu_read_unlock();
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100590 return;
591 }
592
593 /* Now we will figure out the appropriate event... */
594 event = PLINK_UNDEFINED;
Thomas Pedersen54ef6562011-08-11 19:35:12 -0700595 if (ftype != WLAN_SP_MESH_PEERING_CLOSE &&
Thomas Pedersenf743ff42012-04-18 19:23:43 -0700596 !mesh_matches_local(sdata, &elems)) {
Christian Lamparterd12c7452010-10-08 22:27:07 +0200597 matches_local = false;
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100598 switch (ftype) {
Thomas Pedersen54ef6562011-08-11 19:35:12 -0700599 case WLAN_SP_MESH_PEERING_OPEN:
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100600 event = OPN_RJCT;
601 break;
Thomas Pedersen54ef6562011-08-11 19:35:12 -0700602 case WLAN_SP_MESH_PEERING_CONFIRM:
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100603 event = CNF_RJCT;
604 break;
Thomas Pedersen54ef6562011-08-11 19:35:12 -0700605 default:
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100606 break;
607 }
Christian Lamparterd12c7452010-10-08 22:27:07 +0200608 }
609
610 if (!sta && !matches_local) {
611 rcu_read_unlock();
Thomas Pedersen54ef6562011-08-11 19:35:12 -0700612 reason = cpu_to_le16(WLAN_REASON_MESH_CONFIG);
Christian Lamparterd12c7452010-10-08 22:27:07 +0200613 llid = 0;
Thomas Pedersen54ef6562011-08-11 19:35:12 -0700614 mesh_plink_frame_tx(sdata, WLAN_SP_MESH_PEERING_CLOSE,
615 mgmt->sa, llid, plid, reason);
Christian Lamparterd12c7452010-10-08 22:27:07 +0200616 return;
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100617 } else if (!sta) {
Thomas Pedersen54ef6562011-08-11 19:35:12 -0700618 /* ftype == WLAN_SP_MESH_PEERING_OPEN */
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100619 if (!mesh_plink_free_count(sdata)) {
620 mpl_dbg("Mesh plink error: no more free plinks\n");
Johannes Berg73651ee2008-02-25 16:27:47 +0100621 rcu_read_unlock();
622 return;
623 }
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100624 event = OPN_ACPT;
Christian Lamparterd12c7452010-10-08 22:27:07 +0200625 } else if (matches_local) {
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100626 switch (ftype) {
Thomas Pedersen54ef6562011-08-11 19:35:12 -0700627 case WLAN_SP_MESH_PEERING_OPEN:
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100628 if (!mesh_plink_free_count(sdata) ||
Johannes Bergd0709a62008-02-25 16:27:46 +0100629 (sta->plid && sta->plid != plid))
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100630 event = OPN_IGNR;
631 else
632 event = OPN_ACPT;
633 break;
Thomas Pedersen54ef6562011-08-11 19:35:12 -0700634 case WLAN_SP_MESH_PEERING_CONFIRM:
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100635 if (!mesh_plink_free_count(sdata) ||
Johannes Bergd0709a62008-02-25 16:27:46 +0100636 (sta->llid != llid || sta->plid != plid))
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100637 event = CNF_IGNR;
638 else
639 event = CNF_ACPT;
640 break;
Thomas Pedersen54ef6562011-08-11 19:35:12 -0700641 case WLAN_SP_MESH_PEERING_CLOSE:
Javier Cardona57cf8042011-05-13 10:45:43 -0700642 if (sta->plink_state == NL80211_PLINK_ESTAB)
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100643 /* Do not check for llid or plid. This does not
644 * follow the standard but since multiple plinks
645 * per sta are not supported, it is necessary in
646 * order to avoid a livelock when MP A sees an
647 * establish peer link to MP B but MP B does not
648 * see it. This can be caused by a timeout in
649 * B's peer link establishment or B beign
650 * restarted.
651 */
652 event = CLS_ACPT;
653 else if (sta->plid != plid)
654 event = CLS_IGNR;
655 else if (ie_len == 7 && sta->llid != llid)
656 event = CLS_IGNR;
657 else
658 event = CLS_ACPT;
659 break;
660 default:
661 mpl_dbg("Mesh plink: unknown frame subtype\n");
Johannes Bergd0709a62008-02-25 16:27:46 +0100662 rcu_read_unlock();
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100663 return;
664 }
Thomas Pedersen54ab1ff2012-04-18 19:23:42 -0700665 }
666
667 if (event == OPN_ACPT) {
668 /* allocate sta entry if necessary and update info */
Thomas Pedersenf743ff42012-04-18 19:23:43 -0700669 sta = mesh_peer_init(sdata, mgmt->sa, &elems);
Thomas Pedersen54ab1ff2012-04-18 19:23:42 -0700670 if (!sta) {
671 mpl_dbg("Mesh plink: failed to init peer!\n");
672 rcu_read_unlock();
673 return;
674 }
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100675 }
676
Rui Paulo1460dd12009-11-09 23:46:48 +0000677 mpl_dbg("Mesh plink (peer, state, llid, plid, event): %pM %s %d %d %d\n",
678 mgmt->sa, mplstates[sta->plink_state],
Johannes Berg0c68ae262008-10-27 15:56:10 -0700679 le16_to_cpu(sta->llid), le16_to_cpu(sta->plid),
680 event);
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100681 reason = 0;
Thomas Pedersen54ab1ff2012-04-18 19:23:42 -0700682 spin_lock_bh(&sta->lock);
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100683 switch (sta->plink_state) {
684 /* spin_unlock as soon as state is updated at each case */
Javier Cardona57cf8042011-05-13 10:45:43 -0700685 case NL80211_PLINK_LISTEN:
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100686 switch (event) {
687 case CLS_ACPT:
688 mesh_plink_fsm_restart(sta);
Johannes Berg07346f812008-05-03 01:02:02 +0200689 spin_unlock_bh(&sta->lock);
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100690 break;
691 case OPN_ACPT:
Javier Cardona57cf8042011-05-13 10:45:43 -0700692 sta->plink_state = NL80211_PLINK_OPN_RCVD;
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100693 sta->plid = plid;
694 get_random_bytes(&llid, 2);
695 sta->llid = llid;
696 mesh_plink_timer_set(sta, dot11MeshRetryTimeout(sdata));
Johannes Berg07346f812008-05-03 01:02:02 +0200697 spin_unlock_bh(&sta->lock);
Thomas Pedersen54ef6562011-08-11 19:35:12 -0700698 mesh_plink_frame_tx(sdata,
699 WLAN_SP_MESH_PEERING_OPEN,
700 sta->sta.addr, llid, 0, 0);
701 mesh_plink_frame_tx(sdata,
702 WLAN_SP_MESH_PEERING_CONFIRM,
703 sta->sta.addr, llid, plid, 0);
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100704 break;
705 default:
Johannes Berg07346f812008-05-03 01:02:02 +0200706 spin_unlock_bh(&sta->lock);
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100707 break;
708 }
709 break;
710
Javier Cardona57cf8042011-05-13 10:45:43 -0700711 case NL80211_PLINK_OPN_SNT:
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100712 switch (event) {
713 case OPN_RJCT:
714 case CNF_RJCT:
Thomas Pedersen54ef6562011-08-11 19:35:12 -0700715 reason = cpu_to_le16(WLAN_REASON_MESH_CONFIG);
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100716 case CLS_ACPT:
717 if (!reason)
Thomas Pedersen54ef6562011-08-11 19:35:12 -0700718 reason = cpu_to_le16(WLAN_REASON_MESH_CLOSE);
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100719 sta->reason = reason;
Javier Cardona57cf8042011-05-13 10:45:43 -0700720 sta->plink_state = NL80211_PLINK_HOLDING;
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100721 if (!mod_plink_timer(sta,
722 dot11MeshHoldingTimeout(sdata)))
723 sta->ignore_plink_timer = true;
724
725 llid = sta->llid;
Johannes Berg07346f812008-05-03 01:02:02 +0200726 spin_unlock_bh(&sta->lock);
Thomas Pedersen54ef6562011-08-11 19:35:12 -0700727 mesh_plink_frame_tx(sdata,
728 WLAN_SP_MESH_PEERING_CLOSE,
729 sta->sta.addr, llid, plid, reason);
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100730 break;
731 case OPN_ACPT:
732 /* retry timer is left untouched */
Javier Cardona57cf8042011-05-13 10:45:43 -0700733 sta->plink_state = NL80211_PLINK_OPN_RCVD;
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100734 sta->plid = plid;
735 llid = sta->llid;
Johannes Berg07346f812008-05-03 01:02:02 +0200736 spin_unlock_bh(&sta->lock);
Thomas Pedersen54ef6562011-08-11 19:35:12 -0700737 mesh_plink_frame_tx(sdata,
738 WLAN_SP_MESH_PEERING_CONFIRM,
739 sta->sta.addr, llid, plid, 0);
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100740 break;
741 case CNF_ACPT:
Javier Cardona57cf8042011-05-13 10:45:43 -0700742 sta->plink_state = NL80211_PLINK_CNF_RCVD;
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100743 if (!mod_plink_timer(sta,
744 dot11MeshConfirmTimeout(sdata)))
745 sta->ignore_plink_timer = true;
746
Johannes Berg07346f812008-05-03 01:02:02 +0200747 spin_unlock_bh(&sta->lock);
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100748 break;
749 default:
Johannes Berg07346f812008-05-03 01:02:02 +0200750 spin_unlock_bh(&sta->lock);
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100751 break;
752 }
753 break;
754
Javier Cardona57cf8042011-05-13 10:45:43 -0700755 case NL80211_PLINK_OPN_RCVD:
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100756 switch (event) {
757 case OPN_RJCT:
758 case CNF_RJCT:
Thomas Pedersen54ef6562011-08-11 19:35:12 -0700759 reason = cpu_to_le16(WLAN_REASON_MESH_CONFIG);
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100760 case CLS_ACPT:
761 if (!reason)
Thomas Pedersen54ef6562011-08-11 19:35:12 -0700762 reason = cpu_to_le16(WLAN_REASON_MESH_CLOSE);
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100763 sta->reason = reason;
Javier Cardona57cf8042011-05-13 10:45:43 -0700764 sta->plink_state = NL80211_PLINK_HOLDING;
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100765 if (!mod_plink_timer(sta,
766 dot11MeshHoldingTimeout(sdata)))
767 sta->ignore_plink_timer = true;
768
769 llid = sta->llid;
Johannes Berg07346f812008-05-03 01:02:02 +0200770 spin_unlock_bh(&sta->lock);
Thomas Pedersen54ef6562011-08-11 19:35:12 -0700771 mesh_plink_frame_tx(sdata, WLAN_SP_MESH_PEERING_CLOSE,
772 sta->sta.addr, llid, plid, reason);
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100773 break;
774 case OPN_ACPT:
775 llid = sta->llid;
Johannes Berg07346f812008-05-03 01:02:02 +0200776 spin_unlock_bh(&sta->lock);
Thomas Pedersen54ef6562011-08-11 19:35:12 -0700777 mesh_plink_frame_tx(sdata,
778 WLAN_SP_MESH_PEERING_CONFIRM,
779 sta->sta.addr, llid, plid, 0);
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100780 break;
781 case CNF_ACPT:
Johannes Bergd0709a62008-02-25 16:27:46 +0100782 del_timer(&sta->plink_timer);
Javier Cardona57cf8042011-05-13 10:45:43 -0700783 sta->plink_state = NL80211_PLINK_ESTAB;
Johannes Berg07346f812008-05-03 01:02:02 +0200784 spin_unlock_bh(&sta->lock);
John W. Linvillec9370192010-06-21 17:14:07 -0400785 mesh_plink_inc_estab_count(sdata);
786 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BEACON);
Johannes Berg0c68ae262008-10-27 15:56:10 -0700787 mpl_dbg("Mesh plink with %pM ESTABLISHED\n",
788 sta->sta.addr);
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100789 break;
790 default:
Johannes Berg07346f812008-05-03 01:02:02 +0200791 spin_unlock_bh(&sta->lock);
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100792 break;
793 }
794 break;
795
Javier Cardona57cf8042011-05-13 10:45:43 -0700796 case NL80211_PLINK_CNF_RCVD:
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100797 switch (event) {
798 case OPN_RJCT:
799 case CNF_RJCT:
Thomas Pedersen54ef6562011-08-11 19:35:12 -0700800 reason = cpu_to_le16(WLAN_REASON_MESH_CONFIG);
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100801 case CLS_ACPT:
802 if (!reason)
Thomas Pedersen54ef6562011-08-11 19:35:12 -0700803 reason = cpu_to_le16(WLAN_REASON_MESH_CLOSE);
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100804 sta->reason = reason;
Javier Cardona57cf8042011-05-13 10:45:43 -0700805 sta->plink_state = NL80211_PLINK_HOLDING;
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100806 if (!mod_plink_timer(sta,
807 dot11MeshHoldingTimeout(sdata)))
808 sta->ignore_plink_timer = true;
809
810 llid = sta->llid;
Johannes Berg07346f812008-05-03 01:02:02 +0200811 spin_unlock_bh(&sta->lock);
Thomas Pedersen54ef6562011-08-11 19:35:12 -0700812 mesh_plink_frame_tx(sdata,
813 WLAN_SP_MESH_PEERING_CLOSE,
814 sta->sta.addr, llid, plid, reason);
Johannes Bergff59dc72008-02-25 10:11:50 +0100815 break;
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100816 case OPN_ACPT:
Johannes Bergd0709a62008-02-25 16:27:46 +0100817 del_timer(&sta->plink_timer);
Javier Cardona57cf8042011-05-13 10:45:43 -0700818 sta->plink_state = NL80211_PLINK_ESTAB;
Johannes Berg07346f812008-05-03 01:02:02 +0200819 spin_unlock_bh(&sta->lock);
John W. Linvillec9370192010-06-21 17:14:07 -0400820 mesh_plink_inc_estab_count(sdata);
821 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BEACON);
Johannes Berg0c68ae262008-10-27 15:56:10 -0700822 mpl_dbg("Mesh plink with %pM ESTABLISHED\n",
823 sta->sta.addr);
Thomas Pedersen54ef6562011-08-11 19:35:12 -0700824 mesh_plink_frame_tx(sdata,
825 WLAN_SP_MESH_PEERING_CONFIRM,
826 sta->sta.addr, llid, plid, 0);
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100827 break;
828 default:
Johannes Berg07346f812008-05-03 01:02:02 +0200829 spin_unlock_bh(&sta->lock);
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100830 break;
831 }
832 break;
833
Javier Cardona57cf8042011-05-13 10:45:43 -0700834 case NL80211_PLINK_ESTAB:
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100835 switch (event) {
836 case CLS_ACPT:
Thomas Pedersen54ef6562011-08-11 19:35:12 -0700837 reason = cpu_to_le16(WLAN_REASON_MESH_CLOSE);
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100838 sta->reason = reason;
John W. Linvillec9370192010-06-21 17:14:07 -0400839 deactivated = __mesh_plink_deactivate(sta);
Javier Cardona57cf8042011-05-13 10:45:43 -0700840 sta->plink_state = NL80211_PLINK_HOLDING;
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100841 llid = sta->llid;
Johannes Bergd0709a62008-02-25 16:27:46 +0100842 mod_plink_timer(sta, dot11MeshHoldingTimeout(sdata));
Johannes Berg07346f812008-05-03 01:02:02 +0200843 spin_unlock_bh(&sta->lock);
John W. Linvillec9370192010-06-21 17:14:07 -0400844 if (deactivated)
845 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BEACON);
Thomas Pedersen54ef6562011-08-11 19:35:12 -0700846 mesh_plink_frame_tx(sdata, WLAN_SP_MESH_PEERING_CLOSE,
847 sta->sta.addr, llid, plid, reason);
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100848 break;
849 case OPN_ACPT:
850 llid = sta->llid;
Johannes Berg07346f812008-05-03 01:02:02 +0200851 spin_unlock_bh(&sta->lock);
Thomas Pedersen54ef6562011-08-11 19:35:12 -0700852 mesh_plink_frame_tx(sdata,
853 WLAN_SP_MESH_PEERING_CONFIRM,
854 sta->sta.addr, llid, plid, 0);
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100855 break;
856 default:
Johannes Berg07346f812008-05-03 01:02:02 +0200857 spin_unlock_bh(&sta->lock);
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100858 break;
859 }
860 break;
Javier Cardona57cf8042011-05-13 10:45:43 -0700861 case NL80211_PLINK_HOLDING:
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100862 switch (event) {
863 case CLS_ACPT:
Johannes Bergd0709a62008-02-25 16:27:46 +0100864 if (del_timer(&sta->plink_timer))
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100865 sta->ignore_plink_timer = 1;
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100866 mesh_plink_fsm_restart(sta);
Johannes Berg07346f812008-05-03 01:02:02 +0200867 spin_unlock_bh(&sta->lock);
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100868 break;
869 case OPN_ACPT:
870 case CNF_ACPT:
871 case OPN_RJCT:
872 case CNF_RJCT:
873 llid = sta->llid;
874 reason = sta->reason;
Johannes Berg07346f812008-05-03 01:02:02 +0200875 spin_unlock_bh(&sta->lock);
Thomas Pedersen54ef6562011-08-11 19:35:12 -0700876 mesh_plink_frame_tx(sdata, WLAN_SP_MESH_PEERING_CLOSE,
877 sta->sta.addr, llid, plid, reason);
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100878 break;
879 default:
Johannes Berg07346f812008-05-03 01:02:02 +0200880 spin_unlock_bh(&sta->lock);
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100881 }
882 break;
883 default:
Luis Carlos Cobob4e08ea2008-02-29 15:46:08 -0800884 /* should not get here, PLINK_BLOCKED is dealt with at the
Daniel Mack3ad2f3f2010-02-03 08:01:28 +0800885 * beginning of the function
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100886 */
Johannes Berg07346f812008-05-03 01:02:02 +0200887 spin_unlock_bh(&sta->lock);
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100888 break;
889 }
Johannes Bergd0709a62008-02-25 16:27:46 +0100890
891 rcu_read_unlock();
Luis Carlos Coboc3896d2c2008-02-23 15:17:13 +0100892}