ipcomp.c 10 KB

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