mesh_plink.c 22 KB

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