xfrm6_mode_beet.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. struct ip_beet_phdr *ph;
  38. int optlen, hdr_len;
  39. hdr_len = 0;
  40. optlen = XFRM_MODE_SKB_CB(skb)->optlen;
  41. if (unlikely(optlen))
  42. hdr_len += IPV4_BEET_PHMAXLEN - (optlen & 4);
  43. skb_set_network_header(skb, -x->props.header_len - hdr_len);
  44. if (x->sel.family != AF_INET6)
  45. skb->network_header += IPV4_BEET_PHMAXLEN;
  46. skb->mac_header = skb->network_header +
  47. offsetof(struct ipv6hdr, nexthdr);
  48. skb->transport_header = skb->network_header + sizeof(*top_iph);
  49. ph = __skb_pull(skb, XFRM_MODE_SKB_CB(skb)->ihl - hdr_len);
  50. xfrm6_beet_make_header(skb);
  51. top_iph = ipv6_hdr(skb);
  52. if (unlikely(optlen)) {
  53. BUG_ON(optlen < 0);
  54. ph->padlen = 4 - (optlen & 4);
  55. ph->hdrlen = optlen / 8;
  56. ph->nexthdr = top_iph->nexthdr;
  57. if (ph->padlen)
  58. memset(ph + 1, IPOPT_NOP, ph->padlen);
  59. top_iph->nexthdr = IPPROTO_BEETPH;
  60. }
  61. top_iph->saddr = *(struct in6_addr *)&x->props.saddr;
  62. top_iph->daddr = *(struct in6_addr *)&x->id.daddr;
  63. return 0;
  64. }
  65. static int xfrm6_beet_input(struct xfrm_state *x, struct sk_buff *skb)
  66. {
  67. struct ipv6hdr *ip6h;
  68. int size = sizeof(struct ipv6hdr);
  69. int err;
  70. err = skb_cow_head(skb, size + skb->mac_len);
  71. if (err)
  72. goto out;
  73. __skb_push(skb, size);
  74. skb_reset_network_header(skb);
  75. skb_mac_header_rebuild(skb);
  76. xfrm6_beet_make_header(skb);
  77. ip6h = ipv6_hdr(skb);
  78. ip6h->payload_len = htons(skb->len - size);
  79. ip6h->daddr = x->sel.daddr.in6;
  80. ip6h->saddr = x->sel.saddr.in6;
  81. err = 0;
  82. out:
  83. return err;
  84. }
  85. static struct xfrm_mode xfrm6_beet_mode = {
  86. .input2 = xfrm6_beet_input,
  87. .input = xfrm_prepare_input,
  88. .output2 = xfrm6_beet_output,
  89. .output = xfrm6_prepare_output,
  90. .owner = THIS_MODULE,
  91. .encap = XFRM_MODE_BEET,
  92. .flags = XFRM_MODE_FLAG_TUNNEL,
  93. };
  94. static int __init xfrm6_beet_init(void)
  95. {
  96. return xfrm_register_mode(&xfrm6_beet_mode, AF_INET6);
  97. }
  98. static void __exit xfrm6_beet_exit(void)
  99. {
  100. int err;
  101. err = xfrm_unregister_mode(&xfrm6_beet_mode, AF_INET6);
  102. BUG_ON(err);
  103. }
  104. module_init(xfrm6_beet_init);
  105. module_exit(xfrm6_beet_exit);
  106. MODULE_LICENSE("GPL");
  107. MODULE_ALIAS_XFRM_MODE(AF_INET6, XFRM_MODE_BEET);