mesh.c 12 KB

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