blob: 5afaf4079489db6c0fa492e7a55b7b9ac3024052 [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;
Derrik Pates664c0a32005-02-01 13:28:14 +000048static int sockfd_use = 0;
Marc Bouchere6869a82000-03-20 06:03:29 +000049static void *iptc_fn = NULL;
50
Patrick McHardy0b639362007-09-08 16:00:01 +000051static const char *hooknames[] = {
52 [HOOK_PRE_ROUTING] = "PREROUTING",
53 [HOOK_LOCAL_IN] = "INPUT",
54 [HOOK_FORWARD] = "FORWARD",
55 [HOOK_LOCAL_OUT] = "OUTPUT",
56 [HOOK_POST_ROUTING] = "POSTROUTING",
Rusty Russell10758b72000-09-14 07:37:33 +000057#ifdef HOOK_DROPPING
Patrick McHardy0b639362007-09-08 16:00:01 +000058 [HOOK_DROPPING] = "DROPPING"
Rusty Russell10758b72000-09-14 07:37:33 +000059#endif
Marc Bouchere6869a82000-03-20 06:03:29 +000060};
61
Harald Welteaae69be2004-08-29 23:32:14 +000062/* Convenience structures */
63struct ipt_error_target
64{
65 STRUCT_ENTRY_TARGET t;
66 char error[TABLE_MAXNAMELEN];
67};
68
69struct chain_head;
70struct rule_head;
71
Marc Bouchere6869a82000-03-20 06:03:29 +000072struct counter_map
73{
74 enum {
75 COUNTER_MAP_NOMAP,
76 COUNTER_MAP_NORMAL_MAP,
Harald Welte1cef74d2001-01-05 15:22:59 +000077 COUNTER_MAP_ZEROED,
78 COUNTER_MAP_SET
Marc Bouchere6869a82000-03-20 06:03:29 +000079 } maptype;
80 unsigned int mappos;
81};
82
Harald Welteaae69be2004-08-29 23:32:14 +000083enum iptcc_rule_type {
84 IPTCC_R_STANDARD, /* standard target (ACCEPT, ...) */
85 IPTCC_R_MODULE, /* extension module (SNAT, ...) */
86 IPTCC_R_FALLTHROUGH, /* fallthrough rule */
87 IPTCC_R_JUMP, /* jump to other chain */
Marc Bouchere6869a82000-03-20 06:03:29 +000088};
89
Harald Welteaae69be2004-08-29 23:32:14 +000090struct rule_head
Rusty Russell30fd6e52000-04-23 09:16:06 +000091{
Harald Welteaae69be2004-08-29 23:32:14 +000092 struct list_head list;
93 struct chain_head *chain;
94 struct counter_map counter_map;
95
96 unsigned int index; /* index (needed for counter_map) */
97 unsigned int offset; /* offset in rule blob */
98
99 enum iptcc_rule_type type;
100 struct chain_head *jump; /* jump target, if IPTCC_R_JUMP */
101
102 unsigned int size; /* size of entry data */
103 STRUCT_ENTRY entry[0];
104};
105
106struct chain_head
107{
108 struct list_head list;
Rusty Russell79dee072000-05-02 16:45:16 +0000109 char name[TABLE_MAXNAMELEN];
Harald Welteaae69be2004-08-29 23:32:14 +0000110 unsigned int hooknum; /* hook number+1 if builtin */
111 unsigned int references; /* how many jumps reference us */
112 int verdict; /* verdict if builtin */
113
114 STRUCT_COUNTERS counters; /* per-chain counters */
115 struct counter_map counter_map;
116
117 unsigned int num_rules; /* number of rules in list */
118 struct list_head rules; /* list of rules */
119
120 unsigned int index; /* index (needed for jump resolval) */
121 unsigned int head_offset; /* offset in rule blob */
122 unsigned int foot_index; /* index (needed for counter_map) */
123 unsigned int foot_offset; /* offset in rule blob */
Rusty Russell30fd6e52000-04-23 09:16:06 +0000124};
125
Rusty Russell79dee072000-05-02 16:45:16 +0000126STRUCT_TC_HANDLE
Marc Bouchere6869a82000-03-20 06:03:29 +0000127{
Harald Welteaae69be2004-08-29 23:32:14 +0000128 int changed; /* Have changes been made? */
129
130 struct list_head chains;
131
132 struct chain_head *chain_iterator_cur;
133 struct rule_head *rule_iterator_cur;
134
Rusty Russell79dee072000-05-02 16:45:16 +0000135 STRUCT_GETINFO info;
Harald Welteaae69be2004-08-29 23:32:14 +0000136 STRUCT_GET_ENTRIES *entries;
Marc Bouchere6869a82000-03-20 06:03:29 +0000137};
138
Harald Welteaae69be2004-08-29 23:32:14 +0000139/* allocate a new chain head for the cache */
140static struct chain_head *iptcc_alloc_chain_head(const char *name, int hooknum)
141{
142 struct chain_head *c = malloc(sizeof(*c));
143 if (!c)
144 return NULL;
145 memset(c, 0, sizeof(*c));
146
147 strncpy(c->name, name, TABLE_MAXNAMELEN);
148 c->hooknum = hooknum;
149 INIT_LIST_HEAD(&c->rules);
150
151 return c;
152}
153
154/* allocate and initialize a new rule for the cache */
155static struct rule_head *iptcc_alloc_rule(struct chain_head *c, unsigned int size)
156{
157 struct rule_head *r = malloc(sizeof(*r)+size);
158 if (!r)
159 return NULL;
160 memset(r, 0, sizeof(*r));
161
162 r->chain = c;
163 r->size = size;
164
165 return r;
166}
167
168/* notify us that the ruleset has been modified by the user */
Jesper Dangaard Brouer91093982008-01-15 17:01:58 +0000169static inline void
Rusty Russell79dee072000-05-02 16:45:16 +0000170set_changed(TC_HANDLE_T h)
Rusty Russell175f6412000-03-24 09:32:20 +0000171{
Rusty Russell175f6412000-03-24 09:32:20 +0000172 h->changed = 1;
173}
174
Harald Welte380ba5f2002-02-13 16:19:55 +0000175#ifdef IPTC_DEBUG
Rusty Russell79dee072000-05-02 16:45:16 +0000176static void do_check(TC_HANDLE_T h, unsigned int line);
Rusty Russell849779c2000-04-23 15:51:51 +0000177#define CHECK(h) do { if (!getenv("IPTC_NO_CHECK")) do_check((h), __LINE__); } while(0)
Rusty Russell30fd6e52000-04-23 09:16:06 +0000178#else
179#define CHECK(h)
180#endif
Marc Bouchere6869a82000-03-20 06:03:29 +0000181
Harald Welteaae69be2004-08-29 23:32:14 +0000182
183/**********************************************************************
184 * iptc blob utility functions (iptcb_*)
185 **********************************************************************/
186
Marc Bouchere6869a82000-03-20 06:03:29 +0000187static inline int
Harald Welteaae69be2004-08-29 23:32:14 +0000188iptcb_get_number(const STRUCT_ENTRY *i,
Rusty Russell79dee072000-05-02 16:45:16 +0000189 const STRUCT_ENTRY *seek,
Marc Bouchere6869a82000-03-20 06:03:29 +0000190 unsigned int *pos)
191{
192 if (i == seek)
193 return 1;
194 (*pos)++;
195 return 0;
196}
197
Marc Bouchere6869a82000-03-20 06:03:29 +0000198static inline int
Harald Welteaae69be2004-08-29 23:32:14 +0000199iptcb_get_entry_n(STRUCT_ENTRY *i,
Marc Bouchere6869a82000-03-20 06:03:29 +0000200 unsigned int number,
201 unsigned int *pos,
Rusty Russell79dee072000-05-02 16:45:16 +0000202 STRUCT_ENTRY **pe)
Marc Bouchere6869a82000-03-20 06:03:29 +0000203{
204 if (*pos == number) {
205 *pe = i;
206 return 1;
207 }
208 (*pos)++;
209 return 0;
210}
211
Harald Welteaae69be2004-08-29 23:32:14 +0000212static inline STRUCT_ENTRY *
213iptcb_get_entry(TC_HANDLE_T h, unsigned int offset)
214{
215 return (STRUCT_ENTRY *)((char *)h->entries->entrytable + offset);
216}
217
218static unsigned int
219iptcb_entry2index(const TC_HANDLE_T h, const STRUCT_ENTRY *seek)
Marc Bouchere6869a82000-03-20 06:03:29 +0000220{
221 unsigned int pos = 0;
Marc Bouchere6869a82000-03-20 06:03:29 +0000222
Harald Welteaae69be2004-08-29 23:32:14 +0000223 if (ENTRY_ITERATE(h->entries->entrytable, h->entries->size,
224 iptcb_get_number, seek, &pos) == 0) {
225 fprintf(stderr, "ERROR: offset %u not an entry!\n",
226 (unsigned int)((char *)seek - (char *)h->entries->entrytable));
227 abort();
228 }
229 return pos;
Marc Bouchere6869a82000-03-20 06:03:29 +0000230}
231
Harald Welte0113fe72004-01-06 19:04:02 +0000232static inline STRUCT_ENTRY *
Harald Welteaae69be2004-08-29 23:32:14 +0000233iptcb_offset2entry(TC_HANDLE_T h, unsigned int offset)
Harald Welte0113fe72004-01-06 19:04:02 +0000234{
Harald Welteaae69be2004-08-29 23:32:14 +0000235 return (STRUCT_ENTRY *) ((void *)h->entries->entrytable+offset);
Harald Welte0113fe72004-01-06 19:04:02 +0000236}
237
Harald Welteaae69be2004-08-29 23:32:14 +0000238
Harald Welte0113fe72004-01-06 19:04:02 +0000239static inline unsigned long
Harald Welteaae69be2004-08-29 23:32:14 +0000240iptcb_entry2offset(const TC_HANDLE_T h, const STRUCT_ENTRY *e)
Harald Welte0113fe72004-01-06 19:04:02 +0000241{
Harald Welteaae69be2004-08-29 23:32:14 +0000242 return (void *)e - (void *)h->entries->entrytable;
Harald Welte3ea8f402003-06-23 18:25:59 +0000243}
244
245static inline unsigned int
Harald Welteaae69be2004-08-29 23:32:14 +0000246iptcb_offset2index(const TC_HANDLE_T h, unsigned int offset)
Harald Welte3ea8f402003-06-23 18:25:59 +0000247{
Harald Welteaae69be2004-08-29 23:32:14 +0000248 return iptcb_entry2index(h, iptcb_offset2entry(h, offset));
249}
250
251/* Returns 0 if not hook entry, else hooknumber + 1 */
252static inline unsigned int
253iptcb_ent_is_hook_entry(STRUCT_ENTRY *e, TC_HANDLE_T h)
254{
255 unsigned int i;
256
257 for (i = 0; i < NUMHOOKS; i++) {
258 if ((h->info.valid_hooks & (1 << i))
259 && iptcb_get_entry(h, h->info.hook_entry[i]) == e)
260 return i+1;
261 }
262 return 0;
Harald Welte3ea8f402003-06-23 18:25:59 +0000263}
264
265
Harald Welteaae69be2004-08-29 23:32:14 +0000266/**********************************************************************
267 * iptc cache utility functions (iptcc_*)
268 **********************************************************************/
Harald Welte0113fe72004-01-06 19:04:02 +0000269
Harald Welteaae69be2004-08-29 23:32:14 +0000270/* Is the given chain builtin (1) or user-defined (0) */
Jesper Dangaard Brouer91093982008-01-15 17:01:58 +0000271static inline unsigned int iptcc_is_builtin(struct chain_head *c)
Harald Welteaae69be2004-08-29 23:32:14 +0000272{
273 return (c->hooknum ? 1 : 0);
274}
275
276/* Get a specific rule within a chain */
277static struct rule_head *iptcc_get_rule_num(struct chain_head *c,
278 unsigned int rulenum)
279{
280 struct rule_head *r;
281 unsigned int num = 0;
282
283 list_for_each_entry(r, &c->rules, list) {
284 num++;
285 if (num == rulenum)
286 return r;
287 }
288 return NULL;
289}
290
Martin Josefssona5616dc2004-10-24 22:27:31 +0000291/* Get a specific rule within a chain backwards */
292static struct rule_head *iptcc_get_rule_num_reverse(struct chain_head *c,
293 unsigned int rulenum)
294{
295 struct rule_head *r;
296 unsigned int num = 0;
297
298 list_for_each_entry_reverse(r, &c->rules, list) {
299 num++;
300 if (num == rulenum)
301 return r;
302 }
303 return NULL;
304}
305
Harald Welteaae69be2004-08-29 23:32:14 +0000306/* Returns chain head if found, otherwise NULL. */
307static struct chain_head *
308iptcc_find_chain_by_offset(TC_HANDLE_T handle, unsigned int offset)
309{
310 struct list_head *pos;
311
312 if (list_empty(&handle->chains))
313 return NULL;
314
315 list_for_each(pos, &handle->chains) {
316 struct chain_head *c = list_entry(pos, struct chain_head, list);
317 if (offset >= c->head_offset && offset <= c->foot_offset)
318 return c;
Harald Welte0113fe72004-01-06 19:04:02 +0000319 }
320
Harald Welteaae69be2004-08-29 23:32:14 +0000321 return NULL;
Harald Welte0113fe72004-01-06 19:04:02 +0000322}
Harald Welteaae69be2004-08-29 23:32:14 +0000323/* Returns chain head if found, otherwise NULL. */
324static struct chain_head *
325iptcc_find_label(const char *name, TC_HANDLE_T handle)
326{
327 struct list_head *pos;
328
329 if (list_empty(&handle->chains))
330 return NULL;
331
332 list_for_each(pos, &handle->chains) {
333 struct chain_head *c = list_entry(pos, struct chain_head, list);
334 if (!strcmp(c->name, name))
335 return c;
336 }
337
338 return NULL;
339}
340
341/* called when rule is to be removed from cache */
342static void iptcc_delete_rule(struct rule_head *r)
343{
344 DEBUGP("deleting rule %p (offset %u)\n", r, r->offset);
345 /* clean up reference count of called chain */
346 if (r->type == IPTCC_R_JUMP
347 && r->jump)
348 r->jump->references--;
349
350 list_del(&r->list);
351 free(r);
352}
353
354
355/**********************************************************************
356 * RULESET PARSER (blob -> cache)
357 **********************************************************************/
358
Harald Welteaae69be2004-08-29 23:32:14 +0000359/* Delete policy rule of previous chain, since cache doesn't contain
360 * chain policy rules.
361 * WARNING: This function has ugly design and relies on a lot of context, only
362 * to be called from specific places within the parser */
363static int __iptcc_p_del_policy(TC_HANDLE_T h, unsigned int num)
364{
365 if (h->chain_iterator_cur) {
366 /* policy rule is last rule */
367 struct rule_head *pr = (struct rule_head *)
368 h->chain_iterator_cur->rules.prev;
369
370 /* save verdict */
371 h->chain_iterator_cur->verdict =
372 *(int *)GET_TARGET(pr->entry)->data;
373
374 /* save counter and counter_map information */
375 h->chain_iterator_cur->counter_map.maptype =
376 COUNTER_MAP_NORMAL_MAP;
377 h->chain_iterator_cur->counter_map.mappos = num-1;
378 memcpy(&h->chain_iterator_cur->counters, &pr->entry->counters,
379 sizeof(h->chain_iterator_cur->counters));
380
381 /* foot_offset points to verdict rule */
382 h->chain_iterator_cur->foot_index = num;
383 h->chain_iterator_cur->foot_offset = pr->offset;
384
385 /* delete rule from cache */
386 iptcc_delete_rule(pr);
Martin Josefsson8d1b38a2004-09-22 21:00:19 +0000387 h->chain_iterator_cur->num_rules--;
Harald Welteaae69be2004-08-29 23:32:14 +0000388
389 return 1;
390 }
391 return 0;
392}
393
Harald Welteec30b6c2005-02-01 16:45:56 +0000394/* alphabetically insert a chain into the list */
395static inline void iptc_insert_chain(TC_HANDLE_T h, struct chain_head *c)
396{
397 struct chain_head *tmp;
398
Olaf Rempel9d3ed772005-03-04 23:08:30 +0000399 /* sort only user defined chains */
400 if (!c->hooknum) {
401 list_for_each_entry(tmp, &h->chains, list) {
Robert de Barthfeca0572005-07-31 07:04:59 +0000402 if (!tmp->hooknum && strcmp(c->name, tmp->name) <= 0) {
Olaf Rempel9d3ed772005-03-04 23:08:30 +0000403 list_add(&c->list, tmp->list.prev);
404 return;
405 }
Harald Welteec30b6c2005-02-01 16:45:56 +0000406 }
407 }
408
409 /* survived till end of list: add at tail */
410 list_add_tail(&c->list, &h->chains);
411}
412
Harald Welteaae69be2004-08-29 23:32:14 +0000413/* Another ugly helper function split out of cache_add_entry to make it less
414 * spaghetti code */
415static void __iptcc_p_add_chain(TC_HANDLE_T h, struct chain_head *c,
416 unsigned int offset, unsigned int *num)
417{
Jesper Dangaard Brouer13364512007-12-12 15:20:42 +0000418 struct list_head *tail = h->chains.prev;
419 struct chain_head *ctail;
420
Harald Welteaae69be2004-08-29 23:32:14 +0000421 __iptcc_p_del_policy(h, *num);
422
423 c->head_offset = offset;
424 c->index = *num;
425
Jesper Dangaard Brouer13364512007-12-12 15:20:42 +0000426 /* Chains from kernel are already sorted, as they are inserted
427 * sorted. But there exists an issue when shifting to 1.4.0
428 * from an older version, as old versions allow last created
429 * chain to be unsorted.
430 */
431 if (iptcc_is_builtin(c)) /* Only user defined chains are sorted*/
432 list_add_tail(&c->list, &h->chains);
433 else {
434 ctail = list_entry(tail, struct chain_head, list);
435 if (strcmp(c->name, ctail->name) > 0)
436 list_add_tail(&c->list, &h->chains);/* Already sorted*/
437 else
438 iptc_insert_chain(h, c);/* Was not sorted */
439 }
Jesper Dangaard Brouerd8cb7872007-11-28 08:40:26 +0000440
Harald Welteaae69be2004-08-29 23:32:14 +0000441 h->chain_iterator_cur = c;
442}
443
444/* main parser function: add an entry from the blob to the cache */
445static int cache_add_entry(STRUCT_ENTRY *e,
446 TC_HANDLE_T h,
447 STRUCT_ENTRY **prev,
448 unsigned int *num)
449{
450 unsigned int builtin;
451 unsigned int offset = (char *)e - (char *)h->entries->entrytable;
452
453 DEBUGP("entering...");
454
455 /* Last entry ("policy rule"). End it.*/
456 if (iptcb_entry2offset(h,e) + e->next_offset == h->entries->size) {
457 /* This is the ERROR node at the end of the chain */
458 DEBUGP_C("%u:%u: end of table:\n", *num, offset);
459
460 __iptcc_p_del_policy(h, *num);
461
462 h->chain_iterator_cur = NULL;
463 goto out_inc;
464 }
465
466 /* We know this is the start of a new chain if it's an ERROR
467 * target, or a hook entry point */
468
469 if (strcmp(GET_TARGET(e)->u.user.name, ERROR_TARGET) == 0) {
470 struct chain_head *c =
471 iptcc_alloc_chain_head((const char *)GET_TARGET(e)->data, 0);
472 DEBUGP_C("%u:%u:new userdefined chain %s: %p\n", *num, offset,
473 (char *)c->name, c);
474 if (!c) {
475 errno = -ENOMEM;
476 return -1;
477 }
478
479 __iptcc_p_add_chain(h, c, offset, num);
480
481 } else if ((builtin = iptcb_ent_is_hook_entry(e, h)) != 0) {
482 struct chain_head *c =
483 iptcc_alloc_chain_head((char *)hooknames[builtin-1],
484 builtin);
485 DEBUGP_C("%u:%u new builtin chain: %p (rules=%p)\n",
486 *num, offset, c, &c->rules);
487 if (!c) {
488 errno = -ENOMEM;
489 return -1;
490 }
491
492 c->hooknum = builtin;
493
494 __iptcc_p_add_chain(h, c, offset, num);
495
496 /* FIXME: this is ugly. */
497 goto new_rule;
498 } else {
499 /* has to be normal rule */
500 struct rule_head *r;
501new_rule:
502
503 if (!(r = iptcc_alloc_rule(h->chain_iterator_cur,
504 e->next_offset))) {
505 errno = ENOMEM;
506 return -1;
507 }
508 DEBUGP_C("%u:%u normal rule: %p: ", *num, offset, r);
509
510 r->index = *num;
511 r->offset = offset;
512 memcpy(r->entry, e, e->next_offset);
513 r->counter_map.maptype = COUNTER_MAP_NORMAL_MAP;
514 r->counter_map.mappos = r->index;
515
516 /* handling of jumps, etc. */
517 if (!strcmp(GET_TARGET(e)->u.user.name, STANDARD_TARGET)) {
518 STRUCT_STANDARD_TARGET *t;
519
520 t = (STRUCT_STANDARD_TARGET *)GET_TARGET(e);
521 if (t->target.u.target_size
522 != ALIGN(sizeof(STRUCT_STANDARD_TARGET))) {
523 errno = EINVAL;
524 return -1;
525 }
526
527 if (t->verdict < 0) {
528 DEBUGP_C("standard, verdict=%d\n", t->verdict);
529 r->type = IPTCC_R_STANDARD;
530 } else if (t->verdict == r->offset+e->next_offset) {
531 DEBUGP_C("fallthrough\n");
532 r->type = IPTCC_R_FALLTHROUGH;
533 } else {
534 DEBUGP_C("jump, target=%u\n", t->verdict);
535 r->type = IPTCC_R_JUMP;
536 /* Jump target fixup has to be deferred
537 * until second pass, since we migh not
538 * yet have parsed the target */
539 }
Martin Josefsson52c38022004-09-22 19:39:40 +0000540 } else {
541 DEBUGP_C("module, target=%s\n", GET_TARGET(e)->u.user.name);
542 r->type = IPTCC_R_MODULE;
Harald Welteaae69be2004-08-29 23:32:14 +0000543 }
544
545 list_add_tail(&r->list, &h->chain_iterator_cur->rules);
Martin Josefsson8d1b38a2004-09-22 21:00:19 +0000546 h->chain_iterator_cur->num_rules++;
Harald Welteaae69be2004-08-29 23:32:14 +0000547 }
548out_inc:
549 (*num)++;
550 return 0;
551}
552
553
554/* parse an iptables blob into it's pieces */
555static int parse_table(TC_HANDLE_T h)
556{
557 STRUCT_ENTRY *prev;
558 unsigned int num = 0;
559 struct chain_head *c;
560
561 /* First pass: over ruleset blob */
562 ENTRY_ITERATE(h->entries->entrytable, h->entries->size,
563 cache_add_entry, h, &prev, &num);
564
565 /* Second pass: fixup parsed data from first pass */
566 list_for_each_entry(c, &h->chains, list) {
567 struct rule_head *r;
568 list_for_each_entry(r, &c->rules, list) {
569 struct chain_head *c;
570 STRUCT_STANDARD_TARGET *t;
571
572 if (r->type != IPTCC_R_JUMP)
573 continue;
574
575 t = (STRUCT_STANDARD_TARGET *)GET_TARGET(r->entry);
576 c = iptcc_find_chain_by_offset(h, t->verdict);
577 if (!c)
578 return -1;
579 r->jump = c;
580 c->references++;
581 }
582 }
583
584 /* FIXME: sort chains */
585
586 return 1;
587}
588
589
590/**********************************************************************
591 * RULESET COMPILATION (cache -> blob)
592 **********************************************************************/
593
594/* Convenience structures */
595struct iptcb_chain_start{
596 STRUCT_ENTRY e;
597 struct ipt_error_target name;
598};
599#define IPTCB_CHAIN_START_SIZE (sizeof(STRUCT_ENTRY) + \
600 ALIGN(sizeof(struct ipt_error_target)))
601
602struct iptcb_chain_foot {
603 STRUCT_ENTRY e;
604 STRUCT_STANDARD_TARGET target;
605};
606#define IPTCB_CHAIN_FOOT_SIZE (sizeof(STRUCT_ENTRY) + \
607 ALIGN(sizeof(STRUCT_STANDARD_TARGET)))
608
609struct iptcb_chain_error {
610 STRUCT_ENTRY entry;
611 struct ipt_error_target target;
612};
613#define IPTCB_CHAIN_ERROR_SIZE (sizeof(STRUCT_ENTRY) + \
614 ALIGN(sizeof(struct ipt_error_target)))
615
616
617
618/* compile rule from cache into blob */
619static inline int iptcc_compile_rule (TC_HANDLE_T h, STRUCT_REPLACE *repl, struct rule_head *r)
620{
621 /* handle jumps */
622 if (r->type == IPTCC_R_JUMP) {
623 STRUCT_STANDARD_TARGET *t;
624 t = (STRUCT_STANDARD_TARGET *)GET_TARGET(r->entry);
625 /* memset for memcmp convenience on delete/replace */
626 memset(t->target.u.user.name, 0, FUNCTION_MAXNAMELEN);
627 strcpy(t->target.u.user.name, STANDARD_TARGET);
628 /* Jumps can only happen to builtin chains, so we
629 * can safely assume that they always have a header */
630 t->verdict = r->jump->head_offset + IPTCB_CHAIN_START_SIZE;
631 } else if (r->type == IPTCC_R_FALLTHROUGH) {
632 STRUCT_STANDARD_TARGET *t;
633 t = (STRUCT_STANDARD_TARGET *)GET_TARGET(r->entry);
634 t->verdict = r->offset + r->size;
635 }
636
637 /* copy entry from cache to blob */
638 memcpy((char *)repl->entries+r->offset, r->entry, r->size);
639
640 return 1;
641}
642
643/* compile chain from cache into blob */
644static int iptcc_compile_chain(TC_HANDLE_T h, STRUCT_REPLACE *repl, struct chain_head *c)
645{
646 int ret;
647 struct rule_head *r;
648 struct iptcb_chain_start *head;
649 struct iptcb_chain_foot *foot;
650
651 /* only user-defined chains have heaer */
652 if (!iptcc_is_builtin(c)) {
653 /* put chain header in place */
654 head = (void *)repl->entries + c->head_offset;
655 head->e.target_offset = sizeof(STRUCT_ENTRY);
656 head->e.next_offset = IPTCB_CHAIN_START_SIZE;
657 strcpy(head->name.t.u.user.name, ERROR_TARGET);
658 head->name.t.u.target_size =
659 ALIGN(sizeof(struct ipt_error_target));
660 strcpy(head->name.error, c->name);
661 } else {
662 repl->hook_entry[c->hooknum-1] = c->head_offset;
663 repl->underflow[c->hooknum-1] = c->foot_offset;
664 }
665
666 /* iterate over rules */
667 list_for_each_entry(r, &c->rules, list) {
668 ret = iptcc_compile_rule(h, repl, r);
669 if (ret < 0)
670 return ret;
671 }
672
673 /* put chain footer in place */
674 foot = (void *)repl->entries + c->foot_offset;
675 foot->e.target_offset = sizeof(STRUCT_ENTRY);
676 foot->e.next_offset = IPTCB_CHAIN_FOOT_SIZE;
677 strcpy(foot->target.target.u.user.name, STANDARD_TARGET);
678 foot->target.target.u.target_size =
679 ALIGN(sizeof(STRUCT_STANDARD_TARGET));
680 /* builtin targets have verdict, others return */
681 if (iptcc_is_builtin(c))
682 foot->target.verdict = c->verdict;
683 else
684 foot->target.verdict = RETURN;
685 /* set policy-counters */
686 memcpy(&foot->e.counters, &c->counters, sizeof(STRUCT_COUNTERS));
687
688 return 0;
689}
690
691/* calculate offset and number for every rule in the cache */
692static int iptcc_compile_chain_offsets(TC_HANDLE_T h, struct chain_head *c,
Harald Welteefa8fc22005-07-19 22:03:49 +0000693 unsigned int *offset, unsigned int *num)
Harald Welteaae69be2004-08-29 23:32:14 +0000694{
695 struct rule_head *r;
696
697 c->head_offset = *offset;
698 DEBUGP("%s: chain_head %u, offset=%u\n", c->name, *num, *offset);
699
700 if (!iptcc_is_builtin(c)) {
701 /* Chain has header */
702 *offset += sizeof(STRUCT_ENTRY)
703 + ALIGN(sizeof(struct ipt_error_target));
704 (*num)++;
705 }
706
707 list_for_each_entry(r, &c->rules, list) {
708 DEBUGP("rule %u, offset=%u, index=%u\n", *num, *offset, *num);
709 r->offset = *offset;
710 r->index = *num;
711 *offset += r->size;
712 (*num)++;
713 }
714
715 DEBUGP("%s; chain_foot %u, offset=%u, index=%u\n", c->name, *num,
716 *offset, *num);
717 c->foot_offset = *offset;
718 c->foot_index = *num;
719 *offset += sizeof(STRUCT_ENTRY)
720 + ALIGN(sizeof(STRUCT_STANDARD_TARGET));
721 (*num)++;
722
723 return 1;
724}
725
726/* put the pieces back together again */
727static int iptcc_compile_table_prep(TC_HANDLE_T h, unsigned int *size)
728{
729 struct chain_head *c;
730 unsigned int offset = 0, num = 0;
731 int ret = 0;
732
733 /* First pass: calculate offset for every rule */
734 list_for_each_entry(c, &h->chains, list) {
735 ret = iptcc_compile_chain_offsets(h, c, &offset, &num);
736 if (ret < 0)
737 return ret;
738 }
739
740 /* Append one error rule at end of chain */
741 num++;
742 offset += sizeof(STRUCT_ENTRY)
743 + ALIGN(sizeof(struct ipt_error_target));
744
745 /* ruleset size is now in offset */
746 *size = offset;
747 return num;
748}
749
750static int iptcc_compile_table(TC_HANDLE_T h, STRUCT_REPLACE *repl)
751{
752 struct chain_head *c;
753 struct iptcb_chain_error *error;
754
755 /* Second pass: copy from cache to offsets, fill in jumps */
756 list_for_each_entry(c, &h->chains, list) {
757 int ret = iptcc_compile_chain(h, repl, c);
758 if (ret < 0)
759 return ret;
760 }
761
762 /* Append error rule at end of chain */
763 error = (void *)repl->entries + repl->size - IPTCB_CHAIN_ERROR_SIZE;
764 error->entry.target_offset = sizeof(STRUCT_ENTRY);
765 error->entry.next_offset = IPTCB_CHAIN_ERROR_SIZE;
766 error->target.t.u.user.target_size =
767 ALIGN(sizeof(struct ipt_error_target));
768 strcpy((char *)&error->target.t.u.user.name, ERROR_TARGET);
769 strcpy((char *)&error->target.error, "ERROR");
770
771 return 1;
772}
773
774/**********************************************************************
775 * EXTERNAL API (operates on cache only)
776 **********************************************************************/
Marc Bouchere6869a82000-03-20 06:03:29 +0000777
778/* Allocate handle of given size */
Rusty Russell79dee072000-05-02 16:45:16 +0000779static TC_HANDLE_T
Harald Welte0113fe72004-01-06 19:04:02 +0000780alloc_handle(const char *tablename, unsigned int size, unsigned int num_rules)
Marc Bouchere6869a82000-03-20 06:03:29 +0000781{
782 size_t len;
Rusty Russell79dee072000-05-02 16:45:16 +0000783 TC_HANDLE_T h;
Marc Bouchere6869a82000-03-20 06:03:29 +0000784
Harald Welteaae69be2004-08-29 23:32:14 +0000785 len = sizeof(STRUCT_TC_HANDLE) + size;
Marc Bouchere6869a82000-03-20 06:03:29 +0000786
Harald Welteaae69be2004-08-29 23:32:14 +0000787 h = malloc(sizeof(STRUCT_TC_HANDLE));
788 if (!h) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000789 errno = ENOMEM;
790 return NULL;
791 }
Harald Welteaae69be2004-08-29 23:32:14 +0000792 memset(h, 0, sizeof(*h));
793 INIT_LIST_HEAD(&h->chains);
Marc Bouchere6869a82000-03-20 06:03:29 +0000794 strcpy(h->info.name, tablename);
Harald Welteaae69be2004-08-29 23:32:14 +0000795
Harald Welte0371c0c2004-09-19 21:00:12 +0000796 h->entries = malloc(sizeof(STRUCT_GET_ENTRIES) + size);
Harald Welteaae69be2004-08-29 23:32:14 +0000797 if (!h->entries)
798 goto out_free_handle;
799
800 strcpy(h->entries->name, tablename);
Harald Welte0371c0c2004-09-19 21:00:12 +0000801 h->entries->size = size;
Marc Bouchere6869a82000-03-20 06:03:29 +0000802
803 return h;
Harald Welteaae69be2004-08-29 23:32:14 +0000804
805out_free_handle:
806 free(h);
807
808 return NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +0000809}
810
Harald Welteaae69be2004-08-29 23:32:14 +0000811
Rusty Russell79dee072000-05-02 16:45:16 +0000812TC_HANDLE_T
813TC_INIT(const char *tablename)
Marc Bouchere6869a82000-03-20 06:03:29 +0000814{
Rusty Russell79dee072000-05-02 16:45:16 +0000815 TC_HANDLE_T h;
816 STRUCT_GETINFO info;
Harald Welteefa8fc22005-07-19 22:03:49 +0000817 unsigned int tmp;
Marc Bouchere6869a82000-03-20 06:03:29 +0000818 socklen_t s;
819
Rusty Russell79dee072000-05-02 16:45:16 +0000820 iptc_fn = TC_INIT;
Marc Bouchere6869a82000-03-20 06:03:29 +0000821
Martin Josefsson841e4ae2003-05-02 15:30:11 +0000822 if (strlen(tablename) >= TABLE_MAXNAMELEN) {
823 errno = EINVAL;
824 return NULL;
825 }
826
Derrik Pates664c0a32005-02-01 13:28:14 +0000827 if (sockfd_use == 0) {
828 sockfd = socket(TC_AF, SOCK_RAW, IPPROTO_RAW);
829 if (sockfd < 0)
830 return NULL;
831 }
832 sockfd_use++;
Marc Bouchere6869a82000-03-20 06:03:29 +0000833
834 s = sizeof(info);
Martin Josefsson841e4ae2003-05-02 15:30:11 +0000835
Marc Bouchere6869a82000-03-20 06:03:29 +0000836 strcpy(info.name, tablename);
Derrik Pates664c0a32005-02-01 13:28:14 +0000837 if (getsockopt(sockfd, TC_IPPROTO, SO_GET_INFO, &info, &s) < 0) {
838 if (--sockfd_use == 0) {
839 close(sockfd);
840 sockfd = -1;
841 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000842 return NULL;
Derrik Pates664c0a32005-02-01 13:28:14 +0000843 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000844
Harald Welteaae69be2004-08-29 23:32:14 +0000845 DEBUGP("valid_hooks=0x%08x, num_entries=%u, size=%u\n",
846 info.valid_hooks, info.num_entries, info.size);
847
Harald Welte0113fe72004-01-06 19:04:02 +0000848 if ((h = alloc_handle(info.name, info.size, info.num_entries))
Martin Josefsson841e4ae2003-05-02 15:30:11 +0000849 == NULL) {
Derrik Pates664c0a32005-02-01 13:28:14 +0000850 if (--sockfd_use == 0) {
851 close(sockfd);
852 sockfd = -1;
853 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000854 return NULL;
Martin Josefsson841e4ae2003-05-02 15:30:11 +0000855 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000856
Marc Bouchere6869a82000-03-20 06:03:29 +0000857 /* Initialize current state */
858 h->info = info;
Harald Welte0113fe72004-01-06 19:04:02 +0000859
Harald Welteaae69be2004-08-29 23:32:14 +0000860 h->entries->size = h->info.size;
Marc Bouchere6869a82000-03-20 06:03:29 +0000861
Rusty Russell79dee072000-05-02 16:45:16 +0000862 tmp = sizeof(STRUCT_GET_ENTRIES) + h->info.size;
Marc Bouchere6869a82000-03-20 06:03:29 +0000863
Harald Welteaae69be2004-08-29 23:32:14 +0000864 if (getsockopt(sockfd, TC_IPPROTO, SO_GET_ENTRIES, h->entries,
865 &tmp) < 0)
866 goto error;
867
868#ifdef IPTC_DEBUG2
869 {
870 int fd = open("/tmp/libiptc-so_get_entries.blob",
871 O_CREAT|O_WRONLY);
872 if (fd >= 0) {
873 write(fd, h->entries, tmp);
874 close(fd);
875 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000876 }
Harald Welteaae69be2004-08-29 23:32:14 +0000877#endif
878
879 if (parse_table(h) < 0)
880 goto error;
Rusty Russell7e53bf92000-03-20 07:03:28 +0000881
Marc Bouchere6869a82000-03-20 06:03:29 +0000882 CHECK(h);
883 return h;
Harald Welteaae69be2004-08-29 23:32:14 +0000884error:
885 TC_FREE(&h);
886 return NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +0000887}
888
Martin Josefsson841e4ae2003-05-02 15:30:11 +0000889void
890TC_FREE(TC_HANDLE_T *h)
891{
Harald Welteaae69be2004-08-29 23:32:14 +0000892 struct chain_head *c, *tmp;
893
Derrik Pates664c0a32005-02-01 13:28:14 +0000894 iptc_fn = TC_FREE;
895 if (--sockfd_use == 0) {
896 close(sockfd);
897 sockfd = -1;
898 }
Harald Welteaae69be2004-08-29 23:32:14 +0000899
900 list_for_each_entry_safe(c, tmp, &(*h)->chains, list) {
901 struct rule_head *r, *rtmp;
902
903 list_for_each_entry_safe(r, rtmp, &c->rules, list) {
904 free(r);
905 }
906
907 free(c);
908 }
909
910 free((*h)->entries);
Martin Josefsson841e4ae2003-05-02 15:30:11 +0000911 free(*h);
Harald Welteaae69be2004-08-29 23:32:14 +0000912
Martin Josefsson841e4ae2003-05-02 15:30:11 +0000913 *h = NULL;
914}
915
Marc Bouchere6869a82000-03-20 06:03:29 +0000916static inline int
Rusty Russell79dee072000-05-02 16:45:16 +0000917print_match(const STRUCT_ENTRY_MATCH *m)
Marc Bouchere6869a82000-03-20 06:03:29 +0000918{
Rusty Russell228e98d2000-04-27 10:28:06 +0000919 printf("Match name: `%s'\n", m->u.user.name);
Marc Bouchere6869a82000-03-20 06:03:29 +0000920 return 0;
921}
922
Rusty Russell79dee072000-05-02 16:45:16 +0000923static int dump_entry(STRUCT_ENTRY *e, const TC_HANDLE_T handle);
924
Marc Bouchere6869a82000-03-20 06:03:29 +0000925void
Rusty Russell79dee072000-05-02 16:45:16 +0000926TC_DUMP_ENTRIES(const TC_HANDLE_T handle)
Marc Bouchere6869a82000-03-20 06:03:29 +0000927{
Derrik Pates664c0a32005-02-01 13:28:14 +0000928 iptc_fn = TC_DUMP_ENTRIES;
Marc Bouchere6869a82000-03-20 06:03:29 +0000929 CHECK(handle);
Patrick McHardy97fb2f12007-09-08 16:52:25 +0000930
Rusty Russelle45c7132004-12-16 13:21:44 +0000931 printf("libiptc v%s. %u bytes.\n",
932 IPTABLES_VERSION, handle->entries->size);
Marc Bouchere6869a82000-03-20 06:03:29 +0000933 printf("Table `%s'\n", handle->info.name);
934 printf("Hooks: pre/in/fwd/out/post = %u/%u/%u/%u/%u\n",
Rusty Russell67088e72000-05-10 01:18:57 +0000935 handle->info.hook_entry[HOOK_PRE_ROUTING],
936 handle->info.hook_entry[HOOK_LOCAL_IN],
937 handle->info.hook_entry[HOOK_FORWARD],
938 handle->info.hook_entry[HOOK_LOCAL_OUT],
939 handle->info.hook_entry[HOOK_POST_ROUTING]);
Marc Bouchere6869a82000-03-20 06:03:29 +0000940 printf("Underflows: pre/in/fwd/out/post = %u/%u/%u/%u/%u\n",
Rusty Russell67088e72000-05-10 01:18:57 +0000941 handle->info.underflow[HOOK_PRE_ROUTING],
942 handle->info.underflow[HOOK_LOCAL_IN],
943 handle->info.underflow[HOOK_FORWARD],
944 handle->info.underflow[HOOK_LOCAL_OUT],
945 handle->info.underflow[HOOK_POST_ROUTING]);
Marc Bouchere6869a82000-03-20 06:03:29 +0000946
Harald Welteaae69be2004-08-29 23:32:14 +0000947 ENTRY_ITERATE(handle->entries->entrytable, handle->entries->size,
Rusty Russell79dee072000-05-02 16:45:16 +0000948 dump_entry, handle);
Harald Welte0113fe72004-01-06 19:04:02 +0000949}
Rusty Russell30fd6e52000-04-23 09:16:06 +0000950
Marc Bouchere6869a82000-03-20 06:03:29 +0000951/* Does this chain exist? */
Rusty Russell79dee072000-05-02 16:45:16 +0000952int TC_IS_CHAIN(const char *chain, const TC_HANDLE_T handle)
Marc Bouchere6869a82000-03-20 06:03:29 +0000953{
Derrik Pates664c0a32005-02-01 13:28:14 +0000954 iptc_fn = TC_IS_CHAIN;
Harald Welteaae69be2004-08-29 23:32:14 +0000955 return iptcc_find_label(chain, handle) != NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +0000956}
957
Harald Welteaae69be2004-08-29 23:32:14 +0000958static void iptcc_chain_iterator_advance(TC_HANDLE_T handle)
959{
960 struct chain_head *c = handle->chain_iterator_cur;
961
962 if (c->list.next == &handle->chains)
963 handle->chain_iterator_cur = NULL;
964 else
965 handle->chain_iterator_cur =
966 list_entry(c->list.next, struct chain_head, list);
967}
Marc Bouchere6869a82000-03-20 06:03:29 +0000968
Rusty Russell30fd6e52000-04-23 09:16:06 +0000969/* Iterator functions to run through the chains. */
Marc Bouchere6869a82000-03-20 06:03:29 +0000970const char *
Philip Blundell8c700902000-05-15 02:17:52 +0000971TC_FIRST_CHAIN(TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +0000972{
Harald Welteaae69be2004-08-29 23:32:14 +0000973 struct chain_head *c = list_entry((*handle)->chains.next,
974 struct chain_head, list);
975
976 iptc_fn = TC_FIRST_CHAIN;
977
978
979 if (list_empty(&(*handle)->chains)) {
980 DEBUGP(": no chains\n");
Harald Welte0113fe72004-01-06 19:04:02 +0000981 return NULL;
Harald Welteaae69be2004-08-29 23:32:14 +0000982 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000983
Harald Welteaae69be2004-08-29 23:32:14 +0000984 (*handle)->chain_iterator_cur = c;
985 iptcc_chain_iterator_advance(*handle);
Harald Welte0113fe72004-01-06 19:04:02 +0000986
Harald Welteaae69be2004-08-29 23:32:14 +0000987 DEBUGP(": returning `%s'\n", c->name);
988 return c->name;
Marc Bouchere6869a82000-03-20 06:03:29 +0000989}
990
Rusty Russell30fd6e52000-04-23 09:16:06 +0000991/* Iterator functions to run through the chains. Returns NULL at end. */
992const char *
Rusty Russell79dee072000-05-02 16:45:16 +0000993TC_NEXT_CHAIN(TC_HANDLE_T *handle)
Rusty Russell30fd6e52000-04-23 09:16:06 +0000994{
Harald Welteaae69be2004-08-29 23:32:14 +0000995 struct chain_head *c = (*handle)->chain_iterator_cur;
Rusty Russell30fd6e52000-04-23 09:16:06 +0000996
Harald Welteaae69be2004-08-29 23:32:14 +0000997 iptc_fn = TC_NEXT_CHAIN;
998
999 if (!c) {
1000 DEBUGP(": no more chains\n");
Rusty Russell30fd6e52000-04-23 09:16:06 +00001001 return NULL;
Harald Welteaae69be2004-08-29 23:32:14 +00001002 }
Rusty Russell30fd6e52000-04-23 09:16:06 +00001003
Harald Welteaae69be2004-08-29 23:32:14 +00001004 iptcc_chain_iterator_advance(*handle);
1005
1006 DEBUGP(": returning `%s'\n", c->name);
1007 return c->name;
Rusty Russell30fd6e52000-04-23 09:16:06 +00001008}
1009
1010/* Get first rule in the given chain: NULL for empty chain. */
Rusty Russell79dee072000-05-02 16:45:16 +00001011const STRUCT_ENTRY *
Philip Blundell8c700902000-05-15 02:17:52 +00001012TC_FIRST_RULE(const char *chain, TC_HANDLE_T *handle)
Rusty Russell30fd6e52000-04-23 09:16:06 +00001013{
Harald Welteaae69be2004-08-29 23:32:14 +00001014 struct chain_head *c;
1015 struct rule_head *r;
Rusty Russell30fd6e52000-04-23 09:16:06 +00001016
Harald Welteaae69be2004-08-29 23:32:14 +00001017 iptc_fn = TC_FIRST_RULE;
1018
1019 DEBUGP("first rule(%s): ", chain);
1020
1021 c = iptcc_find_label(chain, *handle);
Rusty Russell30fd6e52000-04-23 09:16:06 +00001022 if (!c) {
1023 errno = ENOENT;
1024 return NULL;
1025 }
1026
1027 /* Empty chain: single return/policy rule */
Harald Welteaae69be2004-08-29 23:32:14 +00001028 if (list_empty(&c->rules)) {
1029 DEBUGP_C("no rules, returning NULL\n");
Rusty Russell30fd6e52000-04-23 09:16:06 +00001030 return NULL;
Harald Welteaae69be2004-08-29 23:32:14 +00001031 }
Rusty Russell30fd6e52000-04-23 09:16:06 +00001032
Harald Welteaae69be2004-08-29 23:32:14 +00001033 r = list_entry(c->rules.next, struct rule_head, list);
1034 (*handle)->rule_iterator_cur = r;
1035 DEBUGP_C("%p\n", r);
1036
1037 return r->entry;
Rusty Russell30fd6e52000-04-23 09:16:06 +00001038}
1039
1040/* Returns NULL when rules run out. */
Rusty Russell79dee072000-05-02 16:45:16 +00001041const STRUCT_ENTRY *
Philip Blundell8c700902000-05-15 02:17:52 +00001042TC_NEXT_RULE(const STRUCT_ENTRY *prev, TC_HANDLE_T *handle)
Rusty Russell30fd6e52000-04-23 09:16:06 +00001043{
Harald Welteaae69be2004-08-29 23:32:14 +00001044 struct rule_head *r;
Rusty Russell30fd6e52000-04-23 09:16:06 +00001045
Derrik Pates664c0a32005-02-01 13:28:14 +00001046 iptc_fn = TC_NEXT_RULE;
Harald Welteaae69be2004-08-29 23:32:14 +00001047 DEBUGP("rule_iterator_cur=%p...", (*handle)->rule_iterator_cur);
1048
1049 if (!(*handle)->rule_iterator_cur) {
1050 DEBUGP_C("returning NULL\n");
1051 return NULL;
1052 }
1053
1054 r = list_entry((*handle)->rule_iterator_cur->list.next,
1055 struct rule_head, list);
1056
1057 iptc_fn = TC_NEXT_RULE;
1058
1059 DEBUGP_C("next=%p, head=%p...", &r->list,
1060 &(*handle)->rule_iterator_cur->chain->rules);
1061
1062 if (&r->list == &(*handle)->rule_iterator_cur->chain->rules) {
1063 (*handle)->rule_iterator_cur = NULL;
1064 DEBUGP_C("finished, returning NULL\n");
1065 return NULL;
1066 }
1067
1068 (*handle)->rule_iterator_cur = r;
1069
1070 /* NOTE: prev is without any influence ! */
1071 DEBUGP_C("returning rule %p\n", r);
1072 return r->entry;
Rusty Russell30fd6e52000-04-23 09:16:06 +00001073}
1074
Marc Bouchere6869a82000-03-20 06:03:29 +00001075/* How many rules in this chain? */
1076unsigned int
Rusty Russell79dee072000-05-02 16:45:16 +00001077TC_NUM_RULES(const char *chain, TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001078{
Harald Welteaae69be2004-08-29 23:32:14 +00001079 struct chain_head *c;
1080 iptc_fn = TC_NUM_RULES;
Marc Bouchere6869a82000-03-20 06:03:29 +00001081 CHECK(*handle);
Harald Welteaae69be2004-08-29 23:32:14 +00001082
1083 c = iptcc_find_label(chain, *handle);
1084 if (!c) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001085 errno = ENOENT;
1086 return (unsigned int)-1;
1087 }
Harald Welteaae69be2004-08-29 23:32:14 +00001088
1089 return c->num_rules;
Marc Bouchere6869a82000-03-20 06:03:29 +00001090}
1091
Rusty Russell79dee072000-05-02 16:45:16 +00001092const STRUCT_ENTRY *TC_GET_RULE(const char *chain,
1093 unsigned int n,
1094 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001095{
Harald Welteaae69be2004-08-29 23:32:14 +00001096 struct chain_head *c;
1097 struct rule_head *r;
1098
1099 iptc_fn = TC_GET_RULE;
Marc Bouchere6869a82000-03-20 06:03:29 +00001100
1101 CHECK(*handle);
Harald Welteaae69be2004-08-29 23:32:14 +00001102
1103 c = iptcc_find_label(chain, *handle);
1104 if (!c) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001105 errno = ENOENT;
1106 return NULL;
1107 }
1108
Harald Welteaae69be2004-08-29 23:32:14 +00001109 r = iptcc_get_rule_num(c, n);
1110 if (!r)
1111 return NULL;
1112 return r->entry;
Marc Bouchere6869a82000-03-20 06:03:29 +00001113}
1114
1115/* Returns a pointer to the target name of this position. */
Harald Welteaae69be2004-08-29 23:32:14 +00001116const char *standard_target_map(int verdict)
Marc Bouchere6869a82000-03-20 06:03:29 +00001117{
Harald Welteaae69be2004-08-29 23:32:14 +00001118 switch (verdict) {
1119 case RETURN:
1120 return LABEL_RETURN;
1121 break;
1122 case -NF_ACCEPT-1:
1123 return LABEL_ACCEPT;
1124 break;
1125 case -NF_DROP-1:
1126 return LABEL_DROP;
1127 break;
1128 case -NF_QUEUE-1:
1129 return LABEL_QUEUE;
1130 break;
1131 default:
1132 fprintf(stderr, "ERROR: %d not a valid target)\n",
1133 verdict);
1134 abort();
1135 break;
1136 }
1137 /* not reached */
1138 return NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +00001139}
1140
Harald Welteaae69be2004-08-29 23:32:14 +00001141/* Returns a pointer to the target name of this position. */
1142const char *TC_GET_TARGET(const STRUCT_ENTRY *ce,
1143 TC_HANDLE_T *handle)
1144{
1145 STRUCT_ENTRY *e = (STRUCT_ENTRY *)ce;
Rusty Russelle45c7132004-12-16 13:21:44 +00001146 struct rule_head *r = container_of(e, struct rule_head, entry[0]);
Harald Welteaae69be2004-08-29 23:32:14 +00001147
1148 iptc_fn = TC_GET_TARGET;
1149
1150 switch(r->type) {
1151 int spos;
1152 case IPTCC_R_FALLTHROUGH:
1153 return "";
1154 break;
1155 case IPTCC_R_JUMP:
1156 DEBUGP("r=%p, jump=%p, name=`%s'\n", r, r->jump, r->jump->name);
1157 return r->jump->name;
1158 break;
1159 case IPTCC_R_STANDARD:
1160 spos = *(int *)GET_TARGET(e)->data;
1161 DEBUGP("r=%p, spos=%d'\n", r, spos);
1162 return standard_target_map(spos);
1163 break;
1164 case IPTCC_R_MODULE:
1165 return GET_TARGET(e)->u.user.name;
1166 break;
1167 }
1168 return NULL;
1169}
Marc Bouchere6869a82000-03-20 06:03:29 +00001170/* Is this a built-in chain? Actually returns hook + 1. */
1171int
Rusty Russell79dee072000-05-02 16:45:16 +00001172TC_BUILTIN(const char *chain, const TC_HANDLE_T handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001173{
Harald Welteaae69be2004-08-29 23:32:14 +00001174 struct chain_head *c;
1175
1176 iptc_fn = TC_BUILTIN;
Marc Bouchere6869a82000-03-20 06:03:29 +00001177
Harald Welteaae69be2004-08-29 23:32:14 +00001178 c = iptcc_find_label(chain, handle);
1179 if (!c) {
1180 errno = ENOENT;
Martin Josefssonb0f3d2d2004-09-23 18:23:20 +00001181 return 0;
Marc Bouchere6869a82000-03-20 06:03:29 +00001182 }
Harald Welteaae69be2004-08-29 23:32:14 +00001183
1184 return iptcc_is_builtin(c);
Marc Bouchere6869a82000-03-20 06:03:29 +00001185}
1186
1187/* Get the policy of a given built-in chain */
1188const char *
Rusty Russell79dee072000-05-02 16:45:16 +00001189TC_GET_POLICY(const char *chain,
1190 STRUCT_COUNTERS *counters,
1191 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001192{
Harald Welteaae69be2004-08-29 23:32:14 +00001193 struct chain_head *c;
Marc Bouchere6869a82000-03-20 06:03:29 +00001194
Harald Welteaae69be2004-08-29 23:32:14 +00001195 iptc_fn = TC_GET_POLICY;
1196
1197 DEBUGP("called for chain %s\n", chain);
1198
1199 c = iptcc_find_label(chain, *handle);
1200 if (!c) {
1201 errno = ENOENT;
1202 return NULL;
1203 }
1204
1205 if (!iptcc_is_builtin(c))
Marc Bouchere6869a82000-03-20 06:03:29 +00001206 return NULL;
1207
Harald Welteaae69be2004-08-29 23:32:14 +00001208 *counters = c->counters;
Marc Bouchere6869a82000-03-20 06:03:29 +00001209
Harald Welteaae69be2004-08-29 23:32:14 +00001210 return standard_target_map(c->verdict);
Harald Welte0113fe72004-01-06 19:04:02 +00001211}
1212
1213static int
Harald Welteaae69be2004-08-29 23:32:14 +00001214iptcc_standard_map(struct rule_head *r, int verdict)
Harald Welte0113fe72004-01-06 19:04:02 +00001215{
Harald Welteaae69be2004-08-29 23:32:14 +00001216 STRUCT_ENTRY *e = r->entry;
Rusty Russell79dee072000-05-02 16:45:16 +00001217 STRUCT_STANDARD_TARGET *t;
Marc Bouchere6869a82000-03-20 06:03:29 +00001218
Rusty Russell79dee072000-05-02 16:45:16 +00001219 t = (STRUCT_STANDARD_TARGET *)GET_TARGET(e);
Marc Bouchere6869a82000-03-20 06:03:29 +00001220
Rusty Russell67088e72000-05-10 01:18:57 +00001221 if (t->target.u.target_size
Philip Blundell8c700902000-05-15 02:17:52 +00001222 != ALIGN(sizeof(STRUCT_STANDARD_TARGET))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001223 errno = EINVAL;
1224 return 0;
1225 }
1226 /* memset for memcmp convenience on delete/replace */
Rusty Russell79dee072000-05-02 16:45:16 +00001227 memset(t->target.u.user.name, 0, FUNCTION_MAXNAMELEN);
1228 strcpy(t->target.u.user.name, STANDARD_TARGET);
Marc Bouchere6869a82000-03-20 06:03:29 +00001229 t->verdict = verdict;
1230
Harald Welteaae69be2004-08-29 23:32:14 +00001231 r->type = IPTCC_R_STANDARD;
1232
Marc Bouchere6869a82000-03-20 06:03:29 +00001233 return 1;
1234}
Rusty Russell7e53bf92000-03-20 07:03:28 +00001235
Marc Bouchere6869a82000-03-20 06:03:29 +00001236static int
Harald Welteaae69be2004-08-29 23:32:14 +00001237iptcc_map_target(const TC_HANDLE_T handle,
1238 struct rule_head *r)
Marc Bouchere6869a82000-03-20 06:03:29 +00001239{
Harald Welteaae69be2004-08-29 23:32:14 +00001240 STRUCT_ENTRY *e = r->entry;
Harald Welte0113fe72004-01-06 19:04:02 +00001241 STRUCT_ENTRY_TARGET *t = GET_TARGET(e);
Marc Bouchere6869a82000-03-20 06:03:29 +00001242
Marc Bouchere6869a82000-03-20 06:03:29 +00001243 /* Maybe it's empty (=> fall through) */
Harald Welteaae69be2004-08-29 23:32:14 +00001244 if (strcmp(t->u.user.name, "") == 0) {
1245 r->type = IPTCC_R_FALLTHROUGH;
1246 return 1;
1247 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001248 /* Maybe it's a standard target name... */
Rusty Russell79dee072000-05-02 16:45:16 +00001249 else if (strcmp(t->u.user.name, LABEL_ACCEPT) == 0)
Harald Welteaae69be2004-08-29 23:32:14 +00001250 return iptcc_standard_map(r, -NF_ACCEPT - 1);
Rusty Russell79dee072000-05-02 16:45:16 +00001251 else if (strcmp(t->u.user.name, LABEL_DROP) == 0)
Harald Welteaae69be2004-08-29 23:32:14 +00001252 return iptcc_standard_map(r, -NF_DROP - 1);
Rusty Russell67088e72000-05-10 01:18:57 +00001253 else if (strcmp(t->u.user.name, LABEL_QUEUE) == 0)
Harald Welteaae69be2004-08-29 23:32:14 +00001254 return iptcc_standard_map(r, -NF_QUEUE - 1);
Rusty Russell79dee072000-05-02 16:45:16 +00001255 else if (strcmp(t->u.user.name, LABEL_RETURN) == 0)
Harald Welteaae69be2004-08-29 23:32:14 +00001256 return iptcc_standard_map(r, RETURN);
Rusty Russell79dee072000-05-02 16:45:16 +00001257 else if (TC_BUILTIN(t->u.user.name, handle)) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001258 /* Can't jump to builtins. */
1259 errno = EINVAL;
1260 return 0;
1261 } else {
1262 /* Maybe it's an existing chain name. */
Harald Welteaae69be2004-08-29 23:32:14 +00001263 struct chain_head *c;
1264 DEBUGP("trying to find chain `%s': ", t->u.user.name);
Marc Bouchere6869a82000-03-20 06:03:29 +00001265
Harald Welteaae69be2004-08-29 23:32:14 +00001266 c = iptcc_find_label(t->u.user.name, handle);
1267 if (c) {
1268 DEBUGP_C("found!\n");
1269 r->type = IPTCC_R_JUMP;
1270 r->jump = c;
1271 c->references++;
1272 return 1;
1273 }
1274 DEBUGP_C("not found :(\n");
Marc Bouchere6869a82000-03-20 06:03:29 +00001275 }
1276
1277 /* Must be a module? If not, kernel will reject... */
Rusty Russell3aef54d2005-01-03 03:48:40 +00001278 /* memset to all 0 for your memcmp convenience: don't clear version */
Rusty Russell228e98d2000-04-27 10:28:06 +00001279 memset(t->u.user.name + strlen(t->u.user.name),
Marc Bouchere6869a82000-03-20 06:03:29 +00001280 0,
Rusty Russell3aef54d2005-01-03 03:48:40 +00001281 FUNCTION_MAXNAMELEN - 1 - strlen(t->u.user.name));
Rusty Russell733e54b2004-12-16 14:22:23 +00001282 r->type = IPTCC_R_MODULE;
Harald Welteaae69be2004-08-29 23:32:14 +00001283 set_changed(handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001284 return 1;
1285}
1286
Harald Welte0113fe72004-01-06 19:04:02 +00001287/* Insert the entry `fw' in chain `chain' into position `rulenum'. */
Marc Bouchere6869a82000-03-20 06:03:29 +00001288int
Rusty Russell79dee072000-05-02 16:45:16 +00001289TC_INSERT_ENTRY(const IPT_CHAINLABEL chain,
1290 const STRUCT_ENTRY *e,
1291 unsigned int rulenum,
1292 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001293{
Harald Welteaae69be2004-08-29 23:32:14 +00001294 struct chain_head *c;
Martin Josefssoneb066cc2004-09-22 21:04:07 +00001295 struct rule_head *r;
1296 struct list_head *prev;
Marc Bouchere6869a82000-03-20 06:03:29 +00001297
Rusty Russell79dee072000-05-02 16:45:16 +00001298 iptc_fn = TC_INSERT_ENTRY;
Harald Welteaae69be2004-08-29 23:32:14 +00001299
1300 if (!(c = iptcc_find_label(chain, *handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001301 errno = ENOENT;
1302 return 0;
1303 }
1304
Martin Josefssoneb066cc2004-09-22 21:04:07 +00001305 /* first rulenum index = 0
1306 first c->num_rules index = 1 */
1307 if (rulenum > c->num_rules) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001308 errno = E2BIG;
1309 return 0;
1310 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001311
Martin Josefsson631f3612004-09-23 19:25:06 +00001312 /* If we are inserting at the end just take advantage of the
1313 double linked list, insert will happen before the entry
1314 prev points to. */
1315 if (rulenum == c->num_rules) {
Martin Josefssoneb066cc2004-09-22 21:04:07 +00001316 prev = &c->rules;
Martin Josefssona5616dc2004-10-24 22:27:31 +00001317 } else if (rulenum + 1 <= c->num_rules/2) {
Martin Josefsson631f3612004-09-23 19:25:06 +00001318 r = iptcc_get_rule_num(c, rulenum + 1);
Martin Josefssona5616dc2004-10-24 22:27:31 +00001319 prev = &r->list;
1320 } else {
1321 r = iptcc_get_rule_num_reverse(c, c->num_rules - rulenum);
Martin Josefsson631f3612004-09-23 19:25:06 +00001322 prev = &r->list;
1323 }
Martin Josefssoneb066cc2004-09-22 21:04:07 +00001324
Harald Welteaae69be2004-08-29 23:32:14 +00001325 if (!(r = iptcc_alloc_rule(c, e->next_offset))) {
1326 errno = ENOMEM;
Harald Welte0113fe72004-01-06 19:04:02 +00001327 return 0;
Harald Welteaae69be2004-08-29 23:32:14 +00001328 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001329
Harald Welteaae69be2004-08-29 23:32:14 +00001330 memcpy(r->entry, e, e->next_offset);
1331 r->counter_map.maptype = COUNTER_MAP_SET;
1332
1333 if (!iptcc_map_target(*handle, r)) {
1334 free(r);
1335 return 0;
1336 }
1337
Martin Josefssoneb066cc2004-09-22 21:04:07 +00001338 list_add_tail(&r->list, prev);
Harald Welteaae69be2004-08-29 23:32:14 +00001339 c->num_rules++;
1340
1341 set_changed(*handle);
1342
1343 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00001344}
1345
1346/* Atomically replace rule `rulenum' in `chain' with `fw'. */
1347int
Rusty Russell79dee072000-05-02 16:45:16 +00001348TC_REPLACE_ENTRY(const IPT_CHAINLABEL chain,
1349 const STRUCT_ENTRY *e,
1350 unsigned int rulenum,
1351 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001352{
Harald Welteaae69be2004-08-29 23:32:14 +00001353 struct chain_head *c;
1354 struct rule_head *r, *old;
Marc Bouchere6869a82000-03-20 06:03:29 +00001355
Rusty Russell79dee072000-05-02 16:45:16 +00001356 iptc_fn = TC_REPLACE_ENTRY;
Marc Bouchere6869a82000-03-20 06:03:29 +00001357
Harald Welteaae69be2004-08-29 23:32:14 +00001358 if (!(c = iptcc_find_label(chain, *handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001359 errno = ENOENT;
1360 return 0;
1361 }
1362
Martin Josefsson0f9b8b12004-12-18 17:18:49 +00001363 if (rulenum >= c->num_rules) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001364 errno = E2BIG;
1365 return 0;
1366 }
1367
Martin Josefsson0f9b8b12004-12-18 17:18:49 +00001368 /* Take advantage of the double linked list if possible. */
1369 if (rulenum + 1 <= c->num_rules/2) {
1370 old = iptcc_get_rule_num(c, rulenum + 1);
1371 } else {
1372 old = iptcc_get_rule_num_reverse(c, c->num_rules - rulenum);
1373 }
1374
Harald Welteaae69be2004-08-29 23:32:14 +00001375 if (!(r = iptcc_alloc_rule(c, e->next_offset))) {
1376 errno = ENOMEM;
Marc Bouchere6869a82000-03-20 06:03:29 +00001377 return 0;
Harald Welteaae69be2004-08-29 23:32:14 +00001378 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001379
Harald Welteaae69be2004-08-29 23:32:14 +00001380 memcpy(r->entry, e, e->next_offset);
1381 r->counter_map.maptype = COUNTER_MAP_SET;
1382
1383 if (!iptcc_map_target(*handle, r)) {
1384 free(r);
Harald Welte0113fe72004-01-06 19:04:02 +00001385 return 0;
Harald Welteaae69be2004-08-29 23:32:14 +00001386 }
Harald Welte0113fe72004-01-06 19:04:02 +00001387
Harald Welteaae69be2004-08-29 23:32:14 +00001388 list_add(&r->list, &old->list);
1389 iptcc_delete_rule(old);
1390
1391 set_changed(*handle);
1392
1393 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00001394}
1395
Harald Welte0113fe72004-01-06 19:04:02 +00001396/* Append entry `fw' to chain `chain'. Equivalent to insert with
Marc Bouchere6869a82000-03-20 06:03:29 +00001397 rulenum = length of chain. */
1398int
Rusty Russell79dee072000-05-02 16:45:16 +00001399TC_APPEND_ENTRY(const IPT_CHAINLABEL chain,
1400 const STRUCT_ENTRY *e,
1401 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001402{
Harald Welteaae69be2004-08-29 23:32:14 +00001403 struct chain_head *c;
1404 struct rule_head *r;
Marc Bouchere6869a82000-03-20 06:03:29 +00001405
Rusty Russell79dee072000-05-02 16:45:16 +00001406 iptc_fn = TC_APPEND_ENTRY;
Harald Welteaae69be2004-08-29 23:32:14 +00001407 if (!(c = iptcc_find_label(chain, *handle))) {
1408 DEBUGP("unable to find chain `%s'\n", chain);
Marc Bouchere6869a82000-03-20 06:03:29 +00001409 errno = ENOENT;
1410 return 0;
1411 }
1412
Harald Welteaae69be2004-08-29 23:32:14 +00001413 if (!(r = iptcc_alloc_rule(c, e->next_offset))) {
1414 DEBUGP("unable to allocate rule for chain `%s'\n", chain);
1415 errno = ENOMEM;
Harald Welte0113fe72004-01-06 19:04:02 +00001416 return 0;
Harald Welteaae69be2004-08-29 23:32:14 +00001417 }
Harald Welte0113fe72004-01-06 19:04:02 +00001418
Harald Welteaae69be2004-08-29 23:32:14 +00001419 memcpy(r->entry, e, e->next_offset);
1420 r->counter_map.maptype = COUNTER_MAP_SET;
1421
1422 if (!iptcc_map_target(*handle, r)) {
Martin Josefsson12009532004-09-23 18:24:29 +00001423 DEBUGP("unable to map target of rule for chain `%s'\n", chain);
Harald Welteaae69be2004-08-29 23:32:14 +00001424 free(r);
1425 return 0;
1426 }
1427
1428 list_add_tail(&r->list, &c->rules);
1429 c->num_rules++;
1430
1431 set_changed(*handle);
1432
1433 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00001434}
1435
1436static inline int
Rusty Russell79dee072000-05-02 16:45:16 +00001437match_different(const STRUCT_ENTRY_MATCH *a,
Rusty Russelledf14cf2000-04-19 11:26:44 +00001438 const unsigned char *a_elems,
1439 const unsigned char *b_elems,
1440 unsigned char **maskptr)
Marc Bouchere6869a82000-03-20 06:03:29 +00001441{
Rusty Russell79dee072000-05-02 16:45:16 +00001442 const STRUCT_ENTRY_MATCH *b;
Rusty Russelledf14cf2000-04-19 11:26:44 +00001443 unsigned int i;
Marc Bouchere6869a82000-03-20 06:03:29 +00001444
1445 /* Offset of b is the same as a. */
Rusty Russell30fd6e52000-04-23 09:16:06 +00001446 b = (void *)b_elems + ((unsigned char *)a - a_elems);
Marc Bouchere6869a82000-03-20 06:03:29 +00001447
Rusty Russell228e98d2000-04-27 10:28:06 +00001448 if (a->u.match_size != b->u.match_size)
Marc Bouchere6869a82000-03-20 06:03:29 +00001449 return 1;
1450
Rusty Russell228e98d2000-04-27 10:28:06 +00001451 if (strcmp(a->u.user.name, b->u.user.name) != 0)
Marc Bouchere6869a82000-03-20 06:03:29 +00001452 return 1;
1453
Rusty Russell73ef09b2000-07-03 10:24:04 +00001454 *maskptr += ALIGN(sizeof(*a));
Rusty Russelledf14cf2000-04-19 11:26:44 +00001455
Rusty Russell73ef09b2000-07-03 10:24:04 +00001456 for (i = 0; i < a->u.match_size - ALIGN(sizeof(*a)); i++)
Rusty Russelledf14cf2000-04-19 11:26:44 +00001457 if (((a->data[i] ^ b->data[i]) & (*maskptr)[i]) != 0)
Rusty Russell90e712a2000-03-29 04:19:26 +00001458 return 1;
Rusty Russelledf14cf2000-04-19 11:26:44 +00001459 *maskptr += i;
1460 return 0;
1461}
1462
1463static inline int
Rusty Russell733e54b2004-12-16 14:22:23 +00001464target_same(struct rule_head *a, struct rule_head *b,const unsigned char *mask)
Rusty Russelledf14cf2000-04-19 11:26:44 +00001465{
1466 unsigned int i;
Rusty Russell733e54b2004-12-16 14:22:23 +00001467 STRUCT_ENTRY_TARGET *ta, *tb;
Marc Bouchere6869a82000-03-20 06:03:29 +00001468
Rusty Russell733e54b2004-12-16 14:22:23 +00001469 if (a->type != b->type)
1470 return 0;
1471
1472 ta = GET_TARGET(a->entry);
1473 tb = GET_TARGET(b->entry);
1474
1475 switch (a->type) {
1476 case IPTCC_R_FALLTHROUGH:
1477 return 1;
1478 case IPTCC_R_JUMP:
1479 return a->jump == b->jump;
1480 case IPTCC_R_STANDARD:
1481 return ((STRUCT_STANDARD_TARGET *)ta)->verdict
1482 == ((STRUCT_STANDARD_TARGET *)tb)->verdict;
1483 case IPTCC_R_MODULE:
1484 if (ta->u.target_size != tb->u.target_size)
1485 return 0;
1486 if (strcmp(ta->u.user.name, tb->u.user.name) != 0)
1487 return 0;
1488
1489 for (i = 0; i < ta->u.target_size - sizeof(*ta); i++)
Rusty Russelldaade442004-12-29 11:14:52 +00001490 if (((ta->data[i] ^ tb->data[i]) & mask[i]) != 0)
Rusty Russell733e54b2004-12-16 14:22:23 +00001491 return 0;
1492 return 1;
1493 default:
1494 fprintf(stderr, "ERROR: bad type %i\n", a->type);
1495 abort();
1496 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001497}
1498
Rusty Russell733e54b2004-12-16 14:22:23 +00001499static unsigned char *
Rusty Russell79dee072000-05-02 16:45:16 +00001500is_same(const STRUCT_ENTRY *a,
1501 const STRUCT_ENTRY *b,
1502 unsigned char *matchmask);
Marc Bouchere6869a82000-03-20 06:03:29 +00001503
Harald Welte0113fe72004-01-06 19:04:02 +00001504/* Delete the first rule in `chain' which matches `fw'. */
Marc Bouchere6869a82000-03-20 06:03:29 +00001505int
Rusty Russell79dee072000-05-02 16:45:16 +00001506TC_DELETE_ENTRY(const IPT_CHAINLABEL chain,
1507 const STRUCT_ENTRY *origfw,
1508 unsigned char *matchmask,
1509 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001510{
Harald Welteaae69be2004-08-29 23:32:14 +00001511 struct chain_head *c;
Rusty Russelle45c7132004-12-16 13:21:44 +00001512 struct rule_head *r, *i;
Marc Bouchere6869a82000-03-20 06:03:29 +00001513
Rusty Russell79dee072000-05-02 16:45:16 +00001514 iptc_fn = TC_DELETE_ENTRY;
Harald Welteaae69be2004-08-29 23:32:14 +00001515 if (!(c = iptcc_find_label(chain, *handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001516 errno = ENOENT;
1517 return 0;
1518 }
1519
Rusty Russelle45c7132004-12-16 13:21:44 +00001520 /* Create a rule_head from origfw. */
1521 r = iptcc_alloc_rule(c, origfw->next_offset);
1522 if (!r) {
Harald Welte0113fe72004-01-06 19:04:02 +00001523 errno = ENOMEM;
1524 return 0;
1525 }
1526
Rusty Russelle45c7132004-12-16 13:21:44 +00001527 memcpy(r->entry, origfw, origfw->next_offset);
1528 r->counter_map.maptype = COUNTER_MAP_NOMAP;
1529 if (!iptcc_map_target(*handle, r)) {
1530 DEBUGP("unable to map target of rule for chain `%s'\n", chain);
1531 free(r);
1532 return 0;
Patrick McHardyJesper Brouer04a1e4c2006-07-25 01:50:48 +00001533 } else {
1534 /* iptcc_map_target increment target chain references
1535 * since this is a fake rule only used for matching
1536 * the chain references count is decremented again.
1537 */
1538 if (r->type == IPTCC_R_JUMP
1539 && r->jump)
1540 r->jump->references--;
Rusty Russelle45c7132004-12-16 13:21:44 +00001541 }
Harald Welte0113fe72004-01-06 19:04:02 +00001542
Rusty Russelle45c7132004-12-16 13:21:44 +00001543 list_for_each_entry(i, &c->rules, list) {
Rusty Russell733e54b2004-12-16 14:22:23 +00001544 unsigned char *mask;
Harald Weltefe537072004-08-30 20:28:53 +00001545
Rusty Russell733e54b2004-12-16 14:22:23 +00001546 mask = is_same(r->entry, i->entry, matchmask);
1547 if (!mask)
1548 continue;
Martin Josefsson2a5dbbb2004-09-22 21:37:41 +00001549
Rusty Russell733e54b2004-12-16 14:22:23 +00001550 if (!target_same(r, i, mask))
1551 continue;
1552
1553 /* If we are about to delete the rule that is the
1554 * current iterator, move rule iterator back. next
1555 * pointer will then point to real next node */
1556 if (i == (*handle)->rule_iterator_cur) {
1557 (*handle)->rule_iterator_cur =
1558 list_entry((*handle)->rule_iterator_cur->list.prev,
1559 struct rule_head, list);
Marc Bouchere6869a82000-03-20 06:03:29 +00001560 }
Rusty Russell733e54b2004-12-16 14:22:23 +00001561
1562 c->num_rules--;
1563 iptcc_delete_rule(i);
1564
1565 set_changed(*handle);
1566 free(r);
1567 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00001568 }
1569
Rusty Russelle45c7132004-12-16 13:21:44 +00001570 free(r);
Marc Bouchere6869a82000-03-20 06:03:29 +00001571 errno = ENOENT;
1572 return 0;
Rusty Russell7e53bf92000-03-20 07:03:28 +00001573}
Harald Welteaae69be2004-08-29 23:32:14 +00001574
Marc Bouchere6869a82000-03-20 06:03:29 +00001575
1576/* Delete the rule in position `rulenum' in `chain'. */
1577int
Rusty Russell79dee072000-05-02 16:45:16 +00001578TC_DELETE_NUM_ENTRY(const IPT_CHAINLABEL chain,
1579 unsigned int rulenum,
1580 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001581{
Harald Welteaae69be2004-08-29 23:32:14 +00001582 struct chain_head *c;
1583 struct rule_head *r;
Marc Bouchere6869a82000-03-20 06:03:29 +00001584
Rusty Russell79dee072000-05-02 16:45:16 +00001585 iptc_fn = TC_DELETE_NUM_ENTRY;
Harald Welteaae69be2004-08-29 23:32:14 +00001586
1587 if (!(c = iptcc_find_label(chain, *handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001588 errno = ENOENT;
1589 return 0;
1590 }
1591
Martin Josefssona5616dc2004-10-24 22:27:31 +00001592 if (rulenum >= c->num_rules) {
Martin Josefsson631f3612004-09-23 19:25:06 +00001593 errno = E2BIG;
1594 return 0;
1595 }
1596
1597 /* Take advantage of the double linked list if possible. */
Martin Josefssona5616dc2004-10-24 22:27:31 +00001598 if (rulenum + 1 <= c->num_rules/2) {
1599 r = iptcc_get_rule_num(c, rulenum + 1);
1600 } else {
1601 r = iptcc_get_rule_num_reverse(c, c->num_rules - rulenum);
Marc Bouchere6869a82000-03-20 06:03:29 +00001602 }
1603
Harald Welteaae69be2004-08-29 23:32:14 +00001604 /* If we are about to delete the rule that is the current
1605 * iterator, move rule iterator back. next pointer will then
1606 * point to real next node */
1607 if (r == (*handle)->rule_iterator_cur) {
1608 (*handle)->rule_iterator_cur =
1609 list_entry((*handle)->rule_iterator_cur->list.prev,
1610 struct rule_head, list);
Harald Welte0113fe72004-01-06 19:04:02 +00001611 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001612
Harald Welteaae69be2004-08-29 23:32:14 +00001613 c->num_rules--;
1614 iptcc_delete_rule(r);
1615
Martin Josefsson2a5dbbb2004-09-22 21:37:41 +00001616 set_changed(*handle);
1617
Harald Welteaae69be2004-08-29 23:32:14 +00001618 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00001619}
1620
1621/* Check the packet `fw' on chain `chain'. Returns the verdict, or
1622 NULL and sets errno. */
1623const char *
Rusty Russell79dee072000-05-02 16:45:16 +00001624TC_CHECK_PACKET(const IPT_CHAINLABEL chain,
1625 STRUCT_ENTRY *entry,
1626 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001627{
Derrik Pates664c0a32005-02-01 13:28:14 +00001628 iptc_fn = TC_CHECK_PACKET;
Marc Bouchere6869a82000-03-20 06:03:29 +00001629 errno = ENOSYS;
1630 return NULL;
1631}
1632
1633/* Flushes the entries in the given chain (ie. empties chain). */
1634int
Rusty Russell79dee072000-05-02 16:45:16 +00001635TC_FLUSH_ENTRIES(const IPT_CHAINLABEL chain, TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001636{
Harald Welteaae69be2004-08-29 23:32:14 +00001637 struct chain_head *c;
1638 struct rule_head *r, *tmp;
Marc Bouchere6869a82000-03-20 06:03:29 +00001639
Harald Welte0113fe72004-01-06 19:04:02 +00001640 iptc_fn = TC_FLUSH_ENTRIES;
Harald Welteaae69be2004-08-29 23:32:14 +00001641 if (!(c = iptcc_find_label(chain, *handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001642 errno = ENOENT;
1643 return 0;
1644 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001645
Harald Welteaae69be2004-08-29 23:32:14 +00001646 list_for_each_entry_safe(r, tmp, &c->rules, list) {
1647 iptcc_delete_rule(r);
1648 }
1649
1650 c->num_rules = 0;
1651
1652 set_changed(*handle);
1653
1654 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00001655}
1656
1657/* Zeroes the counters in a chain. */
1658int
Rusty Russell79dee072000-05-02 16:45:16 +00001659TC_ZERO_ENTRIES(const IPT_CHAINLABEL chain, TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001660{
Harald Welteaae69be2004-08-29 23:32:14 +00001661 struct chain_head *c;
1662 struct rule_head *r;
Rusty Russell7e53bf92000-03-20 07:03:28 +00001663
Derrik Pates664c0a32005-02-01 13:28:14 +00001664 iptc_fn = TC_ZERO_ENTRIES;
Harald Welteaae69be2004-08-29 23:32:14 +00001665 if (!(c = iptcc_find_label(chain, *handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001666 errno = ENOENT;
1667 return 0;
1668 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001669
Andy Gaye5bd1d72006-08-22 02:56:41 +00001670 if (c->counter_map.maptype == COUNTER_MAP_NORMAL_MAP)
1671 c->counter_map.maptype = COUNTER_MAP_ZEROED;
1672
Harald Welteaae69be2004-08-29 23:32:14 +00001673 list_for_each_entry(r, &c->rules, list) {
1674 if (r->counter_map.maptype == COUNTER_MAP_NORMAL_MAP)
1675 r->counter_map.maptype = COUNTER_MAP_ZEROED;
Marc Bouchere6869a82000-03-20 06:03:29 +00001676 }
Harald Welteaae69be2004-08-29 23:32:14 +00001677
Rusty Russell175f6412000-03-24 09:32:20 +00001678 set_changed(*handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001679
Marc Bouchere6869a82000-03-20 06:03:29 +00001680 return 1;
1681}
1682
Harald Welte1cef74d2001-01-05 15:22:59 +00001683STRUCT_COUNTERS *
1684TC_READ_COUNTER(const IPT_CHAINLABEL chain,
1685 unsigned int rulenum,
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
1691 iptc_fn = TC_READ_COUNTER;
1692 CHECK(*handle);
1693
Harald Welteaae69be2004-08-29 23:32:14 +00001694 if (!(c = iptcc_find_label(chain, *handle))) {
Harald Welte1cef74d2001-01-05 15:22:59 +00001695 errno = ENOENT;
1696 return NULL;
1697 }
1698
Harald Welteaae69be2004-08-29 23:32:14 +00001699 if (!(r = iptcc_get_rule_num(c, rulenum))) {
Harald Welte0113fe72004-01-06 19:04:02 +00001700 errno = E2BIG;
1701 return NULL;
1702 }
1703
Harald Welteaae69be2004-08-29 23:32:14 +00001704 return &r->entry[0].counters;
Harald Welte1cef74d2001-01-05 15:22:59 +00001705}
1706
1707int
1708TC_ZERO_COUNTER(const IPT_CHAINLABEL chain,
1709 unsigned int rulenum,
1710 TC_HANDLE_T *handle)
1711{
Harald Welteaae69be2004-08-29 23:32:14 +00001712 struct chain_head *c;
1713 struct rule_head *r;
Harald Welte1cef74d2001-01-05 15:22:59 +00001714
1715 iptc_fn = TC_ZERO_COUNTER;
1716 CHECK(*handle);
1717
Harald Welteaae69be2004-08-29 23:32:14 +00001718 if (!(c = iptcc_find_label(chain, *handle))) {
Harald Welte1cef74d2001-01-05 15:22:59 +00001719 errno = ENOENT;
1720 return 0;
1721 }
1722
Harald Welteaae69be2004-08-29 23:32:14 +00001723 if (!(r = iptcc_get_rule_num(c, rulenum))) {
Harald Welte0113fe72004-01-06 19:04:02 +00001724 errno = E2BIG;
1725 return 0;
1726 }
1727
Harald Welteaae69be2004-08-29 23:32:14 +00001728 if (r->counter_map.maptype == COUNTER_MAP_NORMAL_MAP)
1729 r->counter_map.maptype = COUNTER_MAP_ZEROED;
Harald Welte1cef74d2001-01-05 15:22:59 +00001730
1731 set_changed(*handle);
1732
1733 return 1;
1734}
1735
1736int
1737TC_SET_COUNTER(const IPT_CHAINLABEL chain,
1738 unsigned int rulenum,
1739 STRUCT_COUNTERS *counters,
1740 TC_HANDLE_T *handle)
1741{
Harald Welteaae69be2004-08-29 23:32:14 +00001742 struct chain_head *c;
1743 struct rule_head *r;
Harald Welte1cef74d2001-01-05 15:22:59 +00001744 STRUCT_ENTRY *e;
Harald Welte1cef74d2001-01-05 15:22:59 +00001745
1746 iptc_fn = TC_SET_COUNTER;
1747 CHECK(*handle);
1748
Harald Welteaae69be2004-08-29 23:32:14 +00001749 if (!(c = iptcc_find_label(chain, *handle))) {
Harald Welte1cef74d2001-01-05 15:22:59 +00001750 errno = ENOENT;
1751 return 0;
1752 }
Harald Welte0113fe72004-01-06 19:04:02 +00001753
Harald Welteaae69be2004-08-29 23:32:14 +00001754 if (!(r = iptcc_get_rule_num(c, rulenum))) {
Harald Welte0113fe72004-01-06 19:04:02 +00001755 errno = E2BIG;
1756 return 0;
1757 }
1758
Harald Welteaae69be2004-08-29 23:32:14 +00001759 e = r->entry;
1760 r->counter_map.maptype = COUNTER_MAP_SET;
Harald Welte0113fe72004-01-06 19:04:02 +00001761
1762 memcpy(&e->counters, counters, sizeof(STRUCT_COUNTERS));
Harald Welte1cef74d2001-01-05 15:22:59 +00001763
1764 set_changed(*handle);
1765
1766 return 1;
1767}
1768
Marc Bouchere6869a82000-03-20 06:03:29 +00001769/* Creates a new chain. */
1770/* To create a chain, create two rules: error node and unconditional
1771 * return. */
1772int
Rusty Russell79dee072000-05-02 16:45:16 +00001773TC_CREATE_CHAIN(const IPT_CHAINLABEL chain, TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001774{
Harald Welteaae69be2004-08-29 23:32:14 +00001775 static struct chain_head *c;
Marc Bouchere6869a82000-03-20 06:03:29 +00001776
Rusty Russell79dee072000-05-02 16:45:16 +00001777 iptc_fn = TC_CREATE_CHAIN;
Marc Bouchere6869a82000-03-20 06:03:29 +00001778
1779 /* find_label doesn't cover built-in targets: DROP, ACCEPT,
1780 QUEUE, RETURN. */
Harald Welteaae69be2004-08-29 23:32:14 +00001781 if (iptcc_find_label(chain, *handle)
Rusty Russell79dee072000-05-02 16:45:16 +00001782 || strcmp(chain, LABEL_DROP) == 0
1783 || strcmp(chain, LABEL_ACCEPT) == 0
Rusty Russell67088e72000-05-10 01:18:57 +00001784 || strcmp(chain, LABEL_QUEUE) == 0
Rusty Russell79dee072000-05-02 16:45:16 +00001785 || strcmp(chain, LABEL_RETURN) == 0) {
Harald Welteaae69be2004-08-29 23:32:14 +00001786 DEBUGP("Chain `%s' already exists\n", chain);
Marc Bouchere6869a82000-03-20 06:03:29 +00001787 errno = EEXIST;
1788 return 0;
1789 }
1790
Rusty Russell79dee072000-05-02 16:45:16 +00001791 if (strlen(chain)+1 > sizeof(IPT_CHAINLABEL)) {
Harald Welteaae69be2004-08-29 23:32:14 +00001792 DEBUGP("Chain name `%s' too long\n", chain);
Marc Bouchere6869a82000-03-20 06:03:29 +00001793 errno = EINVAL;
1794 return 0;
1795 }
1796
Harald Welteaae69be2004-08-29 23:32:14 +00001797 c = iptcc_alloc_chain_head(chain, 0);
1798 if (!c) {
1799 DEBUGP("Cannot allocate memory for chain `%s'\n", chain);
1800 errno = ENOMEM;
1801 return 0;
Marc Bouchere6869a82000-03-20 06:03:29 +00001802
Harald Welteaae69be2004-08-29 23:32:14 +00001803 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001804
Harald Welteaae69be2004-08-29 23:32:14 +00001805 DEBUGP("Creating chain `%s'\n", chain);
Jesper Dangaard Brouerd8cb7872007-11-28 08:40:26 +00001806 iptc_insert_chain(*handle, c); /* Insert sorted */
Harald Welte0113fe72004-01-06 19:04:02 +00001807
1808 set_changed(*handle);
1809
Harald Welteaae69be2004-08-29 23:32:14 +00001810 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00001811}
1812
1813/* Get the number of references to this chain. */
1814int
Rusty Russell79dee072000-05-02 16:45:16 +00001815TC_GET_REFERENCES(unsigned int *ref, const IPT_CHAINLABEL chain,
1816 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001817{
Harald Welteaae69be2004-08-29 23:32:14 +00001818 struct chain_head *c;
Marc Bouchere6869a82000-03-20 06:03:29 +00001819
Derrik Pates664c0a32005-02-01 13:28:14 +00001820 iptc_fn = TC_GET_REFERENCES;
Harald Welteaae69be2004-08-29 23:32:14 +00001821 if (!(c = iptcc_find_label(chain, *handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001822 errno = ENOENT;
1823 return 0;
1824 }
1825
Harald Welteaae69be2004-08-29 23:32:14 +00001826 *ref = c->references;
1827
Marc Bouchere6869a82000-03-20 06:03:29 +00001828 return 1;
1829}
1830
1831/* Deletes a chain. */
1832int
Rusty Russell79dee072000-05-02 16:45:16 +00001833TC_DELETE_CHAIN(const IPT_CHAINLABEL chain, TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001834{
Marc Bouchere6869a82000-03-20 06:03:29 +00001835 unsigned int references;
Harald Welteaae69be2004-08-29 23:32:14 +00001836 struct chain_head *c;
Rusty Russell7e53bf92000-03-20 07:03:28 +00001837
Rusty Russell79dee072000-05-02 16:45:16 +00001838 iptc_fn = TC_DELETE_CHAIN;
Marc Bouchere6869a82000-03-20 06:03:29 +00001839
Harald Welteaae69be2004-08-29 23:32:14 +00001840 if (!(c = iptcc_find_label(chain, *handle))) {
1841 DEBUGP("cannot find chain `%s'\n", chain);
Marc Bouchere6869a82000-03-20 06:03:29 +00001842 errno = ENOENT;
1843 return 0;
1844 }
1845
Harald Welteaae69be2004-08-29 23:32:14 +00001846 if (TC_BUILTIN(chain, *handle)) {
1847 DEBUGP("cannot remove builtin chain `%s'\n", chain);
1848 errno = EINVAL;
1849 return 0;
1850 }
1851
1852 if (!TC_GET_REFERENCES(&references, chain, handle)) {
1853 DEBUGP("cannot get references on chain `%s'\n", chain);
1854 return 0;
1855 }
1856
1857 if (references > 0) {
1858 DEBUGP("chain `%s' still has references\n", chain);
1859 errno = EMLINK;
1860 return 0;
1861 }
1862
1863 if (c->num_rules) {
1864 DEBUGP("chain `%s' is not empty\n", chain);
Marc Bouchere6869a82000-03-20 06:03:29 +00001865 errno = ENOTEMPTY;
1866 return 0;
1867 }
1868
Harald Welteaae69be2004-08-29 23:32:14 +00001869 /* If we are about to delete the chain that is the current
1870 * iterator, move chain iterator firward. */
1871 if (c == (*handle)->chain_iterator_cur)
1872 iptcc_chain_iterator_advance(*handle);
Harald Welte0113fe72004-01-06 19:04:02 +00001873
Harald Welteaae69be2004-08-29 23:32:14 +00001874 list_del(&c->list);
1875 free(c);
Harald Welte0113fe72004-01-06 19:04:02 +00001876
Harald Welteaae69be2004-08-29 23:32:14 +00001877 DEBUGP("chain `%s' deleted\n", chain);
1878
1879 set_changed(*handle);
1880
1881 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00001882}
1883
1884/* Renames a chain. */
Rusty Russell79dee072000-05-02 16:45:16 +00001885int TC_RENAME_CHAIN(const IPT_CHAINLABEL oldname,
1886 const IPT_CHAINLABEL newname,
1887 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001888{
Harald Welteaae69be2004-08-29 23:32:14 +00001889 struct chain_head *c;
Rusty Russell79dee072000-05-02 16:45:16 +00001890 iptc_fn = TC_RENAME_CHAIN;
Marc Bouchere6869a82000-03-20 06:03:29 +00001891
Harald Welte1de80462000-10-30 12:00:27 +00001892 /* find_label doesn't cover built-in targets: DROP, ACCEPT,
1893 QUEUE, RETURN. */
Harald Welteaae69be2004-08-29 23:32:14 +00001894 if (iptcc_find_label(newname, *handle)
Rusty Russell79dee072000-05-02 16:45:16 +00001895 || strcmp(newname, LABEL_DROP) == 0
1896 || strcmp(newname, LABEL_ACCEPT) == 0
Harald Welte1de80462000-10-30 12:00:27 +00001897 || strcmp(newname, LABEL_QUEUE) == 0
Rusty Russell79dee072000-05-02 16:45:16 +00001898 || strcmp(newname, LABEL_RETURN) == 0) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001899 errno = EEXIST;
1900 return 0;
1901 }
1902
Harald Welteaae69be2004-08-29 23:32:14 +00001903 if (!(c = iptcc_find_label(oldname, *handle))
Rusty Russell79dee072000-05-02 16:45:16 +00001904 || TC_BUILTIN(oldname, *handle)) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001905 errno = ENOENT;
1906 return 0;
1907 }
1908
Rusty Russell79dee072000-05-02 16:45:16 +00001909 if (strlen(newname)+1 > sizeof(IPT_CHAINLABEL)) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001910 errno = EINVAL;
1911 return 0;
1912 }
1913
Harald Welteaae69be2004-08-29 23:32:14 +00001914 strncpy(c->name, newname, sizeof(IPT_CHAINLABEL));
1915
Harald Welte0113fe72004-01-06 19:04:02 +00001916 set_changed(*handle);
1917
Marc Bouchere6869a82000-03-20 06:03:29 +00001918 return 1;
1919}
1920
1921/* Sets the policy on a built-in chain. */
1922int
Rusty Russell79dee072000-05-02 16:45:16 +00001923TC_SET_POLICY(const IPT_CHAINLABEL chain,
1924 const IPT_CHAINLABEL policy,
Harald Welte1cef74d2001-01-05 15:22:59 +00001925 STRUCT_COUNTERS *counters,
Rusty Russell79dee072000-05-02 16:45:16 +00001926 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001927{
Harald Welteaae69be2004-08-29 23:32:14 +00001928 struct chain_head *c;
Marc Bouchere6869a82000-03-20 06:03:29 +00001929
Rusty Russell79dee072000-05-02 16:45:16 +00001930 iptc_fn = TC_SET_POLICY;
Marc Bouchere6869a82000-03-20 06:03:29 +00001931
Harald Welteaae69be2004-08-29 23:32:14 +00001932 if (!(c = iptcc_find_label(chain, *handle))) {
1933 DEBUGP("cannot find chain `%s'\n", chain);
1934 errno = ENOENT;
Marc Bouchere6869a82000-03-20 06:03:29 +00001935 return 0;
1936 }
1937
Harald Welteaae69be2004-08-29 23:32:14 +00001938 if (!iptcc_is_builtin(c)) {
1939 DEBUGP("cannot set policy of userdefinedchain `%s'\n", chain);
1940 errno = ENOENT;
1941 return 0;
1942 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001943
Rusty Russell79dee072000-05-02 16:45:16 +00001944 if (strcmp(policy, LABEL_ACCEPT) == 0)
Harald Welteaae69be2004-08-29 23:32:14 +00001945 c->verdict = -NF_ACCEPT - 1;
Rusty Russell79dee072000-05-02 16:45:16 +00001946 else if (strcmp(policy, LABEL_DROP) == 0)
Harald Welteaae69be2004-08-29 23:32:14 +00001947 c->verdict = -NF_DROP - 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00001948 else {
1949 errno = EINVAL;
1950 return 0;
1951 }
Harald Welte1cef74d2001-01-05 15:22:59 +00001952
Harald Welte1cef74d2001-01-05 15:22:59 +00001953 if (counters) {
1954 /* set byte and packet counters */
Harald Welteaae69be2004-08-29 23:32:14 +00001955 memcpy(&c->counters, counters, sizeof(STRUCT_COUNTERS));
1956 c->counter_map.maptype = COUNTER_MAP_SET;
Harald Welte1cef74d2001-01-05 15:22:59 +00001957 } else {
Harald Welteaae69be2004-08-29 23:32:14 +00001958 c->counter_map.maptype = COUNTER_MAP_NOMAP;
Harald Welte1cef74d2001-01-05 15:22:59 +00001959 }
1960
Rusty Russell175f6412000-03-24 09:32:20 +00001961 set_changed(*handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001962
Marc Bouchere6869a82000-03-20 06:03:29 +00001963 return 1;
1964}
1965
1966/* Without this, on gcc 2.7.2.3, we get:
Rusty Russell79dee072000-05-02 16:45:16 +00001967 libiptc.c: In function `TC_COMMIT':
Marc Bouchere6869a82000-03-20 06:03:29 +00001968 libiptc.c:833: fixed or forbidden register was spilled.
1969 This may be due to a compiler bug or to impossible asm
1970 statements or clauses.
1971*/
1972static void
Rusty Russell79dee072000-05-02 16:45:16 +00001973subtract_counters(STRUCT_COUNTERS *answer,
1974 const STRUCT_COUNTERS *a,
1975 const STRUCT_COUNTERS *b)
Marc Bouchere6869a82000-03-20 06:03:29 +00001976{
1977 answer->pcnt = a->pcnt - b->pcnt;
1978 answer->bcnt = a->bcnt - b->bcnt;
1979}
1980
Harald Welteaae69be2004-08-29 23:32:14 +00001981
1982static void counters_nomap(STRUCT_COUNTERS_INFO *newcounters,
1983 unsigned int index)
1984{
1985 newcounters->counters[index] = ((STRUCT_COUNTERS) { 0, 0});
1986 DEBUGP_C("NOMAP => zero\n");
1987}
1988
1989static void counters_normal_map(STRUCT_COUNTERS_INFO *newcounters,
1990 STRUCT_REPLACE *repl,
1991 unsigned int index,
1992 unsigned int mappos)
1993{
1994 /* Original read: X.
1995 * Atomic read on replacement: X + Y.
1996 * Currently in kernel: Z.
1997 * Want in kernel: X + Y + Z.
1998 * => Add in X + Y
1999 * => Add in replacement read.
2000 */
2001 newcounters->counters[index] = repl->counters[mappos];
2002 DEBUGP_C("NORMAL_MAP => mappos %u \n", mappos);
2003}
2004
2005static void counters_map_zeroed(STRUCT_COUNTERS_INFO *newcounters,
2006 STRUCT_REPLACE *repl,
2007 unsigned int index,
2008 unsigned int mappos,
2009 STRUCT_COUNTERS *counters)
2010{
2011 /* Original read: X.
2012 * Atomic read on replacement: X + Y.
2013 * Currently in kernel: Z.
2014 * Want in kernel: Y + Z.
2015 * => Add in Y.
2016 * => Add in (replacement read - original read).
2017 */
2018 subtract_counters(&newcounters->counters[index],
2019 &repl->counters[mappos],
2020 counters);
2021 DEBUGP_C("ZEROED => mappos %u\n", mappos);
2022}
2023
2024static void counters_map_set(STRUCT_COUNTERS_INFO *newcounters,
2025 unsigned int index,
2026 STRUCT_COUNTERS *counters)
2027{
2028 /* Want to set counter (iptables-restore) */
2029
2030 memcpy(&newcounters->counters[index], counters,
2031 sizeof(STRUCT_COUNTERS));
2032
2033 DEBUGP_C("SET\n");
2034}
2035
2036
Marc Bouchere6869a82000-03-20 06:03:29 +00002037int
Rusty Russell79dee072000-05-02 16:45:16 +00002038TC_COMMIT(TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00002039{
2040 /* Replace, then map back the counters. */
Rusty Russell79dee072000-05-02 16:45:16 +00002041 STRUCT_REPLACE *repl;
2042 STRUCT_COUNTERS_INFO *newcounters;
Harald Welteaae69be2004-08-29 23:32:14 +00002043 struct chain_head *c;
2044 int ret;
Martin Josefsson841e4ae2003-05-02 15:30:11 +00002045 size_t counterlen;
Harald Welteaae69be2004-08-29 23:32:14 +00002046 int new_number;
2047 unsigned int new_size;
Marc Bouchere6869a82000-03-20 06:03:29 +00002048
Derrik Pates664c0a32005-02-01 13:28:14 +00002049 iptc_fn = TC_COMMIT;
Marc Bouchere6869a82000-03-20 06:03:29 +00002050 CHECK(*handle);
Martin Josefsson841e4ae2003-05-02 15:30:11 +00002051
Marc Bouchere6869a82000-03-20 06:03:29 +00002052 /* Don't commit if nothing changed. */
2053 if (!(*handle)->changed)
2054 goto finished;
2055
Harald Welteaae69be2004-08-29 23:32:14 +00002056 new_number = iptcc_compile_table_prep(*handle, &new_size);
2057 if (new_number < 0) {
2058 errno = ENOMEM;
Harald Welted6ba6f52005-11-12 10:39:40 +00002059 goto out_zero;
Harald Welteaae69be2004-08-29 23:32:14 +00002060 }
2061
2062 repl = malloc(sizeof(*repl) + new_size);
Marc Bouchere6869a82000-03-20 06:03:29 +00002063 if (!repl) {
2064 errno = ENOMEM;
Harald Welted6ba6f52005-11-12 10:39:40 +00002065 goto out_zero;
Marc Bouchere6869a82000-03-20 06:03:29 +00002066 }
Martin Josefssonad3b4f92004-09-22 22:04:07 +00002067 memset(repl, 0, sizeof(*repl) + new_size);
Harald Welteaae69be2004-08-29 23:32:14 +00002068
Rusty Russelle45c7132004-12-16 13:21:44 +00002069#if 0
2070 TC_DUMP_ENTRIES(*handle);
2071#endif
2072
Harald Welteaae69be2004-08-29 23:32:14 +00002073 counterlen = sizeof(STRUCT_COUNTERS_INFO)
2074 + sizeof(STRUCT_COUNTERS) * new_number;
Marc Bouchere6869a82000-03-20 06:03:29 +00002075
2076 /* These are the old counters we will get from kernel */
Rusty Russell79dee072000-05-02 16:45:16 +00002077 repl->counters = malloc(sizeof(STRUCT_COUNTERS)
Marc Bouchere6869a82000-03-20 06:03:29 +00002078 * (*handle)->info.num_entries);
2079 if (!repl->counters) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002080 errno = ENOMEM;
Harald Welted6ba6f52005-11-12 10:39:40 +00002081 goto out_free_repl;
Marc Bouchere6869a82000-03-20 06:03:29 +00002082 }
Marc Bouchere6869a82000-03-20 06:03:29 +00002083 /* These are the counters we're going to put back, later. */
2084 newcounters = malloc(counterlen);
2085 if (!newcounters) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002086 errno = ENOMEM;
Harald Welted6ba6f52005-11-12 10:39:40 +00002087 goto out_free_repl_counters;
Marc Bouchere6869a82000-03-20 06:03:29 +00002088 }
Harald Welteaae69be2004-08-29 23:32:14 +00002089 memset(newcounters, 0, counterlen);
Marc Bouchere6869a82000-03-20 06:03:29 +00002090
2091 strcpy(repl->name, (*handle)->info.name);
Harald Welteaae69be2004-08-29 23:32:14 +00002092 repl->num_entries = new_number;
2093 repl->size = new_size;
2094
Marc Bouchere6869a82000-03-20 06:03:29 +00002095 repl->num_counters = (*handle)->info.num_entries;
2096 repl->valid_hooks = (*handle)->info.valid_hooks;
Harald Welteaae69be2004-08-29 23:32:14 +00002097
2098 DEBUGP("num_entries=%u, size=%u, num_counters=%u\n",
2099 repl->num_entries, repl->size, repl->num_counters);
2100
2101 ret = iptcc_compile_table(*handle, repl);
2102 if (ret < 0) {
2103 errno = ret;
Harald Welted6ba6f52005-11-12 10:39:40 +00002104 goto out_free_newcounters;
Harald Welteaae69be2004-08-29 23:32:14 +00002105 }
2106
2107
2108#ifdef IPTC_DEBUG2
2109 {
2110 int fd = open("/tmp/libiptc-so_set_replace.blob",
2111 O_CREAT|O_WRONLY);
2112 if (fd >= 0) {
2113 write(fd, repl, sizeof(*repl) + repl->size);
2114 close(fd);
2115 }
2116 }
2117#endif
Marc Bouchere6869a82000-03-20 06:03:29 +00002118
Harald Welted6ba6f52005-11-12 10:39:40 +00002119 ret = setsockopt(sockfd, TC_IPPROTO, SO_SET_REPLACE, repl,
2120 sizeof(*repl) + repl->size);
Patrick McHardye0865ad2006-04-22 02:08:56 +00002121 if (ret < 0)
Harald Welted6ba6f52005-11-12 10:39:40 +00002122 goto out_free_newcounters;
Marc Bouchere6869a82000-03-20 06:03:29 +00002123
2124 /* Put counters back. */
2125 strcpy(newcounters->name, (*handle)->info.name);
Harald Welteaae69be2004-08-29 23:32:14 +00002126 newcounters->num_counters = new_number;
Marc Bouchere6869a82000-03-20 06:03:29 +00002127
Harald Welteaae69be2004-08-29 23:32:14 +00002128 list_for_each_entry(c, &(*handle)->chains, list) {
2129 struct rule_head *r;
Marc Bouchere6869a82000-03-20 06:03:29 +00002130
Harald Welteaae69be2004-08-29 23:32:14 +00002131 /* Builtin chains have their own counters */
2132 if (iptcc_is_builtin(c)) {
2133 DEBUGP("counter for chain-index %u: ", c->foot_index);
2134 switch(c->counter_map.maptype) {
2135 case COUNTER_MAP_NOMAP:
2136 counters_nomap(newcounters, c->foot_index);
2137 break;
2138 case COUNTER_MAP_NORMAL_MAP:
2139 counters_normal_map(newcounters, repl,
2140 c->foot_index,
2141 c->counter_map.mappos);
2142 break;
2143 case COUNTER_MAP_ZEROED:
2144 counters_map_zeroed(newcounters, repl,
2145 c->foot_index,
2146 c->counter_map.mappos,
2147 &c->counters);
2148 break;
2149 case COUNTER_MAP_SET:
2150 counters_map_set(newcounters, c->foot_index,
2151 &c->counters);
2152 break;
2153 }
2154 }
Harald Welte1cef74d2001-01-05 15:22:59 +00002155
Harald Welteaae69be2004-08-29 23:32:14 +00002156 list_for_each_entry(r, &c->rules, list) {
2157 DEBUGP("counter for index %u: ", r->index);
2158 switch (r->counter_map.maptype) {
2159 case COUNTER_MAP_NOMAP:
2160 counters_nomap(newcounters, r->index);
2161 break;
Harald Welte1cef74d2001-01-05 15:22:59 +00002162
Harald Welteaae69be2004-08-29 23:32:14 +00002163 case COUNTER_MAP_NORMAL_MAP:
2164 counters_normal_map(newcounters, repl,
2165 r->index,
2166 r->counter_map.mappos);
2167 break;
Harald Welte1cef74d2001-01-05 15:22:59 +00002168
Harald Welteaae69be2004-08-29 23:32:14 +00002169 case COUNTER_MAP_ZEROED:
2170 counters_map_zeroed(newcounters, repl,
2171 r->index,
2172 r->counter_map.mappos,
2173 &r->entry->counters);
2174 break;
2175
2176 case COUNTER_MAP_SET:
2177 counters_map_set(newcounters, r->index,
2178 &r->entry->counters);
2179 break;
2180 }
Marc Bouchere6869a82000-03-20 06:03:29 +00002181 }
2182 }
Rusty Russell62527ce2000-09-04 09:45:54 +00002183
Harald Welteaae69be2004-08-29 23:32:14 +00002184#ifdef IPTC_DEBUG2
2185 {
2186 int fd = open("/tmp/libiptc-so_set_add_counters.blob",
2187 O_CREAT|O_WRONLY);
2188 if (fd >= 0) {
2189 write(fd, newcounters, counterlen);
2190 close(fd);
2191 }
2192 }
2193#endif
2194
Harald Welted6ba6f52005-11-12 10:39:40 +00002195 ret = setsockopt(sockfd, TC_IPPROTO, SO_SET_ADD_COUNTERS,
2196 newcounters, counterlen);
Patrick McHardye0865ad2006-04-22 02:08:56 +00002197 if (ret < 0)
Harald Welted6ba6f52005-11-12 10:39:40 +00002198 goto out_free_newcounters;
Marc Bouchere6869a82000-03-20 06:03:29 +00002199
2200 free(repl->counters);
2201 free(repl);
2202 free(newcounters);
2203
Harald Welted6ba6f52005-11-12 10:39:40 +00002204finished:
Martin Josefsson841e4ae2003-05-02 15:30:11 +00002205 TC_FREE(handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00002206 return 1;
Harald Welted6ba6f52005-11-12 10:39:40 +00002207
2208out_free_newcounters:
2209 free(newcounters);
2210out_free_repl_counters:
2211 free(repl->counters);
2212out_free_repl:
2213 free(repl);
2214out_zero:
2215 return 0;
Marc Bouchere6869a82000-03-20 06:03:29 +00002216}
2217
2218/* Get raw socket. */
2219int
Patrick McHardy0b639362007-09-08 16:00:01 +00002220TC_GET_RAW_SOCKET(void)
Marc Bouchere6869a82000-03-20 06:03:29 +00002221{
2222 return sockfd;
2223}
2224
2225/* Translates errno numbers into more human-readable form than strerror. */
2226const char *
Rusty Russell79dee072000-05-02 16:45:16 +00002227TC_STRERROR(int err)
Marc Bouchere6869a82000-03-20 06:03:29 +00002228{
2229 unsigned int i;
2230 struct table_struct {
2231 void *fn;
2232 int err;
2233 const char *message;
2234 } table [] =
Harald Welte4ccfa632001-07-30 15:12:43 +00002235 { { TC_INIT, EPERM, "Permission denied (you must be root)" },
Rusty Russell79dee072000-05-02 16:45:16 +00002236 { TC_INIT, EINVAL, "Module is wrong version" },
Harald Welte4ccfa632001-07-30 15:12:43 +00002237 { TC_INIT, ENOENT,
2238 "Table does not exist (do you need to insmod?)" },
Rusty Russell79dee072000-05-02 16:45:16 +00002239 { TC_DELETE_CHAIN, ENOTEMPTY, "Chain is not empty" },
2240 { TC_DELETE_CHAIN, EINVAL, "Can't delete built-in chain" },
2241 { TC_DELETE_CHAIN, EMLINK,
Marc Bouchere6869a82000-03-20 06:03:29 +00002242 "Can't delete chain with references left" },
Rusty Russell79dee072000-05-02 16:45:16 +00002243 { TC_CREATE_CHAIN, EEXIST, "Chain already exists" },
2244 { TC_INSERT_ENTRY, E2BIG, "Index of insertion too big" },
2245 { TC_REPLACE_ENTRY, E2BIG, "Index of replacement too big" },
2246 { TC_DELETE_NUM_ENTRY, E2BIG, "Index of deletion too big" },
Harald Welte1cef74d2001-01-05 15:22:59 +00002247 { TC_READ_COUNTER, E2BIG, "Index of counter too big" },
2248 { TC_ZERO_COUNTER, E2BIG, "Index of counter too big" },
Rusty Russell79dee072000-05-02 16:45:16 +00002249 { TC_INSERT_ENTRY, ELOOP, "Loop found in table" },
2250 { TC_INSERT_ENTRY, EINVAL, "Target problem" },
Marc Bouchere6869a82000-03-20 06:03:29 +00002251 /* EINVAL for CHECK probably means bad interface. */
Rusty Russell79dee072000-05-02 16:45:16 +00002252 { TC_CHECK_PACKET, EINVAL,
Marc Boucherc8264992000-04-22 22:34:44 +00002253 "Bad arguments (does that interface exist?)" },
Harald Welte4ccfa632001-07-30 15:12:43 +00002254 { TC_CHECK_PACKET, ENOSYS,
2255 "Checking will most likely never get implemented" },
Marc Bouchere6869a82000-03-20 06:03:29 +00002256 /* ENOENT for DELETE probably means no matching rule */
Rusty Russell79dee072000-05-02 16:45:16 +00002257 { TC_DELETE_ENTRY, ENOENT,
Marc Boucherc8264992000-04-22 22:34:44 +00002258 "Bad rule (does a matching rule exist in that chain?)" },
Rusty Russell79dee072000-05-02 16:45:16 +00002259 { TC_SET_POLICY, ENOENT,
Marc Boucherc8264992000-04-22 22:34:44 +00002260 "Bad built-in chain name" },
Rusty Russell79dee072000-05-02 16:45:16 +00002261 { TC_SET_POLICY, EINVAL,
Marc Boucherc8264992000-04-22 22:34:44 +00002262 "Bad policy name" },
Harald Welte4ccfa632001-07-30 15:12:43 +00002263
2264 { NULL, 0, "Incompatible with this kernel" },
2265 { NULL, ENOPROTOOPT, "iptables who? (do you need to insmod?)" },
2266 { NULL, ENOSYS, "Will be implemented real soon. I promise ;)" },
2267 { NULL, ENOMEM, "Memory allocation problem" },
2268 { NULL, ENOENT, "No chain/target/match by that name" },
Marc Bouchere6869a82000-03-20 06:03:29 +00002269 };
2270
2271 for (i = 0; i < sizeof(table)/sizeof(struct table_struct); i++) {
2272 if ((!table[i].fn || table[i].fn == iptc_fn)
2273 && table[i].err == err)
2274 return table[i].message;
2275 }
2276
2277 return strerror(err);
2278}