mesh.c 41 KB

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