libnftnl  1.1.4
nft-expr_bitwise-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 
15 #include <netinet/in.h>
16 #include <netinet/ip.h>
17 #include <linux/netfilter/nf_tables.h>
18 #include <libmnl/libmnl.h>
19 #include <libnftnl/rule.h>
20 #include <libnftnl/expr.h>
21 
22 static int test_ok = 1;
23 
24 static void print_err(const char *msg)
25 {
26  test_ok = 0;
27  printf("\033[31mERROR:\e[0m %s\n", msg);
28 }
29 
30 static void cmp_nftnl_expr(struct nftnl_expr *rule_a,
31  struct nftnl_expr *rule_b)
32 {
33  uint32_t maska, maskb;
34  uint32_t xora, xorb;
35 
36  if (nftnl_expr_get_u32(rule_a, NFTNL_EXPR_BITWISE_DREG) !=
37  nftnl_expr_get_u32(rule_b, NFTNL_EXPR_BITWISE_DREG))
38  print_err("Expr BITWISE_DREG mismatches");
39  if (nftnl_expr_get_u32(rule_a, NFTNL_EXPR_BITWISE_SREG) !=
40  nftnl_expr_get_u32(rule_b, NFTNL_EXPR_BITWISE_SREG))
41  print_err("Expr BITWISE_SREG mismatches");
42  if (nftnl_expr_get_u16(rule_a, NFTNL_EXPR_BITWISE_LEN) !=
43  nftnl_expr_get_u16(rule_b, NFTNL_EXPR_BITWISE_LEN))
44  print_err("Expr BITWISE_DREG mismatches");
45  nftnl_expr_get(rule_a, NFTNL_EXPR_BITWISE_MASK, &maska);
46  nftnl_expr_get(rule_b, NFTNL_EXPR_BITWISE_MASK, &maskb);
47  if (maska != maskb)
48  print_err("Size of BITWISE_MASK mismatches");
49  nftnl_expr_get(rule_a, NFTNL_EXPR_BITWISE_XOR, &xora);
50  nftnl_expr_get(rule_b, NFTNL_EXPR_BITWISE_XOR, &xorb);
51  if (xora != xorb)
52  print_err("Size of BITWISE_XOR mismatches");
53 
54 }
55 int main(int argc, char *argv[])
56 {
57  struct nftnl_rule *a, *b = NULL;
58  struct nftnl_expr *ex = NULL;
59  struct nlmsghdr *nlh;
60  char buf[4096];
61  struct nftnl_expr_iter *iter_a, *iter_b = NULL;
62  struct nftnl_expr *rule_a, *rule_b = NULL;
63  uint32_t mask = 0x01010101;
64  uint32_t xor = 0x12345678;
65 
66  a = nftnl_rule_alloc();
67  b = nftnl_rule_alloc();
68  if (a == NULL || b == NULL)
69  print_err("OOM");
70  ex = nftnl_expr_alloc("bitwise");
71  if (ex == NULL)
72  print_err("OOM");
73 
74  nftnl_expr_set_u32(ex, NFTNL_EXPR_BITWISE_SREG, 0x12345678);
75  nftnl_expr_set_u32(ex, NFTNL_EXPR_BITWISE_DREG, 0x78123456);
76  nftnl_expr_set_u32(ex, NFTNL_EXPR_BITWISE_LEN, 0x56781234);
77  nftnl_expr_set(ex, NFTNL_EXPR_BITWISE_MASK, &mask, sizeof(mask));
78  nftnl_expr_set(ex, NFTNL_EXPR_BITWISE_XOR, &xor, sizeof(xor));
79 
80  nftnl_rule_add_expr(a, ex);
81 
82  nlh = nftnl_rule_nlmsg_build_hdr(buf, NFT_MSG_NEWRULE, AF_INET, 0, 1234);
83  nftnl_rule_nlmsg_build_payload(nlh, a);
84 
85  if (nftnl_rule_nlmsg_parse(nlh, b) < 0)
86  print_err("parsing problems");
87 
88  iter_a = nftnl_expr_iter_create(a);
89  iter_b = nftnl_expr_iter_create(b);
90  if (iter_a == NULL || iter_b == NULL)
91  print_err("OOM");
92 
93  rule_a = nftnl_expr_iter_next(iter_a);
94  rule_b = nftnl_expr_iter_next(iter_b);
95  if (rule_a == NULL || rule_b == NULL)
96  print_err("OOM");
97 
98  if (nftnl_expr_iter_next(iter_a) != NULL ||
99  nftnl_expr_iter_next(iter_b) != NULL)
100  print_err("More 1 expr.");
101 
102  nftnl_expr_iter_destroy(iter_a);
103  nftnl_expr_iter_destroy(iter_b);
104 
105  cmp_nftnl_expr(rule_a,rule_b);
106 
107  nftnl_rule_free(a);
108  nftnl_rule_free(b);
109 
110  if (!test_ok)
111  exit(EXIT_FAILURE);
112 
113  printf("%s: \033[32mOK\e[0m\n", argv[0]);
114  return EXIT_SUCCESS;
115 }