-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathroute.go
298 lines (264 loc) · 6.64 KB
/
route.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
package ip
import (
"fmt"
"net"
"strings"
"syscall"
"github.com/Asphaltt/go-iproute2"
"github.com/Asphaltt/go-iproute2/internal/etc"
"github.com/mdlayher/netlink"
"golang.org/x/sys/unix"
)
type RouteType int
func (typ RouteType) String() string {
switch typ {
case unix.RTN_UNSPEC:
return "none"
case unix.RTN_UNICAST:
return "unicast"
case unix.RTN_LOCAL:
return "local"
case unix.RTN_BROADCAST:
return "broadcast"
case unix.RTN_ANYCAST:
return "anycast"
case unix.RTN_MULTICAST:
return "multicast"
case unix.RTN_BLACKHOLE:
return "blackhole"
case unix.RTN_UNREACHABLE:
return "unreachable"
case unix.RTN_PROHIBIT:
return "prohibit"
case unix.RTN_THROW:
return "throw"
case unix.RTN_NAT:
return "nat"
case unix.RTN_XRESOLVE:
return "xresolve"
default:
return fmt.Sprintf("%d", typ)
}
}
type RouteTable int
func (t RouteTable) String() string {
names, _ := etc.ReadRouteTables()
names[unix.RT_TABLE_DEFAULT] = "default"
names[unix.RT_TABLE_MAIN] = "main"
names[unix.RT_TABLE_LOCAL] = "local"
if name, ok := names[int(t)]; ok {
return name
}
return fmt.Sprintf("%d", t)
}
type RouteProtocol int
func (p RouteProtocol) String() string {
protocols, _ := etc.ReadRouteProtos()
protocols[unix.RTPROT_UNSPEC] = "unspec"
protocols[unix.RTPROT_REDIRECT] = "redirect"
protocols[unix.RTPROT_KERNEL] = "kernel"
protocols[unix.RTPROT_BOOT] = "boot"
protocols[unix.RTPROT_STATIC] = "static"
protocols[unix.RTPROT_GATED] = "gated"
protocols[unix.RTPROT_RA] = "ra"
protocols[unix.RTPROT_MRT] = "mrt"
protocols[unix.RTPROT_ZEBRA] = "zebra"
protocols[unix.RTPROT_BIRD] = "bird"
protocols[unix.RTPROT_BABEL] = "babel"
protocols[unix.RTPROT_DNROUTED] = "dnrouted"
protocols[unix.RTPROT_XORP] = "xorp"
protocols[unix.RTPROT_NTK] = "ntk"
protocols[unix.RTPROT_DHCP] = "dhcp"
protocols[unix.RTPROT_KEEPALIVED] = "keepalived"
protocols[unix.RTPROT_BGP] = "bgp"
protocols[unix.RTPROT_ISIS] = "isis"
protocols[unix.RTPROT_OSPF] = "ospf"
protocols[unix.RTPROT_RIP] = "rip"
protocols[unix.RTPROT_EIGRP] = "eigrp"
if proto, ok := protocols[int(p)]; ok {
return proto
}
return fmt.Sprintf("%d", p)
}
type RouteScope int
func (s RouteScope) String() string {
scopes, _ := etc.ReadRouteScopes()
scopes[unix.RT_SCOPE_UNIVERSE] = "global"
scopes[unix.RT_SCOPE_NOWHERE] = "nowhere"
scopes[unix.RT_SCOPE_HOST] = "host"
scopes[unix.RT_SCOPE_LINK] = "link"
scopes[unix.RT_SCOPE_SITE] = "site"
if scope, ok := scopes[int(s)]; ok {
return scope
}
return fmt.Sprintf("%d", s)
}
type RouteFlags int
const (
RTM_F_OFFLOAD_FAILED = 0x20000000 /* route offload failed, this value
* is chosen to avoid conflicts with
* other flags defined in
* include/uapi/linux/ipv6_route.h
*/
)
func (f RouteFlags) String() string {
flags := []string{}
if int(f)&unix.RTNH_F_DEAD != 0 {
flags = append(flags, "dead")
}
if int(f)&unix.RTNH_F_ONLINK != 0 {
flags = append(flags, "onlink")
}
if int(f)&unix.RTNH_F_PERVASIVE != 0 {
flags = append(flags, "pervasive")
}
if int(f)&unix.RTNH_F_OFFLOAD != 0 {
flags = append(flags, "offload")
}
// if int(f)&unix.RTNH_F_TRAP != 0 {
// flags = append(flags, "trap")
// }
if int(f)&unix.RTM_F_NOTIFY != 0 {
flags = append(flags, "notify")
}
if int(f)&unix.RTNH_F_LINKDOWN != 0 {
flags = append(flags, "linkdown")
}
if int(f)&unix.RTNH_F_UNRESOLVED != 0 {
flags = append(flags, "unresolved")
}
if int(f)&unix.RTM_F_OFFLOAD != 0 {
flags = append(flags, "rt_offload")
}
if int(f)&unix.RTM_F_TRAP != 0 {
flags = append(flags, "rt_trap")
}
if int(f)&RTM_F_OFFLOAD_FAILED != 0 {
flags = append(flags, "rt_offload_failed")
}
return strings.Join(flags, " ")
}
type RoutePref int
const (
ICMPV6_ROUTER_PREF_LOW = 0x3
ICMPV6_ROUTER_PREF_MEDIUM = 0x0
ICMPV6_ROUTER_PREF_HIGH = 0x1
ICMPV6_ROUTER_PREF_INVALID = 0x2
)
func (p RoutePref) String() string {
prefs := map[int]string{
ICMPV6_ROUTER_PREF_LOW: "low",
ICMPV6_ROUTER_PREF_MEDIUM: "medium",
ICMPV6_ROUTER_PREF_HIGH: "high",
}
return prefs[int(p)]
}
type RouteEntry struct {
Family int
DstLen int
SrcLen int
Tos int
TableID RouteTable
Protocol RouteProtocol
Scope RouteScope
Type RouteType
Flags RouteFlags
Daddr net.IP
Saddr net.IP
InIfindex int
OutIfindex int
Gateway net.IP
Table RouteTable
Priority int
PrefSrc net.IP
Metric int
Pref RoutePref
}
func (e *RouteEntry) init() {
e.Table = -1
e.Priority = -1
e.Pref = -1
}
func (c *Client) ListRoutes() ([]*RouteEntry, error) {
return c.listRoutes(syscall.AF_UNSPEC)
}
func (c *Client) ListRoutesV4() ([]*RouteEntry, error) {
return c.listRoutes(syscall.AF_INET)
}
func (c *Client) ListRoutesV6() ([]*RouteEntry, error) {
return c.listRoutes(syscall.AF_INET6)
}
func (c *Client) listRoutes(family uint8) ([]*RouteEntry, error) {
var msg netlink.Message
msg.Header.Type = unix.RTM_GETROUTE
msg.Header.Flags = netlink.Dump | netlink.Request
var rtmsg iproute2.RtMsg
rtmsg.Family = family
msg.Data, _ = rtmsg.MarshalBinary()
msgs, err := c.conn.Execute(msg)
if err != nil {
return nil, err
}
entries := make([]*RouteEntry, 0, len(msgs))
for _, msg := range msgs {
if msg.Header.Type != unix.RTM_NEWROUTE {
continue
}
e, ok, err := parseRouteMsg(&msg)
if err != nil {
return entries, err
}
if ok {
entries = append(entries, e)
}
}
return entries, nil
}
// parseRouteMsg parses a route information from a netlink message.
func parseRouteMsg(msg *netlink.Message) (*RouteEntry, bool, error) {
var rtmsg iproute2.RtMsg
if err := rtmsg.UnmarshalBinary(msg.Data); err != nil {
return nil, false, err
}
var e RouteEntry
e.init()
e.Family = int(rtmsg.Family)
e.DstLen = int(rtmsg.Dst_len)
e.SrcLen = int(rtmsg.Src_len)
e.Tos = int(rtmsg.Tos)
e.TableID = RouteTable(rtmsg.Table)
e.Protocol = RouteProtocol(rtmsg.Protocol)
e.Scope = RouteScope(rtmsg.Scope)
e.Type = RouteType(rtmsg.Type)
e.Flags = RouteFlags(rtmsg.Flags)
ad, err := netlink.NewAttributeDecoder(msg.Data[iproute2.SizeofRtMsg:])
if err != nil {
return &e, false, err
}
for ad.Next() {
switch ad.Type() {
case unix.RTA_DST:
e.Daddr = net.IP(ad.Bytes())
case unix.RTA_SRC:
e.Saddr = net.IP(ad.Bytes())
case unix.RTA_IIF:
e.InIfindex = int(ad.Uint32())
case unix.RTA_OIF:
e.OutIfindex = int(ad.Uint32())
case unix.RTA_GATEWAY:
e.Gateway = net.IP(ad.Bytes())
case unix.RTA_PRIORITY:
e.Priority = int(ad.Uint32())
case unix.RTA_PREFSRC:
e.PrefSrc = net.IP(ad.Bytes())
case unix.RTA_METRICS:
e.Metric = int(ad.Uint32())
case unix.RTA_TABLE:
e.Table = RouteTable(ad.Uint32())
case unix.RTA_PREF:
e.Pref = RoutePref(ad.Uint8())
}
}
err = ad.Err()
return &e, err == nil, err
}