mesh.c 12 KB

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