mesh_plink.c 32 KB

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