xfrm_ipcomp.c 7.6 KB

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