blob: 059850503c32eb1ba5b20d58da23a2fe27611d96 [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 */
Rusty Russell175f6412000-03-24 09:32:20 +0000169static 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) */
271static unsigned int iptcc_is_builtin(struct chain_head *c)
272{
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{
418 __iptcc_p_del_policy(h, *num);
419
420 c->head_offset = offset;
421 c->index = *num;
422
Jesper Dangaard Brouerd8cb7872007-11-28 08:40:26 +0000423 list_add_tail(&c->list, &h->chains); /* Its already sorted */
424
Harald Welteaae69be2004-08-29 23:32:14 +0000425 h->chain_iterator_cur = c;
426}
427
428/* main parser function: add an entry from the blob to the cache */
429static int cache_add_entry(STRUCT_ENTRY *e,
430 TC_HANDLE_T h,
431 STRUCT_ENTRY **prev,
432 unsigned int *num)
433{
434 unsigned int builtin;
435 unsigned int offset = (char *)e - (char *)h->entries->entrytable;
436
437 DEBUGP("entering...");
438
439 /* Last entry ("policy rule"). End it.*/
440 if (iptcb_entry2offset(h,e) + e->next_offset == h->entries->size) {
441 /* This is the ERROR node at the end of the chain */
442 DEBUGP_C("%u:%u: end of table:\n", *num, offset);
443
444 __iptcc_p_del_policy(h, *num);
445
446 h->chain_iterator_cur = NULL;
447 goto out_inc;
448 }
449
450 /* We know this is the start of a new chain if it's an ERROR
451 * target, or a hook entry point */
452
453 if (strcmp(GET_TARGET(e)->u.user.name, ERROR_TARGET) == 0) {
454 struct chain_head *c =
455 iptcc_alloc_chain_head((const char *)GET_TARGET(e)->data, 0);
456 DEBUGP_C("%u:%u:new userdefined chain %s: %p\n", *num, offset,
457 (char *)c->name, c);
458 if (!c) {
459 errno = -ENOMEM;
460 return -1;
461 }
462
463 __iptcc_p_add_chain(h, c, offset, num);
464
465 } else if ((builtin = iptcb_ent_is_hook_entry(e, h)) != 0) {
466 struct chain_head *c =
467 iptcc_alloc_chain_head((char *)hooknames[builtin-1],
468 builtin);
469 DEBUGP_C("%u:%u new builtin chain: %p (rules=%p)\n",
470 *num, offset, c, &c->rules);
471 if (!c) {
472 errno = -ENOMEM;
473 return -1;
474 }
475
476 c->hooknum = builtin;
477
478 __iptcc_p_add_chain(h, c, offset, num);
479
480 /* FIXME: this is ugly. */
481 goto new_rule;
482 } else {
483 /* has to be normal rule */
484 struct rule_head *r;
485new_rule:
486
487 if (!(r = iptcc_alloc_rule(h->chain_iterator_cur,
488 e->next_offset))) {
489 errno = ENOMEM;
490 return -1;
491 }
492 DEBUGP_C("%u:%u normal rule: %p: ", *num, offset, r);
493
494 r->index = *num;
495 r->offset = offset;
496 memcpy(r->entry, e, e->next_offset);
497 r->counter_map.maptype = COUNTER_MAP_NORMAL_MAP;
498 r->counter_map.mappos = r->index;
499
500 /* handling of jumps, etc. */
501 if (!strcmp(GET_TARGET(e)->u.user.name, STANDARD_TARGET)) {
502 STRUCT_STANDARD_TARGET *t;
503
504 t = (STRUCT_STANDARD_TARGET *)GET_TARGET(e);
505 if (t->target.u.target_size
506 != ALIGN(sizeof(STRUCT_STANDARD_TARGET))) {
507 errno = EINVAL;
508 return -1;
509 }
510
511 if (t->verdict < 0) {
512 DEBUGP_C("standard, verdict=%d\n", t->verdict);
513 r->type = IPTCC_R_STANDARD;
514 } else if (t->verdict == r->offset+e->next_offset) {
515 DEBUGP_C("fallthrough\n");
516 r->type = IPTCC_R_FALLTHROUGH;
517 } else {
518 DEBUGP_C("jump, target=%u\n", t->verdict);
519 r->type = IPTCC_R_JUMP;
520 /* Jump target fixup has to be deferred
521 * until second pass, since we migh not
522 * yet have parsed the target */
523 }
Martin Josefsson52c38022004-09-22 19:39:40 +0000524 } else {
525 DEBUGP_C("module, target=%s\n", GET_TARGET(e)->u.user.name);
526 r->type = IPTCC_R_MODULE;
Harald Welteaae69be2004-08-29 23:32:14 +0000527 }
528
529 list_add_tail(&r->list, &h->chain_iterator_cur->rules);
Martin Josefsson8d1b38a2004-09-22 21:00:19 +0000530 h->chain_iterator_cur->num_rules++;
Harald Welteaae69be2004-08-29 23:32:14 +0000531 }
532out_inc:
533 (*num)++;
534 return 0;
535}
536
537
538/* parse an iptables blob into it's pieces */
539static int parse_table(TC_HANDLE_T h)
540{
541 STRUCT_ENTRY *prev;
542 unsigned int num = 0;
543 struct chain_head *c;
544
545 /* First pass: over ruleset blob */
546 ENTRY_ITERATE(h->entries->entrytable, h->entries->size,
547 cache_add_entry, h, &prev, &num);
548
549 /* Second pass: fixup parsed data from first pass */
550 list_for_each_entry(c, &h->chains, list) {
551 struct rule_head *r;
552 list_for_each_entry(r, &c->rules, list) {
553 struct chain_head *c;
554 STRUCT_STANDARD_TARGET *t;
555
556 if (r->type != IPTCC_R_JUMP)
557 continue;
558
559 t = (STRUCT_STANDARD_TARGET *)GET_TARGET(r->entry);
560 c = iptcc_find_chain_by_offset(h, t->verdict);
561 if (!c)
562 return -1;
563 r->jump = c;
564 c->references++;
565 }
566 }
567
568 /* FIXME: sort chains */
569
570 return 1;
571}
572
573
574/**********************************************************************
575 * RULESET COMPILATION (cache -> blob)
576 **********************************************************************/
577
578/* Convenience structures */
579struct iptcb_chain_start{
580 STRUCT_ENTRY e;
581 struct ipt_error_target name;
582};
583#define IPTCB_CHAIN_START_SIZE (sizeof(STRUCT_ENTRY) + \
584 ALIGN(sizeof(struct ipt_error_target)))
585
586struct iptcb_chain_foot {
587 STRUCT_ENTRY e;
588 STRUCT_STANDARD_TARGET target;
589};
590#define IPTCB_CHAIN_FOOT_SIZE (sizeof(STRUCT_ENTRY) + \
591 ALIGN(sizeof(STRUCT_STANDARD_TARGET)))
592
593struct iptcb_chain_error {
594 STRUCT_ENTRY entry;
595 struct ipt_error_target target;
596};
597#define IPTCB_CHAIN_ERROR_SIZE (sizeof(STRUCT_ENTRY) + \
598 ALIGN(sizeof(struct ipt_error_target)))
599
600
601
602/* compile rule from cache into blob */
603static inline int iptcc_compile_rule (TC_HANDLE_T h, STRUCT_REPLACE *repl, struct rule_head *r)
604{
605 /* handle jumps */
606 if (r->type == IPTCC_R_JUMP) {
607 STRUCT_STANDARD_TARGET *t;
608 t = (STRUCT_STANDARD_TARGET *)GET_TARGET(r->entry);
609 /* memset for memcmp convenience on delete/replace */
610 memset(t->target.u.user.name, 0, FUNCTION_MAXNAMELEN);
611 strcpy(t->target.u.user.name, STANDARD_TARGET);
612 /* Jumps can only happen to builtin chains, so we
613 * can safely assume that they always have a header */
614 t->verdict = r->jump->head_offset + IPTCB_CHAIN_START_SIZE;
615 } else if (r->type == IPTCC_R_FALLTHROUGH) {
616 STRUCT_STANDARD_TARGET *t;
617 t = (STRUCT_STANDARD_TARGET *)GET_TARGET(r->entry);
618 t->verdict = r->offset + r->size;
619 }
620
621 /* copy entry from cache to blob */
622 memcpy((char *)repl->entries+r->offset, r->entry, r->size);
623
624 return 1;
625}
626
627/* compile chain from cache into blob */
628static int iptcc_compile_chain(TC_HANDLE_T h, STRUCT_REPLACE *repl, struct chain_head *c)
629{
630 int ret;
631 struct rule_head *r;
632 struct iptcb_chain_start *head;
633 struct iptcb_chain_foot *foot;
634
635 /* only user-defined chains have heaer */
636 if (!iptcc_is_builtin(c)) {
637 /* put chain header in place */
638 head = (void *)repl->entries + c->head_offset;
639 head->e.target_offset = sizeof(STRUCT_ENTRY);
640 head->e.next_offset = IPTCB_CHAIN_START_SIZE;
641 strcpy(head->name.t.u.user.name, ERROR_TARGET);
642 head->name.t.u.target_size =
643 ALIGN(sizeof(struct ipt_error_target));
644 strcpy(head->name.error, c->name);
645 } else {
646 repl->hook_entry[c->hooknum-1] = c->head_offset;
647 repl->underflow[c->hooknum-1] = c->foot_offset;
648 }
649
650 /* iterate over rules */
651 list_for_each_entry(r, &c->rules, list) {
652 ret = iptcc_compile_rule(h, repl, r);
653 if (ret < 0)
654 return ret;
655 }
656
657 /* put chain footer in place */
658 foot = (void *)repl->entries + c->foot_offset;
659 foot->e.target_offset = sizeof(STRUCT_ENTRY);
660 foot->e.next_offset = IPTCB_CHAIN_FOOT_SIZE;
661 strcpy(foot->target.target.u.user.name, STANDARD_TARGET);
662 foot->target.target.u.target_size =
663 ALIGN(sizeof(STRUCT_STANDARD_TARGET));
664 /* builtin targets have verdict, others return */
665 if (iptcc_is_builtin(c))
666 foot->target.verdict = c->verdict;
667 else
668 foot->target.verdict = RETURN;
669 /* set policy-counters */
670 memcpy(&foot->e.counters, &c->counters, sizeof(STRUCT_COUNTERS));
671
672 return 0;
673}
674
675/* calculate offset and number for every rule in the cache */
676static int iptcc_compile_chain_offsets(TC_HANDLE_T h, struct chain_head *c,
Harald Welteefa8fc22005-07-19 22:03:49 +0000677 unsigned int *offset, unsigned int *num)
Harald Welteaae69be2004-08-29 23:32:14 +0000678{
679 struct rule_head *r;
680
681 c->head_offset = *offset;
682 DEBUGP("%s: chain_head %u, offset=%u\n", c->name, *num, *offset);
683
684 if (!iptcc_is_builtin(c)) {
685 /* Chain has header */
686 *offset += sizeof(STRUCT_ENTRY)
687 + ALIGN(sizeof(struct ipt_error_target));
688 (*num)++;
689 }
690
691 list_for_each_entry(r, &c->rules, list) {
692 DEBUGP("rule %u, offset=%u, index=%u\n", *num, *offset, *num);
693 r->offset = *offset;
694 r->index = *num;
695 *offset += r->size;
696 (*num)++;
697 }
698
699 DEBUGP("%s; chain_foot %u, offset=%u, index=%u\n", c->name, *num,
700 *offset, *num);
701 c->foot_offset = *offset;
702 c->foot_index = *num;
703 *offset += sizeof(STRUCT_ENTRY)
704 + ALIGN(sizeof(STRUCT_STANDARD_TARGET));
705 (*num)++;
706
707 return 1;
708}
709
710/* put the pieces back together again */
711static int iptcc_compile_table_prep(TC_HANDLE_T h, unsigned int *size)
712{
713 struct chain_head *c;
714 unsigned int offset = 0, num = 0;
715 int ret = 0;
716
717 /* First pass: calculate offset for every rule */
718 list_for_each_entry(c, &h->chains, list) {
719 ret = iptcc_compile_chain_offsets(h, c, &offset, &num);
720 if (ret < 0)
721 return ret;
722 }
723
724 /* Append one error rule at end of chain */
725 num++;
726 offset += sizeof(STRUCT_ENTRY)
727 + ALIGN(sizeof(struct ipt_error_target));
728
729 /* ruleset size is now in offset */
730 *size = offset;
731 return num;
732}
733
734static int iptcc_compile_table(TC_HANDLE_T h, STRUCT_REPLACE *repl)
735{
736 struct chain_head *c;
737 struct iptcb_chain_error *error;
738
739 /* Second pass: copy from cache to offsets, fill in jumps */
740 list_for_each_entry(c, &h->chains, list) {
741 int ret = iptcc_compile_chain(h, repl, c);
742 if (ret < 0)
743 return ret;
744 }
745
746 /* Append error rule at end of chain */
747 error = (void *)repl->entries + repl->size - IPTCB_CHAIN_ERROR_SIZE;
748 error->entry.target_offset = sizeof(STRUCT_ENTRY);
749 error->entry.next_offset = IPTCB_CHAIN_ERROR_SIZE;
750 error->target.t.u.user.target_size =
751 ALIGN(sizeof(struct ipt_error_target));
752 strcpy((char *)&error->target.t.u.user.name, ERROR_TARGET);
753 strcpy((char *)&error->target.error, "ERROR");
754
755 return 1;
756}
757
758/**********************************************************************
759 * EXTERNAL API (operates on cache only)
760 **********************************************************************/
Marc Bouchere6869a82000-03-20 06:03:29 +0000761
762/* Allocate handle of given size */
Rusty Russell79dee072000-05-02 16:45:16 +0000763static TC_HANDLE_T
Harald Welte0113fe72004-01-06 19:04:02 +0000764alloc_handle(const char *tablename, unsigned int size, unsigned int num_rules)
Marc Bouchere6869a82000-03-20 06:03:29 +0000765{
766 size_t len;
Rusty Russell79dee072000-05-02 16:45:16 +0000767 TC_HANDLE_T h;
Marc Bouchere6869a82000-03-20 06:03:29 +0000768
Harald Welteaae69be2004-08-29 23:32:14 +0000769 len = sizeof(STRUCT_TC_HANDLE) + size;
Marc Bouchere6869a82000-03-20 06:03:29 +0000770
Harald Welteaae69be2004-08-29 23:32:14 +0000771 h = malloc(sizeof(STRUCT_TC_HANDLE));
772 if (!h) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000773 errno = ENOMEM;
774 return NULL;
775 }
Harald Welteaae69be2004-08-29 23:32:14 +0000776 memset(h, 0, sizeof(*h));
777 INIT_LIST_HEAD(&h->chains);
Marc Bouchere6869a82000-03-20 06:03:29 +0000778 strcpy(h->info.name, tablename);
Harald Welteaae69be2004-08-29 23:32:14 +0000779
Harald Welte0371c0c2004-09-19 21:00:12 +0000780 h->entries = malloc(sizeof(STRUCT_GET_ENTRIES) + size);
Harald Welteaae69be2004-08-29 23:32:14 +0000781 if (!h->entries)
782 goto out_free_handle;
783
784 strcpy(h->entries->name, tablename);
Harald Welte0371c0c2004-09-19 21:00:12 +0000785 h->entries->size = size;
Marc Bouchere6869a82000-03-20 06:03:29 +0000786
787 return h;
Harald Welteaae69be2004-08-29 23:32:14 +0000788
789out_free_handle:
790 free(h);
791
792 return NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +0000793}
794
Harald Welteaae69be2004-08-29 23:32:14 +0000795
Rusty Russell79dee072000-05-02 16:45:16 +0000796TC_HANDLE_T
797TC_INIT(const char *tablename)
Marc Bouchere6869a82000-03-20 06:03:29 +0000798{
Rusty Russell79dee072000-05-02 16:45:16 +0000799 TC_HANDLE_T h;
800 STRUCT_GETINFO info;
Harald Welteefa8fc22005-07-19 22:03:49 +0000801 unsigned int tmp;
Marc Bouchere6869a82000-03-20 06:03:29 +0000802 socklen_t s;
803
Rusty Russell79dee072000-05-02 16:45:16 +0000804 iptc_fn = TC_INIT;
Marc Bouchere6869a82000-03-20 06:03:29 +0000805
Martin Josefsson841e4ae2003-05-02 15:30:11 +0000806 if (strlen(tablename) >= TABLE_MAXNAMELEN) {
807 errno = EINVAL;
808 return NULL;
809 }
810
Derrik Pates664c0a32005-02-01 13:28:14 +0000811 if (sockfd_use == 0) {
812 sockfd = socket(TC_AF, SOCK_RAW, IPPROTO_RAW);
813 if (sockfd < 0)
814 return NULL;
815 }
816 sockfd_use++;
Marc Bouchere6869a82000-03-20 06:03:29 +0000817
818 s = sizeof(info);
Martin Josefsson841e4ae2003-05-02 15:30:11 +0000819
Marc Bouchere6869a82000-03-20 06:03:29 +0000820 strcpy(info.name, tablename);
Derrik Pates664c0a32005-02-01 13:28:14 +0000821 if (getsockopt(sockfd, TC_IPPROTO, SO_GET_INFO, &info, &s) < 0) {
822 if (--sockfd_use == 0) {
823 close(sockfd);
824 sockfd = -1;
825 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000826 return NULL;
Derrik Pates664c0a32005-02-01 13:28:14 +0000827 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000828
Harald Welteaae69be2004-08-29 23:32:14 +0000829 DEBUGP("valid_hooks=0x%08x, num_entries=%u, size=%u\n",
830 info.valid_hooks, info.num_entries, info.size);
831
Harald Welte0113fe72004-01-06 19:04:02 +0000832 if ((h = alloc_handle(info.name, info.size, info.num_entries))
Martin Josefsson841e4ae2003-05-02 15:30:11 +0000833 == NULL) {
Derrik Pates664c0a32005-02-01 13:28:14 +0000834 if (--sockfd_use == 0) {
835 close(sockfd);
836 sockfd = -1;
837 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000838 return NULL;
Martin Josefsson841e4ae2003-05-02 15:30:11 +0000839 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000840
Marc Bouchere6869a82000-03-20 06:03:29 +0000841 /* Initialize current state */
842 h->info = info;
Harald Welte0113fe72004-01-06 19:04:02 +0000843
Harald Welteaae69be2004-08-29 23:32:14 +0000844 h->entries->size = h->info.size;
Marc Bouchere6869a82000-03-20 06:03:29 +0000845
Rusty Russell79dee072000-05-02 16:45:16 +0000846 tmp = sizeof(STRUCT_GET_ENTRIES) + h->info.size;
Marc Bouchere6869a82000-03-20 06:03:29 +0000847
Harald Welteaae69be2004-08-29 23:32:14 +0000848 if (getsockopt(sockfd, TC_IPPROTO, SO_GET_ENTRIES, h->entries,
849 &tmp) < 0)
850 goto error;
851
852#ifdef IPTC_DEBUG2
853 {
854 int fd = open("/tmp/libiptc-so_get_entries.blob",
855 O_CREAT|O_WRONLY);
856 if (fd >= 0) {
857 write(fd, h->entries, tmp);
858 close(fd);
859 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000860 }
Harald Welteaae69be2004-08-29 23:32:14 +0000861#endif
862
863 if (parse_table(h) < 0)
864 goto error;
Rusty Russell7e53bf92000-03-20 07:03:28 +0000865
Marc Bouchere6869a82000-03-20 06:03:29 +0000866 CHECK(h);
867 return h;
Harald Welteaae69be2004-08-29 23:32:14 +0000868error:
869 TC_FREE(&h);
870 return NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +0000871}
872
Martin Josefsson841e4ae2003-05-02 15:30:11 +0000873void
874TC_FREE(TC_HANDLE_T *h)
875{
Harald Welteaae69be2004-08-29 23:32:14 +0000876 struct chain_head *c, *tmp;
877
Derrik Pates664c0a32005-02-01 13:28:14 +0000878 iptc_fn = TC_FREE;
879 if (--sockfd_use == 0) {
880 close(sockfd);
881 sockfd = -1;
882 }
Harald Welteaae69be2004-08-29 23:32:14 +0000883
884 list_for_each_entry_safe(c, tmp, &(*h)->chains, list) {
885 struct rule_head *r, *rtmp;
886
887 list_for_each_entry_safe(r, rtmp, &c->rules, list) {
888 free(r);
889 }
890
891 free(c);
892 }
893
894 free((*h)->entries);
Martin Josefsson841e4ae2003-05-02 15:30:11 +0000895 free(*h);
Harald Welteaae69be2004-08-29 23:32:14 +0000896
Martin Josefsson841e4ae2003-05-02 15:30:11 +0000897 *h = NULL;
898}
899
Marc Bouchere6869a82000-03-20 06:03:29 +0000900static inline int
Rusty Russell79dee072000-05-02 16:45:16 +0000901print_match(const STRUCT_ENTRY_MATCH *m)
Marc Bouchere6869a82000-03-20 06:03:29 +0000902{
Rusty Russell228e98d2000-04-27 10:28:06 +0000903 printf("Match name: `%s'\n", m->u.user.name);
Marc Bouchere6869a82000-03-20 06:03:29 +0000904 return 0;
905}
906
Rusty Russell79dee072000-05-02 16:45:16 +0000907static int dump_entry(STRUCT_ENTRY *e, const TC_HANDLE_T handle);
908
Marc Bouchere6869a82000-03-20 06:03:29 +0000909void
Rusty Russell79dee072000-05-02 16:45:16 +0000910TC_DUMP_ENTRIES(const TC_HANDLE_T handle)
Marc Bouchere6869a82000-03-20 06:03:29 +0000911{
Derrik Pates664c0a32005-02-01 13:28:14 +0000912 iptc_fn = TC_DUMP_ENTRIES;
Marc Bouchere6869a82000-03-20 06:03:29 +0000913 CHECK(handle);
Patrick McHardy97fb2f12007-09-08 16:52:25 +0000914
Rusty Russelle45c7132004-12-16 13:21:44 +0000915 printf("libiptc v%s. %u bytes.\n",
916 IPTABLES_VERSION, handle->entries->size);
Marc Bouchere6869a82000-03-20 06:03:29 +0000917 printf("Table `%s'\n", handle->info.name);
918 printf("Hooks: pre/in/fwd/out/post = %u/%u/%u/%u/%u\n",
Rusty Russell67088e72000-05-10 01:18:57 +0000919 handle->info.hook_entry[HOOK_PRE_ROUTING],
920 handle->info.hook_entry[HOOK_LOCAL_IN],
921 handle->info.hook_entry[HOOK_FORWARD],
922 handle->info.hook_entry[HOOK_LOCAL_OUT],
923 handle->info.hook_entry[HOOK_POST_ROUTING]);
Marc Bouchere6869a82000-03-20 06:03:29 +0000924 printf("Underflows: pre/in/fwd/out/post = %u/%u/%u/%u/%u\n",
Rusty Russell67088e72000-05-10 01:18:57 +0000925 handle->info.underflow[HOOK_PRE_ROUTING],
926 handle->info.underflow[HOOK_LOCAL_IN],
927 handle->info.underflow[HOOK_FORWARD],
928 handle->info.underflow[HOOK_LOCAL_OUT],
929 handle->info.underflow[HOOK_POST_ROUTING]);
Marc Bouchere6869a82000-03-20 06:03:29 +0000930
Harald Welteaae69be2004-08-29 23:32:14 +0000931 ENTRY_ITERATE(handle->entries->entrytable, handle->entries->size,
Rusty Russell79dee072000-05-02 16:45:16 +0000932 dump_entry, handle);
Harald Welte0113fe72004-01-06 19:04:02 +0000933}
Rusty Russell30fd6e52000-04-23 09:16:06 +0000934
Marc Bouchere6869a82000-03-20 06:03:29 +0000935/* Does this chain exist? */
Rusty Russell79dee072000-05-02 16:45:16 +0000936int TC_IS_CHAIN(const char *chain, const TC_HANDLE_T handle)
Marc Bouchere6869a82000-03-20 06:03:29 +0000937{
Derrik Pates664c0a32005-02-01 13:28:14 +0000938 iptc_fn = TC_IS_CHAIN;
Harald Welteaae69be2004-08-29 23:32:14 +0000939 return iptcc_find_label(chain, handle) != NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +0000940}
941
Harald Welteaae69be2004-08-29 23:32:14 +0000942static void iptcc_chain_iterator_advance(TC_HANDLE_T handle)
943{
944 struct chain_head *c = handle->chain_iterator_cur;
945
946 if (c->list.next == &handle->chains)
947 handle->chain_iterator_cur = NULL;
948 else
949 handle->chain_iterator_cur =
950 list_entry(c->list.next, struct chain_head, list);
951}
Marc Bouchere6869a82000-03-20 06:03:29 +0000952
Rusty Russell30fd6e52000-04-23 09:16:06 +0000953/* Iterator functions to run through the chains. */
Marc Bouchere6869a82000-03-20 06:03:29 +0000954const char *
Philip Blundell8c700902000-05-15 02:17:52 +0000955TC_FIRST_CHAIN(TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +0000956{
Harald Welteaae69be2004-08-29 23:32:14 +0000957 struct chain_head *c = list_entry((*handle)->chains.next,
958 struct chain_head, list);
959
960 iptc_fn = TC_FIRST_CHAIN;
961
962
963 if (list_empty(&(*handle)->chains)) {
964 DEBUGP(": no chains\n");
Harald Welte0113fe72004-01-06 19:04:02 +0000965 return NULL;
Harald Welteaae69be2004-08-29 23:32:14 +0000966 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000967
Harald Welteaae69be2004-08-29 23:32:14 +0000968 (*handle)->chain_iterator_cur = c;
969 iptcc_chain_iterator_advance(*handle);
Harald Welte0113fe72004-01-06 19:04:02 +0000970
Harald Welteaae69be2004-08-29 23:32:14 +0000971 DEBUGP(": returning `%s'\n", c->name);
972 return c->name;
Marc Bouchere6869a82000-03-20 06:03:29 +0000973}
974
Rusty Russell30fd6e52000-04-23 09:16:06 +0000975/* Iterator functions to run through the chains. Returns NULL at end. */
976const char *
Rusty Russell79dee072000-05-02 16:45:16 +0000977TC_NEXT_CHAIN(TC_HANDLE_T *handle)
Rusty Russell30fd6e52000-04-23 09:16:06 +0000978{
Harald Welteaae69be2004-08-29 23:32:14 +0000979 struct chain_head *c = (*handle)->chain_iterator_cur;
Rusty Russell30fd6e52000-04-23 09:16:06 +0000980
Harald Welteaae69be2004-08-29 23:32:14 +0000981 iptc_fn = TC_NEXT_CHAIN;
982
983 if (!c) {
984 DEBUGP(": no more chains\n");
Rusty Russell30fd6e52000-04-23 09:16:06 +0000985 return NULL;
Harald Welteaae69be2004-08-29 23:32:14 +0000986 }
Rusty Russell30fd6e52000-04-23 09:16:06 +0000987
Harald Welteaae69be2004-08-29 23:32:14 +0000988 iptcc_chain_iterator_advance(*handle);
989
990 DEBUGP(": returning `%s'\n", c->name);
991 return c->name;
Rusty Russell30fd6e52000-04-23 09:16:06 +0000992}
993
994/* Get first rule in the given chain: NULL for empty chain. */
Rusty Russell79dee072000-05-02 16:45:16 +0000995const STRUCT_ENTRY *
Philip Blundell8c700902000-05-15 02:17:52 +0000996TC_FIRST_RULE(const char *chain, TC_HANDLE_T *handle)
Rusty Russell30fd6e52000-04-23 09:16:06 +0000997{
Harald Welteaae69be2004-08-29 23:32:14 +0000998 struct chain_head *c;
999 struct rule_head *r;
Rusty Russell30fd6e52000-04-23 09:16:06 +00001000
Harald Welteaae69be2004-08-29 23:32:14 +00001001 iptc_fn = TC_FIRST_RULE;
1002
1003 DEBUGP("first rule(%s): ", chain);
1004
1005 c = iptcc_find_label(chain, *handle);
Rusty Russell30fd6e52000-04-23 09:16:06 +00001006 if (!c) {
1007 errno = ENOENT;
1008 return NULL;
1009 }
1010
1011 /* Empty chain: single return/policy rule */
Harald Welteaae69be2004-08-29 23:32:14 +00001012 if (list_empty(&c->rules)) {
1013 DEBUGP_C("no rules, returning NULL\n");
Rusty Russell30fd6e52000-04-23 09:16:06 +00001014 return NULL;
Harald Welteaae69be2004-08-29 23:32:14 +00001015 }
Rusty Russell30fd6e52000-04-23 09:16:06 +00001016
Harald Welteaae69be2004-08-29 23:32:14 +00001017 r = list_entry(c->rules.next, struct rule_head, list);
1018 (*handle)->rule_iterator_cur = r;
1019 DEBUGP_C("%p\n", r);
1020
1021 return r->entry;
Rusty Russell30fd6e52000-04-23 09:16:06 +00001022}
1023
1024/* Returns NULL when rules run out. */
Rusty Russell79dee072000-05-02 16:45:16 +00001025const STRUCT_ENTRY *
Philip Blundell8c700902000-05-15 02:17:52 +00001026TC_NEXT_RULE(const STRUCT_ENTRY *prev, TC_HANDLE_T *handle)
Rusty Russell30fd6e52000-04-23 09:16:06 +00001027{
Harald Welteaae69be2004-08-29 23:32:14 +00001028 struct rule_head *r;
Rusty Russell30fd6e52000-04-23 09:16:06 +00001029
Derrik Pates664c0a32005-02-01 13:28:14 +00001030 iptc_fn = TC_NEXT_RULE;
Harald Welteaae69be2004-08-29 23:32:14 +00001031 DEBUGP("rule_iterator_cur=%p...", (*handle)->rule_iterator_cur);
1032
1033 if (!(*handle)->rule_iterator_cur) {
1034 DEBUGP_C("returning NULL\n");
1035 return NULL;
1036 }
1037
1038 r = list_entry((*handle)->rule_iterator_cur->list.next,
1039 struct rule_head, list);
1040
1041 iptc_fn = TC_NEXT_RULE;
1042
1043 DEBUGP_C("next=%p, head=%p...", &r->list,
1044 &(*handle)->rule_iterator_cur->chain->rules);
1045
1046 if (&r->list == &(*handle)->rule_iterator_cur->chain->rules) {
1047 (*handle)->rule_iterator_cur = NULL;
1048 DEBUGP_C("finished, returning NULL\n");
1049 return NULL;
1050 }
1051
1052 (*handle)->rule_iterator_cur = r;
1053
1054 /* NOTE: prev is without any influence ! */
1055 DEBUGP_C("returning rule %p\n", r);
1056 return r->entry;
Rusty Russell30fd6e52000-04-23 09:16:06 +00001057}
1058
Marc Bouchere6869a82000-03-20 06:03:29 +00001059/* How many rules in this chain? */
1060unsigned int
Rusty Russell79dee072000-05-02 16:45:16 +00001061TC_NUM_RULES(const char *chain, TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001062{
Harald Welteaae69be2004-08-29 23:32:14 +00001063 struct chain_head *c;
1064 iptc_fn = TC_NUM_RULES;
Marc Bouchere6869a82000-03-20 06:03:29 +00001065 CHECK(*handle);
Harald Welteaae69be2004-08-29 23:32:14 +00001066
1067 c = iptcc_find_label(chain, *handle);
1068 if (!c) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001069 errno = ENOENT;
1070 return (unsigned int)-1;
1071 }
Harald Welteaae69be2004-08-29 23:32:14 +00001072
1073 return c->num_rules;
Marc Bouchere6869a82000-03-20 06:03:29 +00001074}
1075
Rusty Russell79dee072000-05-02 16:45:16 +00001076const STRUCT_ENTRY *TC_GET_RULE(const char *chain,
1077 unsigned int n,
1078 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001079{
Harald Welteaae69be2004-08-29 23:32:14 +00001080 struct chain_head *c;
1081 struct rule_head *r;
1082
1083 iptc_fn = TC_GET_RULE;
Marc Bouchere6869a82000-03-20 06:03:29 +00001084
1085 CHECK(*handle);
Harald Welteaae69be2004-08-29 23:32:14 +00001086
1087 c = iptcc_find_label(chain, *handle);
1088 if (!c) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001089 errno = ENOENT;
1090 return NULL;
1091 }
1092
Harald Welteaae69be2004-08-29 23:32:14 +00001093 r = iptcc_get_rule_num(c, n);
1094 if (!r)
1095 return NULL;
1096 return r->entry;
Marc Bouchere6869a82000-03-20 06:03:29 +00001097}
1098
1099/* Returns a pointer to the target name of this position. */
Harald Welteaae69be2004-08-29 23:32:14 +00001100const char *standard_target_map(int verdict)
Marc Bouchere6869a82000-03-20 06:03:29 +00001101{
Harald Welteaae69be2004-08-29 23:32:14 +00001102 switch (verdict) {
1103 case RETURN:
1104 return LABEL_RETURN;
1105 break;
1106 case -NF_ACCEPT-1:
1107 return LABEL_ACCEPT;
1108 break;
1109 case -NF_DROP-1:
1110 return LABEL_DROP;
1111 break;
1112 case -NF_QUEUE-1:
1113 return LABEL_QUEUE;
1114 break;
1115 default:
1116 fprintf(stderr, "ERROR: %d not a valid target)\n",
1117 verdict);
1118 abort();
1119 break;
1120 }
1121 /* not reached */
1122 return NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +00001123}
1124
Harald Welteaae69be2004-08-29 23:32:14 +00001125/* Returns a pointer to the target name of this position. */
1126const char *TC_GET_TARGET(const STRUCT_ENTRY *ce,
1127 TC_HANDLE_T *handle)
1128{
1129 STRUCT_ENTRY *e = (STRUCT_ENTRY *)ce;
Rusty Russelle45c7132004-12-16 13:21:44 +00001130 struct rule_head *r = container_of(e, struct rule_head, entry[0]);
Harald Welteaae69be2004-08-29 23:32:14 +00001131
1132 iptc_fn = TC_GET_TARGET;
1133
1134 switch(r->type) {
1135 int spos;
1136 case IPTCC_R_FALLTHROUGH:
1137 return "";
1138 break;
1139 case IPTCC_R_JUMP:
1140 DEBUGP("r=%p, jump=%p, name=`%s'\n", r, r->jump, r->jump->name);
1141 return r->jump->name;
1142 break;
1143 case IPTCC_R_STANDARD:
1144 spos = *(int *)GET_TARGET(e)->data;
1145 DEBUGP("r=%p, spos=%d'\n", r, spos);
1146 return standard_target_map(spos);
1147 break;
1148 case IPTCC_R_MODULE:
1149 return GET_TARGET(e)->u.user.name;
1150 break;
1151 }
1152 return NULL;
1153}
Marc Bouchere6869a82000-03-20 06:03:29 +00001154/* Is this a built-in chain? Actually returns hook + 1. */
1155int
Rusty Russell79dee072000-05-02 16:45:16 +00001156TC_BUILTIN(const char *chain, const TC_HANDLE_T handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001157{
Harald Welteaae69be2004-08-29 23:32:14 +00001158 struct chain_head *c;
1159
1160 iptc_fn = TC_BUILTIN;
Marc Bouchere6869a82000-03-20 06:03:29 +00001161
Harald Welteaae69be2004-08-29 23:32:14 +00001162 c = iptcc_find_label(chain, handle);
1163 if (!c) {
1164 errno = ENOENT;
Martin Josefssonb0f3d2d2004-09-23 18:23:20 +00001165 return 0;
Marc Bouchere6869a82000-03-20 06:03:29 +00001166 }
Harald Welteaae69be2004-08-29 23:32:14 +00001167
1168 return iptcc_is_builtin(c);
Marc Bouchere6869a82000-03-20 06:03:29 +00001169}
1170
1171/* Get the policy of a given built-in chain */
1172const char *
Rusty Russell79dee072000-05-02 16:45:16 +00001173TC_GET_POLICY(const char *chain,
1174 STRUCT_COUNTERS *counters,
1175 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001176{
Harald Welteaae69be2004-08-29 23:32:14 +00001177 struct chain_head *c;
Marc Bouchere6869a82000-03-20 06:03:29 +00001178
Harald Welteaae69be2004-08-29 23:32:14 +00001179 iptc_fn = TC_GET_POLICY;
1180
1181 DEBUGP("called for chain %s\n", chain);
1182
1183 c = iptcc_find_label(chain, *handle);
1184 if (!c) {
1185 errno = ENOENT;
1186 return NULL;
1187 }
1188
1189 if (!iptcc_is_builtin(c))
Marc Bouchere6869a82000-03-20 06:03:29 +00001190 return NULL;
1191
Harald Welteaae69be2004-08-29 23:32:14 +00001192 *counters = c->counters;
Marc Bouchere6869a82000-03-20 06:03:29 +00001193
Harald Welteaae69be2004-08-29 23:32:14 +00001194 return standard_target_map(c->verdict);
Harald Welte0113fe72004-01-06 19:04:02 +00001195}
1196
1197static int
Harald Welteaae69be2004-08-29 23:32:14 +00001198iptcc_standard_map(struct rule_head *r, int verdict)
Harald Welte0113fe72004-01-06 19:04:02 +00001199{
Harald Welteaae69be2004-08-29 23:32:14 +00001200 STRUCT_ENTRY *e = r->entry;
Rusty Russell79dee072000-05-02 16:45:16 +00001201 STRUCT_STANDARD_TARGET *t;
Marc Bouchere6869a82000-03-20 06:03:29 +00001202
Rusty Russell79dee072000-05-02 16:45:16 +00001203 t = (STRUCT_STANDARD_TARGET *)GET_TARGET(e);
Marc Bouchere6869a82000-03-20 06:03:29 +00001204
Rusty Russell67088e72000-05-10 01:18:57 +00001205 if (t->target.u.target_size
Philip Blundell8c700902000-05-15 02:17:52 +00001206 != ALIGN(sizeof(STRUCT_STANDARD_TARGET))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001207 errno = EINVAL;
1208 return 0;
1209 }
1210 /* memset for memcmp convenience on delete/replace */
Rusty Russell79dee072000-05-02 16:45:16 +00001211 memset(t->target.u.user.name, 0, FUNCTION_MAXNAMELEN);
1212 strcpy(t->target.u.user.name, STANDARD_TARGET);
Marc Bouchere6869a82000-03-20 06:03:29 +00001213 t->verdict = verdict;
1214
Harald Welteaae69be2004-08-29 23:32:14 +00001215 r->type = IPTCC_R_STANDARD;
1216
Marc Bouchere6869a82000-03-20 06:03:29 +00001217 return 1;
1218}
Rusty Russell7e53bf92000-03-20 07:03:28 +00001219
Marc Bouchere6869a82000-03-20 06:03:29 +00001220static int
Harald Welteaae69be2004-08-29 23:32:14 +00001221iptcc_map_target(const TC_HANDLE_T handle,
1222 struct rule_head *r)
Marc Bouchere6869a82000-03-20 06:03:29 +00001223{
Harald Welteaae69be2004-08-29 23:32:14 +00001224 STRUCT_ENTRY *e = r->entry;
Harald Welte0113fe72004-01-06 19:04:02 +00001225 STRUCT_ENTRY_TARGET *t = GET_TARGET(e);
Marc Bouchere6869a82000-03-20 06:03:29 +00001226
Marc Bouchere6869a82000-03-20 06:03:29 +00001227 /* Maybe it's empty (=> fall through) */
Harald Welteaae69be2004-08-29 23:32:14 +00001228 if (strcmp(t->u.user.name, "") == 0) {
1229 r->type = IPTCC_R_FALLTHROUGH;
1230 return 1;
1231 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001232 /* Maybe it's a standard target name... */
Rusty Russell79dee072000-05-02 16:45:16 +00001233 else if (strcmp(t->u.user.name, LABEL_ACCEPT) == 0)
Harald Welteaae69be2004-08-29 23:32:14 +00001234 return iptcc_standard_map(r, -NF_ACCEPT - 1);
Rusty Russell79dee072000-05-02 16:45:16 +00001235 else if (strcmp(t->u.user.name, LABEL_DROP) == 0)
Harald Welteaae69be2004-08-29 23:32:14 +00001236 return iptcc_standard_map(r, -NF_DROP - 1);
Rusty Russell67088e72000-05-10 01:18:57 +00001237 else if (strcmp(t->u.user.name, LABEL_QUEUE) == 0)
Harald Welteaae69be2004-08-29 23:32:14 +00001238 return iptcc_standard_map(r, -NF_QUEUE - 1);
Rusty Russell79dee072000-05-02 16:45:16 +00001239 else if (strcmp(t->u.user.name, LABEL_RETURN) == 0)
Harald Welteaae69be2004-08-29 23:32:14 +00001240 return iptcc_standard_map(r, RETURN);
Rusty Russell79dee072000-05-02 16:45:16 +00001241 else if (TC_BUILTIN(t->u.user.name, handle)) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001242 /* Can't jump to builtins. */
1243 errno = EINVAL;
1244 return 0;
1245 } else {
1246 /* Maybe it's an existing chain name. */
Harald Welteaae69be2004-08-29 23:32:14 +00001247 struct chain_head *c;
1248 DEBUGP("trying to find chain `%s': ", t->u.user.name);
Marc Bouchere6869a82000-03-20 06:03:29 +00001249
Harald Welteaae69be2004-08-29 23:32:14 +00001250 c = iptcc_find_label(t->u.user.name, handle);
1251 if (c) {
1252 DEBUGP_C("found!\n");
1253 r->type = IPTCC_R_JUMP;
1254 r->jump = c;
1255 c->references++;
1256 return 1;
1257 }
1258 DEBUGP_C("not found :(\n");
Marc Bouchere6869a82000-03-20 06:03:29 +00001259 }
1260
1261 /* Must be a module? If not, kernel will reject... */
Rusty Russell3aef54d2005-01-03 03:48:40 +00001262 /* memset to all 0 for your memcmp convenience: don't clear version */
Rusty Russell228e98d2000-04-27 10:28:06 +00001263 memset(t->u.user.name + strlen(t->u.user.name),
Marc Bouchere6869a82000-03-20 06:03:29 +00001264 0,
Rusty Russell3aef54d2005-01-03 03:48:40 +00001265 FUNCTION_MAXNAMELEN - 1 - strlen(t->u.user.name));
Rusty Russell733e54b2004-12-16 14:22:23 +00001266 r->type = IPTCC_R_MODULE;
Harald Welteaae69be2004-08-29 23:32:14 +00001267 set_changed(handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001268 return 1;
1269}
1270
Harald Welte0113fe72004-01-06 19:04:02 +00001271/* Insert the entry `fw' in chain `chain' into position `rulenum'. */
Marc Bouchere6869a82000-03-20 06:03:29 +00001272int
Rusty Russell79dee072000-05-02 16:45:16 +00001273TC_INSERT_ENTRY(const IPT_CHAINLABEL chain,
1274 const STRUCT_ENTRY *e,
1275 unsigned int rulenum,
1276 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001277{
Harald Welteaae69be2004-08-29 23:32:14 +00001278 struct chain_head *c;
Martin Josefssoneb066cc2004-09-22 21:04:07 +00001279 struct rule_head *r;
1280 struct list_head *prev;
Marc Bouchere6869a82000-03-20 06:03:29 +00001281
Rusty Russell79dee072000-05-02 16:45:16 +00001282 iptc_fn = TC_INSERT_ENTRY;
Harald Welteaae69be2004-08-29 23:32:14 +00001283
1284 if (!(c = iptcc_find_label(chain, *handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001285 errno = ENOENT;
1286 return 0;
1287 }
1288
Martin Josefssoneb066cc2004-09-22 21:04:07 +00001289 /* first rulenum index = 0
1290 first c->num_rules index = 1 */
1291 if (rulenum > c->num_rules) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001292 errno = E2BIG;
1293 return 0;
1294 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001295
Martin Josefsson631f3612004-09-23 19:25:06 +00001296 /* If we are inserting at the end just take advantage of the
1297 double linked list, insert will happen before the entry
1298 prev points to. */
1299 if (rulenum == c->num_rules) {
Martin Josefssoneb066cc2004-09-22 21:04:07 +00001300 prev = &c->rules;
Martin Josefssona5616dc2004-10-24 22:27:31 +00001301 } else if (rulenum + 1 <= c->num_rules/2) {
Martin Josefsson631f3612004-09-23 19:25:06 +00001302 r = iptcc_get_rule_num(c, rulenum + 1);
Martin Josefssona5616dc2004-10-24 22:27:31 +00001303 prev = &r->list;
1304 } else {
1305 r = iptcc_get_rule_num_reverse(c, c->num_rules - rulenum);
Martin Josefsson631f3612004-09-23 19:25:06 +00001306 prev = &r->list;
1307 }
Martin Josefssoneb066cc2004-09-22 21:04:07 +00001308
Harald Welteaae69be2004-08-29 23:32:14 +00001309 if (!(r = iptcc_alloc_rule(c, e->next_offset))) {
1310 errno = ENOMEM;
Harald Welte0113fe72004-01-06 19:04:02 +00001311 return 0;
Harald Welteaae69be2004-08-29 23:32:14 +00001312 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001313
Harald Welteaae69be2004-08-29 23:32:14 +00001314 memcpy(r->entry, e, e->next_offset);
1315 r->counter_map.maptype = COUNTER_MAP_SET;
1316
1317 if (!iptcc_map_target(*handle, r)) {
1318 free(r);
1319 return 0;
1320 }
1321
Martin Josefssoneb066cc2004-09-22 21:04:07 +00001322 list_add_tail(&r->list, prev);
Harald Welteaae69be2004-08-29 23:32:14 +00001323 c->num_rules++;
1324
1325 set_changed(*handle);
1326
1327 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00001328}
1329
1330/* Atomically replace rule `rulenum' in `chain' with `fw'. */
1331int
Rusty Russell79dee072000-05-02 16:45:16 +00001332TC_REPLACE_ENTRY(const IPT_CHAINLABEL chain,
1333 const STRUCT_ENTRY *e,
1334 unsigned int rulenum,
1335 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001336{
Harald Welteaae69be2004-08-29 23:32:14 +00001337 struct chain_head *c;
1338 struct rule_head *r, *old;
Marc Bouchere6869a82000-03-20 06:03:29 +00001339
Rusty Russell79dee072000-05-02 16:45:16 +00001340 iptc_fn = TC_REPLACE_ENTRY;
Marc Bouchere6869a82000-03-20 06:03:29 +00001341
Harald Welteaae69be2004-08-29 23:32:14 +00001342 if (!(c = iptcc_find_label(chain, *handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001343 errno = ENOENT;
1344 return 0;
1345 }
1346
Martin Josefsson0f9b8b12004-12-18 17:18:49 +00001347 if (rulenum >= c->num_rules) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001348 errno = E2BIG;
1349 return 0;
1350 }
1351
Martin Josefsson0f9b8b12004-12-18 17:18:49 +00001352 /* Take advantage of the double linked list if possible. */
1353 if (rulenum + 1 <= c->num_rules/2) {
1354 old = iptcc_get_rule_num(c, rulenum + 1);
1355 } else {
1356 old = iptcc_get_rule_num_reverse(c, c->num_rules - rulenum);
1357 }
1358
Harald Welteaae69be2004-08-29 23:32:14 +00001359 if (!(r = iptcc_alloc_rule(c, e->next_offset))) {
1360 errno = ENOMEM;
Marc Bouchere6869a82000-03-20 06:03:29 +00001361 return 0;
Harald Welteaae69be2004-08-29 23:32:14 +00001362 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001363
Harald Welteaae69be2004-08-29 23:32:14 +00001364 memcpy(r->entry, e, e->next_offset);
1365 r->counter_map.maptype = COUNTER_MAP_SET;
1366
1367 if (!iptcc_map_target(*handle, r)) {
1368 free(r);
Harald Welte0113fe72004-01-06 19:04:02 +00001369 return 0;
Harald Welteaae69be2004-08-29 23:32:14 +00001370 }
Harald Welte0113fe72004-01-06 19:04:02 +00001371
Harald Welteaae69be2004-08-29 23:32:14 +00001372 list_add(&r->list, &old->list);
1373 iptcc_delete_rule(old);
1374
1375 set_changed(*handle);
1376
1377 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00001378}
1379
Harald Welte0113fe72004-01-06 19:04:02 +00001380/* Append entry `fw' to chain `chain'. Equivalent to insert with
Marc Bouchere6869a82000-03-20 06:03:29 +00001381 rulenum = length of chain. */
1382int
Rusty Russell79dee072000-05-02 16:45:16 +00001383TC_APPEND_ENTRY(const IPT_CHAINLABEL chain,
1384 const STRUCT_ENTRY *e,
1385 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001386{
Harald Welteaae69be2004-08-29 23:32:14 +00001387 struct chain_head *c;
1388 struct rule_head *r;
Marc Bouchere6869a82000-03-20 06:03:29 +00001389
Rusty Russell79dee072000-05-02 16:45:16 +00001390 iptc_fn = TC_APPEND_ENTRY;
Harald Welteaae69be2004-08-29 23:32:14 +00001391 if (!(c = iptcc_find_label(chain, *handle))) {
1392 DEBUGP("unable to find chain `%s'\n", chain);
Marc Bouchere6869a82000-03-20 06:03:29 +00001393 errno = ENOENT;
1394 return 0;
1395 }
1396
Harald Welteaae69be2004-08-29 23:32:14 +00001397 if (!(r = iptcc_alloc_rule(c, e->next_offset))) {
1398 DEBUGP("unable to allocate rule for chain `%s'\n", chain);
1399 errno = ENOMEM;
Harald Welte0113fe72004-01-06 19:04:02 +00001400 return 0;
Harald Welteaae69be2004-08-29 23:32:14 +00001401 }
Harald Welte0113fe72004-01-06 19:04:02 +00001402
Harald Welteaae69be2004-08-29 23:32:14 +00001403 memcpy(r->entry, e, e->next_offset);
1404 r->counter_map.maptype = COUNTER_MAP_SET;
1405
1406 if (!iptcc_map_target(*handle, r)) {
Martin Josefsson12009532004-09-23 18:24:29 +00001407 DEBUGP("unable to map target of rule for chain `%s'\n", chain);
Harald Welteaae69be2004-08-29 23:32:14 +00001408 free(r);
1409 return 0;
1410 }
1411
1412 list_add_tail(&r->list, &c->rules);
1413 c->num_rules++;
1414
1415 set_changed(*handle);
1416
1417 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00001418}
1419
1420static inline int
Rusty Russell79dee072000-05-02 16:45:16 +00001421match_different(const STRUCT_ENTRY_MATCH *a,
Rusty Russelledf14cf2000-04-19 11:26:44 +00001422 const unsigned char *a_elems,
1423 const unsigned char *b_elems,
1424 unsigned char **maskptr)
Marc Bouchere6869a82000-03-20 06:03:29 +00001425{
Rusty Russell79dee072000-05-02 16:45:16 +00001426 const STRUCT_ENTRY_MATCH *b;
Rusty Russelledf14cf2000-04-19 11:26:44 +00001427 unsigned int i;
Marc Bouchere6869a82000-03-20 06:03:29 +00001428
1429 /* Offset of b is the same as a. */
Rusty Russell30fd6e52000-04-23 09:16:06 +00001430 b = (void *)b_elems + ((unsigned char *)a - a_elems);
Marc Bouchere6869a82000-03-20 06:03:29 +00001431
Rusty Russell228e98d2000-04-27 10:28:06 +00001432 if (a->u.match_size != b->u.match_size)
Marc Bouchere6869a82000-03-20 06:03:29 +00001433 return 1;
1434
Rusty Russell228e98d2000-04-27 10:28:06 +00001435 if (strcmp(a->u.user.name, b->u.user.name) != 0)
Marc Bouchere6869a82000-03-20 06:03:29 +00001436 return 1;
1437
Rusty Russell73ef09b2000-07-03 10:24:04 +00001438 *maskptr += ALIGN(sizeof(*a));
Rusty Russelledf14cf2000-04-19 11:26:44 +00001439
Rusty Russell73ef09b2000-07-03 10:24:04 +00001440 for (i = 0; i < a->u.match_size - ALIGN(sizeof(*a)); i++)
Rusty Russelledf14cf2000-04-19 11:26:44 +00001441 if (((a->data[i] ^ b->data[i]) & (*maskptr)[i]) != 0)
Rusty Russell90e712a2000-03-29 04:19:26 +00001442 return 1;
Rusty Russelledf14cf2000-04-19 11:26:44 +00001443 *maskptr += i;
1444 return 0;
1445}
1446
1447static inline int
Rusty Russell733e54b2004-12-16 14:22:23 +00001448target_same(struct rule_head *a, struct rule_head *b,const unsigned char *mask)
Rusty Russelledf14cf2000-04-19 11:26:44 +00001449{
1450 unsigned int i;
Rusty Russell733e54b2004-12-16 14:22:23 +00001451 STRUCT_ENTRY_TARGET *ta, *tb;
Marc Bouchere6869a82000-03-20 06:03:29 +00001452
Rusty Russell733e54b2004-12-16 14:22:23 +00001453 if (a->type != b->type)
1454 return 0;
1455
1456 ta = GET_TARGET(a->entry);
1457 tb = GET_TARGET(b->entry);
1458
1459 switch (a->type) {
1460 case IPTCC_R_FALLTHROUGH:
1461 return 1;
1462 case IPTCC_R_JUMP:
1463 return a->jump == b->jump;
1464 case IPTCC_R_STANDARD:
1465 return ((STRUCT_STANDARD_TARGET *)ta)->verdict
1466 == ((STRUCT_STANDARD_TARGET *)tb)->verdict;
1467 case IPTCC_R_MODULE:
1468 if (ta->u.target_size != tb->u.target_size)
1469 return 0;
1470 if (strcmp(ta->u.user.name, tb->u.user.name) != 0)
1471 return 0;
1472
1473 for (i = 0; i < ta->u.target_size - sizeof(*ta); i++)
Rusty Russelldaade442004-12-29 11:14:52 +00001474 if (((ta->data[i] ^ tb->data[i]) & mask[i]) != 0)
Rusty Russell733e54b2004-12-16 14:22:23 +00001475 return 0;
1476 return 1;
1477 default:
1478 fprintf(stderr, "ERROR: bad type %i\n", a->type);
1479 abort();
1480 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001481}
1482
Rusty Russell733e54b2004-12-16 14:22:23 +00001483static unsigned char *
Rusty Russell79dee072000-05-02 16:45:16 +00001484is_same(const STRUCT_ENTRY *a,
1485 const STRUCT_ENTRY *b,
1486 unsigned char *matchmask);
Marc Bouchere6869a82000-03-20 06:03:29 +00001487
Harald Welte0113fe72004-01-06 19:04:02 +00001488/* Delete the first rule in `chain' which matches `fw'. */
Marc Bouchere6869a82000-03-20 06:03:29 +00001489int
Rusty Russell79dee072000-05-02 16:45:16 +00001490TC_DELETE_ENTRY(const IPT_CHAINLABEL chain,
1491 const STRUCT_ENTRY *origfw,
1492 unsigned char *matchmask,
1493 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001494{
Harald Welteaae69be2004-08-29 23:32:14 +00001495 struct chain_head *c;
Rusty Russelle45c7132004-12-16 13:21:44 +00001496 struct rule_head *r, *i;
Marc Bouchere6869a82000-03-20 06:03:29 +00001497
Rusty Russell79dee072000-05-02 16:45:16 +00001498 iptc_fn = TC_DELETE_ENTRY;
Harald Welteaae69be2004-08-29 23:32:14 +00001499 if (!(c = iptcc_find_label(chain, *handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001500 errno = ENOENT;
1501 return 0;
1502 }
1503
Rusty Russelle45c7132004-12-16 13:21:44 +00001504 /* Create a rule_head from origfw. */
1505 r = iptcc_alloc_rule(c, origfw->next_offset);
1506 if (!r) {
Harald Welte0113fe72004-01-06 19:04:02 +00001507 errno = ENOMEM;
1508 return 0;
1509 }
1510
Rusty Russelle45c7132004-12-16 13:21:44 +00001511 memcpy(r->entry, origfw, origfw->next_offset);
1512 r->counter_map.maptype = COUNTER_MAP_NOMAP;
1513 if (!iptcc_map_target(*handle, r)) {
1514 DEBUGP("unable to map target of rule for chain `%s'\n", chain);
1515 free(r);
1516 return 0;
Patrick McHardyJesper Brouer04a1e4c2006-07-25 01:50:48 +00001517 } else {
1518 /* iptcc_map_target increment target chain references
1519 * since this is a fake rule only used for matching
1520 * the chain references count is decremented again.
1521 */
1522 if (r->type == IPTCC_R_JUMP
1523 && r->jump)
1524 r->jump->references--;
Rusty Russelle45c7132004-12-16 13:21:44 +00001525 }
Harald Welte0113fe72004-01-06 19:04:02 +00001526
Rusty Russelle45c7132004-12-16 13:21:44 +00001527 list_for_each_entry(i, &c->rules, list) {
Rusty Russell733e54b2004-12-16 14:22:23 +00001528 unsigned char *mask;
Harald Weltefe537072004-08-30 20:28:53 +00001529
Rusty Russell733e54b2004-12-16 14:22:23 +00001530 mask = is_same(r->entry, i->entry, matchmask);
1531 if (!mask)
1532 continue;
Martin Josefsson2a5dbbb2004-09-22 21:37:41 +00001533
Rusty Russell733e54b2004-12-16 14:22:23 +00001534 if (!target_same(r, i, mask))
1535 continue;
1536
1537 /* If we are about to delete the rule that is the
1538 * current iterator, move rule iterator back. next
1539 * pointer will then point to real next node */
1540 if (i == (*handle)->rule_iterator_cur) {
1541 (*handle)->rule_iterator_cur =
1542 list_entry((*handle)->rule_iterator_cur->list.prev,
1543 struct rule_head, list);
Marc Bouchere6869a82000-03-20 06:03:29 +00001544 }
Rusty Russell733e54b2004-12-16 14:22:23 +00001545
1546 c->num_rules--;
1547 iptcc_delete_rule(i);
1548
1549 set_changed(*handle);
1550 free(r);
1551 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00001552 }
1553
Rusty Russelle45c7132004-12-16 13:21:44 +00001554 free(r);
Marc Bouchere6869a82000-03-20 06:03:29 +00001555 errno = ENOENT;
1556 return 0;
Rusty Russell7e53bf92000-03-20 07:03:28 +00001557}
Harald Welteaae69be2004-08-29 23:32:14 +00001558
Marc Bouchere6869a82000-03-20 06:03:29 +00001559
1560/* Delete the rule in position `rulenum' in `chain'. */
1561int
Rusty Russell79dee072000-05-02 16:45:16 +00001562TC_DELETE_NUM_ENTRY(const IPT_CHAINLABEL chain,
1563 unsigned int rulenum,
1564 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001565{
Harald Welteaae69be2004-08-29 23:32:14 +00001566 struct chain_head *c;
1567 struct rule_head *r;
Marc Bouchere6869a82000-03-20 06:03:29 +00001568
Rusty Russell79dee072000-05-02 16:45:16 +00001569 iptc_fn = TC_DELETE_NUM_ENTRY;
Harald Welteaae69be2004-08-29 23:32:14 +00001570
1571 if (!(c = iptcc_find_label(chain, *handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001572 errno = ENOENT;
1573 return 0;
1574 }
1575
Martin Josefssona5616dc2004-10-24 22:27:31 +00001576 if (rulenum >= c->num_rules) {
Martin Josefsson631f3612004-09-23 19:25:06 +00001577 errno = E2BIG;
1578 return 0;
1579 }
1580
1581 /* Take advantage of the double linked list if possible. */
Martin Josefssona5616dc2004-10-24 22:27:31 +00001582 if (rulenum + 1 <= c->num_rules/2) {
1583 r = iptcc_get_rule_num(c, rulenum + 1);
1584 } else {
1585 r = iptcc_get_rule_num_reverse(c, c->num_rules - rulenum);
Marc Bouchere6869a82000-03-20 06:03:29 +00001586 }
1587
Harald Welteaae69be2004-08-29 23:32:14 +00001588 /* If we are about to delete the rule that is the current
1589 * iterator, move rule iterator back. next pointer will then
1590 * point to real next node */
1591 if (r == (*handle)->rule_iterator_cur) {
1592 (*handle)->rule_iterator_cur =
1593 list_entry((*handle)->rule_iterator_cur->list.prev,
1594 struct rule_head, list);
Harald Welte0113fe72004-01-06 19:04:02 +00001595 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001596
Harald Welteaae69be2004-08-29 23:32:14 +00001597 c->num_rules--;
1598 iptcc_delete_rule(r);
1599
Martin Josefsson2a5dbbb2004-09-22 21:37:41 +00001600 set_changed(*handle);
1601
Harald Welteaae69be2004-08-29 23:32:14 +00001602 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00001603}
1604
1605/* Check the packet `fw' on chain `chain'. Returns the verdict, or
1606 NULL and sets errno. */
1607const char *
Rusty Russell79dee072000-05-02 16:45:16 +00001608TC_CHECK_PACKET(const IPT_CHAINLABEL chain,
1609 STRUCT_ENTRY *entry,
1610 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001611{
Derrik Pates664c0a32005-02-01 13:28:14 +00001612 iptc_fn = TC_CHECK_PACKET;
Marc Bouchere6869a82000-03-20 06:03:29 +00001613 errno = ENOSYS;
1614 return NULL;
1615}
1616
1617/* Flushes the entries in the given chain (ie. empties chain). */
1618int
Rusty Russell79dee072000-05-02 16:45:16 +00001619TC_FLUSH_ENTRIES(const IPT_CHAINLABEL chain, TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001620{
Harald Welteaae69be2004-08-29 23:32:14 +00001621 struct chain_head *c;
1622 struct rule_head *r, *tmp;
Marc Bouchere6869a82000-03-20 06:03:29 +00001623
Harald Welte0113fe72004-01-06 19:04:02 +00001624 iptc_fn = TC_FLUSH_ENTRIES;
Harald Welteaae69be2004-08-29 23:32:14 +00001625 if (!(c = iptcc_find_label(chain, *handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001626 errno = ENOENT;
1627 return 0;
1628 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001629
Harald Welteaae69be2004-08-29 23:32:14 +00001630 list_for_each_entry_safe(r, tmp, &c->rules, list) {
1631 iptcc_delete_rule(r);
1632 }
1633
1634 c->num_rules = 0;
1635
1636 set_changed(*handle);
1637
1638 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00001639}
1640
1641/* Zeroes the counters in a chain. */
1642int
Rusty Russell79dee072000-05-02 16:45:16 +00001643TC_ZERO_ENTRIES(const IPT_CHAINLABEL chain, TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001644{
Harald Welteaae69be2004-08-29 23:32:14 +00001645 struct chain_head *c;
1646 struct rule_head *r;
Rusty Russell7e53bf92000-03-20 07:03:28 +00001647
Derrik Pates664c0a32005-02-01 13:28:14 +00001648 iptc_fn = TC_ZERO_ENTRIES;
Harald Welteaae69be2004-08-29 23:32:14 +00001649 if (!(c = iptcc_find_label(chain, *handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001650 errno = ENOENT;
1651 return 0;
1652 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001653
Andy Gaye5bd1d72006-08-22 02:56:41 +00001654 if (c->counter_map.maptype == COUNTER_MAP_NORMAL_MAP)
1655 c->counter_map.maptype = COUNTER_MAP_ZEROED;
1656
Harald Welteaae69be2004-08-29 23:32:14 +00001657 list_for_each_entry(r, &c->rules, list) {
1658 if (r->counter_map.maptype == COUNTER_MAP_NORMAL_MAP)
1659 r->counter_map.maptype = COUNTER_MAP_ZEROED;
Marc Bouchere6869a82000-03-20 06:03:29 +00001660 }
Harald Welteaae69be2004-08-29 23:32:14 +00001661
Rusty Russell175f6412000-03-24 09:32:20 +00001662 set_changed(*handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001663
Marc Bouchere6869a82000-03-20 06:03:29 +00001664 return 1;
1665}
1666
Harald Welte1cef74d2001-01-05 15:22:59 +00001667STRUCT_COUNTERS *
1668TC_READ_COUNTER(const IPT_CHAINLABEL chain,
1669 unsigned int rulenum,
1670 TC_HANDLE_T *handle)
1671{
Harald Welteaae69be2004-08-29 23:32:14 +00001672 struct chain_head *c;
1673 struct rule_head *r;
Harald Welte1cef74d2001-01-05 15:22:59 +00001674
1675 iptc_fn = TC_READ_COUNTER;
1676 CHECK(*handle);
1677
Harald Welteaae69be2004-08-29 23:32:14 +00001678 if (!(c = iptcc_find_label(chain, *handle))) {
Harald Welte1cef74d2001-01-05 15:22:59 +00001679 errno = ENOENT;
1680 return NULL;
1681 }
1682
Harald Welteaae69be2004-08-29 23:32:14 +00001683 if (!(r = iptcc_get_rule_num(c, rulenum))) {
Harald Welte0113fe72004-01-06 19:04:02 +00001684 errno = E2BIG;
1685 return NULL;
1686 }
1687
Harald Welteaae69be2004-08-29 23:32:14 +00001688 return &r->entry[0].counters;
Harald Welte1cef74d2001-01-05 15:22:59 +00001689}
1690
1691int
1692TC_ZERO_COUNTER(const IPT_CHAINLABEL chain,
1693 unsigned int rulenum,
1694 TC_HANDLE_T *handle)
1695{
Harald Welteaae69be2004-08-29 23:32:14 +00001696 struct chain_head *c;
1697 struct rule_head *r;
Harald Welte1cef74d2001-01-05 15:22:59 +00001698
1699 iptc_fn = TC_ZERO_COUNTER;
1700 CHECK(*handle);
1701
Harald Welteaae69be2004-08-29 23:32:14 +00001702 if (!(c = iptcc_find_label(chain, *handle))) {
Harald Welte1cef74d2001-01-05 15:22:59 +00001703 errno = ENOENT;
1704 return 0;
1705 }
1706
Harald Welteaae69be2004-08-29 23:32:14 +00001707 if (!(r = iptcc_get_rule_num(c, rulenum))) {
Harald Welte0113fe72004-01-06 19:04:02 +00001708 errno = E2BIG;
1709 return 0;
1710 }
1711
Harald Welteaae69be2004-08-29 23:32:14 +00001712 if (r->counter_map.maptype == COUNTER_MAP_NORMAL_MAP)
1713 r->counter_map.maptype = COUNTER_MAP_ZEROED;
Harald Welte1cef74d2001-01-05 15:22:59 +00001714
1715 set_changed(*handle);
1716
1717 return 1;
1718}
1719
1720int
1721TC_SET_COUNTER(const IPT_CHAINLABEL chain,
1722 unsigned int rulenum,
1723 STRUCT_COUNTERS *counters,
1724 TC_HANDLE_T *handle)
1725{
Harald Welteaae69be2004-08-29 23:32:14 +00001726 struct chain_head *c;
1727 struct rule_head *r;
Harald Welte1cef74d2001-01-05 15:22:59 +00001728 STRUCT_ENTRY *e;
Harald Welte1cef74d2001-01-05 15:22:59 +00001729
1730 iptc_fn = TC_SET_COUNTER;
1731 CHECK(*handle);
1732
Harald Welteaae69be2004-08-29 23:32:14 +00001733 if (!(c = iptcc_find_label(chain, *handle))) {
Harald Welte1cef74d2001-01-05 15:22:59 +00001734 errno = ENOENT;
1735 return 0;
1736 }
Harald Welte0113fe72004-01-06 19:04:02 +00001737
Harald Welteaae69be2004-08-29 23:32:14 +00001738 if (!(r = iptcc_get_rule_num(c, rulenum))) {
Harald Welte0113fe72004-01-06 19:04:02 +00001739 errno = E2BIG;
1740 return 0;
1741 }
1742
Harald Welteaae69be2004-08-29 23:32:14 +00001743 e = r->entry;
1744 r->counter_map.maptype = COUNTER_MAP_SET;
Harald Welte0113fe72004-01-06 19:04:02 +00001745
1746 memcpy(&e->counters, counters, sizeof(STRUCT_COUNTERS));
Harald Welte1cef74d2001-01-05 15:22:59 +00001747
1748 set_changed(*handle);
1749
1750 return 1;
1751}
1752
Marc Bouchere6869a82000-03-20 06:03:29 +00001753/* Creates a new chain. */
1754/* To create a chain, create two rules: error node and unconditional
1755 * return. */
1756int
Rusty Russell79dee072000-05-02 16:45:16 +00001757TC_CREATE_CHAIN(const IPT_CHAINLABEL chain, TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001758{
Harald Welteaae69be2004-08-29 23:32:14 +00001759 static struct chain_head *c;
Marc Bouchere6869a82000-03-20 06:03:29 +00001760
Rusty Russell79dee072000-05-02 16:45:16 +00001761 iptc_fn = TC_CREATE_CHAIN;
Marc Bouchere6869a82000-03-20 06:03:29 +00001762
1763 /* find_label doesn't cover built-in targets: DROP, ACCEPT,
1764 QUEUE, RETURN. */
Harald Welteaae69be2004-08-29 23:32:14 +00001765 if (iptcc_find_label(chain, *handle)
Rusty Russell79dee072000-05-02 16:45:16 +00001766 || strcmp(chain, LABEL_DROP) == 0
1767 || strcmp(chain, LABEL_ACCEPT) == 0
Rusty Russell67088e72000-05-10 01:18:57 +00001768 || strcmp(chain, LABEL_QUEUE) == 0
Rusty Russell79dee072000-05-02 16:45:16 +00001769 || strcmp(chain, LABEL_RETURN) == 0) {
Harald Welteaae69be2004-08-29 23:32:14 +00001770 DEBUGP("Chain `%s' already exists\n", chain);
Marc Bouchere6869a82000-03-20 06:03:29 +00001771 errno = EEXIST;
1772 return 0;
1773 }
1774
Rusty Russell79dee072000-05-02 16:45:16 +00001775 if (strlen(chain)+1 > sizeof(IPT_CHAINLABEL)) {
Harald Welteaae69be2004-08-29 23:32:14 +00001776 DEBUGP("Chain name `%s' too long\n", chain);
Marc Bouchere6869a82000-03-20 06:03:29 +00001777 errno = EINVAL;
1778 return 0;
1779 }
1780
Harald Welteaae69be2004-08-29 23:32:14 +00001781 c = iptcc_alloc_chain_head(chain, 0);
1782 if (!c) {
1783 DEBUGP("Cannot allocate memory for chain `%s'\n", chain);
1784 errno = ENOMEM;
1785 return 0;
Marc Bouchere6869a82000-03-20 06:03:29 +00001786
Harald Welteaae69be2004-08-29 23:32:14 +00001787 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001788
Harald Welteaae69be2004-08-29 23:32:14 +00001789 DEBUGP("Creating chain `%s'\n", chain);
Jesper Dangaard Brouerd8cb7872007-11-28 08:40:26 +00001790 iptc_insert_chain(*handle, c); /* Insert sorted */
Harald Welte0113fe72004-01-06 19:04:02 +00001791
1792 set_changed(*handle);
1793
Harald Welteaae69be2004-08-29 23:32:14 +00001794 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00001795}
1796
1797/* Get the number of references to this chain. */
1798int
Rusty Russell79dee072000-05-02 16:45:16 +00001799TC_GET_REFERENCES(unsigned int *ref, const IPT_CHAINLABEL chain,
1800 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001801{
Harald Welteaae69be2004-08-29 23:32:14 +00001802 struct chain_head *c;
Marc Bouchere6869a82000-03-20 06:03:29 +00001803
Derrik Pates664c0a32005-02-01 13:28:14 +00001804 iptc_fn = TC_GET_REFERENCES;
Harald Welteaae69be2004-08-29 23:32:14 +00001805 if (!(c = iptcc_find_label(chain, *handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001806 errno = ENOENT;
1807 return 0;
1808 }
1809
Harald Welteaae69be2004-08-29 23:32:14 +00001810 *ref = c->references;
1811
Marc Bouchere6869a82000-03-20 06:03:29 +00001812 return 1;
1813}
1814
1815/* Deletes a chain. */
1816int
Rusty Russell79dee072000-05-02 16:45:16 +00001817TC_DELETE_CHAIN(const IPT_CHAINLABEL chain, TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001818{
Marc Bouchere6869a82000-03-20 06:03:29 +00001819 unsigned int references;
Harald Welteaae69be2004-08-29 23:32:14 +00001820 struct chain_head *c;
Rusty Russell7e53bf92000-03-20 07:03:28 +00001821
Rusty Russell79dee072000-05-02 16:45:16 +00001822 iptc_fn = TC_DELETE_CHAIN;
Marc Bouchere6869a82000-03-20 06:03:29 +00001823
Harald Welteaae69be2004-08-29 23:32:14 +00001824 if (!(c = iptcc_find_label(chain, *handle))) {
1825 DEBUGP("cannot find chain `%s'\n", chain);
Marc Bouchere6869a82000-03-20 06:03:29 +00001826 errno = ENOENT;
1827 return 0;
1828 }
1829
Harald Welteaae69be2004-08-29 23:32:14 +00001830 if (TC_BUILTIN(chain, *handle)) {
1831 DEBUGP("cannot remove builtin chain `%s'\n", chain);
1832 errno = EINVAL;
1833 return 0;
1834 }
1835
1836 if (!TC_GET_REFERENCES(&references, chain, handle)) {
1837 DEBUGP("cannot get references on chain `%s'\n", chain);
1838 return 0;
1839 }
1840
1841 if (references > 0) {
1842 DEBUGP("chain `%s' still has references\n", chain);
1843 errno = EMLINK;
1844 return 0;
1845 }
1846
1847 if (c->num_rules) {
1848 DEBUGP("chain `%s' is not empty\n", chain);
Marc Bouchere6869a82000-03-20 06:03:29 +00001849 errno = ENOTEMPTY;
1850 return 0;
1851 }
1852
Harald Welteaae69be2004-08-29 23:32:14 +00001853 /* If we are about to delete the chain that is the current
1854 * iterator, move chain iterator firward. */
1855 if (c == (*handle)->chain_iterator_cur)
1856 iptcc_chain_iterator_advance(*handle);
Harald Welte0113fe72004-01-06 19:04:02 +00001857
Harald Welteaae69be2004-08-29 23:32:14 +00001858 list_del(&c->list);
1859 free(c);
Harald Welte0113fe72004-01-06 19:04:02 +00001860
Harald Welteaae69be2004-08-29 23:32:14 +00001861 DEBUGP("chain `%s' deleted\n", chain);
1862
1863 set_changed(*handle);
1864
1865 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00001866}
1867
1868/* Renames a chain. */
Rusty Russell79dee072000-05-02 16:45:16 +00001869int TC_RENAME_CHAIN(const IPT_CHAINLABEL oldname,
1870 const IPT_CHAINLABEL newname,
1871 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001872{
Harald Welteaae69be2004-08-29 23:32:14 +00001873 struct chain_head *c;
Rusty Russell79dee072000-05-02 16:45:16 +00001874 iptc_fn = TC_RENAME_CHAIN;
Marc Bouchere6869a82000-03-20 06:03:29 +00001875
Harald Welte1de80462000-10-30 12:00:27 +00001876 /* find_label doesn't cover built-in targets: DROP, ACCEPT,
1877 QUEUE, RETURN. */
Harald Welteaae69be2004-08-29 23:32:14 +00001878 if (iptcc_find_label(newname, *handle)
Rusty Russell79dee072000-05-02 16:45:16 +00001879 || strcmp(newname, LABEL_DROP) == 0
1880 || strcmp(newname, LABEL_ACCEPT) == 0
Harald Welte1de80462000-10-30 12:00:27 +00001881 || strcmp(newname, LABEL_QUEUE) == 0
Rusty Russell79dee072000-05-02 16:45:16 +00001882 || strcmp(newname, LABEL_RETURN) == 0) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001883 errno = EEXIST;
1884 return 0;
1885 }
1886
Harald Welteaae69be2004-08-29 23:32:14 +00001887 if (!(c = iptcc_find_label(oldname, *handle))
Rusty Russell79dee072000-05-02 16:45:16 +00001888 || TC_BUILTIN(oldname, *handle)) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001889 errno = ENOENT;
1890 return 0;
1891 }
1892
Rusty Russell79dee072000-05-02 16:45:16 +00001893 if (strlen(newname)+1 > sizeof(IPT_CHAINLABEL)) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001894 errno = EINVAL;
1895 return 0;
1896 }
1897
Harald Welteaae69be2004-08-29 23:32:14 +00001898 strncpy(c->name, newname, sizeof(IPT_CHAINLABEL));
1899
Harald Welte0113fe72004-01-06 19:04:02 +00001900 set_changed(*handle);
1901
Marc Bouchere6869a82000-03-20 06:03:29 +00001902 return 1;
1903}
1904
1905/* Sets the policy on a built-in chain. */
1906int
Rusty Russell79dee072000-05-02 16:45:16 +00001907TC_SET_POLICY(const IPT_CHAINLABEL chain,
1908 const IPT_CHAINLABEL policy,
Harald Welte1cef74d2001-01-05 15:22:59 +00001909 STRUCT_COUNTERS *counters,
Rusty Russell79dee072000-05-02 16:45:16 +00001910 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001911{
Harald Welteaae69be2004-08-29 23:32:14 +00001912 struct chain_head *c;
Marc Bouchere6869a82000-03-20 06:03:29 +00001913
Rusty Russell79dee072000-05-02 16:45:16 +00001914 iptc_fn = TC_SET_POLICY;
Marc Bouchere6869a82000-03-20 06:03:29 +00001915
Harald Welteaae69be2004-08-29 23:32:14 +00001916 if (!(c = iptcc_find_label(chain, *handle))) {
1917 DEBUGP("cannot find chain `%s'\n", chain);
1918 errno = ENOENT;
Marc Bouchere6869a82000-03-20 06:03:29 +00001919 return 0;
1920 }
1921
Harald Welteaae69be2004-08-29 23:32:14 +00001922 if (!iptcc_is_builtin(c)) {
1923 DEBUGP("cannot set policy of userdefinedchain `%s'\n", chain);
1924 errno = ENOENT;
1925 return 0;
1926 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001927
Rusty Russell79dee072000-05-02 16:45:16 +00001928 if (strcmp(policy, LABEL_ACCEPT) == 0)
Harald Welteaae69be2004-08-29 23:32:14 +00001929 c->verdict = -NF_ACCEPT - 1;
Rusty Russell79dee072000-05-02 16:45:16 +00001930 else if (strcmp(policy, LABEL_DROP) == 0)
Harald Welteaae69be2004-08-29 23:32:14 +00001931 c->verdict = -NF_DROP - 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00001932 else {
1933 errno = EINVAL;
1934 return 0;
1935 }
Harald Welte1cef74d2001-01-05 15:22:59 +00001936
Harald Welte1cef74d2001-01-05 15:22:59 +00001937 if (counters) {
1938 /* set byte and packet counters */
Harald Welteaae69be2004-08-29 23:32:14 +00001939 memcpy(&c->counters, counters, sizeof(STRUCT_COUNTERS));
1940 c->counter_map.maptype = COUNTER_MAP_SET;
Harald Welte1cef74d2001-01-05 15:22:59 +00001941 } else {
Harald Welteaae69be2004-08-29 23:32:14 +00001942 c->counter_map.maptype = COUNTER_MAP_NOMAP;
Harald Welte1cef74d2001-01-05 15:22:59 +00001943 }
1944
Rusty Russell175f6412000-03-24 09:32:20 +00001945 set_changed(*handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001946
Marc Bouchere6869a82000-03-20 06:03:29 +00001947 return 1;
1948}
1949
1950/* Without this, on gcc 2.7.2.3, we get:
Rusty Russell79dee072000-05-02 16:45:16 +00001951 libiptc.c: In function `TC_COMMIT':
Marc Bouchere6869a82000-03-20 06:03:29 +00001952 libiptc.c:833: fixed or forbidden register was spilled.
1953 This may be due to a compiler bug or to impossible asm
1954 statements or clauses.
1955*/
1956static void
Rusty Russell79dee072000-05-02 16:45:16 +00001957subtract_counters(STRUCT_COUNTERS *answer,
1958 const STRUCT_COUNTERS *a,
1959 const STRUCT_COUNTERS *b)
Marc Bouchere6869a82000-03-20 06:03:29 +00001960{
1961 answer->pcnt = a->pcnt - b->pcnt;
1962 answer->bcnt = a->bcnt - b->bcnt;
1963}
1964
Harald Welteaae69be2004-08-29 23:32:14 +00001965
1966static void counters_nomap(STRUCT_COUNTERS_INFO *newcounters,
1967 unsigned int index)
1968{
1969 newcounters->counters[index] = ((STRUCT_COUNTERS) { 0, 0});
1970 DEBUGP_C("NOMAP => zero\n");
1971}
1972
1973static void counters_normal_map(STRUCT_COUNTERS_INFO *newcounters,
1974 STRUCT_REPLACE *repl,
1975 unsigned int index,
1976 unsigned int mappos)
1977{
1978 /* Original read: X.
1979 * Atomic read on replacement: X + Y.
1980 * Currently in kernel: Z.
1981 * Want in kernel: X + Y + Z.
1982 * => Add in X + Y
1983 * => Add in replacement read.
1984 */
1985 newcounters->counters[index] = repl->counters[mappos];
1986 DEBUGP_C("NORMAL_MAP => mappos %u \n", mappos);
1987}
1988
1989static void counters_map_zeroed(STRUCT_COUNTERS_INFO *newcounters,
1990 STRUCT_REPLACE *repl,
1991 unsigned int index,
1992 unsigned int mappos,
1993 STRUCT_COUNTERS *counters)
1994{
1995 /* Original read: X.
1996 * Atomic read on replacement: X + Y.
1997 * Currently in kernel: Z.
1998 * Want in kernel: Y + Z.
1999 * => Add in Y.
2000 * => Add in (replacement read - original read).
2001 */
2002 subtract_counters(&newcounters->counters[index],
2003 &repl->counters[mappos],
2004 counters);
2005 DEBUGP_C("ZEROED => mappos %u\n", mappos);
2006}
2007
2008static void counters_map_set(STRUCT_COUNTERS_INFO *newcounters,
2009 unsigned int index,
2010 STRUCT_COUNTERS *counters)
2011{
2012 /* Want to set counter (iptables-restore) */
2013
2014 memcpy(&newcounters->counters[index], counters,
2015 sizeof(STRUCT_COUNTERS));
2016
2017 DEBUGP_C("SET\n");
2018}
2019
2020
Marc Bouchere6869a82000-03-20 06:03:29 +00002021int
Rusty Russell79dee072000-05-02 16:45:16 +00002022TC_COMMIT(TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00002023{
2024 /* Replace, then map back the counters. */
Rusty Russell79dee072000-05-02 16:45:16 +00002025 STRUCT_REPLACE *repl;
2026 STRUCT_COUNTERS_INFO *newcounters;
Harald Welteaae69be2004-08-29 23:32:14 +00002027 struct chain_head *c;
2028 int ret;
Martin Josefsson841e4ae2003-05-02 15:30:11 +00002029 size_t counterlen;
Harald Welteaae69be2004-08-29 23:32:14 +00002030 int new_number;
2031 unsigned int new_size;
Marc Bouchere6869a82000-03-20 06:03:29 +00002032
Derrik Pates664c0a32005-02-01 13:28:14 +00002033 iptc_fn = TC_COMMIT;
Marc Bouchere6869a82000-03-20 06:03:29 +00002034 CHECK(*handle);
Martin Josefsson841e4ae2003-05-02 15:30:11 +00002035
Marc Bouchere6869a82000-03-20 06:03:29 +00002036 /* Don't commit if nothing changed. */
2037 if (!(*handle)->changed)
2038 goto finished;
2039
Harald Welteaae69be2004-08-29 23:32:14 +00002040 new_number = iptcc_compile_table_prep(*handle, &new_size);
2041 if (new_number < 0) {
2042 errno = ENOMEM;
Harald Welted6ba6f52005-11-12 10:39:40 +00002043 goto out_zero;
Harald Welteaae69be2004-08-29 23:32:14 +00002044 }
2045
2046 repl = malloc(sizeof(*repl) + new_size);
Marc Bouchere6869a82000-03-20 06:03:29 +00002047 if (!repl) {
2048 errno = ENOMEM;
Harald Welted6ba6f52005-11-12 10:39:40 +00002049 goto out_zero;
Marc Bouchere6869a82000-03-20 06:03:29 +00002050 }
Martin Josefssonad3b4f92004-09-22 22:04:07 +00002051 memset(repl, 0, sizeof(*repl) + new_size);
Harald Welteaae69be2004-08-29 23:32:14 +00002052
Rusty Russelle45c7132004-12-16 13:21:44 +00002053#if 0
2054 TC_DUMP_ENTRIES(*handle);
2055#endif
2056
Harald Welteaae69be2004-08-29 23:32:14 +00002057 counterlen = sizeof(STRUCT_COUNTERS_INFO)
2058 + sizeof(STRUCT_COUNTERS) * new_number;
Marc Bouchere6869a82000-03-20 06:03:29 +00002059
2060 /* These are the old counters we will get from kernel */
Rusty Russell79dee072000-05-02 16:45:16 +00002061 repl->counters = malloc(sizeof(STRUCT_COUNTERS)
Marc Bouchere6869a82000-03-20 06:03:29 +00002062 * (*handle)->info.num_entries);
2063 if (!repl->counters) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002064 errno = ENOMEM;
Harald Welted6ba6f52005-11-12 10:39:40 +00002065 goto out_free_repl;
Marc Bouchere6869a82000-03-20 06:03:29 +00002066 }
Marc Bouchere6869a82000-03-20 06:03:29 +00002067 /* These are the counters we're going to put back, later. */
2068 newcounters = malloc(counterlen);
2069 if (!newcounters) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002070 errno = ENOMEM;
Harald Welted6ba6f52005-11-12 10:39:40 +00002071 goto out_free_repl_counters;
Marc Bouchere6869a82000-03-20 06:03:29 +00002072 }
Harald Welteaae69be2004-08-29 23:32:14 +00002073 memset(newcounters, 0, counterlen);
Marc Bouchere6869a82000-03-20 06:03:29 +00002074
2075 strcpy(repl->name, (*handle)->info.name);
Harald Welteaae69be2004-08-29 23:32:14 +00002076 repl->num_entries = new_number;
2077 repl->size = new_size;
2078
Marc Bouchere6869a82000-03-20 06:03:29 +00002079 repl->num_counters = (*handle)->info.num_entries;
2080 repl->valid_hooks = (*handle)->info.valid_hooks;
Harald Welteaae69be2004-08-29 23:32:14 +00002081
2082 DEBUGP("num_entries=%u, size=%u, num_counters=%u\n",
2083 repl->num_entries, repl->size, repl->num_counters);
2084
2085 ret = iptcc_compile_table(*handle, repl);
2086 if (ret < 0) {
2087 errno = ret;
Harald Welted6ba6f52005-11-12 10:39:40 +00002088 goto out_free_newcounters;
Harald Welteaae69be2004-08-29 23:32:14 +00002089 }
2090
2091
2092#ifdef IPTC_DEBUG2
2093 {
2094 int fd = open("/tmp/libiptc-so_set_replace.blob",
2095 O_CREAT|O_WRONLY);
2096 if (fd >= 0) {
2097 write(fd, repl, sizeof(*repl) + repl->size);
2098 close(fd);
2099 }
2100 }
2101#endif
Marc Bouchere6869a82000-03-20 06:03:29 +00002102
Harald Welted6ba6f52005-11-12 10:39:40 +00002103 ret = setsockopt(sockfd, TC_IPPROTO, SO_SET_REPLACE, repl,
2104 sizeof(*repl) + repl->size);
Patrick McHardye0865ad2006-04-22 02:08:56 +00002105 if (ret < 0)
Harald Welted6ba6f52005-11-12 10:39:40 +00002106 goto out_free_newcounters;
Marc Bouchere6869a82000-03-20 06:03:29 +00002107
2108 /* Put counters back. */
2109 strcpy(newcounters->name, (*handle)->info.name);
Harald Welteaae69be2004-08-29 23:32:14 +00002110 newcounters->num_counters = new_number;
Marc Bouchere6869a82000-03-20 06:03:29 +00002111
Harald Welteaae69be2004-08-29 23:32:14 +00002112 list_for_each_entry(c, &(*handle)->chains, list) {
2113 struct rule_head *r;
Marc Bouchere6869a82000-03-20 06:03:29 +00002114
Harald Welteaae69be2004-08-29 23:32:14 +00002115 /* Builtin chains have their own counters */
2116 if (iptcc_is_builtin(c)) {
2117 DEBUGP("counter for chain-index %u: ", c->foot_index);
2118 switch(c->counter_map.maptype) {
2119 case COUNTER_MAP_NOMAP:
2120 counters_nomap(newcounters, c->foot_index);
2121 break;
2122 case COUNTER_MAP_NORMAL_MAP:
2123 counters_normal_map(newcounters, repl,
2124 c->foot_index,
2125 c->counter_map.mappos);
2126 break;
2127 case COUNTER_MAP_ZEROED:
2128 counters_map_zeroed(newcounters, repl,
2129 c->foot_index,
2130 c->counter_map.mappos,
2131 &c->counters);
2132 break;
2133 case COUNTER_MAP_SET:
2134 counters_map_set(newcounters, c->foot_index,
2135 &c->counters);
2136 break;
2137 }
2138 }
Harald Welte1cef74d2001-01-05 15:22:59 +00002139
Harald Welteaae69be2004-08-29 23:32:14 +00002140 list_for_each_entry(r, &c->rules, list) {
2141 DEBUGP("counter for index %u: ", r->index);
2142 switch (r->counter_map.maptype) {
2143 case COUNTER_MAP_NOMAP:
2144 counters_nomap(newcounters, r->index);
2145 break;
Harald Welte1cef74d2001-01-05 15:22:59 +00002146
Harald Welteaae69be2004-08-29 23:32:14 +00002147 case COUNTER_MAP_NORMAL_MAP:
2148 counters_normal_map(newcounters, repl,
2149 r->index,
2150 r->counter_map.mappos);
2151 break;
Harald Welte1cef74d2001-01-05 15:22:59 +00002152
Harald Welteaae69be2004-08-29 23:32:14 +00002153 case COUNTER_MAP_ZEROED:
2154 counters_map_zeroed(newcounters, repl,
2155 r->index,
2156 r->counter_map.mappos,
2157 &r->entry->counters);
2158 break;
2159
2160 case COUNTER_MAP_SET:
2161 counters_map_set(newcounters, r->index,
2162 &r->entry->counters);
2163 break;
2164 }
Marc Bouchere6869a82000-03-20 06:03:29 +00002165 }
2166 }
Rusty Russell62527ce2000-09-04 09:45:54 +00002167
Harald Welteaae69be2004-08-29 23:32:14 +00002168#ifdef IPTC_DEBUG2
2169 {
2170 int fd = open("/tmp/libiptc-so_set_add_counters.blob",
2171 O_CREAT|O_WRONLY);
2172 if (fd >= 0) {
2173 write(fd, newcounters, counterlen);
2174 close(fd);
2175 }
2176 }
2177#endif
2178
Harald Welted6ba6f52005-11-12 10:39:40 +00002179 ret = setsockopt(sockfd, TC_IPPROTO, SO_SET_ADD_COUNTERS,
2180 newcounters, counterlen);
Patrick McHardye0865ad2006-04-22 02:08:56 +00002181 if (ret < 0)
Harald Welted6ba6f52005-11-12 10:39:40 +00002182 goto out_free_newcounters;
Marc Bouchere6869a82000-03-20 06:03:29 +00002183
2184 free(repl->counters);
2185 free(repl);
2186 free(newcounters);
2187
Harald Welted6ba6f52005-11-12 10:39:40 +00002188finished:
Martin Josefsson841e4ae2003-05-02 15:30:11 +00002189 TC_FREE(handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00002190 return 1;
Harald Welted6ba6f52005-11-12 10:39:40 +00002191
2192out_free_newcounters:
2193 free(newcounters);
2194out_free_repl_counters:
2195 free(repl->counters);
2196out_free_repl:
2197 free(repl);
2198out_zero:
2199 return 0;
Marc Bouchere6869a82000-03-20 06:03:29 +00002200}
2201
2202/* Get raw socket. */
2203int
Patrick McHardy0b639362007-09-08 16:00:01 +00002204TC_GET_RAW_SOCKET(void)
Marc Bouchere6869a82000-03-20 06:03:29 +00002205{
2206 return sockfd;
2207}
2208
2209/* Translates errno numbers into more human-readable form than strerror. */
2210const char *
Rusty Russell79dee072000-05-02 16:45:16 +00002211TC_STRERROR(int err)
Marc Bouchere6869a82000-03-20 06:03:29 +00002212{
2213 unsigned int i;
2214 struct table_struct {
2215 void *fn;
2216 int err;
2217 const char *message;
2218 } table [] =
Harald Welte4ccfa632001-07-30 15:12:43 +00002219 { { TC_INIT, EPERM, "Permission denied (you must be root)" },
Rusty Russell79dee072000-05-02 16:45:16 +00002220 { TC_INIT, EINVAL, "Module is wrong version" },
Harald Welte4ccfa632001-07-30 15:12:43 +00002221 { TC_INIT, ENOENT,
2222 "Table does not exist (do you need to insmod?)" },
Rusty Russell79dee072000-05-02 16:45:16 +00002223 { TC_DELETE_CHAIN, ENOTEMPTY, "Chain is not empty" },
2224 { TC_DELETE_CHAIN, EINVAL, "Can't delete built-in chain" },
2225 { TC_DELETE_CHAIN, EMLINK,
Marc Bouchere6869a82000-03-20 06:03:29 +00002226 "Can't delete chain with references left" },
Rusty Russell79dee072000-05-02 16:45:16 +00002227 { TC_CREATE_CHAIN, EEXIST, "Chain already exists" },
2228 { TC_INSERT_ENTRY, E2BIG, "Index of insertion too big" },
2229 { TC_REPLACE_ENTRY, E2BIG, "Index of replacement too big" },
2230 { TC_DELETE_NUM_ENTRY, E2BIG, "Index of deletion too big" },
Harald Welte1cef74d2001-01-05 15:22:59 +00002231 { TC_READ_COUNTER, E2BIG, "Index of counter too big" },
2232 { TC_ZERO_COUNTER, E2BIG, "Index of counter too big" },
Rusty Russell79dee072000-05-02 16:45:16 +00002233 { TC_INSERT_ENTRY, ELOOP, "Loop found in table" },
2234 { TC_INSERT_ENTRY, EINVAL, "Target problem" },
Marc Bouchere6869a82000-03-20 06:03:29 +00002235 /* EINVAL for CHECK probably means bad interface. */
Rusty Russell79dee072000-05-02 16:45:16 +00002236 { TC_CHECK_PACKET, EINVAL,
Marc Boucherc8264992000-04-22 22:34:44 +00002237 "Bad arguments (does that interface exist?)" },
Harald Welte4ccfa632001-07-30 15:12:43 +00002238 { TC_CHECK_PACKET, ENOSYS,
2239 "Checking will most likely never get implemented" },
Marc Bouchere6869a82000-03-20 06:03:29 +00002240 /* ENOENT for DELETE probably means no matching rule */
Rusty Russell79dee072000-05-02 16:45:16 +00002241 { TC_DELETE_ENTRY, ENOENT,
Marc Boucherc8264992000-04-22 22:34:44 +00002242 "Bad rule (does a matching rule exist in that chain?)" },
Rusty Russell79dee072000-05-02 16:45:16 +00002243 { TC_SET_POLICY, ENOENT,
Marc Boucherc8264992000-04-22 22:34:44 +00002244 "Bad built-in chain name" },
Rusty Russell79dee072000-05-02 16:45:16 +00002245 { TC_SET_POLICY, EINVAL,
Marc Boucherc8264992000-04-22 22:34:44 +00002246 "Bad policy name" },
Harald Welte4ccfa632001-07-30 15:12:43 +00002247
2248 { NULL, 0, "Incompatible with this kernel" },
2249 { NULL, ENOPROTOOPT, "iptables who? (do you need to insmod?)" },
2250 { NULL, ENOSYS, "Will be implemented real soon. I promise ;)" },
2251 { NULL, ENOMEM, "Memory allocation problem" },
2252 { NULL, ENOENT, "No chain/target/match by that name" },
Marc Bouchere6869a82000-03-20 06:03:29 +00002253 };
2254
2255 for (i = 0; i < sizeof(table)/sizeof(struct table_struct); i++) {
2256 if ((!table[i].fn || table[i].fn == iptc_fn)
2257 && table[i].err == err)
2258 return table[i].message;
2259 }
2260
2261 return strerror(err);
2262}