Development discussion of WireGuard
 help / color / mirror / Atom feed
From: 曹煜 <cao88yu@gmail.com>
To: "Daniel Gröber" <dxld@darkboxed.org>
Cc: Nico Schottelius <nico.schottelius@ungleich.ch>,
	Roman Mamedov <rm@romanrm.net>,  tlhackque <tlhackque@yahoo.com>,
	wireguard@lists.zx2c4.com
Subject: Re: Src addr code review (Was: Source IP incorrect on multi homed systems)
Date: Mon, 20 Feb 2023 08:28:48 +0800	[thread overview]
Message-ID: <CACu-5+0+7h5XjM1xVjK+frwF=dOteoa+wuw_vhesJ7_ek1vfjQ@mail.gmail.com> (raw)
In-Reply-To: <20230219224200.g5mwcaybee4hujov@House.clients.dxld.at>

Hi all,
I've hacked that source code myself months ago, and it works well on
my use case (I have 4 dual stack pppoe wan set on my openwrt router,
and seted a wireguard sever on it), my hack will pickup the dst_addr
from incoming handshake packet in kernel sk_buff, and then use that
addr as src_addr to reply.
I'm not good at source code, and I know that my hack may be ugly, but
it works, hope this patch can help:
https://github.com/openwrt/packages/issues/9538#issuecomment-1150592803

Daniel Gröber <dxld@darkboxed.org> 于2023年2月20日周一 06:42写道:
>
> Hi,
>
> I though it might be useful to do some quick and dirty code review instead
> of speculating wildly to figure out where these source IP selection
> problems could be coming from ;)
>
> From previous code deep dives I know the udp_tunnel_xmit_skb function is
> where tunnel packets get handed off to the kernel. So in
> net/wireguard/socket.c:send4 we have:
>
>         udp_tunnel_xmit_skb(rt, sock, skb, fl.saddr, fl.daddr, ds,
>                             ip4_dst_hoplimit(&rt->dst), 0, fl.fl4_sport,
>                             fl.fl4_dport, false, false);
>
> Where fl.saddr is the source address that's supposedly wrong (sometimes? I
> guess?) Where does that come from?
>
> Let's look at the code (heavily culled):
>
>         struct flowi4 fl = {
>                 .saddr = endpoint->src4.s_addr,
>         };
>         if (cache)
>                 rt = dst_cache_get_ip4(cache, &fl.saddr);
>         if (!rt) {
>                 if (unlikely(!inet_confirm_addr(sock_net(sock), NULL, 0,
>                                                 fl.saddr, RT_SCOPE_HOST)))
>                         fl.saddr = 0;
>                 if (unlikely(endpoint->src_if4 && ((IS_ERR(rt) &&
>                              PTR_ERR(rt) == -EINVAL) || (!IS_ERR(rt) &&
>                              rt->dst.dev->ifindex != endpoint->src_if4))))
>                         fl.saddr = 0;
>
> Well it's initialized from endpoint->src4.s_addr, overwritten with zero in
> some cases, which I believe lets the kernel do it's regular source addr
> selection, and populated from something called dst_cache at some callsites.
>
> @Nico could it perhaps simply be that you're hitting one of these zero'ing
> cases and that's why it's using regular kernel src addr selection instead
> of the cached endpoint src4 address?
>
> The first case !inet_confirm_addr(..., RT_SCOPE_HOST) ought to confirm that
> the saddr is actually still a local address. Makes sens if the address we
> remembered was removed from the interface we can't use it anymore.
>
> The second case looks like it's checking if the (sometimes cached) src_if4
> interface index is still what the route we're about to use points to.
>
> If neither of those seem likely we can keep reading :)
>
> --Daniel
>
>
>

  reply	other threads:[~2023-02-20  0:29 UTC|newest]

Thread overview: 34+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-02-18 20:14 Source IP incorrect on multi homed systems Nico Schottelius
     [not found] ` <CAHx9msc1cNV80YU7HRmQ9gsjSEiVZ=pb31aYqfP62hy8DeuGZA@mail.gmail.com>
2023-02-18 22:34   ` Nico Schottelius
2023-02-19  0:45 ` Mike O'Connor
2023-02-19  8:01   ` Nico Schottelius
2023-02-19  9:19     ` Mikma
2023-02-19 12:04       ` Nico Schottelius
2023-02-19 12:10     ` Nico Schottelius
2023-02-19 18:59       ` Peter Linder
     [not found]     ` <2ed829aaed9fec59ac2a9b32c4ce0a9005b8d8b850be81c81a226791855fe4eb@mu.id>
2023-02-19 12:13       ` Nico Schottelius
2023-02-19 14:39         ` Christoph Loesch
2023-02-19 16:32           ` David Kerr
2023-02-19 16:54             ` Sebastian Hyrvall
2023-02-19 18:04               ` Janne Johansson
2023-02-19 18:08                 ` Sebastian Hyrvall
2023-02-19 20:11                 ` Nico Schottelius
2023-02-19 17:05             ` tlhackque
2023-02-19 18:37               ` David Kerr
2023-02-19 18:52                 ` tlhackque
2023-02-19 18:42               ` tlhackque
2023-02-19 20:18                 ` Nico Schottelius
2023-02-19 20:42                   ` Roman Mamedov
2023-02-19 21:19                     ` Nico Schottelius
2023-02-19 22:06                       ` tlhackque
2023-02-19 22:42                       ` Src addr code review (Was: Source IP incorrect on multi homed systems) Daniel Gröber
2023-02-20  0:28                         ` 曹煜 [this message]
2023-02-20 10:40                           ` Nico Schottelius
2023-02-20 11:21                             ` 曹煜
2023-02-20  9:47                         ` Nico Schottelius
2023-02-20 20:43                           ` dxld
2023-02-19 21:39                     ` Source IP incorrect on multi homed systems tlhackque
     [not found]               ` <CADGd2DoE6TCtCxxWL7JWyNW5+yy_Pe+9MNzHznbudMWLTXQreA@mail.gmail.com>
2023-02-19 18:30                 ` Fwd: " John Lauro
2023-02-19 22:28                 ` tlhackque
2023-02-20  0:58                   ` Luiz Angelo Daros de Luca
2023-02-19 20:02           ` Nico Schottelius

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='CACu-5+0+7h5XjM1xVjK+frwF=dOteoa+wuw_vhesJ7_ek1vfjQ@mail.gmail.com' \
    --to=cao88yu@gmail.com \
    --cc=dxld@darkboxed.org \
    --cc=nico.schottelius@ungleich.ch \
    --cc=rm@romanrm.net \
    --cc=tlhackque@yahoo.com \
    --cc=wireguard@lists.zx2c4.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;
as well as URLs for NNTP newsgroup(s).