Live data from Hacker News

Grokking NAT and packet mangling in Linux

vivekn.dev

21–30 of 41 posts

Re: Grokking NAT and packet mangling in Linux

#21

Earlier quoted context omitted.

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...

Also, have we all forgotten about Groklaw already?

RIP Groklaw

Re: Grokking NAT and packet mangling in Linux

#23
post #5

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…

> 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.

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.

Re: Grokking NAT and packet mangling in Linux

#25
post #5

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…

> 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.

Re: Grokking NAT and packet mangling in Linux

#27
post #3

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…

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.

Re: Grokking NAT and packet mangling in Linux

#29
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  /dev/null
    net.ipv4.ip_forward=1
    EOF

    sudo sysctl --system

Re: Grokking NAT and packet mangling in Linux

#30
post #5

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…

hi! thanks for explaining this bit in detail. i agree, fragmentation should be handled in the transport layer!
Post reply on HN