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