mesh_plink.c 26 KB

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