mesh.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480
  1. /*
  2. * Copyright (c) 2008, 2009 open80211s Ltd.
  3. * Authors: Luis Carlos Cobo <luisca@cozybit.com>
  4. * Javier Cardona <javier@cozybit.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. */
  10. #include <linux/slab.h>
  11. #include <asm/unaligned.h>
  12. #include "ieee80211_i.h"
  13. #include "mesh.h"
  14. #include "driver-ops.h"
  15. static int mesh_allocated;
  16. static struct kmem_cache *rm_cache;
  17. bool mesh_action_is_path_sel(struct ieee80211_mgmt *mgmt)
  18. {
  19. return (mgmt->u.action.u.mesh_action.action_code ==
  20. WLAN_MESH_ACTION_HWMP_PATH_SELECTION);
  21. }
  22. void ieee80211s_init(void)
  23. {
  24. mesh_allocated = 1;
  25. rm_cache = kmem_cache_create("mesh_rmc", sizeof(struct rmc_entry),
  26. 0, 0, NULL);
  27. }
  28. void ieee80211s_stop(void)
  29. {
  30. if (!mesh_allocated)
  31. return;
  32. kmem_cache_destroy(rm_cache);
  33. }
  34. static void ieee80211_mesh_housekeeping_timer(struct timer_list *t)
  35. {
  36. struct ieee80211_sub_if_data *sdata =
  37. from_timer(sdata, t, u.mesh.housekeeping_timer);
  38. struct ieee80211_local *local = sdata->local;
  39. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  40. set_bit(MESH_WORK_HOUSEKEEPING, &ifmsh->wrkq_flags);
  41. ieee80211_queue_work(&local->hw, &sdata->work);
  42. }
  43. /**
  44. * mesh_matches_local - check if the config of a mesh point matches ours
  45. *
  46. * @sdata: local mesh subif
  47. * @ie: information elements of a management frame from the mesh peer
  48. *
  49. * This function checks if the mesh configuration of a mesh point matches the
  50. * local mesh configuration, i.e. if both nodes belong to the same mesh network.
  51. */
  52. bool mesh_matches_local(struct ieee80211_sub_if_data *sdata,
  53. struct ieee802_11_elems *ie)
  54. {
  55. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  56. u32 basic_rates = 0;
  57. struct cfg80211_chan_def sta_chan_def;
  58. struct ieee80211_supported_band *sband;
  59. /*
  60. * As support for each feature is added, check for matching
  61. * - On mesh config capabilities
  62. * - Power Save Support En
  63. * - Sync support enabled
  64. * - Sync support active
  65. * - Sync support required from peer
  66. * - MDA enabled
  67. * - Power management control on fc
  68. */
  69. if (!(ifmsh->mesh_id_len == ie->mesh_id_len &&
  70. memcmp(ifmsh->mesh_id, ie->mesh_id, ie->mesh_id_len) == 0 &&
  71. (ifmsh->mesh_pp_id == ie->mesh_config->meshconf_psel) &&
  72. (ifmsh->mesh_pm_id == ie->mesh_config->meshconf_pmetric) &&
  73. (ifmsh->mesh_cc_id == ie->mesh_config->meshconf_congest) &&
  74. (ifmsh->mesh_sp_id == ie->mesh_config->meshconf_synch) &&
  75. (ifmsh->mesh_auth_id == ie->mesh_config->meshconf_auth)))
  76. return false;
  77. sband = ieee80211_get_sband(sdata);
  78. if (!sband)
  79. return false;
  80. ieee80211_sta_get_rates(sdata, ie, sband->band,
  81. &basic_rates);
  82. if (sdata->vif.bss_conf.basic_rates != basic_rates)
  83. return false;
  84. cfg80211_chandef_create(&sta_chan_def, sdata->vif.bss_conf.chandef.chan,
  85. NL80211_CHAN_NO_HT);
  86. ieee80211_chandef_ht_oper(ie->ht_operation, &sta_chan_def);
  87. ieee80211_chandef_vht_oper(ie->vht_operation, &sta_chan_def);
  88. if (!cfg80211_chandef_compatible(&sdata->vif.bss_conf.chandef,
  89. &sta_chan_def))
  90. return false;
  91. return true;
  92. }
  93. /**
  94. * mesh_peer_accepts_plinks - check if an mp is willing to establish peer links
  95. *
  96. * @ie: information elements of a management frame from the mesh peer
  97. */
  98. bool mesh_peer_accepts_plinks(struct ieee802_11_elems *ie)
  99. {
  100. return (ie->mesh_config->meshconf_cap &
  101. IEEE80211_MESHCONF_CAPAB_ACCEPT_PLINKS) != 0;
  102. }
  103. /**
  104. * mesh_accept_plinks_update - update accepting_plink in local mesh beacons
  105. *
  106. * @sdata: mesh interface in which mesh beacons are going to be updated
  107. *
  108. * Returns: beacon changed flag if the beacon content changed.
  109. */
  110. u32 mesh_accept_plinks_update(struct ieee80211_sub_if_data *sdata)
  111. {
  112. bool free_plinks;
  113. u32 changed = 0;
  114. /* In case mesh_plink_free_count > 0 and mesh_plinktbl_capacity == 0,
  115. * the mesh interface might be able to establish plinks with peers that
  116. * are already on the table but are not on PLINK_ESTAB state. However,
  117. * in general the mesh interface is not accepting peer link requests
  118. * from new peers, and that must be reflected in the beacon
  119. */
  120. free_plinks = mesh_plink_availables(sdata);
  121. if (free_plinks != sdata->u.mesh.accepting_plinks) {
  122. sdata->u.mesh.accepting_plinks = free_plinks;
  123. changed = BSS_CHANGED_BEACON;
  124. }
  125. return changed;
  126. }
  127. /*
  128. * mesh_sta_cleanup - clean up any mesh sta state
  129. *
  130. * @sta: mesh sta to clean up.
  131. */
  132. void mesh_sta_cleanup(struct sta_info *sta)
  133. {
  134. struct ieee80211_sub_if_data *sdata = sta->sdata;
  135. u32 changed = mesh_plink_deactivate(sta);
  136. if (changed)
  137. ieee80211_mbss_info_change_notify(sdata, changed);
  138. }
  139. int mesh_rmc_init(struct ieee80211_sub_if_data *sdata)
  140. {
  141. int i;
  142. sdata->u.mesh.rmc = kmalloc(sizeof(struct mesh_rmc), GFP_KERNEL);
  143. if (!sdata->u.mesh.rmc)
  144. return -ENOMEM;
  145. sdata->u.mesh.rmc->idx_mask = RMC_BUCKETS - 1;
  146. for (i = 0; i < RMC_BUCKETS; i++)
  147. INIT_HLIST_HEAD(&sdata->u.mesh.rmc->bucket[i]);
  148. return 0;
  149. }
  150. void mesh_rmc_free(struct ieee80211_sub_if_data *sdata)
  151. {
  152. struct mesh_rmc *rmc = sdata->u.mesh.rmc;
  153. struct rmc_entry *p;
  154. struct hlist_node *n;
  155. int i;
  156. if (!sdata->u.mesh.rmc)
  157. return;
  158. for (i = 0; i < RMC_BUCKETS; i++) {
  159. hlist_for_each_entry_safe(p, n, &rmc->bucket[i], list) {
  160. hlist_del(&p->list);
  161. kmem_cache_free(rm_cache, p);
  162. }
  163. }
  164. kfree(rmc);
  165. sdata->u.mesh.rmc = NULL;
  166. }
  167. /**
  168. * mesh_rmc_check - Check frame in recent multicast cache and add if absent.
  169. *
  170. * @sdata: interface
  171. * @sa: source address
  172. * @mesh_hdr: mesh_header
  173. *
  174. * Returns: 0 if the frame is not in the cache, nonzero otherwise.
  175. *
  176. * Checks using the source address and the mesh sequence number if we have
  177. * received this frame lately. If the frame is not in the cache, it is added to
  178. * it.
  179. */
  180. int mesh_rmc_check(struct ieee80211_sub_if_data *sdata,
  181. const u8 *sa, struct ieee80211s_hdr *mesh_hdr)
  182. {
  183. struct mesh_rmc *rmc = sdata->u.mesh.rmc;
  184. u32 seqnum = 0;
  185. int entries = 0;
  186. u8 idx;
  187. struct rmc_entry *p;
  188. struct hlist_node *n;
  189. if (!rmc)
  190. return -1;
  191. /* Don't care about endianness since only match matters */
  192. memcpy(&seqnum, &mesh_hdr->seqnum, sizeof(mesh_hdr->seqnum));
  193. idx = le32_to_cpu(mesh_hdr->seqnum) & rmc->idx_mask;
  194. hlist_for_each_entry_safe(p, n, &rmc->bucket[idx], list) {
  195. ++entries;
  196. if (time_after(jiffies, p->exp_time) ||
  197. entries == RMC_QUEUE_MAX_LEN) {
  198. hlist_del(&p->list);
  199. kmem_cache_free(rm_cache, p);
  200. --entries;
  201. } else if ((seqnum == p->seqnum) && ether_addr_equal(sa, p->sa))
  202. return -1;
  203. }
  204. p = kmem_cache_alloc(rm_cache, GFP_ATOMIC);
  205. if (!p)
  206. return 0;
  207. p->seqnum = seqnum;
  208. p->exp_time = jiffies + RMC_TIMEOUT;
  209. memcpy(p->sa, sa, ETH_ALEN);
  210. hlist_add_head(&p->list, &rmc->bucket[idx]);
  211. return 0;
  212. }
  213. int mesh_add_meshconf_ie(struct ieee80211_sub_if_data *sdata,
  214. struct sk_buff *skb)
  215. {
  216. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  217. u8 *pos, neighbors;
  218. u8 meshconf_len = sizeof(struct ieee80211_meshconf_ie);
  219. if (skb_tailroom(skb) < 2 + meshconf_len)
  220. return -ENOMEM;
  221. pos = skb_put(skb, 2 + meshconf_len);
  222. *pos++ = WLAN_EID_MESH_CONFIG;
  223. *pos++ = meshconf_len;
  224. /* save a pointer for quick updates in pre-tbtt */
  225. ifmsh->meshconf_offset = pos - skb->data;
  226. /* Active path selection protocol ID */
  227. *pos++ = ifmsh->mesh_pp_id;
  228. /* Active path selection metric ID */
  229. *pos++ = ifmsh->mesh_pm_id;
  230. /* Congestion control mode identifier */
  231. *pos++ = ifmsh->mesh_cc_id;
  232. /* Synchronization protocol identifier */
  233. *pos++ = ifmsh->mesh_sp_id;
  234. /* Authentication Protocol identifier */
  235. *pos++ = ifmsh->mesh_auth_id;
  236. /* Mesh Formation Info - number of neighbors */
  237. neighbors = atomic_read(&ifmsh->estab_plinks);
  238. neighbors = min_t(int, neighbors, IEEE80211_MAX_MESH_PEERINGS);
  239. *pos++ = neighbors << 1;
  240. /* Mesh capability */
  241. *pos = 0x00;
  242. *pos |= ifmsh->mshcfg.dot11MeshForwarding ?
  243. IEEE80211_MESHCONF_CAPAB_FORWARDING : 0x00;
  244. *pos |= ifmsh->accepting_plinks ?
  245. IEEE80211_MESHCONF_CAPAB_ACCEPT_PLINKS : 0x00;
  246. /* Mesh PS mode. See IEEE802.11-2012 8.4.2.100.8 */
  247. *pos |= ifmsh->ps_peers_deep_sleep ?
  248. IEEE80211_MESHCONF_CAPAB_POWER_SAVE_LEVEL : 0x00;
  249. return 0;
  250. }
  251. int mesh_add_meshid_ie(struct ieee80211_sub_if_data *sdata, struct sk_buff *skb)
  252. {
  253. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  254. u8 *pos;
  255. if (skb_tailroom(skb) < 2 + ifmsh->mesh_id_len)
  256. return -ENOMEM;
  257. pos = skb_put(skb, 2 + ifmsh->mesh_id_len);
  258. *pos++ = WLAN_EID_MESH_ID;
  259. *pos++ = ifmsh->mesh_id_len;
  260. if (ifmsh->mesh_id_len)
  261. memcpy(pos, ifmsh->mesh_id, ifmsh->mesh_id_len);
  262. return 0;
  263. }
  264. static int mesh_add_awake_window_ie(struct ieee80211_sub_if_data *sdata,
  265. struct sk_buff *skb)
  266. {
  267. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  268. u8 *pos;
  269. /* see IEEE802.11-2012 13.14.6 */
  270. if (ifmsh->ps_peers_light_sleep == 0 &&
  271. ifmsh->ps_peers_deep_sleep == 0 &&
  272. ifmsh->nonpeer_pm == NL80211_MESH_POWER_ACTIVE)
  273. return 0;
  274. if (skb_tailroom(skb) < 4)
  275. return -ENOMEM;
  276. pos = skb_put(skb, 2 + 2);
  277. *pos++ = WLAN_EID_MESH_AWAKE_WINDOW;
  278. *pos++ = 2;
  279. put_unaligned_le16(ifmsh->mshcfg.dot11MeshAwakeWindowDuration, pos);
  280. return 0;
  281. }
  282. int mesh_add_vendor_ies(struct ieee80211_sub_if_data *sdata,
  283. struct sk_buff *skb)
  284. {
  285. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  286. u8 offset, len;
  287. const u8 *data;
  288. if (!ifmsh->ie || !ifmsh->ie_len)
  289. return 0;
  290. /* fast-forward to vendor IEs */
  291. offset = ieee80211_ie_split_vendor(ifmsh->ie, ifmsh->ie_len, 0);
  292. if (offset < ifmsh->ie_len) {
  293. len = ifmsh->ie_len - offset;
  294. data = ifmsh->ie + offset;
  295. if (skb_tailroom(skb) < len)
  296. return -ENOMEM;
  297. skb_put_data(skb, data, len);
  298. }
  299. return 0;
  300. }
  301. int mesh_add_rsn_ie(struct ieee80211_sub_if_data *sdata, struct sk_buff *skb)
  302. {
  303. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  304. u8 len = 0;
  305. const u8 *data;
  306. if (!ifmsh->ie || !ifmsh->ie_len)
  307. return 0;
  308. /* find RSN IE */
  309. data = cfg80211_find_ie(WLAN_EID_RSN, ifmsh->ie, ifmsh->ie_len);
  310. if (!data)
  311. return 0;
  312. len = data[1] + 2;
  313. if (skb_tailroom(skb) < len)
  314. return -ENOMEM;
  315. skb_put_data(skb, data, len);
  316. return 0;
  317. }
  318. static int mesh_add_ds_params_ie(struct ieee80211_sub_if_data *sdata,
  319. struct sk_buff *skb)
  320. {
  321. struct ieee80211_chanctx_conf *chanctx_conf;
  322. struct ieee80211_channel *chan;
  323. u8 *pos;
  324. if (skb_tailroom(skb) < 3)
  325. return -ENOMEM;
  326. rcu_read_lock();
  327. chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
  328. if (WARN_ON(!chanctx_conf)) {
  329. rcu_read_unlock();
  330. return -EINVAL;
  331. }
  332. chan = chanctx_conf->def.chan;
  333. rcu_read_unlock();
  334. pos = skb_put(skb, 2 + 1);
  335. *pos++ = WLAN_EID_DS_PARAMS;
  336. *pos++ = 1;
  337. *pos++ = ieee80211_frequency_to_channel(chan->center_freq);
  338. return 0;
  339. }
  340. int mesh_add_ht_cap_ie(struct ieee80211_sub_if_data *sdata,
  341. struct sk_buff *skb)
  342. {
  343. struct ieee80211_supported_band *sband;
  344. u8 *pos;
  345. sband = ieee80211_get_sband(sdata);
  346. if (!sband)
  347. return -EINVAL;
  348. if (!sband->ht_cap.ht_supported ||
  349. sdata->vif.bss_conf.chandef.width == NL80211_CHAN_WIDTH_20_NOHT ||
  350. sdata->vif.bss_conf.chandef.width == NL80211_CHAN_WIDTH_5 ||
  351. sdata->vif.bss_conf.chandef.width == NL80211_CHAN_WIDTH_10)
  352. return 0;
  353. if (skb_tailroom(skb) < 2 + sizeof(struct ieee80211_ht_cap))
  354. return -ENOMEM;
  355. pos = skb_put(skb, 2 + sizeof(struct ieee80211_ht_cap));
  356. ieee80211_ie_build_ht_cap(pos, &sband->ht_cap, sband->ht_cap.cap);
  357. return 0;
  358. }
  359. int mesh_add_ht_oper_ie(struct ieee80211_sub_if_data *sdata,
  360. struct sk_buff *skb)
  361. {
  362. struct ieee80211_local *local = sdata->local;
  363. struct ieee80211_chanctx_conf *chanctx_conf;
  364. struct ieee80211_channel *channel;
  365. struct ieee80211_supported_band *sband;
  366. struct ieee80211_sta_ht_cap *ht_cap;
  367. u8 *pos;
  368. rcu_read_lock();
  369. chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
  370. if (WARN_ON(!chanctx_conf)) {
  371. rcu_read_unlock();
  372. return -EINVAL;
  373. }
  374. channel = chanctx_conf->def.chan;
  375. rcu_read_unlock();
  376. sband = local->hw.wiphy->bands[channel->band];
  377. ht_cap = &sband->ht_cap;
  378. if (!ht_cap->ht_supported ||
  379. sdata->vif.bss_conf.chandef.width == NL80211_CHAN_WIDTH_20_NOHT ||
  380. sdata->vif.bss_conf.chandef.width == NL80211_CHAN_WIDTH_5 ||
  381. sdata->vif.bss_conf.chandef.width == NL80211_CHAN_WIDTH_10)
  382. return 0;
  383. if (skb_tailroom(skb) < 2 + sizeof(struct ieee80211_ht_operation))
  384. return -ENOMEM;
  385. pos = skb_put(skb, 2 + sizeof(struct ieee80211_ht_operation));
  386. ieee80211_ie_build_ht_oper(pos, ht_cap, &sdata->vif.bss_conf.chandef,
  387. sdata->vif.bss_conf.ht_operation_mode,
  388. false);
  389. return 0;
  390. }
  391. int mesh_add_vht_cap_ie(struct ieee80211_sub_if_data *sdata,
  392. struct sk_buff *skb)
  393. {
  394. struct ieee80211_supported_band *sband;
  395. u8 *pos;
  396. sband = ieee80211_get_sband(sdata);
  397. if (!sband)
  398. return -EINVAL;
  399. if (!sband->vht_cap.vht_supported ||
  400. sdata->vif.bss_conf.chandef.width == NL80211_CHAN_WIDTH_20_NOHT ||
  401. sdata->vif.bss_conf.chandef.width == NL80211_CHAN_WIDTH_5 ||
  402. sdata->vif.bss_conf.chandef.width == NL80211_CHAN_WIDTH_10)
  403. return 0;
  404. if (skb_tailroom(skb) < 2 + sizeof(struct ieee80211_vht_cap))
  405. return -ENOMEM;
  406. pos = skb_put(skb, 2 + sizeof(struct ieee80211_vht_cap));
  407. ieee80211_ie_build_vht_cap(pos, &sband->vht_cap, sband->vht_cap.cap);
  408. return 0;
  409. }
  410. int mesh_add_vht_oper_ie(struct ieee80211_sub_if_data *sdata,
  411. struct sk_buff *skb)
  412. {
  413. struct ieee80211_local *local = sdata->local;
  414. struct ieee80211_chanctx_conf *chanctx_conf;
  415. struct ieee80211_channel *channel;
  416. struct ieee80211_supported_band *sband;
  417. struct ieee80211_sta_vht_cap *vht_cap;
  418. u8 *pos;
  419. rcu_read_lock();
  420. chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
  421. if (WARN_ON(!chanctx_conf)) {
  422. rcu_read_unlock();
  423. return -EINVAL;
  424. }
  425. channel = chanctx_conf->def.chan;
  426. rcu_read_unlock();
  427. sband = local->hw.wiphy->bands[channel->band];
  428. vht_cap = &sband->vht_cap;
  429. if (!vht_cap->vht_supported ||
  430. sdata->vif.bss_conf.chandef.width == NL80211_CHAN_WIDTH_20_NOHT ||
  431. sdata->vif.bss_conf.chandef.width == NL80211_CHAN_WIDTH_5 ||
  432. sdata->vif.bss_conf.chandef.width == NL80211_CHAN_WIDTH_10)
  433. return 0;
  434. if (skb_tailroom(skb) < 2 + sizeof(struct ieee80211_vht_operation))
  435. return -ENOMEM;
  436. pos = skb_put(skb, 2 + sizeof(struct ieee80211_vht_operation));
  437. ieee80211_ie_build_vht_oper(pos, vht_cap,
  438. &sdata->vif.bss_conf.chandef);
  439. return 0;
  440. }
  441. static void ieee80211_mesh_path_timer(struct timer_list *t)
  442. {
  443. struct ieee80211_sub_if_data *sdata =
  444. from_timer(sdata, t, u.mesh.mesh_path_timer);
  445. ieee80211_queue_work(&sdata->local->hw, &sdata->work);
  446. }
  447. static void ieee80211_mesh_path_root_timer(struct timer_list *t)
  448. {
  449. struct ieee80211_sub_if_data *sdata =
  450. from_timer(sdata, t, u.mesh.mesh_path_root_timer);
  451. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  452. set_bit(MESH_WORK_ROOT, &ifmsh->wrkq_flags);
  453. ieee80211_queue_work(&sdata->local->hw, &sdata->work);
  454. }
  455. void ieee80211_mesh_root_setup(struct ieee80211_if_mesh *ifmsh)
  456. {
  457. if (ifmsh->mshcfg.dot11MeshHWMPRootMode > IEEE80211_ROOTMODE_ROOT)
  458. set_bit(MESH_WORK_ROOT, &ifmsh->wrkq_flags);
  459. else {
  460. clear_bit(MESH_WORK_ROOT, &ifmsh->wrkq_flags);
  461. /* stop running timer */
  462. del_timer_sync(&ifmsh->mesh_path_root_timer);
  463. }
  464. }
  465. /**
  466. * ieee80211_fill_mesh_addresses - fill addresses of a locally originated mesh frame
  467. * @hdr: 802.11 frame header
  468. * @fc: frame control field
  469. * @meshda: destination address in the mesh
  470. * @meshsa: source address address in the mesh. Same as TA, as frame is
  471. * locally originated.
  472. *
  473. * Return the length of the 802.11 (does not include a mesh control header)
  474. */
  475. int ieee80211_fill_mesh_addresses(struct ieee80211_hdr *hdr, __le16 *fc,
  476. const u8 *meshda, const u8 *meshsa)
  477. {
  478. if (is_multicast_ether_addr(meshda)) {
  479. *fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS);
  480. /* DA TA SA */
  481. memcpy(hdr->addr1, meshda, ETH_ALEN);
  482. memcpy(hdr->addr2, meshsa, ETH_ALEN);
  483. memcpy(hdr->addr3, meshsa, ETH_ALEN);
  484. return 24;
  485. } else {
  486. *fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS);
  487. /* RA TA DA SA */
  488. eth_zero_addr(hdr->addr1); /* RA is resolved later */
  489. memcpy(hdr->addr2, meshsa, ETH_ALEN);
  490. memcpy(hdr->addr3, meshda, ETH_ALEN);
  491. memcpy(hdr->addr4, meshsa, ETH_ALEN);
  492. return 30;
  493. }
  494. }
  495. /**
  496. * ieee80211_new_mesh_header - create a new mesh header
  497. * @sdata: mesh interface to be used
  498. * @meshhdr: uninitialized mesh header
  499. * @addr4or5: 1st address in the ae header, which may correspond to address 4
  500. * (if addr6 is NULL) or address 5 (if addr6 is present). It may
  501. * be NULL.
  502. * @addr6: 2nd address in the ae header, which corresponds to addr6 of the
  503. * mesh frame
  504. *
  505. * Return the header length.
  506. */
  507. unsigned int ieee80211_new_mesh_header(struct ieee80211_sub_if_data *sdata,
  508. struct ieee80211s_hdr *meshhdr,
  509. const char *addr4or5, const char *addr6)
  510. {
  511. if (WARN_ON(!addr4or5 && addr6))
  512. return 0;
  513. memset(meshhdr, 0, sizeof(*meshhdr));
  514. meshhdr->ttl = sdata->u.mesh.mshcfg.dot11MeshTTL;
  515. /* FIXME: racy -- TX on multiple queues can be concurrent */
  516. put_unaligned(cpu_to_le32(sdata->u.mesh.mesh_seqnum), &meshhdr->seqnum);
  517. sdata->u.mesh.mesh_seqnum++;
  518. if (addr4or5 && !addr6) {
  519. meshhdr->flags |= MESH_FLAGS_AE_A4;
  520. memcpy(meshhdr->eaddr1, addr4or5, ETH_ALEN);
  521. return 2 * ETH_ALEN;
  522. } else if (addr4or5 && addr6) {
  523. meshhdr->flags |= MESH_FLAGS_AE_A5_A6;
  524. memcpy(meshhdr->eaddr1, addr4or5, ETH_ALEN);
  525. memcpy(meshhdr->eaddr2, addr6, ETH_ALEN);
  526. return 3 * ETH_ALEN;
  527. }
  528. return ETH_ALEN;
  529. }
  530. static void ieee80211_mesh_housekeeping(struct ieee80211_sub_if_data *sdata)
  531. {
  532. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  533. u32 changed;
  534. if (ifmsh->mshcfg.plink_timeout > 0)
  535. ieee80211_sta_expire(sdata, ifmsh->mshcfg.plink_timeout * HZ);
  536. mesh_path_expire(sdata);
  537. changed = mesh_accept_plinks_update(sdata);
  538. ieee80211_mbss_info_change_notify(sdata, changed);
  539. mod_timer(&ifmsh->housekeeping_timer,
  540. round_jiffies(jiffies +
  541. IEEE80211_MESH_HOUSEKEEPING_INTERVAL));
  542. }
  543. static void ieee80211_mesh_rootpath(struct ieee80211_sub_if_data *sdata)
  544. {
  545. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  546. u32 interval;
  547. mesh_path_tx_root_frame(sdata);
  548. if (ifmsh->mshcfg.dot11MeshHWMPRootMode == IEEE80211_PROACTIVE_RANN)
  549. interval = ifmsh->mshcfg.dot11MeshHWMPRannInterval;
  550. else
  551. interval = ifmsh->mshcfg.dot11MeshHWMProotInterval;
  552. mod_timer(&ifmsh->mesh_path_root_timer,
  553. round_jiffies(TU_TO_EXP_TIME(interval)));
  554. }
  555. static int
  556. ieee80211_mesh_build_beacon(struct ieee80211_if_mesh *ifmsh)
  557. {
  558. struct beacon_data *bcn;
  559. int head_len, tail_len;
  560. struct sk_buff *skb;
  561. struct ieee80211_mgmt *mgmt;
  562. struct ieee80211_chanctx_conf *chanctx_conf;
  563. struct mesh_csa_settings *csa;
  564. enum nl80211_band band;
  565. u8 *pos;
  566. struct ieee80211_sub_if_data *sdata;
  567. int hdr_len = offsetofend(struct ieee80211_mgmt, u.beacon);
  568. sdata = container_of(ifmsh, struct ieee80211_sub_if_data, u.mesh);
  569. rcu_read_lock();
  570. chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
  571. band = chanctx_conf->def.chan->band;
  572. rcu_read_unlock();
  573. head_len = hdr_len +
  574. 2 + /* NULL SSID */
  575. /* Channel Switch Announcement */
  576. 2 + sizeof(struct ieee80211_channel_sw_ie) +
  577. /* Mesh Channel Switch Parameters */
  578. 2 + sizeof(struct ieee80211_mesh_chansw_params_ie) +
  579. /* Channel Switch Wrapper + Wide Bandwidth CSA IE */
  580. 2 + 2 + sizeof(struct ieee80211_wide_bw_chansw_ie) +
  581. 2 + sizeof(struct ieee80211_sec_chan_offs_ie) +
  582. 2 + 8 + /* supported rates */
  583. 2 + 3; /* DS params */
  584. tail_len = 2 + (IEEE80211_MAX_SUPP_RATES - 8) +
  585. 2 + sizeof(struct ieee80211_ht_cap) +
  586. 2 + sizeof(struct ieee80211_ht_operation) +
  587. 2 + ifmsh->mesh_id_len +
  588. 2 + sizeof(struct ieee80211_meshconf_ie) +
  589. 2 + sizeof(__le16) + /* awake window */
  590. 2 + sizeof(struct ieee80211_vht_cap) +
  591. 2 + sizeof(struct ieee80211_vht_operation) +
  592. ifmsh->ie_len;
  593. bcn = kzalloc(sizeof(*bcn) + head_len + tail_len, GFP_KERNEL);
  594. /* need an skb for IE builders to operate on */
  595. skb = dev_alloc_skb(max(head_len, tail_len));
  596. if (!bcn || !skb)
  597. goto out_free;
  598. /*
  599. * pointers go into the block we allocated,
  600. * memory is | beacon_data | head | tail |
  601. */
  602. bcn->head = ((u8 *) bcn) + sizeof(*bcn);
  603. /* fill in the head */
  604. mgmt = skb_put_zero(skb, hdr_len);
  605. mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
  606. IEEE80211_STYPE_BEACON);
  607. eth_broadcast_addr(mgmt->da);
  608. memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
  609. memcpy(mgmt->bssid, sdata->vif.addr, ETH_ALEN);
  610. ieee80211_mps_set_frame_flags(sdata, NULL, (void *) mgmt);
  611. mgmt->u.beacon.beacon_int =
  612. cpu_to_le16(sdata->vif.bss_conf.beacon_int);
  613. mgmt->u.beacon.capab_info |= cpu_to_le16(
  614. sdata->u.mesh.security ? WLAN_CAPABILITY_PRIVACY : 0);
  615. pos = skb_put(skb, 2);
  616. *pos++ = WLAN_EID_SSID;
  617. *pos++ = 0x0;
  618. rcu_read_lock();
  619. csa = rcu_dereference(ifmsh->csa);
  620. if (csa) {
  621. enum nl80211_channel_type ct;
  622. struct cfg80211_chan_def *chandef;
  623. int ie_len = 2 + sizeof(struct ieee80211_channel_sw_ie) +
  624. 2 + sizeof(struct ieee80211_mesh_chansw_params_ie);
  625. pos = skb_put_zero(skb, ie_len);
  626. *pos++ = WLAN_EID_CHANNEL_SWITCH;
  627. *pos++ = 3;
  628. *pos++ = 0x0;
  629. *pos++ = ieee80211_frequency_to_channel(
  630. csa->settings.chandef.chan->center_freq);
  631. bcn->csa_current_counter = csa->settings.count;
  632. bcn->csa_counter_offsets[0] = hdr_len + 6;
  633. *pos++ = csa->settings.count;
  634. *pos++ = WLAN_EID_CHAN_SWITCH_PARAM;
  635. *pos++ = 6;
  636. if (ifmsh->csa_role == IEEE80211_MESH_CSA_ROLE_INIT) {
  637. *pos++ = ifmsh->mshcfg.dot11MeshTTL;
  638. *pos |= WLAN_EID_CHAN_SWITCH_PARAM_INITIATOR;
  639. } else {
  640. *pos++ = ifmsh->chsw_ttl;
  641. }
  642. *pos++ |= csa->settings.block_tx ?
  643. WLAN_EID_CHAN_SWITCH_PARAM_TX_RESTRICT : 0x00;
  644. put_unaligned_le16(WLAN_REASON_MESH_CHAN, pos);
  645. pos += 2;
  646. put_unaligned_le16(ifmsh->pre_value, pos);
  647. pos += 2;
  648. switch (csa->settings.chandef.width) {
  649. case NL80211_CHAN_WIDTH_40:
  650. ie_len = 2 + sizeof(struct ieee80211_sec_chan_offs_ie);
  651. pos = skb_put_zero(skb, ie_len);
  652. *pos++ = WLAN_EID_SECONDARY_CHANNEL_OFFSET; /* EID */
  653. *pos++ = 1; /* len */
  654. ct = cfg80211_get_chandef_type(&csa->settings.chandef);
  655. if (ct == NL80211_CHAN_HT40PLUS)
  656. *pos++ = IEEE80211_HT_PARAM_CHA_SEC_ABOVE;
  657. else
  658. *pos++ = IEEE80211_HT_PARAM_CHA_SEC_BELOW;
  659. break;
  660. case NL80211_CHAN_WIDTH_80:
  661. case NL80211_CHAN_WIDTH_80P80:
  662. case NL80211_CHAN_WIDTH_160:
  663. /* Channel Switch Wrapper + Wide Bandwidth CSA IE */
  664. ie_len = 2 + 2 +
  665. sizeof(struct ieee80211_wide_bw_chansw_ie);
  666. pos = skb_put_zero(skb, ie_len);
  667. *pos++ = WLAN_EID_CHANNEL_SWITCH_WRAPPER; /* EID */
  668. *pos++ = 5; /* len */
  669. /* put sub IE */
  670. chandef = &csa->settings.chandef;
  671. ieee80211_ie_build_wide_bw_cs(pos, chandef);
  672. break;
  673. default:
  674. break;
  675. }
  676. }
  677. rcu_read_unlock();
  678. if (ieee80211_add_srates_ie(sdata, skb, true, band) ||
  679. mesh_add_ds_params_ie(sdata, skb))
  680. goto out_free;
  681. bcn->head_len = skb->len;
  682. memcpy(bcn->head, skb->data, bcn->head_len);
  683. /* now the tail */
  684. skb_trim(skb, 0);
  685. bcn->tail = bcn->head + bcn->head_len;
  686. if (ieee80211_add_ext_srates_ie(sdata, skb, true, band) ||
  687. mesh_add_rsn_ie(sdata, skb) ||
  688. mesh_add_ht_cap_ie(sdata, skb) ||
  689. mesh_add_ht_oper_ie(sdata, skb) ||
  690. mesh_add_meshid_ie(sdata, skb) ||
  691. mesh_add_meshconf_ie(sdata, skb) ||
  692. mesh_add_awake_window_ie(sdata, skb) ||
  693. mesh_add_vht_cap_ie(sdata, skb) ||
  694. mesh_add_vht_oper_ie(sdata, skb) ||
  695. mesh_add_vendor_ies(sdata, skb))
  696. goto out_free;
  697. bcn->tail_len = skb->len;
  698. memcpy(bcn->tail, skb->data, bcn->tail_len);
  699. bcn->meshconf = (struct ieee80211_meshconf_ie *)
  700. (bcn->tail + ifmsh->meshconf_offset);
  701. dev_kfree_skb(skb);
  702. rcu_assign_pointer(ifmsh->beacon, bcn);
  703. return 0;
  704. out_free:
  705. kfree(bcn);
  706. dev_kfree_skb(skb);
  707. return -ENOMEM;
  708. }
  709. static int
  710. ieee80211_mesh_rebuild_beacon(struct ieee80211_sub_if_data *sdata)
  711. {
  712. struct beacon_data *old_bcn;
  713. int ret;
  714. old_bcn = rcu_dereference_protected(sdata->u.mesh.beacon,
  715. lockdep_is_held(&sdata->wdev.mtx));
  716. ret = ieee80211_mesh_build_beacon(&sdata->u.mesh);
  717. if (ret)
  718. /* just reuse old beacon */
  719. return ret;
  720. if (old_bcn)
  721. kfree_rcu(old_bcn, rcu_head);
  722. return 0;
  723. }
  724. void ieee80211_mbss_info_change_notify(struct ieee80211_sub_if_data *sdata,
  725. u32 changed)
  726. {
  727. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  728. unsigned long bits = changed;
  729. u32 bit;
  730. if (!bits)
  731. return;
  732. /* if we race with running work, worst case this work becomes a noop */
  733. for_each_set_bit(bit, &bits, sizeof(changed) * BITS_PER_BYTE)
  734. set_bit(bit, &ifmsh->mbss_changed);
  735. set_bit(MESH_WORK_MBSS_CHANGED, &ifmsh->wrkq_flags);
  736. ieee80211_queue_work(&sdata->local->hw, &sdata->work);
  737. }
  738. int ieee80211_start_mesh(struct ieee80211_sub_if_data *sdata)
  739. {
  740. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  741. struct ieee80211_local *local = sdata->local;
  742. u32 changed = BSS_CHANGED_BEACON |
  743. BSS_CHANGED_BEACON_ENABLED |
  744. BSS_CHANGED_HT |
  745. BSS_CHANGED_BASIC_RATES |
  746. BSS_CHANGED_BEACON_INT |
  747. BSS_CHANGED_MCAST_RATE;
  748. local->fif_other_bss++;
  749. /* mesh ifaces must set allmulti to forward mcast traffic */
  750. atomic_inc(&local->iff_allmultis);
  751. ieee80211_configure_filter(local);
  752. ifmsh->mesh_cc_id = 0; /* Disabled */
  753. /* register sync ops from extensible synchronization framework */
  754. ifmsh->sync_ops = ieee80211_mesh_sync_ops_get(ifmsh->mesh_sp_id);
  755. ifmsh->sync_offset_clockdrift_max = 0;
  756. set_bit(MESH_WORK_HOUSEKEEPING, &ifmsh->wrkq_flags);
  757. ieee80211_mesh_root_setup(ifmsh);
  758. ieee80211_queue_work(&local->hw, &sdata->work);
  759. sdata->vif.bss_conf.ht_operation_mode =
  760. ifmsh->mshcfg.ht_opmode;
  761. sdata->vif.bss_conf.enable_beacon = true;
  762. changed |= ieee80211_mps_local_status_update(sdata);
  763. if (ieee80211_mesh_build_beacon(ifmsh)) {
  764. ieee80211_stop_mesh(sdata);
  765. return -ENOMEM;
  766. }
  767. ieee80211_recalc_dtim(local, sdata);
  768. ieee80211_bss_info_change_notify(sdata, changed);
  769. netif_carrier_on(sdata->dev);
  770. return 0;
  771. }
  772. void ieee80211_stop_mesh(struct ieee80211_sub_if_data *sdata)
  773. {
  774. struct ieee80211_local *local = sdata->local;
  775. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  776. struct beacon_data *bcn;
  777. netif_carrier_off(sdata->dev);
  778. /* flush STAs and mpaths on this iface */
  779. sta_info_flush(sdata);
  780. ieee80211_free_keys(sdata, true);
  781. mesh_path_flush_by_iface(sdata);
  782. /* stop the beacon */
  783. ifmsh->mesh_id_len = 0;
  784. sdata->vif.bss_conf.enable_beacon = false;
  785. clear_bit(SDATA_STATE_OFFCHANNEL_BEACON_STOPPED, &sdata->state);
  786. ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BEACON_ENABLED);
  787. /* remove beacon */
  788. bcn = rcu_dereference_protected(ifmsh->beacon,
  789. lockdep_is_held(&sdata->wdev.mtx));
  790. RCU_INIT_POINTER(ifmsh->beacon, NULL);
  791. kfree_rcu(bcn, rcu_head);
  792. /* free all potentially still buffered group-addressed frames */
  793. local->total_ps_buffered -= skb_queue_len(&ifmsh->ps.bc_buf);
  794. skb_queue_purge(&ifmsh->ps.bc_buf);
  795. del_timer_sync(&sdata->u.mesh.housekeeping_timer);
  796. del_timer_sync(&sdata->u.mesh.mesh_path_root_timer);
  797. del_timer_sync(&sdata->u.mesh.mesh_path_timer);
  798. /* clear any mesh work (for next join) we may have accrued */
  799. ifmsh->wrkq_flags = 0;
  800. ifmsh->mbss_changed = 0;
  801. local->fif_other_bss--;
  802. atomic_dec(&local->iff_allmultis);
  803. ieee80211_configure_filter(local);
  804. }
  805. static void ieee80211_mesh_csa_mark_radar(struct ieee80211_sub_if_data *sdata)
  806. {
  807. int err;
  808. /* if the current channel is a DFS channel, mark the channel as
  809. * unavailable.
  810. */
  811. err = cfg80211_chandef_dfs_required(sdata->local->hw.wiphy,
  812. &sdata->vif.bss_conf.chandef,
  813. NL80211_IFTYPE_MESH_POINT);
  814. if (err > 0)
  815. cfg80211_radar_event(sdata->local->hw.wiphy,
  816. &sdata->vif.bss_conf.chandef, GFP_ATOMIC);
  817. }
  818. static bool
  819. ieee80211_mesh_process_chnswitch(struct ieee80211_sub_if_data *sdata,
  820. struct ieee802_11_elems *elems, bool beacon)
  821. {
  822. struct cfg80211_csa_settings params;
  823. struct ieee80211_csa_ie csa_ie;
  824. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  825. struct ieee80211_supported_band *sband;
  826. int err;
  827. u32 sta_flags;
  828. sdata_assert_lock(sdata);
  829. sband = ieee80211_get_sband(sdata);
  830. if (!sband)
  831. return false;
  832. sta_flags = 0;
  833. switch (sdata->vif.bss_conf.chandef.width) {
  834. case NL80211_CHAN_WIDTH_20_NOHT:
  835. sta_flags |= IEEE80211_STA_DISABLE_HT;
  836. /* fall through */
  837. case NL80211_CHAN_WIDTH_20:
  838. sta_flags |= IEEE80211_STA_DISABLE_40MHZ;
  839. /* fall through */
  840. case NL80211_CHAN_WIDTH_40:
  841. sta_flags |= IEEE80211_STA_DISABLE_VHT;
  842. break;
  843. default:
  844. break;
  845. }
  846. memset(&params, 0, sizeof(params));
  847. err = ieee80211_parse_ch_switch_ie(sdata, elems, sband->band,
  848. sta_flags, sdata->vif.addr,
  849. &csa_ie);
  850. if (err < 0)
  851. return false;
  852. if (err)
  853. return false;
  854. /* Mark the channel unavailable if the reason for the switch is
  855. * regulatory.
  856. */
  857. if (csa_ie.reason_code == WLAN_REASON_MESH_CHAN_REGULATORY)
  858. ieee80211_mesh_csa_mark_radar(sdata);
  859. params.chandef = csa_ie.chandef;
  860. params.count = csa_ie.count;
  861. if (!cfg80211_chandef_usable(sdata->local->hw.wiphy, &params.chandef,
  862. IEEE80211_CHAN_DISABLED) ||
  863. !cfg80211_reg_can_beacon(sdata->local->hw.wiphy, &params.chandef,
  864. NL80211_IFTYPE_MESH_POINT)) {
  865. sdata_info(sdata,
  866. "mesh STA %pM switches to unsupported channel (%d MHz, width:%d, CF1/2: %d/%d MHz), aborting\n",
  867. sdata->vif.addr,
  868. params.chandef.chan->center_freq,
  869. params.chandef.width,
  870. params.chandef.center_freq1,
  871. params.chandef.center_freq2);
  872. return false;
  873. }
  874. err = cfg80211_chandef_dfs_required(sdata->local->hw.wiphy,
  875. &params.chandef,
  876. NL80211_IFTYPE_MESH_POINT);
  877. if (err < 0)
  878. return false;
  879. if (err > 0 && !ifmsh->userspace_handles_dfs) {
  880. sdata_info(sdata,
  881. "mesh STA %pM switches to channel requiring DFS (%d MHz, width:%d, CF1/2: %d/%d MHz), aborting\n",
  882. sdata->vif.addr,
  883. params.chandef.chan->center_freq,
  884. params.chandef.width,
  885. params.chandef.center_freq1,
  886. params.chandef.center_freq2);
  887. return false;
  888. }
  889. params.radar_required = err;
  890. if (cfg80211_chandef_identical(&params.chandef,
  891. &sdata->vif.bss_conf.chandef)) {
  892. mcsa_dbg(sdata,
  893. "received csa with an identical chandef, ignoring\n");
  894. return true;
  895. }
  896. mcsa_dbg(sdata,
  897. "received channel switch announcement to go to channel %d MHz\n",
  898. params.chandef.chan->center_freq);
  899. params.block_tx = csa_ie.mode & WLAN_EID_CHAN_SWITCH_PARAM_TX_RESTRICT;
  900. if (beacon) {
  901. ifmsh->chsw_ttl = csa_ie.ttl - 1;
  902. if (ifmsh->pre_value >= csa_ie.pre_value)
  903. return false;
  904. ifmsh->pre_value = csa_ie.pre_value;
  905. }
  906. if (ifmsh->chsw_ttl >= ifmsh->mshcfg.dot11MeshTTL)
  907. return false;
  908. ifmsh->csa_role = IEEE80211_MESH_CSA_ROLE_REPEATER;
  909. if (ieee80211_channel_switch(sdata->local->hw.wiphy, sdata->dev,
  910. &params) < 0)
  911. return false;
  912. return true;
  913. }
  914. static void
  915. ieee80211_mesh_rx_probe_req(struct ieee80211_sub_if_data *sdata,
  916. struct ieee80211_mgmt *mgmt, size_t len)
  917. {
  918. struct ieee80211_local *local = sdata->local;
  919. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  920. struct sk_buff *presp;
  921. struct beacon_data *bcn;
  922. struct ieee80211_mgmt *hdr;
  923. struct ieee802_11_elems elems;
  924. size_t baselen;
  925. u8 *pos;
  926. pos = mgmt->u.probe_req.variable;
  927. baselen = (u8 *) pos - (u8 *) mgmt;
  928. if (baselen > len)
  929. return;
  930. ieee802_11_parse_elems(pos, len - baselen, false, &elems);
  931. if (!elems.mesh_id)
  932. return;
  933. /* 802.11-2012 10.1.4.3.2 */
  934. if ((!ether_addr_equal(mgmt->da, sdata->vif.addr) &&
  935. !is_broadcast_ether_addr(mgmt->da)) ||
  936. elems.ssid_len != 0)
  937. return;
  938. if (elems.mesh_id_len != 0 &&
  939. (elems.mesh_id_len != ifmsh->mesh_id_len ||
  940. memcmp(elems.mesh_id, ifmsh->mesh_id, ifmsh->mesh_id_len)))
  941. return;
  942. rcu_read_lock();
  943. bcn = rcu_dereference(ifmsh->beacon);
  944. if (!bcn)
  945. goto out;
  946. presp = dev_alloc_skb(local->tx_headroom +
  947. bcn->head_len + bcn->tail_len);
  948. if (!presp)
  949. goto out;
  950. skb_reserve(presp, local->tx_headroom);
  951. skb_put_data(presp, bcn->head, bcn->head_len);
  952. skb_put_data(presp, bcn->tail, bcn->tail_len);
  953. hdr = (struct ieee80211_mgmt *) presp->data;
  954. hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
  955. IEEE80211_STYPE_PROBE_RESP);
  956. memcpy(hdr->da, mgmt->sa, ETH_ALEN);
  957. IEEE80211_SKB_CB(presp)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
  958. ieee80211_tx_skb(sdata, presp);
  959. out:
  960. rcu_read_unlock();
  961. }
  962. static void ieee80211_mesh_rx_bcn_presp(struct ieee80211_sub_if_data *sdata,
  963. u16 stype,
  964. struct ieee80211_mgmt *mgmt,
  965. size_t len,
  966. struct ieee80211_rx_status *rx_status)
  967. {
  968. struct ieee80211_local *local = sdata->local;
  969. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  970. struct ieee802_11_elems elems;
  971. struct ieee80211_channel *channel;
  972. size_t baselen;
  973. int freq;
  974. enum nl80211_band band = rx_status->band;
  975. /* ignore ProbeResp to foreign address */
  976. if (stype == IEEE80211_STYPE_PROBE_RESP &&
  977. !ether_addr_equal(mgmt->da, sdata->vif.addr))
  978. return;
  979. baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt;
  980. if (baselen > len)
  981. return;
  982. ieee802_11_parse_elems(mgmt->u.probe_resp.variable, len - baselen,
  983. false, &elems);
  984. /* ignore non-mesh or secure / unsecure mismatch */
  985. if ((!elems.mesh_id || !elems.mesh_config) ||
  986. (elems.rsn && sdata->u.mesh.security == IEEE80211_MESH_SEC_NONE) ||
  987. (!elems.rsn && sdata->u.mesh.security != IEEE80211_MESH_SEC_NONE))
  988. return;
  989. if (elems.ds_params)
  990. freq = ieee80211_channel_to_frequency(elems.ds_params[0], band);
  991. else
  992. freq = rx_status->freq;
  993. channel = ieee80211_get_channel(local->hw.wiphy, freq);
  994. if (!channel || channel->flags & IEEE80211_CHAN_DISABLED)
  995. return;
  996. if (mesh_matches_local(sdata, &elems)) {
  997. mpl_dbg(sdata, "rssi_threshold=%d,rx_status->signal=%d\n",
  998. sdata->u.mesh.mshcfg.rssi_threshold, rx_status->signal);
  999. if (!sdata->u.mesh.user_mpm ||
  1000. sdata->u.mesh.mshcfg.rssi_threshold == 0 ||
  1001. sdata->u.mesh.mshcfg.rssi_threshold < rx_status->signal)
  1002. mesh_neighbour_update(sdata, mgmt->sa, &elems);
  1003. }
  1004. if (ifmsh->sync_ops)
  1005. ifmsh->sync_ops->rx_bcn_presp(sdata,
  1006. stype, mgmt, &elems, rx_status);
  1007. if (ifmsh->csa_role != IEEE80211_MESH_CSA_ROLE_INIT &&
  1008. !sdata->vif.csa_active)
  1009. ieee80211_mesh_process_chnswitch(sdata, &elems, true);
  1010. }
  1011. int ieee80211_mesh_finish_csa(struct ieee80211_sub_if_data *sdata)
  1012. {
  1013. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  1014. struct mesh_csa_settings *tmp_csa_settings;
  1015. int ret = 0;
  1016. int changed = 0;
  1017. /* Reset the TTL value and Initiator flag */
  1018. ifmsh->csa_role = IEEE80211_MESH_CSA_ROLE_NONE;
  1019. ifmsh->chsw_ttl = 0;
  1020. /* Remove the CSA and MCSP elements from the beacon */
  1021. tmp_csa_settings = rcu_dereference_protected(ifmsh->csa,
  1022. lockdep_is_held(&sdata->wdev.mtx));
  1023. RCU_INIT_POINTER(ifmsh->csa, NULL);
  1024. if (tmp_csa_settings)
  1025. kfree_rcu(tmp_csa_settings, rcu_head);
  1026. ret = ieee80211_mesh_rebuild_beacon(sdata);
  1027. if (ret)
  1028. return -EINVAL;
  1029. changed |= BSS_CHANGED_BEACON;
  1030. mcsa_dbg(sdata, "complete switching to center freq %d MHz",
  1031. sdata->vif.bss_conf.chandef.chan->center_freq);
  1032. return changed;
  1033. }
  1034. int ieee80211_mesh_csa_beacon(struct ieee80211_sub_if_data *sdata,
  1035. struct cfg80211_csa_settings *csa_settings)
  1036. {
  1037. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  1038. struct mesh_csa_settings *tmp_csa_settings;
  1039. int ret = 0;
  1040. lockdep_assert_held(&sdata->wdev.mtx);
  1041. tmp_csa_settings = kmalloc(sizeof(*tmp_csa_settings),
  1042. GFP_ATOMIC);
  1043. if (!tmp_csa_settings)
  1044. return -ENOMEM;
  1045. memcpy(&tmp_csa_settings->settings, csa_settings,
  1046. sizeof(struct cfg80211_csa_settings));
  1047. rcu_assign_pointer(ifmsh->csa, tmp_csa_settings);
  1048. ret = ieee80211_mesh_rebuild_beacon(sdata);
  1049. if (ret) {
  1050. tmp_csa_settings = rcu_dereference(ifmsh->csa);
  1051. RCU_INIT_POINTER(ifmsh->csa, NULL);
  1052. kfree_rcu(tmp_csa_settings, rcu_head);
  1053. return ret;
  1054. }
  1055. return BSS_CHANGED_BEACON;
  1056. }
  1057. static int mesh_fwd_csa_frame(struct ieee80211_sub_if_data *sdata,
  1058. struct ieee80211_mgmt *mgmt, size_t len,
  1059. struct ieee802_11_elems *elems)
  1060. {
  1061. struct ieee80211_mgmt *mgmt_fwd;
  1062. struct sk_buff *skb;
  1063. struct ieee80211_local *local = sdata->local;
  1064. skb = dev_alloc_skb(local->tx_headroom + len);
  1065. if (!skb)
  1066. return -ENOMEM;
  1067. skb_reserve(skb, local->tx_headroom);
  1068. mgmt_fwd = skb_put(skb, len);
  1069. elems->mesh_chansw_params_ie->mesh_ttl--;
  1070. elems->mesh_chansw_params_ie->mesh_flags &=
  1071. ~WLAN_EID_CHAN_SWITCH_PARAM_INITIATOR;
  1072. memcpy(mgmt_fwd, mgmt, len);
  1073. eth_broadcast_addr(mgmt_fwd->da);
  1074. memcpy(mgmt_fwd->sa, sdata->vif.addr, ETH_ALEN);
  1075. memcpy(mgmt_fwd->bssid, sdata->vif.addr, ETH_ALEN);
  1076. ieee80211_tx_skb(sdata, skb);
  1077. return 0;
  1078. }
  1079. static void mesh_rx_csa_frame(struct ieee80211_sub_if_data *sdata,
  1080. struct ieee80211_mgmt *mgmt, size_t len)
  1081. {
  1082. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  1083. struct ieee802_11_elems elems;
  1084. u16 pre_value;
  1085. bool fwd_csa = true;
  1086. size_t baselen;
  1087. u8 *pos;
  1088. if (mgmt->u.action.u.measurement.action_code !=
  1089. WLAN_ACTION_SPCT_CHL_SWITCH)
  1090. return;
  1091. pos = mgmt->u.action.u.chan_switch.variable;
  1092. baselen = offsetof(struct ieee80211_mgmt,
  1093. u.action.u.chan_switch.variable);
  1094. ieee802_11_parse_elems(pos, len - baselen, true, &elems);
  1095. ifmsh->chsw_ttl = elems.mesh_chansw_params_ie->mesh_ttl;
  1096. if (!--ifmsh->chsw_ttl)
  1097. fwd_csa = false;
  1098. pre_value = le16_to_cpu(elems.mesh_chansw_params_ie->mesh_pre_value);
  1099. if (ifmsh->pre_value >= pre_value)
  1100. return;
  1101. ifmsh->pre_value = pre_value;
  1102. if (!sdata->vif.csa_active &&
  1103. !ieee80211_mesh_process_chnswitch(sdata, &elems, false)) {
  1104. mcsa_dbg(sdata, "Failed to process CSA action frame");
  1105. return;
  1106. }
  1107. /* forward or re-broadcast the CSA frame */
  1108. if (fwd_csa) {
  1109. if (mesh_fwd_csa_frame(sdata, mgmt, len, &elems) < 0)
  1110. mcsa_dbg(sdata, "Failed to forward the CSA frame");
  1111. }
  1112. }
  1113. static void ieee80211_mesh_rx_mgmt_action(struct ieee80211_sub_if_data *sdata,
  1114. struct ieee80211_mgmt *mgmt,
  1115. size_t len,
  1116. struct ieee80211_rx_status *rx_status)
  1117. {
  1118. switch (mgmt->u.action.category) {
  1119. case WLAN_CATEGORY_SELF_PROTECTED:
  1120. switch (mgmt->u.action.u.self_prot.action_code) {
  1121. case WLAN_SP_MESH_PEERING_OPEN:
  1122. case WLAN_SP_MESH_PEERING_CLOSE:
  1123. case WLAN_SP_MESH_PEERING_CONFIRM:
  1124. mesh_rx_plink_frame(sdata, mgmt, len, rx_status);
  1125. break;
  1126. }
  1127. break;
  1128. case WLAN_CATEGORY_MESH_ACTION:
  1129. if (mesh_action_is_path_sel(mgmt))
  1130. mesh_rx_path_sel_frame(sdata, mgmt, len);
  1131. break;
  1132. case WLAN_CATEGORY_SPECTRUM_MGMT:
  1133. mesh_rx_csa_frame(sdata, mgmt, len);
  1134. break;
  1135. }
  1136. }
  1137. void ieee80211_mesh_rx_queued_mgmt(struct ieee80211_sub_if_data *sdata,
  1138. struct sk_buff *skb)
  1139. {
  1140. struct ieee80211_rx_status *rx_status;
  1141. struct ieee80211_mgmt *mgmt;
  1142. u16 stype;
  1143. sdata_lock(sdata);
  1144. /* mesh already went down */
  1145. if (!sdata->u.mesh.mesh_id_len)
  1146. goto out;
  1147. rx_status = IEEE80211_SKB_RXCB(skb);
  1148. mgmt = (struct ieee80211_mgmt *) skb->data;
  1149. stype = le16_to_cpu(mgmt->frame_control) & IEEE80211_FCTL_STYPE;
  1150. switch (stype) {
  1151. case IEEE80211_STYPE_PROBE_RESP:
  1152. case IEEE80211_STYPE_BEACON:
  1153. ieee80211_mesh_rx_bcn_presp(sdata, stype, mgmt, skb->len,
  1154. rx_status);
  1155. break;
  1156. case IEEE80211_STYPE_PROBE_REQ:
  1157. ieee80211_mesh_rx_probe_req(sdata, mgmt, skb->len);
  1158. break;
  1159. case IEEE80211_STYPE_ACTION:
  1160. ieee80211_mesh_rx_mgmt_action(sdata, mgmt, skb->len, rx_status);
  1161. break;
  1162. }
  1163. out:
  1164. sdata_unlock(sdata);
  1165. }
  1166. static void mesh_bss_info_changed(struct ieee80211_sub_if_data *sdata)
  1167. {
  1168. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  1169. u32 bit, changed = 0;
  1170. for_each_set_bit(bit, &ifmsh->mbss_changed,
  1171. sizeof(changed) * BITS_PER_BYTE) {
  1172. clear_bit(bit, &ifmsh->mbss_changed);
  1173. changed |= BIT(bit);
  1174. }
  1175. if (sdata->vif.bss_conf.enable_beacon &&
  1176. (changed & (BSS_CHANGED_BEACON |
  1177. BSS_CHANGED_HT |
  1178. BSS_CHANGED_BASIC_RATES |
  1179. BSS_CHANGED_BEACON_INT)))
  1180. if (ieee80211_mesh_rebuild_beacon(sdata))
  1181. return;
  1182. ieee80211_bss_info_change_notify(sdata, changed);
  1183. }
  1184. void ieee80211_mesh_work(struct ieee80211_sub_if_data *sdata)
  1185. {
  1186. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  1187. sdata_lock(sdata);
  1188. /* mesh already went down */
  1189. if (!sdata->u.mesh.mesh_id_len)
  1190. goto out;
  1191. if (ifmsh->preq_queue_len &&
  1192. time_after(jiffies,
  1193. ifmsh->last_preq + msecs_to_jiffies(ifmsh->mshcfg.dot11MeshHWMPpreqMinInterval)))
  1194. mesh_path_start_discovery(sdata);
  1195. if (test_and_clear_bit(MESH_WORK_HOUSEKEEPING, &ifmsh->wrkq_flags))
  1196. ieee80211_mesh_housekeeping(sdata);
  1197. if (test_and_clear_bit(MESH_WORK_ROOT, &ifmsh->wrkq_flags))
  1198. ieee80211_mesh_rootpath(sdata);
  1199. if (test_and_clear_bit(MESH_WORK_DRIFT_ADJUST, &ifmsh->wrkq_flags))
  1200. mesh_sync_adjust_tsf(sdata);
  1201. if (test_and_clear_bit(MESH_WORK_MBSS_CHANGED, &ifmsh->wrkq_flags))
  1202. mesh_bss_info_changed(sdata);
  1203. out:
  1204. sdata_unlock(sdata);
  1205. }
  1206. void ieee80211_mesh_init_sdata(struct ieee80211_sub_if_data *sdata)
  1207. {
  1208. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  1209. static u8 zero_addr[ETH_ALEN] = {};
  1210. timer_setup(&ifmsh->housekeeping_timer,
  1211. ieee80211_mesh_housekeeping_timer, 0);
  1212. ifmsh->accepting_plinks = true;
  1213. atomic_set(&ifmsh->mpaths, 0);
  1214. mesh_rmc_init(sdata);
  1215. ifmsh->last_preq = jiffies;
  1216. ifmsh->next_perr = jiffies;
  1217. ifmsh->csa_role = IEEE80211_MESH_CSA_ROLE_NONE;
  1218. /* Allocate all mesh structures when creating the first mesh interface. */
  1219. if (!mesh_allocated)
  1220. ieee80211s_init();
  1221. mesh_pathtbl_init(sdata);
  1222. timer_setup(&ifmsh->mesh_path_timer, ieee80211_mesh_path_timer, 0);
  1223. timer_setup(&ifmsh->mesh_path_root_timer,
  1224. ieee80211_mesh_path_root_timer, 0);
  1225. INIT_LIST_HEAD(&ifmsh->preq_queue.list);
  1226. skb_queue_head_init(&ifmsh->ps.bc_buf);
  1227. spin_lock_init(&ifmsh->mesh_preq_queue_lock);
  1228. spin_lock_init(&ifmsh->sync_offset_lock);
  1229. RCU_INIT_POINTER(ifmsh->beacon, NULL);
  1230. sdata->vif.bss_conf.bssid = zero_addr;
  1231. }
  1232. void ieee80211_mesh_teardown_sdata(struct ieee80211_sub_if_data *sdata)
  1233. {
  1234. mesh_rmc_free(sdata);
  1235. mesh_pathtbl_unregister(sdata);
  1236. }