mesh_plink.c 29 KB

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