mesh_plink.c 29 KB

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