mesh_plink.c 33 KB

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