blob: bf32617556df0ebf66360b4c1fc737944a9053ca [file] [log] [blame]
Harald Welteaae69be2004-08-29 23:32:14 +00001/* Library which manipulates firewall rules. Version $Revision: 1.39 $ */
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);
385
386 return 1;
387 }
388 return 0;
389}
390
391/* Another ugly helper function split out of cache_add_entry to make it less
392 * spaghetti code */
393static void __iptcc_p_add_chain(TC_HANDLE_T h, struct chain_head *c,
394 unsigned int offset, unsigned int *num)
395{
396 __iptcc_p_del_policy(h, *num);
397
398 c->head_offset = offset;
399 c->index = *num;
400
401 list_add_tail(&c->list, &h->chains);
402 h->chain_iterator_cur = c;
403}
404
405/* main parser function: add an entry from the blob to the cache */
406static int cache_add_entry(STRUCT_ENTRY *e,
407 TC_HANDLE_T h,
408 STRUCT_ENTRY **prev,
409 unsigned int *num)
410{
411 unsigned int builtin;
412 unsigned int offset = (char *)e - (char *)h->entries->entrytable;
413
414 DEBUGP("entering...");
415
416 /* Last entry ("policy rule"). End it.*/
417 if (iptcb_entry2offset(h,e) + e->next_offset == h->entries->size) {
418 /* This is the ERROR node at the end of the chain */
419 DEBUGP_C("%u:%u: end of table:\n", *num, offset);
420
421 __iptcc_p_del_policy(h, *num);
422
423 h->chain_iterator_cur = NULL;
424 goto out_inc;
425 }
426
427 /* We know this is the start of a new chain if it's an ERROR
428 * target, or a hook entry point */
429
430 if (strcmp(GET_TARGET(e)->u.user.name, ERROR_TARGET) == 0) {
431 struct chain_head *c =
432 iptcc_alloc_chain_head((const char *)GET_TARGET(e)->data, 0);
433 DEBUGP_C("%u:%u:new userdefined chain %s: %p\n", *num, offset,
434 (char *)c->name, c);
435 if (!c) {
436 errno = -ENOMEM;
437 return -1;
438 }
439
440 __iptcc_p_add_chain(h, c, offset, num);
441
442 } else if ((builtin = iptcb_ent_is_hook_entry(e, h)) != 0) {
443 struct chain_head *c =
444 iptcc_alloc_chain_head((char *)hooknames[builtin-1],
445 builtin);
446 DEBUGP_C("%u:%u new builtin chain: %p (rules=%p)\n",
447 *num, offset, c, &c->rules);
448 if (!c) {
449 errno = -ENOMEM;
450 return -1;
451 }
452
453 c->hooknum = builtin;
454
455 __iptcc_p_add_chain(h, c, offset, num);
456
457 /* FIXME: this is ugly. */
458 goto new_rule;
459 } else {
460 /* has to be normal rule */
461 struct rule_head *r;
462new_rule:
463
464 if (!(r = iptcc_alloc_rule(h->chain_iterator_cur,
465 e->next_offset))) {
466 errno = ENOMEM;
467 return -1;
468 }
469 DEBUGP_C("%u:%u normal rule: %p: ", *num, offset, r);
470
471 r->index = *num;
472 r->offset = offset;
473 memcpy(r->entry, e, e->next_offset);
474 r->counter_map.maptype = COUNTER_MAP_NORMAL_MAP;
475 r->counter_map.mappos = r->index;
476
477 /* handling of jumps, etc. */
478 if (!strcmp(GET_TARGET(e)->u.user.name, STANDARD_TARGET)) {
479 STRUCT_STANDARD_TARGET *t;
480
481 t = (STRUCT_STANDARD_TARGET *)GET_TARGET(e);
482 if (t->target.u.target_size
483 != ALIGN(sizeof(STRUCT_STANDARD_TARGET))) {
484 errno = EINVAL;
485 return -1;
486 }
487
488 if (t->verdict < 0) {
489 DEBUGP_C("standard, verdict=%d\n", t->verdict);
490 r->type = IPTCC_R_STANDARD;
491 } else if (t->verdict == r->offset+e->next_offset) {
492 DEBUGP_C("fallthrough\n");
493 r->type = IPTCC_R_FALLTHROUGH;
494 } else {
495 DEBUGP_C("jump, target=%u\n", t->verdict);
496 r->type = IPTCC_R_JUMP;
497 /* Jump target fixup has to be deferred
498 * until second pass, since we migh not
499 * yet have parsed the target */
500 }
501 }
502
503 list_add_tail(&r->list, &h->chain_iterator_cur->rules);
504 }
505out_inc:
506 (*num)++;
507 return 0;
508}
509
510
511/* parse an iptables blob into it's pieces */
512static int parse_table(TC_HANDLE_T h)
513{
514 STRUCT_ENTRY *prev;
515 unsigned int num = 0;
516 struct chain_head *c;
517
518 /* First pass: over ruleset blob */
519 ENTRY_ITERATE(h->entries->entrytable, h->entries->size,
520 cache_add_entry, h, &prev, &num);
521
522 /* Second pass: fixup parsed data from first pass */
523 list_for_each_entry(c, &h->chains, list) {
524 struct rule_head *r;
525 list_for_each_entry(r, &c->rules, list) {
526 struct chain_head *c;
527 STRUCT_STANDARD_TARGET *t;
528
529 if (r->type != IPTCC_R_JUMP)
530 continue;
531
532 t = (STRUCT_STANDARD_TARGET *)GET_TARGET(r->entry);
533 c = iptcc_find_chain_by_offset(h, t->verdict);
534 if (!c)
535 return -1;
536 r->jump = c;
537 c->references++;
538 }
539 }
540
541 /* FIXME: sort chains */
542
543 return 1;
544}
545
546
547/**********************************************************************
548 * RULESET COMPILATION (cache -> blob)
549 **********************************************************************/
550
551/* Convenience structures */
552struct iptcb_chain_start{
553 STRUCT_ENTRY e;
554 struct ipt_error_target name;
555};
556#define IPTCB_CHAIN_START_SIZE (sizeof(STRUCT_ENTRY) + \
557 ALIGN(sizeof(struct ipt_error_target)))
558
559struct iptcb_chain_foot {
560 STRUCT_ENTRY e;
561 STRUCT_STANDARD_TARGET target;
562};
563#define IPTCB_CHAIN_FOOT_SIZE (sizeof(STRUCT_ENTRY) + \
564 ALIGN(sizeof(STRUCT_STANDARD_TARGET)))
565
566struct iptcb_chain_error {
567 STRUCT_ENTRY entry;
568 struct ipt_error_target target;
569};
570#define IPTCB_CHAIN_ERROR_SIZE (sizeof(STRUCT_ENTRY) + \
571 ALIGN(sizeof(struct ipt_error_target)))
572
573
574
575/* compile rule from cache into blob */
576static inline int iptcc_compile_rule (TC_HANDLE_T h, STRUCT_REPLACE *repl, struct rule_head *r)
577{
578 /* handle jumps */
579 if (r->type == IPTCC_R_JUMP) {
580 STRUCT_STANDARD_TARGET *t;
581 t = (STRUCT_STANDARD_TARGET *)GET_TARGET(r->entry);
582 /* memset for memcmp convenience on delete/replace */
583 memset(t->target.u.user.name, 0, FUNCTION_MAXNAMELEN);
584 strcpy(t->target.u.user.name, STANDARD_TARGET);
585 /* Jumps can only happen to builtin chains, so we
586 * can safely assume that they always have a header */
587 t->verdict = r->jump->head_offset + IPTCB_CHAIN_START_SIZE;
588 } else if (r->type == IPTCC_R_FALLTHROUGH) {
589 STRUCT_STANDARD_TARGET *t;
590 t = (STRUCT_STANDARD_TARGET *)GET_TARGET(r->entry);
591 t->verdict = r->offset + r->size;
592 }
593
594 /* copy entry from cache to blob */
595 memcpy((char *)repl->entries+r->offset, r->entry, r->size);
596
597 return 1;
598}
599
600/* compile chain from cache into blob */
601static int iptcc_compile_chain(TC_HANDLE_T h, STRUCT_REPLACE *repl, struct chain_head *c)
602{
603 int ret;
604 struct rule_head *r;
605 struct iptcb_chain_start *head;
606 struct iptcb_chain_foot *foot;
607
608 /* only user-defined chains have heaer */
609 if (!iptcc_is_builtin(c)) {
610 /* put chain header in place */
611 head = (void *)repl->entries + c->head_offset;
612 head->e.target_offset = sizeof(STRUCT_ENTRY);
613 head->e.next_offset = IPTCB_CHAIN_START_SIZE;
614 strcpy(head->name.t.u.user.name, ERROR_TARGET);
615 head->name.t.u.target_size =
616 ALIGN(sizeof(struct ipt_error_target));
617 strcpy(head->name.error, c->name);
618 } else {
619 repl->hook_entry[c->hooknum-1] = c->head_offset;
620 repl->underflow[c->hooknum-1] = c->foot_offset;
621 }
622
623 /* iterate over rules */
624 list_for_each_entry(r, &c->rules, list) {
625 ret = iptcc_compile_rule(h, repl, r);
626 if (ret < 0)
627 return ret;
628 }
629
630 /* put chain footer in place */
631 foot = (void *)repl->entries + c->foot_offset;
632 foot->e.target_offset = sizeof(STRUCT_ENTRY);
633 foot->e.next_offset = IPTCB_CHAIN_FOOT_SIZE;
634 strcpy(foot->target.target.u.user.name, STANDARD_TARGET);
635 foot->target.target.u.target_size =
636 ALIGN(sizeof(STRUCT_STANDARD_TARGET));
637 /* builtin targets have verdict, others return */
638 if (iptcc_is_builtin(c))
639 foot->target.verdict = c->verdict;
640 else
641 foot->target.verdict = RETURN;
642 /* set policy-counters */
643 memcpy(&foot->e.counters, &c->counters, sizeof(STRUCT_COUNTERS));
644
645 return 0;
646}
647
648/* calculate offset and number for every rule in the cache */
649static int iptcc_compile_chain_offsets(TC_HANDLE_T h, struct chain_head *c,
650 int *offset, int *num)
651{
652 struct rule_head *r;
653
654 c->head_offset = *offset;
655 DEBUGP("%s: chain_head %u, offset=%u\n", c->name, *num, *offset);
656
657 if (!iptcc_is_builtin(c)) {
658 /* Chain has header */
659 *offset += sizeof(STRUCT_ENTRY)
660 + ALIGN(sizeof(struct ipt_error_target));
661 (*num)++;
662 }
663
664 list_for_each_entry(r, &c->rules, list) {
665 DEBUGP("rule %u, offset=%u, index=%u\n", *num, *offset, *num);
666 r->offset = *offset;
667 r->index = *num;
668 *offset += r->size;
669 (*num)++;
670 }
671
672 DEBUGP("%s; chain_foot %u, offset=%u, index=%u\n", c->name, *num,
673 *offset, *num);
674 c->foot_offset = *offset;
675 c->foot_index = *num;
676 *offset += sizeof(STRUCT_ENTRY)
677 + ALIGN(sizeof(STRUCT_STANDARD_TARGET));
678 (*num)++;
679
680 return 1;
681}
682
683/* put the pieces back together again */
684static int iptcc_compile_table_prep(TC_HANDLE_T h, unsigned int *size)
685{
686 struct chain_head *c;
687 unsigned int offset = 0, num = 0;
688 int ret = 0;
689
690 /* First pass: calculate offset for every rule */
691 list_for_each_entry(c, &h->chains, list) {
692 ret = iptcc_compile_chain_offsets(h, c, &offset, &num);
693 if (ret < 0)
694 return ret;
695 }
696
697 /* Append one error rule at end of chain */
698 num++;
699 offset += sizeof(STRUCT_ENTRY)
700 + ALIGN(sizeof(struct ipt_error_target));
701
702 /* ruleset size is now in offset */
703 *size = offset;
704 return num;
705}
706
707static int iptcc_compile_table(TC_HANDLE_T h, STRUCT_REPLACE *repl)
708{
709 struct chain_head *c;
710 struct iptcb_chain_error *error;
711
712 /* Second pass: copy from cache to offsets, fill in jumps */
713 list_for_each_entry(c, &h->chains, list) {
714 int ret = iptcc_compile_chain(h, repl, c);
715 if (ret < 0)
716 return ret;
717 }
718
719 /* Append error rule at end of chain */
720 error = (void *)repl->entries + repl->size - IPTCB_CHAIN_ERROR_SIZE;
721 error->entry.target_offset = sizeof(STRUCT_ENTRY);
722 error->entry.next_offset = IPTCB_CHAIN_ERROR_SIZE;
723 error->target.t.u.user.target_size =
724 ALIGN(sizeof(struct ipt_error_target));
725 strcpy((char *)&error->target.t.u.user.name, ERROR_TARGET);
726 strcpy((char *)&error->target.error, "ERROR");
727
728 return 1;
729}
730
731/**********************************************************************
732 * EXTERNAL API (operates on cache only)
733 **********************************************************************/
Marc Bouchere6869a82000-03-20 06:03:29 +0000734
735/* Allocate handle of given size */
Rusty Russell79dee072000-05-02 16:45:16 +0000736static TC_HANDLE_T
Harald Welte0113fe72004-01-06 19:04:02 +0000737alloc_handle(const char *tablename, unsigned int size, unsigned int num_rules)
Marc Bouchere6869a82000-03-20 06:03:29 +0000738{
739 size_t len;
Rusty Russell79dee072000-05-02 16:45:16 +0000740 TC_HANDLE_T h;
Marc Bouchere6869a82000-03-20 06:03:29 +0000741
Harald Welteaae69be2004-08-29 23:32:14 +0000742 len = sizeof(STRUCT_TC_HANDLE) + size;
Marc Bouchere6869a82000-03-20 06:03:29 +0000743
Harald Welteaae69be2004-08-29 23:32:14 +0000744 h = malloc(sizeof(STRUCT_TC_HANDLE));
745 if (!h) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000746 errno = ENOMEM;
747 return NULL;
748 }
Harald Welteaae69be2004-08-29 23:32:14 +0000749 memset(h, 0, sizeof(*h));
750 INIT_LIST_HEAD(&h->chains);
Marc Bouchere6869a82000-03-20 06:03:29 +0000751 strcpy(h->info.name, tablename);
Harald Welteaae69be2004-08-29 23:32:14 +0000752
753 h->entries = malloc(size);
754 if (!h->entries)
755 goto out_free_handle;
756
757 strcpy(h->entries->name, tablename);
Marc Bouchere6869a82000-03-20 06:03:29 +0000758
759 return h;
Harald Welteaae69be2004-08-29 23:32:14 +0000760
761out_free_handle:
762 free(h);
763
764 return NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +0000765}
766
Harald Welteaae69be2004-08-29 23:32:14 +0000767
Rusty Russell79dee072000-05-02 16:45:16 +0000768TC_HANDLE_T
769TC_INIT(const char *tablename)
Marc Bouchere6869a82000-03-20 06:03:29 +0000770{
Rusty Russell79dee072000-05-02 16:45:16 +0000771 TC_HANDLE_T h;
772 STRUCT_GETINFO info;
Marc Bouchere6869a82000-03-20 06:03:29 +0000773 int tmp;
774 socklen_t s;
775
Rusty Russell79dee072000-05-02 16:45:16 +0000776 iptc_fn = TC_INIT;
Marc Bouchere6869a82000-03-20 06:03:29 +0000777
Martin Josefssone560fd62003-06-13 16:56:51 +0000778 if (sockfd != -1) {
Harald Welte366454b2002-01-07 13:46:50 +0000779 close(sockfd);
Martin Josefssone560fd62003-06-13 16:56:51 +0000780 sockfd = -1;
781 }
Harald Welte366454b2002-01-07 13:46:50 +0000782
Martin Josefsson841e4ae2003-05-02 15:30:11 +0000783 if (strlen(tablename) >= TABLE_MAXNAMELEN) {
784 errno = EINVAL;
785 return NULL;
786 }
787
Rusty Russell79dee072000-05-02 16:45:16 +0000788 sockfd = socket(TC_AF, SOCK_RAW, IPPROTO_RAW);
Marc Bouchere6869a82000-03-20 06:03:29 +0000789 if (sockfd < 0)
790 return NULL;
791
792 s = sizeof(info);
Martin Josefsson841e4ae2003-05-02 15:30:11 +0000793
Marc Bouchere6869a82000-03-20 06:03:29 +0000794 strcpy(info.name, tablename);
Rusty Russell79dee072000-05-02 16:45:16 +0000795 if (getsockopt(sockfd, TC_IPPROTO, SO_GET_INFO, &info, &s) < 0)
Marc Bouchere6869a82000-03-20 06:03:29 +0000796 return NULL;
797
Harald Welteaae69be2004-08-29 23:32:14 +0000798 DEBUGP("valid_hooks=0x%08x, num_entries=%u, size=%u\n",
799 info.valid_hooks, info.num_entries, info.size);
800
Harald Welte0113fe72004-01-06 19:04:02 +0000801 if ((h = alloc_handle(info.name, info.size, info.num_entries))
Martin Josefsson841e4ae2003-05-02 15:30:11 +0000802 == NULL) {
803 close(sockfd);
Martin Josefssone560fd62003-06-13 16:56:51 +0000804 sockfd = -1;
Marc Bouchere6869a82000-03-20 06:03:29 +0000805 return NULL;
Martin Josefsson841e4ae2003-05-02 15:30:11 +0000806 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000807
Marc Bouchere6869a82000-03-20 06:03:29 +0000808 /* Initialize current state */
809 h->info = info;
Harald Welte0113fe72004-01-06 19:04:02 +0000810
Harald Welteaae69be2004-08-29 23:32:14 +0000811 h->entries->size = h->info.size;
Marc Bouchere6869a82000-03-20 06:03:29 +0000812
Rusty Russell79dee072000-05-02 16:45:16 +0000813 tmp = sizeof(STRUCT_GET_ENTRIES) + h->info.size;
Marc Bouchere6869a82000-03-20 06:03:29 +0000814
Harald Welteaae69be2004-08-29 23:32:14 +0000815 if (getsockopt(sockfd, TC_IPPROTO, SO_GET_ENTRIES, h->entries,
816 &tmp) < 0)
817 goto error;
818
819#ifdef IPTC_DEBUG2
820 {
821 int fd = open("/tmp/libiptc-so_get_entries.blob",
822 O_CREAT|O_WRONLY);
823 if (fd >= 0) {
824 write(fd, h->entries, tmp);
825 close(fd);
826 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000827 }
Harald Welteaae69be2004-08-29 23:32:14 +0000828#endif
829
830 if (parse_table(h) < 0)
831 goto error;
Rusty Russell7e53bf92000-03-20 07:03:28 +0000832
Marc Bouchere6869a82000-03-20 06:03:29 +0000833 CHECK(h);
834 return h;
Harald Welteaae69be2004-08-29 23:32:14 +0000835error:
836 TC_FREE(&h);
837 return NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +0000838}
839
Martin Josefsson841e4ae2003-05-02 15:30:11 +0000840void
841TC_FREE(TC_HANDLE_T *h)
842{
Harald Welteaae69be2004-08-29 23:32:14 +0000843 struct chain_head *c, *tmp;
844
Martin Josefsson841e4ae2003-05-02 15:30:11 +0000845 close(sockfd);
Martin Josefssone560fd62003-06-13 16:56:51 +0000846 sockfd = -1;
Harald Welteaae69be2004-08-29 23:32:14 +0000847
848 list_for_each_entry_safe(c, tmp, &(*h)->chains, list) {
849 struct rule_head *r, *rtmp;
850
851 list_for_each_entry_safe(r, rtmp, &c->rules, list) {
852 free(r);
853 }
854
855 free(c);
856 }
857
858 free((*h)->entries);
Martin Josefsson841e4ae2003-05-02 15:30:11 +0000859 free(*h);
Harald Welteaae69be2004-08-29 23:32:14 +0000860
Martin Josefsson841e4ae2003-05-02 15:30:11 +0000861 *h = NULL;
862}
863
Marc Bouchere6869a82000-03-20 06:03:29 +0000864static inline int
Rusty Russell79dee072000-05-02 16:45:16 +0000865print_match(const STRUCT_ENTRY_MATCH *m)
Marc Bouchere6869a82000-03-20 06:03:29 +0000866{
Rusty Russell228e98d2000-04-27 10:28:06 +0000867 printf("Match name: `%s'\n", m->u.user.name);
Marc Bouchere6869a82000-03-20 06:03:29 +0000868 return 0;
869}
870
Rusty Russell79dee072000-05-02 16:45:16 +0000871static int dump_entry(STRUCT_ENTRY *e, const TC_HANDLE_T handle);
872
Marc Bouchere6869a82000-03-20 06:03:29 +0000873void
Rusty Russell79dee072000-05-02 16:45:16 +0000874TC_DUMP_ENTRIES(const TC_HANDLE_T handle)
Marc Bouchere6869a82000-03-20 06:03:29 +0000875{
876 CHECK(handle);
Harald Welteaae69be2004-08-29 23:32:14 +0000877#if 0
Marc Bouchere6869a82000-03-20 06:03:29 +0000878 printf("libiptc v%s. %u entries, %u bytes.\n",
Harald Welte80fe35d2002-05-29 13:08:15 +0000879 IPTABLES_VERSION,
Harald Welteaae69be2004-08-29 23:32:14 +0000880 handle->new_number, handle->entries->size);
Marc Bouchere6869a82000-03-20 06:03:29 +0000881 printf("Table `%s'\n", handle->info.name);
882 printf("Hooks: pre/in/fwd/out/post = %u/%u/%u/%u/%u\n",
Rusty Russell67088e72000-05-10 01:18:57 +0000883 handle->info.hook_entry[HOOK_PRE_ROUTING],
884 handle->info.hook_entry[HOOK_LOCAL_IN],
885 handle->info.hook_entry[HOOK_FORWARD],
886 handle->info.hook_entry[HOOK_LOCAL_OUT],
887 handle->info.hook_entry[HOOK_POST_ROUTING]);
Marc Bouchere6869a82000-03-20 06:03:29 +0000888 printf("Underflows: pre/in/fwd/out/post = %u/%u/%u/%u/%u\n",
Rusty Russell67088e72000-05-10 01:18:57 +0000889 handle->info.underflow[HOOK_PRE_ROUTING],
890 handle->info.underflow[HOOK_LOCAL_IN],
891 handle->info.underflow[HOOK_FORWARD],
892 handle->info.underflow[HOOK_LOCAL_OUT],
893 handle->info.underflow[HOOK_POST_ROUTING]);
Marc Bouchere6869a82000-03-20 06:03:29 +0000894
Harald Welteaae69be2004-08-29 23:32:14 +0000895 ENTRY_ITERATE(handle->entries->entrytable, handle->entries->size,
Rusty Russell79dee072000-05-02 16:45:16 +0000896 dump_entry, handle);
Harald Welteaae69be2004-08-29 23:32:14 +0000897#endif
Harald Welte0113fe72004-01-06 19:04:02 +0000898}
Rusty Russell30fd6e52000-04-23 09:16:06 +0000899
Marc Bouchere6869a82000-03-20 06:03:29 +0000900/* Does this chain exist? */
Rusty Russell79dee072000-05-02 16:45:16 +0000901int TC_IS_CHAIN(const char *chain, const TC_HANDLE_T handle)
Marc Bouchere6869a82000-03-20 06:03:29 +0000902{
Harald Welteaae69be2004-08-29 23:32:14 +0000903 return iptcc_find_label(chain, handle) != NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +0000904}
905
Harald Welteaae69be2004-08-29 23:32:14 +0000906#if 0
Marc Bouchere6869a82000-03-20 06:03:29 +0000907/* Returns the position of the final (ie. unconditional) element. */
908static unsigned int
Rusty Russell79dee072000-05-02 16:45:16 +0000909get_chain_end(const TC_HANDLE_T handle, unsigned int start)
Marc Bouchere6869a82000-03-20 06:03:29 +0000910{
911 unsigned int last_off, off;
Rusty Russell79dee072000-05-02 16:45:16 +0000912 STRUCT_ENTRY *e;
Marc Bouchere6869a82000-03-20 06:03:29 +0000913
914 last_off = start;
915 e = get_entry(handle, start);
916
917 /* Terminate when we meet a error label or a hook entry. */
918 for (off = start + e->next_offset;
919 off < handle->entries.size;
920 last_off = off, off += e->next_offset) {
Rusty Russell79dee072000-05-02 16:45:16 +0000921 STRUCT_ENTRY_TARGET *t;
Marc Bouchere6869a82000-03-20 06:03:29 +0000922 unsigned int i;
923
924 e = get_entry(handle, off);
925
926 /* We hit an entry point. */
Rusty Russell79dee072000-05-02 16:45:16 +0000927 for (i = 0; i < NUMHOOKS; i++) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000928 if ((handle->info.valid_hooks & (1 << i))
929 && off == handle->info.hook_entry[i])
930 return last_off;
931 }
932
933 /* We hit a user chain label */
Rusty Russell79dee072000-05-02 16:45:16 +0000934 t = GET_TARGET(e);
Rusty Russell67088e72000-05-10 01:18:57 +0000935 if (strcmp(t->u.user.name, ERROR_TARGET) == 0)
Marc Bouchere6869a82000-03-20 06:03:29 +0000936 return last_off;
937 }
938 /* SHOULD NEVER HAPPEN */
939 fprintf(stderr, "ERROR: Off end (%u) of chain from %u!\n",
940 handle->entries.size, off);
941 abort();
942}
Harald Welteaae69be2004-08-29 23:32:14 +0000943#endif
944
945static void iptcc_chain_iterator_advance(TC_HANDLE_T handle)
946{
947 struct chain_head *c = handle->chain_iterator_cur;
948
949 if (c->list.next == &handle->chains)
950 handle->chain_iterator_cur = NULL;
951 else
952 handle->chain_iterator_cur =
953 list_entry(c->list.next, struct chain_head, list);
954}
Marc Bouchere6869a82000-03-20 06:03:29 +0000955
Rusty Russell30fd6e52000-04-23 09:16:06 +0000956/* Iterator functions to run through the chains. */
Marc Bouchere6869a82000-03-20 06:03:29 +0000957const char *
Philip Blundell8c700902000-05-15 02:17:52 +0000958TC_FIRST_CHAIN(TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +0000959{
Harald Welteaae69be2004-08-29 23:32:14 +0000960 struct chain_head *c = list_entry((*handle)->chains.next,
961 struct chain_head, list);
962
963 iptc_fn = TC_FIRST_CHAIN;
964
965
966 if (list_empty(&(*handle)->chains)) {
967 DEBUGP(": no chains\n");
Harald Welte0113fe72004-01-06 19:04:02 +0000968 return NULL;
Harald Welteaae69be2004-08-29 23:32:14 +0000969 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000970
Harald Welteaae69be2004-08-29 23:32:14 +0000971 (*handle)->chain_iterator_cur = c;
972 iptcc_chain_iterator_advance(*handle);
Harald Welte0113fe72004-01-06 19:04:02 +0000973
Harald Welteaae69be2004-08-29 23:32:14 +0000974 DEBUGP(": returning `%s'\n", c->name);
975 return c->name;
Marc Bouchere6869a82000-03-20 06:03:29 +0000976}
977
Rusty Russell30fd6e52000-04-23 09:16:06 +0000978/* Iterator functions to run through the chains. Returns NULL at end. */
979const char *
Rusty Russell79dee072000-05-02 16:45:16 +0000980TC_NEXT_CHAIN(TC_HANDLE_T *handle)
Rusty Russell30fd6e52000-04-23 09:16:06 +0000981{
Harald Welteaae69be2004-08-29 23:32:14 +0000982 struct chain_head *c = (*handle)->chain_iterator_cur;
Rusty Russell30fd6e52000-04-23 09:16:06 +0000983
Harald Welteaae69be2004-08-29 23:32:14 +0000984 iptc_fn = TC_NEXT_CHAIN;
985
986 if (!c) {
987 DEBUGP(": no more chains\n");
Rusty Russell30fd6e52000-04-23 09:16:06 +0000988 return NULL;
Harald Welteaae69be2004-08-29 23:32:14 +0000989 }
Rusty Russell30fd6e52000-04-23 09:16:06 +0000990
Harald Welteaae69be2004-08-29 23:32:14 +0000991 iptcc_chain_iterator_advance(*handle);
992
993 DEBUGP(": returning `%s'\n", c->name);
994 return c->name;
Rusty Russell30fd6e52000-04-23 09:16:06 +0000995}
996
997/* Get first rule in the given chain: NULL for empty chain. */
Rusty Russell79dee072000-05-02 16:45:16 +0000998const STRUCT_ENTRY *
Philip Blundell8c700902000-05-15 02:17:52 +0000999TC_FIRST_RULE(const char *chain, TC_HANDLE_T *handle)
Rusty Russell30fd6e52000-04-23 09:16:06 +00001000{
Harald Welteaae69be2004-08-29 23:32:14 +00001001 struct chain_head *c;
1002 struct rule_head *r;
Rusty Russell30fd6e52000-04-23 09:16:06 +00001003
Harald Welteaae69be2004-08-29 23:32:14 +00001004 iptc_fn = TC_FIRST_RULE;
1005
1006 DEBUGP("first rule(%s): ", chain);
1007
1008 c = iptcc_find_label(chain, *handle);
Rusty Russell30fd6e52000-04-23 09:16:06 +00001009 if (!c) {
1010 errno = ENOENT;
1011 return NULL;
1012 }
1013
1014 /* Empty chain: single return/policy rule */
Harald Welteaae69be2004-08-29 23:32:14 +00001015 if (list_empty(&c->rules)) {
1016 DEBUGP_C("no rules, returning NULL\n");
Rusty Russell30fd6e52000-04-23 09:16:06 +00001017 return NULL;
Harald Welteaae69be2004-08-29 23:32:14 +00001018 }
Rusty Russell30fd6e52000-04-23 09:16:06 +00001019
Harald Welteaae69be2004-08-29 23:32:14 +00001020 r = list_entry(c->rules.next, struct rule_head, list);
1021 (*handle)->rule_iterator_cur = r;
1022 DEBUGP_C("%p\n", r);
1023
1024 return r->entry;
Rusty Russell30fd6e52000-04-23 09:16:06 +00001025}
1026
1027/* Returns NULL when rules run out. */
Rusty Russell79dee072000-05-02 16:45:16 +00001028const STRUCT_ENTRY *
Philip Blundell8c700902000-05-15 02:17:52 +00001029TC_NEXT_RULE(const STRUCT_ENTRY *prev, TC_HANDLE_T *handle)
Rusty Russell30fd6e52000-04-23 09:16:06 +00001030{
Harald Welteaae69be2004-08-29 23:32:14 +00001031 struct rule_head *r;
Rusty Russell30fd6e52000-04-23 09:16:06 +00001032
Harald Welteaae69be2004-08-29 23:32:14 +00001033 DEBUGP("rule_iterator_cur=%p...", (*handle)->rule_iterator_cur);
1034
1035 if (!(*handle)->rule_iterator_cur) {
1036 DEBUGP_C("returning NULL\n");
1037 return NULL;
1038 }
1039
1040 r = list_entry((*handle)->rule_iterator_cur->list.next,
1041 struct rule_head, list);
1042
1043 iptc_fn = TC_NEXT_RULE;
1044
1045 DEBUGP_C("next=%p, head=%p...", &r->list,
1046 &(*handle)->rule_iterator_cur->chain->rules);
1047
1048 if (&r->list == &(*handle)->rule_iterator_cur->chain->rules) {
1049 (*handle)->rule_iterator_cur = NULL;
1050 DEBUGP_C("finished, returning NULL\n");
1051 return NULL;
1052 }
1053
1054 (*handle)->rule_iterator_cur = r;
1055
1056 /* NOTE: prev is without any influence ! */
1057 DEBUGP_C("returning rule %p\n", r);
1058 return r->entry;
Rusty Russell30fd6e52000-04-23 09:16:06 +00001059}
1060
Marc Bouchere6869a82000-03-20 06:03:29 +00001061/* How many rules in this chain? */
1062unsigned int
Rusty Russell79dee072000-05-02 16:45:16 +00001063TC_NUM_RULES(const char *chain, TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001064{
Harald Welteaae69be2004-08-29 23:32:14 +00001065 struct chain_head *c;
1066 iptc_fn = TC_NUM_RULES;
Marc Bouchere6869a82000-03-20 06:03:29 +00001067 CHECK(*handle);
Harald Welteaae69be2004-08-29 23:32:14 +00001068
1069 c = iptcc_find_label(chain, *handle);
1070 if (!c) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001071 errno = ENOENT;
1072 return (unsigned int)-1;
1073 }
Harald Welteaae69be2004-08-29 23:32:14 +00001074
1075 return c->num_rules;
Marc Bouchere6869a82000-03-20 06:03:29 +00001076}
1077
Rusty Russell79dee072000-05-02 16:45:16 +00001078const STRUCT_ENTRY *TC_GET_RULE(const char *chain,
1079 unsigned int n,
1080 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001081{
Harald Welteaae69be2004-08-29 23:32:14 +00001082 struct chain_head *c;
1083 struct rule_head *r;
1084
1085 iptc_fn = TC_GET_RULE;
Marc Bouchere6869a82000-03-20 06:03:29 +00001086
1087 CHECK(*handle);
Harald Welteaae69be2004-08-29 23:32:14 +00001088
1089 c = iptcc_find_label(chain, *handle);
1090 if (!c) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001091 errno = ENOENT;
1092 return NULL;
1093 }
1094
Harald Welteaae69be2004-08-29 23:32:14 +00001095 r = iptcc_get_rule_num(c, n);
1096 if (!r)
1097 return NULL;
1098 return r->entry;
Marc Bouchere6869a82000-03-20 06:03:29 +00001099}
1100
1101/* Returns a pointer to the target name of this position. */
Harald Welteaae69be2004-08-29 23:32:14 +00001102const char *standard_target_map(int verdict)
Marc Bouchere6869a82000-03-20 06:03:29 +00001103{
Harald Welteaae69be2004-08-29 23:32:14 +00001104 switch (verdict) {
1105 case RETURN:
1106 return LABEL_RETURN;
1107 break;
1108 case -NF_ACCEPT-1:
1109 return LABEL_ACCEPT;
1110 break;
1111 case -NF_DROP-1:
1112 return LABEL_DROP;
1113 break;
1114 case -NF_QUEUE-1:
1115 return LABEL_QUEUE;
1116 break;
1117 default:
1118 fprintf(stderr, "ERROR: %d not a valid target)\n",
1119 verdict);
1120 abort();
1121 break;
1122 }
1123 /* not reached */
1124 return NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +00001125}
1126
Harald Welteaae69be2004-08-29 23:32:14 +00001127/* Returns a pointer to the target name of this position. */
1128const char *TC_GET_TARGET(const STRUCT_ENTRY *ce,
1129 TC_HANDLE_T *handle)
1130{
1131 STRUCT_ENTRY *e = (STRUCT_ENTRY *)ce;
1132 struct rule_head *r = container_of(e, struct rule_head, entry);
1133
1134 iptc_fn = TC_GET_TARGET;
1135
1136 switch(r->type) {
1137 int spos;
1138 case IPTCC_R_FALLTHROUGH:
1139 return "";
1140 break;
1141 case IPTCC_R_JUMP:
1142 DEBUGP("r=%p, jump=%p, name=`%s'\n", r, r->jump, r->jump->name);
1143 return r->jump->name;
1144 break;
1145 case IPTCC_R_STANDARD:
1146 spos = *(int *)GET_TARGET(e)->data;
1147 DEBUGP("r=%p, spos=%d'\n", r, spos);
1148 return standard_target_map(spos);
1149 break;
1150 case IPTCC_R_MODULE:
1151 return GET_TARGET(e)->u.user.name;
1152 break;
1153 }
1154 return NULL;
1155}
Marc Bouchere6869a82000-03-20 06:03:29 +00001156/* Is this a built-in chain? Actually returns hook + 1. */
1157int
Rusty Russell79dee072000-05-02 16:45:16 +00001158TC_BUILTIN(const char *chain, const TC_HANDLE_T handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001159{
Harald Welteaae69be2004-08-29 23:32:14 +00001160 struct chain_head *c;
1161
1162 iptc_fn = TC_BUILTIN;
Marc Bouchere6869a82000-03-20 06:03:29 +00001163
Harald Welteaae69be2004-08-29 23:32:14 +00001164 c = iptcc_find_label(chain, handle);
1165 if (!c) {
1166 errno = ENOENT;
1167 return -1;
Marc Bouchere6869a82000-03-20 06:03:29 +00001168 }
Harald Welteaae69be2004-08-29 23:32:14 +00001169
1170 return iptcc_is_builtin(c);
Marc Bouchere6869a82000-03-20 06:03:29 +00001171}
1172
1173/* Get the policy of a given built-in chain */
1174const char *
Rusty Russell79dee072000-05-02 16:45:16 +00001175TC_GET_POLICY(const char *chain,
1176 STRUCT_COUNTERS *counters,
1177 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001178{
Harald Welteaae69be2004-08-29 23:32:14 +00001179 struct chain_head *c;
Marc Bouchere6869a82000-03-20 06:03:29 +00001180
Harald Welteaae69be2004-08-29 23:32:14 +00001181 iptc_fn = TC_GET_POLICY;
1182
1183 DEBUGP("called for chain %s\n", chain);
1184
1185 c = iptcc_find_label(chain, *handle);
1186 if (!c) {
1187 errno = ENOENT;
1188 return NULL;
1189 }
1190
1191 if (!iptcc_is_builtin(c))
Marc Bouchere6869a82000-03-20 06:03:29 +00001192 return NULL;
1193
Harald Welteaae69be2004-08-29 23:32:14 +00001194 *counters = c->counters;
Marc Bouchere6869a82000-03-20 06:03:29 +00001195
Harald Welteaae69be2004-08-29 23:32:14 +00001196 return standard_target_map(c->verdict);
Harald Welte0113fe72004-01-06 19:04:02 +00001197}
1198
1199static int
Harald Welteaae69be2004-08-29 23:32:14 +00001200iptcc_standard_map(struct rule_head *r, int verdict)
Harald Welte0113fe72004-01-06 19:04:02 +00001201{
Harald Welteaae69be2004-08-29 23:32:14 +00001202 STRUCT_ENTRY *e = r->entry;
Rusty Russell79dee072000-05-02 16:45:16 +00001203 STRUCT_STANDARD_TARGET *t;
Marc Bouchere6869a82000-03-20 06:03:29 +00001204
Rusty Russell79dee072000-05-02 16:45:16 +00001205 t = (STRUCT_STANDARD_TARGET *)GET_TARGET(e);
Marc Bouchere6869a82000-03-20 06:03:29 +00001206
Rusty Russell67088e72000-05-10 01:18:57 +00001207 if (t->target.u.target_size
Philip Blundell8c700902000-05-15 02:17:52 +00001208 != ALIGN(sizeof(STRUCT_STANDARD_TARGET))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001209 errno = EINVAL;
1210 return 0;
1211 }
1212 /* memset for memcmp convenience on delete/replace */
Rusty Russell79dee072000-05-02 16:45:16 +00001213 memset(t->target.u.user.name, 0, FUNCTION_MAXNAMELEN);
1214 strcpy(t->target.u.user.name, STANDARD_TARGET);
Marc Bouchere6869a82000-03-20 06:03:29 +00001215 t->verdict = verdict;
1216
Harald Welteaae69be2004-08-29 23:32:14 +00001217 r->type = IPTCC_R_STANDARD;
1218
Marc Bouchere6869a82000-03-20 06:03:29 +00001219 return 1;
1220}
Rusty Russell7e53bf92000-03-20 07:03:28 +00001221
Marc Bouchere6869a82000-03-20 06:03:29 +00001222static int
Harald Welteaae69be2004-08-29 23:32:14 +00001223iptcc_map_target(const TC_HANDLE_T handle,
1224 struct rule_head *r)
Marc Bouchere6869a82000-03-20 06:03:29 +00001225{
Harald Welteaae69be2004-08-29 23:32:14 +00001226 STRUCT_ENTRY *e = r->entry;
Harald Welte0113fe72004-01-06 19:04:02 +00001227 STRUCT_ENTRY_TARGET *t = GET_TARGET(e);
Marc Bouchere6869a82000-03-20 06:03:29 +00001228
Marc Bouchere6869a82000-03-20 06:03:29 +00001229 /* Maybe it's empty (=> fall through) */
Harald Welteaae69be2004-08-29 23:32:14 +00001230 if (strcmp(t->u.user.name, "") == 0) {
1231 r->type = IPTCC_R_FALLTHROUGH;
1232 return 1;
1233 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001234 /* Maybe it's a standard target name... */
Rusty Russell79dee072000-05-02 16:45:16 +00001235 else if (strcmp(t->u.user.name, LABEL_ACCEPT) == 0)
Harald Welteaae69be2004-08-29 23:32:14 +00001236 return iptcc_standard_map(r, -NF_ACCEPT - 1);
Rusty Russell79dee072000-05-02 16:45:16 +00001237 else if (strcmp(t->u.user.name, LABEL_DROP) == 0)
Harald Welteaae69be2004-08-29 23:32:14 +00001238 return iptcc_standard_map(r, -NF_DROP - 1);
Rusty Russell67088e72000-05-10 01:18:57 +00001239 else if (strcmp(t->u.user.name, LABEL_QUEUE) == 0)
Harald Welteaae69be2004-08-29 23:32:14 +00001240 return iptcc_standard_map(r, -NF_QUEUE - 1);
Rusty Russell79dee072000-05-02 16:45:16 +00001241 else if (strcmp(t->u.user.name, LABEL_RETURN) == 0)
Harald Welteaae69be2004-08-29 23:32:14 +00001242 return iptcc_standard_map(r, RETURN);
Rusty Russell79dee072000-05-02 16:45:16 +00001243 else if (TC_BUILTIN(t->u.user.name, handle)) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001244 /* Can't jump to builtins. */
1245 errno = EINVAL;
1246 return 0;
1247 } else {
1248 /* Maybe it's an existing chain name. */
Harald Welteaae69be2004-08-29 23:32:14 +00001249 struct chain_head *c;
1250 DEBUGP("trying to find chain `%s': ", t->u.user.name);
Marc Bouchere6869a82000-03-20 06:03:29 +00001251
Harald Welteaae69be2004-08-29 23:32:14 +00001252 c = iptcc_find_label(t->u.user.name, handle);
1253 if (c) {
1254 DEBUGP_C("found!\n");
1255 r->type = IPTCC_R_JUMP;
1256 r->jump = c;
1257 c->references++;
1258 return 1;
1259 }
1260 DEBUGP_C("not found :(\n");
Marc Bouchere6869a82000-03-20 06:03:29 +00001261 }
1262
1263 /* Must be a module? If not, kernel will reject... */
1264 /* memset to all 0 for your memcmp convenience. */
Rusty Russell228e98d2000-04-27 10:28:06 +00001265 memset(t->u.user.name + strlen(t->u.user.name),
Marc Bouchere6869a82000-03-20 06:03:29 +00001266 0,
Rusty Russell79dee072000-05-02 16:45:16 +00001267 FUNCTION_MAXNAMELEN - strlen(t->u.user.name));
Harald Welteaae69be2004-08-29 23:32:14 +00001268
1269 set_changed(handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001270 return 1;
1271}
1272
Harald Welte0113fe72004-01-06 19:04:02 +00001273/* Insert the entry `fw' in chain `chain' into position `rulenum'. */
Marc Bouchere6869a82000-03-20 06:03:29 +00001274int
Rusty Russell79dee072000-05-02 16:45:16 +00001275TC_INSERT_ENTRY(const IPT_CHAINLABEL chain,
1276 const STRUCT_ENTRY *e,
1277 unsigned int rulenum,
1278 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001279{
Harald Welteaae69be2004-08-29 23:32:14 +00001280 struct chain_head *c;
1281 struct rule_head *r, *prev;
Marc Bouchere6869a82000-03-20 06:03:29 +00001282
Rusty Russell79dee072000-05-02 16:45:16 +00001283 iptc_fn = TC_INSERT_ENTRY;
Harald Welteaae69be2004-08-29 23:32:14 +00001284
1285 if (!(c = iptcc_find_label(chain, *handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001286 errno = ENOENT;
1287 return 0;
1288 }
1289
Harald Welteaae69be2004-08-29 23:32:14 +00001290 prev = iptcc_get_rule_num(c, rulenum);
1291 if (!prev) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001292 errno = E2BIG;
1293 return 0;
1294 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001295
Harald Welteaae69be2004-08-29 23:32:14 +00001296 if (!(r = iptcc_alloc_rule(c, e->next_offset))) {
1297 errno = ENOMEM;
Harald Welte0113fe72004-01-06 19:04:02 +00001298 return 0;
Harald Welteaae69be2004-08-29 23:32:14 +00001299 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001300
Harald Welteaae69be2004-08-29 23:32:14 +00001301 memcpy(r->entry, e, e->next_offset);
1302 r->counter_map.maptype = COUNTER_MAP_SET;
1303
1304 if (!iptcc_map_target(*handle, r)) {
1305 free(r);
1306 return 0;
1307 }
1308
1309 list_add_tail(&r->list, &prev->list);
1310 c->num_rules++;
1311
1312 set_changed(*handle);
1313
1314 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00001315}
1316
1317/* Atomically replace rule `rulenum' in `chain' with `fw'. */
1318int
Rusty Russell79dee072000-05-02 16:45:16 +00001319TC_REPLACE_ENTRY(const IPT_CHAINLABEL chain,
1320 const STRUCT_ENTRY *e,
1321 unsigned int rulenum,
1322 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001323{
Harald Welteaae69be2004-08-29 23:32:14 +00001324 struct chain_head *c;
1325 struct rule_head *r, *old;
Marc Bouchere6869a82000-03-20 06:03:29 +00001326
Rusty Russell79dee072000-05-02 16:45:16 +00001327 iptc_fn = TC_REPLACE_ENTRY;
Marc Bouchere6869a82000-03-20 06:03:29 +00001328
Harald Welteaae69be2004-08-29 23:32:14 +00001329 if (!(c = iptcc_find_label(chain, *handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001330 errno = ENOENT;
1331 return 0;
1332 }
1333
Harald Welteaae69be2004-08-29 23:32:14 +00001334 if (!(old = iptcc_get_rule_num(c, rulenum))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001335 errno = E2BIG;
1336 return 0;
1337 }
1338
Harald Welteaae69be2004-08-29 23:32:14 +00001339 if (!(r = iptcc_alloc_rule(c, e->next_offset))) {
1340 errno = ENOMEM;
Marc Bouchere6869a82000-03-20 06:03:29 +00001341 return 0;
Harald Welteaae69be2004-08-29 23:32:14 +00001342 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001343
Harald Welteaae69be2004-08-29 23:32:14 +00001344 memcpy(r->entry, e, e->next_offset);
1345 r->counter_map.maptype = COUNTER_MAP_SET;
1346
1347 if (!iptcc_map_target(*handle, r)) {
1348 free(r);
Harald Welte0113fe72004-01-06 19:04:02 +00001349 return 0;
Harald Welteaae69be2004-08-29 23:32:14 +00001350 }
Harald Welte0113fe72004-01-06 19:04:02 +00001351
Harald Welteaae69be2004-08-29 23:32:14 +00001352 list_add(&r->list, &old->list);
1353 iptcc_delete_rule(old);
1354
1355 set_changed(*handle);
1356
1357 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00001358}
1359
Harald Welte0113fe72004-01-06 19:04:02 +00001360/* Append entry `fw' to chain `chain'. Equivalent to insert with
Marc Bouchere6869a82000-03-20 06:03:29 +00001361 rulenum = length of chain. */
1362int
Rusty Russell79dee072000-05-02 16:45:16 +00001363TC_APPEND_ENTRY(const IPT_CHAINLABEL chain,
1364 const STRUCT_ENTRY *e,
1365 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001366{
Harald Welteaae69be2004-08-29 23:32:14 +00001367 struct chain_head *c;
1368 struct rule_head *r;
Marc Bouchere6869a82000-03-20 06:03:29 +00001369
Rusty Russell79dee072000-05-02 16:45:16 +00001370 iptc_fn = TC_APPEND_ENTRY;
Harald Welteaae69be2004-08-29 23:32:14 +00001371 if (!(c = iptcc_find_label(chain, *handle))) {
1372 DEBUGP("unable to find chain `%s'\n", chain);
Marc Bouchere6869a82000-03-20 06:03:29 +00001373 errno = ENOENT;
1374 return 0;
1375 }
1376
Harald Welteaae69be2004-08-29 23:32:14 +00001377 if (!(r = iptcc_alloc_rule(c, e->next_offset))) {
1378 DEBUGP("unable to allocate rule for chain `%s'\n", chain);
1379 errno = ENOMEM;
Harald Welte0113fe72004-01-06 19:04:02 +00001380 return 0;
Harald Welteaae69be2004-08-29 23:32:14 +00001381 }
Harald Welte0113fe72004-01-06 19:04:02 +00001382
Harald Welteaae69be2004-08-29 23:32:14 +00001383 memcpy(r->entry, e, e->next_offset);
1384 r->counter_map.maptype = COUNTER_MAP_SET;
1385
1386 if (!iptcc_map_target(*handle, r)) {
1387 DEBUGP("unable to ma target of rule for chain `%s'\n", chain);
1388 free(r);
1389 return 0;
1390 }
1391
1392 list_add_tail(&r->list, &c->rules);
1393 c->num_rules++;
1394
1395 set_changed(*handle);
1396
1397 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00001398}
1399
1400static inline int
Rusty Russell79dee072000-05-02 16:45:16 +00001401match_different(const STRUCT_ENTRY_MATCH *a,
Rusty Russelledf14cf2000-04-19 11:26:44 +00001402 const unsigned char *a_elems,
1403 const unsigned char *b_elems,
1404 unsigned char **maskptr)
Marc Bouchere6869a82000-03-20 06:03:29 +00001405{
Rusty Russell79dee072000-05-02 16:45:16 +00001406 const STRUCT_ENTRY_MATCH *b;
Rusty Russelledf14cf2000-04-19 11:26:44 +00001407 unsigned int i;
Marc Bouchere6869a82000-03-20 06:03:29 +00001408
1409 /* Offset of b is the same as a. */
Rusty Russell30fd6e52000-04-23 09:16:06 +00001410 b = (void *)b_elems + ((unsigned char *)a - a_elems);
Marc Bouchere6869a82000-03-20 06:03:29 +00001411
Rusty Russell228e98d2000-04-27 10:28:06 +00001412 if (a->u.match_size != b->u.match_size)
Marc Bouchere6869a82000-03-20 06:03:29 +00001413 return 1;
1414
Rusty Russell228e98d2000-04-27 10:28:06 +00001415 if (strcmp(a->u.user.name, b->u.user.name) != 0)
Marc Bouchere6869a82000-03-20 06:03:29 +00001416 return 1;
1417
Rusty Russell73ef09b2000-07-03 10:24:04 +00001418 *maskptr += ALIGN(sizeof(*a));
Rusty Russelledf14cf2000-04-19 11:26:44 +00001419
Rusty Russell73ef09b2000-07-03 10:24:04 +00001420 for (i = 0; i < a->u.match_size - ALIGN(sizeof(*a)); i++)
Rusty Russelledf14cf2000-04-19 11:26:44 +00001421 if (((a->data[i] ^ b->data[i]) & (*maskptr)[i]) != 0)
Rusty Russell90e712a2000-03-29 04:19:26 +00001422 return 1;
Rusty Russelledf14cf2000-04-19 11:26:44 +00001423 *maskptr += i;
1424 return 0;
1425}
1426
1427static inline int
1428target_different(const unsigned char *a_targdata,
1429 const unsigned char *b_targdata,
1430 unsigned int tdatasize,
1431 const unsigned char *mask)
1432{
1433 unsigned int i;
1434 for (i = 0; i < tdatasize; i++)
1435 if (((a_targdata[i] ^ b_targdata[i]) & mask[i]) != 0)
1436 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00001437
1438 return 0;
1439}
Harald Welteaae69be2004-08-29 23:32:14 +00001440#if 0
Marc Bouchere6869a82000-03-20 06:03:29 +00001441
Rusty Russell79dee072000-05-02 16:45:16 +00001442static int
1443is_same(const STRUCT_ENTRY *a,
1444 const STRUCT_ENTRY *b,
1445 unsigned char *matchmask);
Marc Bouchere6869a82000-03-20 06:03:29 +00001446
Harald Welte0113fe72004-01-06 19:04:02 +00001447/* Delete the first rule in `chain' which matches `fw'. */
Marc Bouchere6869a82000-03-20 06:03:29 +00001448int
Rusty Russell79dee072000-05-02 16:45:16 +00001449TC_DELETE_ENTRY(const IPT_CHAINLABEL chain,
1450 const STRUCT_ENTRY *origfw,
1451 unsigned char *matchmask,
1452 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001453{
Harald Welte0113fe72004-01-06 19:04:02 +00001454 unsigned int offset;
Harald Welteaae69be2004-08-29 23:32:14 +00001455 struct chain_head *c;
Harald Welte0113fe72004-01-06 19:04:02 +00001456 STRUCT_ENTRY *e, *fw;
Marc Bouchere6869a82000-03-20 06:03:29 +00001457
Rusty Russell79dee072000-05-02 16:45:16 +00001458 iptc_fn = TC_DELETE_ENTRY;
Harald Welteaae69be2004-08-29 23:32:14 +00001459 if (!(c = iptcc_find_label(chain, *handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001460 errno = ENOENT;
1461 return 0;
1462 }
1463
Harald Welte0113fe72004-01-06 19:04:02 +00001464 fw = malloc(origfw->next_offset);
1465 if (fw == NULL) {
1466 errno = ENOMEM;
1467 return 0;
1468 }
1469
1470 for (offset = c->start_off; offset < c->end_off;
1471 offset += e->next_offset) {
1472 STRUCT_ENTRY_TARGET discard;
1473
1474 memcpy(fw, origfw, origfw->next_offset);
1475
1476 /* FIXME: handle this in is_same --RR */
1477 if (!map_target(*handle, fw, offset, &discard)) {
1478 free(fw);
1479 return 0;
1480 }
1481 e = get_entry(*handle, offset);
1482
1483#if 0
1484 printf("Deleting:\n");
1485 dump_entry(newe);
1486#endif
1487 if (is_same(e, fw, matchmask)) {
1488 int ret;
1489 ret = delete_rules(1, e->next_offset,
1490 offset, entry2index(*handle, e),
1491 handle);
1492 free(fw);
1493 return ret;
Marc Bouchere6869a82000-03-20 06:03:29 +00001494 }
1495 }
1496
Harald Welte0113fe72004-01-06 19:04:02 +00001497 free(fw);
Marc Bouchere6869a82000-03-20 06:03:29 +00001498 errno = ENOENT;
1499 return 0;
Rusty Russell7e53bf92000-03-20 07:03:28 +00001500}
Harald Welteaae69be2004-08-29 23:32:14 +00001501#endif
1502
1503/* Delete the first rule in `chain' which matches `fw'. */
1504int
1505TC_DELETE_ENTRY(const IPT_CHAINLABEL chain,
1506 const STRUCT_ENTRY *origfw,
1507 unsigned char *matchmask,
1508 TC_HANDLE_T *handle)
1509{
1510 errno = ENOSYS;
1511 return 0;
1512}
Marc Bouchere6869a82000-03-20 06:03:29 +00001513
1514/* Delete the rule in position `rulenum' in `chain'. */
1515int
Rusty Russell79dee072000-05-02 16:45:16 +00001516TC_DELETE_NUM_ENTRY(const IPT_CHAINLABEL chain,
1517 unsigned int rulenum,
1518 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001519{
Harald Welteaae69be2004-08-29 23:32:14 +00001520 struct chain_head *c;
1521 struct rule_head *r;
Marc Bouchere6869a82000-03-20 06:03:29 +00001522
Rusty Russell79dee072000-05-02 16:45:16 +00001523 iptc_fn = TC_DELETE_NUM_ENTRY;
Harald Welteaae69be2004-08-29 23:32:14 +00001524
1525 if (!(c = iptcc_find_label(chain, *handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001526 errno = ENOENT;
1527 return 0;
1528 }
1529
Harald Welteaae69be2004-08-29 23:32:14 +00001530 if (!(r = iptcc_get_rule_num(c, rulenum))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001531 errno = E2BIG;
1532 return 0;
1533 }
1534
Harald Welteaae69be2004-08-29 23:32:14 +00001535 /* If we are about to delete the rule that is the current
1536 * iterator, move rule iterator back. next pointer will then
1537 * point to real next node */
1538 if (r == (*handle)->rule_iterator_cur) {
1539 (*handle)->rule_iterator_cur =
1540 list_entry((*handle)->rule_iterator_cur->list.prev,
1541 struct rule_head, list);
Harald Welte0113fe72004-01-06 19:04:02 +00001542 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001543
Harald Welteaae69be2004-08-29 23:32:14 +00001544 c->num_rules--;
1545 iptcc_delete_rule(r);
1546
1547 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00001548}
1549
1550/* Check the packet `fw' on chain `chain'. Returns the verdict, or
1551 NULL and sets errno. */
1552const char *
Rusty Russell79dee072000-05-02 16:45:16 +00001553TC_CHECK_PACKET(const IPT_CHAINLABEL chain,
1554 STRUCT_ENTRY *entry,
1555 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001556{
1557 errno = ENOSYS;
1558 return NULL;
1559}
1560
1561/* Flushes the entries in the given chain (ie. empties chain). */
1562int
Rusty Russell79dee072000-05-02 16:45:16 +00001563TC_FLUSH_ENTRIES(const IPT_CHAINLABEL chain, TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001564{
Harald Welteaae69be2004-08-29 23:32:14 +00001565 struct chain_head *c;
1566 struct rule_head *r, *tmp;
Marc Bouchere6869a82000-03-20 06:03:29 +00001567
Harald Welte0113fe72004-01-06 19:04:02 +00001568 iptc_fn = TC_FLUSH_ENTRIES;
Harald Welteaae69be2004-08-29 23:32:14 +00001569 if (!(c = iptcc_find_label(chain, *handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001570 errno = ENOENT;
1571 return 0;
1572 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001573
Harald Welteaae69be2004-08-29 23:32:14 +00001574 list_for_each_entry_safe(r, tmp, &c->rules, list) {
1575 iptcc_delete_rule(r);
1576 }
1577
1578 c->num_rules = 0;
1579
1580 set_changed(*handle);
1581
1582 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00001583}
1584
1585/* Zeroes the counters in a chain. */
1586int
Rusty Russell79dee072000-05-02 16:45:16 +00001587TC_ZERO_ENTRIES(const IPT_CHAINLABEL chain, TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001588{
Harald Welteaae69be2004-08-29 23:32:14 +00001589 struct chain_head *c;
1590 struct rule_head *r;
Rusty Russell7e53bf92000-03-20 07:03:28 +00001591
Harald Welteaae69be2004-08-29 23:32:14 +00001592 if (!(c = iptcc_find_label(chain, *handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001593 errno = ENOENT;
1594 return 0;
1595 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001596
Harald Welteaae69be2004-08-29 23:32:14 +00001597 list_for_each_entry(r, &c->rules, list) {
1598 if (r->counter_map.maptype == COUNTER_MAP_NORMAL_MAP)
1599 r->counter_map.maptype = COUNTER_MAP_ZEROED;
Marc Bouchere6869a82000-03-20 06:03:29 +00001600 }
Harald Welteaae69be2004-08-29 23:32:14 +00001601
Rusty Russell175f6412000-03-24 09:32:20 +00001602 set_changed(*handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001603
Marc Bouchere6869a82000-03-20 06:03:29 +00001604 return 1;
1605}
1606
Harald Welte1cef74d2001-01-05 15:22:59 +00001607STRUCT_COUNTERS *
1608TC_READ_COUNTER(const IPT_CHAINLABEL chain,
1609 unsigned int rulenum,
1610 TC_HANDLE_T *handle)
1611{
Harald Welteaae69be2004-08-29 23:32:14 +00001612 struct chain_head *c;
1613 struct rule_head *r;
Harald Welte1cef74d2001-01-05 15:22:59 +00001614
1615 iptc_fn = TC_READ_COUNTER;
1616 CHECK(*handle);
1617
Harald Welteaae69be2004-08-29 23:32:14 +00001618 if (!(c = iptcc_find_label(chain, *handle))) {
Harald Welte1cef74d2001-01-05 15:22:59 +00001619 errno = ENOENT;
1620 return NULL;
1621 }
1622
Harald Welteaae69be2004-08-29 23:32:14 +00001623 if (!(r = iptcc_get_rule_num(c, rulenum))) {
Harald Welte0113fe72004-01-06 19:04:02 +00001624 errno = E2BIG;
1625 return NULL;
1626 }
1627
Harald Welteaae69be2004-08-29 23:32:14 +00001628 return &r->entry[0].counters;
Harald Welte1cef74d2001-01-05 15:22:59 +00001629}
1630
1631int
1632TC_ZERO_COUNTER(const IPT_CHAINLABEL chain,
1633 unsigned int rulenum,
1634 TC_HANDLE_T *handle)
1635{
Harald Welteaae69be2004-08-29 23:32:14 +00001636 struct chain_head *c;
1637 struct rule_head *r;
Harald Welte1cef74d2001-01-05 15:22:59 +00001638
1639 iptc_fn = TC_ZERO_COUNTER;
1640 CHECK(*handle);
1641
Harald Welteaae69be2004-08-29 23:32:14 +00001642 if (!(c = iptcc_find_label(chain, *handle))) {
Harald Welte1cef74d2001-01-05 15:22:59 +00001643 errno = ENOENT;
1644 return 0;
1645 }
1646
Harald Welteaae69be2004-08-29 23:32:14 +00001647 if (!(r = iptcc_get_rule_num(c, rulenum))) {
Harald Welte0113fe72004-01-06 19:04:02 +00001648 errno = E2BIG;
1649 return 0;
1650 }
1651
Harald Welteaae69be2004-08-29 23:32:14 +00001652 if (r->counter_map.maptype == COUNTER_MAP_NORMAL_MAP)
1653 r->counter_map.maptype = COUNTER_MAP_ZEROED;
Harald Welte1cef74d2001-01-05 15:22:59 +00001654
1655 set_changed(*handle);
1656
1657 return 1;
1658}
1659
1660int
1661TC_SET_COUNTER(const IPT_CHAINLABEL chain,
1662 unsigned int rulenum,
1663 STRUCT_COUNTERS *counters,
1664 TC_HANDLE_T *handle)
1665{
Harald Welteaae69be2004-08-29 23:32:14 +00001666 struct chain_head *c;
1667 struct rule_head *r;
Harald Welte1cef74d2001-01-05 15:22:59 +00001668 STRUCT_ENTRY *e;
Harald Welte1cef74d2001-01-05 15:22:59 +00001669
1670 iptc_fn = TC_SET_COUNTER;
1671 CHECK(*handle);
1672
Harald Welteaae69be2004-08-29 23:32:14 +00001673 if (!(c = iptcc_find_label(chain, *handle))) {
Harald Welte1cef74d2001-01-05 15:22:59 +00001674 errno = ENOENT;
1675 return 0;
1676 }
Harald Welte0113fe72004-01-06 19:04:02 +00001677
Harald Welteaae69be2004-08-29 23:32:14 +00001678 if (!(r = iptcc_get_rule_num(c, rulenum))) {
Harald Welte0113fe72004-01-06 19:04:02 +00001679 errno = E2BIG;
1680 return 0;
1681 }
1682
Harald Welteaae69be2004-08-29 23:32:14 +00001683 e = r->entry;
1684 r->counter_map.maptype = COUNTER_MAP_SET;
Harald Welte0113fe72004-01-06 19:04:02 +00001685
1686 memcpy(&e->counters, counters, sizeof(STRUCT_COUNTERS));
Harald Welte1cef74d2001-01-05 15:22:59 +00001687
1688 set_changed(*handle);
1689
1690 return 1;
1691}
1692
Marc Bouchere6869a82000-03-20 06:03:29 +00001693/* Creates a new chain. */
1694/* To create a chain, create two rules: error node and unconditional
1695 * return. */
1696int
Rusty Russell79dee072000-05-02 16:45:16 +00001697TC_CREATE_CHAIN(const IPT_CHAINLABEL chain, TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001698{
Harald Welteaae69be2004-08-29 23:32:14 +00001699 static struct chain_head *c;
Marc Bouchere6869a82000-03-20 06:03:29 +00001700
Rusty Russell79dee072000-05-02 16:45:16 +00001701 iptc_fn = TC_CREATE_CHAIN;
Marc Bouchere6869a82000-03-20 06:03:29 +00001702
1703 /* find_label doesn't cover built-in targets: DROP, ACCEPT,
1704 QUEUE, RETURN. */
Harald Welteaae69be2004-08-29 23:32:14 +00001705 if (iptcc_find_label(chain, *handle)
Rusty Russell79dee072000-05-02 16:45:16 +00001706 || strcmp(chain, LABEL_DROP) == 0
1707 || strcmp(chain, LABEL_ACCEPT) == 0
Rusty Russell67088e72000-05-10 01:18:57 +00001708 || strcmp(chain, LABEL_QUEUE) == 0
Rusty Russell79dee072000-05-02 16:45:16 +00001709 || strcmp(chain, LABEL_RETURN) == 0) {
Harald Welteaae69be2004-08-29 23:32:14 +00001710 DEBUGP("Chain `%s' already exists\n", chain);
Marc Bouchere6869a82000-03-20 06:03:29 +00001711 errno = EEXIST;
1712 return 0;
1713 }
1714
Rusty Russell79dee072000-05-02 16:45:16 +00001715 if (strlen(chain)+1 > sizeof(IPT_CHAINLABEL)) {
Harald Welteaae69be2004-08-29 23:32:14 +00001716 DEBUGP("Chain name `%s' too long\n", chain);
Marc Bouchere6869a82000-03-20 06:03:29 +00001717 errno = EINVAL;
1718 return 0;
1719 }
1720
Harald Welteaae69be2004-08-29 23:32:14 +00001721 c = iptcc_alloc_chain_head(chain, 0);
1722 if (!c) {
1723 DEBUGP("Cannot allocate memory for chain `%s'\n", chain);
1724 errno = ENOMEM;
1725 return 0;
Marc Bouchere6869a82000-03-20 06:03:29 +00001726
Harald Welteaae69be2004-08-29 23:32:14 +00001727 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001728
Harald Welteaae69be2004-08-29 23:32:14 +00001729 DEBUGP("Creating chain `%s'\n", chain);
1730 list_add_tail(&c->list, &(*handle)->chains);
Harald Welte0113fe72004-01-06 19:04:02 +00001731
1732 set_changed(*handle);
1733
Harald Welteaae69be2004-08-29 23:32:14 +00001734 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00001735}
1736
1737/* Get the number of references to this chain. */
1738int
Rusty Russell79dee072000-05-02 16:45:16 +00001739TC_GET_REFERENCES(unsigned int *ref, const IPT_CHAINLABEL chain,
1740 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001741{
Harald Welteaae69be2004-08-29 23:32:14 +00001742 struct chain_head *c;
Marc Bouchere6869a82000-03-20 06:03:29 +00001743
Harald Welteaae69be2004-08-29 23:32:14 +00001744 if (!(c = iptcc_find_label(chain, *handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001745 errno = ENOENT;
1746 return 0;
1747 }
1748
Harald Welteaae69be2004-08-29 23:32:14 +00001749 *ref = c->references;
1750
Marc Bouchere6869a82000-03-20 06:03:29 +00001751 return 1;
1752}
1753
1754/* Deletes a chain. */
1755int
Rusty Russell79dee072000-05-02 16:45:16 +00001756TC_DELETE_CHAIN(const IPT_CHAINLABEL chain, TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001757{
Marc Bouchere6869a82000-03-20 06:03:29 +00001758 unsigned int references;
Harald Welteaae69be2004-08-29 23:32:14 +00001759 struct chain_head *c;
Rusty Russell7e53bf92000-03-20 07:03:28 +00001760
Rusty Russell79dee072000-05-02 16:45:16 +00001761 iptc_fn = TC_DELETE_CHAIN;
Marc Bouchere6869a82000-03-20 06:03:29 +00001762
Harald Welteaae69be2004-08-29 23:32:14 +00001763 if (!(c = iptcc_find_label(chain, *handle))) {
1764 DEBUGP("cannot find chain `%s'\n", chain);
Marc Bouchere6869a82000-03-20 06:03:29 +00001765 errno = ENOENT;
1766 return 0;
1767 }
1768
Harald Welteaae69be2004-08-29 23:32:14 +00001769 if (TC_BUILTIN(chain, *handle)) {
1770 DEBUGP("cannot remove builtin chain `%s'\n", chain);
1771 errno = EINVAL;
1772 return 0;
1773 }
1774
1775 if (!TC_GET_REFERENCES(&references, chain, handle)) {
1776 DEBUGP("cannot get references on chain `%s'\n", chain);
1777 return 0;
1778 }
1779
1780 if (references > 0) {
1781 DEBUGP("chain `%s' still has references\n", chain);
1782 errno = EMLINK;
1783 return 0;
1784 }
1785
1786 if (c->num_rules) {
1787 DEBUGP("chain `%s' is not empty\n", chain);
Marc Bouchere6869a82000-03-20 06:03:29 +00001788 errno = ENOTEMPTY;
1789 return 0;
1790 }
1791
Harald Welteaae69be2004-08-29 23:32:14 +00001792 /* If we are about to delete the chain that is the current
1793 * iterator, move chain iterator firward. */
1794 if (c == (*handle)->chain_iterator_cur)
1795 iptcc_chain_iterator_advance(*handle);
Harald Welte0113fe72004-01-06 19:04:02 +00001796
Harald Welteaae69be2004-08-29 23:32:14 +00001797 list_del(&c->list);
1798 free(c);
Harald Welte0113fe72004-01-06 19:04:02 +00001799
Harald Welteaae69be2004-08-29 23:32:14 +00001800 DEBUGP("chain `%s' deleted\n", chain);
1801
1802 set_changed(*handle);
1803
1804 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00001805}
1806
1807/* Renames a chain. */
Rusty Russell79dee072000-05-02 16:45:16 +00001808int TC_RENAME_CHAIN(const IPT_CHAINLABEL oldname,
1809 const IPT_CHAINLABEL newname,
1810 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001811{
Harald Welteaae69be2004-08-29 23:32:14 +00001812 struct chain_head *c;
Rusty Russell79dee072000-05-02 16:45:16 +00001813 iptc_fn = TC_RENAME_CHAIN;
Marc Bouchere6869a82000-03-20 06:03:29 +00001814
Harald Welte1de80462000-10-30 12:00:27 +00001815 /* find_label doesn't cover built-in targets: DROP, ACCEPT,
1816 QUEUE, RETURN. */
Harald Welteaae69be2004-08-29 23:32:14 +00001817 if (iptcc_find_label(newname, *handle)
Rusty Russell79dee072000-05-02 16:45:16 +00001818 || strcmp(newname, LABEL_DROP) == 0
1819 || strcmp(newname, LABEL_ACCEPT) == 0
Harald Welte1de80462000-10-30 12:00:27 +00001820 || strcmp(newname, LABEL_QUEUE) == 0
Rusty Russell79dee072000-05-02 16:45:16 +00001821 || strcmp(newname, LABEL_RETURN) == 0) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001822 errno = EEXIST;
1823 return 0;
1824 }
1825
Harald Welteaae69be2004-08-29 23:32:14 +00001826 if (!(c = iptcc_find_label(oldname, *handle))
Rusty Russell79dee072000-05-02 16:45:16 +00001827 || TC_BUILTIN(oldname, *handle)) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001828 errno = ENOENT;
1829 return 0;
1830 }
1831
Rusty Russell79dee072000-05-02 16:45:16 +00001832 if (strlen(newname)+1 > sizeof(IPT_CHAINLABEL)) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001833 errno = EINVAL;
1834 return 0;
1835 }
1836
Harald Welteaae69be2004-08-29 23:32:14 +00001837 strncpy(c->name, newname, sizeof(IPT_CHAINLABEL));
1838
Harald Welte0113fe72004-01-06 19:04:02 +00001839 set_changed(*handle);
1840
Marc Bouchere6869a82000-03-20 06:03:29 +00001841 return 1;
1842}
1843
1844/* Sets the policy on a built-in chain. */
1845int
Rusty Russell79dee072000-05-02 16:45:16 +00001846TC_SET_POLICY(const IPT_CHAINLABEL chain,
1847 const IPT_CHAINLABEL policy,
Harald Welte1cef74d2001-01-05 15:22:59 +00001848 STRUCT_COUNTERS *counters,
Rusty Russell79dee072000-05-02 16:45:16 +00001849 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001850{
Harald Welteaae69be2004-08-29 23:32:14 +00001851 struct chain_head *c;
Marc Bouchere6869a82000-03-20 06:03:29 +00001852
Rusty Russell79dee072000-05-02 16:45:16 +00001853 iptc_fn = TC_SET_POLICY;
Marc Bouchere6869a82000-03-20 06:03:29 +00001854
Harald Welteaae69be2004-08-29 23:32:14 +00001855 if (!(c = iptcc_find_label(chain, *handle))) {
1856 DEBUGP("cannot find chain `%s'\n", chain);
1857 errno = ENOENT;
Marc Bouchere6869a82000-03-20 06:03:29 +00001858 return 0;
1859 }
1860
Harald Welteaae69be2004-08-29 23:32:14 +00001861 if (!iptcc_is_builtin(c)) {
1862 DEBUGP("cannot set policy of userdefinedchain `%s'\n", chain);
1863 errno = ENOENT;
1864 return 0;
1865 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001866
Rusty Russell79dee072000-05-02 16:45:16 +00001867 if (strcmp(policy, LABEL_ACCEPT) == 0)
Harald Welteaae69be2004-08-29 23:32:14 +00001868 c->verdict = -NF_ACCEPT - 1;
Rusty Russell79dee072000-05-02 16:45:16 +00001869 else if (strcmp(policy, LABEL_DROP) == 0)
Harald Welteaae69be2004-08-29 23:32:14 +00001870 c->verdict = -NF_DROP - 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00001871 else {
1872 errno = EINVAL;
1873 return 0;
1874 }
Harald Welte1cef74d2001-01-05 15:22:59 +00001875
Harald Welte1cef74d2001-01-05 15:22:59 +00001876 if (counters) {
1877 /* set byte and packet counters */
Harald Welteaae69be2004-08-29 23:32:14 +00001878 memcpy(&c->counters, counters, sizeof(STRUCT_COUNTERS));
1879 c->counter_map.maptype = COUNTER_MAP_SET;
Harald Welte1cef74d2001-01-05 15:22:59 +00001880 } else {
Harald Welteaae69be2004-08-29 23:32:14 +00001881 c->counter_map.maptype = COUNTER_MAP_NOMAP;
Harald Welte1cef74d2001-01-05 15:22:59 +00001882 }
1883
Rusty Russell175f6412000-03-24 09:32:20 +00001884 set_changed(*handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001885
Marc Bouchere6869a82000-03-20 06:03:29 +00001886 return 1;
1887}
1888
1889/* Without this, on gcc 2.7.2.3, we get:
Rusty Russell79dee072000-05-02 16:45:16 +00001890 libiptc.c: In function `TC_COMMIT':
Marc Bouchere6869a82000-03-20 06:03:29 +00001891 libiptc.c:833: fixed or forbidden register was spilled.
1892 This may be due to a compiler bug or to impossible asm
1893 statements or clauses.
1894*/
1895static void
Rusty Russell79dee072000-05-02 16:45:16 +00001896subtract_counters(STRUCT_COUNTERS *answer,
1897 const STRUCT_COUNTERS *a,
1898 const STRUCT_COUNTERS *b)
Marc Bouchere6869a82000-03-20 06:03:29 +00001899{
1900 answer->pcnt = a->pcnt - b->pcnt;
1901 answer->bcnt = a->bcnt - b->bcnt;
1902}
1903
Harald Welteaae69be2004-08-29 23:32:14 +00001904
1905static void counters_nomap(STRUCT_COUNTERS_INFO *newcounters,
1906 unsigned int index)
1907{
1908 newcounters->counters[index] = ((STRUCT_COUNTERS) { 0, 0});
1909 DEBUGP_C("NOMAP => zero\n");
1910}
1911
1912static void counters_normal_map(STRUCT_COUNTERS_INFO *newcounters,
1913 STRUCT_REPLACE *repl,
1914 unsigned int index,
1915 unsigned int mappos)
1916{
1917 /* Original read: X.
1918 * Atomic read on replacement: X + Y.
1919 * Currently in kernel: Z.
1920 * Want in kernel: X + Y + Z.
1921 * => Add in X + Y
1922 * => Add in replacement read.
1923 */
1924 newcounters->counters[index] = repl->counters[mappos];
1925 DEBUGP_C("NORMAL_MAP => mappos %u \n", mappos);
1926}
1927
1928static void counters_map_zeroed(STRUCT_COUNTERS_INFO *newcounters,
1929 STRUCT_REPLACE *repl,
1930 unsigned int index,
1931 unsigned int mappos,
1932 STRUCT_COUNTERS *counters)
1933{
1934 /* Original read: X.
1935 * Atomic read on replacement: X + Y.
1936 * Currently in kernel: Z.
1937 * Want in kernel: Y + Z.
1938 * => Add in Y.
1939 * => Add in (replacement read - original read).
1940 */
1941 subtract_counters(&newcounters->counters[index],
1942 &repl->counters[mappos],
1943 counters);
1944 DEBUGP_C("ZEROED => mappos %u\n", mappos);
1945}
1946
1947static void counters_map_set(STRUCT_COUNTERS_INFO *newcounters,
1948 unsigned int index,
1949 STRUCT_COUNTERS *counters)
1950{
1951 /* Want to set counter (iptables-restore) */
1952
1953 memcpy(&newcounters->counters[index], counters,
1954 sizeof(STRUCT_COUNTERS));
1955
1956 DEBUGP_C("SET\n");
1957}
1958
1959
Marc Bouchere6869a82000-03-20 06:03:29 +00001960int
Rusty Russell79dee072000-05-02 16:45:16 +00001961TC_COMMIT(TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001962{
1963 /* Replace, then map back the counters. */
Rusty Russell79dee072000-05-02 16:45:16 +00001964 STRUCT_REPLACE *repl;
1965 STRUCT_COUNTERS_INFO *newcounters;
Harald Welteaae69be2004-08-29 23:32:14 +00001966 struct chain_head *c;
1967 int ret;
Martin Josefsson841e4ae2003-05-02 15:30:11 +00001968 size_t counterlen;
Harald Welteaae69be2004-08-29 23:32:14 +00001969 int new_number;
1970 unsigned int new_size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001971
1972 CHECK(*handle);
Martin Josefsson841e4ae2003-05-02 15:30:11 +00001973
Marc Bouchere6869a82000-03-20 06:03:29 +00001974#if 0
Rusty Russell54c307e2000-09-04 06:47:28 +00001975 TC_DUMP_ENTRIES(*handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001976#endif
1977
1978 /* Don't commit if nothing changed. */
1979 if (!(*handle)->changed)
1980 goto finished;
1981
Harald Welteaae69be2004-08-29 23:32:14 +00001982 new_number = iptcc_compile_table_prep(*handle, &new_size);
1983 if (new_number < 0) {
1984 errno = ENOMEM;
1985 return 0;
1986 }
1987
1988 repl = malloc(sizeof(*repl) + new_size);
Marc Bouchere6869a82000-03-20 06:03:29 +00001989 if (!repl) {
1990 errno = ENOMEM;
1991 return 0;
1992 }
Harald Welteaae69be2004-08-29 23:32:14 +00001993 memset(repl, 0, sizeof(*repl));
1994
1995 counterlen = sizeof(STRUCT_COUNTERS_INFO)
1996 + sizeof(STRUCT_COUNTERS) * new_number;
Marc Bouchere6869a82000-03-20 06:03:29 +00001997
1998 /* These are the old counters we will get from kernel */
Rusty Russell79dee072000-05-02 16:45:16 +00001999 repl->counters = malloc(sizeof(STRUCT_COUNTERS)
Marc Bouchere6869a82000-03-20 06:03:29 +00002000 * (*handle)->info.num_entries);
2001 if (!repl->counters) {
2002 free(repl);
2003 errno = ENOMEM;
2004 return 0;
2005 }
Marc Bouchere6869a82000-03-20 06:03:29 +00002006 /* These are the counters we're going to put back, later. */
2007 newcounters = malloc(counterlen);
2008 if (!newcounters) {
2009 free(repl->counters);
2010 free(repl);
2011 errno = ENOMEM;
2012 return 0;
2013 }
Harald Welteaae69be2004-08-29 23:32:14 +00002014 memset(newcounters, 0, counterlen);
Marc Bouchere6869a82000-03-20 06:03:29 +00002015
2016 strcpy(repl->name, (*handle)->info.name);
Harald Welteaae69be2004-08-29 23:32:14 +00002017 repl->num_entries = new_number;
2018 repl->size = new_size;
2019
Marc Bouchere6869a82000-03-20 06:03:29 +00002020 repl->num_counters = (*handle)->info.num_entries;
2021 repl->valid_hooks = (*handle)->info.valid_hooks;
Harald Welteaae69be2004-08-29 23:32:14 +00002022
2023 DEBUGP("num_entries=%u, size=%u, num_counters=%u\n",
2024 repl->num_entries, repl->size, repl->num_counters);
2025
2026 ret = iptcc_compile_table(*handle, repl);
2027 if (ret < 0) {
2028 errno = ret;
2029 free(repl->counters);
2030 free(repl);
2031 return 0;
2032 }
2033
2034
2035#ifdef IPTC_DEBUG2
2036 {
2037 int fd = open("/tmp/libiptc-so_set_replace.blob",
2038 O_CREAT|O_WRONLY);
2039 if (fd >= 0) {
2040 write(fd, repl, sizeof(*repl) + repl->size);
2041 close(fd);
2042 }
2043 }
2044#endif
Marc Bouchere6869a82000-03-20 06:03:29 +00002045
Rusty Russell79dee072000-05-02 16:45:16 +00002046 if (setsockopt(sockfd, TC_IPPROTO, SO_SET_REPLACE, repl,
Harald Welteaae69be2004-08-29 23:32:14 +00002047 sizeof(*repl) + repl->size) < 0) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002048 free(repl->counters);
2049 free(repl);
2050 free(newcounters);
2051 return 0;
2052 }
2053
2054 /* Put counters back. */
2055 strcpy(newcounters->name, (*handle)->info.name);
Harald Welteaae69be2004-08-29 23:32:14 +00002056 newcounters->num_counters = new_number;
Marc Bouchere6869a82000-03-20 06:03:29 +00002057
Harald Welteaae69be2004-08-29 23:32:14 +00002058 list_for_each_entry(c, &(*handle)->chains, list) {
2059 struct rule_head *r;
Marc Bouchere6869a82000-03-20 06:03:29 +00002060
Harald Welteaae69be2004-08-29 23:32:14 +00002061 /* Builtin chains have their own counters */
2062 if (iptcc_is_builtin(c)) {
2063 DEBUGP("counter for chain-index %u: ", c->foot_index);
2064 switch(c->counter_map.maptype) {
2065 case COUNTER_MAP_NOMAP:
2066 counters_nomap(newcounters, c->foot_index);
2067 break;
2068 case COUNTER_MAP_NORMAL_MAP:
2069 counters_normal_map(newcounters, repl,
2070 c->foot_index,
2071 c->counter_map.mappos);
2072 break;
2073 case COUNTER_MAP_ZEROED:
2074 counters_map_zeroed(newcounters, repl,
2075 c->foot_index,
2076 c->counter_map.mappos,
2077 &c->counters);
2078 break;
2079 case COUNTER_MAP_SET:
2080 counters_map_set(newcounters, c->foot_index,
2081 &c->counters);
2082 break;
2083 }
2084 }
Harald Welte1cef74d2001-01-05 15:22:59 +00002085
Harald Welteaae69be2004-08-29 23:32:14 +00002086 list_for_each_entry(r, &c->rules, list) {
2087 DEBUGP("counter for index %u: ", r->index);
2088 switch (r->counter_map.maptype) {
2089 case COUNTER_MAP_NOMAP:
2090 counters_nomap(newcounters, r->index);
2091 break;
Harald Welte1cef74d2001-01-05 15:22:59 +00002092
Harald Welteaae69be2004-08-29 23:32:14 +00002093 case COUNTER_MAP_NORMAL_MAP:
2094 counters_normal_map(newcounters, repl,
2095 r->index,
2096 r->counter_map.mappos);
2097 break;
Harald Welte1cef74d2001-01-05 15:22:59 +00002098
Harald Welteaae69be2004-08-29 23:32:14 +00002099 case COUNTER_MAP_ZEROED:
2100 counters_map_zeroed(newcounters, repl,
2101 r->index,
2102 r->counter_map.mappos,
2103 &r->entry->counters);
2104 break;
2105
2106 case COUNTER_MAP_SET:
2107 counters_map_set(newcounters, r->index,
2108 &r->entry->counters);
2109 break;
2110 }
Marc Bouchere6869a82000-03-20 06:03:29 +00002111 }
2112 }
Rusty Russell62527ce2000-09-04 09:45:54 +00002113
Harald Welteaae69be2004-08-29 23:32:14 +00002114
Rusty Russell62527ce2000-09-04 09:45:54 +00002115#ifdef KERNEL_64_USERSPACE_32
2116 {
2117 /* Kernel will think that pointer should be 64-bits, and get
2118 padding. So we accomodate here (assumption: alignment of
2119 `counters' is on 64-bit boundary). */
2120 u_int64_t *kernptr = (u_int64_t *)&newcounters->counters;
2121 if ((unsigned long)&newcounters->counters % 8 != 0) {
2122 fprintf(stderr,
2123 "counters alignment incorrect! Mail rusty!\n");
2124 abort();
2125 }
2126 *kernptr = newcounters->counters;
Rusty Russell54c307e2000-09-04 06:47:28 +00002127 }
Rusty Russell62527ce2000-09-04 09:45:54 +00002128#endif /* KERNEL_64_USERSPACE_32 */
Marc Bouchere6869a82000-03-20 06:03:29 +00002129
Harald Welteaae69be2004-08-29 23:32:14 +00002130#ifdef IPTC_DEBUG2
2131 {
2132 int fd = open("/tmp/libiptc-so_set_add_counters.blob",
2133 O_CREAT|O_WRONLY);
2134 if (fd >= 0) {
2135 write(fd, newcounters, counterlen);
2136 close(fd);
2137 }
2138 }
2139#endif
2140
Rusty Russell79dee072000-05-02 16:45:16 +00002141 if (setsockopt(sockfd, TC_IPPROTO, SO_SET_ADD_COUNTERS,
2142 newcounters, counterlen) < 0) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002143 free(repl->counters);
2144 free(repl);
2145 free(newcounters);
2146 return 0;
2147 }
2148
2149 free(repl->counters);
2150 free(repl);
2151 free(newcounters);
2152
2153 finished:
Martin Josefsson841e4ae2003-05-02 15:30:11 +00002154 TC_FREE(handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00002155 return 1;
2156}
2157
2158/* Get raw socket. */
2159int
Rusty Russell79dee072000-05-02 16:45:16 +00002160TC_GET_RAW_SOCKET()
Marc Bouchere6869a82000-03-20 06:03:29 +00002161{
2162 return sockfd;
2163}
2164
2165/* Translates errno numbers into more human-readable form than strerror. */
2166const char *
Rusty Russell79dee072000-05-02 16:45:16 +00002167TC_STRERROR(int err)
Marc Bouchere6869a82000-03-20 06:03:29 +00002168{
2169 unsigned int i;
2170 struct table_struct {
2171 void *fn;
2172 int err;
2173 const char *message;
2174 } table [] =
Harald Welte4ccfa632001-07-30 15:12:43 +00002175 { { TC_INIT, EPERM, "Permission denied (you must be root)" },
Rusty Russell79dee072000-05-02 16:45:16 +00002176 { TC_INIT, EINVAL, "Module is wrong version" },
Harald Welte4ccfa632001-07-30 15:12:43 +00002177 { TC_INIT, ENOENT,
2178 "Table does not exist (do you need to insmod?)" },
Rusty Russell79dee072000-05-02 16:45:16 +00002179 { TC_DELETE_CHAIN, ENOTEMPTY, "Chain is not empty" },
2180 { TC_DELETE_CHAIN, EINVAL, "Can't delete built-in chain" },
2181 { TC_DELETE_CHAIN, EMLINK,
Marc Bouchere6869a82000-03-20 06:03:29 +00002182 "Can't delete chain with references left" },
Rusty Russell79dee072000-05-02 16:45:16 +00002183 { TC_CREATE_CHAIN, EEXIST, "Chain already exists" },
2184 { TC_INSERT_ENTRY, E2BIG, "Index of insertion too big" },
2185 { TC_REPLACE_ENTRY, E2BIG, "Index of replacement too big" },
2186 { TC_DELETE_NUM_ENTRY, E2BIG, "Index of deletion too big" },
Harald Welte1cef74d2001-01-05 15:22:59 +00002187 { TC_READ_COUNTER, E2BIG, "Index of counter too big" },
2188 { TC_ZERO_COUNTER, E2BIG, "Index of counter too big" },
Rusty Russell79dee072000-05-02 16:45:16 +00002189 { TC_INSERT_ENTRY, ELOOP, "Loop found in table" },
2190 { TC_INSERT_ENTRY, EINVAL, "Target problem" },
Marc Bouchere6869a82000-03-20 06:03:29 +00002191 /* EINVAL for CHECK probably means bad interface. */
Rusty Russell79dee072000-05-02 16:45:16 +00002192 { TC_CHECK_PACKET, EINVAL,
Marc Boucherc8264992000-04-22 22:34:44 +00002193 "Bad arguments (does that interface exist?)" },
Harald Welte4ccfa632001-07-30 15:12:43 +00002194 { TC_CHECK_PACKET, ENOSYS,
2195 "Checking will most likely never get implemented" },
Marc Bouchere6869a82000-03-20 06:03:29 +00002196 /* ENOENT for DELETE probably means no matching rule */
Rusty Russell79dee072000-05-02 16:45:16 +00002197 { TC_DELETE_ENTRY, ENOENT,
Marc Boucherc8264992000-04-22 22:34:44 +00002198 "Bad rule (does a matching rule exist in that chain?)" },
Rusty Russell79dee072000-05-02 16:45:16 +00002199 { TC_SET_POLICY, ENOENT,
Marc Boucherc8264992000-04-22 22:34:44 +00002200 "Bad built-in chain name" },
Rusty Russell79dee072000-05-02 16:45:16 +00002201 { TC_SET_POLICY, EINVAL,
Marc Boucherc8264992000-04-22 22:34:44 +00002202 "Bad policy name" },
Harald Welte4ccfa632001-07-30 15:12:43 +00002203
2204 { NULL, 0, "Incompatible with this kernel" },
2205 { NULL, ENOPROTOOPT, "iptables who? (do you need to insmod?)" },
2206 { NULL, ENOSYS, "Will be implemented real soon. I promise ;)" },
2207 { NULL, ENOMEM, "Memory allocation problem" },
2208 { NULL, ENOENT, "No chain/target/match by that name" },
Marc Bouchere6869a82000-03-20 06:03:29 +00002209 };
2210
2211 for (i = 0; i < sizeof(table)/sizeof(struct table_struct); i++) {
2212 if ((!table[i].fn || table[i].fn == iptc_fn)
2213 && table[i].err == err)
2214 return table[i].message;
2215 }
2216
2217 return strerror(err);
2218}