mesh.c 12 KB

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