frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

Open in hackernews

Grokking NAT and packet mangling in Linux

https://vivekn.dev/blog/grokking-nat-and-packet-mangling-in-linux
42•viveknathani_•15h ago

Comments

viveknathani_•15h ago
Wrote something about computer networking. Felt like posting it here. Happy to hear your thoughts, HN!
gregw2•8h ago
Nice writeup on the different type of NATs. I learned something, thank you!

One feedback; I would use a different word ("wrangling"?) rather than "mangling" in your title. Or mention IPv6.

The title use of "mangling" alone triggered flashbacks of tracking down TCP checksum corruption in low cost home routers, or bugs in OpenBSD networking stacks back when I worked on web conferencing software. I that kind of mangling commiseration when clicking your link, but your use of the term was more for an article describing NATv4 and arguing "what IPv4 NAT does is hacky mangling, let's all use IPv6". And while making that argument (which is wistfully fair) also not really acknowledging the benefit of NAT for reducing the attack surface of inbound packets from unsolicited sources and/or explaining why that isn't relevant if you do proper firewalling with IPv6 instead. And when would IPv6 Npt (network /prefix/ translation be desired?)... But I can see that starts to go beyond the scope of your intended argument/perspective perhaps...

akerl_•8h ago
Mangle is the technical term used by the kernel for those parts of the process.
jeroenhd•6h ago
I think mentioning that IPv6 makes NAT unnecessary for most use cases is more than enough.

Of course, NAT still exists in IPv6. It probably shouldn't, but tools like Docker will assign a full /64 to your local network even on systems like VPS servers where you only have a /112 or smaller available to you. Plus, NPT is a type of NAT that just happens to switch only part of the address around, you still need to mangle checksums and such.

Most people could probably get away with Docker using your local GUA for addressing and proxying NDP directly (what's that chance your developers are actually using 2^64 addresses?) but because of the way Docker interacts with nftables and the way most Linux firewalls work, using NAT is probably easier to maintain safety for.

usrme•4h ago
If you enjoyed this, then definitely read through Tailscale's lengthy write-up about NAT traversal: https://tailscale.com/blog/how-nat-traversal-works
viveknathani_•2h ago
hi, thanks! like somebody else mentioned, it is the term used in the linux kernel itself. although i do see your point - NAT does help in reducing the attack surface.
colmmacc•8h ago
A significant wrinkle in how NAT works is IP fragmentation. UDP datagrams can be larger than an IP packet. When that happens the payload is split into multiple IP packets, but only the first packet has a UDP header in it. The NAT device needs to correlate these packets by looking at fragment IDs, and then rewrite the IP addresses in the headers.

That alone implies a second kind of state to maintain, but it gets worse. Fragments can arrive out of order. If the second or later packets arrive before the first, the NAT device has to buffer those fragments until they get the packet with the UDP header in it.

That might seem unlikely but it's surprisingly common. Modern protocols like DNSSEC do require fragmentation and in a large network with many paths fragments can end up taking different paths from each other.

Ordinarily when a network is using multiple links to load balance traffic, the routers will use flow steering. The routers look at the UDP or TCP header, make a hash of the connection/flow tuple, and then use that hash to pick a link to use. That way, all of the packets from the same connection or flow will be steered down the same link.

IP fragmentation breaks this too. Those second and subsequent packets don't have a UDP header in them, so they can't be flow steered statelessly. Smarter routers are clever enough to realize this from the beginning of the datagram and to only use a 3-tuple hash (source IP, dest IP, protocol) ... so the packets will still flow consistently. But many devices get this wrong - some just even assume there will be a UDP header and pick whatever values happen to be there.

The fragments end up taking different paths and if one link is more congested or latent enough than another, they'll ultimately arrive out of order.

This single wrinkle is probably responsible for half the complexity in a robust NAT implementation. Imagine having to solve for all of this in a highly-available and trasnactionally live-replicated implementation like managed NAT gateways.

Worst of all, this was all avoidable. If UDP datagrams were simply fragmented at the UDP layer, and every packet included a UDP header, none of this would be necessary. It's probably the worst mistake in TCP/IP. But obviously overall, it was a very successful design that brought on the Internet.

EvanAnderson•6h ago
> It's probably the worst mistake in TCP/IP.

I vote for TCP/IP lacking a session layer as being the worst mistake. We wouldn't have IP mobility issues if there'd been an explicit session layer to decouple IP from the upper layer protocols.

mindslight•2h ago
That's like the Nethead vs Bellhead argument though, and it's easy to say that with the benefit of several decades of adoption and development.
EvanAnderson•2h ago
I don't necessarily think a session layer protocol is automatically "Bell-headed". It's a natural place to plug-in per-byte billing and that ilk, for sure.

I don't exactly know the timeline between the ITU protocol suite and/or DecNet (both of which have a concept of a session layer protocol) with IPv4. I think they were somewhat contemporaneous. Certainly, the idea of a session layer isn't something that came decades later than IPv4.

Even just a host identifier, in lieu of the IP address of an interface, being used in the TCP tuple would have been so much better than what we have and probably would have been enough of a "session layer". It would be so amazing to have TCP connections that "just work" when clients or servers hop onto different IP networks, use different interfaces, etc.

Edit: It has been mentioned that Vint Cerf regretted the decision to bind the IP into the TCP tuple, too. I don't have an exact quote but I know I've heard him mention it in a talk. Ref: https://argp.github.io/2006/03/05/vint-cerfs-talk/

mindslight•1h ago
> Even just a host identifier, in lieu of the IP address of an interface, being used in the TCP tuple would have been so much better

What are you imagining as the implementation? Is it just in TCP, and IP (/ the network) is unchanged? I can see the benefit of that, but then there still needs to be some mechanism to change the binding of host->IP. And if it's not part of the core network, then it's not straightforward.

There are also other more complex problems not solved by TCP (eg security). I'd rather have a host ID be a pubkey, than some small-namespace ID with a pubkey required on top of that.

It feels like the real problem is the proliferation of different incompatible solutions to any of these problems, which was going to happen even if there was one less problem that needed to be solved.

Another way of looking at it is that TCP got so entrenched because of NAT, and having a session ID within IP instead of (implicitly) within TCP/UDP might have allowed more flexibility with creating new protocols directly on top of IP. But 2+2 more bytes of addressing would have gone a long way too!

Bluecobra•6h ago
Not sure if I agree with it being the worst mistake. The beauty of UDP is its simplicity and you get the absolute minimum. (And that’s the way I like it!) I’ve worked on low latency financial networks that route 40+ Gb of UDP multicast daily and error free. Nobody is fragmenting UDP packets, and most packet sizes are less than 1000 bytes. All financial exchanges have their own proprietary format, but all use sequence numbers in the data gram to keep track of packets.
tptacek•4h ago
A UDP protocol that deliberately keeps datagram sizes below 1000 bytes to avoid fragmentation is essentially handling fragmentation itself, as Colm proposes UDP should have done to begin with.
zokier•5h ago
IP fragmentation does not really have anything to do with UDP, it can happen regardless of the inner protocol.

> Worst of all, this was all avoidable.

It is not that simple. To avoid fragmentation you need robust path mtu detection, which is another whole can of worms. Especially when packets can have multiple paths with different mtu.

zokier•2h ago
> It's probably the worst mistake in TCP/IP.

If you think fragmentation was mistake then what other alternative do you think would have been better while also feasible at the time when ipv4 was specified? IPv6 notably traded fragmentation for path mtu discovery, but I don't think requiring pmtud would have been realistic option in 1981.

viveknathani_•2h ago
hi! thanks for explaining this bit in detail. i agree, fragmentation should be handled in the transport layer!
jekwoooooe•6h ago
I remember back in the day I had to help a hospital set up some crazy double nat Cisco vpn to another hospital. Old school physical appliance and everything. It was such a pain
esseph•5h ago
"Old school physical appliance"

Lololol

It's so funny to me how much the past 10 years absolutely decimated on-prem skills a In some areas.

I don't know what to tell you folks other than Real Locations doing Physical Things still exist, haven't gone away, and there's actually more of them now than there was.

Given the current state of cyber attacks, all eggs in one basket is probably a very bad thing. For instance, CISA has put out many notices that they consider MSPs a massive security liability. Cloud services are also a weak point.

Digital sovereignty anyone???

jekwoooooe•1h ago
On prem has become commoditized though. I would bet on aws having stronger security overall than someone running a bunch of physical appliances in their own rack.
esseph•45m ago
Aws doesn't make batch chemicals, they don't transport fuel or nuclear weapons, they don't control water plants or the electrical grid, etc.

Those things cannot expect to have internet access, and should not.

There are tons of billion dollar companies with multiple datacenters or presences in multiple datacenters because of this.

There is more physical hardware right now deployed by companies of all shapes and sizes than there ever has been in history.

CISA PPD-21 Critical Infrastructure Sectors:

* Chemical Sector

* Commercial Facilities Sector

* Communications Sector

* Critical Manufacturing Sector

* Dams Sector

* Defense Industrial Base Sector

* Emergency Services Sector

* Energy Sector

* Financial Services Sector

* Food and Agriculture Sector

* Government Facilities Sector

* Healthcare and Public Health Sector

* Information Technology Sector

* Nuclear Reactors, Materials, and Waste Sector

* Transportation Systems Sector

* Water and Wastewater Systems Sector

These things need to operate without Internet, full stop. Most of these companies have been around for decades or even centuries. They're not interested in a lot of web/SaaS and can barely even spell SaaS. They're also probably likely to outlive the next few dozen frameworks or language fashions.

jofla_net•3h ago
vpn concentrator id wager
viveknathani_•2h ago
lol!
jxjnskkzxxhx•6h ago
OT does anyone else find it off topic to see the word "grokking"? Does that mean understanding? Do we need a new word for this extremely basic concept?
throawayonthe•5h ago
the term is 60+ years old

https://en.wikipedia.org/wiki/Grok

jxjnskkzxxhx•1h ago
What's your point? It's 60 years old therefore can't possibly be stupid?

Or perhaps you have no point and are just nitpicking that I called it new? Compared to the word "to understand" it's new, it's pretty obvious that my use of the word new had a context attached.

GuinansEyebrows•5h ago
"Grok (/ˈɡrɒk/) is a neologism coined by the American writer Robert A. Heinlein for his 1961 science fiction novel Stranger in a Strange Land. While the Oxford English Dictionary summarizes the meaning of grok as "to understand intuitively or by empathy, to establish rapport with" and "to empathize or communicate sympathetically (with); also, to experience enjoyment", Heinlein's concept is far more nuanced, with critic Istvan Csicsery-Ronay Jr. observing that "the book's major theme can be seen as an extended definition of the term." The concept of grok garnered significant critical scrutiny in the years after the book's initial publication. The term and aspects of the underlying concept have become part of communities such as computer science. "

https://en.wikipedia.org/wiki/Grok

theideaofcoffee•5h ago
It's a pretty common, well-accepted use in the hacker lexicon. See esr's Jargon File [0] where, by some sources [1][2], it started being used in its capacity as meaning 'understanding' for forty-ish years now at this point.

[0] http://www.catb.org/jargon/html/G/grok.html

[1] https://books.google.com/books?id=uS4EAAAAMBAJ&pg=PA32#v=one...

[2] https://en.wikipedia.org/wiki/Grok#In_computer_programmer_cu...

pak9rabid•4h ago
Also, have we all forgotten about Groklaw already?
theideaofcoffee•4h ago
RIP Groklaw
satiated_grue•2h ago
Heard the term IP Masquerading for so long in Linux, I assumed that NAT came later. How wrong I was!

https://tldp.org/HOWTO/IP-Masquerade-HOWTO/index.html

viveknathani_•2h ago
interesting!
nodesocket•2h ago
I recently just created a NAT instance AMI (using Packer) for use on AWS based on Debian 12. The official AWS NAT instance AMI is horrendously outdated and based on end-of-life AWS Linux v1. At any rate, I was surprised to find it's incredibly easy to do using iptables. It's essentially just the following four iptables rules.

    sudo iptables -t nat -A POSTROUTING -o ens5 -j MASQUERADE
    sudo iptables -F FORWARD
    sudo iptables -A FORWARD -i ens5 -m state --state RELATED,ESTABLISHED -j ACCEPT
    sudo iptables -A FORWARD -o ens5 -j ACCEPT

    sudo iptables-save | sudo tee /etc/iptables/rules.v4 > /dev/null
Lastly a small change in sysctl to enable ipv4 forwarding:

    cat <<'EOF' | sudo tee /etc/sysctl.d/99-ip-forwarding.conf > /dev/null
    net.ipv4.ip_forward=1
    EOF

    sudo sysctl --system

Andrej Karpathy's YC AI SUS talk on the future of the industry

https://www.donnamagi.com/articles/karpathy-yc-talk
162•pudiklubi•3h ago•76 comments

The Unreasonable Effectiveness of Fuzzing for Porting Programs

https://rjp.io/blog/2025-06-17-unreasonable-effectiveness-of-fuzzing
107•Bogdanp•4h ago•13 comments

Show HN: Workout.cool – Open-source fitness coaching platform

https://github.com/Snouzy/workout-cool
438•surgomat•8h ago•151 comments

Writing documentation for AI: best practices

https://docs.kapa.ai/improving/writing-best-practices
85•mooreds•4h ago•23 comments

My iPhone 8 Refuses to Die: Now It's a Solar-Powered Vision OCR Server

https://terminalbytes.com/iphone-8-solar-powered-vision-ocr-server/
77•hemant6488•4h ago•23 comments

Show HN: I built a tensor library from scratch in C++/CUDA

https://github.com/nirw4nna/dsc
70•nirw4nna•5h ago•7 comments

Homomorphically Encrypting CRDTs

https://jakelazaroff.com/words/homomorphically-encrypted-crdts/
160•jakelazaroff•7h ago•49 comments

“Poline” is an enigmatic color palette generator using polar coordinates

https://meodai.github.io/poline/
149•zdw•3d ago•34 comments

Yes I Will Read Ulysses Yes

https://www.theatlantic.com/magazine/archive/2025/07/zachary-leader-richard-ellmann-james-joyce-review/682907/
37•petethomas•3h ago•33 comments

Terpstra Keyboard

http://terpstrakeyboard.com/web-app/keys.htm
184•xeonmc•10h ago•65 comments

Introduction to the A* Algorithm

https://www.redblobgames.com/pathfinding/a-star/introduction.html
198•auraham•1d ago•73 comments

MiniMax-M1 open-weight, large-scale hybrid-attention reasoning model

https://github.com/MiniMax-AI/MiniMax-M1
291•danboarder•13h ago•67 comments

Attimet (YC F24) – Quant Trading Research Lab – Is Hiring Founding Engineer

https://www.ycombinator.com/companies/attimet/jobs/b1w9pjE-founding-engineer
1•kbanothu•3h ago

Is There a Half-Life for the Success Rates of AI Agents?

https://www.tobyord.com/writing/half-life
161•EvgeniyZh•9h ago•88 comments

Framework Laptop 12 review

https://arstechnica.com/gadgets/2025/06/framework-laptop-12-review-im-excited-to-see-what-the-2nd-generation-looks-like/
156•moelf•5h ago•196 comments

Scrappy - make little apps for you and your friends

https://pontus.granstrom.me/scrappy/
387•8organicbits•15h ago•125 comments

Revisiting Minsky's Society of Mind in 2025

https://suthakamal.substack.com/p/revisiting-minskys-society-of-mind
37•suthakamal•5h ago•11 comments

Locally hosting an internet-connected server

https://mjg59.dreamwidth.org/72095.html
122•pabs3•15h ago•119 comments

Show HN: Trieve CLI – Terminal-based LLM agent loop with search tool for PDFs

https://github.com/devflowinc/trieve/tree/main/clients/cli
16•skeptrune•6h ago•7 comments

I counted all of the yurts in Mongolia using machine learning

https://monroeclinton.com/counting-all-yurts-in-mongolia/
193•furkansahin•12h ago•71 comments

Building agents using streaming SQL queries

https://www.morling.dev/blog/this-ai-agent-should-have-been-sql-query/
80•rmoff•5h ago•7 comments

After millions of years, why are carnivorous plants still so small?

https://www.smithsonianmag.com/articles/carnivorous-plants-have-been-trapping-animals-for-millions-of-years-so-why-have-they-never-grown-larger-180986708/
177•gmays•5d ago•77 comments

Should we design for iffy internet?

https://bytes.zone/posts/should-we-design-for-iffy-internet/
44•surprisetalk•2d ago•23 comments

Spatializing 6k years of global urbanization from 3700 BC to AD 2000

https://www.nature.com/articles/sdata201634
19•talonx•3d ago•1 comments

A different take on S-expressions

https://gist.github.com/tearflake/569db7fdc8b363b7d320ebfeef8ab503
28•tearflake•3d ago•18 comments

Real-time action chunking with large models

https://www.pi.website/research/real_time_chunking
54•pr337h4m•1d ago•7 comments

The Grug Brained Developer (2022)

https://grugbrain.dev/
983•smartmic•1d ago•481 comments

Spherical CNNs (2018)

https://arxiv.org/abs/1801.10130
8•rkp8000•2d ago•1 comments

Reasoning by Superposition: A Perspective on Chain of Continuous Thought

https://arxiv.org/abs/2505.12514
43•danielmorozoff•8h ago•1 comments

Show HN: Free local security checks for AI coding in VSCode, Cursor and Windsurf

21•jaimefjorge•8h ago•11 comments