Skip to content

Commit

Permalink
Support IPv6
Browse files Browse the repository at this point in the history
  • Loading branch information
jschwinger233 committed Jan 6, 2024
1 parent a9fc8f9 commit 9600eaa
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 3 deletions.
37 changes: 34 additions & 3 deletions control/netns_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,35 @@ func setupIndieNetns() (err error) {
if err = netlink.LinkSetUp(dae0); err != nil {
return fmt.Errorf("Failed to set link dae0 up: %v", err)
}
// sysctl net.ipv4.conf.{dae0,all}.rp_filter=0
// sysctl net.ipv4.conf.dae0.rp_filter=0
if err = SetRpFilter("dae0", "0"); err != nil {
return fmt.Errorf("Failed to set rp_filter for dae0: %v", err)
}
// sysctl net.ipv4.conf.all.rp_filter=0
if err = SetRpFilter("all", "0"); err != nil {
return fmt.Errorf("Failed to set rp_filter for all: %v", err)
}
// sysctl net.ipv4.conf.dae0.arp_filter=0
if err = SetArpFilter("dae0", "0"); err != nil {
return fmt.Errorf("Failed to set arp_filter for dae0: %v", err)
}
// sysctl net.ipv4.conf.all.arp_filter=0
if err = SetArpFilter("all", "0"); err != nil {
return fmt.Errorf("Failed to set arp_filter for all: %v", err)
}
// sysctl net.ipv6.conf.dae0.disable_ipv6=0
if err = SetDisableIpv6("dae0", "0"); err != nil {
return fmt.Errorf("Failed to set disable_ipv6 for dae0: %v", err)
}
// ip -6 a a fe80::ecee:eeff:feee:eeee dev dae0 scope link
if err = netlink.AddrAdd(dae0, &netlink.Addr{
IPNet: &net.IPNet{
IP: net.ParseIP("fe80::ecee:eeff:feee:eeee"),
Mask: net.CIDRMask(128, 128),
},
}); err != nil {
return fmt.Errorf("Failed to add v6 addr to dae0: %v", err)
}
// ip l s dae0peer netns daens
if err = netlink.LinkSetNsFd(dae0peer, int(indieNetns)); err != nil {
return fmt.Errorf("Failed to move dae0peer to daens: %v", err)
Expand All @@ -117,16 +139,25 @@ func setupIndieNetns() (err error) {
return fmt.Errorf("Failed to parse ip: %v", err)
}
if err = netlink.AddrAdd(dae0peer, &netlink.Addr{IPNet: ipNet}); err != nil {
return fmt.Errorf("Failed to add addr to dae0peer: %v", err)
return fmt.Errorf("Failed to add v4 addr to dae0peer: %v", err)
}
// (ip net e daens) ip r a default dev dae0peer
if err = netlink.RouteAdd(&netlink.Route{
LinkIndex: dae0peer.Attrs().Index,
Dst: &net.IPNet{IP: net.IPv4(0, 0, 0, 0), Mask: net.CIDRMask(0, 32)},
Gw: nil,
}); err != nil {
return fmt.Errorf("Failed to add route to dae0peer: %v", err)
return fmt.Errorf("Failed to add v4 route to dae0peer: %v", err)
}
// (ip net e daens) ip -6 r a default via fe80::ecee:eeff:feee:eeee dev dae0peer
if err = netlink.RouteAdd(&netlink.Route{
LinkIndex: dae0peer.Attrs().Index,
Dst: &net.IPNet{IP: net.IPv6zero, Mask: net.CIDRMask(0, 128)},
Gw: net.ParseIP("fe80::ecee:eeff:feee:eeee"),
}); err != nil {
return fmt.Errorf("Failed to add v6 route to dae0peer: %v", err)
}

return
}

Expand Down
4 changes: 4 additions & 0 deletions control/udp.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ func sendPkt(data []byte, from netip.AddrPort, realTo, to netip.AddrPort, lConn

uConn, _, err := DefaultAnyfromPool.GetOrCreate(from.String(), AnyfromTimeout)
if err != nil && errors.Is(err, syscall.EADDRINUSE) {
logrus.WithField("from", from).
WithField("to", to).
WithField("realTo", realTo).
Trace("Port in use, fallback to use netns.")
err = WithIndieNetns(func() (err error) {
uConn, _, err = DefaultAnyfromPool.GetOrCreate(from.String(), AnyfromTimeout)
return err
Expand Down
8 changes: 8 additions & 0 deletions control/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,14 @@ func SetRpFilter(ifname, val string) error {
return os.WriteFile(fmt.Sprintf("/proc/sys/net/ipv4/conf/%s/rp_filter", ifname), []byte(val), 0644)
}

func SetArpFilter(ifname, val string) error {
return os.WriteFile(fmt.Sprintf("/proc/sys/net/ipv4/conf/%s/arp_filter", ifname), []byte(val), 0644)
}

func SetDisableIpv6(ifname, val string) error {
return os.WriteFile(fmt.Sprintf("/proc/sys/net/ipv6/conf/%s/disable_ipv6", ifname), []byte(val), 0644)
}

func checkSendRedirects(ifname string, ipversion consts.IpVersionStr) error {
path := fmt.Sprintf("/proc/sys/net/ipv%v/conf/%v/send_redirects", ipversion, ifname)
b, err := os.ReadFile(path)
Expand Down

0 comments on commit 9600eaa

Please sign in to comment.