netlink.go 3.2 KB

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