mesh.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868
  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. #define TMR_RUNNING_HK 0
  15. #define TMR_RUNNING_MP 1
  16. #define TMR_RUNNING_MPR 2
  17. int mesh_allocated;
  18. static struct kmem_cache *rm_cache;
  19. bool mesh_action_is_path_sel(struct ieee80211_mgmt *mgmt)
  20. {
  21. return (mgmt->u.action.u.mesh_action.action_code ==
  22. WLAN_MESH_ACTION_HWMP_PATH_SELECTION);
  23. }
  24. void ieee80211s_init(void)
  25. {
  26. mesh_pathtbl_init();
  27. mesh_allocated = 1;
  28. rm_cache = kmem_cache_create("mesh_rmc", sizeof(struct rmc_entry),
  29. 0, 0, NULL);
  30. }
  31. void ieee80211s_stop(void)
  32. {
  33. mesh_pathtbl_unregister();
  34. kmem_cache_destroy(rm_cache);
  35. }
  36. static void ieee80211_mesh_housekeeping_timer(unsigned long data)
  37. {
  38. struct ieee80211_sub_if_data *sdata = (void *) data;
  39. struct ieee80211_local *local = sdata->local;
  40. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  41. set_bit(MESH_WORK_HOUSEKEEPING, &ifmsh->wrkq_flags);
  42. if (local->quiescing) {
  43. set_bit(TMR_RUNNING_HK, &ifmsh->timers_running);
  44. return;
  45. }
  46. ieee80211_queue_work(&local->hw, &sdata->work);
  47. }
  48. /**
  49. * mesh_matches_local - check if the config of a mesh point matches ours
  50. *
  51. * @sdata: local mesh subif
  52. * @ie: information elements of a management frame from the mesh peer
  53. *
  54. * This function checks if the mesh configuration of a mesh point matches the
  55. * local mesh configuration, i.e. if both nodes belong to the same mesh network.
  56. */
  57. bool mesh_matches_local(struct ieee80211_sub_if_data *sdata,
  58. struct ieee802_11_elems *ie)
  59. {
  60. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  61. struct ieee80211_local *local = sdata->local;
  62. u32 basic_rates = 0;
  63. struct cfg80211_chan_def sta_chan_def;
  64. /*
  65. * As support for each feature is added, check for matching
  66. * - On mesh config capabilities
  67. * - Power Save Support En
  68. * - Sync support enabled
  69. * - Sync support active
  70. * - Sync support required from peer
  71. * - MDA enabled
  72. * - Power management control on fc
  73. */
  74. if (!(ifmsh->mesh_id_len == ie->mesh_id_len &&
  75. memcmp(ifmsh->mesh_id, ie->mesh_id, ie->mesh_id_len) == 0 &&
  76. (ifmsh->mesh_pp_id == ie->mesh_config->meshconf_psel) &&
  77. (ifmsh->mesh_pm_id == ie->mesh_config->meshconf_pmetric) &&
  78. (ifmsh->mesh_cc_id == ie->mesh_config->meshconf_congest) &&
  79. (ifmsh->mesh_sp_id == ie->mesh_config->meshconf_synch) &&
  80. (ifmsh->mesh_auth_id == ie->mesh_config->meshconf_auth)))
  81. goto mismatch;
  82. ieee80211_sta_get_rates(local, ie, ieee80211_get_sdata_band(sdata),
  83. &basic_rates);
  84. if (sdata->vif.bss_conf.basic_rates != basic_rates)
  85. goto mismatch;
  86. ieee80211_ht_oper_to_chandef(sdata->vif.bss_conf.chandef.chan,
  87. ie->ht_operation, &sta_chan_def);
  88. if (!cfg80211_chandef_compatible(&sdata->vif.bss_conf.chandef,
  89. &sta_chan_def))
  90. goto mismatch;
  91. return true;
  92. mismatch:
  93. return false;
  94. }
  95. /**
  96. * mesh_peer_accepts_plinks - check if an mp is willing to establish peer links
  97. *
  98. * @ie: information elements of a management frame from the mesh peer
  99. */
  100. bool mesh_peer_accepts_plinks(struct ieee802_11_elems *ie)
  101. {
  102. return (ie->mesh_config->meshconf_cap &
  103. IEEE80211_MESHCONF_CAPAB_ACCEPT_PLINKS) != 0;
  104. }
  105. /**
  106. * mesh_accept_plinks_update - update accepting_plink in local mesh beacons
  107. *
  108. * @sdata: mesh interface in which mesh beacons are going to be updated
  109. *
  110. * Returns: beacon changed flag if the beacon content changed.
  111. */
  112. u32 mesh_accept_plinks_update(struct ieee80211_sub_if_data *sdata)
  113. {
  114. bool free_plinks;
  115. u32 changed = 0;
  116. /* In case mesh_plink_free_count > 0 and mesh_plinktbl_capacity == 0,
  117. * the mesh interface might be able to establish plinks with peers that
  118. * are already on the table but are not on PLINK_ESTAB state. However,
  119. * in general the mesh interface is not accepting peer link requests
  120. * from new peers, and that must be reflected in the beacon
  121. */
  122. free_plinks = mesh_plink_availables(sdata);
  123. if (free_plinks != sdata->u.mesh.accepting_plinks) {
  124. sdata->u.mesh.accepting_plinks = free_plinks;
  125. changed = BSS_CHANGED_BEACON;
  126. }
  127. return changed;
  128. }
  129. int mesh_rmc_init(struct ieee80211_sub_if_data *sdata)
  130. {
  131. int i;
  132. sdata->u.mesh.rmc = kmalloc(sizeof(struct mesh_rmc), GFP_KERNEL);
  133. if (!sdata->u.mesh.rmc)
  134. return -ENOMEM;
  135. sdata->u.mesh.rmc->idx_mask = RMC_BUCKETS - 1;
  136. for (i = 0; i < RMC_BUCKETS; i++)
  137. INIT_LIST_HEAD(&sdata->u.mesh.rmc->bucket[i]);
  138. return 0;
  139. }
  140. void mesh_rmc_free(struct ieee80211_sub_if_data *sdata)
  141. {
  142. struct mesh_rmc *rmc = sdata->u.mesh.rmc;
  143. struct rmc_entry *p, *n;
  144. int i;
  145. if (!sdata->u.mesh.rmc)
  146. return;
  147. for (i = 0; i < RMC_BUCKETS; i++)
  148. list_for_each_entry_safe(p, n, &rmc->bucket[i], list) {
  149. list_del(&p->list);
  150. kmem_cache_free(rm_cache, p);
  151. }
  152. kfree(rmc);
  153. sdata->u.mesh.rmc = NULL;
  154. }
  155. /**
  156. * mesh_rmc_check - Check frame in recent multicast cache and add if absent.
  157. *
  158. * @sa: source address
  159. * @mesh_hdr: mesh_header
  160. *
  161. * Returns: 0 if the frame is not in the cache, nonzero otherwise.
  162. *
  163. * Checks using the source address and the mesh sequence number if we have
  164. * received this frame lately. If the frame is not in the cache, it is added to
  165. * it.
  166. */
  167. int mesh_rmc_check(u8 *sa, struct ieee80211s_hdr *mesh_hdr,
  168. struct ieee80211_sub_if_data *sdata)
  169. {
  170. struct mesh_rmc *rmc = sdata->u.mesh.rmc;
  171. u32 seqnum = 0;
  172. int entries = 0;
  173. u8 idx;
  174. struct rmc_entry *p, *n;
  175. /* Don't care about endianness since only match matters */
  176. memcpy(&seqnum, &mesh_hdr->seqnum, sizeof(mesh_hdr->seqnum));
  177. idx = le32_to_cpu(mesh_hdr->seqnum) & rmc->idx_mask;
  178. list_for_each_entry_safe(p, n, &rmc->bucket[idx], list) {
  179. ++entries;
  180. if (time_after(jiffies, p->exp_time) ||
  181. (entries == RMC_QUEUE_MAX_LEN)) {
  182. list_del(&p->list);
  183. kmem_cache_free(rm_cache, p);
  184. --entries;
  185. } else if ((seqnum == p->seqnum) &&
  186. (ether_addr_equal(sa, p->sa)))
  187. return -1;
  188. }
  189. p = kmem_cache_alloc(rm_cache, GFP_ATOMIC);
  190. if (!p)
  191. return 0;
  192. p->seqnum = seqnum;
  193. p->exp_time = jiffies + RMC_TIMEOUT;
  194. memcpy(p->sa, sa, ETH_ALEN);
  195. list_add(&p->list, &rmc->bucket[idx]);
  196. return 0;
  197. }
  198. int
  199. mesh_add_meshconf_ie(struct sk_buff *skb, struct ieee80211_sub_if_data *sdata)
  200. {
  201. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  202. u8 *pos, neighbors;
  203. u8 meshconf_len = sizeof(struct ieee80211_meshconf_ie);
  204. if (skb_tailroom(skb) < 2 + meshconf_len)
  205. return -ENOMEM;
  206. pos = skb_put(skb, 2 + meshconf_len);
  207. *pos++ = WLAN_EID_MESH_CONFIG;
  208. *pos++ = meshconf_len;
  209. /* Active path selection protocol ID */
  210. *pos++ = ifmsh->mesh_pp_id;
  211. /* Active path selection metric ID */
  212. *pos++ = ifmsh->mesh_pm_id;
  213. /* Congestion control mode identifier */
  214. *pos++ = ifmsh->mesh_cc_id;
  215. /* Synchronization protocol identifier */
  216. *pos++ = ifmsh->mesh_sp_id;
  217. /* Authentication Protocol identifier */
  218. *pos++ = ifmsh->mesh_auth_id;
  219. /* Mesh Formation Info - number of neighbors */
  220. neighbors = atomic_read(&ifmsh->estab_plinks);
  221. /* Number of neighbor mesh STAs or 15 whichever is smaller */
  222. neighbors = (neighbors > 15) ? 15 : neighbors;
  223. *pos++ = neighbors << 1;
  224. /* Mesh capability */
  225. *pos = IEEE80211_MESHCONF_CAPAB_FORWARDING;
  226. *pos |= ifmsh->accepting_plinks ?
  227. IEEE80211_MESHCONF_CAPAB_ACCEPT_PLINKS : 0x00;
  228. /* Mesh PS mode. See IEEE802.11-2012 8.4.2.100.8 */
  229. *pos |= ifmsh->ps_peers_deep_sleep ?
  230. IEEE80211_MESHCONF_CAPAB_POWER_SAVE_LEVEL : 0x00;
  231. *pos++ |= ifmsh->adjusting_tbtt ?
  232. IEEE80211_MESHCONF_CAPAB_TBTT_ADJUSTING : 0x00;
  233. *pos++ = 0x00;
  234. return 0;
  235. }
  236. int
  237. mesh_add_meshid_ie(struct sk_buff *skb, struct ieee80211_sub_if_data *sdata)
  238. {
  239. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  240. u8 *pos;
  241. if (skb_tailroom(skb) < 2 + ifmsh->mesh_id_len)
  242. return -ENOMEM;
  243. pos = skb_put(skb, 2 + ifmsh->mesh_id_len);
  244. *pos++ = WLAN_EID_MESH_ID;
  245. *pos++ = ifmsh->mesh_id_len;
  246. if (ifmsh->mesh_id_len)
  247. memcpy(pos, ifmsh->mesh_id, ifmsh->mesh_id_len);
  248. return 0;
  249. }
  250. int mesh_add_awake_window_ie(struct sk_buff *skb,
  251. struct ieee80211_sub_if_data *sdata)
  252. {
  253. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  254. u8 *pos;
  255. /* see IEEE802.11-2012 13.14.6 */
  256. if (ifmsh->ps_peers_light_sleep == 0 &&
  257. ifmsh->ps_peers_deep_sleep == 0 &&
  258. ifmsh->nonpeer_pm == NL80211_MESH_POWER_ACTIVE)
  259. return 0;
  260. if (skb_tailroom(skb) < 4)
  261. return -ENOMEM;
  262. pos = skb_put(skb, 2 + 2);
  263. *pos++ = WLAN_EID_MESH_AWAKE_WINDOW;
  264. *pos++ = 2;
  265. put_unaligned_le16(ifmsh->mshcfg.dot11MeshAwakeWindowDuration, pos);
  266. return 0;
  267. }
  268. int
  269. mesh_add_vendor_ies(struct sk_buff *skb, struct ieee80211_sub_if_data *sdata)
  270. {
  271. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  272. u8 offset, len;
  273. const u8 *data;
  274. if (!ifmsh->ie || !ifmsh->ie_len)
  275. return 0;
  276. /* fast-forward to vendor IEs */
  277. offset = ieee80211_ie_split_vendor(ifmsh->ie, ifmsh->ie_len, 0);
  278. if (offset) {
  279. len = ifmsh->ie_len - offset;
  280. data = ifmsh->ie + offset;
  281. if (skb_tailroom(skb) < len)
  282. return -ENOMEM;
  283. memcpy(skb_put(skb, len), data, len);
  284. }
  285. return 0;
  286. }
  287. int
  288. mesh_add_rsn_ie(struct sk_buff *skb, struct ieee80211_sub_if_data *sdata)
  289. {
  290. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  291. u8 len = 0;
  292. const u8 *data;
  293. if (!ifmsh->ie || !ifmsh->ie_len)
  294. return 0;
  295. /* find RSN IE */
  296. data = ifmsh->ie;
  297. while (data < ifmsh->ie + ifmsh->ie_len) {
  298. if (*data == WLAN_EID_RSN) {
  299. len = data[1] + 2;
  300. break;
  301. }
  302. data++;
  303. }
  304. if (len) {
  305. if (skb_tailroom(skb) < len)
  306. return -ENOMEM;
  307. memcpy(skb_put(skb, len), data, len);
  308. }
  309. return 0;
  310. }
  311. int mesh_add_ds_params_ie(struct sk_buff *skb,
  312. struct ieee80211_sub_if_data *sdata)
  313. {
  314. struct ieee80211_local *local = sdata->local;
  315. struct ieee80211_supported_band *sband;
  316. struct ieee80211_chanctx_conf *chanctx_conf;
  317. struct ieee80211_channel *chan;
  318. u8 *pos;
  319. if (skb_tailroom(skb) < 3)
  320. return -ENOMEM;
  321. rcu_read_lock();
  322. chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
  323. if (WARN_ON(!chanctx_conf)) {
  324. rcu_read_unlock();
  325. return -EINVAL;
  326. }
  327. chan = chanctx_conf->def.chan;
  328. rcu_read_unlock();
  329. sband = local->hw.wiphy->bands[chan->band];
  330. if (sband->band == IEEE80211_BAND_2GHZ) {
  331. pos = skb_put(skb, 2 + 1);
  332. *pos++ = WLAN_EID_DS_PARAMS;
  333. *pos++ = 1;
  334. *pos++ = ieee80211_frequency_to_channel(chan->center_freq);
  335. }
  336. return 0;
  337. }
  338. int mesh_add_ht_cap_ie(struct sk_buff *skb,
  339. struct ieee80211_sub_if_data *sdata)
  340. {
  341. struct ieee80211_local *local = sdata->local;
  342. enum ieee80211_band band = ieee80211_get_sdata_band(sdata);
  343. struct ieee80211_supported_band *sband;
  344. u8 *pos;
  345. sband = local->hw.wiphy->bands[band];
  346. if (!sband->ht_cap.ht_supported ||
  347. sdata->vif.bss_conf.chandef.width == NL80211_CHAN_WIDTH_20_NOHT)
  348. return 0;
  349. if (skb_tailroom(skb) < 2 + sizeof(struct ieee80211_ht_cap))
  350. return -ENOMEM;
  351. pos = skb_put(skb, 2 + sizeof(struct ieee80211_ht_cap));
  352. ieee80211_ie_build_ht_cap(pos, &sband->ht_cap, sband->ht_cap.cap);
  353. return 0;
  354. }
  355. int mesh_add_ht_oper_ie(struct sk_buff *skb,
  356. struct ieee80211_sub_if_data *sdata)
  357. {
  358. struct ieee80211_local *local = sdata->local;
  359. struct ieee80211_chanctx_conf *chanctx_conf;
  360. struct ieee80211_channel *channel;
  361. enum nl80211_channel_type channel_type =
  362. cfg80211_get_chandef_type(&sdata->vif.bss_conf.chandef);
  363. struct ieee80211_supported_band *sband;
  364. struct ieee80211_sta_ht_cap *ht_cap;
  365. u8 *pos;
  366. rcu_read_lock();
  367. chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
  368. if (WARN_ON(!chanctx_conf)) {
  369. rcu_read_unlock();
  370. return -EINVAL;
  371. }
  372. channel = chanctx_conf->def.chan;
  373. rcu_read_unlock();
  374. sband = local->hw.wiphy->bands[channel->band];
  375. ht_cap = &sband->ht_cap;
  376. if (!ht_cap->ht_supported || channel_type == NL80211_CHAN_NO_HT)
  377. return 0;
  378. if (skb_tailroom(skb) < 2 + sizeof(struct ieee80211_ht_operation))
  379. return -ENOMEM;
  380. pos = skb_put(skb, 2 + sizeof(struct ieee80211_ht_operation));
  381. ieee80211_ie_build_ht_oper(pos, ht_cap, &sdata->vif.bss_conf.chandef,
  382. sdata->vif.bss_conf.ht_operation_mode);
  383. return 0;
  384. }
  385. static void ieee80211_mesh_path_timer(unsigned long data)
  386. {
  387. struct ieee80211_sub_if_data *sdata =
  388. (struct ieee80211_sub_if_data *) data;
  389. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  390. struct ieee80211_local *local = sdata->local;
  391. if (local->quiescing) {
  392. set_bit(TMR_RUNNING_MP, &ifmsh->timers_running);
  393. return;
  394. }
  395. ieee80211_queue_work(&local->hw, &sdata->work);
  396. }
  397. static void ieee80211_mesh_path_root_timer(unsigned long data)
  398. {
  399. struct ieee80211_sub_if_data *sdata =
  400. (struct ieee80211_sub_if_data *) data;
  401. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  402. struct ieee80211_local *local = sdata->local;
  403. set_bit(MESH_WORK_ROOT, &ifmsh->wrkq_flags);
  404. if (local->quiescing) {
  405. set_bit(TMR_RUNNING_MPR, &ifmsh->timers_running);
  406. return;
  407. }
  408. ieee80211_queue_work(&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. * @meshhdr: uninitialized mesh header
  453. * @sdata: mesh interface to be used
  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 ieee80211s_hdr *meshhdr,
  463. struct ieee80211_sub_if_data *sdata, char *addr4or5,
  464. char *addr6)
  465. {
  466. int aelen = 0;
  467. BUG_ON(!addr4or5 && addr6);
  468. memset(meshhdr, 0, sizeof(*meshhdr));
  469. meshhdr->ttl = sdata->u.mesh.mshcfg.dot11MeshTTL;
  470. put_unaligned(cpu_to_le32(sdata->u.mesh.mesh_seqnum), &meshhdr->seqnum);
  471. sdata->u.mesh.mesh_seqnum++;
  472. if (addr4or5 && !addr6) {
  473. meshhdr->flags |= MESH_FLAGS_AE_A4;
  474. aelen += ETH_ALEN;
  475. memcpy(meshhdr->eaddr1, addr4or5, ETH_ALEN);
  476. } else if (addr4or5 && addr6) {
  477. meshhdr->flags |= MESH_FLAGS_AE_A5_A6;
  478. aelen += 2 * ETH_ALEN;
  479. memcpy(meshhdr->eaddr1, addr4or5, ETH_ALEN);
  480. memcpy(meshhdr->eaddr2, addr6, ETH_ALEN);
  481. }
  482. return 6 + aelen;
  483. }
  484. static void ieee80211_mesh_housekeeping(struct ieee80211_sub_if_data *sdata,
  485. struct ieee80211_if_mesh *ifmsh)
  486. {
  487. u32 changed;
  488. ieee80211_sta_expire(sdata, IEEE80211_MESH_PEER_INACTIVITY_LIMIT);
  489. mesh_path_expire(sdata);
  490. changed = mesh_accept_plinks_update(sdata);
  491. ieee80211_bss_info_change_notify(sdata, changed);
  492. mod_timer(&ifmsh->housekeeping_timer,
  493. round_jiffies(jiffies + IEEE80211_MESH_HOUSEKEEPING_INTERVAL));
  494. }
  495. static void ieee80211_mesh_rootpath(struct ieee80211_sub_if_data *sdata)
  496. {
  497. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  498. u32 interval;
  499. mesh_path_tx_root_frame(sdata);
  500. if (ifmsh->mshcfg.dot11MeshHWMPRootMode == IEEE80211_PROACTIVE_RANN)
  501. interval = ifmsh->mshcfg.dot11MeshHWMPRannInterval;
  502. else
  503. interval = ifmsh->mshcfg.dot11MeshHWMProotInterval;
  504. mod_timer(&ifmsh->mesh_path_root_timer,
  505. round_jiffies(TU_TO_EXP_TIME(interval)));
  506. }
  507. #ifdef CONFIG_PM
  508. void ieee80211_mesh_quiesce(struct ieee80211_sub_if_data *sdata)
  509. {
  510. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  511. /* use atomic bitops in case all timers fire at the same time */
  512. if (del_timer_sync(&ifmsh->housekeeping_timer))
  513. set_bit(TMR_RUNNING_HK, &ifmsh->timers_running);
  514. if (del_timer_sync(&ifmsh->mesh_path_timer))
  515. set_bit(TMR_RUNNING_MP, &ifmsh->timers_running);
  516. if (del_timer_sync(&ifmsh->mesh_path_root_timer))
  517. set_bit(TMR_RUNNING_MPR, &ifmsh->timers_running);
  518. }
  519. void ieee80211_mesh_restart(struct ieee80211_sub_if_data *sdata)
  520. {
  521. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  522. if (test_and_clear_bit(TMR_RUNNING_HK, &ifmsh->timers_running))
  523. add_timer(&ifmsh->housekeeping_timer);
  524. if (test_and_clear_bit(TMR_RUNNING_MP, &ifmsh->timers_running))
  525. add_timer(&ifmsh->mesh_path_timer);
  526. if (test_and_clear_bit(TMR_RUNNING_MPR, &ifmsh->timers_running))
  527. add_timer(&ifmsh->mesh_path_root_timer);
  528. ieee80211_mesh_root_setup(ifmsh);
  529. }
  530. #endif
  531. void ieee80211_start_mesh(struct ieee80211_sub_if_data *sdata)
  532. {
  533. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  534. struct ieee80211_local *local = sdata->local;
  535. u32 changed = BSS_CHANGED_BEACON |
  536. BSS_CHANGED_BEACON_ENABLED |
  537. BSS_CHANGED_HT |
  538. BSS_CHANGED_BASIC_RATES |
  539. BSS_CHANGED_BEACON_INT;
  540. enum ieee80211_band band = ieee80211_get_sdata_band(sdata);
  541. local->fif_other_bss++;
  542. /* mesh ifaces must set allmulti to forward mcast traffic */
  543. atomic_inc(&local->iff_allmultis);
  544. ieee80211_configure_filter(local);
  545. ifmsh->mesh_cc_id = 0; /* Disabled */
  546. ifmsh->mesh_auth_id = 0; /* Disabled */
  547. /* register sync ops from extensible synchronization framework */
  548. ifmsh->sync_ops = ieee80211_mesh_sync_ops_get(ifmsh->mesh_sp_id);
  549. ifmsh->adjusting_tbtt = false;
  550. ifmsh->sync_offset_clockdrift_max = 0;
  551. set_bit(MESH_WORK_HOUSEKEEPING, &ifmsh->wrkq_flags);
  552. ieee80211_mesh_root_setup(ifmsh);
  553. ieee80211_queue_work(&local->hw, &sdata->work);
  554. sdata->vif.bss_conf.ht_operation_mode =
  555. ifmsh->mshcfg.ht_opmode;
  556. sdata->vif.bss_conf.enable_beacon = true;
  557. sdata->vif.bss_conf.basic_rates =
  558. ieee80211_mandatory_rates(local, band);
  559. ieee80211_mps_local_status_update(sdata);
  560. ieee80211_bss_info_change_notify(sdata, changed);
  561. netif_carrier_on(sdata->dev);
  562. }
  563. void ieee80211_stop_mesh(struct ieee80211_sub_if_data *sdata)
  564. {
  565. struct ieee80211_local *local = sdata->local;
  566. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  567. netif_carrier_off(sdata->dev);
  568. /* stop the beacon */
  569. ifmsh->mesh_id_len = 0;
  570. sdata->vif.bss_conf.enable_beacon = false;
  571. clear_bit(SDATA_STATE_OFFCHANNEL_BEACON_STOPPED, &sdata->state);
  572. ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BEACON_ENABLED);
  573. /* flush STAs and mpaths on this iface */
  574. sta_info_flush(sdata);
  575. mesh_path_flush_by_iface(sdata);
  576. /* free all potentially still buffered group-addressed frames */
  577. local->total_ps_buffered -= skb_queue_len(&ifmsh->ps.bc_buf);
  578. skb_queue_purge(&ifmsh->ps.bc_buf);
  579. del_timer_sync(&sdata->u.mesh.housekeeping_timer);
  580. del_timer_sync(&sdata->u.mesh.mesh_path_root_timer);
  581. del_timer_sync(&sdata->u.mesh.mesh_path_timer);
  582. /*
  583. * If the timer fired while we waited for it, it will have
  584. * requeued the work. Now the work will be running again
  585. * but will not rearm the timer again because it checks
  586. * whether the interface is running, which, at this point,
  587. * it no longer is.
  588. */
  589. cancel_work_sync(&sdata->work);
  590. local->fif_other_bss--;
  591. atomic_dec(&local->iff_allmultis);
  592. ieee80211_configure_filter(local);
  593. sdata->u.mesh.timers_running = 0;
  594. }
  595. static void ieee80211_mesh_rx_bcn_presp(struct ieee80211_sub_if_data *sdata,
  596. u16 stype,
  597. struct ieee80211_mgmt *mgmt,
  598. size_t len,
  599. struct ieee80211_rx_status *rx_status)
  600. {
  601. struct ieee80211_local *local = sdata->local;
  602. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  603. struct ieee802_11_elems elems;
  604. struct ieee80211_channel *channel;
  605. size_t baselen;
  606. int freq;
  607. enum ieee80211_band band = rx_status->band;
  608. /* ignore ProbeResp to foreign address */
  609. if (stype == IEEE80211_STYPE_PROBE_RESP &&
  610. !ether_addr_equal(mgmt->da, sdata->vif.addr))
  611. return;
  612. baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt;
  613. if (baselen > len)
  614. return;
  615. ieee802_11_parse_elems(mgmt->u.probe_resp.variable, len - baselen,
  616. &elems);
  617. /* ignore non-mesh or secure / unsecure mismatch */
  618. if ((!elems.mesh_id || !elems.mesh_config) ||
  619. (elems.rsn && sdata->u.mesh.security == IEEE80211_MESH_SEC_NONE) ||
  620. (!elems.rsn && sdata->u.mesh.security != IEEE80211_MESH_SEC_NONE))
  621. return;
  622. if (elems.ds_params && elems.ds_params_len == 1)
  623. freq = ieee80211_channel_to_frequency(elems.ds_params[0], band);
  624. else
  625. freq = rx_status->freq;
  626. channel = ieee80211_get_channel(local->hw.wiphy, freq);
  627. if (!channel || channel->flags & IEEE80211_CHAN_DISABLED)
  628. return;
  629. if (mesh_matches_local(sdata, &elems))
  630. mesh_neighbour_update(sdata, mgmt->sa, &elems);
  631. if (ifmsh->sync_ops)
  632. ifmsh->sync_ops->rx_bcn_presp(sdata,
  633. stype, mgmt, &elems, rx_status);
  634. }
  635. static void ieee80211_mesh_rx_mgmt_action(struct ieee80211_sub_if_data *sdata,
  636. struct ieee80211_mgmt *mgmt,
  637. size_t len,
  638. struct ieee80211_rx_status *rx_status)
  639. {
  640. switch (mgmt->u.action.category) {
  641. case WLAN_CATEGORY_SELF_PROTECTED:
  642. switch (mgmt->u.action.u.self_prot.action_code) {
  643. case WLAN_SP_MESH_PEERING_OPEN:
  644. case WLAN_SP_MESH_PEERING_CLOSE:
  645. case WLAN_SP_MESH_PEERING_CONFIRM:
  646. mesh_rx_plink_frame(sdata, mgmt, len, rx_status);
  647. break;
  648. }
  649. break;
  650. case WLAN_CATEGORY_MESH_ACTION:
  651. if (mesh_action_is_path_sel(mgmt))
  652. mesh_rx_path_sel_frame(sdata, mgmt, len);
  653. break;
  654. }
  655. }
  656. void ieee80211_mesh_rx_queued_mgmt(struct ieee80211_sub_if_data *sdata,
  657. struct sk_buff *skb)
  658. {
  659. struct ieee80211_rx_status *rx_status;
  660. struct ieee80211_mgmt *mgmt;
  661. u16 stype;
  662. rx_status = IEEE80211_SKB_RXCB(skb);
  663. mgmt = (struct ieee80211_mgmt *) skb->data;
  664. stype = le16_to_cpu(mgmt->frame_control) & IEEE80211_FCTL_STYPE;
  665. switch (stype) {
  666. case IEEE80211_STYPE_PROBE_RESP:
  667. case IEEE80211_STYPE_BEACON:
  668. ieee80211_mesh_rx_bcn_presp(sdata, stype, mgmt, skb->len,
  669. rx_status);
  670. break;
  671. case IEEE80211_STYPE_ACTION:
  672. ieee80211_mesh_rx_mgmt_action(sdata, mgmt, skb->len, rx_status);
  673. break;
  674. }
  675. }
  676. void ieee80211_mesh_work(struct ieee80211_sub_if_data *sdata)
  677. {
  678. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  679. if (ifmsh->preq_queue_len &&
  680. time_after(jiffies,
  681. ifmsh->last_preq + msecs_to_jiffies(ifmsh->mshcfg.dot11MeshHWMPpreqMinInterval)))
  682. mesh_path_start_discovery(sdata);
  683. if (test_and_clear_bit(MESH_WORK_GROW_MPATH_TABLE, &ifmsh->wrkq_flags))
  684. mesh_mpath_table_grow();
  685. if (test_and_clear_bit(MESH_WORK_GROW_MPP_TABLE, &ifmsh->wrkq_flags))
  686. mesh_mpp_table_grow();
  687. if (test_and_clear_bit(MESH_WORK_HOUSEKEEPING, &ifmsh->wrkq_flags))
  688. ieee80211_mesh_housekeeping(sdata, ifmsh);
  689. if (test_and_clear_bit(MESH_WORK_ROOT, &ifmsh->wrkq_flags))
  690. ieee80211_mesh_rootpath(sdata);
  691. if (test_and_clear_bit(MESH_WORK_DRIFT_ADJUST, &ifmsh->wrkq_flags))
  692. mesh_sync_adjust_tbtt(sdata);
  693. }
  694. void ieee80211_mesh_notify_scan_completed(struct ieee80211_local *local)
  695. {
  696. struct ieee80211_sub_if_data *sdata;
  697. rcu_read_lock();
  698. list_for_each_entry_rcu(sdata, &local->interfaces, list)
  699. if (ieee80211_vif_is_mesh(&sdata->vif))
  700. ieee80211_queue_work(&local->hw, &sdata->work);
  701. rcu_read_unlock();
  702. }
  703. void ieee80211_mesh_init_sdata(struct ieee80211_sub_if_data *sdata)
  704. {
  705. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  706. static u8 zero_addr[ETH_ALEN] = {};
  707. setup_timer(&ifmsh->housekeeping_timer,
  708. ieee80211_mesh_housekeeping_timer,
  709. (unsigned long) sdata);
  710. ifmsh->accepting_plinks = true;
  711. ifmsh->preq_id = 0;
  712. ifmsh->sn = 0;
  713. ifmsh->num_gates = 0;
  714. atomic_set(&ifmsh->mpaths, 0);
  715. mesh_rmc_init(sdata);
  716. ifmsh->last_preq = jiffies;
  717. ifmsh->next_perr = jiffies;
  718. /* Allocate all mesh structures when creating the first mesh interface. */
  719. if (!mesh_allocated)
  720. ieee80211s_init();
  721. setup_timer(&ifmsh->mesh_path_timer,
  722. ieee80211_mesh_path_timer,
  723. (unsigned long) sdata);
  724. setup_timer(&ifmsh->mesh_path_root_timer,
  725. ieee80211_mesh_path_root_timer,
  726. (unsigned long) sdata);
  727. INIT_LIST_HEAD(&ifmsh->preq_queue.list);
  728. skb_queue_head_init(&ifmsh->ps.bc_buf);
  729. spin_lock_init(&ifmsh->mesh_preq_queue_lock);
  730. spin_lock_init(&ifmsh->sync_offset_lock);
  731. sdata->vif.bss_conf.bssid = zero_addr;
  732. }