mesh_plink.c 29 KB

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