mesh_plink.c 28 KB

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