| 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 <linux/netfilter/nf_tables.h> |
| 12 |
#include <libnftnl/rule.h> |
| 13 |
#include <libnftnl/udata.h> |
| 14 |
|
| 15 |
static int test_ok = 1; |
| 16 |
|
| 17 |
static void print_err(const char *msg) |
| 18 |
{ |
| 19 |
test_ok = 0; |
| 20 |
printf("\033[31mERROR:\e[0m %s\n", msg); |
| 21 |
} |
| 22 |
|
| 23 |
static void cmp_nftnl_rule(struct nftnl_rule *a, struct nftnl_rule *b) |
| 24 |
{ |
| 25 |
const void *udata_a, *udata_b; |
| 26 |
uint32_t len_a, len_b; |
| 27 |
|
| 28 |
if (nftnl_rule_get_u32(a, NFTNL_RULE_FAMILY) != |
| 29 |
nftnl_rule_get_u32(b, NFTNL_RULE_FAMILY)) |
| 30 |
print_err("Rule family mismatches"); |
| 31 |
if (strcmp(nftnl_rule_get_str(a, NFTNL_RULE_TABLE), |
| 32 |
nftnl_rule_get_str(b, NFTNL_RULE_TABLE)) != 0) |
| 33 |
print_err("Rule table mismatches"); |
| 34 |
if (strcmp(nftnl_rule_get_str(a, NFTNL_RULE_CHAIN), |
| 35 |
nftnl_rule_get_str(b, NFTNL_RULE_CHAIN)) != 0) |
| 36 |
print_err("Rule table mismatches"); |
| 37 |
if (nftnl_rule_get_u64(a, NFTNL_RULE_HANDLE) != |
| 38 |
nftnl_rule_get_u64(b, NFTNL_RULE_HANDLE)) |
| 39 |
print_err("Rule handle mismatches"); |
| 40 |
if (nftnl_rule_get_u32(a, NFTNL_RULE_COMPAT_PROTO) != |
| 41 |
nftnl_rule_get_u32(b, NFTNL_RULE_COMPAT_PROTO)) |
| 42 |
print_err("Rule compat_proto mismatches"); |
| 43 |
if (nftnl_rule_get_u32(a, NFTNL_RULE_COMPAT_FLAGS) != |
| 44 |
nftnl_rule_get_u32(b, NFTNL_RULE_COMPAT_FLAGS)) |
| 45 |
print_err("Rule compat_flags mismatches"); |
| 46 |
if (nftnl_rule_get_u32(a, NFTNL_RULE_ID) != |
| 47 |
nftnl_rule_get_u32(b, NFTNL_RULE_ID)) |
| 48 |
print_err("Rule id mismatches"); |
| 49 |
if (nftnl_rule_get_u32(a, NFTNL_RULE_POSITION_ID) != |
| 50 |
nftnl_rule_get_u32(b, NFTNL_RULE_POSITION_ID)) |
| 51 |
print_err("Rule position_id mismatches"); |
| 52 |
if (nftnl_rule_get_u64(a, NFTNL_RULE_POSITION) != |
| 53 |
nftnl_rule_get_u64(b, NFTNL_RULE_POSITION)) |
| 54 |
print_err("Rule compat_position mismatches"); |
| 55 |
|
| 56 |
udata_a = nftnl_rule_get_data(a, NFTNL_RULE_USERDATA, &len_a); |
| 57 |
udata_b = nftnl_rule_get_data(b, NFTNL_RULE_USERDATA, &len_b); |
| 58 |
|
| 59 |
if (len_a != len_b || memcmp(udata_a, udata_b, len_a) != 0) |
| 60 |
print_err("Rule userdata mismatches"); |
| 61 |
} |
| 62 |
|
| 63 |
int main(int argc, char *argv[]) |
| 64 |
{ |
| 65 |
struct nftnl_udata_buf *udata; |
| 66 |
struct nftnl_rule *a, *b; |
| 67 |
char buf[4096]; |
| 68 |
struct nlmsghdr *nlh; |
| 69 |
|
| 70 |
a = nftnl_rule_alloc(); |
| 71 |
b = nftnl_rule_alloc(); |
| 72 |
if (a == NULL || b == NULL) |
| 73 |
print_err("OOM"); |
| 74 |
|
| 75 |
udata = nftnl_udata_buf_alloc(NFT_USERDATA_MAXLEN); |
| 76 |
if (!udata) |
| 77 |
print_err("OOM"); |
| 78 |
|
| 79 |
if (!nftnl_udata_put_strz(udata, 0, "hello world")) |
| 80 |
print_err("User data too big"); |
| 81 |
|
| 82 |
nftnl_rule_set_u32(a, NFTNL_RULE_FAMILY, AF_INET); |
| 83 |
nftnl_rule_set_str(a, NFTNL_RULE_TABLE, "table"); |
| 84 |
nftnl_rule_set_str(a, NFTNL_RULE_CHAIN, "chain"); |
| 85 |
nftnl_rule_set_u64(a, NFTNL_RULE_HANDLE, 0x1234567812345678); |
| 86 |
nftnl_rule_set_u32(a, NFTNL_RULE_COMPAT_PROTO, 0x12345678); |
| 87 |
nftnl_rule_set_u32(a, NFTNL_RULE_COMPAT_FLAGS, 0x12345678); |
| 88 |
nftnl_rule_set_u32(a, NFTNL_RULE_ID, 0x12345678); |
| 89 |
nftnl_rule_set_u32(a, NFTNL_RULE_POSITION_ID, 0x12345678); |
| 90 |
nftnl_rule_set_u64(a, NFTNL_RULE_POSITION, 0x1234567812345678); |
| 91 |
nftnl_rule_set_data(a, NFTNL_RULE_USERDATA, |
| 92 |
nftnl_udata_buf_data(udata), |
| 93 |
nftnl_udata_buf_len(udata)); |
| 94 |
nftnl_udata_buf_free(udata); |
| 95 |
|
| 96 |
nlh = nftnl_nlmsg_build_hdr(buf, NFT_MSG_NEWRULE, AF_INET, 0, 1234); |
| 97 |
nftnl_rule_nlmsg_build_payload(nlh, a); |
| 98 |
|
| 99 |
if (nftnl_rule_nlmsg_parse(nlh, b) < 0) |
| 100 |
print_err("parsing problems"); |
| 101 |
|
| 102 |
cmp_nftnl_rule(a,b); |
| 103 |
|
| 104 |
nftnl_rule_free(a); |
| 105 |
nftnl_rule_free(b); |
| 106 |
if (!test_ok) |
| 107 |
exit(EXIT_FAILURE); |
| 108 |
|
| 109 |
printf("%s: \033[32mOK\e[0m\n", argv[0]); |
| 110 |
return EXIT_SUCCESS; |
| 111 |
} |