xfrm4_mode_beet.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. /* Add encapsulation header.
  19. *
  20. * The top IP header will be constructed per draft-nikander-esp-beet-mode-06.txt.
  21. * The following fields in it shall be filled in by x->type->output:
  22. * tot_len
  23. * check
  24. *
  25. * On exit, skb->h will be set to the start of the payload to be processed
  26. * by x->type->output and skb->nh will be set to the top IP header.
  27. */
  28. static int xfrm4_beet_output(struct xfrm_state *x, struct sk_buff *skb)
  29. {
  30. struct iphdr *iph, *top_iph = NULL;
  31. int hdrlen, optlen;
  32. iph = skb->nh.iph;
  33. skb->h.ipiph = iph;
  34. hdrlen = 0;
  35. optlen = iph->ihl * 4 - sizeof(*iph);
  36. if (unlikely(optlen))
  37. hdrlen += IPV4_BEET_PHMAXLEN - (optlen & 4);
  38. skb->nh.raw = skb_push(skb, x->props.header_len + hdrlen);
  39. top_iph = skb->nh.iph;
  40. skb->h.raw += sizeof(*iph) - hdrlen;
  41. memmove(top_iph, iph, sizeof(*iph));
  42. if (unlikely(optlen)) {
  43. struct ip_beet_phdr *ph;
  44. BUG_ON(optlen < 0);
  45. ph = (struct ip_beet_phdr *)skb->h.raw;
  46. ph->padlen = 4 - (optlen & 4);
  47. ph->hdrlen = (optlen + ph->padlen + sizeof(*ph)) / 8;
  48. ph->nexthdr = top_iph->protocol;
  49. if (ph->padlen)
  50. memset(ph + 1, IPOPT_NOP, ph->padlen);
  51. top_iph->protocol = IPPROTO_BEETPH;
  52. top_iph->ihl = sizeof(struct iphdr) / 4;
  53. }
  54. top_iph->saddr = x->props.saddr.a4;
  55. top_iph->daddr = x->id.daddr.a4;
  56. return 0;
  57. }
  58. static int xfrm4_beet_input(struct xfrm_state *x, struct sk_buff *skb)
  59. {
  60. struct iphdr *iph = skb->nh.iph;
  61. int phlen = 0;
  62. int optlen = 0;
  63. __u8 ph_nexthdr = 0, protocol = 0;
  64. int err = -EINVAL;
  65. protocol = iph->protocol;
  66. if (unlikely(iph->protocol == IPPROTO_BEETPH)) {
  67. struct ip_beet_phdr *ph = (struct ip_beet_phdr*)(iph + 1);
  68. if (!pskb_may_pull(skb, sizeof(*ph)))
  69. goto out;
  70. phlen = sizeof(*ph) + ph->padlen;
  71. optlen = ph->hdrlen * 8 - phlen;
  72. if (optlen < 0 || optlen & 3 || optlen > 250)
  73. goto out;
  74. if (!pskb_may_pull(skb, phlen + optlen))
  75. goto out;
  76. ph_nexthdr = ph->nexthdr;
  77. }
  78. skb->nh.raw = skb->data + (phlen - sizeof(*iph));
  79. memmove(skb->nh.raw, iph, sizeof(*iph));
  80. skb->h.raw = skb->data + (phlen + optlen);
  81. iph = skb->nh.iph;
  82. iph->ihl = (sizeof(*iph) + optlen) / 4;
  83. iph->tot_len = htons(skb->len + iph->ihl * 4);
  84. iph->daddr = x->sel.daddr.a4;
  85. iph->saddr = x->sel.saddr.a4;
  86. if (ph_nexthdr)
  87. iph->protocol = ph_nexthdr;
  88. else
  89. iph->protocol = protocol;
  90. iph->check = 0;
  91. iph->check = ip_fast_csum(skb->nh.raw, iph->ihl);
  92. err = 0;
  93. out:
  94. return err;
  95. }
  96. static struct xfrm_mode xfrm4_beet_mode = {
  97. .input = xfrm4_beet_input,
  98. .output = xfrm4_beet_output,
  99. .owner = THIS_MODULE,
  100. .encap = XFRM_MODE_BEET,
  101. };
  102. static int __init xfrm4_beet_init(void)
  103. {
  104. return xfrm_register_mode(&xfrm4_beet_mode, AF_INET);
  105. }
  106. static void __exit xfrm4_beet_exit(void)
  107. {
  108. int err;
  109. err = xfrm_unregister_mode(&xfrm4_beet_mode, AF_INET);
  110. BUG_ON(err);
  111. }
  112. module_init(xfrm4_beet_init);
  113. module_exit(xfrm4_beet_exit);
  114. MODULE_LICENSE("GPL");
  115. MODULE_ALIAS_XFRM_MODE(AF_INET, XFRM_MODE_BEET);