github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/pkg/syserr/netstack.go (about)

     1  // Copyright 2018 The gVisor Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package syserr
    16  
    17  import (
    18  	"fmt"
    19  
    20  	"github.com/SagerNet/gvisor/pkg/abi/linux/errno"
    21  	"github.com/SagerNet/gvisor/pkg/tcpip"
    22  )
    23  
    24  // LINT.IfChange
    25  
    26  // Mapping for tcpip.Error types.
    27  var (
    28  	ErrUnknownProtocol       = New((&tcpip.ErrUnknownProtocol{}).String(), errno.EINVAL)
    29  	ErrUnknownNICID          = New((&tcpip.ErrUnknownNICID{}).String(), errno.ENODEV)
    30  	ErrUnknownDevice         = New((&tcpip.ErrUnknownDevice{}).String(), errno.ENODEV)
    31  	ErrUnknownProtocolOption = New((&tcpip.ErrUnknownProtocolOption{}).String(), errno.ENOPROTOOPT)
    32  	ErrDuplicateNICID        = New((&tcpip.ErrDuplicateNICID{}).String(), errno.EEXIST)
    33  	ErrDuplicateAddress      = New((&tcpip.ErrDuplicateAddress{}).String(), errno.EEXIST)
    34  	ErrAlreadyBound          = New((&tcpip.ErrAlreadyBound{}).String(), errno.EINVAL)
    35  	ErrInvalidEndpointState  = New((&tcpip.ErrInvalidEndpointState{}).String(), errno.EINVAL)
    36  	ErrAlreadyConnecting     = New((&tcpip.ErrAlreadyConnecting{}).String(), errno.EALREADY)
    37  	ErrNoPortAvailable       = New((&tcpip.ErrNoPortAvailable{}).String(), errno.EAGAIN)
    38  	ErrPortInUse             = New((&tcpip.ErrPortInUse{}).String(), errno.EADDRINUSE)
    39  	ErrBadLocalAddress       = New((&tcpip.ErrBadLocalAddress{}).String(), errno.EADDRNOTAVAIL)
    40  	ErrClosedForSend         = New((&tcpip.ErrClosedForSend{}).String(), errno.EPIPE)
    41  	ErrClosedForReceive      = New((&tcpip.ErrClosedForReceive{}).String(), errno.NOERRNO)
    42  	ErrTimeout               = New((&tcpip.ErrTimeout{}).String(), errno.ETIMEDOUT)
    43  	ErrAborted               = New((&tcpip.ErrAborted{}).String(), errno.EPIPE)
    44  	ErrConnectStarted        = New((&tcpip.ErrConnectStarted{}).String(), errno.EINPROGRESS)
    45  	ErrDestinationRequired   = New((&tcpip.ErrDestinationRequired{}).String(), errno.EDESTADDRREQ)
    46  	ErrNotSupported          = New((&tcpip.ErrNotSupported{}).String(), errno.EOPNOTSUPP)
    47  	ErrQueueSizeNotSupported = New((&tcpip.ErrQueueSizeNotSupported{}).String(), errno.ENOTTY)
    48  	ErrNoSuchFile            = New((&tcpip.ErrNoSuchFile{}).String(), errno.ENOENT)
    49  	ErrInvalidOptionValue    = New((&tcpip.ErrInvalidOptionValue{}).String(), errno.EINVAL)
    50  	ErrBroadcastDisabled     = New((&tcpip.ErrBroadcastDisabled{}).String(), errno.EACCES)
    51  	ErrNotPermittedNet       = New((&tcpip.ErrNotPermitted{}).String(), errno.EPERM)
    52  	ErrBadBuffer             = New((&tcpip.ErrBadBuffer{}).String(), errno.EFAULT)
    53  	ErrMalformedHeader       = New((&tcpip.ErrMalformedHeader{}).String(), errno.EINVAL)
    54  	ErrInvalidPortRange      = New((&tcpip.ErrInvalidPortRange{}).String(), errno.EINVAL)
    55  )
    56  
    57  // TranslateNetstackError converts an error from the tcpip package to a sentry
    58  // internal error.
    59  func TranslateNetstackError(err tcpip.Error) *Error {
    60  	switch err.(type) {
    61  	case nil:
    62  		return nil
    63  	case *tcpip.ErrUnknownProtocol:
    64  		return ErrUnknownProtocol
    65  	case *tcpip.ErrUnknownNICID:
    66  		return ErrUnknownNICID
    67  	case *tcpip.ErrUnknownDevice:
    68  		return ErrUnknownDevice
    69  	case *tcpip.ErrUnknownProtocolOption:
    70  		return ErrUnknownProtocolOption
    71  	case *tcpip.ErrDuplicateNICID:
    72  		return ErrDuplicateNICID
    73  	case *tcpip.ErrDuplicateAddress:
    74  		return ErrDuplicateAddress
    75  	case *tcpip.ErrNoRoute:
    76  		return ErrNoRoute
    77  	case *tcpip.ErrAlreadyBound:
    78  		return ErrAlreadyBound
    79  	case *tcpip.ErrInvalidEndpointState:
    80  		return ErrInvalidEndpointState
    81  	case *tcpip.ErrAlreadyConnecting:
    82  		return ErrAlreadyConnecting
    83  	case *tcpip.ErrAlreadyConnected:
    84  		return ErrAlreadyConnected
    85  	case *tcpip.ErrNoPortAvailable:
    86  		return ErrNoPortAvailable
    87  	case *tcpip.ErrPortInUse:
    88  		return ErrPortInUse
    89  	case *tcpip.ErrBadLocalAddress:
    90  		return ErrBadLocalAddress
    91  	case *tcpip.ErrClosedForSend:
    92  		return ErrClosedForSend
    93  	case *tcpip.ErrClosedForReceive:
    94  		return ErrClosedForReceive
    95  	case *tcpip.ErrWouldBlock:
    96  		return ErrWouldBlock
    97  	case *tcpip.ErrConnectionRefused:
    98  		return ErrConnectionRefused
    99  	case *tcpip.ErrTimeout:
   100  		return ErrTimeout
   101  	case *tcpip.ErrAborted:
   102  		return ErrAborted
   103  	case *tcpip.ErrConnectStarted:
   104  		return ErrConnectStarted
   105  	case *tcpip.ErrDestinationRequired:
   106  		return ErrDestinationRequired
   107  	case *tcpip.ErrNotSupported:
   108  		return ErrNotSupported
   109  	case *tcpip.ErrQueueSizeNotSupported:
   110  		return ErrQueueSizeNotSupported
   111  	case *tcpip.ErrNotConnected:
   112  		return ErrNotConnected
   113  	case *tcpip.ErrConnectionReset:
   114  		return ErrConnectionReset
   115  	case *tcpip.ErrConnectionAborted:
   116  		return ErrConnectionAborted
   117  	case *tcpip.ErrNoSuchFile:
   118  		return ErrNoSuchFile
   119  	case *tcpip.ErrInvalidOptionValue:
   120  		return ErrInvalidOptionValue
   121  	case *tcpip.ErrBadAddress:
   122  		return ErrBadAddress
   123  	case *tcpip.ErrNetworkUnreachable:
   124  		return ErrNetworkUnreachable
   125  	case *tcpip.ErrMessageTooLong:
   126  		return ErrMessageTooLong
   127  	case *tcpip.ErrNoBufferSpace:
   128  		return ErrNoBufferSpace
   129  	case *tcpip.ErrBroadcastDisabled:
   130  		return ErrBroadcastDisabled
   131  	case *tcpip.ErrNotPermitted:
   132  		return ErrNotPermittedNet
   133  	case *tcpip.ErrAddressFamilyNotSupported:
   134  		return ErrAddressFamilyNotSupported
   135  	case *tcpip.ErrBadBuffer:
   136  		return ErrBadBuffer
   137  	case *tcpip.ErrMalformedHeader:
   138  		return ErrMalformedHeader
   139  	case *tcpip.ErrInvalidPortRange:
   140  		return ErrInvalidPortRange
   141  	default:
   142  		panic(fmt.Sprintf("unknown error %T", err))
   143  	}
   144  }
   145  
   146  // LINT.ThenChange(../tcpip/errors.go)