diff --git a/internal/dhcp/dhcp_test.go b/internal/dhcp/dhcp_test.go index 94f1b78..a2d5e60 100644 --- a/internal/dhcp/dhcp_test.go +++ b/internal/dhcp/dhcp_test.go @@ -51,12 +51,15 @@ func TestIncrementIP_Carry(t *testing.T) { func newConf(t *testing.T, cidr string) Config { t.Helper() _, network, _ := net.ParseCIDR(cidr) + _, vpcNet, _ := net.ParseCIDR("10.0.0.0/16") + gw := net.ParseIP("192.168.1.1").To4() return Config{ - Network: network, - Gateway: net.ParseIP("192.168.1.1").To4(), - DefaultRoute: true, - Name: "test", - ConfDir: t.TempDir(), + Network: network, + VPCGateway: gw, + VPCRoute: vpcNet, + DefaultGateway: gw, + Name: "test", + ConfDir: t.TempDir(), } } @@ -85,24 +88,45 @@ func TestGenerateConfig_FilenameMatchesName(t *testing.T) { } } -func TestGenerateConfig_ContainsGateway(t *testing.T) { +func TestGenerateConfig_ContainsDefaultGateway(t *testing.T) { conf := newConf(t, "192.168.1.0/29") path, _, _ := GenerateConfig(conf) content, _ := os.ReadFile(path) if !strings.Contains(string(content), "dhcp-option=3,192.168.1.1") { - t.Errorf("gateway absente du fichier généré :\n%s", content) + t.Errorf("dhcp-option=3 absente du fichier généré :\n%s", content) } } -func TestGenerateConfig_NoDefaultRoute(t *testing.T) { +func TestGenerateConfig_NoDefaultGateway(t *testing.T) { conf := newConf(t, "192.168.1.0/29") - conf.DefaultRoute = false + conf.DefaultGateway = nil path, _, _ := GenerateConfig(conf) content, _ := os.ReadFile(path) if strings.Contains(string(content), "dhcp-option=3,") { - t.Errorf("dhcp-option=3 présente alors que DefaultRoute=false :\n%s", content) + t.Errorf("dhcp-option=3 présente alors que DefaultGateway=nil :\n%s", content) + } +} + +func TestGenerateConfig_ContainsVPCRoute(t *testing.T) { + conf := newConf(t, "192.168.1.0/29") + path, _, _ := GenerateConfig(conf) + content, _ := os.ReadFile(path) + + if !strings.Contains(string(content), "dhcp-option=121,10.0.0.0/16,192.168.1.1") { + t.Errorf("dhcp-option=121 absente ou incorrecte :\n%s", content) + } +} + +func TestGenerateConfig_NoVPCRoute(t *testing.T) { + conf := newConf(t, "192.168.1.0/29") + conf.VPCRoute = nil + path, _, _ := GenerateConfig(conf) + content, _ := os.ReadFile(path) + + if strings.Contains(string(content), "dhcp-option=121,") { + t.Errorf("dhcp-option=121 présente alors que VPCRoute=nil :\n%s", content) } } @@ -110,7 +134,6 @@ func TestGenerateConfig_ContainsDhcpRange(t *testing.T) { _, network, _ := net.ParseCIDR("10.10.0.0/24") conf := Config{ Network: network, - Gateway: net.ParseIP("10.10.0.1").To4(), Name: "vpc1", ConfDir: t.TempDir(), } @@ -156,7 +179,6 @@ func TestGenerateConfig_CreatesConfDir(t *testing.T) { _, network, _ := net.ParseCIDR("10.0.0.0/30") conf := Config{ Network: network, - Gateway: net.ParseIP("10.0.0.1").To4(), Name: "net", ConfDir: dir, } diff --git a/internal/dhcp/generate.go b/internal/dhcp/generate.go index 90608f3..2b6c72f 100644 --- a/internal/dhcp/generate.go +++ b/internal/dhcp/generate.go @@ -14,8 +14,11 @@ func GenerateConfig(c Config) (string, map[string]string, error) { var sb strings.Builder fmt.Fprintf(&sb, "no-resolv\n") fmt.Fprintf(&sb, "dhcp-range=%s,static,%s,12h\n", c.Network.IP.String(), mask) - if c.DefaultRoute { - fmt.Fprintf(&sb, "dhcp-option=3,%s\n", c.Gateway.String()) + if c.VPCRoute != nil { + fmt.Fprintf(&sb, "dhcp-option=121,%s,%s\n", c.VPCRoute.String(), c.VPCGateway.String()) + } + if c.DefaultGateway != nil { + fmt.Fprintf(&sb, "dhcp-option=3,%s\n", c.DefaultGateway.String()) } fmt.Fprintf(&sb, "dhcp-option=6,1.1.1.1,8.8.8.8\n\n") diff --git a/internal/dhcp/struct.go b/internal/dhcp/struct.go index d73e052..316667c 100644 --- a/internal/dhcp/struct.go +++ b/internal/dhcp/struct.go @@ -5,9 +5,10 @@ import ( ) type Config struct { - Network *net.IPNet - Gateway net.IP - DefaultRoute bool - Name string - ConfDir string + Network *net.IPNet + VPCGateway net.IP // next-hop for VPCRoute (option 121) + VPCRoute *net.IPNet // if non-nil, emit dhcp-option=121,VPCRoute,VPCGateway + DefaultGateway net.IP // if non-nil, emit dhcp-option=3,DefaultGateway + Name string + ConfDir string } diff --git a/internal/subnet/create.go b/internal/subnet/create.go index 8607328..a3faf46 100644 --- a/internal/subnet/create.go +++ b/internal/subnet/create.go @@ -136,11 +136,22 @@ func setupVxlanHost(d subnetData, vethE string) error { func startDHCP(db *badger.DB, subnetName string, d subnetData) error { conf := dhcp.Config{ - Network: d.cidr, - Gateway: d.interfaceIP, - DefaultRoute: d.mode == "vxlan", - Name: d.vpc + "_" + d.bridge, - ConfDir: "/etc/dnsmasq.d", + Network: d.cidr, + Name: d.vpc + "_" + d.bridge, + ConfDir: "/etc/dnsmasq.d", + } + switch d.mode { + case "vxlan": + conf.VPCGateway = d.interfaceIP + conf.VPCRoute = d.vpcCIDR + case "bridge": + if d.defaultRoute { + gw, err := netif.GetDefaultGateway() + if err != nil { + return fmt.Errorf("get default gateway: %w", err) + } + conf.DefaultGateway = gw + } } _, entries, err := dhcp.GenerateConfig(conf) if err != nil {