mesh_plink.c 22 KB

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