mesh.c 38 KB

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