mesh_plink.c 28 KB

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