-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathnetroute_bsd.go
384 lines (334 loc) · 9.89 KB
/
netroute_bsd.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
// Copyright 2012 Google, Inc. All rights reserved.
//
// Use of this source code is governed by a BSD-style license
// that can be found in the LICENSE file in the root of the source
// tree.
//go:build darwin || dragonfly || freebsd || netbsd || openbsd
// This is a BSD import for the routing structure initially found in
// https://github.com/google/gopacket/blob/master/routing/routing.go
// RIB parsing follows the BSD route format described in
// https://github.com/freebsd/freebsd/blob/master/sys/net/route.h
package netroute
import (
"errors"
"fmt"
"net"
"os"
"sync/atomic"
"syscall"
"time"
"github.com/google/gopacket/routing"
"golang.org/x/net/route"
"golang.org/x/sys/unix"
)
const (
RTF_IFSCOPE = 0x1000000
ROUTE_MSGFILTER = 1
)
type bsdRouter struct {
id uintptr
seq atomic.Uint32
}
// toIPAddr converts a route.Addr to a net.IP.
// Returns nil if the address type is not recognized.
func toIPAddr(a route.Addr) net.IP {
switch t := a.(type) {
case *route.Inet4Addr:
return t.IP[:]
case *route.Inet6Addr:
return t.IP[:]
default:
return nil
}
}
// toRouteAddr takes a net.IP and returns the corresponding route.Addr.
// Returns nil if the IP is empty or has an invalid length.
// IPv4 addresses are converted to route.Inet4Addr and IPv6 to route.Inet6Addr.
func toRouteAddr(ip net.IP) route.Addr {
if len(ip) == 0 {
return nil
}
if len(ip) != net.IPv4len && len(ip) != net.IPv6len {
return nil
}
if p4 := ip.To4(); len(p4) == net.IPv4len {
return &route.Inet4Addr{IP: [4]byte(p4)}
}
return &route.Inet6Addr{IP: [16]byte(ip)}
}
// ipToIfIndex takes an IP and returns the index of the interface with the given IP assigned.
// Returns an error if no interface is found with the specified IP address.
func ipToIfIndex(ip net.IP) (int, error) {
ifaces, err := net.Interfaces()
if err != nil {
return -1, fmt.Errorf("failed to get interfaces: %w", err)
}
for _, iface := range ifaces {
addrs, err := iface.Addrs()
if err != nil {
continue
}
for _, addr := range addrs {
inet, ok := addr.(*net.IPNet)
if !ok {
continue
}
if inet.IP.Equal(ip) {
return iface.Index, nil
}
}
}
return -1, fmt.Errorf("no interface found for IP: %s", ip)
}
// macToIfIndex takes a MAC address and returns the index of the interface with the matching hardware address.
// Returns an error if no interface is found with the specified MAC address.
func macToIfIndex(hwAddr net.HardwareAddr) (int, error) {
ifaces, err := net.Interfaces()
if err != nil {
return -1, fmt.Errorf("failed to get interfaces: %w", err)
}
for _, iface := range ifaces {
if hwAddr.String() == iface.HardwareAddr.String() {
return iface.Index, nil
}
}
return -1, fmt.Errorf("no interface found for MAC: %s", hwAddr.String())
}
// getIfIndex determines the interface index based on the provided MAC address and/or IP address.
// If both are provided, it ensures they resolve to the same interface.
// Returns -1 if neither MAC nor IP is provided, or an error if they resolve to different interfaces.
func getIfIndex(MACAddr net.HardwareAddr, ip net.IP) (int, error) {
ipIndex := -1
macIndex := -1
var err error
if ip == nil && MACAddr == nil {
return -1, nil
}
if ip != nil {
ipIndex, err = ipToIfIndex(ip)
if err != nil {
return -1, fmt.Errorf("failed to find interface with IP: %s", ip.String())
}
}
if MACAddr != nil {
macIndex, err = macToIfIndex(MACAddr)
if err != nil {
return -1, fmt.Errorf("failed to find interface with MAC: %s", MACAddr.String())
}
}
switch {
case (ipIndex >= 0 && macIndex >= 0) && (macIndex != ipIndex):
return -1, fmt.Errorf("given MAC address and source IP resolve to different interfaces")
case (ipIndex >= 0 && macIndex >= 0) && (macIndex == ipIndex):
return ipIndex, nil
case ipIndex >= 0:
return ipIndex, nil
case macIndex >= 0:
return macIndex, nil
default:
return -1, fmt.Errorf("no index found for given ip and/or MAC address")
}
}
// composeRouteMsg creates a RTM_GET RouteMessage for querying the routing table.
// It takes the process ID, optional MAC address, optional source IP, and destination IP.
// The function determines the appropriate interface index if MAC or source IP is provided.
func composeRouteMsg(id uintptr, seq int, MACAddr net.HardwareAddr, src, dst net.IP) (*route.RouteMessage, error) {
dstAddr := toRouteAddr(dst)
if dstAddr == nil {
return nil, fmt.Errorf("failed to parse dst: %#v", dst)
}
msg := &route.RouteMessage{
Version: syscall.RTM_VERSION,
Type: unix.RTM_GET,
ID: id,
Seq: seq,
Addrs: []route.Addr{
dstAddr,
nil,
nil,
nil,
&route.LinkAddr{},
},
}
ifIndex, err := getIfIndex(MACAddr, src)
if err != nil {
return nil, err
}
if ifIndex >= 0 {
msg.Flags = RTF_IFSCOPE
msg.Index = ifIndex
}
return msg, nil
}
func (r *bsdRouter) getSeq() int {
return int(r.seq.Add(1))
}
// getRouteMsgReply takes an RTM_GET RouteMessage and returns reply (RouteMessage)
func getRouteMsgReply(msg *route.RouteMessage) (*route.RouteMessage, error) {
if msg.Type != syscall.RTM_GET {
return nil, errors.New("message type is not RTM_GET")
}
fd, err := getRouteFD()
if err != nil {
return nil, err
}
defer unix.Close(fd)
err = writeMsg(fd, msg)
if err != nil {
return nil, err
}
// Read the response message
return readMsg(msg.ID, msg.Seq, fd)
}
// getRouteFD opens a routing socket.
// Returns the file descriptor for the socket or an error if the socket cannot be created.
func getRouteFD() (int, error) {
fd, err := unix.Socket(unix.AF_ROUTE, unix.SOCK_RAW, unix.AF_UNSPEC)
if err != nil {
scope := "route socket - open"
return -1, annotateUnixError(err, scope)
}
return fd, nil
}
// writeMsg marshals and writes a RouteMessage to the specified file descriptor.
// Returns an error if marshaling fails or if the write operation fails.
func writeMsg(fd int, msg *route.RouteMessage) error {
buf, err := msg.Marshal()
if err != nil {
return fmt.Errorf("invalid route message given: %w", err)
}
if _, err = unix.Write(fd, buf); err != nil {
scope := "route socket - write"
return annotateUnixError(err, scope)
}
return nil
}
// readMsg reads a response from the routing socket and returns the matching RouteMessage.
// It spawns a goroutine to read from the socket and filters messages based on the provided ID and sequence number.
// The function includes a 10-second timeout to prevent indefinite blocking.
func readMsg(id uintptr, seq, fd int) (*route.RouteMessage, error) {
type readResult struct {
msg *route.RouteMessage
err error
}
// Create a channel to receive the result
resultCh := make(chan readResult, 1)
go func() {
var rb [2 << 10]byte
for {
n, err := unix.Read(fd, rb[:])
if err != nil {
scope := "route socket - read"
resultCh <- readResult{
err: annotateUnixError(err, scope),
}
return
}
// Parse response messages
msgs, err := route.ParseRIB(route.RIBTypeRoute, rb[:n])
if err != nil {
resultCh <- readResult{
err: fmt.Errorf("failed to parse messages: %w", err),
}
return
}
if len(msgs) != 1 {
resultCh <- readResult{
err: fmt.Errorf("unexpected number of messages received: %d", len(msgs)),
}
return
}
msg, ok := msgs[0].(*route.RouteMessage)
// confirm it is a reply to our query
if !ok || (id != msg.ID && seq != msg.Seq) {
continue
}
resultCh <- readResult{
msg: msg,
}
return
}
}()
select {
case result := <-resultCh:
return result.msg, result.err
case <-time.After(10 * time.Second):
return nil, fmt.Errorf("route socket - read: timedout")
}
}
func annotateUnixError(err error, scope string) error {
var msg string
switch err {
case nil:
return nil
case unix.ESRCH:
msg = "route not found"
// socket errors
case unix.EACCES:
msg = "permission denied"
case unix.EMFILE:
msg = "file system table full"
case unix.ENOBUFS:
msg = "insufficient buffer space"
case unix.EPERM:
msg = "insufficient privileges"
case unix.EPROTOTYPE:
msg = "socket type not supported"
// read errors
case unix.EBADF:
msg = "invalid socket"
case unix.ECONNRESET:
msg = "socket closed"
case unix.EFAULT:
msg = "invalid buffer"
case unix.EIO:
msg = "I/O error"
case unix.EBUSY:
msg = "failed to read from file descriptor"
case unix.EINVAL:
msg = "invalid file descriptor"
case unix.EAGAIN:
msg = "no data available, try again later"
default: // unexpected system error; fatal
msg = "unexpected error"
}
if scope != "" {
msg = scope + ": " + msg
}
return fmt.Errorf("%s: %w", msg, err)
}
// Route implements the routing.Router interface to find the best route to the destination IP.
// Returns the outgoing interface, gateway IP, preferred source IP, and any error encountered.
func (r *bsdRouter) Route(dst net.IP) (iface *net.Interface, gateway, preferredSrc net.IP, err error) {
return r.RouteWithSrc(nil, nil, dst)
}
// RouteWithSrc extends the Route method to allow specifying a source MAC address and IP.
// This enables more precise routing decisions when multiple interfaces are available.
// Returns the outgoing interface, gateway IP, preferred source IP, and any error encountered.
func (r *bsdRouter) RouteWithSrc(MACAddr net.HardwareAddr, src, dst net.IP) (iface *net.Interface, gateway, preferredSrc net.IP, err error) {
msg, err := composeRouteMsg(r.id, r.getSeq(), MACAddr, src, dst)
if err != nil {
return
}
var reply *route.RouteMessage
if reply, err = getRouteMsgReply(msg); err != nil {
return
}
if iface, err = net.InterfaceByIndex(reply.Index); err != nil {
return
}
preferredSrc = toIPAddr(reply.Addrs[5])
isGatewayRequired := dst.String() != preferredSrc.String()
if !isGatewayRequired {
return
}
gateway = toIPAddr(reply.Addrs[1])
return
}
// New returns a new instance of a BSD-specific routing.Router implementation.
// The router is stateless and uses the routing tables of the host system.
func New() (routing.Router, error) {
r := &bsdRouter{}
r.id = uintptr(os.Getpid())
return r, nil
}