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