2023-06-09 18:27:09 +02:00
|
|
|
//go:build darwin || dragonfly || freebsd || netbsd || openbsd
|
|
|
|
|
2024-06-13 13:24:24 +02:00
|
|
|
package systemops
|
2023-06-09 18:27:09 +02:00
|
|
|
|
|
|
|
import (
|
2024-05-07 18:50:34 +02:00
|
|
|
"errors"
|
2023-06-09 18:27:09 +02:00
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"net/netip"
|
2024-05-07 18:50:34 +02:00
|
|
|
"strconv"
|
2023-06-09 18:27:09 +02:00
|
|
|
"syscall"
|
2024-05-07 09:51:43 +02:00
|
|
|
"time"
|
2023-06-09 18:27:09 +02:00
|
|
|
|
2024-05-07 09:51:43 +02:00
|
|
|
"github.com/cenkalti/backoff/v4"
|
2024-04-09 13:25:14 +02:00
|
|
|
log "github.com/sirupsen/logrus"
|
2023-06-09 18:27:09 +02:00
|
|
|
"golang.org/x/net/route"
|
|
|
|
)
|
|
|
|
|
2024-05-07 18:50:34 +02:00
|
|
|
type Route struct {
|
|
|
|
Dst netip.Prefix
|
|
|
|
Gw netip.Addr
|
|
|
|
Interface *net.Interface
|
|
|
|
}
|
2023-06-09 18:27:09 +02:00
|
|
|
|
2023-11-24 11:31:22 +01:00
|
|
|
func getRoutesFromTable() ([]netip.Prefix, error) {
|
2024-05-07 09:51:43 +02:00
|
|
|
tab, err := retryFetchRIB()
|
2023-06-09 18:27:09 +02:00
|
|
|
if err != nil {
|
2024-05-07 18:50:34 +02:00
|
|
|
return nil, fmt.Errorf("fetch RIB: %v", err)
|
2023-06-09 18:27:09 +02:00
|
|
|
}
|
|
|
|
msgs, err := route.ParseRIB(route.RIBTypeRoute, tab)
|
|
|
|
if err != nil {
|
2024-05-07 18:50:34 +02:00
|
|
|
return nil, fmt.Errorf("parse RIB: %v", err)
|
2023-06-09 18:27:09 +02:00
|
|
|
}
|
2024-05-07 18:50:34 +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
|
|
|
}
|
2024-05-07 18:50:34 +02:00
|
|
|
if m.Type != syscall.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
|
|
|
}
|
|
|
|
|
2024-06-13 13:24:24 +02:00
|
|
|
if filterRoutesByFlags(m.Flags) {
|
2023-06-09 18:27:09 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2024-05-07 18:50:34 +02:00
|
|
|
route, err := MsgToRoute(m)
|
|
|
|
if err != nil {
|
|
|
|
log.Warnf("Failed to parse route message: %v", err)
|
2023-11-24 11:31:22 +01:00
|
|
|
continue
|
|
|
|
}
|
2024-05-07 18:50:34 +02:00
|
|
|
if route.Dst.IsValid() {
|
|
|
|
prefixList = append(prefixList, route.Dst)
|
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
|
|
|
}
|
|
|
|
|
2024-05-07 09:51:43 +02:00
|
|
|
func retryFetchRIB() ([]byte, error) {
|
|
|
|
var out []byte
|
|
|
|
operation := func() error {
|
|
|
|
var err error
|
|
|
|
out, err = route.FetchRIB(syscall.AF_UNSPEC, route.RIBTypeRoute, 0)
|
|
|
|
if errors.Is(err, syscall.ENOMEM) {
|
2024-05-07 18:50:34 +02:00
|
|
|
log.Debug("~etrying fetchRIB due to 'cannot allocate memory' error")
|
2024-05-07 09:51:43 +02:00
|
|
|
return err
|
|
|
|
} else if err != nil {
|
|
|
|
return backoff.Permanent(err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
expBackOff := backoff.NewExponentialBackOff()
|
|
|
|
expBackOff.InitialInterval = 50 * time.Millisecond
|
|
|
|
expBackOff.MaxInterval = 500 * time.Millisecond
|
|
|
|
expBackOff.MaxElapsedTime = 1 * time.Second
|
|
|
|
|
|
|
|
err := backoff.Retry(operation, expBackOff)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to fetch routing information: %w", err)
|
|
|
|
}
|
|
|
|
return out, nil
|
|
|
|
}
|
|
|
|
|
2024-05-07 18:50:34 +02:00
|
|
|
func toNetIP(a route.Addr) netip.Addr {
|
2023-06-09 18:27:09 +02:00
|
|
|
switch t := a.(type) {
|
|
|
|
case *route.Inet4Addr:
|
2024-05-07 18:50:34 +02:00
|
|
|
return netip.AddrFrom4(t.IP)
|
|
|
|
case *route.Inet6Addr:
|
|
|
|
ip := netip.AddrFrom16(t.IP)
|
|
|
|
if t.ZoneID != 0 {
|
|
|
|
ip.WithZone(strconv.Itoa(t.ZoneID))
|
|
|
|
}
|
|
|
|
return ip
|
2023-11-24 11:31:22 +01:00
|
|
|
default:
|
2024-05-07 18:50:34 +02:00
|
|
|
return netip.Addr{}
|
2023-11-24 11:31:22 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-13 13:24:24 +02:00
|
|
|
// ones returns the number of leading ones in the mask.
|
2024-05-07 18:50:34 +02:00
|
|
|
func ones(a route.Addr) (int, error) {
|
2023-11-24 11:31:22 +01:00
|
|
|
switch t := a.(type) {
|
|
|
|
case *route.Inet4Addr:
|
2024-05-07 18:50:34 +02:00
|
|
|
mask, _ := net.IPMask(t.IP[:]).Size()
|
|
|
|
return mask, nil
|
|
|
|
case *route.Inet6Addr:
|
|
|
|
mask, _ := net.IPMask(t.IP[:]).Size()
|
|
|
|
return mask, nil
|
|
|
|
default:
|
|
|
|
return 0, fmt.Errorf("unexpected address type: %T", a)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-13 13:24:24 +02:00
|
|
|
// MsgToRoute converts a route message to a Route.
|
2024-05-07 18:50:34 +02:00
|
|
|
func MsgToRoute(msg *route.RouteMessage) (*Route, error) {
|
|
|
|
dstIP, nexthop, dstMask := msg.Addrs[0], msg.Addrs[1], msg.Addrs[2]
|
|
|
|
|
|
|
|
addr := toNetIP(dstIP)
|
|
|
|
|
|
|
|
var nexthopAddr netip.Addr
|
|
|
|
var nexthopIntf *net.Interface
|
|
|
|
|
|
|
|
switch t := nexthop.(type) {
|
|
|
|
case *route.Inet4Addr, *route.Inet6Addr:
|
|
|
|
nexthopAddr = toNetIP(t)
|
|
|
|
case *route.LinkAddr:
|
|
|
|
nexthopIntf = &net.Interface{
|
|
|
|
Index: t.Index,
|
|
|
|
Name: t.Name,
|
|
|
|
}
|
2023-06-09 18:27:09 +02:00
|
|
|
default:
|
2024-05-07 18:50:34 +02:00
|
|
|
return nil, fmt.Errorf("unexpected next hop type: %T", t)
|
2023-06-09 18:27:09 +02:00
|
|
|
}
|
2024-05-07 18:50:34 +02:00
|
|
|
|
|
|
|
var prefix netip.Prefix
|
|
|
|
|
|
|
|
if dstMask == nil {
|
|
|
|
if addr.Is4() {
|
|
|
|
prefix = netip.PrefixFrom(addr, 32)
|
|
|
|
} else {
|
|
|
|
prefix = netip.PrefixFrom(addr, 128)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
bits, err := ones(dstMask)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to parse mask: %v", dstMask)
|
|
|
|
}
|
|
|
|
prefix = netip.PrefixFrom(addr, bits)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &Route{
|
|
|
|
Dst: prefix,
|
|
|
|
Gw: nexthopAddr,
|
|
|
|
Interface: nexthopIntf,
|
|
|
|
}, nil
|
|
|
|
|
2023-06-09 18:27:09 +02:00
|
|
|
}
|