moby / moby/libnetwork

Strange iptables nat MASQUERADE rules

Open
#2,422 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Go
Stars
2.2k
Forks
875
PR merge metrics
No merged PRs in 30d

Description

libnetwork is making some really strange MASQUERADE iptables rules in the nat POSTROUTING table that I can't make any sense of when publishing ports in docker. For example after publishing 34197 udp and 27015 tcp the following rules end up into the iptables:

# iptables -t nat -L -v
...
Chain POSTROUTING (policy ACCEPT 258 packets, 15578 bytes)
 pkts bytes target     prot opt in     out     source               destination
 2806 88291 MASQUERADE  all  --  any    !docker0  172.17.0.0/16        anywhere
    0     0 MASQUERADE  udp  --  any    any     172.17.0.2           172.17.0.2           udp dpt:34197
    0     0 MASQUERADE  tcp  --  any    any     172.17.0.2           172.17.0.2           tcp dpt:27015
...

The first rule is for traffic from the container(s) to the outside and makes sense. But the two other rules supposedly masquerade traffic going from the container's internal IP to the published port on container's internal IP via the host's address on that interface (usually 172.17.0.1). As I understand iptables the nat POSTROUTING table is never consulted for packets with a local destination, meaning these rules cannot possibly be hit. Is there some kind of networking voodoo here I'm not getting?

The code that does this (as far as I can tell) is line 273-283 in iptables.go and was added by @porjo in https://github.com/moby/moby/commit/0da92633b4161ed1f8babe5ec4a9fe98257d34b5#diff-ba8c3ab87579147ddeff26dd29c70f44R149 only described as "Create tests for pkg/iptables" in the commit message. It's part of the Move per-container forward rules to DOCKER chain#7003 pull request in Moby. But there doesn't appear to be any explanation of why these rules were added in that pull request discussion. The only thing I can think of is that they were supposed to be some kind of outbound port mapping, but they are just far to broken for that.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in iptables/iptables.go at lines 273-283, then read the linked Moby commit and pull request for the rule's history. Trace when the POSTROUTING MASQUERADE rules are installed and determine whether they can match published-port traffic. Done means a documented explanation of their purpose and, if they are invalid, a clearly scoped fix or removal plan.

Written by the indexing model from the issue text.

Assessment

Tech stack
go
Domain
networking
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.