mesh.c 29 KB

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