blob: 3557f57aa76db6d7b2086bc3aa68e6c0ab66ce92 [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
359static int alphasort(const void *a, const void *b)
360{
361 return strcmp(((struct chain_head *)a)->name,
362 ((struct chain_head *)b)->name);
363}
364
365/* Delete policy rule of previous chain, since cache doesn't contain
366 * chain policy rules.
367 * WARNING: This function has ugly design and relies on a lot of context, only
368 * to be called from specific places within the parser */
369static int __iptcc_p_del_policy(TC_HANDLE_T h, unsigned int num)
370{
371 if (h->chain_iterator_cur) {
372 /* policy rule is last rule */
373 struct rule_head *pr = (struct rule_head *)
374 h->chain_iterator_cur->rules.prev;
375
376 /* save verdict */
377 h->chain_iterator_cur->verdict =
378 *(int *)GET_TARGET(pr->entry)->data;
379
380 /* save counter and counter_map information */
381 h->chain_iterator_cur->counter_map.maptype =
382 COUNTER_MAP_NORMAL_MAP;
383 h->chain_iterator_cur->counter_map.mappos = num-1;
384 memcpy(&h->chain_iterator_cur->counters, &pr->entry->counters,
385 sizeof(h->chain_iterator_cur->counters));
386
387 /* foot_offset points to verdict rule */
388 h->chain_iterator_cur->foot_index = num;
389 h->chain_iterator_cur->foot_offset = pr->offset;
390
391 /* delete rule from cache */
392 iptcc_delete_rule(pr);
Martin Josefsson8d1b38a2004-09-22 21:00:19 +0000393 h->chain_iterator_cur->num_rules--;
Harald Welteaae69be2004-08-29 23:32:14 +0000394
395 return 1;
396 }
397 return 0;
398}
399
400/* Another ugly helper function split out of cache_add_entry to make it less
401 * spaghetti code */
402static void __iptcc_p_add_chain(TC_HANDLE_T h, struct chain_head *c,
403 unsigned int offset, unsigned int *num)
404{
405 __iptcc_p_del_policy(h, *num);
406
407 c->head_offset = offset;
408 c->index = *num;
409
410 list_add_tail(&c->list, &h->chains);
411 h->chain_iterator_cur = c;
412}
413
414/* main parser function: add an entry from the blob to the cache */
415static int cache_add_entry(STRUCT_ENTRY *e,
416 TC_HANDLE_T h,
417 STRUCT_ENTRY **prev,
418 unsigned int *num)
419{
420 unsigned int builtin;
421 unsigned int offset = (char *)e - (char *)h->entries->entrytable;
422
423 DEBUGP("entering...");
424
425 /* Last entry ("policy rule"). End it.*/
426 if (iptcb_entry2offset(h,e) + e->next_offset == h->entries->size) {
427 /* This is the ERROR node at the end of the chain */
428 DEBUGP_C("%u:%u: end of table:\n", *num, offset);
429
430 __iptcc_p_del_policy(h, *num);
431
432 h->chain_iterator_cur = NULL;
433 goto out_inc;
434 }
435
436 /* We know this is the start of a new chain if it's an ERROR
437 * target, or a hook entry point */
438
439 if (strcmp(GET_TARGET(e)->u.user.name, ERROR_TARGET) == 0) {
440 struct chain_head *c =
441 iptcc_alloc_chain_head((const char *)GET_TARGET(e)->data, 0);
442 DEBUGP_C("%u:%u:new userdefined chain %s: %p\n", *num, offset,
443 (char *)c->name, c);
444 if (!c) {
445 errno = -ENOMEM;
446 return -1;
447 }
448
449 __iptcc_p_add_chain(h, c, offset, num);
450
451 } else if ((builtin = iptcb_ent_is_hook_entry(e, h)) != 0) {
452 struct chain_head *c =
453 iptcc_alloc_chain_head((char *)hooknames[builtin-1],
454 builtin);
455 DEBUGP_C("%u:%u new builtin chain: %p (rules=%p)\n",
456 *num, offset, c, &c->rules);
457 if (!c) {
458 errno = -ENOMEM;
459 return -1;
460 }
461
462 c->hooknum = builtin;
463
464 __iptcc_p_add_chain(h, c, offset, num);
465
466 /* FIXME: this is ugly. */
467 goto new_rule;
468 } else {
469 /* has to be normal rule */
470 struct rule_head *r;
471new_rule:
472
473 if (!(r = iptcc_alloc_rule(h->chain_iterator_cur,
474 e->next_offset))) {
475 errno = ENOMEM;
476 return -1;
477 }
478 DEBUGP_C("%u:%u normal rule: %p: ", *num, offset, r);
479
480 r->index = *num;
481 r->offset = offset;
482 memcpy(r->entry, e, e->next_offset);
483 r->counter_map.maptype = COUNTER_MAP_NORMAL_MAP;
484 r->counter_map.mappos = r->index;
485
486 /* handling of jumps, etc. */
487 if (!strcmp(GET_TARGET(e)->u.user.name, STANDARD_TARGET)) {
488 STRUCT_STANDARD_TARGET *t;
489
490 t = (STRUCT_STANDARD_TARGET *)GET_TARGET(e);
491 if (t->target.u.target_size
492 != ALIGN(sizeof(STRUCT_STANDARD_TARGET))) {
493 errno = EINVAL;
494 return -1;
495 }
496
497 if (t->verdict < 0) {
498 DEBUGP_C("standard, verdict=%d\n", t->verdict);
499 r->type = IPTCC_R_STANDARD;
500 } else if (t->verdict == r->offset+e->next_offset) {
501 DEBUGP_C("fallthrough\n");
502 r->type = IPTCC_R_FALLTHROUGH;
503 } else {
504 DEBUGP_C("jump, target=%u\n", t->verdict);
505 r->type = IPTCC_R_JUMP;
506 /* Jump target fixup has to be deferred
507 * until second pass, since we migh not
508 * yet have parsed the target */
509 }
Martin Josefsson52c38022004-09-22 19:39:40 +0000510 } else {
511 DEBUGP_C("module, target=%s\n", GET_TARGET(e)->u.user.name);
512 r->type = IPTCC_R_MODULE;
Harald Welteaae69be2004-08-29 23:32:14 +0000513 }
514
515 list_add_tail(&r->list, &h->chain_iterator_cur->rules);
Martin Josefsson8d1b38a2004-09-22 21:00:19 +0000516 h->chain_iterator_cur->num_rules++;
Harald Welteaae69be2004-08-29 23:32:14 +0000517 }
518out_inc:
519 (*num)++;
520 return 0;
521}
522
523
524/* parse an iptables blob into it's pieces */
525static int parse_table(TC_HANDLE_T h)
526{
527 STRUCT_ENTRY *prev;
528 unsigned int num = 0;
529 struct chain_head *c;
530
531 /* First pass: over ruleset blob */
532 ENTRY_ITERATE(h->entries->entrytable, h->entries->size,
533 cache_add_entry, h, &prev, &num);
534
535 /* Second pass: fixup parsed data from first pass */
536 list_for_each_entry(c, &h->chains, list) {
537 struct rule_head *r;
538 list_for_each_entry(r, &c->rules, list) {
539 struct chain_head *c;
540 STRUCT_STANDARD_TARGET *t;
541
542 if (r->type != IPTCC_R_JUMP)
543 continue;
544
545 t = (STRUCT_STANDARD_TARGET *)GET_TARGET(r->entry);
546 c = iptcc_find_chain_by_offset(h, t->verdict);
547 if (!c)
548 return -1;
549 r->jump = c;
550 c->references++;
551 }
552 }
553
554 /* FIXME: sort chains */
555
556 return 1;
557}
558
559
560/**********************************************************************
561 * RULESET COMPILATION (cache -> blob)
562 **********************************************************************/
563
564/* Convenience structures */
565struct iptcb_chain_start{
566 STRUCT_ENTRY e;
567 struct ipt_error_target name;
568};
569#define IPTCB_CHAIN_START_SIZE (sizeof(STRUCT_ENTRY) + \
570 ALIGN(sizeof(struct ipt_error_target)))
571
572struct iptcb_chain_foot {
573 STRUCT_ENTRY e;
574 STRUCT_STANDARD_TARGET target;
575};
576#define IPTCB_CHAIN_FOOT_SIZE (sizeof(STRUCT_ENTRY) + \
577 ALIGN(sizeof(STRUCT_STANDARD_TARGET)))
578
579struct iptcb_chain_error {
580 STRUCT_ENTRY entry;
581 struct ipt_error_target target;
582};
583#define IPTCB_CHAIN_ERROR_SIZE (sizeof(STRUCT_ENTRY) + \
584 ALIGN(sizeof(struct ipt_error_target)))
585
586
587
588/* compile rule from cache into blob */
589static inline int iptcc_compile_rule (TC_HANDLE_T h, STRUCT_REPLACE *repl, struct rule_head *r)
590{
591 /* handle jumps */
592 if (r->type == IPTCC_R_JUMP) {
593 STRUCT_STANDARD_TARGET *t;
594 t = (STRUCT_STANDARD_TARGET *)GET_TARGET(r->entry);
595 /* memset for memcmp convenience on delete/replace */
596 memset(t->target.u.user.name, 0, FUNCTION_MAXNAMELEN);
597 strcpy(t->target.u.user.name, STANDARD_TARGET);
598 /* Jumps can only happen to builtin chains, so we
599 * can safely assume that they always have a header */
600 t->verdict = r->jump->head_offset + IPTCB_CHAIN_START_SIZE;
601 } else if (r->type == IPTCC_R_FALLTHROUGH) {
602 STRUCT_STANDARD_TARGET *t;
603 t = (STRUCT_STANDARD_TARGET *)GET_TARGET(r->entry);
604 t->verdict = r->offset + r->size;
605 }
606
607 /* copy entry from cache to blob */
608 memcpy((char *)repl->entries+r->offset, r->entry, r->size);
609
610 return 1;
611}
612
613/* compile chain from cache into blob */
614static int iptcc_compile_chain(TC_HANDLE_T h, STRUCT_REPLACE *repl, struct chain_head *c)
615{
616 int ret;
617 struct rule_head *r;
618 struct iptcb_chain_start *head;
619 struct iptcb_chain_foot *foot;
620
621 /* only user-defined chains have heaer */
622 if (!iptcc_is_builtin(c)) {
623 /* put chain header in place */
624 head = (void *)repl->entries + c->head_offset;
625 head->e.target_offset = sizeof(STRUCT_ENTRY);
626 head->e.next_offset = IPTCB_CHAIN_START_SIZE;
627 strcpy(head->name.t.u.user.name, ERROR_TARGET);
628 head->name.t.u.target_size =
629 ALIGN(sizeof(struct ipt_error_target));
630 strcpy(head->name.error, c->name);
631 } else {
632 repl->hook_entry[c->hooknum-1] = c->head_offset;
633 repl->underflow[c->hooknum-1] = c->foot_offset;
634 }
635
636 /* iterate over rules */
637 list_for_each_entry(r, &c->rules, list) {
638 ret = iptcc_compile_rule(h, repl, r);
639 if (ret < 0)
640 return ret;
641 }
642
643 /* put chain footer in place */
644 foot = (void *)repl->entries + c->foot_offset;
645 foot->e.target_offset = sizeof(STRUCT_ENTRY);
646 foot->e.next_offset = IPTCB_CHAIN_FOOT_SIZE;
647 strcpy(foot->target.target.u.user.name, STANDARD_TARGET);
648 foot->target.target.u.target_size =
649 ALIGN(sizeof(STRUCT_STANDARD_TARGET));
650 /* builtin targets have verdict, others return */
651 if (iptcc_is_builtin(c))
652 foot->target.verdict = c->verdict;
653 else
654 foot->target.verdict = RETURN;
655 /* set policy-counters */
656 memcpy(&foot->e.counters, &c->counters, sizeof(STRUCT_COUNTERS));
657
658 return 0;
659}
660
661/* calculate offset and number for every rule in the cache */
662static int iptcc_compile_chain_offsets(TC_HANDLE_T h, struct chain_head *c,
663 int *offset, int *num)
664{
665 struct rule_head *r;
666
667 c->head_offset = *offset;
668 DEBUGP("%s: chain_head %u, offset=%u\n", c->name, *num, *offset);
669
670 if (!iptcc_is_builtin(c)) {
671 /* Chain has header */
672 *offset += sizeof(STRUCT_ENTRY)
673 + ALIGN(sizeof(struct ipt_error_target));
674 (*num)++;
675 }
676
677 list_for_each_entry(r, &c->rules, list) {
678 DEBUGP("rule %u, offset=%u, index=%u\n", *num, *offset, *num);
679 r->offset = *offset;
680 r->index = *num;
681 *offset += r->size;
682 (*num)++;
683 }
684
685 DEBUGP("%s; chain_foot %u, offset=%u, index=%u\n", c->name, *num,
686 *offset, *num);
687 c->foot_offset = *offset;
688 c->foot_index = *num;
689 *offset += sizeof(STRUCT_ENTRY)
690 + ALIGN(sizeof(STRUCT_STANDARD_TARGET));
691 (*num)++;
692
693 return 1;
694}
695
696/* put the pieces back together again */
697static int iptcc_compile_table_prep(TC_HANDLE_T h, unsigned int *size)
698{
699 struct chain_head *c;
700 unsigned int offset = 0, num = 0;
701 int ret = 0;
702
703 /* First pass: calculate offset for every rule */
704 list_for_each_entry(c, &h->chains, list) {
705 ret = iptcc_compile_chain_offsets(h, c, &offset, &num);
706 if (ret < 0)
707 return ret;
708 }
709
710 /* Append one error rule at end of chain */
711 num++;
712 offset += sizeof(STRUCT_ENTRY)
713 + ALIGN(sizeof(struct ipt_error_target));
714
715 /* ruleset size is now in offset */
716 *size = offset;
717 return num;
718}
719
720static int iptcc_compile_table(TC_HANDLE_T h, STRUCT_REPLACE *repl)
721{
722 struct chain_head *c;
723 struct iptcb_chain_error *error;
724
725 /* Second pass: copy from cache to offsets, fill in jumps */
726 list_for_each_entry(c, &h->chains, list) {
727 int ret = iptcc_compile_chain(h, repl, c);
728 if (ret < 0)
729 return ret;
730 }
731
732 /* Append error rule at end of chain */
733 error = (void *)repl->entries + repl->size - IPTCB_CHAIN_ERROR_SIZE;
734 error->entry.target_offset = sizeof(STRUCT_ENTRY);
735 error->entry.next_offset = IPTCB_CHAIN_ERROR_SIZE;
736 error->target.t.u.user.target_size =
737 ALIGN(sizeof(struct ipt_error_target));
738 strcpy((char *)&error->target.t.u.user.name, ERROR_TARGET);
739 strcpy((char *)&error->target.error, "ERROR");
740
741 return 1;
742}
743
744/**********************************************************************
745 * EXTERNAL API (operates on cache only)
746 **********************************************************************/
Marc Bouchere6869a82000-03-20 06:03:29 +0000747
748/* Allocate handle of given size */
Rusty Russell79dee072000-05-02 16:45:16 +0000749static TC_HANDLE_T
Harald Welte0113fe72004-01-06 19:04:02 +0000750alloc_handle(const char *tablename, unsigned int size, unsigned int num_rules)
Marc Bouchere6869a82000-03-20 06:03:29 +0000751{
752 size_t len;
Rusty Russell79dee072000-05-02 16:45:16 +0000753 TC_HANDLE_T h;
Marc Bouchere6869a82000-03-20 06:03:29 +0000754
Harald Welteaae69be2004-08-29 23:32:14 +0000755 len = sizeof(STRUCT_TC_HANDLE) + size;
Marc Bouchere6869a82000-03-20 06:03:29 +0000756
Harald Welteaae69be2004-08-29 23:32:14 +0000757 h = malloc(sizeof(STRUCT_TC_HANDLE));
758 if (!h) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000759 errno = ENOMEM;
760 return NULL;
761 }
Harald Welteaae69be2004-08-29 23:32:14 +0000762 memset(h, 0, sizeof(*h));
763 INIT_LIST_HEAD(&h->chains);
Marc Bouchere6869a82000-03-20 06:03:29 +0000764 strcpy(h->info.name, tablename);
Harald Welteaae69be2004-08-29 23:32:14 +0000765
Harald Welte0371c0c2004-09-19 21:00:12 +0000766 h->entries = malloc(sizeof(STRUCT_GET_ENTRIES) + size);
Harald Welteaae69be2004-08-29 23:32:14 +0000767 if (!h->entries)
768 goto out_free_handle;
769
770 strcpy(h->entries->name, tablename);
Harald Welte0371c0c2004-09-19 21:00:12 +0000771 h->entries->size = size;
Marc Bouchere6869a82000-03-20 06:03:29 +0000772
773 return h;
Harald Welteaae69be2004-08-29 23:32:14 +0000774
775out_free_handle:
776 free(h);
777
778 return NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +0000779}
780
Harald Welteaae69be2004-08-29 23:32:14 +0000781
Rusty Russell79dee072000-05-02 16:45:16 +0000782TC_HANDLE_T
783TC_INIT(const char *tablename)
Marc Bouchere6869a82000-03-20 06:03:29 +0000784{
Rusty Russell79dee072000-05-02 16:45:16 +0000785 TC_HANDLE_T h;
786 STRUCT_GETINFO info;
Marc Bouchere6869a82000-03-20 06:03:29 +0000787 int tmp;
788 socklen_t s;
789
Rusty Russell79dee072000-05-02 16:45:16 +0000790 iptc_fn = TC_INIT;
Marc Bouchere6869a82000-03-20 06:03:29 +0000791
Martin Josefsson841e4ae2003-05-02 15:30:11 +0000792 if (strlen(tablename) >= TABLE_MAXNAMELEN) {
793 errno = EINVAL;
794 return NULL;
795 }
796
Derrik Pates664c0a32005-02-01 13:28:14 +0000797 if (sockfd_use == 0) {
798 sockfd = socket(TC_AF, SOCK_RAW, IPPROTO_RAW);
799 if (sockfd < 0)
800 return NULL;
801 }
802 sockfd_use++;
Marc Bouchere6869a82000-03-20 06:03:29 +0000803
804 s = sizeof(info);
Martin Josefsson841e4ae2003-05-02 15:30:11 +0000805
Marc Bouchere6869a82000-03-20 06:03:29 +0000806 strcpy(info.name, tablename);
Derrik Pates664c0a32005-02-01 13:28:14 +0000807 if (getsockopt(sockfd, TC_IPPROTO, SO_GET_INFO, &info, &s) < 0) {
808 if (--sockfd_use == 0) {
809 close(sockfd);
810 sockfd = -1;
811 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000812 return NULL;
Derrik Pates664c0a32005-02-01 13:28:14 +0000813 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000814
Harald Welteaae69be2004-08-29 23:32:14 +0000815 DEBUGP("valid_hooks=0x%08x, num_entries=%u, size=%u\n",
816 info.valid_hooks, info.num_entries, info.size);
817
Harald Welte0113fe72004-01-06 19:04:02 +0000818 if ((h = alloc_handle(info.name, info.size, info.num_entries))
Martin Josefsson841e4ae2003-05-02 15:30:11 +0000819 == NULL) {
Derrik Pates664c0a32005-02-01 13:28:14 +0000820 if (--sockfd_use == 0) {
821 close(sockfd);
822 sockfd = -1;
823 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000824 return NULL;
Martin Josefsson841e4ae2003-05-02 15:30:11 +0000825 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000826
Marc Bouchere6869a82000-03-20 06:03:29 +0000827 /* Initialize current state */
828 h->info = info;
Harald Welte0113fe72004-01-06 19:04:02 +0000829
Harald Welteaae69be2004-08-29 23:32:14 +0000830 h->entries->size = h->info.size;
Marc Bouchere6869a82000-03-20 06:03:29 +0000831
Rusty Russell79dee072000-05-02 16:45:16 +0000832 tmp = sizeof(STRUCT_GET_ENTRIES) + h->info.size;
Marc Bouchere6869a82000-03-20 06:03:29 +0000833
Harald Welteaae69be2004-08-29 23:32:14 +0000834 if (getsockopt(sockfd, TC_IPPROTO, SO_GET_ENTRIES, h->entries,
835 &tmp) < 0)
836 goto error;
837
838#ifdef IPTC_DEBUG2
839 {
840 int fd = open("/tmp/libiptc-so_get_entries.blob",
841 O_CREAT|O_WRONLY);
842 if (fd >= 0) {
843 write(fd, h->entries, tmp);
844 close(fd);
845 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000846 }
Harald Welteaae69be2004-08-29 23:32:14 +0000847#endif
848
849 if (parse_table(h) < 0)
850 goto error;
Rusty Russell7e53bf92000-03-20 07:03:28 +0000851
Marc Bouchere6869a82000-03-20 06:03:29 +0000852 CHECK(h);
853 return h;
Harald Welteaae69be2004-08-29 23:32:14 +0000854error:
Derrik Pates664c0a32005-02-01 13:28:14 +0000855 if (--sockfd_use == 0) {
856 close(sockfd);
857 sockfd = -1;
858 }
Harald Welteaae69be2004-08-29 23:32:14 +0000859 TC_FREE(&h);
860 return NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +0000861}
862
Martin Josefsson841e4ae2003-05-02 15:30:11 +0000863void
864TC_FREE(TC_HANDLE_T *h)
865{
Harald Welteaae69be2004-08-29 23:32:14 +0000866 struct chain_head *c, *tmp;
867
Derrik Pates664c0a32005-02-01 13:28:14 +0000868 iptc_fn = TC_FREE;
869 if (--sockfd_use == 0) {
870 close(sockfd);
871 sockfd = -1;
872 }
Harald Welteaae69be2004-08-29 23:32:14 +0000873
874 list_for_each_entry_safe(c, tmp, &(*h)->chains, list) {
875 struct rule_head *r, *rtmp;
876
877 list_for_each_entry_safe(r, rtmp, &c->rules, list) {
878 free(r);
879 }
880
881 free(c);
882 }
883
884 free((*h)->entries);
Martin Josefsson841e4ae2003-05-02 15:30:11 +0000885 free(*h);
Harald Welteaae69be2004-08-29 23:32:14 +0000886
Martin Josefsson841e4ae2003-05-02 15:30:11 +0000887 *h = NULL;
888}
889
Marc Bouchere6869a82000-03-20 06:03:29 +0000890static inline int
Rusty Russell79dee072000-05-02 16:45:16 +0000891print_match(const STRUCT_ENTRY_MATCH *m)
Marc Bouchere6869a82000-03-20 06:03:29 +0000892{
Rusty Russell228e98d2000-04-27 10:28:06 +0000893 printf("Match name: `%s'\n", m->u.user.name);
Marc Bouchere6869a82000-03-20 06:03:29 +0000894 return 0;
895}
896
Rusty Russell79dee072000-05-02 16:45:16 +0000897static int dump_entry(STRUCT_ENTRY *e, const TC_HANDLE_T handle);
898
Marc Bouchere6869a82000-03-20 06:03:29 +0000899void
Rusty Russell79dee072000-05-02 16:45:16 +0000900TC_DUMP_ENTRIES(const TC_HANDLE_T handle)
Marc Bouchere6869a82000-03-20 06:03:29 +0000901{
Derrik Pates664c0a32005-02-01 13:28:14 +0000902 iptc_fn = TC_DUMP_ENTRIES;
Marc Bouchere6869a82000-03-20 06:03:29 +0000903 CHECK(handle);
Harald Welteaae69be2004-08-29 23:32:14 +0000904#if 0
Rusty Russelle45c7132004-12-16 13:21:44 +0000905 printf("libiptc v%s. %u bytes.\n",
906 IPTABLES_VERSION, handle->entries->size);
Marc Bouchere6869a82000-03-20 06:03:29 +0000907 printf("Table `%s'\n", handle->info.name);
908 printf("Hooks: pre/in/fwd/out/post = %u/%u/%u/%u/%u\n",
Rusty Russell67088e72000-05-10 01:18:57 +0000909 handle->info.hook_entry[HOOK_PRE_ROUTING],
910 handle->info.hook_entry[HOOK_LOCAL_IN],
911 handle->info.hook_entry[HOOK_FORWARD],
912 handle->info.hook_entry[HOOK_LOCAL_OUT],
913 handle->info.hook_entry[HOOK_POST_ROUTING]);
Marc Bouchere6869a82000-03-20 06:03:29 +0000914 printf("Underflows: pre/in/fwd/out/post = %u/%u/%u/%u/%u\n",
Rusty Russell67088e72000-05-10 01:18:57 +0000915 handle->info.underflow[HOOK_PRE_ROUTING],
916 handle->info.underflow[HOOK_LOCAL_IN],
917 handle->info.underflow[HOOK_FORWARD],
918 handle->info.underflow[HOOK_LOCAL_OUT],
919 handle->info.underflow[HOOK_POST_ROUTING]);
Marc Bouchere6869a82000-03-20 06:03:29 +0000920
Harald Welteaae69be2004-08-29 23:32:14 +0000921 ENTRY_ITERATE(handle->entries->entrytable, handle->entries->size,
Rusty Russell79dee072000-05-02 16:45:16 +0000922 dump_entry, handle);
Harald Welteaae69be2004-08-29 23:32:14 +0000923#endif
Harald Welte0113fe72004-01-06 19:04:02 +0000924}
Rusty Russell30fd6e52000-04-23 09:16:06 +0000925
Marc Bouchere6869a82000-03-20 06:03:29 +0000926/* Does this chain exist? */
Rusty Russell79dee072000-05-02 16:45:16 +0000927int TC_IS_CHAIN(const char *chain, const TC_HANDLE_T handle)
Marc Bouchere6869a82000-03-20 06:03:29 +0000928{
Derrik Pates664c0a32005-02-01 13:28:14 +0000929 iptc_fn = TC_IS_CHAIN;
Harald Welteaae69be2004-08-29 23:32:14 +0000930 return iptcc_find_label(chain, handle) != NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +0000931}
932
Harald Welteaae69be2004-08-29 23:32:14 +0000933static void iptcc_chain_iterator_advance(TC_HANDLE_T handle)
934{
935 struct chain_head *c = handle->chain_iterator_cur;
936
937 if (c->list.next == &handle->chains)
938 handle->chain_iterator_cur = NULL;
939 else
940 handle->chain_iterator_cur =
941 list_entry(c->list.next, struct chain_head, list);
942}
Marc Bouchere6869a82000-03-20 06:03:29 +0000943
Rusty Russell30fd6e52000-04-23 09:16:06 +0000944/* Iterator functions to run through the chains. */
Marc Bouchere6869a82000-03-20 06:03:29 +0000945const char *
Philip Blundell8c700902000-05-15 02:17:52 +0000946TC_FIRST_CHAIN(TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +0000947{
Harald Welteaae69be2004-08-29 23:32:14 +0000948 struct chain_head *c = list_entry((*handle)->chains.next,
949 struct chain_head, list);
950
951 iptc_fn = TC_FIRST_CHAIN;
952
953
954 if (list_empty(&(*handle)->chains)) {
955 DEBUGP(": no chains\n");
Harald Welte0113fe72004-01-06 19:04:02 +0000956 return NULL;
Harald Welteaae69be2004-08-29 23:32:14 +0000957 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000958
Harald Welteaae69be2004-08-29 23:32:14 +0000959 (*handle)->chain_iterator_cur = c;
960 iptcc_chain_iterator_advance(*handle);
Harald Welte0113fe72004-01-06 19:04:02 +0000961
Harald Welteaae69be2004-08-29 23:32:14 +0000962 DEBUGP(": returning `%s'\n", c->name);
963 return c->name;
Marc Bouchere6869a82000-03-20 06:03:29 +0000964}
965
Rusty Russell30fd6e52000-04-23 09:16:06 +0000966/* Iterator functions to run through the chains. Returns NULL at end. */
967const char *
Rusty Russell79dee072000-05-02 16:45:16 +0000968TC_NEXT_CHAIN(TC_HANDLE_T *handle)
Rusty Russell30fd6e52000-04-23 09:16:06 +0000969{
Harald Welteaae69be2004-08-29 23:32:14 +0000970 struct chain_head *c = (*handle)->chain_iterator_cur;
Rusty Russell30fd6e52000-04-23 09:16:06 +0000971
Harald Welteaae69be2004-08-29 23:32:14 +0000972 iptc_fn = TC_NEXT_CHAIN;
973
974 if (!c) {
975 DEBUGP(": no more chains\n");
Rusty Russell30fd6e52000-04-23 09:16:06 +0000976 return NULL;
Harald Welteaae69be2004-08-29 23:32:14 +0000977 }
Rusty Russell30fd6e52000-04-23 09:16:06 +0000978
Harald Welteaae69be2004-08-29 23:32:14 +0000979 iptcc_chain_iterator_advance(*handle);
980
981 DEBUGP(": returning `%s'\n", c->name);
982 return c->name;
Rusty Russell30fd6e52000-04-23 09:16:06 +0000983}
984
985/* Get first rule in the given chain: NULL for empty chain. */
Rusty Russell79dee072000-05-02 16:45:16 +0000986const STRUCT_ENTRY *
Philip Blundell8c700902000-05-15 02:17:52 +0000987TC_FIRST_RULE(const char *chain, TC_HANDLE_T *handle)
Rusty Russell30fd6e52000-04-23 09:16:06 +0000988{
Harald Welteaae69be2004-08-29 23:32:14 +0000989 struct chain_head *c;
990 struct rule_head *r;
Rusty Russell30fd6e52000-04-23 09:16:06 +0000991
Harald Welteaae69be2004-08-29 23:32:14 +0000992 iptc_fn = TC_FIRST_RULE;
993
994 DEBUGP("first rule(%s): ", chain);
995
996 c = iptcc_find_label(chain, *handle);
Rusty Russell30fd6e52000-04-23 09:16:06 +0000997 if (!c) {
998 errno = ENOENT;
999 return NULL;
1000 }
1001
1002 /* Empty chain: single return/policy rule */
Harald Welteaae69be2004-08-29 23:32:14 +00001003 if (list_empty(&c->rules)) {
1004 DEBUGP_C("no rules, returning NULL\n");
Rusty Russell30fd6e52000-04-23 09:16:06 +00001005 return NULL;
Harald Welteaae69be2004-08-29 23:32:14 +00001006 }
Rusty Russell30fd6e52000-04-23 09:16:06 +00001007
Harald Welteaae69be2004-08-29 23:32:14 +00001008 r = list_entry(c->rules.next, struct rule_head, list);
1009 (*handle)->rule_iterator_cur = r;
1010 DEBUGP_C("%p\n", r);
1011
1012 return r->entry;
Rusty Russell30fd6e52000-04-23 09:16:06 +00001013}
1014
1015/* Returns NULL when rules run out. */
Rusty Russell79dee072000-05-02 16:45:16 +00001016const STRUCT_ENTRY *
Philip Blundell8c700902000-05-15 02:17:52 +00001017TC_NEXT_RULE(const STRUCT_ENTRY *prev, TC_HANDLE_T *handle)
Rusty Russell30fd6e52000-04-23 09:16:06 +00001018{
Harald Welteaae69be2004-08-29 23:32:14 +00001019 struct rule_head *r;
Rusty Russell30fd6e52000-04-23 09:16:06 +00001020
Derrik Pates664c0a32005-02-01 13:28:14 +00001021 iptc_fn = TC_NEXT_RULE;
Harald Welteaae69be2004-08-29 23:32:14 +00001022 DEBUGP("rule_iterator_cur=%p...", (*handle)->rule_iterator_cur);
1023
1024 if (!(*handle)->rule_iterator_cur) {
1025 DEBUGP_C("returning NULL\n");
1026 return NULL;
1027 }
1028
1029 r = list_entry((*handle)->rule_iterator_cur->list.next,
1030 struct rule_head, list);
1031
1032 iptc_fn = TC_NEXT_RULE;
1033
1034 DEBUGP_C("next=%p, head=%p...", &r->list,
1035 &(*handle)->rule_iterator_cur->chain->rules);
1036
1037 if (&r->list == &(*handle)->rule_iterator_cur->chain->rules) {
1038 (*handle)->rule_iterator_cur = NULL;
1039 DEBUGP_C("finished, returning NULL\n");
1040 return NULL;
1041 }
1042
1043 (*handle)->rule_iterator_cur = r;
1044
1045 /* NOTE: prev is without any influence ! */
1046 DEBUGP_C("returning rule %p\n", r);
1047 return r->entry;
Rusty Russell30fd6e52000-04-23 09:16:06 +00001048}
1049
Marc Bouchere6869a82000-03-20 06:03:29 +00001050/* How many rules in this chain? */
1051unsigned int
Rusty Russell79dee072000-05-02 16:45:16 +00001052TC_NUM_RULES(const char *chain, TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001053{
Harald Welteaae69be2004-08-29 23:32:14 +00001054 struct chain_head *c;
1055 iptc_fn = TC_NUM_RULES;
Marc Bouchere6869a82000-03-20 06:03:29 +00001056 CHECK(*handle);
Harald Welteaae69be2004-08-29 23:32:14 +00001057
1058 c = iptcc_find_label(chain, *handle);
1059 if (!c) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001060 errno = ENOENT;
1061 return (unsigned int)-1;
1062 }
Harald Welteaae69be2004-08-29 23:32:14 +00001063
1064 return c->num_rules;
Marc Bouchere6869a82000-03-20 06:03:29 +00001065}
1066
Rusty Russell79dee072000-05-02 16:45:16 +00001067const STRUCT_ENTRY *TC_GET_RULE(const char *chain,
1068 unsigned int n,
1069 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001070{
Harald Welteaae69be2004-08-29 23:32:14 +00001071 struct chain_head *c;
1072 struct rule_head *r;
1073
1074 iptc_fn = TC_GET_RULE;
Marc Bouchere6869a82000-03-20 06:03:29 +00001075
1076 CHECK(*handle);
Harald Welteaae69be2004-08-29 23:32:14 +00001077
1078 c = iptcc_find_label(chain, *handle);
1079 if (!c) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001080 errno = ENOENT;
1081 return NULL;
1082 }
1083
Harald Welteaae69be2004-08-29 23:32:14 +00001084 r = iptcc_get_rule_num(c, n);
1085 if (!r)
1086 return NULL;
1087 return r->entry;
Marc Bouchere6869a82000-03-20 06:03:29 +00001088}
1089
1090/* Returns a pointer to the target name of this position. */
Harald Welteaae69be2004-08-29 23:32:14 +00001091const char *standard_target_map(int verdict)
Marc Bouchere6869a82000-03-20 06:03:29 +00001092{
Harald Welteaae69be2004-08-29 23:32:14 +00001093 switch (verdict) {
1094 case RETURN:
1095 return LABEL_RETURN;
1096 break;
1097 case -NF_ACCEPT-1:
1098 return LABEL_ACCEPT;
1099 break;
1100 case -NF_DROP-1:
1101 return LABEL_DROP;
1102 break;
1103 case -NF_QUEUE-1:
1104 return LABEL_QUEUE;
1105 break;
1106 default:
1107 fprintf(stderr, "ERROR: %d not a valid target)\n",
1108 verdict);
1109 abort();
1110 break;
1111 }
1112 /* not reached */
1113 return NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +00001114}
1115
Harald Welteaae69be2004-08-29 23:32:14 +00001116/* Returns a pointer to the target name of this position. */
1117const char *TC_GET_TARGET(const STRUCT_ENTRY *ce,
1118 TC_HANDLE_T *handle)
1119{
1120 STRUCT_ENTRY *e = (STRUCT_ENTRY *)ce;
Rusty Russelle45c7132004-12-16 13:21:44 +00001121 struct rule_head *r = container_of(e, struct rule_head, entry[0]);
Harald Welteaae69be2004-08-29 23:32:14 +00001122
1123 iptc_fn = TC_GET_TARGET;
1124
1125 switch(r->type) {
1126 int spos;
1127 case IPTCC_R_FALLTHROUGH:
1128 return "";
1129 break;
1130 case IPTCC_R_JUMP:
1131 DEBUGP("r=%p, jump=%p, name=`%s'\n", r, r->jump, r->jump->name);
1132 return r->jump->name;
1133 break;
1134 case IPTCC_R_STANDARD:
1135 spos = *(int *)GET_TARGET(e)->data;
1136 DEBUGP("r=%p, spos=%d'\n", r, spos);
1137 return standard_target_map(spos);
1138 break;
1139 case IPTCC_R_MODULE:
1140 return GET_TARGET(e)->u.user.name;
1141 break;
1142 }
1143 return NULL;
1144}
Marc Bouchere6869a82000-03-20 06:03:29 +00001145/* Is this a built-in chain? Actually returns hook + 1. */
1146int
Rusty Russell79dee072000-05-02 16:45:16 +00001147TC_BUILTIN(const char *chain, const TC_HANDLE_T handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001148{
Harald Welteaae69be2004-08-29 23:32:14 +00001149 struct chain_head *c;
1150
1151 iptc_fn = TC_BUILTIN;
Marc Bouchere6869a82000-03-20 06:03:29 +00001152
Harald Welteaae69be2004-08-29 23:32:14 +00001153 c = iptcc_find_label(chain, handle);
1154 if (!c) {
1155 errno = ENOENT;
Martin Josefssonb0f3d2d2004-09-23 18:23:20 +00001156 return 0;
Marc Bouchere6869a82000-03-20 06:03:29 +00001157 }
Harald Welteaae69be2004-08-29 23:32:14 +00001158
1159 return iptcc_is_builtin(c);
Marc Bouchere6869a82000-03-20 06:03:29 +00001160}
1161
1162/* Get the policy of a given built-in chain */
1163const char *
Rusty Russell79dee072000-05-02 16:45:16 +00001164TC_GET_POLICY(const char *chain,
1165 STRUCT_COUNTERS *counters,
1166 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001167{
Harald Welteaae69be2004-08-29 23:32:14 +00001168 struct chain_head *c;
Marc Bouchere6869a82000-03-20 06:03:29 +00001169
Harald Welteaae69be2004-08-29 23:32:14 +00001170 iptc_fn = TC_GET_POLICY;
1171
1172 DEBUGP("called for chain %s\n", chain);
1173
1174 c = iptcc_find_label(chain, *handle);
1175 if (!c) {
1176 errno = ENOENT;
1177 return NULL;
1178 }
1179
1180 if (!iptcc_is_builtin(c))
Marc Bouchere6869a82000-03-20 06:03:29 +00001181 return NULL;
1182
Harald Welteaae69be2004-08-29 23:32:14 +00001183 *counters = c->counters;
Marc Bouchere6869a82000-03-20 06:03:29 +00001184
Harald Welteaae69be2004-08-29 23:32:14 +00001185 return standard_target_map(c->verdict);
Harald Welte0113fe72004-01-06 19:04:02 +00001186}
1187
1188static int
Harald Welteaae69be2004-08-29 23:32:14 +00001189iptcc_standard_map(struct rule_head *r, int verdict)
Harald Welte0113fe72004-01-06 19:04:02 +00001190{
Harald Welteaae69be2004-08-29 23:32:14 +00001191 STRUCT_ENTRY *e = r->entry;
Rusty Russell79dee072000-05-02 16:45:16 +00001192 STRUCT_STANDARD_TARGET *t;
Marc Bouchere6869a82000-03-20 06:03:29 +00001193
Rusty Russell79dee072000-05-02 16:45:16 +00001194 t = (STRUCT_STANDARD_TARGET *)GET_TARGET(e);
Marc Bouchere6869a82000-03-20 06:03:29 +00001195
Rusty Russell67088e72000-05-10 01:18:57 +00001196 if (t->target.u.target_size
Philip Blundell8c700902000-05-15 02:17:52 +00001197 != ALIGN(sizeof(STRUCT_STANDARD_TARGET))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001198 errno = EINVAL;
1199 return 0;
1200 }
1201 /* memset for memcmp convenience on delete/replace */
Rusty Russell79dee072000-05-02 16:45:16 +00001202 memset(t->target.u.user.name, 0, FUNCTION_MAXNAMELEN);
1203 strcpy(t->target.u.user.name, STANDARD_TARGET);
Marc Bouchere6869a82000-03-20 06:03:29 +00001204 t->verdict = verdict;
1205
Harald Welteaae69be2004-08-29 23:32:14 +00001206 r->type = IPTCC_R_STANDARD;
1207
Marc Bouchere6869a82000-03-20 06:03:29 +00001208 return 1;
1209}
Rusty Russell7e53bf92000-03-20 07:03:28 +00001210
Marc Bouchere6869a82000-03-20 06:03:29 +00001211static int
Harald Welteaae69be2004-08-29 23:32:14 +00001212iptcc_map_target(const TC_HANDLE_T handle,
1213 struct rule_head *r)
Marc Bouchere6869a82000-03-20 06:03:29 +00001214{
Harald Welteaae69be2004-08-29 23:32:14 +00001215 STRUCT_ENTRY *e = r->entry;
Harald Welte0113fe72004-01-06 19:04:02 +00001216 STRUCT_ENTRY_TARGET *t = GET_TARGET(e);
Marc Bouchere6869a82000-03-20 06:03:29 +00001217
Marc Bouchere6869a82000-03-20 06:03:29 +00001218 /* Maybe it's empty (=> fall through) */
Harald Welteaae69be2004-08-29 23:32:14 +00001219 if (strcmp(t->u.user.name, "") == 0) {
1220 r->type = IPTCC_R_FALLTHROUGH;
1221 return 1;
1222 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001223 /* Maybe it's a standard target name... */
Rusty Russell79dee072000-05-02 16:45:16 +00001224 else if (strcmp(t->u.user.name, LABEL_ACCEPT) == 0)
Harald Welteaae69be2004-08-29 23:32:14 +00001225 return iptcc_standard_map(r, -NF_ACCEPT - 1);
Rusty Russell79dee072000-05-02 16:45:16 +00001226 else if (strcmp(t->u.user.name, LABEL_DROP) == 0)
Harald Welteaae69be2004-08-29 23:32:14 +00001227 return iptcc_standard_map(r, -NF_DROP - 1);
Rusty Russell67088e72000-05-10 01:18:57 +00001228 else if (strcmp(t->u.user.name, LABEL_QUEUE) == 0)
Harald Welteaae69be2004-08-29 23:32:14 +00001229 return iptcc_standard_map(r, -NF_QUEUE - 1);
Rusty Russell79dee072000-05-02 16:45:16 +00001230 else if (strcmp(t->u.user.name, LABEL_RETURN) == 0)
Harald Welteaae69be2004-08-29 23:32:14 +00001231 return iptcc_standard_map(r, RETURN);
Rusty Russell79dee072000-05-02 16:45:16 +00001232 else if (TC_BUILTIN(t->u.user.name, handle)) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001233 /* Can't jump to builtins. */
1234 errno = EINVAL;
1235 return 0;
1236 } else {
1237 /* Maybe it's an existing chain name. */
Harald Welteaae69be2004-08-29 23:32:14 +00001238 struct chain_head *c;
1239 DEBUGP("trying to find chain `%s': ", t->u.user.name);
Marc Bouchere6869a82000-03-20 06:03:29 +00001240
Harald Welteaae69be2004-08-29 23:32:14 +00001241 c = iptcc_find_label(t->u.user.name, handle);
1242 if (c) {
1243 DEBUGP_C("found!\n");
1244 r->type = IPTCC_R_JUMP;
1245 r->jump = c;
1246 c->references++;
1247 return 1;
1248 }
1249 DEBUGP_C("not found :(\n");
Marc Bouchere6869a82000-03-20 06:03:29 +00001250 }
1251
1252 /* Must be a module? If not, kernel will reject... */
Rusty Russell3aef54d2005-01-03 03:48:40 +00001253 /* memset to all 0 for your memcmp convenience: don't clear version */
Rusty Russell228e98d2000-04-27 10:28:06 +00001254 memset(t->u.user.name + strlen(t->u.user.name),
Marc Bouchere6869a82000-03-20 06:03:29 +00001255 0,
Rusty Russell3aef54d2005-01-03 03:48:40 +00001256 FUNCTION_MAXNAMELEN - 1 - strlen(t->u.user.name));
Rusty Russell733e54b2004-12-16 14:22:23 +00001257 r->type = IPTCC_R_MODULE;
Harald Welteaae69be2004-08-29 23:32:14 +00001258 set_changed(handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001259 return 1;
1260}
1261
Harald Welte0113fe72004-01-06 19:04:02 +00001262/* Insert the entry `fw' in chain `chain' into position `rulenum'. */
Marc Bouchere6869a82000-03-20 06:03:29 +00001263int
Rusty Russell79dee072000-05-02 16:45:16 +00001264TC_INSERT_ENTRY(const IPT_CHAINLABEL chain,
1265 const STRUCT_ENTRY *e,
1266 unsigned int rulenum,
1267 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001268{
Harald Welteaae69be2004-08-29 23:32:14 +00001269 struct chain_head *c;
Martin Josefssoneb066cc2004-09-22 21:04:07 +00001270 struct rule_head *r;
1271 struct list_head *prev;
Marc Bouchere6869a82000-03-20 06:03:29 +00001272
Rusty Russell79dee072000-05-02 16:45:16 +00001273 iptc_fn = TC_INSERT_ENTRY;
Harald Welteaae69be2004-08-29 23:32:14 +00001274
1275 if (!(c = iptcc_find_label(chain, *handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001276 errno = ENOENT;
1277 return 0;
1278 }
1279
Martin Josefssoneb066cc2004-09-22 21:04:07 +00001280 /* first rulenum index = 0
1281 first c->num_rules index = 1 */
1282 if (rulenum > c->num_rules) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001283 errno = E2BIG;
1284 return 0;
1285 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001286
Martin Josefsson631f3612004-09-23 19:25:06 +00001287 /* If we are inserting at the end just take advantage of the
1288 double linked list, insert will happen before the entry
1289 prev points to. */
1290 if (rulenum == c->num_rules) {
Martin Josefssoneb066cc2004-09-22 21:04:07 +00001291 prev = &c->rules;
Martin Josefssona5616dc2004-10-24 22:27:31 +00001292 } else if (rulenum + 1 <= c->num_rules/2) {
Martin Josefsson631f3612004-09-23 19:25:06 +00001293 r = iptcc_get_rule_num(c, rulenum + 1);
Martin Josefssona5616dc2004-10-24 22:27:31 +00001294 prev = &r->list;
1295 } else {
1296 r = iptcc_get_rule_num_reverse(c, c->num_rules - rulenum);
Martin Josefsson631f3612004-09-23 19:25:06 +00001297 prev = &r->list;
1298 }
Martin Josefssoneb066cc2004-09-22 21:04:07 +00001299
Harald Welteaae69be2004-08-29 23:32:14 +00001300 if (!(r = iptcc_alloc_rule(c, e->next_offset))) {
1301 errno = ENOMEM;
Harald Welte0113fe72004-01-06 19:04:02 +00001302 return 0;
Harald Welteaae69be2004-08-29 23:32:14 +00001303 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001304
Harald Welteaae69be2004-08-29 23:32:14 +00001305 memcpy(r->entry, e, e->next_offset);
1306 r->counter_map.maptype = COUNTER_MAP_SET;
1307
1308 if (!iptcc_map_target(*handle, r)) {
1309 free(r);
1310 return 0;
1311 }
1312
Martin Josefssoneb066cc2004-09-22 21:04:07 +00001313 list_add_tail(&r->list, prev);
Harald Welteaae69be2004-08-29 23:32:14 +00001314 c->num_rules++;
1315
1316 set_changed(*handle);
1317
1318 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00001319}
1320
1321/* Atomically replace rule `rulenum' in `chain' with `fw'. */
1322int
Rusty Russell79dee072000-05-02 16:45:16 +00001323TC_REPLACE_ENTRY(const IPT_CHAINLABEL chain,
1324 const STRUCT_ENTRY *e,
1325 unsigned int rulenum,
1326 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001327{
Harald Welteaae69be2004-08-29 23:32:14 +00001328 struct chain_head *c;
1329 struct rule_head *r, *old;
Marc Bouchere6869a82000-03-20 06:03:29 +00001330
Rusty Russell79dee072000-05-02 16:45:16 +00001331 iptc_fn = TC_REPLACE_ENTRY;
Marc Bouchere6869a82000-03-20 06:03:29 +00001332
Harald Welteaae69be2004-08-29 23:32:14 +00001333 if (!(c = iptcc_find_label(chain, *handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001334 errno = ENOENT;
1335 return 0;
1336 }
1337
Martin Josefsson0f9b8b12004-12-18 17:18:49 +00001338 if (rulenum >= c->num_rules) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001339 errno = E2BIG;
1340 return 0;
1341 }
1342
Martin Josefsson0f9b8b12004-12-18 17:18:49 +00001343 /* Take advantage of the double linked list if possible. */
1344 if (rulenum + 1 <= c->num_rules/2) {
1345 old = iptcc_get_rule_num(c, rulenum + 1);
1346 } else {
1347 old = iptcc_get_rule_num_reverse(c, c->num_rules - rulenum);
1348 }
1349
Harald Welteaae69be2004-08-29 23:32:14 +00001350 if (!(r = iptcc_alloc_rule(c, e->next_offset))) {
1351 errno = ENOMEM;
Marc Bouchere6869a82000-03-20 06:03:29 +00001352 return 0;
Harald Welteaae69be2004-08-29 23:32:14 +00001353 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001354
Harald Welteaae69be2004-08-29 23:32:14 +00001355 memcpy(r->entry, e, e->next_offset);
1356 r->counter_map.maptype = COUNTER_MAP_SET;
1357
1358 if (!iptcc_map_target(*handle, r)) {
1359 free(r);
Harald Welte0113fe72004-01-06 19:04:02 +00001360 return 0;
Harald Welteaae69be2004-08-29 23:32:14 +00001361 }
Harald Welte0113fe72004-01-06 19:04:02 +00001362
Harald Welteaae69be2004-08-29 23:32:14 +00001363 list_add(&r->list, &old->list);
1364 iptcc_delete_rule(old);
1365
1366 set_changed(*handle);
1367
1368 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00001369}
1370
Harald Welte0113fe72004-01-06 19:04:02 +00001371/* Append entry `fw' to chain `chain'. Equivalent to insert with
Marc Bouchere6869a82000-03-20 06:03:29 +00001372 rulenum = length of chain. */
1373int
Rusty Russell79dee072000-05-02 16:45:16 +00001374TC_APPEND_ENTRY(const IPT_CHAINLABEL chain,
1375 const STRUCT_ENTRY *e,
1376 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001377{
Harald Welteaae69be2004-08-29 23:32:14 +00001378 struct chain_head *c;
1379 struct rule_head *r;
Marc Bouchere6869a82000-03-20 06:03:29 +00001380
Rusty Russell79dee072000-05-02 16:45:16 +00001381 iptc_fn = TC_APPEND_ENTRY;
Harald Welteaae69be2004-08-29 23:32:14 +00001382 if (!(c = iptcc_find_label(chain, *handle))) {
1383 DEBUGP("unable to find chain `%s'\n", chain);
Marc Bouchere6869a82000-03-20 06:03:29 +00001384 errno = ENOENT;
1385 return 0;
1386 }
1387
Harald Welteaae69be2004-08-29 23:32:14 +00001388 if (!(r = iptcc_alloc_rule(c, e->next_offset))) {
1389 DEBUGP("unable to allocate rule for chain `%s'\n", chain);
1390 errno = ENOMEM;
Harald Welte0113fe72004-01-06 19:04:02 +00001391 return 0;
Harald Welteaae69be2004-08-29 23:32:14 +00001392 }
Harald Welte0113fe72004-01-06 19:04:02 +00001393
Harald Welteaae69be2004-08-29 23:32:14 +00001394 memcpy(r->entry, e, e->next_offset);
1395 r->counter_map.maptype = COUNTER_MAP_SET;
1396
1397 if (!iptcc_map_target(*handle, r)) {
Martin Josefsson12009532004-09-23 18:24:29 +00001398 DEBUGP("unable to map target of rule for chain `%s'\n", chain);
Harald Welteaae69be2004-08-29 23:32:14 +00001399 free(r);
1400 return 0;
1401 }
1402
1403 list_add_tail(&r->list, &c->rules);
1404 c->num_rules++;
1405
1406 set_changed(*handle);
1407
1408 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00001409}
1410
1411static inline int
Rusty Russell79dee072000-05-02 16:45:16 +00001412match_different(const STRUCT_ENTRY_MATCH *a,
Rusty Russelledf14cf2000-04-19 11:26:44 +00001413 const unsigned char *a_elems,
1414 const unsigned char *b_elems,
1415 unsigned char **maskptr)
Marc Bouchere6869a82000-03-20 06:03:29 +00001416{
Rusty Russell79dee072000-05-02 16:45:16 +00001417 const STRUCT_ENTRY_MATCH *b;
Rusty Russelledf14cf2000-04-19 11:26:44 +00001418 unsigned int i;
Marc Bouchere6869a82000-03-20 06:03:29 +00001419
1420 /* Offset of b is the same as a. */
Rusty Russell30fd6e52000-04-23 09:16:06 +00001421 b = (void *)b_elems + ((unsigned char *)a - a_elems);
Marc Bouchere6869a82000-03-20 06:03:29 +00001422
Rusty Russell228e98d2000-04-27 10:28:06 +00001423 if (a->u.match_size != b->u.match_size)
Marc Bouchere6869a82000-03-20 06:03:29 +00001424 return 1;
1425
Rusty Russell228e98d2000-04-27 10:28:06 +00001426 if (strcmp(a->u.user.name, b->u.user.name) != 0)
Marc Bouchere6869a82000-03-20 06:03:29 +00001427 return 1;
1428
Rusty Russell73ef09b2000-07-03 10:24:04 +00001429 *maskptr += ALIGN(sizeof(*a));
Rusty Russelledf14cf2000-04-19 11:26:44 +00001430
Rusty Russell73ef09b2000-07-03 10:24:04 +00001431 for (i = 0; i < a->u.match_size - ALIGN(sizeof(*a)); i++)
Rusty Russelledf14cf2000-04-19 11:26:44 +00001432 if (((a->data[i] ^ b->data[i]) & (*maskptr)[i]) != 0)
Rusty Russell90e712a2000-03-29 04:19:26 +00001433 return 1;
Rusty Russelledf14cf2000-04-19 11:26:44 +00001434 *maskptr += i;
1435 return 0;
1436}
1437
1438static inline int
Rusty Russell733e54b2004-12-16 14:22:23 +00001439target_same(struct rule_head *a, struct rule_head *b,const unsigned char *mask)
Rusty Russelledf14cf2000-04-19 11:26:44 +00001440{
1441 unsigned int i;
Rusty Russell733e54b2004-12-16 14:22:23 +00001442 STRUCT_ENTRY_TARGET *ta, *tb;
Marc Bouchere6869a82000-03-20 06:03:29 +00001443
Rusty Russell733e54b2004-12-16 14:22:23 +00001444 if (a->type != b->type)
1445 return 0;
1446
1447 ta = GET_TARGET(a->entry);
1448 tb = GET_TARGET(b->entry);
1449
1450 switch (a->type) {
1451 case IPTCC_R_FALLTHROUGH:
1452 return 1;
1453 case IPTCC_R_JUMP:
1454 return a->jump == b->jump;
1455 case IPTCC_R_STANDARD:
1456 return ((STRUCT_STANDARD_TARGET *)ta)->verdict
1457 == ((STRUCT_STANDARD_TARGET *)tb)->verdict;
1458 case IPTCC_R_MODULE:
1459 if (ta->u.target_size != tb->u.target_size)
1460 return 0;
1461 if (strcmp(ta->u.user.name, tb->u.user.name) != 0)
1462 return 0;
1463
1464 for (i = 0; i < ta->u.target_size - sizeof(*ta); i++)
Rusty Russelldaade442004-12-29 11:14:52 +00001465 if (((ta->data[i] ^ tb->data[i]) & mask[i]) != 0)
Rusty Russell733e54b2004-12-16 14:22:23 +00001466 return 0;
1467 return 1;
1468 default:
1469 fprintf(stderr, "ERROR: bad type %i\n", a->type);
1470 abort();
1471 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001472}
1473
Rusty Russell733e54b2004-12-16 14:22:23 +00001474static unsigned char *
Rusty Russell79dee072000-05-02 16:45:16 +00001475is_same(const STRUCT_ENTRY *a,
1476 const STRUCT_ENTRY *b,
1477 unsigned char *matchmask);
Marc Bouchere6869a82000-03-20 06:03:29 +00001478
Harald Welte0113fe72004-01-06 19:04:02 +00001479/* Delete the first rule in `chain' which matches `fw'. */
Marc Bouchere6869a82000-03-20 06:03:29 +00001480int
Rusty Russell79dee072000-05-02 16:45:16 +00001481TC_DELETE_ENTRY(const IPT_CHAINLABEL chain,
1482 const STRUCT_ENTRY *origfw,
1483 unsigned char *matchmask,
1484 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001485{
Harald Welteaae69be2004-08-29 23:32:14 +00001486 struct chain_head *c;
Rusty Russelle45c7132004-12-16 13:21:44 +00001487 struct rule_head *r, *i;
Marc Bouchere6869a82000-03-20 06:03:29 +00001488
Rusty Russell79dee072000-05-02 16:45:16 +00001489 iptc_fn = TC_DELETE_ENTRY;
Harald Welteaae69be2004-08-29 23:32:14 +00001490 if (!(c = iptcc_find_label(chain, *handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001491 errno = ENOENT;
1492 return 0;
1493 }
1494
Rusty Russelle45c7132004-12-16 13:21:44 +00001495 /* Create a rule_head from origfw. */
1496 r = iptcc_alloc_rule(c, origfw->next_offset);
1497 if (!r) {
Harald Welte0113fe72004-01-06 19:04:02 +00001498 errno = ENOMEM;
1499 return 0;
1500 }
1501
Rusty Russelle45c7132004-12-16 13:21:44 +00001502 memcpy(r->entry, origfw, origfw->next_offset);
1503 r->counter_map.maptype = COUNTER_MAP_NOMAP;
1504 if (!iptcc_map_target(*handle, r)) {
1505 DEBUGP("unable to map target of rule for chain `%s'\n", chain);
1506 free(r);
1507 return 0;
1508 }
Harald Welte0113fe72004-01-06 19:04:02 +00001509
Rusty Russelle45c7132004-12-16 13:21:44 +00001510 list_for_each_entry(i, &c->rules, list) {
Rusty Russell733e54b2004-12-16 14:22:23 +00001511 unsigned char *mask;
Harald Weltefe537072004-08-30 20:28:53 +00001512
Rusty Russell733e54b2004-12-16 14:22:23 +00001513 mask = is_same(r->entry, i->entry, matchmask);
1514 if (!mask)
1515 continue;
Martin Josefsson2a5dbbb2004-09-22 21:37:41 +00001516
Rusty Russell733e54b2004-12-16 14:22:23 +00001517 if (!target_same(r, i, mask))
1518 continue;
1519
1520 /* If we are about to delete the rule that is the
1521 * current iterator, move rule iterator back. next
1522 * pointer will then point to real next node */
1523 if (i == (*handle)->rule_iterator_cur) {
1524 (*handle)->rule_iterator_cur =
1525 list_entry((*handle)->rule_iterator_cur->list.prev,
1526 struct rule_head, list);
Marc Bouchere6869a82000-03-20 06:03:29 +00001527 }
Rusty Russell733e54b2004-12-16 14:22:23 +00001528
1529 c->num_rules--;
1530 iptcc_delete_rule(i);
1531
1532 set_changed(*handle);
1533 free(r);
1534 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00001535 }
1536
Rusty Russelle45c7132004-12-16 13:21:44 +00001537 free(r);
Marc Bouchere6869a82000-03-20 06:03:29 +00001538 errno = ENOENT;
1539 return 0;
Rusty Russell7e53bf92000-03-20 07:03:28 +00001540}
Harald Welteaae69be2004-08-29 23:32:14 +00001541
Marc Bouchere6869a82000-03-20 06:03:29 +00001542
1543/* Delete the rule in position `rulenum' in `chain'. */
1544int
Rusty Russell79dee072000-05-02 16:45:16 +00001545TC_DELETE_NUM_ENTRY(const IPT_CHAINLABEL chain,
1546 unsigned int rulenum,
1547 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001548{
Harald Welteaae69be2004-08-29 23:32:14 +00001549 struct chain_head *c;
1550 struct rule_head *r;
Marc Bouchere6869a82000-03-20 06:03:29 +00001551
Rusty Russell79dee072000-05-02 16:45:16 +00001552 iptc_fn = TC_DELETE_NUM_ENTRY;
Harald Welteaae69be2004-08-29 23:32:14 +00001553
1554 if (!(c = iptcc_find_label(chain, *handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001555 errno = ENOENT;
1556 return 0;
1557 }
1558
Martin Josefssona5616dc2004-10-24 22:27:31 +00001559 if (rulenum >= c->num_rules) {
Martin Josefsson631f3612004-09-23 19:25:06 +00001560 errno = E2BIG;
1561 return 0;
1562 }
1563
1564 /* Take advantage of the double linked list if possible. */
Martin Josefssona5616dc2004-10-24 22:27:31 +00001565 if (rulenum + 1 <= c->num_rules/2) {
1566 r = iptcc_get_rule_num(c, rulenum + 1);
1567 } else {
1568 r = iptcc_get_rule_num_reverse(c, c->num_rules - rulenum);
Marc Bouchere6869a82000-03-20 06:03:29 +00001569 }
1570
Harald Welteaae69be2004-08-29 23:32:14 +00001571 /* If we are about to delete the rule that is the current
1572 * iterator, move rule iterator back. next pointer will then
1573 * point to real next node */
1574 if (r == (*handle)->rule_iterator_cur) {
1575 (*handle)->rule_iterator_cur =
1576 list_entry((*handle)->rule_iterator_cur->list.prev,
1577 struct rule_head, list);
Harald Welte0113fe72004-01-06 19:04:02 +00001578 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001579
Harald Welteaae69be2004-08-29 23:32:14 +00001580 c->num_rules--;
1581 iptcc_delete_rule(r);
1582
Martin Josefsson2a5dbbb2004-09-22 21:37:41 +00001583 set_changed(*handle);
1584
Harald Welteaae69be2004-08-29 23:32:14 +00001585 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00001586}
1587
1588/* Check the packet `fw' on chain `chain'. Returns the verdict, or
1589 NULL and sets errno. */
1590const char *
Rusty Russell79dee072000-05-02 16:45:16 +00001591TC_CHECK_PACKET(const IPT_CHAINLABEL chain,
1592 STRUCT_ENTRY *entry,
1593 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001594{
Derrik Pates664c0a32005-02-01 13:28:14 +00001595 iptc_fn = TC_CHECK_PACKET;
Marc Bouchere6869a82000-03-20 06:03:29 +00001596 errno = ENOSYS;
1597 return NULL;
1598}
1599
1600/* Flushes the entries in the given chain (ie. empties chain). */
1601int
Rusty Russell79dee072000-05-02 16:45:16 +00001602TC_FLUSH_ENTRIES(const IPT_CHAINLABEL chain, TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001603{
Harald Welteaae69be2004-08-29 23:32:14 +00001604 struct chain_head *c;
1605 struct rule_head *r, *tmp;
Marc Bouchere6869a82000-03-20 06:03:29 +00001606
Harald Welte0113fe72004-01-06 19:04:02 +00001607 iptc_fn = TC_FLUSH_ENTRIES;
Harald Welteaae69be2004-08-29 23:32:14 +00001608 if (!(c = iptcc_find_label(chain, *handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001609 errno = ENOENT;
1610 return 0;
1611 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001612
Harald Welteaae69be2004-08-29 23:32:14 +00001613 list_for_each_entry_safe(r, tmp, &c->rules, list) {
1614 iptcc_delete_rule(r);
1615 }
1616
1617 c->num_rules = 0;
1618
1619 set_changed(*handle);
1620
1621 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00001622}
1623
1624/* Zeroes the counters in a chain. */
1625int
Rusty Russell79dee072000-05-02 16:45:16 +00001626TC_ZERO_ENTRIES(const IPT_CHAINLABEL chain, TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001627{
Harald Welteaae69be2004-08-29 23:32:14 +00001628 struct chain_head *c;
1629 struct rule_head *r;
Rusty Russell7e53bf92000-03-20 07:03:28 +00001630
Derrik Pates664c0a32005-02-01 13:28:14 +00001631 iptc_fn = TC_ZERO_ENTRIES;
Harald Welteaae69be2004-08-29 23:32:14 +00001632 if (!(c = iptcc_find_label(chain, *handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001633 errno = ENOENT;
1634 return 0;
1635 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001636
Harald Welteaae69be2004-08-29 23:32:14 +00001637 list_for_each_entry(r, &c->rules, list) {
1638 if (r->counter_map.maptype == COUNTER_MAP_NORMAL_MAP)
1639 r->counter_map.maptype = COUNTER_MAP_ZEROED;
Marc Bouchere6869a82000-03-20 06:03:29 +00001640 }
Harald Welteaae69be2004-08-29 23:32:14 +00001641
Rusty Russell175f6412000-03-24 09:32:20 +00001642 set_changed(*handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001643
Marc Bouchere6869a82000-03-20 06:03:29 +00001644 return 1;
1645}
1646
Harald Welte1cef74d2001-01-05 15:22:59 +00001647STRUCT_COUNTERS *
1648TC_READ_COUNTER(const IPT_CHAINLABEL chain,
1649 unsigned int rulenum,
1650 TC_HANDLE_T *handle)
1651{
Harald Welteaae69be2004-08-29 23:32:14 +00001652 struct chain_head *c;
1653 struct rule_head *r;
Harald Welte1cef74d2001-01-05 15:22:59 +00001654
1655 iptc_fn = TC_READ_COUNTER;
1656 CHECK(*handle);
1657
Harald Welteaae69be2004-08-29 23:32:14 +00001658 if (!(c = iptcc_find_label(chain, *handle))) {
Harald Welte1cef74d2001-01-05 15:22:59 +00001659 errno = ENOENT;
1660 return NULL;
1661 }
1662
Harald Welteaae69be2004-08-29 23:32:14 +00001663 if (!(r = iptcc_get_rule_num(c, rulenum))) {
Harald Welte0113fe72004-01-06 19:04:02 +00001664 errno = E2BIG;
1665 return NULL;
1666 }
1667
Harald Welteaae69be2004-08-29 23:32:14 +00001668 return &r->entry[0].counters;
Harald Welte1cef74d2001-01-05 15:22:59 +00001669}
1670
1671int
1672TC_ZERO_COUNTER(const IPT_CHAINLABEL chain,
1673 unsigned int rulenum,
1674 TC_HANDLE_T *handle)
1675{
Harald Welteaae69be2004-08-29 23:32:14 +00001676 struct chain_head *c;
1677 struct rule_head *r;
Harald Welte1cef74d2001-01-05 15:22:59 +00001678
1679 iptc_fn = TC_ZERO_COUNTER;
1680 CHECK(*handle);
1681
Harald Welteaae69be2004-08-29 23:32:14 +00001682 if (!(c = iptcc_find_label(chain, *handle))) {
Harald Welte1cef74d2001-01-05 15:22:59 +00001683 errno = ENOENT;
1684 return 0;
1685 }
1686
Harald Welteaae69be2004-08-29 23:32:14 +00001687 if (!(r = iptcc_get_rule_num(c, rulenum))) {
Harald Welte0113fe72004-01-06 19:04:02 +00001688 errno = E2BIG;
1689 return 0;
1690 }
1691
Harald Welteaae69be2004-08-29 23:32:14 +00001692 if (r->counter_map.maptype == COUNTER_MAP_NORMAL_MAP)
1693 r->counter_map.maptype = COUNTER_MAP_ZEROED;
Harald Welte1cef74d2001-01-05 15:22:59 +00001694
1695 set_changed(*handle);
1696
1697 return 1;
1698}
1699
1700int
1701TC_SET_COUNTER(const IPT_CHAINLABEL chain,
1702 unsigned int rulenum,
1703 STRUCT_COUNTERS *counters,
1704 TC_HANDLE_T *handle)
1705{
Harald Welteaae69be2004-08-29 23:32:14 +00001706 struct chain_head *c;
1707 struct rule_head *r;
Harald Welte1cef74d2001-01-05 15:22:59 +00001708 STRUCT_ENTRY *e;
Harald Welte1cef74d2001-01-05 15:22:59 +00001709
1710 iptc_fn = TC_SET_COUNTER;
1711 CHECK(*handle);
1712
Harald Welteaae69be2004-08-29 23:32:14 +00001713 if (!(c = iptcc_find_label(chain, *handle))) {
Harald Welte1cef74d2001-01-05 15:22:59 +00001714 errno = ENOENT;
1715 return 0;
1716 }
Harald Welte0113fe72004-01-06 19:04:02 +00001717
Harald Welteaae69be2004-08-29 23:32:14 +00001718 if (!(r = iptcc_get_rule_num(c, rulenum))) {
Harald Welte0113fe72004-01-06 19:04:02 +00001719 errno = E2BIG;
1720 return 0;
1721 }
1722
Harald Welteaae69be2004-08-29 23:32:14 +00001723 e = r->entry;
1724 r->counter_map.maptype = COUNTER_MAP_SET;
Harald Welte0113fe72004-01-06 19:04:02 +00001725
1726 memcpy(&e->counters, counters, sizeof(STRUCT_COUNTERS));
Harald Welte1cef74d2001-01-05 15:22:59 +00001727
1728 set_changed(*handle);
1729
1730 return 1;
1731}
1732
Marc Bouchere6869a82000-03-20 06:03:29 +00001733/* Creates a new chain. */
1734/* To create a chain, create two rules: error node and unconditional
1735 * return. */
1736int
Rusty Russell79dee072000-05-02 16:45:16 +00001737TC_CREATE_CHAIN(const IPT_CHAINLABEL chain, TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001738{
Harald Welteaae69be2004-08-29 23:32:14 +00001739 static struct chain_head *c;
Marc Bouchere6869a82000-03-20 06:03:29 +00001740
Rusty Russell79dee072000-05-02 16:45:16 +00001741 iptc_fn = TC_CREATE_CHAIN;
Marc Bouchere6869a82000-03-20 06:03:29 +00001742
1743 /* find_label doesn't cover built-in targets: DROP, ACCEPT,
1744 QUEUE, RETURN. */
Harald Welteaae69be2004-08-29 23:32:14 +00001745 if (iptcc_find_label(chain, *handle)
Rusty Russell79dee072000-05-02 16:45:16 +00001746 || strcmp(chain, LABEL_DROP) == 0
1747 || strcmp(chain, LABEL_ACCEPT) == 0
Rusty Russell67088e72000-05-10 01:18:57 +00001748 || strcmp(chain, LABEL_QUEUE) == 0
Rusty Russell79dee072000-05-02 16:45:16 +00001749 || strcmp(chain, LABEL_RETURN) == 0) {
Harald Welteaae69be2004-08-29 23:32:14 +00001750 DEBUGP("Chain `%s' already exists\n", chain);
Marc Bouchere6869a82000-03-20 06:03:29 +00001751 errno = EEXIST;
1752 return 0;
1753 }
1754
Rusty Russell79dee072000-05-02 16:45:16 +00001755 if (strlen(chain)+1 > sizeof(IPT_CHAINLABEL)) {
Harald Welteaae69be2004-08-29 23:32:14 +00001756 DEBUGP("Chain name `%s' too long\n", chain);
Marc Bouchere6869a82000-03-20 06:03:29 +00001757 errno = EINVAL;
1758 return 0;
1759 }
1760
Harald Welteaae69be2004-08-29 23:32:14 +00001761 c = iptcc_alloc_chain_head(chain, 0);
1762 if (!c) {
1763 DEBUGP("Cannot allocate memory for chain `%s'\n", chain);
1764 errno = ENOMEM;
1765 return 0;
Marc Bouchere6869a82000-03-20 06:03:29 +00001766
Harald Welteaae69be2004-08-29 23:32:14 +00001767 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001768
Harald Welteaae69be2004-08-29 23:32:14 +00001769 DEBUGP("Creating chain `%s'\n", chain);
1770 list_add_tail(&c->list, &(*handle)->chains);
Harald Welte0113fe72004-01-06 19:04:02 +00001771
1772 set_changed(*handle);
1773
Harald Welteaae69be2004-08-29 23:32:14 +00001774 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00001775}
1776
1777/* Get the number of references to this chain. */
1778int
Rusty Russell79dee072000-05-02 16:45:16 +00001779TC_GET_REFERENCES(unsigned int *ref, const IPT_CHAINLABEL chain,
1780 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001781{
Harald Welteaae69be2004-08-29 23:32:14 +00001782 struct chain_head *c;
Marc Bouchere6869a82000-03-20 06:03:29 +00001783
Derrik Pates664c0a32005-02-01 13:28:14 +00001784 iptc_fn = TC_GET_REFERENCES;
Harald Welteaae69be2004-08-29 23:32:14 +00001785 if (!(c = iptcc_find_label(chain, *handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001786 errno = ENOENT;
1787 return 0;
1788 }
1789
Harald Welteaae69be2004-08-29 23:32:14 +00001790 *ref = c->references;
1791
Marc Bouchere6869a82000-03-20 06:03:29 +00001792 return 1;
1793}
1794
1795/* Deletes a chain. */
1796int
Rusty Russell79dee072000-05-02 16:45:16 +00001797TC_DELETE_CHAIN(const IPT_CHAINLABEL chain, TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001798{
Marc Bouchere6869a82000-03-20 06:03:29 +00001799 unsigned int references;
Harald Welteaae69be2004-08-29 23:32:14 +00001800 struct chain_head *c;
Rusty Russell7e53bf92000-03-20 07:03:28 +00001801
Rusty Russell79dee072000-05-02 16:45:16 +00001802 iptc_fn = TC_DELETE_CHAIN;
Marc Bouchere6869a82000-03-20 06:03:29 +00001803
Harald Welteaae69be2004-08-29 23:32:14 +00001804 if (!(c = iptcc_find_label(chain, *handle))) {
1805 DEBUGP("cannot find chain `%s'\n", chain);
Marc Bouchere6869a82000-03-20 06:03:29 +00001806 errno = ENOENT;
1807 return 0;
1808 }
1809
Harald Welteaae69be2004-08-29 23:32:14 +00001810 if (TC_BUILTIN(chain, *handle)) {
1811 DEBUGP("cannot remove builtin chain `%s'\n", chain);
1812 errno = EINVAL;
1813 return 0;
1814 }
1815
1816 if (!TC_GET_REFERENCES(&references, chain, handle)) {
1817 DEBUGP("cannot get references on chain `%s'\n", chain);
1818 return 0;
1819 }
1820
1821 if (references > 0) {
1822 DEBUGP("chain `%s' still has references\n", chain);
1823 errno = EMLINK;
1824 return 0;
1825 }
1826
1827 if (c->num_rules) {
1828 DEBUGP("chain `%s' is not empty\n", chain);
Marc Bouchere6869a82000-03-20 06:03:29 +00001829 errno = ENOTEMPTY;
1830 return 0;
1831 }
1832
Harald Welteaae69be2004-08-29 23:32:14 +00001833 /* If we are about to delete the chain that is the current
1834 * iterator, move chain iterator firward. */
1835 if (c == (*handle)->chain_iterator_cur)
1836 iptcc_chain_iterator_advance(*handle);
Harald Welte0113fe72004-01-06 19:04:02 +00001837
Harald Welteaae69be2004-08-29 23:32:14 +00001838 list_del(&c->list);
1839 free(c);
Harald Welte0113fe72004-01-06 19:04:02 +00001840
Harald Welteaae69be2004-08-29 23:32:14 +00001841 DEBUGP("chain `%s' deleted\n", chain);
1842
1843 set_changed(*handle);
1844
1845 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00001846}
1847
1848/* Renames a chain. */
Rusty Russell79dee072000-05-02 16:45:16 +00001849int TC_RENAME_CHAIN(const IPT_CHAINLABEL oldname,
1850 const IPT_CHAINLABEL newname,
1851 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001852{
Harald Welteaae69be2004-08-29 23:32:14 +00001853 struct chain_head *c;
Rusty Russell79dee072000-05-02 16:45:16 +00001854 iptc_fn = TC_RENAME_CHAIN;
Marc Bouchere6869a82000-03-20 06:03:29 +00001855
Harald Welte1de80462000-10-30 12:00:27 +00001856 /* find_label doesn't cover built-in targets: DROP, ACCEPT,
1857 QUEUE, RETURN. */
Harald Welteaae69be2004-08-29 23:32:14 +00001858 if (iptcc_find_label(newname, *handle)
Rusty Russell79dee072000-05-02 16:45:16 +00001859 || strcmp(newname, LABEL_DROP) == 0
1860 || strcmp(newname, LABEL_ACCEPT) == 0
Harald Welte1de80462000-10-30 12:00:27 +00001861 || strcmp(newname, LABEL_QUEUE) == 0
Rusty Russell79dee072000-05-02 16:45:16 +00001862 || strcmp(newname, LABEL_RETURN) == 0) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001863 errno = EEXIST;
1864 return 0;
1865 }
1866
Harald Welteaae69be2004-08-29 23:32:14 +00001867 if (!(c = iptcc_find_label(oldname, *handle))
Rusty Russell79dee072000-05-02 16:45:16 +00001868 || TC_BUILTIN(oldname, *handle)) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001869 errno = ENOENT;
1870 return 0;
1871 }
1872
Rusty Russell79dee072000-05-02 16:45:16 +00001873 if (strlen(newname)+1 > sizeof(IPT_CHAINLABEL)) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001874 errno = EINVAL;
1875 return 0;
1876 }
1877
Harald Welteaae69be2004-08-29 23:32:14 +00001878 strncpy(c->name, newname, sizeof(IPT_CHAINLABEL));
1879
Harald Welte0113fe72004-01-06 19:04:02 +00001880 set_changed(*handle);
1881
Marc Bouchere6869a82000-03-20 06:03:29 +00001882 return 1;
1883}
1884
1885/* Sets the policy on a built-in chain. */
1886int
Rusty Russell79dee072000-05-02 16:45:16 +00001887TC_SET_POLICY(const IPT_CHAINLABEL chain,
1888 const IPT_CHAINLABEL policy,
Harald Welte1cef74d2001-01-05 15:22:59 +00001889 STRUCT_COUNTERS *counters,
Rusty Russell79dee072000-05-02 16:45:16 +00001890 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001891{
Harald Welteaae69be2004-08-29 23:32:14 +00001892 struct chain_head *c;
Marc Bouchere6869a82000-03-20 06:03:29 +00001893
Rusty Russell79dee072000-05-02 16:45:16 +00001894 iptc_fn = TC_SET_POLICY;
Marc Bouchere6869a82000-03-20 06:03:29 +00001895
Harald Welteaae69be2004-08-29 23:32:14 +00001896 if (!(c = iptcc_find_label(chain, *handle))) {
1897 DEBUGP("cannot find chain `%s'\n", chain);
1898 errno = ENOENT;
Marc Bouchere6869a82000-03-20 06:03:29 +00001899 return 0;
1900 }
1901
Harald Welteaae69be2004-08-29 23:32:14 +00001902 if (!iptcc_is_builtin(c)) {
1903 DEBUGP("cannot set policy of userdefinedchain `%s'\n", chain);
1904 errno = ENOENT;
1905 return 0;
1906 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001907
Rusty Russell79dee072000-05-02 16:45:16 +00001908 if (strcmp(policy, LABEL_ACCEPT) == 0)
Harald Welteaae69be2004-08-29 23:32:14 +00001909 c->verdict = -NF_ACCEPT - 1;
Rusty Russell79dee072000-05-02 16:45:16 +00001910 else if (strcmp(policy, LABEL_DROP) == 0)
Harald Welteaae69be2004-08-29 23:32:14 +00001911 c->verdict = -NF_DROP - 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00001912 else {
1913 errno = EINVAL;
1914 return 0;
1915 }
Harald Welte1cef74d2001-01-05 15:22:59 +00001916
Harald Welte1cef74d2001-01-05 15:22:59 +00001917 if (counters) {
1918 /* set byte and packet counters */
Harald Welteaae69be2004-08-29 23:32:14 +00001919 memcpy(&c->counters, counters, sizeof(STRUCT_COUNTERS));
1920 c->counter_map.maptype = COUNTER_MAP_SET;
Harald Welte1cef74d2001-01-05 15:22:59 +00001921 } else {
Harald Welteaae69be2004-08-29 23:32:14 +00001922 c->counter_map.maptype = COUNTER_MAP_NOMAP;
Harald Welte1cef74d2001-01-05 15:22:59 +00001923 }
1924
Rusty Russell175f6412000-03-24 09:32:20 +00001925 set_changed(*handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001926
Marc Bouchere6869a82000-03-20 06:03:29 +00001927 return 1;
1928}
1929
1930/* Without this, on gcc 2.7.2.3, we get:
Rusty Russell79dee072000-05-02 16:45:16 +00001931 libiptc.c: In function `TC_COMMIT':
Marc Bouchere6869a82000-03-20 06:03:29 +00001932 libiptc.c:833: fixed or forbidden register was spilled.
1933 This may be due to a compiler bug or to impossible asm
1934 statements or clauses.
1935*/
1936static void
Rusty Russell79dee072000-05-02 16:45:16 +00001937subtract_counters(STRUCT_COUNTERS *answer,
1938 const STRUCT_COUNTERS *a,
1939 const STRUCT_COUNTERS *b)
Marc Bouchere6869a82000-03-20 06:03:29 +00001940{
1941 answer->pcnt = a->pcnt - b->pcnt;
1942 answer->bcnt = a->bcnt - b->bcnt;
1943}
1944
Harald Welteaae69be2004-08-29 23:32:14 +00001945
1946static void counters_nomap(STRUCT_COUNTERS_INFO *newcounters,
1947 unsigned int index)
1948{
1949 newcounters->counters[index] = ((STRUCT_COUNTERS) { 0, 0});
1950 DEBUGP_C("NOMAP => zero\n");
1951}
1952
1953static void counters_normal_map(STRUCT_COUNTERS_INFO *newcounters,
1954 STRUCT_REPLACE *repl,
1955 unsigned int index,
1956 unsigned int mappos)
1957{
1958 /* Original read: X.
1959 * Atomic read on replacement: X + Y.
1960 * Currently in kernel: Z.
1961 * Want in kernel: X + Y + Z.
1962 * => Add in X + Y
1963 * => Add in replacement read.
1964 */
1965 newcounters->counters[index] = repl->counters[mappos];
1966 DEBUGP_C("NORMAL_MAP => mappos %u \n", mappos);
1967}
1968
1969static void counters_map_zeroed(STRUCT_COUNTERS_INFO *newcounters,
1970 STRUCT_REPLACE *repl,
1971 unsigned int index,
1972 unsigned int mappos,
1973 STRUCT_COUNTERS *counters)
1974{
1975 /* Original read: X.
1976 * Atomic read on replacement: X + Y.
1977 * Currently in kernel: Z.
1978 * Want in kernel: Y + Z.
1979 * => Add in Y.
1980 * => Add in (replacement read - original read).
1981 */
1982 subtract_counters(&newcounters->counters[index],
1983 &repl->counters[mappos],
1984 counters);
1985 DEBUGP_C("ZEROED => mappos %u\n", mappos);
1986}
1987
1988static void counters_map_set(STRUCT_COUNTERS_INFO *newcounters,
1989 unsigned int index,
1990 STRUCT_COUNTERS *counters)
1991{
1992 /* Want to set counter (iptables-restore) */
1993
1994 memcpy(&newcounters->counters[index], counters,
1995 sizeof(STRUCT_COUNTERS));
1996
1997 DEBUGP_C("SET\n");
1998}
1999
2000
Marc Bouchere6869a82000-03-20 06:03:29 +00002001int
Rusty Russell79dee072000-05-02 16:45:16 +00002002TC_COMMIT(TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00002003{
2004 /* Replace, then map back the counters. */
Rusty Russell79dee072000-05-02 16:45:16 +00002005 STRUCT_REPLACE *repl;
2006 STRUCT_COUNTERS_INFO *newcounters;
Harald Welteaae69be2004-08-29 23:32:14 +00002007 struct chain_head *c;
2008 int ret;
Martin Josefsson841e4ae2003-05-02 15:30:11 +00002009 size_t counterlen;
Harald Welteaae69be2004-08-29 23:32:14 +00002010 int new_number;
2011 unsigned int new_size;
Marc Bouchere6869a82000-03-20 06:03:29 +00002012
Derrik Pates664c0a32005-02-01 13:28:14 +00002013 iptc_fn = TC_COMMIT;
Marc Bouchere6869a82000-03-20 06:03:29 +00002014 CHECK(*handle);
Martin Josefsson841e4ae2003-05-02 15:30:11 +00002015
Marc Bouchere6869a82000-03-20 06:03:29 +00002016 /* Don't commit if nothing changed. */
2017 if (!(*handle)->changed)
2018 goto finished;
2019
Harald Welteaae69be2004-08-29 23:32:14 +00002020 new_number = iptcc_compile_table_prep(*handle, &new_size);
2021 if (new_number < 0) {
2022 errno = ENOMEM;
2023 return 0;
2024 }
2025
2026 repl = malloc(sizeof(*repl) + new_size);
Marc Bouchere6869a82000-03-20 06:03:29 +00002027 if (!repl) {
2028 errno = ENOMEM;
2029 return 0;
2030 }
Martin Josefssonad3b4f92004-09-22 22:04:07 +00002031 memset(repl, 0, sizeof(*repl) + new_size);
Harald Welteaae69be2004-08-29 23:32:14 +00002032
Rusty Russelle45c7132004-12-16 13:21:44 +00002033#if 0
2034 TC_DUMP_ENTRIES(*handle);
2035#endif
2036
Harald Welteaae69be2004-08-29 23:32:14 +00002037 counterlen = sizeof(STRUCT_COUNTERS_INFO)
2038 + sizeof(STRUCT_COUNTERS) * new_number;
Derrik Pates664c0a32005-02-01 13:28:14 +00002039 memset(repl, 0, sizeof(*repl) + (*handle)->entries->size);
Marc Bouchere6869a82000-03-20 06:03:29 +00002040
2041 /* These are the old counters we will get from kernel */
Rusty Russell79dee072000-05-02 16:45:16 +00002042 repl->counters = malloc(sizeof(STRUCT_COUNTERS)
Marc Bouchere6869a82000-03-20 06:03:29 +00002043 * (*handle)->info.num_entries);
2044 if (!repl->counters) {
2045 free(repl);
2046 errno = ENOMEM;
2047 return 0;
2048 }
Derrik Pates664c0a32005-02-01 13:28:14 +00002049 memset(repl->counters, 0, sizeof(STRUCT_COUNTERS)
2050 * (*handle)->info.num_entries);
Marc Bouchere6869a82000-03-20 06:03:29 +00002051 /* These are the counters we're going to put back, later. */
2052 newcounters = malloc(counterlen);
2053 if (!newcounters) {
2054 free(repl->counters);
2055 free(repl);
2056 errno = ENOMEM;
2057 return 0;
2058 }
Harald Welteaae69be2004-08-29 23:32:14 +00002059 memset(newcounters, 0, counterlen);
Marc Bouchere6869a82000-03-20 06:03:29 +00002060
2061 strcpy(repl->name, (*handle)->info.name);
Harald Welteaae69be2004-08-29 23:32:14 +00002062 repl->num_entries = new_number;
2063 repl->size = new_size;
2064
Marc Bouchere6869a82000-03-20 06:03:29 +00002065 repl->num_counters = (*handle)->info.num_entries;
2066 repl->valid_hooks = (*handle)->info.valid_hooks;
Harald Welteaae69be2004-08-29 23:32:14 +00002067
2068 DEBUGP("num_entries=%u, size=%u, num_counters=%u\n",
2069 repl->num_entries, repl->size, repl->num_counters);
2070
2071 ret = iptcc_compile_table(*handle, repl);
2072 if (ret < 0) {
2073 errno = ret;
2074 free(repl->counters);
2075 free(repl);
2076 return 0;
2077 }
2078
2079
2080#ifdef IPTC_DEBUG2
2081 {
2082 int fd = open("/tmp/libiptc-so_set_replace.blob",
2083 O_CREAT|O_WRONLY);
2084 if (fd >= 0) {
2085 write(fd, repl, sizeof(*repl) + repl->size);
2086 close(fd);
2087 }
2088 }
2089#endif
Marc Bouchere6869a82000-03-20 06:03:29 +00002090
Rusty Russell79dee072000-05-02 16:45:16 +00002091 if (setsockopt(sockfd, TC_IPPROTO, SO_SET_REPLACE, repl,
Harald Welteaae69be2004-08-29 23:32:14 +00002092 sizeof(*repl) + repl->size) < 0) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002093 free(repl->counters);
2094 free(repl);
2095 free(newcounters);
2096 return 0;
2097 }
2098
2099 /* Put counters back. */
2100 strcpy(newcounters->name, (*handle)->info.name);
Harald Welteaae69be2004-08-29 23:32:14 +00002101 newcounters->num_counters = new_number;
Marc Bouchere6869a82000-03-20 06:03:29 +00002102
Harald Welteaae69be2004-08-29 23:32:14 +00002103 list_for_each_entry(c, &(*handle)->chains, list) {
2104 struct rule_head *r;
Marc Bouchere6869a82000-03-20 06:03:29 +00002105
Harald Welteaae69be2004-08-29 23:32:14 +00002106 /* Builtin chains have their own counters */
2107 if (iptcc_is_builtin(c)) {
2108 DEBUGP("counter for chain-index %u: ", c->foot_index);
2109 switch(c->counter_map.maptype) {
2110 case COUNTER_MAP_NOMAP:
2111 counters_nomap(newcounters, c->foot_index);
2112 break;
2113 case COUNTER_MAP_NORMAL_MAP:
2114 counters_normal_map(newcounters, repl,
2115 c->foot_index,
2116 c->counter_map.mappos);
2117 break;
2118 case COUNTER_MAP_ZEROED:
2119 counters_map_zeroed(newcounters, repl,
2120 c->foot_index,
2121 c->counter_map.mappos,
2122 &c->counters);
2123 break;
2124 case COUNTER_MAP_SET:
2125 counters_map_set(newcounters, c->foot_index,
2126 &c->counters);
2127 break;
2128 }
2129 }
Harald Welte1cef74d2001-01-05 15:22:59 +00002130
Harald Welteaae69be2004-08-29 23:32:14 +00002131 list_for_each_entry(r, &c->rules, list) {
2132 DEBUGP("counter for index %u: ", r->index);
2133 switch (r->counter_map.maptype) {
2134 case COUNTER_MAP_NOMAP:
2135 counters_nomap(newcounters, r->index);
2136 break;
Harald Welte1cef74d2001-01-05 15:22:59 +00002137
Harald Welteaae69be2004-08-29 23:32:14 +00002138 case COUNTER_MAP_NORMAL_MAP:
2139 counters_normal_map(newcounters, repl,
2140 r->index,
2141 r->counter_map.mappos);
2142 break;
Harald Welte1cef74d2001-01-05 15:22:59 +00002143
Harald Welteaae69be2004-08-29 23:32:14 +00002144 case COUNTER_MAP_ZEROED:
2145 counters_map_zeroed(newcounters, repl,
2146 r->index,
2147 r->counter_map.mappos,
2148 &r->entry->counters);
2149 break;
2150
2151 case COUNTER_MAP_SET:
2152 counters_map_set(newcounters, r->index,
2153 &r->entry->counters);
2154 break;
2155 }
Marc Bouchere6869a82000-03-20 06:03:29 +00002156 }
2157 }
Rusty Russell62527ce2000-09-04 09:45:54 +00002158
Harald Welteaae69be2004-08-29 23:32:14 +00002159
Rusty Russell62527ce2000-09-04 09:45:54 +00002160#ifdef KERNEL_64_USERSPACE_32
2161 {
2162 /* Kernel will think that pointer should be 64-bits, and get
2163 padding. So we accomodate here (assumption: alignment of
2164 `counters' is on 64-bit boundary). */
2165 u_int64_t *kernptr = (u_int64_t *)&newcounters->counters;
2166 if ((unsigned long)&newcounters->counters % 8 != 0) {
2167 fprintf(stderr,
2168 "counters alignment incorrect! Mail rusty!\n");
2169 abort();
2170 }
2171 *kernptr = newcounters->counters;
Rusty Russell54c307e2000-09-04 06:47:28 +00002172 }
Rusty Russell62527ce2000-09-04 09:45:54 +00002173#endif /* KERNEL_64_USERSPACE_32 */
Marc Bouchere6869a82000-03-20 06:03:29 +00002174
Harald Welteaae69be2004-08-29 23:32:14 +00002175#ifdef IPTC_DEBUG2
2176 {
2177 int fd = open("/tmp/libiptc-so_set_add_counters.blob",
2178 O_CREAT|O_WRONLY);
2179 if (fd >= 0) {
2180 write(fd, newcounters, counterlen);
2181 close(fd);
2182 }
2183 }
2184#endif
2185
Rusty Russell79dee072000-05-02 16:45:16 +00002186 if (setsockopt(sockfd, TC_IPPROTO, SO_SET_ADD_COUNTERS,
2187 newcounters, counterlen) < 0) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002188 free(repl->counters);
2189 free(repl);
2190 free(newcounters);
2191 return 0;
2192 }
2193
2194 free(repl->counters);
2195 free(repl);
2196 free(newcounters);
2197
2198 finished:
Martin Josefsson841e4ae2003-05-02 15:30:11 +00002199 TC_FREE(handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00002200 return 1;
2201}
2202
2203/* Get raw socket. */
2204int
Rusty Russell79dee072000-05-02 16:45:16 +00002205TC_GET_RAW_SOCKET()
Marc Bouchere6869a82000-03-20 06:03:29 +00002206{
2207 return sockfd;
2208}
2209
2210/* Translates errno numbers into more human-readable form than strerror. */
2211const char *
Rusty Russell79dee072000-05-02 16:45:16 +00002212TC_STRERROR(int err)
Marc Bouchere6869a82000-03-20 06:03:29 +00002213{
2214 unsigned int i;
2215 struct table_struct {
2216 void *fn;
2217 int err;
2218 const char *message;
2219 } table [] =
Harald Welte4ccfa632001-07-30 15:12:43 +00002220 { { TC_INIT, EPERM, "Permission denied (you must be root)" },
Rusty Russell79dee072000-05-02 16:45:16 +00002221 { TC_INIT, EINVAL, "Module is wrong version" },
Harald Welte4ccfa632001-07-30 15:12:43 +00002222 { TC_INIT, ENOENT,
2223 "Table does not exist (do you need to insmod?)" },
Rusty Russell79dee072000-05-02 16:45:16 +00002224 { TC_DELETE_CHAIN, ENOTEMPTY, "Chain is not empty" },
2225 { TC_DELETE_CHAIN, EINVAL, "Can't delete built-in chain" },
2226 { TC_DELETE_CHAIN, EMLINK,
Marc Bouchere6869a82000-03-20 06:03:29 +00002227 "Can't delete chain with references left" },
Rusty Russell79dee072000-05-02 16:45:16 +00002228 { TC_CREATE_CHAIN, EEXIST, "Chain already exists" },
2229 { TC_INSERT_ENTRY, E2BIG, "Index of insertion too big" },
2230 { TC_REPLACE_ENTRY, E2BIG, "Index of replacement too big" },
2231 { TC_DELETE_NUM_ENTRY, E2BIG, "Index of deletion too big" },
Harald Welte1cef74d2001-01-05 15:22:59 +00002232 { TC_READ_COUNTER, E2BIG, "Index of counter too big" },
2233 { TC_ZERO_COUNTER, E2BIG, "Index of counter too big" },
Rusty Russell79dee072000-05-02 16:45:16 +00002234 { TC_INSERT_ENTRY, ELOOP, "Loop found in table" },
2235 { TC_INSERT_ENTRY, EINVAL, "Target problem" },
Marc Bouchere6869a82000-03-20 06:03:29 +00002236 /* EINVAL for CHECK probably means bad interface. */
Rusty Russell79dee072000-05-02 16:45:16 +00002237 { TC_CHECK_PACKET, EINVAL,
Marc Boucherc8264992000-04-22 22:34:44 +00002238 "Bad arguments (does that interface exist?)" },
Harald Welte4ccfa632001-07-30 15:12:43 +00002239 { TC_CHECK_PACKET, ENOSYS,
2240 "Checking will most likely never get implemented" },
Marc Bouchere6869a82000-03-20 06:03:29 +00002241 /* ENOENT for DELETE probably means no matching rule */
Rusty Russell79dee072000-05-02 16:45:16 +00002242 { TC_DELETE_ENTRY, ENOENT,
Marc Boucherc8264992000-04-22 22:34:44 +00002243 "Bad rule (does a matching rule exist in that chain?)" },
Rusty Russell79dee072000-05-02 16:45:16 +00002244 { TC_SET_POLICY, ENOENT,
Marc Boucherc8264992000-04-22 22:34:44 +00002245 "Bad built-in chain name" },
Rusty Russell79dee072000-05-02 16:45:16 +00002246 { TC_SET_POLICY, EINVAL,
Marc Boucherc8264992000-04-22 22:34:44 +00002247 "Bad policy name" },
Harald Welte4ccfa632001-07-30 15:12:43 +00002248
2249 { NULL, 0, "Incompatible with this kernel" },
2250 { NULL, ENOPROTOOPT, "iptables who? (do you need to insmod?)" },
2251 { NULL, ENOSYS, "Will be implemented real soon. I promise ;)" },
2252 { NULL, ENOMEM, "Memory allocation problem" },
2253 { NULL, ENOENT, "No chain/target/match by that name" },
Marc Bouchere6869a82000-03-20 06:03:29 +00002254 };
2255
2256 for (i = 0; i < sizeof(table)/sizeof(struct table_struct); i++) {
2257 if ((!table[i].fn || table[i].fn == iptc_fn)
2258 && table[i].err == err)
2259 return table[i].message;
2260 }
2261
2262 return strerror(err);
2263}