| Martin Josefsson | 0f9b8b1 | 2004-12-18 17:18:49 +0000 | [diff] [blame] | 1 | /* Library which manipulates firewall rules. Version $Revision$ */ |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2 | |
| 3 | /* Architecture of firewall rules is as follows: |
| 4 | * |
| 5 | * Chains go INPUT, FORWARD, OUTPUT then user chains. |
| 6 | * Each user chain starts with an ERROR node. |
| 7 | * Every chain ends with an unconditional jump: a RETURN for user chains, |
| 8 | * and a POLICY for built-ins. |
| 9 | */ |
| 10 | |
| Harald Welte | 3ea8f40 | 2003-06-23 18:25:59 +0000 | [diff] [blame] | 11 | /* (C) 1999 Paul ``Rusty'' Russell - Placed under the GNU GPL (See |
| 12 | * COPYING for details). |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 13 | * (C) 2000-2004 by the Netfilter Core Team <coreteam@netfilter.org> |
| Harald Welte | 3ea8f40 | 2003-06-23 18:25:59 +0000 | [diff] [blame] | 14 | * |
| Harald Welte | fbc8523 | 2003-06-24 17:37:21 +0000 | [diff] [blame] | 15 | * 2003-Jun-20: Harald Welte <laforge@netfilter.org>: |
| Harald Welte | 3ea8f40 | 2003-06-23 18:25:59 +0000 | [diff] [blame] | 16 | * - Reimplementation of chain cache to use offsets instead of entries |
| Harald Welte | fbc8523 | 2003-06-24 17:37:21 +0000 | [diff] [blame] | 17 | * 2003-Jun-23: Harald Welte <laforge@netfilter.org>: |
| Harald Welte | 0113fe7 | 2004-01-06 19:04:02 +0000 | [diff] [blame] | 18 | * - performance optimization, sponsored by Astaro AG (http://www.astaro.com/) |
| Harald Welte | fbc8523 | 2003-06-24 17:37:21 +0000 | [diff] [blame] | 19 | * don't rebuild the chain cache after every operation, instead fix it |
| 20 | * up after a ruleset change. |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 21 | * 2004-Aug-18: Harald Welte <laforge@netfilter.org>: |
| 22 | * - futher performance work: total reimplementation of libiptc. |
| 23 | * - libiptc now has a real internal (linked-list) represntation of the |
| 24 | * ruleset and a parser/compiler from/to this internal representation |
| 25 | * - again sponsored by Astaro AG (http://www.astaro.com/) |
| Harald Welte | 3ea8f40 | 2003-06-23 18:25:59 +0000 | [diff] [blame] | 26 | */ |
| Harald Welte | 15920d1 | 2004-05-16 09:05:07 +0000 | [diff] [blame] | 27 | #include <sys/types.h> |
| 28 | #include <sys/socket.h> |
| Stephane Ouellette | 7cd0028 | 2004-05-14 08:21:06 +0000 | [diff] [blame] | 29 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 30 | #include "linux_list.h" |
| 31 | |
| 32 | //#define IPTC_DEBUG2 1 |
| 33 | |
| 34 | #ifdef IPTC_DEBUG2 |
| 35 | #include <fcntl.h> |
| 36 | #define DEBUGP(x, args...) fprintf(stderr, "%s: " x, __FUNCTION__, ## args) |
| 37 | #define DEBUGP_C(x, args...) fprintf(stderr, x, ## args) |
| 38 | #else |
| 39 | #define DEBUGP(x, args...) |
| 40 | #define DEBUGP_C(x, args...) |
| 41 | #endif |
| 42 | |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 43 | #ifndef IPT_LIB_DIR |
| 44 | #define IPT_LIB_DIR "/usr/local/lib/iptables" |
| 45 | #endif |
| 46 | |
| 47 | static int sockfd = -1; |
| Derrik Pates | 664c0a3 | 2005-02-01 13:28:14 +0000 | [diff] [blame^] | 48 | static int sockfd_use = 0; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 49 | static void *iptc_fn = NULL; |
| 50 | |
| 51 | static const char *hooknames[] |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 52 | = { [HOOK_PRE_ROUTING] "PREROUTING", |
| 53 | [HOOK_LOCAL_IN] "INPUT", |
| 54 | [HOOK_FORWARD] "FORWARD", |
| 55 | [HOOK_LOCAL_OUT] "OUTPUT", |
| Rusty Russell | 10758b7 | 2000-09-14 07:37:33 +0000 | [diff] [blame] | 56 | [HOOK_POST_ROUTING] "POSTROUTING", |
| 57 | #ifdef HOOK_DROPPING |
| 58 | [HOOK_DROPPING] "DROPPING" |
| 59 | #endif |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 60 | }; |
| 61 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 62 | /* Convenience structures */ |
| 63 | struct ipt_error_target |
| 64 | { |
| 65 | STRUCT_ENTRY_TARGET t; |
| 66 | char error[TABLE_MAXNAMELEN]; |
| 67 | }; |
| 68 | |
| 69 | struct chain_head; |
| 70 | struct rule_head; |
| 71 | |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 72 | struct counter_map |
| 73 | { |
| 74 | enum { |
| 75 | COUNTER_MAP_NOMAP, |
| 76 | COUNTER_MAP_NORMAL_MAP, |
| Harald Welte | 1cef74d | 2001-01-05 15:22:59 +0000 | [diff] [blame] | 77 | COUNTER_MAP_ZEROED, |
| 78 | COUNTER_MAP_SET |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 79 | } maptype; |
| 80 | unsigned int mappos; |
| 81 | }; |
| 82 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 83 | enum iptcc_rule_type { |
| 84 | IPTCC_R_STANDARD, /* standard target (ACCEPT, ...) */ |
| 85 | IPTCC_R_MODULE, /* extension module (SNAT, ...) */ |
| 86 | IPTCC_R_FALLTHROUGH, /* fallthrough rule */ |
| 87 | IPTCC_R_JUMP, /* jump to other chain */ |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 88 | }; |
| 89 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 90 | struct rule_head |
| Rusty Russell | 30fd6e5 | 2000-04-23 09:16:06 +0000 | [diff] [blame] | 91 | { |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 92 | struct list_head list; |
| 93 | struct chain_head *chain; |
| 94 | struct counter_map counter_map; |
| 95 | |
| 96 | unsigned int index; /* index (needed for counter_map) */ |
| 97 | unsigned int offset; /* offset in rule blob */ |
| 98 | |
| 99 | enum iptcc_rule_type type; |
| 100 | struct chain_head *jump; /* jump target, if IPTCC_R_JUMP */ |
| 101 | |
| 102 | unsigned int size; /* size of entry data */ |
| 103 | STRUCT_ENTRY entry[0]; |
| 104 | }; |
| 105 | |
| 106 | struct chain_head |
| 107 | { |
| 108 | struct list_head list; |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 109 | char name[TABLE_MAXNAMELEN]; |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 110 | unsigned int hooknum; /* hook number+1 if builtin */ |
| 111 | unsigned int references; /* how many jumps reference us */ |
| 112 | int verdict; /* verdict if builtin */ |
| 113 | |
| 114 | STRUCT_COUNTERS counters; /* per-chain counters */ |
| 115 | struct counter_map counter_map; |
| 116 | |
| 117 | unsigned int num_rules; /* number of rules in list */ |
| 118 | struct list_head rules; /* list of rules */ |
| 119 | |
| 120 | unsigned int index; /* index (needed for jump resolval) */ |
| 121 | unsigned int head_offset; /* offset in rule blob */ |
| 122 | unsigned int foot_index; /* index (needed for counter_map) */ |
| 123 | unsigned int foot_offset; /* offset in rule blob */ |
| Rusty Russell | 30fd6e5 | 2000-04-23 09:16:06 +0000 | [diff] [blame] | 124 | }; |
| 125 | |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 126 | STRUCT_TC_HANDLE |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 127 | { |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 128 | int changed; /* Have changes been made? */ |
| 129 | |
| 130 | struct list_head chains; |
| 131 | |
| 132 | struct chain_head *chain_iterator_cur; |
| 133 | struct rule_head *rule_iterator_cur; |
| 134 | |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 135 | STRUCT_GETINFO info; |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 136 | STRUCT_GET_ENTRIES *entries; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 137 | }; |
| 138 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 139 | /* allocate a new chain head for the cache */ |
| 140 | static struct chain_head *iptcc_alloc_chain_head(const char *name, int hooknum) |
| 141 | { |
| 142 | struct chain_head *c = malloc(sizeof(*c)); |
| 143 | if (!c) |
| 144 | return NULL; |
| 145 | memset(c, 0, sizeof(*c)); |
| 146 | |
| 147 | strncpy(c->name, name, TABLE_MAXNAMELEN); |
| 148 | c->hooknum = hooknum; |
| 149 | INIT_LIST_HEAD(&c->rules); |
| 150 | |
| 151 | return c; |
| 152 | } |
| 153 | |
| 154 | /* allocate and initialize a new rule for the cache */ |
| 155 | static struct rule_head *iptcc_alloc_rule(struct chain_head *c, unsigned int size) |
| 156 | { |
| 157 | struct rule_head *r = malloc(sizeof(*r)+size); |
| 158 | if (!r) |
| 159 | return NULL; |
| 160 | memset(r, 0, sizeof(*r)); |
| 161 | |
| 162 | r->chain = c; |
| 163 | r->size = size; |
| 164 | |
| 165 | return r; |
| 166 | } |
| 167 | |
| 168 | /* notify us that the ruleset has been modified by the user */ |
| Rusty Russell | 175f641 | 2000-03-24 09:32:20 +0000 | [diff] [blame] | 169 | static void |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 170 | set_changed(TC_HANDLE_T h) |
| Rusty Russell | 175f641 | 2000-03-24 09:32:20 +0000 | [diff] [blame] | 171 | { |
| Rusty Russell | 175f641 | 2000-03-24 09:32:20 +0000 | [diff] [blame] | 172 | h->changed = 1; |
| 173 | } |
| 174 | |
| Harald Welte | 380ba5f | 2002-02-13 16:19:55 +0000 | [diff] [blame] | 175 | #ifdef IPTC_DEBUG |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 176 | static void do_check(TC_HANDLE_T h, unsigned int line); |
| Rusty Russell | 849779c | 2000-04-23 15:51:51 +0000 | [diff] [blame] | 177 | #define CHECK(h) do { if (!getenv("IPTC_NO_CHECK")) do_check((h), __LINE__); } while(0) |
| Rusty Russell | 30fd6e5 | 2000-04-23 09:16:06 +0000 | [diff] [blame] | 178 | #else |
| 179 | #define CHECK(h) |
| 180 | #endif |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 181 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 182 | |
| 183 | /********************************************************************** |
| 184 | * iptc blob utility functions (iptcb_*) |
| 185 | **********************************************************************/ |
| 186 | |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 187 | static inline int |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 188 | iptcb_get_number(const STRUCT_ENTRY *i, |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 189 | const STRUCT_ENTRY *seek, |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 190 | unsigned int *pos) |
| 191 | { |
| 192 | if (i == seek) |
| 193 | return 1; |
| 194 | (*pos)++; |
| 195 | return 0; |
| 196 | } |
| 197 | |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 198 | static inline int |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 199 | iptcb_get_entry_n(STRUCT_ENTRY *i, |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 200 | unsigned int number, |
| 201 | unsigned int *pos, |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 202 | STRUCT_ENTRY **pe) |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 203 | { |
| 204 | if (*pos == number) { |
| 205 | *pe = i; |
| 206 | return 1; |
| 207 | } |
| 208 | (*pos)++; |
| 209 | return 0; |
| 210 | } |
| 211 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 212 | static inline STRUCT_ENTRY * |
| 213 | iptcb_get_entry(TC_HANDLE_T h, unsigned int offset) |
| 214 | { |
| 215 | return (STRUCT_ENTRY *)((char *)h->entries->entrytable + offset); |
| 216 | } |
| 217 | |
| 218 | static unsigned int |
| 219 | iptcb_entry2index(const TC_HANDLE_T h, const STRUCT_ENTRY *seek) |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 220 | { |
| 221 | unsigned int pos = 0; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 222 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 223 | if (ENTRY_ITERATE(h->entries->entrytable, h->entries->size, |
| 224 | iptcb_get_number, seek, &pos) == 0) { |
| 225 | fprintf(stderr, "ERROR: offset %u not an entry!\n", |
| 226 | (unsigned int)((char *)seek - (char *)h->entries->entrytable)); |
| 227 | abort(); |
| 228 | } |
| 229 | return pos; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 230 | } |
| 231 | |
| Harald Welte | 0113fe7 | 2004-01-06 19:04:02 +0000 | [diff] [blame] | 232 | static inline STRUCT_ENTRY * |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 233 | iptcb_offset2entry(TC_HANDLE_T h, unsigned int offset) |
| Harald Welte | 0113fe7 | 2004-01-06 19:04:02 +0000 | [diff] [blame] | 234 | { |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 235 | return (STRUCT_ENTRY *) ((void *)h->entries->entrytable+offset); |
| Harald Welte | 0113fe7 | 2004-01-06 19:04:02 +0000 | [diff] [blame] | 236 | } |
| 237 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 238 | |
| Harald Welte | 0113fe7 | 2004-01-06 19:04:02 +0000 | [diff] [blame] | 239 | static inline unsigned long |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 240 | iptcb_entry2offset(const TC_HANDLE_T h, const STRUCT_ENTRY *e) |
| Harald Welte | 0113fe7 | 2004-01-06 19:04:02 +0000 | [diff] [blame] | 241 | { |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 242 | return (void *)e - (void *)h->entries->entrytable; |
| Harald Welte | 3ea8f40 | 2003-06-23 18:25:59 +0000 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | static inline unsigned int |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 246 | iptcb_offset2index(const TC_HANDLE_T h, unsigned int offset) |
| Harald Welte | 3ea8f40 | 2003-06-23 18:25:59 +0000 | [diff] [blame] | 247 | { |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 248 | return iptcb_entry2index(h, iptcb_offset2entry(h, offset)); |
| 249 | } |
| 250 | |
| 251 | /* Returns 0 if not hook entry, else hooknumber + 1 */ |
| 252 | static inline unsigned int |
| 253 | iptcb_ent_is_hook_entry(STRUCT_ENTRY *e, TC_HANDLE_T h) |
| 254 | { |
| 255 | unsigned int i; |
| 256 | |
| 257 | for (i = 0; i < NUMHOOKS; i++) { |
| 258 | if ((h->info.valid_hooks & (1 << i)) |
| 259 | && iptcb_get_entry(h, h->info.hook_entry[i]) == e) |
| 260 | return i+1; |
| 261 | } |
| 262 | return 0; |
| Harald Welte | 3ea8f40 | 2003-06-23 18:25:59 +0000 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 266 | /********************************************************************** |
| 267 | * iptc cache utility functions (iptcc_*) |
| 268 | **********************************************************************/ |
| Harald Welte | 0113fe7 | 2004-01-06 19:04:02 +0000 | [diff] [blame] | 269 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 270 | /* Is the given chain builtin (1) or user-defined (0) */ |
| 271 | static unsigned int iptcc_is_builtin(struct chain_head *c) |
| 272 | { |
| 273 | return (c->hooknum ? 1 : 0); |
| 274 | } |
| 275 | |
| 276 | /* Get a specific rule within a chain */ |
| 277 | static struct rule_head *iptcc_get_rule_num(struct chain_head *c, |
| 278 | unsigned int rulenum) |
| 279 | { |
| 280 | struct rule_head *r; |
| 281 | unsigned int num = 0; |
| 282 | |
| 283 | list_for_each_entry(r, &c->rules, list) { |
| 284 | num++; |
| 285 | if (num == rulenum) |
| 286 | return r; |
| 287 | } |
| 288 | return NULL; |
| 289 | } |
| 290 | |
| Martin Josefsson | a5616dc | 2004-10-24 22:27:31 +0000 | [diff] [blame] | 291 | /* Get a specific rule within a chain backwards */ |
| 292 | static struct rule_head *iptcc_get_rule_num_reverse(struct chain_head *c, |
| 293 | unsigned int rulenum) |
| 294 | { |
| 295 | struct rule_head *r; |
| 296 | unsigned int num = 0; |
| 297 | |
| 298 | list_for_each_entry_reverse(r, &c->rules, list) { |
| 299 | num++; |
| 300 | if (num == rulenum) |
| 301 | return r; |
| 302 | } |
| 303 | return NULL; |
| 304 | } |
| 305 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 306 | /* Returns chain head if found, otherwise NULL. */ |
| 307 | static struct chain_head * |
| 308 | iptcc_find_chain_by_offset(TC_HANDLE_T handle, unsigned int offset) |
| 309 | { |
| 310 | struct list_head *pos; |
| 311 | |
| 312 | if (list_empty(&handle->chains)) |
| 313 | return NULL; |
| 314 | |
| 315 | list_for_each(pos, &handle->chains) { |
| 316 | struct chain_head *c = list_entry(pos, struct chain_head, list); |
| 317 | if (offset >= c->head_offset && offset <= c->foot_offset) |
| 318 | return c; |
| Harald Welte | 0113fe7 | 2004-01-06 19:04:02 +0000 | [diff] [blame] | 319 | } |
| 320 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 321 | return NULL; |
| Harald Welte | 0113fe7 | 2004-01-06 19:04:02 +0000 | [diff] [blame] | 322 | } |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 323 | /* Returns chain head if found, otherwise NULL. */ |
| 324 | static struct chain_head * |
| 325 | iptcc_find_label(const char *name, TC_HANDLE_T handle) |
| 326 | { |
| 327 | struct list_head *pos; |
| 328 | |
| 329 | if (list_empty(&handle->chains)) |
| 330 | return NULL; |
| 331 | |
| 332 | list_for_each(pos, &handle->chains) { |
| 333 | struct chain_head *c = list_entry(pos, struct chain_head, list); |
| 334 | if (!strcmp(c->name, name)) |
| 335 | return c; |
| 336 | } |
| 337 | |
| 338 | return NULL; |
| 339 | } |
| 340 | |
| 341 | /* called when rule is to be removed from cache */ |
| 342 | static void iptcc_delete_rule(struct rule_head *r) |
| 343 | { |
| 344 | DEBUGP("deleting rule %p (offset %u)\n", r, r->offset); |
| 345 | /* clean up reference count of called chain */ |
| 346 | if (r->type == IPTCC_R_JUMP |
| 347 | && r->jump) |
| 348 | r->jump->references--; |
| 349 | |
| 350 | list_del(&r->list); |
| 351 | free(r); |
| 352 | } |
| 353 | |
| 354 | |
| 355 | /********************************************************************** |
| 356 | * RULESET PARSER (blob -> cache) |
| 357 | **********************************************************************/ |
| 358 | |
| 359 | static int alphasort(const void *a, const void *b) |
| 360 | { |
| 361 | return strcmp(((struct chain_head *)a)->name, |
| 362 | ((struct chain_head *)b)->name); |
| 363 | } |
| 364 | |
| 365 | /* Delete policy rule of previous chain, since cache doesn't contain |
| 366 | * chain policy rules. |
| 367 | * WARNING: This function has ugly design and relies on a lot of context, only |
| 368 | * to be called from specific places within the parser */ |
| 369 | static int __iptcc_p_del_policy(TC_HANDLE_T h, unsigned int num) |
| 370 | { |
| 371 | if (h->chain_iterator_cur) { |
| 372 | /* policy rule is last rule */ |
| 373 | struct rule_head *pr = (struct rule_head *) |
| 374 | h->chain_iterator_cur->rules.prev; |
| 375 | |
| 376 | /* save verdict */ |
| 377 | h->chain_iterator_cur->verdict = |
| 378 | *(int *)GET_TARGET(pr->entry)->data; |
| 379 | |
| 380 | /* save counter and counter_map information */ |
| 381 | h->chain_iterator_cur->counter_map.maptype = |
| 382 | COUNTER_MAP_NORMAL_MAP; |
| 383 | h->chain_iterator_cur->counter_map.mappos = num-1; |
| 384 | memcpy(&h->chain_iterator_cur->counters, &pr->entry->counters, |
| 385 | sizeof(h->chain_iterator_cur->counters)); |
| 386 | |
| 387 | /* foot_offset points to verdict rule */ |
| 388 | h->chain_iterator_cur->foot_index = num; |
| 389 | h->chain_iterator_cur->foot_offset = pr->offset; |
| 390 | |
| 391 | /* delete rule from cache */ |
| 392 | iptcc_delete_rule(pr); |
| Martin Josefsson | 8d1b38a | 2004-09-22 21:00:19 +0000 | [diff] [blame] | 393 | h->chain_iterator_cur->num_rules--; |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 394 | |
| 395 | return 1; |
| 396 | } |
| 397 | return 0; |
| 398 | } |
| 399 | |
| 400 | /* Another ugly helper function split out of cache_add_entry to make it less |
| 401 | * spaghetti code */ |
| 402 | static void __iptcc_p_add_chain(TC_HANDLE_T h, struct chain_head *c, |
| 403 | unsigned int offset, unsigned int *num) |
| 404 | { |
| 405 | __iptcc_p_del_policy(h, *num); |
| 406 | |
| 407 | c->head_offset = offset; |
| 408 | c->index = *num; |
| 409 | |
| 410 | list_add_tail(&c->list, &h->chains); |
| 411 | h->chain_iterator_cur = c; |
| 412 | } |
| 413 | |
| 414 | /* main parser function: add an entry from the blob to the cache */ |
| 415 | static int cache_add_entry(STRUCT_ENTRY *e, |
| 416 | TC_HANDLE_T h, |
| 417 | STRUCT_ENTRY **prev, |
| 418 | unsigned int *num) |
| 419 | { |
| 420 | unsigned int builtin; |
| 421 | unsigned int offset = (char *)e - (char *)h->entries->entrytable; |
| 422 | |
| 423 | DEBUGP("entering..."); |
| 424 | |
| 425 | /* Last entry ("policy rule"). End it.*/ |
| 426 | if (iptcb_entry2offset(h,e) + e->next_offset == h->entries->size) { |
| 427 | /* This is the ERROR node at the end of the chain */ |
| 428 | DEBUGP_C("%u:%u: end of table:\n", *num, offset); |
| 429 | |
| 430 | __iptcc_p_del_policy(h, *num); |
| 431 | |
| 432 | h->chain_iterator_cur = NULL; |
| 433 | goto out_inc; |
| 434 | } |
| 435 | |
| 436 | /* We know this is the start of a new chain if it's an ERROR |
| 437 | * target, or a hook entry point */ |
| 438 | |
| 439 | if (strcmp(GET_TARGET(e)->u.user.name, ERROR_TARGET) == 0) { |
| 440 | struct chain_head *c = |
| 441 | iptcc_alloc_chain_head((const char *)GET_TARGET(e)->data, 0); |
| 442 | DEBUGP_C("%u:%u:new userdefined chain %s: %p\n", *num, offset, |
| 443 | (char *)c->name, c); |
| 444 | if (!c) { |
| 445 | errno = -ENOMEM; |
| 446 | return -1; |
| 447 | } |
| 448 | |
| 449 | __iptcc_p_add_chain(h, c, offset, num); |
| 450 | |
| 451 | } else if ((builtin = iptcb_ent_is_hook_entry(e, h)) != 0) { |
| 452 | struct chain_head *c = |
| 453 | iptcc_alloc_chain_head((char *)hooknames[builtin-1], |
| 454 | builtin); |
| 455 | DEBUGP_C("%u:%u new builtin chain: %p (rules=%p)\n", |
| 456 | *num, offset, c, &c->rules); |
| 457 | if (!c) { |
| 458 | errno = -ENOMEM; |
| 459 | return -1; |
| 460 | } |
| 461 | |
| 462 | c->hooknum = builtin; |
| 463 | |
| 464 | __iptcc_p_add_chain(h, c, offset, num); |
| 465 | |
| 466 | /* FIXME: this is ugly. */ |
| 467 | goto new_rule; |
| 468 | } else { |
| 469 | /* has to be normal rule */ |
| 470 | struct rule_head *r; |
| 471 | new_rule: |
| 472 | |
| 473 | if (!(r = iptcc_alloc_rule(h->chain_iterator_cur, |
| 474 | e->next_offset))) { |
| 475 | errno = ENOMEM; |
| 476 | return -1; |
| 477 | } |
| 478 | DEBUGP_C("%u:%u normal rule: %p: ", *num, offset, r); |
| 479 | |
| 480 | r->index = *num; |
| 481 | r->offset = offset; |
| 482 | memcpy(r->entry, e, e->next_offset); |
| 483 | r->counter_map.maptype = COUNTER_MAP_NORMAL_MAP; |
| 484 | r->counter_map.mappos = r->index; |
| 485 | |
| 486 | /* handling of jumps, etc. */ |
| 487 | if (!strcmp(GET_TARGET(e)->u.user.name, STANDARD_TARGET)) { |
| 488 | STRUCT_STANDARD_TARGET *t; |
| 489 | |
| 490 | t = (STRUCT_STANDARD_TARGET *)GET_TARGET(e); |
| 491 | if (t->target.u.target_size |
| 492 | != ALIGN(sizeof(STRUCT_STANDARD_TARGET))) { |
| 493 | errno = EINVAL; |
| 494 | return -1; |
| 495 | } |
| 496 | |
| 497 | if (t->verdict < 0) { |
| 498 | DEBUGP_C("standard, verdict=%d\n", t->verdict); |
| 499 | r->type = IPTCC_R_STANDARD; |
| 500 | } else if (t->verdict == r->offset+e->next_offset) { |
| 501 | DEBUGP_C("fallthrough\n"); |
| 502 | r->type = IPTCC_R_FALLTHROUGH; |
| 503 | } else { |
| 504 | DEBUGP_C("jump, target=%u\n", t->verdict); |
| 505 | r->type = IPTCC_R_JUMP; |
| 506 | /* Jump target fixup has to be deferred |
| 507 | * until second pass, since we migh not |
| 508 | * yet have parsed the target */ |
| 509 | } |
| Martin Josefsson | 52c3802 | 2004-09-22 19:39:40 +0000 | [diff] [blame] | 510 | } else { |
| 511 | DEBUGP_C("module, target=%s\n", GET_TARGET(e)->u.user.name); |
| 512 | r->type = IPTCC_R_MODULE; |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 513 | } |
| 514 | |
| 515 | list_add_tail(&r->list, &h->chain_iterator_cur->rules); |
| Martin Josefsson | 8d1b38a | 2004-09-22 21:00:19 +0000 | [diff] [blame] | 516 | h->chain_iterator_cur->num_rules++; |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 517 | } |
| 518 | out_inc: |
| 519 | (*num)++; |
| 520 | return 0; |
| 521 | } |
| 522 | |
| 523 | |
| 524 | /* parse an iptables blob into it's pieces */ |
| 525 | static int parse_table(TC_HANDLE_T h) |
| 526 | { |
| 527 | STRUCT_ENTRY *prev; |
| 528 | unsigned int num = 0; |
| 529 | struct chain_head *c; |
| 530 | |
| 531 | /* First pass: over ruleset blob */ |
| 532 | ENTRY_ITERATE(h->entries->entrytable, h->entries->size, |
| 533 | cache_add_entry, h, &prev, &num); |
| 534 | |
| 535 | /* Second pass: fixup parsed data from first pass */ |
| 536 | list_for_each_entry(c, &h->chains, list) { |
| 537 | struct rule_head *r; |
| 538 | list_for_each_entry(r, &c->rules, list) { |
| 539 | struct chain_head *c; |
| 540 | STRUCT_STANDARD_TARGET *t; |
| 541 | |
| 542 | if (r->type != IPTCC_R_JUMP) |
| 543 | continue; |
| 544 | |
| 545 | t = (STRUCT_STANDARD_TARGET *)GET_TARGET(r->entry); |
| 546 | c = iptcc_find_chain_by_offset(h, t->verdict); |
| 547 | if (!c) |
| 548 | return -1; |
| 549 | r->jump = c; |
| 550 | c->references++; |
| 551 | } |
| 552 | } |
| 553 | |
| 554 | /* FIXME: sort chains */ |
| 555 | |
| 556 | return 1; |
| 557 | } |
| 558 | |
| 559 | |
| 560 | /********************************************************************** |
| 561 | * RULESET COMPILATION (cache -> blob) |
| 562 | **********************************************************************/ |
| 563 | |
| 564 | /* Convenience structures */ |
| 565 | struct iptcb_chain_start{ |
| 566 | STRUCT_ENTRY e; |
| 567 | struct ipt_error_target name; |
| 568 | }; |
| 569 | #define IPTCB_CHAIN_START_SIZE (sizeof(STRUCT_ENTRY) + \ |
| 570 | ALIGN(sizeof(struct ipt_error_target))) |
| 571 | |
| 572 | struct iptcb_chain_foot { |
| 573 | STRUCT_ENTRY e; |
| 574 | STRUCT_STANDARD_TARGET target; |
| 575 | }; |
| 576 | #define IPTCB_CHAIN_FOOT_SIZE (sizeof(STRUCT_ENTRY) + \ |
| 577 | ALIGN(sizeof(STRUCT_STANDARD_TARGET))) |
| 578 | |
| 579 | struct iptcb_chain_error { |
| 580 | STRUCT_ENTRY entry; |
| 581 | struct ipt_error_target target; |
| 582 | }; |
| 583 | #define IPTCB_CHAIN_ERROR_SIZE (sizeof(STRUCT_ENTRY) + \ |
| 584 | ALIGN(sizeof(struct ipt_error_target))) |
| 585 | |
| 586 | |
| 587 | |
| 588 | /* compile rule from cache into blob */ |
| 589 | static inline int iptcc_compile_rule (TC_HANDLE_T h, STRUCT_REPLACE *repl, struct rule_head *r) |
| 590 | { |
| 591 | /* handle jumps */ |
| 592 | if (r->type == IPTCC_R_JUMP) { |
| 593 | STRUCT_STANDARD_TARGET *t; |
| 594 | t = (STRUCT_STANDARD_TARGET *)GET_TARGET(r->entry); |
| 595 | /* memset for memcmp convenience on delete/replace */ |
| 596 | memset(t->target.u.user.name, 0, FUNCTION_MAXNAMELEN); |
| 597 | strcpy(t->target.u.user.name, STANDARD_TARGET); |
| 598 | /* Jumps can only happen to builtin chains, so we |
| 599 | * can safely assume that they always have a header */ |
| 600 | t->verdict = r->jump->head_offset + IPTCB_CHAIN_START_SIZE; |
| 601 | } else if (r->type == IPTCC_R_FALLTHROUGH) { |
| 602 | STRUCT_STANDARD_TARGET *t; |
| 603 | t = (STRUCT_STANDARD_TARGET *)GET_TARGET(r->entry); |
| 604 | t->verdict = r->offset + r->size; |
| 605 | } |
| 606 | |
| 607 | /* copy entry from cache to blob */ |
| 608 | memcpy((char *)repl->entries+r->offset, r->entry, r->size); |
| 609 | |
| 610 | return 1; |
| 611 | } |
| 612 | |
| 613 | /* compile chain from cache into blob */ |
| 614 | static int iptcc_compile_chain(TC_HANDLE_T h, STRUCT_REPLACE *repl, struct chain_head *c) |
| 615 | { |
| 616 | int ret; |
| 617 | struct rule_head *r; |
| 618 | struct iptcb_chain_start *head; |
| 619 | struct iptcb_chain_foot *foot; |
| 620 | |
| 621 | /* only user-defined chains have heaer */ |
| 622 | if (!iptcc_is_builtin(c)) { |
| 623 | /* put chain header in place */ |
| 624 | head = (void *)repl->entries + c->head_offset; |
| 625 | head->e.target_offset = sizeof(STRUCT_ENTRY); |
| 626 | head->e.next_offset = IPTCB_CHAIN_START_SIZE; |
| 627 | strcpy(head->name.t.u.user.name, ERROR_TARGET); |
| 628 | head->name.t.u.target_size = |
| 629 | ALIGN(sizeof(struct ipt_error_target)); |
| 630 | strcpy(head->name.error, c->name); |
| 631 | } else { |
| 632 | repl->hook_entry[c->hooknum-1] = c->head_offset; |
| 633 | repl->underflow[c->hooknum-1] = c->foot_offset; |
| 634 | } |
| 635 | |
| 636 | /* iterate over rules */ |
| 637 | list_for_each_entry(r, &c->rules, list) { |
| 638 | ret = iptcc_compile_rule(h, repl, r); |
| 639 | if (ret < 0) |
| 640 | return ret; |
| 641 | } |
| 642 | |
| 643 | /* put chain footer in place */ |
| 644 | foot = (void *)repl->entries + c->foot_offset; |
| 645 | foot->e.target_offset = sizeof(STRUCT_ENTRY); |
| 646 | foot->e.next_offset = IPTCB_CHAIN_FOOT_SIZE; |
| 647 | strcpy(foot->target.target.u.user.name, STANDARD_TARGET); |
| 648 | foot->target.target.u.target_size = |
| 649 | ALIGN(sizeof(STRUCT_STANDARD_TARGET)); |
| 650 | /* builtin targets have verdict, others return */ |
| 651 | if (iptcc_is_builtin(c)) |
| 652 | foot->target.verdict = c->verdict; |
| 653 | else |
| 654 | foot->target.verdict = RETURN; |
| 655 | /* set policy-counters */ |
| 656 | memcpy(&foot->e.counters, &c->counters, sizeof(STRUCT_COUNTERS)); |
| 657 | |
| 658 | return 0; |
| 659 | } |
| 660 | |
| 661 | /* calculate offset and number for every rule in the cache */ |
| 662 | static int iptcc_compile_chain_offsets(TC_HANDLE_T h, struct chain_head *c, |
| 663 | int *offset, int *num) |
| 664 | { |
| 665 | struct rule_head *r; |
| 666 | |
| 667 | c->head_offset = *offset; |
| 668 | DEBUGP("%s: chain_head %u, offset=%u\n", c->name, *num, *offset); |
| 669 | |
| 670 | if (!iptcc_is_builtin(c)) { |
| 671 | /* Chain has header */ |
| 672 | *offset += sizeof(STRUCT_ENTRY) |
| 673 | + ALIGN(sizeof(struct ipt_error_target)); |
| 674 | (*num)++; |
| 675 | } |
| 676 | |
| 677 | list_for_each_entry(r, &c->rules, list) { |
| 678 | DEBUGP("rule %u, offset=%u, index=%u\n", *num, *offset, *num); |
| 679 | r->offset = *offset; |
| 680 | r->index = *num; |
| 681 | *offset += r->size; |
| 682 | (*num)++; |
| 683 | } |
| 684 | |
| 685 | DEBUGP("%s; chain_foot %u, offset=%u, index=%u\n", c->name, *num, |
| 686 | *offset, *num); |
| 687 | c->foot_offset = *offset; |
| 688 | c->foot_index = *num; |
| 689 | *offset += sizeof(STRUCT_ENTRY) |
| 690 | + ALIGN(sizeof(STRUCT_STANDARD_TARGET)); |
| 691 | (*num)++; |
| 692 | |
| 693 | return 1; |
| 694 | } |
| 695 | |
| 696 | /* put the pieces back together again */ |
| 697 | static int iptcc_compile_table_prep(TC_HANDLE_T h, unsigned int *size) |
| 698 | { |
| 699 | struct chain_head *c; |
| 700 | unsigned int offset = 0, num = 0; |
| 701 | int ret = 0; |
| 702 | |
| 703 | /* First pass: calculate offset for every rule */ |
| 704 | list_for_each_entry(c, &h->chains, list) { |
| 705 | ret = iptcc_compile_chain_offsets(h, c, &offset, &num); |
| 706 | if (ret < 0) |
| 707 | return ret; |
| 708 | } |
| 709 | |
| 710 | /* Append one error rule at end of chain */ |
| 711 | num++; |
| 712 | offset += sizeof(STRUCT_ENTRY) |
| 713 | + ALIGN(sizeof(struct ipt_error_target)); |
| 714 | |
| 715 | /* ruleset size is now in offset */ |
| 716 | *size = offset; |
| 717 | return num; |
| 718 | } |
| 719 | |
| 720 | static int iptcc_compile_table(TC_HANDLE_T h, STRUCT_REPLACE *repl) |
| 721 | { |
| 722 | struct chain_head *c; |
| 723 | struct iptcb_chain_error *error; |
| 724 | |
| 725 | /* Second pass: copy from cache to offsets, fill in jumps */ |
| 726 | list_for_each_entry(c, &h->chains, list) { |
| 727 | int ret = iptcc_compile_chain(h, repl, c); |
| 728 | if (ret < 0) |
| 729 | return ret; |
| 730 | } |
| 731 | |
| 732 | /* Append error rule at end of chain */ |
| 733 | error = (void *)repl->entries + repl->size - IPTCB_CHAIN_ERROR_SIZE; |
| 734 | error->entry.target_offset = sizeof(STRUCT_ENTRY); |
| 735 | error->entry.next_offset = IPTCB_CHAIN_ERROR_SIZE; |
| 736 | error->target.t.u.user.target_size = |
| 737 | ALIGN(sizeof(struct ipt_error_target)); |
| 738 | strcpy((char *)&error->target.t.u.user.name, ERROR_TARGET); |
| 739 | strcpy((char *)&error->target.error, "ERROR"); |
| 740 | |
| 741 | return 1; |
| 742 | } |
| 743 | |
| 744 | /********************************************************************** |
| 745 | * EXTERNAL API (operates on cache only) |
| 746 | **********************************************************************/ |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 747 | |
| 748 | /* Allocate handle of given size */ |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 749 | static TC_HANDLE_T |
| Harald Welte | 0113fe7 | 2004-01-06 19:04:02 +0000 | [diff] [blame] | 750 | alloc_handle(const char *tablename, unsigned int size, unsigned int num_rules) |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 751 | { |
| 752 | size_t len; |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 753 | TC_HANDLE_T h; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 754 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 755 | len = sizeof(STRUCT_TC_HANDLE) + size; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 756 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 757 | h = malloc(sizeof(STRUCT_TC_HANDLE)); |
| 758 | if (!h) { |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 759 | errno = ENOMEM; |
| 760 | return NULL; |
| 761 | } |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 762 | memset(h, 0, sizeof(*h)); |
| 763 | INIT_LIST_HEAD(&h->chains); |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 764 | strcpy(h->info.name, tablename); |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 765 | |
| Harald Welte | 0371c0c | 2004-09-19 21:00:12 +0000 | [diff] [blame] | 766 | h->entries = malloc(sizeof(STRUCT_GET_ENTRIES) + size); |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 767 | if (!h->entries) |
| 768 | goto out_free_handle; |
| 769 | |
| 770 | strcpy(h->entries->name, tablename); |
| Harald Welte | 0371c0c | 2004-09-19 21:00:12 +0000 | [diff] [blame] | 771 | h->entries->size = size; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 772 | |
| 773 | return h; |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 774 | |
| 775 | out_free_handle: |
| 776 | free(h); |
| 777 | |
| 778 | return NULL; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 779 | } |
| 780 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 781 | |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 782 | TC_HANDLE_T |
| 783 | TC_INIT(const char *tablename) |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 784 | { |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 785 | TC_HANDLE_T h; |
| 786 | STRUCT_GETINFO info; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 787 | int tmp; |
| 788 | socklen_t s; |
| 789 | |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 790 | iptc_fn = TC_INIT; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 791 | |
| Martin Josefsson | 841e4ae | 2003-05-02 15:30:11 +0000 | [diff] [blame] | 792 | if (strlen(tablename) >= TABLE_MAXNAMELEN) { |
| 793 | errno = EINVAL; |
| 794 | return NULL; |
| 795 | } |
| 796 | |
| Derrik Pates | 664c0a3 | 2005-02-01 13:28:14 +0000 | [diff] [blame^] | 797 | if (sockfd_use == 0) { |
| 798 | sockfd = socket(TC_AF, SOCK_RAW, IPPROTO_RAW); |
| 799 | if (sockfd < 0) |
| 800 | return NULL; |
| 801 | } |
| 802 | sockfd_use++; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 803 | |
| 804 | s = sizeof(info); |
| Martin Josefsson | 841e4ae | 2003-05-02 15:30:11 +0000 | [diff] [blame] | 805 | |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 806 | strcpy(info.name, tablename); |
| Derrik Pates | 664c0a3 | 2005-02-01 13:28:14 +0000 | [diff] [blame^] | 807 | if (getsockopt(sockfd, TC_IPPROTO, SO_GET_INFO, &info, &s) < 0) { |
| 808 | if (--sockfd_use == 0) { |
| 809 | close(sockfd); |
| 810 | sockfd = -1; |
| 811 | } |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 812 | return NULL; |
| Derrik Pates | 664c0a3 | 2005-02-01 13:28:14 +0000 | [diff] [blame^] | 813 | } |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 814 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 815 | DEBUGP("valid_hooks=0x%08x, num_entries=%u, size=%u\n", |
| 816 | info.valid_hooks, info.num_entries, info.size); |
| 817 | |
| Harald Welte | 0113fe7 | 2004-01-06 19:04:02 +0000 | [diff] [blame] | 818 | if ((h = alloc_handle(info.name, info.size, info.num_entries)) |
| Martin Josefsson | 841e4ae | 2003-05-02 15:30:11 +0000 | [diff] [blame] | 819 | == NULL) { |
| Derrik Pates | 664c0a3 | 2005-02-01 13:28:14 +0000 | [diff] [blame^] | 820 | if (--sockfd_use == 0) { |
| 821 | close(sockfd); |
| 822 | sockfd = -1; |
| 823 | } |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 824 | return NULL; |
| Martin Josefsson | 841e4ae | 2003-05-02 15:30:11 +0000 | [diff] [blame] | 825 | } |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 826 | |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 827 | /* Initialize current state */ |
| 828 | h->info = info; |
| Harald Welte | 0113fe7 | 2004-01-06 19:04:02 +0000 | [diff] [blame] | 829 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 830 | h->entries->size = h->info.size; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 831 | |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 832 | tmp = sizeof(STRUCT_GET_ENTRIES) + h->info.size; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 833 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 834 | if (getsockopt(sockfd, TC_IPPROTO, SO_GET_ENTRIES, h->entries, |
| 835 | &tmp) < 0) |
| 836 | goto error; |
| 837 | |
| 838 | #ifdef IPTC_DEBUG2 |
| 839 | { |
| 840 | int fd = open("/tmp/libiptc-so_get_entries.blob", |
| 841 | O_CREAT|O_WRONLY); |
| 842 | if (fd >= 0) { |
| 843 | write(fd, h->entries, tmp); |
| 844 | close(fd); |
| 845 | } |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 846 | } |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 847 | #endif |
| 848 | |
| 849 | if (parse_table(h) < 0) |
| 850 | goto error; |
| Rusty Russell | 7e53bf9 | 2000-03-20 07:03:28 +0000 | [diff] [blame] | 851 | |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 852 | CHECK(h); |
| 853 | return h; |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 854 | error: |
| Derrik Pates | 664c0a3 | 2005-02-01 13:28:14 +0000 | [diff] [blame^] | 855 | if (--sockfd_use == 0) { |
| 856 | close(sockfd); |
| 857 | sockfd = -1; |
| 858 | } |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 859 | TC_FREE(&h); |
| 860 | return NULL; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 861 | } |
| 862 | |
| Martin Josefsson | 841e4ae | 2003-05-02 15:30:11 +0000 | [diff] [blame] | 863 | void |
| 864 | TC_FREE(TC_HANDLE_T *h) |
| 865 | { |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 866 | struct chain_head *c, *tmp; |
| 867 | |
| Derrik Pates | 664c0a3 | 2005-02-01 13:28:14 +0000 | [diff] [blame^] | 868 | iptc_fn = TC_FREE; |
| 869 | if (--sockfd_use == 0) { |
| 870 | close(sockfd); |
| 871 | sockfd = -1; |
| 872 | } |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 873 | |
| 874 | list_for_each_entry_safe(c, tmp, &(*h)->chains, list) { |
| 875 | struct rule_head *r, *rtmp; |
| 876 | |
| 877 | list_for_each_entry_safe(r, rtmp, &c->rules, list) { |
| 878 | free(r); |
| 879 | } |
| 880 | |
| 881 | free(c); |
| 882 | } |
| 883 | |
| 884 | free((*h)->entries); |
| Martin Josefsson | 841e4ae | 2003-05-02 15:30:11 +0000 | [diff] [blame] | 885 | free(*h); |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 886 | |
| Martin Josefsson | 841e4ae | 2003-05-02 15:30:11 +0000 | [diff] [blame] | 887 | *h = NULL; |
| 888 | } |
| 889 | |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 890 | static inline int |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 891 | print_match(const STRUCT_ENTRY_MATCH *m) |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 892 | { |
| Rusty Russell | 228e98d | 2000-04-27 10:28:06 +0000 | [diff] [blame] | 893 | printf("Match name: `%s'\n", m->u.user.name); |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 894 | return 0; |
| 895 | } |
| 896 | |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 897 | static int dump_entry(STRUCT_ENTRY *e, const TC_HANDLE_T handle); |
| 898 | |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 899 | void |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 900 | TC_DUMP_ENTRIES(const TC_HANDLE_T handle) |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 901 | { |
| Derrik Pates | 664c0a3 | 2005-02-01 13:28:14 +0000 | [diff] [blame^] | 902 | iptc_fn = TC_DUMP_ENTRIES; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 903 | CHECK(handle); |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 904 | #if 0 |
| Rusty Russell | e45c713 | 2004-12-16 13:21:44 +0000 | [diff] [blame] | 905 | printf("libiptc v%s. %u bytes.\n", |
| 906 | IPTABLES_VERSION, handle->entries->size); |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 907 | printf("Table `%s'\n", handle->info.name); |
| 908 | printf("Hooks: pre/in/fwd/out/post = %u/%u/%u/%u/%u\n", |
| Rusty Russell | 67088e7 | 2000-05-10 01:18:57 +0000 | [diff] [blame] | 909 | handle->info.hook_entry[HOOK_PRE_ROUTING], |
| 910 | handle->info.hook_entry[HOOK_LOCAL_IN], |
| 911 | handle->info.hook_entry[HOOK_FORWARD], |
| 912 | handle->info.hook_entry[HOOK_LOCAL_OUT], |
| 913 | handle->info.hook_entry[HOOK_POST_ROUTING]); |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 914 | printf("Underflows: pre/in/fwd/out/post = %u/%u/%u/%u/%u\n", |
| Rusty Russell | 67088e7 | 2000-05-10 01:18:57 +0000 | [diff] [blame] | 915 | handle->info.underflow[HOOK_PRE_ROUTING], |
| 916 | handle->info.underflow[HOOK_LOCAL_IN], |
| 917 | handle->info.underflow[HOOK_FORWARD], |
| 918 | handle->info.underflow[HOOK_LOCAL_OUT], |
| 919 | handle->info.underflow[HOOK_POST_ROUTING]); |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 920 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 921 | ENTRY_ITERATE(handle->entries->entrytable, handle->entries->size, |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 922 | dump_entry, handle); |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 923 | #endif |
| Harald Welte | 0113fe7 | 2004-01-06 19:04:02 +0000 | [diff] [blame] | 924 | } |
| Rusty Russell | 30fd6e5 | 2000-04-23 09:16:06 +0000 | [diff] [blame] | 925 | |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 926 | /* Does this chain exist? */ |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 927 | int TC_IS_CHAIN(const char *chain, const TC_HANDLE_T handle) |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 928 | { |
| Derrik Pates | 664c0a3 | 2005-02-01 13:28:14 +0000 | [diff] [blame^] | 929 | iptc_fn = TC_IS_CHAIN; |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 930 | return iptcc_find_label(chain, handle) != NULL; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 931 | } |
| 932 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 933 | static void iptcc_chain_iterator_advance(TC_HANDLE_T handle) |
| 934 | { |
| 935 | struct chain_head *c = handle->chain_iterator_cur; |
| 936 | |
| 937 | if (c->list.next == &handle->chains) |
| 938 | handle->chain_iterator_cur = NULL; |
| 939 | else |
| 940 | handle->chain_iterator_cur = |
| 941 | list_entry(c->list.next, struct chain_head, list); |
| 942 | } |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 943 | |
| Rusty Russell | 30fd6e5 | 2000-04-23 09:16:06 +0000 | [diff] [blame] | 944 | /* Iterator functions to run through the chains. */ |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 945 | const char * |
| Philip Blundell | 8c70090 | 2000-05-15 02:17:52 +0000 | [diff] [blame] | 946 | TC_FIRST_CHAIN(TC_HANDLE_T *handle) |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 947 | { |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 948 | struct chain_head *c = list_entry((*handle)->chains.next, |
| 949 | struct chain_head, list); |
| 950 | |
| 951 | iptc_fn = TC_FIRST_CHAIN; |
| 952 | |
| 953 | |
| 954 | if (list_empty(&(*handle)->chains)) { |
| 955 | DEBUGP(": no chains\n"); |
| Harald Welte | 0113fe7 | 2004-01-06 19:04:02 +0000 | [diff] [blame] | 956 | return NULL; |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 957 | } |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 958 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 959 | (*handle)->chain_iterator_cur = c; |
| 960 | iptcc_chain_iterator_advance(*handle); |
| Harald Welte | 0113fe7 | 2004-01-06 19:04:02 +0000 | [diff] [blame] | 961 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 962 | DEBUGP(": returning `%s'\n", c->name); |
| 963 | return c->name; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 964 | } |
| 965 | |
| Rusty Russell | 30fd6e5 | 2000-04-23 09:16:06 +0000 | [diff] [blame] | 966 | /* Iterator functions to run through the chains. Returns NULL at end. */ |
| 967 | const char * |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 968 | TC_NEXT_CHAIN(TC_HANDLE_T *handle) |
| Rusty Russell | 30fd6e5 | 2000-04-23 09:16:06 +0000 | [diff] [blame] | 969 | { |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 970 | struct chain_head *c = (*handle)->chain_iterator_cur; |
| Rusty Russell | 30fd6e5 | 2000-04-23 09:16:06 +0000 | [diff] [blame] | 971 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 972 | iptc_fn = TC_NEXT_CHAIN; |
| 973 | |
| 974 | if (!c) { |
| 975 | DEBUGP(": no more chains\n"); |
| Rusty Russell | 30fd6e5 | 2000-04-23 09:16:06 +0000 | [diff] [blame] | 976 | return NULL; |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 977 | } |
| Rusty Russell | 30fd6e5 | 2000-04-23 09:16:06 +0000 | [diff] [blame] | 978 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 979 | iptcc_chain_iterator_advance(*handle); |
| 980 | |
| 981 | DEBUGP(": returning `%s'\n", c->name); |
| 982 | return c->name; |
| Rusty Russell | 30fd6e5 | 2000-04-23 09:16:06 +0000 | [diff] [blame] | 983 | } |
| 984 | |
| 985 | /* Get first rule in the given chain: NULL for empty chain. */ |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 986 | const STRUCT_ENTRY * |
| Philip Blundell | 8c70090 | 2000-05-15 02:17:52 +0000 | [diff] [blame] | 987 | TC_FIRST_RULE(const char *chain, TC_HANDLE_T *handle) |
| Rusty Russell | 30fd6e5 | 2000-04-23 09:16:06 +0000 | [diff] [blame] | 988 | { |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 989 | struct chain_head *c; |
| 990 | struct rule_head *r; |
| Rusty Russell | 30fd6e5 | 2000-04-23 09:16:06 +0000 | [diff] [blame] | 991 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 992 | iptc_fn = TC_FIRST_RULE; |
| 993 | |
| 994 | DEBUGP("first rule(%s): ", chain); |
| 995 | |
| 996 | c = iptcc_find_label(chain, *handle); |
| Rusty Russell | 30fd6e5 | 2000-04-23 09:16:06 +0000 | [diff] [blame] | 997 | if (!c) { |
| 998 | errno = ENOENT; |
| 999 | return NULL; |
| 1000 | } |
| 1001 | |
| 1002 | /* Empty chain: single return/policy rule */ |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1003 | if (list_empty(&c->rules)) { |
| 1004 | DEBUGP_C("no rules, returning NULL\n"); |
| Rusty Russell | 30fd6e5 | 2000-04-23 09:16:06 +0000 | [diff] [blame] | 1005 | return NULL; |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1006 | } |
| Rusty Russell | 30fd6e5 | 2000-04-23 09:16:06 +0000 | [diff] [blame] | 1007 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1008 | r = list_entry(c->rules.next, struct rule_head, list); |
| 1009 | (*handle)->rule_iterator_cur = r; |
| 1010 | DEBUGP_C("%p\n", r); |
| 1011 | |
| 1012 | return r->entry; |
| Rusty Russell | 30fd6e5 | 2000-04-23 09:16:06 +0000 | [diff] [blame] | 1013 | } |
| 1014 | |
| 1015 | /* Returns NULL when rules run out. */ |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1016 | const STRUCT_ENTRY * |
| Philip Blundell | 8c70090 | 2000-05-15 02:17:52 +0000 | [diff] [blame] | 1017 | TC_NEXT_RULE(const STRUCT_ENTRY *prev, TC_HANDLE_T *handle) |
| Rusty Russell | 30fd6e5 | 2000-04-23 09:16:06 +0000 | [diff] [blame] | 1018 | { |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1019 | struct rule_head *r; |
| Rusty Russell | 30fd6e5 | 2000-04-23 09:16:06 +0000 | [diff] [blame] | 1020 | |
| Derrik Pates | 664c0a3 | 2005-02-01 13:28:14 +0000 | [diff] [blame^] | 1021 | iptc_fn = TC_NEXT_RULE; |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1022 | DEBUGP("rule_iterator_cur=%p...", (*handle)->rule_iterator_cur); |
| 1023 | |
| 1024 | if (!(*handle)->rule_iterator_cur) { |
| 1025 | DEBUGP_C("returning NULL\n"); |
| 1026 | return NULL; |
| 1027 | } |
| 1028 | |
| 1029 | r = list_entry((*handle)->rule_iterator_cur->list.next, |
| 1030 | struct rule_head, list); |
| 1031 | |
| 1032 | iptc_fn = TC_NEXT_RULE; |
| 1033 | |
| 1034 | DEBUGP_C("next=%p, head=%p...", &r->list, |
| 1035 | &(*handle)->rule_iterator_cur->chain->rules); |
| 1036 | |
| 1037 | if (&r->list == &(*handle)->rule_iterator_cur->chain->rules) { |
| 1038 | (*handle)->rule_iterator_cur = NULL; |
| 1039 | DEBUGP_C("finished, returning NULL\n"); |
| 1040 | return NULL; |
| 1041 | } |
| 1042 | |
| 1043 | (*handle)->rule_iterator_cur = r; |
| 1044 | |
| 1045 | /* NOTE: prev is without any influence ! */ |
| 1046 | DEBUGP_C("returning rule %p\n", r); |
| 1047 | return r->entry; |
| Rusty Russell | 30fd6e5 | 2000-04-23 09:16:06 +0000 | [diff] [blame] | 1048 | } |
| 1049 | |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1050 | /* How many rules in this chain? */ |
| 1051 | unsigned int |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1052 | TC_NUM_RULES(const char *chain, TC_HANDLE_T *handle) |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1053 | { |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1054 | struct chain_head *c; |
| 1055 | iptc_fn = TC_NUM_RULES; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1056 | CHECK(*handle); |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1057 | |
| 1058 | c = iptcc_find_label(chain, *handle); |
| 1059 | if (!c) { |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1060 | errno = ENOENT; |
| 1061 | return (unsigned int)-1; |
| 1062 | } |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1063 | |
| 1064 | return c->num_rules; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1065 | } |
| 1066 | |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1067 | const STRUCT_ENTRY *TC_GET_RULE(const char *chain, |
| 1068 | unsigned int n, |
| 1069 | TC_HANDLE_T *handle) |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1070 | { |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1071 | struct chain_head *c; |
| 1072 | struct rule_head *r; |
| 1073 | |
| 1074 | iptc_fn = TC_GET_RULE; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1075 | |
| 1076 | CHECK(*handle); |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1077 | |
| 1078 | c = iptcc_find_label(chain, *handle); |
| 1079 | if (!c) { |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1080 | errno = ENOENT; |
| 1081 | return NULL; |
| 1082 | } |
| 1083 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1084 | r = iptcc_get_rule_num(c, n); |
| 1085 | if (!r) |
| 1086 | return NULL; |
| 1087 | return r->entry; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1088 | } |
| 1089 | |
| 1090 | /* Returns a pointer to the target name of this position. */ |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1091 | const char *standard_target_map(int verdict) |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1092 | { |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1093 | switch (verdict) { |
| 1094 | case RETURN: |
| 1095 | return LABEL_RETURN; |
| 1096 | break; |
| 1097 | case -NF_ACCEPT-1: |
| 1098 | return LABEL_ACCEPT; |
| 1099 | break; |
| 1100 | case -NF_DROP-1: |
| 1101 | return LABEL_DROP; |
| 1102 | break; |
| 1103 | case -NF_QUEUE-1: |
| 1104 | return LABEL_QUEUE; |
| 1105 | break; |
| 1106 | default: |
| 1107 | fprintf(stderr, "ERROR: %d not a valid target)\n", |
| 1108 | verdict); |
| 1109 | abort(); |
| 1110 | break; |
| 1111 | } |
| 1112 | /* not reached */ |
| 1113 | return NULL; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1114 | } |
| 1115 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1116 | /* Returns a pointer to the target name of this position. */ |
| 1117 | const char *TC_GET_TARGET(const STRUCT_ENTRY *ce, |
| 1118 | TC_HANDLE_T *handle) |
| 1119 | { |
| 1120 | STRUCT_ENTRY *e = (STRUCT_ENTRY *)ce; |
| Rusty Russell | e45c713 | 2004-12-16 13:21:44 +0000 | [diff] [blame] | 1121 | struct rule_head *r = container_of(e, struct rule_head, entry[0]); |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1122 | |
| 1123 | iptc_fn = TC_GET_TARGET; |
| 1124 | |
| 1125 | switch(r->type) { |
| 1126 | int spos; |
| 1127 | case IPTCC_R_FALLTHROUGH: |
| 1128 | return ""; |
| 1129 | break; |
| 1130 | case IPTCC_R_JUMP: |
| 1131 | DEBUGP("r=%p, jump=%p, name=`%s'\n", r, r->jump, r->jump->name); |
| 1132 | return r->jump->name; |
| 1133 | break; |
| 1134 | case IPTCC_R_STANDARD: |
| 1135 | spos = *(int *)GET_TARGET(e)->data; |
| 1136 | DEBUGP("r=%p, spos=%d'\n", r, spos); |
| 1137 | return standard_target_map(spos); |
| 1138 | break; |
| 1139 | case IPTCC_R_MODULE: |
| 1140 | return GET_TARGET(e)->u.user.name; |
| 1141 | break; |
| 1142 | } |
| 1143 | return NULL; |
| 1144 | } |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1145 | /* Is this a built-in chain? Actually returns hook + 1. */ |
| 1146 | int |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1147 | TC_BUILTIN(const char *chain, const TC_HANDLE_T handle) |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1148 | { |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1149 | struct chain_head *c; |
| 1150 | |
| 1151 | iptc_fn = TC_BUILTIN; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1152 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1153 | c = iptcc_find_label(chain, handle); |
| 1154 | if (!c) { |
| 1155 | errno = ENOENT; |
| Martin Josefsson | b0f3d2d | 2004-09-23 18:23:20 +0000 | [diff] [blame] | 1156 | return 0; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1157 | } |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1158 | |
| 1159 | return iptcc_is_builtin(c); |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1160 | } |
| 1161 | |
| 1162 | /* Get the policy of a given built-in chain */ |
| 1163 | const char * |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1164 | TC_GET_POLICY(const char *chain, |
| 1165 | STRUCT_COUNTERS *counters, |
| 1166 | TC_HANDLE_T *handle) |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1167 | { |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1168 | struct chain_head *c; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1169 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1170 | iptc_fn = TC_GET_POLICY; |
| 1171 | |
| 1172 | DEBUGP("called for chain %s\n", chain); |
| 1173 | |
| 1174 | c = iptcc_find_label(chain, *handle); |
| 1175 | if (!c) { |
| 1176 | errno = ENOENT; |
| 1177 | return NULL; |
| 1178 | } |
| 1179 | |
| 1180 | if (!iptcc_is_builtin(c)) |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1181 | return NULL; |
| 1182 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1183 | *counters = c->counters; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1184 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1185 | return standard_target_map(c->verdict); |
| Harald Welte | 0113fe7 | 2004-01-06 19:04:02 +0000 | [diff] [blame] | 1186 | } |
| 1187 | |
| 1188 | static int |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1189 | iptcc_standard_map(struct rule_head *r, int verdict) |
| Harald Welte | 0113fe7 | 2004-01-06 19:04:02 +0000 | [diff] [blame] | 1190 | { |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1191 | STRUCT_ENTRY *e = r->entry; |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1192 | STRUCT_STANDARD_TARGET *t; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1193 | |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1194 | t = (STRUCT_STANDARD_TARGET *)GET_TARGET(e); |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1195 | |
| Rusty Russell | 67088e7 | 2000-05-10 01:18:57 +0000 | [diff] [blame] | 1196 | if (t->target.u.target_size |
| Philip Blundell | 8c70090 | 2000-05-15 02:17:52 +0000 | [diff] [blame] | 1197 | != ALIGN(sizeof(STRUCT_STANDARD_TARGET))) { |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1198 | errno = EINVAL; |
| 1199 | return 0; |
| 1200 | } |
| 1201 | /* memset for memcmp convenience on delete/replace */ |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1202 | memset(t->target.u.user.name, 0, FUNCTION_MAXNAMELEN); |
| 1203 | strcpy(t->target.u.user.name, STANDARD_TARGET); |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1204 | t->verdict = verdict; |
| 1205 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1206 | r->type = IPTCC_R_STANDARD; |
| 1207 | |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1208 | return 1; |
| 1209 | } |
| Rusty Russell | 7e53bf9 | 2000-03-20 07:03:28 +0000 | [diff] [blame] | 1210 | |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1211 | static int |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1212 | iptcc_map_target(const TC_HANDLE_T handle, |
| 1213 | struct rule_head *r) |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1214 | { |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1215 | STRUCT_ENTRY *e = r->entry; |
| Harald Welte | 0113fe7 | 2004-01-06 19:04:02 +0000 | [diff] [blame] | 1216 | STRUCT_ENTRY_TARGET *t = GET_TARGET(e); |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1217 | |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1218 | /* Maybe it's empty (=> fall through) */ |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1219 | if (strcmp(t->u.user.name, "") == 0) { |
| 1220 | r->type = IPTCC_R_FALLTHROUGH; |
| 1221 | return 1; |
| 1222 | } |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1223 | /* Maybe it's a standard target name... */ |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1224 | else if (strcmp(t->u.user.name, LABEL_ACCEPT) == 0) |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1225 | return iptcc_standard_map(r, -NF_ACCEPT - 1); |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1226 | else if (strcmp(t->u.user.name, LABEL_DROP) == 0) |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1227 | return iptcc_standard_map(r, -NF_DROP - 1); |
| Rusty Russell | 67088e7 | 2000-05-10 01:18:57 +0000 | [diff] [blame] | 1228 | else if (strcmp(t->u.user.name, LABEL_QUEUE) == 0) |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1229 | return iptcc_standard_map(r, -NF_QUEUE - 1); |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1230 | else if (strcmp(t->u.user.name, LABEL_RETURN) == 0) |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1231 | return iptcc_standard_map(r, RETURN); |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1232 | else if (TC_BUILTIN(t->u.user.name, handle)) { |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1233 | /* Can't jump to builtins. */ |
| 1234 | errno = EINVAL; |
| 1235 | return 0; |
| 1236 | } else { |
| 1237 | /* Maybe it's an existing chain name. */ |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1238 | struct chain_head *c; |
| 1239 | DEBUGP("trying to find chain `%s': ", t->u.user.name); |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1240 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1241 | c = iptcc_find_label(t->u.user.name, handle); |
| 1242 | if (c) { |
| 1243 | DEBUGP_C("found!\n"); |
| 1244 | r->type = IPTCC_R_JUMP; |
| 1245 | r->jump = c; |
| 1246 | c->references++; |
| 1247 | return 1; |
| 1248 | } |
| 1249 | DEBUGP_C("not found :(\n"); |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1250 | } |
| 1251 | |
| 1252 | /* Must be a module? If not, kernel will reject... */ |
| Rusty Russell | 3aef54d | 2005-01-03 03:48:40 +0000 | [diff] [blame] | 1253 | /* memset to all 0 for your memcmp convenience: don't clear version */ |
| Rusty Russell | 228e98d | 2000-04-27 10:28:06 +0000 | [diff] [blame] | 1254 | memset(t->u.user.name + strlen(t->u.user.name), |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1255 | 0, |
| Rusty Russell | 3aef54d | 2005-01-03 03:48:40 +0000 | [diff] [blame] | 1256 | FUNCTION_MAXNAMELEN - 1 - strlen(t->u.user.name)); |
| Rusty Russell | 733e54b | 2004-12-16 14:22:23 +0000 | [diff] [blame] | 1257 | r->type = IPTCC_R_MODULE; |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1258 | set_changed(handle); |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1259 | return 1; |
| 1260 | } |
| 1261 | |
| Harald Welte | 0113fe7 | 2004-01-06 19:04:02 +0000 | [diff] [blame] | 1262 | /* Insert the entry `fw' in chain `chain' into position `rulenum'. */ |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1263 | int |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1264 | TC_INSERT_ENTRY(const IPT_CHAINLABEL chain, |
| 1265 | const STRUCT_ENTRY *e, |
| 1266 | unsigned int rulenum, |
| 1267 | TC_HANDLE_T *handle) |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1268 | { |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1269 | struct chain_head *c; |
| Martin Josefsson | eb066cc | 2004-09-22 21:04:07 +0000 | [diff] [blame] | 1270 | struct rule_head *r; |
| 1271 | struct list_head *prev; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1272 | |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1273 | iptc_fn = TC_INSERT_ENTRY; |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1274 | |
| 1275 | if (!(c = iptcc_find_label(chain, *handle))) { |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1276 | errno = ENOENT; |
| 1277 | return 0; |
| 1278 | } |
| 1279 | |
| Martin Josefsson | eb066cc | 2004-09-22 21:04:07 +0000 | [diff] [blame] | 1280 | /* first rulenum index = 0 |
| 1281 | first c->num_rules index = 1 */ |
| 1282 | if (rulenum > c->num_rules) { |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1283 | errno = E2BIG; |
| 1284 | return 0; |
| 1285 | } |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1286 | |
| Martin Josefsson | 631f361 | 2004-09-23 19:25:06 +0000 | [diff] [blame] | 1287 | /* If we are inserting at the end just take advantage of the |
| 1288 | double linked list, insert will happen before the entry |
| 1289 | prev points to. */ |
| 1290 | if (rulenum == c->num_rules) { |
| Martin Josefsson | eb066cc | 2004-09-22 21:04:07 +0000 | [diff] [blame] | 1291 | prev = &c->rules; |
| Martin Josefsson | a5616dc | 2004-10-24 22:27:31 +0000 | [diff] [blame] | 1292 | } else if (rulenum + 1 <= c->num_rules/2) { |
| Martin Josefsson | 631f361 | 2004-09-23 19:25:06 +0000 | [diff] [blame] | 1293 | r = iptcc_get_rule_num(c, rulenum + 1); |
| Martin Josefsson | a5616dc | 2004-10-24 22:27:31 +0000 | [diff] [blame] | 1294 | prev = &r->list; |
| 1295 | } else { |
| 1296 | r = iptcc_get_rule_num_reverse(c, c->num_rules - rulenum); |
| Martin Josefsson | 631f361 | 2004-09-23 19:25:06 +0000 | [diff] [blame] | 1297 | prev = &r->list; |
| 1298 | } |
| Martin Josefsson | eb066cc | 2004-09-22 21:04:07 +0000 | [diff] [blame] | 1299 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1300 | if (!(r = iptcc_alloc_rule(c, e->next_offset))) { |
| 1301 | errno = ENOMEM; |
| Harald Welte | 0113fe7 | 2004-01-06 19:04:02 +0000 | [diff] [blame] | 1302 | return 0; |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1303 | } |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1304 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1305 | memcpy(r->entry, e, e->next_offset); |
| 1306 | r->counter_map.maptype = COUNTER_MAP_SET; |
| 1307 | |
| 1308 | if (!iptcc_map_target(*handle, r)) { |
| 1309 | free(r); |
| 1310 | return 0; |
| 1311 | } |
| 1312 | |
| Martin Josefsson | eb066cc | 2004-09-22 21:04:07 +0000 | [diff] [blame] | 1313 | list_add_tail(&r->list, prev); |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1314 | c->num_rules++; |
| 1315 | |
| 1316 | set_changed(*handle); |
| 1317 | |
| 1318 | return 1; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1319 | } |
| 1320 | |
| 1321 | /* Atomically replace rule `rulenum' in `chain' with `fw'. */ |
| 1322 | int |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1323 | TC_REPLACE_ENTRY(const IPT_CHAINLABEL chain, |
| 1324 | const STRUCT_ENTRY *e, |
| 1325 | unsigned int rulenum, |
| 1326 | TC_HANDLE_T *handle) |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1327 | { |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1328 | struct chain_head *c; |
| 1329 | struct rule_head *r, *old; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1330 | |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1331 | iptc_fn = TC_REPLACE_ENTRY; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1332 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1333 | if (!(c = iptcc_find_label(chain, *handle))) { |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1334 | errno = ENOENT; |
| 1335 | return 0; |
| 1336 | } |
| 1337 | |
| Martin Josefsson | 0f9b8b1 | 2004-12-18 17:18:49 +0000 | [diff] [blame] | 1338 | if (rulenum >= c->num_rules) { |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1339 | errno = E2BIG; |
| 1340 | return 0; |
| 1341 | } |
| 1342 | |
| Martin Josefsson | 0f9b8b1 | 2004-12-18 17:18:49 +0000 | [diff] [blame] | 1343 | /* Take advantage of the double linked list if possible. */ |
| 1344 | if (rulenum + 1 <= c->num_rules/2) { |
| 1345 | old = iptcc_get_rule_num(c, rulenum + 1); |
| 1346 | } else { |
| 1347 | old = iptcc_get_rule_num_reverse(c, c->num_rules - rulenum); |
| 1348 | } |
| 1349 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1350 | if (!(r = iptcc_alloc_rule(c, e->next_offset))) { |
| 1351 | errno = ENOMEM; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1352 | return 0; |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1353 | } |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1354 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1355 | memcpy(r->entry, e, e->next_offset); |
| 1356 | r->counter_map.maptype = COUNTER_MAP_SET; |
| 1357 | |
| 1358 | if (!iptcc_map_target(*handle, r)) { |
| 1359 | free(r); |
| Harald Welte | 0113fe7 | 2004-01-06 19:04:02 +0000 | [diff] [blame] | 1360 | return 0; |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1361 | } |
| Harald Welte | 0113fe7 | 2004-01-06 19:04:02 +0000 | [diff] [blame] | 1362 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1363 | list_add(&r->list, &old->list); |
| 1364 | iptcc_delete_rule(old); |
| 1365 | |
| 1366 | set_changed(*handle); |
| 1367 | |
| 1368 | return 1; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1369 | } |
| 1370 | |
| Harald Welte | 0113fe7 | 2004-01-06 19:04:02 +0000 | [diff] [blame] | 1371 | /* Append entry `fw' to chain `chain'. Equivalent to insert with |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1372 | rulenum = length of chain. */ |
| 1373 | int |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1374 | TC_APPEND_ENTRY(const IPT_CHAINLABEL chain, |
| 1375 | const STRUCT_ENTRY *e, |
| 1376 | TC_HANDLE_T *handle) |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1377 | { |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1378 | struct chain_head *c; |
| 1379 | struct rule_head *r; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1380 | |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1381 | iptc_fn = TC_APPEND_ENTRY; |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1382 | if (!(c = iptcc_find_label(chain, *handle))) { |
| 1383 | DEBUGP("unable to find chain `%s'\n", chain); |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1384 | errno = ENOENT; |
| 1385 | return 0; |
| 1386 | } |
| 1387 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1388 | if (!(r = iptcc_alloc_rule(c, e->next_offset))) { |
| 1389 | DEBUGP("unable to allocate rule for chain `%s'\n", chain); |
| 1390 | errno = ENOMEM; |
| Harald Welte | 0113fe7 | 2004-01-06 19:04:02 +0000 | [diff] [blame] | 1391 | return 0; |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1392 | } |
| Harald Welte | 0113fe7 | 2004-01-06 19:04:02 +0000 | [diff] [blame] | 1393 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1394 | memcpy(r->entry, e, e->next_offset); |
| 1395 | r->counter_map.maptype = COUNTER_MAP_SET; |
| 1396 | |
| 1397 | if (!iptcc_map_target(*handle, r)) { |
| Martin Josefsson | 1200953 | 2004-09-23 18:24:29 +0000 | [diff] [blame] | 1398 | DEBUGP("unable to map target of rule for chain `%s'\n", chain); |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1399 | free(r); |
| 1400 | return 0; |
| 1401 | } |
| 1402 | |
| 1403 | list_add_tail(&r->list, &c->rules); |
| 1404 | c->num_rules++; |
| 1405 | |
| 1406 | set_changed(*handle); |
| 1407 | |
| 1408 | return 1; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1409 | } |
| 1410 | |
| 1411 | static inline int |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1412 | match_different(const STRUCT_ENTRY_MATCH *a, |
| Rusty Russell | edf14cf | 2000-04-19 11:26:44 +0000 | [diff] [blame] | 1413 | const unsigned char *a_elems, |
| 1414 | const unsigned char *b_elems, |
| 1415 | unsigned char **maskptr) |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1416 | { |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1417 | const STRUCT_ENTRY_MATCH *b; |
| Rusty Russell | edf14cf | 2000-04-19 11:26:44 +0000 | [diff] [blame] | 1418 | unsigned int i; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1419 | |
| 1420 | /* Offset of b is the same as a. */ |
| Rusty Russell | 30fd6e5 | 2000-04-23 09:16:06 +0000 | [diff] [blame] | 1421 | b = (void *)b_elems + ((unsigned char *)a - a_elems); |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1422 | |
| Rusty Russell | 228e98d | 2000-04-27 10:28:06 +0000 | [diff] [blame] | 1423 | if (a->u.match_size != b->u.match_size) |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1424 | return 1; |
| 1425 | |
| Rusty Russell | 228e98d | 2000-04-27 10:28:06 +0000 | [diff] [blame] | 1426 | if (strcmp(a->u.user.name, b->u.user.name) != 0) |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1427 | return 1; |
| 1428 | |
| Rusty Russell | 73ef09b | 2000-07-03 10:24:04 +0000 | [diff] [blame] | 1429 | *maskptr += ALIGN(sizeof(*a)); |
| Rusty Russell | edf14cf | 2000-04-19 11:26:44 +0000 | [diff] [blame] | 1430 | |
| Rusty Russell | 73ef09b | 2000-07-03 10:24:04 +0000 | [diff] [blame] | 1431 | for (i = 0; i < a->u.match_size - ALIGN(sizeof(*a)); i++) |
| Rusty Russell | edf14cf | 2000-04-19 11:26:44 +0000 | [diff] [blame] | 1432 | if (((a->data[i] ^ b->data[i]) & (*maskptr)[i]) != 0) |
| Rusty Russell | 90e712a | 2000-03-29 04:19:26 +0000 | [diff] [blame] | 1433 | return 1; |
| Rusty Russell | edf14cf | 2000-04-19 11:26:44 +0000 | [diff] [blame] | 1434 | *maskptr += i; |
| 1435 | return 0; |
| 1436 | } |
| 1437 | |
| 1438 | static inline int |
| Rusty Russell | 733e54b | 2004-12-16 14:22:23 +0000 | [diff] [blame] | 1439 | target_same(struct rule_head *a, struct rule_head *b,const unsigned char *mask) |
| Rusty Russell | edf14cf | 2000-04-19 11:26:44 +0000 | [diff] [blame] | 1440 | { |
| 1441 | unsigned int i; |
| Rusty Russell | 733e54b | 2004-12-16 14:22:23 +0000 | [diff] [blame] | 1442 | STRUCT_ENTRY_TARGET *ta, *tb; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1443 | |
| Rusty Russell | 733e54b | 2004-12-16 14:22:23 +0000 | [diff] [blame] | 1444 | if (a->type != b->type) |
| 1445 | return 0; |
| 1446 | |
| 1447 | ta = GET_TARGET(a->entry); |
| 1448 | tb = GET_TARGET(b->entry); |
| 1449 | |
| 1450 | switch (a->type) { |
| 1451 | case IPTCC_R_FALLTHROUGH: |
| 1452 | return 1; |
| 1453 | case IPTCC_R_JUMP: |
| 1454 | return a->jump == b->jump; |
| 1455 | case IPTCC_R_STANDARD: |
| 1456 | return ((STRUCT_STANDARD_TARGET *)ta)->verdict |
| 1457 | == ((STRUCT_STANDARD_TARGET *)tb)->verdict; |
| 1458 | case IPTCC_R_MODULE: |
| 1459 | if (ta->u.target_size != tb->u.target_size) |
| 1460 | return 0; |
| 1461 | if (strcmp(ta->u.user.name, tb->u.user.name) != 0) |
| 1462 | return 0; |
| 1463 | |
| 1464 | for (i = 0; i < ta->u.target_size - sizeof(*ta); i++) |
| Rusty Russell | daade44 | 2004-12-29 11:14:52 +0000 | [diff] [blame] | 1465 | if (((ta->data[i] ^ tb->data[i]) & mask[i]) != 0) |
| Rusty Russell | 733e54b | 2004-12-16 14:22:23 +0000 | [diff] [blame] | 1466 | return 0; |
| 1467 | return 1; |
| 1468 | default: |
| 1469 | fprintf(stderr, "ERROR: bad type %i\n", a->type); |
| 1470 | abort(); |
| 1471 | } |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1472 | } |
| 1473 | |
| Rusty Russell | 733e54b | 2004-12-16 14:22:23 +0000 | [diff] [blame] | 1474 | static unsigned char * |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1475 | is_same(const STRUCT_ENTRY *a, |
| 1476 | const STRUCT_ENTRY *b, |
| 1477 | unsigned char *matchmask); |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1478 | |
| Harald Welte | 0113fe7 | 2004-01-06 19:04:02 +0000 | [diff] [blame] | 1479 | /* Delete the first rule in `chain' which matches `fw'. */ |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1480 | int |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1481 | TC_DELETE_ENTRY(const IPT_CHAINLABEL chain, |
| 1482 | const STRUCT_ENTRY *origfw, |
| 1483 | unsigned char *matchmask, |
| 1484 | TC_HANDLE_T *handle) |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1485 | { |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1486 | struct chain_head *c; |
| Rusty Russell | e45c713 | 2004-12-16 13:21:44 +0000 | [diff] [blame] | 1487 | struct rule_head *r, *i; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1488 | |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1489 | iptc_fn = TC_DELETE_ENTRY; |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1490 | if (!(c = iptcc_find_label(chain, *handle))) { |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1491 | errno = ENOENT; |
| 1492 | return 0; |
| 1493 | } |
| 1494 | |
| Rusty Russell | e45c713 | 2004-12-16 13:21:44 +0000 | [diff] [blame] | 1495 | /* Create a rule_head from origfw. */ |
| 1496 | r = iptcc_alloc_rule(c, origfw->next_offset); |
| 1497 | if (!r) { |
| Harald Welte | 0113fe7 | 2004-01-06 19:04:02 +0000 | [diff] [blame] | 1498 | errno = ENOMEM; |
| 1499 | return 0; |
| 1500 | } |
| 1501 | |
| Rusty Russell | e45c713 | 2004-12-16 13:21:44 +0000 | [diff] [blame] | 1502 | memcpy(r->entry, origfw, origfw->next_offset); |
| 1503 | r->counter_map.maptype = COUNTER_MAP_NOMAP; |
| 1504 | if (!iptcc_map_target(*handle, r)) { |
| 1505 | DEBUGP("unable to map target of rule for chain `%s'\n", chain); |
| 1506 | free(r); |
| 1507 | return 0; |
| 1508 | } |
| Harald Welte | 0113fe7 | 2004-01-06 19:04:02 +0000 | [diff] [blame] | 1509 | |
| Rusty Russell | e45c713 | 2004-12-16 13:21:44 +0000 | [diff] [blame] | 1510 | list_for_each_entry(i, &c->rules, list) { |
| Rusty Russell | 733e54b | 2004-12-16 14:22:23 +0000 | [diff] [blame] | 1511 | unsigned char *mask; |
| Harald Welte | fe53707 | 2004-08-30 20:28:53 +0000 | [diff] [blame] | 1512 | |
| Rusty Russell | 733e54b | 2004-12-16 14:22:23 +0000 | [diff] [blame] | 1513 | mask = is_same(r->entry, i->entry, matchmask); |
| 1514 | if (!mask) |
| 1515 | continue; |
| Martin Josefsson | 2a5dbbb | 2004-09-22 21:37:41 +0000 | [diff] [blame] | 1516 | |
| Rusty Russell | 733e54b | 2004-12-16 14:22:23 +0000 | [diff] [blame] | 1517 | if (!target_same(r, i, mask)) |
| 1518 | continue; |
| 1519 | |
| 1520 | /* If we are about to delete the rule that is the |
| 1521 | * current iterator, move rule iterator back. next |
| 1522 | * pointer will then point to real next node */ |
| 1523 | if (i == (*handle)->rule_iterator_cur) { |
| 1524 | (*handle)->rule_iterator_cur = |
| 1525 | list_entry((*handle)->rule_iterator_cur->list.prev, |
| 1526 | struct rule_head, list); |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1527 | } |
| Rusty Russell | 733e54b | 2004-12-16 14:22:23 +0000 | [diff] [blame] | 1528 | |
| 1529 | c->num_rules--; |
| 1530 | iptcc_delete_rule(i); |
| 1531 | |
| 1532 | set_changed(*handle); |
| 1533 | free(r); |
| 1534 | return 1; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1535 | } |
| 1536 | |
| Rusty Russell | e45c713 | 2004-12-16 13:21:44 +0000 | [diff] [blame] | 1537 | free(r); |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1538 | errno = ENOENT; |
| 1539 | return 0; |
| Rusty Russell | 7e53bf9 | 2000-03-20 07:03:28 +0000 | [diff] [blame] | 1540 | } |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1541 | |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1542 | |
| 1543 | /* Delete the rule in position `rulenum' in `chain'. */ |
| 1544 | int |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1545 | TC_DELETE_NUM_ENTRY(const IPT_CHAINLABEL chain, |
| 1546 | unsigned int rulenum, |
| 1547 | TC_HANDLE_T *handle) |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1548 | { |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1549 | struct chain_head *c; |
| 1550 | struct rule_head *r; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1551 | |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1552 | iptc_fn = TC_DELETE_NUM_ENTRY; |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1553 | |
| 1554 | if (!(c = iptcc_find_label(chain, *handle))) { |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1555 | errno = ENOENT; |
| 1556 | return 0; |
| 1557 | } |
| 1558 | |
| Martin Josefsson | a5616dc | 2004-10-24 22:27:31 +0000 | [diff] [blame] | 1559 | if (rulenum >= c->num_rules) { |
| Martin Josefsson | 631f361 | 2004-09-23 19:25:06 +0000 | [diff] [blame] | 1560 | errno = E2BIG; |
| 1561 | return 0; |
| 1562 | } |
| 1563 | |
| 1564 | /* Take advantage of the double linked list if possible. */ |
| Martin Josefsson | a5616dc | 2004-10-24 22:27:31 +0000 | [diff] [blame] | 1565 | if (rulenum + 1 <= c->num_rules/2) { |
| 1566 | r = iptcc_get_rule_num(c, rulenum + 1); |
| 1567 | } else { |
| 1568 | r = iptcc_get_rule_num_reverse(c, c->num_rules - rulenum); |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1569 | } |
| 1570 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1571 | /* If we are about to delete the rule that is the current |
| 1572 | * iterator, move rule iterator back. next pointer will then |
| 1573 | * point to real next node */ |
| 1574 | if (r == (*handle)->rule_iterator_cur) { |
| 1575 | (*handle)->rule_iterator_cur = |
| 1576 | list_entry((*handle)->rule_iterator_cur->list.prev, |
| 1577 | struct rule_head, list); |
| Harald Welte | 0113fe7 | 2004-01-06 19:04:02 +0000 | [diff] [blame] | 1578 | } |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1579 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1580 | c->num_rules--; |
| 1581 | iptcc_delete_rule(r); |
| 1582 | |
| Martin Josefsson | 2a5dbbb | 2004-09-22 21:37:41 +0000 | [diff] [blame] | 1583 | set_changed(*handle); |
| 1584 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1585 | return 1; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1586 | } |
| 1587 | |
| 1588 | /* Check the packet `fw' on chain `chain'. Returns the verdict, or |
| 1589 | NULL and sets errno. */ |
| 1590 | const char * |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1591 | TC_CHECK_PACKET(const IPT_CHAINLABEL chain, |
| 1592 | STRUCT_ENTRY *entry, |
| 1593 | TC_HANDLE_T *handle) |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1594 | { |
| Derrik Pates | 664c0a3 | 2005-02-01 13:28:14 +0000 | [diff] [blame^] | 1595 | iptc_fn = TC_CHECK_PACKET; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1596 | errno = ENOSYS; |
| 1597 | return NULL; |
| 1598 | } |
| 1599 | |
| 1600 | /* Flushes the entries in the given chain (ie. empties chain). */ |
| 1601 | int |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1602 | TC_FLUSH_ENTRIES(const IPT_CHAINLABEL chain, TC_HANDLE_T *handle) |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1603 | { |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1604 | struct chain_head *c; |
| 1605 | struct rule_head *r, *tmp; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1606 | |
| Harald Welte | 0113fe7 | 2004-01-06 19:04:02 +0000 | [diff] [blame] | 1607 | iptc_fn = TC_FLUSH_ENTRIES; |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1608 | if (!(c = iptcc_find_label(chain, *handle))) { |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1609 | errno = ENOENT; |
| 1610 | return 0; |
| 1611 | } |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1612 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1613 | list_for_each_entry_safe(r, tmp, &c->rules, list) { |
| 1614 | iptcc_delete_rule(r); |
| 1615 | } |
| 1616 | |
| 1617 | c->num_rules = 0; |
| 1618 | |
| 1619 | set_changed(*handle); |
| 1620 | |
| 1621 | return 1; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1622 | } |
| 1623 | |
| 1624 | /* Zeroes the counters in a chain. */ |
| 1625 | int |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1626 | TC_ZERO_ENTRIES(const IPT_CHAINLABEL chain, TC_HANDLE_T *handle) |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1627 | { |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1628 | struct chain_head *c; |
| 1629 | struct rule_head *r; |
| Rusty Russell | 7e53bf9 | 2000-03-20 07:03:28 +0000 | [diff] [blame] | 1630 | |
| Derrik Pates | 664c0a3 | 2005-02-01 13:28:14 +0000 | [diff] [blame^] | 1631 | iptc_fn = TC_ZERO_ENTRIES; |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1632 | if (!(c = iptcc_find_label(chain, *handle))) { |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1633 | errno = ENOENT; |
| 1634 | return 0; |
| 1635 | } |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1636 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1637 | list_for_each_entry(r, &c->rules, list) { |
| 1638 | if (r->counter_map.maptype == COUNTER_MAP_NORMAL_MAP) |
| 1639 | r->counter_map.maptype = COUNTER_MAP_ZEROED; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1640 | } |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1641 | |
| Rusty Russell | 175f641 | 2000-03-24 09:32:20 +0000 | [diff] [blame] | 1642 | set_changed(*handle); |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1643 | |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1644 | return 1; |
| 1645 | } |
| 1646 | |
| Harald Welte | 1cef74d | 2001-01-05 15:22:59 +0000 | [diff] [blame] | 1647 | STRUCT_COUNTERS * |
| 1648 | TC_READ_COUNTER(const IPT_CHAINLABEL chain, |
| 1649 | unsigned int rulenum, |
| 1650 | TC_HANDLE_T *handle) |
| 1651 | { |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1652 | struct chain_head *c; |
| 1653 | struct rule_head *r; |
| Harald Welte | 1cef74d | 2001-01-05 15:22:59 +0000 | [diff] [blame] | 1654 | |
| 1655 | iptc_fn = TC_READ_COUNTER; |
| 1656 | CHECK(*handle); |
| 1657 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1658 | if (!(c = iptcc_find_label(chain, *handle))) { |
| Harald Welte | 1cef74d | 2001-01-05 15:22:59 +0000 | [diff] [blame] | 1659 | errno = ENOENT; |
| 1660 | return NULL; |
| 1661 | } |
| 1662 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1663 | if (!(r = iptcc_get_rule_num(c, rulenum))) { |
| Harald Welte | 0113fe7 | 2004-01-06 19:04:02 +0000 | [diff] [blame] | 1664 | errno = E2BIG; |
| 1665 | return NULL; |
| 1666 | } |
| 1667 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1668 | return &r->entry[0].counters; |
| Harald Welte | 1cef74d | 2001-01-05 15:22:59 +0000 | [diff] [blame] | 1669 | } |
| 1670 | |
| 1671 | int |
| 1672 | TC_ZERO_COUNTER(const IPT_CHAINLABEL chain, |
| 1673 | unsigned int rulenum, |
| 1674 | TC_HANDLE_T *handle) |
| 1675 | { |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1676 | struct chain_head *c; |
| 1677 | struct rule_head *r; |
| Harald Welte | 1cef74d | 2001-01-05 15:22:59 +0000 | [diff] [blame] | 1678 | |
| 1679 | iptc_fn = TC_ZERO_COUNTER; |
| 1680 | CHECK(*handle); |
| 1681 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1682 | if (!(c = iptcc_find_label(chain, *handle))) { |
| Harald Welte | 1cef74d | 2001-01-05 15:22:59 +0000 | [diff] [blame] | 1683 | errno = ENOENT; |
| 1684 | return 0; |
| 1685 | } |
| 1686 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1687 | if (!(r = iptcc_get_rule_num(c, rulenum))) { |
| Harald Welte | 0113fe7 | 2004-01-06 19:04:02 +0000 | [diff] [blame] | 1688 | errno = E2BIG; |
| 1689 | return 0; |
| 1690 | } |
| 1691 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1692 | if (r->counter_map.maptype == COUNTER_MAP_NORMAL_MAP) |
| 1693 | r->counter_map.maptype = COUNTER_MAP_ZEROED; |
| Harald Welte | 1cef74d | 2001-01-05 15:22:59 +0000 | [diff] [blame] | 1694 | |
| 1695 | set_changed(*handle); |
| 1696 | |
| 1697 | return 1; |
| 1698 | } |
| 1699 | |
| 1700 | int |
| 1701 | TC_SET_COUNTER(const IPT_CHAINLABEL chain, |
| 1702 | unsigned int rulenum, |
| 1703 | STRUCT_COUNTERS *counters, |
| 1704 | TC_HANDLE_T *handle) |
| 1705 | { |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1706 | struct chain_head *c; |
| 1707 | struct rule_head *r; |
| Harald Welte | 1cef74d | 2001-01-05 15:22:59 +0000 | [diff] [blame] | 1708 | STRUCT_ENTRY *e; |
| Harald Welte | 1cef74d | 2001-01-05 15:22:59 +0000 | [diff] [blame] | 1709 | |
| 1710 | iptc_fn = TC_SET_COUNTER; |
| 1711 | CHECK(*handle); |
| 1712 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1713 | if (!(c = iptcc_find_label(chain, *handle))) { |
| Harald Welte | 1cef74d | 2001-01-05 15:22:59 +0000 | [diff] [blame] | 1714 | errno = ENOENT; |
| 1715 | return 0; |
| 1716 | } |
| Harald Welte | 0113fe7 | 2004-01-06 19:04:02 +0000 | [diff] [blame] | 1717 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1718 | if (!(r = iptcc_get_rule_num(c, rulenum))) { |
| Harald Welte | 0113fe7 | 2004-01-06 19:04:02 +0000 | [diff] [blame] | 1719 | errno = E2BIG; |
| 1720 | return 0; |
| 1721 | } |
| 1722 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1723 | e = r->entry; |
| 1724 | r->counter_map.maptype = COUNTER_MAP_SET; |
| Harald Welte | 0113fe7 | 2004-01-06 19:04:02 +0000 | [diff] [blame] | 1725 | |
| 1726 | memcpy(&e->counters, counters, sizeof(STRUCT_COUNTERS)); |
| Harald Welte | 1cef74d | 2001-01-05 15:22:59 +0000 | [diff] [blame] | 1727 | |
| 1728 | set_changed(*handle); |
| 1729 | |
| 1730 | return 1; |
| 1731 | } |
| 1732 | |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1733 | /* Creates a new chain. */ |
| 1734 | /* To create a chain, create two rules: error node and unconditional |
| 1735 | * return. */ |
| 1736 | int |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1737 | TC_CREATE_CHAIN(const IPT_CHAINLABEL chain, TC_HANDLE_T *handle) |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1738 | { |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1739 | static struct chain_head *c; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1740 | |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1741 | iptc_fn = TC_CREATE_CHAIN; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1742 | |
| 1743 | /* find_label doesn't cover built-in targets: DROP, ACCEPT, |
| 1744 | QUEUE, RETURN. */ |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1745 | if (iptcc_find_label(chain, *handle) |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1746 | || strcmp(chain, LABEL_DROP) == 0 |
| 1747 | || strcmp(chain, LABEL_ACCEPT) == 0 |
| Rusty Russell | 67088e7 | 2000-05-10 01:18:57 +0000 | [diff] [blame] | 1748 | || strcmp(chain, LABEL_QUEUE) == 0 |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1749 | || strcmp(chain, LABEL_RETURN) == 0) { |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1750 | DEBUGP("Chain `%s' already exists\n", chain); |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1751 | errno = EEXIST; |
| 1752 | return 0; |
| 1753 | } |
| 1754 | |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1755 | if (strlen(chain)+1 > sizeof(IPT_CHAINLABEL)) { |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1756 | DEBUGP("Chain name `%s' too long\n", chain); |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1757 | errno = EINVAL; |
| 1758 | return 0; |
| 1759 | } |
| 1760 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1761 | c = iptcc_alloc_chain_head(chain, 0); |
| 1762 | if (!c) { |
| 1763 | DEBUGP("Cannot allocate memory for chain `%s'\n", chain); |
| 1764 | errno = ENOMEM; |
| 1765 | return 0; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1766 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1767 | } |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1768 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1769 | DEBUGP("Creating chain `%s'\n", chain); |
| 1770 | list_add_tail(&c->list, &(*handle)->chains); |
| Harald Welte | 0113fe7 | 2004-01-06 19:04:02 +0000 | [diff] [blame] | 1771 | |
| 1772 | set_changed(*handle); |
| 1773 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1774 | return 1; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1775 | } |
| 1776 | |
| 1777 | /* Get the number of references to this chain. */ |
| 1778 | int |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1779 | TC_GET_REFERENCES(unsigned int *ref, const IPT_CHAINLABEL chain, |
| 1780 | TC_HANDLE_T *handle) |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1781 | { |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1782 | struct chain_head *c; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1783 | |
| Derrik Pates | 664c0a3 | 2005-02-01 13:28:14 +0000 | [diff] [blame^] | 1784 | iptc_fn = TC_GET_REFERENCES; |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1785 | if (!(c = iptcc_find_label(chain, *handle))) { |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1786 | errno = ENOENT; |
| 1787 | return 0; |
| 1788 | } |
| 1789 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1790 | *ref = c->references; |
| 1791 | |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1792 | return 1; |
| 1793 | } |
| 1794 | |
| 1795 | /* Deletes a chain. */ |
| 1796 | int |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1797 | TC_DELETE_CHAIN(const IPT_CHAINLABEL chain, TC_HANDLE_T *handle) |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1798 | { |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1799 | unsigned int references; |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1800 | struct chain_head *c; |
| Rusty Russell | 7e53bf9 | 2000-03-20 07:03:28 +0000 | [diff] [blame] | 1801 | |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1802 | iptc_fn = TC_DELETE_CHAIN; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1803 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1804 | if (!(c = iptcc_find_label(chain, *handle))) { |
| 1805 | DEBUGP("cannot find chain `%s'\n", chain); |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1806 | errno = ENOENT; |
| 1807 | return 0; |
| 1808 | } |
| 1809 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1810 | if (TC_BUILTIN(chain, *handle)) { |
| 1811 | DEBUGP("cannot remove builtin chain `%s'\n", chain); |
| 1812 | errno = EINVAL; |
| 1813 | return 0; |
| 1814 | } |
| 1815 | |
| 1816 | if (!TC_GET_REFERENCES(&references, chain, handle)) { |
| 1817 | DEBUGP("cannot get references on chain `%s'\n", chain); |
| 1818 | return 0; |
| 1819 | } |
| 1820 | |
| 1821 | if (references > 0) { |
| 1822 | DEBUGP("chain `%s' still has references\n", chain); |
| 1823 | errno = EMLINK; |
| 1824 | return 0; |
| 1825 | } |
| 1826 | |
| 1827 | if (c->num_rules) { |
| 1828 | DEBUGP("chain `%s' is not empty\n", chain); |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1829 | errno = ENOTEMPTY; |
| 1830 | return 0; |
| 1831 | } |
| 1832 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1833 | /* If we are about to delete the chain that is the current |
| 1834 | * iterator, move chain iterator firward. */ |
| 1835 | if (c == (*handle)->chain_iterator_cur) |
| 1836 | iptcc_chain_iterator_advance(*handle); |
| Harald Welte | 0113fe7 | 2004-01-06 19:04:02 +0000 | [diff] [blame] | 1837 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1838 | list_del(&c->list); |
| 1839 | free(c); |
| Harald Welte | 0113fe7 | 2004-01-06 19:04:02 +0000 | [diff] [blame] | 1840 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1841 | DEBUGP("chain `%s' deleted\n", chain); |
| 1842 | |
| 1843 | set_changed(*handle); |
| 1844 | |
| 1845 | return 1; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1846 | } |
| 1847 | |
| 1848 | /* Renames a chain. */ |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1849 | int TC_RENAME_CHAIN(const IPT_CHAINLABEL oldname, |
| 1850 | const IPT_CHAINLABEL newname, |
| 1851 | TC_HANDLE_T *handle) |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1852 | { |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1853 | struct chain_head *c; |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1854 | iptc_fn = TC_RENAME_CHAIN; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1855 | |
| Harald Welte | 1de8046 | 2000-10-30 12:00:27 +0000 | [diff] [blame] | 1856 | /* find_label doesn't cover built-in targets: DROP, ACCEPT, |
| 1857 | QUEUE, RETURN. */ |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1858 | if (iptcc_find_label(newname, *handle) |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1859 | || strcmp(newname, LABEL_DROP) == 0 |
| 1860 | || strcmp(newname, LABEL_ACCEPT) == 0 |
| Harald Welte | 1de8046 | 2000-10-30 12:00:27 +0000 | [diff] [blame] | 1861 | || strcmp(newname, LABEL_QUEUE) == 0 |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1862 | || strcmp(newname, LABEL_RETURN) == 0) { |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1863 | errno = EEXIST; |
| 1864 | return 0; |
| 1865 | } |
| 1866 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1867 | if (!(c = iptcc_find_label(oldname, *handle)) |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1868 | || TC_BUILTIN(oldname, *handle)) { |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1869 | errno = ENOENT; |
| 1870 | return 0; |
| 1871 | } |
| 1872 | |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1873 | if (strlen(newname)+1 > sizeof(IPT_CHAINLABEL)) { |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1874 | errno = EINVAL; |
| 1875 | return 0; |
| 1876 | } |
| 1877 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1878 | strncpy(c->name, newname, sizeof(IPT_CHAINLABEL)); |
| 1879 | |
| Harald Welte | 0113fe7 | 2004-01-06 19:04:02 +0000 | [diff] [blame] | 1880 | set_changed(*handle); |
| 1881 | |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1882 | return 1; |
| 1883 | } |
| 1884 | |
| 1885 | /* Sets the policy on a built-in chain. */ |
| 1886 | int |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1887 | TC_SET_POLICY(const IPT_CHAINLABEL chain, |
| 1888 | const IPT_CHAINLABEL policy, |
| Harald Welte | 1cef74d | 2001-01-05 15:22:59 +0000 | [diff] [blame] | 1889 | STRUCT_COUNTERS *counters, |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1890 | TC_HANDLE_T *handle) |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1891 | { |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1892 | struct chain_head *c; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1893 | |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1894 | iptc_fn = TC_SET_POLICY; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1895 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1896 | if (!(c = iptcc_find_label(chain, *handle))) { |
| 1897 | DEBUGP("cannot find chain `%s'\n", chain); |
| 1898 | errno = ENOENT; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1899 | return 0; |
| 1900 | } |
| 1901 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1902 | if (!iptcc_is_builtin(c)) { |
| 1903 | DEBUGP("cannot set policy of userdefinedchain `%s'\n", chain); |
| 1904 | errno = ENOENT; |
| 1905 | return 0; |
| 1906 | } |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1907 | |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1908 | if (strcmp(policy, LABEL_ACCEPT) == 0) |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1909 | c->verdict = -NF_ACCEPT - 1; |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1910 | else if (strcmp(policy, LABEL_DROP) == 0) |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1911 | c->verdict = -NF_DROP - 1; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1912 | else { |
| 1913 | errno = EINVAL; |
| 1914 | return 0; |
| 1915 | } |
| Harald Welte | 1cef74d | 2001-01-05 15:22:59 +0000 | [diff] [blame] | 1916 | |
| Harald Welte | 1cef74d | 2001-01-05 15:22:59 +0000 | [diff] [blame] | 1917 | if (counters) { |
| 1918 | /* set byte and packet counters */ |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1919 | memcpy(&c->counters, counters, sizeof(STRUCT_COUNTERS)); |
| 1920 | c->counter_map.maptype = COUNTER_MAP_SET; |
| Harald Welte | 1cef74d | 2001-01-05 15:22:59 +0000 | [diff] [blame] | 1921 | } else { |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1922 | c->counter_map.maptype = COUNTER_MAP_NOMAP; |
| Harald Welte | 1cef74d | 2001-01-05 15:22:59 +0000 | [diff] [blame] | 1923 | } |
| 1924 | |
| Rusty Russell | 175f641 | 2000-03-24 09:32:20 +0000 | [diff] [blame] | 1925 | set_changed(*handle); |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1926 | |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1927 | return 1; |
| 1928 | } |
| 1929 | |
| 1930 | /* Without this, on gcc 2.7.2.3, we get: |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1931 | libiptc.c: In function `TC_COMMIT': |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1932 | libiptc.c:833: fixed or forbidden register was spilled. |
| 1933 | This may be due to a compiler bug or to impossible asm |
| 1934 | statements or clauses. |
| 1935 | */ |
| 1936 | static void |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1937 | subtract_counters(STRUCT_COUNTERS *answer, |
| 1938 | const STRUCT_COUNTERS *a, |
| 1939 | const STRUCT_COUNTERS *b) |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1940 | { |
| 1941 | answer->pcnt = a->pcnt - b->pcnt; |
| 1942 | answer->bcnt = a->bcnt - b->bcnt; |
| 1943 | } |
| 1944 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1945 | |
| 1946 | static void counters_nomap(STRUCT_COUNTERS_INFO *newcounters, |
| 1947 | unsigned int index) |
| 1948 | { |
| 1949 | newcounters->counters[index] = ((STRUCT_COUNTERS) { 0, 0}); |
| 1950 | DEBUGP_C("NOMAP => zero\n"); |
| 1951 | } |
| 1952 | |
| 1953 | static void counters_normal_map(STRUCT_COUNTERS_INFO *newcounters, |
| 1954 | STRUCT_REPLACE *repl, |
| 1955 | unsigned int index, |
| 1956 | unsigned int mappos) |
| 1957 | { |
| 1958 | /* Original read: X. |
| 1959 | * Atomic read on replacement: X + Y. |
| 1960 | * Currently in kernel: Z. |
| 1961 | * Want in kernel: X + Y + Z. |
| 1962 | * => Add in X + Y |
| 1963 | * => Add in replacement read. |
| 1964 | */ |
| 1965 | newcounters->counters[index] = repl->counters[mappos]; |
| 1966 | DEBUGP_C("NORMAL_MAP => mappos %u \n", mappos); |
| 1967 | } |
| 1968 | |
| 1969 | static void counters_map_zeroed(STRUCT_COUNTERS_INFO *newcounters, |
| 1970 | STRUCT_REPLACE *repl, |
| 1971 | unsigned int index, |
| 1972 | unsigned int mappos, |
| 1973 | STRUCT_COUNTERS *counters) |
| 1974 | { |
| 1975 | /* Original read: X. |
| 1976 | * Atomic read on replacement: X + Y. |
| 1977 | * Currently in kernel: Z. |
| 1978 | * Want in kernel: Y + Z. |
| 1979 | * => Add in Y. |
| 1980 | * => Add in (replacement read - original read). |
| 1981 | */ |
| 1982 | subtract_counters(&newcounters->counters[index], |
| 1983 | &repl->counters[mappos], |
| 1984 | counters); |
| 1985 | DEBUGP_C("ZEROED => mappos %u\n", mappos); |
| 1986 | } |
| 1987 | |
| 1988 | static void counters_map_set(STRUCT_COUNTERS_INFO *newcounters, |
| 1989 | unsigned int index, |
| 1990 | STRUCT_COUNTERS *counters) |
| 1991 | { |
| 1992 | /* Want to set counter (iptables-restore) */ |
| 1993 | |
| 1994 | memcpy(&newcounters->counters[index], counters, |
| 1995 | sizeof(STRUCT_COUNTERS)); |
| 1996 | |
| 1997 | DEBUGP_C("SET\n"); |
| 1998 | } |
| 1999 | |
| 2000 | |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2001 | int |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 2002 | TC_COMMIT(TC_HANDLE_T *handle) |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2003 | { |
| 2004 | /* Replace, then map back the counters. */ |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 2005 | STRUCT_REPLACE *repl; |
| 2006 | STRUCT_COUNTERS_INFO *newcounters; |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2007 | struct chain_head *c; |
| 2008 | int ret; |
| Martin Josefsson | 841e4ae | 2003-05-02 15:30:11 +0000 | [diff] [blame] | 2009 | size_t counterlen; |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2010 | int new_number; |
| 2011 | unsigned int new_size; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2012 | |
| Derrik Pates | 664c0a3 | 2005-02-01 13:28:14 +0000 | [diff] [blame^] | 2013 | iptc_fn = TC_COMMIT; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2014 | CHECK(*handle); |
| Martin Josefsson | 841e4ae | 2003-05-02 15:30:11 +0000 | [diff] [blame] | 2015 | |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2016 | /* Don't commit if nothing changed. */ |
| 2017 | if (!(*handle)->changed) |
| 2018 | goto finished; |
| 2019 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2020 | new_number = iptcc_compile_table_prep(*handle, &new_size); |
| 2021 | if (new_number < 0) { |
| 2022 | errno = ENOMEM; |
| 2023 | return 0; |
| 2024 | } |
| 2025 | |
| 2026 | repl = malloc(sizeof(*repl) + new_size); |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2027 | if (!repl) { |
| 2028 | errno = ENOMEM; |
| 2029 | return 0; |
| 2030 | } |
| Martin Josefsson | ad3b4f9 | 2004-09-22 22:04:07 +0000 | [diff] [blame] | 2031 | memset(repl, 0, sizeof(*repl) + new_size); |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2032 | |
| Rusty Russell | e45c713 | 2004-12-16 13:21:44 +0000 | [diff] [blame] | 2033 | #if 0 |
| 2034 | TC_DUMP_ENTRIES(*handle); |
| 2035 | #endif |
| 2036 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2037 | counterlen = sizeof(STRUCT_COUNTERS_INFO) |
| 2038 | + sizeof(STRUCT_COUNTERS) * new_number; |
| Derrik Pates | 664c0a3 | 2005-02-01 13:28:14 +0000 | [diff] [blame^] | 2039 | memset(repl, 0, sizeof(*repl) + (*handle)->entries->size); |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2040 | |
| 2041 | /* These are the old counters we will get from kernel */ |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 2042 | repl->counters = malloc(sizeof(STRUCT_COUNTERS) |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2043 | * (*handle)->info.num_entries); |
| 2044 | if (!repl->counters) { |
| 2045 | free(repl); |
| 2046 | errno = ENOMEM; |
| 2047 | return 0; |
| 2048 | } |
| Derrik Pates | 664c0a3 | 2005-02-01 13:28:14 +0000 | [diff] [blame^] | 2049 | memset(repl->counters, 0, sizeof(STRUCT_COUNTERS) |
| 2050 | * (*handle)->info.num_entries); |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2051 | /* These are the counters we're going to put back, later. */ |
| 2052 | newcounters = malloc(counterlen); |
| 2053 | if (!newcounters) { |
| 2054 | free(repl->counters); |
| 2055 | free(repl); |
| 2056 | errno = ENOMEM; |
| 2057 | return 0; |
| 2058 | } |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2059 | memset(newcounters, 0, counterlen); |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2060 | |
| 2061 | strcpy(repl->name, (*handle)->info.name); |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2062 | repl->num_entries = new_number; |
| 2063 | repl->size = new_size; |
| 2064 | |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2065 | repl->num_counters = (*handle)->info.num_entries; |
| 2066 | repl->valid_hooks = (*handle)->info.valid_hooks; |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2067 | |
| 2068 | DEBUGP("num_entries=%u, size=%u, num_counters=%u\n", |
| 2069 | repl->num_entries, repl->size, repl->num_counters); |
| 2070 | |
| 2071 | ret = iptcc_compile_table(*handle, repl); |
| 2072 | if (ret < 0) { |
| 2073 | errno = ret; |
| 2074 | free(repl->counters); |
| 2075 | free(repl); |
| 2076 | return 0; |
| 2077 | } |
| 2078 | |
| 2079 | |
| 2080 | #ifdef IPTC_DEBUG2 |
| 2081 | { |
| 2082 | int fd = open("/tmp/libiptc-so_set_replace.blob", |
| 2083 | O_CREAT|O_WRONLY); |
| 2084 | if (fd >= 0) { |
| 2085 | write(fd, repl, sizeof(*repl) + repl->size); |
| 2086 | close(fd); |
| 2087 | } |
| 2088 | } |
| 2089 | #endif |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2090 | |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 2091 | if (setsockopt(sockfd, TC_IPPROTO, SO_SET_REPLACE, repl, |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2092 | sizeof(*repl) + repl->size) < 0) { |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2093 | free(repl->counters); |
| 2094 | free(repl); |
| 2095 | free(newcounters); |
| 2096 | return 0; |
| 2097 | } |
| 2098 | |
| 2099 | /* Put counters back. */ |
| 2100 | strcpy(newcounters->name, (*handle)->info.name); |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2101 | newcounters->num_counters = new_number; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2102 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2103 | list_for_each_entry(c, &(*handle)->chains, list) { |
| 2104 | struct rule_head *r; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2105 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2106 | /* Builtin chains have their own counters */ |
| 2107 | if (iptcc_is_builtin(c)) { |
| 2108 | DEBUGP("counter for chain-index %u: ", c->foot_index); |
| 2109 | switch(c->counter_map.maptype) { |
| 2110 | case COUNTER_MAP_NOMAP: |
| 2111 | counters_nomap(newcounters, c->foot_index); |
| 2112 | break; |
| 2113 | case COUNTER_MAP_NORMAL_MAP: |
| 2114 | counters_normal_map(newcounters, repl, |
| 2115 | c->foot_index, |
| 2116 | c->counter_map.mappos); |
| 2117 | break; |
| 2118 | case COUNTER_MAP_ZEROED: |
| 2119 | counters_map_zeroed(newcounters, repl, |
| 2120 | c->foot_index, |
| 2121 | c->counter_map.mappos, |
| 2122 | &c->counters); |
| 2123 | break; |
| 2124 | case COUNTER_MAP_SET: |
| 2125 | counters_map_set(newcounters, c->foot_index, |
| 2126 | &c->counters); |
| 2127 | break; |
| 2128 | } |
| 2129 | } |
| Harald Welte | 1cef74d | 2001-01-05 15:22:59 +0000 | [diff] [blame] | 2130 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2131 | list_for_each_entry(r, &c->rules, list) { |
| 2132 | DEBUGP("counter for index %u: ", r->index); |
| 2133 | switch (r->counter_map.maptype) { |
| 2134 | case COUNTER_MAP_NOMAP: |
| 2135 | counters_nomap(newcounters, r->index); |
| 2136 | break; |
| Harald Welte | 1cef74d | 2001-01-05 15:22:59 +0000 | [diff] [blame] | 2137 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2138 | case COUNTER_MAP_NORMAL_MAP: |
| 2139 | counters_normal_map(newcounters, repl, |
| 2140 | r->index, |
| 2141 | r->counter_map.mappos); |
| 2142 | break; |
| Harald Welte | 1cef74d | 2001-01-05 15:22:59 +0000 | [diff] [blame] | 2143 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2144 | case COUNTER_MAP_ZEROED: |
| 2145 | counters_map_zeroed(newcounters, repl, |
| 2146 | r->index, |
| 2147 | r->counter_map.mappos, |
| 2148 | &r->entry->counters); |
| 2149 | break; |
| 2150 | |
| 2151 | case COUNTER_MAP_SET: |
| 2152 | counters_map_set(newcounters, r->index, |
| 2153 | &r->entry->counters); |
| 2154 | break; |
| 2155 | } |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2156 | } |
| 2157 | } |
| Rusty Russell | 62527ce | 2000-09-04 09:45:54 +0000 | [diff] [blame] | 2158 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2159 | |
| Rusty Russell | 62527ce | 2000-09-04 09:45:54 +0000 | [diff] [blame] | 2160 | #ifdef KERNEL_64_USERSPACE_32 |
| 2161 | { |
| 2162 | /* Kernel will think that pointer should be 64-bits, and get |
| 2163 | padding. So we accomodate here (assumption: alignment of |
| 2164 | `counters' is on 64-bit boundary). */ |
| 2165 | u_int64_t *kernptr = (u_int64_t *)&newcounters->counters; |
| 2166 | if ((unsigned long)&newcounters->counters % 8 != 0) { |
| 2167 | fprintf(stderr, |
| 2168 | "counters alignment incorrect! Mail rusty!\n"); |
| 2169 | abort(); |
| 2170 | } |
| 2171 | *kernptr = newcounters->counters; |
| Rusty Russell | 54c307e | 2000-09-04 06:47:28 +0000 | [diff] [blame] | 2172 | } |
| Rusty Russell | 62527ce | 2000-09-04 09:45:54 +0000 | [diff] [blame] | 2173 | #endif /* KERNEL_64_USERSPACE_32 */ |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2174 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2175 | #ifdef IPTC_DEBUG2 |
| 2176 | { |
| 2177 | int fd = open("/tmp/libiptc-so_set_add_counters.blob", |
| 2178 | O_CREAT|O_WRONLY); |
| 2179 | if (fd >= 0) { |
| 2180 | write(fd, newcounters, counterlen); |
| 2181 | close(fd); |
| 2182 | } |
| 2183 | } |
| 2184 | #endif |
| 2185 | |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 2186 | if (setsockopt(sockfd, TC_IPPROTO, SO_SET_ADD_COUNTERS, |
| 2187 | newcounters, counterlen) < 0) { |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2188 | free(repl->counters); |
| 2189 | free(repl); |
| 2190 | free(newcounters); |
| 2191 | return 0; |
| 2192 | } |
| 2193 | |
| 2194 | free(repl->counters); |
| 2195 | free(repl); |
| 2196 | free(newcounters); |
| 2197 | |
| 2198 | finished: |
| Martin Josefsson | 841e4ae | 2003-05-02 15:30:11 +0000 | [diff] [blame] | 2199 | TC_FREE(handle); |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2200 | return 1; |
| 2201 | } |
| 2202 | |
| 2203 | /* Get raw socket. */ |
| 2204 | int |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 2205 | TC_GET_RAW_SOCKET() |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2206 | { |
| 2207 | return sockfd; |
| 2208 | } |
| 2209 | |
| 2210 | /* Translates errno numbers into more human-readable form than strerror. */ |
| 2211 | const char * |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 2212 | TC_STRERROR(int err) |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2213 | { |
| 2214 | unsigned int i; |
| 2215 | struct table_struct { |
| 2216 | void *fn; |
| 2217 | int err; |
| 2218 | const char *message; |
| 2219 | } table [] = |
| Harald Welte | 4ccfa63 | 2001-07-30 15:12:43 +0000 | [diff] [blame] | 2220 | { { TC_INIT, EPERM, "Permission denied (you must be root)" }, |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 2221 | { TC_INIT, EINVAL, "Module is wrong version" }, |
| Harald Welte | 4ccfa63 | 2001-07-30 15:12:43 +0000 | [diff] [blame] | 2222 | { TC_INIT, ENOENT, |
| 2223 | "Table does not exist (do you need to insmod?)" }, |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 2224 | { TC_DELETE_CHAIN, ENOTEMPTY, "Chain is not empty" }, |
| 2225 | { TC_DELETE_CHAIN, EINVAL, "Can't delete built-in chain" }, |
| 2226 | { TC_DELETE_CHAIN, EMLINK, |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2227 | "Can't delete chain with references left" }, |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 2228 | { TC_CREATE_CHAIN, EEXIST, "Chain already exists" }, |
| 2229 | { TC_INSERT_ENTRY, E2BIG, "Index of insertion too big" }, |
| 2230 | { TC_REPLACE_ENTRY, E2BIG, "Index of replacement too big" }, |
| 2231 | { TC_DELETE_NUM_ENTRY, E2BIG, "Index of deletion too big" }, |
| Harald Welte | 1cef74d | 2001-01-05 15:22:59 +0000 | [diff] [blame] | 2232 | { TC_READ_COUNTER, E2BIG, "Index of counter too big" }, |
| 2233 | { TC_ZERO_COUNTER, E2BIG, "Index of counter too big" }, |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 2234 | { TC_INSERT_ENTRY, ELOOP, "Loop found in table" }, |
| 2235 | { TC_INSERT_ENTRY, EINVAL, "Target problem" }, |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2236 | /* EINVAL for CHECK probably means bad interface. */ |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 2237 | { TC_CHECK_PACKET, EINVAL, |
| Marc Boucher | c826499 | 2000-04-22 22:34:44 +0000 | [diff] [blame] | 2238 | "Bad arguments (does that interface exist?)" }, |
| Harald Welte | 4ccfa63 | 2001-07-30 15:12:43 +0000 | [diff] [blame] | 2239 | { TC_CHECK_PACKET, ENOSYS, |
| 2240 | "Checking will most likely never get implemented" }, |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2241 | /* ENOENT for DELETE probably means no matching rule */ |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 2242 | { TC_DELETE_ENTRY, ENOENT, |
| Marc Boucher | c826499 | 2000-04-22 22:34:44 +0000 | [diff] [blame] | 2243 | "Bad rule (does a matching rule exist in that chain?)" }, |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 2244 | { TC_SET_POLICY, ENOENT, |
| Marc Boucher | c826499 | 2000-04-22 22:34:44 +0000 | [diff] [blame] | 2245 | "Bad built-in chain name" }, |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 2246 | { TC_SET_POLICY, EINVAL, |
| Marc Boucher | c826499 | 2000-04-22 22:34:44 +0000 | [diff] [blame] | 2247 | "Bad policy name" }, |
| Harald Welte | 4ccfa63 | 2001-07-30 15:12:43 +0000 | [diff] [blame] | 2248 | |
| 2249 | { NULL, 0, "Incompatible with this kernel" }, |
| 2250 | { NULL, ENOPROTOOPT, "iptables who? (do you need to insmod?)" }, |
| 2251 | { NULL, ENOSYS, "Will be implemented real soon. I promise ;)" }, |
| 2252 | { NULL, ENOMEM, "Memory allocation problem" }, |
| 2253 | { NULL, ENOENT, "No chain/target/match by that name" }, |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2254 | }; |
| 2255 | |
| 2256 | for (i = 0; i < sizeof(table)/sizeof(struct table_struct); i++) { |
| 2257 | if ((!table[i].fn || table[i].fn == iptc_fn) |
| 2258 | && table[i].err == err) |
| 2259 | return table[i].message; |
| 2260 | } |
| 2261 | |
| 2262 | return strerror(err); |
| 2263 | } |