mesh_plink.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866
  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. #ifdef CONFIG_MAC80211_VERBOSE_MPL_DEBUG
  16. #define mpl_dbg(fmt, args...) printk(KERN_DEBUG fmt, ##args)
  17. #else
  18. #define mpl_dbg(fmt, args...) do { (void)(0); } while (0)
  19. #endif
  20. #define PLINK_GET_LLID(p) (p + 2)
  21. #define PLINK_GET_PLID(p) (p + 4)
  22. #define mod_plink_timer(s, t) (mod_timer(&s->plink_timer, \
  23. jiffies + HZ * t / 1000))
  24. #define dot11MeshMaxRetries(s) (s->u.mesh.mshcfg.dot11MeshMaxRetries)
  25. #define dot11MeshRetryTimeout(s) (s->u.mesh.mshcfg.dot11MeshRetryTimeout)
  26. #define dot11MeshConfirmTimeout(s) (s->u.mesh.mshcfg.dot11MeshConfirmTimeout)
  27. #define dot11MeshHoldingTimeout(s) (s->u.mesh.mshcfg.dot11MeshHoldingTimeout)
  28. #define dot11MeshMaxPeerLinks(s) (s->u.mesh.mshcfg.dot11MeshMaxPeerLinks)
  29. #define sta_meets_rssi_threshold(sta, sdata) \
  30. (sdata->u.mesh.mshcfg.rssi_threshold == 0 ||\
  31. (s8) -ewma_read(&sta->avg_signal) > \
  32. sdata->u.mesh.mshcfg.rssi_threshold)
  33. enum plink_event {
  34. PLINK_UNDEFINED,
  35. OPN_ACPT,
  36. OPN_RJCT,
  37. OPN_IGNR,
  38. CNF_ACPT,
  39. CNF_RJCT,
  40. CNF_IGNR,
  41. CLS_ACPT,
  42. CLS_IGNR
  43. };
  44. static int mesh_plink_frame_tx(struct ieee80211_sub_if_data *sdata,
  45. enum ieee80211_self_protected_actioncode action,
  46. u8 *da, __le16 llid, __le16 plid, __le16 reason);
  47. static inline
  48. void mesh_plink_inc_estab_count(struct ieee80211_sub_if_data *sdata)
  49. {
  50. atomic_inc(&sdata->u.mesh.mshstats.estab_plinks);
  51. mesh_accept_plinks_update(sdata);
  52. }
  53. static inline
  54. void mesh_plink_dec_estab_count(struct ieee80211_sub_if_data *sdata)
  55. {
  56. atomic_dec(&sdata->u.mesh.mshstats.estab_plinks);
  57. mesh_accept_plinks_update(sdata);
  58. }
  59. /**
  60. * mesh_plink_fsm_restart - restart a mesh peer link finite state machine
  61. *
  62. * @sta: mesh peer link to restart
  63. *
  64. * Locking: this function must be called holding sta->lock
  65. */
  66. static inline void mesh_plink_fsm_restart(struct sta_info *sta)
  67. {
  68. sta->plink_state = NL80211_PLINK_LISTEN;
  69. sta->llid = sta->plid = sta->reason = 0;
  70. sta->plink_retries = 0;
  71. }
  72. /*
  73. * NOTE: This is just an alias for sta_info_alloc(), see notes
  74. * on it in the lifecycle management section!
  75. */
  76. static struct sta_info *mesh_plink_alloc(struct ieee80211_sub_if_data *sdata,
  77. u8 *hw_addr, u32 rates,
  78. struct ieee802_11_elems *elems)
  79. {
  80. struct ieee80211_local *local = sdata->local;
  81. struct ieee80211_supported_band *sband;
  82. struct sta_info *sta;
  83. sband = local->hw.wiphy->bands[local->oper_channel->band];
  84. if (local->num_sta >= MESH_MAX_PLINKS)
  85. return NULL;
  86. sta = sta_info_alloc(sdata, hw_addr, GFP_KERNEL);
  87. if (!sta)
  88. return NULL;
  89. sta_info_pre_move_state(sta, IEEE80211_STA_AUTH);
  90. sta_info_pre_move_state(sta, IEEE80211_STA_ASSOC);
  91. sta_info_pre_move_state(sta, IEEE80211_STA_AUTHORIZED);
  92. set_sta_flag(sta, WLAN_STA_WME);
  93. sta->sta.supp_rates[local->hw.conf.channel->band] = rates;
  94. if (elems->ht_cap_elem)
  95. ieee80211_ht_cap_ie_to_sta_ht_cap(sdata, sband,
  96. elems->ht_cap_elem,
  97. &sta->sta.ht_cap);
  98. rate_control_rate_init(sta);
  99. return sta;
  100. }
  101. /**
  102. * __mesh_plink_deactivate - deactivate mesh peer link
  103. *
  104. * @sta: mesh peer link to deactivate
  105. *
  106. * All mesh paths with this peer as next hop will be flushed
  107. *
  108. * Locking: the caller must hold sta->lock
  109. */
  110. static bool __mesh_plink_deactivate(struct sta_info *sta)
  111. {
  112. struct ieee80211_sub_if_data *sdata = sta->sdata;
  113. bool deactivated = false;
  114. if (sta->plink_state == NL80211_PLINK_ESTAB) {
  115. mesh_plink_dec_estab_count(sdata);
  116. deactivated = true;
  117. }
  118. sta->plink_state = NL80211_PLINK_BLOCKED;
  119. mesh_path_flush_by_nexthop(sta);
  120. return deactivated;
  121. }
  122. /**
  123. * mesh_plink_deactivate - deactivate mesh peer link
  124. *
  125. * @sta: mesh peer link to deactivate
  126. *
  127. * All mesh paths with this peer as next hop will be flushed
  128. */
  129. void mesh_plink_deactivate(struct sta_info *sta)
  130. {
  131. struct ieee80211_sub_if_data *sdata = sta->sdata;
  132. bool deactivated;
  133. spin_lock_bh(&sta->lock);
  134. deactivated = __mesh_plink_deactivate(sta);
  135. sta->reason = cpu_to_le16(WLAN_REASON_MESH_PEER_CANCELED);
  136. mesh_plink_frame_tx(sdata, WLAN_SP_MESH_PEERING_CLOSE,
  137. sta->sta.addr, sta->llid, sta->plid,
  138. sta->reason);
  139. spin_unlock_bh(&sta->lock);
  140. if (deactivated)
  141. ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BEACON);
  142. }
  143. static int mesh_plink_frame_tx(struct ieee80211_sub_if_data *sdata,
  144. enum ieee80211_self_protected_actioncode action,
  145. u8 *da, __le16 llid, __le16 plid, __le16 reason) {
  146. struct ieee80211_local *local = sdata->local;
  147. struct sk_buff *skb;
  148. struct ieee80211_mgmt *mgmt;
  149. bool include_plid = false;
  150. u16 peering_proto = 0;
  151. u8 *pos, ie_len = 4;
  152. int hdr_len = offsetof(struct ieee80211_mgmt, u.action.u.self_prot) +
  153. sizeof(mgmt->u.action.u.self_prot);
  154. skb = dev_alloc_skb(local->tx_headroom +
  155. hdr_len +
  156. 2 + /* capability info */
  157. 2 + /* AID */
  158. 2 + 8 + /* supported rates */
  159. 2 + (IEEE80211_MAX_SUPP_RATES - 8) +
  160. 2 + sdata->u.mesh.mesh_id_len +
  161. 2 + sizeof(struct ieee80211_meshconf_ie) +
  162. 2 + sizeof(struct ieee80211_ht_cap) +
  163. 2 + sizeof(struct ieee80211_ht_info) +
  164. 2 + 8 + /* peering IE */
  165. sdata->u.mesh.ie_len);
  166. if (!skb)
  167. return -1;
  168. skb_reserve(skb, local->tx_headroom);
  169. mgmt = (struct ieee80211_mgmt *) skb_put(skb, hdr_len);
  170. memset(mgmt, 0, hdr_len);
  171. mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
  172. IEEE80211_STYPE_ACTION);
  173. memcpy(mgmt->da, da, ETH_ALEN);
  174. memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
  175. memcpy(mgmt->bssid, sdata->vif.addr, ETH_ALEN);
  176. mgmt->u.action.category = WLAN_CATEGORY_SELF_PROTECTED;
  177. mgmt->u.action.u.self_prot.action_code = action;
  178. if (action != WLAN_SP_MESH_PEERING_CLOSE) {
  179. /* capability info */
  180. pos = skb_put(skb, 2);
  181. memset(pos, 0, 2);
  182. if (action == WLAN_SP_MESH_PEERING_CONFIRM) {
  183. /* AID */
  184. pos = skb_put(skb, 2);
  185. memcpy(pos + 2, &plid, 2);
  186. }
  187. if (ieee80211_add_srates_ie(&sdata->vif, skb) ||
  188. ieee80211_add_ext_srates_ie(&sdata->vif, skb) ||
  189. mesh_add_rsn_ie(skb, sdata) ||
  190. mesh_add_meshid_ie(skb, sdata) ||
  191. mesh_add_meshconf_ie(skb, sdata))
  192. return -1;
  193. } else { /* WLAN_SP_MESH_PEERING_CLOSE */
  194. if (mesh_add_meshid_ie(skb, sdata))
  195. return -1;
  196. }
  197. /* Add Mesh Peering Management element */
  198. switch (action) {
  199. case WLAN_SP_MESH_PEERING_OPEN:
  200. break;
  201. case WLAN_SP_MESH_PEERING_CONFIRM:
  202. ie_len += 2;
  203. include_plid = true;
  204. break;
  205. case WLAN_SP_MESH_PEERING_CLOSE:
  206. if (plid) {
  207. ie_len += 2;
  208. include_plid = true;
  209. }
  210. ie_len += 2; /* reason code */
  211. break;
  212. default:
  213. return -EINVAL;
  214. }
  215. if (WARN_ON(skb_tailroom(skb) < 2 + ie_len))
  216. return -ENOMEM;
  217. pos = skb_put(skb, 2 + ie_len);
  218. *pos++ = WLAN_EID_PEER_MGMT;
  219. *pos++ = ie_len;
  220. memcpy(pos, &peering_proto, 2);
  221. pos += 2;
  222. memcpy(pos, &llid, 2);
  223. pos += 2;
  224. if (include_plid) {
  225. memcpy(pos, &plid, 2);
  226. pos += 2;
  227. }
  228. if (action == WLAN_SP_MESH_PEERING_CLOSE) {
  229. memcpy(pos, &reason, 2);
  230. pos += 2;
  231. }
  232. if (action != WLAN_SP_MESH_PEERING_CLOSE) {
  233. if (mesh_add_ht_cap_ie(skb, sdata) ||
  234. mesh_add_ht_info_ie(skb, sdata))
  235. return -1;
  236. }
  237. if (mesh_add_vendor_ies(skb, sdata))
  238. return -1;
  239. ieee80211_tx_skb(sdata, skb);
  240. return 0;
  241. }
  242. void mesh_neighbour_update(u8 *hw_addr, u32 rates,
  243. struct ieee80211_sub_if_data *sdata,
  244. struct ieee802_11_elems *elems)
  245. {
  246. struct ieee80211_local *local = sdata->local;
  247. struct sta_info *sta;
  248. rcu_read_lock();
  249. sta = sta_info_get(sdata, hw_addr);
  250. if (!sta) {
  251. rcu_read_unlock();
  252. /* Userspace handles peer allocation when security is enabled
  253. * */
  254. if (sdata->u.mesh.security & IEEE80211_MESH_SEC_AUTHED)
  255. cfg80211_notify_new_peer_candidate(sdata->dev, hw_addr,
  256. elems->ie_start, elems->total_len,
  257. GFP_KERNEL);
  258. else
  259. sta = mesh_plink_alloc(sdata, hw_addr, rates, elems);
  260. if (!sta)
  261. return;
  262. if (sta_info_insert_rcu(sta)) {
  263. rcu_read_unlock();
  264. return;
  265. }
  266. }
  267. sta->last_rx = jiffies;
  268. sta->sta.supp_rates[local->hw.conf.channel->band] = rates;
  269. if (mesh_peer_accepts_plinks(elems) &&
  270. sta->plink_state == NL80211_PLINK_LISTEN &&
  271. sdata->u.mesh.accepting_plinks &&
  272. sdata->u.mesh.mshcfg.auto_open_plinks &&
  273. sta_meets_rssi_threshold(sta, sdata))
  274. mesh_plink_open(sta);
  275. rcu_read_unlock();
  276. }
  277. static void mesh_plink_timer(unsigned long data)
  278. {
  279. struct sta_info *sta;
  280. __le16 llid, plid, reason;
  281. struct ieee80211_sub_if_data *sdata;
  282. /*
  283. * This STA is valid because sta_info_destroy() will
  284. * del_timer_sync() this timer after having made sure
  285. * it cannot be readded (by deleting the plink.)
  286. */
  287. sta = (struct sta_info *) data;
  288. if (sta->sdata->local->quiescing) {
  289. sta->plink_timer_was_running = true;
  290. return;
  291. }
  292. spin_lock_bh(&sta->lock);
  293. if (sta->ignore_plink_timer) {
  294. sta->ignore_plink_timer = false;
  295. spin_unlock_bh(&sta->lock);
  296. return;
  297. }
  298. mpl_dbg("Mesh plink timer for %pM fired on state %d\n",
  299. sta->sta.addr, sta->plink_state);
  300. reason = 0;
  301. llid = sta->llid;
  302. plid = sta->plid;
  303. sdata = sta->sdata;
  304. switch (sta->plink_state) {
  305. case NL80211_PLINK_OPN_RCVD:
  306. case NL80211_PLINK_OPN_SNT:
  307. /* retry timer */
  308. if (sta->plink_retries < dot11MeshMaxRetries(sdata)) {
  309. u32 rand;
  310. mpl_dbg("Mesh plink for %pM (retry, timeout): %d %d\n",
  311. sta->sta.addr, sta->plink_retries,
  312. sta->plink_timeout);
  313. get_random_bytes(&rand, sizeof(u32));
  314. sta->plink_timeout = sta->plink_timeout +
  315. rand % sta->plink_timeout;
  316. ++sta->plink_retries;
  317. mod_plink_timer(sta, sta->plink_timeout);
  318. spin_unlock_bh(&sta->lock);
  319. mesh_plink_frame_tx(sdata, WLAN_SP_MESH_PEERING_OPEN,
  320. sta->sta.addr, llid, 0, 0);
  321. break;
  322. }
  323. reason = cpu_to_le16(WLAN_REASON_MESH_MAX_RETRIES);
  324. /* fall through on else */
  325. case NL80211_PLINK_CNF_RCVD:
  326. /* confirm timer */
  327. if (!reason)
  328. reason = cpu_to_le16(WLAN_REASON_MESH_CONFIRM_TIMEOUT);
  329. sta->plink_state = NL80211_PLINK_HOLDING;
  330. mod_plink_timer(sta, dot11MeshHoldingTimeout(sdata));
  331. spin_unlock_bh(&sta->lock);
  332. mesh_plink_frame_tx(sdata, WLAN_SP_MESH_PEERING_CLOSE,
  333. sta->sta.addr, llid, plid, reason);
  334. break;
  335. case NL80211_PLINK_HOLDING:
  336. /* holding timer */
  337. del_timer(&sta->plink_timer);
  338. mesh_plink_fsm_restart(sta);
  339. spin_unlock_bh(&sta->lock);
  340. break;
  341. default:
  342. spin_unlock_bh(&sta->lock);
  343. break;
  344. }
  345. }
  346. #ifdef CONFIG_PM
  347. void mesh_plink_quiesce(struct sta_info *sta)
  348. {
  349. if (del_timer_sync(&sta->plink_timer))
  350. sta->plink_timer_was_running = true;
  351. }
  352. void mesh_plink_restart(struct sta_info *sta)
  353. {
  354. if (sta->plink_timer_was_running) {
  355. add_timer(&sta->plink_timer);
  356. sta->plink_timer_was_running = false;
  357. }
  358. }
  359. #endif
  360. static inline void mesh_plink_timer_set(struct sta_info *sta, int timeout)
  361. {
  362. sta->plink_timer.expires = jiffies + (HZ * timeout / 1000);
  363. sta->plink_timer.data = (unsigned long) sta;
  364. sta->plink_timer.function = mesh_plink_timer;
  365. sta->plink_timeout = timeout;
  366. add_timer(&sta->plink_timer);
  367. }
  368. int mesh_plink_open(struct sta_info *sta)
  369. {
  370. __le16 llid;
  371. struct ieee80211_sub_if_data *sdata = sta->sdata;
  372. if (!test_sta_flag(sta, WLAN_STA_AUTH))
  373. return -EPERM;
  374. spin_lock_bh(&sta->lock);
  375. get_random_bytes(&llid, 2);
  376. sta->llid = llid;
  377. if (sta->plink_state != NL80211_PLINK_LISTEN) {
  378. spin_unlock_bh(&sta->lock);
  379. return -EBUSY;
  380. }
  381. sta->plink_state = NL80211_PLINK_OPN_SNT;
  382. mesh_plink_timer_set(sta, dot11MeshRetryTimeout(sdata));
  383. spin_unlock_bh(&sta->lock);
  384. mpl_dbg("Mesh plink: starting establishment with %pM\n",
  385. sta->sta.addr);
  386. return mesh_plink_frame_tx(sdata, WLAN_SP_MESH_PEERING_OPEN,
  387. sta->sta.addr, llid, 0, 0);
  388. }
  389. void mesh_plink_block(struct sta_info *sta)
  390. {
  391. struct ieee80211_sub_if_data *sdata = sta->sdata;
  392. bool deactivated;
  393. spin_lock_bh(&sta->lock);
  394. deactivated = __mesh_plink_deactivate(sta);
  395. sta->plink_state = NL80211_PLINK_BLOCKED;
  396. spin_unlock_bh(&sta->lock);
  397. if (deactivated)
  398. ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BEACON);
  399. }
  400. void mesh_rx_plink_frame(struct ieee80211_sub_if_data *sdata, struct ieee80211_mgmt *mgmt,
  401. size_t len, struct ieee80211_rx_status *rx_status)
  402. {
  403. struct ieee80211_local *local = sdata->local;
  404. struct ieee802_11_elems elems;
  405. struct sta_info *sta;
  406. enum plink_event event;
  407. enum ieee80211_self_protected_actioncode ftype;
  408. size_t baselen;
  409. bool deactivated, matches_local = true;
  410. u8 ie_len;
  411. u8 *baseaddr;
  412. __le16 plid, llid, reason;
  413. #ifdef CONFIG_MAC80211_VERBOSE_MPL_DEBUG
  414. static const char *mplstates[] = {
  415. [NL80211_PLINK_LISTEN] = "LISTEN",
  416. [NL80211_PLINK_OPN_SNT] = "OPN-SNT",
  417. [NL80211_PLINK_OPN_RCVD] = "OPN-RCVD",
  418. [NL80211_PLINK_CNF_RCVD] = "CNF_RCVD",
  419. [NL80211_PLINK_ESTAB] = "ESTAB",
  420. [NL80211_PLINK_HOLDING] = "HOLDING",
  421. [NL80211_PLINK_BLOCKED] = "BLOCKED"
  422. };
  423. #endif
  424. /* need action_code, aux */
  425. if (len < IEEE80211_MIN_ACTION_SIZE + 3)
  426. return;
  427. if (is_multicast_ether_addr(mgmt->da)) {
  428. mpl_dbg("Mesh plink: ignore frame from multicast address");
  429. return;
  430. }
  431. baseaddr = mgmt->u.action.u.self_prot.variable;
  432. baselen = (u8 *) mgmt->u.action.u.self_prot.variable - (u8 *) mgmt;
  433. if (mgmt->u.action.u.self_prot.action_code ==
  434. WLAN_SP_MESH_PEERING_CONFIRM) {
  435. baseaddr += 4;
  436. baselen += 4;
  437. }
  438. ieee802_11_parse_elems(baseaddr, len - baselen, &elems);
  439. if (!elems.peering) {
  440. mpl_dbg("Mesh plink: missing necessary peer link ie\n");
  441. return;
  442. }
  443. if (elems.rsn_len &&
  444. sdata->u.mesh.security == IEEE80211_MESH_SEC_NONE) {
  445. mpl_dbg("Mesh plink: can't establish link with secure peer\n");
  446. return;
  447. }
  448. ftype = mgmt->u.action.u.self_prot.action_code;
  449. ie_len = elems.peering_len;
  450. if ((ftype == WLAN_SP_MESH_PEERING_OPEN && ie_len != 4) ||
  451. (ftype == WLAN_SP_MESH_PEERING_CONFIRM && ie_len != 6) ||
  452. (ftype == WLAN_SP_MESH_PEERING_CLOSE && ie_len != 6
  453. && ie_len != 8)) {
  454. mpl_dbg("Mesh plink: incorrect plink ie length %d %d\n",
  455. ftype, ie_len);
  456. return;
  457. }
  458. if (ftype != WLAN_SP_MESH_PEERING_CLOSE &&
  459. (!elems.mesh_id || !elems.mesh_config)) {
  460. mpl_dbg("Mesh plink: missing necessary ie\n");
  461. return;
  462. }
  463. /* Note the lines below are correct, the llid in the frame is the plid
  464. * from the point of view of this host.
  465. */
  466. memcpy(&plid, PLINK_GET_LLID(elems.peering), 2);
  467. if (ftype == WLAN_SP_MESH_PEERING_CONFIRM ||
  468. (ftype == WLAN_SP_MESH_PEERING_CLOSE && ie_len == 8))
  469. memcpy(&llid, PLINK_GET_PLID(elems.peering), 2);
  470. rcu_read_lock();
  471. sta = sta_info_get(sdata, mgmt->sa);
  472. if (!sta && ftype != WLAN_SP_MESH_PEERING_OPEN) {
  473. mpl_dbg("Mesh plink: cls or cnf from unknown peer\n");
  474. rcu_read_unlock();
  475. return;
  476. }
  477. if (ftype == WLAN_SP_MESH_PEERING_OPEN &&
  478. !sta_meets_rssi_threshold(sta, sdata)) {
  479. mpl_dbg("Mesh plink: %pM does not meet rssi threshold\n",
  480. sta->sta.addr);
  481. rcu_read_unlock();
  482. return;
  483. }
  484. if (sta && !test_sta_flag(sta, WLAN_STA_AUTH)) {
  485. mpl_dbg("Mesh plink: Action frame from non-authed peer\n");
  486. rcu_read_unlock();
  487. return;
  488. }
  489. if (sta && sta->plink_state == NL80211_PLINK_BLOCKED) {
  490. rcu_read_unlock();
  491. return;
  492. }
  493. /* Now we will figure out the appropriate event... */
  494. event = PLINK_UNDEFINED;
  495. if (ftype != WLAN_SP_MESH_PEERING_CLOSE &&
  496. (!mesh_matches_local(&elems, sdata))) {
  497. matches_local = false;
  498. switch (ftype) {
  499. case WLAN_SP_MESH_PEERING_OPEN:
  500. event = OPN_RJCT;
  501. break;
  502. case WLAN_SP_MESH_PEERING_CONFIRM:
  503. event = CNF_RJCT;
  504. break;
  505. default:
  506. break;
  507. }
  508. }
  509. if (!sta && !matches_local) {
  510. rcu_read_unlock();
  511. reason = cpu_to_le16(WLAN_REASON_MESH_CONFIG);
  512. llid = 0;
  513. mesh_plink_frame_tx(sdata, WLAN_SP_MESH_PEERING_CLOSE,
  514. mgmt->sa, llid, plid, reason);
  515. return;
  516. } else if (!sta) {
  517. /* ftype == WLAN_SP_MESH_PEERING_OPEN */
  518. u32 rates;
  519. rcu_read_unlock();
  520. if (!mesh_plink_free_count(sdata)) {
  521. mpl_dbg("Mesh plink error: no more free plinks\n");
  522. return;
  523. }
  524. rates = ieee80211_sta_get_rates(local, &elems, rx_status->band);
  525. sta = mesh_plink_alloc(sdata, mgmt->sa, rates, &elems);
  526. if (!sta) {
  527. mpl_dbg("Mesh plink error: plink table full\n");
  528. return;
  529. }
  530. if (sta_info_insert_rcu(sta)) {
  531. rcu_read_unlock();
  532. return;
  533. }
  534. event = OPN_ACPT;
  535. spin_lock_bh(&sta->lock);
  536. } else if (matches_local) {
  537. spin_lock_bh(&sta->lock);
  538. switch (ftype) {
  539. case WLAN_SP_MESH_PEERING_OPEN:
  540. if (!mesh_plink_free_count(sdata) ||
  541. (sta->plid && sta->plid != plid))
  542. event = OPN_IGNR;
  543. else
  544. event = OPN_ACPT;
  545. break;
  546. case WLAN_SP_MESH_PEERING_CONFIRM:
  547. if (!mesh_plink_free_count(sdata) ||
  548. (sta->llid != llid || sta->plid != plid))
  549. event = CNF_IGNR;
  550. else
  551. event = CNF_ACPT;
  552. break;
  553. case WLAN_SP_MESH_PEERING_CLOSE:
  554. if (sta->plink_state == NL80211_PLINK_ESTAB)
  555. /* Do not check for llid or plid. This does not
  556. * follow the standard but since multiple plinks
  557. * per sta are not supported, it is necessary in
  558. * order to avoid a livelock when MP A sees an
  559. * establish peer link to MP B but MP B does not
  560. * see it. This can be caused by a timeout in
  561. * B's peer link establishment or B beign
  562. * restarted.
  563. */
  564. event = CLS_ACPT;
  565. else if (sta->plid != plid)
  566. event = CLS_IGNR;
  567. else if (ie_len == 7 && sta->llid != llid)
  568. event = CLS_IGNR;
  569. else
  570. event = CLS_ACPT;
  571. break;
  572. default:
  573. mpl_dbg("Mesh plink: unknown frame subtype\n");
  574. spin_unlock_bh(&sta->lock);
  575. rcu_read_unlock();
  576. return;
  577. }
  578. } else {
  579. spin_lock_bh(&sta->lock);
  580. }
  581. mpl_dbg("Mesh plink (peer, state, llid, plid, event): %pM %s %d %d %d\n",
  582. mgmt->sa, mplstates[sta->plink_state],
  583. le16_to_cpu(sta->llid), le16_to_cpu(sta->plid),
  584. event);
  585. reason = 0;
  586. switch (sta->plink_state) {
  587. /* spin_unlock as soon as state is updated at each case */
  588. case NL80211_PLINK_LISTEN:
  589. switch (event) {
  590. case CLS_ACPT:
  591. mesh_plink_fsm_restart(sta);
  592. spin_unlock_bh(&sta->lock);
  593. break;
  594. case OPN_ACPT:
  595. sta->plink_state = NL80211_PLINK_OPN_RCVD;
  596. sta->plid = plid;
  597. get_random_bytes(&llid, 2);
  598. sta->llid = llid;
  599. mesh_plink_timer_set(sta, dot11MeshRetryTimeout(sdata));
  600. spin_unlock_bh(&sta->lock);
  601. mesh_plink_frame_tx(sdata,
  602. WLAN_SP_MESH_PEERING_OPEN,
  603. sta->sta.addr, llid, 0, 0);
  604. mesh_plink_frame_tx(sdata,
  605. WLAN_SP_MESH_PEERING_CONFIRM,
  606. sta->sta.addr, llid, plid, 0);
  607. break;
  608. default:
  609. spin_unlock_bh(&sta->lock);
  610. break;
  611. }
  612. break;
  613. case NL80211_PLINK_OPN_SNT:
  614. switch (event) {
  615. case OPN_RJCT:
  616. case CNF_RJCT:
  617. reason = cpu_to_le16(WLAN_REASON_MESH_CONFIG);
  618. case CLS_ACPT:
  619. if (!reason)
  620. reason = cpu_to_le16(WLAN_REASON_MESH_CLOSE);
  621. sta->reason = reason;
  622. sta->plink_state = NL80211_PLINK_HOLDING;
  623. if (!mod_plink_timer(sta,
  624. dot11MeshHoldingTimeout(sdata)))
  625. sta->ignore_plink_timer = true;
  626. llid = sta->llid;
  627. spin_unlock_bh(&sta->lock);
  628. mesh_plink_frame_tx(sdata,
  629. WLAN_SP_MESH_PEERING_CLOSE,
  630. sta->sta.addr, llid, plid, reason);
  631. break;
  632. case OPN_ACPT:
  633. /* retry timer is left untouched */
  634. sta->plink_state = NL80211_PLINK_OPN_RCVD;
  635. sta->plid = plid;
  636. llid = sta->llid;
  637. spin_unlock_bh(&sta->lock);
  638. mesh_plink_frame_tx(sdata,
  639. WLAN_SP_MESH_PEERING_CONFIRM,
  640. sta->sta.addr, llid, plid, 0);
  641. break;
  642. case CNF_ACPT:
  643. sta->plink_state = NL80211_PLINK_CNF_RCVD;
  644. if (!mod_plink_timer(sta,
  645. dot11MeshConfirmTimeout(sdata)))
  646. sta->ignore_plink_timer = true;
  647. spin_unlock_bh(&sta->lock);
  648. break;
  649. default:
  650. spin_unlock_bh(&sta->lock);
  651. break;
  652. }
  653. break;
  654. case NL80211_PLINK_OPN_RCVD:
  655. switch (event) {
  656. case OPN_RJCT:
  657. case CNF_RJCT:
  658. reason = cpu_to_le16(WLAN_REASON_MESH_CONFIG);
  659. case CLS_ACPT:
  660. if (!reason)
  661. reason = cpu_to_le16(WLAN_REASON_MESH_CLOSE);
  662. sta->reason = reason;
  663. sta->plink_state = NL80211_PLINK_HOLDING;
  664. if (!mod_plink_timer(sta,
  665. dot11MeshHoldingTimeout(sdata)))
  666. sta->ignore_plink_timer = true;
  667. llid = sta->llid;
  668. spin_unlock_bh(&sta->lock);
  669. mesh_plink_frame_tx(sdata, WLAN_SP_MESH_PEERING_CLOSE,
  670. sta->sta.addr, llid, plid, reason);
  671. break;
  672. case OPN_ACPT:
  673. llid = sta->llid;
  674. spin_unlock_bh(&sta->lock);
  675. mesh_plink_frame_tx(sdata,
  676. WLAN_SP_MESH_PEERING_CONFIRM,
  677. sta->sta.addr, llid, plid, 0);
  678. break;
  679. case CNF_ACPT:
  680. del_timer(&sta->plink_timer);
  681. sta->plink_state = NL80211_PLINK_ESTAB;
  682. spin_unlock_bh(&sta->lock);
  683. mesh_plink_inc_estab_count(sdata);
  684. ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BEACON);
  685. mpl_dbg("Mesh plink with %pM ESTABLISHED\n",
  686. sta->sta.addr);
  687. break;
  688. default:
  689. spin_unlock_bh(&sta->lock);
  690. break;
  691. }
  692. break;
  693. case NL80211_PLINK_CNF_RCVD:
  694. switch (event) {
  695. case OPN_RJCT:
  696. case CNF_RJCT:
  697. reason = cpu_to_le16(WLAN_REASON_MESH_CONFIG);
  698. case CLS_ACPT:
  699. if (!reason)
  700. reason = cpu_to_le16(WLAN_REASON_MESH_CLOSE);
  701. sta->reason = reason;
  702. sta->plink_state = NL80211_PLINK_HOLDING;
  703. if (!mod_plink_timer(sta,
  704. dot11MeshHoldingTimeout(sdata)))
  705. sta->ignore_plink_timer = true;
  706. llid = sta->llid;
  707. spin_unlock_bh(&sta->lock);
  708. mesh_plink_frame_tx(sdata,
  709. WLAN_SP_MESH_PEERING_CLOSE,
  710. sta->sta.addr, llid, plid, reason);
  711. break;
  712. case OPN_ACPT:
  713. del_timer(&sta->plink_timer);
  714. sta->plink_state = NL80211_PLINK_ESTAB;
  715. spin_unlock_bh(&sta->lock);
  716. mesh_plink_inc_estab_count(sdata);
  717. ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BEACON);
  718. mpl_dbg("Mesh plink with %pM ESTABLISHED\n",
  719. sta->sta.addr);
  720. mesh_plink_frame_tx(sdata,
  721. WLAN_SP_MESH_PEERING_CONFIRM,
  722. sta->sta.addr, llid, plid, 0);
  723. break;
  724. default:
  725. spin_unlock_bh(&sta->lock);
  726. break;
  727. }
  728. break;
  729. case NL80211_PLINK_ESTAB:
  730. switch (event) {
  731. case CLS_ACPT:
  732. reason = cpu_to_le16(WLAN_REASON_MESH_CLOSE);
  733. sta->reason = reason;
  734. deactivated = __mesh_plink_deactivate(sta);
  735. sta->plink_state = NL80211_PLINK_HOLDING;
  736. llid = sta->llid;
  737. mod_plink_timer(sta, dot11MeshHoldingTimeout(sdata));
  738. spin_unlock_bh(&sta->lock);
  739. if (deactivated)
  740. ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BEACON);
  741. mesh_plink_frame_tx(sdata, WLAN_SP_MESH_PEERING_CLOSE,
  742. sta->sta.addr, llid, plid, reason);
  743. break;
  744. case OPN_ACPT:
  745. llid = sta->llid;
  746. spin_unlock_bh(&sta->lock);
  747. mesh_plink_frame_tx(sdata,
  748. WLAN_SP_MESH_PEERING_CONFIRM,
  749. sta->sta.addr, llid, plid, 0);
  750. break;
  751. default:
  752. spin_unlock_bh(&sta->lock);
  753. break;
  754. }
  755. break;
  756. case NL80211_PLINK_HOLDING:
  757. switch (event) {
  758. case CLS_ACPT:
  759. if (del_timer(&sta->plink_timer))
  760. sta->ignore_plink_timer = 1;
  761. mesh_plink_fsm_restart(sta);
  762. spin_unlock_bh(&sta->lock);
  763. break;
  764. case OPN_ACPT:
  765. case CNF_ACPT:
  766. case OPN_RJCT:
  767. case CNF_RJCT:
  768. llid = sta->llid;
  769. reason = sta->reason;
  770. spin_unlock_bh(&sta->lock);
  771. mesh_plink_frame_tx(sdata, WLAN_SP_MESH_PEERING_CLOSE,
  772. sta->sta.addr, llid, plid, reason);
  773. break;
  774. default:
  775. spin_unlock_bh(&sta->lock);
  776. }
  777. break;
  778. default:
  779. /* should not get here, PLINK_BLOCKED is dealt with at the
  780. * beginning of the function
  781. */
  782. spin_unlock_bh(&sta->lock);
  783. break;
  784. }
  785. rcu_read_unlock();
  786. }