From nobody Mon Nov 25 07:01:00 2024 Received: from mail-pf1-f196.google.com (mail-pf1-f196.google.com [209.85.210.196]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 477A222315; Wed, 30 Oct 2024 01:43:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.196 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730252602; cv=none; b=VpsruBEjTGWYRwttmMOl7bYIgb24fFj0QhJFvtahpfWwpHrGPg1mtX0ge8GRfBZqEjEPxk+LaBwb2uDqsG/K3ynYV8WtpeCcV7/CJqJ/88Y6t4zrfjHNuzN7oHhrF3TOSx0BIohWrttRJcTSPz6wDmXsK/tDYLYcIa+cH9ID7fI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730252602; c=relaxed/simple; bh=LAqzk3ZeLSZRqj1ByixV8/2cQzVAafDYcEAPajrW45Q=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=KX8JyJCBGmhS0mL7OeaCxCnLx07xTChMjckNuiBfvW1MdxweFJNrlqMEn2K5I3/muCljq6wZVwhCpAEfNKlBZUT30H3o+SS+Ki1sW14BvcQ3USC0rhZZaX+iQ2nO43bCPDh665w86RD6d0VoPqVs7Cu1euzCRWRwhPMznSqwApA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=RN8jIu06; arc=none smtp.client-ip=209.85.210.196 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="RN8jIu06" Received: by mail-pf1-f196.google.com with SMTP id d2e1a72fcca58-71e4c2e36daso288757b3a.0; Tue, 29 Oct 2024 18:43:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1730252598; x=1730857398; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=O9fMwGGcbp4z4vwxeeXuhogVmcPVJ1v4SFojuZQbris=; b=RN8jIu06OCTDKkn05tLSwaX+XbLC86gkpK9CvT1o/EtVz1/VTfTemgJnC45t8LRe/d +SRRNleOo92kwKJKTLabFuU0W5qJRpFP1jMYa5riodfUklERykgQQm9STDvApDhpUxx2 RVBYvpAj+GCQqOzLSYyCJ9UbTTqT4fnOPhj6Mw4BO4dQdhv8ukikTxjzEC39AW5NR5AG zrkWUna/rIu3jhdkv4fghc5PdiIgso2281yR07vDUZkDvnmcHoXVMlndLm3FNPokHbSw Ekv38Rggp/0a/WFWj1qN1v6ny+G7L6kwWjlaRnOPg80FARUQaVeHz3jkdN8RDKYLY2AS cVQA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1730252598; x=1730857398; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=O9fMwGGcbp4z4vwxeeXuhogVmcPVJ1v4SFojuZQbris=; b=uusp+scwXapgzCqvXi7ltJfUZXLdNRs9/MP/RVPFaFuOBepL7V53WFcHrLjy7uIN/X IrsBeYjJqX202PczNes8g+FR8iyWM6Ene7pd3MTf7bhX0/u6TjOZ70dRDNcSy9Feib2m 2COwLwjJBkFFnwbRUe5lmnd/yvCAGVaYDZCKUfKflUkyfIjZGgw8VgHvzE8AQfD/PYLO AcZOGObEn4lKv4Xz8ypbbbvEJ051as85F+TIqjZIErDawYXwjATryuWmiTmY4FuW3ulW VKhbKCoMv7NbItMBNieEaDyX8qDp6cuWK9deurg+CSFeMj1U+vWmZth2k0ANO+yoKRTu 8A+w== X-Forwarded-Encrypted: i=1; AJvYcCVRw/2QeCAu7yeTtgvn+jNAa4LJEWhU/pn/ucACTsI1vNWvtRxFw05rQFprkU+ITZBJlkH1gAQK@vger.kernel.org, AJvYcCWjxt06cjzzGvQ0TXHH6OMttMg29j1N0fOIMR5KBV3G3u3S2dzRq226vRyQH81FxDjs224=@vger.kernel.org, AJvYcCWxKz4y31hLhy2m8JXd+pZfzYsZYtmPo361PHghqQ8QYHNqJFeZpIeSH9t4xToyD36oaOXb54M6Hj0uARSY@vger.kernel.org, AJvYcCXUgM5XKQ5wjTEH7Vi8GuLfi4M+GScPDeeMmapVTwX+WF8HdJnjRWe4dfBFt4iLggVQC8zXB0bfoqvDwShdvyUj@vger.kernel.org X-Gm-Message-State: AOJu0Yw4D1pZHiGz6E1APymwoSXPYh/jeJCEk286OGCy8Jsw2M85XpHk GwUfDzh/F1j6ubiPh+cyYheoZYkqn5JK8Hd74TalViSgxPxZp3Hj X-Google-Smtp-Source: AGHT+IFoKDoHpmJaD6yE4W2N1U45T+EI0QyMYYxv0LMNxM6JTH/xn+k5ujEZNnagKfIwfoDj5uadqQ== X-Received: by 2002:a05:6a20:748e:b0:1d8:fb32:1cec with SMTP id adf61e73a8af0-1db7fd9230fmr1285218637.5.1730252598399; Tue, 29 Oct 2024 18:43:18 -0700 (PDT) Received: from localhost.localdomain ([43.129.25.208]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-7edc866906dsm8138407a12.10.2024.10.29.18.43.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 29 Oct 2024 18:43:18 -0700 (PDT) From: Menglong Dong X-Google-Original-From: Menglong Dong To: pabeni@redhat.com Cc: davem@davemloft.net, edumazet@google.com, kuba@kernel.org, horms@kernel.org, dsahern@kernel.org, pablo@netfilter.org, kadlec@netfilter.org, roopa@nvidia.com, razor@blackwall.org, gnault@redhat.com, bigeasy@linutronix.de, hawk@kernel.org, idosch@nvidia.com, dongml2@chinatelecom.cn, netdev@vger.kernel.org, linux-kernel@vger.kernel.org, netfilter-devel@vger.kernel.org, coreteam@netfilter.org, bridge@lists.linux.dev, bpf@vger.kernel.org Subject: [PATCH RESEND net-next v4 4/9] net: ip: make ip_route_input_slow() return drop reasons Date: Wed, 30 Oct 2024 09:41:40 +0800 Message-Id: <20241030014145.1409628-5-dongml2@chinatelecom.cn> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20241030014145.1409628-1-dongml2@chinatelecom.cn> References: <20241030014145.1409628-1-dongml2@chinatelecom.cn> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" In this commit, we make ip_route_input_slow() return skb drop reasons, and following new skb drop reasons are added: SKB_DROP_REASON_IP_INVALID_DEST The only caller of ip_route_input_slow() is ip_route_input_rcu(), and we adjust it by making it return -EINVAL on error. Signed-off-by: Menglong Dong --- v4: - use indentation after the out label --- include/net/dropreason-core.h | 6 ++++ net/ipv4/route.c | 56 ++++++++++++++++++++++------------- 2 files changed, 41 insertions(+), 21 deletions(-) diff --git a/include/net/dropreason-core.h b/include/net/dropreason-core.h index a2a1fb90e0e5..74624d369d48 100644 --- a/include/net/dropreason-core.h +++ b/include/net/dropreason-core.h @@ -79,6 +79,7 @@ FN(IP_LOCAL_SOURCE) \ FN(IP_INVALID_SOURCE) \ FN(IP_LOCALNET) \ + FN(IP_INVALID_DEST) \ FN(PKT_TOO_BIG) \ FN(DUP_FRAG) \ FN(FRAG_REASM_TIMEOUT) \ @@ -386,6 +387,11 @@ enum skb_drop_reason { SKB_DROP_REASON_IP_INVALID_SOURCE, /** @SKB_DROP_REASON_IP_LOCALNET: source or dest ip is local net */ SKB_DROP_REASON_IP_LOCALNET, + /** + * @SKB_DROP_REASON_IP_INVALID_DEST: the dest ip is invalid: + * 1) dest ip is 0 + */ + SKB_DROP_REASON_IP_INVALID_DEST, /** * @SKB_DROP_REASON_PKT_TOO_BIG: packet size is too big (maybe exceed the * MTU) diff --git a/net/ipv4/route.c b/net/ipv4/route.c index 566acd08aedf..1c4727504909 100644 --- a/net/ipv4/route.c +++ b/net/ipv4/route.c @@ -2204,9 +2204,10 @@ static struct net_device *ip_rt_get_dev(struct net *= net, * called with rcu_read_lock() */ =20 -static int ip_route_input_slow(struct sk_buff *skb, __be32 daddr, __be32 s= addr, - dscp_t dscp, struct net_device *dev, - struct fib_result *res) +static enum skb_drop_reason +ip_route_input_slow(struct sk_buff *skb, __be32 daddr, __be32 saddr, + dscp_t dscp, struct net_device *dev, + struct fib_result *res) { enum skb_drop_reason reason =3D SKB_DROP_REASON_NOT_SPECIFIED; struct in_device *in_dev =3D __in_dev_get_rcu(dev); @@ -2236,8 +2237,10 @@ static int ip_route_input_slow(struct sk_buff *skb, = __be32 daddr, __be32 saddr, fl4.flowi4_tun_key.tun_id =3D 0; skb_dst_drop(skb); =20 - if (ipv4_is_multicast(saddr) || ipv4_is_lbcast(saddr)) + if (ipv4_is_multicast(saddr) || ipv4_is_lbcast(saddr)) { + reason =3D SKB_DROP_REASON_IP_INVALID_SOURCE; goto martian_source; + } =20 res->fi =3D NULL; res->table =3D NULL; @@ -2247,21 +2250,29 @@ static int ip_route_input_slow(struct sk_buff *skb,= __be32 daddr, __be32 saddr, /* Accept zero addresses only to limited broadcast; * I even do not know to fix it or not. Waiting for complains :-) */ - if (ipv4_is_zeronet(saddr)) + if (ipv4_is_zeronet(saddr)) { + reason =3D SKB_DROP_REASON_IP_INVALID_SOURCE; goto martian_source; + } =20 - if (ipv4_is_zeronet(daddr)) + if (ipv4_is_zeronet(daddr)) { + reason =3D SKB_DROP_REASON_IP_INVALID_DEST; goto martian_destination; + } =20 /* Following code try to avoid calling IN_DEV_NET_ROUTE_LOCALNET(), * and call it once if daddr or/and saddr are loopback addresses */ if (ipv4_is_loopback(daddr)) { - if (!IN_DEV_NET_ROUTE_LOCALNET(in_dev, net)) + if (!IN_DEV_NET_ROUTE_LOCALNET(in_dev, net)) { + reason =3D SKB_DROP_REASON_IP_LOCALNET; goto martian_destination; + } } else if (ipv4_is_loopback(saddr)) { - if (!IN_DEV_NET_ROUTE_LOCALNET(in_dev, net)) + if (!IN_DEV_NET_ROUTE_LOCALNET(in_dev, net)) { + reason =3D SKB_DROP_REASON_IP_LOCALNET; goto martian_source; + } } =20 /* @@ -2316,19 +2327,26 @@ static int ip_route_input_slow(struct sk_buff *skb,= __be32 daddr, __be32 saddr, err =3D -EHOSTUNREACH; goto no_route; } - if (res->type !=3D RTN_UNICAST) + if (res->type !=3D RTN_UNICAST) { + reason =3D SKB_DROP_REASON_IP_INVALID_DEST; goto martian_destination; + } =20 make_route: err =3D ip_mkroute_input(skb, res, in_dev, daddr, saddr, dscp, flkeys); -out: return err; + if (!err) + reason =3D SKB_NOT_DROPPED_YET; + +out: + return reason; =20 brd_input: - if (skb->protocol !=3D htons(ETH_P_IP)) - goto e_inval; + if (skb->protocol !=3D htons(ETH_P_IP)) { + reason =3D SKB_DROP_REASON_INVALID_PROTO; + goto out; + } =20 if (!ipv4_is_zeronet(saddr)) { - err =3D -EINVAL; reason =3D fib_validate_source_reason(skb, saddr, 0, dscp, 0, dev, in_dev, &itag); if (reason) @@ -2349,7 +2367,7 @@ out: return err; rth =3D rcu_dereference(nhc->nhc_rth_input); if (rt_cache_valid(rth)) { skb_dst_set_noref(skb, &rth->dst); - err =3D 0; + reason =3D SKB_NOT_DROPPED_YET; goto out; } } @@ -2386,7 +2404,7 @@ out: return err; rt_add_uncached_list(rth); } skb_dst_set(skb, &rth->dst); - err =3D 0; + reason =3D SKB_NOT_DROPPED_YET; goto out; =20 no_route: @@ -2407,12 +2425,8 @@ out: return err; &daddr, &saddr, dev->name); #endif =20 -e_inval: - err =3D -EINVAL; - goto out; - e_nobufs: - err =3D -ENOBUFS; + reason =3D SKB_DROP_REASON_NOMEM; goto out; =20 martian_source: @@ -2469,7 +2483,7 @@ static int ip_route_input_rcu(struct sk_buff *skb, __= be32 daddr, __be32 saddr, return reason ? -EINVAL : 0; } =20 - return ip_route_input_slow(skb, daddr, saddr, dscp, dev, res); + return ip_route_input_slow(skb, daddr, saddr, dscp, dev, res) ? -EINVAL := 0; } =20 int ip_route_input_noref(struct sk_buff *skb, __be32 daddr, __be32 saddr, --=20 2.39.5