xfrm6_mode_beet.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. static void xfrm6_beet_make_header(struct sk_buff *skb)
  21. {
  22. struct ipv6hdr *iph = ipv6_hdr(skb);
  23. iph->version = 6;
  24. memcpy(iph->flow_lbl, XFRM_MODE_SKB_CB(skb)->flow_lbl,
  25. sizeof(iph->flow_lbl));
  26. iph->nexthdr = XFRM_MODE_SKB_CB(skb)->protocol;
  27. ipv6_change_dsfield(iph, 0, XFRM_MODE_SKB_CB(skb)->tos);
  28. iph->hop_limit = XFRM_MODE_SKB_CB(skb)->ttl;
  29. }
  30. /* Add encapsulation header.
  31. *
  32. * The top IP header will be constructed per draft-nikander-esp-beet-mode-06.txt.
  33. */
  34. static int xfrm6_beet_output(struct xfrm_state *x, struct sk_buff *skb)
  35. {
  36. struct ipv6hdr *top_iph;
  37. skb_set_network_header(skb, -x->props.header_len);
  38. skb->mac_header = skb->network_header +
  39. offsetof(struct ipv6hdr, nexthdr);
  40. skb->transport_header = skb->network_header + sizeof(*top_iph);
  41. __skb_pull(skb, XFRM_MODE_SKB_CB(skb)->ihl);
  42. xfrm6_beet_make_header(skb);
  43. top_iph = ipv6_hdr(skb);
  44. ipv6_addr_copy(&top_iph->saddr, (struct in6_addr *)&x->props.saddr);
  45. ipv6_addr_copy(&top_iph->daddr, (struct in6_addr *)&x->id.daddr);
  46. return 0;
  47. }
  48. static int xfrm6_beet_input(struct xfrm_state *x, struct sk_buff *skb)
  49. {
  50. struct ipv6hdr *ip6h;
  51. const unsigned char *old_mac;
  52. int size = sizeof(struct ipv6hdr);
  53. int err;
  54. err = skb_cow_head(skb, size + skb->mac_len);
  55. if (err)
  56. goto out;
  57. __skb_push(skb, size);
  58. skb_reset_network_header(skb);
  59. old_mac = skb_mac_header(skb);
  60. skb_set_mac_header(skb, -skb->mac_len);
  61. memmove(skb_mac_header(skb), old_mac, skb->mac_len);
  62. xfrm6_beet_make_header(skb);
  63. ip6h = ipv6_hdr(skb);
  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. .input2 = xfrm6_beet_input,
  73. .input = xfrm_prepare_input,
  74. .output2 = xfrm6_beet_output,
  75. .output = xfrm6_prepare_output,
  76. .owner = THIS_MODULE,
  77. .encap = XFRM_MODE_BEET,
  78. .flags = XFRM_MODE_FLAG_TUNNEL,
  79. };
  80. static int __init xfrm6_beet_init(void)
  81. {
  82. return xfrm_register_mode(&xfrm6_beet_mode, AF_INET6);
  83. }
  84. static void __exit xfrm6_beet_exit(void)
  85. {
  86. int err;
  87. err = xfrm_unregister_mode(&xfrm6_beet_mode, AF_INET6);
  88. BUG_ON(err);
  89. }
  90. module_init(xfrm6_beet_init);
  91. module_exit(xfrm6_beet_exit);
  92. MODULE_LICENSE("GPL");
  93. MODULE_ALIAS_XFRM_MODE(AF_INET6, XFRM_MODE_BEET);