libnftnl 1.2.6
nft-expr_immediate-test.c
1/*
2 * (C) 2013 by Ana Rey Botello <anarey@gmail.com>
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
11#include <stdio.h>
12#include <stdlib.h>
13#include <string.h>
14#include <netinet/in.h>
15#include <netinet/ip.h>
16
17#include <linux/netfilter/nf_tables.h>
18#include <libmnl/libmnl.h>
19#include <libnftnl/rule.h>
20#include <libnftnl/expr.h>
21
22static int test_ok = 1;
23
24static void print_err(const char *msg)
25{
26 test_ok = 0;
27 printf("\033[31mERROR:\e[0m %s\n", msg);
28}
29
30static void cmp_nftnl_expr_verdict(struct nftnl_expr *rule_a,
31 struct nftnl_expr *rule_b)
32{
33 const char *chain_a, *chain_b;
34 uint32_t len_a, len_b;
35
36 if (nftnl_expr_get_u32(rule_a, NFTNL_EXPR_IMM_DREG) !=
37 nftnl_expr_get_u32(rule_b, NFTNL_EXPR_IMM_DREG))
38 print_err("Expr NFTNL_EXPR_IMM_DREG mismatches");
39
40 if (nftnl_expr_get_u32(rule_a, NFTNL_EXPR_IMM_VERDICT) !=
41 nftnl_expr_get_u32(rule_b, NFTNL_EXPR_IMM_VERDICT))
42 print_err("Expr NFTNL_EXPR_IMM_VERDICT mismatches");
43
44 chain_a = nftnl_expr_get(rule_a, NFTNL_EXPR_IMM_CHAIN, &len_a);
45 chain_b = nftnl_expr_get(rule_b, NFTNL_EXPR_IMM_CHAIN, &len_b);
46 if (len_a != len_b || strncmp(chain_a, chain_b, len_a))
47 print_err("Expr NFTNL_EXPR_IMM_CHAIN mismatches");
48}
49
50static void cmp_nftnl_expr_value(struct nftnl_expr *rule_a,
51 struct nftnl_expr *rule_b)
52{
53 const uint32_t *data_a, *data_b;
54 uint32_t len_a, len_b;
55
56 if (nftnl_expr_get_u32(rule_a, NFTNL_EXPR_IMM_DREG) !=
57 nftnl_expr_get_u32(rule_b, NFTNL_EXPR_IMM_DREG))
58 print_err("Expr NFTNL_EXPR_IMM_DREG mismatches");
59
60 data_a = nftnl_expr_get(rule_a, NFTNL_EXPR_IMM_DATA, &len_a);
61 data_b = nftnl_expr_get(rule_b, NFTNL_EXPR_IMM_DATA, &len_b);
62 if (len_a != len_b || memcmp(data_a, data_b, len_a))
63 print_err("Expr NFTNL_EXPR_IMM_DATA mismatches");
64}
65
66int main(int argc, char *argv[])
67{
68 struct nftnl_rule *a, *b;
69 struct nftnl_expr *ex_val, *ex_ver;
70 struct nlmsghdr *nlh;
71 char buf[4096];
72 struct nftnl_expr_iter *iter_a, *iter_b;
73 struct nftnl_expr *rule_a, *rule_b;
74 const char *chain = "tests_chain01234";
75 const char *data = "test_data_01234";
76
77 a = nftnl_rule_alloc();
78 b = nftnl_rule_alloc();
79 if (a == NULL || b == NULL)
80 print_err("OOM");
81 ex_val = nftnl_expr_alloc("immediate");
82 ex_ver = nftnl_expr_alloc("immediate");
83 if (!ex_val || !ex_ver)
84 print_err("OOM");
85
86 nftnl_expr_set_u32(ex_val, NFTNL_EXPR_IMM_DREG, 0x1234568);
87 nftnl_expr_set(ex_val, NFTNL_EXPR_IMM_DATA, data, sizeof(data));
88
89 nftnl_expr_set_u32(ex_ver, NFTNL_EXPR_IMM_DREG, 0x1234568);
90 nftnl_expr_set_u32(ex_ver, NFTNL_EXPR_IMM_VERDICT, NFT_GOTO);
91 nftnl_expr_set(ex_ver, NFTNL_EXPR_IMM_CHAIN, chain, sizeof(chain));
92
93 nftnl_rule_add_expr(a, ex_val);
94 nftnl_rule_add_expr(a, ex_ver);
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 iter_a = nftnl_expr_iter_create(a);
103 iter_b = nftnl_expr_iter_create(b);
104 if (iter_a == NULL || iter_b == NULL)
105 print_err("OOM");
106
107 rule_a = nftnl_expr_iter_next(iter_a);
108 rule_b = nftnl_expr_iter_next(iter_b);
109 if (rule_a == NULL || rule_b == NULL)
110 print_err("OOM");
111
112 cmp_nftnl_expr_value(rule_a, rule_b);
113
114 rule_a = nftnl_expr_iter_next(iter_a);
115 rule_b = nftnl_expr_iter_next(iter_b);
116 if (rule_a == NULL || rule_b == NULL)
117 print_err("OOM");
118
119 cmp_nftnl_expr_verdict(rule_a, rule_b);
120
121 if (nftnl_expr_iter_next(iter_a) != NULL ||
122 nftnl_expr_iter_next(iter_b) != NULL)
123 print_err("More 2 expr.");
124
125 nftnl_expr_iter_destroy(iter_a);
126 nftnl_expr_iter_destroy(iter_b);
127 nftnl_rule_free(a);
128 nftnl_rule_free(b);
129
130 if (!test_ok)
131 exit(EXIT_FAILURE);
132
133 printf("%s: \033[32mOK\e[0m\n", argv[0]);
134 return EXIT_SUCCESS;
135}