From: shiming cheng <shiming.cheng@mediatek.com>
To: <willemdebruijn.kernel@gmail.com>, <davem@davemloft.net>,
<dsahern@kernel.org>, <edumazet@google.com>, <kuba@kernel.org>,
<pabeni@redhat.com>, <horms@kernel.org>, <matthias.bgg@gmail.com>,
<angelogioacchino.delregno@collabora.com>,
<linux-kernel@vger.kernel.org>,
<linux-arm-kernel@lists.infradead.org>,
<linux-mediatek@lists.infradead.org>
Cc: <netdev@vger.kernel.org>, <lena.wang@mediatek.com>,
shiming cheng <shiming.cheng@mediatek.com>
Subject: [PATCH] ipv6: socket SO_BINDTODEVICE lookup routing fail without IPv6 rule.
Date: Thu, 2 Jan 2025 17:51:11 +0800 [thread overview]
Message-ID: <20250102095114.25860-1-shiming.cheng@mediatek.com> (raw)
When using socket IPv6 with SO_BINDTODEVICE, if IPv6 rule is not
matched, it will return ENETUNREACH. In fact, IPv4 does not behave
this way. IPv4 prioritizes looking up IP rules for routing and
forwarding, if not matched it will use socket-bound out interface
to send packets. The modification here is to make IPv6 behave the
same as IPv4. If IP rule is not found, it will also use
socket-bound out interface to send packts.
Signed-off-by: shiming cheng <shiming.cheng@mediatek.com>
---
include/net/ip6_route.h | 2 ++
net/ipv6/ip6_output.c | 6 +++++-
net/ipv6/route.c | 34 ++++++++++++++++++++++++++++++++++
3 files changed, 41 insertions(+), 1 deletion(-)
diff --git a/include/net/ip6_route.h b/include/net/ip6_route.h
index 6dbdf60b342f..0625597def6f 100644
--- a/include/net/ip6_route.h
+++ b/include/net/ip6_route.h
@@ -214,6 +214,8 @@ void rt6_multipath_rebalance(struct fib6_info *f6i);
void rt6_uncached_list_add(struct rt6_info *rt);
void rt6_uncached_list_del(struct rt6_info *rt);
+struct rt6_info *ip6_create_rt_oif_rcu(struct net *net, const struct sock *sk,
+ struct flowi6 *fl6, int flags);
static inline const struct rt6_info *skb_rt6_info(const struct sk_buff *skb)
{
diff --git a/net/ipv6/ip6_output.c b/net/ipv6/ip6_output.c
index f7b4608bb316..ed162ac3cb31 100644
--- a/net/ipv6/ip6_output.c
+++ b/net/ipv6/ip6_output.c
@@ -1156,7 +1156,11 @@ static int ip6_dst_lookup_tail(struct net *net, const struct sock *sk,
*dst = ip6_route_output_flags(net, sk, fl6, flags);
err = (*dst)->error;
- if (err)
+ if (err && (flags & RT6_LOOKUP_F_IFACE)) {
+ *dst = (struct dst_entry *)ip6_create_rt_oif_rcu(net, sk, fl6, flags);
+ if (!*dst)
+ goto out_err_release;
+ } else if (err) {
goto out_err_release;
#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
diff --git a/net/ipv6/route.c b/net/ipv6/route.c
index 67ff16c04718..7d7450fab44f 100644
--- a/net/ipv6/route.c
+++ b/net/ipv6/route.c
@@ -1214,6 +1214,40 @@ static struct rt6_info *ip6_create_rt_rcu(const struct fib6_result *res)
return nrt;
}
+struct rt6_info *ip6_create_rt_oif_rcu(struct net *net, const struct sock *sk,
+ struct flowi6 *fl6, int flags)
+{
+ struct rt6_info *rt;
+ unsigned int prefs;
+ int err;
+ struct net_device *dev = dev_get_by_index_rcu(net, fl6->flowi6_oif);
+
+ if (!dev)
+ return NULL;
+ rt = ip6_dst_alloc(dev_net(dev), dev, flags);
+
+ if (!rt)
+ return NULL;
+ rt->dst.error = 0;
+ rt->dst.output = ip6_output;
+ rt->dst.lastuse = jiffies;
+ prefs = sk ? inet6_sk(sk)->srcprefs : 0;
+ err = ipv6_dev_get_saddr(net, dev, &fl6->daddr, prefs, &fl6->saddr);
+
+ if (err) {
+ dst_release(&rt->dst);
+ return NULL;
+ }
+ rt->rt6i_dst.addr = fl6->daddr;
+ rt->rt6i_dst.plen = 128;
+ rt->rt6i_src.addr = fl6->saddr;
+ rt->rt6i_dst.plen = 128;
+ rt->rt6i_idev = in6_dev_get(dev);
+ rt->rt6i_flags = flags;
+ return rt;
+}
+EXPORT_SYMBOL_GPL(ip6_create_rt_oif_rcu);
+
INDIRECT_CALLABLE_SCOPE struct rt6_info *ip6_pol_route_lookup(struct net *net,
struct fib6_table *table,
struct flowi6 *fl6,
--
2.45.2
next reply other threads:[~2025-01-02 9:48 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-01-02 9:51 shiming cheng [this message]
2025-01-02 12:12 ` [PATCH] ipv6: socket SO_BINDTODEVICE lookup routing fail without IPv6 rule kernel test robot
2025-01-02 13:03 ` kernel test robot
2025-01-02 13:48 ` kernel test robot
2025-01-02 15:57 ` David Ahern
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20250102095114.25860-1-shiming.cheng@mediatek.com \
--to=shiming.cheng@mediatek.com \
--cc=angelogioacchino.delregno@collabora.com \
--cc=davem@davemloft.net \
--cc=dsahern@kernel.org \
--cc=edumazet@google.com \
--cc=horms@kernel.org \
--cc=kuba@kernel.org \
--cc=lena.wang@mediatek.com \
--cc=linux-arm-kernel@lists.infradead.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mediatek@lists.infradead.org \
--cc=matthias.bgg@gmail.com \
--cc=netdev@vger.kernel.org \
--cc=pabeni@redhat.com \
--cc=willemdebruijn.kernel@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox