mesh_plink.c 23 KB

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