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