mirror of
https://github.com/netbirdio/netbird.git
synced 2024-12-24 23:59:25 +01:00
d2d5d4b4b9
Removed ioctl code and remove exception from lint action
74 lines
1.2 KiB
Go
74 lines
1.2 KiB
Go
package routemanager
|
|
|
|
import (
|
|
"github.com/vishvananda/netlink"
|
|
"net"
|
|
"net/netip"
|
|
"os"
|
|
)
|
|
|
|
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 {
|
|
err := os.WriteFile(ipv4ForwardingPath, []byte("1"), 0644)
|
|
return err
|
|
}
|
|
|
|
func isNetForwardHistoryEnabled() bool {
|
|
out, err := os.ReadFile(ipv4ForwardingPath)
|
|
if err != nil {
|
|
// todo
|
|
panic(err)
|
|
}
|
|
return string(out) == "1"
|
|
}
|