xfrm6_mode_beet.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. * xfrm6_mode_beet.c - BEET mode encapsulation for IPv6.
  3. *
  4. * Copyright (c) 2006 Diego Beltrami <diego.beltrami@gmail.com>
  5. * Miika Komu <miika@iki.fi>
  6. * Herbert Xu <herbert@gondor.apana.org.au>
  7. * Abhinav Pathak <abhinav.pathak@hiit.fi>
  8. * Jeff Ahrenholz <ahrenholz@gmail.com>
  9. */
  10. #include <linux/init.h>
  11. #include <linux/kernel.h>
  12. #include <linux/module.h>
  13. #include <linux/skbuff.h>
  14. #include <linux/stringify.h>
  15. #include <net/dsfield.h>
  16. #include <net/dst.h>
  17. #include <net/inet_ecn.h>
  18. #include <net/ipv6.h>
  19. #include <net/xfrm.h>
  20. /* Add encapsulation header.
  21. *
  22. * The top IP header will be constructed per draft-nikander-esp-beet-mode-06.txt.
  23. * The following fields in it shall be filled in by x->type->output:
  24. * payload_len
  25. *
  26. * On exit, skb->h will be set to the start of the encapsulation header to be
  27. * filled in by x->type->output and skb->nh will be set to the nextheader field
  28. * of the extension header directly preceding the encapsulation header, or in
  29. * its absence, that of the top IP header. The value of skb->data will always
  30. * point to the top IP header.
  31. */
  32. static int xfrm6_beet_output(struct xfrm_state *x, struct sk_buff *skb)
  33. {
  34. struct ipv6hdr *iph, *top_iph;
  35. u8 *prevhdr;
  36. int hdr_len;
  37. skb_push(skb, x->props.header_len);
  38. iph = skb->nh.ipv6h;
  39. hdr_len = ip6_find_1stfragopt(skb, &prevhdr);
  40. skb->nh.raw = prevhdr - x->props.header_len;
  41. skb->h.raw = skb->data + hdr_len;
  42. memmove(skb->data, iph, hdr_len);
  43. skb->nh.raw = skb->data;
  44. top_iph = skb->nh.ipv6h;
  45. skb->nh.raw = &top_iph->nexthdr;
  46. skb->h.ipv6h = top_iph + 1;
  47. ipv6_addr_copy(&top_iph->saddr, (struct in6_addr *)&x->props.saddr);
  48. ipv6_addr_copy(&top_iph->daddr, (struct in6_addr *)&x->id.daddr);
  49. return 0;
  50. }
  51. static int xfrm6_beet_input(struct xfrm_state *x, struct sk_buff *skb)
  52. {
  53. struct ipv6hdr *ip6h;
  54. int size = sizeof(struct ipv6hdr);
  55. int err = -EINVAL;
  56. if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
  57. goto out;
  58. skb_push(skb, size);
  59. memmove(skb->data, skb->nh.raw, size);
  60. skb->nh.raw = skb->data;
  61. skb->mac.raw = memmove(skb->data - skb->mac_len,
  62. skb->mac.raw, skb->mac_len);
  63. ip6h = skb->nh.ipv6h;
  64. ip6h->payload_len = htons(skb->len - size);
  65. ipv6_addr_copy(&ip6h->daddr, (struct in6_addr *) &x->sel.daddr.a6);
  66. ipv6_addr_copy(&ip6h->saddr, (struct in6_addr *) &x->sel.saddr.a6);
  67. err = 0;
  68. out:
  69. return err;
  70. }
  71. static struct xfrm_mode xfrm6_beet_mode = {
  72. .input = xfrm6_beet_input,
  73. .output = xfrm6_beet_output,
  74. .owner = THIS_MODULE,
  75. .encap = XFRM_MODE_BEET,
  76. };
  77. static int __init xfrm6_beet_init(void)
  78. {
  79. return xfrm_register_mode(&xfrm6_beet_mode, AF_INET6);
  80. }
  81. static void __exit xfrm6_beet_exit(void)
  82. {
  83. int err;
  84. err = xfrm_unregister_mode(&xfrm6_beet_mode, AF_INET6);
  85. BUG_ON(err);
  86. }
  87. module_init(xfrm6_beet_init);
  88. module_exit(xfrm6_beet_exit);
  89. MODULE_LICENSE("GPL");
  90. MODULE_ALIAS_XFRM_MODE(AF_INET6, XFRM_MODE_BEET);