github.com/elastic/gosigar@v0.14.3/sys/linux/netlink.go (about)

     1  // +build linux
     2  
     3  package linux
     4  
     5  import (
     6  	"errors"
     7  
     8  	"github.com/elastic/gosigar/sys"
     9  )
    10  
    11  // Netlink Error Code Handling
    12  
    13  // ParseNetlinkError parses the errno from the data section of a
    14  // syscall.NetlinkMessage. If netlinkData is less than 4 bytes an error
    15  // describing the problem will be returned.
    16  func ParseNetlinkError(netlinkData []byte) error {
    17  	if len(netlinkData) >= 4 {
    18  		errno := -sys.GetEndian().Uint32(netlinkData[:4])
    19  		return NetlinkErrno(errno)
    20  	}
    21  	return errors.New("received netlink error (data too short to read errno)")
    22  }
    23  
    24  // NetlinkErrno represent the error code contained in a netlink message of
    25  // type NLMSG_ERROR.
    26  type NetlinkErrno uint32
    27  
    28  // Netlink error codes.
    29  const (
    30  	NLE_SUCCESS NetlinkErrno = iota
    31  	NLE_FAILURE
    32  	NLE_INTR
    33  	NLE_BAD_SOCK
    34  	NLE_AGAIN
    35  	NLE_NOMEM
    36  	NLE_EXIST
    37  	NLE_INVAL
    38  	NLE_RANGE
    39  	NLE_MSGSIZE
    40  	NLE_OPNOTSUPP
    41  	NLE_AF_NOSUPPORT
    42  	NLE_OBJ_NOTFOUND
    43  	NLE_NOATTR
    44  	NLE_MISSING_ATTR
    45  	NLE_AF_MISMATCH
    46  	NLE_SEQ_MISMATCH
    47  	NLE_MSG_OVERFLOW
    48  	NLE_MSG_TRUNC
    49  	NLE_NOADDR
    50  	NLE_SRCRT_NOSUPPORT
    51  	NLE_MSG_TOOSHORT
    52  	NLE_MSGTYPE_NOSUPPORT
    53  	NLE_OBJ_MISMATCH
    54  	NLE_NOCACHE
    55  	NLE_BUSY
    56  	NLE_PROTO_MISMATCH
    57  	NLE_NOACCESS
    58  	NLE_PERM
    59  	NLE_PKTLOC_FILE
    60  	NLE_PARSE_ERR
    61  	NLE_NODEV
    62  	NLE_IMMUTABLE
    63  	NLE_DUMP_INTR
    64  	NLE_ATTRSIZE
    65  )
    66  
    67  // https://github.com/thom311/libnl/blob/libnl3_2_28/lib/error.c
    68  var netlinkErrorMsgs = map[NetlinkErrno]string{
    69  	NLE_SUCCESS:           "Success",
    70  	NLE_FAILURE:           "Unspecific failure",
    71  	NLE_INTR:              "Interrupted system call",
    72  	NLE_BAD_SOCK:          "Bad socket",
    73  	NLE_AGAIN:             "Try again",
    74  	NLE_NOMEM:             "Out of memory",
    75  	NLE_EXIST:             "Object exists",
    76  	NLE_INVAL:             "Invalid input data or parameter",
    77  	NLE_RANGE:             "Input data out of range",
    78  	NLE_MSGSIZE:           "Message size not sufficient",
    79  	NLE_OPNOTSUPP:         "Operation not supported",
    80  	NLE_AF_NOSUPPORT:      "Address family not supported",
    81  	NLE_OBJ_NOTFOUND:      "Object not found",
    82  	NLE_NOATTR:            "Attribute not available",
    83  	NLE_MISSING_ATTR:      "Missing attribute",
    84  	NLE_AF_MISMATCH:       "Address family mismatch",
    85  	NLE_SEQ_MISMATCH:      "Message sequence number mismatch",
    86  	NLE_MSG_OVERFLOW:      "Kernel reported message overflow",
    87  	NLE_MSG_TRUNC:         "Kernel reported truncated message",
    88  	NLE_NOADDR:            "Invalid address for specified address family",
    89  	NLE_SRCRT_NOSUPPORT:   "Source based routing not supported",
    90  	NLE_MSG_TOOSHORT:      "Netlink message is too short",
    91  	NLE_MSGTYPE_NOSUPPORT: "Netlink message type is not supported",
    92  	NLE_OBJ_MISMATCH:      "Object type does not match cache",
    93  	NLE_NOCACHE:           "Unknown or invalid cache type",
    94  	NLE_BUSY:              "Object busy",
    95  	NLE_PROTO_MISMATCH:    "Protocol mismatch",
    96  	NLE_NOACCESS:          "No Access",
    97  	NLE_PERM:              "Operation not permitted",
    98  	NLE_PKTLOC_FILE:       "Unable to open packet location file",
    99  	NLE_PARSE_ERR:         "Unable to parse object",
   100  	NLE_NODEV:             "No such device",
   101  	NLE_IMMUTABLE:         "Immutable attribute",
   102  	NLE_DUMP_INTR:         "Dump inconsistency detected, interrupted",
   103  	NLE_ATTRSIZE:          "Attribute max length exceeded",
   104  }
   105  
   106  func (e NetlinkErrno) Error() string {
   107  	if msg, found := netlinkErrorMsgs[e]; found {
   108  		return msg
   109  	}
   110  
   111  	return netlinkErrorMsgs[NLE_FAILURE]
   112  }