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