From 48ddf81147911c35b0e6fa125b2df248a8711984 Mon Sep 17 00:00:00 2001 From: Michael Stapelberg Date: Sun, 3 Jun 2018 20:35:41 +0200 Subject: [PATCH] netconfig: apply IPv6 address to lan0, not uplink0 --- cmd/netconfigd/netconfigd.go | 10 ++++----- integrationnetconfig_test.go | 21 +++++++++++------- internal/netconfig/netconfig.go | 38 ++++++++++++++++++++++----------- 3 files changed, 42 insertions(+), 27 deletions(-) diff --git a/cmd/netconfigd/netconfigd.go b/cmd/netconfigd/netconfigd.go index 1edc91c..292bffc 100644 --- a/cmd/netconfigd/netconfigd.go +++ b/cmd/netconfigd/netconfigd.go @@ -3,16 +3,16 @@ package main import ( "flag" - "fmt" - "io/ioutil" - "log" "os" "os/signal" "syscall" "router7/internal/netconfig" + "router7/internal/teelogger" ) +var log = teelogger.NewConsole() + var ( linger = flag.Bool("linger", true, "linger around after applying the configuration (until killed)") ) @@ -21,7 +21,7 @@ func logic() error { ch := make(chan os.Signal, 1) signal.Notify(ch, syscall.SIGUSR1) for { - err := netconfig.Apply("uplink0", "/perm/") + err := netconfig.Apply("/perm/") // Notify gokrazy about new addresses (netconfig.Apply might have // modified state before returning an error) so that listeners can be // updated. @@ -43,8 +43,6 @@ func logic() error { func main() { flag.Parse() if err := logic(); err != nil { - // TODO: use a logger which writes to /dev/console - ioutil.WriteFile("/dev/console", []byte(fmt.Sprintf("netconfig: %v\n", err)), 0600) log.Fatal(err) } } diff --git a/integrationnetconfig_test.go b/integrationnetconfig_test.go index e7ff1ca..0b50d43 100644 --- a/integrationnetconfig_test.go +++ b/integrationnetconfig_test.go @@ -18,7 +18,7 @@ const goldenInterfaces = ` "interfaces":[ { "hardware_addr": "02:73:53:00:ca:fe", - "name": "dummy23" + "name": "uplink0" } ] } @@ -73,7 +73,7 @@ func TestNetconfig(t *testing.T) { } } - if err := netconfig.Apply("dummy23", tmp); err != nil { + if err := netconfig.Apply(tmp); err != nil { t.Fatalf("netconfig.Apply: %v", err) } @@ -88,7 +88,7 @@ func TestNetconfig(t *testing.T) { nsSetup := []*exec.Cmd{ exec.Command("ip", "netns", "exec", ns, "ip", "link", "add", "dummy0", "type", "dummy"), - exec.Command("ip", "netns", "exec", ns, "ip", "link", "add", "uplink0", "type", "dummy"), + exec.Command("ip", "netns", "exec", ns, "ip", "link", "add", "lan0", "type", "dummy"), exec.Command("ip", "netns", "exec", ns, "ip", "link", "set", "dummy0", "address", "02:73:53:00:ca:fe"), } @@ -106,18 +106,23 @@ func TestNetconfig(t *testing.T) { t.Fatal(err) } - addrs, err := exec.Command("ip", "netns", "exec", ns, "ip", "address", "show", "dev", "dummy23").Output() + addrs, err := exec.Command("ip", "netns", "exec", ns, "ip", "address", "show", "dev", "uplink0").Output() if err != nil { t.Fatal(err) } - addrRe := regexp.MustCompile(`(?m)^\s*inet 85.195.207.62/25 brd 85.195.207.127 scope global dummy23$`) + addrRe := regexp.MustCompile(`(?m)^\s*inet 85.195.207.62/25 brd 85.195.207.127 scope global uplink0$`) if !addrRe.MatchString(string(addrs)) { t.Fatalf("regexp %s does not match %s", addrRe, string(addrs)) } + + addrsLan, err := exec.Command("ip", "netns", "exec", ns, "ip", "address", "show", "dev", "lan0").Output() + if err != nil { + t.Fatal(err) + } addr6Re := regexp.MustCompile(`(?m)^\s*inet6 2a02:168:4a00::1/64 scope global\s*$`) - if !addr6Re.MatchString(string(addrs)) { - t.Fatalf("regexp %s does not match %s", addr6Re, string(addrs)) + if !addr6Re.MatchString(string(addrsLan)) { + t.Fatalf("regexp %s does not match %s", addr6Re, string(addrsLan)) } wantRoutes := []string{ @@ -126,7 +131,7 @@ func TestNetconfig(t *testing.T) { "85.195.207.1 proto dhcp scope link src 85.195.207.62", } - out, err := exec.Command("ip", "netns", "exec", ns, "ip", "route", "show", "dev", "dummy23").Output() + out, err := exec.Command("ip", "netns", "exec", ns, "ip", "route", "show", "dev", "uplink0").Output() if err != nil { t.Fatal(err) } diff --git a/internal/netconfig/netconfig.go b/internal/netconfig/netconfig.go index da65793..33403b0 100644 --- a/internal/netconfig/netconfig.go +++ b/internal/netconfig/netconfig.go @@ -6,6 +6,7 @@ import ( "io/ioutil" "log" "net" + "os" "path/filepath" "strconv" "strings" @@ -34,7 +35,7 @@ func subnetMaskSize(mask string) (int, error) { return ones, nil } -func applyDhcp4(iface, dir string) error { +func applyDhcp4(dir string) error { b, err := ioutil.ReadFile(filepath.Join(dir, "dhcp4/wire/lease.json")) if err != nil { if os.IsNotExist(err) { @@ -47,7 +48,7 @@ func applyDhcp4(iface, dir string) error { return err } - link, err := netlink.LinkByName(iface) + link, err := netlink.LinkByName("uplink0") if err != nil { return err } @@ -105,7 +106,7 @@ func applyDhcp4(iface, dir string) error { return nil } -func applyDhcp6(iface, dir string) error { +func applyDhcp6(dir string) error { b, err := ioutil.ReadFile(filepath.Join(dir, "dhcp6/wire/lease.json")) if err != nil { if os.IsNotExist(err) { @@ -118,7 +119,7 @@ func applyDhcp6(iface, dir string) error { return err } - link, err := netlink.LinkByName(iface) + link, err := netlink.LinkByName("lan0") if err != nil { return err } @@ -169,8 +170,17 @@ func applyInterfaces(dir string) error { links, err := netlink.LinkList() for _, l := range links { attr := l.Attrs() - details, ok := byHardwareAddr[attr.HardwareAddr.String()] + // TODO: prefix log line with details about the interface. + // link &{LinkAttrs:{Index:2 MTU:1500 TxQLen:1000 Name:eth0 HardwareAddr:00:0d:b9:49:70:18 Flags:broadcast|multicast RawFlags:4098 ParentIndex:0 MasterIndex:0 Namespace: Alias: Statistics:0xc4200f45f8 Promisc:0 Xdp:0xc4200ca180 EncapType:ether Protinfo: OperState:down NetNsID:0 NumTxQueues:0 NumRxQueues:0 Vfs:[]}}, attr &{Index:2 MTU:1500 TxQLen:1000 Name:eth0 HardwareAddr:00:0d:b9:49:70:18 Flags:broadcast|multicast RawFlags:4098 ParentIndex:0 MasterIndex:0 Namespace: Alias: Statistics:0xc4200f45f8 Promisc:0 Xdp:0xc4200ca180 EncapType:ether Protinfo: OperState:down NetNsID:0 NumTxQueues:0 NumRxQueues:0 Vfs:[]} + + addr := attr.HardwareAddr.String() + details, ok := byHardwareAddr[addr] if !ok { + if addr == "" { + continue // not a configurable interface (e.g. sit0) + } + log.Printf("no config for hardwareattr %s", addr) + ioutil.WriteFile("/dev/console", []byte(fmt.Sprintf("no config for hardwareattr %s\n", addr)), 0600) continue } log.Printf("apply details %+v", details) @@ -227,6 +237,8 @@ func applyFirewall() error { } func applySysctl() error { + // TODO: increase NAT table size + // TODO: increase keepalive to 7200(?) if err := ioutil.WriteFile("/proc/sys/net/ipv4/ip_forward", []byte("1"), 0644); err != nil { return fmt.Errorf("sysctl(net.ipv4.ip_forward=1): %v", err) } @@ -242,36 +254,36 @@ func applySysctl() error { return nil } -func Apply(iface, dir string) error { +func Apply(dir string) error { // TODO: split into two parts: delay the up until later if err := applyInterfaces(dir); err != nil { - return err + return fmt.Errorf("interfaces: %v", err) } var firstErr error - if err := applyDhcp4(iface, dir); err != nil { + if err := applyDhcp4(dir); err != nil { log.Printf("cannot apply dhcp4 lease: %v", err) - firstErr = err + firstErr = fmt.Errorf("dhcp4: %v", err) } - if err := applyDhcp6(iface, dir); err != nil { + if err := applyDhcp6(dir); err != nil { log.Printf("cannot apply dhcp6 lease: %v", err) if firstErr == nil { - firstErr = err + firstErr = fmt.Errorf("dhcp6: %v", err) } } if err := applySysctl(); err != nil { log.Printf("cannot apply sysctl config: %v", err) if firstErr == nil { - firstErr = err + firstErr = fmt.Errorf("sysctl: %v", err) } } if err := applyFirewall(); err != nil { - return err + return fmt.Errorf("firewall: %v", err) } return firstErr