mesh_plink.c 25 KB

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