xfrm4_input.c 4.1 KB

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