ipcomp.c 10 KB

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