xfrm4_input.c 6.1 KB

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