gvisor.dev/gvisor@v0.0.0-20240520182842-f9d4d51c7e0f/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  	"gvisor.dev/gvisor/pkg/abi/linux/errno"
    21  	"gvisor.dev/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  	ErrMulticastInputCannotBeOutput = New((&tcpip.ErrMulticastInputCannotBeOutput{}).String(), errno.EINVAL)
    56  	ErrMissingRequiredFields        = New((&tcpip.ErrMissingRequiredFields{}).String(), errno.EINVAL)
    57  	ErrNoNet                        = New((&tcpip.ErrNoNet{}).String(), errno.ENONET)
    58  )
    59  
    60  // TranslateNetstackError converts an error from the tcpip package to a sentry
    61  // internal error.
    62  func TranslateNetstackError(err tcpip.Error) *Error {
    63  	switch err.(type) {
    64  	case nil:
    65  		return nil
    66  	case *tcpip.ErrUnknownProtocol:
    67  		return ErrUnknownProtocol
    68  	case *tcpip.ErrUnknownNICID:
    69  		return ErrUnknownNICID
    70  	case *tcpip.ErrUnknownDevice:
    71  		return ErrUnknownDevice
    72  	case *tcpip.ErrUnknownProtocolOption:
    73  		return ErrUnknownProtocolOption
    74  	case *tcpip.ErrDuplicateNICID:
    75  		return ErrDuplicateNICID
    76  	case *tcpip.ErrDuplicateAddress:
    77  		return ErrDuplicateAddress
    78  	case *tcpip.ErrHostUnreachable:
    79  		return ErrHostUnreachable
    80  	case *tcpip.ErrHostDown:
    81  		return ErrHostDown
    82  	case *tcpip.ErrNoNet:
    83  		return ErrNoNet
    84  	case *tcpip.ErrAlreadyBound:
    85  		return ErrAlreadyBound
    86  	case *tcpip.ErrInvalidEndpointState:
    87  		return ErrInvalidEndpointState
    88  	case *tcpip.ErrAlreadyConnecting:
    89  		return ErrAlreadyConnecting
    90  	case *tcpip.ErrAlreadyConnected:
    91  		return ErrAlreadyConnected
    92  	case *tcpip.ErrNoPortAvailable:
    93  		return ErrNoPortAvailable
    94  	case *tcpip.ErrPortInUse:
    95  		return ErrPortInUse
    96  	case *tcpip.ErrBadLocalAddress:
    97  		return ErrBadLocalAddress
    98  	case *tcpip.ErrClosedForSend:
    99  		return ErrClosedForSend
   100  	case *tcpip.ErrClosedForReceive:
   101  		return ErrClosedForReceive
   102  	case *tcpip.ErrWouldBlock:
   103  		return ErrWouldBlock
   104  	case *tcpip.ErrConnectionRefused:
   105  		return ErrConnectionRefused
   106  	case *tcpip.ErrTimeout:
   107  		return ErrTimeout
   108  	case *tcpip.ErrAborted:
   109  		return ErrAborted
   110  	case *tcpip.ErrConnectStarted:
   111  		return ErrConnectStarted
   112  	case *tcpip.ErrDestinationRequired:
   113  		return ErrDestinationRequired
   114  	case *tcpip.ErrNotSupported:
   115  		return ErrNotSupported
   116  	case *tcpip.ErrQueueSizeNotSupported:
   117  		return ErrQueueSizeNotSupported
   118  	case *tcpip.ErrNotConnected:
   119  		return ErrNotConnected
   120  	case *tcpip.ErrConnectionReset:
   121  		return ErrConnectionReset
   122  	case *tcpip.ErrConnectionAborted:
   123  		return ErrConnectionAborted
   124  	case *tcpip.ErrNoSuchFile:
   125  		return ErrNoSuchFile
   126  	case *tcpip.ErrInvalidOptionValue:
   127  		return ErrInvalidOptionValue
   128  	case *tcpip.ErrBadAddress:
   129  		return ErrBadAddress
   130  	case *tcpip.ErrNetworkUnreachable:
   131  		return ErrNetworkUnreachable
   132  	case *tcpip.ErrMessageTooLong:
   133  		return ErrMessageTooLong
   134  	case *tcpip.ErrNoBufferSpace:
   135  		return ErrNoBufferSpace
   136  	case *tcpip.ErrBroadcastDisabled:
   137  		return ErrBroadcastDisabled
   138  	case *tcpip.ErrNotPermitted:
   139  		return ErrNotPermittedNet
   140  	case *tcpip.ErrAddressFamilyNotSupported:
   141  		return ErrAddressFamilyNotSupported
   142  	case *tcpip.ErrBadBuffer:
   143  		return ErrBadBuffer
   144  	case *tcpip.ErrMalformedHeader:
   145  		return ErrMalformedHeader
   146  	case *tcpip.ErrInvalidPortRange:
   147  		return ErrInvalidPortRange
   148  	case *tcpip.ErrMulticastInputCannotBeOutput:
   149  		return ErrMulticastInputCannotBeOutput
   150  	case *tcpip.ErrMissingRequiredFields:
   151  		return ErrMissingRequiredFields
   152  	default:
   153  		panic(fmt.Sprintf("unknown error %T", err))
   154  	}
   155  }
   156  
   157  // LINT.ThenChange(../tcpip/errors.go)