blob: 7bed22115fec163ab16e3869e1f7757dcde75449 [file] [log] [blame]
Martin Josefsson0f9b8b12004-12-18 17:18:49 +00001/* Library which manipulates firewall rules. Version $Revision$ */
Marc Bouchere6869a82000-03-20 06:03:29 +00002
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 Welte3ea8f402003-06-23 18:25:59 +000011/* (C) 1999 Paul ``Rusty'' Russell - Placed under the GNU GPL (See
12 * COPYING for details).
Harald Welteaae69be2004-08-29 23:32:14 +000013 * (C) 2000-2004 by the Netfilter Core Team <coreteam@netfilter.org>
Harald Welte3ea8f402003-06-23 18:25:59 +000014 *
Harald Weltefbc85232003-06-24 17:37:21 +000015 * 2003-Jun-20: Harald Welte <laforge@netfilter.org>:
Harald Welte3ea8f402003-06-23 18:25:59 +000016 * - Reimplementation of chain cache to use offsets instead of entries
Harald Weltefbc85232003-06-24 17:37:21 +000017 * 2003-Jun-23: Harald Welte <laforge@netfilter.org>:
Harald Welte0113fe72004-01-06 19:04:02 +000018 * - performance optimization, sponsored by Astaro AG (http://www.astaro.com/)
Harald Weltefbc85232003-06-24 17:37:21 +000019 * don't rebuild the chain cache after every operation, instead fix it
20 * up after a ruleset change.
Harald Welteaae69be2004-08-29 23:32:14 +000021 * 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 Welte3ea8f402003-06-23 18:25:59 +000026 */
Harald Welte15920d12004-05-16 09:05:07 +000027#include <sys/types.h>
28#include <sys/socket.h>
Stephane Ouellette7cd00282004-05-14 08:21:06 +000029
Harald Welteaae69be2004-08-29 23:32:14 +000030#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 Bouchere6869a82000-03-20 06:03:29 +000043#ifndef IPT_LIB_DIR
44#define IPT_LIB_DIR "/usr/local/lib/iptables"
45#endif
46
47static int sockfd = -1;
Derrik Pates664c0a32005-02-01 13:28:14 +000048static int sockfd_use = 0;
Marc Bouchere6869a82000-03-20 06:03:29 +000049static void *iptc_fn = NULL;
50
51static const char *hooknames[]
Rusty Russell79dee072000-05-02 16:45:16 +000052= { [HOOK_PRE_ROUTING] "PREROUTING",
53 [HOOK_LOCAL_IN] "INPUT",
54 [HOOK_FORWARD] "FORWARD",
55 [HOOK_LOCAL_OUT] "OUTPUT",
Rusty Russell10758b72000-09-14 07:37:33 +000056 [HOOK_POST_ROUTING] "POSTROUTING",
57#ifdef HOOK_DROPPING
58 [HOOK_DROPPING] "DROPPING"
59#endif
Marc Bouchere6869a82000-03-20 06:03:29 +000060};
61
Harald Welteaae69be2004-08-29 23:32:14 +000062/* Convenience structures */
63struct ipt_error_target
64{
65 STRUCT_ENTRY_TARGET t;
66 char error[TABLE_MAXNAMELEN];
67};
68
69struct chain_head;
70struct rule_head;
71
Marc Bouchere6869a82000-03-20 06:03:29 +000072struct counter_map
73{
74 enum {
75 COUNTER_MAP_NOMAP,
76 COUNTER_MAP_NORMAL_MAP,
Harald Welte1cef74d2001-01-05 15:22:59 +000077 COUNTER_MAP_ZEROED,
78 COUNTER_MAP_SET
Marc Bouchere6869a82000-03-20 06:03:29 +000079 } maptype;
80 unsigned int mappos;
81};
82
Harald Welteaae69be2004-08-29 23:32:14 +000083enum 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 Bouchere6869a82000-03-20 06:03:29 +000088};
89
Harald Welteaae69be2004-08-29 23:32:14 +000090struct rule_head
Rusty Russell30fd6e52000-04-23 09:16:06 +000091{
Harald Welteaae69be2004-08-29 23:32:14 +000092 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
106struct chain_head
107{
108 struct list_head list;
Rusty Russell79dee072000-05-02 16:45:16 +0000109 char name[TABLE_MAXNAMELEN];
Harald Welteaae69be2004-08-29 23:32:14 +0000110 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 Russell30fd6e52000-04-23 09:16:06 +0000124};
125
Rusty Russell79dee072000-05-02 16:45:16 +0000126STRUCT_TC_HANDLE
Marc Bouchere6869a82000-03-20 06:03:29 +0000127{
Harald Welteaae69be2004-08-29 23:32:14 +0000128 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 Russell79dee072000-05-02 16:45:16 +0000135 STRUCT_GETINFO info;
Harald Welteaae69be2004-08-29 23:32:14 +0000136 STRUCT_GET_ENTRIES *entries;
Marc Bouchere6869a82000-03-20 06:03:29 +0000137};
138
Harald Welteaae69be2004-08-29 23:32:14 +0000139/* allocate a new chain head for the cache */
140static 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 */
155static 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 Russell175f6412000-03-24 09:32:20 +0000169static void
Rusty Russell79dee072000-05-02 16:45:16 +0000170set_changed(TC_HANDLE_T h)
Rusty Russell175f6412000-03-24 09:32:20 +0000171{
Rusty Russell175f6412000-03-24 09:32:20 +0000172 h->changed = 1;
173}
174
Harald Welte380ba5f2002-02-13 16:19:55 +0000175#ifdef IPTC_DEBUG
Rusty Russell79dee072000-05-02 16:45:16 +0000176static void do_check(TC_HANDLE_T h, unsigned int line);
Rusty Russell849779c2000-04-23 15:51:51 +0000177#define CHECK(h) do { if (!getenv("IPTC_NO_CHECK")) do_check((h), __LINE__); } while(0)
Rusty Russell30fd6e52000-04-23 09:16:06 +0000178#else
179#define CHECK(h)
180#endif
Marc Bouchere6869a82000-03-20 06:03:29 +0000181
Harald Welteaae69be2004-08-29 23:32:14 +0000182
183/**********************************************************************
184 * iptc blob utility functions (iptcb_*)
185 **********************************************************************/
186
Marc Bouchere6869a82000-03-20 06:03:29 +0000187static inline int
Harald Welteaae69be2004-08-29 23:32:14 +0000188iptcb_get_number(const STRUCT_ENTRY *i,
Rusty Russell79dee072000-05-02 16:45:16 +0000189 const STRUCT_ENTRY *seek,
Marc Bouchere6869a82000-03-20 06:03:29 +0000190 unsigned int *pos)
191{
192 if (i == seek)
193 return 1;
194 (*pos)++;
195 return 0;
196}
197
Marc Bouchere6869a82000-03-20 06:03:29 +0000198static inline int
Harald Welteaae69be2004-08-29 23:32:14 +0000199iptcb_get_entry_n(STRUCT_ENTRY *i,
Marc Bouchere6869a82000-03-20 06:03:29 +0000200 unsigned int number,
201 unsigned int *pos,
Rusty Russell79dee072000-05-02 16:45:16 +0000202 STRUCT_ENTRY **pe)
Marc Bouchere6869a82000-03-20 06:03:29 +0000203{
204 if (*pos == number) {
205 *pe = i;
206 return 1;
207 }
208 (*pos)++;
209 return 0;
210}
211
Harald Welteaae69be2004-08-29 23:32:14 +0000212static inline STRUCT_ENTRY *
213iptcb_get_entry(TC_HANDLE_T h, unsigned int offset)
214{
215 return (STRUCT_ENTRY *)((char *)h->entries->entrytable + offset);
216}
217
218static unsigned int
219iptcb_entry2index(const TC_HANDLE_T h, const STRUCT_ENTRY *seek)
Marc Bouchere6869a82000-03-20 06:03:29 +0000220{
221 unsigned int pos = 0;
Marc Bouchere6869a82000-03-20 06:03:29 +0000222
Harald Welteaae69be2004-08-29 23:32:14 +0000223 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 Bouchere6869a82000-03-20 06:03:29 +0000230}
231
Harald Welte0113fe72004-01-06 19:04:02 +0000232static inline STRUCT_ENTRY *
Harald Welteaae69be2004-08-29 23:32:14 +0000233iptcb_offset2entry(TC_HANDLE_T h, unsigned int offset)
Harald Welte0113fe72004-01-06 19:04:02 +0000234{
Harald Welteaae69be2004-08-29 23:32:14 +0000235 return (STRUCT_ENTRY *) ((void *)h->entries->entrytable+offset);
Harald Welte0113fe72004-01-06 19:04:02 +0000236}
237
Harald Welteaae69be2004-08-29 23:32:14 +0000238
Harald Welte0113fe72004-01-06 19:04:02 +0000239static inline unsigned long
Harald Welteaae69be2004-08-29 23:32:14 +0000240iptcb_entry2offset(const TC_HANDLE_T h, const STRUCT_ENTRY *e)
Harald Welte0113fe72004-01-06 19:04:02 +0000241{
Harald Welteaae69be2004-08-29 23:32:14 +0000242 return (void *)e - (void *)h->entries->entrytable;
Harald Welte3ea8f402003-06-23 18:25:59 +0000243}
244
245static inline unsigned int
Harald Welteaae69be2004-08-29 23:32:14 +0000246iptcb_offset2index(const TC_HANDLE_T h, unsigned int offset)
Harald Welte3ea8f402003-06-23 18:25:59 +0000247{
Harald Welteaae69be2004-08-29 23:32:14 +0000248 return iptcb_entry2index(h, iptcb_offset2entry(h, offset));
249}
250
251/* Returns 0 if not hook entry, else hooknumber + 1 */
252static inline unsigned int
253iptcb_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 Welte3ea8f402003-06-23 18:25:59 +0000263}
264
265
Harald Welteaae69be2004-08-29 23:32:14 +0000266/**********************************************************************
267 * iptc cache utility functions (iptcc_*)
268 **********************************************************************/
Harald Welte0113fe72004-01-06 19:04:02 +0000269
Harald Welteaae69be2004-08-29 23:32:14 +0000270/* Is the given chain builtin (1) or user-defined (0) */
271static 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 */
277static 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 Josefssona5616dc2004-10-24 22:27:31 +0000291/* Get a specific rule within a chain backwards */
292static 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 Welteaae69be2004-08-29 23:32:14 +0000306/* Returns chain head if found, otherwise NULL. */
307static struct chain_head *
308iptcc_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 Welte0113fe72004-01-06 19:04:02 +0000319 }
320
Harald Welteaae69be2004-08-29 23:32:14 +0000321 return NULL;
Harald Welte0113fe72004-01-06 19:04:02 +0000322}
Harald Welteaae69be2004-08-29 23:32:14 +0000323/* Returns chain head if found, otherwise NULL. */
324static struct chain_head *
325iptcc_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 */
342static 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 Welteaae69be2004-08-29 23:32:14 +0000359/* 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 */
363static 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 Josefsson8d1b38a2004-09-22 21:00:19 +0000387 h->chain_iterator_cur->num_rules--;
Harald Welteaae69be2004-08-29 23:32:14 +0000388
389 return 1;
390 }
391 return 0;
392}
393
Harald Welteec30b6c2005-02-01 16:45:56 +0000394/* alphabetically insert a chain into the list */
395static inline void iptc_insert_chain(TC_HANDLE_T h, struct chain_head *c)
396{
397 struct chain_head *tmp;
398
399 list_for_each_entry(tmp, &h->chains, list) {
400 if (strcmp(c->name, tmp->name) <= 0) {
401 list_add(&c->list, tmp->list.prev);
402 return;
403 }
404 }
405
406 /* survived till end of list: add at tail */
407 list_add_tail(&c->list, &h->chains);
408}
409
Harald Welteaae69be2004-08-29 23:32:14 +0000410/* Another ugly helper function split out of cache_add_entry to make it less
411 * spaghetti code */
412static void __iptcc_p_add_chain(TC_HANDLE_T h, struct chain_head *c,
413 unsigned int offset, unsigned int *num)
414{
415 __iptcc_p_del_policy(h, *num);
416
417 c->head_offset = offset;
418 c->index = *num;
419
Harald Welteec30b6c2005-02-01 16:45:56 +0000420 iptc_insert_chain(h, c);
421
Harald Welteaae69be2004-08-29 23:32:14 +0000422 h->chain_iterator_cur = c;
423}
424
425/* main parser function: add an entry from the blob to the cache */
426static int cache_add_entry(STRUCT_ENTRY *e,
427 TC_HANDLE_T h,
428 STRUCT_ENTRY **prev,
429 unsigned int *num)
430{
431 unsigned int builtin;
432 unsigned int offset = (char *)e - (char *)h->entries->entrytable;
433
434 DEBUGP("entering...");
435
436 /* Last entry ("policy rule"). End it.*/
437 if (iptcb_entry2offset(h,e) + e->next_offset == h->entries->size) {
438 /* This is the ERROR node at the end of the chain */
439 DEBUGP_C("%u:%u: end of table:\n", *num, offset);
440
441 __iptcc_p_del_policy(h, *num);
442
443 h->chain_iterator_cur = NULL;
444 goto out_inc;
445 }
446
447 /* We know this is the start of a new chain if it's an ERROR
448 * target, or a hook entry point */
449
450 if (strcmp(GET_TARGET(e)->u.user.name, ERROR_TARGET) == 0) {
451 struct chain_head *c =
452 iptcc_alloc_chain_head((const char *)GET_TARGET(e)->data, 0);
453 DEBUGP_C("%u:%u:new userdefined chain %s: %p\n", *num, offset,
454 (char *)c->name, c);
455 if (!c) {
456 errno = -ENOMEM;
457 return -1;
458 }
459
460 __iptcc_p_add_chain(h, c, offset, num);
461
462 } else if ((builtin = iptcb_ent_is_hook_entry(e, h)) != 0) {
463 struct chain_head *c =
464 iptcc_alloc_chain_head((char *)hooknames[builtin-1],
465 builtin);
466 DEBUGP_C("%u:%u new builtin chain: %p (rules=%p)\n",
467 *num, offset, c, &c->rules);
468 if (!c) {
469 errno = -ENOMEM;
470 return -1;
471 }
472
473 c->hooknum = builtin;
474
475 __iptcc_p_add_chain(h, c, offset, num);
476
477 /* FIXME: this is ugly. */
478 goto new_rule;
479 } else {
480 /* has to be normal rule */
481 struct rule_head *r;
482new_rule:
483
484 if (!(r = iptcc_alloc_rule(h->chain_iterator_cur,
485 e->next_offset))) {
486 errno = ENOMEM;
487 return -1;
488 }
489 DEBUGP_C("%u:%u normal rule: %p: ", *num, offset, r);
490
491 r->index = *num;
492 r->offset = offset;
493 memcpy(r->entry, e, e->next_offset);
494 r->counter_map.maptype = COUNTER_MAP_NORMAL_MAP;
495 r->counter_map.mappos = r->index;
496
497 /* handling of jumps, etc. */
498 if (!strcmp(GET_TARGET(e)->u.user.name, STANDARD_TARGET)) {
499 STRUCT_STANDARD_TARGET *t;
500
501 t = (STRUCT_STANDARD_TARGET *)GET_TARGET(e);
502 if (t->target.u.target_size
503 != ALIGN(sizeof(STRUCT_STANDARD_TARGET))) {
504 errno = EINVAL;
505 return -1;
506 }
507
508 if (t->verdict < 0) {
509 DEBUGP_C("standard, verdict=%d\n", t->verdict);
510 r->type = IPTCC_R_STANDARD;
511 } else if (t->verdict == r->offset+e->next_offset) {
512 DEBUGP_C("fallthrough\n");
513 r->type = IPTCC_R_FALLTHROUGH;
514 } else {
515 DEBUGP_C("jump, target=%u\n", t->verdict);
516 r->type = IPTCC_R_JUMP;
517 /* Jump target fixup has to be deferred
518 * until second pass, since we migh not
519 * yet have parsed the target */
520 }
Martin Josefsson52c38022004-09-22 19:39:40 +0000521 } else {
522 DEBUGP_C("module, target=%s\n", GET_TARGET(e)->u.user.name);
523 r->type = IPTCC_R_MODULE;
Harald Welteaae69be2004-08-29 23:32:14 +0000524 }
525
526 list_add_tail(&r->list, &h->chain_iterator_cur->rules);
Martin Josefsson8d1b38a2004-09-22 21:00:19 +0000527 h->chain_iterator_cur->num_rules++;
Harald Welteaae69be2004-08-29 23:32:14 +0000528 }
529out_inc:
530 (*num)++;
531 return 0;
532}
533
534
535/* parse an iptables blob into it's pieces */
536static int parse_table(TC_HANDLE_T h)
537{
538 STRUCT_ENTRY *prev;
539 unsigned int num = 0;
540 struct chain_head *c;
541
542 /* First pass: over ruleset blob */
543 ENTRY_ITERATE(h->entries->entrytable, h->entries->size,
544 cache_add_entry, h, &prev, &num);
545
546 /* Second pass: fixup parsed data from first pass */
547 list_for_each_entry(c, &h->chains, list) {
548 struct rule_head *r;
549 list_for_each_entry(r, &c->rules, list) {
550 struct chain_head *c;
551 STRUCT_STANDARD_TARGET *t;
552
553 if (r->type != IPTCC_R_JUMP)
554 continue;
555
556 t = (STRUCT_STANDARD_TARGET *)GET_TARGET(r->entry);
557 c = iptcc_find_chain_by_offset(h, t->verdict);
558 if (!c)
559 return -1;
560 r->jump = c;
561 c->references++;
562 }
563 }
564
565 /* FIXME: sort chains */
566
567 return 1;
568}
569
570
571/**********************************************************************
572 * RULESET COMPILATION (cache -> blob)
573 **********************************************************************/
574
575/* Convenience structures */
576struct iptcb_chain_start{
577 STRUCT_ENTRY e;
578 struct ipt_error_target name;
579};
580#define IPTCB_CHAIN_START_SIZE (sizeof(STRUCT_ENTRY) + \
581 ALIGN(sizeof(struct ipt_error_target)))
582
583struct iptcb_chain_foot {
584 STRUCT_ENTRY e;
585 STRUCT_STANDARD_TARGET target;
586};
587#define IPTCB_CHAIN_FOOT_SIZE (sizeof(STRUCT_ENTRY) + \
588 ALIGN(sizeof(STRUCT_STANDARD_TARGET)))
589
590struct iptcb_chain_error {
591 STRUCT_ENTRY entry;
592 struct ipt_error_target target;
593};
594#define IPTCB_CHAIN_ERROR_SIZE (sizeof(STRUCT_ENTRY) + \
595 ALIGN(sizeof(struct ipt_error_target)))
596
597
598
599/* compile rule from cache into blob */
600static inline int iptcc_compile_rule (TC_HANDLE_T h, STRUCT_REPLACE *repl, struct rule_head *r)
601{
602 /* handle jumps */
603 if (r->type == IPTCC_R_JUMP) {
604 STRUCT_STANDARD_TARGET *t;
605 t = (STRUCT_STANDARD_TARGET *)GET_TARGET(r->entry);
606 /* memset for memcmp convenience on delete/replace */
607 memset(t->target.u.user.name, 0, FUNCTION_MAXNAMELEN);
608 strcpy(t->target.u.user.name, STANDARD_TARGET);
609 /* Jumps can only happen to builtin chains, so we
610 * can safely assume that they always have a header */
611 t->verdict = r->jump->head_offset + IPTCB_CHAIN_START_SIZE;
612 } else if (r->type == IPTCC_R_FALLTHROUGH) {
613 STRUCT_STANDARD_TARGET *t;
614 t = (STRUCT_STANDARD_TARGET *)GET_TARGET(r->entry);
615 t->verdict = r->offset + r->size;
616 }
617
618 /* copy entry from cache to blob */
619 memcpy((char *)repl->entries+r->offset, r->entry, r->size);
620
621 return 1;
622}
623
624/* compile chain from cache into blob */
625static int iptcc_compile_chain(TC_HANDLE_T h, STRUCT_REPLACE *repl, struct chain_head *c)
626{
627 int ret;
628 struct rule_head *r;
629 struct iptcb_chain_start *head;
630 struct iptcb_chain_foot *foot;
631
632 /* only user-defined chains have heaer */
633 if (!iptcc_is_builtin(c)) {
634 /* put chain header in place */
635 head = (void *)repl->entries + c->head_offset;
636 head->e.target_offset = sizeof(STRUCT_ENTRY);
637 head->e.next_offset = IPTCB_CHAIN_START_SIZE;
638 strcpy(head->name.t.u.user.name, ERROR_TARGET);
639 head->name.t.u.target_size =
640 ALIGN(sizeof(struct ipt_error_target));
641 strcpy(head->name.error, c->name);
642 } else {
643 repl->hook_entry[c->hooknum-1] = c->head_offset;
644 repl->underflow[c->hooknum-1] = c->foot_offset;
645 }
646
647 /* iterate over rules */
648 list_for_each_entry(r, &c->rules, list) {
649 ret = iptcc_compile_rule(h, repl, r);
650 if (ret < 0)
651 return ret;
652 }
653
654 /* put chain footer in place */
655 foot = (void *)repl->entries + c->foot_offset;
656 foot->e.target_offset = sizeof(STRUCT_ENTRY);
657 foot->e.next_offset = IPTCB_CHAIN_FOOT_SIZE;
658 strcpy(foot->target.target.u.user.name, STANDARD_TARGET);
659 foot->target.target.u.target_size =
660 ALIGN(sizeof(STRUCT_STANDARD_TARGET));
661 /* builtin targets have verdict, others return */
662 if (iptcc_is_builtin(c))
663 foot->target.verdict = c->verdict;
664 else
665 foot->target.verdict = RETURN;
666 /* set policy-counters */
667 memcpy(&foot->e.counters, &c->counters, sizeof(STRUCT_COUNTERS));
668
669 return 0;
670}
671
672/* calculate offset and number for every rule in the cache */
673static int iptcc_compile_chain_offsets(TC_HANDLE_T h, struct chain_head *c,
674 int *offset, int *num)
675{
676 struct rule_head *r;
677
678 c->head_offset = *offset;
679 DEBUGP("%s: chain_head %u, offset=%u\n", c->name, *num, *offset);
680
681 if (!iptcc_is_builtin(c)) {
682 /* Chain has header */
683 *offset += sizeof(STRUCT_ENTRY)
684 + ALIGN(sizeof(struct ipt_error_target));
685 (*num)++;
686 }
687
688 list_for_each_entry(r, &c->rules, list) {
689 DEBUGP("rule %u, offset=%u, index=%u\n", *num, *offset, *num);
690 r->offset = *offset;
691 r->index = *num;
692 *offset += r->size;
693 (*num)++;
694 }
695
696 DEBUGP("%s; chain_foot %u, offset=%u, index=%u\n", c->name, *num,
697 *offset, *num);
698 c->foot_offset = *offset;
699 c->foot_index = *num;
700 *offset += sizeof(STRUCT_ENTRY)
701 + ALIGN(sizeof(STRUCT_STANDARD_TARGET));
702 (*num)++;
703
704 return 1;
705}
706
707/* put the pieces back together again */
708static int iptcc_compile_table_prep(TC_HANDLE_T h, unsigned int *size)
709{
710 struct chain_head *c;
711 unsigned int offset = 0, num = 0;
712 int ret = 0;
713
714 /* First pass: calculate offset for every rule */
715 list_for_each_entry(c, &h->chains, list) {
716 ret = iptcc_compile_chain_offsets(h, c, &offset, &num);
717 if (ret < 0)
718 return ret;
719 }
720
721 /* Append one error rule at end of chain */
722 num++;
723 offset += sizeof(STRUCT_ENTRY)
724 + ALIGN(sizeof(struct ipt_error_target));
725
726 /* ruleset size is now in offset */
727 *size = offset;
728 return num;
729}
730
731static int iptcc_compile_table(TC_HANDLE_T h, STRUCT_REPLACE *repl)
732{
733 struct chain_head *c;
734 struct iptcb_chain_error *error;
735
736 /* Second pass: copy from cache to offsets, fill in jumps */
737 list_for_each_entry(c, &h->chains, list) {
738 int ret = iptcc_compile_chain(h, repl, c);
739 if (ret < 0)
740 return ret;
741 }
742
743 /* Append error rule at end of chain */
744 error = (void *)repl->entries + repl->size - IPTCB_CHAIN_ERROR_SIZE;
745 error->entry.target_offset = sizeof(STRUCT_ENTRY);
746 error->entry.next_offset = IPTCB_CHAIN_ERROR_SIZE;
747 error->target.t.u.user.target_size =
748 ALIGN(sizeof(struct ipt_error_target));
749 strcpy((char *)&error->target.t.u.user.name, ERROR_TARGET);
750 strcpy((char *)&error->target.error, "ERROR");
751
752 return 1;
753}
754
755/**********************************************************************
756 * EXTERNAL API (operates on cache only)
757 **********************************************************************/
Marc Bouchere6869a82000-03-20 06:03:29 +0000758
759/* Allocate handle of given size */
Rusty Russell79dee072000-05-02 16:45:16 +0000760static TC_HANDLE_T
Harald Welte0113fe72004-01-06 19:04:02 +0000761alloc_handle(const char *tablename, unsigned int size, unsigned int num_rules)
Marc Bouchere6869a82000-03-20 06:03:29 +0000762{
763 size_t len;
Rusty Russell79dee072000-05-02 16:45:16 +0000764 TC_HANDLE_T h;
Marc Bouchere6869a82000-03-20 06:03:29 +0000765
Harald Welteaae69be2004-08-29 23:32:14 +0000766 len = sizeof(STRUCT_TC_HANDLE) + size;
Marc Bouchere6869a82000-03-20 06:03:29 +0000767
Harald Welteaae69be2004-08-29 23:32:14 +0000768 h = malloc(sizeof(STRUCT_TC_HANDLE));
769 if (!h) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000770 errno = ENOMEM;
771 return NULL;
772 }
Harald Welteaae69be2004-08-29 23:32:14 +0000773 memset(h, 0, sizeof(*h));
774 INIT_LIST_HEAD(&h->chains);
Marc Bouchere6869a82000-03-20 06:03:29 +0000775 strcpy(h->info.name, tablename);
Harald Welteaae69be2004-08-29 23:32:14 +0000776
Harald Welte0371c0c2004-09-19 21:00:12 +0000777 h->entries = malloc(sizeof(STRUCT_GET_ENTRIES) + size);
Harald Welteaae69be2004-08-29 23:32:14 +0000778 if (!h->entries)
779 goto out_free_handle;
780
781 strcpy(h->entries->name, tablename);
Harald Welte0371c0c2004-09-19 21:00:12 +0000782 h->entries->size = size;
Marc Bouchere6869a82000-03-20 06:03:29 +0000783
784 return h;
Harald Welteaae69be2004-08-29 23:32:14 +0000785
786out_free_handle:
787 free(h);
788
789 return NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +0000790}
791
Harald Welteaae69be2004-08-29 23:32:14 +0000792
Rusty Russell79dee072000-05-02 16:45:16 +0000793TC_HANDLE_T
794TC_INIT(const char *tablename)
Marc Bouchere6869a82000-03-20 06:03:29 +0000795{
Rusty Russell79dee072000-05-02 16:45:16 +0000796 TC_HANDLE_T h;
797 STRUCT_GETINFO info;
Marc Bouchere6869a82000-03-20 06:03:29 +0000798 int tmp;
799 socklen_t s;
800
Rusty Russell79dee072000-05-02 16:45:16 +0000801 iptc_fn = TC_INIT;
Marc Bouchere6869a82000-03-20 06:03:29 +0000802
Martin Josefsson841e4ae2003-05-02 15:30:11 +0000803 if (strlen(tablename) >= TABLE_MAXNAMELEN) {
804 errno = EINVAL;
805 return NULL;
806 }
807
Derrik Pates664c0a32005-02-01 13:28:14 +0000808 if (sockfd_use == 0) {
809 sockfd = socket(TC_AF, SOCK_RAW, IPPROTO_RAW);
810 if (sockfd < 0)
811 return NULL;
812 }
813 sockfd_use++;
Marc Bouchere6869a82000-03-20 06:03:29 +0000814
815 s = sizeof(info);
Martin Josefsson841e4ae2003-05-02 15:30:11 +0000816
Marc Bouchere6869a82000-03-20 06:03:29 +0000817 strcpy(info.name, tablename);
Derrik Pates664c0a32005-02-01 13:28:14 +0000818 if (getsockopt(sockfd, TC_IPPROTO, SO_GET_INFO, &info, &s) < 0) {
819 if (--sockfd_use == 0) {
820 close(sockfd);
821 sockfd = -1;
822 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000823 return NULL;
Derrik Pates664c0a32005-02-01 13:28:14 +0000824 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000825
Harald Welteaae69be2004-08-29 23:32:14 +0000826 DEBUGP("valid_hooks=0x%08x, num_entries=%u, size=%u\n",
827 info.valid_hooks, info.num_entries, info.size);
828
Harald Welte0113fe72004-01-06 19:04:02 +0000829 if ((h = alloc_handle(info.name, info.size, info.num_entries))
Martin Josefsson841e4ae2003-05-02 15:30:11 +0000830 == NULL) {
Derrik Pates664c0a32005-02-01 13:28:14 +0000831 if (--sockfd_use == 0) {
832 close(sockfd);
833 sockfd = -1;
834 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000835 return NULL;
Martin Josefsson841e4ae2003-05-02 15:30:11 +0000836 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000837
Marc Bouchere6869a82000-03-20 06:03:29 +0000838 /* Initialize current state */
839 h->info = info;
Harald Welte0113fe72004-01-06 19:04:02 +0000840
Harald Welteaae69be2004-08-29 23:32:14 +0000841 h->entries->size = h->info.size;
Marc Bouchere6869a82000-03-20 06:03:29 +0000842
Rusty Russell79dee072000-05-02 16:45:16 +0000843 tmp = sizeof(STRUCT_GET_ENTRIES) + h->info.size;
Marc Bouchere6869a82000-03-20 06:03:29 +0000844
Harald Welteaae69be2004-08-29 23:32:14 +0000845 if (getsockopt(sockfd, TC_IPPROTO, SO_GET_ENTRIES, h->entries,
846 &tmp) < 0)
847 goto error;
848
849#ifdef IPTC_DEBUG2
850 {
851 int fd = open("/tmp/libiptc-so_get_entries.blob",
852 O_CREAT|O_WRONLY);
853 if (fd >= 0) {
854 write(fd, h->entries, tmp);
855 close(fd);
856 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000857 }
Harald Welteaae69be2004-08-29 23:32:14 +0000858#endif
859
860 if (parse_table(h) < 0)
861 goto error;
Rusty Russell7e53bf92000-03-20 07:03:28 +0000862
Marc Bouchere6869a82000-03-20 06:03:29 +0000863 CHECK(h);
864 return h;
Harald Welteaae69be2004-08-29 23:32:14 +0000865error:
Derrik Pates664c0a32005-02-01 13:28:14 +0000866 if (--sockfd_use == 0) {
867 close(sockfd);
868 sockfd = -1;
869 }
Harald Welteaae69be2004-08-29 23:32:14 +0000870 TC_FREE(&h);
871 return NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +0000872}
873
Martin Josefsson841e4ae2003-05-02 15:30:11 +0000874void
875TC_FREE(TC_HANDLE_T *h)
876{
Harald Welteaae69be2004-08-29 23:32:14 +0000877 struct chain_head *c, *tmp;
878
Derrik Pates664c0a32005-02-01 13:28:14 +0000879 iptc_fn = TC_FREE;
880 if (--sockfd_use == 0) {
881 close(sockfd);
882 sockfd = -1;
883 }
Harald Welteaae69be2004-08-29 23:32:14 +0000884
885 list_for_each_entry_safe(c, tmp, &(*h)->chains, list) {
886 struct rule_head *r, *rtmp;
887
888 list_for_each_entry_safe(r, rtmp, &c->rules, list) {
889 free(r);
890 }
891
892 free(c);
893 }
894
895 free((*h)->entries);
Martin Josefsson841e4ae2003-05-02 15:30:11 +0000896 free(*h);
Harald Welteaae69be2004-08-29 23:32:14 +0000897
Martin Josefsson841e4ae2003-05-02 15:30:11 +0000898 *h = NULL;
899}
900
Marc Bouchere6869a82000-03-20 06:03:29 +0000901static inline int
Rusty Russell79dee072000-05-02 16:45:16 +0000902print_match(const STRUCT_ENTRY_MATCH *m)
Marc Bouchere6869a82000-03-20 06:03:29 +0000903{
Rusty Russell228e98d2000-04-27 10:28:06 +0000904 printf("Match name: `%s'\n", m->u.user.name);
Marc Bouchere6869a82000-03-20 06:03:29 +0000905 return 0;
906}
907
Rusty Russell79dee072000-05-02 16:45:16 +0000908static int dump_entry(STRUCT_ENTRY *e, const TC_HANDLE_T handle);
909
Marc Bouchere6869a82000-03-20 06:03:29 +0000910void
Rusty Russell79dee072000-05-02 16:45:16 +0000911TC_DUMP_ENTRIES(const TC_HANDLE_T handle)
Marc Bouchere6869a82000-03-20 06:03:29 +0000912{
Derrik Pates664c0a32005-02-01 13:28:14 +0000913 iptc_fn = TC_DUMP_ENTRIES;
Marc Bouchere6869a82000-03-20 06:03:29 +0000914 CHECK(handle);
Harald Welteaae69be2004-08-29 23:32:14 +0000915#if 0
Rusty Russelle45c7132004-12-16 13:21:44 +0000916 printf("libiptc v%s. %u bytes.\n",
917 IPTABLES_VERSION, handle->entries->size);
Marc Bouchere6869a82000-03-20 06:03:29 +0000918 printf("Table `%s'\n", handle->info.name);
919 printf("Hooks: pre/in/fwd/out/post = %u/%u/%u/%u/%u\n",
Rusty Russell67088e72000-05-10 01:18:57 +0000920 handle->info.hook_entry[HOOK_PRE_ROUTING],
921 handle->info.hook_entry[HOOK_LOCAL_IN],
922 handle->info.hook_entry[HOOK_FORWARD],
923 handle->info.hook_entry[HOOK_LOCAL_OUT],
924 handle->info.hook_entry[HOOK_POST_ROUTING]);
Marc Bouchere6869a82000-03-20 06:03:29 +0000925 printf("Underflows: pre/in/fwd/out/post = %u/%u/%u/%u/%u\n",
Rusty Russell67088e72000-05-10 01:18:57 +0000926 handle->info.underflow[HOOK_PRE_ROUTING],
927 handle->info.underflow[HOOK_LOCAL_IN],
928 handle->info.underflow[HOOK_FORWARD],
929 handle->info.underflow[HOOK_LOCAL_OUT],
930 handle->info.underflow[HOOK_POST_ROUTING]);
Marc Bouchere6869a82000-03-20 06:03:29 +0000931
Harald Welteaae69be2004-08-29 23:32:14 +0000932 ENTRY_ITERATE(handle->entries->entrytable, handle->entries->size,
Rusty Russell79dee072000-05-02 16:45:16 +0000933 dump_entry, handle);
Harald Welteaae69be2004-08-29 23:32:14 +0000934#endif
Harald Welte0113fe72004-01-06 19:04:02 +0000935}
Rusty Russell30fd6e52000-04-23 09:16:06 +0000936
Marc Bouchere6869a82000-03-20 06:03:29 +0000937/* Does this chain exist? */
Rusty Russell79dee072000-05-02 16:45:16 +0000938int TC_IS_CHAIN(const char *chain, const TC_HANDLE_T handle)
Marc Bouchere6869a82000-03-20 06:03:29 +0000939{
Derrik Pates664c0a32005-02-01 13:28:14 +0000940 iptc_fn = TC_IS_CHAIN;
Harald Welteaae69be2004-08-29 23:32:14 +0000941 return iptcc_find_label(chain, handle) != NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +0000942}
943
Harald Welteaae69be2004-08-29 23:32:14 +0000944static void iptcc_chain_iterator_advance(TC_HANDLE_T handle)
945{
946 struct chain_head *c = handle->chain_iterator_cur;
947
948 if (c->list.next == &handle->chains)
949 handle->chain_iterator_cur = NULL;
950 else
951 handle->chain_iterator_cur =
952 list_entry(c->list.next, struct chain_head, list);
953}
Marc Bouchere6869a82000-03-20 06:03:29 +0000954
Rusty Russell30fd6e52000-04-23 09:16:06 +0000955/* Iterator functions to run through the chains. */
Marc Bouchere6869a82000-03-20 06:03:29 +0000956const char *
Philip Blundell8c700902000-05-15 02:17:52 +0000957TC_FIRST_CHAIN(TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +0000958{
Harald Welteaae69be2004-08-29 23:32:14 +0000959 struct chain_head *c = list_entry((*handle)->chains.next,
960 struct chain_head, list);
961
962 iptc_fn = TC_FIRST_CHAIN;
963
964
965 if (list_empty(&(*handle)->chains)) {
966 DEBUGP(": no chains\n");
Harald Welte0113fe72004-01-06 19:04:02 +0000967 return NULL;
Harald Welteaae69be2004-08-29 23:32:14 +0000968 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000969
Harald Welteaae69be2004-08-29 23:32:14 +0000970 (*handle)->chain_iterator_cur = c;
971 iptcc_chain_iterator_advance(*handle);
Harald Welte0113fe72004-01-06 19:04:02 +0000972
Harald Welteaae69be2004-08-29 23:32:14 +0000973 DEBUGP(": returning `%s'\n", c->name);
974 return c->name;
Marc Bouchere6869a82000-03-20 06:03:29 +0000975}
976
Rusty Russell30fd6e52000-04-23 09:16:06 +0000977/* Iterator functions to run through the chains. Returns NULL at end. */
978const char *
Rusty Russell79dee072000-05-02 16:45:16 +0000979TC_NEXT_CHAIN(TC_HANDLE_T *handle)
Rusty Russell30fd6e52000-04-23 09:16:06 +0000980{
Harald Welteaae69be2004-08-29 23:32:14 +0000981 struct chain_head *c = (*handle)->chain_iterator_cur;
Rusty Russell30fd6e52000-04-23 09:16:06 +0000982
Harald Welteaae69be2004-08-29 23:32:14 +0000983 iptc_fn = TC_NEXT_CHAIN;
984
985 if (!c) {
986 DEBUGP(": no more chains\n");
Rusty Russell30fd6e52000-04-23 09:16:06 +0000987 return NULL;
Harald Welteaae69be2004-08-29 23:32:14 +0000988 }
Rusty Russell30fd6e52000-04-23 09:16:06 +0000989
Harald Welteaae69be2004-08-29 23:32:14 +0000990 iptcc_chain_iterator_advance(*handle);
991
992 DEBUGP(": returning `%s'\n", c->name);
993 return c->name;
Rusty Russell30fd6e52000-04-23 09:16:06 +0000994}
995
996/* Get first rule in the given chain: NULL for empty chain. */
Rusty Russell79dee072000-05-02 16:45:16 +0000997const STRUCT_ENTRY *
Philip Blundell8c700902000-05-15 02:17:52 +0000998TC_FIRST_RULE(const char *chain, TC_HANDLE_T *handle)
Rusty Russell30fd6e52000-04-23 09:16:06 +0000999{
Harald Welteaae69be2004-08-29 23:32:14 +00001000 struct chain_head *c;
1001 struct rule_head *r;
Rusty Russell30fd6e52000-04-23 09:16:06 +00001002
Harald Welteaae69be2004-08-29 23:32:14 +00001003 iptc_fn = TC_FIRST_RULE;
1004
1005 DEBUGP("first rule(%s): ", chain);
1006
1007 c = iptcc_find_label(chain, *handle);
Rusty Russell30fd6e52000-04-23 09:16:06 +00001008 if (!c) {
1009 errno = ENOENT;
1010 return NULL;
1011 }
1012
1013 /* Empty chain: single return/policy rule */
Harald Welteaae69be2004-08-29 23:32:14 +00001014 if (list_empty(&c->rules)) {
1015 DEBUGP_C("no rules, returning NULL\n");
Rusty Russell30fd6e52000-04-23 09:16:06 +00001016 return NULL;
Harald Welteaae69be2004-08-29 23:32:14 +00001017 }
Rusty Russell30fd6e52000-04-23 09:16:06 +00001018
Harald Welteaae69be2004-08-29 23:32:14 +00001019 r = list_entry(c->rules.next, struct rule_head, list);
1020 (*handle)->rule_iterator_cur = r;
1021 DEBUGP_C("%p\n", r);
1022
1023 return r->entry;
Rusty Russell30fd6e52000-04-23 09:16:06 +00001024}
1025
1026/* Returns NULL when rules run out. */
Rusty Russell79dee072000-05-02 16:45:16 +00001027const STRUCT_ENTRY *
Philip Blundell8c700902000-05-15 02:17:52 +00001028TC_NEXT_RULE(const STRUCT_ENTRY *prev, TC_HANDLE_T *handle)
Rusty Russell30fd6e52000-04-23 09:16:06 +00001029{
Harald Welteaae69be2004-08-29 23:32:14 +00001030 struct rule_head *r;
Rusty Russell30fd6e52000-04-23 09:16:06 +00001031
Derrik Pates664c0a32005-02-01 13:28:14 +00001032 iptc_fn = TC_NEXT_RULE;
Harald Welteaae69be2004-08-29 23:32:14 +00001033 DEBUGP("rule_iterator_cur=%p...", (*handle)->rule_iterator_cur);
1034
1035 if (!(*handle)->rule_iterator_cur) {
1036 DEBUGP_C("returning NULL\n");
1037 return NULL;
1038 }
1039
1040 r = list_entry((*handle)->rule_iterator_cur->list.next,
1041 struct rule_head, list);
1042
1043 iptc_fn = TC_NEXT_RULE;
1044
1045 DEBUGP_C("next=%p, head=%p...", &r->list,
1046 &(*handle)->rule_iterator_cur->chain->rules);
1047
1048 if (&r->list == &(*handle)->rule_iterator_cur->chain->rules) {
1049 (*handle)->rule_iterator_cur = NULL;
1050 DEBUGP_C("finished, returning NULL\n");
1051 return NULL;
1052 }
1053
1054 (*handle)->rule_iterator_cur = r;
1055
1056 /* NOTE: prev is without any influence ! */
1057 DEBUGP_C("returning rule %p\n", r);
1058 return r->entry;
Rusty Russell30fd6e52000-04-23 09:16:06 +00001059}
1060
Marc Bouchere6869a82000-03-20 06:03:29 +00001061/* How many rules in this chain? */
1062unsigned int
Rusty Russell79dee072000-05-02 16:45:16 +00001063TC_NUM_RULES(const char *chain, TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001064{
Harald Welteaae69be2004-08-29 23:32:14 +00001065 struct chain_head *c;
1066 iptc_fn = TC_NUM_RULES;
Marc Bouchere6869a82000-03-20 06:03:29 +00001067 CHECK(*handle);
Harald Welteaae69be2004-08-29 23:32:14 +00001068
1069 c = iptcc_find_label(chain, *handle);
1070 if (!c) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001071 errno = ENOENT;
1072 return (unsigned int)-1;
1073 }
Harald Welteaae69be2004-08-29 23:32:14 +00001074
1075 return c->num_rules;
Marc Bouchere6869a82000-03-20 06:03:29 +00001076}
1077
Rusty Russell79dee072000-05-02 16:45:16 +00001078const STRUCT_ENTRY *TC_GET_RULE(const char *chain,
1079 unsigned int n,
1080 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001081{
Harald Welteaae69be2004-08-29 23:32:14 +00001082 struct chain_head *c;
1083 struct rule_head *r;
1084
1085 iptc_fn = TC_GET_RULE;
Marc Bouchere6869a82000-03-20 06:03:29 +00001086
1087 CHECK(*handle);
Harald Welteaae69be2004-08-29 23:32:14 +00001088
1089 c = iptcc_find_label(chain, *handle);
1090 if (!c) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001091 errno = ENOENT;
1092 return NULL;
1093 }
1094
Harald Welteaae69be2004-08-29 23:32:14 +00001095 r = iptcc_get_rule_num(c, n);
1096 if (!r)
1097 return NULL;
1098 return r->entry;
Marc Bouchere6869a82000-03-20 06:03:29 +00001099}
1100
1101/* Returns a pointer to the target name of this position. */
Harald Welteaae69be2004-08-29 23:32:14 +00001102const char *standard_target_map(int verdict)
Marc Bouchere6869a82000-03-20 06:03:29 +00001103{
Harald Welteaae69be2004-08-29 23:32:14 +00001104 switch (verdict) {
1105 case RETURN:
1106 return LABEL_RETURN;
1107 break;
1108 case -NF_ACCEPT-1:
1109 return LABEL_ACCEPT;
1110 break;
1111 case -NF_DROP-1:
1112 return LABEL_DROP;
1113 break;
1114 case -NF_QUEUE-1:
1115 return LABEL_QUEUE;
1116 break;
1117 default:
1118 fprintf(stderr, "ERROR: %d not a valid target)\n",
1119 verdict);
1120 abort();
1121 break;
1122 }
1123 /* not reached */
1124 return NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +00001125}
1126
Harald Welteaae69be2004-08-29 23:32:14 +00001127/* Returns a pointer to the target name of this position. */
1128const char *TC_GET_TARGET(const STRUCT_ENTRY *ce,
1129 TC_HANDLE_T *handle)
1130{
1131 STRUCT_ENTRY *e = (STRUCT_ENTRY *)ce;
Rusty Russelle45c7132004-12-16 13:21:44 +00001132 struct rule_head *r = container_of(e, struct rule_head, entry[0]);
Harald Welteaae69be2004-08-29 23:32:14 +00001133
1134 iptc_fn = TC_GET_TARGET;
1135
1136 switch(r->type) {
1137 int spos;
1138 case IPTCC_R_FALLTHROUGH:
1139 return "";
1140 break;
1141 case IPTCC_R_JUMP:
1142 DEBUGP("r=%p, jump=%p, name=`%s'\n", r, r->jump, r->jump->name);
1143 return r->jump->name;
1144 break;
1145 case IPTCC_R_STANDARD:
1146 spos = *(int *)GET_TARGET(e)->data;
1147 DEBUGP("r=%p, spos=%d'\n", r, spos);
1148 return standard_target_map(spos);
1149 break;
1150 case IPTCC_R_MODULE:
1151 return GET_TARGET(e)->u.user.name;
1152 break;
1153 }
1154 return NULL;
1155}
Marc Bouchere6869a82000-03-20 06:03:29 +00001156/* Is this a built-in chain? Actually returns hook + 1. */
1157int
Rusty Russell79dee072000-05-02 16:45:16 +00001158TC_BUILTIN(const char *chain, const TC_HANDLE_T handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001159{
Harald Welteaae69be2004-08-29 23:32:14 +00001160 struct chain_head *c;
1161
1162 iptc_fn = TC_BUILTIN;
Marc Bouchere6869a82000-03-20 06:03:29 +00001163
Harald Welteaae69be2004-08-29 23:32:14 +00001164 c = iptcc_find_label(chain, handle);
1165 if (!c) {
1166 errno = ENOENT;
Martin Josefssonb0f3d2d2004-09-23 18:23:20 +00001167 return 0;
Marc Bouchere6869a82000-03-20 06:03:29 +00001168 }
Harald Welteaae69be2004-08-29 23:32:14 +00001169
1170 return iptcc_is_builtin(c);
Marc Bouchere6869a82000-03-20 06:03:29 +00001171}
1172
1173/* Get the policy of a given built-in chain */
1174const char *
Rusty Russell79dee072000-05-02 16:45:16 +00001175TC_GET_POLICY(const char *chain,
1176 STRUCT_COUNTERS *counters,
1177 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001178{
Harald Welteaae69be2004-08-29 23:32:14 +00001179 struct chain_head *c;
Marc Bouchere6869a82000-03-20 06:03:29 +00001180
Harald Welteaae69be2004-08-29 23:32:14 +00001181 iptc_fn = TC_GET_POLICY;
1182
1183 DEBUGP("called for chain %s\n", chain);
1184
1185 c = iptcc_find_label(chain, *handle);
1186 if (!c) {
1187 errno = ENOENT;
1188 return NULL;
1189 }
1190
1191 if (!iptcc_is_builtin(c))
Marc Bouchere6869a82000-03-20 06:03:29 +00001192 return NULL;
1193
Harald Welteaae69be2004-08-29 23:32:14 +00001194 *counters = c->counters;
Marc Bouchere6869a82000-03-20 06:03:29 +00001195
Harald Welteaae69be2004-08-29 23:32:14 +00001196 return standard_target_map(c->verdict);
Harald Welte0113fe72004-01-06 19:04:02 +00001197}
1198
1199static int
Harald Welteaae69be2004-08-29 23:32:14 +00001200iptcc_standard_map(struct rule_head *r, int verdict)
Harald Welte0113fe72004-01-06 19:04:02 +00001201{
Harald Welteaae69be2004-08-29 23:32:14 +00001202 STRUCT_ENTRY *e = r->entry;
Rusty Russell79dee072000-05-02 16:45:16 +00001203 STRUCT_STANDARD_TARGET *t;
Marc Bouchere6869a82000-03-20 06:03:29 +00001204
Rusty Russell79dee072000-05-02 16:45:16 +00001205 t = (STRUCT_STANDARD_TARGET *)GET_TARGET(e);
Marc Bouchere6869a82000-03-20 06:03:29 +00001206
Rusty Russell67088e72000-05-10 01:18:57 +00001207 if (t->target.u.target_size
Philip Blundell8c700902000-05-15 02:17:52 +00001208 != ALIGN(sizeof(STRUCT_STANDARD_TARGET))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001209 errno = EINVAL;
1210 return 0;
1211 }
1212 /* memset for memcmp convenience on delete/replace */
Rusty Russell79dee072000-05-02 16:45:16 +00001213 memset(t->target.u.user.name, 0, FUNCTION_MAXNAMELEN);
1214 strcpy(t->target.u.user.name, STANDARD_TARGET);
Marc Bouchere6869a82000-03-20 06:03:29 +00001215 t->verdict = verdict;
1216
Harald Welteaae69be2004-08-29 23:32:14 +00001217 r->type = IPTCC_R_STANDARD;
1218
Marc Bouchere6869a82000-03-20 06:03:29 +00001219 return 1;
1220}
Rusty Russell7e53bf92000-03-20 07:03:28 +00001221
Marc Bouchere6869a82000-03-20 06:03:29 +00001222static int
Harald Welteaae69be2004-08-29 23:32:14 +00001223iptcc_map_target(const TC_HANDLE_T handle,
1224 struct rule_head *r)
Marc Bouchere6869a82000-03-20 06:03:29 +00001225{
Harald Welteaae69be2004-08-29 23:32:14 +00001226 STRUCT_ENTRY *e = r->entry;
Harald Welte0113fe72004-01-06 19:04:02 +00001227 STRUCT_ENTRY_TARGET *t = GET_TARGET(e);
Marc Bouchere6869a82000-03-20 06:03:29 +00001228
Marc Bouchere6869a82000-03-20 06:03:29 +00001229 /* Maybe it's empty (=> fall through) */
Harald Welteaae69be2004-08-29 23:32:14 +00001230 if (strcmp(t->u.user.name, "") == 0) {
1231 r->type = IPTCC_R_FALLTHROUGH;
1232 return 1;
1233 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001234 /* Maybe it's a standard target name... */
Rusty Russell79dee072000-05-02 16:45:16 +00001235 else if (strcmp(t->u.user.name, LABEL_ACCEPT) == 0)
Harald Welteaae69be2004-08-29 23:32:14 +00001236 return iptcc_standard_map(r, -NF_ACCEPT - 1);
Rusty Russell79dee072000-05-02 16:45:16 +00001237 else if (strcmp(t->u.user.name, LABEL_DROP) == 0)
Harald Welteaae69be2004-08-29 23:32:14 +00001238 return iptcc_standard_map(r, -NF_DROP - 1);
Rusty Russell67088e72000-05-10 01:18:57 +00001239 else if (strcmp(t->u.user.name, LABEL_QUEUE) == 0)
Harald Welteaae69be2004-08-29 23:32:14 +00001240 return iptcc_standard_map(r, -NF_QUEUE - 1);
Rusty Russell79dee072000-05-02 16:45:16 +00001241 else if (strcmp(t->u.user.name, LABEL_RETURN) == 0)
Harald Welteaae69be2004-08-29 23:32:14 +00001242 return iptcc_standard_map(r, RETURN);
Rusty Russell79dee072000-05-02 16:45:16 +00001243 else if (TC_BUILTIN(t->u.user.name, handle)) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001244 /* Can't jump to builtins. */
1245 errno = EINVAL;
1246 return 0;
1247 } else {
1248 /* Maybe it's an existing chain name. */
Harald Welteaae69be2004-08-29 23:32:14 +00001249 struct chain_head *c;
1250 DEBUGP("trying to find chain `%s': ", t->u.user.name);
Marc Bouchere6869a82000-03-20 06:03:29 +00001251
Harald Welteaae69be2004-08-29 23:32:14 +00001252 c = iptcc_find_label(t->u.user.name, handle);
1253 if (c) {
1254 DEBUGP_C("found!\n");
1255 r->type = IPTCC_R_JUMP;
1256 r->jump = c;
1257 c->references++;
1258 return 1;
1259 }
1260 DEBUGP_C("not found :(\n");
Marc Bouchere6869a82000-03-20 06:03:29 +00001261 }
1262
1263 /* Must be a module? If not, kernel will reject... */
Rusty Russell3aef54d2005-01-03 03:48:40 +00001264 /* memset to all 0 for your memcmp convenience: don't clear version */
Rusty Russell228e98d2000-04-27 10:28:06 +00001265 memset(t->u.user.name + strlen(t->u.user.name),
Marc Bouchere6869a82000-03-20 06:03:29 +00001266 0,
Rusty Russell3aef54d2005-01-03 03:48:40 +00001267 FUNCTION_MAXNAMELEN - 1 - strlen(t->u.user.name));
Rusty Russell733e54b2004-12-16 14:22:23 +00001268 r->type = IPTCC_R_MODULE;
Harald Welteaae69be2004-08-29 23:32:14 +00001269 set_changed(handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001270 return 1;
1271}
1272
Harald Welte0113fe72004-01-06 19:04:02 +00001273/* Insert the entry `fw' in chain `chain' into position `rulenum'. */
Marc Bouchere6869a82000-03-20 06:03:29 +00001274int
Rusty Russell79dee072000-05-02 16:45:16 +00001275TC_INSERT_ENTRY(const IPT_CHAINLABEL chain,
1276 const STRUCT_ENTRY *e,
1277 unsigned int rulenum,
1278 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001279{
Harald Welteaae69be2004-08-29 23:32:14 +00001280 struct chain_head *c;
Martin Josefssoneb066cc2004-09-22 21:04:07 +00001281 struct rule_head *r;
1282 struct list_head *prev;
Marc Bouchere6869a82000-03-20 06:03:29 +00001283
Rusty Russell79dee072000-05-02 16:45:16 +00001284 iptc_fn = TC_INSERT_ENTRY;
Harald Welteaae69be2004-08-29 23:32:14 +00001285
1286 if (!(c = iptcc_find_label(chain, *handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001287 errno = ENOENT;
1288 return 0;
1289 }
1290
Martin Josefssoneb066cc2004-09-22 21:04:07 +00001291 /* first rulenum index = 0
1292 first c->num_rules index = 1 */
1293 if (rulenum > c->num_rules) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001294 errno = E2BIG;
1295 return 0;
1296 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001297
Martin Josefsson631f3612004-09-23 19:25:06 +00001298 /* If we are inserting at the end just take advantage of the
1299 double linked list, insert will happen before the entry
1300 prev points to. */
1301 if (rulenum == c->num_rules) {
Martin Josefssoneb066cc2004-09-22 21:04:07 +00001302 prev = &c->rules;
Martin Josefssona5616dc2004-10-24 22:27:31 +00001303 } else if (rulenum + 1 <= c->num_rules/2) {
Martin Josefsson631f3612004-09-23 19:25:06 +00001304 r = iptcc_get_rule_num(c, rulenum + 1);
Martin Josefssona5616dc2004-10-24 22:27:31 +00001305 prev = &r->list;
1306 } else {
1307 r = iptcc_get_rule_num_reverse(c, c->num_rules - rulenum);
Martin Josefsson631f3612004-09-23 19:25:06 +00001308 prev = &r->list;
1309 }
Martin Josefssoneb066cc2004-09-22 21:04:07 +00001310
Harald Welteaae69be2004-08-29 23:32:14 +00001311 if (!(r = iptcc_alloc_rule(c, e->next_offset))) {
1312 errno = ENOMEM;
Harald Welte0113fe72004-01-06 19:04:02 +00001313 return 0;
Harald Welteaae69be2004-08-29 23:32:14 +00001314 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001315
Harald Welteaae69be2004-08-29 23:32:14 +00001316 memcpy(r->entry, e, e->next_offset);
1317 r->counter_map.maptype = COUNTER_MAP_SET;
1318
1319 if (!iptcc_map_target(*handle, r)) {
1320 free(r);
1321 return 0;
1322 }
1323
Martin Josefssoneb066cc2004-09-22 21:04:07 +00001324 list_add_tail(&r->list, prev);
Harald Welteaae69be2004-08-29 23:32:14 +00001325 c->num_rules++;
1326
1327 set_changed(*handle);
1328
1329 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00001330}
1331
1332/* Atomically replace rule `rulenum' in `chain' with `fw'. */
1333int
Rusty Russell79dee072000-05-02 16:45:16 +00001334TC_REPLACE_ENTRY(const IPT_CHAINLABEL chain,
1335 const STRUCT_ENTRY *e,
1336 unsigned int rulenum,
1337 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001338{
Harald Welteaae69be2004-08-29 23:32:14 +00001339 struct chain_head *c;
1340 struct rule_head *r, *old;
Marc Bouchere6869a82000-03-20 06:03:29 +00001341
Rusty Russell79dee072000-05-02 16:45:16 +00001342 iptc_fn = TC_REPLACE_ENTRY;
Marc Bouchere6869a82000-03-20 06:03:29 +00001343
Harald Welteaae69be2004-08-29 23:32:14 +00001344 if (!(c = iptcc_find_label(chain, *handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001345 errno = ENOENT;
1346 return 0;
1347 }
1348
Martin Josefsson0f9b8b12004-12-18 17:18:49 +00001349 if (rulenum >= c->num_rules) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001350 errno = E2BIG;
1351 return 0;
1352 }
1353
Martin Josefsson0f9b8b12004-12-18 17:18:49 +00001354 /* Take advantage of the double linked list if possible. */
1355 if (rulenum + 1 <= c->num_rules/2) {
1356 old = iptcc_get_rule_num(c, rulenum + 1);
1357 } else {
1358 old = iptcc_get_rule_num_reverse(c, c->num_rules - rulenum);
1359 }
1360
Harald Welteaae69be2004-08-29 23:32:14 +00001361 if (!(r = iptcc_alloc_rule(c, e->next_offset))) {
1362 errno = ENOMEM;
Marc Bouchere6869a82000-03-20 06:03:29 +00001363 return 0;
Harald Welteaae69be2004-08-29 23:32:14 +00001364 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001365
Harald Welteaae69be2004-08-29 23:32:14 +00001366 memcpy(r->entry, e, e->next_offset);
1367 r->counter_map.maptype = COUNTER_MAP_SET;
1368
1369 if (!iptcc_map_target(*handle, r)) {
1370 free(r);
Harald Welte0113fe72004-01-06 19:04:02 +00001371 return 0;
Harald Welteaae69be2004-08-29 23:32:14 +00001372 }
Harald Welte0113fe72004-01-06 19:04:02 +00001373
Harald Welteaae69be2004-08-29 23:32:14 +00001374 list_add(&r->list, &old->list);
1375 iptcc_delete_rule(old);
1376
1377 set_changed(*handle);
1378
1379 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00001380}
1381
Harald Welte0113fe72004-01-06 19:04:02 +00001382/* Append entry `fw' to chain `chain'. Equivalent to insert with
Marc Bouchere6869a82000-03-20 06:03:29 +00001383 rulenum = length of chain. */
1384int
Rusty Russell79dee072000-05-02 16:45:16 +00001385TC_APPEND_ENTRY(const IPT_CHAINLABEL chain,
1386 const STRUCT_ENTRY *e,
1387 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001388{
Harald Welteaae69be2004-08-29 23:32:14 +00001389 struct chain_head *c;
1390 struct rule_head *r;
Marc Bouchere6869a82000-03-20 06:03:29 +00001391
Rusty Russell79dee072000-05-02 16:45:16 +00001392 iptc_fn = TC_APPEND_ENTRY;
Harald Welteaae69be2004-08-29 23:32:14 +00001393 if (!(c = iptcc_find_label(chain, *handle))) {
1394 DEBUGP("unable to find chain `%s'\n", chain);
Marc Bouchere6869a82000-03-20 06:03:29 +00001395 errno = ENOENT;
1396 return 0;
1397 }
1398
Harald Welteaae69be2004-08-29 23:32:14 +00001399 if (!(r = iptcc_alloc_rule(c, e->next_offset))) {
1400 DEBUGP("unable to allocate rule for chain `%s'\n", chain);
1401 errno = ENOMEM;
Harald Welte0113fe72004-01-06 19:04:02 +00001402 return 0;
Harald Welteaae69be2004-08-29 23:32:14 +00001403 }
Harald Welte0113fe72004-01-06 19:04:02 +00001404
Harald Welteaae69be2004-08-29 23:32:14 +00001405 memcpy(r->entry, e, e->next_offset);
1406 r->counter_map.maptype = COUNTER_MAP_SET;
1407
1408 if (!iptcc_map_target(*handle, r)) {
Martin Josefsson12009532004-09-23 18:24:29 +00001409 DEBUGP("unable to map target of rule for chain `%s'\n", chain);
Harald Welteaae69be2004-08-29 23:32:14 +00001410 free(r);
1411 return 0;
1412 }
1413
1414 list_add_tail(&r->list, &c->rules);
1415 c->num_rules++;
1416
1417 set_changed(*handle);
1418
1419 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00001420}
1421
1422static inline int
Rusty Russell79dee072000-05-02 16:45:16 +00001423match_different(const STRUCT_ENTRY_MATCH *a,
Rusty Russelledf14cf2000-04-19 11:26:44 +00001424 const unsigned char *a_elems,
1425 const unsigned char *b_elems,
1426 unsigned char **maskptr)
Marc Bouchere6869a82000-03-20 06:03:29 +00001427{
Rusty Russell79dee072000-05-02 16:45:16 +00001428 const STRUCT_ENTRY_MATCH *b;
Rusty Russelledf14cf2000-04-19 11:26:44 +00001429 unsigned int i;
Marc Bouchere6869a82000-03-20 06:03:29 +00001430
1431 /* Offset of b is the same as a. */
Rusty Russell30fd6e52000-04-23 09:16:06 +00001432 b = (void *)b_elems + ((unsigned char *)a - a_elems);
Marc Bouchere6869a82000-03-20 06:03:29 +00001433
Rusty Russell228e98d2000-04-27 10:28:06 +00001434 if (a->u.match_size != b->u.match_size)
Marc Bouchere6869a82000-03-20 06:03:29 +00001435 return 1;
1436
Rusty Russell228e98d2000-04-27 10:28:06 +00001437 if (strcmp(a->u.user.name, b->u.user.name) != 0)
Marc Bouchere6869a82000-03-20 06:03:29 +00001438 return 1;
1439
Rusty Russell73ef09b2000-07-03 10:24:04 +00001440 *maskptr += ALIGN(sizeof(*a));
Rusty Russelledf14cf2000-04-19 11:26:44 +00001441
Rusty Russell73ef09b2000-07-03 10:24:04 +00001442 for (i = 0; i < a->u.match_size - ALIGN(sizeof(*a)); i++)
Rusty Russelledf14cf2000-04-19 11:26:44 +00001443 if (((a->data[i] ^ b->data[i]) & (*maskptr)[i]) != 0)
Rusty Russell90e712a2000-03-29 04:19:26 +00001444 return 1;
Rusty Russelledf14cf2000-04-19 11:26:44 +00001445 *maskptr += i;
1446 return 0;
1447}
1448
1449static inline int
Rusty Russell733e54b2004-12-16 14:22:23 +00001450target_same(struct rule_head *a, struct rule_head *b,const unsigned char *mask)
Rusty Russelledf14cf2000-04-19 11:26:44 +00001451{
1452 unsigned int i;
Rusty Russell733e54b2004-12-16 14:22:23 +00001453 STRUCT_ENTRY_TARGET *ta, *tb;
Marc Bouchere6869a82000-03-20 06:03:29 +00001454
Rusty Russell733e54b2004-12-16 14:22:23 +00001455 if (a->type != b->type)
1456 return 0;
1457
1458 ta = GET_TARGET(a->entry);
1459 tb = GET_TARGET(b->entry);
1460
1461 switch (a->type) {
1462 case IPTCC_R_FALLTHROUGH:
1463 return 1;
1464 case IPTCC_R_JUMP:
1465 return a->jump == b->jump;
1466 case IPTCC_R_STANDARD:
1467 return ((STRUCT_STANDARD_TARGET *)ta)->verdict
1468 == ((STRUCT_STANDARD_TARGET *)tb)->verdict;
1469 case IPTCC_R_MODULE:
1470 if (ta->u.target_size != tb->u.target_size)
1471 return 0;
1472 if (strcmp(ta->u.user.name, tb->u.user.name) != 0)
1473 return 0;
1474
1475 for (i = 0; i < ta->u.target_size - sizeof(*ta); i++)
Rusty Russelldaade442004-12-29 11:14:52 +00001476 if (((ta->data[i] ^ tb->data[i]) & mask[i]) != 0)
Rusty Russell733e54b2004-12-16 14:22:23 +00001477 return 0;
1478 return 1;
1479 default:
1480 fprintf(stderr, "ERROR: bad type %i\n", a->type);
1481 abort();
1482 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001483}
1484
Rusty Russell733e54b2004-12-16 14:22:23 +00001485static unsigned char *
Rusty Russell79dee072000-05-02 16:45:16 +00001486is_same(const STRUCT_ENTRY *a,
1487 const STRUCT_ENTRY *b,
1488 unsigned char *matchmask);
Marc Bouchere6869a82000-03-20 06:03:29 +00001489
Harald Welte0113fe72004-01-06 19:04:02 +00001490/* Delete the first rule in `chain' which matches `fw'. */
Marc Bouchere6869a82000-03-20 06:03:29 +00001491int
Rusty Russell79dee072000-05-02 16:45:16 +00001492TC_DELETE_ENTRY(const IPT_CHAINLABEL chain,
1493 const STRUCT_ENTRY *origfw,
1494 unsigned char *matchmask,
1495 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001496{
Harald Welteaae69be2004-08-29 23:32:14 +00001497 struct chain_head *c;
Rusty Russelle45c7132004-12-16 13:21:44 +00001498 struct rule_head *r, *i;
Marc Bouchere6869a82000-03-20 06:03:29 +00001499
Rusty Russell79dee072000-05-02 16:45:16 +00001500 iptc_fn = TC_DELETE_ENTRY;
Harald Welteaae69be2004-08-29 23:32:14 +00001501 if (!(c = iptcc_find_label(chain, *handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001502 errno = ENOENT;
1503 return 0;
1504 }
1505
Rusty Russelle45c7132004-12-16 13:21:44 +00001506 /* Create a rule_head from origfw. */
1507 r = iptcc_alloc_rule(c, origfw->next_offset);
1508 if (!r) {
Harald Welte0113fe72004-01-06 19:04:02 +00001509 errno = ENOMEM;
1510 return 0;
1511 }
1512
Rusty Russelle45c7132004-12-16 13:21:44 +00001513 memcpy(r->entry, origfw, origfw->next_offset);
1514 r->counter_map.maptype = COUNTER_MAP_NOMAP;
1515 if (!iptcc_map_target(*handle, r)) {
1516 DEBUGP("unable to map target of rule for chain `%s'\n", chain);
1517 free(r);
1518 return 0;
1519 }
Harald Welte0113fe72004-01-06 19:04:02 +00001520
Rusty Russelle45c7132004-12-16 13:21:44 +00001521 list_for_each_entry(i, &c->rules, list) {
Rusty Russell733e54b2004-12-16 14:22:23 +00001522 unsigned char *mask;
Harald Weltefe537072004-08-30 20:28:53 +00001523
Rusty Russell733e54b2004-12-16 14:22:23 +00001524 mask = is_same(r->entry, i->entry, matchmask);
1525 if (!mask)
1526 continue;
Martin Josefsson2a5dbbb2004-09-22 21:37:41 +00001527
Rusty Russell733e54b2004-12-16 14:22:23 +00001528 if (!target_same(r, i, mask))
1529 continue;
1530
1531 /* If we are about to delete the rule that is the
1532 * current iterator, move rule iterator back. next
1533 * pointer will then point to real next node */
1534 if (i == (*handle)->rule_iterator_cur) {
1535 (*handle)->rule_iterator_cur =
1536 list_entry((*handle)->rule_iterator_cur->list.prev,
1537 struct rule_head, list);
Marc Bouchere6869a82000-03-20 06:03:29 +00001538 }
Rusty Russell733e54b2004-12-16 14:22:23 +00001539
1540 c->num_rules--;
1541 iptcc_delete_rule(i);
1542
1543 set_changed(*handle);
1544 free(r);
1545 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00001546 }
1547
Rusty Russelle45c7132004-12-16 13:21:44 +00001548 free(r);
Marc Bouchere6869a82000-03-20 06:03:29 +00001549 errno = ENOENT;
1550 return 0;
Rusty Russell7e53bf92000-03-20 07:03:28 +00001551}
Harald Welteaae69be2004-08-29 23:32:14 +00001552
Marc Bouchere6869a82000-03-20 06:03:29 +00001553
1554/* Delete the rule in position `rulenum' in `chain'. */
1555int
Rusty Russell79dee072000-05-02 16:45:16 +00001556TC_DELETE_NUM_ENTRY(const IPT_CHAINLABEL chain,
1557 unsigned int rulenum,
1558 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001559{
Harald Welteaae69be2004-08-29 23:32:14 +00001560 struct chain_head *c;
1561 struct rule_head *r;
Marc Bouchere6869a82000-03-20 06:03:29 +00001562
Rusty Russell79dee072000-05-02 16:45:16 +00001563 iptc_fn = TC_DELETE_NUM_ENTRY;
Harald Welteaae69be2004-08-29 23:32:14 +00001564
1565 if (!(c = iptcc_find_label(chain, *handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001566 errno = ENOENT;
1567 return 0;
1568 }
1569
Martin Josefssona5616dc2004-10-24 22:27:31 +00001570 if (rulenum >= c->num_rules) {
Martin Josefsson631f3612004-09-23 19:25:06 +00001571 errno = E2BIG;
1572 return 0;
1573 }
1574
1575 /* Take advantage of the double linked list if possible. */
Martin Josefssona5616dc2004-10-24 22:27:31 +00001576 if (rulenum + 1 <= c->num_rules/2) {
1577 r = iptcc_get_rule_num(c, rulenum + 1);
1578 } else {
1579 r = iptcc_get_rule_num_reverse(c, c->num_rules - rulenum);
Marc Bouchere6869a82000-03-20 06:03:29 +00001580 }
1581
Harald Welteaae69be2004-08-29 23:32:14 +00001582 /* If we are about to delete the rule that is the current
1583 * iterator, move rule iterator back. next pointer will then
1584 * point to real next node */
1585 if (r == (*handle)->rule_iterator_cur) {
1586 (*handle)->rule_iterator_cur =
1587 list_entry((*handle)->rule_iterator_cur->list.prev,
1588 struct rule_head, list);
Harald Welte0113fe72004-01-06 19:04:02 +00001589 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001590
Harald Welteaae69be2004-08-29 23:32:14 +00001591 c->num_rules--;
1592 iptcc_delete_rule(r);
1593
Martin Josefsson2a5dbbb2004-09-22 21:37:41 +00001594 set_changed(*handle);
1595
Harald Welteaae69be2004-08-29 23:32:14 +00001596 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00001597}
1598
1599/* Check the packet `fw' on chain `chain'. Returns the verdict, or
1600 NULL and sets errno. */
1601const char *
Rusty Russell79dee072000-05-02 16:45:16 +00001602TC_CHECK_PACKET(const IPT_CHAINLABEL chain,
1603 STRUCT_ENTRY *entry,
1604 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001605{
Derrik Pates664c0a32005-02-01 13:28:14 +00001606 iptc_fn = TC_CHECK_PACKET;
Marc Bouchere6869a82000-03-20 06:03:29 +00001607 errno = ENOSYS;
1608 return NULL;
1609}
1610
1611/* Flushes the entries in the given chain (ie. empties chain). */
1612int
Rusty Russell79dee072000-05-02 16:45:16 +00001613TC_FLUSH_ENTRIES(const IPT_CHAINLABEL chain, TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001614{
Harald Welteaae69be2004-08-29 23:32:14 +00001615 struct chain_head *c;
1616 struct rule_head *r, *tmp;
Marc Bouchere6869a82000-03-20 06:03:29 +00001617
Harald Welte0113fe72004-01-06 19:04:02 +00001618 iptc_fn = TC_FLUSH_ENTRIES;
Harald Welteaae69be2004-08-29 23:32:14 +00001619 if (!(c = iptcc_find_label(chain, *handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001620 errno = ENOENT;
1621 return 0;
1622 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001623
Harald Welteaae69be2004-08-29 23:32:14 +00001624 list_for_each_entry_safe(r, tmp, &c->rules, list) {
1625 iptcc_delete_rule(r);
1626 }
1627
1628 c->num_rules = 0;
1629
1630 set_changed(*handle);
1631
1632 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00001633}
1634
1635/* Zeroes the counters in a chain. */
1636int
Rusty Russell79dee072000-05-02 16:45:16 +00001637TC_ZERO_ENTRIES(const IPT_CHAINLABEL chain, TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001638{
Harald Welteaae69be2004-08-29 23:32:14 +00001639 struct chain_head *c;
1640 struct rule_head *r;
Rusty Russell7e53bf92000-03-20 07:03:28 +00001641
Derrik Pates664c0a32005-02-01 13:28:14 +00001642 iptc_fn = TC_ZERO_ENTRIES;
Harald Welteaae69be2004-08-29 23:32:14 +00001643 if (!(c = iptcc_find_label(chain, *handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001644 errno = ENOENT;
1645 return 0;
1646 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001647
Harald Welteaae69be2004-08-29 23:32:14 +00001648 list_for_each_entry(r, &c->rules, list) {
1649 if (r->counter_map.maptype == COUNTER_MAP_NORMAL_MAP)
1650 r->counter_map.maptype = COUNTER_MAP_ZEROED;
Marc Bouchere6869a82000-03-20 06:03:29 +00001651 }
Harald Welteaae69be2004-08-29 23:32:14 +00001652
Rusty Russell175f6412000-03-24 09:32:20 +00001653 set_changed(*handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001654
Marc Bouchere6869a82000-03-20 06:03:29 +00001655 return 1;
1656}
1657
Harald Welte1cef74d2001-01-05 15:22:59 +00001658STRUCT_COUNTERS *
1659TC_READ_COUNTER(const IPT_CHAINLABEL chain,
1660 unsigned int rulenum,
1661 TC_HANDLE_T *handle)
1662{
Harald Welteaae69be2004-08-29 23:32:14 +00001663 struct chain_head *c;
1664 struct rule_head *r;
Harald Welte1cef74d2001-01-05 15:22:59 +00001665
1666 iptc_fn = TC_READ_COUNTER;
1667 CHECK(*handle);
1668
Harald Welteaae69be2004-08-29 23:32:14 +00001669 if (!(c = iptcc_find_label(chain, *handle))) {
Harald Welte1cef74d2001-01-05 15:22:59 +00001670 errno = ENOENT;
1671 return NULL;
1672 }
1673
Harald Welteaae69be2004-08-29 23:32:14 +00001674 if (!(r = iptcc_get_rule_num(c, rulenum))) {
Harald Welte0113fe72004-01-06 19:04:02 +00001675 errno = E2BIG;
1676 return NULL;
1677 }
1678
Harald Welteaae69be2004-08-29 23:32:14 +00001679 return &r->entry[0].counters;
Harald Welte1cef74d2001-01-05 15:22:59 +00001680}
1681
1682int
1683TC_ZERO_COUNTER(const IPT_CHAINLABEL chain,
1684 unsigned int rulenum,
1685 TC_HANDLE_T *handle)
1686{
Harald Welteaae69be2004-08-29 23:32:14 +00001687 struct chain_head *c;
1688 struct rule_head *r;
Harald Welte1cef74d2001-01-05 15:22:59 +00001689
1690 iptc_fn = TC_ZERO_COUNTER;
1691 CHECK(*handle);
1692
Harald Welteaae69be2004-08-29 23:32:14 +00001693 if (!(c = iptcc_find_label(chain, *handle))) {
Harald Welte1cef74d2001-01-05 15:22:59 +00001694 errno = ENOENT;
1695 return 0;
1696 }
1697
Harald Welteaae69be2004-08-29 23:32:14 +00001698 if (!(r = iptcc_get_rule_num(c, rulenum))) {
Harald Welte0113fe72004-01-06 19:04:02 +00001699 errno = E2BIG;
1700 return 0;
1701 }
1702
Harald Welteaae69be2004-08-29 23:32:14 +00001703 if (r->counter_map.maptype == COUNTER_MAP_NORMAL_MAP)
1704 r->counter_map.maptype = COUNTER_MAP_ZEROED;
Harald Welte1cef74d2001-01-05 15:22:59 +00001705
1706 set_changed(*handle);
1707
1708 return 1;
1709}
1710
1711int
1712TC_SET_COUNTER(const IPT_CHAINLABEL chain,
1713 unsigned int rulenum,
1714 STRUCT_COUNTERS *counters,
1715 TC_HANDLE_T *handle)
1716{
Harald Welteaae69be2004-08-29 23:32:14 +00001717 struct chain_head *c;
1718 struct rule_head *r;
Harald Welte1cef74d2001-01-05 15:22:59 +00001719 STRUCT_ENTRY *e;
Harald Welte1cef74d2001-01-05 15:22:59 +00001720
1721 iptc_fn = TC_SET_COUNTER;
1722 CHECK(*handle);
1723
Harald Welteaae69be2004-08-29 23:32:14 +00001724 if (!(c = iptcc_find_label(chain, *handle))) {
Harald Welte1cef74d2001-01-05 15:22:59 +00001725 errno = ENOENT;
1726 return 0;
1727 }
Harald Welte0113fe72004-01-06 19:04:02 +00001728
Harald Welteaae69be2004-08-29 23:32:14 +00001729 if (!(r = iptcc_get_rule_num(c, rulenum))) {
Harald Welte0113fe72004-01-06 19:04:02 +00001730 errno = E2BIG;
1731 return 0;
1732 }
1733
Harald Welteaae69be2004-08-29 23:32:14 +00001734 e = r->entry;
1735 r->counter_map.maptype = COUNTER_MAP_SET;
Harald Welte0113fe72004-01-06 19:04:02 +00001736
1737 memcpy(&e->counters, counters, sizeof(STRUCT_COUNTERS));
Harald Welte1cef74d2001-01-05 15:22:59 +00001738
1739 set_changed(*handle);
1740
1741 return 1;
1742}
1743
Marc Bouchere6869a82000-03-20 06:03:29 +00001744/* Creates a new chain. */
1745/* To create a chain, create two rules: error node and unconditional
1746 * return. */
1747int
Rusty Russell79dee072000-05-02 16:45:16 +00001748TC_CREATE_CHAIN(const IPT_CHAINLABEL chain, TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001749{
Harald Welteaae69be2004-08-29 23:32:14 +00001750 static struct chain_head *c;
Marc Bouchere6869a82000-03-20 06:03:29 +00001751
Rusty Russell79dee072000-05-02 16:45:16 +00001752 iptc_fn = TC_CREATE_CHAIN;
Marc Bouchere6869a82000-03-20 06:03:29 +00001753
1754 /* find_label doesn't cover built-in targets: DROP, ACCEPT,
1755 QUEUE, RETURN. */
Harald Welteaae69be2004-08-29 23:32:14 +00001756 if (iptcc_find_label(chain, *handle)
Rusty Russell79dee072000-05-02 16:45:16 +00001757 || strcmp(chain, LABEL_DROP) == 0
1758 || strcmp(chain, LABEL_ACCEPT) == 0
Rusty Russell67088e72000-05-10 01:18:57 +00001759 || strcmp(chain, LABEL_QUEUE) == 0
Rusty Russell79dee072000-05-02 16:45:16 +00001760 || strcmp(chain, LABEL_RETURN) == 0) {
Harald Welteaae69be2004-08-29 23:32:14 +00001761 DEBUGP("Chain `%s' already exists\n", chain);
Marc Bouchere6869a82000-03-20 06:03:29 +00001762 errno = EEXIST;
1763 return 0;
1764 }
1765
Rusty Russell79dee072000-05-02 16:45:16 +00001766 if (strlen(chain)+1 > sizeof(IPT_CHAINLABEL)) {
Harald Welteaae69be2004-08-29 23:32:14 +00001767 DEBUGP("Chain name `%s' too long\n", chain);
Marc Bouchere6869a82000-03-20 06:03:29 +00001768 errno = EINVAL;
1769 return 0;
1770 }
1771
Harald Welteaae69be2004-08-29 23:32:14 +00001772 c = iptcc_alloc_chain_head(chain, 0);
1773 if (!c) {
1774 DEBUGP("Cannot allocate memory for chain `%s'\n", chain);
1775 errno = ENOMEM;
1776 return 0;
Marc Bouchere6869a82000-03-20 06:03:29 +00001777
Harald Welteaae69be2004-08-29 23:32:14 +00001778 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001779
Harald Welteaae69be2004-08-29 23:32:14 +00001780 DEBUGP("Creating chain `%s'\n", chain);
1781 list_add_tail(&c->list, &(*handle)->chains);
Harald Welte0113fe72004-01-06 19:04:02 +00001782
1783 set_changed(*handle);
1784
Harald Welteaae69be2004-08-29 23:32:14 +00001785 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00001786}
1787
1788/* Get the number of references to this chain. */
1789int
Rusty Russell79dee072000-05-02 16:45:16 +00001790TC_GET_REFERENCES(unsigned int *ref, const IPT_CHAINLABEL chain,
1791 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001792{
Harald Welteaae69be2004-08-29 23:32:14 +00001793 struct chain_head *c;
Marc Bouchere6869a82000-03-20 06:03:29 +00001794
Derrik Pates664c0a32005-02-01 13:28:14 +00001795 iptc_fn = TC_GET_REFERENCES;
Harald Welteaae69be2004-08-29 23:32:14 +00001796 if (!(c = iptcc_find_label(chain, *handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001797 errno = ENOENT;
1798 return 0;
1799 }
1800
Harald Welteaae69be2004-08-29 23:32:14 +00001801 *ref = c->references;
1802
Marc Bouchere6869a82000-03-20 06:03:29 +00001803 return 1;
1804}
1805
1806/* Deletes a chain. */
1807int
Rusty Russell79dee072000-05-02 16:45:16 +00001808TC_DELETE_CHAIN(const IPT_CHAINLABEL chain, TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001809{
Marc Bouchere6869a82000-03-20 06:03:29 +00001810 unsigned int references;
Harald Welteaae69be2004-08-29 23:32:14 +00001811 struct chain_head *c;
Rusty Russell7e53bf92000-03-20 07:03:28 +00001812
Rusty Russell79dee072000-05-02 16:45:16 +00001813 iptc_fn = TC_DELETE_CHAIN;
Marc Bouchere6869a82000-03-20 06:03:29 +00001814
Harald Welteaae69be2004-08-29 23:32:14 +00001815 if (!(c = iptcc_find_label(chain, *handle))) {
1816 DEBUGP("cannot find chain `%s'\n", chain);
Marc Bouchere6869a82000-03-20 06:03:29 +00001817 errno = ENOENT;
1818 return 0;
1819 }
1820
Harald Welteaae69be2004-08-29 23:32:14 +00001821 if (TC_BUILTIN(chain, *handle)) {
1822 DEBUGP("cannot remove builtin chain `%s'\n", chain);
1823 errno = EINVAL;
1824 return 0;
1825 }
1826
1827 if (!TC_GET_REFERENCES(&references, chain, handle)) {
1828 DEBUGP("cannot get references on chain `%s'\n", chain);
1829 return 0;
1830 }
1831
1832 if (references > 0) {
1833 DEBUGP("chain `%s' still has references\n", chain);
1834 errno = EMLINK;
1835 return 0;
1836 }
1837
1838 if (c->num_rules) {
1839 DEBUGP("chain `%s' is not empty\n", chain);
Marc Bouchere6869a82000-03-20 06:03:29 +00001840 errno = ENOTEMPTY;
1841 return 0;
1842 }
1843
Harald Welteaae69be2004-08-29 23:32:14 +00001844 /* If we are about to delete the chain that is the current
1845 * iterator, move chain iterator firward. */
1846 if (c == (*handle)->chain_iterator_cur)
1847 iptcc_chain_iterator_advance(*handle);
Harald Welte0113fe72004-01-06 19:04:02 +00001848
Harald Welteaae69be2004-08-29 23:32:14 +00001849 list_del(&c->list);
1850 free(c);
Harald Welte0113fe72004-01-06 19:04:02 +00001851
Harald Welteaae69be2004-08-29 23:32:14 +00001852 DEBUGP("chain `%s' deleted\n", chain);
1853
1854 set_changed(*handle);
1855
1856 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00001857}
1858
1859/* Renames a chain. */
Rusty Russell79dee072000-05-02 16:45:16 +00001860int TC_RENAME_CHAIN(const IPT_CHAINLABEL oldname,
1861 const IPT_CHAINLABEL newname,
1862 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001863{
Harald Welteaae69be2004-08-29 23:32:14 +00001864 struct chain_head *c;
Rusty Russell79dee072000-05-02 16:45:16 +00001865 iptc_fn = TC_RENAME_CHAIN;
Marc Bouchere6869a82000-03-20 06:03:29 +00001866
Harald Welte1de80462000-10-30 12:00:27 +00001867 /* find_label doesn't cover built-in targets: DROP, ACCEPT,
1868 QUEUE, RETURN. */
Harald Welteaae69be2004-08-29 23:32:14 +00001869 if (iptcc_find_label(newname, *handle)
Rusty Russell79dee072000-05-02 16:45:16 +00001870 || strcmp(newname, LABEL_DROP) == 0
1871 || strcmp(newname, LABEL_ACCEPT) == 0
Harald Welte1de80462000-10-30 12:00:27 +00001872 || strcmp(newname, LABEL_QUEUE) == 0
Rusty Russell79dee072000-05-02 16:45:16 +00001873 || strcmp(newname, LABEL_RETURN) == 0) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001874 errno = EEXIST;
1875 return 0;
1876 }
1877
Harald Welteaae69be2004-08-29 23:32:14 +00001878 if (!(c = iptcc_find_label(oldname, *handle))
Rusty Russell79dee072000-05-02 16:45:16 +00001879 || TC_BUILTIN(oldname, *handle)) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001880 errno = ENOENT;
1881 return 0;
1882 }
1883
Rusty Russell79dee072000-05-02 16:45:16 +00001884 if (strlen(newname)+1 > sizeof(IPT_CHAINLABEL)) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001885 errno = EINVAL;
1886 return 0;
1887 }
1888
Harald Welteaae69be2004-08-29 23:32:14 +00001889 strncpy(c->name, newname, sizeof(IPT_CHAINLABEL));
1890
Harald Welte0113fe72004-01-06 19:04:02 +00001891 set_changed(*handle);
1892
Marc Bouchere6869a82000-03-20 06:03:29 +00001893 return 1;
1894}
1895
1896/* Sets the policy on a built-in chain. */
1897int
Rusty Russell79dee072000-05-02 16:45:16 +00001898TC_SET_POLICY(const IPT_CHAINLABEL chain,
1899 const IPT_CHAINLABEL policy,
Harald Welte1cef74d2001-01-05 15:22:59 +00001900 STRUCT_COUNTERS *counters,
Rusty Russell79dee072000-05-02 16:45:16 +00001901 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001902{
Harald Welteaae69be2004-08-29 23:32:14 +00001903 struct chain_head *c;
Marc Bouchere6869a82000-03-20 06:03:29 +00001904
Rusty Russell79dee072000-05-02 16:45:16 +00001905 iptc_fn = TC_SET_POLICY;
Marc Bouchere6869a82000-03-20 06:03:29 +00001906
Harald Welteaae69be2004-08-29 23:32:14 +00001907 if (!(c = iptcc_find_label(chain, *handle))) {
1908 DEBUGP("cannot find chain `%s'\n", chain);
1909 errno = ENOENT;
Marc Bouchere6869a82000-03-20 06:03:29 +00001910 return 0;
1911 }
1912
Harald Welteaae69be2004-08-29 23:32:14 +00001913 if (!iptcc_is_builtin(c)) {
1914 DEBUGP("cannot set policy of userdefinedchain `%s'\n", chain);
1915 errno = ENOENT;
1916 return 0;
1917 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001918
Rusty Russell79dee072000-05-02 16:45:16 +00001919 if (strcmp(policy, LABEL_ACCEPT) == 0)
Harald Welteaae69be2004-08-29 23:32:14 +00001920 c->verdict = -NF_ACCEPT - 1;
Rusty Russell79dee072000-05-02 16:45:16 +00001921 else if (strcmp(policy, LABEL_DROP) == 0)
Harald Welteaae69be2004-08-29 23:32:14 +00001922 c->verdict = -NF_DROP - 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00001923 else {
1924 errno = EINVAL;
1925 return 0;
1926 }
Harald Welte1cef74d2001-01-05 15:22:59 +00001927
Harald Welte1cef74d2001-01-05 15:22:59 +00001928 if (counters) {
1929 /* set byte and packet counters */
Harald Welteaae69be2004-08-29 23:32:14 +00001930 memcpy(&c->counters, counters, sizeof(STRUCT_COUNTERS));
1931 c->counter_map.maptype = COUNTER_MAP_SET;
Harald Welte1cef74d2001-01-05 15:22:59 +00001932 } else {
Harald Welteaae69be2004-08-29 23:32:14 +00001933 c->counter_map.maptype = COUNTER_MAP_NOMAP;
Harald Welte1cef74d2001-01-05 15:22:59 +00001934 }
1935
Rusty Russell175f6412000-03-24 09:32:20 +00001936 set_changed(*handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001937
Marc Bouchere6869a82000-03-20 06:03:29 +00001938 return 1;
1939}
1940
1941/* Without this, on gcc 2.7.2.3, we get:
Rusty Russell79dee072000-05-02 16:45:16 +00001942 libiptc.c: In function `TC_COMMIT':
Marc Bouchere6869a82000-03-20 06:03:29 +00001943 libiptc.c:833: fixed or forbidden register was spilled.
1944 This may be due to a compiler bug or to impossible asm
1945 statements or clauses.
1946*/
1947static void
Rusty Russell79dee072000-05-02 16:45:16 +00001948subtract_counters(STRUCT_COUNTERS *answer,
1949 const STRUCT_COUNTERS *a,
1950 const STRUCT_COUNTERS *b)
Marc Bouchere6869a82000-03-20 06:03:29 +00001951{
1952 answer->pcnt = a->pcnt - b->pcnt;
1953 answer->bcnt = a->bcnt - b->bcnt;
1954}
1955
Harald Welteaae69be2004-08-29 23:32:14 +00001956
1957static void counters_nomap(STRUCT_COUNTERS_INFO *newcounters,
1958 unsigned int index)
1959{
1960 newcounters->counters[index] = ((STRUCT_COUNTERS) { 0, 0});
1961 DEBUGP_C("NOMAP => zero\n");
1962}
1963
1964static void counters_normal_map(STRUCT_COUNTERS_INFO *newcounters,
1965 STRUCT_REPLACE *repl,
1966 unsigned int index,
1967 unsigned int mappos)
1968{
1969 /* Original read: X.
1970 * Atomic read on replacement: X + Y.
1971 * Currently in kernel: Z.
1972 * Want in kernel: X + Y + Z.
1973 * => Add in X + Y
1974 * => Add in replacement read.
1975 */
1976 newcounters->counters[index] = repl->counters[mappos];
1977 DEBUGP_C("NORMAL_MAP => mappos %u \n", mappos);
1978}
1979
1980static void counters_map_zeroed(STRUCT_COUNTERS_INFO *newcounters,
1981 STRUCT_REPLACE *repl,
1982 unsigned int index,
1983 unsigned int mappos,
1984 STRUCT_COUNTERS *counters)
1985{
1986 /* Original read: X.
1987 * Atomic read on replacement: X + Y.
1988 * Currently in kernel: Z.
1989 * Want in kernel: Y + Z.
1990 * => Add in Y.
1991 * => Add in (replacement read - original read).
1992 */
1993 subtract_counters(&newcounters->counters[index],
1994 &repl->counters[mappos],
1995 counters);
1996 DEBUGP_C("ZEROED => mappos %u\n", mappos);
1997}
1998
1999static void counters_map_set(STRUCT_COUNTERS_INFO *newcounters,
2000 unsigned int index,
2001 STRUCT_COUNTERS *counters)
2002{
2003 /* Want to set counter (iptables-restore) */
2004
2005 memcpy(&newcounters->counters[index], counters,
2006 sizeof(STRUCT_COUNTERS));
2007
2008 DEBUGP_C("SET\n");
2009}
2010
2011
Marc Bouchere6869a82000-03-20 06:03:29 +00002012int
Rusty Russell79dee072000-05-02 16:45:16 +00002013TC_COMMIT(TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00002014{
2015 /* Replace, then map back the counters. */
Rusty Russell79dee072000-05-02 16:45:16 +00002016 STRUCT_REPLACE *repl;
2017 STRUCT_COUNTERS_INFO *newcounters;
Harald Welteaae69be2004-08-29 23:32:14 +00002018 struct chain_head *c;
2019 int ret;
Martin Josefsson841e4ae2003-05-02 15:30:11 +00002020 size_t counterlen;
Harald Welteaae69be2004-08-29 23:32:14 +00002021 int new_number;
2022 unsigned int new_size;
Marc Bouchere6869a82000-03-20 06:03:29 +00002023
Derrik Pates664c0a32005-02-01 13:28:14 +00002024 iptc_fn = TC_COMMIT;
Marc Bouchere6869a82000-03-20 06:03:29 +00002025 CHECK(*handle);
Martin Josefsson841e4ae2003-05-02 15:30:11 +00002026
Marc Bouchere6869a82000-03-20 06:03:29 +00002027 /* Don't commit if nothing changed. */
2028 if (!(*handle)->changed)
2029 goto finished;
2030
Harald Welteaae69be2004-08-29 23:32:14 +00002031 new_number = iptcc_compile_table_prep(*handle, &new_size);
2032 if (new_number < 0) {
2033 errno = ENOMEM;
2034 return 0;
2035 }
2036
2037 repl = malloc(sizeof(*repl) + new_size);
Marc Bouchere6869a82000-03-20 06:03:29 +00002038 if (!repl) {
2039 errno = ENOMEM;
2040 return 0;
2041 }
Martin Josefssonad3b4f92004-09-22 22:04:07 +00002042 memset(repl, 0, sizeof(*repl) + new_size);
Harald Welteaae69be2004-08-29 23:32:14 +00002043
Rusty Russelle45c7132004-12-16 13:21:44 +00002044#if 0
2045 TC_DUMP_ENTRIES(*handle);
2046#endif
2047
Harald Welteaae69be2004-08-29 23:32:14 +00002048 counterlen = sizeof(STRUCT_COUNTERS_INFO)
2049 + sizeof(STRUCT_COUNTERS) * new_number;
Derrik Pates664c0a32005-02-01 13:28:14 +00002050 memset(repl, 0, sizeof(*repl) + (*handle)->entries->size);
Marc Bouchere6869a82000-03-20 06:03:29 +00002051
2052 /* These are the old counters we will get from kernel */
Rusty Russell79dee072000-05-02 16:45:16 +00002053 repl->counters = malloc(sizeof(STRUCT_COUNTERS)
Marc Bouchere6869a82000-03-20 06:03:29 +00002054 * (*handle)->info.num_entries);
2055 if (!repl->counters) {
2056 free(repl);
2057 errno = ENOMEM;
2058 return 0;
2059 }
Derrik Pates664c0a32005-02-01 13:28:14 +00002060 memset(repl->counters, 0, sizeof(STRUCT_COUNTERS)
2061 * (*handle)->info.num_entries);
Marc Bouchere6869a82000-03-20 06:03:29 +00002062 /* These are the counters we're going to put back, later. */
2063 newcounters = malloc(counterlen);
2064 if (!newcounters) {
2065 free(repl->counters);
2066 free(repl);
2067 errno = ENOMEM;
2068 return 0;
2069 }
Harald Welteaae69be2004-08-29 23:32:14 +00002070 memset(newcounters, 0, counterlen);
Marc Bouchere6869a82000-03-20 06:03:29 +00002071
2072 strcpy(repl->name, (*handle)->info.name);
Harald Welteaae69be2004-08-29 23:32:14 +00002073 repl->num_entries = new_number;
2074 repl->size = new_size;
2075
Marc Bouchere6869a82000-03-20 06:03:29 +00002076 repl->num_counters = (*handle)->info.num_entries;
2077 repl->valid_hooks = (*handle)->info.valid_hooks;
Harald Welteaae69be2004-08-29 23:32:14 +00002078
2079 DEBUGP("num_entries=%u, size=%u, num_counters=%u\n",
2080 repl->num_entries, repl->size, repl->num_counters);
2081
2082 ret = iptcc_compile_table(*handle, repl);
2083 if (ret < 0) {
2084 errno = ret;
2085 free(repl->counters);
2086 free(repl);
2087 return 0;
2088 }
2089
2090
2091#ifdef IPTC_DEBUG2
2092 {
2093 int fd = open("/tmp/libiptc-so_set_replace.blob",
2094 O_CREAT|O_WRONLY);
2095 if (fd >= 0) {
2096 write(fd, repl, sizeof(*repl) + repl->size);
2097 close(fd);
2098 }
2099 }
2100#endif
Marc Bouchere6869a82000-03-20 06:03:29 +00002101
Rusty Russell79dee072000-05-02 16:45:16 +00002102 if (setsockopt(sockfd, TC_IPPROTO, SO_SET_REPLACE, repl,
Harald Welteaae69be2004-08-29 23:32:14 +00002103 sizeof(*repl) + repl->size) < 0) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002104 free(repl->counters);
2105 free(repl);
2106 free(newcounters);
2107 return 0;
2108 }
2109
2110 /* Put counters back. */
2111 strcpy(newcounters->name, (*handle)->info.name);
Harald Welteaae69be2004-08-29 23:32:14 +00002112 newcounters->num_counters = new_number;
Marc Bouchere6869a82000-03-20 06:03:29 +00002113
Harald Welteaae69be2004-08-29 23:32:14 +00002114 list_for_each_entry(c, &(*handle)->chains, list) {
2115 struct rule_head *r;
Marc Bouchere6869a82000-03-20 06:03:29 +00002116
Harald Welteaae69be2004-08-29 23:32:14 +00002117 /* Builtin chains have their own counters */
2118 if (iptcc_is_builtin(c)) {
2119 DEBUGP("counter for chain-index %u: ", c->foot_index);
2120 switch(c->counter_map.maptype) {
2121 case COUNTER_MAP_NOMAP:
2122 counters_nomap(newcounters, c->foot_index);
2123 break;
2124 case COUNTER_MAP_NORMAL_MAP:
2125 counters_normal_map(newcounters, repl,
2126 c->foot_index,
2127 c->counter_map.mappos);
2128 break;
2129 case COUNTER_MAP_ZEROED:
2130 counters_map_zeroed(newcounters, repl,
2131 c->foot_index,
2132 c->counter_map.mappos,
2133 &c->counters);
2134 break;
2135 case COUNTER_MAP_SET:
2136 counters_map_set(newcounters, c->foot_index,
2137 &c->counters);
2138 break;
2139 }
2140 }
Harald Welte1cef74d2001-01-05 15:22:59 +00002141
Harald Welteaae69be2004-08-29 23:32:14 +00002142 list_for_each_entry(r, &c->rules, list) {
2143 DEBUGP("counter for index %u: ", r->index);
2144 switch (r->counter_map.maptype) {
2145 case COUNTER_MAP_NOMAP:
2146 counters_nomap(newcounters, r->index);
2147 break;
Harald Welte1cef74d2001-01-05 15:22:59 +00002148
Harald Welteaae69be2004-08-29 23:32:14 +00002149 case COUNTER_MAP_NORMAL_MAP:
2150 counters_normal_map(newcounters, repl,
2151 r->index,
2152 r->counter_map.mappos);
2153 break;
Harald Welte1cef74d2001-01-05 15:22:59 +00002154
Harald Welteaae69be2004-08-29 23:32:14 +00002155 case COUNTER_MAP_ZEROED:
2156 counters_map_zeroed(newcounters, repl,
2157 r->index,
2158 r->counter_map.mappos,
2159 &r->entry->counters);
2160 break;
2161
2162 case COUNTER_MAP_SET:
2163 counters_map_set(newcounters, r->index,
2164 &r->entry->counters);
2165 break;
2166 }
Marc Bouchere6869a82000-03-20 06:03:29 +00002167 }
2168 }
Rusty Russell62527ce2000-09-04 09:45:54 +00002169
Harald Welteaae69be2004-08-29 23:32:14 +00002170
Rusty Russell62527ce2000-09-04 09:45:54 +00002171#ifdef KERNEL_64_USERSPACE_32
2172 {
2173 /* Kernel will think that pointer should be 64-bits, and get
2174 padding. So we accomodate here (assumption: alignment of
2175 `counters' is on 64-bit boundary). */
2176 u_int64_t *kernptr = (u_int64_t *)&newcounters->counters;
2177 if ((unsigned long)&newcounters->counters % 8 != 0) {
2178 fprintf(stderr,
2179 "counters alignment incorrect! Mail rusty!\n");
2180 abort();
2181 }
2182 *kernptr = newcounters->counters;
Rusty Russell54c307e2000-09-04 06:47:28 +00002183 }
Rusty Russell62527ce2000-09-04 09:45:54 +00002184#endif /* KERNEL_64_USERSPACE_32 */
Marc Bouchere6869a82000-03-20 06:03:29 +00002185
Harald Welteaae69be2004-08-29 23:32:14 +00002186#ifdef IPTC_DEBUG2
2187 {
2188 int fd = open("/tmp/libiptc-so_set_add_counters.blob",
2189 O_CREAT|O_WRONLY);
2190 if (fd >= 0) {
2191 write(fd, newcounters, counterlen);
2192 close(fd);
2193 }
2194 }
2195#endif
2196
Rusty Russell79dee072000-05-02 16:45:16 +00002197 if (setsockopt(sockfd, TC_IPPROTO, SO_SET_ADD_COUNTERS,
2198 newcounters, counterlen) < 0) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002199 free(repl->counters);
2200 free(repl);
2201 free(newcounters);
2202 return 0;
2203 }
2204
2205 free(repl->counters);
2206 free(repl);
2207 free(newcounters);
2208
2209 finished:
Martin Josefsson841e4ae2003-05-02 15:30:11 +00002210 TC_FREE(handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00002211 return 1;
2212}
2213
2214/* Get raw socket. */
2215int
Rusty Russell79dee072000-05-02 16:45:16 +00002216TC_GET_RAW_SOCKET()
Marc Bouchere6869a82000-03-20 06:03:29 +00002217{
2218 return sockfd;
2219}
2220
2221/* Translates errno numbers into more human-readable form than strerror. */
2222const char *
Rusty Russell79dee072000-05-02 16:45:16 +00002223TC_STRERROR(int err)
Marc Bouchere6869a82000-03-20 06:03:29 +00002224{
2225 unsigned int i;
2226 struct table_struct {
2227 void *fn;
2228 int err;
2229 const char *message;
2230 } table [] =
Harald Welte4ccfa632001-07-30 15:12:43 +00002231 { { TC_INIT, EPERM, "Permission denied (you must be root)" },
Rusty Russell79dee072000-05-02 16:45:16 +00002232 { TC_INIT, EINVAL, "Module is wrong version" },
Harald Welte4ccfa632001-07-30 15:12:43 +00002233 { TC_INIT, ENOENT,
2234 "Table does not exist (do you need to insmod?)" },
Rusty Russell79dee072000-05-02 16:45:16 +00002235 { TC_DELETE_CHAIN, ENOTEMPTY, "Chain is not empty" },
2236 { TC_DELETE_CHAIN, EINVAL, "Can't delete built-in chain" },
2237 { TC_DELETE_CHAIN, EMLINK,
Marc Bouchere6869a82000-03-20 06:03:29 +00002238 "Can't delete chain with references left" },
Rusty Russell79dee072000-05-02 16:45:16 +00002239 { TC_CREATE_CHAIN, EEXIST, "Chain already exists" },
2240 { TC_INSERT_ENTRY, E2BIG, "Index of insertion too big" },
2241 { TC_REPLACE_ENTRY, E2BIG, "Index of replacement too big" },
2242 { TC_DELETE_NUM_ENTRY, E2BIG, "Index of deletion too big" },
Harald Welte1cef74d2001-01-05 15:22:59 +00002243 { TC_READ_COUNTER, E2BIG, "Index of counter too big" },
2244 { TC_ZERO_COUNTER, E2BIG, "Index of counter too big" },
Rusty Russell79dee072000-05-02 16:45:16 +00002245 { TC_INSERT_ENTRY, ELOOP, "Loop found in table" },
2246 { TC_INSERT_ENTRY, EINVAL, "Target problem" },
Marc Bouchere6869a82000-03-20 06:03:29 +00002247 /* EINVAL for CHECK probably means bad interface. */
Rusty Russell79dee072000-05-02 16:45:16 +00002248 { TC_CHECK_PACKET, EINVAL,
Marc Boucherc8264992000-04-22 22:34:44 +00002249 "Bad arguments (does that interface exist?)" },
Harald Welte4ccfa632001-07-30 15:12:43 +00002250 { TC_CHECK_PACKET, ENOSYS,
2251 "Checking will most likely never get implemented" },
Marc Bouchere6869a82000-03-20 06:03:29 +00002252 /* ENOENT for DELETE probably means no matching rule */
Rusty Russell79dee072000-05-02 16:45:16 +00002253 { TC_DELETE_ENTRY, ENOENT,
Marc Boucherc8264992000-04-22 22:34:44 +00002254 "Bad rule (does a matching rule exist in that chain?)" },
Rusty Russell79dee072000-05-02 16:45:16 +00002255 { TC_SET_POLICY, ENOENT,
Marc Boucherc8264992000-04-22 22:34:44 +00002256 "Bad built-in chain name" },
Rusty Russell79dee072000-05-02 16:45:16 +00002257 { TC_SET_POLICY, EINVAL,
Marc Boucherc8264992000-04-22 22:34:44 +00002258 "Bad policy name" },
Harald Welte4ccfa632001-07-30 15:12:43 +00002259
2260 { NULL, 0, "Incompatible with this kernel" },
2261 { NULL, ENOPROTOOPT, "iptables who? (do you need to insmod?)" },
2262 { NULL, ENOSYS, "Will be implemented real soon. I promise ;)" },
2263 { NULL, ENOMEM, "Memory allocation problem" },
2264 { NULL, ENOENT, "No chain/target/match by that name" },
Marc Bouchere6869a82000-03-20 06:03:29 +00002265 };
2266
2267 for (i = 0; i < sizeof(table)/sizeof(struct table_struct); i++) {
2268 if ((!table[i].fn || table[i].fn == iptc_fn)
2269 && table[i].err == err)
2270 return table[i].message;
2271 }
2272
2273 return strerror(err);
2274}