blob: 1e2cd6e18d404f5a9e6842e99194cd1a38ab6ff3 [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
Jan Engelhardtfd187312008-11-10 16:59:27 +0100188set_changed(struct xtc_handle *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
Jan Engelhardtfd187312008-11-10 16:59:27 +0100194static void do_check(struct xtc_handle *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 *
Jan Engelhardtfd187312008-11-10 16:59:27 +0100231iptcb_get_entry(struct xtc_handle *h, unsigned int offset)
Harald Welteaae69be2004-08-29 23:32:14 +0000232{
233 return (STRUCT_ENTRY *)((char *)h->entries->entrytable + offset);
234}
235
236static unsigned int
Jan Engelhardtfd187312008-11-10 16:59:27 +0100237iptcb_entry2index(struct xtc_handle *const 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 *
Jan Engelhardtfd187312008-11-10 16:59:27 +0100251iptcb_offset2entry(struct xtc_handle *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
Jan Engelhardtfd187312008-11-10 16:59:27 +0100258iptcb_entry2offset(struct xtc_handle *const 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
Jan Engelhardtfd187312008-11-10 16:59:27 +0100264iptcb_offset2index(struct xtc_handle *const 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
Jan Engelhardtfd187312008-11-10 16:59:27 +0100271iptcb_ent_is_hook_entry(STRUCT_ENTRY *e, struct xtc_handle *h)
Harald Welteaae69be2004-08-29 23:32:14 +0000272{
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,
Jan Engelhardtfd187312008-11-10 16:59:27 +0100332 unsigned int *idx, struct xtc_handle *handle,
Jesper Dangaard Brouer4bae3f12008-07-03 20:31:42 +0200333 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,
Jan Engelhardtfd187312008-11-10 16:59:27 +0100435 struct xtc_handle *handle)
Jesper Dangaard Brouer4bae3f12008-07-03 20:31:42 +0200436{
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,
Jan Engelhardtfd187312008-11-10 16:59:27 +0100444 struct xtc_handle *handle)
Jesper Dangaard Brouer4bae3f12008-07-03 20:31:42 +0200445{
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 *
Jan Engelhardtfd187312008-11-10 16:59:27 +0100464iptcc_linearly_search_chain_index(const char *name, struct xtc_handle *handle)
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000465{
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
Jan Engelhardtfd187312008-11-10 16:59:27 +0100492static int iptcc_chain_index_alloc(struct xtc_handle *h)
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000493{
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
Jan Engelhardtfd187312008-11-10 16:59:27 +0100517static void iptcc_chain_index_free(struct xtc_handle *h)
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000518{
519 h->chain_index_sz = 0;
520 free(h->chain_index);
521}
522
523
524#ifdef DEBUG
Jan Engelhardtfd187312008-11-10 16:59:27 +0100525static void iptcc_chain_index_dump(struct xtc_handle *h)
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000526{
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 */
Jan Engelhardtfd187312008-11-10 16:59:27 +0100540static int iptcc_chain_index_build(struct xtc_handle *h)
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000541{
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
Jan Engelhardtfd187312008-11-10 16:59:27 +0100582static int iptcc_chain_index_rebuild(struct xtc_handle *h)
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000583{
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 */
Jan Engelhardtfd187312008-11-10 16:59:27 +0100604static int iptcc_chain_index_delete_chain(struct chain_head *c, struct xtc_handle *h)
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000605{
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 *
Jan Engelhardtfd187312008-11-10 16:59:27 +0100684iptcc_find_chain_by_offset(struct xtc_handle *handle, unsigned int offset)
Harald Welteaae69be2004-08-29 23:32:14 +0000685{
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 *
Jan Engelhardtfd187312008-11-10 16:59:27 +0100716iptcc_find_label(const char *name, struct xtc_handle *handle)
Harald Welteaae69be2004-08-29 23:32:14 +0000717{
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 */
Jan Engelhardtfd187312008-11-10 16:59:27 +0100813static int __iptcc_p_del_policy(struct xtc_handle *h, unsigned int num)
Harald Welteaae69be2004-08-29 23:32:14 +0000814{
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 */
Jan Engelhardtfd187312008-11-10 16:59:27 +0100845static inline void iptc_insert_chain(struct xtc_handle *h, struct chain_head *c)
Harald Welteec30b6c2005-02-01 16:45:56 +0000846{
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 */
Jan Engelhardtfd187312008-11-10 16:59:27 +0100888static void __iptcc_p_add_chain(struct xtc_handle *h, struct chain_head *c,
Harald Welteaae69be2004-08-29 23:32:14 +0000889 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,
Jan Engelhardtfd187312008-11-10 16:59:27 +0100931 struct xtc_handle *h,
Harald Welteaae69be2004-08-29 23:32:14 +0000932 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 */
Jan Engelhardtfd187312008-11-10 16:59:27 +01001041static int parse_table(struct xtc_handle *h)
Harald Welteaae69be2004-08-29 23:32:14 +00001042{
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
Harald Welteaae69be2004-08-29 23:32:14 +00001079 return 1;
1080}
1081
1082
1083/**********************************************************************
1084 * RULESET COMPILATION (cache -> blob)
1085 **********************************************************************/
1086
1087/* Convenience structures */
1088struct iptcb_chain_start{
1089 STRUCT_ENTRY e;
1090 struct ipt_error_target name;
1091};
1092#define IPTCB_CHAIN_START_SIZE (sizeof(STRUCT_ENTRY) + \
1093 ALIGN(sizeof(struct ipt_error_target)))
1094
1095struct iptcb_chain_foot {
1096 STRUCT_ENTRY e;
1097 STRUCT_STANDARD_TARGET target;
1098};
1099#define IPTCB_CHAIN_FOOT_SIZE (sizeof(STRUCT_ENTRY) + \
1100 ALIGN(sizeof(STRUCT_STANDARD_TARGET)))
1101
1102struct iptcb_chain_error {
1103 STRUCT_ENTRY entry;
1104 struct ipt_error_target target;
1105};
1106#define IPTCB_CHAIN_ERROR_SIZE (sizeof(STRUCT_ENTRY) + \
1107 ALIGN(sizeof(struct ipt_error_target)))
1108
1109
1110
1111/* compile rule from cache into blob */
Jan Engelhardtfd187312008-11-10 16:59:27 +01001112static inline int iptcc_compile_rule (struct xtc_handle *h, STRUCT_REPLACE *repl, struct rule_head *r)
Harald Welteaae69be2004-08-29 23:32:14 +00001113{
1114 /* handle jumps */
1115 if (r->type == IPTCC_R_JUMP) {
1116 STRUCT_STANDARD_TARGET *t;
1117 t = (STRUCT_STANDARD_TARGET *)GET_TARGET(r->entry);
1118 /* memset for memcmp convenience on delete/replace */
1119 memset(t->target.u.user.name, 0, FUNCTION_MAXNAMELEN);
1120 strcpy(t->target.u.user.name, STANDARD_TARGET);
1121 /* Jumps can only happen to builtin chains, so we
1122 * can safely assume that they always have a header */
1123 t->verdict = r->jump->head_offset + IPTCB_CHAIN_START_SIZE;
1124 } else if (r->type == IPTCC_R_FALLTHROUGH) {
1125 STRUCT_STANDARD_TARGET *t;
1126 t = (STRUCT_STANDARD_TARGET *)GET_TARGET(r->entry);
1127 t->verdict = r->offset + r->size;
1128 }
1129
1130 /* copy entry from cache to blob */
1131 memcpy((char *)repl->entries+r->offset, r->entry, r->size);
1132
1133 return 1;
1134}
1135
1136/* compile chain from cache into blob */
Jan Engelhardtfd187312008-11-10 16:59:27 +01001137static int iptcc_compile_chain(struct xtc_handle *h, STRUCT_REPLACE *repl, struct chain_head *c)
Harald Welteaae69be2004-08-29 23:32:14 +00001138{
1139 int ret;
1140 struct rule_head *r;
1141 struct iptcb_chain_start *head;
1142 struct iptcb_chain_foot *foot;
1143
1144 /* only user-defined chains have heaer */
1145 if (!iptcc_is_builtin(c)) {
1146 /* put chain header in place */
1147 head = (void *)repl->entries + c->head_offset;
1148 head->e.target_offset = sizeof(STRUCT_ENTRY);
1149 head->e.next_offset = IPTCB_CHAIN_START_SIZE;
1150 strcpy(head->name.t.u.user.name, ERROR_TARGET);
1151 head->name.t.u.target_size =
1152 ALIGN(sizeof(struct ipt_error_target));
1153 strcpy(head->name.error, c->name);
1154 } else {
1155 repl->hook_entry[c->hooknum-1] = c->head_offset;
1156 repl->underflow[c->hooknum-1] = c->foot_offset;
1157 }
1158
1159 /* iterate over rules */
1160 list_for_each_entry(r, &c->rules, list) {
1161 ret = iptcc_compile_rule(h, repl, r);
1162 if (ret < 0)
1163 return ret;
1164 }
1165
1166 /* put chain footer in place */
1167 foot = (void *)repl->entries + c->foot_offset;
1168 foot->e.target_offset = sizeof(STRUCT_ENTRY);
1169 foot->e.next_offset = IPTCB_CHAIN_FOOT_SIZE;
1170 strcpy(foot->target.target.u.user.name, STANDARD_TARGET);
1171 foot->target.target.u.target_size =
1172 ALIGN(sizeof(STRUCT_STANDARD_TARGET));
1173 /* builtin targets have verdict, others return */
1174 if (iptcc_is_builtin(c))
1175 foot->target.verdict = c->verdict;
1176 else
1177 foot->target.verdict = RETURN;
1178 /* set policy-counters */
1179 memcpy(&foot->e.counters, &c->counters, sizeof(STRUCT_COUNTERS));
1180
1181 return 0;
1182}
1183
1184/* calculate offset and number for every rule in the cache */
Jan Engelhardtfd187312008-11-10 16:59:27 +01001185static int iptcc_compile_chain_offsets(struct xtc_handle *h, struct chain_head *c,
Harald Welteefa8fc22005-07-19 22:03:49 +00001186 unsigned int *offset, unsigned int *num)
Harald Welteaae69be2004-08-29 23:32:14 +00001187{
1188 struct rule_head *r;
1189
1190 c->head_offset = *offset;
1191 DEBUGP("%s: chain_head %u, offset=%u\n", c->name, *num, *offset);
1192
1193 if (!iptcc_is_builtin(c)) {
1194 /* Chain has header */
1195 *offset += sizeof(STRUCT_ENTRY)
1196 + ALIGN(sizeof(struct ipt_error_target));
1197 (*num)++;
1198 }
1199
1200 list_for_each_entry(r, &c->rules, list) {
1201 DEBUGP("rule %u, offset=%u, index=%u\n", *num, *offset, *num);
1202 r->offset = *offset;
1203 r->index = *num;
1204 *offset += r->size;
1205 (*num)++;
1206 }
1207
1208 DEBUGP("%s; chain_foot %u, offset=%u, index=%u\n", c->name, *num,
1209 *offset, *num);
1210 c->foot_offset = *offset;
1211 c->foot_index = *num;
1212 *offset += sizeof(STRUCT_ENTRY)
1213 + ALIGN(sizeof(STRUCT_STANDARD_TARGET));
1214 (*num)++;
1215
1216 return 1;
1217}
1218
1219/* put the pieces back together again */
Jan Engelhardtfd187312008-11-10 16:59:27 +01001220static int iptcc_compile_table_prep(struct xtc_handle *h, unsigned int *size)
Harald Welteaae69be2004-08-29 23:32:14 +00001221{
1222 struct chain_head *c;
1223 unsigned int offset = 0, num = 0;
1224 int ret = 0;
1225
1226 /* First pass: calculate offset for every rule */
1227 list_for_each_entry(c, &h->chains, list) {
1228 ret = iptcc_compile_chain_offsets(h, c, &offset, &num);
1229 if (ret < 0)
1230 return ret;
1231 }
1232
1233 /* Append one error rule at end of chain */
1234 num++;
1235 offset += sizeof(STRUCT_ENTRY)
1236 + ALIGN(sizeof(struct ipt_error_target));
1237
1238 /* ruleset size is now in offset */
1239 *size = offset;
1240 return num;
1241}
1242
Jan Engelhardtfd187312008-11-10 16:59:27 +01001243static int iptcc_compile_table(struct xtc_handle *h, STRUCT_REPLACE *repl)
Harald Welteaae69be2004-08-29 23:32:14 +00001244{
1245 struct chain_head *c;
1246 struct iptcb_chain_error *error;
1247
1248 /* Second pass: copy from cache to offsets, fill in jumps */
1249 list_for_each_entry(c, &h->chains, list) {
1250 int ret = iptcc_compile_chain(h, repl, c);
1251 if (ret < 0)
1252 return ret;
1253 }
1254
1255 /* Append error rule at end of chain */
1256 error = (void *)repl->entries + repl->size - IPTCB_CHAIN_ERROR_SIZE;
1257 error->entry.target_offset = sizeof(STRUCT_ENTRY);
1258 error->entry.next_offset = IPTCB_CHAIN_ERROR_SIZE;
1259 error->target.t.u.user.target_size =
1260 ALIGN(sizeof(struct ipt_error_target));
1261 strcpy((char *)&error->target.t.u.user.name, ERROR_TARGET);
1262 strcpy((char *)&error->target.error, "ERROR");
1263
1264 return 1;
1265}
1266
1267/**********************************************************************
1268 * EXTERNAL API (operates on cache only)
1269 **********************************************************************/
Marc Bouchere6869a82000-03-20 06:03:29 +00001270
1271/* Allocate handle of given size */
Jan Engelhardtfd187312008-11-10 16:59:27 +01001272static struct xtc_handle *
Harald Welte0113fe72004-01-06 19:04:02 +00001273alloc_handle(const char *tablename, unsigned int size, unsigned int num_rules)
Marc Bouchere6869a82000-03-20 06:03:29 +00001274{
1275 size_t len;
Jan Engelhardtfd187312008-11-10 16:59:27 +01001276 struct xtc_handle *h;
Marc Bouchere6869a82000-03-20 06:03:29 +00001277
Harald Welteaae69be2004-08-29 23:32:14 +00001278 len = sizeof(STRUCT_TC_HANDLE) + size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001279
Harald Welteaae69be2004-08-29 23:32:14 +00001280 h = malloc(sizeof(STRUCT_TC_HANDLE));
1281 if (!h) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001282 errno = ENOMEM;
1283 return NULL;
1284 }
Harald Welteaae69be2004-08-29 23:32:14 +00001285 memset(h, 0, sizeof(*h));
1286 INIT_LIST_HEAD(&h->chains);
Marc Bouchere6869a82000-03-20 06:03:29 +00001287 strcpy(h->info.name, tablename);
Harald Welteaae69be2004-08-29 23:32:14 +00001288
Harald Welte0371c0c2004-09-19 21:00:12 +00001289 h->entries = malloc(sizeof(STRUCT_GET_ENTRIES) + size);
Harald Welteaae69be2004-08-29 23:32:14 +00001290 if (!h->entries)
1291 goto out_free_handle;
1292
1293 strcpy(h->entries->name, tablename);
Harald Welte0371c0c2004-09-19 21:00:12 +00001294 h->entries->size = size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001295
1296 return h;
Harald Welteaae69be2004-08-29 23:32:14 +00001297
1298out_free_handle:
1299 free(h);
1300
1301 return NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +00001302}
1303
Harald Welteaae69be2004-08-29 23:32:14 +00001304
Jan Engelhardtfd187312008-11-10 16:59:27 +01001305struct xtc_handle *
Rusty Russell79dee072000-05-02 16:45:16 +00001306TC_INIT(const char *tablename)
Marc Bouchere6869a82000-03-20 06:03:29 +00001307{
Jan Engelhardtfd187312008-11-10 16:59:27 +01001308 struct xtc_handle *h;
Rusty Russell79dee072000-05-02 16:45:16 +00001309 STRUCT_GETINFO info;
Harald Welteefa8fc22005-07-19 22:03:49 +00001310 unsigned int tmp;
Marc Bouchere6869a82000-03-20 06:03:29 +00001311 socklen_t s;
1312
Rusty Russell79dee072000-05-02 16:45:16 +00001313 iptc_fn = TC_INIT;
Marc Bouchere6869a82000-03-20 06:03:29 +00001314
Martin Josefsson841e4ae2003-05-02 15:30:11 +00001315 if (strlen(tablename) >= TABLE_MAXNAMELEN) {
1316 errno = EINVAL;
1317 return NULL;
1318 }
1319
Derrik Pates664c0a32005-02-01 13:28:14 +00001320 if (sockfd_use == 0) {
1321 sockfd = socket(TC_AF, SOCK_RAW, IPPROTO_RAW);
1322 if (sockfd < 0)
1323 return NULL;
1324 }
1325 sockfd_use++;
Patrick McHardy2f932052008-04-02 14:01:53 +02001326retry:
Marc Bouchere6869a82000-03-20 06:03:29 +00001327 s = sizeof(info);
Martin Josefsson841e4ae2003-05-02 15:30:11 +00001328
Marc Bouchere6869a82000-03-20 06:03:29 +00001329 strcpy(info.name, tablename);
Derrik Pates664c0a32005-02-01 13:28:14 +00001330 if (getsockopt(sockfd, TC_IPPROTO, SO_GET_INFO, &info, &s) < 0) {
1331 if (--sockfd_use == 0) {
1332 close(sockfd);
1333 sockfd = -1;
1334 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001335 return NULL;
Derrik Pates664c0a32005-02-01 13:28:14 +00001336 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001337
Harald Welteaae69be2004-08-29 23:32:14 +00001338 DEBUGP("valid_hooks=0x%08x, num_entries=%u, size=%u\n",
1339 info.valid_hooks, info.num_entries, info.size);
1340
Harald Welte0113fe72004-01-06 19:04:02 +00001341 if ((h = alloc_handle(info.name, info.size, info.num_entries))
Martin Josefsson841e4ae2003-05-02 15:30:11 +00001342 == NULL) {
Derrik Pates664c0a32005-02-01 13:28:14 +00001343 if (--sockfd_use == 0) {
1344 close(sockfd);
1345 sockfd = -1;
1346 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001347 return NULL;
Martin Josefsson841e4ae2003-05-02 15:30:11 +00001348 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001349
Marc Bouchere6869a82000-03-20 06:03:29 +00001350 /* Initialize current state */
1351 h->info = info;
Harald Welte0113fe72004-01-06 19:04:02 +00001352
Harald Welteaae69be2004-08-29 23:32:14 +00001353 h->entries->size = h->info.size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001354
Rusty Russell79dee072000-05-02 16:45:16 +00001355 tmp = sizeof(STRUCT_GET_ENTRIES) + h->info.size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001356
Harald Welteaae69be2004-08-29 23:32:14 +00001357 if (getsockopt(sockfd, TC_IPPROTO, SO_GET_ENTRIES, h->entries,
1358 &tmp) < 0)
1359 goto error;
1360
1361#ifdef IPTC_DEBUG2
1362 {
1363 int fd = open("/tmp/libiptc-so_get_entries.blob",
1364 O_CREAT|O_WRONLY);
1365 if (fd >= 0) {
1366 write(fd, h->entries, tmp);
1367 close(fd);
1368 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001369 }
Harald Welteaae69be2004-08-29 23:32:14 +00001370#endif
1371
1372 if (parse_table(h) < 0)
1373 goto error;
Rusty Russell7e53bf92000-03-20 07:03:28 +00001374
Marc Bouchere6869a82000-03-20 06:03:29 +00001375 CHECK(h);
1376 return h;
Harald Welteaae69be2004-08-29 23:32:14 +00001377error:
1378 TC_FREE(&h);
Patrick McHardy2f932052008-04-02 14:01:53 +02001379 /* A different process changed the ruleset size, retry */
1380 if (errno == EAGAIN)
1381 goto retry;
Harald Welteaae69be2004-08-29 23:32:14 +00001382 return NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +00001383}
1384
Martin Josefsson841e4ae2003-05-02 15:30:11 +00001385void
Jan Engelhardtfd187312008-11-10 16:59:27 +01001386TC_FREE(struct xtc_handle **h)
Martin Josefsson841e4ae2003-05-02 15:30:11 +00001387{
Harald Welteaae69be2004-08-29 23:32:14 +00001388 struct chain_head *c, *tmp;
1389
Derrik Pates664c0a32005-02-01 13:28:14 +00001390 iptc_fn = TC_FREE;
1391 if (--sockfd_use == 0) {
1392 close(sockfd);
1393 sockfd = -1;
1394 }
Harald Welteaae69be2004-08-29 23:32:14 +00001395
1396 list_for_each_entry_safe(c, tmp, &(*h)->chains, list) {
1397 struct rule_head *r, *rtmp;
1398
1399 list_for_each_entry_safe(r, rtmp, &c->rules, list) {
1400 free(r);
1401 }
1402
1403 free(c);
1404 }
1405
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +00001406 iptcc_chain_index_free(*h);
1407
Harald Welteaae69be2004-08-29 23:32:14 +00001408 free((*h)->entries);
Martin Josefsson841e4ae2003-05-02 15:30:11 +00001409 free(*h);
Harald Welteaae69be2004-08-29 23:32:14 +00001410
Martin Josefsson841e4ae2003-05-02 15:30:11 +00001411 *h = NULL;
1412}
1413
Marc Bouchere6869a82000-03-20 06:03:29 +00001414static inline int
Rusty Russell79dee072000-05-02 16:45:16 +00001415print_match(const STRUCT_ENTRY_MATCH *m)
Marc Bouchere6869a82000-03-20 06:03:29 +00001416{
Rusty Russell228e98d2000-04-27 10:28:06 +00001417 printf("Match name: `%s'\n", m->u.user.name);
Marc Bouchere6869a82000-03-20 06:03:29 +00001418 return 0;
1419}
1420
Jan Engelhardtfd187312008-11-10 16:59:27 +01001421static int dump_entry(STRUCT_ENTRY *e, struct xtc_handle *const handle);
Rusty Russell79dee072000-05-02 16:45:16 +00001422
Marc Bouchere6869a82000-03-20 06:03:29 +00001423void
Jan Engelhardtfd187312008-11-10 16:59:27 +01001424TC_DUMP_ENTRIES(struct xtc_handle *const handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001425{
Derrik Pates664c0a32005-02-01 13:28:14 +00001426 iptc_fn = TC_DUMP_ENTRIES;
Marc Bouchere6869a82000-03-20 06:03:29 +00001427 CHECK(handle);
Patrick McHardy97fb2f12007-09-08 16:52:25 +00001428
Rusty Russelle45c7132004-12-16 13:21:44 +00001429 printf("libiptc v%s. %u bytes.\n",
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +02001430 XTABLES_VERSION, handle->entries->size);
Marc Bouchere6869a82000-03-20 06:03:29 +00001431 printf("Table `%s'\n", handle->info.name);
1432 printf("Hooks: pre/in/fwd/out/post = %u/%u/%u/%u/%u\n",
Rusty Russell67088e72000-05-10 01:18:57 +00001433 handle->info.hook_entry[HOOK_PRE_ROUTING],
1434 handle->info.hook_entry[HOOK_LOCAL_IN],
1435 handle->info.hook_entry[HOOK_FORWARD],
1436 handle->info.hook_entry[HOOK_LOCAL_OUT],
1437 handle->info.hook_entry[HOOK_POST_ROUTING]);
Marc Bouchere6869a82000-03-20 06:03:29 +00001438 printf("Underflows: pre/in/fwd/out/post = %u/%u/%u/%u/%u\n",
Rusty Russell67088e72000-05-10 01:18:57 +00001439 handle->info.underflow[HOOK_PRE_ROUTING],
1440 handle->info.underflow[HOOK_LOCAL_IN],
1441 handle->info.underflow[HOOK_FORWARD],
1442 handle->info.underflow[HOOK_LOCAL_OUT],
1443 handle->info.underflow[HOOK_POST_ROUTING]);
Marc Bouchere6869a82000-03-20 06:03:29 +00001444
Harald Welteaae69be2004-08-29 23:32:14 +00001445 ENTRY_ITERATE(handle->entries->entrytable, handle->entries->size,
Rusty Russell79dee072000-05-02 16:45:16 +00001446 dump_entry, handle);
Harald Welte0113fe72004-01-06 19:04:02 +00001447}
Rusty Russell30fd6e52000-04-23 09:16:06 +00001448
Marc Bouchere6869a82000-03-20 06:03:29 +00001449/* Does this chain exist? */
Jan Engelhardtfd187312008-11-10 16:59:27 +01001450int TC_IS_CHAIN(const char *chain, struct xtc_handle *const handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001451{
Derrik Pates664c0a32005-02-01 13:28:14 +00001452 iptc_fn = TC_IS_CHAIN;
Harald Welteaae69be2004-08-29 23:32:14 +00001453 return iptcc_find_label(chain, handle) != NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +00001454}
1455
Jan Engelhardtfd187312008-11-10 16:59:27 +01001456static void iptcc_chain_iterator_advance(struct xtc_handle *handle)
Harald Welteaae69be2004-08-29 23:32:14 +00001457{
1458 struct chain_head *c = handle->chain_iterator_cur;
1459
1460 if (c->list.next == &handle->chains)
1461 handle->chain_iterator_cur = NULL;
1462 else
1463 handle->chain_iterator_cur =
1464 list_entry(c->list.next, struct chain_head, list);
1465}
Marc Bouchere6869a82000-03-20 06:03:29 +00001466
Rusty Russell30fd6e52000-04-23 09:16:06 +00001467/* Iterator functions to run through the chains. */
Marc Bouchere6869a82000-03-20 06:03:29 +00001468const char *
Jan Engelhardtfd187312008-11-10 16:59:27 +01001469TC_FIRST_CHAIN(struct xtc_handle **handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001470{
Harald Welteaae69be2004-08-29 23:32:14 +00001471 struct chain_head *c = list_entry((*handle)->chains.next,
1472 struct chain_head, list);
1473
1474 iptc_fn = TC_FIRST_CHAIN;
1475
1476
1477 if (list_empty(&(*handle)->chains)) {
1478 DEBUGP(": no chains\n");
Harald Welte0113fe72004-01-06 19:04:02 +00001479 return NULL;
Harald Welteaae69be2004-08-29 23:32:14 +00001480 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001481
Harald Welteaae69be2004-08-29 23:32:14 +00001482 (*handle)->chain_iterator_cur = c;
1483 iptcc_chain_iterator_advance(*handle);
Harald Welte0113fe72004-01-06 19:04:02 +00001484
Harald Welteaae69be2004-08-29 23:32:14 +00001485 DEBUGP(": returning `%s'\n", c->name);
1486 return c->name;
Marc Bouchere6869a82000-03-20 06:03:29 +00001487}
1488
Rusty Russell30fd6e52000-04-23 09:16:06 +00001489/* Iterator functions to run through the chains. Returns NULL at end. */
1490const char *
Jan Engelhardtfd187312008-11-10 16:59:27 +01001491TC_NEXT_CHAIN(struct xtc_handle **handle)
Rusty Russell30fd6e52000-04-23 09:16:06 +00001492{
Harald Welteaae69be2004-08-29 23:32:14 +00001493 struct chain_head *c = (*handle)->chain_iterator_cur;
Rusty Russell30fd6e52000-04-23 09:16:06 +00001494
Harald Welteaae69be2004-08-29 23:32:14 +00001495 iptc_fn = TC_NEXT_CHAIN;
1496
1497 if (!c) {
1498 DEBUGP(": no more chains\n");
Rusty Russell30fd6e52000-04-23 09:16:06 +00001499 return NULL;
Harald Welteaae69be2004-08-29 23:32:14 +00001500 }
Rusty Russell30fd6e52000-04-23 09:16:06 +00001501
Harald Welteaae69be2004-08-29 23:32:14 +00001502 iptcc_chain_iterator_advance(*handle);
1503
1504 DEBUGP(": returning `%s'\n", c->name);
1505 return c->name;
Rusty Russell30fd6e52000-04-23 09:16:06 +00001506}
1507
1508/* Get first rule in the given chain: NULL for empty chain. */
Rusty Russell79dee072000-05-02 16:45:16 +00001509const STRUCT_ENTRY *
Jan Engelhardtfd187312008-11-10 16:59:27 +01001510TC_FIRST_RULE(const char *chain, struct xtc_handle **handle)
Rusty Russell30fd6e52000-04-23 09:16:06 +00001511{
Harald Welteaae69be2004-08-29 23:32:14 +00001512 struct chain_head *c;
1513 struct rule_head *r;
Rusty Russell30fd6e52000-04-23 09:16:06 +00001514
Harald Welteaae69be2004-08-29 23:32:14 +00001515 iptc_fn = TC_FIRST_RULE;
1516
1517 DEBUGP("first rule(%s): ", chain);
1518
1519 c = iptcc_find_label(chain, *handle);
Rusty Russell30fd6e52000-04-23 09:16:06 +00001520 if (!c) {
1521 errno = ENOENT;
1522 return NULL;
1523 }
1524
1525 /* Empty chain: single return/policy rule */
Harald Welteaae69be2004-08-29 23:32:14 +00001526 if (list_empty(&c->rules)) {
1527 DEBUGP_C("no rules, returning NULL\n");
Rusty Russell30fd6e52000-04-23 09:16:06 +00001528 return NULL;
Harald Welteaae69be2004-08-29 23:32:14 +00001529 }
Rusty Russell30fd6e52000-04-23 09:16:06 +00001530
Harald Welteaae69be2004-08-29 23:32:14 +00001531 r = list_entry(c->rules.next, struct rule_head, list);
1532 (*handle)->rule_iterator_cur = r;
1533 DEBUGP_C("%p\n", r);
1534
1535 return r->entry;
Rusty Russell30fd6e52000-04-23 09:16:06 +00001536}
1537
1538/* Returns NULL when rules run out. */
Rusty Russell79dee072000-05-02 16:45:16 +00001539const STRUCT_ENTRY *
Jan Engelhardtfd187312008-11-10 16:59:27 +01001540TC_NEXT_RULE(const STRUCT_ENTRY *prev, struct xtc_handle **handle)
Rusty Russell30fd6e52000-04-23 09:16:06 +00001541{
Harald Welteaae69be2004-08-29 23:32:14 +00001542 struct rule_head *r;
Rusty Russell30fd6e52000-04-23 09:16:06 +00001543
Derrik Pates664c0a32005-02-01 13:28:14 +00001544 iptc_fn = TC_NEXT_RULE;
Harald Welteaae69be2004-08-29 23:32:14 +00001545 DEBUGP("rule_iterator_cur=%p...", (*handle)->rule_iterator_cur);
1546
1547 if (!(*handle)->rule_iterator_cur) {
1548 DEBUGP_C("returning NULL\n");
1549 return NULL;
1550 }
1551
1552 r = list_entry((*handle)->rule_iterator_cur->list.next,
1553 struct rule_head, list);
1554
1555 iptc_fn = TC_NEXT_RULE;
1556
1557 DEBUGP_C("next=%p, head=%p...", &r->list,
1558 &(*handle)->rule_iterator_cur->chain->rules);
1559
1560 if (&r->list == &(*handle)->rule_iterator_cur->chain->rules) {
1561 (*handle)->rule_iterator_cur = NULL;
1562 DEBUGP_C("finished, returning NULL\n");
1563 return NULL;
1564 }
1565
1566 (*handle)->rule_iterator_cur = r;
1567
1568 /* NOTE: prev is without any influence ! */
1569 DEBUGP_C("returning rule %p\n", r);
1570 return r->entry;
Rusty Russell30fd6e52000-04-23 09:16:06 +00001571}
1572
Marc Bouchere6869a82000-03-20 06:03:29 +00001573/* How many rules in this chain? */
Jan Engelhardt33690a12008-02-11 00:54:00 +01001574static unsigned int
Jan Engelhardtfd187312008-11-10 16:59:27 +01001575TC_NUM_RULES(const char *chain, struct xtc_handle **handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001576{
Harald Welteaae69be2004-08-29 23:32:14 +00001577 struct chain_head *c;
1578 iptc_fn = TC_NUM_RULES;
Marc Bouchere6869a82000-03-20 06:03:29 +00001579 CHECK(*handle);
Harald Welteaae69be2004-08-29 23:32:14 +00001580
1581 c = iptcc_find_label(chain, *handle);
1582 if (!c) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001583 errno = ENOENT;
1584 return (unsigned int)-1;
1585 }
Harald Welteaae69be2004-08-29 23:32:14 +00001586
1587 return c->num_rules;
Marc Bouchere6869a82000-03-20 06:03:29 +00001588}
1589
Jan Engelhardt33690a12008-02-11 00:54:00 +01001590static const STRUCT_ENTRY *
Jan Engelhardtfd187312008-11-10 16:59:27 +01001591TC_GET_RULE(const char *chain, unsigned int n, struct xtc_handle **handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001592{
Harald Welteaae69be2004-08-29 23:32:14 +00001593 struct chain_head *c;
1594 struct rule_head *r;
1595
1596 iptc_fn = TC_GET_RULE;
Marc Bouchere6869a82000-03-20 06:03:29 +00001597
1598 CHECK(*handle);
Harald Welteaae69be2004-08-29 23:32:14 +00001599
1600 c = iptcc_find_label(chain, *handle);
1601 if (!c) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001602 errno = ENOENT;
1603 return NULL;
1604 }
1605
Harald Welteaae69be2004-08-29 23:32:14 +00001606 r = iptcc_get_rule_num(c, n);
1607 if (!r)
1608 return NULL;
1609 return r->entry;
Marc Bouchere6869a82000-03-20 06:03:29 +00001610}
1611
1612/* Returns a pointer to the target name of this position. */
Jan Engelhardt33690a12008-02-11 00:54:00 +01001613static const char *standard_target_map(int verdict)
Marc Bouchere6869a82000-03-20 06:03:29 +00001614{
Harald Welteaae69be2004-08-29 23:32:14 +00001615 switch (verdict) {
1616 case RETURN:
1617 return LABEL_RETURN;
1618 break;
1619 case -NF_ACCEPT-1:
1620 return LABEL_ACCEPT;
1621 break;
1622 case -NF_DROP-1:
1623 return LABEL_DROP;
1624 break;
1625 case -NF_QUEUE-1:
1626 return LABEL_QUEUE;
1627 break;
1628 default:
1629 fprintf(stderr, "ERROR: %d not a valid target)\n",
1630 verdict);
1631 abort();
1632 break;
1633 }
1634 /* not reached */
1635 return NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +00001636}
1637
Harald Welteaae69be2004-08-29 23:32:14 +00001638/* Returns a pointer to the target name of this position. */
1639const char *TC_GET_TARGET(const STRUCT_ENTRY *ce,
Jan Engelhardtfd187312008-11-10 16:59:27 +01001640 struct xtc_handle **handle)
Harald Welteaae69be2004-08-29 23:32:14 +00001641{
1642 STRUCT_ENTRY *e = (STRUCT_ENTRY *)ce;
Rusty Russelle45c7132004-12-16 13:21:44 +00001643 struct rule_head *r = container_of(e, struct rule_head, entry[0]);
Harald Welteaae69be2004-08-29 23:32:14 +00001644
1645 iptc_fn = TC_GET_TARGET;
1646
1647 switch(r->type) {
1648 int spos;
1649 case IPTCC_R_FALLTHROUGH:
1650 return "";
1651 break;
1652 case IPTCC_R_JUMP:
1653 DEBUGP("r=%p, jump=%p, name=`%s'\n", r, r->jump, r->jump->name);
1654 return r->jump->name;
1655 break;
1656 case IPTCC_R_STANDARD:
1657 spos = *(int *)GET_TARGET(e)->data;
1658 DEBUGP("r=%p, spos=%d'\n", r, spos);
1659 return standard_target_map(spos);
1660 break;
1661 case IPTCC_R_MODULE:
1662 return GET_TARGET(e)->u.user.name;
1663 break;
1664 }
1665 return NULL;
1666}
Marc Bouchere6869a82000-03-20 06:03:29 +00001667/* Is this a built-in chain? Actually returns hook + 1. */
1668int
Jan Engelhardtfd187312008-11-10 16:59:27 +01001669TC_BUILTIN(const char *chain, struct xtc_handle *const handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001670{
Harald Welteaae69be2004-08-29 23:32:14 +00001671 struct chain_head *c;
1672
1673 iptc_fn = TC_BUILTIN;
Marc Bouchere6869a82000-03-20 06:03:29 +00001674
Harald Welteaae69be2004-08-29 23:32:14 +00001675 c = iptcc_find_label(chain, handle);
1676 if (!c) {
1677 errno = ENOENT;
Martin Josefssonb0f3d2d2004-09-23 18:23:20 +00001678 return 0;
Marc Bouchere6869a82000-03-20 06:03:29 +00001679 }
Harald Welteaae69be2004-08-29 23:32:14 +00001680
1681 return iptcc_is_builtin(c);
Marc Bouchere6869a82000-03-20 06:03:29 +00001682}
1683
1684/* Get the policy of a given built-in chain */
1685const char *
Rusty Russell79dee072000-05-02 16:45:16 +00001686TC_GET_POLICY(const char *chain,
1687 STRUCT_COUNTERS *counters,
Jan Engelhardtfd187312008-11-10 16:59:27 +01001688 struct xtc_handle **handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001689{
Harald Welteaae69be2004-08-29 23:32:14 +00001690 struct chain_head *c;
Marc Bouchere6869a82000-03-20 06:03:29 +00001691
Harald Welteaae69be2004-08-29 23:32:14 +00001692 iptc_fn = TC_GET_POLICY;
1693
1694 DEBUGP("called for chain %s\n", chain);
1695
1696 c = iptcc_find_label(chain, *handle);
1697 if (!c) {
1698 errno = ENOENT;
1699 return NULL;
1700 }
1701
1702 if (!iptcc_is_builtin(c))
Marc Bouchere6869a82000-03-20 06:03:29 +00001703 return NULL;
1704
Harald Welteaae69be2004-08-29 23:32:14 +00001705 *counters = c->counters;
Marc Bouchere6869a82000-03-20 06:03:29 +00001706
Harald Welteaae69be2004-08-29 23:32:14 +00001707 return standard_target_map(c->verdict);
Harald Welte0113fe72004-01-06 19:04:02 +00001708}
1709
1710static int
Harald Welteaae69be2004-08-29 23:32:14 +00001711iptcc_standard_map(struct rule_head *r, int verdict)
Harald Welte0113fe72004-01-06 19:04:02 +00001712{
Harald Welteaae69be2004-08-29 23:32:14 +00001713 STRUCT_ENTRY *e = r->entry;
Rusty Russell79dee072000-05-02 16:45:16 +00001714 STRUCT_STANDARD_TARGET *t;
Marc Bouchere6869a82000-03-20 06:03:29 +00001715
Rusty Russell79dee072000-05-02 16:45:16 +00001716 t = (STRUCT_STANDARD_TARGET *)GET_TARGET(e);
Marc Bouchere6869a82000-03-20 06:03:29 +00001717
Rusty Russell67088e72000-05-10 01:18:57 +00001718 if (t->target.u.target_size
Philip Blundell8c700902000-05-15 02:17:52 +00001719 != ALIGN(sizeof(STRUCT_STANDARD_TARGET))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001720 errno = EINVAL;
1721 return 0;
1722 }
1723 /* memset for memcmp convenience on delete/replace */
Rusty Russell79dee072000-05-02 16:45:16 +00001724 memset(t->target.u.user.name, 0, FUNCTION_MAXNAMELEN);
1725 strcpy(t->target.u.user.name, STANDARD_TARGET);
Marc Bouchere6869a82000-03-20 06:03:29 +00001726 t->verdict = verdict;
1727
Harald Welteaae69be2004-08-29 23:32:14 +00001728 r->type = IPTCC_R_STANDARD;
1729
Marc Bouchere6869a82000-03-20 06:03:29 +00001730 return 1;
1731}
Rusty Russell7e53bf92000-03-20 07:03:28 +00001732
Marc Bouchere6869a82000-03-20 06:03:29 +00001733static int
Jan Engelhardtfd187312008-11-10 16:59:27 +01001734iptcc_map_target(struct xtc_handle *const handle,
Harald Welteaae69be2004-08-29 23:32:14 +00001735 struct rule_head *r)
Marc Bouchere6869a82000-03-20 06:03:29 +00001736{
Harald Welteaae69be2004-08-29 23:32:14 +00001737 STRUCT_ENTRY *e = r->entry;
Harald Welte0113fe72004-01-06 19:04:02 +00001738 STRUCT_ENTRY_TARGET *t = GET_TARGET(e);
Marc Bouchere6869a82000-03-20 06:03:29 +00001739
Marc Bouchere6869a82000-03-20 06:03:29 +00001740 /* Maybe it's empty (=> fall through) */
Harald Welteaae69be2004-08-29 23:32:14 +00001741 if (strcmp(t->u.user.name, "") == 0) {
1742 r->type = IPTCC_R_FALLTHROUGH;
1743 return 1;
1744 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001745 /* Maybe it's a standard target name... */
Rusty Russell79dee072000-05-02 16:45:16 +00001746 else if (strcmp(t->u.user.name, LABEL_ACCEPT) == 0)
Harald Welteaae69be2004-08-29 23:32:14 +00001747 return iptcc_standard_map(r, -NF_ACCEPT - 1);
Rusty Russell79dee072000-05-02 16:45:16 +00001748 else if (strcmp(t->u.user.name, LABEL_DROP) == 0)
Harald Welteaae69be2004-08-29 23:32:14 +00001749 return iptcc_standard_map(r, -NF_DROP - 1);
Rusty Russell67088e72000-05-10 01:18:57 +00001750 else if (strcmp(t->u.user.name, LABEL_QUEUE) == 0)
Harald Welteaae69be2004-08-29 23:32:14 +00001751 return iptcc_standard_map(r, -NF_QUEUE - 1);
Rusty Russell79dee072000-05-02 16:45:16 +00001752 else if (strcmp(t->u.user.name, LABEL_RETURN) == 0)
Harald Welteaae69be2004-08-29 23:32:14 +00001753 return iptcc_standard_map(r, RETURN);
Rusty Russell79dee072000-05-02 16:45:16 +00001754 else if (TC_BUILTIN(t->u.user.name, handle)) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001755 /* Can't jump to builtins. */
1756 errno = EINVAL;
1757 return 0;
1758 } else {
1759 /* Maybe it's an existing chain name. */
Harald Welteaae69be2004-08-29 23:32:14 +00001760 struct chain_head *c;
1761 DEBUGP("trying to find chain `%s': ", t->u.user.name);
Marc Bouchere6869a82000-03-20 06:03:29 +00001762
Harald Welteaae69be2004-08-29 23:32:14 +00001763 c = iptcc_find_label(t->u.user.name, handle);
1764 if (c) {
1765 DEBUGP_C("found!\n");
1766 r->type = IPTCC_R_JUMP;
1767 r->jump = c;
1768 c->references++;
1769 return 1;
1770 }
1771 DEBUGP_C("not found :(\n");
Marc Bouchere6869a82000-03-20 06:03:29 +00001772 }
1773
1774 /* Must be a module? If not, kernel will reject... */
Rusty Russell3aef54d2005-01-03 03:48:40 +00001775 /* memset to all 0 for your memcmp convenience: don't clear version */
Rusty Russell228e98d2000-04-27 10:28:06 +00001776 memset(t->u.user.name + strlen(t->u.user.name),
Marc Bouchere6869a82000-03-20 06:03:29 +00001777 0,
Rusty Russell3aef54d2005-01-03 03:48:40 +00001778 FUNCTION_MAXNAMELEN - 1 - strlen(t->u.user.name));
Rusty Russell733e54b2004-12-16 14:22:23 +00001779 r->type = IPTCC_R_MODULE;
Harald Welteaae69be2004-08-29 23:32:14 +00001780 set_changed(handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001781 return 1;
1782}
1783
Harald Welte0113fe72004-01-06 19:04:02 +00001784/* Insert the entry `fw' in chain `chain' into position `rulenum'. */
Marc Bouchere6869a82000-03-20 06:03:29 +00001785int
Rusty Russell79dee072000-05-02 16:45:16 +00001786TC_INSERT_ENTRY(const IPT_CHAINLABEL chain,
1787 const STRUCT_ENTRY *e,
1788 unsigned int rulenum,
Jan Engelhardtfd187312008-11-10 16:59:27 +01001789 struct xtc_handle **handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001790{
Harald Welteaae69be2004-08-29 23:32:14 +00001791 struct chain_head *c;
Martin Josefssoneb066cc2004-09-22 21:04:07 +00001792 struct rule_head *r;
1793 struct list_head *prev;
Marc Bouchere6869a82000-03-20 06:03:29 +00001794
Rusty Russell79dee072000-05-02 16:45:16 +00001795 iptc_fn = TC_INSERT_ENTRY;
Harald Welteaae69be2004-08-29 23:32:14 +00001796
1797 if (!(c = iptcc_find_label(chain, *handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001798 errno = ENOENT;
1799 return 0;
1800 }
1801
Martin Josefssoneb066cc2004-09-22 21:04:07 +00001802 /* first rulenum index = 0
1803 first c->num_rules index = 1 */
1804 if (rulenum > c->num_rules) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001805 errno = E2BIG;
1806 return 0;
1807 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001808
Martin Josefsson631f3612004-09-23 19:25:06 +00001809 /* If we are inserting at the end just take advantage of the
1810 double linked list, insert will happen before the entry
1811 prev points to. */
1812 if (rulenum == c->num_rules) {
Martin Josefssoneb066cc2004-09-22 21:04:07 +00001813 prev = &c->rules;
Martin Josefssona5616dc2004-10-24 22:27:31 +00001814 } else if (rulenum + 1 <= c->num_rules/2) {
Martin Josefsson631f3612004-09-23 19:25:06 +00001815 r = iptcc_get_rule_num(c, rulenum + 1);
Martin Josefssona5616dc2004-10-24 22:27:31 +00001816 prev = &r->list;
1817 } else {
1818 r = iptcc_get_rule_num_reverse(c, c->num_rules - rulenum);
Martin Josefsson631f3612004-09-23 19:25:06 +00001819 prev = &r->list;
1820 }
Martin Josefssoneb066cc2004-09-22 21:04:07 +00001821
Harald Welteaae69be2004-08-29 23:32:14 +00001822 if (!(r = iptcc_alloc_rule(c, e->next_offset))) {
1823 errno = ENOMEM;
Harald Welte0113fe72004-01-06 19:04:02 +00001824 return 0;
Harald Welteaae69be2004-08-29 23:32:14 +00001825 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001826
Harald Welteaae69be2004-08-29 23:32:14 +00001827 memcpy(r->entry, e, e->next_offset);
1828 r->counter_map.maptype = COUNTER_MAP_SET;
1829
1830 if (!iptcc_map_target(*handle, r)) {
1831 free(r);
1832 return 0;
1833 }
1834
Martin Josefssoneb066cc2004-09-22 21:04:07 +00001835 list_add_tail(&r->list, prev);
Harald Welteaae69be2004-08-29 23:32:14 +00001836 c->num_rules++;
1837
1838 set_changed(*handle);
1839
1840 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00001841}
1842
1843/* Atomically replace rule `rulenum' in `chain' with `fw'. */
1844int
Rusty Russell79dee072000-05-02 16:45:16 +00001845TC_REPLACE_ENTRY(const IPT_CHAINLABEL chain,
1846 const STRUCT_ENTRY *e,
1847 unsigned int rulenum,
Jan Engelhardtfd187312008-11-10 16:59:27 +01001848 struct xtc_handle **handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001849{
Harald Welteaae69be2004-08-29 23:32:14 +00001850 struct chain_head *c;
1851 struct rule_head *r, *old;
Marc Bouchere6869a82000-03-20 06:03:29 +00001852
Rusty Russell79dee072000-05-02 16:45:16 +00001853 iptc_fn = TC_REPLACE_ENTRY;
Marc Bouchere6869a82000-03-20 06:03:29 +00001854
Harald Welteaae69be2004-08-29 23:32:14 +00001855 if (!(c = iptcc_find_label(chain, *handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001856 errno = ENOENT;
1857 return 0;
1858 }
1859
Martin Josefsson0f9b8b12004-12-18 17:18:49 +00001860 if (rulenum >= c->num_rules) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001861 errno = E2BIG;
1862 return 0;
1863 }
1864
Martin Josefsson0f9b8b12004-12-18 17:18:49 +00001865 /* Take advantage of the double linked list if possible. */
1866 if (rulenum + 1 <= c->num_rules/2) {
1867 old = iptcc_get_rule_num(c, rulenum + 1);
1868 } else {
1869 old = iptcc_get_rule_num_reverse(c, c->num_rules - rulenum);
1870 }
1871
Harald Welteaae69be2004-08-29 23:32:14 +00001872 if (!(r = iptcc_alloc_rule(c, e->next_offset))) {
1873 errno = ENOMEM;
Marc Bouchere6869a82000-03-20 06:03:29 +00001874 return 0;
Harald Welteaae69be2004-08-29 23:32:14 +00001875 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001876
Harald Welteaae69be2004-08-29 23:32:14 +00001877 memcpy(r->entry, e, e->next_offset);
1878 r->counter_map.maptype = COUNTER_MAP_SET;
1879
1880 if (!iptcc_map_target(*handle, r)) {
1881 free(r);
Harald Welte0113fe72004-01-06 19:04:02 +00001882 return 0;
Harald Welteaae69be2004-08-29 23:32:14 +00001883 }
Harald Welte0113fe72004-01-06 19:04:02 +00001884
Harald Welteaae69be2004-08-29 23:32:14 +00001885 list_add(&r->list, &old->list);
1886 iptcc_delete_rule(old);
1887
1888 set_changed(*handle);
1889
1890 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00001891}
1892
Harald Welte0113fe72004-01-06 19:04:02 +00001893/* Append entry `fw' to chain `chain'. Equivalent to insert with
Marc Bouchere6869a82000-03-20 06:03:29 +00001894 rulenum = length of chain. */
1895int
Rusty Russell79dee072000-05-02 16:45:16 +00001896TC_APPEND_ENTRY(const IPT_CHAINLABEL chain,
1897 const STRUCT_ENTRY *e,
Jan Engelhardtfd187312008-11-10 16:59:27 +01001898 struct xtc_handle **handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001899{
Harald Welteaae69be2004-08-29 23:32:14 +00001900 struct chain_head *c;
1901 struct rule_head *r;
Marc Bouchere6869a82000-03-20 06:03:29 +00001902
Rusty Russell79dee072000-05-02 16:45:16 +00001903 iptc_fn = TC_APPEND_ENTRY;
Harald Welteaae69be2004-08-29 23:32:14 +00001904 if (!(c = iptcc_find_label(chain, *handle))) {
1905 DEBUGP("unable to find chain `%s'\n", chain);
Marc Bouchere6869a82000-03-20 06:03:29 +00001906 errno = ENOENT;
1907 return 0;
1908 }
1909
Harald Welteaae69be2004-08-29 23:32:14 +00001910 if (!(r = iptcc_alloc_rule(c, e->next_offset))) {
1911 DEBUGP("unable to allocate rule for chain `%s'\n", chain);
1912 errno = ENOMEM;
Harald Welte0113fe72004-01-06 19:04:02 +00001913 return 0;
Harald Welteaae69be2004-08-29 23:32:14 +00001914 }
Harald Welte0113fe72004-01-06 19:04:02 +00001915
Harald Welteaae69be2004-08-29 23:32:14 +00001916 memcpy(r->entry, e, e->next_offset);
1917 r->counter_map.maptype = COUNTER_MAP_SET;
1918
1919 if (!iptcc_map_target(*handle, r)) {
Martin Josefsson12009532004-09-23 18:24:29 +00001920 DEBUGP("unable to map target of rule for chain `%s'\n", chain);
Harald Welteaae69be2004-08-29 23:32:14 +00001921 free(r);
1922 return 0;
1923 }
1924
1925 list_add_tail(&r->list, &c->rules);
1926 c->num_rules++;
1927
1928 set_changed(*handle);
1929
1930 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00001931}
1932
1933static inline int
Rusty Russell79dee072000-05-02 16:45:16 +00001934match_different(const STRUCT_ENTRY_MATCH *a,
Rusty Russelledf14cf2000-04-19 11:26:44 +00001935 const unsigned char *a_elems,
1936 const unsigned char *b_elems,
1937 unsigned char **maskptr)
Marc Bouchere6869a82000-03-20 06:03:29 +00001938{
Rusty Russell79dee072000-05-02 16:45:16 +00001939 const STRUCT_ENTRY_MATCH *b;
Rusty Russelledf14cf2000-04-19 11:26:44 +00001940 unsigned int i;
Marc Bouchere6869a82000-03-20 06:03:29 +00001941
1942 /* Offset of b is the same as a. */
Rusty Russell30fd6e52000-04-23 09:16:06 +00001943 b = (void *)b_elems + ((unsigned char *)a - a_elems);
Marc Bouchere6869a82000-03-20 06:03:29 +00001944
Rusty Russell228e98d2000-04-27 10:28:06 +00001945 if (a->u.match_size != b->u.match_size)
Marc Bouchere6869a82000-03-20 06:03:29 +00001946 return 1;
1947
Rusty Russell228e98d2000-04-27 10:28:06 +00001948 if (strcmp(a->u.user.name, b->u.user.name) != 0)
Marc Bouchere6869a82000-03-20 06:03:29 +00001949 return 1;
1950
Rusty Russell73ef09b2000-07-03 10:24:04 +00001951 *maskptr += ALIGN(sizeof(*a));
Rusty Russelledf14cf2000-04-19 11:26:44 +00001952
Rusty Russell73ef09b2000-07-03 10:24:04 +00001953 for (i = 0; i < a->u.match_size - ALIGN(sizeof(*a)); i++)
Rusty Russelledf14cf2000-04-19 11:26:44 +00001954 if (((a->data[i] ^ b->data[i]) & (*maskptr)[i]) != 0)
Rusty Russell90e712a2000-03-29 04:19:26 +00001955 return 1;
Rusty Russelledf14cf2000-04-19 11:26:44 +00001956 *maskptr += i;
1957 return 0;
1958}
1959
1960static inline int
Rusty Russell733e54b2004-12-16 14:22:23 +00001961target_same(struct rule_head *a, struct rule_head *b,const unsigned char *mask)
Rusty Russelledf14cf2000-04-19 11:26:44 +00001962{
1963 unsigned int i;
Rusty Russell733e54b2004-12-16 14:22:23 +00001964 STRUCT_ENTRY_TARGET *ta, *tb;
Marc Bouchere6869a82000-03-20 06:03:29 +00001965
Rusty Russell733e54b2004-12-16 14:22:23 +00001966 if (a->type != b->type)
1967 return 0;
1968
1969 ta = GET_TARGET(a->entry);
1970 tb = GET_TARGET(b->entry);
1971
1972 switch (a->type) {
1973 case IPTCC_R_FALLTHROUGH:
1974 return 1;
1975 case IPTCC_R_JUMP:
1976 return a->jump == b->jump;
1977 case IPTCC_R_STANDARD:
1978 return ((STRUCT_STANDARD_TARGET *)ta)->verdict
1979 == ((STRUCT_STANDARD_TARGET *)tb)->verdict;
1980 case IPTCC_R_MODULE:
1981 if (ta->u.target_size != tb->u.target_size)
1982 return 0;
1983 if (strcmp(ta->u.user.name, tb->u.user.name) != 0)
1984 return 0;
1985
1986 for (i = 0; i < ta->u.target_size - sizeof(*ta); i++)
Rusty Russelldaade442004-12-29 11:14:52 +00001987 if (((ta->data[i] ^ tb->data[i]) & mask[i]) != 0)
Rusty Russell733e54b2004-12-16 14:22:23 +00001988 return 0;
1989 return 1;
1990 default:
1991 fprintf(stderr, "ERROR: bad type %i\n", a->type);
1992 abort();
1993 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001994}
1995
Rusty Russell733e54b2004-12-16 14:22:23 +00001996static unsigned char *
Rusty Russell79dee072000-05-02 16:45:16 +00001997is_same(const STRUCT_ENTRY *a,
1998 const STRUCT_ENTRY *b,
1999 unsigned char *matchmask);
Marc Bouchere6869a82000-03-20 06:03:29 +00002000
Harald Welte0113fe72004-01-06 19:04:02 +00002001/* Delete the first rule in `chain' which matches `fw'. */
Marc Bouchere6869a82000-03-20 06:03:29 +00002002int
Rusty Russell79dee072000-05-02 16:45:16 +00002003TC_DELETE_ENTRY(const IPT_CHAINLABEL chain,
2004 const STRUCT_ENTRY *origfw,
2005 unsigned char *matchmask,
Jan Engelhardtfd187312008-11-10 16:59:27 +01002006 struct xtc_handle **handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00002007{
Harald Welteaae69be2004-08-29 23:32:14 +00002008 struct chain_head *c;
Rusty Russelle45c7132004-12-16 13:21:44 +00002009 struct rule_head *r, *i;
Marc Bouchere6869a82000-03-20 06:03:29 +00002010
Rusty Russell79dee072000-05-02 16:45:16 +00002011 iptc_fn = TC_DELETE_ENTRY;
Harald Welteaae69be2004-08-29 23:32:14 +00002012 if (!(c = iptcc_find_label(chain, *handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002013 errno = ENOENT;
2014 return 0;
2015 }
2016
Rusty Russelle45c7132004-12-16 13:21:44 +00002017 /* Create a rule_head from origfw. */
2018 r = iptcc_alloc_rule(c, origfw->next_offset);
2019 if (!r) {
Harald Welte0113fe72004-01-06 19:04:02 +00002020 errno = ENOMEM;
2021 return 0;
2022 }
2023
Rusty Russelle45c7132004-12-16 13:21:44 +00002024 memcpy(r->entry, origfw, origfw->next_offset);
2025 r->counter_map.maptype = COUNTER_MAP_NOMAP;
2026 if (!iptcc_map_target(*handle, r)) {
2027 DEBUGP("unable to map target of rule for chain `%s'\n", chain);
2028 free(r);
2029 return 0;
Patrick McHardyJesper Brouer04a1e4c2006-07-25 01:50:48 +00002030 } else {
2031 /* iptcc_map_target increment target chain references
2032 * since this is a fake rule only used for matching
2033 * the chain references count is decremented again.
2034 */
2035 if (r->type == IPTCC_R_JUMP
2036 && r->jump)
2037 r->jump->references--;
Rusty Russelle45c7132004-12-16 13:21:44 +00002038 }
Harald Welte0113fe72004-01-06 19:04:02 +00002039
Rusty Russelle45c7132004-12-16 13:21:44 +00002040 list_for_each_entry(i, &c->rules, list) {
Rusty Russell733e54b2004-12-16 14:22:23 +00002041 unsigned char *mask;
Harald Weltefe537072004-08-30 20:28:53 +00002042
Rusty Russell733e54b2004-12-16 14:22:23 +00002043 mask = is_same(r->entry, i->entry, matchmask);
2044 if (!mask)
2045 continue;
Martin Josefsson2a5dbbb2004-09-22 21:37:41 +00002046
Rusty Russell733e54b2004-12-16 14:22:23 +00002047 if (!target_same(r, i, mask))
2048 continue;
2049
2050 /* If we are about to delete the rule that is the
2051 * current iterator, move rule iterator back. next
2052 * pointer will then point to real next node */
2053 if (i == (*handle)->rule_iterator_cur) {
2054 (*handle)->rule_iterator_cur =
2055 list_entry((*handle)->rule_iterator_cur->list.prev,
2056 struct rule_head, list);
Marc Bouchere6869a82000-03-20 06:03:29 +00002057 }
Rusty Russell733e54b2004-12-16 14:22:23 +00002058
2059 c->num_rules--;
2060 iptcc_delete_rule(i);
2061
2062 set_changed(*handle);
2063 free(r);
2064 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00002065 }
2066
Rusty Russelle45c7132004-12-16 13:21:44 +00002067 free(r);
Marc Bouchere6869a82000-03-20 06:03:29 +00002068 errno = ENOENT;
2069 return 0;
Rusty Russell7e53bf92000-03-20 07:03:28 +00002070}
Harald Welteaae69be2004-08-29 23:32:14 +00002071
Marc Bouchere6869a82000-03-20 06:03:29 +00002072
2073/* Delete the rule in position `rulenum' in `chain'. */
2074int
Rusty Russell79dee072000-05-02 16:45:16 +00002075TC_DELETE_NUM_ENTRY(const IPT_CHAINLABEL chain,
2076 unsigned int rulenum,
Jan Engelhardtfd187312008-11-10 16:59:27 +01002077 struct xtc_handle **handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00002078{
Harald Welteaae69be2004-08-29 23:32:14 +00002079 struct chain_head *c;
2080 struct rule_head *r;
Marc Bouchere6869a82000-03-20 06:03:29 +00002081
Rusty Russell79dee072000-05-02 16:45:16 +00002082 iptc_fn = TC_DELETE_NUM_ENTRY;
Harald Welteaae69be2004-08-29 23:32:14 +00002083
2084 if (!(c = iptcc_find_label(chain, *handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002085 errno = ENOENT;
2086 return 0;
2087 }
2088
Martin Josefssona5616dc2004-10-24 22:27:31 +00002089 if (rulenum >= c->num_rules) {
Martin Josefsson631f3612004-09-23 19:25:06 +00002090 errno = E2BIG;
2091 return 0;
2092 }
2093
2094 /* Take advantage of the double linked list if possible. */
Martin Josefssona5616dc2004-10-24 22:27:31 +00002095 if (rulenum + 1 <= c->num_rules/2) {
2096 r = iptcc_get_rule_num(c, rulenum + 1);
2097 } else {
2098 r = iptcc_get_rule_num_reverse(c, c->num_rules - rulenum);
Marc Bouchere6869a82000-03-20 06:03:29 +00002099 }
2100
Harald Welteaae69be2004-08-29 23:32:14 +00002101 /* If we are about to delete the rule that is the current
2102 * iterator, move rule iterator back. next pointer will then
2103 * point to real next node */
2104 if (r == (*handle)->rule_iterator_cur) {
2105 (*handle)->rule_iterator_cur =
2106 list_entry((*handle)->rule_iterator_cur->list.prev,
2107 struct rule_head, list);
Harald Welte0113fe72004-01-06 19:04:02 +00002108 }
Marc Bouchere6869a82000-03-20 06:03:29 +00002109
Harald Welteaae69be2004-08-29 23:32:14 +00002110 c->num_rules--;
2111 iptcc_delete_rule(r);
2112
Martin Josefsson2a5dbbb2004-09-22 21:37:41 +00002113 set_changed(*handle);
2114
Harald Welteaae69be2004-08-29 23:32:14 +00002115 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00002116}
2117
2118/* Check the packet `fw' on chain `chain'. Returns the verdict, or
2119 NULL and sets errno. */
2120const char *
Rusty Russell79dee072000-05-02 16:45:16 +00002121TC_CHECK_PACKET(const IPT_CHAINLABEL chain,
2122 STRUCT_ENTRY *entry,
Jan Engelhardtfd187312008-11-10 16:59:27 +01002123 struct xtc_handle **handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00002124{
Derrik Pates664c0a32005-02-01 13:28:14 +00002125 iptc_fn = TC_CHECK_PACKET;
Marc Bouchere6869a82000-03-20 06:03:29 +00002126 errno = ENOSYS;
2127 return NULL;
2128}
2129
2130/* Flushes the entries in the given chain (ie. empties chain). */
2131int
Jan Engelhardtfd187312008-11-10 16:59:27 +01002132TC_FLUSH_ENTRIES(const IPT_CHAINLABEL chain, struct xtc_handle **handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00002133{
Harald Welteaae69be2004-08-29 23:32:14 +00002134 struct chain_head *c;
2135 struct rule_head *r, *tmp;
Marc Bouchere6869a82000-03-20 06:03:29 +00002136
Harald Welte0113fe72004-01-06 19:04:02 +00002137 iptc_fn = TC_FLUSH_ENTRIES;
Harald Welteaae69be2004-08-29 23:32:14 +00002138 if (!(c = iptcc_find_label(chain, *handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002139 errno = ENOENT;
2140 return 0;
2141 }
Marc Bouchere6869a82000-03-20 06:03:29 +00002142
Harald Welteaae69be2004-08-29 23:32:14 +00002143 list_for_each_entry_safe(r, tmp, &c->rules, list) {
2144 iptcc_delete_rule(r);
2145 }
2146
2147 c->num_rules = 0;
2148
2149 set_changed(*handle);
2150
2151 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00002152}
2153
2154/* Zeroes the counters in a chain. */
2155int
Jan Engelhardtfd187312008-11-10 16:59:27 +01002156TC_ZERO_ENTRIES(const IPT_CHAINLABEL chain, struct xtc_handle **handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00002157{
Harald Welteaae69be2004-08-29 23:32:14 +00002158 struct chain_head *c;
2159 struct rule_head *r;
Rusty Russell7e53bf92000-03-20 07:03:28 +00002160
Derrik Pates664c0a32005-02-01 13:28:14 +00002161 iptc_fn = TC_ZERO_ENTRIES;
Harald Welteaae69be2004-08-29 23:32:14 +00002162 if (!(c = iptcc_find_label(chain, *handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002163 errno = ENOENT;
2164 return 0;
2165 }
Marc Bouchere6869a82000-03-20 06:03:29 +00002166
Andy Gaye5bd1d72006-08-22 02:56:41 +00002167 if (c->counter_map.maptype == COUNTER_MAP_NORMAL_MAP)
2168 c->counter_map.maptype = COUNTER_MAP_ZEROED;
2169
Harald Welteaae69be2004-08-29 23:32:14 +00002170 list_for_each_entry(r, &c->rules, list) {
2171 if (r->counter_map.maptype == COUNTER_MAP_NORMAL_MAP)
2172 r->counter_map.maptype = COUNTER_MAP_ZEROED;
Marc Bouchere6869a82000-03-20 06:03:29 +00002173 }
Harald Welteaae69be2004-08-29 23:32:14 +00002174
Rusty Russell175f6412000-03-24 09:32:20 +00002175 set_changed(*handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00002176
Marc Bouchere6869a82000-03-20 06:03:29 +00002177 return 1;
2178}
2179
Harald Welte1cef74d2001-01-05 15:22:59 +00002180STRUCT_COUNTERS *
2181TC_READ_COUNTER(const IPT_CHAINLABEL chain,
2182 unsigned int rulenum,
Jan Engelhardtfd187312008-11-10 16:59:27 +01002183 struct xtc_handle **handle)
Harald Welte1cef74d2001-01-05 15:22:59 +00002184{
Harald Welteaae69be2004-08-29 23:32:14 +00002185 struct chain_head *c;
2186 struct rule_head *r;
Harald Welte1cef74d2001-01-05 15:22:59 +00002187
2188 iptc_fn = TC_READ_COUNTER;
2189 CHECK(*handle);
2190
Harald Welteaae69be2004-08-29 23:32:14 +00002191 if (!(c = iptcc_find_label(chain, *handle))) {
Harald Welte1cef74d2001-01-05 15:22:59 +00002192 errno = ENOENT;
2193 return NULL;
2194 }
2195
Harald Welteaae69be2004-08-29 23:32:14 +00002196 if (!(r = iptcc_get_rule_num(c, rulenum))) {
Harald Welte0113fe72004-01-06 19:04:02 +00002197 errno = E2BIG;
2198 return NULL;
2199 }
2200
Harald Welteaae69be2004-08-29 23:32:14 +00002201 return &r->entry[0].counters;
Harald Welte1cef74d2001-01-05 15:22:59 +00002202}
2203
2204int
2205TC_ZERO_COUNTER(const IPT_CHAINLABEL chain,
2206 unsigned int rulenum,
Jan Engelhardtfd187312008-11-10 16:59:27 +01002207 struct xtc_handle **handle)
Harald Welte1cef74d2001-01-05 15:22:59 +00002208{
Harald Welteaae69be2004-08-29 23:32:14 +00002209 struct chain_head *c;
2210 struct rule_head *r;
Harald Welte1cef74d2001-01-05 15:22:59 +00002211
2212 iptc_fn = TC_ZERO_COUNTER;
2213 CHECK(*handle);
2214
Harald Welteaae69be2004-08-29 23:32:14 +00002215 if (!(c = iptcc_find_label(chain, *handle))) {
Harald Welte1cef74d2001-01-05 15:22:59 +00002216 errno = ENOENT;
2217 return 0;
2218 }
2219
Harald Welteaae69be2004-08-29 23:32:14 +00002220 if (!(r = iptcc_get_rule_num(c, rulenum))) {
Harald Welte0113fe72004-01-06 19:04:02 +00002221 errno = E2BIG;
2222 return 0;
2223 }
2224
Harald Welteaae69be2004-08-29 23:32:14 +00002225 if (r->counter_map.maptype == COUNTER_MAP_NORMAL_MAP)
2226 r->counter_map.maptype = COUNTER_MAP_ZEROED;
Harald Welte1cef74d2001-01-05 15:22:59 +00002227
2228 set_changed(*handle);
2229
2230 return 1;
2231}
2232
2233int
2234TC_SET_COUNTER(const IPT_CHAINLABEL chain,
2235 unsigned int rulenum,
2236 STRUCT_COUNTERS *counters,
Jan Engelhardtfd187312008-11-10 16:59:27 +01002237 struct xtc_handle **handle)
Harald Welte1cef74d2001-01-05 15:22:59 +00002238{
Harald Welteaae69be2004-08-29 23:32:14 +00002239 struct chain_head *c;
2240 struct rule_head *r;
Harald Welte1cef74d2001-01-05 15:22:59 +00002241 STRUCT_ENTRY *e;
Harald Welte1cef74d2001-01-05 15:22:59 +00002242
2243 iptc_fn = TC_SET_COUNTER;
2244 CHECK(*handle);
2245
Harald Welteaae69be2004-08-29 23:32:14 +00002246 if (!(c = iptcc_find_label(chain, *handle))) {
Harald Welte1cef74d2001-01-05 15:22:59 +00002247 errno = ENOENT;
2248 return 0;
2249 }
Harald Welte0113fe72004-01-06 19:04:02 +00002250
Harald Welteaae69be2004-08-29 23:32:14 +00002251 if (!(r = iptcc_get_rule_num(c, rulenum))) {
Harald Welte0113fe72004-01-06 19:04:02 +00002252 errno = E2BIG;
2253 return 0;
2254 }
2255
Harald Welteaae69be2004-08-29 23:32:14 +00002256 e = r->entry;
2257 r->counter_map.maptype = COUNTER_MAP_SET;
Harald Welte0113fe72004-01-06 19:04:02 +00002258
2259 memcpy(&e->counters, counters, sizeof(STRUCT_COUNTERS));
Harald Welte1cef74d2001-01-05 15:22:59 +00002260
2261 set_changed(*handle);
2262
2263 return 1;
2264}
2265
Marc Bouchere6869a82000-03-20 06:03:29 +00002266/* Creates a new chain. */
2267/* To create a chain, create two rules: error node and unconditional
2268 * return. */
2269int
Jan Engelhardtfd187312008-11-10 16:59:27 +01002270TC_CREATE_CHAIN(const IPT_CHAINLABEL chain, struct xtc_handle **handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00002271{
Harald Welteaae69be2004-08-29 23:32:14 +00002272 static struct chain_head *c;
Patrick McHardy1f23d3c2008-06-07 15:04:34 +02002273 int capacity;
2274 int exceeded;
Marc Bouchere6869a82000-03-20 06:03:29 +00002275
Rusty Russell79dee072000-05-02 16:45:16 +00002276 iptc_fn = TC_CREATE_CHAIN;
Marc Bouchere6869a82000-03-20 06:03:29 +00002277
2278 /* find_label doesn't cover built-in targets: DROP, ACCEPT,
2279 QUEUE, RETURN. */
Harald Welteaae69be2004-08-29 23:32:14 +00002280 if (iptcc_find_label(chain, *handle)
Rusty Russell79dee072000-05-02 16:45:16 +00002281 || strcmp(chain, LABEL_DROP) == 0
2282 || strcmp(chain, LABEL_ACCEPT) == 0
Rusty Russell67088e72000-05-10 01:18:57 +00002283 || strcmp(chain, LABEL_QUEUE) == 0
Rusty Russell79dee072000-05-02 16:45:16 +00002284 || strcmp(chain, LABEL_RETURN) == 0) {
Harald Welteaae69be2004-08-29 23:32:14 +00002285 DEBUGP("Chain `%s' already exists\n", chain);
Marc Bouchere6869a82000-03-20 06:03:29 +00002286 errno = EEXIST;
2287 return 0;
2288 }
2289
Rusty Russell79dee072000-05-02 16:45:16 +00002290 if (strlen(chain)+1 > sizeof(IPT_CHAINLABEL)) {
Harald Welteaae69be2004-08-29 23:32:14 +00002291 DEBUGP("Chain name `%s' too long\n", chain);
Marc Bouchere6869a82000-03-20 06:03:29 +00002292 errno = EINVAL;
2293 return 0;
2294 }
2295
Harald Welteaae69be2004-08-29 23:32:14 +00002296 c = iptcc_alloc_chain_head(chain, 0);
2297 if (!c) {
2298 DEBUGP("Cannot allocate memory for chain `%s'\n", chain);
2299 errno = ENOMEM;
2300 return 0;
Marc Bouchere6869a82000-03-20 06:03:29 +00002301
Harald Welteaae69be2004-08-29 23:32:14 +00002302 }
Jesper Dangaard Brouer48bde402008-01-15 17:06:48 +00002303 (*handle)->num_chains++; /* New user defined chain */
Marc Bouchere6869a82000-03-20 06:03:29 +00002304
Harald Welteaae69be2004-08-29 23:32:14 +00002305 DEBUGP("Creating chain `%s'\n", chain);
Jesper Dangaard Brouerd8cb7872007-11-28 08:40:26 +00002306 iptc_insert_chain(*handle, c); /* Insert sorted */
Harald Welte0113fe72004-01-06 19:04:02 +00002307
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +00002308 /* Inserting chains don't change the correctness of the chain
2309 * index (except if its smaller than index[0], but that
2310 * handled by iptc_insert_chain). It only causes longer lists
2311 * in the buckets. Thus, only rebuild chain index when the
2312 * capacity is exceed with CHAIN_INDEX_INSERT_MAX chains.
2313 */
Patrick McHardy1f23d3c2008-06-07 15:04:34 +02002314 capacity = (*handle)->chain_index_sz * CHAIN_INDEX_BUCKET_LEN;
2315 exceeded = ((((*handle)->num_chains)-capacity));
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +00002316 if (exceeded > CHAIN_INDEX_INSERT_MAX) {
2317 debug("Capacity(%d) exceeded(%d) rebuild (chains:%d)\n",
2318 capacity, exceeded, (*handle)->num_chains);
2319 iptcc_chain_index_rebuild(*handle);
2320 }
2321
Harald Welte0113fe72004-01-06 19:04:02 +00002322 set_changed(*handle);
2323
Harald Welteaae69be2004-08-29 23:32:14 +00002324 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00002325}
2326
2327/* Get the number of references to this chain. */
2328int
Rusty Russell79dee072000-05-02 16:45:16 +00002329TC_GET_REFERENCES(unsigned int *ref, const IPT_CHAINLABEL chain,
Jan Engelhardtfd187312008-11-10 16:59:27 +01002330 struct xtc_handle **handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00002331{
Harald Welteaae69be2004-08-29 23:32:14 +00002332 struct chain_head *c;
Marc Bouchere6869a82000-03-20 06:03:29 +00002333
Derrik Pates664c0a32005-02-01 13:28:14 +00002334 iptc_fn = TC_GET_REFERENCES;
Harald Welteaae69be2004-08-29 23:32:14 +00002335 if (!(c = iptcc_find_label(chain, *handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002336 errno = ENOENT;
2337 return 0;
2338 }
2339
Harald Welteaae69be2004-08-29 23:32:14 +00002340 *ref = c->references;
2341
Marc Bouchere6869a82000-03-20 06:03:29 +00002342 return 1;
2343}
2344
2345/* Deletes a chain. */
2346int
Jan Engelhardtfd187312008-11-10 16:59:27 +01002347TC_DELETE_CHAIN(const IPT_CHAINLABEL chain, struct xtc_handle **handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00002348{
Marc Bouchere6869a82000-03-20 06:03:29 +00002349 unsigned int references;
Harald Welteaae69be2004-08-29 23:32:14 +00002350 struct chain_head *c;
Rusty Russell7e53bf92000-03-20 07:03:28 +00002351
Rusty Russell79dee072000-05-02 16:45:16 +00002352 iptc_fn = TC_DELETE_CHAIN;
Marc Bouchere6869a82000-03-20 06:03:29 +00002353
Harald Welteaae69be2004-08-29 23:32:14 +00002354 if (!(c = iptcc_find_label(chain, *handle))) {
2355 DEBUGP("cannot find chain `%s'\n", chain);
Marc Bouchere6869a82000-03-20 06:03:29 +00002356 errno = ENOENT;
2357 return 0;
2358 }
2359
Harald Welteaae69be2004-08-29 23:32:14 +00002360 if (TC_BUILTIN(chain, *handle)) {
2361 DEBUGP("cannot remove builtin chain `%s'\n", chain);
2362 errno = EINVAL;
2363 return 0;
2364 }
2365
2366 if (!TC_GET_REFERENCES(&references, chain, handle)) {
2367 DEBUGP("cannot get references on chain `%s'\n", chain);
2368 return 0;
2369 }
2370
2371 if (references > 0) {
2372 DEBUGP("chain `%s' still has references\n", chain);
2373 errno = EMLINK;
2374 return 0;
2375 }
2376
2377 if (c->num_rules) {
2378 DEBUGP("chain `%s' is not empty\n", chain);
Marc Bouchere6869a82000-03-20 06:03:29 +00002379 errno = ENOTEMPTY;
2380 return 0;
2381 }
2382
Harald Welteaae69be2004-08-29 23:32:14 +00002383 /* If we are about to delete the chain that is the current
Jesper Dangaard Brouer48bde402008-01-15 17:06:48 +00002384 * iterator, move chain iterator forward. */
Harald Welteaae69be2004-08-29 23:32:14 +00002385 if (c == (*handle)->chain_iterator_cur)
2386 iptcc_chain_iterator_advance(*handle);
Harald Welte0113fe72004-01-06 19:04:02 +00002387
Jesper Dangaard Brouer48bde402008-01-15 17:06:48 +00002388 (*handle)->num_chains--; /* One user defined chain deleted */
2389
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +00002390 //list_del(&c->list); /* Done in iptcc_chain_index_delete_chain() */
2391 iptcc_chain_index_delete_chain(c, *handle);
2392 free(c);
2393
Harald Welteaae69be2004-08-29 23:32:14 +00002394 DEBUGP("chain `%s' deleted\n", chain);
2395
2396 set_changed(*handle);
2397
2398 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00002399}
2400
2401/* Renames a chain. */
Rusty Russell79dee072000-05-02 16:45:16 +00002402int TC_RENAME_CHAIN(const IPT_CHAINLABEL oldname,
2403 const IPT_CHAINLABEL newname,
Jan Engelhardtfd187312008-11-10 16:59:27 +01002404 struct xtc_handle **handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00002405{
Harald Welteaae69be2004-08-29 23:32:14 +00002406 struct chain_head *c;
Rusty Russell79dee072000-05-02 16:45:16 +00002407 iptc_fn = TC_RENAME_CHAIN;
Marc Bouchere6869a82000-03-20 06:03:29 +00002408
Harald Welte1de80462000-10-30 12:00:27 +00002409 /* find_label doesn't cover built-in targets: DROP, ACCEPT,
2410 QUEUE, RETURN. */
Harald Welteaae69be2004-08-29 23:32:14 +00002411 if (iptcc_find_label(newname, *handle)
Rusty Russell79dee072000-05-02 16:45:16 +00002412 || strcmp(newname, LABEL_DROP) == 0
2413 || strcmp(newname, LABEL_ACCEPT) == 0
Harald Welte1de80462000-10-30 12:00:27 +00002414 || strcmp(newname, LABEL_QUEUE) == 0
Rusty Russell79dee072000-05-02 16:45:16 +00002415 || strcmp(newname, LABEL_RETURN) == 0) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002416 errno = EEXIST;
2417 return 0;
2418 }
2419
Harald Welteaae69be2004-08-29 23:32:14 +00002420 if (!(c = iptcc_find_label(oldname, *handle))
Rusty Russell79dee072000-05-02 16:45:16 +00002421 || TC_BUILTIN(oldname, *handle)) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002422 errno = ENOENT;
2423 return 0;
2424 }
2425
Rusty Russell79dee072000-05-02 16:45:16 +00002426 if (strlen(newname)+1 > sizeof(IPT_CHAINLABEL)) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002427 errno = EINVAL;
2428 return 0;
2429 }
2430
Harald Welteaae69be2004-08-29 23:32:14 +00002431 strncpy(c->name, newname, sizeof(IPT_CHAINLABEL));
2432
Harald Welte0113fe72004-01-06 19:04:02 +00002433 set_changed(*handle);
2434
Marc Bouchere6869a82000-03-20 06:03:29 +00002435 return 1;
2436}
2437
2438/* Sets the policy on a built-in chain. */
2439int
Rusty Russell79dee072000-05-02 16:45:16 +00002440TC_SET_POLICY(const IPT_CHAINLABEL chain,
2441 const IPT_CHAINLABEL policy,
Harald Welte1cef74d2001-01-05 15:22:59 +00002442 STRUCT_COUNTERS *counters,
Jan Engelhardtfd187312008-11-10 16:59:27 +01002443 struct xtc_handle **handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00002444{
Harald Welteaae69be2004-08-29 23:32:14 +00002445 struct chain_head *c;
Marc Bouchere6869a82000-03-20 06:03:29 +00002446
Rusty Russell79dee072000-05-02 16:45:16 +00002447 iptc_fn = TC_SET_POLICY;
Marc Bouchere6869a82000-03-20 06:03:29 +00002448
Harald Welteaae69be2004-08-29 23:32:14 +00002449 if (!(c = iptcc_find_label(chain, *handle))) {
2450 DEBUGP("cannot find chain `%s'\n", chain);
2451 errno = ENOENT;
Marc Bouchere6869a82000-03-20 06:03:29 +00002452 return 0;
2453 }
2454
Harald Welteaae69be2004-08-29 23:32:14 +00002455 if (!iptcc_is_builtin(c)) {
2456 DEBUGP("cannot set policy of userdefinedchain `%s'\n", chain);
2457 errno = ENOENT;
2458 return 0;
2459 }
Marc Bouchere6869a82000-03-20 06:03:29 +00002460
Rusty Russell79dee072000-05-02 16:45:16 +00002461 if (strcmp(policy, LABEL_ACCEPT) == 0)
Harald Welteaae69be2004-08-29 23:32:14 +00002462 c->verdict = -NF_ACCEPT - 1;
Rusty Russell79dee072000-05-02 16:45:16 +00002463 else if (strcmp(policy, LABEL_DROP) == 0)
Harald Welteaae69be2004-08-29 23:32:14 +00002464 c->verdict = -NF_DROP - 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00002465 else {
2466 errno = EINVAL;
2467 return 0;
2468 }
Harald Welte1cef74d2001-01-05 15:22:59 +00002469
Harald Welte1cef74d2001-01-05 15:22:59 +00002470 if (counters) {
2471 /* set byte and packet counters */
Harald Welteaae69be2004-08-29 23:32:14 +00002472 memcpy(&c->counters, counters, sizeof(STRUCT_COUNTERS));
2473 c->counter_map.maptype = COUNTER_MAP_SET;
Harald Welte1cef74d2001-01-05 15:22:59 +00002474 } else {
Harald Welteaae69be2004-08-29 23:32:14 +00002475 c->counter_map.maptype = COUNTER_MAP_NOMAP;
Harald Welte1cef74d2001-01-05 15:22:59 +00002476 }
2477
Rusty Russell175f6412000-03-24 09:32:20 +00002478 set_changed(*handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00002479
Marc Bouchere6869a82000-03-20 06:03:29 +00002480 return 1;
2481}
2482
2483/* Without this, on gcc 2.7.2.3, we get:
Rusty Russell79dee072000-05-02 16:45:16 +00002484 libiptc.c: In function `TC_COMMIT':
Marc Bouchere6869a82000-03-20 06:03:29 +00002485 libiptc.c:833: fixed or forbidden register was spilled.
2486 This may be due to a compiler bug or to impossible asm
2487 statements or clauses.
2488*/
2489static void
Rusty Russell79dee072000-05-02 16:45:16 +00002490subtract_counters(STRUCT_COUNTERS *answer,
2491 const STRUCT_COUNTERS *a,
2492 const STRUCT_COUNTERS *b)
Marc Bouchere6869a82000-03-20 06:03:29 +00002493{
2494 answer->pcnt = a->pcnt - b->pcnt;
2495 answer->bcnt = a->bcnt - b->bcnt;
2496}
2497
Harald Welteaae69be2004-08-29 23:32:14 +00002498
Jan Engelhardtdbb77542008-02-11 00:33:30 +01002499static void counters_nomap(STRUCT_COUNTERS_INFO *newcounters, unsigned int idx)
Harald Welteaae69be2004-08-29 23:32:14 +00002500{
Jan Engelhardtdbb77542008-02-11 00:33:30 +01002501 newcounters->counters[idx] = ((STRUCT_COUNTERS) { 0, 0});
Harald Welteaae69be2004-08-29 23:32:14 +00002502 DEBUGP_C("NOMAP => zero\n");
2503}
2504
2505static void counters_normal_map(STRUCT_COUNTERS_INFO *newcounters,
Jan Engelhardtdbb77542008-02-11 00:33:30 +01002506 STRUCT_REPLACE *repl, unsigned int idx,
Harald Welteaae69be2004-08-29 23:32:14 +00002507 unsigned int mappos)
2508{
2509 /* Original read: X.
2510 * Atomic read on replacement: X + Y.
2511 * Currently in kernel: Z.
2512 * Want in kernel: X + Y + Z.
2513 * => Add in X + Y
2514 * => Add in replacement read.
2515 */
Jan Engelhardtdbb77542008-02-11 00:33:30 +01002516 newcounters->counters[idx] = repl->counters[mappos];
Harald Welteaae69be2004-08-29 23:32:14 +00002517 DEBUGP_C("NORMAL_MAP => mappos %u \n", mappos);
2518}
2519
2520static void counters_map_zeroed(STRUCT_COUNTERS_INFO *newcounters,
Jan Engelhardtdbb77542008-02-11 00:33:30 +01002521 STRUCT_REPLACE *repl, unsigned int idx,
2522 unsigned int mappos, STRUCT_COUNTERS *counters)
Harald Welteaae69be2004-08-29 23:32:14 +00002523{
2524 /* Original read: X.
2525 * Atomic read on replacement: X + Y.
2526 * Currently in kernel: Z.
2527 * Want in kernel: Y + Z.
2528 * => Add in Y.
2529 * => Add in (replacement read - original read).
2530 */
Jan Engelhardtdbb77542008-02-11 00:33:30 +01002531 subtract_counters(&newcounters->counters[idx],
Harald Welteaae69be2004-08-29 23:32:14 +00002532 &repl->counters[mappos],
2533 counters);
2534 DEBUGP_C("ZEROED => mappos %u\n", mappos);
2535}
2536
2537static void counters_map_set(STRUCT_COUNTERS_INFO *newcounters,
Jan Engelhardtdbb77542008-02-11 00:33:30 +01002538 unsigned int idx, STRUCT_COUNTERS *counters)
Harald Welteaae69be2004-08-29 23:32:14 +00002539{
2540 /* Want to set counter (iptables-restore) */
2541
Jan Engelhardtdbb77542008-02-11 00:33:30 +01002542 memcpy(&newcounters->counters[idx], counters,
Harald Welteaae69be2004-08-29 23:32:14 +00002543 sizeof(STRUCT_COUNTERS));
2544
2545 DEBUGP_C("SET\n");
2546}
2547
2548
Marc Bouchere6869a82000-03-20 06:03:29 +00002549int
Jan Engelhardtfd187312008-11-10 16:59:27 +01002550TC_COMMIT(struct xtc_handle **handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00002551{
2552 /* Replace, then map back the counters. */
Rusty Russell79dee072000-05-02 16:45:16 +00002553 STRUCT_REPLACE *repl;
2554 STRUCT_COUNTERS_INFO *newcounters;
Harald Welteaae69be2004-08-29 23:32:14 +00002555 struct chain_head *c;
2556 int ret;
Martin Josefsson841e4ae2003-05-02 15:30:11 +00002557 size_t counterlen;
Harald Welteaae69be2004-08-29 23:32:14 +00002558 int new_number;
2559 unsigned int new_size;
Marc Bouchere6869a82000-03-20 06:03:29 +00002560
Derrik Pates664c0a32005-02-01 13:28:14 +00002561 iptc_fn = TC_COMMIT;
Marc Bouchere6869a82000-03-20 06:03:29 +00002562 CHECK(*handle);
Martin Josefsson841e4ae2003-05-02 15:30:11 +00002563
Marc Bouchere6869a82000-03-20 06:03:29 +00002564 /* Don't commit if nothing changed. */
2565 if (!(*handle)->changed)
2566 goto finished;
2567
Harald Welteaae69be2004-08-29 23:32:14 +00002568 new_number = iptcc_compile_table_prep(*handle, &new_size);
2569 if (new_number < 0) {
2570 errno = ENOMEM;
Harald Welted6ba6f52005-11-12 10:39:40 +00002571 goto out_zero;
Harald Welteaae69be2004-08-29 23:32:14 +00002572 }
2573
2574 repl = malloc(sizeof(*repl) + new_size);
Marc Bouchere6869a82000-03-20 06:03:29 +00002575 if (!repl) {
2576 errno = ENOMEM;
Harald Welted6ba6f52005-11-12 10:39:40 +00002577 goto out_zero;
Marc Bouchere6869a82000-03-20 06:03:29 +00002578 }
Martin Josefssonad3b4f92004-09-22 22:04:07 +00002579 memset(repl, 0, sizeof(*repl) + new_size);
Harald Welteaae69be2004-08-29 23:32:14 +00002580
Rusty Russelle45c7132004-12-16 13:21:44 +00002581#if 0
2582 TC_DUMP_ENTRIES(*handle);
2583#endif
2584
Harald Welteaae69be2004-08-29 23:32:14 +00002585 counterlen = sizeof(STRUCT_COUNTERS_INFO)
2586 + sizeof(STRUCT_COUNTERS) * new_number;
Marc Bouchere6869a82000-03-20 06:03:29 +00002587
2588 /* These are the old counters we will get from kernel */
Rusty Russell79dee072000-05-02 16:45:16 +00002589 repl->counters = malloc(sizeof(STRUCT_COUNTERS)
Marc Bouchere6869a82000-03-20 06:03:29 +00002590 * (*handle)->info.num_entries);
2591 if (!repl->counters) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002592 errno = ENOMEM;
Harald Welted6ba6f52005-11-12 10:39:40 +00002593 goto out_free_repl;
Marc Bouchere6869a82000-03-20 06:03:29 +00002594 }
Marc Bouchere6869a82000-03-20 06:03:29 +00002595 /* These are the counters we're going to put back, later. */
2596 newcounters = malloc(counterlen);
2597 if (!newcounters) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002598 errno = ENOMEM;
Harald Welted6ba6f52005-11-12 10:39:40 +00002599 goto out_free_repl_counters;
Marc Bouchere6869a82000-03-20 06:03:29 +00002600 }
Harald Welteaae69be2004-08-29 23:32:14 +00002601 memset(newcounters, 0, counterlen);
Marc Bouchere6869a82000-03-20 06:03:29 +00002602
2603 strcpy(repl->name, (*handle)->info.name);
Harald Welteaae69be2004-08-29 23:32:14 +00002604 repl->num_entries = new_number;
2605 repl->size = new_size;
2606
Marc Bouchere6869a82000-03-20 06:03:29 +00002607 repl->num_counters = (*handle)->info.num_entries;
2608 repl->valid_hooks = (*handle)->info.valid_hooks;
Harald Welteaae69be2004-08-29 23:32:14 +00002609
2610 DEBUGP("num_entries=%u, size=%u, num_counters=%u\n",
2611 repl->num_entries, repl->size, repl->num_counters);
2612
2613 ret = iptcc_compile_table(*handle, repl);
2614 if (ret < 0) {
2615 errno = ret;
Harald Welted6ba6f52005-11-12 10:39:40 +00002616 goto out_free_newcounters;
Harald Welteaae69be2004-08-29 23:32:14 +00002617 }
2618
2619
2620#ifdef IPTC_DEBUG2
2621 {
2622 int fd = open("/tmp/libiptc-so_set_replace.blob",
2623 O_CREAT|O_WRONLY);
2624 if (fd >= 0) {
2625 write(fd, repl, sizeof(*repl) + repl->size);
2626 close(fd);
2627 }
2628 }
2629#endif
Marc Bouchere6869a82000-03-20 06:03:29 +00002630
Harald Welted6ba6f52005-11-12 10:39:40 +00002631 ret = setsockopt(sockfd, TC_IPPROTO, SO_SET_REPLACE, repl,
2632 sizeof(*repl) + repl->size);
Patrick McHardye0865ad2006-04-22 02:08:56 +00002633 if (ret < 0)
Harald Welted6ba6f52005-11-12 10:39:40 +00002634 goto out_free_newcounters;
Marc Bouchere6869a82000-03-20 06:03:29 +00002635
2636 /* Put counters back. */
2637 strcpy(newcounters->name, (*handle)->info.name);
Harald Welteaae69be2004-08-29 23:32:14 +00002638 newcounters->num_counters = new_number;
Marc Bouchere6869a82000-03-20 06:03:29 +00002639
Harald Welteaae69be2004-08-29 23:32:14 +00002640 list_for_each_entry(c, &(*handle)->chains, list) {
2641 struct rule_head *r;
Marc Bouchere6869a82000-03-20 06:03:29 +00002642
Harald Welteaae69be2004-08-29 23:32:14 +00002643 /* Builtin chains have their own counters */
2644 if (iptcc_is_builtin(c)) {
2645 DEBUGP("counter for chain-index %u: ", c->foot_index);
2646 switch(c->counter_map.maptype) {
2647 case COUNTER_MAP_NOMAP:
2648 counters_nomap(newcounters, c->foot_index);
2649 break;
2650 case COUNTER_MAP_NORMAL_MAP:
2651 counters_normal_map(newcounters, repl,
2652 c->foot_index,
2653 c->counter_map.mappos);
2654 break;
2655 case COUNTER_MAP_ZEROED:
2656 counters_map_zeroed(newcounters, repl,
2657 c->foot_index,
2658 c->counter_map.mappos,
2659 &c->counters);
2660 break;
2661 case COUNTER_MAP_SET:
2662 counters_map_set(newcounters, c->foot_index,
2663 &c->counters);
2664 break;
2665 }
2666 }
Harald Welte1cef74d2001-01-05 15:22:59 +00002667
Harald Welteaae69be2004-08-29 23:32:14 +00002668 list_for_each_entry(r, &c->rules, list) {
2669 DEBUGP("counter for index %u: ", r->index);
2670 switch (r->counter_map.maptype) {
2671 case COUNTER_MAP_NOMAP:
2672 counters_nomap(newcounters, r->index);
2673 break;
Harald Welte1cef74d2001-01-05 15:22:59 +00002674
Harald Welteaae69be2004-08-29 23:32:14 +00002675 case COUNTER_MAP_NORMAL_MAP:
2676 counters_normal_map(newcounters, repl,
2677 r->index,
2678 r->counter_map.mappos);
2679 break;
Harald Welte1cef74d2001-01-05 15:22:59 +00002680
Harald Welteaae69be2004-08-29 23:32:14 +00002681 case COUNTER_MAP_ZEROED:
2682 counters_map_zeroed(newcounters, repl,
2683 r->index,
2684 r->counter_map.mappos,
2685 &r->entry->counters);
2686 break;
2687
2688 case COUNTER_MAP_SET:
2689 counters_map_set(newcounters, r->index,
2690 &r->entry->counters);
2691 break;
2692 }
Marc Bouchere6869a82000-03-20 06:03:29 +00002693 }
2694 }
Rusty Russell62527ce2000-09-04 09:45:54 +00002695
Harald Welteaae69be2004-08-29 23:32:14 +00002696#ifdef IPTC_DEBUG2
2697 {
2698 int fd = open("/tmp/libiptc-so_set_add_counters.blob",
2699 O_CREAT|O_WRONLY);
2700 if (fd >= 0) {
2701 write(fd, newcounters, counterlen);
2702 close(fd);
2703 }
2704 }
2705#endif
2706
Harald Welted6ba6f52005-11-12 10:39:40 +00002707 ret = setsockopt(sockfd, TC_IPPROTO, SO_SET_ADD_COUNTERS,
2708 newcounters, counterlen);
Patrick McHardye0865ad2006-04-22 02:08:56 +00002709 if (ret < 0)
Harald Welted6ba6f52005-11-12 10:39:40 +00002710 goto out_free_newcounters;
Marc Bouchere6869a82000-03-20 06:03:29 +00002711
2712 free(repl->counters);
2713 free(repl);
2714 free(newcounters);
2715
Harald Welted6ba6f52005-11-12 10:39:40 +00002716finished:
Martin Josefsson841e4ae2003-05-02 15:30:11 +00002717 TC_FREE(handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00002718 return 1;
Harald Welted6ba6f52005-11-12 10:39:40 +00002719
2720out_free_newcounters:
2721 free(newcounters);
2722out_free_repl_counters:
2723 free(repl->counters);
2724out_free_repl:
2725 free(repl);
2726out_zero:
2727 return 0;
Marc Bouchere6869a82000-03-20 06:03:29 +00002728}
2729
2730/* Get raw socket. */
2731int
Patrick McHardy0b639362007-09-08 16:00:01 +00002732TC_GET_RAW_SOCKET(void)
Marc Bouchere6869a82000-03-20 06:03:29 +00002733{
2734 return sockfd;
2735}
2736
2737/* Translates errno numbers into more human-readable form than strerror. */
2738const char *
Rusty Russell79dee072000-05-02 16:45:16 +00002739TC_STRERROR(int err)
Marc Bouchere6869a82000-03-20 06:03:29 +00002740{
2741 unsigned int i;
2742 struct table_struct {
2743 void *fn;
2744 int err;
2745 const char *message;
2746 } table [] =
Harald Welte4ccfa632001-07-30 15:12:43 +00002747 { { TC_INIT, EPERM, "Permission denied (you must be root)" },
Rusty Russell79dee072000-05-02 16:45:16 +00002748 { TC_INIT, EINVAL, "Module is wrong version" },
Harald Welte4ccfa632001-07-30 15:12:43 +00002749 { TC_INIT, ENOENT,
2750 "Table does not exist (do you need to insmod?)" },
Rusty Russell79dee072000-05-02 16:45:16 +00002751 { TC_DELETE_CHAIN, ENOTEMPTY, "Chain is not empty" },
2752 { TC_DELETE_CHAIN, EINVAL, "Can't delete built-in chain" },
2753 { TC_DELETE_CHAIN, EMLINK,
Marc Bouchere6869a82000-03-20 06:03:29 +00002754 "Can't delete chain with references left" },
Rusty Russell79dee072000-05-02 16:45:16 +00002755 { TC_CREATE_CHAIN, EEXIST, "Chain already exists" },
2756 { TC_INSERT_ENTRY, E2BIG, "Index of insertion too big" },
2757 { TC_REPLACE_ENTRY, E2BIG, "Index of replacement too big" },
2758 { TC_DELETE_NUM_ENTRY, E2BIG, "Index of deletion too big" },
Harald Welte1cef74d2001-01-05 15:22:59 +00002759 { TC_READ_COUNTER, E2BIG, "Index of counter too big" },
2760 { TC_ZERO_COUNTER, E2BIG, "Index of counter too big" },
Rusty Russell79dee072000-05-02 16:45:16 +00002761 { TC_INSERT_ENTRY, ELOOP, "Loop found in table" },
2762 { TC_INSERT_ENTRY, EINVAL, "Target problem" },
Marc Bouchere6869a82000-03-20 06:03:29 +00002763 /* EINVAL for CHECK probably means bad interface. */
Rusty Russell79dee072000-05-02 16:45:16 +00002764 { TC_CHECK_PACKET, EINVAL,
Marc Boucherc8264992000-04-22 22:34:44 +00002765 "Bad arguments (does that interface exist?)" },
Harald Welte4ccfa632001-07-30 15:12:43 +00002766 { TC_CHECK_PACKET, ENOSYS,
2767 "Checking will most likely never get implemented" },
Marc Bouchere6869a82000-03-20 06:03:29 +00002768 /* ENOENT for DELETE probably means no matching rule */
Rusty Russell79dee072000-05-02 16:45:16 +00002769 { TC_DELETE_ENTRY, ENOENT,
Marc Boucherc8264992000-04-22 22:34:44 +00002770 "Bad rule (does a matching rule exist in that chain?)" },
Rusty Russell79dee072000-05-02 16:45:16 +00002771 { TC_SET_POLICY, ENOENT,
Marc Boucherc8264992000-04-22 22:34:44 +00002772 "Bad built-in chain name" },
Rusty Russell79dee072000-05-02 16:45:16 +00002773 { TC_SET_POLICY, EINVAL,
Marc Boucherc8264992000-04-22 22:34:44 +00002774 "Bad policy name" },
Harald Welte4ccfa632001-07-30 15:12:43 +00002775
2776 { NULL, 0, "Incompatible with this kernel" },
2777 { NULL, ENOPROTOOPT, "iptables who? (do you need to insmod?)" },
2778 { NULL, ENOSYS, "Will be implemented real soon. I promise ;)" },
2779 { NULL, ENOMEM, "Memory allocation problem" },
2780 { NULL, ENOENT, "No chain/target/match by that name" },
Marc Bouchere6869a82000-03-20 06:03:29 +00002781 };
2782
2783 for (i = 0; i < sizeof(table)/sizeof(struct table_struct); i++) {
2784 if ((!table[i].fn || table[i].fn == iptc_fn)
2785 && table[i].err == err)
2786 return table[i].message;
2787 }
2788
2789 return strerror(err);
2790}