mesh.c 23 KB

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