xfrm4_input.c 4.3 KB

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