ipcomp.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. /*
  2. * IP Payload Compression Protocol (IPComp) - RFC3173.
  3. *
  4. * Copyright (c) 2003 James Morris <jmorris@intercode.com.au>
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the Free
  8. * Software Foundation; either version 2 of the License, or (at your option)
  9. * any later version.
  10. *
  11. * Todo:
  12. * - Tunable compression parameters.
  13. * - Compression stats.
  14. * - Adaptive compression.
  15. */
  16. #include <linux/module.h>
  17. #include <asm/scatterlist.h>
  18. #include <asm/semaphore.h>
  19. #include <linux/crypto.h>
  20. #include <linux/pfkeyv2.h>
  21. #include <linux/percpu.h>
  22. #include <linux/smp.h>
  23. #include <linux/list.h>
  24. #include <linux/vmalloc.h>
  25. #include <linux/rtnetlink.h>
  26. #include <linux/mutex.h>
  27. #include <net/ip.h>
  28. #include <net/xfrm.h>
  29. #include <net/icmp.h>
  30. #include <net/ipcomp.h>
  31. #include <net/protocol.h>
  32. struct ipcomp_tfms {
  33. struct list_head list;
  34. struct crypto_comp **tfms;
  35. int users;
  36. };
  37. static DEFINE_MUTEX(ipcomp_resource_mutex);
  38. static void **ipcomp_scratches;
  39. static int ipcomp_scratch_users;
  40. static LIST_HEAD(ipcomp_tfms_list);
  41. static int ipcomp_decompress(struct xfrm_state *x, struct sk_buff *skb)
  42. {
  43. struct ipcomp_data *ipcd = x->data;
  44. const int plen = skb->len;
  45. int dlen = IPCOMP_SCRATCH_SIZE;
  46. const u8 *start = skb->data;
  47. const int cpu = get_cpu();
  48. u8 *scratch = *per_cpu_ptr(ipcomp_scratches, cpu);
  49. struct crypto_comp *tfm = *per_cpu_ptr(ipcd->tfms, cpu);
  50. int err = crypto_comp_decompress(tfm, start, plen, scratch, &dlen);
  51. if (err)
  52. goto out;
  53. if (dlen < (plen + sizeof(struct ip_comp_hdr))) {
  54. err = -EINVAL;
  55. goto out;
  56. }
  57. err = pskb_expand_head(skb, 0, dlen - plen, GFP_ATOMIC);
  58. if (err)
  59. goto out;
  60. skb->truesize += dlen - plen;
  61. __skb_put(skb, dlen - plen);
  62. skb_copy_to_linear_data(skb, scratch, dlen);
  63. out:
  64. put_cpu();
  65. return err;
  66. }
  67. static int ipcomp_input(struct xfrm_state *x, struct sk_buff *skb)
  68. {
  69. int err = -ENOMEM;
  70. struct ip_comp_hdr *ipch;
  71. if (skb_linearize_cow(skb))
  72. goto out;
  73. skb->ip_summed = CHECKSUM_NONE;
  74. /* Remove ipcomp header and decompress original payload */
  75. ipch = (void *)skb->data;
  76. skb->transport_header = skb->network_header + sizeof(*ipch);
  77. __skb_pull(skb, sizeof(*ipch));
  78. err = ipcomp_decompress(x, skb);
  79. if (err)
  80. goto out;
  81. err = ipch->nexthdr;
  82. out:
  83. return err;
  84. }
  85. static int ipcomp_compress(struct xfrm_state *x, struct sk_buff *skb)
  86. {
  87. struct ipcomp_data *ipcd = x->data;
  88. const int plen = skb->len;
  89. int dlen = IPCOMP_SCRATCH_SIZE;
  90. u8 *start = skb->data;
  91. const int cpu = get_cpu();
  92. u8 *scratch = *per_cpu_ptr(ipcomp_scratches, cpu);
  93. struct crypto_comp *tfm = *per_cpu_ptr(ipcd->tfms, cpu);
  94. int err = crypto_comp_compress(tfm, start, plen, scratch, &dlen);
  95. if (err)
  96. goto out;
  97. if ((dlen + sizeof(struct ip_comp_hdr)) >= plen) {
  98. err = -EMSGSIZE;
  99. goto out;
  100. }
  101. memcpy(start + sizeof(struct ip_comp_hdr), scratch, dlen);
  102. put_cpu();
  103. pskb_trim(skb, dlen + sizeof(struct ip_comp_hdr));
  104. return 0;
  105. out:
  106. put_cpu();
  107. return err;
  108. }
  109. static int ipcomp_output(struct xfrm_state *x, struct sk_buff *skb)
  110. {
  111. int err;
  112. struct ip_comp_hdr *ipch;
  113. struct ipcomp_data *ipcd = x->data;
  114. if (skb->len < ipcd->threshold) {
  115. /* Don't bother compressing */
  116. goto out_ok;
  117. }
  118. if (skb_linearize_cow(skb))
  119. goto out_ok;
  120. err = ipcomp_compress(x, skb);
  121. if (err) {
  122. goto out_ok;
  123. }
  124. /* Install ipcomp header, convert into ipcomp datagram. */
  125. ipch = ip_comp_hdr(skb);
  126. ipch->nexthdr = *skb_mac_header(skb);
  127. ipch->flags = 0;
  128. ipch->cpi = htons((u16 )ntohl(x->id.spi));
  129. *skb_mac_header(skb) = IPPROTO_COMP;
  130. out_ok:
  131. skb_push(skb, -skb_network_offset(skb));
  132. return 0;
  133. }
  134. static void ipcomp4_err(struct sk_buff *skb, u32 info)
  135. {
  136. __be32 spi;
  137. struct iphdr *iph = (struct iphdr *)skb->data;
  138. struct ip_comp_hdr *ipch = (struct ip_comp_hdr *)(skb->data+(iph->ihl<<2));
  139. struct xfrm_state *x;
  140. if (icmp_hdr(skb)->type != ICMP_DEST_UNREACH ||
  141. icmp_hdr(skb)->code != ICMP_FRAG_NEEDED)
  142. return;
  143. spi = htonl(ntohs(ipch->cpi));
  144. x = xfrm_state_lookup((xfrm_address_t *)&iph->daddr,
  145. spi, IPPROTO_COMP, AF_INET);
  146. if (!x)
  147. return;
  148. NETDEBUG(KERN_DEBUG "pmtu discovery on SA IPCOMP/%08x/%u.%u.%u.%u\n",
  149. spi, NIPQUAD(iph->daddr));
  150. xfrm_state_put(x);
  151. }
  152. /* We always hold one tunnel user reference to indicate a tunnel */
  153. static struct xfrm_state *ipcomp_tunnel_create(struct xfrm_state *x)
  154. {
  155. struct xfrm_state *t;
  156. u8 mode = XFRM_MODE_TUNNEL;
  157. t = xfrm_state_alloc();
  158. if (t == NULL)
  159. goto out;
  160. t->id.proto = IPPROTO_IPIP;
  161. t->id.spi = x->props.saddr.a4;
  162. t->id.daddr.a4 = x->id.daddr.a4;
  163. memcpy(&t->sel, &x->sel, sizeof(t->sel));
  164. t->props.family = AF_INET;
  165. if (x->props.mode == XFRM_MODE_BEET)
  166. mode = x->props.mode;
  167. t->props.mode = mode;
  168. t->props.saddr.a4 = x->props.saddr.a4;
  169. t->props.flags = x->props.flags;
  170. if (xfrm_init_state(t))
  171. goto error;
  172. atomic_set(&t->tunnel_users, 1);
  173. out:
  174. return t;
  175. error:
  176. t->km.state = XFRM_STATE_DEAD;
  177. xfrm_state_put(t);
  178. t = NULL;
  179. goto out;
  180. }
  181. /*
  182. * Must be protected by xfrm_cfg_mutex. State and tunnel user references are
  183. * always incremented on success.
  184. */
  185. static int ipcomp_tunnel_attach(struct xfrm_state *x)
  186. {
  187. int err = 0;
  188. struct xfrm_state *t;
  189. t = xfrm_state_lookup((xfrm_address_t *)&x->id.daddr.a4,
  190. x->props.saddr.a4, IPPROTO_IPIP, AF_INET);
  191. if (!t) {
  192. t = ipcomp_tunnel_create(x);
  193. if (!t) {
  194. err = -EINVAL;
  195. goto out;
  196. }
  197. xfrm_state_insert(t);
  198. xfrm_state_hold(t);
  199. }
  200. x->tunnel = t;
  201. atomic_inc(&t->tunnel_users);
  202. out:
  203. return err;
  204. }
  205. static void ipcomp_free_scratches(void)
  206. {
  207. int i;
  208. void **scratches;
  209. if (--ipcomp_scratch_users)
  210. return;
  211. scratches = ipcomp_scratches;
  212. if (!scratches)
  213. return;
  214. for_each_possible_cpu(i)
  215. vfree(*per_cpu_ptr(scratches, i));
  216. free_percpu(scratches);
  217. }
  218. static void **ipcomp_alloc_scratches(void)
  219. {
  220. int i;
  221. void **scratches;
  222. if (ipcomp_scratch_users++)
  223. return ipcomp_scratches;
  224. scratches = alloc_percpu(void *);
  225. if (!scratches)
  226. return NULL;
  227. ipcomp_scratches = scratches;
  228. for_each_possible_cpu(i) {
  229. void *scratch = vmalloc(IPCOMP_SCRATCH_SIZE);
  230. if (!scratch)
  231. return NULL;
  232. *per_cpu_ptr(scratches, i) = scratch;
  233. }
  234. return scratches;
  235. }
  236. static void ipcomp_free_tfms(struct crypto_comp **tfms)
  237. {
  238. struct ipcomp_tfms *pos;
  239. int cpu;
  240. list_for_each_entry(pos, &ipcomp_tfms_list, list) {
  241. if (pos->tfms == tfms)
  242. break;
  243. }
  244. BUG_TRAP(pos);
  245. if (--pos->users)
  246. return;
  247. list_del(&pos->list);
  248. kfree(pos);
  249. if (!tfms)
  250. return;
  251. for_each_possible_cpu(cpu) {
  252. struct crypto_comp *tfm = *per_cpu_ptr(tfms, cpu);
  253. crypto_free_comp(tfm);
  254. }
  255. free_percpu(tfms);
  256. }
  257. static struct crypto_comp **ipcomp_alloc_tfms(const char *alg_name)
  258. {
  259. struct ipcomp_tfms *pos;
  260. struct crypto_comp **tfms;
  261. int cpu;
  262. /* This can be any valid CPU ID so we don't need locking. */
  263. cpu = raw_smp_processor_id();
  264. list_for_each_entry(pos, &ipcomp_tfms_list, list) {
  265. struct crypto_comp *tfm;
  266. tfms = pos->tfms;
  267. tfm = *per_cpu_ptr(tfms, cpu);
  268. if (!strcmp(crypto_comp_name(tfm), alg_name)) {
  269. pos->users++;
  270. return tfms;
  271. }
  272. }
  273. pos = kmalloc(sizeof(*pos), GFP_KERNEL);
  274. if (!pos)
  275. return NULL;
  276. pos->users = 1;
  277. INIT_LIST_HEAD(&pos->list);
  278. list_add(&pos->list, &ipcomp_tfms_list);
  279. pos->tfms = tfms = alloc_percpu(struct crypto_comp *);
  280. if (!tfms)
  281. goto error;
  282. for_each_possible_cpu(cpu) {
  283. struct crypto_comp *tfm = crypto_alloc_comp(alg_name, 0,
  284. CRYPTO_ALG_ASYNC);
  285. if (!tfm)
  286. goto error;
  287. *per_cpu_ptr(tfms, cpu) = tfm;
  288. }
  289. return tfms;
  290. error:
  291. ipcomp_free_tfms(tfms);
  292. return NULL;
  293. }
  294. static void ipcomp_free_data(struct ipcomp_data *ipcd)
  295. {
  296. if (ipcd->tfms)
  297. ipcomp_free_tfms(ipcd->tfms);
  298. ipcomp_free_scratches();
  299. }
  300. static void ipcomp_destroy(struct xfrm_state *x)
  301. {
  302. struct ipcomp_data *ipcd = x->data;
  303. if (!ipcd)
  304. return;
  305. xfrm_state_delete_tunnel(x);
  306. mutex_lock(&ipcomp_resource_mutex);
  307. ipcomp_free_data(ipcd);
  308. mutex_unlock(&ipcomp_resource_mutex);
  309. kfree(ipcd);
  310. }
  311. static int ipcomp_init_state(struct xfrm_state *x)
  312. {
  313. int err;
  314. struct ipcomp_data *ipcd;
  315. struct xfrm_algo_desc *calg_desc;
  316. err = -EINVAL;
  317. if (!x->calg)
  318. goto out;
  319. if (x->encap)
  320. goto out;
  321. err = -ENOMEM;
  322. ipcd = kzalloc(sizeof(*ipcd), GFP_KERNEL);
  323. if (!ipcd)
  324. goto out;
  325. x->props.header_len = 0;
  326. if (x->props.mode == XFRM_MODE_TUNNEL)
  327. x->props.header_len += sizeof(struct iphdr);
  328. mutex_lock(&ipcomp_resource_mutex);
  329. if (!ipcomp_alloc_scratches())
  330. goto error;
  331. ipcd->tfms = ipcomp_alloc_tfms(x->calg->alg_name);
  332. if (!ipcd->tfms)
  333. goto error;
  334. mutex_unlock(&ipcomp_resource_mutex);
  335. if (x->props.mode == XFRM_MODE_TUNNEL) {
  336. err = ipcomp_tunnel_attach(x);
  337. if (err)
  338. goto error_tunnel;
  339. }
  340. calg_desc = xfrm_calg_get_byname(x->calg->alg_name, 0);
  341. BUG_ON(!calg_desc);
  342. ipcd->threshold = calg_desc->uinfo.comp.threshold;
  343. x->data = ipcd;
  344. err = 0;
  345. out:
  346. return err;
  347. error_tunnel:
  348. mutex_lock(&ipcomp_resource_mutex);
  349. error:
  350. ipcomp_free_data(ipcd);
  351. mutex_unlock(&ipcomp_resource_mutex);
  352. kfree(ipcd);
  353. goto out;
  354. }
  355. static struct xfrm_type ipcomp_type = {
  356. .description = "IPCOMP4",
  357. .owner = THIS_MODULE,
  358. .proto = IPPROTO_COMP,
  359. .init_state = ipcomp_init_state,
  360. .destructor = ipcomp_destroy,
  361. .input = ipcomp_input,
  362. .output = ipcomp_output
  363. };
  364. static struct net_protocol ipcomp4_protocol = {
  365. .handler = xfrm4_rcv,
  366. .err_handler = ipcomp4_err,
  367. .no_policy = 1,
  368. };
  369. static int __init ipcomp4_init(void)
  370. {
  371. if (xfrm_register_type(&ipcomp_type, AF_INET) < 0) {
  372. printk(KERN_INFO "ipcomp init: can't add xfrm type\n");
  373. return -EAGAIN;
  374. }
  375. if (inet_add_protocol(&ipcomp4_protocol, IPPROTO_COMP) < 0) {
  376. printk(KERN_INFO "ipcomp init: can't add protocol\n");
  377. xfrm_unregister_type(&ipcomp_type, AF_INET);
  378. return -EAGAIN;
  379. }
  380. return 0;
  381. }
  382. static void __exit ipcomp4_fini(void)
  383. {
  384. if (inet_del_protocol(&ipcomp4_protocol, IPPROTO_COMP) < 0)
  385. printk(KERN_INFO "ip ipcomp close: can't remove protocol\n");
  386. if (xfrm_unregister_type(&ipcomp_type, AF_INET) < 0)
  387. printk(KERN_INFO "ip ipcomp close: can't remove xfrm type\n");
  388. }
  389. module_init(ipcomp4_init);
  390. module_exit(ipcomp4_fini);
  391. MODULE_LICENSE("GPL");
  392. MODULE_DESCRIPTION("IP Payload Compression Protocol (IPComp) - RFC3173");
  393. MODULE_AUTHOR("James Morris <jmorris@intercode.com.au>");
  394. MODULE_ALIAS_XFRM_TYPE(AF_INET, XFRM_PROTO_COMP);