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-06-09 18:30:36 +02:00
|
|
|
"syscall"
|
2023-06-09 18:36:49 +02:00
|
|
|
"unsafe"
|
2023-04-17 11:15:37 +02:00
|
|
|
|
|
|
|
"github.com/vishvananda/netlink"
|
2022-09-05 09:06:35 +02:00
|
|
|
)
|
|
|
|
|
2023-06-09 18:36:49 +02:00
|
|
|
// Pulled from http://man7.org/linux/man-pages/man7/rtnetlink.7.html
|
|
|
|
// See the section on RTM_NEWROUTE, specifically 'struct rtmsg'.
|
|
|
|
type routeInfoInMemory struct {
|
|
|
|
Family byte
|
|
|
|
DstLen byte
|
|
|
|
SrcLen byte
|
|
|
|
TOS byte
|
|
|
|
|
|
|
|
Table byte
|
|
|
|
Protocol byte
|
|
|
|
Scope byte
|
|
|
|
Type byte
|
|
|
|
|
|
|
|
Flags uint32
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-11-24 11:31:22 +01:00
|
|
|
func removeFromRouteTable(prefix netip.Prefix, addr string) error {
|
2022-09-05 09:06:35 +02:00
|
|
|
_, ipNet, err := net.ParseCIDR(prefix.String())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-11-24 11:31:22 +01:00
|
|
|
addrMask := "/32"
|
|
|
|
if prefix.Addr().Unmap().Is6() {
|
|
|
|
addrMask = "/128"
|
|
|
|
}
|
|
|
|
|
|
|
|
ip, _, err := net.ParseCIDR(addr + addrMask)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-09-05 09:06:35 +02:00
|
|
|
route := &netlink.Route{
|
|
|
|
Scope: netlink.SCOPE_UNIVERSE,
|
|
|
|
Dst: ipNet,
|
2023-11-24 11:31:22 +01:00
|
|
|
Gw: ip,
|
2022-09-05 09:06:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
err = netlink.RouteDel(route)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-11-24 11:31:22 +01:00
|
|
|
func getRoutesFromTable() ([]netip.Prefix, error) {
|
2023-06-09 18:27:09 +02:00
|
|
|
tab, err := syscall.NetlinkRIB(syscall.RTM_GETROUTE, syscall.AF_UNSPEC)
|
|
|
|
if err != nil {
|
2023-11-24 11:31:22 +01:00
|
|
|
return nil, err
|
2023-06-09 18:27:09 +02:00
|
|
|
}
|
|
|
|
msgs, err := syscall.ParseNetlinkMessage(tab)
|
|
|
|
if err != nil {
|
2023-11-24 11:31:22 +01:00
|
|
|
return nil, err
|
2023-06-09 18:27:09 +02:00
|
|
|
}
|
2023-11-24 11:31:22 +01:00
|
|
|
var prefixList []netip.Prefix
|
2023-06-09 18:27:09 +02:00
|
|
|
loop:
|
|
|
|
for _, m := range msgs {
|
|
|
|
switch m.Header.Type {
|
|
|
|
case syscall.NLMSG_DONE:
|
|
|
|
break loop
|
|
|
|
case syscall.RTM_NEWROUTE:
|
|
|
|
rt := (*routeInfoInMemory)(unsafe.Pointer(&m.Data[0]))
|
2023-12-04 13:34:06 +01:00
|
|
|
msg := m
|
|
|
|
attrs, err := syscall.ParseNetlinkRouteAttr(&msg)
|
2023-06-09 18:27:09 +02:00
|
|
|
if err != nil {
|
2023-11-24 11:31:22 +01:00
|
|
|
return nil, err
|
2023-06-09 18:27:09 +02:00
|
|
|
}
|
|
|
|
if rt.Family != syscall.AF_INET {
|
|
|
|
continue loop
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, attr := range attrs {
|
|
|
|
if attr.Attr.Type == syscall.RTA_DST {
|
2023-11-24 11:31:22 +01:00
|
|
|
addr, ok := netip.AddrFromSlice(attr.Value)
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
2023-06-09 18:27:09 +02:00
|
|
|
mask := net.CIDRMask(int(rt.DstLen), len(attr.Value)*8)
|
|
|
|
cidr, _ := mask.Size()
|
2023-11-24 11:31:22 +01:00
|
|
|
routePrefix := netip.PrefixFrom(addr, cidr)
|
|
|
|
if routePrefix.IsValid() && routePrefix.Addr().Is4() {
|
|
|
|
prefixList = append(prefixList, routePrefix)
|
2023-06-09 18:27:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-11-24 11:31:22 +01:00
|
|
|
return prefixList, nil
|
2023-06-09 18:27:09 +02:00
|
|
|
}
|
|
|
|
|
2022-09-05 09:06:35 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-12-04 13:34:06 +01:00
|
|
|
return os.WriteFile(ipv4ForwardingPath, []byte("1"), 0644) //nolint:gosec
|
2022-09-05 09:06:35 +02:00
|
|
|
}
|