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