mesh_plink.c 29 KB

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