mesh_plink.c 25 KB

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