xfrm4_mode_beet.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. * xfrm4_mode_beet.c - BEET mode encapsulation for IPv4.
  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/dst.h>
  16. #include <net/ip.h>
  17. #include <net/xfrm.h>
  18. static void xfrm4_beet_make_header(struct sk_buff *skb)
  19. {
  20. struct iphdr *iph = ip_hdr(skb);
  21. iph->ihl = 5;
  22. iph->version = 4;
  23. iph->protocol = XFRM_MODE_SKB_CB(skb)->protocol;
  24. iph->tos = XFRM_MODE_SKB_CB(skb)->tos;
  25. iph->id = XFRM_MODE_SKB_CB(skb)->id;
  26. iph->frag_off = XFRM_MODE_SKB_CB(skb)->frag_off;
  27. iph->ttl = XFRM_MODE_SKB_CB(skb)->ttl;
  28. }
  29. /* Add encapsulation header.
  30. *
  31. * The top IP header will be constructed per draft-nikander-esp-beet-mode-06.txt.
  32. */
  33. static int xfrm4_beet_output(struct xfrm_state *x, struct sk_buff *skb)
  34. {
  35. struct ip_beet_phdr *ph;
  36. struct iphdr *top_iph;
  37. int hdrlen, optlen;
  38. hdrlen = 0;
  39. optlen = XFRM_MODE_SKB_CB(skb)->optlen;
  40. if (unlikely(optlen))
  41. hdrlen += IPV4_BEET_PHMAXLEN - (optlen & 4);
  42. skb_set_network_header(skb, -x->props.header_len -
  43. hdrlen + (XFRM_MODE_SKB_CB(skb)->ihl - sizeof(*top_iph)));
  44. if (x->sel.family != AF_INET6)
  45. skb->network_header += IPV4_BEET_PHMAXLEN;
  46. skb->mac_header = skb->network_header +
  47. offsetof(struct iphdr, protocol);
  48. skb->transport_header = skb->network_header + sizeof(*top_iph);
  49. xfrm4_beet_make_header(skb);
  50. ph = __skb_pull(skb, XFRM_MODE_SKB_CB(skb)->ihl - hdrlen);
  51. top_iph = ip_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->protocol;
  57. if (ph->padlen)
  58. memset(ph + 1, IPOPT_NOP, ph->padlen);
  59. top_iph->protocol = IPPROTO_BEETPH;
  60. top_iph->ihl = sizeof(struct iphdr) / 4;
  61. }
  62. top_iph->saddr = x->props.saddr.a4;
  63. top_iph->daddr = x->id.daddr.a4;
  64. return 0;
  65. }
  66. static int xfrm4_beet_input(struct xfrm_state *x, struct sk_buff *skb)
  67. {
  68. struct iphdr *iph;
  69. int optlen = 0;
  70. int err = -EINVAL;
  71. if (unlikely(XFRM_MODE_SKB_CB(skb)->protocol == IPPROTO_BEETPH)) {
  72. struct ip_beet_phdr *ph;
  73. int phlen;
  74. if (!pskb_may_pull(skb, sizeof(*ph)))
  75. goto out;
  76. ph = (struct ip_beet_phdr *)skb->data;
  77. phlen = sizeof(*ph) + ph->padlen;
  78. optlen = ph->hdrlen * 8 + (IPV4_BEET_PHMAXLEN - phlen);
  79. if (optlen < 0 || optlen & 3 || optlen > 250)
  80. goto out;
  81. XFRM_MODE_SKB_CB(skb)->protocol = ph->nexthdr;
  82. if (!pskb_may_pull(skb, phlen))
  83. goto out;
  84. __skb_pull(skb, phlen);
  85. }
  86. skb_push(skb, sizeof(*iph));
  87. skb_reset_network_header(skb);
  88. skb_mac_header_rebuild(skb);
  89. xfrm4_beet_make_header(skb);
  90. iph = ip_hdr(skb);
  91. iph->ihl += optlen / 4;
  92. iph->tot_len = htons(skb->len);
  93. iph->daddr = x->sel.daddr.a4;
  94. iph->saddr = x->sel.saddr.a4;
  95. iph->check = 0;
  96. iph->check = ip_fast_csum(skb_network_header(skb), iph->ihl);
  97. err = 0;
  98. out:
  99. return err;
  100. }
  101. static struct xfrm_mode xfrm4_beet_mode = {
  102. .input2 = xfrm4_beet_input,
  103. .input = xfrm_prepare_input,
  104. .output2 = xfrm4_beet_output,
  105. .output = xfrm4_prepare_output,
  106. .owner = THIS_MODULE,
  107. .encap = XFRM_MODE_BEET,
  108. .flags = XFRM_MODE_FLAG_TUNNEL,
  109. };
  110. static int __init xfrm4_beet_init(void)
  111. {
  112. return xfrm_register_mode(&xfrm4_beet_mode, AF_INET);
  113. }
  114. static void __exit xfrm4_beet_exit(void)
  115. {
  116. int err;
  117. err = xfrm_unregister_mode(&xfrm4_beet_mode, AF_INET);
  118. BUG_ON(err);
  119. }
  120. module_init(xfrm4_beet_init);
  121. module_exit(xfrm4_beet_exit);
  122. MODULE_LICENSE("GPL");
  123. MODULE_ALIAS_XFRM_MODE(AF_INET, XFRM_MODE_BEET);