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