mesh_plink.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884
  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. sdata->local->_oper_channel_type != NL80211_CHAN_NO_HT)
  264. ieee80211_ht_cap_ie_to_sta_ht_cap(sdata, sband,
  265. elems->ht_cap_elem,
  266. &sta->sta.ht_cap);
  267. else
  268. memset(&sta->sta.ht_cap, 0, sizeof(sta->sta.ht_cap));
  269. rate_control_rate_init(sta);
  270. spin_unlock_bh(&sta->lock);
  271. return sta;
  272. }
  273. void mesh_neighbour_update(struct ieee80211_sub_if_data *sdata,
  274. u8 *hw_addr,
  275. struct ieee802_11_elems *elems)
  276. {
  277. struct sta_info *sta;
  278. /* Userspace handles peer allocation when security is enabled */
  279. if (sdata->u.mesh.security & IEEE80211_MESH_SEC_AUTHED) {
  280. cfg80211_notify_new_peer_candidate(sdata->dev, hw_addr,
  281. elems->ie_start,
  282. elems->total_len,
  283. GFP_KERNEL);
  284. return;
  285. }
  286. rcu_read_lock();
  287. sta = mesh_peer_init(sdata, hw_addr, elems);
  288. if (!sta)
  289. goto out;
  290. if (mesh_peer_accepts_plinks(elems) &&
  291. sta->plink_state == NL80211_PLINK_LISTEN &&
  292. sdata->u.mesh.accepting_plinks &&
  293. sdata->u.mesh.mshcfg.auto_open_plinks &&
  294. rssi_threshold_check(sta, sdata))
  295. mesh_plink_open(sta);
  296. out:
  297. rcu_read_unlock();
  298. }
  299. static void mesh_plink_timer(unsigned long data)
  300. {
  301. struct sta_info *sta;
  302. __le16 llid, plid, reason;
  303. struct ieee80211_sub_if_data *sdata;
  304. /*
  305. * This STA is valid because sta_info_destroy() will
  306. * del_timer_sync() this timer after having made sure
  307. * it cannot be readded (by deleting the plink.)
  308. */
  309. sta = (struct sta_info *) data;
  310. if (sta->sdata->local->quiescing) {
  311. sta->plink_timer_was_running = true;
  312. return;
  313. }
  314. spin_lock_bh(&sta->lock);
  315. if (sta->ignore_plink_timer) {
  316. sta->ignore_plink_timer = false;
  317. spin_unlock_bh(&sta->lock);
  318. return;
  319. }
  320. mpl_dbg("Mesh plink timer for %pM fired on state %d\n",
  321. sta->sta.addr, sta->plink_state);
  322. reason = 0;
  323. llid = sta->llid;
  324. plid = sta->plid;
  325. sdata = sta->sdata;
  326. switch (sta->plink_state) {
  327. case NL80211_PLINK_OPN_RCVD:
  328. case NL80211_PLINK_OPN_SNT:
  329. /* retry timer */
  330. if (sta->plink_retries < dot11MeshMaxRetries(sdata)) {
  331. u32 rand;
  332. mpl_dbg("Mesh plink for %pM (retry, timeout): %d %d\n",
  333. sta->sta.addr, sta->plink_retries,
  334. sta->plink_timeout);
  335. get_random_bytes(&rand, sizeof(u32));
  336. sta->plink_timeout = sta->plink_timeout +
  337. rand % sta->plink_timeout;
  338. ++sta->plink_retries;
  339. mod_plink_timer(sta, sta->plink_timeout);
  340. spin_unlock_bh(&sta->lock);
  341. mesh_plink_frame_tx(sdata, WLAN_SP_MESH_PEERING_OPEN,
  342. sta->sta.addr, llid, 0, 0);
  343. break;
  344. }
  345. reason = cpu_to_le16(WLAN_REASON_MESH_MAX_RETRIES);
  346. /* fall through on else */
  347. case NL80211_PLINK_CNF_RCVD:
  348. /* confirm timer */
  349. if (!reason)
  350. reason = cpu_to_le16(WLAN_REASON_MESH_CONFIRM_TIMEOUT);
  351. sta->plink_state = NL80211_PLINK_HOLDING;
  352. mod_plink_timer(sta, dot11MeshHoldingTimeout(sdata));
  353. spin_unlock_bh(&sta->lock);
  354. mesh_plink_frame_tx(sdata, WLAN_SP_MESH_PEERING_CLOSE,
  355. sta->sta.addr, llid, plid, reason);
  356. break;
  357. case NL80211_PLINK_HOLDING:
  358. /* holding timer */
  359. del_timer(&sta->plink_timer);
  360. mesh_plink_fsm_restart(sta);
  361. spin_unlock_bh(&sta->lock);
  362. break;
  363. default:
  364. spin_unlock_bh(&sta->lock);
  365. break;
  366. }
  367. }
  368. #ifdef CONFIG_PM
  369. void mesh_plink_quiesce(struct sta_info *sta)
  370. {
  371. if (del_timer_sync(&sta->plink_timer))
  372. sta->plink_timer_was_running = true;
  373. }
  374. void mesh_plink_restart(struct sta_info *sta)
  375. {
  376. if (sta->plink_timer_was_running) {
  377. add_timer(&sta->plink_timer);
  378. sta->plink_timer_was_running = false;
  379. }
  380. }
  381. #endif
  382. static inline void mesh_plink_timer_set(struct sta_info *sta, int timeout)
  383. {
  384. sta->plink_timer.expires = jiffies + (HZ * timeout / 1000);
  385. sta->plink_timer.data = (unsigned long) sta;
  386. sta->plink_timer.function = mesh_plink_timer;
  387. sta->plink_timeout = timeout;
  388. add_timer(&sta->plink_timer);
  389. }
  390. int mesh_plink_open(struct sta_info *sta)
  391. {
  392. __le16 llid;
  393. struct ieee80211_sub_if_data *sdata = sta->sdata;
  394. if (!test_sta_flag(sta, WLAN_STA_AUTH))
  395. return -EPERM;
  396. spin_lock_bh(&sta->lock);
  397. get_random_bytes(&llid, 2);
  398. sta->llid = llid;
  399. if (sta->plink_state != NL80211_PLINK_LISTEN) {
  400. spin_unlock_bh(&sta->lock);
  401. return -EBUSY;
  402. }
  403. sta->plink_state = NL80211_PLINK_OPN_SNT;
  404. mesh_plink_timer_set(sta, dot11MeshRetryTimeout(sdata));
  405. spin_unlock_bh(&sta->lock);
  406. mpl_dbg("Mesh plink: starting establishment with %pM\n",
  407. sta->sta.addr);
  408. return mesh_plink_frame_tx(sdata, WLAN_SP_MESH_PEERING_OPEN,
  409. sta->sta.addr, llid, 0, 0);
  410. }
  411. void mesh_plink_block(struct sta_info *sta)
  412. {
  413. struct ieee80211_sub_if_data *sdata = sta->sdata;
  414. bool deactivated;
  415. spin_lock_bh(&sta->lock);
  416. deactivated = __mesh_plink_deactivate(sta);
  417. sta->plink_state = NL80211_PLINK_BLOCKED;
  418. spin_unlock_bh(&sta->lock);
  419. if (deactivated)
  420. ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BEACON);
  421. }
  422. void mesh_rx_plink_frame(struct ieee80211_sub_if_data *sdata, struct ieee80211_mgmt *mgmt,
  423. size_t len, struct ieee80211_rx_status *rx_status)
  424. {
  425. struct ieee802_11_elems elems;
  426. struct sta_info *sta;
  427. enum plink_event event;
  428. enum ieee80211_self_protected_actioncode ftype;
  429. size_t baselen;
  430. bool deactivated, matches_local = true;
  431. u8 ie_len;
  432. u8 *baseaddr;
  433. __le16 plid, llid, reason;
  434. #ifdef CONFIG_MAC80211_VERBOSE_MPL_DEBUG
  435. static const char *mplstates[] = {
  436. [NL80211_PLINK_LISTEN] = "LISTEN",
  437. [NL80211_PLINK_OPN_SNT] = "OPN-SNT",
  438. [NL80211_PLINK_OPN_RCVD] = "OPN-RCVD",
  439. [NL80211_PLINK_CNF_RCVD] = "CNF_RCVD",
  440. [NL80211_PLINK_ESTAB] = "ESTAB",
  441. [NL80211_PLINK_HOLDING] = "HOLDING",
  442. [NL80211_PLINK_BLOCKED] = "BLOCKED"
  443. };
  444. #endif
  445. /* need action_code, aux */
  446. if (len < IEEE80211_MIN_ACTION_SIZE + 3)
  447. return;
  448. if (is_multicast_ether_addr(mgmt->da)) {
  449. mpl_dbg("Mesh plink: ignore frame from multicast address");
  450. return;
  451. }
  452. baseaddr = mgmt->u.action.u.self_prot.variable;
  453. baselen = (u8 *) mgmt->u.action.u.self_prot.variable - (u8 *) mgmt;
  454. if (mgmt->u.action.u.self_prot.action_code ==
  455. WLAN_SP_MESH_PEERING_CONFIRM) {
  456. baseaddr += 4;
  457. baselen += 4;
  458. }
  459. ieee802_11_parse_elems(baseaddr, len - baselen, &elems);
  460. if (!elems.peering) {
  461. mpl_dbg("Mesh plink: missing necessary peer link ie\n");
  462. return;
  463. }
  464. if (elems.rsn_len &&
  465. sdata->u.mesh.security == IEEE80211_MESH_SEC_NONE) {
  466. mpl_dbg("Mesh plink: can't establish link with secure peer\n");
  467. return;
  468. }
  469. ftype = mgmt->u.action.u.self_prot.action_code;
  470. ie_len = elems.peering_len;
  471. if ((ftype == WLAN_SP_MESH_PEERING_OPEN && ie_len != 4) ||
  472. (ftype == WLAN_SP_MESH_PEERING_CONFIRM && ie_len != 6) ||
  473. (ftype == WLAN_SP_MESH_PEERING_CLOSE && ie_len != 6
  474. && ie_len != 8)) {
  475. mpl_dbg("Mesh plink: incorrect plink ie length %d %d\n",
  476. ftype, ie_len);
  477. return;
  478. }
  479. if (ftype != WLAN_SP_MESH_PEERING_CLOSE &&
  480. (!elems.mesh_id || !elems.mesh_config)) {
  481. mpl_dbg("Mesh plink: missing necessary ie\n");
  482. return;
  483. }
  484. /* Note the lines below are correct, the llid in the frame is the plid
  485. * from the point of view of this host.
  486. */
  487. memcpy(&plid, PLINK_GET_LLID(elems.peering), 2);
  488. if (ftype == WLAN_SP_MESH_PEERING_CONFIRM ||
  489. (ftype == WLAN_SP_MESH_PEERING_CLOSE && ie_len == 8))
  490. memcpy(&llid, PLINK_GET_PLID(elems.peering), 2);
  491. rcu_read_lock();
  492. sta = sta_info_get(sdata, mgmt->sa);
  493. if (!sta && ftype != WLAN_SP_MESH_PEERING_OPEN) {
  494. mpl_dbg("Mesh plink: cls or cnf from unknown peer\n");
  495. rcu_read_unlock();
  496. return;
  497. }
  498. if (ftype == WLAN_SP_MESH_PEERING_OPEN &&
  499. !rssi_threshold_check(sta, sdata)) {
  500. mpl_dbg("Mesh plink: %pM does not meet rssi threshold\n",
  501. mgmt->sa);
  502. rcu_read_unlock();
  503. return;
  504. }
  505. if (sta && !test_sta_flag(sta, WLAN_STA_AUTH)) {
  506. mpl_dbg("Mesh plink: Action frame from non-authed peer\n");
  507. rcu_read_unlock();
  508. return;
  509. }
  510. if (sta && sta->plink_state == NL80211_PLINK_BLOCKED) {
  511. rcu_read_unlock();
  512. return;
  513. }
  514. /* Now we will figure out the appropriate event... */
  515. event = PLINK_UNDEFINED;
  516. if (ftype != WLAN_SP_MESH_PEERING_CLOSE &&
  517. !mesh_matches_local(sdata, &elems)) {
  518. matches_local = false;
  519. switch (ftype) {
  520. case WLAN_SP_MESH_PEERING_OPEN:
  521. event = OPN_RJCT;
  522. break;
  523. case WLAN_SP_MESH_PEERING_CONFIRM:
  524. event = CNF_RJCT;
  525. break;
  526. default:
  527. break;
  528. }
  529. }
  530. if (!sta && !matches_local) {
  531. rcu_read_unlock();
  532. reason = cpu_to_le16(WLAN_REASON_MESH_CONFIG);
  533. llid = 0;
  534. mesh_plink_frame_tx(sdata, WLAN_SP_MESH_PEERING_CLOSE,
  535. mgmt->sa, llid, plid, reason);
  536. return;
  537. } else if (!sta) {
  538. /* ftype == WLAN_SP_MESH_PEERING_OPEN */
  539. if (!mesh_plink_free_count(sdata)) {
  540. mpl_dbg("Mesh plink error: no more free plinks\n");
  541. rcu_read_unlock();
  542. return;
  543. }
  544. event = OPN_ACPT;
  545. } else if (matches_local) {
  546. switch (ftype) {
  547. case WLAN_SP_MESH_PEERING_OPEN:
  548. if (!mesh_plink_free_count(sdata) ||
  549. (sta->plid && sta->plid != plid))
  550. event = OPN_IGNR;
  551. else
  552. event = OPN_ACPT;
  553. break;
  554. case WLAN_SP_MESH_PEERING_CONFIRM:
  555. if (!mesh_plink_free_count(sdata) ||
  556. (sta->llid != llid || sta->plid != plid))
  557. event = CNF_IGNR;
  558. else
  559. event = CNF_ACPT;
  560. break;
  561. case WLAN_SP_MESH_PEERING_CLOSE:
  562. if (sta->plink_state == NL80211_PLINK_ESTAB)
  563. /* Do not check for llid or plid. This does not
  564. * follow the standard but since multiple plinks
  565. * per sta are not supported, it is necessary in
  566. * order to avoid a livelock when MP A sees an
  567. * establish peer link to MP B but MP B does not
  568. * see it. This can be caused by a timeout in
  569. * B's peer link establishment or B beign
  570. * restarted.
  571. */
  572. event = CLS_ACPT;
  573. else if (sta->plid != plid)
  574. event = CLS_IGNR;
  575. else if (ie_len == 7 && sta->llid != llid)
  576. event = CLS_IGNR;
  577. else
  578. event = CLS_ACPT;
  579. break;
  580. default:
  581. mpl_dbg("Mesh plink: unknown frame subtype\n");
  582. rcu_read_unlock();
  583. return;
  584. }
  585. }
  586. if (event == OPN_ACPT) {
  587. /* allocate sta entry if necessary and update info */
  588. sta = mesh_peer_init(sdata, mgmt->sa, &elems);
  589. if (!sta) {
  590. mpl_dbg("Mesh plink: failed to init peer!\n");
  591. rcu_read_unlock();
  592. return;
  593. }
  594. }
  595. mpl_dbg("Mesh plink (peer, state, llid, plid, event): %pM %s %d %d %d\n",
  596. mgmt->sa, mplstates[sta->plink_state],
  597. le16_to_cpu(sta->llid), le16_to_cpu(sta->plid),
  598. event);
  599. reason = 0;
  600. spin_lock_bh(&sta->lock);
  601. switch (sta->plink_state) {
  602. /* spin_unlock as soon as state is updated at each case */
  603. case NL80211_PLINK_LISTEN:
  604. switch (event) {
  605. case CLS_ACPT:
  606. mesh_plink_fsm_restart(sta);
  607. spin_unlock_bh(&sta->lock);
  608. break;
  609. case OPN_ACPT:
  610. sta->plink_state = NL80211_PLINK_OPN_RCVD;
  611. sta->plid = plid;
  612. get_random_bytes(&llid, 2);
  613. sta->llid = llid;
  614. mesh_plink_timer_set(sta, dot11MeshRetryTimeout(sdata));
  615. spin_unlock_bh(&sta->lock);
  616. mesh_plink_frame_tx(sdata,
  617. WLAN_SP_MESH_PEERING_OPEN,
  618. sta->sta.addr, llid, 0, 0);
  619. mesh_plink_frame_tx(sdata,
  620. WLAN_SP_MESH_PEERING_CONFIRM,
  621. sta->sta.addr, llid, plid, 0);
  622. break;
  623. default:
  624. spin_unlock_bh(&sta->lock);
  625. break;
  626. }
  627. break;
  628. case NL80211_PLINK_OPN_SNT:
  629. switch (event) {
  630. case OPN_RJCT:
  631. case CNF_RJCT:
  632. reason = cpu_to_le16(WLAN_REASON_MESH_CONFIG);
  633. case CLS_ACPT:
  634. if (!reason)
  635. reason = cpu_to_le16(WLAN_REASON_MESH_CLOSE);
  636. sta->reason = reason;
  637. sta->plink_state = NL80211_PLINK_HOLDING;
  638. if (!mod_plink_timer(sta,
  639. dot11MeshHoldingTimeout(sdata)))
  640. sta->ignore_plink_timer = true;
  641. llid = sta->llid;
  642. spin_unlock_bh(&sta->lock);
  643. mesh_plink_frame_tx(sdata,
  644. WLAN_SP_MESH_PEERING_CLOSE,
  645. sta->sta.addr, llid, plid, reason);
  646. break;
  647. case OPN_ACPT:
  648. /* retry timer is left untouched */
  649. sta->plink_state = NL80211_PLINK_OPN_RCVD;
  650. sta->plid = plid;
  651. llid = sta->llid;
  652. spin_unlock_bh(&sta->lock);
  653. mesh_plink_frame_tx(sdata,
  654. WLAN_SP_MESH_PEERING_CONFIRM,
  655. sta->sta.addr, llid, plid, 0);
  656. break;
  657. case CNF_ACPT:
  658. sta->plink_state = NL80211_PLINK_CNF_RCVD;
  659. if (!mod_plink_timer(sta,
  660. dot11MeshConfirmTimeout(sdata)))
  661. sta->ignore_plink_timer = true;
  662. spin_unlock_bh(&sta->lock);
  663. break;
  664. default:
  665. spin_unlock_bh(&sta->lock);
  666. break;
  667. }
  668. break;
  669. case NL80211_PLINK_OPN_RCVD:
  670. switch (event) {
  671. case OPN_RJCT:
  672. case CNF_RJCT:
  673. reason = cpu_to_le16(WLAN_REASON_MESH_CONFIG);
  674. case CLS_ACPT:
  675. if (!reason)
  676. reason = cpu_to_le16(WLAN_REASON_MESH_CLOSE);
  677. sta->reason = reason;
  678. sta->plink_state = NL80211_PLINK_HOLDING;
  679. if (!mod_plink_timer(sta,
  680. dot11MeshHoldingTimeout(sdata)))
  681. sta->ignore_plink_timer = true;
  682. llid = sta->llid;
  683. spin_unlock_bh(&sta->lock);
  684. mesh_plink_frame_tx(sdata, WLAN_SP_MESH_PEERING_CLOSE,
  685. sta->sta.addr, llid, plid, reason);
  686. break;
  687. case OPN_ACPT:
  688. llid = sta->llid;
  689. spin_unlock_bh(&sta->lock);
  690. mesh_plink_frame_tx(sdata,
  691. WLAN_SP_MESH_PEERING_CONFIRM,
  692. sta->sta.addr, llid, plid, 0);
  693. break;
  694. case CNF_ACPT:
  695. del_timer(&sta->plink_timer);
  696. sta->plink_state = NL80211_PLINK_ESTAB;
  697. spin_unlock_bh(&sta->lock);
  698. mesh_plink_inc_estab_count(sdata);
  699. ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BEACON);
  700. mpl_dbg("Mesh plink with %pM ESTABLISHED\n",
  701. sta->sta.addr);
  702. break;
  703. default:
  704. spin_unlock_bh(&sta->lock);
  705. break;
  706. }
  707. break;
  708. case NL80211_PLINK_CNF_RCVD:
  709. switch (event) {
  710. case OPN_RJCT:
  711. case CNF_RJCT:
  712. reason = cpu_to_le16(WLAN_REASON_MESH_CONFIG);
  713. case CLS_ACPT:
  714. if (!reason)
  715. reason = cpu_to_le16(WLAN_REASON_MESH_CLOSE);
  716. sta->reason = reason;
  717. sta->plink_state = NL80211_PLINK_HOLDING;
  718. if (!mod_plink_timer(sta,
  719. dot11MeshHoldingTimeout(sdata)))
  720. sta->ignore_plink_timer = true;
  721. llid = sta->llid;
  722. spin_unlock_bh(&sta->lock);
  723. mesh_plink_frame_tx(sdata,
  724. WLAN_SP_MESH_PEERING_CLOSE,
  725. sta->sta.addr, llid, plid, reason);
  726. break;
  727. case OPN_ACPT:
  728. del_timer(&sta->plink_timer);
  729. sta->plink_state = NL80211_PLINK_ESTAB;
  730. spin_unlock_bh(&sta->lock);
  731. mesh_plink_inc_estab_count(sdata);
  732. ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BEACON);
  733. mpl_dbg("Mesh plink with %pM ESTABLISHED\n",
  734. sta->sta.addr);
  735. mesh_plink_frame_tx(sdata,
  736. WLAN_SP_MESH_PEERING_CONFIRM,
  737. sta->sta.addr, llid, plid, 0);
  738. break;
  739. default:
  740. spin_unlock_bh(&sta->lock);
  741. break;
  742. }
  743. break;
  744. case NL80211_PLINK_ESTAB:
  745. switch (event) {
  746. case CLS_ACPT:
  747. reason = cpu_to_le16(WLAN_REASON_MESH_CLOSE);
  748. sta->reason = reason;
  749. deactivated = __mesh_plink_deactivate(sta);
  750. sta->plink_state = NL80211_PLINK_HOLDING;
  751. llid = sta->llid;
  752. mod_plink_timer(sta, dot11MeshHoldingTimeout(sdata));
  753. spin_unlock_bh(&sta->lock);
  754. if (deactivated)
  755. ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BEACON);
  756. mesh_plink_frame_tx(sdata, WLAN_SP_MESH_PEERING_CLOSE,
  757. sta->sta.addr, llid, plid, reason);
  758. break;
  759. case OPN_ACPT:
  760. llid = sta->llid;
  761. spin_unlock_bh(&sta->lock);
  762. mesh_plink_frame_tx(sdata,
  763. WLAN_SP_MESH_PEERING_CONFIRM,
  764. sta->sta.addr, llid, plid, 0);
  765. break;
  766. default:
  767. spin_unlock_bh(&sta->lock);
  768. break;
  769. }
  770. break;
  771. case NL80211_PLINK_HOLDING:
  772. switch (event) {
  773. case CLS_ACPT:
  774. if (del_timer(&sta->plink_timer))
  775. sta->ignore_plink_timer = 1;
  776. mesh_plink_fsm_restart(sta);
  777. spin_unlock_bh(&sta->lock);
  778. break;
  779. case OPN_ACPT:
  780. case CNF_ACPT:
  781. case OPN_RJCT:
  782. case CNF_RJCT:
  783. llid = sta->llid;
  784. reason = sta->reason;
  785. spin_unlock_bh(&sta->lock);
  786. mesh_plink_frame_tx(sdata, WLAN_SP_MESH_PEERING_CLOSE,
  787. sta->sta.addr, llid, plid, reason);
  788. break;
  789. default:
  790. spin_unlock_bh(&sta->lock);
  791. }
  792. break;
  793. default:
  794. /* should not get here, PLINK_BLOCKED is dealt with at the
  795. * beginning of the function
  796. */
  797. spin_unlock_bh(&sta->lock);
  798. break;
  799. }
  800. rcu_read_unlock();
  801. }