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