| 1 |
/* SPDX-License-Identifier: GPL-2.0-or-later */ |
| 2 |
/* |
| 3 |
* (C) 2013 by Ana Rey Botello <[email protected]> |
| 4 |
*/ |
| 5 |
|
| 6 |
#include <stdio.h> |
| 7 |
#include <stdlib.h> |
| 8 |
#include <string.h> |
| 9 |
|
| 10 |
#include <netinet/in.h> |
| 11 |
#include <netinet/ip.h> |
| 12 |
#include <linux/netfilter/nf_tables.h> |
| 13 |
#include <linux/netfilter/xt_iprange.h> |
| 14 |
#include <libmnl/libmnl.h> |
| 15 |
#include <libnftnl/rule.h> |
| 16 |
#include <libnftnl/expr.h> |
| 17 |
|
| 18 |
static int test_ok = 1; |
| 19 |
|
| 20 |
static void print_err(const char *msg) |
| 21 |
{ |
| 22 |
test_ok = 0; |
| 23 |
printf("\033[31mERROR:\e[0m %s\n", msg); |
| 24 |
} |
| 25 |
|
| 26 |
static void cmp_nftnl_expr(struct nftnl_expr *rule_a, |
| 27 |
struct nftnl_expr *rule_b) |
| 28 |
{ |
| 29 |
if (nftnl_expr_get_u32(rule_a, NFTNL_EXPR_REJECT_TYPE) != |
| 30 |
nftnl_expr_get_u32(rule_b, NFTNL_EXPR_REJECT_TYPE)) |
| 31 |
print_err("Expr NFTNL_EXPR_REJECT_TYPE mismatches"); |
| 32 |
if (nftnl_expr_get_u32(rule_a, NFTNL_EXPR_REJECT_CODE) != |
| 33 |
nftnl_expr_get_u32(rule_b, NFTNL_EXPR_REJECT_CODE)) |
| 34 |
print_err("Expr NFTNL_EXPR_REJECT_CODE mismatches"); |
| 35 |
} |
| 36 |
|
| 37 |
int main(int argc, char *argv[]) |
| 38 |
{ |
| 39 |
struct nftnl_rule *a, *b; |
| 40 |
struct nftnl_expr *ex; |
| 41 |
struct nlmsghdr *nlh; |
| 42 |
char buf[4096]; |
| 43 |
struct nftnl_expr_iter *iter_a, *iter_b; |
| 44 |
struct nftnl_expr *rule_a, *rule_b; |
| 45 |
|
| 46 |
a = nftnl_rule_alloc(); |
| 47 |
b = nftnl_rule_alloc(); |
| 48 |
if (a == NULL || b == NULL) |
| 49 |
print_err("OOM"); |
| 50 |
ex = nftnl_expr_alloc("reject"); |
| 51 |
if (ex == NULL) |
| 52 |
print_err("OOM"); |
| 53 |
|
| 54 |
nftnl_expr_set_u32(ex, NFTNL_EXPR_REJECT_TYPE, 0x12345678); |
| 55 |
nftnl_expr_set_u32(ex, NFTNL_EXPR_REJECT_CODE, 0x45681234); |
| 56 |
|
| 57 |
nftnl_rule_add_expr(a, ex); |
| 58 |
|
| 59 |
nlh = nftnl_nlmsg_build_hdr(buf, NFT_MSG_NEWRULE, AF_INET, 0, 1234); |
| 60 |
nftnl_rule_nlmsg_build_payload(nlh, a); |
| 61 |
|
| 62 |
if (nftnl_rule_nlmsg_parse(nlh, b) < 0) |
| 63 |
print_err("parsing problems"); |
| 64 |
|
| 65 |
iter_a = nftnl_expr_iter_create(a); |
| 66 |
iter_b = nftnl_expr_iter_create(b); |
| 67 |
if (iter_a == NULL || iter_b == NULL) |
| 68 |
print_err("OOM"); |
| 69 |
|
| 70 |
rule_a = nftnl_expr_iter_next(iter_a); |
| 71 |
rule_b = nftnl_expr_iter_next(iter_b); |
| 72 |
if (rule_a == NULL || rule_b == NULL) |
| 73 |
print_err("OOM"); |
| 74 |
|
| 75 |
cmp_nftnl_expr(rule_a, rule_b); |
| 76 |
|
| 77 |
if (nftnl_expr_iter_next(iter_a) != NULL || |
| 78 |
nftnl_expr_iter_next(iter_b) != NULL) |
| 79 |
print_err("More 1 expr."); |
| 80 |
|
| 81 |
nftnl_expr_iter_destroy(iter_a); |
| 82 |
nftnl_expr_iter_destroy(iter_b); |
| 83 |
nftnl_rule_free(a); |
| 84 |
nftnl_rule_free(b); |
| 85 |
|
| 86 |
if (!test_ok) |
| 87 |
exit(EXIT_FAILURE); |
| 88 |
|
| 89 |
printf("%s: \033[32mOK\e[0m\n", argv[0]); |
| 90 |
return EXIT_SUCCESS; |
| 91 |
} |