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