xfrm4_mode_beet.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 = (struct ip_beet_phdr *)
  51. __skb_pull(skb, XFRM_MODE_SKB_CB(skb)->ihl - hdrlen);
  52. top_iph = ip_hdr(skb);
  53. if (unlikely(optlen)) {
  54. BUG_ON(optlen < 0);
  55. ph->padlen = 4 - (optlen & 4);
  56. ph->hdrlen = optlen / 8;
  57. ph->nexthdr = top_iph->protocol;
  58. if (ph->padlen)
  59. memset(ph + 1, IPOPT_NOP, ph->padlen);
  60. top_iph->protocol = IPPROTO_BEETPH;
  61. top_iph->ihl = sizeof(struct iphdr) / 4;
  62. }
  63. top_iph->saddr = x->props.saddr.a4;
  64. top_iph->daddr = x->id.daddr.a4;
  65. return 0;
  66. }
  67. static int xfrm4_beet_input(struct xfrm_state *x, struct sk_buff *skb)
  68. {
  69. struct iphdr *iph;
  70. int optlen = 0;
  71. int err = -EINVAL;
  72. if (unlikely(XFRM_MODE_SKB_CB(skb)->protocol == IPPROTO_BEETPH)) {
  73. struct ip_beet_phdr *ph;
  74. int phlen;
  75. if (!pskb_may_pull(skb, sizeof(*ph)))
  76. goto out;
  77. ph = (struct ip_beet_phdr *)skb->data;
  78. phlen = sizeof(*ph) + ph->padlen;
  79. optlen = ph->hdrlen * 8 + (IPV4_BEET_PHMAXLEN - phlen);
  80. if (optlen < 0 || optlen & 3 || optlen > 250)
  81. goto out;
  82. XFRM_MODE_SKB_CB(skb)->protocol = ph->nexthdr;
  83. if (!pskb_may_pull(skb, phlen))
  84. goto out;
  85. __skb_pull(skb, phlen);
  86. }
  87. skb_push(skb, sizeof(*iph));
  88. skb_reset_network_header(skb);
  89. skb_mac_header_rebuild(skb);
  90. xfrm4_beet_make_header(skb);
  91. iph = ip_hdr(skb);
  92. iph->ihl += optlen / 4;
  93. iph->tot_len = htons(skb->len);
  94. iph->daddr = x->sel.daddr.a4;
  95. iph->saddr = x->sel.saddr.a4;
  96. iph->check = 0;
  97. iph->check = ip_fast_csum(skb_network_header(skb), iph->ihl);
  98. err = 0;
  99. out:
  100. return err;
  101. }
  102. static struct xfrm_mode xfrm4_beet_mode = {
  103. .input2 = xfrm4_beet_input,
  104. .input = xfrm_prepare_input,
  105. .output2 = xfrm4_beet_output,
  106. .output = xfrm4_prepare_output,
  107. .owner = THIS_MODULE,
  108. .encap = XFRM_MODE_BEET,
  109. .flags = XFRM_MODE_FLAG_TUNNEL,
  110. };
  111. static int __init xfrm4_beet_init(void)
  112. {
  113. return xfrm_register_mode(&xfrm4_beet_mode, AF_INET);
  114. }
  115. static void __exit xfrm4_beet_exit(void)
  116. {
  117. int err;
  118. err = xfrm_unregister_mode(&xfrm4_beet_mode, AF_INET);
  119. BUG_ON(err);
  120. }
  121. module_init(xfrm4_beet_init);
  122. module_exit(xfrm4_beet_exit);
  123. MODULE_LICENSE("GPL");
  124. MODULE_ALIAS_XFRM_MODE(AF_INET, XFRM_MODE_BEET);