blob: ce53918b4d309cbecebca461d8e6e995e9824ad2 [file] [log] [blame]
Martin Josefsson52c38022004-09-22 19:39:40 +00001/* Library which manipulates firewall rules. Version $Revision: 1.50 $ */
Marc Bouchere6869a82000-03-20 06:03:29 +00002
3/* Architecture of firewall rules is as follows:
4 *
5 * Chains go INPUT, FORWARD, OUTPUT then user chains.
6 * Each user chain starts with an ERROR node.
7 * Every chain ends with an unconditional jump: a RETURN for user chains,
8 * and a POLICY for built-ins.
9 */
10
Harald Welte3ea8f402003-06-23 18:25:59 +000011/* (C) 1999 Paul ``Rusty'' Russell - Placed under the GNU GPL (See
12 * COPYING for details).
Harald Welteaae69be2004-08-29 23:32:14 +000013 * (C) 2000-2004 by the Netfilter Core Team <coreteam@netfilter.org>
Harald Welte3ea8f402003-06-23 18:25:59 +000014 *
Harald Weltefbc85232003-06-24 17:37:21 +000015 * 2003-Jun-20: Harald Welte <laforge@netfilter.org>:
Harald Welte3ea8f402003-06-23 18:25:59 +000016 * - Reimplementation of chain cache to use offsets instead of entries
Harald Weltefbc85232003-06-24 17:37:21 +000017 * 2003-Jun-23: Harald Welte <laforge@netfilter.org>:
Harald Welte0113fe72004-01-06 19:04:02 +000018 * - performance optimization, sponsored by Astaro AG (http://www.astaro.com/)
Harald Weltefbc85232003-06-24 17:37:21 +000019 * don't rebuild the chain cache after every operation, instead fix it
20 * up after a ruleset change.
Harald Welteaae69be2004-08-29 23:32:14 +000021 * 2004-Aug-18: Harald Welte <laforge@netfilter.org>:
22 * - futher performance work: total reimplementation of libiptc.
23 * - libiptc now has a real internal (linked-list) represntation of the
24 * ruleset and a parser/compiler from/to this internal representation
25 * - again sponsored by Astaro AG (http://www.astaro.com/)
Harald Welte3ea8f402003-06-23 18:25:59 +000026 */
Harald Welte15920d12004-05-16 09:05:07 +000027#include <sys/types.h>
28#include <sys/socket.h>
Stephane Ouellette7cd00282004-05-14 08:21:06 +000029
Harald Welteaae69be2004-08-29 23:32:14 +000030#include "linux_list.h"
31
32//#define IPTC_DEBUG2 1
33
34#ifdef IPTC_DEBUG2
35#include <fcntl.h>
36#define DEBUGP(x, args...) fprintf(stderr, "%s: " x, __FUNCTION__, ## args)
37#define DEBUGP_C(x, args...) fprintf(stderr, x, ## args)
38#else
39#define DEBUGP(x, args...)
40#define DEBUGP_C(x, args...)
41#endif
42
Marc Bouchere6869a82000-03-20 06:03:29 +000043#ifndef IPT_LIB_DIR
44#define IPT_LIB_DIR "/usr/local/lib/iptables"
45#endif
46
Harald Welte0113fe72004-01-06 19:04:02 +000047#ifndef __OPTIMIZE__
48STRUCT_ENTRY_TARGET *
49GET_TARGET(STRUCT_ENTRY *e)
50{
51 return (void *)e + e->target_offset;
52}
53#endif
54
Marc Bouchere6869a82000-03-20 06:03:29 +000055static int sockfd = -1;
56static void *iptc_fn = NULL;
57
58static const char *hooknames[]
Rusty Russell79dee072000-05-02 16:45:16 +000059= { [HOOK_PRE_ROUTING] "PREROUTING",
60 [HOOK_LOCAL_IN] "INPUT",
61 [HOOK_FORWARD] "FORWARD",
62 [HOOK_LOCAL_OUT] "OUTPUT",
Rusty Russell10758b72000-09-14 07:37:33 +000063 [HOOK_POST_ROUTING] "POSTROUTING",
64#ifdef HOOK_DROPPING
65 [HOOK_DROPPING] "DROPPING"
66#endif
Marc Bouchere6869a82000-03-20 06:03:29 +000067};
68
Harald Welteaae69be2004-08-29 23:32:14 +000069/* Convenience structures */
70struct ipt_error_target
71{
72 STRUCT_ENTRY_TARGET t;
73 char error[TABLE_MAXNAMELEN];
74};
75
76struct chain_head;
77struct rule_head;
78
Marc Bouchere6869a82000-03-20 06:03:29 +000079struct counter_map
80{
81 enum {
82 COUNTER_MAP_NOMAP,
83 COUNTER_MAP_NORMAL_MAP,
Harald Welte1cef74d2001-01-05 15:22:59 +000084 COUNTER_MAP_ZEROED,
85 COUNTER_MAP_SET
Marc Bouchere6869a82000-03-20 06:03:29 +000086 } maptype;
87 unsigned int mappos;
88};
89
Harald Welteaae69be2004-08-29 23:32:14 +000090enum iptcc_rule_type {
91 IPTCC_R_STANDARD, /* standard target (ACCEPT, ...) */
92 IPTCC_R_MODULE, /* extension module (SNAT, ...) */
93 IPTCC_R_FALLTHROUGH, /* fallthrough rule */
94 IPTCC_R_JUMP, /* jump to other chain */
Marc Bouchere6869a82000-03-20 06:03:29 +000095};
96
Harald Welteaae69be2004-08-29 23:32:14 +000097struct rule_head
Rusty Russell30fd6e52000-04-23 09:16:06 +000098{
Harald Welteaae69be2004-08-29 23:32:14 +000099 struct list_head list;
100 struct chain_head *chain;
101 struct counter_map counter_map;
102
103 unsigned int index; /* index (needed for counter_map) */
104 unsigned int offset; /* offset in rule blob */
105
106 enum iptcc_rule_type type;
107 struct chain_head *jump; /* jump target, if IPTCC_R_JUMP */
108
109 unsigned int size; /* size of entry data */
110 STRUCT_ENTRY entry[0];
111};
112
113struct chain_head
114{
115 struct list_head list;
Rusty Russell79dee072000-05-02 16:45:16 +0000116 char name[TABLE_MAXNAMELEN];
Harald Welteaae69be2004-08-29 23:32:14 +0000117 unsigned int hooknum; /* hook number+1 if builtin */
118 unsigned int references; /* how many jumps reference us */
119 int verdict; /* verdict if builtin */
120
121 STRUCT_COUNTERS counters; /* per-chain counters */
122 struct counter_map counter_map;
123
124 unsigned int num_rules; /* number of rules in list */
125 struct list_head rules; /* list of rules */
126
127 unsigned int index; /* index (needed for jump resolval) */
128 unsigned int head_offset; /* offset in rule blob */
129 unsigned int foot_index; /* index (needed for counter_map) */
130 unsigned int foot_offset; /* offset in rule blob */
Rusty Russell30fd6e52000-04-23 09:16:06 +0000131};
132
Rusty Russell79dee072000-05-02 16:45:16 +0000133STRUCT_TC_HANDLE
Marc Bouchere6869a82000-03-20 06:03:29 +0000134{
Harald Welteaae69be2004-08-29 23:32:14 +0000135 int changed; /* Have changes been made? */
136
137 struct list_head chains;
138
139 struct chain_head *chain_iterator_cur;
140 struct rule_head *rule_iterator_cur;
141
Rusty Russell79dee072000-05-02 16:45:16 +0000142 STRUCT_GETINFO info;
Harald Welteaae69be2004-08-29 23:32:14 +0000143 STRUCT_GET_ENTRIES *entries;
Marc Bouchere6869a82000-03-20 06:03:29 +0000144};
145
Harald Welteaae69be2004-08-29 23:32:14 +0000146/* allocate a new chain head for the cache */
147static struct chain_head *iptcc_alloc_chain_head(const char *name, int hooknum)
148{
149 struct chain_head *c = malloc(sizeof(*c));
150 if (!c)
151 return NULL;
152 memset(c, 0, sizeof(*c));
153
154 strncpy(c->name, name, TABLE_MAXNAMELEN);
155 c->hooknum = hooknum;
156 INIT_LIST_HEAD(&c->rules);
157
158 return c;
159}
160
161/* allocate and initialize a new rule for the cache */
162static struct rule_head *iptcc_alloc_rule(struct chain_head *c, unsigned int size)
163{
164 struct rule_head *r = malloc(sizeof(*r)+size);
165 if (!r)
166 return NULL;
167 memset(r, 0, sizeof(*r));
168
169 r->chain = c;
170 r->size = size;
171
172 return r;
173}
174
175/* notify us that the ruleset has been modified by the user */
Rusty Russell175f6412000-03-24 09:32:20 +0000176static void
Rusty Russell79dee072000-05-02 16:45:16 +0000177set_changed(TC_HANDLE_T h)
Rusty Russell175f6412000-03-24 09:32:20 +0000178{
Rusty Russell175f6412000-03-24 09:32:20 +0000179 h->changed = 1;
180}
181
Harald Welte380ba5f2002-02-13 16:19:55 +0000182#ifdef IPTC_DEBUG
Rusty Russell79dee072000-05-02 16:45:16 +0000183static void do_check(TC_HANDLE_T h, unsigned int line);
Rusty Russell849779c2000-04-23 15:51:51 +0000184#define CHECK(h) do { if (!getenv("IPTC_NO_CHECK")) do_check((h), __LINE__); } while(0)
Rusty Russell30fd6e52000-04-23 09:16:06 +0000185#else
186#define CHECK(h)
187#endif
Marc Bouchere6869a82000-03-20 06:03:29 +0000188
Harald Welteaae69be2004-08-29 23:32:14 +0000189
190/**********************************************************************
191 * iptc blob utility functions (iptcb_*)
192 **********************************************************************/
193
Marc Bouchere6869a82000-03-20 06:03:29 +0000194static inline int
Harald Welteaae69be2004-08-29 23:32:14 +0000195iptcb_get_number(const STRUCT_ENTRY *i,
Rusty Russell79dee072000-05-02 16:45:16 +0000196 const STRUCT_ENTRY *seek,
Marc Bouchere6869a82000-03-20 06:03:29 +0000197 unsigned int *pos)
198{
199 if (i == seek)
200 return 1;
201 (*pos)++;
202 return 0;
203}
204
Marc Bouchere6869a82000-03-20 06:03:29 +0000205static inline int
Harald Welteaae69be2004-08-29 23:32:14 +0000206iptcb_get_entry_n(STRUCT_ENTRY *i,
Marc Bouchere6869a82000-03-20 06:03:29 +0000207 unsigned int number,
208 unsigned int *pos,
Rusty Russell79dee072000-05-02 16:45:16 +0000209 STRUCT_ENTRY **pe)
Marc Bouchere6869a82000-03-20 06:03:29 +0000210{
211 if (*pos == number) {
212 *pe = i;
213 return 1;
214 }
215 (*pos)++;
216 return 0;
217}
218
Harald Welteaae69be2004-08-29 23:32:14 +0000219static inline STRUCT_ENTRY *
220iptcb_get_entry(TC_HANDLE_T h, unsigned int offset)
221{
222 return (STRUCT_ENTRY *)((char *)h->entries->entrytable + offset);
223}
224
225static unsigned int
226iptcb_entry2index(const TC_HANDLE_T h, const STRUCT_ENTRY *seek)
Marc Bouchere6869a82000-03-20 06:03:29 +0000227{
228 unsigned int pos = 0;
Marc Bouchere6869a82000-03-20 06:03:29 +0000229
Harald Welteaae69be2004-08-29 23:32:14 +0000230 if (ENTRY_ITERATE(h->entries->entrytable, h->entries->size,
231 iptcb_get_number, seek, &pos) == 0) {
232 fprintf(stderr, "ERROR: offset %u not an entry!\n",
233 (unsigned int)((char *)seek - (char *)h->entries->entrytable));
234 abort();
235 }
236 return pos;
Marc Bouchere6869a82000-03-20 06:03:29 +0000237}
238
Harald Welte0113fe72004-01-06 19:04:02 +0000239static inline STRUCT_ENTRY *
Harald Welteaae69be2004-08-29 23:32:14 +0000240iptcb_offset2entry(TC_HANDLE_T h, unsigned int offset)
Harald Welte0113fe72004-01-06 19:04:02 +0000241{
Harald Welteaae69be2004-08-29 23:32:14 +0000242 return (STRUCT_ENTRY *) ((void *)h->entries->entrytable+offset);
Harald Welte0113fe72004-01-06 19:04:02 +0000243}
244
Harald Welteaae69be2004-08-29 23:32:14 +0000245
Harald Welte0113fe72004-01-06 19:04:02 +0000246static inline unsigned long
Harald Welteaae69be2004-08-29 23:32:14 +0000247iptcb_entry2offset(const TC_HANDLE_T h, const STRUCT_ENTRY *e)
Harald Welte0113fe72004-01-06 19:04:02 +0000248{
Harald Welteaae69be2004-08-29 23:32:14 +0000249 return (void *)e - (void *)h->entries->entrytable;
Harald Welte3ea8f402003-06-23 18:25:59 +0000250}
251
252static inline unsigned int
Harald Welteaae69be2004-08-29 23:32:14 +0000253iptcb_offset2index(const TC_HANDLE_T h, unsigned int offset)
Harald Welte3ea8f402003-06-23 18:25:59 +0000254{
Harald Welteaae69be2004-08-29 23:32:14 +0000255 return iptcb_entry2index(h, iptcb_offset2entry(h, offset));
256}
257
258/* Returns 0 if not hook entry, else hooknumber + 1 */
259static inline unsigned int
260iptcb_ent_is_hook_entry(STRUCT_ENTRY *e, TC_HANDLE_T h)
261{
262 unsigned int i;
263
264 for (i = 0; i < NUMHOOKS; i++) {
265 if ((h->info.valid_hooks & (1 << i))
266 && iptcb_get_entry(h, h->info.hook_entry[i]) == e)
267 return i+1;
268 }
269 return 0;
Harald Welte3ea8f402003-06-23 18:25:59 +0000270}
271
272
Harald Welteaae69be2004-08-29 23:32:14 +0000273/**********************************************************************
274 * iptc cache utility functions (iptcc_*)
275 **********************************************************************/
Harald Welte0113fe72004-01-06 19:04:02 +0000276
Harald Welteaae69be2004-08-29 23:32:14 +0000277/* Is the given chain builtin (1) or user-defined (0) */
278static unsigned int iptcc_is_builtin(struct chain_head *c)
279{
280 return (c->hooknum ? 1 : 0);
281}
282
283/* Get a specific rule within a chain */
284static struct rule_head *iptcc_get_rule_num(struct chain_head *c,
285 unsigned int rulenum)
286{
287 struct rule_head *r;
288 unsigned int num = 0;
289
290 list_for_each_entry(r, &c->rules, list) {
291 num++;
292 if (num == rulenum)
293 return r;
294 }
295 return NULL;
296}
297
298/* Returns chain head if found, otherwise NULL. */
299static struct chain_head *
300iptcc_find_chain_by_offset(TC_HANDLE_T handle, unsigned int offset)
301{
302 struct list_head *pos;
303
304 if (list_empty(&handle->chains))
305 return NULL;
306
307 list_for_each(pos, &handle->chains) {
308 struct chain_head *c = list_entry(pos, struct chain_head, list);
309 if (offset >= c->head_offset && offset <= c->foot_offset)
310 return c;
Harald Welte0113fe72004-01-06 19:04:02 +0000311 }
312
Harald Welteaae69be2004-08-29 23:32:14 +0000313 return NULL;
Harald Welte0113fe72004-01-06 19:04:02 +0000314}
Harald Welteaae69be2004-08-29 23:32:14 +0000315/* Returns chain head if found, otherwise NULL. */
316static struct chain_head *
317iptcc_find_label(const char *name, TC_HANDLE_T handle)
318{
319 struct list_head *pos;
320
321 if (list_empty(&handle->chains))
322 return NULL;
323
324 list_for_each(pos, &handle->chains) {
325 struct chain_head *c = list_entry(pos, struct chain_head, list);
326 if (!strcmp(c->name, name))
327 return c;
328 }
329
330 return NULL;
331}
332
333/* called when rule is to be removed from cache */
334static void iptcc_delete_rule(struct rule_head *r)
335{
336 DEBUGP("deleting rule %p (offset %u)\n", r, r->offset);
337 /* clean up reference count of called chain */
338 if (r->type == IPTCC_R_JUMP
339 && r->jump)
340 r->jump->references--;
341
342 list_del(&r->list);
343 free(r);
344}
345
346
347/**********************************************************************
348 * RULESET PARSER (blob -> cache)
349 **********************************************************************/
350
351static int alphasort(const void *a, const void *b)
352{
353 return strcmp(((struct chain_head *)a)->name,
354 ((struct chain_head *)b)->name);
355}
356
357/* Delete policy rule of previous chain, since cache doesn't contain
358 * chain policy rules.
359 * WARNING: This function has ugly design and relies on a lot of context, only
360 * to be called from specific places within the parser */
361static int __iptcc_p_del_policy(TC_HANDLE_T h, unsigned int num)
362{
363 if (h->chain_iterator_cur) {
364 /* policy rule is last rule */
365 struct rule_head *pr = (struct rule_head *)
366 h->chain_iterator_cur->rules.prev;
367
368 /* save verdict */
369 h->chain_iterator_cur->verdict =
370 *(int *)GET_TARGET(pr->entry)->data;
371
372 /* save counter and counter_map information */
373 h->chain_iterator_cur->counter_map.maptype =
374 COUNTER_MAP_NORMAL_MAP;
375 h->chain_iterator_cur->counter_map.mappos = num-1;
376 memcpy(&h->chain_iterator_cur->counters, &pr->entry->counters,
377 sizeof(h->chain_iterator_cur->counters));
378
379 /* foot_offset points to verdict rule */
380 h->chain_iterator_cur->foot_index = num;
381 h->chain_iterator_cur->foot_offset = pr->offset;
382
383 /* delete rule from cache */
384 iptcc_delete_rule(pr);
385
386 return 1;
387 }
388 return 0;
389}
390
391/* Another ugly helper function split out of cache_add_entry to make it less
392 * spaghetti code */
393static void __iptcc_p_add_chain(TC_HANDLE_T h, struct chain_head *c,
394 unsigned int offset, unsigned int *num)
395{
396 __iptcc_p_del_policy(h, *num);
397
398 c->head_offset = offset;
399 c->index = *num;
400
401 list_add_tail(&c->list, &h->chains);
402 h->chain_iterator_cur = c;
403}
404
405/* main parser function: add an entry from the blob to the cache */
406static int cache_add_entry(STRUCT_ENTRY *e,
407 TC_HANDLE_T h,
408 STRUCT_ENTRY **prev,
409 unsigned int *num)
410{
411 unsigned int builtin;
412 unsigned int offset = (char *)e - (char *)h->entries->entrytable;
413
414 DEBUGP("entering...");
415
416 /* Last entry ("policy rule"). End it.*/
417 if (iptcb_entry2offset(h,e) + e->next_offset == h->entries->size) {
418 /* This is the ERROR node at the end of the chain */
419 DEBUGP_C("%u:%u: end of table:\n", *num, offset);
420
421 __iptcc_p_del_policy(h, *num);
422
423 h->chain_iterator_cur = NULL;
424 goto out_inc;
425 }
426
427 /* We know this is the start of a new chain if it's an ERROR
428 * target, or a hook entry point */
429
430 if (strcmp(GET_TARGET(e)->u.user.name, ERROR_TARGET) == 0) {
431 struct chain_head *c =
432 iptcc_alloc_chain_head((const char *)GET_TARGET(e)->data, 0);
433 DEBUGP_C("%u:%u:new userdefined chain %s: %p\n", *num, offset,
434 (char *)c->name, c);
435 if (!c) {
436 errno = -ENOMEM;
437 return -1;
438 }
439
440 __iptcc_p_add_chain(h, c, offset, num);
441
442 } else if ((builtin = iptcb_ent_is_hook_entry(e, h)) != 0) {
443 struct chain_head *c =
444 iptcc_alloc_chain_head((char *)hooknames[builtin-1],
445 builtin);
446 DEBUGP_C("%u:%u new builtin chain: %p (rules=%p)\n",
447 *num, offset, c, &c->rules);
448 if (!c) {
449 errno = -ENOMEM;
450 return -1;
451 }
452
453 c->hooknum = builtin;
454
455 __iptcc_p_add_chain(h, c, offset, num);
456
457 /* FIXME: this is ugly. */
458 goto new_rule;
459 } else {
460 /* has to be normal rule */
461 struct rule_head *r;
462new_rule:
463
464 if (!(r = iptcc_alloc_rule(h->chain_iterator_cur,
465 e->next_offset))) {
466 errno = ENOMEM;
467 return -1;
468 }
469 DEBUGP_C("%u:%u normal rule: %p: ", *num, offset, r);
470
471 r->index = *num;
472 r->offset = offset;
473 memcpy(r->entry, e, e->next_offset);
474 r->counter_map.maptype = COUNTER_MAP_NORMAL_MAP;
475 r->counter_map.mappos = r->index;
476
477 /* handling of jumps, etc. */
478 if (!strcmp(GET_TARGET(e)->u.user.name, STANDARD_TARGET)) {
479 STRUCT_STANDARD_TARGET *t;
480
481 t = (STRUCT_STANDARD_TARGET *)GET_TARGET(e);
482 if (t->target.u.target_size
483 != ALIGN(sizeof(STRUCT_STANDARD_TARGET))) {
484 errno = EINVAL;
485 return -1;
486 }
487
488 if (t->verdict < 0) {
489 DEBUGP_C("standard, verdict=%d\n", t->verdict);
490 r->type = IPTCC_R_STANDARD;
491 } else if (t->verdict == r->offset+e->next_offset) {
492 DEBUGP_C("fallthrough\n");
493 r->type = IPTCC_R_FALLTHROUGH;
494 } else {
495 DEBUGP_C("jump, target=%u\n", t->verdict);
496 r->type = IPTCC_R_JUMP;
497 /* Jump target fixup has to be deferred
498 * until second pass, since we migh not
499 * yet have parsed the target */
500 }
Martin Josefsson52c38022004-09-22 19:39:40 +0000501 } else {
502 DEBUGP_C("module, target=%s\n", GET_TARGET(e)->u.user.name);
503 r->type = IPTCC_R_MODULE;
Harald Welteaae69be2004-08-29 23:32:14 +0000504 }
505
506 list_add_tail(&r->list, &h->chain_iterator_cur->rules);
507 }
508out_inc:
509 (*num)++;
510 return 0;
511}
512
513
514/* parse an iptables blob into it's pieces */
515static int parse_table(TC_HANDLE_T h)
516{
517 STRUCT_ENTRY *prev;
518 unsigned int num = 0;
519 struct chain_head *c;
520
521 /* First pass: over ruleset blob */
522 ENTRY_ITERATE(h->entries->entrytable, h->entries->size,
523 cache_add_entry, h, &prev, &num);
524
525 /* Second pass: fixup parsed data from first pass */
526 list_for_each_entry(c, &h->chains, list) {
527 struct rule_head *r;
528 list_for_each_entry(r, &c->rules, list) {
529 struct chain_head *c;
530 STRUCT_STANDARD_TARGET *t;
531
532 if (r->type != IPTCC_R_JUMP)
533 continue;
534
535 t = (STRUCT_STANDARD_TARGET *)GET_TARGET(r->entry);
536 c = iptcc_find_chain_by_offset(h, t->verdict);
537 if (!c)
538 return -1;
539 r->jump = c;
540 c->references++;
541 }
542 }
543
544 /* FIXME: sort chains */
545
546 return 1;
547}
548
549
550/**********************************************************************
551 * RULESET COMPILATION (cache -> blob)
552 **********************************************************************/
553
554/* Convenience structures */
555struct iptcb_chain_start{
556 STRUCT_ENTRY e;
557 struct ipt_error_target name;
558};
559#define IPTCB_CHAIN_START_SIZE (sizeof(STRUCT_ENTRY) + \
560 ALIGN(sizeof(struct ipt_error_target)))
561
562struct iptcb_chain_foot {
563 STRUCT_ENTRY e;
564 STRUCT_STANDARD_TARGET target;
565};
566#define IPTCB_CHAIN_FOOT_SIZE (sizeof(STRUCT_ENTRY) + \
567 ALIGN(sizeof(STRUCT_STANDARD_TARGET)))
568
569struct iptcb_chain_error {
570 STRUCT_ENTRY entry;
571 struct ipt_error_target target;
572};
573#define IPTCB_CHAIN_ERROR_SIZE (sizeof(STRUCT_ENTRY) + \
574 ALIGN(sizeof(struct ipt_error_target)))
575
576
577
578/* compile rule from cache into blob */
579static inline int iptcc_compile_rule (TC_HANDLE_T h, STRUCT_REPLACE *repl, struct rule_head *r)
580{
581 /* handle jumps */
582 if (r->type == IPTCC_R_JUMP) {
583 STRUCT_STANDARD_TARGET *t;
584 t = (STRUCT_STANDARD_TARGET *)GET_TARGET(r->entry);
585 /* memset for memcmp convenience on delete/replace */
586 memset(t->target.u.user.name, 0, FUNCTION_MAXNAMELEN);
587 strcpy(t->target.u.user.name, STANDARD_TARGET);
588 /* Jumps can only happen to builtin chains, so we
589 * can safely assume that they always have a header */
590 t->verdict = r->jump->head_offset + IPTCB_CHAIN_START_SIZE;
591 } else if (r->type == IPTCC_R_FALLTHROUGH) {
592 STRUCT_STANDARD_TARGET *t;
593 t = (STRUCT_STANDARD_TARGET *)GET_TARGET(r->entry);
594 t->verdict = r->offset + r->size;
595 }
596
597 /* copy entry from cache to blob */
598 memcpy((char *)repl->entries+r->offset, r->entry, r->size);
599
600 return 1;
601}
602
603/* compile chain from cache into blob */
604static int iptcc_compile_chain(TC_HANDLE_T h, STRUCT_REPLACE *repl, struct chain_head *c)
605{
606 int ret;
607 struct rule_head *r;
608 struct iptcb_chain_start *head;
609 struct iptcb_chain_foot *foot;
610
611 /* only user-defined chains have heaer */
612 if (!iptcc_is_builtin(c)) {
613 /* put chain header in place */
614 head = (void *)repl->entries + c->head_offset;
615 head->e.target_offset = sizeof(STRUCT_ENTRY);
616 head->e.next_offset = IPTCB_CHAIN_START_SIZE;
617 strcpy(head->name.t.u.user.name, ERROR_TARGET);
618 head->name.t.u.target_size =
619 ALIGN(sizeof(struct ipt_error_target));
620 strcpy(head->name.error, c->name);
621 } else {
622 repl->hook_entry[c->hooknum-1] = c->head_offset;
623 repl->underflow[c->hooknum-1] = c->foot_offset;
624 }
625
626 /* iterate over rules */
627 list_for_each_entry(r, &c->rules, list) {
628 ret = iptcc_compile_rule(h, repl, r);
629 if (ret < 0)
630 return ret;
631 }
632
633 /* put chain footer in place */
634 foot = (void *)repl->entries + c->foot_offset;
635 foot->e.target_offset = sizeof(STRUCT_ENTRY);
636 foot->e.next_offset = IPTCB_CHAIN_FOOT_SIZE;
637 strcpy(foot->target.target.u.user.name, STANDARD_TARGET);
638 foot->target.target.u.target_size =
639 ALIGN(sizeof(STRUCT_STANDARD_TARGET));
640 /* builtin targets have verdict, others return */
641 if (iptcc_is_builtin(c))
642 foot->target.verdict = c->verdict;
643 else
644 foot->target.verdict = RETURN;
645 /* set policy-counters */
646 memcpy(&foot->e.counters, &c->counters, sizeof(STRUCT_COUNTERS));
647
648 return 0;
649}
650
651/* calculate offset and number for every rule in the cache */
652static int iptcc_compile_chain_offsets(TC_HANDLE_T h, struct chain_head *c,
653 int *offset, int *num)
654{
655 struct rule_head *r;
656
657 c->head_offset = *offset;
658 DEBUGP("%s: chain_head %u, offset=%u\n", c->name, *num, *offset);
659
660 if (!iptcc_is_builtin(c)) {
661 /* Chain has header */
662 *offset += sizeof(STRUCT_ENTRY)
663 + ALIGN(sizeof(struct ipt_error_target));
664 (*num)++;
665 }
666
667 list_for_each_entry(r, &c->rules, list) {
668 DEBUGP("rule %u, offset=%u, index=%u\n", *num, *offset, *num);
669 r->offset = *offset;
670 r->index = *num;
671 *offset += r->size;
672 (*num)++;
673 }
674
675 DEBUGP("%s; chain_foot %u, offset=%u, index=%u\n", c->name, *num,
676 *offset, *num);
677 c->foot_offset = *offset;
678 c->foot_index = *num;
679 *offset += sizeof(STRUCT_ENTRY)
680 + ALIGN(sizeof(STRUCT_STANDARD_TARGET));
681 (*num)++;
682
683 return 1;
684}
685
686/* put the pieces back together again */
687static int iptcc_compile_table_prep(TC_HANDLE_T h, unsigned int *size)
688{
689 struct chain_head *c;
690 unsigned int offset = 0, num = 0;
691 int ret = 0;
692
693 /* First pass: calculate offset for every rule */
694 list_for_each_entry(c, &h->chains, list) {
695 ret = iptcc_compile_chain_offsets(h, c, &offset, &num);
696 if (ret < 0)
697 return ret;
698 }
699
700 /* Append one error rule at end of chain */
701 num++;
702 offset += sizeof(STRUCT_ENTRY)
703 + ALIGN(sizeof(struct ipt_error_target));
704
705 /* ruleset size is now in offset */
706 *size = offset;
707 return num;
708}
709
710static int iptcc_compile_table(TC_HANDLE_T h, STRUCT_REPLACE *repl)
711{
712 struct chain_head *c;
713 struct iptcb_chain_error *error;
714
715 /* Second pass: copy from cache to offsets, fill in jumps */
716 list_for_each_entry(c, &h->chains, list) {
717 int ret = iptcc_compile_chain(h, repl, c);
718 if (ret < 0)
719 return ret;
720 }
721
722 /* Append error rule at end of chain */
723 error = (void *)repl->entries + repl->size - IPTCB_CHAIN_ERROR_SIZE;
724 error->entry.target_offset = sizeof(STRUCT_ENTRY);
725 error->entry.next_offset = IPTCB_CHAIN_ERROR_SIZE;
726 error->target.t.u.user.target_size =
727 ALIGN(sizeof(struct ipt_error_target));
728 strcpy((char *)&error->target.t.u.user.name, ERROR_TARGET);
729 strcpy((char *)&error->target.error, "ERROR");
730
731 return 1;
732}
733
734/**********************************************************************
735 * EXTERNAL API (operates on cache only)
736 **********************************************************************/
Marc Bouchere6869a82000-03-20 06:03:29 +0000737
738/* Allocate handle of given size */
Rusty Russell79dee072000-05-02 16:45:16 +0000739static TC_HANDLE_T
Harald Welte0113fe72004-01-06 19:04:02 +0000740alloc_handle(const char *tablename, unsigned int size, unsigned int num_rules)
Marc Bouchere6869a82000-03-20 06:03:29 +0000741{
742 size_t len;
Rusty Russell79dee072000-05-02 16:45:16 +0000743 TC_HANDLE_T h;
Marc Bouchere6869a82000-03-20 06:03:29 +0000744
Harald Welteaae69be2004-08-29 23:32:14 +0000745 len = sizeof(STRUCT_TC_HANDLE) + size;
Marc Bouchere6869a82000-03-20 06:03:29 +0000746
Harald Welteaae69be2004-08-29 23:32:14 +0000747 h = malloc(sizeof(STRUCT_TC_HANDLE));
748 if (!h) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000749 errno = ENOMEM;
750 return NULL;
751 }
Harald Welteaae69be2004-08-29 23:32:14 +0000752 memset(h, 0, sizeof(*h));
753 INIT_LIST_HEAD(&h->chains);
Marc Bouchere6869a82000-03-20 06:03:29 +0000754 strcpy(h->info.name, tablename);
Harald Welteaae69be2004-08-29 23:32:14 +0000755
Harald Welte0371c0c2004-09-19 21:00:12 +0000756 h->entries = malloc(sizeof(STRUCT_GET_ENTRIES) + size);
Harald Welteaae69be2004-08-29 23:32:14 +0000757 if (!h->entries)
758 goto out_free_handle;
759
760 strcpy(h->entries->name, tablename);
Harald Welte0371c0c2004-09-19 21:00:12 +0000761 h->entries->size = size;
Marc Bouchere6869a82000-03-20 06:03:29 +0000762
763 return h;
Harald Welteaae69be2004-08-29 23:32:14 +0000764
765out_free_handle:
766 free(h);
767
768 return NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +0000769}
770
Harald Welteaae69be2004-08-29 23:32:14 +0000771
Rusty Russell79dee072000-05-02 16:45:16 +0000772TC_HANDLE_T
773TC_INIT(const char *tablename)
Marc Bouchere6869a82000-03-20 06:03:29 +0000774{
Rusty Russell79dee072000-05-02 16:45:16 +0000775 TC_HANDLE_T h;
776 STRUCT_GETINFO info;
Marc Bouchere6869a82000-03-20 06:03:29 +0000777 int tmp;
778 socklen_t s;
779
Rusty Russell79dee072000-05-02 16:45:16 +0000780 iptc_fn = TC_INIT;
Marc Bouchere6869a82000-03-20 06:03:29 +0000781
Martin Josefssone560fd62003-06-13 16:56:51 +0000782 if (sockfd != -1) {
Harald Welte366454b2002-01-07 13:46:50 +0000783 close(sockfd);
Martin Josefssone560fd62003-06-13 16:56:51 +0000784 sockfd = -1;
785 }
Harald Welte366454b2002-01-07 13:46:50 +0000786
Martin Josefsson841e4ae2003-05-02 15:30:11 +0000787 if (strlen(tablename) >= TABLE_MAXNAMELEN) {
788 errno = EINVAL;
789 return NULL;
790 }
791
Rusty Russell79dee072000-05-02 16:45:16 +0000792 sockfd = socket(TC_AF, SOCK_RAW, IPPROTO_RAW);
Marc Bouchere6869a82000-03-20 06:03:29 +0000793 if (sockfd < 0)
794 return NULL;
795
796 s = sizeof(info);
Martin Josefsson841e4ae2003-05-02 15:30:11 +0000797
Marc Bouchere6869a82000-03-20 06:03:29 +0000798 strcpy(info.name, tablename);
Rusty Russell79dee072000-05-02 16:45:16 +0000799 if (getsockopt(sockfd, TC_IPPROTO, SO_GET_INFO, &info, &s) < 0)
Marc Bouchere6869a82000-03-20 06:03:29 +0000800 return NULL;
801
Harald Welteaae69be2004-08-29 23:32:14 +0000802 DEBUGP("valid_hooks=0x%08x, num_entries=%u, size=%u\n",
803 info.valid_hooks, info.num_entries, info.size);
804
Harald Welte0113fe72004-01-06 19:04:02 +0000805 if ((h = alloc_handle(info.name, info.size, info.num_entries))
Martin Josefsson841e4ae2003-05-02 15:30:11 +0000806 == NULL) {
807 close(sockfd);
Martin Josefssone560fd62003-06-13 16:56:51 +0000808 sockfd = -1;
Marc Bouchere6869a82000-03-20 06:03:29 +0000809 return NULL;
Martin Josefsson841e4ae2003-05-02 15:30:11 +0000810 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000811
Marc Bouchere6869a82000-03-20 06:03:29 +0000812 /* Initialize current state */
813 h->info = info;
Harald Welte0113fe72004-01-06 19:04:02 +0000814
Harald Welteaae69be2004-08-29 23:32:14 +0000815 h->entries->size = h->info.size;
Marc Bouchere6869a82000-03-20 06:03:29 +0000816
Rusty Russell79dee072000-05-02 16:45:16 +0000817 tmp = sizeof(STRUCT_GET_ENTRIES) + h->info.size;
Marc Bouchere6869a82000-03-20 06:03:29 +0000818
Harald Welteaae69be2004-08-29 23:32:14 +0000819 if (getsockopt(sockfd, TC_IPPROTO, SO_GET_ENTRIES, h->entries,
820 &tmp) < 0)
821 goto error;
822
823#ifdef IPTC_DEBUG2
824 {
825 int fd = open("/tmp/libiptc-so_get_entries.blob",
826 O_CREAT|O_WRONLY);
827 if (fd >= 0) {
828 write(fd, h->entries, tmp);
829 close(fd);
830 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000831 }
Harald Welteaae69be2004-08-29 23:32:14 +0000832#endif
833
834 if (parse_table(h) < 0)
835 goto error;
Rusty Russell7e53bf92000-03-20 07:03:28 +0000836
Marc Bouchere6869a82000-03-20 06:03:29 +0000837 CHECK(h);
838 return h;
Harald Welteaae69be2004-08-29 23:32:14 +0000839error:
840 TC_FREE(&h);
841 return NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +0000842}
843
Martin Josefsson841e4ae2003-05-02 15:30:11 +0000844void
845TC_FREE(TC_HANDLE_T *h)
846{
Harald Welteaae69be2004-08-29 23:32:14 +0000847 struct chain_head *c, *tmp;
848
Martin Josefsson841e4ae2003-05-02 15:30:11 +0000849 close(sockfd);
Martin Josefssone560fd62003-06-13 16:56:51 +0000850 sockfd = -1;
Harald Welteaae69be2004-08-29 23:32:14 +0000851
852 list_for_each_entry_safe(c, tmp, &(*h)->chains, list) {
853 struct rule_head *r, *rtmp;
854
855 list_for_each_entry_safe(r, rtmp, &c->rules, list) {
856 free(r);
857 }
858
859 free(c);
860 }
861
862 free((*h)->entries);
Martin Josefsson841e4ae2003-05-02 15:30:11 +0000863 free(*h);
Harald Welteaae69be2004-08-29 23:32:14 +0000864
Martin Josefsson841e4ae2003-05-02 15:30:11 +0000865 *h = NULL;
866}
867
Marc Bouchere6869a82000-03-20 06:03:29 +0000868static inline int
Rusty Russell79dee072000-05-02 16:45:16 +0000869print_match(const STRUCT_ENTRY_MATCH *m)
Marc Bouchere6869a82000-03-20 06:03:29 +0000870{
Rusty Russell228e98d2000-04-27 10:28:06 +0000871 printf("Match name: `%s'\n", m->u.user.name);
Marc Bouchere6869a82000-03-20 06:03:29 +0000872 return 0;
873}
874
Rusty Russell79dee072000-05-02 16:45:16 +0000875static int dump_entry(STRUCT_ENTRY *e, const TC_HANDLE_T handle);
876
Marc Bouchere6869a82000-03-20 06:03:29 +0000877void
Rusty Russell79dee072000-05-02 16:45:16 +0000878TC_DUMP_ENTRIES(const TC_HANDLE_T handle)
Marc Bouchere6869a82000-03-20 06:03:29 +0000879{
880 CHECK(handle);
Harald Welteaae69be2004-08-29 23:32:14 +0000881#if 0
Marc Bouchere6869a82000-03-20 06:03:29 +0000882 printf("libiptc v%s. %u entries, %u bytes.\n",
Harald Welte80fe35d2002-05-29 13:08:15 +0000883 IPTABLES_VERSION,
Harald Welteaae69be2004-08-29 23:32:14 +0000884 handle->new_number, handle->entries->size);
Marc Bouchere6869a82000-03-20 06:03:29 +0000885 printf("Table `%s'\n", handle->info.name);
886 printf("Hooks: pre/in/fwd/out/post = %u/%u/%u/%u/%u\n",
Rusty Russell67088e72000-05-10 01:18:57 +0000887 handle->info.hook_entry[HOOK_PRE_ROUTING],
888 handle->info.hook_entry[HOOK_LOCAL_IN],
889 handle->info.hook_entry[HOOK_FORWARD],
890 handle->info.hook_entry[HOOK_LOCAL_OUT],
891 handle->info.hook_entry[HOOK_POST_ROUTING]);
Marc Bouchere6869a82000-03-20 06:03:29 +0000892 printf("Underflows: pre/in/fwd/out/post = %u/%u/%u/%u/%u\n",
Rusty Russell67088e72000-05-10 01:18:57 +0000893 handle->info.underflow[HOOK_PRE_ROUTING],
894 handle->info.underflow[HOOK_LOCAL_IN],
895 handle->info.underflow[HOOK_FORWARD],
896 handle->info.underflow[HOOK_LOCAL_OUT],
897 handle->info.underflow[HOOK_POST_ROUTING]);
Marc Bouchere6869a82000-03-20 06:03:29 +0000898
Harald Welteaae69be2004-08-29 23:32:14 +0000899 ENTRY_ITERATE(handle->entries->entrytable, handle->entries->size,
Rusty Russell79dee072000-05-02 16:45:16 +0000900 dump_entry, handle);
Harald Welteaae69be2004-08-29 23:32:14 +0000901#endif
Harald Welte0113fe72004-01-06 19:04:02 +0000902}
Rusty Russell30fd6e52000-04-23 09:16:06 +0000903
Marc Bouchere6869a82000-03-20 06:03:29 +0000904/* Does this chain exist? */
Rusty Russell79dee072000-05-02 16:45:16 +0000905int TC_IS_CHAIN(const char *chain, const TC_HANDLE_T handle)
Marc Bouchere6869a82000-03-20 06:03:29 +0000906{
Harald Welteaae69be2004-08-29 23:32:14 +0000907 return iptcc_find_label(chain, handle) != NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +0000908}
909
Harald Welteaae69be2004-08-29 23:32:14 +0000910static void iptcc_chain_iterator_advance(TC_HANDLE_T handle)
911{
912 struct chain_head *c = handle->chain_iterator_cur;
913
914 if (c->list.next == &handle->chains)
915 handle->chain_iterator_cur = NULL;
916 else
917 handle->chain_iterator_cur =
918 list_entry(c->list.next, struct chain_head, list);
919}
Marc Bouchere6869a82000-03-20 06:03:29 +0000920
Rusty Russell30fd6e52000-04-23 09:16:06 +0000921/* Iterator functions to run through the chains. */
Marc Bouchere6869a82000-03-20 06:03:29 +0000922const char *
Philip Blundell8c700902000-05-15 02:17:52 +0000923TC_FIRST_CHAIN(TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +0000924{
Harald Welteaae69be2004-08-29 23:32:14 +0000925 struct chain_head *c = list_entry((*handle)->chains.next,
926 struct chain_head, list);
927
928 iptc_fn = TC_FIRST_CHAIN;
929
930
931 if (list_empty(&(*handle)->chains)) {
932 DEBUGP(": no chains\n");
Harald Welte0113fe72004-01-06 19:04:02 +0000933 return NULL;
Harald Welteaae69be2004-08-29 23:32:14 +0000934 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000935
Harald Welteaae69be2004-08-29 23:32:14 +0000936 (*handle)->chain_iterator_cur = c;
937 iptcc_chain_iterator_advance(*handle);
Harald Welte0113fe72004-01-06 19:04:02 +0000938
Harald Welteaae69be2004-08-29 23:32:14 +0000939 DEBUGP(": returning `%s'\n", c->name);
940 return c->name;
Marc Bouchere6869a82000-03-20 06:03:29 +0000941}
942
Rusty Russell30fd6e52000-04-23 09:16:06 +0000943/* Iterator functions to run through the chains. Returns NULL at end. */
944const char *
Rusty Russell79dee072000-05-02 16:45:16 +0000945TC_NEXT_CHAIN(TC_HANDLE_T *handle)
Rusty Russell30fd6e52000-04-23 09:16:06 +0000946{
Harald Welteaae69be2004-08-29 23:32:14 +0000947 struct chain_head *c = (*handle)->chain_iterator_cur;
Rusty Russell30fd6e52000-04-23 09:16:06 +0000948
Harald Welteaae69be2004-08-29 23:32:14 +0000949 iptc_fn = TC_NEXT_CHAIN;
950
951 if (!c) {
952 DEBUGP(": no more chains\n");
Rusty Russell30fd6e52000-04-23 09:16:06 +0000953 return NULL;
Harald Welteaae69be2004-08-29 23:32:14 +0000954 }
Rusty Russell30fd6e52000-04-23 09:16:06 +0000955
Harald Welteaae69be2004-08-29 23:32:14 +0000956 iptcc_chain_iterator_advance(*handle);
957
958 DEBUGP(": returning `%s'\n", c->name);
959 return c->name;
Rusty Russell30fd6e52000-04-23 09:16:06 +0000960}
961
962/* Get first rule in the given chain: NULL for empty chain. */
Rusty Russell79dee072000-05-02 16:45:16 +0000963const STRUCT_ENTRY *
Philip Blundell8c700902000-05-15 02:17:52 +0000964TC_FIRST_RULE(const char *chain, TC_HANDLE_T *handle)
Rusty Russell30fd6e52000-04-23 09:16:06 +0000965{
Harald Welteaae69be2004-08-29 23:32:14 +0000966 struct chain_head *c;
967 struct rule_head *r;
Rusty Russell30fd6e52000-04-23 09:16:06 +0000968
Harald Welteaae69be2004-08-29 23:32:14 +0000969 iptc_fn = TC_FIRST_RULE;
970
971 DEBUGP("first rule(%s): ", chain);
972
973 c = iptcc_find_label(chain, *handle);
Rusty Russell30fd6e52000-04-23 09:16:06 +0000974 if (!c) {
975 errno = ENOENT;
976 return NULL;
977 }
978
979 /* Empty chain: single return/policy rule */
Harald Welteaae69be2004-08-29 23:32:14 +0000980 if (list_empty(&c->rules)) {
981 DEBUGP_C("no rules, returning NULL\n");
Rusty Russell30fd6e52000-04-23 09:16:06 +0000982 return NULL;
Harald Welteaae69be2004-08-29 23:32:14 +0000983 }
Rusty Russell30fd6e52000-04-23 09:16:06 +0000984
Harald Welteaae69be2004-08-29 23:32:14 +0000985 r = list_entry(c->rules.next, struct rule_head, list);
986 (*handle)->rule_iterator_cur = r;
987 DEBUGP_C("%p\n", r);
988
989 return r->entry;
Rusty Russell30fd6e52000-04-23 09:16:06 +0000990}
991
992/* Returns NULL when rules run out. */
Rusty Russell79dee072000-05-02 16:45:16 +0000993const STRUCT_ENTRY *
Philip Blundell8c700902000-05-15 02:17:52 +0000994TC_NEXT_RULE(const STRUCT_ENTRY *prev, TC_HANDLE_T *handle)
Rusty Russell30fd6e52000-04-23 09:16:06 +0000995{
Harald Welteaae69be2004-08-29 23:32:14 +0000996 struct rule_head *r;
Rusty Russell30fd6e52000-04-23 09:16:06 +0000997
Harald Welteaae69be2004-08-29 23:32:14 +0000998 DEBUGP("rule_iterator_cur=%p...", (*handle)->rule_iterator_cur);
999
1000 if (!(*handle)->rule_iterator_cur) {
1001 DEBUGP_C("returning NULL\n");
1002 return NULL;
1003 }
1004
1005 r = list_entry((*handle)->rule_iterator_cur->list.next,
1006 struct rule_head, list);
1007
1008 iptc_fn = TC_NEXT_RULE;
1009
1010 DEBUGP_C("next=%p, head=%p...", &r->list,
1011 &(*handle)->rule_iterator_cur->chain->rules);
1012
1013 if (&r->list == &(*handle)->rule_iterator_cur->chain->rules) {
1014 (*handle)->rule_iterator_cur = NULL;
1015 DEBUGP_C("finished, returning NULL\n");
1016 return NULL;
1017 }
1018
1019 (*handle)->rule_iterator_cur = r;
1020
1021 /* NOTE: prev is without any influence ! */
1022 DEBUGP_C("returning rule %p\n", r);
1023 return r->entry;
Rusty Russell30fd6e52000-04-23 09:16:06 +00001024}
1025
Marc Bouchere6869a82000-03-20 06:03:29 +00001026/* How many rules in this chain? */
1027unsigned int
Rusty Russell79dee072000-05-02 16:45:16 +00001028TC_NUM_RULES(const char *chain, TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001029{
Harald Welteaae69be2004-08-29 23:32:14 +00001030 struct chain_head *c;
1031 iptc_fn = TC_NUM_RULES;
Marc Bouchere6869a82000-03-20 06:03:29 +00001032 CHECK(*handle);
Harald Welteaae69be2004-08-29 23:32:14 +00001033
1034 c = iptcc_find_label(chain, *handle);
1035 if (!c) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001036 errno = ENOENT;
1037 return (unsigned int)-1;
1038 }
Harald Welteaae69be2004-08-29 23:32:14 +00001039
1040 return c->num_rules;
Marc Bouchere6869a82000-03-20 06:03:29 +00001041}
1042
Rusty Russell79dee072000-05-02 16:45:16 +00001043const STRUCT_ENTRY *TC_GET_RULE(const char *chain,
1044 unsigned int n,
1045 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001046{
Harald Welteaae69be2004-08-29 23:32:14 +00001047 struct chain_head *c;
1048 struct rule_head *r;
1049
1050 iptc_fn = TC_GET_RULE;
Marc Bouchere6869a82000-03-20 06:03:29 +00001051
1052 CHECK(*handle);
Harald Welteaae69be2004-08-29 23:32:14 +00001053
1054 c = iptcc_find_label(chain, *handle);
1055 if (!c) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001056 errno = ENOENT;
1057 return NULL;
1058 }
1059
Harald Welteaae69be2004-08-29 23:32:14 +00001060 r = iptcc_get_rule_num(c, n);
1061 if (!r)
1062 return NULL;
1063 return r->entry;
Marc Bouchere6869a82000-03-20 06:03:29 +00001064}
1065
1066/* Returns a pointer to the target name of this position. */
Harald Welteaae69be2004-08-29 23:32:14 +00001067const char *standard_target_map(int verdict)
Marc Bouchere6869a82000-03-20 06:03:29 +00001068{
Harald Welteaae69be2004-08-29 23:32:14 +00001069 switch (verdict) {
1070 case RETURN:
1071 return LABEL_RETURN;
1072 break;
1073 case -NF_ACCEPT-1:
1074 return LABEL_ACCEPT;
1075 break;
1076 case -NF_DROP-1:
1077 return LABEL_DROP;
1078 break;
1079 case -NF_QUEUE-1:
1080 return LABEL_QUEUE;
1081 break;
1082 default:
1083 fprintf(stderr, "ERROR: %d not a valid target)\n",
1084 verdict);
1085 abort();
1086 break;
1087 }
1088 /* not reached */
1089 return NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +00001090}
1091
Harald Welteaae69be2004-08-29 23:32:14 +00001092/* Returns a pointer to the target name of this position. */
1093const char *TC_GET_TARGET(const STRUCT_ENTRY *ce,
1094 TC_HANDLE_T *handle)
1095{
1096 STRUCT_ENTRY *e = (STRUCT_ENTRY *)ce;
1097 struct rule_head *r = container_of(e, struct rule_head, entry);
1098
1099 iptc_fn = TC_GET_TARGET;
1100
1101 switch(r->type) {
1102 int spos;
1103 case IPTCC_R_FALLTHROUGH:
1104 return "";
1105 break;
1106 case IPTCC_R_JUMP:
1107 DEBUGP("r=%p, jump=%p, name=`%s'\n", r, r->jump, r->jump->name);
1108 return r->jump->name;
1109 break;
1110 case IPTCC_R_STANDARD:
1111 spos = *(int *)GET_TARGET(e)->data;
1112 DEBUGP("r=%p, spos=%d'\n", r, spos);
1113 return standard_target_map(spos);
1114 break;
1115 case IPTCC_R_MODULE:
1116 return GET_TARGET(e)->u.user.name;
1117 break;
1118 }
1119 return NULL;
1120}
Marc Bouchere6869a82000-03-20 06:03:29 +00001121/* Is this a built-in chain? Actually returns hook + 1. */
1122int
Rusty Russell79dee072000-05-02 16:45:16 +00001123TC_BUILTIN(const char *chain, const TC_HANDLE_T handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001124{
Harald Welteaae69be2004-08-29 23:32:14 +00001125 struct chain_head *c;
1126
1127 iptc_fn = TC_BUILTIN;
Marc Bouchere6869a82000-03-20 06:03:29 +00001128
Harald Welteaae69be2004-08-29 23:32:14 +00001129 c = iptcc_find_label(chain, handle);
1130 if (!c) {
1131 errno = ENOENT;
1132 return -1;
Marc Bouchere6869a82000-03-20 06:03:29 +00001133 }
Harald Welteaae69be2004-08-29 23:32:14 +00001134
1135 return iptcc_is_builtin(c);
Marc Bouchere6869a82000-03-20 06:03:29 +00001136}
1137
1138/* Get the policy of a given built-in chain */
1139const char *
Rusty Russell79dee072000-05-02 16:45:16 +00001140TC_GET_POLICY(const char *chain,
1141 STRUCT_COUNTERS *counters,
1142 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001143{
Harald Welteaae69be2004-08-29 23:32:14 +00001144 struct chain_head *c;
Marc Bouchere6869a82000-03-20 06:03:29 +00001145
Harald Welteaae69be2004-08-29 23:32:14 +00001146 iptc_fn = TC_GET_POLICY;
1147
1148 DEBUGP("called for chain %s\n", chain);
1149
1150 c = iptcc_find_label(chain, *handle);
1151 if (!c) {
1152 errno = ENOENT;
1153 return NULL;
1154 }
1155
1156 if (!iptcc_is_builtin(c))
Marc Bouchere6869a82000-03-20 06:03:29 +00001157 return NULL;
1158
Harald Welteaae69be2004-08-29 23:32:14 +00001159 *counters = c->counters;
Marc Bouchere6869a82000-03-20 06:03:29 +00001160
Harald Welteaae69be2004-08-29 23:32:14 +00001161 return standard_target_map(c->verdict);
Harald Welte0113fe72004-01-06 19:04:02 +00001162}
1163
1164static int
Harald Welteaae69be2004-08-29 23:32:14 +00001165iptcc_standard_map(struct rule_head *r, int verdict)
Harald Welte0113fe72004-01-06 19:04:02 +00001166{
Harald Welteaae69be2004-08-29 23:32:14 +00001167 STRUCT_ENTRY *e = r->entry;
Rusty Russell79dee072000-05-02 16:45:16 +00001168 STRUCT_STANDARD_TARGET *t;
Marc Bouchere6869a82000-03-20 06:03:29 +00001169
Rusty Russell79dee072000-05-02 16:45:16 +00001170 t = (STRUCT_STANDARD_TARGET *)GET_TARGET(e);
Marc Bouchere6869a82000-03-20 06:03:29 +00001171
Rusty Russell67088e72000-05-10 01:18:57 +00001172 if (t->target.u.target_size
Philip Blundell8c700902000-05-15 02:17:52 +00001173 != ALIGN(sizeof(STRUCT_STANDARD_TARGET))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001174 errno = EINVAL;
1175 return 0;
1176 }
1177 /* memset for memcmp convenience on delete/replace */
Rusty Russell79dee072000-05-02 16:45:16 +00001178 memset(t->target.u.user.name, 0, FUNCTION_MAXNAMELEN);
1179 strcpy(t->target.u.user.name, STANDARD_TARGET);
Marc Bouchere6869a82000-03-20 06:03:29 +00001180 t->verdict = verdict;
1181
Harald Welteaae69be2004-08-29 23:32:14 +00001182 r->type = IPTCC_R_STANDARD;
1183
Marc Bouchere6869a82000-03-20 06:03:29 +00001184 return 1;
1185}
Rusty Russell7e53bf92000-03-20 07:03:28 +00001186
Marc Bouchere6869a82000-03-20 06:03:29 +00001187static int
Harald Welteaae69be2004-08-29 23:32:14 +00001188iptcc_map_target(const TC_HANDLE_T handle,
1189 struct rule_head *r)
Marc Bouchere6869a82000-03-20 06:03:29 +00001190{
Harald Welteaae69be2004-08-29 23:32:14 +00001191 STRUCT_ENTRY *e = r->entry;
Harald Welte0113fe72004-01-06 19:04:02 +00001192 STRUCT_ENTRY_TARGET *t = GET_TARGET(e);
Marc Bouchere6869a82000-03-20 06:03:29 +00001193
Marc Bouchere6869a82000-03-20 06:03:29 +00001194 /* Maybe it's empty (=> fall through) */
Harald Welteaae69be2004-08-29 23:32:14 +00001195 if (strcmp(t->u.user.name, "") == 0) {
1196 r->type = IPTCC_R_FALLTHROUGH;
1197 return 1;
1198 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001199 /* Maybe it's a standard target name... */
Rusty Russell79dee072000-05-02 16:45:16 +00001200 else if (strcmp(t->u.user.name, LABEL_ACCEPT) == 0)
Harald Welteaae69be2004-08-29 23:32:14 +00001201 return iptcc_standard_map(r, -NF_ACCEPT - 1);
Rusty Russell79dee072000-05-02 16:45:16 +00001202 else if (strcmp(t->u.user.name, LABEL_DROP) == 0)
Harald Welteaae69be2004-08-29 23:32:14 +00001203 return iptcc_standard_map(r, -NF_DROP - 1);
Rusty Russell67088e72000-05-10 01:18:57 +00001204 else if (strcmp(t->u.user.name, LABEL_QUEUE) == 0)
Harald Welteaae69be2004-08-29 23:32:14 +00001205 return iptcc_standard_map(r, -NF_QUEUE - 1);
Rusty Russell79dee072000-05-02 16:45:16 +00001206 else if (strcmp(t->u.user.name, LABEL_RETURN) == 0)
Harald Welteaae69be2004-08-29 23:32:14 +00001207 return iptcc_standard_map(r, RETURN);
Rusty Russell79dee072000-05-02 16:45:16 +00001208 else if (TC_BUILTIN(t->u.user.name, handle)) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001209 /* Can't jump to builtins. */
1210 errno = EINVAL;
1211 return 0;
1212 } else {
1213 /* Maybe it's an existing chain name. */
Harald Welteaae69be2004-08-29 23:32:14 +00001214 struct chain_head *c;
1215 DEBUGP("trying to find chain `%s': ", t->u.user.name);
Marc Bouchere6869a82000-03-20 06:03:29 +00001216
Harald Welteaae69be2004-08-29 23:32:14 +00001217 c = iptcc_find_label(t->u.user.name, handle);
1218 if (c) {
1219 DEBUGP_C("found!\n");
1220 r->type = IPTCC_R_JUMP;
1221 r->jump = c;
1222 c->references++;
1223 return 1;
1224 }
1225 DEBUGP_C("not found :(\n");
Marc Bouchere6869a82000-03-20 06:03:29 +00001226 }
1227
1228 /* Must be a module? If not, kernel will reject... */
1229 /* memset to all 0 for your memcmp convenience. */
Rusty Russell228e98d2000-04-27 10:28:06 +00001230 memset(t->u.user.name + strlen(t->u.user.name),
Marc Bouchere6869a82000-03-20 06:03:29 +00001231 0,
Rusty Russell79dee072000-05-02 16:45:16 +00001232 FUNCTION_MAXNAMELEN - strlen(t->u.user.name));
Harald Welteaae69be2004-08-29 23:32:14 +00001233
1234 set_changed(handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001235 return 1;
1236}
1237
Harald Welte0113fe72004-01-06 19:04:02 +00001238/* Insert the entry `fw' in chain `chain' into position `rulenum'. */
Marc Bouchere6869a82000-03-20 06:03:29 +00001239int
Rusty Russell79dee072000-05-02 16:45:16 +00001240TC_INSERT_ENTRY(const IPT_CHAINLABEL chain,
1241 const STRUCT_ENTRY *e,
1242 unsigned int rulenum,
1243 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001244{
Harald Welteaae69be2004-08-29 23:32:14 +00001245 struct chain_head *c;
1246 struct rule_head *r, *prev;
Marc Bouchere6869a82000-03-20 06:03:29 +00001247
Rusty Russell79dee072000-05-02 16:45:16 +00001248 iptc_fn = TC_INSERT_ENTRY;
Harald Welteaae69be2004-08-29 23:32:14 +00001249
1250 if (!(c = iptcc_find_label(chain, *handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001251 errno = ENOENT;
1252 return 0;
1253 }
1254
Harald Welteaae69be2004-08-29 23:32:14 +00001255 prev = iptcc_get_rule_num(c, rulenum);
1256 if (!prev) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001257 errno = E2BIG;
1258 return 0;
1259 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001260
Harald Welteaae69be2004-08-29 23:32:14 +00001261 if (!(r = iptcc_alloc_rule(c, e->next_offset))) {
1262 errno = ENOMEM;
Harald Welte0113fe72004-01-06 19:04:02 +00001263 return 0;
Harald Welteaae69be2004-08-29 23:32:14 +00001264 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001265
Harald Welteaae69be2004-08-29 23:32:14 +00001266 memcpy(r->entry, e, e->next_offset);
1267 r->counter_map.maptype = COUNTER_MAP_SET;
1268
1269 if (!iptcc_map_target(*handle, r)) {
1270 free(r);
1271 return 0;
1272 }
1273
1274 list_add_tail(&r->list, &prev->list);
1275 c->num_rules++;
1276
1277 set_changed(*handle);
1278
1279 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00001280}
1281
1282/* Atomically replace rule `rulenum' in `chain' with `fw'. */
1283int
Rusty Russell79dee072000-05-02 16:45:16 +00001284TC_REPLACE_ENTRY(const IPT_CHAINLABEL chain,
1285 const STRUCT_ENTRY *e,
1286 unsigned int rulenum,
1287 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001288{
Harald Welteaae69be2004-08-29 23:32:14 +00001289 struct chain_head *c;
1290 struct rule_head *r, *old;
Marc Bouchere6869a82000-03-20 06:03:29 +00001291
Rusty Russell79dee072000-05-02 16:45:16 +00001292 iptc_fn = TC_REPLACE_ENTRY;
Marc Bouchere6869a82000-03-20 06:03:29 +00001293
Harald Welteaae69be2004-08-29 23:32:14 +00001294 if (!(c = iptcc_find_label(chain, *handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001295 errno = ENOENT;
1296 return 0;
1297 }
1298
Harald Welteaae69be2004-08-29 23:32:14 +00001299 if (!(old = iptcc_get_rule_num(c, rulenum))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001300 errno = E2BIG;
1301 return 0;
1302 }
1303
Harald Welteaae69be2004-08-29 23:32:14 +00001304 if (!(r = iptcc_alloc_rule(c, e->next_offset))) {
1305 errno = ENOMEM;
Marc Bouchere6869a82000-03-20 06:03:29 +00001306 return 0;
Harald Welteaae69be2004-08-29 23:32:14 +00001307 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001308
Harald Welteaae69be2004-08-29 23:32:14 +00001309 memcpy(r->entry, e, e->next_offset);
1310 r->counter_map.maptype = COUNTER_MAP_SET;
1311
1312 if (!iptcc_map_target(*handle, r)) {
1313 free(r);
Harald Welte0113fe72004-01-06 19:04:02 +00001314 return 0;
Harald Welteaae69be2004-08-29 23:32:14 +00001315 }
Harald Welte0113fe72004-01-06 19:04:02 +00001316
Harald Welteaae69be2004-08-29 23:32:14 +00001317 list_add(&r->list, &old->list);
1318 iptcc_delete_rule(old);
1319
1320 set_changed(*handle);
1321
1322 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00001323}
1324
Harald Welte0113fe72004-01-06 19:04:02 +00001325/* Append entry `fw' to chain `chain'. Equivalent to insert with
Marc Bouchere6869a82000-03-20 06:03:29 +00001326 rulenum = length of chain. */
1327int
Rusty Russell79dee072000-05-02 16:45:16 +00001328TC_APPEND_ENTRY(const IPT_CHAINLABEL chain,
1329 const STRUCT_ENTRY *e,
1330 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001331{
Harald Welteaae69be2004-08-29 23:32:14 +00001332 struct chain_head *c;
1333 struct rule_head *r;
Marc Bouchere6869a82000-03-20 06:03:29 +00001334
Rusty Russell79dee072000-05-02 16:45:16 +00001335 iptc_fn = TC_APPEND_ENTRY;
Harald Welteaae69be2004-08-29 23:32:14 +00001336 if (!(c = iptcc_find_label(chain, *handle))) {
1337 DEBUGP("unable to find chain `%s'\n", chain);
Marc Bouchere6869a82000-03-20 06:03:29 +00001338 errno = ENOENT;
1339 return 0;
1340 }
1341
Harald Welteaae69be2004-08-29 23:32:14 +00001342 if (!(r = iptcc_alloc_rule(c, e->next_offset))) {
1343 DEBUGP("unable to allocate rule for chain `%s'\n", chain);
1344 errno = ENOMEM;
Harald Welte0113fe72004-01-06 19:04:02 +00001345 return 0;
Harald Welteaae69be2004-08-29 23:32:14 +00001346 }
Harald Welte0113fe72004-01-06 19:04:02 +00001347
Harald Welteaae69be2004-08-29 23:32:14 +00001348 memcpy(r->entry, e, e->next_offset);
1349 r->counter_map.maptype = COUNTER_MAP_SET;
1350
1351 if (!iptcc_map_target(*handle, r)) {
1352 DEBUGP("unable to ma target of rule for chain `%s'\n", chain);
1353 free(r);
1354 return 0;
1355 }
1356
1357 list_add_tail(&r->list, &c->rules);
1358 c->num_rules++;
1359
1360 set_changed(*handle);
1361
1362 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00001363}
1364
1365static inline int
Rusty Russell79dee072000-05-02 16:45:16 +00001366match_different(const STRUCT_ENTRY_MATCH *a,
Rusty Russelledf14cf2000-04-19 11:26:44 +00001367 const unsigned char *a_elems,
1368 const unsigned char *b_elems,
1369 unsigned char **maskptr)
Marc Bouchere6869a82000-03-20 06:03:29 +00001370{
Rusty Russell79dee072000-05-02 16:45:16 +00001371 const STRUCT_ENTRY_MATCH *b;
Rusty Russelledf14cf2000-04-19 11:26:44 +00001372 unsigned int i;
Marc Bouchere6869a82000-03-20 06:03:29 +00001373
1374 /* Offset of b is the same as a. */
Rusty Russell30fd6e52000-04-23 09:16:06 +00001375 b = (void *)b_elems + ((unsigned char *)a - a_elems);
Marc Bouchere6869a82000-03-20 06:03:29 +00001376
Rusty Russell228e98d2000-04-27 10:28:06 +00001377 if (a->u.match_size != b->u.match_size)
Marc Bouchere6869a82000-03-20 06:03:29 +00001378 return 1;
1379
Rusty Russell228e98d2000-04-27 10:28:06 +00001380 if (strcmp(a->u.user.name, b->u.user.name) != 0)
Marc Bouchere6869a82000-03-20 06:03:29 +00001381 return 1;
1382
Rusty Russell73ef09b2000-07-03 10:24:04 +00001383 *maskptr += ALIGN(sizeof(*a));
Rusty Russelledf14cf2000-04-19 11:26:44 +00001384
Rusty Russell73ef09b2000-07-03 10:24:04 +00001385 for (i = 0; i < a->u.match_size - ALIGN(sizeof(*a)); i++)
Rusty Russelledf14cf2000-04-19 11:26:44 +00001386 if (((a->data[i] ^ b->data[i]) & (*maskptr)[i]) != 0)
Rusty Russell90e712a2000-03-29 04:19:26 +00001387 return 1;
Rusty Russelledf14cf2000-04-19 11:26:44 +00001388 *maskptr += i;
1389 return 0;
1390}
1391
1392static inline int
1393target_different(const unsigned char *a_targdata,
1394 const unsigned char *b_targdata,
1395 unsigned int tdatasize,
1396 const unsigned char *mask)
1397{
1398 unsigned int i;
1399 for (i = 0; i < tdatasize; i++)
1400 if (((a_targdata[i] ^ b_targdata[i]) & mask[i]) != 0)
1401 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00001402
1403 return 0;
1404}
1405
Rusty Russell79dee072000-05-02 16:45:16 +00001406static int
1407is_same(const STRUCT_ENTRY *a,
1408 const STRUCT_ENTRY *b,
1409 unsigned char *matchmask);
Marc Bouchere6869a82000-03-20 06:03:29 +00001410
Harald Welte0113fe72004-01-06 19:04:02 +00001411/* Delete the first rule in `chain' which matches `fw'. */
Marc Bouchere6869a82000-03-20 06:03:29 +00001412int
Rusty Russell79dee072000-05-02 16:45:16 +00001413TC_DELETE_ENTRY(const IPT_CHAINLABEL chain,
1414 const STRUCT_ENTRY *origfw,
1415 unsigned char *matchmask,
1416 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001417{
Harald Welteaae69be2004-08-29 23:32:14 +00001418 struct chain_head *c;
Harald Weltefe537072004-08-30 20:28:53 +00001419 struct rule_head *r;
Harald Welte0113fe72004-01-06 19:04:02 +00001420 STRUCT_ENTRY *e, *fw;
Marc Bouchere6869a82000-03-20 06:03:29 +00001421
Rusty Russell79dee072000-05-02 16:45:16 +00001422 iptc_fn = TC_DELETE_ENTRY;
Harald Welteaae69be2004-08-29 23:32:14 +00001423 if (!(c = iptcc_find_label(chain, *handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001424 errno = ENOENT;
1425 return 0;
1426 }
1427
Harald Welte0113fe72004-01-06 19:04:02 +00001428 fw = malloc(origfw->next_offset);
1429 if (fw == NULL) {
1430 errno = ENOMEM;
1431 return 0;
1432 }
1433
Harald Weltefe537072004-08-30 20:28:53 +00001434 list_for_each_entry(r, &c->rules, list) {
Harald Welte0113fe72004-01-06 19:04:02 +00001435
1436 memcpy(fw, origfw, origfw->next_offset);
1437
Harald Weltefe537072004-08-30 20:28:53 +00001438#if 0
Harald Welte0113fe72004-01-06 19:04:02 +00001439 /* FIXME: handle this in is_same --RR */
1440 if (!map_target(*handle, fw, offset, &discard)) {
1441 free(fw);
1442 return 0;
1443 }
Harald Welte0113fe72004-01-06 19:04:02 +00001444#endif
Harald Weltefe537072004-08-30 20:28:53 +00001445
1446 e = r->entry;
1447
Harald Welte0113fe72004-01-06 19:04:02 +00001448 if (is_same(e, fw, matchmask)) {
Harald Weltefe537072004-08-30 20:28:53 +00001449 /* If we are about to delete the rule that is the
1450 * current iterator, move rule iterator back. next
1451 * pointer will then point to real next node */
1452 if (r == (*handle)->rule_iterator_cur) {
1453 (*handle)->rule_iterator_cur =
1454 list_entry((*handle)->rule_iterator_cur->list.prev,
1455 struct rule_head, list);
1456 }
1457
1458 c->num_rules--;
1459 iptcc_delete_rule(r);
1460 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00001461 }
1462 }
1463
Harald Welte0113fe72004-01-06 19:04:02 +00001464 free(fw);
Marc Bouchere6869a82000-03-20 06:03:29 +00001465 errno = ENOENT;
1466 return 0;
Rusty Russell7e53bf92000-03-20 07:03:28 +00001467}
Harald Welteaae69be2004-08-29 23:32:14 +00001468
Marc Bouchere6869a82000-03-20 06:03:29 +00001469
1470/* Delete the rule in position `rulenum' in `chain'. */
1471int
Rusty Russell79dee072000-05-02 16:45:16 +00001472TC_DELETE_NUM_ENTRY(const IPT_CHAINLABEL chain,
1473 unsigned int rulenum,
1474 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001475{
Harald Welteaae69be2004-08-29 23:32:14 +00001476 struct chain_head *c;
1477 struct rule_head *r;
Marc Bouchere6869a82000-03-20 06:03:29 +00001478
Rusty Russell79dee072000-05-02 16:45:16 +00001479 iptc_fn = TC_DELETE_NUM_ENTRY;
Harald Welteaae69be2004-08-29 23:32:14 +00001480
1481 if (!(c = iptcc_find_label(chain, *handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001482 errno = ENOENT;
1483 return 0;
1484 }
1485
Harald Welteaae69be2004-08-29 23:32:14 +00001486 if (!(r = iptcc_get_rule_num(c, rulenum))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001487 errno = E2BIG;
1488 return 0;
1489 }
1490
Harald Welteaae69be2004-08-29 23:32:14 +00001491 /* If we are about to delete the rule that is the current
1492 * iterator, move rule iterator back. next pointer will then
1493 * point to real next node */
1494 if (r == (*handle)->rule_iterator_cur) {
1495 (*handle)->rule_iterator_cur =
1496 list_entry((*handle)->rule_iterator_cur->list.prev,
1497 struct rule_head, list);
Harald Welte0113fe72004-01-06 19:04:02 +00001498 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001499
Harald Welteaae69be2004-08-29 23:32:14 +00001500 c->num_rules--;
1501 iptcc_delete_rule(r);
1502
1503 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00001504}
1505
1506/* Check the packet `fw' on chain `chain'. Returns the verdict, or
1507 NULL and sets errno. */
1508const char *
Rusty Russell79dee072000-05-02 16:45:16 +00001509TC_CHECK_PACKET(const IPT_CHAINLABEL chain,
1510 STRUCT_ENTRY *entry,
1511 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001512{
1513 errno = ENOSYS;
1514 return NULL;
1515}
1516
1517/* Flushes the entries in the given chain (ie. empties chain). */
1518int
Rusty Russell79dee072000-05-02 16:45:16 +00001519TC_FLUSH_ENTRIES(const IPT_CHAINLABEL chain, TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001520{
Harald Welteaae69be2004-08-29 23:32:14 +00001521 struct chain_head *c;
1522 struct rule_head *r, *tmp;
Marc Bouchere6869a82000-03-20 06:03:29 +00001523
Harald Welte0113fe72004-01-06 19:04:02 +00001524 iptc_fn = TC_FLUSH_ENTRIES;
Harald Welteaae69be2004-08-29 23:32:14 +00001525 if (!(c = iptcc_find_label(chain, *handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001526 errno = ENOENT;
1527 return 0;
1528 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001529
Harald Welteaae69be2004-08-29 23:32:14 +00001530 list_for_each_entry_safe(r, tmp, &c->rules, list) {
1531 iptcc_delete_rule(r);
1532 }
1533
1534 c->num_rules = 0;
1535
1536 set_changed(*handle);
1537
1538 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00001539}
1540
1541/* Zeroes the counters in a chain. */
1542int
Rusty Russell79dee072000-05-02 16:45:16 +00001543TC_ZERO_ENTRIES(const IPT_CHAINLABEL chain, TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001544{
Harald Welteaae69be2004-08-29 23:32:14 +00001545 struct chain_head *c;
1546 struct rule_head *r;
Rusty Russell7e53bf92000-03-20 07:03:28 +00001547
Harald Welteaae69be2004-08-29 23:32:14 +00001548 if (!(c = iptcc_find_label(chain, *handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001549 errno = ENOENT;
1550 return 0;
1551 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001552
Harald Welteaae69be2004-08-29 23:32:14 +00001553 list_for_each_entry(r, &c->rules, list) {
1554 if (r->counter_map.maptype == COUNTER_MAP_NORMAL_MAP)
1555 r->counter_map.maptype = COUNTER_MAP_ZEROED;
Marc Bouchere6869a82000-03-20 06:03:29 +00001556 }
Harald Welteaae69be2004-08-29 23:32:14 +00001557
Rusty Russell175f6412000-03-24 09:32:20 +00001558 set_changed(*handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001559
Marc Bouchere6869a82000-03-20 06:03:29 +00001560 return 1;
1561}
1562
Harald Welte1cef74d2001-01-05 15:22:59 +00001563STRUCT_COUNTERS *
1564TC_READ_COUNTER(const IPT_CHAINLABEL chain,
1565 unsigned int rulenum,
1566 TC_HANDLE_T *handle)
1567{
Harald Welteaae69be2004-08-29 23:32:14 +00001568 struct chain_head *c;
1569 struct rule_head *r;
Harald Welte1cef74d2001-01-05 15:22:59 +00001570
1571 iptc_fn = TC_READ_COUNTER;
1572 CHECK(*handle);
1573
Harald Welteaae69be2004-08-29 23:32:14 +00001574 if (!(c = iptcc_find_label(chain, *handle))) {
Harald Welte1cef74d2001-01-05 15:22:59 +00001575 errno = ENOENT;
1576 return NULL;
1577 }
1578
Harald Welteaae69be2004-08-29 23:32:14 +00001579 if (!(r = iptcc_get_rule_num(c, rulenum))) {
Harald Welte0113fe72004-01-06 19:04:02 +00001580 errno = E2BIG;
1581 return NULL;
1582 }
1583
Harald Welteaae69be2004-08-29 23:32:14 +00001584 return &r->entry[0].counters;
Harald Welte1cef74d2001-01-05 15:22:59 +00001585}
1586
1587int
1588TC_ZERO_COUNTER(const IPT_CHAINLABEL chain,
1589 unsigned int rulenum,
1590 TC_HANDLE_T *handle)
1591{
Harald Welteaae69be2004-08-29 23:32:14 +00001592 struct chain_head *c;
1593 struct rule_head *r;
Harald Welte1cef74d2001-01-05 15:22:59 +00001594
1595 iptc_fn = TC_ZERO_COUNTER;
1596 CHECK(*handle);
1597
Harald Welteaae69be2004-08-29 23:32:14 +00001598 if (!(c = iptcc_find_label(chain, *handle))) {
Harald Welte1cef74d2001-01-05 15:22:59 +00001599 errno = ENOENT;
1600 return 0;
1601 }
1602
Harald Welteaae69be2004-08-29 23:32:14 +00001603 if (!(r = iptcc_get_rule_num(c, rulenum))) {
Harald Welte0113fe72004-01-06 19:04:02 +00001604 errno = E2BIG;
1605 return 0;
1606 }
1607
Harald Welteaae69be2004-08-29 23:32:14 +00001608 if (r->counter_map.maptype == COUNTER_MAP_NORMAL_MAP)
1609 r->counter_map.maptype = COUNTER_MAP_ZEROED;
Harald Welte1cef74d2001-01-05 15:22:59 +00001610
1611 set_changed(*handle);
1612
1613 return 1;
1614}
1615
1616int
1617TC_SET_COUNTER(const IPT_CHAINLABEL chain,
1618 unsigned int rulenum,
1619 STRUCT_COUNTERS *counters,
1620 TC_HANDLE_T *handle)
1621{
Harald Welteaae69be2004-08-29 23:32:14 +00001622 struct chain_head *c;
1623 struct rule_head *r;
Harald Welte1cef74d2001-01-05 15:22:59 +00001624 STRUCT_ENTRY *e;
Harald Welte1cef74d2001-01-05 15:22:59 +00001625
1626 iptc_fn = TC_SET_COUNTER;
1627 CHECK(*handle);
1628
Harald Welteaae69be2004-08-29 23:32:14 +00001629 if (!(c = iptcc_find_label(chain, *handle))) {
Harald Welte1cef74d2001-01-05 15:22:59 +00001630 errno = ENOENT;
1631 return 0;
1632 }
Harald Welte0113fe72004-01-06 19:04:02 +00001633
Harald Welteaae69be2004-08-29 23:32:14 +00001634 if (!(r = iptcc_get_rule_num(c, rulenum))) {
Harald Welte0113fe72004-01-06 19:04:02 +00001635 errno = E2BIG;
1636 return 0;
1637 }
1638
Harald Welteaae69be2004-08-29 23:32:14 +00001639 e = r->entry;
1640 r->counter_map.maptype = COUNTER_MAP_SET;
Harald Welte0113fe72004-01-06 19:04:02 +00001641
1642 memcpy(&e->counters, counters, sizeof(STRUCT_COUNTERS));
Harald Welte1cef74d2001-01-05 15:22:59 +00001643
1644 set_changed(*handle);
1645
1646 return 1;
1647}
1648
Marc Bouchere6869a82000-03-20 06:03:29 +00001649/* Creates a new chain. */
1650/* To create a chain, create two rules: error node and unconditional
1651 * return. */
1652int
Rusty Russell79dee072000-05-02 16:45:16 +00001653TC_CREATE_CHAIN(const IPT_CHAINLABEL chain, TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001654{
Harald Welteaae69be2004-08-29 23:32:14 +00001655 static struct chain_head *c;
Marc Bouchere6869a82000-03-20 06:03:29 +00001656
Rusty Russell79dee072000-05-02 16:45:16 +00001657 iptc_fn = TC_CREATE_CHAIN;
Marc Bouchere6869a82000-03-20 06:03:29 +00001658
1659 /* find_label doesn't cover built-in targets: DROP, ACCEPT,
1660 QUEUE, RETURN. */
Harald Welteaae69be2004-08-29 23:32:14 +00001661 if (iptcc_find_label(chain, *handle)
Rusty Russell79dee072000-05-02 16:45:16 +00001662 || strcmp(chain, LABEL_DROP) == 0
1663 || strcmp(chain, LABEL_ACCEPT) == 0
Rusty Russell67088e72000-05-10 01:18:57 +00001664 || strcmp(chain, LABEL_QUEUE) == 0
Rusty Russell79dee072000-05-02 16:45:16 +00001665 || strcmp(chain, LABEL_RETURN) == 0) {
Harald Welteaae69be2004-08-29 23:32:14 +00001666 DEBUGP("Chain `%s' already exists\n", chain);
Marc Bouchere6869a82000-03-20 06:03:29 +00001667 errno = EEXIST;
1668 return 0;
1669 }
1670
Rusty Russell79dee072000-05-02 16:45:16 +00001671 if (strlen(chain)+1 > sizeof(IPT_CHAINLABEL)) {
Harald Welteaae69be2004-08-29 23:32:14 +00001672 DEBUGP("Chain name `%s' too long\n", chain);
Marc Bouchere6869a82000-03-20 06:03:29 +00001673 errno = EINVAL;
1674 return 0;
1675 }
1676
Harald Welteaae69be2004-08-29 23:32:14 +00001677 c = iptcc_alloc_chain_head(chain, 0);
1678 if (!c) {
1679 DEBUGP("Cannot allocate memory for chain `%s'\n", chain);
1680 errno = ENOMEM;
1681 return 0;
Marc Bouchere6869a82000-03-20 06:03:29 +00001682
Harald Welteaae69be2004-08-29 23:32:14 +00001683 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001684
Harald Welteaae69be2004-08-29 23:32:14 +00001685 DEBUGP("Creating chain `%s'\n", chain);
1686 list_add_tail(&c->list, &(*handle)->chains);
Harald Welte0113fe72004-01-06 19:04:02 +00001687
1688 set_changed(*handle);
1689
Harald Welteaae69be2004-08-29 23:32:14 +00001690 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00001691}
1692
1693/* Get the number of references to this chain. */
1694int
Rusty Russell79dee072000-05-02 16:45:16 +00001695TC_GET_REFERENCES(unsigned int *ref, const IPT_CHAINLABEL chain,
1696 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001697{
Harald Welteaae69be2004-08-29 23:32:14 +00001698 struct chain_head *c;
Marc Bouchere6869a82000-03-20 06:03:29 +00001699
Harald Welteaae69be2004-08-29 23:32:14 +00001700 if (!(c = iptcc_find_label(chain, *handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001701 errno = ENOENT;
1702 return 0;
1703 }
1704
Harald Welteaae69be2004-08-29 23:32:14 +00001705 *ref = c->references;
1706
Marc Bouchere6869a82000-03-20 06:03:29 +00001707 return 1;
1708}
1709
1710/* Deletes a chain. */
1711int
Rusty Russell79dee072000-05-02 16:45:16 +00001712TC_DELETE_CHAIN(const IPT_CHAINLABEL chain, TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001713{
Marc Bouchere6869a82000-03-20 06:03:29 +00001714 unsigned int references;
Harald Welteaae69be2004-08-29 23:32:14 +00001715 struct chain_head *c;
Rusty Russell7e53bf92000-03-20 07:03:28 +00001716
Rusty Russell79dee072000-05-02 16:45:16 +00001717 iptc_fn = TC_DELETE_CHAIN;
Marc Bouchere6869a82000-03-20 06:03:29 +00001718
Harald Welteaae69be2004-08-29 23:32:14 +00001719 if (!(c = iptcc_find_label(chain, *handle))) {
1720 DEBUGP("cannot find chain `%s'\n", chain);
Marc Bouchere6869a82000-03-20 06:03:29 +00001721 errno = ENOENT;
1722 return 0;
1723 }
1724
Harald Welteaae69be2004-08-29 23:32:14 +00001725 if (TC_BUILTIN(chain, *handle)) {
1726 DEBUGP("cannot remove builtin chain `%s'\n", chain);
1727 errno = EINVAL;
1728 return 0;
1729 }
1730
1731 if (!TC_GET_REFERENCES(&references, chain, handle)) {
1732 DEBUGP("cannot get references on chain `%s'\n", chain);
1733 return 0;
1734 }
1735
1736 if (references > 0) {
1737 DEBUGP("chain `%s' still has references\n", chain);
1738 errno = EMLINK;
1739 return 0;
1740 }
1741
1742 if (c->num_rules) {
1743 DEBUGP("chain `%s' is not empty\n", chain);
Marc Bouchere6869a82000-03-20 06:03:29 +00001744 errno = ENOTEMPTY;
1745 return 0;
1746 }
1747
Harald Welteaae69be2004-08-29 23:32:14 +00001748 /* If we are about to delete the chain that is the current
1749 * iterator, move chain iterator firward. */
1750 if (c == (*handle)->chain_iterator_cur)
1751 iptcc_chain_iterator_advance(*handle);
Harald Welte0113fe72004-01-06 19:04:02 +00001752
Harald Welteaae69be2004-08-29 23:32:14 +00001753 list_del(&c->list);
1754 free(c);
Harald Welte0113fe72004-01-06 19:04:02 +00001755
Harald Welteaae69be2004-08-29 23:32:14 +00001756 DEBUGP("chain `%s' deleted\n", chain);
1757
1758 set_changed(*handle);
1759
1760 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00001761}
1762
1763/* Renames a chain. */
Rusty Russell79dee072000-05-02 16:45:16 +00001764int TC_RENAME_CHAIN(const IPT_CHAINLABEL oldname,
1765 const IPT_CHAINLABEL newname,
1766 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001767{
Harald Welteaae69be2004-08-29 23:32:14 +00001768 struct chain_head *c;
Rusty Russell79dee072000-05-02 16:45:16 +00001769 iptc_fn = TC_RENAME_CHAIN;
Marc Bouchere6869a82000-03-20 06:03:29 +00001770
Harald Welte1de80462000-10-30 12:00:27 +00001771 /* find_label doesn't cover built-in targets: DROP, ACCEPT,
1772 QUEUE, RETURN. */
Harald Welteaae69be2004-08-29 23:32:14 +00001773 if (iptcc_find_label(newname, *handle)
Rusty Russell79dee072000-05-02 16:45:16 +00001774 || strcmp(newname, LABEL_DROP) == 0
1775 || strcmp(newname, LABEL_ACCEPT) == 0
Harald Welte1de80462000-10-30 12:00:27 +00001776 || strcmp(newname, LABEL_QUEUE) == 0
Rusty Russell79dee072000-05-02 16:45:16 +00001777 || strcmp(newname, LABEL_RETURN) == 0) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001778 errno = EEXIST;
1779 return 0;
1780 }
1781
Harald Welteaae69be2004-08-29 23:32:14 +00001782 if (!(c = iptcc_find_label(oldname, *handle))
Rusty Russell79dee072000-05-02 16:45:16 +00001783 || TC_BUILTIN(oldname, *handle)) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001784 errno = ENOENT;
1785 return 0;
1786 }
1787
Rusty Russell79dee072000-05-02 16:45:16 +00001788 if (strlen(newname)+1 > sizeof(IPT_CHAINLABEL)) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001789 errno = EINVAL;
1790 return 0;
1791 }
1792
Harald Welteaae69be2004-08-29 23:32:14 +00001793 strncpy(c->name, newname, sizeof(IPT_CHAINLABEL));
1794
Harald Welte0113fe72004-01-06 19:04:02 +00001795 set_changed(*handle);
1796
Marc Bouchere6869a82000-03-20 06:03:29 +00001797 return 1;
1798}
1799
1800/* Sets the policy on a built-in chain. */
1801int
Rusty Russell79dee072000-05-02 16:45:16 +00001802TC_SET_POLICY(const IPT_CHAINLABEL chain,
1803 const IPT_CHAINLABEL policy,
Harald Welte1cef74d2001-01-05 15:22:59 +00001804 STRUCT_COUNTERS *counters,
Rusty Russell79dee072000-05-02 16:45:16 +00001805 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001806{
Harald Welteaae69be2004-08-29 23:32:14 +00001807 struct chain_head *c;
Marc Bouchere6869a82000-03-20 06:03:29 +00001808
Rusty Russell79dee072000-05-02 16:45:16 +00001809 iptc_fn = TC_SET_POLICY;
Marc Bouchere6869a82000-03-20 06:03:29 +00001810
Harald Welteaae69be2004-08-29 23:32:14 +00001811 if (!(c = iptcc_find_label(chain, *handle))) {
1812 DEBUGP("cannot find chain `%s'\n", chain);
1813 errno = ENOENT;
Marc Bouchere6869a82000-03-20 06:03:29 +00001814 return 0;
1815 }
1816
Harald Welteaae69be2004-08-29 23:32:14 +00001817 if (!iptcc_is_builtin(c)) {
1818 DEBUGP("cannot set policy of userdefinedchain `%s'\n", chain);
1819 errno = ENOENT;
1820 return 0;
1821 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001822
Rusty Russell79dee072000-05-02 16:45:16 +00001823 if (strcmp(policy, LABEL_ACCEPT) == 0)
Harald Welteaae69be2004-08-29 23:32:14 +00001824 c->verdict = -NF_ACCEPT - 1;
Rusty Russell79dee072000-05-02 16:45:16 +00001825 else if (strcmp(policy, LABEL_DROP) == 0)
Harald Welteaae69be2004-08-29 23:32:14 +00001826 c->verdict = -NF_DROP - 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00001827 else {
1828 errno = EINVAL;
1829 return 0;
1830 }
Harald Welte1cef74d2001-01-05 15:22:59 +00001831
Harald Welte1cef74d2001-01-05 15:22:59 +00001832 if (counters) {
1833 /* set byte and packet counters */
Harald Welteaae69be2004-08-29 23:32:14 +00001834 memcpy(&c->counters, counters, sizeof(STRUCT_COUNTERS));
1835 c->counter_map.maptype = COUNTER_MAP_SET;
Harald Welte1cef74d2001-01-05 15:22:59 +00001836 } else {
Harald Welteaae69be2004-08-29 23:32:14 +00001837 c->counter_map.maptype = COUNTER_MAP_NOMAP;
Harald Welte1cef74d2001-01-05 15:22:59 +00001838 }
1839
Rusty Russell175f6412000-03-24 09:32:20 +00001840 set_changed(*handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001841
Marc Bouchere6869a82000-03-20 06:03:29 +00001842 return 1;
1843}
1844
1845/* Without this, on gcc 2.7.2.3, we get:
Rusty Russell79dee072000-05-02 16:45:16 +00001846 libiptc.c: In function `TC_COMMIT':
Marc Bouchere6869a82000-03-20 06:03:29 +00001847 libiptc.c:833: fixed or forbidden register was spilled.
1848 This may be due to a compiler bug or to impossible asm
1849 statements or clauses.
1850*/
1851static void
Rusty Russell79dee072000-05-02 16:45:16 +00001852subtract_counters(STRUCT_COUNTERS *answer,
1853 const STRUCT_COUNTERS *a,
1854 const STRUCT_COUNTERS *b)
Marc Bouchere6869a82000-03-20 06:03:29 +00001855{
1856 answer->pcnt = a->pcnt - b->pcnt;
1857 answer->bcnt = a->bcnt - b->bcnt;
1858}
1859
Harald Welteaae69be2004-08-29 23:32:14 +00001860
1861static void counters_nomap(STRUCT_COUNTERS_INFO *newcounters,
1862 unsigned int index)
1863{
1864 newcounters->counters[index] = ((STRUCT_COUNTERS) { 0, 0});
1865 DEBUGP_C("NOMAP => zero\n");
1866}
1867
1868static void counters_normal_map(STRUCT_COUNTERS_INFO *newcounters,
1869 STRUCT_REPLACE *repl,
1870 unsigned int index,
1871 unsigned int mappos)
1872{
1873 /* Original read: X.
1874 * Atomic read on replacement: X + Y.
1875 * Currently in kernel: Z.
1876 * Want in kernel: X + Y + Z.
1877 * => Add in X + Y
1878 * => Add in replacement read.
1879 */
1880 newcounters->counters[index] = repl->counters[mappos];
1881 DEBUGP_C("NORMAL_MAP => mappos %u \n", mappos);
1882}
1883
1884static void counters_map_zeroed(STRUCT_COUNTERS_INFO *newcounters,
1885 STRUCT_REPLACE *repl,
1886 unsigned int index,
1887 unsigned int mappos,
1888 STRUCT_COUNTERS *counters)
1889{
1890 /* Original read: X.
1891 * Atomic read on replacement: X + Y.
1892 * Currently in kernel: Z.
1893 * Want in kernel: Y + Z.
1894 * => Add in Y.
1895 * => Add in (replacement read - original read).
1896 */
1897 subtract_counters(&newcounters->counters[index],
1898 &repl->counters[mappos],
1899 counters);
1900 DEBUGP_C("ZEROED => mappos %u\n", mappos);
1901}
1902
1903static void counters_map_set(STRUCT_COUNTERS_INFO *newcounters,
1904 unsigned int index,
1905 STRUCT_COUNTERS *counters)
1906{
1907 /* Want to set counter (iptables-restore) */
1908
1909 memcpy(&newcounters->counters[index], counters,
1910 sizeof(STRUCT_COUNTERS));
1911
1912 DEBUGP_C("SET\n");
1913}
1914
1915
Marc Bouchere6869a82000-03-20 06:03:29 +00001916int
Rusty Russell79dee072000-05-02 16:45:16 +00001917TC_COMMIT(TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001918{
1919 /* Replace, then map back the counters. */
Rusty Russell79dee072000-05-02 16:45:16 +00001920 STRUCT_REPLACE *repl;
1921 STRUCT_COUNTERS_INFO *newcounters;
Harald Welteaae69be2004-08-29 23:32:14 +00001922 struct chain_head *c;
1923 int ret;
Martin Josefsson841e4ae2003-05-02 15:30:11 +00001924 size_t counterlen;
Harald Welteaae69be2004-08-29 23:32:14 +00001925 int new_number;
1926 unsigned int new_size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001927
1928 CHECK(*handle);
Martin Josefsson841e4ae2003-05-02 15:30:11 +00001929
Marc Bouchere6869a82000-03-20 06:03:29 +00001930#if 0
Rusty Russell54c307e2000-09-04 06:47:28 +00001931 TC_DUMP_ENTRIES(*handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001932#endif
1933
1934 /* Don't commit if nothing changed. */
1935 if (!(*handle)->changed)
1936 goto finished;
1937
Harald Welteaae69be2004-08-29 23:32:14 +00001938 new_number = iptcc_compile_table_prep(*handle, &new_size);
1939 if (new_number < 0) {
1940 errno = ENOMEM;
1941 return 0;
1942 }
1943
1944 repl = malloc(sizeof(*repl) + new_size);
Marc Bouchere6869a82000-03-20 06:03:29 +00001945 if (!repl) {
1946 errno = ENOMEM;
1947 return 0;
1948 }
Harald Welteaae69be2004-08-29 23:32:14 +00001949 memset(repl, 0, sizeof(*repl));
1950
1951 counterlen = sizeof(STRUCT_COUNTERS_INFO)
1952 + sizeof(STRUCT_COUNTERS) * new_number;
Marc Bouchere6869a82000-03-20 06:03:29 +00001953
1954 /* These are the old counters we will get from kernel */
Rusty Russell79dee072000-05-02 16:45:16 +00001955 repl->counters = malloc(sizeof(STRUCT_COUNTERS)
Marc Bouchere6869a82000-03-20 06:03:29 +00001956 * (*handle)->info.num_entries);
1957 if (!repl->counters) {
1958 free(repl);
1959 errno = ENOMEM;
1960 return 0;
1961 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001962 /* These are the counters we're going to put back, later. */
1963 newcounters = malloc(counterlen);
1964 if (!newcounters) {
1965 free(repl->counters);
1966 free(repl);
1967 errno = ENOMEM;
1968 return 0;
1969 }
Harald Welteaae69be2004-08-29 23:32:14 +00001970 memset(newcounters, 0, counterlen);
Marc Bouchere6869a82000-03-20 06:03:29 +00001971
1972 strcpy(repl->name, (*handle)->info.name);
Harald Welteaae69be2004-08-29 23:32:14 +00001973 repl->num_entries = new_number;
1974 repl->size = new_size;
1975
Marc Bouchere6869a82000-03-20 06:03:29 +00001976 repl->num_counters = (*handle)->info.num_entries;
1977 repl->valid_hooks = (*handle)->info.valid_hooks;
Harald Welteaae69be2004-08-29 23:32:14 +00001978
1979 DEBUGP("num_entries=%u, size=%u, num_counters=%u\n",
1980 repl->num_entries, repl->size, repl->num_counters);
1981
1982 ret = iptcc_compile_table(*handle, repl);
1983 if (ret < 0) {
1984 errno = ret;
1985 free(repl->counters);
1986 free(repl);
1987 return 0;
1988 }
1989
1990
1991#ifdef IPTC_DEBUG2
1992 {
1993 int fd = open("/tmp/libiptc-so_set_replace.blob",
1994 O_CREAT|O_WRONLY);
1995 if (fd >= 0) {
1996 write(fd, repl, sizeof(*repl) + repl->size);
1997 close(fd);
1998 }
1999 }
2000#endif
Marc Bouchere6869a82000-03-20 06:03:29 +00002001
Rusty Russell79dee072000-05-02 16:45:16 +00002002 if (setsockopt(sockfd, TC_IPPROTO, SO_SET_REPLACE, repl,
Harald Welteaae69be2004-08-29 23:32:14 +00002003 sizeof(*repl) + repl->size) < 0) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002004 free(repl->counters);
2005 free(repl);
2006 free(newcounters);
2007 return 0;
2008 }
2009
2010 /* Put counters back. */
2011 strcpy(newcounters->name, (*handle)->info.name);
Harald Welteaae69be2004-08-29 23:32:14 +00002012 newcounters->num_counters = new_number;
Marc Bouchere6869a82000-03-20 06:03:29 +00002013
Harald Welteaae69be2004-08-29 23:32:14 +00002014 list_for_each_entry(c, &(*handle)->chains, list) {
2015 struct rule_head *r;
Marc Bouchere6869a82000-03-20 06:03:29 +00002016
Harald Welteaae69be2004-08-29 23:32:14 +00002017 /* Builtin chains have their own counters */
2018 if (iptcc_is_builtin(c)) {
2019 DEBUGP("counter for chain-index %u: ", c->foot_index);
2020 switch(c->counter_map.maptype) {
2021 case COUNTER_MAP_NOMAP:
2022 counters_nomap(newcounters, c->foot_index);
2023 break;
2024 case COUNTER_MAP_NORMAL_MAP:
2025 counters_normal_map(newcounters, repl,
2026 c->foot_index,
2027 c->counter_map.mappos);
2028 break;
2029 case COUNTER_MAP_ZEROED:
2030 counters_map_zeroed(newcounters, repl,
2031 c->foot_index,
2032 c->counter_map.mappos,
2033 &c->counters);
2034 break;
2035 case COUNTER_MAP_SET:
2036 counters_map_set(newcounters, c->foot_index,
2037 &c->counters);
2038 break;
2039 }
2040 }
Harald Welte1cef74d2001-01-05 15:22:59 +00002041
Harald Welteaae69be2004-08-29 23:32:14 +00002042 list_for_each_entry(r, &c->rules, list) {
2043 DEBUGP("counter for index %u: ", r->index);
2044 switch (r->counter_map.maptype) {
2045 case COUNTER_MAP_NOMAP:
2046 counters_nomap(newcounters, r->index);
2047 break;
Harald Welte1cef74d2001-01-05 15:22:59 +00002048
Harald Welteaae69be2004-08-29 23:32:14 +00002049 case COUNTER_MAP_NORMAL_MAP:
2050 counters_normal_map(newcounters, repl,
2051 r->index,
2052 r->counter_map.mappos);
2053 break;
Harald Welte1cef74d2001-01-05 15:22:59 +00002054
Harald Welteaae69be2004-08-29 23:32:14 +00002055 case COUNTER_MAP_ZEROED:
2056 counters_map_zeroed(newcounters, repl,
2057 r->index,
2058 r->counter_map.mappos,
2059 &r->entry->counters);
2060 break;
2061
2062 case COUNTER_MAP_SET:
2063 counters_map_set(newcounters, r->index,
2064 &r->entry->counters);
2065 break;
2066 }
Marc Bouchere6869a82000-03-20 06:03:29 +00002067 }
2068 }
Rusty Russell62527ce2000-09-04 09:45:54 +00002069
Harald Welteaae69be2004-08-29 23:32:14 +00002070
Rusty Russell62527ce2000-09-04 09:45:54 +00002071#ifdef KERNEL_64_USERSPACE_32
2072 {
2073 /* Kernel will think that pointer should be 64-bits, and get
2074 padding. So we accomodate here (assumption: alignment of
2075 `counters' is on 64-bit boundary). */
2076 u_int64_t *kernptr = (u_int64_t *)&newcounters->counters;
2077 if ((unsigned long)&newcounters->counters % 8 != 0) {
2078 fprintf(stderr,
2079 "counters alignment incorrect! Mail rusty!\n");
2080 abort();
2081 }
2082 *kernptr = newcounters->counters;
Rusty Russell54c307e2000-09-04 06:47:28 +00002083 }
Rusty Russell62527ce2000-09-04 09:45:54 +00002084#endif /* KERNEL_64_USERSPACE_32 */
Marc Bouchere6869a82000-03-20 06:03:29 +00002085
Harald Welteaae69be2004-08-29 23:32:14 +00002086#ifdef IPTC_DEBUG2
2087 {
2088 int fd = open("/tmp/libiptc-so_set_add_counters.blob",
2089 O_CREAT|O_WRONLY);
2090 if (fd >= 0) {
2091 write(fd, newcounters, counterlen);
2092 close(fd);
2093 }
2094 }
2095#endif
2096
Rusty Russell79dee072000-05-02 16:45:16 +00002097 if (setsockopt(sockfd, TC_IPPROTO, SO_SET_ADD_COUNTERS,
2098 newcounters, counterlen) < 0) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002099 free(repl->counters);
2100 free(repl);
2101 free(newcounters);
2102 return 0;
2103 }
2104
2105 free(repl->counters);
2106 free(repl);
2107 free(newcounters);
2108
2109 finished:
Martin Josefsson841e4ae2003-05-02 15:30:11 +00002110 TC_FREE(handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00002111 return 1;
2112}
2113
2114/* Get raw socket. */
2115int
Rusty Russell79dee072000-05-02 16:45:16 +00002116TC_GET_RAW_SOCKET()
Marc Bouchere6869a82000-03-20 06:03:29 +00002117{
2118 return sockfd;
2119}
2120
2121/* Translates errno numbers into more human-readable form than strerror. */
2122const char *
Rusty Russell79dee072000-05-02 16:45:16 +00002123TC_STRERROR(int err)
Marc Bouchere6869a82000-03-20 06:03:29 +00002124{
2125 unsigned int i;
2126 struct table_struct {
2127 void *fn;
2128 int err;
2129 const char *message;
2130 } table [] =
Harald Welte4ccfa632001-07-30 15:12:43 +00002131 { { TC_INIT, EPERM, "Permission denied (you must be root)" },
Rusty Russell79dee072000-05-02 16:45:16 +00002132 { TC_INIT, EINVAL, "Module is wrong version" },
Harald Welte4ccfa632001-07-30 15:12:43 +00002133 { TC_INIT, ENOENT,
2134 "Table does not exist (do you need to insmod?)" },
Rusty Russell79dee072000-05-02 16:45:16 +00002135 { TC_DELETE_CHAIN, ENOTEMPTY, "Chain is not empty" },
2136 { TC_DELETE_CHAIN, EINVAL, "Can't delete built-in chain" },
2137 { TC_DELETE_CHAIN, EMLINK,
Marc Bouchere6869a82000-03-20 06:03:29 +00002138 "Can't delete chain with references left" },
Rusty Russell79dee072000-05-02 16:45:16 +00002139 { TC_CREATE_CHAIN, EEXIST, "Chain already exists" },
2140 { TC_INSERT_ENTRY, E2BIG, "Index of insertion too big" },
2141 { TC_REPLACE_ENTRY, E2BIG, "Index of replacement too big" },
2142 { TC_DELETE_NUM_ENTRY, E2BIG, "Index of deletion too big" },
Harald Welte1cef74d2001-01-05 15:22:59 +00002143 { TC_READ_COUNTER, E2BIG, "Index of counter too big" },
2144 { TC_ZERO_COUNTER, E2BIG, "Index of counter too big" },
Rusty Russell79dee072000-05-02 16:45:16 +00002145 { TC_INSERT_ENTRY, ELOOP, "Loop found in table" },
2146 { TC_INSERT_ENTRY, EINVAL, "Target problem" },
Marc Bouchere6869a82000-03-20 06:03:29 +00002147 /* EINVAL for CHECK probably means bad interface. */
Rusty Russell79dee072000-05-02 16:45:16 +00002148 { TC_CHECK_PACKET, EINVAL,
Marc Boucherc8264992000-04-22 22:34:44 +00002149 "Bad arguments (does that interface exist?)" },
Harald Welte4ccfa632001-07-30 15:12:43 +00002150 { TC_CHECK_PACKET, ENOSYS,
2151 "Checking will most likely never get implemented" },
Marc Bouchere6869a82000-03-20 06:03:29 +00002152 /* ENOENT for DELETE probably means no matching rule */
Rusty Russell79dee072000-05-02 16:45:16 +00002153 { TC_DELETE_ENTRY, ENOENT,
Marc Boucherc8264992000-04-22 22:34:44 +00002154 "Bad rule (does a matching rule exist in that chain?)" },
Rusty Russell79dee072000-05-02 16:45:16 +00002155 { TC_SET_POLICY, ENOENT,
Marc Boucherc8264992000-04-22 22:34:44 +00002156 "Bad built-in chain name" },
Rusty Russell79dee072000-05-02 16:45:16 +00002157 { TC_SET_POLICY, EINVAL,
Marc Boucherc8264992000-04-22 22:34:44 +00002158 "Bad policy name" },
Harald Welte4ccfa632001-07-30 15:12:43 +00002159
2160 { NULL, 0, "Incompatible with this kernel" },
2161 { NULL, ENOPROTOOPT, "iptables who? (do you need to insmod?)" },
2162 { NULL, ENOSYS, "Will be implemented real soon. I promise ;)" },
2163 { NULL, ENOMEM, "Memory allocation problem" },
2164 { NULL, ENOENT, "No chain/target/match by that name" },
Marc Bouchere6869a82000-03-20 06:03:29 +00002165 };
2166
2167 for (i = 0; i < sizeof(table)/sizeof(struct table_struct); i++) {
2168 if ((!table[i].fn || table[i].fn == iptc_fn)
2169 && table[i].err == err)
2170 return table[i].message;
2171 }
2172
2173 return strerror(err);
2174}