xfrm6_input.c 3.2 KB

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