xfrm4_input.c 4.3 KB

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