Add abstract fw3_xt_reset() implementation
[project/firewall3.git] / xtables-5.h
1 /*
2 * firewall3 - 3rd OpenWrt UCI firewall implementation
3 *
4 * Copyright (C) 2013 Jo-Philipp Wich <jow@openwrt.org>
5 *
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19 #ifndef __FW3_XTABLES_5_H
20 #define __FW3_XTABLES_5_H
21
22 static inline void
23 fw3_xt_reset(void)
24 {
25 xtables_matches = NULL;
26 xtables_targets = NULL;
27 }
28
29
30 static inline const char *
31 fw3_xt_get_match_name(struct xtables_match *m)
32 {
33 return m->m->u.user.name;
34 }
35
36 static inline void
37 fw3_xt_set_match_name(struct xtables_match *m)
38 {
39 strcpy(m->m->u.user.name, m->name);
40 }
41
42 static inline bool
43 fw3_xt_has_match_parse(struct xtables_match *m)
44 {
45 return !!m->parse;
46 }
47
48 static inline void
49 fw3_xt_free_match_udata(struct xtables_match *m)
50 {
51 return;
52 }
53
54 static inline void
55 fw3_xt_merge_match_options(struct xtables_globals *g, struct xtables_match *m)
56 {
57 g->opts = xtables_merge_options(g->opts, m->extra_opts, &m->option_offset);
58 }
59
60
61 static inline const char *
62 fw3_xt_get_target_name(struct xtables_target *t)
63 {
64 return t->t->u.user.name;
65 }
66
67 static inline void
68 fw3_xt_set_target_name(struct xtables_target *t, const char *name)
69 {
70 strcpy(t->t->u.user.name, name);
71 }
72
73 static inline bool
74 fw3_xt_has_target_parse(struct xtables_target *t)
75 {
76 return !!t->parse;
77 }
78
79 static inline void
80 fw3_xt_free_target_udata(struct xtables_target *t)
81 {
82 return;
83 }
84
85 static inline void
86 fw3_xt_merge_target_options(struct xtables_globals *g, struct xtables_target *t)
87 {
88 g->opts = xtables_merge_options(g->opts, t->extra_opts, &t->option_offset);
89 }
90
91
92 /* xtables api addons */
93
94 static inline void
95 xtables_option_mpcall(unsigned int c, char **argv, bool invert,
96 struct xtables_match *m, void *fw)
97 {
98 if (m->parse)
99 m->parse(c - m->option_offset, argv, invert, &m->mflags, fw, &m->m);
100 }
101
102 static inline void
103 xtables_option_mfcall(struct xtables_match *m)
104 {
105 if (m->final_check)
106 m->final_check(m->mflags);
107 }
108
109 static inline void
110 xtables_option_tpcall(unsigned int c, char **argv, bool invert,
111 struct xtables_target *t, void *fw)
112 {
113 if (t->parse)
114 t->parse(c - t->option_offset, argv, invert, &t->tflags, fw, &t->t);
115 }
116
117 static inline void
118 xtables_option_tfcall(struct xtables_target *t)
119 {
120 if (t->final_check)
121 t->final_check(t->tflags);
122 }
123
124 static inline void
125 xtables_rule_matches_free(struct xtables_rule_match **matches)
126 {
127 struct xtables_rule_match *mp, *tmp;
128
129 for (mp = *matches; mp;)
130 {
131 tmp = mp->next;
132
133 if (mp->match->m)
134 {
135 free(mp->match->m);
136 mp->match->m = NULL;
137 }
138
139 if (mp->match == mp->match->next)
140 {
141 free(mp->match);
142 mp->match = NULL;
143 }
144
145 free(mp);
146 mp = tmp;
147 }
148
149 *matches = NULL;
150 }
151
152 static inline int
153 xtables_ipmask_to_cidr(const struct in_addr *mask)
154 {
155 int bits;
156 uint32_t m;
157
158 for (m = ntohl(mask->s_addr), bits = 0; m & 0x80000000; m <<= 1)
159 bits++;
160
161 return bits;
162 }
163
164 static inline int
165 xtables_ip6mask_to_cidr(const struct in6_addr *mask)
166 {
167 int bits = 0;
168 uint32_t a, b, c, d;
169
170 a = ntohl(mask->s6_addr32[0]);
171 b = ntohl(mask->s6_addr32[1]);
172 c = ntohl(mask->s6_addr32[2]);
173 d = ntohl(mask->s6_addr32[3]);
174
175 while (a & 0x80000000U)
176 {
177 a <<= 1;
178 a |= (b >> 31) & 1;
179 b <<= 1;
180 b |= (c >> 31) & 1;
181 c <<= 1;
182 c |= (d >> 31) & 1;
183 d <<= 1;
184
185 bits++;
186 }
187
188 return bits;
189 }
190
191 #endif