-
Notifications
You must be signed in to change notification settings - Fork 0
/
neigh_openbsd.go
151 lines (129 loc) · 4.61 KB
/
neigh_openbsd.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
// {{{ Copyright (c) Paul R. Tagliamonte <paul@k3xec.com>, 2022
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE. }}}
//go:build openbsd
package tap
import (
"net"
"os"
"unsafe"
"golang.org/x/sys/unix"
)
// Address order for RTA_* additions.
//
// RTA_DST = 0x1
// RTA_GATEWAY = 0x2
// RTA_NETMASK = 0x4
// RTA_GENMASK = 0x8
// RTA_IFP = 0x10
// RTA_IFA = 0x20
// RTA_AUTHOR = 0x40
// RTA_BRD = 0x80
// RTA_SRC = 0x100
// RTA_SRCMASK = 0x200
// RTA_LABEL = 0x400
const (
sizeofRtMsgData = unix.SizeofSockaddrAny * 4
sizeofRtMsg = unix.SizeofRtMsghdr + sizeofRtMsgData
)
type rtMsg struct {
hdr unix.RtMsghdr
data [sizeofRtMsgData]byte
}
// newNeighRtMsg will create a RTM_ADD rtMsg, which expects two addresses,
// the RTA_DST, and the RTA_GATEWAY. This assumes a HOST (non-local) and
// STATIC route (ARP/NDP).
func newNeighRtMsg() ([]byte, *rtMsg) {
rtMsgBytes := make([]byte, sizeofRtMsg)
msg := (*rtMsg)(unsafe.Pointer(&rtMsgBytes[0]))
hdr := &msg.hdr
hdr.Version = unix.RTM_VERSION
hdr.Type = unix.RTM_ADD
hdr.Flags = unix.RTF_HOST | unix.RTF_STATIC
hdr.Addrs = 0
hdr.Pid = int32(os.Getpid())
hdr.Inits = unix.RTV_EXPIRE
hdr.Seq = 1
return rtMsgBytes, msg
}
func (i Interface) addNeighborIPv4(mac net.HardwareAddr, ip net.IP) error {
netif := i.ps.netif
rtMsgBytes, msg := newNeighRtMsg()
msg.hdr.Index = uint16(netif.Index)
// TODO(paultag): Expose some knobs to allow for expiring routes.
// hdr.Rmx = unix.RtMetrics{
// Expire: time.Now().Add(time.Second * 10).Unix(),
// }
idx := 0
ip4 := rawSockaddrInet4(ip.To4())
msg.hdr.Addrs |= unix.RTA_DST
*(*unix.RawSockaddrInet4)(unsafe.Pointer(&msg.data[idx])) = ip4
idx += int(ip4.Len + (ip4.Len % 8))
dl := rawSockaddrDatalink(mac)
dl.Index = uint16(netif.Index)
msg.hdr.Addrs |= unix.RTA_GATEWAY
*(*unix.RawSockaddrDatalink)(unsafe.Pointer(&msg.data[idx])) = dl
idx += int(dl.Len + (dl.Len % 8))
msg.hdr.Msglen = uint16(unix.SizeofRtMsghdr + idx)
msg.hdr.Hdrlen = uint16(unix.SizeofRtMsghdr)
file, err := afRoute()
if err != nil {
return err
}
defer file.Close()
_, err = file.Write(rtMsgBytes[:msg.hdr.Msglen])
return err
}
func (i Interface) addNeighborIPv6(mac net.HardwareAddr, ip net.IP) error {
netif := i.ps.netif
rtMsgBytes, msg := newNeighRtMsg()
// msg.hdr.Index = uint16(netif.Index)
// // TODO(paultag): Expose some knobs to allow for expiring routes.
// msg.hdr.Rmx = unix.RtMetrics{
// Expire: time.Now().Add(time.Second * 10).Unix(),
// }
idx := 0
msg.hdr.Addrs |= unix.RTA_DST
ip6 := rawSockaddrInet6(ip.To16())
// ip6.Scope_id = uint32(netif.Index)
*(*unix.RawSockaddrInet6)(unsafe.Pointer(&msg.data[idx])) = ip6
idx += int(ip6.Len + (ip6.Len % 8))
dl := rawSockaddrDatalink(mac)
dl.Index = uint16(netif.Index)
msg.hdr.Addrs |= unix.RTA_GATEWAY
*(*unix.RawSockaddrDatalink)(unsafe.Pointer(&msg.data[idx])) = dl
idx += int(dl.Len + (dl.Len % 8))
msg.hdr.Msglen = uint16(unix.SizeofRtMsghdr + idx)
msg.hdr.Hdrlen = uint16(unix.SizeofRtMsghdr)
file, err := afRoute()
if err != nil {
return err
}
defer file.Close()
_, err = file.Write(rtMsgBytes[:msg.hdr.Msglen])
return err
}
// AddNeighbor will add an entry into the ARP / NDP table
func (i Interface) AddNeighbor(mac net.HardwareAddr, ip net.IP) error {
if ip4 := ip.To4(); len(ip4) == net.IPv4len {
return i.addNeighborIPv4(mac, ip4)
}
return i.addNeighborIPv6(mac, ip)
}
// vim: foldmethod=marker