xfrm4_input.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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/inet_ecn.h>
  16. #include <net/ip.h>
  17. #include <net/xfrm.h>
  18. int xfrm4_rcv(struct sk_buff *skb)
  19. {
  20. return xfrm4_rcv_encap(skb, 0);
  21. }
  22. EXPORT_SYMBOL(xfrm4_rcv);
  23. static inline void ipip_ecn_decapsulate(struct sk_buff *skb)
  24. {
  25. struct iphdr *outer_iph = skb->nh.iph;
  26. struct iphdr *inner_iph = skb->h.ipiph;
  27. if (INET_ECN_is_ce(outer_iph->tos))
  28. IP_ECN_set_ce(inner_iph);
  29. }
  30. static int xfrm4_parse_spi(struct sk_buff *skb, u8 nexthdr, u32 *spi, u32 *seq)
  31. {
  32. switch (nexthdr) {
  33. case IPPROTO_IPIP:
  34. *spi = skb->nh.iph->saddr;
  35. *seq = 0;
  36. return 0;
  37. }
  38. return xfrm_parse_spi(skb, nexthdr, spi, seq);
  39. }
  40. #ifdef CONFIG_NETFILTER
  41. static inline int xfrm4_rcv_encap_finish(struct sk_buff *skb)
  42. {
  43. struct iphdr *iph = skb->nh.iph;
  44. if (skb->dst == NULL) {
  45. if (ip_route_input(skb, iph->daddr, iph->saddr, iph->tos,
  46. skb->dev))
  47. goto drop;
  48. }
  49. return dst_input(skb);
  50. drop:
  51. kfree_skb(skb);
  52. return NET_RX_DROP;
  53. }
  54. #endif
  55. int xfrm4_rcv_encap(struct sk_buff *skb, __u16 encap_type)
  56. {
  57. int err;
  58. u32 spi, seq;
  59. struct xfrm_state *xfrm_vec[XFRM_MAX_DEPTH];
  60. struct xfrm_state *x;
  61. int xfrm_nr = 0;
  62. int decaps = 0;
  63. if ((err = xfrm4_parse_spi(skb, skb->nh.iph->protocol, &spi, &seq)) != 0)
  64. goto drop;
  65. do {
  66. struct iphdr *iph = skb->nh.iph;
  67. if (xfrm_nr == XFRM_MAX_DEPTH)
  68. goto drop;
  69. x = xfrm_state_lookup((xfrm_address_t *)&iph->daddr, spi, iph->protocol, AF_INET);
  70. if (x == NULL)
  71. goto drop;
  72. spin_lock(&x->lock);
  73. if (unlikely(x->km.state != XFRM_STATE_VALID))
  74. goto drop_unlock;
  75. if ((x->encap ? x->encap->encap_type : 0) != encap_type)
  76. goto drop_unlock;
  77. if (x->props.replay_window && xfrm_replay_check(x, seq))
  78. goto drop_unlock;
  79. if (xfrm_state_check_expire(x))
  80. goto drop_unlock;
  81. if (x->type->input(x, skb))
  82. goto drop_unlock;
  83. /* only the first xfrm gets the encap type */
  84. encap_type = 0;
  85. if (x->props.replay_window)
  86. xfrm_replay_advance(x, seq);
  87. x->curlft.bytes += skb->len;
  88. x->curlft.packets++;
  89. spin_unlock(&x->lock);
  90. xfrm_vec[xfrm_nr++] = x;
  91. iph = skb->nh.iph;
  92. if (x->props.mode) {
  93. if (iph->protocol != IPPROTO_IPIP)
  94. goto drop;
  95. if (!pskb_may_pull(skb, sizeof(struct iphdr)))
  96. goto drop;
  97. if (skb_cloned(skb) &&
  98. pskb_expand_head(skb, 0, 0, GFP_ATOMIC))
  99. goto drop;
  100. if (x->props.flags & XFRM_STATE_DECAP_DSCP)
  101. ipv4_copy_dscp(iph, skb->h.ipiph);
  102. if (!(x->props.flags & XFRM_STATE_NOECN))
  103. ipip_ecn_decapsulate(skb);
  104. skb->mac.raw = memmove(skb->data - skb->mac_len,
  105. skb->mac.raw, skb->mac_len);
  106. skb->nh.raw = skb->data;
  107. memset(&(IPCB(skb)->opt), 0, sizeof(struct ip_options));
  108. decaps = 1;
  109. break;
  110. }
  111. if ((err = xfrm_parse_spi(skb, skb->nh.iph->protocol, &spi, &seq)) < 0)
  112. goto drop;
  113. } while (!err);
  114. /* Allocate new secpath or COW existing one. */
  115. if (!skb->sp || atomic_read(&skb->sp->refcnt) != 1) {
  116. struct sec_path *sp;
  117. sp = secpath_dup(skb->sp);
  118. if (!sp)
  119. goto drop;
  120. if (skb->sp)
  121. secpath_put(skb->sp);
  122. skb->sp = sp;
  123. }
  124. if (xfrm_nr + skb->sp->len > XFRM_MAX_DEPTH)
  125. goto drop;
  126. memcpy(skb->sp->xvec + skb->sp->len, xfrm_vec,
  127. xfrm_nr * sizeof(xfrm_vec[0]));
  128. skb->sp->len += xfrm_nr;
  129. nf_reset(skb);
  130. if (decaps) {
  131. if (!(skb->dev->flags&IFF_LOOPBACK)) {
  132. dst_release(skb->dst);
  133. skb->dst = NULL;
  134. }
  135. netif_rx(skb);
  136. return 0;
  137. } else {
  138. #ifdef CONFIG_NETFILTER
  139. __skb_push(skb, skb->data - skb->nh.raw);
  140. skb->nh.iph->tot_len = htons(skb->len);
  141. ip_send_check(skb->nh.iph);
  142. NF_HOOK(PF_INET, NF_IP_PRE_ROUTING, skb, skb->dev, NULL,
  143. xfrm4_rcv_encap_finish);
  144. return 0;
  145. #else
  146. return -skb->nh.iph->protocol;
  147. #endif
  148. }
  149. drop_unlock:
  150. spin_unlock(&x->lock);
  151. xfrm_state_put(x);
  152. drop:
  153. while (--xfrm_nr >= 0)
  154. xfrm_state_put(xfrm_vec[xfrm_nr]);
  155. kfree_skb(skb);
  156. return 0;
  157. }