Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/modules/net.md
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,10 @@ static_routes:
multicast: {igmp_snooping: false, igmp_proxy: false, upstream: wan2}
```

`via` 在某个 LAN 侧网络里的 IPv4 静态路由(主表;例如下级路由器后面的网段):从这个网络发往 `target` 的流量做
masquerade(`route-via-lan:<target>`)。否则回包由那台网关直接回给设备、不经过路由器,路由器只看到单向流量,
conntrack 会把后续包当作 invalid 丢掉。

## 运行时:服务、命令、API

| 名称 | 说明 |
Expand Down
31 changes: 31 additions & 0 deletions mr/mod_net_lanroute_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package main

import (
"net"
"testing"
)

// A static route through another router on the LAN (a downstream router's subnet) masquerades LAN traffic
// to that subnet, so the replies come back through us instead of straight from the gateway.
func TestLANGatewayRouteNAT(t *testing.T) {
c := testConfig(t)
ip, lnet, _ := net.ParseCIDR(c.LAN.IPv4)
gw := ip.To4()
gw = net.IPv4(gw[0], gw[1], gw[2], 254)
c.Routes = []Route{
{Name: "down", Target: "10.77.0.0/24", Via: gw.String()},
{Name: "wan-side", Target: "10.88.0.0/24", Via: "203.0.113.1"},
{Name: "other-table", Target: "10.99.0.0/24", Via: gw.String(), Table: 100},
{Name: "v6", Target: "2001:db8:77::/64", Via: "fe80::1", Dev: c.LAN.Bridge},
}
rule := `iifname "` + c.LAN.Bridge + `" oifname "` + c.LAN.Bridge + `" ip saddr ` + lnet.String() +
` ip daddr 10.77.0.0/24 masquerade comment "route-via-lan:10.77.0.0/24"`
nft := renderNft(c, allExist)
mustContain(t, "nft", nft, rule)
for _, x := range []string{"route-via-lan:10.88.0.0/24", "route-via-lan:10.99.0.0/24", "route-via-lan:2001"} {
mustNotContain(t, "nft", nft, x)
}
if errs := c.Validate(); len(errs) > 0 {
t.Fatalf("config invalid: %v", errs)
}
}
25 changes: 25 additions & 0 deletions mr/mod_net_render.go
Original file line number Diff line number Diff line change
Expand Up @@ -591,6 +591,28 @@ func balanceRule(c *Config, members []lbMember) string {
nftIfnames(c.LANBridges()), lanNets4(c), total, strings.Join(elems, ", "))
}

// lanGatewayRouteNAT: a static IPv4 route (main table) whose gateway is another router on a LAN-side network
// (e.g. a downstream router's subnet). Devices on that network reach the target through us, but the reply
// comes straight back from the gateway, so we only see one direction and conntrack drops the rest as invalid.
// Masquerading that traffic to our address makes the replies come back through us.
func lanGatewayRouteNAT(c *Config) []string {
var out []string
for _, r := range c.Routes {
g := net.ParseIP(r.Via)
_, tn, err := net.ParseCIDR(r.Target)
if r.Table != 0 || g == nil || g.To4() == nil || err != nil || tn.IP.To4() == nil {
continue
}
for _, ln := range c.LANNets() {
if _, lnet, err := net.ParseCIDR(ln.IPv4); err == nil && lnet.Contains(g) {
out = append(out, fmt.Sprintf("iifname %q oifname %q ip saddr %s ip daddr %s masquerade comment \"route-via-lan:%s\"",
ln.Bridge, ln.Bridge, lnet, tn, tn))
}
}
}
return out
}

func netNft(c *Config, hook string, n *Nft) {
switch hook {
case "defs":
Expand All @@ -607,6 +629,9 @@ func netNft(c *Config, hook string, n *Nft) {
for _, r := range nat6Rules(c) {
n.W("%s", r)
}
for _, r := range lanGatewayRouteNAT(c) {
n.W("%s", r)
}
case "forward_first":
for _, r := range policyFallbackRules(c) {
n.W("%s", r)
Expand Down
Loading