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