mesh_plink.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074
  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 station allocation */
  383. if (sdata->u.mesh.user_mpm ||
  384. sdata->u.mesh.security & IEEE80211_MESH_SEC_AUTHED)
  385. cfg80211_notify_new_peer_candidate(sdata->dev, addr,
  386. elems->ie_start,
  387. elems->total_len,
  388. GFP_KERNEL);
  389. else
  390. sta = __mesh_sta_info_alloc(sdata, addr);
  391. return sta;
  392. }
  393. /*
  394. * mesh_sta_info_get - return mesh sta info entry for @addr.
  395. *
  396. * @sdata: local meshif
  397. * @addr: peer's address
  398. * @elems: IEs from beacon or mesh peering frame.
  399. *
  400. * Return existing or newly allocated sta_info under RCU read lock.
  401. * (re)initialize with given IEs.
  402. */
  403. static struct sta_info *
  404. mesh_sta_info_get(struct ieee80211_sub_if_data *sdata,
  405. u8 *addr, struct ieee802_11_elems *elems) __acquires(RCU)
  406. {
  407. struct sta_info *sta = NULL;
  408. rcu_read_lock();
  409. sta = sta_info_get(sdata, addr);
  410. if (sta) {
  411. mesh_sta_info_init(sdata, sta, elems, false);
  412. } else {
  413. rcu_read_unlock();
  414. /* can't run atomic */
  415. sta = mesh_sta_info_alloc(sdata, addr, elems);
  416. if (!sta) {
  417. rcu_read_lock();
  418. return NULL;
  419. }
  420. mesh_sta_info_init(sdata, sta, elems, true);
  421. if (sta_info_insert_rcu(sta))
  422. return NULL;
  423. }
  424. return sta;
  425. }
  426. /*
  427. * mesh_neighbour_update - update or initialize new mesh neighbor.
  428. *
  429. * @sdata: local meshif
  430. * @addr: peer's address
  431. * @elems: IEs from beacon or mesh peering frame
  432. *
  433. * Initiates peering if appropriate.
  434. */
  435. void mesh_neighbour_update(struct ieee80211_sub_if_data *sdata,
  436. u8 *hw_addr,
  437. struct ieee802_11_elems *elems)
  438. {
  439. struct sta_info *sta;
  440. u32 changed = 0;
  441. sta = mesh_sta_info_get(sdata, hw_addr, elems);
  442. if (!sta)
  443. goto out;
  444. if (mesh_peer_accepts_plinks(elems) &&
  445. sta->plink_state == NL80211_PLINK_LISTEN &&
  446. sdata->u.mesh.accepting_plinks &&
  447. sdata->u.mesh.mshcfg.auto_open_plinks &&
  448. rssi_threshold_check(sta, sdata))
  449. changed = mesh_plink_open(sta);
  450. ieee80211_mps_frame_release(sta, elems);
  451. out:
  452. rcu_read_unlock();
  453. ieee80211_mbss_info_change_notify(sdata, changed);
  454. }
  455. static void mesh_plink_timer(unsigned long data)
  456. {
  457. struct sta_info *sta;
  458. __le16 llid, plid, reason;
  459. struct ieee80211_sub_if_data *sdata;
  460. struct mesh_config *mshcfg;
  461. /*
  462. * This STA is valid because sta_info_destroy() will
  463. * del_timer_sync() this timer after having made sure
  464. * it cannot be readded (by deleting the plink.)
  465. */
  466. sta = (struct sta_info *) data;
  467. if (sta->sdata->local->quiescing)
  468. return;
  469. spin_lock_bh(&sta->lock);
  470. if (sta->ignore_plink_timer) {
  471. sta->ignore_plink_timer = false;
  472. spin_unlock_bh(&sta->lock);
  473. return;
  474. }
  475. mpl_dbg(sta->sdata,
  476. "Mesh plink timer for %pM fired on state %d\n",
  477. sta->sta.addr, sta->plink_state);
  478. reason = 0;
  479. llid = sta->llid;
  480. plid = sta->plid;
  481. sdata = sta->sdata;
  482. mshcfg = &sdata->u.mesh.mshcfg;
  483. switch (sta->plink_state) {
  484. case NL80211_PLINK_OPN_RCVD:
  485. case NL80211_PLINK_OPN_SNT:
  486. /* retry timer */
  487. if (sta->plink_retries < mshcfg->dot11MeshMaxRetries) {
  488. u32 rand;
  489. mpl_dbg(sta->sdata,
  490. "Mesh plink for %pM (retry, timeout): %d %d\n",
  491. sta->sta.addr, sta->plink_retries,
  492. sta->plink_timeout);
  493. get_random_bytes(&rand, sizeof(u32));
  494. sta->plink_timeout = sta->plink_timeout +
  495. rand % sta->plink_timeout;
  496. ++sta->plink_retries;
  497. mod_plink_timer(sta, sta->plink_timeout);
  498. spin_unlock_bh(&sta->lock);
  499. mesh_plink_frame_tx(sdata, WLAN_SP_MESH_PEERING_OPEN,
  500. sta->sta.addr, llid, 0, 0);
  501. break;
  502. }
  503. reason = cpu_to_le16(WLAN_REASON_MESH_MAX_RETRIES);
  504. /* fall through on else */
  505. case NL80211_PLINK_CNF_RCVD:
  506. /* confirm timer */
  507. if (!reason)
  508. reason = cpu_to_le16(WLAN_REASON_MESH_CONFIRM_TIMEOUT);
  509. sta->plink_state = NL80211_PLINK_HOLDING;
  510. mod_plink_timer(sta, mshcfg->dot11MeshHoldingTimeout);
  511. spin_unlock_bh(&sta->lock);
  512. mesh_plink_frame_tx(sdata, WLAN_SP_MESH_PEERING_CLOSE,
  513. sta->sta.addr, llid, plid, reason);
  514. break;
  515. case NL80211_PLINK_HOLDING:
  516. /* holding timer */
  517. del_timer(&sta->plink_timer);
  518. mesh_plink_fsm_restart(sta);
  519. spin_unlock_bh(&sta->lock);
  520. break;
  521. default:
  522. spin_unlock_bh(&sta->lock);
  523. break;
  524. }
  525. }
  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. /* need action_code, aux */
  586. if (len < IEEE80211_MIN_ACTION_SIZE + 3)
  587. return;
  588. if (sdata->u.mesh.user_mpm)
  589. /* userspace must register for these */
  590. return;
  591. if (is_multicast_ether_addr(mgmt->da)) {
  592. mpl_dbg(sdata,
  593. "Mesh plink: ignore frame from multicast address\n");
  594. return;
  595. }
  596. baseaddr = mgmt->u.action.u.self_prot.variable;
  597. baselen = (u8 *) mgmt->u.action.u.self_prot.variable - (u8 *) mgmt;
  598. if (mgmt->u.action.u.self_prot.action_code ==
  599. WLAN_SP_MESH_PEERING_CONFIRM) {
  600. baseaddr += 4;
  601. baselen += 4;
  602. }
  603. ieee802_11_parse_elems(baseaddr, len - baselen, &elems);
  604. if (!elems.peering) {
  605. mpl_dbg(sdata,
  606. "Mesh plink: missing necessary peer link ie\n");
  607. return;
  608. }
  609. if (elems.rsn_len &&
  610. sdata->u.mesh.security == IEEE80211_MESH_SEC_NONE) {
  611. mpl_dbg(sdata,
  612. "Mesh plink: can't establish link with secure peer\n");
  613. return;
  614. }
  615. ftype = mgmt->u.action.u.self_prot.action_code;
  616. ie_len = elems.peering_len;
  617. if ((ftype == WLAN_SP_MESH_PEERING_OPEN && ie_len != 4) ||
  618. (ftype == WLAN_SP_MESH_PEERING_CONFIRM && ie_len != 6) ||
  619. (ftype == WLAN_SP_MESH_PEERING_CLOSE && ie_len != 6
  620. && ie_len != 8)) {
  621. mpl_dbg(sdata,
  622. "Mesh plink: incorrect plink ie length %d %d\n",
  623. ftype, ie_len);
  624. return;
  625. }
  626. if (ftype != WLAN_SP_MESH_PEERING_CLOSE &&
  627. (!elems.mesh_id || !elems.mesh_config)) {
  628. mpl_dbg(sdata, "Mesh plink: missing necessary ie\n");
  629. return;
  630. }
  631. /* Note the lines below are correct, the llid in the frame is the plid
  632. * from the point of view of this host.
  633. */
  634. memcpy(&plid, PLINK_GET_LLID(elems.peering), 2);
  635. if (ftype == WLAN_SP_MESH_PEERING_CONFIRM ||
  636. (ftype == WLAN_SP_MESH_PEERING_CLOSE && ie_len == 8))
  637. memcpy(&llid, PLINK_GET_PLID(elems.peering), 2);
  638. /* WARNING: Only for sta pointer, is dropped & re-acquired */
  639. rcu_read_lock();
  640. sta = sta_info_get(sdata, mgmt->sa);
  641. if (!sta && ftype != WLAN_SP_MESH_PEERING_OPEN) {
  642. mpl_dbg(sdata, "Mesh plink: cls or cnf from unknown peer\n");
  643. rcu_read_unlock();
  644. return;
  645. }
  646. if (ftype == WLAN_SP_MESH_PEERING_OPEN &&
  647. !rssi_threshold_check(sta, sdata)) {
  648. mpl_dbg(sdata, "Mesh plink: %pM does not meet rssi threshold\n",
  649. mgmt->sa);
  650. rcu_read_unlock();
  651. return;
  652. }
  653. if (sta && !test_sta_flag(sta, WLAN_STA_AUTH)) {
  654. mpl_dbg(sdata, "Mesh plink: Action frame from non-authed peer\n");
  655. rcu_read_unlock();
  656. return;
  657. }
  658. if (sta && sta->plink_state == NL80211_PLINK_BLOCKED) {
  659. rcu_read_unlock();
  660. return;
  661. }
  662. /* Now we will figure out the appropriate event... */
  663. event = PLINK_UNDEFINED;
  664. if (ftype != WLAN_SP_MESH_PEERING_CLOSE &&
  665. !mesh_matches_local(sdata, &elems)) {
  666. matches_local = false;
  667. switch (ftype) {
  668. case WLAN_SP_MESH_PEERING_OPEN:
  669. event = OPN_RJCT;
  670. break;
  671. case WLAN_SP_MESH_PEERING_CONFIRM:
  672. event = CNF_RJCT;
  673. break;
  674. default:
  675. break;
  676. }
  677. }
  678. if (!sta && !matches_local) {
  679. rcu_read_unlock();
  680. reason = cpu_to_le16(WLAN_REASON_MESH_CONFIG);
  681. llid = 0;
  682. mesh_plink_frame_tx(sdata, WLAN_SP_MESH_PEERING_CLOSE,
  683. mgmt->sa, llid, plid, reason);
  684. return;
  685. } else if (!sta) {
  686. /* ftype == WLAN_SP_MESH_PEERING_OPEN */
  687. if (!mesh_plink_free_count(sdata)) {
  688. mpl_dbg(sdata, "Mesh plink error: no more free plinks\n");
  689. rcu_read_unlock();
  690. return;
  691. }
  692. event = OPN_ACPT;
  693. } else if (matches_local) {
  694. switch (ftype) {
  695. case WLAN_SP_MESH_PEERING_OPEN:
  696. if (!mesh_plink_free_count(sdata) ||
  697. (sta->plid && sta->plid != plid))
  698. event = OPN_IGNR;
  699. else
  700. event = OPN_ACPT;
  701. break;
  702. case WLAN_SP_MESH_PEERING_CONFIRM:
  703. if (!mesh_plink_free_count(sdata) ||
  704. (sta->llid != llid || sta->plid != plid))
  705. event = CNF_IGNR;
  706. else
  707. event = CNF_ACPT;
  708. break;
  709. case WLAN_SP_MESH_PEERING_CLOSE:
  710. if (sta->plink_state == NL80211_PLINK_ESTAB)
  711. /* Do not check for llid or plid. This does not
  712. * follow the standard but since multiple plinks
  713. * per sta are not supported, it is necessary in
  714. * order to avoid a livelock when MP A sees an
  715. * establish peer link to MP B but MP B does not
  716. * see it. This can be caused by a timeout in
  717. * B's peer link establishment or B beign
  718. * restarted.
  719. */
  720. event = CLS_ACPT;
  721. else if (sta->plid != plid)
  722. event = CLS_IGNR;
  723. else if (ie_len == 7 && sta->llid != llid)
  724. event = CLS_IGNR;
  725. else
  726. event = CLS_ACPT;
  727. break;
  728. default:
  729. mpl_dbg(sdata, "Mesh plink: unknown frame subtype\n");
  730. rcu_read_unlock();
  731. return;
  732. }
  733. }
  734. if (event == OPN_ACPT) {
  735. rcu_read_unlock();
  736. /* allocate sta entry if necessary and update info */
  737. sta = mesh_sta_info_get(sdata, mgmt->sa, &elems);
  738. if (!sta) {
  739. mpl_dbg(sdata, "Mesh plink: failed to init peer!\n");
  740. rcu_read_unlock();
  741. return;
  742. }
  743. }
  744. mpl_dbg(sdata, "peer %pM in state %s got event %s\n", mgmt->sa,
  745. mplstates[sta->plink_state], mplevents[event]);
  746. reason = 0;
  747. spin_lock_bh(&sta->lock);
  748. switch (sta->plink_state) {
  749. /* spin_unlock as soon as state is updated at each case */
  750. case NL80211_PLINK_LISTEN:
  751. switch (event) {
  752. case CLS_ACPT:
  753. mesh_plink_fsm_restart(sta);
  754. spin_unlock_bh(&sta->lock);
  755. break;
  756. case OPN_ACPT:
  757. sta->plink_state = NL80211_PLINK_OPN_RCVD;
  758. sta->plid = plid;
  759. get_random_bytes(&llid, 2);
  760. sta->llid = llid;
  761. mesh_plink_timer_set(sta,
  762. mshcfg->dot11MeshRetryTimeout);
  763. /* set the non-peer mode to active during peering */
  764. changed |= ieee80211_mps_local_status_update(sdata);
  765. spin_unlock_bh(&sta->lock);
  766. mesh_plink_frame_tx(sdata,
  767. WLAN_SP_MESH_PEERING_OPEN,
  768. sta->sta.addr, llid, 0, 0);
  769. mesh_plink_frame_tx(sdata,
  770. WLAN_SP_MESH_PEERING_CONFIRM,
  771. sta->sta.addr, llid, plid, 0);
  772. break;
  773. default:
  774. spin_unlock_bh(&sta->lock);
  775. break;
  776. }
  777. break;
  778. case NL80211_PLINK_OPN_SNT:
  779. switch (event) {
  780. case OPN_RJCT:
  781. case CNF_RJCT:
  782. reason = cpu_to_le16(WLAN_REASON_MESH_CONFIG);
  783. case CLS_ACPT:
  784. if (!reason)
  785. reason = cpu_to_le16(WLAN_REASON_MESH_CLOSE);
  786. sta->reason = reason;
  787. sta->plink_state = NL80211_PLINK_HOLDING;
  788. if (!mod_plink_timer(sta,
  789. mshcfg->dot11MeshHoldingTimeout))
  790. sta->ignore_plink_timer = true;
  791. llid = sta->llid;
  792. spin_unlock_bh(&sta->lock);
  793. mesh_plink_frame_tx(sdata,
  794. WLAN_SP_MESH_PEERING_CLOSE,
  795. sta->sta.addr, llid, plid, reason);
  796. break;
  797. case OPN_ACPT:
  798. /* retry timer is left untouched */
  799. sta->plink_state = NL80211_PLINK_OPN_RCVD;
  800. sta->plid = plid;
  801. llid = sta->llid;
  802. spin_unlock_bh(&sta->lock);
  803. mesh_plink_frame_tx(sdata,
  804. WLAN_SP_MESH_PEERING_CONFIRM,
  805. sta->sta.addr, llid, plid, 0);
  806. break;
  807. case CNF_ACPT:
  808. sta->plink_state = NL80211_PLINK_CNF_RCVD;
  809. if (!mod_plink_timer(sta,
  810. mshcfg->dot11MeshConfirmTimeout))
  811. sta->ignore_plink_timer = true;
  812. spin_unlock_bh(&sta->lock);
  813. break;
  814. default:
  815. spin_unlock_bh(&sta->lock);
  816. break;
  817. }
  818. break;
  819. case NL80211_PLINK_OPN_RCVD:
  820. switch (event) {
  821. case OPN_RJCT:
  822. case CNF_RJCT:
  823. reason = cpu_to_le16(WLAN_REASON_MESH_CONFIG);
  824. case CLS_ACPT:
  825. if (!reason)
  826. reason = cpu_to_le16(WLAN_REASON_MESH_CLOSE);
  827. sta->reason = reason;
  828. sta->plink_state = NL80211_PLINK_HOLDING;
  829. if (!mod_plink_timer(sta,
  830. mshcfg->dot11MeshHoldingTimeout))
  831. sta->ignore_plink_timer = true;
  832. llid = sta->llid;
  833. spin_unlock_bh(&sta->lock);
  834. mesh_plink_frame_tx(sdata, WLAN_SP_MESH_PEERING_CLOSE,
  835. sta->sta.addr, llid, plid, reason);
  836. break;
  837. case OPN_ACPT:
  838. llid = sta->llid;
  839. spin_unlock_bh(&sta->lock);
  840. mesh_plink_frame_tx(sdata,
  841. WLAN_SP_MESH_PEERING_CONFIRM,
  842. sta->sta.addr, llid, plid, 0);
  843. break;
  844. case CNF_ACPT:
  845. del_timer(&sta->plink_timer);
  846. sta->plink_state = NL80211_PLINK_ESTAB;
  847. spin_unlock_bh(&sta->lock);
  848. changed |= mesh_plink_inc_estab_count(sdata);
  849. changed |= mesh_set_ht_prot_mode(sdata);
  850. changed |= mesh_set_short_slot_time(sdata);
  851. mpl_dbg(sdata, "Mesh plink with %pM ESTABLISHED\n",
  852. sta->sta.addr);
  853. ieee80211_mps_sta_status_update(sta);
  854. changed |= ieee80211_mps_set_sta_local_pm(sta,
  855. mshcfg->power_mode);
  856. break;
  857. default:
  858. spin_unlock_bh(&sta->lock);
  859. break;
  860. }
  861. break;
  862. case NL80211_PLINK_CNF_RCVD:
  863. switch (event) {
  864. case OPN_RJCT:
  865. case CNF_RJCT:
  866. reason = cpu_to_le16(WLAN_REASON_MESH_CONFIG);
  867. case CLS_ACPT:
  868. if (!reason)
  869. reason = cpu_to_le16(WLAN_REASON_MESH_CLOSE);
  870. sta->reason = reason;
  871. sta->plink_state = NL80211_PLINK_HOLDING;
  872. if (!mod_plink_timer(sta,
  873. mshcfg->dot11MeshHoldingTimeout))
  874. sta->ignore_plink_timer = true;
  875. llid = sta->llid;
  876. spin_unlock_bh(&sta->lock);
  877. mesh_plink_frame_tx(sdata,
  878. WLAN_SP_MESH_PEERING_CLOSE,
  879. sta->sta.addr, llid, plid, reason);
  880. break;
  881. case OPN_ACPT:
  882. del_timer(&sta->plink_timer);
  883. sta->plink_state = NL80211_PLINK_ESTAB;
  884. spin_unlock_bh(&sta->lock);
  885. changed |= mesh_plink_inc_estab_count(sdata);
  886. changed |= mesh_set_ht_prot_mode(sdata);
  887. changed |= mesh_set_short_slot_time(sdata);
  888. mpl_dbg(sdata, "Mesh plink with %pM ESTABLISHED\n",
  889. sta->sta.addr);
  890. mesh_plink_frame_tx(sdata,
  891. WLAN_SP_MESH_PEERING_CONFIRM,
  892. sta->sta.addr, llid, plid, 0);
  893. ieee80211_mps_sta_status_update(sta);
  894. changed |= ieee80211_mps_set_sta_local_pm(sta,
  895. mshcfg->power_mode);
  896. break;
  897. default:
  898. spin_unlock_bh(&sta->lock);
  899. break;
  900. }
  901. break;
  902. case NL80211_PLINK_ESTAB:
  903. switch (event) {
  904. case CLS_ACPT:
  905. reason = cpu_to_le16(WLAN_REASON_MESH_CLOSE);
  906. sta->reason = reason;
  907. changed |= __mesh_plink_deactivate(sta);
  908. sta->plink_state = NL80211_PLINK_HOLDING;
  909. llid = sta->llid;
  910. mod_plink_timer(sta, mshcfg->dot11MeshHoldingTimeout);
  911. spin_unlock_bh(&sta->lock);
  912. changed |= mesh_set_ht_prot_mode(sdata);
  913. changed |= mesh_set_short_slot_time(sdata);
  914. mesh_plink_frame_tx(sdata, WLAN_SP_MESH_PEERING_CLOSE,
  915. sta->sta.addr, llid, plid, reason);
  916. break;
  917. case OPN_ACPT:
  918. llid = sta->llid;
  919. spin_unlock_bh(&sta->lock);
  920. mesh_plink_frame_tx(sdata,
  921. WLAN_SP_MESH_PEERING_CONFIRM,
  922. sta->sta.addr, llid, plid, 0);
  923. break;
  924. default:
  925. spin_unlock_bh(&sta->lock);
  926. break;
  927. }
  928. break;
  929. case NL80211_PLINK_HOLDING:
  930. switch (event) {
  931. case CLS_ACPT:
  932. if (del_timer(&sta->plink_timer))
  933. sta->ignore_plink_timer = 1;
  934. mesh_plink_fsm_restart(sta);
  935. spin_unlock_bh(&sta->lock);
  936. break;
  937. case OPN_ACPT:
  938. case CNF_ACPT:
  939. case OPN_RJCT:
  940. case CNF_RJCT:
  941. llid = sta->llid;
  942. reason = sta->reason;
  943. spin_unlock_bh(&sta->lock);
  944. mesh_plink_frame_tx(sdata, WLAN_SP_MESH_PEERING_CLOSE,
  945. sta->sta.addr, llid, plid, reason);
  946. break;
  947. default:
  948. spin_unlock_bh(&sta->lock);
  949. }
  950. break;
  951. default:
  952. /* should not get here, PLINK_BLOCKED is dealt with at the
  953. * beginning of the function
  954. */
  955. spin_unlock_bh(&sta->lock);
  956. break;
  957. }
  958. rcu_read_unlock();
  959. if (changed)
  960. ieee80211_mbss_info_change_notify(sdata, changed);
  961. }