| 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 |
| Jan Engelhardt | fd18731 | 2008-11-10 16:59:27 +0100 | [diff] [blame^] | 188 | set_changed(struct xtc_handle *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 |
| Jan Engelhardt | fd18731 | 2008-11-10 16:59:27 +0100 | [diff] [blame^] | 194 | static void do_check(struct xtc_handle *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 * |
| Jan Engelhardt | fd18731 | 2008-11-10 16:59:27 +0100 | [diff] [blame^] | 231 | iptcb_get_entry(struct xtc_handle *h, unsigned int offset) |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 232 | { |
| 233 | return (STRUCT_ENTRY *)((char *)h->entries->entrytable + offset); |
| 234 | } |
| 235 | |
| 236 | static unsigned int |
| Jan Engelhardt | fd18731 | 2008-11-10 16:59:27 +0100 | [diff] [blame^] | 237 | iptcb_entry2index(struct xtc_handle *const 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 * |
| Jan Engelhardt | fd18731 | 2008-11-10 16:59:27 +0100 | [diff] [blame^] | 251 | iptcb_offset2entry(struct xtc_handle *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 |
| Jan Engelhardt | fd18731 | 2008-11-10 16:59:27 +0100 | [diff] [blame^] | 258 | iptcb_entry2offset(struct xtc_handle *const 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 |
| Jan Engelhardt | fd18731 | 2008-11-10 16:59:27 +0100 | [diff] [blame^] | 264 | iptcb_offset2index(struct xtc_handle *const 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 |
| Jan Engelhardt | fd18731 | 2008-11-10 16:59:27 +0100 | [diff] [blame^] | 271 | iptcb_ent_is_hook_entry(STRUCT_ENTRY *e, struct xtc_handle *h) |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 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, |
| Jan Engelhardt | fd18731 | 2008-11-10 16:59:27 +0100 | [diff] [blame^] | 332 | unsigned int *idx, struct xtc_handle *handle, |
| Jesper Dangaard Brouer | 4bae3f1 | 2008-07-03 20:31:42 +0200 | [diff] [blame] | 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, |
| Jan Engelhardt | fd18731 | 2008-11-10 16:59:27 +0100 | [diff] [blame^] | 435 | struct xtc_handle *handle) |
| Jesper Dangaard Brouer | 4bae3f1 | 2008-07-03 20:31:42 +0200 | [diff] [blame] | 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, |
| Jan Engelhardt | fd18731 | 2008-11-10 16:59:27 +0100 | [diff] [blame^] | 444 | struct xtc_handle *handle) |
| Jesper Dangaard Brouer | 4bae3f1 | 2008-07-03 20:31:42 +0200 | [diff] [blame] | 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 * |
| Jan Engelhardt | fd18731 | 2008-11-10 16:59:27 +0100 | [diff] [blame^] | 464 | iptcc_linearly_search_chain_index(const char *name, struct xtc_handle *handle) |
| Jesper Dangaard Brouer | 01444da | 2008-01-15 17:18:15 +0000 | [diff] [blame] | 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 | |
| Jan Engelhardt | fd18731 | 2008-11-10 16:59:27 +0100 | [diff] [blame^] | 492 | static int iptcc_chain_index_alloc(struct xtc_handle *h) |
| Jesper Dangaard Brouer | 01444da | 2008-01-15 17:18:15 +0000 | [diff] [blame] | 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 | |
| Jan Engelhardt | fd18731 | 2008-11-10 16:59:27 +0100 | [diff] [blame^] | 517 | static void iptcc_chain_index_free(struct xtc_handle *h) |
| Jesper Dangaard Brouer | 01444da | 2008-01-15 17:18:15 +0000 | [diff] [blame] | 518 | { |
| 519 | h->chain_index_sz = 0; |
| 520 | free(h->chain_index); |
| 521 | } |
| 522 | |
| 523 | |
| 524 | #ifdef DEBUG |
| Jan Engelhardt | fd18731 | 2008-11-10 16:59:27 +0100 | [diff] [blame^] | 525 | static void iptcc_chain_index_dump(struct xtc_handle *h) |
| Jesper Dangaard Brouer | 01444da | 2008-01-15 17:18:15 +0000 | [diff] [blame] | 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 */ |
| Jan Engelhardt | fd18731 | 2008-11-10 16:59:27 +0100 | [diff] [blame^] | 540 | static int iptcc_chain_index_build(struct xtc_handle *h) |
| Jesper Dangaard Brouer | 01444da | 2008-01-15 17:18:15 +0000 | [diff] [blame] | 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 | |
| Jan Engelhardt | fd18731 | 2008-11-10 16:59:27 +0100 | [diff] [blame^] | 582 | static int iptcc_chain_index_rebuild(struct xtc_handle *h) |
| Jesper Dangaard Brouer | 01444da | 2008-01-15 17:18:15 +0000 | [diff] [blame] | 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 | */ |
| Jan Engelhardt | fd18731 | 2008-11-10 16:59:27 +0100 | [diff] [blame^] | 604 | static int iptcc_chain_index_delete_chain(struct chain_head *c, struct xtc_handle *h) |
| Jesper Dangaard Brouer | 01444da | 2008-01-15 17:18:15 +0000 | [diff] [blame] | 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 * |
| Jan Engelhardt | fd18731 | 2008-11-10 16:59:27 +0100 | [diff] [blame^] | 684 | iptcc_find_chain_by_offset(struct xtc_handle *handle, unsigned int offset) |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 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 * |
| Jan Engelhardt | fd18731 | 2008-11-10 16:59:27 +0100 | [diff] [blame^] | 716 | iptcc_find_label(const char *name, struct xtc_handle *handle) |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 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 */ |
| Jan Engelhardt | fd18731 | 2008-11-10 16:59:27 +0100 | [diff] [blame^] | 813 | static int __iptcc_p_del_policy(struct xtc_handle *h, unsigned int num) |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 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 */ |
| Jan Engelhardt | fd18731 | 2008-11-10 16:59:27 +0100 | [diff] [blame^] | 845 | static inline void iptc_insert_chain(struct xtc_handle *h, struct chain_head *c) |
| Harald Welte | ec30b6c | 2005-02-01 16:45:56 +0000 | [diff] [blame] | 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 */ |
| Jan Engelhardt | fd18731 | 2008-11-10 16:59:27 +0100 | [diff] [blame^] | 888 | static void __iptcc_p_add_chain(struct xtc_handle *h, struct chain_head *c, |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 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, |
| Jan Engelhardt | fd18731 | 2008-11-10 16:59:27 +0100 | [diff] [blame^] | 931 | struct xtc_handle *h, |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 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 */ |
| Jan Engelhardt | fd18731 | 2008-11-10 16:59:27 +0100 | [diff] [blame^] | 1041 | static int parse_table(struct xtc_handle *h) |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 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 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1079 | return 1; |
| 1080 | } |
| 1081 | |
| 1082 | |
| 1083 | /********************************************************************** |
| 1084 | * RULESET COMPILATION (cache -> blob) |
| 1085 | **********************************************************************/ |
| 1086 | |
| 1087 | /* Convenience structures */ |
| 1088 | struct iptcb_chain_start{ |
| 1089 | STRUCT_ENTRY e; |
| 1090 | struct ipt_error_target name; |
| 1091 | }; |
| 1092 | #define IPTCB_CHAIN_START_SIZE (sizeof(STRUCT_ENTRY) + \ |
| 1093 | ALIGN(sizeof(struct ipt_error_target))) |
| 1094 | |
| 1095 | struct iptcb_chain_foot { |
| 1096 | STRUCT_ENTRY e; |
| 1097 | STRUCT_STANDARD_TARGET target; |
| 1098 | }; |
| 1099 | #define IPTCB_CHAIN_FOOT_SIZE (sizeof(STRUCT_ENTRY) + \ |
| 1100 | ALIGN(sizeof(STRUCT_STANDARD_TARGET))) |
| 1101 | |
| 1102 | struct iptcb_chain_error { |
| 1103 | STRUCT_ENTRY entry; |
| 1104 | struct ipt_error_target target; |
| 1105 | }; |
| 1106 | #define IPTCB_CHAIN_ERROR_SIZE (sizeof(STRUCT_ENTRY) + \ |
| 1107 | ALIGN(sizeof(struct ipt_error_target))) |
| 1108 | |
| 1109 | |
| 1110 | |
| 1111 | /* compile rule from cache into blob */ |
| Jan Engelhardt | fd18731 | 2008-11-10 16:59:27 +0100 | [diff] [blame^] | 1112 | static inline int iptcc_compile_rule (struct xtc_handle *h, STRUCT_REPLACE *repl, struct rule_head *r) |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1113 | { |
| 1114 | /* handle jumps */ |
| 1115 | if (r->type == IPTCC_R_JUMP) { |
| 1116 | STRUCT_STANDARD_TARGET *t; |
| 1117 | t = (STRUCT_STANDARD_TARGET *)GET_TARGET(r->entry); |
| 1118 | /* memset for memcmp convenience on delete/replace */ |
| 1119 | memset(t->target.u.user.name, 0, FUNCTION_MAXNAMELEN); |
| 1120 | strcpy(t->target.u.user.name, STANDARD_TARGET); |
| 1121 | /* Jumps can only happen to builtin chains, so we |
| 1122 | * can safely assume that they always have a header */ |
| 1123 | t->verdict = r->jump->head_offset + IPTCB_CHAIN_START_SIZE; |
| 1124 | } else if (r->type == IPTCC_R_FALLTHROUGH) { |
| 1125 | STRUCT_STANDARD_TARGET *t; |
| 1126 | t = (STRUCT_STANDARD_TARGET *)GET_TARGET(r->entry); |
| 1127 | t->verdict = r->offset + r->size; |
| 1128 | } |
| 1129 | |
| 1130 | /* copy entry from cache to blob */ |
| 1131 | memcpy((char *)repl->entries+r->offset, r->entry, r->size); |
| 1132 | |
| 1133 | return 1; |
| 1134 | } |
| 1135 | |
| 1136 | /* compile chain from cache into blob */ |
| Jan Engelhardt | fd18731 | 2008-11-10 16:59:27 +0100 | [diff] [blame^] | 1137 | static int iptcc_compile_chain(struct xtc_handle *h, STRUCT_REPLACE *repl, struct chain_head *c) |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1138 | { |
| 1139 | int ret; |
| 1140 | struct rule_head *r; |
| 1141 | struct iptcb_chain_start *head; |
| 1142 | struct iptcb_chain_foot *foot; |
| 1143 | |
| 1144 | /* only user-defined chains have heaer */ |
| 1145 | if (!iptcc_is_builtin(c)) { |
| 1146 | /* put chain header in place */ |
| 1147 | head = (void *)repl->entries + c->head_offset; |
| 1148 | head->e.target_offset = sizeof(STRUCT_ENTRY); |
| 1149 | head->e.next_offset = IPTCB_CHAIN_START_SIZE; |
| 1150 | strcpy(head->name.t.u.user.name, ERROR_TARGET); |
| 1151 | head->name.t.u.target_size = |
| 1152 | ALIGN(sizeof(struct ipt_error_target)); |
| 1153 | strcpy(head->name.error, c->name); |
| 1154 | } else { |
| 1155 | repl->hook_entry[c->hooknum-1] = c->head_offset; |
| 1156 | repl->underflow[c->hooknum-1] = c->foot_offset; |
| 1157 | } |
| 1158 | |
| 1159 | /* iterate over rules */ |
| 1160 | list_for_each_entry(r, &c->rules, list) { |
| 1161 | ret = iptcc_compile_rule(h, repl, r); |
| 1162 | if (ret < 0) |
| 1163 | return ret; |
| 1164 | } |
| 1165 | |
| 1166 | /* put chain footer in place */ |
| 1167 | foot = (void *)repl->entries + c->foot_offset; |
| 1168 | foot->e.target_offset = sizeof(STRUCT_ENTRY); |
| 1169 | foot->e.next_offset = IPTCB_CHAIN_FOOT_SIZE; |
| 1170 | strcpy(foot->target.target.u.user.name, STANDARD_TARGET); |
| 1171 | foot->target.target.u.target_size = |
| 1172 | ALIGN(sizeof(STRUCT_STANDARD_TARGET)); |
| 1173 | /* builtin targets have verdict, others return */ |
| 1174 | if (iptcc_is_builtin(c)) |
| 1175 | foot->target.verdict = c->verdict; |
| 1176 | else |
| 1177 | foot->target.verdict = RETURN; |
| 1178 | /* set policy-counters */ |
| 1179 | memcpy(&foot->e.counters, &c->counters, sizeof(STRUCT_COUNTERS)); |
| 1180 | |
| 1181 | return 0; |
| 1182 | } |
| 1183 | |
| 1184 | /* calculate offset and number for every rule in the cache */ |
| Jan Engelhardt | fd18731 | 2008-11-10 16:59:27 +0100 | [diff] [blame^] | 1185 | static int iptcc_compile_chain_offsets(struct xtc_handle *h, struct chain_head *c, |
| Harald Welte | efa8fc2 | 2005-07-19 22:03:49 +0000 | [diff] [blame] | 1186 | unsigned int *offset, unsigned int *num) |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1187 | { |
| 1188 | struct rule_head *r; |
| 1189 | |
| 1190 | c->head_offset = *offset; |
| 1191 | DEBUGP("%s: chain_head %u, offset=%u\n", c->name, *num, *offset); |
| 1192 | |
| 1193 | if (!iptcc_is_builtin(c)) { |
| 1194 | /* Chain has header */ |
| 1195 | *offset += sizeof(STRUCT_ENTRY) |
| 1196 | + ALIGN(sizeof(struct ipt_error_target)); |
| 1197 | (*num)++; |
| 1198 | } |
| 1199 | |
| 1200 | list_for_each_entry(r, &c->rules, list) { |
| 1201 | DEBUGP("rule %u, offset=%u, index=%u\n", *num, *offset, *num); |
| 1202 | r->offset = *offset; |
| 1203 | r->index = *num; |
| 1204 | *offset += r->size; |
| 1205 | (*num)++; |
| 1206 | } |
| 1207 | |
| 1208 | DEBUGP("%s; chain_foot %u, offset=%u, index=%u\n", c->name, *num, |
| 1209 | *offset, *num); |
| 1210 | c->foot_offset = *offset; |
| 1211 | c->foot_index = *num; |
| 1212 | *offset += sizeof(STRUCT_ENTRY) |
| 1213 | + ALIGN(sizeof(STRUCT_STANDARD_TARGET)); |
| 1214 | (*num)++; |
| 1215 | |
| 1216 | return 1; |
| 1217 | } |
| 1218 | |
| 1219 | /* put the pieces back together again */ |
| Jan Engelhardt | fd18731 | 2008-11-10 16:59:27 +0100 | [diff] [blame^] | 1220 | static int iptcc_compile_table_prep(struct xtc_handle *h, unsigned int *size) |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1221 | { |
| 1222 | struct chain_head *c; |
| 1223 | unsigned int offset = 0, num = 0; |
| 1224 | int ret = 0; |
| 1225 | |
| 1226 | /* First pass: calculate offset for every rule */ |
| 1227 | list_for_each_entry(c, &h->chains, list) { |
| 1228 | ret = iptcc_compile_chain_offsets(h, c, &offset, &num); |
| 1229 | if (ret < 0) |
| 1230 | return ret; |
| 1231 | } |
| 1232 | |
| 1233 | /* Append one error rule at end of chain */ |
| 1234 | num++; |
| 1235 | offset += sizeof(STRUCT_ENTRY) |
| 1236 | + ALIGN(sizeof(struct ipt_error_target)); |
| 1237 | |
| 1238 | /* ruleset size is now in offset */ |
| 1239 | *size = offset; |
| 1240 | return num; |
| 1241 | } |
| 1242 | |
| Jan Engelhardt | fd18731 | 2008-11-10 16:59:27 +0100 | [diff] [blame^] | 1243 | static int iptcc_compile_table(struct xtc_handle *h, STRUCT_REPLACE *repl) |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1244 | { |
| 1245 | struct chain_head *c; |
| 1246 | struct iptcb_chain_error *error; |
| 1247 | |
| 1248 | /* Second pass: copy from cache to offsets, fill in jumps */ |
| 1249 | list_for_each_entry(c, &h->chains, list) { |
| 1250 | int ret = iptcc_compile_chain(h, repl, c); |
| 1251 | if (ret < 0) |
| 1252 | return ret; |
| 1253 | } |
| 1254 | |
| 1255 | /* Append error rule at end of chain */ |
| 1256 | error = (void *)repl->entries + repl->size - IPTCB_CHAIN_ERROR_SIZE; |
| 1257 | error->entry.target_offset = sizeof(STRUCT_ENTRY); |
| 1258 | error->entry.next_offset = IPTCB_CHAIN_ERROR_SIZE; |
| 1259 | error->target.t.u.user.target_size = |
| 1260 | ALIGN(sizeof(struct ipt_error_target)); |
| 1261 | strcpy((char *)&error->target.t.u.user.name, ERROR_TARGET); |
| 1262 | strcpy((char *)&error->target.error, "ERROR"); |
| 1263 | |
| 1264 | return 1; |
| 1265 | } |
| 1266 | |
| 1267 | /********************************************************************** |
| 1268 | * EXTERNAL API (operates on cache only) |
| 1269 | **********************************************************************/ |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1270 | |
| 1271 | /* Allocate handle of given size */ |
| Jan Engelhardt | fd18731 | 2008-11-10 16:59:27 +0100 | [diff] [blame^] | 1272 | static struct xtc_handle * |
| Harald Welte | 0113fe7 | 2004-01-06 19:04:02 +0000 | [diff] [blame] | 1273 | alloc_handle(const char *tablename, unsigned int size, unsigned int num_rules) |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1274 | { |
| 1275 | size_t len; |
| Jan Engelhardt | fd18731 | 2008-11-10 16:59:27 +0100 | [diff] [blame^] | 1276 | struct xtc_handle *h; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1277 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1278 | len = sizeof(STRUCT_TC_HANDLE) + size; |
| 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 | h = malloc(sizeof(STRUCT_TC_HANDLE)); |
| 1281 | if (!h) { |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1282 | errno = ENOMEM; |
| 1283 | return NULL; |
| 1284 | } |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1285 | memset(h, 0, sizeof(*h)); |
| 1286 | INIT_LIST_HEAD(&h->chains); |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1287 | strcpy(h->info.name, tablename); |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1288 | |
| Harald Welte | 0371c0c | 2004-09-19 21:00:12 +0000 | [diff] [blame] | 1289 | h->entries = malloc(sizeof(STRUCT_GET_ENTRIES) + size); |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1290 | if (!h->entries) |
| 1291 | goto out_free_handle; |
| 1292 | |
| 1293 | strcpy(h->entries->name, tablename); |
| Harald Welte | 0371c0c | 2004-09-19 21:00:12 +0000 | [diff] [blame] | 1294 | h->entries->size = size; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1295 | |
| 1296 | return h; |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1297 | |
| 1298 | out_free_handle: |
| 1299 | free(h); |
| 1300 | |
| 1301 | return NULL; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1302 | } |
| 1303 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1304 | |
| Jan Engelhardt | fd18731 | 2008-11-10 16:59:27 +0100 | [diff] [blame^] | 1305 | struct xtc_handle * |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1306 | TC_INIT(const char *tablename) |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1307 | { |
| Jan Engelhardt | fd18731 | 2008-11-10 16:59:27 +0100 | [diff] [blame^] | 1308 | struct xtc_handle *h; |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1309 | STRUCT_GETINFO info; |
| Harald Welte | efa8fc2 | 2005-07-19 22:03:49 +0000 | [diff] [blame] | 1310 | unsigned int tmp; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1311 | socklen_t s; |
| 1312 | |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1313 | iptc_fn = TC_INIT; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1314 | |
| Martin Josefsson | 841e4ae | 2003-05-02 15:30:11 +0000 | [diff] [blame] | 1315 | if (strlen(tablename) >= TABLE_MAXNAMELEN) { |
| 1316 | errno = EINVAL; |
| 1317 | return NULL; |
| 1318 | } |
| 1319 | |
| Derrik Pates | 664c0a3 | 2005-02-01 13:28:14 +0000 | [diff] [blame] | 1320 | if (sockfd_use == 0) { |
| 1321 | sockfd = socket(TC_AF, SOCK_RAW, IPPROTO_RAW); |
| 1322 | if (sockfd < 0) |
| 1323 | return NULL; |
| 1324 | } |
| 1325 | sockfd_use++; |
| Patrick McHardy | 2f93205 | 2008-04-02 14:01:53 +0200 | [diff] [blame] | 1326 | retry: |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1327 | s = sizeof(info); |
| Martin Josefsson | 841e4ae | 2003-05-02 15:30:11 +0000 | [diff] [blame] | 1328 | |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1329 | strcpy(info.name, tablename); |
| Derrik Pates | 664c0a3 | 2005-02-01 13:28:14 +0000 | [diff] [blame] | 1330 | if (getsockopt(sockfd, TC_IPPROTO, SO_GET_INFO, &info, &s) < 0) { |
| 1331 | if (--sockfd_use == 0) { |
| 1332 | close(sockfd); |
| 1333 | sockfd = -1; |
| 1334 | } |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1335 | return NULL; |
| Derrik Pates | 664c0a3 | 2005-02-01 13:28:14 +0000 | [diff] [blame] | 1336 | } |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1337 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1338 | DEBUGP("valid_hooks=0x%08x, num_entries=%u, size=%u\n", |
| 1339 | info.valid_hooks, info.num_entries, info.size); |
| 1340 | |
| Harald Welte | 0113fe7 | 2004-01-06 19:04:02 +0000 | [diff] [blame] | 1341 | if ((h = alloc_handle(info.name, info.size, info.num_entries)) |
| Martin Josefsson | 841e4ae | 2003-05-02 15:30:11 +0000 | [diff] [blame] | 1342 | == NULL) { |
| Derrik Pates | 664c0a3 | 2005-02-01 13:28:14 +0000 | [diff] [blame] | 1343 | if (--sockfd_use == 0) { |
| 1344 | close(sockfd); |
| 1345 | sockfd = -1; |
| 1346 | } |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1347 | return NULL; |
| Martin Josefsson | 841e4ae | 2003-05-02 15:30:11 +0000 | [diff] [blame] | 1348 | } |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1349 | |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1350 | /* Initialize current state */ |
| 1351 | h->info = info; |
| Harald Welte | 0113fe7 | 2004-01-06 19:04:02 +0000 | [diff] [blame] | 1352 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1353 | h->entries->size = h->info.size; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1354 | |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1355 | tmp = sizeof(STRUCT_GET_ENTRIES) + h->info.size; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1356 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1357 | if (getsockopt(sockfd, TC_IPPROTO, SO_GET_ENTRIES, h->entries, |
| 1358 | &tmp) < 0) |
| 1359 | goto error; |
| 1360 | |
| 1361 | #ifdef IPTC_DEBUG2 |
| 1362 | { |
| 1363 | int fd = open("/tmp/libiptc-so_get_entries.blob", |
| 1364 | O_CREAT|O_WRONLY); |
| 1365 | if (fd >= 0) { |
| 1366 | write(fd, h->entries, tmp); |
| 1367 | close(fd); |
| 1368 | } |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1369 | } |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1370 | #endif |
| 1371 | |
| 1372 | if (parse_table(h) < 0) |
| 1373 | goto error; |
| Rusty Russell | 7e53bf9 | 2000-03-20 07:03:28 +0000 | [diff] [blame] | 1374 | |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1375 | CHECK(h); |
| 1376 | return h; |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1377 | error: |
| 1378 | TC_FREE(&h); |
| Patrick McHardy | 2f93205 | 2008-04-02 14:01:53 +0200 | [diff] [blame] | 1379 | /* A different process changed the ruleset size, retry */ |
| 1380 | if (errno == EAGAIN) |
| 1381 | goto retry; |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1382 | return NULL; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1383 | } |
| 1384 | |
| Martin Josefsson | 841e4ae | 2003-05-02 15:30:11 +0000 | [diff] [blame] | 1385 | void |
| Jan Engelhardt | fd18731 | 2008-11-10 16:59:27 +0100 | [diff] [blame^] | 1386 | TC_FREE(struct xtc_handle **h) |
| Martin Josefsson | 841e4ae | 2003-05-02 15:30:11 +0000 | [diff] [blame] | 1387 | { |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1388 | struct chain_head *c, *tmp; |
| 1389 | |
| Derrik Pates | 664c0a3 | 2005-02-01 13:28:14 +0000 | [diff] [blame] | 1390 | iptc_fn = TC_FREE; |
| 1391 | if (--sockfd_use == 0) { |
| 1392 | close(sockfd); |
| 1393 | sockfd = -1; |
| 1394 | } |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1395 | |
| 1396 | list_for_each_entry_safe(c, tmp, &(*h)->chains, list) { |
| 1397 | struct rule_head *r, *rtmp; |
| 1398 | |
| 1399 | list_for_each_entry_safe(r, rtmp, &c->rules, list) { |
| 1400 | free(r); |
| 1401 | } |
| 1402 | |
| 1403 | free(c); |
| 1404 | } |
| 1405 | |
| Jesper Dangaard Brouer | 01444da | 2008-01-15 17:18:15 +0000 | [diff] [blame] | 1406 | iptcc_chain_index_free(*h); |
| 1407 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1408 | free((*h)->entries); |
| Martin Josefsson | 841e4ae | 2003-05-02 15:30:11 +0000 | [diff] [blame] | 1409 | free(*h); |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1410 | |
| Martin Josefsson | 841e4ae | 2003-05-02 15:30:11 +0000 | [diff] [blame] | 1411 | *h = NULL; |
| 1412 | } |
| 1413 | |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1414 | static inline int |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1415 | print_match(const STRUCT_ENTRY_MATCH *m) |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1416 | { |
| Rusty Russell | 228e98d | 2000-04-27 10:28:06 +0000 | [diff] [blame] | 1417 | printf("Match name: `%s'\n", m->u.user.name); |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1418 | return 0; |
| 1419 | } |
| 1420 | |
| Jan Engelhardt | fd18731 | 2008-11-10 16:59:27 +0100 | [diff] [blame^] | 1421 | static int dump_entry(STRUCT_ENTRY *e, struct xtc_handle *const handle); |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1422 | |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1423 | void |
| Jan Engelhardt | fd18731 | 2008-11-10 16:59:27 +0100 | [diff] [blame^] | 1424 | TC_DUMP_ENTRIES(struct xtc_handle *const handle) |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1425 | { |
| Derrik Pates | 664c0a3 | 2005-02-01 13:28:14 +0000 | [diff] [blame] | 1426 | iptc_fn = TC_DUMP_ENTRIES; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1427 | CHECK(handle); |
| Patrick McHardy | 97fb2f1 | 2007-09-08 16:52:25 +0000 | [diff] [blame] | 1428 | |
| Rusty Russell | e45c713 | 2004-12-16 13:21:44 +0000 | [diff] [blame] | 1429 | printf("libiptc v%s. %u bytes.\n", |
| Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame] | 1430 | XTABLES_VERSION, handle->entries->size); |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1431 | printf("Table `%s'\n", handle->info.name); |
| 1432 | 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] | 1433 | handle->info.hook_entry[HOOK_PRE_ROUTING], |
| 1434 | handle->info.hook_entry[HOOK_LOCAL_IN], |
| 1435 | handle->info.hook_entry[HOOK_FORWARD], |
| 1436 | handle->info.hook_entry[HOOK_LOCAL_OUT], |
| 1437 | handle->info.hook_entry[HOOK_POST_ROUTING]); |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1438 | 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] | 1439 | handle->info.underflow[HOOK_PRE_ROUTING], |
| 1440 | handle->info.underflow[HOOK_LOCAL_IN], |
| 1441 | handle->info.underflow[HOOK_FORWARD], |
| 1442 | handle->info.underflow[HOOK_LOCAL_OUT], |
| 1443 | handle->info.underflow[HOOK_POST_ROUTING]); |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1444 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1445 | ENTRY_ITERATE(handle->entries->entrytable, handle->entries->size, |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1446 | dump_entry, handle); |
| Harald Welte | 0113fe7 | 2004-01-06 19:04:02 +0000 | [diff] [blame] | 1447 | } |
| Rusty Russell | 30fd6e5 | 2000-04-23 09:16:06 +0000 | [diff] [blame] | 1448 | |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1449 | /* Does this chain exist? */ |
| Jan Engelhardt | fd18731 | 2008-11-10 16:59:27 +0100 | [diff] [blame^] | 1450 | int TC_IS_CHAIN(const char *chain, struct xtc_handle *const handle) |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1451 | { |
| Derrik Pates | 664c0a3 | 2005-02-01 13:28:14 +0000 | [diff] [blame] | 1452 | iptc_fn = TC_IS_CHAIN; |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1453 | return iptcc_find_label(chain, handle) != NULL; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1454 | } |
| 1455 | |
| Jan Engelhardt | fd18731 | 2008-11-10 16:59:27 +0100 | [diff] [blame^] | 1456 | static void iptcc_chain_iterator_advance(struct xtc_handle *handle) |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1457 | { |
| 1458 | struct chain_head *c = handle->chain_iterator_cur; |
| 1459 | |
| 1460 | if (c->list.next == &handle->chains) |
| 1461 | handle->chain_iterator_cur = NULL; |
| 1462 | else |
| 1463 | handle->chain_iterator_cur = |
| 1464 | list_entry(c->list.next, struct chain_head, list); |
| 1465 | } |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1466 | |
| Rusty Russell | 30fd6e5 | 2000-04-23 09:16:06 +0000 | [diff] [blame] | 1467 | /* Iterator functions to run through the chains. */ |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1468 | const char * |
| Jan Engelhardt | fd18731 | 2008-11-10 16:59:27 +0100 | [diff] [blame^] | 1469 | TC_FIRST_CHAIN(struct xtc_handle **handle) |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1470 | { |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1471 | struct chain_head *c = list_entry((*handle)->chains.next, |
| 1472 | struct chain_head, list); |
| 1473 | |
| 1474 | iptc_fn = TC_FIRST_CHAIN; |
| 1475 | |
| 1476 | |
| 1477 | if (list_empty(&(*handle)->chains)) { |
| 1478 | DEBUGP(": no chains\n"); |
| Harald Welte | 0113fe7 | 2004-01-06 19:04:02 +0000 | [diff] [blame] | 1479 | return NULL; |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1480 | } |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1481 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1482 | (*handle)->chain_iterator_cur = c; |
| 1483 | iptcc_chain_iterator_advance(*handle); |
| Harald Welte | 0113fe7 | 2004-01-06 19:04:02 +0000 | [diff] [blame] | 1484 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1485 | DEBUGP(": returning `%s'\n", c->name); |
| 1486 | return c->name; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1487 | } |
| 1488 | |
| Rusty Russell | 30fd6e5 | 2000-04-23 09:16:06 +0000 | [diff] [blame] | 1489 | /* Iterator functions to run through the chains. Returns NULL at end. */ |
| 1490 | const char * |
| Jan Engelhardt | fd18731 | 2008-11-10 16:59:27 +0100 | [diff] [blame^] | 1491 | TC_NEXT_CHAIN(struct xtc_handle **handle) |
| Rusty Russell | 30fd6e5 | 2000-04-23 09:16:06 +0000 | [diff] [blame] | 1492 | { |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1493 | struct chain_head *c = (*handle)->chain_iterator_cur; |
| 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 | iptc_fn = TC_NEXT_CHAIN; |
| 1496 | |
| 1497 | if (!c) { |
| 1498 | DEBUGP(": no more chains\n"); |
| Rusty Russell | 30fd6e5 | 2000-04-23 09:16:06 +0000 | [diff] [blame] | 1499 | return NULL; |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1500 | } |
| Rusty Russell | 30fd6e5 | 2000-04-23 09:16:06 +0000 | [diff] [blame] | 1501 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1502 | iptcc_chain_iterator_advance(*handle); |
| 1503 | |
| 1504 | DEBUGP(": returning `%s'\n", c->name); |
| 1505 | return c->name; |
| Rusty Russell | 30fd6e5 | 2000-04-23 09:16:06 +0000 | [diff] [blame] | 1506 | } |
| 1507 | |
| 1508 | /* Get first rule in the given chain: NULL for empty chain. */ |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1509 | const STRUCT_ENTRY * |
| Jan Engelhardt | fd18731 | 2008-11-10 16:59:27 +0100 | [diff] [blame^] | 1510 | TC_FIRST_RULE(const char *chain, struct xtc_handle **handle) |
| Rusty Russell | 30fd6e5 | 2000-04-23 09:16:06 +0000 | [diff] [blame] | 1511 | { |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1512 | struct chain_head *c; |
| 1513 | struct rule_head *r; |
| Rusty Russell | 30fd6e5 | 2000-04-23 09:16:06 +0000 | [diff] [blame] | 1514 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1515 | iptc_fn = TC_FIRST_RULE; |
| 1516 | |
| 1517 | DEBUGP("first rule(%s): ", chain); |
| 1518 | |
| 1519 | c = iptcc_find_label(chain, *handle); |
| Rusty Russell | 30fd6e5 | 2000-04-23 09:16:06 +0000 | [diff] [blame] | 1520 | if (!c) { |
| 1521 | errno = ENOENT; |
| 1522 | return NULL; |
| 1523 | } |
| 1524 | |
| 1525 | /* Empty chain: single return/policy rule */ |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1526 | if (list_empty(&c->rules)) { |
| 1527 | DEBUGP_C("no rules, returning NULL\n"); |
| Rusty Russell | 30fd6e5 | 2000-04-23 09:16:06 +0000 | [diff] [blame] | 1528 | return NULL; |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1529 | } |
| Rusty Russell | 30fd6e5 | 2000-04-23 09:16:06 +0000 | [diff] [blame] | 1530 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1531 | r = list_entry(c->rules.next, struct rule_head, list); |
| 1532 | (*handle)->rule_iterator_cur = r; |
| 1533 | DEBUGP_C("%p\n", r); |
| 1534 | |
| 1535 | return r->entry; |
| Rusty Russell | 30fd6e5 | 2000-04-23 09:16:06 +0000 | [diff] [blame] | 1536 | } |
| 1537 | |
| 1538 | /* Returns NULL when rules run out. */ |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1539 | const STRUCT_ENTRY * |
| Jan Engelhardt | fd18731 | 2008-11-10 16:59:27 +0100 | [diff] [blame^] | 1540 | TC_NEXT_RULE(const STRUCT_ENTRY *prev, struct xtc_handle **handle) |
| Rusty Russell | 30fd6e5 | 2000-04-23 09:16:06 +0000 | [diff] [blame] | 1541 | { |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1542 | struct rule_head *r; |
| Rusty Russell | 30fd6e5 | 2000-04-23 09:16:06 +0000 | [diff] [blame] | 1543 | |
| Derrik Pates | 664c0a3 | 2005-02-01 13:28:14 +0000 | [diff] [blame] | 1544 | iptc_fn = TC_NEXT_RULE; |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1545 | DEBUGP("rule_iterator_cur=%p...", (*handle)->rule_iterator_cur); |
| 1546 | |
| 1547 | if (!(*handle)->rule_iterator_cur) { |
| 1548 | DEBUGP_C("returning NULL\n"); |
| 1549 | return NULL; |
| 1550 | } |
| 1551 | |
| 1552 | r = list_entry((*handle)->rule_iterator_cur->list.next, |
| 1553 | struct rule_head, list); |
| 1554 | |
| 1555 | iptc_fn = TC_NEXT_RULE; |
| 1556 | |
| 1557 | DEBUGP_C("next=%p, head=%p...", &r->list, |
| 1558 | &(*handle)->rule_iterator_cur->chain->rules); |
| 1559 | |
| 1560 | if (&r->list == &(*handle)->rule_iterator_cur->chain->rules) { |
| 1561 | (*handle)->rule_iterator_cur = NULL; |
| 1562 | DEBUGP_C("finished, returning NULL\n"); |
| 1563 | return NULL; |
| 1564 | } |
| 1565 | |
| 1566 | (*handle)->rule_iterator_cur = r; |
| 1567 | |
| 1568 | /* NOTE: prev is without any influence ! */ |
| 1569 | DEBUGP_C("returning rule %p\n", r); |
| 1570 | return r->entry; |
| Rusty Russell | 30fd6e5 | 2000-04-23 09:16:06 +0000 | [diff] [blame] | 1571 | } |
| 1572 | |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1573 | /* How many rules in this chain? */ |
| Jan Engelhardt | 33690a1 | 2008-02-11 00:54:00 +0100 | [diff] [blame] | 1574 | static unsigned int |
| Jan Engelhardt | fd18731 | 2008-11-10 16:59:27 +0100 | [diff] [blame^] | 1575 | TC_NUM_RULES(const char *chain, struct xtc_handle **handle) |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1576 | { |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1577 | struct chain_head *c; |
| 1578 | iptc_fn = TC_NUM_RULES; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1579 | CHECK(*handle); |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1580 | |
| 1581 | c = iptcc_find_label(chain, *handle); |
| 1582 | if (!c) { |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1583 | errno = ENOENT; |
| 1584 | return (unsigned int)-1; |
| 1585 | } |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1586 | |
| 1587 | return c->num_rules; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1588 | } |
| 1589 | |
| Jan Engelhardt | 33690a1 | 2008-02-11 00:54:00 +0100 | [diff] [blame] | 1590 | static const STRUCT_ENTRY * |
| Jan Engelhardt | fd18731 | 2008-11-10 16:59:27 +0100 | [diff] [blame^] | 1591 | TC_GET_RULE(const char *chain, unsigned int n, struct xtc_handle **handle) |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1592 | { |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1593 | struct chain_head *c; |
| 1594 | struct rule_head *r; |
| 1595 | |
| 1596 | iptc_fn = TC_GET_RULE; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1597 | |
| 1598 | CHECK(*handle); |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1599 | |
| 1600 | c = iptcc_find_label(chain, *handle); |
| 1601 | if (!c) { |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1602 | errno = ENOENT; |
| 1603 | return NULL; |
| 1604 | } |
| 1605 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1606 | r = iptcc_get_rule_num(c, n); |
| 1607 | if (!r) |
| 1608 | return NULL; |
| 1609 | return r->entry; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1610 | } |
| 1611 | |
| 1612 | /* Returns a pointer to the target name of this position. */ |
| Jan Engelhardt | 33690a1 | 2008-02-11 00:54:00 +0100 | [diff] [blame] | 1613 | static const char *standard_target_map(int verdict) |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1614 | { |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1615 | switch (verdict) { |
| 1616 | case RETURN: |
| 1617 | return LABEL_RETURN; |
| 1618 | break; |
| 1619 | case -NF_ACCEPT-1: |
| 1620 | return LABEL_ACCEPT; |
| 1621 | break; |
| 1622 | case -NF_DROP-1: |
| 1623 | return LABEL_DROP; |
| 1624 | break; |
| 1625 | case -NF_QUEUE-1: |
| 1626 | return LABEL_QUEUE; |
| 1627 | break; |
| 1628 | default: |
| 1629 | fprintf(stderr, "ERROR: %d not a valid target)\n", |
| 1630 | verdict); |
| 1631 | abort(); |
| 1632 | break; |
| 1633 | } |
| 1634 | /* not reached */ |
| 1635 | return NULL; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1636 | } |
| 1637 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1638 | /* Returns a pointer to the target name of this position. */ |
| 1639 | const char *TC_GET_TARGET(const STRUCT_ENTRY *ce, |
| Jan Engelhardt | fd18731 | 2008-11-10 16:59:27 +0100 | [diff] [blame^] | 1640 | struct xtc_handle **handle) |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1641 | { |
| 1642 | STRUCT_ENTRY *e = (STRUCT_ENTRY *)ce; |
| Rusty Russell | e45c713 | 2004-12-16 13:21:44 +0000 | [diff] [blame] | 1643 | struct rule_head *r = container_of(e, struct rule_head, entry[0]); |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1644 | |
| 1645 | iptc_fn = TC_GET_TARGET; |
| 1646 | |
| 1647 | switch(r->type) { |
| 1648 | int spos; |
| 1649 | case IPTCC_R_FALLTHROUGH: |
| 1650 | return ""; |
| 1651 | break; |
| 1652 | case IPTCC_R_JUMP: |
| 1653 | DEBUGP("r=%p, jump=%p, name=`%s'\n", r, r->jump, r->jump->name); |
| 1654 | return r->jump->name; |
| 1655 | break; |
| 1656 | case IPTCC_R_STANDARD: |
| 1657 | spos = *(int *)GET_TARGET(e)->data; |
| 1658 | DEBUGP("r=%p, spos=%d'\n", r, spos); |
| 1659 | return standard_target_map(spos); |
| 1660 | break; |
| 1661 | case IPTCC_R_MODULE: |
| 1662 | return GET_TARGET(e)->u.user.name; |
| 1663 | break; |
| 1664 | } |
| 1665 | return NULL; |
| 1666 | } |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1667 | /* Is this a built-in chain? Actually returns hook + 1. */ |
| 1668 | int |
| Jan Engelhardt | fd18731 | 2008-11-10 16:59:27 +0100 | [diff] [blame^] | 1669 | TC_BUILTIN(const char *chain, struct xtc_handle *const handle) |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1670 | { |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1671 | struct chain_head *c; |
| 1672 | |
| 1673 | iptc_fn = TC_BUILTIN; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1674 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1675 | c = iptcc_find_label(chain, handle); |
| 1676 | if (!c) { |
| 1677 | errno = ENOENT; |
| Martin Josefsson | b0f3d2d | 2004-09-23 18:23:20 +0000 | [diff] [blame] | 1678 | return 0; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1679 | } |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1680 | |
| 1681 | return iptcc_is_builtin(c); |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1682 | } |
| 1683 | |
| 1684 | /* Get the policy of a given built-in chain */ |
| 1685 | const char * |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1686 | TC_GET_POLICY(const char *chain, |
| 1687 | STRUCT_COUNTERS *counters, |
| Jan Engelhardt | fd18731 | 2008-11-10 16:59:27 +0100 | [diff] [blame^] | 1688 | struct xtc_handle **handle) |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1689 | { |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1690 | struct chain_head *c; |
| 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 | iptc_fn = TC_GET_POLICY; |
| 1693 | |
| 1694 | DEBUGP("called for chain %s\n", chain); |
| 1695 | |
| 1696 | c = iptcc_find_label(chain, *handle); |
| 1697 | if (!c) { |
| 1698 | errno = ENOENT; |
| 1699 | return NULL; |
| 1700 | } |
| 1701 | |
| 1702 | if (!iptcc_is_builtin(c)) |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1703 | return NULL; |
| 1704 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1705 | *counters = c->counters; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1706 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1707 | return standard_target_map(c->verdict); |
| Harald Welte | 0113fe7 | 2004-01-06 19:04:02 +0000 | [diff] [blame] | 1708 | } |
| 1709 | |
| 1710 | static int |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1711 | iptcc_standard_map(struct rule_head *r, int verdict) |
| Harald Welte | 0113fe7 | 2004-01-06 19:04:02 +0000 | [diff] [blame] | 1712 | { |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1713 | STRUCT_ENTRY *e = r->entry; |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1714 | STRUCT_STANDARD_TARGET *t; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1715 | |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1716 | t = (STRUCT_STANDARD_TARGET *)GET_TARGET(e); |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1717 | |
| Rusty Russell | 67088e7 | 2000-05-10 01:18:57 +0000 | [diff] [blame] | 1718 | if (t->target.u.target_size |
| Philip Blundell | 8c70090 | 2000-05-15 02:17:52 +0000 | [diff] [blame] | 1719 | != ALIGN(sizeof(STRUCT_STANDARD_TARGET))) { |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1720 | errno = EINVAL; |
| 1721 | return 0; |
| 1722 | } |
| 1723 | /* memset for memcmp convenience on delete/replace */ |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1724 | memset(t->target.u.user.name, 0, FUNCTION_MAXNAMELEN); |
| 1725 | strcpy(t->target.u.user.name, STANDARD_TARGET); |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1726 | t->verdict = verdict; |
| 1727 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1728 | r->type = IPTCC_R_STANDARD; |
| 1729 | |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1730 | return 1; |
| 1731 | } |
| Rusty Russell | 7e53bf9 | 2000-03-20 07:03:28 +0000 | [diff] [blame] | 1732 | |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1733 | static int |
| Jan Engelhardt | fd18731 | 2008-11-10 16:59:27 +0100 | [diff] [blame^] | 1734 | iptcc_map_target(struct xtc_handle *const handle, |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1735 | struct rule_head *r) |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1736 | { |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1737 | STRUCT_ENTRY *e = r->entry; |
| Harald Welte | 0113fe7 | 2004-01-06 19:04:02 +0000 | [diff] [blame] | 1738 | STRUCT_ENTRY_TARGET *t = GET_TARGET(e); |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1739 | |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1740 | /* Maybe it's empty (=> fall through) */ |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1741 | if (strcmp(t->u.user.name, "") == 0) { |
| 1742 | r->type = IPTCC_R_FALLTHROUGH; |
| 1743 | return 1; |
| 1744 | } |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1745 | /* Maybe it's a standard target name... */ |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1746 | else if (strcmp(t->u.user.name, LABEL_ACCEPT) == 0) |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1747 | return iptcc_standard_map(r, -NF_ACCEPT - 1); |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1748 | else if (strcmp(t->u.user.name, LABEL_DROP) == 0) |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1749 | return iptcc_standard_map(r, -NF_DROP - 1); |
| Rusty Russell | 67088e7 | 2000-05-10 01:18:57 +0000 | [diff] [blame] | 1750 | else if (strcmp(t->u.user.name, LABEL_QUEUE) == 0) |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1751 | return iptcc_standard_map(r, -NF_QUEUE - 1); |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1752 | else if (strcmp(t->u.user.name, LABEL_RETURN) == 0) |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1753 | return iptcc_standard_map(r, RETURN); |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1754 | else if (TC_BUILTIN(t->u.user.name, handle)) { |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1755 | /* Can't jump to builtins. */ |
| 1756 | errno = EINVAL; |
| 1757 | return 0; |
| 1758 | } else { |
| 1759 | /* Maybe it's an existing chain name. */ |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1760 | struct chain_head *c; |
| 1761 | DEBUGP("trying to find chain `%s': ", t->u.user.name); |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1762 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1763 | c = iptcc_find_label(t->u.user.name, handle); |
| 1764 | if (c) { |
| 1765 | DEBUGP_C("found!\n"); |
| 1766 | r->type = IPTCC_R_JUMP; |
| 1767 | r->jump = c; |
| 1768 | c->references++; |
| 1769 | return 1; |
| 1770 | } |
| 1771 | DEBUGP_C("not found :(\n"); |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1772 | } |
| 1773 | |
| 1774 | /* Must be a module? If not, kernel will reject... */ |
| Rusty Russell | 3aef54d | 2005-01-03 03:48:40 +0000 | [diff] [blame] | 1775 | /* memset to all 0 for your memcmp convenience: don't clear version */ |
| Rusty Russell | 228e98d | 2000-04-27 10:28:06 +0000 | [diff] [blame] | 1776 | memset(t->u.user.name + strlen(t->u.user.name), |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1777 | 0, |
| Rusty Russell | 3aef54d | 2005-01-03 03:48:40 +0000 | [diff] [blame] | 1778 | FUNCTION_MAXNAMELEN - 1 - strlen(t->u.user.name)); |
| Rusty Russell | 733e54b | 2004-12-16 14:22:23 +0000 | [diff] [blame] | 1779 | r->type = IPTCC_R_MODULE; |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1780 | set_changed(handle); |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1781 | return 1; |
| 1782 | } |
| 1783 | |
| Harald Welte | 0113fe7 | 2004-01-06 19:04:02 +0000 | [diff] [blame] | 1784 | /* Insert the entry `fw' in chain `chain' into position `rulenum'. */ |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1785 | int |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1786 | TC_INSERT_ENTRY(const IPT_CHAINLABEL chain, |
| 1787 | const STRUCT_ENTRY *e, |
| 1788 | unsigned int rulenum, |
| Jan Engelhardt | fd18731 | 2008-11-10 16:59:27 +0100 | [diff] [blame^] | 1789 | struct xtc_handle **handle) |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1790 | { |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1791 | struct chain_head *c; |
| Martin Josefsson | eb066cc | 2004-09-22 21:04:07 +0000 | [diff] [blame] | 1792 | struct rule_head *r; |
| 1793 | struct list_head *prev; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1794 | |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1795 | iptc_fn = TC_INSERT_ENTRY; |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1796 | |
| 1797 | if (!(c = iptcc_find_label(chain, *handle))) { |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1798 | errno = ENOENT; |
| 1799 | return 0; |
| 1800 | } |
| 1801 | |
| Martin Josefsson | eb066cc | 2004-09-22 21:04:07 +0000 | [diff] [blame] | 1802 | /* first rulenum index = 0 |
| 1803 | first c->num_rules index = 1 */ |
| 1804 | if (rulenum > c->num_rules) { |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1805 | errno = E2BIG; |
| 1806 | return 0; |
| 1807 | } |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1808 | |
| Martin Josefsson | 631f361 | 2004-09-23 19:25:06 +0000 | [diff] [blame] | 1809 | /* If we are inserting at the end just take advantage of the |
| 1810 | double linked list, insert will happen before the entry |
| 1811 | prev points to. */ |
| 1812 | if (rulenum == c->num_rules) { |
| Martin Josefsson | eb066cc | 2004-09-22 21:04:07 +0000 | [diff] [blame] | 1813 | prev = &c->rules; |
| Martin Josefsson | a5616dc | 2004-10-24 22:27:31 +0000 | [diff] [blame] | 1814 | } else if (rulenum + 1 <= c->num_rules/2) { |
| Martin Josefsson | 631f361 | 2004-09-23 19:25:06 +0000 | [diff] [blame] | 1815 | r = iptcc_get_rule_num(c, rulenum + 1); |
| Martin Josefsson | a5616dc | 2004-10-24 22:27:31 +0000 | [diff] [blame] | 1816 | prev = &r->list; |
| 1817 | } else { |
| 1818 | r = iptcc_get_rule_num_reverse(c, c->num_rules - rulenum); |
| Martin Josefsson | 631f361 | 2004-09-23 19:25:06 +0000 | [diff] [blame] | 1819 | prev = &r->list; |
| 1820 | } |
| Martin Josefsson | eb066cc | 2004-09-22 21:04:07 +0000 | [diff] [blame] | 1821 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1822 | if (!(r = iptcc_alloc_rule(c, e->next_offset))) { |
| 1823 | errno = ENOMEM; |
| Harald Welte | 0113fe7 | 2004-01-06 19:04:02 +0000 | [diff] [blame] | 1824 | return 0; |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1825 | } |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1826 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1827 | memcpy(r->entry, e, e->next_offset); |
| 1828 | r->counter_map.maptype = COUNTER_MAP_SET; |
| 1829 | |
| 1830 | if (!iptcc_map_target(*handle, r)) { |
| 1831 | free(r); |
| 1832 | return 0; |
| 1833 | } |
| 1834 | |
| Martin Josefsson | eb066cc | 2004-09-22 21:04:07 +0000 | [diff] [blame] | 1835 | list_add_tail(&r->list, prev); |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1836 | c->num_rules++; |
| 1837 | |
| 1838 | set_changed(*handle); |
| 1839 | |
| 1840 | return 1; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1841 | } |
| 1842 | |
| 1843 | /* Atomically replace rule `rulenum' in `chain' with `fw'. */ |
| 1844 | int |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1845 | TC_REPLACE_ENTRY(const IPT_CHAINLABEL chain, |
| 1846 | const STRUCT_ENTRY *e, |
| 1847 | unsigned int rulenum, |
| Jan Engelhardt | fd18731 | 2008-11-10 16:59:27 +0100 | [diff] [blame^] | 1848 | struct xtc_handle **handle) |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1849 | { |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1850 | struct chain_head *c; |
| 1851 | struct rule_head *r, *old; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1852 | |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1853 | iptc_fn = TC_REPLACE_ENTRY; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1854 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1855 | if (!(c = iptcc_find_label(chain, *handle))) { |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1856 | errno = ENOENT; |
| 1857 | return 0; |
| 1858 | } |
| 1859 | |
| Martin Josefsson | 0f9b8b1 | 2004-12-18 17:18:49 +0000 | [diff] [blame] | 1860 | if (rulenum >= c->num_rules) { |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1861 | errno = E2BIG; |
| 1862 | return 0; |
| 1863 | } |
| 1864 | |
| Martin Josefsson | 0f9b8b1 | 2004-12-18 17:18:49 +0000 | [diff] [blame] | 1865 | /* Take advantage of the double linked list if possible. */ |
| 1866 | if (rulenum + 1 <= c->num_rules/2) { |
| 1867 | old = iptcc_get_rule_num(c, rulenum + 1); |
| 1868 | } else { |
| 1869 | old = iptcc_get_rule_num_reverse(c, c->num_rules - rulenum); |
| 1870 | } |
| 1871 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1872 | if (!(r = iptcc_alloc_rule(c, e->next_offset))) { |
| 1873 | errno = ENOMEM; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1874 | return 0; |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1875 | } |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1876 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1877 | memcpy(r->entry, e, e->next_offset); |
| 1878 | r->counter_map.maptype = COUNTER_MAP_SET; |
| 1879 | |
| 1880 | if (!iptcc_map_target(*handle, r)) { |
| 1881 | free(r); |
| Harald Welte | 0113fe7 | 2004-01-06 19:04:02 +0000 | [diff] [blame] | 1882 | return 0; |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1883 | } |
| Harald Welte | 0113fe7 | 2004-01-06 19:04:02 +0000 | [diff] [blame] | 1884 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1885 | list_add(&r->list, &old->list); |
| 1886 | iptcc_delete_rule(old); |
| 1887 | |
| 1888 | set_changed(*handle); |
| 1889 | |
| 1890 | return 1; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1891 | } |
| 1892 | |
| Harald Welte | 0113fe7 | 2004-01-06 19:04:02 +0000 | [diff] [blame] | 1893 | /* Append entry `fw' to chain `chain'. Equivalent to insert with |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1894 | rulenum = length of chain. */ |
| 1895 | int |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1896 | TC_APPEND_ENTRY(const IPT_CHAINLABEL chain, |
| 1897 | const STRUCT_ENTRY *e, |
| Jan Engelhardt | fd18731 | 2008-11-10 16:59:27 +0100 | [diff] [blame^] | 1898 | struct xtc_handle **handle) |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1899 | { |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1900 | struct chain_head *c; |
| 1901 | struct rule_head *r; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1902 | |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1903 | iptc_fn = TC_APPEND_ENTRY; |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1904 | if (!(c = iptcc_find_label(chain, *handle))) { |
| 1905 | DEBUGP("unable to find chain `%s'\n", chain); |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1906 | errno = ENOENT; |
| 1907 | return 0; |
| 1908 | } |
| 1909 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1910 | if (!(r = iptcc_alloc_rule(c, e->next_offset))) { |
| 1911 | DEBUGP("unable to allocate rule for chain `%s'\n", chain); |
| 1912 | errno = ENOMEM; |
| Harald Welte | 0113fe7 | 2004-01-06 19:04:02 +0000 | [diff] [blame] | 1913 | return 0; |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1914 | } |
| Harald Welte | 0113fe7 | 2004-01-06 19:04:02 +0000 | [diff] [blame] | 1915 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1916 | memcpy(r->entry, e, e->next_offset); |
| 1917 | r->counter_map.maptype = COUNTER_MAP_SET; |
| 1918 | |
| 1919 | if (!iptcc_map_target(*handle, r)) { |
| Martin Josefsson | 1200953 | 2004-09-23 18:24:29 +0000 | [diff] [blame] | 1920 | DEBUGP("unable to map target of rule for chain `%s'\n", chain); |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 1921 | free(r); |
| 1922 | return 0; |
| 1923 | } |
| 1924 | |
| 1925 | list_add_tail(&r->list, &c->rules); |
| 1926 | c->num_rules++; |
| 1927 | |
| 1928 | set_changed(*handle); |
| 1929 | |
| 1930 | return 1; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1931 | } |
| 1932 | |
| 1933 | static inline int |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1934 | match_different(const STRUCT_ENTRY_MATCH *a, |
| Rusty Russell | edf14cf | 2000-04-19 11:26:44 +0000 | [diff] [blame] | 1935 | const unsigned char *a_elems, |
| 1936 | const unsigned char *b_elems, |
| 1937 | unsigned char **maskptr) |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1938 | { |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1939 | const STRUCT_ENTRY_MATCH *b; |
| Rusty Russell | edf14cf | 2000-04-19 11:26:44 +0000 | [diff] [blame] | 1940 | unsigned int i; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1941 | |
| 1942 | /* Offset of b is the same as a. */ |
| Rusty Russell | 30fd6e5 | 2000-04-23 09:16:06 +0000 | [diff] [blame] | 1943 | b = (void *)b_elems + ((unsigned char *)a - a_elems); |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1944 | |
| Rusty Russell | 228e98d | 2000-04-27 10:28:06 +0000 | [diff] [blame] | 1945 | if (a->u.match_size != b->u.match_size) |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1946 | return 1; |
| 1947 | |
| Rusty Russell | 228e98d | 2000-04-27 10:28:06 +0000 | [diff] [blame] | 1948 | if (strcmp(a->u.user.name, b->u.user.name) != 0) |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1949 | return 1; |
| 1950 | |
| Rusty Russell | 73ef09b | 2000-07-03 10:24:04 +0000 | [diff] [blame] | 1951 | *maskptr += ALIGN(sizeof(*a)); |
| Rusty Russell | edf14cf | 2000-04-19 11:26:44 +0000 | [diff] [blame] | 1952 | |
| Rusty Russell | 73ef09b | 2000-07-03 10:24:04 +0000 | [diff] [blame] | 1953 | for (i = 0; i < a->u.match_size - ALIGN(sizeof(*a)); i++) |
| Rusty Russell | edf14cf | 2000-04-19 11:26:44 +0000 | [diff] [blame] | 1954 | if (((a->data[i] ^ b->data[i]) & (*maskptr)[i]) != 0) |
| Rusty Russell | 90e712a | 2000-03-29 04:19:26 +0000 | [diff] [blame] | 1955 | return 1; |
| Rusty Russell | edf14cf | 2000-04-19 11:26:44 +0000 | [diff] [blame] | 1956 | *maskptr += i; |
| 1957 | return 0; |
| 1958 | } |
| 1959 | |
| 1960 | static inline int |
| Rusty Russell | 733e54b | 2004-12-16 14:22:23 +0000 | [diff] [blame] | 1961 | 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] | 1962 | { |
| 1963 | unsigned int i; |
| Rusty Russell | 733e54b | 2004-12-16 14:22:23 +0000 | [diff] [blame] | 1964 | STRUCT_ENTRY_TARGET *ta, *tb; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1965 | |
| Rusty Russell | 733e54b | 2004-12-16 14:22:23 +0000 | [diff] [blame] | 1966 | if (a->type != b->type) |
| 1967 | return 0; |
| 1968 | |
| 1969 | ta = GET_TARGET(a->entry); |
| 1970 | tb = GET_TARGET(b->entry); |
| 1971 | |
| 1972 | switch (a->type) { |
| 1973 | case IPTCC_R_FALLTHROUGH: |
| 1974 | return 1; |
| 1975 | case IPTCC_R_JUMP: |
| 1976 | return a->jump == b->jump; |
| 1977 | case IPTCC_R_STANDARD: |
| 1978 | return ((STRUCT_STANDARD_TARGET *)ta)->verdict |
| 1979 | == ((STRUCT_STANDARD_TARGET *)tb)->verdict; |
| 1980 | case IPTCC_R_MODULE: |
| 1981 | if (ta->u.target_size != tb->u.target_size) |
| 1982 | return 0; |
| 1983 | if (strcmp(ta->u.user.name, tb->u.user.name) != 0) |
| 1984 | return 0; |
| 1985 | |
| 1986 | for (i = 0; i < ta->u.target_size - sizeof(*ta); i++) |
| Rusty Russell | daade44 | 2004-12-29 11:14:52 +0000 | [diff] [blame] | 1987 | if (((ta->data[i] ^ tb->data[i]) & mask[i]) != 0) |
| Rusty Russell | 733e54b | 2004-12-16 14:22:23 +0000 | [diff] [blame] | 1988 | return 0; |
| 1989 | return 1; |
| 1990 | default: |
| 1991 | fprintf(stderr, "ERROR: bad type %i\n", a->type); |
| 1992 | abort(); |
| 1993 | } |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1994 | } |
| 1995 | |
| Rusty Russell | 733e54b | 2004-12-16 14:22:23 +0000 | [diff] [blame] | 1996 | static unsigned char * |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1997 | is_same(const STRUCT_ENTRY *a, |
| 1998 | const STRUCT_ENTRY *b, |
| 1999 | unsigned char *matchmask); |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2000 | |
| Harald Welte | 0113fe7 | 2004-01-06 19:04:02 +0000 | [diff] [blame] | 2001 | /* Delete the first rule in `chain' which matches `fw'. */ |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2002 | int |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 2003 | TC_DELETE_ENTRY(const IPT_CHAINLABEL chain, |
| 2004 | const STRUCT_ENTRY *origfw, |
| 2005 | unsigned char *matchmask, |
| Jan Engelhardt | fd18731 | 2008-11-10 16:59:27 +0100 | [diff] [blame^] | 2006 | struct xtc_handle **handle) |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2007 | { |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2008 | struct chain_head *c; |
| Rusty Russell | e45c713 | 2004-12-16 13:21:44 +0000 | [diff] [blame] | 2009 | struct rule_head *r, *i; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2010 | |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 2011 | iptc_fn = TC_DELETE_ENTRY; |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2012 | if (!(c = iptcc_find_label(chain, *handle))) { |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2013 | errno = ENOENT; |
| 2014 | return 0; |
| 2015 | } |
| 2016 | |
| Rusty Russell | e45c713 | 2004-12-16 13:21:44 +0000 | [diff] [blame] | 2017 | /* Create a rule_head from origfw. */ |
| 2018 | r = iptcc_alloc_rule(c, origfw->next_offset); |
| 2019 | if (!r) { |
| Harald Welte | 0113fe7 | 2004-01-06 19:04:02 +0000 | [diff] [blame] | 2020 | errno = ENOMEM; |
| 2021 | return 0; |
| 2022 | } |
| 2023 | |
| Rusty Russell | e45c713 | 2004-12-16 13:21:44 +0000 | [diff] [blame] | 2024 | memcpy(r->entry, origfw, origfw->next_offset); |
| 2025 | r->counter_map.maptype = COUNTER_MAP_NOMAP; |
| 2026 | if (!iptcc_map_target(*handle, r)) { |
| 2027 | DEBUGP("unable to map target of rule for chain `%s'\n", chain); |
| 2028 | free(r); |
| 2029 | return 0; |
| Patrick McHardyJesper Brouer | 04a1e4c | 2006-07-25 01:50:48 +0000 | [diff] [blame] | 2030 | } else { |
| 2031 | /* iptcc_map_target increment target chain references |
| 2032 | * since this is a fake rule only used for matching |
| 2033 | * the chain references count is decremented again. |
| 2034 | */ |
| 2035 | if (r->type == IPTCC_R_JUMP |
| 2036 | && r->jump) |
| 2037 | r->jump->references--; |
| Rusty Russell | e45c713 | 2004-12-16 13:21:44 +0000 | [diff] [blame] | 2038 | } |
| Harald Welte | 0113fe7 | 2004-01-06 19:04:02 +0000 | [diff] [blame] | 2039 | |
| Rusty Russell | e45c713 | 2004-12-16 13:21:44 +0000 | [diff] [blame] | 2040 | list_for_each_entry(i, &c->rules, list) { |
| Rusty Russell | 733e54b | 2004-12-16 14:22:23 +0000 | [diff] [blame] | 2041 | unsigned char *mask; |
| Harald Welte | fe53707 | 2004-08-30 20:28:53 +0000 | [diff] [blame] | 2042 | |
| Rusty Russell | 733e54b | 2004-12-16 14:22:23 +0000 | [diff] [blame] | 2043 | mask = is_same(r->entry, i->entry, matchmask); |
| 2044 | if (!mask) |
| 2045 | continue; |
| Martin Josefsson | 2a5dbbb | 2004-09-22 21:37:41 +0000 | [diff] [blame] | 2046 | |
| Rusty Russell | 733e54b | 2004-12-16 14:22:23 +0000 | [diff] [blame] | 2047 | if (!target_same(r, i, mask)) |
| 2048 | continue; |
| 2049 | |
| 2050 | /* If we are about to delete the rule that is the |
| 2051 | * current iterator, move rule iterator back. next |
| 2052 | * pointer will then point to real next node */ |
| 2053 | if (i == (*handle)->rule_iterator_cur) { |
| 2054 | (*handle)->rule_iterator_cur = |
| 2055 | list_entry((*handle)->rule_iterator_cur->list.prev, |
| 2056 | struct rule_head, list); |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2057 | } |
| Rusty Russell | 733e54b | 2004-12-16 14:22:23 +0000 | [diff] [blame] | 2058 | |
| 2059 | c->num_rules--; |
| 2060 | iptcc_delete_rule(i); |
| 2061 | |
| 2062 | set_changed(*handle); |
| 2063 | free(r); |
| 2064 | return 1; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2065 | } |
| 2066 | |
| Rusty Russell | e45c713 | 2004-12-16 13:21:44 +0000 | [diff] [blame] | 2067 | free(r); |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2068 | errno = ENOENT; |
| 2069 | return 0; |
| Rusty Russell | 7e53bf9 | 2000-03-20 07:03:28 +0000 | [diff] [blame] | 2070 | } |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2071 | |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2072 | |
| 2073 | /* Delete the rule in position `rulenum' in `chain'. */ |
| 2074 | int |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 2075 | TC_DELETE_NUM_ENTRY(const IPT_CHAINLABEL chain, |
| 2076 | unsigned int rulenum, |
| Jan Engelhardt | fd18731 | 2008-11-10 16:59:27 +0100 | [diff] [blame^] | 2077 | struct xtc_handle **handle) |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2078 | { |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2079 | struct chain_head *c; |
| 2080 | struct rule_head *r; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2081 | |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 2082 | iptc_fn = TC_DELETE_NUM_ENTRY; |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2083 | |
| 2084 | if (!(c = iptcc_find_label(chain, *handle))) { |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2085 | errno = ENOENT; |
| 2086 | return 0; |
| 2087 | } |
| 2088 | |
| Martin Josefsson | a5616dc | 2004-10-24 22:27:31 +0000 | [diff] [blame] | 2089 | if (rulenum >= c->num_rules) { |
| Martin Josefsson | 631f361 | 2004-09-23 19:25:06 +0000 | [diff] [blame] | 2090 | errno = E2BIG; |
| 2091 | return 0; |
| 2092 | } |
| 2093 | |
| 2094 | /* Take advantage of the double linked list if possible. */ |
| Martin Josefsson | a5616dc | 2004-10-24 22:27:31 +0000 | [diff] [blame] | 2095 | if (rulenum + 1 <= c->num_rules/2) { |
| 2096 | r = iptcc_get_rule_num(c, rulenum + 1); |
| 2097 | } else { |
| 2098 | r = iptcc_get_rule_num_reverse(c, c->num_rules - rulenum); |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2099 | } |
| 2100 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2101 | /* If we are about to delete the rule that is the current |
| 2102 | * iterator, move rule iterator back. next pointer will then |
| 2103 | * point to real next node */ |
| 2104 | if (r == (*handle)->rule_iterator_cur) { |
| 2105 | (*handle)->rule_iterator_cur = |
| 2106 | list_entry((*handle)->rule_iterator_cur->list.prev, |
| 2107 | struct rule_head, list); |
| Harald Welte | 0113fe7 | 2004-01-06 19:04:02 +0000 | [diff] [blame] | 2108 | } |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2109 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2110 | c->num_rules--; |
| 2111 | iptcc_delete_rule(r); |
| 2112 | |
| Martin Josefsson | 2a5dbbb | 2004-09-22 21:37:41 +0000 | [diff] [blame] | 2113 | set_changed(*handle); |
| 2114 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2115 | return 1; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2116 | } |
| 2117 | |
| 2118 | /* Check the packet `fw' on chain `chain'. Returns the verdict, or |
| 2119 | NULL and sets errno. */ |
| 2120 | const char * |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 2121 | TC_CHECK_PACKET(const IPT_CHAINLABEL chain, |
| 2122 | STRUCT_ENTRY *entry, |
| Jan Engelhardt | fd18731 | 2008-11-10 16:59:27 +0100 | [diff] [blame^] | 2123 | struct xtc_handle **handle) |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2124 | { |
| Derrik Pates | 664c0a3 | 2005-02-01 13:28:14 +0000 | [diff] [blame] | 2125 | iptc_fn = TC_CHECK_PACKET; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2126 | errno = ENOSYS; |
| 2127 | return NULL; |
| 2128 | } |
| 2129 | |
| 2130 | /* Flushes the entries in the given chain (ie. empties chain). */ |
| 2131 | int |
| Jan Engelhardt | fd18731 | 2008-11-10 16:59:27 +0100 | [diff] [blame^] | 2132 | TC_FLUSH_ENTRIES(const IPT_CHAINLABEL chain, struct xtc_handle **handle) |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2133 | { |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2134 | struct chain_head *c; |
| 2135 | struct rule_head *r, *tmp; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2136 | |
| Harald Welte | 0113fe7 | 2004-01-06 19:04:02 +0000 | [diff] [blame] | 2137 | iptc_fn = TC_FLUSH_ENTRIES; |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2138 | if (!(c = iptcc_find_label(chain, *handle))) { |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2139 | errno = ENOENT; |
| 2140 | return 0; |
| 2141 | } |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2142 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2143 | list_for_each_entry_safe(r, tmp, &c->rules, list) { |
| 2144 | iptcc_delete_rule(r); |
| 2145 | } |
| 2146 | |
| 2147 | c->num_rules = 0; |
| 2148 | |
| 2149 | set_changed(*handle); |
| 2150 | |
| 2151 | return 1; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2152 | } |
| 2153 | |
| 2154 | /* Zeroes the counters in a chain. */ |
| 2155 | int |
| Jan Engelhardt | fd18731 | 2008-11-10 16:59:27 +0100 | [diff] [blame^] | 2156 | TC_ZERO_ENTRIES(const IPT_CHAINLABEL chain, struct xtc_handle **handle) |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2157 | { |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2158 | struct chain_head *c; |
| 2159 | struct rule_head *r; |
| Rusty Russell | 7e53bf9 | 2000-03-20 07:03:28 +0000 | [diff] [blame] | 2160 | |
| Derrik Pates | 664c0a3 | 2005-02-01 13:28:14 +0000 | [diff] [blame] | 2161 | iptc_fn = TC_ZERO_ENTRIES; |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2162 | if (!(c = iptcc_find_label(chain, *handle))) { |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2163 | errno = ENOENT; |
| 2164 | return 0; |
| 2165 | } |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2166 | |
| Andy Gay | e5bd1d7 | 2006-08-22 02:56:41 +0000 | [diff] [blame] | 2167 | if (c->counter_map.maptype == COUNTER_MAP_NORMAL_MAP) |
| 2168 | c->counter_map.maptype = COUNTER_MAP_ZEROED; |
| 2169 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2170 | list_for_each_entry(r, &c->rules, list) { |
| 2171 | if (r->counter_map.maptype == COUNTER_MAP_NORMAL_MAP) |
| 2172 | r->counter_map.maptype = COUNTER_MAP_ZEROED; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2173 | } |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2174 | |
| Rusty Russell | 175f641 | 2000-03-24 09:32:20 +0000 | [diff] [blame] | 2175 | set_changed(*handle); |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2176 | |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2177 | return 1; |
| 2178 | } |
| 2179 | |
| Harald Welte | 1cef74d | 2001-01-05 15:22:59 +0000 | [diff] [blame] | 2180 | STRUCT_COUNTERS * |
| 2181 | TC_READ_COUNTER(const IPT_CHAINLABEL chain, |
| 2182 | unsigned int rulenum, |
| Jan Engelhardt | fd18731 | 2008-11-10 16:59:27 +0100 | [diff] [blame^] | 2183 | struct xtc_handle **handle) |
| Harald Welte | 1cef74d | 2001-01-05 15:22:59 +0000 | [diff] [blame] | 2184 | { |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2185 | struct chain_head *c; |
| 2186 | struct rule_head *r; |
| Harald Welte | 1cef74d | 2001-01-05 15:22:59 +0000 | [diff] [blame] | 2187 | |
| 2188 | iptc_fn = TC_READ_COUNTER; |
| 2189 | CHECK(*handle); |
| 2190 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2191 | if (!(c = iptcc_find_label(chain, *handle))) { |
| Harald Welte | 1cef74d | 2001-01-05 15:22:59 +0000 | [diff] [blame] | 2192 | errno = ENOENT; |
| 2193 | return NULL; |
| 2194 | } |
| 2195 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2196 | if (!(r = iptcc_get_rule_num(c, rulenum))) { |
| Harald Welte | 0113fe7 | 2004-01-06 19:04:02 +0000 | [diff] [blame] | 2197 | errno = E2BIG; |
| 2198 | return NULL; |
| 2199 | } |
| 2200 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2201 | return &r->entry[0].counters; |
| Harald Welte | 1cef74d | 2001-01-05 15:22:59 +0000 | [diff] [blame] | 2202 | } |
| 2203 | |
| 2204 | int |
| 2205 | TC_ZERO_COUNTER(const IPT_CHAINLABEL chain, |
| 2206 | unsigned int rulenum, |
| Jan Engelhardt | fd18731 | 2008-11-10 16:59:27 +0100 | [diff] [blame^] | 2207 | struct xtc_handle **handle) |
| Harald Welte | 1cef74d | 2001-01-05 15:22:59 +0000 | [diff] [blame] | 2208 | { |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2209 | struct chain_head *c; |
| 2210 | struct rule_head *r; |
| Harald Welte | 1cef74d | 2001-01-05 15:22:59 +0000 | [diff] [blame] | 2211 | |
| 2212 | iptc_fn = TC_ZERO_COUNTER; |
| 2213 | CHECK(*handle); |
| 2214 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2215 | if (!(c = iptcc_find_label(chain, *handle))) { |
| Harald Welte | 1cef74d | 2001-01-05 15:22:59 +0000 | [diff] [blame] | 2216 | errno = ENOENT; |
| 2217 | return 0; |
| 2218 | } |
| 2219 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2220 | if (!(r = iptcc_get_rule_num(c, rulenum))) { |
| Harald Welte | 0113fe7 | 2004-01-06 19:04:02 +0000 | [diff] [blame] | 2221 | errno = E2BIG; |
| 2222 | return 0; |
| 2223 | } |
| 2224 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2225 | if (r->counter_map.maptype == COUNTER_MAP_NORMAL_MAP) |
| 2226 | r->counter_map.maptype = COUNTER_MAP_ZEROED; |
| Harald Welte | 1cef74d | 2001-01-05 15:22:59 +0000 | [diff] [blame] | 2227 | |
| 2228 | set_changed(*handle); |
| 2229 | |
| 2230 | return 1; |
| 2231 | } |
| 2232 | |
| 2233 | int |
| 2234 | TC_SET_COUNTER(const IPT_CHAINLABEL chain, |
| 2235 | unsigned int rulenum, |
| 2236 | STRUCT_COUNTERS *counters, |
| Jan Engelhardt | fd18731 | 2008-11-10 16:59:27 +0100 | [diff] [blame^] | 2237 | struct xtc_handle **handle) |
| Harald Welte | 1cef74d | 2001-01-05 15:22:59 +0000 | [diff] [blame] | 2238 | { |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2239 | struct chain_head *c; |
| 2240 | struct rule_head *r; |
| Harald Welte | 1cef74d | 2001-01-05 15:22:59 +0000 | [diff] [blame] | 2241 | STRUCT_ENTRY *e; |
| Harald Welte | 1cef74d | 2001-01-05 15:22:59 +0000 | [diff] [blame] | 2242 | |
| 2243 | iptc_fn = TC_SET_COUNTER; |
| 2244 | CHECK(*handle); |
| 2245 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2246 | if (!(c = iptcc_find_label(chain, *handle))) { |
| Harald Welte | 1cef74d | 2001-01-05 15:22:59 +0000 | [diff] [blame] | 2247 | errno = ENOENT; |
| 2248 | return 0; |
| 2249 | } |
| Harald Welte | 0113fe7 | 2004-01-06 19:04:02 +0000 | [diff] [blame] | 2250 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2251 | if (!(r = iptcc_get_rule_num(c, rulenum))) { |
| Harald Welte | 0113fe7 | 2004-01-06 19:04:02 +0000 | [diff] [blame] | 2252 | errno = E2BIG; |
| 2253 | return 0; |
| 2254 | } |
| 2255 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2256 | e = r->entry; |
| 2257 | r->counter_map.maptype = COUNTER_MAP_SET; |
| Harald Welte | 0113fe7 | 2004-01-06 19:04:02 +0000 | [diff] [blame] | 2258 | |
| 2259 | memcpy(&e->counters, counters, sizeof(STRUCT_COUNTERS)); |
| Harald Welte | 1cef74d | 2001-01-05 15:22:59 +0000 | [diff] [blame] | 2260 | |
| 2261 | set_changed(*handle); |
| 2262 | |
| 2263 | return 1; |
| 2264 | } |
| 2265 | |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2266 | /* Creates a new chain. */ |
| 2267 | /* To create a chain, create two rules: error node and unconditional |
| 2268 | * return. */ |
| 2269 | int |
| Jan Engelhardt | fd18731 | 2008-11-10 16:59:27 +0100 | [diff] [blame^] | 2270 | TC_CREATE_CHAIN(const IPT_CHAINLABEL chain, struct xtc_handle **handle) |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2271 | { |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2272 | static struct chain_head *c; |
| Patrick McHardy | 1f23d3c | 2008-06-07 15:04:34 +0200 | [diff] [blame] | 2273 | int capacity; |
| 2274 | int exceeded; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2275 | |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 2276 | iptc_fn = TC_CREATE_CHAIN; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2277 | |
| 2278 | /* find_label doesn't cover built-in targets: DROP, ACCEPT, |
| 2279 | QUEUE, RETURN. */ |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2280 | if (iptcc_find_label(chain, *handle) |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 2281 | || strcmp(chain, LABEL_DROP) == 0 |
| 2282 | || strcmp(chain, LABEL_ACCEPT) == 0 |
| Rusty Russell | 67088e7 | 2000-05-10 01:18:57 +0000 | [diff] [blame] | 2283 | || strcmp(chain, LABEL_QUEUE) == 0 |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 2284 | || strcmp(chain, LABEL_RETURN) == 0) { |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2285 | DEBUGP("Chain `%s' already exists\n", chain); |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2286 | errno = EEXIST; |
| 2287 | return 0; |
| 2288 | } |
| 2289 | |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 2290 | if (strlen(chain)+1 > sizeof(IPT_CHAINLABEL)) { |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2291 | DEBUGP("Chain name `%s' too long\n", chain); |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2292 | errno = EINVAL; |
| 2293 | return 0; |
| 2294 | } |
| 2295 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2296 | c = iptcc_alloc_chain_head(chain, 0); |
| 2297 | if (!c) { |
| 2298 | DEBUGP("Cannot allocate memory for chain `%s'\n", chain); |
| 2299 | errno = ENOMEM; |
| 2300 | return 0; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2301 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2302 | } |
| Jesper Dangaard Brouer | 48bde40 | 2008-01-15 17:06:48 +0000 | [diff] [blame] | 2303 | (*handle)->num_chains++; /* New user defined chain */ |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2304 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2305 | DEBUGP("Creating chain `%s'\n", chain); |
| Jesper Dangaard Brouer | d8cb787 | 2007-11-28 08:40:26 +0000 | [diff] [blame] | 2306 | iptc_insert_chain(*handle, c); /* Insert sorted */ |
| Harald Welte | 0113fe7 | 2004-01-06 19:04:02 +0000 | [diff] [blame] | 2307 | |
| Jesper Dangaard Brouer | 01444da | 2008-01-15 17:18:15 +0000 | [diff] [blame] | 2308 | /* Inserting chains don't change the correctness of the chain |
| 2309 | * index (except if its smaller than index[0], but that |
| 2310 | * handled by iptc_insert_chain). It only causes longer lists |
| 2311 | * in the buckets. Thus, only rebuild chain index when the |
| 2312 | * capacity is exceed with CHAIN_INDEX_INSERT_MAX chains. |
| 2313 | */ |
| Patrick McHardy | 1f23d3c | 2008-06-07 15:04:34 +0200 | [diff] [blame] | 2314 | capacity = (*handle)->chain_index_sz * CHAIN_INDEX_BUCKET_LEN; |
| 2315 | exceeded = ((((*handle)->num_chains)-capacity)); |
| Jesper Dangaard Brouer | 01444da | 2008-01-15 17:18:15 +0000 | [diff] [blame] | 2316 | if (exceeded > CHAIN_INDEX_INSERT_MAX) { |
| 2317 | debug("Capacity(%d) exceeded(%d) rebuild (chains:%d)\n", |
| 2318 | capacity, exceeded, (*handle)->num_chains); |
| 2319 | iptcc_chain_index_rebuild(*handle); |
| 2320 | } |
| 2321 | |
| Harald Welte | 0113fe7 | 2004-01-06 19:04:02 +0000 | [diff] [blame] | 2322 | set_changed(*handle); |
| 2323 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2324 | return 1; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2325 | } |
| 2326 | |
| 2327 | /* Get the number of references to this chain. */ |
| 2328 | int |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 2329 | TC_GET_REFERENCES(unsigned int *ref, const IPT_CHAINLABEL chain, |
| Jan Engelhardt | fd18731 | 2008-11-10 16:59:27 +0100 | [diff] [blame^] | 2330 | struct xtc_handle **handle) |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2331 | { |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2332 | struct chain_head *c; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2333 | |
| Derrik Pates | 664c0a3 | 2005-02-01 13:28:14 +0000 | [diff] [blame] | 2334 | iptc_fn = TC_GET_REFERENCES; |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2335 | if (!(c = iptcc_find_label(chain, *handle))) { |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2336 | errno = ENOENT; |
| 2337 | return 0; |
| 2338 | } |
| 2339 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2340 | *ref = c->references; |
| 2341 | |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2342 | return 1; |
| 2343 | } |
| 2344 | |
| 2345 | /* Deletes a chain. */ |
| 2346 | int |
| Jan Engelhardt | fd18731 | 2008-11-10 16:59:27 +0100 | [diff] [blame^] | 2347 | TC_DELETE_CHAIN(const IPT_CHAINLABEL chain, struct xtc_handle **handle) |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2348 | { |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2349 | unsigned int references; |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2350 | struct chain_head *c; |
| Rusty Russell | 7e53bf9 | 2000-03-20 07:03:28 +0000 | [diff] [blame] | 2351 | |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 2352 | iptc_fn = TC_DELETE_CHAIN; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2353 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2354 | if (!(c = iptcc_find_label(chain, *handle))) { |
| 2355 | DEBUGP("cannot find chain `%s'\n", chain); |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2356 | errno = ENOENT; |
| 2357 | return 0; |
| 2358 | } |
| 2359 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2360 | if (TC_BUILTIN(chain, *handle)) { |
| 2361 | DEBUGP("cannot remove builtin chain `%s'\n", chain); |
| 2362 | errno = EINVAL; |
| 2363 | return 0; |
| 2364 | } |
| 2365 | |
| 2366 | if (!TC_GET_REFERENCES(&references, chain, handle)) { |
| 2367 | DEBUGP("cannot get references on chain `%s'\n", chain); |
| 2368 | return 0; |
| 2369 | } |
| 2370 | |
| 2371 | if (references > 0) { |
| 2372 | DEBUGP("chain `%s' still has references\n", chain); |
| 2373 | errno = EMLINK; |
| 2374 | return 0; |
| 2375 | } |
| 2376 | |
| 2377 | if (c->num_rules) { |
| 2378 | DEBUGP("chain `%s' is not empty\n", chain); |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2379 | errno = ENOTEMPTY; |
| 2380 | return 0; |
| 2381 | } |
| 2382 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2383 | /* 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] | 2384 | * iterator, move chain iterator forward. */ |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2385 | if (c == (*handle)->chain_iterator_cur) |
| 2386 | iptcc_chain_iterator_advance(*handle); |
| Harald Welte | 0113fe7 | 2004-01-06 19:04:02 +0000 | [diff] [blame] | 2387 | |
| Jesper Dangaard Brouer | 48bde40 | 2008-01-15 17:06:48 +0000 | [diff] [blame] | 2388 | (*handle)->num_chains--; /* One user defined chain deleted */ |
| 2389 | |
| Jesper Dangaard Brouer | 01444da | 2008-01-15 17:18:15 +0000 | [diff] [blame] | 2390 | //list_del(&c->list); /* Done in iptcc_chain_index_delete_chain() */ |
| 2391 | iptcc_chain_index_delete_chain(c, *handle); |
| 2392 | free(c); |
| 2393 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2394 | DEBUGP("chain `%s' deleted\n", chain); |
| 2395 | |
| 2396 | set_changed(*handle); |
| 2397 | |
| 2398 | return 1; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2399 | } |
| 2400 | |
| 2401 | /* Renames a chain. */ |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 2402 | int TC_RENAME_CHAIN(const IPT_CHAINLABEL oldname, |
| 2403 | const IPT_CHAINLABEL newname, |
| Jan Engelhardt | fd18731 | 2008-11-10 16:59:27 +0100 | [diff] [blame^] | 2404 | struct xtc_handle **handle) |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2405 | { |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2406 | struct chain_head *c; |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 2407 | iptc_fn = TC_RENAME_CHAIN; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2408 | |
| Harald Welte | 1de8046 | 2000-10-30 12:00:27 +0000 | [diff] [blame] | 2409 | /* find_label doesn't cover built-in targets: DROP, ACCEPT, |
| 2410 | QUEUE, RETURN. */ |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2411 | if (iptcc_find_label(newname, *handle) |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 2412 | || strcmp(newname, LABEL_DROP) == 0 |
| 2413 | || strcmp(newname, LABEL_ACCEPT) == 0 |
| Harald Welte | 1de8046 | 2000-10-30 12:00:27 +0000 | [diff] [blame] | 2414 | || strcmp(newname, LABEL_QUEUE) == 0 |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 2415 | || strcmp(newname, LABEL_RETURN) == 0) { |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2416 | errno = EEXIST; |
| 2417 | return 0; |
| 2418 | } |
| 2419 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2420 | if (!(c = iptcc_find_label(oldname, *handle)) |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 2421 | || TC_BUILTIN(oldname, *handle)) { |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2422 | errno = ENOENT; |
| 2423 | return 0; |
| 2424 | } |
| 2425 | |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 2426 | if (strlen(newname)+1 > sizeof(IPT_CHAINLABEL)) { |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2427 | errno = EINVAL; |
| 2428 | return 0; |
| 2429 | } |
| 2430 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2431 | strncpy(c->name, newname, sizeof(IPT_CHAINLABEL)); |
| 2432 | |
| Harald Welte | 0113fe7 | 2004-01-06 19:04:02 +0000 | [diff] [blame] | 2433 | set_changed(*handle); |
| 2434 | |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2435 | return 1; |
| 2436 | } |
| 2437 | |
| 2438 | /* Sets the policy on a built-in chain. */ |
| 2439 | int |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 2440 | TC_SET_POLICY(const IPT_CHAINLABEL chain, |
| 2441 | const IPT_CHAINLABEL policy, |
| Harald Welte | 1cef74d | 2001-01-05 15:22:59 +0000 | [diff] [blame] | 2442 | STRUCT_COUNTERS *counters, |
| Jan Engelhardt | fd18731 | 2008-11-10 16:59:27 +0100 | [diff] [blame^] | 2443 | struct xtc_handle **handle) |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2444 | { |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2445 | struct chain_head *c; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2446 | |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 2447 | iptc_fn = TC_SET_POLICY; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2448 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2449 | if (!(c = iptcc_find_label(chain, *handle))) { |
| 2450 | DEBUGP("cannot find chain `%s'\n", chain); |
| 2451 | errno = ENOENT; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2452 | return 0; |
| 2453 | } |
| 2454 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2455 | if (!iptcc_is_builtin(c)) { |
| 2456 | DEBUGP("cannot set policy of userdefinedchain `%s'\n", chain); |
| 2457 | errno = ENOENT; |
| 2458 | return 0; |
| 2459 | } |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2460 | |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 2461 | if (strcmp(policy, LABEL_ACCEPT) == 0) |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2462 | c->verdict = -NF_ACCEPT - 1; |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 2463 | else if (strcmp(policy, LABEL_DROP) == 0) |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2464 | c->verdict = -NF_DROP - 1; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2465 | else { |
| 2466 | errno = EINVAL; |
| 2467 | return 0; |
| 2468 | } |
| Harald Welte | 1cef74d | 2001-01-05 15:22:59 +0000 | [diff] [blame] | 2469 | |
| Harald Welte | 1cef74d | 2001-01-05 15:22:59 +0000 | [diff] [blame] | 2470 | if (counters) { |
| 2471 | /* set byte and packet counters */ |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2472 | memcpy(&c->counters, counters, sizeof(STRUCT_COUNTERS)); |
| 2473 | c->counter_map.maptype = COUNTER_MAP_SET; |
| Harald Welte | 1cef74d | 2001-01-05 15:22:59 +0000 | [diff] [blame] | 2474 | } else { |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2475 | c->counter_map.maptype = COUNTER_MAP_NOMAP; |
| Harald Welte | 1cef74d | 2001-01-05 15:22:59 +0000 | [diff] [blame] | 2476 | } |
| 2477 | |
| Rusty Russell | 175f641 | 2000-03-24 09:32:20 +0000 | [diff] [blame] | 2478 | set_changed(*handle); |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2479 | |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2480 | return 1; |
| 2481 | } |
| 2482 | |
| 2483 | /* Without this, on gcc 2.7.2.3, we get: |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 2484 | libiptc.c: In function `TC_COMMIT': |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2485 | libiptc.c:833: fixed or forbidden register was spilled. |
| 2486 | This may be due to a compiler bug or to impossible asm |
| 2487 | statements or clauses. |
| 2488 | */ |
| 2489 | static void |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 2490 | subtract_counters(STRUCT_COUNTERS *answer, |
| 2491 | const STRUCT_COUNTERS *a, |
| 2492 | const STRUCT_COUNTERS *b) |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2493 | { |
| 2494 | answer->pcnt = a->pcnt - b->pcnt; |
| 2495 | answer->bcnt = a->bcnt - b->bcnt; |
| 2496 | } |
| 2497 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2498 | |
| Jan Engelhardt | dbb7754 | 2008-02-11 00:33:30 +0100 | [diff] [blame] | 2499 | static void counters_nomap(STRUCT_COUNTERS_INFO *newcounters, unsigned int idx) |
| 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 | newcounters->counters[idx] = ((STRUCT_COUNTERS) { 0, 0}); |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2502 | DEBUGP_C("NOMAP => zero\n"); |
| 2503 | } |
| 2504 | |
| 2505 | static void counters_normal_map(STRUCT_COUNTERS_INFO *newcounters, |
| Jan Engelhardt | dbb7754 | 2008-02-11 00:33:30 +0100 | [diff] [blame] | 2506 | STRUCT_REPLACE *repl, unsigned int idx, |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2507 | unsigned int mappos) |
| 2508 | { |
| 2509 | /* Original read: X. |
| 2510 | * Atomic read on replacement: X + Y. |
| 2511 | * Currently in kernel: Z. |
| 2512 | * Want in kernel: X + Y + Z. |
| 2513 | * => Add in X + Y |
| 2514 | * => Add in replacement read. |
| 2515 | */ |
| Jan Engelhardt | dbb7754 | 2008-02-11 00:33:30 +0100 | [diff] [blame] | 2516 | newcounters->counters[idx] = repl->counters[mappos]; |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2517 | DEBUGP_C("NORMAL_MAP => mappos %u \n", mappos); |
| 2518 | } |
| 2519 | |
| 2520 | static void counters_map_zeroed(STRUCT_COUNTERS_INFO *newcounters, |
| Jan Engelhardt | dbb7754 | 2008-02-11 00:33:30 +0100 | [diff] [blame] | 2521 | STRUCT_REPLACE *repl, unsigned int idx, |
| 2522 | unsigned int mappos, STRUCT_COUNTERS *counters) |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2523 | { |
| 2524 | /* Original read: X. |
| 2525 | * Atomic read on replacement: X + Y. |
| 2526 | * Currently in kernel: Z. |
| 2527 | * Want in kernel: Y + Z. |
| 2528 | * => Add in Y. |
| 2529 | * => Add in (replacement read - original read). |
| 2530 | */ |
| Jan Engelhardt | dbb7754 | 2008-02-11 00:33:30 +0100 | [diff] [blame] | 2531 | subtract_counters(&newcounters->counters[idx], |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2532 | &repl->counters[mappos], |
| 2533 | counters); |
| 2534 | DEBUGP_C("ZEROED => mappos %u\n", mappos); |
| 2535 | } |
| 2536 | |
| 2537 | static void counters_map_set(STRUCT_COUNTERS_INFO *newcounters, |
| Jan Engelhardt | dbb7754 | 2008-02-11 00:33:30 +0100 | [diff] [blame] | 2538 | unsigned int idx, STRUCT_COUNTERS *counters) |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2539 | { |
| 2540 | /* Want to set counter (iptables-restore) */ |
| 2541 | |
| Jan Engelhardt | dbb7754 | 2008-02-11 00:33:30 +0100 | [diff] [blame] | 2542 | memcpy(&newcounters->counters[idx], counters, |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2543 | sizeof(STRUCT_COUNTERS)); |
| 2544 | |
| 2545 | DEBUGP_C("SET\n"); |
| 2546 | } |
| 2547 | |
| 2548 | |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2549 | int |
| Jan Engelhardt | fd18731 | 2008-11-10 16:59:27 +0100 | [diff] [blame^] | 2550 | TC_COMMIT(struct xtc_handle **handle) |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2551 | { |
| 2552 | /* Replace, then map back the counters. */ |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 2553 | STRUCT_REPLACE *repl; |
| 2554 | STRUCT_COUNTERS_INFO *newcounters; |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2555 | struct chain_head *c; |
| 2556 | int ret; |
| Martin Josefsson | 841e4ae | 2003-05-02 15:30:11 +0000 | [diff] [blame] | 2557 | size_t counterlen; |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2558 | int new_number; |
| 2559 | unsigned int new_size; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2560 | |
| Derrik Pates | 664c0a3 | 2005-02-01 13:28:14 +0000 | [diff] [blame] | 2561 | iptc_fn = TC_COMMIT; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2562 | CHECK(*handle); |
| Martin Josefsson | 841e4ae | 2003-05-02 15:30:11 +0000 | [diff] [blame] | 2563 | |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2564 | /* Don't commit if nothing changed. */ |
| 2565 | if (!(*handle)->changed) |
| 2566 | goto finished; |
| 2567 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2568 | new_number = iptcc_compile_table_prep(*handle, &new_size); |
| 2569 | if (new_number < 0) { |
| 2570 | errno = ENOMEM; |
| Harald Welte | d6ba6f5 | 2005-11-12 10:39:40 +0000 | [diff] [blame] | 2571 | goto out_zero; |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2572 | } |
| 2573 | |
| 2574 | repl = malloc(sizeof(*repl) + new_size); |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2575 | if (!repl) { |
| 2576 | errno = ENOMEM; |
| Harald Welte | d6ba6f5 | 2005-11-12 10:39:40 +0000 | [diff] [blame] | 2577 | goto out_zero; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2578 | } |
| Martin Josefsson | ad3b4f9 | 2004-09-22 22:04:07 +0000 | [diff] [blame] | 2579 | memset(repl, 0, sizeof(*repl) + new_size); |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2580 | |
| Rusty Russell | e45c713 | 2004-12-16 13:21:44 +0000 | [diff] [blame] | 2581 | #if 0 |
| 2582 | TC_DUMP_ENTRIES(*handle); |
| 2583 | #endif |
| 2584 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2585 | counterlen = sizeof(STRUCT_COUNTERS_INFO) |
| 2586 | + sizeof(STRUCT_COUNTERS) * new_number; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2587 | |
| 2588 | /* These are the old counters we will get from kernel */ |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 2589 | repl->counters = malloc(sizeof(STRUCT_COUNTERS) |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2590 | * (*handle)->info.num_entries); |
| 2591 | if (!repl->counters) { |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2592 | errno = ENOMEM; |
| Harald Welte | d6ba6f5 | 2005-11-12 10:39:40 +0000 | [diff] [blame] | 2593 | goto out_free_repl; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2594 | } |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2595 | /* These are the counters we're going to put back, later. */ |
| 2596 | newcounters = malloc(counterlen); |
| 2597 | if (!newcounters) { |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2598 | errno = ENOMEM; |
| Harald Welte | d6ba6f5 | 2005-11-12 10:39:40 +0000 | [diff] [blame] | 2599 | goto out_free_repl_counters; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2600 | } |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2601 | memset(newcounters, 0, counterlen); |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2602 | |
| 2603 | strcpy(repl->name, (*handle)->info.name); |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2604 | repl->num_entries = new_number; |
| 2605 | repl->size = new_size; |
| 2606 | |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2607 | repl->num_counters = (*handle)->info.num_entries; |
| 2608 | repl->valid_hooks = (*handle)->info.valid_hooks; |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2609 | |
| 2610 | DEBUGP("num_entries=%u, size=%u, num_counters=%u\n", |
| 2611 | repl->num_entries, repl->size, repl->num_counters); |
| 2612 | |
| 2613 | ret = iptcc_compile_table(*handle, repl); |
| 2614 | if (ret < 0) { |
| 2615 | errno = ret; |
| Harald Welte | d6ba6f5 | 2005-11-12 10:39:40 +0000 | [diff] [blame] | 2616 | goto out_free_newcounters; |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2617 | } |
| 2618 | |
| 2619 | |
| 2620 | #ifdef IPTC_DEBUG2 |
| 2621 | { |
| 2622 | int fd = open("/tmp/libiptc-so_set_replace.blob", |
| 2623 | O_CREAT|O_WRONLY); |
| 2624 | if (fd >= 0) { |
| 2625 | write(fd, repl, sizeof(*repl) + repl->size); |
| 2626 | close(fd); |
| 2627 | } |
| 2628 | } |
| 2629 | #endif |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2630 | |
| Harald Welte | d6ba6f5 | 2005-11-12 10:39:40 +0000 | [diff] [blame] | 2631 | ret = setsockopt(sockfd, TC_IPPROTO, SO_SET_REPLACE, repl, |
| 2632 | sizeof(*repl) + repl->size); |
| Patrick McHardy | e0865ad | 2006-04-22 02:08:56 +0000 | [diff] [blame] | 2633 | if (ret < 0) |
| Harald Welte | d6ba6f5 | 2005-11-12 10:39:40 +0000 | [diff] [blame] | 2634 | goto out_free_newcounters; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2635 | |
| 2636 | /* Put counters back. */ |
| 2637 | strcpy(newcounters->name, (*handle)->info.name); |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2638 | newcounters->num_counters = new_number; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2639 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2640 | list_for_each_entry(c, &(*handle)->chains, list) { |
| 2641 | struct rule_head *r; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2642 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2643 | /* Builtin chains have their own counters */ |
| 2644 | if (iptcc_is_builtin(c)) { |
| 2645 | DEBUGP("counter for chain-index %u: ", c->foot_index); |
| 2646 | switch(c->counter_map.maptype) { |
| 2647 | case COUNTER_MAP_NOMAP: |
| 2648 | counters_nomap(newcounters, c->foot_index); |
| 2649 | break; |
| 2650 | case COUNTER_MAP_NORMAL_MAP: |
| 2651 | counters_normal_map(newcounters, repl, |
| 2652 | c->foot_index, |
| 2653 | c->counter_map.mappos); |
| 2654 | break; |
| 2655 | case COUNTER_MAP_ZEROED: |
| 2656 | counters_map_zeroed(newcounters, repl, |
| 2657 | c->foot_index, |
| 2658 | c->counter_map.mappos, |
| 2659 | &c->counters); |
| 2660 | break; |
| 2661 | case COUNTER_MAP_SET: |
| 2662 | counters_map_set(newcounters, c->foot_index, |
| 2663 | &c->counters); |
| 2664 | break; |
| 2665 | } |
| 2666 | } |
| Harald Welte | 1cef74d | 2001-01-05 15:22:59 +0000 | [diff] [blame] | 2667 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2668 | list_for_each_entry(r, &c->rules, list) { |
| 2669 | DEBUGP("counter for index %u: ", r->index); |
| 2670 | switch (r->counter_map.maptype) { |
| 2671 | case COUNTER_MAP_NOMAP: |
| 2672 | counters_nomap(newcounters, r->index); |
| 2673 | break; |
| Harald Welte | 1cef74d | 2001-01-05 15:22:59 +0000 | [diff] [blame] | 2674 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2675 | case COUNTER_MAP_NORMAL_MAP: |
| 2676 | counters_normal_map(newcounters, repl, |
| 2677 | r->index, |
| 2678 | r->counter_map.mappos); |
| 2679 | break; |
| Harald Welte | 1cef74d | 2001-01-05 15:22:59 +0000 | [diff] [blame] | 2680 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2681 | case COUNTER_MAP_ZEROED: |
| 2682 | counters_map_zeroed(newcounters, repl, |
| 2683 | r->index, |
| 2684 | r->counter_map.mappos, |
| 2685 | &r->entry->counters); |
| 2686 | break; |
| 2687 | |
| 2688 | case COUNTER_MAP_SET: |
| 2689 | counters_map_set(newcounters, r->index, |
| 2690 | &r->entry->counters); |
| 2691 | break; |
| 2692 | } |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2693 | } |
| 2694 | } |
| Rusty Russell | 62527ce | 2000-09-04 09:45:54 +0000 | [diff] [blame] | 2695 | |
| Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 2696 | #ifdef IPTC_DEBUG2 |
| 2697 | { |
| 2698 | int fd = open("/tmp/libiptc-so_set_add_counters.blob", |
| 2699 | O_CREAT|O_WRONLY); |
| 2700 | if (fd >= 0) { |
| 2701 | write(fd, newcounters, counterlen); |
| 2702 | close(fd); |
| 2703 | } |
| 2704 | } |
| 2705 | #endif |
| 2706 | |
| Harald Welte | d6ba6f5 | 2005-11-12 10:39:40 +0000 | [diff] [blame] | 2707 | ret = setsockopt(sockfd, TC_IPPROTO, SO_SET_ADD_COUNTERS, |
| 2708 | newcounters, counterlen); |
| Patrick McHardy | e0865ad | 2006-04-22 02:08:56 +0000 | [diff] [blame] | 2709 | if (ret < 0) |
| Harald Welte | d6ba6f5 | 2005-11-12 10:39:40 +0000 | [diff] [blame] | 2710 | goto out_free_newcounters; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2711 | |
| 2712 | free(repl->counters); |
| 2713 | free(repl); |
| 2714 | free(newcounters); |
| 2715 | |
| Harald Welte | d6ba6f5 | 2005-11-12 10:39:40 +0000 | [diff] [blame] | 2716 | finished: |
| Martin Josefsson | 841e4ae | 2003-05-02 15:30:11 +0000 | [diff] [blame] | 2717 | TC_FREE(handle); |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2718 | return 1; |
| Harald Welte | d6ba6f5 | 2005-11-12 10:39:40 +0000 | [diff] [blame] | 2719 | |
| 2720 | out_free_newcounters: |
| 2721 | free(newcounters); |
| 2722 | out_free_repl_counters: |
| 2723 | free(repl->counters); |
| 2724 | out_free_repl: |
| 2725 | free(repl); |
| 2726 | out_zero: |
| 2727 | return 0; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2728 | } |
| 2729 | |
| 2730 | /* Get raw socket. */ |
| 2731 | int |
| Patrick McHardy | 0b63936 | 2007-09-08 16:00:01 +0000 | [diff] [blame] | 2732 | TC_GET_RAW_SOCKET(void) |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2733 | { |
| 2734 | return sockfd; |
| 2735 | } |
| 2736 | |
| 2737 | /* Translates errno numbers into more human-readable form than strerror. */ |
| 2738 | const char * |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 2739 | TC_STRERROR(int err) |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2740 | { |
| 2741 | unsigned int i; |
| 2742 | struct table_struct { |
| 2743 | void *fn; |
| 2744 | int err; |
| 2745 | const char *message; |
| 2746 | } table [] = |
| Harald Welte | 4ccfa63 | 2001-07-30 15:12:43 +0000 | [diff] [blame] | 2747 | { { TC_INIT, EPERM, "Permission denied (you must be root)" }, |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 2748 | { TC_INIT, EINVAL, "Module is wrong version" }, |
| Harald Welte | 4ccfa63 | 2001-07-30 15:12:43 +0000 | [diff] [blame] | 2749 | { TC_INIT, ENOENT, |
| 2750 | "Table does not exist (do you need to insmod?)" }, |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 2751 | { TC_DELETE_CHAIN, ENOTEMPTY, "Chain is not empty" }, |
| 2752 | { TC_DELETE_CHAIN, EINVAL, "Can't delete built-in chain" }, |
| 2753 | { TC_DELETE_CHAIN, EMLINK, |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2754 | "Can't delete chain with references left" }, |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 2755 | { TC_CREATE_CHAIN, EEXIST, "Chain already exists" }, |
| 2756 | { TC_INSERT_ENTRY, E2BIG, "Index of insertion too big" }, |
| 2757 | { TC_REPLACE_ENTRY, E2BIG, "Index of replacement too big" }, |
| 2758 | { TC_DELETE_NUM_ENTRY, E2BIG, "Index of deletion too big" }, |
| Harald Welte | 1cef74d | 2001-01-05 15:22:59 +0000 | [diff] [blame] | 2759 | { TC_READ_COUNTER, E2BIG, "Index of counter too big" }, |
| 2760 | { TC_ZERO_COUNTER, E2BIG, "Index of counter too big" }, |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 2761 | { TC_INSERT_ENTRY, ELOOP, "Loop found in table" }, |
| 2762 | { TC_INSERT_ENTRY, EINVAL, "Target problem" }, |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2763 | /* EINVAL for CHECK probably means bad interface. */ |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 2764 | { TC_CHECK_PACKET, EINVAL, |
| Marc Boucher | c826499 | 2000-04-22 22:34:44 +0000 | [diff] [blame] | 2765 | "Bad arguments (does that interface exist?)" }, |
| Harald Welte | 4ccfa63 | 2001-07-30 15:12:43 +0000 | [diff] [blame] | 2766 | { TC_CHECK_PACKET, ENOSYS, |
| 2767 | "Checking will most likely never get implemented" }, |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2768 | /* ENOENT for DELETE probably means no matching rule */ |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 2769 | { TC_DELETE_ENTRY, ENOENT, |
| Marc Boucher | c826499 | 2000-04-22 22:34:44 +0000 | [diff] [blame] | 2770 | "Bad rule (does a matching rule exist in that chain?)" }, |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 2771 | { TC_SET_POLICY, ENOENT, |
| Marc Boucher | c826499 | 2000-04-22 22:34:44 +0000 | [diff] [blame] | 2772 | "Bad built-in chain name" }, |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 2773 | { TC_SET_POLICY, EINVAL, |
| Marc Boucher | c826499 | 2000-04-22 22:34:44 +0000 | [diff] [blame] | 2774 | "Bad policy name" }, |
| Harald Welte | 4ccfa63 | 2001-07-30 15:12:43 +0000 | [diff] [blame] | 2775 | |
| 2776 | { NULL, 0, "Incompatible with this kernel" }, |
| 2777 | { NULL, ENOPROTOOPT, "iptables who? (do you need to insmod?)" }, |
| 2778 | { NULL, ENOSYS, "Will be implemented real soon. I promise ;)" }, |
| 2779 | { NULL, ENOMEM, "Memory allocation problem" }, |
| 2780 | { NULL, ENOENT, "No chain/target/match by that name" }, |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 2781 | }; |
| 2782 | |
| 2783 | for (i = 0; i < sizeof(table)/sizeof(struct table_struct); i++) { |
| 2784 | if ((!table[i].fn || table[i].fn == iptc_fn) |
| 2785 | && table[i].err == err) |
| 2786 | return table[i].message; |
| 2787 | } |
| 2788 | |
| 2789 | return strerror(err); |
| 2790 | } |