xfrm4_input.c 4.2 KB

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