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/gfp.h>
  20. #include <linux/list.h>
  21. #include <linux/module.h>
  22. #include <linux/mutex.h>
  23. #include <linux/percpu.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. 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->truesize += len;
  59. __skb_put(skb, len);
  60. len += plen;
  61. skb_copy_to_linear_data(skb, scratch, len);
  62. while ((scratch += len, dlen -= len) > 0) {
  63. skb_frag_t *frag;
  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. frag->page = alloc_page(GFP_ATOMIC);
  69. err = -ENOMEM;
  70. if (!frag->page)
  71. goto out;
  72. len = PAGE_SIZE;
  73. if (dlen < len)
  74. len = dlen;
  75. memcpy(page_address(frag->page), scratch, len);
  76. frag->page_offset = 0;
  77. frag->size = len;
  78. skb->truesize += len;
  79. skb->data_len += len;
  80. skb->len += len;
  81. skb_shinfo(skb)->nr_frags++;
  82. }
  83. err = 0;
  84. out:
  85. put_cpu();
  86. return err;
  87. }
  88. int ipcomp_input(struct xfrm_state *x, struct sk_buff *skb)
  89. {
  90. int nexthdr;
  91. int err = -ENOMEM;
  92. struct ip_comp_hdr *ipch;
  93. if (skb_linearize_cow(skb))
  94. goto out;
  95. skb->ip_summed = CHECKSUM_NONE;
  96. /* Remove ipcomp header and decompress original payload */
  97. ipch = (void *)skb->data;
  98. nexthdr = ipch->nexthdr;
  99. skb->transport_header = skb->network_header + sizeof(*ipch);
  100. __skb_pull(skb, sizeof(*ipch));
  101. err = ipcomp_decompress(x, skb);
  102. if (err)
  103. goto out;
  104. err = nexthdr;
  105. out:
  106. return err;
  107. }
  108. EXPORT_SYMBOL_GPL(ipcomp_input);
  109. static int ipcomp_compress(struct xfrm_state *x, struct sk_buff *skb)
  110. {
  111. struct ipcomp_data *ipcd = x->data;
  112. const int plen = skb->len;
  113. int dlen = IPCOMP_SCRATCH_SIZE;
  114. u8 *start = skb->data;
  115. const int cpu = get_cpu();
  116. u8 *scratch = *per_cpu_ptr(ipcomp_scratches, cpu);
  117. struct crypto_comp *tfm = *per_cpu_ptr(ipcd->tfms, cpu);
  118. int err;
  119. local_bh_disable();
  120. err = crypto_comp_compress(tfm, start, plen, scratch, &dlen);
  121. local_bh_enable();
  122. if (err)
  123. goto out;
  124. if ((dlen + sizeof(struct ip_comp_hdr)) >= plen) {
  125. err = -EMSGSIZE;
  126. goto out;
  127. }
  128. memcpy(start + sizeof(struct ip_comp_hdr), scratch, dlen);
  129. put_cpu();
  130. pskb_trim(skb, dlen + sizeof(struct ip_comp_hdr));
  131. return 0;
  132. out:
  133. put_cpu();
  134. return err;
  135. }
  136. int ipcomp_output(struct xfrm_state *x, struct sk_buff *skb)
  137. {
  138. int err;
  139. struct ip_comp_hdr *ipch;
  140. struct ipcomp_data *ipcd = x->data;
  141. if (skb->len < ipcd->threshold) {
  142. /* Don't bother compressing */
  143. goto out_ok;
  144. }
  145. if (skb_linearize_cow(skb))
  146. goto out_ok;
  147. err = ipcomp_compress(x, skb);
  148. if (err) {
  149. goto out_ok;
  150. }
  151. /* Install ipcomp header, convert into ipcomp datagram. */
  152. ipch = ip_comp_hdr(skb);
  153. ipch->nexthdr = *skb_mac_header(skb);
  154. ipch->flags = 0;
  155. ipch->cpi = htons((u16 )ntohl(x->id.spi));
  156. *skb_mac_header(skb) = IPPROTO_COMP;
  157. out_ok:
  158. skb_push(skb, -skb_network_offset(skb));
  159. return 0;
  160. }
  161. EXPORT_SYMBOL_GPL(ipcomp_output);
  162. static void ipcomp_free_scratches(void)
  163. {
  164. int i;
  165. void **scratches;
  166. if (--ipcomp_scratch_users)
  167. return;
  168. scratches = ipcomp_scratches;
  169. if (!scratches)
  170. return;
  171. for_each_possible_cpu(i)
  172. vfree(*per_cpu_ptr(scratches, i));
  173. free_percpu(scratches);
  174. }
  175. static void **ipcomp_alloc_scratches(void)
  176. {
  177. int i;
  178. void **scratches;
  179. if (ipcomp_scratch_users++)
  180. return ipcomp_scratches;
  181. scratches = alloc_percpu(void *);
  182. if (!scratches)
  183. return NULL;
  184. ipcomp_scratches = scratches;
  185. for_each_possible_cpu(i) {
  186. void *scratch = vmalloc(IPCOMP_SCRATCH_SIZE);
  187. if (!scratch)
  188. return NULL;
  189. *per_cpu_ptr(scratches, i) = scratch;
  190. }
  191. return scratches;
  192. }
  193. static void ipcomp_free_tfms(struct crypto_comp **tfms)
  194. {
  195. struct ipcomp_tfms *pos;
  196. int cpu;
  197. list_for_each_entry(pos, &ipcomp_tfms_list, list) {
  198. if (pos->tfms == tfms)
  199. break;
  200. }
  201. WARN_ON(!pos);
  202. if (--pos->users)
  203. return;
  204. list_del(&pos->list);
  205. kfree(pos);
  206. if (!tfms)
  207. return;
  208. for_each_possible_cpu(cpu) {
  209. struct crypto_comp *tfm = *per_cpu_ptr(tfms, cpu);
  210. crypto_free_comp(tfm);
  211. }
  212. free_percpu(tfms);
  213. }
  214. static struct crypto_comp **ipcomp_alloc_tfms(const char *alg_name)
  215. {
  216. struct ipcomp_tfms *pos;
  217. struct crypto_comp **tfms;
  218. int cpu;
  219. /* This can be any valid CPU ID so we don't need locking. */
  220. cpu = raw_smp_processor_id();
  221. list_for_each_entry(pos, &ipcomp_tfms_list, list) {
  222. struct crypto_comp *tfm;
  223. tfms = pos->tfms;
  224. tfm = *per_cpu_ptr(tfms, cpu);
  225. if (!strcmp(crypto_comp_name(tfm), alg_name)) {
  226. pos->users++;
  227. return 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>");