|
| 1 | +## nftables for a new Daemon |
| 2 | + |
| 3 | +When the daemon starts, it creates two tables, `ip docker-bridges` and |
| 4 | +`ip6 docker-bridges` for IPv4 and IPv6 rules respectively. Each table contains |
| 5 | +some base chains and empty verdict maps. Rules for the default bridge network |
| 6 | +are then added. |
| 7 | + |
| 8 | + table ip docker-bridges { |
| 9 | + map filter-forward-in-jumps { |
| 10 | + type ifname : verdict |
| 11 | + elements = { "docker0" : jump filter-forward-in__docker0 } |
| 12 | + } |
| 13 | + |
| 14 | + map filter-forward-out-jumps { |
| 15 | + type ifname : verdict |
| 16 | + elements = { "docker0" : jump filter-forward-out__docker0 } |
| 17 | + } |
| 18 | + |
| 19 | + map nat-postrouting-in-jumps { |
| 20 | + type ifname : verdict |
| 21 | + elements = { "docker0" : jump nat-postrouting-in__docker0 } |
| 22 | + } |
| 23 | + |
| 24 | + map nat-postrouting-out-jumps { |
| 25 | + type ifname : verdict |
| 26 | + elements = { "docker0" : jump nat-postrouting-out__docker0 } |
| 27 | + } |
| 28 | + |
| 29 | + chain filter-FORWARD { |
| 30 | + type filter hook forward priority filter; policy accept; |
| 31 | + oifname vmap @filter-forward-in-jumps |
| 32 | + iifname vmap @filter-forward-out-jumps |
| 33 | + } |
| 34 | + |
| 35 | + chain nat-OUTPUT { |
| 36 | + type nat hook output priority -100; policy accept; |
| 37 | + ip daddr != 127.0.0.0/8 fib daddr type local counter jump nat-prerouting-and-output |
| 38 | + } |
| 39 | + |
| 40 | + chain nat-POSTROUTING { |
| 41 | + type nat hook postrouting priority srcnat; policy accept; |
| 42 | + iifname vmap @nat-postrouting-out-jumps |
| 43 | + oifname vmap @nat-postrouting-in-jumps |
| 44 | + } |
| 45 | + |
| 46 | + chain nat-PREROUTING { |
| 47 | + type nat hook prerouting priority dstnat; policy accept; |
| 48 | + fib daddr type local counter jump nat-prerouting-and-output |
| 49 | + } |
| 50 | + |
| 51 | + chain nat-prerouting-and-output { |
| 52 | + } |
| 53 | + |
| 54 | + chain raw-PREROUTING { |
| 55 | + type filter hook prerouting priority raw; policy accept; |
| 56 | + } |
| 57 | + |
| 58 | + chain filter-forward-in__docker0 { |
| 59 | + ct state established,related counter accept |
| 60 | + iifname "docker0" counter accept comment "ICC" |
| 61 | + counter drop comment "UNPUBLISHED PORT DROP" |
| 62 | + } |
| 63 | + |
| 64 | + chain filter-forward-out__docker0 { |
| 65 | + ct state established,related counter accept |
| 66 | + counter accept comment "OUTGOING" |
| 67 | + } |
| 68 | + |
| 69 | + chain nat-postrouting-in__docker0 { |
| 70 | + } |
| 71 | + |
| 72 | + chain nat-postrouting-out__docker0 { |
| 73 | + oifname != "docker0" ip saddr 172.17.0.0/16 counter masquerade comment "MASQUERADE" |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + |
| 78 | +#### filter-FORWARD |
| 79 | + |
| 80 | +Chain `filter-FORWARD` is a base chain, with type `filter` and hook `forward`. |
| 81 | +_So, it's equivalent to the iptables built-in chain `FORWARD` in the `filter` |
| 82 | +table._ It's initialised with two rules that use the output and input |
| 83 | +interface names as keys in verdict maps: |
| 84 | + |
| 85 | + chain filter-FORWARD { |
| 86 | + type filter hook forward priority filter; policy accept; |
| 87 | + oifname vmap @filter-forward-in-jumps |
| 88 | + iifname vmap @filter-forward-out-jumps |
| 89 | + } |
| 90 | + |
| 91 | + |
| 92 | +The verdict maps will be populated with an element per bridge network, each |
| 93 | +jumping to a chain containing rules for that bridge. (So, for packets that |
| 94 | +aren't going to-or-from a Docker bridge device, no jump rules are found in |
| 95 | +the verdict map, and the packets don't need any further processing by this |
| 96 | +base chain.) |
| 97 | + |
| 98 | +The filter-FORWARD chain's policy shown above is `accept`. However: |
| 99 | + |
| 100 | + - For IPv4, the policy is `drop` if the sysctl |
| 101 | + net.ipv4.ip_forward was not set to '1', and the daemon set it itself when |
| 102 | + an IPv4-enabled bridge network was created. |
| 103 | + - For IPv6, similar, but for sysctls "/proc/sys/net/ipv6/conf/default/forwarding" |
| 104 | + and "/proc/sys/net/ipv6/conf/all/forwarding". |
| 105 | + |
| 106 | +#### Per-network filter-FORWARD rules |
| 107 | + |
| 108 | +Chains added for the default bridge network are named after the base chain |
| 109 | +hook they're called from, and the network's bridge. |
| 110 | + |
| 111 | +Packets processed by `filter-forward-in__*` will be delivered to the bridge |
| 112 | +network if accepted. For docker0, the chain is: |
| 113 | + |
| 114 | + chain filter-forward-in__docker0 { |
| 115 | + ct state established,related counter accept |
| 116 | + iifname "docker0" counter accept comment "ICC" |
| 117 | + counter drop comment "UNPUBLISHED PORT DROP" |
| 118 | + } |
| 119 | + |
| 120 | + |
| 121 | +The rules are: |
| 122 | +- conntrack accept for established flows. _Note that accept only applies to the |
| 123 | + base chain, accepted packets may be processed by other base chains registered |
| 124 | + with the same hook._ |
| 125 | +- accept packets originating within the network, because inter-container |
| 126 | + communication (ICC) is enabled. |
| 127 | +- drop any other packets, because no there are no containers in the network |
| 128 | + with published ports. _This means there is no dependency on the filter-FORWARD |
| 129 | + chain's default policy. Even if it is ACCEPT, packets will be dropped unless |
| 130 | + container ports/protocols are published._ |
| 131 | + |
| 132 | +Packets processed by `filter-forward-out__*` originate from the bridge network: |
| 133 | + |
| 134 | + chain filter-forward-out__docker0 { |
| 135 | + ct state established,related counter accept |
| 136 | + counter accept comment "OUTGOING" |
| 137 | + } |
| 138 | + |
| 139 | + |
| 140 | +The rules in docker0's chain are: |
| 141 | +- conntrack accept for established flows. |
| 142 | +- an accept rule, containers in this network have access to external networks. |
| 143 | + |
| 144 | +#### nat-POSTROUTING |
| 145 | + |
| 146 | +Like the filter-FORWARD chain, nat-POSTROUTING has a jump to per-network chains |
| 147 | +for packets to and from the network. |
| 148 | + |
| 149 | + chain nat-POSTROUTING { |
| 150 | + type nat hook postrouting priority srcnat; policy accept; |
| 151 | + iifname vmap @nat-postrouting-out-jumps |
| 152 | + oifname vmap @nat-postrouting-in-jumps |
| 153 | + } |
| 154 | + |
| 155 | + |
| 156 | +#### Per-network nat-POSTROUTING rules |
| 157 | + |
| 158 | +In docker0's nat-postrouting chains, there's a single masquerade rule for packets |
| 159 | +leaving the network: |
| 160 | + |
| 161 | + chain nat-postrouting-in__docker0 { |
| 162 | + } |
| 163 | + |
| 164 | + chain nat-postrouting-out__docker0 { |
| 165 | + oifname != "docker0" ip saddr 172.17.0.0/16 counter masquerade comment "MASQUERADE" |
| 166 | + } |
| 167 | + |
0 commit comments