-
Notifications
You must be signed in to change notification settings - Fork 1
/
sctp_linux.go
575 lines (513 loc) · 14.2 KB
/
sctp_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
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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
//go:build linux && !386
// +build linux,!386
// Copyright 2019 Wataru Ishida. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package sctp
import (
"fmt"
"io"
"net"
"sync/atomic"
"syscall"
"unsafe"
"runtime"
)
func setsockopt(fd int, optname, optval, optlen uintptr) (uintptr, uintptr, error) {
// NOTE: syscall.SYS_SETSOCKOPT is undefined on 386
r0, r1, errno := syscall.Syscall6(syscall.SYS_SETSOCKOPT,
uintptr(fd),
SOL_SCTP,
optname,
optval,
optlen,
0)
if errno != 0 {
return r0, r1, errno
}
return r0, r1, nil
}
func getsockopt(fd int, optname, optval, optlen uintptr) (uintptr, uintptr, error) {
if runtime.GOARCH == "s390x" {
optlen = uintptr(unsafe.Pointer(&optlen))
}
// NOTE: syscall.SYS_GETSOCKOPT is undefined on 386
r0, r1, errno := syscall.Syscall6(syscall.SYS_GETSOCKOPT,
uintptr(fd),
SOL_SCTP,
optname,
optval,
optlen,
0)
if errno != 0 {
return r0, r1, errno
}
return r0, r1, nil
}
type rawConn struct {
sockfd int
}
func (r rawConn) Control(f func(fd uintptr)) error {
f(uintptr(r.sockfd))
return nil
}
func (r rawConn) Read(f func(fd uintptr) (done bool)) error {
panic("not implemented")
}
func (r rawConn) Write(f func(fd uintptr) (done bool)) error {
panic("not implemented")
}
func (c *SCTPConn) SCTPWrite(b []byte, info *SndRcvInfo) (int, error) {
var cbuf []byte
if info != nil {
cmsgBuf := toBuf(info)
hdr := &syscall.Cmsghdr{
Level: syscall.IPPROTO_SCTP,
Type: SCTP_CMSG_SNDRCV,
}
// bitwidth of hdr.Len is platform-specific,
// so we use hdr.SetLen() rather than directly setting hdr.Len
hdr.SetLen(syscall.CmsgSpace(len(cmsgBuf)))
cbuf = append(toBuf(hdr), cmsgBuf...)
}
return syscall.SendmsgN(c.fd(), b, cbuf, nil, 0)
}
func parseSndRcvInfo(b []byte) (*SndRcvInfo, error) {
msgs, err := syscall.ParseSocketControlMessage(b)
if err != nil {
return nil, err
}
for _, m := range msgs {
if m.Header.Level == syscall.IPPROTO_SCTP {
switch m.Header.Type {
case SCTP_CMSG_SNDRCV:
return (*SndRcvInfo)(unsafe.Pointer(&m.Data[0])), nil
}
}
}
return nil, nil
}
func parseNotification(b []byte) Notification {
snType := SCTPNotificationType(nativeEndian.Uint16(b[:2]))
switch snType {
case SCTP_SHUTDOWN_EVENT:
notification := SCTPShutdownEvent{
sseType: nativeEndian.Uint16(b[:2]),
sseFlags: nativeEndian.Uint16(b[2:4]),
sseLength: nativeEndian.Uint32(b[4:8]),
sseAssocID: SCTPAssocID(nativeEndian.Uint32(b[8:])),
}
return ¬ification
case SCTP_ASSOC_CHANGE:
notification := SCTPAssocChangeEvent{
sacType: nativeEndian.Uint16(b[:2]),
sacFlags: nativeEndian.Uint16(b[2:4]),
sacLength: nativeEndian.Uint32(b[4:8]),
sacState: SCTPState(nativeEndian.Uint16(b[8:10])),
sacError: nativeEndian.Uint16(b[10:12]),
sacOutboundStreams: nativeEndian.Uint16(b[12:14]),
sacInboundStreams: nativeEndian.Uint16(b[14:16]),
sacAssocID: SCTPAssocID(nativeEndian.Uint32(b[16:20])),
sacInfo: b[20:],
}
return ¬ification
default:
return nil
}
}
// SCTPRead use syscall.Recvmsg to receive SCTP message and return sctp sndrcvinfo/notification if need
func (c *SCTPConn) SCTPRead(b []byte) (int, *SndRcvInfo, Notification, error) {
oob := make([]byte, 254)
n, oobn, recvflags, _, err := syscall.Recvmsg(c.fd(), b, oob, 0)
if err != nil {
return n, nil, nil, err
}
if n == 0 && oobn == 0 {
return 0, nil, nil, io.EOF
}
if recvflags&MSG_NOTIFICATION > 0 {
notification := parseNotification(b[:n])
return n, nil, notification, nil
} else {
var info *SndRcvInfo
if oobn > 0 {
info, err = parseSndRcvInfo(oob[:oobn])
}
return n, info, nil, err
}
}
func (c *SCTPConn) Close() error {
if c != nil {
fd := atomic.SwapInt32(&c._fd, -1)
if fd > 0 {
info := &SndRcvInfo{
Flags: SCTP_EOF,
}
_, err := c.SCTPWrite(nil, info)
if err != nil {
fmt.Printf("SCTPConn: SCTPWrite failed %v\n", err)
}
err = syscall.Shutdown(int(fd), syscall.SHUT_RDWR)
if err != nil {
fmt.Printf("SCTPConn: Shutdown fd failed %v\n", err)
}
return syscall.Close(int(fd))
}
}
return syscall.EBADF
}
func (c *SCTPConn) SetWriteBuffer(bytes int) error {
return syscall.SetsockoptInt(c.fd(), syscall.SOL_SOCKET, syscall.SO_SNDBUF, bytes)
}
func (c *SCTPConn) GetWriteBuffer() (int, error) {
return syscall.GetsockoptInt(c.fd(), syscall.SOL_SOCKET, syscall.SO_SNDBUF)
}
func (c *SCTPConn) SetReadBuffer(bytes int) error {
return syscall.SetsockoptInt(c.fd(), syscall.SOL_SOCKET, syscall.SO_RCVBUF, bytes)
}
func (c *SCTPConn) GetReadBuffer() (int, error) {
return syscall.GetsockoptInt(c.fd(), syscall.SOL_SOCKET, syscall.SO_RCVBUF)
}
func (c *SCTPConn) SetWriteTimeout(tv syscall.Timeval) error {
return syscall.SetsockoptTimeval(c.fd(), syscall.SOL_SOCKET, syscall.SO_SNDTIMEO, &tv)
}
func (c *SCTPConn) SetReadTimeout(tv syscall.Timeval) error {
return syscall.SetsockoptTimeval(c.fd(), syscall.SOL_SOCKET, syscall.SO_RCVTIMEO, &tv)
}
func (c *SCTPConn) SetNonBlock(nonBlock bool) error {
return syscall.SetNonblock(c.fd(), nonBlock)
}
func (c *SCTPConn) GetRtoInfo() (*RtoInfo, error) {
return getRtoInfo(c.fd())
}
func (c *SCTPConn) SetRtoInfo(rtoInfo RtoInfo) error {
return setRtoInfo(c.fd(), rtoInfo)
}
func (c *SCTPConn) GetAssocInfo() (*AssocInfo, error) {
return getAssocInfo(c.fd())
}
func (c *SCTPConn) SetAssocInfo(info AssocInfo) error {
return setAssocInfo(c.fd(), info)
}
func (c *SCTPConn) GetMaxSegSize() (*int, error) {
return getMaxSegSize(c.fd())
}
func (c *SCTPConn) SetMaxSegSize(size int) error {
return setMaxSegSize(c.fd(), size)
}
// ListenSCTP - start listener on specified address/port
func ListenSCTP(net string, laddr *SCTPAddr) (*SCTPListener, error) {
return ListenSCTPExt(net, laddr, InitMsg{NumOstreams: SCTP_MAX_STREAM}, nil, nil, SCTP_DEFAULT_MAXSEG)
}
// ListenSCTPExt - start listener on specified address/port with given SCTP options
func ListenSCTPExt(
network string,
laddr *SCTPAddr,
options InitMsg,
rtoInfo *RtoInfo,
assocInfo *AssocInfo,
maxSeg int,
) (*SCTPListener, error) {
return listenSCTPExtConfig(network, laddr, options, rtoInfo, assocInfo, maxSeg, nil)
}
// listenSCTPExtConfig - start listener on specified address/port with given SCTP options and socket configuration
func listenSCTPExtConfig(
network string,
laddr *SCTPAddr,
options InitMsg,
rtoInfo *RtoInfo,
assocInfo *AssocInfo,
maxSeg int,
control func(network, address string, c syscall.RawConn) error,
) (*SCTPListener, error) {
af, ipv6only := favoriteAddrFamily(network, laddr, nil, "listen")
sock, err := syscall.Socket(
af,
syscall.SOCK_STREAM|syscall.SOCK_NONBLOCK|syscall.SOCK_CLOEXEC,
syscall.IPPROTO_SCTP,
)
if err != nil {
return nil, err
}
// close socket on error
defer func() {
if err != nil {
err2 := syscall.Close(sock)
if err2 != nil {
fmt.Printf("listenSCTPExtConfig: close sock failed %v", err2)
}
}
}()
if err = setDefaultSockopts(sock, af, ipv6only); err != nil {
return nil, err
}
// enable REUSEADDR option
if err = syscall.SetsockoptInt(sock, syscall.SOL_SOCKET, syscall.SO_REUSEADDR, 1); err != nil {
return nil, err
}
if control != nil {
rc := rawConn{sockfd: sock}
if err = control(network, laddr.String(), rc); err != nil {
return nil, err
}
}
// RTO
if rtoInfo != nil {
err = setRtoInfo(sock, *rtoInfo)
if err != nil {
return nil, err
}
}
// MAXSEG
if maxSeg > 0 {
if err = setMaxSegSize(sock, maxSeg); err != nil {
return nil, err
}
}
// set default association parameters (RFC 6458 8.1.2)
if assocInfo != nil {
err = setAssocInfo(sock, *assocInfo)
if err != nil {
return nil, err
}
}
err = setInitOpts(sock, options)
if err != nil {
return nil, err
}
if laddr != nil {
// If IP address and/or port was not provided so far, let's use the unspecified IPv4 or IPv6 address
if len(laddr.IPAddrs) == 0 {
if af == syscall.AF_INET {
laddr.IPAddrs = append(laddr.IPAddrs, net.IPAddr{IP: net.IPv4zero})
} else if af == syscall.AF_INET6 {
laddr.IPAddrs = append(laddr.IPAddrs, net.IPAddr{IP: net.IPv6zero})
}
}
err = SCTPBind(sock, laddr, SCTP_BINDX_ADD_ADDR)
if err != nil {
return nil, err
}
}
err = syscall.Listen(sock, syscall.SOMAXCONN)
if err != nil {
return nil, err
}
// epoll will be used in Accept() to avoid busy waiting because of non-blocking socket
epfd, err := createEpollForSock(sock)
if err != nil {
return nil, err
}
return &SCTPListener{
fd: sock,
epfd: epfd,
cancel: make(chan struct{}),
}, nil
}
// createEpollForSock - create an epoll for sock; return an epoll fd if no error
func createEpollForSock(sock int) (int, error) {
epfd, err := syscall.EpollCreate1(syscall.EPOLL_CLOEXEC)
if err != nil {
return -1, err
}
// close epfd on error
defer func() {
if err != nil {
err2 := syscall.Close(epfd)
if err2 != nil {
fmt.Printf("listenSCTPExtConfig: close sock failed %v", err2)
}
}
}()
event := syscall.EpollEvent{
Events: syscall.EPOLLIN,
Fd: int32(sock),
}
err = syscall.EpollCtl(epfd, syscall.EPOLL_CTL_ADD, sock, &event)
if err != nil {
return -1, err
}
return epfd, nil
}
func (ln *SCTPListener) IsStopped() bool {
return ln.isStopped.Load()
}
// AcceptSCTP waits for and returns the next SCTP connection to the listener.
// it will use EpollWait to wait for a incoming connection then call syscall.Accept4 to accept
// user can set timeout for cancel to be done if SCTPListener.Close() is execulated
func (ln *SCTPListener) AcceptSCTP(timeout int) (*SCTPConn, error) {
var events [1]syscall.EpollEvent
for {
select {
case <-ln.cancel:
ln.isStopped.Store(true)
return nil, nil // Exit signal received, return with no error.
default:
n, err := syscall.EpollWait(ln.epfd, events[:], timeout)
if err != nil {
if err == syscall.EBADF {
ln.isStopped.Store(true)
return nil, nil // EpollWait() was canceled, return with no error.
}
return nil, err // Other error occurred, return the error.
}
if n == 0 {
continue
}
if events[0].Fd == int32(ln.fd) {
fd, _, err := syscall.Accept4(ln.fd, 0)
return NewSCTPConn(fd, nil), err
}
}
}
}
// Accept waits for and returns the next connection connection to the listener.
func (ln *SCTPListener) Accept(timeout int) (net.Conn, error) {
return ln.AcceptSCTP(timeout)
}
func (ln *SCTPListener) Close() error {
err := syscall.Shutdown(ln.fd, syscall.SHUT_RDWR)
if err != nil {
fmt.Printf("SCTP: Failed to shutdown fd %v\n", err)
}
err = syscall.Close(ln.epfd)
if err != nil {
fmt.Printf("SCTP: Failed to close epfd %v\n", err)
}
err = syscall.Close(ln.fd)
if err != nil {
fmt.Printf("SCTP: Failed to close fd %v\n", err)
}
select {
case ln.cancel <- struct{}{}:
default:
}
return nil
}
// DialSCTP - bind socket to laddr (if given) and connect to raddr
func DialSCTP(net string, laddr, raddr *SCTPAddr) (*SCTPConn, error) {
return DialSCTPExt(
net,
laddr,
raddr,
InitMsg{NumOstreams: SCTP_MAX_STREAM},
nil,
nil,
SCTP_DEFAULT_MAXSEG,
)
}
// DialSCTPExt - same as DialSCTP but with given SCTP options
func DialSCTPExt(
network string,
laddr, raddr *SCTPAddr,
options InitMsg,
rtoInfo *RtoInfo,
assocInfo *AssocInfo,
maxSeg int,
) (*SCTPConn, error) {
return dialSCTPExtConfig(network, laddr, raddr, options, rtoInfo, assocInfo, maxSeg, nil)
}
// dialSCTPExtConfig - same as DialSCTP but with given SCTP options and socket configuration
func dialSCTPExtConfig(
network string,
laddr, raddr *SCTPAddr,
options InitMsg,
rtoInfo *RtoInfo,
assocInfo *AssocInfo,
maxSeg int,
control func(network, address string, c syscall.RawConn) error,
) (*SCTPConn, error) {
af, ipv6only := favoriteAddrFamily(network, laddr, raddr, "dial")
sock, err := syscall.Socket(
af,
syscall.SOCK_STREAM,
syscall.IPPROTO_SCTP,
)
if err != nil {
return nil, err
}
// close socket on error
defer func() {
if err != nil {
err2 := syscall.Close(sock)
if err2 != nil {
fmt.Printf("listenSCTPExtConfig: close sock failed %v", err2)
}
}
}()
if err = setDefaultSockopts(sock, af, ipv6only); err != nil {
return nil, err
}
if control != nil {
rc := rawConn{sockfd: sock}
if err = control(network, laddr.String(), rc); err != nil {
return nil, err
}
}
// RTO
if rtoInfo != nil {
err = setRtoInfo(sock, *rtoInfo)
if err != nil {
return nil, err
}
}
// AssocInfo
if assocInfo != nil {
err = setAssocInfo(sock, *assocInfo)
if err != nil {
return nil, err
}
}
// MAXSEG
if maxSeg > 0 {
if err = setMaxSegSize(sock, maxSeg); err != nil {
return nil, err
}
}
err = setInitOpts(sock, options)
if err != nil {
return nil, err
}
if laddr != nil {
// If IP address and/or port was not provided so far, let's use the unspecified IPv4 or IPv6 address
if len(laddr.IPAddrs) == 0 {
if af == syscall.AF_INET {
laddr.IPAddrs = append(laddr.IPAddrs, net.IPAddr{IP: net.IPv4zero})
} else if af == syscall.AF_INET6 {
laddr.IPAddrs = append(laddr.IPAddrs, net.IPAddr{IP: net.IPv6zero})
}
}
err = SCTPBind(sock, laddr, SCTP_BINDX_ADD_ADDR)
if err != nil {
return nil, err
}
}
_, err = SCTPConnect(sock, raddr)
switch err {
/* Asynchronous preemption introduced in go1.14 can allow syscalls to be preempted with EINTR errors.
* Upon preemption, SA_RESTART may lead getsockopt to execute twice, which leads to
* the return of EISCONN, EALREADY, or EINPROGRESS on the previously connected socket.
* We still return the socket fd, but preserve the error and let the client-side decide
* whether these errors should be deemed as error or not.
*/
case syscall.EISCONN, syscall.EALREADY, syscall.EINPROGRESS:
retErr := err
err = nil // Prevent socket close by defer function on these errors
return NewSCTPConn(sock, nil), retErr
case nil:
return NewSCTPConn(sock, nil), nil
default:
return nil, err
}
}