-
Notifications
You must be signed in to change notification settings - Fork 0
/
conn_linux.go
209 lines (176 loc) · 5.27 KB
/
conn_linux.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
package rtnetlink
import (
"os"
"syscall"
"time"
"unsafe"
"golang.org/x/sys/unix"
)
// Conn is a netlink connection.
type Conn struct {
file *os.File
rawConn syscall.RawConn
}
// Open opens a netlink connection.
func Open(groups uint32) (*Conn, error) {
fd, err := unix.Socket(unix.AF_NETLINK, unix.SOCK_RAW|unix.SOCK_NONBLOCK|unix.SOCK_CLOEXEC, unix.NETLINK_ROUTE)
if err != nil {
return nil, os.NewSyscallError("socket", err)
}
if err = setupSocket(fd, groups); err != nil {
_ = unix.Close(fd)
return nil, err
}
f := os.NewFile(uintptr(fd), "netlink")
rawConn, err := f.SyscallConn()
if err != nil {
_ = f.Close()
return nil, err
}
return &Conn{
file: f,
rawConn: rawConn,
}, nil
}
// Close closes the netlink connection.
func (c *Conn) Close() error {
return c.file.Close()
}
// SetDeadline sets the read and write deadlines associated with the connection.
func (c *Conn) SetDeadline(t time.Time) error {
return c.file.SetDeadline(t)
}
// SetReadDeadline sets the read deadline associated with the connection.
func (c *Conn) SetReadDeadline(t time.Time) error {
return c.file.SetReadDeadline(t)
}
// SetWriteDeadline sets the write deadline associated with the connection.
func (c *Conn) SetWriteDeadline(t time.Time) error {
return c.file.SetWriteDeadline(t)
}
// NewRConn returns the connection wrapped in a new [RConn] for reading.
func (c *Conn) NewRConn() *RConn {
rc := &RConn{
Conn: *c,
}
rc.rawReadFunc = func(fd uintptr) (done bool) {
var errno syscall.Errno
rc.readN, errno = recvmsg(int(fd), rc.readMsg, rc.readFlags)
switch errno {
case 0:
rc.readErr = nil
case syscall.EAGAIN:
return false
default:
rc.readErr = os.NewSyscallError("recvmsg", errno)
}
return true
}
return rc
}
// NewWConn returns the connection wrapped in a new [WConn] for writing.
func (c *Conn) NewWConn() *WConn {
wc := &WConn{
Conn: *c,
}
wc.rawWriteFunc = func(fd uintptr) (done bool) {
var errno syscall.Errno
wc.writeN, errno = sendmsg(int(fd), wc.writeMsg, wc.writeFlags)
switch errno {
case 0:
wc.writeErr = nil
case syscall.EAGAIN:
return false
default:
wc.writeErr = os.NewSyscallError("sendmsg", errno)
}
return true
}
return wc
}
// RConn provides read access to the netlink connection.
//
// RConn is not safe for concurrent use.
// Always create a new RConn for each goroutine.
type RConn struct {
Conn
rawReadFunc func(fd uintptr) (done bool)
readMsg *unix.Msghdr
readFlags int
readN int
readErr error
}
// ReadMsg reads a netlink message.
func (rc *RConn) ReadMsg(msg *unix.Msghdr, flags int) (n int, err error) {
rc.readMsg = msg
rc.readFlags = flags
if err = rc.rawConn.Read(rc.rawReadFunc); err != nil {
return 0, err
}
return rc.readN, rc.readErr
}
// WConn provides write access to the netlink connection.
//
// WConn is not safe for concurrent use.
// Always create a new WConn for each goroutine.
type WConn struct {
Conn
rawWriteFunc func(fd uintptr) (done bool)
writeMsg *unix.Msghdr
writeFlags int
writeN int
writeErr error
}
// WriteMsg writes a netlink message.
func (wc *WConn) WriteMsg(msg *unix.Msghdr, flags int) (n int, err error) {
wc.writeMsg = msg
wc.writeFlags = flags
if err = wc.rawConn.Write(wc.rawWriteFunc); err != nil {
return 0, err
}
return wc.writeN, wc.writeErr
}
func setupSocket(fd int, groups uint32) error {
// Set send buffer size to 32 KiB.
if err := unix.SetsockoptInt(fd, unix.SOL_SOCKET, unix.SO_SNDBUF, 32*1024); err != nil {
return os.NewSyscallError("setsockopt(SO_SNDBUF)", err)
}
// Set receive buffer size to 1 MiB.
if err := unix.SetsockoptInt(fd, unix.SOL_SOCKET, unix.SO_RCVBUF, 1024*1024); err != nil {
return os.NewSyscallError("setsockopt(SO_RCVBUF)", err)
}
// Set NETLINK_CAP_ACK to disable request echoing in ACK messages.
if err := unix.SetsockoptInt(fd, unix.SOL_NETLINK, unix.NETLINK_CAP_ACK, 1); err != nil {
return os.NewSyscallError("setsockopt(NETLINK_CAP_ACK)", err)
}
// Set NETLINK_EXT_ACK to receive extended ACK messages.
if err := unix.SetsockoptInt(fd, unix.SOL_NETLINK, unix.NETLINK_EXT_ACK, 1); err != nil {
return os.NewSyscallError("setsockopt(NETLINK_EXT_ACK)", err)
}
// Set NETLINK_GET_STRICT_CHK to enable strict input checking.
if err := unix.SetsockoptInt(fd, unix.SOL_NETLINK, unix.NETLINK_GET_STRICT_CHK, 1); err != nil {
return os.NewSyscallError("setsockopt(NETLINK_GET_STRICT_CHK)", err)
}
// Bind the socket to the netlink family and relevant groups.
if errno := bind(fd, &unix.RawSockaddrNetlink{
Family: unix.AF_NETLINK,
Groups: groups,
}); errno != 0 {
return os.NewSyscallError("bind", errno)
}
return nil
}
func bind(fd int, sa *unix.RawSockaddrNetlink) syscall.Errno {
_, _, errno := unix.Syscall(unix.SYS_BIND, uintptr(fd), uintptr(unsafe.Pointer(sa)), unix.SizeofSockaddrNetlink)
return errno
}
func msgSyscall(trap uintptr, fd int, msg *unix.Msghdr, flags int) (int, syscall.Errno) {
ret, _, errno := unix.Syscall(trap, uintptr(fd), uintptr(unsafe.Pointer(msg)), uintptr(flags))
return int(ret), errno
}
func recvmsg(fd int, msg *unix.Msghdr, flags int) (int, syscall.Errno) {
return msgSyscall(unix.SYS_RECVMSG, fd, msg, flags)
}
func sendmsg(fd int, msg *unix.Msghdr, flags int) (int, syscall.Errno) {
return msgSyscall(unix.SYS_SENDMSG, fd, msg, flags)
}