xfrm4_input.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. * xfrm4_input.c
  3. *
  4. * Changes:
  5. * YOSHIFUJI Hideaki @USAGI
  6. * Split up af-specific portion
  7. * Derek Atkins <derek@ihtfp.com>
  8. * Add Encapsulation support
  9. *
  10. */
  11. #include <linux/module.h>
  12. #include <linux/string.h>
  13. #include <linux/netfilter.h>
  14. #include <linux/netfilter_ipv4.h>
  15. #include <net/ip.h>
  16. #include <net/xfrm.h>
  17. int xfrm4_rcv(struct sk_buff *skb)
  18. {
  19. return xfrm4_rcv_encap(skb, 0);
  20. }
  21. EXPORT_SYMBOL(xfrm4_rcv);
  22. static int xfrm4_parse_spi(struct sk_buff *skb, u8 nexthdr, __be32 *spi, __be32 *seq)
  23. {
  24. switch (nexthdr) {
  25. case IPPROTO_IPIP:
  26. *spi = skb->nh.iph->saddr;
  27. *seq = 0;
  28. return 0;
  29. }
  30. return xfrm_parse_spi(skb, nexthdr, spi, seq);
  31. }
  32. #ifdef CONFIG_NETFILTER
  33. static inline int xfrm4_rcv_encap_finish(struct sk_buff *skb)
  34. {
  35. struct iphdr *iph = skb->nh.iph;
  36. if (skb->dst == NULL) {
  37. if (ip_route_input(skb, iph->daddr, iph->saddr, iph->tos,
  38. skb->dev))
  39. goto drop;
  40. }
  41. return dst_input(skb);
  42. drop:
  43. kfree_skb(skb);
  44. return NET_RX_DROP;
  45. }
  46. #endif
  47. int xfrm4_rcv_encap(struct sk_buff *skb, __u16 encap_type)
  48. {
  49. int err;
  50. __be32 spi, seq;
  51. struct xfrm_state *xfrm_vec[XFRM_MAX_DEPTH];
  52. struct xfrm_state *x;
  53. int xfrm_nr = 0;
  54. int decaps = 0;
  55. if ((err = xfrm4_parse_spi(skb, skb->nh.iph->protocol, &spi, &seq)) != 0)
  56. goto drop;
  57. do {
  58. struct iphdr *iph = skb->nh.iph;
  59. if (xfrm_nr == XFRM_MAX_DEPTH)
  60. goto drop;
  61. x = xfrm_state_lookup((xfrm_address_t *)&iph->daddr, spi, iph->protocol, AF_INET);
  62. if (x == NULL)
  63. goto drop;
  64. spin_lock(&x->lock);
  65. if (unlikely(x->km.state != XFRM_STATE_VALID))
  66. goto drop_unlock;
  67. if ((x->encap ? x->encap->encap_type : 0) != encap_type)
  68. goto drop_unlock;
  69. if (x->props.replay_window && xfrm_replay_check(x, seq))
  70. goto drop_unlock;
  71. if (xfrm_state_check_expire(x))
  72. goto drop_unlock;
  73. if (x->type->input(x, skb))
  74. goto drop_unlock;
  75. /* only the first xfrm gets the encap type */
  76. encap_type = 0;
  77. if (x->props.replay_window)
  78. xfrm_replay_advance(x, seq);
  79. x->curlft.bytes += skb->len;
  80. x->curlft.packets++;
  81. spin_unlock(&x->lock);
  82. xfrm_vec[xfrm_nr++] = x;
  83. if (x->mode->input(x, skb))
  84. goto drop;
  85. if (x->props.mode == XFRM_MODE_TUNNEL) {
  86. decaps = 1;
  87. break;
  88. }
  89. if ((err = xfrm_parse_spi(skb, skb->nh.iph->protocol, &spi, &seq)) < 0)
  90. goto drop;
  91. } while (!err);
  92. /* Allocate new secpath or COW existing one. */
  93. if (!skb->sp || atomic_read(&skb->sp->refcnt) != 1) {
  94. struct sec_path *sp;
  95. sp = secpath_dup(skb->sp);
  96. if (!sp)
  97. goto drop;
  98. if (skb->sp)
  99. secpath_put(skb->sp);
  100. skb->sp = sp;
  101. }
  102. if (xfrm_nr + skb->sp->len > XFRM_MAX_DEPTH)
  103. goto drop;
  104. memcpy(skb->sp->xvec + skb->sp->len, xfrm_vec,
  105. xfrm_nr * sizeof(xfrm_vec[0]));
  106. skb->sp->len += xfrm_nr;
  107. nf_reset(skb);
  108. if (decaps) {
  109. if (!(skb->dev->flags&IFF_LOOPBACK)) {
  110. dst_release(skb->dst);
  111. skb->dst = NULL;
  112. }
  113. netif_rx(skb);
  114. return 0;
  115. } else {
  116. #ifdef CONFIG_NETFILTER
  117. __skb_push(skb, skb->data - skb->nh.raw);
  118. skb->nh.iph->tot_len = htons(skb->len);
  119. ip_send_check(skb->nh.iph);
  120. NF_HOOK(PF_INET, NF_IP_PRE_ROUTING, skb, skb->dev, NULL,
  121. xfrm4_rcv_encap_finish);
  122. return 0;
  123. #else
  124. return -skb->nh.iph->protocol;
  125. #endif
  126. }
  127. drop_unlock:
  128. spin_unlock(&x->lock);
  129. xfrm_state_put(x);
  130. drop:
  131. while (--xfrm_nr >= 0)
  132. xfrm_state_put(xfrm_vec[xfrm_nr]);
  133. kfree_skb(skb);
  134. return 0;
  135. }