mesh_plink.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073
  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. sta_info_pre_move_state(sta, IEEE80211_STA_AUTH);
  371. sta_info_pre_move_state(sta, IEEE80211_STA_ASSOC);
  372. sta_info_pre_move_state(sta, IEEE80211_STA_AUTHORIZED);
  373. set_sta_flag(sta, WLAN_STA_WME);
  374. return sta;
  375. }
  376. static struct sta_info *
  377. mesh_sta_info_alloc(struct ieee80211_sub_if_data *sdata, u8 *addr,
  378. struct ieee802_11_elems *elems)
  379. {
  380. struct sta_info *sta = NULL;
  381. /* Userspace handles station allocation */
  382. if (sdata->u.mesh.user_mpm ||
  383. 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. return;
  468. spin_lock_bh(&sta->lock);
  469. if (sta->ignore_plink_timer) {
  470. sta->ignore_plink_timer = false;
  471. spin_unlock_bh(&sta->lock);
  472. return;
  473. }
  474. mpl_dbg(sta->sdata,
  475. "Mesh plink timer for %pM fired on state %d\n",
  476. sta->sta.addr, sta->plink_state);
  477. reason = 0;
  478. llid = sta->llid;
  479. plid = sta->plid;
  480. sdata = sta->sdata;
  481. mshcfg = &sdata->u.mesh.mshcfg;
  482. switch (sta->plink_state) {
  483. case NL80211_PLINK_OPN_RCVD:
  484. case NL80211_PLINK_OPN_SNT:
  485. /* retry timer */
  486. if (sta->plink_retries < mshcfg->dot11MeshMaxRetries) {
  487. u32 rand;
  488. mpl_dbg(sta->sdata,
  489. "Mesh plink for %pM (retry, timeout): %d %d\n",
  490. sta->sta.addr, sta->plink_retries,
  491. sta->plink_timeout);
  492. get_random_bytes(&rand, sizeof(u32));
  493. sta->plink_timeout = sta->plink_timeout +
  494. rand % sta->plink_timeout;
  495. ++sta->plink_retries;
  496. mod_plink_timer(sta, sta->plink_timeout);
  497. spin_unlock_bh(&sta->lock);
  498. mesh_plink_frame_tx(sdata, WLAN_SP_MESH_PEERING_OPEN,
  499. sta->sta.addr, llid, 0, 0);
  500. break;
  501. }
  502. reason = cpu_to_le16(WLAN_REASON_MESH_MAX_RETRIES);
  503. /* fall through on else */
  504. case NL80211_PLINK_CNF_RCVD:
  505. /* confirm timer */
  506. if (!reason)
  507. reason = cpu_to_le16(WLAN_REASON_MESH_CONFIRM_TIMEOUT);
  508. sta->plink_state = NL80211_PLINK_HOLDING;
  509. mod_plink_timer(sta, mshcfg->dot11MeshHoldingTimeout);
  510. spin_unlock_bh(&sta->lock);
  511. mesh_plink_frame_tx(sdata, WLAN_SP_MESH_PEERING_CLOSE,
  512. sta->sta.addr, llid, plid, reason);
  513. break;
  514. case NL80211_PLINK_HOLDING:
  515. /* holding timer */
  516. del_timer(&sta->plink_timer);
  517. mesh_plink_fsm_restart(sta);
  518. spin_unlock_bh(&sta->lock);
  519. break;
  520. default:
  521. spin_unlock_bh(&sta->lock);
  522. break;
  523. }
  524. }
  525. static inline void mesh_plink_timer_set(struct sta_info *sta, int timeout)
  526. {
  527. sta->plink_timer.expires = jiffies + (HZ * timeout / 1000);
  528. sta->plink_timer.data = (unsigned long) sta;
  529. sta->plink_timer.function = mesh_plink_timer;
  530. sta->plink_timeout = timeout;
  531. add_timer(&sta->plink_timer);
  532. }
  533. u32 mesh_plink_open(struct sta_info *sta)
  534. {
  535. __le16 llid;
  536. struct ieee80211_sub_if_data *sdata = sta->sdata;
  537. u32 changed;
  538. if (!test_sta_flag(sta, WLAN_STA_AUTH))
  539. return 0;
  540. spin_lock_bh(&sta->lock);
  541. get_random_bytes(&llid, 2);
  542. sta->llid = llid;
  543. if (sta->plink_state != NL80211_PLINK_LISTEN &&
  544. sta->plink_state != NL80211_PLINK_BLOCKED) {
  545. spin_unlock_bh(&sta->lock);
  546. return 0;
  547. }
  548. sta->plink_state = NL80211_PLINK_OPN_SNT;
  549. mesh_plink_timer_set(sta, sdata->u.mesh.mshcfg.dot11MeshRetryTimeout);
  550. spin_unlock_bh(&sta->lock);
  551. mpl_dbg(sdata,
  552. "Mesh plink: starting establishment with %pM\n",
  553. sta->sta.addr);
  554. /* set the non-peer mode to active during peering */
  555. changed = ieee80211_mps_local_status_update(sdata);
  556. mesh_plink_frame_tx(sdata, WLAN_SP_MESH_PEERING_OPEN,
  557. sta->sta.addr, llid, 0, 0);
  558. return changed;
  559. }
  560. u32 mesh_plink_block(struct sta_info *sta)
  561. {
  562. u32 changed;
  563. spin_lock_bh(&sta->lock);
  564. changed = __mesh_plink_deactivate(sta);
  565. sta->plink_state = NL80211_PLINK_BLOCKED;
  566. spin_unlock_bh(&sta->lock);
  567. return changed;
  568. }
  569. void mesh_rx_plink_frame(struct ieee80211_sub_if_data *sdata,
  570. struct ieee80211_mgmt *mgmt, size_t len,
  571. struct ieee80211_rx_status *rx_status)
  572. {
  573. struct mesh_config *mshcfg = &sdata->u.mesh.mshcfg;
  574. struct ieee802_11_elems elems;
  575. struct sta_info *sta;
  576. enum plink_event event;
  577. enum ieee80211_self_protected_actioncode ftype;
  578. size_t baselen;
  579. bool matches_local = true;
  580. u8 ie_len;
  581. u8 *baseaddr;
  582. u32 changed = 0;
  583. __le16 plid, llid, reason;
  584. /* need action_code, aux */
  585. if (len < IEEE80211_MIN_ACTION_SIZE + 3)
  586. return;
  587. if (sdata->u.mesh.user_mpm)
  588. /* userspace must register for these */
  589. return;
  590. if (is_multicast_ether_addr(mgmt->da)) {
  591. mpl_dbg(sdata,
  592. "Mesh plink: ignore frame from multicast address\n");
  593. return;
  594. }
  595. baseaddr = mgmt->u.action.u.self_prot.variable;
  596. baselen = (u8 *) mgmt->u.action.u.self_prot.variable - (u8 *) mgmt;
  597. if (mgmt->u.action.u.self_prot.action_code ==
  598. WLAN_SP_MESH_PEERING_CONFIRM) {
  599. baseaddr += 4;
  600. baselen += 4;
  601. }
  602. ieee802_11_parse_elems(baseaddr, len - baselen, &elems);
  603. if (!elems.peering) {
  604. mpl_dbg(sdata,
  605. "Mesh plink: missing necessary peer link ie\n");
  606. return;
  607. }
  608. if (elems.rsn_len &&
  609. sdata->u.mesh.security == IEEE80211_MESH_SEC_NONE) {
  610. mpl_dbg(sdata,
  611. "Mesh plink: can't establish link with secure peer\n");
  612. return;
  613. }
  614. ftype = mgmt->u.action.u.self_prot.action_code;
  615. ie_len = elems.peering_len;
  616. if ((ftype == WLAN_SP_MESH_PEERING_OPEN && ie_len != 4) ||
  617. (ftype == WLAN_SP_MESH_PEERING_CONFIRM && ie_len != 6) ||
  618. (ftype == WLAN_SP_MESH_PEERING_CLOSE && ie_len != 6
  619. && ie_len != 8)) {
  620. mpl_dbg(sdata,
  621. "Mesh plink: incorrect plink ie length %d %d\n",
  622. ftype, ie_len);
  623. return;
  624. }
  625. if (ftype != WLAN_SP_MESH_PEERING_CLOSE &&
  626. (!elems.mesh_id || !elems.mesh_config)) {
  627. mpl_dbg(sdata, "Mesh plink: missing necessary ie\n");
  628. return;
  629. }
  630. /* Note the lines below are correct, the llid in the frame is the plid
  631. * from the point of view of this host.
  632. */
  633. memcpy(&plid, PLINK_GET_LLID(elems.peering), 2);
  634. if (ftype == WLAN_SP_MESH_PEERING_CONFIRM ||
  635. (ftype == WLAN_SP_MESH_PEERING_CLOSE && ie_len == 8))
  636. memcpy(&llid, PLINK_GET_PLID(elems.peering), 2);
  637. /* WARNING: Only for sta pointer, is dropped & re-acquired */
  638. rcu_read_lock();
  639. sta = sta_info_get(sdata, mgmt->sa);
  640. if (!sta && ftype != WLAN_SP_MESH_PEERING_OPEN) {
  641. mpl_dbg(sdata, "Mesh plink: cls or cnf from unknown peer\n");
  642. rcu_read_unlock();
  643. return;
  644. }
  645. if (ftype == WLAN_SP_MESH_PEERING_OPEN &&
  646. !rssi_threshold_check(sta, sdata)) {
  647. mpl_dbg(sdata, "Mesh plink: %pM does not meet rssi threshold\n",
  648. mgmt->sa);
  649. rcu_read_unlock();
  650. return;
  651. }
  652. if (sta && !test_sta_flag(sta, WLAN_STA_AUTH)) {
  653. mpl_dbg(sdata, "Mesh plink: Action frame from non-authed peer\n");
  654. rcu_read_unlock();
  655. return;
  656. }
  657. if (sta && sta->plink_state == NL80211_PLINK_BLOCKED) {
  658. rcu_read_unlock();
  659. return;
  660. }
  661. /* Now we will figure out the appropriate event... */
  662. event = PLINK_UNDEFINED;
  663. if (ftype != WLAN_SP_MESH_PEERING_CLOSE &&
  664. !mesh_matches_local(sdata, &elems)) {
  665. matches_local = false;
  666. switch (ftype) {
  667. case WLAN_SP_MESH_PEERING_OPEN:
  668. event = OPN_RJCT;
  669. break;
  670. case WLAN_SP_MESH_PEERING_CONFIRM:
  671. event = CNF_RJCT;
  672. break;
  673. default:
  674. break;
  675. }
  676. }
  677. if (!sta && !matches_local) {
  678. rcu_read_unlock();
  679. reason = cpu_to_le16(WLAN_REASON_MESH_CONFIG);
  680. llid = 0;
  681. mesh_plink_frame_tx(sdata, WLAN_SP_MESH_PEERING_CLOSE,
  682. mgmt->sa, llid, plid, reason);
  683. return;
  684. } else if (!sta) {
  685. /* ftype == WLAN_SP_MESH_PEERING_OPEN */
  686. if (!mesh_plink_free_count(sdata)) {
  687. mpl_dbg(sdata, "Mesh plink error: no more free plinks\n");
  688. rcu_read_unlock();
  689. return;
  690. }
  691. event = OPN_ACPT;
  692. } else if (matches_local) {
  693. switch (ftype) {
  694. case WLAN_SP_MESH_PEERING_OPEN:
  695. if (!mesh_plink_free_count(sdata) ||
  696. (sta->plid && sta->plid != plid))
  697. event = OPN_IGNR;
  698. else
  699. event = OPN_ACPT;
  700. break;
  701. case WLAN_SP_MESH_PEERING_CONFIRM:
  702. if (!mesh_plink_free_count(sdata) ||
  703. (sta->llid != llid || sta->plid != plid))
  704. event = CNF_IGNR;
  705. else
  706. event = CNF_ACPT;
  707. break;
  708. case WLAN_SP_MESH_PEERING_CLOSE:
  709. if (sta->plink_state == NL80211_PLINK_ESTAB)
  710. /* Do not check for llid or plid. This does not
  711. * follow the standard but since multiple plinks
  712. * per sta are not supported, it is necessary in
  713. * order to avoid a livelock when MP A sees an
  714. * establish peer link to MP B but MP B does not
  715. * see it. This can be caused by a timeout in
  716. * B's peer link establishment or B beign
  717. * restarted.
  718. */
  719. event = CLS_ACPT;
  720. else if (sta->plid != plid)
  721. event = CLS_IGNR;
  722. else if (ie_len == 7 && sta->llid != llid)
  723. event = CLS_IGNR;
  724. else
  725. event = CLS_ACPT;
  726. break;
  727. default:
  728. mpl_dbg(sdata, "Mesh plink: unknown frame subtype\n");
  729. rcu_read_unlock();
  730. return;
  731. }
  732. }
  733. if (event == OPN_ACPT) {
  734. rcu_read_unlock();
  735. /* allocate sta entry if necessary and update info */
  736. sta = mesh_sta_info_get(sdata, mgmt->sa, &elems);
  737. if (!sta) {
  738. mpl_dbg(sdata, "Mesh plink: failed to init peer!\n");
  739. rcu_read_unlock();
  740. return;
  741. }
  742. }
  743. mpl_dbg(sdata, "peer %pM in state %s got event %s\n", mgmt->sa,
  744. mplstates[sta->plink_state], mplevents[event]);
  745. reason = 0;
  746. spin_lock_bh(&sta->lock);
  747. switch (sta->plink_state) {
  748. /* spin_unlock as soon as state is updated at each case */
  749. case NL80211_PLINK_LISTEN:
  750. switch (event) {
  751. case CLS_ACPT:
  752. mesh_plink_fsm_restart(sta);
  753. spin_unlock_bh(&sta->lock);
  754. break;
  755. case OPN_ACPT:
  756. sta->plink_state = NL80211_PLINK_OPN_RCVD;
  757. sta->plid = plid;
  758. get_random_bytes(&llid, 2);
  759. sta->llid = llid;
  760. mesh_plink_timer_set(sta,
  761. mshcfg->dot11MeshRetryTimeout);
  762. /* set the non-peer mode to active during peering */
  763. changed |= ieee80211_mps_local_status_update(sdata);
  764. spin_unlock_bh(&sta->lock);
  765. mesh_plink_frame_tx(sdata,
  766. WLAN_SP_MESH_PEERING_OPEN,
  767. sta->sta.addr, llid, 0, 0);
  768. mesh_plink_frame_tx(sdata,
  769. WLAN_SP_MESH_PEERING_CONFIRM,
  770. sta->sta.addr, llid, plid, 0);
  771. break;
  772. default:
  773. spin_unlock_bh(&sta->lock);
  774. break;
  775. }
  776. break;
  777. case NL80211_PLINK_OPN_SNT:
  778. switch (event) {
  779. case OPN_RJCT:
  780. case CNF_RJCT:
  781. reason = cpu_to_le16(WLAN_REASON_MESH_CONFIG);
  782. case CLS_ACPT:
  783. if (!reason)
  784. reason = cpu_to_le16(WLAN_REASON_MESH_CLOSE);
  785. sta->reason = reason;
  786. sta->plink_state = NL80211_PLINK_HOLDING;
  787. if (!mod_plink_timer(sta,
  788. mshcfg->dot11MeshHoldingTimeout))
  789. sta->ignore_plink_timer = true;
  790. llid = sta->llid;
  791. spin_unlock_bh(&sta->lock);
  792. mesh_plink_frame_tx(sdata,
  793. WLAN_SP_MESH_PEERING_CLOSE,
  794. sta->sta.addr, llid, plid, reason);
  795. break;
  796. case OPN_ACPT:
  797. /* retry timer is left untouched */
  798. sta->plink_state = NL80211_PLINK_OPN_RCVD;
  799. sta->plid = plid;
  800. llid = sta->llid;
  801. spin_unlock_bh(&sta->lock);
  802. mesh_plink_frame_tx(sdata,
  803. WLAN_SP_MESH_PEERING_CONFIRM,
  804. sta->sta.addr, llid, plid, 0);
  805. break;
  806. case CNF_ACPT:
  807. sta->plink_state = NL80211_PLINK_CNF_RCVD;
  808. if (!mod_plink_timer(sta,
  809. mshcfg->dot11MeshConfirmTimeout))
  810. sta->ignore_plink_timer = true;
  811. spin_unlock_bh(&sta->lock);
  812. break;
  813. default:
  814. spin_unlock_bh(&sta->lock);
  815. break;
  816. }
  817. break;
  818. case NL80211_PLINK_OPN_RCVD:
  819. switch (event) {
  820. case OPN_RJCT:
  821. case CNF_RJCT:
  822. reason = cpu_to_le16(WLAN_REASON_MESH_CONFIG);
  823. case CLS_ACPT:
  824. if (!reason)
  825. reason = cpu_to_le16(WLAN_REASON_MESH_CLOSE);
  826. sta->reason = reason;
  827. sta->plink_state = NL80211_PLINK_HOLDING;
  828. if (!mod_plink_timer(sta,
  829. mshcfg->dot11MeshHoldingTimeout))
  830. sta->ignore_plink_timer = true;
  831. llid = sta->llid;
  832. spin_unlock_bh(&sta->lock);
  833. mesh_plink_frame_tx(sdata, WLAN_SP_MESH_PEERING_CLOSE,
  834. sta->sta.addr, llid, plid, reason);
  835. break;
  836. case OPN_ACPT:
  837. llid = sta->llid;
  838. spin_unlock_bh(&sta->lock);
  839. mesh_plink_frame_tx(sdata,
  840. WLAN_SP_MESH_PEERING_CONFIRM,
  841. sta->sta.addr, llid, plid, 0);
  842. break;
  843. case CNF_ACPT:
  844. del_timer(&sta->plink_timer);
  845. sta->plink_state = NL80211_PLINK_ESTAB;
  846. spin_unlock_bh(&sta->lock);
  847. changed |= mesh_plink_inc_estab_count(sdata);
  848. changed |= mesh_set_ht_prot_mode(sdata);
  849. changed |= mesh_set_short_slot_time(sdata);
  850. mpl_dbg(sdata, "Mesh plink with %pM ESTABLISHED\n",
  851. sta->sta.addr);
  852. ieee80211_mps_sta_status_update(sta);
  853. changed |= ieee80211_mps_set_sta_local_pm(sta,
  854. mshcfg->power_mode);
  855. break;
  856. default:
  857. spin_unlock_bh(&sta->lock);
  858. break;
  859. }
  860. break;
  861. case NL80211_PLINK_CNF_RCVD:
  862. switch (event) {
  863. case OPN_RJCT:
  864. case CNF_RJCT:
  865. reason = cpu_to_le16(WLAN_REASON_MESH_CONFIG);
  866. case CLS_ACPT:
  867. if (!reason)
  868. reason = cpu_to_le16(WLAN_REASON_MESH_CLOSE);
  869. sta->reason = reason;
  870. sta->plink_state = NL80211_PLINK_HOLDING;
  871. if (!mod_plink_timer(sta,
  872. mshcfg->dot11MeshHoldingTimeout))
  873. sta->ignore_plink_timer = true;
  874. llid = sta->llid;
  875. spin_unlock_bh(&sta->lock);
  876. mesh_plink_frame_tx(sdata,
  877. WLAN_SP_MESH_PEERING_CLOSE,
  878. sta->sta.addr, llid, plid, reason);
  879. break;
  880. case OPN_ACPT:
  881. del_timer(&sta->plink_timer);
  882. sta->plink_state = NL80211_PLINK_ESTAB;
  883. spin_unlock_bh(&sta->lock);
  884. changed |= mesh_plink_inc_estab_count(sdata);
  885. changed |= mesh_set_ht_prot_mode(sdata);
  886. changed |= mesh_set_short_slot_time(sdata);
  887. mpl_dbg(sdata, "Mesh plink with %pM ESTABLISHED\n",
  888. sta->sta.addr);
  889. mesh_plink_frame_tx(sdata,
  890. WLAN_SP_MESH_PEERING_CONFIRM,
  891. sta->sta.addr, llid, plid, 0);
  892. ieee80211_mps_sta_status_update(sta);
  893. changed |= ieee80211_mps_set_sta_local_pm(sta,
  894. mshcfg->power_mode);
  895. break;
  896. default:
  897. spin_unlock_bh(&sta->lock);
  898. break;
  899. }
  900. break;
  901. case NL80211_PLINK_ESTAB:
  902. switch (event) {
  903. case CLS_ACPT:
  904. reason = cpu_to_le16(WLAN_REASON_MESH_CLOSE);
  905. sta->reason = reason;
  906. changed |= __mesh_plink_deactivate(sta);
  907. sta->plink_state = NL80211_PLINK_HOLDING;
  908. llid = sta->llid;
  909. mod_plink_timer(sta, mshcfg->dot11MeshHoldingTimeout);
  910. spin_unlock_bh(&sta->lock);
  911. changed |= mesh_set_ht_prot_mode(sdata);
  912. changed |= mesh_set_short_slot_time(sdata);
  913. mesh_plink_frame_tx(sdata, WLAN_SP_MESH_PEERING_CLOSE,
  914. sta->sta.addr, llid, plid, reason);
  915. break;
  916. case OPN_ACPT:
  917. llid = sta->llid;
  918. spin_unlock_bh(&sta->lock);
  919. mesh_plink_frame_tx(sdata,
  920. WLAN_SP_MESH_PEERING_CONFIRM,
  921. sta->sta.addr, llid, plid, 0);
  922. break;
  923. default:
  924. spin_unlock_bh(&sta->lock);
  925. break;
  926. }
  927. break;
  928. case NL80211_PLINK_HOLDING:
  929. switch (event) {
  930. case CLS_ACPT:
  931. if (del_timer(&sta->plink_timer))
  932. sta->ignore_plink_timer = 1;
  933. mesh_plink_fsm_restart(sta);
  934. spin_unlock_bh(&sta->lock);
  935. break;
  936. case OPN_ACPT:
  937. case CNF_ACPT:
  938. case OPN_RJCT:
  939. case CNF_RJCT:
  940. llid = sta->llid;
  941. reason = sta->reason;
  942. spin_unlock_bh(&sta->lock);
  943. mesh_plink_frame_tx(sdata, WLAN_SP_MESH_PEERING_CLOSE,
  944. sta->sta.addr, llid, plid, reason);
  945. break;
  946. default:
  947. spin_unlock_bh(&sta->lock);
  948. }
  949. break;
  950. default:
  951. /* should not get here, PLINK_BLOCKED is dealt with at the
  952. * beginning of the function
  953. */
  954. spin_unlock_bh(&sta->lock);
  955. break;
  956. }
  957. rcu_read_unlock();
  958. if (changed)
  959. ieee80211_mbss_info_change_notify(sdata, changed);
  960. }