mesh_plink.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107
  1. /*
  2. * Copyright (c) 2008, 2009 open80211s Ltd.
  3. * Author: Luis Carlos Cobo <luisca@cozybit.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. */
  9. #include <linux/gfp.h>
  10. #include <linux/kernel.h>
  11. #include <linux/random.h>
  12. #include "ieee80211_i.h"
  13. #include "rate.h"
  14. #include "mesh.h"
  15. #define PLINK_GET_LLID(p) (p + 2)
  16. #define PLINK_GET_PLID(p) (p + 4)
  17. #define mod_plink_timer(s, t) (mod_timer(&s->plink_timer, \
  18. jiffies + HZ * t / 1000))
  19. 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->lock
  67. */
  68. static inline void mesh_plink_fsm_restart(struct sta_info *sta)
  69. {
  70. sta->plink_state = NL80211_PLINK_LISTEN;
  71. sta->llid = sta->plid = sta->reason = 0;
  72. sta->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. enum ieee80211_band band = ieee80211_get_sdata_band(sdata);
  88. struct ieee80211_supported_band *sband = local->hw.wiphy->bands[band];
  89. struct sta_info *sta;
  90. u32 erp_rates = 0, changed = 0;
  91. int i;
  92. bool short_slot = false;
  93. if (band == IEEE80211_BAND_5GHZ) {
  94. /* (IEEE 802.11-2012 19.4.5) */
  95. short_slot = true;
  96. goto out;
  97. } else if (band != IEEE80211_BAND_2GHZ ||
  98. (band == IEEE80211_BAND_2GHZ &&
  99. local->hw.flags & IEEE80211_HW_2GHZ_SHORT_SLOT_INCAPABLE))
  100. goto out;
  101. for (i = 0; i < sband->n_bitrates; i++)
  102. if (sband->bitrates[i].flags & IEEE80211_RATE_ERP_G)
  103. erp_rates |= BIT(i);
  104. if (!erp_rates)
  105. goto out;
  106. rcu_read_lock();
  107. list_for_each_entry_rcu(sta, &local->sta_list, list) {
  108. if (sdata != sta->sdata ||
  109. sta->plink_state != NL80211_PLINK_ESTAB)
  110. continue;
  111. short_slot = false;
  112. if (erp_rates & sta->sta.supp_rates[band])
  113. short_slot = true;
  114. else
  115. break;
  116. }
  117. rcu_read_unlock();
  118. out:
  119. if (sdata->vif.bss_conf.use_short_slot != short_slot) {
  120. sdata->vif.bss_conf.use_short_slot = short_slot;
  121. changed = BSS_CHANGED_ERP_SLOT;
  122. mpl_dbg(sdata, "mesh_plink %pM: ERP short slot time %d\n",
  123. sdata->vif.addr, short_slot);
  124. }
  125. return changed;
  126. }
  127. /**
  128. * mesh_set_ht_prot_mode - set correct HT protection mode
  129. *
  130. * Section 9.23.3.5 of IEEE 80211-2012 describes the protection rules for HT
  131. * mesh STA in a MBSS. Three HT protection modes are supported for now, non-HT
  132. * mixed mode, 20MHz-protection and no-protection mode. non-HT mixed mode is
  133. * selected if any non-HT peers are present in our MBSS. 20MHz-protection mode
  134. * is selected if all peers in our 20/40MHz MBSS support HT and atleast one
  135. * HT20 peer is present. Otherwise no-protection mode is selected.
  136. */
  137. static u32 mesh_set_ht_prot_mode(struct ieee80211_sub_if_data *sdata)
  138. {
  139. struct ieee80211_local *local = sdata->local;
  140. struct sta_info *sta;
  141. u16 ht_opmode;
  142. bool non_ht_sta = false, ht20_sta = false;
  143. switch (sdata->vif.bss_conf.chandef.width) {
  144. case NL80211_CHAN_WIDTH_20_NOHT:
  145. case NL80211_CHAN_WIDTH_5:
  146. case NL80211_CHAN_WIDTH_10:
  147. return 0;
  148. default:
  149. break;
  150. }
  151. rcu_read_lock();
  152. list_for_each_entry_rcu(sta, &local->sta_list, list) {
  153. if (sdata != sta->sdata ||
  154. sta->plink_state != NL80211_PLINK_ESTAB)
  155. continue;
  156. if (sta->sta.bandwidth > IEEE80211_STA_RX_BW_20)
  157. continue;
  158. if (!sta->sta.ht_cap.ht_supported) {
  159. mpl_dbg(sdata, "nonHT sta (%pM) is present\n",
  160. sta->sta.addr);
  161. non_ht_sta = true;
  162. break;
  163. }
  164. mpl_dbg(sdata, "HT20 sta (%pM) is present\n", sta->sta.addr);
  165. ht20_sta = true;
  166. }
  167. rcu_read_unlock();
  168. if (non_ht_sta)
  169. ht_opmode = IEEE80211_HT_OP_MODE_PROTECTION_NONHT_MIXED;
  170. else if (ht20_sta &&
  171. sdata->vif.bss_conf.chandef.width > NL80211_CHAN_WIDTH_20)
  172. ht_opmode = IEEE80211_HT_OP_MODE_PROTECTION_20MHZ;
  173. else
  174. ht_opmode = IEEE80211_HT_OP_MODE_PROTECTION_NONE;
  175. if (sdata->vif.bss_conf.ht_operation_mode == ht_opmode)
  176. return 0;
  177. sdata->vif.bss_conf.ht_operation_mode = ht_opmode;
  178. sdata->u.mesh.mshcfg.ht_opmode = ht_opmode;
  179. mpl_dbg(sdata, "selected new HT protection mode %d\n", ht_opmode);
  180. return BSS_CHANGED_HT;
  181. }
  182. /**
  183. * __mesh_plink_deactivate - deactivate mesh peer link
  184. *
  185. * @sta: mesh peer link to deactivate
  186. *
  187. * All mesh paths with this peer as next hop will be flushed
  188. * Returns beacon changed flag if the beacon content changed.
  189. *
  190. * Locking: the caller must hold sta->lock
  191. */
  192. static u32 __mesh_plink_deactivate(struct sta_info *sta)
  193. {
  194. struct ieee80211_sub_if_data *sdata = sta->sdata;
  195. u32 changed = 0;
  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->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->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 + 2);
  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. sband = local->hw.wiphy->bands[band];
  343. rates = ieee80211_sta_get_rates(sdata, elems, band, &basic_rates);
  344. spin_lock_bh(&sta->lock);
  345. sta->last_rx = jiffies;
  346. /* rates and capabilities don't change during peering */
  347. if (sta->plink_state == NL80211_PLINK_ESTAB)
  348. goto out;
  349. if (sta->sta.supp_rates[band] != rates)
  350. changed |= IEEE80211_RC_SUPP_RATES_CHANGED;
  351. sta->sta.supp_rates[band] = rates;
  352. if (ieee80211_ht_cap_ie_to_sta_ht_cap(sdata, sband,
  353. elems->ht_cap_elem, sta))
  354. changed |= IEEE80211_RC_BW_CHANGED;
  355. /* HT peer is operating 20MHz-only */
  356. if (elems->ht_operation &&
  357. !(elems->ht_operation->ht_param &
  358. IEEE80211_HT_PARAM_CHAN_WIDTH_ANY)) {
  359. if (sta->sta.bandwidth != IEEE80211_STA_RX_BW_20)
  360. changed |= IEEE80211_RC_BW_CHANGED;
  361. sta->sta.bandwidth = IEEE80211_STA_RX_BW_20;
  362. }
  363. if (insert)
  364. rate_control_rate_init(sta);
  365. else
  366. rate_control_rate_update(local, sband, sta, changed);
  367. out:
  368. spin_unlock_bh(&sta->lock);
  369. }
  370. static struct sta_info *
  371. __mesh_sta_info_alloc(struct ieee80211_sub_if_data *sdata, u8 *hw_addr)
  372. {
  373. struct sta_info *sta;
  374. if (sdata->local->num_sta >= MESH_MAX_PLINKS)
  375. return NULL;
  376. sta = sta_info_alloc(sdata, hw_addr, GFP_KERNEL);
  377. if (!sta)
  378. return NULL;
  379. sta->plink_state = NL80211_PLINK_LISTEN;
  380. sta_info_pre_move_state(sta, IEEE80211_STA_AUTH);
  381. sta_info_pre_move_state(sta, IEEE80211_STA_ASSOC);
  382. sta_info_pre_move_state(sta, IEEE80211_STA_AUTHORIZED);
  383. set_sta_flag(sta, WLAN_STA_WME);
  384. sta->sta.wme = true;
  385. return sta;
  386. }
  387. static struct sta_info *
  388. mesh_sta_info_alloc(struct ieee80211_sub_if_data *sdata, u8 *addr,
  389. struct ieee802_11_elems *elems)
  390. {
  391. struct sta_info *sta = NULL;
  392. /* Userspace handles station allocation */
  393. if (sdata->u.mesh.user_mpm ||
  394. sdata->u.mesh.security & IEEE80211_MESH_SEC_AUTHED)
  395. cfg80211_notify_new_peer_candidate(sdata->dev, addr,
  396. elems->ie_start,
  397. elems->total_len,
  398. GFP_KERNEL);
  399. else
  400. sta = __mesh_sta_info_alloc(sdata, addr);
  401. return sta;
  402. }
  403. /*
  404. * mesh_sta_info_get - return mesh sta info entry for @addr.
  405. *
  406. * @sdata: local meshif
  407. * @addr: peer's address
  408. * @elems: IEs from beacon or mesh peering frame.
  409. *
  410. * Return existing or newly allocated sta_info under RCU read lock.
  411. * (re)initialize with given IEs.
  412. */
  413. static struct sta_info *
  414. mesh_sta_info_get(struct ieee80211_sub_if_data *sdata,
  415. u8 *addr, struct ieee802_11_elems *elems) __acquires(RCU)
  416. {
  417. struct sta_info *sta = NULL;
  418. rcu_read_lock();
  419. sta = sta_info_get(sdata, addr);
  420. if (sta) {
  421. mesh_sta_info_init(sdata, sta, elems, false);
  422. } else {
  423. rcu_read_unlock();
  424. /* can't run atomic */
  425. sta = mesh_sta_info_alloc(sdata, addr, elems);
  426. if (!sta) {
  427. rcu_read_lock();
  428. return NULL;
  429. }
  430. mesh_sta_info_init(sdata, sta, elems, true);
  431. if (sta_info_insert_rcu(sta))
  432. return NULL;
  433. }
  434. return sta;
  435. }
  436. /*
  437. * mesh_neighbour_update - update or initialize new mesh neighbor.
  438. *
  439. * @sdata: local meshif
  440. * @addr: peer's address
  441. * @elems: IEs from beacon or mesh peering frame
  442. *
  443. * Initiates peering if appropriate.
  444. */
  445. void mesh_neighbour_update(struct ieee80211_sub_if_data *sdata,
  446. u8 *hw_addr,
  447. struct ieee802_11_elems *elems)
  448. {
  449. struct sta_info *sta;
  450. u32 changed = 0;
  451. sta = mesh_sta_info_get(sdata, hw_addr, elems);
  452. if (!sta)
  453. goto out;
  454. if (mesh_peer_accepts_plinks(elems) &&
  455. sta->plink_state == NL80211_PLINK_LISTEN &&
  456. sdata->u.mesh.accepting_plinks &&
  457. sdata->u.mesh.mshcfg.auto_open_plinks &&
  458. rssi_threshold_check(sdata, sta))
  459. changed = mesh_plink_open(sta);
  460. ieee80211_mps_frame_release(sta, elems);
  461. out:
  462. rcu_read_unlock();
  463. ieee80211_mbss_info_change_notify(sdata, changed);
  464. }
  465. static void mesh_plink_timer(unsigned long data)
  466. {
  467. struct sta_info *sta;
  468. u16 reason = 0;
  469. struct ieee80211_sub_if_data *sdata;
  470. struct mesh_config *mshcfg;
  471. enum ieee80211_self_protected_actioncode action = 0;
  472. /*
  473. * This STA is valid because sta_info_destroy() will
  474. * del_timer_sync() this timer after having made sure
  475. * it cannot be readded (by deleting the plink.)
  476. */
  477. sta = (struct sta_info *) data;
  478. if (sta->sdata->local->quiescing)
  479. return;
  480. spin_lock_bh(&sta->lock);
  481. if (sta->ignore_plink_timer) {
  482. sta->ignore_plink_timer = false;
  483. spin_unlock_bh(&sta->lock);
  484. return;
  485. }
  486. mpl_dbg(sta->sdata,
  487. "Mesh plink timer for %pM fired on state %s\n",
  488. sta->sta.addr, mplstates[sta->plink_state]);
  489. sdata = sta->sdata;
  490. mshcfg = &sdata->u.mesh.mshcfg;
  491. switch (sta->plink_state) {
  492. case NL80211_PLINK_OPN_RCVD:
  493. case NL80211_PLINK_OPN_SNT:
  494. /* retry timer */
  495. if (sta->plink_retries < mshcfg->dot11MeshMaxRetries) {
  496. u32 rand;
  497. mpl_dbg(sta->sdata,
  498. "Mesh plink for %pM (retry, timeout): %d %d\n",
  499. sta->sta.addr, sta->plink_retries,
  500. sta->plink_timeout);
  501. get_random_bytes(&rand, sizeof(u32));
  502. sta->plink_timeout = sta->plink_timeout +
  503. rand % sta->plink_timeout;
  504. ++sta->plink_retries;
  505. mod_plink_timer(sta, sta->plink_timeout);
  506. action = WLAN_SP_MESH_PEERING_OPEN;
  507. break;
  508. }
  509. reason = WLAN_REASON_MESH_MAX_RETRIES;
  510. /* fall through on else */
  511. case NL80211_PLINK_CNF_RCVD:
  512. /* confirm timer */
  513. if (!reason)
  514. reason = WLAN_REASON_MESH_CONFIRM_TIMEOUT;
  515. sta->plink_state = NL80211_PLINK_HOLDING;
  516. mod_plink_timer(sta, mshcfg->dot11MeshHoldingTimeout);
  517. action = WLAN_SP_MESH_PEERING_CLOSE;
  518. break;
  519. case NL80211_PLINK_HOLDING:
  520. /* holding timer */
  521. del_timer(&sta->plink_timer);
  522. mesh_plink_fsm_restart(sta);
  523. break;
  524. default:
  525. break;
  526. }
  527. spin_unlock_bh(&sta->lock);
  528. if (action)
  529. mesh_plink_frame_tx(sdata, action, sta->sta.addr,
  530. sta->llid, sta->plid, reason);
  531. }
  532. static inline void mesh_plink_timer_set(struct sta_info *sta, int timeout)
  533. {
  534. sta->plink_timer.expires = jiffies + (HZ * timeout / 1000);
  535. sta->plink_timer.data = (unsigned long) sta;
  536. sta->plink_timer.function = mesh_plink_timer;
  537. sta->plink_timeout = timeout;
  538. add_timer(&sta->plink_timer);
  539. }
  540. static bool llid_in_use(struct ieee80211_sub_if_data *sdata,
  541. u16 llid)
  542. {
  543. struct ieee80211_local *local = sdata->local;
  544. bool in_use = false;
  545. struct sta_info *sta;
  546. rcu_read_lock();
  547. list_for_each_entry_rcu(sta, &local->sta_list, list) {
  548. if (!memcmp(&sta->llid, &llid, sizeof(llid))) {
  549. in_use = true;
  550. break;
  551. }
  552. }
  553. rcu_read_unlock();
  554. return in_use;
  555. }
  556. static u16 mesh_get_new_llid(struct ieee80211_sub_if_data *sdata)
  557. {
  558. u16 llid;
  559. do {
  560. get_random_bytes(&llid, sizeof(llid));
  561. /* for mesh PS we still only have the AID range for TIM bits */
  562. llid = (llid % IEEE80211_MAX_AID) + 1;
  563. } while (llid_in_use(sdata, llid));
  564. return llid;
  565. }
  566. u32 mesh_plink_open(struct sta_info *sta)
  567. {
  568. struct ieee80211_sub_if_data *sdata = sta->sdata;
  569. u32 changed;
  570. if (!test_sta_flag(sta, WLAN_STA_AUTH))
  571. return 0;
  572. spin_lock_bh(&sta->lock);
  573. sta->llid = mesh_get_new_llid(sdata);
  574. if (sta->plink_state != NL80211_PLINK_LISTEN &&
  575. sta->plink_state != NL80211_PLINK_BLOCKED) {
  576. spin_unlock_bh(&sta->lock);
  577. return 0;
  578. }
  579. sta->plink_state = NL80211_PLINK_OPN_SNT;
  580. mesh_plink_timer_set(sta, sdata->u.mesh.mshcfg.dot11MeshRetryTimeout);
  581. spin_unlock_bh(&sta->lock);
  582. mpl_dbg(sdata,
  583. "Mesh plink: starting establishment with %pM\n",
  584. sta->sta.addr);
  585. /* set the non-peer mode to active during peering */
  586. changed = ieee80211_mps_local_status_update(sdata);
  587. mesh_plink_frame_tx(sdata, WLAN_SP_MESH_PEERING_OPEN,
  588. sta->sta.addr, sta->llid, 0, 0);
  589. return changed;
  590. }
  591. u32 mesh_plink_block(struct sta_info *sta)
  592. {
  593. u32 changed;
  594. spin_lock_bh(&sta->lock);
  595. changed = __mesh_plink_deactivate(sta);
  596. sta->plink_state = NL80211_PLINK_BLOCKED;
  597. spin_unlock_bh(&sta->lock);
  598. return changed;
  599. }
  600. static void mesh_plink_close(struct ieee80211_sub_if_data *sdata,
  601. struct sta_info *sta,
  602. enum plink_event event)
  603. {
  604. struct mesh_config *mshcfg = &sdata->u.mesh.mshcfg;
  605. u16 reason = (event == CLS_ACPT) ?
  606. WLAN_REASON_MESH_CLOSE : WLAN_REASON_MESH_CONFIG;
  607. sta->reason = reason;
  608. sta->plink_state = NL80211_PLINK_HOLDING;
  609. mod_plink_timer(sta, mshcfg->dot11MeshHoldingTimeout);
  610. }
  611. static u32 mesh_plink_establish(struct ieee80211_sub_if_data *sdata,
  612. struct sta_info *sta)
  613. {
  614. struct mesh_config *mshcfg = &sdata->u.mesh.mshcfg;
  615. u32 changed = 0;
  616. del_timer(&sta->plink_timer);
  617. sta->plink_state = NL80211_PLINK_ESTAB;
  618. changed |= mesh_plink_inc_estab_count(sdata);
  619. changed |= mesh_set_ht_prot_mode(sdata);
  620. changed |= mesh_set_short_slot_time(sdata);
  621. mpl_dbg(sdata, "Mesh plink with %pM ESTABLISHED\n", sta->sta.addr);
  622. ieee80211_mps_sta_status_update(sta);
  623. changed |= ieee80211_mps_set_sta_local_pm(sta, mshcfg->power_mode);
  624. return changed;
  625. }
  626. /**
  627. * mesh_plink_fsm - step @sta MPM based on @event
  628. *
  629. * @sdata: interface
  630. * @sta: mesh neighbor
  631. * @event: peering event
  632. *
  633. * Return: changed MBSS flags
  634. */
  635. static u32 mesh_plink_fsm(struct ieee80211_sub_if_data *sdata,
  636. struct sta_info *sta, enum plink_event event)
  637. {
  638. struct mesh_config *mshcfg = &sdata->u.mesh.mshcfg;
  639. enum ieee80211_self_protected_actioncode action = 0;
  640. u32 changed = 0;
  641. mpl_dbg(sdata, "peer %pM in state %s got event %s\n", sta->sta.addr,
  642. mplstates[sta->plink_state], mplevents[event]);
  643. spin_lock_bh(&sta->lock);
  644. switch (sta->plink_state) {
  645. case NL80211_PLINK_LISTEN:
  646. switch (event) {
  647. case CLS_ACPT:
  648. mesh_plink_fsm_restart(sta);
  649. break;
  650. case OPN_ACPT:
  651. sta->plink_state = NL80211_PLINK_OPN_RCVD;
  652. sta->llid = mesh_get_new_llid(sdata);
  653. mesh_plink_timer_set(sta,
  654. mshcfg->dot11MeshRetryTimeout);
  655. /* set the non-peer mode to active during peering */
  656. changed |= ieee80211_mps_local_status_update(sdata);
  657. action = WLAN_SP_MESH_PEERING_OPEN;
  658. break;
  659. default:
  660. break;
  661. }
  662. break;
  663. case NL80211_PLINK_OPN_SNT:
  664. switch (event) {
  665. case OPN_RJCT:
  666. case CNF_RJCT:
  667. case CLS_ACPT:
  668. mesh_plink_close(sdata, sta, event);
  669. action = WLAN_SP_MESH_PEERING_CLOSE;
  670. break;
  671. case OPN_ACPT:
  672. /* retry timer is left untouched */
  673. sta->plink_state = NL80211_PLINK_OPN_RCVD;
  674. action = WLAN_SP_MESH_PEERING_CONFIRM;
  675. break;
  676. case CNF_ACPT:
  677. sta->plink_state = NL80211_PLINK_CNF_RCVD;
  678. if (!mod_plink_timer(sta,
  679. mshcfg->dot11MeshConfirmTimeout))
  680. sta->ignore_plink_timer = true;
  681. break;
  682. default:
  683. break;
  684. }
  685. break;
  686. case NL80211_PLINK_OPN_RCVD:
  687. switch (event) {
  688. case OPN_RJCT:
  689. case CNF_RJCT:
  690. case CLS_ACPT:
  691. mesh_plink_close(sdata, sta, event);
  692. action = WLAN_SP_MESH_PEERING_CLOSE;
  693. break;
  694. case OPN_ACPT:
  695. action = WLAN_SP_MESH_PEERING_CONFIRM;
  696. break;
  697. case CNF_ACPT:
  698. changed |= mesh_plink_establish(sdata, sta);
  699. break;
  700. default:
  701. break;
  702. }
  703. break;
  704. case NL80211_PLINK_CNF_RCVD:
  705. switch (event) {
  706. case OPN_RJCT:
  707. case CNF_RJCT:
  708. case CLS_ACPT:
  709. mesh_plink_close(sdata, sta, event);
  710. action = WLAN_SP_MESH_PEERING_CLOSE;
  711. break;
  712. case OPN_ACPT:
  713. changed |= mesh_plink_establish(sdata, sta);
  714. action = WLAN_SP_MESH_PEERING_CONFIRM;
  715. break;
  716. default:
  717. break;
  718. }
  719. break;
  720. case NL80211_PLINK_ESTAB:
  721. switch (event) {
  722. case CLS_ACPT:
  723. changed |= __mesh_plink_deactivate(sta);
  724. changed |= mesh_set_ht_prot_mode(sdata);
  725. changed |= mesh_set_short_slot_time(sdata);
  726. mesh_plink_close(sdata, sta, event);
  727. action = WLAN_SP_MESH_PEERING_CLOSE;
  728. break;
  729. case OPN_ACPT:
  730. action = WLAN_SP_MESH_PEERING_CONFIRM;
  731. break;
  732. default:
  733. break;
  734. }
  735. break;
  736. case NL80211_PLINK_HOLDING:
  737. switch (event) {
  738. case CLS_ACPT:
  739. if (del_timer(&sta->plink_timer))
  740. sta->ignore_plink_timer = 1;
  741. mesh_plink_fsm_restart(sta);
  742. break;
  743. case OPN_ACPT:
  744. case CNF_ACPT:
  745. case OPN_RJCT:
  746. case CNF_RJCT:
  747. action = WLAN_SP_MESH_PEERING_CLOSE;
  748. break;
  749. default:
  750. break;
  751. }
  752. break;
  753. default:
  754. /* should not get here, PLINK_BLOCKED is dealt with at the
  755. * beginning of the function
  756. */
  757. break;
  758. }
  759. spin_unlock_bh(&sta->lock);
  760. if (action) {
  761. mesh_plink_frame_tx(sdata, action, sta->sta.addr,
  762. sta->llid, sta->plid, sta->reason);
  763. /* also send confirm in open case */
  764. if (action == WLAN_SP_MESH_PEERING_OPEN) {
  765. mesh_plink_frame_tx(sdata,
  766. WLAN_SP_MESH_PEERING_CONFIRM,
  767. sta->sta.addr, sta->llid,
  768. sta->plid, 0);
  769. }
  770. }
  771. return changed;
  772. }
  773. /*
  774. * mesh_plink_get_event - get correct MPM event
  775. *
  776. * @sdata: interface
  777. * @sta: peer, leave NULL if processing a frame from a new suitable peer
  778. * @elems: peering management IEs
  779. * @ftype: frame type
  780. * @llid: peer's peer link ID
  781. * @plid: peer's local link ID
  782. *
  783. * Return: new peering event for @sta, but PLINK_UNDEFINED should be treated as
  784. * an error.
  785. */
  786. static enum plink_event
  787. mesh_plink_get_event(struct ieee80211_sub_if_data *sdata,
  788. struct sta_info *sta,
  789. struct ieee802_11_elems *elems,
  790. enum ieee80211_self_protected_actioncode ftype,
  791. u16 llid, u16 plid)
  792. {
  793. enum plink_event event = PLINK_UNDEFINED;
  794. u8 ie_len = elems->peering_len;
  795. bool matches_local;
  796. matches_local = (ftype == WLAN_SP_MESH_PEERING_CLOSE ||
  797. mesh_matches_local(sdata, elems));
  798. /* deny open request from non-matching peer */
  799. if (!matches_local && !sta) {
  800. event = OPN_RJCT;
  801. goto out;
  802. }
  803. if (!sta) {
  804. if (ftype != WLAN_SP_MESH_PEERING_OPEN) {
  805. mpl_dbg(sdata, "Mesh plink: cls or cnf from unknown peer\n");
  806. goto out;
  807. }
  808. /* ftype == WLAN_SP_MESH_PEERING_OPEN */
  809. if (!mesh_plink_free_count(sdata)) {
  810. mpl_dbg(sdata, "Mesh plink error: no more free plinks\n");
  811. goto out;
  812. }
  813. } else {
  814. if (!test_sta_flag(sta, WLAN_STA_AUTH)) {
  815. mpl_dbg(sdata, "Mesh plink: Action frame from non-authed peer\n");
  816. goto out;
  817. }
  818. if (sta->plink_state == NL80211_PLINK_BLOCKED)
  819. goto out;
  820. }
  821. /* new matching peer */
  822. if (!sta) {
  823. event = OPN_ACPT;
  824. goto out;
  825. }
  826. switch (ftype) {
  827. case WLAN_SP_MESH_PEERING_OPEN:
  828. if (!matches_local)
  829. event = OPN_RJCT;
  830. if (!mesh_plink_free_count(sdata) ||
  831. (sta->plid && sta->plid != plid))
  832. event = OPN_IGNR;
  833. else
  834. event = OPN_ACPT;
  835. break;
  836. case WLAN_SP_MESH_PEERING_CONFIRM:
  837. if (!matches_local)
  838. event = CNF_RJCT;
  839. if (!mesh_plink_free_count(sdata) ||
  840. (sta->llid != llid || sta->plid != plid))
  841. event = CNF_IGNR;
  842. else
  843. event = CNF_ACPT;
  844. break;
  845. case WLAN_SP_MESH_PEERING_CLOSE:
  846. if (sta->plink_state == NL80211_PLINK_ESTAB)
  847. /* Do not check for llid or plid. This does not
  848. * follow the standard but since multiple plinks
  849. * per sta are not supported, it is necessary in
  850. * order to avoid a livelock when MP A sees an
  851. * establish peer link to MP B but MP B does not
  852. * see it. This can be caused by a timeout in
  853. * B's peer link establishment or B beign
  854. * restarted.
  855. */
  856. event = CLS_ACPT;
  857. else if (sta->plid != plid)
  858. event = CLS_IGNR;
  859. else if (ie_len == 8 && sta->llid != llid)
  860. event = CLS_IGNR;
  861. else
  862. event = CLS_ACPT;
  863. break;
  864. default:
  865. mpl_dbg(sdata, "Mesh plink: unknown frame subtype\n");
  866. break;
  867. }
  868. out:
  869. return event;
  870. }
  871. static void
  872. mesh_process_plink_frame(struct ieee80211_sub_if_data *sdata,
  873. struct ieee80211_mgmt *mgmt,
  874. struct ieee802_11_elems *elems)
  875. {
  876. struct sta_info *sta;
  877. enum plink_event event;
  878. enum ieee80211_self_protected_actioncode ftype;
  879. u32 changed = 0;
  880. u8 ie_len = elems->peering_len;
  881. __le16 _plid, _llid;
  882. u16 plid, llid = 0;
  883. if (!elems->peering) {
  884. mpl_dbg(sdata,
  885. "Mesh plink: missing necessary peer link ie\n");
  886. return;
  887. }
  888. if (elems->rsn_len &&
  889. sdata->u.mesh.security == IEEE80211_MESH_SEC_NONE) {
  890. mpl_dbg(sdata,
  891. "Mesh plink: can't establish link with secure peer\n");
  892. return;
  893. }
  894. ftype = mgmt->u.action.u.self_prot.action_code;
  895. if ((ftype == WLAN_SP_MESH_PEERING_OPEN && ie_len != 4) ||
  896. (ftype == WLAN_SP_MESH_PEERING_CONFIRM && ie_len != 6) ||
  897. (ftype == WLAN_SP_MESH_PEERING_CLOSE && ie_len != 6
  898. && ie_len != 8)) {
  899. mpl_dbg(sdata,
  900. "Mesh plink: incorrect plink ie length %d %d\n",
  901. ftype, ie_len);
  902. return;
  903. }
  904. if (ftype != WLAN_SP_MESH_PEERING_CLOSE &&
  905. (!elems->mesh_id || !elems->mesh_config)) {
  906. mpl_dbg(sdata, "Mesh plink: missing necessary ie\n");
  907. return;
  908. }
  909. /* Note the lines below are correct, the llid in the frame is the plid
  910. * from the point of view of this host.
  911. */
  912. memcpy(&_plid, PLINK_GET_LLID(elems->peering), sizeof(__le16));
  913. plid = le16_to_cpu(_plid);
  914. if (ftype == WLAN_SP_MESH_PEERING_CONFIRM ||
  915. (ftype == WLAN_SP_MESH_PEERING_CLOSE && ie_len == 8)) {
  916. memcpy(&_llid, PLINK_GET_PLID(elems->peering), sizeof(__le16));
  917. llid = le16_to_cpu(_llid);
  918. }
  919. /* WARNING: Only for sta pointer, is dropped & re-acquired */
  920. rcu_read_lock();
  921. sta = sta_info_get(sdata, mgmt->sa);
  922. if (ftype == WLAN_SP_MESH_PEERING_OPEN &&
  923. !rssi_threshold_check(sdata, sta)) {
  924. mpl_dbg(sdata, "Mesh plink: %pM does not meet rssi threshold\n",
  925. mgmt->sa);
  926. goto unlock_rcu;
  927. }
  928. /* Now we will figure out the appropriate event... */
  929. event = mesh_plink_get_event(sdata, sta, elems, ftype, llid, plid);
  930. if (event == OPN_ACPT) {
  931. rcu_read_unlock();
  932. /* allocate sta entry if necessary and update info */
  933. sta = mesh_sta_info_get(sdata, mgmt->sa, elems);
  934. if (!sta) {
  935. mpl_dbg(sdata, "Mesh plink: failed to init peer!\n");
  936. goto unlock_rcu;
  937. }
  938. sta->plid = plid;
  939. } else if (!sta && event == OPN_RJCT) {
  940. mesh_plink_frame_tx(sdata, WLAN_SP_MESH_PEERING_CLOSE,
  941. mgmt->sa, 0, plid,
  942. WLAN_REASON_MESH_CONFIG);
  943. goto unlock_rcu;
  944. } else if (!sta || event == PLINK_UNDEFINED) {
  945. /* something went wrong */
  946. goto unlock_rcu;
  947. }
  948. changed |= mesh_plink_fsm(sdata, sta, event);
  949. unlock_rcu:
  950. rcu_read_unlock();
  951. if (changed)
  952. ieee80211_mbss_info_change_notify(sdata, changed);
  953. }
  954. void mesh_rx_plink_frame(struct ieee80211_sub_if_data *sdata,
  955. struct ieee80211_mgmt *mgmt, size_t len,
  956. struct ieee80211_rx_status *rx_status)
  957. {
  958. struct ieee802_11_elems elems;
  959. size_t baselen;
  960. u8 *baseaddr;
  961. /* need action_code, aux */
  962. if (len < IEEE80211_MIN_ACTION_SIZE + 3)
  963. return;
  964. if (sdata->u.mesh.user_mpm)
  965. /* userspace must register for these */
  966. return;
  967. if (is_multicast_ether_addr(mgmt->da)) {
  968. mpl_dbg(sdata,
  969. "Mesh plink: ignore frame from multicast address\n");
  970. return;
  971. }
  972. baseaddr = mgmt->u.action.u.self_prot.variable;
  973. baselen = (u8 *) mgmt->u.action.u.self_prot.variable - (u8 *) mgmt;
  974. if (mgmt->u.action.u.self_prot.action_code ==
  975. WLAN_SP_MESH_PEERING_CONFIRM) {
  976. baseaddr += 4;
  977. baselen += 4;
  978. }
  979. ieee802_11_parse_elems(baseaddr, len - baselen, true, &elems);
  980. mesh_process_plink_frame(sdata, mgmt, &elems);
  981. }