About UDP services

Hello!
I have some doubts about how UDP works.
In this case I'm using a Mikrotik router, but I don't know if this is a common behaviour.
It's not really a problem, i just want to understand why and how some things happens.
Let me explain.

When a client contacts a web server, so protocol TCP, it asks from a random port a connection to the 443 port. The server replies from the 443 port to the random client port.

I've noticed different behaviour with the UDP protocol, in this case on VoIP and SNTP services.
The requests come from the client using the the same port that the service exposes.

For example:

forward: in:LocalLAN out:PPPoE_WAN, connection-state:established,snat src-mac MAC_Address, proto UDP, 192.168.1.30:5060->VoIP_Server_address:5060, NAT (192.168.30.1:5060->WAN_IP_Address:5060)->VoIP_Server_address:5060, len 782

I find it quite strange. I've noticed it just because, followind a guide, I was securing the network dropping the connection coming from the WAN from the most common ports (123, 5060 and so on). I don't have such servers in my network, so I thought closing these ports would be harmless.

So, this is a common behaviour or I'm simply doing something wrong elsewhere?

As usual, thank you for helping me understand things!

This - as is so often the case - again depends.

Back in the days DNS queries from server to server via UDP were port 53 to port 53. Later this was changed to a random high port at the side initiating the exchange.

NTP, though, is still port 123 source and destination for all involved servers, defined by the standard.

Your SIP example seems to use 5060 at both ends, too, but IIRC this is not mandatory so implementations can effectively do whatever they like.

UDP encapsulated IPsec/ESP is 4500 at both ends. Most implementations make it configurable but it is still a single fixed port.

So the general rule seems to be: don't make assumptions. Products/protocols/implementations can do whatever they please.

Not always but ordinarily

https://www.rfc-editor.org/info/rfc5905/

srcport: UDP port number of the server or reference clock. This
becomes the destination port number in packets sent from this
association. When operating in symmetric modes (1 and 2), this field
must contain the NTP port number PORT (123) assigned by the IANA. In
other modes, it can contain any number consistent with local policy.
....

dstport: UDP port number of the client, ordinarily the NTP port
number PORT (123)
assigned by the IANA. This becomes the source port
number in packets sent from this association.

Thank you very much!

I read the RFC and understood how to setup some rules to use correctly SNTP and VoIP, but at the same time protect the router from incoming unwanted connection.

I'm still playing with the rules, and I came across the problem of setting up the router as a DNS server.

So, I have this:

/ip dns
set allow-remote-requests=yes servers=8.8.8.8,208.67.222.222

then the redirection of the port 53, before the generic masquerading rule:

/ip firewall nat
add action=redirect chain=dstnat dst-port=53 in-interface-list=LAN protocol=udp src-address-type=!local
add action=redirect chain=dstnat dst-port=53 in-interface-list=LAN protocol=tcp src-address-type=!local
add action=masquerade chain=srcnat out-interface-list=WAN

Everything works fine as long as the DNS server on the clients is set to the address of the router itsels, in my case 192.168.1.1.
If I set the DNS server of the client to something different, like 8.8.4.4, the client stops resolving the names.
I can of course push the correct DNS server to the clients with the DHCP, but what I can do if I want to avoid the problem of the manually set servers, or hardcoded ones?

Is there a way to "translate" the DNS request sent to other servers to the address of the router?

Thank you again!

When action=redirect does not work you could try action=dst-nat with to-addresses=192.168.88.1 or whatever the address of your router is.

That works at least for me, but understand that this does not capture all DNS traffic from clients, so do not expect this to be a solution to e.g. redirect traffic to some domain name to a local address.

My DNS redirection rules that have been working fine for years look like this:

/ip firewall nat
add action=redirect chain=dstnat dst-address-type=!local dst-port=53 in-interface-list=XXX protocol=udp
add action=redirect chain=dstnat dst-address-type=!local dst-port=53 in-interface-list=XXX protocol=tcp

Using dst-address-type=!local is more suitable than what you are doing src-address-type=!local because:

  • When packets are coming from outside (as restricted by your in-interface-list=LAN) they normally don't have one of the router's IP addresses as source, unless the packets were intentionally spoofed. Which means src-address-type=!local is unnecessary.

  • We don't want to perform the redirect action, when the destination address of the packets is already the router, hence dst-address-type=!local is perfect to prevent this.

However, if the redirect rules don't work even in your version, then check whether your filter table has some drop action related to connection-nat-state=dstnat somewhere.

Hi!
Of course you were right! It was my fault.
So, I've changed the NAT rules as you suggested:

/ip firewall nat
add action=redirect chain=dstnat dst-address-type=!local dst-port=53 in-interface-list=LAN protocol=udp
add action=redirect chain=dstnat dst-address-type=!local dst-port=53 in-interface-list=LAN protocol=tcp

then I checked the mangle rules I use to send some traffic on a secondary WAN. This is the original:

/ip firewall mangle
add action=accept chain=prerouting dst-address-list=LAN_Subnet
add action=mark-connection chain=prerouting connection-mark=no-mark dst-address-type=!local in-interface-list=LAN new-connection-mark=WAN2_connection src-address-list=Secondary_WAN
add action=mark-connection chain=prerouting connection-mark=no-mark dst-address-type=!local in-interface-list=LAN new-connection-mark=WAN1_connection src-address-list=!Secondary_WAN
add action=mark-routing chain=prerouting connection-mark=WAN2_connection in-interface-list=LAN new-routing-mark=to_WAN2
add action=mark-routing chain=prerouting connection-mark=WAN1_connection in-interface-list=LAN new-routing-mark=to_WAN1

I've added two rules to bypass the route selection for the DNS protocol before the actual marking protocol, works:

/ip firewall mangle
add action=accept chain=prerouting dst-address-list=LAN_Subnet
add action=accept chain=prerouting port=53 protocol=udp
add action=accept chain=prerouting port=53 protocol=tcp
...

So, now I have to understand why this is happening.
Maybe the router itself is following the "main" routing table, so someway it doesn't understand the routing mark?
I'll try some tests!

You did not tell us before that you have multiple routing tables and routing marks!

Indeed that is a known source of trouble especially for router-initiated traffic.

This is why many people ask for an export file right away,
rather than relying on the person writing the post to use their common sense
and include the full configuration.

The reason why it didn't work with the previous mangle rules can be explained, but first, some literatures:

As you already did in your original version of the mangle rules, normally the correct thing to do is to exclude packet destined to the router itself from being affected by mangle mark-routing. The reason is that because in the default routing lookup order (listed under policy-rules) mangle comes before local.

If the destination of a packet is one of the router's IP addresses, normally if you want the router to be able to receive it, it must reach the local chain of the routing lookup. But if you applied mangle mark-routing on the packet, it will be handled by the mangle chain, which by default comes before local. Which means the router will not be able to receive the packets.

That's why normally you see the condition such as dst-address-type=!local added to the mangle rules, like you also did with the mark-connection rules. So that the packet with destination address being an address of the router will be excluded from mark-connection and thus later from mark-routing.

Usually that's enough, however, not when DSTNAT is involved. If you look at the linked flowchart:

Mangle prerouting comes at #4, before DSTNAT, which is at #10. So when the client make a DNS query to 8.8.8.8, the initial UDP53 or TCP53 packet that hits the mangle mark-connection rule still has dst-address=8.8.8.8, which does satisfy dst-address-type=!local because 8.8.8.8 is not one of the router's IP addresses. The connection will be marked. The later mark-routing rule will apply the routing mark on the packet.

Then at #10 DSTNAT redirect changes 8.8.8.8 to the router's IP address on the LAN interface.

At #11 where Routing Lookup happens, the packet is handled by the mangle routing decision chain, but the packet has a destination address of type local.

The mangle chain can only use the routing table pointed at by the routing mark for the lookup, but for a packet destined for the router, those tables are useless, only the local routing decision chain has the ability to correctly handle them, but this chain sits below mangle. As a result, the packet is routed outside of the router, using one of the matching routes of the table (for example a dst-address=0.0.0.0/0 route that sends the packets to the internet), or if no active matching route is found, the packet will be discarded.

The fix is to make sure that those DNS packets are not affected by your mark-connection / mark-routing rules, as you've figured out with:

Well, I think "thank you" it's not enough!
I hope you didn't have to spend too much time writing this answer, but I'm deeply grateful!
I've learned a lot and learned new concepts, and the graph is enlightening; I've already printed several copies to keep in my notes. I'd found the packet flow graph in the official Mikrotik documents, but I'd gotten lost several times trying to understand it; this one is more immediate and within my reach.
Thank you so much, you've been very kind!