Working radvd

This commit is contained in:
Michael Stapelberg 2018-05-28 09:53:54 +02:00
parent 6b9ce5728a
commit fa626839b1
5 changed files with 31 additions and 15 deletions

View File

@ -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. // /perm/dhcp4/wire/lease.json and notifies netconfigd.
package main package main

View File

@ -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. // /perm/dhcp6/wire/lease.json and notifies netconfigd.
package main package main

View File

@ -21,12 +21,21 @@ func logic() error {
ch := make(chan os.Signal, 1) ch := make(chan os.Signal, 1)
signal.Notify(ch, syscall.SIGUSR1) signal.Notify(ch, syscall.SIGUSR1)
for { 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 return err
} }
if *linger { if !*linger {
<-ch break
} }
<-ch
} }
return nil return nil
} }

View File

@ -114,7 +114,7 @@ func TestNetconfig(t *testing.T) {
if !addrRe.MatchString(string(addrs)) { if !addrRe.MatchString(string(addrs)) {
t.Fatalf("regexp %s does not match %s", addrRe, 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)) { if !addr6Re.MatchString(string(addrs)) {
t.Fatalf("regexp %s does not match %s", addr6Re, string(addrs)) t.Fatalf("regexp %s does not match %s", addr6Re, string(addrs))
} }

View File

@ -6,11 +6,9 @@ import (
"io/ioutil" "io/ioutil"
"log" "log"
"net" "net"
"os"
"path/filepath" "path/filepath"
"strconv" "strconv"
"strings" "strings"
"syscall"
"github.com/vishvananda/netlink" "github.com/vishvananda/netlink"
"golang.org/x/sys/unix" "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 // pick the first address of the prefix, e.g. address 2a02:168:4a00::1
// for prefix 2a02:168:4a00::/48 // for prefix 2a02:168:4a00::/48
prefix.IP[len(prefix.IP)-1] = 1 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()) addr, err := netlink.ParseAddr(prefix.String())
if err != nil { if err != nil {
return err return err
@ -220,12 +222,23 @@ func applyFirewall() error {
func applySysctl() error { func applySysctl() error {
if err := ioutil.WriteFile("/proc/sys/net/ipv4/ip_forward", []byte("1"), 0644); err != nil { 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 return nil
} }
func Apply(iface, dir string) error { func Apply(iface, dir string) error {
// TODO: split into two parts: delay the up until later
if err := applyInterfaces(dir); err != nil { if err := applyInterfaces(dir); err != nil {
return err return err
} }
@ -246,11 +259,5 @@ func Apply(iface, dir string) error {
return err 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 return nil
} }