mesh_plink.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094
  1. /*
  2. * Copyright (c) 2008, 2009 open80211s Ltd.
  3. * Author: Luis Carlos Cobo <luisca@cozybit.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. */
  9. #include <linux/gfp.h>
  10. #include <linux/kernel.h>
  11. #include <linux/random.h>
  12. #include "ieee80211_i.h"
  13. #include "rate.h"
  14. #include "mesh.h"
  15. #define PLINK_GET_LLID(p) (p + 2)
  16. #define PLINK_GET_PLID(p) (p + 4)
  17. #define mod_plink_timer(s, t) (mod_timer(&s->plink_timer, \
  18. jiffies + HZ * t / 1000))
  19. /* We only need a valid sta if user configured a minimum rssi_threshold. */
  20. #define rssi_threshold_check(sta, sdata) \
  21. (sdata->u.mesh.mshcfg.rssi_threshold == 0 ||\
  22. (sta && (s8) -ewma_read(&sta->avg_signal) > \
  23. sdata->u.mesh.mshcfg.rssi_threshold))
  24. enum plink_event {
  25. PLINK_UNDEFINED,
  26. OPN_ACPT,
  27. OPN_RJCT,
  28. OPN_IGNR,
  29. CNF_ACPT,
  30. CNF_RJCT,
  31. CNF_IGNR,
  32. CLS_ACPT,
  33. CLS_IGNR
  34. };
  35. static const char * const mplstates[] = {
  36. [NL80211_PLINK_LISTEN] = "LISTEN",
  37. [NL80211_PLINK_OPN_SNT] = "OPN-SNT",
  38. [NL80211_PLINK_OPN_RCVD] = "OPN-RCVD",
  39. [NL80211_PLINK_CNF_RCVD] = "CNF_RCVD",
  40. [NL80211_PLINK_ESTAB] = "ESTAB",
  41. [NL80211_PLINK_HOLDING] = "HOLDING",
  42. [NL80211_PLINK_BLOCKED] = "BLOCKED"
  43. };
  44. static const char * const mplevents[] = {
  45. [PLINK_UNDEFINED] = "NONE",
  46. [OPN_ACPT] = "OPN_ACPT",
  47. [OPN_RJCT] = "OPN_RJCT",
  48. [OPN_IGNR] = "OPN_IGNR",
  49. [CNF_ACPT] = "CNF_ACPT",
  50. [CNF_RJCT] = "CNF_RJCT",
  51. [CNF_IGNR] = "CNF_IGNR",
  52. [CLS_ACPT] = "CLS_ACPT",
  53. [CLS_IGNR] = "CLS_IGNR"
  54. };
  55. static int mesh_plink_frame_tx(struct ieee80211_sub_if_data *sdata,
  56. enum ieee80211_self_protected_actioncode action,
  57. u8 *da, __le16 llid, __le16 plid, __le16 reason);
  58. /**
  59. * mesh_plink_fsm_restart - restart a mesh peer link finite state machine
  60. *
  61. * @sta: mesh peer link to restart
  62. *
  63. * Locking: this function must be called holding sta->lock
  64. */
  65. static inline void mesh_plink_fsm_restart(struct sta_info *sta)
  66. {
  67. sta->plink_state = NL80211_PLINK_LISTEN;
  68. sta->llid = sta->plid = sta->reason = 0;
  69. sta->plink_retries = 0;
  70. }
  71. /*
  72. * mesh_set_short_slot_time - enable / disable ERP short slot time.
  73. *
  74. * The standard indirectly mandates mesh STAs to turn off short slot time by
  75. * disallowing advertising this (802.11-2012 8.4.1.4), but that doesn't mean we
  76. * can't be sneaky about it. Enable short slot time if all mesh STAs in the
  77. * MBSS support ERP rates.
  78. *
  79. * Returns BSS_CHANGED_ERP_SLOT or 0 for no change.
  80. */
  81. static u32 mesh_set_short_slot_time(struct ieee80211_sub_if_data *sdata)
  82. {
  83. struct ieee80211_local *local = sdata->local;
  84. enum ieee80211_band band = ieee80211_get_sdata_band(sdata);
  85. struct ieee80211_supported_band *sband = local->hw.wiphy->bands[band];
  86. struct sta_info *sta;
  87. u32 erp_rates = 0, changed = 0;
  88. int i;
  89. bool short_slot = false;
  90. if (band == IEEE80211_BAND_5GHZ) {
  91. /* (IEEE 802.11-2012 19.4.5) */
  92. short_slot = true;
  93. goto out;
  94. } else if (band != IEEE80211_BAND_2GHZ ||
  95. (band == IEEE80211_BAND_2GHZ &&
  96. local->hw.flags & IEEE80211_HW_2GHZ_SHORT_SLOT_INCAPABLE))
  97. goto out;
  98. for (i = 0; i < sband->n_bitrates; i++)
  99. if (sband->bitrates[i].flags & IEEE80211_RATE_ERP_G)
  100. erp_rates |= BIT(i);
  101. if (!erp_rates)
  102. goto out;
  103. rcu_read_lock();
  104. list_for_each_entry_rcu(sta, &local->sta_list, list) {
  105. if (sdata != sta->sdata ||
  106. sta->plink_state != NL80211_PLINK_ESTAB)
  107. continue;
  108. short_slot = false;
  109. if (erp_rates & sta->sta.supp_rates[band])
  110. short_slot = true;
  111. else
  112. break;
  113. }
  114. rcu_read_unlock();
  115. out:
  116. if (sdata->vif.bss_conf.use_short_slot != short_slot) {
  117. sdata->vif.bss_conf.use_short_slot = short_slot;
  118. changed = BSS_CHANGED_ERP_SLOT;
  119. mpl_dbg(sdata, "mesh_plink %pM: ERP short slot time %d\n",
  120. sdata->vif.addr, short_slot);
  121. }
  122. return changed;
  123. }
  124. /**
  125. * mesh_set_ht_prot_mode - set correct HT protection mode
  126. *
  127. * Section 9.23.3.5 of IEEE 80211-2012 describes the protection rules for HT
  128. * mesh STA in a MBSS. Three HT protection modes are supported for now, non-HT
  129. * mixed mode, 20MHz-protection and no-protection mode. non-HT mixed mode is
  130. * selected if any non-HT peers are present in our MBSS. 20MHz-protection mode
  131. * is selected if all peers in our 20/40MHz MBSS support HT and atleast one
  132. * HT20 peer is present. Otherwise no-protection mode is selected.
  133. */
  134. static u32 mesh_set_ht_prot_mode(struct ieee80211_sub_if_data *sdata)
  135. {
  136. struct ieee80211_local *local = sdata->local;
  137. struct sta_info *sta;
  138. u16 ht_opmode;
  139. bool non_ht_sta = false, ht20_sta = false;
  140. if (sdata->vif.bss_conf.chandef.width == NL80211_CHAN_WIDTH_20_NOHT)
  141. return 0;
  142. rcu_read_lock();
  143. list_for_each_entry_rcu(sta, &local->sta_list, list) {
  144. if (sdata != sta->sdata ||
  145. sta->plink_state != NL80211_PLINK_ESTAB)
  146. continue;
  147. if (sta->sta.bandwidth > IEEE80211_STA_RX_BW_20)
  148. continue;
  149. if (!sta->sta.ht_cap.ht_supported) {
  150. mpl_dbg(sdata, "nonHT sta (%pM) is present\n",
  151. sta->sta.addr);
  152. non_ht_sta = true;
  153. break;
  154. }
  155. mpl_dbg(sdata, "HT20 sta (%pM) is present\n", sta->sta.addr);
  156. ht20_sta = true;
  157. }
  158. rcu_read_unlock();
  159. if (non_ht_sta)
  160. ht_opmode = IEEE80211_HT_OP_MODE_PROTECTION_NONHT_MIXED;
  161. else if (ht20_sta &&
  162. sdata->vif.bss_conf.chandef.width > NL80211_CHAN_WIDTH_20)
  163. ht_opmode = IEEE80211_HT_OP_MODE_PROTECTION_20MHZ;
  164. else
  165. ht_opmode = IEEE80211_HT_OP_MODE_PROTECTION_NONE;
  166. if (sdata->vif.bss_conf.ht_operation_mode == ht_opmode)
  167. return 0;
  168. sdata->vif.bss_conf.ht_operation_mode = ht_opmode;
  169. sdata->u.mesh.mshcfg.ht_opmode = ht_opmode;
  170. mpl_dbg(sdata, "selected new HT protection mode %d\n", ht_opmode);
  171. return BSS_CHANGED_HT;
  172. }
  173. /**
  174. * __mesh_plink_deactivate - deactivate mesh peer link
  175. *
  176. * @sta: mesh peer link to deactivate
  177. *
  178. * All mesh paths with this peer as next hop will be flushed
  179. * Returns beacon changed flag if the beacon content changed.
  180. *
  181. * Locking: the caller must hold sta->lock
  182. */
  183. static u32 __mesh_plink_deactivate(struct sta_info *sta)
  184. {
  185. struct ieee80211_sub_if_data *sdata = sta->sdata;
  186. u32 changed = 0;
  187. if (sta->plink_state == NL80211_PLINK_ESTAB)
  188. changed = mesh_plink_dec_estab_count(sdata);
  189. sta->plink_state = NL80211_PLINK_BLOCKED;
  190. mesh_path_flush_by_nexthop(sta);
  191. ieee80211_mps_sta_status_update(sta);
  192. changed |= ieee80211_mps_local_status_update(sdata);
  193. return changed;
  194. }
  195. /**
  196. * mesh_plink_deactivate - deactivate mesh peer link
  197. *
  198. * @sta: mesh peer link to deactivate
  199. *
  200. * All mesh paths with this peer as next hop will be flushed
  201. */
  202. u32 mesh_plink_deactivate(struct sta_info *sta)
  203. {
  204. struct ieee80211_sub_if_data *sdata = sta->sdata;
  205. u32 changed;
  206. spin_lock_bh(&sta->lock);
  207. changed = __mesh_plink_deactivate(sta);
  208. sta->reason = cpu_to_le16(WLAN_REASON_MESH_PEER_CANCELED);
  209. mesh_plink_frame_tx(sdata, WLAN_SP_MESH_PEERING_CLOSE,
  210. sta->sta.addr, sta->llid, sta->plid,
  211. sta->reason);
  212. spin_unlock_bh(&sta->lock);
  213. return changed;
  214. }
  215. static int mesh_plink_frame_tx(struct ieee80211_sub_if_data *sdata,
  216. enum ieee80211_self_protected_actioncode action,
  217. u8 *da, __le16 llid, __le16 plid, __le16 reason)
  218. {
  219. struct ieee80211_local *local = sdata->local;
  220. struct sk_buff *skb;
  221. struct ieee80211_tx_info *info;
  222. struct ieee80211_mgmt *mgmt;
  223. bool include_plid = false;
  224. u16 peering_proto = 0;
  225. u8 *pos, ie_len = 4;
  226. int hdr_len = offsetof(struct ieee80211_mgmt, u.action.u.self_prot) +
  227. sizeof(mgmt->u.action.u.self_prot);
  228. int err = -ENOMEM;
  229. skb = dev_alloc_skb(local->tx_headroom +
  230. hdr_len +
  231. 2 + /* capability info */
  232. 2 + /* AID */
  233. 2 + 8 + /* supported rates */
  234. 2 + (IEEE80211_MAX_SUPP_RATES - 8) +
  235. 2 + sdata->u.mesh.mesh_id_len +
  236. 2 + sizeof(struct ieee80211_meshconf_ie) +
  237. 2 + sizeof(struct ieee80211_ht_cap) +
  238. 2 + sizeof(struct ieee80211_ht_operation) +
  239. 2 + 8 + /* peering IE */
  240. sdata->u.mesh.ie_len);
  241. if (!skb)
  242. return -1;
  243. info = IEEE80211_SKB_CB(skb);
  244. skb_reserve(skb, local->tx_headroom);
  245. mgmt = (struct ieee80211_mgmt *) skb_put(skb, hdr_len);
  246. memset(mgmt, 0, hdr_len);
  247. mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
  248. IEEE80211_STYPE_ACTION);
  249. memcpy(mgmt->da, da, ETH_ALEN);
  250. memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
  251. memcpy(mgmt->bssid, sdata->vif.addr, ETH_ALEN);
  252. mgmt->u.action.category = WLAN_CATEGORY_SELF_PROTECTED;
  253. mgmt->u.action.u.self_prot.action_code = action;
  254. if (action != WLAN_SP_MESH_PEERING_CLOSE) {
  255. enum ieee80211_band band = ieee80211_get_sdata_band(sdata);
  256. /* capability info */
  257. pos = skb_put(skb, 2);
  258. memset(pos, 0, 2);
  259. if (action == WLAN_SP_MESH_PEERING_CONFIRM) {
  260. /* AID */
  261. pos = skb_put(skb, 2);
  262. memcpy(pos + 2, &plid, 2);
  263. }
  264. if (ieee80211_add_srates_ie(sdata, skb, true, band) ||
  265. ieee80211_add_ext_srates_ie(sdata, skb, true, band) ||
  266. mesh_add_rsn_ie(sdata, skb) ||
  267. mesh_add_meshid_ie(sdata, skb) ||
  268. mesh_add_meshconf_ie(sdata, skb))
  269. goto free;
  270. } else { /* WLAN_SP_MESH_PEERING_CLOSE */
  271. info->flags |= IEEE80211_TX_CTL_NO_ACK;
  272. if (mesh_add_meshid_ie(sdata, skb))
  273. goto free;
  274. }
  275. /* Add Mesh Peering Management element */
  276. switch (action) {
  277. case WLAN_SP_MESH_PEERING_OPEN:
  278. break;
  279. case WLAN_SP_MESH_PEERING_CONFIRM:
  280. ie_len += 2;
  281. include_plid = true;
  282. break;
  283. case WLAN_SP_MESH_PEERING_CLOSE:
  284. if (plid) {
  285. ie_len += 2;
  286. include_plid = true;
  287. }
  288. ie_len += 2; /* reason code */
  289. break;
  290. default:
  291. err = -EINVAL;
  292. goto free;
  293. }
  294. if (WARN_ON(skb_tailroom(skb) < 2 + ie_len))
  295. goto free;
  296. pos = skb_put(skb, 2 + ie_len);
  297. *pos++ = WLAN_EID_PEER_MGMT;
  298. *pos++ = ie_len;
  299. memcpy(pos, &peering_proto, 2);
  300. pos += 2;
  301. memcpy(pos, &llid, 2);
  302. pos += 2;
  303. if (include_plid) {
  304. memcpy(pos, &plid, 2);
  305. pos += 2;
  306. }
  307. if (action == WLAN_SP_MESH_PEERING_CLOSE) {
  308. memcpy(pos, &reason, 2);
  309. pos += 2;
  310. }
  311. if (action != WLAN_SP_MESH_PEERING_CLOSE) {
  312. if (mesh_add_ht_cap_ie(sdata, skb) ||
  313. mesh_add_ht_oper_ie(sdata, skb))
  314. goto free;
  315. }
  316. if (mesh_add_vendor_ies(sdata, skb))
  317. goto free;
  318. ieee80211_tx_skb(sdata, skb);
  319. return 0;
  320. free:
  321. kfree_skb(skb);
  322. return err;
  323. }
  324. static void mesh_sta_info_init(struct ieee80211_sub_if_data *sdata,
  325. struct sta_info *sta,
  326. struct ieee802_11_elems *elems, bool insert)
  327. {
  328. struct ieee80211_local *local = sdata->local;
  329. enum ieee80211_band band = ieee80211_get_sdata_band(sdata);
  330. struct ieee80211_supported_band *sband;
  331. u32 rates, basic_rates = 0, changed = 0;
  332. sband = local->hw.wiphy->bands[band];
  333. rates = ieee80211_sta_get_rates(local, elems, band, &basic_rates);
  334. spin_lock_bh(&sta->lock);
  335. sta->last_rx = jiffies;
  336. /* rates and capabilities don't change during peering */
  337. if (sta->plink_state == NL80211_PLINK_ESTAB)
  338. goto out;
  339. if (sta->sta.supp_rates[band] != rates)
  340. changed |= IEEE80211_RC_SUPP_RATES_CHANGED;
  341. sta->sta.supp_rates[band] = rates;
  342. if (ieee80211_ht_cap_ie_to_sta_ht_cap(sdata, sband,
  343. elems->ht_cap_elem, sta))
  344. changed |= IEEE80211_RC_BW_CHANGED;
  345. /* HT peer is operating 20MHz-only */
  346. if (elems->ht_operation &&
  347. !(elems->ht_operation->ht_param &
  348. IEEE80211_HT_PARAM_CHAN_WIDTH_ANY)) {
  349. if (sta->sta.bandwidth != IEEE80211_STA_RX_BW_20)
  350. changed |= IEEE80211_RC_BW_CHANGED;
  351. sta->sta.bandwidth = IEEE80211_STA_RX_BW_20;
  352. }
  353. if (insert)
  354. rate_control_rate_init(sta);
  355. else
  356. rate_control_rate_update(local, sband, sta, changed);
  357. out:
  358. spin_unlock_bh(&sta->lock);
  359. }
  360. static struct sta_info *
  361. __mesh_sta_info_alloc(struct ieee80211_sub_if_data *sdata, u8 *hw_addr)
  362. {
  363. struct sta_info *sta;
  364. if (sdata->local->num_sta >= MESH_MAX_PLINKS)
  365. return NULL;
  366. sta = sta_info_alloc(sdata, hw_addr, GFP_KERNEL);
  367. if (!sta)
  368. return NULL;
  369. sta->plink_state = NL80211_PLINK_LISTEN;
  370. init_timer(&sta->plink_timer);
  371. sta_info_pre_move_state(sta, IEEE80211_STA_AUTH);
  372. sta_info_pre_move_state(sta, IEEE80211_STA_ASSOC);
  373. sta_info_pre_move_state(sta, IEEE80211_STA_AUTHORIZED);
  374. set_sta_flag(sta, WLAN_STA_WME);
  375. return sta;
  376. }
  377. static struct sta_info *
  378. mesh_sta_info_alloc(struct ieee80211_sub_if_data *sdata, u8 *addr,
  379. struct ieee802_11_elems *elems)
  380. {
  381. struct sta_info *sta = NULL;
  382. /* Userspace handles peer allocation when security is enabled */
  383. if (sdata->u.mesh.security & IEEE80211_MESH_SEC_AUTHED)
  384. cfg80211_notify_new_peer_candidate(sdata->dev, addr,
  385. elems->ie_start,
  386. elems->total_len,
  387. GFP_KERNEL);
  388. else
  389. sta = __mesh_sta_info_alloc(sdata, addr);
  390. return sta;
  391. }
  392. /*
  393. * mesh_sta_info_get - return mesh sta info entry for @addr.
  394. *
  395. * @sdata: local meshif
  396. * @addr: peer's address
  397. * @elems: IEs from beacon or mesh peering frame.
  398. *
  399. * Return existing or newly allocated sta_info under RCU read lock.
  400. * (re)initialize with given IEs.
  401. */
  402. static struct sta_info *
  403. mesh_sta_info_get(struct ieee80211_sub_if_data *sdata,
  404. u8 *addr, struct ieee802_11_elems *elems) __acquires(RCU)
  405. {
  406. struct sta_info *sta = NULL;
  407. rcu_read_lock();
  408. sta = sta_info_get(sdata, addr);
  409. if (sta) {
  410. mesh_sta_info_init(sdata, sta, elems, false);
  411. } else {
  412. rcu_read_unlock();
  413. /* can't run atomic */
  414. sta = mesh_sta_info_alloc(sdata, addr, elems);
  415. if (!sta) {
  416. rcu_read_lock();
  417. return NULL;
  418. }
  419. mesh_sta_info_init(sdata, sta, elems, true);
  420. if (sta_info_insert_rcu(sta))
  421. return NULL;
  422. }
  423. return sta;
  424. }
  425. /*
  426. * mesh_neighbour_update - update or initialize new mesh neighbor.
  427. *
  428. * @sdata: local meshif
  429. * @addr: peer's address
  430. * @elems: IEs from beacon or mesh peering frame
  431. *
  432. * Initiates peering if appropriate.
  433. */
  434. void mesh_neighbour_update(struct ieee80211_sub_if_data *sdata,
  435. u8 *hw_addr,
  436. struct ieee802_11_elems *elems)
  437. {
  438. struct sta_info *sta;
  439. u32 changed = 0;
  440. sta = mesh_sta_info_get(sdata, hw_addr, elems);
  441. if (!sta)
  442. goto out;
  443. if (mesh_peer_accepts_plinks(elems) &&
  444. sta->plink_state == NL80211_PLINK_LISTEN &&
  445. sdata->u.mesh.accepting_plinks &&
  446. sdata->u.mesh.mshcfg.auto_open_plinks &&
  447. rssi_threshold_check(sta, sdata))
  448. changed = mesh_plink_open(sta);
  449. ieee80211_mps_frame_release(sta, elems);
  450. out:
  451. rcu_read_unlock();
  452. ieee80211_mbss_info_change_notify(sdata, changed);
  453. }
  454. static void mesh_plink_timer(unsigned long data)
  455. {
  456. struct sta_info *sta;
  457. __le16 llid, plid, reason;
  458. struct ieee80211_sub_if_data *sdata;
  459. struct mesh_config *mshcfg;
  460. /*
  461. * This STA is valid because sta_info_destroy() will
  462. * del_timer_sync() this timer after having made sure
  463. * it cannot be readded (by deleting the plink.)
  464. */
  465. sta = (struct sta_info *) data;
  466. if (sta->sdata->local->quiescing) {
  467. sta->plink_timer_was_running = true;
  468. return;
  469. }
  470. spin_lock_bh(&sta->lock);
  471. if (sta->ignore_plink_timer) {
  472. sta->ignore_plink_timer = false;
  473. spin_unlock_bh(&sta->lock);
  474. return;
  475. }
  476. mpl_dbg(sta->sdata,
  477. "Mesh plink timer for %pM fired on state %d\n",
  478. sta->sta.addr, sta->plink_state);
  479. reason = 0;
  480. llid = sta->llid;
  481. plid = sta->plid;
  482. sdata = sta->sdata;
  483. mshcfg = &sdata->u.mesh.mshcfg;
  484. switch (sta->plink_state) {
  485. case NL80211_PLINK_OPN_RCVD:
  486. case NL80211_PLINK_OPN_SNT:
  487. /* retry timer */
  488. if (sta->plink_retries < mshcfg->dot11MeshMaxRetries) {
  489. u32 rand;
  490. mpl_dbg(sta->sdata,
  491. "Mesh plink for %pM (retry, timeout): %d %d\n",
  492. sta->sta.addr, sta->plink_retries,
  493. sta->plink_timeout);
  494. get_random_bytes(&rand, sizeof(u32));
  495. sta->plink_timeout = sta->plink_timeout +
  496. rand % sta->plink_timeout;
  497. ++sta->plink_retries;
  498. mod_plink_timer(sta, sta->plink_timeout);
  499. spin_unlock_bh(&sta->lock);
  500. mesh_plink_frame_tx(sdata, WLAN_SP_MESH_PEERING_OPEN,
  501. sta->sta.addr, llid, 0, 0);
  502. break;
  503. }
  504. reason = cpu_to_le16(WLAN_REASON_MESH_MAX_RETRIES);
  505. /* fall through on else */
  506. case NL80211_PLINK_CNF_RCVD:
  507. /* confirm timer */
  508. if (!reason)
  509. reason = cpu_to_le16(WLAN_REASON_MESH_CONFIRM_TIMEOUT);
  510. sta->plink_state = NL80211_PLINK_HOLDING;
  511. mod_plink_timer(sta, mshcfg->dot11MeshHoldingTimeout);
  512. spin_unlock_bh(&sta->lock);
  513. mesh_plink_frame_tx(sdata, WLAN_SP_MESH_PEERING_CLOSE,
  514. sta->sta.addr, llid, plid, reason);
  515. break;
  516. case NL80211_PLINK_HOLDING:
  517. /* holding timer */
  518. del_timer(&sta->plink_timer);
  519. mesh_plink_fsm_restart(sta);
  520. spin_unlock_bh(&sta->lock);
  521. break;
  522. default:
  523. spin_unlock_bh(&sta->lock);
  524. break;
  525. }
  526. }
  527. #ifdef CONFIG_PM
  528. void mesh_plink_quiesce(struct sta_info *sta)
  529. {
  530. if (!ieee80211_vif_is_mesh(&sta->sdata->vif))
  531. return;
  532. /* no kernel mesh sta timers have been initialized */
  533. if (sta->sdata->u.mesh.security != IEEE80211_MESH_SEC_NONE)
  534. return;
  535. if (del_timer_sync(&sta->plink_timer))
  536. sta->plink_timer_was_running = true;
  537. }
  538. void mesh_plink_restart(struct sta_info *sta)
  539. {
  540. if (sta->plink_timer_was_running) {
  541. add_timer(&sta->plink_timer);
  542. sta->plink_timer_was_running = false;
  543. }
  544. }
  545. #endif
  546. static inline void mesh_plink_timer_set(struct sta_info *sta, int timeout)
  547. {
  548. sta->plink_timer.expires = jiffies + (HZ * timeout / 1000);
  549. sta->plink_timer.data = (unsigned long) sta;
  550. sta->plink_timer.function = mesh_plink_timer;
  551. sta->plink_timeout = timeout;
  552. add_timer(&sta->plink_timer);
  553. }
  554. u32 mesh_plink_open(struct sta_info *sta)
  555. {
  556. __le16 llid;
  557. struct ieee80211_sub_if_data *sdata = sta->sdata;
  558. u32 changed;
  559. if (!test_sta_flag(sta, WLAN_STA_AUTH))
  560. return 0;
  561. spin_lock_bh(&sta->lock);
  562. get_random_bytes(&llid, 2);
  563. sta->llid = llid;
  564. if (sta->plink_state != NL80211_PLINK_LISTEN &&
  565. sta->plink_state != NL80211_PLINK_BLOCKED) {
  566. spin_unlock_bh(&sta->lock);
  567. return 0;
  568. }
  569. sta->plink_state = NL80211_PLINK_OPN_SNT;
  570. mesh_plink_timer_set(sta, sdata->u.mesh.mshcfg.dot11MeshRetryTimeout);
  571. spin_unlock_bh(&sta->lock);
  572. mpl_dbg(sdata,
  573. "Mesh plink: starting establishment with %pM\n",
  574. sta->sta.addr);
  575. /* set the non-peer mode to active during peering */
  576. changed = ieee80211_mps_local_status_update(sdata);
  577. mesh_plink_frame_tx(sdata, WLAN_SP_MESH_PEERING_OPEN,
  578. sta->sta.addr, llid, 0, 0);
  579. return changed;
  580. }
  581. u32 mesh_plink_block(struct sta_info *sta)
  582. {
  583. u32 changed;
  584. spin_lock_bh(&sta->lock);
  585. changed = __mesh_plink_deactivate(sta);
  586. sta->plink_state = NL80211_PLINK_BLOCKED;
  587. spin_unlock_bh(&sta->lock);
  588. return changed;
  589. }
  590. void mesh_rx_plink_frame(struct ieee80211_sub_if_data *sdata,
  591. struct ieee80211_mgmt *mgmt, size_t len,
  592. struct ieee80211_rx_status *rx_status)
  593. {
  594. struct mesh_config *mshcfg = &sdata->u.mesh.mshcfg;
  595. struct ieee802_11_elems elems;
  596. struct sta_info *sta;
  597. enum plink_event event;
  598. enum ieee80211_self_protected_actioncode ftype;
  599. size_t baselen;
  600. bool matches_local = true;
  601. u8 ie_len;
  602. u8 *baseaddr;
  603. u32 changed = 0;
  604. __le16 plid, llid, reason;
  605. /* need action_code, aux */
  606. if (len < IEEE80211_MIN_ACTION_SIZE + 3)
  607. return;
  608. if (is_multicast_ether_addr(mgmt->da)) {
  609. mpl_dbg(sdata,
  610. "Mesh plink: ignore frame from multicast address\n");
  611. return;
  612. }
  613. baseaddr = mgmt->u.action.u.self_prot.variable;
  614. baselen = (u8 *) mgmt->u.action.u.self_prot.variable - (u8 *) mgmt;
  615. if (mgmt->u.action.u.self_prot.action_code ==
  616. WLAN_SP_MESH_PEERING_CONFIRM) {
  617. baseaddr += 4;
  618. baselen += 4;
  619. }
  620. ieee802_11_parse_elems(baseaddr, len - baselen, &elems);
  621. if (!elems.peering) {
  622. mpl_dbg(sdata,
  623. "Mesh plink: missing necessary peer link ie\n");
  624. return;
  625. }
  626. if (elems.rsn_len &&
  627. sdata->u.mesh.security == IEEE80211_MESH_SEC_NONE) {
  628. mpl_dbg(sdata,
  629. "Mesh plink: can't establish link with secure peer\n");
  630. return;
  631. }
  632. ftype = mgmt->u.action.u.self_prot.action_code;
  633. ie_len = elems.peering_len;
  634. if ((ftype == WLAN_SP_MESH_PEERING_OPEN && ie_len != 4) ||
  635. (ftype == WLAN_SP_MESH_PEERING_CONFIRM && ie_len != 6) ||
  636. (ftype == WLAN_SP_MESH_PEERING_CLOSE && ie_len != 6
  637. && ie_len != 8)) {
  638. mpl_dbg(sdata,
  639. "Mesh plink: incorrect plink ie length %d %d\n",
  640. ftype, ie_len);
  641. return;
  642. }
  643. if (ftype != WLAN_SP_MESH_PEERING_CLOSE &&
  644. (!elems.mesh_id || !elems.mesh_config)) {
  645. mpl_dbg(sdata, "Mesh plink: missing necessary ie\n");
  646. return;
  647. }
  648. /* Note the lines below are correct, the llid in the frame is the plid
  649. * from the point of view of this host.
  650. */
  651. memcpy(&plid, PLINK_GET_LLID(elems.peering), 2);
  652. if (ftype == WLAN_SP_MESH_PEERING_CONFIRM ||
  653. (ftype == WLAN_SP_MESH_PEERING_CLOSE && ie_len == 8))
  654. memcpy(&llid, PLINK_GET_PLID(elems.peering), 2);
  655. /* WARNING: Only for sta pointer, is dropped & re-acquired */
  656. rcu_read_lock();
  657. sta = sta_info_get(sdata, mgmt->sa);
  658. if (!sta && ftype != WLAN_SP_MESH_PEERING_OPEN) {
  659. mpl_dbg(sdata, "Mesh plink: cls or cnf from unknown peer\n");
  660. rcu_read_unlock();
  661. return;
  662. }
  663. if (ftype == WLAN_SP_MESH_PEERING_OPEN &&
  664. !rssi_threshold_check(sta, sdata)) {
  665. mpl_dbg(sdata, "Mesh plink: %pM does not meet rssi threshold\n",
  666. mgmt->sa);
  667. rcu_read_unlock();
  668. return;
  669. }
  670. if (sta && !test_sta_flag(sta, WLAN_STA_AUTH)) {
  671. mpl_dbg(sdata, "Mesh plink: Action frame from non-authed peer\n");
  672. rcu_read_unlock();
  673. return;
  674. }
  675. if (sta && sta->plink_state == NL80211_PLINK_BLOCKED) {
  676. rcu_read_unlock();
  677. return;
  678. }
  679. /* Now we will figure out the appropriate event... */
  680. event = PLINK_UNDEFINED;
  681. if (ftype != WLAN_SP_MESH_PEERING_CLOSE &&
  682. !mesh_matches_local(sdata, &elems)) {
  683. matches_local = false;
  684. switch (ftype) {
  685. case WLAN_SP_MESH_PEERING_OPEN:
  686. event = OPN_RJCT;
  687. break;
  688. case WLAN_SP_MESH_PEERING_CONFIRM:
  689. event = CNF_RJCT;
  690. break;
  691. default:
  692. break;
  693. }
  694. }
  695. if (!sta && !matches_local) {
  696. rcu_read_unlock();
  697. reason = cpu_to_le16(WLAN_REASON_MESH_CONFIG);
  698. llid = 0;
  699. mesh_plink_frame_tx(sdata, WLAN_SP_MESH_PEERING_CLOSE,
  700. mgmt->sa, llid, plid, reason);
  701. return;
  702. } else if (!sta) {
  703. /* ftype == WLAN_SP_MESH_PEERING_OPEN */
  704. if (!mesh_plink_free_count(sdata)) {
  705. mpl_dbg(sdata, "Mesh plink error: no more free plinks\n");
  706. rcu_read_unlock();
  707. return;
  708. }
  709. event = OPN_ACPT;
  710. } else if (matches_local) {
  711. switch (ftype) {
  712. case WLAN_SP_MESH_PEERING_OPEN:
  713. if (!mesh_plink_free_count(sdata) ||
  714. (sta->plid && sta->plid != plid))
  715. event = OPN_IGNR;
  716. else
  717. event = OPN_ACPT;
  718. break;
  719. case WLAN_SP_MESH_PEERING_CONFIRM:
  720. if (!mesh_plink_free_count(sdata) ||
  721. (sta->llid != llid || sta->plid != plid))
  722. event = CNF_IGNR;
  723. else
  724. event = CNF_ACPT;
  725. break;
  726. case WLAN_SP_MESH_PEERING_CLOSE:
  727. if (sta->plink_state == NL80211_PLINK_ESTAB)
  728. /* Do not check for llid or plid. This does not
  729. * follow the standard but since multiple plinks
  730. * per sta are not supported, it is necessary in
  731. * order to avoid a livelock when MP A sees an
  732. * establish peer link to MP B but MP B does not
  733. * see it. This can be caused by a timeout in
  734. * B's peer link establishment or B beign
  735. * restarted.
  736. */
  737. event = CLS_ACPT;
  738. else if (sta->plid != plid)
  739. event = CLS_IGNR;
  740. else if (ie_len == 7 && sta->llid != llid)
  741. event = CLS_IGNR;
  742. else
  743. event = CLS_ACPT;
  744. break;
  745. default:
  746. mpl_dbg(sdata, "Mesh plink: unknown frame subtype\n");
  747. rcu_read_unlock();
  748. return;
  749. }
  750. }
  751. if (event == OPN_ACPT) {
  752. rcu_read_unlock();
  753. /* allocate sta entry if necessary and update info */
  754. sta = mesh_sta_info_get(sdata, mgmt->sa, &elems);
  755. if (!sta) {
  756. mpl_dbg(sdata, "Mesh plink: failed to init peer!\n");
  757. rcu_read_unlock();
  758. return;
  759. }
  760. }
  761. mpl_dbg(sdata, "peer %pM in state %s got event %s\n", mgmt->sa,
  762. mplstates[sta->plink_state], mplevents[event]);
  763. reason = 0;
  764. spin_lock_bh(&sta->lock);
  765. switch (sta->plink_state) {
  766. /* spin_unlock as soon as state is updated at each case */
  767. case NL80211_PLINK_LISTEN:
  768. switch (event) {
  769. case CLS_ACPT:
  770. mesh_plink_fsm_restart(sta);
  771. spin_unlock_bh(&sta->lock);
  772. break;
  773. case OPN_ACPT:
  774. sta->plink_state = NL80211_PLINK_OPN_RCVD;
  775. sta->plid = plid;
  776. get_random_bytes(&llid, 2);
  777. sta->llid = llid;
  778. mesh_plink_timer_set(sta,
  779. mshcfg->dot11MeshRetryTimeout);
  780. /* set the non-peer mode to active during peering */
  781. changed |= ieee80211_mps_local_status_update(sdata);
  782. spin_unlock_bh(&sta->lock);
  783. mesh_plink_frame_tx(sdata,
  784. WLAN_SP_MESH_PEERING_OPEN,
  785. sta->sta.addr, llid, 0, 0);
  786. mesh_plink_frame_tx(sdata,
  787. WLAN_SP_MESH_PEERING_CONFIRM,
  788. sta->sta.addr, llid, plid, 0);
  789. break;
  790. default:
  791. spin_unlock_bh(&sta->lock);
  792. break;
  793. }
  794. break;
  795. case NL80211_PLINK_OPN_SNT:
  796. switch (event) {
  797. case OPN_RJCT:
  798. case CNF_RJCT:
  799. reason = cpu_to_le16(WLAN_REASON_MESH_CONFIG);
  800. case CLS_ACPT:
  801. if (!reason)
  802. reason = cpu_to_le16(WLAN_REASON_MESH_CLOSE);
  803. sta->reason = reason;
  804. sta->plink_state = NL80211_PLINK_HOLDING;
  805. if (!mod_plink_timer(sta,
  806. mshcfg->dot11MeshHoldingTimeout))
  807. sta->ignore_plink_timer = true;
  808. llid = sta->llid;
  809. spin_unlock_bh(&sta->lock);
  810. mesh_plink_frame_tx(sdata,
  811. WLAN_SP_MESH_PEERING_CLOSE,
  812. sta->sta.addr, llid, plid, reason);
  813. break;
  814. case OPN_ACPT:
  815. /* retry timer is left untouched */
  816. sta->plink_state = NL80211_PLINK_OPN_RCVD;
  817. sta->plid = plid;
  818. llid = sta->llid;
  819. spin_unlock_bh(&sta->lock);
  820. mesh_plink_frame_tx(sdata,
  821. WLAN_SP_MESH_PEERING_CONFIRM,
  822. sta->sta.addr, llid, plid, 0);
  823. break;
  824. case CNF_ACPT:
  825. sta->plink_state = NL80211_PLINK_CNF_RCVD;
  826. if (!mod_plink_timer(sta,
  827. mshcfg->dot11MeshConfirmTimeout))
  828. sta->ignore_plink_timer = true;
  829. spin_unlock_bh(&sta->lock);
  830. break;
  831. default:
  832. spin_unlock_bh(&sta->lock);
  833. break;
  834. }
  835. break;
  836. case NL80211_PLINK_OPN_RCVD:
  837. switch (event) {
  838. case OPN_RJCT:
  839. case CNF_RJCT:
  840. reason = cpu_to_le16(WLAN_REASON_MESH_CONFIG);
  841. case CLS_ACPT:
  842. if (!reason)
  843. reason = cpu_to_le16(WLAN_REASON_MESH_CLOSE);
  844. sta->reason = reason;
  845. sta->plink_state = NL80211_PLINK_HOLDING;
  846. if (!mod_plink_timer(sta,
  847. mshcfg->dot11MeshHoldingTimeout))
  848. sta->ignore_plink_timer = true;
  849. llid = sta->llid;
  850. spin_unlock_bh(&sta->lock);
  851. mesh_plink_frame_tx(sdata, WLAN_SP_MESH_PEERING_CLOSE,
  852. sta->sta.addr, llid, plid, reason);
  853. break;
  854. case OPN_ACPT:
  855. llid = sta->llid;
  856. spin_unlock_bh(&sta->lock);
  857. mesh_plink_frame_tx(sdata,
  858. WLAN_SP_MESH_PEERING_CONFIRM,
  859. sta->sta.addr, llid, plid, 0);
  860. break;
  861. case CNF_ACPT:
  862. del_timer(&sta->plink_timer);
  863. sta->plink_state = NL80211_PLINK_ESTAB;
  864. spin_unlock_bh(&sta->lock);
  865. changed |= mesh_plink_inc_estab_count(sdata);
  866. changed |= mesh_set_ht_prot_mode(sdata);
  867. changed |= mesh_set_short_slot_time(sdata);
  868. mpl_dbg(sdata, "Mesh plink with %pM ESTABLISHED\n",
  869. sta->sta.addr);
  870. ieee80211_mps_sta_status_update(sta);
  871. changed |= ieee80211_mps_set_sta_local_pm(sta,
  872. mshcfg->power_mode);
  873. break;
  874. default:
  875. spin_unlock_bh(&sta->lock);
  876. break;
  877. }
  878. break;
  879. case NL80211_PLINK_CNF_RCVD:
  880. switch (event) {
  881. case OPN_RJCT:
  882. case CNF_RJCT:
  883. reason = cpu_to_le16(WLAN_REASON_MESH_CONFIG);
  884. case CLS_ACPT:
  885. if (!reason)
  886. reason = cpu_to_le16(WLAN_REASON_MESH_CLOSE);
  887. sta->reason = reason;
  888. sta->plink_state = NL80211_PLINK_HOLDING;
  889. if (!mod_plink_timer(sta,
  890. mshcfg->dot11MeshHoldingTimeout))
  891. sta->ignore_plink_timer = true;
  892. llid = sta->llid;
  893. spin_unlock_bh(&sta->lock);
  894. mesh_plink_frame_tx(sdata,
  895. WLAN_SP_MESH_PEERING_CLOSE,
  896. sta->sta.addr, llid, plid, reason);
  897. break;
  898. case OPN_ACPT:
  899. del_timer(&sta->plink_timer);
  900. sta->plink_state = NL80211_PLINK_ESTAB;
  901. spin_unlock_bh(&sta->lock);
  902. changed |= mesh_plink_inc_estab_count(sdata);
  903. changed |= mesh_set_ht_prot_mode(sdata);
  904. changed |= mesh_set_short_slot_time(sdata);
  905. mpl_dbg(sdata, "Mesh plink with %pM ESTABLISHED\n",
  906. sta->sta.addr);
  907. mesh_plink_frame_tx(sdata,
  908. WLAN_SP_MESH_PEERING_CONFIRM,
  909. sta->sta.addr, llid, plid, 0);
  910. ieee80211_mps_sta_status_update(sta);
  911. changed |= ieee80211_mps_set_sta_local_pm(sta,
  912. mshcfg->power_mode);
  913. break;
  914. default:
  915. spin_unlock_bh(&sta->lock);
  916. break;
  917. }
  918. break;
  919. case NL80211_PLINK_ESTAB:
  920. switch (event) {
  921. case CLS_ACPT:
  922. reason = cpu_to_le16(WLAN_REASON_MESH_CLOSE);
  923. sta->reason = reason;
  924. changed |= __mesh_plink_deactivate(sta);
  925. sta->plink_state = NL80211_PLINK_HOLDING;
  926. llid = sta->llid;
  927. mod_plink_timer(sta, mshcfg->dot11MeshHoldingTimeout);
  928. spin_unlock_bh(&sta->lock);
  929. changed |= mesh_set_ht_prot_mode(sdata);
  930. changed |= mesh_set_short_slot_time(sdata);
  931. mesh_plink_frame_tx(sdata, WLAN_SP_MESH_PEERING_CLOSE,
  932. sta->sta.addr, llid, plid, reason);
  933. break;
  934. case OPN_ACPT:
  935. llid = sta->llid;
  936. spin_unlock_bh(&sta->lock);
  937. mesh_plink_frame_tx(sdata,
  938. WLAN_SP_MESH_PEERING_CONFIRM,
  939. sta->sta.addr, llid, plid, 0);
  940. break;
  941. default:
  942. spin_unlock_bh(&sta->lock);
  943. break;
  944. }
  945. break;
  946. case NL80211_PLINK_HOLDING:
  947. switch (event) {
  948. case CLS_ACPT:
  949. if (del_timer(&sta->plink_timer))
  950. sta->ignore_plink_timer = 1;
  951. mesh_plink_fsm_restart(sta);
  952. spin_unlock_bh(&sta->lock);
  953. break;
  954. case OPN_ACPT:
  955. case CNF_ACPT:
  956. case OPN_RJCT:
  957. case CNF_RJCT:
  958. llid = sta->llid;
  959. reason = sta->reason;
  960. spin_unlock_bh(&sta->lock);
  961. mesh_plink_frame_tx(sdata, WLAN_SP_MESH_PEERING_CLOSE,
  962. sta->sta.addr, llid, plid, reason);
  963. break;
  964. default:
  965. spin_unlock_bh(&sta->lock);
  966. }
  967. break;
  968. default:
  969. /* should not get here, PLINK_BLOCKED is dealt with at the
  970. * beginning of the function
  971. */
  972. spin_unlock_bh(&sta->lock);
  973. break;
  974. }
  975. rcu_read_unlock();
  976. if (changed)
  977. ieee80211_mbss_info_change_notify(sdata, changed);
  978. }