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. #include <net/protocol.h>
  32. struct ipcomp_tfms {
  33. struct list_head list;
  34. struct crypto_tfm **tfms;
  35. int users;
  36. };
  37. static DECLARE_MUTEX(ipcomp_resource_sem);
  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 iphdr *iph;
  45. struct ipcomp_data *ipcd = x->data;
  46. u8 *start, *scratch;
  47. struct crypto_tfm *tfm;
  48. int cpu;
  49. plen = skb->len;
  50. dlen = IPCOMP_SCRATCH_SIZE;
  51. start = skb->data;
  52. cpu = get_cpu();
  53. scratch = *per_cpu_ptr(ipcomp_scratches, cpu);
  54. tfm = *per_cpu_ptr(ipcd->tfms, cpu);
  55. err = crypto_comp_decompress(tfm, start, plen, scratch, &dlen);
  56. if (err)
  57. goto out;
  58. if (dlen < (plen + sizeof(struct ip_comp_hdr))) {
  59. err = -EINVAL;
  60. goto out;
  61. }
  62. err = pskb_expand_head(skb, 0, dlen - plen, GFP_ATOMIC);
  63. if (err)
  64. goto out;
  65. skb_put(skb, dlen - plen);
  66. memcpy(skb->data, scratch, dlen);
  67. iph = skb->nh.iph;
  68. iph->tot_len = htons(dlen + iph->ihl * 4);
  69. out:
  70. put_cpu();
  71. return err;
  72. }
  73. static int ipcomp_input(struct xfrm_state *x,
  74. struct xfrm_decap_state *decap, struct sk_buff *skb)
  75. {
  76. u8 nexthdr;
  77. int err = 0;
  78. struct iphdr *iph;
  79. union {
  80. struct iphdr iph;
  81. char buf[60];
  82. } tmp_iph;
  83. if ((skb_is_nonlinear(skb) || skb_cloned(skb)) &&
  84. skb_linearize(skb, GFP_ATOMIC) != 0) {
  85. err = -ENOMEM;
  86. goto out;
  87. }
  88. skb->ip_summed = CHECKSUM_NONE;
  89. /* Remove ipcomp header and decompress original payload */
  90. iph = skb->nh.iph;
  91. memcpy(&tmp_iph, iph, iph->ihl * 4);
  92. nexthdr = *(u8 *)skb->data;
  93. skb_pull(skb, sizeof(struct ip_comp_hdr));
  94. skb->nh.raw += sizeof(struct ip_comp_hdr);
  95. memcpy(skb->nh.raw, &tmp_iph, tmp_iph.iph.ihl * 4);
  96. iph = skb->nh.iph;
  97. iph->tot_len = htons(ntohs(iph->tot_len) - sizeof(struct ip_comp_hdr));
  98. iph->protocol = nexthdr;
  99. skb->h.raw = skb->data;
  100. err = ipcomp_decompress(x, skb);
  101. out:
  102. return err;
  103. }
  104. static int ipcomp_compress(struct xfrm_state *x, struct sk_buff *skb)
  105. {
  106. int err, plen, dlen, ihlen;
  107. struct iphdr *iph = skb->nh.iph;
  108. struct ipcomp_data *ipcd = x->data;
  109. u8 *start, *scratch;
  110. struct crypto_tfm *tfm;
  111. int cpu;
  112. ihlen = iph->ihl * 4;
  113. plen = skb->len - ihlen;
  114. dlen = IPCOMP_SCRATCH_SIZE;
  115. start = skb->data + ihlen;
  116. cpu = get_cpu();
  117. scratch = *per_cpu_ptr(ipcomp_scratches, cpu);
  118. tfm = *per_cpu_ptr(ipcd->tfms, cpu);
  119. err = crypto_comp_compress(tfm, start, plen, scratch, &dlen);
  120. if (err)
  121. goto out;
  122. if ((dlen + sizeof(struct ip_comp_hdr)) >= plen) {
  123. err = -EMSGSIZE;
  124. goto out;
  125. }
  126. memcpy(start + sizeof(struct ip_comp_hdr), scratch, dlen);
  127. put_cpu();
  128. pskb_trim(skb, ihlen + dlen + sizeof(struct ip_comp_hdr));
  129. return 0;
  130. out:
  131. put_cpu();
  132. return err;
  133. }
  134. static int ipcomp_output(struct xfrm_state *x, struct sk_buff *skb)
  135. {
  136. int err;
  137. struct iphdr *iph;
  138. struct ip_comp_hdr *ipch;
  139. struct ipcomp_data *ipcd = x->data;
  140. int hdr_len = 0;
  141. iph = skb->nh.iph;
  142. iph->tot_len = htons(skb->len);
  143. hdr_len = iph->ihl * 4;
  144. if ((skb->len - hdr_len) < ipcd->threshold) {
  145. /* Don't bother compressing */
  146. goto out_ok;
  147. }
  148. if ((skb_is_nonlinear(skb) || skb_cloned(skb)) &&
  149. skb_linearize(skb, GFP_ATOMIC) != 0) {
  150. goto out_ok;
  151. }
  152. err = ipcomp_compress(x, skb);
  153. iph = skb->nh.iph;
  154. if (err) {
  155. goto out_ok;
  156. }
  157. /* Install ipcomp header, convert into ipcomp datagram. */
  158. iph->tot_len = htons(skb->len);
  159. ipch = (struct ip_comp_hdr *)((char *)iph + iph->ihl * 4);
  160. ipch->nexthdr = iph->protocol;
  161. ipch->flags = 0;
  162. ipch->cpi = htons((u16 )ntohl(x->id.spi));
  163. iph->protocol = IPPROTO_COMP;
  164. ip_send_check(iph);
  165. return 0;
  166. out_ok:
  167. if (x->props.mode)
  168. ip_send_check(iph);
  169. return 0;
  170. }
  171. static void ipcomp4_err(struct sk_buff *skb, u32 info)
  172. {
  173. u32 spi;
  174. struct iphdr *iph = (struct iphdr *)skb->data;
  175. struct ip_comp_hdr *ipch = (struct ip_comp_hdr *)(skb->data+(iph->ihl<<2));
  176. struct xfrm_state *x;
  177. if (skb->h.icmph->type != ICMP_DEST_UNREACH ||
  178. skb->h.icmph->code != ICMP_FRAG_NEEDED)
  179. return;
  180. spi = ntohl(ntohs(ipch->cpi));
  181. x = xfrm_state_lookup((xfrm_address_t *)&iph->daddr,
  182. spi, IPPROTO_COMP, AF_INET);
  183. if (!x)
  184. return;
  185. NETDEBUG(KERN_DEBUG "pmtu discovery on SA IPCOMP/%08x/%u.%u.%u.%u\n",
  186. spi, NIPQUAD(iph->daddr));
  187. xfrm_state_put(x);
  188. }
  189. /* We always hold one tunnel user reference to indicate a tunnel */
  190. static struct xfrm_state *ipcomp_tunnel_create(struct xfrm_state *x)
  191. {
  192. struct xfrm_state *t;
  193. t = xfrm_state_alloc();
  194. if (t == NULL)
  195. goto out;
  196. t->id.proto = IPPROTO_IPIP;
  197. t->id.spi = x->props.saddr.a4;
  198. t->id.daddr.a4 = x->id.daddr.a4;
  199. memcpy(&t->sel, &x->sel, sizeof(t->sel));
  200. t->props.family = AF_INET;
  201. t->props.mode = 1;
  202. t->props.saddr.a4 = x->props.saddr.a4;
  203. t->props.flags = x->props.flags;
  204. if (xfrm_init_state(t))
  205. goto error;
  206. atomic_set(&t->tunnel_users, 1);
  207. out:
  208. return t;
  209. error:
  210. t->km.state = XFRM_STATE_DEAD;
  211. xfrm_state_put(t);
  212. t = NULL;
  213. goto out;
  214. }
  215. /*
  216. * Must be protected by xfrm_cfg_sem. State and tunnel user references are
  217. * always incremented on success.
  218. */
  219. static int ipcomp_tunnel_attach(struct xfrm_state *x)
  220. {
  221. int err = 0;
  222. struct xfrm_state *t;
  223. t = xfrm_state_lookup((xfrm_address_t *)&x->id.daddr.a4,
  224. x->props.saddr.a4, IPPROTO_IPIP, AF_INET);
  225. if (!t) {
  226. t = ipcomp_tunnel_create(x);
  227. if (!t) {
  228. err = -EINVAL;
  229. goto out;
  230. }
  231. xfrm_state_insert(t);
  232. xfrm_state_hold(t);
  233. }
  234. x->tunnel = t;
  235. atomic_inc(&t->tunnel_users);
  236. out:
  237. return err;
  238. }
  239. static void ipcomp_free_scratches(void)
  240. {
  241. int i;
  242. void **scratches;
  243. if (--ipcomp_scratch_users)
  244. return;
  245. scratches = ipcomp_scratches;
  246. if (!scratches)
  247. return;
  248. for_each_cpu(i) {
  249. void *scratch = *per_cpu_ptr(scratches, i);
  250. if (scratch)
  251. vfree(scratch);
  252. }
  253. free_percpu(scratches);
  254. }
  255. static void **ipcomp_alloc_scratches(void)
  256. {
  257. int i;
  258. void **scratches;
  259. if (ipcomp_scratch_users++)
  260. return ipcomp_scratches;
  261. scratches = alloc_percpu(void *);
  262. if (!scratches)
  263. return NULL;
  264. ipcomp_scratches = scratches;
  265. for_each_cpu(i) {
  266. void *scratch = vmalloc(IPCOMP_SCRATCH_SIZE);
  267. if (!scratch)
  268. return NULL;
  269. *per_cpu_ptr(scratches, i) = scratch;
  270. }
  271. return scratches;
  272. }
  273. static void ipcomp_free_tfms(struct crypto_tfm **tfms)
  274. {
  275. struct ipcomp_tfms *pos;
  276. int cpu;
  277. list_for_each_entry(pos, &ipcomp_tfms_list, list) {
  278. if (pos->tfms == tfms)
  279. break;
  280. }
  281. BUG_TRAP(pos);
  282. if (--pos->users)
  283. return;
  284. list_del(&pos->list);
  285. kfree(pos);
  286. if (!tfms)
  287. return;
  288. for_each_cpu(cpu) {
  289. struct crypto_tfm *tfm = *per_cpu_ptr(tfms, cpu);
  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 = raw_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>");