ipcomp.c 10 KB

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