mesh_plink.c 21 KB

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