mesh.c 41 KB

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