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