mesh_plink.c 25 KB

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