# RouterOS blatantly ignores pref-src. Can this really be a bug?

**URL:** <https://forum.mikrotik.com/t/routeros-blatantly-ignores-pref-src-can-this-really-be-a-bug/180360>\
**Category:** General\
**Created:** [November 28, 2024, 12:31am UTC](https://forum.mikrotik.com/t/routeros-blatantly-ignores-pref-src-can-this-really-be-a-bug/180360 "2024-11-28T00:31:56Z")\
**Posts on this page:** 1\
**Showing post:** 79

<div class="post-metadata">

**Author:** ![lurker888](https://forum.mikrotik.com/letter_avatar/lurker888/32/6_449c533f586939b6e5f246632a46421f.png) [@lurker888](https://forum.mikrotik.com/u/lurker888)\
**Post date:** [April 5, 2025, 7:22pm UTC](https://forum.mikrotik.com/t/routeros-blatantly-ignores-pref-src-can-this-really-be-a-bug/180360/79 "2025-04-05T19:22:41Z")

</div>

> [@](#):
>
> My bad that is valid, but this is assuming the remote router is an MT router. ( client peer for handshake) … makes sense, so other peers connecting to the local router can easily re-enter the tunnel and reach the remote router via the local router, so to speak.  
> The local router needs  
> allowed IPs=192.168.50.1/32 You cant have both ends with allowed addresses at 192.168.50.0/24 LOL

As described, the side whose configuration was presented has this peer entry, and this address (and the dynamic route it adds):

```plaintext
/interface wireguard
add listen-port=13231 mtu=1420 name=wireguard1
/interface wireguard peers
add allowed-address=192.168.50.0/24 interface=wireguard1 name=peer1 public-key="22SMLvYh7cQzj3/sGFeFLn7l0xVePXM59DLms3qnNUY=" responder=yes

/ip address
add address=192.168.50.2/24 interface=wireguard1

```

The other side has this:

```plaintext
/interface wireguard
add listen-port=13231 mtu=1420 name=wg-test
/interface wireguard peers
add allowed-address=192.168.50.0/24 endpoint-address=192.168.112.10 endpoint-port=13231 interface=wg-test name=peer4 persistent-keepalive=10s public-key="+XbZ8kxNGw2i6my6hOzSK3Ih2VMH03gjzckpr4+5cj0="

/ip address
add address=192.168.50.1/24 interface=wg-test

```

Nothing wrong with it and works fine. Both sides can ping each other, transit traffic, etc.

Using a /32 allowed address on one end and a wider subnet on the other is a common config. So is using /32 on both. So is using the same /30 on both.

> [@](#):
>
> +++++++++++++++++++++++++++++++++++++
> 
> Hmm interesting comment:  
> The first thing is that actually the normal way of “apply connection mark, add a routing table, mark routing on output” thing _would_ work just fine with wireguard. **The problem is that the source address of the outgoing packet is determined using the “main” routing table,** and thus conntrack can’t identify the connection (thus treating it as new, and the connection mark is therefore “not there”). If we disregard this and force the routing table, even though the table selection and the egress interface will be correct, the source address will still be incorrect.
> 
> Makes me wonder if we shouldn’t simply  
> _add dst-address=RemoteWANIP gateway=WAN2 table=main distance=2  
> add dst-address=0.0.0.0/0 gateway=WAN1 table=main distance=4  
> add dst-address=0.0.0.0/0 gateway=WAN2 table=main distance=6_
> 
> Fix our problem LOL if we had flexibility in what entry arguments were for destination address.  
> ++++++++++++++++++++++++

Absolutely correct. You don’t even have to have a smaller distance for the /32 route - longer prefixes are always preferred, regardless of distance. If you know the peer’s address is advance this is actually fine to do.

> [@](#):
>
> The first crux of my lack of understanding is exactly here, everything above I followed…
> 
> - **for determining the src address, a lookup in the “main” table is done,** and a route to 172.16.10.0/30 is found (added dynamically by the address assignment), and for this the appropriate src address is 172.16.10.1 - thus this is selected
> 
> I simply dont understand this process, where does this rule come from? Is this some basic networking stuff I would know if I actually studied IT and networking??  
> Why is the table or routing even in the picture for this decision???.. since the destination packets were sent to 172.16.10.1 wouldnt that automatically mean the that address would be the source address of reply packets…

For normal services (such as DNS over udp, OpenVPN over udp, etc.) you are correct and the source address is where the request was sent to. Wireguard follows a different architecture, and leaves the source address to be assigned by the initial output routing stage. (It is actually done to allow easy peer roaming.)

My post with the full description:  
[http://forum.mikrotik.com/t/mangle-policy-based-routing/181586/6](http://forum.mikrotik.com/t/mangle-policy-based-routing/181586/6)  
To quote:

> [@](#):
>
> The _other_ - and in your current case more important - story concerns the source address of packets. Source addresses are normally assigned either (1) by the application layer (e.g. a DNS server receives a UDP query, it should clearly specify (to the kernel) as the source address of the reply the address the query was received on), (2) by other networking layers, e.g. TCP will originate packets from the address the SYN was received on, or from which the SYN was sent during the handshake. But if still a packet has no source address (often referenced as the “address of last resort”) the packet acquires the “pref src address” of the route it takes. And here’s the strangeness: this assignment happens during the initial “routing decision” and is not (can’t be) corrected in the “routing adjustment” phase. Wireguard - by its design - doesn’t assign source addresses to its outgoing packets, which leads to a situation where if the packet is to be redirected in the “mangle postrouting” chain, it will go out on the correct interface, but with the wrong source address. Happy world.

> [@](#):
>
> I do understand that conntrac sees 172.16.10.1 sending traffic to 172.16.10.2  
> What I dont understand is why the connection marks on any of this traffic was removed…

Packets don’t carry connection marks (it’s not in the IP packet), they are assigned by conntrack. (There are some exceptions.) The packet output by wireguard has no marks whatsoever on it initially. The packet goes:

[emitted by wg] → initial routing decision → conntrack → mangle output → final routing decision (routing adjustment) → and so on

The packet is annotated with the connection mark in the conntrack phase. Until then, there is no associated connection mark. (On normal linux, wg interfaces have a property fwmark, which allows all packets emitted by wg to be marked on creation - this is not currently available on Mikrotik.)

> [@](#):
>
> What I dont understand is how the router can magically put them back on without admin direction…

Again, packets on interfaces (ethernet, virtual interfaces, all of them) come into the router “naked”. Conntrack labels them based on the conntrack entry it associates with them. When a mangle assigns a connection mark, it is noted in the conntrack entry the packets nf metadata references.

> [@](#):
>
> Where was conntrack involved in the process of changing the addresses\>\> Conntrac is not involved after DSTNAT in prerouting its before, and of course before the next block of input srcnat.
> 
> (as per packet flow diagram)

Again, dst-nat modifies the conntrack entry. That’s its primary purpose. The translation is done immediately to be in line with the (freshly modified) conntrack entry.

> [@](#):
>
> Connection tracking is only seen again in output just before mangle output which I suppose is where we move that traffic out WAN2 with the connection marks and route mark.
> 
> I do understand the concepts of un-dst and un-src so that we have a traffic flow that now resembles 192.168.112.10 to 192.168.33.1.  
> I do understand that the original dst port (listening port on local device) and original src port ( listening port from remote device) were maintained throughout the process and need no shenanigans.
> 
> The other thing that is not clear to me is how this fools the router at all vis-a-vis avoiding WAN1. In SINDYs rule we fool the router in the last step, by getting the router to un-dsnat out wan2 vice WAN1.  
> We do no such thing in your example. We piddle around a faux bridge, which is mainly to avoid the scenario if WAN1 dies. but I dont see how we avoid WAN1 in main table, which SIndys rule addresses.

Un-dstnat only changes the address. The interface is decided by routing. In mangle output a routing mark “wan2” is applied based on the connection mark “wg-wan2”, and the output interface is determined in the “routing adjustment” phase based on the routing mark “wan2” to be the wan2 interface.

---

_[View the full topic](https://forum.mikrotik.com/t/routeros-blatantly-ignores-pref-src-can-this-really-be-a-bug/180360)._
