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_operation) +
  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, true) ||
  189. ieee80211_add_ext_srates_ie(&sdata->vif, skb, true) ||
  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_oper_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. u32 rates, basic_rates = 0;
  414. __le16 plid, llid, reason;
  415. #ifdef CONFIG_MAC80211_VERBOSE_MPL_DEBUG
  416. static const char *mplstates[] = {
  417. [NL80211_PLINK_LISTEN] = "LISTEN",
  418. [NL80211_PLINK_OPN_SNT] = "OPN-SNT",
  419. [NL80211_PLINK_OPN_RCVD] = "OPN-RCVD",
  420. [NL80211_PLINK_CNF_RCVD] = "CNF_RCVD",
  421. [NL80211_PLINK_ESTAB] = "ESTAB",
  422. [NL80211_PLINK_HOLDING] = "HOLDING",
  423. [NL80211_PLINK_BLOCKED] = "BLOCKED"
  424. };
  425. #endif
  426. /* need action_code, aux */
  427. if (len < IEEE80211_MIN_ACTION_SIZE + 3)
  428. return;
  429. if (is_multicast_ether_addr(mgmt->da)) {
  430. mpl_dbg("Mesh plink: ignore frame from multicast address");
  431. return;
  432. }
  433. baseaddr = mgmt->u.action.u.self_prot.variable;
  434. baselen = (u8 *) mgmt->u.action.u.self_prot.variable - (u8 *) mgmt;
  435. if (mgmt->u.action.u.self_prot.action_code ==
  436. WLAN_SP_MESH_PEERING_CONFIRM) {
  437. baseaddr += 4;
  438. baselen += 4;
  439. }
  440. ieee802_11_parse_elems(baseaddr, len - baselen, &elems);
  441. if (!elems.peering) {
  442. mpl_dbg("Mesh plink: missing necessary peer link ie\n");
  443. return;
  444. }
  445. if (elems.rsn_len &&
  446. sdata->u.mesh.security == IEEE80211_MESH_SEC_NONE) {
  447. mpl_dbg("Mesh plink: can't establish link with secure peer\n");
  448. return;
  449. }
  450. ftype = mgmt->u.action.u.self_prot.action_code;
  451. ie_len = elems.peering_len;
  452. if ((ftype == WLAN_SP_MESH_PEERING_OPEN && ie_len != 4) ||
  453. (ftype == WLAN_SP_MESH_PEERING_CONFIRM && ie_len != 6) ||
  454. (ftype == WLAN_SP_MESH_PEERING_CLOSE && ie_len != 6
  455. && ie_len != 8)) {
  456. mpl_dbg("Mesh plink: incorrect plink ie length %d %d\n",
  457. ftype, ie_len);
  458. return;
  459. }
  460. if (ftype != WLAN_SP_MESH_PEERING_CLOSE &&
  461. (!elems.mesh_id || !elems.mesh_config)) {
  462. mpl_dbg("Mesh plink: missing necessary ie\n");
  463. return;
  464. }
  465. /* Note the lines below are correct, the llid in the frame is the plid
  466. * from the point of view of this host.
  467. */
  468. memcpy(&plid, PLINK_GET_LLID(elems.peering), 2);
  469. if (ftype == WLAN_SP_MESH_PEERING_CONFIRM ||
  470. (ftype == WLAN_SP_MESH_PEERING_CLOSE && ie_len == 8))
  471. memcpy(&llid, PLINK_GET_PLID(elems.peering), 2);
  472. rcu_read_lock();
  473. sta = sta_info_get(sdata, mgmt->sa);
  474. if (!sta && ftype != WLAN_SP_MESH_PEERING_OPEN) {
  475. mpl_dbg("Mesh plink: cls or cnf from unknown peer\n");
  476. rcu_read_unlock();
  477. return;
  478. }
  479. if (ftype == WLAN_SP_MESH_PEERING_OPEN &&
  480. !rssi_threshold_check(sta, sdata)) {
  481. mpl_dbg("Mesh plink: %pM does not meet rssi threshold\n",
  482. mgmt->sa);
  483. rcu_read_unlock();
  484. return;
  485. }
  486. if (sta && !test_sta_flag(sta, WLAN_STA_AUTH)) {
  487. mpl_dbg("Mesh plink: Action frame from non-authed peer\n");
  488. rcu_read_unlock();
  489. return;
  490. }
  491. if (sta && sta->plink_state == NL80211_PLINK_BLOCKED) {
  492. rcu_read_unlock();
  493. return;
  494. }
  495. /* Now we will figure out the appropriate event... */
  496. event = PLINK_UNDEFINED;
  497. rates = ieee80211_sta_get_rates(local, &elems,
  498. rx_status->band, &basic_rates);
  499. if (ftype != WLAN_SP_MESH_PEERING_CLOSE &&
  500. (!mesh_matches_local(&elems, sdata, basic_rates))) {
  501. matches_local = false;
  502. switch (ftype) {
  503. case WLAN_SP_MESH_PEERING_OPEN:
  504. event = OPN_RJCT;
  505. break;
  506. case WLAN_SP_MESH_PEERING_CONFIRM:
  507. event = CNF_RJCT;
  508. break;
  509. default:
  510. break;
  511. }
  512. }
  513. if (!sta && !matches_local) {
  514. rcu_read_unlock();
  515. reason = cpu_to_le16(WLAN_REASON_MESH_CONFIG);
  516. llid = 0;
  517. mesh_plink_frame_tx(sdata, WLAN_SP_MESH_PEERING_CLOSE,
  518. mgmt->sa, llid, plid, reason);
  519. return;
  520. } else if (!sta) {
  521. /* ftype == WLAN_SP_MESH_PEERING_OPEN */
  522. rcu_read_unlock();
  523. if (!mesh_plink_free_count(sdata)) {
  524. mpl_dbg("Mesh plink error: no more free plinks\n");
  525. return;
  526. }
  527. sta = mesh_plink_alloc(sdata, mgmt->sa, rates, &elems);
  528. if (!sta) {
  529. mpl_dbg("Mesh plink error: plink table full\n");
  530. return;
  531. }
  532. if (sta_info_insert_rcu(sta)) {
  533. rcu_read_unlock();
  534. return;
  535. }
  536. event = OPN_ACPT;
  537. spin_lock_bh(&sta->lock);
  538. } else if (matches_local) {
  539. spin_lock_bh(&sta->lock);
  540. switch (ftype) {
  541. case WLAN_SP_MESH_PEERING_OPEN:
  542. if (!mesh_plink_free_count(sdata) ||
  543. (sta->plid && sta->plid != plid))
  544. event = OPN_IGNR;
  545. else
  546. event = OPN_ACPT;
  547. break;
  548. case WLAN_SP_MESH_PEERING_CONFIRM:
  549. if (!mesh_plink_free_count(sdata) ||
  550. (sta->llid != llid || sta->plid != plid))
  551. event = CNF_IGNR;
  552. else
  553. event = CNF_ACPT;
  554. break;
  555. case WLAN_SP_MESH_PEERING_CLOSE:
  556. if (sta->plink_state == NL80211_PLINK_ESTAB)
  557. /* Do not check for llid or plid. This does not
  558. * follow the standard but since multiple plinks
  559. * per sta are not supported, it is necessary in
  560. * order to avoid a livelock when MP A sees an
  561. * establish peer link to MP B but MP B does not
  562. * see it. This can be caused by a timeout in
  563. * B's peer link establishment or B beign
  564. * restarted.
  565. */
  566. event = CLS_ACPT;
  567. else if (sta->plid != plid)
  568. event = CLS_IGNR;
  569. else if (ie_len == 7 && sta->llid != llid)
  570. event = CLS_IGNR;
  571. else
  572. event = CLS_ACPT;
  573. break;
  574. default:
  575. mpl_dbg("Mesh plink: unknown frame subtype\n");
  576. spin_unlock_bh(&sta->lock);
  577. rcu_read_unlock();
  578. return;
  579. }
  580. } else {
  581. spin_lock_bh(&sta->lock);
  582. }
  583. mpl_dbg("Mesh plink (peer, state, llid, plid, event): %pM %s %d %d %d\n",
  584. mgmt->sa, mplstates[sta->plink_state],
  585. le16_to_cpu(sta->llid), le16_to_cpu(sta->plid),
  586. event);
  587. reason = 0;
  588. switch (sta->plink_state) {
  589. /* spin_unlock as soon as state is updated at each case */
  590. case NL80211_PLINK_LISTEN:
  591. switch (event) {
  592. case CLS_ACPT:
  593. mesh_plink_fsm_restart(sta);
  594. spin_unlock_bh(&sta->lock);
  595. break;
  596. case OPN_ACPT:
  597. sta->plink_state = NL80211_PLINK_OPN_RCVD;
  598. sta->plid = plid;
  599. get_random_bytes(&llid, 2);
  600. sta->llid = llid;
  601. mesh_plink_timer_set(sta, dot11MeshRetryTimeout(sdata));
  602. spin_unlock_bh(&sta->lock);
  603. mesh_plink_frame_tx(sdata,
  604. WLAN_SP_MESH_PEERING_OPEN,
  605. sta->sta.addr, llid, 0, 0);
  606. mesh_plink_frame_tx(sdata,
  607. WLAN_SP_MESH_PEERING_CONFIRM,
  608. sta->sta.addr, llid, plid, 0);
  609. break;
  610. default:
  611. spin_unlock_bh(&sta->lock);
  612. break;
  613. }
  614. break;
  615. case NL80211_PLINK_OPN_SNT:
  616. switch (event) {
  617. case OPN_RJCT:
  618. case CNF_RJCT:
  619. reason = cpu_to_le16(WLAN_REASON_MESH_CONFIG);
  620. case CLS_ACPT:
  621. if (!reason)
  622. reason = cpu_to_le16(WLAN_REASON_MESH_CLOSE);
  623. sta->reason = reason;
  624. sta->plink_state = NL80211_PLINK_HOLDING;
  625. if (!mod_plink_timer(sta,
  626. dot11MeshHoldingTimeout(sdata)))
  627. sta->ignore_plink_timer = true;
  628. llid = sta->llid;
  629. spin_unlock_bh(&sta->lock);
  630. mesh_plink_frame_tx(sdata,
  631. WLAN_SP_MESH_PEERING_CLOSE,
  632. sta->sta.addr, llid, plid, reason);
  633. break;
  634. case OPN_ACPT:
  635. /* retry timer is left untouched */
  636. sta->plink_state = NL80211_PLINK_OPN_RCVD;
  637. sta->plid = plid;
  638. llid = sta->llid;
  639. spin_unlock_bh(&sta->lock);
  640. mesh_plink_frame_tx(sdata,
  641. WLAN_SP_MESH_PEERING_CONFIRM,
  642. sta->sta.addr, llid, plid, 0);
  643. break;
  644. case CNF_ACPT:
  645. sta->plink_state = NL80211_PLINK_CNF_RCVD;
  646. if (!mod_plink_timer(sta,
  647. dot11MeshConfirmTimeout(sdata)))
  648. sta->ignore_plink_timer = true;
  649. spin_unlock_bh(&sta->lock);
  650. break;
  651. default:
  652. spin_unlock_bh(&sta->lock);
  653. break;
  654. }
  655. break;
  656. case NL80211_PLINK_OPN_RCVD:
  657. switch (event) {
  658. case OPN_RJCT:
  659. case CNF_RJCT:
  660. reason = cpu_to_le16(WLAN_REASON_MESH_CONFIG);
  661. case CLS_ACPT:
  662. if (!reason)
  663. reason = cpu_to_le16(WLAN_REASON_MESH_CLOSE);
  664. sta->reason = reason;
  665. sta->plink_state = NL80211_PLINK_HOLDING;
  666. if (!mod_plink_timer(sta,
  667. dot11MeshHoldingTimeout(sdata)))
  668. sta->ignore_plink_timer = true;
  669. llid = sta->llid;
  670. spin_unlock_bh(&sta->lock);
  671. mesh_plink_frame_tx(sdata, WLAN_SP_MESH_PEERING_CLOSE,
  672. sta->sta.addr, llid, plid, reason);
  673. break;
  674. case OPN_ACPT:
  675. llid = sta->llid;
  676. spin_unlock_bh(&sta->lock);
  677. mesh_plink_frame_tx(sdata,
  678. WLAN_SP_MESH_PEERING_CONFIRM,
  679. sta->sta.addr, llid, plid, 0);
  680. break;
  681. case CNF_ACPT:
  682. del_timer(&sta->plink_timer);
  683. sta->plink_state = NL80211_PLINK_ESTAB;
  684. spin_unlock_bh(&sta->lock);
  685. mesh_plink_inc_estab_count(sdata);
  686. ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BEACON);
  687. mpl_dbg("Mesh plink with %pM ESTABLISHED\n",
  688. sta->sta.addr);
  689. break;
  690. default:
  691. spin_unlock_bh(&sta->lock);
  692. break;
  693. }
  694. break;
  695. case NL80211_PLINK_CNF_RCVD:
  696. switch (event) {
  697. case OPN_RJCT:
  698. case CNF_RJCT:
  699. reason = cpu_to_le16(WLAN_REASON_MESH_CONFIG);
  700. case CLS_ACPT:
  701. if (!reason)
  702. reason = cpu_to_le16(WLAN_REASON_MESH_CLOSE);
  703. sta->reason = reason;
  704. sta->plink_state = NL80211_PLINK_HOLDING;
  705. if (!mod_plink_timer(sta,
  706. dot11MeshHoldingTimeout(sdata)))
  707. sta->ignore_plink_timer = true;
  708. llid = sta->llid;
  709. spin_unlock_bh(&sta->lock);
  710. mesh_plink_frame_tx(sdata,
  711. WLAN_SP_MESH_PEERING_CLOSE,
  712. sta->sta.addr, llid, plid, reason);
  713. break;
  714. case OPN_ACPT:
  715. del_timer(&sta->plink_timer);
  716. sta->plink_state = NL80211_PLINK_ESTAB;
  717. spin_unlock_bh(&sta->lock);
  718. mesh_plink_inc_estab_count(sdata);
  719. ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BEACON);
  720. mpl_dbg("Mesh plink with %pM ESTABLISHED\n",
  721. sta->sta.addr);
  722. mesh_plink_frame_tx(sdata,
  723. WLAN_SP_MESH_PEERING_CONFIRM,
  724. sta->sta.addr, llid, plid, 0);
  725. break;
  726. default:
  727. spin_unlock_bh(&sta->lock);
  728. break;
  729. }
  730. break;
  731. case NL80211_PLINK_ESTAB:
  732. switch (event) {
  733. case CLS_ACPT:
  734. reason = cpu_to_le16(WLAN_REASON_MESH_CLOSE);
  735. sta->reason = reason;
  736. deactivated = __mesh_plink_deactivate(sta);
  737. sta->plink_state = NL80211_PLINK_HOLDING;
  738. llid = sta->llid;
  739. mod_plink_timer(sta, dot11MeshHoldingTimeout(sdata));
  740. spin_unlock_bh(&sta->lock);
  741. if (deactivated)
  742. ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BEACON);
  743. mesh_plink_frame_tx(sdata, WLAN_SP_MESH_PEERING_CLOSE,
  744. sta->sta.addr, llid, plid, reason);
  745. break;
  746. case OPN_ACPT:
  747. llid = sta->llid;
  748. spin_unlock_bh(&sta->lock);
  749. mesh_plink_frame_tx(sdata,
  750. WLAN_SP_MESH_PEERING_CONFIRM,
  751. sta->sta.addr, llid, plid, 0);
  752. break;
  753. default:
  754. spin_unlock_bh(&sta->lock);
  755. break;
  756. }
  757. break;
  758. case NL80211_PLINK_HOLDING:
  759. switch (event) {
  760. case CLS_ACPT:
  761. if (del_timer(&sta->plink_timer))
  762. sta->ignore_plink_timer = 1;
  763. mesh_plink_fsm_restart(sta);
  764. spin_unlock_bh(&sta->lock);
  765. break;
  766. case OPN_ACPT:
  767. case CNF_ACPT:
  768. case OPN_RJCT:
  769. case CNF_RJCT:
  770. llid = sta->llid;
  771. reason = sta->reason;
  772. spin_unlock_bh(&sta->lock);
  773. mesh_plink_frame_tx(sdata, WLAN_SP_MESH_PEERING_CLOSE,
  774. sta->sta.addr, llid, plid, reason);
  775. break;
  776. default:
  777. spin_unlock_bh(&sta->lock);
  778. }
  779. break;
  780. default:
  781. /* should not get here, PLINK_BLOCKED is dealt with at the
  782. * beginning of the function
  783. */
  784. spin_unlock_bh(&sta->lock);
  785. break;
  786. }
  787. rcu_read_unlock();
  788. }