s6-netdev/thetford-mines.canada/main.go

270 lines
5.8 KiB
Go

package main
import (
"fmt"
"log"
"net"
"net/netip"
"os"
"git.redxen.eu/nnd/s6-netdev"
)
func main() {
var (
err error
l = log.New(os.Stderr, "s6-netdev: ", log.Ltime|log.Lshortfile|log.Lmsgprefix)
ifs = make(s6netdev.IfMap)
)
t := s6netdev.S6NewTree()
// Loopback
ifs.AddIf(&s6netdev.Iface{
Name: "lo",
Type: &s6netdev.NetdevIfTypes.Loopback,
})
// Physical interfaces
for _, v := range []int{9, 12, 13, 14, 15} {
ifs.AddIf(&s6netdev.Iface{
Name: fmt.Sprintf("enp%ds0", v),
Type: &s6netdev.NetdevIfTypes.Phys,
})
}
// VLAN Interfaces
for _, v := range []int{42, 66, 100, 101} {
ifs.AddIf(&s6netdev.Iface{
Name: fmt.Sprintf("phys.%d", v),
Type: &s6netdev.NetdevIfTypes.Vlan,
VlanId: s6netdev.VLAN(v),
})
}
// Bridge interfaces
ifs.AddIf(&s6netdev.Iface{
Name: "phys",
Type: &s6netdev.NetdevIfTypes.Bridge,
MACAddr: net.HardwareAddr{0x52, 0x54, 0x00, 0x81, 0xcb, 0x62},
DHCP: s6netdev.DHCP_IP{V4: true},
Properties: []s6netdev.Property{
{Key: "stp_state", Value: "1", Default: "0"},
{Key: "mcast_snooping", Value: "0", Default: "1"},
},
Sysctls: s6netdev.Sysctl_IP{
V6: []s6netdev.Property{
{Key: "forwarding", Value: "0", Default: "0"},
},
},
})
ifs.AddIf(&s6netdev.Iface{
Name: "br-dn42",
Type: &s6netdev.NetdevIfTypes.Bridge,
Sysctls: s6netdev.Sysctl_IP{
V6: []s6netdev.Property{
{Key: "forwarding", Value: "1", Default: "0"},
},
},
Properties: []s6netdev.Property{
{Key: "stp_state", Value: "0", Default: "0"},
{Key: "mcast_snooping", Value: "0", Default: "1"},
},
})
ifs.AddIf(&s6netdev.Iface{
Name: "b00b",
Type: &s6netdev.NetdevIfTypes.Bridge,
MACAddr: net.HardwareAddr{0x02, 0x00, 0x00, 0x01, 0xb0, 0x0b},
Addresses: []netip.Prefix{
netip.MustParsePrefix("2a04:5b81:2060:b00b::2/64"),
},
Sysctls: s6netdev.Sysctl_IP{
V6: []s6netdev.Property{
{Key: "forwarding", Value: "1", Default: "0"},
{Key: "autoconf", Value: "0", Default: "0"},
},
},
Properties: []s6netdev.Property{
{Key: "stp_state", Value: "0", Default: "0"},
{Key: "mcast_snooping", Value: "0", Default: "1"},
},
})
ifs.AddIf(&s6netdev.Iface{
Name: "f33d",
Type: &s6netdev.NetdevIfTypes.Bridge,
MACAddr: net.HardwareAddr{0x02, 0x00, 0x00, 0x01, 0xf3, 0x3d},
Addresses: []netip.Prefix{
netip.MustParsePrefix("2a04:5b81:2060:f33d::2/64"),
},
Sysctls: s6netdev.Sysctl_IP{
V6: []s6netdev.Property{
{Key: "forwarding", Value: "1", Default: "0"},
{Key: "autoconf", Value: "0", Default: "0"},
},
},
Properties: []s6netdev.Property{
{Key: "stp_state", Value: "0", Default: "0"},
{Key: "mcast_snooping", Value: "0", Default: "1"},
},
})
ifs.AddIf(&s6netdev.Iface{
Name: "d00d",
Type: &s6netdev.NetdevIfTypes.Bridge,
MACAddr: net.HardwareAddr{0x02, 0x00, 0x00, 0x01, 0xd0, 0x0d},
Addresses: []netip.Prefix{
netip.MustParsePrefix("2a04:5b81:2060:d00d::2/64"),
},
Sysctls: s6netdev.Sysctl_IP{
V6: []s6netdev.Property{
{Key: "forwarding", Value: "1", Default: "0"},
{Key: "autoconf", Value: "0", Default: "0"},
},
},
Properties: []s6netdev.Property{
{Key: "stp_state", Value: "0", Default: "0"},
{Key: "mcast_snooping", Value: "0", Default: "1"},
},
})
// VRFs
ifs.AddIf(&s6netdev.Iface{
Name: "vrf-dn42",
Type: &s6netdev.NetdevIfTypes.Vrf,
Table: 20,
Sysctls: s6netdev.Sysctl_IP{
V6: []s6netdev.Property{
{Key: "forwarding", Value: "1", Default: "0"},
},
},
})
ifs.AddIf(&s6netdev.Iface{
Name: "vrf-v6",
Type: &s6netdev.NetdevIfTypes.Vrf,
Table: 10,
Sysctls: s6netdev.Sysctl_IP{
V6: []s6netdev.Property{
{Key: "forwarding", Value: "1", Default: "0"},
},
},
})
// Wireguard interfaces
ifs.AddIf(&s6netdev.Iface{
Name: "vultrbgp",
Type: &s6netdev.NetdevIfTypes.Wireguard,
Addresses: []netip.Prefix{
netip.MustParsePrefix("fe80::2/64"),
},
Sysctls: s6netdev.Sysctl_IP{
V6: []s6netdev.Property{
{Key: "forwarding", Value: "1", Default: "0"},
},
},
})
for _, m := range []struct {
Master string
Slaves []string
}{
{
Master: "vrf-dn42",
Slaves: []string{
"br-dn42",
},
},
{
Master: "vrf-v6",
Slaves: []string{
"vultrbgp",
"b00b",
"f33d",
"d00d",
},
},
{
Master: "phys",
Slaves: []string{
"enp12s0",
},
},
{
Master: "b00b",
Slaves: []string{
"enp9s0",
"phys.66",
},
},
{
Master: "br-dn42",
Slaves: []string{
"enp15s0",
"phys.42",
},
},
{
Master: "f33d",
Slaves: []string{
"enp14s0",
"phys.100",
},
},
{
Master: "d00d",
Slaves: []string{
"enp13s0",
"phys.101",
},
},
} {
master := ifs[m.Master]
for _, slave := range m.Slaves {
master.Slaves = append(master.Slaves, ifs[slave])
}
ifs[m.Master] = master
}
// Parent of VLANs
for _, v := range []int{42, 66, 100, 101} {
ifs[fmt.Sprintf("phys.%d", v)].Parent = ifs["phys"]
}
// Unreachable routes
ifs["lo"].Routes = append(ifs["lo"].Routes, s6netdev.Route{
Type: "unreachable",
Default: true,
IPver: 6,
Vrf: ifs["vrf-v6"],
Metric: 4278198272,
}, s6netdev.Route{
Type: "unreachable",
Default: true,
IPver: 6,
Vrf: ifs["vrf-dn42"],
Metric: 4278198272,
})
// Default router for vrf (defrtr from ra isn't installed if autoconf not enabled)
ifs["b00b"].Routes = append(ifs["b00b"].Routes, s6netdev.Route{
Default: true,
Via: netip.MustParseAddr("fe80::ff:fe00:b00b"), // Link local of lakewood.united-states
Vrf: ifs["vrf-v6"],
Metric: 4096,
})
for _, v := range ifs {
t.Services(*v)
}
for _, v := range t.S6Services() {
if s6netdev.NetdevIsDummy(v.Name) {
continue
}
l.Printf("Commiting %s\n", v.Name)
if err = t.S6CommitService(v); err != nil {
l.Fatalf("Failed to commit %s, %s\n", v.Name, err)
}
}
}