mesh.c 39 KB

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