mesh_plink.c 28 KB

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