]> Cypherpunks.ru repositories - gostls13.git/blob - src/net/mptcpsock_linux.go
15a788249890af812ccef966314f1c285e9f2551
[gostls13.git] / src / net / mptcpsock_linux.go
1 // Copyright 2023 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package net
6
7 import (
8         "context"
9         "errors"
10         "internal/poll"
11         "sync"
12         "syscall"
13 )
14
15 var (
16         mptcpOnce      sync.Once
17         mptcpAvailable bool
18 )
19
20 // These constants aren't in the syscall package, which is frozen
21 const (
22         _IPPROTO_MPTCP = 0x106
23 )
24
25 func supportsMultipathTCP() bool {
26         mptcpOnce.Do(initMPTCPavailable)
27         return mptcpAvailable
28 }
29
30 // Check that MPTCP is supported by attemting to create an MPTCP socket and by
31 // looking at the returned error if any.
32 func initMPTCPavailable() {
33         s, err := sysSocket(syscall.AF_INET, syscall.SOCK_STREAM, _IPPROTO_MPTCP)
34         switch {
35         case errors.Is(err, syscall.EPROTONOSUPPORT): // Not supported: >= v5.6
36         case errors.Is(err, syscall.EINVAL): // Not supported: < v5.6
37         case err == nil: // Supported and no error
38                 poll.CloseFunc(s)
39                 fallthrough
40         default:
41                 // another error: MPTCP was not available but it might be later
42                 mptcpAvailable = true
43         }
44 }
45
46 func (sd *sysDialer) dialMPTCP(ctx context.Context, laddr, raddr *TCPAddr) (*TCPConn, error) {
47         if supportsMultipathTCP() {
48                 if conn, err := sd.doDialTCPProto(ctx, laddr, raddr, _IPPROTO_MPTCP); err == nil {
49                         return conn, nil
50                 }
51         }
52
53         // Fallback to dialTCP if Multipath TCP isn't supported on this operating
54         // system. But also fallback in case of any error with MPTCP.
55         //
56         // Possible MPTCP specific error: ENOPROTOOPT (sysctl net.mptcp.enabled=0)
57         // But just in case MPTCP is blocked differently (SELinux, etc.), just
58         // retry with "plain" TCP.
59         return sd.dialTCP(ctx, laddr, raddr)
60 }
61
62 func (sl *sysListener) listenMPTCP(ctx context.Context, laddr *TCPAddr) (*TCPListener, error) {
63         if supportsMultipathTCP() {
64                 if dial, err := sl.listenTCPProto(ctx, laddr, _IPPROTO_MPTCP); err == nil {
65                         return dial, nil
66                 }
67         }
68
69         // Fallback to listenTCP if Multipath TCP isn't supported on this operating
70         // system. But also fallback in case of any error with MPTCP.
71         //
72         // Possible MPTCP specific error: ENOPROTOOPT (sysctl net.mptcp.enabled=0)
73         // But just in case MPTCP is blocked differently (SELinux, etc.), just
74         // retry with "plain" TCP.
75         return sl.listenTCP(ctx, laddr)
76 }