ipcomp.c 10 KB

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