mesh.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082
  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. static int mesh_allocated;
  15. static struct kmem_cache *rm_cache;
  16. bool mesh_action_is_path_sel(struct ieee80211_mgmt *mgmt)
  17. {
  18. return (mgmt->u.action.u.mesh_action.action_code ==
  19. WLAN_MESH_ACTION_HWMP_PATH_SELECTION);
  20. }
  21. void ieee80211s_init(void)
  22. {
  23. mesh_pathtbl_init();
  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. mesh_pathtbl_unregister();
  33. kmem_cache_destroy(rm_cache);
  34. }
  35. static void ieee80211_mesh_housekeeping_timer(unsigned long data)
  36. {
  37. struct ieee80211_sub_if_data *sdata = (void *) data;
  38. struct ieee80211_local *local = sdata->local;
  39. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  40. set_bit(MESH_WORK_HOUSEKEEPING, &ifmsh->wrkq_flags);
  41. ieee80211_queue_work(&local->hw, &sdata->work);
  42. }
  43. /**
  44. * mesh_matches_local - check if the config of a mesh point matches ours
  45. *
  46. * @sdata: local mesh subif
  47. * @ie: information elements of a management frame from the mesh peer
  48. *
  49. * This function checks if the mesh configuration of a mesh point matches the
  50. * local mesh configuration, i.e. if both nodes belong to the same mesh network.
  51. */
  52. bool mesh_matches_local(struct ieee80211_sub_if_data *sdata,
  53. struct ieee802_11_elems *ie)
  54. {
  55. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  56. u32 basic_rates = 0;
  57. struct cfg80211_chan_def sta_chan_def;
  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. ieee80211_sta_get_rates(sdata, ie, ieee80211_get_sdata_band(sdata),
  77. &basic_rates);
  78. if (sdata->vif.bss_conf.basic_rates != basic_rates)
  79. return false;
  80. ieee80211_ht_oper_to_chandef(sdata->vif.bss_conf.chandef.chan,
  81. ie->ht_operation, &sta_chan_def);
  82. if (!cfg80211_chandef_compatible(&sdata->vif.bss_conf.chandef,
  83. &sta_chan_def))
  84. return false;
  85. return true;
  86. }
  87. /**
  88. * mesh_peer_accepts_plinks - check if an mp is willing to establish peer links
  89. *
  90. * @ie: information elements of a management frame from the mesh peer
  91. */
  92. bool mesh_peer_accepts_plinks(struct ieee802_11_elems *ie)
  93. {
  94. return (ie->mesh_config->meshconf_cap &
  95. IEEE80211_MESHCONF_CAPAB_ACCEPT_PLINKS) != 0;
  96. }
  97. /**
  98. * mesh_accept_plinks_update - update accepting_plink in local mesh beacons
  99. *
  100. * @sdata: mesh interface in which mesh beacons are going to be updated
  101. *
  102. * Returns: beacon changed flag if the beacon content changed.
  103. */
  104. u32 mesh_accept_plinks_update(struct ieee80211_sub_if_data *sdata)
  105. {
  106. bool free_plinks;
  107. u32 changed = 0;
  108. /* In case mesh_plink_free_count > 0 and mesh_plinktbl_capacity == 0,
  109. * the mesh interface might be able to establish plinks with peers that
  110. * are already on the table but are not on PLINK_ESTAB state. However,
  111. * in general the mesh interface is not accepting peer link requests
  112. * from new peers, and that must be reflected in the beacon
  113. */
  114. free_plinks = mesh_plink_availables(sdata);
  115. if (free_plinks != sdata->u.mesh.accepting_plinks) {
  116. sdata->u.mesh.accepting_plinks = free_plinks;
  117. changed = BSS_CHANGED_BEACON;
  118. }
  119. return changed;
  120. }
  121. /*
  122. * mesh_sta_cleanup - clean up any mesh sta state
  123. *
  124. * @sta: mesh sta to clean up.
  125. */
  126. void mesh_sta_cleanup(struct sta_info *sta)
  127. {
  128. struct ieee80211_sub_if_data *sdata = sta->sdata;
  129. u32 changed;
  130. /*
  131. * maybe userspace handles peer allocation and peering, but in either
  132. * case the beacon is still generated by the kernel and we might need
  133. * an update.
  134. */
  135. changed = mesh_accept_plinks_update(sdata);
  136. if (!sdata->u.mesh.user_mpm) {
  137. changed |= mesh_plink_deactivate(sta);
  138. del_timer_sync(&sta->plink_timer);
  139. }
  140. if (changed)
  141. ieee80211_mbss_info_change_notify(sdata, changed);
  142. }
  143. int mesh_rmc_init(struct ieee80211_sub_if_data *sdata)
  144. {
  145. int i;
  146. sdata->u.mesh.rmc = kmalloc(sizeof(struct mesh_rmc), GFP_KERNEL);
  147. if (!sdata->u.mesh.rmc)
  148. return -ENOMEM;
  149. sdata->u.mesh.rmc->idx_mask = RMC_BUCKETS - 1;
  150. for (i = 0; i < RMC_BUCKETS; i++)
  151. INIT_LIST_HEAD(&sdata->u.mesh.rmc->bucket[i]);
  152. return 0;
  153. }
  154. void mesh_rmc_free(struct ieee80211_sub_if_data *sdata)
  155. {
  156. struct mesh_rmc *rmc = sdata->u.mesh.rmc;
  157. struct rmc_entry *p, *n;
  158. int i;
  159. if (!sdata->u.mesh.rmc)
  160. return;
  161. for (i = 0; i < RMC_BUCKETS; i++) {
  162. list_for_each_entry_safe(p, n, &rmc->bucket[i], list) {
  163. list_del(&p->list);
  164. kmem_cache_free(rm_cache, p);
  165. }
  166. }
  167. kfree(rmc);
  168. sdata->u.mesh.rmc = NULL;
  169. }
  170. /**
  171. * mesh_rmc_check - Check frame in recent multicast cache and add if absent.
  172. *
  173. * @sdata: interface
  174. * @sa: source address
  175. * @mesh_hdr: mesh_header
  176. *
  177. * Returns: 0 if the frame is not in the cache, nonzero otherwise.
  178. *
  179. * Checks using the source address and the mesh sequence number if we have
  180. * received this frame lately. If the frame is not in the cache, it is added to
  181. * it.
  182. */
  183. int mesh_rmc_check(struct ieee80211_sub_if_data *sdata,
  184. const u8 *sa, struct ieee80211s_hdr *mesh_hdr)
  185. {
  186. struct mesh_rmc *rmc = sdata->u.mesh.rmc;
  187. u32 seqnum = 0;
  188. int entries = 0;
  189. u8 idx;
  190. struct rmc_entry *p, *n;
  191. /* Don't care about endianness since only match matters */
  192. memcpy(&seqnum, &mesh_hdr->seqnum, sizeof(mesh_hdr->seqnum));
  193. idx = le32_to_cpu(mesh_hdr->seqnum) & rmc->idx_mask;
  194. list_for_each_entry_safe(p, n, &rmc->bucket[idx], list) {
  195. ++entries;
  196. if (time_after(jiffies, p->exp_time) ||
  197. entries == RMC_QUEUE_MAX_LEN) {
  198. list_del(&p->list);
  199. kmem_cache_free(rm_cache, p);
  200. --entries;
  201. } else if ((seqnum == p->seqnum) && ether_addr_equal(sa, p->sa))
  202. return -1;
  203. }
  204. p = kmem_cache_alloc(rm_cache, GFP_ATOMIC);
  205. if (!p)
  206. return 0;
  207. p->seqnum = seqnum;
  208. p->exp_time = jiffies + RMC_TIMEOUT;
  209. memcpy(p->sa, sa, ETH_ALEN);
  210. list_add(&p->list, &rmc->bucket[idx]);
  211. return 0;
  212. }
  213. int mesh_add_meshconf_ie(struct ieee80211_sub_if_data *sdata,
  214. struct sk_buff *skb)
  215. {
  216. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  217. u8 *pos, neighbors;
  218. u8 meshconf_len = sizeof(struct ieee80211_meshconf_ie);
  219. if (skb_tailroom(skb) < 2 + meshconf_len)
  220. return -ENOMEM;
  221. pos = skb_put(skb, 2 + meshconf_len);
  222. *pos++ = WLAN_EID_MESH_CONFIG;
  223. *pos++ = meshconf_len;
  224. /* Active path selection protocol ID */
  225. *pos++ = ifmsh->mesh_pp_id;
  226. /* Active path selection metric ID */
  227. *pos++ = ifmsh->mesh_pm_id;
  228. /* Congestion control mode identifier */
  229. *pos++ = ifmsh->mesh_cc_id;
  230. /* Synchronization protocol identifier */
  231. *pos++ = ifmsh->mesh_sp_id;
  232. /* Authentication Protocol identifier */
  233. *pos++ = ifmsh->mesh_auth_id;
  234. /* Mesh Formation Info - number of neighbors */
  235. neighbors = atomic_read(&ifmsh->estab_plinks);
  236. neighbors = min_t(int, neighbors, IEEE80211_MAX_MESH_PEERINGS);
  237. *pos++ = neighbors << 1;
  238. /* Mesh capability */
  239. *pos = 0x00;
  240. *pos |= ifmsh->mshcfg.dot11MeshForwarding ?
  241. IEEE80211_MESHCONF_CAPAB_FORWARDING : 0x00;
  242. *pos |= ifmsh->accepting_plinks ?
  243. IEEE80211_MESHCONF_CAPAB_ACCEPT_PLINKS : 0x00;
  244. /* Mesh PS mode. See IEEE802.11-2012 8.4.2.100.8 */
  245. *pos |= ifmsh->ps_peers_deep_sleep ?
  246. IEEE80211_MESHCONF_CAPAB_POWER_SAVE_LEVEL : 0x00;
  247. *pos++ |= ifmsh->adjusting_tbtt ?
  248. IEEE80211_MESHCONF_CAPAB_TBTT_ADJUSTING : 0x00;
  249. *pos++ = 0x00;
  250. return 0;
  251. }
  252. int mesh_add_meshid_ie(struct ieee80211_sub_if_data *sdata, struct sk_buff *skb)
  253. {
  254. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  255. u8 *pos;
  256. if (skb_tailroom(skb) < 2 + ifmsh->mesh_id_len)
  257. return -ENOMEM;
  258. pos = skb_put(skb, 2 + ifmsh->mesh_id_len);
  259. *pos++ = WLAN_EID_MESH_ID;
  260. *pos++ = ifmsh->mesh_id_len;
  261. if (ifmsh->mesh_id_len)
  262. memcpy(pos, ifmsh->mesh_id, ifmsh->mesh_id_len);
  263. return 0;
  264. }
  265. static int mesh_add_awake_window_ie(struct ieee80211_sub_if_data *sdata,
  266. struct sk_buff *skb)
  267. {
  268. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  269. u8 *pos;
  270. /* see IEEE802.11-2012 13.14.6 */
  271. if (ifmsh->ps_peers_light_sleep == 0 &&
  272. ifmsh->ps_peers_deep_sleep == 0 &&
  273. ifmsh->nonpeer_pm == NL80211_MESH_POWER_ACTIVE)
  274. return 0;
  275. if (skb_tailroom(skb) < 4)
  276. return -ENOMEM;
  277. pos = skb_put(skb, 2 + 2);
  278. *pos++ = WLAN_EID_MESH_AWAKE_WINDOW;
  279. *pos++ = 2;
  280. put_unaligned_le16(ifmsh->mshcfg.dot11MeshAwakeWindowDuration, pos);
  281. return 0;
  282. }
  283. int mesh_add_vendor_ies(struct ieee80211_sub_if_data *sdata,
  284. struct sk_buff *skb)
  285. {
  286. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  287. u8 offset, len;
  288. const u8 *data;
  289. if (!ifmsh->ie || !ifmsh->ie_len)
  290. return 0;
  291. /* fast-forward to vendor IEs */
  292. offset = ieee80211_ie_split_vendor(ifmsh->ie, ifmsh->ie_len, 0);
  293. if (offset) {
  294. len = ifmsh->ie_len - offset;
  295. data = ifmsh->ie + offset;
  296. if (skb_tailroom(skb) < len)
  297. return -ENOMEM;
  298. memcpy(skb_put(skb, len), data, len);
  299. }
  300. return 0;
  301. }
  302. int mesh_add_rsn_ie(struct ieee80211_sub_if_data *sdata, struct sk_buff *skb)
  303. {
  304. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  305. u8 len = 0;
  306. const u8 *data;
  307. if (!ifmsh->ie || !ifmsh->ie_len)
  308. return 0;
  309. /* find RSN IE */
  310. data = ifmsh->ie;
  311. while (data < ifmsh->ie + ifmsh->ie_len) {
  312. if (*data == WLAN_EID_RSN) {
  313. len = data[1] + 2;
  314. break;
  315. }
  316. data++;
  317. }
  318. if (len) {
  319. if (skb_tailroom(skb) < len)
  320. return -ENOMEM;
  321. memcpy(skb_put(skb, len), data, len);
  322. }
  323. return 0;
  324. }
  325. static int mesh_add_ds_params_ie(struct ieee80211_sub_if_data *sdata,
  326. struct sk_buff *skb)
  327. {
  328. struct ieee80211_chanctx_conf *chanctx_conf;
  329. struct ieee80211_channel *chan;
  330. u8 *pos;
  331. if (skb_tailroom(skb) < 3)
  332. return -ENOMEM;
  333. rcu_read_lock();
  334. chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
  335. if (WARN_ON(!chanctx_conf)) {
  336. rcu_read_unlock();
  337. return -EINVAL;
  338. }
  339. chan = chanctx_conf->def.chan;
  340. rcu_read_unlock();
  341. pos = skb_put(skb, 2 + 1);
  342. *pos++ = WLAN_EID_DS_PARAMS;
  343. *pos++ = 1;
  344. *pos++ = ieee80211_frequency_to_channel(chan->center_freq);
  345. return 0;
  346. }
  347. int mesh_add_ht_cap_ie(struct ieee80211_sub_if_data *sdata,
  348. struct sk_buff *skb)
  349. {
  350. struct ieee80211_local *local = sdata->local;
  351. enum ieee80211_band band = ieee80211_get_sdata_band(sdata);
  352. struct ieee80211_supported_band *sband;
  353. u8 *pos;
  354. sband = local->hw.wiphy->bands[band];
  355. if (!sband->ht_cap.ht_supported ||
  356. sdata->vif.bss_conf.chandef.width == NL80211_CHAN_WIDTH_20_NOHT ||
  357. sdata->vif.bss_conf.chandef.width == NL80211_CHAN_WIDTH_5 ||
  358. sdata->vif.bss_conf.chandef.width == NL80211_CHAN_WIDTH_10)
  359. return 0;
  360. if (skb_tailroom(skb) < 2 + sizeof(struct ieee80211_ht_cap))
  361. return -ENOMEM;
  362. pos = skb_put(skb, 2 + sizeof(struct ieee80211_ht_cap));
  363. ieee80211_ie_build_ht_cap(pos, &sband->ht_cap, sband->ht_cap.cap);
  364. return 0;
  365. }
  366. int mesh_add_ht_oper_ie(struct ieee80211_sub_if_data *sdata,
  367. struct sk_buff *skb)
  368. {
  369. struct ieee80211_local *local = sdata->local;
  370. struct ieee80211_chanctx_conf *chanctx_conf;
  371. struct ieee80211_channel *channel;
  372. enum nl80211_channel_type channel_type =
  373. cfg80211_get_chandef_type(&sdata->vif.bss_conf.chandef);
  374. struct ieee80211_supported_band *sband;
  375. struct ieee80211_sta_ht_cap *ht_cap;
  376. u8 *pos;
  377. rcu_read_lock();
  378. chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
  379. if (WARN_ON(!chanctx_conf)) {
  380. rcu_read_unlock();
  381. return -EINVAL;
  382. }
  383. channel = chanctx_conf->def.chan;
  384. rcu_read_unlock();
  385. sband = local->hw.wiphy->bands[channel->band];
  386. ht_cap = &sband->ht_cap;
  387. if (!ht_cap->ht_supported || channel_type == NL80211_CHAN_NO_HT)
  388. return 0;
  389. if (skb_tailroom(skb) < 2 + sizeof(struct ieee80211_ht_operation))
  390. return -ENOMEM;
  391. pos = skb_put(skb, 2 + sizeof(struct ieee80211_ht_operation));
  392. ieee80211_ie_build_ht_oper(pos, ht_cap, &sdata->vif.bss_conf.chandef,
  393. sdata->vif.bss_conf.ht_operation_mode);
  394. return 0;
  395. }
  396. static void ieee80211_mesh_path_timer(unsigned long data)
  397. {
  398. struct ieee80211_sub_if_data *sdata =
  399. (struct ieee80211_sub_if_data *) data;
  400. ieee80211_queue_work(&sdata->local->hw, &sdata->work);
  401. }
  402. static void ieee80211_mesh_path_root_timer(unsigned long data)
  403. {
  404. struct ieee80211_sub_if_data *sdata =
  405. (struct ieee80211_sub_if_data *) data;
  406. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  407. set_bit(MESH_WORK_ROOT, &ifmsh->wrkq_flags);
  408. ieee80211_queue_work(&sdata->local->hw, &sdata->work);
  409. }
  410. void ieee80211_mesh_root_setup(struct ieee80211_if_mesh *ifmsh)
  411. {
  412. if (ifmsh->mshcfg.dot11MeshHWMPRootMode > IEEE80211_ROOTMODE_ROOT)
  413. set_bit(MESH_WORK_ROOT, &ifmsh->wrkq_flags);
  414. else {
  415. clear_bit(MESH_WORK_ROOT, &ifmsh->wrkq_flags);
  416. /* stop running timer */
  417. del_timer_sync(&ifmsh->mesh_path_root_timer);
  418. }
  419. }
  420. /**
  421. * ieee80211_fill_mesh_addresses - fill addresses of a locally originated mesh frame
  422. * @hdr: 802.11 frame header
  423. * @fc: frame control field
  424. * @meshda: destination address in the mesh
  425. * @meshsa: source address address in the mesh. Same as TA, as frame is
  426. * locally originated.
  427. *
  428. * Return the length of the 802.11 (does not include a mesh control header)
  429. */
  430. int ieee80211_fill_mesh_addresses(struct ieee80211_hdr *hdr, __le16 *fc,
  431. const u8 *meshda, const u8 *meshsa)
  432. {
  433. if (is_multicast_ether_addr(meshda)) {
  434. *fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS);
  435. /* DA TA SA */
  436. memcpy(hdr->addr1, meshda, ETH_ALEN);
  437. memcpy(hdr->addr2, meshsa, ETH_ALEN);
  438. memcpy(hdr->addr3, meshsa, ETH_ALEN);
  439. return 24;
  440. } else {
  441. *fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS);
  442. /* RA TA DA SA */
  443. memset(hdr->addr1, 0, ETH_ALEN); /* RA is resolved later */
  444. memcpy(hdr->addr2, meshsa, ETH_ALEN);
  445. memcpy(hdr->addr3, meshda, ETH_ALEN);
  446. memcpy(hdr->addr4, meshsa, ETH_ALEN);
  447. return 30;
  448. }
  449. }
  450. /**
  451. * ieee80211_new_mesh_header - create a new mesh header
  452. * @sdata: mesh interface to be used
  453. * @meshhdr: uninitialized mesh header
  454. * @addr4or5: 1st address in the ae header, which may correspond to address 4
  455. * (if addr6 is NULL) or address 5 (if addr6 is present). It may
  456. * be NULL.
  457. * @addr6: 2nd address in the ae header, which corresponds to addr6 of the
  458. * mesh frame
  459. *
  460. * Return the header length.
  461. */
  462. int ieee80211_new_mesh_header(struct ieee80211_sub_if_data *sdata,
  463. struct ieee80211s_hdr *meshhdr,
  464. const char *addr4or5, const char *addr6)
  465. {
  466. if (WARN_ON(!addr4or5 && addr6))
  467. return 0;
  468. memset(meshhdr, 0, sizeof(*meshhdr));
  469. meshhdr->ttl = sdata->u.mesh.mshcfg.dot11MeshTTL;
  470. /* FIXME: racy -- TX on multiple queues can be concurrent */
  471. put_unaligned(cpu_to_le32(sdata->u.mesh.mesh_seqnum), &meshhdr->seqnum);
  472. sdata->u.mesh.mesh_seqnum++;
  473. if (addr4or5 && !addr6) {
  474. meshhdr->flags |= MESH_FLAGS_AE_A4;
  475. memcpy(meshhdr->eaddr1, addr4or5, ETH_ALEN);
  476. return 2 * ETH_ALEN;
  477. } else if (addr4or5 && addr6) {
  478. meshhdr->flags |= MESH_FLAGS_AE_A5_A6;
  479. memcpy(meshhdr->eaddr1, addr4or5, ETH_ALEN);
  480. memcpy(meshhdr->eaddr2, addr6, ETH_ALEN);
  481. return 3 * ETH_ALEN;
  482. }
  483. return ETH_ALEN;
  484. }
  485. static void ieee80211_mesh_housekeeping(struct ieee80211_sub_if_data *sdata)
  486. {
  487. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  488. u32 changed;
  489. ieee80211_sta_expire(sdata, ifmsh->mshcfg.plink_timeout * HZ);
  490. mesh_path_expire(sdata);
  491. changed = mesh_accept_plinks_update(sdata);
  492. ieee80211_mbss_info_change_notify(sdata, changed);
  493. mod_timer(&ifmsh->housekeeping_timer,
  494. round_jiffies(jiffies +
  495. IEEE80211_MESH_HOUSEKEEPING_INTERVAL));
  496. }
  497. static void ieee80211_mesh_rootpath(struct ieee80211_sub_if_data *sdata)
  498. {
  499. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  500. u32 interval;
  501. mesh_path_tx_root_frame(sdata);
  502. if (ifmsh->mshcfg.dot11MeshHWMPRootMode == IEEE80211_PROACTIVE_RANN)
  503. interval = ifmsh->mshcfg.dot11MeshHWMPRannInterval;
  504. else
  505. interval = ifmsh->mshcfg.dot11MeshHWMProotInterval;
  506. mod_timer(&ifmsh->mesh_path_root_timer,
  507. round_jiffies(TU_TO_EXP_TIME(interval)));
  508. }
  509. static int
  510. ieee80211_mesh_build_beacon(struct ieee80211_if_mesh *ifmsh)
  511. {
  512. struct beacon_data *bcn;
  513. int head_len, tail_len;
  514. struct sk_buff *skb;
  515. struct ieee80211_mgmt *mgmt;
  516. struct ieee80211_chanctx_conf *chanctx_conf;
  517. enum ieee80211_band band;
  518. u8 *pos;
  519. struct ieee80211_sub_if_data *sdata;
  520. int hdr_len = offsetof(struct ieee80211_mgmt, u.beacon) +
  521. sizeof(mgmt->u.beacon);
  522. sdata = container_of(ifmsh, struct ieee80211_sub_if_data, u.mesh);
  523. rcu_read_lock();
  524. chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
  525. band = chanctx_conf->def.chan->band;
  526. rcu_read_unlock();
  527. head_len = hdr_len +
  528. 2 + /* NULL SSID */
  529. 2 + 8 + /* supported rates */
  530. 2 + 3; /* DS params */
  531. tail_len = 2 + (IEEE80211_MAX_SUPP_RATES - 8) +
  532. 2 + sizeof(struct ieee80211_ht_cap) +
  533. 2 + sizeof(struct ieee80211_ht_operation) +
  534. 2 + ifmsh->mesh_id_len +
  535. 2 + sizeof(struct ieee80211_meshconf_ie) +
  536. 2 + sizeof(__le16) + /* awake window */
  537. ifmsh->ie_len;
  538. bcn = kzalloc(sizeof(*bcn) + head_len + tail_len, GFP_KERNEL);
  539. /* need an skb for IE builders to operate on */
  540. skb = dev_alloc_skb(max(head_len, tail_len));
  541. if (!bcn || !skb)
  542. goto out_free;
  543. /*
  544. * pointers go into the block we allocated,
  545. * memory is | beacon_data | head | tail |
  546. */
  547. bcn->head = ((u8 *) bcn) + sizeof(*bcn);
  548. /* fill in the head */
  549. mgmt = (struct ieee80211_mgmt *) skb_put(skb, hdr_len);
  550. memset(mgmt, 0, hdr_len);
  551. mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
  552. IEEE80211_STYPE_BEACON);
  553. eth_broadcast_addr(mgmt->da);
  554. memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
  555. memcpy(mgmt->bssid, sdata->vif.addr, ETH_ALEN);
  556. ieee80211_mps_set_frame_flags(sdata, NULL, (void *) mgmt);
  557. mgmt->u.beacon.beacon_int =
  558. cpu_to_le16(sdata->vif.bss_conf.beacon_int);
  559. mgmt->u.beacon.capab_info |= cpu_to_le16(
  560. sdata->u.mesh.security ? WLAN_CAPABILITY_PRIVACY : 0);
  561. pos = skb_put(skb, 2);
  562. *pos++ = WLAN_EID_SSID;
  563. *pos++ = 0x0;
  564. if (ieee80211_add_srates_ie(sdata, skb, true, band) ||
  565. mesh_add_ds_params_ie(sdata, skb))
  566. goto out_free;
  567. bcn->head_len = skb->len;
  568. memcpy(bcn->head, skb->data, bcn->head_len);
  569. /* now the tail */
  570. skb_trim(skb, 0);
  571. bcn->tail = bcn->head + bcn->head_len;
  572. if (ieee80211_add_ext_srates_ie(sdata, skb, true, band) ||
  573. mesh_add_rsn_ie(sdata, skb) ||
  574. mesh_add_ht_cap_ie(sdata, skb) ||
  575. mesh_add_ht_oper_ie(sdata, skb) ||
  576. mesh_add_meshid_ie(sdata, skb) ||
  577. mesh_add_meshconf_ie(sdata, skb) ||
  578. mesh_add_awake_window_ie(sdata, skb) ||
  579. mesh_add_vendor_ies(sdata, skb))
  580. goto out_free;
  581. bcn->tail_len = skb->len;
  582. memcpy(bcn->tail, skb->data, bcn->tail_len);
  583. dev_kfree_skb(skb);
  584. rcu_assign_pointer(ifmsh->beacon, bcn);
  585. return 0;
  586. out_free:
  587. kfree(bcn);
  588. dev_kfree_skb(skb);
  589. return -ENOMEM;
  590. }
  591. static int
  592. ieee80211_mesh_rebuild_beacon(struct ieee80211_sub_if_data *sdata)
  593. {
  594. struct beacon_data *old_bcn;
  595. int ret;
  596. old_bcn = rcu_dereference_protected(sdata->u.mesh.beacon,
  597. lockdep_is_held(&sdata->wdev.mtx));
  598. ret = ieee80211_mesh_build_beacon(&sdata->u.mesh);
  599. if (ret)
  600. /* just reuse old beacon */
  601. return ret;
  602. if (old_bcn)
  603. kfree_rcu(old_bcn, rcu_head);
  604. return 0;
  605. }
  606. void ieee80211_mbss_info_change_notify(struct ieee80211_sub_if_data *sdata,
  607. u32 changed)
  608. {
  609. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  610. unsigned long bits = changed;
  611. u32 bit;
  612. if (!bits)
  613. return;
  614. /* if we race with running work, worst case this work becomes a noop */
  615. for_each_set_bit(bit, &bits, sizeof(changed) * BITS_PER_BYTE)
  616. set_bit(bit, &ifmsh->mbss_changed);
  617. set_bit(MESH_WORK_MBSS_CHANGED, &ifmsh->wrkq_flags);
  618. ieee80211_queue_work(&sdata->local->hw, &sdata->work);
  619. }
  620. int ieee80211_start_mesh(struct ieee80211_sub_if_data *sdata)
  621. {
  622. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  623. struct ieee80211_local *local = sdata->local;
  624. u32 changed = BSS_CHANGED_BEACON |
  625. BSS_CHANGED_BEACON_ENABLED |
  626. BSS_CHANGED_HT |
  627. BSS_CHANGED_BASIC_RATES |
  628. BSS_CHANGED_BEACON_INT;
  629. local->fif_other_bss++;
  630. /* mesh ifaces must set allmulti to forward mcast traffic */
  631. atomic_inc(&local->iff_allmultis);
  632. ieee80211_configure_filter(local);
  633. ifmsh->mesh_cc_id = 0; /* Disabled */
  634. /* register sync ops from extensible synchronization framework */
  635. ifmsh->sync_ops = ieee80211_mesh_sync_ops_get(ifmsh->mesh_sp_id);
  636. ifmsh->adjusting_tbtt = false;
  637. ifmsh->sync_offset_clockdrift_max = 0;
  638. set_bit(MESH_WORK_HOUSEKEEPING, &ifmsh->wrkq_flags);
  639. ieee80211_mesh_root_setup(ifmsh);
  640. ieee80211_queue_work(&local->hw, &sdata->work);
  641. sdata->vif.bss_conf.ht_operation_mode =
  642. ifmsh->mshcfg.ht_opmode;
  643. sdata->vif.bss_conf.enable_beacon = true;
  644. changed |= ieee80211_mps_local_status_update(sdata);
  645. if (ieee80211_mesh_build_beacon(ifmsh)) {
  646. ieee80211_stop_mesh(sdata);
  647. return -ENOMEM;
  648. }
  649. ieee80211_bss_info_change_notify(sdata, changed);
  650. netif_carrier_on(sdata->dev);
  651. return 0;
  652. }
  653. void ieee80211_stop_mesh(struct ieee80211_sub_if_data *sdata)
  654. {
  655. struct ieee80211_local *local = sdata->local;
  656. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  657. struct beacon_data *bcn;
  658. netif_carrier_off(sdata->dev);
  659. /* stop the beacon */
  660. ifmsh->mesh_id_len = 0;
  661. sdata->vif.bss_conf.enable_beacon = false;
  662. clear_bit(SDATA_STATE_OFFCHANNEL_BEACON_STOPPED, &sdata->state);
  663. ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BEACON_ENABLED);
  664. bcn = rcu_dereference_protected(ifmsh->beacon,
  665. lockdep_is_held(&sdata->wdev.mtx));
  666. rcu_assign_pointer(ifmsh->beacon, NULL);
  667. kfree_rcu(bcn, rcu_head);
  668. /* flush STAs and mpaths on this iface */
  669. sta_info_flush(sdata);
  670. mesh_path_flush_by_iface(sdata);
  671. /* free all potentially still buffered group-addressed frames */
  672. local->total_ps_buffered -= skb_queue_len(&ifmsh->ps.bc_buf);
  673. skb_queue_purge(&ifmsh->ps.bc_buf);
  674. del_timer_sync(&sdata->u.mesh.housekeeping_timer);
  675. del_timer_sync(&sdata->u.mesh.mesh_path_root_timer);
  676. del_timer_sync(&sdata->u.mesh.mesh_path_timer);
  677. /* clear any mesh work (for next join) we may have accrued */
  678. ifmsh->wrkq_flags = 0;
  679. ifmsh->mbss_changed = 0;
  680. local->fif_other_bss--;
  681. atomic_dec(&local->iff_allmultis);
  682. ieee80211_configure_filter(local);
  683. }
  684. static void
  685. ieee80211_mesh_rx_probe_req(struct ieee80211_sub_if_data *sdata,
  686. struct ieee80211_mgmt *mgmt, size_t len)
  687. {
  688. struct ieee80211_local *local = sdata->local;
  689. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  690. struct sk_buff *presp;
  691. struct beacon_data *bcn;
  692. struct ieee80211_mgmt *hdr;
  693. struct ieee802_11_elems elems;
  694. size_t baselen;
  695. u8 *pos;
  696. pos = mgmt->u.probe_req.variable;
  697. baselen = (u8 *) pos - (u8 *) mgmt;
  698. if (baselen > len)
  699. return;
  700. ieee802_11_parse_elems(pos, len - baselen, false, &elems);
  701. if (!elems.mesh_id)
  702. return;
  703. /* 802.11-2012 10.1.4.3.2 */
  704. if ((!ether_addr_equal(mgmt->da, sdata->vif.addr) &&
  705. !is_broadcast_ether_addr(mgmt->da)) ||
  706. elems.ssid_len != 0)
  707. return;
  708. if (elems.mesh_id_len != 0 &&
  709. (elems.mesh_id_len != ifmsh->mesh_id_len ||
  710. memcmp(elems.mesh_id, ifmsh->mesh_id, ifmsh->mesh_id_len)))
  711. return;
  712. rcu_read_lock();
  713. bcn = rcu_dereference(ifmsh->beacon);
  714. if (!bcn)
  715. goto out;
  716. presp = dev_alloc_skb(local->tx_headroom +
  717. bcn->head_len + bcn->tail_len);
  718. if (!presp)
  719. goto out;
  720. skb_reserve(presp, local->tx_headroom);
  721. memcpy(skb_put(presp, bcn->head_len), bcn->head, bcn->head_len);
  722. memcpy(skb_put(presp, bcn->tail_len), bcn->tail, bcn->tail_len);
  723. hdr = (struct ieee80211_mgmt *) presp->data;
  724. hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
  725. IEEE80211_STYPE_PROBE_RESP);
  726. memcpy(hdr->da, mgmt->sa, ETH_ALEN);
  727. IEEE80211_SKB_CB(presp)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
  728. ieee80211_tx_skb(sdata, presp);
  729. out:
  730. rcu_read_unlock();
  731. }
  732. static void ieee80211_mesh_rx_bcn_presp(struct ieee80211_sub_if_data *sdata,
  733. u16 stype,
  734. struct ieee80211_mgmt *mgmt,
  735. size_t len,
  736. struct ieee80211_rx_status *rx_status)
  737. {
  738. struct ieee80211_local *local = sdata->local;
  739. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  740. struct ieee802_11_elems elems;
  741. struct ieee80211_channel *channel;
  742. size_t baselen;
  743. int freq;
  744. enum ieee80211_band band = rx_status->band;
  745. /* ignore ProbeResp to foreign address */
  746. if (stype == IEEE80211_STYPE_PROBE_RESP &&
  747. !ether_addr_equal(mgmt->da, sdata->vif.addr))
  748. return;
  749. baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt;
  750. if (baselen > len)
  751. return;
  752. ieee802_11_parse_elems(mgmt->u.probe_resp.variable, len - baselen,
  753. false, &elems);
  754. /* ignore non-mesh or secure / unsecure mismatch */
  755. if ((!elems.mesh_id || !elems.mesh_config) ||
  756. (elems.rsn && sdata->u.mesh.security == IEEE80211_MESH_SEC_NONE) ||
  757. (!elems.rsn && sdata->u.mesh.security != IEEE80211_MESH_SEC_NONE))
  758. return;
  759. if (elems.ds_params)
  760. freq = ieee80211_channel_to_frequency(elems.ds_params[0], band);
  761. else
  762. freq = rx_status->freq;
  763. channel = ieee80211_get_channel(local->hw.wiphy, freq);
  764. if (!channel || channel->flags & IEEE80211_CHAN_DISABLED)
  765. return;
  766. if (mesh_matches_local(sdata, &elems))
  767. mesh_neighbour_update(sdata, mgmt->sa, &elems);
  768. if (ifmsh->sync_ops)
  769. ifmsh->sync_ops->rx_bcn_presp(sdata,
  770. stype, mgmt, &elems, rx_status);
  771. }
  772. static void ieee80211_mesh_rx_mgmt_action(struct ieee80211_sub_if_data *sdata,
  773. struct ieee80211_mgmt *mgmt,
  774. size_t len,
  775. struct ieee80211_rx_status *rx_status)
  776. {
  777. switch (mgmt->u.action.category) {
  778. case WLAN_CATEGORY_SELF_PROTECTED:
  779. switch (mgmt->u.action.u.self_prot.action_code) {
  780. case WLAN_SP_MESH_PEERING_OPEN:
  781. case WLAN_SP_MESH_PEERING_CLOSE:
  782. case WLAN_SP_MESH_PEERING_CONFIRM:
  783. mesh_rx_plink_frame(sdata, mgmt, len, rx_status);
  784. break;
  785. }
  786. break;
  787. case WLAN_CATEGORY_MESH_ACTION:
  788. if (mesh_action_is_path_sel(mgmt))
  789. mesh_rx_path_sel_frame(sdata, mgmt, len);
  790. break;
  791. }
  792. }
  793. void ieee80211_mesh_rx_queued_mgmt(struct ieee80211_sub_if_data *sdata,
  794. struct sk_buff *skb)
  795. {
  796. struct ieee80211_rx_status *rx_status;
  797. struct ieee80211_mgmt *mgmt;
  798. u16 stype;
  799. sdata_lock(sdata);
  800. /* mesh already went down */
  801. if (!sdata->wdev.mesh_id_len)
  802. goto out;
  803. rx_status = IEEE80211_SKB_RXCB(skb);
  804. mgmt = (struct ieee80211_mgmt *) skb->data;
  805. stype = le16_to_cpu(mgmt->frame_control) & IEEE80211_FCTL_STYPE;
  806. switch (stype) {
  807. case IEEE80211_STYPE_PROBE_RESP:
  808. case IEEE80211_STYPE_BEACON:
  809. ieee80211_mesh_rx_bcn_presp(sdata, stype, mgmt, skb->len,
  810. rx_status);
  811. break;
  812. case IEEE80211_STYPE_PROBE_REQ:
  813. ieee80211_mesh_rx_probe_req(sdata, mgmt, skb->len);
  814. break;
  815. case IEEE80211_STYPE_ACTION:
  816. ieee80211_mesh_rx_mgmt_action(sdata, mgmt, skb->len, rx_status);
  817. break;
  818. }
  819. out:
  820. sdata_unlock(sdata);
  821. }
  822. static void mesh_bss_info_changed(struct ieee80211_sub_if_data *sdata)
  823. {
  824. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  825. u32 bit, changed = 0;
  826. for_each_set_bit(bit, &ifmsh->mbss_changed,
  827. sizeof(changed) * BITS_PER_BYTE) {
  828. clear_bit(bit, &ifmsh->mbss_changed);
  829. changed |= BIT(bit);
  830. }
  831. if (sdata->vif.bss_conf.enable_beacon &&
  832. (changed & (BSS_CHANGED_BEACON |
  833. BSS_CHANGED_HT |
  834. BSS_CHANGED_BASIC_RATES |
  835. BSS_CHANGED_BEACON_INT)))
  836. if (ieee80211_mesh_rebuild_beacon(sdata))
  837. return;
  838. ieee80211_bss_info_change_notify(sdata, changed);
  839. }
  840. void ieee80211_mesh_work(struct ieee80211_sub_if_data *sdata)
  841. {
  842. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  843. sdata_lock(sdata);
  844. /* mesh already went down */
  845. if (!sdata->wdev.mesh_id_len)
  846. goto out;
  847. if (ifmsh->preq_queue_len &&
  848. time_after(jiffies,
  849. ifmsh->last_preq + msecs_to_jiffies(ifmsh->mshcfg.dot11MeshHWMPpreqMinInterval)))
  850. mesh_path_start_discovery(sdata);
  851. if (test_and_clear_bit(MESH_WORK_GROW_MPATH_TABLE, &ifmsh->wrkq_flags))
  852. mesh_mpath_table_grow();
  853. if (test_and_clear_bit(MESH_WORK_GROW_MPP_TABLE, &ifmsh->wrkq_flags))
  854. mesh_mpp_table_grow();
  855. if (test_and_clear_bit(MESH_WORK_HOUSEKEEPING, &ifmsh->wrkq_flags))
  856. ieee80211_mesh_housekeeping(sdata);
  857. if (test_and_clear_bit(MESH_WORK_ROOT, &ifmsh->wrkq_flags))
  858. ieee80211_mesh_rootpath(sdata);
  859. if (test_and_clear_bit(MESH_WORK_DRIFT_ADJUST, &ifmsh->wrkq_flags))
  860. mesh_sync_adjust_tbtt(sdata);
  861. if (test_and_clear_bit(MESH_WORK_MBSS_CHANGED, &ifmsh->wrkq_flags))
  862. mesh_bss_info_changed(sdata);
  863. out:
  864. sdata_unlock(sdata);
  865. }
  866. void ieee80211_mesh_notify_scan_completed(struct ieee80211_local *local)
  867. {
  868. struct ieee80211_sub_if_data *sdata;
  869. rcu_read_lock();
  870. list_for_each_entry_rcu(sdata, &local->interfaces, list)
  871. if (ieee80211_vif_is_mesh(&sdata->vif) &&
  872. ieee80211_sdata_running(sdata))
  873. ieee80211_queue_work(&local->hw, &sdata->work);
  874. rcu_read_unlock();
  875. }
  876. void ieee80211_mesh_init_sdata(struct ieee80211_sub_if_data *sdata)
  877. {
  878. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  879. static u8 zero_addr[ETH_ALEN] = {};
  880. setup_timer(&ifmsh->housekeeping_timer,
  881. ieee80211_mesh_housekeeping_timer,
  882. (unsigned long) sdata);
  883. ifmsh->accepting_plinks = true;
  884. ifmsh->preq_id = 0;
  885. ifmsh->sn = 0;
  886. ifmsh->num_gates = 0;
  887. atomic_set(&ifmsh->mpaths, 0);
  888. mesh_rmc_init(sdata);
  889. ifmsh->last_preq = jiffies;
  890. ifmsh->next_perr = jiffies;
  891. /* Allocate all mesh structures when creating the first mesh interface. */
  892. if (!mesh_allocated)
  893. ieee80211s_init();
  894. setup_timer(&ifmsh->mesh_path_timer,
  895. ieee80211_mesh_path_timer,
  896. (unsigned long) sdata);
  897. setup_timer(&ifmsh->mesh_path_root_timer,
  898. ieee80211_mesh_path_root_timer,
  899. (unsigned long) sdata);
  900. INIT_LIST_HEAD(&ifmsh->preq_queue.list);
  901. skb_queue_head_init(&ifmsh->ps.bc_buf);
  902. spin_lock_init(&ifmsh->mesh_preq_queue_lock);
  903. spin_lock_init(&ifmsh->sync_offset_lock);
  904. RCU_INIT_POINTER(ifmsh->beacon, NULL);
  905. sdata->vif.bss_conf.bssid = zero_addr;
  906. }