ipcomp6.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  1. /*
  2. * IP Payload Compression Protocol (IPComp) for IPv6 - RFC3173
  3. *
  4. * Copyright (C)2003 USAGI/WIDE Project
  5. *
  6. * Author Mitsuru KANDA <mk@linux-ipv6.org>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. */
  22. /*
  23. * [Memo]
  24. *
  25. * Outbound:
  26. * The compression of IP datagram MUST be done before AH/ESP processing,
  27. * fragmentation, and the addition of Hop-by-Hop/Routing header.
  28. *
  29. * Inbound:
  30. * The decompression of IP datagram MUST be done after the reassembly,
  31. * AH/ESP processing.
  32. */
  33. #include <linux/config.h>
  34. #include <linux/module.h>
  35. #include <net/ip.h>
  36. #include <net/xfrm.h>
  37. #include <net/ipcomp.h>
  38. #include <asm/scatterlist.h>
  39. #include <asm/semaphore.h>
  40. #include <linux/crypto.h>
  41. #include <linux/pfkeyv2.h>
  42. #include <linux/random.h>
  43. #include <linux/percpu.h>
  44. #include <linux/smp.h>
  45. #include <linux/list.h>
  46. #include <linux/vmalloc.h>
  47. #include <linux/rtnetlink.h>
  48. #include <net/icmp.h>
  49. #include <net/ipv6.h>
  50. #include <linux/ipv6.h>
  51. #include <linux/icmpv6.h>
  52. struct ipcomp6_tfms {
  53. struct list_head list;
  54. struct crypto_tfm **tfms;
  55. int users;
  56. };
  57. static DECLARE_MUTEX(ipcomp6_resource_sem);
  58. static void **ipcomp6_scratches;
  59. static int ipcomp6_scratch_users;
  60. static LIST_HEAD(ipcomp6_tfms_list);
  61. static int ipcomp6_input(struct xfrm_state *x, struct xfrm_decap_state *decap, struct sk_buff *skb)
  62. {
  63. int err = 0;
  64. u8 nexthdr = 0;
  65. int hdr_len = skb->h.raw - skb->nh.raw;
  66. unsigned char *tmp_hdr = NULL;
  67. struct ipv6hdr *iph;
  68. int plen, dlen;
  69. struct ipcomp_data *ipcd = x->data;
  70. u8 *start, *scratch;
  71. struct crypto_tfm *tfm;
  72. int cpu;
  73. if ((skb_is_nonlinear(skb) || skb_cloned(skb)) &&
  74. skb_linearize(skb, GFP_ATOMIC) != 0) {
  75. err = -ENOMEM;
  76. goto out;
  77. }
  78. skb->ip_summed = CHECKSUM_NONE;
  79. /* Remove ipcomp header and decompress original payload */
  80. iph = skb->nh.ipv6h;
  81. tmp_hdr = kmalloc(hdr_len, GFP_ATOMIC);
  82. if (!tmp_hdr)
  83. goto out;
  84. memcpy(tmp_hdr, iph, hdr_len);
  85. nexthdr = *(u8 *)skb->data;
  86. skb_pull(skb, sizeof(struct ipv6_comp_hdr));
  87. skb->nh.raw += sizeof(struct ipv6_comp_hdr);
  88. memcpy(skb->nh.raw, tmp_hdr, hdr_len);
  89. iph = skb->nh.ipv6h;
  90. iph->payload_len = htons(ntohs(iph->payload_len) - sizeof(struct ipv6_comp_hdr));
  91. skb->h.raw = skb->data;
  92. /* decompression */
  93. plen = skb->len;
  94. dlen = IPCOMP_SCRATCH_SIZE;
  95. start = skb->data;
  96. cpu = get_cpu();
  97. scratch = *per_cpu_ptr(ipcomp6_scratches, cpu);
  98. tfm = *per_cpu_ptr(ipcd->tfms, cpu);
  99. err = crypto_comp_decompress(tfm, start, plen, scratch, &dlen);
  100. if (err) {
  101. err = -EINVAL;
  102. goto out_put_cpu;
  103. }
  104. if (dlen < (plen + sizeof(struct ipv6_comp_hdr))) {
  105. err = -EINVAL;
  106. goto out_put_cpu;
  107. }
  108. err = pskb_expand_head(skb, 0, dlen - plen, GFP_ATOMIC);
  109. if (err) {
  110. goto out_put_cpu;
  111. }
  112. skb_put(skb, dlen - plen);
  113. memcpy(skb->data, scratch, dlen);
  114. iph = skb->nh.ipv6h;
  115. iph->payload_len = htons(skb->len);
  116. out_put_cpu:
  117. put_cpu();
  118. out:
  119. if (tmp_hdr)
  120. kfree(tmp_hdr);
  121. if (err)
  122. goto error_out;
  123. return nexthdr;
  124. error_out:
  125. return err;
  126. }
  127. static int ipcomp6_output(struct xfrm_state *x, struct sk_buff *skb)
  128. {
  129. int err;
  130. struct ipv6hdr *top_iph;
  131. int hdr_len;
  132. struct ipv6_comp_hdr *ipch;
  133. struct ipcomp_data *ipcd = x->data;
  134. int plen, dlen;
  135. u8 *start, *scratch;
  136. struct crypto_tfm *tfm;
  137. int cpu;
  138. hdr_len = skb->h.raw - skb->data;
  139. /* check whether datagram len is larger than threshold */
  140. if ((skb->len - hdr_len) < ipcd->threshold) {
  141. goto out_ok;
  142. }
  143. if ((skb_is_nonlinear(skb) || skb_cloned(skb)) &&
  144. skb_linearize(skb, GFP_ATOMIC) != 0) {
  145. goto out_ok;
  146. }
  147. /* compression */
  148. plen = skb->len - hdr_len;
  149. dlen = IPCOMP_SCRATCH_SIZE;
  150. start = skb->h.raw;
  151. cpu = get_cpu();
  152. scratch = *per_cpu_ptr(ipcomp6_scratches, cpu);
  153. tfm = *per_cpu_ptr(ipcd->tfms, cpu);
  154. err = crypto_comp_compress(tfm, start, plen, scratch, &dlen);
  155. if (err || (dlen + sizeof(struct ipv6_comp_hdr)) >= plen) {
  156. put_cpu();
  157. goto out_ok;
  158. }
  159. memcpy(start + sizeof(struct ip_comp_hdr), scratch, dlen);
  160. put_cpu();
  161. pskb_trim(skb, hdr_len + dlen + sizeof(struct ip_comp_hdr));
  162. /* insert ipcomp header and replace datagram */
  163. top_iph = (struct ipv6hdr *)skb->data;
  164. top_iph->payload_len = htons(skb->len - sizeof(struct ipv6hdr));
  165. ipch = (struct ipv6_comp_hdr *)start;
  166. ipch->nexthdr = *skb->nh.raw;
  167. ipch->flags = 0;
  168. ipch->cpi = htons((u16 )ntohl(x->id.spi));
  169. *skb->nh.raw = IPPROTO_COMP;
  170. out_ok:
  171. return 0;
  172. }
  173. static void ipcomp6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
  174. int type, int code, int offset, __u32 info)
  175. {
  176. u32 spi;
  177. struct ipv6hdr *iph = (struct ipv6hdr*)skb->data;
  178. struct ipv6_comp_hdr *ipcomph = (struct ipv6_comp_hdr*)(skb->data+offset);
  179. struct xfrm_state *x;
  180. if (type != ICMPV6_DEST_UNREACH && type != ICMPV6_PKT_TOOBIG)
  181. return;
  182. spi = ntohl(ntohs(ipcomph->cpi));
  183. x = xfrm_state_lookup((xfrm_address_t *)&iph->daddr, spi, IPPROTO_COMP, AF_INET6);
  184. if (!x)
  185. return;
  186. printk(KERN_DEBUG "pmtu discovery on SA IPCOMP/%08x/"
  187. "%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x\n",
  188. spi, NIP6(iph->daddr));
  189. xfrm_state_put(x);
  190. }
  191. static struct xfrm_state *ipcomp6_tunnel_create(struct xfrm_state *x)
  192. {
  193. struct xfrm_state *t = NULL;
  194. t = xfrm_state_alloc();
  195. if (!t)
  196. goto out;
  197. t->id.proto = IPPROTO_IPV6;
  198. t->id.spi = xfrm6_tunnel_alloc_spi((xfrm_address_t *)&x->props.saddr);
  199. memcpy(t->id.daddr.a6, x->id.daddr.a6, sizeof(struct in6_addr));
  200. memcpy(&t->sel, &x->sel, sizeof(t->sel));
  201. t->props.family = AF_INET6;
  202. t->props.mode = 1;
  203. memcpy(t->props.saddr.a6, x->props.saddr.a6, sizeof(struct in6_addr));
  204. t->type = xfrm_get_type(IPPROTO_IPV6, t->props.family);
  205. if (t->type == NULL)
  206. goto error;
  207. if (t->type->init_state(t, NULL))
  208. goto error;
  209. t->km.state = XFRM_STATE_VALID;
  210. atomic_set(&t->tunnel_users, 1);
  211. out:
  212. return t;
  213. error:
  214. xfrm_state_put(t);
  215. goto out;
  216. }
  217. static int ipcomp6_tunnel_attach(struct xfrm_state *x)
  218. {
  219. int err = 0;
  220. struct xfrm_state *t = NULL;
  221. u32 spi;
  222. spi = xfrm6_tunnel_spi_lookup((xfrm_address_t *)&x->props.saddr);
  223. if (spi)
  224. t = xfrm_state_lookup((xfrm_address_t *)&x->id.daddr,
  225. spi, IPPROTO_IPV6, AF_INET6);
  226. if (!t) {
  227. t = ipcomp6_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 ipcomp6_free_scratches(void)
  241. {
  242. int i;
  243. void **scratches;
  244. if (--ipcomp6_scratch_users)
  245. return;
  246. scratches = ipcomp6_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 **ipcomp6_alloc_scratches(void)
  257. {
  258. int i;
  259. void **scratches;
  260. if (ipcomp6_scratch_users++)
  261. return ipcomp6_scratches;
  262. scratches = alloc_percpu(void *);
  263. if (!scratches)
  264. return NULL;
  265. ipcomp6_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 ipcomp6_free_tfms(struct crypto_tfm **tfms)
  275. {
  276. struct ipcomp6_tfms *pos;
  277. int cpu;
  278. list_for_each_entry(pos, &ipcomp6_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. if (tfm)
  292. crypto_free_tfm(tfm);
  293. }
  294. free_percpu(tfms);
  295. }
  296. static struct crypto_tfm **ipcomp6_alloc_tfms(const char *alg_name)
  297. {
  298. struct ipcomp6_tfms *pos;
  299. struct crypto_tfm **tfms;
  300. int cpu;
  301. /* This can be any valid CPU ID so we don't need locking. */
  302. cpu = smp_processor_id();
  303. list_for_each_entry(pos, &ipcomp6_tfms_list, list) {
  304. struct crypto_tfm *tfm;
  305. tfms = pos->tfms;
  306. tfm = *per_cpu_ptr(tfms, cpu);
  307. if (!strcmp(crypto_tfm_alg_name(tfm), alg_name)) {
  308. pos->users++;
  309. return tfms;
  310. }
  311. }
  312. pos = kmalloc(sizeof(*pos), GFP_KERNEL);
  313. if (!pos)
  314. return NULL;
  315. pos->users = 1;
  316. INIT_LIST_HEAD(&pos->list);
  317. list_add(&pos->list, &ipcomp6_tfms_list);
  318. pos->tfms = tfms = alloc_percpu(struct crypto_tfm *);
  319. if (!tfms)
  320. goto error;
  321. for_each_cpu(cpu) {
  322. struct crypto_tfm *tfm = crypto_alloc_tfm(alg_name, 0);
  323. if (!tfm)
  324. goto error;
  325. *per_cpu_ptr(tfms, cpu) = tfm;
  326. }
  327. return tfms;
  328. error:
  329. ipcomp6_free_tfms(tfms);
  330. return NULL;
  331. }
  332. static void ipcomp6_free_data(struct ipcomp_data *ipcd)
  333. {
  334. if (ipcd->tfms)
  335. ipcomp6_free_tfms(ipcd->tfms);
  336. ipcomp6_free_scratches();
  337. }
  338. static void ipcomp6_destroy(struct xfrm_state *x)
  339. {
  340. struct ipcomp_data *ipcd = x->data;
  341. if (!ipcd)
  342. return;
  343. xfrm_state_delete_tunnel(x);
  344. down(&ipcomp6_resource_sem);
  345. ipcomp6_free_data(ipcd);
  346. up(&ipcomp6_resource_sem);
  347. kfree(ipcd);
  348. xfrm6_tunnel_free_spi((xfrm_address_t *)&x->props.saddr);
  349. }
  350. static int ipcomp6_init_state(struct xfrm_state *x, void *args)
  351. {
  352. int err;
  353. struct ipcomp_data *ipcd;
  354. struct xfrm_algo_desc *calg_desc;
  355. err = -EINVAL;
  356. if (!x->calg)
  357. goto out;
  358. if (x->encap)
  359. goto out;
  360. err = -ENOMEM;
  361. ipcd = kmalloc(sizeof(*ipcd), GFP_KERNEL);
  362. if (!ipcd)
  363. goto out;
  364. memset(ipcd, 0, sizeof(*ipcd));
  365. x->props.header_len = 0;
  366. if (x->props.mode)
  367. x->props.header_len += sizeof(struct ipv6hdr);
  368. down(&ipcomp6_resource_sem);
  369. if (!ipcomp6_alloc_scratches())
  370. goto error;
  371. ipcd->tfms = ipcomp6_alloc_tfms(x->calg->alg_name);
  372. if (!ipcd->tfms)
  373. goto error;
  374. up(&ipcomp6_resource_sem);
  375. if (x->props.mode) {
  376. err = ipcomp6_tunnel_attach(x);
  377. if (err)
  378. goto error_tunnel;
  379. }
  380. calg_desc = xfrm_calg_get_byname(x->calg->alg_name, 0);
  381. BUG_ON(!calg_desc);
  382. ipcd->threshold = calg_desc->uinfo.comp.threshold;
  383. x->data = ipcd;
  384. err = 0;
  385. out:
  386. return err;
  387. error_tunnel:
  388. down(&ipcomp6_resource_sem);
  389. error:
  390. ipcomp6_free_data(ipcd);
  391. up(&ipcomp6_resource_sem);
  392. kfree(ipcd);
  393. goto out;
  394. }
  395. static struct xfrm_type ipcomp6_type =
  396. {
  397. .description = "IPCOMP6",
  398. .owner = THIS_MODULE,
  399. .proto = IPPROTO_COMP,
  400. .init_state = ipcomp6_init_state,
  401. .destructor = ipcomp6_destroy,
  402. .input = ipcomp6_input,
  403. .output = ipcomp6_output,
  404. };
  405. static struct inet6_protocol ipcomp6_protocol =
  406. {
  407. .handler = xfrm6_rcv,
  408. .err_handler = ipcomp6_err,
  409. .flags = INET6_PROTO_NOPOLICY,
  410. };
  411. static int __init ipcomp6_init(void)
  412. {
  413. if (xfrm_register_type(&ipcomp6_type, AF_INET6) < 0) {
  414. printk(KERN_INFO "ipcomp6 init: can't add xfrm type\n");
  415. return -EAGAIN;
  416. }
  417. if (inet6_add_protocol(&ipcomp6_protocol, IPPROTO_COMP) < 0) {
  418. printk(KERN_INFO "ipcomp6 init: can't add protocol\n");
  419. xfrm_unregister_type(&ipcomp6_type, AF_INET6);
  420. return -EAGAIN;
  421. }
  422. return 0;
  423. }
  424. static void __exit ipcomp6_fini(void)
  425. {
  426. if (inet6_del_protocol(&ipcomp6_protocol, IPPROTO_COMP) < 0)
  427. printk(KERN_INFO "ipv6 ipcomp close: can't remove protocol\n");
  428. if (xfrm_unregister_type(&ipcomp6_type, AF_INET6) < 0)
  429. printk(KERN_INFO "ipv6 ipcomp close: can't remove xfrm type\n");
  430. }
  431. module_init(ipcomp6_init);
  432. module_exit(ipcomp6_fini);
  433. MODULE_LICENSE("GPL");
  434. MODULE_DESCRIPTION("IP Payload Compression Protocol (IPComp) for IPv6 - RFC3173");
  435. MODULE_AUTHOR("Mitsuru KANDA <mk@linux-ipv6.org>");