mesh_plink.c 26 KB

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