xfrm6_mode_beet.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. const unsigned char *old_mac;
  55. int size = sizeof(struct ipv6hdr);
  56. int err = -EINVAL;
  57. if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
  58. goto out;
  59. skb_push(skb, size);
  60. memmove(skb->data, skb->nh.raw, size);
  61. skb->nh.raw = skb->data;
  62. old_mac = skb->mac.raw;
  63. skb_set_mac_header(skb, -skb->mac_len);
  64. memmove(skb->mac.raw, old_mac, skb->mac_len);
  65. ip6h = skb->nh.ipv6h;
  66. ip6h->payload_len = htons(skb->len - size);
  67. ipv6_addr_copy(&ip6h->daddr, (struct in6_addr *) &x->sel.daddr.a6);
  68. ipv6_addr_copy(&ip6h->saddr, (struct in6_addr *) &x->sel.saddr.a6);
  69. err = 0;
  70. out:
  71. return err;
  72. }
  73. static struct xfrm_mode xfrm6_beet_mode = {
  74. .input = xfrm6_beet_input,
  75. .output = xfrm6_beet_output,
  76. .owner = THIS_MODULE,
  77. .encap = XFRM_MODE_BEET,
  78. };
  79. static int __init xfrm6_beet_init(void)
  80. {
  81. return xfrm_register_mode(&xfrm6_beet_mode, AF_INET6);
  82. }
  83. static void __exit xfrm6_beet_exit(void)
  84. {
  85. int err;
  86. err = xfrm_unregister_mode(&xfrm6_beet_mode, AF_INET6);
  87. BUG_ON(err);
  88. }
  89. module_init(xfrm6_beet_init);
  90. module_exit(xfrm6_beet_exit);
  91. MODULE_LICENSE("GPL");
  92. MODULE_ALIAS_XFRM_MODE(AF_INET6, XFRM_MODE_BEET);