mesh_plink.c 26 KB

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