xfrm6_input.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. struct ip6_tnl *t)
  23. {
  24. XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip6 = t;
  25. XFRM_SPI_SKB_CB(skb)->family = AF_INET6;
  26. XFRM_SPI_SKB_CB(skb)->daddroff = offsetof(struct ipv6hdr, daddr);
  27. return xfrm_input(skb, nexthdr, spi, 0);
  28. }
  29. EXPORT_SYMBOL(xfrm6_rcv_spi);
  30. int xfrm6_transport_finish(struct sk_buff *skb, int async)
  31. {
  32. skb_network_header(skb)[IP6CB(skb)->nhoff] =
  33. XFRM_MODE_SKB_CB(skb)->protocol;
  34. #ifndef CONFIG_NETFILTER
  35. if (!async)
  36. return 1;
  37. #endif
  38. ipv6_hdr(skb)->payload_len = htons(skb->len);
  39. __skb_push(skb, skb->data - skb_network_header(skb));
  40. NF_HOOK(NFPROTO_IPV6, NF_INET_PRE_ROUTING,
  41. dev_net(skb->dev), NULL, skb, skb->dev, NULL,
  42. ip6_rcv_finish);
  43. return -1;
  44. }
  45. int xfrm6_rcv_tnl(struct sk_buff *skb, struct ip6_tnl *t)
  46. {
  47. return xfrm6_rcv_spi(skb, skb_network_header(skb)[IP6CB(skb)->nhoff],
  48. 0, t);
  49. }
  50. EXPORT_SYMBOL(xfrm6_rcv_tnl);
  51. int xfrm6_rcv(struct sk_buff *skb)
  52. {
  53. return xfrm6_rcv_tnl(skb, NULL);
  54. }
  55. EXPORT_SYMBOL(xfrm6_rcv);
  56. int xfrm6_input_addr(struct sk_buff *skb, xfrm_address_t *daddr,
  57. xfrm_address_t *saddr, u8 proto)
  58. {
  59. struct net *net = dev_net(skb->dev);
  60. struct xfrm_state *x = NULL;
  61. int i = 0;
  62. /* Allocate new secpath or COW existing one. */
  63. if (!skb->sp || atomic_read(&skb->sp->refcnt) != 1) {
  64. struct sec_path *sp;
  65. sp = secpath_dup(skb->sp);
  66. if (!sp) {
  67. XFRM_INC_STATS(net, LINUX_MIB_XFRMINERROR);
  68. goto drop;
  69. }
  70. if (skb->sp)
  71. secpath_put(skb->sp);
  72. skb->sp = sp;
  73. }
  74. if (1 + skb->sp->len == XFRM_MAX_DEPTH) {
  75. XFRM_INC_STATS(net, LINUX_MIB_XFRMINBUFFERERROR);
  76. goto drop;
  77. }
  78. for (i = 0; i < 3; i++) {
  79. xfrm_address_t *dst, *src;
  80. switch (i) {
  81. case 0:
  82. dst = daddr;
  83. src = saddr;
  84. break;
  85. case 1:
  86. /* lookup state with wild-card source address */
  87. dst = daddr;
  88. src = (xfrm_address_t *)&in6addr_any;
  89. break;
  90. default:
  91. /* lookup state with wild-card addresses */
  92. dst = (xfrm_address_t *)&in6addr_any;
  93. src = (xfrm_address_t *)&in6addr_any;
  94. break;
  95. }
  96. x = xfrm_state_lookup_byaddr(net, skb->mark, dst, src, proto, AF_INET6);
  97. if (!x)
  98. continue;
  99. spin_lock(&x->lock);
  100. if ((!i || (x->props.flags & XFRM_STATE_WILDRECV)) &&
  101. likely(x->km.state == XFRM_STATE_VALID) &&
  102. !xfrm_state_check_expire(x)) {
  103. spin_unlock(&x->lock);
  104. if (x->type->input(x, skb) > 0) {
  105. /* found a valid state */
  106. break;
  107. }
  108. } else
  109. spin_unlock(&x->lock);
  110. xfrm_state_put(x);
  111. x = NULL;
  112. }
  113. if (!x) {
  114. XFRM_INC_STATS(net, LINUX_MIB_XFRMINNOSTATES);
  115. xfrm_audit_state_notfound_simple(skb, AF_INET6);
  116. goto drop;
  117. }
  118. skb->sp->xvec[skb->sp->len++] = x;
  119. spin_lock(&x->lock);
  120. x->curlft.bytes += skb->len;
  121. x->curlft.packets++;
  122. spin_unlock(&x->lock);
  123. return 1;
  124. drop:
  125. return -1;
  126. }
  127. EXPORT_SYMBOL(xfrm6_input_addr);