xfrm6_mode_beet.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. */
  24. static int xfrm6_beet_output(struct xfrm_state *x, struct sk_buff *skb)
  25. {
  26. struct ipv6hdr *top_iph;
  27. skb_set_network_header(skb, -x->props.header_len);
  28. skb->mac_header = skb->network_header +
  29. offsetof(struct ipv6hdr, nexthdr);
  30. skb->transport_header = skb->network_header + sizeof(*top_iph);
  31. top_iph = ipv6_hdr(skb);
  32. top_iph->version = 6;
  33. memcpy(top_iph->flow_lbl, XFRM_MODE_SKB_CB(skb)->flow_lbl,
  34. sizeof(top_iph->flow_lbl));
  35. top_iph->nexthdr = XFRM_MODE_SKB_CB(skb)->protocol;
  36. ipv6_change_dsfield(top_iph, 0, XFRM_MODE_SKB_CB(skb)->tos);
  37. top_iph->hop_limit = XFRM_MODE_SKB_CB(skb)->ttl;
  38. ipv6_addr_copy(&top_iph->saddr, (struct in6_addr *)&x->props.saddr);
  39. ipv6_addr_copy(&top_iph->daddr, (struct in6_addr *)&x->id.daddr);
  40. return 0;
  41. }
  42. static int xfrm6_beet_input(struct xfrm_state *x, struct sk_buff *skb)
  43. {
  44. struct ipv6hdr *ip6h;
  45. const unsigned char *old_mac;
  46. int size = sizeof(struct ipv6hdr);
  47. int err = -EINVAL;
  48. if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
  49. goto out;
  50. skb_push(skb, size);
  51. memmove(skb->data, skb_network_header(skb), size);
  52. skb_reset_network_header(skb);
  53. old_mac = skb_mac_header(skb);
  54. skb_set_mac_header(skb, -skb->mac_len);
  55. memmove(skb_mac_header(skb), old_mac, skb->mac_len);
  56. ip6h = ipv6_hdr(skb);
  57. ip6h->payload_len = htons(skb->len - size);
  58. ipv6_addr_copy(&ip6h->daddr, (struct in6_addr *) &x->sel.daddr.a6);
  59. ipv6_addr_copy(&ip6h->saddr, (struct in6_addr *) &x->sel.saddr.a6);
  60. err = 0;
  61. out:
  62. return err;
  63. }
  64. static struct xfrm_mode xfrm6_beet_mode = {
  65. .input = xfrm6_beet_input,
  66. .output2 = xfrm6_beet_output,
  67. .output = xfrm6_prepare_output,
  68. .owner = THIS_MODULE,
  69. .encap = XFRM_MODE_BEET,
  70. .flags = XFRM_MODE_FLAG_TUNNEL,
  71. };
  72. static int __init xfrm6_beet_init(void)
  73. {
  74. return xfrm_register_mode(&xfrm6_beet_mode, AF_INET6);
  75. }
  76. static void __exit xfrm6_beet_exit(void)
  77. {
  78. int err;
  79. err = xfrm_unregister_mode(&xfrm6_beet_mode, AF_INET6);
  80. BUG_ON(err);
  81. }
  82. module_init(xfrm6_beet_init);
  83. module_exit(xfrm6_beet_exit);
  84. MODULE_LICENSE("GPL");
  85. MODULE_ALIAS_XFRM_MODE(AF_INET6, XFRM_MODE_BEET);