xfrm_ipcomp.c 7.6 KB

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