xfrm_ipcomp.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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/slab.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 * __percpu *tfms;
  32. int users;
  33. };
  34. static DEFINE_MUTEX(ipcomp_resource_mutex);
  35. static void * __percpu *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. int len;
  49. if (err)
  50. goto out;
  51. if (dlen < (plen + sizeof(struct ip_comp_hdr))) {
  52. err = -EINVAL;
  53. goto out;
  54. }
  55. len = dlen - plen;
  56. if (len > skb_tailroom(skb))
  57. len = skb_tailroom(skb);
  58. __skb_put(skb, len);
  59. len += plen;
  60. skb_copy_to_linear_data(skb, scratch, len);
  61. while ((scratch += len, dlen -= len) > 0) {
  62. skb_frag_t *frag;
  63. err = -EMSGSIZE;
  64. if (WARN_ON(skb_shinfo(skb)->nr_frags >= MAX_SKB_FRAGS))
  65. goto out;
  66. frag = skb_shinfo(skb)->frags + skb_shinfo(skb)->nr_frags;
  67. frag->page = alloc_page(GFP_ATOMIC);
  68. err = -ENOMEM;
  69. if (!frag->page)
  70. goto out;
  71. len = PAGE_SIZE;
  72. if (dlen < len)
  73. len = dlen;
  74. memcpy(page_address(frag->page), scratch, len);
  75. frag->page_offset = 0;
  76. frag->size = len;
  77. skb->truesize += len;
  78. skb->data_len += len;
  79. skb->len += len;
  80. skb_shinfo(skb)->nr_frags++;
  81. }
  82. err = 0;
  83. out:
  84. put_cpu();
  85. return err;
  86. }
  87. int ipcomp_input(struct xfrm_state *x, struct sk_buff *skb)
  88. {
  89. int nexthdr;
  90. int err = -ENOMEM;
  91. struct ip_comp_hdr *ipch;
  92. if (skb_linearize_cow(skb))
  93. goto out;
  94. skb->ip_summed = CHECKSUM_NONE;
  95. /* Remove ipcomp header and decompress original payload */
  96. ipch = (void *)skb->data;
  97. nexthdr = ipch->nexthdr;
  98. skb->transport_header = skb->network_header + sizeof(*ipch);
  99. __skb_pull(skb, sizeof(*ipch));
  100. err = ipcomp_decompress(x, skb);
  101. if (err)
  102. goto out;
  103. err = nexthdr;
  104. out:
  105. return err;
  106. }
  107. EXPORT_SYMBOL_GPL(ipcomp_input);
  108. static int ipcomp_compress(struct xfrm_state *x, struct sk_buff *skb)
  109. {
  110. struct ipcomp_data *ipcd = x->data;
  111. const int plen = skb->len;
  112. int dlen = IPCOMP_SCRATCH_SIZE;
  113. u8 *start = skb->data;
  114. const int cpu = get_cpu();
  115. u8 *scratch = *per_cpu_ptr(ipcomp_scratches, cpu);
  116. struct crypto_comp *tfm = *per_cpu_ptr(ipcd->tfms, cpu);
  117. int err;
  118. local_bh_disable();
  119. err = crypto_comp_compress(tfm, start, plen, scratch, &dlen);
  120. local_bh_enable();
  121. if (err)
  122. goto out;
  123. if ((dlen + sizeof(struct ip_comp_hdr)) >= plen) {
  124. err = -EMSGSIZE;
  125. goto out;
  126. }
  127. memcpy(start + sizeof(struct ip_comp_hdr), scratch, dlen);
  128. put_cpu();
  129. pskb_trim(skb, dlen + sizeof(struct ip_comp_hdr));
  130. return 0;
  131. out:
  132. put_cpu();
  133. return err;
  134. }
  135. int ipcomp_output(struct xfrm_state *x, struct sk_buff *skb)
  136. {
  137. int err;
  138. struct ip_comp_hdr *ipch;
  139. struct ipcomp_data *ipcd = x->data;
  140. if (skb->len < ipcd->threshold) {
  141. /* Don't bother compressing */
  142. goto out_ok;
  143. }
  144. if (skb_linearize_cow(skb))
  145. goto out_ok;
  146. err = ipcomp_compress(x, skb);
  147. if (err) {
  148. goto out_ok;
  149. }
  150. /* Install ipcomp header, convert into ipcomp datagram. */
  151. ipch = ip_comp_hdr(skb);
  152. ipch->nexthdr = *skb_mac_header(skb);
  153. ipch->flags = 0;
  154. ipch->cpi = htons((u16 )ntohl(x->id.spi));
  155. *skb_mac_header(skb) = IPPROTO_COMP;
  156. out_ok:
  157. skb_push(skb, -skb_network_offset(skb));
  158. return 0;
  159. }
  160. EXPORT_SYMBOL_GPL(ipcomp_output);
  161. static void ipcomp_free_scratches(void)
  162. {
  163. int i;
  164. void * __percpu *scratches;
  165. if (--ipcomp_scratch_users)
  166. return;
  167. scratches = ipcomp_scratches;
  168. if (!scratches)
  169. return;
  170. for_each_possible_cpu(i)
  171. vfree(*per_cpu_ptr(scratches, i));
  172. free_percpu(scratches);
  173. }
  174. static void * __percpu *ipcomp_alloc_scratches(void)
  175. {
  176. int i;
  177. void * __percpu *scratches;
  178. if (ipcomp_scratch_users++)
  179. return ipcomp_scratches;
  180. scratches = alloc_percpu(void *);
  181. if (!scratches)
  182. return NULL;
  183. ipcomp_scratches = scratches;
  184. for_each_possible_cpu(i) {
  185. void *scratch = vmalloc(IPCOMP_SCRATCH_SIZE);
  186. if (!scratch)
  187. return NULL;
  188. *per_cpu_ptr(scratches, i) = scratch;
  189. }
  190. return scratches;
  191. }
  192. static void ipcomp_free_tfms(struct crypto_comp * __percpu *tfms)
  193. {
  194. struct ipcomp_tfms *pos;
  195. int cpu;
  196. list_for_each_entry(pos, &ipcomp_tfms_list, list) {
  197. if (pos->tfms == tfms)
  198. break;
  199. }
  200. WARN_ON(!pos);
  201. if (--pos->users)
  202. return;
  203. list_del(&pos->list);
  204. kfree(pos);
  205. if (!tfms)
  206. return;
  207. for_each_possible_cpu(cpu) {
  208. struct crypto_comp *tfm = *per_cpu_ptr(tfms, cpu);
  209. crypto_free_comp(tfm);
  210. }
  211. free_percpu(tfms);
  212. }
  213. static struct crypto_comp * __percpu *ipcomp_alloc_tfms(const char *alg_name)
  214. {
  215. struct ipcomp_tfms *pos;
  216. struct crypto_comp * __percpu *tfms;
  217. int cpu;
  218. /* This can be any valid CPU ID so we don't need locking. */
  219. cpu = raw_smp_processor_id();
  220. list_for_each_entry(pos, &ipcomp_tfms_list, list) {
  221. struct crypto_comp *tfm;
  222. tfms = pos->tfms;
  223. tfm = *per_cpu_ptr(tfms, cpu);
  224. if (!strcmp(crypto_comp_name(tfm), alg_name)) {
  225. pos->users++;
  226. return tfms;
  227. }
  228. }
  229. pos = kmalloc(sizeof(*pos), GFP_KERNEL);
  230. if (!pos)
  231. return NULL;
  232. pos->users = 1;
  233. INIT_LIST_HEAD(&pos->list);
  234. list_add(&pos->list, &ipcomp_tfms_list);
  235. pos->tfms = tfms = alloc_percpu(struct crypto_comp *);
  236. if (!tfms)
  237. goto error;
  238. for_each_possible_cpu(cpu) {
  239. struct crypto_comp *tfm = crypto_alloc_comp(alg_name, 0,
  240. CRYPTO_ALG_ASYNC);
  241. if (IS_ERR(tfm))
  242. goto error;
  243. *per_cpu_ptr(tfms, cpu) = tfm;
  244. }
  245. return tfms;
  246. error:
  247. ipcomp_free_tfms(tfms);
  248. return NULL;
  249. }
  250. static void ipcomp_free_data(struct ipcomp_data *ipcd)
  251. {
  252. if (ipcd->tfms)
  253. ipcomp_free_tfms(ipcd->tfms);
  254. ipcomp_free_scratches();
  255. }
  256. void ipcomp_destroy(struct xfrm_state *x)
  257. {
  258. struct ipcomp_data *ipcd = x->data;
  259. if (!ipcd)
  260. return;
  261. xfrm_state_delete_tunnel(x);
  262. mutex_lock(&ipcomp_resource_mutex);
  263. ipcomp_free_data(ipcd);
  264. mutex_unlock(&ipcomp_resource_mutex);
  265. kfree(ipcd);
  266. }
  267. EXPORT_SYMBOL_GPL(ipcomp_destroy);
  268. int ipcomp_init_state(struct xfrm_state *x)
  269. {
  270. int err;
  271. struct ipcomp_data *ipcd;
  272. struct xfrm_algo_desc *calg_desc;
  273. err = -EINVAL;
  274. if (!x->calg)
  275. goto out;
  276. if (x->encap)
  277. goto out;
  278. err = -ENOMEM;
  279. ipcd = kzalloc(sizeof(*ipcd), GFP_KERNEL);
  280. if (!ipcd)
  281. goto out;
  282. mutex_lock(&ipcomp_resource_mutex);
  283. if (!ipcomp_alloc_scratches())
  284. goto error;
  285. ipcd->tfms = ipcomp_alloc_tfms(x->calg->alg_name);
  286. if (!ipcd->tfms)
  287. goto error;
  288. mutex_unlock(&ipcomp_resource_mutex);
  289. calg_desc = xfrm_calg_get_byname(x->calg->alg_name, 0);
  290. BUG_ON(!calg_desc);
  291. ipcd->threshold = calg_desc->uinfo.comp.threshold;
  292. x->data = ipcd;
  293. err = 0;
  294. out:
  295. return err;
  296. error:
  297. ipcomp_free_data(ipcd);
  298. mutex_unlock(&ipcomp_resource_mutex);
  299. kfree(ipcd);
  300. goto out;
  301. }
  302. EXPORT_SYMBOL_GPL(ipcomp_init_state);
  303. MODULE_LICENSE("GPL");
  304. MODULE_DESCRIPTION("IP Payload Compression Protocol (IPComp) - RFC3173");
  305. MODULE_AUTHOR("James Morris <jmorris@intercode.com.au>");