2023-04-17 11:15:37 +02:00
|
|
|
//go:build !android
|
|
|
|
|
2022-09-05 09:06:35 +02:00
|
|
|
package routemanager
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
|
|
|
"net/netip"
|
2022-12-04 13:22:21 +01:00
|
|
|
"os"
|
2023-04-17 11:15:37 +02:00
|
|
|
|
|
|
|
"github.com/vishvananda/netlink"
|
2022-09-05 09:06:35 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
const ipv4ForwardingPath = "/proc/sys/net/ipv4/ip_forward"
|
|
|
|
|
|
|
|
func addToRouteTable(prefix netip.Prefix, addr string) error {
|
|
|
|
_, ipNet, err := net.ParseCIDR(prefix.String())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
addrMask := "/32"
|
|
|
|
if prefix.Addr().Unmap().Is6() {
|
|
|
|
addrMask = "/128"
|
|
|
|
}
|
|
|
|
|
|
|
|
ip, _, err := net.ParseCIDR(addr + addrMask)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
route := &netlink.Route{
|
|
|
|
Scope: netlink.SCOPE_UNIVERSE,
|
|
|
|
Dst: ipNet,
|
|
|
|
Gw: ip,
|
|
|
|
}
|
|
|
|
|
|
|
|
err = netlink.RouteAdd(route)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func removeFromRouteTable(prefix netip.Prefix) error {
|
|
|
|
_, ipNet, err := net.ParseCIDR(prefix.String())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
route := &netlink.Route{
|
|
|
|
Scope: netlink.SCOPE_UNIVERSE,
|
|
|
|
Dst: ipNet,
|
|
|
|
}
|
|
|
|
|
|
|
|
err = netlink.RouteDel(route)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func enableIPForwarding() error {
|
2023-05-18 19:31:54 +02:00
|
|
|
bytes, err := os.ReadFile(ipv4ForwardingPath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// check if it is already enabled
|
|
|
|
// see more: https://github.com/netbirdio/netbird/issues/872
|
|
|
|
if len(bytes) > 0 && bytes[0] == 49 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return os.WriteFile(ipv4ForwardingPath, []byte("1"), 0644)
|
2022-09-05 09:06:35 +02:00
|
|
|
}
|