ipcomp.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  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, 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_mutex. 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_possible_cpu(i)
  249. vfree(*per_cpu_ptr(scratches, i));
  250. free_percpu(scratches);
  251. }
  252. static void **ipcomp_alloc_scratches(void)
  253. {
  254. int i;
  255. void **scratches;
  256. if (ipcomp_scratch_users++)
  257. return ipcomp_scratches;
  258. scratches = alloc_percpu(void *);
  259. if (!scratches)
  260. return NULL;
  261. ipcomp_scratches = scratches;
  262. for_each_possible_cpu(i) {
  263. void *scratch = vmalloc(IPCOMP_SCRATCH_SIZE);
  264. if (!scratch)
  265. return NULL;
  266. *per_cpu_ptr(scratches, i) = scratch;
  267. }
  268. return scratches;
  269. }
  270. static void ipcomp_free_tfms(struct crypto_tfm **tfms)
  271. {
  272. struct ipcomp_tfms *pos;
  273. int cpu;
  274. list_for_each_entry(pos, &ipcomp_tfms_list, list) {
  275. if (pos->tfms == tfms)
  276. break;
  277. }
  278. BUG_TRAP(pos);
  279. if (--pos->users)
  280. return;
  281. list_del(&pos->list);
  282. kfree(pos);
  283. if (!tfms)
  284. return;
  285. for_each_possible_cpu(cpu) {
  286. struct crypto_tfm *tfm = *per_cpu_ptr(tfms, cpu);
  287. crypto_free_tfm(tfm);
  288. }
  289. free_percpu(tfms);
  290. }
  291. static struct crypto_tfm **ipcomp_alloc_tfms(const char *alg_name)
  292. {
  293. struct ipcomp_tfms *pos;
  294. struct crypto_tfm **tfms;
  295. int cpu;
  296. /* This can be any valid CPU ID so we don't need locking. */
  297. cpu = raw_smp_processor_id();
  298. list_for_each_entry(pos, &ipcomp_tfms_list, list) {
  299. struct crypto_tfm *tfm;
  300. tfms = pos->tfms;
  301. tfm = *per_cpu_ptr(tfms, cpu);
  302. if (!strcmp(crypto_tfm_alg_name(tfm), alg_name)) {
  303. pos->users++;
  304. return tfms;
  305. }
  306. }
  307. pos = kmalloc(sizeof(*pos), GFP_KERNEL);
  308. if (!pos)
  309. return NULL;
  310. pos->users = 1;
  311. INIT_LIST_HEAD(&pos->list);
  312. list_add(&pos->list, &ipcomp_tfms_list);
  313. pos->tfms = tfms = alloc_percpu(struct crypto_tfm *);
  314. if (!tfms)
  315. goto error;
  316. for_each_possible_cpu(cpu) {
  317. struct crypto_tfm *tfm = crypto_alloc_tfm(alg_name, 0);
  318. if (!tfm)
  319. goto error;
  320. *per_cpu_ptr(tfms, cpu) = tfm;
  321. }
  322. return tfms;
  323. error:
  324. ipcomp_free_tfms(tfms);
  325. return NULL;
  326. }
  327. static void ipcomp_free_data(struct ipcomp_data *ipcd)
  328. {
  329. if (ipcd->tfms)
  330. ipcomp_free_tfms(ipcd->tfms);
  331. ipcomp_free_scratches();
  332. }
  333. static void ipcomp_destroy(struct xfrm_state *x)
  334. {
  335. struct ipcomp_data *ipcd = x->data;
  336. if (!ipcd)
  337. return;
  338. xfrm_state_delete_tunnel(x);
  339. mutex_lock(&ipcomp_resource_mutex);
  340. ipcomp_free_data(ipcd);
  341. mutex_unlock(&ipcomp_resource_mutex);
  342. kfree(ipcd);
  343. }
  344. static int ipcomp_init_state(struct xfrm_state *x)
  345. {
  346. int err;
  347. struct ipcomp_data *ipcd;
  348. struct xfrm_algo_desc *calg_desc;
  349. err = -EINVAL;
  350. if (!x->calg)
  351. goto out;
  352. if (x->encap)
  353. goto out;
  354. err = -ENOMEM;
  355. ipcd = kmalloc(sizeof(*ipcd), GFP_KERNEL);
  356. if (!ipcd)
  357. goto out;
  358. memset(ipcd, 0, sizeof(*ipcd));
  359. x->props.header_len = 0;
  360. if (x->props.mode)
  361. x->props.header_len += sizeof(struct iphdr);
  362. mutex_lock(&ipcomp_resource_mutex);
  363. if (!ipcomp_alloc_scratches())
  364. goto error;
  365. ipcd->tfms = ipcomp_alloc_tfms(x->calg->alg_name);
  366. if (!ipcd->tfms)
  367. goto error;
  368. mutex_unlock(&ipcomp_resource_mutex);
  369. if (x->props.mode) {
  370. err = ipcomp_tunnel_attach(x);
  371. if (err)
  372. goto error_tunnel;
  373. }
  374. calg_desc = xfrm_calg_get_byname(x->calg->alg_name, 0);
  375. BUG_ON(!calg_desc);
  376. ipcd->threshold = calg_desc->uinfo.comp.threshold;
  377. x->data = ipcd;
  378. err = 0;
  379. out:
  380. return err;
  381. error_tunnel:
  382. mutex_lock(&ipcomp_resource_mutex);
  383. error:
  384. ipcomp_free_data(ipcd);
  385. mutex_unlock(&ipcomp_resource_mutex);
  386. kfree(ipcd);
  387. goto out;
  388. }
  389. static struct xfrm_type ipcomp_type = {
  390. .description = "IPCOMP4",
  391. .owner = THIS_MODULE,
  392. .proto = IPPROTO_COMP,
  393. .init_state = ipcomp_init_state,
  394. .destructor = ipcomp_destroy,
  395. .input = ipcomp_input,
  396. .output = ipcomp_output
  397. };
  398. static struct net_protocol ipcomp4_protocol = {
  399. .handler = xfrm4_rcv,
  400. .err_handler = ipcomp4_err,
  401. .no_policy = 1,
  402. };
  403. static int __init ipcomp4_init(void)
  404. {
  405. if (xfrm_register_type(&ipcomp_type, AF_INET) < 0) {
  406. printk(KERN_INFO "ipcomp init: can't add xfrm type\n");
  407. return -EAGAIN;
  408. }
  409. if (inet_add_protocol(&ipcomp4_protocol, IPPROTO_COMP) < 0) {
  410. printk(KERN_INFO "ipcomp init: can't add protocol\n");
  411. xfrm_unregister_type(&ipcomp_type, AF_INET);
  412. return -EAGAIN;
  413. }
  414. return 0;
  415. }
  416. static void __exit ipcomp4_fini(void)
  417. {
  418. if (inet_del_protocol(&ipcomp4_protocol, IPPROTO_COMP) < 0)
  419. printk(KERN_INFO "ip ipcomp close: can't remove protocol\n");
  420. if (xfrm_unregister_type(&ipcomp_type, AF_INET) < 0)
  421. printk(KERN_INFO "ip ipcomp close: can't remove xfrm type\n");
  422. }
  423. module_init(ipcomp4_init);
  424. module_exit(ipcomp4_fini);
  425. MODULE_LICENSE("GPL");
  426. MODULE_DESCRIPTION("IP Payload Compression Protocol (IPComp) - RFC3173");
  427. MODULE_AUTHOR("James Morris <jmorris@intercode.com.au>");