mesh_plink.c 28 KB

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