[PR #2808] [MERGED] [client] Use the prerouting chain to mark for masquerading to support older systems #18294

Open
opened 2026-08-05 04:08:21 -04:00 by saavagebueno · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/netbirdio/netbird/pull/2808
Author: @lixmal
Created: 10/29/2024
Status: Merged
Merged: 11/7/2024
Merged by: @lixmal

Base: mainHead: use-prerouting-for-masq


📝 Commits (3)

  • 4441ec6 Use the prerouting chain to mark for masquerading to support older systems
  • 0446a03 Use constants
  • 9797e31 Fix lint

📊 Changes

8 files changed (+455 additions, -270 deletions)

View changed files

📝 client/firewall/iptables/acl_linux.go (+2 -2)
📝 client/firewall/iptables/router_linux.go (+116 -53)
📝 client/firewall/iptables/router_linux_test.go (+95 -67)
📝 client/firewall/manager/firewall.go (+1 -0)
📝 client/firewall/nftables/acl_linux.go (+2 -2)
📝 client/firewall/nftables/router_linux.go (+132 -28)
📝 client/firewall/nftables/router_linux_test.go (+102 -116)
📝 util/net/net.go (+5 -2)

📄 Description

Describe your changes

Replace dynamic postrouting rules with input interface with dynamic prerouting rules + static postrouting rules.
Older versions, such as Ubuntu 20.04, don't support matching on the input interface in the postrouting chain. As a result the masquerade rules didn't match at all.

Following an example with route 10.20.1.0/24

Before:

nftables:

table ip netbird {
        chain netbird-rt-postrouting {
                type nat hook postrouting priority srcnat - 1; policy accept;
                iifname "wt0" oifname != "lo" ip daddr 10.20.1.0/24 counter packets 0 bytes 0 masquerade
                oifname "wt0" iifname != "lo" ip saddr 10.20.1.0/24 counter packets 0 bytes 0 masquerade
        }
}

iptables:

-A POSTROUTING -j NETBIRD-RT-NAT
-A NETBIRD-RT-NAT -d 10.20.1.0/24 -i wt0 ! -o lo -j MASQUERADE
-A NETBIRD-RT-NAT -s 10.20.1.0/24 ! -i lo -o wt0 -j MASQUERADE

After:

nftables:

table ip netbird {
        chain netbird-rt-postrouting {
                type nat hook postrouting priority srcnat - 1; policy accept;
                meta mark 0x0001bd11 oifname != "lo" counter packets 0 bytes 0 masquerade
                meta mark 0x0001bd12 oifname "wt0" counter packets 0 bytes 0 masquerade
        }

        chain netbird-rt-prerouting {
                type filter hook prerouting priority mangle; policy accept;
                ct state new iifname "wt0" ip daddr 10.20.1.0/24 meta mark set 0x0001bd11
                ct state new iifname != "wt0" ip saddr 10.20.1.0/24 meta mark set 0x0001bd12
        }
}

iptables:


*mangle
-A PREROUTING -j NETBIRD-RT-PRE
-A NETBIRD-RT-PRE -d 10.20.1.0/24 -i wt0 -m conntrack --ctstate NEW -j MARK --set-xmark 0x1bd11/0xffffffff
-A NETBIRD-RT-PRE -s 10.20.1.0/24 ! -i wt0 -m conntrack --ctstate NEW -j MARK --set-xmark 0x1bd12/0xffffffff

*nat
-A POSTROUTING -j NETBIRD-RT-NAT
-A NETBIRD-RT-NAT ! -o lo -m mark --mark 0x1bd11 -j MASQUERADE
-A NETBIRD-RT-NAT -o wt0 -m mark --mark 0x1bd12 -j MASQUERADE

#2752

Checklist

  • Is it a bug fix
  • Is a typo/documentation fix
  • Is a feature enhancement
  • It is a refactor
  • Created tests that fail without the change (if possible)
  • Extended the README / documentation, if necessary

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/netbirdio/netbird/pull/2808 **Author:** [@lixmal](https://github.com/lixmal) **Created:** 10/29/2024 **Status:** ✅ Merged **Merged:** 11/7/2024 **Merged by:** [@lixmal](https://github.com/lixmal) **Base:** `main` ← **Head:** `use-prerouting-for-masq` --- ### 📝 Commits (3) - [`4441ec6`](https://github.com/netbirdio/netbird/commit/4441ec6dc1aeb4590436ac66be8ca5e902c20856) Use the prerouting chain to mark for masquerading to support older systems - [`0446a03`](https://github.com/netbirdio/netbird/commit/0446a036afde5b66aa0997229cf907c0136ed4ce) Use constants - [`9797e31`](https://github.com/netbirdio/netbird/commit/9797e318107f1df3639b2984b0a329d4266e8cdd) Fix lint ### 📊 Changes **8 files changed** (+455 additions, -270 deletions) <details> <summary>View changed files</summary> 📝 `client/firewall/iptables/acl_linux.go` (+2 -2) 📝 `client/firewall/iptables/router_linux.go` (+116 -53) 📝 `client/firewall/iptables/router_linux_test.go` (+95 -67) 📝 `client/firewall/manager/firewall.go` (+1 -0) 📝 `client/firewall/nftables/acl_linux.go` (+2 -2) 📝 `client/firewall/nftables/router_linux.go` (+132 -28) 📝 `client/firewall/nftables/router_linux_test.go` (+102 -116) 📝 `util/net/net.go` (+5 -2) </details> ### 📄 Description ## Describe your changes Replace dynamic postrouting rules with input interface with dynamic prerouting rules + static postrouting rules. Older versions, such as Ubuntu 20.04, don't support matching on the input interface in the postrouting chain. As a result the masquerade rules didn't match at all. Following an example with route `10.20.1.0/24` ### Before: #### nftables: ``` table ip netbird { chain netbird-rt-postrouting { type nat hook postrouting priority srcnat - 1; policy accept; iifname "wt0" oifname != "lo" ip daddr 10.20.1.0/24 counter packets 0 bytes 0 masquerade oifname "wt0" iifname != "lo" ip saddr 10.20.1.0/24 counter packets 0 bytes 0 masquerade } } ``` #### iptables: ``` -A POSTROUTING -j NETBIRD-RT-NAT -A NETBIRD-RT-NAT -d 10.20.1.0/24 -i wt0 ! -o lo -j MASQUERADE -A NETBIRD-RT-NAT -s 10.20.1.0/24 ! -i lo -o wt0 -j MASQUERADE ``` ### After: #### nftables: ``` table ip netbird { chain netbird-rt-postrouting { type nat hook postrouting priority srcnat - 1; policy accept; meta mark 0x0001bd11 oifname != "lo" counter packets 0 bytes 0 masquerade meta mark 0x0001bd12 oifname "wt0" counter packets 0 bytes 0 masquerade } chain netbird-rt-prerouting { type filter hook prerouting priority mangle; policy accept; ct state new iifname "wt0" ip daddr 10.20.1.0/24 meta mark set 0x0001bd11 ct state new iifname != "wt0" ip saddr 10.20.1.0/24 meta mark set 0x0001bd12 } } ``` #### iptables: ``` *mangle -A PREROUTING -j NETBIRD-RT-PRE -A NETBIRD-RT-PRE -d 10.20.1.0/24 -i wt0 -m conntrack --ctstate NEW -j MARK --set-xmark 0x1bd11/0xffffffff -A NETBIRD-RT-PRE -s 10.20.1.0/24 ! -i wt0 -m conntrack --ctstate NEW -j MARK --set-xmark 0x1bd12/0xffffffff *nat -A POSTROUTING -j NETBIRD-RT-NAT -A NETBIRD-RT-NAT ! -o lo -m mark --mark 0x1bd11 -j MASQUERADE -A NETBIRD-RT-NAT -o wt0 -m mark --mark 0x1bd12 -j MASQUERADE ``` ## Issue ticket number and link #2752 ### Checklist - [x] Is it a bug fix - [ ] Is a typo/documentation fix - [ ] Is a feature enhancement - [ ] It is a refactor - [ ] Created tests that fail without the change (if possible) - [ ] Extended the README / documentation, if necessary --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
saavagebueno added the pull-request label 2026-08-05 04:08:21 -04:00
Sign in to join this conversation.
No Label pull-request
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: DYNR/netbird#18294