| 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> |
| Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame] | 29 | #include <xtables.h> |
| Stephane Ouellette | 7cd0028 | 2004-05-14 08:21:06 +0000 | [diff] [blame] | 30 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 31 | #include "linux_list.h" |
| 32 | |
| 33 | //#define IPTC_DEBUG2 1 |
| 34 | |
| 35 | #ifdef IPTC_DEBUG2 |
| 36 | #include <fcntl.h> |
| 37 | #define DEBUGP(x, args...) fprintf(stderr, "%s: " x, __FUNCTION__, ## args) |
| 38 | #define DEBUGP_C(x, args...) fprintf(stderr, x, ## args) |
| 39 | #else |
| 40 | #define DEBUGP(x, args...) |
| 41 | #define DEBUGP_C(x, args...) |
| 42 | #endif |
| 43 | |
| Jesper Dangaard Brouer | 01444da | 2008-01-15 17:18:15 +0000 | [diff] [blame] | 44 | #ifdef DEBUG |
| 45 | #define debug(x, args...) fprintf(stderr, x, ## args) |
| 46 | #else |
| 47 | #define debug(x, args...) |
| 48 | #endif |
| 49 | |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 50 | static int sockfd = -1; |
| Derrik Pates | 664c0a3 | 2005-02-01 13:28:14 +0000 | [diff] [blame] | 51 | static int sockfd_use = 0; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 52 | static void *iptc_fn = NULL; |
| 53 | |
| Patrick McHardy | 0b63936 | 2007-09-08 16:00:01 +0000 | [diff] [blame] | 54 | static const char *hooknames[] = { |
| 55 | [HOOK_PRE_ROUTING] = "PREROUTING", |
| 56 | [HOOK_LOCAL_IN] = "INPUT", |
| 57 | [HOOK_FORWARD] = "FORWARD", |
| 58 | [HOOK_LOCAL_OUT] = "OUTPUT", |
| 59 | [HOOK_POST_ROUTING] = "POSTROUTING", |
| Rusty Russell | 10758b7 | 2000-09-14 07:37:33 +0000 | [diff] [blame] | 60 | #ifdef HOOK_DROPPING |
| Patrick McHardy | 0b63936 | 2007-09-08 16:00:01 +0000 | [diff] [blame] | 61 | [HOOK_DROPPING] = "DROPPING" |
| Rusty Russell | 10758b7 | 2000-09-14 07:37:33 +0000 | [diff] [blame] | 62 | #endif |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 63 | }; |
| 64 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 65 | /* Convenience structures */ |
| 66 | struct ipt_error_target |
| 67 | { |
| 68 | STRUCT_ENTRY_TARGET t; |
| 69 | char error[TABLE_MAXNAMELEN]; |
| 70 | }; |
| 71 | |
| 72 | struct chain_head; |
| 73 | struct rule_head; |
| 74 | |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 75 | struct counter_map |
| 76 | { |
| 77 | enum { |
| 78 | COUNTER_MAP_NOMAP, |
| 79 | COUNTER_MAP_NORMAL_MAP, |
| Harald Welte | 1cef74d | 2001-01-05 15:22:59 +0000 | [diff] [blame] | 80 | COUNTER_MAP_ZEROED, |
| 81 | COUNTER_MAP_SET |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 82 | } maptype; |
| 83 | unsigned int mappos; |
| 84 | }; |
| 85 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 86 | enum iptcc_rule_type { |
| 87 | IPTCC_R_STANDARD, /* standard target (ACCEPT, ...) */ |
| 88 | IPTCC_R_MODULE, /* extension module (SNAT, ...) */ |
| 89 | IPTCC_R_FALLTHROUGH, /* fallthrough rule */ |
| 90 | IPTCC_R_JUMP, /* jump to other chain */ |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 91 | }; |
| 92 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 93 | struct rule_head |
| Rusty Russell | 30fd6e5 | 2000-04-23 09:16:06 +0000 | [diff] [blame] | 94 | { |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 95 | struct list_head list; |
| 96 | struct chain_head *chain; |
| 97 | struct counter_map counter_map; |
| 98 | |
| 99 | unsigned int index; /* index (needed for counter_map) */ |
| 100 | unsigned int offset; /* offset in rule blob */ |
| 101 | |
| 102 | enum iptcc_rule_type type; |
| 103 | struct chain_head *jump; /* jump target, if IPTCC_R_JUMP */ |
| 104 | |
| 105 | unsigned int size; /* size of entry data */ |
| 106 | STRUCT_ENTRY entry[0]; |
| 107 | }; |
| 108 | |
| 109 | struct chain_head |
| 110 | { |
| 111 | struct list_head list; |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 112 | char name[TABLE_MAXNAMELEN]; |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 113 | unsigned int hooknum; /* hook number+1 if builtin */ |
| 114 | unsigned int references; /* how many jumps reference us */ |
| 115 | int verdict; /* verdict if builtin */ |
| 116 | |
| 117 | STRUCT_COUNTERS counters; /* per-chain counters */ |
| 118 | struct counter_map counter_map; |
| 119 | |
| 120 | unsigned int num_rules; /* number of rules in list */ |
| 121 | struct list_head rules; /* list of rules */ |
| 122 | |
| 123 | unsigned int index; /* index (needed for jump resolval) */ |
| 124 | unsigned int head_offset; /* offset in rule blob */ |
| 125 | unsigned int foot_index; /* index (needed for counter_map) */ |
| 126 | unsigned int foot_offset; /* offset in rule blob */ |
| Rusty Russell | 30fd6e5 | 2000-04-23 09:16:06 +0000 | [diff] [blame] | 127 | }; |
| 128 | |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 129 | STRUCT_TC_HANDLE |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 130 | { |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 131 | int changed; /* Have changes been made? */ |
| 132 | |
| 133 | struct list_head chains; |
| 134 | |
| 135 | struct chain_head *chain_iterator_cur; |
| 136 | struct rule_head *rule_iterator_cur; |
| 137 | |
| Jesper Dangaard Brouer | 48bde40 | 2008-01-15 17:06:48 +0000 | [diff] [blame] | 138 | unsigned int num_chains; /* number of user defined chains */ |
| 139 | |
| Jesper Dangaard Brouer | 01444da | 2008-01-15 17:18:15 +0000 | [diff] [blame] | 140 | struct chain_head **chain_index; /* array for fast chain list access*/ |
| 141 | unsigned int chain_index_sz;/* size of chain index array */ |
| 142 | |
| Jesper Dangaard Brouer | 4bae3f1 | 2008-07-03 20:31:42 +0200 | [diff] [blame^] | 143 | int sorted_offsets; /* if chains are received sorted from kernel, |
| 144 | * then the offsets are also sorted. Says if its |
| 145 | * possible to bsearch offsets using chain_index. |
| 146 | */ |
| 147 | |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 148 | STRUCT_GETINFO info; |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 149 | STRUCT_GET_ENTRIES *entries; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 150 | }; |
| 151 | |
| Jesper Dangaard Brouer | 4bae3f1 | 2008-07-03 20:31:42 +0200 | [diff] [blame^] | 152 | enum bsearch_type { |
| 153 | BSEARCH_NAME, /* Binary search after chain name */ |
| 154 | BSEARCH_OFFSET, /* Binary search based on offset */ |
| 155 | }; |
| 156 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 157 | /* allocate a new chain head for the cache */ |
| 158 | static struct chain_head *iptcc_alloc_chain_head(const char *name, int hooknum) |
| 159 | { |
| 160 | struct chain_head *c = malloc(sizeof(*c)); |
| 161 | if (!c) |
| 162 | return NULL; |
| 163 | memset(c, 0, sizeof(*c)); |
| 164 | |
| 165 | strncpy(c->name, name, TABLE_MAXNAMELEN); |
| 166 | c->hooknum = hooknum; |
| 167 | INIT_LIST_HEAD(&c->rules); |
| 168 | |
| 169 | return c; |
| 170 | } |
| 171 | |
| 172 | /* allocate and initialize a new rule for the cache */ |
| 173 | static struct rule_head *iptcc_alloc_rule(struct chain_head *c, unsigned int size) |
| 174 | { |
| 175 | struct rule_head *r = malloc(sizeof(*r)+size); |
| 176 | if (!r) |
| 177 | return NULL; |
| 178 | memset(r, 0, sizeof(*r)); |
| 179 | |
| 180 | r->chain = c; |
| 181 | r->size = size; |
| 182 | |
| 183 | return r; |
| 184 | } |
| 185 | |
| 186 | /* notify us that the ruleset has been modified by the user */ |
| Jesper Dangaard Brouer | 9109398 | 2008-01-15 17:01:58 +0000 | [diff] [blame] | 187 | static inline void |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 188 | set_changed(TC_HANDLE_T h) |
| Rusty Russell | 175f641 | 2000-03-24 09:32:20 +0000 | [diff] [blame] | 189 | { |
| Rusty Russell | 175f641 | 2000-03-24 09:32:20 +0000 | [diff] [blame] | 190 | h->changed = 1; |
| 191 | } |
| 192 | |
| Harald Welte | 380ba5f | 2002-02-13 16:19:55 +0000 | [diff] [blame] | 193 | #ifdef IPTC_DEBUG |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 194 | static void do_check(TC_HANDLE_T h, unsigned int line); |
| Rusty Russell | 849779c | 2000-04-23 15:51:51 +0000 | [diff] [blame] | 195 | #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] | 196 | #else |
| 197 | #define CHECK(h) |
| 198 | #endif |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 199 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 200 | |
| 201 | /********************************************************************** |
| 202 | * iptc blob utility functions (iptcb_*) |
| 203 | **********************************************************************/ |
| 204 | |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 205 | static inline int |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 206 | iptcb_get_number(const STRUCT_ENTRY *i, |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 207 | const STRUCT_ENTRY *seek, |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 208 | unsigned int *pos) |
| 209 | { |
| 210 | if (i == seek) |
| 211 | return 1; |
| 212 | (*pos)++; |
| 213 | return 0; |
| 214 | } |
| 215 | |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 216 | static inline int |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 217 | iptcb_get_entry_n(STRUCT_ENTRY *i, |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 218 | unsigned int number, |
| 219 | unsigned int *pos, |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 220 | STRUCT_ENTRY **pe) |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 221 | { |
| 222 | if (*pos == number) { |
| 223 | *pe = i; |
| 224 | return 1; |
| 225 | } |
| 226 | (*pos)++; |
| 227 | return 0; |
| 228 | } |
| 229 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 230 | static inline STRUCT_ENTRY * |
| 231 | iptcb_get_entry(TC_HANDLE_T h, unsigned int offset) |
| 232 | { |
| 233 | return (STRUCT_ENTRY *)((char *)h->entries->entrytable + offset); |
| 234 | } |
| 235 | |
| 236 | static unsigned int |
| 237 | iptcb_entry2index(const TC_HANDLE_T h, const STRUCT_ENTRY *seek) |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 238 | { |
| 239 | unsigned int pos = 0; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 240 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 241 | if (ENTRY_ITERATE(h->entries->entrytable, h->entries->size, |
| 242 | iptcb_get_number, seek, &pos) == 0) { |
| 243 | fprintf(stderr, "ERROR: offset %u not an entry!\n", |
| 244 | (unsigned int)((char *)seek - (char *)h->entries->entrytable)); |
| 245 | abort(); |
| 246 | } |
| 247 | return pos; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 248 | } |
| 249 | |
| Harald Welte | 0113fe7 | 2004-01-06 19:04:02 +0000 | [diff] [blame] | 250 | static inline STRUCT_ENTRY * |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 251 | iptcb_offset2entry(TC_HANDLE_T h, unsigned int offset) |
| Harald Welte | 0113fe7 | 2004-01-06 19:04:02 +0000 | [diff] [blame] | 252 | { |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 253 | return (STRUCT_ENTRY *) ((void *)h->entries->entrytable+offset); |
| Harald Welte | 0113fe7 | 2004-01-06 19:04:02 +0000 | [diff] [blame] | 254 | } |
| 255 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 256 | |
| Harald Welte | 0113fe7 | 2004-01-06 19:04:02 +0000 | [diff] [blame] | 257 | static inline unsigned long |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 258 | iptcb_entry2offset(const TC_HANDLE_T h, const STRUCT_ENTRY *e) |
| Harald Welte | 0113fe7 | 2004-01-06 19:04:02 +0000 | [diff] [blame] | 259 | { |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 260 | return (void *)e - (void *)h->entries->entrytable; |
| Harald Welte | 3ea8f40 | 2003-06-23 18:25:59 +0000 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | static inline unsigned int |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 264 | iptcb_offset2index(const TC_HANDLE_T h, unsigned int offset) |
| Harald Welte | 3ea8f40 | 2003-06-23 18:25:59 +0000 | [diff] [blame] | 265 | { |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 266 | return iptcb_entry2index(h, iptcb_offset2entry(h, offset)); |
| 267 | } |
| 268 | |
| 269 | /* Returns 0 if not hook entry, else hooknumber + 1 */ |
| 270 | static inline unsigned int |
| 271 | iptcb_ent_is_hook_entry(STRUCT_ENTRY *e, TC_HANDLE_T h) |
| 272 | { |
| 273 | unsigned int i; |
| 274 | |
| 275 | for (i = 0; i < NUMHOOKS; i++) { |
| 276 | if ((h->info.valid_hooks & (1 << i)) |
| 277 | && iptcb_get_entry(h, h->info.hook_entry[i]) == e) |
| 278 | return i+1; |
| 279 | } |
| 280 | return 0; |
| Harald Welte | 3ea8f40 | 2003-06-23 18:25:59 +0000 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 284 | /********************************************************************** |
| Jesper Dangaard Brouer | 01444da | 2008-01-15 17:18:15 +0000 | [diff] [blame] | 285 | * Chain index (cache utility) functions |
| 286 | ********************************************************************** |
| 287 | * The chain index is an array with pointers into the chain list, with |
| 288 | * CHAIN_INDEX_BUCKET_LEN spacing. This facilitates the ability to |
| 289 | * speedup chain list searching, by find a more optimal starting |
| 290 | * points when searching the linked list. |
| 291 | * |
| 292 | * The starting point can be found fast by using a binary search of |
| 293 | * the chain index. Thus, reducing the previous search complexity of |
| 294 | * O(n) to O(log(n/k) + k) where k is CHAIN_INDEX_BUCKET_LEN. |
| 295 | * |
| 296 | * A nice property of the chain index, is that the "bucket" list |
| 297 | * length is max CHAIN_INDEX_BUCKET_LEN (when just build, inserts will |
| 298 | * change this). Oppose to hashing, where the "bucket" list length can |
| 299 | * vary a lot. |
| 300 | */ |
| 301 | #ifndef CHAIN_INDEX_BUCKET_LEN |
| 302 | #define CHAIN_INDEX_BUCKET_LEN 40 |
| 303 | #endif |
| 304 | |
| 305 | /* Another nice property of the chain index is that inserting/creating |
| 306 | * chains in chain list don't change the correctness of the chain |
| 307 | * index, it only causes longer lists in the buckets. |
| 308 | * |
| 309 | * To mitigate the performance penalty of longer bucket lists and the |
| 310 | * penalty of rebuilding, the chain index is rebuild only when |
| 311 | * CHAIN_INDEX_INSERT_MAX chains has been added. |
| 312 | */ |
| 313 | #ifndef CHAIN_INDEX_INSERT_MAX |
| 314 | #define CHAIN_INDEX_INSERT_MAX 355 |
| 315 | #endif |
| 316 | |
| 317 | static inline unsigned int iptcc_is_builtin(struct chain_head *c); |
| 318 | |
| Jesper Dangaard Brouer | 01444da | 2008-01-15 17:18:15 +0000 | [diff] [blame] | 319 | /* Use binary search in the chain index array, to find a chain_head |
| 320 | * pointer closest to the place of the searched name element. |
| 321 | * |
| 322 | * Notes that, binary search (obviously) requires that the chain list |
| 323 | * is sorted by name. |
| Jesper Dangaard Brouer | 4bae3f1 | 2008-07-03 20:31:42 +0200 | [diff] [blame^] | 324 | * |
| 325 | * The not so obvious: The chain index array, is actually both sorted |
| 326 | * by name and offset, at the same time!. This is only true because, |
| 327 | * chain are stored sorted in the kernel (as we pushed it in sorted). |
| 328 | * |
| Jesper Dangaard Brouer | 01444da | 2008-01-15 17:18:15 +0000 | [diff] [blame] | 329 | */ |
| 330 | static struct list_head * |
| Jesper Dangaard Brouer | 4bae3f1 | 2008-07-03 20:31:42 +0200 | [diff] [blame^] | 331 | __iptcc_bsearch_chain_index(const char *name, unsigned int offset, |
| 332 | unsigned int *idx, TC_HANDLE_T handle, |
| 333 | enum bsearch_type type) |
| Jesper Dangaard Brouer | 01444da | 2008-01-15 17:18:15 +0000 | [diff] [blame] | 334 | { |
| 335 | unsigned int pos, end; |
| 336 | int res; |
| 337 | |
| 338 | struct list_head *list_pos; |
| 339 | list_pos=&handle->chains; |
| 340 | |
| 341 | /* Check for empty array, e.g. no user defined chains */ |
| 342 | if (handle->chain_index_sz == 0) { |
| 343 | debug("WARNING: handle->chain_index_sz == 0\n"); |
| 344 | return list_pos; |
| 345 | } |
| 346 | |
| 347 | /* Init */ |
| 348 | end = handle->chain_index_sz; |
| 349 | pos = end / 2; |
| 350 | |
| Jesper Dangaard Brouer | 4bae3f1 | 2008-07-03 20:31:42 +0200 | [diff] [blame^] | 351 | debug("bsearch Find chain:%s (pos:%d end:%d) (offset:%d)\n", |
| 352 | name, pos, end, offset); |
| Jesper Dangaard Brouer | 01444da | 2008-01-15 17:18:15 +0000 | [diff] [blame] | 353 | |
| 354 | /* Loop */ |
| 355 | loop: |
| 356 | if (!handle->chain_index[pos]) { |
| 357 | fprintf(stderr, "ERROR: NULL pointer chain_index[%d]\n", pos); |
| 358 | return &handle->chains; /* Be safe, return orig start pos */ |
| 359 | } |
| 360 | |
| Jesper Dangaard Brouer | 4bae3f1 | 2008-07-03 20:31:42 +0200 | [diff] [blame^] | 361 | debug("bsearch Index[%d] name:%s ", |
| 362 | pos, handle->chain_index[pos]->name); |
| 363 | |
| 364 | /* Support for different compare functions */ |
| 365 | switch (type) { |
| 366 | case BSEARCH_NAME: |
| 367 | res = strcmp(name, handle->chain_index[pos]->name); |
| 368 | break; |
| 369 | case BSEARCH_OFFSET: |
| 370 | debug("head_offset:[%d] foot_offset:[%d] ", |
| 371 | handle->chain_index[pos]->head_offset, |
| 372 | handle->chain_index[pos]->foot_offset); |
| 373 | res = offset - handle->chain_index[pos]->head_offset; |
| 374 | break; |
| 375 | default: |
| 376 | fprintf(stderr, "ERROR: %d not a valid bsearch type\n", |
| 377 | type); |
| 378 | abort(); |
| 379 | break; |
| 380 | } |
| 381 | debug("res:%d ", res); |
| 382 | |
| 383 | |
| Jesper Dangaard Brouer | 01444da | 2008-01-15 17:18:15 +0000 | [diff] [blame] | 384 | list_pos = &handle->chain_index[pos]->list; |
| Jan Engelhardt | dbb7754 | 2008-02-11 00:33:30 +0100 | [diff] [blame] | 385 | *idx = pos; |
| Jesper Dangaard Brouer | 01444da | 2008-01-15 17:18:15 +0000 | [diff] [blame] | 386 | |
| Jesper Dangaard Brouer | 01444da | 2008-01-15 17:18:15 +0000 | [diff] [blame] | 387 | if (res == 0) { /* Found element, by direct hit */ |
| 388 | debug("[found] Direct hit pos:%d end:%d\n", pos, end); |
| 389 | return list_pos; |
| 390 | } else if (res < 0) { /* Too far, jump back */ |
| 391 | end = pos; |
| 392 | pos = pos / 2; |
| 393 | |
| 394 | /* Exit case: First element of array */ |
| 395 | if (end == 0) { |
| 396 | debug("[found] Reached first array elem (end%d)\n",end); |
| 397 | return list_pos; |
| 398 | } |
| 399 | debug("jump back to pos:%d (end:%d)\n", pos, end); |
| 400 | goto loop; |
| 401 | } else if (res > 0 ){ /* Not far enough, jump forward */ |
| 402 | |
| 403 | /* Exit case: Last element of array */ |
| 404 | if (pos == handle->chain_index_sz-1) { |
| 405 | debug("[found] Last array elem (end:%d)\n", end); |
| 406 | return list_pos; |
| 407 | } |
| 408 | |
| 409 | /* Exit case: Next index less, thus elem in this list section */ |
| Jesper Dangaard Brouer | 4bae3f1 | 2008-07-03 20:31:42 +0200 | [diff] [blame^] | 410 | switch (type) { |
| 411 | case BSEARCH_NAME: |
| 412 | res = strcmp(name, handle->chain_index[pos+1]->name); |
| 413 | break; |
| 414 | case BSEARCH_OFFSET: |
| 415 | res = offset - handle->chain_index[pos+1]->head_offset; |
| 416 | break; |
| 417 | } |
| 418 | |
| Jesper Dangaard Brouer | 01444da | 2008-01-15 17:18:15 +0000 | [diff] [blame] | 419 | if (res < 0) { |
| 420 | debug("[found] closest list (end:%d)\n", end); |
| 421 | return list_pos; |
| 422 | } |
| 423 | |
| 424 | pos = (pos+end)/2; |
| 425 | debug("jump forward to pos:%d (end:%d)\n", pos, end); |
| 426 | goto loop; |
| 427 | } |
| 428 | |
| 429 | return list_pos; |
| 430 | } |
| 431 | |
| Jesper Dangaard Brouer | 4bae3f1 | 2008-07-03 20:31:42 +0200 | [diff] [blame^] | 432 | /* Wrapper for string chain name based bsearch */ |
| 433 | static struct list_head * |
| 434 | iptcc_bsearch_chain_index(const char *name, unsigned int *idx, |
| 435 | TC_HANDLE_T handle) |
| 436 | { |
| 437 | return __iptcc_bsearch_chain_index(name, 0, idx, handle, BSEARCH_NAME); |
| 438 | } |
| 439 | |
| 440 | |
| 441 | /* Wrapper for offset chain based bsearch */ |
| 442 | static struct list_head * |
| 443 | iptcc_bsearch_chain_offset(unsigned int offset, unsigned int *idx, |
| 444 | TC_HANDLE_T handle) |
| 445 | { |
| 446 | struct list_head *pos; |
| 447 | |
| 448 | /* If chains were not received sorted from kernel, then the |
| 449 | * offset bsearch is not possible. |
| 450 | */ |
| 451 | if (!handle->sorted_offsets) |
| 452 | pos = handle->chains.next; |
| 453 | else |
| 454 | pos = __iptcc_bsearch_chain_index(NULL, offset, idx, handle, |
| 455 | BSEARCH_OFFSET); |
| 456 | return pos; |
| 457 | } |
| 458 | |
| 459 | |
| Jesper Dangaard Brouer | 01444da | 2008-01-15 17:18:15 +0000 | [diff] [blame] | 460 | #ifdef DEBUG |
| 461 | /* Trivial linear search of chain index. Function used for verifying |
| 462 | the output of bsearch function */ |
| 463 | static struct list_head * |
| 464 | iptcc_linearly_search_chain_index(const char *name, TC_HANDLE_T handle) |
| 465 | { |
| 466 | unsigned int i=0; |
| 467 | int res=0; |
| 468 | |
| 469 | struct list_head *list_pos; |
| 470 | list_pos = &handle->chains; |
| 471 | |
| 472 | if (handle->chain_index_sz) |
| 473 | list_pos = &handle->chain_index[0]->list; |
| 474 | |
| 475 | /* Linearly walk of chain index array */ |
| 476 | |
| 477 | for (i=0; i < handle->chain_index_sz; i++) { |
| 478 | if (handle->chain_index[i]) { |
| 479 | res = strcmp(handle->chain_index[i]->name, name); |
| 480 | if (res > 0) |
| 481 | break; // One step too far |
| 482 | list_pos = &handle->chain_index[i]->list; |
| 483 | if (res == 0) |
| 484 | break; // Direct hit |
| 485 | } |
| 486 | } |
| 487 | |
| 488 | return list_pos; |
| 489 | } |
| 490 | #endif |
| 491 | |
| 492 | static int iptcc_chain_index_alloc(TC_HANDLE_T h) |
| 493 | { |
| 494 | unsigned int list_length = CHAIN_INDEX_BUCKET_LEN; |
| 495 | unsigned int array_elems; |
| 496 | unsigned int array_mem; |
| 497 | |
| 498 | /* Allocate memory for the chain index array */ |
| 499 | array_elems = (h->num_chains / list_length) + |
| 500 | (h->num_chains % list_length ? 1 : 0); |
| 501 | array_mem = sizeof(h->chain_index) * array_elems; |
| 502 | |
| 503 | debug("Alloc Chain index, elems:%d mem:%d bytes\n", |
| 504 | array_elems, array_mem); |
| 505 | |
| 506 | h->chain_index = malloc(array_mem); |
| 507 | if (!h->chain_index) { |
| 508 | h->chain_index_sz = 0; |
| 509 | return -ENOMEM; |
| 510 | } |
| 511 | memset(h->chain_index, 0, array_mem); |
| 512 | h->chain_index_sz = array_elems; |
| 513 | |
| 514 | return 1; |
| 515 | } |
| 516 | |
| 517 | static void iptcc_chain_index_free(TC_HANDLE_T h) |
| 518 | { |
| 519 | h->chain_index_sz = 0; |
| 520 | free(h->chain_index); |
| 521 | } |
| 522 | |
| 523 | |
| 524 | #ifdef DEBUG |
| 525 | static void iptcc_chain_index_dump(TC_HANDLE_T h) |
| 526 | { |
| 527 | unsigned int i = 0; |
| 528 | |
| 529 | /* Dump: contents of chain index array */ |
| 530 | for (i=0; i < h->chain_index_sz; i++) { |
| 531 | if (h->chain_index[i]) { |
| 532 | fprintf(stderr, "Chain index[%d].name: %s\n", |
| 533 | i, h->chain_index[i]->name); |
| 534 | } |
| 535 | } |
| 536 | } |
| 537 | #endif |
| 538 | |
| 539 | /* Build the chain index */ |
| 540 | static int iptcc_chain_index_build(TC_HANDLE_T h) |
| 541 | { |
| 542 | unsigned int list_length = CHAIN_INDEX_BUCKET_LEN; |
| 543 | unsigned int chains = 0; |
| 544 | unsigned int cindex = 0; |
| 545 | struct chain_head *c; |
| 546 | |
| 547 | /* Build up the chain index array here */ |
| 548 | debug("Building chain index\n"); |
| 549 | |
| 550 | debug("Number of user defined chains:%d bucket_sz:%d array_sz:%d\n", |
| 551 | h->num_chains, list_length, h->chain_index_sz); |
| 552 | |
| 553 | if (h->chain_index_sz == 0) |
| 554 | return 0; |
| 555 | |
| 556 | list_for_each_entry(c, &h->chains, list) { |
| 557 | |
| 558 | /* Issue: The index array needs to start after the |
| 559 | * builtin chains, as they are not sorted */ |
| 560 | if (!iptcc_is_builtin(c)) { |
| 561 | cindex=chains / list_length; |
| 562 | |
| 563 | /* Safe guard, break out on array limit, this |
| 564 | * is useful if chains are added and array is |
| 565 | * rebuild, without realloc of memory. */ |
| 566 | if (cindex >= h->chain_index_sz) |
| 567 | break; |
| 568 | |
| 569 | if ((chains % list_length)== 0) { |
| 570 | debug("\nIndex[%d] Chains:", cindex); |
| 571 | h->chain_index[cindex] = c; |
| 572 | } |
| 573 | chains++; |
| 574 | } |
| 575 | debug("%s, ", c->name); |
| 576 | } |
| 577 | debug("\n"); |
| 578 | |
| 579 | return 1; |
| 580 | } |
| 581 | |
| 582 | static int iptcc_chain_index_rebuild(TC_HANDLE_T h) |
| 583 | { |
| 584 | debug("REBUILD chain index array\n"); |
| 585 | iptcc_chain_index_free(h); |
| 586 | if ((iptcc_chain_index_alloc(h)) < 0) |
| 587 | return -ENOMEM; |
| 588 | iptcc_chain_index_build(h); |
| 589 | return 1; |
| 590 | } |
| 591 | |
| 592 | /* Delete chain (pointer) from index array. Removing an element from |
| 593 | * the chain list only affects the chain index array, if the chain |
| 594 | * index points-to/uses that list pointer. |
| 595 | * |
| 596 | * There are different strategies, the simple and safe is to rebuild |
| 597 | * the chain index every time. The more advanced is to update the |
| 598 | * array index to point to the next element, but that requires some |
| 599 | * house keeping and boundry checks. The advanced is implemented, as |
| 600 | * the simple approach behaves badly when all chains are deleted |
| 601 | * because list_for_each processing will always hit the first chain |
| 602 | * index, thus causing a rebuild for every chain. |
| 603 | */ |
| 604 | static int iptcc_chain_index_delete_chain(struct chain_head *c, TC_HANDLE_T h) |
| 605 | { |
| 606 | struct list_head *index_ptr, *index_ptr2, *next; |
| 607 | struct chain_head *c2; |
| Jan Engelhardt | dbb7754 | 2008-02-11 00:33:30 +0100 | [diff] [blame] | 608 | unsigned int idx, idx2; |
| Jesper Dangaard Brouer | 01444da | 2008-01-15 17:18:15 +0000 | [diff] [blame] | 609 | |
| Jan Engelhardt | dbb7754 | 2008-02-11 00:33:30 +0100 | [diff] [blame] | 610 | index_ptr = iptcc_bsearch_chain_index(c->name, &idx, h); |
| Jesper Dangaard Brouer | 01444da | 2008-01-15 17:18:15 +0000 | [diff] [blame] | 611 | |
| 612 | debug("Del chain[%s] c->list:%p index_ptr:%p\n", |
| 613 | c->name, &c->list, index_ptr); |
| 614 | |
| 615 | /* Save the next pointer */ |
| 616 | next = c->list.next; |
| 617 | list_del(&c->list); |
| 618 | |
| 619 | if (index_ptr == &c->list) { /* Chain used as index ptr */ |
| 620 | |
| 621 | /* See if its possible to avoid a rebuild, by shifting |
| 622 | * to next pointer. Its possible if the next pointer |
| 623 | * is located in the same index bucket. |
| 624 | */ |
| 625 | c2 = list_entry(next, struct chain_head, list); |
| Jan Engelhardt | dbb7754 | 2008-02-11 00:33:30 +0100 | [diff] [blame] | 626 | index_ptr2 = iptcc_bsearch_chain_index(c2->name, &idx2, h); |
| 627 | if (idx != idx2) { |
| Jesper Dangaard Brouer | 01444da | 2008-01-15 17:18:15 +0000 | [diff] [blame] | 628 | /* Rebuild needed */ |
| 629 | return iptcc_chain_index_rebuild(h); |
| 630 | } else { |
| 631 | /* Avoiding rebuild */ |
| 632 | debug("Update cindex[%d] with next ptr name:[%s]\n", |
| Jan Engelhardt | dbb7754 | 2008-02-11 00:33:30 +0100 | [diff] [blame] | 633 | idx, c2->name); |
| 634 | h->chain_index[idx]=c2; |
| Jesper Dangaard Brouer | 01444da | 2008-01-15 17:18:15 +0000 | [diff] [blame] | 635 | return 0; |
| 636 | } |
| 637 | } |
| 638 | return 0; |
| 639 | } |
| 640 | |
| 641 | |
| 642 | /********************************************************************** |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 643 | * iptc cache utility functions (iptcc_*) |
| 644 | **********************************************************************/ |
| Harald Welte | 0113fe7 | 2004-01-06 19:04:02 +0000 | [diff] [blame] | 645 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 646 | /* Is the given chain builtin (1) or user-defined (0) */ |
| Jesper Dangaard Brouer | 9109398 | 2008-01-15 17:01:58 +0000 | [diff] [blame] | 647 | static inline unsigned int iptcc_is_builtin(struct chain_head *c) |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 648 | { |
| 649 | return (c->hooknum ? 1 : 0); |
| 650 | } |
| 651 | |
| 652 | /* Get a specific rule within a chain */ |
| 653 | static struct rule_head *iptcc_get_rule_num(struct chain_head *c, |
| 654 | unsigned int rulenum) |
| 655 | { |
| 656 | struct rule_head *r; |
| 657 | unsigned int num = 0; |
| 658 | |
| 659 | list_for_each_entry(r, &c->rules, list) { |
| 660 | num++; |
| 661 | if (num == rulenum) |
| 662 | return r; |
| 663 | } |
| 664 | return NULL; |
| 665 | } |
| 666 | |
| Martin Josefsson | a5616dc | 2004-10-24 22:27:31 +0000 | [diff] [blame] | 667 | /* Get a specific rule within a chain backwards */ |
| 668 | static struct rule_head *iptcc_get_rule_num_reverse(struct chain_head *c, |
| 669 | unsigned int rulenum) |
| 670 | { |
| 671 | struct rule_head *r; |
| 672 | unsigned int num = 0; |
| 673 | |
| 674 | list_for_each_entry_reverse(r, &c->rules, list) { |
| 675 | num++; |
| 676 | if (num == rulenum) |
| 677 | return r; |
| 678 | } |
| 679 | return NULL; |
| 680 | } |
| 681 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 682 | /* Returns chain head if found, otherwise NULL. */ |
| 683 | static struct chain_head * |
| 684 | iptcc_find_chain_by_offset(TC_HANDLE_T handle, unsigned int offset) |
| 685 | { |
| 686 | struct list_head *pos; |
| Jesper Dangaard Brouer | 4bae3f1 | 2008-07-03 20:31:42 +0200 | [diff] [blame^] | 687 | struct list_head *list_start_pos; |
| 688 | unsigned int i; |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 689 | |
| 690 | if (list_empty(&handle->chains)) |
| 691 | return NULL; |
| 692 | |
| Jesper Dangaard Brouer | 4bae3f1 | 2008-07-03 20:31:42 +0200 | [diff] [blame^] | 693 | /* Find a smart place to start the search */ |
| 694 | list_start_pos = iptcc_bsearch_chain_offset(offset, &i, handle); |
| 695 | |
| 696 | /* Note that iptcc_bsearch_chain_offset() skips builtin |
| 697 | * chains, but this function is only used for finding jump |
| 698 | * targets, and a buildin chain is not a valid jump target */ |
| 699 | |
| 700 | debug("Offset:[%u] starting search at index:[%u]\n", offset, i); |
| 701 | // list_for_each(pos, &handle->chains) { |
| 702 | list_for_each(pos, list_start_pos->prev) { |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 703 | struct chain_head *c = list_entry(pos, struct chain_head, list); |
| Jesper Dangaard Brouer | 4bae3f1 | 2008-07-03 20:31:42 +0200 | [diff] [blame^] | 704 | debug("."); |
| 705 | if (offset >= c->head_offset && offset <= c->foot_offset) { |
| 706 | debug("Offset search found chain:[%s]\n", c->name); |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 707 | return c; |
| Jesper Dangaard Brouer | 4bae3f1 | 2008-07-03 20:31:42 +0200 | [diff] [blame^] | 708 | } |
| Harald Welte | 0113fe7 | 2004-01-06 19:04:02 +0000 | [diff] [blame] | 709 | } |
| 710 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 711 | return NULL; |
| Harald Welte | 0113fe7 | 2004-01-06 19:04:02 +0000 | [diff] [blame] | 712 | } |
| Jesper Dangaard Brouer | 01444da | 2008-01-15 17:18:15 +0000 | [diff] [blame] | 713 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 714 | /* Returns chain head if found, otherwise NULL. */ |
| 715 | static struct chain_head * |
| 716 | iptcc_find_label(const char *name, TC_HANDLE_T handle) |
| 717 | { |
| 718 | struct list_head *pos; |
| Jesper Dangaard Brouer | 01444da | 2008-01-15 17:18:15 +0000 | [diff] [blame] | 719 | struct list_head *list_start_pos; |
| 720 | unsigned int i=0; |
| 721 | int res; |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 722 | |
| 723 | if (list_empty(&handle->chains)) |
| 724 | return NULL; |
| 725 | |
| Jesper Dangaard Brouer | 01444da | 2008-01-15 17:18:15 +0000 | [diff] [blame] | 726 | /* First look at builtin chains */ |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 727 | list_for_each(pos, &handle->chains) { |
| 728 | struct chain_head *c = list_entry(pos, struct chain_head, list); |
| Jesper Dangaard Brouer | 01444da | 2008-01-15 17:18:15 +0000 | [diff] [blame] | 729 | if (!iptcc_is_builtin(c)) |
| 730 | break; |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 731 | if (!strcmp(c->name, name)) |
| 732 | return c; |
| 733 | } |
| 734 | |
| Jesper Dangaard Brouer | 01444da | 2008-01-15 17:18:15 +0000 | [diff] [blame] | 735 | /* Find a smart place to start the search via chain index */ |
| 736 | //list_start_pos = iptcc_linearly_search_chain_index(name, handle); |
| 737 | list_start_pos = iptcc_bsearch_chain_index(name, &i, handle); |
| 738 | |
| 739 | /* Handel if bsearch bails out early */ |
| 740 | if (list_start_pos == &handle->chains) { |
| 741 | list_start_pos = pos; |
| 742 | } |
| 743 | #ifdef DEBUG |
| 744 | else { |
| 745 | /* Verify result of bsearch against linearly index search */ |
| 746 | struct list_head *test_pos; |
| 747 | struct chain_head *test_c, *tmp_c; |
| 748 | test_pos = iptcc_linearly_search_chain_index(name, handle); |
| 749 | if (list_start_pos != test_pos) { |
| 750 | debug("BUG in chain_index search\n"); |
| 751 | test_c=list_entry(test_pos, struct chain_head,list); |
| 752 | tmp_c =list_entry(list_start_pos,struct chain_head,list); |
| 753 | debug("Verify search found:\n"); |
| 754 | debug(" Chain:%s\n", test_c->name); |
| 755 | debug("BSearch found:\n"); |
| 756 | debug(" Chain:%s\n", tmp_c->name); |
| 757 | exit(42); |
| 758 | } |
| 759 | } |
| 760 | #endif |
| 761 | |
| 762 | /* Initial/special case, no user defined chains */ |
| 763 | if (handle->num_chains == 0) |
| 764 | return NULL; |
| 765 | |
| 766 | /* Start searching through the chain list */ |
| 767 | list_for_each(pos, list_start_pos->prev) { |
| 768 | struct chain_head *c = list_entry(pos, struct chain_head, list); |
| 769 | res = strcmp(c->name, name); |
| 770 | debug("List search name:%s == %s res:%d\n", name, c->name, res); |
| 771 | if (res==0) |
| 772 | return c; |
| 773 | |
| 774 | /* We can stop earlier as we know list is sorted */ |
| 775 | if (res>0 && !iptcc_is_builtin(c)) { /* Walked too far*/ |
| 776 | debug(" Not in list, walked too far, sorted list\n"); |
| 777 | return NULL; |
| 778 | } |
| 779 | |
| 780 | /* Stop on wrap around, if list head is reached */ |
| 781 | if (pos == &handle->chains) { |
| 782 | debug("Stop, list head reached\n"); |
| 783 | return NULL; |
| 784 | } |
| 785 | } |
| 786 | |
| 787 | debug("List search NOT found name:%s\n", name); |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 788 | return NULL; |
| 789 | } |
| 790 | |
| 791 | /* called when rule is to be removed from cache */ |
| 792 | static void iptcc_delete_rule(struct rule_head *r) |
| 793 | { |
| 794 | DEBUGP("deleting rule %p (offset %u)\n", r, r->offset); |
| 795 | /* clean up reference count of called chain */ |
| 796 | if (r->type == IPTCC_R_JUMP |
| 797 | && r->jump) |
| 798 | r->jump->references--; |
| 799 | |
| 800 | list_del(&r->list); |
| 801 | free(r); |
| 802 | } |
| 803 | |
| 804 | |
| 805 | /********************************************************************** |
| 806 | * RULESET PARSER (blob -> cache) |
| 807 | **********************************************************************/ |
| 808 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 809 | /* Delete policy rule of previous chain, since cache doesn't contain |
| 810 | * chain policy rules. |
| 811 | * WARNING: This function has ugly design and relies on a lot of context, only |
| 812 | * to be called from specific places within the parser */ |
| 813 | static int __iptcc_p_del_policy(TC_HANDLE_T h, unsigned int num) |
| 814 | { |
| 815 | if (h->chain_iterator_cur) { |
| 816 | /* policy rule is last rule */ |
| 817 | struct rule_head *pr = (struct rule_head *) |
| 818 | h->chain_iterator_cur->rules.prev; |
| 819 | |
| 820 | /* save verdict */ |
| 821 | h->chain_iterator_cur->verdict = |
| 822 | *(int *)GET_TARGET(pr->entry)->data; |
| 823 | |
| 824 | /* save counter and counter_map information */ |
| 825 | h->chain_iterator_cur->counter_map.maptype = |
| 826 | COUNTER_MAP_NORMAL_MAP; |
| 827 | h->chain_iterator_cur->counter_map.mappos = num-1; |
| 828 | memcpy(&h->chain_iterator_cur->counters, &pr->entry->counters, |
| 829 | sizeof(h->chain_iterator_cur->counters)); |
| 830 | |
| 831 | /* foot_offset points to verdict rule */ |
| 832 | h->chain_iterator_cur->foot_index = num; |
| 833 | h->chain_iterator_cur->foot_offset = pr->offset; |
| 834 | |
| 835 | /* delete rule from cache */ |
| 836 | iptcc_delete_rule(pr); |
| Martin Josefsson | 8d1b38a | 2004-09-22 21:00:19 +0000 | [diff] [blame] | 837 | h->chain_iterator_cur->num_rules--; |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 838 | |
| 839 | return 1; |
| 840 | } |
| 841 | return 0; |
| 842 | } |
| 843 | |
| Harald Welte | ec30b6c | 2005-02-01 16:45:56 +0000 | [diff] [blame] | 844 | /* alphabetically insert a chain into the list */ |
| 845 | static inline void iptc_insert_chain(TC_HANDLE_T h, struct chain_head *c) |
| 846 | { |
| 847 | struct chain_head *tmp; |
| Jesper Dangaard Brouer | 01444da | 2008-01-15 17:18:15 +0000 | [diff] [blame] | 848 | struct list_head *list_start_pos; |
| 849 | unsigned int i=1; |
| 850 | |
| 851 | /* Find a smart place to start the insert search */ |
| 852 | list_start_pos = iptcc_bsearch_chain_index(c->name, &i, h); |
| 853 | |
| 854 | /* Handle the case, where chain.name is smaller than index[0] */ |
| 855 | if (i==0 && strcmp(c->name, h->chain_index[0]->name) <= 0) { |
| 856 | h->chain_index[0] = c; /* Update chain index head */ |
| 857 | list_start_pos = h->chains.next; |
| 858 | debug("Update chain_index[0] with %s\n", c->name); |
| 859 | } |
| 860 | |
| 861 | /* Handel if bsearch bails out early */ |
| 862 | if (list_start_pos == &h->chains) { |
| 863 | list_start_pos = h->chains.next; |
| 864 | } |
| Harald Welte | ec30b6c | 2005-02-01 16:45:56 +0000 | [diff] [blame] | 865 | |
| Olaf Rempel | 9d3ed77 | 2005-03-04 23:08:30 +0000 | [diff] [blame] | 866 | /* sort only user defined chains */ |
| 867 | if (!c->hooknum) { |
| Jesper Dangaard Brouer | 01444da | 2008-01-15 17:18:15 +0000 | [diff] [blame] | 868 | list_for_each_entry(tmp, list_start_pos->prev, list) { |
| Robert de Barth | feca057 | 2005-07-31 07:04:59 +0000 | [diff] [blame] | 869 | if (!tmp->hooknum && strcmp(c->name, tmp->name) <= 0) { |
| Olaf Rempel | 9d3ed77 | 2005-03-04 23:08:30 +0000 | [diff] [blame] | 870 | list_add(&c->list, tmp->list.prev); |
| 871 | return; |
| 872 | } |
| Jesper Dangaard Brouer | 01444da | 2008-01-15 17:18:15 +0000 | [diff] [blame] | 873 | |
| 874 | /* Stop if list head is reached */ |
| 875 | if (&tmp->list == &h->chains) { |
| 876 | debug("Insert, list head reached add to tail\n"); |
| 877 | break; |
| 878 | } |
| Harald Welte | ec30b6c | 2005-02-01 16:45:56 +0000 | [diff] [blame] | 879 | } |
| 880 | } |
| 881 | |
| 882 | /* survived till end of list: add at tail */ |
| 883 | list_add_tail(&c->list, &h->chains); |
| 884 | } |
| 885 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 886 | /* Another ugly helper function split out of cache_add_entry to make it less |
| 887 | * spaghetti code */ |
| 888 | static void __iptcc_p_add_chain(TC_HANDLE_T h, struct chain_head *c, |
| 889 | unsigned int offset, unsigned int *num) |
| 890 | { |
| Jesper Dangaard Brouer | 1336451 | 2007-12-12 15:20:42 +0000 | [diff] [blame] | 891 | struct list_head *tail = h->chains.prev; |
| 892 | struct chain_head *ctail; |
| 893 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 894 | __iptcc_p_del_policy(h, *num); |
| 895 | |
| 896 | c->head_offset = offset; |
| 897 | c->index = *num; |
| 898 | |
| Jesper Dangaard Brouer | 1336451 | 2007-12-12 15:20:42 +0000 | [diff] [blame] | 899 | /* Chains from kernel are already sorted, as they are inserted |
| 900 | * sorted. But there exists an issue when shifting to 1.4.0 |
| 901 | * from an older version, as old versions allow last created |
| 902 | * chain to be unsorted. |
| 903 | */ |
| 904 | if (iptcc_is_builtin(c)) /* Only user defined chains are sorted*/ |
| 905 | list_add_tail(&c->list, &h->chains); |
| 906 | else { |
| 907 | ctail = list_entry(tail, struct chain_head, list); |
| Jesper Dangaard Brouer | 4bae3f1 | 2008-07-03 20:31:42 +0200 | [diff] [blame^] | 908 | |
| Jesper Dangaard Brouer | 526d3e1 | 2008-07-03 20:29:34 +0200 | [diff] [blame] | 909 | if (strcmp(c->name, ctail->name) > 0 || |
| 910 | iptcc_is_builtin(ctail)) |
| Jesper Dangaard Brouer | 1336451 | 2007-12-12 15:20:42 +0000 | [diff] [blame] | 911 | list_add_tail(&c->list, &h->chains);/* Already sorted*/ |
| Jesper Dangaard Brouer | 4bae3f1 | 2008-07-03 20:31:42 +0200 | [diff] [blame^] | 912 | else { |
| Jesper Dangaard Brouer | 1336451 | 2007-12-12 15:20:42 +0000 | [diff] [blame] | 913 | iptc_insert_chain(h, c);/* Was not sorted */ |
| Jesper Dangaard Brouer | 4bae3f1 | 2008-07-03 20:31:42 +0200 | [diff] [blame^] | 914 | |
| 915 | /* Notice, if chains were not received sorted |
| 916 | * from kernel, then an offset bsearch is no |
| 917 | * longer valid. |
| 918 | */ |
| 919 | h->sorted_offsets = 0; |
| 920 | |
| 921 | debug("NOTICE: chain:[%s] was NOT sorted(ctail:%s)\n", |
| 922 | c->name, ctail->name); |
| 923 | } |
| Jesper Dangaard Brouer | 1336451 | 2007-12-12 15:20:42 +0000 | [diff] [blame] | 924 | } |
| Jesper Dangaard Brouer | d8cb787 | 2007-11-28 08:40:26 +0000 | [diff] [blame] | 925 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 926 | h->chain_iterator_cur = c; |
| 927 | } |
| 928 | |
| 929 | /* main parser function: add an entry from the blob to the cache */ |
| 930 | static int cache_add_entry(STRUCT_ENTRY *e, |
| 931 | TC_HANDLE_T h, |
| 932 | STRUCT_ENTRY **prev, |
| 933 | unsigned int *num) |
| 934 | { |
| 935 | unsigned int builtin; |
| 936 | unsigned int offset = (char *)e - (char *)h->entries->entrytable; |
| 937 | |
| 938 | DEBUGP("entering..."); |
| 939 | |
| 940 | /* Last entry ("policy rule"). End it.*/ |
| 941 | if (iptcb_entry2offset(h,e) + e->next_offset == h->entries->size) { |
| 942 | /* This is the ERROR node at the end of the chain */ |
| 943 | DEBUGP_C("%u:%u: end of table:\n", *num, offset); |
| 944 | |
| 945 | __iptcc_p_del_policy(h, *num); |
| 946 | |
| 947 | h->chain_iterator_cur = NULL; |
| 948 | goto out_inc; |
| 949 | } |
| 950 | |
| 951 | /* We know this is the start of a new chain if it's an ERROR |
| 952 | * target, or a hook entry point */ |
| 953 | |
| 954 | if (strcmp(GET_TARGET(e)->u.user.name, ERROR_TARGET) == 0) { |
| 955 | struct chain_head *c = |
| 956 | iptcc_alloc_chain_head((const char *)GET_TARGET(e)->data, 0); |
| 957 | DEBUGP_C("%u:%u:new userdefined chain %s: %p\n", *num, offset, |
| 958 | (char *)c->name, c); |
| 959 | if (!c) { |
| 960 | errno = -ENOMEM; |
| 961 | return -1; |
| 962 | } |
| Jesper Dangaard Brouer | 48bde40 | 2008-01-15 17:06:48 +0000 | [diff] [blame] | 963 | h->num_chains++; /* New user defined chain */ |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 964 | |
| 965 | __iptcc_p_add_chain(h, c, offset, num); |
| 966 | |
| 967 | } else if ((builtin = iptcb_ent_is_hook_entry(e, h)) != 0) { |
| 968 | struct chain_head *c = |
| 969 | iptcc_alloc_chain_head((char *)hooknames[builtin-1], |
| 970 | builtin); |
| 971 | DEBUGP_C("%u:%u new builtin chain: %p (rules=%p)\n", |
| 972 | *num, offset, c, &c->rules); |
| 973 | if (!c) { |
| 974 | errno = -ENOMEM; |
| 975 | return -1; |
| 976 | } |
| 977 | |
| 978 | c->hooknum = builtin; |
| 979 | |
| 980 | __iptcc_p_add_chain(h, c, offset, num); |
| 981 | |
| 982 | /* FIXME: this is ugly. */ |
| 983 | goto new_rule; |
| 984 | } else { |
| 985 | /* has to be normal rule */ |
| 986 | struct rule_head *r; |
| 987 | new_rule: |
| 988 | |
| 989 | if (!(r = iptcc_alloc_rule(h->chain_iterator_cur, |
| 990 | e->next_offset))) { |
| 991 | errno = ENOMEM; |
| 992 | return -1; |
| 993 | } |
| 994 | DEBUGP_C("%u:%u normal rule: %p: ", *num, offset, r); |
| 995 | |
| 996 | r->index = *num; |
| 997 | r->offset = offset; |
| 998 | memcpy(r->entry, e, e->next_offset); |
| 999 | r->counter_map.maptype = COUNTER_MAP_NORMAL_MAP; |
| 1000 | r->counter_map.mappos = r->index; |
| 1001 | |
| 1002 | /* handling of jumps, etc. */ |
| 1003 | if (!strcmp(GET_TARGET(e)->u.user.name, STANDARD_TARGET)) { |
| 1004 | STRUCT_STANDARD_TARGET *t; |
| 1005 | |
| 1006 | t = (STRUCT_STANDARD_TARGET *)GET_TARGET(e); |
| 1007 | if (t->target.u.target_size |
| 1008 | != ALIGN(sizeof(STRUCT_STANDARD_TARGET))) { |
| 1009 | errno = EINVAL; |
| 1010 | return -1; |
| 1011 | } |
| 1012 | |
| 1013 | if (t->verdict < 0) { |
| 1014 | DEBUGP_C("standard, verdict=%d\n", t->verdict); |
| 1015 | r->type = IPTCC_R_STANDARD; |
| 1016 | } else if (t->verdict == r->offset+e->next_offset) { |
| 1017 | DEBUGP_C("fallthrough\n"); |
| 1018 | r->type = IPTCC_R_FALLTHROUGH; |
| 1019 | } else { |
| 1020 | DEBUGP_C("jump, target=%u\n", t->verdict); |
| 1021 | r->type = IPTCC_R_JUMP; |
| 1022 | /* Jump target fixup has to be deferred |
| 1023 | * until second pass, since we migh not |
| 1024 | * yet have parsed the target */ |
| 1025 | } |
| Martin Josefsson | 52c3802 | 2004-09-22 19:39:40 +0000 | [diff] [blame] | 1026 | } else { |
| 1027 | DEBUGP_C("module, target=%s\n", GET_TARGET(e)->u.user.name); |
| 1028 | r->type = IPTCC_R_MODULE; |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1029 | } |
| 1030 | |
| 1031 | list_add_tail(&r->list, &h->chain_iterator_cur->rules); |
| Martin Josefsson | 8d1b38a | 2004-09-22 21:00:19 +0000 | [diff] [blame] | 1032 | h->chain_iterator_cur->num_rules++; |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1033 | } |
| 1034 | out_inc: |
| 1035 | (*num)++; |
| 1036 | return 0; |
| 1037 | } |
| 1038 | |
| 1039 | |
| 1040 | /* parse an iptables blob into it's pieces */ |
| 1041 | static int parse_table(TC_HANDLE_T h) |
| 1042 | { |
| 1043 | STRUCT_ENTRY *prev; |
| 1044 | unsigned int num = 0; |
| 1045 | struct chain_head *c; |
| 1046 | |
| Jesper Dangaard Brouer | 4bae3f1 | 2008-07-03 20:31:42 +0200 | [diff] [blame^] | 1047 | /* Assume that chains offsets are sorted, this verified during |
| 1048 | parsing of ruleset (in __iptcc_p_add_chain())*/ |
| 1049 | h->sorted_offsets = 1; |
| 1050 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1051 | /* First pass: over ruleset blob */ |
| 1052 | ENTRY_ITERATE(h->entries->entrytable, h->entries->size, |
| 1053 | cache_add_entry, h, &prev, &num); |
| 1054 | |
| Jesper Dangaard Brouer | 01444da | 2008-01-15 17:18:15 +0000 | [diff] [blame] | 1055 | /* Build the chain index, used for chain list search speedup */ |
| 1056 | if ((iptcc_chain_index_alloc(h)) < 0) |
| 1057 | return -ENOMEM; |
| 1058 | iptcc_chain_index_build(h); |
| 1059 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1060 | /* Second pass: fixup parsed data from first pass */ |
| 1061 | list_for_each_entry(c, &h->chains, list) { |
| 1062 | struct rule_head *r; |
| 1063 | list_for_each_entry(r, &c->rules, list) { |
| Jan Engelhardt | dbb7754 | 2008-02-11 00:33:30 +0100 | [diff] [blame] | 1064 | struct chain_head *lc; |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1065 | STRUCT_STANDARD_TARGET *t; |
| 1066 | |
| 1067 | if (r->type != IPTCC_R_JUMP) |
| 1068 | continue; |
| 1069 | |
| 1070 | t = (STRUCT_STANDARD_TARGET *)GET_TARGET(r->entry); |
| Jan Engelhardt | dbb7754 | 2008-02-11 00:33:30 +0100 | [diff] [blame] | 1071 | lc = iptcc_find_chain_by_offset(h, t->verdict); |
| 1072 | if (!lc) |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1073 | return -1; |
| Jan Engelhardt | dbb7754 | 2008-02-11 00:33:30 +0100 | [diff] [blame] | 1074 | r->jump = lc; |
| 1075 | lc->references++; |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1076 | } |
| 1077 | } |
| 1078 | |
| 1079 | /* FIXME: sort chains */ |
| 1080 | |
| 1081 | return 1; |
| 1082 | } |
| 1083 | |
| 1084 | |
| 1085 | /********************************************************************** |
| 1086 | * RULESET COMPILATION (cache -> blob) |
| 1087 | **********************************************************************/ |
| 1088 | |
| 1089 | /* Convenience structures */ |
| 1090 | struct iptcb_chain_start{ |
| 1091 | STRUCT_ENTRY e; |
| 1092 | struct ipt_error_target name; |
| 1093 | }; |
| 1094 | #define IPTCB_CHAIN_START_SIZE (sizeof(STRUCT_ENTRY) + \ |
| 1095 | ALIGN(sizeof(struct ipt_error_target))) |
| 1096 | |
| 1097 | struct iptcb_chain_foot { |
| 1098 | STRUCT_ENTRY e; |
| 1099 | STRUCT_STANDARD_TARGET target; |
| 1100 | }; |
| 1101 | #define IPTCB_CHAIN_FOOT_SIZE (sizeof(STRUCT_ENTRY) + \ |
| 1102 | ALIGN(sizeof(STRUCT_STANDARD_TARGET))) |
| 1103 | |
| 1104 | struct iptcb_chain_error { |
| 1105 | STRUCT_ENTRY entry; |
| 1106 | struct ipt_error_target target; |
| 1107 | }; |
| 1108 | #define IPTCB_CHAIN_ERROR_SIZE (sizeof(STRUCT_ENTRY) + \ |
| 1109 | ALIGN(sizeof(struct ipt_error_target))) |
| 1110 | |
| 1111 | |
| 1112 | |
| 1113 | /* compile rule from cache into blob */ |
| 1114 | static inline int iptcc_compile_rule (TC_HANDLE_T h, STRUCT_REPLACE *repl, struct rule_head *r) |
| 1115 | { |
| 1116 | /* handle jumps */ |
| 1117 | if (r->type == IPTCC_R_JUMP) { |
| 1118 | STRUCT_STANDARD_TARGET *t; |
| 1119 | t = (STRUCT_STANDARD_TARGET *)GET_TARGET(r->entry); |
| 1120 | /* memset for memcmp convenience on delete/replace */ |
| 1121 | memset(t->target.u.user.name, 0, FUNCTION_MAXNAMELEN); |
| 1122 | strcpy(t->target.u.user.name, STANDARD_TARGET); |
| 1123 | /* Jumps can only happen to builtin chains, so we |
| 1124 | * can safely assume that they always have a header */ |
| 1125 | t->verdict = r->jump->head_offset + IPTCB_CHAIN_START_SIZE; |
| 1126 | } else if (r->type == IPTCC_R_FALLTHROUGH) { |
| 1127 | STRUCT_STANDARD_TARGET *t; |
| 1128 | t = (STRUCT_STANDARD_TARGET *)GET_TARGET(r->entry); |
| 1129 | t->verdict = r->offset + r->size; |
| 1130 | } |
| 1131 | |
| 1132 | /* copy entry from cache to blob */ |
| 1133 | memcpy((char *)repl->entries+r->offset, r->entry, r->size); |
| 1134 | |
| 1135 | return 1; |
| 1136 | } |
| 1137 | |
| 1138 | /* compile chain from cache into blob */ |
| 1139 | static int iptcc_compile_chain(TC_HANDLE_T h, STRUCT_REPLACE *repl, struct chain_head *c) |
| 1140 | { |
| 1141 | int ret; |
| 1142 | struct rule_head *r; |
| 1143 | struct iptcb_chain_start *head; |
| 1144 | struct iptcb_chain_foot *foot; |
| 1145 | |
| 1146 | /* only user-defined chains have heaer */ |
| 1147 | if (!iptcc_is_builtin(c)) { |
| 1148 | /* put chain header in place */ |
| 1149 | head = (void *)repl->entries + c->head_offset; |
| 1150 | head->e.target_offset = sizeof(STRUCT_ENTRY); |
| 1151 | head->e.next_offset = IPTCB_CHAIN_START_SIZE; |
| 1152 | strcpy(head->name.t.u.user.name, ERROR_TARGET); |
| 1153 | head->name.t.u.target_size = |
| 1154 | ALIGN(sizeof(struct ipt_error_target)); |
| 1155 | strcpy(head->name.error, c->name); |
| 1156 | } else { |
| 1157 | repl->hook_entry[c->hooknum-1] = c->head_offset; |
| 1158 | repl->underflow[c->hooknum-1] = c->foot_offset; |
| 1159 | } |
| 1160 | |
| 1161 | /* iterate over rules */ |
| 1162 | list_for_each_entry(r, &c->rules, list) { |
| 1163 | ret = iptcc_compile_rule(h, repl, r); |
| 1164 | if (ret < 0) |
| 1165 | return ret; |
| 1166 | } |
| 1167 | |
| 1168 | /* put chain footer in place */ |
| 1169 | foot = (void *)repl->entries + c->foot_offset; |
| 1170 | foot->e.target_offset = sizeof(STRUCT_ENTRY); |
| 1171 | foot->e.next_offset = IPTCB_CHAIN_FOOT_SIZE; |
| 1172 | strcpy(foot->target.target.u.user.name, STANDARD_TARGET); |
| 1173 | foot->target.target.u.target_size = |
| 1174 | ALIGN(sizeof(STRUCT_STANDARD_TARGET)); |
| 1175 | /* builtin targets have verdict, others return */ |
| 1176 | if (iptcc_is_builtin(c)) |
| 1177 | foot->target.verdict = c->verdict; |
| 1178 | else |
| 1179 | foot->target.verdict = RETURN; |
| 1180 | /* set policy-counters */ |
| 1181 | memcpy(&foot->e.counters, &c->counters, sizeof(STRUCT_COUNTERS)); |
| 1182 | |
| 1183 | return 0; |
| 1184 | } |
| 1185 | |
| 1186 | /* calculate offset and number for every rule in the cache */ |
| 1187 | static int iptcc_compile_chain_offsets(TC_HANDLE_T h, struct chain_head *c, |
| Harald Welte | efa8fc2 | 2005-07-19 22:03:49 +0000 | [diff] [blame] | 1188 | unsigned int *offset, unsigned int *num) |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1189 | { |
| 1190 | struct rule_head *r; |
| 1191 | |
| 1192 | c->head_offset = *offset; |
| 1193 | DEBUGP("%s: chain_head %u, offset=%u\n", c->name, *num, *offset); |
| 1194 | |
| 1195 | if (!iptcc_is_builtin(c)) { |
| 1196 | /* Chain has header */ |
| 1197 | *offset += sizeof(STRUCT_ENTRY) |
| 1198 | + ALIGN(sizeof(struct ipt_error_target)); |
| 1199 | (*num)++; |
| 1200 | } |
| 1201 | |
| 1202 | list_for_each_entry(r, &c->rules, list) { |
| 1203 | DEBUGP("rule %u, offset=%u, index=%u\n", *num, *offset, *num); |
| 1204 | r->offset = *offset; |
| 1205 | r->index = *num; |
| 1206 | *offset += r->size; |
| 1207 | (*num)++; |
| 1208 | } |
| 1209 | |
| 1210 | DEBUGP("%s; chain_foot %u, offset=%u, index=%u\n", c->name, *num, |
| 1211 | *offset, *num); |
| 1212 | c->foot_offset = *offset; |
| 1213 | c->foot_index = *num; |
| 1214 | *offset += sizeof(STRUCT_ENTRY) |
| 1215 | + ALIGN(sizeof(STRUCT_STANDARD_TARGET)); |
| 1216 | (*num)++; |
| 1217 | |
| 1218 | return 1; |
| 1219 | } |
| 1220 | |
| 1221 | /* put the pieces back together again */ |
| 1222 | static int iptcc_compile_table_prep(TC_HANDLE_T h, unsigned int *size) |
| 1223 | { |
| 1224 | struct chain_head *c; |
| 1225 | unsigned int offset = 0, num = 0; |
| 1226 | int ret = 0; |
| 1227 | |
| 1228 | /* First pass: calculate offset for every rule */ |
| 1229 | list_for_each_entry(c, &h->chains, list) { |
| 1230 | ret = iptcc_compile_chain_offsets(h, c, &offset, &num); |
| 1231 | if (ret < 0) |
| 1232 | return ret; |
| 1233 | } |
| 1234 | |
| 1235 | /* Append one error rule at end of chain */ |
| 1236 | num++; |
| 1237 | offset += sizeof(STRUCT_ENTRY) |
| 1238 | + ALIGN(sizeof(struct ipt_error_target)); |
| 1239 | |
| 1240 | /* ruleset size is now in offset */ |
| 1241 | *size = offset; |
| 1242 | return num; |
| 1243 | } |
| 1244 | |
| 1245 | static int iptcc_compile_table(TC_HANDLE_T h, STRUCT_REPLACE *repl) |
| 1246 | { |
| 1247 | struct chain_head *c; |
| 1248 | struct iptcb_chain_error *error; |
| 1249 | |
| 1250 | /* Second pass: copy from cache to offsets, fill in jumps */ |
| 1251 | list_for_each_entry(c, &h->chains, list) { |
| 1252 | int ret = iptcc_compile_chain(h, repl, c); |
| 1253 | if (ret < 0) |
| 1254 | return ret; |
| 1255 | } |
| 1256 | |
| 1257 | /* Append error rule at end of chain */ |
| 1258 | error = (void *)repl->entries + repl->size - IPTCB_CHAIN_ERROR_SIZE; |
| 1259 | error->entry.target_offset = sizeof(STRUCT_ENTRY); |
| 1260 | error->entry.next_offset = IPTCB_CHAIN_ERROR_SIZE; |
| 1261 | error->target.t.u.user.target_size = |
| 1262 | ALIGN(sizeof(struct ipt_error_target)); |
| 1263 | strcpy((char *)&error->target.t.u.user.name, ERROR_TARGET); |
| 1264 | strcpy((char *)&error->target.error, "ERROR"); |
| 1265 | |
| 1266 | return 1; |
| 1267 | } |
| 1268 | |
| 1269 | /********************************************************************** |
| 1270 | * EXTERNAL API (operates on cache only) |
| 1271 | **********************************************************************/ |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1272 | |
| 1273 | /* Allocate handle of given size */ |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1274 | static TC_HANDLE_T |
| Harald Welte | 0113fe7 | 2004-01-06 19:04:02 +0000 | [diff] [blame] | 1275 | alloc_handle(const char *tablename, unsigned int size, unsigned int num_rules) |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1276 | { |
| 1277 | size_t len; |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1278 | TC_HANDLE_T h; |
| 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 | len = sizeof(STRUCT_TC_HANDLE) + size; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1281 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1282 | h = malloc(sizeof(STRUCT_TC_HANDLE)); |
| 1283 | if (!h) { |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1284 | errno = ENOMEM; |
| 1285 | return NULL; |
| 1286 | } |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1287 | memset(h, 0, sizeof(*h)); |
| 1288 | INIT_LIST_HEAD(&h->chains); |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1289 | strcpy(h->info.name, tablename); |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1290 | |
| Harald Welte | 0371c0c | 2004-09-19 21:00:12 +0000 | [diff] [blame] | 1291 | h->entries = malloc(sizeof(STRUCT_GET_ENTRIES) + size); |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1292 | if (!h->entries) |
| 1293 | goto out_free_handle; |
| 1294 | |
| 1295 | strcpy(h->entries->name, tablename); |
| Harald Welte | 0371c0c | 2004-09-19 21:00:12 +0000 | [diff] [blame] | 1296 | h->entries->size = size; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1297 | |
| 1298 | return h; |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1299 | |
| 1300 | out_free_handle: |
| 1301 | free(h); |
| 1302 | |
| 1303 | return NULL; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1304 | } |
| 1305 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1306 | |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1307 | TC_HANDLE_T |
| 1308 | TC_INIT(const char *tablename) |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1309 | { |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1310 | TC_HANDLE_T h; |
| 1311 | STRUCT_GETINFO info; |
| Harald Welte | efa8fc2 | 2005-07-19 22:03:49 +0000 | [diff] [blame] | 1312 | unsigned int tmp; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1313 | socklen_t s; |
| 1314 | |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1315 | iptc_fn = TC_INIT; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1316 | |
| Martin Josefsson | 841e4ae | 2003-05-02 15:30:11 +0000 | [diff] [blame] | 1317 | if (strlen(tablename) >= TABLE_MAXNAMELEN) { |
| 1318 | errno = EINVAL; |
| 1319 | return NULL; |
| 1320 | } |
| 1321 | |
| Derrik Pates | 664c0a3 | 2005-02-01 13:28:14 +0000 | [diff] [blame] | 1322 | if (sockfd_use == 0) { |
| 1323 | sockfd = socket(TC_AF, SOCK_RAW, IPPROTO_RAW); |
| 1324 | if (sockfd < 0) |
| 1325 | return NULL; |
| 1326 | } |
| 1327 | sockfd_use++; |
| Patrick McHardy | 2f93205 | 2008-04-02 14:01:53 +0200 | [diff] [blame] | 1328 | retry: |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1329 | s = sizeof(info); |
| Martin Josefsson | 841e4ae | 2003-05-02 15:30:11 +0000 | [diff] [blame] | 1330 | |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1331 | strcpy(info.name, tablename); |
| Derrik Pates | 664c0a3 | 2005-02-01 13:28:14 +0000 | [diff] [blame] | 1332 | if (getsockopt(sockfd, TC_IPPROTO, SO_GET_INFO, &info, &s) < 0) { |
| 1333 | if (--sockfd_use == 0) { |
| 1334 | close(sockfd); |
| 1335 | sockfd = -1; |
| 1336 | } |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1337 | return NULL; |
| Derrik Pates | 664c0a3 | 2005-02-01 13:28:14 +0000 | [diff] [blame] | 1338 | } |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1339 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1340 | DEBUGP("valid_hooks=0x%08x, num_entries=%u, size=%u\n", |
| 1341 | info.valid_hooks, info.num_entries, info.size); |
| 1342 | |
| Harald Welte | 0113fe7 | 2004-01-06 19:04:02 +0000 | [diff] [blame] | 1343 | if ((h = alloc_handle(info.name, info.size, info.num_entries)) |
| Martin Josefsson | 841e4ae | 2003-05-02 15:30:11 +0000 | [diff] [blame] | 1344 | == NULL) { |
| Derrik Pates | 664c0a3 | 2005-02-01 13:28:14 +0000 | [diff] [blame] | 1345 | if (--sockfd_use == 0) { |
| 1346 | close(sockfd); |
| 1347 | sockfd = -1; |
| 1348 | } |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1349 | return NULL; |
| Martin Josefsson | 841e4ae | 2003-05-02 15:30:11 +0000 | [diff] [blame] | 1350 | } |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1351 | |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1352 | /* Initialize current state */ |
| 1353 | h->info = info; |
| Harald Welte | 0113fe7 | 2004-01-06 19:04:02 +0000 | [diff] [blame] | 1354 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1355 | h->entries->size = h->info.size; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1356 | |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1357 | tmp = sizeof(STRUCT_GET_ENTRIES) + h->info.size; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1358 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1359 | if (getsockopt(sockfd, TC_IPPROTO, SO_GET_ENTRIES, h->entries, |
| 1360 | &tmp) < 0) |
| 1361 | goto error; |
| 1362 | |
| 1363 | #ifdef IPTC_DEBUG2 |
| 1364 | { |
| 1365 | int fd = open("/tmp/libiptc-so_get_entries.blob", |
| 1366 | O_CREAT|O_WRONLY); |
| 1367 | if (fd >= 0) { |
| 1368 | write(fd, h->entries, tmp); |
| 1369 | close(fd); |
| 1370 | } |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1371 | } |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1372 | #endif |
| 1373 | |
| 1374 | if (parse_table(h) < 0) |
| 1375 | goto error; |
| Rusty Russell | 7e53bf9 | 2000-03-20 07:03:28 +0000 | [diff] [blame] | 1376 | |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1377 | CHECK(h); |
| 1378 | return h; |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1379 | error: |
| 1380 | TC_FREE(&h); |
| Patrick McHardy | 2f93205 | 2008-04-02 14:01:53 +0200 | [diff] [blame] | 1381 | /* A different process changed the ruleset size, retry */ |
| 1382 | if (errno == EAGAIN) |
| 1383 | goto retry; |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1384 | return NULL; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1385 | } |
| 1386 | |
| Martin Josefsson | 841e4ae | 2003-05-02 15:30:11 +0000 | [diff] [blame] | 1387 | void |
| 1388 | TC_FREE(TC_HANDLE_T *h) |
| 1389 | { |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1390 | struct chain_head *c, *tmp; |
| 1391 | |
| Derrik Pates | 664c0a3 | 2005-02-01 13:28:14 +0000 | [diff] [blame] | 1392 | iptc_fn = TC_FREE; |
| 1393 | if (--sockfd_use == 0) { |
| 1394 | close(sockfd); |
| 1395 | sockfd = -1; |
| 1396 | } |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1397 | |
| 1398 | list_for_each_entry_safe(c, tmp, &(*h)->chains, list) { |
| 1399 | struct rule_head *r, *rtmp; |
| 1400 | |
| 1401 | list_for_each_entry_safe(r, rtmp, &c->rules, list) { |
| 1402 | free(r); |
| 1403 | } |
| 1404 | |
| 1405 | free(c); |
| 1406 | } |
| 1407 | |
| Jesper Dangaard Brouer | 01444da | 2008-01-15 17:18:15 +0000 | [diff] [blame] | 1408 | iptcc_chain_index_free(*h); |
| 1409 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1410 | free((*h)->entries); |
| Martin Josefsson | 841e4ae | 2003-05-02 15:30:11 +0000 | [diff] [blame] | 1411 | free(*h); |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1412 | |
| Martin Josefsson | 841e4ae | 2003-05-02 15:30:11 +0000 | [diff] [blame] | 1413 | *h = NULL; |
| 1414 | } |
| 1415 | |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1416 | static inline int |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1417 | print_match(const STRUCT_ENTRY_MATCH *m) |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1418 | { |
| Rusty Russell | 228e98d | 2000-04-27 10:28:06 +0000 | [diff] [blame] | 1419 | printf("Match name: `%s'\n", m->u.user.name); |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1420 | return 0; |
| 1421 | } |
| 1422 | |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1423 | static int dump_entry(STRUCT_ENTRY *e, const TC_HANDLE_T handle); |
| 1424 | |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1425 | void |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1426 | TC_DUMP_ENTRIES(const TC_HANDLE_T handle) |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1427 | { |
| Derrik Pates | 664c0a3 | 2005-02-01 13:28:14 +0000 | [diff] [blame] | 1428 | iptc_fn = TC_DUMP_ENTRIES; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1429 | CHECK(handle); |
| Patrick McHardy | 97fb2f1 | 2007-09-08 16:52:25 +0000 | [diff] [blame] | 1430 | |
| Rusty Russell | e45c713 | 2004-12-16 13:21:44 +0000 | [diff] [blame] | 1431 | printf("libiptc v%s. %u bytes.\n", |
| Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame] | 1432 | XTABLES_VERSION, handle->entries->size); |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1433 | printf("Table `%s'\n", handle->info.name); |
| 1434 | 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] | 1435 | handle->info.hook_entry[HOOK_PRE_ROUTING], |
| 1436 | handle->info.hook_entry[HOOK_LOCAL_IN], |
| 1437 | handle->info.hook_entry[HOOK_FORWARD], |
| 1438 | handle->info.hook_entry[HOOK_LOCAL_OUT], |
| 1439 | handle->info.hook_entry[HOOK_POST_ROUTING]); |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1440 | 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] | 1441 | handle->info.underflow[HOOK_PRE_ROUTING], |
| 1442 | handle->info.underflow[HOOK_LOCAL_IN], |
| 1443 | handle->info.underflow[HOOK_FORWARD], |
| 1444 | handle->info.underflow[HOOK_LOCAL_OUT], |
| 1445 | handle->info.underflow[HOOK_POST_ROUTING]); |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1446 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1447 | ENTRY_ITERATE(handle->entries->entrytable, handle->entries->size, |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1448 | dump_entry, handle); |
| Harald Welte | 0113fe7 | 2004-01-06 19:04:02 +0000 | [diff] [blame] | 1449 | } |
| Rusty Russell | 30fd6e5 | 2000-04-23 09:16:06 +0000 | [diff] [blame] | 1450 | |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1451 | /* Does this chain exist? */ |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1452 | int TC_IS_CHAIN(const char *chain, const TC_HANDLE_T handle) |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1453 | { |
| Derrik Pates | 664c0a3 | 2005-02-01 13:28:14 +0000 | [diff] [blame] | 1454 | iptc_fn = TC_IS_CHAIN; |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1455 | return iptcc_find_label(chain, handle) != NULL; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1456 | } |
| 1457 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1458 | static void iptcc_chain_iterator_advance(TC_HANDLE_T handle) |
| 1459 | { |
| 1460 | struct chain_head *c = handle->chain_iterator_cur; |
| 1461 | |
| 1462 | if (c->list.next == &handle->chains) |
| 1463 | handle->chain_iterator_cur = NULL; |
| 1464 | else |
| 1465 | handle->chain_iterator_cur = |
| 1466 | list_entry(c->list.next, struct chain_head, list); |
| 1467 | } |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1468 | |
| Rusty Russell | 30fd6e5 | 2000-04-23 09:16:06 +0000 | [diff] [blame] | 1469 | /* Iterator functions to run through the chains. */ |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1470 | const char * |
| Philip Blundell | 8c70090 | 2000-05-15 02:17:52 +0000 | [diff] [blame] | 1471 | TC_FIRST_CHAIN(TC_HANDLE_T *handle) |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1472 | { |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1473 | struct chain_head *c = list_entry((*handle)->chains.next, |
| 1474 | struct chain_head, list); |
| 1475 | |
| 1476 | iptc_fn = TC_FIRST_CHAIN; |
| 1477 | |
| 1478 | |
| 1479 | if (list_empty(&(*handle)->chains)) { |
| 1480 | DEBUGP(": no chains\n"); |
| Harald Welte | 0113fe7 | 2004-01-06 19:04:02 +0000 | [diff] [blame] | 1481 | return NULL; |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1482 | } |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1483 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1484 | (*handle)->chain_iterator_cur = c; |
| 1485 | iptcc_chain_iterator_advance(*handle); |
| Harald Welte | 0113fe7 | 2004-01-06 19:04:02 +0000 | [diff] [blame] | 1486 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1487 | DEBUGP(": returning `%s'\n", c->name); |
| 1488 | return c->name; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1489 | } |
| 1490 | |
| Rusty Russell | 30fd6e5 | 2000-04-23 09:16:06 +0000 | [diff] [blame] | 1491 | /* Iterator functions to run through the chains. Returns NULL at end. */ |
| 1492 | const char * |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1493 | TC_NEXT_CHAIN(TC_HANDLE_T *handle) |
| Rusty Russell | 30fd6e5 | 2000-04-23 09:16:06 +0000 | [diff] [blame] | 1494 | { |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1495 | struct chain_head *c = (*handle)->chain_iterator_cur; |
| Rusty Russell | 30fd6e5 | 2000-04-23 09:16:06 +0000 | [diff] [blame] | 1496 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1497 | iptc_fn = TC_NEXT_CHAIN; |
| 1498 | |
| 1499 | if (!c) { |
| 1500 | DEBUGP(": no more chains\n"); |
| Rusty Russell | 30fd6e5 | 2000-04-23 09:16:06 +0000 | [diff] [blame] | 1501 | return NULL; |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1502 | } |
| Rusty Russell | 30fd6e5 | 2000-04-23 09:16:06 +0000 | [diff] [blame] | 1503 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1504 | iptcc_chain_iterator_advance(*handle); |
| 1505 | |
| 1506 | DEBUGP(": returning `%s'\n", c->name); |
| 1507 | return c->name; |
| Rusty Russell | 30fd6e5 | 2000-04-23 09:16:06 +0000 | [diff] [blame] | 1508 | } |
| 1509 | |
| 1510 | /* Get first rule in the given chain: NULL for empty chain. */ |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1511 | const STRUCT_ENTRY * |
| Philip Blundell | 8c70090 | 2000-05-15 02:17:52 +0000 | [diff] [blame] | 1512 | TC_FIRST_RULE(const char *chain, TC_HANDLE_T *handle) |
| Rusty Russell | 30fd6e5 | 2000-04-23 09:16:06 +0000 | [diff] [blame] | 1513 | { |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1514 | struct chain_head *c; |
| 1515 | struct rule_head *r; |
| Rusty Russell | 30fd6e5 | 2000-04-23 09:16:06 +0000 | [diff] [blame] | 1516 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1517 | iptc_fn = TC_FIRST_RULE; |
| 1518 | |
| 1519 | DEBUGP("first rule(%s): ", chain); |
| 1520 | |
| 1521 | c = iptcc_find_label(chain, *handle); |
| Rusty Russell | 30fd6e5 | 2000-04-23 09:16:06 +0000 | [diff] [blame] | 1522 | if (!c) { |
| 1523 | errno = ENOENT; |
| 1524 | return NULL; |
| 1525 | } |
| 1526 | |
| 1527 | /* Empty chain: single return/policy rule */ |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1528 | if (list_empty(&c->rules)) { |
| 1529 | DEBUGP_C("no rules, returning NULL\n"); |
| Rusty Russell | 30fd6e5 | 2000-04-23 09:16:06 +0000 | [diff] [blame] | 1530 | return NULL; |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1531 | } |
| Rusty Russell | 30fd6e5 | 2000-04-23 09:16:06 +0000 | [diff] [blame] | 1532 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1533 | r = list_entry(c->rules.next, struct rule_head, list); |
| 1534 | (*handle)->rule_iterator_cur = r; |
| 1535 | DEBUGP_C("%p\n", r); |
| 1536 | |
| 1537 | return r->entry; |
| Rusty Russell | 30fd6e5 | 2000-04-23 09:16:06 +0000 | [diff] [blame] | 1538 | } |
| 1539 | |
| 1540 | /* Returns NULL when rules run out. */ |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1541 | const STRUCT_ENTRY * |
| Philip Blundell | 8c70090 | 2000-05-15 02:17:52 +0000 | [diff] [blame] | 1542 | TC_NEXT_RULE(const STRUCT_ENTRY *prev, TC_HANDLE_T *handle) |
| Rusty Russell | 30fd6e5 | 2000-04-23 09:16:06 +0000 | [diff] [blame] | 1543 | { |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1544 | struct rule_head *r; |
| Rusty Russell | 30fd6e5 | 2000-04-23 09:16:06 +0000 | [diff] [blame] | 1545 | |
| Derrik Pates | 664c0a3 | 2005-02-01 13:28:14 +0000 | [diff] [blame] | 1546 | iptc_fn = TC_NEXT_RULE; |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1547 | DEBUGP("rule_iterator_cur=%p...", (*handle)->rule_iterator_cur); |
| 1548 | |
| 1549 | if (!(*handle)->rule_iterator_cur) { |
| 1550 | DEBUGP_C("returning NULL\n"); |
| 1551 | return NULL; |
| 1552 | } |
| 1553 | |
| 1554 | r = list_entry((*handle)->rule_iterator_cur->list.next, |
| 1555 | struct rule_head, list); |
| 1556 | |
| 1557 | iptc_fn = TC_NEXT_RULE; |
| 1558 | |
| 1559 | DEBUGP_C("next=%p, head=%p...", &r->list, |
| 1560 | &(*handle)->rule_iterator_cur->chain->rules); |
| 1561 | |
| 1562 | if (&r->list == &(*handle)->rule_iterator_cur->chain->rules) { |
| 1563 | (*handle)->rule_iterator_cur = NULL; |
| 1564 | DEBUGP_C("finished, returning NULL\n"); |
| 1565 | return NULL; |
| 1566 | } |
| 1567 | |
| 1568 | (*handle)->rule_iterator_cur = r; |
| 1569 | |
| 1570 | /* NOTE: prev is without any influence ! */ |
| 1571 | DEBUGP_C("returning rule %p\n", r); |
| 1572 | return r->entry; |
| Rusty Russell | 30fd6e5 | 2000-04-23 09:16:06 +0000 | [diff] [blame] | 1573 | } |
| 1574 | |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1575 | /* How many rules in this chain? */ |
| Jan Engelhardt | 33690a1 | 2008-02-11 00:54:00 +0100 | [diff] [blame] | 1576 | static unsigned int |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1577 | TC_NUM_RULES(const char *chain, TC_HANDLE_T *handle) |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1578 | { |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1579 | struct chain_head *c; |
| 1580 | iptc_fn = TC_NUM_RULES; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1581 | CHECK(*handle); |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1582 | |
| 1583 | c = iptcc_find_label(chain, *handle); |
| 1584 | if (!c) { |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1585 | errno = ENOENT; |
| 1586 | return (unsigned int)-1; |
| 1587 | } |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1588 | |
| 1589 | return c->num_rules; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1590 | } |
| 1591 | |
| Jan Engelhardt | 33690a1 | 2008-02-11 00:54:00 +0100 | [diff] [blame] | 1592 | static const STRUCT_ENTRY * |
| 1593 | TC_GET_RULE(const char *chain, unsigned int n, TC_HANDLE_T *handle) |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1594 | { |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1595 | struct chain_head *c; |
| 1596 | struct rule_head *r; |
| 1597 | |
| 1598 | iptc_fn = TC_GET_RULE; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1599 | |
| 1600 | CHECK(*handle); |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1601 | |
| 1602 | c = iptcc_find_label(chain, *handle); |
| 1603 | if (!c) { |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1604 | errno = ENOENT; |
| 1605 | return NULL; |
| 1606 | } |
| 1607 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1608 | r = iptcc_get_rule_num(c, n); |
| 1609 | if (!r) |
| 1610 | return NULL; |
| 1611 | return r->entry; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1612 | } |
| 1613 | |
| 1614 | /* Returns a pointer to the target name of this position. */ |
| Jan Engelhardt | 33690a1 | 2008-02-11 00:54:00 +0100 | [diff] [blame] | 1615 | static const char *standard_target_map(int verdict) |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1616 | { |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1617 | switch (verdict) { |
| 1618 | case RETURN: |
| 1619 | return LABEL_RETURN; |
| 1620 | break; |
| 1621 | case -NF_ACCEPT-1: |
| 1622 | return LABEL_ACCEPT; |
| 1623 | break; |
| 1624 | case -NF_DROP-1: |
| 1625 | return LABEL_DROP; |
| 1626 | break; |
| 1627 | case -NF_QUEUE-1: |
| 1628 | return LABEL_QUEUE; |
| 1629 | break; |
| 1630 | default: |
| 1631 | fprintf(stderr, "ERROR: %d not a valid target)\n", |
| 1632 | verdict); |
| 1633 | abort(); |
| 1634 | break; |
| 1635 | } |
| 1636 | /* not reached */ |
| 1637 | return NULL; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1638 | } |
| 1639 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1640 | /* Returns a pointer to the target name of this position. */ |
| 1641 | const char *TC_GET_TARGET(const STRUCT_ENTRY *ce, |
| 1642 | TC_HANDLE_T *handle) |
| 1643 | { |
| 1644 | STRUCT_ENTRY *e = (STRUCT_ENTRY *)ce; |
| Rusty Russell | e45c713 | 2004-12-16 13:21:44 +0000 | [diff] [blame] | 1645 | struct rule_head *r = container_of(e, struct rule_head, entry[0]); |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1646 | |
| 1647 | iptc_fn = TC_GET_TARGET; |
| 1648 | |
| 1649 | switch(r->type) { |
| 1650 | int spos; |
| 1651 | case IPTCC_R_FALLTHROUGH: |
| 1652 | return ""; |
| 1653 | break; |
| 1654 | case IPTCC_R_JUMP: |
| 1655 | DEBUGP("r=%p, jump=%p, name=`%s'\n", r, r->jump, r->jump->name); |
| 1656 | return r->jump->name; |
| 1657 | break; |
| 1658 | case IPTCC_R_STANDARD: |
| 1659 | spos = *(int *)GET_TARGET(e)->data; |
| 1660 | DEBUGP("r=%p, spos=%d'\n", r, spos); |
| 1661 | return standard_target_map(spos); |
| 1662 | break; |
| 1663 | case IPTCC_R_MODULE: |
| 1664 | return GET_TARGET(e)->u.user.name; |
| 1665 | break; |
| 1666 | } |
| 1667 | return NULL; |
| 1668 | } |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1669 | /* Is this a built-in chain? Actually returns hook + 1. */ |
| 1670 | int |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1671 | TC_BUILTIN(const char *chain, const TC_HANDLE_T handle) |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1672 | { |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1673 | struct chain_head *c; |
| 1674 | |
| 1675 | iptc_fn = TC_BUILTIN; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1676 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1677 | c = iptcc_find_label(chain, handle); |
| 1678 | if (!c) { |
| 1679 | errno = ENOENT; |
| Martin Josefsson | b0f3d2d | 2004-09-23 18:23:20 +0000 | [diff] [blame] | 1680 | return 0; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1681 | } |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1682 | |
| 1683 | return iptcc_is_builtin(c); |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1684 | } |
| 1685 | |
| 1686 | /* Get the policy of a given built-in chain */ |
| 1687 | const char * |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1688 | TC_GET_POLICY(const char *chain, |
| 1689 | STRUCT_COUNTERS *counters, |
| 1690 | TC_HANDLE_T *handle) |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1691 | { |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1692 | struct chain_head *c; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1693 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1694 | iptc_fn = TC_GET_POLICY; |
| 1695 | |
| 1696 | DEBUGP("called for chain %s\n", chain); |
| 1697 | |
| 1698 | c = iptcc_find_label(chain, *handle); |
| 1699 | if (!c) { |
| 1700 | errno = ENOENT; |
| 1701 | return NULL; |
| 1702 | } |
| 1703 | |
| 1704 | if (!iptcc_is_builtin(c)) |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1705 | return NULL; |
| 1706 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1707 | *counters = c->counters; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1708 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1709 | return standard_target_map(c->verdict); |
| Harald Welte | 0113fe7 | 2004-01-06 19:04:02 +0000 | [diff] [blame] | 1710 | } |
| 1711 | |
| 1712 | static int |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1713 | iptcc_standard_map(struct rule_head *r, int verdict) |
| Harald Welte | 0113fe7 | 2004-01-06 19:04:02 +0000 | [diff] [blame] | 1714 | { |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1715 | STRUCT_ENTRY *e = r->entry; |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1716 | STRUCT_STANDARD_TARGET *t; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1717 | |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1718 | t = (STRUCT_STANDARD_TARGET *)GET_TARGET(e); |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1719 | |
| Rusty Russell | 67088e7 | 2000-05-10 01:18:57 +0000 | [diff] [blame] | 1720 | if (t->target.u.target_size |
| Philip Blundell | 8c70090 | 2000-05-15 02:17:52 +0000 | [diff] [blame] | 1721 | != ALIGN(sizeof(STRUCT_STANDARD_TARGET))) { |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1722 | errno = EINVAL; |
| 1723 | return 0; |
| 1724 | } |
| 1725 | /* memset for memcmp convenience on delete/replace */ |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1726 | memset(t->target.u.user.name, 0, FUNCTION_MAXNAMELEN); |
| 1727 | strcpy(t->target.u.user.name, STANDARD_TARGET); |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1728 | t->verdict = verdict; |
| 1729 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1730 | r->type = IPTCC_R_STANDARD; |
| 1731 | |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1732 | return 1; |
| 1733 | } |
| Rusty Russell | 7e53bf9 | 2000-03-20 07:03:28 +0000 | [diff] [blame] | 1734 | |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1735 | static int |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1736 | iptcc_map_target(const TC_HANDLE_T handle, |
| 1737 | struct rule_head *r) |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1738 | { |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1739 | STRUCT_ENTRY *e = r->entry; |
| Harald Welte | 0113fe7 | 2004-01-06 19:04:02 +0000 | [diff] [blame] | 1740 | STRUCT_ENTRY_TARGET *t = GET_TARGET(e); |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1741 | |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1742 | /* Maybe it's empty (=> fall through) */ |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1743 | if (strcmp(t->u.user.name, "") == 0) { |
| 1744 | r->type = IPTCC_R_FALLTHROUGH; |
| 1745 | return 1; |
| 1746 | } |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1747 | /* Maybe it's a standard target name... */ |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1748 | else if (strcmp(t->u.user.name, LABEL_ACCEPT) == 0) |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1749 | return iptcc_standard_map(r, -NF_ACCEPT - 1); |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1750 | else if (strcmp(t->u.user.name, LABEL_DROP) == 0) |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1751 | return iptcc_standard_map(r, -NF_DROP - 1); |
| Rusty Russell | 67088e7 | 2000-05-10 01:18:57 +0000 | [diff] [blame] | 1752 | else if (strcmp(t->u.user.name, LABEL_QUEUE) == 0) |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1753 | return iptcc_standard_map(r, -NF_QUEUE - 1); |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1754 | else if (strcmp(t->u.user.name, LABEL_RETURN) == 0) |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1755 | return iptcc_standard_map(r, RETURN); |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1756 | else if (TC_BUILTIN(t->u.user.name, handle)) { |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1757 | /* Can't jump to builtins. */ |
| 1758 | errno = EINVAL; |
| 1759 | return 0; |
| 1760 | } else { |
| 1761 | /* Maybe it's an existing chain name. */ |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1762 | struct chain_head *c; |
| 1763 | DEBUGP("trying to find chain `%s': ", t->u.user.name); |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1764 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1765 | c = iptcc_find_label(t->u.user.name, handle); |
| 1766 | if (c) { |
| 1767 | DEBUGP_C("found!\n"); |
| 1768 | r->type = IPTCC_R_JUMP; |
| 1769 | r->jump = c; |
| 1770 | c->references++; |
| 1771 | return 1; |
| 1772 | } |
| 1773 | DEBUGP_C("not found :(\n"); |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1774 | } |
| 1775 | |
| 1776 | /* Must be a module? If not, kernel will reject... */ |
| Rusty Russell | 3aef54d | 2005-01-03 03:48:40 +0000 | [diff] [blame] | 1777 | /* memset to all 0 for your memcmp convenience: don't clear version */ |
| Rusty Russell | 228e98d | 2000-04-27 10:28:06 +0000 | [diff] [blame] | 1778 | memset(t->u.user.name + strlen(t->u.user.name), |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1779 | 0, |
| Rusty Russell | 3aef54d | 2005-01-03 03:48:40 +0000 | [diff] [blame] | 1780 | FUNCTION_MAXNAMELEN - 1 - strlen(t->u.user.name)); |
| Rusty Russell | 733e54b | 2004-12-16 14:22:23 +0000 | [diff] [blame] | 1781 | r->type = IPTCC_R_MODULE; |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1782 | set_changed(handle); |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1783 | return 1; |
| 1784 | } |
| 1785 | |
| Harald Welte | 0113fe7 | 2004-01-06 19:04:02 +0000 | [diff] [blame] | 1786 | /* Insert the entry `fw' in chain `chain' into position `rulenum'. */ |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1787 | int |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1788 | TC_INSERT_ENTRY(const IPT_CHAINLABEL chain, |
| 1789 | const STRUCT_ENTRY *e, |
| 1790 | unsigned int rulenum, |
| 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; |
| Martin Josefsson | eb066cc | 2004-09-22 21:04:07 +0000 | [diff] [blame] | 1794 | struct rule_head *r; |
| 1795 | struct list_head *prev; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1796 | |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1797 | iptc_fn = TC_INSERT_ENTRY; |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1798 | |
| 1799 | if (!(c = iptcc_find_label(chain, *handle))) { |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1800 | errno = ENOENT; |
| 1801 | return 0; |
| 1802 | } |
| 1803 | |
| Martin Josefsson | eb066cc | 2004-09-22 21:04:07 +0000 | [diff] [blame] | 1804 | /* first rulenum index = 0 |
| 1805 | first c->num_rules index = 1 */ |
| 1806 | if (rulenum > c->num_rules) { |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1807 | errno = E2BIG; |
| 1808 | return 0; |
| 1809 | } |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1810 | |
| Martin Josefsson | 631f361 | 2004-09-23 19:25:06 +0000 | [diff] [blame] | 1811 | /* If we are inserting at the end just take advantage of the |
| 1812 | double linked list, insert will happen before the entry |
| 1813 | prev points to. */ |
| 1814 | if (rulenum == c->num_rules) { |
| Martin Josefsson | eb066cc | 2004-09-22 21:04:07 +0000 | [diff] [blame] | 1815 | prev = &c->rules; |
| Martin Josefsson | a5616dc | 2004-10-24 22:27:31 +0000 | [diff] [blame] | 1816 | } else if (rulenum + 1 <= c->num_rules/2) { |
| Martin Josefsson | 631f361 | 2004-09-23 19:25:06 +0000 | [diff] [blame] | 1817 | r = iptcc_get_rule_num(c, rulenum + 1); |
| Martin Josefsson | a5616dc | 2004-10-24 22:27:31 +0000 | [diff] [blame] | 1818 | prev = &r->list; |
| 1819 | } else { |
| 1820 | r = iptcc_get_rule_num_reverse(c, c->num_rules - rulenum); |
| Martin Josefsson | 631f361 | 2004-09-23 19:25:06 +0000 | [diff] [blame] | 1821 | prev = &r->list; |
| 1822 | } |
| Martin Josefsson | eb066cc | 2004-09-22 21:04:07 +0000 | [diff] [blame] | 1823 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1824 | if (!(r = iptcc_alloc_rule(c, e->next_offset))) { |
| 1825 | errno = ENOMEM; |
| Harald Welte | 0113fe7 | 2004-01-06 19:04:02 +0000 | [diff] [blame] | 1826 | return 0; |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1827 | } |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1828 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1829 | memcpy(r->entry, e, e->next_offset); |
| 1830 | r->counter_map.maptype = COUNTER_MAP_SET; |
| 1831 | |
| 1832 | if (!iptcc_map_target(*handle, r)) { |
| 1833 | free(r); |
| 1834 | return 0; |
| 1835 | } |
| 1836 | |
| Martin Josefsson | eb066cc | 2004-09-22 21:04:07 +0000 | [diff] [blame] | 1837 | list_add_tail(&r->list, prev); |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1838 | c->num_rules++; |
| 1839 | |
| 1840 | set_changed(*handle); |
| 1841 | |
| 1842 | return 1; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1843 | } |
| 1844 | |
| 1845 | /* Atomically replace rule `rulenum' in `chain' with `fw'. */ |
| 1846 | int |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1847 | TC_REPLACE_ENTRY(const IPT_CHAINLABEL chain, |
| 1848 | const STRUCT_ENTRY *e, |
| 1849 | unsigned int rulenum, |
| 1850 | TC_HANDLE_T *handle) |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1851 | { |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1852 | struct chain_head *c; |
| 1853 | struct rule_head *r, *old; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1854 | |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1855 | iptc_fn = TC_REPLACE_ENTRY; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1856 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1857 | if (!(c = iptcc_find_label(chain, *handle))) { |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1858 | errno = ENOENT; |
| 1859 | return 0; |
| 1860 | } |
| 1861 | |
| Martin Josefsson | 0f9b8b1 | 2004-12-18 17:18:49 +0000 | [diff] [blame] | 1862 | if (rulenum >= c->num_rules) { |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1863 | errno = E2BIG; |
| 1864 | return 0; |
| 1865 | } |
| 1866 | |
| Martin Josefsson | 0f9b8b1 | 2004-12-18 17:18:49 +0000 | [diff] [blame] | 1867 | /* Take advantage of the double linked list if possible. */ |
| 1868 | if (rulenum + 1 <= c->num_rules/2) { |
| 1869 | old = iptcc_get_rule_num(c, rulenum + 1); |
| 1870 | } else { |
| 1871 | old = iptcc_get_rule_num_reverse(c, c->num_rules - rulenum); |
| 1872 | } |
| 1873 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1874 | if (!(r = iptcc_alloc_rule(c, e->next_offset))) { |
| 1875 | errno = ENOMEM; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1876 | return 0; |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1877 | } |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1878 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1879 | memcpy(r->entry, e, e->next_offset); |
| 1880 | r->counter_map.maptype = COUNTER_MAP_SET; |
| 1881 | |
| 1882 | if (!iptcc_map_target(*handle, r)) { |
| 1883 | free(r); |
| Harald Welte | 0113fe7 | 2004-01-06 19:04:02 +0000 | [diff] [blame] | 1884 | return 0; |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1885 | } |
| Harald Welte | 0113fe7 | 2004-01-06 19:04:02 +0000 | [diff] [blame] | 1886 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1887 | list_add(&r->list, &old->list); |
| 1888 | iptcc_delete_rule(old); |
| 1889 | |
| 1890 | set_changed(*handle); |
| 1891 | |
| 1892 | return 1; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1893 | } |
| 1894 | |
| Harald Welte | 0113fe7 | 2004-01-06 19:04:02 +0000 | [diff] [blame] | 1895 | /* Append entry `fw' to chain `chain'. Equivalent to insert with |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1896 | rulenum = length of chain. */ |
| 1897 | int |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1898 | TC_APPEND_ENTRY(const IPT_CHAINLABEL chain, |
| 1899 | const STRUCT_ENTRY *e, |
| 1900 | TC_HANDLE_T *handle) |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1901 | { |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1902 | struct chain_head *c; |
| 1903 | struct rule_head *r; |
| 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_APPEND_ENTRY; |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1906 | if (!(c = iptcc_find_label(chain, *handle))) { |
| 1907 | DEBUGP("unable to find chain `%s'\n", chain); |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1908 | errno = ENOENT; |
| 1909 | return 0; |
| 1910 | } |
| 1911 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1912 | if (!(r = iptcc_alloc_rule(c, e->next_offset))) { |
| 1913 | DEBUGP("unable to allocate rule for chain `%s'\n", chain); |
| 1914 | errno = ENOMEM; |
| Harald Welte | 0113fe7 | 2004-01-06 19:04:02 +0000 | [diff] [blame] | 1915 | return 0; |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1916 | } |
| Harald Welte | 0113fe7 | 2004-01-06 19:04:02 +0000 | [diff] [blame] | 1917 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1918 | memcpy(r->entry, e, e->next_offset); |
| 1919 | r->counter_map.maptype = COUNTER_MAP_SET; |
| 1920 | |
| 1921 | if (!iptcc_map_target(*handle, r)) { |
| Martin Josefsson | 1200953 | 2004-09-23 18:24:29 +0000 | [diff] [blame] | 1922 | DEBUGP("unable to map target of rule for chain `%s'\n", chain); |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1923 | free(r); |
| 1924 | return 0; |
| 1925 | } |
| 1926 | |
| 1927 | list_add_tail(&r->list, &c->rules); |
| 1928 | c->num_rules++; |
| 1929 | |
| 1930 | set_changed(*handle); |
| 1931 | |
| 1932 | return 1; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1933 | } |
| 1934 | |
| 1935 | static inline int |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1936 | match_different(const STRUCT_ENTRY_MATCH *a, |
| Rusty Russell | edf14cf | 2000-04-19 11:26:44 +0000 | [diff] [blame] | 1937 | const unsigned char *a_elems, |
| 1938 | const unsigned char *b_elems, |
| 1939 | unsigned char **maskptr) |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1940 | { |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1941 | const STRUCT_ENTRY_MATCH *b; |
| Rusty Russell | edf14cf | 2000-04-19 11:26:44 +0000 | [diff] [blame] | 1942 | unsigned int i; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1943 | |
| 1944 | /* Offset of b is the same as a. */ |
| Rusty Russell | 30fd6e5 | 2000-04-23 09:16:06 +0000 | [diff] [blame] | 1945 | b = (void *)b_elems + ((unsigned char *)a - a_elems); |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1946 | |
| Rusty Russell | 228e98d | 2000-04-27 10:28:06 +0000 | [diff] [blame] | 1947 | if (a->u.match_size != b->u.match_size) |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1948 | return 1; |
| 1949 | |
| Rusty Russell | 228e98d | 2000-04-27 10:28:06 +0000 | [diff] [blame] | 1950 | if (strcmp(a->u.user.name, b->u.user.name) != 0) |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1951 | return 1; |
| 1952 | |
| Rusty Russell | 73ef09b | 2000-07-03 10:24:04 +0000 | [diff] [blame] | 1953 | *maskptr += ALIGN(sizeof(*a)); |
| Rusty Russell | edf14cf | 2000-04-19 11:26:44 +0000 | [diff] [blame] | 1954 | |
| Rusty Russell | 73ef09b | 2000-07-03 10:24:04 +0000 | [diff] [blame] | 1955 | for (i = 0; i < a->u.match_size - ALIGN(sizeof(*a)); i++) |
| Rusty Russell | edf14cf | 2000-04-19 11:26:44 +0000 | [diff] [blame] | 1956 | if (((a->data[i] ^ b->data[i]) & (*maskptr)[i]) != 0) |
| Rusty Russell | 90e712a | 2000-03-29 04:19:26 +0000 | [diff] [blame] | 1957 | return 1; |
| Rusty Russell | edf14cf | 2000-04-19 11:26:44 +0000 | [diff] [blame] | 1958 | *maskptr += i; |
| 1959 | return 0; |
| 1960 | } |
| 1961 | |
| 1962 | static inline int |
| Rusty Russell | 733e54b | 2004-12-16 14:22:23 +0000 | [diff] [blame] | 1963 | 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] | 1964 | { |
| 1965 | unsigned int i; |
| Rusty Russell | 733e54b | 2004-12-16 14:22:23 +0000 | [diff] [blame] | 1966 | STRUCT_ENTRY_TARGET *ta, *tb; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1967 | |
| Rusty Russell | 733e54b | 2004-12-16 14:22:23 +0000 | [diff] [blame] | 1968 | if (a->type != b->type) |
| 1969 | return 0; |
| 1970 | |
| 1971 | ta = GET_TARGET(a->entry); |
| 1972 | tb = GET_TARGET(b->entry); |
| 1973 | |
| 1974 | switch (a->type) { |
| 1975 | case IPTCC_R_FALLTHROUGH: |
| 1976 | return 1; |
| 1977 | case IPTCC_R_JUMP: |
| 1978 | return a->jump == b->jump; |
| 1979 | case IPTCC_R_STANDARD: |
| 1980 | return ((STRUCT_STANDARD_TARGET *)ta)->verdict |
| 1981 | == ((STRUCT_STANDARD_TARGET *)tb)->verdict; |
| 1982 | case IPTCC_R_MODULE: |
| 1983 | if (ta->u.target_size != tb->u.target_size) |
| 1984 | return 0; |
| 1985 | if (strcmp(ta->u.user.name, tb->u.user.name) != 0) |
| 1986 | return 0; |
| 1987 | |
| 1988 | for (i = 0; i < ta->u.target_size - sizeof(*ta); i++) |
| Rusty Russell | daade44 | 2004-12-29 11:14:52 +0000 | [diff] [blame] | 1989 | if (((ta->data[i] ^ tb->data[i]) & mask[i]) != 0) |
| Rusty Russell | 733e54b | 2004-12-16 14:22:23 +0000 | [diff] [blame] | 1990 | return 0; |
| 1991 | return 1; |
| 1992 | default: |
| 1993 | fprintf(stderr, "ERROR: bad type %i\n", a->type); |
| 1994 | abort(); |
| 1995 | } |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1996 | } |
| 1997 | |
| Rusty Russell | 733e54b | 2004-12-16 14:22:23 +0000 | [diff] [blame] | 1998 | static unsigned char * |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1999 | is_same(const STRUCT_ENTRY *a, |
| 2000 | const STRUCT_ENTRY *b, |
| 2001 | unsigned char *matchmask); |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2002 | |
| Harald Welte | 0113fe7 | 2004-01-06 19:04:02 +0000 | [diff] [blame] | 2003 | /* Delete the first rule in `chain' which matches `fw'. */ |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2004 | int |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 2005 | TC_DELETE_ENTRY(const IPT_CHAINLABEL chain, |
| 2006 | const STRUCT_ENTRY *origfw, |
| 2007 | unsigned char *matchmask, |
| 2008 | TC_HANDLE_T *handle) |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2009 | { |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2010 | struct chain_head *c; |
| Rusty Russell | e45c713 | 2004-12-16 13:21:44 +0000 | [diff] [blame] | 2011 | struct rule_head *r, *i; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2012 | |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 2013 | iptc_fn = TC_DELETE_ENTRY; |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2014 | if (!(c = iptcc_find_label(chain, *handle))) { |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2015 | errno = ENOENT; |
| 2016 | return 0; |
| 2017 | } |
| 2018 | |
| Rusty Russell | e45c713 | 2004-12-16 13:21:44 +0000 | [diff] [blame] | 2019 | /* Create a rule_head from origfw. */ |
| 2020 | r = iptcc_alloc_rule(c, origfw->next_offset); |
| 2021 | if (!r) { |
| Harald Welte | 0113fe7 | 2004-01-06 19:04:02 +0000 | [diff] [blame] | 2022 | errno = ENOMEM; |
| 2023 | return 0; |
| 2024 | } |
| 2025 | |
| Rusty Russell | e45c713 | 2004-12-16 13:21:44 +0000 | [diff] [blame] | 2026 | memcpy(r->entry, origfw, origfw->next_offset); |
| 2027 | r->counter_map.maptype = COUNTER_MAP_NOMAP; |
| 2028 | if (!iptcc_map_target(*handle, r)) { |
| 2029 | DEBUGP("unable to map target of rule for chain `%s'\n", chain); |
| 2030 | free(r); |
| 2031 | return 0; |
| Patrick McHardyJesper Brouer | 04a1e4c | 2006-07-25 01:50:48 +0000 | [diff] [blame] | 2032 | } else { |
| 2033 | /* iptcc_map_target increment target chain references |
| 2034 | * since this is a fake rule only used for matching |
| 2035 | * the chain references count is decremented again. |
| 2036 | */ |
| 2037 | if (r->type == IPTCC_R_JUMP |
| 2038 | && r->jump) |
| 2039 | r->jump->references--; |
| Rusty Russell | e45c713 | 2004-12-16 13:21:44 +0000 | [diff] [blame] | 2040 | } |
| Harald Welte | 0113fe7 | 2004-01-06 19:04:02 +0000 | [diff] [blame] | 2041 | |
| Rusty Russell | e45c713 | 2004-12-16 13:21:44 +0000 | [diff] [blame] | 2042 | list_for_each_entry(i, &c->rules, list) { |
| Rusty Russell | 733e54b | 2004-12-16 14:22:23 +0000 | [diff] [blame] | 2043 | unsigned char *mask; |
| Harald Welte | fe53707 | 2004-08-30 20:28:53 +0000 | [diff] [blame] | 2044 | |
| Rusty Russell | 733e54b | 2004-12-16 14:22:23 +0000 | [diff] [blame] | 2045 | mask = is_same(r->entry, i->entry, matchmask); |
| 2046 | if (!mask) |
| 2047 | continue; |
| Martin Josefsson | 2a5dbbb | 2004-09-22 21:37:41 +0000 | [diff] [blame] | 2048 | |
| Rusty Russell | 733e54b | 2004-12-16 14:22:23 +0000 | [diff] [blame] | 2049 | if (!target_same(r, i, mask)) |
| 2050 | continue; |
| 2051 | |
| 2052 | /* If we are about to delete the rule that is the |
| 2053 | * current iterator, move rule iterator back. next |
| 2054 | * pointer will then point to real next node */ |
| 2055 | if (i == (*handle)->rule_iterator_cur) { |
| 2056 | (*handle)->rule_iterator_cur = |
| 2057 | list_entry((*handle)->rule_iterator_cur->list.prev, |
| 2058 | struct rule_head, list); |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2059 | } |
| Rusty Russell | 733e54b | 2004-12-16 14:22:23 +0000 | [diff] [blame] | 2060 | |
| 2061 | c->num_rules--; |
| 2062 | iptcc_delete_rule(i); |
| 2063 | |
| 2064 | set_changed(*handle); |
| 2065 | free(r); |
| 2066 | return 1; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2067 | } |
| 2068 | |
| Rusty Russell | e45c713 | 2004-12-16 13:21:44 +0000 | [diff] [blame] | 2069 | free(r); |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2070 | errno = ENOENT; |
| 2071 | return 0; |
| Rusty Russell | 7e53bf9 | 2000-03-20 07:03:28 +0000 | [diff] [blame] | 2072 | } |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2073 | |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2074 | |
| 2075 | /* Delete the rule in position `rulenum' in `chain'. */ |
| 2076 | int |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 2077 | TC_DELETE_NUM_ENTRY(const IPT_CHAINLABEL chain, |
| 2078 | unsigned int rulenum, |
| 2079 | TC_HANDLE_T *handle) |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2080 | { |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2081 | struct chain_head *c; |
| 2082 | struct rule_head *r; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2083 | |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 2084 | iptc_fn = TC_DELETE_NUM_ENTRY; |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2085 | |
| 2086 | if (!(c = iptcc_find_label(chain, *handle))) { |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2087 | errno = ENOENT; |
| 2088 | return 0; |
| 2089 | } |
| 2090 | |
| Martin Josefsson | a5616dc | 2004-10-24 22:27:31 +0000 | [diff] [blame] | 2091 | if (rulenum >= c->num_rules) { |
| Martin Josefsson | 631f361 | 2004-09-23 19:25:06 +0000 | [diff] [blame] | 2092 | errno = E2BIG; |
| 2093 | return 0; |
| 2094 | } |
| 2095 | |
| 2096 | /* Take advantage of the double linked list if possible. */ |
| Martin Josefsson | a5616dc | 2004-10-24 22:27:31 +0000 | [diff] [blame] | 2097 | if (rulenum + 1 <= c->num_rules/2) { |
| 2098 | r = iptcc_get_rule_num(c, rulenum + 1); |
| 2099 | } else { |
| 2100 | r = iptcc_get_rule_num_reverse(c, c->num_rules - rulenum); |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2101 | } |
| 2102 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2103 | /* If we are about to delete the rule that is the current |
| 2104 | * iterator, move rule iterator back. next pointer will then |
| 2105 | * point to real next node */ |
| 2106 | if (r == (*handle)->rule_iterator_cur) { |
| 2107 | (*handle)->rule_iterator_cur = |
| 2108 | list_entry((*handle)->rule_iterator_cur->list.prev, |
| 2109 | struct rule_head, list); |
| Harald Welte | 0113fe7 | 2004-01-06 19:04:02 +0000 | [diff] [blame] | 2110 | } |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2111 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2112 | c->num_rules--; |
| 2113 | iptcc_delete_rule(r); |
| 2114 | |
| Martin Josefsson | 2a5dbbb | 2004-09-22 21:37:41 +0000 | [diff] [blame] | 2115 | set_changed(*handle); |
| 2116 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2117 | return 1; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2118 | } |
| 2119 | |
| 2120 | /* Check the packet `fw' on chain `chain'. Returns the verdict, or |
| 2121 | NULL and sets errno. */ |
| 2122 | const char * |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 2123 | TC_CHECK_PACKET(const IPT_CHAINLABEL chain, |
| 2124 | STRUCT_ENTRY *entry, |
| 2125 | TC_HANDLE_T *handle) |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2126 | { |
| Derrik Pates | 664c0a3 | 2005-02-01 13:28:14 +0000 | [diff] [blame] | 2127 | iptc_fn = TC_CHECK_PACKET; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2128 | errno = ENOSYS; |
| 2129 | return NULL; |
| 2130 | } |
| 2131 | |
| 2132 | /* Flushes the entries in the given chain (ie. empties chain). */ |
| 2133 | int |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 2134 | TC_FLUSH_ENTRIES(const IPT_CHAINLABEL chain, TC_HANDLE_T *handle) |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2135 | { |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2136 | struct chain_head *c; |
| 2137 | struct rule_head *r, *tmp; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2138 | |
| Harald Welte | 0113fe7 | 2004-01-06 19:04:02 +0000 | [diff] [blame] | 2139 | iptc_fn = TC_FLUSH_ENTRIES; |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2140 | if (!(c = iptcc_find_label(chain, *handle))) { |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2141 | errno = ENOENT; |
| 2142 | return 0; |
| 2143 | } |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2144 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2145 | list_for_each_entry_safe(r, tmp, &c->rules, list) { |
| 2146 | iptcc_delete_rule(r); |
| 2147 | } |
| 2148 | |
| 2149 | c->num_rules = 0; |
| 2150 | |
| 2151 | set_changed(*handle); |
| 2152 | |
| 2153 | return 1; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2154 | } |
| 2155 | |
| 2156 | /* Zeroes the counters in a chain. */ |
| 2157 | int |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 2158 | TC_ZERO_ENTRIES(const IPT_CHAINLABEL chain, TC_HANDLE_T *handle) |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2159 | { |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2160 | struct chain_head *c; |
| 2161 | struct rule_head *r; |
| Rusty Russell | 7e53bf9 | 2000-03-20 07:03:28 +0000 | [diff] [blame] | 2162 | |
| Derrik Pates | 664c0a3 | 2005-02-01 13:28:14 +0000 | [diff] [blame] | 2163 | iptc_fn = TC_ZERO_ENTRIES; |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2164 | if (!(c = iptcc_find_label(chain, *handle))) { |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2165 | errno = ENOENT; |
| 2166 | return 0; |
| 2167 | } |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2168 | |
| Andy Gay | e5bd1d7 | 2006-08-22 02:56:41 +0000 | [diff] [blame] | 2169 | if (c->counter_map.maptype == COUNTER_MAP_NORMAL_MAP) |
| 2170 | c->counter_map.maptype = COUNTER_MAP_ZEROED; |
| 2171 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2172 | list_for_each_entry(r, &c->rules, list) { |
| 2173 | if (r->counter_map.maptype == COUNTER_MAP_NORMAL_MAP) |
| 2174 | r->counter_map.maptype = COUNTER_MAP_ZEROED; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2175 | } |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2176 | |
| Rusty Russell | 175f641 | 2000-03-24 09:32:20 +0000 | [diff] [blame] | 2177 | set_changed(*handle); |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2178 | |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2179 | return 1; |
| 2180 | } |
| 2181 | |
| Harald Welte | 1cef74d | 2001-01-05 15:22:59 +0000 | [diff] [blame] | 2182 | STRUCT_COUNTERS * |
| 2183 | TC_READ_COUNTER(const IPT_CHAINLABEL chain, |
| 2184 | unsigned int rulenum, |
| 2185 | TC_HANDLE_T *handle) |
| 2186 | { |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2187 | struct chain_head *c; |
| 2188 | struct rule_head *r; |
| Harald Welte | 1cef74d | 2001-01-05 15:22:59 +0000 | [diff] [blame] | 2189 | |
| 2190 | iptc_fn = TC_READ_COUNTER; |
| 2191 | CHECK(*handle); |
| 2192 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2193 | if (!(c = iptcc_find_label(chain, *handle))) { |
| Harald Welte | 1cef74d | 2001-01-05 15:22:59 +0000 | [diff] [blame] | 2194 | errno = ENOENT; |
| 2195 | return NULL; |
| 2196 | } |
| 2197 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2198 | if (!(r = iptcc_get_rule_num(c, rulenum))) { |
| Harald Welte | 0113fe7 | 2004-01-06 19:04:02 +0000 | [diff] [blame] | 2199 | errno = E2BIG; |
| 2200 | return NULL; |
| 2201 | } |
| 2202 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2203 | return &r->entry[0].counters; |
| Harald Welte | 1cef74d | 2001-01-05 15:22:59 +0000 | [diff] [blame] | 2204 | } |
| 2205 | |
| 2206 | int |
| 2207 | TC_ZERO_COUNTER(const IPT_CHAINLABEL chain, |
| 2208 | unsigned int rulenum, |
| 2209 | TC_HANDLE_T *handle) |
| 2210 | { |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2211 | struct chain_head *c; |
| 2212 | struct rule_head *r; |
| Harald Welte | 1cef74d | 2001-01-05 15:22:59 +0000 | [diff] [blame] | 2213 | |
| 2214 | iptc_fn = TC_ZERO_COUNTER; |
| 2215 | CHECK(*handle); |
| 2216 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2217 | if (!(c = iptcc_find_label(chain, *handle))) { |
| Harald Welte | 1cef74d | 2001-01-05 15:22:59 +0000 | [diff] [blame] | 2218 | errno = ENOENT; |
| 2219 | return 0; |
| 2220 | } |
| 2221 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2222 | if (!(r = iptcc_get_rule_num(c, rulenum))) { |
| Harald Welte | 0113fe7 | 2004-01-06 19:04:02 +0000 | [diff] [blame] | 2223 | errno = E2BIG; |
| 2224 | return 0; |
| 2225 | } |
| 2226 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2227 | if (r->counter_map.maptype == COUNTER_MAP_NORMAL_MAP) |
| 2228 | r->counter_map.maptype = COUNTER_MAP_ZEROED; |
| Harald Welte | 1cef74d | 2001-01-05 15:22:59 +0000 | [diff] [blame] | 2229 | |
| 2230 | set_changed(*handle); |
| 2231 | |
| 2232 | return 1; |
| 2233 | } |
| 2234 | |
| 2235 | int |
| 2236 | TC_SET_COUNTER(const IPT_CHAINLABEL chain, |
| 2237 | unsigned int rulenum, |
| 2238 | STRUCT_COUNTERS *counters, |
| 2239 | TC_HANDLE_T *handle) |
| 2240 | { |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2241 | struct chain_head *c; |
| 2242 | struct rule_head *r; |
| Harald Welte | 1cef74d | 2001-01-05 15:22:59 +0000 | [diff] [blame] | 2243 | STRUCT_ENTRY *e; |
| Harald Welte | 1cef74d | 2001-01-05 15:22:59 +0000 | [diff] [blame] | 2244 | |
| 2245 | iptc_fn = TC_SET_COUNTER; |
| 2246 | CHECK(*handle); |
| 2247 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2248 | if (!(c = iptcc_find_label(chain, *handle))) { |
| Harald Welte | 1cef74d | 2001-01-05 15:22:59 +0000 | [diff] [blame] | 2249 | errno = ENOENT; |
| 2250 | return 0; |
| 2251 | } |
| Harald Welte | 0113fe7 | 2004-01-06 19:04:02 +0000 | [diff] [blame] | 2252 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2253 | if (!(r = iptcc_get_rule_num(c, rulenum))) { |
| Harald Welte | 0113fe7 | 2004-01-06 19:04:02 +0000 | [diff] [blame] | 2254 | errno = E2BIG; |
| 2255 | return 0; |
| 2256 | } |
| 2257 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2258 | e = r->entry; |
| 2259 | r->counter_map.maptype = COUNTER_MAP_SET; |
| Harald Welte | 0113fe7 | 2004-01-06 19:04:02 +0000 | [diff] [blame] | 2260 | |
| 2261 | memcpy(&e->counters, counters, sizeof(STRUCT_COUNTERS)); |
| Harald Welte | 1cef74d | 2001-01-05 15:22:59 +0000 | [diff] [blame] | 2262 | |
| 2263 | set_changed(*handle); |
| 2264 | |
| 2265 | return 1; |
| 2266 | } |
| 2267 | |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2268 | /* Creates a new chain. */ |
| 2269 | /* To create a chain, create two rules: error node and unconditional |
| 2270 | * return. */ |
| 2271 | int |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 2272 | TC_CREATE_CHAIN(const IPT_CHAINLABEL chain, TC_HANDLE_T *handle) |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2273 | { |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2274 | static struct chain_head *c; |
| Patrick McHardy | 1f23d3c | 2008-06-07 15:04:34 +0200 | [diff] [blame] | 2275 | int capacity; |
| 2276 | int exceeded; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2277 | |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 2278 | iptc_fn = TC_CREATE_CHAIN; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2279 | |
| 2280 | /* find_label doesn't cover built-in targets: DROP, ACCEPT, |
| 2281 | QUEUE, RETURN. */ |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2282 | if (iptcc_find_label(chain, *handle) |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 2283 | || strcmp(chain, LABEL_DROP) == 0 |
| 2284 | || strcmp(chain, LABEL_ACCEPT) == 0 |
| Rusty Russell | 67088e7 | 2000-05-10 01:18:57 +0000 | [diff] [blame] | 2285 | || strcmp(chain, LABEL_QUEUE) == 0 |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 2286 | || strcmp(chain, LABEL_RETURN) == 0) { |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2287 | DEBUGP("Chain `%s' already exists\n", chain); |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2288 | errno = EEXIST; |
| 2289 | return 0; |
| 2290 | } |
| 2291 | |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 2292 | if (strlen(chain)+1 > sizeof(IPT_CHAINLABEL)) { |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2293 | DEBUGP("Chain name `%s' too long\n", chain); |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2294 | errno = EINVAL; |
| 2295 | return 0; |
| 2296 | } |
| 2297 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2298 | c = iptcc_alloc_chain_head(chain, 0); |
| 2299 | if (!c) { |
| 2300 | DEBUGP("Cannot allocate memory for chain `%s'\n", chain); |
| 2301 | errno = ENOMEM; |
| 2302 | return 0; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2303 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2304 | } |
| Jesper Dangaard Brouer | 48bde40 | 2008-01-15 17:06:48 +0000 | [diff] [blame] | 2305 | (*handle)->num_chains++; /* New user defined chain */ |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2306 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2307 | DEBUGP("Creating chain `%s'\n", chain); |
| Jesper Dangaard Brouer | d8cb787 | 2007-11-28 08:40:26 +0000 | [diff] [blame] | 2308 | iptc_insert_chain(*handle, c); /* Insert sorted */ |
| Harald Welte | 0113fe7 | 2004-01-06 19:04:02 +0000 | [diff] [blame] | 2309 | |
| Jesper Dangaard Brouer | 01444da | 2008-01-15 17:18:15 +0000 | [diff] [blame] | 2310 | /* Inserting chains don't change the correctness of the chain |
| 2311 | * index (except if its smaller than index[0], but that |
| 2312 | * handled by iptc_insert_chain). It only causes longer lists |
| 2313 | * in the buckets. Thus, only rebuild chain index when the |
| 2314 | * capacity is exceed with CHAIN_INDEX_INSERT_MAX chains. |
| 2315 | */ |
| Patrick McHardy | 1f23d3c | 2008-06-07 15:04:34 +0200 | [diff] [blame] | 2316 | capacity = (*handle)->chain_index_sz * CHAIN_INDEX_BUCKET_LEN; |
| 2317 | exceeded = ((((*handle)->num_chains)-capacity)); |
| Jesper Dangaard Brouer | 01444da | 2008-01-15 17:18:15 +0000 | [diff] [blame] | 2318 | if (exceeded > CHAIN_INDEX_INSERT_MAX) { |
| 2319 | debug("Capacity(%d) exceeded(%d) rebuild (chains:%d)\n", |
| 2320 | capacity, exceeded, (*handle)->num_chains); |
| 2321 | iptcc_chain_index_rebuild(*handle); |
| 2322 | } |
| 2323 | |
| Harald Welte | 0113fe7 | 2004-01-06 19:04:02 +0000 | [diff] [blame] | 2324 | set_changed(*handle); |
| 2325 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2326 | return 1; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2327 | } |
| 2328 | |
| 2329 | /* Get the number of references to this chain. */ |
| 2330 | int |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 2331 | TC_GET_REFERENCES(unsigned int *ref, const IPT_CHAINLABEL chain, |
| 2332 | TC_HANDLE_T *handle) |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2333 | { |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2334 | struct chain_head *c; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2335 | |
| Derrik Pates | 664c0a3 | 2005-02-01 13:28:14 +0000 | [diff] [blame] | 2336 | iptc_fn = TC_GET_REFERENCES; |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2337 | if (!(c = iptcc_find_label(chain, *handle))) { |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2338 | errno = ENOENT; |
| 2339 | return 0; |
| 2340 | } |
| 2341 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2342 | *ref = c->references; |
| 2343 | |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2344 | return 1; |
| 2345 | } |
| 2346 | |
| 2347 | /* Deletes a chain. */ |
| 2348 | int |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 2349 | TC_DELETE_CHAIN(const IPT_CHAINLABEL chain, TC_HANDLE_T *handle) |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2350 | { |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2351 | unsigned int references; |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2352 | struct chain_head *c; |
| Rusty Russell | 7e53bf9 | 2000-03-20 07:03:28 +0000 | [diff] [blame] | 2353 | |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 2354 | iptc_fn = TC_DELETE_CHAIN; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2355 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2356 | if (!(c = iptcc_find_label(chain, *handle))) { |
| 2357 | DEBUGP("cannot find chain `%s'\n", chain); |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2358 | errno = ENOENT; |
| 2359 | return 0; |
| 2360 | } |
| 2361 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2362 | if (TC_BUILTIN(chain, *handle)) { |
| 2363 | DEBUGP("cannot remove builtin chain `%s'\n", chain); |
| 2364 | errno = EINVAL; |
| 2365 | return 0; |
| 2366 | } |
| 2367 | |
| 2368 | if (!TC_GET_REFERENCES(&references, chain, handle)) { |
| 2369 | DEBUGP("cannot get references on chain `%s'\n", chain); |
| 2370 | return 0; |
| 2371 | } |
| 2372 | |
| 2373 | if (references > 0) { |
| 2374 | DEBUGP("chain `%s' still has references\n", chain); |
| 2375 | errno = EMLINK; |
| 2376 | return 0; |
| 2377 | } |
| 2378 | |
| 2379 | if (c->num_rules) { |
| 2380 | DEBUGP("chain `%s' is not empty\n", chain); |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2381 | errno = ENOTEMPTY; |
| 2382 | return 0; |
| 2383 | } |
| 2384 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2385 | /* If we are about to delete the chain that is the current |
| Jesper Dangaard Brouer | 48bde40 | 2008-01-15 17:06:48 +0000 | [diff] [blame] | 2386 | * iterator, move chain iterator forward. */ |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2387 | if (c == (*handle)->chain_iterator_cur) |
| 2388 | iptcc_chain_iterator_advance(*handle); |
| Harald Welte | 0113fe7 | 2004-01-06 19:04:02 +0000 | [diff] [blame] | 2389 | |
| Jesper Dangaard Brouer | 48bde40 | 2008-01-15 17:06:48 +0000 | [diff] [blame] | 2390 | (*handle)->num_chains--; /* One user defined chain deleted */ |
| 2391 | |
| Jesper Dangaard Brouer | 01444da | 2008-01-15 17:18:15 +0000 | [diff] [blame] | 2392 | //list_del(&c->list); /* Done in iptcc_chain_index_delete_chain() */ |
| 2393 | iptcc_chain_index_delete_chain(c, *handle); |
| 2394 | free(c); |
| 2395 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2396 | DEBUGP("chain `%s' deleted\n", chain); |
| 2397 | |
| 2398 | set_changed(*handle); |
| 2399 | |
| 2400 | return 1; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2401 | } |
| 2402 | |
| 2403 | /* Renames a chain. */ |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 2404 | int TC_RENAME_CHAIN(const IPT_CHAINLABEL oldname, |
| 2405 | const IPT_CHAINLABEL newname, |
| 2406 | TC_HANDLE_T *handle) |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2407 | { |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2408 | struct chain_head *c; |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 2409 | iptc_fn = TC_RENAME_CHAIN; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2410 | |
| Harald Welte | 1de8046 | 2000-10-30 12:00:27 +0000 | [diff] [blame] | 2411 | /* find_label doesn't cover built-in targets: DROP, ACCEPT, |
| 2412 | QUEUE, RETURN. */ |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2413 | if (iptcc_find_label(newname, *handle) |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 2414 | || strcmp(newname, LABEL_DROP) == 0 |
| 2415 | || strcmp(newname, LABEL_ACCEPT) == 0 |
| Harald Welte | 1de8046 | 2000-10-30 12:00:27 +0000 | [diff] [blame] | 2416 | || strcmp(newname, LABEL_QUEUE) == 0 |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 2417 | || strcmp(newname, LABEL_RETURN) == 0) { |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2418 | errno = EEXIST; |
| 2419 | return 0; |
| 2420 | } |
| 2421 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2422 | if (!(c = iptcc_find_label(oldname, *handle)) |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 2423 | || TC_BUILTIN(oldname, *handle)) { |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2424 | errno = ENOENT; |
| 2425 | return 0; |
| 2426 | } |
| 2427 | |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 2428 | if (strlen(newname)+1 > sizeof(IPT_CHAINLABEL)) { |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2429 | errno = EINVAL; |
| 2430 | return 0; |
| 2431 | } |
| 2432 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2433 | strncpy(c->name, newname, sizeof(IPT_CHAINLABEL)); |
| 2434 | |
| Harald Welte | 0113fe7 | 2004-01-06 19:04:02 +0000 | [diff] [blame] | 2435 | set_changed(*handle); |
| 2436 | |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2437 | return 1; |
| 2438 | } |
| 2439 | |
| 2440 | /* Sets the policy on a built-in chain. */ |
| 2441 | int |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 2442 | TC_SET_POLICY(const IPT_CHAINLABEL chain, |
| 2443 | const IPT_CHAINLABEL policy, |
| Harald Welte | 1cef74d | 2001-01-05 15:22:59 +0000 | [diff] [blame] | 2444 | STRUCT_COUNTERS *counters, |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 2445 | TC_HANDLE_T *handle) |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2446 | { |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2447 | struct chain_head *c; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2448 | |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 2449 | iptc_fn = TC_SET_POLICY; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2450 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2451 | if (!(c = iptcc_find_label(chain, *handle))) { |
| 2452 | DEBUGP("cannot find chain `%s'\n", chain); |
| 2453 | errno = ENOENT; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2454 | return 0; |
| 2455 | } |
| 2456 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2457 | if (!iptcc_is_builtin(c)) { |
| 2458 | DEBUGP("cannot set policy of userdefinedchain `%s'\n", chain); |
| 2459 | errno = ENOENT; |
| 2460 | return 0; |
| 2461 | } |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2462 | |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 2463 | if (strcmp(policy, LABEL_ACCEPT) == 0) |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2464 | c->verdict = -NF_ACCEPT - 1; |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 2465 | else if (strcmp(policy, LABEL_DROP) == 0) |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2466 | c->verdict = -NF_DROP - 1; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2467 | else { |
| 2468 | errno = EINVAL; |
| 2469 | return 0; |
| 2470 | } |
| Harald Welte | 1cef74d | 2001-01-05 15:22:59 +0000 | [diff] [blame] | 2471 | |
| Harald Welte | 1cef74d | 2001-01-05 15:22:59 +0000 | [diff] [blame] | 2472 | if (counters) { |
| 2473 | /* set byte and packet counters */ |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2474 | memcpy(&c->counters, counters, sizeof(STRUCT_COUNTERS)); |
| 2475 | c->counter_map.maptype = COUNTER_MAP_SET; |
| Harald Welte | 1cef74d | 2001-01-05 15:22:59 +0000 | [diff] [blame] | 2476 | } else { |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2477 | c->counter_map.maptype = COUNTER_MAP_NOMAP; |
| Harald Welte | 1cef74d | 2001-01-05 15:22:59 +0000 | [diff] [blame] | 2478 | } |
| 2479 | |
| Rusty Russell | 175f641 | 2000-03-24 09:32:20 +0000 | [diff] [blame] | 2480 | set_changed(*handle); |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2481 | |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2482 | return 1; |
| 2483 | } |
| 2484 | |
| 2485 | /* Without this, on gcc 2.7.2.3, we get: |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 2486 | libiptc.c: In function `TC_COMMIT': |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2487 | libiptc.c:833: fixed or forbidden register was spilled. |
| 2488 | This may be due to a compiler bug or to impossible asm |
| 2489 | statements or clauses. |
| 2490 | */ |
| 2491 | static void |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 2492 | subtract_counters(STRUCT_COUNTERS *answer, |
| 2493 | const STRUCT_COUNTERS *a, |
| 2494 | const STRUCT_COUNTERS *b) |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2495 | { |
| 2496 | answer->pcnt = a->pcnt - b->pcnt; |
| 2497 | answer->bcnt = a->bcnt - b->bcnt; |
| 2498 | } |
| 2499 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2500 | |
| Jan Engelhardt | dbb7754 | 2008-02-11 00:33:30 +0100 | [diff] [blame] | 2501 | static void counters_nomap(STRUCT_COUNTERS_INFO *newcounters, unsigned int idx) |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2502 | { |
| Jan Engelhardt | dbb7754 | 2008-02-11 00:33:30 +0100 | [diff] [blame] | 2503 | newcounters->counters[idx] = ((STRUCT_COUNTERS) { 0, 0}); |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2504 | DEBUGP_C("NOMAP => zero\n"); |
| 2505 | } |
| 2506 | |
| 2507 | static void counters_normal_map(STRUCT_COUNTERS_INFO *newcounters, |
| Jan Engelhardt | dbb7754 | 2008-02-11 00:33:30 +0100 | [diff] [blame] | 2508 | STRUCT_REPLACE *repl, unsigned int idx, |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2509 | unsigned int mappos) |
| 2510 | { |
| 2511 | /* Original read: X. |
| 2512 | * Atomic read on replacement: X + Y. |
| 2513 | * Currently in kernel: Z. |
| 2514 | * Want in kernel: X + Y + Z. |
| 2515 | * => Add in X + Y |
| 2516 | * => Add in replacement read. |
| 2517 | */ |
| Jan Engelhardt | dbb7754 | 2008-02-11 00:33:30 +0100 | [diff] [blame] | 2518 | newcounters->counters[idx] = repl->counters[mappos]; |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2519 | DEBUGP_C("NORMAL_MAP => mappos %u \n", mappos); |
| 2520 | } |
| 2521 | |
| 2522 | static void counters_map_zeroed(STRUCT_COUNTERS_INFO *newcounters, |
| Jan Engelhardt | dbb7754 | 2008-02-11 00:33:30 +0100 | [diff] [blame] | 2523 | STRUCT_REPLACE *repl, unsigned int idx, |
| 2524 | unsigned int mappos, STRUCT_COUNTERS *counters) |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2525 | { |
| 2526 | /* Original read: X. |
| 2527 | * Atomic read on replacement: X + Y. |
| 2528 | * Currently in kernel: Z. |
| 2529 | * Want in kernel: Y + Z. |
| 2530 | * => Add in Y. |
| 2531 | * => Add in (replacement read - original read). |
| 2532 | */ |
| Jan Engelhardt | dbb7754 | 2008-02-11 00:33:30 +0100 | [diff] [blame] | 2533 | subtract_counters(&newcounters->counters[idx], |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2534 | &repl->counters[mappos], |
| 2535 | counters); |
| 2536 | DEBUGP_C("ZEROED => mappos %u\n", mappos); |
| 2537 | } |
| 2538 | |
| 2539 | static void counters_map_set(STRUCT_COUNTERS_INFO *newcounters, |
| Jan Engelhardt | dbb7754 | 2008-02-11 00:33:30 +0100 | [diff] [blame] | 2540 | unsigned int idx, STRUCT_COUNTERS *counters) |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2541 | { |
| 2542 | /* Want to set counter (iptables-restore) */ |
| 2543 | |
| Jan Engelhardt | dbb7754 | 2008-02-11 00:33:30 +0100 | [diff] [blame] | 2544 | memcpy(&newcounters->counters[idx], counters, |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2545 | sizeof(STRUCT_COUNTERS)); |
| 2546 | |
| 2547 | DEBUGP_C("SET\n"); |
| 2548 | } |
| 2549 | |
| 2550 | |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2551 | int |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 2552 | TC_COMMIT(TC_HANDLE_T *handle) |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2553 | { |
| 2554 | /* Replace, then map back the counters. */ |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 2555 | STRUCT_REPLACE *repl; |
| 2556 | STRUCT_COUNTERS_INFO *newcounters; |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2557 | struct chain_head *c; |
| 2558 | int ret; |
| Martin Josefsson | 841e4ae | 2003-05-02 15:30:11 +0000 | [diff] [blame] | 2559 | size_t counterlen; |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2560 | int new_number; |
| 2561 | unsigned int new_size; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2562 | |
| Derrik Pates | 664c0a3 | 2005-02-01 13:28:14 +0000 | [diff] [blame] | 2563 | iptc_fn = TC_COMMIT; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2564 | CHECK(*handle); |
| Martin Josefsson | 841e4ae | 2003-05-02 15:30:11 +0000 | [diff] [blame] | 2565 | |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2566 | /* Don't commit if nothing changed. */ |
| 2567 | if (!(*handle)->changed) |
| 2568 | goto finished; |
| 2569 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2570 | new_number = iptcc_compile_table_prep(*handle, &new_size); |
| 2571 | if (new_number < 0) { |
| 2572 | errno = ENOMEM; |
| Harald Welte | d6ba6f5 | 2005-11-12 10:39:40 +0000 | [diff] [blame] | 2573 | goto out_zero; |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2574 | } |
| 2575 | |
| 2576 | repl = malloc(sizeof(*repl) + new_size); |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2577 | if (!repl) { |
| 2578 | errno = ENOMEM; |
| Harald Welte | d6ba6f5 | 2005-11-12 10:39:40 +0000 | [diff] [blame] | 2579 | goto out_zero; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2580 | } |
| Martin Josefsson | ad3b4f9 | 2004-09-22 22:04:07 +0000 | [diff] [blame] | 2581 | memset(repl, 0, sizeof(*repl) + new_size); |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2582 | |
| Rusty Russell | e45c713 | 2004-12-16 13:21:44 +0000 | [diff] [blame] | 2583 | #if 0 |
| 2584 | TC_DUMP_ENTRIES(*handle); |
| 2585 | #endif |
| 2586 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2587 | counterlen = sizeof(STRUCT_COUNTERS_INFO) |
| 2588 | + sizeof(STRUCT_COUNTERS) * new_number; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2589 | |
| 2590 | /* These are the old counters we will get from kernel */ |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 2591 | repl->counters = malloc(sizeof(STRUCT_COUNTERS) |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2592 | * (*handle)->info.num_entries); |
| 2593 | if (!repl->counters) { |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2594 | errno = ENOMEM; |
| Harald Welte | d6ba6f5 | 2005-11-12 10:39:40 +0000 | [diff] [blame] | 2595 | goto out_free_repl; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2596 | } |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2597 | /* These are the counters we're going to put back, later. */ |
| 2598 | newcounters = malloc(counterlen); |
| 2599 | if (!newcounters) { |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2600 | errno = ENOMEM; |
| Harald Welte | d6ba6f5 | 2005-11-12 10:39:40 +0000 | [diff] [blame] | 2601 | goto out_free_repl_counters; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2602 | } |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2603 | memset(newcounters, 0, counterlen); |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2604 | |
| 2605 | strcpy(repl->name, (*handle)->info.name); |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2606 | repl->num_entries = new_number; |
| 2607 | repl->size = new_size; |
| 2608 | |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2609 | repl->num_counters = (*handle)->info.num_entries; |
| 2610 | repl->valid_hooks = (*handle)->info.valid_hooks; |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2611 | |
| 2612 | DEBUGP("num_entries=%u, size=%u, num_counters=%u\n", |
| 2613 | repl->num_entries, repl->size, repl->num_counters); |
| 2614 | |
| 2615 | ret = iptcc_compile_table(*handle, repl); |
| 2616 | if (ret < 0) { |
| 2617 | errno = ret; |
| Harald Welte | d6ba6f5 | 2005-11-12 10:39:40 +0000 | [diff] [blame] | 2618 | goto out_free_newcounters; |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2619 | } |
| 2620 | |
| 2621 | |
| 2622 | #ifdef IPTC_DEBUG2 |
| 2623 | { |
| 2624 | int fd = open("/tmp/libiptc-so_set_replace.blob", |
| 2625 | O_CREAT|O_WRONLY); |
| 2626 | if (fd >= 0) { |
| 2627 | write(fd, repl, sizeof(*repl) + repl->size); |
| 2628 | close(fd); |
| 2629 | } |
| 2630 | } |
| 2631 | #endif |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2632 | |
| Harald Welte | d6ba6f5 | 2005-11-12 10:39:40 +0000 | [diff] [blame] | 2633 | ret = setsockopt(sockfd, TC_IPPROTO, SO_SET_REPLACE, repl, |
| 2634 | sizeof(*repl) + repl->size); |
| Patrick McHardy | e0865ad | 2006-04-22 02:08:56 +0000 | [diff] [blame] | 2635 | if (ret < 0) |
| Harald Welte | d6ba6f5 | 2005-11-12 10:39:40 +0000 | [diff] [blame] | 2636 | goto out_free_newcounters; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2637 | |
| 2638 | /* Put counters back. */ |
| 2639 | strcpy(newcounters->name, (*handle)->info.name); |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2640 | newcounters->num_counters = new_number; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2641 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2642 | list_for_each_entry(c, &(*handle)->chains, list) { |
| 2643 | struct rule_head *r; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2644 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2645 | /* Builtin chains have their own counters */ |
| 2646 | if (iptcc_is_builtin(c)) { |
| 2647 | DEBUGP("counter for chain-index %u: ", c->foot_index); |
| 2648 | switch(c->counter_map.maptype) { |
| 2649 | case COUNTER_MAP_NOMAP: |
| 2650 | counters_nomap(newcounters, c->foot_index); |
| 2651 | break; |
| 2652 | case COUNTER_MAP_NORMAL_MAP: |
| 2653 | counters_normal_map(newcounters, repl, |
| 2654 | c->foot_index, |
| 2655 | c->counter_map.mappos); |
| 2656 | break; |
| 2657 | case COUNTER_MAP_ZEROED: |
| 2658 | counters_map_zeroed(newcounters, repl, |
| 2659 | c->foot_index, |
| 2660 | c->counter_map.mappos, |
| 2661 | &c->counters); |
| 2662 | break; |
| 2663 | case COUNTER_MAP_SET: |
| 2664 | counters_map_set(newcounters, c->foot_index, |
| 2665 | &c->counters); |
| 2666 | break; |
| 2667 | } |
| 2668 | } |
| Harald Welte | 1cef74d | 2001-01-05 15:22:59 +0000 | [diff] [blame] | 2669 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2670 | list_for_each_entry(r, &c->rules, list) { |
| 2671 | DEBUGP("counter for index %u: ", r->index); |
| 2672 | switch (r->counter_map.maptype) { |
| 2673 | case COUNTER_MAP_NOMAP: |
| 2674 | counters_nomap(newcounters, r->index); |
| 2675 | break; |
| Harald Welte | 1cef74d | 2001-01-05 15:22:59 +0000 | [diff] [blame] | 2676 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2677 | case COUNTER_MAP_NORMAL_MAP: |
| 2678 | counters_normal_map(newcounters, repl, |
| 2679 | r->index, |
| 2680 | r->counter_map.mappos); |
| 2681 | break; |
| Harald Welte | 1cef74d | 2001-01-05 15:22:59 +0000 | [diff] [blame] | 2682 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2683 | case COUNTER_MAP_ZEROED: |
| 2684 | counters_map_zeroed(newcounters, repl, |
| 2685 | r->index, |
| 2686 | r->counter_map.mappos, |
| 2687 | &r->entry->counters); |
| 2688 | break; |
| 2689 | |
| 2690 | case COUNTER_MAP_SET: |
| 2691 | counters_map_set(newcounters, r->index, |
| 2692 | &r->entry->counters); |
| 2693 | break; |
| 2694 | } |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2695 | } |
| 2696 | } |
| Rusty Russell | 62527ce | 2000-09-04 09:45:54 +0000 | [diff] [blame] | 2697 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2698 | #ifdef IPTC_DEBUG2 |
| 2699 | { |
| 2700 | int fd = open("/tmp/libiptc-so_set_add_counters.blob", |
| 2701 | O_CREAT|O_WRONLY); |
| 2702 | if (fd >= 0) { |
| 2703 | write(fd, newcounters, counterlen); |
| 2704 | close(fd); |
| 2705 | } |
| 2706 | } |
| 2707 | #endif |
| 2708 | |
| Harald Welte | d6ba6f5 | 2005-11-12 10:39:40 +0000 | [diff] [blame] | 2709 | ret = setsockopt(sockfd, TC_IPPROTO, SO_SET_ADD_COUNTERS, |
| 2710 | newcounters, counterlen); |
| Patrick McHardy | e0865ad | 2006-04-22 02:08:56 +0000 | [diff] [blame] | 2711 | if (ret < 0) |
| Harald Welte | d6ba6f5 | 2005-11-12 10:39:40 +0000 | [diff] [blame] | 2712 | goto out_free_newcounters; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2713 | |
| 2714 | free(repl->counters); |
| 2715 | free(repl); |
| 2716 | free(newcounters); |
| 2717 | |
| Harald Welte | d6ba6f5 | 2005-11-12 10:39:40 +0000 | [diff] [blame] | 2718 | finished: |
| Martin Josefsson | 841e4ae | 2003-05-02 15:30:11 +0000 | [diff] [blame] | 2719 | TC_FREE(handle); |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2720 | return 1; |
| Harald Welte | d6ba6f5 | 2005-11-12 10:39:40 +0000 | [diff] [blame] | 2721 | |
| 2722 | out_free_newcounters: |
| 2723 | free(newcounters); |
| 2724 | out_free_repl_counters: |
| 2725 | free(repl->counters); |
| 2726 | out_free_repl: |
| 2727 | free(repl); |
| 2728 | out_zero: |
| 2729 | return 0; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2730 | } |
| 2731 | |
| 2732 | /* Get raw socket. */ |
| 2733 | int |
| Patrick McHardy | 0b63936 | 2007-09-08 16:00:01 +0000 | [diff] [blame] | 2734 | TC_GET_RAW_SOCKET(void) |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2735 | { |
| 2736 | return sockfd; |
| 2737 | } |
| 2738 | |
| 2739 | /* Translates errno numbers into more human-readable form than strerror. */ |
| 2740 | const char * |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 2741 | TC_STRERROR(int err) |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2742 | { |
| 2743 | unsigned int i; |
| 2744 | struct table_struct { |
| 2745 | void *fn; |
| 2746 | int err; |
| 2747 | const char *message; |
| 2748 | } table [] = |
| Harald Welte | 4ccfa63 | 2001-07-30 15:12:43 +0000 | [diff] [blame] | 2749 | { { TC_INIT, EPERM, "Permission denied (you must be root)" }, |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 2750 | { TC_INIT, EINVAL, "Module is wrong version" }, |
| Harald Welte | 4ccfa63 | 2001-07-30 15:12:43 +0000 | [diff] [blame] | 2751 | { TC_INIT, ENOENT, |
| 2752 | "Table does not exist (do you need to insmod?)" }, |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 2753 | { TC_DELETE_CHAIN, ENOTEMPTY, "Chain is not empty" }, |
| 2754 | { TC_DELETE_CHAIN, EINVAL, "Can't delete built-in chain" }, |
| 2755 | { TC_DELETE_CHAIN, EMLINK, |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2756 | "Can't delete chain with references left" }, |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 2757 | { TC_CREATE_CHAIN, EEXIST, "Chain already exists" }, |
| 2758 | { TC_INSERT_ENTRY, E2BIG, "Index of insertion too big" }, |
| 2759 | { TC_REPLACE_ENTRY, E2BIG, "Index of replacement too big" }, |
| 2760 | { TC_DELETE_NUM_ENTRY, E2BIG, "Index of deletion too big" }, |
| Harald Welte | 1cef74d | 2001-01-05 15:22:59 +0000 | [diff] [blame] | 2761 | { TC_READ_COUNTER, E2BIG, "Index of counter too big" }, |
| 2762 | { TC_ZERO_COUNTER, E2BIG, "Index of counter too big" }, |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 2763 | { TC_INSERT_ENTRY, ELOOP, "Loop found in table" }, |
| 2764 | { TC_INSERT_ENTRY, EINVAL, "Target problem" }, |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2765 | /* EINVAL for CHECK probably means bad interface. */ |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 2766 | { TC_CHECK_PACKET, EINVAL, |
| Marc Boucher | c826499 | 2000-04-22 22:34:44 +0000 | [diff] [blame] | 2767 | "Bad arguments (does that interface exist?)" }, |
| Harald Welte | 4ccfa63 | 2001-07-30 15:12:43 +0000 | [diff] [blame] | 2768 | { TC_CHECK_PACKET, ENOSYS, |
| 2769 | "Checking will most likely never get implemented" }, |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2770 | /* ENOENT for DELETE probably means no matching rule */ |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 2771 | { TC_DELETE_ENTRY, ENOENT, |
| Marc Boucher | c826499 | 2000-04-22 22:34:44 +0000 | [diff] [blame] | 2772 | "Bad rule (does a matching rule exist in that chain?)" }, |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 2773 | { TC_SET_POLICY, ENOENT, |
| Marc Boucher | c826499 | 2000-04-22 22:34:44 +0000 | [diff] [blame] | 2774 | "Bad built-in chain name" }, |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 2775 | { TC_SET_POLICY, EINVAL, |
| Marc Boucher | c826499 | 2000-04-22 22:34:44 +0000 | [diff] [blame] | 2776 | "Bad policy name" }, |
| Harald Welte | 4ccfa63 | 2001-07-30 15:12:43 +0000 | [diff] [blame] | 2777 | |
| 2778 | { NULL, 0, "Incompatible with this kernel" }, |
| 2779 | { NULL, ENOPROTOOPT, "iptables who? (do you need to insmod?)" }, |
| 2780 | { NULL, ENOSYS, "Will be implemented real soon. I promise ;)" }, |
| 2781 | { NULL, ENOMEM, "Memory allocation problem" }, |
| 2782 | { NULL, ENOENT, "No chain/target/match by that name" }, |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2783 | }; |
| 2784 | |
| 2785 | for (i = 0; i < sizeof(table)/sizeof(struct table_struct); i++) { |
| 2786 | if ((!table[i].fn || table[i].fn == iptc_fn) |
| 2787 | && table[i].err == err) |
| 2788 | return table[i].message; |
| 2789 | } |
| 2790 | |
| 2791 | return strerror(err); |
| 2792 | } |