mesh.c 38 KB

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