| 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 <string.h> /* for memset */ |
| 12 |
#include <errno.h> |
| 13 |
#include <assert.h> |
| 14 |
#include <libmnl/libmnl.h> |
| 15 |
|
| 16 |
#include "internal/internal.h" |
| 17 |
|
| 18 |
/** |
| 19 |
* \defgroup exp Expect object handling |
| 20 |
* @{ |
| 21 |
*/ |
| 22 |
|
| 23 |
/** |
| 24 |
* nfexp_new - allocate a new expectation |
| 25 |
* |
| 26 |
* In case of success, this function returns a valid pointer to a memory blob, |
| 27 |
* otherwise NULL is returned and errno is set appropiately. |
| 28 |
*/ |
| 29 |
struct nf_expect *nfexp_new(void) |
| 30 |
{ |
| 31 |
struct nf_expect *exp; |
| 32 |
|
| 33 |
exp = malloc(sizeof(struct nf_expect)); |
| 34 |
if (!exp) |
| 35 |
return NULL; |
| 36 |
|
| 37 |
memset(exp, 0, sizeof(struct nf_expect)); |
| 38 |
|
| 39 |
return exp; |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* nfexp_destroy - release an expectation object |
| 44 |
* \param exp pointer to the expectation object |
| 45 |
*/ |
| 46 |
void nfexp_destroy(struct nf_expect *exp) |
| 47 |
{ |
| 48 |
assert(exp != NULL); |
| 49 |
free(exp); |
| 50 |
exp = NULL; /* bugtrap */ |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* nfexp_sizeof - return the size in bytes of a certain expect object |
| 55 |
* \param exp pointer to the expect object |
| 56 |
*/ |
| 57 |
size_t nfexp_sizeof(const struct nf_expect *exp) |
| 58 |
{ |
| 59 |
assert(exp != NULL); |
| 60 |
return sizeof(*exp); |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* nfexp_maxsize - return the maximum size in bytes of a expect object |
| 65 |
* |
| 66 |
* Use this function if you want to allocate a expect object in the stack |
| 67 |
* instead of the heap. For example: |
| 68 |
* |
| 69 |
* char buf[nfexp_maxsize()]; |
| 70 |
* struct nf_expect *exp = (struct nf_expect *) buf; |
| 71 |
* memset(exp, 0, nfexp_maxsize()); |
| 72 |
* |
| 73 |
* Note: As for now this function returns the same size that nfexp_sizeof(exp) |
| 74 |
* does although _this could change in the future_. Therefore, do not assume |
| 75 |
* that nfexp_sizeof(exp) == nfexp_maxsize(). |
| 76 |
*/ |
| 77 |
size_t nfexp_maxsize(void) |
| 78 |
{ |
| 79 |
return sizeof(struct nf_expect); |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* nfexp_clone - clone a expectation object |
| 84 |
* \param exp pointer to a valid expectation object |
| 85 |
* |
| 86 |
* On error, NULL is returned and errno is appropiately set. Otherwise, |
| 87 |
* a valid pointer to the clone expect is returned. |
| 88 |
*/ |
| 89 |
struct nf_expect *nfexp_clone(const struct nf_expect *exp) |
| 90 |
{ |
| 91 |
struct nf_expect *clone; |
| 92 |
|
| 93 |
assert(exp != NULL); |
| 94 |
|
| 95 |
if ((clone = nfexp_new()) == NULL) |
| 96 |
return NULL; |
| 97 |
memcpy(clone, exp, sizeof(*exp)); |
| 98 |
|
| 99 |
return clone; |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* nfexp_cmp - compare two expectation objects |
| 104 |
* \param exp1 pointer to a valid expectation object |
| 105 |
* \param exp2 pointer to a valid expectation object |
| 106 |
* \param flags flags |
| 107 |
* |
| 108 |
* This function only compare attribute set in both objects, by default |
| 109 |
* the comparison is not strict, ie. if a certain attribute is not set in one |
| 110 |
* of the objects, then such attribute is not used in the comparison. |
| 111 |
* If you want more strict comparisons, you can use the appropriate flags |
| 112 |
* to modify this behaviour (see NFCT_CMP_STRICT and NFCT_CMP_MASK). |
| 113 |
* |
| 114 |
* The available flags are: |
| 115 |
* - NFCT_CMP_STRICT: the compared objects must have the same attributes |
| 116 |
* and the same values, otherwise it returns that the objects are |
| 117 |
* different. |
| 118 |
* - NFCT_CMP_MASK: the first object is used as mask, this means that |
| 119 |
* if an attribute is present in exp1 but not in exp2, this function |
| 120 |
* returns that the objects are different. |
| 121 |
* |
| 122 |
* Other existing flags that are used by nfct_cmp() are ignored. |
| 123 |
* |
| 124 |
* If both conntrack object are equal, this function returns 1, otherwise |
| 125 |
* 0 is returned. |
| 126 |
*/ |
| 127 |
int nfexp_cmp(const struct nf_expect *exp1, const struct nf_expect *exp2, |
| 128 |
unsigned int flags) |
| 129 |
{ |
| 130 |
assert(exp1 != NULL); |
| 131 |
assert(exp2 != NULL); |
| 132 |
|
| 133 |
return __cmp_expect(exp1, exp2, flags); |
| 134 |
} |
| 135 |
|
| 136 |
/** |
| 137 |
* @} |
| 138 |
*/ |
| 139 |
|
| 140 |
/** |
| 141 |
* \defgroup LibrarySetup Library setup |
| 142 |
* @{ |
| 143 |
*/ |
| 144 |
|
| 145 |
/** |
| 146 |
* nfexp_callback_register - register a callback |
| 147 |
* \param h library handler |
| 148 |
* \param cb callback used to process expect received |
| 149 |
* \param data data used by the callback, if any. |
| 150 |
* |
| 151 |
* This function register a callback to handle the expect received, |
| 152 |
* in case of error -1 is returned and errno is set appropiately, otherwise |
| 153 |
* 0 is returned. |
| 154 |
* |
| 155 |
* Note that the data parameter is optional, if you do not want to pass any |
| 156 |
* data to your callback, then use NULL. |
| 157 |
*/ |
| 158 |
int nfexp_callback_register(struct nfct_handle *h, |
| 159 |
enum nf_conntrack_msg_type type, |
| 160 |
int (*cb)(enum nf_conntrack_msg_type type, |
| 161 |
struct nf_expect *exp, |
| 162 |
void *data), |
| 163 |
void *data) |
| 164 |
{ |
| 165 |
struct __data_container *container; |
| 166 |
|
| 167 |
assert(h != NULL); |
| 168 |
|
| 169 |
container = malloc(sizeof(struct __data_container)); |
| 170 |
if (!container) |
| 171 |
return -1; |
| 172 |
memset(container, 0, sizeof(struct __data_container)); |
| 173 |
|
| 174 |
h->expect_cb = cb; |
| 175 |
container->h = h; |
| 176 |
container->type = type; |
| 177 |
container->data = data; |
| 178 |
|
| 179 |
h->nfnl_cb_exp.call = __callback; |
| 180 |
h->nfnl_cb_exp.data = container; |
| 181 |
h->nfnl_cb_exp.attr_count = CTA_EXPECT_MAX; |
| 182 |
|
| 183 |
nfnl_callback_register(h->nfnlssh_exp, |
| 184 |
IPCTNL_MSG_EXP_NEW, |
| 185 |
&h->nfnl_cb_exp); |
| 186 |
|
| 187 |
nfnl_callback_register(h->nfnlssh_exp, |
| 188 |
IPCTNL_MSG_EXP_DELETE, |
| 189 |
&h->nfnl_cb_exp); |
| 190 |
|
| 191 |
return 0; |
| 192 |
} |
| 193 |
|
| 194 |
/** |
| 195 |
* nfexp_callback_unregister - unregister a callback |
| 196 |
* \param h library handler |
| 197 |
*/ |
| 198 |
void nfexp_callback_unregister(struct nfct_handle *h) |
| 199 |
{ |
| 200 |
assert(h != NULL); |
| 201 |
|
| 202 |
nfnl_callback_unregister(h->nfnlssh_exp, IPCTNL_MSG_EXP_NEW); |
| 203 |
nfnl_callback_unregister(h->nfnlssh_exp, IPCTNL_MSG_EXP_DELETE); |
| 204 |
|
| 205 |
h->expect_cb = NULL; |
| 206 |
free(h->nfnl_cb_exp.data); |
| 207 |
|
| 208 |
h->nfnl_cb_exp.call = NULL; |
| 209 |
h->nfnl_cb_exp.data = NULL; |
| 210 |
h->nfnl_cb_exp.attr_count = 0; |
| 211 |
} |
| 212 |
|
| 213 |
/** |
| 214 |
* nfexp_callback_register2 - register a callback |
| 215 |
* \param h library handler |
| 216 |
* \param cb callback used to process expect received |
| 217 |
* \param data data used by the callback, if any. |
| 218 |
* |
| 219 |
* This function register a callback to handle the expect received, |
| 220 |
* in case of error -1 is returned and errno is set appropiately, otherwise |
| 221 |
* 0 is returned. |
| 222 |
* |
| 223 |
* Note that the data parameter is optional, if you do not want to pass any |
| 224 |
* data to your callback, then use NULL. |
| 225 |
* |
| 226 |
* NOTICE: The difference with nfexp_callback_register() is that this function |
| 227 |
* uses the new callback interface that includes the Netlink header. |
| 228 |
* |
| 229 |
* WARNING: Don't mix nfexp_callback_register() and nfexp_callback_register2() |
| 230 |
* calls, use only once at a time. |
| 231 |
*/ |
| 232 |
int nfexp_callback_register2(struct nfct_handle *h, |
| 233 |
enum nf_conntrack_msg_type type, |
| 234 |
int (*cb)(const struct nlmsghdr *nlh, |
| 235 |
enum nf_conntrack_msg_type type, |
| 236 |
struct nf_expect *exp, |
| 237 |
void *data), |
| 238 |
void *data) |
| 239 |
{ |
| 240 |
struct __data_container *container; |
| 241 |
|
| 242 |
assert(h != NULL); |
| 243 |
|
| 244 |
container = malloc(sizeof(struct __data_container)); |
| 245 |
if (!container) |
| 246 |
return -1; |
| 247 |
memset(container, 0, sizeof(struct __data_container)); |
| 248 |
|
| 249 |
h->expect_cb2 = cb; |
| 250 |
container->h = h; |
| 251 |
container->type = type; |
| 252 |
container->data = data; |
| 253 |
|
| 254 |
h->nfnl_cb_exp.call = __callback; |
| 255 |
h->nfnl_cb_exp.data = container; |
| 256 |
h->nfnl_cb_exp.attr_count = CTA_EXPECT_MAX; |
| 257 |
|
| 258 |
nfnl_callback_register(h->nfnlssh_exp, |
| 259 |
IPCTNL_MSG_EXP_NEW, |
| 260 |
&h->nfnl_cb_exp); |
| 261 |
|
| 262 |
nfnl_callback_register(h->nfnlssh_exp, |
| 263 |
IPCTNL_MSG_EXP_DELETE, |
| 264 |
&h->nfnl_cb_exp); |
| 265 |
|
| 266 |
return 0; |
| 267 |
} |
| 268 |
|
| 269 |
/** |
| 270 |
* nfexp_callback_unregister2 - unregister a callback |
| 271 |
* \param h library handler |
| 272 |
*/ |
| 273 |
void nfexp_callback_unregister2(struct nfct_handle *h) |
| 274 |
{ |
| 275 |
assert(h != NULL); |
| 276 |
|
| 277 |
nfnl_callback_unregister(h->nfnlssh_exp, IPCTNL_MSG_EXP_NEW); |
| 278 |
nfnl_callback_unregister(h->nfnlssh_exp, IPCTNL_MSG_EXP_DELETE); |
| 279 |
|
| 280 |
h->expect_cb2 = NULL; |
| 281 |
free(h->nfnl_cb_exp.data); |
| 282 |
|
| 283 |
h->nfnl_cb_exp.call = NULL; |
| 284 |
h->nfnl_cb_exp.data = NULL; |
| 285 |
h->nfnl_cb_exp.attr_count = 0; |
| 286 |
} |
| 287 |
|
| 288 |
/** |
| 289 |
* @} |
| 290 |
*/ |
| 291 |
|
| 292 |
/** |
| 293 |
* \defgroup exp Expect object handling |
| 294 |
* @{ |
| 295 |
*/ |
| 296 |
|
| 297 |
/** |
| 298 |
* nfexp_set_attr - set the value of a certain expect attribute |
| 299 |
* \param exp pointer to a valid expect |
| 300 |
* \param type attribute type |
| 301 |
* \param value pointer to the attribute value |
| 302 |
* |
| 303 |
* Note that certain attributes are unsettable: |
| 304 |
* - ATTR_EXP_USE |
| 305 |
* - ATTR_EXP_ID |
| 306 |
* - ATTR_EXP_*_COUNTER_* |
| 307 |
* The call of this function for such attributes do nothing. |
| 308 |
*/ |
| 309 |
void nfexp_set_attr(struct nf_expect *exp, |
| 310 |
const enum nf_expect_attr type, |
| 311 |
const void *value) |
| 312 |
{ |
| 313 |
assert(exp != NULL); |
| 314 |
assert(value != NULL); |
| 315 |
|
| 316 |
if (type >= ATTR_EXP_MAX) |
| 317 |
return; |
| 318 |
|
| 319 |
if (set_exp_attr_array[type]) { |
| 320 |
set_exp_attr_array[type](exp, value); |
| 321 |
set_bit(type, exp->set); |
| 322 |
} |
| 323 |
} |
| 324 |
|
| 325 |
/** |
| 326 |
* nfexp_set_attr_u8 - set the value of a certain expect attribute |
| 327 |
* \param exp pointer to a valid expect |
| 328 |
* \param type attribute type |
| 329 |
* \param value unsigned 8 bits attribute value |
| 330 |
*/ |
| 331 |
void nfexp_set_attr_u8(struct nf_expect *exp, |
| 332 |
const enum nf_expect_attr type, |
| 333 |
uint8_t value) |
| 334 |
{ |
| 335 |
nfexp_set_attr(exp, type, &value); |
| 336 |
} |
| 337 |
|
| 338 |
/** |
| 339 |
* nfexp_set_attr_u16 - set the value of a certain expect attribute |
| 340 |
* \param exp pointer to a valid expect |
| 341 |
* \param type attribute type |
| 342 |
* \param value unsigned 16 bits attribute value |
| 343 |
*/ |
| 344 |
void nfexp_set_attr_u16(struct nf_expect *exp, |
| 345 |
const enum nf_expect_attr type, |
| 346 |
uint16_t value) |
| 347 |
{ |
| 348 |
nfexp_set_attr(exp, type, &value); |
| 349 |
} |
| 350 |
|
| 351 |
/** |
| 352 |
* nfexp_set_attr_u32 - set the value of a certain expect attribute |
| 353 |
* \param exp pointer to a valid expect |
| 354 |
* \param type attribute type |
| 355 |
* \param value unsigned 32 bits attribute value |
| 356 |
*/ |
| 357 |
void nfexp_set_attr_u32(struct nf_expect *exp, |
| 358 |
const enum nf_expect_attr type, |
| 359 |
uint32_t value) |
| 360 |
{ |
| 361 |
nfexp_set_attr(exp, type, &value); |
| 362 |
} |
| 363 |
|
| 364 |
/** |
| 365 |
* nfexp_get_attr - get an expect attribute |
| 366 |
* \param exp pointer to a valid expect |
| 367 |
* \param type attribute type |
| 368 |
* |
| 369 |
* In case of success a valid pointer to the attribute requested is returned, |
| 370 |
* on error NULL is returned and errno is set appropiately. |
| 371 |
*/ |
| 372 |
const void *nfexp_get_attr(const struct nf_expect *exp, |
| 373 |
const enum nf_expect_attr type) |
| 374 |
{ |
| 375 |
assert(exp != NULL); |
| 376 |
|
| 377 |
if (type >= ATTR_EXP_MAX) { |
| 378 |
errno = EINVAL; |
| 379 |
return NULL; |
| 380 |
} |
| 381 |
|
| 382 |
if (!test_bit(type, exp->set)) { |
| 383 |
errno = ENODATA; |
| 384 |
return NULL; |
| 385 |
} |
| 386 |
|
| 387 |
return get_exp_attr_array[type](exp); |
| 388 |
} |
| 389 |
|
| 390 |
/** |
| 391 |
* nfexp_get_attr_u8 - get attribute of unsigned 8-bits long |
| 392 |
* \param exp pointer to a valid expectation |
| 393 |
* \param type attribute type |
| 394 |
* |
| 395 |
* Returns the value of the requested attribute, if the attribute is not |
| 396 |
* set, 0 is returned. In order to check if the attribute is set or not, |
| 397 |
* use nfexp_attr_is_set. |
| 398 |
*/ |
| 399 |
uint8_t nfexp_get_attr_u8(const struct nf_expect *exp, |
| 400 |
const enum nf_expect_attr type) |
| 401 |
{ |
| 402 |
const uint8_t *ret = nfexp_get_attr(exp, type); |
| 403 |
return ret == NULL ? 0 : *ret; |
| 404 |
} |
| 405 |
|
| 406 |
/** |
| 407 |
* nfexp_get_attr_u16 - get attribute of unsigned 16-bits long |
| 408 |
* \param exp pointer to a valid expectation |
| 409 |
* \param type attribute type |
| 410 |
* |
| 411 |
* Returns the value of the requested attribute, if the attribute is not |
| 412 |
* set, 0 is returned. In order to check if the attribute is set or not, |
| 413 |
* use nfexp_attr_is_set. |
| 414 |
*/ |
| 415 |
uint16_t nfexp_get_attr_u16(const struct nf_expect *exp, |
| 416 |
const enum nf_expect_attr type) |
| 417 |
{ |
| 418 |
const uint16_t *ret = nfexp_get_attr(exp, type); |
| 419 |
return ret == NULL ? 0 : *ret; |
| 420 |
} |
| 421 |
|
| 422 |
/** |
| 423 |
* nfexp_get_attr_u32 - get attribute of unsigned 32-bits long |
| 424 |
* \param exp pointer to a valid expectation |
| 425 |
* \param type attribute type |
| 426 |
* |
| 427 |
* Returns the value of the requested attribute, if the attribute is not |
| 428 |
* set, 0 is returned. In order to check if the attribute is set or not, |
| 429 |
* use nfexp_attr_is_set. |
| 430 |
*/ |
| 431 |
uint32_t nfexp_get_attr_u32(const struct nf_expect *exp, |
| 432 |
const enum nf_expect_attr type) |
| 433 |
{ |
| 434 |
const uint32_t *ret = nfexp_get_attr(exp, type); |
| 435 |
return ret == NULL ? 0 : *ret; |
| 436 |
} |
| 437 |
|
| 438 |
/** |
| 439 |
* nfexp_attr_is_set - check if a certain attribute is set |
| 440 |
* \param exp pointer to a valid expectation object |
| 441 |
* \param type attribute type |
| 442 |
* |
| 443 |
* On error, -1 is returned and errno is set appropiately, otherwise |
| 444 |
* the value of the attribute is returned. |
| 445 |
*/ |
| 446 |
int nfexp_attr_is_set(const struct nf_expect *exp, |
| 447 |
const enum nf_expect_attr type) |
| 448 |
{ |
| 449 |
assert(exp != NULL); |
| 450 |
|
| 451 |
if (type >= ATTR_EXP_MAX) { |
| 452 |
errno = EINVAL; |
| 453 |
return -1; |
| 454 |
} |
| 455 |
return test_bit(type, exp->set); |
| 456 |
} |
| 457 |
|
| 458 |
/** |
| 459 |
* nfexp_attr_unset - unset a certain attribute |
| 460 |
* \param type attribute type |
| 461 |
* \param exp pointer to a valid expectation object |
| 462 |
* |
| 463 |
* On error, -1 is returned and errno is set appropiately, otherwise |
| 464 |
* 0 is returned. |
| 465 |
*/ |
| 466 |
int nfexp_attr_unset(struct nf_expect *exp, |
| 467 |
const enum nf_expect_attr type) |
| 468 |
{ |
| 469 |
assert(exp != NULL); |
| 470 |
|
| 471 |
if (type >= ATTR_EXP_MAX) { |
| 472 |
errno = EINVAL; |
| 473 |
return -1; |
| 474 |
} |
| 475 |
unset_bit(type, exp->set); |
| 476 |
|
| 477 |
return 0; |
| 478 |
} |
| 479 |
|
| 480 |
/** |
| 481 |
* @} |
| 482 |
*/ |
| 483 |
|
| 484 |
/** |
| 485 |
* \defgroup nl Low level object to Netlink message |
| 486 |
* @{ |
| 487 |
*/ |
| 488 |
|
| 489 |
/** |
| 490 |
* nfexp_build_expect - build a netlink message from a conntrack object |
| 491 |
* \param ssh nfnetlink subsystem handler |
| 492 |
* \param req buffer used to build the netlink message |
| 493 |
* \param size size of the buffer passed |
| 494 |
* \param type netlink message type |
| 495 |
* \param flags netlink flags |
| 496 |
* \param exp pointer to a conntrack object |
| 497 |
* |
| 498 |
* This is a low level function for those that require to be close to |
| 499 |
* netlink details via libnfnetlink. If you do want to obviate the netlink |
| 500 |
* details then we suggest you to use nfexp_query. |
| 501 |
* |
| 502 |
* On error, -1 is returned and errno is appropiately set. |
| 503 |
* On success, 0 is returned. |
| 504 |
*/ |
| 505 |
int nfexp_build_expect(struct nfnl_subsys_handle *ssh, |
| 506 |
void *req, |
| 507 |
size_t size, |
| 508 |
uint16_t type, |
| 509 |
uint16_t flags, |
| 510 |
const struct nf_expect *exp) |
| 511 |
{ |
| 512 |
assert(ssh != NULL); |
| 513 |
assert(req != NULL); |
| 514 |
assert(exp != NULL); |
| 515 |
|
| 516 |
memset(req, 0, size); |
| 517 |
|
| 518 |
return __build_expect(req, size, type, flags, exp); |
| 519 |
} |
| 520 |
|
| 521 |
static void nfexp_fill_hdr(struct nfnlhdr *req, uint16_t type, uint16_t flags, |
| 522 |
uint8_t l3num, uint8_t version) |
| 523 |
{ |
| 524 |
char *buf = (char *)&req->nlh; |
| 525 |
struct nlmsghdr *nlh; |
| 526 |
struct nfgenmsg *nfh; |
| 527 |
|
| 528 |
nlh = mnl_nlmsg_put_header(buf); |
| 529 |
nlh->nlmsg_type = (NFNL_SUBSYS_CTNETLINK_EXP << 8) | type; |
| 530 |
nlh->nlmsg_flags = NLM_F_REQUEST | flags; |
| 531 |
nlh->nlmsg_seq = 0; |
| 532 |
|
| 533 |
nfh = mnl_nlmsg_put_extra_header(nlh, sizeof(struct nfgenmsg)); |
| 534 |
nfh->nfgen_family = l3num; |
| 535 |
nfh->version = version; |
| 536 |
nfh->res_id = 0; |
| 537 |
} |
| 538 |
|
| 539 |
static int |
| 540 |
__build_query_exp(const enum nf_conntrack_query qt, |
| 541 |
const void *data, void *buffer, unsigned int size) |
| 542 |
{ |
| 543 |
struct nfnlhdr *req = buffer; |
| 544 |
const uint8_t *family = data; |
| 545 |
|
| 546 |
assert(data != NULL); |
| 547 |
assert(req != NULL); |
| 548 |
|
| 549 |
memset(buffer, 0, size); |
| 550 |
|
| 551 |
switch(qt) { |
| 552 |
case NFCT_Q_CREATE: |
| 553 |
__build_expect(req, size, IPCTNL_MSG_EXP_NEW, NLM_F_REQUEST|NLM_F_CREATE|NLM_F_ACK|NLM_F_EXCL, data); |
| 554 |
break; |
| 555 |
case NFCT_Q_CREATE_UPDATE: |
| 556 |
__build_expect(req, size, IPCTNL_MSG_EXP_NEW, NLM_F_REQUEST|NLM_F_CREATE|NLM_F_ACK, data); |
| 557 |
break; |
| 558 |
case NFCT_Q_GET: |
| 559 |
__build_expect(req, size, IPCTNL_MSG_EXP_GET, NLM_F_REQUEST|NLM_F_ACK, data); |
| 560 |
break; |
| 561 |
case NFCT_Q_DESTROY: |
| 562 |
__build_expect(req, size, IPCTNL_MSG_EXP_DELETE, NLM_F_REQUEST|NLM_F_ACK, data); |
| 563 |
break; |
| 564 |
case NFCT_Q_FLUSH: |
| 565 |
nfexp_fill_hdr(req, IPCTNL_MSG_EXP_DELETE, NLM_F_ACK, *family, |
| 566 |
NFNETLINK_V0); |
| 567 |
break; |
| 568 |
case NFCT_Q_DUMP: |
| 569 |
nfexp_fill_hdr(req, IPCTNL_MSG_EXP_GET, NLM_F_DUMP, *family, |
| 570 |
NFNETLINK_V0); |
| 571 |
break; |
| 572 |
default: |
| 573 |
errno = ENOTSUP; |
| 574 |
return -1; |
| 575 |
} |
| 576 |
return 1; |
| 577 |
} |
| 578 |
|
| 579 |
/** |
| 580 |
* nfexp_build_query - build a query in netlink message format for ctnetlink |
| 581 |
* \param ssh nfnetlink subsystem handler |
| 582 |
* \param qt query type |
| 583 |
* \param data data required to build the query |
| 584 |
* \param req buffer to build the netlink message |
| 585 |
* \param size size of the buffer passed |
| 586 |
* |
| 587 |
* This is a low level function, use it if you want to require to work |
| 588 |
* with netlink details via libnfnetlink, otherwise we suggest you to |
| 589 |
* use nfexp_query. |
| 590 |
* |
| 591 |
* The pointer to data can be a conntrack object or the protocol family |
| 592 |
* depending on the request. |
| 593 |
* |
| 594 |
* For query types: |
| 595 |
* NFEXP_Q_CREATE |
| 596 |
* NFEXP_Q_DESTROY |
| 597 |
* |
| 598 |
* Pass a valid pointer to an expectation object. |
| 599 |
* |
| 600 |
* For query types: |
| 601 |
* NFEXP_Q_FLUSH |
| 602 |
* NFEXP_Q_DUMP |
| 603 |
* |
| 604 |
* Pass a valid pointer to the protocol family (uint8_t) |
| 605 |
* |
| 606 |
* On success, 0 is returned. On error, -1 is returned and errno is set |
| 607 |
* appropiately. |
| 608 |
*/ |
| 609 |
int nfexp_build_query(struct nfnl_subsys_handle *ssh, |
| 610 |
const enum nf_conntrack_query qt, |
| 611 |
const void *data, |
| 612 |
void *buffer, |
| 613 |
unsigned int size) |
| 614 |
{ |
| 615 |
return __build_query_exp(qt, data, buffer, size); |
| 616 |
} |
| 617 |
|
| 618 |
static int __parse_expect_message_type(const struct nlmsghdr *nlh) |
| 619 |
{ |
| 620 |
uint16_t type = NFNL_MSG_TYPE(nlh->nlmsg_type); |
| 621 |
uint16_t flags = nlh->nlmsg_flags; |
| 622 |
int ret = NFCT_T_UNKNOWN; |
| 623 |
|
| 624 |
if (type == IPCTNL_MSG_EXP_NEW) { |
| 625 |
if (flags & (NLM_F_CREATE|NLM_F_EXCL)) |
| 626 |
ret = NFCT_T_NEW; |
| 627 |
else |
| 628 |
ret = NFCT_T_UPDATE; |
| 629 |
} else if (type == IPCTNL_MSG_EXP_DELETE) |
| 630 |
ret = NFCT_T_DESTROY; |
| 631 |
|
| 632 |
return ret; |
| 633 |
} |
| 634 |
|
| 635 |
/** |
| 636 |
* nfexp_parse_expect - translate a netlink message to a conntrack object |
| 637 |
* \param type do the translation iif the message type is of a certain type |
| 638 |
* \param nlh pointer to the netlink message |
| 639 |
* \param exp pointer to the conntrack object |
| 640 |
* |
| 641 |
* This is a low level function, use it in case that you require to work |
| 642 |
* with netlink details via libnfnetlink. Otherwise, we suggest you to |
| 643 |
* use the high level API. |
| 644 |
* |
| 645 |
* The message types are: |
| 646 |
* |
| 647 |
* NFEXP_T_NEW: parse messages with new conntracks |
| 648 |
* NFEXP_T_UPDATE: parse messages with conntrack updates |
| 649 |
* NFEXP_T_DESTROY: parse messages with conntrack destroy |
| 650 |
* NFEXP_T_ALL: all message types |
| 651 |
* |
| 652 |
* The message type is a flag, therefore the can be combined, ie. |
| 653 |
* NFEXP_T_NEW | NFEXP_T_DESTROY to parse only new and destroy messages |
| 654 |
* |
| 655 |
* On error, NFEXP_T_ERROR is returned and errno is set appropiately. If |
| 656 |
* the message received is not of the requested type then 0 is returned, |
| 657 |
* otherwise this function returns the message type parsed. |
| 658 |
*/ |
| 659 |
int nfexp_parse_expect(enum nf_conntrack_msg_type type, |
| 660 |
const struct nlmsghdr *nlh, |
| 661 |
struct nf_expect *exp) |
| 662 |
{ |
| 663 |
unsigned int flags; |
| 664 |
|
| 665 |
assert(nlh != NULL); |
| 666 |
assert(exp != NULL); |
| 667 |
|
| 668 |
flags = __parse_expect_message_type(nlh); |
| 669 |
if (!(flags & type)) |
| 670 |
return 0; |
| 671 |
|
| 672 |
nfexp_nlmsg_parse(nlh, exp); |
| 673 |
|
| 674 |
return flags; |
| 675 |
} |
| 676 |
|
| 677 |
/** |
| 678 |
* @} |
| 679 |
*/ |
| 680 |
|
| 681 |
/** |
| 682 |
* \defgroup cmd Send commands to kernel-space and receive replies |
| 683 |
* @{ |
| 684 |
*/ |
| 685 |
|
| 686 |
/** |
| 687 |
* nfexp_query - send a query to ctnetlink |
| 688 |
* \param h library handler |
| 689 |
* \param qt query type |
| 690 |
* \param data data required to send the query |
| 691 |
* |
| 692 |
* On error, -1 is returned and errno is explicitely set. On success, 0 |
| 693 |
* is returned. |
| 694 |
*/ |
| 695 |
int nfexp_query(struct nfct_handle *h, |
| 696 |
const enum nf_conntrack_query qt, |
| 697 |
const void *data) |
| 698 |
{ |
| 699 |
const size_t size = 4096; /* enough for now */ |
| 700 |
union { |
| 701 |
char buffer[size]; |
| 702 |
struct nfnlhdr req; |
| 703 |
} u; |
| 704 |
|
| 705 |
assert(h != NULL); |
| 706 |
assert(data != NULL); |
| 707 |
|
| 708 |
if (__build_query_exp(qt, data, &u.req, size) == -1) |
| 709 |
return -1; |
| 710 |
|
| 711 |
return nfnl_query(h->nfnlh, &u.req.nlh); |
| 712 |
} |
| 713 |
|
| 714 |
/** |
| 715 |
* nfexp_send - send a query to ctnetlink |
| 716 |
* \param h library handler |
| 717 |
* \param qt query type |
| 718 |
* \param data data required to send the query |
| 719 |
* |
| 720 |
* Like nfexp_query but we do not wait for the reply from ctnetlink. |
| 721 |
* You can use nfexp_send() and nfexp_catch() to emulate nfexp_query(). |
| 722 |
* This is particularly useful when the socket is non-blocking. |
| 723 |
* |
| 724 |
* On error, -1 is returned and errno is explicitely set. On success, 0 |
| 725 |
* is returned. |
| 726 |
*/ |
| 727 |
int nfexp_send(struct nfct_handle *h, |
| 728 |
const enum nf_conntrack_query qt, |
| 729 |
const void *data) |
| 730 |
{ |
| 731 |
const size_t size = 4096; /* enough for now */ |
| 732 |
union { |
| 733 |
char buffer[size]; |
| 734 |
struct nfnlhdr req; |
| 735 |
} u; |
| 736 |
|
| 737 |
assert(h != NULL); |
| 738 |
assert(data != NULL); |
| 739 |
|
| 740 |
if (__build_query_exp(qt, data, &u.req, size) == -1) |
| 741 |
return -1; |
| 742 |
|
| 743 |
return nfnl_send(h->nfnlh, &u.req.nlh); |
| 744 |
} |
| 745 |
|
| 746 |
/** |
| 747 |
* nfexp_catch - catch events |
| 748 |
* \param h library handler |
| 749 |
* |
| 750 |
* This function receives the event from the kernel and it invokes the |
| 751 |
* callback that was registered to this handle. |
| 752 |
* |
| 753 |
* On error, -1 is returned and errno is set appropiately. On success, |
| 754 |
* a value greater or equal to 0 is returned indicating the callback |
| 755 |
* verdict: NFCT_CB_STOP, NFCT_CB_CONTINUE or NFCT_CB_STOLEN. |
| 756 |
* |
| 757 |
* Beware that this function is equivalent to nfct_catch(), so it handles both |
| 758 |
* conntrack and expectation events. |
| 759 |
*/ |
| 760 |
int nfexp_catch(struct nfct_handle *h) |
| 761 |
{ |
| 762 |
assert(h != NULL); |
| 763 |
|
| 764 |
return nfnl_catch(h->nfnlh); |
| 765 |
} |
| 766 |
|
| 767 |
/** |
| 768 |
* @} |
| 769 |
*/ |
| 770 |
|
| 771 |
/** |
| 772 |
* \defgroup exp Expect object handling |
| 773 |
* @{ |
| 774 |
*/ |
| 775 |
|
| 776 |
/** |
| 777 |
* nfexp_snprintf - print a conntrack object to a buffer |
| 778 |
* \param buf buffer used to build the printable conntrack |
| 779 |
* \param size size of the buffer |
| 780 |
* \param exp pointer to a valid expectation object |
| 781 |
* \param message_type print message type (NFEXP_T_UNKNOWN, NFEXP_T_NEW,...) |
| 782 |
* \param output_type print type (NFEXP_O_DEFAULT, NFEXP_O_XML, ...) |
| 783 |
* \param flags extra flags for the output type (NFEXP_OF_LAYER3) |
| 784 |
* |
| 785 |
* If you are listening to events, probably you want to display the message |
| 786 |
* type as well. In that case, set the message type parameter to any of the |
| 787 |
* known existing types, ie. NFEXP_T_NEW, NFEXP_T_UPDATE, NFEXP_T_DESTROY. |
| 788 |
* If you pass NFEXP_T_UNKNOWN, the message type will not be output. |
| 789 |
* |
| 790 |
* Currently, the output available are: |
| 791 |
* - NFEXP_O_DEFAULT: default /proc-like output |
| 792 |
* - NFEXP_O_XML: XML output |
| 793 |
* |
| 794 |
* The output flags are: |
| 795 |
* - NFEXP_O_LAYER: include layer 3 information in the output, this is |
| 796 |
* *only* required by NFEXP_O_DEFAULT. |
| 797 |
* |
| 798 |
* On error, -1 is returned and errno is set appropiately. Otherwise the |
| 799 |
* size of what _would_ be written is returned, even if the size of the |
| 800 |
* buffer is insufficient. This behaviour is similar to snprintf. |
| 801 |
*/ |
| 802 |
int nfexp_snprintf(char *buf, |
| 803 |
unsigned int size, |
| 804 |
const struct nf_expect *exp, |
| 805 |
unsigned int msg_type, |
| 806 |
unsigned int out_type, |
| 807 |
unsigned int flags) |
| 808 |
{ |
| 809 |
assert(buf != NULL); |
| 810 |
assert(size > 0); |
| 811 |
assert(exp != NULL); |
| 812 |
|
| 813 |
return __snprintf_expect(buf, size, exp, msg_type, out_type, flags); |
| 814 |
} |
| 815 |
|
| 816 |
/** |
| 817 |
* @} |
| 818 |
*/ |