mesh.c 37 KB

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