Live data from Hacker News

How does Linux NAT a ping?

devnonsense.com

71–80 of 108 posts

Re: How does Linux NAT a ping?

#71
It annoys me when I write blog posts like this, it's so hard to link to a specific line of code and have that link stay alive and useful/fresh over time.

I guess if it's GitHub, you can tie it to a specific commit hash, file name, line number tuple, but if the codebase ever changes a lot its not super useful. I've also not had luck with other, less used git webviews (git.blender.org)

Re: How does Linux NAT a ping?

#72

It annoys me when I write blog posts like this, it's so hard to link to a specific line of code and have that link stay alive and useful/fresh over time. I guess if it's GitHub, you can tie it to a specific commit hash, file name, line number tuple, but if the codebase ever changes a lot its not super useful. I've also not had luck with other, less used git webviews (git.blender.org)

For linux kernel code, you can use elixir, so it'll at least be linked to a specific version. You can use an LTS version if you want the code to have at least some staying power.

https://elixir.bootlin.com/linux/latest/source

Re: How does Linux NAT a ping?

#73
post #58

When a ping is sent from a device on a local network to a device on the internet, the router performing NAT rewrites the source address of the ping to its public IP address and rewrites the ID field of the ICMP packet to a unique value. When the response is received, the router uses the unique ID value to forward the response to the correct device on the local network.

Why not use the source private IP instead of the “unique value”?

One reason would be to not expose details about your private network to every hop the ICMP packet traverses. Even if knowing you have some 192.168.1.x host is not on its own very useful to an attacker, it'd be preferable to not expose that.

It's another reason WebRTC/STUN was a big issue when it first became widely available, it made it easy to leak details about your LAN to outside servers.

Re: How does Linux NAT a ping?

#74

When a ping is sent from a device on a local network to a device on the internet, the router performing NAT rewrites the source address of the ping to its public IP address and rewrites the ID field of the ICMP packet to a unique value. When the response is received, the router uses the unique ID value to forward the response to the correct device on the local network.

Ping needs that bit if state itself anyway to match replies to requests.

Re: How does Linux NAT a ping?

#75

Earlier quoted context omitted.

You could be using IPv6 ULA addresses internally on your home network to have static addressing. The real solution is moving to DNS names though with your router maintaining them based on DHCP leases or just using multicast DNS (Zeroconf). In the future you can probably go "IPv6-mostly" with a CLAT engine to ditch dual-stack: https://blog.apnic.net/2022/11/21/deploying-ipv6-mostly-acce...

> In the future you can probably go "IPv6-mostly" with a CLAT engine ...although there still isn't any kernel support for the necessary SIIT v4 v6 translation, so to implement CLAT you end up using unmaintained (and unmergeably bad) out-of-tree kernel modules or unmaintained (and slow) userspace daemons hanging off a tuntap interface.

pf on OpenBSD does it fine.

Re: How does Linux NAT a ping?

#76
post #51
post #22

Earlier quoted context omitted.

Taking this thought just a tiny bit further, this is changing a stateless protocol to a stateful one.

NAT stands for Network Address Translation, which means a NAT device maintains a translation table of internal IPs to external, so that it can return response packets coming from Internet to a proper destination on the internal network. By definition NAT will maintain state which is translation table. Now that table can be dynamic or static, but it doesn't change the fact that there will be some state to maintain.

> By definition NAT will maintain state which is translation table.

Stateless NAT is also possible, but then it has to be 1:1. Which has it's purpose, but is rarely used.

A practical example would be with IPv6 if your ISP doesn't allocate you a static prefix. Stateless NAT would allow you to use a /64 from the private range of fd00::/8 in your local network which the router would translate to your globally unique /64. No state needed, because there would be as many IPs available in your LAN prefix as in your GUA prefix. All it would do would be translating fdxx:xxxx:xxxx:xxxx:1234:1234:1234:1234 to 2yyy:yyyy:yyyy:yyyy:1234:1234:1234:1234 and vice versa.

I've also done stateless NAT on IPv4. When you request more IPs from some cloud providers, they assign you a bunch of /32s, not a proper subnet, virtually requiring you to run a cloud router.

Re: How does Linux NAT a ping?

#77
post #66

NAT is such a trashy abstraction. IPv4 needs to die.

IPv6 needs to die also. It had more than enough time to become dominant and has just floundered.

https://www.google.com/intl/en/ipv6/statistics.html 45% (and growing) of all traffic to Google is IPv6. Hardly "floundered". It's just that most major ISPs in the developed world have so many IPv4 addresses they don't care that much about IPv6 yet.

Now, try starting a new ISP without CGNAT (which will lead to a garbage experience for everyone) or IPv6. You'll have to spend literal tens (if not hundreds) of millions just on IP addresses alone.

Re: How does Linux NAT a ping?

#78

NAT is such a trashy abstraction. IPv4 needs to die.

I have a few devices on my home internet, on a handful of 192.168 subnets The other week I moved my ISP. The AS my house belonged to obviously changed to the new ISP, and I got a new v4 IP All I had to do was update my Wan router to forward trafffic from the new Ip. Instead with ipv6 I would have to change every node on my network, update my internal DNS. Now in theory I could have my own /48 which I take with me. Th…

In IPv6 you'd do exactly like you'd do with IPv4, by assigning ULAs (private, local addresses) to your machines from fc00::/7.

With the (IMHO) big advantage that unless some madman has configured NAT66, the traffic over ULA will *never* get out into the internet.

The fact you have GUAs allocated doesn't mean you have to necessarily use them for your internal traffic. Most of the time link local addresses (on small scale, with auto discovery via LLMNR or mDNS) or ULAs are way more convenient than GUAs or IPv4 local addresses.

Re: How does Linux NAT a ping?

#79
post #48
post #32

Earlier quoted context omitted.

Priorities. I don't have to put up with PPPoE in 2023, but it's a hell of a lot less expensive than pulling munifiber to my garage (and the monthly fees for munifiber are higher too, so there's no point in time where it makes economic sense), and consistency and stable addressing is currently winning over the promise of 5g/leo satellite.

Sure, but you're choosing that prioritisation. It's not being forced on you.

I'm not even sure if you and GP agree or disagree.

As for me, I want IPv4 to stay forever. It works for me and I don't see any reason to spend time and migrate to something else.

Post reply on HN