Fix compatibility with older libiptc/libip6tc
[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 const char *
23 fw3_xt_get_match_name(struct xtables_match *m)
24 {
25 return m->m->u.user.name;
26 }
27
28 static inline void
29 fw3_xt_set_match_name(struct xtables_match *m)
30 {
31 strcpy(m->m->u.user.name, m->name);
32 }
33
34 static inline bool
35 fw3_xt_has_match_parse(struct xtables_match *m)
36 {
37 return !!m->parse;
38 }
39
40 static inline void
41 fw3_xt_free_match_udata(struct xtables_match *m)
42 {
43 return;
44 }
45
46 static inline void
47 fw3_xt_merge_match_options(struct xtables_globals *g, struct xtables_match *m)
48 {
49 g->opts = xtables_merge_options(g->opts, m->extra_opts, &m->option_offset);
50 }
51
52
53 static inline const char *
54 fw3_xt_get_target_name(struct xtables_target *t)
55 {
56 return t->t->u.user.name;
57 }
58
59 static inline void
60 fw3_xt_set_target_name(struct xtables_target *t, const char *name)
61 {
62 strcpy(t->t->u.user.name, name);
63 }
64
65 static inline bool
66 fw3_xt_has_target_parse(struct xtables_target *t)
67 {
68 return !!t->parse;
69 }
70
71 static inline void
72 fw3_xt_free_target_udata(struct xtables_target *t)
73 {
74 return;
75 }
76
77 static inline void
78 fw3_xt_merge_target_options(struct xtables_globals *g, struct xtables_target *t)
79 {
80 g->opts = xtables_merge_options(g->opts, t->extra_opts, &t->option_offset);
81 }
82
83
84 /* xtables api addons */
85
86 static inline void
87 xtables_option_mpcall(unsigned int c, char **argv, bool invert,
88 struct xtables_match *m, void *fw)
89 {
90 if (m->parse)
91 m->parse(c - m->option_offset, argv, invert, &m->mflags, fw, &m->m);
92 }
93
94 static inline void
95 xtables_option_mfcall(struct xtables_match *m)
96 {
97 if (m->final_check)
98 m->final_check(m->mflags);
99 }
100
101 static inline void
102 xtables_option_tpcall(unsigned int c, char **argv, bool invert,
103 struct xtables_target *t, void *fw)
104 {
105 if (t->parse)
106 t->parse(c - t->option_offset, argv, invert, &t->tflags, fw, &t->t);
107 }
108
109 static inline void
110 xtables_option_tfcall(struct xtables_target *t)
111 {
112 if (t->final_check)
113 t->final_check(t->tflags);
114 }
115
116 static inline void
117 xtables_rule_matches_free(struct xtables_rule_match **matches)
118 {
119 struct xtables_rule_match *mp, *tmp;
120
121 for (mp = *matches; mp;)
122 {
123 tmp = mp->next;
124
125 if (mp->match->m)
126 {
127 free(mp->match->m);
128 mp->match->m = NULL;
129 }
130
131 if (mp->match == mp->match->next)
132 {
133 free(mp->match);
134 mp->match = NULL;
135 }
136
137 free(mp);
138 mp = tmp;
139 }
140
141 *matches = NULL;
142 }
143
144 static inline int
145 xtables_ipmask_to_cidr(const struct in_addr *mask)
146 {
147 int bits;
148 uint32_t m;
149
150 for (m = ntohl(mask->s_addr), bits = 0; m & 0x80000000; m <<= 1)
151 bits++;
152
153 return bits;
154 }
155
156 static inline int
157 xtables_ip6mask_to_cidr(const struct in6_addr *mask)
158 {
159 int bits = 0;
160 uint32_t a, b, c, d;
161
162 a = ntohl(mask->s6_addr32[0]);
163 b = ntohl(mask->s6_addr32[1]);
164 c = ntohl(mask->s6_addr32[2]);
165 d = ntohl(mask->s6_addr32[3]);
166
167 while (a & 0x80000000U)
168 {
169 a <<= 1;
170 a |= (b >> 31) & 1;
171 b <<= 1;
172 b |= (c >> 31) & 1;
173 c <<= 1;
174 c |= (d >> 31) & 1;
175 d <<= 1;
176
177 bits++;
178 }
179
180 return bits;
181 }
182
183 #endif