xfrm4_input.c 3.4 KB

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