| 1 |
/* SPDX-License-Identifier: LGPL-2.1-only */ |
| 2 |
/* |
| 3 |
* Copyright (c) 2008 Thomas Graf <[email protected]> |
| 4 |
*/ |
| 5 |
|
| 6 |
#include "nl-default.h" |
| 7 |
|
| 8 |
#include <netlink/netlink.h> |
| 9 |
|
| 10 |
static const char *errmsg[NLE_MAX+1] = { |
| 11 |
[NLE_SUCCESS] = "Success", |
| 12 |
[NLE_FAILURE] = "Unspecific failure", |
| 13 |
[NLE_INTR] = "Interrupted system call", |
| 14 |
[NLE_BAD_SOCK] = "Bad socket", |
| 15 |
[NLE_AGAIN] = "Try again", |
| 16 |
[NLE_NOMEM] = "Out of memory", |
| 17 |
[NLE_EXIST] = "Object exists", |
| 18 |
[NLE_INVAL] = "Invalid input data or parameter", |
| 19 |
[NLE_RANGE] = "Input data out of range", |
| 20 |
[NLE_MSGSIZE] = "Message size not sufficient", |
| 21 |
[NLE_OPNOTSUPP] = "Operation not supported", |
| 22 |
[NLE_AF_NOSUPPORT] = "Address family not supported", |
| 23 |
[NLE_OBJ_NOTFOUND] = "Object not found", |
| 24 |
[NLE_NOATTR] = "Attribute not available", |
| 25 |
[NLE_MISSING_ATTR] = "Missing attribute", |
| 26 |
[NLE_AF_MISMATCH] = "Address family mismatch", |
| 27 |
[NLE_SEQ_MISMATCH] = "Message sequence number mismatch", |
| 28 |
[NLE_MSG_OVERFLOW] = "Kernel reported message overflow", |
| 29 |
[NLE_MSG_TRUNC] = "Kernel reported truncated message", |
| 30 |
[NLE_NOADDR] = "Invalid address for specified address family", |
| 31 |
[NLE_SRCRT_NOSUPPORT] = "Source based routing not supported", |
| 32 |
[NLE_MSG_TOOSHORT] = "Netlink message is too short", |
| 33 |
[NLE_MSGTYPE_NOSUPPORT] = "Netlink message type is not supported", |
| 34 |
[NLE_OBJ_MISMATCH] = "Object type does not match cache", |
| 35 |
[NLE_NOCACHE] = "Unknown or invalid cache type", |
| 36 |
[NLE_BUSY] = "Object busy", |
| 37 |
[NLE_PROTO_MISMATCH] = "Protocol mismatch", |
| 38 |
[NLE_NOACCESS] = "No Access", |
| 39 |
[NLE_PERM] = "Operation not permitted", |
| 40 |
[NLE_PKTLOC_FILE] = "Unable to open packet location file", |
| 41 |
[NLE_PARSE_ERR] = "Unable to parse object", |
| 42 |
[NLE_NODEV] = "No such device", |
| 43 |
[NLE_IMMUTABLE] = "Immutable attribute", |
| 44 |
[NLE_DUMP_INTR] = "Dump inconsistency detected, interrupted", |
| 45 |
[NLE_ATTRSIZE] = "Attribute max length exceeded", |
| 46 |
[NLE_HOSTUNREACH] = "Host is unreachable", |
| 47 |
[NLE_NETDOWN] = "Network is down", |
| 48 |
}; |
| 49 |
|
| 50 |
/** |
| 51 |
* Return error message for an error code |
| 52 |
* @return error message |
| 53 |
*/ |
| 54 |
const char *nl_geterror(int error) |
| 55 |
{ |
| 56 |
error = abs(error); |
| 57 |
|
| 58 |
if (error > NLE_MAX) |
| 59 |
error = NLE_FAILURE; |
| 60 |
|
| 61 |
return errmsg[error]; |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* Print a libnl error message |
| 66 |
* @arg s error message prefix |
| 67 |
* |
| 68 |
* Prints the error message of the call that failed last. |
| 69 |
* |
| 70 |
* If s is not NULL and *s is not a null byte the argument |
| 71 |
* string is printed, followed by a colon and a blank. Then |
| 72 |
* the error message and a new-line. |
| 73 |
*/ |
| 74 |
void nl_perror(int error, const char *s) |
| 75 |
{ |
| 76 |
if (s && *s) |
| 77 |
fprintf(stderr, "%s: %s\n", s, nl_geterror(error)); |
| 78 |
else |
| 79 |
fprintf(stderr, "%s\n", nl_geterror(error)); |
| 80 |
} |
| 81 |
|
| 82 |
int nl_syserr2nlerr(int error) |
| 83 |
{ |
| 84 |
error = abs(error); |
| 85 |
|
| 86 |
switch (error) { |
| 87 |
case EBADF: return NLE_BAD_SOCK; |
| 88 |
case EADDRINUSE: return NLE_EXIST; |
| 89 |
case EEXIST: return NLE_EXIST; |
| 90 |
case EADDRNOTAVAIL: return NLE_NOADDR; |
| 91 |
case ESRCH: /* fall through */ |
| 92 |
case ENOENT: return NLE_OBJ_NOTFOUND; |
| 93 |
case EINTR: return NLE_INTR; |
| 94 |
case EAGAIN: return NLE_AGAIN; |
| 95 |
case ENOTSOCK: return NLE_BAD_SOCK; |
| 96 |
case ENOPROTOOPT: return NLE_INVAL; |
| 97 |
case EFAULT: return NLE_INVAL; |
| 98 |
case EACCES: return NLE_NOACCESS; |
| 99 |
case EINVAL: return NLE_INVAL; |
| 100 |
case ENOBUFS: return NLE_NOMEM; |
| 101 |
case ENOMEM: return NLE_NOMEM; |
| 102 |
case EAFNOSUPPORT: return NLE_AF_NOSUPPORT; |
| 103 |
case EPROTONOSUPPORT: return NLE_PROTO_MISMATCH; |
| 104 |
case EOPNOTSUPP: return NLE_OPNOTSUPP; |
| 105 |
case EPERM: return NLE_PERM; |
| 106 |
case EBUSY: return NLE_BUSY; |
| 107 |
case ERANGE: return NLE_RANGE; |
| 108 |
case ENODEV: return NLE_NODEV; |
| 109 |
case EHOSTUNREACH: return NLE_HOSTUNREACH; |
| 110 |
case ENETDOWN: return NLE_NETDOWN; |
| 111 |
default: return NLE_FAILURE; |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
/** @} */ |