mesh_plink.c 25 KB

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