mesh_plink.c 22 KB

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