mesh_plink.c 21 KB

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