mesh.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078
  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. static int mesh_allocated;
  15. static struct kmem_cache *rm_cache;
  16. bool mesh_action_is_path_sel(struct ieee80211_mgmt *mgmt)
  17. {
  18. return (mgmt->u.action.u.mesh_action.action_code ==
  19. WLAN_MESH_ACTION_HWMP_PATH_SELECTION);
  20. }
  21. void ieee80211s_init(void)
  22. {
  23. mesh_pathtbl_init();
  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. mesh_pathtbl_unregister();
  33. kmem_cache_destroy(rm_cache);
  34. }
  35. static void ieee80211_mesh_housekeeping_timer(unsigned long data)
  36. {
  37. struct ieee80211_sub_if_data *sdata = (void *) data;
  38. struct ieee80211_local *local = sdata->local;
  39. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  40. set_bit(MESH_WORK_HOUSEKEEPING, &ifmsh->wrkq_flags);
  41. ieee80211_queue_work(&local->hw, &sdata->work);
  42. }
  43. /**
  44. * mesh_matches_local - check if the config of a mesh point matches ours
  45. *
  46. * @sdata: local mesh subif
  47. * @ie: information elements of a management frame from the mesh peer
  48. *
  49. * This function checks if the mesh configuration of a mesh point matches the
  50. * local mesh configuration, i.e. if both nodes belong to the same mesh network.
  51. */
  52. bool mesh_matches_local(struct ieee80211_sub_if_data *sdata,
  53. struct ieee802_11_elems *ie)
  54. {
  55. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  56. struct ieee80211_local *local = sdata->local;
  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(local, ie, ieee80211_get_sdata_band(sdata),
  78. &basic_rates);
  79. if (sdata->vif.bss_conf.basic_rates != basic_rates)
  80. return false;
  81. ieee80211_ht_oper_to_chandef(sdata->vif.bss_conf.chandef.chan,
  82. ie->ht_operation, &sta_chan_def);
  83. if (!cfg80211_chandef_compatible(&sdata->vif.bss_conf.chandef,
  84. &sta_chan_def))
  85. return false;
  86. return true;
  87. }
  88. /**
  89. * mesh_peer_accepts_plinks - check if an mp is willing to establish peer links
  90. *
  91. * @ie: information elements of a management frame from the mesh peer
  92. */
  93. bool mesh_peer_accepts_plinks(struct ieee802_11_elems *ie)
  94. {
  95. return (ie->mesh_config->meshconf_cap &
  96. IEEE80211_MESHCONF_CAPAB_ACCEPT_PLINKS) != 0;
  97. }
  98. /**
  99. * mesh_accept_plinks_update - update accepting_plink in local mesh beacons
  100. *
  101. * @sdata: mesh interface in which mesh beacons are going to be updated
  102. *
  103. * Returns: beacon changed flag if the beacon content changed.
  104. */
  105. u32 mesh_accept_plinks_update(struct ieee80211_sub_if_data *sdata)
  106. {
  107. bool free_plinks;
  108. u32 changed = 0;
  109. /* In case mesh_plink_free_count > 0 and mesh_plinktbl_capacity == 0,
  110. * the mesh interface might be able to establish plinks with peers that
  111. * are already on the table but are not on PLINK_ESTAB state. However,
  112. * in general the mesh interface is not accepting peer link requests
  113. * from new peers, and that must be reflected in the beacon
  114. */
  115. free_plinks = mesh_plink_availables(sdata);
  116. if (free_plinks != sdata->u.mesh.accepting_plinks) {
  117. sdata->u.mesh.accepting_plinks = free_plinks;
  118. changed = BSS_CHANGED_BEACON;
  119. }
  120. return changed;
  121. }
  122. /*
  123. * mesh_sta_cleanup - clean up any mesh sta state
  124. *
  125. * @sta: mesh sta to clean up.
  126. */
  127. void mesh_sta_cleanup(struct sta_info *sta)
  128. {
  129. struct ieee80211_sub_if_data *sdata = sta->sdata;
  130. u32 changed;
  131. /*
  132. * maybe userspace handles peer allocation and peering, but in either
  133. * case the beacon is still generated by the kernel and we might need
  134. * an update.
  135. */
  136. changed = mesh_accept_plinks_update(sdata);
  137. if (!sdata->u.mesh.user_mpm) {
  138. changed |= mesh_plink_deactivate(sta);
  139. del_timer_sync(&sta->plink_timer);
  140. }
  141. if (changed)
  142. ieee80211_mbss_info_change_notify(sdata, changed);
  143. }
  144. int mesh_rmc_init(struct ieee80211_sub_if_data *sdata)
  145. {
  146. int i;
  147. sdata->u.mesh.rmc = kmalloc(sizeof(struct mesh_rmc), GFP_KERNEL);
  148. if (!sdata->u.mesh.rmc)
  149. return -ENOMEM;
  150. sdata->u.mesh.rmc->idx_mask = RMC_BUCKETS - 1;
  151. for (i = 0; i < RMC_BUCKETS; i++)
  152. INIT_LIST_HEAD(&sdata->u.mesh.rmc->bucket[i]);
  153. return 0;
  154. }
  155. void mesh_rmc_free(struct ieee80211_sub_if_data *sdata)
  156. {
  157. struct mesh_rmc *rmc = sdata->u.mesh.rmc;
  158. struct rmc_entry *p, *n;
  159. int i;
  160. if (!sdata->u.mesh.rmc)
  161. return;
  162. for (i = 0; i < RMC_BUCKETS; i++) {
  163. list_for_each_entry_safe(p, n, &rmc->bucket[i], list) {
  164. list_del(&p->list);
  165. kmem_cache_free(rm_cache, p);
  166. }
  167. }
  168. kfree(rmc);
  169. sdata->u.mesh.rmc = NULL;
  170. }
  171. /**
  172. * mesh_rmc_check - Check frame in recent multicast cache and add if absent.
  173. *
  174. * @sdata: interface
  175. * @sa: source address
  176. * @mesh_hdr: mesh_header
  177. *
  178. * Returns: 0 if the frame is not in the cache, nonzero otherwise.
  179. *
  180. * Checks using the source address and the mesh sequence number if we have
  181. * received this frame lately. If the frame is not in the cache, it is added to
  182. * it.
  183. */
  184. int mesh_rmc_check(struct ieee80211_sub_if_data *sdata,
  185. const u8 *sa, struct ieee80211s_hdr *mesh_hdr)
  186. {
  187. struct mesh_rmc *rmc = sdata->u.mesh.rmc;
  188. u32 seqnum = 0;
  189. int entries = 0;
  190. u8 idx;
  191. struct rmc_entry *p, *n;
  192. /* Don't care about endianness since only match matters */
  193. memcpy(&seqnum, &mesh_hdr->seqnum, sizeof(mesh_hdr->seqnum));
  194. idx = le32_to_cpu(mesh_hdr->seqnum) & rmc->idx_mask;
  195. list_for_each_entry_safe(p, n, &rmc->bucket[idx], list) {
  196. ++entries;
  197. if (time_after(jiffies, p->exp_time) ||
  198. entries == RMC_QUEUE_MAX_LEN) {
  199. list_del(&p->list);
  200. kmem_cache_free(rm_cache, p);
  201. --entries;
  202. } else if ((seqnum == p->seqnum) && ether_addr_equal(sa, p->sa))
  203. return -1;
  204. }
  205. p = kmem_cache_alloc(rm_cache, GFP_ATOMIC);
  206. if (!p)
  207. return 0;
  208. p->seqnum = seqnum;
  209. p->exp_time = jiffies + RMC_TIMEOUT;
  210. memcpy(p->sa, sa, ETH_ALEN);
  211. list_add(&p->list, &rmc->bucket[idx]);
  212. return 0;
  213. }
  214. int mesh_add_meshconf_ie(struct ieee80211_sub_if_data *sdata,
  215. struct sk_buff *skb)
  216. {
  217. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  218. u8 *pos, neighbors;
  219. u8 meshconf_len = sizeof(struct ieee80211_meshconf_ie);
  220. if (skb_tailroom(skb) < 2 + meshconf_len)
  221. return -ENOMEM;
  222. pos = skb_put(skb, 2 + meshconf_len);
  223. *pos++ = WLAN_EID_MESH_CONFIG;
  224. *pos++ = meshconf_len;
  225. /* Active path selection protocol ID */
  226. *pos++ = ifmsh->mesh_pp_id;
  227. /* Active path selection metric ID */
  228. *pos++ = ifmsh->mesh_pm_id;
  229. /* Congestion control mode identifier */
  230. *pos++ = ifmsh->mesh_cc_id;
  231. /* Synchronization protocol identifier */
  232. *pos++ = ifmsh->mesh_sp_id;
  233. /* Authentication Protocol identifier */
  234. *pos++ = ifmsh->mesh_auth_id;
  235. /* Mesh Formation Info - number of neighbors */
  236. neighbors = atomic_read(&ifmsh->estab_plinks);
  237. neighbors = min_t(int, neighbors, IEEE80211_MAX_MESH_PEERINGS);
  238. *pos++ = neighbors << 1;
  239. /* Mesh capability */
  240. *pos = IEEE80211_MESHCONF_CAPAB_FORWARDING;
  241. *pos |= ifmsh->accepting_plinks ?
  242. IEEE80211_MESHCONF_CAPAB_ACCEPT_PLINKS : 0x00;
  243. /* Mesh PS mode. See IEEE802.11-2012 8.4.2.100.8 */
  244. *pos |= ifmsh->ps_peers_deep_sleep ?
  245. IEEE80211_MESHCONF_CAPAB_POWER_SAVE_LEVEL : 0x00;
  246. *pos++ |= ifmsh->adjusting_tbtt ?
  247. IEEE80211_MESHCONF_CAPAB_TBTT_ADJUSTING : 0x00;
  248. *pos++ = 0x00;
  249. return 0;
  250. }
  251. int mesh_add_meshid_ie(struct ieee80211_sub_if_data *sdata, struct sk_buff *skb)
  252. {
  253. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  254. u8 *pos;
  255. if (skb_tailroom(skb) < 2 + ifmsh->mesh_id_len)
  256. return -ENOMEM;
  257. pos = skb_put(skb, 2 + ifmsh->mesh_id_len);
  258. *pos++ = WLAN_EID_MESH_ID;
  259. *pos++ = ifmsh->mesh_id_len;
  260. if (ifmsh->mesh_id_len)
  261. memcpy(pos, ifmsh->mesh_id, ifmsh->mesh_id_len);
  262. return 0;
  263. }
  264. static int mesh_add_awake_window_ie(struct ieee80211_sub_if_data *sdata,
  265. struct sk_buff *skb)
  266. {
  267. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  268. u8 *pos;
  269. /* see IEEE802.11-2012 13.14.6 */
  270. if (ifmsh->ps_peers_light_sleep == 0 &&
  271. ifmsh->ps_peers_deep_sleep == 0 &&
  272. ifmsh->nonpeer_pm == NL80211_MESH_POWER_ACTIVE)
  273. return 0;
  274. if (skb_tailroom(skb) < 4)
  275. return -ENOMEM;
  276. pos = skb_put(skb, 2 + 2);
  277. *pos++ = WLAN_EID_MESH_AWAKE_WINDOW;
  278. *pos++ = 2;
  279. put_unaligned_le16(ifmsh->mshcfg.dot11MeshAwakeWindowDuration, pos);
  280. return 0;
  281. }
  282. int mesh_add_vendor_ies(struct ieee80211_sub_if_data *sdata,
  283. struct sk_buff *skb)
  284. {
  285. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  286. u8 offset, len;
  287. const u8 *data;
  288. if (!ifmsh->ie || !ifmsh->ie_len)
  289. return 0;
  290. /* fast-forward to vendor IEs */
  291. offset = ieee80211_ie_split_vendor(ifmsh->ie, ifmsh->ie_len, 0);
  292. if (offset) {
  293. len = ifmsh->ie_len - offset;
  294. data = ifmsh->ie + offset;
  295. if (skb_tailroom(skb) < len)
  296. return -ENOMEM;
  297. memcpy(skb_put(skb, len), data, len);
  298. }
  299. return 0;
  300. }
  301. int mesh_add_rsn_ie(struct ieee80211_sub_if_data *sdata, struct sk_buff *skb)
  302. {
  303. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  304. u8 len = 0;
  305. const u8 *data;
  306. if (!ifmsh->ie || !ifmsh->ie_len)
  307. return 0;
  308. /* find RSN IE */
  309. data = ifmsh->ie;
  310. while (data < ifmsh->ie + ifmsh->ie_len) {
  311. if (*data == WLAN_EID_RSN) {
  312. len = data[1] + 2;
  313. break;
  314. }
  315. data++;
  316. }
  317. if (len) {
  318. if (skb_tailroom(skb) < len)
  319. return -ENOMEM;
  320. memcpy(skb_put(skb, len), data, len);
  321. }
  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. enum nl80211_channel_type channel_type =
  372. cfg80211_get_chandef_type(&sdata->vif.bss_conf.chandef);
  373. struct ieee80211_supported_band *sband;
  374. struct ieee80211_sta_ht_cap *ht_cap;
  375. u8 *pos;
  376. rcu_read_lock();
  377. chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
  378. if (WARN_ON(!chanctx_conf)) {
  379. rcu_read_unlock();
  380. return -EINVAL;
  381. }
  382. channel = chanctx_conf->def.chan;
  383. rcu_read_unlock();
  384. sband = local->hw.wiphy->bands[channel->band];
  385. ht_cap = &sband->ht_cap;
  386. if (!ht_cap->ht_supported || channel_type == NL80211_CHAN_NO_HT)
  387. return 0;
  388. if (skb_tailroom(skb) < 2 + sizeof(struct ieee80211_ht_operation))
  389. return -ENOMEM;
  390. pos = skb_put(skb, 2 + sizeof(struct ieee80211_ht_operation));
  391. ieee80211_ie_build_ht_oper(pos, ht_cap, &sdata->vif.bss_conf.chandef,
  392. sdata->vif.bss_conf.ht_operation_mode);
  393. return 0;
  394. }
  395. static void ieee80211_mesh_path_timer(unsigned long data)
  396. {
  397. struct ieee80211_sub_if_data *sdata =
  398. (struct ieee80211_sub_if_data *) data;
  399. ieee80211_queue_work(&sdata->local->hw, &sdata->work);
  400. }
  401. static void ieee80211_mesh_path_root_timer(unsigned long data)
  402. {
  403. struct ieee80211_sub_if_data *sdata =
  404. (struct ieee80211_sub_if_data *) data;
  405. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  406. set_bit(MESH_WORK_ROOT, &ifmsh->wrkq_flags);
  407. ieee80211_queue_work(&sdata->local->hw, &sdata->work);
  408. }
  409. void ieee80211_mesh_root_setup(struct ieee80211_if_mesh *ifmsh)
  410. {
  411. if (ifmsh->mshcfg.dot11MeshHWMPRootMode > IEEE80211_ROOTMODE_ROOT)
  412. set_bit(MESH_WORK_ROOT, &ifmsh->wrkq_flags);
  413. else {
  414. clear_bit(MESH_WORK_ROOT, &ifmsh->wrkq_flags);
  415. /* stop running timer */
  416. del_timer_sync(&ifmsh->mesh_path_root_timer);
  417. }
  418. }
  419. /**
  420. * ieee80211_fill_mesh_addresses - fill addresses of a locally originated mesh frame
  421. * @hdr: 802.11 frame header
  422. * @fc: frame control field
  423. * @meshda: destination address in the mesh
  424. * @meshsa: source address address in the mesh. Same as TA, as frame is
  425. * locally originated.
  426. *
  427. * Return the length of the 802.11 (does not include a mesh control header)
  428. */
  429. int ieee80211_fill_mesh_addresses(struct ieee80211_hdr *hdr, __le16 *fc,
  430. const u8 *meshda, const u8 *meshsa)
  431. {
  432. if (is_multicast_ether_addr(meshda)) {
  433. *fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS);
  434. /* DA TA SA */
  435. memcpy(hdr->addr1, meshda, ETH_ALEN);
  436. memcpy(hdr->addr2, meshsa, ETH_ALEN);
  437. memcpy(hdr->addr3, meshsa, ETH_ALEN);
  438. return 24;
  439. } else {
  440. *fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS);
  441. /* RA TA DA SA */
  442. memset(hdr->addr1, 0, ETH_ALEN); /* RA is resolved later */
  443. memcpy(hdr->addr2, meshsa, ETH_ALEN);
  444. memcpy(hdr->addr3, meshda, ETH_ALEN);
  445. memcpy(hdr->addr4, meshsa, ETH_ALEN);
  446. return 30;
  447. }
  448. }
  449. /**
  450. * ieee80211_new_mesh_header - create a new mesh header
  451. * @sdata: mesh interface to be used
  452. * @meshhdr: uninitialized mesh header
  453. * @addr4or5: 1st address in the ae header, which may correspond to address 4
  454. * (if addr6 is NULL) or address 5 (if addr6 is present). It may
  455. * be NULL.
  456. * @addr6: 2nd address in the ae header, which corresponds to addr6 of the
  457. * mesh frame
  458. *
  459. * Return the header length.
  460. */
  461. int ieee80211_new_mesh_header(struct ieee80211_sub_if_data *sdata,
  462. struct ieee80211s_hdr *meshhdr,
  463. const char *addr4or5, const char *addr6)
  464. {
  465. if (WARN_ON(!addr4or5 && addr6))
  466. return 0;
  467. memset(meshhdr, 0, sizeof(*meshhdr));
  468. meshhdr->ttl = sdata->u.mesh.mshcfg.dot11MeshTTL;
  469. /* FIXME: racy -- TX on multiple queues can be concurrent */
  470. put_unaligned(cpu_to_le32(sdata->u.mesh.mesh_seqnum), &meshhdr->seqnum);
  471. sdata->u.mesh.mesh_seqnum++;
  472. if (addr4or5 && !addr6) {
  473. meshhdr->flags |= MESH_FLAGS_AE_A4;
  474. memcpy(meshhdr->eaddr1, addr4or5, ETH_ALEN);
  475. return 2 * ETH_ALEN;
  476. } else if (addr4or5 && addr6) {
  477. meshhdr->flags |= MESH_FLAGS_AE_A5_A6;
  478. memcpy(meshhdr->eaddr1, addr4or5, ETH_ALEN);
  479. memcpy(meshhdr->eaddr2, addr6, ETH_ALEN);
  480. return 3 * ETH_ALEN;
  481. }
  482. return ETH_ALEN;
  483. }
  484. static void ieee80211_mesh_housekeeping(struct ieee80211_sub_if_data *sdata)
  485. {
  486. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  487. u32 changed;
  488. ieee80211_sta_expire(sdata, ifmsh->mshcfg.plink_timeout * HZ);
  489. mesh_path_expire(sdata);
  490. changed = mesh_accept_plinks_update(sdata);
  491. ieee80211_mbss_info_change_notify(sdata, changed);
  492. mod_timer(&ifmsh->housekeeping_timer,
  493. round_jiffies(jiffies +
  494. IEEE80211_MESH_HOUSEKEEPING_INTERVAL));
  495. }
  496. static void ieee80211_mesh_rootpath(struct ieee80211_sub_if_data *sdata)
  497. {
  498. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  499. u32 interval;
  500. mesh_path_tx_root_frame(sdata);
  501. if (ifmsh->mshcfg.dot11MeshHWMPRootMode == IEEE80211_PROACTIVE_RANN)
  502. interval = ifmsh->mshcfg.dot11MeshHWMPRannInterval;
  503. else
  504. interval = ifmsh->mshcfg.dot11MeshHWMProotInterval;
  505. mod_timer(&ifmsh->mesh_path_root_timer,
  506. round_jiffies(TU_TO_EXP_TIME(interval)));
  507. }
  508. static int
  509. ieee80211_mesh_build_beacon(struct ieee80211_if_mesh *ifmsh)
  510. {
  511. struct beacon_data *bcn;
  512. int head_len, tail_len;
  513. struct sk_buff *skb;
  514. struct ieee80211_mgmt *mgmt;
  515. struct ieee80211_chanctx_conf *chanctx_conf;
  516. enum ieee80211_band band;
  517. u8 *pos;
  518. struct ieee80211_sub_if_data *sdata;
  519. int hdr_len = offsetof(struct ieee80211_mgmt, u.beacon) +
  520. sizeof(mgmt->u.beacon);
  521. sdata = container_of(ifmsh, struct ieee80211_sub_if_data, u.mesh);
  522. rcu_read_lock();
  523. chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
  524. band = chanctx_conf->def.chan->band;
  525. rcu_read_unlock();
  526. head_len = hdr_len +
  527. 2 + /* NULL SSID */
  528. 2 + 8 + /* supported rates */
  529. 2 + 3; /* DS params */
  530. tail_len = 2 + (IEEE80211_MAX_SUPP_RATES - 8) +
  531. 2 + sizeof(struct ieee80211_ht_cap) +
  532. 2 + sizeof(struct ieee80211_ht_operation) +
  533. 2 + ifmsh->mesh_id_len +
  534. 2 + sizeof(struct ieee80211_meshconf_ie) +
  535. 2 + sizeof(__le16) + /* awake window */
  536. ifmsh->ie_len;
  537. bcn = kzalloc(sizeof(*bcn) + head_len + tail_len, GFP_KERNEL);
  538. /* need an skb for IE builders to operate on */
  539. skb = dev_alloc_skb(max(head_len, tail_len));
  540. if (!bcn || !skb)
  541. goto out_free;
  542. /*
  543. * pointers go into the block we allocated,
  544. * memory is | beacon_data | head | tail |
  545. */
  546. bcn->head = ((u8 *) bcn) + sizeof(*bcn);
  547. /* fill in the head */
  548. mgmt = (struct ieee80211_mgmt *) skb_put(skb, hdr_len);
  549. memset(mgmt, 0, hdr_len);
  550. mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
  551. IEEE80211_STYPE_BEACON);
  552. eth_broadcast_addr(mgmt->da);
  553. memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
  554. memcpy(mgmt->bssid, sdata->vif.addr, ETH_ALEN);
  555. ieee80211_mps_set_frame_flags(sdata, NULL, (void *) mgmt);
  556. mgmt->u.beacon.beacon_int =
  557. cpu_to_le16(sdata->vif.bss_conf.beacon_int);
  558. mgmt->u.beacon.capab_info |= cpu_to_le16(
  559. sdata->u.mesh.security ? WLAN_CAPABILITY_PRIVACY : 0);
  560. pos = skb_put(skb, 2);
  561. *pos++ = WLAN_EID_SSID;
  562. *pos++ = 0x0;
  563. if (ieee80211_add_srates_ie(sdata, skb, true, band) ||
  564. mesh_add_ds_params_ie(sdata, skb))
  565. goto out_free;
  566. bcn->head_len = skb->len;
  567. memcpy(bcn->head, skb->data, bcn->head_len);
  568. /* now the tail */
  569. skb_trim(skb, 0);
  570. bcn->tail = bcn->head + bcn->head_len;
  571. if (ieee80211_add_ext_srates_ie(sdata, skb, true, band) ||
  572. mesh_add_rsn_ie(sdata, skb) ||
  573. mesh_add_ht_cap_ie(sdata, skb) ||
  574. mesh_add_ht_oper_ie(sdata, skb) ||
  575. mesh_add_meshid_ie(sdata, skb) ||
  576. mesh_add_meshconf_ie(sdata, skb) ||
  577. mesh_add_awake_window_ie(sdata, skb) ||
  578. mesh_add_vendor_ies(sdata, skb))
  579. goto out_free;
  580. bcn->tail_len = skb->len;
  581. memcpy(bcn->tail, skb->data, bcn->tail_len);
  582. dev_kfree_skb(skb);
  583. rcu_assign_pointer(ifmsh->beacon, bcn);
  584. return 0;
  585. out_free:
  586. kfree(bcn);
  587. dev_kfree_skb(skb);
  588. return -ENOMEM;
  589. }
  590. static int
  591. ieee80211_mesh_rebuild_beacon(struct ieee80211_sub_if_data *sdata)
  592. {
  593. struct beacon_data *old_bcn;
  594. int ret;
  595. old_bcn = rcu_dereference_protected(sdata->u.mesh.beacon,
  596. lockdep_is_held(&sdata->wdev.mtx));
  597. ret = ieee80211_mesh_build_beacon(&sdata->u.mesh);
  598. if (ret)
  599. /* just reuse old beacon */
  600. return ret;
  601. if (old_bcn)
  602. kfree_rcu(old_bcn, rcu_head);
  603. return 0;
  604. }
  605. void ieee80211_mbss_info_change_notify(struct ieee80211_sub_if_data *sdata,
  606. u32 changed)
  607. {
  608. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  609. unsigned long bits = changed;
  610. u32 bit;
  611. if (!bits)
  612. return;
  613. /* if we race with running work, worst case this work becomes a noop */
  614. for_each_set_bit(bit, &bits, sizeof(changed) * BITS_PER_BYTE)
  615. set_bit(bit, &ifmsh->mbss_changed);
  616. set_bit(MESH_WORK_MBSS_CHANGED, &ifmsh->wrkq_flags);
  617. ieee80211_queue_work(&sdata->local->hw, &sdata->work);
  618. }
  619. int ieee80211_start_mesh(struct ieee80211_sub_if_data *sdata)
  620. {
  621. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  622. struct ieee80211_local *local = sdata->local;
  623. u32 changed = BSS_CHANGED_BEACON |
  624. BSS_CHANGED_BEACON_ENABLED |
  625. BSS_CHANGED_HT |
  626. BSS_CHANGED_BASIC_RATES |
  627. BSS_CHANGED_BEACON_INT;
  628. local->fif_other_bss++;
  629. /* mesh ifaces must set allmulti to forward mcast traffic */
  630. atomic_inc(&local->iff_allmultis);
  631. ieee80211_configure_filter(local);
  632. ifmsh->mesh_cc_id = 0; /* Disabled */
  633. /* register sync ops from extensible synchronization framework */
  634. ifmsh->sync_ops = ieee80211_mesh_sync_ops_get(ifmsh->mesh_sp_id);
  635. ifmsh->adjusting_tbtt = false;
  636. ifmsh->sync_offset_clockdrift_max = 0;
  637. set_bit(MESH_WORK_HOUSEKEEPING, &ifmsh->wrkq_flags);
  638. ieee80211_mesh_root_setup(ifmsh);
  639. ieee80211_queue_work(&local->hw, &sdata->work);
  640. sdata->vif.bss_conf.ht_operation_mode =
  641. ifmsh->mshcfg.ht_opmode;
  642. sdata->vif.bss_conf.enable_beacon = true;
  643. changed |= ieee80211_mps_local_status_update(sdata);
  644. if (ieee80211_mesh_build_beacon(ifmsh)) {
  645. ieee80211_stop_mesh(sdata);
  646. return -ENOMEM;
  647. }
  648. ieee80211_bss_info_change_notify(sdata, changed);
  649. netif_carrier_on(sdata->dev);
  650. return 0;
  651. }
  652. void ieee80211_stop_mesh(struct ieee80211_sub_if_data *sdata)
  653. {
  654. struct ieee80211_local *local = sdata->local;
  655. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  656. struct beacon_data *bcn;
  657. netif_carrier_off(sdata->dev);
  658. /* stop the beacon */
  659. ifmsh->mesh_id_len = 0;
  660. sdata->vif.bss_conf.enable_beacon = false;
  661. clear_bit(SDATA_STATE_OFFCHANNEL_BEACON_STOPPED, &sdata->state);
  662. ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BEACON_ENABLED);
  663. bcn = rcu_dereference_protected(ifmsh->beacon,
  664. lockdep_is_held(&sdata->wdev.mtx));
  665. rcu_assign_pointer(ifmsh->beacon, NULL);
  666. kfree_rcu(bcn, rcu_head);
  667. /* flush STAs and mpaths on this iface */
  668. sta_info_flush(sdata);
  669. mesh_path_flush_by_iface(sdata);
  670. /* free all potentially still buffered group-addressed frames */
  671. local->total_ps_buffered -= skb_queue_len(&ifmsh->ps.bc_buf);
  672. skb_queue_purge(&ifmsh->ps.bc_buf);
  673. del_timer_sync(&sdata->u.mesh.housekeeping_timer);
  674. del_timer_sync(&sdata->u.mesh.mesh_path_root_timer);
  675. del_timer_sync(&sdata->u.mesh.mesh_path_timer);
  676. /* clear any mesh work (for next join) we may have accrued */
  677. ifmsh->wrkq_flags = 0;
  678. ifmsh->mbss_changed = 0;
  679. local->fif_other_bss--;
  680. atomic_dec(&local->iff_allmultis);
  681. ieee80211_configure_filter(local);
  682. }
  683. static void
  684. ieee80211_mesh_rx_probe_req(struct ieee80211_sub_if_data *sdata,
  685. struct ieee80211_mgmt *mgmt, size_t len)
  686. {
  687. struct ieee80211_local *local = sdata->local;
  688. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  689. struct sk_buff *presp;
  690. struct beacon_data *bcn;
  691. struct ieee80211_mgmt *hdr;
  692. struct ieee802_11_elems elems;
  693. size_t baselen;
  694. u8 *pos;
  695. pos = mgmt->u.probe_req.variable;
  696. baselen = (u8 *) pos - (u8 *) mgmt;
  697. if (baselen > len)
  698. return;
  699. ieee802_11_parse_elems(pos, len - baselen, false, &elems);
  700. /* 802.11-2012 10.1.4.3.2 */
  701. if ((!ether_addr_equal(mgmt->da, sdata->vif.addr) &&
  702. !is_broadcast_ether_addr(mgmt->da)) ||
  703. elems.ssid_len != 0)
  704. return;
  705. if (elems.mesh_id_len != 0 &&
  706. (elems.mesh_id_len != ifmsh->mesh_id_len ||
  707. memcmp(elems.mesh_id, ifmsh->mesh_id, ifmsh->mesh_id_len)))
  708. return;
  709. rcu_read_lock();
  710. bcn = rcu_dereference(ifmsh->beacon);
  711. if (!bcn)
  712. goto out;
  713. presp = dev_alloc_skb(local->tx_headroom +
  714. bcn->head_len + bcn->tail_len);
  715. if (!presp)
  716. goto out;
  717. skb_reserve(presp, local->tx_headroom);
  718. memcpy(skb_put(presp, bcn->head_len), bcn->head, bcn->head_len);
  719. memcpy(skb_put(presp, bcn->tail_len), bcn->tail, bcn->tail_len);
  720. hdr = (struct ieee80211_mgmt *) presp->data;
  721. hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
  722. IEEE80211_STYPE_PROBE_RESP);
  723. memcpy(hdr->da, mgmt->sa, ETH_ALEN);
  724. IEEE80211_SKB_CB(presp)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
  725. ieee80211_tx_skb(sdata, presp);
  726. out:
  727. rcu_read_unlock();
  728. }
  729. static void ieee80211_mesh_rx_bcn_presp(struct ieee80211_sub_if_data *sdata,
  730. u16 stype,
  731. struct ieee80211_mgmt *mgmt,
  732. size_t len,
  733. struct ieee80211_rx_status *rx_status)
  734. {
  735. struct ieee80211_local *local = sdata->local;
  736. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  737. struct ieee802_11_elems elems;
  738. struct ieee80211_channel *channel;
  739. size_t baselen;
  740. int freq;
  741. enum ieee80211_band band = rx_status->band;
  742. /* ignore ProbeResp to foreign address */
  743. if (stype == IEEE80211_STYPE_PROBE_RESP &&
  744. !ether_addr_equal(mgmt->da, sdata->vif.addr))
  745. return;
  746. baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt;
  747. if (baselen > len)
  748. return;
  749. ieee802_11_parse_elems(mgmt->u.probe_resp.variable, len - baselen,
  750. false, &elems);
  751. /* ignore non-mesh or secure / unsecure mismatch */
  752. if ((!elems.mesh_id || !elems.mesh_config) ||
  753. (elems.rsn && sdata->u.mesh.security == IEEE80211_MESH_SEC_NONE) ||
  754. (!elems.rsn && sdata->u.mesh.security != IEEE80211_MESH_SEC_NONE))
  755. return;
  756. if (elems.ds_params)
  757. freq = ieee80211_channel_to_frequency(elems.ds_params[0], band);
  758. else
  759. freq = rx_status->freq;
  760. channel = ieee80211_get_channel(local->hw.wiphy, freq);
  761. if (!channel || channel->flags & IEEE80211_CHAN_DISABLED)
  762. return;
  763. if (mesh_matches_local(sdata, &elems))
  764. mesh_neighbour_update(sdata, mgmt->sa, &elems);
  765. if (ifmsh->sync_ops)
  766. ifmsh->sync_ops->rx_bcn_presp(sdata,
  767. stype, mgmt, &elems, rx_status);
  768. }
  769. static void ieee80211_mesh_rx_mgmt_action(struct ieee80211_sub_if_data *sdata,
  770. struct ieee80211_mgmt *mgmt,
  771. size_t len,
  772. struct ieee80211_rx_status *rx_status)
  773. {
  774. switch (mgmt->u.action.category) {
  775. case WLAN_CATEGORY_SELF_PROTECTED:
  776. switch (mgmt->u.action.u.self_prot.action_code) {
  777. case WLAN_SP_MESH_PEERING_OPEN:
  778. case WLAN_SP_MESH_PEERING_CLOSE:
  779. case WLAN_SP_MESH_PEERING_CONFIRM:
  780. mesh_rx_plink_frame(sdata, mgmt, len, rx_status);
  781. break;
  782. }
  783. break;
  784. case WLAN_CATEGORY_MESH_ACTION:
  785. if (mesh_action_is_path_sel(mgmt))
  786. mesh_rx_path_sel_frame(sdata, mgmt, len);
  787. break;
  788. }
  789. }
  790. void ieee80211_mesh_rx_queued_mgmt(struct ieee80211_sub_if_data *sdata,
  791. struct sk_buff *skb)
  792. {
  793. struct ieee80211_rx_status *rx_status;
  794. struct ieee80211_mgmt *mgmt;
  795. u16 stype;
  796. sdata_lock(sdata);
  797. /* mesh already went down */
  798. if (!sdata->wdev.mesh_id_len)
  799. goto out;
  800. rx_status = IEEE80211_SKB_RXCB(skb);
  801. mgmt = (struct ieee80211_mgmt *) skb->data;
  802. stype = le16_to_cpu(mgmt->frame_control) & IEEE80211_FCTL_STYPE;
  803. switch (stype) {
  804. case IEEE80211_STYPE_PROBE_RESP:
  805. case IEEE80211_STYPE_BEACON:
  806. ieee80211_mesh_rx_bcn_presp(sdata, stype, mgmt, skb->len,
  807. rx_status);
  808. break;
  809. case IEEE80211_STYPE_PROBE_REQ:
  810. ieee80211_mesh_rx_probe_req(sdata, mgmt, skb->len);
  811. break;
  812. case IEEE80211_STYPE_ACTION:
  813. ieee80211_mesh_rx_mgmt_action(sdata, mgmt, skb->len, rx_status);
  814. break;
  815. }
  816. out:
  817. sdata_unlock(sdata);
  818. }
  819. static void mesh_bss_info_changed(struct ieee80211_sub_if_data *sdata)
  820. {
  821. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  822. u32 bit, changed = 0;
  823. for_each_set_bit(bit, &ifmsh->mbss_changed,
  824. sizeof(changed) * BITS_PER_BYTE) {
  825. clear_bit(bit, &ifmsh->mbss_changed);
  826. changed |= BIT(bit);
  827. }
  828. if (sdata->vif.bss_conf.enable_beacon &&
  829. (changed & (BSS_CHANGED_BEACON |
  830. BSS_CHANGED_HT |
  831. BSS_CHANGED_BASIC_RATES |
  832. BSS_CHANGED_BEACON_INT)))
  833. if (ieee80211_mesh_rebuild_beacon(sdata))
  834. return;
  835. ieee80211_bss_info_change_notify(sdata, changed);
  836. }
  837. void ieee80211_mesh_work(struct ieee80211_sub_if_data *sdata)
  838. {
  839. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  840. sdata_lock(sdata);
  841. /* mesh already went down */
  842. if (!sdata->wdev.mesh_id_len)
  843. goto out;
  844. if (ifmsh->preq_queue_len &&
  845. time_after(jiffies,
  846. ifmsh->last_preq + msecs_to_jiffies(ifmsh->mshcfg.dot11MeshHWMPpreqMinInterval)))
  847. mesh_path_start_discovery(sdata);
  848. if (test_and_clear_bit(MESH_WORK_GROW_MPATH_TABLE, &ifmsh->wrkq_flags))
  849. mesh_mpath_table_grow();
  850. if (test_and_clear_bit(MESH_WORK_GROW_MPP_TABLE, &ifmsh->wrkq_flags))
  851. mesh_mpp_table_grow();
  852. if (test_and_clear_bit(MESH_WORK_HOUSEKEEPING, &ifmsh->wrkq_flags))
  853. ieee80211_mesh_housekeeping(sdata);
  854. if (test_and_clear_bit(MESH_WORK_ROOT, &ifmsh->wrkq_flags))
  855. ieee80211_mesh_rootpath(sdata);
  856. if (test_and_clear_bit(MESH_WORK_DRIFT_ADJUST, &ifmsh->wrkq_flags))
  857. mesh_sync_adjust_tbtt(sdata);
  858. if (test_and_clear_bit(MESH_WORK_MBSS_CHANGED, &ifmsh->wrkq_flags))
  859. mesh_bss_info_changed(sdata);
  860. out:
  861. sdata_unlock(sdata);
  862. }
  863. void ieee80211_mesh_notify_scan_completed(struct ieee80211_local *local)
  864. {
  865. struct ieee80211_sub_if_data *sdata;
  866. rcu_read_lock();
  867. list_for_each_entry_rcu(sdata, &local->interfaces, list)
  868. if (ieee80211_vif_is_mesh(&sdata->vif) &&
  869. ieee80211_sdata_running(sdata))
  870. ieee80211_queue_work(&local->hw, &sdata->work);
  871. rcu_read_unlock();
  872. }
  873. void ieee80211_mesh_init_sdata(struct ieee80211_sub_if_data *sdata)
  874. {
  875. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  876. static u8 zero_addr[ETH_ALEN] = {};
  877. setup_timer(&ifmsh->housekeeping_timer,
  878. ieee80211_mesh_housekeeping_timer,
  879. (unsigned long) sdata);
  880. ifmsh->accepting_plinks = true;
  881. ifmsh->preq_id = 0;
  882. ifmsh->sn = 0;
  883. ifmsh->num_gates = 0;
  884. atomic_set(&ifmsh->mpaths, 0);
  885. mesh_rmc_init(sdata);
  886. ifmsh->last_preq = jiffies;
  887. ifmsh->next_perr = jiffies;
  888. /* Allocate all mesh structures when creating the first mesh interface. */
  889. if (!mesh_allocated)
  890. ieee80211s_init();
  891. setup_timer(&ifmsh->mesh_path_timer,
  892. ieee80211_mesh_path_timer,
  893. (unsigned long) sdata);
  894. setup_timer(&ifmsh->mesh_path_root_timer,
  895. ieee80211_mesh_path_root_timer,
  896. (unsigned long) sdata);
  897. INIT_LIST_HEAD(&ifmsh->preq_queue.list);
  898. skb_queue_head_init(&ifmsh->ps.bc_buf);
  899. spin_lock_init(&ifmsh->mesh_preq_queue_lock);
  900. spin_lock_init(&ifmsh->sync_offset_lock);
  901. RCU_INIT_POINTER(ifmsh->beacon, NULL);
  902. sdata->vif.bss_conf.bssid = zero_addr;
  903. }