xfrm4_input.c 4.1 KB

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