blob: 7c711b195370b8bd04e559113282bea6587a5851 [file] [log] [blame]
Martin Josefsson2a5dbbb2004-09-22 21:37:41 +00001/* Library which manipulates firewall rules. Version $Revision: 1.54 $ */
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
Harald Welte0113fe72004-01-06 19:04:02 +000047#ifndef __OPTIMIZE__
48STRUCT_ENTRY_TARGET *
49GET_TARGET(STRUCT_ENTRY *e)
50{
51 return (void *)e + e->target_offset;
52}
53#endif
54
Marc Bouchere6869a82000-03-20 06:03:29 +000055static int sockfd = -1;
56static void *iptc_fn = NULL;
57
58static const char *hooknames[]
Rusty Russell79dee072000-05-02 16:45:16 +000059= { [HOOK_PRE_ROUTING] "PREROUTING",
60 [HOOK_LOCAL_IN] "INPUT",
61 [HOOK_FORWARD] "FORWARD",
62 [HOOK_LOCAL_OUT] "OUTPUT",
Rusty Russell10758b72000-09-14 07:37:33 +000063 [HOOK_POST_ROUTING] "POSTROUTING",
64#ifdef HOOK_DROPPING
65 [HOOK_DROPPING] "DROPPING"
66#endif
Marc Bouchere6869a82000-03-20 06:03:29 +000067};
68
Harald Welteaae69be2004-08-29 23:32:14 +000069/* Convenience structures */
70struct ipt_error_target
71{
72 STRUCT_ENTRY_TARGET t;
73 char error[TABLE_MAXNAMELEN];
74};
75
76struct chain_head;
77struct rule_head;
78
Marc Bouchere6869a82000-03-20 06:03:29 +000079struct counter_map
80{
81 enum {
82 COUNTER_MAP_NOMAP,
83 COUNTER_MAP_NORMAL_MAP,
Harald Welte1cef74d2001-01-05 15:22:59 +000084 COUNTER_MAP_ZEROED,
85 COUNTER_MAP_SET
Marc Bouchere6869a82000-03-20 06:03:29 +000086 } maptype;
87 unsigned int mappos;
88};
89
Harald Welteaae69be2004-08-29 23:32:14 +000090enum iptcc_rule_type {
91 IPTCC_R_STANDARD, /* standard target (ACCEPT, ...) */
92 IPTCC_R_MODULE, /* extension module (SNAT, ...) */
93 IPTCC_R_FALLTHROUGH, /* fallthrough rule */
94 IPTCC_R_JUMP, /* jump to other chain */
Marc Bouchere6869a82000-03-20 06:03:29 +000095};
96
Harald Welteaae69be2004-08-29 23:32:14 +000097struct rule_head
Rusty Russell30fd6e52000-04-23 09:16:06 +000098{
Harald Welteaae69be2004-08-29 23:32:14 +000099 struct list_head list;
100 struct chain_head *chain;
101 struct counter_map counter_map;
102
103 unsigned int index; /* index (needed for counter_map) */
104 unsigned int offset; /* offset in rule blob */
105
106 enum iptcc_rule_type type;
107 struct chain_head *jump; /* jump target, if IPTCC_R_JUMP */
108
109 unsigned int size; /* size of entry data */
110 STRUCT_ENTRY entry[0];
111};
112
113struct chain_head
114{
115 struct list_head list;
Rusty Russell79dee072000-05-02 16:45:16 +0000116 char name[TABLE_MAXNAMELEN];
Harald Welteaae69be2004-08-29 23:32:14 +0000117 unsigned int hooknum; /* hook number+1 if builtin */
118 unsigned int references; /* how many jumps reference us */
119 int verdict; /* verdict if builtin */
120
121 STRUCT_COUNTERS counters; /* per-chain counters */
122 struct counter_map counter_map;
123
124 unsigned int num_rules; /* number of rules in list */
125 struct list_head rules; /* list of rules */
126
127 unsigned int index; /* index (needed for jump resolval) */
128 unsigned int head_offset; /* offset in rule blob */
129 unsigned int foot_index; /* index (needed for counter_map) */
130 unsigned int foot_offset; /* offset in rule blob */
Rusty Russell30fd6e52000-04-23 09:16:06 +0000131};
132
Rusty Russell79dee072000-05-02 16:45:16 +0000133STRUCT_TC_HANDLE
Marc Bouchere6869a82000-03-20 06:03:29 +0000134{
Harald Welteaae69be2004-08-29 23:32:14 +0000135 int changed; /* Have changes been made? */
136
137 struct list_head chains;
138
139 struct chain_head *chain_iterator_cur;
140 struct rule_head *rule_iterator_cur;
141
Rusty Russell79dee072000-05-02 16:45:16 +0000142 STRUCT_GETINFO info;
Harald Welteaae69be2004-08-29 23:32:14 +0000143 STRUCT_GET_ENTRIES *entries;
Marc Bouchere6869a82000-03-20 06:03:29 +0000144};
145
Harald Welteaae69be2004-08-29 23:32:14 +0000146/* allocate a new chain head for the cache */
147static struct chain_head *iptcc_alloc_chain_head(const char *name, int hooknum)
148{
149 struct chain_head *c = malloc(sizeof(*c));
150 if (!c)
151 return NULL;
152 memset(c, 0, sizeof(*c));
153
154 strncpy(c->name, name, TABLE_MAXNAMELEN);
155 c->hooknum = hooknum;
156 INIT_LIST_HEAD(&c->rules);
157
158 return c;
159}
160
161/* allocate and initialize a new rule for the cache */
162static struct rule_head *iptcc_alloc_rule(struct chain_head *c, unsigned int size)
163{
164 struct rule_head *r = malloc(sizeof(*r)+size);
165 if (!r)
166 return NULL;
167 memset(r, 0, sizeof(*r));
168
169 r->chain = c;
170 r->size = size;
171
172 return r;
173}
174
175/* notify us that the ruleset has been modified by the user */
Rusty Russell175f6412000-03-24 09:32:20 +0000176static void
Rusty Russell79dee072000-05-02 16:45:16 +0000177set_changed(TC_HANDLE_T h)
Rusty Russell175f6412000-03-24 09:32:20 +0000178{
Rusty Russell175f6412000-03-24 09:32:20 +0000179 h->changed = 1;
180}
181
Harald Welte380ba5f2002-02-13 16:19:55 +0000182#ifdef IPTC_DEBUG
Rusty Russell79dee072000-05-02 16:45:16 +0000183static void do_check(TC_HANDLE_T h, unsigned int line);
Rusty Russell849779c2000-04-23 15:51:51 +0000184#define CHECK(h) do { if (!getenv("IPTC_NO_CHECK")) do_check((h), __LINE__); } while(0)
Rusty Russell30fd6e52000-04-23 09:16:06 +0000185#else
186#define CHECK(h)
187#endif
Marc Bouchere6869a82000-03-20 06:03:29 +0000188
Harald Welteaae69be2004-08-29 23:32:14 +0000189
190/**********************************************************************
191 * iptc blob utility functions (iptcb_*)
192 **********************************************************************/
193
Marc Bouchere6869a82000-03-20 06:03:29 +0000194static inline int
Harald Welteaae69be2004-08-29 23:32:14 +0000195iptcb_get_number(const STRUCT_ENTRY *i,
Rusty Russell79dee072000-05-02 16:45:16 +0000196 const STRUCT_ENTRY *seek,
Marc Bouchere6869a82000-03-20 06:03:29 +0000197 unsigned int *pos)
198{
199 if (i == seek)
200 return 1;
201 (*pos)++;
202 return 0;
203}
204
Marc Bouchere6869a82000-03-20 06:03:29 +0000205static inline int
Harald Welteaae69be2004-08-29 23:32:14 +0000206iptcb_get_entry_n(STRUCT_ENTRY *i,
Marc Bouchere6869a82000-03-20 06:03:29 +0000207 unsigned int number,
208 unsigned int *pos,
Rusty Russell79dee072000-05-02 16:45:16 +0000209 STRUCT_ENTRY **pe)
Marc Bouchere6869a82000-03-20 06:03:29 +0000210{
211 if (*pos == number) {
212 *pe = i;
213 return 1;
214 }
215 (*pos)++;
216 return 0;
217}
218
Harald Welteaae69be2004-08-29 23:32:14 +0000219static inline STRUCT_ENTRY *
220iptcb_get_entry(TC_HANDLE_T h, unsigned int offset)
221{
222 return (STRUCT_ENTRY *)((char *)h->entries->entrytable + offset);
223}
224
225static unsigned int
226iptcb_entry2index(const TC_HANDLE_T h, const STRUCT_ENTRY *seek)
Marc Bouchere6869a82000-03-20 06:03:29 +0000227{
228 unsigned int pos = 0;
Marc Bouchere6869a82000-03-20 06:03:29 +0000229
Harald Welteaae69be2004-08-29 23:32:14 +0000230 if (ENTRY_ITERATE(h->entries->entrytable, h->entries->size,
231 iptcb_get_number, seek, &pos) == 0) {
232 fprintf(stderr, "ERROR: offset %u not an entry!\n",
233 (unsigned int)((char *)seek - (char *)h->entries->entrytable));
234 abort();
235 }
236 return pos;
Marc Bouchere6869a82000-03-20 06:03:29 +0000237}
238
Harald Welte0113fe72004-01-06 19:04:02 +0000239static inline STRUCT_ENTRY *
Harald Welteaae69be2004-08-29 23:32:14 +0000240iptcb_offset2entry(TC_HANDLE_T h, unsigned int offset)
Harald Welte0113fe72004-01-06 19:04:02 +0000241{
Harald Welteaae69be2004-08-29 23:32:14 +0000242 return (STRUCT_ENTRY *) ((void *)h->entries->entrytable+offset);
Harald Welte0113fe72004-01-06 19:04:02 +0000243}
244
Harald Welteaae69be2004-08-29 23:32:14 +0000245
Harald Welte0113fe72004-01-06 19:04:02 +0000246static inline unsigned long
Harald Welteaae69be2004-08-29 23:32:14 +0000247iptcb_entry2offset(const TC_HANDLE_T h, const STRUCT_ENTRY *e)
Harald Welte0113fe72004-01-06 19:04:02 +0000248{
Harald Welteaae69be2004-08-29 23:32:14 +0000249 return (void *)e - (void *)h->entries->entrytable;
Harald Welte3ea8f402003-06-23 18:25:59 +0000250}
251
252static inline unsigned int
Harald Welteaae69be2004-08-29 23:32:14 +0000253iptcb_offset2index(const TC_HANDLE_T h, unsigned int offset)
Harald Welte3ea8f402003-06-23 18:25:59 +0000254{
Harald Welteaae69be2004-08-29 23:32:14 +0000255 return iptcb_entry2index(h, iptcb_offset2entry(h, offset));
256}
257
258/* Returns 0 if not hook entry, else hooknumber + 1 */
259static inline unsigned int
260iptcb_ent_is_hook_entry(STRUCT_ENTRY *e, TC_HANDLE_T h)
261{
262 unsigned int i;
263
264 for (i = 0; i < NUMHOOKS; i++) {
265 if ((h->info.valid_hooks & (1 << i))
266 && iptcb_get_entry(h, h->info.hook_entry[i]) == e)
267 return i+1;
268 }
269 return 0;
Harald Welte3ea8f402003-06-23 18:25:59 +0000270}
271
272
Harald Welteaae69be2004-08-29 23:32:14 +0000273/**********************************************************************
274 * iptc cache utility functions (iptcc_*)
275 **********************************************************************/
Harald Welte0113fe72004-01-06 19:04:02 +0000276
Harald Welteaae69be2004-08-29 23:32:14 +0000277/* Is the given chain builtin (1) or user-defined (0) */
278static unsigned int iptcc_is_builtin(struct chain_head *c)
279{
280 return (c->hooknum ? 1 : 0);
281}
282
283/* Get a specific rule within a chain */
284static struct rule_head *iptcc_get_rule_num(struct chain_head *c,
285 unsigned int rulenum)
286{
287 struct rule_head *r;
288 unsigned int num = 0;
289
290 list_for_each_entry(r, &c->rules, list) {
291 num++;
292 if (num == rulenum)
293 return r;
294 }
295 return NULL;
296}
297
298/* Returns chain head if found, otherwise NULL. */
299static struct chain_head *
300iptcc_find_chain_by_offset(TC_HANDLE_T handle, unsigned int offset)
301{
302 struct list_head *pos;
303
304 if (list_empty(&handle->chains))
305 return NULL;
306
307 list_for_each(pos, &handle->chains) {
308 struct chain_head *c = list_entry(pos, struct chain_head, list);
309 if (offset >= c->head_offset && offset <= c->foot_offset)
310 return c;
Harald Welte0113fe72004-01-06 19:04:02 +0000311 }
312
Harald Welteaae69be2004-08-29 23:32:14 +0000313 return NULL;
Harald Welte0113fe72004-01-06 19:04:02 +0000314}
Harald Welteaae69be2004-08-29 23:32:14 +0000315/* Returns chain head if found, otherwise NULL. */
316static struct chain_head *
317iptcc_find_label(const char *name, TC_HANDLE_T handle)
318{
319 struct list_head *pos;
320
321 if (list_empty(&handle->chains))
322 return NULL;
323
324 list_for_each(pos, &handle->chains) {
325 struct chain_head *c = list_entry(pos, struct chain_head, list);
326 if (!strcmp(c->name, name))
327 return c;
328 }
329
330 return NULL;
331}
332
333/* called when rule is to be removed from cache */
334static void iptcc_delete_rule(struct rule_head *r)
335{
336 DEBUGP("deleting rule %p (offset %u)\n", r, r->offset);
337 /* clean up reference count of called chain */
338 if (r->type == IPTCC_R_JUMP
339 && r->jump)
340 r->jump->references--;
341
342 list_del(&r->list);
343 free(r);
344}
345
346
347/**********************************************************************
348 * RULESET PARSER (blob -> cache)
349 **********************************************************************/
350
351static int alphasort(const void *a, const void *b)
352{
353 return strcmp(((struct chain_head *)a)->name,
354 ((struct chain_head *)b)->name);
355}
356
357/* Delete policy rule of previous chain, since cache doesn't contain
358 * chain policy rules.
359 * WARNING: This function has ugly design and relies on a lot of context, only
360 * to be called from specific places within the parser */
361static int __iptcc_p_del_policy(TC_HANDLE_T h, unsigned int num)
362{
363 if (h->chain_iterator_cur) {
364 /* policy rule is last rule */
365 struct rule_head *pr = (struct rule_head *)
366 h->chain_iterator_cur->rules.prev;
367
368 /* save verdict */
369 h->chain_iterator_cur->verdict =
370 *(int *)GET_TARGET(pr->entry)->data;
371
372 /* save counter and counter_map information */
373 h->chain_iterator_cur->counter_map.maptype =
374 COUNTER_MAP_NORMAL_MAP;
375 h->chain_iterator_cur->counter_map.mappos = num-1;
376 memcpy(&h->chain_iterator_cur->counters, &pr->entry->counters,
377 sizeof(h->chain_iterator_cur->counters));
378
379 /* foot_offset points to verdict rule */
380 h->chain_iterator_cur->foot_index = num;
381 h->chain_iterator_cur->foot_offset = pr->offset;
382
383 /* delete rule from cache */
384 iptcc_delete_rule(pr);
Martin Josefsson8d1b38a2004-09-22 21:00:19 +0000385 h->chain_iterator_cur->num_rules--;
Harald Welteaae69be2004-08-29 23:32:14 +0000386
387 return 1;
388 }
389 return 0;
390}
391
392/* Another ugly helper function split out of cache_add_entry to make it less
393 * spaghetti code */
394static void __iptcc_p_add_chain(TC_HANDLE_T h, struct chain_head *c,
395 unsigned int offset, unsigned int *num)
396{
397 __iptcc_p_del_policy(h, *num);
398
399 c->head_offset = offset;
400 c->index = *num;
401
402 list_add_tail(&c->list, &h->chains);
403 h->chain_iterator_cur = c;
404}
405
406/* main parser function: add an entry from the blob to the cache */
407static int cache_add_entry(STRUCT_ENTRY *e,
408 TC_HANDLE_T h,
409 STRUCT_ENTRY **prev,
410 unsigned int *num)
411{
412 unsigned int builtin;
413 unsigned int offset = (char *)e - (char *)h->entries->entrytable;
414
415 DEBUGP("entering...");
416
417 /* Last entry ("policy rule"). End it.*/
418 if (iptcb_entry2offset(h,e) + e->next_offset == h->entries->size) {
419 /* This is the ERROR node at the end of the chain */
420 DEBUGP_C("%u:%u: end of table:\n", *num, offset);
421
422 __iptcc_p_del_policy(h, *num);
423
424 h->chain_iterator_cur = NULL;
425 goto out_inc;
426 }
427
428 /* We know this is the start of a new chain if it's an ERROR
429 * target, or a hook entry point */
430
431 if (strcmp(GET_TARGET(e)->u.user.name, ERROR_TARGET) == 0) {
432 struct chain_head *c =
433 iptcc_alloc_chain_head((const char *)GET_TARGET(e)->data, 0);
434 DEBUGP_C("%u:%u:new userdefined chain %s: %p\n", *num, offset,
435 (char *)c->name, c);
436 if (!c) {
437 errno = -ENOMEM;
438 return -1;
439 }
440
441 __iptcc_p_add_chain(h, c, offset, num);
442
443 } else if ((builtin = iptcb_ent_is_hook_entry(e, h)) != 0) {
444 struct chain_head *c =
445 iptcc_alloc_chain_head((char *)hooknames[builtin-1],
446 builtin);
447 DEBUGP_C("%u:%u new builtin chain: %p (rules=%p)\n",
448 *num, offset, c, &c->rules);
449 if (!c) {
450 errno = -ENOMEM;
451 return -1;
452 }
453
454 c->hooknum = builtin;
455
456 __iptcc_p_add_chain(h, c, offset, num);
457
458 /* FIXME: this is ugly. */
459 goto new_rule;
460 } else {
461 /* has to be normal rule */
462 struct rule_head *r;
463new_rule:
464
465 if (!(r = iptcc_alloc_rule(h->chain_iterator_cur,
466 e->next_offset))) {
467 errno = ENOMEM;
468 return -1;
469 }
470 DEBUGP_C("%u:%u normal rule: %p: ", *num, offset, r);
471
472 r->index = *num;
473 r->offset = offset;
474 memcpy(r->entry, e, e->next_offset);
475 r->counter_map.maptype = COUNTER_MAP_NORMAL_MAP;
476 r->counter_map.mappos = r->index;
477
478 /* handling of jumps, etc. */
479 if (!strcmp(GET_TARGET(e)->u.user.name, STANDARD_TARGET)) {
480 STRUCT_STANDARD_TARGET *t;
481
482 t = (STRUCT_STANDARD_TARGET *)GET_TARGET(e);
483 if (t->target.u.target_size
484 != ALIGN(sizeof(STRUCT_STANDARD_TARGET))) {
485 errno = EINVAL;
486 return -1;
487 }
488
489 if (t->verdict < 0) {
490 DEBUGP_C("standard, verdict=%d\n", t->verdict);
491 r->type = IPTCC_R_STANDARD;
492 } else if (t->verdict == r->offset+e->next_offset) {
493 DEBUGP_C("fallthrough\n");
494 r->type = IPTCC_R_FALLTHROUGH;
495 } else {
496 DEBUGP_C("jump, target=%u\n", t->verdict);
497 r->type = IPTCC_R_JUMP;
498 /* Jump target fixup has to be deferred
499 * until second pass, since we migh not
500 * yet have parsed the target */
501 }
Martin Josefsson52c38022004-09-22 19:39:40 +0000502 } else {
503 DEBUGP_C("module, target=%s\n", GET_TARGET(e)->u.user.name);
504 r->type = IPTCC_R_MODULE;
Harald Welteaae69be2004-08-29 23:32:14 +0000505 }
506
507 list_add_tail(&r->list, &h->chain_iterator_cur->rules);
Martin Josefsson8d1b38a2004-09-22 21:00:19 +0000508 h->chain_iterator_cur->num_rules++;
Harald Welteaae69be2004-08-29 23:32:14 +0000509 }
510out_inc:
511 (*num)++;
512 return 0;
513}
514
515
516/* parse an iptables blob into it's pieces */
517static int parse_table(TC_HANDLE_T h)
518{
519 STRUCT_ENTRY *prev;
520 unsigned int num = 0;
521 struct chain_head *c;
522
523 /* First pass: over ruleset blob */
524 ENTRY_ITERATE(h->entries->entrytable, h->entries->size,
525 cache_add_entry, h, &prev, &num);
526
527 /* Second pass: fixup parsed data from first pass */
528 list_for_each_entry(c, &h->chains, list) {
529 struct rule_head *r;
530 list_for_each_entry(r, &c->rules, list) {
531 struct chain_head *c;
532 STRUCT_STANDARD_TARGET *t;
533
534 if (r->type != IPTCC_R_JUMP)
535 continue;
536
537 t = (STRUCT_STANDARD_TARGET *)GET_TARGET(r->entry);
538 c = iptcc_find_chain_by_offset(h, t->verdict);
539 if (!c)
540 return -1;
541 r->jump = c;
542 c->references++;
543 }
544 }
545
546 /* FIXME: sort chains */
547
548 return 1;
549}
550
551
552/**********************************************************************
553 * RULESET COMPILATION (cache -> blob)
554 **********************************************************************/
555
556/* Convenience structures */
557struct iptcb_chain_start{
558 STRUCT_ENTRY e;
559 struct ipt_error_target name;
560};
561#define IPTCB_CHAIN_START_SIZE (sizeof(STRUCT_ENTRY) + \
562 ALIGN(sizeof(struct ipt_error_target)))
563
564struct iptcb_chain_foot {
565 STRUCT_ENTRY e;
566 STRUCT_STANDARD_TARGET target;
567};
568#define IPTCB_CHAIN_FOOT_SIZE (sizeof(STRUCT_ENTRY) + \
569 ALIGN(sizeof(STRUCT_STANDARD_TARGET)))
570
571struct iptcb_chain_error {
572 STRUCT_ENTRY entry;
573 struct ipt_error_target target;
574};
575#define IPTCB_CHAIN_ERROR_SIZE (sizeof(STRUCT_ENTRY) + \
576 ALIGN(sizeof(struct ipt_error_target)))
577
578
579
580/* compile rule from cache into blob */
581static inline int iptcc_compile_rule (TC_HANDLE_T h, STRUCT_REPLACE *repl, struct rule_head *r)
582{
583 /* handle jumps */
584 if (r->type == IPTCC_R_JUMP) {
585 STRUCT_STANDARD_TARGET *t;
586 t = (STRUCT_STANDARD_TARGET *)GET_TARGET(r->entry);
587 /* memset for memcmp convenience on delete/replace */
588 memset(t->target.u.user.name, 0, FUNCTION_MAXNAMELEN);
589 strcpy(t->target.u.user.name, STANDARD_TARGET);
590 /* Jumps can only happen to builtin chains, so we
591 * can safely assume that they always have a header */
592 t->verdict = r->jump->head_offset + IPTCB_CHAIN_START_SIZE;
593 } else if (r->type == IPTCC_R_FALLTHROUGH) {
594 STRUCT_STANDARD_TARGET *t;
595 t = (STRUCT_STANDARD_TARGET *)GET_TARGET(r->entry);
596 t->verdict = r->offset + r->size;
597 }
598
599 /* copy entry from cache to blob */
600 memcpy((char *)repl->entries+r->offset, r->entry, r->size);
601
602 return 1;
603}
604
605/* compile chain from cache into blob */
606static int iptcc_compile_chain(TC_HANDLE_T h, STRUCT_REPLACE *repl, struct chain_head *c)
607{
608 int ret;
609 struct rule_head *r;
610 struct iptcb_chain_start *head;
611 struct iptcb_chain_foot *foot;
612
613 /* only user-defined chains have heaer */
614 if (!iptcc_is_builtin(c)) {
615 /* put chain header in place */
616 head = (void *)repl->entries + c->head_offset;
617 head->e.target_offset = sizeof(STRUCT_ENTRY);
618 head->e.next_offset = IPTCB_CHAIN_START_SIZE;
619 strcpy(head->name.t.u.user.name, ERROR_TARGET);
620 head->name.t.u.target_size =
621 ALIGN(sizeof(struct ipt_error_target));
622 strcpy(head->name.error, c->name);
623 } else {
624 repl->hook_entry[c->hooknum-1] = c->head_offset;
625 repl->underflow[c->hooknum-1] = c->foot_offset;
626 }
627
628 /* iterate over rules */
629 list_for_each_entry(r, &c->rules, list) {
630 ret = iptcc_compile_rule(h, repl, r);
631 if (ret < 0)
632 return ret;
633 }
634
635 /* put chain footer in place */
636 foot = (void *)repl->entries + c->foot_offset;
637 foot->e.target_offset = sizeof(STRUCT_ENTRY);
638 foot->e.next_offset = IPTCB_CHAIN_FOOT_SIZE;
639 strcpy(foot->target.target.u.user.name, STANDARD_TARGET);
640 foot->target.target.u.target_size =
641 ALIGN(sizeof(STRUCT_STANDARD_TARGET));
642 /* builtin targets have verdict, others return */
643 if (iptcc_is_builtin(c))
644 foot->target.verdict = c->verdict;
645 else
646 foot->target.verdict = RETURN;
647 /* set policy-counters */
648 memcpy(&foot->e.counters, &c->counters, sizeof(STRUCT_COUNTERS));
649
650 return 0;
651}
652
653/* calculate offset and number for every rule in the cache */
654static int iptcc_compile_chain_offsets(TC_HANDLE_T h, struct chain_head *c,
655 int *offset, int *num)
656{
657 struct rule_head *r;
658
659 c->head_offset = *offset;
660 DEBUGP("%s: chain_head %u, offset=%u\n", c->name, *num, *offset);
661
662 if (!iptcc_is_builtin(c)) {
663 /* Chain has header */
664 *offset += sizeof(STRUCT_ENTRY)
665 + ALIGN(sizeof(struct ipt_error_target));
666 (*num)++;
667 }
668
669 list_for_each_entry(r, &c->rules, list) {
670 DEBUGP("rule %u, offset=%u, index=%u\n", *num, *offset, *num);
671 r->offset = *offset;
672 r->index = *num;
673 *offset += r->size;
674 (*num)++;
675 }
676
677 DEBUGP("%s; chain_foot %u, offset=%u, index=%u\n", c->name, *num,
678 *offset, *num);
679 c->foot_offset = *offset;
680 c->foot_index = *num;
681 *offset += sizeof(STRUCT_ENTRY)
682 + ALIGN(sizeof(STRUCT_STANDARD_TARGET));
683 (*num)++;
684
685 return 1;
686}
687
688/* put the pieces back together again */
689static int iptcc_compile_table_prep(TC_HANDLE_T h, unsigned int *size)
690{
691 struct chain_head *c;
692 unsigned int offset = 0, num = 0;
693 int ret = 0;
694
695 /* First pass: calculate offset for every rule */
696 list_for_each_entry(c, &h->chains, list) {
697 ret = iptcc_compile_chain_offsets(h, c, &offset, &num);
698 if (ret < 0)
699 return ret;
700 }
701
702 /* Append one error rule at end of chain */
703 num++;
704 offset += sizeof(STRUCT_ENTRY)
705 + ALIGN(sizeof(struct ipt_error_target));
706
707 /* ruleset size is now in offset */
708 *size = offset;
709 return num;
710}
711
712static int iptcc_compile_table(TC_HANDLE_T h, STRUCT_REPLACE *repl)
713{
714 struct chain_head *c;
715 struct iptcb_chain_error *error;
716
717 /* Second pass: copy from cache to offsets, fill in jumps */
718 list_for_each_entry(c, &h->chains, list) {
719 int ret = iptcc_compile_chain(h, repl, c);
720 if (ret < 0)
721 return ret;
722 }
723
724 /* Append error rule at end of chain */
725 error = (void *)repl->entries + repl->size - IPTCB_CHAIN_ERROR_SIZE;
726 error->entry.target_offset = sizeof(STRUCT_ENTRY);
727 error->entry.next_offset = IPTCB_CHAIN_ERROR_SIZE;
728 error->target.t.u.user.target_size =
729 ALIGN(sizeof(struct ipt_error_target));
730 strcpy((char *)&error->target.t.u.user.name, ERROR_TARGET);
731 strcpy((char *)&error->target.error, "ERROR");
732
733 return 1;
734}
735
736/**********************************************************************
737 * EXTERNAL API (operates on cache only)
738 **********************************************************************/
Marc Bouchere6869a82000-03-20 06:03:29 +0000739
740/* Allocate handle of given size */
Rusty Russell79dee072000-05-02 16:45:16 +0000741static TC_HANDLE_T
Harald Welte0113fe72004-01-06 19:04:02 +0000742alloc_handle(const char *tablename, unsigned int size, unsigned int num_rules)
Marc Bouchere6869a82000-03-20 06:03:29 +0000743{
744 size_t len;
Rusty Russell79dee072000-05-02 16:45:16 +0000745 TC_HANDLE_T h;
Marc Bouchere6869a82000-03-20 06:03:29 +0000746
Harald Welteaae69be2004-08-29 23:32:14 +0000747 len = sizeof(STRUCT_TC_HANDLE) + size;
Marc Bouchere6869a82000-03-20 06:03:29 +0000748
Harald Welteaae69be2004-08-29 23:32:14 +0000749 h = malloc(sizeof(STRUCT_TC_HANDLE));
750 if (!h) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000751 errno = ENOMEM;
752 return NULL;
753 }
Harald Welteaae69be2004-08-29 23:32:14 +0000754 memset(h, 0, sizeof(*h));
755 INIT_LIST_HEAD(&h->chains);
Marc Bouchere6869a82000-03-20 06:03:29 +0000756 strcpy(h->info.name, tablename);
Harald Welteaae69be2004-08-29 23:32:14 +0000757
Harald Welte0371c0c2004-09-19 21:00:12 +0000758 h->entries = malloc(sizeof(STRUCT_GET_ENTRIES) + size);
Harald Welteaae69be2004-08-29 23:32:14 +0000759 if (!h->entries)
760 goto out_free_handle;
761
762 strcpy(h->entries->name, tablename);
Harald Welte0371c0c2004-09-19 21:00:12 +0000763 h->entries->size = size;
Marc Bouchere6869a82000-03-20 06:03:29 +0000764
765 return h;
Harald Welteaae69be2004-08-29 23:32:14 +0000766
767out_free_handle:
768 free(h);
769
770 return NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +0000771}
772
Harald Welteaae69be2004-08-29 23:32:14 +0000773
Rusty Russell79dee072000-05-02 16:45:16 +0000774TC_HANDLE_T
775TC_INIT(const char *tablename)
Marc Bouchere6869a82000-03-20 06:03:29 +0000776{
Rusty Russell79dee072000-05-02 16:45:16 +0000777 TC_HANDLE_T h;
778 STRUCT_GETINFO info;
Marc Bouchere6869a82000-03-20 06:03:29 +0000779 int tmp;
780 socklen_t s;
781
Rusty Russell79dee072000-05-02 16:45:16 +0000782 iptc_fn = TC_INIT;
Marc Bouchere6869a82000-03-20 06:03:29 +0000783
Martin Josefssone560fd62003-06-13 16:56:51 +0000784 if (sockfd != -1) {
Harald Welte366454b2002-01-07 13:46:50 +0000785 close(sockfd);
Martin Josefssone560fd62003-06-13 16:56:51 +0000786 sockfd = -1;
787 }
Harald Welte366454b2002-01-07 13:46:50 +0000788
Martin Josefsson841e4ae2003-05-02 15:30:11 +0000789 if (strlen(tablename) >= TABLE_MAXNAMELEN) {
790 errno = EINVAL;
791 return NULL;
792 }
793
Rusty Russell79dee072000-05-02 16:45:16 +0000794 sockfd = socket(TC_AF, SOCK_RAW, IPPROTO_RAW);
Marc Bouchere6869a82000-03-20 06:03:29 +0000795 if (sockfd < 0)
796 return NULL;
797
798 s = sizeof(info);
Martin Josefsson841e4ae2003-05-02 15:30:11 +0000799
Marc Bouchere6869a82000-03-20 06:03:29 +0000800 strcpy(info.name, tablename);
Rusty Russell79dee072000-05-02 16:45:16 +0000801 if (getsockopt(sockfd, TC_IPPROTO, SO_GET_INFO, &info, &s) < 0)
Marc Bouchere6869a82000-03-20 06:03:29 +0000802 return NULL;
803
Harald Welteaae69be2004-08-29 23:32:14 +0000804 DEBUGP("valid_hooks=0x%08x, num_entries=%u, size=%u\n",
805 info.valid_hooks, info.num_entries, info.size);
806
Harald Welte0113fe72004-01-06 19:04:02 +0000807 if ((h = alloc_handle(info.name, info.size, info.num_entries))
Martin Josefsson841e4ae2003-05-02 15:30:11 +0000808 == NULL) {
809 close(sockfd);
Martin Josefssone560fd62003-06-13 16:56:51 +0000810 sockfd = -1;
Marc Bouchere6869a82000-03-20 06:03:29 +0000811 return NULL;
Martin Josefsson841e4ae2003-05-02 15:30:11 +0000812 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000813
Marc Bouchere6869a82000-03-20 06:03:29 +0000814 /* Initialize current state */
815 h->info = info;
Harald Welte0113fe72004-01-06 19:04:02 +0000816
Harald Welteaae69be2004-08-29 23:32:14 +0000817 h->entries->size = h->info.size;
Marc Bouchere6869a82000-03-20 06:03:29 +0000818
Rusty Russell79dee072000-05-02 16:45:16 +0000819 tmp = sizeof(STRUCT_GET_ENTRIES) + h->info.size;
Marc Bouchere6869a82000-03-20 06:03:29 +0000820
Harald Welteaae69be2004-08-29 23:32:14 +0000821 if (getsockopt(sockfd, TC_IPPROTO, SO_GET_ENTRIES, h->entries,
822 &tmp) < 0)
823 goto error;
824
825#ifdef IPTC_DEBUG2
826 {
827 int fd = open("/tmp/libiptc-so_get_entries.blob",
828 O_CREAT|O_WRONLY);
829 if (fd >= 0) {
830 write(fd, h->entries, tmp);
831 close(fd);
832 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000833 }
Harald Welteaae69be2004-08-29 23:32:14 +0000834#endif
835
836 if (parse_table(h) < 0)
837 goto error;
Rusty Russell7e53bf92000-03-20 07:03:28 +0000838
Marc Bouchere6869a82000-03-20 06:03:29 +0000839 CHECK(h);
840 return h;
Harald Welteaae69be2004-08-29 23:32:14 +0000841error:
842 TC_FREE(&h);
843 return NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +0000844}
845
Martin Josefsson841e4ae2003-05-02 15:30:11 +0000846void
847TC_FREE(TC_HANDLE_T *h)
848{
Harald Welteaae69be2004-08-29 23:32:14 +0000849 struct chain_head *c, *tmp;
850
Martin Josefsson841e4ae2003-05-02 15:30:11 +0000851 close(sockfd);
Martin Josefssone560fd62003-06-13 16:56:51 +0000852 sockfd = -1;
Harald Welteaae69be2004-08-29 23:32:14 +0000853
854 list_for_each_entry_safe(c, tmp, &(*h)->chains, list) {
855 struct rule_head *r, *rtmp;
856
857 list_for_each_entry_safe(r, rtmp, &c->rules, list) {
858 free(r);
859 }
860
861 free(c);
862 }
863
864 free((*h)->entries);
Martin Josefsson841e4ae2003-05-02 15:30:11 +0000865 free(*h);
Harald Welteaae69be2004-08-29 23:32:14 +0000866
Martin Josefsson841e4ae2003-05-02 15:30:11 +0000867 *h = NULL;
868}
869
Marc Bouchere6869a82000-03-20 06:03:29 +0000870static inline int
Rusty Russell79dee072000-05-02 16:45:16 +0000871print_match(const STRUCT_ENTRY_MATCH *m)
Marc Bouchere6869a82000-03-20 06:03:29 +0000872{
Rusty Russell228e98d2000-04-27 10:28:06 +0000873 printf("Match name: `%s'\n", m->u.user.name);
Marc Bouchere6869a82000-03-20 06:03:29 +0000874 return 0;
875}
876
Rusty Russell79dee072000-05-02 16:45:16 +0000877static int dump_entry(STRUCT_ENTRY *e, const TC_HANDLE_T handle);
878
Marc Bouchere6869a82000-03-20 06:03:29 +0000879void
Rusty Russell79dee072000-05-02 16:45:16 +0000880TC_DUMP_ENTRIES(const TC_HANDLE_T handle)
Marc Bouchere6869a82000-03-20 06:03:29 +0000881{
882 CHECK(handle);
Harald Welteaae69be2004-08-29 23:32:14 +0000883#if 0
Marc Bouchere6869a82000-03-20 06:03:29 +0000884 printf("libiptc v%s. %u entries, %u bytes.\n",
Harald Welte80fe35d2002-05-29 13:08:15 +0000885 IPTABLES_VERSION,
Harald Welteaae69be2004-08-29 23:32:14 +0000886 handle->new_number, handle->entries->size);
Marc Bouchere6869a82000-03-20 06:03:29 +0000887 printf("Table `%s'\n", handle->info.name);
888 printf("Hooks: pre/in/fwd/out/post = %u/%u/%u/%u/%u\n",
Rusty Russell67088e72000-05-10 01:18:57 +0000889 handle->info.hook_entry[HOOK_PRE_ROUTING],
890 handle->info.hook_entry[HOOK_LOCAL_IN],
891 handle->info.hook_entry[HOOK_FORWARD],
892 handle->info.hook_entry[HOOK_LOCAL_OUT],
893 handle->info.hook_entry[HOOK_POST_ROUTING]);
Marc Bouchere6869a82000-03-20 06:03:29 +0000894 printf("Underflows: pre/in/fwd/out/post = %u/%u/%u/%u/%u\n",
Rusty Russell67088e72000-05-10 01:18:57 +0000895 handle->info.underflow[HOOK_PRE_ROUTING],
896 handle->info.underflow[HOOK_LOCAL_IN],
897 handle->info.underflow[HOOK_FORWARD],
898 handle->info.underflow[HOOK_LOCAL_OUT],
899 handle->info.underflow[HOOK_POST_ROUTING]);
Marc Bouchere6869a82000-03-20 06:03:29 +0000900
Harald Welteaae69be2004-08-29 23:32:14 +0000901 ENTRY_ITERATE(handle->entries->entrytable, handle->entries->size,
Rusty Russell79dee072000-05-02 16:45:16 +0000902 dump_entry, handle);
Harald Welteaae69be2004-08-29 23:32:14 +0000903#endif
Harald Welte0113fe72004-01-06 19:04:02 +0000904}
Rusty Russell30fd6e52000-04-23 09:16:06 +0000905
Marc Bouchere6869a82000-03-20 06:03:29 +0000906/* Does this chain exist? */
Rusty Russell79dee072000-05-02 16:45:16 +0000907int TC_IS_CHAIN(const char *chain, const TC_HANDLE_T handle)
Marc Bouchere6869a82000-03-20 06:03:29 +0000908{
Harald Welteaae69be2004-08-29 23:32:14 +0000909 return iptcc_find_label(chain, handle) != NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +0000910}
911
Harald Welteaae69be2004-08-29 23:32:14 +0000912static void iptcc_chain_iterator_advance(TC_HANDLE_T handle)
913{
914 struct chain_head *c = handle->chain_iterator_cur;
915
916 if (c->list.next == &handle->chains)
917 handle->chain_iterator_cur = NULL;
918 else
919 handle->chain_iterator_cur =
920 list_entry(c->list.next, struct chain_head, list);
921}
Marc Bouchere6869a82000-03-20 06:03:29 +0000922
Rusty Russell30fd6e52000-04-23 09:16:06 +0000923/* Iterator functions to run through the chains. */
Marc Bouchere6869a82000-03-20 06:03:29 +0000924const char *
Philip Blundell8c700902000-05-15 02:17:52 +0000925TC_FIRST_CHAIN(TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +0000926{
Harald Welteaae69be2004-08-29 23:32:14 +0000927 struct chain_head *c = list_entry((*handle)->chains.next,
928 struct chain_head, list);
929
930 iptc_fn = TC_FIRST_CHAIN;
931
932
933 if (list_empty(&(*handle)->chains)) {
934 DEBUGP(": no chains\n");
Harald Welte0113fe72004-01-06 19:04:02 +0000935 return NULL;
Harald Welteaae69be2004-08-29 23:32:14 +0000936 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000937
Harald Welteaae69be2004-08-29 23:32:14 +0000938 (*handle)->chain_iterator_cur = c;
939 iptcc_chain_iterator_advance(*handle);
Harald Welte0113fe72004-01-06 19:04:02 +0000940
Harald Welteaae69be2004-08-29 23:32:14 +0000941 DEBUGP(": returning `%s'\n", c->name);
942 return c->name;
Marc Bouchere6869a82000-03-20 06:03:29 +0000943}
944
Rusty Russell30fd6e52000-04-23 09:16:06 +0000945/* Iterator functions to run through the chains. Returns NULL at end. */
946const char *
Rusty Russell79dee072000-05-02 16:45:16 +0000947TC_NEXT_CHAIN(TC_HANDLE_T *handle)
Rusty Russell30fd6e52000-04-23 09:16:06 +0000948{
Harald Welteaae69be2004-08-29 23:32:14 +0000949 struct chain_head *c = (*handle)->chain_iterator_cur;
Rusty Russell30fd6e52000-04-23 09:16:06 +0000950
Harald Welteaae69be2004-08-29 23:32:14 +0000951 iptc_fn = TC_NEXT_CHAIN;
952
953 if (!c) {
954 DEBUGP(": no more chains\n");
Rusty Russell30fd6e52000-04-23 09:16:06 +0000955 return NULL;
Harald Welteaae69be2004-08-29 23:32:14 +0000956 }
Rusty Russell30fd6e52000-04-23 09:16:06 +0000957
Harald Welteaae69be2004-08-29 23:32:14 +0000958 iptcc_chain_iterator_advance(*handle);
959
960 DEBUGP(": returning `%s'\n", c->name);
961 return c->name;
Rusty Russell30fd6e52000-04-23 09:16:06 +0000962}
963
964/* Get first rule in the given chain: NULL for empty chain. */
Rusty Russell79dee072000-05-02 16:45:16 +0000965const STRUCT_ENTRY *
Philip Blundell8c700902000-05-15 02:17:52 +0000966TC_FIRST_RULE(const char *chain, TC_HANDLE_T *handle)
Rusty Russell30fd6e52000-04-23 09:16:06 +0000967{
Harald Welteaae69be2004-08-29 23:32:14 +0000968 struct chain_head *c;
969 struct rule_head *r;
Rusty Russell30fd6e52000-04-23 09:16:06 +0000970
Harald Welteaae69be2004-08-29 23:32:14 +0000971 iptc_fn = TC_FIRST_RULE;
972
973 DEBUGP("first rule(%s): ", chain);
974
975 c = iptcc_find_label(chain, *handle);
Rusty Russell30fd6e52000-04-23 09:16:06 +0000976 if (!c) {
977 errno = ENOENT;
978 return NULL;
979 }
980
981 /* Empty chain: single return/policy rule */
Harald Welteaae69be2004-08-29 23:32:14 +0000982 if (list_empty(&c->rules)) {
983 DEBUGP_C("no rules, returning NULL\n");
Rusty Russell30fd6e52000-04-23 09:16:06 +0000984 return NULL;
Harald Welteaae69be2004-08-29 23:32:14 +0000985 }
Rusty Russell30fd6e52000-04-23 09:16:06 +0000986
Harald Welteaae69be2004-08-29 23:32:14 +0000987 r = list_entry(c->rules.next, struct rule_head, list);
988 (*handle)->rule_iterator_cur = r;
989 DEBUGP_C("%p\n", r);
990
991 return r->entry;
Rusty Russell30fd6e52000-04-23 09:16:06 +0000992}
993
994/* Returns NULL when rules run out. */
Rusty Russell79dee072000-05-02 16:45:16 +0000995const STRUCT_ENTRY *
Philip Blundell8c700902000-05-15 02:17:52 +0000996TC_NEXT_RULE(const STRUCT_ENTRY *prev, TC_HANDLE_T *handle)
Rusty Russell30fd6e52000-04-23 09:16:06 +0000997{
Harald Welteaae69be2004-08-29 23:32:14 +0000998 struct rule_head *r;
Rusty Russell30fd6e52000-04-23 09:16:06 +0000999
Harald Welteaae69be2004-08-29 23:32:14 +00001000 DEBUGP("rule_iterator_cur=%p...", (*handle)->rule_iterator_cur);
1001
1002 if (!(*handle)->rule_iterator_cur) {
1003 DEBUGP_C("returning NULL\n");
1004 return NULL;
1005 }
1006
1007 r = list_entry((*handle)->rule_iterator_cur->list.next,
1008 struct rule_head, list);
1009
1010 iptc_fn = TC_NEXT_RULE;
1011
1012 DEBUGP_C("next=%p, head=%p...", &r->list,
1013 &(*handle)->rule_iterator_cur->chain->rules);
1014
1015 if (&r->list == &(*handle)->rule_iterator_cur->chain->rules) {
1016 (*handle)->rule_iterator_cur = NULL;
1017 DEBUGP_C("finished, returning NULL\n");
1018 return NULL;
1019 }
1020
1021 (*handle)->rule_iterator_cur = r;
1022
1023 /* NOTE: prev is without any influence ! */
1024 DEBUGP_C("returning rule %p\n", r);
1025 return r->entry;
Rusty Russell30fd6e52000-04-23 09:16:06 +00001026}
1027
Marc Bouchere6869a82000-03-20 06:03:29 +00001028/* How many rules in this chain? */
1029unsigned int
Rusty Russell79dee072000-05-02 16:45:16 +00001030TC_NUM_RULES(const char *chain, TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001031{
Harald Welteaae69be2004-08-29 23:32:14 +00001032 struct chain_head *c;
1033 iptc_fn = TC_NUM_RULES;
Marc Bouchere6869a82000-03-20 06:03:29 +00001034 CHECK(*handle);
Harald Welteaae69be2004-08-29 23:32:14 +00001035
1036 c = iptcc_find_label(chain, *handle);
1037 if (!c) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001038 errno = ENOENT;
1039 return (unsigned int)-1;
1040 }
Harald Welteaae69be2004-08-29 23:32:14 +00001041
1042 return c->num_rules;
Marc Bouchere6869a82000-03-20 06:03:29 +00001043}
1044
Rusty Russell79dee072000-05-02 16:45:16 +00001045const STRUCT_ENTRY *TC_GET_RULE(const char *chain,
1046 unsigned int n,
1047 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001048{
Harald Welteaae69be2004-08-29 23:32:14 +00001049 struct chain_head *c;
1050 struct rule_head *r;
1051
1052 iptc_fn = TC_GET_RULE;
Marc Bouchere6869a82000-03-20 06:03:29 +00001053
1054 CHECK(*handle);
Harald Welteaae69be2004-08-29 23:32:14 +00001055
1056 c = iptcc_find_label(chain, *handle);
1057 if (!c) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001058 errno = ENOENT;
1059 return NULL;
1060 }
1061
Harald Welteaae69be2004-08-29 23:32:14 +00001062 r = iptcc_get_rule_num(c, n);
1063 if (!r)
1064 return NULL;
1065 return r->entry;
Marc Bouchere6869a82000-03-20 06:03:29 +00001066}
1067
1068/* Returns a pointer to the target name of this position. */
Harald Welteaae69be2004-08-29 23:32:14 +00001069const char *standard_target_map(int verdict)
Marc Bouchere6869a82000-03-20 06:03:29 +00001070{
Harald Welteaae69be2004-08-29 23:32:14 +00001071 switch (verdict) {
1072 case RETURN:
1073 return LABEL_RETURN;
1074 break;
1075 case -NF_ACCEPT-1:
1076 return LABEL_ACCEPT;
1077 break;
1078 case -NF_DROP-1:
1079 return LABEL_DROP;
1080 break;
1081 case -NF_QUEUE-1:
1082 return LABEL_QUEUE;
1083 break;
1084 default:
1085 fprintf(stderr, "ERROR: %d not a valid target)\n",
1086 verdict);
1087 abort();
1088 break;
1089 }
1090 /* not reached */
1091 return NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +00001092}
1093
Harald Welteaae69be2004-08-29 23:32:14 +00001094/* Returns a pointer to the target name of this position. */
1095const char *TC_GET_TARGET(const STRUCT_ENTRY *ce,
1096 TC_HANDLE_T *handle)
1097{
1098 STRUCT_ENTRY *e = (STRUCT_ENTRY *)ce;
1099 struct rule_head *r = container_of(e, struct rule_head, entry);
1100
1101 iptc_fn = TC_GET_TARGET;
1102
1103 switch(r->type) {
1104 int spos;
1105 case IPTCC_R_FALLTHROUGH:
1106 return "";
1107 break;
1108 case IPTCC_R_JUMP:
1109 DEBUGP("r=%p, jump=%p, name=`%s'\n", r, r->jump, r->jump->name);
1110 return r->jump->name;
1111 break;
1112 case IPTCC_R_STANDARD:
1113 spos = *(int *)GET_TARGET(e)->data;
1114 DEBUGP("r=%p, spos=%d'\n", r, spos);
1115 return standard_target_map(spos);
1116 break;
1117 case IPTCC_R_MODULE:
1118 return GET_TARGET(e)->u.user.name;
1119 break;
1120 }
1121 return NULL;
1122}
Marc Bouchere6869a82000-03-20 06:03:29 +00001123/* Is this a built-in chain? Actually returns hook + 1. */
1124int
Rusty Russell79dee072000-05-02 16:45:16 +00001125TC_BUILTIN(const char *chain, const TC_HANDLE_T handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001126{
Harald Welteaae69be2004-08-29 23:32:14 +00001127 struct chain_head *c;
1128
1129 iptc_fn = TC_BUILTIN;
Marc Bouchere6869a82000-03-20 06:03:29 +00001130
Harald Welteaae69be2004-08-29 23:32:14 +00001131 c = iptcc_find_label(chain, handle);
1132 if (!c) {
1133 errno = ENOENT;
1134 return -1;
Marc Bouchere6869a82000-03-20 06:03:29 +00001135 }
Harald Welteaae69be2004-08-29 23:32:14 +00001136
1137 return iptcc_is_builtin(c);
Marc Bouchere6869a82000-03-20 06:03:29 +00001138}
1139
1140/* Get the policy of a given built-in chain */
1141const char *
Rusty Russell79dee072000-05-02 16:45:16 +00001142TC_GET_POLICY(const char *chain,
1143 STRUCT_COUNTERS *counters,
1144 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001145{
Harald Welteaae69be2004-08-29 23:32:14 +00001146 struct chain_head *c;
Marc Bouchere6869a82000-03-20 06:03:29 +00001147
Harald Welteaae69be2004-08-29 23:32:14 +00001148 iptc_fn = TC_GET_POLICY;
1149
1150 DEBUGP("called for chain %s\n", chain);
1151
1152 c = iptcc_find_label(chain, *handle);
1153 if (!c) {
1154 errno = ENOENT;
1155 return NULL;
1156 }
1157
1158 if (!iptcc_is_builtin(c))
Marc Bouchere6869a82000-03-20 06:03:29 +00001159 return NULL;
1160
Harald Welteaae69be2004-08-29 23:32:14 +00001161 *counters = c->counters;
Marc Bouchere6869a82000-03-20 06:03:29 +00001162
Harald Welteaae69be2004-08-29 23:32:14 +00001163 return standard_target_map(c->verdict);
Harald Welte0113fe72004-01-06 19:04:02 +00001164}
1165
1166static int
Harald Welteaae69be2004-08-29 23:32:14 +00001167iptcc_standard_map(struct rule_head *r, int verdict)
Harald Welte0113fe72004-01-06 19:04:02 +00001168{
Harald Welteaae69be2004-08-29 23:32:14 +00001169 STRUCT_ENTRY *e = r->entry;
Rusty Russell79dee072000-05-02 16:45:16 +00001170 STRUCT_STANDARD_TARGET *t;
Marc Bouchere6869a82000-03-20 06:03:29 +00001171
Rusty Russell79dee072000-05-02 16:45:16 +00001172 t = (STRUCT_STANDARD_TARGET *)GET_TARGET(e);
Marc Bouchere6869a82000-03-20 06:03:29 +00001173
Rusty Russell67088e72000-05-10 01:18:57 +00001174 if (t->target.u.target_size
Philip Blundell8c700902000-05-15 02:17:52 +00001175 != ALIGN(sizeof(STRUCT_STANDARD_TARGET))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001176 errno = EINVAL;
1177 return 0;
1178 }
1179 /* memset for memcmp convenience on delete/replace */
Rusty Russell79dee072000-05-02 16:45:16 +00001180 memset(t->target.u.user.name, 0, FUNCTION_MAXNAMELEN);
1181 strcpy(t->target.u.user.name, STANDARD_TARGET);
Marc Bouchere6869a82000-03-20 06:03:29 +00001182 t->verdict = verdict;
1183
Harald Welteaae69be2004-08-29 23:32:14 +00001184 r->type = IPTCC_R_STANDARD;
1185
Marc Bouchere6869a82000-03-20 06:03:29 +00001186 return 1;
1187}
Rusty Russell7e53bf92000-03-20 07:03:28 +00001188
Marc Bouchere6869a82000-03-20 06:03:29 +00001189static int
Harald Welteaae69be2004-08-29 23:32:14 +00001190iptcc_map_target(const TC_HANDLE_T handle,
1191 struct rule_head *r)
Marc Bouchere6869a82000-03-20 06:03:29 +00001192{
Harald Welteaae69be2004-08-29 23:32:14 +00001193 STRUCT_ENTRY *e = r->entry;
Harald Welte0113fe72004-01-06 19:04:02 +00001194 STRUCT_ENTRY_TARGET *t = GET_TARGET(e);
Marc Bouchere6869a82000-03-20 06:03:29 +00001195
Marc Bouchere6869a82000-03-20 06:03:29 +00001196 /* Maybe it's empty (=> fall through) */
Harald Welteaae69be2004-08-29 23:32:14 +00001197 if (strcmp(t->u.user.name, "") == 0) {
1198 r->type = IPTCC_R_FALLTHROUGH;
1199 return 1;
1200 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001201 /* Maybe it's a standard target name... */
Rusty Russell79dee072000-05-02 16:45:16 +00001202 else if (strcmp(t->u.user.name, LABEL_ACCEPT) == 0)
Harald Welteaae69be2004-08-29 23:32:14 +00001203 return iptcc_standard_map(r, -NF_ACCEPT - 1);
Rusty Russell79dee072000-05-02 16:45:16 +00001204 else if (strcmp(t->u.user.name, LABEL_DROP) == 0)
Harald Welteaae69be2004-08-29 23:32:14 +00001205 return iptcc_standard_map(r, -NF_DROP - 1);
Rusty Russell67088e72000-05-10 01:18:57 +00001206 else if (strcmp(t->u.user.name, LABEL_QUEUE) == 0)
Harald Welteaae69be2004-08-29 23:32:14 +00001207 return iptcc_standard_map(r, -NF_QUEUE - 1);
Rusty Russell79dee072000-05-02 16:45:16 +00001208 else if (strcmp(t->u.user.name, LABEL_RETURN) == 0)
Harald Welteaae69be2004-08-29 23:32:14 +00001209 return iptcc_standard_map(r, RETURN);
Rusty Russell79dee072000-05-02 16:45:16 +00001210 else if (TC_BUILTIN(t->u.user.name, handle)) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001211 /* Can't jump to builtins. */
1212 errno = EINVAL;
1213 return 0;
1214 } else {
1215 /* Maybe it's an existing chain name. */
Harald Welteaae69be2004-08-29 23:32:14 +00001216 struct chain_head *c;
1217 DEBUGP("trying to find chain `%s': ", t->u.user.name);
Marc Bouchere6869a82000-03-20 06:03:29 +00001218
Harald Welteaae69be2004-08-29 23:32:14 +00001219 c = iptcc_find_label(t->u.user.name, handle);
1220 if (c) {
1221 DEBUGP_C("found!\n");
1222 r->type = IPTCC_R_JUMP;
1223 r->jump = c;
1224 c->references++;
1225 return 1;
1226 }
1227 DEBUGP_C("not found :(\n");
Marc Bouchere6869a82000-03-20 06:03:29 +00001228 }
1229
1230 /* Must be a module? If not, kernel will reject... */
1231 /* memset to all 0 for your memcmp convenience. */
Rusty Russell228e98d2000-04-27 10:28:06 +00001232 memset(t->u.user.name + strlen(t->u.user.name),
Marc Bouchere6869a82000-03-20 06:03:29 +00001233 0,
Rusty Russell79dee072000-05-02 16:45:16 +00001234 FUNCTION_MAXNAMELEN - strlen(t->u.user.name));
Harald Welteaae69be2004-08-29 23:32:14 +00001235
1236 set_changed(handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001237 return 1;
1238}
1239
Harald Welte0113fe72004-01-06 19:04:02 +00001240/* Insert the entry `fw' in chain `chain' into position `rulenum'. */
Marc Bouchere6869a82000-03-20 06:03:29 +00001241int
Rusty Russell79dee072000-05-02 16:45:16 +00001242TC_INSERT_ENTRY(const IPT_CHAINLABEL chain,
1243 const STRUCT_ENTRY *e,
1244 unsigned int rulenum,
1245 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001246{
Harald Welteaae69be2004-08-29 23:32:14 +00001247 struct chain_head *c;
Martin Josefssoneb066cc2004-09-22 21:04:07 +00001248 struct rule_head *r;
1249 struct list_head *prev;
Marc Bouchere6869a82000-03-20 06:03:29 +00001250
Rusty Russell79dee072000-05-02 16:45:16 +00001251 iptc_fn = TC_INSERT_ENTRY;
Harald Welteaae69be2004-08-29 23:32:14 +00001252
1253 if (!(c = iptcc_find_label(chain, *handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001254 errno = ENOENT;
1255 return 0;
1256 }
1257
Martin Josefssoneb066cc2004-09-22 21:04:07 +00001258 /* first rulenum index = 0
1259 first c->num_rules index = 1 */
1260 if (rulenum > c->num_rules) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001261 errno = E2BIG;
1262 return 0;
1263 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001264
Martin Josefssoneb066cc2004-09-22 21:04:07 +00001265 /* Try to get the rule we want to insert after.
1266 In case of no rules, insert after chain head. */
1267 r = iptcc_get_rule_num(c, rulenum + 1);
1268 if (r)
1269 prev = &r->list;
1270 else
1271 prev = &c->rules;
1272
Harald Welteaae69be2004-08-29 23:32:14 +00001273 if (!(r = iptcc_alloc_rule(c, e->next_offset))) {
1274 errno = ENOMEM;
Harald Welte0113fe72004-01-06 19:04:02 +00001275 return 0;
Harald Welteaae69be2004-08-29 23:32:14 +00001276 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001277
Harald Welteaae69be2004-08-29 23:32:14 +00001278 memcpy(r->entry, e, e->next_offset);
1279 r->counter_map.maptype = COUNTER_MAP_SET;
1280
1281 if (!iptcc_map_target(*handle, r)) {
1282 free(r);
1283 return 0;
1284 }
1285
Martin Josefssoneb066cc2004-09-22 21:04:07 +00001286 list_add_tail(&r->list, prev);
Harald Welteaae69be2004-08-29 23:32:14 +00001287 c->num_rules++;
1288
1289 set_changed(*handle);
1290
1291 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00001292}
1293
1294/* Atomically replace rule `rulenum' in `chain' with `fw'. */
1295int
Rusty Russell79dee072000-05-02 16:45:16 +00001296TC_REPLACE_ENTRY(const IPT_CHAINLABEL chain,
1297 const STRUCT_ENTRY *e,
1298 unsigned int rulenum,
1299 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001300{
Harald Welteaae69be2004-08-29 23:32:14 +00001301 struct chain_head *c;
1302 struct rule_head *r, *old;
Marc Bouchere6869a82000-03-20 06:03:29 +00001303
Rusty Russell79dee072000-05-02 16:45:16 +00001304 iptc_fn = TC_REPLACE_ENTRY;
Marc Bouchere6869a82000-03-20 06:03:29 +00001305
Harald Welteaae69be2004-08-29 23:32:14 +00001306 if (!(c = iptcc_find_label(chain, *handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001307 errno = ENOENT;
1308 return 0;
1309 }
1310
Martin Josefsson8e795b02004-09-22 21:31:09 +00001311 if (!(old = iptcc_get_rule_num(c, rulenum + 1))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001312 errno = E2BIG;
1313 return 0;
1314 }
1315
Harald Welteaae69be2004-08-29 23:32:14 +00001316 if (!(r = iptcc_alloc_rule(c, e->next_offset))) {
1317 errno = ENOMEM;
Marc Bouchere6869a82000-03-20 06:03:29 +00001318 return 0;
Harald Welteaae69be2004-08-29 23:32:14 +00001319 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001320
Harald Welteaae69be2004-08-29 23:32:14 +00001321 memcpy(r->entry, e, e->next_offset);
1322 r->counter_map.maptype = COUNTER_MAP_SET;
1323
1324 if (!iptcc_map_target(*handle, r)) {
1325 free(r);
Harald Welte0113fe72004-01-06 19:04:02 +00001326 return 0;
Harald Welteaae69be2004-08-29 23:32:14 +00001327 }
Harald Welte0113fe72004-01-06 19:04:02 +00001328
Harald Welteaae69be2004-08-29 23:32:14 +00001329 list_add(&r->list, &old->list);
1330 iptcc_delete_rule(old);
1331
1332 set_changed(*handle);
1333
1334 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00001335}
1336
Harald Welte0113fe72004-01-06 19:04:02 +00001337/* Append entry `fw' to chain `chain'. Equivalent to insert with
Marc Bouchere6869a82000-03-20 06:03:29 +00001338 rulenum = length of chain. */
1339int
Rusty Russell79dee072000-05-02 16:45:16 +00001340TC_APPEND_ENTRY(const IPT_CHAINLABEL chain,
1341 const STRUCT_ENTRY *e,
1342 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001343{
Harald Welteaae69be2004-08-29 23:32:14 +00001344 struct chain_head *c;
1345 struct rule_head *r;
Marc Bouchere6869a82000-03-20 06:03:29 +00001346
Rusty Russell79dee072000-05-02 16:45:16 +00001347 iptc_fn = TC_APPEND_ENTRY;
Harald Welteaae69be2004-08-29 23:32:14 +00001348 if (!(c = iptcc_find_label(chain, *handle))) {
1349 DEBUGP("unable to find chain `%s'\n", chain);
Marc Bouchere6869a82000-03-20 06:03:29 +00001350 errno = ENOENT;
1351 return 0;
1352 }
1353
Harald Welteaae69be2004-08-29 23:32:14 +00001354 if (!(r = iptcc_alloc_rule(c, e->next_offset))) {
1355 DEBUGP("unable to allocate rule for chain `%s'\n", chain);
1356 errno = ENOMEM;
Harald Welte0113fe72004-01-06 19:04:02 +00001357 return 0;
Harald Welteaae69be2004-08-29 23:32:14 +00001358 }
Harald Welte0113fe72004-01-06 19:04:02 +00001359
Harald Welteaae69be2004-08-29 23:32:14 +00001360 memcpy(r->entry, e, e->next_offset);
1361 r->counter_map.maptype = COUNTER_MAP_SET;
1362
1363 if (!iptcc_map_target(*handle, r)) {
1364 DEBUGP("unable to ma target of rule for chain `%s'\n", chain);
1365 free(r);
1366 return 0;
1367 }
1368
1369 list_add_tail(&r->list, &c->rules);
1370 c->num_rules++;
1371
1372 set_changed(*handle);
1373
1374 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00001375}
1376
1377static inline int
Rusty Russell79dee072000-05-02 16:45:16 +00001378match_different(const STRUCT_ENTRY_MATCH *a,
Rusty Russelledf14cf2000-04-19 11:26:44 +00001379 const unsigned char *a_elems,
1380 const unsigned char *b_elems,
1381 unsigned char **maskptr)
Marc Bouchere6869a82000-03-20 06:03:29 +00001382{
Rusty Russell79dee072000-05-02 16:45:16 +00001383 const STRUCT_ENTRY_MATCH *b;
Rusty Russelledf14cf2000-04-19 11:26:44 +00001384 unsigned int i;
Marc Bouchere6869a82000-03-20 06:03:29 +00001385
1386 /* Offset of b is the same as a. */
Rusty Russell30fd6e52000-04-23 09:16:06 +00001387 b = (void *)b_elems + ((unsigned char *)a - a_elems);
Marc Bouchere6869a82000-03-20 06:03:29 +00001388
Rusty Russell228e98d2000-04-27 10:28:06 +00001389 if (a->u.match_size != b->u.match_size)
Marc Bouchere6869a82000-03-20 06:03:29 +00001390 return 1;
1391
Rusty Russell228e98d2000-04-27 10:28:06 +00001392 if (strcmp(a->u.user.name, b->u.user.name) != 0)
Marc Bouchere6869a82000-03-20 06:03:29 +00001393 return 1;
1394
Rusty Russell73ef09b2000-07-03 10:24:04 +00001395 *maskptr += ALIGN(sizeof(*a));
Rusty Russelledf14cf2000-04-19 11:26:44 +00001396
Rusty Russell73ef09b2000-07-03 10:24:04 +00001397 for (i = 0; i < a->u.match_size - ALIGN(sizeof(*a)); i++)
Rusty Russelledf14cf2000-04-19 11:26:44 +00001398 if (((a->data[i] ^ b->data[i]) & (*maskptr)[i]) != 0)
Rusty Russell90e712a2000-03-29 04:19:26 +00001399 return 1;
Rusty Russelledf14cf2000-04-19 11:26:44 +00001400 *maskptr += i;
1401 return 0;
1402}
1403
1404static inline int
1405target_different(const unsigned char *a_targdata,
1406 const unsigned char *b_targdata,
1407 unsigned int tdatasize,
1408 const unsigned char *mask)
1409{
1410 unsigned int i;
1411 for (i = 0; i < tdatasize; i++)
1412 if (((a_targdata[i] ^ b_targdata[i]) & mask[i]) != 0)
1413 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00001414
1415 return 0;
1416}
1417
Rusty Russell79dee072000-05-02 16:45:16 +00001418static int
1419is_same(const STRUCT_ENTRY *a,
1420 const STRUCT_ENTRY *b,
1421 unsigned char *matchmask);
Marc Bouchere6869a82000-03-20 06:03:29 +00001422
Harald Welte0113fe72004-01-06 19:04:02 +00001423/* Delete the first rule in `chain' which matches `fw'. */
Marc Bouchere6869a82000-03-20 06:03:29 +00001424int
Rusty Russell79dee072000-05-02 16:45:16 +00001425TC_DELETE_ENTRY(const IPT_CHAINLABEL chain,
1426 const STRUCT_ENTRY *origfw,
1427 unsigned char *matchmask,
1428 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001429{
Harald Welteaae69be2004-08-29 23:32:14 +00001430 struct chain_head *c;
Harald Weltefe537072004-08-30 20:28:53 +00001431 struct rule_head *r;
Harald Welte0113fe72004-01-06 19:04:02 +00001432 STRUCT_ENTRY *e, *fw;
Marc Bouchere6869a82000-03-20 06:03:29 +00001433
Rusty Russell79dee072000-05-02 16:45:16 +00001434 iptc_fn = TC_DELETE_ENTRY;
Harald Welteaae69be2004-08-29 23:32:14 +00001435 if (!(c = iptcc_find_label(chain, *handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001436 errno = ENOENT;
1437 return 0;
1438 }
1439
Harald Welte0113fe72004-01-06 19:04:02 +00001440 fw = malloc(origfw->next_offset);
1441 if (fw == NULL) {
1442 errno = ENOMEM;
1443 return 0;
1444 }
1445
Harald Weltefe537072004-08-30 20:28:53 +00001446 list_for_each_entry(r, &c->rules, list) {
Harald Welte0113fe72004-01-06 19:04:02 +00001447
1448 memcpy(fw, origfw, origfw->next_offset);
1449
Harald Weltefe537072004-08-30 20:28:53 +00001450#if 0
Harald Welte0113fe72004-01-06 19:04:02 +00001451 /* FIXME: handle this in is_same --RR */
1452 if (!map_target(*handle, fw, offset, &discard)) {
1453 free(fw);
1454 return 0;
1455 }
Harald Welte0113fe72004-01-06 19:04:02 +00001456#endif
Harald Weltefe537072004-08-30 20:28:53 +00001457
1458 e = r->entry;
1459
Harald Welte0113fe72004-01-06 19:04:02 +00001460 if (is_same(e, fw, matchmask)) {
Harald Weltefe537072004-08-30 20:28:53 +00001461 /* If we are about to delete the rule that is the
1462 * current iterator, move rule iterator back. next
1463 * pointer will then point to real next node */
1464 if (r == (*handle)->rule_iterator_cur) {
1465 (*handle)->rule_iterator_cur =
1466 list_entry((*handle)->rule_iterator_cur->list.prev,
1467 struct rule_head, list);
1468 }
1469
1470 c->num_rules--;
1471 iptcc_delete_rule(r);
Martin Josefsson2a5dbbb2004-09-22 21:37:41 +00001472
1473 set_changed(*handle);
Harald Weltefe537072004-08-30 20:28:53 +00001474 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00001475 }
1476 }
1477
Harald Welte0113fe72004-01-06 19:04:02 +00001478 free(fw);
Marc Bouchere6869a82000-03-20 06:03:29 +00001479 errno = ENOENT;
1480 return 0;
Rusty Russell7e53bf92000-03-20 07:03:28 +00001481}
Harald Welteaae69be2004-08-29 23:32:14 +00001482
Marc Bouchere6869a82000-03-20 06:03:29 +00001483
1484/* Delete the rule in position `rulenum' in `chain'. */
1485int
Rusty Russell79dee072000-05-02 16:45:16 +00001486TC_DELETE_NUM_ENTRY(const IPT_CHAINLABEL chain,
1487 unsigned int rulenum,
1488 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001489{
Harald Welteaae69be2004-08-29 23:32:14 +00001490 struct chain_head *c;
1491 struct rule_head *r;
Marc Bouchere6869a82000-03-20 06:03:29 +00001492
Rusty Russell79dee072000-05-02 16:45:16 +00001493 iptc_fn = TC_DELETE_NUM_ENTRY;
Harald Welteaae69be2004-08-29 23:32:14 +00001494
1495 if (!(c = iptcc_find_label(chain, *handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001496 errno = ENOENT;
1497 return 0;
1498 }
1499
Martin Josefsson8e795b02004-09-22 21:31:09 +00001500 if (!(r = iptcc_get_rule_num(c, rulenum + 1))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001501 errno = E2BIG;
1502 return 0;
1503 }
1504
Harald Welteaae69be2004-08-29 23:32:14 +00001505 /* If we are about to delete the rule that is the current
1506 * iterator, move rule iterator back. next pointer will then
1507 * point to real next node */
1508 if (r == (*handle)->rule_iterator_cur) {
1509 (*handle)->rule_iterator_cur =
1510 list_entry((*handle)->rule_iterator_cur->list.prev,
1511 struct rule_head, list);
Harald Welte0113fe72004-01-06 19:04:02 +00001512 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001513
Harald Welteaae69be2004-08-29 23:32:14 +00001514 c->num_rules--;
1515 iptcc_delete_rule(r);
1516
Martin Josefsson2a5dbbb2004-09-22 21:37:41 +00001517 set_changed(*handle);
1518
Harald Welteaae69be2004-08-29 23:32:14 +00001519 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00001520}
1521
1522/* Check the packet `fw' on chain `chain'. Returns the verdict, or
1523 NULL and sets errno. */
1524const char *
Rusty Russell79dee072000-05-02 16:45:16 +00001525TC_CHECK_PACKET(const IPT_CHAINLABEL chain,
1526 STRUCT_ENTRY *entry,
1527 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001528{
1529 errno = ENOSYS;
1530 return NULL;
1531}
1532
1533/* Flushes the entries in the given chain (ie. empties chain). */
1534int
Rusty Russell79dee072000-05-02 16:45:16 +00001535TC_FLUSH_ENTRIES(const IPT_CHAINLABEL chain, TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001536{
Harald Welteaae69be2004-08-29 23:32:14 +00001537 struct chain_head *c;
1538 struct rule_head *r, *tmp;
Marc Bouchere6869a82000-03-20 06:03:29 +00001539
Harald Welte0113fe72004-01-06 19:04:02 +00001540 iptc_fn = TC_FLUSH_ENTRIES;
Harald Welteaae69be2004-08-29 23:32:14 +00001541 if (!(c = iptcc_find_label(chain, *handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001542 errno = ENOENT;
1543 return 0;
1544 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001545
Harald Welteaae69be2004-08-29 23:32:14 +00001546 list_for_each_entry_safe(r, tmp, &c->rules, list) {
1547 iptcc_delete_rule(r);
1548 }
1549
1550 c->num_rules = 0;
1551
1552 set_changed(*handle);
1553
1554 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00001555}
1556
1557/* Zeroes the counters in a chain. */
1558int
Rusty Russell79dee072000-05-02 16:45:16 +00001559TC_ZERO_ENTRIES(const IPT_CHAINLABEL chain, TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001560{
Harald Welteaae69be2004-08-29 23:32:14 +00001561 struct chain_head *c;
1562 struct rule_head *r;
Rusty Russell7e53bf92000-03-20 07:03:28 +00001563
Harald Welteaae69be2004-08-29 23:32:14 +00001564 if (!(c = iptcc_find_label(chain, *handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001565 errno = ENOENT;
1566 return 0;
1567 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001568
Harald Welteaae69be2004-08-29 23:32:14 +00001569 list_for_each_entry(r, &c->rules, list) {
1570 if (r->counter_map.maptype == COUNTER_MAP_NORMAL_MAP)
1571 r->counter_map.maptype = COUNTER_MAP_ZEROED;
Marc Bouchere6869a82000-03-20 06:03:29 +00001572 }
Harald Welteaae69be2004-08-29 23:32:14 +00001573
Rusty Russell175f6412000-03-24 09:32:20 +00001574 set_changed(*handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001575
Marc Bouchere6869a82000-03-20 06:03:29 +00001576 return 1;
1577}
1578
Harald Welte1cef74d2001-01-05 15:22:59 +00001579STRUCT_COUNTERS *
1580TC_READ_COUNTER(const IPT_CHAINLABEL chain,
1581 unsigned int rulenum,
1582 TC_HANDLE_T *handle)
1583{
Harald Welteaae69be2004-08-29 23:32:14 +00001584 struct chain_head *c;
1585 struct rule_head *r;
Harald Welte1cef74d2001-01-05 15:22:59 +00001586
1587 iptc_fn = TC_READ_COUNTER;
1588 CHECK(*handle);
1589
Harald Welteaae69be2004-08-29 23:32:14 +00001590 if (!(c = iptcc_find_label(chain, *handle))) {
Harald Welte1cef74d2001-01-05 15:22:59 +00001591 errno = ENOENT;
1592 return NULL;
1593 }
1594
Harald Welteaae69be2004-08-29 23:32:14 +00001595 if (!(r = iptcc_get_rule_num(c, rulenum))) {
Harald Welte0113fe72004-01-06 19:04:02 +00001596 errno = E2BIG;
1597 return NULL;
1598 }
1599
Harald Welteaae69be2004-08-29 23:32:14 +00001600 return &r->entry[0].counters;
Harald Welte1cef74d2001-01-05 15:22:59 +00001601}
1602
1603int
1604TC_ZERO_COUNTER(const IPT_CHAINLABEL chain,
1605 unsigned int rulenum,
1606 TC_HANDLE_T *handle)
1607{
Harald Welteaae69be2004-08-29 23:32:14 +00001608 struct chain_head *c;
1609 struct rule_head *r;
Harald Welte1cef74d2001-01-05 15:22:59 +00001610
1611 iptc_fn = TC_ZERO_COUNTER;
1612 CHECK(*handle);
1613
Harald Welteaae69be2004-08-29 23:32:14 +00001614 if (!(c = iptcc_find_label(chain, *handle))) {
Harald Welte1cef74d2001-01-05 15:22:59 +00001615 errno = ENOENT;
1616 return 0;
1617 }
1618
Harald Welteaae69be2004-08-29 23:32:14 +00001619 if (!(r = iptcc_get_rule_num(c, rulenum))) {
Harald Welte0113fe72004-01-06 19:04:02 +00001620 errno = E2BIG;
1621 return 0;
1622 }
1623
Harald Welteaae69be2004-08-29 23:32:14 +00001624 if (r->counter_map.maptype == COUNTER_MAP_NORMAL_MAP)
1625 r->counter_map.maptype = COUNTER_MAP_ZEROED;
Harald Welte1cef74d2001-01-05 15:22:59 +00001626
1627 set_changed(*handle);
1628
1629 return 1;
1630}
1631
1632int
1633TC_SET_COUNTER(const IPT_CHAINLABEL chain,
1634 unsigned int rulenum,
1635 STRUCT_COUNTERS *counters,
1636 TC_HANDLE_T *handle)
1637{
Harald Welteaae69be2004-08-29 23:32:14 +00001638 struct chain_head *c;
1639 struct rule_head *r;
Harald Welte1cef74d2001-01-05 15:22:59 +00001640 STRUCT_ENTRY *e;
Harald Welte1cef74d2001-01-05 15:22:59 +00001641
1642 iptc_fn = TC_SET_COUNTER;
1643 CHECK(*handle);
1644
Harald Welteaae69be2004-08-29 23:32:14 +00001645 if (!(c = iptcc_find_label(chain, *handle))) {
Harald Welte1cef74d2001-01-05 15:22:59 +00001646 errno = ENOENT;
1647 return 0;
1648 }
Harald Welte0113fe72004-01-06 19:04:02 +00001649
Harald Welteaae69be2004-08-29 23:32:14 +00001650 if (!(r = iptcc_get_rule_num(c, rulenum))) {
Harald Welte0113fe72004-01-06 19:04:02 +00001651 errno = E2BIG;
1652 return 0;
1653 }
1654
Harald Welteaae69be2004-08-29 23:32:14 +00001655 e = r->entry;
1656 r->counter_map.maptype = COUNTER_MAP_SET;
Harald Welte0113fe72004-01-06 19:04:02 +00001657
1658 memcpy(&e->counters, counters, sizeof(STRUCT_COUNTERS));
Harald Welte1cef74d2001-01-05 15:22:59 +00001659
1660 set_changed(*handle);
1661
1662 return 1;
1663}
1664
Marc Bouchere6869a82000-03-20 06:03:29 +00001665/* Creates a new chain. */
1666/* To create a chain, create two rules: error node and unconditional
1667 * return. */
1668int
Rusty Russell79dee072000-05-02 16:45:16 +00001669TC_CREATE_CHAIN(const IPT_CHAINLABEL chain, TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001670{
Harald Welteaae69be2004-08-29 23:32:14 +00001671 static struct chain_head *c;
Marc Bouchere6869a82000-03-20 06:03:29 +00001672
Rusty Russell79dee072000-05-02 16:45:16 +00001673 iptc_fn = TC_CREATE_CHAIN;
Marc Bouchere6869a82000-03-20 06:03:29 +00001674
1675 /* find_label doesn't cover built-in targets: DROP, ACCEPT,
1676 QUEUE, RETURN. */
Harald Welteaae69be2004-08-29 23:32:14 +00001677 if (iptcc_find_label(chain, *handle)
Rusty Russell79dee072000-05-02 16:45:16 +00001678 || strcmp(chain, LABEL_DROP) == 0
1679 || strcmp(chain, LABEL_ACCEPT) == 0
Rusty Russell67088e72000-05-10 01:18:57 +00001680 || strcmp(chain, LABEL_QUEUE) == 0
Rusty Russell79dee072000-05-02 16:45:16 +00001681 || strcmp(chain, LABEL_RETURN) == 0) {
Harald Welteaae69be2004-08-29 23:32:14 +00001682 DEBUGP("Chain `%s' already exists\n", chain);
Marc Bouchere6869a82000-03-20 06:03:29 +00001683 errno = EEXIST;
1684 return 0;
1685 }
1686
Rusty Russell79dee072000-05-02 16:45:16 +00001687 if (strlen(chain)+1 > sizeof(IPT_CHAINLABEL)) {
Harald Welteaae69be2004-08-29 23:32:14 +00001688 DEBUGP("Chain name `%s' too long\n", chain);
Marc Bouchere6869a82000-03-20 06:03:29 +00001689 errno = EINVAL;
1690 return 0;
1691 }
1692
Harald Welteaae69be2004-08-29 23:32:14 +00001693 c = iptcc_alloc_chain_head(chain, 0);
1694 if (!c) {
1695 DEBUGP("Cannot allocate memory for chain `%s'\n", chain);
1696 errno = ENOMEM;
1697 return 0;
Marc Bouchere6869a82000-03-20 06:03:29 +00001698
Harald Welteaae69be2004-08-29 23:32:14 +00001699 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001700
Harald Welteaae69be2004-08-29 23:32:14 +00001701 DEBUGP("Creating chain `%s'\n", chain);
1702 list_add_tail(&c->list, &(*handle)->chains);
Harald Welte0113fe72004-01-06 19:04:02 +00001703
1704 set_changed(*handle);
1705
Harald Welteaae69be2004-08-29 23:32:14 +00001706 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00001707}
1708
1709/* Get the number of references to this chain. */
1710int
Rusty Russell79dee072000-05-02 16:45:16 +00001711TC_GET_REFERENCES(unsigned int *ref, const IPT_CHAINLABEL chain,
1712 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001713{
Harald Welteaae69be2004-08-29 23:32:14 +00001714 struct chain_head *c;
Marc Bouchere6869a82000-03-20 06:03:29 +00001715
Harald Welteaae69be2004-08-29 23:32:14 +00001716 if (!(c = iptcc_find_label(chain, *handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001717 errno = ENOENT;
1718 return 0;
1719 }
1720
Harald Welteaae69be2004-08-29 23:32:14 +00001721 *ref = c->references;
1722
Marc Bouchere6869a82000-03-20 06:03:29 +00001723 return 1;
1724}
1725
1726/* Deletes a chain. */
1727int
Rusty Russell79dee072000-05-02 16:45:16 +00001728TC_DELETE_CHAIN(const IPT_CHAINLABEL chain, TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001729{
Marc Bouchere6869a82000-03-20 06:03:29 +00001730 unsigned int references;
Harald Welteaae69be2004-08-29 23:32:14 +00001731 struct chain_head *c;
Rusty Russell7e53bf92000-03-20 07:03:28 +00001732
Rusty Russell79dee072000-05-02 16:45:16 +00001733 iptc_fn = TC_DELETE_CHAIN;
Marc Bouchere6869a82000-03-20 06:03:29 +00001734
Harald Welteaae69be2004-08-29 23:32:14 +00001735 if (!(c = iptcc_find_label(chain, *handle))) {
1736 DEBUGP("cannot find chain `%s'\n", chain);
Marc Bouchere6869a82000-03-20 06:03:29 +00001737 errno = ENOENT;
1738 return 0;
1739 }
1740
Harald Welteaae69be2004-08-29 23:32:14 +00001741 if (TC_BUILTIN(chain, *handle)) {
1742 DEBUGP("cannot remove builtin chain `%s'\n", chain);
1743 errno = EINVAL;
1744 return 0;
1745 }
1746
1747 if (!TC_GET_REFERENCES(&references, chain, handle)) {
1748 DEBUGP("cannot get references on chain `%s'\n", chain);
1749 return 0;
1750 }
1751
1752 if (references > 0) {
1753 DEBUGP("chain `%s' still has references\n", chain);
1754 errno = EMLINK;
1755 return 0;
1756 }
1757
1758 if (c->num_rules) {
1759 DEBUGP("chain `%s' is not empty\n", chain);
Marc Bouchere6869a82000-03-20 06:03:29 +00001760 errno = ENOTEMPTY;
1761 return 0;
1762 }
1763
Harald Welteaae69be2004-08-29 23:32:14 +00001764 /* If we are about to delete the chain that is the current
1765 * iterator, move chain iterator firward. */
1766 if (c == (*handle)->chain_iterator_cur)
1767 iptcc_chain_iterator_advance(*handle);
Harald Welte0113fe72004-01-06 19:04:02 +00001768
Harald Welteaae69be2004-08-29 23:32:14 +00001769 list_del(&c->list);
1770 free(c);
Harald Welte0113fe72004-01-06 19:04:02 +00001771
Harald Welteaae69be2004-08-29 23:32:14 +00001772 DEBUGP("chain `%s' deleted\n", chain);
1773
1774 set_changed(*handle);
1775
1776 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00001777}
1778
1779/* Renames a chain. */
Rusty Russell79dee072000-05-02 16:45:16 +00001780int TC_RENAME_CHAIN(const IPT_CHAINLABEL oldname,
1781 const IPT_CHAINLABEL newname,
1782 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001783{
Harald Welteaae69be2004-08-29 23:32:14 +00001784 struct chain_head *c;
Rusty Russell79dee072000-05-02 16:45:16 +00001785 iptc_fn = TC_RENAME_CHAIN;
Marc Bouchere6869a82000-03-20 06:03:29 +00001786
Harald Welte1de80462000-10-30 12:00:27 +00001787 /* find_label doesn't cover built-in targets: DROP, ACCEPT,
1788 QUEUE, RETURN. */
Harald Welteaae69be2004-08-29 23:32:14 +00001789 if (iptcc_find_label(newname, *handle)
Rusty Russell79dee072000-05-02 16:45:16 +00001790 || strcmp(newname, LABEL_DROP) == 0
1791 || strcmp(newname, LABEL_ACCEPT) == 0
Harald Welte1de80462000-10-30 12:00:27 +00001792 || strcmp(newname, LABEL_QUEUE) == 0
Rusty Russell79dee072000-05-02 16:45:16 +00001793 || strcmp(newname, LABEL_RETURN) == 0) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001794 errno = EEXIST;
1795 return 0;
1796 }
1797
Harald Welteaae69be2004-08-29 23:32:14 +00001798 if (!(c = iptcc_find_label(oldname, *handle))
Rusty Russell79dee072000-05-02 16:45:16 +00001799 || TC_BUILTIN(oldname, *handle)) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001800 errno = ENOENT;
1801 return 0;
1802 }
1803
Rusty Russell79dee072000-05-02 16:45:16 +00001804 if (strlen(newname)+1 > sizeof(IPT_CHAINLABEL)) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001805 errno = EINVAL;
1806 return 0;
1807 }
1808
Harald Welteaae69be2004-08-29 23:32:14 +00001809 strncpy(c->name, newname, sizeof(IPT_CHAINLABEL));
1810
Harald Welte0113fe72004-01-06 19:04:02 +00001811 set_changed(*handle);
1812
Marc Bouchere6869a82000-03-20 06:03:29 +00001813 return 1;
1814}
1815
1816/* Sets the policy on a built-in chain. */
1817int
Rusty Russell79dee072000-05-02 16:45:16 +00001818TC_SET_POLICY(const IPT_CHAINLABEL chain,
1819 const IPT_CHAINLABEL policy,
Harald Welte1cef74d2001-01-05 15:22:59 +00001820 STRUCT_COUNTERS *counters,
Rusty Russell79dee072000-05-02 16:45:16 +00001821 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001822{
Harald Welteaae69be2004-08-29 23:32:14 +00001823 struct chain_head *c;
Marc Bouchere6869a82000-03-20 06:03:29 +00001824
Rusty Russell79dee072000-05-02 16:45:16 +00001825 iptc_fn = TC_SET_POLICY;
Marc Bouchere6869a82000-03-20 06:03:29 +00001826
Harald Welteaae69be2004-08-29 23:32:14 +00001827 if (!(c = iptcc_find_label(chain, *handle))) {
1828 DEBUGP("cannot find chain `%s'\n", chain);
1829 errno = ENOENT;
Marc Bouchere6869a82000-03-20 06:03:29 +00001830 return 0;
1831 }
1832
Harald Welteaae69be2004-08-29 23:32:14 +00001833 if (!iptcc_is_builtin(c)) {
1834 DEBUGP("cannot set policy of userdefinedchain `%s'\n", chain);
1835 errno = ENOENT;
1836 return 0;
1837 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001838
Rusty Russell79dee072000-05-02 16:45:16 +00001839 if (strcmp(policy, LABEL_ACCEPT) == 0)
Harald Welteaae69be2004-08-29 23:32:14 +00001840 c->verdict = -NF_ACCEPT - 1;
Rusty Russell79dee072000-05-02 16:45:16 +00001841 else if (strcmp(policy, LABEL_DROP) == 0)
Harald Welteaae69be2004-08-29 23:32:14 +00001842 c->verdict = -NF_DROP - 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00001843 else {
1844 errno = EINVAL;
1845 return 0;
1846 }
Harald Welte1cef74d2001-01-05 15:22:59 +00001847
Harald Welte1cef74d2001-01-05 15:22:59 +00001848 if (counters) {
1849 /* set byte and packet counters */
Harald Welteaae69be2004-08-29 23:32:14 +00001850 memcpy(&c->counters, counters, sizeof(STRUCT_COUNTERS));
1851 c->counter_map.maptype = COUNTER_MAP_SET;
Harald Welte1cef74d2001-01-05 15:22:59 +00001852 } else {
Harald Welteaae69be2004-08-29 23:32:14 +00001853 c->counter_map.maptype = COUNTER_MAP_NOMAP;
Harald Welte1cef74d2001-01-05 15:22:59 +00001854 }
1855
Rusty Russell175f6412000-03-24 09:32:20 +00001856 set_changed(*handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001857
Marc Bouchere6869a82000-03-20 06:03:29 +00001858 return 1;
1859}
1860
1861/* Without this, on gcc 2.7.2.3, we get:
Rusty Russell79dee072000-05-02 16:45:16 +00001862 libiptc.c: In function `TC_COMMIT':
Marc Bouchere6869a82000-03-20 06:03:29 +00001863 libiptc.c:833: fixed or forbidden register was spilled.
1864 This may be due to a compiler bug or to impossible asm
1865 statements or clauses.
1866*/
1867static void
Rusty Russell79dee072000-05-02 16:45:16 +00001868subtract_counters(STRUCT_COUNTERS *answer,
1869 const STRUCT_COUNTERS *a,
1870 const STRUCT_COUNTERS *b)
Marc Bouchere6869a82000-03-20 06:03:29 +00001871{
1872 answer->pcnt = a->pcnt - b->pcnt;
1873 answer->bcnt = a->bcnt - b->bcnt;
1874}
1875
Harald Welteaae69be2004-08-29 23:32:14 +00001876
1877static void counters_nomap(STRUCT_COUNTERS_INFO *newcounters,
1878 unsigned int index)
1879{
1880 newcounters->counters[index] = ((STRUCT_COUNTERS) { 0, 0});
1881 DEBUGP_C("NOMAP => zero\n");
1882}
1883
1884static void counters_normal_map(STRUCT_COUNTERS_INFO *newcounters,
1885 STRUCT_REPLACE *repl,
1886 unsigned int index,
1887 unsigned int mappos)
1888{
1889 /* Original read: X.
1890 * Atomic read on replacement: X + Y.
1891 * Currently in kernel: Z.
1892 * Want in kernel: X + Y + Z.
1893 * => Add in X + Y
1894 * => Add in replacement read.
1895 */
1896 newcounters->counters[index] = repl->counters[mappos];
1897 DEBUGP_C("NORMAL_MAP => mappos %u \n", mappos);
1898}
1899
1900static void counters_map_zeroed(STRUCT_COUNTERS_INFO *newcounters,
1901 STRUCT_REPLACE *repl,
1902 unsigned int index,
1903 unsigned int mappos,
1904 STRUCT_COUNTERS *counters)
1905{
1906 /* Original read: X.
1907 * Atomic read on replacement: X + Y.
1908 * Currently in kernel: Z.
1909 * Want in kernel: Y + Z.
1910 * => Add in Y.
1911 * => Add in (replacement read - original read).
1912 */
1913 subtract_counters(&newcounters->counters[index],
1914 &repl->counters[mappos],
1915 counters);
1916 DEBUGP_C("ZEROED => mappos %u\n", mappos);
1917}
1918
1919static void counters_map_set(STRUCT_COUNTERS_INFO *newcounters,
1920 unsigned int index,
1921 STRUCT_COUNTERS *counters)
1922{
1923 /* Want to set counter (iptables-restore) */
1924
1925 memcpy(&newcounters->counters[index], counters,
1926 sizeof(STRUCT_COUNTERS));
1927
1928 DEBUGP_C("SET\n");
1929}
1930
1931
Marc Bouchere6869a82000-03-20 06:03:29 +00001932int
Rusty Russell79dee072000-05-02 16:45:16 +00001933TC_COMMIT(TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001934{
1935 /* Replace, then map back the counters. */
Rusty Russell79dee072000-05-02 16:45:16 +00001936 STRUCT_REPLACE *repl;
1937 STRUCT_COUNTERS_INFO *newcounters;
Harald Welteaae69be2004-08-29 23:32:14 +00001938 struct chain_head *c;
1939 int ret;
Martin Josefsson841e4ae2003-05-02 15:30:11 +00001940 size_t counterlen;
Harald Welteaae69be2004-08-29 23:32:14 +00001941 int new_number;
1942 unsigned int new_size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001943
1944 CHECK(*handle);
Martin Josefsson841e4ae2003-05-02 15:30:11 +00001945
Marc Bouchere6869a82000-03-20 06:03:29 +00001946#if 0
Rusty Russell54c307e2000-09-04 06:47:28 +00001947 TC_DUMP_ENTRIES(*handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001948#endif
1949
1950 /* Don't commit if nothing changed. */
1951 if (!(*handle)->changed)
1952 goto finished;
1953
Harald Welteaae69be2004-08-29 23:32:14 +00001954 new_number = iptcc_compile_table_prep(*handle, &new_size);
1955 if (new_number < 0) {
1956 errno = ENOMEM;
1957 return 0;
1958 }
1959
1960 repl = malloc(sizeof(*repl) + new_size);
Marc Bouchere6869a82000-03-20 06:03:29 +00001961 if (!repl) {
1962 errno = ENOMEM;
1963 return 0;
1964 }
Harald Welteaae69be2004-08-29 23:32:14 +00001965 memset(repl, 0, sizeof(*repl));
1966
1967 counterlen = sizeof(STRUCT_COUNTERS_INFO)
1968 + sizeof(STRUCT_COUNTERS) * new_number;
Marc Bouchere6869a82000-03-20 06:03:29 +00001969
1970 /* These are the old counters we will get from kernel */
Rusty Russell79dee072000-05-02 16:45:16 +00001971 repl->counters = malloc(sizeof(STRUCT_COUNTERS)
Marc Bouchere6869a82000-03-20 06:03:29 +00001972 * (*handle)->info.num_entries);
1973 if (!repl->counters) {
1974 free(repl);
1975 errno = ENOMEM;
1976 return 0;
1977 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001978 /* These are the counters we're going to put back, later. */
1979 newcounters = malloc(counterlen);
1980 if (!newcounters) {
1981 free(repl->counters);
1982 free(repl);
1983 errno = ENOMEM;
1984 return 0;
1985 }
Harald Welteaae69be2004-08-29 23:32:14 +00001986 memset(newcounters, 0, counterlen);
Marc Bouchere6869a82000-03-20 06:03:29 +00001987
1988 strcpy(repl->name, (*handle)->info.name);
Harald Welteaae69be2004-08-29 23:32:14 +00001989 repl->num_entries = new_number;
1990 repl->size = new_size;
1991
Marc Bouchere6869a82000-03-20 06:03:29 +00001992 repl->num_counters = (*handle)->info.num_entries;
1993 repl->valid_hooks = (*handle)->info.valid_hooks;
Harald Welteaae69be2004-08-29 23:32:14 +00001994
1995 DEBUGP("num_entries=%u, size=%u, num_counters=%u\n",
1996 repl->num_entries, repl->size, repl->num_counters);
1997
1998 ret = iptcc_compile_table(*handle, repl);
1999 if (ret < 0) {
2000 errno = ret;
2001 free(repl->counters);
2002 free(repl);
2003 return 0;
2004 }
2005
2006
2007#ifdef IPTC_DEBUG2
2008 {
2009 int fd = open("/tmp/libiptc-so_set_replace.blob",
2010 O_CREAT|O_WRONLY);
2011 if (fd >= 0) {
2012 write(fd, repl, sizeof(*repl) + repl->size);
2013 close(fd);
2014 }
2015 }
2016#endif
Marc Bouchere6869a82000-03-20 06:03:29 +00002017
Rusty Russell79dee072000-05-02 16:45:16 +00002018 if (setsockopt(sockfd, TC_IPPROTO, SO_SET_REPLACE, repl,
Harald Welteaae69be2004-08-29 23:32:14 +00002019 sizeof(*repl) + repl->size) < 0) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002020 free(repl->counters);
2021 free(repl);
2022 free(newcounters);
2023 return 0;
2024 }
2025
2026 /* Put counters back. */
2027 strcpy(newcounters->name, (*handle)->info.name);
Harald Welteaae69be2004-08-29 23:32:14 +00002028 newcounters->num_counters = new_number;
Marc Bouchere6869a82000-03-20 06:03:29 +00002029
Harald Welteaae69be2004-08-29 23:32:14 +00002030 list_for_each_entry(c, &(*handle)->chains, list) {
2031 struct rule_head *r;
Marc Bouchere6869a82000-03-20 06:03:29 +00002032
Harald Welteaae69be2004-08-29 23:32:14 +00002033 /* Builtin chains have their own counters */
2034 if (iptcc_is_builtin(c)) {
2035 DEBUGP("counter for chain-index %u: ", c->foot_index);
2036 switch(c->counter_map.maptype) {
2037 case COUNTER_MAP_NOMAP:
2038 counters_nomap(newcounters, c->foot_index);
2039 break;
2040 case COUNTER_MAP_NORMAL_MAP:
2041 counters_normal_map(newcounters, repl,
2042 c->foot_index,
2043 c->counter_map.mappos);
2044 break;
2045 case COUNTER_MAP_ZEROED:
2046 counters_map_zeroed(newcounters, repl,
2047 c->foot_index,
2048 c->counter_map.mappos,
2049 &c->counters);
2050 break;
2051 case COUNTER_MAP_SET:
2052 counters_map_set(newcounters, c->foot_index,
2053 &c->counters);
2054 break;
2055 }
2056 }
Harald Welte1cef74d2001-01-05 15:22:59 +00002057
Harald Welteaae69be2004-08-29 23:32:14 +00002058 list_for_each_entry(r, &c->rules, list) {
2059 DEBUGP("counter for index %u: ", r->index);
2060 switch (r->counter_map.maptype) {
2061 case COUNTER_MAP_NOMAP:
2062 counters_nomap(newcounters, r->index);
2063 break;
Harald Welte1cef74d2001-01-05 15:22:59 +00002064
Harald Welteaae69be2004-08-29 23:32:14 +00002065 case COUNTER_MAP_NORMAL_MAP:
2066 counters_normal_map(newcounters, repl,
2067 r->index,
2068 r->counter_map.mappos);
2069 break;
Harald Welte1cef74d2001-01-05 15:22:59 +00002070
Harald Welteaae69be2004-08-29 23:32:14 +00002071 case COUNTER_MAP_ZEROED:
2072 counters_map_zeroed(newcounters, repl,
2073 r->index,
2074 r->counter_map.mappos,
2075 &r->entry->counters);
2076 break;
2077
2078 case COUNTER_MAP_SET:
2079 counters_map_set(newcounters, r->index,
2080 &r->entry->counters);
2081 break;
2082 }
Marc Bouchere6869a82000-03-20 06:03:29 +00002083 }
2084 }
Rusty Russell62527ce2000-09-04 09:45:54 +00002085
Harald Welteaae69be2004-08-29 23:32:14 +00002086
Rusty Russell62527ce2000-09-04 09:45:54 +00002087#ifdef KERNEL_64_USERSPACE_32
2088 {
2089 /* Kernel will think that pointer should be 64-bits, and get
2090 padding. So we accomodate here (assumption: alignment of
2091 `counters' is on 64-bit boundary). */
2092 u_int64_t *kernptr = (u_int64_t *)&newcounters->counters;
2093 if ((unsigned long)&newcounters->counters % 8 != 0) {
2094 fprintf(stderr,
2095 "counters alignment incorrect! Mail rusty!\n");
2096 abort();
2097 }
2098 *kernptr = newcounters->counters;
Rusty Russell54c307e2000-09-04 06:47:28 +00002099 }
Rusty Russell62527ce2000-09-04 09:45:54 +00002100#endif /* KERNEL_64_USERSPACE_32 */
Marc Bouchere6869a82000-03-20 06:03:29 +00002101
Harald Welteaae69be2004-08-29 23:32:14 +00002102#ifdef IPTC_DEBUG2
2103 {
2104 int fd = open("/tmp/libiptc-so_set_add_counters.blob",
2105 O_CREAT|O_WRONLY);
2106 if (fd >= 0) {
2107 write(fd, newcounters, counterlen);
2108 close(fd);
2109 }
2110 }
2111#endif
2112
Rusty Russell79dee072000-05-02 16:45:16 +00002113 if (setsockopt(sockfd, TC_IPPROTO, SO_SET_ADD_COUNTERS,
2114 newcounters, counterlen) < 0) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002115 free(repl->counters);
2116 free(repl);
2117 free(newcounters);
2118 return 0;
2119 }
2120
2121 free(repl->counters);
2122 free(repl);
2123 free(newcounters);
2124
2125 finished:
Martin Josefsson841e4ae2003-05-02 15:30:11 +00002126 TC_FREE(handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00002127 return 1;
2128}
2129
2130/* Get raw socket. */
2131int
Rusty Russell79dee072000-05-02 16:45:16 +00002132TC_GET_RAW_SOCKET()
Marc Bouchere6869a82000-03-20 06:03:29 +00002133{
2134 return sockfd;
2135}
2136
2137/* Translates errno numbers into more human-readable form than strerror. */
2138const char *
Rusty Russell79dee072000-05-02 16:45:16 +00002139TC_STRERROR(int err)
Marc Bouchere6869a82000-03-20 06:03:29 +00002140{
2141 unsigned int i;
2142 struct table_struct {
2143 void *fn;
2144 int err;
2145 const char *message;
2146 } table [] =
Harald Welte4ccfa632001-07-30 15:12:43 +00002147 { { TC_INIT, EPERM, "Permission denied (you must be root)" },
Rusty Russell79dee072000-05-02 16:45:16 +00002148 { TC_INIT, EINVAL, "Module is wrong version" },
Harald Welte4ccfa632001-07-30 15:12:43 +00002149 { TC_INIT, ENOENT,
2150 "Table does not exist (do you need to insmod?)" },
Rusty Russell79dee072000-05-02 16:45:16 +00002151 { TC_DELETE_CHAIN, ENOTEMPTY, "Chain is not empty" },
2152 { TC_DELETE_CHAIN, EINVAL, "Can't delete built-in chain" },
2153 { TC_DELETE_CHAIN, EMLINK,
Marc Bouchere6869a82000-03-20 06:03:29 +00002154 "Can't delete chain with references left" },
Rusty Russell79dee072000-05-02 16:45:16 +00002155 { TC_CREATE_CHAIN, EEXIST, "Chain already exists" },
2156 { TC_INSERT_ENTRY, E2BIG, "Index of insertion too big" },
2157 { TC_REPLACE_ENTRY, E2BIG, "Index of replacement too big" },
2158 { TC_DELETE_NUM_ENTRY, E2BIG, "Index of deletion too big" },
Harald Welte1cef74d2001-01-05 15:22:59 +00002159 { TC_READ_COUNTER, E2BIG, "Index of counter too big" },
2160 { TC_ZERO_COUNTER, E2BIG, "Index of counter too big" },
Rusty Russell79dee072000-05-02 16:45:16 +00002161 { TC_INSERT_ENTRY, ELOOP, "Loop found in table" },
2162 { TC_INSERT_ENTRY, EINVAL, "Target problem" },
Marc Bouchere6869a82000-03-20 06:03:29 +00002163 /* EINVAL for CHECK probably means bad interface. */
Rusty Russell79dee072000-05-02 16:45:16 +00002164 { TC_CHECK_PACKET, EINVAL,
Marc Boucherc8264992000-04-22 22:34:44 +00002165 "Bad arguments (does that interface exist?)" },
Harald Welte4ccfa632001-07-30 15:12:43 +00002166 { TC_CHECK_PACKET, ENOSYS,
2167 "Checking will most likely never get implemented" },
Marc Bouchere6869a82000-03-20 06:03:29 +00002168 /* ENOENT for DELETE probably means no matching rule */
Rusty Russell79dee072000-05-02 16:45:16 +00002169 { TC_DELETE_ENTRY, ENOENT,
Marc Boucherc8264992000-04-22 22:34:44 +00002170 "Bad rule (does a matching rule exist in that chain?)" },
Rusty Russell79dee072000-05-02 16:45:16 +00002171 { TC_SET_POLICY, ENOENT,
Marc Boucherc8264992000-04-22 22:34:44 +00002172 "Bad built-in chain name" },
Rusty Russell79dee072000-05-02 16:45:16 +00002173 { TC_SET_POLICY, EINVAL,
Marc Boucherc8264992000-04-22 22:34:44 +00002174 "Bad policy name" },
Harald Welte4ccfa632001-07-30 15:12:43 +00002175
2176 { NULL, 0, "Incompatible with this kernel" },
2177 { NULL, ENOPROTOOPT, "iptables who? (do you need to insmod?)" },
2178 { NULL, ENOSYS, "Will be implemented real soon. I promise ;)" },
2179 { NULL, ENOMEM, "Memory allocation problem" },
2180 { NULL, ENOENT, "No chain/target/match by that name" },
Marc Bouchere6869a82000-03-20 06:03:29 +00002181 };
2182
2183 for (i = 0; i < sizeof(table)/sizeof(struct table_struct); i++) {
2184 if ((!table[i].fn || table[i].fn == iptc_fn)
2185 && table[i].err == err)
2186 return table[i].message;
2187 }
2188
2189 return strerror(err);
2190}