ipcomp.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  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. u8 mode = XFRM_MODE_TUNNEL;
  177. t = xfrm_state_alloc();
  178. if (t == NULL)
  179. goto out;
  180. t->id.proto = IPPROTO_IPIP;
  181. t->id.spi = x->props.saddr.a4;
  182. t->id.daddr.a4 = x->id.daddr.a4;
  183. memcpy(&t->sel, &x->sel, sizeof(t->sel));
  184. t->props.family = AF_INET;
  185. if (x->props.mode == XFRM_MODE_BEET)
  186. mode = x->props.mode;
  187. t->props.mode = mode;
  188. t->props.saddr.a4 = x->props.saddr.a4;
  189. t->props.flags = x->props.flags;
  190. if (xfrm_init_state(t))
  191. goto error;
  192. atomic_set(&t->tunnel_users, 1);
  193. out:
  194. return t;
  195. error:
  196. t->km.state = XFRM_STATE_DEAD;
  197. xfrm_state_put(t);
  198. t = NULL;
  199. goto out;
  200. }
  201. /*
  202. * Must be protected by xfrm_cfg_mutex. State and tunnel user references are
  203. * always incremented on success.
  204. */
  205. static int ipcomp_tunnel_attach(struct xfrm_state *x)
  206. {
  207. int err = 0;
  208. struct xfrm_state *t;
  209. t = xfrm_state_lookup((xfrm_address_t *)&x->id.daddr.a4,
  210. x->props.saddr.a4, IPPROTO_IPIP, AF_INET);
  211. if (!t) {
  212. t = ipcomp_tunnel_create(x);
  213. if (!t) {
  214. err = -EINVAL;
  215. goto out;
  216. }
  217. xfrm_state_insert(t);
  218. xfrm_state_hold(t);
  219. }
  220. x->tunnel = t;
  221. atomic_inc(&t->tunnel_users);
  222. out:
  223. return err;
  224. }
  225. static void ipcomp_free_scratches(void)
  226. {
  227. int i;
  228. void **scratches;
  229. if (--ipcomp_scratch_users)
  230. return;
  231. scratches = ipcomp_scratches;
  232. if (!scratches)
  233. return;
  234. for_each_possible_cpu(i)
  235. vfree(*per_cpu_ptr(scratches, i));
  236. free_percpu(scratches);
  237. }
  238. static void **ipcomp_alloc_scratches(void)
  239. {
  240. int i;
  241. void **scratches;
  242. if (ipcomp_scratch_users++)
  243. return ipcomp_scratches;
  244. scratches = alloc_percpu(void *);
  245. if (!scratches)
  246. return NULL;
  247. ipcomp_scratches = scratches;
  248. for_each_possible_cpu(i) {
  249. void *scratch = vmalloc(IPCOMP_SCRATCH_SIZE);
  250. if (!scratch)
  251. return NULL;
  252. *per_cpu_ptr(scratches, i) = scratch;
  253. }
  254. return scratches;
  255. }
  256. static void ipcomp_free_tfms(struct crypto_comp **tfms)
  257. {
  258. struct ipcomp_tfms *pos;
  259. int cpu;
  260. list_for_each_entry(pos, &ipcomp_tfms_list, list) {
  261. if (pos->tfms == tfms)
  262. break;
  263. }
  264. BUG_TRAP(pos);
  265. if (--pos->users)
  266. return;
  267. list_del(&pos->list);
  268. kfree(pos);
  269. if (!tfms)
  270. return;
  271. for_each_possible_cpu(cpu) {
  272. struct crypto_comp *tfm = *per_cpu_ptr(tfms, cpu);
  273. crypto_free_comp(tfm);
  274. }
  275. free_percpu(tfms);
  276. }
  277. static struct crypto_comp **ipcomp_alloc_tfms(const char *alg_name)
  278. {
  279. struct ipcomp_tfms *pos;
  280. struct crypto_comp **tfms;
  281. int cpu;
  282. /* This can be any valid CPU ID so we don't need locking. */
  283. cpu = raw_smp_processor_id();
  284. list_for_each_entry(pos, &ipcomp_tfms_list, list) {
  285. struct crypto_comp *tfm;
  286. tfms = pos->tfms;
  287. tfm = *per_cpu_ptr(tfms, cpu);
  288. if (!strcmp(crypto_comp_name(tfm), alg_name)) {
  289. pos->users++;
  290. return tfms;
  291. }
  292. }
  293. pos = kmalloc(sizeof(*pos), GFP_KERNEL);
  294. if (!pos)
  295. return NULL;
  296. pos->users = 1;
  297. INIT_LIST_HEAD(&pos->list);
  298. list_add(&pos->list, &ipcomp_tfms_list);
  299. pos->tfms = tfms = alloc_percpu(struct crypto_comp *);
  300. if (!tfms)
  301. goto error;
  302. for_each_possible_cpu(cpu) {
  303. struct crypto_comp *tfm = crypto_alloc_comp(alg_name, 0,
  304. CRYPTO_ALG_ASYNC);
  305. if (!tfm)
  306. goto error;
  307. *per_cpu_ptr(tfms, cpu) = tfm;
  308. }
  309. return tfms;
  310. error:
  311. ipcomp_free_tfms(tfms);
  312. return NULL;
  313. }
  314. static void ipcomp_free_data(struct ipcomp_data *ipcd)
  315. {
  316. if (ipcd->tfms)
  317. ipcomp_free_tfms(ipcd->tfms);
  318. ipcomp_free_scratches();
  319. }
  320. static void ipcomp_destroy(struct xfrm_state *x)
  321. {
  322. struct ipcomp_data *ipcd = x->data;
  323. if (!ipcd)
  324. return;
  325. xfrm_state_delete_tunnel(x);
  326. mutex_lock(&ipcomp_resource_mutex);
  327. ipcomp_free_data(ipcd);
  328. mutex_unlock(&ipcomp_resource_mutex);
  329. kfree(ipcd);
  330. }
  331. static int ipcomp_init_state(struct xfrm_state *x)
  332. {
  333. int err;
  334. struct ipcomp_data *ipcd;
  335. struct xfrm_algo_desc *calg_desc;
  336. err = -EINVAL;
  337. if (!x->calg)
  338. goto out;
  339. if (x->encap)
  340. goto out;
  341. err = -ENOMEM;
  342. ipcd = kzalloc(sizeof(*ipcd), GFP_KERNEL);
  343. if (!ipcd)
  344. goto out;
  345. x->props.header_len = 0;
  346. if (x->props.mode == XFRM_MODE_TUNNEL)
  347. x->props.header_len += sizeof(struct iphdr);
  348. mutex_lock(&ipcomp_resource_mutex);
  349. if (!ipcomp_alloc_scratches())
  350. goto error;
  351. ipcd->tfms = ipcomp_alloc_tfms(x->calg->alg_name);
  352. if (!ipcd->tfms)
  353. goto error;
  354. mutex_unlock(&ipcomp_resource_mutex);
  355. if (x->props.mode == XFRM_MODE_TUNNEL) {
  356. err = ipcomp_tunnel_attach(x);
  357. if (err)
  358. goto error_tunnel;
  359. }
  360. calg_desc = xfrm_calg_get_byname(x->calg->alg_name, 0);
  361. BUG_ON(!calg_desc);
  362. ipcd->threshold = calg_desc->uinfo.comp.threshold;
  363. x->data = ipcd;
  364. err = 0;
  365. out:
  366. return err;
  367. error_tunnel:
  368. mutex_lock(&ipcomp_resource_mutex);
  369. error:
  370. ipcomp_free_data(ipcd);
  371. mutex_unlock(&ipcomp_resource_mutex);
  372. kfree(ipcd);
  373. goto out;
  374. }
  375. static struct xfrm_type ipcomp_type = {
  376. .description = "IPCOMP4",
  377. .owner = THIS_MODULE,
  378. .proto = IPPROTO_COMP,
  379. .init_state = ipcomp_init_state,
  380. .destructor = ipcomp_destroy,
  381. .input = ipcomp_input,
  382. .output = ipcomp_output
  383. };
  384. static struct net_protocol ipcomp4_protocol = {
  385. .handler = xfrm4_rcv,
  386. .err_handler = ipcomp4_err,
  387. .no_policy = 1,
  388. };
  389. static int __init ipcomp4_init(void)
  390. {
  391. if (xfrm_register_type(&ipcomp_type, AF_INET) < 0) {
  392. printk(KERN_INFO "ipcomp init: can't add xfrm type\n");
  393. return -EAGAIN;
  394. }
  395. if (inet_add_protocol(&ipcomp4_protocol, IPPROTO_COMP) < 0) {
  396. printk(KERN_INFO "ipcomp init: can't add protocol\n");
  397. xfrm_unregister_type(&ipcomp_type, AF_INET);
  398. return -EAGAIN;
  399. }
  400. return 0;
  401. }
  402. static void __exit ipcomp4_fini(void)
  403. {
  404. if (inet_del_protocol(&ipcomp4_protocol, IPPROTO_COMP) < 0)
  405. printk(KERN_INFO "ip ipcomp close: can't remove protocol\n");
  406. if (xfrm_unregister_type(&ipcomp_type, AF_INET) < 0)
  407. printk(KERN_INFO "ip ipcomp close: can't remove xfrm type\n");
  408. }
  409. module_init(ipcomp4_init);
  410. module_exit(ipcomp4_fini);
  411. MODULE_LICENSE("GPL");
  412. MODULE_DESCRIPTION("IP Payload Compression Protocol (IPComp) - RFC3173");
  413. MODULE_AUTHOR("James Morris <jmorris@intercode.com.au>");