xfrm_ipcomp.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. /*
  2. * IP Payload Compression Protocol (IPComp) - RFC3173.
  3. *
  4. * Copyright (c) 2003 James Morris <jmorris@intercode.com.au>
  5. * Copyright (c) 2003-2008 Herbert Xu <herbert@gondor.apana.org.au>
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License as published by the Free
  9. * Software Foundation; either version 2 of the License, or (at your option)
  10. * any later version.
  11. *
  12. * Todo:
  13. * - Tunable compression parameters.
  14. * - Compression stats.
  15. * - Adaptive compression.
  16. */
  17. #include <linux/crypto.h>
  18. #include <linux/err.h>
  19. #include <linux/list.h>
  20. #include <linux/module.h>
  21. #include <linux/mutex.h>
  22. #include <linux/percpu.h>
  23. #include <linux/rtnetlink.h>
  24. #include <linux/smp.h>
  25. #include <linux/vmalloc.h>
  26. #include <net/ip.h>
  27. #include <net/ipcomp.h>
  28. #include <net/xfrm.h>
  29. struct ipcomp_tfms {
  30. struct list_head list;
  31. struct crypto_comp **tfms;
  32. int users;
  33. };
  34. static DEFINE_MUTEX(ipcomp_resource_mutex);
  35. static void **ipcomp_scratches;
  36. static int ipcomp_scratch_users;
  37. static LIST_HEAD(ipcomp_tfms_list);
  38. static int ipcomp_decompress(struct xfrm_state *x, struct sk_buff *skb)
  39. {
  40. struct ipcomp_data *ipcd = x->data;
  41. const int plen = skb->len;
  42. int dlen = IPCOMP_SCRATCH_SIZE;
  43. const u8 *start = skb->data;
  44. const int cpu = get_cpu();
  45. u8 *scratch = *per_cpu_ptr(ipcomp_scratches, cpu);
  46. struct crypto_comp *tfm = *per_cpu_ptr(ipcd->tfms, cpu);
  47. int err = crypto_comp_decompress(tfm, start, plen, scratch, &dlen);
  48. if (err)
  49. goto out;
  50. if (dlen < (plen + sizeof(struct ip_comp_hdr))) {
  51. err = -EINVAL;
  52. goto out;
  53. }
  54. err = pskb_expand_head(skb, 0, dlen - plen, GFP_ATOMIC);
  55. if (err)
  56. goto out;
  57. skb->truesize += dlen - plen;
  58. __skb_put(skb, dlen - plen);
  59. skb_copy_to_linear_data(skb, scratch, dlen);
  60. out:
  61. put_cpu();
  62. return err;
  63. }
  64. int ipcomp_input(struct xfrm_state *x, struct sk_buff *skb)
  65. {
  66. int nexthdr;
  67. int err = -ENOMEM;
  68. struct ip_comp_hdr *ipch;
  69. if (skb_linearize_cow(skb))
  70. goto out;
  71. skb->ip_summed = CHECKSUM_NONE;
  72. /* Remove ipcomp header and decompress original payload */
  73. ipch = (void *)skb->data;
  74. nexthdr = ipch->nexthdr;
  75. skb->transport_header = skb->network_header + sizeof(*ipch);
  76. __skb_pull(skb, sizeof(*ipch));
  77. err = ipcomp_decompress(x, skb);
  78. if (err)
  79. goto out;
  80. err = nexthdr;
  81. out:
  82. return err;
  83. }
  84. EXPORT_SYMBOL_GPL(ipcomp_input);
  85. static int ipcomp_compress(struct xfrm_state *x, struct sk_buff *skb)
  86. {
  87. struct ipcomp_data *ipcd = x->data;
  88. const int plen = skb->len;
  89. int dlen = IPCOMP_SCRATCH_SIZE;
  90. u8 *start = skb->data;
  91. const int cpu = get_cpu();
  92. u8 *scratch = *per_cpu_ptr(ipcomp_scratches, cpu);
  93. struct crypto_comp *tfm = *per_cpu_ptr(ipcd->tfms, cpu);
  94. int err;
  95. local_bh_disable();
  96. err = crypto_comp_compress(tfm, start, plen, scratch, &dlen);
  97. local_bh_enable();
  98. if (err)
  99. goto out;
  100. if ((dlen + sizeof(struct ip_comp_hdr)) >= plen) {
  101. err = -EMSGSIZE;
  102. goto out;
  103. }
  104. memcpy(start + sizeof(struct ip_comp_hdr), scratch, dlen);
  105. put_cpu();
  106. pskb_trim(skb, dlen + sizeof(struct ip_comp_hdr));
  107. return 0;
  108. out:
  109. put_cpu();
  110. return err;
  111. }
  112. int ipcomp_output(struct xfrm_state *x, struct sk_buff *skb)
  113. {
  114. int err;
  115. struct ip_comp_hdr *ipch;
  116. struct ipcomp_data *ipcd = x->data;
  117. if (skb->len < ipcd->threshold) {
  118. /* Don't bother compressing */
  119. goto out_ok;
  120. }
  121. if (skb_linearize_cow(skb))
  122. goto out_ok;
  123. err = ipcomp_compress(x, skb);
  124. if (err) {
  125. goto out_ok;
  126. }
  127. /* Install ipcomp header, convert into ipcomp datagram. */
  128. ipch = ip_comp_hdr(skb);
  129. ipch->nexthdr = *skb_mac_header(skb);
  130. ipch->flags = 0;
  131. ipch->cpi = htons((u16 )ntohl(x->id.spi));
  132. *skb_mac_header(skb) = IPPROTO_COMP;
  133. out_ok:
  134. skb_push(skb, -skb_network_offset(skb));
  135. return 0;
  136. }
  137. EXPORT_SYMBOL_GPL(ipcomp_output);
  138. static void ipcomp_free_scratches(void)
  139. {
  140. int i;
  141. void **scratches;
  142. if (--ipcomp_scratch_users)
  143. return;
  144. scratches = ipcomp_scratches;
  145. if (!scratches)
  146. return;
  147. for_each_possible_cpu(i)
  148. vfree(*per_cpu_ptr(scratches, i));
  149. free_percpu(scratches);
  150. }
  151. static void **ipcomp_alloc_scratches(void)
  152. {
  153. int i;
  154. void **scratches;
  155. if (ipcomp_scratch_users++)
  156. return ipcomp_scratches;
  157. scratches = alloc_percpu(void *);
  158. if (!scratches)
  159. return NULL;
  160. ipcomp_scratches = scratches;
  161. for_each_possible_cpu(i) {
  162. void *scratch = vmalloc(IPCOMP_SCRATCH_SIZE);
  163. if (!scratch)
  164. return NULL;
  165. *per_cpu_ptr(scratches, i) = scratch;
  166. }
  167. return scratches;
  168. }
  169. static void ipcomp_free_tfms(struct crypto_comp **tfms)
  170. {
  171. struct ipcomp_tfms *pos;
  172. int cpu;
  173. list_for_each_entry(pos, &ipcomp_tfms_list, list) {
  174. if (pos->tfms == tfms)
  175. break;
  176. }
  177. BUG_TRAP(pos);
  178. if (--pos->users)
  179. return;
  180. list_del(&pos->list);
  181. kfree(pos);
  182. if (!tfms)
  183. return;
  184. for_each_possible_cpu(cpu) {
  185. struct crypto_comp *tfm = *per_cpu_ptr(tfms, cpu);
  186. crypto_free_comp(tfm);
  187. }
  188. free_percpu(tfms);
  189. }
  190. static struct crypto_comp **ipcomp_alloc_tfms(const char *alg_name)
  191. {
  192. struct ipcomp_tfms *pos;
  193. struct crypto_comp **tfms;
  194. int cpu;
  195. /* This can be any valid CPU ID so we don't need locking. */
  196. cpu = raw_smp_processor_id();
  197. list_for_each_entry(pos, &ipcomp_tfms_list, list) {
  198. struct crypto_comp *tfm;
  199. tfms = pos->tfms;
  200. tfm = *per_cpu_ptr(tfms, cpu);
  201. if (!strcmp(crypto_comp_name(tfm), alg_name)) {
  202. pos->users++;
  203. return tfms;
  204. }
  205. }
  206. pos = kmalloc(sizeof(*pos), GFP_KERNEL);
  207. if (!pos)
  208. return NULL;
  209. pos->users = 1;
  210. INIT_LIST_HEAD(&pos->list);
  211. list_add(&pos->list, &ipcomp_tfms_list);
  212. pos->tfms = tfms = alloc_percpu(struct crypto_comp *);
  213. if (!tfms)
  214. goto error;
  215. for_each_possible_cpu(cpu) {
  216. struct crypto_comp *tfm = crypto_alloc_comp(alg_name, 0,
  217. CRYPTO_ALG_ASYNC);
  218. if (IS_ERR(tfm))
  219. goto error;
  220. *per_cpu_ptr(tfms, cpu) = tfm;
  221. }
  222. return tfms;
  223. error:
  224. ipcomp_free_tfms(tfms);
  225. return NULL;
  226. }
  227. static void ipcomp_free_data(struct ipcomp_data *ipcd)
  228. {
  229. if (ipcd->tfms)
  230. ipcomp_free_tfms(ipcd->tfms);
  231. ipcomp_free_scratches();
  232. }
  233. void ipcomp_destroy(struct xfrm_state *x)
  234. {
  235. struct ipcomp_data *ipcd = x->data;
  236. if (!ipcd)
  237. return;
  238. xfrm_state_delete_tunnel(x);
  239. mutex_lock(&ipcomp_resource_mutex);
  240. ipcomp_free_data(ipcd);
  241. mutex_unlock(&ipcomp_resource_mutex);
  242. kfree(ipcd);
  243. }
  244. EXPORT_SYMBOL_GPL(ipcomp_destroy);
  245. int ipcomp_init_state(struct xfrm_state *x)
  246. {
  247. int err;
  248. struct ipcomp_data *ipcd;
  249. struct xfrm_algo_desc *calg_desc;
  250. err = -EINVAL;
  251. if (!x->calg)
  252. goto out;
  253. if (x->encap)
  254. goto out;
  255. err = -ENOMEM;
  256. ipcd = kzalloc(sizeof(*ipcd), GFP_KERNEL);
  257. if (!ipcd)
  258. goto out;
  259. mutex_lock(&ipcomp_resource_mutex);
  260. if (!ipcomp_alloc_scratches())
  261. goto error;
  262. ipcd->tfms = ipcomp_alloc_tfms(x->calg->alg_name);
  263. if (!ipcd->tfms)
  264. goto error;
  265. mutex_unlock(&ipcomp_resource_mutex);
  266. calg_desc = xfrm_calg_get_byname(x->calg->alg_name, 0);
  267. BUG_ON(!calg_desc);
  268. ipcd->threshold = calg_desc->uinfo.comp.threshold;
  269. x->data = ipcd;
  270. err = 0;
  271. out:
  272. return err;
  273. error:
  274. ipcomp_free_data(ipcd);
  275. mutex_unlock(&ipcomp_resource_mutex);
  276. kfree(ipcd);
  277. goto out;
  278. }
  279. EXPORT_SYMBOL_GPL(ipcomp_init_state);
  280. MODULE_LICENSE("GPL");
  281. MODULE_DESCRIPTION("IP Payload Compression Protocol (IPComp) - RFC3173");
  282. MODULE_AUTHOR("James Morris <jmorris@intercode.com.au>");