mesh_plink.c 22 KB

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