xfrm4_input.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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/slab.h>
  12. #include <linux/module.h>
  13. #include <linux/string.h>
  14. #include <linux/netfilter.h>
  15. #include <linux/netfilter_ipv4.h>
  16. #include <net/ip.h>
  17. #include <net/xfrm.h>
  18. int xfrm4_extract_input(struct xfrm_state *x, struct sk_buff *skb)
  19. {
  20. return xfrm4_extract_header(skb);
  21. }
  22. static inline int xfrm4_rcv_encap_finish(struct sk_buff *skb)
  23. {
  24. if (skb_dst(skb) == NULL) {
  25. const struct iphdr *iph = ip_hdr(skb);
  26. if (ip_route_input_noref(skb, iph->daddr, iph->saddr,
  27. iph->tos, skb->dev))
  28. goto drop;
  29. }
  30. return dst_input(skb);
  31. drop:
  32. kfree_skb(skb);
  33. return NET_RX_DROP;
  34. }
  35. int xfrm4_rcv_encap(struct sk_buff *skb, int nexthdr, __be32 spi,
  36. int encap_type)
  37. {
  38. XFRM_SPI_SKB_CB(skb)->family = AF_INET;
  39. XFRM_SPI_SKB_CB(skb)->daddroff = offsetof(struct iphdr, daddr);
  40. return xfrm_input(skb, nexthdr, spi, encap_type);
  41. }
  42. EXPORT_SYMBOL(xfrm4_rcv_encap);
  43. int xfrm4_transport_finish(struct sk_buff *skb, int async)
  44. {
  45. struct iphdr *iph = ip_hdr(skb);
  46. iph->protocol = XFRM_MODE_SKB_CB(skb)->protocol;
  47. #ifndef CONFIG_NETFILTER
  48. if (!async)
  49. return -iph->protocol;
  50. #endif
  51. __skb_push(skb, skb->data - skb_network_header(skb));
  52. iph->tot_len = htons(skb->len);
  53. ip_send_check(iph);
  54. NF_HOOK(NFPROTO_IPV4, NF_INET_PRE_ROUTING, skb, skb->dev, NULL,
  55. xfrm4_rcv_encap_finish);
  56. return 0;
  57. }
  58. /* If it's a keepalive packet, then just eat it.
  59. * If it's an encapsulated packet, then pass it to the
  60. * IPsec xfrm input.
  61. * Returns 0 if skb passed to xfrm or was dropped.
  62. * Returns >0 if skb should be passed to UDP.
  63. * Returns <0 if skb should be resubmitted (-ret is protocol)
  64. */
  65. int xfrm4_udp_encap_rcv(struct sock *sk, struct sk_buff *skb)
  66. {
  67. struct udp_sock *up = udp_sk(sk);
  68. struct udphdr *uh;
  69. struct iphdr *iph;
  70. int iphlen, len;
  71. __u8 *udpdata;
  72. __be32 *udpdata32;
  73. __u16 encap_type = up->encap_type;
  74. /* if this is not encapsulated socket, then just return now */
  75. if (!encap_type)
  76. return 1;
  77. /* If this is a paged skb, make sure we pull up
  78. * whatever data we need to look at. */
  79. len = skb->len - sizeof(struct udphdr);
  80. if (!pskb_may_pull(skb, sizeof(struct udphdr) + min(len, 8)))
  81. return 1;
  82. /* Now we can get the pointers */
  83. uh = udp_hdr(skb);
  84. udpdata = (__u8 *)uh + sizeof(struct udphdr);
  85. udpdata32 = (__be32 *)udpdata;
  86. switch (encap_type) {
  87. default:
  88. case UDP_ENCAP_ESPINUDP:
  89. /* Check if this is a keepalive packet. If so, eat it. */
  90. if (len == 1 && udpdata[0] == 0xff) {
  91. goto drop;
  92. } else if (len > sizeof(struct ip_esp_hdr) && udpdata32[0] != 0) {
  93. /* ESP Packet without Non-ESP header */
  94. len = sizeof(struct udphdr);
  95. } else
  96. /* Must be an IKE packet.. pass it through */
  97. return 1;
  98. break;
  99. case UDP_ENCAP_ESPINUDP_NON_IKE:
  100. /* Check if this is a keepalive packet. If so, eat it. */
  101. if (len == 1 && udpdata[0] == 0xff) {
  102. goto drop;
  103. } else if (len > 2 * sizeof(u32) + sizeof(struct ip_esp_hdr) &&
  104. udpdata32[0] == 0 && udpdata32[1] == 0) {
  105. /* ESP Packet with Non-IKE marker */
  106. len = sizeof(struct udphdr) + 2 * sizeof(u32);
  107. } else
  108. /* Must be an IKE packet.. pass it through */
  109. return 1;
  110. break;
  111. }
  112. /* At this point we are sure that this is an ESPinUDP packet,
  113. * so we need to remove 'len' bytes from the packet (the UDP
  114. * header and optional ESP marker bytes) and then modify the
  115. * protocol to ESP, and then call into the transform receiver.
  116. */
  117. if (skb_cloned(skb) && pskb_expand_head(skb, 0, 0, GFP_ATOMIC))
  118. goto drop;
  119. /* Now we can update and verify the packet length... */
  120. iph = ip_hdr(skb);
  121. iphlen = iph->ihl << 2;
  122. iph->tot_len = htons(ntohs(iph->tot_len) - len);
  123. if (skb->len < iphlen + len) {
  124. /* packet is too small!?! */
  125. goto drop;
  126. }
  127. /* pull the data buffer up to the ESP header and set the
  128. * transport header to point to ESP. Keep UDP on the stack
  129. * for later.
  130. */
  131. __skb_pull(skb, len);
  132. skb_reset_transport_header(skb);
  133. /* process ESP */
  134. return xfrm4_rcv_encap(skb, IPPROTO_ESP, 0, encap_type);
  135. drop:
  136. kfree_skb(skb);
  137. return 0;
  138. }
  139. int xfrm4_rcv(struct sk_buff *skb)
  140. {
  141. return xfrm4_rcv_spi(skb, ip_hdr(skb)->protocol, 0);
  142. }
  143. EXPORT_SYMBOL(xfrm4_rcv);