forked from insomniacslk/u-root
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ops.go
229 lines (206 loc) · 5.56 KB
/
ops.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
// Copyright 2012-2017 the u-root Authors. All rights reserved
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"fmt"
"io"
"math"
"net"
"strings"
"github.com/vishvananda/netlink"
"golang.org/x/sys/unix"
)
func showLinks(w io.Writer, withAddresses bool) error {
ifaces, err := netlink.LinkList()
if err != nil {
return fmt.Errorf("can't enumerate interfaces: %v", err)
}
for _, v := range ifaces {
l := v.Attrs()
master := ""
if l.MasterIndex != 0 {
link, err := netlink.LinkByIndex(l.MasterIndex)
if err != nil {
return fmt.Errorf("can't get link with index %d: %v", l.MasterIndex, err)
}
master = fmt.Sprintf("master %s ", link.Attrs().Name)
}
fmt.Fprintf(w, "%d: %s: <%s> mtu %d %sstate %s\n", l.Index, l.Name,
strings.Replace(strings.ToUpper(l.Flags.String()), "|", ",", -1),
l.MTU, master, strings.ToUpper(l.OperState.String()))
fmt.Fprintf(w, " link/%s %s\n", l.EncapType, l.HardwareAddr)
if withAddresses {
showLinkAddresses(w, v)
}
}
return nil
}
func showLinkAddresses(w io.Writer, link netlink.Link) error {
addrs, err := netlink.AddrList(link, netlink.FAMILY_ALL)
if err != nil {
return fmt.Errorf("can't enumerate addresses: %v", err)
}
for _, addr := range addrs {
var inet string
switch len(addr.IPNet.IP) {
case 4:
inet = "inet"
case 16:
inet = "inet6"
default:
return fmt.Errorf("can't figure out IP protocol version: IP length is %d", len(addr.IPNet.IP))
}
fmt.Fprintf(w, " %s %s", inet, addr.IP)
if addr.Broadcast != nil {
fmt.Fprintf(w, " brd %s", addr.Broadcast)
}
fmt.Fprintf(w, " scope %s %s\n", addrScopes[netlink.Scope(addr.Scope)], addr.Label)
var validLft, preferredLft string
// TODO: fix vishnavanda/netlink. *Lft should be uint32, not int.
if uint32(addr.PreferedLft) == math.MaxUint32 {
preferredLft = "forever"
} else {
preferredLft = fmt.Sprintf("%dsec", addr.PreferedLft)
}
if uint32(addr.ValidLft) == math.MaxUint32 {
validLft = "forever"
} else {
validLft = fmt.Sprintf("%dsec", addr.ValidLft)
}
fmt.Fprintf(w, " valid_lft %s preferred_lft %s\n", validLft, preferredLft)
}
return nil
}
var neighStates = map[int]string{
netlink.NUD_NONE: "NONE",
netlink.NUD_INCOMPLETE: "INCOMPLETE",
netlink.NUD_REACHABLE: "REACHABLE",
netlink.NUD_STALE: "STALE",
netlink.NUD_DELAY: "DELAY",
netlink.NUD_PROBE: "PROBE",
netlink.NUD_FAILED: "FAILED",
netlink.NUD_NOARP: "NOARP",
netlink.NUD_PERMANENT: "PERMANENT",
}
func getState(state int) string {
ret := make([]string, 0)
for st, name := range neighStates {
if state&st != 0 {
ret = append(ret, name)
}
}
if len(ret) == 0 {
return "UNKNOWN"
}
return strings.Join(ret, ",")
}
func showNeighbours(w io.Writer, withAddresses bool) error {
ifaces, err := net.Interfaces()
if err != nil {
return err
}
for _, iface := range ifaces {
neighs, err := netlink.NeighList(iface.Index, 0)
if err != nil {
return fmt.Errorf("can't list neighbours: %v", err)
}
for _, v := range neighs {
if v.State&netlink.NUD_NOARP != 0 {
continue
}
entry := fmt.Sprintf("%s dev %s", v.IP.String(), iface.Name)
if v.HardwareAddr != nil {
entry += fmt.Sprintf(" lladdr %s", v.HardwareAddr)
}
if v.Flags&netlink.NTF_ROUTER != 0 {
entry += " router"
}
entry += " " + getState(v.State)
fmt.Println(entry)
}
}
return nil
}
const (
defaultFmt = "default via %v dev %s proto %s metric %d\n"
routeFmt = "%v dev %s proto %s scope %s src %s metric %d\n"
route6Fmt = "%s dev %s proto %s metric %d\n"
routeVia6Fmt = "%s via %s dev %s proto %s metric %d\n"
)
// routing protocol identifier
// specified in Linux Kernel header: include/uapi/linux/rtnetlink.h
// See man IP-ROUTE(8) and RTNETLINK(7)
var rtProto = map[int]string{
unix.RTPROT_BABEL: "babel",
unix.RTPROT_BGP: "bgp",
unix.RTPROT_BIRD: "bird",
unix.RTPROT_BOOT: "boot",
unix.RTPROT_DHCP: "dhcp",
unix.RTPROT_DNROUTED: "dnrouted",
unix.RTPROT_EIGRP: "eigrp",
unix.RTPROT_GATED: "gated",
unix.RTPROT_ISIS: "isis",
unix.RTPROT_KERNEL: "kernel",
unix.RTPROT_MROUTED: "mrouted",
unix.RTPROT_MRT: "mrt",
unix.RTPROT_NTK: "ntk",
unix.RTPROT_OSPF: "ospf",
unix.RTPROT_RA: "ra",
unix.RTPROT_REDIRECT: "redirect",
unix.RTPROT_RIP: "rip",
unix.RTPROT_STATIC: "static",
unix.RTPROT_UNSPEC: "unspec",
unix.RTPROT_XORP: "xorp",
unix.RTPROT_ZEBRA: "zebra",
}
func showRoutes(inet6 bool) error {
var f int
if inet6 {
f = netlink.FAMILY_V6
} else {
f = netlink.FAMILY_V4
}
routes, err := netlink.RouteList(nil, f)
if err != nil {
return err
}
for _, route := range routes {
link, err := netlink.LinkByIndex(route.LinkIndex)
if err != nil {
return err
}
if route.Dst == nil {
defaultRoute(route, link)
} else {
showRoute(route, link, f)
}
}
return nil
}
func defaultRoute(r netlink.Route, l netlink.Link) {
gw := r.Gw
name := l.Attrs().Name
proto := rtProto[r.Protocol]
metric := r.Priority
fmt.Printf(defaultFmt, gw, name, proto, metric)
}
func showRoute(r netlink.Route, l netlink.Link, f int) {
dest := r.Dst
name := l.Attrs().Name
proto := rtProto[r.Protocol]
metric := r.Priority
switch f {
case netlink.FAMILY_V4:
scope := addrScopes[r.Scope]
src := r.Src
fmt.Printf(routeFmt, dest, name, proto, scope, src, metric)
case netlink.FAMILY_V6:
if r.Gw != nil {
gw := r.Gw
fmt.Printf(routeVia6Fmt, dest, gw, name, proto, metric)
} else {
fmt.Printf(route6Fmt, dest, name, proto, metric)
}
}
}