mesh_plink.c 29 KB

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