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