xfrm4_input.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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. static int xfrm4_parse_spi(struct sk_buff *skb, u8 nexthdr, __be32 *spi, __be32 *seq)
  18. {
  19. switch (nexthdr) {
  20. case IPPROTO_IPIP:
  21. case IPPROTO_IPV6:
  22. *spi = ip_hdr(skb)->saddr;
  23. *seq = 0;
  24. return 0;
  25. }
  26. return xfrm_parse_spi(skb, nexthdr, spi, seq);
  27. }
  28. #ifdef CONFIG_NETFILTER
  29. static inline int xfrm4_rcv_encap_finish(struct sk_buff *skb)
  30. {
  31. if (skb->dst == NULL) {
  32. const struct iphdr *iph = ip_hdr(skb);
  33. if (ip_route_input(skb, iph->daddr, iph->saddr, iph->tos,
  34. skb->dev))
  35. goto drop;
  36. }
  37. return dst_input(skb);
  38. drop:
  39. kfree_skb(skb);
  40. return NET_RX_DROP;
  41. }
  42. #endif
  43. static int xfrm4_rcv_encap(struct sk_buff *skb, __u16 encap_type)
  44. {
  45. __be32 spi, seq;
  46. struct xfrm_state *xfrm_vec[XFRM_MAX_DEPTH];
  47. struct xfrm_state *x;
  48. int xfrm_nr = 0;
  49. int decaps = 0;
  50. int err = xfrm4_parse_spi(skb, ip_hdr(skb)->protocol, &spi, &seq);
  51. if (err != 0)
  52. goto drop;
  53. do {
  54. const struct iphdr *iph = ip_hdr(skb);
  55. if (xfrm_nr == XFRM_MAX_DEPTH)
  56. goto drop;
  57. x = xfrm_state_lookup((xfrm_address_t *)&iph->daddr, spi,
  58. iph->protocol != IPPROTO_IPV6 ? iph->protocol : IPPROTO_IPIP, AF_INET);
  59. if (x == NULL)
  60. goto drop;
  61. spin_lock(&x->lock);
  62. if (unlikely(x->km.state != XFRM_STATE_VALID))
  63. goto drop_unlock;
  64. if ((x->encap ? x->encap->encap_type : 0) != encap_type)
  65. goto drop_unlock;
  66. if (x->props.replay_window && xfrm_replay_check(x, seq))
  67. goto drop_unlock;
  68. if (xfrm_state_check_expire(x))
  69. goto drop_unlock;
  70. if (x->type->input(x, skb))
  71. goto drop_unlock;
  72. /* only the first xfrm gets the encap type */
  73. encap_type = 0;
  74. if (x->props.replay_window)
  75. xfrm_replay_advance(x, seq);
  76. x->curlft.bytes += skb->len;
  77. x->curlft.packets++;
  78. spin_unlock(&x->lock);
  79. xfrm_vec[xfrm_nr++] = x;
  80. if (x->mode->input(x, skb))
  81. goto drop;
  82. if (x->props.mode == XFRM_MODE_TUNNEL) {
  83. decaps = 1;
  84. break;
  85. }
  86. err = xfrm_parse_spi(skb, ip_hdr(skb)->protocol, &spi, &seq);
  87. if (err < 0)
  88. goto drop;
  89. } while (!err);
  90. /* Allocate new secpath or COW existing one. */
  91. if (!skb->sp || atomic_read(&skb->sp->refcnt) != 1) {
  92. struct sec_path *sp;
  93. sp = secpath_dup(skb->sp);
  94. if (!sp)
  95. goto drop;
  96. if (skb->sp)
  97. secpath_put(skb->sp);
  98. skb->sp = sp;
  99. }
  100. if (xfrm_nr + skb->sp->len > XFRM_MAX_DEPTH)
  101. goto drop;
  102. memcpy(skb->sp->xvec + skb->sp->len, xfrm_vec,
  103. xfrm_nr * sizeof(xfrm_vec[0]));
  104. skb->sp->len += xfrm_nr;
  105. nf_reset(skb);
  106. if (decaps) {
  107. dst_release(skb->dst);
  108. skb->dst = NULL;
  109. netif_rx(skb);
  110. return 0;
  111. } else {
  112. #ifdef CONFIG_NETFILTER
  113. __skb_push(skb, skb->data - skb_network_header(skb));
  114. ip_hdr(skb)->tot_len = htons(skb->len);
  115. ip_send_check(ip_hdr(skb));
  116. NF_HOOK(PF_INET, NF_IP_PRE_ROUTING, skb, skb->dev, NULL,
  117. xfrm4_rcv_encap_finish);
  118. return 0;
  119. #else
  120. return -ip_hdr(skb)->protocol;
  121. #endif
  122. }
  123. drop_unlock:
  124. spin_unlock(&x->lock);
  125. xfrm_state_put(x);
  126. drop:
  127. while (--xfrm_nr >= 0)
  128. xfrm_state_put(xfrm_vec[xfrm_nr]);
  129. kfree_skb(skb);
  130. return 0;
  131. }
  132. /* If it's a keepalive packet, then just eat it.
  133. * If it's an encapsulated packet, then pass it to the
  134. * IPsec xfrm input.
  135. * Returns 0 if skb passed to xfrm or was dropped.
  136. * Returns >0 if skb should be passed to UDP.
  137. * Returns <0 if skb should be resubmitted (-ret is protocol)
  138. */
  139. int xfrm4_udp_encap_rcv(struct sock *sk, struct sk_buff *skb)
  140. {
  141. struct udp_sock *up = udp_sk(sk);
  142. struct udphdr *uh;
  143. struct iphdr *iph;
  144. int iphlen, len;
  145. int ret;
  146. __u8 *udpdata;
  147. __be32 *udpdata32;
  148. __u16 encap_type = up->encap_type;
  149. /* if this is not encapsulated socket, then just return now */
  150. if (!encap_type)
  151. return 1;
  152. /* If this is a paged skb, make sure we pull up
  153. * whatever data we need to look at. */
  154. len = skb->len - sizeof(struct udphdr);
  155. if (!pskb_may_pull(skb, sizeof(struct udphdr) + min(len, 8)))
  156. return 1;
  157. /* Now we can get the pointers */
  158. uh = udp_hdr(skb);
  159. udpdata = (__u8 *)uh + sizeof(struct udphdr);
  160. udpdata32 = (__be32 *)udpdata;
  161. switch (encap_type) {
  162. default:
  163. case UDP_ENCAP_ESPINUDP:
  164. /* Check if this is a keepalive packet. If so, eat it. */
  165. if (len == 1 && udpdata[0] == 0xff) {
  166. goto drop;
  167. } else if (len > sizeof(struct ip_esp_hdr) && udpdata32[0] != 0) {
  168. /* ESP Packet without Non-ESP header */
  169. len = sizeof(struct udphdr);
  170. } else
  171. /* Must be an IKE packet.. pass it through */
  172. return 1;
  173. break;
  174. case UDP_ENCAP_ESPINUDP_NON_IKE:
  175. /* Check if this is a keepalive packet. If so, eat it. */
  176. if (len == 1 && udpdata[0] == 0xff) {
  177. goto drop;
  178. } else if (len > 2 * sizeof(u32) + sizeof(struct ip_esp_hdr) &&
  179. udpdata32[0] == 0 && udpdata32[1] == 0) {
  180. /* ESP Packet with Non-IKE marker */
  181. len = sizeof(struct udphdr) + 2 * sizeof(u32);
  182. } else
  183. /* Must be an IKE packet.. pass it through */
  184. return 1;
  185. break;
  186. }
  187. /* At this point we are sure that this is an ESPinUDP packet,
  188. * so we need to remove 'len' bytes from the packet (the UDP
  189. * header and optional ESP marker bytes) and then modify the
  190. * protocol to ESP, and then call into the transform receiver.
  191. */
  192. if (skb_cloned(skb) && pskb_expand_head(skb, 0, 0, GFP_ATOMIC))
  193. goto drop;
  194. /* Now we can update and verify the packet length... */
  195. iph = ip_hdr(skb);
  196. iphlen = iph->ihl << 2;
  197. iph->tot_len = htons(ntohs(iph->tot_len) - len);
  198. if (skb->len < iphlen + len) {
  199. /* packet is too small!?! */
  200. goto drop;
  201. }
  202. /* pull the data buffer up to the ESP header and set the
  203. * transport header to point to ESP. Keep UDP on the stack
  204. * for later.
  205. */
  206. __skb_pull(skb, len);
  207. skb_reset_transport_header(skb);
  208. /* modify the protocol (it's ESP!) */
  209. iph->protocol = IPPROTO_ESP;
  210. /* process ESP */
  211. ret = xfrm4_rcv_encap(skb, encap_type);
  212. return ret;
  213. drop:
  214. kfree_skb(skb);
  215. return 0;
  216. }
  217. int xfrm4_rcv(struct sk_buff *skb)
  218. {
  219. return xfrm4_rcv_encap(skb, 0);
  220. }
  221. EXPORT_SYMBOL(xfrm4_rcv);