blob: dfa2d1f097b574201bfd011b4d9e1cebc2a57c08 [file] [log] [blame]
Martin Josefsson0f9b8b12004-12-18 17:18:49 +00001/* Library which manipulates firewall rules. Version $Revision$ */
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
47static int sockfd = -1;
48static void *iptc_fn = NULL;
49
50static const char *hooknames[]
Rusty Russell79dee072000-05-02 16:45:16 +000051= { [HOOK_PRE_ROUTING] "PREROUTING",
52 [HOOK_LOCAL_IN] "INPUT",
53 [HOOK_FORWARD] "FORWARD",
54 [HOOK_LOCAL_OUT] "OUTPUT",
Rusty Russell10758b72000-09-14 07:37:33 +000055 [HOOK_POST_ROUTING] "POSTROUTING",
56#ifdef HOOK_DROPPING
57 [HOOK_DROPPING] "DROPPING"
58#endif
Marc Bouchere6869a82000-03-20 06:03:29 +000059};
60
Harald Welteaae69be2004-08-29 23:32:14 +000061/* Convenience structures */
62struct ipt_error_target
63{
64 STRUCT_ENTRY_TARGET t;
65 char error[TABLE_MAXNAMELEN];
66};
67
68struct chain_head;
69struct rule_head;
70
Marc Bouchere6869a82000-03-20 06:03:29 +000071struct counter_map
72{
73 enum {
74 COUNTER_MAP_NOMAP,
75 COUNTER_MAP_NORMAL_MAP,
Harald Welte1cef74d2001-01-05 15:22:59 +000076 COUNTER_MAP_ZEROED,
77 COUNTER_MAP_SET
Marc Bouchere6869a82000-03-20 06:03:29 +000078 } maptype;
79 unsigned int mappos;
80};
81
Harald Welteaae69be2004-08-29 23:32:14 +000082enum iptcc_rule_type {
83 IPTCC_R_STANDARD, /* standard target (ACCEPT, ...) */
84 IPTCC_R_MODULE, /* extension module (SNAT, ...) */
85 IPTCC_R_FALLTHROUGH, /* fallthrough rule */
86 IPTCC_R_JUMP, /* jump to other chain */
Marc Bouchere6869a82000-03-20 06:03:29 +000087};
88
Harald Welteaae69be2004-08-29 23:32:14 +000089struct rule_head
Rusty Russell30fd6e52000-04-23 09:16:06 +000090{
Harald Welteaae69be2004-08-29 23:32:14 +000091 struct list_head list;
92 struct chain_head *chain;
93 struct counter_map counter_map;
94
95 unsigned int index; /* index (needed for counter_map) */
96 unsigned int offset; /* offset in rule blob */
97
98 enum iptcc_rule_type type;
99 struct chain_head *jump; /* jump target, if IPTCC_R_JUMP */
100
101 unsigned int size; /* size of entry data */
102 STRUCT_ENTRY entry[0];
103};
104
105struct chain_head
106{
107 struct list_head list;
Rusty Russell79dee072000-05-02 16:45:16 +0000108 char name[TABLE_MAXNAMELEN];
Harald Welteaae69be2004-08-29 23:32:14 +0000109 unsigned int hooknum; /* hook number+1 if builtin */
110 unsigned int references; /* how many jumps reference us */
111 int verdict; /* verdict if builtin */
112
113 STRUCT_COUNTERS counters; /* per-chain counters */
114 struct counter_map counter_map;
115
116 unsigned int num_rules; /* number of rules in list */
117 struct list_head rules; /* list of rules */
118
119 unsigned int index; /* index (needed for jump resolval) */
120 unsigned int head_offset; /* offset in rule blob */
121 unsigned int foot_index; /* index (needed for counter_map) */
122 unsigned int foot_offset; /* offset in rule blob */
Rusty Russell30fd6e52000-04-23 09:16:06 +0000123};
124
Rusty Russell79dee072000-05-02 16:45:16 +0000125STRUCT_TC_HANDLE
Marc Bouchere6869a82000-03-20 06:03:29 +0000126{
Harald Welteaae69be2004-08-29 23:32:14 +0000127 int changed; /* Have changes been made? */
128
129 struct list_head chains;
130
131 struct chain_head *chain_iterator_cur;
132 struct rule_head *rule_iterator_cur;
133
Rusty Russell79dee072000-05-02 16:45:16 +0000134 STRUCT_GETINFO info;
Harald Welteaae69be2004-08-29 23:32:14 +0000135 STRUCT_GET_ENTRIES *entries;
Marc Bouchere6869a82000-03-20 06:03:29 +0000136};
137
Harald Welteaae69be2004-08-29 23:32:14 +0000138/* allocate a new chain head for the cache */
139static struct chain_head *iptcc_alloc_chain_head(const char *name, int hooknum)
140{
141 struct chain_head *c = malloc(sizeof(*c));
142 if (!c)
143 return NULL;
144 memset(c, 0, sizeof(*c));
145
146 strncpy(c->name, name, TABLE_MAXNAMELEN);
147 c->hooknum = hooknum;
148 INIT_LIST_HEAD(&c->rules);
149
150 return c;
151}
152
153/* allocate and initialize a new rule for the cache */
154static struct rule_head *iptcc_alloc_rule(struct chain_head *c, unsigned int size)
155{
156 struct rule_head *r = malloc(sizeof(*r)+size);
157 if (!r)
158 return NULL;
159 memset(r, 0, sizeof(*r));
160
161 r->chain = c;
162 r->size = size;
163
164 return r;
165}
166
167/* notify us that the ruleset has been modified by the user */
Rusty Russell175f6412000-03-24 09:32:20 +0000168static void
Rusty Russell79dee072000-05-02 16:45:16 +0000169set_changed(TC_HANDLE_T h)
Rusty Russell175f6412000-03-24 09:32:20 +0000170{
Rusty Russell175f6412000-03-24 09:32:20 +0000171 h->changed = 1;
172}
173
Harald Welte380ba5f2002-02-13 16:19:55 +0000174#ifdef IPTC_DEBUG
Rusty Russell79dee072000-05-02 16:45:16 +0000175static void do_check(TC_HANDLE_T h, unsigned int line);
Rusty Russell849779c2000-04-23 15:51:51 +0000176#define CHECK(h) do { if (!getenv("IPTC_NO_CHECK")) do_check((h), __LINE__); } while(0)
Rusty Russell30fd6e52000-04-23 09:16:06 +0000177#else
178#define CHECK(h)
179#endif
Marc Bouchere6869a82000-03-20 06:03:29 +0000180
Harald Welteaae69be2004-08-29 23:32:14 +0000181
182/**********************************************************************
183 * iptc blob utility functions (iptcb_*)
184 **********************************************************************/
185
Marc Bouchere6869a82000-03-20 06:03:29 +0000186static inline int
Harald Welteaae69be2004-08-29 23:32:14 +0000187iptcb_get_number(const STRUCT_ENTRY *i,
Rusty Russell79dee072000-05-02 16:45:16 +0000188 const STRUCT_ENTRY *seek,
Marc Bouchere6869a82000-03-20 06:03:29 +0000189 unsigned int *pos)
190{
191 if (i == seek)
192 return 1;
193 (*pos)++;
194 return 0;
195}
196
Marc Bouchere6869a82000-03-20 06:03:29 +0000197static inline int
Harald Welteaae69be2004-08-29 23:32:14 +0000198iptcb_get_entry_n(STRUCT_ENTRY *i,
Marc Bouchere6869a82000-03-20 06:03:29 +0000199 unsigned int number,
200 unsigned int *pos,
Rusty Russell79dee072000-05-02 16:45:16 +0000201 STRUCT_ENTRY **pe)
Marc Bouchere6869a82000-03-20 06:03:29 +0000202{
203 if (*pos == number) {
204 *pe = i;
205 return 1;
206 }
207 (*pos)++;
208 return 0;
209}
210
Harald Welteaae69be2004-08-29 23:32:14 +0000211static inline STRUCT_ENTRY *
212iptcb_get_entry(TC_HANDLE_T h, unsigned int offset)
213{
214 return (STRUCT_ENTRY *)((char *)h->entries->entrytable + offset);
215}
216
217static unsigned int
218iptcb_entry2index(const TC_HANDLE_T h, const STRUCT_ENTRY *seek)
Marc Bouchere6869a82000-03-20 06:03:29 +0000219{
220 unsigned int pos = 0;
Marc Bouchere6869a82000-03-20 06:03:29 +0000221
Harald Welteaae69be2004-08-29 23:32:14 +0000222 if (ENTRY_ITERATE(h->entries->entrytable, h->entries->size,
223 iptcb_get_number, seek, &pos) == 0) {
224 fprintf(stderr, "ERROR: offset %u not an entry!\n",
225 (unsigned int)((char *)seek - (char *)h->entries->entrytable));
226 abort();
227 }
228 return pos;
Marc Bouchere6869a82000-03-20 06:03:29 +0000229}
230
Harald Welte0113fe72004-01-06 19:04:02 +0000231static inline STRUCT_ENTRY *
Harald Welteaae69be2004-08-29 23:32:14 +0000232iptcb_offset2entry(TC_HANDLE_T h, unsigned int offset)
Harald Welte0113fe72004-01-06 19:04:02 +0000233{
Harald Welteaae69be2004-08-29 23:32:14 +0000234 return (STRUCT_ENTRY *) ((void *)h->entries->entrytable+offset);
Harald Welte0113fe72004-01-06 19:04:02 +0000235}
236
Harald Welteaae69be2004-08-29 23:32:14 +0000237
Harald Welte0113fe72004-01-06 19:04:02 +0000238static inline unsigned long
Harald Welteaae69be2004-08-29 23:32:14 +0000239iptcb_entry2offset(const TC_HANDLE_T h, const STRUCT_ENTRY *e)
Harald Welte0113fe72004-01-06 19:04:02 +0000240{
Harald Welteaae69be2004-08-29 23:32:14 +0000241 return (void *)e - (void *)h->entries->entrytable;
Harald Welte3ea8f402003-06-23 18:25:59 +0000242}
243
244static inline unsigned int
Harald Welteaae69be2004-08-29 23:32:14 +0000245iptcb_offset2index(const TC_HANDLE_T h, unsigned int offset)
Harald Welte3ea8f402003-06-23 18:25:59 +0000246{
Harald Welteaae69be2004-08-29 23:32:14 +0000247 return iptcb_entry2index(h, iptcb_offset2entry(h, offset));
248}
249
250/* Returns 0 if not hook entry, else hooknumber + 1 */
251static inline unsigned int
252iptcb_ent_is_hook_entry(STRUCT_ENTRY *e, TC_HANDLE_T h)
253{
254 unsigned int i;
255
256 for (i = 0; i < NUMHOOKS; i++) {
257 if ((h->info.valid_hooks & (1 << i))
258 && iptcb_get_entry(h, h->info.hook_entry[i]) == e)
259 return i+1;
260 }
261 return 0;
Harald Welte3ea8f402003-06-23 18:25:59 +0000262}
263
264
Harald Welteaae69be2004-08-29 23:32:14 +0000265/**********************************************************************
266 * iptc cache utility functions (iptcc_*)
267 **********************************************************************/
Harald Welte0113fe72004-01-06 19:04:02 +0000268
Harald Welteaae69be2004-08-29 23:32:14 +0000269/* Is the given chain builtin (1) or user-defined (0) */
270static unsigned int iptcc_is_builtin(struct chain_head *c)
271{
272 return (c->hooknum ? 1 : 0);
273}
274
275/* Get a specific rule within a chain */
276static struct rule_head *iptcc_get_rule_num(struct chain_head *c,
277 unsigned int rulenum)
278{
279 struct rule_head *r;
280 unsigned int num = 0;
281
282 list_for_each_entry(r, &c->rules, list) {
283 num++;
284 if (num == rulenum)
285 return r;
286 }
287 return NULL;
288}
289
Martin Josefssona5616dc2004-10-24 22:27:31 +0000290/* Get a specific rule within a chain backwards */
291static struct rule_head *iptcc_get_rule_num_reverse(struct chain_head *c,
292 unsigned int rulenum)
293{
294 struct rule_head *r;
295 unsigned int num = 0;
296
297 list_for_each_entry_reverse(r, &c->rules, list) {
298 num++;
299 if (num == rulenum)
300 return r;
301 }
302 return NULL;
303}
304
Harald Welteaae69be2004-08-29 23:32:14 +0000305/* Returns chain head if found, otherwise NULL. */
306static struct chain_head *
307iptcc_find_chain_by_offset(TC_HANDLE_T handle, unsigned int offset)
308{
309 struct list_head *pos;
310
311 if (list_empty(&handle->chains))
312 return NULL;
313
314 list_for_each(pos, &handle->chains) {
315 struct chain_head *c = list_entry(pos, struct chain_head, list);
316 if (offset >= c->head_offset && offset <= c->foot_offset)
317 return c;
Harald Welte0113fe72004-01-06 19:04:02 +0000318 }
319
Harald Welteaae69be2004-08-29 23:32:14 +0000320 return NULL;
Harald Welte0113fe72004-01-06 19:04:02 +0000321}
Harald Welteaae69be2004-08-29 23:32:14 +0000322/* Returns chain head if found, otherwise NULL. */
323static struct chain_head *
324iptcc_find_label(const char *name, TC_HANDLE_T handle)
325{
326 struct list_head *pos;
327
328 if (list_empty(&handle->chains))
329 return NULL;
330
331 list_for_each(pos, &handle->chains) {
332 struct chain_head *c = list_entry(pos, struct chain_head, list);
333 if (!strcmp(c->name, name))
334 return c;
335 }
336
337 return NULL;
338}
339
340/* called when rule is to be removed from cache */
341static void iptcc_delete_rule(struct rule_head *r)
342{
343 DEBUGP("deleting rule %p (offset %u)\n", r, r->offset);
344 /* clean up reference count of called chain */
345 if (r->type == IPTCC_R_JUMP
346 && r->jump)
347 r->jump->references--;
348
349 list_del(&r->list);
350 free(r);
351}
352
353
354/**********************************************************************
355 * RULESET PARSER (blob -> cache)
356 **********************************************************************/
357
358static int alphasort(const void *a, const void *b)
359{
360 return strcmp(((struct chain_head *)a)->name,
361 ((struct chain_head *)b)->name);
362}
363
364/* Delete policy rule of previous chain, since cache doesn't contain
365 * chain policy rules.
366 * WARNING: This function has ugly design and relies on a lot of context, only
367 * to be called from specific places within the parser */
368static int __iptcc_p_del_policy(TC_HANDLE_T h, unsigned int num)
369{
370 if (h->chain_iterator_cur) {
371 /* policy rule is last rule */
372 struct rule_head *pr = (struct rule_head *)
373 h->chain_iterator_cur->rules.prev;
374
375 /* save verdict */
376 h->chain_iterator_cur->verdict =
377 *(int *)GET_TARGET(pr->entry)->data;
378
379 /* save counter and counter_map information */
380 h->chain_iterator_cur->counter_map.maptype =
381 COUNTER_MAP_NORMAL_MAP;
382 h->chain_iterator_cur->counter_map.mappos = num-1;
383 memcpy(&h->chain_iterator_cur->counters, &pr->entry->counters,
384 sizeof(h->chain_iterator_cur->counters));
385
386 /* foot_offset points to verdict rule */
387 h->chain_iterator_cur->foot_index = num;
388 h->chain_iterator_cur->foot_offset = pr->offset;
389
390 /* delete rule from cache */
391 iptcc_delete_rule(pr);
Martin Josefsson8d1b38a2004-09-22 21:00:19 +0000392 h->chain_iterator_cur->num_rules--;
Harald Welteaae69be2004-08-29 23:32:14 +0000393
394 return 1;
395 }
396 return 0;
397}
398
399/* Another ugly helper function split out of cache_add_entry to make it less
400 * spaghetti code */
401static void __iptcc_p_add_chain(TC_HANDLE_T h, struct chain_head *c,
402 unsigned int offset, unsigned int *num)
403{
404 __iptcc_p_del_policy(h, *num);
405
406 c->head_offset = offset;
407 c->index = *num;
408
409 list_add_tail(&c->list, &h->chains);
410 h->chain_iterator_cur = c;
411}
412
413/* main parser function: add an entry from the blob to the cache */
414static int cache_add_entry(STRUCT_ENTRY *e,
415 TC_HANDLE_T h,
416 STRUCT_ENTRY **prev,
417 unsigned int *num)
418{
419 unsigned int builtin;
420 unsigned int offset = (char *)e - (char *)h->entries->entrytable;
421
422 DEBUGP("entering...");
423
424 /* Last entry ("policy rule"). End it.*/
425 if (iptcb_entry2offset(h,e) + e->next_offset == h->entries->size) {
426 /* This is the ERROR node at the end of the chain */
427 DEBUGP_C("%u:%u: end of table:\n", *num, offset);
428
429 __iptcc_p_del_policy(h, *num);
430
431 h->chain_iterator_cur = NULL;
432 goto out_inc;
433 }
434
435 /* We know this is the start of a new chain if it's an ERROR
436 * target, or a hook entry point */
437
438 if (strcmp(GET_TARGET(e)->u.user.name, ERROR_TARGET) == 0) {
439 struct chain_head *c =
440 iptcc_alloc_chain_head((const char *)GET_TARGET(e)->data, 0);
441 DEBUGP_C("%u:%u:new userdefined chain %s: %p\n", *num, offset,
442 (char *)c->name, c);
443 if (!c) {
444 errno = -ENOMEM;
445 return -1;
446 }
447
448 __iptcc_p_add_chain(h, c, offset, num);
449
450 } else if ((builtin = iptcb_ent_is_hook_entry(e, h)) != 0) {
451 struct chain_head *c =
452 iptcc_alloc_chain_head((char *)hooknames[builtin-1],
453 builtin);
454 DEBUGP_C("%u:%u new builtin chain: %p (rules=%p)\n",
455 *num, offset, c, &c->rules);
456 if (!c) {
457 errno = -ENOMEM;
458 return -1;
459 }
460
461 c->hooknum = builtin;
462
463 __iptcc_p_add_chain(h, c, offset, num);
464
465 /* FIXME: this is ugly. */
466 goto new_rule;
467 } else {
468 /* has to be normal rule */
469 struct rule_head *r;
470new_rule:
471
472 if (!(r = iptcc_alloc_rule(h->chain_iterator_cur,
473 e->next_offset))) {
474 errno = ENOMEM;
475 return -1;
476 }
477 DEBUGP_C("%u:%u normal rule: %p: ", *num, offset, r);
478
479 r->index = *num;
480 r->offset = offset;
481 memcpy(r->entry, e, e->next_offset);
482 r->counter_map.maptype = COUNTER_MAP_NORMAL_MAP;
483 r->counter_map.mappos = r->index;
484
485 /* handling of jumps, etc. */
486 if (!strcmp(GET_TARGET(e)->u.user.name, STANDARD_TARGET)) {
487 STRUCT_STANDARD_TARGET *t;
488
489 t = (STRUCT_STANDARD_TARGET *)GET_TARGET(e);
490 if (t->target.u.target_size
491 != ALIGN(sizeof(STRUCT_STANDARD_TARGET))) {
492 errno = EINVAL;
493 return -1;
494 }
495
496 if (t->verdict < 0) {
497 DEBUGP_C("standard, verdict=%d\n", t->verdict);
498 r->type = IPTCC_R_STANDARD;
499 } else if (t->verdict == r->offset+e->next_offset) {
500 DEBUGP_C("fallthrough\n");
501 r->type = IPTCC_R_FALLTHROUGH;
502 } else {
503 DEBUGP_C("jump, target=%u\n", t->verdict);
504 r->type = IPTCC_R_JUMP;
505 /* Jump target fixup has to be deferred
506 * until second pass, since we migh not
507 * yet have parsed the target */
508 }
Martin Josefsson52c38022004-09-22 19:39:40 +0000509 } else {
510 DEBUGP_C("module, target=%s\n", GET_TARGET(e)->u.user.name);
511 r->type = IPTCC_R_MODULE;
Harald Welteaae69be2004-08-29 23:32:14 +0000512 }
513
514 list_add_tail(&r->list, &h->chain_iterator_cur->rules);
Martin Josefsson8d1b38a2004-09-22 21:00:19 +0000515 h->chain_iterator_cur->num_rules++;
Harald Welteaae69be2004-08-29 23:32:14 +0000516 }
517out_inc:
518 (*num)++;
519 return 0;
520}
521
522
523/* parse an iptables blob into it's pieces */
524static int parse_table(TC_HANDLE_T h)
525{
526 STRUCT_ENTRY *prev;
527 unsigned int num = 0;
528 struct chain_head *c;
529
530 /* First pass: over ruleset blob */
531 ENTRY_ITERATE(h->entries->entrytable, h->entries->size,
532 cache_add_entry, h, &prev, &num);
533
534 /* Second pass: fixup parsed data from first pass */
535 list_for_each_entry(c, &h->chains, list) {
536 struct rule_head *r;
537 list_for_each_entry(r, &c->rules, list) {
538 struct chain_head *c;
539 STRUCT_STANDARD_TARGET *t;
540
541 if (r->type != IPTCC_R_JUMP)
542 continue;
543
544 t = (STRUCT_STANDARD_TARGET *)GET_TARGET(r->entry);
545 c = iptcc_find_chain_by_offset(h, t->verdict);
546 if (!c)
547 return -1;
548 r->jump = c;
549 c->references++;
550 }
551 }
552
553 /* FIXME: sort chains */
554
555 return 1;
556}
557
558
559/**********************************************************************
560 * RULESET COMPILATION (cache -> blob)
561 **********************************************************************/
562
563/* Convenience structures */
564struct iptcb_chain_start{
565 STRUCT_ENTRY e;
566 struct ipt_error_target name;
567};
568#define IPTCB_CHAIN_START_SIZE (sizeof(STRUCT_ENTRY) + \
569 ALIGN(sizeof(struct ipt_error_target)))
570
571struct iptcb_chain_foot {
572 STRUCT_ENTRY e;
573 STRUCT_STANDARD_TARGET target;
574};
575#define IPTCB_CHAIN_FOOT_SIZE (sizeof(STRUCT_ENTRY) + \
576 ALIGN(sizeof(STRUCT_STANDARD_TARGET)))
577
578struct iptcb_chain_error {
579 STRUCT_ENTRY entry;
580 struct ipt_error_target target;
581};
582#define IPTCB_CHAIN_ERROR_SIZE (sizeof(STRUCT_ENTRY) + \
583 ALIGN(sizeof(struct ipt_error_target)))
584
585
586
587/* compile rule from cache into blob */
588static inline int iptcc_compile_rule (TC_HANDLE_T h, STRUCT_REPLACE *repl, struct rule_head *r)
589{
590 /* handle jumps */
591 if (r->type == IPTCC_R_JUMP) {
592 STRUCT_STANDARD_TARGET *t;
593 t = (STRUCT_STANDARD_TARGET *)GET_TARGET(r->entry);
594 /* memset for memcmp convenience on delete/replace */
595 memset(t->target.u.user.name, 0, FUNCTION_MAXNAMELEN);
596 strcpy(t->target.u.user.name, STANDARD_TARGET);
597 /* Jumps can only happen to builtin chains, so we
598 * can safely assume that they always have a header */
599 t->verdict = r->jump->head_offset + IPTCB_CHAIN_START_SIZE;
600 } else if (r->type == IPTCC_R_FALLTHROUGH) {
601 STRUCT_STANDARD_TARGET *t;
602 t = (STRUCT_STANDARD_TARGET *)GET_TARGET(r->entry);
603 t->verdict = r->offset + r->size;
604 }
605
606 /* copy entry from cache to blob */
607 memcpy((char *)repl->entries+r->offset, r->entry, r->size);
608
609 return 1;
610}
611
612/* compile chain from cache into blob */
613static int iptcc_compile_chain(TC_HANDLE_T h, STRUCT_REPLACE *repl, struct chain_head *c)
614{
615 int ret;
616 struct rule_head *r;
617 struct iptcb_chain_start *head;
618 struct iptcb_chain_foot *foot;
619
620 /* only user-defined chains have heaer */
621 if (!iptcc_is_builtin(c)) {
622 /* put chain header in place */
623 head = (void *)repl->entries + c->head_offset;
624 head->e.target_offset = sizeof(STRUCT_ENTRY);
625 head->e.next_offset = IPTCB_CHAIN_START_SIZE;
626 strcpy(head->name.t.u.user.name, ERROR_TARGET);
627 head->name.t.u.target_size =
628 ALIGN(sizeof(struct ipt_error_target));
629 strcpy(head->name.error, c->name);
630 } else {
631 repl->hook_entry[c->hooknum-1] = c->head_offset;
632 repl->underflow[c->hooknum-1] = c->foot_offset;
633 }
634
635 /* iterate over rules */
636 list_for_each_entry(r, &c->rules, list) {
637 ret = iptcc_compile_rule(h, repl, r);
638 if (ret < 0)
639 return ret;
640 }
641
642 /* put chain footer in place */
643 foot = (void *)repl->entries + c->foot_offset;
644 foot->e.target_offset = sizeof(STRUCT_ENTRY);
645 foot->e.next_offset = IPTCB_CHAIN_FOOT_SIZE;
646 strcpy(foot->target.target.u.user.name, STANDARD_TARGET);
647 foot->target.target.u.target_size =
648 ALIGN(sizeof(STRUCT_STANDARD_TARGET));
649 /* builtin targets have verdict, others return */
650 if (iptcc_is_builtin(c))
651 foot->target.verdict = c->verdict;
652 else
653 foot->target.verdict = RETURN;
654 /* set policy-counters */
655 memcpy(&foot->e.counters, &c->counters, sizeof(STRUCT_COUNTERS));
656
657 return 0;
658}
659
660/* calculate offset and number for every rule in the cache */
661static int iptcc_compile_chain_offsets(TC_HANDLE_T h, struct chain_head *c,
662 int *offset, int *num)
663{
664 struct rule_head *r;
665
666 c->head_offset = *offset;
667 DEBUGP("%s: chain_head %u, offset=%u\n", c->name, *num, *offset);
668
669 if (!iptcc_is_builtin(c)) {
670 /* Chain has header */
671 *offset += sizeof(STRUCT_ENTRY)
672 + ALIGN(sizeof(struct ipt_error_target));
673 (*num)++;
674 }
675
676 list_for_each_entry(r, &c->rules, list) {
677 DEBUGP("rule %u, offset=%u, index=%u\n", *num, *offset, *num);
678 r->offset = *offset;
679 r->index = *num;
680 *offset += r->size;
681 (*num)++;
682 }
683
684 DEBUGP("%s; chain_foot %u, offset=%u, index=%u\n", c->name, *num,
685 *offset, *num);
686 c->foot_offset = *offset;
687 c->foot_index = *num;
688 *offset += sizeof(STRUCT_ENTRY)
689 + ALIGN(sizeof(STRUCT_STANDARD_TARGET));
690 (*num)++;
691
692 return 1;
693}
694
695/* put the pieces back together again */
696static int iptcc_compile_table_prep(TC_HANDLE_T h, unsigned int *size)
697{
698 struct chain_head *c;
699 unsigned int offset = 0, num = 0;
700 int ret = 0;
701
702 /* First pass: calculate offset for every rule */
703 list_for_each_entry(c, &h->chains, list) {
704 ret = iptcc_compile_chain_offsets(h, c, &offset, &num);
705 if (ret < 0)
706 return ret;
707 }
708
709 /* Append one error rule at end of chain */
710 num++;
711 offset += sizeof(STRUCT_ENTRY)
712 + ALIGN(sizeof(struct ipt_error_target));
713
714 /* ruleset size is now in offset */
715 *size = offset;
716 return num;
717}
718
719static int iptcc_compile_table(TC_HANDLE_T h, STRUCT_REPLACE *repl)
720{
721 struct chain_head *c;
722 struct iptcb_chain_error *error;
723
724 /* Second pass: copy from cache to offsets, fill in jumps */
725 list_for_each_entry(c, &h->chains, list) {
726 int ret = iptcc_compile_chain(h, repl, c);
727 if (ret < 0)
728 return ret;
729 }
730
731 /* Append error rule at end of chain */
732 error = (void *)repl->entries + repl->size - IPTCB_CHAIN_ERROR_SIZE;
733 error->entry.target_offset = sizeof(STRUCT_ENTRY);
734 error->entry.next_offset = IPTCB_CHAIN_ERROR_SIZE;
735 error->target.t.u.user.target_size =
736 ALIGN(sizeof(struct ipt_error_target));
737 strcpy((char *)&error->target.t.u.user.name, ERROR_TARGET);
738 strcpy((char *)&error->target.error, "ERROR");
739
740 return 1;
741}
742
743/**********************************************************************
744 * EXTERNAL API (operates on cache only)
745 **********************************************************************/
Marc Bouchere6869a82000-03-20 06:03:29 +0000746
747/* Allocate handle of given size */
Rusty Russell79dee072000-05-02 16:45:16 +0000748static TC_HANDLE_T
Harald Welte0113fe72004-01-06 19:04:02 +0000749alloc_handle(const char *tablename, unsigned int size, unsigned int num_rules)
Marc Bouchere6869a82000-03-20 06:03:29 +0000750{
751 size_t len;
Rusty Russell79dee072000-05-02 16:45:16 +0000752 TC_HANDLE_T h;
Marc Bouchere6869a82000-03-20 06:03:29 +0000753
Harald Welteaae69be2004-08-29 23:32:14 +0000754 len = sizeof(STRUCT_TC_HANDLE) + size;
Marc Bouchere6869a82000-03-20 06:03:29 +0000755
Harald Welteaae69be2004-08-29 23:32:14 +0000756 h = malloc(sizeof(STRUCT_TC_HANDLE));
757 if (!h) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000758 errno = ENOMEM;
759 return NULL;
760 }
Harald Welteaae69be2004-08-29 23:32:14 +0000761 memset(h, 0, sizeof(*h));
762 INIT_LIST_HEAD(&h->chains);
Marc Bouchere6869a82000-03-20 06:03:29 +0000763 strcpy(h->info.name, tablename);
Harald Welteaae69be2004-08-29 23:32:14 +0000764
Harald Welte0371c0c2004-09-19 21:00:12 +0000765 h->entries = malloc(sizeof(STRUCT_GET_ENTRIES) + size);
Harald Welteaae69be2004-08-29 23:32:14 +0000766 if (!h->entries)
767 goto out_free_handle;
768
769 strcpy(h->entries->name, tablename);
Harald Welte0371c0c2004-09-19 21:00:12 +0000770 h->entries->size = size;
Marc Bouchere6869a82000-03-20 06:03:29 +0000771
772 return h;
Harald Welteaae69be2004-08-29 23:32:14 +0000773
774out_free_handle:
775 free(h);
776
777 return NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +0000778}
779
Harald Welteaae69be2004-08-29 23:32:14 +0000780
Rusty Russell79dee072000-05-02 16:45:16 +0000781TC_HANDLE_T
782TC_INIT(const char *tablename)
Marc Bouchere6869a82000-03-20 06:03:29 +0000783{
Rusty Russell79dee072000-05-02 16:45:16 +0000784 TC_HANDLE_T h;
785 STRUCT_GETINFO info;
Marc Bouchere6869a82000-03-20 06:03:29 +0000786 int tmp;
787 socklen_t s;
788
Rusty Russell79dee072000-05-02 16:45:16 +0000789 iptc_fn = TC_INIT;
Marc Bouchere6869a82000-03-20 06:03:29 +0000790
Martin Josefssone560fd62003-06-13 16:56:51 +0000791 if (sockfd != -1) {
Harald Welte366454b2002-01-07 13:46:50 +0000792 close(sockfd);
Martin Josefssone560fd62003-06-13 16:56:51 +0000793 sockfd = -1;
794 }
Harald Welte366454b2002-01-07 13:46:50 +0000795
Martin Josefsson841e4ae2003-05-02 15:30:11 +0000796 if (strlen(tablename) >= TABLE_MAXNAMELEN) {
797 errno = EINVAL;
798 return NULL;
799 }
800
Rusty Russell79dee072000-05-02 16:45:16 +0000801 sockfd = socket(TC_AF, SOCK_RAW, IPPROTO_RAW);
Marc Bouchere6869a82000-03-20 06:03:29 +0000802 if (sockfd < 0)
803 return NULL;
804
805 s = sizeof(info);
Martin Josefsson841e4ae2003-05-02 15:30:11 +0000806
Marc Bouchere6869a82000-03-20 06:03:29 +0000807 strcpy(info.name, tablename);
Rusty Russell79dee072000-05-02 16:45:16 +0000808 if (getsockopt(sockfd, TC_IPPROTO, SO_GET_INFO, &info, &s) < 0)
Marc Bouchere6869a82000-03-20 06:03:29 +0000809 return NULL;
810
Harald Welteaae69be2004-08-29 23:32:14 +0000811 DEBUGP("valid_hooks=0x%08x, num_entries=%u, size=%u\n",
812 info.valid_hooks, info.num_entries, info.size);
813
Harald Welte0113fe72004-01-06 19:04:02 +0000814 if ((h = alloc_handle(info.name, info.size, info.num_entries))
Martin Josefsson841e4ae2003-05-02 15:30:11 +0000815 == NULL) {
816 close(sockfd);
Martin Josefssone560fd62003-06-13 16:56:51 +0000817 sockfd = -1;
Marc Bouchere6869a82000-03-20 06:03:29 +0000818 return NULL;
Martin Josefsson841e4ae2003-05-02 15:30:11 +0000819 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000820
Marc Bouchere6869a82000-03-20 06:03:29 +0000821 /* Initialize current state */
822 h->info = info;
Harald Welte0113fe72004-01-06 19:04:02 +0000823
Harald Welteaae69be2004-08-29 23:32:14 +0000824 h->entries->size = h->info.size;
Marc Bouchere6869a82000-03-20 06:03:29 +0000825
Rusty Russell79dee072000-05-02 16:45:16 +0000826 tmp = sizeof(STRUCT_GET_ENTRIES) + h->info.size;
Marc Bouchere6869a82000-03-20 06:03:29 +0000827
Harald Welteaae69be2004-08-29 23:32:14 +0000828 if (getsockopt(sockfd, TC_IPPROTO, SO_GET_ENTRIES, h->entries,
829 &tmp) < 0)
830 goto error;
831
832#ifdef IPTC_DEBUG2
833 {
834 int fd = open("/tmp/libiptc-so_get_entries.blob",
835 O_CREAT|O_WRONLY);
836 if (fd >= 0) {
837 write(fd, h->entries, tmp);
838 close(fd);
839 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000840 }
Harald Welteaae69be2004-08-29 23:32:14 +0000841#endif
842
843 if (parse_table(h) < 0)
844 goto error;
Rusty Russell7e53bf92000-03-20 07:03:28 +0000845
Marc Bouchere6869a82000-03-20 06:03:29 +0000846 CHECK(h);
847 return h;
Harald Welteaae69be2004-08-29 23:32:14 +0000848error:
849 TC_FREE(&h);
850 return NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +0000851}
852
Martin Josefsson841e4ae2003-05-02 15:30:11 +0000853void
854TC_FREE(TC_HANDLE_T *h)
855{
Harald Welteaae69be2004-08-29 23:32:14 +0000856 struct chain_head *c, *tmp;
857
Martin Josefsson841e4ae2003-05-02 15:30:11 +0000858 close(sockfd);
Martin Josefssone560fd62003-06-13 16:56:51 +0000859 sockfd = -1;
Harald Welteaae69be2004-08-29 23:32:14 +0000860
861 list_for_each_entry_safe(c, tmp, &(*h)->chains, list) {
862 struct rule_head *r, *rtmp;
863
864 list_for_each_entry_safe(r, rtmp, &c->rules, list) {
865 free(r);
866 }
867
868 free(c);
869 }
870
871 free((*h)->entries);
Martin Josefsson841e4ae2003-05-02 15:30:11 +0000872 free(*h);
Harald Welteaae69be2004-08-29 23:32:14 +0000873
Martin Josefsson841e4ae2003-05-02 15:30:11 +0000874 *h = NULL;
875}
876
Marc Bouchere6869a82000-03-20 06:03:29 +0000877static inline int
Rusty Russell79dee072000-05-02 16:45:16 +0000878print_match(const STRUCT_ENTRY_MATCH *m)
Marc Bouchere6869a82000-03-20 06:03:29 +0000879{
Rusty Russell228e98d2000-04-27 10:28:06 +0000880 printf("Match name: `%s'\n", m->u.user.name);
Marc Bouchere6869a82000-03-20 06:03:29 +0000881 return 0;
882}
883
Rusty Russell79dee072000-05-02 16:45:16 +0000884static int dump_entry(STRUCT_ENTRY *e, const TC_HANDLE_T handle);
885
Marc Bouchere6869a82000-03-20 06:03:29 +0000886void
Rusty Russell79dee072000-05-02 16:45:16 +0000887TC_DUMP_ENTRIES(const TC_HANDLE_T handle)
Marc Bouchere6869a82000-03-20 06:03:29 +0000888{
889 CHECK(handle);
Harald Welteaae69be2004-08-29 23:32:14 +0000890#if 0
Rusty Russelle45c7132004-12-16 13:21:44 +0000891 printf("libiptc v%s. %u bytes.\n",
892 IPTABLES_VERSION, handle->entries->size);
Marc Bouchere6869a82000-03-20 06:03:29 +0000893 printf("Table `%s'\n", handle->info.name);
894 printf("Hooks: pre/in/fwd/out/post = %u/%u/%u/%u/%u\n",
Rusty Russell67088e72000-05-10 01:18:57 +0000895 handle->info.hook_entry[HOOK_PRE_ROUTING],
896 handle->info.hook_entry[HOOK_LOCAL_IN],
897 handle->info.hook_entry[HOOK_FORWARD],
898 handle->info.hook_entry[HOOK_LOCAL_OUT],
899 handle->info.hook_entry[HOOK_POST_ROUTING]);
Marc Bouchere6869a82000-03-20 06:03:29 +0000900 printf("Underflows: pre/in/fwd/out/post = %u/%u/%u/%u/%u\n",
Rusty Russell67088e72000-05-10 01:18:57 +0000901 handle->info.underflow[HOOK_PRE_ROUTING],
902 handle->info.underflow[HOOK_LOCAL_IN],
903 handle->info.underflow[HOOK_FORWARD],
904 handle->info.underflow[HOOK_LOCAL_OUT],
905 handle->info.underflow[HOOK_POST_ROUTING]);
Marc Bouchere6869a82000-03-20 06:03:29 +0000906
Harald Welteaae69be2004-08-29 23:32:14 +0000907 ENTRY_ITERATE(handle->entries->entrytable, handle->entries->size,
Rusty Russell79dee072000-05-02 16:45:16 +0000908 dump_entry, handle);
Harald Welteaae69be2004-08-29 23:32:14 +0000909#endif
Harald Welte0113fe72004-01-06 19:04:02 +0000910}
Rusty Russell30fd6e52000-04-23 09:16:06 +0000911
Marc Bouchere6869a82000-03-20 06:03:29 +0000912/* Does this chain exist? */
Rusty Russell79dee072000-05-02 16:45:16 +0000913int TC_IS_CHAIN(const char *chain, const TC_HANDLE_T handle)
Marc Bouchere6869a82000-03-20 06:03:29 +0000914{
Harald Welteaae69be2004-08-29 23:32:14 +0000915 return iptcc_find_label(chain, handle) != NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +0000916}
917
Harald Welteaae69be2004-08-29 23:32:14 +0000918static void iptcc_chain_iterator_advance(TC_HANDLE_T handle)
919{
920 struct chain_head *c = handle->chain_iterator_cur;
921
922 if (c->list.next == &handle->chains)
923 handle->chain_iterator_cur = NULL;
924 else
925 handle->chain_iterator_cur =
926 list_entry(c->list.next, struct chain_head, list);
927}
Marc Bouchere6869a82000-03-20 06:03:29 +0000928
Rusty Russell30fd6e52000-04-23 09:16:06 +0000929/* Iterator functions to run through the chains. */
Marc Bouchere6869a82000-03-20 06:03:29 +0000930const char *
Philip Blundell8c700902000-05-15 02:17:52 +0000931TC_FIRST_CHAIN(TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +0000932{
Harald Welteaae69be2004-08-29 23:32:14 +0000933 struct chain_head *c = list_entry((*handle)->chains.next,
934 struct chain_head, list);
935
936 iptc_fn = TC_FIRST_CHAIN;
937
938
939 if (list_empty(&(*handle)->chains)) {
940 DEBUGP(": no chains\n");
Harald Welte0113fe72004-01-06 19:04:02 +0000941 return NULL;
Harald Welteaae69be2004-08-29 23:32:14 +0000942 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000943
Harald Welteaae69be2004-08-29 23:32:14 +0000944 (*handle)->chain_iterator_cur = c;
945 iptcc_chain_iterator_advance(*handle);
Harald Welte0113fe72004-01-06 19:04:02 +0000946
Harald Welteaae69be2004-08-29 23:32:14 +0000947 DEBUGP(": returning `%s'\n", c->name);
948 return c->name;
Marc Bouchere6869a82000-03-20 06:03:29 +0000949}
950
Rusty Russell30fd6e52000-04-23 09:16:06 +0000951/* Iterator functions to run through the chains. Returns NULL at end. */
952const char *
Rusty Russell79dee072000-05-02 16:45:16 +0000953TC_NEXT_CHAIN(TC_HANDLE_T *handle)
Rusty Russell30fd6e52000-04-23 09:16:06 +0000954{
Harald Welteaae69be2004-08-29 23:32:14 +0000955 struct chain_head *c = (*handle)->chain_iterator_cur;
Rusty Russell30fd6e52000-04-23 09:16:06 +0000956
Harald Welteaae69be2004-08-29 23:32:14 +0000957 iptc_fn = TC_NEXT_CHAIN;
958
959 if (!c) {
960 DEBUGP(": no more chains\n");
Rusty Russell30fd6e52000-04-23 09:16:06 +0000961 return NULL;
Harald Welteaae69be2004-08-29 23:32:14 +0000962 }
Rusty Russell30fd6e52000-04-23 09:16:06 +0000963
Harald Welteaae69be2004-08-29 23:32:14 +0000964 iptcc_chain_iterator_advance(*handle);
965
966 DEBUGP(": returning `%s'\n", c->name);
967 return c->name;
Rusty Russell30fd6e52000-04-23 09:16:06 +0000968}
969
970/* Get first rule in the given chain: NULL for empty chain. */
Rusty Russell79dee072000-05-02 16:45:16 +0000971const STRUCT_ENTRY *
Philip Blundell8c700902000-05-15 02:17:52 +0000972TC_FIRST_RULE(const char *chain, TC_HANDLE_T *handle)
Rusty Russell30fd6e52000-04-23 09:16:06 +0000973{
Harald Welteaae69be2004-08-29 23:32:14 +0000974 struct chain_head *c;
975 struct rule_head *r;
Rusty Russell30fd6e52000-04-23 09:16:06 +0000976
Harald Welteaae69be2004-08-29 23:32:14 +0000977 iptc_fn = TC_FIRST_RULE;
978
979 DEBUGP("first rule(%s): ", chain);
980
981 c = iptcc_find_label(chain, *handle);
Rusty Russell30fd6e52000-04-23 09:16:06 +0000982 if (!c) {
983 errno = ENOENT;
984 return NULL;
985 }
986
987 /* Empty chain: single return/policy rule */
Harald Welteaae69be2004-08-29 23:32:14 +0000988 if (list_empty(&c->rules)) {
989 DEBUGP_C("no rules, returning NULL\n");
Rusty Russell30fd6e52000-04-23 09:16:06 +0000990 return NULL;
Harald Welteaae69be2004-08-29 23:32:14 +0000991 }
Rusty Russell30fd6e52000-04-23 09:16:06 +0000992
Harald Welteaae69be2004-08-29 23:32:14 +0000993 r = list_entry(c->rules.next, struct rule_head, list);
994 (*handle)->rule_iterator_cur = r;
995 DEBUGP_C("%p\n", r);
996
997 return r->entry;
Rusty Russell30fd6e52000-04-23 09:16:06 +0000998}
999
1000/* Returns NULL when rules run out. */
Rusty Russell79dee072000-05-02 16:45:16 +00001001const STRUCT_ENTRY *
Philip Blundell8c700902000-05-15 02:17:52 +00001002TC_NEXT_RULE(const STRUCT_ENTRY *prev, TC_HANDLE_T *handle)
Rusty Russell30fd6e52000-04-23 09:16:06 +00001003{
Harald Welteaae69be2004-08-29 23:32:14 +00001004 struct rule_head *r;
Rusty Russell30fd6e52000-04-23 09:16:06 +00001005
Harald Welteaae69be2004-08-29 23:32:14 +00001006 DEBUGP("rule_iterator_cur=%p...", (*handle)->rule_iterator_cur);
1007
1008 if (!(*handle)->rule_iterator_cur) {
1009 DEBUGP_C("returning NULL\n");
1010 return NULL;
1011 }
1012
1013 r = list_entry((*handle)->rule_iterator_cur->list.next,
1014 struct rule_head, list);
1015
1016 iptc_fn = TC_NEXT_RULE;
1017
1018 DEBUGP_C("next=%p, head=%p...", &r->list,
1019 &(*handle)->rule_iterator_cur->chain->rules);
1020
1021 if (&r->list == &(*handle)->rule_iterator_cur->chain->rules) {
1022 (*handle)->rule_iterator_cur = NULL;
1023 DEBUGP_C("finished, returning NULL\n");
1024 return NULL;
1025 }
1026
1027 (*handle)->rule_iterator_cur = r;
1028
1029 /* NOTE: prev is without any influence ! */
1030 DEBUGP_C("returning rule %p\n", r);
1031 return r->entry;
Rusty Russell30fd6e52000-04-23 09:16:06 +00001032}
1033
Marc Bouchere6869a82000-03-20 06:03:29 +00001034/* How many rules in this chain? */
1035unsigned int
Rusty Russell79dee072000-05-02 16:45:16 +00001036TC_NUM_RULES(const char *chain, TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001037{
Harald Welteaae69be2004-08-29 23:32:14 +00001038 struct chain_head *c;
1039 iptc_fn = TC_NUM_RULES;
Marc Bouchere6869a82000-03-20 06:03:29 +00001040 CHECK(*handle);
Harald Welteaae69be2004-08-29 23:32:14 +00001041
1042 c = iptcc_find_label(chain, *handle);
1043 if (!c) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001044 errno = ENOENT;
1045 return (unsigned int)-1;
1046 }
Harald Welteaae69be2004-08-29 23:32:14 +00001047
1048 return c->num_rules;
Marc Bouchere6869a82000-03-20 06:03:29 +00001049}
1050
Rusty Russell79dee072000-05-02 16:45:16 +00001051const STRUCT_ENTRY *TC_GET_RULE(const char *chain,
1052 unsigned int n,
1053 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001054{
Harald Welteaae69be2004-08-29 23:32:14 +00001055 struct chain_head *c;
1056 struct rule_head *r;
1057
1058 iptc_fn = TC_GET_RULE;
Marc Bouchere6869a82000-03-20 06:03:29 +00001059
1060 CHECK(*handle);
Harald Welteaae69be2004-08-29 23:32:14 +00001061
1062 c = iptcc_find_label(chain, *handle);
1063 if (!c) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001064 errno = ENOENT;
1065 return NULL;
1066 }
1067
Harald Welteaae69be2004-08-29 23:32:14 +00001068 r = iptcc_get_rule_num(c, n);
1069 if (!r)
1070 return NULL;
1071 return r->entry;
Marc Bouchere6869a82000-03-20 06:03:29 +00001072}
1073
1074/* Returns a pointer to the target name of this position. */
Harald Welteaae69be2004-08-29 23:32:14 +00001075const char *standard_target_map(int verdict)
Marc Bouchere6869a82000-03-20 06:03:29 +00001076{
Harald Welteaae69be2004-08-29 23:32:14 +00001077 switch (verdict) {
1078 case RETURN:
1079 return LABEL_RETURN;
1080 break;
1081 case -NF_ACCEPT-1:
1082 return LABEL_ACCEPT;
1083 break;
1084 case -NF_DROP-1:
1085 return LABEL_DROP;
1086 break;
1087 case -NF_QUEUE-1:
1088 return LABEL_QUEUE;
1089 break;
1090 default:
1091 fprintf(stderr, "ERROR: %d not a valid target)\n",
1092 verdict);
1093 abort();
1094 break;
1095 }
1096 /* not reached */
1097 return NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +00001098}
1099
Harald Welteaae69be2004-08-29 23:32:14 +00001100/* Returns a pointer to the target name of this position. */
1101const char *TC_GET_TARGET(const STRUCT_ENTRY *ce,
1102 TC_HANDLE_T *handle)
1103{
1104 STRUCT_ENTRY *e = (STRUCT_ENTRY *)ce;
Rusty Russelle45c7132004-12-16 13:21:44 +00001105 struct rule_head *r = container_of(e, struct rule_head, entry[0]);
Harald Welteaae69be2004-08-29 23:32:14 +00001106
1107 iptc_fn = TC_GET_TARGET;
1108
1109 switch(r->type) {
1110 int spos;
1111 case IPTCC_R_FALLTHROUGH:
1112 return "";
1113 break;
1114 case IPTCC_R_JUMP:
1115 DEBUGP("r=%p, jump=%p, name=`%s'\n", r, r->jump, r->jump->name);
1116 return r->jump->name;
1117 break;
1118 case IPTCC_R_STANDARD:
1119 spos = *(int *)GET_TARGET(e)->data;
1120 DEBUGP("r=%p, spos=%d'\n", r, spos);
1121 return standard_target_map(spos);
1122 break;
1123 case IPTCC_R_MODULE:
1124 return GET_TARGET(e)->u.user.name;
1125 break;
1126 }
1127 return NULL;
1128}
Marc Bouchere6869a82000-03-20 06:03:29 +00001129/* Is this a built-in chain? Actually returns hook + 1. */
1130int
Rusty Russell79dee072000-05-02 16:45:16 +00001131TC_BUILTIN(const char *chain, const TC_HANDLE_T handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001132{
Harald Welteaae69be2004-08-29 23:32:14 +00001133 struct chain_head *c;
1134
1135 iptc_fn = TC_BUILTIN;
Marc Bouchere6869a82000-03-20 06:03:29 +00001136
Harald Welteaae69be2004-08-29 23:32:14 +00001137 c = iptcc_find_label(chain, handle);
1138 if (!c) {
1139 errno = ENOENT;
Martin Josefssonb0f3d2d2004-09-23 18:23:20 +00001140 return 0;
Marc Bouchere6869a82000-03-20 06:03:29 +00001141 }
Harald Welteaae69be2004-08-29 23:32:14 +00001142
1143 return iptcc_is_builtin(c);
Marc Bouchere6869a82000-03-20 06:03:29 +00001144}
1145
1146/* Get the policy of a given built-in chain */
1147const char *
Rusty Russell79dee072000-05-02 16:45:16 +00001148TC_GET_POLICY(const char *chain,
1149 STRUCT_COUNTERS *counters,
1150 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001151{
Harald Welteaae69be2004-08-29 23:32:14 +00001152 struct chain_head *c;
Marc Bouchere6869a82000-03-20 06:03:29 +00001153
Harald Welteaae69be2004-08-29 23:32:14 +00001154 iptc_fn = TC_GET_POLICY;
1155
1156 DEBUGP("called for chain %s\n", chain);
1157
1158 c = iptcc_find_label(chain, *handle);
1159 if (!c) {
1160 errno = ENOENT;
1161 return NULL;
1162 }
1163
1164 if (!iptcc_is_builtin(c))
Marc Bouchere6869a82000-03-20 06:03:29 +00001165 return NULL;
1166
Harald Welteaae69be2004-08-29 23:32:14 +00001167 *counters = c->counters;
Marc Bouchere6869a82000-03-20 06:03:29 +00001168
Harald Welteaae69be2004-08-29 23:32:14 +00001169 return standard_target_map(c->verdict);
Harald Welte0113fe72004-01-06 19:04:02 +00001170}
1171
1172static int
Harald Welteaae69be2004-08-29 23:32:14 +00001173iptcc_standard_map(struct rule_head *r, int verdict)
Harald Welte0113fe72004-01-06 19:04:02 +00001174{
Harald Welteaae69be2004-08-29 23:32:14 +00001175 STRUCT_ENTRY *e = r->entry;
Rusty Russell79dee072000-05-02 16:45:16 +00001176 STRUCT_STANDARD_TARGET *t;
Marc Bouchere6869a82000-03-20 06:03:29 +00001177
Rusty Russell79dee072000-05-02 16:45:16 +00001178 t = (STRUCT_STANDARD_TARGET *)GET_TARGET(e);
Marc Bouchere6869a82000-03-20 06:03:29 +00001179
Rusty Russell67088e72000-05-10 01:18:57 +00001180 if (t->target.u.target_size
Philip Blundell8c700902000-05-15 02:17:52 +00001181 != ALIGN(sizeof(STRUCT_STANDARD_TARGET))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001182 errno = EINVAL;
1183 return 0;
1184 }
1185 /* memset for memcmp convenience on delete/replace */
Rusty Russell79dee072000-05-02 16:45:16 +00001186 memset(t->target.u.user.name, 0, FUNCTION_MAXNAMELEN);
1187 strcpy(t->target.u.user.name, STANDARD_TARGET);
Marc Bouchere6869a82000-03-20 06:03:29 +00001188 t->verdict = verdict;
1189
Harald Welteaae69be2004-08-29 23:32:14 +00001190 r->type = IPTCC_R_STANDARD;
1191
Marc Bouchere6869a82000-03-20 06:03:29 +00001192 return 1;
1193}
Rusty Russell7e53bf92000-03-20 07:03:28 +00001194
Marc Bouchere6869a82000-03-20 06:03:29 +00001195static int
Harald Welteaae69be2004-08-29 23:32:14 +00001196iptcc_map_target(const TC_HANDLE_T handle,
1197 struct rule_head *r)
Marc Bouchere6869a82000-03-20 06:03:29 +00001198{
Harald Welteaae69be2004-08-29 23:32:14 +00001199 STRUCT_ENTRY *e = r->entry;
Harald Welte0113fe72004-01-06 19:04:02 +00001200 STRUCT_ENTRY_TARGET *t = GET_TARGET(e);
Marc Bouchere6869a82000-03-20 06:03:29 +00001201
Marc Bouchere6869a82000-03-20 06:03:29 +00001202 /* Maybe it's empty (=> fall through) */
Harald Welteaae69be2004-08-29 23:32:14 +00001203 if (strcmp(t->u.user.name, "") == 0) {
1204 r->type = IPTCC_R_FALLTHROUGH;
1205 return 1;
1206 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001207 /* Maybe it's a standard target name... */
Rusty Russell79dee072000-05-02 16:45:16 +00001208 else if (strcmp(t->u.user.name, LABEL_ACCEPT) == 0)
Harald Welteaae69be2004-08-29 23:32:14 +00001209 return iptcc_standard_map(r, -NF_ACCEPT - 1);
Rusty Russell79dee072000-05-02 16:45:16 +00001210 else if (strcmp(t->u.user.name, LABEL_DROP) == 0)
Harald Welteaae69be2004-08-29 23:32:14 +00001211 return iptcc_standard_map(r, -NF_DROP - 1);
Rusty Russell67088e72000-05-10 01:18:57 +00001212 else if (strcmp(t->u.user.name, LABEL_QUEUE) == 0)
Harald Welteaae69be2004-08-29 23:32:14 +00001213 return iptcc_standard_map(r, -NF_QUEUE - 1);
Rusty Russell79dee072000-05-02 16:45:16 +00001214 else if (strcmp(t->u.user.name, LABEL_RETURN) == 0)
Harald Welteaae69be2004-08-29 23:32:14 +00001215 return iptcc_standard_map(r, RETURN);
Rusty Russell79dee072000-05-02 16:45:16 +00001216 else if (TC_BUILTIN(t->u.user.name, handle)) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001217 /* Can't jump to builtins. */
1218 errno = EINVAL;
1219 return 0;
1220 } else {
1221 /* Maybe it's an existing chain name. */
Harald Welteaae69be2004-08-29 23:32:14 +00001222 struct chain_head *c;
1223 DEBUGP("trying to find chain `%s': ", t->u.user.name);
Marc Bouchere6869a82000-03-20 06:03:29 +00001224
Harald Welteaae69be2004-08-29 23:32:14 +00001225 c = iptcc_find_label(t->u.user.name, handle);
1226 if (c) {
1227 DEBUGP_C("found!\n");
1228 r->type = IPTCC_R_JUMP;
1229 r->jump = c;
1230 c->references++;
1231 return 1;
1232 }
1233 DEBUGP_C("not found :(\n");
Marc Bouchere6869a82000-03-20 06:03:29 +00001234 }
1235
1236 /* Must be a module? If not, kernel will reject... */
1237 /* memset to all 0 for your memcmp convenience. */
Rusty Russell228e98d2000-04-27 10:28:06 +00001238 memset(t->u.user.name + strlen(t->u.user.name),
Marc Bouchere6869a82000-03-20 06:03:29 +00001239 0,
Rusty Russell79dee072000-05-02 16:45:16 +00001240 FUNCTION_MAXNAMELEN - strlen(t->u.user.name));
Rusty Russell733e54b2004-12-16 14:22:23 +00001241 r->type = IPTCC_R_MODULE;
Harald Welteaae69be2004-08-29 23:32:14 +00001242 set_changed(handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001243 return 1;
1244}
1245
Harald Welte0113fe72004-01-06 19:04:02 +00001246/* Insert the entry `fw' in chain `chain' into position `rulenum'. */
Marc Bouchere6869a82000-03-20 06:03:29 +00001247int
Rusty Russell79dee072000-05-02 16:45:16 +00001248TC_INSERT_ENTRY(const IPT_CHAINLABEL chain,
1249 const STRUCT_ENTRY *e,
1250 unsigned int rulenum,
1251 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001252{
Harald Welteaae69be2004-08-29 23:32:14 +00001253 struct chain_head *c;
Martin Josefssoneb066cc2004-09-22 21:04:07 +00001254 struct rule_head *r;
1255 struct list_head *prev;
Marc Bouchere6869a82000-03-20 06:03:29 +00001256
Rusty Russell79dee072000-05-02 16:45:16 +00001257 iptc_fn = TC_INSERT_ENTRY;
Harald Welteaae69be2004-08-29 23:32:14 +00001258
1259 if (!(c = iptcc_find_label(chain, *handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001260 errno = ENOENT;
1261 return 0;
1262 }
1263
Martin Josefssoneb066cc2004-09-22 21:04:07 +00001264 /* first rulenum index = 0
1265 first c->num_rules index = 1 */
1266 if (rulenum > c->num_rules) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001267 errno = E2BIG;
1268 return 0;
1269 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001270
Martin Josefsson631f3612004-09-23 19:25:06 +00001271 /* If we are inserting at the end just take advantage of the
1272 double linked list, insert will happen before the entry
1273 prev points to. */
1274 if (rulenum == c->num_rules) {
Martin Josefssoneb066cc2004-09-22 21:04:07 +00001275 prev = &c->rules;
Martin Josefssona5616dc2004-10-24 22:27:31 +00001276 } else if (rulenum + 1 <= c->num_rules/2) {
Martin Josefsson631f3612004-09-23 19:25:06 +00001277 r = iptcc_get_rule_num(c, rulenum + 1);
Martin Josefssona5616dc2004-10-24 22:27:31 +00001278 prev = &r->list;
1279 } else {
1280 r = iptcc_get_rule_num_reverse(c, c->num_rules - rulenum);
Martin Josefsson631f3612004-09-23 19:25:06 +00001281 prev = &r->list;
1282 }
Martin Josefssoneb066cc2004-09-22 21:04:07 +00001283
Harald Welteaae69be2004-08-29 23:32:14 +00001284 if (!(r = iptcc_alloc_rule(c, e->next_offset))) {
1285 errno = ENOMEM;
Harald Welte0113fe72004-01-06 19:04:02 +00001286 return 0;
Harald Welteaae69be2004-08-29 23:32:14 +00001287 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001288
Harald Welteaae69be2004-08-29 23:32:14 +00001289 memcpy(r->entry, e, e->next_offset);
1290 r->counter_map.maptype = COUNTER_MAP_SET;
1291
1292 if (!iptcc_map_target(*handle, r)) {
1293 free(r);
1294 return 0;
1295 }
1296
Martin Josefssoneb066cc2004-09-22 21:04:07 +00001297 list_add_tail(&r->list, prev);
Harald Welteaae69be2004-08-29 23:32:14 +00001298 c->num_rules++;
1299
1300 set_changed(*handle);
1301
1302 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00001303}
1304
1305/* Atomically replace rule `rulenum' in `chain' with `fw'. */
1306int
Rusty Russell79dee072000-05-02 16:45:16 +00001307TC_REPLACE_ENTRY(const IPT_CHAINLABEL chain,
1308 const STRUCT_ENTRY *e,
1309 unsigned int rulenum,
1310 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001311{
Harald Welteaae69be2004-08-29 23:32:14 +00001312 struct chain_head *c;
1313 struct rule_head *r, *old;
Marc Bouchere6869a82000-03-20 06:03:29 +00001314
Rusty Russell79dee072000-05-02 16:45:16 +00001315 iptc_fn = TC_REPLACE_ENTRY;
Marc Bouchere6869a82000-03-20 06:03:29 +00001316
Harald Welteaae69be2004-08-29 23:32:14 +00001317 if (!(c = iptcc_find_label(chain, *handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001318 errno = ENOENT;
1319 return 0;
1320 }
1321
Martin Josefsson0f9b8b12004-12-18 17:18:49 +00001322 if (rulenum >= c->num_rules) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001323 errno = E2BIG;
1324 return 0;
1325 }
1326
Martin Josefsson0f9b8b12004-12-18 17:18:49 +00001327 /* Take advantage of the double linked list if possible. */
1328 if (rulenum + 1 <= c->num_rules/2) {
1329 old = iptcc_get_rule_num(c, rulenum + 1);
1330 } else {
1331 old = iptcc_get_rule_num_reverse(c, c->num_rules - rulenum);
1332 }
1333
Harald Welteaae69be2004-08-29 23:32:14 +00001334 if (!(r = iptcc_alloc_rule(c, e->next_offset))) {
1335 errno = ENOMEM;
Marc Bouchere6869a82000-03-20 06:03:29 +00001336 return 0;
Harald Welteaae69be2004-08-29 23:32:14 +00001337 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001338
Harald Welteaae69be2004-08-29 23:32:14 +00001339 memcpy(r->entry, e, e->next_offset);
1340 r->counter_map.maptype = COUNTER_MAP_SET;
1341
1342 if (!iptcc_map_target(*handle, r)) {
1343 free(r);
Harald Welte0113fe72004-01-06 19:04:02 +00001344 return 0;
Harald Welteaae69be2004-08-29 23:32:14 +00001345 }
Harald Welte0113fe72004-01-06 19:04:02 +00001346
Harald Welteaae69be2004-08-29 23:32:14 +00001347 list_add(&r->list, &old->list);
1348 iptcc_delete_rule(old);
1349
1350 set_changed(*handle);
1351
1352 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00001353}
1354
Harald Welte0113fe72004-01-06 19:04:02 +00001355/* Append entry `fw' to chain `chain'. Equivalent to insert with
Marc Bouchere6869a82000-03-20 06:03:29 +00001356 rulenum = length of chain. */
1357int
Rusty Russell79dee072000-05-02 16:45:16 +00001358TC_APPEND_ENTRY(const IPT_CHAINLABEL chain,
1359 const STRUCT_ENTRY *e,
1360 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001361{
Harald Welteaae69be2004-08-29 23:32:14 +00001362 struct chain_head *c;
1363 struct rule_head *r;
Marc Bouchere6869a82000-03-20 06:03:29 +00001364
Rusty Russell79dee072000-05-02 16:45:16 +00001365 iptc_fn = TC_APPEND_ENTRY;
Harald Welteaae69be2004-08-29 23:32:14 +00001366 if (!(c = iptcc_find_label(chain, *handle))) {
1367 DEBUGP("unable to find chain `%s'\n", chain);
Marc Bouchere6869a82000-03-20 06:03:29 +00001368 errno = ENOENT;
1369 return 0;
1370 }
1371
Harald Welteaae69be2004-08-29 23:32:14 +00001372 if (!(r = iptcc_alloc_rule(c, e->next_offset))) {
1373 DEBUGP("unable to allocate rule for chain `%s'\n", chain);
1374 errno = ENOMEM;
Harald Welte0113fe72004-01-06 19:04:02 +00001375 return 0;
Harald Welteaae69be2004-08-29 23:32:14 +00001376 }
Harald Welte0113fe72004-01-06 19:04:02 +00001377
Harald Welteaae69be2004-08-29 23:32:14 +00001378 memcpy(r->entry, e, e->next_offset);
1379 r->counter_map.maptype = COUNTER_MAP_SET;
1380
1381 if (!iptcc_map_target(*handle, r)) {
Martin Josefsson12009532004-09-23 18:24:29 +00001382 DEBUGP("unable to map target of rule for chain `%s'\n", chain);
Harald Welteaae69be2004-08-29 23:32:14 +00001383 free(r);
1384 return 0;
1385 }
1386
1387 list_add_tail(&r->list, &c->rules);
1388 c->num_rules++;
1389
1390 set_changed(*handle);
1391
1392 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00001393}
1394
1395static inline int
Rusty Russell79dee072000-05-02 16:45:16 +00001396match_different(const STRUCT_ENTRY_MATCH *a,
Rusty Russelledf14cf2000-04-19 11:26:44 +00001397 const unsigned char *a_elems,
1398 const unsigned char *b_elems,
1399 unsigned char **maskptr)
Marc Bouchere6869a82000-03-20 06:03:29 +00001400{
Rusty Russell79dee072000-05-02 16:45:16 +00001401 const STRUCT_ENTRY_MATCH *b;
Rusty Russelledf14cf2000-04-19 11:26:44 +00001402 unsigned int i;
Marc Bouchere6869a82000-03-20 06:03:29 +00001403
1404 /* Offset of b is the same as a. */
Rusty Russell30fd6e52000-04-23 09:16:06 +00001405 b = (void *)b_elems + ((unsigned char *)a - a_elems);
Marc Bouchere6869a82000-03-20 06:03:29 +00001406
Rusty Russell228e98d2000-04-27 10:28:06 +00001407 if (a->u.match_size != b->u.match_size)
Marc Bouchere6869a82000-03-20 06:03:29 +00001408 return 1;
1409
Rusty Russell228e98d2000-04-27 10:28:06 +00001410 if (strcmp(a->u.user.name, b->u.user.name) != 0)
Marc Bouchere6869a82000-03-20 06:03:29 +00001411 return 1;
1412
Rusty Russell73ef09b2000-07-03 10:24:04 +00001413 *maskptr += ALIGN(sizeof(*a));
Rusty Russelledf14cf2000-04-19 11:26:44 +00001414
Rusty Russell73ef09b2000-07-03 10:24:04 +00001415 for (i = 0; i < a->u.match_size - ALIGN(sizeof(*a)); i++)
Rusty Russelledf14cf2000-04-19 11:26:44 +00001416 if (((a->data[i] ^ b->data[i]) & (*maskptr)[i]) != 0)
Rusty Russell90e712a2000-03-29 04:19:26 +00001417 return 1;
Rusty Russelledf14cf2000-04-19 11:26:44 +00001418 *maskptr += i;
1419 return 0;
1420}
1421
1422static inline int
Rusty Russell733e54b2004-12-16 14:22:23 +00001423target_same(struct rule_head *a, struct rule_head *b,const unsigned char *mask)
Rusty Russelledf14cf2000-04-19 11:26:44 +00001424{
1425 unsigned int i;
Rusty Russell733e54b2004-12-16 14:22:23 +00001426 STRUCT_ENTRY_TARGET *ta, *tb;
Marc Bouchere6869a82000-03-20 06:03:29 +00001427
Rusty Russell733e54b2004-12-16 14:22:23 +00001428 if (a->type != b->type)
1429 return 0;
1430
1431 ta = GET_TARGET(a->entry);
1432 tb = GET_TARGET(b->entry);
1433
1434 switch (a->type) {
1435 case IPTCC_R_FALLTHROUGH:
1436 return 1;
1437 case IPTCC_R_JUMP:
1438 return a->jump == b->jump;
1439 case IPTCC_R_STANDARD:
1440 return ((STRUCT_STANDARD_TARGET *)ta)->verdict
1441 == ((STRUCT_STANDARD_TARGET *)tb)->verdict;
1442 case IPTCC_R_MODULE:
1443 if (ta->u.target_size != tb->u.target_size)
1444 return 0;
1445 if (strcmp(ta->u.user.name, tb->u.user.name) != 0)
1446 return 0;
1447
1448 for (i = 0; i < ta->u.target_size - sizeof(*ta); i++)
1449 if (((ta->data[i] ^ ta->data[i]) & mask[i]) != 0)
1450 return 0;
1451 return 1;
1452 default:
1453 fprintf(stderr, "ERROR: bad type %i\n", a->type);
1454 abort();
1455 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001456}
1457
Rusty Russell733e54b2004-12-16 14:22:23 +00001458static unsigned char *
Rusty Russell79dee072000-05-02 16:45:16 +00001459is_same(const STRUCT_ENTRY *a,
1460 const STRUCT_ENTRY *b,
1461 unsigned char *matchmask);
Marc Bouchere6869a82000-03-20 06:03:29 +00001462
Harald Welte0113fe72004-01-06 19:04:02 +00001463/* Delete the first rule in `chain' which matches `fw'. */
Marc Bouchere6869a82000-03-20 06:03:29 +00001464int
Rusty Russell79dee072000-05-02 16:45:16 +00001465TC_DELETE_ENTRY(const IPT_CHAINLABEL chain,
1466 const STRUCT_ENTRY *origfw,
1467 unsigned char *matchmask,
1468 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001469{
Harald Welteaae69be2004-08-29 23:32:14 +00001470 struct chain_head *c;
Rusty Russelle45c7132004-12-16 13:21:44 +00001471 struct rule_head *r, *i;
Marc Bouchere6869a82000-03-20 06:03:29 +00001472
Rusty Russell79dee072000-05-02 16:45:16 +00001473 iptc_fn = TC_DELETE_ENTRY;
Harald Welteaae69be2004-08-29 23:32:14 +00001474 if (!(c = iptcc_find_label(chain, *handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001475 errno = ENOENT;
1476 return 0;
1477 }
1478
Rusty Russelle45c7132004-12-16 13:21:44 +00001479 /* Create a rule_head from origfw. */
1480 r = iptcc_alloc_rule(c, origfw->next_offset);
1481 if (!r) {
Harald Welte0113fe72004-01-06 19:04:02 +00001482 errno = ENOMEM;
1483 return 0;
1484 }
1485
Rusty Russelle45c7132004-12-16 13:21:44 +00001486 memcpy(r->entry, origfw, origfw->next_offset);
1487 r->counter_map.maptype = COUNTER_MAP_NOMAP;
1488 if (!iptcc_map_target(*handle, r)) {
1489 DEBUGP("unable to map target of rule for chain `%s'\n", chain);
1490 free(r);
1491 return 0;
1492 }
Harald Welte0113fe72004-01-06 19:04:02 +00001493
Rusty Russelle45c7132004-12-16 13:21:44 +00001494 list_for_each_entry(i, &c->rules, list) {
Rusty Russell733e54b2004-12-16 14:22:23 +00001495 unsigned char *mask;
Harald Weltefe537072004-08-30 20:28:53 +00001496
Rusty Russell733e54b2004-12-16 14:22:23 +00001497 mask = is_same(r->entry, i->entry, matchmask);
1498 if (!mask)
1499 continue;
Martin Josefsson2a5dbbb2004-09-22 21:37:41 +00001500
Rusty Russell733e54b2004-12-16 14:22:23 +00001501 if (!target_same(r, i, mask))
1502 continue;
1503
1504 /* If we are about to delete the rule that is the
1505 * current iterator, move rule iterator back. next
1506 * pointer will then point to real next node */
1507 if (i == (*handle)->rule_iterator_cur) {
1508 (*handle)->rule_iterator_cur =
1509 list_entry((*handle)->rule_iterator_cur->list.prev,
1510 struct rule_head, list);
Marc Bouchere6869a82000-03-20 06:03:29 +00001511 }
Rusty Russell733e54b2004-12-16 14:22:23 +00001512
1513 c->num_rules--;
1514 iptcc_delete_rule(i);
1515
1516 set_changed(*handle);
1517 free(r);
1518 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00001519 }
1520
Rusty Russelle45c7132004-12-16 13:21:44 +00001521 free(r);
Marc Bouchere6869a82000-03-20 06:03:29 +00001522 errno = ENOENT;
1523 return 0;
Rusty Russell7e53bf92000-03-20 07:03:28 +00001524}
Harald Welteaae69be2004-08-29 23:32:14 +00001525
Marc Bouchere6869a82000-03-20 06:03:29 +00001526
1527/* Delete the rule in position `rulenum' in `chain'. */
1528int
Rusty Russell79dee072000-05-02 16:45:16 +00001529TC_DELETE_NUM_ENTRY(const IPT_CHAINLABEL chain,
1530 unsigned int rulenum,
1531 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;
Marc Bouchere6869a82000-03-20 06:03:29 +00001535
Rusty Russell79dee072000-05-02 16:45:16 +00001536 iptc_fn = TC_DELETE_NUM_ENTRY;
Harald Welteaae69be2004-08-29 23:32:14 +00001537
1538 if (!(c = iptcc_find_label(chain, *handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001539 errno = ENOENT;
1540 return 0;
1541 }
1542
Martin Josefssona5616dc2004-10-24 22:27:31 +00001543 if (rulenum >= c->num_rules) {
Martin Josefsson631f3612004-09-23 19:25:06 +00001544 errno = E2BIG;
1545 return 0;
1546 }
1547
1548 /* Take advantage of the double linked list if possible. */
Martin Josefssona5616dc2004-10-24 22:27:31 +00001549 if (rulenum + 1 <= c->num_rules/2) {
1550 r = iptcc_get_rule_num(c, rulenum + 1);
1551 } else {
1552 r = iptcc_get_rule_num_reverse(c, c->num_rules - rulenum);
Marc Bouchere6869a82000-03-20 06:03:29 +00001553 }
1554
Harald Welteaae69be2004-08-29 23:32:14 +00001555 /* If we are about to delete the rule that is the current
1556 * iterator, move rule iterator back. next pointer will then
1557 * point to real next node */
1558 if (r == (*handle)->rule_iterator_cur) {
1559 (*handle)->rule_iterator_cur =
1560 list_entry((*handle)->rule_iterator_cur->list.prev,
1561 struct rule_head, list);
Harald Welte0113fe72004-01-06 19:04:02 +00001562 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001563
Harald Welteaae69be2004-08-29 23:32:14 +00001564 c->num_rules--;
1565 iptcc_delete_rule(r);
1566
Martin Josefsson2a5dbbb2004-09-22 21:37:41 +00001567 set_changed(*handle);
1568
Harald Welteaae69be2004-08-29 23:32:14 +00001569 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00001570}
1571
1572/* Check the packet `fw' on chain `chain'. Returns the verdict, or
1573 NULL and sets errno. */
1574const char *
Rusty Russell79dee072000-05-02 16:45:16 +00001575TC_CHECK_PACKET(const IPT_CHAINLABEL chain,
1576 STRUCT_ENTRY *entry,
1577 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001578{
1579 errno = ENOSYS;
1580 return NULL;
1581}
1582
1583/* Flushes the entries in the given chain (ie. empties chain). */
1584int
Rusty Russell79dee072000-05-02 16:45:16 +00001585TC_FLUSH_ENTRIES(const IPT_CHAINLABEL chain, TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001586{
Harald Welteaae69be2004-08-29 23:32:14 +00001587 struct chain_head *c;
1588 struct rule_head *r, *tmp;
Marc Bouchere6869a82000-03-20 06:03:29 +00001589
Harald Welte0113fe72004-01-06 19:04:02 +00001590 iptc_fn = TC_FLUSH_ENTRIES;
Harald Welteaae69be2004-08-29 23:32:14 +00001591 if (!(c = iptcc_find_label(chain, *handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001592 errno = ENOENT;
1593 return 0;
1594 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001595
Harald Welteaae69be2004-08-29 23:32:14 +00001596 list_for_each_entry_safe(r, tmp, &c->rules, list) {
1597 iptcc_delete_rule(r);
1598 }
1599
1600 c->num_rules = 0;
1601
1602 set_changed(*handle);
1603
1604 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00001605}
1606
1607/* Zeroes the counters in a chain. */
1608int
Rusty Russell79dee072000-05-02 16:45:16 +00001609TC_ZERO_ENTRIES(const IPT_CHAINLABEL chain, TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001610{
Harald Welteaae69be2004-08-29 23:32:14 +00001611 struct chain_head *c;
1612 struct rule_head *r;
Rusty Russell7e53bf92000-03-20 07:03:28 +00001613
Harald Welteaae69be2004-08-29 23:32:14 +00001614 if (!(c = iptcc_find_label(chain, *handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001615 errno = ENOENT;
1616 return 0;
1617 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001618
Harald Welteaae69be2004-08-29 23:32:14 +00001619 list_for_each_entry(r, &c->rules, list) {
1620 if (r->counter_map.maptype == COUNTER_MAP_NORMAL_MAP)
1621 r->counter_map.maptype = COUNTER_MAP_ZEROED;
Marc Bouchere6869a82000-03-20 06:03:29 +00001622 }
Harald Welteaae69be2004-08-29 23:32:14 +00001623
Rusty Russell175f6412000-03-24 09:32:20 +00001624 set_changed(*handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001625
Marc Bouchere6869a82000-03-20 06:03:29 +00001626 return 1;
1627}
1628
Harald Welte1cef74d2001-01-05 15:22:59 +00001629STRUCT_COUNTERS *
1630TC_READ_COUNTER(const IPT_CHAINLABEL chain,
1631 unsigned int rulenum,
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
1637 iptc_fn = TC_READ_COUNTER;
1638 CHECK(*handle);
1639
Harald Welteaae69be2004-08-29 23:32:14 +00001640 if (!(c = iptcc_find_label(chain, *handle))) {
Harald Welte1cef74d2001-01-05 15:22:59 +00001641 errno = ENOENT;
1642 return NULL;
1643 }
1644
Harald Welteaae69be2004-08-29 23:32:14 +00001645 if (!(r = iptcc_get_rule_num(c, rulenum))) {
Harald Welte0113fe72004-01-06 19:04:02 +00001646 errno = E2BIG;
1647 return NULL;
1648 }
1649
Harald Welteaae69be2004-08-29 23:32:14 +00001650 return &r->entry[0].counters;
Harald Welte1cef74d2001-01-05 15:22:59 +00001651}
1652
1653int
1654TC_ZERO_COUNTER(const IPT_CHAINLABEL chain,
1655 unsigned int rulenum,
1656 TC_HANDLE_T *handle)
1657{
Harald Welteaae69be2004-08-29 23:32:14 +00001658 struct chain_head *c;
1659 struct rule_head *r;
Harald Welte1cef74d2001-01-05 15:22:59 +00001660
1661 iptc_fn = TC_ZERO_COUNTER;
1662 CHECK(*handle);
1663
Harald Welteaae69be2004-08-29 23:32:14 +00001664 if (!(c = iptcc_find_label(chain, *handle))) {
Harald Welte1cef74d2001-01-05 15:22:59 +00001665 errno = ENOENT;
1666 return 0;
1667 }
1668
Harald Welteaae69be2004-08-29 23:32:14 +00001669 if (!(r = iptcc_get_rule_num(c, rulenum))) {
Harald Welte0113fe72004-01-06 19:04:02 +00001670 errno = E2BIG;
1671 return 0;
1672 }
1673
Harald Welteaae69be2004-08-29 23:32:14 +00001674 if (r->counter_map.maptype == COUNTER_MAP_NORMAL_MAP)
1675 r->counter_map.maptype = COUNTER_MAP_ZEROED;
Harald Welte1cef74d2001-01-05 15:22:59 +00001676
1677 set_changed(*handle);
1678
1679 return 1;
1680}
1681
1682int
1683TC_SET_COUNTER(const IPT_CHAINLABEL chain,
1684 unsigned int rulenum,
1685 STRUCT_COUNTERS *counters,
1686 TC_HANDLE_T *handle)
1687{
Harald Welteaae69be2004-08-29 23:32:14 +00001688 struct chain_head *c;
1689 struct rule_head *r;
Harald Welte1cef74d2001-01-05 15:22:59 +00001690 STRUCT_ENTRY *e;
Harald Welte1cef74d2001-01-05 15:22:59 +00001691
1692 iptc_fn = TC_SET_COUNTER;
1693 CHECK(*handle);
1694
Harald Welteaae69be2004-08-29 23:32:14 +00001695 if (!(c = iptcc_find_label(chain, *handle))) {
Harald Welte1cef74d2001-01-05 15:22:59 +00001696 errno = ENOENT;
1697 return 0;
1698 }
Harald Welte0113fe72004-01-06 19:04:02 +00001699
Harald Welteaae69be2004-08-29 23:32:14 +00001700 if (!(r = iptcc_get_rule_num(c, rulenum))) {
Harald Welte0113fe72004-01-06 19:04:02 +00001701 errno = E2BIG;
1702 return 0;
1703 }
1704
Harald Welteaae69be2004-08-29 23:32:14 +00001705 e = r->entry;
1706 r->counter_map.maptype = COUNTER_MAP_SET;
Harald Welte0113fe72004-01-06 19:04:02 +00001707
1708 memcpy(&e->counters, counters, sizeof(STRUCT_COUNTERS));
Harald Welte1cef74d2001-01-05 15:22:59 +00001709
1710 set_changed(*handle);
1711
1712 return 1;
1713}
1714
Marc Bouchere6869a82000-03-20 06:03:29 +00001715/* Creates a new chain. */
1716/* To create a chain, create two rules: error node and unconditional
1717 * return. */
1718int
Rusty Russell79dee072000-05-02 16:45:16 +00001719TC_CREATE_CHAIN(const IPT_CHAINLABEL chain, TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001720{
Harald Welteaae69be2004-08-29 23:32:14 +00001721 static struct chain_head *c;
Marc Bouchere6869a82000-03-20 06:03:29 +00001722
Rusty Russell79dee072000-05-02 16:45:16 +00001723 iptc_fn = TC_CREATE_CHAIN;
Marc Bouchere6869a82000-03-20 06:03:29 +00001724
1725 /* find_label doesn't cover built-in targets: DROP, ACCEPT,
1726 QUEUE, RETURN. */
Harald Welteaae69be2004-08-29 23:32:14 +00001727 if (iptcc_find_label(chain, *handle)
Rusty Russell79dee072000-05-02 16:45:16 +00001728 || strcmp(chain, LABEL_DROP) == 0
1729 || strcmp(chain, LABEL_ACCEPT) == 0
Rusty Russell67088e72000-05-10 01:18:57 +00001730 || strcmp(chain, LABEL_QUEUE) == 0
Rusty Russell79dee072000-05-02 16:45:16 +00001731 || strcmp(chain, LABEL_RETURN) == 0) {
Harald Welteaae69be2004-08-29 23:32:14 +00001732 DEBUGP("Chain `%s' already exists\n", chain);
Marc Bouchere6869a82000-03-20 06:03:29 +00001733 errno = EEXIST;
1734 return 0;
1735 }
1736
Rusty Russell79dee072000-05-02 16:45:16 +00001737 if (strlen(chain)+1 > sizeof(IPT_CHAINLABEL)) {
Harald Welteaae69be2004-08-29 23:32:14 +00001738 DEBUGP("Chain name `%s' too long\n", chain);
Marc Bouchere6869a82000-03-20 06:03:29 +00001739 errno = EINVAL;
1740 return 0;
1741 }
1742
Harald Welteaae69be2004-08-29 23:32:14 +00001743 c = iptcc_alloc_chain_head(chain, 0);
1744 if (!c) {
1745 DEBUGP("Cannot allocate memory for chain `%s'\n", chain);
1746 errno = ENOMEM;
1747 return 0;
Marc Bouchere6869a82000-03-20 06:03:29 +00001748
Harald Welteaae69be2004-08-29 23:32:14 +00001749 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001750
Harald Welteaae69be2004-08-29 23:32:14 +00001751 DEBUGP("Creating chain `%s'\n", chain);
1752 list_add_tail(&c->list, &(*handle)->chains);
Harald Welte0113fe72004-01-06 19:04:02 +00001753
1754 set_changed(*handle);
1755
Harald Welteaae69be2004-08-29 23:32:14 +00001756 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00001757}
1758
1759/* Get the number of references to this chain. */
1760int
Rusty Russell79dee072000-05-02 16:45:16 +00001761TC_GET_REFERENCES(unsigned int *ref, const IPT_CHAINLABEL chain,
1762 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001763{
Harald Welteaae69be2004-08-29 23:32:14 +00001764 struct chain_head *c;
Marc Bouchere6869a82000-03-20 06:03:29 +00001765
Harald Welteaae69be2004-08-29 23:32:14 +00001766 if (!(c = iptcc_find_label(chain, *handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001767 errno = ENOENT;
1768 return 0;
1769 }
1770
Harald Welteaae69be2004-08-29 23:32:14 +00001771 *ref = c->references;
1772
Marc Bouchere6869a82000-03-20 06:03:29 +00001773 return 1;
1774}
1775
1776/* Deletes a chain. */
1777int
Rusty Russell79dee072000-05-02 16:45:16 +00001778TC_DELETE_CHAIN(const IPT_CHAINLABEL chain, TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001779{
Marc Bouchere6869a82000-03-20 06:03:29 +00001780 unsigned int references;
Harald Welteaae69be2004-08-29 23:32:14 +00001781 struct chain_head *c;
Rusty Russell7e53bf92000-03-20 07:03:28 +00001782
Rusty Russell79dee072000-05-02 16:45:16 +00001783 iptc_fn = TC_DELETE_CHAIN;
Marc Bouchere6869a82000-03-20 06:03:29 +00001784
Harald Welteaae69be2004-08-29 23:32:14 +00001785 if (!(c = iptcc_find_label(chain, *handle))) {
1786 DEBUGP("cannot find chain `%s'\n", chain);
Marc Bouchere6869a82000-03-20 06:03:29 +00001787 errno = ENOENT;
1788 return 0;
1789 }
1790
Harald Welteaae69be2004-08-29 23:32:14 +00001791 if (TC_BUILTIN(chain, *handle)) {
1792 DEBUGP("cannot remove builtin chain `%s'\n", chain);
1793 errno = EINVAL;
1794 return 0;
1795 }
1796
1797 if (!TC_GET_REFERENCES(&references, chain, handle)) {
1798 DEBUGP("cannot get references on chain `%s'\n", chain);
1799 return 0;
1800 }
1801
1802 if (references > 0) {
1803 DEBUGP("chain `%s' still has references\n", chain);
1804 errno = EMLINK;
1805 return 0;
1806 }
1807
1808 if (c->num_rules) {
1809 DEBUGP("chain `%s' is not empty\n", chain);
Marc Bouchere6869a82000-03-20 06:03:29 +00001810 errno = ENOTEMPTY;
1811 return 0;
1812 }
1813
Harald Welteaae69be2004-08-29 23:32:14 +00001814 /* If we are about to delete the chain that is the current
1815 * iterator, move chain iterator firward. */
1816 if (c == (*handle)->chain_iterator_cur)
1817 iptcc_chain_iterator_advance(*handle);
Harald Welte0113fe72004-01-06 19:04:02 +00001818
Harald Welteaae69be2004-08-29 23:32:14 +00001819 list_del(&c->list);
1820 free(c);
Harald Welte0113fe72004-01-06 19:04:02 +00001821
Harald Welteaae69be2004-08-29 23:32:14 +00001822 DEBUGP("chain `%s' deleted\n", chain);
1823
1824 set_changed(*handle);
1825
1826 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00001827}
1828
1829/* Renames a chain. */
Rusty Russell79dee072000-05-02 16:45:16 +00001830int TC_RENAME_CHAIN(const IPT_CHAINLABEL oldname,
1831 const IPT_CHAINLABEL newname,
1832 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001833{
Harald Welteaae69be2004-08-29 23:32:14 +00001834 struct chain_head *c;
Rusty Russell79dee072000-05-02 16:45:16 +00001835 iptc_fn = TC_RENAME_CHAIN;
Marc Bouchere6869a82000-03-20 06:03:29 +00001836
Harald Welte1de80462000-10-30 12:00:27 +00001837 /* find_label doesn't cover built-in targets: DROP, ACCEPT,
1838 QUEUE, RETURN. */
Harald Welteaae69be2004-08-29 23:32:14 +00001839 if (iptcc_find_label(newname, *handle)
Rusty Russell79dee072000-05-02 16:45:16 +00001840 || strcmp(newname, LABEL_DROP) == 0
1841 || strcmp(newname, LABEL_ACCEPT) == 0
Harald Welte1de80462000-10-30 12:00:27 +00001842 || strcmp(newname, LABEL_QUEUE) == 0
Rusty Russell79dee072000-05-02 16:45:16 +00001843 || strcmp(newname, LABEL_RETURN) == 0) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001844 errno = EEXIST;
1845 return 0;
1846 }
1847
Harald Welteaae69be2004-08-29 23:32:14 +00001848 if (!(c = iptcc_find_label(oldname, *handle))
Rusty Russell79dee072000-05-02 16:45:16 +00001849 || TC_BUILTIN(oldname, *handle)) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001850 errno = ENOENT;
1851 return 0;
1852 }
1853
Rusty Russell79dee072000-05-02 16:45:16 +00001854 if (strlen(newname)+1 > sizeof(IPT_CHAINLABEL)) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001855 errno = EINVAL;
1856 return 0;
1857 }
1858
Harald Welteaae69be2004-08-29 23:32:14 +00001859 strncpy(c->name, newname, sizeof(IPT_CHAINLABEL));
1860
Harald Welte0113fe72004-01-06 19:04:02 +00001861 set_changed(*handle);
1862
Marc Bouchere6869a82000-03-20 06:03:29 +00001863 return 1;
1864}
1865
1866/* Sets the policy on a built-in chain. */
1867int
Rusty Russell79dee072000-05-02 16:45:16 +00001868TC_SET_POLICY(const IPT_CHAINLABEL chain,
1869 const IPT_CHAINLABEL policy,
Harald Welte1cef74d2001-01-05 15:22:59 +00001870 STRUCT_COUNTERS *counters,
Rusty Russell79dee072000-05-02 16:45:16 +00001871 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001872{
Harald Welteaae69be2004-08-29 23:32:14 +00001873 struct chain_head *c;
Marc Bouchere6869a82000-03-20 06:03:29 +00001874
Rusty Russell79dee072000-05-02 16:45:16 +00001875 iptc_fn = TC_SET_POLICY;
Marc Bouchere6869a82000-03-20 06:03:29 +00001876
Harald Welteaae69be2004-08-29 23:32:14 +00001877 if (!(c = iptcc_find_label(chain, *handle))) {
1878 DEBUGP("cannot find chain `%s'\n", chain);
1879 errno = ENOENT;
Marc Bouchere6869a82000-03-20 06:03:29 +00001880 return 0;
1881 }
1882
Harald Welteaae69be2004-08-29 23:32:14 +00001883 if (!iptcc_is_builtin(c)) {
1884 DEBUGP("cannot set policy of userdefinedchain `%s'\n", chain);
1885 errno = ENOENT;
1886 return 0;
1887 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001888
Rusty Russell79dee072000-05-02 16:45:16 +00001889 if (strcmp(policy, LABEL_ACCEPT) == 0)
Harald Welteaae69be2004-08-29 23:32:14 +00001890 c->verdict = -NF_ACCEPT - 1;
Rusty Russell79dee072000-05-02 16:45:16 +00001891 else if (strcmp(policy, LABEL_DROP) == 0)
Harald Welteaae69be2004-08-29 23:32:14 +00001892 c->verdict = -NF_DROP - 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00001893 else {
1894 errno = EINVAL;
1895 return 0;
1896 }
Harald Welte1cef74d2001-01-05 15:22:59 +00001897
Harald Welte1cef74d2001-01-05 15:22:59 +00001898 if (counters) {
1899 /* set byte and packet counters */
Harald Welteaae69be2004-08-29 23:32:14 +00001900 memcpy(&c->counters, counters, sizeof(STRUCT_COUNTERS));
1901 c->counter_map.maptype = COUNTER_MAP_SET;
Harald Welte1cef74d2001-01-05 15:22:59 +00001902 } else {
Harald Welteaae69be2004-08-29 23:32:14 +00001903 c->counter_map.maptype = COUNTER_MAP_NOMAP;
Harald Welte1cef74d2001-01-05 15:22:59 +00001904 }
1905
Rusty Russell175f6412000-03-24 09:32:20 +00001906 set_changed(*handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001907
Marc Bouchere6869a82000-03-20 06:03:29 +00001908 return 1;
1909}
1910
1911/* Without this, on gcc 2.7.2.3, we get:
Rusty Russell79dee072000-05-02 16:45:16 +00001912 libiptc.c: In function `TC_COMMIT':
Marc Bouchere6869a82000-03-20 06:03:29 +00001913 libiptc.c:833: fixed or forbidden register was spilled.
1914 This may be due to a compiler bug or to impossible asm
1915 statements or clauses.
1916*/
1917static void
Rusty Russell79dee072000-05-02 16:45:16 +00001918subtract_counters(STRUCT_COUNTERS *answer,
1919 const STRUCT_COUNTERS *a,
1920 const STRUCT_COUNTERS *b)
Marc Bouchere6869a82000-03-20 06:03:29 +00001921{
1922 answer->pcnt = a->pcnt - b->pcnt;
1923 answer->bcnt = a->bcnt - b->bcnt;
1924}
1925
Harald Welteaae69be2004-08-29 23:32:14 +00001926
1927static void counters_nomap(STRUCT_COUNTERS_INFO *newcounters,
1928 unsigned int index)
1929{
1930 newcounters->counters[index] = ((STRUCT_COUNTERS) { 0, 0});
1931 DEBUGP_C("NOMAP => zero\n");
1932}
1933
1934static void counters_normal_map(STRUCT_COUNTERS_INFO *newcounters,
1935 STRUCT_REPLACE *repl,
1936 unsigned int index,
1937 unsigned int mappos)
1938{
1939 /* Original read: X.
1940 * Atomic read on replacement: X + Y.
1941 * Currently in kernel: Z.
1942 * Want in kernel: X + Y + Z.
1943 * => Add in X + Y
1944 * => Add in replacement read.
1945 */
1946 newcounters->counters[index] = repl->counters[mappos];
1947 DEBUGP_C("NORMAL_MAP => mappos %u \n", mappos);
1948}
1949
1950static void counters_map_zeroed(STRUCT_COUNTERS_INFO *newcounters,
1951 STRUCT_REPLACE *repl,
1952 unsigned int index,
1953 unsigned int mappos,
1954 STRUCT_COUNTERS *counters)
1955{
1956 /* Original read: X.
1957 * Atomic read on replacement: X + Y.
1958 * Currently in kernel: Z.
1959 * Want in kernel: Y + Z.
1960 * => Add in Y.
1961 * => Add in (replacement read - original read).
1962 */
1963 subtract_counters(&newcounters->counters[index],
1964 &repl->counters[mappos],
1965 counters);
1966 DEBUGP_C("ZEROED => mappos %u\n", mappos);
1967}
1968
1969static void counters_map_set(STRUCT_COUNTERS_INFO *newcounters,
1970 unsigned int index,
1971 STRUCT_COUNTERS *counters)
1972{
1973 /* Want to set counter (iptables-restore) */
1974
1975 memcpy(&newcounters->counters[index], counters,
1976 sizeof(STRUCT_COUNTERS));
1977
1978 DEBUGP_C("SET\n");
1979}
1980
1981
Marc Bouchere6869a82000-03-20 06:03:29 +00001982int
Rusty Russell79dee072000-05-02 16:45:16 +00001983TC_COMMIT(TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001984{
1985 /* Replace, then map back the counters. */
Rusty Russell79dee072000-05-02 16:45:16 +00001986 STRUCT_REPLACE *repl;
1987 STRUCT_COUNTERS_INFO *newcounters;
Harald Welteaae69be2004-08-29 23:32:14 +00001988 struct chain_head *c;
1989 int ret;
Martin Josefsson841e4ae2003-05-02 15:30:11 +00001990 size_t counterlen;
Harald Welteaae69be2004-08-29 23:32:14 +00001991 int new_number;
1992 unsigned int new_size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001993
1994 CHECK(*handle);
Martin Josefsson841e4ae2003-05-02 15:30:11 +00001995
Marc Bouchere6869a82000-03-20 06:03:29 +00001996 /* Don't commit if nothing changed. */
1997 if (!(*handle)->changed)
1998 goto finished;
1999
Harald Welteaae69be2004-08-29 23:32:14 +00002000 new_number = iptcc_compile_table_prep(*handle, &new_size);
2001 if (new_number < 0) {
2002 errno = ENOMEM;
2003 return 0;
2004 }
2005
2006 repl = malloc(sizeof(*repl) + new_size);
Marc Bouchere6869a82000-03-20 06:03:29 +00002007 if (!repl) {
2008 errno = ENOMEM;
2009 return 0;
2010 }
Martin Josefssonad3b4f92004-09-22 22:04:07 +00002011 memset(repl, 0, sizeof(*repl) + new_size);
Harald Welteaae69be2004-08-29 23:32:14 +00002012
Rusty Russelle45c7132004-12-16 13:21:44 +00002013#if 0
2014 TC_DUMP_ENTRIES(*handle);
2015#endif
2016
Harald Welteaae69be2004-08-29 23:32:14 +00002017 counterlen = sizeof(STRUCT_COUNTERS_INFO)
2018 + sizeof(STRUCT_COUNTERS) * new_number;
Marc Bouchere6869a82000-03-20 06:03:29 +00002019
2020 /* These are the old counters we will get from kernel */
Rusty Russell79dee072000-05-02 16:45:16 +00002021 repl->counters = malloc(sizeof(STRUCT_COUNTERS)
Marc Bouchere6869a82000-03-20 06:03:29 +00002022 * (*handle)->info.num_entries);
2023 if (!repl->counters) {
2024 free(repl);
2025 errno = ENOMEM;
2026 return 0;
2027 }
Marc Bouchere6869a82000-03-20 06:03:29 +00002028 /* These are the counters we're going to put back, later. */
2029 newcounters = malloc(counterlen);
2030 if (!newcounters) {
2031 free(repl->counters);
2032 free(repl);
2033 errno = ENOMEM;
2034 return 0;
2035 }
Harald Welteaae69be2004-08-29 23:32:14 +00002036 memset(newcounters, 0, counterlen);
Marc Bouchere6869a82000-03-20 06:03:29 +00002037
2038 strcpy(repl->name, (*handle)->info.name);
Harald Welteaae69be2004-08-29 23:32:14 +00002039 repl->num_entries = new_number;
2040 repl->size = new_size;
2041
Marc Bouchere6869a82000-03-20 06:03:29 +00002042 repl->num_counters = (*handle)->info.num_entries;
2043 repl->valid_hooks = (*handle)->info.valid_hooks;
Harald Welteaae69be2004-08-29 23:32:14 +00002044
2045 DEBUGP("num_entries=%u, size=%u, num_counters=%u\n",
2046 repl->num_entries, repl->size, repl->num_counters);
2047
2048 ret = iptcc_compile_table(*handle, repl);
2049 if (ret < 0) {
2050 errno = ret;
2051 free(repl->counters);
2052 free(repl);
2053 return 0;
2054 }
2055
2056
2057#ifdef IPTC_DEBUG2
2058 {
2059 int fd = open("/tmp/libiptc-so_set_replace.blob",
2060 O_CREAT|O_WRONLY);
2061 if (fd >= 0) {
2062 write(fd, repl, sizeof(*repl) + repl->size);
2063 close(fd);
2064 }
2065 }
2066#endif
Marc Bouchere6869a82000-03-20 06:03:29 +00002067
Rusty Russell79dee072000-05-02 16:45:16 +00002068 if (setsockopt(sockfd, TC_IPPROTO, SO_SET_REPLACE, repl,
Harald Welteaae69be2004-08-29 23:32:14 +00002069 sizeof(*repl) + repl->size) < 0) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002070 free(repl->counters);
2071 free(repl);
2072 free(newcounters);
2073 return 0;
2074 }
2075
2076 /* Put counters back. */
2077 strcpy(newcounters->name, (*handle)->info.name);
Harald Welteaae69be2004-08-29 23:32:14 +00002078 newcounters->num_counters = new_number;
Marc Bouchere6869a82000-03-20 06:03:29 +00002079
Harald Welteaae69be2004-08-29 23:32:14 +00002080 list_for_each_entry(c, &(*handle)->chains, list) {
2081 struct rule_head *r;
Marc Bouchere6869a82000-03-20 06:03:29 +00002082
Harald Welteaae69be2004-08-29 23:32:14 +00002083 /* Builtin chains have their own counters */
2084 if (iptcc_is_builtin(c)) {
2085 DEBUGP("counter for chain-index %u: ", c->foot_index);
2086 switch(c->counter_map.maptype) {
2087 case COUNTER_MAP_NOMAP:
2088 counters_nomap(newcounters, c->foot_index);
2089 break;
2090 case COUNTER_MAP_NORMAL_MAP:
2091 counters_normal_map(newcounters, repl,
2092 c->foot_index,
2093 c->counter_map.mappos);
2094 break;
2095 case COUNTER_MAP_ZEROED:
2096 counters_map_zeroed(newcounters, repl,
2097 c->foot_index,
2098 c->counter_map.mappos,
2099 &c->counters);
2100 break;
2101 case COUNTER_MAP_SET:
2102 counters_map_set(newcounters, c->foot_index,
2103 &c->counters);
2104 break;
2105 }
2106 }
Harald Welte1cef74d2001-01-05 15:22:59 +00002107
Harald Welteaae69be2004-08-29 23:32:14 +00002108 list_for_each_entry(r, &c->rules, list) {
2109 DEBUGP("counter for index %u: ", r->index);
2110 switch (r->counter_map.maptype) {
2111 case COUNTER_MAP_NOMAP:
2112 counters_nomap(newcounters, r->index);
2113 break;
Harald Welte1cef74d2001-01-05 15:22:59 +00002114
Harald Welteaae69be2004-08-29 23:32:14 +00002115 case COUNTER_MAP_NORMAL_MAP:
2116 counters_normal_map(newcounters, repl,
2117 r->index,
2118 r->counter_map.mappos);
2119 break;
Harald Welte1cef74d2001-01-05 15:22:59 +00002120
Harald Welteaae69be2004-08-29 23:32:14 +00002121 case COUNTER_MAP_ZEROED:
2122 counters_map_zeroed(newcounters, repl,
2123 r->index,
2124 r->counter_map.mappos,
2125 &r->entry->counters);
2126 break;
2127
2128 case COUNTER_MAP_SET:
2129 counters_map_set(newcounters, r->index,
2130 &r->entry->counters);
2131 break;
2132 }
Marc Bouchere6869a82000-03-20 06:03:29 +00002133 }
2134 }
Rusty Russell62527ce2000-09-04 09:45:54 +00002135
Harald Welteaae69be2004-08-29 23:32:14 +00002136
Rusty Russell62527ce2000-09-04 09:45:54 +00002137#ifdef KERNEL_64_USERSPACE_32
2138 {
2139 /* Kernel will think that pointer should be 64-bits, and get
2140 padding. So we accomodate here (assumption: alignment of
2141 `counters' is on 64-bit boundary). */
2142 u_int64_t *kernptr = (u_int64_t *)&newcounters->counters;
2143 if ((unsigned long)&newcounters->counters % 8 != 0) {
2144 fprintf(stderr,
2145 "counters alignment incorrect! Mail rusty!\n");
2146 abort();
2147 }
2148 *kernptr = newcounters->counters;
Rusty Russell54c307e2000-09-04 06:47:28 +00002149 }
Rusty Russell62527ce2000-09-04 09:45:54 +00002150#endif /* KERNEL_64_USERSPACE_32 */
Marc Bouchere6869a82000-03-20 06:03:29 +00002151
Harald Welteaae69be2004-08-29 23:32:14 +00002152#ifdef IPTC_DEBUG2
2153 {
2154 int fd = open("/tmp/libiptc-so_set_add_counters.blob",
2155 O_CREAT|O_WRONLY);
2156 if (fd >= 0) {
2157 write(fd, newcounters, counterlen);
2158 close(fd);
2159 }
2160 }
2161#endif
2162
Rusty Russell79dee072000-05-02 16:45:16 +00002163 if (setsockopt(sockfd, TC_IPPROTO, SO_SET_ADD_COUNTERS,
2164 newcounters, counterlen) < 0) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002165 free(repl->counters);
2166 free(repl);
2167 free(newcounters);
2168 return 0;
2169 }
2170
2171 free(repl->counters);
2172 free(repl);
2173 free(newcounters);
2174
2175 finished:
Martin Josefsson841e4ae2003-05-02 15:30:11 +00002176 TC_FREE(handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00002177 return 1;
2178}
2179
2180/* Get raw socket. */
2181int
Rusty Russell79dee072000-05-02 16:45:16 +00002182TC_GET_RAW_SOCKET()
Marc Bouchere6869a82000-03-20 06:03:29 +00002183{
2184 return sockfd;
2185}
2186
2187/* Translates errno numbers into more human-readable form than strerror. */
2188const char *
Rusty Russell79dee072000-05-02 16:45:16 +00002189TC_STRERROR(int err)
Marc Bouchere6869a82000-03-20 06:03:29 +00002190{
2191 unsigned int i;
2192 struct table_struct {
2193 void *fn;
2194 int err;
2195 const char *message;
2196 } table [] =
Harald Welte4ccfa632001-07-30 15:12:43 +00002197 { { TC_INIT, EPERM, "Permission denied (you must be root)" },
Rusty Russell79dee072000-05-02 16:45:16 +00002198 { TC_INIT, EINVAL, "Module is wrong version" },
Harald Welte4ccfa632001-07-30 15:12:43 +00002199 { TC_INIT, ENOENT,
2200 "Table does not exist (do you need to insmod?)" },
Rusty Russell79dee072000-05-02 16:45:16 +00002201 { TC_DELETE_CHAIN, ENOTEMPTY, "Chain is not empty" },
2202 { TC_DELETE_CHAIN, EINVAL, "Can't delete built-in chain" },
2203 { TC_DELETE_CHAIN, EMLINK,
Marc Bouchere6869a82000-03-20 06:03:29 +00002204 "Can't delete chain with references left" },
Rusty Russell79dee072000-05-02 16:45:16 +00002205 { TC_CREATE_CHAIN, EEXIST, "Chain already exists" },
2206 { TC_INSERT_ENTRY, E2BIG, "Index of insertion too big" },
2207 { TC_REPLACE_ENTRY, E2BIG, "Index of replacement too big" },
2208 { TC_DELETE_NUM_ENTRY, E2BIG, "Index of deletion too big" },
Harald Welte1cef74d2001-01-05 15:22:59 +00002209 { TC_READ_COUNTER, E2BIG, "Index of counter too big" },
2210 { TC_ZERO_COUNTER, E2BIG, "Index of counter too big" },
Rusty Russell79dee072000-05-02 16:45:16 +00002211 { TC_INSERT_ENTRY, ELOOP, "Loop found in table" },
2212 { TC_INSERT_ENTRY, EINVAL, "Target problem" },
Marc Bouchere6869a82000-03-20 06:03:29 +00002213 /* EINVAL for CHECK probably means bad interface. */
Rusty Russell79dee072000-05-02 16:45:16 +00002214 { TC_CHECK_PACKET, EINVAL,
Marc Boucherc8264992000-04-22 22:34:44 +00002215 "Bad arguments (does that interface exist?)" },
Harald Welte4ccfa632001-07-30 15:12:43 +00002216 { TC_CHECK_PACKET, ENOSYS,
2217 "Checking will most likely never get implemented" },
Marc Bouchere6869a82000-03-20 06:03:29 +00002218 /* ENOENT for DELETE probably means no matching rule */
Rusty Russell79dee072000-05-02 16:45:16 +00002219 { TC_DELETE_ENTRY, ENOENT,
Marc Boucherc8264992000-04-22 22:34:44 +00002220 "Bad rule (does a matching rule exist in that chain?)" },
Rusty Russell79dee072000-05-02 16:45:16 +00002221 { TC_SET_POLICY, ENOENT,
Marc Boucherc8264992000-04-22 22:34:44 +00002222 "Bad built-in chain name" },
Rusty Russell79dee072000-05-02 16:45:16 +00002223 { TC_SET_POLICY, EINVAL,
Marc Boucherc8264992000-04-22 22:34:44 +00002224 "Bad policy name" },
Harald Welte4ccfa632001-07-30 15:12:43 +00002225
2226 { NULL, 0, "Incompatible with this kernel" },
2227 { NULL, ENOPROTOOPT, "iptables who? (do you need to insmod?)" },
2228 { NULL, ENOSYS, "Will be implemented real soon. I promise ;)" },
2229 { NULL, ENOMEM, "Memory allocation problem" },
2230 { NULL, ENOENT, "No chain/target/match by that name" },
Marc Bouchere6869a82000-03-20 06:03:29 +00002231 };
2232
2233 for (i = 0; i < sizeof(table)/sizeof(struct table_struct); i++) {
2234 if ((!table[i].fn || table[i].fn == iptc_fn)
2235 && table[i].err == err)
2236 return table[i].message;
2237 }
2238
2239 return strerror(err);
2240}