xfrm4_input.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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 sec_decap_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->props.replay_window && xfrm_replay_check(x, seq))
  78. goto drop_unlock;
  79. if (xfrm_state_check_expire(x))
  80. goto drop_unlock;
  81. xfrm_vec[xfrm_nr].decap.decap_type = encap_type;
  82. if (x->type->input(x, &(xfrm_vec[xfrm_nr].decap), skb))
  83. goto drop_unlock;
  84. /* only the first xfrm gets the encap type */
  85. encap_type = 0;
  86. if (x->props.replay_window)
  87. xfrm_replay_advance(x, seq);
  88. x->curlft.bytes += skb->len;
  89. x->curlft.packets++;
  90. spin_unlock(&x->lock);
  91. xfrm_vec[xfrm_nr++].xvec = x;
  92. iph = skb->nh.iph;
  93. if (x->props.mode) {
  94. if (iph->protocol != IPPROTO_IPIP)
  95. goto drop;
  96. if (!pskb_may_pull(skb, sizeof(struct iphdr)))
  97. goto drop;
  98. if (skb_cloned(skb) &&
  99. pskb_expand_head(skb, 0, 0, GFP_ATOMIC))
  100. goto drop;
  101. if (x->props.flags & XFRM_STATE_DECAP_DSCP)
  102. ipv4_copy_dscp(iph, skb->h.ipiph);
  103. if (!(x->props.flags & XFRM_STATE_NOECN))
  104. ipip_ecn_decapsulate(skb);
  105. skb->mac.raw = memmove(skb->data - skb->mac_len,
  106. skb->mac.raw, skb->mac_len);
  107. skb->nh.raw = skb->data;
  108. memset(&(IPCB(skb)->opt), 0, sizeof(struct ip_options));
  109. decaps = 1;
  110. break;
  111. }
  112. if ((err = xfrm_parse_spi(skb, skb->nh.iph->protocol, &spi, &seq)) < 0)
  113. goto drop;
  114. } while (!err);
  115. /* Allocate new secpath or COW existing one. */
  116. if (!skb->sp || atomic_read(&skb->sp->refcnt) != 1) {
  117. struct sec_path *sp;
  118. sp = secpath_dup(skb->sp);
  119. if (!sp)
  120. goto drop;
  121. if (skb->sp)
  122. secpath_put(skb->sp);
  123. skb->sp = sp;
  124. }
  125. if (xfrm_nr + skb->sp->len > XFRM_MAX_DEPTH)
  126. goto drop;
  127. memcpy(skb->sp->x+skb->sp->len, xfrm_vec, xfrm_nr*sizeof(struct sec_decap_state));
  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].xvec);
  155. kfree_skb(skb);
  156. return 0;
  157. }