A packet that reaches gre_rcv() and does not belong to any tunnel is dropped, after an ICMP port unreachable is sent back, as SKB_DROP_REASON_NOT_SPECIFIED from the same kfree_skb_reason() call as a truncated ERSPAN header or a failed metadata allocation. This is the GRE counterpart of a UDP packet hitting no socket, yet neither the reason nor the call site tells it apart. The fallback devices such as gre0 only take such a packet while they are up, and they are created down. Report SKB_DROP_REASON_TUNNEL_NOT_FOUND for it, which vxlan reports when no device matches the VNI, and add GRE to its description. erspan_rcv(), __ipgre_rcv() and ipgre_rcv() now return SKB_NOT_DROPPED_YET where they returned PACKET_RCVD, including the paths that free the packet themselves, and a drop reason where they returned PACKET_REJECT or PACKET_NEXT. __ipgre_rcv() only returned PACKET_NEXT when no tunnel matched, so the retry ipgre_rcv() does for ETH_P_TEB is now taken on SKB_DROP_REASON_TUNNEL_NOT_FOUND. The length checks use pskb_may_pull_reason(), and the headers are pulled with __iptunnel_pull_header(), which returns a drop reason since an earlier patch of this series. Both report a packet too short to pull as SKB_DROP_REASON_PKT_TOO_SMALL and an allocation failure as SKB_DROP_REASON_NOMEM. The metadata allocation failures reuse SKB_DROP_REASON_NOMEM as well. erspan_rcv() and __ipgre_rcv() follow the same rule for the reason as gre_rcv(): it is initialized to SKB_DROP_REASON_NOT_SPECIFIED, and their drop labels fall back to it, as the pull helpers store their result in the reason. Assisted-by: LLM Signed-off-by: Anton Danilov --- include/net/dropreason-core.h | 3 +- net/ipv4/ip_gre.c | 81 ++++++++++++++++++++--------------- 2 files changed, 49 insertions(+), 35 deletions(-) diff --git a/include/net/dropreason-core.h b/include/net/dropreason-core.h index 0d963f18a21e..6f5273e16548 100644 --- a/include/net/dropreason-core.h +++ b/include/net/dropreason-core.h @@ -550,7 +550,8 @@ enum skb_drop_reason { SKB_DROP_REASON_TUNNEL_INVALID_HDR, /** * @SKB_DROP_REASON_TUNNEL_NOT_FOUND: no tunnel device found for the - * packet, e.g. no VXLAN device for its VNI + * packet, e.g. no VXLAN device for its VNI or no GRE tunnel for its + * endpoints and key */ SKB_DROP_REASON_TUNNEL_NOT_FOUND, /** @SKB_DROP_REASON_MAC_INVALID_SOURCE: source mac is invalid */ diff --git a/net/ipv4/ip_gre.c b/net/ipv4/ip_gre.c index a1b44007b1d7..afd8ece02d7f 100644 --- a/net/ipv4/ip_gre.c +++ b/net/ipv4/ip_gre.c @@ -264,9 +264,11 @@ static bool is_erspan_type1(int gre_hdr_len) return gre_hdr_len == 4; } -static int erspan_rcv(struct sk_buff *skb, struct tnl_ptk_info *tpi, - int gre_hdr_len) +static enum skb_drop_reason erspan_rcv(struct sk_buff *skb, + struct tnl_ptk_info *tpi, + int gre_hdr_len) { + enum skb_drop_reason reason = SKB_DROP_REASON_NOT_SPECIFIED; struct net *net = dev_net(skb->dev); struct metadata_dst *tun_dst = NULL; struct erspan_base_hdr *ershdr; @@ -288,9 +290,10 @@ static int erspan_rcv(struct sk_buff *skb, struct tnl_ptk_info *tpi, tunnel = ip_tunnel_lookup(itn, skb->dev->ifindex, flags, iph->saddr, iph->daddr, 0); } else { - if (unlikely(!pskb_may_pull(skb, - gre_hdr_len + sizeof(*ershdr)))) - return PACKET_REJECT; + reason = pskb_may_pull_reason(skb, + gre_hdr_len + sizeof(*ershdr)); + if (unlikely(reason)) + return reason; ershdr = (struct erspan_base_hdr *)(skb->data + gre_hdr_len); ver = ershdr->ver; @@ -306,13 +309,13 @@ static int erspan_rcv(struct sk_buff *skb, struct tnl_ptk_info *tpi, else len = gre_hdr_len + erspan_hdr_len(ver); - if (unlikely(!pskb_may_pull(skb, len))) - return PACKET_REJECT; + reason = pskb_may_pull_reason(skb, len); + if (unlikely(reason)) + return reason; - if (__iptunnel_pull_header(skb, - len, - htons(ETH_P_TEB), - false, false)) + reason = __iptunnel_pull_header(skb, len, htons(ETH_P_TEB), + false, false); + if (reason) goto drop; if (tunnel->collect_md) { @@ -328,7 +331,7 @@ static int erspan_rcv(struct sk_buff *skb, struct tnl_ptk_info *tpi, tun_dst = ip_tun_rx_dst(skb, flags, tun_id, sizeof(*md)); if (!tun_dst) - return PACKET_REJECT; + return SKB_DROP_REASON_NOMEM; /* MUST set options_len before referencing options */ info = &tun_dst->u.tun_info; @@ -354,18 +357,22 @@ static int erspan_rcv(struct sk_buff *skb, struct tnl_ptk_info *tpi, skb_reset_mac_header(skb); ip_tunnel_rcv(tunnel, skb, tpi, tun_dst, log_ecn_error); - return PACKET_RCVD; + return SKB_NOT_DROPPED_YET; } - return PACKET_REJECT; + return SKB_DROP_REASON_TUNNEL_NOT_FOUND; drop: - kfree_skb(skb); - return PACKET_RCVD; + reason = reason ?: SKB_DROP_REASON_NOT_SPECIFIED; + kfree_skb_reason(skb, reason); + return SKB_NOT_DROPPED_YET; } -static int __ipgre_rcv(struct sk_buff *skb, const struct tnl_ptk_info *tpi, - struct ip_tunnel_net *itn, int hdr_len, bool raw_proto) +static enum skb_drop_reason __ipgre_rcv(struct sk_buff *skb, + const struct tnl_ptk_info *tpi, + struct ip_tunnel_net *itn, + int hdr_len, bool raw_proto) { + enum skb_drop_reason reason = SKB_DROP_REASON_NOT_SPECIFIED; struct metadata_dst *tun_dst = NULL; const struct iphdr *iph; struct ip_tunnel *tunnel; @@ -377,8 +384,9 @@ static int __ipgre_rcv(struct sk_buff *skb, const struct tnl_ptk_info *tpi, if (tunnel) { const struct iphdr *tnl_params; - if (__iptunnel_pull_header(skb, hdr_len, tpi->proto, - raw_proto, false)) + reason = __iptunnel_pull_header(skb, hdr_len, tpi->proto, + raw_proto, false); + if (reason) goto drop; /* Special case for ipgre_header_parse(), which expects the @@ -401,40 +409,43 @@ static int __ipgre_rcv(struct sk_buff *skb, const struct tnl_ptk_info *tpi, tun_id = key32_to_tunnel_id(tpi->key); tun_dst = ip_tun_rx_dst(skb, flags, tun_id, 0); if (!tun_dst) - return PACKET_REJECT; + return SKB_DROP_REASON_NOMEM; } ip_tunnel_rcv(tunnel, skb, tpi, tun_dst, log_ecn_error); - return PACKET_RCVD; + return SKB_NOT_DROPPED_YET; } - return PACKET_NEXT; + return SKB_DROP_REASON_TUNNEL_NOT_FOUND; drop: - kfree_skb(skb); - return PACKET_RCVD; + reason = reason ?: SKB_DROP_REASON_NOT_SPECIFIED; + kfree_skb_reason(skb, reason); + return SKB_NOT_DROPPED_YET; } -static int ipgre_rcv(struct sk_buff *skb, const struct tnl_ptk_info *tpi, - int hdr_len) +static enum skb_drop_reason ipgre_rcv(struct sk_buff *skb, + const struct tnl_ptk_info *tpi, + int hdr_len) { struct net *net = dev_net(skb->dev); + enum skb_drop_reason reason; struct ip_tunnel_net *itn; - int res; if (tpi->proto == htons(ETH_P_TEB)) itn = net_generic(net, gre_tap_net_id); else itn = net_generic(net, ipgre_net_id); - res = __ipgre_rcv(skb, tpi, itn, hdr_len, false); - if (res == PACKET_NEXT && tpi->proto == htons(ETH_P_TEB)) { + reason = __ipgre_rcv(skb, tpi, itn, hdr_len, false); + if (reason == SKB_DROP_REASON_TUNNEL_NOT_FOUND && + tpi->proto == htons(ETH_P_TEB)) { /* ipgre tunnels in collect metadata mode should receive * also ETH_P_TEB traffic. */ itn = net_generic(net, ipgre_net_id); - res = __ipgre_rcv(skb, tpi, itn, hdr_len, true); + reason = __ipgre_rcv(skb, tpi, itn, hdr_len, true); } - return res; + return reason; } static int gre_rcv(struct sk_buff *skb) @@ -456,12 +467,14 @@ static int gre_rcv(struct sk_buff *skb) if (unlikely(tpi.proto == htons(ETH_P_ERSPAN) || tpi.proto == htons(ETH_P_ERSPAN2))) { - if (erspan_rcv(skb, &tpi, tpi.hdr_len) == PACKET_RCVD) + reason = erspan_rcv(skb, &tpi, tpi.hdr_len); + if (!reason) return 0; goto out; } - if (ipgre_rcv(skb, &tpi, tpi.hdr_len) == PACKET_RCVD) + reason = ipgre_rcv(skb, &tpi, tpi.hdr_len); + if (!reason) return 0; out: -- 2.47.3