mesh_plink.c 26 KB

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