libnetfilter_conntrack  1.0.6
expect/compare.c
1 /*
2  * (C) 2005-2012 by Pablo Neira Ayuso <pablo@netfilter.org>
3  * (C) 2012 by Vyatta Inc. <http://www.vyatta.com>
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  */
10 
11 #include "internal/internal.h"
12 
13 static int exp_cmp(int attr,
14  const struct nf_expect *exp1,
15  const struct nf_expect *exp2,
16  unsigned int flags,
17  int (*cmp)(const struct nf_expect *exp1,
18  const struct nf_expect *exp2,
19  unsigned int flags))
20 {
21  int a = test_bit(attr, exp1->set);
22  int b = test_bit(attr, exp2->set);
23 
24  if (a && b) {
25  return cmp(exp1, exp2, flags);
26  } else if (!a && !b) {
27  return 1;
28  } else if (flags & NFCT_CMP_MASK &&
29  test_bit(attr, exp1->set)) {
30  return 0;
31  } else if (flags & NFCT_CMP_STRICT) {
32  return 0;
33  }
34  return 1;
35 }
36 
37 static int
38 cmp_exp_master(const struct nf_expect *exp1, const struct nf_expect *exp2,
39  unsigned int flags)
40 {
41  return __cmp_orig((struct nf_conntrack *)&exp1->master,
42  (struct nf_conntrack *)&exp2->master, flags);
43 }
44 
45 static int
46 cmp_exp_expected(const struct nf_expect *exp1, const struct nf_expect *exp2,
47  unsigned int flags)
48 {
49  return __cmp_orig((struct nf_conntrack *)&exp1->expected,
50  (struct nf_conntrack *)&exp2->expected, flags);
51 }
52 
53 static int
54 cmp_exp_mask(const struct nf_expect *exp1, const struct nf_expect *exp2,
55  unsigned int flags)
56 {
57  return __cmp_orig((struct nf_conntrack *)&exp1->mask,
58  (struct nf_conntrack *)&exp2->mask, flags);
59 
60 }
61 
62 static int
63 cmp_exp_zone(const struct nf_expect *exp1, const struct nf_expect *exp2,
64  unsigned int flags)
65 {
66  return exp1->zone == exp2->zone;
67 }
68 
69 static int
70 cmp_exp_flags(const struct nf_expect *exp1, const struct nf_expect *exp2,
71  unsigned int flags)
72 {
73  return (exp1->flags == exp2->flags);
74 }
75 
76 static int
77 cmp_exp_hname(const struct nf_expect *exp1, const struct nf_expect *exp2,
78  unsigned int flags)
79 {
80  return strcmp(exp1->helper_name, exp2->helper_name) == 0;
81 }
82 
83 static int
84 cmp_exp_class(const struct nf_expect *exp1, const struct nf_expect *exp2,
85  unsigned int flags)
86 {
87  return (exp1->class == exp2->class);
88 }
89 
90 static int
91 cmp_exp_natt(const struct nf_expect *exp1, const struct nf_expect *exp2,
92  unsigned int flags)
93 {
94  return __cmp_orig((struct nf_conntrack *)&exp1->nat,
95  (struct nf_conntrack *)&exp2->nat, flags);
96 
97 }
98 
99 static int
100 cmp_exp_natdir(const struct nf_expect *exp1, const struct nf_expect *exp2,
101  unsigned int flags)
102 {
103  return exp1->nat_dir == exp2->nat_dir;
104 }
105 
106 static int
107 cmp_exp_expfn(const struct nf_expect *exp1, const struct nf_expect *exp2,
108  unsigned int flags)
109 {
110  return strcmp(exp1->expectfn, exp2->expectfn) == 0;
111 }
112 
113 
114 int __cmp_expect(const struct nf_expect *exp1,
115  const struct nf_expect *exp2,
116  unsigned int flags)
117 {
118  if (!exp_cmp(ATTR_EXP_MASTER, exp1, exp2, flags, cmp_exp_master))
119  return 0;
120  if (!exp_cmp(ATTR_EXP_EXPECTED, exp1, exp2, flags, cmp_exp_expected))
121  return 0;
122  if (!exp_cmp(ATTR_EXP_MASK, exp1, exp2, flags, cmp_exp_mask))
123  return 0;
124  /* ATTR_EXP_TIMEOUT is intentionally not compared at this time; the expectations should
125  * be considered equal if only the timeout is different */
126  if (!exp_cmp(ATTR_EXP_ZONE, exp1, exp2, flags, cmp_exp_zone))
127  return 0;
128  if (!exp_cmp(ATTR_EXP_FLAGS, exp1, exp2, flags, cmp_exp_flags))
129  return 0;
130  if (!exp_cmp(ATTR_EXP_HELPER_NAME, exp1, exp2, flags, cmp_exp_hname))
131  return 0;
132  if (!exp_cmp(ATTR_EXP_CLASS, exp1, exp2, flags, cmp_exp_class))
133  return 0;
134  if (!exp_cmp(ATTR_EXP_NAT_TUPLE, exp1, exp2, flags, cmp_exp_natt))
135  return 0;
136  if (!exp_cmp(ATTR_EXP_NAT_DIR, exp1, exp2, flags, cmp_exp_natdir))
137  return 0;
138  if (!exp_cmp(ATTR_EXP_FN, exp1, exp2, flags, cmp_exp_expfn))
139  return 0;
140  return 1;
141 }