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