mesh_plink.c 22 KB

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