blob: b68e48c32f2f2063ad88a0fef6dc2d221e9c4265 [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>
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +020029#include <xtables.h>
Stephane Ouellette7cd00282004-05-14 08:21:06 +000030
Harald Welteaae69be2004-08-29 23:32:14 +000031#include "linux_list.h"
32
33//#define IPTC_DEBUG2 1
34
35#ifdef IPTC_DEBUG2
36#include <fcntl.h>
37#define DEBUGP(x, args...) fprintf(stderr, "%s: " x, __FUNCTION__, ## args)
38#define DEBUGP_C(x, args...) fprintf(stderr, x, ## args)
39#else
40#define DEBUGP(x, args...)
41#define DEBUGP_C(x, args...)
42#endif
43
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +000044#ifdef DEBUG
45#define debug(x, args...) fprintf(stderr, x, ## args)
46#else
47#define debug(x, args...)
48#endif
49
Marc Bouchere6869a82000-03-20 06:03:29 +000050static int sockfd = -1;
Derrik Pates664c0a32005-02-01 13:28:14 +000051static int sockfd_use = 0;
Marc Bouchere6869a82000-03-20 06:03:29 +000052static void *iptc_fn = NULL;
53
Patrick McHardy0b639362007-09-08 16:00:01 +000054static const char *hooknames[] = {
55 [HOOK_PRE_ROUTING] = "PREROUTING",
56 [HOOK_LOCAL_IN] = "INPUT",
57 [HOOK_FORWARD] = "FORWARD",
58 [HOOK_LOCAL_OUT] = "OUTPUT",
59 [HOOK_POST_ROUTING] = "POSTROUTING",
Rusty Russell10758b72000-09-14 07:37:33 +000060#ifdef HOOK_DROPPING
Patrick McHardy0b639362007-09-08 16:00:01 +000061 [HOOK_DROPPING] = "DROPPING"
Rusty Russell10758b72000-09-14 07:37:33 +000062#endif
Marc Bouchere6869a82000-03-20 06:03:29 +000063};
64
Harald Welteaae69be2004-08-29 23:32:14 +000065/* Convenience structures */
66struct ipt_error_target
67{
68 STRUCT_ENTRY_TARGET t;
69 char error[TABLE_MAXNAMELEN];
70};
71
72struct chain_head;
73struct rule_head;
74
Marc Bouchere6869a82000-03-20 06:03:29 +000075struct counter_map
76{
77 enum {
78 COUNTER_MAP_NOMAP,
79 COUNTER_MAP_NORMAL_MAP,
Harald Welte1cef74d2001-01-05 15:22:59 +000080 COUNTER_MAP_ZEROED,
81 COUNTER_MAP_SET
Marc Bouchere6869a82000-03-20 06:03:29 +000082 } maptype;
83 unsigned int mappos;
84};
85
Harald Welteaae69be2004-08-29 23:32:14 +000086enum iptcc_rule_type {
87 IPTCC_R_STANDARD, /* standard target (ACCEPT, ...) */
88 IPTCC_R_MODULE, /* extension module (SNAT, ...) */
89 IPTCC_R_FALLTHROUGH, /* fallthrough rule */
90 IPTCC_R_JUMP, /* jump to other chain */
Marc Bouchere6869a82000-03-20 06:03:29 +000091};
92
Harald Welteaae69be2004-08-29 23:32:14 +000093struct rule_head
Rusty Russell30fd6e52000-04-23 09:16:06 +000094{
Harald Welteaae69be2004-08-29 23:32:14 +000095 struct list_head list;
96 struct chain_head *chain;
97 struct counter_map counter_map;
98
99 unsigned int index; /* index (needed for counter_map) */
100 unsigned int offset; /* offset in rule blob */
101
102 enum iptcc_rule_type type;
103 struct chain_head *jump; /* jump target, if IPTCC_R_JUMP */
104
105 unsigned int size; /* size of entry data */
106 STRUCT_ENTRY entry[0];
107};
108
109struct chain_head
110{
111 struct list_head list;
Rusty Russell79dee072000-05-02 16:45:16 +0000112 char name[TABLE_MAXNAMELEN];
Harald Welteaae69be2004-08-29 23:32:14 +0000113 unsigned int hooknum; /* hook number+1 if builtin */
114 unsigned int references; /* how many jumps reference us */
115 int verdict; /* verdict if builtin */
116
117 STRUCT_COUNTERS counters; /* per-chain counters */
118 struct counter_map counter_map;
119
120 unsigned int num_rules; /* number of rules in list */
121 struct list_head rules; /* list of rules */
122
123 unsigned int index; /* index (needed for jump resolval) */
124 unsigned int head_offset; /* offset in rule blob */
125 unsigned int foot_index; /* index (needed for counter_map) */
126 unsigned int foot_offset; /* offset in rule blob */
Rusty Russell30fd6e52000-04-23 09:16:06 +0000127};
128
Rusty Russell79dee072000-05-02 16:45:16 +0000129STRUCT_TC_HANDLE
Marc Bouchere6869a82000-03-20 06:03:29 +0000130{
Harald Welteaae69be2004-08-29 23:32:14 +0000131 int changed; /* Have changes been made? */
132
133 struct list_head chains;
134
135 struct chain_head *chain_iterator_cur;
136 struct rule_head *rule_iterator_cur;
137
Jesper Dangaard Brouer48bde402008-01-15 17:06:48 +0000138 unsigned int num_chains; /* number of user defined chains */
139
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000140 struct chain_head **chain_index; /* array for fast chain list access*/
141 unsigned int chain_index_sz;/* size of chain index array */
142
Jesper Dangaard Brouer4bae3f12008-07-03 20:31:42 +0200143 int sorted_offsets; /* if chains are received sorted from kernel,
144 * then the offsets are also sorted. Says if its
145 * possible to bsearch offsets using chain_index.
146 */
147
Rusty Russell79dee072000-05-02 16:45:16 +0000148 STRUCT_GETINFO info;
Harald Welteaae69be2004-08-29 23:32:14 +0000149 STRUCT_GET_ENTRIES *entries;
Marc Bouchere6869a82000-03-20 06:03:29 +0000150};
151
Jesper Dangaard Brouer4bae3f12008-07-03 20:31:42 +0200152enum bsearch_type {
153 BSEARCH_NAME, /* Binary search after chain name */
154 BSEARCH_OFFSET, /* Binary search based on offset */
155};
156
Harald Welteaae69be2004-08-29 23:32:14 +0000157/* allocate a new chain head for the cache */
158static struct chain_head *iptcc_alloc_chain_head(const char *name, int hooknum)
159{
160 struct chain_head *c = malloc(sizeof(*c));
161 if (!c)
162 return NULL;
163 memset(c, 0, sizeof(*c));
164
165 strncpy(c->name, name, TABLE_MAXNAMELEN);
166 c->hooknum = hooknum;
167 INIT_LIST_HEAD(&c->rules);
168
169 return c;
170}
171
172/* allocate and initialize a new rule for the cache */
173static struct rule_head *iptcc_alloc_rule(struct chain_head *c, unsigned int size)
174{
175 struct rule_head *r = malloc(sizeof(*r)+size);
176 if (!r)
177 return NULL;
178 memset(r, 0, sizeof(*r));
179
180 r->chain = c;
181 r->size = size;
182
183 return r;
184}
185
186/* notify us that the ruleset has been modified by the user */
Jesper Dangaard Brouer91093982008-01-15 17:01:58 +0000187static inline void
Rusty Russell79dee072000-05-02 16:45:16 +0000188set_changed(TC_HANDLE_T h)
Rusty Russell175f6412000-03-24 09:32:20 +0000189{
Rusty Russell175f6412000-03-24 09:32:20 +0000190 h->changed = 1;
191}
192
Harald Welte380ba5f2002-02-13 16:19:55 +0000193#ifdef IPTC_DEBUG
Rusty Russell79dee072000-05-02 16:45:16 +0000194static void do_check(TC_HANDLE_T h, unsigned int line);
Rusty Russell849779c2000-04-23 15:51:51 +0000195#define CHECK(h) do { if (!getenv("IPTC_NO_CHECK")) do_check((h), __LINE__); } while(0)
Rusty Russell30fd6e52000-04-23 09:16:06 +0000196#else
197#define CHECK(h)
198#endif
Marc Bouchere6869a82000-03-20 06:03:29 +0000199
Harald Welteaae69be2004-08-29 23:32:14 +0000200
201/**********************************************************************
202 * iptc blob utility functions (iptcb_*)
203 **********************************************************************/
204
Marc Bouchere6869a82000-03-20 06:03:29 +0000205static inline int
Harald Welteaae69be2004-08-29 23:32:14 +0000206iptcb_get_number(const STRUCT_ENTRY *i,
Rusty Russell79dee072000-05-02 16:45:16 +0000207 const STRUCT_ENTRY *seek,
Marc Bouchere6869a82000-03-20 06:03:29 +0000208 unsigned int *pos)
209{
210 if (i == seek)
211 return 1;
212 (*pos)++;
213 return 0;
214}
215
Marc Bouchere6869a82000-03-20 06:03:29 +0000216static inline int
Harald Welteaae69be2004-08-29 23:32:14 +0000217iptcb_get_entry_n(STRUCT_ENTRY *i,
Marc Bouchere6869a82000-03-20 06:03:29 +0000218 unsigned int number,
219 unsigned int *pos,
Rusty Russell79dee072000-05-02 16:45:16 +0000220 STRUCT_ENTRY **pe)
Marc Bouchere6869a82000-03-20 06:03:29 +0000221{
222 if (*pos == number) {
223 *pe = i;
224 return 1;
225 }
226 (*pos)++;
227 return 0;
228}
229
Harald Welteaae69be2004-08-29 23:32:14 +0000230static inline STRUCT_ENTRY *
231iptcb_get_entry(TC_HANDLE_T h, unsigned int offset)
232{
233 return (STRUCT_ENTRY *)((char *)h->entries->entrytable + offset);
234}
235
236static unsigned int
237iptcb_entry2index(const TC_HANDLE_T h, const STRUCT_ENTRY *seek)
Marc Bouchere6869a82000-03-20 06:03:29 +0000238{
239 unsigned int pos = 0;
Marc Bouchere6869a82000-03-20 06:03:29 +0000240
Harald Welteaae69be2004-08-29 23:32:14 +0000241 if (ENTRY_ITERATE(h->entries->entrytable, h->entries->size,
242 iptcb_get_number, seek, &pos) == 0) {
243 fprintf(stderr, "ERROR: offset %u not an entry!\n",
244 (unsigned int)((char *)seek - (char *)h->entries->entrytable));
245 abort();
246 }
247 return pos;
Marc Bouchere6869a82000-03-20 06:03:29 +0000248}
249
Harald Welte0113fe72004-01-06 19:04:02 +0000250static inline STRUCT_ENTRY *
Harald Welteaae69be2004-08-29 23:32:14 +0000251iptcb_offset2entry(TC_HANDLE_T h, unsigned int offset)
Harald Welte0113fe72004-01-06 19:04:02 +0000252{
Harald Welteaae69be2004-08-29 23:32:14 +0000253 return (STRUCT_ENTRY *) ((void *)h->entries->entrytable+offset);
Harald Welte0113fe72004-01-06 19:04:02 +0000254}
255
Harald Welteaae69be2004-08-29 23:32:14 +0000256
Harald Welte0113fe72004-01-06 19:04:02 +0000257static inline unsigned long
Harald Welteaae69be2004-08-29 23:32:14 +0000258iptcb_entry2offset(const TC_HANDLE_T h, const STRUCT_ENTRY *e)
Harald Welte0113fe72004-01-06 19:04:02 +0000259{
Harald Welteaae69be2004-08-29 23:32:14 +0000260 return (void *)e - (void *)h->entries->entrytable;
Harald Welte3ea8f402003-06-23 18:25:59 +0000261}
262
263static inline unsigned int
Harald Welteaae69be2004-08-29 23:32:14 +0000264iptcb_offset2index(const TC_HANDLE_T h, unsigned int offset)
Harald Welte3ea8f402003-06-23 18:25:59 +0000265{
Harald Welteaae69be2004-08-29 23:32:14 +0000266 return iptcb_entry2index(h, iptcb_offset2entry(h, offset));
267}
268
269/* Returns 0 if not hook entry, else hooknumber + 1 */
270static inline unsigned int
271iptcb_ent_is_hook_entry(STRUCT_ENTRY *e, TC_HANDLE_T h)
272{
273 unsigned int i;
274
275 for (i = 0; i < NUMHOOKS; i++) {
276 if ((h->info.valid_hooks & (1 << i))
277 && iptcb_get_entry(h, h->info.hook_entry[i]) == e)
278 return i+1;
279 }
280 return 0;
Harald Welte3ea8f402003-06-23 18:25:59 +0000281}
282
283
Harald Welteaae69be2004-08-29 23:32:14 +0000284/**********************************************************************
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000285 * Chain index (cache utility) functions
286 **********************************************************************
287 * The chain index is an array with pointers into the chain list, with
288 * CHAIN_INDEX_BUCKET_LEN spacing. This facilitates the ability to
289 * speedup chain list searching, by find a more optimal starting
290 * points when searching the linked list.
291 *
292 * The starting point can be found fast by using a binary search of
293 * the chain index. Thus, reducing the previous search complexity of
294 * O(n) to O(log(n/k) + k) where k is CHAIN_INDEX_BUCKET_LEN.
295 *
296 * A nice property of the chain index, is that the "bucket" list
297 * length is max CHAIN_INDEX_BUCKET_LEN (when just build, inserts will
298 * change this). Oppose to hashing, where the "bucket" list length can
299 * vary a lot.
300 */
301#ifndef CHAIN_INDEX_BUCKET_LEN
302#define CHAIN_INDEX_BUCKET_LEN 40
303#endif
304
305/* Another nice property of the chain index is that inserting/creating
306 * chains in chain list don't change the correctness of the chain
307 * index, it only causes longer lists in the buckets.
308 *
309 * To mitigate the performance penalty of longer bucket lists and the
310 * penalty of rebuilding, the chain index is rebuild only when
311 * CHAIN_INDEX_INSERT_MAX chains has been added.
312 */
313#ifndef CHAIN_INDEX_INSERT_MAX
314#define CHAIN_INDEX_INSERT_MAX 355
315#endif
316
317static inline unsigned int iptcc_is_builtin(struct chain_head *c);
318
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000319/* Use binary search in the chain index array, to find a chain_head
320 * pointer closest to the place of the searched name element.
321 *
322 * Notes that, binary search (obviously) requires that the chain list
323 * is sorted by name.
Jesper Dangaard Brouer4bae3f12008-07-03 20:31:42 +0200324 *
325 * The not so obvious: The chain index array, is actually both sorted
326 * by name and offset, at the same time!. This is only true because,
327 * chain are stored sorted in the kernel (as we pushed it in sorted).
328 *
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000329 */
330static struct list_head *
Jesper Dangaard Brouer4bae3f12008-07-03 20:31:42 +0200331__iptcc_bsearch_chain_index(const char *name, unsigned int offset,
332 unsigned int *idx, TC_HANDLE_T handle,
333 enum bsearch_type type)
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000334{
335 unsigned int pos, end;
336 int res;
337
338 struct list_head *list_pos;
339 list_pos=&handle->chains;
340
341 /* Check for empty array, e.g. no user defined chains */
342 if (handle->chain_index_sz == 0) {
343 debug("WARNING: handle->chain_index_sz == 0\n");
344 return list_pos;
345 }
346
347 /* Init */
348 end = handle->chain_index_sz;
349 pos = end / 2;
350
Jesper Dangaard Brouer4bae3f12008-07-03 20:31:42 +0200351 debug("bsearch Find chain:%s (pos:%d end:%d) (offset:%d)\n",
352 name, pos, end, offset);
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000353
354 /* Loop */
355 loop:
356 if (!handle->chain_index[pos]) {
357 fprintf(stderr, "ERROR: NULL pointer chain_index[%d]\n", pos);
358 return &handle->chains; /* Be safe, return orig start pos */
359 }
360
Jesper Dangaard Brouer4bae3f12008-07-03 20:31:42 +0200361 debug("bsearch Index[%d] name:%s ",
362 pos, handle->chain_index[pos]->name);
363
364 /* Support for different compare functions */
365 switch (type) {
366 case BSEARCH_NAME:
367 res = strcmp(name, handle->chain_index[pos]->name);
368 break;
369 case BSEARCH_OFFSET:
370 debug("head_offset:[%d] foot_offset:[%d] ",
371 handle->chain_index[pos]->head_offset,
372 handle->chain_index[pos]->foot_offset);
373 res = offset - handle->chain_index[pos]->head_offset;
374 break;
375 default:
376 fprintf(stderr, "ERROR: %d not a valid bsearch type\n",
377 type);
378 abort();
379 break;
380 }
381 debug("res:%d ", res);
382
383
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000384 list_pos = &handle->chain_index[pos]->list;
Jan Engelhardtdbb77542008-02-11 00:33:30 +0100385 *idx = pos;
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000386
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000387 if (res == 0) { /* Found element, by direct hit */
388 debug("[found] Direct hit pos:%d end:%d\n", pos, end);
389 return list_pos;
390 } else if (res < 0) { /* Too far, jump back */
391 end = pos;
392 pos = pos / 2;
393
394 /* Exit case: First element of array */
395 if (end == 0) {
396 debug("[found] Reached first array elem (end%d)\n",end);
397 return list_pos;
398 }
399 debug("jump back to pos:%d (end:%d)\n", pos, end);
400 goto loop;
401 } else if (res > 0 ){ /* Not far enough, jump forward */
402
403 /* Exit case: Last element of array */
404 if (pos == handle->chain_index_sz-1) {
405 debug("[found] Last array elem (end:%d)\n", end);
406 return list_pos;
407 }
408
409 /* Exit case: Next index less, thus elem in this list section */
Jesper Dangaard Brouer4bae3f12008-07-03 20:31:42 +0200410 switch (type) {
411 case BSEARCH_NAME:
412 res = strcmp(name, handle->chain_index[pos+1]->name);
413 break;
414 case BSEARCH_OFFSET:
415 res = offset - handle->chain_index[pos+1]->head_offset;
416 break;
417 }
418
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000419 if (res < 0) {
420 debug("[found] closest list (end:%d)\n", end);
421 return list_pos;
422 }
423
424 pos = (pos+end)/2;
425 debug("jump forward to pos:%d (end:%d)\n", pos, end);
426 goto loop;
427 }
428
429 return list_pos;
430}
431
Jesper Dangaard Brouer4bae3f12008-07-03 20:31:42 +0200432/* Wrapper for string chain name based bsearch */
433static struct list_head *
434iptcc_bsearch_chain_index(const char *name, unsigned int *idx,
435 TC_HANDLE_T handle)
436{
437 return __iptcc_bsearch_chain_index(name, 0, idx, handle, BSEARCH_NAME);
438}
439
440
441/* Wrapper for offset chain based bsearch */
442static struct list_head *
443iptcc_bsearch_chain_offset(unsigned int offset, unsigned int *idx,
444 TC_HANDLE_T handle)
445{
446 struct list_head *pos;
447
448 /* If chains were not received sorted from kernel, then the
449 * offset bsearch is not possible.
450 */
451 if (!handle->sorted_offsets)
452 pos = handle->chains.next;
453 else
454 pos = __iptcc_bsearch_chain_index(NULL, offset, idx, handle,
455 BSEARCH_OFFSET);
456 return pos;
457}
458
459
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000460#ifdef DEBUG
461/* Trivial linear search of chain index. Function used for verifying
462 the output of bsearch function */
463static struct list_head *
464iptcc_linearly_search_chain_index(const char *name, TC_HANDLE_T handle)
465{
466 unsigned int i=0;
467 int res=0;
468
469 struct list_head *list_pos;
470 list_pos = &handle->chains;
471
472 if (handle->chain_index_sz)
473 list_pos = &handle->chain_index[0]->list;
474
475 /* Linearly walk of chain index array */
476
477 for (i=0; i < handle->chain_index_sz; i++) {
478 if (handle->chain_index[i]) {
479 res = strcmp(handle->chain_index[i]->name, name);
480 if (res > 0)
481 break; // One step too far
482 list_pos = &handle->chain_index[i]->list;
483 if (res == 0)
484 break; // Direct hit
485 }
486 }
487
488 return list_pos;
489}
490#endif
491
492static int iptcc_chain_index_alloc(TC_HANDLE_T h)
493{
494 unsigned int list_length = CHAIN_INDEX_BUCKET_LEN;
495 unsigned int array_elems;
496 unsigned int array_mem;
497
498 /* Allocate memory for the chain index array */
499 array_elems = (h->num_chains / list_length) +
500 (h->num_chains % list_length ? 1 : 0);
501 array_mem = sizeof(h->chain_index) * array_elems;
502
503 debug("Alloc Chain index, elems:%d mem:%d bytes\n",
504 array_elems, array_mem);
505
506 h->chain_index = malloc(array_mem);
507 if (!h->chain_index) {
508 h->chain_index_sz = 0;
509 return -ENOMEM;
510 }
511 memset(h->chain_index, 0, array_mem);
512 h->chain_index_sz = array_elems;
513
514 return 1;
515}
516
517static void iptcc_chain_index_free(TC_HANDLE_T h)
518{
519 h->chain_index_sz = 0;
520 free(h->chain_index);
521}
522
523
524#ifdef DEBUG
525static void iptcc_chain_index_dump(TC_HANDLE_T h)
526{
527 unsigned int i = 0;
528
529 /* Dump: contents of chain index array */
530 for (i=0; i < h->chain_index_sz; i++) {
531 if (h->chain_index[i]) {
532 fprintf(stderr, "Chain index[%d].name: %s\n",
533 i, h->chain_index[i]->name);
534 }
535 }
536}
537#endif
538
539/* Build the chain index */
540static int iptcc_chain_index_build(TC_HANDLE_T h)
541{
542 unsigned int list_length = CHAIN_INDEX_BUCKET_LEN;
543 unsigned int chains = 0;
544 unsigned int cindex = 0;
545 struct chain_head *c;
546
547 /* Build up the chain index array here */
548 debug("Building chain index\n");
549
550 debug("Number of user defined chains:%d bucket_sz:%d array_sz:%d\n",
551 h->num_chains, list_length, h->chain_index_sz);
552
553 if (h->chain_index_sz == 0)
554 return 0;
555
556 list_for_each_entry(c, &h->chains, list) {
557
558 /* Issue: The index array needs to start after the
559 * builtin chains, as they are not sorted */
560 if (!iptcc_is_builtin(c)) {
561 cindex=chains / list_length;
562
563 /* Safe guard, break out on array limit, this
564 * is useful if chains are added and array is
565 * rebuild, without realloc of memory. */
566 if (cindex >= h->chain_index_sz)
567 break;
568
569 if ((chains % list_length)== 0) {
570 debug("\nIndex[%d] Chains:", cindex);
571 h->chain_index[cindex] = c;
572 }
573 chains++;
574 }
575 debug("%s, ", c->name);
576 }
577 debug("\n");
578
579 return 1;
580}
581
582static int iptcc_chain_index_rebuild(TC_HANDLE_T h)
583{
584 debug("REBUILD chain index array\n");
585 iptcc_chain_index_free(h);
586 if ((iptcc_chain_index_alloc(h)) < 0)
587 return -ENOMEM;
588 iptcc_chain_index_build(h);
589 return 1;
590}
591
592/* Delete chain (pointer) from index array. Removing an element from
593 * the chain list only affects the chain index array, if the chain
594 * index points-to/uses that list pointer.
595 *
596 * There are different strategies, the simple and safe is to rebuild
597 * the chain index every time. The more advanced is to update the
598 * array index to point to the next element, but that requires some
599 * house keeping and boundry checks. The advanced is implemented, as
600 * the simple approach behaves badly when all chains are deleted
601 * because list_for_each processing will always hit the first chain
602 * index, thus causing a rebuild for every chain.
603 */
604static int iptcc_chain_index_delete_chain(struct chain_head *c, TC_HANDLE_T h)
605{
606 struct list_head *index_ptr, *index_ptr2, *next;
607 struct chain_head *c2;
Jan Engelhardtdbb77542008-02-11 00:33:30 +0100608 unsigned int idx, idx2;
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000609
Jan Engelhardtdbb77542008-02-11 00:33:30 +0100610 index_ptr = iptcc_bsearch_chain_index(c->name, &idx, h);
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000611
612 debug("Del chain[%s] c->list:%p index_ptr:%p\n",
613 c->name, &c->list, index_ptr);
614
615 /* Save the next pointer */
616 next = c->list.next;
617 list_del(&c->list);
618
619 if (index_ptr == &c->list) { /* Chain used as index ptr */
620
621 /* See if its possible to avoid a rebuild, by shifting
622 * to next pointer. Its possible if the next pointer
623 * is located in the same index bucket.
624 */
625 c2 = list_entry(next, struct chain_head, list);
Jan Engelhardtdbb77542008-02-11 00:33:30 +0100626 index_ptr2 = iptcc_bsearch_chain_index(c2->name, &idx2, h);
627 if (idx != idx2) {
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000628 /* Rebuild needed */
629 return iptcc_chain_index_rebuild(h);
630 } else {
631 /* Avoiding rebuild */
632 debug("Update cindex[%d] with next ptr name:[%s]\n",
Jan Engelhardtdbb77542008-02-11 00:33:30 +0100633 idx, c2->name);
634 h->chain_index[idx]=c2;
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000635 return 0;
636 }
637 }
638 return 0;
639}
640
641
642/**********************************************************************
Harald Welteaae69be2004-08-29 23:32:14 +0000643 * iptc cache utility functions (iptcc_*)
644 **********************************************************************/
Harald Welte0113fe72004-01-06 19:04:02 +0000645
Harald Welteaae69be2004-08-29 23:32:14 +0000646/* Is the given chain builtin (1) or user-defined (0) */
Jesper Dangaard Brouer91093982008-01-15 17:01:58 +0000647static inline unsigned int iptcc_is_builtin(struct chain_head *c)
Harald Welteaae69be2004-08-29 23:32:14 +0000648{
649 return (c->hooknum ? 1 : 0);
650}
651
652/* Get a specific rule within a chain */
653static struct rule_head *iptcc_get_rule_num(struct chain_head *c,
654 unsigned int rulenum)
655{
656 struct rule_head *r;
657 unsigned int num = 0;
658
659 list_for_each_entry(r, &c->rules, list) {
660 num++;
661 if (num == rulenum)
662 return r;
663 }
664 return NULL;
665}
666
Martin Josefssona5616dc2004-10-24 22:27:31 +0000667/* Get a specific rule within a chain backwards */
668static struct rule_head *iptcc_get_rule_num_reverse(struct chain_head *c,
669 unsigned int rulenum)
670{
671 struct rule_head *r;
672 unsigned int num = 0;
673
674 list_for_each_entry_reverse(r, &c->rules, list) {
675 num++;
676 if (num == rulenum)
677 return r;
678 }
679 return NULL;
680}
681
Harald Welteaae69be2004-08-29 23:32:14 +0000682/* Returns chain head if found, otherwise NULL. */
683static struct chain_head *
684iptcc_find_chain_by_offset(TC_HANDLE_T handle, unsigned int offset)
685{
686 struct list_head *pos;
Jesper Dangaard Brouer4bae3f12008-07-03 20:31:42 +0200687 struct list_head *list_start_pos;
688 unsigned int i;
Harald Welteaae69be2004-08-29 23:32:14 +0000689
690 if (list_empty(&handle->chains))
691 return NULL;
692
Jesper Dangaard Brouer4bae3f12008-07-03 20:31:42 +0200693 /* Find a smart place to start the search */
694 list_start_pos = iptcc_bsearch_chain_offset(offset, &i, handle);
695
696 /* Note that iptcc_bsearch_chain_offset() skips builtin
697 * chains, but this function is only used for finding jump
698 * targets, and a buildin chain is not a valid jump target */
699
700 debug("Offset:[%u] starting search at index:[%u]\n", offset, i);
701// list_for_each(pos, &handle->chains) {
702 list_for_each(pos, list_start_pos->prev) {
Harald Welteaae69be2004-08-29 23:32:14 +0000703 struct chain_head *c = list_entry(pos, struct chain_head, list);
Jesper Dangaard Brouer4bae3f12008-07-03 20:31:42 +0200704 debug(".");
705 if (offset >= c->head_offset && offset <= c->foot_offset) {
706 debug("Offset search found chain:[%s]\n", c->name);
Harald Welteaae69be2004-08-29 23:32:14 +0000707 return c;
Jesper Dangaard Brouer4bae3f12008-07-03 20:31:42 +0200708 }
Harald Welte0113fe72004-01-06 19:04:02 +0000709 }
710
Harald Welteaae69be2004-08-29 23:32:14 +0000711 return NULL;
Harald Welte0113fe72004-01-06 19:04:02 +0000712}
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000713
Harald Welteaae69be2004-08-29 23:32:14 +0000714/* Returns chain head if found, otherwise NULL. */
715static struct chain_head *
716iptcc_find_label(const char *name, TC_HANDLE_T handle)
717{
718 struct list_head *pos;
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000719 struct list_head *list_start_pos;
720 unsigned int i=0;
721 int res;
Harald Welteaae69be2004-08-29 23:32:14 +0000722
723 if (list_empty(&handle->chains))
724 return NULL;
725
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000726 /* First look at builtin chains */
Harald Welteaae69be2004-08-29 23:32:14 +0000727 list_for_each(pos, &handle->chains) {
728 struct chain_head *c = list_entry(pos, struct chain_head, list);
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000729 if (!iptcc_is_builtin(c))
730 break;
Harald Welteaae69be2004-08-29 23:32:14 +0000731 if (!strcmp(c->name, name))
732 return c;
733 }
734
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000735 /* Find a smart place to start the search via chain index */
736 //list_start_pos = iptcc_linearly_search_chain_index(name, handle);
737 list_start_pos = iptcc_bsearch_chain_index(name, &i, handle);
738
739 /* Handel if bsearch bails out early */
740 if (list_start_pos == &handle->chains) {
741 list_start_pos = pos;
742 }
743#ifdef DEBUG
744 else {
745 /* Verify result of bsearch against linearly index search */
746 struct list_head *test_pos;
747 struct chain_head *test_c, *tmp_c;
748 test_pos = iptcc_linearly_search_chain_index(name, handle);
749 if (list_start_pos != test_pos) {
750 debug("BUG in chain_index search\n");
751 test_c=list_entry(test_pos, struct chain_head,list);
752 tmp_c =list_entry(list_start_pos,struct chain_head,list);
753 debug("Verify search found:\n");
754 debug(" Chain:%s\n", test_c->name);
755 debug("BSearch found:\n");
756 debug(" Chain:%s\n", tmp_c->name);
757 exit(42);
758 }
759 }
760#endif
761
762 /* Initial/special case, no user defined chains */
763 if (handle->num_chains == 0)
764 return NULL;
765
766 /* Start searching through the chain list */
767 list_for_each(pos, list_start_pos->prev) {
768 struct chain_head *c = list_entry(pos, struct chain_head, list);
769 res = strcmp(c->name, name);
770 debug("List search name:%s == %s res:%d\n", name, c->name, res);
771 if (res==0)
772 return c;
773
774 /* We can stop earlier as we know list is sorted */
775 if (res>0 && !iptcc_is_builtin(c)) { /* Walked too far*/
776 debug(" Not in list, walked too far, sorted list\n");
777 return NULL;
778 }
779
780 /* Stop on wrap around, if list head is reached */
781 if (pos == &handle->chains) {
782 debug("Stop, list head reached\n");
783 return NULL;
784 }
785 }
786
787 debug("List search NOT found name:%s\n", name);
Harald Welteaae69be2004-08-29 23:32:14 +0000788 return NULL;
789}
790
791/* called when rule is to be removed from cache */
792static void iptcc_delete_rule(struct rule_head *r)
793{
794 DEBUGP("deleting rule %p (offset %u)\n", r, r->offset);
795 /* clean up reference count of called chain */
796 if (r->type == IPTCC_R_JUMP
797 && r->jump)
798 r->jump->references--;
799
800 list_del(&r->list);
801 free(r);
802}
803
804
805/**********************************************************************
806 * RULESET PARSER (blob -> cache)
807 **********************************************************************/
808
Harald Welteaae69be2004-08-29 23:32:14 +0000809/* Delete policy rule of previous chain, since cache doesn't contain
810 * chain policy rules.
811 * WARNING: This function has ugly design and relies on a lot of context, only
812 * to be called from specific places within the parser */
813static int __iptcc_p_del_policy(TC_HANDLE_T h, unsigned int num)
814{
815 if (h->chain_iterator_cur) {
816 /* policy rule is last rule */
817 struct rule_head *pr = (struct rule_head *)
818 h->chain_iterator_cur->rules.prev;
819
820 /* save verdict */
821 h->chain_iterator_cur->verdict =
822 *(int *)GET_TARGET(pr->entry)->data;
823
824 /* save counter and counter_map information */
825 h->chain_iterator_cur->counter_map.maptype =
826 COUNTER_MAP_NORMAL_MAP;
827 h->chain_iterator_cur->counter_map.mappos = num-1;
828 memcpy(&h->chain_iterator_cur->counters, &pr->entry->counters,
829 sizeof(h->chain_iterator_cur->counters));
830
831 /* foot_offset points to verdict rule */
832 h->chain_iterator_cur->foot_index = num;
833 h->chain_iterator_cur->foot_offset = pr->offset;
834
835 /* delete rule from cache */
836 iptcc_delete_rule(pr);
Martin Josefsson8d1b38a2004-09-22 21:00:19 +0000837 h->chain_iterator_cur->num_rules--;
Harald Welteaae69be2004-08-29 23:32:14 +0000838
839 return 1;
840 }
841 return 0;
842}
843
Harald Welteec30b6c2005-02-01 16:45:56 +0000844/* alphabetically insert a chain into the list */
845static inline void iptc_insert_chain(TC_HANDLE_T h, struct chain_head *c)
846{
847 struct chain_head *tmp;
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000848 struct list_head *list_start_pos;
849 unsigned int i=1;
850
851 /* Find a smart place to start the insert search */
852 list_start_pos = iptcc_bsearch_chain_index(c->name, &i, h);
853
854 /* Handle the case, where chain.name is smaller than index[0] */
855 if (i==0 && strcmp(c->name, h->chain_index[0]->name) <= 0) {
856 h->chain_index[0] = c; /* Update chain index head */
857 list_start_pos = h->chains.next;
858 debug("Update chain_index[0] with %s\n", c->name);
859 }
860
861 /* Handel if bsearch bails out early */
862 if (list_start_pos == &h->chains) {
863 list_start_pos = h->chains.next;
864 }
Harald Welteec30b6c2005-02-01 16:45:56 +0000865
Olaf Rempel9d3ed772005-03-04 23:08:30 +0000866 /* sort only user defined chains */
867 if (!c->hooknum) {
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000868 list_for_each_entry(tmp, list_start_pos->prev, list) {
Robert de Barthfeca0572005-07-31 07:04:59 +0000869 if (!tmp->hooknum && strcmp(c->name, tmp->name) <= 0) {
Olaf Rempel9d3ed772005-03-04 23:08:30 +0000870 list_add(&c->list, tmp->list.prev);
871 return;
872 }
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000873
874 /* Stop if list head is reached */
875 if (&tmp->list == &h->chains) {
876 debug("Insert, list head reached add to tail\n");
877 break;
878 }
Harald Welteec30b6c2005-02-01 16:45:56 +0000879 }
880 }
881
882 /* survived till end of list: add at tail */
883 list_add_tail(&c->list, &h->chains);
884}
885
Harald Welteaae69be2004-08-29 23:32:14 +0000886/* Another ugly helper function split out of cache_add_entry to make it less
887 * spaghetti code */
888static void __iptcc_p_add_chain(TC_HANDLE_T h, struct chain_head *c,
889 unsigned int offset, unsigned int *num)
890{
Jesper Dangaard Brouer13364512007-12-12 15:20:42 +0000891 struct list_head *tail = h->chains.prev;
892 struct chain_head *ctail;
893
Harald Welteaae69be2004-08-29 23:32:14 +0000894 __iptcc_p_del_policy(h, *num);
895
896 c->head_offset = offset;
897 c->index = *num;
898
Jesper Dangaard Brouer13364512007-12-12 15:20:42 +0000899 /* Chains from kernel are already sorted, as they are inserted
900 * sorted. But there exists an issue when shifting to 1.4.0
901 * from an older version, as old versions allow last created
902 * chain to be unsorted.
903 */
904 if (iptcc_is_builtin(c)) /* Only user defined chains are sorted*/
905 list_add_tail(&c->list, &h->chains);
906 else {
907 ctail = list_entry(tail, struct chain_head, list);
Jesper Dangaard Brouer4bae3f12008-07-03 20:31:42 +0200908
Jesper Dangaard Brouer526d3e12008-07-03 20:29:34 +0200909 if (strcmp(c->name, ctail->name) > 0 ||
910 iptcc_is_builtin(ctail))
Jesper Dangaard Brouer13364512007-12-12 15:20:42 +0000911 list_add_tail(&c->list, &h->chains);/* Already sorted*/
Jesper Dangaard Brouer4bae3f12008-07-03 20:31:42 +0200912 else {
Jesper Dangaard Brouer13364512007-12-12 15:20:42 +0000913 iptc_insert_chain(h, c);/* Was not sorted */
Jesper Dangaard Brouer4bae3f12008-07-03 20:31:42 +0200914
915 /* Notice, if chains were not received sorted
916 * from kernel, then an offset bsearch is no
917 * longer valid.
918 */
919 h->sorted_offsets = 0;
920
921 debug("NOTICE: chain:[%s] was NOT sorted(ctail:%s)\n",
922 c->name, ctail->name);
923 }
Jesper Dangaard Brouer13364512007-12-12 15:20:42 +0000924 }
Jesper Dangaard Brouerd8cb7872007-11-28 08:40:26 +0000925
Harald Welteaae69be2004-08-29 23:32:14 +0000926 h->chain_iterator_cur = c;
927}
928
929/* main parser function: add an entry from the blob to the cache */
930static int cache_add_entry(STRUCT_ENTRY *e,
931 TC_HANDLE_T h,
932 STRUCT_ENTRY **prev,
933 unsigned int *num)
934{
935 unsigned int builtin;
936 unsigned int offset = (char *)e - (char *)h->entries->entrytable;
937
938 DEBUGP("entering...");
939
940 /* Last entry ("policy rule"). End it.*/
941 if (iptcb_entry2offset(h,e) + e->next_offset == h->entries->size) {
942 /* This is the ERROR node at the end of the chain */
943 DEBUGP_C("%u:%u: end of table:\n", *num, offset);
944
945 __iptcc_p_del_policy(h, *num);
946
947 h->chain_iterator_cur = NULL;
948 goto out_inc;
949 }
950
951 /* We know this is the start of a new chain if it's an ERROR
952 * target, or a hook entry point */
953
954 if (strcmp(GET_TARGET(e)->u.user.name, ERROR_TARGET) == 0) {
955 struct chain_head *c =
956 iptcc_alloc_chain_head((const char *)GET_TARGET(e)->data, 0);
957 DEBUGP_C("%u:%u:new userdefined chain %s: %p\n", *num, offset,
958 (char *)c->name, c);
959 if (!c) {
960 errno = -ENOMEM;
961 return -1;
962 }
Jesper Dangaard Brouer48bde402008-01-15 17:06:48 +0000963 h->num_chains++; /* New user defined chain */
Harald Welteaae69be2004-08-29 23:32:14 +0000964
965 __iptcc_p_add_chain(h, c, offset, num);
966
967 } else if ((builtin = iptcb_ent_is_hook_entry(e, h)) != 0) {
968 struct chain_head *c =
969 iptcc_alloc_chain_head((char *)hooknames[builtin-1],
970 builtin);
971 DEBUGP_C("%u:%u new builtin chain: %p (rules=%p)\n",
972 *num, offset, c, &c->rules);
973 if (!c) {
974 errno = -ENOMEM;
975 return -1;
976 }
977
978 c->hooknum = builtin;
979
980 __iptcc_p_add_chain(h, c, offset, num);
981
982 /* FIXME: this is ugly. */
983 goto new_rule;
984 } else {
985 /* has to be normal rule */
986 struct rule_head *r;
987new_rule:
988
989 if (!(r = iptcc_alloc_rule(h->chain_iterator_cur,
990 e->next_offset))) {
991 errno = ENOMEM;
992 return -1;
993 }
994 DEBUGP_C("%u:%u normal rule: %p: ", *num, offset, r);
995
996 r->index = *num;
997 r->offset = offset;
998 memcpy(r->entry, e, e->next_offset);
999 r->counter_map.maptype = COUNTER_MAP_NORMAL_MAP;
1000 r->counter_map.mappos = r->index;
1001
1002 /* handling of jumps, etc. */
1003 if (!strcmp(GET_TARGET(e)->u.user.name, STANDARD_TARGET)) {
1004 STRUCT_STANDARD_TARGET *t;
1005
1006 t = (STRUCT_STANDARD_TARGET *)GET_TARGET(e);
1007 if (t->target.u.target_size
1008 != ALIGN(sizeof(STRUCT_STANDARD_TARGET))) {
1009 errno = EINVAL;
1010 return -1;
1011 }
1012
1013 if (t->verdict < 0) {
1014 DEBUGP_C("standard, verdict=%d\n", t->verdict);
1015 r->type = IPTCC_R_STANDARD;
1016 } else if (t->verdict == r->offset+e->next_offset) {
1017 DEBUGP_C("fallthrough\n");
1018 r->type = IPTCC_R_FALLTHROUGH;
1019 } else {
1020 DEBUGP_C("jump, target=%u\n", t->verdict);
1021 r->type = IPTCC_R_JUMP;
1022 /* Jump target fixup has to be deferred
1023 * until second pass, since we migh not
1024 * yet have parsed the target */
1025 }
Martin Josefsson52c38022004-09-22 19:39:40 +00001026 } else {
1027 DEBUGP_C("module, target=%s\n", GET_TARGET(e)->u.user.name);
1028 r->type = IPTCC_R_MODULE;
Harald Welteaae69be2004-08-29 23:32:14 +00001029 }
1030
1031 list_add_tail(&r->list, &h->chain_iterator_cur->rules);
Martin Josefsson8d1b38a2004-09-22 21:00:19 +00001032 h->chain_iterator_cur->num_rules++;
Harald Welteaae69be2004-08-29 23:32:14 +00001033 }
1034out_inc:
1035 (*num)++;
1036 return 0;
1037}
1038
1039
1040/* parse an iptables blob into it's pieces */
1041static int parse_table(TC_HANDLE_T h)
1042{
1043 STRUCT_ENTRY *prev;
1044 unsigned int num = 0;
1045 struct chain_head *c;
1046
Jesper Dangaard Brouer4bae3f12008-07-03 20:31:42 +02001047 /* Assume that chains offsets are sorted, this verified during
1048 parsing of ruleset (in __iptcc_p_add_chain())*/
1049 h->sorted_offsets = 1;
1050
Harald Welteaae69be2004-08-29 23:32:14 +00001051 /* First pass: over ruleset blob */
1052 ENTRY_ITERATE(h->entries->entrytable, h->entries->size,
1053 cache_add_entry, h, &prev, &num);
1054
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +00001055 /* Build the chain index, used for chain list search speedup */
1056 if ((iptcc_chain_index_alloc(h)) < 0)
1057 return -ENOMEM;
1058 iptcc_chain_index_build(h);
1059
Harald Welteaae69be2004-08-29 23:32:14 +00001060 /* Second pass: fixup parsed data from first pass */
1061 list_for_each_entry(c, &h->chains, list) {
1062 struct rule_head *r;
1063 list_for_each_entry(r, &c->rules, list) {
Jan Engelhardtdbb77542008-02-11 00:33:30 +01001064 struct chain_head *lc;
Harald Welteaae69be2004-08-29 23:32:14 +00001065 STRUCT_STANDARD_TARGET *t;
1066
1067 if (r->type != IPTCC_R_JUMP)
1068 continue;
1069
1070 t = (STRUCT_STANDARD_TARGET *)GET_TARGET(r->entry);
Jan Engelhardtdbb77542008-02-11 00:33:30 +01001071 lc = iptcc_find_chain_by_offset(h, t->verdict);
1072 if (!lc)
Harald Welteaae69be2004-08-29 23:32:14 +00001073 return -1;
Jan Engelhardtdbb77542008-02-11 00:33:30 +01001074 r->jump = lc;
1075 lc->references++;
Harald Welteaae69be2004-08-29 23:32:14 +00001076 }
1077 }
1078
1079 /* FIXME: sort chains */
1080
1081 return 1;
1082}
1083
1084
1085/**********************************************************************
1086 * RULESET COMPILATION (cache -> blob)
1087 **********************************************************************/
1088
1089/* Convenience structures */
1090struct iptcb_chain_start{
1091 STRUCT_ENTRY e;
1092 struct ipt_error_target name;
1093};
1094#define IPTCB_CHAIN_START_SIZE (sizeof(STRUCT_ENTRY) + \
1095 ALIGN(sizeof(struct ipt_error_target)))
1096
1097struct iptcb_chain_foot {
1098 STRUCT_ENTRY e;
1099 STRUCT_STANDARD_TARGET target;
1100};
1101#define IPTCB_CHAIN_FOOT_SIZE (sizeof(STRUCT_ENTRY) + \
1102 ALIGN(sizeof(STRUCT_STANDARD_TARGET)))
1103
1104struct iptcb_chain_error {
1105 STRUCT_ENTRY entry;
1106 struct ipt_error_target target;
1107};
1108#define IPTCB_CHAIN_ERROR_SIZE (sizeof(STRUCT_ENTRY) + \
1109 ALIGN(sizeof(struct ipt_error_target)))
1110
1111
1112
1113/* compile rule from cache into blob */
1114static inline int iptcc_compile_rule (TC_HANDLE_T h, STRUCT_REPLACE *repl, struct rule_head *r)
1115{
1116 /* handle jumps */
1117 if (r->type == IPTCC_R_JUMP) {
1118 STRUCT_STANDARD_TARGET *t;
1119 t = (STRUCT_STANDARD_TARGET *)GET_TARGET(r->entry);
1120 /* memset for memcmp convenience on delete/replace */
1121 memset(t->target.u.user.name, 0, FUNCTION_MAXNAMELEN);
1122 strcpy(t->target.u.user.name, STANDARD_TARGET);
1123 /* Jumps can only happen to builtin chains, so we
1124 * can safely assume that they always have a header */
1125 t->verdict = r->jump->head_offset + IPTCB_CHAIN_START_SIZE;
1126 } else if (r->type == IPTCC_R_FALLTHROUGH) {
1127 STRUCT_STANDARD_TARGET *t;
1128 t = (STRUCT_STANDARD_TARGET *)GET_TARGET(r->entry);
1129 t->verdict = r->offset + r->size;
1130 }
1131
1132 /* copy entry from cache to blob */
1133 memcpy((char *)repl->entries+r->offset, r->entry, r->size);
1134
1135 return 1;
1136}
1137
1138/* compile chain from cache into blob */
1139static int iptcc_compile_chain(TC_HANDLE_T h, STRUCT_REPLACE *repl, struct chain_head *c)
1140{
1141 int ret;
1142 struct rule_head *r;
1143 struct iptcb_chain_start *head;
1144 struct iptcb_chain_foot *foot;
1145
1146 /* only user-defined chains have heaer */
1147 if (!iptcc_is_builtin(c)) {
1148 /* put chain header in place */
1149 head = (void *)repl->entries + c->head_offset;
1150 head->e.target_offset = sizeof(STRUCT_ENTRY);
1151 head->e.next_offset = IPTCB_CHAIN_START_SIZE;
1152 strcpy(head->name.t.u.user.name, ERROR_TARGET);
1153 head->name.t.u.target_size =
1154 ALIGN(sizeof(struct ipt_error_target));
1155 strcpy(head->name.error, c->name);
1156 } else {
1157 repl->hook_entry[c->hooknum-1] = c->head_offset;
1158 repl->underflow[c->hooknum-1] = c->foot_offset;
1159 }
1160
1161 /* iterate over rules */
1162 list_for_each_entry(r, &c->rules, list) {
1163 ret = iptcc_compile_rule(h, repl, r);
1164 if (ret < 0)
1165 return ret;
1166 }
1167
1168 /* put chain footer in place */
1169 foot = (void *)repl->entries + c->foot_offset;
1170 foot->e.target_offset = sizeof(STRUCT_ENTRY);
1171 foot->e.next_offset = IPTCB_CHAIN_FOOT_SIZE;
1172 strcpy(foot->target.target.u.user.name, STANDARD_TARGET);
1173 foot->target.target.u.target_size =
1174 ALIGN(sizeof(STRUCT_STANDARD_TARGET));
1175 /* builtin targets have verdict, others return */
1176 if (iptcc_is_builtin(c))
1177 foot->target.verdict = c->verdict;
1178 else
1179 foot->target.verdict = RETURN;
1180 /* set policy-counters */
1181 memcpy(&foot->e.counters, &c->counters, sizeof(STRUCT_COUNTERS));
1182
1183 return 0;
1184}
1185
1186/* calculate offset and number for every rule in the cache */
1187static int iptcc_compile_chain_offsets(TC_HANDLE_T h, struct chain_head *c,
Harald Welteefa8fc22005-07-19 22:03:49 +00001188 unsigned int *offset, unsigned int *num)
Harald Welteaae69be2004-08-29 23:32:14 +00001189{
1190 struct rule_head *r;
1191
1192 c->head_offset = *offset;
1193 DEBUGP("%s: chain_head %u, offset=%u\n", c->name, *num, *offset);
1194
1195 if (!iptcc_is_builtin(c)) {
1196 /* Chain has header */
1197 *offset += sizeof(STRUCT_ENTRY)
1198 + ALIGN(sizeof(struct ipt_error_target));
1199 (*num)++;
1200 }
1201
1202 list_for_each_entry(r, &c->rules, list) {
1203 DEBUGP("rule %u, offset=%u, index=%u\n", *num, *offset, *num);
1204 r->offset = *offset;
1205 r->index = *num;
1206 *offset += r->size;
1207 (*num)++;
1208 }
1209
1210 DEBUGP("%s; chain_foot %u, offset=%u, index=%u\n", c->name, *num,
1211 *offset, *num);
1212 c->foot_offset = *offset;
1213 c->foot_index = *num;
1214 *offset += sizeof(STRUCT_ENTRY)
1215 + ALIGN(sizeof(STRUCT_STANDARD_TARGET));
1216 (*num)++;
1217
1218 return 1;
1219}
1220
1221/* put the pieces back together again */
1222static int iptcc_compile_table_prep(TC_HANDLE_T h, unsigned int *size)
1223{
1224 struct chain_head *c;
1225 unsigned int offset = 0, num = 0;
1226 int ret = 0;
1227
1228 /* First pass: calculate offset for every rule */
1229 list_for_each_entry(c, &h->chains, list) {
1230 ret = iptcc_compile_chain_offsets(h, c, &offset, &num);
1231 if (ret < 0)
1232 return ret;
1233 }
1234
1235 /* Append one error rule at end of chain */
1236 num++;
1237 offset += sizeof(STRUCT_ENTRY)
1238 + ALIGN(sizeof(struct ipt_error_target));
1239
1240 /* ruleset size is now in offset */
1241 *size = offset;
1242 return num;
1243}
1244
1245static int iptcc_compile_table(TC_HANDLE_T h, STRUCT_REPLACE *repl)
1246{
1247 struct chain_head *c;
1248 struct iptcb_chain_error *error;
1249
1250 /* Second pass: copy from cache to offsets, fill in jumps */
1251 list_for_each_entry(c, &h->chains, list) {
1252 int ret = iptcc_compile_chain(h, repl, c);
1253 if (ret < 0)
1254 return ret;
1255 }
1256
1257 /* Append error rule at end of chain */
1258 error = (void *)repl->entries + repl->size - IPTCB_CHAIN_ERROR_SIZE;
1259 error->entry.target_offset = sizeof(STRUCT_ENTRY);
1260 error->entry.next_offset = IPTCB_CHAIN_ERROR_SIZE;
1261 error->target.t.u.user.target_size =
1262 ALIGN(sizeof(struct ipt_error_target));
1263 strcpy((char *)&error->target.t.u.user.name, ERROR_TARGET);
1264 strcpy((char *)&error->target.error, "ERROR");
1265
1266 return 1;
1267}
1268
1269/**********************************************************************
1270 * EXTERNAL API (operates on cache only)
1271 **********************************************************************/
Marc Bouchere6869a82000-03-20 06:03:29 +00001272
1273/* Allocate handle of given size */
Rusty Russell79dee072000-05-02 16:45:16 +00001274static TC_HANDLE_T
Harald Welte0113fe72004-01-06 19:04:02 +00001275alloc_handle(const char *tablename, unsigned int size, unsigned int num_rules)
Marc Bouchere6869a82000-03-20 06:03:29 +00001276{
1277 size_t len;
Rusty Russell79dee072000-05-02 16:45:16 +00001278 TC_HANDLE_T h;
Marc Bouchere6869a82000-03-20 06:03:29 +00001279
Harald Welteaae69be2004-08-29 23:32:14 +00001280 len = sizeof(STRUCT_TC_HANDLE) + size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001281
Harald Welteaae69be2004-08-29 23:32:14 +00001282 h = malloc(sizeof(STRUCT_TC_HANDLE));
1283 if (!h) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001284 errno = ENOMEM;
1285 return NULL;
1286 }
Harald Welteaae69be2004-08-29 23:32:14 +00001287 memset(h, 0, sizeof(*h));
1288 INIT_LIST_HEAD(&h->chains);
Marc Bouchere6869a82000-03-20 06:03:29 +00001289 strcpy(h->info.name, tablename);
Harald Welteaae69be2004-08-29 23:32:14 +00001290
Harald Welte0371c0c2004-09-19 21:00:12 +00001291 h->entries = malloc(sizeof(STRUCT_GET_ENTRIES) + size);
Harald Welteaae69be2004-08-29 23:32:14 +00001292 if (!h->entries)
1293 goto out_free_handle;
1294
1295 strcpy(h->entries->name, tablename);
Harald Welte0371c0c2004-09-19 21:00:12 +00001296 h->entries->size = size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001297
1298 return h;
Harald Welteaae69be2004-08-29 23:32:14 +00001299
1300out_free_handle:
1301 free(h);
1302
1303 return NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +00001304}
1305
Harald Welteaae69be2004-08-29 23:32:14 +00001306
Rusty Russell79dee072000-05-02 16:45:16 +00001307TC_HANDLE_T
1308TC_INIT(const char *tablename)
Marc Bouchere6869a82000-03-20 06:03:29 +00001309{
Rusty Russell79dee072000-05-02 16:45:16 +00001310 TC_HANDLE_T h;
1311 STRUCT_GETINFO info;
Harald Welteefa8fc22005-07-19 22:03:49 +00001312 unsigned int tmp;
Marc Bouchere6869a82000-03-20 06:03:29 +00001313 socklen_t s;
1314
Rusty Russell79dee072000-05-02 16:45:16 +00001315 iptc_fn = TC_INIT;
Marc Bouchere6869a82000-03-20 06:03:29 +00001316
Martin Josefsson841e4ae2003-05-02 15:30:11 +00001317 if (strlen(tablename) >= TABLE_MAXNAMELEN) {
1318 errno = EINVAL;
1319 return NULL;
1320 }
1321
Derrik Pates664c0a32005-02-01 13:28:14 +00001322 if (sockfd_use == 0) {
1323 sockfd = socket(TC_AF, SOCK_RAW, IPPROTO_RAW);
1324 if (sockfd < 0)
1325 return NULL;
1326 }
1327 sockfd_use++;
Patrick McHardy2f932052008-04-02 14:01:53 +02001328retry:
Marc Bouchere6869a82000-03-20 06:03:29 +00001329 s = sizeof(info);
Martin Josefsson841e4ae2003-05-02 15:30:11 +00001330
Marc Bouchere6869a82000-03-20 06:03:29 +00001331 strcpy(info.name, tablename);
Derrik Pates664c0a32005-02-01 13:28:14 +00001332 if (getsockopt(sockfd, TC_IPPROTO, SO_GET_INFO, &info, &s) < 0) {
1333 if (--sockfd_use == 0) {
1334 close(sockfd);
1335 sockfd = -1;
1336 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001337 return NULL;
Derrik Pates664c0a32005-02-01 13:28:14 +00001338 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001339
Harald Welteaae69be2004-08-29 23:32:14 +00001340 DEBUGP("valid_hooks=0x%08x, num_entries=%u, size=%u\n",
1341 info.valid_hooks, info.num_entries, info.size);
1342
Harald Welte0113fe72004-01-06 19:04:02 +00001343 if ((h = alloc_handle(info.name, info.size, info.num_entries))
Martin Josefsson841e4ae2003-05-02 15:30:11 +00001344 == NULL) {
Derrik Pates664c0a32005-02-01 13:28:14 +00001345 if (--sockfd_use == 0) {
1346 close(sockfd);
1347 sockfd = -1;
1348 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001349 return NULL;
Martin Josefsson841e4ae2003-05-02 15:30:11 +00001350 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001351
Marc Bouchere6869a82000-03-20 06:03:29 +00001352 /* Initialize current state */
1353 h->info = info;
Harald Welte0113fe72004-01-06 19:04:02 +00001354
Harald Welteaae69be2004-08-29 23:32:14 +00001355 h->entries->size = h->info.size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001356
Rusty Russell79dee072000-05-02 16:45:16 +00001357 tmp = sizeof(STRUCT_GET_ENTRIES) + h->info.size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001358
Harald Welteaae69be2004-08-29 23:32:14 +00001359 if (getsockopt(sockfd, TC_IPPROTO, SO_GET_ENTRIES, h->entries,
1360 &tmp) < 0)
1361 goto error;
1362
1363#ifdef IPTC_DEBUG2
1364 {
1365 int fd = open("/tmp/libiptc-so_get_entries.blob",
1366 O_CREAT|O_WRONLY);
1367 if (fd >= 0) {
1368 write(fd, h->entries, tmp);
1369 close(fd);
1370 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001371 }
Harald Welteaae69be2004-08-29 23:32:14 +00001372#endif
1373
1374 if (parse_table(h) < 0)
1375 goto error;
Rusty Russell7e53bf92000-03-20 07:03:28 +00001376
Marc Bouchere6869a82000-03-20 06:03:29 +00001377 CHECK(h);
1378 return h;
Harald Welteaae69be2004-08-29 23:32:14 +00001379error:
1380 TC_FREE(&h);
Patrick McHardy2f932052008-04-02 14:01:53 +02001381 /* A different process changed the ruleset size, retry */
1382 if (errno == EAGAIN)
1383 goto retry;
Harald Welteaae69be2004-08-29 23:32:14 +00001384 return NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +00001385}
1386
Martin Josefsson841e4ae2003-05-02 15:30:11 +00001387void
1388TC_FREE(TC_HANDLE_T *h)
1389{
Harald Welteaae69be2004-08-29 23:32:14 +00001390 struct chain_head *c, *tmp;
1391
Derrik Pates664c0a32005-02-01 13:28:14 +00001392 iptc_fn = TC_FREE;
1393 if (--sockfd_use == 0) {
1394 close(sockfd);
1395 sockfd = -1;
1396 }
Harald Welteaae69be2004-08-29 23:32:14 +00001397
1398 list_for_each_entry_safe(c, tmp, &(*h)->chains, list) {
1399 struct rule_head *r, *rtmp;
1400
1401 list_for_each_entry_safe(r, rtmp, &c->rules, list) {
1402 free(r);
1403 }
1404
1405 free(c);
1406 }
1407
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +00001408 iptcc_chain_index_free(*h);
1409
Harald Welteaae69be2004-08-29 23:32:14 +00001410 free((*h)->entries);
Martin Josefsson841e4ae2003-05-02 15:30:11 +00001411 free(*h);
Harald Welteaae69be2004-08-29 23:32:14 +00001412
Martin Josefsson841e4ae2003-05-02 15:30:11 +00001413 *h = NULL;
1414}
1415
Marc Bouchere6869a82000-03-20 06:03:29 +00001416static inline int
Rusty Russell79dee072000-05-02 16:45:16 +00001417print_match(const STRUCT_ENTRY_MATCH *m)
Marc Bouchere6869a82000-03-20 06:03:29 +00001418{
Rusty Russell228e98d2000-04-27 10:28:06 +00001419 printf("Match name: `%s'\n", m->u.user.name);
Marc Bouchere6869a82000-03-20 06:03:29 +00001420 return 0;
1421}
1422
Rusty Russell79dee072000-05-02 16:45:16 +00001423static int dump_entry(STRUCT_ENTRY *e, const TC_HANDLE_T handle);
1424
Marc Bouchere6869a82000-03-20 06:03:29 +00001425void
Rusty Russell79dee072000-05-02 16:45:16 +00001426TC_DUMP_ENTRIES(const TC_HANDLE_T handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001427{
Derrik Pates664c0a32005-02-01 13:28:14 +00001428 iptc_fn = TC_DUMP_ENTRIES;
Marc Bouchere6869a82000-03-20 06:03:29 +00001429 CHECK(handle);
Patrick McHardy97fb2f12007-09-08 16:52:25 +00001430
Rusty Russelle45c7132004-12-16 13:21:44 +00001431 printf("libiptc v%s. %u bytes.\n",
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +02001432 XTABLES_VERSION, handle->entries->size);
Marc Bouchere6869a82000-03-20 06:03:29 +00001433 printf("Table `%s'\n", handle->info.name);
1434 printf("Hooks: pre/in/fwd/out/post = %u/%u/%u/%u/%u\n",
Rusty Russell67088e72000-05-10 01:18:57 +00001435 handle->info.hook_entry[HOOK_PRE_ROUTING],
1436 handle->info.hook_entry[HOOK_LOCAL_IN],
1437 handle->info.hook_entry[HOOK_FORWARD],
1438 handle->info.hook_entry[HOOK_LOCAL_OUT],
1439 handle->info.hook_entry[HOOK_POST_ROUTING]);
Marc Bouchere6869a82000-03-20 06:03:29 +00001440 printf("Underflows: pre/in/fwd/out/post = %u/%u/%u/%u/%u\n",
Rusty Russell67088e72000-05-10 01:18:57 +00001441 handle->info.underflow[HOOK_PRE_ROUTING],
1442 handle->info.underflow[HOOK_LOCAL_IN],
1443 handle->info.underflow[HOOK_FORWARD],
1444 handle->info.underflow[HOOK_LOCAL_OUT],
1445 handle->info.underflow[HOOK_POST_ROUTING]);
Marc Bouchere6869a82000-03-20 06:03:29 +00001446
Harald Welteaae69be2004-08-29 23:32:14 +00001447 ENTRY_ITERATE(handle->entries->entrytable, handle->entries->size,
Rusty Russell79dee072000-05-02 16:45:16 +00001448 dump_entry, handle);
Harald Welte0113fe72004-01-06 19:04:02 +00001449}
Rusty Russell30fd6e52000-04-23 09:16:06 +00001450
Marc Bouchere6869a82000-03-20 06:03:29 +00001451/* Does this chain exist? */
Rusty Russell79dee072000-05-02 16:45:16 +00001452int TC_IS_CHAIN(const char *chain, const TC_HANDLE_T handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001453{
Derrik Pates664c0a32005-02-01 13:28:14 +00001454 iptc_fn = TC_IS_CHAIN;
Harald Welteaae69be2004-08-29 23:32:14 +00001455 return iptcc_find_label(chain, handle) != NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +00001456}
1457
Harald Welteaae69be2004-08-29 23:32:14 +00001458static void iptcc_chain_iterator_advance(TC_HANDLE_T handle)
1459{
1460 struct chain_head *c = handle->chain_iterator_cur;
1461
1462 if (c->list.next == &handle->chains)
1463 handle->chain_iterator_cur = NULL;
1464 else
1465 handle->chain_iterator_cur =
1466 list_entry(c->list.next, struct chain_head, list);
1467}
Marc Bouchere6869a82000-03-20 06:03:29 +00001468
Rusty Russell30fd6e52000-04-23 09:16:06 +00001469/* Iterator functions to run through the chains. */
Marc Bouchere6869a82000-03-20 06:03:29 +00001470const char *
Philip Blundell8c700902000-05-15 02:17:52 +00001471TC_FIRST_CHAIN(TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001472{
Harald Welteaae69be2004-08-29 23:32:14 +00001473 struct chain_head *c = list_entry((*handle)->chains.next,
1474 struct chain_head, list);
1475
1476 iptc_fn = TC_FIRST_CHAIN;
1477
1478
1479 if (list_empty(&(*handle)->chains)) {
1480 DEBUGP(": no chains\n");
Harald Welte0113fe72004-01-06 19:04:02 +00001481 return NULL;
Harald Welteaae69be2004-08-29 23:32:14 +00001482 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001483
Harald Welteaae69be2004-08-29 23:32:14 +00001484 (*handle)->chain_iterator_cur = c;
1485 iptcc_chain_iterator_advance(*handle);
Harald Welte0113fe72004-01-06 19:04:02 +00001486
Harald Welteaae69be2004-08-29 23:32:14 +00001487 DEBUGP(": returning `%s'\n", c->name);
1488 return c->name;
Marc Bouchere6869a82000-03-20 06:03:29 +00001489}
1490
Rusty Russell30fd6e52000-04-23 09:16:06 +00001491/* Iterator functions to run through the chains. Returns NULL at end. */
1492const char *
Rusty Russell79dee072000-05-02 16:45:16 +00001493TC_NEXT_CHAIN(TC_HANDLE_T *handle)
Rusty Russell30fd6e52000-04-23 09:16:06 +00001494{
Harald Welteaae69be2004-08-29 23:32:14 +00001495 struct chain_head *c = (*handle)->chain_iterator_cur;
Rusty Russell30fd6e52000-04-23 09:16:06 +00001496
Harald Welteaae69be2004-08-29 23:32:14 +00001497 iptc_fn = TC_NEXT_CHAIN;
1498
1499 if (!c) {
1500 DEBUGP(": no more chains\n");
Rusty Russell30fd6e52000-04-23 09:16:06 +00001501 return NULL;
Harald Welteaae69be2004-08-29 23:32:14 +00001502 }
Rusty Russell30fd6e52000-04-23 09:16:06 +00001503
Harald Welteaae69be2004-08-29 23:32:14 +00001504 iptcc_chain_iterator_advance(*handle);
1505
1506 DEBUGP(": returning `%s'\n", c->name);
1507 return c->name;
Rusty Russell30fd6e52000-04-23 09:16:06 +00001508}
1509
1510/* Get first rule in the given chain: NULL for empty chain. */
Rusty Russell79dee072000-05-02 16:45:16 +00001511const STRUCT_ENTRY *
Philip Blundell8c700902000-05-15 02:17:52 +00001512TC_FIRST_RULE(const char *chain, TC_HANDLE_T *handle)
Rusty Russell30fd6e52000-04-23 09:16:06 +00001513{
Harald Welteaae69be2004-08-29 23:32:14 +00001514 struct chain_head *c;
1515 struct rule_head *r;
Rusty Russell30fd6e52000-04-23 09:16:06 +00001516
Harald Welteaae69be2004-08-29 23:32:14 +00001517 iptc_fn = TC_FIRST_RULE;
1518
1519 DEBUGP("first rule(%s): ", chain);
1520
1521 c = iptcc_find_label(chain, *handle);
Rusty Russell30fd6e52000-04-23 09:16:06 +00001522 if (!c) {
1523 errno = ENOENT;
1524 return NULL;
1525 }
1526
1527 /* Empty chain: single return/policy rule */
Harald Welteaae69be2004-08-29 23:32:14 +00001528 if (list_empty(&c->rules)) {
1529 DEBUGP_C("no rules, returning NULL\n");
Rusty Russell30fd6e52000-04-23 09:16:06 +00001530 return NULL;
Harald Welteaae69be2004-08-29 23:32:14 +00001531 }
Rusty Russell30fd6e52000-04-23 09:16:06 +00001532
Harald Welteaae69be2004-08-29 23:32:14 +00001533 r = list_entry(c->rules.next, struct rule_head, list);
1534 (*handle)->rule_iterator_cur = r;
1535 DEBUGP_C("%p\n", r);
1536
1537 return r->entry;
Rusty Russell30fd6e52000-04-23 09:16:06 +00001538}
1539
1540/* Returns NULL when rules run out. */
Rusty Russell79dee072000-05-02 16:45:16 +00001541const STRUCT_ENTRY *
Philip Blundell8c700902000-05-15 02:17:52 +00001542TC_NEXT_RULE(const STRUCT_ENTRY *prev, TC_HANDLE_T *handle)
Rusty Russell30fd6e52000-04-23 09:16:06 +00001543{
Harald Welteaae69be2004-08-29 23:32:14 +00001544 struct rule_head *r;
Rusty Russell30fd6e52000-04-23 09:16:06 +00001545
Derrik Pates664c0a32005-02-01 13:28:14 +00001546 iptc_fn = TC_NEXT_RULE;
Harald Welteaae69be2004-08-29 23:32:14 +00001547 DEBUGP("rule_iterator_cur=%p...", (*handle)->rule_iterator_cur);
1548
1549 if (!(*handle)->rule_iterator_cur) {
1550 DEBUGP_C("returning NULL\n");
1551 return NULL;
1552 }
1553
1554 r = list_entry((*handle)->rule_iterator_cur->list.next,
1555 struct rule_head, list);
1556
1557 iptc_fn = TC_NEXT_RULE;
1558
1559 DEBUGP_C("next=%p, head=%p...", &r->list,
1560 &(*handle)->rule_iterator_cur->chain->rules);
1561
1562 if (&r->list == &(*handle)->rule_iterator_cur->chain->rules) {
1563 (*handle)->rule_iterator_cur = NULL;
1564 DEBUGP_C("finished, returning NULL\n");
1565 return NULL;
1566 }
1567
1568 (*handle)->rule_iterator_cur = r;
1569
1570 /* NOTE: prev is without any influence ! */
1571 DEBUGP_C("returning rule %p\n", r);
1572 return r->entry;
Rusty Russell30fd6e52000-04-23 09:16:06 +00001573}
1574
Marc Bouchere6869a82000-03-20 06:03:29 +00001575/* How many rules in this chain? */
Jan Engelhardt33690a12008-02-11 00:54:00 +01001576static unsigned int
Rusty Russell79dee072000-05-02 16:45:16 +00001577TC_NUM_RULES(const char *chain, TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001578{
Harald Welteaae69be2004-08-29 23:32:14 +00001579 struct chain_head *c;
1580 iptc_fn = TC_NUM_RULES;
Marc Bouchere6869a82000-03-20 06:03:29 +00001581 CHECK(*handle);
Harald Welteaae69be2004-08-29 23:32:14 +00001582
1583 c = iptcc_find_label(chain, *handle);
1584 if (!c) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001585 errno = ENOENT;
1586 return (unsigned int)-1;
1587 }
Harald Welteaae69be2004-08-29 23:32:14 +00001588
1589 return c->num_rules;
Marc Bouchere6869a82000-03-20 06:03:29 +00001590}
1591
Jan Engelhardt33690a12008-02-11 00:54:00 +01001592static const STRUCT_ENTRY *
1593TC_GET_RULE(const char *chain, unsigned int n, TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001594{
Harald Welteaae69be2004-08-29 23:32:14 +00001595 struct chain_head *c;
1596 struct rule_head *r;
1597
1598 iptc_fn = TC_GET_RULE;
Marc Bouchere6869a82000-03-20 06:03:29 +00001599
1600 CHECK(*handle);
Harald Welteaae69be2004-08-29 23:32:14 +00001601
1602 c = iptcc_find_label(chain, *handle);
1603 if (!c) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001604 errno = ENOENT;
1605 return NULL;
1606 }
1607
Harald Welteaae69be2004-08-29 23:32:14 +00001608 r = iptcc_get_rule_num(c, n);
1609 if (!r)
1610 return NULL;
1611 return r->entry;
Marc Bouchere6869a82000-03-20 06:03:29 +00001612}
1613
1614/* Returns a pointer to the target name of this position. */
Jan Engelhardt33690a12008-02-11 00:54:00 +01001615static const char *standard_target_map(int verdict)
Marc Bouchere6869a82000-03-20 06:03:29 +00001616{
Harald Welteaae69be2004-08-29 23:32:14 +00001617 switch (verdict) {
1618 case RETURN:
1619 return LABEL_RETURN;
1620 break;
1621 case -NF_ACCEPT-1:
1622 return LABEL_ACCEPT;
1623 break;
1624 case -NF_DROP-1:
1625 return LABEL_DROP;
1626 break;
1627 case -NF_QUEUE-1:
1628 return LABEL_QUEUE;
1629 break;
1630 default:
1631 fprintf(stderr, "ERROR: %d not a valid target)\n",
1632 verdict);
1633 abort();
1634 break;
1635 }
1636 /* not reached */
1637 return NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +00001638}
1639
Harald Welteaae69be2004-08-29 23:32:14 +00001640/* Returns a pointer to the target name of this position. */
1641const char *TC_GET_TARGET(const STRUCT_ENTRY *ce,
1642 TC_HANDLE_T *handle)
1643{
1644 STRUCT_ENTRY *e = (STRUCT_ENTRY *)ce;
Rusty Russelle45c7132004-12-16 13:21:44 +00001645 struct rule_head *r = container_of(e, struct rule_head, entry[0]);
Harald Welteaae69be2004-08-29 23:32:14 +00001646
1647 iptc_fn = TC_GET_TARGET;
1648
1649 switch(r->type) {
1650 int spos;
1651 case IPTCC_R_FALLTHROUGH:
1652 return "";
1653 break;
1654 case IPTCC_R_JUMP:
1655 DEBUGP("r=%p, jump=%p, name=`%s'\n", r, r->jump, r->jump->name);
1656 return r->jump->name;
1657 break;
1658 case IPTCC_R_STANDARD:
1659 spos = *(int *)GET_TARGET(e)->data;
1660 DEBUGP("r=%p, spos=%d'\n", r, spos);
1661 return standard_target_map(spos);
1662 break;
1663 case IPTCC_R_MODULE:
1664 return GET_TARGET(e)->u.user.name;
1665 break;
1666 }
1667 return NULL;
1668}
Marc Bouchere6869a82000-03-20 06:03:29 +00001669/* Is this a built-in chain? Actually returns hook + 1. */
1670int
Rusty Russell79dee072000-05-02 16:45:16 +00001671TC_BUILTIN(const char *chain, const TC_HANDLE_T handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001672{
Harald Welteaae69be2004-08-29 23:32:14 +00001673 struct chain_head *c;
1674
1675 iptc_fn = TC_BUILTIN;
Marc Bouchere6869a82000-03-20 06:03:29 +00001676
Harald Welteaae69be2004-08-29 23:32:14 +00001677 c = iptcc_find_label(chain, handle);
1678 if (!c) {
1679 errno = ENOENT;
Martin Josefssonb0f3d2d2004-09-23 18:23:20 +00001680 return 0;
Marc Bouchere6869a82000-03-20 06:03:29 +00001681 }
Harald Welteaae69be2004-08-29 23:32:14 +00001682
1683 return iptcc_is_builtin(c);
Marc Bouchere6869a82000-03-20 06:03:29 +00001684}
1685
1686/* Get the policy of a given built-in chain */
1687const char *
Rusty Russell79dee072000-05-02 16:45:16 +00001688TC_GET_POLICY(const char *chain,
1689 STRUCT_COUNTERS *counters,
1690 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001691{
Harald Welteaae69be2004-08-29 23:32:14 +00001692 struct chain_head *c;
Marc Bouchere6869a82000-03-20 06:03:29 +00001693
Harald Welteaae69be2004-08-29 23:32:14 +00001694 iptc_fn = TC_GET_POLICY;
1695
1696 DEBUGP("called for chain %s\n", chain);
1697
1698 c = iptcc_find_label(chain, *handle);
1699 if (!c) {
1700 errno = ENOENT;
1701 return NULL;
1702 }
1703
1704 if (!iptcc_is_builtin(c))
Marc Bouchere6869a82000-03-20 06:03:29 +00001705 return NULL;
1706
Harald Welteaae69be2004-08-29 23:32:14 +00001707 *counters = c->counters;
Marc Bouchere6869a82000-03-20 06:03:29 +00001708
Harald Welteaae69be2004-08-29 23:32:14 +00001709 return standard_target_map(c->verdict);
Harald Welte0113fe72004-01-06 19:04:02 +00001710}
1711
1712static int
Harald Welteaae69be2004-08-29 23:32:14 +00001713iptcc_standard_map(struct rule_head *r, int verdict)
Harald Welte0113fe72004-01-06 19:04:02 +00001714{
Harald Welteaae69be2004-08-29 23:32:14 +00001715 STRUCT_ENTRY *e = r->entry;
Rusty Russell79dee072000-05-02 16:45:16 +00001716 STRUCT_STANDARD_TARGET *t;
Marc Bouchere6869a82000-03-20 06:03:29 +00001717
Rusty Russell79dee072000-05-02 16:45:16 +00001718 t = (STRUCT_STANDARD_TARGET *)GET_TARGET(e);
Marc Bouchere6869a82000-03-20 06:03:29 +00001719
Rusty Russell67088e72000-05-10 01:18:57 +00001720 if (t->target.u.target_size
Philip Blundell8c700902000-05-15 02:17:52 +00001721 != ALIGN(sizeof(STRUCT_STANDARD_TARGET))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001722 errno = EINVAL;
1723 return 0;
1724 }
1725 /* memset for memcmp convenience on delete/replace */
Rusty Russell79dee072000-05-02 16:45:16 +00001726 memset(t->target.u.user.name, 0, FUNCTION_MAXNAMELEN);
1727 strcpy(t->target.u.user.name, STANDARD_TARGET);
Marc Bouchere6869a82000-03-20 06:03:29 +00001728 t->verdict = verdict;
1729
Harald Welteaae69be2004-08-29 23:32:14 +00001730 r->type = IPTCC_R_STANDARD;
1731
Marc Bouchere6869a82000-03-20 06:03:29 +00001732 return 1;
1733}
Rusty Russell7e53bf92000-03-20 07:03:28 +00001734
Marc Bouchere6869a82000-03-20 06:03:29 +00001735static int
Harald Welteaae69be2004-08-29 23:32:14 +00001736iptcc_map_target(const TC_HANDLE_T handle,
1737 struct rule_head *r)
Marc Bouchere6869a82000-03-20 06:03:29 +00001738{
Harald Welteaae69be2004-08-29 23:32:14 +00001739 STRUCT_ENTRY *e = r->entry;
Harald Welte0113fe72004-01-06 19:04:02 +00001740 STRUCT_ENTRY_TARGET *t = GET_TARGET(e);
Marc Bouchere6869a82000-03-20 06:03:29 +00001741
Marc Bouchere6869a82000-03-20 06:03:29 +00001742 /* Maybe it's empty (=> fall through) */
Harald Welteaae69be2004-08-29 23:32:14 +00001743 if (strcmp(t->u.user.name, "") == 0) {
1744 r->type = IPTCC_R_FALLTHROUGH;
1745 return 1;
1746 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001747 /* Maybe it's a standard target name... */
Rusty Russell79dee072000-05-02 16:45:16 +00001748 else if (strcmp(t->u.user.name, LABEL_ACCEPT) == 0)
Harald Welteaae69be2004-08-29 23:32:14 +00001749 return iptcc_standard_map(r, -NF_ACCEPT - 1);
Rusty Russell79dee072000-05-02 16:45:16 +00001750 else if (strcmp(t->u.user.name, LABEL_DROP) == 0)
Harald Welteaae69be2004-08-29 23:32:14 +00001751 return iptcc_standard_map(r, -NF_DROP - 1);
Rusty Russell67088e72000-05-10 01:18:57 +00001752 else if (strcmp(t->u.user.name, LABEL_QUEUE) == 0)
Harald Welteaae69be2004-08-29 23:32:14 +00001753 return iptcc_standard_map(r, -NF_QUEUE - 1);
Rusty Russell79dee072000-05-02 16:45:16 +00001754 else if (strcmp(t->u.user.name, LABEL_RETURN) == 0)
Harald Welteaae69be2004-08-29 23:32:14 +00001755 return iptcc_standard_map(r, RETURN);
Rusty Russell79dee072000-05-02 16:45:16 +00001756 else if (TC_BUILTIN(t->u.user.name, handle)) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001757 /* Can't jump to builtins. */
1758 errno = EINVAL;
1759 return 0;
1760 } else {
1761 /* Maybe it's an existing chain name. */
Harald Welteaae69be2004-08-29 23:32:14 +00001762 struct chain_head *c;
1763 DEBUGP("trying to find chain `%s': ", t->u.user.name);
Marc Bouchere6869a82000-03-20 06:03:29 +00001764
Harald Welteaae69be2004-08-29 23:32:14 +00001765 c = iptcc_find_label(t->u.user.name, handle);
1766 if (c) {
1767 DEBUGP_C("found!\n");
1768 r->type = IPTCC_R_JUMP;
1769 r->jump = c;
1770 c->references++;
1771 return 1;
1772 }
1773 DEBUGP_C("not found :(\n");
Marc Bouchere6869a82000-03-20 06:03:29 +00001774 }
1775
1776 /* Must be a module? If not, kernel will reject... */
Rusty Russell3aef54d2005-01-03 03:48:40 +00001777 /* memset to all 0 for your memcmp convenience: don't clear version */
Rusty Russell228e98d2000-04-27 10:28:06 +00001778 memset(t->u.user.name + strlen(t->u.user.name),
Marc Bouchere6869a82000-03-20 06:03:29 +00001779 0,
Rusty Russell3aef54d2005-01-03 03:48:40 +00001780 FUNCTION_MAXNAMELEN - 1 - strlen(t->u.user.name));
Rusty Russell733e54b2004-12-16 14:22:23 +00001781 r->type = IPTCC_R_MODULE;
Harald Welteaae69be2004-08-29 23:32:14 +00001782 set_changed(handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001783 return 1;
1784}
1785
Harald Welte0113fe72004-01-06 19:04:02 +00001786/* Insert the entry `fw' in chain `chain' into position `rulenum'. */
Marc Bouchere6869a82000-03-20 06:03:29 +00001787int
Rusty Russell79dee072000-05-02 16:45:16 +00001788TC_INSERT_ENTRY(const IPT_CHAINLABEL chain,
1789 const STRUCT_ENTRY *e,
1790 unsigned int rulenum,
1791 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001792{
Harald Welteaae69be2004-08-29 23:32:14 +00001793 struct chain_head *c;
Martin Josefssoneb066cc2004-09-22 21:04:07 +00001794 struct rule_head *r;
1795 struct list_head *prev;
Marc Bouchere6869a82000-03-20 06:03:29 +00001796
Rusty Russell79dee072000-05-02 16:45:16 +00001797 iptc_fn = TC_INSERT_ENTRY;
Harald Welteaae69be2004-08-29 23:32:14 +00001798
1799 if (!(c = iptcc_find_label(chain, *handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001800 errno = ENOENT;
1801 return 0;
1802 }
1803
Martin Josefssoneb066cc2004-09-22 21:04:07 +00001804 /* first rulenum index = 0
1805 first c->num_rules index = 1 */
1806 if (rulenum > c->num_rules) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001807 errno = E2BIG;
1808 return 0;
1809 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001810
Martin Josefsson631f3612004-09-23 19:25:06 +00001811 /* If we are inserting at the end just take advantage of the
1812 double linked list, insert will happen before the entry
1813 prev points to. */
1814 if (rulenum == c->num_rules) {
Martin Josefssoneb066cc2004-09-22 21:04:07 +00001815 prev = &c->rules;
Martin Josefssona5616dc2004-10-24 22:27:31 +00001816 } else if (rulenum + 1 <= c->num_rules/2) {
Martin Josefsson631f3612004-09-23 19:25:06 +00001817 r = iptcc_get_rule_num(c, rulenum + 1);
Martin Josefssona5616dc2004-10-24 22:27:31 +00001818 prev = &r->list;
1819 } else {
1820 r = iptcc_get_rule_num_reverse(c, c->num_rules - rulenum);
Martin Josefsson631f3612004-09-23 19:25:06 +00001821 prev = &r->list;
1822 }
Martin Josefssoneb066cc2004-09-22 21:04:07 +00001823
Harald Welteaae69be2004-08-29 23:32:14 +00001824 if (!(r = iptcc_alloc_rule(c, e->next_offset))) {
1825 errno = ENOMEM;
Harald Welte0113fe72004-01-06 19:04:02 +00001826 return 0;
Harald Welteaae69be2004-08-29 23:32:14 +00001827 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001828
Harald Welteaae69be2004-08-29 23:32:14 +00001829 memcpy(r->entry, e, e->next_offset);
1830 r->counter_map.maptype = COUNTER_MAP_SET;
1831
1832 if (!iptcc_map_target(*handle, r)) {
1833 free(r);
1834 return 0;
1835 }
1836
Martin Josefssoneb066cc2004-09-22 21:04:07 +00001837 list_add_tail(&r->list, prev);
Harald Welteaae69be2004-08-29 23:32:14 +00001838 c->num_rules++;
1839
1840 set_changed(*handle);
1841
1842 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00001843}
1844
1845/* Atomically replace rule `rulenum' in `chain' with `fw'. */
1846int
Rusty Russell79dee072000-05-02 16:45:16 +00001847TC_REPLACE_ENTRY(const IPT_CHAINLABEL chain,
1848 const STRUCT_ENTRY *e,
1849 unsigned int rulenum,
1850 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001851{
Harald Welteaae69be2004-08-29 23:32:14 +00001852 struct chain_head *c;
1853 struct rule_head *r, *old;
Marc Bouchere6869a82000-03-20 06:03:29 +00001854
Rusty Russell79dee072000-05-02 16:45:16 +00001855 iptc_fn = TC_REPLACE_ENTRY;
Marc Bouchere6869a82000-03-20 06:03:29 +00001856
Harald Welteaae69be2004-08-29 23:32:14 +00001857 if (!(c = iptcc_find_label(chain, *handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001858 errno = ENOENT;
1859 return 0;
1860 }
1861
Martin Josefsson0f9b8b12004-12-18 17:18:49 +00001862 if (rulenum >= c->num_rules) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001863 errno = E2BIG;
1864 return 0;
1865 }
1866
Martin Josefsson0f9b8b12004-12-18 17:18:49 +00001867 /* Take advantage of the double linked list if possible. */
1868 if (rulenum + 1 <= c->num_rules/2) {
1869 old = iptcc_get_rule_num(c, rulenum + 1);
1870 } else {
1871 old = iptcc_get_rule_num_reverse(c, c->num_rules - rulenum);
1872 }
1873
Harald Welteaae69be2004-08-29 23:32:14 +00001874 if (!(r = iptcc_alloc_rule(c, e->next_offset))) {
1875 errno = ENOMEM;
Marc Bouchere6869a82000-03-20 06:03:29 +00001876 return 0;
Harald Welteaae69be2004-08-29 23:32:14 +00001877 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001878
Harald Welteaae69be2004-08-29 23:32:14 +00001879 memcpy(r->entry, e, e->next_offset);
1880 r->counter_map.maptype = COUNTER_MAP_SET;
1881
1882 if (!iptcc_map_target(*handle, r)) {
1883 free(r);
Harald Welte0113fe72004-01-06 19:04:02 +00001884 return 0;
Harald Welteaae69be2004-08-29 23:32:14 +00001885 }
Harald Welte0113fe72004-01-06 19:04:02 +00001886
Harald Welteaae69be2004-08-29 23:32:14 +00001887 list_add(&r->list, &old->list);
1888 iptcc_delete_rule(old);
1889
1890 set_changed(*handle);
1891
1892 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00001893}
1894
Harald Welte0113fe72004-01-06 19:04:02 +00001895/* Append entry `fw' to chain `chain'. Equivalent to insert with
Marc Bouchere6869a82000-03-20 06:03:29 +00001896 rulenum = length of chain. */
1897int
Rusty Russell79dee072000-05-02 16:45:16 +00001898TC_APPEND_ENTRY(const IPT_CHAINLABEL chain,
1899 const STRUCT_ENTRY *e,
1900 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001901{
Harald Welteaae69be2004-08-29 23:32:14 +00001902 struct chain_head *c;
1903 struct rule_head *r;
Marc Bouchere6869a82000-03-20 06:03:29 +00001904
Rusty Russell79dee072000-05-02 16:45:16 +00001905 iptc_fn = TC_APPEND_ENTRY;
Harald Welteaae69be2004-08-29 23:32:14 +00001906 if (!(c = iptcc_find_label(chain, *handle))) {
1907 DEBUGP("unable to find chain `%s'\n", chain);
Marc Bouchere6869a82000-03-20 06:03:29 +00001908 errno = ENOENT;
1909 return 0;
1910 }
1911
Harald Welteaae69be2004-08-29 23:32:14 +00001912 if (!(r = iptcc_alloc_rule(c, e->next_offset))) {
1913 DEBUGP("unable to allocate rule for chain `%s'\n", chain);
1914 errno = ENOMEM;
Harald Welte0113fe72004-01-06 19:04:02 +00001915 return 0;
Harald Welteaae69be2004-08-29 23:32:14 +00001916 }
Harald Welte0113fe72004-01-06 19:04:02 +00001917
Harald Welteaae69be2004-08-29 23:32:14 +00001918 memcpy(r->entry, e, e->next_offset);
1919 r->counter_map.maptype = COUNTER_MAP_SET;
1920
1921 if (!iptcc_map_target(*handle, r)) {
Martin Josefsson12009532004-09-23 18:24:29 +00001922 DEBUGP("unable to map target of rule for chain `%s'\n", chain);
Harald Welteaae69be2004-08-29 23:32:14 +00001923 free(r);
1924 return 0;
1925 }
1926
1927 list_add_tail(&r->list, &c->rules);
1928 c->num_rules++;
1929
1930 set_changed(*handle);
1931
1932 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00001933}
1934
1935static inline int
Rusty Russell79dee072000-05-02 16:45:16 +00001936match_different(const STRUCT_ENTRY_MATCH *a,
Rusty Russelledf14cf2000-04-19 11:26:44 +00001937 const unsigned char *a_elems,
1938 const unsigned char *b_elems,
1939 unsigned char **maskptr)
Marc Bouchere6869a82000-03-20 06:03:29 +00001940{
Rusty Russell79dee072000-05-02 16:45:16 +00001941 const STRUCT_ENTRY_MATCH *b;
Rusty Russelledf14cf2000-04-19 11:26:44 +00001942 unsigned int i;
Marc Bouchere6869a82000-03-20 06:03:29 +00001943
1944 /* Offset of b is the same as a. */
Rusty Russell30fd6e52000-04-23 09:16:06 +00001945 b = (void *)b_elems + ((unsigned char *)a - a_elems);
Marc Bouchere6869a82000-03-20 06:03:29 +00001946
Rusty Russell228e98d2000-04-27 10:28:06 +00001947 if (a->u.match_size != b->u.match_size)
Marc Bouchere6869a82000-03-20 06:03:29 +00001948 return 1;
1949
Rusty Russell228e98d2000-04-27 10:28:06 +00001950 if (strcmp(a->u.user.name, b->u.user.name) != 0)
Marc Bouchere6869a82000-03-20 06:03:29 +00001951 return 1;
1952
Rusty Russell73ef09b2000-07-03 10:24:04 +00001953 *maskptr += ALIGN(sizeof(*a));
Rusty Russelledf14cf2000-04-19 11:26:44 +00001954
Rusty Russell73ef09b2000-07-03 10:24:04 +00001955 for (i = 0; i < a->u.match_size - ALIGN(sizeof(*a)); i++)
Rusty Russelledf14cf2000-04-19 11:26:44 +00001956 if (((a->data[i] ^ b->data[i]) & (*maskptr)[i]) != 0)
Rusty Russell90e712a2000-03-29 04:19:26 +00001957 return 1;
Rusty Russelledf14cf2000-04-19 11:26:44 +00001958 *maskptr += i;
1959 return 0;
1960}
1961
1962static inline int
Rusty Russell733e54b2004-12-16 14:22:23 +00001963target_same(struct rule_head *a, struct rule_head *b,const unsigned char *mask)
Rusty Russelledf14cf2000-04-19 11:26:44 +00001964{
1965 unsigned int i;
Rusty Russell733e54b2004-12-16 14:22:23 +00001966 STRUCT_ENTRY_TARGET *ta, *tb;
Marc Bouchere6869a82000-03-20 06:03:29 +00001967
Rusty Russell733e54b2004-12-16 14:22:23 +00001968 if (a->type != b->type)
1969 return 0;
1970
1971 ta = GET_TARGET(a->entry);
1972 tb = GET_TARGET(b->entry);
1973
1974 switch (a->type) {
1975 case IPTCC_R_FALLTHROUGH:
1976 return 1;
1977 case IPTCC_R_JUMP:
1978 return a->jump == b->jump;
1979 case IPTCC_R_STANDARD:
1980 return ((STRUCT_STANDARD_TARGET *)ta)->verdict
1981 == ((STRUCT_STANDARD_TARGET *)tb)->verdict;
1982 case IPTCC_R_MODULE:
1983 if (ta->u.target_size != tb->u.target_size)
1984 return 0;
1985 if (strcmp(ta->u.user.name, tb->u.user.name) != 0)
1986 return 0;
1987
1988 for (i = 0; i < ta->u.target_size - sizeof(*ta); i++)
Rusty Russelldaade442004-12-29 11:14:52 +00001989 if (((ta->data[i] ^ tb->data[i]) & mask[i]) != 0)
Rusty Russell733e54b2004-12-16 14:22:23 +00001990 return 0;
1991 return 1;
1992 default:
1993 fprintf(stderr, "ERROR: bad type %i\n", a->type);
1994 abort();
1995 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001996}
1997
Rusty Russell733e54b2004-12-16 14:22:23 +00001998static unsigned char *
Rusty Russell79dee072000-05-02 16:45:16 +00001999is_same(const STRUCT_ENTRY *a,
2000 const STRUCT_ENTRY *b,
2001 unsigned char *matchmask);
Marc Bouchere6869a82000-03-20 06:03:29 +00002002
Harald Welte0113fe72004-01-06 19:04:02 +00002003/* Delete the first rule in `chain' which matches `fw'. */
Marc Bouchere6869a82000-03-20 06:03:29 +00002004int
Rusty Russell79dee072000-05-02 16:45:16 +00002005TC_DELETE_ENTRY(const IPT_CHAINLABEL chain,
2006 const STRUCT_ENTRY *origfw,
2007 unsigned char *matchmask,
2008 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00002009{
Harald Welteaae69be2004-08-29 23:32:14 +00002010 struct chain_head *c;
Rusty Russelle45c7132004-12-16 13:21:44 +00002011 struct rule_head *r, *i;
Marc Bouchere6869a82000-03-20 06:03:29 +00002012
Rusty Russell79dee072000-05-02 16:45:16 +00002013 iptc_fn = TC_DELETE_ENTRY;
Harald Welteaae69be2004-08-29 23:32:14 +00002014 if (!(c = iptcc_find_label(chain, *handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002015 errno = ENOENT;
2016 return 0;
2017 }
2018
Rusty Russelle45c7132004-12-16 13:21:44 +00002019 /* Create a rule_head from origfw. */
2020 r = iptcc_alloc_rule(c, origfw->next_offset);
2021 if (!r) {
Harald Welte0113fe72004-01-06 19:04:02 +00002022 errno = ENOMEM;
2023 return 0;
2024 }
2025
Rusty Russelle45c7132004-12-16 13:21:44 +00002026 memcpy(r->entry, origfw, origfw->next_offset);
2027 r->counter_map.maptype = COUNTER_MAP_NOMAP;
2028 if (!iptcc_map_target(*handle, r)) {
2029 DEBUGP("unable to map target of rule for chain `%s'\n", chain);
2030 free(r);
2031 return 0;
Patrick McHardyJesper Brouer04a1e4c2006-07-25 01:50:48 +00002032 } else {
2033 /* iptcc_map_target increment target chain references
2034 * since this is a fake rule only used for matching
2035 * the chain references count is decremented again.
2036 */
2037 if (r->type == IPTCC_R_JUMP
2038 && r->jump)
2039 r->jump->references--;
Rusty Russelle45c7132004-12-16 13:21:44 +00002040 }
Harald Welte0113fe72004-01-06 19:04:02 +00002041
Rusty Russelle45c7132004-12-16 13:21:44 +00002042 list_for_each_entry(i, &c->rules, list) {
Rusty Russell733e54b2004-12-16 14:22:23 +00002043 unsigned char *mask;
Harald Weltefe537072004-08-30 20:28:53 +00002044
Rusty Russell733e54b2004-12-16 14:22:23 +00002045 mask = is_same(r->entry, i->entry, matchmask);
2046 if (!mask)
2047 continue;
Martin Josefsson2a5dbbb2004-09-22 21:37:41 +00002048
Rusty Russell733e54b2004-12-16 14:22:23 +00002049 if (!target_same(r, i, mask))
2050 continue;
2051
2052 /* If we are about to delete the rule that is the
2053 * current iterator, move rule iterator back. next
2054 * pointer will then point to real next node */
2055 if (i == (*handle)->rule_iterator_cur) {
2056 (*handle)->rule_iterator_cur =
2057 list_entry((*handle)->rule_iterator_cur->list.prev,
2058 struct rule_head, list);
Marc Bouchere6869a82000-03-20 06:03:29 +00002059 }
Rusty Russell733e54b2004-12-16 14:22:23 +00002060
2061 c->num_rules--;
2062 iptcc_delete_rule(i);
2063
2064 set_changed(*handle);
2065 free(r);
2066 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00002067 }
2068
Rusty Russelle45c7132004-12-16 13:21:44 +00002069 free(r);
Marc Bouchere6869a82000-03-20 06:03:29 +00002070 errno = ENOENT;
2071 return 0;
Rusty Russell7e53bf92000-03-20 07:03:28 +00002072}
Harald Welteaae69be2004-08-29 23:32:14 +00002073
Marc Bouchere6869a82000-03-20 06:03:29 +00002074
2075/* Delete the rule in position `rulenum' in `chain'. */
2076int
Rusty Russell79dee072000-05-02 16:45:16 +00002077TC_DELETE_NUM_ENTRY(const IPT_CHAINLABEL chain,
2078 unsigned int rulenum,
2079 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00002080{
Harald Welteaae69be2004-08-29 23:32:14 +00002081 struct chain_head *c;
2082 struct rule_head *r;
Marc Bouchere6869a82000-03-20 06:03:29 +00002083
Rusty Russell79dee072000-05-02 16:45:16 +00002084 iptc_fn = TC_DELETE_NUM_ENTRY;
Harald Welteaae69be2004-08-29 23:32:14 +00002085
2086 if (!(c = iptcc_find_label(chain, *handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002087 errno = ENOENT;
2088 return 0;
2089 }
2090
Martin Josefssona5616dc2004-10-24 22:27:31 +00002091 if (rulenum >= c->num_rules) {
Martin Josefsson631f3612004-09-23 19:25:06 +00002092 errno = E2BIG;
2093 return 0;
2094 }
2095
2096 /* Take advantage of the double linked list if possible. */
Martin Josefssona5616dc2004-10-24 22:27:31 +00002097 if (rulenum + 1 <= c->num_rules/2) {
2098 r = iptcc_get_rule_num(c, rulenum + 1);
2099 } else {
2100 r = iptcc_get_rule_num_reverse(c, c->num_rules - rulenum);
Marc Bouchere6869a82000-03-20 06:03:29 +00002101 }
2102
Harald Welteaae69be2004-08-29 23:32:14 +00002103 /* If we are about to delete the rule that is the current
2104 * iterator, move rule iterator back. next pointer will then
2105 * point to real next node */
2106 if (r == (*handle)->rule_iterator_cur) {
2107 (*handle)->rule_iterator_cur =
2108 list_entry((*handle)->rule_iterator_cur->list.prev,
2109 struct rule_head, list);
Harald Welte0113fe72004-01-06 19:04:02 +00002110 }
Marc Bouchere6869a82000-03-20 06:03:29 +00002111
Harald Welteaae69be2004-08-29 23:32:14 +00002112 c->num_rules--;
2113 iptcc_delete_rule(r);
2114
Martin Josefsson2a5dbbb2004-09-22 21:37:41 +00002115 set_changed(*handle);
2116
Harald Welteaae69be2004-08-29 23:32:14 +00002117 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00002118}
2119
2120/* Check the packet `fw' on chain `chain'. Returns the verdict, or
2121 NULL and sets errno. */
2122const char *
Rusty Russell79dee072000-05-02 16:45:16 +00002123TC_CHECK_PACKET(const IPT_CHAINLABEL chain,
2124 STRUCT_ENTRY *entry,
2125 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00002126{
Derrik Pates664c0a32005-02-01 13:28:14 +00002127 iptc_fn = TC_CHECK_PACKET;
Marc Bouchere6869a82000-03-20 06:03:29 +00002128 errno = ENOSYS;
2129 return NULL;
2130}
2131
2132/* Flushes the entries in the given chain (ie. empties chain). */
2133int
Rusty Russell79dee072000-05-02 16:45:16 +00002134TC_FLUSH_ENTRIES(const IPT_CHAINLABEL chain, TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00002135{
Harald Welteaae69be2004-08-29 23:32:14 +00002136 struct chain_head *c;
2137 struct rule_head *r, *tmp;
Marc Bouchere6869a82000-03-20 06:03:29 +00002138
Harald Welte0113fe72004-01-06 19:04:02 +00002139 iptc_fn = TC_FLUSH_ENTRIES;
Harald Welteaae69be2004-08-29 23:32:14 +00002140 if (!(c = iptcc_find_label(chain, *handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002141 errno = ENOENT;
2142 return 0;
2143 }
Marc Bouchere6869a82000-03-20 06:03:29 +00002144
Harald Welteaae69be2004-08-29 23:32:14 +00002145 list_for_each_entry_safe(r, tmp, &c->rules, list) {
2146 iptcc_delete_rule(r);
2147 }
2148
2149 c->num_rules = 0;
2150
2151 set_changed(*handle);
2152
2153 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00002154}
2155
2156/* Zeroes the counters in a chain. */
2157int
Rusty Russell79dee072000-05-02 16:45:16 +00002158TC_ZERO_ENTRIES(const IPT_CHAINLABEL chain, TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00002159{
Harald Welteaae69be2004-08-29 23:32:14 +00002160 struct chain_head *c;
2161 struct rule_head *r;
Rusty Russell7e53bf92000-03-20 07:03:28 +00002162
Derrik Pates664c0a32005-02-01 13:28:14 +00002163 iptc_fn = TC_ZERO_ENTRIES;
Harald Welteaae69be2004-08-29 23:32:14 +00002164 if (!(c = iptcc_find_label(chain, *handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002165 errno = ENOENT;
2166 return 0;
2167 }
Marc Bouchere6869a82000-03-20 06:03:29 +00002168
Andy Gaye5bd1d72006-08-22 02:56:41 +00002169 if (c->counter_map.maptype == COUNTER_MAP_NORMAL_MAP)
2170 c->counter_map.maptype = COUNTER_MAP_ZEROED;
2171
Harald Welteaae69be2004-08-29 23:32:14 +00002172 list_for_each_entry(r, &c->rules, list) {
2173 if (r->counter_map.maptype == COUNTER_MAP_NORMAL_MAP)
2174 r->counter_map.maptype = COUNTER_MAP_ZEROED;
Marc Bouchere6869a82000-03-20 06:03:29 +00002175 }
Harald Welteaae69be2004-08-29 23:32:14 +00002176
Rusty Russell175f6412000-03-24 09:32:20 +00002177 set_changed(*handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00002178
Marc Bouchere6869a82000-03-20 06:03:29 +00002179 return 1;
2180}
2181
Harald Welte1cef74d2001-01-05 15:22:59 +00002182STRUCT_COUNTERS *
2183TC_READ_COUNTER(const IPT_CHAINLABEL chain,
2184 unsigned int rulenum,
2185 TC_HANDLE_T *handle)
2186{
Harald Welteaae69be2004-08-29 23:32:14 +00002187 struct chain_head *c;
2188 struct rule_head *r;
Harald Welte1cef74d2001-01-05 15:22:59 +00002189
2190 iptc_fn = TC_READ_COUNTER;
2191 CHECK(*handle);
2192
Harald Welteaae69be2004-08-29 23:32:14 +00002193 if (!(c = iptcc_find_label(chain, *handle))) {
Harald Welte1cef74d2001-01-05 15:22:59 +00002194 errno = ENOENT;
2195 return NULL;
2196 }
2197
Harald Welteaae69be2004-08-29 23:32:14 +00002198 if (!(r = iptcc_get_rule_num(c, rulenum))) {
Harald Welte0113fe72004-01-06 19:04:02 +00002199 errno = E2BIG;
2200 return NULL;
2201 }
2202
Harald Welteaae69be2004-08-29 23:32:14 +00002203 return &r->entry[0].counters;
Harald Welte1cef74d2001-01-05 15:22:59 +00002204}
2205
2206int
2207TC_ZERO_COUNTER(const IPT_CHAINLABEL chain,
2208 unsigned int rulenum,
2209 TC_HANDLE_T *handle)
2210{
Harald Welteaae69be2004-08-29 23:32:14 +00002211 struct chain_head *c;
2212 struct rule_head *r;
Harald Welte1cef74d2001-01-05 15:22:59 +00002213
2214 iptc_fn = TC_ZERO_COUNTER;
2215 CHECK(*handle);
2216
Harald Welteaae69be2004-08-29 23:32:14 +00002217 if (!(c = iptcc_find_label(chain, *handle))) {
Harald Welte1cef74d2001-01-05 15:22:59 +00002218 errno = ENOENT;
2219 return 0;
2220 }
2221
Harald Welteaae69be2004-08-29 23:32:14 +00002222 if (!(r = iptcc_get_rule_num(c, rulenum))) {
Harald Welte0113fe72004-01-06 19:04:02 +00002223 errno = E2BIG;
2224 return 0;
2225 }
2226
Harald Welteaae69be2004-08-29 23:32:14 +00002227 if (r->counter_map.maptype == COUNTER_MAP_NORMAL_MAP)
2228 r->counter_map.maptype = COUNTER_MAP_ZEROED;
Harald Welte1cef74d2001-01-05 15:22:59 +00002229
2230 set_changed(*handle);
2231
2232 return 1;
2233}
2234
2235int
2236TC_SET_COUNTER(const IPT_CHAINLABEL chain,
2237 unsigned int rulenum,
2238 STRUCT_COUNTERS *counters,
2239 TC_HANDLE_T *handle)
2240{
Harald Welteaae69be2004-08-29 23:32:14 +00002241 struct chain_head *c;
2242 struct rule_head *r;
Harald Welte1cef74d2001-01-05 15:22:59 +00002243 STRUCT_ENTRY *e;
Harald Welte1cef74d2001-01-05 15:22:59 +00002244
2245 iptc_fn = TC_SET_COUNTER;
2246 CHECK(*handle);
2247
Harald Welteaae69be2004-08-29 23:32:14 +00002248 if (!(c = iptcc_find_label(chain, *handle))) {
Harald Welte1cef74d2001-01-05 15:22:59 +00002249 errno = ENOENT;
2250 return 0;
2251 }
Harald Welte0113fe72004-01-06 19:04:02 +00002252
Harald Welteaae69be2004-08-29 23:32:14 +00002253 if (!(r = iptcc_get_rule_num(c, rulenum))) {
Harald Welte0113fe72004-01-06 19:04:02 +00002254 errno = E2BIG;
2255 return 0;
2256 }
2257
Harald Welteaae69be2004-08-29 23:32:14 +00002258 e = r->entry;
2259 r->counter_map.maptype = COUNTER_MAP_SET;
Harald Welte0113fe72004-01-06 19:04:02 +00002260
2261 memcpy(&e->counters, counters, sizeof(STRUCT_COUNTERS));
Harald Welte1cef74d2001-01-05 15:22:59 +00002262
2263 set_changed(*handle);
2264
2265 return 1;
2266}
2267
Marc Bouchere6869a82000-03-20 06:03:29 +00002268/* Creates a new chain. */
2269/* To create a chain, create two rules: error node and unconditional
2270 * return. */
2271int
Rusty Russell79dee072000-05-02 16:45:16 +00002272TC_CREATE_CHAIN(const IPT_CHAINLABEL chain, TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00002273{
Harald Welteaae69be2004-08-29 23:32:14 +00002274 static struct chain_head *c;
Patrick McHardy1f23d3c2008-06-07 15:04:34 +02002275 int capacity;
2276 int exceeded;
Marc Bouchere6869a82000-03-20 06:03:29 +00002277
Rusty Russell79dee072000-05-02 16:45:16 +00002278 iptc_fn = TC_CREATE_CHAIN;
Marc Bouchere6869a82000-03-20 06:03:29 +00002279
2280 /* find_label doesn't cover built-in targets: DROP, ACCEPT,
2281 QUEUE, RETURN. */
Harald Welteaae69be2004-08-29 23:32:14 +00002282 if (iptcc_find_label(chain, *handle)
Rusty Russell79dee072000-05-02 16:45:16 +00002283 || strcmp(chain, LABEL_DROP) == 0
2284 || strcmp(chain, LABEL_ACCEPT) == 0
Rusty Russell67088e72000-05-10 01:18:57 +00002285 || strcmp(chain, LABEL_QUEUE) == 0
Rusty Russell79dee072000-05-02 16:45:16 +00002286 || strcmp(chain, LABEL_RETURN) == 0) {
Harald Welteaae69be2004-08-29 23:32:14 +00002287 DEBUGP("Chain `%s' already exists\n", chain);
Marc Bouchere6869a82000-03-20 06:03:29 +00002288 errno = EEXIST;
2289 return 0;
2290 }
2291
Rusty Russell79dee072000-05-02 16:45:16 +00002292 if (strlen(chain)+1 > sizeof(IPT_CHAINLABEL)) {
Harald Welteaae69be2004-08-29 23:32:14 +00002293 DEBUGP("Chain name `%s' too long\n", chain);
Marc Bouchere6869a82000-03-20 06:03:29 +00002294 errno = EINVAL;
2295 return 0;
2296 }
2297
Harald Welteaae69be2004-08-29 23:32:14 +00002298 c = iptcc_alloc_chain_head(chain, 0);
2299 if (!c) {
2300 DEBUGP("Cannot allocate memory for chain `%s'\n", chain);
2301 errno = ENOMEM;
2302 return 0;
Marc Bouchere6869a82000-03-20 06:03:29 +00002303
Harald Welteaae69be2004-08-29 23:32:14 +00002304 }
Jesper Dangaard Brouer48bde402008-01-15 17:06:48 +00002305 (*handle)->num_chains++; /* New user defined chain */
Marc Bouchere6869a82000-03-20 06:03:29 +00002306
Harald Welteaae69be2004-08-29 23:32:14 +00002307 DEBUGP("Creating chain `%s'\n", chain);
Jesper Dangaard Brouerd8cb7872007-11-28 08:40:26 +00002308 iptc_insert_chain(*handle, c); /* Insert sorted */
Harald Welte0113fe72004-01-06 19:04:02 +00002309
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +00002310 /* Inserting chains don't change the correctness of the chain
2311 * index (except if its smaller than index[0], but that
2312 * handled by iptc_insert_chain). It only causes longer lists
2313 * in the buckets. Thus, only rebuild chain index when the
2314 * capacity is exceed with CHAIN_INDEX_INSERT_MAX chains.
2315 */
Patrick McHardy1f23d3c2008-06-07 15:04:34 +02002316 capacity = (*handle)->chain_index_sz * CHAIN_INDEX_BUCKET_LEN;
2317 exceeded = ((((*handle)->num_chains)-capacity));
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +00002318 if (exceeded > CHAIN_INDEX_INSERT_MAX) {
2319 debug("Capacity(%d) exceeded(%d) rebuild (chains:%d)\n",
2320 capacity, exceeded, (*handle)->num_chains);
2321 iptcc_chain_index_rebuild(*handle);
2322 }
2323
Harald Welte0113fe72004-01-06 19:04:02 +00002324 set_changed(*handle);
2325
Harald Welteaae69be2004-08-29 23:32:14 +00002326 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00002327}
2328
2329/* Get the number of references to this chain. */
2330int
Rusty Russell79dee072000-05-02 16:45:16 +00002331TC_GET_REFERENCES(unsigned int *ref, const IPT_CHAINLABEL chain,
2332 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00002333{
Harald Welteaae69be2004-08-29 23:32:14 +00002334 struct chain_head *c;
Marc Bouchere6869a82000-03-20 06:03:29 +00002335
Derrik Pates664c0a32005-02-01 13:28:14 +00002336 iptc_fn = TC_GET_REFERENCES;
Harald Welteaae69be2004-08-29 23:32:14 +00002337 if (!(c = iptcc_find_label(chain, *handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002338 errno = ENOENT;
2339 return 0;
2340 }
2341
Harald Welteaae69be2004-08-29 23:32:14 +00002342 *ref = c->references;
2343
Marc Bouchere6869a82000-03-20 06:03:29 +00002344 return 1;
2345}
2346
2347/* Deletes a chain. */
2348int
Rusty Russell79dee072000-05-02 16:45:16 +00002349TC_DELETE_CHAIN(const IPT_CHAINLABEL chain, TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00002350{
Marc Bouchere6869a82000-03-20 06:03:29 +00002351 unsigned int references;
Harald Welteaae69be2004-08-29 23:32:14 +00002352 struct chain_head *c;
Rusty Russell7e53bf92000-03-20 07:03:28 +00002353
Rusty Russell79dee072000-05-02 16:45:16 +00002354 iptc_fn = TC_DELETE_CHAIN;
Marc Bouchere6869a82000-03-20 06:03:29 +00002355
Harald Welteaae69be2004-08-29 23:32:14 +00002356 if (!(c = iptcc_find_label(chain, *handle))) {
2357 DEBUGP("cannot find chain `%s'\n", chain);
Marc Bouchere6869a82000-03-20 06:03:29 +00002358 errno = ENOENT;
2359 return 0;
2360 }
2361
Harald Welteaae69be2004-08-29 23:32:14 +00002362 if (TC_BUILTIN(chain, *handle)) {
2363 DEBUGP("cannot remove builtin chain `%s'\n", chain);
2364 errno = EINVAL;
2365 return 0;
2366 }
2367
2368 if (!TC_GET_REFERENCES(&references, chain, handle)) {
2369 DEBUGP("cannot get references on chain `%s'\n", chain);
2370 return 0;
2371 }
2372
2373 if (references > 0) {
2374 DEBUGP("chain `%s' still has references\n", chain);
2375 errno = EMLINK;
2376 return 0;
2377 }
2378
2379 if (c->num_rules) {
2380 DEBUGP("chain `%s' is not empty\n", chain);
Marc Bouchere6869a82000-03-20 06:03:29 +00002381 errno = ENOTEMPTY;
2382 return 0;
2383 }
2384
Harald Welteaae69be2004-08-29 23:32:14 +00002385 /* If we are about to delete the chain that is the current
Jesper Dangaard Brouer48bde402008-01-15 17:06:48 +00002386 * iterator, move chain iterator forward. */
Harald Welteaae69be2004-08-29 23:32:14 +00002387 if (c == (*handle)->chain_iterator_cur)
2388 iptcc_chain_iterator_advance(*handle);
Harald Welte0113fe72004-01-06 19:04:02 +00002389
Jesper Dangaard Brouer48bde402008-01-15 17:06:48 +00002390 (*handle)->num_chains--; /* One user defined chain deleted */
2391
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +00002392 //list_del(&c->list); /* Done in iptcc_chain_index_delete_chain() */
2393 iptcc_chain_index_delete_chain(c, *handle);
2394 free(c);
2395
Harald Welteaae69be2004-08-29 23:32:14 +00002396 DEBUGP("chain `%s' deleted\n", chain);
2397
2398 set_changed(*handle);
2399
2400 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00002401}
2402
2403/* Renames a chain. */
Rusty Russell79dee072000-05-02 16:45:16 +00002404int TC_RENAME_CHAIN(const IPT_CHAINLABEL oldname,
2405 const IPT_CHAINLABEL newname,
2406 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00002407{
Harald Welteaae69be2004-08-29 23:32:14 +00002408 struct chain_head *c;
Rusty Russell79dee072000-05-02 16:45:16 +00002409 iptc_fn = TC_RENAME_CHAIN;
Marc Bouchere6869a82000-03-20 06:03:29 +00002410
Harald Welte1de80462000-10-30 12:00:27 +00002411 /* find_label doesn't cover built-in targets: DROP, ACCEPT,
2412 QUEUE, RETURN. */
Harald Welteaae69be2004-08-29 23:32:14 +00002413 if (iptcc_find_label(newname, *handle)
Rusty Russell79dee072000-05-02 16:45:16 +00002414 || strcmp(newname, LABEL_DROP) == 0
2415 || strcmp(newname, LABEL_ACCEPT) == 0
Harald Welte1de80462000-10-30 12:00:27 +00002416 || strcmp(newname, LABEL_QUEUE) == 0
Rusty Russell79dee072000-05-02 16:45:16 +00002417 || strcmp(newname, LABEL_RETURN) == 0) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002418 errno = EEXIST;
2419 return 0;
2420 }
2421
Harald Welteaae69be2004-08-29 23:32:14 +00002422 if (!(c = iptcc_find_label(oldname, *handle))
Rusty Russell79dee072000-05-02 16:45:16 +00002423 || TC_BUILTIN(oldname, *handle)) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002424 errno = ENOENT;
2425 return 0;
2426 }
2427
Rusty Russell79dee072000-05-02 16:45:16 +00002428 if (strlen(newname)+1 > sizeof(IPT_CHAINLABEL)) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002429 errno = EINVAL;
2430 return 0;
2431 }
2432
Harald Welteaae69be2004-08-29 23:32:14 +00002433 strncpy(c->name, newname, sizeof(IPT_CHAINLABEL));
2434
Harald Welte0113fe72004-01-06 19:04:02 +00002435 set_changed(*handle);
2436
Marc Bouchere6869a82000-03-20 06:03:29 +00002437 return 1;
2438}
2439
2440/* Sets the policy on a built-in chain. */
2441int
Rusty Russell79dee072000-05-02 16:45:16 +00002442TC_SET_POLICY(const IPT_CHAINLABEL chain,
2443 const IPT_CHAINLABEL policy,
Harald Welte1cef74d2001-01-05 15:22:59 +00002444 STRUCT_COUNTERS *counters,
Rusty Russell79dee072000-05-02 16:45:16 +00002445 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00002446{
Harald Welteaae69be2004-08-29 23:32:14 +00002447 struct chain_head *c;
Marc Bouchere6869a82000-03-20 06:03:29 +00002448
Rusty Russell79dee072000-05-02 16:45:16 +00002449 iptc_fn = TC_SET_POLICY;
Marc Bouchere6869a82000-03-20 06:03:29 +00002450
Harald Welteaae69be2004-08-29 23:32:14 +00002451 if (!(c = iptcc_find_label(chain, *handle))) {
2452 DEBUGP("cannot find chain `%s'\n", chain);
2453 errno = ENOENT;
Marc Bouchere6869a82000-03-20 06:03:29 +00002454 return 0;
2455 }
2456
Harald Welteaae69be2004-08-29 23:32:14 +00002457 if (!iptcc_is_builtin(c)) {
2458 DEBUGP("cannot set policy of userdefinedchain `%s'\n", chain);
2459 errno = ENOENT;
2460 return 0;
2461 }
Marc Bouchere6869a82000-03-20 06:03:29 +00002462
Rusty Russell79dee072000-05-02 16:45:16 +00002463 if (strcmp(policy, LABEL_ACCEPT) == 0)
Harald Welteaae69be2004-08-29 23:32:14 +00002464 c->verdict = -NF_ACCEPT - 1;
Rusty Russell79dee072000-05-02 16:45:16 +00002465 else if (strcmp(policy, LABEL_DROP) == 0)
Harald Welteaae69be2004-08-29 23:32:14 +00002466 c->verdict = -NF_DROP - 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00002467 else {
2468 errno = EINVAL;
2469 return 0;
2470 }
Harald Welte1cef74d2001-01-05 15:22:59 +00002471
Harald Welte1cef74d2001-01-05 15:22:59 +00002472 if (counters) {
2473 /* set byte and packet counters */
Harald Welteaae69be2004-08-29 23:32:14 +00002474 memcpy(&c->counters, counters, sizeof(STRUCT_COUNTERS));
2475 c->counter_map.maptype = COUNTER_MAP_SET;
Harald Welte1cef74d2001-01-05 15:22:59 +00002476 } else {
Harald Welteaae69be2004-08-29 23:32:14 +00002477 c->counter_map.maptype = COUNTER_MAP_NOMAP;
Harald Welte1cef74d2001-01-05 15:22:59 +00002478 }
2479
Rusty Russell175f6412000-03-24 09:32:20 +00002480 set_changed(*handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00002481
Marc Bouchere6869a82000-03-20 06:03:29 +00002482 return 1;
2483}
2484
2485/* Without this, on gcc 2.7.2.3, we get:
Rusty Russell79dee072000-05-02 16:45:16 +00002486 libiptc.c: In function `TC_COMMIT':
Marc Bouchere6869a82000-03-20 06:03:29 +00002487 libiptc.c:833: fixed or forbidden register was spilled.
2488 This may be due to a compiler bug or to impossible asm
2489 statements or clauses.
2490*/
2491static void
Rusty Russell79dee072000-05-02 16:45:16 +00002492subtract_counters(STRUCT_COUNTERS *answer,
2493 const STRUCT_COUNTERS *a,
2494 const STRUCT_COUNTERS *b)
Marc Bouchere6869a82000-03-20 06:03:29 +00002495{
2496 answer->pcnt = a->pcnt - b->pcnt;
2497 answer->bcnt = a->bcnt - b->bcnt;
2498}
2499
Harald Welteaae69be2004-08-29 23:32:14 +00002500
Jan Engelhardtdbb77542008-02-11 00:33:30 +01002501static void counters_nomap(STRUCT_COUNTERS_INFO *newcounters, unsigned int idx)
Harald Welteaae69be2004-08-29 23:32:14 +00002502{
Jan Engelhardtdbb77542008-02-11 00:33:30 +01002503 newcounters->counters[idx] = ((STRUCT_COUNTERS) { 0, 0});
Harald Welteaae69be2004-08-29 23:32:14 +00002504 DEBUGP_C("NOMAP => zero\n");
2505}
2506
2507static void counters_normal_map(STRUCT_COUNTERS_INFO *newcounters,
Jan Engelhardtdbb77542008-02-11 00:33:30 +01002508 STRUCT_REPLACE *repl, unsigned int idx,
Harald Welteaae69be2004-08-29 23:32:14 +00002509 unsigned int mappos)
2510{
2511 /* Original read: X.
2512 * Atomic read on replacement: X + Y.
2513 * Currently in kernel: Z.
2514 * Want in kernel: X + Y + Z.
2515 * => Add in X + Y
2516 * => Add in replacement read.
2517 */
Jan Engelhardtdbb77542008-02-11 00:33:30 +01002518 newcounters->counters[idx] = repl->counters[mappos];
Harald Welteaae69be2004-08-29 23:32:14 +00002519 DEBUGP_C("NORMAL_MAP => mappos %u \n", mappos);
2520}
2521
2522static void counters_map_zeroed(STRUCT_COUNTERS_INFO *newcounters,
Jan Engelhardtdbb77542008-02-11 00:33:30 +01002523 STRUCT_REPLACE *repl, unsigned int idx,
2524 unsigned int mappos, STRUCT_COUNTERS *counters)
Harald Welteaae69be2004-08-29 23:32:14 +00002525{
2526 /* Original read: X.
2527 * Atomic read on replacement: X + Y.
2528 * Currently in kernel: Z.
2529 * Want in kernel: Y + Z.
2530 * => Add in Y.
2531 * => Add in (replacement read - original read).
2532 */
Jan Engelhardtdbb77542008-02-11 00:33:30 +01002533 subtract_counters(&newcounters->counters[idx],
Harald Welteaae69be2004-08-29 23:32:14 +00002534 &repl->counters[mappos],
2535 counters);
2536 DEBUGP_C("ZEROED => mappos %u\n", mappos);
2537}
2538
2539static void counters_map_set(STRUCT_COUNTERS_INFO *newcounters,
Jan Engelhardtdbb77542008-02-11 00:33:30 +01002540 unsigned int idx, STRUCT_COUNTERS *counters)
Harald Welteaae69be2004-08-29 23:32:14 +00002541{
2542 /* Want to set counter (iptables-restore) */
2543
Jan Engelhardtdbb77542008-02-11 00:33:30 +01002544 memcpy(&newcounters->counters[idx], counters,
Harald Welteaae69be2004-08-29 23:32:14 +00002545 sizeof(STRUCT_COUNTERS));
2546
2547 DEBUGP_C("SET\n");
2548}
2549
2550
Marc Bouchere6869a82000-03-20 06:03:29 +00002551int
Rusty Russell79dee072000-05-02 16:45:16 +00002552TC_COMMIT(TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00002553{
2554 /* Replace, then map back the counters. */
Rusty Russell79dee072000-05-02 16:45:16 +00002555 STRUCT_REPLACE *repl;
2556 STRUCT_COUNTERS_INFO *newcounters;
Harald Welteaae69be2004-08-29 23:32:14 +00002557 struct chain_head *c;
2558 int ret;
Martin Josefsson841e4ae2003-05-02 15:30:11 +00002559 size_t counterlen;
Harald Welteaae69be2004-08-29 23:32:14 +00002560 int new_number;
2561 unsigned int new_size;
Marc Bouchere6869a82000-03-20 06:03:29 +00002562
Derrik Pates664c0a32005-02-01 13:28:14 +00002563 iptc_fn = TC_COMMIT;
Marc Bouchere6869a82000-03-20 06:03:29 +00002564 CHECK(*handle);
Martin Josefsson841e4ae2003-05-02 15:30:11 +00002565
Marc Bouchere6869a82000-03-20 06:03:29 +00002566 /* Don't commit if nothing changed. */
2567 if (!(*handle)->changed)
2568 goto finished;
2569
Harald Welteaae69be2004-08-29 23:32:14 +00002570 new_number = iptcc_compile_table_prep(*handle, &new_size);
2571 if (new_number < 0) {
2572 errno = ENOMEM;
Harald Welted6ba6f52005-11-12 10:39:40 +00002573 goto out_zero;
Harald Welteaae69be2004-08-29 23:32:14 +00002574 }
2575
2576 repl = malloc(sizeof(*repl) + new_size);
Marc Bouchere6869a82000-03-20 06:03:29 +00002577 if (!repl) {
2578 errno = ENOMEM;
Harald Welted6ba6f52005-11-12 10:39:40 +00002579 goto out_zero;
Marc Bouchere6869a82000-03-20 06:03:29 +00002580 }
Martin Josefssonad3b4f92004-09-22 22:04:07 +00002581 memset(repl, 0, sizeof(*repl) + new_size);
Harald Welteaae69be2004-08-29 23:32:14 +00002582
Rusty Russelle45c7132004-12-16 13:21:44 +00002583#if 0
2584 TC_DUMP_ENTRIES(*handle);
2585#endif
2586
Harald Welteaae69be2004-08-29 23:32:14 +00002587 counterlen = sizeof(STRUCT_COUNTERS_INFO)
2588 + sizeof(STRUCT_COUNTERS) * new_number;
Marc Bouchere6869a82000-03-20 06:03:29 +00002589
2590 /* These are the old counters we will get from kernel */
Rusty Russell79dee072000-05-02 16:45:16 +00002591 repl->counters = malloc(sizeof(STRUCT_COUNTERS)
Marc Bouchere6869a82000-03-20 06:03:29 +00002592 * (*handle)->info.num_entries);
2593 if (!repl->counters) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002594 errno = ENOMEM;
Harald Welted6ba6f52005-11-12 10:39:40 +00002595 goto out_free_repl;
Marc Bouchere6869a82000-03-20 06:03:29 +00002596 }
Marc Bouchere6869a82000-03-20 06:03:29 +00002597 /* These are the counters we're going to put back, later. */
2598 newcounters = malloc(counterlen);
2599 if (!newcounters) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002600 errno = ENOMEM;
Harald Welted6ba6f52005-11-12 10:39:40 +00002601 goto out_free_repl_counters;
Marc Bouchere6869a82000-03-20 06:03:29 +00002602 }
Harald Welteaae69be2004-08-29 23:32:14 +00002603 memset(newcounters, 0, counterlen);
Marc Bouchere6869a82000-03-20 06:03:29 +00002604
2605 strcpy(repl->name, (*handle)->info.name);
Harald Welteaae69be2004-08-29 23:32:14 +00002606 repl->num_entries = new_number;
2607 repl->size = new_size;
2608
Marc Bouchere6869a82000-03-20 06:03:29 +00002609 repl->num_counters = (*handle)->info.num_entries;
2610 repl->valid_hooks = (*handle)->info.valid_hooks;
Harald Welteaae69be2004-08-29 23:32:14 +00002611
2612 DEBUGP("num_entries=%u, size=%u, num_counters=%u\n",
2613 repl->num_entries, repl->size, repl->num_counters);
2614
2615 ret = iptcc_compile_table(*handle, repl);
2616 if (ret < 0) {
2617 errno = ret;
Harald Welted6ba6f52005-11-12 10:39:40 +00002618 goto out_free_newcounters;
Harald Welteaae69be2004-08-29 23:32:14 +00002619 }
2620
2621
2622#ifdef IPTC_DEBUG2
2623 {
2624 int fd = open("/tmp/libiptc-so_set_replace.blob",
2625 O_CREAT|O_WRONLY);
2626 if (fd >= 0) {
2627 write(fd, repl, sizeof(*repl) + repl->size);
2628 close(fd);
2629 }
2630 }
2631#endif
Marc Bouchere6869a82000-03-20 06:03:29 +00002632
Harald Welted6ba6f52005-11-12 10:39:40 +00002633 ret = setsockopt(sockfd, TC_IPPROTO, SO_SET_REPLACE, repl,
2634 sizeof(*repl) + repl->size);
Patrick McHardye0865ad2006-04-22 02:08:56 +00002635 if (ret < 0)
Harald Welted6ba6f52005-11-12 10:39:40 +00002636 goto out_free_newcounters;
Marc Bouchere6869a82000-03-20 06:03:29 +00002637
2638 /* Put counters back. */
2639 strcpy(newcounters->name, (*handle)->info.name);
Harald Welteaae69be2004-08-29 23:32:14 +00002640 newcounters->num_counters = new_number;
Marc Bouchere6869a82000-03-20 06:03:29 +00002641
Harald Welteaae69be2004-08-29 23:32:14 +00002642 list_for_each_entry(c, &(*handle)->chains, list) {
2643 struct rule_head *r;
Marc Bouchere6869a82000-03-20 06:03:29 +00002644
Harald Welteaae69be2004-08-29 23:32:14 +00002645 /* Builtin chains have their own counters */
2646 if (iptcc_is_builtin(c)) {
2647 DEBUGP("counter for chain-index %u: ", c->foot_index);
2648 switch(c->counter_map.maptype) {
2649 case COUNTER_MAP_NOMAP:
2650 counters_nomap(newcounters, c->foot_index);
2651 break;
2652 case COUNTER_MAP_NORMAL_MAP:
2653 counters_normal_map(newcounters, repl,
2654 c->foot_index,
2655 c->counter_map.mappos);
2656 break;
2657 case COUNTER_MAP_ZEROED:
2658 counters_map_zeroed(newcounters, repl,
2659 c->foot_index,
2660 c->counter_map.mappos,
2661 &c->counters);
2662 break;
2663 case COUNTER_MAP_SET:
2664 counters_map_set(newcounters, c->foot_index,
2665 &c->counters);
2666 break;
2667 }
2668 }
Harald Welte1cef74d2001-01-05 15:22:59 +00002669
Harald Welteaae69be2004-08-29 23:32:14 +00002670 list_for_each_entry(r, &c->rules, list) {
2671 DEBUGP("counter for index %u: ", r->index);
2672 switch (r->counter_map.maptype) {
2673 case COUNTER_MAP_NOMAP:
2674 counters_nomap(newcounters, r->index);
2675 break;
Harald Welte1cef74d2001-01-05 15:22:59 +00002676
Harald Welteaae69be2004-08-29 23:32:14 +00002677 case COUNTER_MAP_NORMAL_MAP:
2678 counters_normal_map(newcounters, repl,
2679 r->index,
2680 r->counter_map.mappos);
2681 break;
Harald Welte1cef74d2001-01-05 15:22:59 +00002682
Harald Welteaae69be2004-08-29 23:32:14 +00002683 case COUNTER_MAP_ZEROED:
2684 counters_map_zeroed(newcounters, repl,
2685 r->index,
2686 r->counter_map.mappos,
2687 &r->entry->counters);
2688 break;
2689
2690 case COUNTER_MAP_SET:
2691 counters_map_set(newcounters, r->index,
2692 &r->entry->counters);
2693 break;
2694 }
Marc Bouchere6869a82000-03-20 06:03:29 +00002695 }
2696 }
Rusty Russell62527ce2000-09-04 09:45:54 +00002697
Harald Welteaae69be2004-08-29 23:32:14 +00002698#ifdef IPTC_DEBUG2
2699 {
2700 int fd = open("/tmp/libiptc-so_set_add_counters.blob",
2701 O_CREAT|O_WRONLY);
2702 if (fd >= 0) {
2703 write(fd, newcounters, counterlen);
2704 close(fd);
2705 }
2706 }
2707#endif
2708
Harald Welted6ba6f52005-11-12 10:39:40 +00002709 ret = setsockopt(sockfd, TC_IPPROTO, SO_SET_ADD_COUNTERS,
2710 newcounters, counterlen);
Patrick McHardye0865ad2006-04-22 02:08:56 +00002711 if (ret < 0)
Harald Welted6ba6f52005-11-12 10:39:40 +00002712 goto out_free_newcounters;
Marc Bouchere6869a82000-03-20 06:03:29 +00002713
2714 free(repl->counters);
2715 free(repl);
2716 free(newcounters);
2717
Harald Welted6ba6f52005-11-12 10:39:40 +00002718finished:
Martin Josefsson841e4ae2003-05-02 15:30:11 +00002719 TC_FREE(handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00002720 return 1;
Harald Welted6ba6f52005-11-12 10:39:40 +00002721
2722out_free_newcounters:
2723 free(newcounters);
2724out_free_repl_counters:
2725 free(repl->counters);
2726out_free_repl:
2727 free(repl);
2728out_zero:
2729 return 0;
Marc Bouchere6869a82000-03-20 06:03:29 +00002730}
2731
2732/* Get raw socket. */
2733int
Patrick McHardy0b639362007-09-08 16:00:01 +00002734TC_GET_RAW_SOCKET(void)
Marc Bouchere6869a82000-03-20 06:03:29 +00002735{
2736 return sockfd;
2737}
2738
2739/* Translates errno numbers into more human-readable form than strerror. */
2740const char *
Rusty Russell79dee072000-05-02 16:45:16 +00002741TC_STRERROR(int err)
Marc Bouchere6869a82000-03-20 06:03:29 +00002742{
2743 unsigned int i;
2744 struct table_struct {
2745 void *fn;
2746 int err;
2747 const char *message;
2748 } table [] =
Harald Welte4ccfa632001-07-30 15:12:43 +00002749 { { TC_INIT, EPERM, "Permission denied (you must be root)" },
Rusty Russell79dee072000-05-02 16:45:16 +00002750 { TC_INIT, EINVAL, "Module is wrong version" },
Harald Welte4ccfa632001-07-30 15:12:43 +00002751 { TC_INIT, ENOENT,
2752 "Table does not exist (do you need to insmod?)" },
Rusty Russell79dee072000-05-02 16:45:16 +00002753 { TC_DELETE_CHAIN, ENOTEMPTY, "Chain is not empty" },
2754 { TC_DELETE_CHAIN, EINVAL, "Can't delete built-in chain" },
2755 { TC_DELETE_CHAIN, EMLINK,
Marc Bouchere6869a82000-03-20 06:03:29 +00002756 "Can't delete chain with references left" },
Rusty Russell79dee072000-05-02 16:45:16 +00002757 { TC_CREATE_CHAIN, EEXIST, "Chain already exists" },
2758 { TC_INSERT_ENTRY, E2BIG, "Index of insertion too big" },
2759 { TC_REPLACE_ENTRY, E2BIG, "Index of replacement too big" },
2760 { TC_DELETE_NUM_ENTRY, E2BIG, "Index of deletion too big" },
Harald Welte1cef74d2001-01-05 15:22:59 +00002761 { TC_READ_COUNTER, E2BIG, "Index of counter too big" },
2762 { TC_ZERO_COUNTER, E2BIG, "Index of counter too big" },
Rusty Russell79dee072000-05-02 16:45:16 +00002763 { TC_INSERT_ENTRY, ELOOP, "Loop found in table" },
2764 { TC_INSERT_ENTRY, EINVAL, "Target problem" },
Marc Bouchere6869a82000-03-20 06:03:29 +00002765 /* EINVAL for CHECK probably means bad interface. */
Rusty Russell79dee072000-05-02 16:45:16 +00002766 { TC_CHECK_PACKET, EINVAL,
Marc Boucherc8264992000-04-22 22:34:44 +00002767 "Bad arguments (does that interface exist?)" },
Harald Welte4ccfa632001-07-30 15:12:43 +00002768 { TC_CHECK_PACKET, ENOSYS,
2769 "Checking will most likely never get implemented" },
Marc Bouchere6869a82000-03-20 06:03:29 +00002770 /* ENOENT for DELETE probably means no matching rule */
Rusty Russell79dee072000-05-02 16:45:16 +00002771 { TC_DELETE_ENTRY, ENOENT,
Marc Boucherc8264992000-04-22 22:34:44 +00002772 "Bad rule (does a matching rule exist in that chain?)" },
Rusty Russell79dee072000-05-02 16:45:16 +00002773 { TC_SET_POLICY, ENOENT,
Marc Boucherc8264992000-04-22 22:34:44 +00002774 "Bad built-in chain name" },
Rusty Russell79dee072000-05-02 16:45:16 +00002775 { TC_SET_POLICY, EINVAL,
Marc Boucherc8264992000-04-22 22:34:44 +00002776 "Bad policy name" },
Harald Welte4ccfa632001-07-30 15:12:43 +00002777
2778 { NULL, 0, "Incompatible with this kernel" },
2779 { NULL, ENOPROTOOPT, "iptables who? (do you need to insmod?)" },
2780 { NULL, ENOSYS, "Will be implemented real soon. I promise ;)" },
2781 { NULL, ENOMEM, "Memory allocation problem" },
2782 { NULL, ENOENT, "No chain/target/match by that name" },
Marc Bouchere6869a82000-03-20 06:03:29 +00002783 };
2784
2785 for (i = 0; i < sizeof(table)/sizeof(struct table_struct); i++) {
2786 if ((!table[i].fn || table[i].fn == iptc_fn)
2787 && table[i].err == err)
2788 return table[i].message;
2789 }
2790
2791 return strerror(err);
2792}