mesh_plink.c 29 KB

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