mesh.c 39 KB

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