xfrm_ipcomp.c 7.5 KB

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