ipcomp.c 11 KB

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