ipcomp.c 10 KB

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