blob: fad337c1dda2c253017c6187feadbd9521522cbe [file] [log] [blame]
Martin Josefsson631f3612004-09-23 19:25:06 +00001/* Library which manipulates firewall rules. Version $Revision: 1.56 $ */
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;
48static void *iptc_fn = NULL;
49
50static const char *hooknames[]
Rusty Russell79dee072000-05-02 16:45:16 +000051= { [HOOK_PRE_ROUTING] "PREROUTING",
52 [HOOK_LOCAL_IN] "INPUT",
53 [HOOK_FORWARD] "FORWARD",
54 [HOOK_LOCAL_OUT] "OUTPUT",
Rusty Russell10758b72000-09-14 07:37:33 +000055 [HOOK_POST_ROUTING] "POSTROUTING",
56#ifdef HOOK_DROPPING
57 [HOOK_DROPPING] "DROPPING"
58#endif
Marc Bouchere6869a82000-03-20 06:03:29 +000059};
60
Harald Welteaae69be2004-08-29 23:32:14 +000061/* Convenience structures */
62struct ipt_error_target
63{
64 STRUCT_ENTRY_TARGET t;
65 char error[TABLE_MAXNAMELEN];
66};
67
68struct chain_head;
69struct rule_head;
70
Marc Bouchere6869a82000-03-20 06:03:29 +000071struct counter_map
72{
73 enum {
74 COUNTER_MAP_NOMAP,
75 COUNTER_MAP_NORMAL_MAP,
Harald Welte1cef74d2001-01-05 15:22:59 +000076 COUNTER_MAP_ZEROED,
77 COUNTER_MAP_SET
Marc Bouchere6869a82000-03-20 06:03:29 +000078 } maptype;
79 unsigned int mappos;
80};
81
Harald Welteaae69be2004-08-29 23:32:14 +000082enum iptcc_rule_type {
83 IPTCC_R_STANDARD, /* standard target (ACCEPT, ...) */
84 IPTCC_R_MODULE, /* extension module (SNAT, ...) */
85 IPTCC_R_FALLTHROUGH, /* fallthrough rule */
86 IPTCC_R_JUMP, /* jump to other chain */
Marc Bouchere6869a82000-03-20 06:03:29 +000087};
88
Harald Welteaae69be2004-08-29 23:32:14 +000089struct rule_head
Rusty Russell30fd6e52000-04-23 09:16:06 +000090{
Harald Welteaae69be2004-08-29 23:32:14 +000091 struct list_head list;
92 struct chain_head *chain;
93 struct counter_map counter_map;
94
95 unsigned int index; /* index (needed for counter_map) */
96 unsigned int offset; /* offset in rule blob */
97
98 enum iptcc_rule_type type;
99 struct chain_head *jump; /* jump target, if IPTCC_R_JUMP */
100
101 unsigned int size; /* size of entry data */
102 STRUCT_ENTRY entry[0];
103};
104
105struct chain_head
106{
107 struct list_head list;
Rusty Russell79dee072000-05-02 16:45:16 +0000108 char name[TABLE_MAXNAMELEN];
Harald Welteaae69be2004-08-29 23:32:14 +0000109 unsigned int hooknum; /* hook number+1 if builtin */
110 unsigned int references; /* how many jumps reference us */
111 int verdict; /* verdict if builtin */
112
113 STRUCT_COUNTERS counters; /* per-chain counters */
114 struct counter_map counter_map;
115
116 unsigned int num_rules; /* number of rules in list */
117 struct list_head rules; /* list of rules */
118
119 unsigned int index; /* index (needed for jump resolval) */
120 unsigned int head_offset; /* offset in rule blob */
121 unsigned int foot_index; /* index (needed for counter_map) */
122 unsigned int foot_offset; /* offset in rule blob */
Rusty Russell30fd6e52000-04-23 09:16:06 +0000123};
124
Rusty Russell79dee072000-05-02 16:45:16 +0000125STRUCT_TC_HANDLE
Marc Bouchere6869a82000-03-20 06:03:29 +0000126{
Harald Welteaae69be2004-08-29 23:32:14 +0000127 int changed; /* Have changes been made? */
128
129 struct list_head chains;
130
131 struct chain_head *chain_iterator_cur;
132 struct rule_head *rule_iterator_cur;
133
Rusty Russell79dee072000-05-02 16:45:16 +0000134 STRUCT_GETINFO info;
Harald Welteaae69be2004-08-29 23:32:14 +0000135 STRUCT_GET_ENTRIES *entries;
Marc Bouchere6869a82000-03-20 06:03:29 +0000136};
137
Harald Welteaae69be2004-08-29 23:32:14 +0000138/* allocate a new chain head for the cache */
139static struct chain_head *iptcc_alloc_chain_head(const char *name, int hooknum)
140{
141 struct chain_head *c = malloc(sizeof(*c));
142 if (!c)
143 return NULL;
144 memset(c, 0, sizeof(*c));
145
146 strncpy(c->name, name, TABLE_MAXNAMELEN);
147 c->hooknum = hooknum;
148 INIT_LIST_HEAD(&c->rules);
149
150 return c;
151}
152
153/* allocate and initialize a new rule for the cache */
154static struct rule_head *iptcc_alloc_rule(struct chain_head *c, unsigned int size)
155{
156 struct rule_head *r = malloc(sizeof(*r)+size);
157 if (!r)
158 return NULL;
159 memset(r, 0, sizeof(*r));
160
161 r->chain = c;
162 r->size = size;
163
164 return r;
165}
166
167/* notify us that the ruleset has been modified by the user */
Rusty Russell175f6412000-03-24 09:32:20 +0000168static void
Rusty Russell79dee072000-05-02 16:45:16 +0000169set_changed(TC_HANDLE_T h)
Rusty Russell175f6412000-03-24 09:32:20 +0000170{
Rusty Russell175f6412000-03-24 09:32:20 +0000171 h->changed = 1;
172}
173
Harald Welte380ba5f2002-02-13 16:19:55 +0000174#ifdef IPTC_DEBUG
Rusty Russell79dee072000-05-02 16:45:16 +0000175static void do_check(TC_HANDLE_T h, unsigned int line);
Rusty Russell849779c2000-04-23 15:51:51 +0000176#define CHECK(h) do { if (!getenv("IPTC_NO_CHECK")) do_check((h), __LINE__); } while(0)
Rusty Russell30fd6e52000-04-23 09:16:06 +0000177#else
178#define CHECK(h)
179#endif
Marc Bouchere6869a82000-03-20 06:03:29 +0000180
Harald Welteaae69be2004-08-29 23:32:14 +0000181
182/**********************************************************************
183 * iptc blob utility functions (iptcb_*)
184 **********************************************************************/
185
Marc Bouchere6869a82000-03-20 06:03:29 +0000186static inline int
Harald Welteaae69be2004-08-29 23:32:14 +0000187iptcb_get_number(const STRUCT_ENTRY *i,
Rusty Russell79dee072000-05-02 16:45:16 +0000188 const STRUCT_ENTRY *seek,
Marc Bouchere6869a82000-03-20 06:03:29 +0000189 unsigned int *pos)
190{
191 if (i == seek)
192 return 1;
193 (*pos)++;
194 return 0;
195}
196
Marc Bouchere6869a82000-03-20 06:03:29 +0000197static inline int
Harald Welteaae69be2004-08-29 23:32:14 +0000198iptcb_get_entry_n(STRUCT_ENTRY *i,
Marc Bouchere6869a82000-03-20 06:03:29 +0000199 unsigned int number,
200 unsigned int *pos,
Rusty Russell79dee072000-05-02 16:45:16 +0000201 STRUCT_ENTRY **pe)
Marc Bouchere6869a82000-03-20 06:03:29 +0000202{
203 if (*pos == number) {
204 *pe = i;
205 return 1;
206 }
207 (*pos)++;
208 return 0;
209}
210
Harald Welteaae69be2004-08-29 23:32:14 +0000211static inline STRUCT_ENTRY *
212iptcb_get_entry(TC_HANDLE_T h, unsigned int offset)
213{
214 return (STRUCT_ENTRY *)((char *)h->entries->entrytable + offset);
215}
216
217static unsigned int
218iptcb_entry2index(const TC_HANDLE_T h, const STRUCT_ENTRY *seek)
Marc Bouchere6869a82000-03-20 06:03:29 +0000219{
220 unsigned int pos = 0;
Marc Bouchere6869a82000-03-20 06:03:29 +0000221
Harald Welteaae69be2004-08-29 23:32:14 +0000222 if (ENTRY_ITERATE(h->entries->entrytable, h->entries->size,
223 iptcb_get_number, seek, &pos) == 0) {
224 fprintf(stderr, "ERROR: offset %u not an entry!\n",
225 (unsigned int)((char *)seek - (char *)h->entries->entrytable));
226 abort();
227 }
228 return pos;
Marc Bouchere6869a82000-03-20 06:03:29 +0000229}
230
Harald Welte0113fe72004-01-06 19:04:02 +0000231static inline STRUCT_ENTRY *
Harald Welteaae69be2004-08-29 23:32:14 +0000232iptcb_offset2entry(TC_HANDLE_T h, unsigned int offset)
Harald Welte0113fe72004-01-06 19:04:02 +0000233{
Harald Welteaae69be2004-08-29 23:32:14 +0000234 return (STRUCT_ENTRY *) ((void *)h->entries->entrytable+offset);
Harald Welte0113fe72004-01-06 19:04:02 +0000235}
236
Harald Welteaae69be2004-08-29 23:32:14 +0000237
Harald Welte0113fe72004-01-06 19:04:02 +0000238static inline unsigned long
Harald Welteaae69be2004-08-29 23:32:14 +0000239iptcb_entry2offset(const TC_HANDLE_T h, const STRUCT_ENTRY *e)
Harald Welte0113fe72004-01-06 19:04:02 +0000240{
Harald Welteaae69be2004-08-29 23:32:14 +0000241 return (void *)e - (void *)h->entries->entrytable;
Harald Welte3ea8f402003-06-23 18:25:59 +0000242}
243
244static inline unsigned int
Harald Welteaae69be2004-08-29 23:32:14 +0000245iptcb_offset2index(const TC_HANDLE_T h, unsigned int offset)
Harald Welte3ea8f402003-06-23 18:25:59 +0000246{
Harald Welteaae69be2004-08-29 23:32:14 +0000247 return iptcb_entry2index(h, iptcb_offset2entry(h, offset));
248}
249
250/* Returns 0 if not hook entry, else hooknumber + 1 */
251static inline unsigned int
252iptcb_ent_is_hook_entry(STRUCT_ENTRY *e, TC_HANDLE_T h)
253{
254 unsigned int i;
255
256 for (i = 0; i < NUMHOOKS; i++) {
257 if ((h->info.valid_hooks & (1 << i))
258 && iptcb_get_entry(h, h->info.hook_entry[i]) == e)
259 return i+1;
260 }
261 return 0;
Harald Welte3ea8f402003-06-23 18:25:59 +0000262}
263
264
Harald Welteaae69be2004-08-29 23:32:14 +0000265/**********************************************************************
266 * iptc cache utility functions (iptcc_*)
267 **********************************************************************/
Harald Welte0113fe72004-01-06 19:04:02 +0000268
Harald Welteaae69be2004-08-29 23:32:14 +0000269/* Is the given chain builtin (1) or user-defined (0) */
270static unsigned int iptcc_is_builtin(struct chain_head *c)
271{
272 return (c->hooknum ? 1 : 0);
273}
274
275/* Get a specific rule within a chain */
276static struct rule_head *iptcc_get_rule_num(struct chain_head *c,
277 unsigned int rulenum)
278{
279 struct rule_head *r;
280 unsigned int num = 0;
281
282 list_for_each_entry(r, &c->rules, list) {
283 num++;
284 if (num == rulenum)
285 return r;
286 }
287 return NULL;
288}
289
Martin Josefssona5616dc2004-10-24 22:27:31 +0000290/* Get a specific rule within a chain backwards */
291static struct rule_head *iptcc_get_rule_num_reverse(struct chain_head *c,
292 unsigned int rulenum)
293{
294 struct rule_head *r;
295 unsigned int num = 0;
296
297 list_for_each_entry_reverse(r, &c->rules, list) {
298 num++;
299 if (num == rulenum)
300 return r;
301 }
302 return NULL;
303}
304
Harald Welteaae69be2004-08-29 23:32:14 +0000305/* Returns chain head if found, otherwise NULL. */
306static struct chain_head *
307iptcc_find_chain_by_offset(TC_HANDLE_T handle, unsigned int offset)
308{
309 struct list_head *pos;
310
311 if (list_empty(&handle->chains))
312 return NULL;
313
314 list_for_each(pos, &handle->chains) {
315 struct chain_head *c = list_entry(pos, struct chain_head, list);
316 if (offset >= c->head_offset && offset <= c->foot_offset)
317 return c;
Harald Welte0113fe72004-01-06 19:04:02 +0000318 }
319
Harald Welteaae69be2004-08-29 23:32:14 +0000320 return NULL;
Harald Welte0113fe72004-01-06 19:04:02 +0000321}
Harald Welteaae69be2004-08-29 23:32:14 +0000322/* Returns chain head if found, otherwise NULL. */
323static struct chain_head *
324iptcc_find_label(const char *name, TC_HANDLE_T handle)
325{
326 struct list_head *pos;
327
328 if (list_empty(&handle->chains))
329 return NULL;
330
331 list_for_each(pos, &handle->chains) {
332 struct chain_head *c = list_entry(pos, struct chain_head, list);
333 if (!strcmp(c->name, name))
334 return c;
335 }
336
337 return NULL;
338}
339
340/* called when rule is to be removed from cache */
341static void iptcc_delete_rule(struct rule_head *r)
342{
343 DEBUGP("deleting rule %p (offset %u)\n", r, r->offset);
344 /* clean up reference count of called chain */
345 if (r->type == IPTCC_R_JUMP
346 && r->jump)
347 r->jump->references--;
348
349 list_del(&r->list);
350 free(r);
351}
352
353
354/**********************************************************************
355 * RULESET PARSER (blob -> cache)
356 **********************************************************************/
357
358static int alphasort(const void *a, const void *b)
359{
360 return strcmp(((struct chain_head *)a)->name,
361 ((struct chain_head *)b)->name);
362}
363
364/* Delete policy rule of previous chain, since cache doesn't contain
365 * chain policy rules.
366 * WARNING: This function has ugly design and relies on a lot of context, only
367 * to be called from specific places within the parser */
368static int __iptcc_p_del_policy(TC_HANDLE_T h, unsigned int num)
369{
370 if (h->chain_iterator_cur) {
371 /* policy rule is last rule */
372 struct rule_head *pr = (struct rule_head *)
373 h->chain_iterator_cur->rules.prev;
374
375 /* save verdict */
376 h->chain_iterator_cur->verdict =
377 *(int *)GET_TARGET(pr->entry)->data;
378
379 /* save counter and counter_map information */
380 h->chain_iterator_cur->counter_map.maptype =
381 COUNTER_MAP_NORMAL_MAP;
382 h->chain_iterator_cur->counter_map.mappos = num-1;
383 memcpy(&h->chain_iterator_cur->counters, &pr->entry->counters,
384 sizeof(h->chain_iterator_cur->counters));
385
386 /* foot_offset points to verdict rule */
387 h->chain_iterator_cur->foot_index = num;
388 h->chain_iterator_cur->foot_offset = pr->offset;
389
390 /* delete rule from cache */
391 iptcc_delete_rule(pr);
Martin Josefsson8d1b38a2004-09-22 21:00:19 +0000392 h->chain_iterator_cur->num_rules--;
Harald Welteaae69be2004-08-29 23:32:14 +0000393
394 return 1;
395 }
396 return 0;
397}
398
399/* Another ugly helper function split out of cache_add_entry to make it less
400 * spaghetti code */
401static void __iptcc_p_add_chain(TC_HANDLE_T h, struct chain_head *c,
402 unsigned int offset, unsigned int *num)
403{
404 __iptcc_p_del_policy(h, *num);
405
406 c->head_offset = offset;
407 c->index = *num;
408
409 list_add_tail(&c->list, &h->chains);
410 h->chain_iterator_cur = c;
411}
412
413/* main parser function: add an entry from the blob to the cache */
414static int cache_add_entry(STRUCT_ENTRY *e,
415 TC_HANDLE_T h,
416 STRUCT_ENTRY **prev,
417 unsigned int *num)
418{
419 unsigned int builtin;
420 unsigned int offset = (char *)e - (char *)h->entries->entrytable;
421
422 DEBUGP("entering...");
423
424 /* Last entry ("policy rule"). End it.*/
425 if (iptcb_entry2offset(h,e) + e->next_offset == h->entries->size) {
426 /* This is the ERROR node at the end of the chain */
427 DEBUGP_C("%u:%u: end of table:\n", *num, offset);
428
429 __iptcc_p_del_policy(h, *num);
430
431 h->chain_iterator_cur = NULL;
432 goto out_inc;
433 }
434
435 /* We know this is the start of a new chain if it's an ERROR
436 * target, or a hook entry point */
437
438 if (strcmp(GET_TARGET(e)->u.user.name, ERROR_TARGET) == 0) {
439 struct chain_head *c =
440 iptcc_alloc_chain_head((const char *)GET_TARGET(e)->data, 0);
441 DEBUGP_C("%u:%u:new userdefined chain %s: %p\n", *num, offset,
442 (char *)c->name, c);
443 if (!c) {
444 errno = -ENOMEM;
445 return -1;
446 }
447
448 __iptcc_p_add_chain(h, c, offset, num);
449
450 } else if ((builtin = iptcb_ent_is_hook_entry(e, h)) != 0) {
451 struct chain_head *c =
452 iptcc_alloc_chain_head((char *)hooknames[builtin-1],
453 builtin);
454 DEBUGP_C("%u:%u new builtin chain: %p (rules=%p)\n",
455 *num, offset, c, &c->rules);
456 if (!c) {
457 errno = -ENOMEM;
458 return -1;
459 }
460
461 c->hooknum = builtin;
462
463 __iptcc_p_add_chain(h, c, offset, num);
464
465 /* FIXME: this is ugly. */
466 goto new_rule;
467 } else {
468 /* has to be normal rule */
469 struct rule_head *r;
470new_rule:
471
472 if (!(r = iptcc_alloc_rule(h->chain_iterator_cur,
473 e->next_offset))) {
474 errno = ENOMEM;
475 return -1;
476 }
477 DEBUGP_C("%u:%u normal rule: %p: ", *num, offset, r);
478
479 r->index = *num;
480 r->offset = offset;
481 memcpy(r->entry, e, e->next_offset);
482 r->counter_map.maptype = COUNTER_MAP_NORMAL_MAP;
483 r->counter_map.mappos = r->index;
484
485 /* handling of jumps, etc. */
486 if (!strcmp(GET_TARGET(e)->u.user.name, STANDARD_TARGET)) {
487 STRUCT_STANDARD_TARGET *t;
488
489 t = (STRUCT_STANDARD_TARGET *)GET_TARGET(e);
490 if (t->target.u.target_size
491 != ALIGN(sizeof(STRUCT_STANDARD_TARGET))) {
492 errno = EINVAL;
493 return -1;
494 }
495
496 if (t->verdict < 0) {
497 DEBUGP_C("standard, verdict=%d\n", t->verdict);
498 r->type = IPTCC_R_STANDARD;
499 } else if (t->verdict == r->offset+e->next_offset) {
500 DEBUGP_C("fallthrough\n");
501 r->type = IPTCC_R_FALLTHROUGH;
502 } else {
503 DEBUGP_C("jump, target=%u\n", t->verdict);
504 r->type = IPTCC_R_JUMP;
505 /* Jump target fixup has to be deferred
506 * until second pass, since we migh not
507 * yet have parsed the target */
508 }
Martin Josefsson52c38022004-09-22 19:39:40 +0000509 } else {
510 DEBUGP_C("module, target=%s\n", GET_TARGET(e)->u.user.name);
511 r->type = IPTCC_R_MODULE;
Harald Welteaae69be2004-08-29 23:32:14 +0000512 }
513
514 list_add_tail(&r->list, &h->chain_iterator_cur->rules);
Martin Josefsson8d1b38a2004-09-22 21:00:19 +0000515 h->chain_iterator_cur->num_rules++;
Harald Welteaae69be2004-08-29 23:32:14 +0000516 }
517out_inc:
518 (*num)++;
519 return 0;
520}
521
522
523/* parse an iptables blob into it's pieces */
524static int parse_table(TC_HANDLE_T h)
525{
526 STRUCT_ENTRY *prev;
527 unsigned int num = 0;
528 struct chain_head *c;
529
530 /* First pass: over ruleset blob */
531 ENTRY_ITERATE(h->entries->entrytable, h->entries->size,
532 cache_add_entry, h, &prev, &num);
533
534 /* Second pass: fixup parsed data from first pass */
535 list_for_each_entry(c, &h->chains, list) {
536 struct rule_head *r;
537 list_for_each_entry(r, &c->rules, list) {
538 struct chain_head *c;
539 STRUCT_STANDARD_TARGET *t;
540
541 if (r->type != IPTCC_R_JUMP)
542 continue;
543
544 t = (STRUCT_STANDARD_TARGET *)GET_TARGET(r->entry);
545 c = iptcc_find_chain_by_offset(h, t->verdict);
546 if (!c)
547 return -1;
548 r->jump = c;
549 c->references++;
550 }
551 }
552
553 /* FIXME: sort chains */
554
555 return 1;
556}
557
558
559/**********************************************************************
560 * RULESET COMPILATION (cache -> blob)
561 **********************************************************************/
562
563/* Convenience structures */
564struct iptcb_chain_start{
565 STRUCT_ENTRY e;
566 struct ipt_error_target name;
567};
568#define IPTCB_CHAIN_START_SIZE (sizeof(STRUCT_ENTRY) + \
569 ALIGN(sizeof(struct ipt_error_target)))
570
571struct iptcb_chain_foot {
572 STRUCT_ENTRY e;
573 STRUCT_STANDARD_TARGET target;
574};
575#define IPTCB_CHAIN_FOOT_SIZE (sizeof(STRUCT_ENTRY) + \
576 ALIGN(sizeof(STRUCT_STANDARD_TARGET)))
577
578struct iptcb_chain_error {
579 STRUCT_ENTRY entry;
580 struct ipt_error_target target;
581};
582#define IPTCB_CHAIN_ERROR_SIZE (sizeof(STRUCT_ENTRY) + \
583 ALIGN(sizeof(struct ipt_error_target)))
584
585
586
587/* compile rule from cache into blob */
588static inline int iptcc_compile_rule (TC_HANDLE_T h, STRUCT_REPLACE *repl, struct rule_head *r)
589{
590 /* handle jumps */
591 if (r->type == IPTCC_R_JUMP) {
592 STRUCT_STANDARD_TARGET *t;
593 t = (STRUCT_STANDARD_TARGET *)GET_TARGET(r->entry);
594 /* memset for memcmp convenience on delete/replace */
595 memset(t->target.u.user.name, 0, FUNCTION_MAXNAMELEN);
596 strcpy(t->target.u.user.name, STANDARD_TARGET);
597 /* Jumps can only happen to builtin chains, so we
598 * can safely assume that they always have a header */
599 t->verdict = r->jump->head_offset + IPTCB_CHAIN_START_SIZE;
600 } else if (r->type == IPTCC_R_FALLTHROUGH) {
601 STRUCT_STANDARD_TARGET *t;
602 t = (STRUCT_STANDARD_TARGET *)GET_TARGET(r->entry);
603 t->verdict = r->offset + r->size;
604 }
605
606 /* copy entry from cache to blob */
607 memcpy((char *)repl->entries+r->offset, r->entry, r->size);
608
609 return 1;
610}
611
612/* compile chain from cache into blob */
613static int iptcc_compile_chain(TC_HANDLE_T h, STRUCT_REPLACE *repl, struct chain_head *c)
614{
615 int ret;
616 struct rule_head *r;
617 struct iptcb_chain_start *head;
618 struct iptcb_chain_foot *foot;
619
620 /* only user-defined chains have heaer */
621 if (!iptcc_is_builtin(c)) {
622 /* put chain header in place */
623 head = (void *)repl->entries + c->head_offset;
624 head->e.target_offset = sizeof(STRUCT_ENTRY);
625 head->e.next_offset = IPTCB_CHAIN_START_SIZE;
626 strcpy(head->name.t.u.user.name, ERROR_TARGET);
627 head->name.t.u.target_size =
628 ALIGN(sizeof(struct ipt_error_target));
629 strcpy(head->name.error, c->name);
630 } else {
631 repl->hook_entry[c->hooknum-1] = c->head_offset;
632 repl->underflow[c->hooknum-1] = c->foot_offset;
633 }
634
635 /* iterate over rules */
636 list_for_each_entry(r, &c->rules, list) {
637 ret = iptcc_compile_rule(h, repl, r);
638 if (ret < 0)
639 return ret;
640 }
641
642 /* put chain footer in place */
643 foot = (void *)repl->entries + c->foot_offset;
644 foot->e.target_offset = sizeof(STRUCT_ENTRY);
645 foot->e.next_offset = IPTCB_CHAIN_FOOT_SIZE;
646 strcpy(foot->target.target.u.user.name, STANDARD_TARGET);
647 foot->target.target.u.target_size =
648 ALIGN(sizeof(STRUCT_STANDARD_TARGET));
649 /* builtin targets have verdict, others return */
650 if (iptcc_is_builtin(c))
651 foot->target.verdict = c->verdict;
652 else
653 foot->target.verdict = RETURN;
654 /* set policy-counters */
655 memcpy(&foot->e.counters, &c->counters, sizeof(STRUCT_COUNTERS));
656
657 return 0;
658}
659
660/* calculate offset and number for every rule in the cache */
661static int iptcc_compile_chain_offsets(TC_HANDLE_T h, struct chain_head *c,
662 int *offset, int *num)
663{
664 struct rule_head *r;
665
666 c->head_offset = *offset;
667 DEBUGP("%s: chain_head %u, offset=%u\n", c->name, *num, *offset);
668
669 if (!iptcc_is_builtin(c)) {
670 /* Chain has header */
671 *offset += sizeof(STRUCT_ENTRY)
672 + ALIGN(sizeof(struct ipt_error_target));
673 (*num)++;
674 }
675
676 list_for_each_entry(r, &c->rules, list) {
677 DEBUGP("rule %u, offset=%u, index=%u\n", *num, *offset, *num);
678 r->offset = *offset;
679 r->index = *num;
680 *offset += r->size;
681 (*num)++;
682 }
683
684 DEBUGP("%s; chain_foot %u, offset=%u, index=%u\n", c->name, *num,
685 *offset, *num);
686 c->foot_offset = *offset;
687 c->foot_index = *num;
688 *offset += sizeof(STRUCT_ENTRY)
689 + ALIGN(sizeof(STRUCT_STANDARD_TARGET));
690 (*num)++;
691
692 return 1;
693}
694
695/* put the pieces back together again */
696static int iptcc_compile_table_prep(TC_HANDLE_T h, unsigned int *size)
697{
698 struct chain_head *c;
699 unsigned int offset = 0, num = 0;
700 int ret = 0;
701
702 /* First pass: calculate offset for every rule */
703 list_for_each_entry(c, &h->chains, list) {
704 ret = iptcc_compile_chain_offsets(h, c, &offset, &num);
705 if (ret < 0)
706 return ret;
707 }
708
709 /* Append one error rule at end of chain */
710 num++;
711 offset += sizeof(STRUCT_ENTRY)
712 + ALIGN(sizeof(struct ipt_error_target));
713
714 /* ruleset size is now in offset */
715 *size = offset;
716 return num;
717}
718
719static int iptcc_compile_table(TC_HANDLE_T h, STRUCT_REPLACE *repl)
720{
721 struct chain_head *c;
722 struct iptcb_chain_error *error;
723
724 /* Second pass: copy from cache to offsets, fill in jumps */
725 list_for_each_entry(c, &h->chains, list) {
726 int ret = iptcc_compile_chain(h, repl, c);
727 if (ret < 0)
728 return ret;
729 }
730
731 /* Append error rule at end of chain */
732 error = (void *)repl->entries + repl->size - IPTCB_CHAIN_ERROR_SIZE;
733 error->entry.target_offset = sizeof(STRUCT_ENTRY);
734 error->entry.next_offset = IPTCB_CHAIN_ERROR_SIZE;
735 error->target.t.u.user.target_size =
736 ALIGN(sizeof(struct ipt_error_target));
737 strcpy((char *)&error->target.t.u.user.name, ERROR_TARGET);
738 strcpy((char *)&error->target.error, "ERROR");
739
740 return 1;
741}
742
743/**********************************************************************
744 * EXTERNAL API (operates on cache only)
745 **********************************************************************/
Marc Bouchere6869a82000-03-20 06:03:29 +0000746
747/* Allocate handle of given size */
Rusty Russell79dee072000-05-02 16:45:16 +0000748static TC_HANDLE_T
Harald Welte0113fe72004-01-06 19:04:02 +0000749alloc_handle(const char *tablename, unsigned int size, unsigned int num_rules)
Marc Bouchere6869a82000-03-20 06:03:29 +0000750{
751 size_t len;
Rusty Russell79dee072000-05-02 16:45:16 +0000752 TC_HANDLE_T h;
Marc Bouchere6869a82000-03-20 06:03:29 +0000753
Harald Welteaae69be2004-08-29 23:32:14 +0000754 len = sizeof(STRUCT_TC_HANDLE) + size;
Marc Bouchere6869a82000-03-20 06:03:29 +0000755
Harald Welteaae69be2004-08-29 23:32:14 +0000756 h = malloc(sizeof(STRUCT_TC_HANDLE));
757 if (!h) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000758 errno = ENOMEM;
759 return NULL;
760 }
Harald Welteaae69be2004-08-29 23:32:14 +0000761 memset(h, 0, sizeof(*h));
762 INIT_LIST_HEAD(&h->chains);
Marc Bouchere6869a82000-03-20 06:03:29 +0000763 strcpy(h->info.name, tablename);
Harald Welteaae69be2004-08-29 23:32:14 +0000764
Harald Welte0371c0c2004-09-19 21:00:12 +0000765 h->entries = malloc(sizeof(STRUCT_GET_ENTRIES) + size);
Harald Welteaae69be2004-08-29 23:32:14 +0000766 if (!h->entries)
767 goto out_free_handle;
768
769 strcpy(h->entries->name, tablename);
Harald Welte0371c0c2004-09-19 21:00:12 +0000770 h->entries->size = size;
Marc Bouchere6869a82000-03-20 06:03:29 +0000771
772 return h;
Harald Welteaae69be2004-08-29 23:32:14 +0000773
774out_free_handle:
775 free(h);
776
777 return NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +0000778}
779
Harald Welteaae69be2004-08-29 23:32:14 +0000780
Rusty Russell79dee072000-05-02 16:45:16 +0000781TC_HANDLE_T
782TC_INIT(const char *tablename)
Marc Bouchere6869a82000-03-20 06:03:29 +0000783{
Rusty Russell79dee072000-05-02 16:45:16 +0000784 TC_HANDLE_T h;
785 STRUCT_GETINFO info;
Marc Bouchere6869a82000-03-20 06:03:29 +0000786 int tmp;
787 socklen_t s;
788
Rusty Russell79dee072000-05-02 16:45:16 +0000789 iptc_fn = TC_INIT;
Marc Bouchere6869a82000-03-20 06:03:29 +0000790
Martin Josefssone560fd62003-06-13 16:56:51 +0000791 if (sockfd != -1) {
Harald Welte366454b2002-01-07 13:46:50 +0000792 close(sockfd);
Martin Josefssone560fd62003-06-13 16:56:51 +0000793 sockfd = -1;
794 }
Harald Welte366454b2002-01-07 13:46:50 +0000795
Martin Josefsson841e4ae2003-05-02 15:30:11 +0000796 if (strlen(tablename) >= TABLE_MAXNAMELEN) {
797 errno = EINVAL;
798 return NULL;
799 }
800
Rusty Russell79dee072000-05-02 16:45:16 +0000801 sockfd = socket(TC_AF, SOCK_RAW, IPPROTO_RAW);
Marc Bouchere6869a82000-03-20 06:03:29 +0000802 if (sockfd < 0)
803 return NULL;
804
805 s = sizeof(info);
Martin Josefsson841e4ae2003-05-02 15:30:11 +0000806
Marc Bouchere6869a82000-03-20 06:03:29 +0000807 strcpy(info.name, tablename);
Rusty Russell79dee072000-05-02 16:45:16 +0000808 if (getsockopt(sockfd, TC_IPPROTO, SO_GET_INFO, &info, &s) < 0)
Marc Bouchere6869a82000-03-20 06:03:29 +0000809 return NULL;
810
Harald Welteaae69be2004-08-29 23:32:14 +0000811 DEBUGP("valid_hooks=0x%08x, num_entries=%u, size=%u\n",
812 info.valid_hooks, info.num_entries, info.size);
813
Harald Welte0113fe72004-01-06 19:04:02 +0000814 if ((h = alloc_handle(info.name, info.size, info.num_entries))
Martin Josefsson841e4ae2003-05-02 15:30:11 +0000815 == NULL) {
816 close(sockfd);
Martin Josefssone560fd62003-06-13 16:56:51 +0000817 sockfd = -1;
Marc Bouchere6869a82000-03-20 06:03:29 +0000818 return NULL;
Martin Josefsson841e4ae2003-05-02 15:30:11 +0000819 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000820
Marc Bouchere6869a82000-03-20 06:03:29 +0000821 /* Initialize current state */
822 h->info = info;
Harald Welte0113fe72004-01-06 19:04:02 +0000823
Harald Welteaae69be2004-08-29 23:32:14 +0000824 h->entries->size = h->info.size;
Marc Bouchere6869a82000-03-20 06:03:29 +0000825
Rusty Russell79dee072000-05-02 16:45:16 +0000826 tmp = sizeof(STRUCT_GET_ENTRIES) + h->info.size;
Marc Bouchere6869a82000-03-20 06:03:29 +0000827
Harald Welteaae69be2004-08-29 23:32:14 +0000828 if (getsockopt(sockfd, TC_IPPROTO, SO_GET_ENTRIES, h->entries,
829 &tmp) < 0)
830 goto error;
831
832#ifdef IPTC_DEBUG2
833 {
834 int fd = open("/tmp/libiptc-so_get_entries.blob",
835 O_CREAT|O_WRONLY);
836 if (fd >= 0) {
837 write(fd, h->entries, tmp);
838 close(fd);
839 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000840 }
Harald Welteaae69be2004-08-29 23:32:14 +0000841#endif
842
843 if (parse_table(h) < 0)
844 goto error;
Rusty Russell7e53bf92000-03-20 07:03:28 +0000845
Marc Bouchere6869a82000-03-20 06:03:29 +0000846 CHECK(h);
847 return h;
Harald Welteaae69be2004-08-29 23:32:14 +0000848error:
849 TC_FREE(&h);
850 return NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +0000851}
852
Martin Josefsson841e4ae2003-05-02 15:30:11 +0000853void
854TC_FREE(TC_HANDLE_T *h)
855{
Harald Welteaae69be2004-08-29 23:32:14 +0000856 struct chain_head *c, *tmp;
857
Martin Josefsson841e4ae2003-05-02 15:30:11 +0000858 close(sockfd);
Martin Josefssone560fd62003-06-13 16:56:51 +0000859 sockfd = -1;
Harald Welteaae69be2004-08-29 23:32:14 +0000860
861 list_for_each_entry_safe(c, tmp, &(*h)->chains, list) {
862 struct rule_head *r, *rtmp;
863
864 list_for_each_entry_safe(r, rtmp, &c->rules, list) {
865 free(r);
866 }
867
868 free(c);
869 }
870
871 free((*h)->entries);
Martin Josefsson841e4ae2003-05-02 15:30:11 +0000872 free(*h);
Harald Welteaae69be2004-08-29 23:32:14 +0000873
Martin Josefsson841e4ae2003-05-02 15:30:11 +0000874 *h = NULL;
875}
876
Marc Bouchere6869a82000-03-20 06:03:29 +0000877static inline int
Rusty Russell79dee072000-05-02 16:45:16 +0000878print_match(const STRUCT_ENTRY_MATCH *m)
Marc Bouchere6869a82000-03-20 06:03:29 +0000879{
Rusty Russell228e98d2000-04-27 10:28:06 +0000880 printf("Match name: `%s'\n", m->u.user.name);
Marc Bouchere6869a82000-03-20 06:03:29 +0000881 return 0;
882}
883
Rusty Russell79dee072000-05-02 16:45:16 +0000884static int dump_entry(STRUCT_ENTRY *e, const TC_HANDLE_T handle);
885
Marc Bouchere6869a82000-03-20 06:03:29 +0000886void
Rusty Russell79dee072000-05-02 16:45:16 +0000887TC_DUMP_ENTRIES(const TC_HANDLE_T handle)
Marc Bouchere6869a82000-03-20 06:03:29 +0000888{
889 CHECK(handle);
Harald Welteaae69be2004-08-29 23:32:14 +0000890#if 0
Rusty Russelle45c7132004-12-16 13:21:44 +0000891 printf("libiptc v%s. %u bytes.\n",
892 IPTABLES_VERSION, handle->entries->size);
Marc Bouchere6869a82000-03-20 06:03:29 +0000893 printf("Table `%s'\n", handle->info.name);
894 printf("Hooks: pre/in/fwd/out/post = %u/%u/%u/%u/%u\n",
Rusty Russell67088e72000-05-10 01:18:57 +0000895 handle->info.hook_entry[HOOK_PRE_ROUTING],
896 handle->info.hook_entry[HOOK_LOCAL_IN],
897 handle->info.hook_entry[HOOK_FORWARD],
898 handle->info.hook_entry[HOOK_LOCAL_OUT],
899 handle->info.hook_entry[HOOK_POST_ROUTING]);
Marc Bouchere6869a82000-03-20 06:03:29 +0000900 printf("Underflows: pre/in/fwd/out/post = %u/%u/%u/%u/%u\n",
Rusty Russell67088e72000-05-10 01:18:57 +0000901 handle->info.underflow[HOOK_PRE_ROUTING],
902 handle->info.underflow[HOOK_LOCAL_IN],
903 handle->info.underflow[HOOK_FORWARD],
904 handle->info.underflow[HOOK_LOCAL_OUT],
905 handle->info.underflow[HOOK_POST_ROUTING]);
Marc Bouchere6869a82000-03-20 06:03:29 +0000906
Harald Welteaae69be2004-08-29 23:32:14 +0000907 ENTRY_ITERATE(handle->entries->entrytable, handle->entries->size,
Rusty Russell79dee072000-05-02 16:45:16 +0000908 dump_entry, handle);
Harald Welteaae69be2004-08-29 23:32:14 +0000909#endif
Harald Welte0113fe72004-01-06 19:04:02 +0000910}
Rusty Russell30fd6e52000-04-23 09:16:06 +0000911
Marc Bouchere6869a82000-03-20 06:03:29 +0000912/* Does this chain exist? */
Rusty Russell79dee072000-05-02 16:45:16 +0000913int TC_IS_CHAIN(const char *chain, const TC_HANDLE_T handle)
Marc Bouchere6869a82000-03-20 06:03:29 +0000914{
Harald Welteaae69be2004-08-29 23:32:14 +0000915 return iptcc_find_label(chain, handle) != NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +0000916}
917
Harald Welteaae69be2004-08-29 23:32:14 +0000918static void iptcc_chain_iterator_advance(TC_HANDLE_T handle)
919{
920 struct chain_head *c = handle->chain_iterator_cur;
921
922 if (c->list.next == &handle->chains)
923 handle->chain_iterator_cur = NULL;
924 else
925 handle->chain_iterator_cur =
926 list_entry(c->list.next, struct chain_head, list);
927}
Marc Bouchere6869a82000-03-20 06:03:29 +0000928
Rusty Russell30fd6e52000-04-23 09:16:06 +0000929/* Iterator functions to run through the chains. */
Marc Bouchere6869a82000-03-20 06:03:29 +0000930const char *
Philip Blundell8c700902000-05-15 02:17:52 +0000931TC_FIRST_CHAIN(TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +0000932{
Harald Welteaae69be2004-08-29 23:32:14 +0000933 struct chain_head *c = list_entry((*handle)->chains.next,
934 struct chain_head, list);
935
936 iptc_fn = TC_FIRST_CHAIN;
937
938
939 if (list_empty(&(*handle)->chains)) {
940 DEBUGP(": no chains\n");
Harald Welte0113fe72004-01-06 19:04:02 +0000941 return NULL;
Harald Welteaae69be2004-08-29 23:32:14 +0000942 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000943
Harald Welteaae69be2004-08-29 23:32:14 +0000944 (*handle)->chain_iterator_cur = c;
945 iptcc_chain_iterator_advance(*handle);
Harald Welte0113fe72004-01-06 19:04:02 +0000946
Harald Welteaae69be2004-08-29 23:32:14 +0000947 DEBUGP(": returning `%s'\n", c->name);
948 return c->name;
Marc Bouchere6869a82000-03-20 06:03:29 +0000949}
950
Rusty Russell30fd6e52000-04-23 09:16:06 +0000951/* Iterator functions to run through the chains. Returns NULL at end. */
952const char *
Rusty Russell79dee072000-05-02 16:45:16 +0000953TC_NEXT_CHAIN(TC_HANDLE_T *handle)
Rusty Russell30fd6e52000-04-23 09:16:06 +0000954{
Harald Welteaae69be2004-08-29 23:32:14 +0000955 struct chain_head *c = (*handle)->chain_iterator_cur;
Rusty Russell30fd6e52000-04-23 09:16:06 +0000956
Harald Welteaae69be2004-08-29 23:32:14 +0000957 iptc_fn = TC_NEXT_CHAIN;
958
959 if (!c) {
960 DEBUGP(": no more chains\n");
Rusty Russell30fd6e52000-04-23 09:16:06 +0000961 return NULL;
Harald Welteaae69be2004-08-29 23:32:14 +0000962 }
Rusty Russell30fd6e52000-04-23 09:16:06 +0000963
Harald Welteaae69be2004-08-29 23:32:14 +0000964 iptcc_chain_iterator_advance(*handle);
965
966 DEBUGP(": returning `%s'\n", c->name);
967 return c->name;
Rusty Russell30fd6e52000-04-23 09:16:06 +0000968}
969
970/* Get first rule in the given chain: NULL for empty chain. */
Rusty Russell79dee072000-05-02 16:45:16 +0000971const STRUCT_ENTRY *
Philip Blundell8c700902000-05-15 02:17:52 +0000972TC_FIRST_RULE(const char *chain, TC_HANDLE_T *handle)
Rusty Russell30fd6e52000-04-23 09:16:06 +0000973{
Harald Welteaae69be2004-08-29 23:32:14 +0000974 struct chain_head *c;
975 struct rule_head *r;
Rusty Russell30fd6e52000-04-23 09:16:06 +0000976
Harald Welteaae69be2004-08-29 23:32:14 +0000977 iptc_fn = TC_FIRST_RULE;
978
979 DEBUGP("first rule(%s): ", chain);
980
981 c = iptcc_find_label(chain, *handle);
Rusty Russell30fd6e52000-04-23 09:16:06 +0000982 if (!c) {
983 errno = ENOENT;
984 return NULL;
985 }
986
987 /* Empty chain: single return/policy rule */
Harald Welteaae69be2004-08-29 23:32:14 +0000988 if (list_empty(&c->rules)) {
989 DEBUGP_C("no rules, returning NULL\n");
Rusty Russell30fd6e52000-04-23 09:16:06 +0000990 return NULL;
Harald Welteaae69be2004-08-29 23:32:14 +0000991 }
Rusty Russell30fd6e52000-04-23 09:16:06 +0000992
Harald Welteaae69be2004-08-29 23:32:14 +0000993 r = list_entry(c->rules.next, struct rule_head, list);
994 (*handle)->rule_iterator_cur = r;
995 DEBUGP_C("%p\n", r);
996
997 return r->entry;
Rusty Russell30fd6e52000-04-23 09:16:06 +0000998}
999
1000/* Returns NULL when rules run out. */
Rusty Russell79dee072000-05-02 16:45:16 +00001001const STRUCT_ENTRY *
Philip Blundell8c700902000-05-15 02:17:52 +00001002TC_NEXT_RULE(const STRUCT_ENTRY *prev, TC_HANDLE_T *handle)
Rusty Russell30fd6e52000-04-23 09:16:06 +00001003{
Harald Welteaae69be2004-08-29 23:32:14 +00001004 struct rule_head *r;
Rusty Russell30fd6e52000-04-23 09:16:06 +00001005
Harald Welteaae69be2004-08-29 23:32:14 +00001006 DEBUGP("rule_iterator_cur=%p...", (*handle)->rule_iterator_cur);
1007
1008 if (!(*handle)->rule_iterator_cur) {
1009 DEBUGP_C("returning NULL\n");
1010 return NULL;
1011 }
1012
1013 r = list_entry((*handle)->rule_iterator_cur->list.next,
1014 struct rule_head, list);
1015
1016 iptc_fn = TC_NEXT_RULE;
1017
1018 DEBUGP_C("next=%p, head=%p...", &r->list,
1019 &(*handle)->rule_iterator_cur->chain->rules);
1020
1021 if (&r->list == &(*handle)->rule_iterator_cur->chain->rules) {
1022 (*handle)->rule_iterator_cur = NULL;
1023 DEBUGP_C("finished, returning NULL\n");
1024 return NULL;
1025 }
1026
1027 (*handle)->rule_iterator_cur = r;
1028
1029 /* NOTE: prev is without any influence ! */
1030 DEBUGP_C("returning rule %p\n", r);
1031 return r->entry;
Rusty Russell30fd6e52000-04-23 09:16:06 +00001032}
1033
Marc Bouchere6869a82000-03-20 06:03:29 +00001034/* How many rules in this chain? */
1035unsigned int
Rusty Russell79dee072000-05-02 16:45:16 +00001036TC_NUM_RULES(const char *chain, TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001037{
Harald Welteaae69be2004-08-29 23:32:14 +00001038 struct chain_head *c;
1039 iptc_fn = TC_NUM_RULES;
Marc Bouchere6869a82000-03-20 06:03:29 +00001040 CHECK(*handle);
Harald Welteaae69be2004-08-29 23:32:14 +00001041
1042 c = iptcc_find_label(chain, *handle);
1043 if (!c) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001044 errno = ENOENT;
1045 return (unsigned int)-1;
1046 }
Harald Welteaae69be2004-08-29 23:32:14 +00001047
1048 return c->num_rules;
Marc Bouchere6869a82000-03-20 06:03:29 +00001049}
1050
Rusty Russell79dee072000-05-02 16:45:16 +00001051const STRUCT_ENTRY *TC_GET_RULE(const char *chain,
1052 unsigned int n,
1053 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001054{
Harald Welteaae69be2004-08-29 23:32:14 +00001055 struct chain_head *c;
1056 struct rule_head *r;
1057
1058 iptc_fn = TC_GET_RULE;
Marc Bouchere6869a82000-03-20 06:03:29 +00001059
1060 CHECK(*handle);
Harald Welteaae69be2004-08-29 23:32:14 +00001061
1062 c = iptcc_find_label(chain, *handle);
1063 if (!c) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001064 errno = ENOENT;
1065 return NULL;
1066 }
1067
Harald Welteaae69be2004-08-29 23:32:14 +00001068 r = iptcc_get_rule_num(c, n);
1069 if (!r)
1070 return NULL;
1071 return r->entry;
Marc Bouchere6869a82000-03-20 06:03:29 +00001072}
1073
1074/* Returns a pointer to the target name of this position. */
Harald Welteaae69be2004-08-29 23:32:14 +00001075const char *standard_target_map(int verdict)
Marc Bouchere6869a82000-03-20 06:03:29 +00001076{
Harald Welteaae69be2004-08-29 23:32:14 +00001077 switch (verdict) {
1078 case RETURN:
1079 return LABEL_RETURN;
1080 break;
1081 case -NF_ACCEPT-1:
1082 return LABEL_ACCEPT;
1083 break;
1084 case -NF_DROP-1:
1085 return LABEL_DROP;
1086 break;
1087 case -NF_QUEUE-1:
1088 return LABEL_QUEUE;
1089 break;
1090 default:
1091 fprintf(stderr, "ERROR: %d not a valid target)\n",
1092 verdict);
1093 abort();
1094 break;
1095 }
1096 /* not reached */
1097 return NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +00001098}
1099
Harald Welteaae69be2004-08-29 23:32:14 +00001100/* Returns a pointer to the target name of this position. */
1101const char *TC_GET_TARGET(const STRUCT_ENTRY *ce,
1102 TC_HANDLE_T *handle)
1103{
1104 STRUCT_ENTRY *e = (STRUCT_ENTRY *)ce;
Rusty Russelle45c7132004-12-16 13:21:44 +00001105 struct rule_head *r = container_of(e, struct rule_head, entry[0]);
Harald Welteaae69be2004-08-29 23:32:14 +00001106
1107 iptc_fn = TC_GET_TARGET;
1108
1109 switch(r->type) {
1110 int spos;
1111 case IPTCC_R_FALLTHROUGH:
1112 return "";
1113 break;
1114 case IPTCC_R_JUMP:
1115 DEBUGP("r=%p, jump=%p, name=`%s'\n", r, r->jump, r->jump->name);
1116 return r->jump->name;
1117 break;
1118 case IPTCC_R_STANDARD:
1119 spos = *(int *)GET_TARGET(e)->data;
1120 DEBUGP("r=%p, spos=%d'\n", r, spos);
1121 return standard_target_map(spos);
1122 break;
1123 case IPTCC_R_MODULE:
1124 return GET_TARGET(e)->u.user.name;
1125 break;
1126 }
1127 return NULL;
1128}
Marc Bouchere6869a82000-03-20 06:03:29 +00001129/* Is this a built-in chain? Actually returns hook + 1. */
1130int
Rusty Russell79dee072000-05-02 16:45:16 +00001131TC_BUILTIN(const char *chain, const TC_HANDLE_T handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001132{
Harald Welteaae69be2004-08-29 23:32:14 +00001133 struct chain_head *c;
1134
1135 iptc_fn = TC_BUILTIN;
Marc Bouchere6869a82000-03-20 06:03:29 +00001136
Harald Welteaae69be2004-08-29 23:32:14 +00001137 c = iptcc_find_label(chain, handle);
1138 if (!c) {
1139 errno = ENOENT;
Martin Josefssonb0f3d2d2004-09-23 18:23:20 +00001140 return 0;
Marc Bouchere6869a82000-03-20 06:03:29 +00001141 }
Harald Welteaae69be2004-08-29 23:32:14 +00001142
1143 return iptcc_is_builtin(c);
Marc Bouchere6869a82000-03-20 06:03:29 +00001144}
1145
1146/* Get the policy of a given built-in chain */
1147const char *
Rusty Russell79dee072000-05-02 16:45:16 +00001148TC_GET_POLICY(const char *chain,
1149 STRUCT_COUNTERS *counters,
1150 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001151{
Harald Welteaae69be2004-08-29 23:32:14 +00001152 struct chain_head *c;
Marc Bouchere6869a82000-03-20 06:03:29 +00001153
Harald Welteaae69be2004-08-29 23:32:14 +00001154 iptc_fn = TC_GET_POLICY;
1155
1156 DEBUGP("called for chain %s\n", chain);
1157
1158 c = iptcc_find_label(chain, *handle);
1159 if (!c) {
1160 errno = ENOENT;
1161 return NULL;
1162 }
1163
1164 if (!iptcc_is_builtin(c))
Marc Bouchere6869a82000-03-20 06:03:29 +00001165 return NULL;
1166
Harald Welteaae69be2004-08-29 23:32:14 +00001167 *counters = c->counters;
Marc Bouchere6869a82000-03-20 06:03:29 +00001168
Harald Welteaae69be2004-08-29 23:32:14 +00001169 return standard_target_map(c->verdict);
Harald Welte0113fe72004-01-06 19:04:02 +00001170}
1171
1172static int
Harald Welteaae69be2004-08-29 23:32:14 +00001173iptcc_standard_map(struct rule_head *r, int verdict)
Harald Welte0113fe72004-01-06 19:04:02 +00001174{
Harald Welteaae69be2004-08-29 23:32:14 +00001175 STRUCT_ENTRY *e = r->entry;
Rusty Russell79dee072000-05-02 16:45:16 +00001176 STRUCT_STANDARD_TARGET *t;
Marc Bouchere6869a82000-03-20 06:03:29 +00001177
Rusty Russell79dee072000-05-02 16:45:16 +00001178 t = (STRUCT_STANDARD_TARGET *)GET_TARGET(e);
Marc Bouchere6869a82000-03-20 06:03:29 +00001179
Rusty Russell67088e72000-05-10 01:18:57 +00001180 if (t->target.u.target_size
Philip Blundell8c700902000-05-15 02:17:52 +00001181 != ALIGN(sizeof(STRUCT_STANDARD_TARGET))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001182 errno = EINVAL;
1183 return 0;
1184 }
1185 /* memset for memcmp convenience on delete/replace */
Rusty Russell79dee072000-05-02 16:45:16 +00001186 memset(t->target.u.user.name, 0, FUNCTION_MAXNAMELEN);
1187 strcpy(t->target.u.user.name, STANDARD_TARGET);
Marc Bouchere6869a82000-03-20 06:03:29 +00001188 t->verdict = verdict;
1189
Harald Welteaae69be2004-08-29 23:32:14 +00001190 r->type = IPTCC_R_STANDARD;
1191
Marc Bouchere6869a82000-03-20 06:03:29 +00001192 return 1;
1193}
Rusty Russell7e53bf92000-03-20 07:03:28 +00001194
Marc Bouchere6869a82000-03-20 06:03:29 +00001195static int
Harald Welteaae69be2004-08-29 23:32:14 +00001196iptcc_map_target(const TC_HANDLE_T handle,
1197 struct rule_head *r)
Marc Bouchere6869a82000-03-20 06:03:29 +00001198{
Harald Welteaae69be2004-08-29 23:32:14 +00001199 STRUCT_ENTRY *e = r->entry;
Harald Welte0113fe72004-01-06 19:04:02 +00001200 STRUCT_ENTRY_TARGET *t = GET_TARGET(e);
Marc Bouchere6869a82000-03-20 06:03:29 +00001201
Marc Bouchere6869a82000-03-20 06:03:29 +00001202 /* Maybe it's empty (=> fall through) */
Harald Welteaae69be2004-08-29 23:32:14 +00001203 if (strcmp(t->u.user.name, "") == 0) {
1204 r->type = IPTCC_R_FALLTHROUGH;
1205 return 1;
1206 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001207 /* Maybe it's a standard target name... */
Rusty Russell79dee072000-05-02 16:45:16 +00001208 else if (strcmp(t->u.user.name, LABEL_ACCEPT) == 0)
Harald Welteaae69be2004-08-29 23:32:14 +00001209 return iptcc_standard_map(r, -NF_ACCEPT - 1);
Rusty Russell79dee072000-05-02 16:45:16 +00001210 else if (strcmp(t->u.user.name, LABEL_DROP) == 0)
Harald Welteaae69be2004-08-29 23:32:14 +00001211 return iptcc_standard_map(r, -NF_DROP - 1);
Rusty Russell67088e72000-05-10 01:18:57 +00001212 else if (strcmp(t->u.user.name, LABEL_QUEUE) == 0)
Harald Welteaae69be2004-08-29 23:32:14 +00001213 return iptcc_standard_map(r, -NF_QUEUE - 1);
Rusty Russell79dee072000-05-02 16:45:16 +00001214 else if (strcmp(t->u.user.name, LABEL_RETURN) == 0)
Harald Welteaae69be2004-08-29 23:32:14 +00001215 return iptcc_standard_map(r, RETURN);
Rusty Russell79dee072000-05-02 16:45:16 +00001216 else if (TC_BUILTIN(t->u.user.name, handle)) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001217 /* Can't jump to builtins. */
1218 errno = EINVAL;
1219 return 0;
1220 } else {
1221 /* Maybe it's an existing chain name. */
Harald Welteaae69be2004-08-29 23:32:14 +00001222 struct chain_head *c;
1223 DEBUGP("trying to find chain `%s': ", t->u.user.name);
Marc Bouchere6869a82000-03-20 06:03:29 +00001224
Harald Welteaae69be2004-08-29 23:32:14 +00001225 c = iptcc_find_label(t->u.user.name, handle);
1226 if (c) {
1227 DEBUGP_C("found!\n");
1228 r->type = IPTCC_R_JUMP;
1229 r->jump = c;
1230 c->references++;
1231 return 1;
1232 }
1233 DEBUGP_C("not found :(\n");
Marc Bouchere6869a82000-03-20 06:03:29 +00001234 }
1235
1236 /* Must be a module? If not, kernel will reject... */
1237 /* memset to all 0 for your memcmp convenience. */
Rusty Russell228e98d2000-04-27 10:28:06 +00001238 memset(t->u.user.name + strlen(t->u.user.name),
Marc Bouchere6869a82000-03-20 06:03:29 +00001239 0,
Rusty Russell79dee072000-05-02 16:45:16 +00001240 FUNCTION_MAXNAMELEN - strlen(t->u.user.name));
Harald Welteaae69be2004-08-29 23:32:14 +00001241
1242 set_changed(handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001243 return 1;
1244}
1245
Harald Welte0113fe72004-01-06 19:04:02 +00001246/* Insert the entry `fw' in chain `chain' into position `rulenum'. */
Marc Bouchere6869a82000-03-20 06:03:29 +00001247int
Rusty Russell79dee072000-05-02 16:45:16 +00001248TC_INSERT_ENTRY(const IPT_CHAINLABEL chain,
1249 const STRUCT_ENTRY *e,
1250 unsigned int rulenum,
1251 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001252{
Harald Welteaae69be2004-08-29 23:32:14 +00001253 struct chain_head *c;
Martin Josefssoneb066cc2004-09-22 21:04:07 +00001254 struct rule_head *r;
1255 struct list_head *prev;
Marc Bouchere6869a82000-03-20 06:03:29 +00001256
Rusty Russell79dee072000-05-02 16:45:16 +00001257 iptc_fn = TC_INSERT_ENTRY;
Harald Welteaae69be2004-08-29 23:32:14 +00001258
1259 if (!(c = iptcc_find_label(chain, *handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001260 errno = ENOENT;
1261 return 0;
1262 }
1263
Martin Josefssoneb066cc2004-09-22 21:04:07 +00001264 /* first rulenum index = 0
1265 first c->num_rules index = 1 */
1266 if (rulenum > c->num_rules) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001267 errno = E2BIG;
1268 return 0;
1269 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001270
Martin Josefsson631f3612004-09-23 19:25:06 +00001271 /* If we are inserting at the end just take advantage of the
1272 double linked list, insert will happen before the entry
1273 prev points to. */
1274 if (rulenum == c->num_rules) {
Martin Josefssoneb066cc2004-09-22 21:04:07 +00001275 prev = &c->rules;
Martin Josefssona5616dc2004-10-24 22:27:31 +00001276 } else if (rulenum + 1 <= c->num_rules/2) {
Martin Josefsson631f3612004-09-23 19:25:06 +00001277 r = iptcc_get_rule_num(c, rulenum + 1);
Martin Josefssona5616dc2004-10-24 22:27:31 +00001278 prev = &r->list;
1279 } else {
1280 r = iptcc_get_rule_num_reverse(c, c->num_rules - rulenum);
Martin Josefsson631f3612004-09-23 19:25:06 +00001281 prev = &r->list;
1282 }
Martin Josefssoneb066cc2004-09-22 21:04:07 +00001283
Harald Welteaae69be2004-08-29 23:32:14 +00001284 if (!(r = iptcc_alloc_rule(c, e->next_offset))) {
1285 errno = ENOMEM;
Harald Welte0113fe72004-01-06 19:04:02 +00001286 return 0;
Harald Welteaae69be2004-08-29 23:32:14 +00001287 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001288
Harald Welteaae69be2004-08-29 23:32:14 +00001289 memcpy(r->entry, e, e->next_offset);
1290 r->counter_map.maptype = COUNTER_MAP_SET;
1291
1292 if (!iptcc_map_target(*handle, r)) {
1293 free(r);
1294 return 0;
1295 }
1296
Martin Josefssoneb066cc2004-09-22 21:04:07 +00001297 list_add_tail(&r->list, prev);
Harald Welteaae69be2004-08-29 23:32:14 +00001298 c->num_rules++;
1299
1300 set_changed(*handle);
1301
1302 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00001303}
1304
1305/* Atomically replace rule `rulenum' in `chain' with `fw'. */
1306int
Rusty Russell79dee072000-05-02 16:45:16 +00001307TC_REPLACE_ENTRY(const IPT_CHAINLABEL chain,
1308 const STRUCT_ENTRY *e,
1309 unsigned int rulenum,
1310 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001311{
Harald Welteaae69be2004-08-29 23:32:14 +00001312 struct chain_head *c;
1313 struct rule_head *r, *old;
Marc Bouchere6869a82000-03-20 06:03:29 +00001314
Rusty Russell79dee072000-05-02 16:45:16 +00001315 iptc_fn = TC_REPLACE_ENTRY;
Marc Bouchere6869a82000-03-20 06:03:29 +00001316
Harald Welteaae69be2004-08-29 23:32:14 +00001317 if (!(c = iptcc_find_label(chain, *handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001318 errno = ENOENT;
1319 return 0;
1320 }
1321
Martin Josefsson8e795b02004-09-22 21:31:09 +00001322 if (!(old = iptcc_get_rule_num(c, rulenum + 1))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001323 errno = E2BIG;
1324 return 0;
1325 }
1326
Harald Welteaae69be2004-08-29 23:32:14 +00001327 if (!(r = iptcc_alloc_rule(c, e->next_offset))) {
1328 errno = ENOMEM;
Marc Bouchere6869a82000-03-20 06:03:29 +00001329 return 0;
Harald Welteaae69be2004-08-29 23:32:14 +00001330 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001331
Harald Welteaae69be2004-08-29 23:32:14 +00001332 memcpy(r->entry, e, e->next_offset);
1333 r->counter_map.maptype = COUNTER_MAP_SET;
1334
1335 if (!iptcc_map_target(*handle, r)) {
1336 free(r);
Harald Welte0113fe72004-01-06 19:04:02 +00001337 return 0;
Harald Welteaae69be2004-08-29 23:32:14 +00001338 }
Harald Welte0113fe72004-01-06 19:04:02 +00001339
Harald Welteaae69be2004-08-29 23:32:14 +00001340 list_add(&r->list, &old->list);
1341 iptcc_delete_rule(old);
1342
1343 set_changed(*handle);
1344
1345 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00001346}
1347
Harald Welte0113fe72004-01-06 19:04:02 +00001348/* Append entry `fw' to chain `chain'. Equivalent to insert with
Marc Bouchere6869a82000-03-20 06:03:29 +00001349 rulenum = length of chain. */
1350int
Rusty Russell79dee072000-05-02 16:45:16 +00001351TC_APPEND_ENTRY(const IPT_CHAINLABEL chain,
1352 const STRUCT_ENTRY *e,
1353 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001354{
Harald Welteaae69be2004-08-29 23:32:14 +00001355 struct chain_head *c;
1356 struct rule_head *r;
Marc Bouchere6869a82000-03-20 06:03:29 +00001357
Rusty Russell79dee072000-05-02 16:45:16 +00001358 iptc_fn = TC_APPEND_ENTRY;
Harald Welteaae69be2004-08-29 23:32:14 +00001359 if (!(c = iptcc_find_label(chain, *handle))) {
1360 DEBUGP("unable to find chain `%s'\n", chain);
Marc Bouchere6869a82000-03-20 06:03:29 +00001361 errno = ENOENT;
1362 return 0;
1363 }
1364
Harald Welteaae69be2004-08-29 23:32:14 +00001365 if (!(r = iptcc_alloc_rule(c, e->next_offset))) {
1366 DEBUGP("unable to allocate rule for chain `%s'\n", chain);
1367 errno = ENOMEM;
Harald Welte0113fe72004-01-06 19:04:02 +00001368 return 0;
Harald Welteaae69be2004-08-29 23:32:14 +00001369 }
Harald Welte0113fe72004-01-06 19:04:02 +00001370
Harald Welteaae69be2004-08-29 23:32:14 +00001371 memcpy(r->entry, e, e->next_offset);
1372 r->counter_map.maptype = COUNTER_MAP_SET;
1373
1374 if (!iptcc_map_target(*handle, r)) {
Martin Josefsson12009532004-09-23 18:24:29 +00001375 DEBUGP("unable to map target of rule for chain `%s'\n", chain);
Harald Welteaae69be2004-08-29 23:32:14 +00001376 free(r);
1377 return 0;
1378 }
1379
1380 list_add_tail(&r->list, &c->rules);
1381 c->num_rules++;
1382
1383 set_changed(*handle);
1384
1385 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00001386}
1387
1388static inline int
Rusty Russell79dee072000-05-02 16:45:16 +00001389match_different(const STRUCT_ENTRY_MATCH *a,
Rusty Russelledf14cf2000-04-19 11:26:44 +00001390 const unsigned char *a_elems,
1391 const unsigned char *b_elems,
1392 unsigned char **maskptr)
Marc Bouchere6869a82000-03-20 06:03:29 +00001393{
Rusty Russell79dee072000-05-02 16:45:16 +00001394 const STRUCT_ENTRY_MATCH *b;
Rusty Russelledf14cf2000-04-19 11:26:44 +00001395 unsigned int i;
Marc Bouchere6869a82000-03-20 06:03:29 +00001396
1397 /* Offset of b is the same as a. */
Rusty Russell30fd6e52000-04-23 09:16:06 +00001398 b = (void *)b_elems + ((unsigned char *)a - a_elems);
Marc Bouchere6869a82000-03-20 06:03:29 +00001399
Rusty Russell228e98d2000-04-27 10:28:06 +00001400 if (a->u.match_size != b->u.match_size)
Marc Bouchere6869a82000-03-20 06:03:29 +00001401 return 1;
1402
Rusty Russell228e98d2000-04-27 10:28:06 +00001403 if (strcmp(a->u.user.name, b->u.user.name) != 0)
Marc Bouchere6869a82000-03-20 06:03:29 +00001404 return 1;
1405
Rusty Russell73ef09b2000-07-03 10:24:04 +00001406 *maskptr += ALIGN(sizeof(*a));
Rusty Russelledf14cf2000-04-19 11:26:44 +00001407
Rusty Russell73ef09b2000-07-03 10:24:04 +00001408 for (i = 0; i < a->u.match_size - ALIGN(sizeof(*a)); i++)
Rusty Russelledf14cf2000-04-19 11:26:44 +00001409 if (((a->data[i] ^ b->data[i]) & (*maskptr)[i]) != 0)
Rusty Russell90e712a2000-03-29 04:19:26 +00001410 return 1;
Rusty Russelledf14cf2000-04-19 11:26:44 +00001411 *maskptr += i;
1412 return 0;
1413}
1414
1415static inline int
1416target_different(const unsigned char *a_targdata,
1417 const unsigned char *b_targdata,
1418 unsigned int tdatasize,
1419 const unsigned char *mask)
1420{
1421 unsigned int i;
1422 for (i = 0; i < tdatasize; i++)
1423 if (((a_targdata[i] ^ b_targdata[i]) & mask[i]) != 0)
1424 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00001425
1426 return 0;
1427}
1428
Rusty Russell79dee072000-05-02 16:45:16 +00001429static int
1430is_same(const STRUCT_ENTRY *a,
1431 const STRUCT_ENTRY *b,
1432 unsigned char *matchmask);
Marc Bouchere6869a82000-03-20 06:03:29 +00001433
Harald Welte0113fe72004-01-06 19:04:02 +00001434/* Delete the first rule in `chain' which matches `fw'. */
Marc Bouchere6869a82000-03-20 06:03:29 +00001435int
Rusty Russell79dee072000-05-02 16:45:16 +00001436TC_DELETE_ENTRY(const IPT_CHAINLABEL chain,
1437 const STRUCT_ENTRY *origfw,
1438 unsigned char *matchmask,
1439 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001440{
Harald Welteaae69be2004-08-29 23:32:14 +00001441 struct chain_head *c;
Rusty Russelle45c7132004-12-16 13:21:44 +00001442 struct rule_head *r, *i;
Marc Bouchere6869a82000-03-20 06:03:29 +00001443
Rusty Russell79dee072000-05-02 16:45:16 +00001444 iptc_fn = TC_DELETE_ENTRY;
Harald Welteaae69be2004-08-29 23:32:14 +00001445 if (!(c = iptcc_find_label(chain, *handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001446 errno = ENOENT;
1447 return 0;
1448 }
1449
Rusty Russelle45c7132004-12-16 13:21:44 +00001450 /* Create a rule_head from origfw. */
1451 r = iptcc_alloc_rule(c, origfw->next_offset);
1452 if (!r) {
Harald Welte0113fe72004-01-06 19:04:02 +00001453 errno = ENOMEM;
1454 return 0;
1455 }
1456
Rusty Russelle45c7132004-12-16 13:21:44 +00001457 memcpy(r->entry, origfw, origfw->next_offset);
1458 r->counter_map.maptype = COUNTER_MAP_NOMAP;
1459 if (!iptcc_map_target(*handle, r)) {
1460 DEBUGP("unable to map target of rule for chain `%s'\n", chain);
1461 free(r);
1462 return 0;
1463 }
Harald Welte0113fe72004-01-06 19:04:02 +00001464
Rusty Russelle45c7132004-12-16 13:21:44 +00001465 list_for_each_entry(i, &c->rules, list) {
1466 if (r->type == i->type
1467 && is_same(r->entry, i->entry, matchmask)) {
Harald Weltefe537072004-08-30 20:28:53 +00001468 /* If we are about to delete the rule that is the
1469 * current iterator, move rule iterator back. next
1470 * pointer will then point to real next node */
Rusty Russelle45c7132004-12-16 13:21:44 +00001471 if (i == (*handle)->rule_iterator_cur) {
Harald Weltefe537072004-08-30 20:28:53 +00001472 (*handle)->rule_iterator_cur =
1473 list_entry((*handle)->rule_iterator_cur->list.prev,
1474 struct rule_head, list);
1475 }
1476
1477 c->num_rules--;
Rusty Russelle45c7132004-12-16 13:21:44 +00001478 iptcc_delete_rule(i);
Martin Josefsson2a5dbbb2004-09-22 21:37:41 +00001479
1480 set_changed(*handle);
Rusty Russelle45c7132004-12-16 13:21:44 +00001481 free(r);
Harald Weltefe537072004-08-30 20:28:53 +00001482 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00001483 }
1484 }
1485
Rusty Russelle45c7132004-12-16 13:21:44 +00001486 free(r);
Marc Bouchere6869a82000-03-20 06:03:29 +00001487 errno = ENOENT;
1488 return 0;
Rusty Russell7e53bf92000-03-20 07:03:28 +00001489}
Harald Welteaae69be2004-08-29 23:32:14 +00001490
Marc Bouchere6869a82000-03-20 06:03:29 +00001491
1492/* Delete the rule in position `rulenum' in `chain'. */
1493int
Rusty Russell79dee072000-05-02 16:45:16 +00001494TC_DELETE_NUM_ENTRY(const IPT_CHAINLABEL chain,
1495 unsigned int rulenum,
1496 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001497{
Harald Welteaae69be2004-08-29 23:32:14 +00001498 struct chain_head *c;
1499 struct rule_head *r;
Marc Bouchere6869a82000-03-20 06:03:29 +00001500
Rusty Russell79dee072000-05-02 16:45:16 +00001501 iptc_fn = TC_DELETE_NUM_ENTRY;
Harald Welteaae69be2004-08-29 23:32:14 +00001502
1503 if (!(c = iptcc_find_label(chain, *handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001504 errno = ENOENT;
1505 return 0;
1506 }
1507
Martin Josefssona5616dc2004-10-24 22:27:31 +00001508 if (rulenum >= c->num_rules) {
Martin Josefsson631f3612004-09-23 19:25:06 +00001509 errno = E2BIG;
1510 return 0;
1511 }
1512
1513 /* Take advantage of the double linked list if possible. */
Martin Josefssona5616dc2004-10-24 22:27:31 +00001514 if (rulenum + 1 <= c->num_rules/2) {
1515 r = iptcc_get_rule_num(c, rulenum + 1);
1516 } else {
1517 r = iptcc_get_rule_num_reverse(c, c->num_rules - rulenum);
Marc Bouchere6869a82000-03-20 06:03:29 +00001518 }
1519
Harald Welteaae69be2004-08-29 23:32:14 +00001520 /* If we are about to delete the rule that is the current
1521 * iterator, move rule iterator back. next pointer will then
1522 * point to real next node */
1523 if (r == (*handle)->rule_iterator_cur) {
1524 (*handle)->rule_iterator_cur =
1525 list_entry((*handle)->rule_iterator_cur->list.prev,
1526 struct rule_head, list);
Harald Welte0113fe72004-01-06 19:04:02 +00001527 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001528
Harald Welteaae69be2004-08-29 23:32:14 +00001529 c->num_rules--;
1530 iptcc_delete_rule(r);
1531
Martin Josefsson2a5dbbb2004-09-22 21:37:41 +00001532 set_changed(*handle);
1533
Harald Welteaae69be2004-08-29 23:32:14 +00001534 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00001535}
1536
1537/* Check the packet `fw' on chain `chain'. Returns the verdict, or
1538 NULL and sets errno. */
1539const char *
Rusty Russell79dee072000-05-02 16:45:16 +00001540TC_CHECK_PACKET(const IPT_CHAINLABEL chain,
1541 STRUCT_ENTRY *entry,
1542 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001543{
1544 errno = ENOSYS;
1545 return NULL;
1546}
1547
1548/* Flushes the entries in the given chain (ie. empties chain). */
1549int
Rusty Russell79dee072000-05-02 16:45:16 +00001550TC_FLUSH_ENTRIES(const IPT_CHAINLABEL chain, TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001551{
Harald Welteaae69be2004-08-29 23:32:14 +00001552 struct chain_head *c;
1553 struct rule_head *r, *tmp;
Marc Bouchere6869a82000-03-20 06:03:29 +00001554
Harald Welte0113fe72004-01-06 19:04:02 +00001555 iptc_fn = TC_FLUSH_ENTRIES;
Harald Welteaae69be2004-08-29 23:32:14 +00001556 if (!(c = iptcc_find_label(chain, *handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001557 errno = ENOENT;
1558 return 0;
1559 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001560
Harald Welteaae69be2004-08-29 23:32:14 +00001561 list_for_each_entry_safe(r, tmp, &c->rules, list) {
1562 iptcc_delete_rule(r);
1563 }
1564
1565 c->num_rules = 0;
1566
1567 set_changed(*handle);
1568
1569 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00001570}
1571
1572/* Zeroes the counters in a chain. */
1573int
Rusty Russell79dee072000-05-02 16:45:16 +00001574TC_ZERO_ENTRIES(const IPT_CHAINLABEL chain, TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001575{
Harald Welteaae69be2004-08-29 23:32:14 +00001576 struct chain_head *c;
1577 struct rule_head *r;
Rusty Russell7e53bf92000-03-20 07:03:28 +00001578
Harald Welteaae69be2004-08-29 23:32:14 +00001579 if (!(c = iptcc_find_label(chain, *handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001580 errno = ENOENT;
1581 return 0;
1582 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001583
Harald Welteaae69be2004-08-29 23:32:14 +00001584 list_for_each_entry(r, &c->rules, list) {
1585 if (r->counter_map.maptype == COUNTER_MAP_NORMAL_MAP)
1586 r->counter_map.maptype = COUNTER_MAP_ZEROED;
Marc Bouchere6869a82000-03-20 06:03:29 +00001587 }
Harald Welteaae69be2004-08-29 23:32:14 +00001588
Rusty Russell175f6412000-03-24 09:32:20 +00001589 set_changed(*handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001590
Marc Bouchere6869a82000-03-20 06:03:29 +00001591 return 1;
1592}
1593
Harald Welte1cef74d2001-01-05 15:22:59 +00001594STRUCT_COUNTERS *
1595TC_READ_COUNTER(const IPT_CHAINLABEL chain,
1596 unsigned int rulenum,
1597 TC_HANDLE_T *handle)
1598{
Harald Welteaae69be2004-08-29 23:32:14 +00001599 struct chain_head *c;
1600 struct rule_head *r;
Harald Welte1cef74d2001-01-05 15:22:59 +00001601
1602 iptc_fn = TC_READ_COUNTER;
1603 CHECK(*handle);
1604
Harald Welteaae69be2004-08-29 23:32:14 +00001605 if (!(c = iptcc_find_label(chain, *handle))) {
Harald Welte1cef74d2001-01-05 15:22:59 +00001606 errno = ENOENT;
1607 return NULL;
1608 }
1609
Harald Welteaae69be2004-08-29 23:32:14 +00001610 if (!(r = iptcc_get_rule_num(c, rulenum))) {
Harald Welte0113fe72004-01-06 19:04:02 +00001611 errno = E2BIG;
1612 return NULL;
1613 }
1614
Harald Welteaae69be2004-08-29 23:32:14 +00001615 return &r->entry[0].counters;
Harald Welte1cef74d2001-01-05 15:22:59 +00001616}
1617
1618int
1619TC_ZERO_COUNTER(const IPT_CHAINLABEL chain,
1620 unsigned int rulenum,
1621 TC_HANDLE_T *handle)
1622{
Harald Welteaae69be2004-08-29 23:32:14 +00001623 struct chain_head *c;
1624 struct rule_head *r;
Harald Welte1cef74d2001-01-05 15:22:59 +00001625
1626 iptc_fn = TC_ZERO_COUNTER;
1627 CHECK(*handle);
1628
Harald Welteaae69be2004-08-29 23:32:14 +00001629 if (!(c = iptcc_find_label(chain, *handle))) {
Harald Welte1cef74d2001-01-05 15:22:59 +00001630 errno = ENOENT;
1631 return 0;
1632 }
1633
Harald Welteaae69be2004-08-29 23:32:14 +00001634 if (!(r = iptcc_get_rule_num(c, rulenum))) {
Harald Welte0113fe72004-01-06 19:04:02 +00001635 errno = E2BIG;
1636 return 0;
1637 }
1638
Harald Welteaae69be2004-08-29 23:32:14 +00001639 if (r->counter_map.maptype == COUNTER_MAP_NORMAL_MAP)
1640 r->counter_map.maptype = COUNTER_MAP_ZEROED;
Harald Welte1cef74d2001-01-05 15:22:59 +00001641
1642 set_changed(*handle);
1643
1644 return 1;
1645}
1646
1647int
1648TC_SET_COUNTER(const IPT_CHAINLABEL chain,
1649 unsigned int rulenum,
1650 STRUCT_COUNTERS *counters,
1651 TC_HANDLE_T *handle)
1652{
Harald Welteaae69be2004-08-29 23:32:14 +00001653 struct chain_head *c;
1654 struct rule_head *r;
Harald Welte1cef74d2001-01-05 15:22:59 +00001655 STRUCT_ENTRY *e;
Harald Welte1cef74d2001-01-05 15:22:59 +00001656
1657 iptc_fn = TC_SET_COUNTER;
1658 CHECK(*handle);
1659
Harald Welteaae69be2004-08-29 23:32:14 +00001660 if (!(c = iptcc_find_label(chain, *handle))) {
Harald Welte1cef74d2001-01-05 15:22:59 +00001661 errno = ENOENT;
1662 return 0;
1663 }
Harald Welte0113fe72004-01-06 19:04:02 +00001664
Harald Welteaae69be2004-08-29 23:32:14 +00001665 if (!(r = iptcc_get_rule_num(c, rulenum))) {
Harald Welte0113fe72004-01-06 19:04:02 +00001666 errno = E2BIG;
1667 return 0;
1668 }
1669
Harald Welteaae69be2004-08-29 23:32:14 +00001670 e = r->entry;
1671 r->counter_map.maptype = COUNTER_MAP_SET;
Harald Welte0113fe72004-01-06 19:04:02 +00001672
1673 memcpy(&e->counters, counters, sizeof(STRUCT_COUNTERS));
Harald Welte1cef74d2001-01-05 15:22:59 +00001674
1675 set_changed(*handle);
1676
1677 return 1;
1678}
1679
Marc Bouchere6869a82000-03-20 06:03:29 +00001680/* Creates a new chain. */
1681/* To create a chain, create two rules: error node and unconditional
1682 * return. */
1683int
Rusty Russell79dee072000-05-02 16:45:16 +00001684TC_CREATE_CHAIN(const IPT_CHAINLABEL chain, TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001685{
Harald Welteaae69be2004-08-29 23:32:14 +00001686 static struct chain_head *c;
Marc Bouchere6869a82000-03-20 06:03:29 +00001687
Rusty Russell79dee072000-05-02 16:45:16 +00001688 iptc_fn = TC_CREATE_CHAIN;
Marc Bouchere6869a82000-03-20 06:03:29 +00001689
1690 /* find_label doesn't cover built-in targets: DROP, ACCEPT,
1691 QUEUE, RETURN. */
Harald Welteaae69be2004-08-29 23:32:14 +00001692 if (iptcc_find_label(chain, *handle)
Rusty Russell79dee072000-05-02 16:45:16 +00001693 || strcmp(chain, LABEL_DROP) == 0
1694 || strcmp(chain, LABEL_ACCEPT) == 0
Rusty Russell67088e72000-05-10 01:18:57 +00001695 || strcmp(chain, LABEL_QUEUE) == 0
Rusty Russell79dee072000-05-02 16:45:16 +00001696 || strcmp(chain, LABEL_RETURN) == 0) {
Harald Welteaae69be2004-08-29 23:32:14 +00001697 DEBUGP("Chain `%s' already exists\n", chain);
Marc Bouchere6869a82000-03-20 06:03:29 +00001698 errno = EEXIST;
1699 return 0;
1700 }
1701
Rusty Russell79dee072000-05-02 16:45:16 +00001702 if (strlen(chain)+1 > sizeof(IPT_CHAINLABEL)) {
Harald Welteaae69be2004-08-29 23:32:14 +00001703 DEBUGP("Chain name `%s' too long\n", chain);
Marc Bouchere6869a82000-03-20 06:03:29 +00001704 errno = EINVAL;
1705 return 0;
1706 }
1707
Harald Welteaae69be2004-08-29 23:32:14 +00001708 c = iptcc_alloc_chain_head(chain, 0);
1709 if (!c) {
1710 DEBUGP("Cannot allocate memory for chain `%s'\n", chain);
1711 errno = ENOMEM;
1712 return 0;
Marc Bouchere6869a82000-03-20 06:03:29 +00001713
Harald Welteaae69be2004-08-29 23:32:14 +00001714 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001715
Harald Welteaae69be2004-08-29 23:32:14 +00001716 DEBUGP("Creating chain `%s'\n", chain);
1717 list_add_tail(&c->list, &(*handle)->chains);
Harald Welte0113fe72004-01-06 19:04:02 +00001718
1719 set_changed(*handle);
1720
Harald Welteaae69be2004-08-29 23:32:14 +00001721 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00001722}
1723
1724/* Get the number of references to this chain. */
1725int
Rusty Russell79dee072000-05-02 16:45:16 +00001726TC_GET_REFERENCES(unsigned int *ref, const IPT_CHAINLABEL chain,
1727 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001728{
Harald Welteaae69be2004-08-29 23:32:14 +00001729 struct chain_head *c;
Marc Bouchere6869a82000-03-20 06:03:29 +00001730
Harald Welteaae69be2004-08-29 23:32:14 +00001731 if (!(c = iptcc_find_label(chain, *handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001732 errno = ENOENT;
1733 return 0;
1734 }
1735
Harald Welteaae69be2004-08-29 23:32:14 +00001736 *ref = c->references;
1737
Marc Bouchere6869a82000-03-20 06:03:29 +00001738 return 1;
1739}
1740
1741/* Deletes a chain. */
1742int
Rusty Russell79dee072000-05-02 16:45:16 +00001743TC_DELETE_CHAIN(const IPT_CHAINLABEL chain, TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001744{
Marc Bouchere6869a82000-03-20 06:03:29 +00001745 unsigned int references;
Harald Welteaae69be2004-08-29 23:32:14 +00001746 struct chain_head *c;
Rusty Russell7e53bf92000-03-20 07:03:28 +00001747
Rusty Russell79dee072000-05-02 16:45:16 +00001748 iptc_fn = TC_DELETE_CHAIN;
Marc Bouchere6869a82000-03-20 06:03:29 +00001749
Harald Welteaae69be2004-08-29 23:32:14 +00001750 if (!(c = iptcc_find_label(chain, *handle))) {
1751 DEBUGP("cannot find chain `%s'\n", chain);
Marc Bouchere6869a82000-03-20 06:03:29 +00001752 errno = ENOENT;
1753 return 0;
1754 }
1755
Harald Welteaae69be2004-08-29 23:32:14 +00001756 if (TC_BUILTIN(chain, *handle)) {
1757 DEBUGP("cannot remove builtin chain `%s'\n", chain);
1758 errno = EINVAL;
1759 return 0;
1760 }
1761
1762 if (!TC_GET_REFERENCES(&references, chain, handle)) {
1763 DEBUGP("cannot get references on chain `%s'\n", chain);
1764 return 0;
1765 }
1766
1767 if (references > 0) {
1768 DEBUGP("chain `%s' still has references\n", chain);
1769 errno = EMLINK;
1770 return 0;
1771 }
1772
1773 if (c->num_rules) {
1774 DEBUGP("chain `%s' is not empty\n", chain);
Marc Bouchere6869a82000-03-20 06:03:29 +00001775 errno = ENOTEMPTY;
1776 return 0;
1777 }
1778
Harald Welteaae69be2004-08-29 23:32:14 +00001779 /* If we are about to delete the chain that is the current
1780 * iterator, move chain iterator firward. */
1781 if (c == (*handle)->chain_iterator_cur)
1782 iptcc_chain_iterator_advance(*handle);
Harald Welte0113fe72004-01-06 19:04:02 +00001783
Harald Welteaae69be2004-08-29 23:32:14 +00001784 list_del(&c->list);
1785 free(c);
Harald Welte0113fe72004-01-06 19:04:02 +00001786
Harald Welteaae69be2004-08-29 23:32:14 +00001787 DEBUGP("chain `%s' deleted\n", chain);
1788
1789 set_changed(*handle);
1790
1791 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00001792}
1793
1794/* Renames a chain. */
Rusty Russell79dee072000-05-02 16:45:16 +00001795int TC_RENAME_CHAIN(const IPT_CHAINLABEL oldname,
1796 const IPT_CHAINLABEL newname,
1797 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001798{
Harald Welteaae69be2004-08-29 23:32:14 +00001799 struct chain_head *c;
Rusty Russell79dee072000-05-02 16:45:16 +00001800 iptc_fn = TC_RENAME_CHAIN;
Marc Bouchere6869a82000-03-20 06:03:29 +00001801
Harald Welte1de80462000-10-30 12:00:27 +00001802 /* find_label doesn't cover built-in targets: DROP, ACCEPT,
1803 QUEUE, RETURN. */
Harald Welteaae69be2004-08-29 23:32:14 +00001804 if (iptcc_find_label(newname, *handle)
Rusty Russell79dee072000-05-02 16:45:16 +00001805 || strcmp(newname, LABEL_DROP) == 0
1806 || strcmp(newname, LABEL_ACCEPT) == 0
Harald Welte1de80462000-10-30 12:00:27 +00001807 || strcmp(newname, LABEL_QUEUE) == 0
Rusty Russell79dee072000-05-02 16:45:16 +00001808 || strcmp(newname, LABEL_RETURN) == 0) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001809 errno = EEXIST;
1810 return 0;
1811 }
1812
Harald Welteaae69be2004-08-29 23:32:14 +00001813 if (!(c = iptcc_find_label(oldname, *handle))
Rusty Russell79dee072000-05-02 16:45:16 +00001814 || TC_BUILTIN(oldname, *handle)) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001815 errno = ENOENT;
1816 return 0;
1817 }
1818
Rusty Russell79dee072000-05-02 16:45:16 +00001819 if (strlen(newname)+1 > sizeof(IPT_CHAINLABEL)) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001820 errno = EINVAL;
1821 return 0;
1822 }
1823
Harald Welteaae69be2004-08-29 23:32:14 +00001824 strncpy(c->name, newname, sizeof(IPT_CHAINLABEL));
1825
Harald Welte0113fe72004-01-06 19:04:02 +00001826 set_changed(*handle);
1827
Marc Bouchere6869a82000-03-20 06:03:29 +00001828 return 1;
1829}
1830
1831/* Sets the policy on a built-in chain. */
1832int
Rusty Russell79dee072000-05-02 16:45:16 +00001833TC_SET_POLICY(const IPT_CHAINLABEL chain,
1834 const IPT_CHAINLABEL policy,
Harald Welte1cef74d2001-01-05 15:22:59 +00001835 STRUCT_COUNTERS *counters,
Rusty Russell79dee072000-05-02 16:45:16 +00001836 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001837{
Harald Welteaae69be2004-08-29 23:32:14 +00001838 struct chain_head *c;
Marc Bouchere6869a82000-03-20 06:03:29 +00001839
Rusty Russell79dee072000-05-02 16:45:16 +00001840 iptc_fn = TC_SET_POLICY;
Marc Bouchere6869a82000-03-20 06:03:29 +00001841
Harald Welteaae69be2004-08-29 23:32:14 +00001842 if (!(c = iptcc_find_label(chain, *handle))) {
1843 DEBUGP("cannot find chain `%s'\n", chain);
1844 errno = ENOENT;
Marc Bouchere6869a82000-03-20 06:03:29 +00001845 return 0;
1846 }
1847
Harald Welteaae69be2004-08-29 23:32:14 +00001848 if (!iptcc_is_builtin(c)) {
1849 DEBUGP("cannot set policy of userdefinedchain `%s'\n", chain);
1850 errno = ENOENT;
1851 return 0;
1852 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001853
Rusty Russell79dee072000-05-02 16:45:16 +00001854 if (strcmp(policy, LABEL_ACCEPT) == 0)
Harald Welteaae69be2004-08-29 23:32:14 +00001855 c->verdict = -NF_ACCEPT - 1;
Rusty Russell79dee072000-05-02 16:45:16 +00001856 else if (strcmp(policy, LABEL_DROP) == 0)
Harald Welteaae69be2004-08-29 23:32:14 +00001857 c->verdict = -NF_DROP - 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00001858 else {
1859 errno = EINVAL;
1860 return 0;
1861 }
Harald Welte1cef74d2001-01-05 15:22:59 +00001862
Harald Welte1cef74d2001-01-05 15:22:59 +00001863 if (counters) {
1864 /* set byte and packet counters */
Harald Welteaae69be2004-08-29 23:32:14 +00001865 memcpy(&c->counters, counters, sizeof(STRUCT_COUNTERS));
1866 c->counter_map.maptype = COUNTER_MAP_SET;
Harald Welte1cef74d2001-01-05 15:22:59 +00001867 } else {
Harald Welteaae69be2004-08-29 23:32:14 +00001868 c->counter_map.maptype = COUNTER_MAP_NOMAP;
Harald Welte1cef74d2001-01-05 15:22:59 +00001869 }
1870
Rusty Russell175f6412000-03-24 09:32:20 +00001871 set_changed(*handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001872
Marc Bouchere6869a82000-03-20 06:03:29 +00001873 return 1;
1874}
1875
1876/* Without this, on gcc 2.7.2.3, we get:
Rusty Russell79dee072000-05-02 16:45:16 +00001877 libiptc.c: In function `TC_COMMIT':
Marc Bouchere6869a82000-03-20 06:03:29 +00001878 libiptc.c:833: fixed or forbidden register was spilled.
1879 This may be due to a compiler bug or to impossible asm
1880 statements or clauses.
1881*/
1882static void
Rusty Russell79dee072000-05-02 16:45:16 +00001883subtract_counters(STRUCT_COUNTERS *answer,
1884 const STRUCT_COUNTERS *a,
1885 const STRUCT_COUNTERS *b)
Marc Bouchere6869a82000-03-20 06:03:29 +00001886{
1887 answer->pcnt = a->pcnt - b->pcnt;
1888 answer->bcnt = a->bcnt - b->bcnt;
1889}
1890
Harald Welteaae69be2004-08-29 23:32:14 +00001891
1892static void counters_nomap(STRUCT_COUNTERS_INFO *newcounters,
1893 unsigned int index)
1894{
1895 newcounters->counters[index] = ((STRUCT_COUNTERS) { 0, 0});
1896 DEBUGP_C("NOMAP => zero\n");
1897}
1898
1899static void counters_normal_map(STRUCT_COUNTERS_INFO *newcounters,
1900 STRUCT_REPLACE *repl,
1901 unsigned int index,
1902 unsigned int mappos)
1903{
1904 /* Original read: X.
1905 * Atomic read on replacement: X + Y.
1906 * Currently in kernel: Z.
1907 * Want in kernel: X + Y + Z.
1908 * => Add in X + Y
1909 * => Add in replacement read.
1910 */
1911 newcounters->counters[index] = repl->counters[mappos];
1912 DEBUGP_C("NORMAL_MAP => mappos %u \n", mappos);
1913}
1914
1915static void counters_map_zeroed(STRUCT_COUNTERS_INFO *newcounters,
1916 STRUCT_REPLACE *repl,
1917 unsigned int index,
1918 unsigned int mappos,
1919 STRUCT_COUNTERS *counters)
1920{
1921 /* Original read: X.
1922 * Atomic read on replacement: X + Y.
1923 * Currently in kernel: Z.
1924 * Want in kernel: Y + Z.
1925 * => Add in Y.
1926 * => Add in (replacement read - original read).
1927 */
1928 subtract_counters(&newcounters->counters[index],
1929 &repl->counters[mappos],
1930 counters);
1931 DEBUGP_C("ZEROED => mappos %u\n", mappos);
1932}
1933
1934static void counters_map_set(STRUCT_COUNTERS_INFO *newcounters,
1935 unsigned int index,
1936 STRUCT_COUNTERS *counters)
1937{
1938 /* Want to set counter (iptables-restore) */
1939
1940 memcpy(&newcounters->counters[index], counters,
1941 sizeof(STRUCT_COUNTERS));
1942
1943 DEBUGP_C("SET\n");
1944}
1945
1946
Marc Bouchere6869a82000-03-20 06:03:29 +00001947int
Rusty Russell79dee072000-05-02 16:45:16 +00001948TC_COMMIT(TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001949{
1950 /* Replace, then map back the counters. */
Rusty Russell79dee072000-05-02 16:45:16 +00001951 STRUCT_REPLACE *repl;
1952 STRUCT_COUNTERS_INFO *newcounters;
Harald Welteaae69be2004-08-29 23:32:14 +00001953 struct chain_head *c;
1954 int ret;
Martin Josefsson841e4ae2003-05-02 15:30:11 +00001955 size_t counterlen;
Harald Welteaae69be2004-08-29 23:32:14 +00001956 int new_number;
1957 unsigned int new_size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001958
1959 CHECK(*handle);
Martin Josefsson841e4ae2003-05-02 15:30:11 +00001960
Marc Bouchere6869a82000-03-20 06:03:29 +00001961 /* Don't commit if nothing changed. */
1962 if (!(*handle)->changed)
1963 goto finished;
1964
Harald Welteaae69be2004-08-29 23:32:14 +00001965 new_number = iptcc_compile_table_prep(*handle, &new_size);
1966 if (new_number < 0) {
1967 errno = ENOMEM;
1968 return 0;
1969 }
1970
1971 repl = malloc(sizeof(*repl) + new_size);
Marc Bouchere6869a82000-03-20 06:03:29 +00001972 if (!repl) {
1973 errno = ENOMEM;
1974 return 0;
1975 }
Martin Josefssonad3b4f92004-09-22 22:04:07 +00001976 memset(repl, 0, sizeof(*repl) + new_size);
Harald Welteaae69be2004-08-29 23:32:14 +00001977
Rusty Russelle45c7132004-12-16 13:21:44 +00001978#if 0
1979 TC_DUMP_ENTRIES(*handle);
1980#endif
1981
Harald Welteaae69be2004-08-29 23:32:14 +00001982 counterlen = sizeof(STRUCT_COUNTERS_INFO)
1983 + sizeof(STRUCT_COUNTERS) * new_number;
Marc Bouchere6869a82000-03-20 06:03:29 +00001984
1985 /* These are the old counters we will get from kernel */
Rusty Russell79dee072000-05-02 16:45:16 +00001986 repl->counters = malloc(sizeof(STRUCT_COUNTERS)
Marc Bouchere6869a82000-03-20 06:03:29 +00001987 * (*handle)->info.num_entries);
1988 if (!repl->counters) {
1989 free(repl);
1990 errno = ENOMEM;
1991 return 0;
1992 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001993 /* These are the counters we're going to put back, later. */
1994 newcounters = malloc(counterlen);
1995 if (!newcounters) {
1996 free(repl->counters);
1997 free(repl);
1998 errno = ENOMEM;
1999 return 0;
2000 }
Harald Welteaae69be2004-08-29 23:32:14 +00002001 memset(newcounters, 0, counterlen);
Marc Bouchere6869a82000-03-20 06:03:29 +00002002
2003 strcpy(repl->name, (*handle)->info.name);
Harald Welteaae69be2004-08-29 23:32:14 +00002004 repl->num_entries = new_number;
2005 repl->size = new_size;
2006
Marc Bouchere6869a82000-03-20 06:03:29 +00002007 repl->num_counters = (*handle)->info.num_entries;
2008 repl->valid_hooks = (*handle)->info.valid_hooks;
Harald Welteaae69be2004-08-29 23:32:14 +00002009
2010 DEBUGP("num_entries=%u, size=%u, num_counters=%u\n",
2011 repl->num_entries, repl->size, repl->num_counters);
2012
2013 ret = iptcc_compile_table(*handle, repl);
2014 if (ret < 0) {
2015 errno = ret;
2016 free(repl->counters);
2017 free(repl);
2018 return 0;
2019 }
2020
2021
2022#ifdef IPTC_DEBUG2
2023 {
2024 int fd = open("/tmp/libiptc-so_set_replace.blob",
2025 O_CREAT|O_WRONLY);
2026 if (fd >= 0) {
2027 write(fd, repl, sizeof(*repl) + repl->size);
2028 close(fd);
2029 }
2030 }
2031#endif
Marc Bouchere6869a82000-03-20 06:03:29 +00002032
Rusty Russell79dee072000-05-02 16:45:16 +00002033 if (setsockopt(sockfd, TC_IPPROTO, SO_SET_REPLACE, repl,
Harald Welteaae69be2004-08-29 23:32:14 +00002034 sizeof(*repl) + repl->size) < 0) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002035 free(repl->counters);
2036 free(repl);
2037 free(newcounters);
2038 return 0;
2039 }
2040
2041 /* Put counters back. */
2042 strcpy(newcounters->name, (*handle)->info.name);
Harald Welteaae69be2004-08-29 23:32:14 +00002043 newcounters->num_counters = new_number;
Marc Bouchere6869a82000-03-20 06:03:29 +00002044
Harald Welteaae69be2004-08-29 23:32:14 +00002045 list_for_each_entry(c, &(*handle)->chains, list) {
2046 struct rule_head *r;
Marc Bouchere6869a82000-03-20 06:03:29 +00002047
Harald Welteaae69be2004-08-29 23:32:14 +00002048 /* Builtin chains have their own counters */
2049 if (iptcc_is_builtin(c)) {
2050 DEBUGP("counter for chain-index %u: ", c->foot_index);
2051 switch(c->counter_map.maptype) {
2052 case COUNTER_MAP_NOMAP:
2053 counters_nomap(newcounters, c->foot_index);
2054 break;
2055 case COUNTER_MAP_NORMAL_MAP:
2056 counters_normal_map(newcounters, repl,
2057 c->foot_index,
2058 c->counter_map.mappos);
2059 break;
2060 case COUNTER_MAP_ZEROED:
2061 counters_map_zeroed(newcounters, repl,
2062 c->foot_index,
2063 c->counter_map.mappos,
2064 &c->counters);
2065 break;
2066 case COUNTER_MAP_SET:
2067 counters_map_set(newcounters, c->foot_index,
2068 &c->counters);
2069 break;
2070 }
2071 }
Harald Welte1cef74d2001-01-05 15:22:59 +00002072
Harald Welteaae69be2004-08-29 23:32:14 +00002073 list_for_each_entry(r, &c->rules, list) {
2074 DEBUGP("counter for index %u: ", r->index);
2075 switch (r->counter_map.maptype) {
2076 case COUNTER_MAP_NOMAP:
2077 counters_nomap(newcounters, r->index);
2078 break;
Harald Welte1cef74d2001-01-05 15:22:59 +00002079
Harald Welteaae69be2004-08-29 23:32:14 +00002080 case COUNTER_MAP_NORMAL_MAP:
2081 counters_normal_map(newcounters, repl,
2082 r->index,
2083 r->counter_map.mappos);
2084 break;
Harald Welte1cef74d2001-01-05 15:22:59 +00002085
Harald Welteaae69be2004-08-29 23:32:14 +00002086 case COUNTER_MAP_ZEROED:
2087 counters_map_zeroed(newcounters, repl,
2088 r->index,
2089 r->counter_map.mappos,
2090 &r->entry->counters);
2091 break;
2092
2093 case COUNTER_MAP_SET:
2094 counters_map_set(newcounters, r->index,
2095 &r->entry->counters);
2096 break;
2097 }
Marc Bouchere6869a82000-03-20 06:03:29 +00002098 }
2099 }
Rusty Russell62527ce2000-09-04 09:45:54 +00002100
Harald Welteaae69be2004-08-29 23:32:14 +00002101
Rusty Russell62527ce2000-09-04 09:45:54 +00002102#ifdef KERNEL_64_USERSPACE_32
2103 {
2104 /* Kernel will think that pointer should be 64-bits, and get
2105 padding. So we accomodate here (assumption: alignment of
2106 `counters' is on 64-bit boundary). */
2107 u_int64_t *kernptr = (u_int64_t *)&newcounters->counters;
2108 if ((unsigned long)&newcounters->counters % 8 != 0) {
2109 fprintf(stderr,
2110 "counters alignment incorrect! Mail rusty!\n");
2111 abort();
2112 }
2113 *kernptr = newcounters->counters;
Rusty Russell54c307e2000-09-04 06:47:28 +00002114 }
Rusty Russell62527ce2000-09-04 09:45:54 +00002115#endif /* KERNEL_64_USERSPACE_32 */
Marc Bouchere6869a82000-03-20 06:03:29 +00002116
Harald Welteaae69be2004-08-29 23:32:14 +00002117#ifdef IPTC_DEBUG2
2118 {
2119 int fd = open("/tmp/libiptc-so_set_add_counters.blob",
2120 O_CREAT|O_WRONLY);
2121 if (fd >= 0) {
2122 write(fd, newcounters, counterlen);
2123 close(fd);
2124 }
2125 }
2126#endif
2127
Rusty Russell79dee072000-05-02 16:45:16 +00002128 if (setsockopt(sockfd, TC_IPPROTO, SO_SET_ADD_COUNTERS,
2129 newcounters, counterlen) < 0) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002130 free(repl->counters);
2131 free(repl);
2132 free(newcounters);
2133 return 0;
2134 }
2135
2136 free(repl->counters);
2137 free(repl);
2138 free(newcounters);
2139
2140 finished:
Martin Josefsson841e4ae2003-05-02 15:30:11 +00002141 TC_FREE(handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00002142 return 1;
2143}
2144
2145/* Get raw socket. */
2146int
Rusty Russell79dee072000-05-02 16:45:16 +00002147TC_GET_RAW_SOCKET()
Marc Bouchere6869a82000-03-20 06:03:29 +00002148{
2149 return sockfd;
2150}
2151
2152/* Translates errno numbers into more human-readable form than strerror. */
2153const char *
Rusty Russell79dee072000-05-02 16:45:16 +00002154TC_STRERROR(int err)
Marc Bouchere6869a82000-03-20 06:03:29 +00002155{
2156 unsigned int i;
2157 struct table_struct {
2158 void *fn;
2159 int err;
2160 const char *message;
2161 } table [] =
Harald Welte4ccfa632001-07-30 15:12:43 +00002162 { { TC_INIT, EPERM, "Permission denied (you must be root)" },
Rusty Russell79dee072000-05-02 16:45:16 +00002163 { TC_INIT, EINVAL, "Module is wrong version" },
Harald Welte4ccfa632001-07-30 15:12:43 +00002164 { TC_INIT, ENOENT,
2165 "Table does not exist (do you need to insmod?)" },
Rusty Russell79dee072000-05-02 16:45:16 +00002166 { TC_DELETE_CHAIN, ENOTEMPTY, "Chain is not empty" },
2167 { TC_DELETE_CHAIN, EINVAL, "Can't delete built-in chain" },
2168 { TC_DELETE_CHAIN, EMLINK,
Marc Bouchere6869a82000-03-20 06:03:29 +00002169 "Can't delete chain with references left" },
Rusty Russell79dee072000-05-02 16:45:16 +00002170 { TC_CREATE_CHAIN, EEXIST, "Chain already exists" },
2171 { TC_INSERT_ENTRY, E2BIG, "Index of insertion too big" },
2172 { TC_REPLACE_ENTRY, E2BIG, "Index of replacement too big" },
2173 { TC_DELETE_NUM_ENTRY, E2BIG, "Index of deletion too big" },
Harald Welte1cef74d2001-01-05 15:22:59 +00002174 { TC_READ_COUNTER, E2BIG, "Index of counter too big" },
2175 { TC_ZERO_COUNTER, E2BIG, "Index of counter too big" },
Rusty Russell79dee072000-05-02 16:45:16 +00002176 { TC_INSERT_ENTRY, ELOOP, "Loop found in table" },
2177 { TC_INSERT_ENTRY, EINVAL, "Target problem" },
Marc Bouchere6869a82000-03-20 06:03:29 +00002178 /* EINVAL for CHECK probably means bad interface. */
Rusty Russell79dee072000-05-02 16:45:16 +00002179 { TC_CHECK_PACKET, EINVAL,
Marc Boucherc8264992000-04-22 22:34:44 +00002180 "Bad arguments (does that interface exist?)" },
Harald Welte4ccfa632001-07-30 15:12:43 +00002181 { TC_CHECK_PACKET, ENOSYS,
2182 "Checking will most likely never get implemented" },
Marc Bouchere6869a82000-03-20 06:03:29 +00002183 /* ENOENT for DELETE probably means no matching rule */
Rusty Russell79dee072000-05-02 16:45:16 +00002184 { TC_DELETE_ENTRY, ENOENT,
Marc Boucherc8264992000-04-22 22:34:44 +00002185 "Bad rule (does a matching rule exist in that chain?)" },
Rusty Russell79dee072000-05-02 16:45:16 +00002186 { TC_SET_POLICY, ENOENT,
Marc Boucherc8264992000-04-22 22:34:44 +00002187 "Bad built-in chain name" },
Rusty Russell79dee072000-05-02 16:45:16 +00002188 { TC_SET_POLICY, EINVAL,
Marc Boucherc8264992000-04-22 22:34:44 +00002189 "Bad policy name" },
Harald Welte4ccfa632001-07-30 15:12:43 +00002190
2191 { NULL, 0, "Incompatible with this kernel" },
2192 { NULL, ENOPROTOOPT, "iptables who? (do you need to insmod?)" },
2193 { NULL, ENOSYS, "Will be implemented real soon. I promise ;)" },
2194 { NULL, ENOMEM, "Memory allocation problem" },
2195 { NULL, ENOENT, "No chain/target/match by that name" },
Marc Bouchere6869a82000-03-20 06:03:29 +00002196 };
2197
2198 for (i = 0; i < sizeof(table)/sizeof(struct table_struct); i++) {
2199 if ((!table[i].fn || table[i].fn == iptc_fn)
2200 && table[i].err == err)
2201 return table[i].message;
2202 }
2203
2204 return strerror(err);
2205}