mesh.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754
  1. /*
  2. * Copyright (c) 2008 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 <asm/unaligned.h>
  11. #include "ieee80211_i.h"
  12. #include "mesh.h"
  13. #define IEEE80211_MESH_PEER_INACTIVITY_LIMIT (1800 * HZ)
  14. #define IEEE80211_MESH_HOUSEKEEPING_INTERVAL (60 * HZ)
  15. #define PP_OFFSET 1 /* Path Selection Protocol */
  16. #define PM_OFFSET 5 /* Path Selection Metric */
  17. #define CC_OFFSET 9 /* Congestion Control Mode */
  18. #define CAPAB_OFFSET 17
  19. #define ACCEPT_PLINKS 0x80
  20. #define TMR_RUNNING_HK 0
  21. #define TMR_RUNNING_MP 1
  22. int mesh_allocated;
  23. static struct kmem_cache *rm_cache;
  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. ifmsh->housekeeping = true;
  42. if (local->quiescing) {
  43. set_bit(TMR_RUNNING_HK, &ifmsh->timers_running);
  44. return;
  45. }
  46. ieee80211_queue_work(&local->hw, &ifmsh->work);
  47. }
  48. /**
  49. * mesh_matches_local - check if the config of a mesh point matches ours
  50. *
  51. * @ie: information elements of a management frame from the mesh peer
  52. * @sdata: local mesh subif
  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 ieee802_11_elems *ie, struct ieee80211_sub_if_data *sdata)
  58. {
  59. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  60. /*
  61. * As support for each feature is added, check for matching
  62. * - On mesh config capabilities
  63. * - Power Save Support En
  64. * - Sync support enabled
  65. * - Sync support active
  66. * - Sync support required from peer
  67. * - MDA enabled
  68. * - Power management control on fc
  69. */
  70. if (ifmsh->mesh_id_len == ie->mesh_id_len &&
  71. memcmp(ifmsh->mesh_id, ie->mesh_id, ie->mesh_id_len) == 0 &&
  72. memcmp(ifmsh->mesh_pp_id, ie->mesh_config + PP_OFFSET, 4) == 0 &&
  73. memcmp(ifmsh->mesh_pm_id, ie->mesh_config + PM_OFFSET, 4) == 0 &&
  74. memcmp(ifmsh->mesh_cc_id, ie->mesh_config + CC_OFFSET, 4) == 0)
  75. return true;
  76. return false;
  77. }
  78. /**
  79. * mesh_peer_accepts_plinks - check if an mp is willing to establish peer links
  80. *
  81. * @ie: information elements of a management frame from the mesh peer
  82. */
  83. bool mesh_peer_accepts_plinks(struct ieee802_11_elems *ie)
  84. {
  85. return (*(ie->mesh_config + CAPAB_OFFSET) & ACCEPT_PLINKS) != 0;
  86. }
  87. /**
  88. * mesh_accept_plinks_update: update accepting_plink in local mesh beacons
  89. *
  90. * @sdata: mesh interface in which mesh beacons are going to be updated
  91. */
  92. void mesh_accept_plinks_update(struct ieee80211_sub_if_data *sdata)
  93. {
  94. bool free_plinks;
  95. /* In case mesh_plink_free_count > 0 and mesh_plinktbl_capacity == 0,
  96. * the mesh interface might be able to establish plinks with peers that
  97. * are already on the table but are not on PLINK_ESTAB state. However,
  98. * in general the mesh interface is not accepting peer link requests
  99. * from new peers, and that must be reflected in the beacon
  100. */
  101. free_plinks = mesh_plink_availables(sdata);
  102. if (free_plinks != sdata->u.mesh.accepting_plinks)
  103. ieee80211_mesh_housekeeping_timer((unsigned long) sdata);
  104. }
  105. void mesh_ids_set_default(struct ieee80211_if_mesh *sta)
  106. {
  107. u8 def_id[4] = {0x00, 0x0F, 0xAC, 0xff};
  108. memcpy(sta->mesh_pp_id, def_id, 4);
  109. memcpy(sta->mesh_pm_id, def_id, 4);
  110. memcpy(sta->mesh_cc_id, def_id, 4);
  111. }
  112. int mesh_rmc_init(struct ieee80211_sub_if_data *sdata)
  113. {
  114. int i;
  115. sdata->u.mesh.rmc = kmalloc(sizeof(struct mesh_rmc), GFP_KERNEL);
  116. if (!sdata->u.mesh.rmc)
  117. return -ENOMEM;
  118. sdata->u.mesh.rmc->idx_mask = RMC_BUCKETS - 1;
  119. for (i = 0; i < RMC_BUCKETS; i++)
  120. INIT_LIST_HEAD(&sdata->u.mesh.rmc->bucket[i].list);
  121. return 0;
  122. }
  123. void mesh_rmc_free(struct ieee80211_sub_if_data *sdata)
  124. {
  125. struct mesh_rmc *rmc = sdata->u.mesh.rmc;
  126. struct rmc_entry *p, *n;
  127. int i;
  128. if (!sdata->u.mesh.rmc)
  129. return;
  130. for (i = 0; i < RMC_BUCKETS; i++)
  131. list_for_each_entry_safe(p, n, &rmc->bucket[i].list, list) {
  132. list_del(&p->list);
  133. kmem_cache_free(rm_cache, p);
  134. }
  135. kfree(rmc);
  136. sdata->u.mesh.rmc = NULL;
  137. }
  138. /**
  139. * mesh_rmc_check - Check frame in recent multicast cache and add if absent.
  140. *
  141. * @sa: source address
  142. * @mesh_hdr: mesh_header
  143. *
  144. * Returns: 0 if the frame is not in the cache, nonzero otherwise.
  145. *
  146. * Checks using the source address and the mesh sequence number if we have
  147. * received this frame lately. If the frame is not in the cache, it is added to
  148. * it.
  149. */
  150. int mesh_rmc_check(u8 *sa, struct ieee80211s_hdr *mesh_hdr,
  151. struct ieee80211_sub_if_data *sdata)
  152. {
  153. struct mesh_rmc *rmc = sdata->u.mesh.rmc;
  154. u32 seqnum = 0;
  155. int entries = 0;
  156. u8 idx;
  157. struct rmc_entry *p, *n;
  158. /* Don't care about endianness since only match matters */
  159. memcpy(&seqnum, &mesh_hdr->seqnum, sizeof(mesh_hdr->seqnum));
  160. idx = le32_to_cpu(mesh_hdr->seqnum) & rmc->idx_mask;
  161. list_for_each_entry_safe(p, n, &rmc->bucket[idx].list, list) {
  162. ++entries;
  163. if (time_after(jiffies, p->exp_time) ||
  164. (entries == RMC_QUEUE_MAX_LEN)) {
  165. list_del(&p->list);
  166. kmem_cache_free(rm_cache, p);
  167. --entries;
  168. } else if ((seqnum == p->seqnum)
  169. && (memcmp(sa, p->sa, ETH_ALEN) == 0))
  170. return -1;
  171. }
  172. p = kmem_cache_alloc(rm_cache, GFP_ATOMIC);
  173. if (!p) {
  174. printk(KERN_DEBUG "o11s: could not allocate RMC entry\n");
  175. return 0;
  176. }
  177. p->seqnum = seqnum;
  178. p->exp_time = jiffies + RMC_TIMEOUT;
  179. memcpy(p->sa, sa, ETH_ALEN);
  180. list_add(&p->list, &rmc->bucket[idx].list);
  181. return 0;
  182. }
  183. void mesh_mgmt_ies_add(struct sk_buff *skb, struct ieee80211_sub_if_data *sdata)
  184. {
  185. struct ieee80211_local *local = sdata->local;
  186. struct ieee80211_supported_band *sband;
  187. u8 *pos;
  188. int len, i, rate;
  189. sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
  190. len = sband->n_bitrates;
  191. if (len > 8)
  192. len = 8;
  193. pos = skb_put(skb, len + 2);
  194. *pos++ = WLAN_EID_SUPP_RATES;
  195. *pos++ = len;
  196. for (i = 0; i < len; i++) {
  197. rate = sband->bitrates[i].bitrate;
  198. *pos++ = (u8) (rate / 5);
  199. }
  200. if (sband->n_bitrates > len) {
  201. pos = skb_put(skb, sband->n_bitrates - len + 2);
  202. *pos++ = WLAN_EID_EXT_SUPP_RATES;
  203. *pos++ = sband->n_bitrates - len;
  204. for (i = len; i < sband->n_bitrates; i++) {
  205. rate = sband->bitrates[i].bitrate;
  206. *pos++ = (u8) (rate / 5);
  207. }
  208. }
  209. pos = skb_put(skb, 2 + sdata->u.mesh.mesh_id_len);
  210. *pos++ = WLAN_EID_MESH_ID;
  211. *pos++ = sdata->u.mesh.mesh_id_len;
  212. if (sdata->u.mesh.mesh_id_len)
  213. memcpy(pos, sdata->u.mesh.mesh_id, sdata->u.mesh.mesh_id_len);
  214. pos = skb_put(skb, 21);
  215. *pos++ = WLAN_EID_MESH_CONFIG;
  216. *pos++ = IEEE80211_MESH_CONFIG_LEN;
  217. /* Version */
  218. *pos++ = 1;
  219. /* Active path selection protocol ID */
  220. memcpy(pos, sdata->u.mesh.mesh_pp_id, 4);
  221. pos += 4;
  222. /* Active path selection metric ID */
  223. memcpy(pos, sdata->u.mesh.mesh_pm_id, 4);
  224. pos += 4;
  225. /* Congestion control mode identifier */
  226. memcpy(pos, sdata->u.mesh.mesh_cc_id, 4);
  227. pos += 4;
  228. /* Channel precedence:
  229. * Not running simple channel unification protocol
  230. */
  231. memset(pos, 0x00, 4);
  232. pos += 4;
  233. /* Mesh capability */
  234. sdata->u.mesh.accepting_plinks = mesh_plink_availables(sdata);
  235. *pos++ = sdata->u.mesh.accepting_plinks ? ACCEPT_PLINKS : 0x00;
  236. *pos++ = 0x00;
  237. return;
  238. }
  239. u32 mesh_table_hash(u8 *addr, struct ieee80211_sub_if_data *sdata, struct mesh_table *tbl)
  240. {
  241. /* Use last four bytes of hw addr and interface index as hash index */
  242. return jhash_2words(*(u32 *)(addr+2), sdata->dev->ifindex, tbl->hash_rnd)
  243. & tbl->hash_mask;
  244. }
  245. struct mesh_table *mesh_table_alloc(int size_order)
  246. {
  247. int i;
  248. struct mesh_table *newtbl;
  249. newtbl = kmalloc(sizeof(struct mesh_table), GFP_KERNEL);
  250. if (!newtbl)
  251. return NULL;
  252. newtbl->hash_buckets = kzalloc(sizeof(struct hlist_head) *
  253. (1 << size_order), GFP_KERNEL);
  254. if (!newtbl->hash_buckets) {
  255. kfree(newtbl);
  256. return NULL;
  257. }
  258. newtbl->hashwlock = kmalloc(sizeof(spinlock_t) *
  259. (1 << size_order), GFP_KERNEL);
  260. if (!newtbl->hashwlock) {
  261. kfree(newtbl->hash_buckets);
  262. kfree(newtbl);
  263. return NULL;
  264. }
  265. newtbl->size_order = size_order;
  266. newtbl->hash_mask = (1 << size_order) - 1;
  267. atomic_set(&newtbl->entries, 0);
  268. get_random_bytes(&newtbl->hash_rnd,
  269. sizeof(newtbl->hash_rnd));
  270. for (i = 0; i <= newtbl->hash_mask; i++)
  271. spin_lock_init(&newtbl->hashwlock[i]);
  272. return newtbl;
  273. }
  274. static void __mesh_table_free(struct mesh_table *tbl)
  275. {
  276. kfree(tbl->hash_buckets);
  277. kfree(tbl->hashwlock);
  278. kfree(tbl);
  279. }
  280. void mesh_table_free(struct mesh_table *tbl, bool free_leafs)
  281. {
  282. struct hlist_head *mesh_hash;
  283. struct hlist_node *p, *q;
  284. int i;
  285. mesh_hash = tbl->hash_buckets;
  286. for (i = 0; i <= tbl->hash_mask; i++) {
  287. spin_lock(&tbl->hashwlock[i]);
  288. hlist_for_each_safe(p, q, &mesh_hash[i]) {
  289. tbl->free_node(p, free_leafs);
  290. atomic_dec(&tbl->entries);
  291. }
  292. spin_unlock(&tbl->hashwlock[i]);
  293. }
  294. __mesh_table_free(tbl);
  295. }
  296. static void ieee80211_mesh_path_timer(unsigned long data)
  297. {
  298. struct ieee80211_sub_if_data *sdata =
  299. (struct ieee80211_sub_if_data *) data;
  300. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  301. struct ieee80211_local *local = sdata->local;
  302. if (local->quiescing) {
  303. set_bit(TMR_RUNNING_MP, &ifmsh->timers_running);
  304. return;
  305. }
  306. ieee80211_queue_work(&local->hw, &ifmsh->work);
  307. }
  308. struct mesh_table *mesh_table_grow(struct mesh_table *tbl)
  309. {
  310. struct mesh_table *newtbl;
  311. struct hlist_head *oldhash;
  312. struct hlist_node *p, *q;
  313. int i;
  314. if (atomic_read(&tbl->entries)
  315. < tbl->mean_chain_len * (tbl->hash_mask + 1))
  316. goto endgrow;
  317. newtbl = mesh_table_alloc(tbl->size_order + 1);
  318. if (!newtbl)
  319. goto endgrow;
  320. newtbl->free_node = tbl->free_node;
  321. newtbl->mean_chain_len = tbl->mean_chain_len;
  322. newtbl->copy_node = tbl->copy_node;
  323. atomic_set(&newtbl->entries, atomic_read(&tbl->entries));
  324. oldhash = tbl->hash_buckets;
  325. for (i = 0; i <= tbl->hash_mask; i++)
  326. hlist_for_each(p, &oldhash[i])
  327. if (tbl->copy_node(p, newtbl) < 0)
  328. goto errcopy;
  329. return newtbl;
  330. errcopy:
  331. for (i = 0; i <= newtbl->hash_mask; i++) {
  332. hlist_for_each_safe(p, q, &newtbl->hash_buckets[i])
  333. tbl->free_node(p, 0);
  334. }
  335. __mesh_table_free(newtbl);
  336. endgrow:
  337. return NULL;
  338. }
  339. /**
  340. * ieee80211_fill_mesh_addresses - fill addresses of a locally originated mesh frame
  341. * @hdr: 802.11 frame header
  342. * @fc: frame control field
  343. * @meshda: destination address in the mesh
  344. * @meshsa: source address address in the mesh. Same as TA, as frame is
  345. * locally originated.
  346. *
  347. * Return the length of the 802.11 (does not include a mesh control header)
  348. */
  349. int ieee80211_fill_mesh_addresses(struct ieee80211_hdr *hdr, __le16 *fc, char
  350. *meshda, char *meshsa) {
  351. if (is_multicast_ether_addr(meshda)) {
  352. *fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS);
  353. /* DA TA SA */
  354. memcpy(hdr->addr1, meshda, ETH_ALEN);
  355. memcpy(hdr->addr2, meshsa, ETH_ALEN);
  356. memcpy(hdr->addr3, meshsa, ETH_ALEN);
  357. return 24;
  358. } else {
  359. *fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS |
  360. IEEE80211_FCTL_TODS);
  361. /* RA TA DA SA */
  362. memset(hdr->addr1, 0, ETH_ALEN); /* RA is resolved later */
  363. memcpy(hdr->addr2, meshsa, ETH_ALEN);
  364. memcpy(hdr->addr3, meshda, ETH_ALEN);
  365. memcpy(hdr->addr4, meshsa, ETH_ALEN);
  366. return 30;
  367. }
  368. }
  369. /**
  370. * ieee80211_new_mesh_header - create a new mesh header
  371. * @meshhdr: uninitialized mesh header
  372. * @sdata: mesh interface to be used
  373. * @addr4: addr4 of the mesh frame (1st in ae header)
  374. * may be NULL
  375. * @addr5: addr5 of the mesh frame (1st or 2nd in ae header)
  376. * may be NULL unless addr6 is present
  377. * @addr6: addr6 of the mesh frame (2nd or 3rd in ae header)
  378. * may be NULL unless addr5 is present
  379. *
  380. * Return the header length.
  381. */
  382. int ieee80211_new_mesh_header(struct ieee80211s_hdr *meshhdr,
  383. struct ieee80211_sub_if_data *sdata, char *addr4,
  384. char *addr5, char *addr6)
  385. {
  386. int aelen = 0;
  387. memset(meshhdr, 0, sizeof(meshhdr));
  388. meshhdr->ttl = sdata->u.mesh.mshcfg.dot11MeshTTL;
  389. put_unaligned(cpu_to_le32(sdata->u.mesh.mesh_seqnum), &meshhdr->seqnum);
  390. sdata->u.mesh.mesh_seqnum++;
  391. if (addr4) {
  392. meshhdr->flags |= MESH_FLAGS_AE_A4;
  393. aelen += ETH_ALEN;
  394. memcpy(meshhdr->eaddr1, addr4, ETH_ALEN);
  395. }
  396. if (addr5 && addr6) {
  397. meshhdr->flags |= MESH_FLAGS_AE_A5_A6;
  398. aelen += 2 * ETH_ALEN;
  399. if (!addr4) {
  400. memcpy(meshhdr->eaddr1, addr5, ETH_ALEN);
  401. memcpy(meshhdr->eaddr2, addr6, ETH_ALEN);
  402. } else {
  403. memcpy(meshhdr->eaddr2, addr5, ETH_ALEN);
  404. memcpy(meshhdr->eaddr3, addr6, ETH_ALEN);
  405. }
  406. }
  407. return 6 + aelen;
  408. }
  409. static void ieee80211_mesh_housekeeping(struct ieee80211_sub_if_data *sdata,
  410. struct ieee80211_if_mesh *ifmsh)
  411. {
  412. bool free_plinks;
  413. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  414. printk(KERN_DEBUG "%s: running mesh housekeeping\n",
  415. sdata->dev->name);
  416. #endif
  417. ieee80211_sta_expire(sdata, IEEE80211_MESH_PEER_INACTIVITY_LIMIT);
  418. mesh_path_expire(sdata);
  419. free_plinks = mesh_plink_availables(sdata);
  420. if (free_plinks != sdata->u.mesh.accepting_plinks)
  421. ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BEACON);
  422. ifmsh->housekeeping = false;
  423. mod_timer(&ifmsh->housekeeping_timer,
  424. round_jiffies(jiffies + IEEE80211_MESH_HOUSEKEEPING_INTERVAL));
  425. }
  426. #ifdef CONFIG_PM
  427. void ieee80211_mesh_quiesce(struct ieee80211_sub_if_data *sdata)
  428. {
  429. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  430. /* might restart the timer but that doesn't matter */
  431. cancel_work_sync(&ifmsh->work);
  432. /* use atomic bitops in case both timers fire at the same time */
  433. if (del_timer_sync(&ifmsh->housekeeping_timer))
  434. set_bit(TMR_RUNNING_HK, &ifmsh->timers_running);
  435. if (del_timer_sync(&ifmsh->mesh_path_timer))
  436. set_bit(TMR_RUNNING_MP, &ifmsh->timers_running);
  437. }
  438. void ieee80211_mesh_restart(struct ieee80211_sub_if_data *sdata)
  439. {
  440. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  441. if (test_and_clear_bit(TMR_RUNNING_HK, &ifmsh->timers_running))
  442. add_timer(&ifmsh->housekeeping_timer);
  443. if (test_and_clear_bit(TMR_RUNNING_MP, &ifmsh->timers_running))
  444. add_timer(&ifmsh->mesh_path_timer);
  445. }
  446. #endif
  447. void ieee80211_start_mesh(struct ieee80211_sub_if_data *sdata)
  448. {
  449. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  450. struct ieee80211_local *local = sdata->local;
  451. ifmsh->housekeeping = true;
  452. ieee80211_queue_work(&local->hw, &ifmsh->work);
  453. ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BEACON |
  454. BSS_CHANGED_BEACON_ENABLED);
  455. }
  456. void ieee80211_stop_mesh(struct ieee80211_sub_if_data *sdata)
  457. {
  458. del_timer_sync(&sdata->u.mesh.housekeeping_timer);
  459. /*
  460. * If the timer fired while we waited for it, it will have
  461. * requeued the work. Now the work will be running again
  462. * but will not rearm the timer again because it checks
  463. * whether the interface is running, which, at this point,
  464. * it no longer is.
  465. */
  466. cancel_work_sync(&sdata->u.mesh.work);
  467. /*
  468. * When we get here, the interface is marked down.
  469. * Call synchronize_rcu() to wait for the RX path
  470. * should it be using the interface and enqueuing
  471. * frames at this very time on another CPU.
  472. */
  473. rcu_barrier(); /* Wait for RX path and call_rcu()'s */
  474. skb_queue_purge(&sdata->u.mesh.skb_queue);
  475. }
  476. static void ieee80211_mesh_rx_bcn_presp(struct ieee80211_sub_if_data *sdata,
  477. u16 stype,
  478. struct ieee80211_mgmt *mgmt,
  479. size_t len,
  480. struct ieee80211_rx_status *rx_status)
  481. {
  482. struct ieee80211_local *local = sdata->local;
  483. struct ieee802_11_elems elems;
  484. struct ieee80211_channel *channel;
  485. u32 supp_rates = 0;
  486. size_t baselen;
  487. int freq;
  488. enum ieee80211_band band = rx_status->band;
  489. /* ignore ProbeResp to foreign address */
  490. if (stype == IEEE80211_STYPE_PROBE_RESP &&
  491. compare_ether_addr(mgmt->da, sdata->dev->dev_addr))
  492. return;
  493. baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt;
  494. if (baselen > len)
  495. return;
  496. ieee802_11_parse_elems(mgmt->u.probe_resp.variable, len - baselen,
  497. &elems);
  498. if (elems.ds_params && elems.ds_params_len == 1)
  499. freq = ieee80211_channel_to_frequency(elems.ds_params[0]);
  500. else
  501. freq = rx_status->freq;
  502. channel = ieee80211_get_channel(local->hw.wiphy, freq);
  503. if (!channel || channel->flags & IEEE80211_CHAN_DISABLED)
  504. return;
  505. if (elems.mesh_id && elems.mesh_config &&
  506. mesh_matches_local(&elems, sdata)) {
  507. supp_rates = ieee80211_sta_get_rates(local, &elems, band);
  508. mesh_neighbour_update(mgmt->sa, supp_rates, sdata,
  509. mesh_peer_accepts_plinks(&elems));
  510. }
  511. }
  512. static void ieee80211_mesh_rx_mgmt_action(struct ieee80211_sub_if_data *sdata,
  513. struct ieee80211_mgmt *mgmt,
  514. size_t len,
  515. struct ieee80211_rx_status *rx_status)
  516. {
  517. switch (mgmt->u.action.category) {
  518. case PLINK_CATEGORY:
  519. mesh_rx_plink_frame(sdata, mgmt, len, rx_status);
  520. break;
  521. case MESH_PATH_SEL_CATEGORY:
  522. mesh_rx_path_sel_frame(sdata, mgmt, len);
  523. break;
  524. }
  525. }
  526. static void ieee80211_mesh_rx_queued_mgmt(struct ieee80211_sub_if_data *sdata,
  527. struct sk_buff *skb)
  528. {
  529. struct ieee80211_rx_status *rx_status;
  530. struct ieee80211_if_mesh *ifmsh;
  531. struct ieee80211_mgmt *mgmt;
  532. u16 stype;
  533. ifmsh = &sdata->u.mesh;
  534. rx_status = IEEE80211_SKB_RXCB(skb);
  535. mgmt = (struct ieee80211_mgmt *) skb->data;
  536. stype = le16_to_cpu(mgmt->frame_control) & IEEE80211_FCTL_STYPE;
  537. switch (stype) {
  538. case IEEE80211_STYPE_PROBE_RESP:
  539. case IEEE80211_STYPE_BEACON:
  540. ieee80211_mesh_rx_bcn_presp(sdata, stype, mgmt, skb->len,
  541. rx_status);
  542. break;
  543. case IEEE80211_STYPE_ACTION:
  544. ieee80211_mesh_rx_mgmt_action(sdata, mgmt, skb->len, rx_status);
  545. break;
  546. }
  547. kfree_skb(skb);
  548. }
  549. static void ieee80211_mesh_work(struct work_struct *work)
  550. {
  551. struct ieee80211_sub_if_data *sdata =
  552. container_of(work, struct ieee80211_sub_if_data, u.mesh.work);
  553. struct ieee80211_local *local = sdata->local;
  554. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  555. struct sk_buff *skb;
  556. if (!netif_running(sdata->dev))
  557. return;
  558. if (local->scanning)
  559. return;
  560. while ((skb = skb_dequeue(&ifmsh->skb_queue)))
  561. ieee80211_mesh_rx_queued_mgmt(sdata, skb);
  562. if (ifmsh->preq_queue_len &&
  563. time_after(jiffies,
  564. ifmsh->last_preq + msecs_to_jiffies(ifmsh->mshcfg.dot11MeshHWMPpreqMinInterval)))
  565. mesh_path_start_discovery(sdata);
  566. if (ifmsh->housekeeping)
  567. ieee80211_mesh_housekeeping(sdata, ifmsh);
  568. }
  569. void ieee80211_mesh_notify_scan_completed(struct ieee80211_local *local)
  570. {
  571. struct ieee80211_sub_if_data *sdata;
  572. rcu_read_lock();
  573. list_for_each_entry_rcu(sdata, &local->interfaces, list)
  574. if (ieee80211_vif_is_mesh(&sdata->vif))
  575. ieee80211_queue_work(&local->hw, &sdata->u.mesh.work);
  576. rcu_read_unlock();
  577. }
  578. void ieee80211_mesh_init_sdata(struct ieee80211_sub_if_data *sdata)
  579. {
  580. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  581. INIT_WORK(&ifmsh->work, ieee80211_mesh_work);
  582. setup_timer(&ifmsh->housekeeping_timer,
  583. ieee80211_mesh_housekeeping_timer,
  584. (unsigned long) sdata);
  585. skb_queue_head_init(&sdata->u.mesh.skb_queue);
  586. ifmsh->mshcfg.dot11MeshRetryTimeout = MESH_RET_T;
  587. ifmsh->mshcfg.dot11MeshConfirmTimeout = MESH_CONF_T;
  588. ifmsh->mshcfg.dot11MeshHoldingTimeout = MESH_HOLD_T;
  589. ifmsh->mshcfg.dot11MeshMaxRetries = MESH_MAX_RETR;
  590. ifmsh->mshcfg.dot11MeshTTL = MESH_TTL;
  591. ifmsh->mshcfg.auto_open_plinks = true;
  592. ifmsh->mshcfg.dot11MeshMaxPeerLinks =
  593. MESH_MAX_ESTAB_PLINKS;
  594. ifmsh->mshcfg.dot11MeshHWMPactivePathTimeout =
  595. MESH_PATH_TIMEOUT;
  596. ifmsh->mshcfg.dot11MeshHWMPpreqMinInterval =
  597. MESH_PREQ_MIN_INT;
  598. ifmsh->mshcfg.dot11MeshHWMPnetDiameterTraversalTime =
  599. MESH_DIAM_TRAVERSAL_TIME;
  600. ifmsh->mshcfg.dot11MeshHWMPmaxPREQretries =
  601. MESH_MAX_PREQ_RETRIES;
  602. ifmsh->mshcfg.path_refresh_time =
  603. MESH_PATH_REFRESH_TIME;
  604. ifmsh->mshcfg.min_discovery_timeout =
  605. MESH_MIN_DISCOVERY_TIMEOUT;
  606. ifmsh->accepting_plinks = true;
  607. ifmsh->preq_id = 0;
  608. ifmsh->dsn = 0;
  609. atomic_set(&ifmsh->mpaths, 0);
  610. mesh_rmc_init(sdata);
  611. ifmsh->last_preq = jiffies;
  612. /* Allocate all mesh structures when creating the first mesh interface. */
  613. if (!mesh_allocated)
  614. ieee80211s_init();
  615. mesh_ids_set_default(ifmsh);
  616. setup_timer(&ifmsh->mesh_path_timer,
  617. ieee80211_mesh_path_timer,
  618. (unsigned long) sdata);
  619. INIT_LIST_HEAD(&ifmsh->preq_queue.list);
  620. spin_lock_init(&ifmsh->mesh_preq_queue_lock);
  621. }
  622. ieee80211_rx_result
  623. ieee80211_mesh_rx_mgmt(struct ieee80211_sub_if_data *sdata, struct sk_buff *skb)
  624. {
  625. struct ieee80211_local *local = sdata->local;
  626. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  627. struct ieee80211_mgmt *mgmt;
  628. u16 fc;
  629. if (skb->len < 24)
  630. return RX_DROP_MONITOR;
  631. mgmt = (struct ieee80211_mgmt *) skb->data;
  632. fc = le16_to_cpu(mgmt->frame_control);
  633. switch (fc & IEEE80211_FCTL_STYPE) {
  634. case IEEE80211_STYPE_ACTION:
  635. if (skb->len < IEEE80211_MIN_ACTION_SIZE)
  636. return RX_DROP_MONITOR;
  637. /* fall through */
  638. case IEEE80211_STYPE_PROBE_RESP:
  639. case IEEE80211_STYPE_BEACON:
  640. skb_queue_tail(&ifmsh->skb_queue, skb);
  641. ieee80211_queue_work(&local->hw, &ifmsh->work);
  642. return RX_QUEUED;
  643. }
  644. return RX_CONTINUE;
  645. }