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