mesh.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  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 PP_OFFSET 1 /* Path Selection Protocol */
  14. #define PM_OFFSET 5 /* Path Selection Metric */
  15. #define CC_OFFSET 9 /* Congestion Control Mode */
  16. #define CAPAB_OFFSET 17
  17. #define ACCEPT_PLINKS 0x80
  18. int mesh_allocated;
  19. static struct kmem_cache *rm_cache;
  20. void ieee80211s_init(void)
  21. {
  22. mesh_pathtbl_init();
  23. mesh_allocated = 1;
  24. rm_cache = kmem_cache_create("mesh_rmc", sizeof(struct rmc_entry),
  25. 0, 0, NULL);
  26. }
  27. void ieee80211s_stop(void)
  28. {
  29. mesh_pathtbl_unregister();
  30. kmem_cache_destroy(rm_cache);
  31. }
  32. /**
  33. * mesh_matches_local - check if the config of a mesh point matches ours
  34. *
  35. * @ie: information elements of a management frame from the mesh peer
  36. * @dev: local mesh interface
  37. *
  38. * This function checks if the mesh configuration of a mesh point matches the
  39. * local mesh configuration, i.e. if both nodes belong to the same mesh network.
  40. */
  41. bool mesh_matches_local(struct ieee802_11_elems *ie, struct net_device *dev)
  42. {
  43. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  44. struct ieee80211_if_sta *sta = &sdata->u.sta;
  45. /*
  46. * As support for each feature is added, check for matching
  47. * - On mesh config capabilities
  48. * - Power Save Support En
  49. * - Sync support enabled
  50. * - Sync support active
  51. * - Sync support required from peer
  52. * - MDA enabled
  53. * - Power management control on fc
  54. */
  55. if (sta->mesh_id_len == ie->mesh_id_len &&
  56. memcmp(sta->mesh_id, ie->mesh_id, ie->mesh_id_len) == 0 &&
  57. memcmp(sta->mesh_pp_id, ie->mesh_config + PP_OFFSET, 4) == 0 &&
  58. memcmp(sta->mesh_pm_id, ie->mesh_config + PM_OFFSET, 4) == 0 &&
  59. memcmp(sta->mesh_cc_id, ie->mesh_config + CC_OFFSET, 4) == 0)
  60. return true;
  61. return false;
  62. }
  63. /**
  64. * mesh_peer_accepts_plinks - check if an mp is willing to establish peer links
  65. *
  66. * @ie: information elements of a management frame from the mesh peer
  67. * @dev: local mesh interface
  68. */
  69. bool mesh_peer_accepts_plinks(struct ieee802_11_elems *ie,
  70. struct net_device *dev)
  71. {
  72. return (*(ie->mesh_config + CAPAB_OFFSET) & ACCEPT_PLINKS) != 0;
  73. }
  74. /**
  75. * mesh_accept_plinks_update: update accepting_plink in local mesh beacons
  76. *
  77. * @sdata: mesh interface in which mesh beacons are going to be updated
  78. */
  79. void mesh_accept_plinks_update(struct ieee80211_sub_if_data *sdata)
  80. {
  81. bool free_plinks;
  82. /* In case mesh_plink_free_count > 0 and mesh_plinktbl_capacity == 0,
  83. * the mesh interface might be able to establish plinks with peers that
  84. * are already on the table but are not on PLINK_ESTAB state. However,
  85. * in general the mesh interface is not accepting peer link requests
  86. * from new peers, and that must be reflected in the beacon
  87. */
  88. free_plinks = mesh_plink_availables(sdata);
  89. if (free_plinks != sdata->u.sta.accepting_plinks)
  90. ieee80211_sta_timer((unsigned long) sdata);
  91. }
  92. void mesh_ids_set_default(struct ieee80211_if_sta *sta)
  93. {
  94. u8 def_id[4] = {0x00, 0x0F, 0xAC, 0xff};
  95. memcpy(sta->mesh_pp_id, def_id, 4);
  96. memcpy(sta->mesh_pm_id, def_id, 4);
  97. memcpy(sta->mesh_cc_id, def_id, 4);
  98. }
  99. int mesh_rmc_init(struct net_device *dev)
  100. {
  101. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  102. int i;
  103. sdata->u.sta.rmc = kmalloc(sizeof(struct mesh_rmc), GFP_KERNEL);
  104. if (!sdata->u.sta.rmc)
  105. return -ENOMEM;
  106. sdata->u.sta.rmc->idx_mask = RMC_BUCKETS - 1;
  107. for (i = 0; i < RMC_BUCKETS; i++)
  108. INIT_LIST_HEAD(&sdata->u.sta.rmc->bucket[i].list);
  109. return 0;
  110. }
  111. void mesh_rmc_free(struct net_device *dev)
  112. {
  113. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  114. struct mesh_rmc *rmc = sdata->u.sta.rmc;
  115. struct rmc_entry *p, *n;
  116. int i;
  117. if (!sdata->u.sta.rmc)
  118. return;
  119. for (i = 0; i < RMC_BUCKETS; i++)
  120. list_for_each_entry_safe(p, n, &rmc->bucket[i].list, list) {
  121. list_del(&p->list);
  122. kmem_cache_free(rm_cache, p);
  123. }
  124. kfree(rmc);
  125. sdata->u.sta.rmc = NULL;
  126. }
  127. /**
  128. * mesh_rmc_check - Check frame in recent multicast cache and add if absent.
  129. *
  130. * @sa: source address
  131. * @mesh_hdr: mesh_header
  132. *
  133. * Returns: 0 if the frame is not in the cache, nonzero otherwise.
  134. *
  135. * Checks using the source address and the mesh sequence number if we have
  136. * received this frame lately. If the frame is not in the cache, it is added to
  137. * it.
  138. */
  139. int mesh_rmc_check(u8 *sa, struct ieee80211s_hdr *mesh_hdr,
  140. struct net_device *dev)
  141. {
  142. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  143. struct mesh_rmc *rmc = sdata->u.sta.rmc;
  144. u32 seqnum = 0;
  145. int entries = 0;
  146. u8 idx;
  147. struct rmc_entry *p, *n;
  148. /* Don't care about endianness since only match matters */
  149. memcpy(&seqnum, &mesh_hdr->seqnum, sizeof(mesh_hdr->seqnum));
  150. idx = le32_to_cpu(mesh_hdr->seqnum) & rmc->idx_mask;
  151. list_for_each_entry_safe(p, n, &rmc->bucket[idx].list, list) {
  152. ++entries;
  153. if (time_after(jiffies, p->exp_time) ||
  154. (entries == RMC_QUEUE_MAX_LEN)) {
  155. list_del(&p->list);
  156. kmem_cache_free(rm_cache, p);
  157. --entries;
  158. } else if ((seqnum == p->seqnum)
  159. && (memcmp(sa, p->sa, ETH_ALEN) == 0))
  160. return -1;
  161. }
  162. p = kmem_cache_alloc(rm_cache, GFP_ATOMIC);
  163. if (!p) {
  164. printk(KERN_DEBUG "o11s: could not allocate RMC entry\n");
  165. return 0;
  166. }
  167. p->seqnum = seqnum;
  168. p->exp_time = jiffies + RMC_TIMEOUT;
  169. memcpy(p->sa, sa, ETH_ALEN);
  170. list_add(&p->list, &rmc->bucket[idx].list);
  171. return 0;
  172. }
  173. void mesh_mgmt_ies_add(struct sk_buff *skb, struct net_device *dev)
  174. {
  175. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  176. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  177. struct ieee80211_supported_band *sband;
  178. u8 *pos;
  179. int len, i, rate;
  180. sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
  181. len = sband->n_bitrates;
  182. if (len > 8)
  183. len = 8;
  184. pos = skb_put(skb, len + 2);
  185. *pos++ = WLAN_EID_SUPP_RATES;
  186. *pos++ = len;
  187. for (i = 0; i < len; i++) {
  188. rate = sband->bitrates[i].bitrate;
  189. *pos++ = (u8) (rate / 5);
  190. }
  191. if (sband->n_bitrates > len) {
  192. pos = skb_put(skb, sband->n_bitrates - len + 2);
  193. *pos++ = WLAN_EID_EXT_SUPP_RATES;
  194. *pos++ = sband->n_bitrates - len;
  195. for (i = len; i < sband->n_bitrates; i++) {
  196. rate = sband->bitrates[i].bitrate;
  197. *pos++ = (u8) (rate / 5);
  198. }
  199. }
  200. pos = skb_put(skb, 2 + sdata->u.sta.mesh_id_len);
  201. *pos++ = WLAN_EID_MESH_ID;
  202. *pos++ = sdata->u.sta.mesh_id_len;
  203. if (sdata->u.sta.mesh_id_len)
  204. memcpy(pos, sdata->u.sta.mesh_id, sdata->u.sta.mesh_id_len);
  205. pos = skb_put(skb, 21);
  206. *pos++ = WLAN_EID_MESH_CONFIG;
  207. *pos++ = MESH_CFG_LEN;
  208. /* Version */
  209. *pos++ = 1;
  210. /* Active path selection protocol ID */
  211. memcpy(pos, sdata->u.sta.mesh_pp_id, 4);
  212. pos += 4;
  213. /* Active path selection metric ID */
  214. memcpy(pos, sdata->u.sta.mesh_pm_id, 4);
  215. pos += 4;
  216. /* Congestion control mode identifier */
  217. memcpy(pos, sdata->u.sta.mesh_cc_id, 4);
  218. pos += 4;
  219. /* Channel precedence:
  220. * Not running simple channel unification protocol
  221. */
  222. memset(pos, 0x00, 4);
  223. pos += 4;
  224. /* Mesh capability */
  225. sdata->u.sta.accepting_plinks = mesh_plink_availables(sdata);
  226. *pos++ = sdata->u.sta.accepting_plinks ? ACCEPT_PLINKS : 0x00;
  227. *pos++ = 0x00;
  228. return;
  229. }
  230. u32 mesh_table_hash(u8 *addr, struct net_device *dev, struct mesh_table *tbl)
  231. {
  232. /* Use last four bytes of hw addr and interface index as hash index */
  233. return jhash_2words(*(u32 *)(addr+2), dev->ifindex, tbl->hash_rnd)
  234. & tbl->hash_mask;
  235. }
  236. u8 mesh_id_hash(u8 *mesh_id, int mesh_id_len)
  237. {
  238. if (!mesh_id_len)
  239. return 1;
  240. else if (mesh_id_len == 1)
  241. return (u8) mesh_id[0];
  242. else
  243. return (u8) (mesh_id[0] + 2 * mesh_id[1]);
  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_sta *ifsta = &sdata->u.sta;
  301. struct ieee80211_local *local = wdev_priv(&sdata->wdev);
  302. queue_work(local->hw.workqueue, &ifsta->work);
  303. }
  304. struct mesh_table *mesh_table_grow(struct mesh_table *tbl)
  305. {
  306. struct mesh_table *newtbl;
  307. struct hlist_head *oldhash;
  308. struct hlist_node *p, *q;
  309. int i;
  310. if (atomic_read(&tbl->entries)
  311. < tbl->mean_chain_len * (tbl->hash_mask + 1))
  312. goto endgrow;
  313. newtbl = mesh_table_alloc(tbl->size_order + 1);
  314. if (!newtbl)
  315. goto endgrow;
  316. newtbl->free_node = tbl->free_node;
  317. newtbl->mean_chain_len = tbl->mean_chain_len;
  318. newtbl->copy_node = tbl->copy_node;
  319. atomic_set(&newtbl->entries, atomic_read(&tbl->entries));
  320. oldhash = tbl->hash_buckets;
  321. for (i = 0; i <= tbl->hash_mask; i++)
  322. hlist_for_each(p, &oldhash[i])
  323. if (tbl->copy_node(p, newtbl) < 0)
  324. goto errcopy;
  325. return newtbl;
  326. errcopy:
  327. for (i = 0; i <= newtbl->hash_mask; i++) {
  328. hlist_for_each_safe(p, q, &newtbl->hash_buckets[i])
  329. tbl->free_node(p, 0);
  330. }
  331. __mesh_table_free(newtbl);
  332. endgrow:
  333. return NULL;
  334. }
  335. /**
  336. * ieee80211_new_mesh_header - create a new mesh header
  337. * @meshhdr: uninitialized mesh header
  338. * @sdata: mesh interface to be used
  339. *
  340. * Return the header length.
  341. */
  342. int ieee80211_new_mesh_header(struct ieee80211s_hdr *meshhdr,
  343. struct ieee80211_sub_if_data *sdata)
  344. {
  345. meshhdr->flags = 0;
  346. meshhdr->ttl = sdata->u.sta.mshcfg.dot11MeshTTL;
  347. put_unaligned(cpu_to_le32(sdata->u.sta.mesh_seqnum), &meshhdr->seqnum);
  348. sdata->u.sta.mesh_seqnum++;
  349. return 6;
  350. }
  351. void ieee80211_mesh_init_sdata(struct ieee80211_sub_if_data *sdata)
  352. {
  353. struct ieee80211_if_sta *ifsta = &sdata->u.sta;
  354. ifsta->mshcfg.dot11MeshRetryTimeout = MESH_RET_T;
  355. ifsta->mshcfg.dot11MeshConfirmTimeout = MESH_CONF_T;
  356. ifsta->mshcfg.dot11MeshHoldingTimeout = MESH_HOLD_T;
  357. ifsta->mshcfg.dot11MeshMaxRetries = MESH_MAX_RETR;
  358. ifsta->mshcfg.dot11MeshTTL = MESH_TTL;
  359. ifsta->mshcfg.auto_open_plinks = true;
  360. ifsta->mshcfg.dot11MeshMaxPeerLinks =
  361. MESH_MAX_ESTAB_PLINKS;
  362. ifsta->mshcfg.dot11MeshHWMPactivePathTimeout =
  363. MESH_PATH_TIMEOUT;
  364. ifsta->mshcfg.dot11MeshHWMPpreqMinInterval =
  365. MESH_PREQ_MIN_INT;
  366. ifsta->mshcfg.dot11MeshHWMPnetDiameterTraversalTime =
  367. MESH_DIAM_TRAVERSAL_TIME;
  368. ifsta->mshcfg.dot11MeshHWMPmaxPREQretries =
  369. MESH_MAX_PREQ_RETRIES;
  370. ifsta->mshcfg.path_refresh_time =
  371. MESH_PATH_REFRESH_TIME;
  372. ifsta->mshcfg.min_discovery_timeout =
  373. MESH_MIN_DISCOVERY_TIMEOUT;
  374. ifsta->accepting_plinks = true;
  375. ifsta->preq_id = 0;
  376. ifsta->dsn = 0;
  377. atomic_set(&ifsta->mpaths, 0);
  378. mesh_rmc_init(sdata->dev);
  379. ifsta->last_preq = jiffies;
  380. /* Allocate all mesh structures when creating the first mesh interface. */
  381. if (!mesh_allocated)
  382. ieee80211s_init();
  383. mesh_ids_set_default(ifsta);
  384. setup_timer(&ifsta->mesh_path_timer,
  385. ieee80211_mesh_path_timer,
  386. (unsigned long) sdata);
  387. INIT_LIST_HEAD(&ifsta->preq_queue.list);
  388. spin_lock_init(&ifsta->mesh_preq_queue_lock);
  389. }