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