ipcomp.c 11 KB

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