xfrm6_input.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. * xfrm6_input.c: based on net/ipv4/xfrm4_input.c
  3. *
  4. * Authors:
  5. * Mitsuru KANDA @USAGI
  6. * Kazunori MIYAZAWA @USAGI
  7. * Kunihiro Ishiguro <kunihiro@ipinfusion.com>
  8. * YOSHIFUJI Hideaki @USAGI
  9. * IPv6 support
  10. */
  11. #include <linux/module.h>
  12. #include <linux/string.h>
  13. #include <linux/netfilter.h>
  14. #include <linux/netfilter_ipv6.h>
  15. #include <net/ipv6.h>
  16. #include <net/xfrm.h>
  17. int xfrm6_extract_input(struct xfrm_state *x, struct sk_buff *skb)
  18. {
  19. return xfrm6_extract_header(skb);
  20. }
  21. int xfrm6_rcv_spi(struct sk_buff *skb, int nexthdr, __be32 spi)
  22. {
  23. XFRM_SPI_SKB_CB(skb)->daddroff = offsetof(struct ipv6hdr, daddr);
  24. return xfrm_input(skb, nexthdr, spi, 0);
  25. }
  26. EXPORT_SYMBOL(xfrm6_rcv_spi);
  27. int xfrm6_transport_finish(struct sk_buff *skb, int async)
  28. {
  29. skb_network_header(skb)[IP6CB(skb)->nhoff] =
  30. XFRM_MODE_SKB_CB(skb)->protocol;
  31. #ifdef CONFIG_NETFILTER
  32. ipv6_hdr(skb)->payload_len = htons(skb->len);
  33. __skb_push(skb, skb->data - skb_network_header(skb));
  34. NF_HOOK(PF_INET6, NF_IP6_PRE_ROUTING, skb, skb->dev, NULL,
  35. ip6_rcv_finish);
  36. return -1;
  37. #else
  38. if (async)
  39. return ip6_rcv_finish(skb);
  40. return 1;
  41. #endif
  42. }
  43. int xfrm6_rcv(struct sk_buff *skb)
  44. {
  45. return xfrm6_rcv_spi(skb, skb_network_header(skb)[IP6CB(skb)->nhoff],
  46. 0);
  47. }
  48. EXPORT_SYMBOL(xfrm6_rcv);
  49. int xfrm6_input_addr(struct sk_buff *skb, xfrm_address_t *daddr,
  50. xfrm_address_t *saddr, u8 proto)
  51. {
  52. struct xfrm_state *x = NULL;
  53. int wildcard = 0;
  54. xfrm_address_t *xany;
  55. struct xfrm_state *xfrm_vec_one = NULL;
  56. int nh = 0;
  57. int i = 0;
  58. xany = (xfrm_address_t *)&in6addr_any;
  59. for (i = 0; i < 3; i++) {
  60. xfrm_address_t *dst, *src;
  61. switch (i) {
  62. case 0:
  63. dst = daddr;
  64. src = saddr;
  65. break;
  66. case 1:
  67. /* lookup state with wild-card source address */
  68. wildcard = 1;
  69. dst = daddr;
  70. src = xany;
  71. break;
  72. case 2:
  73. default:
  74. /* lookup state with wild-card addresses */
  75. wildcard = 1; /* XXX */
  76. dst = xany;
  77. src = xany;
  78. break;
  79. }
  80. x = xfrm_state_lookup_byaddr(dst, src, proto, AF_INET6);
  81. if (!x)
  82. continue;
  83. spin_lock(&x->lock);
  84. if (wildcard) {
  85. if ((x->props.flags & XFRM_STATE_WILDRECV) == 0) {
  86. spin_unlock(&x->lock);
  87. xfrm_state_put(x);
  88. x = NULL;
  89. continue;
  90. }
  91. }
  92. if (unlikely(x->km.state != XFRM_STATE_VALID)) {
  93. spin_unlock(&x->lock);
  94. xfrm_state_put(x);
  95. x = NULL;
  96. continue;
  97. }
  98. if (xfrm_state_check_expire(x)) {
  99. spin_unlock(&x->lock);
  100. xfrm_state_put(x);
  101. x = NULL;
  102. continue;
  103. }
  104. nh = x->type->input(x, skb);
  105. if (nh <= 0) {
  106. spin_unlock(&x->lock);
  107. xfrm_state_put(x);
  108. x = NULL;
  109. continue;
  110. }
  111. x->curlft.bytes += skb->len;
  112. x->curlft.packets++;
  113. spin_unlock(&x->lock);
  114. xfrm_vec_one = x;
  115. break;
  116. }
  117. if (!xfrm_vec_one)
  118. goto drop;
  119. /* Allocate new secpath or COW existing one. */
  120. if (!skb->sp || atomic_read(&skb->sp->refcnt) != 1) {
  121. struct sec_path *sp;
  122. sp = secpath_dup(skb->sp);
  123. if (!sp)
  124. goto drop;
  125. if (skb->sp)
  126. secpath_put(skb->sp);
  127. skb->sp = sp;
  128. }
  129. if (1 + skb->sp->len > XFRM_MAX_DEPTH)
  130. goto drop;
  131. skb->sp->xvec[skb->sp->len] = xfrm_vec_one;
  132. skb->sp->len ++;
  133. return 1;
  134. drop:
  135. if (xfrm_vec_one)
  136. xfrm_state_put(xfrm_vec_one);
  137. return -1;
  138. }
  139. EXPORT_SYMBOL(xfrm6_input_addr);