2023-06-09 18:27:09 +02:00
|
|
|
//go:build darwin || dragonfly || freebsd || netbsd || openbsd
|
|
|
|
// +build darwin dragonfly freebsd netbsd openbsd
|
|
|
|
|
|
|
|
package routemanager
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"net/netip"
|
|
|
|
"syscall"
|
|
|
|
|
|
|
|
"golang.org/x/net/route"
|
|
|
|
)
|
|
|
|
|
|
|
|
// selected BSD Route flags.
|
|
|
|
const (
|
|
|
|
RTF_UP = 0x1
|
|
|
|
RTF_GATEWAY = 0x2
|
|
|
|
RTF_HOST = 0x4
|
|
|
|
RTF_REJECT = 0x8
|
|
|
|
RTF_DYNAMIC = 0x10
|
|
|
|
RTF_MODIFIED = 0x20
|
|
|
|
RTF_STATIC = 0x800
|
|
|
|
RTF_BLACKHOLE = 0x1000
|
|
|
|
RTF_LOCAL = 0x200000
|
|
|
|
RTF_BROADCAST = 0x400000
|
|
|
|
RTF_MULTICAST = 0x800000
|
|
|
|
)
|
|
|
|
|
2023-11-24 11:31:22 +01:00
|
|
|
func getRoutesFromTable() ([]netip.Prefix, error) {
|
2023-06-09 18:27:09 +02:00
|
|
|
tab, err := route.FetchRIB(syscall.AF_UNSPEC, route.RIBTypeRoute, 0)
|
|
|
|
if err != nil {
|
2023-11-24 11:31:22 +01:00
|
|
|
return nil, err
|
2023-06-09 18:27:09 +02:00
|
|
|
}
|
|
|
|
msgs, err := route.ParseRIB(route.RIBTypeRoute, 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
|
|
|
for _, msg := range msgs {
|
|
|
|
m := msg.(*route.RouteMessage)
|
|
|
|
|
|
|
|
if m.Version < 3 || m.Version > 5 {
|
2023-11-24 11:31:22 +01:00
|
|
|
return nil, fmt.Errorf("unexpected RIB message version: %d", m.Version)
|
2023-06-09 18:27:09 +02:00
|
|
|
}
|
|
|
|
if m.Type != 4 /* RTM_GET */ {
|
2023-11-24 11:31:22 +01:00
|
|
|
return nil, fmt.Errorf("unexpected RIB message type: %d", m.Type)
|
2023-06-09 18:27:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if m.Flags&RTF_UP == 0 ||
|
|
|
|
m.Flags&(RTF_REJECT|RTF_BLACKHOLE) != 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2023-11-24 11:31:22 +01:00
|
|
|
addr, ok := toNetIPAddr(m.Addrs[0])
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
mask, ok := toNetIPMASK(m.Addrs[2])
|
|
|
|
if !ok {
|
|
|
|
continue
|
2023-06-09 18:27:09 +02:00
|
|
|
}
|
2023-11-24 11:31:22 +01:00
|
|
|
cidr, _ := mask.Size()
|
2023-06-09 18:27:09 +02:00
|
|
|
|
2023-11-24 11:31:22 +01:00
|
|
|
routePrefix := netip.PrefixFrom(addr, cidr)
|
|
|
|
if routePrefix.IsValid() {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2023-11-24 11:31:22 +01:00
|
|
|
func toNetIPAddr(a route.Addr) (netip.Addr, bool) {
|
2023-06-09 18:27:09 +02:00
|
|
|
switch t := a.(type) {
|
|
|
|
case *route.Inet4Addr:
|
|
|
|
ip := net.IPv4(t.IP[0], t.IP[1], t.IP[2], t.IP[3])
|
2023-11-24 11:31:22 +01:00
|
|
|
addr := netip.MustParseAddr(ip.String())
|
|
|
|
return addr, true
|
|
|
|
default:
|
|
|
|
return netip.Addr{}, false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func toNetIPMASK(a route.Addr) (net.IPMask, bool) {
|
|
|
|
switch t := a.(type) {
|
|
|
|
case *route.Inet4Addr:
|
|
|
|
mask := net.IPv4Mask(t.IP[0], t.IP[1], t.IP[2], t.IP[3])
|
|
|
|
return mask, true
|
2023-06-09 18:27:09 +02:00
|
|
|
default:
|
2023-11-24 11:31:22 +01:00
|
|
|
return nil, false
|
2023-06-09 18:27:09 +02:00
|
|
|
}
|
|
|
|
}
|