| 1 |
/* libnfnetlink.c: generic library for communication with netfilter |
| 2 |
* |
| 3 |
* (C) 2002-2006 by Harald Welte <[email protected]> |
| 4 |
* (C) 2006-2011 by Pablo Neira Ayuso <[email protected]> |
| 5 |
* |
| 6 |
* Based on some original ideas from Jay Schulist <[email protected]> |
| 7 |
* |
| 8 |
* Development of this code funded by Astaro AG (http://www.astaro.com) |
| 9 |
* |
| 10 |
* This program is free software; you can redistribute it and/or modify it |
| 11 |
* under the terms of the GNU General Public License version 2 as published |
| 12 |
* by the Free Software Foundation. |
| 13 |
* |
| 14 |
* 2005-09-14 Pablo Neira Ayuso <[email protected]>: |
| 15 |
* Define structure nfnlhdr |
| 16 |
* Added __be64_to_cpu function |
| 17 |
* Use NFA_TYPE macro to get the attribute type |
| 18 |
* |
| 19 |
* 2006-01-14 Harald Welte <[email protected]>: |
| 20 |
* introduce nfnl_subsys_handle |
| 21 |
* |
| 22 |
* 2006-01-15 Pablo Neira Ayuso <[email protected]>: |
| 23 |
* set missing subsys_id in nfnl_subsys_open |
| 24 |
* set missing nfnlh->local.nl_pid in nfnl_open |
| 25 |
* |
| 26 |
* 2006-01-26 Harald Welte <[email protected]>: |
| 27 |
* remove bogus nfnlh->local.nl_pid from nfnl_open ;) |
| 28 |
* add 16bit attribute functions |
| 29 |
* |
| 30 |
* 2006-07-03 Pablo Neira Ayuso <[email protected]>: |
| 31 |
* add iterator API |
| 32 |
* add replacements for nfnl_listen and nfnl_talk |
| 33 |
* fix error handling |
| 34 |
* add assertions |
| 35 |
* add documentation |
| 36 |
* minor cleanups |
| 37 |
*/ |
| 38 |
|
| 39 |
#include <stdlib.h> |
| 40 |
#include <stdio.h> |
| 41 |
#include <unistd.h> |
| 42 |
#include <errno.h> |
| 43 |
#include <string.h> |
| 44 |
#include <time.h> |
| 45 |
#include <netinet/in.h> |
| 46 |
#include <assert.h> |
| 47 |
#include <linux/types.h> |
| 48 |
#include <sys/socket.h> |
| 49 |
#include <sys/uio.h> |
| 50 |
|
| 51 |
#include <linux/netlink.h> |
| 52 |
|
| 53 |
#include <libnfnetlink/libnfnetlink.h> |
| 54 |
|
| 55 |
/** |
| 56 |
* \mainpage |
| 57 |
* |
| 58 |
* Deprecated |
| 59 |
* --- |
| 60 |
* New applications should use |
| 61 |
* [libmnl](https://netfilter.org/projects/libmnl/doxygen/html/). |
| 62 |
* |
| 63 |
* [libnetfilter_queue] |
| 64 |
* (https://netfilter.org/projects/libnetfilter_queue/doxygen/html/) |
| 65 |
* now provides a set of helpers for libmnl, in addition to those it provides |
| 66 |
* for libnfnetlink (which are now deprecated). |
| 67 |
*/ |
| 68 |
|
| 69 |
/** |
| 70 |
* \defgroup libnfnetlink Functions in libnfnetlink.c [DEPRECATED] |
| 71 |
* This documentation is provided for the benefit of maintainers of legacy code. |
| 72 |
* |
| 73 |
* New applications should use |
| 74 |
* [libmnl](https://netfilter.org/projects/libmnl/doxygen/html/). |
| 75 |
* @{ |
| 76 |
*/ |
| 77 |
|
| 78 |
#ifndef NETLINK_ADD_MEMBERSHIP |
| 79 |
#define NETLINK_ADD_MEMBERSHIP 1 |
| 80 |
#endif |
| 81 |
|
| 82 |
#ifndef SOL_NETLINK |
| 83 |
#define SOL_NETLINK 270 |
| 84 |
#endif |
| 85 |
|
| 86 |
#define nfnl_error(format, args...) \ |
| 87 |
fprintf(stderr, "%s: " format "\n", __FUNCTION__, ## args) |
| 88 |
|
| 89 |
#ifdef _NFNL_DEBUG |
| 90 |
#define nfnl_debug_dump_packet nfnl_dump_packet |
| 91 |
#else |
| 92 |
#define nfnl_debug_dump_packet(a, b, ...) |
| 93 |
#endif |
| 94 |
|
| 95 |
struct nfnl_subsys_handle { |
| 96 |
struct nfnl_handle *nfnlh; |
| 97 |
uint32_t subscriptions; |
| 98 |
uint8_t subsys_id; |
| 99 |
uint8_t cb_count; |
| 100 |
struct nfnl_callback *cb; /* array of callbacks */ |
| 101 |
}; |
| 102 |
|
| 103 |
#define NFNL_MAX_SUBSYS 16 /* enough for now */ |
| 104 |
|
| 105 |
#define NFNL_F_SEQTRACK_ENABLED (1 << 0) |
| 106 |
|
| 107 |
struct nfnl_handle { |
| 108 |
int fd; |
| 109 |
struct sockaddr_nl local; |
| 110 |
struct sockaddr_nl peer; |
| 111 |
uint32_t subscriptions; |
| 112 |
uint32_t seq; |
| 113 |
uint32_t dump; |
| 114 |
uint32_t rcv_buffer_size; /* for nfnl_catch */ |
| 115 |
uint32_t flags; |
| 116 |
struct nlmsghdr *last_nlhdr; |
| 117 |
struct nfnl_subsys_handle subsys[NFNL_MAX_SUBSYS+1]; |
| 118 |
}; |
| 119 |
|
| 120 |
void nfnl_dump_packet(struct nlmsghdr *nlh, int received_len, char *desc) |
| 121 |
{ |
| 122 |
void *nlmsg_data = NLMSG_DATA(nlh); |
| 123 |
struct nfattr *nfa = NFM_NFA(NLMSG_DATA(nlh)); |
| 124 |
int len = NFM_PAYLOAD(nlh); |
| 125 |
|
| 126 |
printf("%s called from %s\n", __FUNCTION__, desc); |
| 127 |
printf(" nlmsghdr = %p, received_len = %u\n", nlh, received_len); |
| 128 |
printf(" NLMSG_DATA(nlh) = %p (+%td bytes)\n", nlmsg_data, |
| 129 |
(nlmsg_data - (void *)nlh)); |
| 130 |
printf(" NFM_NFA(NLMSG_DATA(nlh)) = %p (+%td bytes)\n", |
| 131 |
nfa, ((void *)nfa - (void *)nlh)); |
| 132 |
printf(" NFM_PAYLOAD(nlh) = %u\n", len); |
| 133 |
printf(" nlmsg_type = %u, nlmsg_len = %u, nlmsg_seq = %u " |
| 134 |
"nlmsg_flags = 0x%x\n", nlh->nlmsg_type, nlh->nlmsg_len, |
| 135 |
nlh->nlmsg_seq, nlh->nlmsg_flags); |
| 136 |
|
| 137 |
while (NFA_OK(nfa, len)) { |
| 138 |
printf(" nfa@%p: nfa_type=%u, nfa_len=%u\n", |
| 139 |
nfa, NFA_TYPE(nfa), nfa->nfa_len); |
| 140 |
nfa = NFA_NEXT(nfa,len); |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
/** |
| 145 |
* nfnl_fd - returns the descriptor that identifies the socket |
| 146 |
* @nfnlh: nfnetlink handler |
| 147 |
* |
| 148 |
* Use this function if you need to interact with the socket. Common |
| 149 |
* scenarios are the use of poll()/select() to achieve multiplexation. |
| 150 |
*/ |
| 151 |
int nfnl_fd(struct nfnl_handle *h) |
| 152 |
{ |
| 153 |
assert(h); |
| 154 |
return h->fd; |
| 155 |
} |
| 156 |
|
| 157 |
/** |
| 158 |
* nfnl_portid - returns the Netlink port ID of this socket |
| 159 |
* @h: nfnetlink handler |
| 160 |
*/ |
| 161 |
unsigned int nfnl_portid(const struct nfnl_handle *h) |
| 162 |
{ |
| 163 |
assert(h); |
| 164 |
return h->local.nl_pid; |
| 165 |
} |
| 166 |
|
| 167 |
static int recalc_rebind_subscriptions(struct nfnl_handle *nfnlh) |
| 168 |
{ |
| 169 |
int i, err; |
| 170 |
uint32_t new_subscriptions = nfnlh->subscriptions; |
| 171 |
|
| 172 |
for (i = 0; i < NFNL_MAX_SUBSYS; i++) |
| 173 |
new_subscriptions |= nfnlh->subsys[i].subscriptions; |
| 174 |
|
| 175 |
nfnlh->local.nl_groups = new_subscriptions; |
| 176 |
err = bind(nfnlh->fd, (struct sockaddr *)&nfnlh->local, |
| 177 |
sizeof(nfnlh->local)); |
| 178 |
if (err == -1) |
| 179 |
return -1; |
| 180 |
|
| 181 |
nfnlh->subscriptions = new_subscriptions; |
| 182 |
|
| 183 |
return 0; |
| 184 |
} |
| 185 |
|
| 186 |
/** |
| 187 |
* nfnl_open - open a nfnetlink handler |
| 188 |
* |
| 189 |
* This function creates a nfnetlink handler, this is required to establish |
| 190 |
* a communication between the userspace and the nfnetlink system. |
| 191 |
* |
| 192 |
* On success, a valid address that points to a nfnl_handle structure |
| 193 |
* is returned. On error, NULL is returned and errno is set approapiately. |
| 194 |
*/ |
| 195 |
struct nfnl_handle *nfnl_open(void) |
| 196 |
{ |
| 197 |
struct nfnl_handle *nfnlh; |
| 198 |
unsigned int addr_len; |
| 199 |
|
| 200 |
nfnlh = malloc(sizeof(*nfnlh)); |
| 201 |
if (!nfnlh) |
| 202 |
return NULL; |
| 203 |
|
| 204 |
memset(nfnlh, 0, sizeof(*nfnlh)); |
| 205 |
nfnlh->fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_NETFILTER); |
| 206 |
if (nfnlh->fd == -1) |
| 207 |
goto err_free; |
| 208 |
|
| 209 |
nfnlh->local.nl_family = AF_NETLINK; |
| 210 |
nfnlh->peer.nl_family = AF_NETLINK; |
| 211 |
|
| 212 |
addr_len = sizeof(nfnlh->local); |
| 213 |
if (getsockname(nfnlh->fd, (struct sockaddr *)&nfnlh->local, &addr_len)) |
| 214 |
goto err_close; |
| 215 |
if (addr_len != sizeof(nfnlh->local)) { |
| 216 |
errno = EINVAL; |
| 217 |
goto err_close; |
| 218 |
} |
| 219 |
if (nfnlh->local.nl_family != AF_NETLINK) { |
| 220 |
errno = EINVAL; |
| 221 |
goto err_close; |
| 222 |
} |
| 223 |
nfnlh->seq = time(NULL); |
| 224 |
nfnlh->rcv_buffer_size = NFNL_BUFFSIZE; |
| 225 |
|
| 226 |
/* don't set pid here, only first socket of process has real pid !!! |
| 227 |
* binding to pid '0' will default */ |
| 228 |
|
| 229 |
/* let us do the initial bind */ |
| 230 |
if (recalc_rebind_subscriptions(nfnlh) < 0) |
| 231 |
goto err_close; |
| 232 |
|
| 233 |
/* use getsockname to get the netlink pid that the kernel assigned us */ |
| 234 |
addr_len = sizeof(nfnlh->local); |
| 235 |
if (getsockname(nfnlh->fd, (struct sockaddr *)&nfnlh->local, &addr_len)) |
| 236 |
goto err_close; |
| 237 |
if (addr_len != sizeof(nfnlh->local)) { |
| 238 |
errno = EINVAL; |
| 239 |
goto err_close; |
| 240 |
} |
| 241 |
/* sequence tracking enabled by default */ |
| 242 |
nfnlh->flags |= NFNL_F_SEQTRACK_ENABLED; |
| 243 |
|
| 244 |
return nfnlh; |
| 245 |
|
| 246 |
err_close: |
| 247 |
close(nfnlh->fd); |
| 248 |
err_free: |
| 249 |
free(nfnlh); |
| 250 |
return NULL; |
| 251 |
} |
| 252 |
|
| 253 |
/** |
| 254 |
* nfnl_set_sequence_tracking - set netlink sequence tracking |
| 255 |
* @h: nfnetlink handler |
| 256 |
*/ |
| 257 |
void nfnl_set_sequence_tracking(struct nfnl_handle *h) |
| 258 |
{ |
| 259 |
h->flags |= NFNL_F_SEQTRACK_ENABLED; |
| 260 |
} |
| 261 |
|
| 262 |
/** |
| 263 |
* nfnl_unset_sequence_tracking - set netlink sequence tracking |
| 264 |
* @h: nfnetlink handler |
| 265 |
*/ |
| 266 |
void nfnl_unset_sequence_tracking(struct nfnl_handle *h) |
| 267 |
{ |
| 268 |
h->flags &= ~NFNL_F_SEQTRACK_ENABLED; |
| 269 |
} |
| 270 |
|
| 271 |
/** |
| 272 |
* nfnl_set_rcv_buffer_size - set the size of the receive buffer |
| 273 |
* @h: libnfnetlink handler |
| 274 |
* @size: buffer size |
| 275 |
* |
| 276 |
* This function sets the size of the receive buffer size, i.e. the size |
| 277 |
* of the buffer used by nfnl_recv. Default value is 4096 bytes. |
| 278 |
*/ |
| 279 |
void nfnl_set_rcv_buffer_size(struct nfnl_handle *h, unsigned int size) |
| 280 |
{ |
| 281 |
h->rcv_buffer_size = size; |
| 282 |
} |
| 283 |
|
| 284 |
/** |
| 285 |
* nfnl_subsys_open - open a netlink subsystem |
| 286 |
* @nfnlh: libnfnetlink handle |
| 287 |
* @subsys_id: which nfnetlink subsystem we are interested in |
| 288 |
* @cb_count: number of callbacks that are used maximum. |
| 289 |
* @subscriptions: netlink groups we want to be subscribed to |
| 290 |
* |
| 291 |
* This function creates a subsystem handler that contains the set of |
| 292 |
* callbacks that handle certain types of messages coming from a netfilter |
| 293 |
* subsystem. Initially the callback set is empty, you can register callbacks |
| 294 |
* via nfnl_callback_register(). |
| 295 |
* |
| 296 |
* On error, NULL is returned and errno is set appropiately. On success, |
| 297 |
* a valid address that points to a nfnl_subsys_handle structure is returned. |
| 298 |
*/ |
| 299 |
struct nfnl_subsys_handle * |
| 300 |
nfnl_subsys_open(struct nfnl_handle *nfnlh, uint8_t subsys_id, |
| 301 |
uint8_t cb_count, uint32_t subscriptions) |
| 302 |
{ |
| 303 |
struct nfnl_subsys_handle *ssh; |
| 304 |
|
| 305 |
assert(nfnlh); |
| 306 |
|
| 307 |
if (subsys_id > NFNL_MAX_SUBSYS) { |
| 308 |
errno = ENOENT; |
| 309 |
return NULL; |
| 310 |
} |
| 311 |
|
| 312 |
ssh = &nfnlh->subsys[subsys_id]; |
| 313 |
if (ssh->cb) { |
| 314 |
errno = EBUSY; |
| 315 |
return NULL; |
| 316 |
} |
| 317 |
|
| 318 |
ssh->cb = calloc(cb_count, sizeof(*(ssh->cb))); |
| 319 |
if (!ssh->cb) |
| 320 |
return NULL; |
| 321 |
|
| 322 |
ssh->nfnlh = nfnlh; |
| 323 |
ssh->cb_count = cb_count; |
| 324 |
ssh->subscriptions = subscriptions; |
| 325 |
ssh->subsys_id = subsys_id; |
| 326 |
|
| 327 |
/* although now we have nfnl_join to subscribe to certain |
| 328 |
* groups, just keep this to ensure compatibility */ |
| 329 |
if (recalc_rebind_subscriptions(nfnlh) < 0) { |
| 330 |
free(ssh->cb); |
| 331 |
ssh->cb = NULL; |
| 332 |
return NULL; |
| 333 |
} |
| 334 |
|
| 335 |
return ssh; |
| 336 |
} |
| 337 |
|
| 338 |
/** |
| 339 |
* nfnl_subsys_close - close a nfnetlink subsys handler |
| 340 |
* @ssh: nfnetlink subsystem handler |
| 341 |
* |
| 342 |
* Release all the callbacks registered in a subsystem handler. |
| 343 |
*/ |
| 344 |
void nfnl_subsys_close(struct nfnl_subsys_handle *ssh) |
| 345 |
{ |
| 346 |
assert(ssh); |
| 347 |
|
| 348 |
ssh->subscriptions = 0; |
| 349 |
ssh->cb_count = 0; |
| 350 |
if (ssh->cb) { |
| 351 |
free(ssh->cb); |
| 352 |
ssh->cb = NULL; |
| 353 |
} |
| 354 |
} |
| 355 |
|
| 356 |
/** |
| 357 |
* nfnl_close - close a nfnetlink handler |
| 358 |
* @nfnlh: nfnetlink handler |
| 359 |
* |
| 360 |
* This function closes the nfnetlink handler. On success, 0 is returned. |
| 361 |
* On error, -1 is returned and errno is set appropiately. |
| 362 |
*/ |
| 363 |
int nfnl_close(struct nfnl_handle *nfnlh) |
| 364 |
{ |
| 365 |
int i, ret; |
| 366 |
|
| 367 |
assert(nfnlh); |
| 368 |
|
| 369 |
for (i = 0; i < NFNL_MAX_SUBSYS; i++) |
| 370 |
nfnl_subsys_close(&nfnlh->subsys[i]); |
| 371 |
|
| 372 |
ret = close(nfnlh->fd); |
| 373 |
if (ret < 0) |
| 374 |
return ret; |
| 375 |
|
| 376 |
free(nfnlh); |
| 377 |
|
| 378 |
return 0; |
| 379 |
} |
| 380 |
|
| 381 |
/** |
| 382 |
* nfnl_join - join a nfnetlink multicast group |
| 383 |
* @nfnlh: nfnetlink handler |
| 384 |
* @group: group we want to join |
| 385 |
* |
| 386 |
* This function is used to join a certain multicast group. It must be |
| 387 |
* called once the nfnetlink handler has been created. If any doubt, |
| 388 |
* just use it if you have to listen to nfnetlink events. |
| 389 |
* |
| 390 |
* On success, 0 is returned. On error, -1 is returned and errno is set |
| 391 |
* approapiately. |
| 392 |
*/ |
| 393 |
int nfnl_join(const struct nfnl_handle *nfnlh, unsigned int group) |
| 394 |
{ |
| 395 |
assert(nfnlh); |
| 396 |
return setsockopt(nfnlh->fd, SOL_NETLINK, NETLINK_ADD_MEMBERSHIP, |
| 397 |
&group, sizeof(group)); |
| 398 |
} |
| 399 |
|
| 400 |
/** |
| 401 |
* nfnl_send - send a nfnetlink message through netlink socket |
| 402 |
* @nfnlh: nfnetlink handler |
| 403 |
* @n: netlink message |
| 404 |
* |
| 405 |
* On success, the number of bytes is returned. On error, -1 is returned |
| 406 |
* and errno is set appropiately. |
| 407 |
*/ |
| 408 |
int nfnl_send(struct nfnl_handle *nfnlh, struct nlmsghdr *n) |
| 409 |
{ |
| 410 |
assert(nfnlh); |
| 411 |
assert(n); |
| 412 |
|
| 413 |
nfnl_debug_dump_packet(n, n->nlmsg_len+sizeof(*n), "nfnl_send"); |
| 414 |
|
| 415 |
return sendto(nfnlh->fd, n, n->nlmsg_len, 0, |
| 416 |
(struct sockaddr *)&nfnlh->peer, sizeof(nfnlh->peer)); |
| 417 |
} |
| 418 |
|
| 419 |
int nfnl_sendmsg(const struct nfnl_handle *nfnlh, const struct msghdr *msg, |
| 420 |
unsigned int flags) |
| 421 |
{ |
| 422 |
assert(nfnlh); |
| 423 |
assert(msg); |
| 424 |
|
| 425 |
return sendmsg(nfnlh->fd, msg, flags); |
| 426 |
} |
| 427 |
|
| 428 |
int nfnl_sendiov(const struct nfnl_handle *nfnlh, const struct iovec *iov, |
| 429 |
unsigned int num, unsigned int flags) |
| 430 |
{ |
| 431 |
struct msghdr msg; |
| 432 |
|
| 433 |
assert(nfnlh); |
| 434 |
|
| 435 |
msg.msg_name = (struct sockaddr *) &nfnlh->peer; |
| 436 |
msg.msg_namelen = sizeof(nfnlh->peer); |
| 437 |
msg.msg_iov = (struct iovec *) iov; |
| 438 |
msg.msg_iovlen = num; |
| 439 |
msg.msg_control = NULL; |
| 440 |
msg.msg_controllen = 0; |
| 441 |
msg.msg_flags = 0; |
| 442 |
|
| 443 |
return nfnl_sendmsg(nfnlh, &msg, flags); |
| 444 |
} |
| 445 |
|
| 446 |
/** |
| 447 |
* nfnl_fill_hdr - fill in netlink and nfnetlink header |
| 448 |
* @nfnlh: nfnetlink handle |
| 449 |
* @nlh: netlink message to be filled in |
| 450 |
* @len: length of _payload_ bytes (not including nfgenmsg) |
| 451 |
* @family: AF_INET / ... |
| 452 |
* @res_id: resource id |
| 453 |
* @msg_type: nfnetlink message type (without subsystem) |
| 454 |
* @msg_flags: netlink message flags |
| 455 |
* |
| 456 |
* This function sets up appropiately the nfnetlink header. See that the |
| 457 |
* pointer to the netlink message passed must point to a memory region of |
| 458 |
* at least the size of struct nlmsghdr + struct nfgenmsg. |
| 459 |
*/ |
| 460 |
void nfnl_fill_hdr(struct nfnl_subsys_handle *ssh, |
| 461 |
struct nlmsghdr *nlh, unsigned int len, |
| 462 |
uint8_t family, |
| 463 |
uint16_t res_id, |
| 464 |
uint16_t msg_type, |
| 465 |
uint16_t msg_flags) |
| 466 |
{ |
| 467 |
assert(ssh); |
| 468 |
assert(nlh); |
| 469 |
|
| 470 |
struct nfgenmsg *nfg = (void *)nlh + sizeof(*nlh); |
| 471 |
|
| 472 |
nlh->nlmsg_len = NLMSG_LENGTH(len+sizeof(*nfg)); |
| 473 |
nlh->nlmsg_type = (ssh->subsys_id<<8)|msg_type; |
| 474 |
nlh->nlmsg_flags = msg_flags; |
| 475 |
nlh->nlmsg_pid = 0; |
| 476 |
|
| 477 |
if (ssh->nfnlh->flags & NFNL_F_SEQTRACK_ENABLED) { |
| 478 |
nlh->nlmsg_seq = ++ssh->nfnlh->seq; |
| 479 |
/* kernel uses sequence number zero for events */ |
| 480 |
if (!ssh->nfnlh->seq) |
| 481 |
nlh->nlmsg_seq = ssh->nfnlh->seq = time(NULL); |
| 482 |
} else { |
| 483 |
/* unset sequence number, ignore it */ |
| 484 |
nlh->nlmsg_seq = 0; |
| 485 |
} |
| 486 |
|
| 487 |
nfg->nfgen_family = family; |
| 488 |
nfg->version = NFNETLINK_V0; |
| 489 |
nfg->res_id = htons(res_id); |
| 490 |
} |
| 491 |
|
| 492 |
struct nfattr * |
| 493 |
nfnl_parse_hdr(const struct nfnl_handle *nfnlh, |
| 494 |
const struct nlmsghdr *nlh, |
| 495 |
struct nfgenmsg **genmsg) |
| 496 |
{ |
| 497 |
if (nlh->nlmsg_len < NLMSG_LENGTH(sizeof(struct nfgenmsg))) |
| 498 |
return NULL; |
| 499 |
|
| 500 |
if (nlh->nlmsg_len == NLMSG_LENGTH(sizeof(struct nfgenmsg))) { |
| 501 |
if (genmsg) |
| 502 |
*genmsg = (void *)nlh + sizeof(*nlh); |
| 503 |
return NULL; |
| 504 |
} |
| 505 |
|
| 506 |
if (genmsg) |
| 507 |
*genmsg = (void *)nlh + sizeof(*nlh); |
| 508 |
|
| 509 |
return (void *)nlh + NLMSG_LENGTH(sizeof(struct nfgenmsg)); |
| 510 |
} |
| 511 |
|
| 512 |
/** |
| 513 |
* nfnl_recv - receive data from a nfnetlink subsystem |
| 514 |
* @h: nfnetlink handler |
| 515 |
* @buf: buffer where the data will be stored |
| 516 |
* @len: size of the buffer |
| 517 |
* |
| 518 |
* This function doesn't perform any sanity checking. So do no expect |
| 519 |
* that the data is well-formed. Such checkings are done by the parsing |
| 520 |
* functions. |
| 521 |
* |
| 522 |
* On success, 0 is returned. On error, -1 is returned and errno is set |
| 523 |
* appropiately. |
| 524 |
* |
| 525 |
* Note that ENOBUFS is returned in case that nfnetlink is exhausted. In |
| 526 |
* that case is possible that the information requested is incomplete. |
| 527 |
*/ |
| 528 |
ssize_t |
| 529 |
nfnl_recv(const struct nfnl_handle *h, unsigned char *buf, size_t len) |
| 530 |
{ |
| 531 |
socklen_t addrlen; |
| 532 |
int status; |
| 533 |
struct sockaddr_nl peer; |
| 534 |
|
| 535 |
assert(h); |
| 536 |
assert(buf); |
| 537 |
assert(len > 0); |
| 538 |
|
| 539 |
if (len < sizeof(struct nlmsgerr) |
| 540 |
|| len < sizeof(struct nlmsghdr)) { |
| 541 |
errno = EBADMSG; |
| 542 |
return -1; |
| 543 |
} |
| 544 |
|
| 545 |
addrlen = sizeof(h->peer); |
| 546 |
status = recvfrom(h->fd, buf, len, 0, (struct sockaddr *)&peer, |
| 547 |
&addrlen); |
| 548 |
if (status <= 0) |
| 549 |
return status; |
| 550 |
|
| 551 |
if (addrlen != sizeof(peer)) { |
| 552 |
errno = EINVAL; |
| 553 |
return -1; |
| 554 |
} |
| 555 |
|
| 556 |
if (peer.nl_pid != 0) { |
| 557 |
errno = ENOMSG; |
| 558 |
return -1; |
| 559 |
} |
| 560 |
|
| 561 |
return status; |
| 562 |
} |
| 563 |
/** |
| 564 |
* nfnl_listen: listen for one or more netlink messages |
| 565 |
* @nfnhl: libnfnetlink handle |
| 566 |
* @handler: callback function to be called for every netlink message |
| 567 |
* - the callback handler should normally return 0 |
| 568 |
* - but may return a negative error code which will cause |
| 569 |
* nfnl_listen to return immediately with the same error code |
| 570 |
* - or return a postivie error code which will cause |
| 571 |
* nfnl_listen to return after it has finished processing all |
| 572 |
* the netlink messages in the current packet |
| 573 |
* Thus a positive error code will terminate nfnl_listen "soon" |
| 574 |
* without any loss of data, a negative error code will terminate |
| 575 |
* nfnl_listen "very soon" and throw away data already read from |
| 576 |
* the netlink socket. |
| 577 |
* @jarg: opaque argument passed on to callback |
| 578 |
* |
| 579 |
* This function is used to receive and process messages coming from an open |
| 580 |
* nfnetlink handler like events or information request via nfnl_send(). |
| 581 |
* |
| 582 |
* On error, -1 is returned, unfortunately errno is not always set |
| 583 |
* appropiately. For that reason, the use of this function is DEPRECATED. |
| 584 |
* Please, use nfnl_receive_process() instead. |
| 585 |
*/ |
| 586 |
int nfnl_listen(struct nfnl_handle *nfnlh, |
| 587 |
int (*handler)(struct sockaddr_nl *, struct nlmsghdr *n, |
| 588 |
void *), void *jarg) |
| 589 |
{ |
| 590 |
struct sockaddr_nl nladdr; |
| 591 |
char buf[NFNL_BUFFSIZE] __attribute__ ((aligned)); |
| 592 |
struct iovec iov; |
| 593 |
int remain; |
| 594 |
struct nlmsghdr *h; |
| 595 |
struct nlmsgerr *msgerr; |
| 596 |
int quit=0; |
| 597 |
|
| 598 |
struct msghdr msg = { |
| 599 |
.msg_name = &nladdr, |
| 600 |
.msg_namelen = sizeof(nladdr), |
| 601 |
.msg_iov = &iov, |
| 602 |
.msg_iovlen = 1, |
| 603 |
}; |
| 604 |
|
| 605 |
memset(&nladdr, 0, sizeof(nladdr)); |
| 606 |
nladdr.nl_family = AF_NETLINK; |
| 607 |
iov.iov_base = buf; |
| 608 |
iov.iov_len = sizeof(buf); |
| 609 |
|
| 610 |
while (! quit) { |
| 611 |
remain = recvmsg(nfnlh->fd, &msg, 0); |
| 612 |
if (remain < 0) { |
| 613 |
if (errno == EINTR) |
| 614 |
continue; |
| 615 |
/* Bad file descriptor */ |
| 616 |
else if (errno == EBADF) |
| 617 |
break; |
| 618 |
else if (errno == EAGAIN) |
| 619 |
break; |
| 620 |
nfnl_error("recvmsg overrun: %s", strerror(errno)); |
| 621 |
continue; |
| 622 |
} |
| 623 |
if (remain == 0) { |
| 624 |
nfnl_error("EOF on netlink"); |
| 625 |
return -1; |
| 626 |
} |
| 627 |
if (msg.msg_namelen != sizeof(nladdr)) { |
| 628 |
nfnl_error("Bad sender address len (%d)", |
| 629 |
msg.msg_namelen); |
| 630 |
return -1; |
| 631 |
} |
| 632 |
|
| 633 |
for (h = (struct nlmsghdr *)buf; remain >= sizeof(*h);) { |
| 634 |
int err; |
| 635 |
int len = h->nlmsg_len; |
| 636 |
int l = len - sizeof(*h); |
| 637 |
|
| 638 |
if (l < 0 || len > remain) { |
| 639 |
if (msg.msg_flags & MSG_TRUNC) { |
| 640 |
nfnl_error("MSG_TRUNC"); |
| 641 |
return -1; |
| 642 |
} |
| 643 |
nfnl_error("Malformed msg (len=%d)", len); |
| 644 |
return -1; |
| 645 |
} |
| 646 |
|
| 647 |
/* end of messages reached, let's return */ |
| 648 |
if (h->nlmsg_type == NLMSG_DONE) |
| 649 |
return 0; |
| 650 |
|
| 651 |
/* Break the loop if success is explicitely |
| 652 |
* reported via NLM_F_ACK flag set */ |
| 653 |
if (h->nlmsg_type == NLMSG_ERROR) { |
| 654 |
msgerr = NLMSG_DATA(h); |
| 655 |
return msgerr->error; |
| 656 |
} |
| 657 |
|
| 658 |
err = handler(&nladdr, h, jarg); |
| 659 |
if (err < 0) |
| 660 |
return err; |
| 661 |
quit |= err; |
| 662 |
|
| 663 |
/* FIXME: why not _NEXT macros, etc.? */ |
| 664 |
//h = NLMSG_NEXT(h, remain); |
| 665 |
remain -= NLMSG_ALIGN(len); |
| 666 |
h = (struct nlmsghdr *)((char *)h + NLMSG_ALIGN(len)); |
| 667 |
} |
| 668 |
if (msg.msg_flags & MSG_TRUNC) { |
| 669 |
nfnl_error("MSG_TRUNC"); |
| 670 |
continue; |
| 671 |
} |
| 672 |
if (remain) { |
| 673 |
nfnl_error("remnant size %d", remain); |
| 674 |
return -1; |
| 675 |
} |
| 676 |
} |
| 677 |
|
| 678 |
return quit; |
| 679 |
} |
| 680 |
|
| 681 |
/** |
| 682 |
* nfnl_talk - send a request and then receive and process messages returned |
| 683 |
* @nfnlh: nfnetelink handler |
| 684 |
* @n: netlink message that contains the request |
| 685 |
* @peer: peer PID |
| 686 |
* @groups: netlink groups |
| 687 |
* @junk: callback called if out-of-sequence messages were received |
| 688 |
* @jarg: data for the junk callback |
| 689 |
* |
| 690 |
* This function is used to request an action that does not returns any |
| 691 |
* information. On error, a negative value is returned, errno could be |
| 692 |
* set appropiately. For that reason, the use of this function is DEPRECATED. |
| 693 |
* Please, use nfnl_query() instead. |
| 694 |
*/ |
| 695 |
int nfnl_talk(struct nfnl_handle *nfnlh, struct nlmsghdr *n, pid_t peer, |
| 696 |
unsigned groups, struct nlmsghdr *answer, |
| 697 |
int (*junk)(struct sockaddr_nl *, struct nlmsghdr *n, void *), |
| 698 |
void *jarg) |
| 699 |
{ |
| 700 |
char buf[NFNL_BUFFSIZE] __attribute__ ((aligned)); |
| 701 |
struct sockaddr_nl nladdr; |
| 702 |
struct nlmsghdr *h; |
| 703 |
unsigned int seq; |
| 704 |
int status; |
| 705 |
struct iovec iov = { |
| 706 |
n, n->nlmsg_len |
| 707 |
}; |
| 708 |
struct msghdr msg = { |
| 709 |
.msg_name = &nladdr, |
| 710 |
.msg_namelen = sizeof(nladdr), |
| 711 |
.msg_iov = &iov, |
| 712 |
.msg_iovlen = 1, |
| 713 |
}; |
| 714 |
|
| 715 |
memset(&nladdr, 0, sizeof(nladdr)); |
| 716 |
nladdr.nl_family = AF_NETLINK; |
| 717 |
nladdr.nl_pid = peer; |
| 718 |
nladdr.nl_groups = groups; |
| 719 |
|
| 720 |
n->nlmsg_seq = seq = ++nfnlh->seq; |
| 721 |
/* FIXME: why ? */ |
| 722 |
if (!answer) |
| 723 |
n->nlmsg_flags |= NLM_F_ACK; |
| 724 |
|
| 725 |
status = sendmsg(nfnlh->fd, &msg, 0); |
| 726 |
if (status < 0) { |
| 727 |
nfnl_error("sendmsg(netlink) %s", strerror(errno)); |
| 728 |
return -1; |
| 729 |
} |
| 730 |
iov.iov_base = buf; |
| 731 |
iov.iov_len = sizeof(buf); |
| 732 |
|
| 733 |
while (1) { |
| 734 |
status = recvmsg(nfnlh->fd, &msg, 0); |
| 735 |
if (status < 0) { |
| 736 |
if (errno == EINTR) |
| 737 |
continue; |
| 738 |
nfnl_error("recvmsg over-run"); |
| 739 |
continue; |
| 740 |
} |
| 741 |
if (status == 0) { |
| 742 |
nfnl_error("EOF on netlink"); |
| 743 |
return -1; |
| 744 |
} |
| 745 |
if (msg.msg_namelen != sizeof(nladdr)) { |
| 746 |
nfnl_error("Bad sender address len %d", |
| 747 |
msg.msg_namelen); |
| 748 |
return -1; |
| 749 |
} |
| 750 |
|
| 751 |
for (h = (struct nlmsghdr *)buf; status >= sizeof(*h); ) { |
| 752 |
int len = h->nlmsg_len; |
| 753 |
int l = len - sizeof(*h); |
| 754 |
int err; |
| 755 |
|
| 756 |
if (l < 0 || len > status) { |
| 757 |
if (msg.msg_flags & MSG_TRUNC) { |
| 758 |
nfnl_error("Truncated message\n"); |
| 759 |
return -1; |
| 760 |
} |
| 761 |
nfnl_error("Malformed message: len=%d\n", len); |
| 762 |
return -1; /* FIXME: libnetlink exits here */ |
| 763 |
} |
| 764 |
|
| 765 |
if (h->nlmsg_pid != nfnlh->local.nl_pid || |
| 766 |
h->nlmsg_seq != seq) { |
| 767 |
if (junk) { |
| 768 |
err = junk(&nladdr, h, jarg); |
| 769 |
if (err < 0) |
| 770 |
return err; |
| 771 |
} |
| 772 |
goto cont; |
| 773 |
} |
| 774 |
|
| 775 |
if (h->nlmsg_type == NLMSG_ERROR) { |
| 776 |
struct nlmsgerr *err = NLMSG_DATA(h); |
| 777 |
if (l < sizeof(struct nlmsgerr)) |
| 778 |
nfnl_error("ERROR truncated\n"); |
| 779 |
else { |
| 780 |
errno = -err->error; |
| 781 |
if (errno == 0) { |
| 782 |
if (answer) |
| 783 |
memcpy(answer, h, h->nlmsg_len); |
| 784 |
return 0; |
| 785 |
} |
| 786 |
perror("NFNETLINK answers"); |
| 787 |
} |
| 788 |
return err->error; |
| 789 |
} |
| 790 |
if (answer) { |
| 791 |
memcpy(answer, h, h->nlmsg_len); |
| 792 |
return 0; |
| 793 |
} |
| 794 |
|
| 795 |
nfnl_error("Unexpected reply!\n"); |
| 796 |
cont: |
| 797 |
status -= NLMSG_ALIGN(len); |
| 798 |
h = (struct nlmsghdr *)((char *)h + NLMSG_ALIGN(len)); |
| 799 |
} |
| 800 |
if (msg.msg_flags & MSG_TRUNC) { |
| 801 |
nfnl_error("Messages truncated\n"); |
| 802 |
continue; |
| 803 |
} |
| 804 |
if (status) |
| 805 |
nfnl_error("Remnant of size %d\n", status); |
| 806 |
} |
| 807 |
} |
| 808 |
|
| 809 |
/** |
| 810 |
* nfnl_addattr_l - Add variable length attribute to nlmsghdr |
| 811 |
* @n: netlink message header to which attribute is to be added |
| 812 |
* @maxlen: maximum length of netlink message header |
| 813 |
* @type: type of new attribute |
| 814 |
* @data: content of new attribute |
| 815 |
* @len: attribute length |
| 816 |
*/ |
| 817 |
int nfnl_addattr_l(struct nlmsghdr *n, int maxlen, int type, const void *data, |
| 818 |
int alen) |
| 819 |
{ |
| 820 |
int len = NFA_LENGTH(alen); |
| 821 |
struct nfattr *nfa; |
| 822 |
|
| 823 |
assert(n); |
| 824 |
assert(maxlen > 0); |
| 825 |
assert(type >= 0); |
| 826 |
|
| 827 |
if ((NLMSG_ALIGN(n->nlmsg_len) + len) > maxlen) { |
| 828 |
errno = ENOSPC; |
| 829 |
return -1; |
| 830 |
} |
| 831 |
|
| 832 |
nfa = NLMSG_TAIL(n); |
| 833 |
nfa->nfa_type = type; |
| 834 |
nfa->nfa_len = len; |
| 835 |
memcpy(NFA_DATA(nfa), data, alen); |
| 836 |
memset((uint8_t *)nfa + nfa->nfa_len, 0, NFA_ALIGN(alen) - alen); |
| 837 |
n->nlmsg_len = (NLMSG_ALIGN(n->nlmsg_len) + NFA_ALIGN(len)); |
| 838 |
return 0; |
| 839 |
} |
| 840 |
|
| 841 |
/** |
| 842 |
* nfnl_nfa_addattr_l - Add variable length attribute to struct nfattr |
| 843 |
* |
| 844 |
* @nfa: struct nfattr |
| 845 |
* @maxlen: maximal length of nfattr buffer |
| 846 |
* @type: type for new attribute |
| 847 |
* @data: content of new attribute |
| 848 |
* @alen: length of new attribute |
| 849 |
* |
| 850 |
*/ |
| 851 |
int nfnl_nfa_addattr_l(struct nfattr *nfa, int maxlen, int type, |
| 852 |
const void *data, int alen) |
| 853 |
{ |
| 854 |
struct nfattr *subnfa; |
| 855 |
int len = NFA_LENGTH(alen); |
| 856 |
|
| 857 |
assert(nfa); |
| 858 |
assert(maxlen > 0); |
| 859 |
assert(type >= 0); |
| 860 |
|
| 861 |
if (NFA_ALIGN(nfa->nfa_len) + len > maxlen) { |
| 862 |
errno = ENOSPC; |
| 863 |
return -1; |
| 864 |
} |
| 865 |
|
| 866 |
subnfa = (struct nfattr *)(((char *)nfa) + NFA_ALIGN(nfa->nfa_len)); |
| 867 |
subnfa->nfa_type = type; |
| 868 |
subnfa->nfa_len = len; |
| 869 |
memcpy(NFA_DATA(subnfa), data, alen); |
| 870 |
nfa->nfa_len = NFA_ALIGN(nfa->nfa_len) + len; |
| 871 |
|
| 872 |
return 0; |
| 873 |
} |
| 874 |
|
| 875 |
/** |
| 876 |
* nfnl_addattr8 - Add uint8_t attribute to nlmsghdr |
| 877 |
* |
| 878 |
* @n: netlink message header to which attribute is to be added |
| 879 |
* @maxlen: maximum length of netlink message header |
| 880 |
* @type: type of new attribute |
| 881 |
* @data: content of new attribute |
| 882 |
*/ |
| 883 |
int nfnl_addattr8(struct nlmsghdr *n, int maxlen, int type, uint8_t data) |
| 884 |
{ |
| 885 |
assert(n); |
| 886 |
assert(maxlen > 0); |
| 887 |
assert(type >= 0); |
| 888 |
|
| 889 |
return nfnl_addattr_l(n, maxlen, type, &data, sizeof(data)); |
| 890 |
} |
| 891 |
|
| 892 |
/** |
| 893 |
* nfnl_nfa_addattr16 - Add uint16_t attribute to struct nfattr |
| 894 |
* |
| 895 |
* @nfa: struct nfattr |
| 896 |
* @maxlen: maximal length of nfattr buffer |
| 897 |
* @type: type for new attribute |
| 898 |
* @data: content of new attribute |
| 899 |
* |
| 900 |
*/ |
| 901 |
int nfnl_nfa_addattr16(struct nfattr *nfa, int maxlen, int type, |
| 902 |
uint16_t data) |
| 903 |
{ |
| 904 |
assert(nfa); |
| 905 |
assert(maxlen > 0); |
| 906 |
assert(type >= 0); |
| 907 |
|
| 908 |
return nfnl_nfa_addattr_l(nfa, maxlen, type, &data, sizeof(data)); |
| 909 |
} |
| 910 |
|
| 911 |
/** |
| 912 |
* nfnl_addattr16 - Add uint16_t attribute to nlmsghdr |
| 913 |
* |
| 914 |
* @n: netlink message header to which attribute is to be added |
| 915 |
* @maxlen: maximum length of netlink message header |
| 916 |
* @type: type of new attribute |
| 917 |
* @data: content of new attribute |
| 918 |
* |
| 919 |
*/ |
| 920 |
int nfnl_addattr16(struct nlmsghdr *n, int maxlen, int type, |
| 921 |
uint16_t data) |
| 922 |
{ |
| 923 |
assert(n); |
| 924 |
assert(maxlen > 0); |
| 925 |
assert(type >= 0); |
| 926 |
|
| 927 |
return nfnl_addattr_l(n, maxlen, type, &data, sizeof(data)); |
| 928 |
} |
| 929 |
|
| 930 |
/** |
| 931 |
* nfnl_nfa_addattr32 - Add uint32_t attribute to struct nfattr |
| 932 |
* |
| 933 |
* @nfa: struct nfattr |
| 934 |
* @maxlen: maximal length of nfattr buffer |
| 935 |
* @type: type for new attribute |
| 936 |
* @data: content of new attribute |
| 937 |
* |
| 938 |
*/ |
| 939 |
int nfnl_nfa_addattr32(struct nfattr *nfa, int maxlen, int type, |
| 940 |
uint32_t data) |
| 941 |
{ |
| 942 |
assert(nfa); |
| 943 |
assert(maxlen > 0); |
| 944 |
assert(type >= 0); |
| 945 |
|
| 946 |
return nfnl_nfa_addattr_l(nfa, maxlen, type, &data, sizeof(data)); |
| 947 |
} |
| 948 |
|
| 949 |
/** |
| 950 |
* nfnl_addattr32 - Add uint32_t attribute to nlmsghdr |
| 951 |
* |
| 952 |
* @n: netlink message header to which attribute is to be added |
| 953 |
* @maxlen: maximum length of netlink message header |
| 954 |
* @type: type of new attribute |
| 955 |
* @data: content of new attribute |
| 956 |
* |
| 957 |
*/ |
| 958 |
int nfnl_addattr32(struct nlmsghdr *n, int maxlen, int type, |
| 959 |
uint32_t data) |
| 960 |
{ |
| 961 |
assert(n); |
| 962 |
assert(maxlen > 0); |
| 963 |
assert(type >= 0); |
| 964 |
|
| 965 |
return nfnl_addattr_l(n, maxlen, type, &data, sizeof(data)); |
| 966 |
} |
| 967 |
|
| 968 |
/** |
| 969 |
* nfnl_parse_attr - Parse a list of nfattrs into a pointer array |
| 970 |
* |
| 971 |
* @tb: pointer array, will be filled in (output) |
| 972 |
* @max: size of pointer array |
| 973 |
* @nfa: pointer to list of nfattrs |
| 974 |
* @len: length of 'nfa' |
| 975 |
* |
| 976 |
* The returned value is equal to the number of remaining bytes of the netlink |
| 977 |
* message that cannot be parsed. |
| 978 |
*/ |
| 979 |
int nfnl_parse_attr(struct nfattr *tb[], int max, struct nfattr *nfa, int len) |
| 980 |
{ |
| 981 |
assert(tb); |
| 982 |
assert(max > 0); |
| 983 |
assert(nfa); |
| 984 |
|
| 985 |
memset(tb, 0, sizeof(struct nfattr *) * max); |
| 986 |
|
| 987 |
while (NFA_OK(nfa, len)) { |
| 988 |
if (NFA_TYPE(nfa) <= max) |
| 989 |
tb[NFA_TYPE(nfa)-1] = nfa; |
| 990 |
nfa = NFA_NEXT(nfa,len); |
| 991 |
} |
| 992 |
|
| 993 |
return len; |
| 994 |
} |
| 995 |
|
| 996 |
/** |
| 997 |
* nfnl_build_nfa_iovec - Build two iovec's from tag, length and value |
| 998 |
* |
| 999 |
* @iov: pointer to array of two 'struct iovec' (caller-allocated) |
| 1000 |
* @nfa: pointer to 'struct nfattr' (caller-allocated) |
| 1001 |
* @type: type (tag) of attribute |
| 1002 |
* @len: length of value |
| 1003 |
* @val: pointer to buffer containing 'value' |
| 1004 |
* |
| 1005 |
*/ |
| 1006 |
void nfnl_build_nfa_iovec(struct iovec *iov, struct nfattr *nfa, |
| 1007 |
uint16_t type, uint32_t len, unsigned char *val) |
| 1008 |
{ |
| 1009 |
assert(iov); |
| 1010 |
assert(nfa); |
| 1011 |
|
| 1012 |
/* Set the attribut values */ |
| 1013 |
nfa->nfa_len = sizeof(struct nfattr) + len; |
| 1014 |
nfa->nfa_type = type; |
| 1015 |
|
| 1016 |
iov[0].iov_base = nfa; |
| 1017 |
iov[0].iov_len = sizeof(*nfa); |
| 1018 |
iov[1].iov_base = val; |
| 1019 |
iov[1].iov_len = NFA_ALIGN(len); |
| 1020 |
} |
| 1021 |
|
| 1022 |
#ifndef SO_RCVBUFFORCE |
| 1023 |
#define SO_RCVBUFFORCE (33) |
| 1024 |
#endif |
| 1025 |
|
| 1026 |
/** |
| 1027 |
* nfnl_rcvbufsiz - set the socket buffer size |
| 1028 |
* @h: nfnetlink handler |
| 1029 |
* @size: size of the buffer we want to set |
| 1030 |
* |
| 1031 |
* This function sets the new size of the socket buffer. Use this setting |
| 1032 |
* to increase the socket buffer size if your system is reporting ENOBUFS |
| 1033 |
* errors. |
| 1034 |
* |
| 1035 |
* This function returns the new size of the socket buffer. |
| 1036 |
*/ |
| 1037 |
unsigned int nfnl_rcvbufsiz(const struct nfnl_handle *h, unsigned int size) |
| 1038 |
{ |
| 1039 |
int status; |
| 1040 |
socklen_t socklen = sizeof(size); |
| 1041 |
unsigned int read_size = 0; |
| 1042 |
|
| 1043 |
assert(h); |
| 1044 |
|
| 1045 |
/* first we try the FORCE option, which is introduced in kernel |
| 1046 |
* 2.6.14 to give "root" the ability to override the system wide |
| 1047 |
* maximum */ |
| 1048 |
status = setsockopt(h->fd, SOL_SOCKET, SO_RCVBUFFORCE, &size, socklen); |
| 1049 |
if (status < 0) { |
| 1050 |
/* if this didn't work, we try at least to get the system |
| 1051 |
* wide maximum (or whatever the user requested) */ |
| 1052 |
setsockopt(h->fd, SOL_SOCKET, SO_RCVBUF, &size, socklen); |
| 1053 |
} |
| 1054 |
getsockopt(h->fd, SOL_SOCKET, SO_RCVBUF, &read_size, &socklen); |
| 1055 |
|
| 1056 |
return read_size; |
| 1057 |
} |
| 1058 |
|
| 1059 |
/** |
| 1060 |
* nfnl_get_msg_first - get the first message of a multipart netlink message |
| 1061 |
* @h: nfnetlink handle |
| 1062 |
* @buf: data received that we want to process |
| 1063 |
* @len: size of the data received |
| 1064 |
* |
| 1065 |
* This function returns a pointer to the first netlink message contained |
| 1066 |
* in the chunk of data received from certain nfnetlink subsystem. |
| 1067 |
* |
| 1068 |
* On success, a valid address that points to the netlink message is returned. |
| 1069 |
* On error, NULL is returned. |
| 1070 |
*/ |
| 1071 |
struct nlmsghdr *nfnl_get_msg_first(struct nfnl_handle *h, |
| 1072 |
const unsigned char *buf, |
| 1073 |
size_t len) |
| 1074 |
{ |
| 1075 |
struct nlmsghdr *nlh; |
| 1076 |
|
| 1077 |
assert(h); |
| 1078 |
assert(buf); |
| 1079 |
assert(len > 0); |
| 1080 |
|
| 1081 |
/* first message in buffer */ |
| 1082 |
nlh = (struct nlmsghdr *)buf; |
| 1083 |
if (!NLMSG_OK(nlh, len)) |
| 1084 |
return NULL; |
| 1085 |
h->last_nlhdr = nlh; |
| 1086 |
|
| 1087 |
return nlh; |
| 1088 |
} |
| 1089 |
|
| 1090 |
struct nlmsghdr *nfnl_get_msg_next(struct nfnl_handle *h, |
| 1091 |
const unsigned char *buf, |
| 1092 |
size_t len) |
| 1093 |
{ |
| 1094 |
struct nlmsghdr *nlh; |
| 1095 |
size_t remain_len; |
| 1096 |
|
| 1097 |
assert(h); |
| 1098 |
assert(buf); |
| 1099 |
assert(len > 0); |
| 1100 |
|
| 1101 |
/* if last header in handle not inside this buffer, |
| 1102 |
* drop reference to last header */ |
| 1103 |
if (!h->last_nlhdr || |
| 1104 |
(unsigned char *)h->last_nlhdr >= (buf + len) || |
| 1105 |
(unsigned char *)h->last_nlhdr < buf) { |
| 1106 |
h->last_nlhdr = NULL; |
| 1107 |
return NULL; |
| 1108 |
} |
| 1109 |
|
| 1110 |
/* n-th part of multipart message */ |
| 1111 |
if (h->last_nlhdr->nlmsg_type == NLMSG_DONE || |
| 1112 |
h->last_nlhdr->nlmsg_flags & NLM_F_MULTI) { |
| 1113 |
/* if last part in multipart message or no |
| 1114 |
* multipart message at all, return */ |
| 1115 |
h->last_nlhdr = NULL; |
| 1116 |
return NULL; |
| 1117 |
} |
| 1118 |
|
| 1119 |
remain_len = (len - ((unsigned char *)h->last_nlhdr - buf)); |
| 1120 |
nlh = NLMSG_NEXT(h->last_nlhdr, remain_len); |
| 1121 |
|
| 1122 |
if (!NLMSG_OK(nlh, remain_len)) { |
| 1123 |
h->last_nlhdr = NULL; |
| 1124 |
return NULL; |
| 1125 |
} |
| 1126 |
|
| 1127 |
h->last_nlhdr = nlh; |
| 1128 |
|
| 1129 |
return nlh; |
| 1130 |
} |
| 1131 |
|
| 1132 |
/** |
| 1133 |
* nfnl_callback_register - register a callback for a certain message type |
| 1134 |
* @ssh: nfnetlink subsys handler |
| 1135 |
* @type: subsys call |
| 1136 |
* @cb: nfnetlink callback to be registered |
| 1137 |
* |
| 1138 |
* On success, 0 is returned. On error, -1 is returned and errno is set |
| 1139 |
* appropiately. |
| 1140 |
*/ |
| 1141 |
int nfnl_callback_register(struct nfnl_subsys_handle *ssh, |
| 1142 |
uint8_t type, struct nfnl_callback *cb) |
| 1143 |
{ |
| 1144 |
assert(ssh); |
| 1145 |
assert(cb); |
| 1146 |
|
| 1147 |
if (type >= ssh->cb_count) { |
| 1148 |
errno = EINVAL; |
| 1149 |
return -1; |
| 1150 |
} |
| 1151 |
|
| 1152 |
memcpy(&ssh->cb[type], cb, sizeof(*cb)); |
| 1153 |
|
| 1154 |
return 0; |
| 1155 |
} |
| 1156 |
|
| 1157 |
/** |
| 1158 |
* nfnl_callback_unregister - unregister a certain callback |
| 1159 |
* @ssh: nfnetlink subsys handler |
| 1160 |
* @type: subsys call |
| 1161 |
* |
| 1162 |
* On sucess, 0 is returned. On error, -1 is returned and errno is |
| 1163 |
* set appropiately. |
| 1164 |
*/ |
| 1165 |
int nfnl_callback_unregister(struct nfnl_subsys_handle *ssh, uint8_t type) |
| 1166 |
{ |
| 1167 |
assert(ssh); |
| 1168 |
|
| 1169 |
if (type >= ssh->cb_count) { |
| 1170 |
errno = EINVAL; |
| 1171 |
return -1; |
| 1172 |
} |
| 1173 |
|
| 1174 |
ssh->cb[type].call = NULL; |
| 1175 |
|
| 1176 |
return 0; |
| 1177 |
} |
| 1178 |
|
| 1179 |
int nfnl_check_attributes(const struct nfnl_handle *h, |
| 1180 |
const struct nlmsghdr *nlh, |
| 1181 |
struct nfattr *nfa[]) |
| 1182 |
{ |
| 1183 |
assert(h); |
| 1184 |
assert(nlh); |
| 1185 |
assert(nfa); |
| 1186 |
|
| 1187 |
int min_len; |
| 1188 |
uint8_t type = NFNL_MSG_TYPE(nlh->nlmsg_type); |
| 1189 |
uint8_t subsys_id = NFNL_SUBSYS_ID(nlh->nlmsg_type); |
| 1190 |
const struct nfnl_subsys_handle *ssh; |
| 1191 |
struct nfnl_callback *cb; |
| 1192 |
|
| 1193 |
if (subsys_id > NFNL_MAX_SUBSYS) |
| 1194 |
return -EINVAL; |
| 1195 |
|
| 1196 |
ssh = &h->subsys[subsys_id]; |
| 1197 |
cb = &ssh->cb[type]; |
| 1198 |
|
| 1199 |
#if 1 |
| 1200 |
/* checks need to be enabled as soon as this is called from |
| 1201 |
* somebody else than __nfnl_handle_msg */ |
| 1202 |
if (type >= ssh->cb_count) |
| 1203 |
return -EINVAL; |
| 1204 |
|
| 1205 |
min_len = NLMSG_SPACE(sizeof(struct nfgenmsg)); |
| 1206 |
if (nlh->nlmsg_len < min_len) |
| 1207 |
return -EINVAL; |
| 1208 |
#endif |
| 1209 |
memset(nfa, 0, sizeof(struct nfattr *) * cb->attr_count); |
| 1210 |
|
| 1211 |
if (nlh->nlmsg_len > min_len) { |
| 1212 |
struct nfattr *attr = NFM_NFA(NLMSG_DATA(nlh)); |
| 1213 |
int attrlen = nlh->nlmsg_len - NLMSG_ALIGN(min_len); |
| 1214 |
|
| 1215 |
while (NFA_OK(attr, attrlen)) { |
| 1216 |
unsigned int flavor = NFA_TYPE(attr); |
| 1217 |
if (flavor) { |
| 1218 |
if (flavor > cb->attr_count) { |
| 1219 |
/* we have received an attribute from |
| 1220 |
* the kernel which we don't understand |
| 1221 |
* yet. We have to silently ignore this |
| 1222 |
* for the sake of future compatibility */ |
| 1223 |
attr = NFA_NEXT(attr, attrlen); |
| 1224 |
continue; |
| 1225 |
} |
| 1226 |
nfa[flavor - 1] = attr; |
| 1227 |
} |
| 1228 |
attr = NFA_NEXT(attr, attrlen); |
| 1229 |
} |
| 1230 |
} |
| 1231 |
|
| 1232 |
return 0; |
| 1233 |
} |
| 1234 |
|
| 1235 |
static int __nfnl_handle_msg(struct nfnl_handle *h, struct nlmsghdr *nlh, |
| 1236 |
int len) |
| 1237 |
{ |
| 1238 |
struct nfnl_subsys_handle *ssh; |
| 1239 |
uint8_t type = NFNL_MSG_TYPE(nlh->nlmsg_type); |
| 1240 |
uint8_t subsys_id = NFNL_SUBSYS_ID(nlh->nlmsg_type); |
| 1241 |
int err = 0; |
| 1242 |
|
| 1243 |
if (subsys_id > NFNL_MAX_SUBSYS) |
| 1244 |
return -1; |
| 1245 |
|
| 1246 |
ssh = &h->subsys[subsys_id]; |
| 1247 |
|
| 1248 |
if (nlh->nlmsg_len < NLMSG_LENGTH(NLMSG_ALIGN(sizeof(struct nfgenmsg)))) |
| 1249 |
return -1; |
| 1250 |
|
| 1251 |
if (type >= ssh->cb_count) |
| 1252 |
return -1; |
| 1253 |
|
| 1254 |
if (ssh->cb[type].attr_count) { |
| 1255 |
struct nfattr *nfa[ssh->cb[type].attr_count]; |
| 1256 |
|
| 1257 |
err = nfnl_check_attributes(h, nlh, nfa); |
| 1258 |
if (err < 0) |
| 1259 |
return err; |
| 1260 |
if (ssh->cb[type].call) |
| 1261 |
return ssh->cb[type].call(nlh, nfa, ssh->cb[type].data); |
| 1262 |
} |
| 1263 |
return 0; |
| 1264 |
} |
| 1265 |
|
| 1266 |
int nfnl_handle_packet(struct nfnl_handle *h, char *buf, int len) |
| 1267 |
{ |
| 1268 |
|
| 1269 |
while (len >= NLMSG_SPACE(0)) { |
| 1270 |
uint32_t rlen; |
| 1271 |
struct nlmsghdr *nlh = (struct nlmsghdr *)buf; |
| 1272 |
|
| 1273 |
if (nlh->nlmsg_len < sizeof(struct nlmsghdr) |
| 1274 |
|| len < nlh->nlmsg_len) |
| 1275 |
return -1; |
| 1276 |
|
| 1277 |
rlen = NLMSG_ALIGN(nlh->nlmsg_len); |
| 1278 |
if (rlen > len) |
| 1279 |
rlen = len; |
| 1280 |
|
| 1281 |
if (__nfnl_handle_msg(h, nlh, rlen) < 0) |
| 1282 |
return -1; |
| 1283 |
|
| 1284 |
len -= rlen; |
| 1285 |
buf += rlen; |
| 1286 |
} |
| 1287 |
return 0; |
| 1288 |
} |
| 1289 |
|
| 1290 |
static int nfnl_is_error(struct nfnl_handle *h, struct nlmsghdr *nlh) |
| 1291 |
{ |
| 1292 |
/* This message is an ACK or a DONE */ |
| 1293 |
if (nlh->nlmsg_type == NLMSG_ERROR || |
| 1294 |
(nlh->nlmsg_type == NLMSG_DONE && |
| 1295 |
nlh->nlmsg_flags & NLM_F_MULTI)) { |
| 1296 |
if (nlh->nlmsg_len < NLMSG_ALIGN(sizeof(struct nlmsgerr))) { |
| 1297 |
errno = EBADMSG; |
| 1298 |
return 1; |
| 1299 |
} |
| 1300 |
errno = -(*((int *)NLMSG_DATA(nlh))); |
| 1301 |
return 1; |
| 1302 |
} |
| 1303 |
return 0; |
| 1304 |
} |
| 1305 |
|
| 1306 |
/* On error, -1 is returned and errno is set appropiately. On success, |
| 1307 |
* 0 is returned if there is no more data to process, >0 if there is |
| 1308 |
* more data to process */ |
| 1309 |
static int nfnl_step(struct nfnl_handle *h, struct nlmsghdr *nlh) |
| 1310 |
{ |
| 1311 |
struct nfnl_subsys_handle *ssh; |
| 1312 |
uint8_t type = NFNL_MSG_TYPE(nlh->nlmsg_type); |
| 1313 |
uint8_t subsys_id = NFNL_SUBSYS_ID(nlh->nlmsg_type); |
| 1314 |
|
| 1315 |
/* Is this an error message? */ |
| 1316 |
if (nfnl_is_error(h, nlh)) { |
| 1317 |
/* This is an ACK */ |
| 1318 |
if (errno == 0) |
| 1319 |
return 0; |
| 1320 |
/* This an error message */ |
| 1321 |
return -1; |
| 1322 |
} |
| 1323 |
|
| 1324 |
/* nfnetlink sanity checks: check for nfgenmsg size */ |
| 1325 |
if (nlh->nlmsg_len < NLMSG_SPACE(sizeof(struct nfgenmsg))) { |
| 1326 |
errno = ENOSPC; |
| 1327 |
return -1; |
| 1328 |
} |
| 1329 |
|
| 1330 |
if (subsys_id > NFNL_MAX_SUBSYS) { |
| 1331 |
errno = ENOENT; |
| 1332 |
return -1; |
| 1333 |
} |
| 1334 |
|
| 1335 |
ssh = &h->subsys[subsys_id]; |
| 1336 |
if (!ssh) { |
| 1337 |
errno = ENOENT; |
| 1338 |
return -1; |
| 1339 |
} |
| 1340 |
|
| 1341 |
if (type >= ssh->cb_count) { |
| 1342 |
errno = ENOENT; |
| 1343 |
return -1; |
| 1344 |
} |
| 1345 |
|
| 1346 |
if (ssh->cb[type].attr_count) { |
| 1347 |
int err; |
| 1348 |
struct nfattr *tb[ssh->cb[type].attr_count]; |
| 1349 |
struct nfattr *attr = NFM_NFA(NLMSG_DATA(nlh)); |
| 1350 |
int min_len = NLMSG_SPACE(sizeof(struct nfgenmsg)); |
| 1351 |
int len = nlh->nlmsg_len - NLMSG_ALIGN(min_len); |
| 1352 |
|
| 1353 |
err = nfnl_parse_attr(tb, ssh->cb[type].attr_count, attr, len); |
| 1354 |
if (err == -1) |
| 1355 |
return -1; |
| 1356 |
|
| 1357 |
if (ssh->cb[type].call) { |
| 1358 |
/* |
| 1359 |
* On error, the callback returns NFNL_CB_FAILURE and |
| 1360 |
* errno must be explicitely set. On success, |
| 1361 |
* NFNL_CB_STOP is returned and we're done, otherwise |
| 1362 |
* NFNL_CB_CONTINUE means that we want to continue |
| 1363 |
* data processing. |
| 1364 |
*/ |
| 1365 |
return ssh->cb[type].call(nlh, |
| 1366 |
tb, |
| 1367 |
ssh->cb[type].data); |
| 1368 |
} |
| 1369 |
} |
| 1370 |
/* no callback set, continue data processing */ |
| 1371 |
return 1; |
| 1372 |
} |
| 1373 |
|
| 1374 |
/** |
| 1375 |
* nfnl_process - process data coming from a nfnetlink system |
| 1376 |
* @h: nfnetlink handler |
| 1377 |
* @buf: buffer that contains the netlink message |
| 1378 |
* @len: size of the data contained in the buffer (not the buffer size) |
| 1379 |
* |
| 1380 |
* This function processes all the nfnetlink messages contained inside a |
| 1381 |
* buffer. It performs the appropiate sanity checks and passes the message |
| 1382 |
* to a certain handler that is registered via register_callback(). |
| 1383 |
* |
| 1384 |
* On success, NFNL_CB_STOP is returned if the data processing has finished. |
| 1385 |
* If a value NFNL_CB_CONTINUE is returned, then there is more data to |
| 1386 |
* process. On error, NFNL_CB_CONTINUE is returned and errno is set to the |
| 1387 |
* appropiate value. |
| 1388 |
* |
| 1389 |
* In case that the callback returns NFNL_CB_FAILURE, errno may be set by |
| 1390 |
* the library client. If your callback decides not to process data anymore |
| 1391 |
* for any reason, then it must return NFNL_CB_STOP. Otherwise, if the |
| 1392 |
* callback continues the processing NFNL_CB_CONTINUE is returned. |
| 1393 |
*/ |
| 1394 |
int nfnl_process(struct nfnl_handle *h, const unsigned char *buf, size_t len) |
| 1395 |
{ |
| 1396 |
int ret = 0; |
| 1397 |
struct nlmsghdr *nlh = (struct nlmsghdr *)buf; |
| 1398 |
|
| 1399 |
assert(h); |
| 1400 |
assert(buf); |
| 1401 |
assert(len > 0); |
| 1402 |
|
| 1403 |
/* check for out of sequence message */ |
| 1404 |
if (nlh->nlmsg_seq && nlh->nlmsg_seq != h->seq) { |
| 1405 |
errno = EILSEQ; |
| 1406 |
return -1; |
| 1407 |
} |
| 1408 |
while (len >= NLMSG_SPACE(0) && NLMSG_OK(nlh, len)) { |
| 1409 |
|
| 1410 |
ret = nfnl_step(h, nlh); |
| 1411 |
if (ret <= NFNL_CB_STOP) |
| 1412 |
break; |
| 1413 |
|
| 1414 |
nlh = NLMSG_NEXT(nlh, len); |
| 1415 |
} |
| 1416 |
return ret; |
| 1417 |
} |
| 1418 |
|
| 1419 |
/* |
| 1420 |
* New parsing functions based on iterators |
| 1421 |
*/ |
| 1422 |
|
| 1423 |
struct nfnl_iterator { |
| 1424 |
struct nlmsghdr *nlh; |
| 1425 |
unsigned int len; |
| 1426 |
}; |
| 1427 |
|
| 1428 |
/** |
| 1429 |
* nfnl_iterator_create: create an nfnetlink iterator |
| 1430 |
* @h: nfnetlink handler |
| 1431 |
* @buf: buffer that contains data received from a nfnetlink system |
| 1432 |
* @len: size of the data contained in the buffer (not the buffer size) |
| 1433 |
* |
| 1434 |
* This function creates an iterator that can be used to parse nfnetlink |
| 1435 |
* message one by one. The iterator gives more control to the programmer |
| 1436 |
* in the messages processing. |
| 1437 |
* |
| 1438 |
* On success, a valid address is returned. On error, NULL is returned |
| 1439 |
* and errno is set to the appropiate value. |
| 1440 |
*/ |
| 1441 |
struct nfnl_iterator * |
| 1442 |
nfnl_iterator_create(const struct nfnl_handle *h, |
| 1443 |
const char *buf, |
| 1444 |
size_t len) |
| 1445 |
{ |
| 1446 |
struct nlmsghdr *nlh; |
| 1447 |
struct nfnl_iterator *it; |
| 1448 |
|
| 1449 |
assert(h); |
| 1450 |
assert(buf); |
| 1451 |
assert(len > 0); |
| 1452 |
|
| 1453 |
it = malloc(sizeof(struct nfnl_iterator)); |
| 1454 |
if (!it) { |
| 1455 |
errno = ENOMEM; |
| 1456 |
return NULL; |
| 1457 |
} |
| 1458 |
|
| 1459 |
/* first message in buffer */ |
| 1460 |
nlh = (struct nlmsghdr *)buf; |
| 1461 |
if (len < NLMSG_SPACE(0) || !NLMSG_OK(nlh, len)) { |
| 1462 |
free(it); |
| 1463 |
errno = EBADMSG; |
| 1464 |
return NULL; |
| 1465 |
} |
| 1466 |
it->nlh = nlh; |
| 1467 |
it->len = len; |
| 1468 |
|
| 1469 |
return it; |
| 1470 |
} |
| 1471 |
|
| 1472 |
/** |
| 1473 |
* nfnl_iterator_destroy - destroy a nfnetlink iterator |
| 1474 |
* @it: nfnetlink iterator |
| 1475 |
* |
| 1476 |
* This function destroys a certain iterator. Nothing is returned. |
| 1477 |
*/ |
| 1478 |
void nfnl_iterator_destroy(struct nfnl_iterator *it) |
| 1479 |
{ |
| 1480 |
assert(it); |
| 1481 |
free(it); |
| 1482 |
} |
| 1483 |
|
| 1484 |
/** |
| 1485 |
* nfnl_iterator_process - process a nfnetlink message |
| 1486 |
* @h: nfnetlink handler |
| 1487 |
* @it: nfnetlink iterator that contains the current message to be proccesed |
| 1488 |
* |
| 1489 |
* This function process just the current message selected by the iterator. |
| 1490 |
* On success, a value greater or equal to zero is returned. On error, |
| 1491 |
* -1 is returned and errno is appropiately set. |
| 1492 |
*/ |
| 1493 |
int nfnl_iterator_process(struct nfnl_handle *h, struct nfnl_iterator *it) |
| 1494 |
{ |
| 1495 |
assert(h); |
| 1496 |
assert(it->nlh); |
| 1497 |
|
| 1498 |
/* check for out of sequence message */ |
| 1499 |
if (it->nlh->nlmsg_seq && it->nlh->nlmsg_seq != h->seq) { |
| 1500 |
errno = EILSEQ; |
| 1501 |
return -1; |
| 1502 |
} |
| 1503 |
if (it->len < NLMSG_SPACE(0) || !NLMSG_OK(it->nlh, it->len)) { |
| 1504 |
errno = EBADMSG; |
| 1505 |
return -1; |
| 1506 |
} |
| 1507 |
return nfnl_step(h, it->nlh); |
| 1508 |
} |
| 1509 |
|
| 1510 |
/** |
| 1511 |
* nfnl_iterator_next - get the next message hold by the iterator |
| 1512 |
* @h: nfnetlink handler |
| 1513 |
* @it: nfnetlink iterator that contains the current message processed |
| 1514 |
* |
| 1515 |
* This function update the current message to be processed pointer. |
| 1516 |
* It returns NFNL_CB_CONTINUE if there is still more messages to be |
| 1517 |
* processed, otherwise NFNL_CB_STOP is returned. |
| 1518 |
*/ |
| 1519 |
int nfnl_iterator_next(const struct nfnl_handle *h, struct nfnl_iterator *it) |
| 1520 |
{ |
| 1521 |
assert(h); |
| 1522 |
assert(it); |
| 1523 |
|
| 1524 |
it->nlh = NLMSG_NEXT(it->nlh, it->len); |
| 1525 |
if (!it->nlh) |
| 1526 |
return 0; |
| 1527 |
return 1; |
| 1528 |
} |
| 1529 |
|
| 1530 |
/** |
| 1531 |
* nfnl_catch - get responses from the nfnetlink system and process them |
| 1532 |
* @h: nfnetlink handler |
| 1533 |
* |
| 1534 |
* This function handles the data received from the nfnetlink system. |
| 1535 |
* For example, events generated by one of the subsystems. The message |
| 1536 |
* is passed to the callback registered via callback_register(). Note that |
| 1537 |
* this a replacement of nfnl_listen and its use is recommended. |
| 1538 |
* |
| 1539 |
* On success, 0 is returned. On error, a -1 is returned. If you do not |
| 1540 |
* want to listen to events anymore, then your callback must return |
| 1541 |
* NFNL_CB_STOP. |
| 1542 |
* |
| 1543 |
* Note that ENOBUFS is returned in case that nfnetlink is exhausted. In |
| 1544 |
* that case is possible that the information requested is incomplete. |
| 1545 |
*/ |
| 1546 |
int nfnl_catch(struct nfnl_handle *h) |
| 1547 |
{ |
| 1548 |
int ret; |
| 1549 |
|
| 1550 |
assert(h); |
| 1551 |
|
| 1552 |
while (1) { |
| 1553 |
unsigned char buf[h->rcv_buffer_size] |
| 1554 |
__attribute__ ((aligned)); |
| 1555 |
|
| 1556 |
ret = nfnl_recv(h, buf, sizeof(buf)); |
| 1557 |
if (ret == -1) { |
| 1558 |
/* interrupted syscall must retry */ |
| 1559 |
if (errno == EINTR) |
| 1560 |
continue; |
| 1561 |
break; |
| 1562 |
} |
| 1563 |
|
| 1564 |
ret = nfnl_process(h, buf, ret); |
| 1565 |
if (ret <= NFNL_CB_STOP) |
| 1566 |
break; |
| 1567 |
} |
| 1568 |
|
| 1569 |
return ret; |
| 1570 |
} |
| 1571 |
|
| 1572 |
/** |
| 1573 |
* nfnl_query - request/response communication challenge |
| 1574 |
* @h: nfnetlink handler |
| 1575 |
* @nlh: nfnetlink message to be sent |
| 1576 |
* |
| 1577 |
* This function sends a nfnetlink message to a certain subsystem and |
| 1578 |
* receives the response messages associated, such messages are passed to |
| 1579 |
* the callback registered via register_callback(). Note that this function |
| 1580 |
* is a replacement for nfnl_talk, its use is recommended. |
| 1581 |
* |
| 1582 |
* On success, 0 is returned. On error, a negative is returned. If your |
| 1583 |
* does not want to listen to events anymore, then your callback must |
| 1584 |
* return NFNL_CB_STOP. |
| 1585 |
* |
| 1586 |
* Note that ENOBUFS is returned in case that nfnetlink is exhausted. In |
| 1587 |
* that case is possible that the information requested is incomplete. |
| 1588 |
*/ |
| 1589 |
int nfnl_query(struct nfnl_handle *h, struct nlmsghdr *nlh) |
| 1590 |
{ |
| 1591 |
assert(h); |
| 1592 |
assert(nlh); |
| 1593 |
|
| 1594 |
if (nfnl_send(h, nlh) == -1) |
| 1595 |
return -1; |
| 1596 |
|
| 1597 |
return nfnl_catch(h); |
| 1598 |
} |
| 1599 |
|
| 1600 |
/** |
| 1601 |
* @} |
| 1602 |
*/ |