mesh_plink.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122
  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->sta.wme = true;
  381. sta_info_pre_move_state(sta, IEEE80211_STA_AUTH);
  382. sta_info_pre_move_state(sta, IEEE80211_STA_ASSOC);
  383. sta_info_pre_move_state(sta, IEEE80211_STA_AUTHORIZED);
  384. return sta;
  385. }
  386. static struct sta_info *
  387. mesh_sta_info_alloc(struct ieee80211_sub_if_data *sdata, u8 *addr,
  388. struct ieee802_11_elems *elems)
  389. {
  390. struct sta_info *sta = NULL;
  391. /* Userspace handles station allocation */
  392. if (sdata->u.mesh.user_mpm ||
  393. sdata->u.mesh.security & IEEE80211_MESH_SEC_AUTHED)
  394. cfg80211_notify_new_peer_candidate(sdata->dev, addr,
  395. elems->ie_start,
  396. elems->total_len,
  397. GFP_KERNEL);
  398. else
  399. sta = __mesh_sta_info_alloc(sdata, addr);
  400. return sta;
  401. }
  402. /*
  403. * mesh_sta_info_get - return mesh sta info entry for @addr.
  404. *
  405. * @sdata: local meshif
  406. * @addr: peer's address
  407. * @elems: IEs from beacon or mesh peering frame.
  408. *
  409. * Return existing or newly allocated sta_info under RCU read lock.
  410. * (re)initialize with given IEs.
  411. */
  412. static struct sta_info *
  413. mesh_sta_info_get(struct ieee80211_sub_if_data *sdata,
  414. u8 *addr, struct ieee802_11_elems *elems) __acquires(RCU)
  415. {
  416. struct sta_info *sta = NULL;
  417. rcu_read_lock();
  418. sta = sta_info_get(sdata, addr);
  419. if (sta) {
  420. mesh_sta_info_init(sdata, sta, elems, false);
  421. } else {
  422. rcu_read_unlock();
  423. /* can't run atomic */
  424. sta = mesh_sta_info_alloc(sdata, addr, elems);
  425. if (!sta) {
  426. rcu_read_lock();
  427. return NULL;
  428. }
  429. mesh_sta_info_init(sdata, sta, elems, true);
  430. if (sta_info_insert_rcu(sta))
  431. return NULL;
  432. }
  433. return sta;
  434. }
  435. /*
  436. * mesh_neighbour_update - update or initialize new mesh neighbor.
  437. *
  438. * @sdata: local meshif
  439. * @addr: peer's address
  440. * @elems: IEs from beacon or mesh peering frame
  441. *
  442. * Initiates peering if appropriate.
  443. */
  444. void mesh_neighbour_update(struct ieee80211_sub_if_data *sdata,
  445. u8 *hw_addr,
  446. struct ieee802_11_elems *elems)
  447. {
  448. struct sta_info *sta;
  449. u32 changed = 0;
  450. sta = mesh_sta_info_get(sdata, hw_addr, elems);
  451. if (!sta)
  452. goto out;
  453. if (mesh_peer_accepts_plinks(elems) &&
  454. sta->plink_state == NL80211_PLINK_LISTEN &&
  455. sdata->u.mesh.accepting_plinks &&
  456. sdata->u.mesh.mshcfg.auto_open_plinks &&
  457. rssi_threshold_check(sdata, sta))
  458. changed = mesh_plink_open(sta);
  459. ieee80211_mps_frame_release(sta, elems);
  460. out:
  461. rcu_read_unlock();
  462. ieee80211_mbss_info_change_notify(sdata, changed);
  463. }
  464. static void mesh_plink_timer(unsigned long data)
  465. {
  466. struct sta_info *sta;
  467. u16 reason = 0;
  468. struct ieee80211_sub_if_data *sdata;
  469. struct mesh_config *mshcfg;
  470. enum ieee80211_self_protected_actioncode action = 0;
  471. /*
  472. * This STA is valid because sta_info_destroy() will
  473. * del_timer_sync() this timer after having made sure
  474. * it cannot be readded (by deleting the plink.)
  475. */
  476. sta = (struct sta_info *) data;
  477. if (sta->sdata->local->quiescing)
  478. return;
  479. spin_lock_bh(&sta->lock);
  480. /* If a timer fires just before a state transition on another CPU,
  481. * we may have already extended the timeout and changed state by the
  482. * time we've acquired the lock and arrived here. In that case,
  483. * skip this timer and wait for the new one.
  484. */
  485. if (time_before(jiffies, sta->plink_timer.expires)) {
  486. mpl_dbg(sta->sdata,
  487. "Ignoring timer for %pM in state %s (timer adjusted)",
  488. sta->sta.addr, mplstates[sta->plink_state]);
  489. spin_unlock_bh(&sta->lock);
  490. return;
  491. }
  492. /* del_timer() and handler may race when entering these states */
  493. if (sta->plink_state == NL80211_PLINK_LISTEN ||
  494. sta->plink_state == NL80211_PLINK_ESTAB) {
  495. mpl_dbg(sta->sdata,
  496. "Ignoring timer for %pM in state %s (timer deleted)",
  497. sta->sta.addr, mplstates[sta->plink_state]);
  498. spin_unlock_bh(&sta->lock);
  499. return;
  500. }
  501. mpl_dbg(sta->sdata,
  502. "Mesh plink timer for %pM fired on state %s\n",
  503. sta->sta.addr, mplstates[sta->plink_state]);
  504. sdata = sta->sdata;
  505. mshcfg = &sdata->u.mesh.mshcfg;
  506. switch (sta->plink_state) {
  507. case NL80211_PLINK_OPN_RCVD:
  508. case NL80211_PLINK_OPN_SNT:
  509. /* retry timer */
  510. if (sta->plink_retries < mshcfg->dot11MeshMaxRetries) {
  511. u32 rand;
  512. mpl_dbg(sta->sdata,
  513. "Mesh plink for %pM (retry, timeout): %d %d\n",
  514. sta->sta.addr, sta->plink_retries,
  515. sta->plink_timeout);
  516. get_random_bytes(&rand, sizeof(u32));
  517. sta->plink_timeout = sta->plink_timeout +
  518. rand % sta->plink_timeout;
  519. ++sta->plink_retries;
  520. mod_plink_timer(sta, sta->plink_timeout);
  521. action = WLAN_SP_MESH_PEERING_OPEN;
  522. break;
  523. }
  524. reason = WLAN_REASON_MESH_MAX_RETRIES;
  525. /* fall through on else */
  526. case NL80211_PLINK_CNF_RCVD:
  527. /* confirm timer */
  528. if (!reason)
  529. reason = WLAN_REASON_MESH_CONFIRM_TIMEOUT;
  530. sta->plink_state = NL80211_PLINK_HOLDING;
  531. mod_plink_timer(sta, mshcfg->dot11MeshHoldingTimeout);
  532. action = WLAN_SP_MESH_PEERING_CLOSE;
  533. break;
  534. case NL80211_PLINK_HOLDING:
  535. /* holding timer */
  536. del_timer(&sta->plink_timer);
  537. mesh_plink_fsm_restart(sta);
  538. break;
  539. default:
  540. break;
  541. }
  542. spin_unlock_bh(&sta->lock);
  543. if (action)
  544. mesh_plink_frame_tx(sdata, action, sta->sta.addr,
  545. sta->llid, sta->plid, reason);
  546. }
  547. static inline void mesh_plink_timer_set(struct sta_info *sta, int timeout)
  548. {
  549. sta->plink_timer.expires = jiffies + (HZ * timeout / 1000);
  550. sta->plink_timer.data = (unsigned long) sta;
  551. sta->plink_timer.function = mesh_plink_timer;
  552. sta->plink_timeout = timeout;
  553. add_timer(&sta->plink_timer);
  554. }
  555. static bool llid_in_use(struct ieee80211_sub_if_data *sdata,
  556. u16 llid)
  557. {
  558. struct ieee80211_local *local = sdata->local;
  559. bool in_use = false;
  560. struct sta_info *sta;
  561. rcu_read_lock();
  562. list_for_each_entry_rcu(sta, &local->sta_list, list) {
  563. if (!memcmp(&sta->llid, &llid, sizeof(llid))) {
  564. in_use = true;
  565. break;
  566. }
  567. }
  568. rcu_read_unlock();
  569. return in_use;
  570. }
  571. static u16 mesh_get_new_llid(struct ieee80211_sub_if_data *sdata)
  572. {
  573. u16 llid;
  574. do {
  575. get_random_bytes(&llid, sizeof(llid));
  576. /* for mesh PS we still only have the AID range for TIM bits */
  577. llid = (llid % IEEE80211_MAX_AID) + 1;
  578. } while (llid_in_use(sdata, llid));
  579. return llid;
  580. }
  581. u32 mesh_plink_open(struct sta_info *sta)
  582. {
  583. struct ieee80211_sub_if_data *sdata = sta->sdata;
  584. u32 changed;
  585. if (!test_sta_flag(sta, WLAN_STA_AUTH))
  586. return 0;
  587. spin_lock_bh(&sta->lock);
  588. sta->llid = mesh_get_new_llid(sdata);
  589. if (sta->plink_state != NL80211_PLINK_LISTEN &&
  590. sta->plink_state != NL80211_PLINK_BLOCKED) {
  591. spin_unlock_bh(&sta->lock);
  592. return 0;
  593. }
  594. sta->plink_state = NL80211_PLINK_OPN_SNT;
  595. mesh_plink_timer_set(sta, sdata->u.mesh.mshcfg.dot11MeshRetryTimeout);
  596. spin_unlock_bh(&sta->lock);
  597. mpl_dbg(sdata,
  598. "Mesh plink: starting establishment with %pM\n",
  599. sta->sta.addr);
  600. /* set the non-peer mode to active during peering */
  601. changed = ieee80211_mps_local_status_update(sdata);
  602. mesh_plink_frame_tx(sdata, WLAN_SP_MESH_PEERING_OPEN,
  603. sta->sta.addr, sta->llid, 0, 0);
  604. return changed;
  605. }
  606. u32 mesh_plink_block(struct sta_info *sta)
  607. {
  608. u32 changed;
  609. spin_lock_bh(&sta->lock);
  610. changed = __mesh_plink_deactivate(sta);
  611. sta->plink_state = NL80211_PLINK_BLOCKED;
  612. spin_unlock_bh(&sta->lock);
  613. return changed;
  614. }
  615. static void mesh_plink_close(struct ieee80211_sub_if_data *sdata,
  616. struct sta_info *sta,
  617. enum plink_event event)
  618. {
  619. struct mesh_config *mshcfg = &sdata->u.mesh.mshcfg;
  620. u16 reason = (event == CLS_ACPT) ?
  621. WLAN_REASON_MESH_CLOSE : WLAN_REASON_MESH_CONFIG;
  622. sta->reason = reason;
  623. sta->plink_state = NL80211_PLINK_HOLDING;
  624. mod_plink_timer(sta, mshcfg->dot11MeshHoldingTimeout);
  625. }
  626. static u32 mesh_plink_establish(struct ieee80211_sub_if_data *sdata,
  627. struct sta_info *sta)
  628. {
  629. struct mesh_config *mshcfg = &sdata->u.mesh.mshcfg;
  630. u32 changed = 0;
  631. del_timer(&sta->plink_timer);
  632. sta->plink_state = NL80211_PLINK_ESTAB;
  633. changed |= mesh_plink_inc_estab_count(sdata);
  634. changed |= mesh_set_ht_prot_mode(sdata);
  635. changed |= mesh_set_short_slot_time(sdata);
  636. mpl_dbg(sdata, "Mesh plink with %pM ESTABLISHED\n", sta->sta.addr);
  637. ieee80211_mps_sta_status_update(sta);
  638. changed |= ieee80211_mps_set_sta_local_pm(sta, mshcfg->power_mode);
  639. return changed;
  640. }
  641. /**
  642. * mesh_plink_fsm - step @sta MPM based on @event
  643. *
  644. * @sdata: interface
  645. * @sta: mesh neighbor
  646. * @event: peering event
  647. *
  648. * Return: changed MBSS flags
  649. */
  650. static u32 mesh_plink_fsm(struct ieee80211_sub_if_data *sdata,
  651. struct sta_info *sta, enum plink_event event)
  652. {
  653. struct mesh_config *mshcfg = &sdata->u.mesh.mshcfg;
  654. enum ieee80211_self_protected_actioncode action = 0;
  655. u32 changed = 0;
  656. mpl_dbg(sdata, "peer %pM in state %s got event %s\n", sta->sta.addr,
  657. mplstates[sta->plink_state], mplevents[event]);
  658. spin_lock_bh(&sta->lock);
  659. switch (sta->plink_state) {
  660. case NL80211_PLINK_LISTEN:
  661. switch (event) {
  662. case CLS_ACPT:
  663. mesh_plink_fsm_restart(sta);
  664. break;
  665. case OPN_ACPT:
  666. sta->plink_state = NL80211_PLINK_OPN_RCVD;
  667. sta->llid = mesh_get_new_llid(sdata);
  668. mesh_plink_timer_set(sta,
  669. mshcfg->dot11MeshRetryTimeout);
  670. /* set the non-peer mode to active during peering */
  671. changed |= ieee80211_mps_local_status_update(sdata);
  672. action = WLAN_SP_MESH_PEERING_OPEN;
  673. break;
  674. default:
  675. break;
  676. }
  677. break;
  678. case NL80211_PLINK_OPN_SNT:
  679. switch (event) {
  680. case OPN_RJCT:
  681. case CNF_RJCT:
  682. case CLS_ACPT:
  683. mesh_plink_close(sdata, sta, event);
  684. action = WLAN_SP_MESH_PEERING_CLOSE;
  685. break;
  686. case OPN_ACPT:
  687. /* retry timer is left untouched */
  688. sta->plink_state = NL80211_PLINK_OPN_RCVD;
  689. action = WLAN_SP_MESH_PEERING_CONFIRM;
  690. break;
  691. case CNF_ACPT:
  692. sta->plink_state = NL80211_PLINK_CNF_RCVD;
  693. mod_plink_timer(sta, mshcfg->dot11MeshConfirmTimeout);
  694. break;
  695. default:
  696. break;
  697. }
  698. break;
  699. case NL80211_PLINK_OPN_RCVD:
  700. switch (event) {
  701. case OPN_RJCT:
  702. case CNF_RJCT:
  703. case CLS_ACPT:
  704. mesh_plink_close(sdata, sta, event);
  705. action = WLAN_SP_MESH_PEERING_CLOSE;
  706. break;
  707. case OPN_ACPT:
  708. action = WLAN_SP_MESH_PEERING_CONFIRM;
  709. break;
  710. case CNF_ACPT:
  711. changed |= mesh_plink_establish(sdata, sta);
  712. break;
  713. default:
  714. break;
  715. }
  716. break;
  717. case NL80211_PLINK_CNF_RCVD:
  718. switch (event) {
  719. case OPN_RJCT:
  720. case CNF_RJCT:
  721. case CLS_ACPT:
  722. mesh_plink_close(sdata, sta, event);
  723. action = WLAN_SP_MESH_PEERING_CLOSE;
  724. break;
  725. case OPN_ACPT:
  726. changed |= mesh_plink_establish(sdata, sta);
  727. action = WLAN_SP_MESH_PEERING_CONFIRM;
  728. break;
  729. default:
  730. break;
  731. }
  732. break;
  733. case NL80211_PLINK_ESTAB:
  734. switch (event) {
  735. case CLS_ACPT:
  736. changed |= __mesh_plink_deactivate(sta);
  737. changed |= mesh_set_ht_prot_mode(sdata);
  738. changed |= mesh_set_short_slot_time(sdata);
  739. mesh_plink_close(sdata, sta, event);
  740. action = WLAN_SP_MESH_PEERING_CLOSE;
  741. break;
  742. case OPN_ACPT:
  743. action = WLAN_SP_MESH_PEERING_CONFIRM;
  744. break;
  745. default:
  746. break;
  747. }
  748. break;
  749. case NL80211_PLINK_HOLDING:
  750. switch (event) {
  751. case CLS_ACPT:
  752. del_timer(&sta->plink_timer);
  753. mesh_plink_fsm_restart(sta);
  754. break;
  755. case OPN_ACPT:
  756. case CNF_ACPT:
  757. case OPN_RJCT:
  758. case CNF_RJCT:
  759. action = WLAN_SP_MESH_PEERING_CLOSE;
  760. break;
  761. default:
  762. break;
  763. }
  764. break;
  765. default:
  766. /* should not get here, PLINK_BLOCKED is dealt with at the
  767. * beginning of the function
  768. */
  769. break;
  770. }
  771. spin_unlock_bh(&sta->lock);
  772. if (action) {
  773. mesh_plink_frame_tx(sdata, action, sta->sta.addr,
  774. sta->llid, sta->plid, sta->reason);
  775. /* also send confirm in open case */
  776. if (action == WLAN_SP_MESH_PEERING_OPEN) {
  777. mesh_plink_frame_tx(sdata,
  778. WLAN_SP_MESH_PEERING_CONFIRM,
  779. sta->sta.addr, sta->llid,
  780. sta->plid, 0);
  781. }
  782. }
  783. return changed;
  784. }
  785. /*
  786. * mesh_plink_get_event - get correct MPM event
  787. *
  788. * @sdata: interface
  789. * @sta: peer, leave NULL if processing a frame from a new suitable peer
  790. * @elems: peering management IEs
  791. * @ftype: frame type
  792. * @llid: peer's peer link ID
  793. * @plid: peer's local link ID
  794. *
  795. * Return: new peering event for @sta, but PLINK_UNDEFINED should be treated as
  796. * an error.
  797. */
  798. static enum plink_event
  799. mesh_plink_get_event(struct ieee80211_sub_if_data *sdata,
  800. struct sta_info *sta,
  801. struct ieee802_11_elems *elems,
  802. enum ieee80211_self_protected_actioncode ftype,
  803. u16 llid, u16 plid)
  804. {
  805. enum plink_event event = PLINK_UNDEFINED;
  806. u8 ie_len = elems->peering_len;
  807. bool matches_local;
  808. matches_local = (ftype == WLAN_SP_MESH_PEERING_CLOSE ||
  809. mesh_matches_local(sdata, elems));
  810. /* deny open request from non-matching peer */
  811. if (!matches_local && !sta) {
  812. event = OPN_RJCT;
  813. goto out;
  814. }
  815. if (!sta) {
  816. if (ftype != WLAN_SP_MESH_PEERING_OPEN) {
  817. mpl_dbg(sdata, "Mesh plink: cls or cnf from unknown peer\n");
  818. goto out;
  819. }
  820. /* ftype == WLAN_SP_MESH_PEERING_OPEN */
  821. if (!mesh_plink_free_count(sdata)) {
  822. mpl_dbg(sdata, "Mesh plink error: no more free plinks\n");
  823. goto out;
  824. }
  825. } else {
  826. if (!test_sta_flag(sta, WLAN_STA_AUTH)) {
  827. mpl_dbg(sdata, "Mesh plink: Action frame from non-authed peer\n");
  828. goto out;
  829. }
  830. if (sta->plink_state == NL80211_PLINK_BLOCKED)
  831. goto out;
  832. }
  833. /* new matching peer */
  834. if (!sta) {
  835. event = OPN_ACPT;
  836. goto out;
  837. }
  838. switch (ftype) {
  839. case WLAN_SP_MESH_PEERING_OPEN:
  840. if (!matches_local)
  841. event = OPN_RJCT;
  842. if (!mesh_plink_free_count(sdata) ||
  843. (sta->plid && sta->plid != plid))
  844. event = OPN_IGNR;
  845. else
  846. event = OPN_ACPT;
  847. break;
  848. case WLAN_SP_MESH_PEERING_CONFIRM:
  849. if (!matches_local)
  850. event = CNF_RJCT;
  851. if (!mesh_plink_free_count(sdata) ||
  852. sta->llid != llid ||
  853. (sta->plid && sta->plid != plid))
  854. event = CNF_IGNR;
  855. else
  856. event = CNF_ACPT;
  857. break;
  858. case WLAN_SP_MESH_PEERING_CLOSE:
  859. if (sta->plink_state == NL80211_PLINK_ESTAB)
  860. /* Do not check for llid or plid. This does not
  861. * follow the standard but since multiple plinks
  862. * per sta are not supported, it is necessary in
  863. * order to avoid a livelock when MP A sees an
  864. * establish peer link to MP B but MP B does not
  865. * see it. This can be caused by a timeout in
  866. * B's peer link establishment or B beign
  867. * restarted.
  868. */
  869. event = CLS_ACPT;
  870. else if (sta->plid != plid)
  871. event = CLS_IGNR;
  872. else if (ie_len == 8 && sta->llid != llid)
  873. event = CLS_IGNR;
  874. else
  875. event = CLS_ACPT;
  876. break;
  877. default:
  878. mpl_dbg(sdata, "Mesh plink: unknown frame subtype\n");
  879. break;
  880. }
  881. out:
  882. return event;
  883. }
  884. static void
  885. mesh_process_plink_frame(struct ieee80211_sub_if_data *sdata,
  886. struct ieee80211_mgmt *mgmt,
  887. struct ieee802_11_elems *elems)
  888. {
  889. struct sta_info *sta;
  890. enum plink_event event;
  891. enum ieee80211_self_protected_actioncode ftype;
  892. u32 changed = 0;
  893. u8 ie_len = elems->peering_len;
  894. u16 plid, llid = 0;
  895. if (!elems->peering) {
  896. mpl_dbg(sdata,
  897. "Mesh plink: missing necessary peer link ie\n");
  898. return;
  899. }
  900. if (elems->rsn_len &&
  901. sdata->u.mesh.security == IEEE80211_MESH_SEC_NONE) {
  902. mpl_dbg(sdata,
  903. "Mesh plink: can't establish link with secure peer\n");
  904. return;
  905. }
  906. ftype = mgmt->u.action.u.self_prot.action_code;
  907. if ((ftype == WLAN_SP_MESH_PEERING_OPEN && ie_len != 4) ||
  908. (ftype == WLAN_SP_MESH_PEERING_CONFIRM && ie_len != 6) ||
  909. (ftype == WLAN_SP_MESH_PEERING_CLOSE && ie_len != 6
  910. && ie_len != 8)) {
  911. mpl_dbg(sdata,
  912. "Mesh plink: incorrect plink ie length %d %d\n",
  913. ftype, ie_len);
  914. return;
  915. }
  916. if (ftype != WLAN_SP_MESH_PEERING_CLOSE &&
  917. (!elems->mesh_id || !elems->mesh_config)) {
  918. mpl_dbg(sdata, "Mesh plink: missing necessary ie\n");
  919. return;
  920. }
  921. /* Note the lines below are correct, the llid in the frame is the plid
  922. * from the point of view of this host.
  923. */
  924. plid = get_unaligned_le16(PLINK_GET_LLID(elems->peering));
  925. if (ftype == WLAN_SP_MESH_PEERING_CONFIRM ||
  926. (ftype == WLAN_SP_MESH_PEERING_CLOSE && ie_len == 8))
  927. llid = get_unaligned_le16(PLINK_GET_PLID(elems->peering));
  928. /* WARNING: Only for sta pointer, is dropped & re-acquired */
  929. rcu_read_lock();
  930. sta = sta_info_get(sdata, mgmt->sa);
  931. if (ftype == WLAN_SP_MESH_PEERING_OPEN &&
  932. !rssi_threshold_check(sdata, sta)) {
  933. mpl_dbg(sdata, "Mesh plink: %pM does not meet rssi threshold\n",
  934. mgmt->sa);
  935. goto unlock_rcu;
  936. }
  937. /* Now we will figure out the appropriate event... */
  938. event = mesh_plink_get_event(sdata, sta, elems, ftype, llid, plid);
  939. if (event == OPN_ACPT) {
  940. rcu_read_unlock();
  941. /* allocate sta entry if necessary and update info */
  942. sta = mesh_sta_info_get(sdata, mgmt->sa, elems);
  943. if (!sta) {
  944. mpl_dbg(sdata, "Mesh plink: failed to init peer!\n");
  945. goto unlock_rcu;
  946. }
  947. sta->plid = plid;
  948. } else if (!sta && event == OPN_RJCT) {
  949. mesh_plink_frame_tx(sdata, WLAN_SP_MESH_PEERING_CLOSE,
  950. mgmt->sa, 0, plid,
  951. WLAN_REASON_MESH_CONFIG);
  952. goto unlock_rcu;
  953. } else if (!sta || event == PLINK_UNDEFINED) {
  954. /* something went wrong */
  955. goto unlock_rcu;
  956. }
  957. /* 802.11-2012 13.3.7.2 - update plid on CNF if not set */
  958. if (!sta->plid && event == CNF_ACPT)
  959. sta->plid = plid;
  960. changed |= mesh_plink_fsm(sdata, sta, event);
  961. unlock_rcu:
  962. rcu_read_unlock();
  963. if (changed)
  964. ieee80211_mbss_info_change_notify(sdata, changed);
  965. }
  966. void mesh_rx_plink_frame(struct ieee80211_sub_if_data *sdata,
  967. struct ieee80211_mgmt *mgmt, size_t len,
  968. struct ieee80211_rx_status *rx_status)
  969. {
  970. struct ieee802_11_elems elems;
  971. size_t baselen;
  972. u8 *baseaddr;
  973. /* need action_code, aux */
  974. if (len < IEEE80211_MIN_ACTION_SIZE + 3)
  975. return;
  976. if (sdata->u.mesh.user_mpm)
  977. /* userspace must register for these */
  978. return;
  979. if (is_multicast_ether_addr(mgmt->da)) {
  980. mpl_dbg(sdata,
  981. "Mesh plink: ignore frame from multicast address\n");
  982. return;
  983. }
  984. baseaddr = mgmt->u.action.u.self_prot.variable;
  985. baselen = (u8 *) mgmt->u.action.u.self_prot.variable - (u8 *) mgmt;
  986. if (mgmt->u.action.u.self_prot.action_code ==
  987. WLAN_SP_MESH_PEERING_CONFIRM) {
  988. baseaddr += 4;
  989. baselen += 4;
  990. }
  991. ieee802_11_parse_elems(baseaddr, len - baselen, true, &elems);
  992. mesh_process_plink_frame(sdata, mgmt, &elems);
  993. }