mesh_plink.c 21 KB

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