xfrm4_input.c 4.2 KB

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