| 1 |
/* |
| 2 |
* (C) 2005-2011 by Pablo Neira Ayuso <[email protected]> |
| 3 |
* |
| 4 |
* This program is free software; you can redistribute it and/or modify it |
| 5 |
* under the terms of the GNU General Public License as published by |
| 6 |
* the Free Software Foundation; either version 2 of the License, or |
| 7 |
* (at your option) any later version. |
| 8 |
*/ |
| 9 |
|
| 10 |
#include <stdlib.h> |
| 11 |
#include <stdbool.h> |
| 12 |
#include <string.h> /* for memset */ |
| 13 |
#include <errno.h> |
| 14 |
#include <assert.h> |
| 15 |
#include <libmnl/libmnl.h> |
| 16 |
|
| 17 |
#include "internal/internal.h" |
| 18 |
|
| 19 |
/** |
| 20 |
* \mainpage |
| 21 |
* |
| 22 |
* libnetfilter_conntrack is a userspace library providing a programming |
| 23 |
* interface (API) to the in-kernel connection tracking state table. The |
| 24 |
* library libnetfilter_conntrack has been previously known as |
| 25 |
* libnfnetlink_conntrack and libctnetlink. This library is currently used by |
| 26 |
* conntrack-tools among many other applications. |
| 27 |
* |
| 28 |
* libnetfilter_conntrack homepage is: |
| 29 |
* http://netfilter.org/projects/libnetfilter_conntrack/ |
| 30 |
* |
| 31 |
* \section Dependencies |
| 32 |
* libnetfilter_conntrack requires libnfnetlink and a kernel that includes the |
| 33 |
* nf_conntrack_netlink subsystem (i.e. 2.6.14 or later, >= 2.6.18 recommended). |
| 34 |
* |
| 35 |
* \section Main Features |
| 36 |
* - listing/retrieving entries from the kernel connection tracking table. |
| 37 |
* - inserting/modifying/deleting entries from the kernel connection tracking |
| 38 |
* table. |
| 39 |
* - listing/retrieving entries from the kernel expect table. |
| 40 |
* - inserting/modifying/deleting entries from the kernel expect table. |
| 41 |
* \section Git Tree |
| 42 |
* The current development version of libnetfilter_conntrack can be accessed at |
| 43 |
* https://git.netfilter.org/libnetfilter_conntrack/ |
| 44 |
* |
| 45 |
* \section Privileges |
| 46 |
* You need the CAP_NET_ADMIN capability in order to allow your application |
| 47 |
* to receive events from and to send commands to kernel-space, excepting |
| 48 |
* the conntrack table dumping operation. |
| 49 |
* |
| 50 |
* \section using Using libnetfilter_conntrack |
| 51 |
* To write your own program using libnetfilter_conntrack, you should start by |
| 52 |
* reading the doxygen documentation (start by \link LibrarySetup \endlink page) |
| 53 |
* and check examples available under utils/ in the libnetfilter_conntrack |
| 54 |
* source code tree. You can compile these examples by invoking `make check'. |
| 55 |
* |
| 56 |
* \section Authors |
| 57 |
* libnetfilter_conntrack has been almost entirely written by Pablo Neira Ayuso. |
| 58 |
* |
| 59 |
* \section python Python Binding |
| 60 |
* pynetfilter_conntrack is a Python binding of libnetfilter_conntrack written |
| 61 |
* by Victor Stinner. You can visit his official web site at |
| 62 |
* http://software.inl.fr/trac/trac.cgi/wiki/pynetfilter_conntrack. |
| 63 |
*/ |
| 64 |
|
| 65 |
/** |
| 66 |
* \defgroup ct Conntrack object handling |
| 67 |
* @{ |
| 68 |
*/ |
| 69 |
|
| 70 |
/** |
| 71 |
* nfct_conntrack_new - allocate a new conntrack |
| 72 |
* |
| 73 |
* In case of success, this function returns a valid pointer to a memory blob, |
| 74 |
* otherwise NULL is returned and errno is set appropiately. |
| 75 |
*/ |
| 76 |
struct nf_conntrack *nfct_new(void) |
| 77 |
{ |
| 78 |
struct nf_conntrack *ct; |
| 79 |
|
| 80 |
ct = malloc(sizeof(struct nf_conntrack)); |
| 81 |
if (!ct) |
| 82 |
return NULL; |
| 83 |
|
| 84 |
memset(ct, 0, sizeof(struct nf_conntrack)); |
| 85 |
|
| 86 |
return ct; |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* nf_conntrack_destroy - release a conntrack object |
| 91 |
* \param ct pointer to the conntrack object |
| 92 |
*/ |
| 93 |
void nfct_destroy(struct nf_conntrack *ct) |
| 94 |
{ |
| 95 |
assert(ct != NULL); |
| 96 |
if (ct->secctx) |
| 97 |
free(ct->secctx); |
| 98 |
if (ct->helper_info) |
| 99 |
free(ct->helper_info); |
| 100 |
if (ct->connlabels) |
| 101 |
nfct_bitmask_destroy(ct->connlabels); |
| 102 |
if (ct->connlabels_mask) |
| 103 |
nfct_bitmask_destroy(ct->connlabels_mask); |
| 104 |
free(ct); |
| 105 |
ct = NULL; /* bugtrap */ |
| 106 |
} |
| 107 |
|
| 108 |
/** |
| 109 |
* nf_sizeof - return the size in bytes of a certain conntrack object |
| 110 |
* \param ct pointer to the conntrack object |
| 111 |
* |
| 112 |
* This function is DEPRECATED, don't use it in your code. |
| 113 |
*/ |
| 114 |
size_t nfct_sizeof(const struct nf_conntrack *ct) |
| 115 |
{ |
| 116 |
assert(ct != NULL); |
| 117 |
return sizeof(*ct); |
| 118 |
} |
| 119 |
|
| 120 |
/** |
| 121 |
* nfct_maxsize - return the maximum size in bytes of a conntrack object |
| 122 |
* |
| 123 |
* Use this function if you want to allocate a conntrack object in the stack |
| 124 |
* instead of the heap. For example: |
| 125 |
* \verbatim |
| 126 |
char buf[nfct_maxsize()]; |
| 127 |
struct nf_conntrack *ct = (struct nf_conntrack *) buf; |
| 128 |
memset(ct, 0, nfct_maxsize()); |
| 129 |
\endverbatim |
| 130 |
* Note: As for now this function returns the same size that nfct_sizeof(ct) |
| 131 |
* does although _this could change in the future_. Therefore, do not assume |
| 132 |
* that nfct_sizeof(ct) == nfct_maxsize(). |
| 133 |
* |
| 134 |
* This function is DEPRECATED, don't use it in your code. |
| 135 |
*/ |
| 136 |
size_t nfct_maxsize(void) |
| 137 |
{ |
| 138 |
return sizeof(struct nf_conntrack); |
| 139 |
} |
| 140 |
|
| 141 |
/** |
| 142 |
* nfct_clone - clone a conntrack object |
| 143 |
* \param ct pointer to a valid conntrack object |
| 144 |
* |
| 145 |
* On error, NULL is returned and errno is appropiately set. Otherwise, |
| 146 |
* a valid pointer to the clone conntrack is returned. |
| 147 |
*/ |
| 148 |
struct nf_conntrack *nfct_clone(const struct nf_conntrack *ct) |
| 149 |
{ |
| 150 |
struct nf_conntrack *clone; |
| 151 |
|
| 152 |
assert(ct != NULL); |
| 153 |
|
| 154 |
if ((clone = nfct_new()) == NULL) |
| 155 |
return NULL; |
| 156 |
nfct_copy(clone, ct, NFCT_CP_OVERRIDE); |
| 157 |
|
| 158 |
return clone; |
| 159 |
} |
| 160 |
|
| 161 |
/** |
| 162 |
* nfct_setobjopt - set a certain option for a conntrack object |
| 163 |
* \param ct conntrack object |
| 164 |
* \param option option parameter |
| 165 |
* |
| 166 |
* In case of error, -1 is returned and errno is appropiately set. On success, |
| 167 |
* 0 is returned. |
| 168 |
*/ |
| 169 |
int nfct_setobjopt(struct nf_conntrack *ct, unsigned int option) |
| 170 |
{ |
| 171 |
assert(ct != NULL); |
| 172 |
|
| 173 |
if (unlikely(option > NFCT_SOPT_MAX)) { |
| 174 |
errno = EOPNOTSUPP; |
| 175 |
return -1; |
| 176 |
} |
| 177 |
|
| 178 |
return __setobjopt(ct, option); |
| 179 |
} |
| 180 |
|
| 181 |
/** |
| 182 |
* nfct_getobjopt - get a certain option for a conntrack object |
| 183 |
* \param ct conntrack object |
| 184 |
* \param option option parameter |
| 185 |
* |
| 186 |
* In case of error, -1 is returned and errno is appropiately set. On success, |
| 187 |
* 0 is returned. |
| 188 |
*/ |
| 189 |
int nfct_getobjopt(const struct nf_conntrack *ct, unsigned int option) |
| 190 |
{ |
| 191 |
assert(ct != NULL); |
| 192 |
|
| 193 |
if (unlikely(option > NFCT_GOPT_MAX)) { |
| 194 |
errno = EOPNOTSUPP; |
| 195 |
return -1; |
| 196 |
} |
| 197 |
|
| 198 |
return __getobjopt(ct, option); |
| 199 |
} |
| 200 |
|
| 201 |
/** |
| 202 |
* @} |
| 203 |
*/ |
| 204 |
|
| 205 |
/** |
| 206 |
* \defgroup LibrarySetup Library setup |
| 207 |
* @{ |
| 208 |
*/ |
| 209 |
|
| 210 |
/** |
| 211 |
* nf_callback_register - register a callback |
| 212 |
* \param h library handler |
| 213 |
* \param type message type (see enum nf_conntrack_msg_type definition) |
| 214 |
* \param cb callback used to process conntrack received |
| 215 |
* \param data data used by the callback, if any. |
| 216 |
* |
| 217 |
* This function register a callback to handle the conntrack received, |
| 218 |
* in case of error -1 is returned and errno is set appropiately, otherwise |
| 219 |
* 0 is returned. |
| 220 |
* |
| 221 |
* Note that the data parameter is optional, if you do not want to pass any |
| 222 |
* data to your callback, then use NULL. |
| 223 |
*/ |
| 224 |
int nfct_callback_register(struct nfct_handle *h, |
| 225 |
enum nf_conntrack_msg_type type, |
| 226 |
int (*cb)(enum nf_conntrack_msg_type type, |
| 227 |
struct nf_conntrack *ct, |
| 228 |
void *data), |
| 229 |
void *data) |
| 230 |
{ |
| 231 |
struct __data_container *container; |
| 232 |
|
| 233 |
assert(h != NULL); |
| 234 |
|
| 235 |
container = malloc(sizeof(struct __data_container)); |
| 236 |
if (!container) |
| 237 |
return -1; |
| 238 |
memset(container, 0, sizeof(struct __data_container)); |
| 239 |
|
| 240 |
h->cb = cb; |
| 241 |
container->h = h; |
| 242 |
container->type = type; |
| 243 |
container->data = data; |
| 244 |
|
| 245 |
h->nfnl_cb_ct.call = __callback; |
| 246 |
h->nfnl_cb_ct.data = container; |
| 247 |
h->nfnl_cb_ct.attr_count = CTA_MAX; |
| 248 |
|
| 249 |
nfnl_callback_register(h->nfnlssh_ct, |
| 250 |
IPCTNL_MSG_CT_NEW, |
| 251 |
&h->nfnl_cb_ct); |
| 252 |
|
| 253 |
nfnl_callback_register(h->nfnlssh_ct, |
| 254 |
IPCTNL_MSG_CT_DELETE, |
| 255 |
&h->nfnl_cb_ct); |
| 256 |
|
| 257 |
return 0; |
| 258 |
} |
| 259 |
|
| 260 |
/** |
| 261 |
* nfct_callback_unregister - unregister a callback |
| 262 |
* \param h library handler |
| 263 |
*/ |
| 264 |
void nfct_callback_unregister(struct nfct_handle *h) |
| 265 |
{ |
| 266 |
assert(h != NULL); |
| 267 |
|
| 268 |
nfnl_callback_unregister(h->nfnlssh_ct, IPCTNL_MSG_CT_NEW); |
| 269 |
nfnl_callback_unregister(h->nfnlssh_ct, IPCTNL_MSG_CT_DELETE); |
| 270 |
|
| 271 |
h->cb = NULL; |
| 272 |
free(h->nfnl_cb_ct.data); |
| 273 |
|
| 274 |
h->nfnl_cb_ct.call = NULL; |
| 275 |
h->nfnl_cb_ct.data = NULL; |
| 276 |
h->nfnl_cb_ct.attr_count = 0; |
| 277 |
} |
| 278 |
|
| 279 |
/** |
| 280 |
* nf_callback_register2 - register a callback |
| 281 |
* \param h library handler |
| 282 |
* \param cb callback used to process conntrack received |
| 283 |
* \param data data used by the callback, if any. |
| 284 |
* |
| 285 |
* This function register a callback to handle the conntrack received, |
| 286 |
* in case of error -1 is returned and errno is set appropiately, otherwise |
| 287 |
* 0 is returned. |
| 288 |
* |
| 289 |
* Note that the data parameter is optional, if you do not want to pass any |
| 290 |
* data to your callback, then use NULL. |
| 291 |
* |
| 292 |
* NOTICE: The difference with nf_callback_register() is that this function |
| 293 |
* uses the new callback interface that includes the Netlink header. |
| 294 |
* |
| 295 |
* WARNING: Don't mix nf_callback_register() and nf_callback_register2() |
| 296 |
* calls, use only once at a time. |
| 297 |
*/ |
| 298 |
int nfct_callback_register2(struct nfct_handle *h, |
| 299 |
enum nf_conntrack_msg_type type, |
| 300 |
int (*cb)(const struct nlmsghdr *nlh, |
| 301 |
enum nf_conntrack_msg_type type, |
| 302 |
struct nf_conntrack *ct, |
| 303 |
void *data), |
| 304 |
void *data) |
| 305 |
{ |
| 306 |
struct __data_container *container; |
| 307 |
|
| 308 |
assert(h != NULL); |
| 309 |
|
| 310 |
container = calloc(1, sizeof(struct __data_container)); |
| 311 |
if (container == NULL) |
| 312 |
return -1; |
| 313 |
|
| 314 |
h->cb2 = cb; |
| 315 |
container->h = h; |
| 316 |
container->type = type; |
| 317 |
container->data = data; |
| 318 |
|
| 319 |
h->nfnl_cb_ct.call = __callback; |
| 320 |
h->nfnl_cb_ct.data = container; |
| 321 |
h->nfnl_cb_ct.attr_count = CTA_MAX; |
| 322 |
|
| 323 |
nfnl_callback_register(h->nfnlssh_ct, |
| 324 |
IPCTNL_MSG_CT_NEW, |
| 325 |
&h->nfnl_cb_ct); |
| 326 |
|
| 327 |
nfnl_callback_register(h->nfnlssh_ct, |
| 328 |
IPCTNL_MSG_CT_DELETE, |
| 329 |
&h->nfnl_cb_ct); |
| 330 |
|
| 331 |
return 0; |
| 332 |
} |
| 333 |
|
| 334 |
/** |
| 335 |
* nfct_callback_unregister2 - unregister a callback |
| 336 |
* \param h library handler |
| 337 |
*/ |
| 338 |
void nfct_callback_unregister2(struct nfct_handle *h) |
| 339 |
{ |
| 340 |
assert(h != NULL); |
| 341 |
|
| 342 |
nfnl_callback_unregister(h->nfnlssh_ct, IPCTNL_MSG_CT_NEW); |
| 343 |
nfnl_callback_unregister(h->nfnlssh_ct, IPCTNL_MSG_CT_DELETE); |
| 344 |
|
| 345 |
h->cb2 = NULL; |
| 346 |
free(h->nfnl_cb_ct.data); |
| 347 |
|
| 348 |
h->nfnl_cb_ct.call = NULL; |
| 349 |
h->nfnl_cb_ct.data = NULL; |
| 350 |
h->nfnl_cb_ct.attr_count = 0; |
| 351 |
} |
| 352 |
|
| 353 |
/** |
| 354 |
* @} |
| 355 |
*/ |
| 356 |
|
| 357 |
/** |
| 358 |
* \defgroup ct Conntrack object handling |
| 359 |
* @{ |
| 360 |
*/ |
| 361 |
|
| 362 |
/** |
| 363 |
* nfct_set_attr_l - set the value of a certain conntrack attribute |
| 364 |
* \param ct pointer to a valid conntrack |
| 365 |
* \param type attribute type |
| 366 |
* \param pointer to attribute value |
| 367 |
* \param length of attribute value (in bytes) |
| 368 |
*/ |
| 369 |
void |
| 370 |
nfct_set_attr_l(struct nf_conntrack *ct, const enum nf_conntrack_attr type, |
| 371 |
const void *value, size_t len) |
| 372 |
{ |
| 373 |
assert(ct != NULL); |
| 374 |
assert(value != NULL); |
| 375 |
|
| 376 |
if (unlikely(type >= ATTR_MAX)) |
| 377 |
return; |
| 378 |
|
| 379 |
if (set_attr_array[type]) { |
| 380 |
set_attr_array[type](ct, value, len); |
| 381 |
set_bit(type, ct->head.set); |
| 382 |
} |
| 383 |
} |
| 384 |
|
| 385 |
/** |
| 386 |
* nfct_set_attr - set the value of a certain conntrack attribute |
| 387 |
* \param ct pointer to a valid conntrack |
| 388 |
* \param type attribute type |
| 389 |
* \param value pointer to the attribute value |
| 390 |
* |
| 391 |
* Note that certain attributes are unsettable: |
| 392 |
* - ATTR_USE |
| 393 |
* - ATTR_ID |
| 394 |
* - ATTR_*_COUNTER_* |
| 395 |
* - ATTR_SECCTX |
| 396 |
* - ATTR_TIMESTAMP_* |
| 397 |
* The call of this function for such attributes do nothing. |
| 398 |
*/ |
| 399 |
void nfct_set_attr(struct nf_conntrack *ct, |
| 400 |
const enum nf_conntrack_attr type, |
| 401 |
const void *value) |
| 402 |
{ |
| 403 |
/* We assume the setter knows the size of the passed pointer. */ |
| 404 |
nfct_set_attr_l(ct, type, value, 0); |
| 405 |
} |
| 406 |
|
| 407 |
/** |
| 408 |
* nfct_set_attr_u8 - set the value of a certain conntrack attribute |
| 409 |
* \param ct pointer to a valid conntrack |
| 410 |
* \param type attribute type |
| 411 |
* \param value unsigned 8 bits attribute value |
| 412 |
*/ |
| 413 |
void nfct_set_attr_u8(struct nf_conntrack *ct, |
| 414 |
const enum nf_conntrack_attr type, |
| 415 |
uint8_t value) |
| 416 |
{ |
| 417 |
nfct_set_attr_l(ct, type, &value, sizeof(uint8_t)); |
| 418 |
} |
| 419 |
|
| 420 |
/** |
| 421 |
* nfct_set_attr_u16 - set the value of a certain conntrack attribute |
| 422 |
* \param ct pointer to a valid conntrack |
| 423 |
* \param type attribute type |
| 424 |
* \param value unsigned 16 bits attribute value |
| 425 |
*/ |
| 426 |
void nfct_set_attr_u16(struct nf_conntrack *ct, |
| 427 |
const enum nf_conntrack_attr type, |
| 428 |
uint16_t value) |
| 429 |
{ |
| 430 |
nfct_set_attr_l(ct, type, &value, sizeof(uint16_t)); |
| 431 |
} |
| 432 |
|
| 433 |
/** |
| 434 |
* nfct_set_attr_u32 - set the value of a certain conntrack attribute |
| 435 |
* \param ct pointer to a valid conntrack |
| 436 |
* \param type attribute type |
| 437 |
* \param value unsigned 32 bits attribute value |
| 438 |
*/ |
| 439 |
void nfct_set_attr_u32(struct nf_conntrack *ct, |
| 440 |
const enum nf_conntrack_attr type, |
| 441 |
uint32_t value) |
| 442 |
{ |
| 443 |
nfct_set_attr_l(ct, type, &value, sizeof(uint32_t)); |
| 444 |
} |
| 445 |
|
| 446 |
/** |
| 447 |
* nfct_set_attr_u64 - set the value of a certain conntrack attribute |
| 448 |
* \param ct pointer to a valid conntrack |
| 449 |
* \param type attribute type |
| 450 |
* \param value unsigned 64 bits attribute value |
| 451 |
*/ |
| 452 |
void nfct_set_attr_u64(struct nf_conntrack *ct, |
| 453 |
const enum nf_conntrack_attr type, |
| 454 |
uint64_t value) |
| 455 |
{ |
| 456 |
nfct_set_attr_l(ct, type, &value, sizeof(uint64_t)); |
| 457 |
} |
| 458 |
|
| 459 |
/** |
| 460 |
* nfct_get_attr - get a conntrack attribute |
| 461 |
* \param ct pointer to a valid conntrack |
| 462 |
* \param type attribute type |
| 463 |
* |
| 464 |
* In case of success a valid pointer to the attribute requested is returned, |
| 465 |
* on error NULL is returned and errno is set appropiately. |
| 466 |
*/ |
| 467 |
const void *nfct_get_attr(const struct nf_conntrack *ct, |
| 468 |
const enum nf_conntrack_attr type) |
| 469 |
{ |
| 470 |
assert(ct != NULL); |
| 471 |
|
| 472 |
if (unlikely(type >= ATTR_MAX)) { |
| 473 |
errno = EINVAL; |
| 474 |
return NULL; |
| 475 |
} |
| 476 |
|
| 477 |
if (!test_bit(type, ct->head.set)) { |
| 478 |
errno = ENODATA; |
| 479 |
return NULL; |
| 480 |
} |
| 481 |
|
| 482 |
assert(get_attr_array[type]); |
| 483 |
|
| 484 |
return get_attr_array[type](ct); |
| 485 |
} |
| 486 |
|
| 487 |
/** |
| 488 |
* nfct_get_attr_u8 - get attribute of unsigned 8-bits long |
| 489 |
* \param ct pointer to a valid conntrack |
| 490 |
* \param type attribute type |
| 491 |
* |
| 492 |
* Returns the value of the requested attribute, if the attribute is not |
| 493 |
* set, 0 is returned. In order to check if the attribute is set or not, |
| 494 |
* use nfct_attr_is_set. |
| 495 |
*/ |
| 496 |
uint8_t nfct_get_attr_u8(const struct nf_conntrack *ct, |
| 497 |
const enum nf_conntrack_attr type) |
| 498 |
{ |
| 499 |
const uint8_t *ret = nfct_get_attr(ct, type); |
| 500 |
return ret == NULL ? 0 : *ret; |
| 501 |
} |
| 502 |
|
| 503 |
/** |
| 504 |
* nfct_get_attr_u16 - get attribute of unsigned 16-bits long |
| 505 |
* \param ct pointer to a valid conntrack |
| 506 |
* \param type attribute type |
| 507 |
* |
| 508 |
* Returns the value of the requested attribute, if the attribute is not |
| 509 |
* set, 0 is returned. In order to check if the attribute is set or not, |
| 510 |
* use nfct_attr_is_set. |
| 511 |
*/ |
| 512 |
uint16_t nfct_get_attr_u16(const struct nf_conntrack *ct, |
| 513 |
const enum nf_conntrack_attr type) |
| 514 |
{ |
| 515 |
const uint16_t *ret = nfct_get_attr(ct, type); |
| 516 |
return ret == NULL ? 0 : *ret; |
| 517 |
} |
| 518 |
|
| 519 |
/** |
| 520 |
* nfct_get_attr_u32 - get attribute of unsigned 32-bits long |
| 521 |
* \param ct pointer to a valid conntrack |
| 522 |
* \param type attribute type |
| 523 |
* |
| 524 |
* Returns the value of the requested attribute, if the attribute is not |
| 525 |
* set, 0 is returned. In order to check if the attribute is set or not, |
| 526 |
* use nfct_attr_is_set. |
| 527 |
*/ |
| 528 |
uint32_t nfct_get_attr_u32(const struct nf_conntrack *ct, |
| 529 |
const enum nf_conntrack_attr type) |
| 530 |
{ |
| 531 |
const uint32_t *ret = nfct_get_attr(ct, type); |
| 532 |
return ret == NULL ? 0 : *ret; |
| 533 |
} |
| 534 |
|
| 535 |
/** |
| 536 |
* nfct_get_attr_u64 - get attribute of unsigned 32-bits long |
| 537 |
* \param ct pointer to a valid conntrack |
| 538 |
* \param type attribute type |
| 539 |
* |
| 540 |
* Returns the value of the requested attribute, if the attribute is not |
| 541 |
* set, 0 is returned. In order to check if the attribute is set or not, |
| 542 |
* use nfct_attr_is_set. |
| 543 |
*/ |
| 544 |
uint64_t nfct_get_attr_u64(const struct nf_conntrack *ct, |
| 545 |
const enum nf_conntrack_attr type) |
| 546 |
{ |
| 547 |
const uint64_t *ret = nfct_get_attr(ct, type); |
| 548 |
return ret == NULL ? 0 : *ret; |
| 549 |
} |
| 550 |
|
| 551 |
/** |
| 552 |
* nfct_attr_is_set - check if a certain attribute is set |
| 553 |
* \param ct pointer to a valid conntrack object |
| 554 |
* \param type attribute type |
| 555 |
* |
| 556 |
* On error, -1 is returned and errno is set appropiately, otherwise |
| 557 |
* the value of the attribute is returned. |
| 558 |
*/ |
| 559 |
int nfct_attr_is_set(const struct nf_conntrack *ct, |
| 560 |
const enum nf_conntrack_attr type) |
| 561 |
{ |
| 562 |
assert(ct != NULL); |
| 563 |
|
| 564 |
if (unlikely(type >= ATTR_MAX)) { |
| 565 |
errno = EINVAL; |
| 566 |
return -1; |
| 567 |
} |
| 568 |
return test_bit(type, ct->head.set); |
| 569 |
} |
| 570 |
|
| 571 |
/** |
| 572 |
* nfct_attr_is_set_array - check if an array of attribute types is set |
| 573 |
* \param ct pointer to a valid conntrack object |
| 574 |
* \param array attribute type array |
| 575 |
* \param size size of the array |
| 576 |
* |
| 577 |
* On error, -1 is returned and errno is set appropiately, otherwise |
| 578 |
* the value of the attribute is returned. |
| 579 |
*/ |
| 580 |
int nfct_attr_is_set_array(const struct nf_conntrack *ct, |
| 581 |
const enum nf_conntrack_attr *type_array, |
| 582 |
int size) |
| 583 |
{ |
| 584 |
int i; |
| 585 |
|
| 586 |
assert(ct != NULL); |
| 587 |
|
| 588 |
for (i=0; i<size; i++) { |
| 589 |
if (unlikely(type_array[i] >= ATTR_MAX)) { |
| 590 |
errno = EINVAL; |
| 591 |
return -1; |
| 592 |
} |
| 593 |
if (!test_bit(type_array[i], ct->head.set)) |
| 594 |
return 0; |
| 595 |
} |
| 596 |
return 1; |
| 597 |
} |
| 598 |
|
| 599 |
/** |
| 600 |
* nfct_attr_unset - unset a certain attribute |
| 601 |
* \param type attribute type |
| 602 |
* \param ct pointer to a valid conntrack object |
| 603 |
* |
| 604 |
* On error, -1 is returned and errno is set appropiately, otherwise |
| 605 |
* 0 is returned. |
| 606 |
*/ |
| 607 |
int nfct_attr_unset(struct nf_conntrack *ct, |
| 608 |
const enum nf_conntrack_attr type) |
| 609 |
{ |
| 610 |
assert(ct != NULL); |
| 611 |
|
| 612 |
if (unlikely(type >= ATTR_MAX)) { |
| 613 |
errno = EINVAL; |
| 614 |
return -1; |
| 615 |
} |
| 616 |
unset_bit(type, ct->head.set); |
| 617 |
|
| 618 |
return 0; |
| 619 |
} |
| 620 |
|
| 621 |
/** |
| 622 |
* nfct_set_attr_grp - set a group of attributes |
| 623 |
* \param ct pointer to a valid conntrack object |
| 624 |
* \param type attribute group (see ATTR_GRP_*) |
| 625 |
* \param data pointer to struct (see struct nfct_attr_grp_*) |
| 626 |
* |
| 627 |
* Note that calling this function for ATTR_GRP_COUNTER_* and ATTR_GRP_ADDR_* |
| 628 |
* have no effect. |
| 629 |
*/ |
| 630 |
void nfct_set_attr_grp(struct nf_conntrack *ct, |
| 631 |
const enum nf_conntrack_attr_grp type, |
| 632 |
const void *data) |
| 633 |
{ |
| 634 |
assert(ct != NULL); |
| 635 |
|
| 636 |
if (unlikely(type >= ATTR_GRP_MAX)) |
| 637 |
return; |
| 638 |
|
| 639 |
if (set_attr_grp_array[type]) { |
| 640 |
set_attr_grp_array[type](ct, data); |
| 641 |
set_bitmask_u32(ct->head.set, |
| 642 |
attr_grp_bitmask[type].bitmask, __NFCT_BITSET); |
| 643 |
} |
| 644 |
} |
| 645 |
|
| 646 |
/** |
| 647 |
* nfct_get_attr_grp - get an attribute group |
| 648 |
* \param ct pointer to a valid conntrack object |
| 649 |
* \param type attribute group (see ATTR_GRP_*) |
| 650 |
* \param data pointer to struct (see struct nfct_attr_grp_*) |
| 651 |
* |
| 652 |
* On error, it returns -1 and errno is appropriately set. On success, the |
| 653 |
* data pointer contains the attribute group. |
| 654 |
*/ |
| 655 |
int nfct_get_attr_grp(const struct nf_conntrack *ct, |
| 656 |
const enum nf_conntrack_attr_grp type, |
| 657 |
void *data) |
| 658 |
{ |
| 659 |
assert(ct != NULL); |
| 660 |
|
| 661 |
if (unlikely(type >= ATTR_GRP_MAX)) { |
| 662 |
errno = EINVAL; |
| 663 |
return -1; |
| 664 |
} |
| 665 |
switch(attr_grp_bitmask[type].type) { |
| 666 |
case NFCT_BITMASK_AND: |
| 667 |
if (!test_bitmask_u32(ct->head.set, |
| 668 |
attr_grp_bitmask[type].bitmask, |
| 669 |
__NFCT_BITSET)) { |
| 670 |
errno = ENODATA; |
| 671 |
return -1; |
| 672 |
} |
| 673 |
break; |
| 674 |
case NFCT_BITMASK_OR: |
| 675 |
if (!test_bitmask_u32_or(ct->head.set, |
| 676 |
attr_grp_bitmask[type].bitmask, |
| 677 |
__NFCT_BITSET)) { |
| 678 |
errno = ENODATA; |
| 679 |
return -1; |
| 680 |
} |
| 681 |
break; |
| 682 |
} |
| 683 |
assert(get_attr_grp_array[type]); |
| 684 |
get_attr_grp_array[type](ct, data); |
| 685 |
return 0; |
| 686 |
} |
| 687 |
|
| 688 |
/** |
| 689 |
* nfct_attr_grp_is_set - check if an attribute group is set |
| 690 |
* \param ct pointer to a valid conntrack object |
| 691 |
* \param type attribute group (see ATTR_GRP_*) |
| 692 |
* |
| 693 |
* If the attribute group is set, this function returns 1, otherwise 0. |
| 694 |
*/ |
| 695 |
int nfct_attr_grp_is_set(const struct nf_conntrack *ct, |
| 696 |
const enum nf_conntrack_attr_grp type) |
| 697 |
{ |
| 698 |
assert(ct != NULL); |
| 699 |
|
| 700 |
if (unlikely(type >= ATTR_GRP_MAX)) { |
| 701 |
errno = EINVAL; |
| 702 |
return -1; |
| 703 |
} |
| 704 |
switch(attr_grp_bitmask[type].type) { |
| 705 |
case NFCT_BITMASK_AND: |
| 706 |
if (test_bitmask_u32(ct->head.set, |
| 707 |
attr_grp_bitmask[type].bitmask, |
| 708 |
__NFCT_BITSET)) { |
| 709 |
return 1; |
| 710 |
} |
| 711 |
break; |
| 712 |
case NFCT_BITMASK_OR: |
| 713 |
if (test_bitmask_u32_or(ct->head.set, |
| 714 |
attr_grp_bitmask[type].bitmask, |
| 715 |
__NFCT_BITSET)) { |
| 716 |
return 1; |
| 717 |
} |
| 718 |
break; |
| 719 |
} |
| 720 |
return 0; |
| 721 |
} |
| 722 |
|
| 723 |
/** |
| 724 |
* nfct_attr_grp_unset - unset an attribute group |
| 725 |
* \param ct pointer to a valid conntrack object |
| 726 |
* \param type attribute group (see ATTR_GRP_*) |
| 727 |
* |
| 728 |
* On error, it returns -1 and errno is appropriately set. On success, |
| 729 |
* this function returns 0. |
| 730 |
*/ |
| 731 |
int nfct_attr_grp_unset(struct nf_conntrack *ct, |
| 732 |
const enum nf_conntrack_attr_grp type) |
| 733 |
{ |
| 734 |
assert(ct != NULL); |
| 735 |
|
| 736 |
if (unlikely(type >= ATTR_GRP_MAX)) { |
| 737 |
errno = EINVAL; |
| 738 |
return -1; |
| 739 |
} |
| 740 |
unset_bitmask_u32(ct->head.set, attr_grp_bitmask[type].bitmask, |
| 741 |
__NFCT_BITSET); |
| 742 |
|
| 743 |
return 0; |
| 744 |
} |
| 745 |
|
| 746 |
/** |
| 747 |
* @} |
| 748 |
*/ |
| 749 |
|
| 750 |
/** |
| 751 |
* \defgroup nl Low level object to Netlink message |
| 752 |
* @{ |
| 753 |
*/ |
| 754 |
|
| 755 |
/** |
| 756 |
* nfct_build_conntrack - build a netlink message from a conntrack object |
| 757 |
* \param ssh nfnetlink subsystem handler |
| 758 |
* \param req buffer used to build the netlink message |
| 759 |
* \param size size of the buffer passed |
| 760 |
* \param type netlink message type |
| 761 |
* \param flags netlink flags |
| 762 |
* \param ct pointer to a conntrack object |
| 763 |
* |
| 764 |
* This is a low level function for those that require to be close to |
| 765 |
* netlink details via libnfnetlink. If you do want to obviate the netlink |
| 766 |
* details then we suggest you to use nfct_query. |
| 767 |
* |
| 768 |
* On error, -1 is returned and errno is appropiately set. |
| 769 |
* On success, 0 is returned. |
| 770 |
*/ |
| 771 |
int nfct_build_conntrack(struct nfnl_subsys_handle *ssh, |
| 772 |
void *req, |
| 773 |
size_t size, |
| 774 |
uint16_t type, |
| 775 |
uint16_t flags, |
| 776 |
const struct nf_conntrack *ct) |
| 777 |
{ |
| 778 |
assert(ssh != NULL); |
| 779 |
assert(req != NULL); |
| 780 |
assert(ct != NULL); |
| 781 |
|
| 782 |
memset(req, 0, size); |
| 783 |
|
| 784 |
return __build_conntrack(req, size, type, flags, ct); |
| 785 |
} |
| 786 |
|
| 787 |
static void nfct_fill_hdr(struct nfnlhdr *req, uint16_t type, uint16_t flags, |
| 788 |
uint8_t l3num, uint8_t version) |
| 789 |
{ |
| 790 |
char *buf = (char *)&req->nlh; |
| 791 |
struct nlmsghdr *nlh; |
| 792 |
struct nfgenmsg *nfh; |
| 793 |
|
| 794 |
nlh = mnl_nlmsg_put_header(buf); |
| 795 |
nlh->nlmsg_type = (NFNL_SUBSYS_CTNETLINK << 8) | type; |
| 796 |
nlh->nlmsg_flags = NLM_F_REQUEST | flags; |
| 797 |
nlh->nlmsg_seq = 0; |
| 798 |
|
| 799 |
nfh = mnl_nlmsg_put_extra_header(nlh, sizeof(struct nfgenmsg)); |
| 800 |
nfh->nfgen_family = l3num; |
| 801 |
nfh->version = version; |
| 802 |
nfh->res_id = 0; |
| 803 |
} |
| 804 |
|
| 805 |
static int |
| 806 |
__build_query_ct(const enum nf_conntrack_query qt, |
| 807 |
const void *data, void *buffer, unsigned int size) |
| 808 |
{ |
| 809 |
struct nfnlhdr *req = buffer; |
| 810 |
const uint32_t *family = data; |
| 811 |
|
| 812 |
assert(data != NULL); |
| 813 |
assert(req != NULL); |
| 814 |
|
| 815 |
memset(buffer, 0, size); |
| 816 |
|
| 817 |
switch(qt) { |
| 818 |
case NFCT_Q_CREATE: |
| 819 |
__build_conntrack(req, size, IPCTNL_MSG_CT_NEW, NLM_F_REQUEST|NLM_F_CREATE|NLM_F_ACK|NLM_F_EXCL, data); |
| 820 |
break; |
| 821 |
case NFCT_Q_UPDATE: |
| 822 |
__build_conntrack(req, size, IPCTNL_MSG_CT_NEW, NLM_F_REQUEST|NLM_F_ACK, data); |
| 823 |
break; |
| 824 |
case NFCT_Q_DESTROY: |
| 825 |
__build_conntrack(req, size, IPCTNL_MSG_CT_DELETE, NLM_F_REQUEST|NLM_F_ACK, data); |
| 826 |
break; |
| 827 |
case NFCT_Q_GET: |
| 828 |
__build_conntrack(req, size, IPCTNL_MSG_CT_GET, NLM_F_REQUEST|NLM_F_ACK, data); |
| 829 |
break; |
| 830 |
case NFCT_Q_FLUSH: |
| 831 |
nfct_fill_hdr(req, IPCTNL_MSG_CT_DELETE, NLM_F_ACK, *family, |
| 832 |
NFNETLINK_V0); |
| 833 |
break; |
| 834 |
case NFCT_Q_FLUSH_FILTER: |
| 835 |
nfct_fill_hdr(req, IPCTNL_MSG_CT_DELETE, NLM_F_ACK, *family, 1); |
| 836 |
if (__build_filter_flush(req, size, data) < 0) |
| 837 |
return -1; |
| 838 |
break; |
| 839 |
case NFCT_Q_DUMP: |
| 840 |
nfct_fill_hdr(req, IPCTNL_MSG_CT_GET, NLM_F_DUMP, *family, |
| 841 |
NFNETLINK_V0); |
| 842 |
break; |
| 843 |
case NFCT_Q_DUMP_RESET: |
| 844 |
nfct_fill_hdr(req, IPCTNL_MSG_CT_GET_CTRZERO, NLM_F_DUMP, |
| 845 |
*family, NFNETLINK_V0); |
| 846 |
break; |
| 847 |
case NFCT_Q_CREATE_UPDATE: |
| 848 |
__build_conntrack(req, size, IPCTNL_MSG_CT_NEW, NLM_F_REQUEST|NLM_F_CREATE|NLM_F_ACK, data); |
| 849 |
break; |
| 850 |
case NFCT_Q_DUMP_FILTER: |
| 851 |
nfct_fill_hdr(req, IPCTNL_MSG_CT_GET, NLM_F_DUMP, AF_UNSPEC, |
| 852 |
NFNETLINK_V0); |
| 853 |
if (__build_filter_dump(req, size, data) < 0) |
| 854 |
return -1; |
| 855 |
break; |
| 856 |
case NFCT_Q_DUMP_FILTER_RESET: |
| 857 |
nfct_fill_hdr(req, IPCTNL_MSG_CT_GET_CTRZERO, NLM_F_DUMP, |
| 858 |
AF_UNSPEC, NFNETLINK_V0); |
| 859 |
if (__build_filter_dump(req, size, data) < 0) |
| 860 |
return -1; |
| 861 |
break; |
| 862 |
default: |
| 863 |
errno = ENOTSUP; |
| 864 |
return -1; |
| 865 |
} |
| 866 |
return 1; |
| 867 |
} |
| 868 |
|
| 869 |
/** |
| 870 |
* nfct_build_query - build a query in netlink message format for ctnetlink |
| 871 |
* \param ssh nfnetlink subsystem handler |
| 872 |
* \param qt query type |
| 873 |
* \param data data required to build the query |
| 874 |
* \param req buffer to build the netlink message |
| 875 |
* \param size size of the buffer passed |
| 876 |
* |
| 877 |
* This is a low level function, use it if you want to require to work |
| 878 |
* with netlink details via libnfnetlink, otherwise we suggest you to |
| 879 |
* use nfct_query. |
| 880 |
* |
| 881 |
* The pointer to data can be a conntrack object or the protocol family |
| 882 |
* depending on the request. |
| 883 |
* |
| 884 |
* For query types: |
| 885 |
* - NFCT_Q_CREATE: add a new conntrack, if it exists, fail |
| 886 |
* - NFCT_O_CREATE_UPDATE: add a new conntrack, if it exists, update it |
| 887 |
* - NFCT_Q_UPDATE: update a conntrack |
| 888 |
* - NFCT_Q_DESTROY: destroy a conntrack |
| 889 |
* - NFCT_Q_GET: get a conntrack |
| 890 |
* |
| 891 |
* Pass a valid pointer to a conntrack object. |
| 892 |
* |
| 893 |
* For query types: |
| 894 |
* - NFCT_Q_FLUSH: flush the conntrack table |
| 895 |
* - NFCT_Q_DUMP: dump the conntrack table |
| 896 |
* - NFCT_Q_DUMP_RESET: dump the conntrack table and reset counters |
| 897 |
* - NFCT_Q_DUMP_FILTER: dump the conntrack table |
| 898 |
* - NFCT_Q_DUMP_FILTER_RESET: dump the conntrack table and reset counters |
| 899 |
* |
| 900 |
* Pass a valid pointer to the protocol family (uint32_t) |
| 901 |
* |
| 902 |
* On success, 0 is returned. On error, -1 is returned and errno is set |
| 903 |
* appropiately. |
| 904 |
*/ |
| 905 |
int nfct_build_query(struct nfnl_subsys_handle *ssh, |
| 906 |
const enum nf_conntrack_query qt, |
| 907 |
const void *data, |
| 908 |
void *buffer, |
| 909 |
unsigned int size) |
| 910 |
{ |
| 911 |
return __build_query_ct(qt, data, buffer, size); |
| 912 |
} |
| 913 |
|
| 914 |
static int __parse_message_type(const struct nlmsghdr *nlh) |
| 915 |
{ |
| 916 |
uint16_t type = NFNL_MSG_TYPE(nlh->nlmsg_type); |
| 917 |
uint16_t flags = nlh->nlmsg_flags; |
| 918 |
int ret = NFCT_T_UNKNOWN; |
| 919 |
|
| 920 |
if (type == IPCTNL_MSG_CT_NEW) { |
| 921 |
if (flags & (NLM_F_CREATE|NLM_F_EXCL)) |
| 922 |
ret = NFCT_T_NEW; |
| 923 |
else |
| 924 |
ret = NFCT_T_UPDATE; |
| 925 |
} else if (type == IPCTNL_MSG_CT_DELETE) |
| 926 |
ret = NFCT_T_DESTROY; |
| 927 |
|
| 928 |
return ret; |
| 929 |
} |
| 930 |
|
| 931 |
/** |
| 932 |
* nfct_parse_conntrack - translate a netlink message to a conntrack object |
| 933 |
* \param type do the translation iif the message type is of a certain type |
| 934 |
* \param nlh pointer to the netlink message |
| 935 |
* \param ct pointer to the conntrack object |
| 936 |
* |
| 937 |
* This is a low level function, use it in case that you require to work |
| 938 |
* with netlink details via libnfnetlink. Otherwise, we suggest you to |
| 939 |
* use the high level API. |
| 940 |
* |
| 941 |
* The message types are: |
| 942 |
* |
| 943 |
* - NFCT_T_NEW: parse messages with new conntracks |
| 944 |
* - NFCT_T_UPDATE: parse messages with conntrack updates |
| 945 |
* - NFCT_T_DESTROY: parse messages with conntrack destroy |
| 946 |
* - NFCT_T_ALL: all message types |
| 947 |
* |
| 948 |
* The message type is a flag, therefore the can be combined, ie. |
| 949 |
* NFCT_T_NEW | NFCT_T_DESTROY to parse only new and destroy messages |
| 950 |
* |
| 951 |
* On error, NFCT_T_ERROR is returned and errno is set appropiately. If |
| 952 |
* the message received is not of the requested type then 0 is returned, |
| 953 |
* otherwise this function returns the message type parsed. |
| 954 |
*/ |
| 955 |
int nfct_parse_conntrack(enum nf_conntrack_msg_type type, |
| 956 |
const struct nlmsghdr *nlh, |
| 957 |
struct nf_conntrack *ct) |
| 958 |
{ |
| 959 |
unsigned int flags; |
| 960 |
|
| 961 |
assert(nlh != NULL); |
| 962 |
assert(ct != NULL); |
| 963 |
|
| 964 |
flags = __parse_message_type(nlh); |
| 965 |
if (!(flags & type)) |
| 966 |
return 0; |
| 967 |
|
| 968 |
nfct_nlmsg_parse(nlh, ct); |
| 969 |
|
| 970 |
return flags; |
| 971 |
} |
| 972 |
|
| 973 |
/** |
| 974 |
* @} |
| 975 |
*/ |
| 976 |
|
| 977 |
/** |
| 978 |
* \defgroup cmd Send commands to kernel-space and receive replies |
| 979 |
* @{ |
| 980 |
*/ |
| 981 |
|
| 982 |
/** |
| 983 |
* nfct_query - send a query to ctnetlink and handle the reply |
| 984 |
* \param h library handler |
| 985 |
* \param qt query type |
| 986 |
* \param data data required to send the query |
| 987 |
* |
| 988 |
* On error, -1 is returned and errno is explicitely set. On success, 0 |
| 989 |
* is returned. |
| 990 |
*/ |
| 991 |
int nfct_query(struct nfct_handle *h, |
| 992 |
const enum nf_conntrack_query qt, |
| 993 |
const void *data) |
| 994 |
{ |
| 995 |
const size_t size = 4096; /* enough for now */ |
| 996 |
union { |
| 997 |
char buffer[size]; |
| 998 |
struct nfnlhdr req; |
| 999 |
} u; |
| 1000 |
|
| 1001 |
assert(h != NULL); |
| 1002 |
assert(data != NULL); |
| 1003 |
|
| 1004 |
if (__build_query_ct(qt, data, &u.req, size) == -1) |
| 1005 |
return -1; |
| 1006 |
|
| 1007 |
return nfnl_query(h->nfnlh, &u.req.nlh); |
| 1008 |
} |
| 1009 |
|
| 1010 |
/** |
| 1011 |
* nfct_send - send a query to ctnetlink |
| 1012 |
* \param h library handler |
| 1013 |
* \param qt query type |
| 1014 |
* \param data data required to send the query |
| 1015 |
* |
| 1016 |
* Like nfct_query but we do not wait for the reply from ctnetlink. |
| 1017 |
* You can use nfct_send() and nfct_catch() to emulate nfct_query(). |
| 1018 |
* This is particularly useful when the socket is non-blocking. |
| 1019 |
* |
| 1020 |
* On error, -1 is returned and errno is explicitely set. On success, 0 |
| 1021 |
* is returned. |
| 1022 |
*/ |
| 1023 |
int nfct_send(struct nfct_handle *h, |
| 1024 |
const enum nf_conntrack_query qt, |
| 1025 |
const void *data) |
| 1026 |
{ |
| 1027 |
const size_t size = 4096; /* enough for now */ |
| 1028 |
union { |
| 1029 |
char buffer[size]; |
| 1030 |
struct nfnlhdr req; |
| 1031 |
} u; |
| 1032 |
|
| 1033 |
assert(h != NULL); |
| 1034 |
assert(data != NULL); |
| 1035 |
|
| 1036 |
if (__build_query_ct(qt, data, &u.req, size) == -1) |
| 1037 |
return -1; |
| 1038 |
|
| 1039 |
return nfnl_send(h->nfnlh, &u.req.nlh); |
| 1040 |
} |
| 1041 |
|
| 1042 |
|
| 1043 |
/** |
| 1044 |
* nfct_catch - catch events |
| 1045 |
* \param h library handler |
| 1046 |
* |
| 1047 |
* This function receives the event from the kernel and it invokes the |
| 1048 |
* callback that was registered to this handle. |
| 1049 |
* |
| 1050 |
* On error, -1 is returned and errno is set appropiately. On success, |
| 1051 |
* a value greater or equal to 0 is returned indicating the callback |
| 1052 |
* verdict: NFCT_CB_STOP, NFCT_CB_CONTINUE or NFCT_CB_STOLEN. |
| 1053 |
* |
| 1054 |
* Beware that this function also handles expectation events, in case they are |
| 1055 |
* received through this handle. |
| 1056 |
*/ |
| 1057 |
int nfct_catch(struct nfct_handle *h) |
| 1058 |
{ |
| 1059 |
assert(h != NULL); |
| 1060 |
|
| 1061 |
return nfnl_catch(h->nfnlh); |
| 1062 |
} |
| 1063 |
|
| 1064 |
/** |
| 1065 |
* @} |
| 1066 |
*/ |
| 1067 |
|
| 1068 |
/** |
| 1069 |
* \defgroup ct Conntrack object handling |
| 1070 |
* @{ |
| 1071 |
*/ |
| 1072 |
|
| 1073 |
/** |
| 1074 |
* nfct_snprintf - print a conntrack object to a buffer |
| 1075 |
* \param buf buffer used to build the printable conntrack |
| 1076 |
* \param size size of the buffer |
| 1077 |
* \param ct pointer to a valid conntrack object |
| 1078 |
* \param message_type print message type (NFCT_T_UNKNOWN, NFCT_T_NEW,...) |
| 1079 |
* \param output_type print type (NFCT_O_DEFAULT, NFCT_O_XML, ...) |
| 1080 |
* \param flags extra flags for the output type (NFCT_OF_LAYER3) |
| 1081 |
* |
| 1082 |
* If you are listening to events, probably you want to display the message |
| 1083 |
* type as well. In that case, set the message type parameter to any of the |
| 1084 |
* known existing types, ie. NFCT_T_NEW, NFCT_T_UPDATE, NFCT_T_DESTROY. |
| 1085 |
* If you pass NFCT_T_UNKNOWN, the message type will not be output. |
| 1086 |
* |
| 1087 |
* Currently, the output available are: |
| 1088 |
* - NFCT_O_DEFAULT: default /proc-like output |
| 1089 |
* - NFCT_O_XML: XML output |
| 1090 |
* |
| 1091 |
* The output flags are: |
| 1092 |
* - NFCT_OF_SHOW_LAYER3: include layer 3 information in the output, |
| 1093 |
* this is *only* required by NFCT_O_DEFAULT. |
| 1094 |
* - NFCT_OF_TIME: display current time. |
| 1095 |
* - NFCT_OF_ID: display the ID number. |
| 1096 |
* - NFCT_OF_TIMESTAMP: display creation and (if exists) deletion time. |
| 1097 |
* |
| 1098 |
* To use NFCT_OF_TIMESTAMP, you have to: |
| 1099 |
* \verbatim |
| 1100 |
* $ echo 1 > /proc/sys/net/netfilter/nf_conntrack_timestamp |
| 1101 |
\endverbatim |
| 1102 |
* This requires a Linux kernel >= 2.6.38. |
| 1103 |
* |
| 1104 |
* Note that NFCT_OF_TIME displays the current time when nfct_snprintf() has |
| 1105 |
* been called. Thus, it can be used to know when a flow was destroy if you |
| 1106 |
* print the message just after you receive the destroy event. If you want |
| 1107 |
* more accurate timestamping, use NFCT_OF_TIMESTAMP. |
| 1108 |
* |
| 1109 |
* On error, -1 is returned and errno is set appropiately. Otherwise the |
| 1110 |
* size of what _would_ be written is returned, even if the size of the |
| 1111 |
* buffer is insufficient. This behaviour is similar to snprintf. |
| 1112 |
*/ |
| 1113 |
int nfct_snprintf(char *buf, |
| 1114 |
unsigned int size, |
| 1115 |
const struct nf_conntrack *ct, |
| 1116 |
unsigned int msg_type, |
| 1117 |
unsigned int out_type, |
| 1118 |
unsigned int flags) |
| 1119 |
{ |
| 1120 |
assert(buf != NULL); |
| 1121 |
assert(size > 0); |
| 1122 |
assert(ct != NULL); |
| 1123 |
|
| 1124 |
return __snprintf_conntrack(buf, size, ct, msg_type, out_type, flags, NULL); |
| 1125 |
} |
| 1126 |
|
| 1127 |
/** |
| 1128 |
* nfct_snprintf_labels - print a bitmask object to a buffer including labels |
| 1129 |
* \param buf buffer used to build the printable conntrack |
| 1130 |
* \param size size of the buffer |
| 1131 |
* \param ct pointer to a valid conntrack object |
| 1132 |
* \param message_type print message type (NFCT_T_UNKNOWN, NFCT_T_NEW,...) |
| 1133 |
* \param output_type print type (NFCT_O_DEFAULT, NFCT_O_XML, ...) |
| 1134 |
* \param flags extra flags for the output type (NFCT_OF_LAYER3) |
| 1135 |
* \param map nfct_labelmap describing the connlabel translation, or NULL. |
| 1136 |
* |
| 1137 |
* When map is NULL, the function is equal to nfct_snprintf(). |
| 1138 |
* Otherwise, if the conntrack object has a connlabel attribute, the active |
| 1139 |
* labels are translated using the label map and added to the buffer. |
| 1140 |
*/ |
| 1141 |
int nfct_snprintf_labels(char *buf, |
| 1142 |
unsigned int size, |
| 1143 |
const struct nf_conntrack *ct, |
| 1144 |
unsigned int msg_type, |
| 1145 |
unsigned int out_type, |
| 1146 |
unsigned int flags, |
| 1147 |
struct nfct_labelmap *map) |
| 1148 |
{ |
| 1149 |
return __snprintf_conntrack(buf, size, ct, msg_type, out_type, flags, map); |
| 1150 |
} |
| 1151 |
|
| 1152 |
/** |
| 1153 |
* nfct_compare - compare two conntrack objects |
| 1154 |
* \param ct1 pointer to a valid conntrack object |
| 1155 |
* \param ct2 pointer to a valid conntrack object |
| 1156 |
* |
| 1157 |
* This function only compare attribute set in both objects, ie. if a certain |
| 1158 |
* attribute is not set in ct1 but it is in ct2, then the value of such |
| 1159 |
* attribute is not used in the comparison. |
| 1160 |
* |
| 1161 |
* If both conntrack object are equal, this function returns 1, otherwise |
| 1162 |
* 0 is returned. |
| 1163 |
* |
| 1164 |
* NOTICE: The use nfct_cmp is preferred. |
| 1165 |
*/ |
| 1166 |
int nfct_compare(const struct nf_conntrack *ct1, |
| 1167 |
const struct nf_conntrack *ct2) |
| 1168 |
{ |
| 1169 |
assert(ct1 != NULL); |
| 1170 |
assert(ct2 != NULL); |
| 1171 |
|
| 1172 |
return __compare(ct1, ct2, NFCT_CMP_ALL); |
| 1173 |
} |
| 1174 |
|
| 1175 |
/** |
| 1176 |
* nfct_cmp - compare two conntrack objects |
| 1177 |
* \param ct1 pointer to a valid conntrack object |
| 1178 |
* \param ct2 pointer to a valid conntrack object |
| 1179 |
* \param flags flags |
| 1180 |
* |
| 1181 |
* This function only compare attribute set in both objects, by default |
| 1182 |
* the comparison is not strict, ie. if a certain attribute is not set in one |
| 1183 |
* of the objects, then such attribute is not used in the comparison. |
| 1184 |
* If you want more strict comparisons, you can use the appropriate flags |
| 1185 |
* to modify this behaviour (see NFCT_CMP_STRICT and NFCT_CMP_MASK). |
| 1186 |
* |
| 1187 |
* The available flags are: |
| 1188 |
* |
| 1189 |
* - NFCT_CMP_STRICT: the compared objects must have the same attributes |
| 1190 |
* and the same values, otherwise it returns that the objects are |
| 1191 |
* different. |
| 1192 |
* - NFCT_CMP_MASK: the first object is used as mask, this means that |
| 1193 |
* if an attribute is present in ct1 but not in ct2, this function |
| 1194 |
* returns that the objects are different. |
| 1195 |
* - NFCT_CMP_ALL: full comparison of both objects |
| 1196 |
* - NFCT_CMP_ORIG: it only compares the source and destination address; |
| 1197 |
* source and destination ports; the layer 3 and 4 protocol numbers |
| 1198 |
* of the original direction; and the id (if present). |
| 1199 |
* - NFCT_CMP_REPL: like NFCT_CMP_REPL but it compares the flow |
| 1200 |
* information that goes in the reply direction. |
| 1201 |
* - NFCT_CMP_TIMEOUT_EQ: timeout(ct1) == timeout(ct2) |
| 1202 |
* - NFCT_CMP_TIMEOUT_GT: timeout(ct1) > timeout(ct2) |
| 1203 |
* - NFCT_CMP_TIMEOUT_LT: timeout(ct1) < timeout(ct2) |
| 1204 |
* - NFCT_CMP_TIMEOUT_GE: timeout(ct1) >= timeout(ct2) |
| 1205 |
* - NFCT_CMP_TIMEOUT_LE: timeout(ct1) <= timeout(ct2) |
| 1206 |
* |
| 1207 |
* The status bits comparison is status(ct1) & status(ct2) == status(ct1). |
| 1208 |
* |
| 1209 |
* If both conntrack object are equal, this function returns 1, otherwise |
| 1210 |
* 0 is returned. |
| 1211 |
*/ |
| 1212 |
int nfct_cmp(const struct nf_conntrack *ct1, |
| 1213 |
const struct nf_conntrack *ct2, |
| 1214 |
unsigned int flags) |
| 1215 |
{ |
| 1216 |
assert(ct1 != NULL); |
| 1217 |
assert(ct2 != NULL); |
| 1218 |
|
| 1219 |
return __compare(ct1, ct2, flags); |
| 1220 |
} |
| 1221 |
|
| 1222 |
/** |
| 1223 |
* nfct_copy - copy part of one source object to another |
| 1224 |
* \param ct1 destination object |
| 1225 |
* \param ct2 source object |
| 1226 |
* \param flags flags |
| 1227 |
* |
| 1228 |
* This function copies one part of the source object to the target. |
| 1229 |
* It behaves like clone but: |
| 1230 |
* |
| 1231 |
* 1) You have to pass an already allocated space for the target object |
| 1232 |
* 2) You can copy only a part of the source object to the target |
| 1233 |
* |
| 1234 |
* The current supported flags are: |
| 1235 |
* - NFCT_CP_ALL: that copies the object entirely. |
| 1236 |
* - NFCT_CP_ORIG and NFCT_CP_REPL: that can be used to copy the |
| 1237 |
* information that identifies a flow in the original and the reply |
| 1238 |
* direction. This information is usually composed of: source and |
| 1239 |
* destination IP address; source and destination ports; layer 3 |
| 1240 |
* and 4 protocol number. |
| 1241 |
* - NFCT_CP_META: that copies the metainformation |
| 1242 |
* (all the attributes >= ATTR_TCP_STATE) |
| 1243 |
* - NFCT_CP_OVERRIDE: changes the default behaviour of nfct_copy() since |
| 1244 |
* it overrides the destination object. After the copy, the destination |
| 1245 |
* is a clone of the origin. This flag provides faster copying. |
| 1246 |
*/ |
| 1247 |
void nfct_copy(struct nf_conntrack *ct1, |
| 1248 |
const struct nf_conntrack *ct2, |
| 1249 |
unsigned int flags) |
| 1250 |
{ |
| 1251 |
int i; |
| 1252 |
|
| 1253 |
assert(ct1 != NULL); |
| 1254 |
assert(ct2 != NULL); |
| 1255 |
|
| 1256 |
if (flags & NFCT_CP_OVERRIDE) { |
| 1257 |
__copy_fast(ct1, ct2); |
| 1258 |
return; |
| 1259 |
} |
| 1260 |
if (flags == NFCT_CP_ALL) { |
| 1261 |
for (i=0; i<ATTR_MAX; i++) { |
| 1262 |
if (test_bit(i, ct2->head.set)) { |
| 1263 |
assert(copy_attr_array[i]); |
| 1264 |
copy_attr_array[i](ct1, ct2); |
| 1265 |
set_bit(i, ct1->head.set); |
| 1266 |
} |
| 1267 |
} |
| 1268 |
return; |
| 1269 |
} |
| 1270 |
|
| 1271 |
static const int cp_orig_mask[] = { |
| 1272 |
ATTR_ORIG_IPV4_SRC, |
| 1273 |
ATTR_ORIG_IPV4_DST, |
| 1274 |
ATTR_ORIG_IPV6_SRC, |
| 1275 |
ATTR_ORIG_IPV6_DST, |
| 1276 |
ATTR_ORIG_PORT_SRC, |
| 1277 |
ATTR_ORIG_PORT_DST, |
| 1278 |
ATTR_ICMP_TYPE, |
| 1279 |
ATTR_ICMP_CODE, |
| 1280 |
ATTR_ICMP_ID, |
| 1281 |
ATTR_ORIG_L3PROTO, |
| 1282 |
ATTR_ORIG_L4PROTO, |
| 1283 |
}; |
| 1284 |
#define __CP_ORIG_MAX sizeof(cp_orig_mask)/sizeof(int) |
| 1285 |
|
| 1286 |
if (flags & NFCT_CP_ORIG) { |
| 1287 |
for (i=0; i<__CP_ORIG_MAX; i++) { |
| 1288 |
if (test_bit(cp_orig_mask[i], ct2->head.set)) { |
| 1289 |
assert(copy_attr_array[i]); |
| 1290 |
copy_attr_array[cp_orig_mask[i]](ct1, ct2); |
| 1291 |
set_bit(cp_orig_mask[i], ct1->head.set); |
| 1292 |
} |
| 1293 |
} |
| 1294 |
} |
| 1295 |
|
| 1296 |
static const int cp_repl_mask[] = { |
| 1297 |
ATTR_REPL_IPV4_SRC, |
| 1298 |
ATTR_REPL_IPV4_DST, |
| 1299 |
ATTR_REPL_IPV6_SRC, |
| 1300 |
ATTR_REPL_IPV6_DST, |
| 1301 |
ATTR_REPL_PORT_SRC, |
| 1302 |
ATTR_REPL_PORT_DST, |
| 1303 |
ATTR_REPL_L3PROTO, |
| 1304 |
ATTR_REPL_L4PROTO, |
| 1305 |
}; |
| 1306 |
#define __CP_REPL_MAX sizeof(cp_repl_mask)/sizeof(int) |
| 1307 |
|
| 1308 |
if (flags & NFCT_CP_REPL) { |
| 1309 |
for (i=0; i<__CP_REPL_MAX; i++) { |
| 1310 |
if (test_bit(cp_repl_mask[i], ct2->head.set)) { |
| 1311 |
assert(copy_attr_array[i]); |
| 1312 |
copy_attr_array[cp_repl_mask[i]](ct1, ct2); |
| 1313 |
set_bit(cp_repl_mask[i], ct1->head.set); |
| 1314 |
} |
| 1315 |
} |
| 1316 |
} |
| 1317 |
|
| 1318 |
if (flags & NFCT_CP_META) { |
| 1319 |
for (i=ATTR_TCP_STATE; i<ATTR_MAX; i++) { |
| 1320 |
if (test_bit(i, ct2->head.set)) { |
| 1321 |
assert(copy_attr_array[i]), |
| 1322 |
copy_attr_array[i](ct1, ct2); |
| 1323 |
set_bit(i, ct1->head.set); |
| 1324 |
} |
| 1325 |
} |
| 1326 |
} |
| 1327 |
} |
| 1328 |
|
| 1329 |
/** |
| 1330 |
* nfct_copy_attr - copy an attribute of one source object to another |
| 1331 |
* \param ct1 destination object |
| 1332 |
* \param ct2 source object |
| 1333 |
* \param flags flags |
| 1334 |
* |
| 1335 |
* This function copies one attribute (if present) to another object. |
| 1336 |
*/ |
| 1337 |
void nfct_copy_attr(struct nf_conntrack *ct1, |
| 1338 |
const struct nf_conntrack *ct2, |
| 1339 |
const enum nf_conntrack_attr type) |
| 1340 |
{ |
| 1341 |
if (test_bit(type, ct2->head.set)) { |
| 1342 |
assert(copy_attr_array[type]); |
| 1343 |
copy_attr_array[type](ct1, ct2); |
| 1344 |
set_bit(type, ct1->head.set); |
| 1345 |
} |
| 1346 |
} |
| 1347 |
|
| 1348 |
/** |
| 1349 |
* @} |
| 1350 |
*/ |
| 1351 |
|
| 1352 |
/** |
| 1353 |
* \defgroup bsf Kernel-space filtering for events |
| 1354 |
* |
| 1355 |
* @{ |
| 1356 |
*/ |
| 1357 |
|
| 1358 |
/** |
| 1359 |
* nfct_filter_create - create a filter |
| 1360 |
* |
| 1361 |
* This function returns a valid pointer on success, otherwise NULL is |
| 1362 |
* returned and errno is appropriately set. |
| 1363 |
*/ |
| 1364 |
struct nfct_filter *nfct_filter_create(void) |
| 1365 |
{ |
| 1366 |
return calloc(1, sizeof(struct nfct_filter)); |
| 1367 |
} |
| 1368 |
|
| 1369 |
/** |
| 1370 |
* nfct_filter_destroy - destroy a filter |
| 1371 |
* \param filter filter that we want to destroy |
| 1372 |
* |
| 1373 |
* This function releases the memory that is used by the filter object. |
| 1374 |
* However, please note that this function does *not* detach an already |
| 1375 |
* attached filter. |
| 1376 |
*/ |
| 1377 |
void nfct_filter_destroy(struct nfct_filter *filter) |
| 1378 |
{ |
| 1379 |
assert(filter != NULL); |
| 1380 |
free(filter); |
| 1381 |
filter = NULL; |
| 1382 |
} |
| 1383 |
|
| 1384 |
/** |
| 1385 |
* nfct_filter_add_attr - add a filter attribute of the filter object |
| 1386 |
* \param filter filter object that we want to modify |
| 1387 |
* \param type filter attribute type |
| 1388 |
* \param value pointer to the value of the filter attribute |
| 1389 |
* |
| 1390 |
* Limitations: You can add up to 127 IPv4 addresses and masks for |
| 1391 |
* NFCT_FILTER_SRC_IPV4 and, similarly, 127 for NFCT_FILTER_DST_IPV4. |
| 1392 |
*/ |
| 1393 |
void nfct_filter_add_attr(struct nfct_filter *filter, |
| 1394 |
const enum nfct_filter_attr type, |
| 1395 |
const void *value) |
| 1396 |
{ |
| 1397 |
assert(filter != NULL); |
| 1398 |
assert(value != NULL); |
| 1399 |
|
| 1400 |
if (unlikely(type >= NFCT_FILTER_MAX)) |
| 1401 |
return; |
| 1402 |
|
| 1403 |
if (filter_attr_array[type]) { |
| 1404 |
filter_attr_array[type](filter, value); |
| 1405 |
set_bit(type, filter->set); |
| 1406 |
} |
| 1407 |
} |
| 1408 |
|
| 1409 |
/** |
| 1410 |
* nfct_filter_add_attr_u32 - add an u32 filter attribute of the filter object |
| 1411 |
* \param filter filter object that we want to modify |
| 1412 |
* \param type filter attribute type |
| 1413 |
* \param value value of the filter attribute using unsigned int (32 bits). |
| 1414 |
* |
| 1415 |
* Limitations: You can add up to 255 protocols which is a reasonable limit. |
| 1416 |
*/ |
| 1417 |
void nfct_filter_add_attr_u32(struct nfct_filter *filter, |
| 1418 |
const enum nfct_filter_attr type, |
| 1419 |
uint32_t value) |
| 1420 |
{ |
| 1421 |
nfct_filter_add_attr(filter, type, &value); |
| 1422 |
} |
| 1423 |
|
| 1424 |
/** |
| 1425 |
* nfct_filter_set_logic - set the filter logic for an attribute type |
| 1426 |
* \param filter filter object that we want to modify |
| 1427 |
* \param type filter attribute type |
| 1428 |
* \param logic filter logic that we want to use |
| 1429 |
* |
| 1430 |
* You can only use this function once to set the filtering logic for |
| 1431 |
* one attribute. You can define two logics: NFCT_FILTER_LOGIC_POSITIVE |
| 1432 |
* that accept events that match the filter, and NFCT_FILTER_LOGIC_NEGATIVE |
| 1433 |
* that rejects events that match the filter. Default filtering logic is |
| 1434 |
* NFCT_FILTER_LOGIC_POSITIVE. |
| 1435 |
* |
| 1436 |
* On error, it returns -1 and errno is appropriately set. On success, it |
| 1437 |
* returns 0. |
| 1438 |
*/ |
| 1439 |
int nfct_filter_set_logic(struct nfct_filter *filter, |
| 1440 |
const enum nfct_filter_attr type, |
| 1441 |
const enum nfct_filter_logic logic) |
| 1442 |
{ |
| 1443 |
if (unlikely(type >= NFCT_FILTER_MAX)) { |
| 1444 |
errno = ENOTSUP; |
| 1445 |
return -1; |
| 1446 |
} |
| 1447 |
|
| 1448 |
if (filter->logic[type]) { |
| 1449 |
errno = EBUSY; |
| 1450 |
return -1; |
| 1451 |
} |
| 1452 |
|
| 1453 |
filter->logic[type] = logic; |
| 1454 |
|
| 1455 |
return 0; |
| 1456 |
} |
| 1457 |
|
| 1458 |
/** |
| 1459 |
* nfct_filter_attach - attach a filter to a socket descriptor |
| 1460 |
* \param fd socket descriptor |
| 1461 |
* \param filter filter that we want to attach to the socket |
| 1462 |
* |
| 1463 |
* This function returns -1 on error and set errno appropriately. If the |
| 1464 |
* function returns EINVAL probably you have found a bug in it. Please, |
| 1465 |
* report this. |
| 1466 |
*/ |
| 1467 |
int nfct_filter_attach(int fd, struct nfct_filter *filter) |
| 1468 |
{ |
| 1469 |
assert(filter != NULL); |
| 1470 |
|
| 1471 |
return __setup_netlink_socket_filter(fd, filter); |
| 1472 |
} |
| 1473 |
|
| 1474 |
/** |
| 1475 |
* nfct_filter_detach - detach an existing filter |
| 1476 |
* \param fd socket descriptor |
| 1477 |
* |
| 1478 |
* This function returns -1 on error and set errno appropriately. |
| 1479 |
*/ |
| 1480 |
int nfct_filter_detach(int fd) |
| 1481 |
{ |
| 1482 |
int val = 0; |
| 1483 |
|
| 1484 |
return setsockopt(fd, SOL_SOCKET, SO_DETACH_FILTER, &val, sizeof(val)); |
| 1485 |
} |
| 1486 |
|
| 1487 |
/** |
| 1488 |
* @} |
| 1489 |
*/ |
| 1490 |
|
| 1491 |
/** |
| 1492 |
* \defgroup dumpfilter Kernel-space filtering for dumping |
| 1493 |
* |
| 1494 |
* @{ |
| 1495 |
*/ |
| 1496 |
|
| 1497 |
/** |
| 1498 |
* nfct_filter_dump_create - create a dump filter |
| 1499 |
* |
| 1500 |
* This function returns a valid pointer on success, otherwise NULL is |
| 1501 |
* returned and errno is appropriately set. |
| 1502 |
*/ |
| 1503 |
struct nfct_filter_dump *nfct_filter_dump_create(void) |
| 1504 |
{ |
| 1505 |
return calloc(1, sizeof(struct nfct_filter_dump)); |
| 1506 |
} |
| 1507 |
|
| 1508 |
/** |
| 1509 |
* nfct_filter_dump_destroy - destroy a dump filter |
| 1510 |
* \param filter filter that we want to destroy |
| 1511 |
* |
| 1512 |
* This function releases the memory that is used by the filter object. |
| 1513 |
*/ |
| 1514 |
void nfct_filter_dump_destroy(struct nfct_filter_dump *filter) |
| 1515 |
{ |
| 1516 |
assert(filter != NULL); |
| 1517 |
free(filter); |
| 1518 |
filter = NULL; |
| 1519 |
} |
| 1520 |
|
| 1521 |
/** |
| 1522 |
* nfct_filter_dump_attr_set - set filter attribute |
| 1523 |
* \param filter dump filter object that we want to modify |
| 1524 |
* \param type filter attribute type |
| 1525 |
* \param value pointer to the value of the filter attribute |
| 1526 |
*/ |
| 1527 |
void nfct_filter_dump_set_attr(struct nfct_filter_dump *filter_dump, |
| 1528 |
const enum nfct_filter_dump_attr type, |
| 1529 |
const void *value) |
| 1530 |
{ |
| 1531 |
assert(filter_dump != NULL); |
| 1532 |
assert(value != NULL); |
| 1533 |
|
| 1534 |
if (unlikely(type >= NFCT_FILTER_DUMP_MAX)) |
| 1535 |
return; |
| 1536 |
|
| 1537 |
if (set_filter_dump_attr_array[type]) { |
| 1538 |
set_filter_dump_attr_array[type](filter_dump, value); |
| 1539 |
filter_dump->set |= (1 << type); |
| 1540 |
} |
| 1541 |
} |
| 1542 |
|
| 1543 |
/** |
| 1544 |
* nfct_filter_dump_attr_set_u8 - set u8 dump filter attribute |
| 1545 |
* \param filter dump filter object that we want to modify |
| 1546 |
* \param type filter attribute type |
| 1547 |
* \param value value of the filter attribute using unsigned int (32 bits). |
| 1548 |
*/ |
| 1549 |
void nfct_filter_dump_set_attr_u8(struct nfct_filter_dump *filter_dump, |
| 1550 |
const enum nfct_filter_dump_attr type, |
| 1551 |
uint8_t value) |
| 1552 |
{ |
| 1553 |
nfct_filter_dump_set_attr(filter_dump, type, &value); |
| 1554 |
} |
| 1555 |
|
| 1556 |
/** |
| 1557 |
* nfct_filter_dump_attr_set_u16 - set u16 dump filter attribute |
| 1558 |
* \param filter dump filter object that we want to modify |
| 1559 |
* \param type filter attribute type |
| 1560 |
* \param value value of the filter attribute using unsigned int (32 bits). |
| 1561 |
*/ |
| 1562 |
void nfct_filter_dump_set_attr_u16(struct nfct_filter_dump *filter_dump, |
| 1563 |
const enum nfct_filter_dump_attr type, |
| 1564 |
uint16_t value) |
| 1565 |
{ |
| 1566 |
nfct_filter_dump_set_attr(filter_dump, type, &value); |
| 1567 |
} |
| 1568 |
|
| 1569 |
/** |
| 1570 |
* @} |
| 1571 |
*/ |
| 1572 |
|
| 1573 |
/** |
| 1574 |
* \defgroup label Conntrack labels |
| 1575 |
* |
| 1576 |
* @{ |
| 1577 |
*/ |
| 1578 |
|
| 1579 |
/** |
| 1580 |
* nfct_labels_get_path - get name of default config path |
| 1581 |
* |
| 1582 |
* returns a pointer to a immutable (static) string containing |
| 1583 |
* the default connlabel.conf file location. |
| 1584 |
*/ |
| 1585 |
const char *nfct_labels_get_path(void) |
| 1586 |
{ |
| 1587 |
return __labels_get_path(); |
| 1588 |
} |
| 1589 |
|
| 1590 |
/** |
| 1591 |
* nfct_labelmap_get_name - get name of the label bit |
| 1592 |
* |
| 1593 |
* \param m label map obtained from nfct_label_open |
| 1594 |
* \param bit whose name should be returned |
| 1595 |
* |
| 1596 |
* returns a pointer to the name associated with the label. |
| 1597 |
* If no name has been configured, the empty string is returned. |
| 1598 |
* If bit is out of range, NULL is returned. |
| 1599 |
*/ |
| 1600 |
const char *nfct_labelmap_get_name(struct nfct_labelmap *m, unsigned int bit) |
| 1601 |
{ |
| 1602 |
return __labelmap_get_name(m, bit); |
| 1603 |
} |
| 1604 |
|
| 1605 |
/** |
| 1606 |
* nfct_labelmap_get_bit - get bit associated with the name |
| 1607 |
* |
| 1608 |
* \param h label handle obtained from nfct_labelmap_new |
| 1609 |
* \param name name of the label |
| 1610 |
* |
| 1611 |
* returns the bit associated with the name, or negative value on error. |
| 1612 |
*/ |
| 1613 |
int nfct_labelmap_get_bit(struct nfct_labelmap *m, const char *name) |
| 1614 |
{ |
| 1615 |
return __labelmap_get_bit(m, name); |
| 1616 |
} |
| 1617 |
|
| 1618 |
/** |
| 1619 |
* nfct_labelmap_new - create a new label map |
| 1620 |
* |
| 1621 |
* \param mapfile the file containing the bit <-> name mapping |
| 1622 |
* |
| 1623 |
* If mapfile is NULL, the default mapping file is used. |
| 1624 |
* returns a new label map, or NULL on error. |
| 1625 |
*/ |
| 1626 |
struct nfct_labelmap *nfct_labelmap_new(const char *mapfile) |
| 1627 |
{ |
| 1628 |
return __labelmap_new(mapfile); |
| 1629 |
} |
| 1630 |
|
| 1631 |
/** |
| 1632 |
* nfct_labelmap_destroy - destroy nfct_labelmap object |
| 1633 |
* |
| 1634 |
* \param map the label object to destroy. |
| 1635 |
* |
| 1636 |
* This function releases the memory that is used by the labelmap object. |
| 1637 |
*/ |
| 1638 |
void nfct_labelmap_destroy(struct nfct_labelmap *map) |
| 1639 |
{ |
| 1640 |
__labelmap_destroy(map); |
| 1641 |
} |
| 1642 |
|
| 1643 |
/** |
| 1644 |
* @} |
| 1645 |
*/ |
| 1646 |
|
| 1647 |
/* |
| 1648 |
* \defgroup bitmask bitmask object |
| 1649 |
* |
| 1650 |
* @{ |
| 1651 |
*/ |
| 1652 |
|
| 1653 |
/** |
| 1654 |
* nfct_bitmask_new - allocate a new bitmask |
| 1655 |
* |
| 1656 |
* \param max highest valid bit that can be set/unset. |
| 1657 |
* |
| 1658 |
* In case of success, this function returns a valid pointer to a memory blob, |
| 1659 |
* otherwise NULL is returned and errno is set appropiately. |
| 1660 |
*/ |
| 1661 |
struct nfct_bitmask *nfct_bitmask_new(unsigned int max) |
| 1662 |
{ |
| 1663 |
struct nfct_bitmask *b; |
| 1664 |
unsigned int bytes, words; |
| 1665 |
|
| 1666 |
if (max > 0xffff) |
| 1667 |
return NULL; |
| 1668 |
|
| 1669 |
words = DIV_ROUND_UP(max+1, 32); |
| 1670 |
bytes = words * sizeof(b->bits[0]); |
| 1671 |
|
| 1672 |
b = malloc(sizeof(*b) + bytes); |
| 1673 |
if (b) { |
| 1674 |
memset(b->bits, 0, bytes); |
| 1675 |
b->words = words; |
| 1676 |
} |
| 1677 |
return b; |
| 1678 |
} |
| 1679 |
|
| 1680 |
/* |
| 1681 |
* nfct_bitmask_clone - duplicate a bitmask object |
| 1682 |
* |
| 1683 |
* \param b pointer to the bitmask object to duplicate |
| 1684 |
* |
| 1685 |
* returns an identical copy of the bitmask. |
| 1686 |
*/ |
| 1687 |
struct nfct_bitmask *nfct_bitmask_clone(const struct nfct_bitmask *b) |
| 1688 |
{ |
| 1689 |
unsigned int bytes = b->words * sizeof(b->bits[0]); |
| 1690 |
struct nfct_bitmask *copy; |
| 1691 |
|
| 1692 |
bytes += sizeof(*b); |
| 1693 |
|
| 1694 |
copy = malloc(bytes); |
| 1695 |
if (copy) |
| 1696 |
memcpy(copy, b, bytes); |
| 1697 |
return copy; |
| 1698 |
} |
| 1699 |
|
| 1700 |
/* |
| 1701 |
* nfct_bitmask_set_bit - set bit in the bitmask |
| 1702 |
* |
| 1703 |
* \param b pointer to the bitmask object |
| 1704 |
* \param bit the bit to set |
| 1705 |
*/ |
| 1706 |
void nfct_bitmask_set_bit(struct nfct_bitmask *b, unsigned int bit) |
| 1707 |
{ |
| 1708 |
unsigned int bits = b->words * 32; |
| 1709 |
if (bit < bits) |
| 1710 |
set_bit(bit, b->bits); |
| 1711 |
} |
| 1712 |
|
| 1713 |
/* |
| 1714 |
* nfct_bitmask_test_bit - test if a bit in the bitmask is set |
| 1715 |
* |
| 1716 |
* \param b pointer to the bitmask object |
| 1717 |
* \param bit the bit to test |
| 1718 |
* |
| 1719 |
* returns 0 if the bit is not set. |
| 1720 |
*/ |
| 1721 |
int nfct_bitmask_test_bit(const struct nfct_bitmask *b, unsigned int bit) |
| 1722 |
{ |
| 1723 |
unsigned int bits = b->words * 32; |
| 1724 |
return bit < bits && test_bit(bit, b->bits); |
| 1725 |
} |
| 1726 |
|
| 1727 |
/* |
| 1728 |
* nfct_bitmask_unset_bit - unset bit in the bitmask |
| 1729 |
* |
| 1730 |
* \param b pointer to the bitmask object |
| 1731 |
* \param bit the bit to clear |
| 1732 |
*/ |
| 1733 |
void nfct_bitmask_unset_bit(struct nfct_bitmask *b, unsigned int bit) |
| 1734 |
{ |
| 1735 |
unsigned int bits = b->words * 32; |
| 1736 |
if (bit < bits) |
| 1737 |
unset_bit(bit, b->bits); |
| 1738 |
} |
| 1739 |
|
| 1740 |
/* |
| 1741 |
* nfct_bitmask_maxbit - return highest bit that may be set/unset |
| 1742 |
* |
| 1743 |
* \param b pointer to the bitmask object |
| 1744 |
*/ |
| 1745 |
unsigned int nfct_bitmask_maxbit(const struct nfct_bitmask *b) |
| 1746 |
{ |
| 1747 |
return (b->words * 32) - 1; |
| 1748 |
} |
| 1749 |
|
| 1750 |
/* |
| 1751 |
* nfct_bitmask_destroy - destroy bitmask object |
| 1752 |
* |
| 1753 |
* \param b pointer to the bitmask object |
| 1754 |
* |
| 1755 |
* This function releases the memory that is used by the bitmask object. |
| 1756 |
* |
| 1757 |
* If you assign a bitmask object to a nf_conntrack object using |
| 1758 |
* nfct_set_attr ATTR_CONNLABEL, then the ownership of the bitmask |
| 1759 |
* object passes on to the nf_conntrack object. The nfct_bitmask object |
| 1760 |
* will be destroyed when the nf_conntrack object is destroyed. |
| 1761 |
*/ |
| 1762 |
void nfct_bitmask_destroy(struct nfct_bitmask *b) |
| 1763 |
{ |
| 1764 |
free(b); |
| 1765 |
} |
| 1766 |
|
| 1767 |
/* |
| 1768 |
* nfct_bitmask_clear - clear a bitmask object |
| 1769 |
* |
| 1770 |
* \param b pointer to the bitmask object to clear |
| 1771 |
*/ |
| 1772 |
void nfct_bitmask_clear(struct nfct_bitmask *b) |
| 1773 |
{ |
| 1774 |
unsigned int bytes = b->words * sizeof(b->bits[0]); |
| 1775 |
memset(b->bits, 0, bytes); |
| 1776 |
} |
| 1777 |
|
| 1778 |
/* |
| 1779 |
* nfct_bitmask_equal - compare two bitmask objects |
| 1780 |
* |
| 1781 |
* \param b1 pointer to a valid bitmask object |
| 1782 |
* \param b2 pointer to a valid bitmask object |
| 1783 |
* |
| 1784 |
* If both bitmask object are equal, this function returns true, otherwise |
| 1785 |
* false is returned. |
| 1786 |
*/ |
| 1787 |
bool nfct_bitmask_equal(const struct nfct_bitmask *b1, const struct nfct_bitmask *b2) |
| 1788 |
{ |
| 1789 |
if (b1->words != b2->words) |
| 1790 |
return false; |
| 1791 |
|
| 1792 |
return memcmp(b1->bits, b2->bits, b1->words * sizeof(b1->bits[0])) == 0; |
| 1793 |
} |
| 1794 |
|
| 1795 |
/** |
| 1796 |
* @} |
| 1797 |
*/ |