mesh_plink.c 26 KB

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