xfrm_ipcomp.c 7.5 KB

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