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