mesh_plink.c 22 KB

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