allow configuring extra addresses on interfaces

Useful when you need IPv6 and IPv4 addresses on a WireGuard tunnel.
This commit is contained in:
Michael Stapelberg 2022-06-06 13:58:57 +02:00
parent 40f8eb5b1b
commit f52deeed03
2 changed files with 29 additions and 5 deletions

View File

@ -50,7 +50,10 @@ const goldenInterfaces = `
}, },
{ {
"name": "wg0", "name": "wg0",
"addr": "fe80::1/64" "addr": "fe80::1/64",
"extra_addrs": [
"10.22.100.1/24"
]
} }
] ]
} }
@ -461,6 +464,12 @@ peer: AVU3LodtnFaFnJmMyNNW7cUk4462lqnVULTFkjWYvRo=
if !upRe.MatchString(string(out)) { if !upRe.MatchString(string(out)) {
t.Errorf("regexp %s does not match %s", upRe, string(out)) t.Errorf("regexp %s does not match %s", upRe, string(out))
} }
addr4Re := regexp.MustCompile(`(?m)^\s*inet 10.22.100.1/24 brd 10.22.100.255 scope global wg0\s*$`)
if !addr4Re.MatchString(string(out)) {
t.Errorf("regexp %s does not match %s", addr4Re, string(out))
}
addr6Re := regexp.MustCompile(`(?m)^\s*inet6 fe80::1/64 scope link\s*$`) addr6Re := regexp.MustCompile(`(?m)^\s*inet6 fe80::1/64 scope link\s*$`)
if !addr6Re.MatchString(string(out)) { if !addr6Re.MatchString(string(out)) {
t.Errorf("regexp %s does not match %s", addr6Re, string(out)) t.Errorf("regexp %s does not match %s", addr6Re, string(out))

View File

@ -192,10 +192,11 @@ func applyDhcp6(dir string) error {
} }
type InterfaceDetails struct { type InterfaceDetails struct {
HardwareAddr string `json:"hardware_addr"` // e.g. dc:9b:9c:ee:72:fd HardwareAddr string `json:"hardware_addr"` // e.g. dc:9b:9c:ee:72:fd
SpoofHardwareAddr string `json:"spoof_hardware_addr"` // e.g. dc:9b:9c:ee:72:fd SpoofHardwareAddr string `json:"spoof_hardware_addr"` // e.g. dc:9b:9c:ee:72:fd
Name string `json:"name"` // e.g. uplink0, or lan0 Name string `json:"name"` // e.g. uplink0, or lan0
Addr string `json:"addr"` // e.g. 192.168.42.1/24 Addr string `json:"addr"` // e.g. 192.168.42.1/24
ExtraAddrs []string `json:"extra_addrs"` // e.g. ["192.168.23.1/24"]
} }
type BridgeDetails struct { type BridgeDetails struct {
@ -399,6 +400,20 @@ func applyInterfaces(dir, root string) error {
} }
} }
} }
for _, addr := range details.ExtraAddrs {
addr, err := netlink.ParseAddr(addr)
if err != nil {
return fmt.Errorf("ParseAddr(%q): %v", addr, err)
}
if err := netlink.AddrReplace(l, addr); err != nil {
return fmt.Errorf("AddrReplace(%s, %v): %v", attr.Name, addr, err)
}
}
// TODO: allow static route configuration (ExtraRoutes)
// 2a02:168:4a00:22::/64 via fe80::2 dev wg0
} }
return nil return nil
} }