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