blob: f017e498c288b89cd3f0bd49084e620b73b7ec1c [file] [log] [blame]
Harald Welte0371c0c2004-09-19 21:00:12 +00001/* Library which manipulates firewall rules. Version $Revision: 1.49 $ */
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
Harald Welte0371c0c2004-09-19 21:00:12 +0000753 h->entries = malloc(sizeof(STRUCT_GET_ENTRIES) + size);
Harald Welteaae69be2004-08-29 23:32:14 +0000754 if (!h->entries)
755 goto out_free_handle;
756
757 strcpy(h->entries->name, tablename);
Harald Welte0371c0c2004-09-19 21:00:12 +0000758 h->entries->size = size;
Marc Bouchere6869a82000-03-20 06:03:29 +0000759
760 return h;
Harald Welteaae69be2004-08-29 23:32:14 +0000761
762out_free_handle:
763 free(h);
764
765 return NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +0000766}
767
Harald Welteaae69be2004-08-29 23:32:14 +0000768
Rusty Russell79dee072000-05-02 16:45:16 +0000769TC_HANDLE_T
770TC_INIT(const char *tablename)
Marc Bouchere6869a82000-03-20 06:03:29 +0000771{
Rusty Russell79dee072000-05-02 16:45:16 +0000772 TC_HANDLE_T h;
773 STRUCT_GETINFO info;
Marc Bouchere6869a82000-03-20 06:03:29 +0000774 int tmp;
775 socklen_t s;
776
Rusty Russell79dee072000-05-02 16:45:16 +0000777 iptc_fn = TC_INIT;
Marc Bouchere6869a82000-03-20 06:03:29 +0000778
Martin Josefssone560fd62003-06-13 16:56:51 +0000779 if (sockfd != -1) {
Harald Welte366454b2002-01-07 13:46:50 +0000780 close(sockfd);
Martin Josefssone560fd62003-06-13 16:56:51 +0000781 sockfd = -1;
782 }
Harald Welte366454b2002-01-07 13:46:50 +0000783
Martin Josefsson841e4ae2003-05-02 15:30:11 +0000784 if (strlen(tablename) >= TABLE_MAXNAMELEN) {
785 errno = EINVAL;
786 return NULL;
787 }
788
Rusty Russell79dee072000-05-02 16:45:16 +0000789 sockfd = socket(TC_AF, SOCK_RAW, IPPROTO_RAW);
Marc Bouchere6869a82000-03-20 06:03:29 +0000790 if (sockfd < 0)
791 return NULL;
792
793 s = sizeof(info);
Martin Josefsson841e4ae2003-05-02 15:30:11 +0000794
Marc Bouchere6869a82000-03-20 06:03:29 +0000795 strcpy(info.name, tablename);
Rusty Russell79dee072000-05-02 16:45:16 +0000796 if (getsockopt(sockfd, TC_IPPROTO, SO_GET_INFO, &info, &s) < 0)
Marc Bouchere6869a82000-03-20 06:03:29 +0000797 return NULL;
798
Harald Welteaae69be2004-08-29 23:32:14 +0000799 DEBUGP("valid_hooks=0x%08x, num_entries=%u, size=%u\n",
800 info.valid_hooks, info.num_entries, info.size);
801
Harald Welte0113fe72004-01-06 19:04:02 +0000802 if ((h = alloc_handle(info.name, info.size, info.num_entries))
Martin Josefsson841e4ae2003-05-02 15:30:11 +0000803 == NULL) {
804 close(sockfd);
Martin Josefssone560fd62003-06-13 16:56:51 +0000805 sockfd = -1;
Marc Bouchere6869a82000-03-20 06:03:29 +0000806 return NULL;
Martin Josefsson841e4ae2003-05-02 15:30:11 +0000807 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000808
Marc Bouchere6869a82000-03-20 06:03:29 +0000809 /* Initialize current state */
810 h->info = info;
Harald Welte0113fe72004-01-06 19:04:02 +0000811
Harald Welteaae69be2004-08-29 23:32:14 +0000812 h->entries->size = h->info.size;
Marc Bouchere6869a82000-03-20 06:03:29 +0000813
Rusty Russell79dee072000-05-02 16:45:16 +0000814 tmp = sizeof(STRUCT_GET_ENTRIES) + h->info.size;
Marc Bouchere6869a82000-03-20 06:03:29 +0000815
Harald Welteaae69be2004-08-29 23:32:14 +0000816 if (getsockopt(sockfd, TC_IPPROTO, SO_GET_ENTRIES, h->entries,
817 &tmp) < 0)
818 goto error;
819
820#ifdef IPTC_DEBUG2
821 {
822 int fd = open("/tmp/libiptc-so_get_entries.blob",
823 O_CREAT|O_WRONLY);
824 if (fd >= 0) {
825 write(fd, h->entries, tmp);
826 close(fd);
827 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000828 }
Harald Welteaae69be2004-08-29 23:32:14 +0000829#endif
830
831 if (parse_table(h) < 0)
832 goto error;
Rusty Russell7e53bf92000-03-20 07:03:28 +0000833
Marc Bouchere6869a82000-03-20 06:03:29 +0000834 CHECK(h);
835 return h;
Harald Welteaae69be2004-08-29 23:32:14 +0000836error:
837 TC_FREE(&h);
838 return NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +0000839}
840
Martin Josefsson841e4ae2003-05-02 15:30:11 +0000841void
842TC_FREE(TC_HANDLE_T *h)
843{
Harald Welteaae69be2004-08-29 23:32:14 +0000844 struct chain_head *c, *tmp;
845
Martin Josefsson841e4ae2003-05-02 15:30:11 +0000846 close(sockfd);
Martin Josefssone560fd62003-06-13 16:56:51 +0000847 sockfd = -1;
Harald Welteaae69be2004-08-29 23:32:14 +0000848
849 list_for_each_entry_safe(c, tmp, &(*h)->chains, list) {
850 struct rule_head *r, *rtmp;
851
852 list_for_each_entry_safe(r, rtmp, &c->rules, list) {
853 free(r);
854 }
855
856 free(c);
857 }
858
859 free((*h)->entries);
Martin Josefsson841e4ae2003-05-02 15:30:11 +0000860 free(*h);
Harald Welteaae69be2004-08-29 23:32:14 +0000861
Martin Josefsson841e4ae2003-05-02 15:30:11 +0000862 *h = NULL;
863}
864
Marc Bouchere6869a82000-03-20 06:03:29 +0000865static inline int
Rusty Russell79dee072000-05-02 16:45:16 +0000866print_match(const STRUCT_ENTRY_MATCH *m)
Marc Bouchere6869a82000-03-20 06:03:29 +0000867{
Rusty Russell228e98d2000-04-27 10:28:06 +0000868 printf("Match name: `%s'\n", m->u.user.name);
Marc Bouchere6869a82000-03-20 06:03:29 +0000869 return 0;
870}
871
Rusty Russell79dee072000-05-02 16:45:16 +0000872static int dump_entry(STRUCT_ENTRY *e, const TC_HANDLE_T handle);
873
Marc Bouchere6869a82000-03-20 06:03:29 +0000874void
Rusty Russell79dee072000-05-02 16:45:16 +0000875TC_DUMP_ENTRIES(const TC_HANDLE_T handle)
Marc Bouchere6869a82000-03-20 06:03:29 +0000876{
877 CHECK(handle);
Harald Welteaae69be2004-08-29 23:32:14 +0000878#if 0
Marc Bouchere6869a82000-03-20 06:03:29 +0000879 printf("libiptc v%s. %u entries, %u bytes.\n",
Harald Welte80fe35d2002-05-29 13:08:15 +0000880 IPTABLES_VERSION,
Harald Welteaae69be2004-08-29 23:32:14 +0000881 handle->new_number, handle->entries->size);
Marc Bouchere6869a82000-03-20 06:03:29 +0000882 printf("Table `%s'\n", handle->info.name);
883 printf("Hooks: pre/in/fwd/out/post = %u/%u/%u/%u/%u\n",
Rusty Russell67088e72000-05-10 01:18:57 +0000884 handle->info.hook_entry[HOOK_PRE_ROUTING],
885 handle->info.hook_entry[HOOK_LOCAL_IN],
886 handle->info.hook_entry[HOOK_FORWARD],
887 handle->info.hook_entry[HOOK_LOCAL_OUT],
888 handle->info.hook_entry[HOOK_POST_ROUTING]);
Marc Bouchere6869a82000-03-20 06:03:29 +0000889 printf("Underflows: pre/in/fwd/out/post = %u/%u/%u/%u/%u\n",
Rusty Russell67088e72000-05-10 01:18:57 +0000890 handle->info.underflow[HOOK_PRE_ROUTING],
891 handle->info.underflow[HOOK_LOCAL_IN],
892 handle->info.underflow[HOOK_FORWARD],
893 handle->info.underflow[HOOK_LOCAL_OUT],
894 handle->info.underflow[HOOK_POST_ROUTING]);
Marc Bouchere6869a82000-03-20 06:03:29 +0000895
Harald Welteaae69be2004-08-29 23:32:14 +0000896 ENTRY_ITERATE(handle->entries->entrytable, handle->entries->size,
Rusty Russell79dee072000-05-02 16:45:16 +0000897 dump_entry, handle);
Harald Welteaae69be2004-08-29 23:32:14 +0000898#endif
Harald Welte0113fe72004-01-06 19:04:02 +0000899}
Rusty Russell30fd6e52000-04-23 09:16:06 +0000900
Marc Bouchere6869a82000-03-20 06:03:29 +0000901/* Does this chain exist? */
Rusty Russell79dee072000-05-02 16:45:16 +0000902int TC_IS_CHAIN(const char *chain, const TC_HANDLE_T handle)
Marc Bouchere6869a82000-03-20 06:03:29 +0000903{
Harald Welteaae69be2004-08-29 23:32:14 +0000904 return iptcc_find_label(chain, handle) != NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +0000905}
906
Harald Welteaae69be2004-08-29 23:32:14 +0000907static void iptcc_chain_iterator_advance(TC_HANDLE_T handle)
908{
909 struct chain_head *c = handle->chain_iterator_cur;
910
911 if (c->list.next == &handle->chains)
912 handle->chain_iterator_cur = NULL;
913 else
914 handle->chain_iterator_cur =
915 list_entry(c->list.next, struct chain_head, list);
916}
Marc Bouchere6869a82000-03-20 06:03:29 +0000917
Rusty Russell30fd6e52000-04-23 09:16:06 +0000918/* Iterator functions to run through the chains. */
Marc Bouchere6869a82000-03-20 06:03:29 +0000919const char *
Philip Blundell8c700902000-05-15 02:17:52 +0000920TC_FIRST_CHAIN(TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +0000921{
Harald Welteaae69be2004-08-29 23:32:14 +0000922 struct chain_head *c = list_entry((*handle)->chains.next,
923 struct chain_head, list);
924
925 iptc_fn = TC_FIRST_CHAIN;
926
927
928 if (list_empty(&(*handle)->chains)) {
929 DEBUGP(": no chains\n");
Harald Welte0113fe72004-01-06 19:04:02 +0000930 return NULL;
Harald Welteaae69be2004-08-29 23:32:14 +0000931 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000932
Harald Welteaae69be2004-08-29 23:32:14 +0000933 (*handle)->chain_iterator_cur = c;
934 iptcc_chain_iterator_advance(*handle);
Harald Welte0113fe72004-01-06 19:04:02 +0000935
Harald Welteaae69be2004-08-29 23:32:14 +0000936 DEBUGP(": returning `%s'\n", c->name);
937 return c->name;
Marc Bouchere6869a82000-03-20 06:03:29 +0000938}
939
Rusty Russell30fd6e52000-04-23 09:16:06 +0000940/* Iterator functions to run through the chains. Returns NULL at end. */
941const char *
Rusty Russell79dee072000-05-02 16:45:16 +0000942TC_NEXT_CHAIN(TC_HANDLE_T *handle)
Rusty Russell30fd6e52000-04-23 09:16:06 +0000943{
Harald Welteaae69be2004-08-29 23:32:14 +0000944 struct chain_head *c = (*handle)->chain_iterator_cur;
Rusty Russell30fd6e52000-04-23 09:16:06 +0000945
Harald Welteaae69be2004-08-29 23:32:14 +0000946 iptc_fn = TC_NEXT_CHAIN;
947
948 if (!c) {
949 DEBUGP(": no more chains\n");
Rusty Russell30fd6e52000-04-23 09:16:06 +0000950 return NULL;
Harald Welteaae69be2004-08-29 23:32:14 +0000951 }
Rusty Russell30fd6e52000-04-23 09:16:06 +0000952
Harald Welteaae69be2004-08-29 23:32:14 +0000953 iptcc_chain_iterator_advance(*handle);
954
955 DEBUGP(": returning `%s'\n", c->name);
956 return c->name;
Rusty Russell30fd6e52000-04-23 09:16:06 +0000957}
958
959/* Get first rule in the given chain: NULL for empty chain. */
Rusty Russell79dee072000-05-02 16:45:16 +0000960const STRUCT_ENTRY *
Philip Blundell8c700902000-05-15 02:17:52 +0000961TC_FIRST_RULE(const char *chain, TC_HANDLE_T *handle)
Rusty Russell30fd6e52000-04-23 09:16:06 +0000962{
Harald Welteaae69be2004-08-29 23:32:14 +0000963 struct chain_head *c;
964 struct rule_head *r;
Rusty Russell30fd6e52000-04-23 09:16:06 +0000965
Harald Welteaae69be2004-08-29 23:32:14 +0000966 iptc_fn = TC_FIRST_RULE;
967
968 DEBUGP("first rule(%s): ", chain);
969
970 c = iptcc_find_label(chain, *handle);
Rusty Russell30fd6e52000-04-23 09:16:06 +0000971 if (!c) {
972 errno = ENOENT;
973 return NULL;
974 }
975
976 /* Empty chain: single return/policy rule */
Harald Welteaae69be2004-08-29 23:32:14 +0000977 if (list_empty(&c->rules)) {
978 DEBUGP_C("no rules, returning NULL\n");
Rusty Russell30fd6e52000-04-23 09:16:06 +0000979 return NULL;
Harald Welteaae69be2004-08-29 23:32:14 +0000980 }
Rusty Russell30fd6e52000-04-23 09:16:06 +0000981
Harald Welteaae69be2004-08-29 23:32:14 +0000982 r = list_entry(c->rules.next, struct rule_head, list);
983 (*handle)->rule_iterator_cur = r;
984 DEBUGP_C("%p\n", r);
985
986 return r->entry;
Rusty Russell30fd6e52000-04-23 09:16:06 +0000987}
988
989/* Returns NULL when rules run out. */
Rusty Russell79dee072000-05-02 16:45:16 +0000990const STRUCT_ENTRY *
Philip Blundell8c700902000-05-15 02:17:52 +0000991TC_NEXT_RULE(const STRUCT_ENTRY *prev, TC_HANDLE_T *handle)
Rusty Russell30fd6e52000-04-23 09:16:06 +0000992{
Harald Welteaae69be2004-08-29 23:32:14 +0000993 struct rule_head *r;
Rusty Russell30fd6e52000-04-23 09:16:06 +0000994
Harald Welteaae69be2004-08-29 23:32:14 +0000995 DEBUGP("rule_iterator_cur=%p...", (*handle)->rule_iterator_cur);
996
997 if (!(*handle)->rule_iterator_cur) {
998 DEBUGP_C("returning NULL\n");
999 return NULL;
1000 }
1001
1002 r = list_entry((*handle)->rule_iterator_cur->list.next,
1003 struct rule_head, list);
1004
1005 iptc_fn = TC_NEXT_RULE;
1006
1007 DEBUGP_C("next=%p, head=%p...", &r->list,
1008 &(*handle)->rule_iterator_cur->chain->rules);
1009
1010 if (&r->list == &(*handle)->rule_iterator_cur->chain->rules) {
1011 (*handle)->rule_iterator_cur = NULL;
1012 DEBUGP_C("finished, returning NULL\n");
1013 return NULL;
1014 }
1015
1016 (*handle)->rule_iterator_cur = r;
1017
1018 /* NOTE: prev is without any influence ! */
1019 DEBUGP_C("returning rule %p\n", r);
1020 return r->entry;
Rusty Russell30fd6e52000-04-23 09:16:06 +00001021}
1022
Marc Bouchere6869a82000-03-20 06:03:29 +00001023/* How many rules in this chain? */
1024unsigned int
Rusty Russell79dee072000-05-02 16:45:16 +00001025TC_NUM_RULES(const char *chain, TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001026{
Harald Welteaae69be2004-08-29 23:32:14 +00001027 struct chain_head *c;
1028 iptc_fn = TC_NUM_RULES;
Marc Bouchere6869a82000-03-20 06:03:29 +00001029 CHECK(*handle);
Harald Welteaae69be2004-08-29 23:32:14 +00001030
1031 c = iptcc_find_label(chain, *handle);
1032 if (!c) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001033 errno = ENOENT;
1034 return (unsigned int)-1;
1035 }
Harald Welteaae69be2004-08-29 23:32:14 +00001036
1037 return c->num_rules;
Marc Bouchere6869a82000-03-20 06:03:29 +00001038}
1039
Rusty Russell79dee072000-05-02 16:45:16 +00001040const STRUCT_ENTRY *TC_GET_RULE(const char *chain,
1041 unsigned int n,
1042 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001043{
Harald Welteaae69be2004-08-29 23:32:14 +00001044 struct chain_head *c;
1045 struct rule_head *r;
1046
1047 iptc_fn = TC_GET_RULE;
Marc Bouchere6869a82000-03-20 06:03:29 +00001048
1049 CHECK(*handle);
Harald Welteaae69be2004-08-29 23:32:14 +00001050
1051 c = iptcc_find_label(chain, *handle);
1052 if (!c) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001053 errno = ENOENT;
1054 return NULL;
1055 }
1056
Harald Welteaae69be2004-08-29 23:32:14 +00001057 r = iptcc_get_rule_num(c, n);
1058 if (!r)
1059 return NULL;
1060 return r->entry;
Marc Bouchere6869a82000-03-20 06:03:29 +00001061}
1062
1063/* Returns a pointer to the target name of this position. */
Harald Welteaae69be2004-08-29 23:32:14 +00001064const char *standard_target_map(int verdict)
Marc Bouchere6869a82000-03-20 06:03:29 +00001065{
Harald Welteaae69be2004-08-29 23:32:14 +00001066 switch (verdict) {
1067 case RETURN:
1068 return LABEL_RETURN;
1069 break;
1070 case -NF_ACCEPT-1:
1071 return LABEL_ACCEPT;
1072 break;
1073 case -NF_DROP-1:
1074 return LABEL_DROP;
1075 break;
1076 case -NF_QUEUE-1:
1077 return LABEL_QUEUE;
1078 break;
1079 default:
1080 fprintf(stderr, "ERROR: %d not a valid target)\n",
1081 verdict);
1082 abort();
1083 break;
1084 }
1085 /* not reached */
1086 return NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +00001087}
1088
Harald Welteaae69be2004-08-29 23:32:14 +00001089/* Returns a pointer to the target name of this position. */
1090const char *TC_GET_TARGET(const STRUCT_ENTRY *ce,
1091 TC_HANDLE_T *handle)
1092{
1093 STRUCT_ENTRY *e = (STRUCT_ENTRY *)ce;
1094 struct rule_head *r = container_of(e, struct rule_head, entry);
1095
1096 iptc_fn = TC_GET_TARGET;
1097
1098 switch(r->type) {
1099 int spos;
1100 case IPTCC_R_FALLTHROUGH:
1101 return "";
1102 break;
1103 case IPTCC_R_JUMP:
1104 DEBUGP("r=%p, jump=%p, name=`%s'\n", r, r->jump, r->jump->name);
1105 return r->jump->name;
1106 break;
1107 case IPTCC_R_STANDARD:
1108 spos = *(int *)GET_TARGET(e)->data;
1109 DEBUGP("r=%p, spos=%d'\n", r, spos);
1110 return standard_target_map(spos);
1111 break;
1112 case IPTCC_R_MODULE:
1113 return GET_TARGET(e)->u.user.name;
1114 break;
1115 }
1116 return NULL;
1117}
Marc Bouchere6869a82000-03-20 06:03:29 +00001118/* Is this a built-in chain? Actually returns hook + 1. */
1119int
Rusty Russell79dee072000-05-02 16:45:16 +00001120TC_BUILTIN(const char *chain, const TC_HANDLE_T handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001121{
Harald Welteaae69be2004-08-29 23:32:14 +00001122 struct chain_head *c;
1123
1124 iptc_fn = TC_BUILTIN;
Marc Bouchere6869a82000-03-20 06:03:29 +00001125
Harald Welteaae69be2004-08-29 23:32:14 +00001126 c = iptcc_find_label(chain, handle);
1127 if (!c) {
1128 errno = ENOENT;
1129 return -1;
Marc Bouchere6869a82000-03-20 06:03:29 +00001130 }
Harald Welteaae69be2004-08-29 23:32:14 +00001131
1132 return iptcc_is_builtin(c);
Marc Bouchere6869a82000-03-20 06:03:29 +00001133}
1134
1135/* Get the policy of a given built-in chain */
1136const char *
Rusty Russell79dee072000-05-02 16:45:16 +00001137TC_GET_POLICY(const char *chain,
1138 STRUCT_COUNTERS *counters,
1139 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001140{
Harald Welteaae69be2004-08-29 23:32:14 +00001141 struct chain_head *c;
Marc Bouchere6869a82000-03-20 06:03:29 +00001142
Harald Welteaae69be2004-08-29 23:32:14 +00001143 iptc_fn = TC_GET_POLICY;
1144
1145 DEBUGP("called for chain %s\n", chain);
1146
1147 c = iptcc_find_label(chain, *handle);
1148 if (!c) {
1149 errno = ENOENT;
1150 return NULL;
1151 }
1152
1153 if (!iptcc_is_builtin(c))
Marc Bouchere6869a82000-03-20 06:03:29 +00001154 return NULL;
1155
Harald Welteaae69be2004-08-29 23:32:14 +00001156 *counters = c->counters;
Marc Bouchere6869a82000-03-20 06:03:29 +00001157
Harald Welteaae69be2004-08-29 23:32:14 +00001158 return standard_target_map(c->verdict);
Harald Welte0113fe72004-01-06 19:04:02 +00001159}
1160
1161static int
Harald Welteaae69be2004-08-29 23:32:14 +00001162iptcc_standard_map(struct rule_head *r, int verdict)
Harald Welte0113fe72004-01-06 19:04:02 +00001163{
Harald Welteaae69be2004-08-29 23:32:14 +00001164 STRUCT_ENTRY *e = r->entry;
Rusty Russell79dee072000-05-02 16:45:16 +00001165 STRUCT_STANDARD_TARGET *t;
Marc Bouchere6869a82000-03-20 06:03:29 +00001166
Rusty Russell79dee072000-05-02 16:45:16 +00001167 t = (STRUCT_STANDARD_TARGET *)GET_TARGET(e);
Marc Bouchere6869a82000-03-20 06:03:29 +00001168
Rusty Russell67088e72000-05-10 01:18:57 +00001169 if (t->target.u.target_size
Philip Blundell8c700902000-05-15 02:17:52 +00001170 != ALIGN(sizeof(STRUCT_STANDARD_TARGET))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001171 errno = EINVAL;
1172 return 0;
1173 }
1174 /* memset for memcmp convenience on delete/replace */
Rusty Russell79dee072000-05-02 16:45:16 +00001175 memset(t->target.u.user.name, 0, FUNCTION_MAXNAMELEN);
1176 strcpy(t->target.u.user.name, STANDARD_TARGET);
Marc Bouchere6869a82000-03-20 06:03:29 +00001177 t->verdict = verdict;
1178
Harald Welteaae69be2004-08-29 23:32:14 +00001179 r->type = IPTCC_R_STANDARD;
1180
Marc Bouchere6869a82000-03-20 06:03:29 +00001181 return 1;
1182}
Rusty Russell7e53bf92000-03-20 07:03:28 +00001183
Marc Bouchere6869a82000-03-20 06:03:29 +00001184static int
Harald Welteaae69be2004-08-29 23:32:14 +00001185iptcc_map_target(const TC_HANDLE_T handle,
1186 struct rule_head *r)
Marc Bouchere6869a82000-03-20 06:03:29 +00001187{
Harald Welteaae69be2004-08-29 23:32:14 +00001188 STRUCT_ENTRY *e = r->entry;
Harald Welte0113fe72004-01-06 19:04:02 +00001189 STRUCT_ENTRY_TARGET *t = GET_TARGET(e);
Marc Bouchere6869a82000-03-20 06:03:29 +00001190
Marc Bouchere6869a82000-03-20 06:03:29 +00001191 /* Maybe it's empty (=> fall through) */
Harald Welteaae69be2004-08-29 23:32:14 +00001192 if (strcmp(t->u.user.name, "") == 0) {
1193 r->type = IPTCC_R_FALLTHROUGH;
1194 return 1;
1195 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001196 /* Maybe it's a standard target name... */
Rusty Russell79dee072000-05-02 16:45:16 +00001197 else if (strcmp(t->u.user.name, LABEL_ACCEPT) == 0)
Harald Welteaae69be2004-08-29 23:32:14 +00001198 return iptcc_standard_map(r, -NF_ACCEPT - 1);
Rusty Russell79dee072000-05-02 16:45:16 +00001199 else if (strcmp(t->u.user.name, LABEL_DROP) == 0)
Harald Welteaae69be2004-08-29 23:32:14 +00001200 return iptcc_standard_map(r, -NF_DROP - 1);
Rusty Russell67088e72000-05-10 01:18:57 +00001201 else if (strcmp(t->u.user.name, LABEL_QUEUE) == 0)
Harald Welteaae69be2004-08-29 23:32:14 +00001202 return iptcc_standard_map(r, -NF_QUEUE - 1);
Rusty Russell79dee072000-05-02 16:45:16 +00001203 else if (strcmp(t->u.user.name, LABEL_RETURN) == 0)
Harald Welteaae69be2004-08-29 23:32:14 +00001204 return iptcc_standard_map(r, RETURN);
Rusty Russell79dee072000-05-02 16:45:16 +00001205 else if (TC_BUILTIN(t->u.user.name, handle)) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001206 /* Can't jump to builtins. */
1207 errno = EINVAL;
1208 return 0;
1209 } else {
1210 /* Maybe it's an existing chain name. */
Harald Welteaae69be2004-08-29 23:32:14 +00001211 struct chain_head *c;
1212 DEBUGP("trying to find chain `%s': ", t->u.user.name);
Marc Bouchere6869a82000-03-20 06:03:29 +00001213
Harald Welteaae69be2004-08-29 23:32:14 +00001214 c = iptcc_find_label(t->u.user.name, handle);
1215 if (c) {
1216 DEBUGP_C("found!\n");
1217 r->type = IPTCC_R_JUMP;
1218 r->jump = c;
1219 c->references++;
1220 return 1;
1221 }
1222 DEBUGP_C("not found :(\n");
Marc Bouchere6869a82000-03-20 06:03:29 +00001223 }
1224
1225 /* Must be a module? If not, kernel will reject... */
1226 /* memset to all 0 for your memcmp convenience. */
Rusty Russell228e98d2000-04-27 10:28:06 +00001227 memset(t->u.user.name + strlen(t->u.user.name),
Marc Bouchere6869a82000-03-20 06:03:29 +00001228 0,
Rusty Russell79dee072000-05-02 16:45:16 +00001229 FUNCTION_MAXNAMELEN - strlen(t->u.user.name));
Harald Welteaae69be2004-08-29 23:32:14 +00001230
1231 set_changed(handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001232 return 1;
1233}
1234
Harald Welte0113fe72004-01-06 19:04:02 +00001235/* Insert the entry `fw' in chain `chain' into position `rulenum'. */
Marc Bouchere6869a82000-03-20 06:03:29 +00001236int
Rusty Russell79dee072000-05-02 16:45:16 +00001237TC_INSERT_ENTRY(const IPT_CHAINLABEL chain,
1238 const STRUCT_ENTRY *e,
1239 unsigned int rulenum,
1240 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001241{
Harald Welteaae69be2004-08-29 23:32:14 +00001242 struct chain_head *c;
1243 struct rule_head *r, *prev;
Marc Bouchere6869a82000-03-20 06:03:29 +00001244
Rusty Russell79dee072000-05-02 16:45:16 +00001245 iptc_fn = TC_INSERT_ENTRY;
Harald Welteaae69be2004-08-29 23:32:14 +00001246
1247 if (!(c = iptcc_find_label(chain, *handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001248 errno = ENOENT;
1249 return 0;
1250 }
1251
Harald Welteaae69be2004-08-29 23:32:14 +00001252 prev = iptcc_get_rule_num(c, rulenum);
1253 if (!prev) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001254 errno = E2BIG;
1255 return 0;
1256 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001257
Harald Welteaae69be2004-08-29 23:32:14 +00001258 if (!(r = iptcc_alloc_rule(c, e->next_offset))) {
1259 errno = ENOMEM;
Harald Welte0113fe72004-01-06 19:04:02 +00001260 return 0;
Harald Welteaae69be2004-08-29 23:32:14 +00001261 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001262
Harald Welteaae69be2004-08-29 23:32:14 +00001263 memcpy(r->entry, e, e->next_offset);
1264 r->counter_map.maptype = COUNTER_MAP_SET;
1265
1266 if (!iptcc_map_target(*handle, r)) {
1267 free(r);
1268 return 0;
1269 }
1270
1271 list_add_tail(&r->list, &prev->list);
1272 c->num_rules++;
1273
1274 set_changed(*handle);
1275
1276 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00001277}
1278
1279/* Atomically replace rule `rulenum' in `chain' with `fw'. */
1280int
Rusty Russell79dee072000-05-02 16:45:16 +00001281TC_REPLACE_ENTRY(const IPT_CHAINLABEL chain,
1282 const STRUCT_ENTRY *e,
1283 unsigned int rulenum,
1284 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001285{
Harald Welteaae69be2004-08-29 23:32:14 +00001286 struct chain_head *c;
1287 struct rule_head *r, *old;
Marc Bouchere6869a82000-03-20 06:03:29 +00001288
Rusty Russell79dee072000-05-02 16:45:16 +00001289 iptc_fn = TC_REPLACE_ENTRY;
Marc Bouchere6869a82000-03-20 06:03:29 +00001290
Harald Welteaae69be2004-08-29 23:32:14 +00001291 if (!(c = iptcc_find_label(chain, *handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001292 errno = ENOENT;
1293 return 0;
1294 }
1295
Harald Welteaae69be2004-08-29 23:32:14 +00001296 if (!(old = iptcc_get_rule_num(c, rulenum))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001297 errno = E2BIG;
1298 return 0;
1299 }
1300
Harald Welteaae69be2004-08-29 23:32:14 +00001301 if (!(r = iptcc_alloc_rule(c, e->next_offset))) {
1302 errno = ENOMEM;
Marc Bouchere6869a82000-03-20 06:03:29 +00001303 return 0;
Harald Welteaae69be2004-08-29 23:32:14 +00001304 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001305
Harald Welteaae69be2004-08-29 23:32:14 +00001306 memcpy(r->entry, e, e->next_offset);
1307 r->counter_map.maptype = COUNTER_MAP_SET;
1308
1309 if (!iptcc_map_target(*handle, r)) {
1310 free(r);
Harald Welte0113fe72004-01-06 19:04:02 +00001311 return 0;
Harald Welteaae69be2004-08-29 23:32:14 +00001312 }
Harald Welte0113fe72004-01-06 19:04:02 +00001313
Harald Welteaae69be2004-08-29 23:32:14 +00001314 list_add(&r->list, &old->list);
1315 iptcc_delete_rule(old);
1316
1317 set_changed(*handle);
1318
1319 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00001320}
1321
Harald Welte0113fe72004-01-06 19:04:02 +00001322/* Append entry `fw' to chain `chain'. Equivalent to insert with
Marc Bouchere6869a82000-03-20 06:03:29 +00001323 rulenum = length of chain. */
1324int
Rusty Russell79dee072000-05-02 16:45:16 +00001325TC_APPEND_ENTRY(const IPT_CHAINLABEL chain,
1326 const STRUCT_ENTRY *e,
1327 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001328{
Harald Welteaae69be2004-08-29 23:32:14 +00001329 struct chain_head *c;
1330 struct rule_head *r;
Marc Bouchere6869a82000-03-20 06:03:29 +00001331
Rusty Russell79dee072000-05-02 16:45:16 +00001332 iptc_fn = TC_APPEND_ENTRY;
Harald Welteaae69be2004-08-29 23:32:14 +00001333 if (!(c = iptcc_find_label(chain, *handle))) {
1334 DEBUGP("unable to find chain `%s'\n", chain);
Marc Bouchere6869a82000-03-20 06:03:29 +00001335 errno = ENOENT;
1336 return 0;
1337 }
1338
Harald Welteaae69be2004-08-29 23:32:14 +00001339 if (!(r = iptcc_alloc_rule(c, e->next_offset))) {
1340 DEBUGP("unable to allocate rule for chain `%s'\n", chain);
1341 errno = ENOMEM;
Harald Welte0113fe72004-01-06 19:04:02 +00001342 return 0;
Harald Welteaae69be2004-08-29 23:32:14 +00001343 }
Harald Welte0113fe72004-01-06 19:04:02 +00001344
Harald Welteaae69be2004-08-29 23:32:14 +00001345 memcpy(r->entry, e, e->next_offset);
1346 r->counter_map.maptype = COUNTER_MAP_SET;
1347
1348 if (!iptcc_map_target(*handle, r)) {
1349 DEBUGP("unable to ma target of rule for chain `%s'\n", chain);
1350 free(r);
1351 return 0;
1352 }
1353
1354 list_add_tail(&r->list, &c->rules);
1355 c->num_rules++;
1356
1357 set_changed(*handle);
1358
1359 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00001360}
1361
1362static inline int
Rusty Russell79dee072000-05-02 16:45:16 +00001363match_different(const STRUCT_ENTRY_MATCH *a,
Rusty Russelledf14cf2000-04-19 11:26:44 +00001364 const unsigned char *a_elems,
1365 const unsigned char *b_elems,
1366 unsigned char **maskptr)
Marc Bouchere6869a82000-03-20 06:03:29 +00001367{
Rusty Russell79dee072000-05-02 16:45:16 +00001368 const STRUCT_ENTRY_MATCH *b;
Rusty Russelledf14cf2000-04-19 11:26:44 +00001369 unsigned int i;
Marc Bouchere6869a82000-03-20 06:03:29 +00001370
1371 /* Offset of b is the same as a. */
Rusty Russell30fd6e52000-04-23 09:16:06 +00001372 b = (void *)b_elems + ((unsigned char *)a - a_elems);
Marc Bouchere6869a82000-03-20 06:03:29 +00001373
Rusty Russell228e98d2000-04-27 10:28:06 +00001374 if (a->u.match_size != b->u.match_size)
Marc Bouchere6869a82000-03-20 06:03:29 +00001375 return 1;
1376
Rusty Russell228e98d2000-04-27 10:28:06 +00001377 if (strcmp(a->u.user.name, b->u.user.name) != 0)
Marc Bouchere6869a82000-03-20 06:03:29 +00001378 return 1;
1379
Rusty Russell73ef09b2000-07-03 10:24:04 +00001380 *maskptr += ALIGN(sizeof(*a));
Rusty Russelledf14cf2000-04-19 11:26:44 +00001381
Rusty Russell73ef09b2000-07-03 10:24:04 +00001382 for (i = 0; i < a->u.match_size - ALIGN(sizeof(*a)); i++)
Rusty Russelledf14cf2000-04-19 11:26:44 +00001383 if (((a->data[i] ^ b->data[i]) & (*maskptr)[i]) != 0)
Rusty Russell90e712a2000-03-29 04:19:26 +00001384 return 1;
Rusty Russelledf14cf2000-04-19 11:26:44 +00001385 *maskptr += i;
1386 return 0;
1387}
1388
1389static inline int
1390target_different(const unsigned char *a_targdata,
1391 const unsigned char *b_targdata,
1392 unsigned int tdatasize,
1393 const unsigned char *mask)
1394{
1395 unsigned int i;
1396 for (i = 0; i < tdatasize; i++)
1397 if (((a_targdata[i] ^ b_targdata[i]) & mask[i]) != 0)
1398 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00001399
1400 return 0;
1401}
1402
Rusty Russell79dee072000-05-02 16:45:16 +00001403static int
1404is_same(const STRUCT_ENTRY *a,
1405 const STRUCT_ENTRY *b,
1406 unsigned char *matchmask);
Marc Bouchere6869a82000-03-20 06:03:29 +00001407
Harald Welte0113fe72004-01-06 19:04:02 +00001408/* Delete the first rule in `chain' which matches `fw'. */
Marc Bouchere6869a82000-03-20 06:03:29 +00001409int
Rusty Russell79dee072000-05-02 16:45:16 +00001410TC_DELETE_ENTRY(const IPT_CHAINLABEL chain,
1411 const STRUCT_ENTRY *origfw,
1412 unsigned char *matchmask,
1413 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001414{
Harald Welteaae69be2004-08-29 23:32:14 +00001415 struct chain_head *c;
Harald Weltefe537072004-08-30 20:28:53 +00001416 struct rule_head *r;
Harald Welte0113fe72004-01-06 19:04:02 +00001417 STRUCT_ENTRY *e, *fw;
Marc Bouchere6869a82000-03-20 06:03:29 +00001418
Rusty Russell79dee072000-05-02 16:45:16 +00001419 iptc_fn = TC_DELETE_ENTRY;
Harald Welteaae69be2004-08-29 23:32:14 +00001420 if (!(c = iptcc_find_label(chain, *handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001421 errno = ENOENT;
1422 return 0;
1423 }
1424
Harald Welte0113fe72004-01-06 19:04:02 +00001425 fw = malloc(origfw->next_offset);
1426 if (fw == NULL) {
1427 errno = ENOMEM;
1428 return 0;
1429 }
1430
Harald Weltefe537072004-08-30 20:28:53 +00001431 list_for_each_entry(r, &c->rules, list) {
Harald Welte0113fe72004-01-06 19:04:02 +00001432
1433 memcpy(fw, origfw, origfw->next_offset);
1434
Harald Weltefe537072004-08-30 20:28:53 +00001435#if 0
Harald Welte0113fe72004-01-06 19:04:02 +00001436 /* FIXME: handle this in is_same --RR */
1437 if (!map_target(*handle, fw, offset, &discard)) {
1438 free(fw);
1439 return 0;
1440 }
Harald Welte0113fe72004-01-06 19:04:02 +00001441#endif
Harald Weltefe537072004-08-30 20:28:53 +00001442
1443 e = r->entry;
1444
Harald Welte0113fe72004-01-06 19:04:02 +00001445 if (is_same(e, fw, matchmask)) {
Harald Weltefe537072004-08-30 20:28:53 +00001446 /* If we are about to delete the rule that is the
1447 * current iterator, move rule iterator back. next
1448 * pointer will then point to real next node */
1449 if (r == (*handle)->rule_iterator_cur) {
1450 (*handle)->rule_iterator_cur =
1451 list_entry((*handle)->rule_iterator_cur->list.prev,
1452 struct rule_head, list);
1453 }
1454
1455 c->num_rules--;
1456 iptcc_delete_rule(r);
1457 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00001458 }
1459 }
1460
Harald Welte0113fe72004-01-06 19:04:02 +00001461 free(fw);
Marc Bouchere6869a82000-03-20 06:03:29 +00001462 errno = ENOENT;
1463 return 0;
Rusty Russell7e53bf92000-03-20 07:03:28 +00001464}
Harald Welteaae69be2004-08-29 23:32:14 +00001465
Marc Bouchere6869a82000-03-20 06:03:29 +00001466
1467/* Delete the rule in position `rulenum' in `chain'. */
1468int
Rusty Russell79dee072000-05-02 16:45:16 +00001469TC_DELETE_NUM_ENTRY(const IPT_CHAINLABEL chain,
1470 unsigned int rulenum,
1471 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001472{
Harald Welteaae69be2004-08-29 23:32:14 +00001473 struct chain_head *c;
1474 struct rule_head *r;
Marc Bouchere6869a82000-03-20 06:03:29 +00001475
Rusty Russell79dee072000-05-02 16:45:16 +00001476 iptc_fn = TC_DELETE_NUM_ENTRY;
Harald Welteaae69be2004-08-29 23:32:14 +00001477
1478 if (!(c = iptcc_find_label(chain, *handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001479 errno = ENOENT;
1480 return 0;
1481 }
1482
Harald Welteaae69be2004-08-29 23:32:14 +00001483 if (!(r = iptcc_get_rule_num(c, rulenum))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001484 errno = E2BIG;
1485 return 0;
1486 }
1487
Harald Welteaae69be2004-08-29 23:32:14 +00001488 /* If we are about to delete the rule that is the current
1489 * iterator, move rule iterator back. next pointer will then
1490 * point to real next node */
1491 if (r == (*handle)->rule_iterator_cur) {
1492 (*handle)->rule_iterator_cur =
1493 list_entry((*handle)->rule_iterator_cur->list.prev,
1494 struct rule_head, list);
Harald Welte0113fe72004-01-06 19:04:02 +00001495 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001496
Harald Welteaae69be2004-08-29 23:32:14 +00001497 c->num_rules--;
1498 iptcc_delete_rule(r);
1499
1500 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00001501}
1502
1503/* Check the packet `fw' on chain `chain'. Returns the verdict, or
1504 NULL and sets errno. */
1505const char *
Rusty Russell79dee072000-05-02 16:45:16 +00001506TC_CHECK_PACKET(const IPT_CHAINLABEL chain,
1507 STRUCT_ENTRY *entry,
1508 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001509{
1510 errno = ENOSYS;
1511 return NULL;
1512}
1513
1514/* Flushes the entries in the given chain (ie. empties chain). */
1515int
Rusty Russell79dee072000-05-02 16:45:16 +00001516TC_FLUSH_ENTRIES(const IPT_CHAINLABEL chain, TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001517{
Harald Welteaae69be2004-08-29 23:32:14 +00001518 struct chain_head *c;
1519 struct rule_head *r, *tmp;
Marc Bouchere6869a82000-03-20 06:03:29 +00001520
Harald Welte0113fe72004-01-06 19:04:02 +00001521 iptc_fn = TC_FLUSH_ENTRIES;
Harald Welteaae69be2004-08-29 23:32:14 +00001522 if (!(c = iptcc_find_label(chain, *handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001523 errno = ENOENT;
1524 return 0;
1525 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001526
Harald Welteaae69be2004-08-29 23:32:14 +00001527 list_for_each_entry_safe(r, tmp, &c->rules, list) {
1528 iptcc_delete_rule(r);
1529 }
1530
1531 c->num_rules = 0;
1532
1533 set_changed(*handle);
1534
1535 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00001536}
1537
1538/* Zeroes the counters in a chain. */
1539int
Rusty Russell79dee072000-05-02 16:45:16 +00001540TC_ZERO_ENTRIES(const IPT_CHAINLABEL chain, TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001541{
Harald Welteaae69be2004-08-29 23:32:14 +00001542 struct chain_head *c;
1543 struct rule_head *r;
Rusty Russell7e53bf92000-03-20 07:03:28 +00001544
Harald Welteaae69be2004-08-29 23:32:14 +00001545 if (!(c = iptcc_find_label(chain, *handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001546 errno = ENOENT;
1547 return 0;
1548 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001549
Harald Welteaae69be2004-08-29 23:32:14 +00001550 list_for_each_entry(r, &c->rules, list) {
1551 if (r->counter_map.maptype == COUNTER_MAP_NORMAL_MAP)
1552 r->counter_map.maptype = COUNTER_MAP_ZEROED;
Marc Bouchere6869a82000-03-20 06:03:29 +00001553 }
Harald Welteaae69be2004-08-29 23:32:14 +00001554
Rusty Russell175f6412000-03-24 09:32:20 +00001555 set_changed(*handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001556
Marc Bouchere6869a82000-03-20 06:03:29 +00001557 return 1;
1558}
1559
Harald Welte1cef74d2001-01-05 15:22:59 +00001560STRUCT_COUNTERS *
1561TC_READ_COUNTER(const IPT_CHAINLABEL chain,
1562 unsigned int rulenum,
1563 TC_HANDLE_T *handle)
1564{
Harald Welteaae69be2004-08-29 23:32:14 +00001565 struct chain_head *c;
1566 struct rule_head *r;
Harald Welte1cef74d2001-01-05 15:22:59 +00001567
1568 iptc_fn = TC_READ_COUNTER;
1569 CHECK(*handle);
1570
Harald Welteaae69be2004-08-29 23:32:14 +00001571 if (!(c = iptcc_find_label(chain, *handle))) {
Harald Welte1cef74d2001-01-05 15:22:59 +00001572 errno = ENOENT;
1573 return NULL;
1574 }
1575
Harald Welteaae69be2004-08-29 23:32:14 +00001576 if (!(r = iptcc_get_rule_num(c, rulenum))) {
Harald Welte0113fe72004-01-06 19:04:02 +00001577 errno = E2BIG;
1578 return NULL;
1579 }
1580
Harald Welteaae69be2004-08-29 23:32:14 +00001581 return &r->entry[0].counters;
Harald Welte1cef74d2001-01-05 15:22:59 +00001582}
1583
1584int
1585TC_ZERO_COUNTER(const IPT_CHAINLABEL chain,
1586 unsigned int rulenum,
1587 TC_HANDLE_T *handle)
1588{
Harald Welteaae69be2004-08-29 23:32:14 +00001589 struct chain_head *c;
1590 struct rule_head *r;
Harald Welte1cef74d2001-01-05 15:22:59 +00001591
1592 iptc_fn = TC_ZERO_COUNTER;
1593 CHECK(*handle);
1594
Harald Welteaae69be2004-08-29 23:32:14 +00001595 if (!(c = iptcc_find_label(chain, *handle))) {
Harald Welte1cef74d2001-01-05 15:22:59 +00001596 errno = ENOENT;
1597 return 0;
1598 }
1599
Harald Welteaae69be2004-08-29 23:32:14 +00001600 if (!(r = iptcc_get_rule_num(c, rulenum))) {
Harald Welte0113fe72004-01-06 19:04:02 +00001601 errno = E2BIG;
1602 return 0;
1603 }
1604
Harald Welteaae69be2004-08-29 23:32:14 +00001605 if (r->counter_map.maptype == COUNTER_MAP_NORMAL_MAP)
1606 r->counter_map.maptype = COUNTER_MAP_ZEROED;
Harald Welte1cef74d2001-01-05 15:22:59 +00001607
1608 set_changed(*handle);
1609
1610 return 1;
1611}
1612
1613int
1614TC_SET_COUNTER(const IPT_CHAINLABEL chain,
1615 unsigned int rulenum,
1616 STRUCT_COUNTERS *counters,
1617 TC_HANDLE_T *handle)
1618{
Harald Welteaae69be2004-08-29 23:32:14 +00001619 struct chain_head *c;
1620 struct rule_head *r;
Harald Welte1cef74d2001-01-05 15:22:59 +00001621 STRUCT_ENTRY *e;
Harald Welte1cef74d2001-01-05 15:22:59 +00001622
1623 iptc_fn = TC_SET_COUNTER;
1624 CHECK(*handle);
1625
Harald Welteaae69be2004-08-29 23:32:14 +00001626 if (!(c = iptcc_find_label(chain, *handle))) {
Harald Welte1cef74d2001-01-05 15:22:59 +00001627 errno = ENOENT;
1628 return 0;
1629 }
Harald Welte0113fe72004-01-06 19:04:02 +00001630
Harald Welteaae69be2004-08-29 23:32:14 +00001631 if (!(r = iptcc_get_rule_num(c, rulenum))) {
Harald Welte0113fe72004-01-06 19:04:02 +00001632 errno = E2BIG;
1633 return 0;
1634 }
1635
Harald Welteaae69be2004-08-29 23:32:14 +00001636 e = r->entry;
1637 r->counter_map.maptype = COUNTER_MAP_SET;
Harald Welte0113fe72004-01-06 19:04:02 +00001638
1639 memcpy(&e->counters, counters, sizeof(STRUCT_COUNTERS));
Harald Welte1cef74d2001-01-05 15:22:59 +00001640
1641 set_changed(*handle);
1642
1643 return 1;
1644}
1645
Marc Bouchere6869a82000-03-20 06:03:29 +00001646/* Creates a new chain. */
1647/* To create a chain, create two rules: error node and unconditional
1648 * return. */
1649int
Rusty Russell79dee072000-05-02 16:45:16 +00001650TC_CREATE_CHAIN(const IPT_CHAINLABEL chain, TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001651{
Harald Welteaae69be2004-08-29 23:32:14 +00001652 static struct chain_head *c;
Marc Bouchere6869a82000-03-20 06:03:29 +00001653
Rusty Russell79dee072000-05-02 16:45:16 +00001654 iptc_fn = TC_CREATE_CHAIN;
Marc Bouchere6869a82000-03-20 06:03:29 +00001655
1656 /* find_label doesn't cover built-in targets: DROP, ACCEPT,
1657 QUEUE, RETURN. */
Harald Welteaae69be2004-08-29 23:32:14 +00001658 if (iptcc_find_label(chain, *handle)
Rusty Russell79dee072000-05-02 16:45:16 +00001659 || strcmp(chain, LABEL_DROP) == 0
1660 || strcmp(chain, LABEL_ACCEPT) == 0
Rusty Russell67088e72000-05-10 01:18:57 +00001661 || strcmp(chain, LABEL_QUEUE) == 0
Rusty Russell79dee072000-05-02 16:45:16 +00001662 || strcmp(chain, LABEL_RETURN) == 0) {
Harald Welteaae69be2004-08-29 23:32:14 +00001663 DEBUGP("Chain `%s' already exists\n", chain);
Marc Bouchere6869a82000-03-20 06:03:29 +00001664 errno = EEXIST;
1665 return 0;
1666 }
1667
Rusty Russell79dee072000-05-02 16:45:16 +00001668 if (strlen(chain)+1 > sizeof(IPT_CHAINLABEL)) {
Harald Welteaae69be2004-08-29 23:32:14 +00001669 DEBUGP("Chain name `%s' too long\n", chain);
Marc Bouchere6869a82000-03-20 06:03:29 +00001670 errno = EINVAL;
1671 return 0;
1672 }
1673
Harald Welteaae69be2004-08-29 23:32:14 +00001674 c = iptcc_alloc_chain_head(chain, 0);
1675 if (!c) {
1676 DEBUGP("Cannot allocate memory for chain `%s'\n", chain);
1677 errno = ENOMEM;
1678 return 0;
Marc Bouchere6869a82000-03-20 06:03:29 +00001679
Harald Welteaae69be2004-08-29 23:32:14 +00001680 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001681
Harald Welteaae69be2004-08-29 23:32:14 +00001682 DEBUGP("Creating chain `%s'\n", chain);
1683 list_add_tail(&c->list, &(*handle)->chains);
Harald Welte0113fe72004-01-06 19:04:02 +00001684
1685 set_changed(*handle);
1686
Harald Welteaae69be2004-08-29 23:32:14 +00001687 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00001688}
1689
1690/* Get the number of references to this chain. */
1691int
Rusty Russell79dee072000-05-02 16:45:16 +00001692TC_GET_REFERENCES(unsigned int *ref, const IPT_CHAINLABEL chain,
1693 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001694{
Harald Welteaae69be2004-08-29 23:32:14 +00001695 struct chain_head *c;
Marc Bouchere6869a82000-03-20 06:03:29 +00001696
Harald Welteaae69be2004-08-29 23:32:14 +00001697 if (!(c = iptcc_find_label(chain, *handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001698 errno = ENOENT;
1699 return 0;
1700 }
1701
Harald Welteaae69be2004-08-29 23:32:14 +00001702 *ref = c->references;
1703
Marc Bouchere6869a82000-03-20 06:03:29 +00001704 return 1;
1705}
1706
1707/* Deletes a chain. */
1708int
Rusty Russell79dee072000-05-02 16:45:16 +00001709TC_DELETE_CHAIN(const IPT_CHAINLABEL chain, TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001710{
Marc Bouchere6869a82000-03-20 06:03:29 +00001711 unsigned int references;
Harald Welteaae69be2004-08-29 23:32:14 +00001712 struct chain_head *c;
Rusty Russell7e53bf92000-03-20 07:03:28 +00001713
Rusty Russell79dee072000-05-02 16:45:16 +00001714 iptc_fn = TC_DELETE_CHAIN;
Marc Bouchere6869a82000-03-20 06:03:29 +00001715
Harald Welteaae69be2004-08-29 23:32:14 +00001716 if (!(c = iptcc_find_label(chain, *handle))) {
1717 DEBUGP("cannot find chain `%s'\n", chain);
Marc Bouchere6869a82000-03-20 06:03:29 +00001718 errno = ENOENT;
1719 return 0;
1720 }
1721
Harald Welteaae69be2004-08-29 23:32:14 +00001722 if (TC_BUILTIN(chain, *handle)) {
1723 DEBUGP("cannot remove builtin chain `%s'\n", chain);
1724 errno = EINVAL;
1725 return 0;
1726 }
1727
1728 if (!TC_GET_REFERENCES(&references, chain, handle)) {
1729 DEBUGP("cannot get references on chain `%s'\n", chain);
1730 return 0;
1731 }
1732
1733 if (references > 0) {
1734 DEBUGP("chain `%s' still has references\n", chain);
1735 errno = EMLINK;
1736 return 0;
1737 }
1738
1739 if (c->num_rules) {
1740 DEBUGP("chain `%s' is not empty\n", chain);
Marc Bouchere6869a82000-03-20 06:03:29 +00001741 errno = ENOTEMPTY;
1742 return 0;
1743 }
1744
Harald Welteaae69be2004-08-29 23:32:14 +00001745 /* If we are about to delete the chain that is the current
1746 * iterator, move chain iterator firward. */
1747 if (c == (*handle)->chain_iterator_cur)
1748 iptcc_chain_iterator_advance(*handle);
Harald Welte0113fe72004-01-06 19:04:02 +00001749
Harald Welteaae69be2004-08-29 23:32:14 +00001750 list_del(&c->list);
1751 free(c);
Harald Welte0113fe72004-01-06 19:04:02 +00001752
Harald Welteaae69be2004-08-29 23:32:14 +00001753 DEBUGP("chain `%s' deleted\n", chain);
1754
1755 set_changed(*handle);
1756
1757 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00001758}
1759
1760/* Renames a chain. */
Rusty Russell79dee072000-05-02 16:45:16 +00001761int TC_RENAME_CHAIN(const IPT_CHAINLABEL oldname,
1762 const IPT_CHAINLABEL newname,
1763 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001764{
Harald Welteaae69be2004-08-29 23:32:14 +00001765 struct chain_head *c;
Rusty Russell79dee072000-05-02 16:45:16 +00001766 iptc_fn = TC_RENAME_CHAIN;
Marc Bouchere6869a82000-03-20 06:03:29 +00001767
Harald Welte1de80462000-10-30 12:00:27 +00001768 /* find_label doesn't cover built-in targets: DROP, ACCEPT,
1769 QUEUE, RETURN. */
Harald Welteaae69be2004-08-29 23:32:14 +00001770 if (iptcc_find_label(newname, *handle)
Rusty Russell79dee072000-05-02 16:45:16 +00001771 || strcmp(newname, LABEL_DROP) == 0
1772 || strcmp(newname, LABEL_ACCEPT) == 0
Harald Welte1de80462000-10-30 12:00:27 +00001773 || strcmp(newname, LABEL_QUEUE) == 0
Rusty Russell79dee072000-05-02 16:45:16 +00001774 || strcmp(newname, LABEL_RETURN) == 0) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001775 errno = EEXIST;
1776 return 0;
1777 }
1778
Harald Welteaae69be2004-08-29 23:32:14 +00001779 if (!(c = iptcc_find_label(oldname, *handle))
Rusty Russell79dee072000-05-02 16:45:16 +00001780 || TC_BUILTIN(oldname, *handle)) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001781 errno = ENOENT;
1782 return 0;
1783 }
1784
Rusty Russell79dee072000-05-02 16:45:16 +00001785 if (strlen(newname)+1 > sizeof(IPT_CHAINLABEL)) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001786 errno = EINVAL;
1787 return 0;
1788 }
1789
Harald Welteaae69be2004-08-29 23:32:14 +00001790 strncpy(c->name, newname, sizeof(IPT_CHAINLABEL));
1791
Harald Welte0113fe72004-01-06 19:04:02 +00001792 set_changed(*handle);
1793
Marc Bouchere6869a82000-03-20 06:03:29 +00001794 return 1;
1795}
1796
1797/* Sets the policy on a built-in chain. */
1798int
Rusty Russell79dee072000-05-02 16:45:16 +00001799TC_SET_POLICY(const IPT_CHAINLABEL chain,
1800 const IPT_CHAINLABEL policy,
Harald Welte1cef74d2001-01-05 15:22:59 +00001801 STRUCT_COUNTERS *counters,
Rusty Russell79dee072000-05-02 16:45:16 +00001802 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001803{
Harald Welteaae69be2004-08-29 23:32:14 +00001804 struct chain_head *c;
Marc Bouchere6869a82000-03-20 06:03:29 +00001805
Rusty Russell79dee072000-05-02 16:45:16 +00001806 iptc_fn = TC_SET_POLICY;
Marc Bouchere6869a82000-03-20 06:03:29 +00001807
Harald Welteaae69be2004-08-29 23:32:14 +00001808 if (!(c = iptcc_find_label(chain, *handle))) {
1809 DEBUGP("cannot find chain `%s'\n", chain);
1810 errno = ENOENT;
Marc Bouchere6869a82000-03-20 06:03:29 +00001811 return 0;
1812 }
1813
Harald Welteaae69be2004-08-29 23:32:14 +00001814 if (!iptcc_is_builtin(c)) {
1815 DEBUGP("cannot set policy of userdefinedchain `%s'\n", chain);
1816 errno = ENOENT;
1817 return 0;
1818 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001819
Rusty Russell79dee072000-05-02 16:45:16 +00001820 if (strcmp(policy, LABEL_ACCEPT) == 0)
Harald Welteaae69be2004-08-29 23:32:14 +00001821 c->verdict = -NF_ACCEPT - 1;
Rusty Russell79dee072000-05-02 16:45:16 +00001822 else if (strcmp(policy, LABEL_DROP) == 0)
Harald Welteaae69be2004-08-29 23:32:14 +00001823 c->verdict = -NF_DROP - 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00001824 else {
1825 errno = EINVAL;
1826 return 0;
1827 }
Harald Welte1cef74d2001-01-05 15:22:59 +00001828
Harald Welte1cef74d2001-01-05 15:22:59 +00001829 if (counters) {
1830 /* set byte and packet counters */
Harald Welteaae69be2004-08-29 23:32:14 +00001831 memcpy(&c->counters, counters, sizeof(STRUCT_COUNTERS));
1832 c->counter_map.maptype = COUNTER_MAP_SET;
Harald Welte1cef74d2001-01-05 15:22:59 +00001833 } else {
Harald Welteaae69be2004-08-29 23:32:14 +00001834 c->counter_map.maptype = COUNTER_MAP_NOMAP;
Harald Welte1cef74d2001-01-05 15:22:59 +00001835 }
1836
Rusty Russell175f6412000-03-24 09:32:20 +00001837 set_changed(*handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001838
Marc Bouchere6869a82000-03-20 06:03:29 +00001839 return 1;
1840}
1841
1842/* Without this, on gcc 2.7.2.3, we get:
Rusty Russell79dee072000-05-02 16:45:16 +00001843 libiptc.c: In function `TC_COMMIT':
Marc Bouchere6869a82000-03-20 06:03:29 +00001844 libiptc.c:833: fixed or forbidden register was spilled.
1845 This may be due to a compiler bug or to impossible asm
1846 statements or clauses.
1847*/
1848static void
Rusty Russell79dee072000-05-02 16:45:16 +00001849subtract_counters(STRUCT_COUNTERS *answer,
1850 const STRUCT_COUNTERS *a,
1851 const STRUCT_COUNTERS *b)
Marc Bouchere6869a82000-03-20 06:03:29 +00001852{
1853 answer->pcnt = a->pcnt - b->pcnt;
1854 answer->bcnt = a->bcnt - b->bcnt;
1855}
1856
Harald Welteaae69be2004-08-29 23:32:14 +00001857
1858static void counters_nomap(STRUCT_COUNTERS_INFO *newcounters,
1859 unsigned int index)
1860{
1861 newcounters->counters[index] = ((STRUCT_COUNTERS) { 0, 0});
1862 DEBUGP_C("NOMAP => zero\n");
1863}
1864
1865static void counters_normal_map(STRUCT_COUNTERS_INFO *newcounters,
1866 STRUCT_REPLACE *repl,
1867 unsigned int index,
1868 unsigned int mappos)
1869{
1870 /* Original read: X.
1871 * Atomic read on replacement: X + Y.
1872 * Currently in kernel: Z.
1873 * Want in kernel: X + Y + Z.
1874 * => Add in X + Y
1875 * => Add in replacement read.
1876 */
1877 newcounters->counters[index] = repl->counters[mappos];
1878 DEBUGP_C("NORMAL_MAP => mappos %u \n", mappos);
1879}
1880
1881static void counters_map_zeroed(STRUCT_COUNTERS_INFO *newcounters,
1882 STRUCT_REPLACE *repl,
1883 unsigned int index,
1884 unsigned int mappos,
1885 STRUCT_COUNTERS *counters)
1886{
1887 /* Original read: X.
1888 * Atomic read on replacement: X + Y.
1889 * Currently in kernel: Z.
1890 * Want in kernel: Y + Z.
1891 * => Add in Y.
1892 * => Add in (replacement read - original read).
1893 */
1894 subtract_counters(&newcounters->counters[index],
1895 &repl->counters[mappos],
1896 counters);
1897 DEBUGP_C("ZEROED => mappos %u\n", mappos);
1898}
1899
1900static void counters_map_set(STRUCT_COUNTERS_INFO *newcounters,
1901 unsigned int index,
1902 STRUCT_COUNTERS *counters)
1903{
1904 /* Want to set counter (iptables-restore) */
1905
1906 memcpy(&newcounters->counters[index], counters,
1907 sizeof(STRUCT_COUNTERS));
1908
1909 DEBUGP_C("SET\n");
1910}
1911
1912
Marc Bouchere6869a82000-03-20 06:03:29 +00001913int
Rusty Russell79dee072000-05-02 16:45:16 +00001914TC_COMMIT(TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001915{
1916 /* Replace, then map back the counters. */
Rusty Russell79dee072000-05-02 16:45:16 +00001917 STRUCT_REPLACE *repl;
1918 STRUCT_COUNTERS_INFO *newcounters;
Harald Welteaae69be2004-08-29 23:32:14 +00001919 struct chain_head *c;
1920 int ret;
Martin Josefsson841e4ae2003-05-02 15:30:11 +00001921 size_t counterlen;
Harald Welteaae69be2004-08-29 23:32:14 +00001922 int new_number;
1923 unsigned int new_size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001924
1925 CHECK(*handle);
Martin Josefsson841e4ae2003-05-02 15:30:11 +00001926
Marc Bouchere6869a82000-03-20 06:03:29 +00001927#if 0
Rusty Russell54c307e2000-09-04 06:47:28 +00001928 TC_DUMP_ENTRIES(*handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001929#endif
1930
1931 /* Don't commit if nothing changed. */
1932 if (!(*handle)->changed)
1933 goto finished;
1934
Harald Welteaae69be2004-08-29 23:32:14 +00001935 new_number = iptcc_compile_table_prep(*handle, &new_size);
1936 if (new_number < 0) {
1937 errno = ENOMEM;
1938 return 0;
1939 }
1940
1941 repl = malloc(sizeof(*repl) + new_size);
Marc Bouchere6869a82000-03-20 06:03:29 +00001942 if (!repl) {
1943 errno = ENOMEM;
1944 return 0;
1945 }
Harald Welteaae69be2004-08-29 23:32:14 +00001946 memset(repl, 0, sizeof(*repl));
1947
1948 counterlen = sizeof(STRUCT_COUNTERS_INFO)
1949 + sizeof(STRUCT_COUNTERS) * new_number;
Marc Bouchere6869a82000-03-20 06:03:29 +00001950
1951 /* These are the old counters we will get from kernel */
Rusty Russell79dee072000-05-02 16:45:16 +00001952 repl->counters = malloc(sizeof(STRUCT_COUNTERS)
Marc Bouchere6869a82000-03-20 06:03:29 +00001953 * (*handle)->info.num_entries);
1954 if (!repl->counters) {
1955 free(repl);
1956 errno = ENOMEM;
1957 return 0;
1958 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001959 /* These are the counters we're going to put back, later. */
1960 newcounters = malloc(counterlen);
1961 if (!newcounters) {
1962 free(repl->counters);
1963 free(repl);
1964 errno = ENOMEM;
1965 return 0;
1966 }
Harald Welteaae69be2004-08-29 23:32:14 +00001967 memset(newcounters, 0, counterlen);
Marc Bouchere6869a82000-03-20 06:03:29 +00001968
1969 strcpy(repl->name, (*handle)->info.name);
Harald Welteaae69be2004-08-29 23:32:14 +00001970 repl->num_entries = new_number;
1971 repl->size = new_size;
1972
Marc Bouchere6869a82000-03-20 06:03:29 +00001973 repl->num_counters = (*handle)->info.num_entries;
1974 repl->valid_hooks = (*handle)->info.valid_hooks;
Harald Welteaae69be2004-08-29 23:32:14 +00001975
1976 DEBUGP("num_entries=%u, size=%u, num_counters=%u\n",
1977 repl->num_entries, repl->size, repl->num_counters);
1978
1979 ret = iptcc_compile_table(*handle, repl);
1980 if (ret < 0) {
1981 errno = ret;
1982 free(repl->counters);
1983 free(repl);
1984 return 0;
1985 }
1986
1987
1988#ifdef IPTC_DEBUG2
1989 {
1990 int fd = open("/tmp/libiptc-so_set_replace.blob",
1991 O_CREAT|O_WRONLY);
1992 if (fd >= 0) {
1993 write(fd, repl, sizeof(*repl) + repl->size);
1994 close(fd);
1995 }
1996 }
1997#endif
Marc Bouchere6869a82000-03-20 06:03:29 +00001998
Rusty Russell79dee072000-05-02 16:45:16 +00001999 if (setsockopt(sockfd, TC_IPPROTO, SO_SET_REPLACE, repl,
Harald Welteaae69be2004-08-29 23:32:14 +00002000 sizeof(*repl) + repl->size) < 0) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002001 free(repl->counters);
2002 free(repl);
2003 free(newcounters);
2004 return 0;
2005 }
2006
2007 /* Put counters back. */
2008 strcpy(newcounters->name, (*handle)->info.name);
Harald Welteaae69be2004-08-29 23:32:14 +00002009 newcounters->num_counters = new_number;
Marc Bouchere6869a82000-03-20 06:03:29 +00002010
Harald Welteaae69be2004-08-29 23:32:14 +00002011 list_for_each_entry(c, &(*handle)->chains, list) {
2012 struct rule_head *r;
Marc Bouchere6869a82000-03-20 06:03:29 +00002013
Harald Welteaae69be2004-08-29 23:32:14 +00002014 /* Builtin chains have their own counters */
2015 if (iptcc_is_builtin(c)) {
2016 DEBUGP("counter for chain-index %u: ", c->foot_index);
2017 switch(c->counter_map.maptype) {
2018 case COUNTER_MAP_NOMAP:
2019 counters_nomap(newcounters, c->foot_index);
2020 break;
2021 case COUNTER_MAP_NORMAL_MAP:
2022 counters_normal_map(newcounters, repl,
2023 c->foot_index,
2024 c->counter_map.mappos);
2025 break;
2026 case COUNTER_MAP_ZEROED:
2027 counters_map_zeroed(newcounters, repl,
2028 c->foot_index,
2029 c->counter_map.mappos,
2030 &c->counters);
2031 break;
2032 case COUNTER_MAP_SET:
2033 counters_map_set(newcounters, c->foot_index,
2034 &c->counters);
2035 break;
2036 }
2037 }
Harald Welte1cef74d2001-01-05 15:22:59 +00002038
Harald Welteaae69be2004-08-29 23:32:14 +00002039 list_for_each_entry(r, &c->rules, list) {
2040 DEBUGP("counter for index %u: ", r->index);
2041 switch (r->counter_map.maptype) {
2042 case COUNTER_MAP_NOMAP:
2043 counters_nomap(newcounters, r->index);
2044 break;
Harald Welte1cef74d2001-01-05 15:22:59 +00002045
Harald Welteaae69be2004-08-29 23:32:14 +00002046 case COUNTER_MAP_NORMAL_MAP:
2047 counters_normal_map(newcounters, repl,
2048 r->index,
2049 r->counter_map.mappos);
2050 break;
Harald Welte1cef74d2001-01-05 15:22:59 +00002051
Harald Welteaae69be2004-08-29 23:32:14 +00002052 case COUNTER_MAP_ZEROED:
2053 counters_map_zeroed(newcounters, repl,
2054 r->index,
2055 r->counter_map.mappos,
2056 &r->entry->counters);
2057 break;
2058
2059 case COUNTER_MAP_SET:
2060 counters_map_set(newcounters, r->index,
2061 &r->entry->counters);
2062 break;
2063 }
Marc Bouchere6869a82000-03-20 06:03:29 +00002064 }
2065 }
Rusty Russell62527ce2000-09-04 09:45:54 +00002066
Harald Welteaae69be2004-08-29 23:32:14 +00002067
Rusty Russell62527ce2000-09-04 09:45:54 +00002068#ifdef KERNEL_64_USERSPACE_32
2069 {
2070 /* Kernel will think that pointer should be 64-bits, and get
2071 padding. So we accomodate here (assumption: alignment of
2072 `counters' is on 64-bit boundary). */
2073 u_int64_t *kernptr = (u_int64_t *)&newcounters->counters;
2074 if ((unsigned long)&newcounters->counters % 8 != 0) {
2075 fprintf(stderr,
2076 "counters alignment incorrect! Mail rusty!\n");
2077 abort();
2078 }
2079 *kernptr = newcounters->counters;
Rusty Russell54c307e2000-09-04 06:47:28 +00002080 }
Rusty Russell62527ce2000-09-04 09:45:54 +00002081#endif /* KERNEL_64_USERSPACE_32 */
Marc Bouchere6869a82000-03-20 06:03:29 +00002082
Harald Welteaae69be2004-08-29 23:32:14 +00002083#ifdef IPTC_DEBUG2
2084 {
2085 int fd = open("/tmp/libiptc-so_set_add_counters.blob",
2086 O_CREAT|O_WRONLY);
2087 if (fd >= 0) {
2088 write(fd, newcounters, counterlen);
2089 close(fd);
2090 }
2091 }
2092#endif
2093
Rusty Russell79dee072000-05-02 16:45:16 +00002094 if (setsockopt(sockfd, TC_IPPROTO, SO_SET_ADD_COUNTERS,
2095 newcounters, counterlen) < 0) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002096 free(repl->counters);
2097 free(repl);
2098 free(newcounters);
2099 return 0;
2100 }
2101
2102 free(repl->counters);
2103 free(repl);
2104 free(newcounters);
2105
2106 finished:
Martin Josefsson841e4ae2003-05-02 15:30:11 +00002107 TC_FREE(handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00002108 return 1;
2109}
2110
2111/* Get raw socket. */
2112int
Rusty Russell79dee072000-05-02 16:45:16 +00002113TC_GET_RAW_SOCKET()
Marc Bouchere6869a82000-03-20 06:03:29 +00002114{
2115 return sockfd;
2116}
2117
2118/* Translates errno numbers into more human-readable form than strerror. */
2119const char *
Rusty Russell79dee072000-05-02 16:45:16 +00002120TC_STRERROR(int err)
Marc Bouchere6869a82000-03-20 06:03:29 +00002121{
2122 unsigned int i;
2123 struct table_struct {
2124 void *fn;
2125 int err;
2126 const char *message;
2127 } table [] =
Harald Welte4ccfa632001-07-30 15:12:43 +00002128 { { TC_INIT, EPERM, "Permission denied (you must be root)" },
Rusty Russell79dee072000-05-02 16:45:16 +00002129 { TC_INIT, EINVAL, "Module is wrong version" },
Harald Welte4ccfa632001-07-30 15:12:43 +00002130 { TC_INIT, ENOENT,
2131 "Table does not exist (do you need to insmod?)" },
Rusty Russell79dee072000-05-02 16:45:16 +00002132 { TC_DELETE_CHAIN, ENOTEMPTY, "Chain is not empty" },
2133 { TC_DELETE_CHAIN, EINVAL, "Can't delete built-in chain" },
2134 { TC_DELETE_CHAIN, EMLINK,
Marc Bouchere6869a82000-03-20 06:03:29 +00002135 "Can't delete chain with references left" },
Rusty Russell79dee072000-05-02 16:45:16 +00002136 { TC_CREATE_CHAIN, EEXIST, "Chain already exists" },
2137 { TC_INSERT_ENTRY, E2BIG, "Index of insertion too big" },
2138 { TC_REPLACE_ENTRY, E2BIG, "Index of replacement too big" },
2139 { TC_DELETE_NUM_ENTRY, E2BIG, "Index of deletion too big" },
Harald Welte1cef74d2001-01-05 15:22:59 +00002140 { TC_READ_COUNTER, E2BIG, "Index of counter too big" },
2141 { TC_ZERO_COUNTER, E2BIG, "Index of counter too big" },
Rusty Russell79dee072000-05-02 16:45:16 +00002142 { TC_INSERT_ENTRY, ELOOP, "Loop found in table" },
2143 { TC_INSERT_ENTRY, EINVAL, "Target problem" },
Marc Bouchere6869a82000-03-20 06:03:29 +00002144 /* EINVAL for CHECK probably means bad interface. */
Rusty Russell79dee072000-05-02 16:45:16 +00002145 { TC_CHECK_PACKET, EINVAL,
Marc Boucherc8264992000-04-22 22:34:44 +00002146 "Bad arguments (does that interface exist?)" },
Harald Welte4ccfa632001-07-30 15:12:43 +00002147 { TC_CHECK_PACKET, ENOSYS,
2148 "Checking will most likely never get implemented" },
Marc Bouchere6869a82000-03-20 06:03:29 +00002149 /* ENOENT for DELETE probably means no matching rule */
Rusty Russell79dee072000-05-02 16:45:16 +00002150 { TC_DELETE_ENTRY, ENOENT,
Marc Boucherc8264992000-04-22 22:34:44 +00002151 "Bad rule (does a matching rule exist in that chain?)" },
Rusty Russell79dee072000-05-02 16:45:16 +00002152 { TC_SET_POLICY, ENOENT,
Marc Boucherc8264992000-04-22 22:34:44 +00002153 "Bad built-in chain name" },
Rusty Russell79dee072000-05-02 16:45:16 +00002154 { TC_SET_POLICY, EINVAL,
Marc Boucherc8264992000-04-22 22:34:44 +00002155 "Bad policy name" },
Harald Welte4ccfa632001-07-30 15:12:43 +00002156
2157 { NULL, 0, "Incompatible with this kernel" },
2158 { NULL, ENOPROTOOPT, "iptables who? (do you need to insmod?)" },
2159 { NULL, ENOSYS, "Will be implemented real soon. I promise ;)" },
2160 { NULL, ENOMEM, "Memory allocation problem" },
2161 { NULL, ENOENT, "No chain/target/match by that name" },
Marc Bouchere6869a82000-03-20 06:03:29 +00002162 };
2163
2164 for (i = 0; i < sizeof(table)/sizeof(struct table_struct); i++) {
2165 if ((!table[i].fn || table[i].fn == iptc_fn)
2166 && table[i].err == err)
2167 return table[i].message;
2168 }
2169
2170 return strerror(err);
2171}