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