xfrm4_input.c 5.4 KB

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