blob: a71ecad1027d31bc0abaf5703d87688613a44692 [file] [log] [blame]
Martin Josefssoneb066cc2004-09-22 21:04:07 +00001/* Library which manipulates firewall rules. Version $Revision: 1.52 $ */
Marc Bouchere6869a82000-03-20 06:03:29 +00002
3/* Architecture of firewall rules is as follows:
4 *
5 * Chains go INPUT, FORWARD, OUTPUT then user chains.
6 * Each user chain starts with an ERROR node.
7 * Every chain ends with an unconditional jump: a RETURN for user chains,
8 * and a POLICY for built-ins.
9 */
10
Harald Welte3ea8f402003-06-23 18:25:59 +000011/* (C) 1999 Paul ``Rusty'' Russell - Placed under the GNU GPL (See
12 * COPYING for details).
Harald Welteaae69be2004-08-29 23:32:14 +000013 * (C) 2000-2004 by the Netfilter Core Team <coreteam@netfilter.org>
Harald Welte3ea8f402003-06-23 18:25:59 +000014 *
Harald Weltefbc85232003-06-24 17:37:21 +000015 * 2003-Jun-20: Harald Welte <laforge@netfilter.org>:
Harald Welte3ea8f402003-06-23 18:25:59 +000016 * - Reimplementation of chain cache to use offsets instead of entries
Harald Weltefbc85232003-06-24 17:37:21 +000017 * 2003-Jun-23: Harald Welte <laforge@netfilter.org>:
Harald Welte0113fe72004-01-06 19:04:02 +000018 * - performance optimization, sponsored by Astaro AG (http://www.astaro.com/)
Harald Weltefbc85232003-06-24 17:37:21 +000019 * don't rebuild the chain cache after every operation, instead fix it
20 * up after a ruleset change.
Harald Welteaae69be2004-08-29 23:32:14 +000021 * 2004-Aug-18: Harald Welte <laforge@netfilter.org>:
22 * - futher performance work: total reimplementation of libiptc.
23 * - libiptc now has a real internal (linked-list) represntation of the
24 * ruleset and a parser/compiler from/to this internal representation
25 * - again sponsored by Astaro AG (http://www.astaro.com/)
Harald Welte3ea8f402003-06-23 18:25:59 +000026 */
Harald Welte15920d12004-05-16 09:05:07 +000027#include <sys/types.h>
28#include <sys/socket.h>
Stephane Ouellette7cd00282004-05-14 08:21:06 +000029
Harald Welteaae69be2004-08-29 23:32:14 +000030#include "linux_list.h"
31
32//#define IPTC_DEBUG2 1
33
34#ifdef IPTC_DEBUG2
35#include <fcntl.h>
36#define DEBUGP(x, args...) fprintf(stderr, "%s: " x, __FUNCTION__, ## args)
37#define DEBUGP_C(x, args...) fprintf(stderr, x, ## args)
38#else
39#define DEBUGP(x, args...)
40#define DEBUGP_C(x, args...)
41#endif
42
Marc Bouchere6869a82000-03-20 06:03:29 +000043#ifndef IPT_LIB_DIR
44#define IPT_LIB_DIR "/usr/local/lib/iptables"
45#endif
46
Harald Welte0113fe72004-01-06 19:04:02 +000047#ifndef __OPTIMIZE__
48STRUCT_ENTRY_TARGET *
49GET_TARGET(STRUCT_ENTRY *e)
50{
51 return (void *)e + e->target_offset;
52}
53#endif
54
Marc Bouchere6869a82000-03-20 06:03:29 +000055static int sockfd = -1;
56static void *iptc_fn = NULL;
57
58static const char *hooknames[]
Rusty Russell79dee072000-05-02 16:45:16 +000059= { [HOOK_PRE_ROUTING] "PREROUTING",
60 [HOOK_LOCAL_IN] "INPUT",
61 [HOOK_FORWARD] "FORWARD",
62 [HOOK_LOCAL_OUT] "OUTPUT",
Rusty Russell10758b72000-09-14 07:37:33 +000063 [HOOK_POST_ROUTING] "POSTROUTING",
64#ifdef HOOK_DROPPING
65 [HOOK_DROPPING] "DROPPING"
66#endif
Marc Bouchere6869a82000-03-20 06:03:29 +000067};
68
Harald Welteaae69be2004-08-29 23:32:14 +000069/* Convenience structures */
70struct ipt_error_target
71{
72 STRUCT_ENTRY_TARGET t;
73 char error[TABLE_MAXNAMELEN];
74};
75
76struct chain_head;
77struct rule_head;
78
Marc Bouchere6869a82000-03-20 06:03:29 +000079struct counter_map
80{
81 enum {
82 COUNTER_MAP_NOMAP,
83 COUNTER_MAP_NORMAL_MAP,
Harald Welte1cef74d2001-01-05 15:22:59 +000084 COUNTER_MAP_ZEROED,
85 COUNTER_MAP_SET
Marc Bouchere6869a82000-03-20 06:03:29 +000086 } maptype;
87 unsigned int mappos;
88};
89
Harald Welteaae69be2004-08-29 23:32:14 +000090enum iptcc_rule_type {
91 IPTCC_R_STANDARD, /* standard target (ACCEPT, ...) */
92 IPTCC_R_MODULE, /* extension module (SNAT, ...) */
93 IPTCC_R_FALLTHROUGH, /* fallthrough rule */
94 IPTCC_R_JUMP, /* jump to other chain */
Marc Bouchere6869a82000-03-20 06:03:29 +000095};
96
Harald Welteaae69be2004-08-29 23:32:14 +000097struct rule_head
Rusty Russell30fd6e52000-04-23 09:16:06 +000098{
Harald Welteaae69be2004-08-29 23:32:14 +000099 struct list_head list;
100 struct chain_head *chain;
101 struct counter_map counter_map;
102
103 unsigned int index; /* index (needed for counter_map) */
104 unsigned int offset; /* offset in rule blob */
105
106 enum iptcc_rule_type type;
107 struct chain_head *jump; /* jump target, if IPTCC_R_JUMP */
108
109 unsigned int size; /* size of entry data */
110 STRUCT_ENTRY entry[0];
111};
112
113struct chain_head
114{
115 struct list_head list;
Rusty Russell79dee072000-05-02 16:45:16 +0000116 char name[TABLE_MAXNAMELEN];
Harald Welteaae69be2004-08-29 23:32:14 +0000117 unsigned int hooknum; /* hook number+1 if builtin */
118 unsigned int references; /* how many jumps reference us */
119 int verdict; /* verdict if builtin */
120
121 STRUCT_COUNTERS counters; /* per-chain counters */
122 struct counter_map counter_map;
123
124 unsigned int num_rules; /* number of rules in list */
125 struct list_head rules; /* list of rules */
126
127 unsigned int index; /* index (needed for jump resolval) */
128 unsigned int head_offset; /* offset in rule blob */
129 unsigned int foot_index; /* index (needed for counter_map) */
130 unsigned int foot_offset; /* offset in rule blob */
Rusty Russell30fd6e52000-04-23 09:16:06 +0000131};
132
Rusty Russell79dee072000-05-02 16:45:16 +0000133STRUCT_TC_HANDLE
Marc Bouchere6869a82000-03-20 06:03:29 +0000134{
Harald Welteaae69be2004-08-29 23:32:14 +0000135 int changed; /* Have changes been made? */
136
137 struct list_head chains;
138
139 struct chain_head *chain_iterator_cur;
140 struct rule_head *rule_iterator_cur;
141
Rusty Russell79dee072000-05-02 16:45:16 +0000142 STRUCT_GETINFO info;
Harald Welteaae69be2004-08-29 23:32:14 +0000143 STRUCT_GET_ENTRIES *entries;
Marc Bouchere6869a82000-03-20 06:03:29 +0000144};
145
Harald Welteaae69be2004-08-29 23:32:14 +0000146/* allocate a new chain head for the cache */
147static struct chain_head *iptcc_alloc_chain_head(const char *name, int hooknum)
148{
149 struct chain_head *c = malloc(sizeof(*c));
150 if (!c)
151 return NULL;
152 memset(c, 0, sizeof(*c));
153
154 strncpy(c->name, name, TABLE_MAXNAMELEN);
155 c->hooknum = hooknum;
156 INIT_LIST_HEAD(&c->rules);
157
158 return c;
159}
160
161/* allocate and initialize a new rule for the cache */
162static struct rule_head *iptcc_alloc_rule(struct chain_head *c, unsigned int size)
163{
164 struct rule_head *r = malloc(sizeof(*r)+size);
165 if (!r)
166 return NULL;
167 memset(r, 0, sizeof(*r));
168
169 r->chain = c;
170 r->size = size;
171
172 return r;
173}
174
175/* notify us that the ruleset has been modified by the user */
Rusty Russell175f6412000-03-24 09:32:20 +0000176static void
Rusty Russell79dee072000-05-02 16:45:16 +0000177set_changed(TC_HANDLE_T h)
Rusty Russell175f6412000-03-24 09:32:20 +0000178{
Rusty Russell175f6412000-03-24 09:32:20 +0000179 h->changed = 1;
180}
181
Harald Welte380ba5f2002-02-13 16:19:55 +0000182#ifdef IPTC_DEBUG
Rusty Russell79dee072000-05-02 16:45:16 +0000183static void do_check(TC_HANDLE_T h, unsigned int line);
Rusty Russell849779c2000-04-23 15:51:51 +0000184#define CHECK(h) do { if (!getenv("IPTC_NO_CHECK")) do_check((h), __LINE__); } while(0)
Rusty Russell30fd6e52000-04-23 09:16:06 +0000185#else
186#define CHECK(h)
187#endif
Marc Bouchere6869a82000-03-20 06:03:29 +0000188
Harald Welteaae69be2004-08-29 23:32:14 +0000189
190/**********************************************************************
191 * iptc blob utility functions (iptcb_*)
192 **********************************************************************/
193
Marc Bouchere6869a82000-03-20 06:03:29 +0000194static inline int
Harald Welteaae69be2004-08-29 23:32:14 +0000195iptcb_get_number(const STRUCT_ENTRY *i,
Rusty Russell79dee072000-05-02 16:45:16 +0000196 const STRUCT_ENTRY *seek,
Marc Bouchere6869a82000-03-20 06:03:29 +0000197 unsigned int *pos)
198{
199 if (i == seek)
200 return 1;
201 (*pos)++;
202 return 0;
203}
204
Marc Bouchere6869a82000-03-20 06:03:29 +0000205static inline int
Harald Welteaae69be2004-08-29 23:32:14 +0000206iptcb_get_entry_n(STRUCT_ENTRY *i,
Marc Bouchere6869a82000-03-20 06:03:29 +0000207 unsigned int number,
208 unsigned int *pos,
Rusty Russell79dee072000-05-02 16:45:16 +0000209 STRUCT_ENTRY **pe)
Marc Bouchere6869a82000-03-20 06:03:29 +0000210{
211 if (*pos == number) {
212 *pe = i;
213 return 1;
214 }
215 (*pos)++;
216 return 0;
217}
218
Harald Welteaae69be2004-08-29 23:32:14 +0000219static inline STRUCT_ENTRY *
220iptcb_get_entry(TC_HANDLE_T h, unsigned int offset)
221{
222 return (STRUCT_ENTRY *)((char *)h->entries->entrytable + offset);
223}
224
225static unsigned int
226iptcb_entry2index(const TC_HANDLE_T h, const STRUCT_ENTRY *seek)
Marc Bouchere6869a82000-03-20 06:03:29 +0000227{
228 unsigned int pos = 0;
Marc Bouchere6869a82000-03-20 06:03:29 +0000229
Harald Welteaae69be2004-08-29 23:32:14 +0000230 if (ENTRY_ITERATE(h->entries->entrytable, h->entries->size,
231 iptcb_get_number, seek, &pos) == 0) {
232 fprintf(stderr, "ERROR: offset %u not an entry!\n",
233 (unsigned int)((char *)seek - (char *)h->entries->entrytable));
234 abort();
235 }
236 return pos;
Marc Bouchere6869a82000-03-20 06:03:29 +0000237}
238
Harald Welte0113fe72004-01-06 19:04:02 +0000239static inline STRUCT_ENTRY *
Harald Welteaae69be2004-08-29 23:32:14 +0000240iptcb_offset2entry(TC_HANDLE_T h, unsigned int offset)
Harald Welte0113fe72004-01-06 19:04:02 +0000241{
Harald Welteaae69be2004-08-29 23:32:14 +0000242 return (STRUCT_ENTRY *) ((void *)h->entries->entrytable+offset);
Harald Welte0113fe72004-01-06 19:04:02 +0000243}
244
Harald Welteaae69be2004-08-29 23:32:14 +0000245
Harald Welte0113fe72004-01-06 19:04:02 +0000246static inline unsigned long
Harald Welteaae69be2004-08-29 23:32:14 +0000247iptcb_entry2offset(const TC_HANDLE_T h, const STRUCT_ENTRY *e)
Harald Welte0113fe72004-01-06 19:04:02 +0000248{
Harald Welteaae69be2004-08-29 23:32:14 +0000249 return (void *)e - (void *)h->entries->entrytable;
Harald Welte3ea8f402003-06-23 18:25:59 +0000250}
251
252static inline unsigned int
Harald Welteaae69be2004-08-29 23:32:14 +0000253iptcb_offset2index(const TC_HANDLE_T h, unsigned int offset)
Harald Welte3ea8f402003-06-23 18:25:59 +0000254{
Harald Welteaae69be2004-08-29 23:32:14 +0000255 return iptcb_entry2index(h, iptcb_offset2entry(h, offset));
256}
257
258/* Returns 0 if not hook entry, else hooknumber + 1 */
259static inline unsigned int
260iptcb_ent_is_hook_entry(STRUCT_ENTRY *e, TC_HANDLE_T h)
261{
262 unsigned int i;
263
264 for (i = 0; i < NUMHOOKS; i++) {
265 if ((h->info.valid_hooks & (1 << i))
266 && iptcb_get_entry(h, h->info.hook_entry[i]) == e)
267 return i+1;
268 }
269 return 0;
Harald Welte3ea8f402003-06-23 18:25:59 +0000270}
271
272
Harald Welteaae69be2004-08-29 23:32:14 +0000273/**********************************************************************
274 * iptc cache utility functions (iptcc_*)
275 **********************************************************************/
Harald Welte0113fe72004-01-06 19:04:02 +0000276
Harald Welteaae69be2004-08-29 23:32:14 +0000277/* Is the given chain builtin (1) or user-defined (0) */
278static unsigned int iptcc_is_builtin(struct chain_head *c)
279{
280 return (c->hooknum ? 1 : 0);
281}
282
283/* Get a specific rule within a chain */
284static struct rule_head *iptcc_get_rule_num(struct chain_head *c,
285 unsigned int rulenum)
286{
287 struct rule_head *r;
288 unsigned int num = 0;
289
290 list_for_each_entry(r, &c->rules, list) {
291 num++;
292 if (num == rulenum)
293 return r;
294 }
295 return NULL;
296}
297
298/* Returns chain head if found, otherwise NULL. */
299static struct chain_head *
300iptcc_find_chain_by_offset(TC_HANDLE_T handle, unsigned int offset)
301{
302 struct list_head *pos;
303
304 if (list_empty(&handle->chains))
305 return NULL;
306
307 list_for_each(pos, &handle->chains) {
308 struct chain_head *c = list_entry(pos, struct chain_head, list);
309 if (offset >= c->head_offset && offset <= c->foot_offset)
310 return c;
Harald Welte0113fe72004-01-06 19:04:02 +0000311 }
312
Harald Welteaae69be2004-08-29 23:32:14 +0000313 return NULL;
Harald Welte0113fe72004-01-06 19:04:02 +0000314}
Harald Welteaae69be2004-08-29 23:32:14 +0000315/* Returns chain head if found, otherwise NULL. */
316static struct chain_head *
317iptcc_find_label(const char *name, TC_HANDLE_T handle)
318{
319 struct list_head *pos;
320
321 if (list_empty(&handle->chains))
322 return NULL;
323
324 list_for_each(pos, &handle->chains) {
325 struct chain_head *c = list_entry(pos, struct chain_head, list);
326 if (!strcmp(c->name, name))
327 return c;
328 }
329
330 return NULL;
331}
332
333/* called when rule is to be removed from cache */
334static void iptcc_delete_rule(struct rule_head *r)
335{
336 DEBUGP("deleting rule %p (offset %u)\n", r, r->offset);
337 /* clean up reference count of called chain */
338 if (r->type == IPTCC_R_JUMP
339 && r->jump)
340 r->jump->references--;
341
342 list_del(&r->list);
343 free(r);
344}
345
346
347/**********************************************************************
348 * RULESET PARSER (blob -> cache)
349 **********************************************************************/
350
351static int alphasort(const void *a, const void *b)
352{
353 return strcmp(((struct chain_head *)a)->name,
354 ((struct chain_head *)b)->name);
355}
356
357/* Delete policy rule of previous chain, since cache doesn't contain
358 * chain policy rules.
359 * WARNING: This function has ugly design and relies on a lot of context, only
360 * to be called from specific places within the parser */
361static int __iptcc_p_del_policy(TC_HANDLE_T h, unsigned int num)
362{
363 if (h->chain_iterator_cur) {
364 /* policy rule is last rule */
365 struct rule_head *pr = (struct rule_head *)
366 h->chain_iterator_cur->rules.prev;
367
368 /* save verdict */
369 h->chain_iterator_cur->verdict =
370 *(int *)GET_TARGET(pr->entry)->data;
371
372 /* save counter and counter_map information */
373 h->chain_iterator_cur->counter_map.maptype =
374 COUNTER_MAP_NORMAL_MAP;
375 h->chain_iterator_cur->counter_map.mappos = num-1;
376 memcpy(&h->chain_iterator_cur->counters, &pr->entry->counters,
377 sizeof(h->chain_iterator_cur->counters));
378
379 /* foot_offset points to verdict rule */
380 h->chain_iterator_cur->foot_index = num;
381 h->chain_iterator_cur->foot_offset = pr->offset;
382
383 /* delete rule from cache */
384 iptcc_delete_rule(pr);
Martin Josefsson8d1b38a2004-09-22 21:00:19 +0000385 h->chain_iterator_cur->num_rules--;
Harald Welteaae69be2004-08-29 23:32:14 +0000386
387 return 1;
388 }
389 return 0;
390}
391
392/* Another ugly helper function split out of cache_add_entry to make it less
393 * spaghetti code */
394static void __iptcc_p_add_chain(TC_HANDLE_T h, struct chain_head *c,
395 unsigned int offset, unsigned int *num)
396{
397 __iptcc_p_del_policy(h, *num);
398
399 c->head_offset = offset;
400 c->index = *num;
401
402 list_add_tail(&c->list, &h->chains);
403 h->chain_iterator_cur = c;
404}
405
406/* main parser function: add an entry from the blob to the cache */
407static int cache_add_entry(STRUCT_ENTRY *e,
408 TC_HANDLE_T h,
409 STRUCT_ENTRY **prev,
410 unsigned int *num)
411{
412 unsigned int builtin;
413 unsigned int offset = (char *)e - (char *)h->entries->entrytable;
414
415 DEBUGP("entering...");
416
417 /* Last entry ("policy rule"). End it.*/
418 if (iptcb_entry2offset(h,e) + e->next_offset == h->entries->size) {
419 /* This is the ERROR node at the end of the chain */
420 DEBUGP_C("%u:%u: end of table:\n", *num, offset);
421
422 __iptcc_p_del_policy(h, *num);
423
424 h->chain_iterator_cur = NULL;
425 goto out_inc;
426 }
427
428 /* We know this is the start of a new chain if it's an ERROR
429 * target, or a hook entry point */
430
431 if (strcmp(GET_TARGET(e)->u.user.name, ERROR_TARGET) == 0) {
432 struct chain_head *c =
433 iptcc_alloc_chain_head((const char *)GET_TARGET(e)->data, 0);
434 DEBUGP_C("%u:%u:new userdefined chain %s: %p\n", *num, offset,
435 (char *)c->name, c);
436 if (!c) {
437 errno = -ENOMEM;
438 return -1;
439 }
440
441 __iptcc_p_add_chain(h, c, offset, num);
442
443 } else if ((builtin = iptcb_ent_is_hook_entry(e, h)) != 0) {
444 struct chain_head *c =
445 iptcc_alloc_chain_head((char *)hooknames[builtin-1],
446 builtin);
447 DEBUGP_C("%u:%u new builtin chain: %p (rules=%p)\n",
448 *num, offset, c, &c->rules);
449 if (!c) {
450 errno = -ENOMEM;
451 return -1;
452 }
453
454 c->hooknum = builtin;
455
456 __iptcc_p_add_chain(h, c, offset, num);
457
458 /* FIXME: this is ugly. */
459 goto new_rule;
460 } else {
461 /* has to be normal rule */
462 struct rule_head *r;
463new_rule:
464
465 if (!(r = iptcc_alloc_rule(h->chain_iterator_cur,
466 e->next_offset))) {
467 errno = ENOMEM;
468 return -1;
469 }
470 DEBUGP_C("%u:%u normal rule: %p: ", *num, offset, r);
471
472 r->index = *num;
473 r->offset = offset;
474 memcpy(r->entry, e, e->next_offset);
475 r->counter_map.maptype = COUNTER_MAP_NORMAL_MAP;
476 r->counter_map.mappos = r->index;
477
478 /* handling of jumps, etc. */
479 if (!strcmp(GET_TARGET(e)->u.user.name, STANDARD_TARGET)) {
480 STRUCT_STANDARD_TARGET *t;
481
482 t = (STRUCT_STANDARD_TARGET *)GET_TARGET(e);
483 if (t->target.u.target_size
484 != ALIGN(sizeof(STRUCT_STANDARD_TARGET))) {
485 errno = EINVAL;
486 return -1;
487 }
488
489 if (t->verdict < 0) {
490 DEBUGP_C("standard, verdict=%d\n", t->verdict);
491 r->type = IPTCC_R_STANDARD;
492 } else if (t->verdict == r->offset+e->next_offset) {
493 DEBUGP_C("fallthrough\n");
494 r->type = IPTCC_R_FALLTHROUGH;
495 } else {
496 DEBUGP_C("jump, target=%u\n", t->verdict);
497 r->type = IPTCC_R_JUMP;
498 /* Jump target fixup has to be deferred
499 * until second pass, since we migh not
500 * yet have parsed the target */
501 }
Martin Josefsson52c38022004-09-22 19:39:40 +0000502 } else {
503 DEBUGP_C("module, target=%s\n", GET_TARGET(e)->u.user.name);
504 r->type = IPTCC_R_MODULE;
Harald Welteaae69be2004-08-29 23:32:14 +0000505 }
506
507 list_add_tail(&r->list, &h->chain_iterator_cur->rules);
Martin Josefsson8d1b38a2004-09-22 21:00:19 +0000508 h->chain_iterator_cur->num_rules++;
Harald Welteaae69be2004-08-29 23:32:14 +0000509 }
510out_inc:
511 (*num)++;
512 return 0;
513}
514
515
516/* parse an iptables blob into it's pieces */
517static int parse_table(TC_HANDLE_T h)
518{
519 STRUCT_ENTRY *prev;
520 unsigned int num = 0;
521 struct chain_head *c;
522
523 /* First pass: over ruleset blob */
524 ENTRY_ITERATE(h->entries->entrytable, h->entries->size,
525 cache_add_entry, h, &prev, &num);
526
527 /* Second pass: fixup parsed data from first pass */
528 list_for_each_entry(c, &h->chains, list) {
529 struct rule_head *r;
530 list_for_each_entry(r, &c->rules, list) {
531 struct chain_head *c;
532 STRUCT_STANDARD_TARGET *t;
533
534 if (r->type != IPTCC_R_JUMP)
535 continue;
536
537 t = (STRUCT_STANDARD_TARGET *)GET_TARGET(r->entry);
538 c = iptcc_find_chain_by_offset(h, t->verdict);
539 if (!c)
540 return -1;
541 r->jump = c;
542 c->references++;
543 }
544 }
545
546 /* FIXME: sort chains */
547
548 return 1;
549}
550
551
552/**********************************************************************
553 * RULESET COMPILATION (cache -> blob)
554 **********************************************************************/
555
556/* Convenience structures */
557struct iptcb_chain_start{
558 STRUCT_ENTRY e;
559 struct ipt_error_target name;
560};
561#define IPTCB_CHAIN_START_SIZE (sizeof(STRUCT_ENTRY) + \
562 ALIGN(sizeof(struct ipt_error_target)))
563
564struct iptcb_chain_foot {
565 STRUCT_ENTRY e;
566 STRUCT_STANDARD_TARGET target;
567};
568#define IPTCB_CHAIN_FOOT_SIZE (sizeof(STRUCT_ENTRY) + \
569 ALIGN(sizeof(STRUCT_STANDARD_TARGET)))
570
571struct iptcb_chain_error {
572 STRUCT_ENTRY entry;
573 struct ipt_error_target target;
574};
575#define IPTCB_CHAIN_ERROR_SIZE (sizeof(STRUCT_ENTRY) + \
576 ALIGN(sizeof(struct ipt_error_target)))
577
578
579
580/* compile rule from cache into blob */
581static inline int iptcc_compile_rule (TC_HANDLE_T h, STRUCT_REPLACE *repl, struct rule_head *r)
582{
583 /* handle jumps */
584 if (r->type == IPTCC_R_JUMP) {
585 STRUCT_STANDARD_TARGET *t;
586 t = (STRUCT_STANDARD_TARGET *)GET_TARGET(r->entry);
587 /* memset for memcmp convenience on delete/replace */
588 memset(t->target.u.user.name, 0, FUNCTION_MAXNAMELEN);
589 strcpy(t->target.u.user.name, STANDARD_TARGET);
590 /* Jumps can only happen to builtin chains, so we
591 * can safely assume that they always have a header */
592 t->verdict = r->jump->head_offset + IPTCB_CHAIN_START_SIZE;
593 } else if (r->type == IPTCC_R_FALLTHROUGH) {
594 STRUCT_STANDARD_TARGET *t;
595 t = (STRUCT_STANDARD_TARGET *)GET_TARGET(r->entry);
596 t->verdict = r->offset + r->size;
597 }
598
599 /* copy entry from cache to blob */
600 memcpy((char *)repl->entries+r->offset, r->entry, r->size);
601
602 return 1;
603}
604
605/* compile chain from cache into blob */
606static int iptcc_compile_chain(TC_HANDLE_T h, STRUCT_REPLACE *repl, struct chain_head *c)
607{
608 int ret;
609 struct rule_head *r;
610 struct iptcb_chain_start *head;
611 struct iptcb_chain_foot *foot;
612
613 /* only user-defined chains have heaer */
614 if (!iptcc_is_builtin(c)) {
615 /* put chain header in place */
616 head = (void *)repl->entries + c->head_offset;
617 head->e.target_offset = sizeof(STRUCT_ENTRY);
618 head->e.next_offset = IPTCB_CHAIN_START_SIZE;
619 strcpy(head->name.t.u.user.name, ERROR_TARGET);
620 head->name.t.u.target_size =
621 ALIGN(sizeof(struct ipt_error_target));
622 strcpy(head->name.error, c->name);
623 } else {
624 repl->hook_entry[c->hooknum-1] = c->head_offset;
625 repl->underflow[c->hooknum-1] = c->foot_offset;
626 }
627
628 /* iterate over rules */
629 list_for_each_entry(r, &c->rules, list) {
630 ret = iptcc_compile_rule(h, repl, r);
631 if (ret < 0)
632 return ret;
633 }
634
635 /* put chain footer in place */
636 foot = (void *)repl->entries + c->foot_offset;
637 foot->e.target_offset = sizeof(STRUCT_ENTRY);
638 foot->e.next_offset = IPTCB_CHAIN_FOOT_SIZE;
639 strcpy(foot->target.target.u.user.name, STANDARD_TARGET);
640 foot->target.target.u.target_size =
641 ALIGN(sizeof(STRUCT_STANDARD_TARGET));
642 /* builtin targets have verdict, others return */
643 if (iptcc_is_builtin(c))
644 foot->target.verdict = c->verdict;
645 else
646 foot->target.verdict = RETURN;
647 /* set policy-counters */
648 memcpy(&foot->e.counters, &c->counters, sizeof(STRUCT_COUNTERS));
649
650 return 0;
651}
652
653/* calculate offset and number for every rule in the cache */
654static int iptcc_compile_chain_offsets(TC_HANDLE_T h, struct chain_head *c,
655 int *offset, int *num)
656{
657 struct rule_head *r;
658
659 c->head_offset = *offset;
660 DEBUGP("%s: chain_head %u, offset=%u\n", c->name, *num, *offset);
661
662 if (!iptcc_is_builtin(c)) {
663 /* Chain has header */
664 *offset += sizeof(STRUCT_ENTRY)
665 + ALIGN(sizeof(struct ipt_error_target));
666 (*num)++;
667 }
668
669 list_for_each_entry(r, &c->rules, list) {
670 DEBUGP("rule %u, offset=%u, index=%u\n", *num, *offset, *num);
671 r->offset = *offset;
672 r->index = *num;
673 *offset += r->size;
674 (*num)++;
675 }
676
677 DEBUGP("%s; chain_foot %u, offset=%u, index=%u\n", c->name, *num,
678 *offset, *num);
679 c->foot_offset = *offset;
680 c->foot_index = *num;
681 *offset += sizeof(STRUCT_ENTRY)
682 + ALIGN(sizeof(STRUCT_STANDARD_TARGET));
683 (*num)++;
684
685 return 1;
686}
687
688/* put the pieces back together again */
689static int iptcc_compile_table_prep(TC_HANDLE_T h, unsigned int *size)
690{
691 struct chain_head *c;
692 unsigned int offset = 0, num = 0;
693 int ret = 0;
694
695 /* First pass: calculate offset for every rule */
696 list_for_each_entry(c, &h->chains, list) {
697 ret = iptcc_compile_chain_offsets(h, c, &offset, &num);
698 if (ret < 0)
699 return ret;
700 }
701
702 /* Append one error rule at end of chain */
703 num++;
704 offset += sizeof(STRUCT_ENTRY)
705 + ALIGN(sizeof(struct ipt_error_target));
706
707 /* ruleset size is now in offset */
708 *size = offset;
709 return num;
710}
711
712static int iptcc_compile_table(TC_HANDLE_T h, STRUCT_REPLACE *repl)
713{
714 struct chain_head *c;
715 struct iptcb_chain_error *error;
716
717 /* Second pass: copy from cache to offsets, fill in jumps */
718 list_for_each_entry(c, &h->chains, list) {
719 int ret = iptcc_compile_chain(h, repl, c);
720 if (ret < 0)
721 return ret;
722 }
723
724 /* Append error rule at end of chain */
725 error = (void *)repl->entries + repl->size - IPTCB_CHAIN_ERROR_SIZE;
726 error->entry.target_offset = sizeof(STRUCT_ENTRY);
727 error->entry.next_offset = IPTCB_CHAIN_ERROR_SIZE;
728 error->target.t.u.user.target_size =
729 ALIGN(sizeof(struct ipt_error_target));
730 strcpy((char *)&error->target.t.u.user.name, ERROR_TARGET);
731 strcpy((char *)&error->target.error, "ERROR");
732
733 return 1;
734}
735
736/**********************************************************************
737 * EXTERNAL API (operates on cache only)
738 **********************************************************************/
Marc Bouchere6869a82000-03-20 06:03:29 +0000739
740/* Allocate handle of given size */
Rusty Russell79dee072000-05-02 16:45:16 +0000741static TC_HANDLE_T
Harald Welte0113fe72004-01-06 19:04:02 +0000742alloc_handle(const char *tablename, unsigned int size, unsigned int num_rules)
Marc Bouchere6869a82000-03-20 06:03:29 +0000743{
744 size_t len;
Rusty Russell79dee072000-05-02 16:45:16 +0000745 TC_HANDLE_T h;
Marc Bouchere6869a82000-03-20 06:03:29 +0000746
Harald Welteaae69be2004-08-29 23:32:14 +0000747 len = sizeof(STRUCT_TC_HANDLE) + size;
Marc Bouchere6869a82000-03-20 06:03:29 +0000748
Harald Welteaae69be2004-08-29 23:32:14 +0000749 h = malloc(sizeof(STRUCT_TC_HANDLE));
750 if (!h) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000751 errno = ENOMEM;
752 return NULL;
753 }
Harald Welteaae69be2004-08-29 23:32:14 +0000754 memset(h, 0, sizeof(*h));
755 INIT_LIST_HEAD(&h->chains);
Marc Bouchere6869a82000-03-20 06:03:29 +0000756 strcpy(h->info.name, tablename);
Harald Welteaae69be2004-08-29 23:32:14 +0000757
Harald Welte0371c0c2004-09-19 21:00:12 +0000758 h->entries = malloc(sizeof(STRUCT_GET_ENTRIES) + size);
Harald Welteaae69be2004-08-29 23:32:14 +0000759 if (!h->entries)
760 goto out_free_handle;
761
762 strcpy(h->entries->name, tablename);
Harald Welte0371c0c2004-09-19 21:00:12 +0000763 h->entries->size = size;
Marc Bouchere6869a82000-03-20 06:03:29 +0000764
765 return h;
Harald Welteaae69be2004-08-29 23:32:14 +0000766
767out_free_handle:
768 free(h);
769
770 return NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +0000771}
772
Harald Welteaae69be2004-08-29 23:32:14 +0000773
Rusty Russell79dee072000-05-02 16:45:16 +0000774TC_HANDLE_T
775TC_INIT(const char *tablename)
Marc Bouchere6869a82000-03-20 06:03:29 +0000776{
Rusty Russell79dee072000-05-02 16:45:16 +0000777 TC_HANDLE_T h;
778 STRUCT_GETINFO info;
Marc Bouchere6869a82000-03-20 06:03:29 +0000779 int tmp;
780 socklen_t s;
781
Rusty Russell79dee072000-05-02 16:45:16 +0000782 iptc_fn = TC_INIT;
Marc Bouchere6869a82000-03-20 06:03:29 +0000783
Martin Josefssone560fd62003-06-13 16:56:51 +0000784 if (sockfd != -1) {
Harald Welte366454b2002-01-07 13:46:50 +0000785 close(sockfd);
Martin Josefssone560fd62003-06-13 16:56:51 +0000786 sockfd = -1;
787 }
Harald Welte366454b2002-01-07 13:46:50 +0000788
Martin Josefsson841e4ae2003-05-02 15:30:11 +0000789 if (strlen(tablename) >= TABLE_MAXNAMELEN) {
790 errno = EINVAL;
791 return NULL;
792 }
793
Rusty Russell79dee072000-05-02 16:45:16 +0000794 sockfd = socket(TC_AF, SOCK_RAW, IPPROTO_RAW);
Marc Bouchere6869a82000-03-20 06:03:29 +0000795 if (sockfd < 0)
796 return NULL;
797
798 s = sizeof(info);
Martin Josefsson841e4ae2003-05-02 15:30:11 +0000799
Marc Bouchere6869a82000-03-20 06:03:29 +0000800 strcpy(info.name, tablename);
Rusty Russell79dee072000-05-02 16:45:16 +0000801 if (getsockopt(sockfd, TC_IPPROTO, SO_GET_INFO, &info, &s) < 0)
Marc Bouchere6869a82000-03-20 06:03:29 +0000802 return NULL;
803
Harald Welteaae69be2004-08-29 23:32:14 +0000804 DEBUGP("valid_hooks=0x%08x, num_entries=%u, size=%u\n",
805 info.valid_hooks, info.num_entries, info.size);
806
Harald Welte0113fe72004-01-06 19:04:02 +0000807 if ((h = alloc_handle(info.name, info.size, info.num_entries))
Martin Josefsson841e4ae2003-05-02 15:30:11 +0000808 == NULL) {
809 close(sockfd);
Martin Josefssone560fd62003-06-13 16:56:51 +0000810 sockfd = -1;
Marc Bouchere6869a82000-03-20 06:03:29 +0000811 return NULL;
Martin Josefsson841e4ae2003-05-02 15:30:11 +0000812 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000813
Marc Bouchere6869a82000-03-20 06:03:29 +0000814 /* Initialize current state */
815 h->info = info;
Harald Welte0113fe72004-01-06 19:04:02 +0000816
Harald Welteaae69be2004-08-29 23:32:14 +0000817 h->entries->size = h->info.size;
Marc Bouchere6869a82000-03-20 06:03:29 +0000818
Rusty Russell79dee072000-05-02 16:45:16 +0000819 tmp = sizeof(STRUCT_GET_ENTRIES) + h->info.size;
Marc Bouchere6869a82000-03-20 06:03:29 +0000820
Harald Welteaae69be2004-08-29 23:32:14 +0000821 if (getsockopt(sockfd, TC_IPPROTO, SO_GET_ENTRIES, h->entries,
822 &tmp) < 0)
823 goto error;
824
825#ifdef IPTC_DEBUG2
826 {
827 int fd = open("/tmp/libiptc-so_get_entries.blob",
828 O_CREAT|O_WRONLY);
829 if (fd >= 0) {
830 write(fd, h->entries, tmp);
831 close(fd);
832 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000833 }
Harald Welteaae69be2004-08-29 23:32:14 +0000834#endif
835
836 if (parse_table(h) < 0)
837 goto error;
Rusty Russell7e53bf92000-03-20 07:03:28 +0000838
Marc Bouchere6869a82000-03-20 06:03:29 +0000839 CHECK(h);
840 return h;
Harald Welteaae69be2004-08-29 23:32:14 +0000841error:
842 TC_FREE(&h);
843 return NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +0000844}
845
Martin Josefsson841e4ae2003-05-02 15:30:11 +0000846void
847TC_FREE(TC_HANDLE_T *h)
848{
Harald Welteaae69be2004-08-29 23:32:14 +0000849 struct chain_head *c, *tmp;
850
Martin Josefsson841e4ae2003-05-02 15:30:11 +0000851 close(sockfd);
Martin Josefssone560fd62003-06-13 16:56:51 +0000852 sockfd = -1;
Harald Welteaae69be2004-08-29 23:32:14 +0000853
854 list_for_each_entry_safe(c, tmp, &(*h)->chains, list) {
855 struct rule_head *r, *rtmp;
856
857 list_for_each_entry_safe(r, rtmp, &c->rules, list) {
858 free(r);
859 }
860
861 free(c);
862 }
863
864 free((*h)->entries);
Martin Josefsson841e4ae2003-05-02 15:30:11 +0000865 free(*h);
Harald Welteaae69be2004-08-29 23:32:14 +0000866
Martin Josefsson841e4ae2003-05-02 15:30:11 +0000867 *h = NULL;
868}
869
Marc Bouchere6869a82000-03-20 06:03:29 +0000870static inline int
Rusty Russell79dee072000-05-02 16:45:16 +0000871print_match(const STRUCT_ENTRY_MATCH *m)
Marc Bouchere6869a82000-03-20 06:03:29 +0000872{
Rusty Russell228e98d2000-04-27 10:28:06 +0000873 printf("Match name: `%s'\n", m->u.user.name);
Marc Bouchere6869a82000-03-20 06:03:29 +0000874 return 0;
875}
876
Rusty Russell79dee072000-05-02 16:45:16 +0000877static int dump_entry(STRUCT_ENTRY *e, const TC_HANDLE_T handle);
878
Marc Bouchere6869a82000-03-20 06:03:29 +0000879void
Rusty Russell79dee072000-05-02 16:45:16 +0000880TC_DUMP_ENTRIES(const TC_HANDLE_T handle)
Marc Bouchere6869a82000-03-20 06:03:29 +0000881{
882 CHECK(handle);
Harald Welteaae69be2004-08-29 23:32:14 +0000883#if 0
Marc Bouchere6869a82000-03-20 06:03:29 +0000884 printf("libiptc v%s. %u entries, %u bytes.\n",
Harald Welte80fe35d2002-05-29 13:08:15 +0000885 IPTABLES_VERSION,
Harald Welteaae69be2004-08-29 23:32:14 +0000886 handle->new_number, handle->entries->size);
Marc Bouchere6869a82000-03-20 06:03:29 +0000887 printf("Table `%s'\n", handle->info.name);
888 printf("Hooks: pre/in/fwd/out/post = %u/%u/%u/%u/%u\n",
Rusty Russell67088e72000-05-10 01:18:57 +0000889 handle->info.hook_entry[HOOK_PRE_ROUTING],
890 handle->info.hook_entry[HOOK_LOCAL_IN],
891 handle->info.hook_entry[HOOK_FORWARD],
892 handle->info.hook_entry[HOOK_LOCAL_OUT],
893 handle->info.hook_entry[HOOK_POST_ROUTING]);
Marc Bouchere6869a82000-03-20 06:03:29 +0000894 printf("Underflows: pre/in/fwd/out/post = %u/%u/%u/%u/%u\n",
Rusty Russell67088e72000-05-10 01:18:57 +0000895 handle->info.underflow[HOOK_PRE_ROUTING],
896 handle->info.underflow[HOOK_LOCAL_IN],
897 handle->info.underflow[HOOK_FORWARD],
898 handle->info.underflow[HOOK_LOCAL_OUT],
899 handle->info.underflow[HOOK_POST_ROUTING]);
Marc Bouchere6869a82000-03-20 06:03:29 +0000900
Harald Welteaae69be2004-08-29 23:32:14 +0000901 ENTRY_ITERATE(handle->entries->entrytable, handle->entries->size,
Rusty Russell79dee072000-05-02 16:45:16 +0000902 dump_entry, handle);
Harald Welteaae69be2004-08-29 23:32:14 +0000903#endif
Harald Welte0113fe72004-01-06 19:04:02 +0000904}
Rusty Russell30fd6e52000-04-23 09:16:06 +0000905
Marc Bouchere6869a82000-03-20 06:03:29 +0000906/* Does this chain exist? */
Rusty Russell79dee072000-05-02 16:45:16 +0000907int TC_IS_CHAIN(const char *chain, const TC_HANDLE_T handle)
Marc Bouchere6869a82000-03-20 06:03:29 +0000908{
Harald Welteaae69be2004-08-29 23:32:14 +0000909 return iptcc_find_label(chain, handle) != NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +0000910}
911
Harald Welteaae69be2004-08-29 23:32:14 +0000912static void iptcc_chain_iterator_advance(TC_HANDLE_T handle)
913{
914 struct chain_head *c = handle->chain_iterator_cur;
915
916 if (c->list.next == &handle->chains)
917 handle->chain_iterator_cur = NULL;
918 else
919 handle->chain_iterator_cur =
920 list_entry(c->list.next, struct chain_head, list);
921}
Marc Bouchere6869a82000-03-20 06:03:29 +0000922
Rusty Russell30fd6e52000-04-23 09:16:06 +0000923/* Iterator functions to run through the chains. */
Marc Bouchere6869a82000-03-20 06:03:29 +0000924const char *
Philip Blundell8c700902000-05-15 02:17:52 +0000925TC_FIRST_CHAIN(TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +0000926{
Harald Welteaae69be2004-08-29 23:32:14 +0000927 struct chain_head *c = list_entry((*handle)->chains.next,
928 struct chain_head, list);
929
930 iptc_fn = TC_FIRST_CHAIN;
931
932
933 if (list_empty(&(*handle)->chains)) {
934 DEBUGP(": no chains\n");
Harald Welte0113fe72004-01-06 19:04:02 +0000935 return NULL;
Harald Welteaae69be2004-08-29 23:32:14 +0000936 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000937
Harald Welteaae69be2004-08-29 23:32:14 +0000938 (*handle)->chain_iterator_cur = c;
939 iptcc_chain_iterator_advance(*handle);
Harald Welte0113fe72004-01-06 19:04:02 +0000940
Harald Welteaae69be2004-08-29 23:32:14 +0000941 DEBUGP(": returning `%s'\n", c->name);
942 return c->name;
Marc Bouchere6869a82000-03-20 06:03:29 +0000943}
944
Rusty Russell30fd6e52000-04-23 09:16:06 +0000945/* Iterator functions to run through the chains. Returns NULL at end. */
946const char *
Rusty Russell79dee072000-05-02 16:45:16 +0000947TC_NEXT_CHAIN(TC_HANDLE_T *handle)
Rusty Russell30fd6e52000-04-23 09:16:06 +0000948{
Harald Welteaae69be2004-08-29 23:32:14 +0000949 struct chain_head *c = (*handle)->chain_iterator_cur;
Rusty Russell30fd6e52000-04-23 09:16:06 +0000950
Harald Welteaae69be2004-08-29 23:32:14 +0000951 iptc_fn = TC_NEXT_CHAIN;
952
953 if (!c) {
954 DEBUGP(": no more chains\n");
Rusty Russell30fd6e52000-04-23 09:16:06 +0000955 return NULL;
Harald Welteaae69be2004-08-29 23:32:14 +0000956 }
Rusty Russell30fd6e52000-04-23 09:16:06 +0000957
Harald Welteaae69be2004-08-29 23:32:14 +0000958 iptcc_chain_iterator_advance(*handle);
959
960 DEBUGP(": returning `%s'\n", c->name);
961 return c->name;
Rusty Russell30fd6e52000-04-23 09:16:06 +0000962}
963
964/* Get first rule in the given chain: NULL for empty chain. */
Rusty Russell79dee072000-05-02 16:45:16 +0000965const STRUCT_ENTRY *
Philip Blundell8c700902000-05-15 02:17:52 +0000966TC_FIRST_RULE(const char *chain, TC_HANDLE_T *handle)
Rusty Russell30fd6e52000-04-23 09:16:06 +0000967{
Harald Welteaae69be2004-08-29 23:32:14 +0000968 struct chain_head *c;
969 struct rule_head *r;
Rusty Russell30fd6e52000-04-23 09:16:06 +0000970
Harald Welteaae69be2004-08-29 23:32:14 +0000971 iptc_fn = TC_FIRST_RULE;
972
973 DEBUGP("first rule(%s): ", chain);
974
975 c = iptcc_find_label(chain, *handle);
Rusty Russell30fd6e52000-04-23 09:16:06 +0000976 if (!c) {
977 errno = ENOENT;
978 return NULL;
979 }
980
981 /* Empty chain: single return/policy rule */
Harald Welteaae69be2004-08-29 23:32:14 +0000982 if (list_empty(&c->rules)) {
983 DEBUGP_C("no rules, returning NULL\n");
Rusty Russell30fd6e52000-04-23 09:16:06 +0000984 return NULL;
Harald Welteaae69be2004-08-29 23:32:14 +0000985 }
Rusty Russell30fd6e52000-04-23 09:16:06 +0000986
Harald Welteaae69be2004-08-29 23:32:14 +0000987 r = list_entry(c->rules.next, struct rule_head, list);
988 (*handle)->rule_iterator_cur = r;
989 DEBUGP_C("%p\n", r);
990
991 return r->entry;
Rusty Russell30fd6e52000-04-23 09:16:06 +0000992}
993
994/* Returns NULL when rules run out. */
Rusty Russell79dee072000-05-02 16:45:16 +0000995const STRUCT_ENTRY *
Philip Blundell8c700902000-05-15 02:17:52 +0000996TC_NEXT_RULE(const STRUCT_ENTRY *prev, TC_HANDLE_T *handle)
Rusty Russell30fd6e52000-04-23 09:16:06 +0000997{
Harald Welteaae69be2004-08-29 23:32:14 +0000998 struct rule_head *r;
Rusty Russell30fd6e52000-04-23 09:16:06 +0000999
Harald Welteaae69be2004-08-29 23:32:14 +00001000 DEBUGP("rule_iterator_cur=%p...", (*handle)->rule_iterator_cur);
1001
1002 if (!(*handle)->rule_iterator_cur) {
1003 DEBUGP_C("returning NULL\n");
1004 return NULL;
1005 }
1006
1007 r = list_entry((*handle)->rule_iterator_cur->list.next,
1008 struct rule_head, list);
1009
1010 iptc_fn = TC_NEXT_RULE;
1011
1012 DEBUGP_C("next=%p, head=%p...", &r->list,
1013 &(*handle)->rule_iterator_cur->chain->rules);
1014
1015 if (&r->list == &(*handle)->rule_iterator_cur->chain->rules) {
1016 (*handle)->rule_iterator_cur = NULL;
1017 DEBUGP_C("finished, returning NULL\n");
1018 return NULL;
1019 }
1020
1021 (*handle)->rule_iterator_cur = r;
1022
1023 /* NOTE: prev is without any influence ! */
1024 DEBUGP_C("returning rule %p\n", r);
1025 return r->entry;
Rusty Russell30fd6e52000-04-23 09:16:06 +00001026}
1027
Marc Bouchere6869a82000-03-20 06:03:29 +00001028/* How many rules in this chain? */
1029unsigned int
Rusty Russell79dee072000-05-02 16:45:16 +00001030TC_NUM_RULES(const char *chain, TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001031{
Harald Welteaae69be2004-08-29 23:32:14 +00001032 struct chain_head *c;
1033 iptc_fn = TC_NUM_RULES;
Marc Bouchere6869a82000-03-20 06:03:29 +00001034 CHECK(*handle);
Harald Welteaae69be2004-08-29 23:32:14 +00001035
1036 c = iptcc_find_label(chain, *handle);
1037 if (!c) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001038 errno = ENOENT;
1039 return (unsigned int)-1;
1040 }
Harald Welteaae69be2004-08-29 23:32:14 +00001041
1042 return c->num_rules;
Marc Bouchere6869a82000-03-20 06:03:29 +00001043}
1044
Rusty Russell79dee072000-05-02 16:45:16 +00001045const STRUCT_ENTRY *TC_GET_RULE(const char *chain,
1046 unsigned int n,
1047 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001048{
Harald Welteaae69be2004-08-29 23:32:14 +00001049 struct chain_head *c;
1050 struct rule_head *r;
1051
1052 iptc_fn = TC_GET_RULE;
Marc Bouchere6869a82000-03-20 06:03:29 +00001053
1054 CHECK(*handle);
Harald Welteaae69be2004-08-29 23:32:14 +00001055
1056 c = iptcc_find_label(chain, *handle);
1057 if (!c) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001058 errno = ENOENT;
1059 return NULL;
1060 }
1061
Harald Welteaae69be2004-08-29 23:32:14 +00001062 r = iptcc_get_rule_num(c, n);
1063 if (!r)
1064 return NULL;
1065 return r->entry;
Marc Bouchere6869a82000-03-20 06:03:29 +00001066}
1067
1068/* Returns a pointer to the target name of this position. */
Harald Welteaae69be2004-08-29 23:32:14 +00001069const char *standard_target_map(int verdict)
Marc Bouchere6869a82000-03-20 06:03:29 +00001070{
Harald Welteaae69be2004-08-29 23:32:14 +00001071 switch (verdict) {
1072 case RETURN:
1073 return LABEL_RETURN;
1074 break;
1075 case -NF_ACCEPT-1:
1076 return LABEL_ACCEPT;
1077 break;
1078 case -NF_DROP-1:
1079 return LABEL_DROP;
1080 break;
1081 case -NF_QUEUE-1:
1082 return LABEL_QUEUE;
1083 break;
1084 default:
1085 fprintf(stderr, "ERROR: %d not a valid target)\n",
1086 verdict);
1087 abort();
1088 break;
1089 }
1090 /* not reached */
1091 return NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +00001092}
1093
Harald Welteaae69be2004-08-29 23:32:14 +00001094/* Returns a pointer to the target name of this position. */
1095const char *TC_GET_TARGET(const STRUCT_ENTRY *ce,
1096 TC_HANDLE_T *handle)
1097{
1098 STRUCT_ENTRY *e = (STRUCT_ENTRY *)ce;
1099 struct rule_head *r = container_of(e, struct rule_head, entry);
1100
1101 iptc_fn = TC_GET_TARGET;
1102
1103 switch(r->type) {
1104 int spos;
1105 case IPTCC_R_FALLTHROUGH:
1106 return "";
1107 break;
1108 case IPTCC_R_JUMP:
1109 DEBUGP("r=%p, jump=%p, name=`%s'\n", r, r->jump, r->jump->name);
1110 return r->jump->name;
1111 break;
1112 case IPTCC_R_STANDARD:
1113 spos = *(int *)GET_TARGET(e)->data;
1114 DEBUGP("r=%p, spos=%d'\n", r, spos);
1115 return standard_target_map(spos);
1116 break;
1117 case IPTCC_R_MODULE:
1118 return GET_TARGET(e)->u.user.name;
1119 break;
1120 }
1121 return NULL;
1122}
Marc Bouchere6869a82000-03-20 06:03:29 +00001123/* Is this a built-in chain? Actually returns hook + 1. */
1124int
Rusty Russell79dee072000-05-02 16:45:16 +00001125TC_BUILTIN(const char *chain, const TC_HANDLE_T handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001126{
Harald Welteaae69be2004-08-29 23:32:14 +00001127 struct chain_head *c;
1128
1129 iptc_fn = TC_BUILTIN;
Marc Bouchere6869a82000-03-20 06:03:29 +00001130
Harald Welteaae69be2004-08-29 23:32:14 +00001131 c = iptcc_find_label(chain, handle);
1132 if (!c) {
1133 errno = ENOENT;
1134 return -1;
Marc Bouchere6869a82000-03-20 06:03:29 +00001135 }
Harald Welteaae69be2004-08-29 23:32:14 +00001136
1137 return iptcc_is_builtin(c);
Marc Bouchere6869a82000-03-20 06:03:29 +00001138}
1139
1140/* Get the policy of a given built-in chain */
1141const char *
Rusty Russell79dee072000-05-02 16:45:16 +00001142TC_GET_POLICY(const char *chain,
1143 STRUCT_COUNTERS *counters,
1144 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001145{
Harald Welteaae69be2004-08-29 23:32:14 +00001146 struct chain_head *c;
Marc Bouchere6869a82000-03-20 06:03:29 +00001147
Harald Welteaae69be2004-08-29 23:32:14 +00001148 iptc_fn = TC_GET_POLICY;
1149
1150 DEBUGP("called for chain %s\n", chain);
1151
1152 c = iptcc_find_label(chain, *handle);
1153 if (!c) {
1154 errno = ENOENT;
1155 return NULL;
1156 }
1157
1158 if (!iptcc_is_builtin(c))
Marc Bouchere6869a82000-03-20 06:03:29 +00001159 return NULL;
1160
Harald Welteaae69be2004-08-29 23:32:14 +00001161 *counters = c->counters;
Marc Bouchere6869a82000-03-20 06:03:29 +00001162
Harald Welteaae69be2004-08-29 23:32:14 +00001163 return standard_target_map(c->verdict);
Harald Welte0113fe72004-01-06 19:04:02 +00001164}
1165
1166static int
Harald Welteaae69be2004-08-29 23:32:14 +00001167iptcc_standard_map(struct rule_head *r, int verdict)
Harald Welte0113fe72004-01-06 19:04:02 +00001168{
Harald Welteaae69be2004-08-29 23:32:14 +00001169 STRUCT_ENTRY *e = r->entry;
Rusty Russell79dee072000-05-02 16:45:16 +00001170 STRUCT_STANDARD_TARGET *t;
Marc Bouchere6869a82000-03-20 06:03:29 +00001171
Rusty Russell79dee072000-05-02 16:45:16 +00001172 t = (STRUCT_STANDARD_TARGET *)GET_TARGET(e);
Marc Bouchere6869a82000-03-20 06:03:29 +00001173
Rusty Russell67088e72000-05-10 01:18:57 +00001174 if (t->target.u.target_size
Philip Blundell8c700902000-05-15 02:17:52 +00001175 != ALIGN(sizeof(STRUCT_STANDARD_TARGET))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001176 errno = EINVAL;
1177 return 0;
1178 }
1179 /* memset for memcmp convenience on delete/replace */
Rusty Russell79dee072000-05-02 16:45:16 +00001180 memset(t->target.u.user.name, 0, FUNCTION_MAXNAMELEN);
1181 strcpy(t->target.u.user.name, STANDARD_TARGET);
Marc Bouchere6869a82000-03-20 06:03:29 +00001182 t->verdict = verdict;
1183
Harald Welteaae69be2004-08-29 23:32:14 +00001184 r->type = IPTCC_R_STANDARD;
1185
Marc Bouchere6869a82000-03-20 06:03:29 +00001186 return 1;
1187}
Rusty Russell7e53bf92000-03-20 07:03:28 +00001188
Marc Bouchere6869a82000-03-20 06:03:29 +00001189static int
Harald Welteaae69be2004-08-29 23:32:14 +00001190iptcc_map_target(const TC_HANDLE_T handle,
1191 struct rule_head *r)
Marc Bouchere6869a82000-03-20 06:03:29 +00001192{
Harald Welteaae69be2004-08-29 23:32:14 +00001193 STRUCT_ENTRY *e = r->entry;
Harald Welte0113fe72004-01-06 19:04:02 +00001194 STRUCT_ENTRY_TARGET *t = GET_TARGET(e);
Marc Bouchere6869a82000-03-20 06:03:29 +00001195
Marc Bouchere6869a82000-03-20 06:03:29 +00001196 /* Maybe it's empty (=> fall through) */
Harald Welteaae69be2004-08-29 23:32:14 +00001197 if (strcmp(t->u.user.name, "") == 0) {
1198 r->type = IPTCC_R_FALLTHROUGH;
1199 return 1;
1200 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001201 /* Maybe it's a standard target name... */
Rusty Russell79dee072000-05-02 16:45:16 +00001202 else if (strcmp(t->u.user.name, LABEL_ACCEPT) == 0)
Harald Welteaae69be2004-08-29 23:32:14 +00001203 return iptcc_standard_map(r, -NF_ACCEPT - 1);
Rusty Russell79dee072000-05-02 16:45:16 +00001204 else if (strcmp(t->u.user.name, LABEL_DROP) == 0)
Harald Welteaae69be2004-08-29 23:32:14 +00001205 return iptcc_standard_map(r, -NF_DROP - 1);
Rusty Russell67088e72000-05-10 01:18:57 +00001206 else if (strcmp(t->u.user.name, LABEL_QUEUE) == 0)
Harald Welteaae69be2004-08-29 23:32:14 +00001207 return iptcc_standard_map(r, -NF_QUEUE - 1);
Rusty Russell79dee072000-05-02 16:45:16 +00001208 else if (strcmp(t->u.user.name, LABEL_RETURN) == 0)
Harald Welteaae69be2004-08-29 23:32:14 +00001209 return iptcc_standard_map(r, RETURN);
Rusty Russell79dee072000-05-02 16:45:16 +00001210 else if (TC_BUILTIN(t->u.user.name, handle)) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001211 /* Can't jump to builtins. */
1212 errno = EINVAL;
1213 return 0;
1214 } else {
1215 /* Maybe it's an existing chain name. */
Harald Welteaae69be2004-08-29 23:32:14 +00001216 struct chain_head *c;
1217 DEBUGP("trying to find chain `%s': ", t->u.user.name);
Marc Bouchere6869a82000-03-20 06:03:29 +00001218
Harald Welteaae69be2004-08-29 23:32:14 +00001219 c = iptcc_find_label(t->u.user.name, handle);
1220 if (c) {
1221 DEBUGP_C("found!\n");
1222 r->type = IPTCC_R_JUMP;
1223 r->jump = c;
1224 c->references++;
1225 return 1;
1226 }
1227 DEBUGP_C("not found :(\n");
Marc Bouchere6869a82000-03-20 06:03:29 +00001228 }
1229
1230 /* Must be a module? If not, kernel will reject... */
1231 /* memset to all 0 for your memcmp convenience. */
Rusty Russell228e98d2000-04-27 10:28:06 +00001232 memset(t->u.user.name + strlen(t->u.user.name),
Marc Bouchere6869a82000-03-20 06:03:29 +00001233 0,
Rusty Russell79dee072000-05-02 16:45:16 +00001234 FUNCTION_MAXNAMELEN - strlen(t->u.user.name));
Harald Welteaae69be2004-08-29 23:32:14 +00001235
1236 set_changed(handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001237 return 1;
1238}
1239
Harald Welte0113fe72004-01-06 19:04:02 +00001240/* Insert the entry `fw' in chain `chain' into position `rulenum'. */
Marc Bouchere6869a82000-03-20 06:03:29 +00001241int
Rusty Russell79dee072000-05-02 16:45:16 +00001242TC_INSERT_ENTRY(const IPT_CHAINLABEL chain,
1243 const STRUCT_ENTRY *e,
1244 unsigned int rulenum,
1245 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001246{
Harald Welteaae69be2004-08-29 23:32:14 +00001247 struct chain_head *c;
Martin Josefssoneb066cc2004-09-22 21:04:07 +00001248 struct rule_head *r;
1249 struct list_head *prev;
Marc Bouchere6869a82000-03-20 06:03:29 +00001250
Rusty Russell79dee072000-05-02 16:45:16 +00001251 iptc_fn = TC_INSERT_ENTRY;
Harald Welteaae69be2004-08-29 23:32:14 +00001252
1253 if (!(c = iptcc_find_label(chain, *handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001254 errno = ENOENT;
1255 return 0;
1256 }
1257
Martin Josefssoneb066cc2004-09-22 21:04:07 +00001258 /* first rulenum index = 0
1259 first c->num_rules index = 1 */
1260 if (rulenum > c->num_rules) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001261 errno = E2BIG;
1262 return 0;
1263 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001264
Martin Josefssoneb066cc2004-09-22 21:04:07 +00001265 /* Try to get the rule we want to insert after.
1266 In case of no rules, insert after chain head. */
1267 r = iptcc_get_rule_num(c, rulenum + 1);
1268 if (r)
1269 prev = &r->list;
1270 else
1271 prev = &c->rules;
1272
Harald Welteaae69be2004-08-29 23:32:14 +00001273 if (!(r = iptcc_alloc_rule(c, e->next_offset))) {
1274 errno = ENOMEM;
Harald Welte0113fe72004-01-06 19:04:02 +00001275 return 0;
Harald Welteaae69be2004-08-29 23:32:14 +00001276 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001277
Harald Welteaae69be2004-08-29 23:32:14 +00001278 memcpy(r->entry, e, e->next_offset);
1279 r->counter_map.maptype = COUNTER_MAP_SET;
1280
1281 if (!iptcc_map_target(*handle, r)) {
1282 free(r);
1283 return 0;
1284 }
1285
Martin Josefssoneb066cc2004-09-22 21:04:07 +00001286 list_add_tail(&r->list, prev);
Harald Welteaae69be2004-08-29 23:32:14 +00001287 c->num_rules++;
1288
1289 set_changed(*handle);
1290
1291 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00001292}
1293
1294/* Atomically replace rule `rulenum' in `chain' with `fw'. */
1295int
Rusty Russell79dee072000-05-02 16:45:16 +00001296TC_REPLACE_ENTRY(const IPT_CHAINLABEL chain,
1297 const STRUCT_ENTRY *e,
1298 unsigned int rulenum,
1299 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001300{
Harald Welteaae69be2004-08-29 23:32:14 +00001301 struct chain_head *c;
1302 struct rule_head *r, *old;
Marc Bouchere6869a82000-03-20 06:03:29 +00001303
Rusty Russell79dee072000-05-02 16:45:16 +00001304 iptc_fn = TC_REPLACE_ENTRY;
Marc Bouchere6869a82000-03-20 06:03:29 +00001305
Harald Welteaae69be2004-08-29 23:32:14 +00001306 if (!(c = iptcc_find_label(chain, *handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001307 errno = ENOENT;
1308 return 0;
1309 }
1310
Harald Welteaae69be2004-08-29 23:32:14 +00001311 if (!(old = iptcc_get_rule_num(c, rulenum))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001312 errno = E2BIG;
1313 return 0;
1314 }
1315
Harald Welteaae69be2004-08-29 23:32:14 +00001316 if (!(r = iptcc_alloc_rule(c, e->next_offset))) {
1317 errno = ENOMEM;
Marc Bouchere6869a82000-03-20 06:03:29 +00001318 return 0;
Harald Welteaae69be2004-08-29 23:32:14 +00001319 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001320
Harald Welteaae69be2004-08-29 23:32:14 +00001321 memcpy(r->entry, e, e->next_offset);
1322 r->counter_map.maptype = COUNTER_MAP_SET;
1323
1324 if (!iptcc_map_target(*handle, r)) {
1325 free(r);
Harald Welte0113fe72004-01-06 19:04:02 +00001326 return 0;
Harald Welteaae69be2004-08-29 23:32:14 +00001327 }
Harald Welte0113fe72004-01-06 19:04:02 +00001328
Harald Welteaae69be2004-08-29 23:32:14 +00001329 list_add(&r->list, &old->list);
1330 iptcc_delete_rule(old);
1331
1332 set_changed(*handle);
1333
1334 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00001335}
1336
Harald Welte0113fe72004-01-06 19:04:02 +00001337/* Append entry `fw' to chain `chain'. Equivalent to insert with
Marc Bouchere6869a82000-03-20 06:03:29 +00001338 rulenum = length of chain. */
1339int
Rusty Russell79dee072000-05-02 16:45:16 +00001340TC_APPEND_ENTRY(const IPT_CHAINLABEL chain,
1341 const STRUCT_ENTRY *e,
1342 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001343{
Harald Welteaae69be2004-08-29 23:32:14 +00001344 struct chain_head *c;
1345 struct rule_head *r;
Marc Bouchere6869a82000-03-20 06:03:29 +00001346
Rusty Russell79dee072000-05-02 16:45:16 +00001347 iptc_fn = TC_APPEND_ENTRY;
Harald Welteaae69be2004-08-29 23:32:14 +00001348 if (!(c = iptcc_find_label(chain, *handle))) {
1349 DEBUGP("unable to find chain `%s'\n", chain);
Marc Bouchere6869a82000-03-20 06:03:29 +00001350 errno = ENOENT;
1351 return 0;
1352 }
1353
Harald Welteaae69be2004-08-29 23:32:14 +00001354 if (!(r = iptcc_alloc_rule(c, e->next_offset))) {
1355 DEBUGP("unable to allocate rule for chain `%s'\n", chain);
1356 errno = ENOMEM;
Harald Welte0113fe72004-01-06 19:04:02 +00001357 return 0;
Harald Welteaae69be2004-08-29 23:32:14 +00001358 }
Harald Welte0113fe72004-01-06 19:04:02 +00001359
Harald Welteaae69be2004-08-29 23:32:14 +00001360 memcpy(r->entry, e, e->next_offset);
1361 r->counter_map.maptype = COUNTER_MAP_SET;
1362
1363 if (!iptcc_map_target(*handle, r)) {
1364 DEBUGP("unable to ma target of rule for chain `%s'\n", chain);
1365 free(r);
1366 return 0;
1367 }
1368
1369 list_add_tail(&r->list, &c->rules);
1370 c->num_rules++;
1371
1372 set_changed(*handle);
1373
1374 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00001375}
1376
1377static inline int
Rusty Russell79dee072000-05-02 16:45:16 +00001378match_different(const STRUCT_ENTRY_MATCH *a,
Rusty Russelledf14cf2000-04-19 11:26:44 +00001379 const unsigned char *a_elems,
1380 const unsigned char *b_elems,
1381 unsigned char **maskptr)
Marc Bouchere6869a82000-03-20 06:03:29 +00001382{
Rusty Russell79dee072000-05-02 16:45:16 +00001383 const STRUCT_ENTRY_MATCH *b;
Rusty Russelledf14cf2000-04-19 11:26:44 +00001384 unsigned int i;
Marc Bouchere6869a82000-03-20 06:03:29 +00001385
1386 /* Offset of b is the same as a. */
Rusty Russell30fd6e52000-04-23 09:16:06 +00001387 b = (void *)b_elems + ((unsigned char *)a - a_elems);
Marc Bouchere6869a82000-03-20 06:03:29 +00001388
Rusty Russell228e98d2000-04-27 10:28:06 +00001389 if (a->u.match_size != b->u.match_size)
Marc Bouchere6869a82000-03-20 06:03:29 +00001390 return 1;
1391
Rusty Russell228e98d2000-04-27 10:28:06 +00001392 if (strcmp(a->u.user.name, b->u.user.name) != 0)
Marc Bouchere6869a82000-03-20 06:03:29 +00001393 return 1;
1394
Rusty Russell73ef09b2000-07-03 10:24:04 +00001395 *maskptr += ALIGN(sizeof(*a));
Rusty Russelledf14cf2000-04-19 11:26:44 +00001396
Rusty Russell73ef09b2000-07-03 10:24:04 +00001397 for (i = 0; i < a->u.match_size - ALIGN(sizeof(*a)); i++)
Rusty Russelledf14cf2000-04-19 11:26:44 +00001398 if (((a->data[i] ^ b->data[i]) & (*maskptr)[i]) != 0)
Rusty Russell90e712a2000-03-29 04:19:26 +00001399 return 1;
Rusty Russelledf14cf2000-04-19 11:26:44 +00001400 *maskptr += i;
1401 return 0;
1402}
1403
1404static inline int
1405target_different(const unsigned char *a_targdata,
1406 const unsigned char *b_targdata,
1407 unsigned int tdatasize,
1408 const unsigned char *mask)
1409{
1410 unsigned int i;
1411 for (i = 0; i < tdatasize; i++)
1412 if (((a_targdata[i] ^ b_targdata[i]) & mask[i]) != 0)
1413 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00001414
1415 return 0;
1416}
1417
Rusty Russell79dee072000-05-02 16:45:16 +00001418static int
1419is_same(const STRUCT_ENTRY *a,
1420 const STRUCT_ENTRY *b,
1421 unsigned char *matchmask);
Marc Bouchere6869a82000-03-20 06:03:29 +00001422
Harald Welte0113fe72004-01-06 19:04:02 +00001423/* Delete the first rule in `chain' which matches `fw'. */
Marc Bouchere6869a82000-03-20 06:03:29 +00001424int
Rusty Russell79dee072000-05-02 16:45:16 +00001425TC_DELETE_ENTRY(const IPT_CHAINLABEL chain,
1426 const STRUCT_ENTRY *origfw,
1427 unsigned char *matchmask,
1428 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001429{
Harald Welteaae69be2004-08-29 23:32:14 +00001430 struct chain_head *c;
Harald Weltefe537072004-08-30 20:28:53 +00001431 struct rule_head *r;
Harald Welte0113fe72004-01-06 19:04:02 +00001432 STRUCT_ENTRY *e, *fw;
Marc Bouchere6869a82000-03-20 06:03:29 +00001433
Rusty Russell79dee072000-05-02 16:45:16 +00001434 iptc_fn = TC_DELETE_ENTRY;
Harald Welteaae69be2004-08-29 23:32:14 +00001435 if (!(c = iptcc_find_label(chain, *handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001436 errno = ENOENT;
1437 return 0;
1438 }
1439
Harald Welte0113fe72004-01-06 19:04:02 +00001440 fw = malloc(origfw->next_offset);
1441 if (fw == NULL) {
1442 errno = ENOMEM;
1443 return 0;
1444 }
1445
Harald Weltefe537072004-08-30 20:28:53 +00001446 list_for_each_entry(r, &c->rules, list) {
Harald Welte0113fe72004-01-06 19:04:02 +00001447
1448 memcpy(fw, origfw, origfw->next_offset);
1449
Harald Weltefe537072004-08-30 20:28:53 +00001450#if 0
Harald Welte0113fe72004-01-06 19:04:02 +00001451 /* FIXME: handle this in is_same --RR */
1452 if (!map_target(*handle, fw, offset, &discard)) {
1453 free(fw);
1454 return 0;
1455 }
Harald Welte0113fe72004-01-06 19:04:02 +00001456#endif
Harald Weltefe537072004-08-30 20:28:53 +00001457
1458 e = r->entry;
1459
Harald Welte0113fe72004-01-06 19:04:02 +00001460 if (is_same(e, fw, matchmask)) {
Harald Weltefe537072004-08-30 20:28:53 +00001461 /* If we are about to delete the rule that is the
1462 * current iterator, move rule iterator back. next
1463 * pointer will then point to real next node */
1464 if (r == (*handle)->rule_iterator_cur) {
1465 (*handle)->rule_iterator_cur =
1466 list_entry((*handle)->rule_iterator_cur->list.prev,
1467 struct rule_head, list);
1468 }
1469
1470 c->num_rules--;
1471 iptcc_delete_rule(r);
1472 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00001473 }
1474 }
1475
Harald Welte0113fe72004-01-06 19:04:02 +00001476 free(fw);
Marc Bouchere6869a82000-03-20 06:03:29 +00001477 errno = ENOENT;
1478 return 0;
Rusty Russell7e53bf92000-03-20 07:03:28 +00001479}
Harald Welteaae69be2004-08-29 23:32:14 +00001480
Marc Bouchere6869a82000-03-20 06:03:29 +00001481
1482/* Delete the rule in position `rulenum' in `chain'. */
1483int
Rusty Russell79dee072000-05-02 16:45:16 +00001484TC_DELETE_NUM_ENTRY(const IPT_CHAINLABEL chain,
1485 unsigned int rulenum,
1486 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001487{
Harald Welteaae69be2004-08-29 23:32:14 +00001488 struct chain_head *c;
1489 struct rule_head *r;
Marc Bouchere6869a82000-03-20 06:03:29 +00001490
Rusty Russell79dee072000-05-02 16:45:16 +00001491 iptc_fn = TC_DELETE_NUM_ENTRY;
Harald Welteaae69be2004-08-29 23:32:14 +00001492
1493 if (!(c = iptcc_find_label(chain, *handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001494 errno = ENOENT;
1495 return 0;
1496 }
1497
Harald Welteaae69be2004-08-29 23:32:14 +00001498 if (!(r = iptcc_get_rule_num(c, rulenum))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001499 errno = E2BIG;
1500 return 0;
1501 }
1502
Harald Welteaae69be2004-08-29 23:32:14 +00001503 /* If we are about to delete the rule that is the current
1504 * iterator, move rule iterator back. next pointer will then
1505 * point to real next node */
1506 if (r == (*handle)->rule_iterator_cur) {
1507 (*handle)->rule_iterator_cur =
1508 list_entry((*handle)->rule_iterator_cur->list.prev,
1509 struct rule_head, list);
Harald Welte0113fe72004-01-06 19:04:02 +00001510 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001511
Harald Welteaae69be2004-08-29 23:32:14 +00001512 c->num_rules--;
1513 iptcc_delete_rule(r);
1514
1515 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00001516}
1517
1518/* Check the packet `fw' on chain `chain'. Returns the verdict, or
1519 NULL and sets errno. */
1520const char *
Rusty Russell79dee072000-05-02 16:45:16 +00001521TC_CHECK_PACKET(const IPT_CHAINLABEL chain,
1522 STRUCT_ENTRY *entry,
1523 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001524{
1525 errno = ENOSYS;
1526 return NULL;
1527}
1528
1529/* Flushes the entries in the given chain (ie. empties chain). */
1530int
Rusty Russell79dee072000-05-02 16:45:16 +00001531TC_FLUSH_ENTRIES(const IPT_CHAINLABEL chain, TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001532{
Harald Welteaae69be2004-08-29 23:32:14 +00001533 struct chain_head *c;
1534 struct rule_head *r, *tmp;
Marc Bouchere6869a82000-03-20 06:03:29 +00001535
Harald Welte0113fe72004-01-06 19:04:02 +00001536 iptc_fn = TC_FLUSH_ENTRIES;
Harald Welteaae69be2004-08-29 23:32:14 +00001537 if (!(c = iptcc_find_label(chain, *handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001538 errno = ENOENT;
1539 return 0;
1540 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001541
Harald Welteaae69be2004-08-29 23:32:14 +00001542 list_for_each_entry_safe(r, tmp, &c->rules, list) {
1543 iptcc_delete_rule(r);
1544 }
1545
1546 c->num_rules = 0;
1547
1548 set_changed(*handle);
1549
1550 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00001551}
1552
1553/* Zeroes the counters in a chain. */
1554int
Rusty Russell79dee072000-05-02 16:45:16 +00001555TC_ZERO_ENTRIES(const IPT_CHAINLABEL chain, TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001556{
Harald Welteaae69be2004-08-29 23:32:14 +00001557 struct chain_head *c;
1558 struct rule_head *r;
Rusty Russell7e53bf92000-03-20 07:03:28 +00001559
Harald Welteaae69be2004-08-29 23:32:14 +00001560 if (!(c = iptcc_find_label(chain, *handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001561 errno = ENOENT;
1562 return 0;
1563 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001564
Harald Welteaae69be2004-08-29 23:32:14 +00001565 list_for_each_entry(r, &c->rules, list) {
1566 if (r->counter_map.maptype == COUNTER_MAP_NORMAL_MAP)
1567 r->counter_map.maptype = COUNTER_MAP_ZEROED;
Marc Bouchere6869a82000-03-20 06:03:29 +00001568 }
Harald Welteaae69be2004-08-29 23:32:14 +00001569
Rusty Russell175f6412000-03-24 09:32:20 +00001570 set_changed(*handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001571
Marc Bouchere6869a82000-03-20 06:03:29 +00001572 return 1;
1573}
1574
Harald Welte1cef74d2001-01-05 15:22:59 +00001575STRUCT_COUNTERS *
1576TC_READ_COUNTER(const IPT_CHAINLABEL chain,
1577 unsigned int rulenum,
1578 TC_HANDLE_T *handle)
1579{
Harald Welteaae69be2004-08-29 23:32:14 +00001580 struct chain_head *c;
1581 struct rule_head *r;
Harald Welte1cef74d2001-01-05 15:22:59 +00001582
1583 iptc_fn = TC_READ_COUNTER;
1584 CHECK(*handle);
1585
Harald Welteaae69be2004-08-29 23:32:14 +00001586 if (!(c = iptcc_find_label(chain, *handle))) {
Harald Welte1cef74d2001-01-05 15:22:59 +00001587 errno = ENOENT;
1588 return NULL;
1589 }
1590
Harald Welteaae69be2004-08-29 23:32:14 +00001591 if (!(r = iptcc_get_rule_num(c, rulenum))) {
Harald Welte0113fe72004-01-06 19:04:02 +00001592 errno = E2BIG;
1593 return NULL;
1594 }
1595
Harald Welteaae69be2004-08-29 23:32:14 +00001596 return &r->entry[0].counters;
Harald Welte1cef74d2001-01-05 15:22:59 +00001597}
1598
1599int
1600TC_ZERO_COUNTER(const IPT_CHAINLABEL chain,
1601 unsigned int rulenum,
1602 TC_HANDLE_T *handle)
1603{
Harald Welteaae69be2004-08-29 23:32:14 +00001604 struct chain_head *c;
1605 struct rule_head *r;
Harald Welte1cef74d2001-01-05 15:22:59 +00001606
1607 iptc_fn = TC_ZERO_COUNTER;
1608 CHECK(*handle);
1609
Harald Welteaae69be2004-08-29 23:32:14 +00001610 if (!(c = iptcc_find_label(chain, *handle))) {
Harald Welte1cef74d2001-01-05 15:22:59 +00001611 errno = ENOENT;
1612 return 0;
1613 }
1614
Harald Welteaae69be2004-08-29 23:32:14 +00001615 if (!(r = iptcc_get_rule_num(c, rulenum))) {
Harald Welte0113fe72004-01-06 19:04:02 +00001616 errno = E2BIG;
1617 return 0;
1618 }
1619
Harald Welteaae69be2004-08-29 23:32:14 +00001620 if (r->counter_map.maptype == COUNTER_MAP_NORMAL_MAP)
1621 r->counter_map.maptype = COUNTER_MAP_ZEROED;
Harald Welte1cef74d2001-01-05 15:22:59 +00001622
1623 set_changed(*handle);
1624
1625 return 1;
1626}
1627
1628int
1629TC_SET_COUNTER(const IPT_CHAINLABEL chain,
1630 unsigned int rulenum,
1631 STRUCT_COUNTERS *counters,
1632 TC_HANDLE_T *handle)
1633{
Harald Welteaae69be2004-08-29 23:32:14 +00001634 struct chain_head *c;
1635 struct rule_head *r;
Harald Welte1cef74d2001-01-05 15:22:59 +00001636 STRUCT_ENTRY *e;
Harald Welte1cef74d2001-01-05 15:22:59 +00001637
1638 iptc_fn = TC_SET_COUNTER;
1639 CHECK(*handle);
1640
Harald Welteaae69be2004-08-29 23:32:14 +00001641 if (!(c = iptcc_find_label(chain, *handle))) {
Harald Welte1cef74d2001-01-05 15:22:59 +00001642 errno = ENOENT;
1643 return 0;
1644 }
Harald Welte0113fe72004-01-06 19:04:02 +00001645
Harald Welteaae69be2004-08-29 23:32:14 +00001646 if (!(r = iptcc_get_rule_num(c, rulenum))) {
Harald Welte0113fe72004-01-06 19:04:02 +00001647 errno = E2BIG;
1648 return 0;
1649 }
1650
Harald Welteaae69be2004-08-29 23:32:14 +00001651 e = r->entry;
1652 r->counter_map.maptype = COUNTER_MAP_SET;
Harald Welte0113fe72004-01-06 19:04:02 +00001653
1654 memcpy(&e->counters, counters, sizeof(STRUCT_COUNTERS));
Harald Welte1cef74d2001-01-05 15:22:59 +00001655
1656 set_changed(*handle);
1657
1658 return 1;
1659}
1660
Marc Bouchere6869a82000-03-20 06:03:29 +00001661/* Creates a new chain. */
1662/* To create a chain, create two rules: error node and unconditional
1663 * return. */
1664int
Rusty Russell79dee072000-05-02 16:45:16 +00001665TC_CREATE_CHAIN(const IPT_CHAINLABEL chain, TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001666{
Harald Welteaae69be2004-08-29 23:32:14 +00001667 static struct chain_head *c;
Marc Bouchere6869a82000-03-20 06:03:29 +00001668
Rusty Russell79dee072000-05-02 16:45:16 +00001669 iptc_fn = TC_CREATE_CHAIN;
Marc Bouchere6869a82000-03-20 06:03:29 +00001670
1671 /* find_label doesn't cover built-in targets: DROP, ACCEPT,
1672 QUEUE, RETURN. */
Harald Welteaae69be2004-08-29 23:32:14 +00001673 if (iptcc_find_label(chain, *handle)
Rusty Russell79dee072000-05-02 16:45:16 +00001674 || strcmp(chain, LABEL_DROP) == 0
1675 || strcmp(chain, LABEL_ACCEPT) == 0
Rusty Russell67088e72000-05-10 01:18:57 +00001676 || strcmp(chain, LABEL_QUEUE) == 0
Rusty Russell79dee072000-05-02 16:45:16 +00001677 || strcmp(chain, LABEL_RETURN) == 0) {
Harald Welteaae69be2004-08-29 23:32:14 +00001678 DEBUGP("Chain `%s' already exists\n", chain);
Marc Bouchere6869a82000-03-20 06:03:29 +00001679 errno = EEXIST;
1680 return 0;
1681 }
1682
Rusty Russell79dee072000-05-02 16:45:16 +00001683 if (strlen(chain)+1 > sizeof(IPT_CHAINLABEL)) {
Harald Welteaae69be2004-08-29 23:32:14 +00001684 DEBUGP("Chain name `%s' too long\n", chain);
Marc Bouchere6869a82000-03-20 06:03:29 +00001685 errno = EINVAL;
1686 return 0;
1687 }
1688
Harald Welteaae69be2004-08-29 23:32:14 +00001689 c = iptcc_alloc_chain_head(chain, 0);
1690 if (!c) {
1691 DEBUGP("Cannot allocate memory for chain `%s'\n", chain);
1692 errno = ENOMEM;
1693 return 0;
Marc Bouchere6869a82000-03-20 06:03:29 +00001694
Harald Welteaae69be2004-08-29 23:32:14 +00001695 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001696
Harald Welteaae69be2004-08-29 23:32:14 +00001697 DEBUGP("Creating chain `%s'\n", chain);
1698 list_add_tail(&c->list, &(*handle)->chains);
Harald Welte0113fe72004-01-06 19:04:02 +00001699
1700 set_changed(*handle);
1701
Harald Welteaae69be2004-08-29 23:32:14 +00001702 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00001703}
1704
1705/* Get the number of references to this chain. */
1706int
Rusty Russell79dee072000-05-02 16:45:16 +00001707TC_GET_REFERENCES(unsigned int *ref, const IPT_CHAINLABEL chain,
1708 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001709{
Harald Welteaae69be2004-08-29 23:32:14 +00001710 struct chain_head *c;
Marc Bouchere6869a82000-03-20 06:03:29 +00001711
Harald Welteaae69be2004-08-29 23:32:14 +00001712 if (!(c = iptcc_find_label(chain, *handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001713 errno = ENOENT;
1714 return 0;
1715 }
1716
Harald Welteaae69be2004-08-29 23:32:14 +00001717 *ref = c->references;
1718
Marc Bouchere6869a82000-03-20 06:03:29 +00001719 return 1;
1720}
1721
1722/* Deletes a chain. */
1723int
Rusty Russell79dee072000-05-02 16:45:16 +00001724TC_DELETE_CHAIN(const IPT_CHAINLABEL chain, TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001725{
Marc Bouchere6869a82000-03-20 06:03:29 +00001726 unsigned int references;
Harald Welteaae69be2004-08-29 23:32:14 +00001727 struct chain_head *c;
Rusty Russell7e53bf92000-03-20 07:03:28 +00001728
Rusty Russell79dee072000-05-02 16:45:16 +00001729 iptc_fn = TC_DELETE_CHAIN;
Marc Bouchere6869a82000-03-20 06:03:29 +00001730
Harald Welteaae69be2004-08-29 23:32:14 +00001731 if (!(c = iptcc_find_label(chain, *handle))) {
1732 DEBUGP("cannot find chain `%s'\n", chain);
Marc Bouchere6869a82000-03-20 06:03:29 +00001733 errno = ENOENT;
1734 return 0;
1735 }
1736
Harald Welteaae69be2004-08-29 23:32:14 +00001737 if (TC_BUILTIN(chain, *handle)) {
1738 DEBUGP("cannot remove builtin chain `%s'\n", chain);
1739 errno = EINVAL;
1740 return 0;
1741 }
1742
1743 if (!TC_GET_REFERENCES(&references, chain, handle)) {
1744 DEBUGP("cannot get references on chain `%s'\n", chain);
1745 return 0;
1746 }
1747
1748 if (references > 0) {
1749 DEBUGP("chain `%s' still has references\n", chain);
1750 errno = EMLINK;
1751 return 0;
1752 }
1753
1754 if (c->num_rules) {
1755 DEBUGP("chain `%s' is not empty\n", chain);
Marc Bouchere6869a82000-03-20 06:03:29 +00001756 errno = ENOTEMPTY;
1757 return 0;
1758 }
1759
Harald Welteaae69be2004-08-29 23:32:14 +00001760 /* If we are about to delete the chain that is the current
1761 * iterator, move chain iterator firward. */
1762 if (c == (*handle)->chain_iterator_cur)
1763 iptcc_chain_iterator_advance(*handle);
Harald Welte0113fe72004-01-06 19:04:02 +00001764
Harald Welteaae69be2004-08-29 23:32:14 +00001765 list_del(&c->list);
1766 free(c);
Harald Welte0113fe72004-01-06 19:04:02 +00001767
Harald Welteaae69be2004-08-29 23:32:14 +00001768 DEBUGP("chain `%s' deleted\n", chain);
1769
1770 set_changed(*handle);
1771
1772 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00001773}
1774
1775/* Renames a chain. */
Rusty Russell79dee072000-05-02 16:45:16 +00001776int TC_RENAME_CHAIN(const IPT_CHAINLABEL oldname,
1777 const IPT_CHAINLABEL newname,
1778 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001779{
Harald Welteaae69be2004-08-29 23:32:14 +00001780 struct chain_head *c;
Rusty Russell79dee072000-05-02 16:45:16 +00001781 iptc_fn = TC_RENAME_CHAIN;
Marc Bouchere6869a82000-03-20 06:03:29 +00001782
Harald Welte1de80462000-10-30 12:00:27 +00001783 /* find_label doesn't cover built-in targets: DROP, ACCEPT,
1784 QUEUE, RETURN. */
Harald Welteaae69be2004-08-29 23:32:14 +00001785 if (iptcc_find_label(newname, *handle)
Rusty Russell79dee072000-05-02 16:45:16 +00001786 || strcmp(newname, LABEL_DROP) == 0
1787 || strcmp(newname, LABEL_ACCEPT) == 0
Harald Welte1de80462000-10-30 12:00:27 +00001788 || strcmp(newname, LABEL_QUEUE) == 0
Rusty Russell79dee072000-05-02 16:45:16 +00001789 || strcmp(newname, LABEL_RETURN) == 0) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001790 errno = EEXIST;
1791 return 0;
1792 }
1793
Harald Welteaae69be2004-08-29 23:32:14 +00001794 if (!(c = iptcc_find_label(oldname, *handle))
Rusty Russell79dee072000-05-02 16:45:16 +00001795 || TC_BUILTIN(oldname, *handle)) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001796 errno = ENOENT;
1797 return 0;
1798 }
1799
Rusty Russell79dee072000-05-02 16:45:16 +00001800 if (strlen(newname)+1 > sizeof(IPT_CHAINLABEL)) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001801 errno = EINVAL;
1802 return 0;
1803 }
1804
Harald Welteaae69be2004-08-29 23:32:14 +00001805 strncpy(c->name, newname, sizeof(IPT_CHAINLABEL));
1806
Harald Welte0113fe72004-01-06 19:04:02 +00001807 set_changed(*handle);
1808
Marc Bouchere6869a82000-03-20 06:03:29 +00001809 return 1;
1810}
1811
1812/* Sets the policy on a built-in chain. */
1813int
Rusty Russell79dee072000-05-02 16:45:16 +00001814TC_SET_POLICY(const IPT_CHAINLABEL chain,
1815 const IPT_CHAINLABEL policy,
Harald Welte1cef74d2001-01-05 15:22:59 +00001816 STRUCT_COUNTERS *counters,
Rusty Russell79dee072000-05-02 16:45:16 +00001817 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001818{
Harald Welteaae69be2004-08-29 23:32:14 +00001819 struct chain_head *c;
Marc Bouchere6869a82000-03-20 06:03:29 +00001820
Rusty Russell79dee072000-05-02 16:45:16 +00001821 iptc_fn = TC_SET_POLICY;
Marc Bouchere6869a82000-03-20 06:03:29 +00001822
Harald Welteaae69be2004-08-29 23:32:14 +00001823 if (!(c = iptcc_find_label(chain, *handle))) {
1824 DEBUGP("cannot find chain `%s'\n", chain);
1825 errno = ENOENT;
Marc Bouchere6869a82000-03-20 06:03:29 +00001826 return 0;
1827 }
1828
Harald Welteaae69be2004-08-29 23:32:14 +00001829 if (!iptcc_is_builtin(c)) {
1830 DEBUGP("cannot set policy of userdefinedchain `%s'\n", chain);
1831 errno = ENOENT;
1832 return 0;
1833 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001834
Rusty Russell79dee072000-05-02 16:45:16 +00001835 if (strcmp(policy, LABEL_ACCEPT) == 0)
Harald Welteaae69be2004-08-29 23:32:14 +00001836 c->verdict = -NF_ACCEPT - 1;
Rusty Russell79dee072000-05-02 16:45:16 +00001837 else if (strcmp(policy, LABEL_DROP) == 0)
Harald Welteaae69be2004-08-29 23:32:14 +00001838 c->verdict = -NF_DROP - 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00001839 else {
1840 errno = EINVAL;
1841 return 0;
1842 }
Harald Welte1cef74d2001-01-05 15:22:59 +00001843
Harald Welte1cef74d2001-01-05 15:22:59 +00001844 if (counters) {
1845 /* set byte and packet counters */
Harald Welteaae69be2004-08-29 23:32:14 +00001846 memcpy(&c->counters, counters, sizeof(STRUCT_COUNTERS));
1847 c->counter_map.maptype = COUNTER_MAP_SET;
Harald Welte1cef74d2001-01-05 15:22:59 +00001848 } else {
Harald Welteaae69be2004-08-29 23:32:14 +00001849 c->counter_map.maptype = COUNTER_MAP_NOMAP;
Harald Welte1cef74d2001-01-05 15:22:59 +00001850 }
1851
Rusty Russell175f6412000-03-24 09:32:20 +00001852 set_changed(*handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001853
Marc Bouchere6869a82000-03-20 06:03:29 +00001854 return 1;
1855}
1856
1857/* Without this, on gcc 2.7.2.3, we get:
Rusty Russell79dee072000-05-02 16:45:16 +00001858 libiptc.c: In function `TC_COMMIT':
Marc Bouchere6869a82000-03-20 06:03:29 +00001859 libiptc.c:833: fixed or forbidden register was spilled.
1860 This may be due to a compiler bug or to impossible asm
1861 statements or clauses.
1862*/
1863static void
Rusty Russell79dee072000-05-02 16:45:16 +00001864subtract_counters(STRUCT_COUNTERS *answer,
1865 const STRUCT_COUNTERS *a,
1866 const STRUCT_COUNTERS *b)
Marc Bouchere6869a82000-03-20 06:03:29 +00001867{
1868 answer->pcnt = a->pcnt - b->pcnt;
1869 answer->bcnt = a->bcnt - b->bcnt;
1870}
1871
Harald Welteaae69be2004-08-29 23:32:14 +00001872
1873static void counters_nomap(STRUCT_COUNTERS_INFO *newcounters,
1874 unsigned int index)
1875{
1876 newcounters->counters[index] = ((STRUCT_COUNTERS) { 0, 0});
1877 DEBUGP_C("NOMAP => zero\n");
1878}
1879
1880static void counters_normal_map(STRUCT_COUNTERS_INFO *newcounters,
1881 STRUCT_REPLACE *repl,
1882 unsigned int index,
1883 unsigned int mappos)
1884{
1885 /* Original read: X.
1886 * Atomic read on replacement: X + Y.
1887 * Currently in kernel: Z.
1888 * Want in kernel: X + Y + Z.
1889 * => Add in X + Y
1890 * => Add in replacement read.
1891 */
1892 newcounters->counters[index] = repl->counters[mappos];
1893 DEBUGP_C("NORMAL_MAP => mappos %u \n", mappos);
1894}
1895
1896static void counters_map_zeroed(STRUCT_COUNTERS_INFO *newcounters,
1897 STRUCT_REPLACE *repl,
1898 unsigned int index,
1899 unsigned int mappos,
1900 STRUCT_COUNTERS *counters)
1901{
1902 /* Original read: X.
1903 * Atomic read on replacement: X + Y.
1904 * Currently in kernel: Z.
1905 * Want in kernel: Y + Z.
1906 * => Add in Y.
1907 * => Add in (replacement read - original read).
1908 */
1909 subtract_counters(&newcounters->counters[index],
1910 &repl->counters[mappos],
1911 counters);
1912 DEBUGP_C("ZEROED => mappos %u\n", mappos);
1913}
1914
1915static void counters_map_set(STRUCT_COUNTERS_INFO *newcounters,
1916 unsigned int index,
1917 STRUCT_COUNTERS *counters)
1918{
1919 /* Want to set counter (iptables-restore) */
1920
1921 memcpy(&newcounters->counters[index], counters,
1922 sizeof(STRUCT_COUNTERS));
1923
1924 DEBUGP_C("SET\n");
1925}
1926
1927
Marc Bouchere6869a82000-03-20 06:03:29 +00001928int
Rusty Russell79dee072000-05-02 16:45:16 +00001929TC_COMMIT(TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001930{
1931 /* Replace, then map back the counters. */
Rusty Russell79dee072000-05-02 16:45:16 +00001932 STRUCT_REPLACE *repl;
1933 STRUCT_COUNTERS_INFO *newcounters;
Harald Welteaae69be2004-08-29 23:32:14 +00001934 struct chain_head *c;
1935 int ret;
Martin Josefsson841e4ae2003-05-02 15:30:11 +00001936 size_t counterlen;
Harald Welteaae69be2004-08-29 23:32:14 +00001937 int new_number;
1938 unsigned int new_size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001939
1940 CHECK(*handle);
Martin Josefsson841e4ae2003-05-02 15:30:11 +00001941
Marc Bouchere6869a82000-03-20 06:03:29 +00001942#if 0
Rusty Russell54c307e2000-09-04 06:47:28 +00001943 TC_DUMP_ENTRIES(*handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001944#endif
1945
1946 /* Don't commit if nothing changed. */
1947 if (!(*handle)->changed)
1948 goto finished;
1949
Harald Welteaae69be2004-08-29 23:32:14 +00001950 new_number = iptcc_compile_table_prep(*handle, &new_size);
1951 if (new_number < 0) {
1952 errno = ENOMEM;
1953 return 0;
1954 }
1955
1956 repl = malloc(sizeof(*repl) + new_size);
Marc Bouchere6869a82000-03-20 06:03:29 +00001957 if (!repl) {
1958 errno = ENOMEM;
1959 return 0;
1960 }
Harald Welteaae69be2004-08-29 23:32:14 +00001961 memset(repl, 0, sizeof(*repl));
1962
1963 counterlen = sizeof(STRUCT_COUNTERS_INFO)
1964 + sizeof(STRUCT_COUNTERS) * new_number;
Marc Bouchere6869a82000-03-20 06:03:29 +00001965
1966 /* These are the old counters we will get from kernel */
Rusty Russell79dee072000-05-02 16:45:16 +00001967 repl->counters = malloc(sizeof(STRUCT_COUNTERS)
Marc Bouchere6869a82000-03-20 06:03:29 +00001968 * (*handle)->info.num_entries);
1969 if (!repl->counters) {
1970 free(repl);
1971 errno = ENOMEM;
1972 return 0;
1973 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001974 /* These are the counters we're going to put back, later. */
1975 newcounters = malloc(counterlen);
1976 if (!newcounters) {
1977 free(repl->counters);
1978 free(repl);
1979 errno = ENOMEM;
1980 return 0;
1981 }
Harald Welteaae69be2004-08-29 23:32:14 +00001982 memset(newcounters, 0, counterlen);
Marc Bouchere6869a82000-03-20 06:03:29 +00001983
1984 strcpy(repl->name, (*handle)->info.name);
Harald Welteaae69be2004-08-29 23:32:14 +00001985 repl->num_entries = new_number;
1986 repl->size = new_size;
1987
Marc Bouchere6869a82000-03-20 06:03:29 +00001988 repl->num_counters = (*handle)->info.num_entries;
1989 repl->valid_hooks = (*handle)->info.valid_hooks;
Harald Welteaae69be2004-08-29 23:32:14 +00001990
1991 DEBUGP("num_entries=%u, size=%u, num_counters=%u\n",
1992 repl->num_entries, repl->size, repl->num_counters);
1993
1994 ret = iptcc_compile_table(*handle, repl);
1995 if (ret < 0) {
1996 errno = ret;
1997 free(repl->counters);
1998 free(repl);
1999 return 0;
2000 }
2001
2002
2003#ifdef IPTC_DEBUG2
2004 {
2005 int fd = open("/tmp/libiptc-so_set_replace.blob",
2006 O_CREAT|O_WRONLY);
2007 if (fd >= 0) {
2008 write(fd, repl, sizeof(*repl) + repl->size);
2009 close(fd);
2010 }
2011 }
2012#endif
Marc Bouchere6869a82000-03-20 06:03:29 +00002013
Rusty Russell79dee072000-05-02 16:45:16 +00002014 if (setsockopt(sockfd, TC_IPPROTO, SO_SET_REPLACE, repl,
Harald Welteaae69be2004-08-29 23:32:14 +00002015 sizeof(*repl) + repl->size) < 0) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002016 free(repl->counters);
2017 free(repl);
2018 free(newcounters);
2019 return 0;
2020 }
2021
2022 /* Put counters back. */
2023 strcpy(newcounters->name, (*handle)->info.name);
Harald Welteaae69be2004-08-29 23:32:14 +00002024 newcounters->num_counters = new_number;
Marc Bouchere6869a82000-03-20 06:03:29 +00002025
Harald Welteaae69be2004-08-29 23:32:14 +00002026 list_for_each_entry(c, &(*handle)->chains, list) {
2027 struct rule_head *r;
Marc Bouchere6869a82000-03-20 06:03:29 +00002028
Harald Welteaae69be2004-08-29 23:32:14 +00002029 /* Builtin chains have their own counters */
2030 if (iptcc_is_builtin(c)) {
2031 DEBUGP("counter for chain-index %u: ", c->foot_index);
2032 switch(c->counter_map.maptype) {
2033 case COUNTER_MAP_NOMAP:
2034 counters_nomap(newcounters, c->foot_index);
2035 break;
2036 case COUNTER_MAP_NORMAL_MAP:
2037 counters_normal_map(newcounters, repl,
2038 c->foot_index,
2039 c->counter_map.mappos);
2040 break;
2041 case COUNTER_MAP_ZEROED:
2042 counters_map_zeroed(newcounters, repl,
2043 c->foot_index,
2044 c->counter_map.mappos,
2045 &c->counters);
2046 break;
2047 case COUNTER_MAP_SET:
2048 counters_map_set(newcounters, c->foot_index,
2049 &c->counters);
2050 break;
2051 }
2052 }
Harald Welte1cef74d2001-01-05 15:22:59 +00002053
Harald Welteaae69be2004-08-29 23:32:14 +00002054 list_for_each_entry(r, &c->rules, list) {
2055 DEBUGP("counter for index %u: ", r->index);
2056 switch (r->counter_map.maptype) {
2057 case COUNTER_MAP_NOMAP:
2058 counters_nomap(newcounters, r->index);
2059 break;
Harald Welte1cef74d2001-01-05 15:22:59 +00002060
Harald Welteaae69be2004-08-29 23:32:14 +00002061 case COUNTER_MAP_NORMAL_MAP:
2062 counters_normal_map(newcounters, repl,
2063 r->index,
2064 r->counter_map.mappos);
2065 break;
Harald Welte1cef74d2001-01-05 15:22:59 +00002066
Harald Welteaae69be2004-08-29 23:32:14 +00002067 case COUNTER_MAP_ZEROED:
2068 counters_map_zeroed(newcounters, repl,
2069 r->index,
2070 r->counter_map.mappos,
2071 &r->entry->counters);
2072 break;
2073
2074 case COUNTER_MAP_SET:
2075 counters_map_set(newcounters, r->index,
2076 &r->entry->counters);
2077 break;
2078 }
Marc Bouchere6869a82000-03-20 06:03:29 +00002079 }
2080 }
Rusty Russell62527ce2000-09-04 09:45:54 +00002081
Harald Welteaae69be2004-08-29 23:32:14 +00002082
Rusty Russell62527ce2000-09-04 09:45:54 +00002083#ifdef KERNEL_64_USERSPACE_32
2084 {
2085 /* Kernel will think that pointer should be 64-bits, and get
2086 padding. So we accomodate here (assumption: alignment of
2087 `counters' is on 64-bit boundary). */
2088 u_int64_t *kernptr = (u_int64_t *)&newcounters->counters;
2089 if ((unsigned long)&newcounters->counters % 8 != 0) {
2090 fprintf(stderr,
2091 "counters alignment incorrect! Mail rusty!\n");
2092 abort();
2093 }
2094 *kernptr = newcounters->counters;
Rusty Russell54c307e2000-09-04 06:47:28 +00002095 }
Rusty Russell62527ce2000-09-04 09:45:54 +00002096#endif /* KERNEL_64_USERSPACE_32 */
Marc Bouchere6869a82000-03-20 06:03:29 +00002097
Harald Welteaae69be2004-08-29 23:32:14 +00002098#ifdef IPTC_DEBUG2
2099 {
2100 int fd = open("/tmp/libiptc-so_set_add_counters.blob",
2101 O_CREAT|O_WRONLY);
2102 if (fd >= 0) {
2103 write(fd, newcounters, counterlen);
2104 close(fd);
2105 }
2106 }
2107#endif
2108
Rusty Russell79dee072000-05-02 16:45:16 +00002109 if (setsockopt(sockfd, TC_IPPROTO, SO_SET_ADD_COUNTERS,
2110 newcounters, counterlen) < 0) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002111 free(repl->counters);
2112 free(repl);
2113 free(newcounters);
2114 return 0;
2115 }
2116
2117 free(repl->counters);
2118 free(repl);
2119 free(newcounters);
2120
2121 finished:
Martin Josefsson841e4ae2003-05-02 15:30:11 +00002122 TC_FREE(handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00002123 return 1;
2124}
2125
2126/* Get raw socket. */
2127int
Rusty Russell79dee072000-05-02 16:45:16 +00002128TC_GET_RAW_SOCKET()
Marc Bouchere6869a82000-03-20 06:03:29 +00002129{
2130 return sockfd;
2131}
2132
2133/* Translates errno numbers into more human-readable form than strerror. */
2134const char *
Rusty Russell79dee072000-05-02 16:45:16 +00002135TC_STRERROR(int err)
Marc Bouchere6869a82000-03-20 06:03:29 +00002136{
2137 unsigned int i;
2138 struct table_struct {
2139 void *fn;
2140 int err;
2141 const char *message;
2142 } table [] =
Harald Welte4ccfa632001-07-30 15:12:43 +00002143 { { TC_INIT, EPERM, "Permission denied (you must be root)" },
Rusty Russell79dee072000-05-02 16:45:16 +00002144 { TC_INIT, EINVAL, "Module is wrong version" },
Harald Welte4ccfa632001-07-30 15:12:43 +00002145 { TC_INIT, ENOENT,
2146 "Table does not exist (do you need to insmod?)" },
Rusty Russell79dee072000-05-02 16:45:16 +00002147 { TC_DELETE_CHAIN, ENOTEMPTY, "Chain is not empty" },
2148 { TC_DELETE_CHAIN, EINVAL, "Can't delete built-in chain" },
2149 { TC_DELETE_CHAIN, EMLINK,
Marc Bouchere6869a82000-03-20 06:03:29 +00002150 "Can't delete chain with references left" },
Rusty Russell79dee072000-05-02 16:45:16 +00002151 { TC_CREATE_CHAIN, EEXIST, "Chain already exists" },
2152 { TC_INSERT_ENTRY, E2BIG, "Index of insertion too big" },
2153 { TC_REPLACE_ENTRY, E2BIG, "Index of replacement too big" },
2154 { TC_DELETE_NUM_ENTRY, E2BIG, "Index of deletion too big" },
Harald Welte1cef74d2001-01-05 15:22:59 +00002155 { TC_READ_COUNTER, E2BIG, "Index of counter too big" },
2156 { TC_ZERO_COUNTER, E2BIG, "Index of counter too big" },
Rusty Russell79dee072000-05-02 16:45:16 +00002157 { TC_INSERT_ENTRY, ELOOP, "Loop found in table" },
2158 { TC_INSERT_ENTRY, EINVAL, "Target problem" },
Marc Bouchere6869a82000-03-20 06:03:29 +00002159 /* EINVAL for CHECK probably means bad interface. */
Rusty Russell79dee072000-05-02 16:45:16 +00002160 { TC_CHECK_PACKET, EINVAL,
Marc Boucherc8264992000-04-22 22:34:44 +00002161 "Bad arguments (does that interface exist?)" },
Harald Welte4ccfa632001-07-30 15:12:43 +00002162 { TC_CHECK_PACKET, ENOSYS,
2163 "Checking will most likely never get implemented" },
Marc Bouchere6869a82000-03-20 06:03:29 +00002164 /* ENOENT for DELETE probably means no matching rule */
Rusty Russell79dee072000-05-02 16:45:16 +00002165 { TC_DELETE_ENTRY, ENOENT,
Marc Boucherc8264992000-04-22 22:34:44 +00002166 "Bad rule (does a matching rule exist in that chain?)" },
Rusty Russell79dee072000-05-02 16:45:16 +00002167 { TC_SET_POLICY, ENOENT,
Marc Boucherc8264992000-04-22 22:34:44 +00002168 "Bad built-in chain name" },
Rusty Russell79dee072000-05-02 16:45:16 +00002169 { TC_SET_POLICY, EINVAL,
Marc Boucherc8264992000-04-22 22:34:44 +00002170 "Bad policy name" },
Harald Welte4ccfa632001-07-30 15:12:43 +00002171
2172 { NULL, 0, "Incompatible with this kernel" },
2173 { NULL, ENOPROTOOPT, "iptables who? (do you need to insmod?)" },
2174 { NULL, ENOSYS, "Will be implemented real soon. I promise ;)" },
2175 { NULL, ENOMEM, "Memory allocation problem" },
2176 { NULL, ENOENT, "No chain/target/match by that name" },
Marc Bouchere6869a82000-03-20 06:03:29 +00002177 };
2178
2179 for (i = 0; i < sizeof(table)/sizeof(struct table_struct); i++) {
2180 if ((!table[i].fn || table[i].fn == iptc_fn)
2181 && table[i].err == err)
2182 return table[i].message;
2183 }
2184
2185 return strerror(err);
2186}