From fa626839b1a55dc7ea235cde0fcfe928f7197c2a Mon Sep 17 00:00:00 2001 From: Michael Stapelberg Date: Mon, 28 May 2018 09:53:54 +0200 Subject: [PATCH] Working radvd --- cmd/dhcp4/dhcp4.go | 2 +- cmd/dhcp6/dhcp6.go | 2 +- cmd/netconfigd/netconfigd.go | 15 ++++++++++++--- integrationnetconfig_test.go | 2 +- internal/netconfig/netconfig.go | 25 ++++++++++++++++--------- 5 files changed, 31 insertions(+), 15 deletions(-) diff --git a/cmd/dhcp4/dhcp4.go b/cmd/dhcp4/dhcp4.go index 5158807..b4ddee0 100644 --- a/cmd/dhcp4/dhcp4.go +++ b/cmd/dhcp4/dhcp4.go @@ -1,4 +1,4 @@ -// Binary dhcp4 obtains a DHCPv4 lease, persists its contents to +// Binary dhcp4 obtains a DHCPv4 lease, persists it to // /perm/dhcp4/wire/lease.json and notifies netconfigd. package main diff --git a/cmd/dhcp6/dhcp6.go b/cmd/dhcp6/dhcp6.go index 5e16cbd..c887516 100644 --- a/cmd/dhcp6/dhcp6.go +++ b/cmd/dhcp6/dhcp6.go @@ -1,4 +1,4 @@ -// Binary dhcp6 obtains a DHCPv6 lease, persists its contents to +// Binary dhcp6 obtains a DHCPv6 lease, persists it to // /perm/dhcp6/wire/lease.json and notifies netconfigd. package main diff --git a/cmd/netconfigd/netconfigd.go b/cmd/netconfigd/netconfigd.go index 8fa09ad..1edc91c 100644 --- a/cmd/netconfigd/netconfigd.go +++ b/cmd/netconfigd/netconfigd.go @@ -21,12 +21,21 @@ func logic() error { ch := make(chan os.Signal, 1) signal.Notify(ch, syscall.SIGUSR1) for { - if err := netconfig.Apply("uplink0", "/perm/"); err != nil { + err := netconfig.Apply("uplink0", "/perm/") + // Notify gokrazy about new addresses (netconfig.Apply might have + // modified state before returning an error) so that listeners can be + // updated. + p, _ := os.FindProcess(1) + if err := p.Signal(syscall.SIGHUP); err != nil { + log.Printf("kill -HUP 1: %v", err) + } + if err != nil { return err } - if *linger { - <-ch + if !*linger { + break } + <-ch } return nil } diff --git a/integrationnetconfig_test.go b/integrationnetconfig_test.go index b448570..9f69b13 100644 --- a/integrationnetconfig_test.go +++ b/integrationnetconfig_test.go @@ -114,7 +114,7 @@ func TestNetconfig(t *testing.T) { if !addrRe.MatchString(string(addrs)) { t.Fatalf("regexp %s does not match %s", addrRe, string(addrs)) } - addr6Re := regexp.MustCompile(`(?m)^\s*inet6 2a02:168:4a00::1/48 scope global\s*$`) + 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)) } diff --git a/internal/netconfig/netconfig.go b/internal/netconfig/netconfig.go index 7e93b98..b85bf99 100644 --- a/internal/netconfig/netconfig.go +++ b/internal/netconfig/netconfig.go @@ -6,11 +6,9 @@ import ( "io/ioutil" "log" "net" - "os" "path/filepath" "strconv" "strings" - "syscall" "github.com/vishvananda/netlink" "golang.org/x/sys/unix" @@ -123,6 +121,10 @@ func applyDhcp6(iface, dir string) error { // pick the first address of the prefix, e.g. address 2a02:168:4a00::1 // for prefix 2a02:168:4a00::/48 prefix.IP[len(prefix.IP)-1] = 1 + // Use the first /64 subnet within larger prefixes + if ones, bits := prefix.Mask.Size(); ones < 64 { + prefix.Mask = net.CIDRMask(64, bits) + } addr, err := netlink.ParseAddr(prefix.String()) if err != nil { return err @@ -220,12 +222,23 @@ func applyFirewall() error { func applySysctl() error { if err := ioutil.WriteFile("/proc/sys/net/ipv4/ip_forward", []byte("1"), 0644); err != nil { - return err + return fmt.Errorf("sysctl(net.ipv4.ip_forward=1): %v", err) } + + if err := ioutil.WriteFile("/proc/sys/net/ipv6/conf/all/forwarding", []byte("1"), 0644); err != nil { + return fmt.Errorf("sysctl(net.ipv6.conf.all.forwarding=1): %v", err) + } + + if err := ioutil.WriteFile("/proc/sys/net/ipv6/conf/uplink0/accept_ra", []byte("2"), 0644); err != nil { + return fmt.Errorf("sysctl(net.ipv6.conf.uplink0.accept_ra=2): %v", err) + } + return nil } func Apply(iface, dir string) error { + + // TODO: split into two parts: delay the up until later if err := applyInterfaces(dir); err != nil { return err } @@ -246,11 +259,5 @@ func Apply(iface, dir string) error { return err } - // Notify gokrazy init of new addresses - p, _ := os.FindProcess(1) - if err := p.Signal(syscall.SIGHUP); err != nil { - log.Printf("send SIGHUP to init: %v", err) - } - return nil }