mesh_plink.c 29 KB

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