blob: 74f56f4fa16f19b32cc8324a735f39848a42a0d2 [file] [log] [blame]
Martin Josefsson8d1b38a2004-09-22 21:00:19 +00001/* Library which manipulates firewall rules. Version $Revision: 1.51 $ */
Marc Bouchere6869a82000-03-20 06:03:29 +00002
3/* Architecture of firewall rules is as follows:
4 *
5 * Chains go INPUT, FORWARD, OUTPUT then user chains.
6 * Each user chain starts with an ERROR node.
7 * Every chain ends with an unconditional jump: a RETURN for user chains,
8 * and a POLICY for built-ins.
9 */
10
Harald Welte3ea8f402003-06-23 18:25:59 +000011/* (C) 1999 Paul ``Rusty'' Russell - Placed under the GNU GPL (See
12 * COPYING for details).
Harald Welteaae69be2004-08-29 23:32:14 +000013 * (C) 2000-2004 by the Netfilter Core Team <coreteam@netfilter.org>
Harald Welte3ea8f402003-06-23 18:25:59 +000014 *
Harald Weltefbc85232003-06-24 17:37:21 +000015 * 2003-Jun-20: Harald Welte <laforge@netfilter.org>:
Harald Welte3ea8f402003-06-23 18:25:59 +000016 * - Reimplementation of chain cache to use offsets instead of entries
Harald Weltefbc85232003-06-24 17:37:21 +000017 * 2003-Jun-23: Harald Welte <laforge@netfilter.org>:
Harald Welte0113fe72004-01-06 19:04:02 +000018 * - performance optimization, sponsored by Astaro AG (http://www.astaro.com/)
Harald Weltefbc85232003-06-24 17:37:21 +000019 * don't rebuild the chain cache after every operation, instead fix it
20 * up after a ruleset change.
Harald Welteaae69be2004-08-29 23:32:14 +000021 * 2004-Aug-18: Harald Welte <laforge@netfilter.org>:
22 * - futher performance work: total reimplementation of libiptc.
23 * - libiptc now has a real internal (linked-list) represntation of the
24 * ruleset and a parser/compiler from/to this internal representation
25 * - again sponsored by Astaro AG (http://www.astaro.com/)
Harald Welte3ea8f402003-06-23 18:25:59 +000026 */
Harald Welte15920d12004-05-16 09:05:07 +000027#include <sys/types.h>
28#include <sys/socket.h>
Stephane Ouellette7cd00282004-05-14 08:21:06 +000029
Harald Welteaae69be2004-08-29 23:32:14 +000030#include "linux_list.h"
31
32//#define IPTC_DEBUG2 1
33
34#ifdef IPTC_DEBUG2
35#include <fcntl.h>
36#define DEBUGP(x, args...) fprintf(stderr, "%s: " x, __FUNCTION__, ## args)
37#define DEBUGP_C(x, args...) fprintf(stderr, x, ## args)
38#else
39#define DEBUGP(x, args...)
40#define DEBUGP_C(x, args...)
41#endif
42
Marc Bouchere6869a82000-03-20 06:03:29 +000043#ifndef IPT_LIB_DIR
44#define IPT_LIB_DIR "/usr/local/lib/iptables"
45#endif
46
Harald Welte0113fe72004-01-06 19:04:02 +000047#ifndef __OPTIMIZE__
48STRUCT_ENTRY_TARGET *
49GET_TARGET(STRUCT_ENTRY *e)
50{
51 return (void *)e + e->target_offset;
52}
53#endif
54
Marc Bouchere6869a82000-03-20 06:03:29 +000055static int sockfd = -1;
56static void *iptc_fn = NULL;
57
58static const char *hooknames[]
Rusty Russell79dee072000-05-02 16:45:16 +000059= { [HOOK_PRE_ROUTING] "PREROUTING",
60 [HOOK_LOCAL_IN] "INPUT",
61 [HOOK_FORWARD] "FORWARD",
62 [HOOK_LOCAL_OUT] "OUTPUT",
Rusty Russell10758b72000-09-14 07:37:33 +000063 [HOOK_POST_ROUTING] "POSTROUTING",
64#ifdef HOOK_DROPPING
65 [HOOK_DROPPING] "DROPPING"
66#endif
Marc Bouchere6869a82000-03-20 06:03:29 +000067};
68
Harald Welteaae69be2004-08-29 23:32:14 +000069/* Convenience structures */
70struct ipt_error_target
71{
72 STRUCT_ENTRY_TARGET t;
73 char error[TABLE_MAXNAMELEN];
74};
75
76struct chain_head;
77struct rule_head;
78
Marc Bouchere6869a82000-03-20 06:03:29 +000079struct counter_map
80{
81 enum {
82 COUNTER_MAP_NOMAP,
83 COUNTER_MAP_NORMAL_MAP,
Harald Welte1cef74d2001-01-05 15:22:59 +000084 COUNTER_MAP_ZEROED,
85 COUNTER_MAP_SET
Marc Bouchere6869a82000-03-20 06:03:29 +000086 } maptype;
87 unsigned int mappos;
88};
89
Harald Welteaae69be2004-08-29 23:32:14 +000090enum iptcc_rule_type {
91 IPTCC_R_STANDARD, /* standard target (ACCEPT, ...) */
92 IPTCC_R_MODULE, /* extension module (SNAT, ...) */
93 IPTCC_R_FALLTHROUGH, /* fallthrough rule */
94 IPTCC_R_JUMP, /* jump to other chain */
Marc Bouchere6869a82000-03-20 06:03:29 +000095};
96
Harald Welteaae69be2004-08-29 23:32:14 +000097struct rule_head
Rusty Russell30fd6e52000-04-23 09:16:06 +000098{
Harald Welteaae69be2004-08-29 23:32:14 +000099 struct list_head list;
100 struct chain_head *chain;
101 struct counter_map counter_map;
102
103 unsigned int index; /* index (needed for counter_map) */
104 unsigned int offset; /* offset in rule blob */
105
106 enum iptcc_rule_type type;
107 struct chain_head *jump; /* jump target, if IPTCC_R_JUMP */
108
109 unsigned int size; /* size of entry data */
110 STRUCT_ENTRY entry[0];
111};
112
113struct chain_head
114{
115 struct list_head list;
Rusty Russell79dee072000-05-02 16:45:16 +0000116 char name[TABLE_MAXNAMELEN];
Harald Welteaae69be2004-08-29 23:32:14 +0000117 unsigned int hooknum; /* hook number+1 if builtin */
118 unsigned int references; /* how many jumps reference us */
119 int verdict; /* verdict if builtin */
120
121 STRUCT_COUNTERS counters; /* per-chain counters */
122 struct counter_map counter_map;
123
124 unsigned int num_rules; /* number of rules in list */
125 struct list_head rules; /* list of rules */
126
127 unsigned int index; /* index (needed for jump resolval) */
128 unsigned int head_offset; /* offset in rule blob */
129 unsigned int foot_index; /* index (needed for counter_map) */
130 unsigned int foot_offset; /* offset in rule blob */
Rusty Russell30fd6e52000-04-23 09:16:06 +0000131};
132
Rusty Russell79dee072000-05-02 16:45:16 +0000133STRUCT_TC_HANDLE
Marc Bouchere6869a82000-03-20 06:03:29 +0000134{
Harald Welteaae69be2004-08-29 23:32:14 +0000135 int changed; /* Have changes been made? */
136
137 struct list_head chains;
138
139 struct chain_head *chain_iterator_cur;
140 struct rule_head *rule_iterator_cur;
141
Rusty Russell79dee072000-05-02 16:45:16 +0000142 STRUCT_GETINFO info;
Harald Welteaae69be2004-08-29 23:32:14 +0000143 STRUCT_GET_ENTRIES *entries;
Marc Bouchere6869a82000-03-20 06:03:29 +0000144};
145
Harald Welteaae69be2004-08-29 23:32:14 +0000146/* allocate a new chain head for the cache */
147static struct chain_head *iptcc_alloc_chain_head(const char *name, int hooknum)
148{
149 struct chain_head *c = malloc(sizeof(*c));
150 if (!c)
151 return NULL;
152 memset(c, 0, sizeof(*c));
153
154 strncpy(c->name, name, TABLE_MAXNAMELEN);
155 c->hooknum = hooknum;
156 INIT_LIST_HEAD(&c->rules);
157
158 return c;
159}
160
161/* allocate and initialize a new rule for the cache */
162static struct rule_head *iptcc_alloc_rule(struct chain_head *c, unsigned int size)
163{
164 struct rule_head *r = malloc(sizeof(*r)+size);
165 if (!r)
166 return NULL;
167 memset(r, 0, sizeof(*r));
168
169 r->chain = c;
170 r->size = size;
171
172 return r;
173}
174
175/* notify us that the ruleset has been modified by the user */
Rusty Russell175f6412000-03-24 09:32:20 +0000176static void
Rusty Russell79dee072000-05-02 16:45:16 +0000177set_changed(TC_HANDLE_T h)
Rusty Russell175f6412000-03-24 09:32:20 +0000178{
Rusty Russell175f6412000-03-24 09:32:20 +0000179 h->changed = 1;
180}
181
Harald Welte380ba5f2002-02-13 16:19:55 +0000182#ifdef IPTC_DEBUG
Rusty Russell79dee072000-05-02 16:45:16 +0000183static void do_check(TC_HANDLE_T h, unsigned int line);
Rusty Russell849779c2000-04-23 15:51:51 +0000184#define CHECK(h) do { if (!getenv("IPTC_NO_CHECK")) do_check((h), __LINE__); } while(0)
Rusty Russell30fd6e52000-04-23 09:16:06 +0000185#else
186#define CHECK(h)
187#endif
Marc Bouchere6869a82000-03-20 06:03:29 +0000188
Harald Welteaae69be2004-08-29 23:32:14 +0000189
190/**********************************************************************
191 * iptc blob utility functions (iptcb_*)
192 **********************************************************************/
193
Marc Bouchere6869a82000-03-20 06:03:29 +0000194static inline int
Harald Welteaae69be2004-08-29 23:32:14 +0000195iptcb_get_number(const STRUCT_ENTRY *i,
Rusty Russell79dee072000-05-02 16:45:16 +0000196 const STRUCT_ENTRY *seek,
Marc Bouchere6869a82000-03-20 06:03:29 +0000197 unsigned int *pos)
198{
199 if (i == seek)
200 return 1;
201 (*pos)++;
202 return 0;
203}
204
Marc Bouchere6869a82000-03-20 06:03:29 +0000205static inline int
Harald Welteaae69be2004-08-29 23:32:14 +0000206iptcb_get_entry_n(STRUCT_ENTRY *i,
Marc Bouchere6869a82000-03-20 06:03:29 +0000207 unsigned int number,
208 unsigned int *pos,
Rusty Russell79dee072000-05-02 16:45:16 +0000209 STRUCT_ENTRY **pe)
Marc Bouchere6869a82000-03-20 06:03:29 +0000210{
211 if (*pos == number) {
212 *pe = i;
213 return 1;
214 }
215 (*pos)++;
216 return 0;
217}
218
Harald Welteaae69be2004-08-29 23:32:14 +0000219static inline STRUCT_ENTRY *
220iptcb_get_entry(TC_HANDLE_T h, unsigned int offset)
221{
222 return (STRUCT_ENTRY *)((char *)h->entries->entrytable + offset);
223}
224
225static unsigned int
226iptcb_entry2index(const TC_HANDLE_T h, const STRUCT_ENTRY *seek)
Marc Bouchere6869a82000-03-20 06:03:29 +0000227{
228 unsigned int pos = 0;
Marc Bouchere6869a82000-03-20 06:03:29 +0000229
Harald Welteaae69be2004-08-29 23:32:14 +0000230 if (ENTRY_ITERATE(h->entries->entrytable, h->entries->size,
231 iptcb_get_number, seek, &pos) == 0) {
232 fprintf(stderr, "ERROR: offset %u not an entry!\n",
233 (unsigned int)((char *)seek - (char *)h->entries->entrytable));
234 abort();
235 }
236 return pos;
Marc Bouchere6869a82000-03-20 06:03:29 +0000237}
238
Harald Welte0113fe72004-01-06 19:04:02 +0000239static inline STRUCT_ENTRY *
Harald Welteaae69be2004-08-29 23:32:14 +0000240iptcb_offset2entry(TC_HANDLE_T h, unsigned int offset)
Harald Welte0113fe72004-01-06 19:04:02 +0000241{
Harald Welteaae69be2004-08-29 23:32:14 +0000242 return (STRUCT_ENTRY *) ((void *)h->entries->entrytable+offset);
Harald Welte0113fe72004-01-06 19:04:02 +0000243}
244
Harald Welteaae69be2004-08-29 23:32:14 +0000245
Harald Welte0113fe72004-01-06 19:04:02 +0000246static inline unsigned long
Harald Welteaae69be2004-08-29 23:32:14 +0000247iptcb_entry2offset(const TC_HANDLE_T h, const STRUCT_ENTRY *e)
Harald Welte0113fe72004-01-06 19:04:02 +0000248{
Harald Welteaae69be2004-08-29 23:32:14 +0000249 return (void *)e - (void *)h->entries->entrytable;
Harald Welte3ea8f402003-06-23 18:25:59 +0000250}
251
252static inline unsigned int
Harald Welteaae69be2004-08-29 23:32:14 +0000253iptcb_offset2index(const TC_HANDLE_T h, unsigned int offset)
Harald Welte3ea8f402003-06-23 18:25:59 +0000254{
Harald Welteaae69be2004-08-29 23:32:14 +0000255 return iptcb_entry2index(h, iptcb_offset2entry(h, offset));
256}
257
258/* Returns 0 if not hook entry, else hooknumber + 1 */
259static inline unsigned int
260iptcb_ent_is_hook_entry(STRUCT_ENTRY *e, TC_HANDLE_T h)
261{
262 unsigned int i;
263
264 for (i = 0; i < NUMHOOKS; i++) {
265 if ((h->info.valid_hooks & (1 << i))
266 && iptcb_get_entry(h, h->info.hook_entry[i]) == e)
267 return i+1;
268 }
269 return 0;
Harald Welte3ea8f402003-06-23 18:25:59 +0000270}
271
272
Harald Welteaae69be2004-08-29 23:32:14 +0000273/**********************************************************************
274 * iptc cache utility functions (iptcc_*)
275 **********************************************************************/
Harald Welte0113fe72004-01-06 19:04:02 +0000276
Harald Welteaae69be2004-08-29 23:32:14 +0000277/* Is the given chain builtin (1) or user-defined (0) */
278static unsigned int iptcc_is_builtin(struct chain_head *c)
279{
280 return (c->hooknum ? 1 : 0);
281}
282
283/* Get a specific rule within a chain */
284static struct rule_head *iptcc_get_rule_num(struct chain_head *c,
285 unsigned int rulenum)
286{
287 struct rule_head *r;
288 unsigned int num = 0;
289
290 list_for_each_entry(r, &c->rules, list) {
291 num++;
292 if (num == rulenum)
293 return r;
294 }
295 return NULL;
296}
297
298/* Returns chain head if found, otherwise NULL. */
299static struct chain_head *
300iptcc_find_chain_by_offset(TC_HANDLE_T handle, unsigned int offset)
301{
302 struct list_head *pos;
303
304 if (list_empty(&handle->chains))
305 return NULL;
306
307 list_for_each(pos, &handle->chains) {
308 struct chain_head *c = list_entry(pos, struct chain_head, list);
309 if (offset >= c->head_offset && offset <= c->foot_offset)
310 return c;
Harald Welte0113fe72004-01-06 19:04:02 +0000311 }
312
Harald Welteaae69be2004-08-29 23:32:14 +0000313 return NULL;
Harald Welte0113fe72004-01-06 19:04:02 +0000314}
Harald Welteaae69be2004-08-29 23:32:14 +0000315/* Returns chain head if found, otherwise NULL. */
316static struct chain_head *
317iptcc_find_label(const char *name, TC_HANDLE_T handle)
318{
319 struct list_head *pos;
320
321 if (list_empty(&handle->chains))
322 return NULL;
323
324 list_for_each(pos, &handle->chains) {
325 struct chain_head *c = list_entry(pos, struct chain_head, list);
326 if (!strcmp(c->name, name))
327 return c;
328 }
329
330 return NULL;
331}
332
333/* called when rule is to be removed from cache */
334static void iptcc_delete_rule(struct rule_head *r)
335{
336 DEBUGP("deleting rule %p (offset %u)\n", r, r->offset);
337 /* clean up reference count of called chain */
338 if (r->type == IPTCC_R_JUMP
339 && r->jump)
340 r->jump->references--;
341
342 list_del(&r->list);
343 free(r);
344}
345
346
347/**********************************************************************
348 * RULESET PARSER (blob -> cache)
349 **********************************************************************/
350
351static int alphasort(const void *a, const void *b)
352{
353 return strcmp(((struct chain_head *)a)->name,
354 ((struct chain_head *)b)->name);
355}
356
357/* Delete policy rule of previous chain, since cache doesn't contain
358 * chain policy rules.
359 * WARNING: This function has ugly design and relies on a lot of context, only
360 * to be called from specific places within the parser */
361static int __iptcc_p_del_policy(TC_HANDLE_T h, unsigned int num)
362{
363 if (h->chain_iterator_cur) {
364 /* policy rule is last rule */
365 struct rule_head *pr = (struct rule_head *)
366 h->chain_iterator_cur->rules.prev;
367
368 /* save verdict */
369 h->chain_iterator_cur->verdict =
370 *(int *)GET_TARGET(pr->entry)->data;
371
372 /* save counter and counter_map information */
373 h->chain_iterator_cur->counter_map.maptype =
374 COUNTER_MAP_NORMAL_MAP;
375 h->chain_iterator_cur->counter_map.mappos = num-1;
376 memcpy(&h->chain_iterator_cur->counters, &pr->entry->counters,
377 sizeof(h->chain_iterator_cur->counters));
378
379 /* foot_offset points to verdict rule */
380 h->chain_iterator_cur->foot_index = num;
381 h->chain_iterator_cur->foot_offset = pr->offset;
382
383 /* delete rule from cache */
384 iptcc_delete_rule(pr);
Martin Josefsson8d1b38a2004-09-22 21:00:19 +0000385 h->chain_iterator_cur->num_rules--;
Harald Welteaae69be2004-08-29 23:32:14 +0000386
387 return 1;
388 }
389 return 0;
390}
391
392/* Another ugly helper function split out of cache_add_entry to make it less
393 * spaghetti code */
394static void __iptcc_p_add_chain(TC_HANDLE_T h, struct chain_head *c,
395 unsigned int offset, unsigned int *num)
396{
397 __iptcc_p_del_policy(h, *num);
398
399 c->head_offset = offset;
400 c->index = *num;
401
402 list_add_tail(&c->list, &h->chains);
403 h->chain_iterator_cur = c;
404}
405
406/* main parser function: add an entry from the blob to the cache */
407static int cache_add_entry(STRUCT_ENTRY *e,
408 TC_HANDLE_T h,
409 STRUCT_ENTRY **prev,
410 unsigned int *num)
411{
412 unsigned int builtin;
413 unsigned int offset = (char *)e - (char *)h->entries->entrytable;
414
415 DEBUGP("entering...");
416
417 /* Last entry ("policy rule"). End it.*/
418 if (iptcb_entry2offset(h,e) + e->next_offset == h->entries->size) {
419 /* This is the ERROR node at the end of the chain */
420 DEBUGP_C("%u:%u: end of table:\n", *num, offset);
421
422 __iptcc_p_del_policy(h, *num);
423
424 h->chain_iterator_cur = NULL;
425 goto out_inc;
426 }
427
428 /* We know this is the start of a new chain if it's an ERROR
429 * target, or a hook entry point */
430
431 if (strcmp(GET_TARGET(e)->u.user.name, ERROR_TARGET) == 0) {
432 struct chain_head *c =
433 iptcc_alloc_chain_head((const char *)GET_TARGET(e)->data, 0);
434 DEBUGP_C("%u:%u:new userdefined chain %s: %p\n", *num, offset,
435 (char *)c->name, c);
436 if (!c) {
437 errno = -ENOMEM;
438 return -1;
439 }
440
441 __iptcc_p_add_chain(h, c, offset, num);
442
443 } else if ((builtin = iptcb_ent_is_hook_entry(e, h)) != 0) {
444 struct chain_head *c =
445 iptcc_alloc_chain_head((char *)hooknames[builtin-1],
446 builtin);
447 DEBUGP_C("%u:%u new builtin chain: %p (rules=%p)\n",
448 *num, offset, c, &c->rules);
449 if (!c) {
450 errno = -ENOMEM;
451 return -1;
452 }
453
454 c->hooknum = builtin;
455
456 __iptcc_p_add_chain(h, c, offset, num);
457
458 /* FIXME: this is ugly. */
459 goto new_rule;
460 } else {
461 /* has to be normal rule */
462 struct rule_head *r;
463new_rule:
464
465 if (!(r = iptcc_alloc_rule(h->chain_iterator_cur,
466 e->next_offset))) {
467 errno = ENOMEM;
468 return -1;
469 }
470 DEBUGP_C("%u:%u normal rule: %p: ", *num, offset, r);
471
472 r->index = *num;
473 r->offset = offset;
474 memcpy(r->entry, e, e->next_offset);
475 r->counter_map.maptype = COUNTER_MAP_NORMAL_MAP;
476 r->counter_map.mappos = r->index;
477
478 /* handling of jumps, etc. */
479 if (!strcmp(GET_TARGET(e)->u.user.name, STANDARD_TARGET)) {
480 STRUCT_STANDARD_TARGET *t;
481
482 t = (STRUCT_STANDARD_TARGET *)GET_TARGET(e);
483 if (t->target.u.target_size
484 != ALIGN(sizeof(STRUCT_STANDARD_TARGET))) {
485 errno = EINVAL;
486 return -1;
487 }
488
489 if (t->verdict < 0) {
490 DEBUGP_C("standard, verdict=%d\n", t->verdict);
491 r->type = IPTCC_R_STANDARD;
492 } else if (t->verdict == r->offset+e->next_offset) {
493 DEBUGP_C("fallthrough\n");
494 r->type = IPTCC_R_FALLTHROUGH;
495 } else {
496 DEBUGP_C("jump, target=%u\n", t->verdict);
497 r->type = IPTCC_R_JUMP;
498 /* Jump target fixup has to be deferred
499 * until second pass, since we migh not
500 * yet have parsed the target */
501 }
Martin Josefsson52c38022004-09-22 19:39:40 +0000502 } else {
503 DEBUGP_C("module, target=%s\n", GET_TARGET(e)->u.user.name);
504 r->type = IPTCC_R_MODULE;
Harald Welteaae69be2004-08-29 23:32:14 +0000505 }
506
507 list_add_tail(&r->list, &h->chain_iterator_cur->rules);
Martin Josefsson8d1b38a2004-09-22 21:00:19 +0000508 h->chain_iterator_cur->num_rules++;
Harald Welteaae69be2004-08-29 23:32:14 +0000509 }
510out_inc:
511 (*num)++;
512 return 0;
513}
514
515
516/* parse an iptables blob into it's pieces */
517static int parse_table(TC_HANDLE_T h)
518{
519 STRUCT_ENTRY *prev;
520 unsigned int num = 0;
521 struct chain_head *c;
522
523 /* First pass: over ruleset blob */
524 ENTRY_ITERATE(h->entries->entrytable, h->entries->size,
525 cache_add_entry, h, &prev, &num);
526
527 /* Second pass: fixup parsed data from first pass */
528 list_for_each_entry(c, &h->chains, list) {
529 struct rule_head *r;
530 list_for_each_entry(r, &c->rules, list) {
531 struct chain_head *c;
532 STRUCT_STANDARD_TARGET *t;
533
534 if (r->type != IPTCC_R_JUMP)
535 continue;
536
537 t = (STRUCT_STANDARD_TARGET *)GET_TARGET(r->entry);
538 c = iptcc_find_chain_by_offset(h, t->verdict);
539 if (!c)
540 return -1;
541 r->jump = c;
542 c->references++;
543 }
544 }
545
546 /* FIXME: sort chains */
547
548 return 1;
549}
550
551
552/**********************************************************************
553 * RULESET COMPILATION (cache -> blob)
554 **********************************************************************/
555
556/* Convenience structures */
557struct iptcb_chain_start{
558 STRUCT_ENTRY e;
559 struct ipt_error_target name;
560};
561#define IPTCB_CHAIN_START_SIZE (sizeof(STRUCT_ENTRY) + \
562 ALIGN(sizeof(struct ipt_error_target)))
563
564struct iptcb_chain_foot {
565 STRUCT_ENTRY e;
566 STRUCT_STANDARD_TARGET target;
567};
568#define IPTCB_CHAIN_FOOT_SIZE (sizeof(STRUCT_ENTRY) + \
569 ALIGN(sizeof(STRUCT_STANDARD_TARGET)))
570
571struct iptcb_chain_error {
572 STRUCT_ENTRY entry;
573 struct ipt_error_target target;
574};
575#define IPTCB_CHAIN_ERROR_SIZE (sizeof(STRUCT_ENTRY) + \
576 ALIGN(sizeof(struct ipt_error_target)))
577
578
579
580/* compile rule from cache into blob */
581static inline int iptcc_compile_rule (TC_HANDLE_T h, STRUCT_REPLACE *repl, struct rule_head *r)
582{
583 /* handle jumps */
584 if (r->type == IPTCC_R_JUMP) {
585 STRUCT_STANDARD_TARGET *t;
586 t = (STRUCT_STANDARD_TARGET *)GET_TARGET(r->entry);
587 /* memset for memcmp convenience on delete/replace */
588 memset(t->target.u.user.name, 0, FUNCTION_MAXNAMELEN);
589 strcpy(t->target.u.user.name, STANDARD_TARGET);
590 /* Jumps can only happen to builtin chains, so we
591 * can safely assume that they always have a header */
592 t->verdict = r->jump->head_offset + IPTCB_CHAIN_START_SIZE;
593 } else if (r->type == IPTCC_R_FALLTHROUGH) {
594 STRUCT_STANDARD_TARGET *t;
595 t = (STRUCT_STANDARD_TARGET *)GET_TARGET(r->entry);
596 t->verdict = r->offset + r->size;
597 }
598
599 /* copy entry from cache to blob */
600 memcpy((char *)repl->entries+r->offset, r->entry, r->size);
601
602 return 1;
603}
604
605/* compile chain from cache into blob */
606static int iptcc_compile_chain(TC_HANDLE_T h, STRUCT_REPLACE *repl, struct chain_head *c)
607{
608 int ret;
609 struct rule_head *r;
610 struct iptcb_chain_start *head;
611 struct iptcb_chain_foot *foot;
612
613 /* only user-defined chains have heaer */
614 if (!iptcc_is_builtin(c)) {
615 /* put chain header in place */
616 head = (void *)repl->entries + c->head_offset;
617 head->e.target_offset = sizeof(STRUCT_ENTRY);
618 head->e.next_offset = IPTCB_CHAIN_START_SIZE;
619 strcpy(head->name.t.u.user.name, ERROR_TARGET);
620 head->name.t.u.target_size =
621 ALIGN(sizeof(struct ipt_error_target));
622 strcpy(head->name.error, c->name);
623 } else {
624 repl->hook_entry[c->hooknum-1] = c->head_offset;
625 repl->underflow[c->hooknum-1] = c->foot_offset;
626 }
627
628 /* iterate over rules */
629 list_for_each_entry(r, &c->rules, list) {
630 ret = iptcc_compile_rule(h, repl, r);
631 if (ret < 0)
632 return ret;
633 }
634
635 /* put chain footer in place */
636 foot = (void *)repl->entries + c->foot_offset;
637 foot->e.target_offset = sizeof(STRUCT_ENTRY);
638 foot->e.next_offset = IPTCB_CHAIN_FOOT_SIZE;
639 strcpy(foot->target.target.u.user.name, STANDARD_TARGET);
640 foot->target.target.u.target_size =
641 ALIGN(sizeof(STRUCT_STANDARD_TARGET));
642 /* builtin targets have verdict, others return */
643 if (iptcc_is_builtin(c))
644 foot->target.verdict = c->verdict;
645 else
646 foot->target.verdict = RETURN;
647 /* set policy-counters */
648 memcpy(&foot->e.counters, &c->counters, sizeof(STRUCT_COUNTERS));
649
650 return 0;
651}
652
653/* calculate offset and number for every rule in the cache */
654static int iptcc_compile_chain_offsets(TC_HANDLE_T h, struct chain_head *c,
655 int *offset, int *num)
656{
657 struct rule_head *r;
658
659 c->head_offset = *offset;
660 DEBUGP("%s: chain_head %u, offset=%u\n", c->name, *num, *offset);
661
662 if (!iptcc_is_builtin(c)) {
663 /* Chain has header */
664 *offset += sizeof(STRUCT_ENTRY)
665 + ALIGN(sizeof(struct ipt_error_target));
666 (*num)++;
667 }
668
669 list_for_each_entry(r, &c->rules, list) {
670 DEBUGP("rule %u, offset=%u, index=%u\n", *num, *offset, *num);
671 r->offset = *offset;
672 r->index = *num;
673 *offset += r->size;
674 (*num)++;
675 }
676
677 DEBUGP("%s; chain_foot %u, offset=%u, index=%u\n", c->name, *num,
678 *offset, *num);
679 c->foot_offset = *offset;
680 c->foot_index = *num;
681 *offset += sizeof(STRUCT_ENTRY)
682 + ALIGN(sizeof(STRUCT_STANDARD_TARGET));
683 (*num)++;
684
685 return 1;
686}
687
688/* put the pieces back together again */
689static int iptcc_compile_table_prep(TC_HANDLE_T h, unsigned int *size)
690{
691 struct chain_head *c;
692 unsigned int offset = 0, num = 0;
693 int ret = 0;
694
695 /* First pass: calculate offset for every rule */
696 list_for_each_entry(c, &h->chains, list) {
697 ret = iptcc_compile_chain_offsets(h, c, &offset, &num);
698 if (ret < 0)
699 return ret;
700 }
701
702 /* Append one error rule at end of chain */
703 num++;
704 offset += sizeof(STRUCT_ENTRY)
705 + ALIGN(sizeof(struct ipt_error_target));
706
707 /* ruleset size is now in offset */
708 *size = offset;
709 return num;
710}
711
712static int iptcc_compile_table(TC_HANDLE_T h, STRUCT_REPLACE *repl)
713{
714 struct chain_head *c;
715 struct iptcb_chain_error *error;
716
717 /* Second pass: copy from cache to offsets, fill in jumps */
718 list_for_each_entry(c, &h->chains, list) {
719 int ret = iptcc_compile_chain(h, repl, c);
720 if (ret < 0)
721 return ret;
722 }
723
724 /* Append error rule at end of chain */
725 error = (void *)repl->entries + repl->size - IPTCB_CHAIN_ERROR_SIZE;
726 error->entry.target_offset = sizeof(STRUCT_ENTRY);
727 error->entry.next_offset = IPTCB_CHAIN_ERROR_SIZE;
728 error->target.t.u.user.target_size =
729 ALIGN(sizeof(struct ipt_error_target));
730 strcpy((char *)&error->target.t.u.user.name, ERROR_TARGET);
731 strcpy((char *)&error->target.error, "ERROR");
732
733 return 1;
734}
735
736/**********************************************************************
737 * EXTERNAL API (operates on cache only)
738 **********************************************************************/
Marc Bouchere6869a82000-03-20 06:03:29 +0000739
740/* Allocate handle of given size */
Rusty Russell79dee072000-05-02 16:45:16 +0000741static TC_HANDLE_T
Harald Welte0113fe72004-01-06 19:04:02 +0000742alloc_handle(const char *tablename, unsigned int size, unsigned int num_rules)
Marc Bouchere6869a82000-03-20 06:03:29 +0000743{
744 size_t len;
Rusty Russell79dee072000-05-02 16:45:16 +0000745 TC_HANDLE_T h;
Marc Bouchere6869a82000-03-20 06:03:29 +0000746
Harald Welteaae69be2004-08-29 23:32:14 +0000747 len = sizeof(STRUCT_TC_HANDLE) + size;
Marc Bouchere6869a82000-03-20 06:03:29 +0000748
Harald Welteaae69be2004-08-29 23:32:14 +0000749 h = malloc(sizeof(STRUCT_TC_HANDLE));
750 if (!h) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000751 errno = ENOMEM;
752 return NULL;
753 }
Harald Welteaae69be2004-08-29 23:32:14 +0000754 memset(h, 0, sizeof(*h));
755 INIT_LIST_HEAD(&h->chains);
Marc Bouchere6869a82000-03-20 06:03:29 +0000756 strcpy(h->info.name, tablename);
Harald Welteaae69be2004-08-29 23:32:14 +0000757
Harald Welte0371c0c2004-09-19 21:00:12 +0000758 h->entries = malloc(sizeof(STRUCT_GET_ENTRIES) + size);
Harald Welteaae69be2004-08-29 23:32:14 +0000759 if (!h->entries)
760 goto out_free_handle;
761
762 strcpy(h->entries->name, tablename);
Harald Welte0371c0c2004-09-19 21:00:12 +0000763 h->entries->size = size;
Marc Bouchere6869a82000-03-20 06:03:29 +0000764
765 return h;
Harald Welteaae69be2004-08-29 23:32:14 +0000766
767out_free_handle:
768 free(h);
769
770 return NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +0000771}
772
Harald Welteaae69be2004-08-29 23:32:14 +0000773
Rusty Russell79dee072000-05-02 16:45:16 +0000774TC_HANDLE_T
775TC_INIT(const char *tablename)
Marc Bouchere6869a82000-03-20 06:03:29 +0000776{
Rusty Russell79dee072000-05-02 16:45:16 +0000777 TC_HANDLE_T h;
778 STRUCT_GETINFO info;
Marc Bouchere6869a82000-03-20 06:03:29 +0000779 int tmp;
780 socklen_t s;
781
Rusty Russell79dee072000-05-02 16:45:16 +0000782 iptc_fn = TC_INIT;
Marc Bouchere6869a82000-03-20 06:03:29 +0000783
Martin Josefssone560fd62003-06-13 16:56:51 +0000784 if (sockfd != -1) {
Harald Welte366454b2002-01-07 13:46:50 +0000785 close(sockfd);
Martin Josefssone560fd62003-06-13 16:56:51 +0000786 sockfd = -1;
787 }
Harald Welte366454b2002-01-07 13:46:50 +0000788
Martin Josefsson841e4ae2003-05-02 15:30:11 +0000789 if (strlen(tablename) >= TABLE_MAXNAMELEN) {
790 errno = EINVAL;
791 return NULL;
792 }
793
Rusty Russell79dee072000-05-02 16:45:16 +0000794 sockfd = socket(TC_AF, SOCK_RAW, IPPROTO_RAW);
Marc Bouchere6869a82000-03-20 06:03:29 +0000795 if (sockfd < 0)
796 return NULL;
797
798 s = sizeof(info);
Martin Josefsson841e4ae2003-05-02 15:30:11 +0000799
Marc Bouchere6869a82000-03-20 06:03:29 +0000800 strcpy(info.name, tablename);
Rusty Russell79dee072000-05-02 16:45:16 +0000801 if (getsockopt(sockfd, TC_IPPROTO, SO_GET_INFO, &info, &s) < 0)
Marc Bouchere6869a82000-03-20 06:03:29 +0000802 return NULL;
803
Harald Welteaae69be2004-08-29 23:32:14 +0000804 DEBUGP("valid_hooks=0x%08x, num_entries=%u, size=%u\n",
805 info.valid_hooks, info.num_entries, info.size);
806
Harald Welte0113fe72004-01-06 19:04:02 +0000807 if ((h = alloc_handle(info.name, info.size, info.num_entries))
Martin Josefsson841e4ae2003-05-02 15:30:11 +0000808 == NULL) {
809 close(sockfd);
Martin Josefssone560fd62003-06-13 16:56:51 +0000810 sockfd = -1;
Marc Bouchere6869a82000-03-20 06:03:29 +0000811 return NULL;
Martin Josefsson841e4ae2003-05-02 15:30:11 +0000812 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000813
Marc Bouchere6869a82000-03-20 06:03:29 +0000814 /* Initialize current state */
815 h->info = info;
Harald Welte0113fe72004-01-06 19:04:02 +0000816
Harald Welteaae69be2004-08-29 23:32:14 +0000817 h->entries->size = h->info.size;
Marc Bouchere6869a82000-03-20 06:03:29 +0000818
Rusty Russell79dee072000-05-02 16:45:16 +0000819 tmp = sizeof(STRUCT_GET_ENTRIES) + h->info.size;
Marc Bouchere6869a82000-03-20 06:03:29 +0000820
Harald Welteaae69be2004-08-29 23:32:14 +0000821 if (getsockopt(sockfd, TC_IPPROTO, SO_GET_ENTRIES, h->entries,
822 &tmp) < 0)
823 goto error;
824
825#ifdef IPTC_DEBUG2
826 {
827 int fd = open("/tmp/libiptc-so_get_entries.blob",
828 O_CREAT|O_WRONLY);
829 if (fd >= 0) {
830 write(fd, h->entries, tmp);
831 close(fd);
832 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000833 }
Harald Welteaae69be2004-08-29 23:32:14 +0000834#endif
835
836 if (parse_table(h) < 0)
837 goto error;
Rusty Russell7e53bf92000-03-20 07:03:28 +0000838
Marc Bouchere6869a82000-03-20 06:03:29 +0000839 CHECK(h);
840 return h;
Harald Welteaae69be2004-08-29 23:32:14 +0000841error:
842 TC_FREE(&h);
843 return NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +0000844}
845
Martin Josefsson841e4ae2003-05-02 15:30:11 +0000846void
847TC_FREE(TC_HANDLE_T *h)
848{
Harald Welteaae69be2004-08-29 23:32:14 +0000849 struct chain_head *c, *tmp;
850
Martin Josefsson841e4ae2003-05-02 15:30:11 +0000851 close(sockfd);
Martin Josefssone560fd62003-06-13 16:56:51 +0000852 sockfd = -1;
Harald Welteaae69be2004-08-29 23:32:14 +0000853
854 list_for_each_entry_safe(c, tmp, &(*h)->chains, list) {
855 struct rule_head *r, *rtmp;
856
857 list_for_each_entry_safe(r, rtmp, &c->rules, list) {
858 free(r);
859 }
860
861 free(c);
862 }
863
864 free((*h)->entries);
Martin Josefsson841e4ae2003-05-02 15:30:11 +0000865 free(*h);
Harald Welteaae69be2004-08-29 23:32:14 +0000866
Martin Josefsson841e4ae2003-05-02 15:30:11 +0000867 *h = NULL;
868}
869
Marc Bouchere6869a82000-03-20 06:03:29 +0000870static inline int
Rusty Russell79dee072000-05-02 16:45:16 +0000871print_match(const STRUCT_ENTRY_MATCH *m)
Marc Bouchere6869a82000-03-20 06:03:29 +0000872{
Rusty Russell228e98d2000-04-27 10:28:06 +0000873 printf("Match name: `%s'\n", m->u.user.name);
Marc Bouchere6869a82000-03-20 06:03:29 +0000874 return 0;
875}
876
Rusty Russell79dee072000-05-02 16:45:16 +0000877static int dump_entry(STRUCT_ENTRY *e, const TC_HANDLE_T handle);
878
Marc Bouchere6869a82000-03-20 06:03:29 +0000879void
Rusty Russell79dee072000-05-02 16:45:16 +0000880TC_DUMP_ENTRIES(const TC_HANDLE_T handle)
Marc Bouchere6869a82000-03-20 06:03:29 +0000881{
882 CHECK(handle);
Harald Welteaae69be2004-08-29 23:32:14 +0000883#if 0
Marc Bouchere6869a82000-03-20 06:03:29 +0000884 printf("libiptc v%s. %u entries, %u bytes.\n",
Harald Welte80fe35d2002-05-29 13:08:15 +0000885 IPTABLES_VERSION,
Harald Welteaae69be2004-08-29 23:32:14 +0000886 handle->new_number, handle->entries->size);
Marc Bouchere6869a82000-03-20 06:03:29 +0000887 printf("Table `%s'\n", handle->info.name);
888 printf("Hooks: pre/in/fwd/out/post = %u/%u/%u/%u/%u\n",
Rusty Russell67088e72000-05-10 01:18:57 +0000889 handle->info.hook_entry[HOOK_PRE_ROUTING],
890 handle->info.hook_entry[HOOK_LOCAL_IN],
891 handle->info.hook_entry[HOOK_FORWARD],
892 handle->info.hook_entry[HOOK_LOCAL_OUT],
893 handle->info.hook_entry[HOOK_POST_ROUTING]);
Marc Bouchere6869a82000-03-20 06:03:29 +0000894 printf("Underflows: pre/in/fwd/out/post = %u/%u/%u/%u/%u\n",
Rusty Russell67088e72000-05-10 01:18:57 +0000895 handle->info.underflow[HOOK_PRE_ROUTING],
896 handle->info.underflow[HOOK_LOCAL_IN],
897 handle->info.underflow[HOOK_FORWARD],
898 handle->info.underflow[HOOK_LOCAL_OUT],
899 handle->info.underflow[HOOK_POST_ROUTING]);
Marc Bouchere6869a82000-03-20 06:03:29 +0000900
Harald Welteaae69be2004-08-29 23:32:14 +0000901 ENTRY_ITERATE(handle->entries->entrytable, handle->entries->size,
Rusty Russell79dee072000-05-02 16:45:16 +0000902 dump_entry, handle);
Harald Welteaae69be2004-08-29 23:32:14 +0000903#endif
Harald Welte0113fe72004-01-06 19:04:02 +0000904}
Rusty Russell30fd6e52000-04-23 09:16:06 +0000905
Marc Bouchere6869a82000-03-20 06:03:29 +0000906/* Does this chain exist? */
Rusty Russell79dee072000-05-02 16:45:16 +0000907int TC_IS_CHAIN(const char *chain, const TC_HANDLE_T handle)
Marc Bouchere6869a82000-03-20 06:03:29 +0000908{
Harald Welteaae69be2004-08-29 23:32:14 +0000909 return iptcc_find_label(chain, handle) != NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +0000910}
911
Harald Welteaae69be2004-08-29 23:32:14 +0000912static void iptcc_chain_iterator_advance(TC_HANDLE_T handle)
913{
914 struct chain_head *c = handle->chain_iterator_cur;
915
916 if (c->list.next == &handle->chains)
917 handle->chain_iterator_cur = NULL;
918 else
919 handle->chain_iterator_cur =
920 list_entry(c->list.next, struct chain_head, list);
921}
Marc Bouchere6869a82000-03-20 06:03:29 +0000922
Rusty Russell30fd6e52000-04-23 09:16:06 +0000923/* Iterator functions to run through the chains. */
Marc Bouchere6869a82000-03-20 06:03:29 +0000924const char *
Philip Blundell8c700902000-05-15 02:17:52 +0000925TC_FIRST_CHAIN(TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +0000926{
Harald Welteaae69be2004-08-29 23:32:14 +0000927 struct chain_head *c = list_entry((*handle)->chains.next,
928 struct chain_head, list);
929
930 iptc_fn = TC_FIRST_CHAIN;
931
932
933 if (list_empty(&(*handle)->chains)) {
934 DEBUGP(": no chains\n");
Harald Welte0113fe72004-01-06 19:04:02 +0000935 return NULL;
Harald Welteaae69be2004-08-29 23:32:14 +0000936 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000937
Harald Welteaae69be2004-08-29 23:32:14 +0000938 (*handle)->chain_iterator_cur = c;
939 iptcc_chain_iterator_advance(*handle);
Harald Welte0113fe72004-01-06 19:04:02 +0000940
Harald Welteaae69be2004-08-29 23:32:14 +0000941 DEBUGP(": returning `%s'\n", c->name);
942 return c->name;
Marc Bouchere6869a82000-03-20 06:03:29 +0000943}
944
Rusty Russell30fd6e52000-04-23 09:16:06 +0000945/* Iterator functions to run through the chains. Returns NULL at end. */
946const char *
Rusty Russell79dee072000-05-02 16:45:16 +0000947TC_NEXT_CHAIN(TC_HANDLE_T *handle)
Rusty Russell30fd6e52000-04-23 09:16:06 +0000948{
Harald Welteaae69be2004-08-29 23:32:14 +0000949 struct chain_head *c = (*handle)->chain_iterator_cur;
Rusty Russell30fd6e52000-04-23 09:16:06 +0000950
Harald Welteaae69be2004-08-29 23:32:14 +0000951 iptc_fn = TC_NEXT_CHAIN;
952
953 if (!c) {
954 DEBUGP(": no more chains\n");
Rusty Russell30fd6e52000-04-23 09:16:06 +0000955 return NULL;
Harald Welteaae69be2004-08-29 23:32:14 +0000956 }
Rusty Russell30fd6e52000-04-23 09:16:06 +0000957
Harald Welteaae69be2004-08-29 23:32:14 +0000958 iptcc_chain_iterator_advance(*handle);
959
960 DEBUGP(": returning `%s'\n", c->name);
961 return c->name;
Rusty Russell30fd6e52000-04-23 09:16:06 +0000962}
963
964/* Get first rule in the given chain: NULL for empty chain. */
Rusty Russell79dee072000-05-02 16:45:16 +0000965const STRUCT_ENTRY *
Philip Blundell8c700902000-05-15 02:17:52 +0000966TC_FIRST_RULE(const char *chain, TC_HANDLE_T *handle)
Rusty Russell30fd6e52000-04-23 09:16:06 +0000967{
Harald Welteaae69be2004-08-29 23:32:14 +0000968 struct chain_head *c;
969 struct rule_head *r;
Rusty Russell30fd6e52000-04-23 09:16:06 +0000970
Harald Welteaae69be2004-08-29 23:32:14 +0000971 iptc_fn = TC_FIRST_RULE;
972
973 DEBUGP("first rule(%s): ", chain);
974
975 c = iptcc_find_label(chain, *handle);
Rusty Russell30fd6e52000-04-23 09:16:06 +0000976 if (!c) {
977 errno = ENOENT;
978 return NULL;
979 }
980
981 /* Empty chain: single return/policy rule */
Harald Welteaae69be2004-08-29 23:32:14 +0000982 if (list_empty(&c->rules)) {
983 DEBUGP_C("no rules, returning NULL\n");
Rusty Russell30fd6e52000-04-23 09:16:06 +0000984 return NULL;
Harald Welteaae69be2004-08-29 23:32:14 +0000985 }
Rusty Russell30fd6e52000-04-23 09:16:06 +0000986
Harald Welteaae69be2004-08-29 23:32:14 +0000987 r = list_entry(c->rules.next, struct rule_head, list);
988 (*handle)->rule_iterator_cur = r;
989 DEBUGP_C("%p\n", r);
990
991 return r->entry;
Rusty Russell30fd6e52000-04-23 09:16:06 +0000992}
993
994/* Returns NULL when rules run out. */
Rusty Russell79dee072000-05-02 16:45:16 +0000995const STRUCT_ENTRY *
Philip Blundell8c700902000-05-15 02:17:52 +0000996TC_NEXT_RULE(const STRUCT_ENTRY *prev, TC_HANDLE_T *handle)
Rusty Russell30fd6e52000-04-23 09:16:06 +0000997{
Harald Welteaae69be2004-08-29 23:32:14 +0000998 struct rule_head *r;
Rusty Russell30fd6e52000-04-23 09:16:06 +0000999
Harald Welteaae69be2004-08-29 23:32:14 +00001000 DEBUGP("rule_iterator_cur=%p...", (*handle)->rule_iterator_cur);
1001
1002 if (!(*handle)->rule_iterator_cur) {
1003 DEBUGP_C("returning NULL\n");
1004 return NULL;
1005 }
1006
1007 r = list_entry((*handle)->rule_iterator_cur->list.next,
1008 struct rule_head, list);
1009
1010 iptc_fn = TC_NEXT_RULE;
1011
1012 DEBUGP_C("next=%p, head=%p...", &r->list,
1013 &(*handle)->rule_iterator_cur->chain->rules);
1014
1015 if (&r->list == &(*handle)->rule_iterator_cur->chain->rules) {
1016 (*handle)->rule_iterator_cur = NULL;
1017 DEBUGP_C("finished, returning NULL\n");
1018 return NULL;
1019 }
1020
1021 (*handle)->rule_iterator_cur = r;
1022
1023 /* NOTE: prev is without any influence ! */
1024 DEBUGP_C("returning rule %p\n", r);
1025 return r->entry;
Rusty Russell30fd6e52000-04-23 09:16:06 +00001026}
1027
Marc Bouchere6869a82000-03-20 06:03:29 +00001028/* How many rules in this chain? */
1029unsigned int
Rusty Russell79dee072000-05-02 16:45:16 +00001030TC_NUM_RULES(const char *chain, TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001031{
Harald Welteaae69be2004-08-29 23:32:14 +00001032 struct chain_head *c;
1033 iptc_fn = TC_NUM_RULES;
Marc Bouchere6869a82000-03-20 06:03:29 +00001034 CHECK(*handle);
Harald Welteaae69be2004-08-29 23:32:14 +00001035
1036 c = iptcc_find_label(chain, *handle);
1037 if (!c) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001038 errno = ENOENT;
1039 return (unsigned int)-1;
1040 }
Harald Welteaae69be2004-08-29 23:32:14 +00001041
1042 return c->num_rules;
Marc Bouchere6869a82000-03-20 06:03:29 +00001043}
1044
Rusty Russell79dee072000-05-02 16:45:16 +00001045const STRUCT_ENTRY *TC_GET_RULE(const char *chain,
1046 unsigned int n,
1047 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001048{
Harald Welteaae69be2004-08-29 23:32:14 +00001049 struct chain_head *c;
1050 struct rule_head *r;
1051
1052 iptc_fn = TC_GET_RULE;
Marc Bouchere6869a82000-03-20 06:03:29 +00001053
1054 CHECK(*handle);
Harald Welteaae69be2004-08-29 23:32:14 +00001055
1056 c = iptcc_find_label(chain, *handle);
1057 if (!c) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001058 errno = ENOENT;
1059 return NULL;
1060 }
1061
Harald Welteaae69be2004-08-29 23:32:14 +00001062 r = iptcc_get_rule_num(c, n);
1063 if (!r)
1064 return NULL;
1065 return r->entry;
Marc Bouchere6869a82000-03-20 06:03:29 +00001066}
1067
1068/* Returns a pointer to the target name of this position. */
Harald Welteaae69be2004-08-29 23:32:14 +00001069const char *standard_target_map(int verdict)
Marc Bouchere6869a82000-03-20 06:03:29 +00001070{
Harald Welteaae69be2004-08-29 23:32:14 +00001071 switch (verdict) {
1072 case RETURN:
1073 return LABEL_RETURN;
1074 break;
1075 case -NF_ACCEPT-1:
1076 return LABEL_ACCEPT;
1077 break;
1078 case -NF_DROP-1:
1079 return LABEL_DROP;
1080 break;
1081 case -NF_QUEUE-1:
1082 return LABEL_QUEUE;
1083 break;
1084 default:
1085 fprintf(stderr, "ERROR: %d not a valid target)\n",
1086 verdict);
1087 abort();
1088 break;
1089 }
1090 /* not reached */
1091 return NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +00001092}
1093
Harald Welteaae69be2004-08-29 23:32:14 +00001094/* Returns a pointer to the target name of this position. */
1095const char *TC_GET_TARGET(const STRUCT_ENTRY *ce,
1096 TC_HANDLE_T *handle)
1097{
1098 STRUCT_ENTRY *e = (STRUCT_ENTRY *)ce;
1099 struct rule_head *r = container_of(e, struct rule_head, entry);
1100
1101 iptc_fn = TC_GET_TARGET;
1102
1103 switch(r->type) {
1104 int spos;
1105 case IPTCC_R_FALLTHROUGH:
1106 return "";
1107 break;
1108 case IPTCC_R_JUMP:
1109 DEBUGP("r=%p, jump=%p, name=`%s'\n", r, r->jump, r->jump->name);
1110 return r->jump->name;
1111 break;
1112 case IPTCC_R_STANDARD:
1113 spos = *(int *)GET_TARGET(e)->data;
1114 DEBUGP("r=%p, spos=%d'\n", r, spos);
1115 return standard_target_map(spos);
1116 break;
1117 case IPTCC_R_MODULE:
1118 return GET_TARGET(e)->u.user.name;
1119 break;
1120 }
1121 return NULL;
1122}
Marc Bouchere6869a82000-03-20 06:03:29 +00001123/* Is this a built-in chain? Actually returns hook + 1. */
1124int
Rusty Russell79dee072000-05-02 16:45:16 +00001125TC_BUILTIN(const char *chain, const TC_HANDLE_T handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001126{
Harald Welteaae69be2004-08-29 23:32:14 +00001127 struct chain_head *c;
1128
1129 iptc_fn = TC_BUILTIN;
Marc Bouchere6869a82000-03-20 06:03:29 +00001130
Harald Welteaae69be2004-08-29 23:32:14 +00001131 c = iptcc_find_label(chain, handle);
1132 if (!c) {
1133 errno = ENOENT;
1134 return -1;
Marc Bouchere6869a82000-03-20 06:03:29 +00001135 }
Harald Welteaae69be2004-08-29 23:32:14 +00001136
1137 return iptcc_is_builtin(c);
Marc Bouchere6869a82000-03-20 06:03:29 +00001138}
1139
1140/* Get the policy of a given built-in chain */
1141const char *
Rusty Russell79dee072000-05-02 16:45:16 +00001142TC_GET_POLICY(const char *chain,
1143 STRUCT_COUNTERS *counters,
1144 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001145{
Harald Welteaae69be2004-08-29 23:32:14 +00001146 struct chain_head *c;
Marc Bouchere6869a82000-03-20 06:03:29 +00001147
Harald Welteaae69be2004-08-29 23:32:14 +00001148 iptc_fn = TC_GET_POLICY;
1149
1150 DEBUGP("called for chain %s\n", chain);
1151
1152 c = iptcc_find_label(chain, *handle);
1153 if (!c) {
1154 errno = ENOENT;
1155 return NULL;
1156 }
1157
1158 if (!iptcc_is_builtin(c))
Marc Bouchere6869a82000-03-20 06:03:29 +00001159 return NULL;
1160
Harald Welteaae69be2004-08-29 23:32:14 +00001161 *counters = c->counters;
Marc Bouchere6869a82000-03-20 06:03:29 +00001162
Harald Welteaae69be2004-08-29 23:32:14 +00001163 return standard_target_map(c->verdict);
Harald Welte0113fe72004-01-06 19:04:02 +00001164}
1165
1166static int
Harald Welteaae69be2004-08-29 23:32:14 +00001167iptcc_standard_map(struct rule_head *r, int verdict)
Harald Welte0113fe72004-01-06 19:04:02 +00001168{
Harald Welteaae69be2004-08-29 23:32:14 +00001169 STRUCT_ENTRY *e = r->entry;
Rusty Russell79dee072000-05-02 16:45:16 +00001170 STRUCT_STANDARD_TARGET *t;
Marc Bouchere6869a82000-03-20 06:03:29 +00001171
Rusty Russell79dee072000-05-02 16:45:16 +00001172 t = (STRUCT_STANDARD_TARGET *)GET_TARGET(e);
Marc Bouchere6869a82000-03-20 06:03:29 +00001173
Rusty Russell67088e72000-05-10 01:18:57 +00001174 if (t->target.u.target_size
Philip Blundell8c700902000-05-15 02:17:52 +00001175 != ALIGN(sizeof(STRUCT_STANDARD_TARGET))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001176 errno = EINVAL;
1177 return 0;
1178 }
1179 /* memset for memcmp convenience on delete/replace */
Rusty Russell79dee072000-05-02 16:45:16 +00001180 memset(t->target.u.user.name, 0, FUNCTION_MAXNAMELEN);
1181 strcpy(t->target.u.user.name, STANDARD_TARGET);
Marc Bouchere6869a82000-03-20 06:03:29 +00001182 t->verdict = verdict;
1183
Harald Welteaae69be2004-08-29 23:32:14 +00001184 r->type = IPTCC_R_STANDARD;
1185
Marc Bouchere6869a82000-03-20 06:03:29 +00001186 return 1;
1187}
Rusty Russell7e53bf92000-03-20 07:03:28 +00001188
Marc Bouchere6869a82000-03-20 06:03:29 +00001189static int
Harald Welteaae69be2004-08-29 23:32:14 +00001190iptcc_map_target(const TC_HANDLE_T handle,
1191 struct rule_head *r)
Marc Bouchere6869a82000-03-20 06:03:29 +00001192{
Harald Welteaae69be2004-08-29 23:32:14 +00001193 STRUCT_ENTRY *e = r->entry;
Harald Welte0113fe72004-01-06 19:04:02 +00001194 STRUCT_ENTRY_TARGET *t = GET_TARGET(e);
Marc Bouchere6869a82000-03-20 06:03:29 +00001195
Marc Bouchere6869a82000-03-20 06:03:29 +00001196 /* Maybe it's empty (=> fall through) */
Harald Welteaae69be2004-08-29 23:32:14 +00001197 if (strcmp(t->u.user.name, "") == 0) {
1198 r->type = IPTCC_R_FALLTHROUGH;
1199 return 1;
1200 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001201 /* Maybe it's a standard target name... */
Rusty Russell79dee072000-05-02 16:45:16 +00001202 else if (strcmp(t->u.user.name, LABEL_ACCEPT) == 0)
Harald Welteaae69be2004-08-29 23:32:14 +00001203 return iptcc_standard_map(r, -NF_ACCEPT - 1);
Rusty Russell79dee072000-05-02 16:45:16 +00001204 else if (strcmp(t->u.user.name, LABEL_DROP) == 0)
Harald Welteaae69be2004-08-29 23:32:14 +00001205 return iptcc_standard_map(r, -NF_DROP - 1);
Rusty Russell67088e72000-05-10 01:18:57 +00001206 else if (strcmp(t->u.user.name, LABEL_QUEUE) == 0)
Harald Welteaae69be2004-08-29 23:32:14 +00001207 return iptcc_standard_map(r, -NF_QUEUE - 1);
Rusty Russell79dee072000-05-02 16:45:16 +00001208 else if (strcmp(t->u.user.name, LABEL_RETURN) == 0)
Harald Welteaae69be2004-08-29 23:32:14 +00001209 return iptcc_standard_map(r, RETURN);
Rusty Russell79dee072000-05-02 16:45:16 +00001210 else if (TC_BUILTIN(t->u.user.name, handle)) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001211 /* Can't jump to builtins. */
1212 errno = EINVAL;
1213 return 0;
1214 } else {
1215 /* Maybe it's an existing chain name. */
Harald Welteaae69be2004-08-29 23:32:14 +00001216 struct chain_head *c;
1217 DEBUGP("trying to find chain `%s': ", t->u.user.name);
Marc Bouchere6869a82000-03-20 06:03:29 +00001218
Harald Welteaae69be2004-08-29 23:32:14 +00001219 c = iptcc_find_label(t->u.user.name, handle);
1220 if (c) {
1221 DEBUGP_C("found!\n");
1222 r->type = IPTCC_R_JUMP;
1223 r->jump = c;
1224 c->references++;
1225 return 1;
1226 }
1227 DEBUGP_C("not found :(\n");
Marc Bouchere6869a82000-03-20 06:03:29 +00001228 }
1229
1230 /* Must be a module? If not, kernel will reject... */
1231 /* memset to all 0 for your memcmp convenience. */
Rusty Russell228e98d2000-04-27 10:28:06 +00001232 memset(t->u.user.name + strlen(t->u.user.name),
Marc Bouchere6869a82000-03-20 06:03:29 +00001233 0,
Rusty Russell79dee072000-05-02 16:45:16 +00001234 FUNCTION_MAXNAMELEN - strlen(t->u.user.name));
Harald Welteaae69be2004-08-29 23:32:14 +00001235
1236 set_changed(handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001237 return 1;
1238}
1239
Harald Welte0113fe72004-01-06 19:04:02 +00001240/* Insert the entry `fw' in chain `chain' into position `rulenum'. */
Marc Bouchere6869a82000-03-20 06:03:29 +00001241int
Rusty Russell79dee072000-05-02 16:45:16 +00001242TC_INSERT_ENTRY(const IPT_CHAINLABEL chain,
1243 const STRUCT_ENTRY *e,
1244 unsigned int rulenum,
1245 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001246{
Harald Welteaae69be2004-08-29 23:32:14 +00001247 struct chain_head *c;
1248 struct rule_head *r, *prev;
Marc Bouchere6869a82000-03-20 06:03:29 +00001249
Rusty Russell79dee072000-05-02 16:45:16 +00001250 iptc_fn = TC_INSERT_ENTRY;
Harald Welteaae69be2004-08-29 23:32:14 +00001251
1252 if (!(c = iptcc_find_label(chain, *handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001253 errno = ENOENT;
1254 return 0;
1255 }
1256
Harald Welteaae69be2004-08-29 23:32:14 +00001257 prev = iptcc_get_rule_num(c, rulenum);
1258 if (!prev) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001259 errno = E2BIG;
1260 return 0;
1261 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001262
Harald Welteaae69be2004-08-29 23:32:14 +00001263 if (!(r = iptcc_alloc_rule(c, e->next_offset))) {
1264 errno = ENOMEM;
Harald Welte0113fe72004-01-06 19:04:02 +00001265 return 0;
Harald Welteaae69be2004-08-29 23:32:14 +00001266 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001267
Harald Welteaae69be2004-08-29 23:32:14 +00001268 memcpy(r->entry, e, e->next_offset);
1269 r->counter_map.maptype = COUNTER_MAP_SET;
1270
1271 if (!iptcc_map_target(*handle, r)) {
1272 free(r);
1273 return 0;
1274 }
1275
1276 list_add_tail(&r->list, &prev->list);
1277 c->num_rules++;
1278
1279 set_changed(*handle);
1280
1281 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00001282}
1283
1284/* Atomically replace rule `rulenum' in `chain' with `fw'. */
1285int
Rusty Russell79dee072000-05-02 16:45:16 +00001286TC_REPLACE_ENTRY(const IPT_CHAINLABEL chain,
1287 const STRUCT_ENTRY *e,
1288 unsigned int rulenum,
1289 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001290{
Harald Welteaae69be2004-08-29 23:32:14 +00001291 struct chain_head *c;
1292 struct rule_head *r, *old;
Marc Bouchere6869a82000-03-20 06:03:29 +00001293
Rusty Russell79dee072000-05-02 16:45:16 +00001294 iptc_fn = TC_REPLACE_ENTRY;
Marc Bouchere6869a82000-03-20 06:03:29 +00001295
Harald Welteaae69be2004-08-29 23:32:14 +00001296 if (!(c = iptcc_find_label(chain, *handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001297 errno = ENOENT;
1298 return 0;
1299 }
1300
Harald Welteaae69be2004-08-29 23:32:14 +00001301 if (!(old = iptcc_get_rule_num(c, rulenum))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001302 errno = E2BIG;
1303 return 0;
1304 }
1305
Harald Welteaae69be2004-08-29 23:32:14 +00001306 if (!(r = iptcc_alloc_rule(c, e->next_offset))) {
1307 errno = ENOMEM;
Marc Bouchere6869a82000-03-20 06:03:29 +00001308 return 0;
Harald Welteaae69be2004-08-29 23:32:14 +00001309 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001310
Harald Welteaae69be2004-08-29 23:32:14 +00001311 memcpy(r->entry, e, e->next_offset);
1312 r->counter_map.maptype = COUNTER_MAP_SET;
1313
1314 if (!iptcc_map_target(*handle, r)) {
1315 free(r);
Harald Welte0113fe72004-01-06 19:04:02 +00001316 return 0;
Harald Welteaae69be2004-08-29 23:32:14 +00001317 }
Harald Welte0113fe72004-01-06 19:04:02 +00001318
Harald Welteaae69be2004-08-29 23:32:14 +00001319 list_add(&r->list, &old->list);
1320 iptcc_delete_rule(old);
1321
1322 set_changed(*handle);
1323
1324 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00001325}
1326
Harald Welte0113fe72004-01-06 19:04:02 +00001327/* Append entry `fw' to chain `chain'. Equivalent to insert with
Marc Bouchere6869a82000-03-20 06:03:29 +00001328 rulenum = length of chain. */
1329int
Rusty Russell79dee072000-05-02 16:45:16 +00001330TC_APPEND_ENTRY(const IPT_CHAINLABEL chain,
1331 const STRUCT_ENTRY *e,
1332 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001333{
Harald Welteaae69be2004-08-29 23:32:14 +00001334 struct chain_head *c;
1335 struct rule_head *r;
Marc Bouchere6869a82000-03-20 06:03:29 +00001336
Rusty Russell79dee072000-05-02 16:45:16 +00001337 iptc_fn = TC_APPEND_ENTRY;
Harald Welteaae69be2004-08-29 23:32:14 +00001338 if (!(c = iptcc_find_label(chain, *handle))) {
1339 DEBUGP("unable to find chain `%s'\n", chain);
Marc Bouchere6869a82000-03-20 06:03:29 +00001340 errno = ENOENT;
1341 return 0;
1342 }
1343
Harald Welteaae69be2004-08-29 23:32:14 +00001344 if (!(r = iptcc_alloc_rule(c, e->next_offset))) {
1345 DEBUGP("unable to allocate rule for chain `%s'\n", chain);
1346 errno = ENOMEM;
Harald Welte0113fe72004-01-06 19:04:02 +00001347 return 0;
Harald Welteaae69be2004-08-29 23:32:14 +00001348 }
Harald Welte0113fe72004-01-06 19:04:02 +00001349
Harald Welteaae69be2004-08-29 23:32:14 +00001350 memcpy(r->entry, e, e->next_offset);
1351 r->counter_map.maptype = COUNTER_MAP_SET;
1352
1353 if (!iptcc_map_target(*handle, r)) {
1354 DEBUGP("unable to ma target of rule for chain `%s'\n", chain);
1355 free(r);
1356 return 0;
1357 }
1358
1359 list_add_tail(&r->list, &c->rules);
1360 c->num_rules++;
1361
1362 set_changed(*handle);
1363
1364 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00001365}
1366
1367static inline int
Rusty Russell79dee072000-05-02 16:45:16 +00001368match_different(const STRUCT_ENTRY_MATCH *a,
Rusty Russelledf14cf2000-04-19 11:26:44 +00001369 const unsigned char *a_elems,
1370 const unsigned char *b_elems,
1371 unsigned char **maskptr)
Marc Bouchere6869a82000-03-20 06:03:29 +00001372{
Rusty Russell79dee072000-05-02 16:45:16 +00001373 const STRUCT_ENTRY_MATCH *b;
Rusty Russelledf14cf2000-04-19 11:26:44 +00001374 unsigned int i;
Marc Bouchere6869a82000-03-20 06:03:29 +00001375
1376 /* Offset of b is the same as a. */
Rusty Russell30fd6e52000-04-23 09:16:06 +00001377 b = (void *)b_elems + ((unsigned char *)a - a_elems);
Marc Bouchere6869a82000-03-20 06:03:29 +00001378
Rusty Russell228e98d2000-04-27 10:28:06 +00001379 if (a->u.match_size != b->u.match_size)
Marc Bouchere6869a82000-03-20 06:03:29 +00001380 return 1;
1381
Rusty Russell228e98d2000-04-27 10:28:06 +00001382 if (strcmp(a->u.user.name, b->u.user.name) != 0)
Marc Bouchere6869a82000-03-20 06:03:29 +00001383 return 1;
1384
Rusty Russell73ef09b2000-07-03 10:24:04 +00001385 *maskptr += ALIGN(sizeof(*a));
Rusty Russelledf14cf2000-04-19 11:26:44 +00001386
Rusty Russell73ef09b2000-07-03 10:24:04 +00001387 for (i = 0; i < a->u.match_size - ALIGN(sizeof(*a)); i++)
Rusty Russelledf14cf2000-04-19 11:26:44 +00001388 if (((a->data[i] ^ b->data[i]) & (*maskptr)[i]) != 0)
Rusty Russell90e712a2000-03-29 04:19:26 +00001389 return 1;
Rusty Russelledf14cf2000-04-19 11:26:44 +00001390 *maskptr += i;
1391 return 0;
1392}
1393
1394static inline int
1395target_different(const unsigned char *a_targdata,
1396 const unsigned char *b_targdata,
1397 unsigned int tdatasize,
1398 const unsigned char *mask)
1399{
1400 unsigned int i;
1401 for (i = 0; i < tdatasize; i++)
1402 if (((a_targdata[i] ^ b_targdata[i]) & mask[i]) != 0)
1403 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00001404
1405 return 0;
1406}
1407
Rusty Russell79dee072000-05-02 16:45:16 +00001408static int
1409is_same(const STRUCT_ENTRY *a,
1410 const STRUCT_ENTRY *b,
1411 unsigned char *matchmask);
Marc Bouchere6869a82000-03-20 06:03:29 +00001412
Harald Welte0113fe72004-01-06 19:04:02 +00001413/* Delete the first rule in `chain' which matches `fw'. */
Marc Bouchere6869a82000-03-20 06:03:29 +00001414int
Rusty Russell79dee072000-05-02 16:45:16 +00001415TC_DELETE_ENTRY(const IPT_CHAINLABEL chain,
1416 const STRUCT_ENTRY *origfw,
1417 unsigned char *matchmask,
1418 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001419{
Harald Welteaae69be2004-08-29 23:32:14 +00001420 struct chain_head *c;
Harald Weltefe537072004-08-30 20:28:53 +00001421 struct rule_head *r;
Harald Welte0113fe72004-01-06 19:04:02 +00001422 STRUCT_ENTRY *e, *fw;
Marc Bouchere6869a82000-03-20 06:03:29 +00001423
Rusty Russell79dee072000-05-02 16:45:16 +00001424 iptc_fn = TC_DELETE_ENTRY;
Harald Welteaae69be2004-08-29 23:32:14 +00001425 if (!(c = iptcc_find_label(chain, *handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001426 errno = ENOENT;
1427 return 0;
1428 }
1429
Harald Welte0113fe72004-01-06 19:04:02 +00001430 fw = malloc(origfw->next_offset);
1431 if (fw == NULL) {
1432 errno = ENOMEM;
1433 return 0;
1434 }
1435
Harald Weltefe537072004-08-30 20:28:53 +00001436 list_for_each_entry(r, &c->rules, list) {
Harald Welte0113fe72004-01-06 19:04:02 +00001437
1438 memcpy(fw, origfw, origfw->next_offset);
1439
Harald Weltefe537072004-08-30 20:28:53 +00001440#if 0
Harald Welte0113fe72004-01-06 19:04:02 +00001441 /* FIXME: handle this in is_same --RR */
1442 if (!map_target(*handle, fw, offset, &discard)) {
1443 free(fw);
1444 return 0;
1445 }
Harald Welte0113fe72004-01-06 19:04:02 +00001446#endif
Harald Weltefe537072004-08-30 20:28:53 +00001447
1448 e = r->entry;
1449
Harald Welte0113fe72004-01-06 19:04:02 +00001450 if (is_same(e, fw, matchmask)) {
Harald Weltefe537072004-08-30 20:28:53 +00001451 /* If we are about to delete the rule that is the
1452 * current iterator, move rule iterator back. next
1453 * pointer will then point to real next node */
1454 if (r == (*handle)->rule_iterator_cur) {
1455 (*handle)->rule_iterator_cur =
1456 list_entry((*handle)->rule_iterator_cur->list.prev,
1457 struct rule_head, list);
1458 }
1459
1460 c->num_rules--;
1461 iptcc_delete_rule(r);
1462 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00001463 }
1464 }
1465
Harald Welte0113fe72004-01-06 19:04:02 +00001466 free(fw);
Marc Bouchere6869a82000-03-20 06:03:29 +00001467 errno = ENOENT;
1468 return 0;
Rusty Russell7e53bf92000-03-20 07:03:28 +00001469}
Harald Welteaae69be2004-08-29 23:32:14 +00001470
Marc Bouchere6869a82000-03-20 06:03:29 +00001471
1472/* Delete the rule in position `rulenum' in `chain'. */
1473int
Rusty Russell79dee072000-05-02 16:45:16 +00001474TC_DELETE_NUM_ENTRY(const IPT_CHAINLABEL chain,
1475 unsigned int rulenum,
1476 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001477{
Harald Welteaae69be2004-08-29 23:32:14 +00001478 struct chain_head *c;
1479 struct rule_head *r;
Marc Bouchere6869a82000-03-20 06:03:29 +00001480
Rusty Russell79dee072000-05-02 16:45:16 +00001481 iptc_fn = TC_DELETE_NUM_ENTRY;
Harald Welteaae69be2004-08-29 23:32:14 +00001482
1483 if (!(c = iptcc_find_label(chain, *handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001484 errno = ENOENT;
1485 return 0;
1486 }
1487
Harald Welteaae69be2004-08-29 23:32:14 +00001488 if (!(r = iptcc_get_rule_num(c, rulenum))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001489 errno = E2BIG;
1490 return 0;
1491 }
1492
Harald Welteaae69be2004-08-29 23:32:14 +00001493 /* If we are about to delete the rule that is the current
1494 * iterator, move rule iterator back. next pointer will then
1495 * point to real next node */
1496 if (r == (*handle)->rule_iterator_cur) {
1497 (*handle)->rule_iterator_cur =
1498 list_entry((*handle)->rule_iterator_cur->list.prev,
1499 struct rule_head, list);
Harald Welte0113fe72004-01-06 19:04:02 +00001500 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001501
Harald Welteaae69be2004-08-29 23:32:14 +00001502 c->num_rules--;
1503 iptcc_delete_rule(r);
1504
1505 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00001506}
1507
1508/* Check the packet `fw' on chain `chain'. Returns the verdict, or
1509 NULL and sets errno. */
1510const char *
Rusty Russell79dee072000-05-02 16:45:16 +00001511TC_CHECK_PACKET(const IPT_CHAINLABEL chain,
1512 STRUCT_ENTRY *entry,
1513 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001514{
1515 errno = ENOSYS;
1516 return NULL;
1517}
1518
1519/* Flushes the entries in the given chain (ie. empties chain). */
1520int
Rusty Russell79dee072000-05-02 16:45:16 +00001521TC_FLUSH_ENTRIES(const IPT_CHAINLABEL chain, TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001522{
Harald Welteaae69be2004-08-29 23:32:14 +00001523 struct chain_head *c;
1524 struct rule_head *r, *tmp;
Marc Bouchere6869a82000-03-20 06:03:29 +00001525
Harald Welte0113fe72004-01-06 19:04:02 +00001526 iptc_fn = TC_FLUSH_ENTRIES;
Harald Welteaae69be2004-08-29 23:32:14 +00001527 if (!(c = iptcc_find_label(chain, *handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001528 errno = ENOENT;
1529 return 0;
1530 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001531
Harald Welteaae69be2004-08-29 23:32:14 +00001532 list_for_each_entry_safe(r, tmp, &c->rules, list) {
1533 iptcc_delete_rule(r);
1534 }
1535
1536 c->num_rules = 0;
1537
1538 set_changed(*handle);
1539
1540 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00001541}
1542
1543/* Zeroes the counters in a chain. */
1544int
Rusty Russell79dee072000-05-02 16:45:16 +00001545TC_ZERO_ENTRIES(const IPT_CHAINLABEL chain, TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001546{
Harald Welteaae69be2004-08-29 23:32:14 +00001547 struct chain_head *c;
1548 struct rule_head *r;
Rusty Russell7e53bf92000-03-20 07:03:28 +00001549
Harald Welteaae69be2004-08-29 23:32:14 +00001550 if (!(c = iptcc_find_label(chain, *handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001551 errno = ENOENT;
1552 return 0;
1553 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001554
Harald Welteaae69be2004-08-29 23:32:14 +00001555 list_for_each_entry(r, &c->rules, list) {
1556 if (r->counter_map.maptype == COUNTER_MAP_NORMAL_MAP)
1557 r->counter_map.maptype = COUNTER_MAP_ZEROED;
Marc Bouchere6869a82000-03-20 06:03:29 +00001558 }
Harald Welteaae69be2004-08-29 23:32:14 +00001559
Rusty Russell175f6412000-03-24 09:32:20 +00001560 set_changed(*handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001561
Marc Bouchere6869a82000-03-20 06:03:29 +00001562 return 1;
1563}
1564
Harald Welte1cef74d2001-01-05 15:22:59 +00001565STRUCT_COUNTERS *
1566TC_READ_COUNTER(const IPT_CHAINLABEL chain,
1567 unsigned int rulenum,
1568 TC_HANDLE_T *handle)
1569{
Harald Welteaae69be2004-08-29 23:32:14 +00001570 struct chain_head *c;
1571 struct rule_head *r;
Harald Welte1cef74d2001-01-05 15:22:59 +00001572
1573 iptc_fn = TC_READ_COUNTER;
1574 CHECK(*handle);
1575
Harald Welteaae69be2004-08-29 23:32:14 +00001576 if (!(c = iptcc_find_label(chain, *handle))) {
Harald Welte1cef74d2001-01-05 15:22:59 +00001577 errno = ENOENT;
1578 return NULL;
1579 }
1580
Harald Welteaae69be2004-08-29 23:32:14 +00001581 if (!(r = iptcc_get_rule_num(c, rulenum))) {
Harald Welte0113fe72004-01-06 19:04:02 +00001582 errno = E2BIG;
1583 return NULL;
1584 }
1585
Harald Welteaae69be2004-08-29 23:32:14 +00001586 return &r->entry[0].counters;
Harald Welte1cef74d2001-01-05 15:22:59 +00001587}
1588
1589int
1590TC_ZERO_COUNTER(const IPT_CHAINLABEL chain,
1591 unsigned int rulenum,
1592 TC_HANDLE_T *handle)
1593{
Harald Welteaae69be2004-08-29 23:32:14 +00001594 struct chain_head *c;
1595 struct rule_head *r;
Harald Welte1cef74d2001-01-05 15:22:59 +00001596
1597 iptc_fn = TC_ZERO_COUNTER;
1598 CHECK(*handle);
1599
Harald Welteaae69be2004-08-29 23:32:14 +00001600 if (!(c = iptcc_find_label(chain, *handle))) {
Harald Welte1cef74d2001-01-05 15:22:59 +00001601 errno = ENOENT;
1602 return 0;
1603 }
1604
Harald Welteaae69be2004-08-29 23:32:14 +00001605 if (!(r = iptcc_get_rule_num(c, rulenum))) {
Harald Welte0113fe72004-01-06 19:04:02 +00001606 errno = E2BIG;
1607 return 0;
1608 }
1609
Harald Welteaae69be2004-08-29 23:32:14 +00001610 if (r->counter_map.maptype == COUNTER_MAP_NORMAL_MAP)
1611 r->counter_map.maptype = COUNTER_MAP_ZEROED;
Harald Welte1cef74d2001-01-05 15:22:59 +00001612
1613 set_changed(*handle);
1614
1615 return 1;
1616}
1617
1618int
1619TC_SET_COUNTER(const IPT_CHAINLABEL chain,
1620 unsigned int rulenum,
1621 STRUCT_COUNTERS *counters,
1622 TC_HANDLE_T *handle)
1623{
Harald Welteaae69be2004-08-29 23:32:14 +00001624 struct chain_head *c;
1625 struct rule_head *r;
Harald Welte1cef74d2001-01-05 15:22:59 +00001626 STRUCT_ENTRY *e;
Harald Welte1cef74d2001-01-05 15:22:59 +00001627
1628 iptc_fn = TC_SET_COUNTER;
1629 CHECK(*handle);
1630
Harald Welteaae69be2004-08-29 23:32:14 +00001631 if (!(c = iptcc_find_label(chain, *handle))) {
Harald Welte1cef74d2001-01-05 15:22:59 +00001632 errno = ENOENT;
1633 return 0;
1634 }
Harald Welte0113fe72004-01-06 19:04:02 +00001635
Harald Welteaae69be2004-08-29 23:32:14 +00001636 if (!(r = iptcc_get_rule_num(c, rulenum))) {
Harald Welte0113fe72004-01-06 19:04:02 +00001637 errno = E2BIG;
1638 return 0;
1639 }
1640
Harald Welteaae69be2004-08-29 23:32:14 +00001641 e = r->entry;
1642 r->counter_map.maptype = COUNTER_MAP_SET;
Harald Welte0113fe72004-01-06 19:04:02 +00001643
1644 memcpy(&e->counters, counters, sizeof(STRUCT_COUNTERS));
Harald Welte1cef74d2001-01-05 15:22:59 +00001645
1646 set_changed(*handle);
1647
1648 return 1;
1649}
1650
Marc Bouchere6869a82000-03-20 06:03:29 +00001651/* Creates a new chain. */
1652/* To create a chain, create two rules: error node and unconditional
1653 * return. */
1654int
Rusty Russell79dee072000-05-02 16:45:16 +00001655TC_CREATE_CHAIN(const IPT_CHAINLABEL chain, TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001656{
Harald Welteaae69be2004-08-29 23:32:14 +00001657 static struct chain_head *c;
Marc Bouchere6869a82000-03-20 06:03:29 +00001658
Rusty Russell79dee072000-05-02 16:45:16 +00001659 iptc_fn = TC_CREATE_CHAIN;
Marc Bouchere6869a82000-03-20 06:03:29 +00001660
1661 /* find_label doesn't cover built-in targets: DROP, ACCEPT,
1662 QUEUE, RETURN. */
Harald Welteaae69be2004-08-29 23:32:14 +00001663 if (iptcc_find_label(chain, *handle)
Rusty Russell79dee072000-05-02 16:45:16 +00001664 || strcmp(chain, LABEL_DROP) == 0
1665 || strcmp(chain, LABEL_ACCEPT) == 0
Rusty Russell67088e72000-05-10 01:18:57 +00001666 || strcmp(chain, LABEL_QUEUE) == 0
Rusty Russell79dee072000-05-02 16:45:16 +00001667 || strcmp(chain, LABEL_RETURN) == 0) {
Harald Welteaae69be2004-08-29 23:32:14 +00001668 DEBUGP("Chain `%s' already exists\n", chain);
Marc Bouchere6869a82000-03-20 06:03:29 +00001669 errno = EEXIST;
1670 return 0;
1671 }
1672
Rusty Russell79dee072000-05-02 16:45:16 +00001673 if (strlen(chain)+1 > sizeof(IPT_CHAINLABEL)) {
Harald Welteaae69be2004-08-29 23:32:14 +00001674 DEBUGP("Chain name `%s' too long\n", chain);
Marc Bouchere6869a82000-03-20 06:03:29 +00001675 errno = EINVAL;
1676 return 0;
1677 }
1678
Harald Welteaae69be2004-08-29 23:32:14 +00001679 c = iptcc_alloc_chain_head(chain, 0);
1680 if (!c) {
1681 DEBUGP("Cannot allocate memory for chain `%s'\n", chain);
1682 errno = ENOMEM;
1683 return 0;
Marc Bouchere6869a82000-03-20 06:03:29 +00001684
Harald Welteaae69be2004-08-29 23:32:14 +00001685 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001686
Harald Welteaae69be2004-08-29 23:32:14 +00001687 DEBUGP("Creating chain `%s'\n", chain);
1688 list_add_tail(&c->list, &(*handle)->chains);
Harald Welte0113fe72004-01-06 19:04:02 +00001689
1690 set_changed(*handle);
1691
Harald Welteaae69be2004-08-29 23:32:14 +00001692 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00001693}
1694
1695/* Get the number of references to this chain. */
1696int
Rusty Russell79dee072000-05-02 16:45:16 +00001697TC_GET_REFERENCES(unsigned int *ref, const IPT_CHAINLABEL chain,
1698 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001699{
Harald Welteaae69be2004-08-29 23:32:14 +00001700 struct chain_head *c;
Marc Bouchere6869a82000-03-20 06:03:29 +00001701
Harald Welteaae69be2004-08-29 23:32:14 +00001702 if (!(c = iptcc_find_label(chain, *handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001703 errno = ENOENT;
1704 return 0;
1705 }
1706
Harald Welteaae69be2004-08-29 23:32:14 +00001707 *ref = c->references;
1708
Marc Bouchere6869a82000-03-20 06:03:29 +00001709 return 1;
1710}
1711
1712/* Deletes a chain. */
1713int
Rusty Russell79dee072000-05-02 16:45:16 +00001714TC_DELETE_CHAIN(const IPT_CHAINLABEL chain, TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001715{
Marc Bouchere6869a82000-03-20 06:03:29 +00001716 unsigned int references;
Harald Welteaae69be2004-08-29 23:32:14 +00001717 struct chain_head *c;
Rusty Russell7e53bf92000-03-20 07:03:28 +00001718
Rusty Russell79dee072000-05-02 16:45:16 +00001719 iptc_fn = TC_DELETE_CHAIN;
Marc Bouchere6869a82000-03-20 06:03:29 +00001720
Harald Welteaae69be2004-08-29 23:32:14 +00001721 if (!(c = iptcc_find_label(chain, *handle))) {
1722 DEBUGP("cannot find chain `%s'\n", chain);
Marc Bouchere6869a82000-03-20 06:03:29 +00001723 errno = ENOENT;
1724 return 0;
1725 }
1726
Harald Welteaae69be2004-08-29 23:32:14 +00001727 if (TC_BUILTIN(chain, *handle)) {
1728 DEBUGP("cannot remove builtin chain `%s'\n", chain);
1729 errno = EINVAL;
1730 return 0;
1731 }
1732
1733 if (!TC_GET_REFERENCES(&references, chain, handle)) {
1734 DEBUGP("cannot get references on chain `%s'\n", chain);
1735 return 0;
1736 }
1737
1738 if (references > 0) {
1739 DEBUGP("chain `%s' still has references\n", chain);
1740 errno = EMLINK;
1741 return 0;
1742 }
1743
1744 if (c->num_rules) {
1745 DEBUGP("chain `%s' is not empty\n", chain);
Marc Bouchere6869a82000-03-20 06:03:29 +00001746 errno = ENOTEMPTY;
1747 return 0;
1748 }
1749
Harald Welteaae69be2004-08-29 23:32:14 +00001750 /* If we are about to delete the chain that is the current
1751 * iterator, move chain iterator firward. */
1752 if (c == (*handle)->chain_iterator_cur)
1753 iptcc_chain_iterator_advance(*handle);
Harald Welte0113fe72004-01-06 19:04:02 +00001754
Harald Welteaae69be2004-08-29 23:32:14 +00001755 list_del(&c->list);
1756 free(c);
Harald Welte0113fe72004-01-06 19:04:02 +00001757
Harald Welteaae69be2004-08-29 23:32:14 +00001758 DEBUGP("chain `%s' deleted\n", chain);
1759
1760 set_changed(*handle);
1761
1762 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00001763}
1764
1765/* Renames a chain. */
Rusty Russell79dee072000-05-02 16:45:16 +00001766int TC_RENAME_CHAIN(const IPT_CHAINLABEL oldname,
1767 const IPT_CHAINLABEL newname,
1768 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001769{
Harald Welteaae69be2004-08-29 23:32:14 +00001770 struct chain_head *c;
Rusty Russell79dee072000-05-02 16:45:16 +00001771 iptc_fn = TC_RENAME_CHAIN;
Marc Bouchere6869a82000-03-20 06:03:29 +00001772
Harald Welte1de80462000-10-30 12:00:27 +00001773 /* find_label doesn't cover built-in targets: DROP, ACCEPT,
1774 QUEUE, RETURN. */
Harald Welteaae69be2004-08-29 23:32:14 +00001775 if (iptcc_find_label(newname, *handle)
Rusty Russell79dee072000-05-02 16:45:16 +00001776 || strcmp(newname, LABEL_DROP) == 0
1777 || strcmp(newname, LABEL_ACCEPT) == 0
Harald Welte1de80462000-10-30 12:00:27 +00001778 || strcmp(newname, LABEL_QUEUE) == 0
Rusty Russell79dee072000-05-02 16:45:16 +00001779 || strcmp(newname, LABEL_RETURN) == 0) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001780 errno = EEXIST;
1781 return 0;
1782 }
1783
Harald Welteaae69be2004-08-29 23:32:14 +00001784 if (!(c = iptcc_find_label(oldname, *handle))
Rusty Russell79dee072000-05-02 16:45:16 +00001785 || TC_BUILTIN(oldname, *handle)) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001786 errno = ENOENT;
1787 return 0;
1788 }
1789
Rusty Russell79dee072000-05-02 16:45:16 +00001790 if (strlen(newname)+1 > sizeof(IPT_CHAINLABEL)) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001791 errno = EINVAL;
1792 return 0;
1793 }
1794
Harald Welteaae69be2004-08-29 23:32:14 +00001795 strncpy(c->name, newname, sizeof(IPT_CHAINLABEL));
1796
Harald Welte0113fe72004-01-06 19:04:02 +00001797 set_changed(*handle);
1798
Marc Bouchere6869a82000-03-20 06:03:29 +00001799 return 1;
1800}
1801
1802/* Sets the policy on a built-in chain. */
1803int
Rusty Russell79dee072000-05-02 16:45:16 +00001804TC_SET_POLICY(const IPT_CHAINLABEL chain,
1805 const IPT_CHAINLABEL policy,
Harald Welte1cef74d2001-01-05 15:22:59 +00001806 STRUCT_COUNTERS *counters,
Rusty Russell79dee072000-05-02 16:45:16 +00001807 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001808{
Harald Welteaae69be2004-08-29 23:32:14 +00001809 struct chain_head *c;
Marc Bouchere6869a82000-03-20 06:03:29 +00001810
Rusty Russell79dee072000-05-02 16:45:16 +00001811 iptc_fn = TC_SET_POLICY;
Marc Bouchere6869a82000-03-20 06:03:29 +00001812
Harald Welteaae69be2004-08-29 23:32:14 +00001813 if (!(c = iptcc_find_label(chain, *handle))) {
1814 DEBUGP("cannot find chain `%s'\n", chain);
1815 errno = ENOENT;
Marc Bouchere6869a82000-03-20 06:03:29 +00001816 return 0;
1817 }
1818
Harald Welteaae69be2004-08-29 23:32:14 +00001819 if (!iptcc_is_builtin(c)) {
1820 DEBUGP("cannot set policy of userdefinedchain `%s'\n", chain);
1821 errno = ENOENT;
1822 return 0;
1823 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001824
Rusty Russell79dee072000-05-02 16:45:16 +00001825 if (strcmp(policy, LABEL_ACCEPT) == 0)
Harald Welteaae69be2004-08-29 23:32:14 +00001826 c->verdict = -NF_ACCEPT - 1;
Rusty Russell79dee072000-05-02 16:45:16 +00001827 else if (strcmp(policy, LABEL_DROP) == 0)
Harald Welteaae69be2004-08-29 23:32:14 +00001828 c->verdict = -NF_DROP - 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00001829 else {
1830 errno = EINVAL;
1831 return 0;
1832 }
Harald Welte1cef74d2001-01-05 15:22:59 +00001833
Harald Welte1cef74d2001-01-05 15:22:59 +00001834 if (counters) {
1835 /* set byte and packet counters */
Harald Welteaae69be2004-08-29 23:32:14 +00001836 memcpy(&c->counters, counters, sizeof(STRUCT_COUNTERS));
1837 c->counter_map.maptype = COUNTER_MAP_SET;
Harald Welte1cef74d2001-01-05 15:22:59 +00001838 } else {
Harald Welteaae69be2004-08-29 23:32:14 +00001839 c->counter_map.maptype = COUNTER_MAP_NOMAP;
Harald Welte1cef74d2001-01-05 15:22:59 +00001840 }
1841
Rusty Russell175f6412000-03-24 09:32:20 +00001842 set_changed(*handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001843
Marc Bouchere6869a82000-03-20 06:03:29 +00001844 return 1;
1845}
1846
1847/* Without this, on gcc 2.7.2.3, we get:
Rusty Russell79dee072000-05-02 16:45:16 +00001848 libiptc.c: In function `TC_COMMIT':
Marc Bouchere6869a82000-03-20 06:03:29 +00001849 libiptc.c:833: fixed or forbidden register was spilled.
1850 This may be due to a compiler bug or to impossible asm
1851 statements or clauses.
1852*/
1853static void
Rusty Russell79dee072000-05-02 16:45:16 +00001854subtract_counters(STRUCT_COUNTERS *answer,
1855 const STRUCT_COUNTERS *a,
1856 const STRUCT_COUNTERS *b)
Marc Bouchere6869a82000-03-20 06:03:29 +00001857{
1858 answer->pcnt = a->pcnt - b->pcnt;
1859 answer->bcnt = a->bcnt - b->bcnt;
1860}
1861
Harald Welteaae69be2004-08-29 23:32:14 +00001862
1863static void counters_nomap(STRUCT_COUNTERS_INFO *newcounters,
1864 unsigned int index)
1865{
1866 newcounters->counters[index] = ((STRUCT_COUNTERS) { 0, 0});
1867 DEBUGP_C("NOMAP => zero\n");
1868}
1869
1870static void counters_normal_map(STRUCT_COUNTERS_INFO *newcounters,
1871 STRUCT_REPLACE *repl,
1872 unsigned int index,
1873 unsigned int mappos)
1874{
1875 /* Original read: X.
1876 * Atomic read on replacement: X + Y.
1877 * Currently in kernel: Z.
1878 * Want in kernel: X + Y + Z.
1879 * => Add in X + Y
1880 * => Add in replacement read.
1881 */
1882 newcounters->counters[index] = repl->counters[mappos];
1883 DEBUGP_C("NORMAL_MAP => mappos %u \n", mappos);
1884}
1885
1886static void counters_map_zeroed(STRUCT_COUNTERS_INFO *newcounters,
1887 STRUCT_REPLACE *repl,
1888 unsigned int index,
1889 unsigned int mappos,
1890 STRUCT_COUNTERS *counters)
1891{
1892 /* Original read: X.
1893 * Atomic read on replacement: X + Y.
1894 * Currently in kernel: Z.
1895 * Want in kernel: Y + Z.
1896 * => Add in Y.
1897 * => Add in (replacement read - original read).
1898 */
1899 subtract_counters(&newcounters->counters[index],
1900 &repl->counters[mappos],
1901 counters);
1902 DEBUGP_C("ZEROED => mappos %u\n", mappos);
1903}
1904
1905static void counters_map_set(STRUCT_COUNTERS_INFO *newcounters,
1906 unsigned int index,
1907 STRUCT_COUNTERS *counters)
1908{
1909 /* Want to set counter (iptables-restore) */
1910
1911 memcpy(&newcounters->counters[index], counters,
1912 sizeof(STRUCT_COUNTERS));
1913
1914 DEBUGP_C("SET\n");
1915}
1916
1917
Marc Bouchere6869a82000-03-20 06:03:29 +00001918int
Rusty Russell79dee072000-05-02 16:45:16 +00001919TC_COMMIT(TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001920{
1921 /* Replace, then map back the counters. */
Rusty Russell79dee072000-05-02 16:45:16 +00001922 STRUCT_REPLACE *repl;
1923 STRUCT_COUNTERS_INFO *newcounters;
Harald Welteaae69be2004-08-29 23:32:14 +00001924 struct chain_head *c;
1925 int ret;
Martin Josefsson841e4ae2003-05-02 15:30:11 +00001926 size_t counterlen;
Harald Welteaae69be2004-08-29 23:32:14 +00001927 int new_number;
1928 unsigned int new_size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001929
1930 CHECK(*handle);
Martin Josefsson841e4ae2003-05-02 15:30:11 +00001931
Marc Bouchere6869a82000-03-20 06:03:29 +00001932#if 0
Rusty Russell54c307e2000-09-04 06:47:28 +00001933 TC_DUMP_ENTRIES(*handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001934#endif
1935
1936 /* Don't commit if nothing changed. */
1937 if (!(*handle)->changed)
1938 goto finished;
1939
Harald Welteaae69be2004-08-29 23:32:14 +00001940 new_number = iptcc_compile_table_prep(*handle, &new_size);
1941 if (new_number < 0) {
1942 errno = ENOMEM;
1943 return 0;
1944 }
1945
1946 repl = malloc(sizeof(*repl) + new_size);
Marc Bouchere6869a82000-03-20 06:03:29 +00001947 if (!repl) {
1948 errno = ENOMEM;
1949 return 0;
1950 }
Harald Welteaae69be2004-08-29 23:32:14 +00001951 memset(repl, 0, sizeof(*repl));
1952
1953 counterlen = sizeof(STRUCT_COUNTERS_INFO)
1954 + sizeof(STRUCT_COUNTERS) * new_number;
Marc Bouchere6869a82000-03-20 06:03:29 +00001955
1956 /* These are the old counters we will get from kernel */
Rusty Russell79dee072000-05-02 16:45:16 +00001957 repl->counters = malloc(sizeof(STRUCT_COUNTERS)
Marc Bouchere6869a82000-03-20 06:03:29 +00001958 * (*handle)->info.num_entries);
1959 if (!repl->counters) {
1960 free(repl);
1961 errno = ENOMEM;
1962 return 0;
1963 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001964 /* These are the counters we're going to put back, later. */
1965 newcounters = malloc(counterlen);
1966 if (!newcounters) {
1967 free(repl->counters);
1968 free(repl);
1969 errno = ENOMEM;
1970 return 0;
1971 }
Harald Welteaae69be2004-08-29 23:32:14 +00001972 memset(newcounters, 0, counterlen);
Marc Bouchere6869a82000-03-20 06:03:29 +00001973
1974 strcpy(repl->name, (*handle)->info.name);
Harald Welteaae69be2004-08-29 23:32:14 +00001975 repl->num_entries = new_number;
1976 repl->size = new_size;
1977
Marc Bouchere6869a82000-03-20 06:03:29 +00001978 repl->num_counters = (*handle)->info.num_entries;
1979 repl->valid_hooks = (*handle)->info.valid_hooks;
Harald Welteaae69be2004-08-29 23:32:14 +00001980
1981 DEBUGP("num_entries=%u, size=%u, num_counters=%u\n",
1982 repl->num_entries, repl->size, repl->num_counters);
1983
1984 ret = iptcc_compile_table(*handle, repl);
1985 if (ret < 0) {
1986 errno = ret;
1987 free(repl->counters);
1988 free(repl);
1989 return 0;
1990 }
1991
1992
1993#ifdef IPTC_DEBUG2
1994 {
1995 int fd = open("/tmp/libiptc-so_set_replace.blob",
1996 O_CREAT|O_WRONLY);
1997 if (fd >= 0) {
1998 write(fd, repl, sizeof(*repl) + repl->size);
1999 close(fd);
2000 }
2001 }
2002#endif
Marc Bouchere6869a82000-03-20 06:03:29 +00002003
Rusty Russell79dee072000-05-02 16:45:16 +00002004 if (setsockopt(sockfd, TC_IPPROTO, SO_SET_REPLACE, repl,
Harald Welteaae69be2004-08-29 23:32:14 +00002005 sizeof(*repl) + repl->size) < 0) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002006 free(repl->counters);
2007 free(repl);
2008 free(newcounters);
2009 return 0;
2010 }
2011
2012 /* Put counters back. */
2013 strcpy(newcounters->name, (*handle)->info.name);
Harald Welteaae69be2004-08-29 23:32:14 +00002014 newcounters->num_counters = new_number;
Marc Bouchere6869a82000-03-20 06:03:29 +00002015
Harald Welteaae69be2004-08-29 23:32:14 +00002016 list_for_each_entry(c, &(*handle)->chains, list) {
2017 struct rule_head *r;
Marc Bouchere6869a82000-03-20 06:03:29 +00002018
Harald Welteaae69be2004-08-29 23:32:14 +00002019 /* Builtin chains have their own counters */
2020 if (iptcc_is_builtin(c)) {
2021 DEBUGP("counter for chain-index %u: ", c->foot_index);
2022 switch(c->counter_map.maptype) {
2023 case COUNTER_MAP_NOMAP:
2024 counters_nomap(newcounters, c->foot_index);
2025 break;
2026 case COUNTER_MAP_NORMAL_MAP:
2027 counters_normal_map(newcounters, repl,
2028 c->foot_index,
2029 c->counter_map.mappos);
2030 break;
2031 case COUNTER_MAP_ZEROED:
2032 counters_map_zeroed(newcounters, repl,
2033 c->foot_index,
2034 c->counter_map.mappos,
2035 &c->counters);
2036 break;
2037 case COUNTER_MAP_SET:
2038 counters_map_set(newcounters, c->foot_index,
2039 &c->counters);
2040 break;
2041 }
2042 }
Harald Welte1cef74d2001-01-05 15:22:59 +00002043
Harald Welteaae69be2004-08-29 23:32:14 +00002044 list_for_each_entry(r, &c->rules, list) {
2045 DEBUGP("counter for index %u: ", r->index);
2046 switch (r->counter_map.maptype) {
2047 case COUNTER_MAP_NOMAP:
2048 counters_nomap(newcounters, r->index);
2049 break;
Harald Welte1cef74d2001-01-05 15:22:59 +00002050
Harald Welteaae69be2004-08-29 23:32:14 +00002051 case COUNTER_MAP_NORMAL_MAP:
2052 counters_normal_map(newcounters, repl,
2053 r->index,
2054 r->counter_map.mappos);
2055 break;
Harald Welte1cef74d2001-01-05 15:22:59 +00002056
Harald Welteaae69be2004-08-29 23:32:14 +00002057 case COUNTER_MAP_ZEROED:
2058 counters_map_zeroed(newcounters, repl,
2059 r->index,
2060 r->counter_map.mappos,
2061 &r->entry->counters);
2062 break;
2063
2064 case COUNTER_MAP_SET:
2065 counters_map_set(newcounters, r->index,
2066 &r->entry->counters);
2067 break;
2068 }
Marc Bouchere6869a82000-03-20 06:03:29 +00002069 }
2070 }
Rusty Russell62527ce2000-09-04 09:45:54 +00002071
Harald Welteaae69be2004-08-29 23:32:14 +00002072
Rusty Russell62527ce2000-09-04 09:45:54 +00002073#ifdef KERNEL_64_USERSPACE_32
2074 {
2075 /* Kernel will think that pointer should be 64-bits, and get
2076 padding. So we accomodate here (assumption: alignment of
2077 `counters' is on 64-bit boundary). */
2078 u_int64_t *kernptr = (u_int64_t *)&newcounters->counters;
2079 if ((unsigned long)&newcounters->counters % 8 != 0) {
2080 fprintf(stderr,
2081 "counters alignment incorrect! Mail rusty!\n");
2082 abort();
2083 }
2084 *kernptr = newcounters->counters;
Rusty Russell54c307e2000-09-04 06:47:28 +00002085 }
Rusty Russell62527ce2000-09-04 09:45:54 +00002086#endif /* KERNEL_64_USERSPACE_32 */
Marc Bouchere6869a82000-03-20 06:03:29 +00002087
Harald Welteaae69be2004-08-29 23:32:14 +00002088#ifdef IPTC_DEBUG2
2089 {
2090 int fd = open("/tmp/libiptc-so_set_add_counters.blob",
2091 O_CREAT|O_WRONLY);
2092 if (fd >= 0) {
2093 write(fd, newcounters, counterlen);
2094 close(fd);
2095 }
2096 }
2097#endif
2098
Rusty Russell79dee072000-05-02 16:45:16 +00002099 if (setsockopt(sockfd, TC_IPPROTO, SO_SET_ADD_COUNTERS,
2100 newcounters, counterlen) < 0) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002101 free(repl->counters);
2102 free(repl);
2103 free(newcounters);
2104 return 0;
2105 }
2106
2107 free(repl->counters);
2108 free(repl);
2109 free(newcounters);
2110
2111 finished:
Martin Josefsson841e4ae2003-05-02 15:30:11 +00002112 TC_FREE(handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00002113 return 1;
2114}
2115
2116/* Get raw socket. */
2117int
Rusty Russell79dee072000-05-02 16:45:16 +00002118TC_GET_RAW_SOCKET()
Marc Bouchere6869a82000-03-20 06:03:29 +00002119{
2120 return sockfd;
2121}
2122
2123/* Translates errno numbers into more human-readable form than strerror. */
2124const char *
Rusty Russell79dee072000-05-02 16:45:16 +00002125TC_STRERROR(int err)
Marc Bouchere6869a82000-03-20 06:03:29 +00002126{
2127 unsigned int i;
2128 struct table_struct {
2129 void *fn;
2130 int err;
2131 const char *message;
2132 } table [] =
Harald Welte4ccfa632001-07-30 15:12:43 +00002133 { { TC_INIT, EPERM, "Permission denied (you must be root)" },
Rusty Russell79dee072000-05-02 16:45:16 +00002134 { TC_INIT, EINVAL, "Module is wrong version" },
Harald Welte4ccfa632001-07-30 15:12:43 +00002135 { TC_INIT, ENOENT,
2136 "Table does not exist (do you need to insmod?)" },
Rusty Russell79dee072000-05-02 16:45:16 +00002137 { TC_DELETE_CHAIN, ENOTEMPTY, "Chain is not empty" },
2138 { TC_DELETE_CHAIN, EINVAL, "Can't delete built-in chain" },
2139 { TC_DELETE_CHAIN, EMLINK,
Marc Bouchere6869a82000-03-20 06:03:29 +00002140 "Can't delete chain with references left" },
Rusty Russell79dee072000-05-02 16:45:16 +00002141 { TC_CREATE_CHAIN, EEXIST, "Chain already exists" },
2142 { TC_INSERT_ENTRY, E2BIG, "Index of insertion too big" },
2143 { TC_REPLACE_ENTRY, E2BIG, "Index of replacement too big" },
2144 { TC_DELETE_NUM_ENTRY, E2BIG, "Index of deletion too big" },
Harald Welte1cef74d2001-01-05 15:22:59 +00002145 { TC_READ_COUNTER, E2BIG, "Index of counter too big" },
2146 { TC_ZERO_COUNTER, E2BIG, "Index of counter too big" },
Rusty Russell79dee072000-05-02 16:45:16 +00002147 { TC_INSERT_ENTRY, ELOOP, "Loop found in table" },
2148 { TC_INSERT_ENTRY, EINVAL, "Target problem" },
Marc Bouchere6869a82000-03-20 06:03:29 +00002149 /* EINVAL for CHECK probably means bad interface. */
Rusty Russell79dee072000-05-02 16:45:16 +00002150 { TC_CHECK_PACKET, EINVAL,
Marc Boucherc8264992000-04-22 22:34:44 +00002151 "Bad arguments (does that interface exist?)" },
Harald Welte4ccfa632001-07-30 15:12:43 +00002152 { TC_CHECK_PACKET, ENOSYS,
2153 "Checking will most likely never get implemented" },
Marc Bouchere6869a82000-03-20 06:03:29 +00002154 /* ENOENT for DELETE probably means no matching rule */
Rusty Russell79dee072000-05-02 16:45:16 +00002155 { TC_DELETE_ENTRY, ENOENT,
Marc Boucherc8264992000-04-22 22:34:44 +00002156 "Bad rule (does a matching rule exist in that chain?)" },
Rusty Russell79dee072000-05-02 16:45:16 +00002157 { TC_SET_POLICY, ENOENT,
Marc Boucherc8264992000-04-22 22:34:44 +00002158 "Bad built-in chain name" },
Rusty Russell79dee072000-05-02 16:45:16 +00002159 { TC_SET_POLICY, EINVAL,
Marc Boucherc8264992000-04-22 22:34:44 +00002160 "Bad policy name" },
Harald Welte4ccfa632001-07-30 15:12:43 +00002161
2162 { NULL, 0, "Incompatible with this kernel" },
2163 { NULL, ENOPROTOOPT, "iptables who? (do you need to insmod?)" },
2164 { NULL, ENOSYS, "Will be implemented real soon. I promise ;)" },
2165 { NULL, ENOMEM, "Memory allocation problem" },
2166 { NULL, ENOENT, "No chain/target/match by that name" },
Marc Bouchere6869a82000-03-20 06:03:29 +00002167 };
2168
2169 for (i = 0; i < sizeof(table)/sizeof(struct table_struct); i++) {
2170 if ((!table[i].fn || table[i].fn == iptc_fn)
2171 && table[i].err == err)
2172 return table[i].message;
2173 }
2174
2175 return strerror(err);
2176}