xfrm4_input.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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. #include <net/protocol.h>
  20. #include <net/gro.h>
  21. static int xfrm4_rcv_encap_finish2(struct net *net, struct sock *sk,
  22. struct sk_buff *skb)
  23. {
  24. return dst_input(skb);
  25. }
  26. static inline int xfrm4_rcv_encap_finish(struct net *net, struct sock *sk,
  27. struct sk_buff *skb)
  28. {
  29. if (!skb_dst(skb)) {
  30. const struct iphdr *iph = ip_hdr(skb);
  31. if (ip_route_input_noref(skb, iph->daddr, iph->saddr,
  32. iph->tos, skb->dev))
  33. goto drop;
  34. }
  35. if (xfrm_trans_queue(skb, xfrm4_rcv_encap_finish2))
  36. goto drop;
  37. return 0;
  38. drop:
  39. kfree_skb(skb);
  40. return NET_RX_DROP;
  41. }
  42. int xfrm4_transport_finish(struct sk_buff *skb, int async)
  43. {
  44. struct xfrm_offload *xo = xfrm_offload(skb);
  45. struct iphdr *iph = ip_hdr(skb);
  46. iph->protocol = XFRM_MODE_SKB_CB(skb)->protocol;
  47. #ifndef CONFIG_NETFILTER
  48. if (!async)
  49. return -iph->protocol;
  50. #endif
  51. __skb_push(skb, -skb_network_offset(skb));
  52. iph->tot_len = htons(skb->len);
  53. ip_send_check(iph);
  54. if (xo && (xo->flags & XFRM_GRO)) {
  55. /* The full l2 header needs to be preserved so that re-injecting the packet at l2
  56. * works correctly in the presence of vlan tags.
  57. */
  58. skb_mac_header_rebuild_full(skb, xo->orig_mac_len);
  59. skb_reset_network_header(skb);
  60. skb_reset_transport_header(skb);
  61. return 0;
  62. }
  63. NF_HOOK(NFPROTO_IPV4, NF_INET_PRE_ROUTING,
  64. dev_net(skb->dev), NULL, skb, skb->dev, NULL,
  65. xfrm4_rcv_encap_finish);
  66. return 0;
  67. }
  68. static int __xfrm4_udp_encap_rcv(struct sock *sk, struct sk_buff *skb, bool pull)
  69. {
  70. struct udp_sock *up = udp_sk(sk);
  71. struct udphdr *uh;
  72. struct iphdr *iph;
  73. int iphlen, len;
  74. __u8 *udpdata;
  75. __be32 *udpdata32;
  76. u16 encap_type;
  77. encap_type = READ_ONCE(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. return -EINVAL;
  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. }
  104. /* At this point we are sure that this is an ESPinUDP packet,
  105. * so we need to remove 'len' bytes from the packet (the UDP
  106. * header and optional ESP marker bytes) and then modify the
  107. * protocol to ESP, and then call into the transform receiver.
  108. */
  109. if (skb_unclone(skb, GFP_ATOMIC))
  110. return -EINVAL;
  111. /* Now we can update and verify the packet length... */
  112. iph = ip_hdr(skb);
  113. iphlen = iph->ihl << 2;
  114. iph->tot_len = htons(ntohs(iph->tot_len) - len);
  115. if (skb->len < iphlen + len) {
  116. /* packet is too small!?! */
  117. return -EINVAL;
  118. }
  119. /* pull the data buffer up to the ESP header and set the
  120. * transport header to point to ESP. Keep UDP on the stack
  121. * for later.
  122. */
  123. if (pull) {
  124. __skb_pull(skb, len);
  125. skb_reset_transport_header(skb);
  126. } else {
  127. skb_set_transport_header(skb, len);
  128. }
  129. /* process ESP */
  130. return 0;
  131. }
  132. /* If it's a keepalive packet, then just eat it.
  133. * If it's an encapsulated packet, then pass it to the
  134. * IPsec xfrm input.
  135. * Returns 0 if skb passed to xfrm or was dropped.
  136. * Returns >0 if skb should be passed to UDP.
  137. * Returns <0 if skb should be resubmitted (-ret is protocol)
  138. */
  139. int xfrm4_udp_encap_rcv(struct sock *sk, struct sk_buff *skb)
  140. {
  141. int ret;
  142. ret = __xfrm4_udp_encap_rcv(sk, skb, true);
  143. if (!ret)
  144. return xfrm4_rcv_encap(skb, IPPROTO_ESP, 0,
  145. udp_sk(sk)->encap_type);
  146. if (ret < 0) {
  147. kfree_skb(skb);
  148. return 0;
  149. }
  150. return ret;
  151. }
  152. EXPORT_SYMBOL(xfrm4_udp_encap_rcv);
  153. struct sk_buff *xfrm4_gro_udp_encap_rcv(struct sock *sk, struct list_head *head,
  154. struct sk_buff *skb)
  155. {
  156. int offset = skb_gro_offset(skb);
  157. const struct net_offload *ops;
  158. struct sk_buff *pp = NULL;
  159. int len, dlen;
  160. __u8 *udpdata;
  161. __be32 *udpdata32;
  162. len = skb->len - offset;
  163. dlen = offset + min(len, 8);
  164. udpdata = skb_gro_header(skb, dlen, offset);
  165. udpdata32 = (__be32 *)udpdata;
  166. if (unlikely(!udpdata))
  167. return NULL;
  168. rcu_read_lock();
  169. ops = rcu_dereference(inet_offloads[IPPROTO_ESP]);
  170. if (!ops || !ops->callbacks.gro_receive)
  171. goto out;
  172. /* check if it is a keepalive or IKE packet */
  173. if (len <= sizeof(struct ip_esp_hdr) || udpdata32[0] == 0)
  174. goto out;
  175. /* set the transport header to ESP */
  176. skb_set_transport_header(skb, offset);
  177. NAPI_GRO_CB(skb)->proto = IPPROTO_UDP;
  178. pp = call_gro_receive(ops->callbacks.gro_receive, head, skb);
  179. rcu_read_unlock();
  180. return pp;
  181. out:
  182. rcu_read_unlock();
  183. NAPI_GRO_CB(skb)->same_flow = 0;
  184. NAPI_GRO_CB(skb)->flush = 1;
  185. return NULL;
  186. }
  187. EXPORT_SYMBOL(xfrm4_gro_udp_encap_rcv);
  188. int xfrm4_rcv(struct sk_buff *skb)
  189. {
  190. return xfrm4_rcv_spi(skb, ip_hdr(skb)->protocol, 0);
  191. }
  192. EXPORT_SYMBOL(xfrm4_rcv);