mesh.c 50 KB

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