xfrm4_input.c 3.9 KB

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