xfrm6_mode_beet.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 the mac header will be set to the
  28. * nextheader field of the extension header directly preceding the
  29. * encapsulation header, or in its absence, that of the top IP header.
  30. * The value of skb->data and the network header will always point to the
  31. * top IP header.
  32. */
  33. static int xfrm6_beet_output(struct xfrm_state *x, struct sk_buff *skb)
  34. {
  35. struct ipv6hdr *iph, *top_iph;
  36. u8 *prevhdr;
  37. int hdr_len;
  38. skb_push(skb, x->props.header_len);
  39. iph = ipv6_hdr(skb);
  40. hdr_len = ip6_find_1stfragopt(skb, &prevhdr);
  41. memmove(skb->data, iph, hdr_len);
  42. skb_set_mac_header(skb, (prevhdr - x->props.header_len) - skb->data);
  43. skb_reset_network_header(skb);
  44. skb_set_transport_header(skb, hdr_len);
  45. top_iph = ipv6_hdr(skb);
  46. ipv6_addr_copy(&top_iph->saddr, (struct in6_addr *)&x->props.saddr);
  47. ipv6_addr_copy(&top_iph->daddr, (struct in6_addr *)&x->id.daddr);
  48. return 0;
  49. }
  50. static int xfrm6_beet_input(struct xfrm_state *x, struct sk_buff *skb)
  51. {
  52. struct ipv6hdr *ip6h;
  53. const unsigned char *old_mac;
  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_network_header(skb), size);
  60. skb_reset_network_header(skb);
  61. old_mac = skb_mac_header(skb);
  62. skb_set_mac_header(skb, -skb->mac_len);
  63. memmove(skb_mac_header(skb), old_mac, skb->mac_len);
  64. ip6h = ipv6_hdr(skb);
  65. ip6h->payload_len = htons(skb->len - size);
  66. ipv6_addr_copy(&ip6h->daddr, (struct in6_addr *) &x->sel.daddr.a6);
  67. ipv6_addr_copy(&ip6h->saddr, (struct in6_addr *) &x->sel.saddr.a6);
  68. err = 0;
  69. out:
  70. return err;
  71. }
  72. static struct xfrm_mode xfrm6_beet_mode = {
  73. .input = xfrm6_beet_input,
  74. .output = xfrm6_beet_output,
  75. .owner = THIS_MODULE,
  76. .encap = XFRM_MODE_BEET,
  77. };
  78. static int __init xfrm6_beet_init(void)
  79. {
  80. return xfrm_register_mode(&xfrm6_beet_mode, AF_INET6);
  81. }
  82. static void __exit xfrm6_beet_exit(void)
  83. {
  84. int err;
  85. err = xfrm_unregister_mode(&xfrm6_beet_mode, AF_INET6);
  86. BUG_ON(err);
  87. }
  88. module_init(xfrm6_beet_init);
  89. module_exit(xfrm6_beet_exit);
  90. MODULE_LICENSE("GPL");
  91. MODULE_ALIAS_XFRM_MODE(AF_INET6, XFRM_MODE_BEET);