-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnic.go
364 lines (321 loc) · 10.7 KB
/
nic.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
package packet
import (
"bufio"
"bytes"
"context"
"encoding/binary"
"fmt"
"io/ioutil"
"log"
"net"
"net/netip"
"os"
"os/exec"
"strconv"
"strings"
"time"
"github.com/vishvananda/netlink"
)
// NICInfo stores the network interface info
type NICInfo struct {
IFI *net.Interface
HomeLAN4 netip.Prefix // IPv4: home LAN netmask
HostAddr4 Addr // IPv4: host MAC and IPv4
RouterAddr4 Addr // IPv4: router MAC and IPv4
HostLLA netip.Prefix // IPv6: host LLA
HostGUA netip.Prefix // IPv6: host GUA
RouterLLA netip.Prefix // IPv6: router LLA
RouterGUA netip.Prefix // IPv6: router GUA
RouterPrefix net.IP // IPv6: router prefix
}
func (e NICInfo) String() string {
return fmt.Sprintf("mac=%s homeLAN=%s hostip4=%s lla=%s gua=%s routerIP4=%s routerMAC=%s", e.HostAddr4.MAC, e.HomeLAN4, e.HostAddr4.IP, e.HostLLA, e.HostGUA, e.RouterAddr4.IP, e.RouterAddr4.MAC)
}
// CopyIP simply copies the IP to a new buffer
// Always return len 16 - using go internal 16 bytes for ipv4
func CopyIP(srcIP net.IP) net.IP {
if len(srcIP) == 4 {
return srcIP.To16() // this will copy to a new 16 len buffer
}
ip := make(net.IP, len(srcIP))
copy(ip, srcIP)
return ip
}
// CopyMAC simply copies a mac address to a new buffer with the same len
func CopyMAC(srcMAC net.HardwareAddr) net.HardwareAddr {
mac := make(net.HardwareAddr, len(srcMAC))
copy(mac, srcMAC)
return mac
}
// CopyBytes simply copies a mac address to a new buffer with the same len
func CopyBytes(b []byte) []byte {
bb := make([]byte, len(b))
copy(bb, b)
return bb
}
// GetNICInfo returns the interface configuration
//
// TODO: use routing package to identify default router
// https://github.com/google/gopacket/tree/v1.1.19/routing
func GetNICInfo(nic string) (info *NICInfo, err error) {
info = &NICInfo{}
info.IFI, err = net.InterfaceByName(nic)
if err != nil {
return nil, err
}
addrs, err := info.IFI.Addrs()
if err != nil {
return nil, err
}
for i := range addrs {
// ip, ipNet, err := net.ParseCIDR(addrs[i].String())
ip, err := netip.ParsePrefix(addrs[i].String())
if err != nil {
log.Printf("NIC cannot parse IP %s error %s ", addrs[i].String(), err)
continue
}
if ip.Addr().Is4() && ip.Addr().IsGlobalUnicast() {
info.HostAddr4.IP = ip.Addr()
info.HomeLAN4 = ip.Masked()
}
if ip.Addr().Is6() {
if ip.Addr().IsLinkLocalUnicast() {
info.HostLLA = ip
}
if ip.Addr().IsGlobalUnicast() {
info.HostGUA = ip
}
}
}
if !info.HostAddr4.IP.IsValid() || info.HostAddr4.IP.IsUnspecified() {
return nil, fmt.Errorf("ipv4 not found on interface")
}
// Print warning if nic has more than one IPv4 and our chosen IP is not the default.
//
// The following "ip addrs" output is possible on linuxi if dhcpclient is running:
//
// 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
// link/ether 02:42:15:e6:10:08 brd ff:ff:ff:ff:ff:ff
// inet 192.168.0.107/24 brd 192.168.0.255 scope global dynamic eth0
// valid_lft 86208sec preferred_lft 86208sec
// inet 192.168.0.129/24 brd 192.168.0.255 scope global secondary eth0
// valid_lft forever preferred_lft forever
if defaultIP := defaultOutboundIP(); info.HostAddr4.IP != defaultIP {
Logger.Msg("warning host IP is different than default outbound IP").IP("hostIP", info.HostAddr4.IP).IP("defaultIP", defaultIP).Write()
}
defaultGW, err := GetIP4DefaultGatewayAddr(nic)
if err != nil {
return nil, err
}
info.RouterAddr4 = Addr{MAC: defaultGW.MAC, IP: defaultGW.IP}
info.HostAddr4 = Addr{MAC: info.IFI.HardwareAddr, IP: info.HostAddr4.IP}
return info, nil
}
// defaultOutboundIP return the preferred outbound ip
func defaultOutboundIP() netip.Addr {
conn, err := net.Dial("udp", "8.8.8.8:80")
if err != nil {
return netip.Addr{}
}
defer conn.Close()
localAddr := conn.LocalAddr().(*net.UDPAddr)
ip, _ := netip.AddrFromSlice(localAddr.IP)
return ip
}
const (
file = "/proc/net/route"
line = 1 // line containing the gateway addr. (first line: 0)
sep = "\t" // field separator
)
// GetLinuxDefaultGateway read the default gateway from linux route file
//
// file: /proc/net/route file:
// Iface Destination Gateway Flags RefCnt Use Metric Mask
// eth0 00000000 C900A8C0 0003 0 0 100 00000000 0 00
// eth0 0000A8C0 00000000 0001 0 0 100 00FFFFFF 0 00
//
func GetLinuxDefaultGateway() (gw netip.Addr, err error) {
file, err := os.Open(file)
if err != nil {
return netip.Addr{}, err
}
defer file.Close()
scanner := bufio.NewScanner(file)
if scanner.Scan() {
// jump to line containing the gateway address
for i := 0; i < line; i++ {
scanner.Scan()
}
// get field containing gateway address
tokens := strings.Split(scanner.Text(), sep)
gatewayHex := "0x" + tokens[2] // field containing hex gateway address (first field: 0)
d, _ := strconv.ParseInt(gatewayHex, 0, 64)
d32 := uint32(d)
var buf [4]byte
binary.LittleEndian.PutUint32(buf[:], d32)
gw = netip.AddrFrom4(buf)
if gw.IsUnspecified() {
return netip.Addr{}, ErrInvalidIP
}
}
return gw, nil
}
// LoadLinuxARPTable read arp entries from linux proc file
//
// /proc/net/arp format:
// IP address HW type Flags HW address Mask Device
// 192.168.0.1 0x1 0x2 20:0c:c8:23:f7:1a * eth0
// 192.168.0.4 0x1 0x2 4c:bb:58:f4:b2:d7 * eth0
// 192.168.0.5 0x1 0x2 84:b1:53:ea:1f:40 * eth0
//
func LoadLinuxARPTable(nic string) (list []Addr, err error) {
const name = "/proc/net/arp"
file, err := os.Open(name)
if err != nil {
return nil, fmt.Errorf("failed to open proc file=%s: %w ", name, err)
}
defer file.Close()
scanner := bufio.NewScanner(file)
scanner.Scan() // skip first row with fields description
for scanner.Scan() {
tokens := strings.Fields(scanner.Text())
if len(tokens) < 6 {
fmt.Println("raw: error in loadARPProcTable - missing fields", tokens)
continue
}
if tokens[5] != nic {
continue
}
ip, err := netip.ParseAddr(tokens[0])
if err != nil || ip.IsUnspecified() {
fmt.Println("raw: error in loadARPProcTable - invalid IP", tokens, err)
continue
}
mac, err := net.ParseMAC(tokens[3])
if err != nil || bytes.Equal(mac, net.HardwareAddr{0, 0, 0, 0, 0, 0}) || bytes.Equal(mac, net.HardwareAddr{}) {
// local IP has entry "192.168.0.129 0x1 0x0 00:00:00:00:00:00 * eth0"
if Logger.IsDebug() {
fmt.Println("raw: error in loadARPProcTable - invalid MAC", tokens)
}
continue
}
list = append(list, Addr{MAC: mac, IP: ip})
}
return list, nil
}
// GetIP4DefaultGatewayAddr return the IP4 default gatewy for nic
func GetIP4DefaultGatewayAddr(nic string) (addr Addr, err error) {
if addr.IP, err = GetLinuxDefaultGateway(); err != nil {
fmt.Println("error getting router ", err)
return Addr{}, ErrInvalidIP
}
// Try 3 times to read arp table
// This is required if we just reset the interface and the arp table is nil
var arpList []Addr
for i := 0; i < 3; i++ {
ExecPing(addr.IP.String()) // ping to populate arp table
time.Sleep(time.Millisecond * 15)
arpList, err = LoadLinuxARPTable(nic)
if err == nil {
// search in table; if the arp entry is not yeet complete, the mac will be zero or wont exist
for _, v := range arpList {
if v.IP == addr.IP {
addr.MAC = v.MAC
break
}
}
}
}
if addr.MAC == nil {
return Addr{}, fmt.Errorf("default gw mac not found on interface")
}
return addr, nil
}
func EnableIP4Forwarding(nic string) error {
// enable ip_forward
// sudo echo 1 | sudo tee /proc/sys/net/ipv4/ip_forward
proc := fmt.Sprintf("/proc/sys/net/ipv4/ip_forward")
if err := ioutil.WriteFile(proc, []byte{'1'}, os.ModePerm); err != nil {
return fmt.Errorf("failed to set %s: %w", proc, err)
}
return nil
}
// ExecPing execute /usr/bin/ping
//
// This is usefull when engine is not yet running and you need to populate the local arp/ndp cache
// If passing an IPv6 LLA, then must pass the scope as in "fe80::1%eth0"
func ExecPing(ip string) (err error) {
// -w deadline - wait 1 second
// -i frequency - one request each 0,2 seconds
// -c count - how many replies to receive before returning (in conjuction with -w)
cmd := exec.Command("/usr/bin/ping", ip, "-w", "1", "-i", "0.2", "-c", "1")
var stdout, stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
if err = cmd.Run(); err != nil {
// fmt.Printf("packet: failed to ping ip=%s error=%s\n", ip, err)
return err
}
// fmt.Printf("ping: %q\n", stdout.String())
// fmt.Printf("errs: %q\n", stderr.String())
return err
}
func LinuxConfigureInterface(nic string, hostIP netip.Prefix, newIP netip.Prefix, gw netip.Prefix) error {
if !hostIP.IsValid() || !newIP.IsValid() || !gw.IsValid() {
return fmt.Errorf("invalid ip prefix")
}
if hostIP.Addr() == newIP.Addr() {
return fmt.Errorf("host ip cannot be the same as new ip")
}
// Get a structure describing the network interface.
localInterface, err := netlink.LinkByName(nic)
if err != nil {
return err
}
// set new IP and mask - this will automatically set lifetime to forever.
ipConfig := &netlink.Addr{IPNet: &net.IPNet{IP: newIP.Addr().AsSlice(), Mask: net.CIDRMask(newIP.Bits(), 32)}}
if err = netlink.AddrAdd(localInterface, ipConfig); err != nil {
return fmt.Errorf("netlink failed to set new host ip=%s: %w", newIP, err)
}
if gw.Addr().Is4() {
// Setup the default route, so traffic that doesn't hit
// 192.168.1.(1-255) can be routed.
if err = netlink.RouteAdd(&netlink.Route{
Scope: netlink.SCOPE_UNIVERSE,
LinkIndex: localInterface.Attrs().Index,
Dst: &net.IPNet{IP: gw.Addr().AsSlice(), Mask: net.CIDRMask(gw.Bits(), 32)},
}); err != nil {
return err
}
}
// Lastly, bring up the interface.
if err = netlink.LinkSetUp(localInterface); err != nil {
return err
}
// delete previous IP
ipConfig = &netlink.Addr{IPNet: &net.IPNet{IP: hostIP.Addr().AsSlice(), Mask: net.CIDRMask(hostIP.Bits(), 32)}}
if err = netlink.AddrDel(localInterface, ipConfig); err != nil {
return fmt.Errorf("netlink failed to delete host ip=%s: %w", hostIP, err)
}
return nil
}
// ServerIsReacheable attemps to resolve "google.com" using the serverIP.
// It return nil if okay or error if server is unreachable.
func ServerIsReacheable(ctx context.Context, serverIP net.IP) (err error) {
r := &net.Resolver{
PreferGo: true,
StrictErrors: true,
Dial: func(ctx context.Context, network, address string) (net.Conn, error) {
d := net.Dialer{}
// return d.DialContext(ctx, "udp", "8.8.4.4:53")
return d.DialContext(ctx, "udp", fmt.Sprintf("%s:53", serverIP))
},
}
ctx2, cancel := context.WithTimeout(context.Background(), time.Second*5)
if ctx == nil {
ctx = ctx2
}
_, err = r.LookupHost(ctx, "google.com")
cancel()
return err
}