mesh.c 50 KB

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