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. #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. /** mesh_set_ht_prot_mode - set correct HT protection mode
  92. *
  93. * Section 9.23.3.5 of IEEE 80211s standard describes the protection rules for
  94. * HT mesh STA in a MBSS. Three HT protection modes are supported for now,
  95. * non-HT mixed mode, 20MHz-protection and no-protection mode. non-HT mixed
  96. * mode is selected if any non-HT peers are present in our MBSS.
  97. * 20MHz-protection mode is selected if all peers in our 20/40MHz MBSS support
  98. * HT and atleast one HT20 peer is present. Otherwise no-protection mode is
  99. * 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. switch (sta->ch_type) {
  115. case NL80211_CHAN_NO_HT:
  116. mpl_dbg("mesh_plink %pM: nonHT sta (%pM) is present",
  117. sdata->vif.addr, sta->sta.addr);
  118. non_ht_sta = true;
  119. goto out;
  120. case NL80211_CHAN_HT20:
  121. mpl_dbg("mesh_plink %pM: HT20 sta (%pM) is present",
  122. sdata->vif.addr, sta->sta.addr);
  123. ht20_sta = true;
  124. default:
  125. break;
  126. }
  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. changed = BSS_CHANGED_HT;
  140. mpl_dbg("mesh_plink %pM: protection mode changed to %d",
  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. *
  152. * Locking: the caller must hold sta->lock
  153. */
  154. static bool __mesh_plink_deactivate(struct sta_info *sta)
  155. {
  156. struct ieee80211_sub_if_data *sdata = sta->sdata;
  157. bool deactivated = false;
  158. if (sta->plink_state == NL80211_PLINK_ESTAB) {
  159. mesh_plink_dec_estab_count(sdata);
  160. deactivated = true;
  161. }
  162. sta->plink_state = NL80211_PLINK_BLOCKED;
  163. mesh_path_flush_by_nexthop(sta);
  164. return deactivated;
  165. }
  166. /**
  167. * mesh_plink_deactivate - deactivate mesh peer link
  168. *
  169. * @sta: mesh peer link to deactivate
  170. *
  171. * All mesh paths with this peer as next hop will be flushed
  172. */
  173. void mesh_plink_deactivate(struct sta_info *sta)
  174. {
  175. struct ieee80211_sub_if_data *sdata = sta->sdata;
  176. bool deactivated;
  177. spin_lock_bh(&sta->lock);
  178. deactivated = __mesh_plink_deactivate(sta);
  179. sta->reason = cpu_to_le16(WLAN_REASON_MESH_PEER_CANCELED);
  180. mesh_plink_frame_tx(sdata, WLAN_SP_MESH_PEERING_CLOSE,
  181. sta->sta.addr, sta->llid, sta->plid,
  182. sta->reason);
  183. spin_unlock_bh(&sta->lock);
  184. if (deactivated)
  185. ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BEACON);
  186. }
  187. static int mesh_plink_frame_tx(struct ieee80211_sub_if_data *sdata,
  188. enum ieee80211_self_protected_actioncode action,
  189. u8 *da, __le16 llid, __le16 plid, __le16 reason) {
  190. struct ieee80211_local *local = sdata->local;
  191. struct sk_buff *skb;
  192. struct ieee80211_mgmt *mgmt;
  193. bool include_plid = false;
  194. u16 peering_proto = 0;
  195. u8 *pos, ie_len = 4;
  196. int hdr_len = offsetof(struct ieee80211_mgmt, u.action.u.self_prot) +
  197. sizeof(mgmt->u.action.u.self_prot);
  198. 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. skb_reserve(skb, local->tx_headroom);
  213. mgmt = (struct ieee80211_mgmt *) skb_put(skb, hdr_len);
  214. memset(mgmt, 0, hdr_len);
  215. mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
  216. IEEE80211_STYPE_ACTION);
  217. memcpy(mgmt->da, da, ETH_ALEN);
  218. memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
  219. memcpy(mgmt->bssid, sdata->vif.addr, ETH_ALEN);
  220. mgmt->u.action.category = WLAN_CATEGORY_SELF_PROTECTED;
  221. mgmt->u.action.u.self_prot.action_code = action;
  222. if (action != WLAN_SP_MESH_PEERING_CLOSE) {
  223. /* capability info */
  224. pos = skb_put(skb, 2);
  225. memset(pos, 0, 2);
  226. if (action == WLAN_SP_MESH_PEERING_CONFIRM) {
  227. /* AID */
  228. pos = skb_put(skb, 2);
  229. memcpy(pos + 2, &plid, 2);
  230. }
  231. if (ieee80211_add_srates_ie(&sdata->vif, skb, true) ||
  232. ieee80211_add_ext_srates_ie(&sdata->vif, skb, true) ||
  233. mesh_add_rsn_ie(skb, sdata) ||
  234. mesh_add_meshid_ie(skb, sdata) ||
  235. mesh_add_meshconf_ie(skb, sdata))
  236. return -1;
  237. } else { /* WLAN_SP_MESH_PEERING_CLOSE */
  238. if (mesh_add_meshid_ie(skb, sdata))
  239. return -1;
  240. }
  241. /* Add Mesh Peering Management element */
  242. switch (action) {
  243. case WLAN_SP_MESH_PEERING_OPEN:
  244. break;
  245. case WLAN_SP_MESH_PEERING_CONFIRM:
  246. ie_len += 2;
  247. include_plid = true;
  248. break;
  249. case WLAN_SP_MESH_PEERING_CLOSE:
  250. if (plid) {
  251. ie_len += 2;
  252. include_plid = true;
  253. }
  254. ie_len += 2; /* reason code */
  255. break;
  256. default:
  257. return -EINVAL;
  258. }
  259. if (WARN_ON(skb_tailroom(skb) < 2 + ie_len))
  260. return -ENOMEM;
  261. pos = skb_put(skb, 2 + ie_len);
  262. *pos++ = WLAN_EID_PEER_MGMT;
  263. *pos++ = ie_len;
  264. memcpy(pos, &peering_proto, 2);
  265. pos += 2;
  266. memcpy(pos, &llid, 2);
  267. pos += 2;
  268. if (include_plid) {
  269. memcpy(pos, &plid, 2);
  270. pos += 2;
  271. }
  272. if (action == WLAN_SP_MESH_PEERING_CLOSE) {
  273. memcpy(pos, &reason, 2);
  274. pos += 2;
  275. }
  276. if (action != WLAN_SP_MESH_PEERING_CLOSE) {
  277. if (mesh_add_ht_cap_ie(skb, sdata) ||
  278. mesh_add_ht_oper_ie(skb, sdata))
  279. return -1;
  280. }
  281. if (mesh_add_vendor_ies(skb, sdata))
  282. return -1;
  283. ieee80211_tx_skb(sdata, skb);
  284. return 0;
  285. }
  286. /* mesh_peer_init - initialize new mesh peer and return resulting sta_info
  287. *
  288. * @sdata: local meshif
  289. * @addr: peer's address
  290. * @elems: IEs from beacon or mesh peering frame
  291. *
  292. * call under RCU
  293. */
  294. static struct sta_info *mesh_peer_init(struct ieee80211_sub_if_data *sdata,
  295. u8 *addr,
  296. struct ieee802_11_elems *elems)
  297. {
  298. struct ieee80211_local *local = sdata->local;
  299. enum ieee80211_band band = local->oper_channel->band;
  300. struct ieee80211_supported_band *sband;
  301. u32 rates, basic_rates = 0;
  302. struct sta_info *sta;
  303. bool insert = false;
  304. sband = local->hw.wiphy->bands[band];
  305. rates = ieee80211_sta_get_rates(local, elems, band, &basic_rates);
  306. sta = sta_info_get(sdata, addr);
  307. if (!sta) {
  308. sta = mesh_plink_alloc(sdata, addr);
  309. if (!sta)
  310. return NULL;
  311. insert = true;
  312. }
  313. spin_lock_bh(&sta->lock);
  314. sta->last_rx = jiffies;
  315. sta->sta.supp_rates[band] = rates;
  316. if (elems->ht_cap_elem &&
  317. sdata->local->_oper_channel_type != NL80211_CHAN_NO_HT)
  318. ieee80211_ht_cap_ie_to_sta_ht_cap(sdata, sband,
  319. elems->ht_cap_elem,
  320. &sta->sta.ht_cap);
  321. else
  322. memset(&sta->sta.ht_cap, 0, sizeof(sta->sta.ht_cap));
  323. if (elems->ht_operation) {
  324. if (!(elems->ht_operation->ht_param &
  325. IEEE80211_HT_PARAM_CHAN_WIDTH_ANY))
  326. sta->sta.ht_cap.cap &=
  327. ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
  328. sta->ch_type =
  329. ieee80211_ht_oper_to_channel_type(elems->ht_operation);
  330. }
  331. rate_control_rate_init(sta);
  332. spin_unlock_bh(&sta->lock);
  333. if (insert && sta_info_insert(sta))
  334. return NULL;
  335. return sta;
  336. }
  337. void mesh_neighbour_update(struct ieee80211_sub_if_data *sdata,
  338. u8 *hw_addr,
  339. struct ieee802_11_elems *elems)
  340. {
  341. struct sta_info *sta;
  342. /* Userspace handles peer allocation when security is enabled */
  343. if (sdata->u.mesh.security & IEEE80211_MESH_SEC_AUTHED) {
  344. cfg80211_notify_new_peer_candidate(sdata->dev, hw_addr,
  345. elems->ie_start,
  346. elems->total_len,
  347. GFP_KERNEL);
  348. return;
  349. }
  350. rcu_read_lock();
  351. sta = mesh_peer_init(sdata, hw_addr, elems);
  352. if (!sta)
  353. goto out;
  354. if (mesh_peer_accepts_plinks(elems) &&
  355. sta->plink_state == NL80211_PLINK_LISTEN &&
  356. sdata->u.mesh.accepting_plinks &&
  357. sdata->u.mesh.mshcfg.auto_open_plinks &&
  358. rssi_threshold_check(sta, sdata))
  359. mesh_plink_open(sta);
  360. out:
  361. rcu_read_unlock();
  362. }
  363. static void mesh_plink_timer(unsigned long data)
  364. {
  365. struct sta_info *sta;
  366. __le16 llid, plid, reason;
  367. struct ieee80211_sub_if_data *sdata;
  368. /*
  369. * This STA is valid because sta_info_destroy() will
  370. * del_timer_sync() this timer after having made sure
  371. * it cannot be readded (by deleting the plink.)
  372. */
  373. sta = (struct sta_info *) data;
  374. if (sta->sdata->local->quiescing) {
  375. sta->plink_timer_was_running = true;
  376. return;
  377. }
  378. spin_lock_bh(&sta->lock);
  379. if (sta->ignore_plink_timer) {
  380. sta->ignore_plink_timer = false;
  381. spin_unlock_bh(&sta->lock);
  382. return;
  383. }
  384. mpl_dbg("Mesh plink timer for %pM fired on state %d\n",
  385. sta->sta.addr, sta->plink_state);
  386. reason = 0;
  387. llid = sta->llid;
  388. plid = sta->plid;
  389. sdata = sta->sdata;
  390. switch (sta->plink_state) {
  391. case NL80211_PLINK_OPN_RCVD:
  392. case NL80211_PLINK_OPN_SNT:
  393. /* retry timer */
  394. if (sta->plink_retries < dot11MeshMaxRetries(sdata)) {
  395. u32 rand;
  396. mpl_dbg("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("Mesh plink: starting establishment with %pM\n",
  471. sta->sta.addr);
  472. return mesh_plink_frame_tx(sdata, WLAN_SP_MESH_PEERING_OPEN,
  473. sta->sta.addr, llid, 0, 0);
  474. }
  475. void mesh_plink_block(struct sta_info *sta)
  476. {
  477. struct ieee80211_sub_if_data *sdata = sta->sdata;
  478. bool deactivated;
  479. spin_lock_bh(&sta->lock);
  480. deactivated = __mesh_plink_deactivate(sta);
  481. sta->plink_state = NL80211_PLINK_BLOCKED;
  482. spin_unlock_bh(&sta->lock);
  483. if (deactivated)
  484. ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BEACON);
  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. #ifdef CONFIG_MAC80211_VERBOSE_MPL_DEBUG
  500. static const char *mplstates[] = {
  501. [NL80211_PLINK_LISTEN] = "LISTEN",
  502. [NL80211_PLINK_OPN_SNT] = "OPN-SNT",
  503. [NL80211_PLINK_OPN_RCVD] = "OPN-RCVD",
  504. [NL80211_PLINK_CNF_RCVD] = "CNF_RCVD",
  505. [NL80211_PLINK_ESTAB] = "ESTAB",
  506. [NL80211_PLINK_HOLDING] = "HOLDING",
  507. [NL80211_PLINK_BLOCKED] = "BLOCKED"
  508. };
  509. #endif
  510. /* need action_code, aux */
  511. if (len < IEEE80211_MIN_ACTION_SIZE + 3)
  512. return;
  513. if (is_multicast_ether_addr(mgmt->da)) {
  514. mpl_dbg("Mesh plink: ignore frame from multicast address");
  515. return;
  516. }
  517. baseaddr = mgmt->u.action.u.self_prot.variable;
  518. baselen = (u8 *) mgmt->u.action.u.self_prot.variable - (u8 *) mgmt;
  519. if (mgmt->u.action.u.self_prot.action_code ==
  520. WLAN_SP_MESH_PEERING_CONFIRM) {
  521. baseaddr += 4;
  522. baselen += 4;
  523. }
  524. ieee802_11_parse_elems(baseaddr, len - baselen, &elems);
  525. if (!elems.peering) {
  526. mpl_dbg("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("Mesh plink: can't establish link with secure peer\n");
  532. return;
  533. }
  534. ftype = mgmt->u.action.u.self_prot.action_code;
  535. ie_len = elems.peering_len;
  536. if ((ftype == WLAN_SP_MESH_PEERING_OPEN && ie_len != 4) ||
  537. (ftype == WLAN_SP_MESH_PEERING_CONFIRM && ie_len != 6) ||
  538. (ftype == WLAN_SP_MESH_PEERING_CLOSE && ie_len != 6
  539. && ie_len != 8)) {
  540. mpl_dbg("Mesh plink: incorrect plink ie length %d %d\n",
  541. ftype, ie_len);
  542. return;
  543. }
  544. if (ftype != WLAN_SP_MESH_PEERING_CLOSE &&
  545. (!elems.mesh_id || !elems.mesh_config)) {
  546. mpl_dbg("Mesh plink: missing necessary ie\n");
  547. return;
  548. }
  549. /* Note the lines below are correct, the llid in the frame is the plid
  550. * from the point of view of this host.
  551. */
  552. memcpy(&plid, PLINK_GET_LLID(elems.peering), 2);
  553. if (ftype == WLAN_SP_MESH_PEERING_CONFIRM ||
  554. (ftype == WLAN_SP_MESH_PEERING_CLOSE && ie_len == 8))
  555. memcpy(&llid, PLINK_GET_PLID(elems.peering), 2);
  556. rcu_read_lock();
  557. sta = sta_info_get(sdata, mgmt->sa);
  558. if (!sta && ftype != WLAN_SP_MESH_PEERING_OPEN) {
  559. mpl_dbg("Mesh plink: cls or cnf from unknown peer\n");
  560. rcu_read_unlock();
  561. return;
  562. }
  563. if (ftype == WLAN_SP_MESH_PEERING_OPEN &&
  564. !rssi_threshold_check(sta, sdata)) {
  565. mpl_dbg("Mesh plink: %pM does not meet rssi threshold\n",
  566. mgmt->sa);
  567. rcu_read_unlock();
  568. return;
  569. }
  570. if (sta && !test_sta_flag(sta, WLAN_STA_AUTH)) {
  571. mpl_dbg("Mesh plink: Action frame from non-authed peer\n");
  572. rcu_read_unlock();
  573. return;
  574. }
  575. if (sta && sta->plink_state == NL80211_PLINK_BLOCKED) {
  576. rcu_read_unlock();
  577. return;
  578. }
  579. /* Now we will figure out the appropriate event... */
  580. event = PLINK_UNDEFINED;
  581. if (ftype != WLAN_SP_MESH_PEERING_CLOSE &&
  582. !mesh_matches_local(sdata, &elems)) {
  583. matches_local = false;
  584. switch (ftype) {
  585. case WLAN_SP_MESH_PEERING_OPEN:
  586. event = OPN_RJCT;
  587. break;
  588. case WLAN_SP_MESH_PEERING_CONFIRM:
  589. event = CNF_RJCT;
  590. break;
  591. default:
  592. break;
  593. }
  594. }
  595. if (!sta && !matches_local) {
  596. rcu_read_unlock();
  597. reason = cpu_to_le16(WLAN_REASON_MESH_CONFIG);
  598. llid = 0;
  599. mesh_plink_frame_tx(sdata, WLAN_SP_MESH_PEERING_CLOSE,
  600. mgmt->sa, llid, plid, reason);
  601. return;
  602. } else if (!sta) {
  603. /* ftype == WLAN_SP_MESH_PEERING_OPEN */
  604. if (!mesh_plink_free_count(sdata)) {
  605. mpl_dbg("Mesh plink error: no more free plinks\n");
  606. rcu_read_unlock();
  607. return;
  608. }
  609. event = OPN_ACPT;
  610. } else if (matches_local) {
  611. switch (ftype) {
  612. case WLAN_SP_MESH_PEERING_OPEN:
  613. if (!mesh_plink_free_count(sdata) ||
  614. (sta->plid && sta->plid != plid))
  615. event = OPN_IGNR;
  616. else
  617. event = OPN_ACPT;
  618. break;
  619. case WLAN_SP_MESH_PEERING_CONFIRM:
  620. if (!mesh_plink_free_count(sdata) ||
  621. (sta->llid != llid || sta->plid != plid))
  622. event = CNF_IGNR;
  623. else
  624. event = CNF_ACPT;
  625. break;
  626. case WLAN_SP_MESH_PEERING_CLOSE:
  627. if (sta->plink_state == NL80211_PLINK_ESTAB)
  628. /* Do not check for llid or plid. This does not
  629. * follow the standard but since multiple plinks
  630. * per sta are not supported, it is necessary in
  631. * order to avoid a livelock when MP A sees an
  632. * establish peer link to MP B but MP B does not
  633. * see it. This can be caused by a timeout in
  634. * B's peer link establishment or B beign
  635. * restarted.
  636. */
  637. event = CLS_ACPT;
  638. else if (sta->plid != plid)
  639. event = CLS_IGNR;
  640. else if (ie_len == 7 && sta->llid != llid)
  641. event = CLS_IGNR;
  642. else
  643. event = CLS_ACPT;
  644. break;
  645. default:
  646. mpl_dbg("Mesh plink: unknown frame subtype\n");
  647. rcu_read_unlock();
  648. return;
  649. }
  650. }
  651. if (event == OPN_ACPT) {
  652. /* allocate sta entry if necessary and update info */
  653. sta = mesh_peer_init(sdata, mgmt->sa, &elems);
  654. if (!sta) {
  655. mpl_dbg("Mesh plink: failed to init peer!\n");
  656. rcu_read_unlock();
  657. return;
  658. }
  659. }
  660. mpl_dbg("Mesh plink (peer, state, llid, plid, event): %pM %s %d %d %d\n",
  661. mgmt->sa, mplstates[sta->plink_state],
  662. le16_to_cpu(sta->llid), le16_to_cpu(sta->plid),
  663. event);
  664. reason = 0;
  665. spin_lock_bh(&sta->lock);
  666. switch (sta->plink_state) {
  667. /* spin_unlock as soon as state is updated at each case */
  668. case NL80211_PLINK_LISTEN:
  669. switch (event) {
  670. case CLS_ACPT:
  671. mesh_plink_fsm_restart(sta);
  672. spin_unlock_bh(&sta->lock);
  673. break;
  674. case OPN_ACPT:
  675. sta->plink_state = NL80211_PLINK_OPN_RCVD;
  676. sta->plid = plid;
  677. get_random_bytes(&llid, 2);
  678. sta->llid = llid;
  679. mesh_plink_timer_set(sta, dot11MeshRetryTimeout(sdata));
  680. spin_unlock_bh(&sta->lock);
  681. mesh_plink_frame_tx(sdata,
  682. WLAN_SP_MESH_PEERING_OPEN,
  683. sta->sta.addr, llid, 0, 0);
  684. mesh_plink_frame_tx(sdata,
  685. WLAN_SP_MESH_PEERING_CONFIRM,
  686. sta->sta.addr, llid, plid, 0);
  687. break;
  688. default:
  689. spin_unlock_bh(&sta->lock);
  690. break;
  691. }
  692. break;
  693. case NL80211_PLINK_OPN_SNT:
  694. switch (event) {
  695. case OPN_RJCT:
  696. case CNF_RJCT:
  697. reason = cpu_to_le16(WLAN_REASON_MESH_CONFIG);
  698. case CLS_ACPT:
  699. if (!reason)
  700. reason = cpu_to_le16(WLAN_REASON_MESH_CLOSE);
  701. sta->reason = reason;
  702. sta->plink_state = NL80211_PLINK_HOLDING;
  703. if (!mod_plink_timer(sta,
  704. dot11MeshHoldingTimeout(sdata)))
  705. sta->ignore_plink_timer = true;
  706. llid = sta->llid;
  707. spin_unlock_bh(&sta->lock);
  708. mesh_plink_frame_tx(sdata,
  709. WLAN_SP_MESH_PEERING_CLOSE,
  710. sta->sta.addr, llid, plid, reason);
  711. break;
  712. case OPN_ACPT:
  713. /* retry timer is left untouched */
  714. sta->plink_state = NL80211_PLINK_OPN_RCVD;
  715. sta->plid = plid;
  716. llid = sta->llid;
  717. spin_unlock_bh(&sta->lock);
  718. mesh_plink_frame_tx(sdata,
  719. WLAN_SP_MESH_PEERING_CONFIRM,
  720. sta->sta.addr, llid, plid, 0);
  721. break;
  722. case CNF_ACPT:
  723. sta->plink_state = NL80211_PLINK_CNF_RCVD;
  724. if (!mod_plink_timer(sta,
  725. dot11MeshConfirmTimeout(sdata)))
  726. sta->ignore_plink_timer = true;
  727. spin_unlock_bh(&sta->lock);
  728. break;
  729. default:
  730. spin_unlock_bh(&sta->lock);
  731. break;
  732. }
  733. break;
  734. case NL80211_PLINK_OPN_RCVD:
  735. switch (event) {
  736. case OPN_RJCT:
  737. case CNF_RJCT:
  738. reason = cpu_to_le16(WLAN_REASON_MESH_CONFIG);
  739. case CLS_ACPT:
  740. if (!reason)
  741. reason = cpu_to_le16(WLAN_REASON_MESH_CLOSE);
  742. sta->reason = reason;
  743. sta->plink_state = NL80211_PLINK_HOLDING;
  744. if (!mod_plink_timer(sta,
  745. dot11MeshHoldingTimeout(sdata)))
  746. sta->ignore_plink_timer = true;
  747. llid = sta->llid;
  748. spin_unlock_bh(&sta->lock);
  749. mesh_plink_frame_tx(sdata, WLAN_SP_MESH_PEERING_CLOSE,
  750. sta->sta.addr, llid, plid, reason);
  751. break;
  752. case OPN_ACPT:
  753. llid = sta->llid;
  754. spin_unlock_bh(&sta->lock);
  755. mesh_plink_frame_tx(sdata,
  756. WLAN_SP_MESH_PEERING_CONFIRM,
  757. sta->sta.addr, llid, plid, 0);
  758. break;
  759. case CNF_ACPT:
  760. del_timer(&sta->plink_timer);
  761. sta->plink_state = NL80211_PLINK_ESTAB;
  762. spin_unlock_bh(&sta->lock);
  763. mesh_plink_inc_estab_count(sdata);
  764. changed |= mesh_set_ht_prot_mode(sdata);
  765. changed |= BSS_CHANGED_BEACON;
  766. mpl_dbg("Mesh plink with %pM ESTABLISHED\n",
  767. sta->sta.addr);
  768. break;
  769. default:
  770. spin_unlock_bh(&sta->lock);
  771. break;
  772. }
  773. break;
  774. case NL80211_PLINK_CNF_RCVD:
  775. switch (event) {
  776. case OPN_RJCT:
  777. case CNF_RJCT:
  778. reason = cpu_to_le16(WLAN_REASON_MESH_CONFIG);
  779. case CLS_ACPT:
  780. if (!reason)
  781. reason = cpu_to_le16(WLAN_REASON_MESH_CLOSE);
  782. sta->reason = reason;
  783. sta->plink_state = NL80211_PLINK_HOLDING;
  784. if (!mod_plink_timer(sta,
  785. dot11MeshHoldingTimeout(sdata)))
  786. sta->ignore_plink_timer = true;
  787. llid = sta->llid;
  788. spin_unlock_bh(&sta->lock);
  789. mesh_plink_frame_tx(sdata,
  790. WLAN_SP_MESH_PEERING_CLOSE,
  791. sta->sta.addr, llid, plid, reason);
  792. break;
  793. case OPN_ACPT:
  794. del_timer(&sta->plink_timer);
  795. sta->plink_state = NL80211_PLINK_ESTAB;
  796. spin_unlock_bh(&sta->lock);
  797. mesh_plink_inc_estab_count(sdata);
  798. changed |= mesh_set_ht_prot_mode(sdata);
  799. changed |= BSS_CHANGED_BEACON;
  800. mpl_dbg("Mesh plink with %pM ESTABLISHED\n",
  801. sta->sta.addr);
  802. mesh_plink_frame_tx(sdata,
  803. WLAN_SP_MESH_PEERING_CONFIRM,
  804. sta->sta.addr, llid, plid, 0);
  805. break;
  806. default:
  807. spin_unlock_bh(&sta->lock);
  808. break;
  809. }
  810. break;
  811. case NL80211_PLINK_ESTAB:
  812. switch (event) {
  813. case CLS_ACPT:
  814. reason = cpu_to_le16(WLAN_REASON_MESH_CLOSE);
  815. sta->reason = reason;
  816. __mesh_plink_deactivate(sta);
  817. sta->plink_state = NL80211_PLINK_HOLDING;
  818. llid = sta->llid;
  819. mod_plink_timer(sta, dot11MeshHoldingTimeout(sdata));
  820. spin_unlock_bh(&sta->lock);
  821. changed |= mesh_set_ht_prot_mode(sdata);
  822. changed |= BSS_CHANGED_BEACON;
  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. }