mesh.c 39 KB

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