mesh_plink.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980
  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. if (insert)
  349. rate_control_rate_init(sta);
  350. spin_unlock_bh(&sta->lock);
  351. if (insert && sta_info_insert(sta))
  352. return NULL;
  353. return sta;
  354. }
  355. void mesh_neighbour_update(struct ieee80211_sub_if_data *sdata,
  356. u8 *hw_addr,
  357. struct ieee802_11_elems *elems)
  358. {
  359. struct sta_info *sta;
  360. rcu_read_lock();
  361. sta = mesh_peer_init(sdata, hw_addr, elems);
  362. if (!sta)
  363. goto out;
  364. if (mesh_peer_accepts_plinks(elems) &&
  365. sta->plink_state == NL80211_PLINK_LISTEN &&
  366. sdata->u.mesh.accepting_plinks &&
  367. sdata->u.mesh.mshcfg.auto_open_plinks &&
  368. rssi_threshold_check(sta, sdata))
  369. mesh_plink_open(sta);
  370. out:
  371. rcu_read_unlock();
  372. }
  373. static void mesh_plink_timer(unsigned long data)
  374. {
  375. struct sta_info *sta;
  376. __le16 llid, plid, reason;
  377. struct ieee80211_sub_if_data *sdata;
  378. struct mesh_config *mshcfg;
  379. /*
  380. * This STA is valid because sta_info_destroy() will
  381. * del_timer_sync() this timer after having made sure
  382. * it cannot be readded (by deleting the plink.)
  383. */
  384. sta = (struct sta_info *) data;
  385. if (sta->sdata->local->quiescing) {
  386. sta->plink_timer_was_running = true;
  387. return;
  388. }
  389. spin_lock_bh(&sta->lock);
  390. if (sta->ignore_plink_timer) {
  391. sta->ignore_plink_timer = false;
  392. spin_unlock_bh(&sta->lock);
  393. return;
  394. }
  395. mpl_dbg(sta->sdata,
  396. "Mesh plink timer for %pM fired on state %d\n",
  397. sta->sta.addr, sta->plink_state);
  398. reason = 0;
  399. llid = sta->llid;
  400. plid = sta->plid;
  401. sdata = sta->sdata;
  402. mshcfg = &sdata->u.mesh.mshcfg;
  403. switch (sta->plink_state) {
  404. case NL80211_PLINK_OPN_RCVD:
  405. case NL80211_PLINK_OPN_SNT:
  406. /* retry timer */
  407. if (sta->plink_retries < mshcfg->dot11MeshMaxRetries) {
  408. u32 rand;
  409. mpl_dbg(sta->sdata,
  410. "Mesh plink for %pM (retry, timeout): %d %d\n",
  411. sta->sta.addr, sta->plink_retries,
  412. sta->plink_timeout);
  413. get_random_bytes(&rand, sizeof(u32));
  414. sta->plink_timeout = sta->plink_timeout +
  415. rand % sta->plink_timeout;
  416. ++sta->plink_retries;
  417. mod_plink_timer(sta, sta->plink_timeout);
  418. spin_unlock_bh(&sta->lock);
  419. mesh_plink_frame_tx(sdata, WLAN_SP_MESH_PEERING_OPEN,
  420. sta->sta.addr, llid, 0, 0);
  421. break;
  422. }
  423. reason = cpu_to_le16(WLAN_REASON_MESH_MAX_RETRIES);
  424. /* fall through on else */
  425. case NL80211_PLINK_CNF_RCVD:
  426. /* confirm timer */
  427. if (!reason)
  428. reason = cpu_to_le16(WLAN_REASON_MESH_CONFIRM_TIMEOUT);
  429. sta->plink_state = NL80211_PLINK_HOLDING;
  430. mod_plink_timer(sta, mshcfg->dot11MeshHoldingTimeout);
  431. spin_unlock_bh(&sta->lock);
  432. mesh_plink_frame_tx(sdata, WLAN_SP_MESH_PEERING_CLOSE,
  433. sta->sta.addr, llid, plid, reason);
  434. break;
  435. case NL80211_PLINK_HOLDING:
  436. /* holding timer */
  437. del_timer(&sta->plink_timer);
  438. mesh_plink_fsm_restart(sta);
  439. spin_unlock_bh(&sta->lock);
  440. break;
  441. default:
  442. spin_unlock_bh(&sta->lock);
  443. break;
  444. }
  445. }
  446. #ifdef CONFIG_PM
  447. void mesh_plink_quiesce(struct sta_info *sta)
  448. {
  449. if (del_timer_sync(&sta->plink_timer))
  450. sta->plink_timer_was_running = true;
  451. }
  452. void mesh_plink_restart(struct sta_info *sta)
  453. {
  454. if (sta->plink_timer_was_running) {
  455. add_timer(&sta->plink_timer);
  456. sta->plink_timer_was_running = false;
  457. }
  458. }
  459. #endif
  460. static inline void mesh_plink_timer_set(struct sta_info *sta, int timeout)
  461. {
  462. sta->plink_timer.expires = jiffies + (HZ * timeout / 1000);
  463. sta->plink_timer.data = (unsigned long) sta;
  464. sta->plink_timer.function = mesh_plink_timer;
  465. sta->plink_timeout = timeout;
  466. add_timer(&sta->plink_timer);
  467. }
  468. int mesh_plink_open(struct sta_info *sta)
  469. {
  470. __le16 llid;
  471. struct ieee80211_sub_if_data *sdata = sta->sdata;
  472. if (!test_sta_flag(sta, WLAN_STA_AUTH))
  473. return -EPERM;
  474. spin_lock_bh(&sta->lock);
  475. get_random_bytes(&llid, 2);
  476. sta->llid = llid;
  477. if (sta->plink_state != NL80211_PLINK_LISTEN &&
  478. sta->plink_state != NL80211_PLINK_BLOCKED) {
  479. spin_unlock_bh(&sta->lock);
  480. return -EBUSY;
  481. }
  482. sta->plink_state = NL80211_PLINK_OPN_SNT;
  483. mesh_plink_timer_set(sta, sdata->u.mesh.mshcfg.dot11MeshRetryTimeout);
  484. spin_unlock_bh(&sta->lock);
  485. mpl_dbg(sdata,
  486. "Mesh plink: starting establishment with %pM\n",
  487. sta->sta.addr);
  488. return mesh_plink_frame_tx(sdata, WLAN_SP_MESH_PEERING_OPEN,
  489. sta->sta.addr, llid, 0, 0);
  490. }
  491. void mesh_plink_block(struct sta_info *sta)
  492. {
  493. struct ieee80211_sub_if_data *sdata = sta->sdata;
  494. u32 changed;
  495. spin_lock_bh(&sta->lock);
  496. changed = __mesh_plink_deactivate(sta);
  497. sta->plink_state = NL80211_PLINK_BLOCKED;
  498. spin_unlock_bh(&sta->lock);
  499. ieee80211_bss_info_change_notify(sdata, changed);
  500. }
  501. void mesh_rx_plink_frame(struct ieee80211_sub_if_data *sdata, struct ieee80211_mgmt *mgmt,
  502. size_t len, struct ieee80211_rx_status *rx_status)
  503. {
  504. struct mesh_config *mshcfg = &sdata->u.mesh.mshcfg;
  505. struct ieee802_11_elems elems;
  506. struct sta_info *sta;
  507. enum plink_event event;
  508. enum ieee80211_self_protected_actioncode ftype;
  509. size_t baselen;
  510. bool matches_local = true;
  511. u8 ie_len;
  512. u8 *baseaddr;
  513. u32 changed = 0;
  514. __le16 plid, llid, reason;
  515. static const char *mplstates[] = {
  516. [NL80211_PLINK_LISTEN] = "LISTEN",
  517. [NL80211_PLINK_OPN_SNT] = "OPN-SNT",
  518. [NL80211_PLINK_OPN_RCVD] = "OPN-RCVD",
  519. [NL80211_PLINK_CNF_RCVD] = "CNF_RCVD",
  520. [NL80211_PLINK_ESTAB] = "ESTAB",
  521. [NL80211_PLINK_HOLDING] = "HOLDING",
  522. [NL80211_PLINK_BLOCKED] = "BLOCKED"
  523. };
  524. /* need action_code, aux */
  525. if (len < IEEE80211_MIN_ACTION_SIZE + 3)
  526. return;
  527. if (is_multicast_ether_addr(mgmt->da)) {
  528. mpl_dbg(sdata,
  529. "Mesh plink: ignore frame from multicast address\n");
  530. return;
  531. }
  532. baseaddr = mgmt->u.action.u.self_prot.variable;
  533. baselen = (u8 *) mgmt->u.action.u.self_prot.variable - (u8 *) mgmt;
  534. if (mgmt->u.action.u.self_prot.action_code ==
  535. WLAN_SP_MESH_PEERING_CONFIRM) {
  536. baseaddr += 4;
  537. baselen += 4;
  538. }
  539. ieee802_11_parse_elems(baseaddr, len - baselen, &elems);
  540. if (!elems.peering) {
  541. mpl_dbg(sdata,
  542. "Mesh plink: missing necessary peer link ie\n");
  543. return;
  544. }
  545. if (elems.rsn_len &&
  546. sdata->u.mesh.security == IEEE80211_MESH_SEC_NONE) {
  547. mpl_dbg(sdata,
  548. "Mesh plink: can't establish link with secure peer\n");
  549. return;
  550. }
  551. ftype = mgmt->u.action.u.self_prot.action_code;
  552. ie_len = elems.peering_len;
  553. if ((ftype == WLAN_SP_MESH_PEERING_OPEN && ie_len != 4) ||
  554. (ftype == WLAN_SP_MESH_PEERING_CONFIRM && ie_len != 6) ||
  555. (ftype == WLAN_SP_MESH_PEERING_CLOSE && ie_len != 6
  556. && ie_len != 8)) {
  557. mpl_dbg(sdata,
  558. "Mesh plink: incorrect plink ie length %d %d\n",
  559. ftype, ie_len);
  560. return;
  561. }
  562. if (ftype != WLAN_SP_MESH_PEERING_CLOSE &&
  563. (!elems.mesh_id || !elems.mesh_config)) {
  564. mpl_dbg(sdata, "Mesh plink: missing necessary ie\n");
  565. return;
  566. }
  567. /* Note the lines below are correct, the llid in the frame is the plid
  568. * from the point of view of this host.
  569. */
  570. memcpy(&plid, PLINK_GET_LLID(elems.peering), 2);
  571. if (ftype == WLAN_SP_MESH_PEERING_CONFIRM ||
  572. (ftype == WLAN_SP_MESH_PEERING_CLOSE && ie_len == 8))
  573. memcpy(&llid, PLINK_GET_PLID(elems.peering), 2);
  574. rcu_read_lock();
  575. sta = sta_info_get(sdata, mgmt->sa);
  576. if (!sta && ftype != WLAN_SP_MESH_PEERING_OPEN) {
  577. mpl_dbg(sdata, "Mesh plink: cls or cnf from unknown peer\n");
  578. rcu_read_unlock();
  579. return;
  580. }
  581. if (ftype == WLAN_SP_MESH_PEERING_OPEN &&
  582. !rssi_threshold_check(sta, sdata)) {
  583. mpl_dbg(sdata, "Mesh plink: %pM does not meet rssi threshold\n",
  584. mgmt->sa);
  585. rcu_read_unlock();
  586. return;
  587. }
  588. if (sta && !test_sta_flag(sta, WLAN_STA_AUTH)) {
  589. mpl_dbg(sdata, "Mesh plink: Action frame from non-authed peer\n");
  590. rcu_read_unlock();
  591. return;
  592. }
  593. if (sta && sta->plink_state == NL80211_PLINK_BLOCKED) {
  594. rcu_read_unlock();
  595. return;
  596. }
  597. /* Now we will figure out the appropriate event... */
  598. event = PLINK_UNDEFINED;
  599. if (ftype != WLAN_SP_MESH_PEERING_CLOSE &&
  600. !mesh_matches_local(sdata, &elems)) {
  601. matches_local = false;
  602. switch (ftype) {
  603. case WLAN_SP_MESH_PEERING_OPEN:
  604. event = OPN_RJCT;
  605. break;
  606. case WLAN_SP_MESH_PEERING_CONFIRM:
  607. event = CNF_RJCT;
  608. break;
  609. default:
  610. break;
  611. }
  612. }
  613. if (!sta && !matches_local) {
  614. rcu_read_unlock();
  615. reason = cpu_to_le16(WLAN_REASON_MESH_CONFIG);
  616. llid = 0;
  617. mesh_plink_frame_tx(sdata, WLAN_SP_MESH_PEERING_CLOSE,
  618. mgmt->sa, llid, plid, reason);
  619. return;
  620. } else if (!sta) {
  621. /* ftype == WLAN_SP_MESH_PEERING_OPEN */
  622. if (!mesh_plink_free_count(sdata)) {
  623. mpl_dbg(sdata, "Mesh plink error: no more free plinks\n");
  624. rcu_read_unlock();
  625. return;
  626. }
  627. event = OPN_ACPT;
  628. } else if (matches_local) {
  629. switch (ftype) {
  630. case WLAN_SP_MESH_PEERING_OPEN:
  631. if (!mesh_plink_free_count(sdata) ||
  632. (sta->plid && sta->plid != plid))
  633. event = OPN_IGNR;
  634. else
  635. event = OPN_ACPT;
  636. break;
  637. case WLAN_SP_MESH_PEERING_CONFIRM:
  638. if (!mesh_plink_free_count(sdata) ||
  639. (sta->llid != llid || sta->plid != plid))
  640. event = CNF_IGNR;
  641. else
  642. event = CNF_ACPT;
  643. break;
  644. case WLAN_SP_MESH_PEERING_CLOSE:
  645. if (sta->plink_state == NL80211_PLINK_ESTAB)
  646. /* Do not check for llid or plid. This does not
  647. * follow the standard but since multiple plinks
  648. * per sta are not supported, it is necessary in
  649. * order to avoid a livelock when MP A sees an
  650. * establish peer link to MP B but MP B does not
  651. * see it. This can be caused by a timeout in
  652. * B's peer link establishment or B beign
  653. * restarted.
  654. */
  655. event = CLS_ACPT;
  656. else if (sta->plid != plid)
  657. event = CLS_IGNR;
  658. else if (ie_len == 7 && sta->llid != llid)
  659. event = CLS_IGNR;
  660. else
  661. event = CLS_ACPT;
  662. break;
  663. default:
  664. mpl_dbg(sdata, "Mesh plink: unknown frame subtype\n");
  665. rcu_read_unlock();
  666. return;
  667. }
  668. }
  669. if (event == OPN_ACPT) {
  670. /* allocate sta entry if necessary and update info */
  671. sta = mesh_peer_init(sdata, mgmt->sa, &elems);
  672. if (!sta) {
  673. mpl_dbg(sdata, "Mesh plink: failed to init peer!\n");
  674. rcu_read_unlock();
  675. return;
  676. }
  677. }
  678. mpl_dbg(sdata,
  679. "Mesh plink (peer, state, llid, plid, event): %pM %s %d %d %d\n",
  680. mgmt->sa, mplstates[sta->plink_state],
  681. le16_to_cpu(sta->llid), le16_to_cpu(sta->plid),
  682. event);
  683. reason = 0;
  684. spin_lock_bh(&sta->lock);
  685. switch (sta->plink_state) {
  686. /* spin_unlock as soon as state is updated at each case */
  687. case NL80211_PLINK_LISTEN:
  688. switch (event) {
  689. case CLS_ACPT:
  690. mesh_plink_fsm_restart(sta);
  691. spin_unlock_bh(&sta->lock);
  692. break;
  693. case OPN_ACPT:
  694. sta->plink_state = NL80211_PLINK_OPN_RCVD;
  695. sta->plid = plid;
  696. get_random_bytes(&llid, 2);
  697. sta->llid = llid;
  698. mesh_plink_timer_set(sta,
  699. mshcfg->dot11MeshRetryTimeout);
  700. spin_unlock_bh(&sta->lock);
  701. mesh_plink_frame_tx(sdata,
  702. WLAN_SP_MESH_PEERING_OPEN,
  703. sta->sta.addr, llid, 0, 0);
  704. mesh_plink_frame_tx(sdata,
  705. WLAN_SP_MESH_PEERING_CONFIRM,
  706. sta->sta.addr, llid, plid, 0);
  707. break;
  708. default:
  709. spin_unlock_bh(&sta->lock);
  710. break;
  711. }
  712. break;
  713. case NL80211_PLINK_OPN_SNT:
  714. switch (event) {
  715. case OPN_RJCT:
  716. case CNF_RJCT:
  717. reason = cpu_to_le16(WLAN_REASON_MESH_CONFIG);
  718. case CLS_ACPT:
  719. if (!reason)
  720. reason = cpu_to_le16(WLAN_REASON_MESH_CLOSE);
  721. sta->reason = reason;
  722. sta->plink_state = NL80211_PLINK_HOLDING;
  723. if (!mod_plink_timer(sta,
  724. mshcfg->dot11MeshHoldingTimeout))
  725. sta->ignore_plink_timer = true;
  726. llid = sta->llid;
  727. spin_unlock_bh(&sta->lock);
  728. mesh_plink_frame_tx(sdata,
  729. WLAN_SP_MESH_PEERING_CLOSE,
  730. sta->sta.addr, llid, plid, reason);
  731. break;
  732. case OPN_ACPT:
  733. /* retry timer is left untouched */
  734. sta->plink_state = NL80211_PLINK_OPN_RCVD;
  735. sta->plid = plid;
  736. llid = sta->llid;
  737. spin_unlock_bh(&sta->lock);
  738. mesh_plink_frame_tx(sdata,
  739. WLAN_SP_MESH_PEERING_CONFIRM,
  740. sta->sta.addr, llid, plid, 0);
  741. break;
  742. case CNF_ACPT:
  743. sta->plink_state = NL80211_PLINK_CNF_RCVD;
  744. if (!mod_plink_timer(sta,
  745. mshcfg->dot11MeshConfirmTimeout))
  746. sta->ignore_plink_timer = true;
  747. spin_unlock_bh(&sta->lock);
  748. break;
  749. default:
  750. spin_unlock_bh(&sta->lock);
  751. break;
  752. }
  753. break;
  754. case NL80211_PLINK_OPN_RCVD:
  755. switch (event) {
  756. case OPN_RJCT:
  757. case CNF_RJCT:
  758. reason = cpu_to_le16(WLAN_REASON_MESH_CONFIG);
  759. case CLS_ACPT:
  760. if (!reason)
  761. reason = cpu_to_le16(WLAN_REASON_MESH_CLOSE);
  762. sta->reason = reason;
  763. sta->plink_state = NL80211_PLINK_HOLDING;
  764. if (!mod_plink_timer(sta,
  765. mshcfg->dot11MeshHoldingTimeout))
  766. sta->ignore_plink_timer = true;
  767. llid = sta->llid;
  768. spin_unlock_bh(&sta->lock);
  769. mesh_plink_frame_tx(sdata, WLAN_SP_MESH_PEERING_CLOSE,
  770. sta->sta.addr, llid, plid, reason);
  771. break;
  772. case OPN_ACPT:
  773. llid = sta->llid;
  774. spin_unlock_bh(&sta->lock);
  775. mesh_plink_frame_tx(sdata,
  776. WLAN_SP_MESH_PEERING_CONFIRM,
  777. sta->sta.addr, llid, plid, 0);
  778. break;
  779. case CNF_ACPT:
  780. del_timer(&sta->plink_timer);
  781. sta->plink_state = NL80211_PLINK_ESTAB;
  782. spin_unlock_bh(&sta->lock);
  783. changed |= mesh_plink_inc_estab_count(sdata);
  784. changed |= mesh_set_ht_prot_mode(sdata);
  785. mpl_dbg(sdata, "Mesh plink with %pM ESTABLISHED\n",
  786. sta->sta.addr);
  787. break;
  788. default:
  789. spin_unlock_bh(&sta->lock);
  790. break;
  791. }
  792. break;
  793. case NL80211_PLINK_CNF_RCVD:
  794. switch (event) {
  795. case OPN_RJCT:
  796. case CNF_RJCT:
  797. reason = cpu_to_le16(WLAN_REASON_MESH_CONFIG);
  798. case CLS_ACPT:
  799. if (!reason)
  800. reason = cpu_to_le16(WLAN_REASON_MESH_CLOSE);
  801. sta->reason = reason;
  802. sta->plink_state = NL80211_PLINK_HOLDING;
  803. if (!mod_plink_timer(sta,
  804. mshcfg->dot11MeshHoldingTimeout))
  805. sta->ignore_plink_timer = true;
  806. llid = sta->llid;
  807. spin_unlock_bh(&sta->lock);
  808. mesh_plink_frame_tx(sdata,
  809. WLAN_SP_MESH_PEERING_CLOSE,
  810. sta->sta.addr, llid, plid, reason);
  811. break;
  812. case OPN_ACPT:
  813. del_timer(&sta->plink_timer);
  814. sta->plink_state = NL80211_PLINK_ESTAB;
  815. spin_unlock_bh(&sta->lock);
  816. changed |= mesh_plink_inc_estab_count(sdata);
  817. changed |= mesh_set_ht_prot_mode(sdata);
  818. mpl_dbg(sdata, "Mesh plink with %pM ESTABLISHED\n",
  819. sta->sta.addr);
  820. mesh_plink_frame_tx(sdata,
  821. WLAN_SP_MESH_PEERING_CONFIRM,
  822. sta->sta.addr, llid, plid, 0);
  823. break;
  824. default:
  825. spin_unlock_bh(&sta->lock);
  826. break;
  827. }
  828. break;
  829. case NL80211_PLINK_ESTAB:
  830. switch (event) {
  831. case CLS_ACPT:
  832. reason = cpu_to_le16(WLAN_REASON_MESH_CLOSE);
  833. sta->reason = reason;
  834. changed |= __mesh_plink_deactivate(sta);
  835. sta->plink_state = NL80211_PLINK_HOLDING;
  836. llid = sta->llid;
  837. mod_plink_timer(sta, mshcfg->dot11MeshHoldingTimeout);
  838. spin_unlock_bh(&sta->lock);
  839. changed |= mesh_set_ht_prot_mode(sdata);
  840. mesh_plink_frame_tx(sdata, WLAN_SP_MESH_PEERING_CLOSE,
  841. sta->sta.addr, llid, plid, reason);
  842. break;
  843. case OPN_ACPT:
  844. llid = sta->llid;
  845. spin_unlock_bh(&sta->lock);
  846. mesh_plink_frame_tx(sdata,
  847. WLAN_SP_MESH_PEERING_CONFIRM,
  848. sta->sta.addr, llid, plid, 0);
  849. break;
  850. default:
  851. spin_unlock_bh(&sta->lock);
  852. break;
  853. }
  854. break;
  855. case NL80211_PLINK_HOLDING:
  856. switch (event) {
  857. case CLS_ACPT:
  858. if (del_timer(&sta->plink_timer))
  859. sta->ignore_plink_timer = 1;
  860. mesh_plink_fsm_restart(sta);
  861. spin_unlock_bh(&sta->lock);
  862. break;
  863. case OPN_ACPT:
  864. case CNF_ACPT:
  865. case OPN_RJCT:
  866. case CNF_RJCT:
  867. llid = sta->llid;
  868. reason = sta->reason;
  869. spin_unlock_bh(&sta->lock);
  870. mesh_plink_frame_tx(sdata, WLAN_SP_MESH_PEERING_CLOSE,
  871. sta->sta.addr, llid, plid, reason);
  872. break;
  873. default:
  874. spin_unlock_bh(&sta->lock);
  875. }
  876. break;
  877. default:
  878. /* should not get here, PLINK_BLOCKED is dealt with at the
  879. * beginning of the function
  880. */
  881. spin_unlock_bh(&sta->lock);
  882. break;
  883. }
  884. rcu_read_unlock();
  885. if (changed)
  886. ieee80211_bss_info_change_notify(sdata, changed);
  887. }