mesh_plink.c 23 KB

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