-
Notifications
You must be signed in to change notification settings - Fork 2
/
raw_linux.go
287 lines (246 loc) · 7.52 KB
/
raw_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
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
// +build linux
package raw
import (
"net"
"os"
"sync"
"syscall"
"time"
"unsafe"
"golang.org/x/net/bpf"
"golang.org/x/sys/unix"
)
var (
// Must implement net.PacketConn at compile-time.
_ net.PacketConn = &packetConn{}
)
// packetConn is the Linux-specific implementation of net.PacketConn for this
// package.
type packetConn struct {
ifi *net.Interface
s socket
pbe uint16
// Timeouts set via Set{Read,}Deadline, guarded by mutex
timeoutMu sync.RWMutex
rtimeout time.Time
}
// socket is an interface which enables swapping out socket syscalls for
// testing.
type socket interface {
Bind(syscall.Sockaddr) error
Close() error
FD() int
Recvfrom([]byte, int) (int, syscall.Sockaddr, error)
Sendto([]byte, int, syscall.Sockaddr) error
SetSockopt(level, name int, v unsafe.Pointer, l uint32) error
SetTimeout(time.Duration) error
}
// listenPacket creates a net.PacketConn which can be used to send and receive
// data at the device driver level.
func listenPacket(ifi *net.Interface, proto uint16, cfg *Config) (*packetConn, error) {
// Convert proto to big endian.
pbe := htons(proto)
// Enabling overriding the socket type via config.
typ := syscall.SOCK_RAW
if cfg != nil && cfg.LinuxSockDGRAM {
typ = syscall.SOCK_DGRAM
}
// Open a packet socket using specified socket and protocol types.
sock, err := syscall.Socket(syscall.AF_PACKET, typ, int(pbe))
if err != nil {
return nil, err
}
// Wrap raw socket in socket interface.
return newPacketConn(
ifi,
&sysSocket{
fd: sock,
},
pbe,
)
}
// newPacketConn creates a net.PacketConn using the specified network
// interface, wrapped socket and big endian protocol number.
//
// It is the entry point for tests in this package.
func newPacketConn(ifi *net.Interface, s socket, pbe uint16) (*packetConn, error) {
// Bind the packet socket to the interface specified by ifi
// packet(7):
// Only the sll_protocol and the sll_ifindex address fields are used for
// purposes of binding.
err := s.Bind(&syscall.SockaddrLinklayer{
Protocol: pbe,
Ifindex: ifi.Index,
})
return &packetConn{
ifi: ifi,
s: s,
pbe: pbe,
}, err
}
// ReadFrom implements the net.PacketConn.ReadFrom method.
func (p *packetConn) ReadFrom(b []byte) (int, net.Addr, error) {
p.timeoutMu.Lock()
deadline := p.rtimeout
p.timeoutMu.Unlock()
// Information returned by syscall.Recvfrom
var n int
var addr syscall.Sockaddr
for {
var timeout time.Duration
if deadline.IsZero() {
timeout = readTimeout
} else {
timeout = deadline.Sub(time.Now())
if timeout > readTimeout {
timeout = readTimeout
}
}
err := p.s.SetTimeout(timeout)
if err != nil {
return 0, nil, err
}
// Attempt to receive on socket
// The recvfrom sycall will NOT be interrupted by closing of the socket
n, addr, err = p.s.Recvfrom(b, 0)
if err == syscall.EAGAIN {
// timeout
continue
}
if err != nil {
n = 0
// Return on error
return n, nil, err
}
// Got data, exit the loop
break
}
// Retrieve hardware address and other information from addr
sa, ok := addr.(*syscall.SockaddrLinklayer)
if !ok || sa.Halen < 6 {
return n, nil, syscall.EINVAL
}
// Use length specified to convert byte array into a hardware address slice
mac := make(net.HardwareAddr, sa.Halen)
copy(mac, sa.Addr[:])
// packet(7):
// sll_hatype and sll_pkttype are set on received packets for your
// information.
// TODO(mdlayher): determine if similar fields exist and are useful on
// non-Linux platforms
return n, &Addr{
HardwareAddr: mac,
}, nil
}
// WriteTo implements the net.PacketConn.WriteTo method.
func (p *packetConn) WriteTo(b []byte, addr net.Addr) (int, error) {
// Ensure correct Addr type
a, ok := addr.(*Addr)
if !ok || a.HardwareAddr == nil || len(a.HardwareAddr) < 6 {
return 0, syscall.EINVAL
}
// Convert hardware address back to byte array form
var baddr [8]byte
copy(baddr[:], a.HardwareAddr)
// Send message on socket to the specified hardware address from addr
// packet(7):
// When you send packets it is enough to specify sll_family, sll_addr,
// sll_halen, sll_ifindex, and sll_protocol. The other fields should
// be 0.
// In this case, sll_family is taken care of automatically by syscall.
err := p.s.Sendto(b, 0, &syscall.SockaddrLinklayer{
Ifindex: p.ifi.Index,
Halen: uint8(len(a.HardwareAddr)),
Addr: baddr,
Protocol: p.pbe,
})
return len(b), err
}
// Close closes the connection.
func (p *packetConn) Close() error {
return p.s.Close()
}
// LocalAddr returns the local network address.
func (p *packetConn) LocalAddr() net.Addr {
return &Addr{
HardwareAddr: p.ifi.HardwareAddr,
}
}
// TODO(mdlayher): it is unfortunate that we have to implement deadlines using
// a context, but it appears that there may not be a better solution until
// Go 1.6 or later. See here: https://github.com/golang/go/issues/10565.
// SetDeadline implements the net.PacketConn.SetDeadline method.
func (p *packetConn) SetDeadline(t time.Time) error {
return p.SetReadDeadline(t)
}
// SetReadDeadline implements the net.PacketConn.SetReadDeadline method.
func (p *packetConn) SetReadDeadline(t time.Time) error {
p.timeoutMu.Lock()
p.rtimeout = t
p.timeoutMu.Unlock()
return nil
}
// SetWriteDeadline implements the net.PacketConn.SetWriteDeadline method.
func (p *packetConn) SetWriteDeadline(t time.Time) error {
return nil
}
// SetBPF attaches an assembled BPF program to a raw net.PacketConn.
func (p *packetConn) SetBPF(filter []bpf.RawInstruction) error {
prog := syscall.SockFprog{
Len: uint16(len(filter)),
Filter: (*syscall.SockFilter)(unsafe.Pointer(&filter[0])),
}
err := p.s.SetSockopt(
syscall.SOL_SOCKET,
syscall.SO_ATTACH_FILTER,
unsafe.Pointer(&prog),
uint32(unsafe.Sizeof(prog)),
)
if err != nil {
return os.NewSyscallError("setsockopt", err)
}
return nil
}
// SetPromiscuous enables or disables promiscuous mode on the interface, allowing it
// to receive traffic that is not addressed to the interface.
func (p *packetConn) SetPromiscuous(b bool) error {
mreq := unix.PacketMreq{
Ifindex: int32(p.ifi.Index),
Type: unix.PACKET_MR_PROMISC,
}
membership := unix.PACKET_ADD_MEMBERSHIP
if !b {
membership = unix.PACKET_DROP_MEMBERSHIP
}
return p.s.SetSockopt(unix.SOL_PACKET, membership, unsafe.Pointer(&mreq), unix.SizeofPacketMreq)
}
// sysSocket is the default socket implementation. It makes use of
// Linux-specific system calls to handle raw socket functionality.
type sysSocket struct {
fd int
}
// Method implementations simply invoke the syscall of the same name, but pass
// the file descriptor stored in the sysSocket as the socket to use.
func (s *sysSocket) Bind(sa syscall.Sockaddr) error { return syscall.Bind(s.fd, sa) }
func (s *sysSocket) Close() error { return syscall.Close(s.fd) }
func (s *sysSocket) FD() int { return s.fd }
func (s *sysSocket) Recvfrom(p []byte, flags int) (int, syscall.Sockaddr, error) {
return syscall.Recvfrom(s.fd, p, flags)
}
func (s *sysSocket) Sendto(p []byte, flags int, to syscall.Sockaddr) error {
return syscall.Sendto(s.fd, p, flags, to)
}
func (s *sysSocket) SetSockopt(level, name int, v unsafe.Pointer, l uint32) error {
_, _, err := syscall.Syscall6(syscall.SYS_SETSOCKOPT, uintptr(s.fd), uintptr(level), uintptr(name), uintptr(v), uintptr(l), 0)
if err != 0 {
return syscall.Errno(err)
}
return nil
}
func (s *sysSocket) SetTimeout(timeout time.Duration) error {
tv, err := newTimeval(timeout)
if err != nil {
return err
}
return syscall.SetsockoptTimeval(s.fd, syscall.SOL_SOCKET, syscall.SO_RCVTIMEO, tv)
}