| 1 |
/* SPDX-License-Identifier: LGPL-2.1-only */ |
| 2 |
/* |
| 3 |
* Copyright (c) 2003-2012 Thomas Graf <[email protected]> |
| 4 |
*/ |
| 5 |
|
| 6 |
/** |
| 7 |
* @defgroup core Core Library (libnl) |
| 8 |
* |
| 9 |
* Socket handling, connection management, sending and receiving of data, |
| 10 |
* message construction and parsing, object caching system, ... |
| 11 |
* |
| 12 |
* This is the API reference of the core library. It is not meant as a guide |
| 13 |
* but as a reference. Please refer to the core library guide for detailed |
| 14 |
* documentation on the library architecture and examples: |
| 15 |
* |
| 16 |
* * @ref_asciidoc{core,_,Netlink Core Library Development Guide} |
| 17 |
* |
| 18 |
* |
| 19 |
* @{ |
| 20 |
*/ |
| 21 |
|
| 22 |
#include "nl-default.h" |
| 23 |
|
| 24 |
#include <linux/socket.h> |
| 25 |
|
| 26 |
#include <netlink/netlink.h> |
| 27 |
#include <netlink/utils.h> |
| 28 |
#include <netlink/handlers.h> |
| 29 |
#include <netlink/msg.h> |
| 30 |
#include <netlink/attr.h> |
| 31 |
|
| 32 |
#include "nl-core.h" |
| 33 |
#include "nl-priv-dynamic-core/nl-core.h" |
| 34 |
#include "nl-aux-core/nl-core.h" |
| 35 |
#include "nl-priv-dynamic-core/cache-api.h" |
| 36 |
|
| 37 |
/** |
| 38 |
* @defgroup core_types Data Types |
| 39 |
* |
| 40 |
* Core library data types |
| 41 |
* @{ |
| 42 |
* @} |
| 43 |
* |
| 44 |
* @defgroup send_recv Send & Receive Data |
| 45 |
* |
| 46 |
* Connection management, sending & receiving of data |
| 47 |
* |
| 48 |
* Related sections in the development guide: |
| 49 |
* - @core_doc{core_send_recv, Sending & Receiving} |
| 50 |
* - @core_doc{core_sockets, Sockets} |
| 51 |
* |
| 52 |
* @{ |
| 53 |
* |
| 54 |
* Header |
| 55 |
* ------ |
| 56 |
* ~~~~{.c} |
| 57 |
* #include <netlink/netlink.h> |
| 58 |
* ~~~~ |
| 59 |
*/ |
| 60 |
|
| 61 |
/** |
| 62 |
* @name Connection Management |
| 63 |
* @{ |
| 64 |
*/ |
| 65 |
|
| 66 |
/** |
| 67 |
* Create file descriptor and bind socket. |
| 68 |
* @arg sk Netlink socket (required) |
| 69 |
* @arg protocol Netlink protocol to use (required) |
| 70 |
* |
| 71 |
* Creates a new Netlink socket using `socket()` and binds the socket to the |
| 72 |
* protocol and local port specified in the `sk` socket object. Fails if |
| 73 |
* the socket is already connected. |
| 74 |
* |
| 75 |
* @note If available, the `close-on-exec` (`SOCK_CLOEXEC`) feature is enabled |
| 76 |
* automatically on the new file descriptor. This causes the socket to |
| 77 |
* be closed automatically if any of the `exec` family functions succeed. |
| 78 |
* This is essential for multi threaded programs. |
| 79 |
* |
| 80 |
* @note The local port (`nl_socket_get_local_port()`) is unspecified after |
| 81 |
* creating a new socket. It only gets determined when accessing the |
| 82 |
* port the first time or during `nl_connect()`. When nl_connect() |
| 83 |
* fails during `bind()` due to `ADDRINUSE`, it will retry with |
| 84 |
* different ports if the port is unspecified. Unless you want to enforce |
| 85 |
* the use of a specific local port, don't access the local port (or |
| 86 |
* reset it to `unspecified` by calling `nl_socket_set_local_port(sk, 0)`). |
| 87 |
* This capability is indicated by |
| 88 |
* `%NL_CAPABILITY_NL_CONNECT_RETRY_GENERATE_PORT_ON_ADDRINUSE`. |
| 89 |
* |
| 90 |
* @note nl_connect() creates and sets the file descriptor. You can setup the file |
| 91 |
* descriptor yourself by creating and binding it, and then calling |
| 92 |
* nl_socket_set_fd(). The result will be the same. |
| 93 |
* |
| 94 |
* @see nl_socket_alloc() |
| 95 |
* @see nl_close() |
| 96 |
* @see nl_socket_set_fd() |
| 97 |
* |
| 98 |
* @return 0 on success or a negative error code. |
| 99 |
* |
| 100 |
* @retval -NLE_BAD_SOCK Socket is already connected |
| 101 |
*/ |
| 102 |
int nl_connect(struct nl_sock *sk, int protocol) |
| 103 |
{ |
| 104 |
int err, flags = 0; |
| 105 |
int errsv; |
| 106 |
socklen_t addrlen; |
| 107 |
struct sockaddr_nl local = { 0 }; |
| 108 |
int try_bind = 1; |
| 109 |
|
| 110 |
#ifdef SOCK_CLOEXEC |
| 111 |
flags |= SOCK_CLOEXEC; |
| 112 |
#endif |
| 113 |
|
| 114 |
if (sk->s_fd != -1) |
| 115 |
return -NLE_BAD_SOCK; |
| 116 |
|
| 117 |
sk->s_fd = socket(AF_NETLINK, SOCK_RAW | flags, protocol); |
| 118 |
if (sk->s_fd < 0) { |
| 119 |
errsv = errno; |
| 120 |
NL_DBG(4, "nl_connect(%p): socket() failed with %d (%s)\n", sk, errsv, |
| 121 |
nl_strerror_l(errsv)); |
| 122 |
err = -nl_syserr2nlerr(errsv); |
| 123 |
goto errout; |
| 124 |
} |
| 125 |
|
| 126 |
err = nl_socket_set_buffer_size(sk, 0, 0); |
| 127 |
if (err < 0) |
| 128 |
goto errout; |
| 129 |
|
| 130 |
if (_nl_socket_is_local_port_unspecified (sk)) { |
| 131 |
uint32_t port; |
| 132 |
uint32_t used_ports[32] = { 0 }; |
| 133 |
int ntries = 0; |
| 134 |
|
| 135 |
while (1) { |
| 136 |
if (ntries++ > 5) { |
| 137 |
/* try only a few times. We hit this only if many ports are already in |
| 138 |
* use but allocated *outside* libnl/generate_local_port(). */ |
| 139 |
_nl_socket_set_local_port_no_release (sk, 0); |
| 140 |
break; |
| 141 |
} |
| 142 |
|
| 143 |
port = _nl_socket_set_local_port_no_release(sk, 1); |
| 144 |
if (port == 0) |
| 145 |
break; |
| 146 |
|
| 147 |
err = bind(sk->s_fd, (struct sockaddr*) &sk->s_local, |
| 148 |
sizeof(sk->s_local)); |
| 149 |
if (err == 0) { |
| 150 |
try_bind = 0; |
| 151 |
break; |
| 152 |
} |
| 153 |
|
| 154 |
errsv = errno; |
| 155 |
if (errsv == EADDRINUSE) { |
| 156 |
NL_DBG(4, "nl_connect(%p): local port %u already in use. Retry.\n", sk, (unsigned) port); |
| 157 |
_nl_socket_used_ports_set(used_ports, port); |
| 158 |
} else { |
| 159 |
NL_DBG(4, "nl_connect(%p): bind() for port %u failed with %d (%s)\n", |
| 160 |
sk, (unsigned) port, errsv, nl_strerror_l(errsv)); |
| 161 |
_nl_socket_used_ports_release_all(used_ports); |
| 162 |
err = -nl_syserr2nlerr(errsv); |
| 163 |
goto errout; |
| 164 |
} |
| 165 |
} |
| 166 |
_nl_socket_used_ports_release_all(used_ports); |
| 167 |
} |
| 168 |
if (try_bind) { |
| 169 |
err = bind(sk->s_fd, (struct sockaddr*) &sk->s_local, |
| 170 |
sizeof(sk->s_local)); |
| 171 |
if (err != 0) { |
| 172 |
errsv = errno; |
| 173 |
NL_DBG(4, "nl_connect(%p): bind() failed with %d (%s)\n", |
| 174 |
sk, errsv, nl_strerror_l(errsv)); |
| 175 |
err = -nl_syserr2nlerr(errsv); |
| 176 |
goto errout; |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
addrlen = sizeof(local); |
| 181 |
err = getsockname(sk->s_fd, (struct sockaddr *) &local, |
| 182 |
&addrlen); |
| 183 |
if (err < 0) { |
| 184 |
NL_DBG(4, "nl_connect(%p): getsockname() failed with %d (%s)\n", |
| 185 |
sk, errno, nl_strerror_l(errno)); |
| 186 |
err = -nl_syserr2nlerr(errno); |
| 187 |
goto errout; |
| 188 |
} |
| 189 |
|
| 190 |
if (addrlen != sizeof(local)) { |
| 191 |
err = -NLE_NOADDR; |
| 192 |
goto errout; |
| 193 |
} |
| 194 |
|
| 195 |
if (local.nl_family != AF_NETLINK) { |
| 196 |
err = -NLE_AF_NOSUPPORT; |
| 197 |
goto errout; |
| 198 |
} |
| 199 |
|
| 200 |
if (sk->s_local.nl_pid != local.nl_pid) { |
| 201 |
/* The port id is different. That can happen if the port id was zero |
| 202 |
* and kernel assigned a local port. */ |
| 203 |
nl_socket_set_local_port (sk, local.nl_pid); |
| 204 |
} |
| 205 |
sk->s_local = local; |
| 206 |
sk->s_proto = protocol; |
| 207 |
|
| 208 |
return 0; |
| 209 |
errout: |
| 210 |
if (sk->s_fd != -1) { |
| 211 |
close(sk->s_fd); |
| 212 |
sk->s_fd = -1; |
| 213 |
} |
| 214 |
|
| 215 |
return err; |
| 216 |
} |
| 217 |
|
| 218 |
/** |
| 219 |
* Close Netlink socket |
| 220 |
* @arg sk Netlink socket (required) |
| 221 |
* |
| 222 |
* Closes the Netlink socket using `close()`. |
| 223 |
* |
| 224 |
* @note The socket is closed automatically if a `struct nl_sock` object is |
| 225 |
* freed using `nl_socket_free()`. |
| 226 |
* |
| 227 |
* @see nl_connect() |
| 228 |
*/ |
| 229 |
void nl_close(struct nl_sock *sk) |
| 230 |
{ |
| 231 |
if (sk->s_fd >= 0) { |
| 232 |
close(sk->s_fd); |
| 233 |
sk->s_fd = -1; |
| 234 |
} |
| 235 |
|
| 236 |
sk->s_proto = 0; |
| 237 |
} |
| 238 |
|
| 239 |
/** @} */ |
| 240 |
|
| 241 |
/** |
| 242 |
* @name Send |
| 243 |
* @{ |
| 244 |
*/ |
| 245 |
|
| 246 |
/** |
| 247 |
* Transmit raw data over Netlink socket. |
| 248 |
* @arg sk Netlink socket (required) |
| 249 |
* @arg buf Buffer carrying data to send (required) |
| 250 |
* @arg size Size of buffer (required) |
| 251 |
* |
| 252 |
* Transmits "raw" data over the specified Netlink socket. Unlike the other |
| 253 |
* transmit functions it does not modify the data in any way. It directly |
| 254 |
* passes the buffer \c buf of \c size to sendto(). |
| 255 |
* |
| 256 |
* The message is addressed to the peer as specified in the socket by either |
| 257 |
* the nl_socket_set_peer_port() or nl_socket_set_peer_groups() function. |
| 258 |
* |
| 259 |
* @note Because there is no indication on the message boundaries of the data |
| 260 |
* being sent, the \c NL_CB_MSG_OUT callback handler will not be invoked |
| 261 |
* for data that is being sent using this function. |
| 262 |
* |
| 263 |
* @see nl_socket_set_peer_port() |
| 264 |
* @see nl_socket_set_peer_groups() |
| 265 |
* @see nl_sendmsg() |
| 266 |
* |
| 267 |
* @return Number of bytes sent or a negative error code. |
| 268 |
*/ |
| 269 |
int nl_sendto(struct nl_sock *sk, void *buf, size_t size) |
| 270 |
{ |
| 271 |
int ret; |
| 272 |
|
| 273 |
if (!buf) |
| 274 |
return -NLE_INVAL; |
| 275 |
|
| 276 |
if (sk->s_fd < 0) |
| 277 |
return -NLE_BAD_SOCK; |
| 278 |
|
| 279 |
ret = sendto(sk->s_fd, buf, size, 0, (struct sockaddr *) |
| 280 |
&sk->s_peer, sizeof(sk->s_peer)); |
| 281 |
if (ret < 0) { |
| 282 |
NL_DBG(4, "nl_sendto(%p): sendto() failed with %d (%s)\n", |
| 283 |
sk, errno, nl_strerror_l(errno)); |
| 284 |
return -nl_syserr2nlerr(errno); |
| 285 |
} |
| 286 |
|
| 287 |
return ret; |
| 288 |
} |
| 289 |
|
| 290 |
/** |
| 291 |
* Transmit Netlink message using sendmsg() |
| 292 |
* @arg sk Netlink socket (required) |
| 293 |
* @arg msg Netlink message to be sent (required) |
| 294 |
* @arg hdr sendmsg() message header (required) |
| 295 |
* |
| 296 |
* Transmits the message specified in \c hdr over the Netlink socket using the |
| 297 |
* sendmsg() system call. |
| 298 |
* |
| 299 |
* @attention |
| 300 |
* The `msg` argument will *not* be used to derive the message payload that |
| 301 |
* is being sent out. The `msg` argument is *only* passed on to the |
| 302 |
* `NL_CB_MSG_OUT` callback. The caller is responsible to initialize the |
| 303 |
* `hdr` struct properly and have it point to the message payload and |
| 304 |
* socket address. |
| 305 |
* |
| 306 |
* @note |
| 307 |
* This function uses `nlmsg_set_src()` to modify the `msg` argument prior to |
| 308 |
* invoking the `NL_CB_MSG_OUT` callback to provide the local port number. |
| 309 |
* |
| 310 |
* @callback This function triggers the `NL_CB_MSG_OUT` callback. |
| 311 |
* |
| 312 |
* @attention |
| 313 |
* Think twice before using this function. It provides a low level access to |
| 314 |
* the Netlink socket. Among other limitations, it does not add credentials |
| 315 |
* even if enabled or respect the destination address specified in the `msg` |
| 316 |
* object. |
| 317 |
* |
| 318 |
* @see nl_socket_set_local_port() |
| 319 |
* @see nl_send_auto() |
| 320 |
* @see nl_send_iovec() |
| 321 |
* |
| 322 |
* @return Number of bytes sent on success or a negative error code. |
| 323 |
* |
| 324 |
* @lowlevel |
| 325 |
*/ |
| 326 |
int nl_sendmsg(struct nl_sock *sk, struct nl_msg *msg, struct msghdr *hdr) |
| 327 |
{ |
| 328 |
struct nl_cb *cb; |
| 329 |
int ret; |
| 330 |
|
| 331 |
if (sk->s_fd < 0) |
| 332 |
return -NLE_BAD_SOCK; |
| 333 |
|
| 334 |
nlmsg_set_src(msg, &sk->s_local); |
| 335 |
|
| 336 |
cb = sk->s_cb; |
| 337 |
if (cb->cb_set[NL_CB_MSG_OUT]) |
| 338 |
if ((ret = nl_cb_call(cb, NL_CB_MSG_OUT, msg)) != NL_OK) |
| 339 |
return ret; |
| 340 |
|
| 341 |
ret = sendmsg(sk->s_fd, hdr, 0); |
| 342 |
if (ret < 0) { |
| 343 |
NL_DBG(4, "nl_sendmsg(%p): sendmsg() failed with %d (%s)\n", |
| 344 |
sk, errno, nl_strerror_l(errno)); |
| 345 |
return -nl_syserr2nlerr(errno); |
| 346 |
} |
| 347 |
|
| 348 |
NL_DBG(4, "sent %d bytes\n", ret); |
| 349 |
return ret; |
| 350 |
} |
| 351 |
|
| 352 |
|
| 353 |
/** |
| 354 |
* Transmit Netlink message (taking IO vector) |
| 355 |
* @arg sk Netlink socket (required) |
| 356 |
* @arg msg Netlink message to be sent (required) |
| 357 |
* @arg iov IO vector to be sent (required) |
| 358 |
* @arg iovlen Number of struct iovec to be sent (required) |
| 359 |
* |
| 360 |
* This function is identical to nl_send() except that instead of taking a |
| 361 |
* `struct nl_msg` object it takes an IO vector. Please see the description |
| 362 |
* of `nl_send()`. |
| 363 |
* |
| 364 |
* @callback This function triggers the `NL_CB_MSG_OUT` callback. |
| 365 |
* |
| 366 |
* @see nl_send() |
| 367 |
* |
| 368 |
* @return Number of bytes sent on success or a negative error code. |
| 369 |
* |
| 370 |
* @lowlevel |
| 371 |
*/ |
| 372 |
int nl_send_iovec(struct nl_sock *sk, struct nl_msg *msg, struct iovec *iov, unsigned iovlen) |
| 373 |
{ |
| 374 |
struct sockaddr_nl *dst; |
| 375 |
struct ucred *creds; |
| 376 |
struct msghdr hdr = { |
| 377 |
.msg_name = (void *) &sk->s_peer, |
| 378 |
.msg_namelen = sizeof(struct sockaddr_nl), |
| 379 |
.msg_iov = iov, |
| 380 |
.msg_iovlen = iovlen, |
| 381 |
}; |
| 382 |
char buf[CMSG_SPACE(sizeof(struct ucred))]; |
| 383 |
|
| 384 |
/* Overwrite destination if specified in the message itself, defaults |
| 385 |
* to the peer address of the socket. |
| 386 |
*/ |
| 387 |
dst = nlmsg_get_dst(msg); |
| 388 |
if (dst->nl_family == AF_NETLINK) |
| 389 |
hdr.msg_name = dst; |
| 390 |
|
| 391 |
/* Add credentials if present. */ |
| 392 |
creds = nlmsg_get_creds(msg); |
| 393 |
if (creds != NULL) { |
| 394 |
struct cmsghdr *cmsg; |
| 395 |
|
| 396 |
hdr.msg_control = buf; |
| 397 |
hdr.msg_controllen = sizeof(buf); |
| 398 |
|
| 399 |
cmsg = CMSG_FIRSTHDR(&hdr); |
| 400 |
cmsg->cmsg_level = SOL_SOCKET; |
| 401 |
cmsg->cmsg_type = SCM_CREDENTIALS; |
| 402 |
cmsg->cmsg_len = CMSG_LEN(sizeof(struct ucred)); |
| 403 |
memcpy(CMSG_DATA(cmsg), creds, sizeof(struct ucred)); |
| 404 |
} |
| 405 |
|
| 406 |
return nl_sendmsg(sk, msg, &hdr); |
| 407 |
} |
| 408 |
|
| 409 |
/** |
| 410 |
* Transmit Netlink message |
| 411 |
* @arg sk Netlink socket (required) |
| 412 |
* @arg msg Netlink message (required) |
| 413 |
* |
| 414 |
* Transmits the Netlink message `msg` over the Netlink socket using the |
| 415 |
* `sendmsg()` system call. This function is based on `nl_send_iovec()` but |
| 416 |
* takes care of initializing a `struct iovec` based on the `msg` object. |
| 417 |
* |
| 418 |
* The message is addressed to the peer as specified in the socket by either |
| 419 |
* the nl_socket_set_peer_port() or nl_socket_set_peer_groups() function. |
| 420 |
* The peer address can be overwritten by specifying an address in the `msg` |
| 421 |
* object using nlmsg_set_dst(). |
| 422 |
* |
| 423 |
* If present in the `msg`, credentials set by the nlmsg_set_creds() function |
| 424 |
* are added to the control buffer of the message. |
| 425 |
* |
| 426 |
* @par Overwriting Capability: |
| 427 |
* Calls to this function can be overwritten by providing an alternative using |
| 428 |
* the nl_cb_overwrite_send() function. |
| 429 |
* |
| 430 |
* @callback This function triggers the `NL_CB_MSG_OUT` callback. |
| 431 |
* |
| 432 |
* @attention |
| 433 |
* Unlike `nl_send_auto()`, this function does *not* finalize the message in |
| 434 |
* terms of automatically adding needed flags or filling out port numbers. |
| 435 |
* |
| 436 |
* @see nl_send_auto() |
| 437 |
* @see nl_send_iovec() |
| 438 |
* @see nl_socket_set_peer_port() |
| 439 |
* @see nl_socket_set_peer_groups() |
| 440 |
* @see nlmsg_set_dst() |
| 441 |
* @see nlmsg_set_creds() |
| 442 |
* @see nl_cb_overwrite_send() |
| 443 |
* |
| 444 |
* @return Number of bytes sent on success or a negative error code. |
| 445 |
*/ |
| 446 |
int nl_send(struct nl_sock *sk, struct nl_msg *msg) |
| 447 |
{ |
| 448 |
struct nl_cb *cb = sk->s_cb; |
| 449 |
|
| 450 |
if (cb->cb_send_ow) |
| 451 |
return cb->cb_send_ow(sk, msg); |
| 452 |
else { |
| 453 |
struct iovec iov = { |
| 454 |
.iov_base = (void *) nlmsg_hdr(msg), |
| 455 |
.iov_len = nlmsg_hdr(msg)->nlmsg_len, |
| 456 |
}; |
| 457 |
|
| 458 |
return nl_send_iovec(sk, msg, &iov, 1); |
| 459 |
} |
| 460 |
} |
| 461 |
|
| 462 |
/** |
| 463 |
* Finalize Netlink message |
| 464 |
* @arg sk Netlink socket (required) |
| 465 |
* @arg msg Netlink message (required) |
| 466 |
* |
| 467 |
* This function finalizes a Netlink message by completing the message with |
| 468 |
* desirable flags and values depending on the socket configuration. |
| 469 |
* |
| 470 |
* - If not yet filled out, the source address of the message (`nlmsg_pid`) |
| 471 |
* will be set to the local port number of the socket. |
| 472 |
* - If not yet specified, the next available sequence number is assigned |
| 473 |
* to the message (`nlmsg_seq`). |
| 474 |
* - If not yet specified, the protocol field of the message will be set to |
| 475 |
* the protocol field of the socket. |
| 476 |
* - The `NLM_F_REQUEST` Netlink message flag will be set. |
| 477 |
* - The `NLM_F_ACK` flag will be set if Auto-ACK mode is enabled on the |
| 478 |
* socket. |
| 479 |
*/ |
| 480 |
void nl_complete_msg(struct nl_sock *sk, struct nl_msg *msg) |
| 481 |
{ |
| 482 |
struct nlmsghdr *nlh; |
| 483 |
|
| 484 |
nlh = nlmsg_hdr(msg); |
| 485 |
if (nlh->nlmsg_pid == NL_AUTO_PORT) |
| 486 |
nlh->nlmsg_pid = nl_socket_get_local_port(sk); |
| 487 |
|
| 488 |
if (nlh->nlmsg_seq == NL_AUTO_SEQ) |
| 489 |
nlh->nlmsg_seq = nl_socket_use_seq(sk); |
| 490 |
|
| 491 |
if (msg->nm_protocol == -1) |
| 492 |
msg->nm_protocol = sk->s_proto; |
| 493 |
|
| 494 |
nlh->nlmsg_flags |= NLM_F_REQUEST; |
| 495 |
|
| 496 |
if (!(sk->s_flags & NL_NO_AUTO_ACK)) |
| 497 |
nlh->nlmsg_flags |= NLM_F_ACK; |
| 498 |
} |
| 499 |
|
| 500 |
/** |
| 501 |
* Finalize and transmit Netlink message |
| 502 |
* @arg sk Netlink socket (required) |
| 503 |
* @arg msg Netlink message (required) |
| 504 |
* |
| 505 |
* Finalizes the message by passing it to `nl_complete_msg()` and transmits it |
| 506 |
* by passing it to `nl_send()`. |
| 507 |
* |
| 508 |
* @callback This function triggers the `NL_CB_MSG_OUT` callback. |
| 509 |
* |
| 510 |
* @see nl_complete_msg() |
| 511 |
* @see nl_send() |
| 512 |
* |
| 513 |
* @return Number of bytes sent or a negative error code. |
| 514 |
*/ |
| 515 |
int nl_send_auto(struct nl_sock *sk, struct nl_msg *msg) |
| 516 |
{ |
| 517 |
nl_complete_msg(sk, msg); |
| 518 |
|
| 519 |
return nl_send(sk, msg); |
| 520 |
} |
| 521 |
|
| 522 |
/** |
| 523 |
* Finalize and transmit Netlink message and wait for ACK or error message |
| 524 |
* @arg sk Netlink socket (required) |
| 525 |
* @arg msg Netlink message (required) |
| 526 |
* |
| 527 |
* Passes the `msg` to `nl_send_auto()` to finalize and transmit it. Frees the |
| 528 |
* message and waits (sleeps) for the ACK or error message to be received. |
| 529 |
* |
| 530 |
* @attention |
| 531 |
* Disabling Auto-ACK (nl_socket_disable_auto_ack()) will cause this function |
| 532 |
* to return immediately after transmitting the message. However, the peer may |
| 533 |
* still be returning an error message in response to the request. It is the |
| 534 |
* responsibility of the caller to handle such messages. |
| 535 |
* |
| 536 |
* @callback This function triggers the `NL_CB_MSG_OUT` callback. |
| 537 |
* |
| 538 |
* @attention |
| 539 |
* This function frees the `msg` object after transmitting it by calling |
| 540 |
* `nlmsg_free()`. |
| 541 |
* |
| 542 |
* @see nl_send_auto(). |
| 543 |
* @see nl_wait_for_ack() |
| 544 |
* |
| 545 |
* @return 0 on success or a negative error code. |
| 546 |
*/ |
| 547 |
int nl_send_sync(struct nl_sock *sk, struct nl_msg *msg) |
| 548 |
{ |
| 549 |
int err; |
| 550 |
|
| 551 |
err = nl_send_auto(sk, msg); |
| 552 |
nlmsg_free(msg); |
| 553 |
if (err < 0) |
| 554 |
return err; |
| 555 |
|
| 556 |
return wait_for_ack(sk); |
| 557 |
} |
| 558 |
|
| 559 |
/** |
| 560 |
* Construct and transmit a Netlink message |
| 561 |
* @arg sk Netlink socket (required) |
| 562 |
* @arg type Netlink message type (required) |
| 563 |
* @arg flags Netlink message flags (optional) |
| 564 |
* @arg buf Data buffer (optional) |
| 565 |
* @arg size Size of data buffer (optional) |
| 566 |
* |
| 567 |
* Allocates a new Netlink message based on `type` and `flags`. If `buf` |
| 568 |
* points to payload of length `size` that payload will be appended to the |
| 569 |
* message. |
| 570 |
* |
| 571 |
* Sends out the message using `nl_send_auto()` and frees the message |
| 572 |
* afterwards. |
| 573 |
* |
| 574 |
* @see nl_send_auto() |
| 575 |
* |
| 576 |
* @return Number of characters sent on success or a negative error code. |
| 577 |
* @retval -NLE_NOMEM Unable to allocate Netlink message |
| 578 |
*/ |
| 579 |
int nl_send_simple(struct nl_sock *sk, int type, int flags, void *buf, |
| 580 |
size_t size) |
| 581 |
{ |
| 582 |
int err; |
| 583 |
struct nl_msg *msg; |
| 584 |
|
| 585 |
msg = nlmsg_alloc_simple(type, flags); |
| 586 |
if (!msg) |
| 587 |
return -NLE_NOMEM; |
| 588 |
|
| 589 |
if (buf && size) { |
| 590 |
err = nlmsg_append(msg, buf, size, NLMSG_ALIGNTO); |
| 591 |
if (err < 0) |
| 592 |
goto errout; |
| 593 |
} |
| 594 |
|
| 595 |
err = nl_send_auto(sk, msg); |
| 596 |
errout: |
| 597 |
nlmsg_free(msg); |
| 598 |
|
| 599 |
return err; |
| 600 |
} |
| 601 |
|
| 602 |
/** @} */ |
| 603 |
|
| 604 |
/** |
| 605 |
* @name Receive |
| 606 |
* @{ |
| 607 |
*/ |
| 608 |
|
| 609 |
/** |
| 610 |
* Receive data from netlink socket |
| 611 |
* @arg sk Netlink socket (required) |
| 612 |
* @arg nla Netlink socket structure to hold address of peer (required) |
| 613 |
* @arg buf Destination pointer for message content (required) |
| 614 |
* @arg creds Destination pointer for credentials (optional) |
| 615 |
* |
| 616 |
* Receives data from a connected netlink socket using recvmsg() and returns |
| 617 |
* the number of bytes read. The read data is stored in a newly allocated |
| 618 |
* buffer that is assigned to \c *buf. The peer's netlink address will be |
| 619 |
* stored in \c *nla. |
| 620 |
* |
| 621 |
* This function blocks until data is available to be read unless the socket |
| 622 |
* has been put into non-blocking mode using nl_socket_set_nonblocking() in |
| 623 |
* which case this function will return immediately with a return value of |
| 624 |
* -NLA_AGAIN (versions before 3.2.22 returned instead 0, in which case you |
| 625 |
* should check first clear errno and then check for errno EAGAIN). |
| 626 |
* |
| 627 |
* The buffer size used when reading from the netlink socket and thus limiting |
| 628 |
* the maximum size of a netlink message that can be read defaults to the size |
| 629 |
* of a memory page (getpagesize()). The buffer size can be modified on a per |
| 630 |
* socket level using the function nl_socket_set_msg_buf_size(). |
| 631 |
* |
| 632 |
* If message peeking is enabled using nl_socket_enable_msg_peek() the size of |
| 633 |
* the message to be read will be determined using the MSG_PEEK flag prior to |
| 634 |
* performing the actual read. This leads to an additional recvmsg() call for |
| 635 |
* every read operation which has performance implications and is not |
| 636 |
* recommended for high throughput protocols. |
| 637 |
* |
| 638 |
* An eventual interruption of the recvmsg() system call is automatically |
| 639 |
* handled by retrying the operation. |
| 640 |
* |
| 641 |
* If receiving of credentials has been enabled using the function |
| 642 |
* nl_socket_set_passcred(), this function will allocate a new struct ucred |
| 643 |
* filled with the received credentials and assign it to \c *creds. The caller |
| 644 |
* is responsible for freeing the buffer. |
| 645 |
* |
| 646 |
* @note The caller is responsible to free the returned data buffer and if |
| 647 |
* enabled, the credentials buffer. |
| 648 |
* |
| 649 |
* @see nl_socket_set_nonblocking() |
| 650 |
* @see nl_socket_set_msg_buf_size() |
| 651 |
* @see nl_socket_enable_msg_peek() |
| 652 |
* @see nl_socket_set_passcred() |
| 653 |
* |
| 654 |
* @return Number of bytes read, 0 on EOF, 0 on no data event (non-blocking |
| 655 |
* mode), or a negative error code. |
| 656 |
*/ |
| 657 |
int nl_recv(struct nl_sock *sk, struct sockaddr_nl *nla, |
| 658 |
unsigned char **buf, struct ucred **creds) |
| 659 |
{ |
| 660 |
ssize_t n; |
| 661 |
int flags = 0; |
| 662 |
static int page_size = 0; /* GLOBAL! */ |
| 663 |
struct iovec iov; |
| 664 |
struct msghdr msg = { |
| 665 |
.msg_name = (void *) nla, |
| 666 |
.msg_namelen = sizeof(struct sockaddr_nl), |
| 667 |
.msg_iov = &iov, |
| 668 |
.msg_iovlen = 1, |
| 669 |
}; |
| 670 |
struct ucred* tmpcreds = NULL; |
| 671 |
int retval = 0; |
| 672 |
|
| 673 |
if (!buf || !nla) |
| 674 |
return -NLE_INVAL; |
| 675 |
|
| 676 |
if ( (sk->s_flags & NL_MSG_PEEK) |
| 677 |
|| (!(sk->s_flags & NL_MSG_PEEK_EXPLICIT) && sk->s_bufsize == 0)) |
| 678 |
flags |= MSG_PEEK | MSG_TRUNC; |
| 679 |
|
| 680 |
if (page_size == 0) |
| 681 |
page_size = getpagesize() * 4; |
| 682 |
|
| 683 |
iov.iov_len = sk->s_bufsize ? sk->s_bufsize : ((size_t)page_size); |
| 684 |
iov.iov_base = malloc(iov.iov_len); |
| 685 |
|
| 686 |
if (!iov.iov_base) { |
| 687 |
retval = -NLE_NOMEM; |
| 688 |
goto abort; |
| 689 |
} |
| 690 |
|
| 691 |
if (creds && (sk->s_flags & NL_SOCK_PASSCRED)) { |
| 692 |
msg.msg_controllen = CMSG_SPACE(sizeof(struct ucred)); |
| 693 |
msg.msg_control = malloc(msg.msg_controllen); |
| 694 |
if (!msg.msg_control) { |
| 695 |
retval = -NLE_NOMEM; |
| 696 |
goto abort; |
| 697 |
} |
| 698 |
} |
| 699 |
retry: |
| 700 |
|
| 701 |
n = recvmsg(sk->s_fd, &msg, flags); |
| 702 |
if (!n) { |
| 703 |
retval = 0; |
| 704 |
goto abort; |
| 705 |
} |
| 706 |
if (n < 0) { |
| 707 |
if (errno == EINTR) { |
| 708 |
NL_DBG(3, "recvmsg() returned EINTR, retrying\n"); |
| 709 |
goto retry; |
| 710 |
} |
| 711 |
|
| 712 |
NL_DBG(4, "recvmsg(%p): nl_recv() failed with %d (%s)\n", |
| 713 |
sk, errno, nl_strerror_l(errno)); |
| 714 |
retval = -nl_syserr2nlerr(errno); |
| 715 |
goto abort; |
| 716 |
} |
| 717 |
|
| 718 |
if (msg.msg_flags & MSG_CTRUNC) { |
| 719 |
void *tmp; |
| 720 |
|
| 721 |
if (msg.msg_controllen == 0) { |
| 722 |
retval = -NLE_MSG_TRUNC; |
| 723 |
NL_DBG(4, "recvmsg(%p): Received unexpected control data", sk); |
| 724 |
goto abort; |
| 725 |
} |
| 726 |
|
| 727 |
msg.msg_controllen *= 2; |
| 728 |
tmp = realloc(msg.msg_control, msg.msg_controllen); |
| 729 |
if (!tmp) { |
| 730 |
retval = -NLE_NOMEM; |
| 731 |
goto abort; |
| 732 |
} |
| 733 |
msg.msg_control = tmp; |
| 734 |
goto retry; |
| 735 |
} |
| 736 |
|
| 737 |
if (iov.iov_len < ((size_t)n) || (msg.msg_flags & MSG_TRUNC)) { |
| 738 |
void *tmp; |
| 739 |
|
| 740 |
/* respond with error to an incomplete message */ |
| 741 |
if (flags == 0) { |
| 742 |
retval = -NLE_MSG_TRUNC; |
| 743 |
goto abort; |
| 744 |
} |
| 745 |
|
| 746 |
/* Provided buffer is not long enough, enlarge it |
| 747 |
* to size of n (which should be total length of the message) |
| 748 |
* and try again. */ |
| 749 |
iov.iov_len = n; |
| 750 |
tmp = realloc(iov.iov_base, iov.iov_len); |
| 751 |
if (!tmp) { |
| 752 |
retval = -NLE_NOMEM; |
| 753 |
goto abort; |
| 754 |
} |
| 755 |
iov.iov_base = tmp; |
| 756 |
flags = 0; |
| 757 |
goto retry; |
| 758 |
} |
| 759 |
|
| 760 |
if (flags != 0) { |
| 761 |
/* Buffer is big enough, do the actual reading */ |
| 762 |
flags = 0; |
| 763 |
goto retry; |
| 764 |
} |
| 765 |
|
| 766 |
if (msg.msg_namelen != sizeof(struct sockaddr_nl)) { |
| 767 |
retval = -NLE_NOADDR; |
| 768 |
goto abort; |
| 769 |
} |
| 770 |
|
| 771 |
if (creds && (sk->s_flags & NL_SOCK_PASSCRED)) { |
| 772 |
struct cmsghdr *cmsg; |
| 773 |
|
| 774 |
for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg)) { |
| 775 |
if (cmsg->cmsg_level != SOL_SOCKET) |
| 776 |
continue; |
| 777 |
if (cmsg->cmsg_type != SCM_CREDENTIALS) |
| 778 |
continue; |
| 779 |
tmpcreds = malloc(sizeof(*tmpcreds)); |
| 780 |
if (!tmpcreds) { |
| 781 |
retval = -NLE_NOMEM; |
| 782 |
goto abort; |
| 783 |
} |
| 784 |
memcpy(tmpcreds, CMSG_DATA(cmsg), sizeof(*tmpcreds)); |
| 785 |
break; |
| 786 |
} |
| 787 |
} |
| 788 |
|
| 789 |
retval = n; |
| 790 |
abort: |
| 791 |
free(msg.msg_control); |
| 792 |
|
| 793 |
if (retval <= 0) { |
| 794 |
free(iov.iov_base); |
| 795 |
iov.iov_base = NULL; |
| 796 |
free(tmpcreds); |
| 797 |
tmpcreds = NULL; |
| 798 |
} else |
| 799 |
*buf = iov.iov_base; |
| 800 |
|
| 801 |
if (creds) |
| 802 |
*creds = tmpcreds; |
| 803 |
|
| 804 |
return retval; |
| 805 |
} |
| 806 |
|
| 807 |
/** @cond SKIP */ |
| 808 |
#define NL_CB_CALL(cb, type, msg) \ |
| 809 |
do { \ |
| 810 |
err = nl_cb_call(cb, type, msg); \ |
| 811 |
switch (err) { \ |
| 812 |
case NL_OK: \ |
| 813 |
err = 0; \ |
| 814 |
break; \ |
| 815 |
case NL_SKIP: \ |
| 816 |
goto skip; \ |
| 817 |
case NL_STOP: \ |
| 818 |
goto stop; \ |
| 819 |
default: \ |
| 820 |
goto out; \ |
| 821 |
} \ |
| 822 |
} while (0) |
| 823 |
/** @endcond */ |
| 824 |
|
| 825 |
static int recvmsgs(struct nl_sock *sk, struct nl_cb *cb) |
| 826 |
{ |
| 827 |
int n, err = 0, multipart = 0, interrupted = 0, nrecv = 0; |
| 828 |
unsigned char *buf = NULL; |
| 829 |
struct nlmsghdr *hdr; |
| 830 |
|
| 831 |
/* |
| 832 |
nla is passed on to not only to nl_recv() but may also be passed |
| 833 |
to a function pointer provided by the caller which may or may not |
| 834 |
initialize the variable. Thomas Graf. |
| 835 |
*/ |
| 836 |
struct sockaddr_nl nla = {0}; |
| 837 |
struct nl_msg *msg = NULL; |
| 838 |
struct ucred *creds = NULL; |
| 839 |
|
| 840 |
continue_reading: |
| 841 |
NL_DBG(3, "Attempting to read from %p\n", sk); |
| 842 |
if (cb->cb_recv_ow) |
| 843 |
n = cb->cb_recv_ow(sk, &nla, &buf, &creds); |
| 844 |
else |
| 845 |
n = nl_recv(sk, &nla, &buf, &creds); |
| 846 |
|
| 847 |
if (n <= 0) |
| 848 |
return n; |
| 849 |
|
| 850 |
NL_DBG(3, "recvmsgs(%p): Read %d bytes\n", sk, n); |
| 851 |
|
| 852 |
hdr = (struct nlmsghdr *) buf; |
| 853 |
while (nlmsg_ok(hdr, n)) { |
| 854 |
NL_DBG(3, "recvmsgs(%p): Processing valid message...\n", sk); |
| 855 |
|
| 856 |
nlmsg_free(msg); |
| 857 |
msg = nlmsg_convert(hdr); |
| 858 |
if (!msg) { |
| 859 |
err = -NLE_NOMEM; |
| 860 |
goto out; |
| 861 |
} |
| 862 |
|
| 863 |
nlmsg_set_proto(msg, sk->s_proto); |
| 864 |
nlmsg_set_src(msg, &nla); |
| 865 |
if (creds) |
| 866 |
nlmsg_set_creds(msg, creds); |
| 867 |
|
| 868 |
nrecv++; |
| 869 |
|
| 870 |
/* Raw callback is the first, it gives the most control |
| 871 |
* to the user and he can do his very own parsing. */ |
| 872 |
if (cb->cb_set[NL_CB_MSG_IN]) |
| 873 |
NL_CB_CALL(cb, NL_CB_MSG_IN, msg); |
| 874 |
|
| 875 |
/* Sequence number checking. The check may be done by |
| 876 |
* the user, otherwise a very simple check is applied |
| 877 |
* enforcing strict ordering */ |
| 878 |
if (cb->cb_set[NL_CB_SEQ_CHECK]) { |
| 879 |
NL_CB_CALL(cb, NL_CB_SEQ_CHECK, msg); |
| 880 |
|
| 881 |
/* Only do sequence checking if auto-ack mode is enabled */ |
| 882 |
} else if (!(sk->s_flags & NL_NO_AUTO_ACK)) { |
| 883 |
if (hdr->nlmsg_seq != sk->s_seq_expect) { |
| 884 |
if (cb->cb_set[NL_CB_INVALID]) |
| 885 |
NL_CB_CALL(cb, NL_CB_INVALID, msg); |
| 886 |
else { |
| 887 |
err = -NLE_SEQ_MISMATCH; |
| 888 |
goto out; |
| 889 |
} |
| 890 |
} |
| 891 |
} |
| 892 |
|
| 893 |
if (hdr->nlmsg_type == NLMSG_DONE || |
| 894 |
hdr->nlmsg_type == NLMSG_ERROR || |
| 895 |
hdr->nlmsg_type == NLMSG_NOOP || |
| 896 |
hdr->nlmsg_type == NLMSG_OVERRUN) { |
| 897 |
/* We can't check for !NLM_F_MULTI since some netlink |
| 898 |
* users in the kernel are broken. */ |
| 899 |
if (sk->s_seq_expect != UINT_MAX) |
| 900 |
sk->s_seq_expect++; |
| 901 |
else |
| 902 |
sk->s_seq_expect = 0; |
| 903 |
NL_DBG(3, "recvmsgs(%p): Increased expected " \ |
| 904 |
"sequence number to %d\n", |
| 905 |
sk, sk->s_seq_expect); |
| 906 |
} |
| 907 |
|
| 908 |
if (hdr->nlmsg_flags & NLM_F_MULTI) |
| 909 |
multipart = 1; |
| 910 |
|
| 911 |
if (hdr->nlmsg_flags & NLM_F_DUMP_INTR) { |
| 912 |
if (cb->cb_set[NL_CB_DUMP_INTR]) |
| 913 |
NL_CB_CALL(cb, NL_CB_DUMP_INTR, msg); |
| 914 |
else { |
| 915 |
/* |
| 916 |
* We have to continue reading to clear |
| 917 |
* all messages until a NLMSG_DONE is |
| 918 |
* received and report the inconsistency. |
| 919 |
*/ |
| 920 |
interrupted = 1; |
| 921 |
} |
| 922 |
} |
| 923 |
|
| 924 |
/* Other side wishes to see an ack for this message */ |
| 925 |
if (hdr->nlmsg_flags & NLM_F_ACK) { |
| 926 |
if (cb->cb_set[NL_CB_SEND_ACK]) |
| 927 |
NL_CB_CALL(cb, NL_CB_SEND_ACK, msg); |
| 928 |
else { |
| 929 |
/* FIXME: implement */ |
| 930 |
} |
| 931 |
} |
| 932 |
|
| 933 |
/* messages terminates a multipart message, this is |
| 934 |
* usually the end of a message and therefore we slip |
| 935 |
* out of the loop by default. the user may overrule |
| 936 |
* this action by skipping this packet. */ |
| 937 |
if (hdr->nlmsg_type == NLMSG_DONE) { |
| 938 |
multipart = 0; |
| 939 |
if (cb->cb_set[NL_CB_FINISH]) |
| 940 |
NL_CB_CALL(cb, NL_CB_FINISH, msg); |
| 941 |
} |
| 942 |
|
| 943 |
/* Message to be ignored, the default action is to |
| 944 |
* skip this message if no callback is specified. The |
| 945 |
* user may overrule this action by returning |
| 946 |
* NL_PROCEED. */ |
| 947 |
else if (hdr->nlmsg_type == NLMSG_NOOP) { |
| 948 |
if (cb->cb_set[NL_CB_SKIPPED]) |
| 949 |
NL_CB_CALL(cb, NL_CB_SKIPPED, msg); |
| 950 |
else |
| 951 |
goto skip; |
| 952 |
} |
| 953 |
|
| 954 |
/* Data got lost, report back to user. The default action is to |
| 955 |
* quit parsing. The user may overrule this action by retuning |
| 956 |
* NL_SKIP or NL_PROCEED (dangerous) */ |
| 957 |
else if (hdr->nlmsg_type == NLMSG_OVERRUN) { |
| 958 |
if (cb->cb_set[NL_CB_OVERRUN]) |
| 959 |
NL_CB_CALL(cb, NL_CB_OVERRUN, msg); |
| 960 |
else { |
| 961 |
err = -NLE_MSG_OVERFLOW; |
| 962 |
goto out; |
| 963 |
} |
| 964 |
} |
| 965 |
|
| 966 |
/* Message carries a nlmsgerr */ |
| 967 |
else if (hdr->nlmsg_type == NLMSG_ERROR) { |
| 968 |
struct nlmsgerr *e = nlmsg_data(hdr); |
| 969 |
|
| 970 |
if (hdr->nlmsg_len < |
| 971 |
((unsigned)nlmsg_size(sizeof(*e)))) { |
| 972 |
/* Truncated error message, the default action |
| 973 |
* is to stop parsing. The user may overrule |
| 974 |
* this action by returning NL_SKIP or |
| 975 |
* NL_PROCEED (dangerous) */ |
| 976 |
if (cb->cb_set[NL_CB_INVALID]) |
| 977 |
NL_CB_CALL(cb, NL_CB_INVALID, msg); |
| 978 |
else { |
| 979 |
err = -NLE_MSG_TRUNC; |
| 980 |
goto out; |
| 981 |
} |
| 982 |
} else if (e->error) { |
| 983 |
NL_DBG(4, "recvmsgs(%p): RTNETLINK responded with %d (%s)\n", |
| 984 |
sk, -e->error, nl_strerror_l(-e->error)); |
| 985 |
|
| 986 |
/* Error message reported back from kernel. */ |
| 987 |
if (cb->cb_err) { |
| 988 |
err = cb->cb_err(&nla, e, |
| 989 |
cb->cb_err_arg); |
| 990 |
if (err < 0) |
| 991 |
goto out; |
| 992 |
else if (err == NL_SKIP) |
| 993 |
goto skip; |
| 994 |
else if (err == NL_STOP) { |
| 995 |
err = -nl_syserr2nlerr(e->error); |
| 996 |
goto out; |
| 997 |
} |
| 998 |
} else { |
| 999 |
err = -nl_syserr2nlerr(e->error); |
| 1000 |
goto out; |
| 1001 |
} |
| 1002 |
} else if (cb->cb_set[NL_CB_ACK]) |
| 1003 |
NL_CB_CALL(cb, NL_CB_ACK, msg); |
| 1004 |
} else { |
| 1005 |
/* Valid message (not checking for MULTIPART bit to |
| 1006 |
* get along with broken kernels. NL_SKIP has no |
| 1007 |
* effect on this. */ |
| 1008 |
if (cb->cb_set[NL_CB_VALID]) |
| 1009 |
NL_CB_CALL(cb, NL_CB_VALID, msg); |
| 1010 |
} |
| 1011 |
skip: |
| 1012 |
err = 0; |
| 1013 |
hdr = nlmsg_next(hdr, &n); |
| 1014 |
} |
| 1015 |
|
| 1016 |
nlmsg_free(msg); |
| 1017 |
free(buf); |
| 1018 |
free(creds); |
| 1019 |
buf = NULL; |
| 1020 |
msg = NULL; |
| 1021 |
creds = NULL; |
| 1022 |
|
| 1023 |
if (multipart) { |
| 1024 |
/* Multipart message not yet complete, continue reading */ |
| 1025 |
goto continue_reading; |
| 1026 |
} |
| 1027 |
stop: |
| 1028 |
err = 0; |
| 1029 |
out: |
| 1030 |
nlmsg_free(msg); |
| 1031 |
free(buf); |
| 1032 |
free(creds); |
| 1033 |
|
| 1034 |
if (interrupted) |
| 1035 |
err = -NLE_DUMP_INTR; |
| 1036 |
|
| 1037 |
if (!err) |
| 1038 |
err = nrecv; |
| 1039 |
|
| 1040 |
return err; |
| 1041 |
} |
| 1042 |
|
| 1043 |
/** |
| 1044 |
* Receive a set of messages from a netlink socket and report parsed messages |
| 1045 |
* @arg sk Netlink socket. |
| 1046 |
* @arg cb set of callbacks to control behaviour. |
| 1047 |
* |
| 1048 |
* This function is identical to nl_recvmsgs() to the point that it will |
| 1049 |
* return the number of parsed messages instead of 0 on success. |
| 1050 |
* |
| 1051 |
* @see nl_recvmsgs() |
| 1052 |
* |
| 1053 |
* @return Number of received messages or a negative error code from nl_recv(). |
| 1054 |
*/ |
| 1055 |
int nl_recvmsgs_report(struct nl_sock *sk, struct nl_cb *cb) |
| 1056 |
{ |
| 1057 |
if (cb->cb_recvmsgs_ow) |
| 1058 |
return cb->cb_recvmsgs_ow(sk, cb); |
| 1059 |
else |
| 1060 |
return recvmsgs(sk, cb); |
| 1061 |
} |
| 1062 |
|
| 1063 |
/** |
| 1064 |
* Receive a set of messages from a netlink socket. |
| 1065 |
* @arg sk Netlink socket. |
| 1066 |
* @arg cb set of callbacks to control behaviour. |
| 1067 |
* |
| 1068 |
* Repeatedly calls nl_recv() or the respective replacement if provided |
| 1069 |
* by the application (see nl_cb_overwrite_recv()) and parses the |
| 1070 |
* received data as netlink messages. Stops reading if one of the |
| 1071 |
* callbacks returns NL_STOP or nl_recv returns either 0 or a negative error code. |
| 1072 |
* |
| 1073 |
* A non-blocking sockets causes the function to return immediately if |
| 1074 |
* no data is available. |
| 1075 |
* |
| 1076 |
* @see nl_recvmsgs_report() |
| 1077 |
* |
| 1078 |
* @return 0 on success or a negative error code from nl_recv(). |
| 1079 |
*/ |
| 1080 |
int nl_recvmsgs(struct nl_sock *sk, struct nl_cb *cb) |
| 1081 |
{ |
| 1082 |
int err; |
| 1083 |
|
| 1084 |
if ((err = nl_recvmsgs_report(sk, cb)) > 0) |
| 1085 |
err = 0; |
| 1086 |
|
| 1087 |
return err; |
| 1088 |
} |
| 1089 |
|
| 1090 |
/** |
| 1091 |
* Receive a set of message from a netlink socket using handlers in nl_sock. |
| 1092 |
* @arg sk Netlink socket. |
| 1093 |
* |
| 1094 |
* Calls nl_recvmsgs() with the handlers configured in the netlink socket. |
| 1095 |
*/ |
| 1096 |
int nl_recvmsgs_default(struct nl_sock *sk) |
| 1097 |
{ |
| 1098 |
return nl_recvmsgs(sk, sk->s_cb); |
| 1099 |
|
| 1100 |
} |
| 1101 |
|
| 1102 |
static int ack_wait_handler(struct nl_msg *msg, void *arg) |
| 1103 |
{ |
| 1104 |
return NL_STOP; |
| 1105 |
} |
| 1106 |
|
| 1107 |
/** |
| 1108 |
* Wait for ACK. |
| 1109 |
* @arg sk Netlink socket. |
| 1110 |
* @pre The netlink socket must be in blocking state. |
| 1111 |
* |
| 1112 |
* Waits until an ACK is received for the latest not yet acknowledged |
| 1113 |
* netlink message. |
| 1114 |
*/ |
| 1115 |
int nl_wait_for_ack(struct nl_sock *sk) |
| 1116 |
{ |
| 1117 |
int err; |
| 1118 |
struct nl_cb *cb; |
| 1119 |
|
| 1120 |
cb = nl_cb_clone(sk->s_cb); |
| 1121 |
if (cb == NULL) |
| 1122 |
return -NLE_NOMEM; |
| 1123 |
|
| 1124 |
nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, ack_wait_handler, NULL); |
| 1125 |
err = nl_recvmsgs(sk, cb); |
| 1126 |
nl_cb_put(cb); |
| 1127 |
|
| 1128 |
return err; |
| 1129 |
} |
| 1130 |
|
| 1131 |
/** @cond SKIP */ |
| 1132 |
struct pickup_param |
| 1133 |
{ |
| 1134 |
int (*parser)(struct nl_cache_ops *, struct sockaddr_nl *, |
| 1135 |
struct nlmsghdr *, struct nl_parser_param *); |
| 1136 |
struct nl_object *result; |
| 1137 |
int *syserror; |
| 1138 |
}; |
| 1139 |
|
| 1140 |
static int __store_answer(struct nl_object *obj, struct nl_parser_param *p) |
| 1141 |
{ |
| 1142 |
struct pickup_param *pp = p->pp_arg; |
| 1143 |
/* |
| 1144 |
* the parser will put() the object at the end, expecting the cache |
| 1145 |
* to take the reference. |
| 1146 |
*/ |
| 1147 |
nl_object_get(obj); |
| 1148 |
pp->result = obj; |
| 1149 |
|
| 1150 |
return 0; |
| 1151 |
} |
| 1152 |
|
| 1153 |
static int __pickup_answer(struct nl_msg *msg, void *arg) |
| 1154 |
{ |
| 1155 |
struct pickup_param *pp = arg; |
| 1156 |
struct nl_parser_param parse_arg = { |
| 1157 |
.pp_cb = __store_answer, |
| 1158 |
.pp_arg = pp, |
| 1159 |
}; |
| 1160 |
|
| 1161 |
return pp->parser(NULL, &msg->nm_src, msg->nm_nlh, &parse_arg); |
| 1162 |
} |
| 1163 |
|
| 1164 |
static int __pickup_answer_syserr(struct sockaddr_nl *nla, struct nlmsgerr *nlerr, void *arg) |
| 1165 |
{ |
| 1166 |
*(((struct pickup_param *) arg)->syserror) = nlerr->error; |
| 1167 |
|
| 1168 |
return -nl_syserr2nlerr(nlerr->error); |
| 1169 |
} |
| 1170 |
|
| 1171 |
/** @endcond */ |
| 1172 |
|
| 1173 |
/** |
| 1174 |
* Pickup netlink answer, parse is and return object |
| 1175 |
* @arg sk Netlink socket |
| 1176 |
* @arg parser Parser function to parse answer |
| 1177 |
* @arg result Result pointer to return parsed object |
| 1178 |
* |
| 1179 |
* @return 0 on success or a negative error code. |
| 1180 |
*/ |
| 1181 |
int nl_pickup(struct nl_sock *sk, |
| 1182 |
int (*parser)(struct nl_cache_ops *, struct sockaddr_nl *, |
| 1183 |
struct nlmsghdr *, struct nl_parser_param *), |
| 1184 |
struct nl_object **result) |
| 1185 |
{ |
| 1186 |
return nl_pickup_keep_syserr(sk, parser, result, NULL); |
| 1187 |
} |
| 1188 |
|
| 1189 |
/** |
| 1190 |
* Pickup netlink answer, parse is and return object with preserving system error |
| 1191 |
* @arg sk Netlink socket |
| 1192 |
* @arg parser Parser function to parse answer |
| 1193 |
* @arg result Result pointer to return parsed object |
| 1194 |
* @arg syserr Result pointer for the system error in case of failure |
| 1195 |
* |
| 1196 |
* @return 0 on success or a negative error code. |
| 1197 |
*/ |
| 1198 |
int nl_pickup_keep_syserr(struct nl_sock *sk, |
| 1199 |
int (*parser)(struct nl_cache_ops *, struct sockaddr_nl *, |
| 1200 |
struct nlmsghdr *, struct nl_parser_param *), |
| 1201 |
struct nl_object **result, |
| 1202 |
int *syserror) |
| 1203 |
{ |
| 1204 |
struct nl_cb *cb; |
| 1205 |
int err; |
| 1206 |
struct pickup_param pp = { |
| 1207 |
.parser = parser, |
| 1208 |
}; |
| 1209 |
|
| 1210 |
cb = nl_cb_clone(sk->s_cb); |
| 1211 |
if (cb == NULL) |
| 1212 |
return -NLE_NOMEM; |
| 1213 |
|
| 1214 |
nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, __pickup_answer, &pp); |
| 1215 |
if (syserror) { |
| 1216 |
*syserror = 0; |
| 1217 |
pp.syserror = syserror; |
| 1218 |
nl_cb_err(cb, NL_CB_CUSTOM, __pickup_answer_syserr, &pp); |
| 1219 |
} |
| 1220 |
|
| 1221 |
err = nl_recvmsgs(sk, cb); |
| 1222 |
if (err < 0) |
| 1223 |
goto errout; |
| 1224 |
|
| 1225 |
*result = pp.result; |
| 1226 |
errout: |
| 1227 |
nl_cb_put(cb); |
| 1228 |
|
| 1229 |
return err; |
| 1230 |
} |
| 1231 |
|
| 1232 |
/** @} */ |
| 1233 |
|
| 1234 |
/** |
| 1235 |
* @name Deprecated |
| 1236 |
* @{ |
| 1237 |
*/ |
| 1238 |
|
| 1239 |
/** |
| 1240 |
* @deprecated Please use nl_complete_msg() |
| 1241 |
*/ |
| 1242 |
void nl_auto_complete(struct nl_sock *sk, struct nl_msg *msg) |
| 1243 |
{ |
| 1244 |
nl_complete_msg(sk, msg); |
| 1245 |
} |
| 1246 |
|
| 1247 |
/** |
| 1248 |
* @deprecated Please use nl_send_auto() |
| 1249 |
*/ |
| 1250 |
int nl_send_auto_complete(struct nl_sock *sk, struct nl_msg *msg) |
| 1251 |
{ |
| 1252 |
return nl_send_auto(sk, msg); |
| 1253 |
} |
| 1254 |
|
| 1255 |
|
| 1256 |
/** @} */ |
| 1257 |
|
| 1258 |
/** @} */ |
| 1259 |
|
| 1260 |
/** @} */ |