mesh_plink.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886
  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. * @rates: station's supported rates
  239. * @elems: IEs from beacon or mesh peering frame
  240. *
  241. * call under RCU
  242. */
  243. static struct sta_info *mesh_peer_init(struct ieee80211_sub_if_data *sdata,
  244. u8 *addr, u32 rates,
  245. struct ieee802_11_elems *elems)
  246. {
  247. struct ieee80211_local *local = sdata->local;
  248. struct ieee80211_supported_band *sband;
  249. struct sta_info *sta;
  250. sband = local->hw.wiphy->bands[local->oper_channel->band];
  251. sta = sta_info_get(sdata, addr);
  252. if (!sta) {
  253. sta = mesh_plink_alloc(sdata, addr);
  254. if (!sta)
  255. return NULL;
  256. }
  257. spin_lock_bh(&sta->lock);
  258. sta->last_rx = jiffies;
  259. sta->sta.supp_rates[local->hw.conf.channel->band] = rates;
  260. if (elems->ht_cap_elem)
  261. ieee80211_ht_cap_ie_to_sta_ht_cap(sdata, sband,
  262. elems->ht_cap_elem,
  263. &sta->sta.ht_cap);
  264. else
  265. memset(&sta->sta.ht_cap, 0, sizeof(sta->sta.ht_cap));
  266. rate_control_rate_init(sta);
  267. spin_unlock_bh(&sta->lock);
  268. return sta;
  269. }
  270. void mesh_neighbour_update(u8 *hw_addr, u32 rates,
  271. struct ieee80211_sub_if_data *sdata,
  272. struct ieee802_11_elems *elems)
  273. {
  274. struct sta_info *sta;
  275. /* Userspace handles peer allocation when security is enabled */
  276. if (sdata->u.mesh.security & IEEE80211_MESH_SEC_AUTHED) {
  277. cfg80211_notify_new_peer_candidate(sdata->dev, hw_addr,
  278. elems->ie_start,
  279. elems->total_len,
  280. GFP_KERNEL);
  281. return;
  282. }
  283. rcu_read_lock();
  284. sta = mesh_peer_init(sdata, hw_addr, rates, elems);
  285. if (!sta)
  286. goto out;
  287. if (mesh_peer_accepts_plinks(elems) &&
  288. sta->plink_state == NL80211_PLINK_LISTEN &&
  289. sdata->u.mesh.accepting_plinks &&
  290. sdata->u.mesh.mshcfg.auto_open_plinks &&
  291. rssi_threshold_check(sta, sdata))
  292. mesh_plink_open(sta);
  293. out:
  294. rcu_read_unlock();
  295. }
  296. static void mesh_plink_timer(unsigned long data)
  297. {
  298. struct sta_info *sta;
  299. __le16 llid, plid, reason;
  300. struct ieee80211_sub_if_data *sdata;
  301. /*
  302. * This STA is valid because sta_info_destroy() will
  303. * del_timer_sync() this timer after having made sure
  304. * it cannot be readded (by deleting the plink.)
  305. */
  306. sta = (struct sta_info *) data;
  307. if (sta->sdata->local->quiescing) {
  308. sta->plink_timer_was_running = true;
  309. return;
  310. }
  311. spin_lock_bh(&sta->lock);
  312. if (sta->ignore_plink_timer) {
  313. sta->ignore_plink_timer = false;
  314. spin_unlock_bh(&sta->lock);
  315. return;
  316. }
  317. mpl_dbg("Mesh plink timer for %pM fired on state %d\n",
  318. sta->sta.addr, sta->plink_state);
  319. reason = 0;
  320. llid = sta->llid;
  321. plid = sta->plid;
  322. sdata = sta->sdata;
  323. switch (sta->plink_state) {
  324. case NL80211_PLINK_OPN_RCVD:
  325. case NL80211_PLINK_OPN_SNT:
  326. /* retry timer */
  327. if (sta->plink_retries < dot11MeshMaxRetries(sdata)) {
  328. u32 rand;
  329. mpl_dbg("Mesh plink for %pM (retry, timeout): %d %d\n",
  330. sta->sta.addr, sta->plink_retries,
  331. sta->plink_timeout);
  332. get_random_bytes(&rand, sizeof(u32));
  333. sta->plink_timeout = sta->plink_timeout +
  334. rand % sta->plink_timeout;
  335. ++sta->plink_retries;
  336. mod_plink_timer(sta, sta->plink_timeout);
  337. spin_unlock_bh(&sta->lock);
  338. mesh_plink_frame_tx(sdata, WLAN_SP_MESH_PEERING_OPEN,
  339. sta->sta.addr, llid, 0, 0);
  340. break;
  341. }
  342. reason = cpu_to_le16(WLAN_REASON_MESH_MAX_RETRIES);
  343. /* fall through on else */
  344. case NL80211_PLINK_CNF_RCVD:
  345. /* confirm timer */
  346. if (!reason)
  347. reason = cpu_to_le16(WLAN_REASON_MESH_CONFIRM_TIMEOUT);
  348. sta->plink_state = NL80211_PLINK_HOLDING;
  349. mod_plink_timer(sta, dot11MeshHoldingTimeout(sdata));
  350. spin_unlock_bh(&sta->lock);
  351. mesh_plink_frame_tx(sdata, WLAN_SP_MESH_PEERING_CLOSE,
  352. sta->sta.addr, llid, plid, reason);
  353. break;
  354. case NL80211_PLINK_HOLDING:
  355. /* holding timer */
  356. del_timer(&sta->plink_timer);
  357. mesh_plink_fsm_restart(sta);
  358. spin_unlock_bh(&sta->lock);
  359. break;
  360. default:
  361. spin_unlock_bh(&sta->lock);
  362. break;
  363. }
  364. }
  365. #ifdef CONFIG_PM
  366. void mesh_plink_quiesce(struct sta_info *sta)
  367. {
  368. if (del_timer_sync(&sta->plink_timer))
  369. sta->plink_timer_was_running = true;
  370. }
  371. void mesh_plink_restart(struct sta_info *sta)
  372. {
  373. if (sta->plink_timer_was_running) {
  374. add_timer(&sta->plink_timer);
  375. sta->plink_timer_was_running = false;
  376. }
  377. }
  378. #endif
  379. static inline void mesh_plink_timer_set(struct sta_info *sta, int timeout)
  380. {
  381. sta->plink_timer.expires = jiffies + (HZ * timeout / 1000);
  382. sta->plink_timer.data = (unsigned long) sta;
  383. sta->plink_timer.function = mesh_plink_timer;
  384. sta->plink_timeout = timeout;
  385. add_timer(&sta->plink_timer);
  386. }
  387. int mesh_plink_open(struct sta_info *sta)
  388. {
  389. __le16 llid;
  390. struct ieee80211_sub_if_data *sdata = sta->sdata;
  391. if (!test_sta_flag(sta, WLAN_STA_AUTH))
  392. return -EPERM;
  393. spin_lock_bh(&sta->lock);
  394. get_random_bytes(&llid, 2);
  395. sta->llid = llid;
  396. if (sta->plink_state != NL80211_PLINK_LISTEN) {
  397. spin_unlock_bh(&sta->lock);
  398. return -EBUSY;
  399. }
  400. sta->plink_state = NL80211_PLINK_OPN_SNT;
  401. mesh_plink_timer_set(sta, dot11MeshRetryTimeout(sdata));
  402. spin_unlock_bh(&sta->lock);
  403. mpl_dbg("Mesh plink: starting establishment with %pM\n",
  404. sta->sta.addr);
  405. return mesh_plink_frame_tx(sdata, WLAN_SP_MESH_PEERING_OPEN,
  406. sta->sta.addr, llid, 0, 0);
  407. }
  408. void mesh_plink_block(struct sta_info *sta)
  409. {
  410. struct ieee80211_sub_if_data *sdata = sta->sdata;
  411. bool deactivated;
  412. spin_lock_bh(&sta->lock);
  413. deactivated = __mesh_plink_deactivate(sta);
  414. sta->plink_state = NL80211_PLINK_BLOCKED;
  415. spin_unlock_bh(&sta->lock);
  416. if (deactivated)
  417. ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BEACON);
  418. }
  419. void mesh_rx_plink_frame(struct ieee80211_sub_if_data *sdata, struct ieee80211_mgmt *mgmt,
  420. size_t len, struct ieee80211_rx_status *rx_status)
  421. {
  422. struct ieee80211_local *local = sdata->local;
  423. struct ieee802_11_elems elems;
  424. struct sta_info *sta;
  425. enum plink_event event;
  426. enum ieee80211_self_protected_actioncode ftype;
  427. size_t baselen;
  428. bool deactivated, matches_local = true;
  429. u8 ie_len;
  430. u8 *baseaddr;
  431. u32 rates, basic_rates = 0;
  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. rates = ieee80211_sta_get_rates(local, &elems,
  516. rx_status->band, &basic_rates);
  517. if (ftype != WLAN_SP_MESH_PEERING_CLOSE &&
  518. (!mesh_matches_local(&elems, sdata, basic_rates))) {
  519. matches_local = false;
  520. switch (ftype) {
  521. case WLAN_SP_MESH_PEERING_OPEN:
  522. event = OPN_RJCT;
  523. break;
  524. case WLAN_SP_MESH_PEERING_CONFIRM:
  525. event = CNF_RJCT;
  526. break;
  527. default:
  528. break;
  529. }
  530. }
  531. if (!sta && !matches_local) {
  532. rcu_read_unlock();
  533. reason = cpu_to_le16(WLAN_REASON_MESH_CONFIG);
  534. llid = 0;
  535. mesh_plink_frame_tx(sdata, WLAN_SP_MESH_PEERING_CLOSE,
  536. mgmt->sa, llid, plid, reason);
  537. return;
  538. } else if (!sta) {
  539. /* ftype == WLAN_SP_MESH_PEERING_OPEN */
  540. if (!mesh_plink_free_count(sdata)) {
  541. mpl_dbg("Mesh plink error: no more free plinks\n");
  542. rcu_read_unlock();
  543. return;
  544. }
  545. event = OPN_ACPT;
  546. } else if (matches_local) {
  547. switch (ftype) {
  548. case WLAN_SP_MESH_PEERING_OPEN:
  549. if (!mesh_plink_free_count(sdata) ||
  550. (sta->plid && sta->plid != plid))
  551. event = OPN_IGNR;
  552. else
  553. event = OPN_ACPT;
  554. break;
  555. case WLAN_SP_MESH_PEERING_CONFIRM:
  556. if (!mesh_plink_free_count(sdata) ||
  557. (sta->llid != llid || sta->plid != plid))
  558. event = CNF_IGNR;
  559. else
  560. event = CNF_ACPT;
  561. break;
  562. case WLAN_SP_MESH_PEERING_CLOSE:
  563. if (sta->plink_state == NL80211_PLINK_ESTAB)
  564. /* Do not check for llid or plid. This does not
  565. * follow the standard but since multiple plinks
  566. * per sta are not supported, it is necessary in
  567. * order to avoid a livelock when MP A sees an
  568. * establish peer link to MP B but MP B does not
  569. * see it. This can be caused by a timeout in
  570. * B's peer link establishment or B beign
  571. * restarted.
  572. */
  573. event = CLS_ACPT;
  574. else if (sta->plid != plid)
  575. event = CLS_IGNR;
  576. else if (ie_len == 7 && sta->llid != llid)
  577. event = CLS_IGNR;
  578. else
  579. event = CLS_ACPT;
  580. break;
  581. default:
  582. mpl_dbg("Mesh plink: unknown frame subtype\n");
  583. rcu_read_unlock();
  584. return;
  585. }
  586. }
  587. if (event == OPN_ACPT) {
  588. /* allocate sta entry if necessary and update info */
  589. sta = mesh_peer_init(sdata, mgmt->sa, rates, &elems);
  590. if (!sta) {
  591. mpl_dbg("Mesh plink: failed to init peer!\n");
  592. rcu_read_unlock();
  593. return;
  594. }
  595. }
  596. mpl_dbg("Mesh plink (peer, state, llid, plid, event): %pM %s %d %d %d\n",
  597. mgmt->sa, mplstates[sta->plink_state],
  598. le16_to_cpu(sta->llid), le16_to_cpu(sta->plid),
  599. event);
  600. reason = 0;
  601. spin_lock_bh(&sta->lock);
  602. switch (sta->plink_state) {
  603. /* spin_unlock as soon as state is updated at each case */
  604. case NL80211_PLINK_LISTEN:
  605. switch (event) {
  606. case CLS_ACPT:
  607. mesh_plink_fsm_restart(sta);
  608. spin_unlock_bh(&sta->lock);
  609. break;
  610. case OPN_ACPT:
  611. sta->plink_state = NL80211_PLINK_OPN_RCVD;
  612. sta->plid = plid;
  613. get_random_bytes(&llid, 2);
  614. sta->llid = llid;
  615. mesh_plink_timer_set(sta, dot11MeshRetryTimeout(sdata));
  616. spin_unlock_bh(&sta->lock);
  617. mesh_plink_frame_tx(sdata,
  618. WLAN_SP_MESH_PEERING_OPEN,
  619. sta->sta.addr, llid, 0, 0);
  620. mesh_plink_frame_tx(sdata,
  621. WLAN_SP_MESH_PEERING_CONFIRM,
  622. sta->sta.addr, llid, plid, 0);
  623. break;
  624. default:
  625. spin_unlock_bh(&sta->lock);
  626. break;
  627. }
  628. break;
  629. case NL80211_PLINK_OPN_SNT:
  630. switch (event) {
  631. case OPN_RJCT:
  632. case CNF_RJCT:
  633. reason = cpu_to_le16(WLAN_REASON_MESH_CONFIG);
  634. case CLS_ACPT:
  635. if (!reason)
  636. reason = cpu_to_le16(WLAN_REASON_MESH_CLOSE);
  637. sta->reason = reason;
  638. sta->plink_state = NL80211_PLINK_HOLDING;
  639. if (!mod_plink_timer(sta,
  640. dot11MeshHoldingTimeout(sdata)))
  641. sta->ignore_plink_timer = true;
  642. llid = sta->llid;
  643. spin_unlock_bh(&sta->lock);
  644. mesh_plink_frame_tx(sdata,
  645. WLAN_SP_MESH_PEERING_CLOSE,
  646. sta->sta.addr, llid, plid, reason);
  647. break;
  648. case OPN_ACPT:
  649. /* retry timer is left untouched */
  650. sta->plink_state = NL80211_PLINK_OPN_RCVD;
  651. sta->plid = plid;
  652. llid = sta->llid;
  653. spin_unlock_bh(&sta->lock);
  654. mesh_plink_frame_tx(sdata,
  655. WLAN_SP_MESH_PEERING_CONFIRM,
  656. sta->sta.addr, llid, plid, 0);
  657. break;
  658. case CNF_ACPT:
  659. sta->plink_state = NL80211_PLINK_CNF_RCVD;
  660. if (!mod_plink_timer(sta,
  661. dot11MeshConfirmTimeout(sdata)))
  662. sta->ignore_plink_timer = true;
  663. spin_unlock_bh(&sta->lock);
  664. break;
  665. default:
  666. spin_unlock_bh(&sta->lock);
  667. break;
  668. }
  669. break;
  670. case NL80211_PLINK_OPN_RCVD:
  671. switch (event) {
  672. case OPN_RJCT:
  673. case CNF_RJCT:
  674. reason = cpu_to_le16(WLAN_REASON_MESH_CONFIG);
  675. case CLS_ACPT:
  676. if (!reason)
  677. reason = cpu_to_le16(WLAN_REASON_MESH_CLOSE);
  678. sta->reason = reason;
  679. sta->plink_state = NL80211_PLINK_HOLDING;
  680. if (!mod_plink_timer(sta,
  681. dot11MeshHoldingTimeout(sdata)))
  682. sta->ignore_plink_timer = true;
  683. llid = sta->llid;
  684. spin_unlock_bh(&sta->lock);
  685. mesh_plink_frame_tx(sdata, WLAN_SP_MESH_PEERING_CLOSE,
  686. sta->sta.addr, llid, plid, reason);
  687. break;
  688. case OPN_ACPT:
  689. llid = sta->llid;
  690. spin_unlock_bh(&sta->lock);
  691. mesh_plink_frame_tx(sdata,
  692. WLAN_SP_MESH_PEERING_CONFIRM,
  693. sta->sta.addr, llid, plid, 0);
  694. break;
  695. case CNF_ACPT:
  696. del_timer(&sta->plink_timer);
  697. sta->plink_state = NL80211_PLINK_ESTAB;
  698. spin_unlock_bh(&sta->lock);
  699. mesh_plink_inc_estab_count(sdata);
  700. ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BEACON);
  701. mpl_dbg("Mesh plink with %pM ESTABLISHED\n",
  702. sta->sta.addr);
  703. break;
  704. default:
  705. spin_unlock_bh(&sta->lock);
  706. break;
  707. }
  708. break;
  709. case NL80211_PLINK_CNF_RCVD:
  710. switch (event) {
  711. case OPN_RJCT:
  712. case CNF_RJCT:
  713. reason = cpu_to_le16(WLAN_REASON_MESH_CONFIG);
  714. case CLS_ACPT:
  715. if (!reason)
  716. reason = cpu_to_le16(WLAN_REASON_MESH_CLOSE);
  717. sta->reason = reason;
  718. sta->plink_state = NL80211_PLINK_HOLDING;
  719. if (!mod_plink_timer(sta,
  720. dot11MeshHoldingTimeout(sdata)))
  721. sta->ignore_plink_timer = true;
  722. llid = sta->llid;
  723. spin_unlock_bh(&sta->lock);
  724. mesh_plink_frame_tx(sdata,
  725. WLAN_SP_MESH_PEERING_CLOSE,
  726. sta->sta.addr, llid, plid, reason);
  727. break;
  728. case OPN_ACPT:
  729. del_timer(&sta->plink_timer);
  730. sta->plink_state = NL80211_PLINK_ESTAB;
  731. spin_unlock_bh(&sta->lock);
  732. mesh_plink_inc_estab_count(sdata);
  733. ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BEACON);
  734. mpl_dbg("Mesh plink with %pM ESTABLISHED\n",
  735. sta->sta.addr);
  736. mesh_plink_frame_tx(sdata,
  737. WLAN_SP_MESH_PEERING_CONFIRM,
  738. sta->sta.addr, llid, plid, 0);
  739. break;
  740. default:
  741. spin_unlock_bh(&sta->lock);
  742. break;
  743. }
  744. break;
  745. case NL80211_PLINK_ESTAB:
  746. switch (event) {
  747. case CLS_ACPT:
  748. reason = cpu_to_le16(WLAN_REASON_MESH_CLOSE);
  749. sta->reason = reason;
  750. deactivated = __mesh_plink_deactivate(sta);
  751. sta->plink_state = NL80211_PLINK_HOLDING;
  752. llid = sta->llid;
  753. mod_plink_timer(sta, dot11MeshHoldingTimeout(sdata));
  754. spin_unlock_bh(&sta->lock);
  755. if (deactivated)
  756. ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BEACON);
  757. mesh_plink_frame_tx(sdata, WLAN_SP_MESH_PEERING_CLOSE,
  758. sta->sta.addr, llid, plid, reason);
  759. break;
  760. case OPN_ACPT:
  761. llid = sta->llid;
  762. spin_unlock_bh(&sta->lock);
  763. mesh_plink_frame_tx(sdata,
  764. WLAN_SP_MESH_PEERING_CONFIRM,
  765. sta->sta.addr, llid, plid, 0);
  766. break;
  767. default:
  768. spin_unlock_bh(&sta->lock);
  769. break;
  770. }
  771. break;
  772. case NL80211_PLINK_HOLDING:
  773. switch (event) {
  774. case CLS_ACPT:
  775. if (del_timer(&sta->plink_timer))
  776. sta->ignore_plink_timer = 1;
  777. mesh_plink_fsm_restart(sta);
  778. spin_unlock_bh(&sta->lock);
  779. break;
  780. case OPN_ACPT:
  781. case CNF_ACPT:
  782. case OPN_RJCT:
  783. case CNF_RJCT:
  784. llid = sta->llid;
  785. reason = sta->reason;
  786. spin_unlock_bh(&sta->lock);
  787. mesh_plink_frame_tx(sdata, WLAN_SP_MESH_PEERING_CLOSE,
  788. sta->sta.addr, llid, plid, reason);
  789. break;
  790. default:
  791. spin_unlock_bh(&sta->lock);
  792. }
  793. break;
  794. default:
  795. /* should not get here, PLINK_BLOCKED is dealt with at the
  796. * beginning of the function
  797. */
  798. spin_unlock_bh(&sta->lock);
  799. break;
  800. }
  801. rcu_read_unlock();
  802. }