blob: 544a5b294064d365997aad67af640ea1a087ec02 [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 void *iptc_fn = NULL;
51
Patrick McHardy0b639362007-09-08 16:00:01 +000052static const char *hooknames[] = {
53 [HOOK_PRE_ROUTING] = "PREROUTING",
54 [HOOK_LOCAL_IN] = "INPUT",
55 [HOOK_FORWARD] = "FORWARD",
56 [HOOK_LOCAL_OUT] = "OUTPUT",
57 [HOOK_POST_ROUTING] = "POSTROUTING",
Rusty Russell10758b72000-09-14 07:37:33 +000058#ifdef HOOK_DROPPING
Patrick McHardy0b639362007-09-08 16:00:01 +000059 [HOOK_DROPPING] = "DROPPING"
Rusty Russell10758b72000-09-14 07:37:33 +000060#endif
Marc Bouchere6869a82000-03-20 06:03:29 +000061};
62
Harald Welteaae69be2004-08-29 23:32:14 +000063/* Convenience structures */
64struct ipt_error_target
65{
66 STRUCT_ENTRY_TARGET t;
67 char error[TABLE_MAXNAMELEN];
68};
69
70struct chain_head;
71struct rule_head;
72
Marc Bouchere6869a82000-03-20 06:03:29 +000073struct counter_map
74{
75 enum {
76 COUNTER_MAP_NOMAP,
77 COUNTER_MAP_NORMAL_MAP,
Harald Welte1cef74d2001-01-05 15:22:59 +000078 COUNTER_MAP_ZEROED,
79 COUNTER_MAP_SET
Marc Bouchere6869a82000-03-20 06:03:29 +000080 } maptype;
81 unsigned int mappos;
82};
83
Harald Welteaae69be2004-08-29 23:32:14 +000084enum iptcc_rule_type {
85 IPTCC_R_STANDARD, /* standard target (ACCEPT, ...) */
86 IPTCC_R_MODULE, /* extension module (SNAT, ...) */
87 IPTCC_R_FALLTHROUGH, /* fallthrough rule */
88 IPTCC_R_JUMP, /* jump to other chain */
Marc Bouchere6869a82000-03-20 06:03:29 +000089};
90
Harald Welteaae69be2004-08-29 23:32:14 +000091struct rule_head
Rusty Russell30fd6e52000-04-23 09:16:06 +000092{
Harald Welteaae69be2004-08-29 23:32:14 +000093 struct list_head list;
94 struct chain_head *chain;
95 struct counter_map counter_map;
96
97 unsigned int index; /* index (needed for counter_map) */
98 unsigned int offset; /* offset in rule blob */
99
100 enum iptcc_rule_type type;
101 struct chain_head *jump; /* jump target, if IPTCC_R_JUMP */
102
103 unsigned int size; /* size of entry data */
104 STRUCT_ENTRY entry[0];
105};
106
107struct chain_head
108{
109 struct list_head list;
Rusty Russell79dee072000-05-02 16:45:16 +0000110 char name[TABLE_MAXNAMELEN];
Harald Welteaae69be2004-08-29 23:32:14 +0000111 unsigned int hooknum; /* hook number+1 if builtin */
112 unsigned int references; /* how many jumps reference us */
113 int verdict; /* verdict if builtin */
114
115 STRUCT_COUNTERS counters; /* per-chain counters */
116 struct counter_map counter_map;
117
118 unsigned int num_rules; /* number of rules in list */
119 struct list_head rules; /* list of rules */
120
121 unsigned int index; /* index (needed for jump resolval) */
122 unsigned int head_offset; /* offset in rule blob */
123 unsigned int foot_index; /* index (needed for counter_map) */
124 unsigned int foot_offset; /* offset in rule blob */
Rusty Russell30fd6e52000-04-23 09:16:06 +0000125};
126
Rusty Russell79dee072000-05-02 16:45:16 +0000127STRUCT_TC_HANDLE
Marc Bouchere6869a82000-03-20 06:03:29 +0000128{
Jan Engelhardt175f4512008-11-10 17:25:55 +0100129 int sockfd;
Harald Welteaae69be2004-08-29 23:32:14 +0000130 int changed; /* Have changes been made? */
131
132 struct list_head chains;
133
134 struct chain_head *chain_iterator_cur;
135 struct rule_head *rule_iterator_cur;
136
Jesper Dangaard Brouer48bde402008-01-15 17:06:48 +0000137 unsigned int num_chains; /* number of user defined chains */
138
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000139 struct chain_head **chain_index; /* array for fast chain list access*/
140 unsigned int chain_index_sz;/* size of chain index array */
141
Jesper Dangaard Brouer4bae3f12008-07-03 20:31:42 +0200142 int sorted_offsets; /* if chains are received sorted from kernel,
143 * then the offsets are also sorted. Says if its
144 * possible to bsearch offsets using chain_index.
145 */
146
Rusty Russell79dee072000-05-02 16:45:16 +0000147 STRUCT_GETINFO info;
Harald Welteaae69be2004-08-29 23:32:14 +0000148 STRUCT_GET_ENTRIES *entries;
Marc Bouchere6869a82000-03-20 06:03:29 +0000149};
150
Jesper Dangaard Brouer4bae3f12008-07-03 20:31:42 +0200151enum bsearch_type {
152 BSEARCH_NAME, /* Binary search after chain name */
153 BSEARCH_OFFSET, /* Binary search based on offset */
154};
155
Harald Welteaae69be2004-08-29 23:32:14 +0000156/* allocate a new chain head for the cache */
157static struct chain_head *iptcc_alloc_chain_head(const char *name, int hooknum)
158{
159 struct chain_head *c = malloc(sizeof(*c));
160 if (!c)
161 return NULL;
162 memset(c, 0, sizeof(*c));
163
164 strncpy(c->name, name, TABLE_MAXNAMELEN);
165 c->hooknum = hooknum;
166 INIT_LIST_HEAD(&c->rules);
167
168 return c;
169}
170
171/* allocate and initialize a new rule for the cache */
172static struct rule_head *iptcc_alloc_rule(struct chain_head *c, unsigned int size)
173{
174 struct rule_head *r = malloc(sizeof(*r)+size);
175 if (!r)
176 return NULL;
177 memset(r, 0, sizeof(*r));
178
179 r->chain = c;
180 r->size = size;
181
182 return r;
183}
184
185/* notify us that the ruleset has been modified by the user */
Jesper Dangaard Brouer91093982008-01-15 17:01:58 +0000186static inline void
Jan Engelhardtfd187312008-11-10 16:59:27 +0100187set_changed(struct xtc_handle *h)
Rusty Russell175f6412000-03-24 09:32:20 +0000188{
Rusty Russell175f6412000-03-24 09:32:20 +0000189 h->changed = 1;
190}
191
Harald Welte380ba5f2002-02-13 16:19:55 +0000192#ifdef IPTC_DEBUG
Jan Engelhardtfd187312008-11-10 16:59:27 +0100193static void do_check(struct xtc_handle *h, unsigned int line);
Rusty Russell849779c2000-04-23 15:51:51 +0000194#define CHECK(h) do { if (!getenv("IPTC_NO_CHECK")) do_check((h), __LINE__); } while(0)
Rusty Russell30fd6e52000-04-23 09:16:06 +0000195#else
196#define CHECK(h)
197#endif
Marc Bouchere6869a82000-03-20 06:03:29 +0000198
Harald Welteaae69be2004-08-29 23:32:14 +0000199
200/**********************************************************************
201 * iptc blob utility functions (iptcb_*)
202 **********************************************************************/
203
Marc Bouchere6869a82000-03-20 06:03:29 +0000204static inline int
Harald Welteaae69be2004-08-29 23:32:14 +0000205iptcb_get_number(const STRUCT_ENTRY *i,
Rusty Russell79dee072000-05-02 16:45:16 +0000206 const STRUCT_ENTRY *seek,
Marc Bouchere6869a82000-03-20 06:03:29 +0000207 unsigned int *pos)
208{
209 if (i == seek)
210 return 1;
211 (*pos)++;
212 return 0;
213}
214
Marc Bouchere6869a82000-03-20 06:03:29 +0000215static inline int
Harald Welteaae69be2004-08-29 23:32:14 +0000216iptcb_get_entry_n(STRUCT_ENTRY *i,
Marc Bouchere6869a82000-03-20 06:03:29 +0000217 unsigned int number,
218 unsigned int *pos,
Rusty Russell79dee072000-05-02 16:45:16 +0000219 STRUCT_ENTRY **pe)
Marc Bouchere6869a82000-03-20 06:03:29 +0000220{
221 if (*pos == number) {
222 *pe = i;
223 return 1;
224 }
225 (*pos)++;
226 return 0;
227}
228
Harald Welteaae69be2004-08-29 23:32:14 +0000229static inline STRUCT_ENTRY *
Jan Engelhardtfd187312008-11-10 16:59:27 +0100230iptcb_get_entry(struct xtc_handle *h, unsigned int offset)
Harald Welteaae69be2004-08-29 23:32:14 +0000231{
232 return (STRUCT_ENTRY *)((char *)h->entries->entrytable + offset);
233}
234
235static unsigned int
Jan Engelhardtfd187312008-11-10 16:59:27 +0100236iptcb_entry2index(struct xtc_handle *const h, const STRUCT_ENTRY *seek)
Marc Bouchere6869a82000-03-20 06:03:29 +0000237{
238 unsigned int pos = 0;
Marc Bouchere6869a82000-03-20 06:03:29 +0000239
Harald Welteaae69be2004-08-29 23:32:14 +0000240 if (ENTRY_ITERATE(h->entries->entrytable, h->entries->size,
241 iptcb_get_number, seek, &pos) == 0) {
242 fprintf(stderr, "ERROR: offset %u not an entry!\n",
243 (unsigned int)((char *)seek - (char *)h->entries->entrytable));
244 abort();
245 }
246 return pos;
Marc Bouchere6869a82000-03-20 06:03:29 +0000247}
248
Harald Welte0113fe72004-01-06 19:04:02 +0000249static inline STRUCT_ENTRY *
Jan Engelhardtfd187312008-11-10 16:59:27 +0100250iptcb_offset2entry(struct xtc_handle *h, unsigned int offset)
Harald Welte0113fe72004-01-06 19:04:02 +0000251{
Harald Welteaae69be2004-08-29 23:32:14 +0000252 return (STRUCT_ENTRY *) ((void *)h->entries->entrytable+offset);
Harald Welte0113fe72004-01-06 19:04:02 +0000253}
254
Harald Welteaae69be2004-08-29 23:32:14 +0000255
Harald Welte0113fe72004-01-06 19:04:02 +0000256static inline unsigned long
Jan Engelhardtfd187312008-11-10 16:59:27 +0100257iptcb_entry2offset(struct xtc_handle *const h, const STRUCT_ENTRY *e)
Harald Welte0113fe72004-01-06 19:04:02 +0000258{
Harald Welteaae69be2004-08-29 23:32:14 +0000259 return (void *)e - (void *)h->entries->entrytable;
Harald Welte3ea8f402003-06-23 18:25:59 +0000260}
261
262static inline unsigned int
Jan Engelhardtfd187312008-11-10 16:59:27 +0100263iptcb_offset2index(struct xtc_handle *const h, unsigned int offset)
Harald Welte3ea8f402003-06-23 18:25:59 +0000264{
Harald Welteaae69be2004-08-29 23:32:14 +0000265 return iptcb_entry2index(h, iptcb_offset2entry(h, offset));
266}
267
268/* Returns 0 if not hook entry, else hooknumber + 1 */
269static inline unsigned int
Jan Engelhardtfd187312008-11-10 16:59:27 +0100270iptcb_ent_is_hook_entry(STRUCT_ENTRY *e, struct xtc_handle *h)
Harald Welteaae69be2004-08-29 23:32:14 +0000271{
272 unsigned int i;
273
274 for (i = 0; i < NUMHOOKS; i++) {
275 if ((h->info.valid_hooks & (1 << i))
276 && iptcb_get_entry(h, h->info.hook_entry[i]) == e)
277 return i+1;
278 }
279 return 0;
Harald Welte3ea8f402003-06-23 18:25:59 +0000280}
281
282
Harald Welteaae69be2004-08-29 23:32:14 +0000283/**********************************************************************
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000284 * Chain index (cache utility) functions
285 **********************************************************************
286 * The chain index is an array with pointers into the chain list, with
287 * CHAIN_INDEX_BUCKET_LEN spacing. This facilitates the ability to
288 * speedup chain list searching, by find a more optimal starting
289 * points when searching the linked list.
290 *
291 * The starting point can be found fast by using a binary search of
292 * the chain index. Thus, reducing the previous search complexity of
293 * O(n) to O(log(n/k) + k) where k is CHAIN_INDEX_BUCKET_LEN.
294 *
295 * A nice property of the chain index, is that the "bucket" list
296 * length is max CHAIN_INDEX_BUCKET_LEN (when just build, inserts will
297 * change this). Oppose to hashing, where the "bucket" list length can
298 * vary a lot.
299 */
300#ifndef CHAIN_INDEX_BUCKET_LEN
301#define CHAIN_INDEX_BUCKET_LEN 40
302#endif
303
304/* Another nice property of the chain index is that inserting/creating
305 * chains in chain list don't change the correctness of the chain
306 * index, it only causes longer lists in the buckets.
307 *
308 * To mitigate the performance penalty of longer bucket lists and the
309 * penalty of rebuilding, the chain index is rebuild only when
310 * CHAIN_INDEX_INSERT_MAX chains has been added.
311 */
312#ifndef CHAIN_INDEX_INSERT_MAX
313#define CHAIN_INDEX_INSERT_MAX 355
314#endif
315
316static inline unsigned int iptcc_is_builtin(struct chain_head *c);
317
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000318/* Use binary search in the chain index array, to find a chain_head
319 * pointer closest to the place of the searched name element.
320 *
321 * Notes that, binary search (obviously) requires that the chain list
322 * is sorted by name.
Jesper Dangaard Brouer4bae3f12008-07-03 20:31:42 +0200323 *
324 * The not so obvious: The chain index array, is actually both sorted
325 * by name and offset, at the same time!. This is only true because,
326 * chain are stored sorted in the kernel (as we pushed it in sorted).
327 *
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000328 */
329static struct list_head *
Jesper Dangaard Brouer4bae3f12008-07-03 20:31:42 +0200330__iptcc_bsearch_chain_index(const char *name, unsigned int offset,
Jan Engelhardtfd187312008-11-10 16:59:27 +0100331 unsigned int *idx, struct xtc_handle *handle,
Jesper Dangaard Brouer4bae3f12008-07-03 20:31:42 +0200332 enum bsearch_type type)
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000333{
334 unsigned int pos, end;
335 int res;
336
337 struct list_head *list_pos;
338 list_pos=&handle->chains;
339
340 /* Check for empty array, e.g. no user defined chains */
341 if (handle->chain_index_sz == 0) {
342 debug("WARNING: handle->chain_index_sz == 0\n");
343 return list_pos;
344 }
345
346 /* Init */
347 end = handle->chain_index_sz;
348 pos = end / 2;
349
Jesper Dangaard Brouer4bae3f12008-07-03 20:31:42 +0200350 debug("bsearch Find chain:%s (pos:%d end:%d) (offset:%d)\n",
351 name, pos, end, offset);
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000352
353 /* Loop */
354 loop:
355 if (!handle->chain_index[pos]) {
356 fprintf(stderr, "ERROR: NULL pointer chain_index[%d]\n", pos);
357 return &handle->chains; /* Be safe, return orig start pos */
358 }
359
Jesper Dangaard Brouer4bae3f12008-07-03 20:31:42 +0200360 debug("bsearch Index[%d] name:%s ",
361 pos, handle->chain_index[pos]->name);
362
363 /* Support for different compare functions */
364 switch (type) {
365 case BSEARCH_NAME:
366 res = strcmp(name, handle->chain_index[pos]->name);
367 break;
368 case BSEARCH_OFFSET:
369 debug("head_offset:[%d] foot_offset:[%d] ",
370 handle->chain_index[pos]->head_offset,
371 handle->chain_index[pos]->foot_offset);
372 res = offset - handle->chain_index[pos]->head_offset;
373 break;
374 default:
375 fprintf(stderr, "ERROR: %d not a valid bsearch type\n",
376 type);
377 abort();
378 break;
379 }
380 debug("res:%d ", res);
381
382
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000383 list_pos = &handle->chain_index[pos]->list;
Jan Engelhardtdbb77542008-02-11 00:33:30 +0100384 *idx = pos;
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000385
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000386 if (res == 0) { /* Found element, by direct hit */
387 debug("[found] Direct hit pos:%d end:%d\n", pos, end);
388 return list_pos;
389 } else if (res < 0) { /* Too far, jump back */
390 end = pos;
391 pos = pos / 2;
392
393 /* Exit case: First element of array */
394 if (end == 0) {
395 debug("[found] Reached first array elem (end%d)\n",end);
396 return list_pos;
397 }
398 debug("jump back to pos:%d (end:%d)\n", pos, end);
399 goto loop;
400 } else if (res > 0 ){ /* Not far enough, jump forward */
401
402 /* Exit case: Last element of array */
403 if (pos == handle->chain_index_sz-1) {
404 debug("[found] Last array elem (end:%d)\n", end);
405 return list_pos;
406 }
407
408 /* Exit case: Next index less, thus elem in this list section */
Jesper Dangaard Brouer4bae3f12008-07-03 20:31:42 +0200409 switch (type) {
410 case BSEARCH_NAME:
411 res = strcmp(name, handle->chain_index[pos+1]->name);
412 break;
413 case BSEARCH_OFFSET:
414 res = offset - handle->chain_index[pos+1]->head_offset;
415 break;
416 }
417
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000418 if (res < 0) {
419 debug("[found] closest list (end:%d)\n", end);
420 return list_pos;
421 }
422
423 pos = (pos+end)/2;
424 debug("jump forward to pos:%d (end:%d)\n", pos, end);
425 goto loop;
426 }
427
428 return list_pos;
429}
430
Jesper Dangaard Brouer4bae3f12008-07-03 20:31:42 +0200431/* Wrapper for string chain name based bsearch */
432static struct list_head *
433iptcc_bsearch_chain_index(const char *name, unsigned int *idx,
Jan Engelhardtfd187312008-11-10 16:59:27 +0100434 struct xtc_handle *handle)
Jesper Dangaard Brouer4bae3f12008-07-03 20:31:42 +0200435{
436 return __iptcc_bsearch_chain_index(name, 0, idx, handle, BSEARCH_NAME);
437}
438
439
440/* Wrapper for offset chain based bsearch */
441static struct list_head *
442iptcc_bsearch_chain_offset(unsigned int offset, unsigned int *idx,
Jan Engelhardtfd187312008-11-10 16:59:27 +0100443 struct xtc_handle *handle)
Jesper Dangaard Brouer4bae3f12008-07-03 20:31:42 +0200444{
445 struct list_head *pos;
446
447 /* If chains were not received sorted from kernel, then the
448 * offset bsearch is not possible.
449 */
450 if (!handle->sorted_offsets)
451 pos = handle->chains.next;
452 else
453 pos = __iptcc_bsearch_chain_index(NULL, offset, idx, handle,
454 BSEARCH_OFFSET);
455 return pos;
456}
457
458
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000459#ifdef DEBUG
460/* Trivial linear search of chain index. Function used for verifying
461 the output of bsearch function */
462static struct list_head *
Jan Engelhardtfd187312008-11-10 16:59:27 +0100463iptcc_linearly_search_chain_index(const char *name, struct xtc_handle *handle)
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000464{
465 unsigned int i=0;
466 int res=0;
467
468 struct list_head *list_pos;
469 list_pos = &handle->chains;
470
471 if (handle->chain_index_sz)
472 list_pos = &handle->chain_index[0]->list;
473
474 /* Linearly walk of chain index array */
475
476 for (i=0; i < handle->chain_index_sz; i++) {
477 if (handle->chain_index[i]) {
478 res = strcmp(handle->chain_index[i]->name, name);
479 if (res > 0)
480 break; // One step too far
481 list_pos = &handle->chain_index[i]->list;
482 if (res == 0)
483 break; // Direct hit
484 }
485 }
486
487 return list_pos;
488}
489#endif
490
Jan Engelhardtfd187312008-11-10 16:59:27 +0100491static int iptcc_chain_index_alloc(struct xtc_handle *h)
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000492{
493 unsigned int list_length = CHAIN_INDEX_BUCKET_LEN;
494 unsigned int array_elems;
495 unsigned int array_mem;
496
497 /* Allocate memory for the chain index array */
498 array_elems = (h->num_chains / list_length) +
499 (h->num_chains % list_length ? 1 : 0);
500 array_mem = sizeof(h->chain_index) * array_elems;
501
502 debug("Alloc Chain index, elems:%d mem:%d bytes\n",
503 array_elems, array_mem);
504
505 h->chain_index = malloc(array_mem);
Jan Engelhardt0eee3002008-11-26 17:18:08 +0100506 if (h->chain_index == NULL && array_mem > 0) {
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000507 h->chain_index_sz = 0;
508 return -ENOMEM;
509 }
510 memset(h->chain_index, 0, array_mem);
511 h->chain_index_sz = array_elems;
512
513 return 1;
514}
515
Jan Engelhardtfd187312008-11-10 16:59:27 +0100516static void iptcc_chain_index_free(struct xtc_handle *h)
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000517{
518 h->chain_index_sz = 0;
519 free(h->chain_index);
520}
521
522
523#ifdef DEBUG
Jan Engelhardtfd187312008-11-10 16:59:27 +0100524static void iptcc_chain_index_dump(struct xtc_handle *h)
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000525{
526 unsigned int i = 0;
527
528 /* Dump: contents of chain index array */
529 for (i=0; i < h->chain_index_sz; i++) {
530 if (h->chain_index[i]) {
531 fprintf(stderr, "Chain index[%d].name: %s\n",
532 i, h->chain_index[i]->name);
533 }
534 }
535}
536#endif
537
538/* Build the chain index */
Jan Engelhardtfd187312008-11-10 16:59:27 +0100539static int iptcc_chain_index_build(struct xtc_handle *h)
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000540{
541 unsigned int list_length = CHAIN_INDEX_BUCKET_LEN;
542 unsigned int chains = 0;
543 unsigned int cindex = 0;
544 struct chain_head *c;
545
546 /* Build up the chain index array here */
547 debug("Building chain index\n");
548
549 debug("Number of user defined chains:%d bucket_sz:%d array_sz:%d\n",
550 h->num_chains, list_length, h->chain_index_sz);
551
552 if (h->chain_index_sz == 0)
553 return 0;
554
555 list_for_each_entry(c, &h->chains, list) {
556
557 /* Issue: The index array needs to start after the
558 * builtin chains, as they are not sorted */
559 if (!iptcc_is_builtin(c)) {
560 cindex=chains / list_length;
561
562 /* Safe guard, break out on array limit, this
563 * is useful if chains are added and array is
564 * rebuild, without realloc of memory. */
565 if (cindex >= h->chain_index_sz)
566 break;
567
568 if ((chains % list_length)== 0) {
569 debug("\nIndex[%d] Chains:", cindex);
570 h->chain_index[cindex] = c;
571 }
572 chains++;
573 }
574 debug("%s, ", c->name);
575 }
576 debug("\n");
577
578 return 1;
579}
580
Jan Engelhardtfd187312008-11-10 16:59:27 +0100581static int iptcc_chain_index_rebuild(struct xtc_handle *h)
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000582{
583 debug("REBUILD chain index array\n");
584 iptcc_chain_index_free(h);
585 if ((iptcc_chain_index_alloc(h)) < 0)
586 return -ENOMEM;
587 iptcc_chain_index_build(h);
588 return 1;
589}
590
591/* Delete chain (pointer) from index array. Removing an element from
592 * the chain list only affects the chain index array, if the chain
593 * index points-to/uses that list pointer.
594 *
595 * There are different strategies, the simple and safe is to rebuild
596 * the chain index every time. The more advanced is to update the
597 * array index to point to the next element, but that requires some
598 * house keeping and boundry checks. The advanced is implemented, as
599 * the simple approach behaves badly when all chains are deleted
600 * because list_for_each processing will always hit the first chain
601 * index, thus causing a rebuild for every chain.
602 */
Jan Engelhardtfd187312008-11-10 16:59:27 +0100603static int iptcc_chain_index_delete_chain(struct chain_head *c, struct xtc_handle *h)
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000604{
605 struct list_head *index_ptr, *index_ptr2, *next;
606 struct chain_head *c2;
Jan Engelhardtdbb77542008-02-11 00:33:30 +0100607 unsigned int idx, idx2;
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000608
Jan Engelhardtdbb77542008-02-11 00:33:30 +0100609 index_ptr = iptcc_bsearch_chain_index(c->name, &idx, h);
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000610
611 debug("Del chain[%s] c->list:%p index_ptr:%p\n",
612 c->name, &c->list, index_ptr);
613
614 /* Save the next pointer */
615 next = c->list.next;
616 list_del(&c->list);
617
618 if (index_ptr == &c->list) { /* Chain used as index ptr */
619
620 /* See if its possible to avoid a rebuild, by shifting
621 * to next pointer. Its possible if the next pointer
622 * is located in the same index bucket.
623 */
624 c2 = list_entry(next, struct chain_head, list);
Jan Engelhardtdbb77542008-02-11 00:33:30 +0100625 index_ptr2 = iptcc_bsearch_chain_index(c2->name, &idx2, h);
626 if (idx != idx2) {
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000627 /* Rebuild needed */
628 return iptcc_chain_index_rebuild(h);
629 } else {
630 /* Avoiding rebuild */
631 debug("Update cindex[%d] with next ptr name:[%s]\n",
Jan Engelhardtdbb77542008-02-11 00:33:30 +0100632 idx, c2->name);
633 h->chain_index[idx]=c2;
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000634 return 0;
635 }
636 }
637 return 0;
638}
639
640
641/**********************************************************************
Harald Welteaae69be2004-08-29 23:32:14 +0000642 * iptc cache utility functions (iptcc_*)
643 **********************************************************************/
Harald Welte0113fe72004-01-06 19:04:02 +0000644
Harald Welteaae69be2004-08-29 23:32:14 +0000645/* Is the given chain builtin (1) or user-defined (0) */
Jesper Dangaard Brouer91093982008-01-15 17:01:58 +0000646static inline unsigned int iptcc_is_builtin(struct chain_head *c)
Harald Welteaae69be2004-08-29 23:32:14 +0000647{
648 return (c->hooknum ? 1 : 0);
649}
650
651/* Get a specific rule within a chain */
652static struct rule_head *iptcc_get_rule_num(struct chain_head *c,
653 unsigned int rulenum)
654{
655 struct rule_head *r;
656 unsigned int num = 0;
657
658 list_for_each_entry(r, &c->rules, list) {
659 num++;
660 if (num == rulenum)
661 return r;
662 }
663 return NULL;
664}
665
Martin Josefssona5616dc2004-10-24 22:27:31 +0000666/* Get a specific rule within a chain backwards */
667static struct rule_head *iptcc_get_rule_num_reverse(struct chain_head *c,
668 unsigned int rulenum)
669{
670 struct rule_head *r;
671 unsigned int num = 0;
672
673 list_for_each_entry_reverse(r, &c->rules, list) {
674 num++;
675 if (num == rulenum)
676 return r;
677 }
678 return NULL;
679}
680
Harald Welteaae69be2004-08-29 23:32:14 +0000681/* Returns chain head if found, otherwise NULL. */
682static struct chain_head *
Jan Engelhardtfd187312008-11-10 16:59:27 +0100683iptcc_find_chain_by_offset(struct xtc_handle *handle, unsigned int offset)
Harald Welteaae69be2004-08-29 23:32:14 +0000684{
685 struct list_head *pos;
Jesper Dangaard Brouer4bae3f12008-07-03 20:31:42 +0200686 struct list_head *list_start_pos;
687 unsigned int i;
Harald Welteaae69be2004-08-29 23:32:14 +0000688
689 if (list_empty(&handle->chains))
690 return NULL;
691
Jesper Dangaard Brouer4bae3f12008-07-03 20:31:42 +0200692 /* Find a smart place to start the search */
693 list_start_pos = iptcc_bsearch_chain_offset(offset, &i, handle);
694
695 /* Note that iptcc_bsearch_chain_offset() skips builtin
696 * chains, but this function is only used for finding jump
697 * targets, and a buildin chain is not a valid jump target */
698
699 debug("Offset:[%u] starting search at index:[%u]\n", offset, i);
700// list_for_each(pos, &handle->chains) {
701 list_for_each(pos, list_start_pos->prev) {
Harald Welteaae69be2004-08-29 23:32:14 +0000702 struct chain_head *c = list_entry(pos, struct chain_head, list);
Jesper Dangaard Brouer4bae3f12008-07-03 20:31:42 +0200703 debug(".");
704 if (offset >= c->head_offset && offset <= c->foot_offset) {
705 debug("Offset search found chain:[%s]\n", c->name);
Harald Welteaae69be2004-08-29 23:32:14 +0000706 return c;
Jesper Dangaard Brouer4bae3f12008-07-03 20:31:42 +0200707 }
Harald Welte0113fe72004-01-06 19:04:02 +0000708 }
709
Harald Welteaae69be2004-08-29 23:32:14 +0000710 return NULL;
Harald Welte0113fe72004-01-06 19:04:02 +0000711}
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000712
Harald Welteaae69be2004-08-29 23:32:14 +0000713/* Returns chain head if found, otherwise NULL. */
714static struct chain_head *
Jan Engelhardtfd187312008-11-10 16:59:27 +0100715iptcc_find_label(const char *name, struct xtc_handle *handle)
Harald Welteaae69be2004-08-29 23:32:14 +0000716{
717 struct list_head *pos;
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000718 struct list_head *list_start_pos;
719 unsigned int i=0;
720 int res;
Harald Welteaae69be2004-08-29 23:32:14 +0000721
722 if (list_empty(&handle->chains))
723 return NULL;
724
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000725 /* First look at builtin chains */
Harald Welteaae69be2004-08-29 23:32:14 +0000726 list_for_each(pos, &handle->chains) {
727 struct chain_head *c = list_entry(pos, struct chain_head, list);
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000728 if (!iptcc_is_builtin(c))
729 break;
Harald Welteaae69be2004-08-29 23:32:14 +0000730 if (!strcmp(c->name, name))
731 return c;
732 }
733
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000734 /* Find a smart place to start the search via chain index */
735 //list_start_pos = iptcc_linearly_search_chain_index(name, handle);
736 list_start_pos = iptcc_bsearch_chain_index(name, &i, handle);
737
738 /* Handel if bsearch bails out early */
739 if (list_start_pos == &handle->chains) {
740 list_start_pos = pos;
741 }
742#ifdef DEBUG
743 else {
744 /* Verify result of bsearch against linearly index search */
745 struct list_head *test_pos;
746 struct chain_head *test_c, *tmp_c;
747 test_pos = iptcc_linearly_search_chain_index(name, handle);
748 if (list_start_pos != test_pos) {
749 debug("BUG in chain_index search\n");
750 test_c=list_entry(test_pos, struct chain_head,list);
751 tmp_c =list_entry(list_start_pos,struct chain_head,list);
752 debug("Verify search found:\n");
753 debug(" Chain:%s\n", test_c->name);
754 debug("BSearch found:\n");
755 debug(" Chain:%s\n", tmp_c->name);
756 exit(42);
757 }
758 }
759#endif
760
761 /* Initial/special case, no user defined chains */
762 if (handle->num_chains == 0)
763 return NULL;
764
765 /* Start searching through the chain list */
766 list_for_each(pos, list_start_pos->prev) {
767 struct chain_head *c = list_entry(pos, struct chain_head, list);
768 res = strcmp(c->name, name);
769 debug("List search name:%s == %s res:%d\n", name, c->name, res);
770 if (res==0)
771 return c;
772
773 /* We can stop earlier as we know list is sorted */
774 if (res>0 && !iptcc_is_builtin(c)) { /* Walked too far*/
775 debug(" Not in list, walked too far, sorted list\n");
776 return NULL;
777 }
778
779 /* Stop on wrap around, if list head is reached */
780 if (pos == &handle->chains) {
781 debug("Stop, list head reached\n");
782 return NULL;
783 }
784 }
785
786 debug("List search NOT found name:%s\n", name);
Harald Welteaae69be2004-08-29 23:32:14 +0000787 return NULL;
788}
789
790/* called when rule is to be removed from cache */
791static void iptcc_delete_rule(struct rule_head *r)
792{
793 DEBUGP("deleting rule %p (offset %u)\n", r, r->offset);
794 /* clean up reference count of called chain */
795 if (r->type == IPTCC_R_JUMP
796 && r->jump)
797 r->jump->references--;
798
799 list_del(&r->list);
800 free(r);
801}
802
803
804/**********************************************************************
805 * RULESET PARSER (blob -> cache)
806 **********************************************************************/
807
Harald Welteaae69be2004-08-29 23:32:14 +0000808/* Delete policy rule of previous chain, since cache doesn't contain
809 * chain policy rules.
810 * WARNING: This function has ugly design and relies on a lot of context, only
811 * to be called from specific places within the parser */
Jan Engelhardtfd187312008-11-10 16:59:27 +0100812static int __iptcc_p_del_policy(struct xtc_handle *h, unsigned int num)
Harald Welteaae69be2004-08-29 23:32:14 +0000813{
814 if (h->chain_iterator_cur) {
815 /* policy rule is last rule */
816 struct rule_head *pr = (struct rule_head *)
817 h->chain_iterator_cur->rules.prev;
818
819 /* save verdict */
820 h->chain_iterator_cur->verdict =
821 *(int *)GET_TARGET(pr->entry)->data;
822
823 /* save counter and counter_map information */
824 h->chain_iterator_cur->counter_map.maptype =
825 COUNTER_MAP_NORMAL_MAP;
826 h->chain_iterator_cur->counter_map.mappos = num-1;
827 memcpy(&h->chain_iterator_cur->counters, &pr->entry->counters,
828 sizeof(h->chain_iterator_cur->counters));
829
830 /* foot_offset points to verdict rule */
831 h->chain_iterator_cur->foot_index = num;
832 h->chain_iterator_cur->foot_offset = pr->offset;
833
834 /* delete rule from cache */
835 iptcc_delete_rule(pr);
Martin Josefsson8d1b38a2004-09-22 21:00:19 +0000836 h->chain_iterator_cur->num_rules--;
Harald Welteaae69be2004-08-29 23:32:14 +0000837
838 return 1;
839 }
840 return 0;
841}
842
Harald Welteec30b6c2005-02-01 16:45:56 +0000843/* alphabetically insert a chain into the list */
Jan Engelhardtfd187312008-11-10 16:59:27 +0100844static inline void iptc_insert_chain(struct xtc_handle *h, struct chain_head *c)
Harald Welteec30b6c2005-02-01 16:45:56 +0000845{
846 struct chain_head *tmp;
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000847 struct list_head *list_start_pos;
848 unsigned int i=1;
849
850 /* Find a smart place to start the insert search */
851 list_start_pos = iptcc_bsearch_chain_index(c->name, &i, h);
852
853 /* Handle the case, where chain.name is smaller than index[0] */
854 if (i==0 && strcmp(c->name, h->chain_index[0]->name) <= 0) {
855 h->chain_index[0] = c; /* Update chain index head */
856 list_start_pos = h->chains.next;
857 debug("Update chain_index[0] with %s\n", c->name);
858 }
859
860 /* Handel if bsearch bails out early */
861 if (list_start_pos == &h->chains) {
862 list_start_pos = h->chains.next;
863 }
Harald Welteec30b6c2005-02-01 16:45:56 +0000864
Olaf Rempel9d3ed772005-03-04 23:08:30 +0000865 /* sort only user defined chains */
866 if (!c->hooknum) {
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000867 list_for_each_entry(tmp, list_start_pos->prev, list) {
Robert de Barthfeca0572005-07-31 07:04:59 +0000868 if (!tmp->hooknum && strcmp(c->name, tmp->name) <= 0) {
Olaf Rempel9d3ed772005-03-04 23:08:30 +0000869 list_add(&c->list, tmp->list.prev);
870 return;
871 }
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000872
873 /* Stop if list head is reached */
874 if (&tmp->list == &h->chains) {
875 debug("Insert, list head reached add to tail\n");
876 break;
877 }
Harald Welteec30b6c2005-02-01 16:45:56 +0000878 }
879 }
880
881 /* survived till end of list: add at tail */
882 list_add_tail(&c->list, &h->chains);
883}
884
Harald Welteaae69be2004-08-29 23:32:14 +0000885/* Another ugly helper function split out of cache_add_entry to make it less
886 * spaghetti code */
Jan Engelhardtfd187312008-11-10 16:59:27 +0100887static void __iptcc_p_add_chain(struct xtc_handle *h, struct chain_head *c,
Harald Welteaae69be2004-08-29 23:32:14 +0000888 unsigned int offset, unsigned int *num)
889{
Jesper Dangaard Brouer13364512007-12-12 15:20:42 +0000890 struct list_head *tail = h->chains.prev;
891 struct chain_head *ctail;
892
Harald Welteaae69be2004-08-29 23:32:14 +0000893 __iptcc_p_del_policy(h, *num);
894
895 c->head_offset = offset;
896 c->index = *num;
897
Jesper Dangaard Brouer13364512007-12-12 15:20:42 +0000898 /* Chains from kernel are already sorted, as they are inserted
899 * sorted. But there exists an issue when shifting to 1.4.0
900 * from an older version, as old versions allow last created
901 * chain to be unsorted.
902 */
903 if (iptcc_is_builtin(c)) /* Only user defined chains are sorted*/
904 list_add_tail(&c->list, &h->chains);
905 else {
906 ctail = list_entry(tail, struct chain_head, list);
Jesper Dangaard Brouer4bae3f12008-07-03 20:31:42 +0200907
Jesper Dangaard Brouer526d3e12008-07-03 20:29:34 +0200908 if (strcmp(c->name, ctail->name) > 0 ||
909 iptcc_is_builtin(ctail))
Jesper Dangaard Brouer13364512007-12-12 15:20:42 +0000910 list_add_tail(&c->list, &h->chains);/* Already sorted*/
Jesper Dangaard Brouer4bae3f12008-07-03 20:31:42 +0200911 else {
Jesper Dangaard Brouer13364512007-12-12 15:20:42 +0000912 iptc_insert_chain(h, c);/* Was not sorted */
Jesper Dangaard Brouer4bae3f12008-07-03 20:31:42 +0200913
914 /* Notice, if chains were not received sorted
915 * from kernel, then an offset bsearch is no
916 * longer valid.
917 */
918 h->sorted_offsets = 0;
919
920 debug("NOTICE: chain:[%s] was NOT sorted(ctail:%s)\n",
921 c->name, ctail->name);
922 }
Jesper Dangaard Brouer13364512007-12-12 15:20:42 +0000923 }
Jesper Dangaard Brouerd8cb7872007-11-28 08:40:26 +0000924
Harald Welteaae69be2004-08-29 23:32:14 +0000925 h->chain_iterator_cur = c;
926}
927
928/* main parser function: add an entry from the blob to the cache */
929static int cache_add_entry(STRUCT_ENTRY *e,
Jan Engelhardtfd187312008-11-10 16:59:27 +0100930 struct xtc_handle *h,
Harald Welteaae69be2004-08-29 23:32:14 +0000931 STRUCT_ENTRY **prev,
932 unsigned int *num)
933{
934 unsigned int builtin;
935 unsigned int offset = (char *)e - (char *)h->entries->entrytable;
936
937 DEBUGP("entering...");
938
939 /* Last entry ("policy rule"). End it.*/
940 if (iptcb_entry2offset(h,e) + e->next_offset == h->entries->size) {
941 /* This is the ERROR node at the end of the chain */
942 DEBUGP_C("%u:%u: end of table:\n", *num, offset);
943
944 __iptcc_p_del_policy(h, *num);
945
946 h->chain_iterator_cur = NULL;
947 goto out_inc;
948 }
949
950 /* We know this is the start of a new chain if it's an ERROR
951 * target, or a hook entry point */
952
953 if (strcmp(GET_TARGET(e)->u.user.name, ERROR_TARGET) == 0) {
954 struct chain_head *c =
955 iptcc_alloc_chain_head((const char *)GET_TARGET(e)->data, 0);
956 DEBUGP_C("%u:%u:new userdefined chain %s: %p\n", *num, offset,
957 (char *)c->name, c);
958 if (!c) {
959 errno = -ENOMEM;
960 return -1;
961 }
Jesper Dangaard Brouer48bde402008-01-15 17:06:48 +0000962 h->num_chains++; /* New user defined chain */
Harald Welteaae69be2004-08-29 23:32:14 +0000963
964 __iptcc_p_add_chain(h, c, offset, num);
965
966 } else if ((builtin = iptcb_ent_is_hook_entry(e, h)) != 0) {
967 struct chain_head *c =
968 iptcc_alloc_chain_head((char *)hooknames[builtin-1],
969 builtin);
970 DEBUGP_C("%u:%u new builtin chain: %p (rules=%p)\n",
971 *num, offset, c, &c->rules);
972 if (!c) {
973 errno = -ENOMEM;
974 return -1;
975 }
976
977 c->hooknum = builtin;
978
979 __iptcc_p_add_chain(h, c, offset, num);
980
981 /* FIXME: this is ugly. */
982 goto new_rule;
983 } else {
984 /* has to be normal rule */
985 struct rule_head *r;
986new_rule:
987
988 if (!(r = iptcc_alloc_rule(h->chain_iterator_cur,
989 e->next_offset))) {
990 errno = ENOMEM;
991 return -1;
992 }
993 DEBUGP_C("%u:%u normal rule: %p: ", *num, offset, r);
994
995 r->index = *num;
996 r->offset = offset;
997 memcpy(r->entry, e, e->next_offset);
998 r->counter_map.maptype = COUNTER_MAP_NORMAL_MAP;
999 r->counter_map.mappos = r->index;
1000
1001 /* handling of jumps, etc. */
1002 if (!strcmp(GET_TARGET(e)->u.user.name, STANDARD_TARGET)) {
1003 STRUCT_STANDARD_TARGET *t;
1004
1005 t = (STRUCT_STANDARD_TARGET *)GET_TARGET(e);
1006 if (t->target.u.target_size
1007 != ALIGN(sizeof(STRUCT_STANDARD_TARGET))) {
1008 errno = EINVAL;
1009 return -1;
1010 }
1011
1012 if (t->verdict < 0) {
1013 DEBUGP_C("standard, verdict=%d\n", t->verdict);
1014 r->type = IPTCC_R_STANDARD;
1015 } else if (t->verdict == r->offset+e->next_offset) {
1016 DEBUGP_C("fallthrough\n");
1017 r->type = IPTCC_R_FALLTHROUGH;
1018 } else {
1019 DEBUGP_C("jump, target=%u\n", t->verdict);
1020 r->type = IPTCC_R_JUMP;
1021 /* Jump target fixup has to be deferred
1022 * until second pass, since we migh not
1023 * yet have parsed the target */
1024 }
Martin Josefsson52c38022004-09-22 19:39:40 +00001025 } else {
1026 DEBUGP_C("module, target=%s\n", GET_TARGET(e)->u.user.name);
1027 r->type = IPTCC_R_MODULE;
Harald Welteaae69be2004-08-29 23:32:14 +00001028 }
1029
1030 list_add_tail(&r->list, &h->chain_iterator_cur->rules);
Martin Josefsson8d1b38a2004-09-22 21:00:19 +00001031 h->chain_iterator_cur->num_rules++;
Harald Welteaae69be2004-08-29 23:32:14 +00001032 }
1033out_inc:
1034 (*num)++;
1035 return 0;
1036}
1037
1038
1039/* parse an iptables blob into it's pieces */
Jan Engelhardtfd187312008-11-10 16:59:27 +01001040static int parse_table(struct xtc_handle *h)
Harald Welteaae69be2004-08-29 23:32:14 +00001041{
1042 STRUCT_ENTRY *prev;
1043 unsigned int num = 0;
1044 struct chain_head *c;
1045
Jesper Dangaard Brouer4bae3f12008-07-03 20:31:42 +02001046 /* Assume that chains offsets are sorted, this verified during
1047 parsing of ruleset (in __iptcc_p_add_chain())*/
1048 h->sorted_offsets = 1;
1049
Harald Welteaae69be2004-08-29 23:32:14 +00001050 /* First pass: over ruleset blob */
1051 ENTRY_ITERATE(h->entries->entrytable, h->entries->size,
1052 cache_add_entry, h, &prev, &num);
1053
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +00001054 /* Build the chain index, used for chain list search speedup */
1055 if ((iptcc_chain_index_alloc(h)) < 0)
1056 return -ENOMEM;
1057 iptcc_chain_index_build(h);
1058
Harald Welteaae69be2004-08-29 23:32:14 +00001059 /* Second pass: fixup parsed data from first pass */
1060 list_for_each_entry(c, &h->chains, list) {
1061 struct rule_head *r;
1062 list_for_each_entry(r, &c->rules, list) {
Jan Engelhardtdbb77542008-02-11 00:33:30 +01001063 struct chain_head *lc;
Harald Welteaae69be2004-08-29 23:32:14 +00001064 STRUCT_STANDARD_TARGET *t;
1065
1066 if (r->type != IPTCC_R_JUMP)
1067 continue;
1068
1069 t = (STRUCT_STANDARD_TARGET *)GET_TARGET(r->entry);
Jan Engelhardtdbb77542008-02-11 00:33:30 +01001070 lc = iptcc_find_chain_by_offset(h, t->verdict);
1071 if (!lc)
Harald Welteaae69be2004-08-29 23:32:14 +00001072 return -1;
Jan Engelhardtdbb77542008-02-11 00:33:30 +01001073 r->jump = lc;
1074 lc->references++;
Harald Welteaae69be2004-08-29 23:32:14 +00001075 }
1076 }
1077
Harald Welteaae69be2004-08-29 23:32:14 +00001078 return 1;
1079}
1080
1081
1082/**********************************************************************
1083 * RULESET COMPILATION (cache -> blob)
1084 **********************************************************************/
1085
1086/* Convenience structures */
1087struct iptcb_chain_start{
1088 STRUCT_ENTRY e;
1089 struct ipt_error_target name;
1090};
1091#define IPTCB_CHAIN_START_SIZE (sizeof(STRUCT_ENTRY) + \
1092 ALIGN(sizeof(struct ipt_error_target)))
1093
1094struct iptcb_chain_foot {
1095 STRUCT_ENTRY e;
1096 STRUCT_STANDARD_TARGET target;
1097};
1098#define IPTCB_CHAIN_FOOT_SIZE (sizeof(STRUCT_ENTRY) + \
1099 ALIGN(sizeof(STRUCT_STANDARD_TARGET)))
1100
1101struct iptcb_chain_error {
1102 STRUCT_ENTRY entry;
1103 struct ipt_error_target target;
1104};
1105#define IPTCB_CHAIN_ERROR_SIZE (sizeof(STRUCT_ENTRY) + \
1106 ALIGN(sizeof(struct ipt_error_target)))
1107
1108
1109
1110/* compile rule from cache into blob */
Jan Engelhardtfd187312008-11-10 16:59:27 +01001111static inline int iptcc_compile_rule (struct xtc_handle *h, STRUCT_REPLACE *repl, struct rule_head *r)
Harald Welteaae69be2004-08-29 23:32:14 +00001112{
1113 /* handle jumps */
1114 if (r->type == IPTCC_R_JUMP) {
1115 STRUCT_STANDARD_TARGET *t;
1116 t = (STRUCT_STANDARD_TARGET *)GET_TARGET(r->entry);
1117 /* memset for memcmp convenience on delete/replace */
1118 memset(t->target.u.user.name, 0, FUNCTION_MAXNAMELEN);
1119 strcpy(t->target.u.user.name, STANDARD_TARGET);
1120 /* Jumps can only happen to builtin chains, so we
1121 * can safely assume that they always have a header */
1122 t->verdict = r->jump->head_offset + IPTCB_CHAIN_START_SIZE;
1123 } else if (r->type == IPTCC_R_FALLTHROUGH) {
1124 STRUCT_STANDARD_TARGET *t;
1125 t = (STRUCT_STANDARD_TARGET *)GET_TARGET(r->entry);
1126 t->verdict = r->offset + r->size;
1127 }
1128
1129 /* copy entry from cache to blob */
1130 memcpy((char *)repl->entries+r->offset, r->entry, r->size);
1131
1132 return 1;
1133}
1134
1135/* compile chain from cache into blob */
Jan Engelhardtfd187312008-11-10 16:59:27 +01001136static int iptcc_compile_chain(struct xtc_handle *h, STRUCT_REPLACE *repl, struct chain_head *c)
Harald Welteaae69be2004-08-29 23:32:14 +00001137{
1138 int ret;
1139 struct rule_head *r;
1140 struct iptcb_chain_start *head;
1141 struct iptcb_chain_foot *foot;
1142
1143 /* only user-defined chains have heaer */
1144 if (!iptcc_is_builtin(c)) {
1145 /* put chain header in place */
1146 head = (void *)repl->entries + c->head_offset;
1147 head->e.target_offset = sizeof(STRUCT_ENTRY);
1148 head->e.next_offset = IPTCB_CHAIN_START_SIZE;
1149 strcpy(head->name.t.u.user.name, ERROR_TARGET);
1150 head->name.t.u.target_size =
1151 ALIGN(sizeof(struct ipt_error_target));
1152 strcpy(head->name.error, c->name);
1153 } else {
1154 repl->hook_entry[c->hooknum-1] = c->head_offset;
1155 repl->underflow[c->hooknum-1] = c->foot_offset;
1156 }
1157
1158 /* iterate over rules */
1159 list_for_each_entry(r, &c->rules, list) {
1160 ret = iptcc_compile_rule(h, repl, r);
1161 if (ret < 0)
1162 return ret;
1163 }
1164
1165 /* put chain footer in place */
1166 foot = (void *)repl->entries + c->foot_offset;
1167 foot->e.target_offset = sizeof(STRUCT_ENTRY);
1168 foot->e.next_offset = IPTCB_CHAIN_FOOT_SIZE;
1169 strcpy(foot->target.target.u.user.name, STANDARD_TARGET);
1170 foot->target.target.u.target_size =
1171 ALIGN(sizeof(STRUCT_STANDARD_TARGET));
1172 /* builtin targets have verdict, others return */
1173 if (iptcc_is_builtin(c))
1174 foot->target.verdict = c->verdict;
1175 else
1176 foot->target.verdict = RETURN;
1177 /* set policy-counters */
1178 memcpy(&foot->e.counters, &c->counters, sizeof(STRUCT_COUNTERS));
1179
1180 return 0;
1181}
1182
1183/* calculate offset and number for every rule in the cache */
Jan Engelhardtfd187312008-11-10 16:59:27 +01001184static int iptcc_compile_chain_offsets(struct xtc_handle *h, struct chain_head *c,
Harald Welteefa8fc22005-07-19 22:03:49 +00001185 unsigned int *offset, unsigned int *num)
Harald Welteaae69be2004-08-29 23:32:14 +00001186{
1187 struct rule_head *r;
1188
1189 c->head_offset = *offset;
1190 DEBUGP("%s: chain_head %u, offset=%u\n", c->name, *num, *offset);
1191
1192 if (!iptcc_is_builtin(c)) {
1193 /* Chain has header */
1194 *offset += sizeof(STRUCT_ENTRY)
1195 + ALIGN(sizeof(struct ipt_error_target));
1196 (*num)++;
1197 }
1198
1199 list_for_each_entry(r, &c->rules, list) {
1200 DEBUGP("rule %u, offset=%u, index=%u\n", *num, *offset, *num);
1201 r->offset = *offset;
1202 r->index = *num;
1203 *offset += r->size;
1204 (*num)++;
1205 }
1206
1207 DEBUGP("%s; chain_foot %u, offset=%u, index=%u\n", c->name, *num,
1208 *offset, *num);
1209 c->foot_offset = *offset;
1210 c->foot_index = *num;
1211 *offset += sizeof(STRUCT_ENTRY)
1212 + ALIGN(sizeof(STRUCT_STANDARD_TARGET));
1213 (*num)++;
1214
1215 return 1;
1216}
1217
1218/* put the pieces back together again */
Jan Engelhardtfd187312008-11-10 16:59:27 +01001219static int iptcc_compile_table_prep(struct xtc_handle *h, unsigned int *size)
Harald Welteaae69be2004-08-29 23:32:14 +00001220{
1221 struct chain_head *c;
1222 unsigned int offset = 0, num = 0;
1223 int ret = 0;
1224
1225 /* First pass: calculate offset for every rule */
1226 list_for_each_entry(c, &h->chains, list) {
1227 ret = iptcc_compile_chain_offsets(h, c, &offset, &num);
1228 if (ret < 0)
1229 return ret;
1230 }
1231
1232 /* Append one error rule at end of chain */
1233 num++;
1234 offset += sizeof(STRUCT_ENTRY)
1235 + ALIGN(sizeof(struct ipt_error_target));
1236
1237 /* ruleset size is now in offset */
1238 *size = offset;
1239 return num;
1240}
1241
Jan Engelhardtfd187312008-11-10 16:59:27 +01001242static int iptcc_compile_table(struct xtc_handle *h, STRUCT_REPLACE *repl)
Harald Welteaae69be2004-08-29 23:32:14 +00001243{
1244 struct chain_head *c;
1245 struct iptcb_chain_error *error;
1246
1247 /* Second pass: copy from cache to offsets, fill in jumps */
1248 list_for_each_entry(c, &h->chains, list) {
1249 int ret = iptcc_compile_chain(h, repl, c);
1250 if (ret < 0)
1251 return ret;
1252 }
1253
1254 /* Append error rule at end of chain */
1255 error = (void *)repl->entries + repl->size - IPTCB_CHAIN_ERROR_SIZE;
1256 error->entry.target_offset = sizeof(STRUCT_ENTRY);
1257 error->entry.next_offset = IPTCB_CHAIN_ERROR_SIZE;
1258 error->target.t.u.user.target_size =
1259 ALIGN(sizeof(struct ipt_error_target));
1260 strcpy((char *)&error->target.t.u.user.name, ERROR_TARGET);
1261 strcpy((char *)&error->target.error, "ERROR");
1262
1263 return 1;
1264}
1265
1266/**********************************************************************
1267 * EXTERNAL API (operates on cache only)
1268 **********************************************************************/
Marc Bouchere6869a82000-03-20 06:03:29 +00001269
1270/* Allocate handle of given size */
Jan Engelhardtfd187312008-11-10 16:59:27 +01001271static struct xtc_handle *
Harald Welte0113fe72004-01-06 19:04:02 +00001272alloc_handle(const char *tablename, unsigned int size, unsigned int num_rules)
Marc Bouchere6869a82000-03-20 06:03:29 +00001273{
1274 size_t len;
Jan Engelhardtfd187312008-11-10 16:59:27 +01001275 struct xtc_handle *h;
Marc Bouchere6869a82000-03-20 06:03:29 +00001276
Harald Welteaae69be2004-08-29 23:32:14 +00001277 len = sizeof(STRUCT_TC_HANDLE) + size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001278
Harald Welteaae69be2004-08-29 23:32:14 +00001279 h = malloc(sizeof(STRUCT_TC_HANDLE));
1280 if (!h) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001281 errno = ENOMEM;
1282 return NULL;
1283 }
Harald Welteaae69be2004-08-29 23:32:14 +00001284 memset(h, 0, sizeof(*h));
1285 INIT_LIST_HEAD(&h->chains);
Marc Bouchere6869a82000-03-20 06:03:29 +00001286 strcpy(h->info.name, tablename);
Harald Welteaae69be2004-08-29 23:32:14 +00001287
Harald Welte0371c0c2004-09-19 21:00:12 +00001288 h->entries = malloc(sizeof(STRUCT_GET_ENTRIES) + size);
Harald Welteaae69be2004-08-29 23:32:14 +00001289 if (!h->entries)
1290 goto out_free_handle;
1291
1292 strcpy(h->entries->name, tablename);
Harald Welte0371c0c2004-09-19 21:00:12 +00001293 h->entries->size = size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001294
1295 return h;
Harald Welteaae69be2004-08-29 23:32:14 +00001296
1297out_free_handle:
1298 free(h);
1299
1300 return NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +00001301}
1302
Harald Welteaae69be2004-08-29 23:32:14 +00001303
Jan Engelhardtfd187312008-11-10 16:59:27 +01001304struct xtc_handle *
Rusty Russell79dee072000-05-02 16:45:16 +00001305TC_INIT(const char *tablename)
Marc Bouchere6869a82000-03-20 06:03:29 +00001306{
Jan Engelhardtfd187312008-11-10 16:59:27 +01001307 struct xtc_handle *h;
Rusty Russell79dee072000-05-02 16:45:16 +00001308 STRUCT_GETINFO info;
Harald Welteefa8fc22005-07-19 22:03:49 +00001309 unsigned int tmp;
Marc Bouchere6869a82000-03-20 06:03:29 +00001310 socklen_t s;
Jan Engelhardt175f4512008-11-10 17:25:55 +01001311 int sockfd;
Marc Bouchere6869a82000-03-20 06:03:29 +00001312
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 }
Jan Engelhardt175f4512008-11-10 17:25:55 +01001319
1320 sockfd = socket(TC_AF, SOCK_RAW, IPPROTO_RAW);
1321 if (sockfd < 0)
1322 return NULL;
1323
Patrick McHardy2f932052008-04-02 14:01:53 +02001324retry:
Marc Bouchere6869a82000-03-20 06:03:29 +00001325 s = sizeof(info);
Martin Josefsson841e4ae2003-05-02 15:30:11 +00001326
Marc Bouchere6869a82000-03-20 06:03:29 +00001327 strcpy(info.name, tablename);
Derrik Pates664c0a32005-02-01 13:28:14 +00001328 if (getsockopt(sockfd, TC_IPPROTO, SO_GET_INFO, &info, &s) < 0) {
Jan Engelhardt175f4512008-11-10 17:25:55 +01001329 close(sockfd);
Marc Bouchere6869a82000-03-20 06:03:29 +00001330 return NULL;
Derrik Pates664c0a32005-02-01 13:28:14 +00001331 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001332
Harald Welteaae69be2004-08-29 23:32:14 +00001333 DEBUGP("valid_hooks=0x%08x, num_entries=%u, size=%u\n",
1334 info.valid_hooks, info.num_entries, info.size);
1335
Harald Welte0113fe72004-01-06 19:04:02 +00001336 if ((h = alloc_handle(info.name, info.size, info.num_entries))
Martin Josefsson841e4ae2003-05-02 15:30:11 +00001337 == NULL) {
Jan Engelhardt175f4512008-11-10 17:25:55 +01001338 close(sockfd);
Marc Bouchere6869a82000-03-20 06:03:29 +00001339 return NULL;
Martin Josefsson841e4ae2003-05-02 15:30:11 +00001340 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001341
Marc Bouchere6869a82000-03-20 06:03:29 +00001342 /* Initialize current state */
Jan Engelhardt175f4512008-11-10 17:25:55 +01001343 h->sockfd = sockfd;
Marc Bouchere6869a82000-03-20 06:03:29 +00001344 h->info = info;
Harald Welte0113fe72004-01-06 19:04:02 +00001345
Harald Welteaae69be2004-08-29 23:32:14 +00001346 h->entries->size = h->info.size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001347
Rusty Russell79dee072000-05-02 16:45:16 +00001348 tmp = sizeof(STRUCT_GET_ENTRIES) + h->info.size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001349
Jan Engelhardt175f4512008-11-10 17:25:55 +01001350 if (getsockopt(h->sockfd, TC_IPPROTO, SO_GET_ENTRIES, h->entries,
Harald Welteaae69be2004-08-29 23:32:14 +00001351 &tmp) < 0)
1352 goto error;
1353
1354#ifdef IPTC_DEBUG2
1355 {
1356 int fd = open("/tmp/libiptc-so_get_entries.blob",
1357 O_CREAT|O_WRONLY);
1358 if (fd >= 0) {
1359 write(fd, h->entries, tmp);
1360 close(fd);
1361 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001362 }
Harald Welteaae69be2004-08-29 23:32:14 +00001363#endif
1364
1365 if (parse_table(h) < 0)
1366 goto error;
Rusty Russell7e53bf92000-03-20 07:03:28 +00001367
Marc Bouchere6869a82000-03-20 06:03:29 +00001368 CHECK(h);
1369 return h;
Harald Welteaae69be2004-08-29 23:32:14 +00001370error:
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001371 TC_FREE(h);
Patrick McHardy2f932052008-04-02 14:01:53 +02001372 /* A different process changed the ruleset size, retry */
1373 if (errno == EAGAIN)
1374 goto retry;
Harald Welteaae69be2004-08-29 23:32:14 +00001375 return NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +00001376}
1377
Martin Josefsson841e4ae2003-05-02 15:30:11 +00001378void
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001379TC_FREE(struct xtc_handle *h)
Martin Josefsson841e4ae2003-05-02 15:30:11 +00001380{
Harald Welteaae69be2004-08-29 23:32:14 +00001381 struct chain_head *c, *tmp;
1382
Derrik Pates664c0a32005-02-01 13:28:14 +00001383 iptc_fn = TC_FREE;
Jan Engelhardt175f4512008-11-10 17:25:55 +01001384 close(h->sockfd);
Harald Welteaae69be2004-08-29 23:32:14 +00001385
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001386 list_for_each_entry_safe(c, tmp, &h->chains, list) {
Harald Welteaae69be2004-08-29 23:32:14 +00001387 struct rule_head *r, *rtmp;
1388
1389 list_for_each_entry_safe(r, rtmp, &c->rules, list) {
1390 free(r);
1391 }
1392
1393 free(c);
1394 }
1395
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001396 iptcc_chain_index_free(h);
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +00001397
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001398 free(h->entries);
1399 free(h);
Martin Josefsson841e4ae2003-05-02 15:30:11 +00001400}
1401
Marc Bouchere6869a82000-03-20 06:03:29 +00001402static inline int
Rusty Russell79dee072000-05-02 16:45:16 +00001403print_match(const STRUCT_ENTRY_MATCH *m)
Marc Bouchere6869a82000-03-20 06:03:29 +00001404{
Rusty Russell228e98d2000-04-27 10:28:06 +00001405 printf("Match name: `%s'\n", m->u.user.name);
Marc Bouchere6869a82000-03-20 06:03:29 +00001406 return 0;
1407}
1408
Jan Engelhardtfd187312008-11-10 16:59:27 +01001409static int dump_entry(STRUCT_ENTRY *e, struct xtc_handle *const handle);
Rusty Russell79dee072000-05-02 16:45:16 +00001410
Marc Bouchere6869a82000-03-20 06:03:29 +00001411void
Jan Engelhardtfd187312008-11-10 16:59:27 +01001412TC_DUMP_ENTRIES(struct xtc_handle *const handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001413{
Derrik Pates664c0a32005-02-01 13:28:14 +00001414 iptc_fn = TC_DUMP_ENTRIES;
Marc Bouchere6869a82000-03-20 06:03:29 +00001415 CHECK(handle);
Patrick McHardy97fb2f12007-09-08 16:52:25 +00001416
Rusty Russelle45c7132004-12-16 13:21:44 +00001417 printf("libiptc v%s. %u bytes.\n",
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +02001418 XTABLES_VERSION, handle->entries->size);
Marc Bouchere6869a82000-03-20 06:03:29 +00001419 printf("Table `%s'\n", handle->info.name);
Jan Engelhardtd73af642008-11-10 17:07:31 +01001420 printf("Hooks: pre/in/fwd/out/post = %x/%x/%x/%x/%x\n",
Rusty Russell67088e72000-05-10 01:18:57 +00001421 handle->info.hook_entry[HOOK_PRE_ROUTING],
1422 handle->info.hook_entry[HOOK_LOCAL_IN],
1423 handle->info.hook_entry[HOOK_FORWARD],
1424 handle->info.hook_entry[HOOK_LOCAL_OUT],
1425 handle->info.hook_entry[HOOK_POST_ROUTING]);
Jan Engelhardtd73af642008-11-10 17:07:31 +01001426 printf("Underflows: pre/in/fwd/out/post = %x/%x/%x/%x/%x\n",
Rusty Russell67088e72000-05-10 01:18:57 +00001427 handle->info.underflow[HOOK_PRE_ROUTING],
1428 handle->info.underflow[HOOK_LOCAL_IN],
1429 handle->info.underflow[HOOK_FORWARD],
1430 handle->info.underflow[HOOK_LOCAL_OUT],
1431 handle->info.underflow[HOOK_POST_ROUTING]);
Marc Bouchere6869a82000-03-20 06:03:29 +00001432
Harald Welteaae69be2004-08-29 23:32:14 +00001433 ENTRY_ITERATE(handle->entries->entrytable, handle->entries->size,
Rusty Russell79dee072000-05-02 16:45:16 +00001434 dump_entry, handle);
Harald Welte0113fe72004-01-06 19:04:02 +00001435}
Rusty Russell30fd6e52000-04-23 09:16:06 +00001436
Marc Bouchere6869a82000-03-20 06:03:29 +00001437/* Does this chain exist? */
Jan Engelhardtfd187312008-11-10 16:59:27 +01001438int TC_IS_CHAIN(const char *chain, struct xtc_handle *const handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001439{
Derrik Pates664c0a32005-02-01 13:28:14 +00001440 iptc_fn = TC_IS_CHAIN;
Harald Welteaae69be2004-08-29 23:32:14 +00001441 return iptcc_find_label(chain, handle) != NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +00001442}
1443
Jan Engelhardtfd187312008-11-10 16:59:27 +01001444static void iptcc_chain_iterator_advance(struct xtc_handle *handle)
Harald Welteaae69be2004-08-29 23:32:14 +00001445{
1446 struct chain_head *c = handle->chain_iterator_cur;
1447
1448 if (c->list.next == &handle->chains)
1449 handle->chain_iterator_cur = NULL;
1450 else
1451 handle->chain_iterator_cur =
1452 list_entry(c->list.next, struct chain_head, list);
1453}
Marc Bouchere6869a82000-03-20 06:03:29 +00001454
Rusty Russell30fd6e52000-04-23 09:16:06 +00001455/* Iterator functions to run through the chains. */
Marc Bouchere6869a82000-03-20 06:03:29 +00001456const char *
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001457TC_FIRST_CHAIN(struct xtc_handle *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001458{
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001459 struct chain_head *c = list_entry(handle->chains.next,
Harald Welteaae69be2004-08-29 23:32:14 +00001460 struct chain_head, list);
1461
1462 iptc_fn = TC_FIRST_CHAIN;
1463
1464
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001465 if (list_empty(&handle->chains)) {
Harald Welteaae69be2004-08-29 23:32:14 +00001466 DEBUGP(": no chains\n");
Harald Welte0113fe72004-01-06 19:04:02 +00001467 return NULL;
Harald Welteaae69be2004-08-29 23:32:14 +00001468 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001469
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001470 handle->chain_iterator_cur = c;
1471 iptcc_chain_iterator_advance(handle);
Harald Welte0113fe72004-01-06 19:04:02 +00001472
Harald Welteaae69be2004-08-29 23:32:14 +00001473 DEBUGP(": returning `%s'\n", c->name);
1474 return c->name;
Marc Bouchere6869a82000-03-20 06:03:29 +00001475}
1476
Rusty Russell30fd6e52000-04-23 09:16:06 +00001477/* Iterator functions to run through the chains. Returns NULL at end. */
1478const char *
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001479TC_NEXT_CHAIN(struct xtc_handle *handle)
Rusty Russell30fd6e52000-04-23 09:16:06 +00001480{
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001481 struct chain_head *c = handle->chain_iterator_cur;
Rusty Russell30fd6e52000-04-23 09:16:06 +00001482
Harald Welteaae69be2004-08-29 23:32:14 +00001483 iptc_fn = TC_NEXT_CHAIN;
1484
1485 if (!c) {
1486 DEBUGP(": no more chains\n");
Rusty Russell30fd6e52000-04-23 09:16:06 +00001487 return NULL;
Harald Welteaae69be2004-08-29 23:32:14 +00001488 }
Rusty Russell30fd6e52000-04-23 09:16:06 +00001489
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001490 iptcc_chain_iterator_advance(handle);
Harald Welteaae69be2004-08-29 23:32:14 +00001491
1492 DEBUGP(": returning `%s'\n", c->name);
1493 return c->name;
Rusty Russell30fd6e52000-04-23 09:16:06 +00001494}
1495
1496/* Get first rule in the given chain: NULL for empty chain. */
Rusty Russell79dee072000-05-02 16:45:16 +00001497const STRUCT_ENTRY *
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001498TC_FIRST_RULE(const char *chain, struct xtc_handle *handle)
Rusty Russell30fd6e52000-04-23 09:16:06 +00001499{
Harald Welteaae69be2004-08-29 23:32:14 +00001500 struct chain_head *c;
1501 struct rule_head *r;
Rusty Russell30fd6e52000-04-23 09:16:06 +00001502
Harald Welteaae69be2004-08-29 23:32:14 +00001503 iptc_fn = TC_FIRST_RULE;
1504
1505 DEBUGP("first rule(%s): ", chain);
1506
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001507 c = iptcc_find_label(chain, handle);
Rusty Russell30fd6e52000-04-23 09:16:06 +00001508 if (!c) {
1509 errno = ENOENT;
1510 return NULL;
1511 }
1512
1513 /* Empty chain: single return/policy rule */
Harald Welteaae69be2004-08-29 23:32:14 +00001514 if (list_empty(&c->rules)) {
1515 DEBUGP_C("no rules, returning NULL\n");
Rusty Russell30fd6e52000-04-23 09:16:06 +00001516 return NULL;
Harald Welteaae69be2004-08-29 23:32:14 +00001517 }
Rusty Russell30fd6e52000-04-23 09:16:06 +00001518
Harald Welteaae69be2004-08-29 23:32:14 +00001519 r = list_entry(c->rules.next, struct rule_head, list);
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001520 handle->rule_iterator_cur = r;
Harald Welteaae69be2004-08-29 23:32:14 +00001521 DEBUGP_C("%p\n", r);
1522
1523 return r->entry;
Rusty Russell30fd6e52000-04-23 09:16:06 +00001524}
1525
1526/* Returns NULL when rules run out. */
Rusty Russell79dee072000-05-02 16:45:16 +00001527const STRUCT_ENTRY *
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001528TC_NEXT_RULE(const STRUCT_ENTRY *prev, struct xtc_handle *handle)
Rusty Russell30fd6e52000-04-23 09:16:06 +00001529{
Harald Welteaae69be2004-08-29 23:32:14 +00001530 struct rule_head *r;
Rusty Russell30fd6e52000-04-23 09:16:06 +00001531
Derrik Pates664c0a32005-02-01 13:28:14 +00001532 iptc_fn = TC_NEXT_RULE;
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001533 DEBUGP("rule_iterator_cur=%p...", handle->rule_iterator_cur);
Harald Welteaae69be2004-08-29 23:32:14 +00001534
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001535 if (handle->rule_iterator_cur == NULL) {
Harald Welteaae69be2004-08-29 23:32:14 +00001536 DEBUGP_C("returning NULL\n");
1537 return NULL;
1538 }
1539
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001540 r = list_entry(handle->rule_iterator_cur->list.next,
Harald Welteaae69be2004-08-29 23:32:14 +00001541 struct rule_head, list);
1542
1543 iptc_fn = TC_NEXT_RULE;
1544
1545 DEBUGP_C("next=%p, head=%p...", &r->list,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001546 &handle->rule_iterator_cur->chain->rules);
Harald Welteaae69be2004-08-29 23:32:14 +00001547
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001548 if (&r->list == &handle->rule_iterator_cur->chain->rules) {
1549 handle->rule_iterator_cur = NULL;
Harald Welteaae69be2004-08-29 23:32:14 +00001550 DEBUGP_C("finished, returning NULL\n");
1551 return NULL;
1552 }
1553
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001554 handle->rule_iterator_cur = r;
Harald Welteaae69be2004-08-29 23:32:14 +00001555
1556 /* NOTE: prev is without any influence ! */
1557 DEBUGP_C("returning rule %p\n", r);
1558 return r->entry;
Rusty Russell30fd6e52000-04-23 09:16:06 +00001559}
1560
Marc Bouchere6869a82000-03-20 06:03:29 +00001561/* How many rules in this chain? */
Jan Engelhardt33690a12008-02-11 00:54:00 +01001562static unsigned int
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001563TC_NUM_RULES(const char *chain, struct xtc_handle *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001564{
Harald Welteaae69be2004-08-29 23:32:14 +00001565 struct chain_head *c;
1566 iptc_fn = TC_NUM_RULES;
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001567 CHECK(handle);
Harald Welteaae69be2004-08-29 23:32:14 +00001568
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001569 c = iptcc_find_label(chain, handle);
Harald Welteaae69be2004-08-29 23:32:14 +00001570 if (!c) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001571 errno = ENOENT;
1572 return (unsigned int)-1;
1573 }
Harald Welteaae69be2004-08-29 23:32:14 +00001574
1575 return c->num_rules;
Marc Bouchere6869a82000-03-20 06:03:29 +00001576}
1577
Jan Engelhardt33690a12008-02-11 00:54:00 +01001578static const STRUCT_ENTRY *
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001579TC_GET_RULE(const char *chain, unsigned int n, struct xtc_handle *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001580{
Harald Welteaae69be2004-08-29 23:32:14 +00001581 struct chain_head *c;
1582 struct rule_head *r;
1583
1584 iptc_fn = TC_GET_RULE;
Marc Bouchere6869a82000-03-20 06:03:29 +00001585
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001586 CHECK(handle);
Harald Welteaae69be2004-08-29 23:32:14 +00001587
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001588 c = iptcc_find_label(chain, handle);
Harald Welteaae69be2004-08-29 23:32:14 +00001589 if (!c) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001590 errno = ENOENT;
1591 return NULL;
1592 }
1593
Harald Welteaae69be2004-08-29 23:32:14 +00001594 r = iptcc_get_rule_num(c, n);
1595 if (!r)
1596 return NULL;
1597 return r->entry;
Marc Bouchere6869a82000-03-20 06:03:29 +00001598}
1599
1600/* Returns a pointer to the target name of this position. */
Jan Engelhardt33690a12008-02-11 00:54:00 +01001601static const char *standard_target_map(int verdict)
Marc Bouchere6869a82000-03-20 06:03:29 +00001602{
Harald Welteaae69be2004-08-29 23:32:14 +00001603 switch (verdict) {
1604 case RETURN:
1605 return LABEL_RETURN;
1606 break;
1607 case -NF_ACCEPT-1:
1608 return LABEL_ACCEPT;
1609 break;
1610 case -NF_DROP-1:
1611 return LABEL_DROP;
1612 break;
1613 case -NF_QUEUE-1:
1614 return LABEL_QUEUE;
1615 break;
1616 default:
1617 fprintf(stderr, "ERROR: %d not a valid target)\n",
1618 verdict);
1619 abort();
1620 break;
1621 }
1622 /* not reached */
1623 return NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +00001624}
1625
Harald Welteaae69be2004-08-29 23:32:14 +00001626/* Returns a pointer to the target name of this position. */
1627const char *TC_GET_TARGET(const STRUCT_ENTRY *ce,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001628 struct xtc_handle *handle)
Harald Welteaae69be2004-08-29 23:32:14 +00001629{
1630 STRUCT_ENTRY *e = (STRUCT_ENTRY *)ce;
Rusty Russelle45c7132004-12-16 13:21:44 +00001631 struct rule_head *r = container_of(e, struct rule_head, entry[0]);
Harald Welteaae69be2004-08-29 23:32:14 +00001632
1633 iptc_fn = TC_GET_TARGET;
1634
1635 switch(r->type) {
1636 int spos;
1637 case IPTCC_R_FALLTHROUGH:
1638 return "";
1639 break;
1640 case IPTCC_R_JUMP:
1641 DEBUGP("r=%p, jump=%p, name=`%s'\n", r, r->jump, r->jump->name);
1642 return r->jump->name;
1643 break;
1644 case IPTCC_R_STANDARD:
1645 spos = *(int *)GET_TARGET(e)->data;
1646 DEBUGP("r=%p, spos=%d'\n", r, spos);
1647 return standard_target_map(spos);
1648 break;
1649 case IPTCC_R_MODULE:
1650 return GET_TARGET(e)->u.user.name;
1651 break;
1652 }
1653 return NULL;
1654}
Marc Bouchere6869a82000-03-20 06:03:29 +00001655/* Is this a built-in chain? Actually returns hook + 1. */
1656int
Jan Engelhardtfd187312008-11-10 16:59:27 +01001657TC_BUILTIN(const char *chain, struct xtc_handle *const handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001658{
Harald Welteaae69be2004-08-29 23:32:14 +00001659 struct chain_head *c;
1660
1661 iptc_fn = TC_BUILTIN;
Marc Bouchere6869a82000-03-20 06:03:29 +00001662
Harald Welteaae69be2004-08-29 23:32:14 +00001663 c = iptcc_find_label(chain, handle);
1664 if (!c) {
1665 errno = ENOENT;
Martin Josefssonb0f3d2d2004-09-23 18:23:20 +00001666 return 0;
Marc Bouchere6869a82000-03-20 06:03:29 +00001667 }
Harald Welteaae69be2004-08-29 23:32:14 +00001668
1669 return iptcc_is_builtin(c);
Marc Bouchere6869a82000-03-20 06:03:29 +00001670}
1671
1672/* Get the policy of a given built-in chain */
1673const char *
Rusty Russell79dee072000-05-02 16:45:16 +00001674TC_GET_POLICY(const char *chain,
1675 STRUCT_COUNTERS *counters,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001676 struct xtc_handle *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001677{
Harald Welteaae69be2004-08-29 23:32:14 +00001678 struct chain_head *c;
Marc Bouchere6869a82000-03-20 06:03:29 +00001679
Harald Welteaae69be2004-08-29 23:32:14 +00001680 iptc_fn = TC_GET_POLICY;
1681
1682 DEBUGP("called for chain %s\n", chain);
1683
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001684 c = iptcc_find_label(chain, handle);
Harald Welteaae69be2004-08-29 23:32:14 +00001685 if (!c) {
1686 errno = ENOENT;
1687 return NULL;
1688 }
1689
1690 if (!iptcc_is_builtin(c))
Marc Bouchere6869a82000-03-20 06:03:29 +00001691 return NULL;
1692
Harald Welteaae69be2004-08-29 23:32:14 +00001693 *counters = c->counters;
Marc Bouchere6869a82000-03-20 06:03:29 +00001694
Harald Welteaae69be2004-08-29 23:32:14 +00001695 return standard_target_map(c->verdict);
Harald Welte0113fe72004-01-06 19:04:02 +00001696}
1697
1698static int
Harald Welteaae69be2004-08-29 23:32:14 +00001699iptcc_standard_map(struct rule_head *r, int verdict)
Harald Welte0113fe72004-01-06 19:04:02 +00001700{
Harald Welteaae69be2004-08-29 23:32:14 +00001701 STRUCT_ENTRY *e = r->entry;
Rusty Russell79dee072000-05-02 16:45:16 +00001702 STRUCT_STANDARD_TARGET *t;
Marc Bouchere6869a82000-03-20 06:03:29 +00001703
Rusty Russell79dee072000-05-02 16:45:16 +00001704 t = (STRUCT_STANDARD_TARGET *)GET_TARGET(e);
Marc Bouchere6869a82000-03-20 06:03:29 +00001705
Rusty Russell67088e72000-05-10 01:18:57 +00001706 if (t->target.u.target_size
Philip Blundell8c700902000-05-15 02:17:52 +00001707 != ALIGN(sizeof(STRUCT_STANDARD_TARGET))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001708 errno = EINVAL;
1709 return 0;
1710 }
1711 /* memset for memcmp convenience on delete/replace */
Rusty Russell79dee072000-05-02 16:45:16 +00001712 memset(t->target.u.user.name, 0, FUNCTION_MAXNAMELEN);
1713 strcpy(t->target.u.user.name, STANDARD_TARGET);
Marc Bouchere6869a82000-03-20 06:03:29 +00001714 t->verdict = verdict;
1715
Harald Welteaae69be2004-08-29 23:32:14 +00001716 r->type = IPTCC_R_STANDARD;
1717
Marc Bouchere6869a82000-03-20 06:03:29 +00001718 return 1;
1719}
Rusty Russell7e53bf92000-03-20 07:03:28 +00001720
Marc Bouchere6869a82000-03-20 06:03:29 +00001721static int
Jan Engelhardtfd187312008-11-10 16:59:27 +01001722iptcc_map_target(struct xtc_handle *const handle,
Harald Welteaae69be2004-08-29 23:32:14 +00001723 struct rule_head *r)
Marc Bouchere6869a82000-03-20 06:03:29 +00001724{
Harald Welteaae69be2004-08-29 23:32:14 +00001725 STRUCT_ENTRY *e = r->entry;
Harald Welte0113fe72004-01-06 19:04:02 +00001726 STRUCT_ENTRY_TARGET *t = GET_TARGET(e);
Marc Bouchere6869a82000-03-20 06:03:29 +00001727
Marc Bouchere6869a82000-03-20 06:03:29 +00001728 /* Maybe it's empty (=> fall through) */
Harald Welteaae69be2004-08-29 23:32:14 +00001729 if (strcmp(t->u.user.name, "") == 0) {
1730 r->type = IPTCC_R_FALLTHROUGH;
1731 return 1;
1732 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001733 /* Maybe it's a standard target name... */
Rusty Russell79dee072000-05-02 16:45:16 +00001734 else if (strcmp(t->u.user.name, LABEL_ACCEPT) == 0)
Harald Welteaae69be2004-08-29 23:32:14 +00001735 return iptcc_standard_map(r, -NF_ACCEPT - 1);
Rusty Russell79dee072000-05-02 16:45:16 +00001736 else if (strcmp(t->u.user.name, LABEL_DROP) == 0)
Harald Welteaae69be2004-08-29 23:32:14 +00001737 return iptcc_standard_map(r, -NF_DROP - 1);
Rusty Russell67088e72000-05-10 01:18:57 +00001738 else if (strcmp(t->u.user.name, LABEL_QUEUE) == 0)
Harald Welteaae69be2004-08-29 23:32:14 +00001739 return iptcc_standard_map(r, -NF_QUEUE - 1);
Rusty Russell79dee072000-05-02 16:45:16 +00001740 else if (strcmp(t->u.user.name, LABEL_RETURN) == 0)
Harald Welteaae69be2004-08-29 23:32:14 +00001741 return iptcc_standard_map(r, RETURN);
Rusty Russell79dee072000-05-02 16:45:16 +00001742 else if (TC_BUILTIN(t->u.user.name, handle)) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001743 /* Can't jump to builtins. */
1744 errno = EINVAL;
1745 return 0;
1746 } else {
1747 /* Maybe it's an existing chain name. */
Harald Welteaae69be2004-08-29 23:32:14 +00001748 struct chain_head *c;
1749 DEBUGP("trying to find chain `%s': ", t->u.user.name);
Marc Bouchere6869a82000-03-20 06:03:29 +00001750
Harald Welteaae69be2004-08-29 23:32:14 +00001751 c = iptcc_find_label(t->u.user.name, handle);
1752 if (c) {
1753 DEBUGP_C("found!\n");
1754 r->type = IPTCC_R_JUMP;
1755 r->jump = c;
1756 c->references++;
1757 return 1;
1758 }
1759 DEBUGP_C("not found :(\n");
Marc Bouchere6869a82000-03-20 06:03:29 +00001760 }
1761
1762 /* Must be a module? If not, kernel will reject... */
Rusty Russell3aef54d2005-01-03 03:48:40 +00001763 /* memset to all 0 for your memcmp convenience: don't clear version */
Rusty Russell228e98d2000-04-27 10:28:06 +00001764 memset(t->u.user.name + strlen(t->u.user.name),
Marc Bouchere6869a82000-03-20 06:03:29 +00001765 0,
Rusty Russell3aef54d2005-01-03 03:48:40 +00001766 FUNCTION_MAXNAMELEN - 1 - strlen(t->u.user.name));
Rusty Russell733e54b2004-12-16 14:22:23 +00001767 r->type = IPTCC_R_MODULE;
Harald Welteaae69be2004-08-29 23:32:14 +00001768 set_changed(handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001769 return 1;
1770}
1771
Harald Welte0113fe72004-01-06 19:04:02 +00001772/* Insert the entry `fw' in chain `chain' into position `rulenum'. */
Marc Bouchere6869a82000-03-20 06:03:29 +00001773int
Rusty Russell79dee072000-05-02 16:45:16 +00001774TC_INSERT_ENTRY(const IPT_CHAINLABEL chain,
1775 const STRUCT_ENTRY *e,
1776 unsigned int rulenum,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001777 struct xtc_handle *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001778{
Harald Welteaae69be2004-08-29 23:32:14 +00001779 struct chain_head *c;
Martin Josefssoneb066cc2004-09-22 21:04:07 +00001780 struct rule_head *r;
1781 struct list_head *prev;
Marc Bouchere6869a82000-03-20 06:03:29 +00001782
Rusty Russell79dee072000-05-02 16:45:16 +00001783 iptc_fn = TC_INSERT_ENTRY;
Harald Welteaae69be2004-08-29 23:32:14 +00001784
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001785 if (!(c = iptcc_find_label(chain, handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001786 errno = ENOENT;
1787 return 0;
1788 }
1789
Martin Josefssoneb066cc2004-09-22 21:04:07 +00001790 /* first rulenum index = 0
1791 first c->num_rules index = 1 */
1792 if (rulenum > c->num_rules) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001793 errno = E2BIG;
1794 return 0;
1795 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001796
Martin Josefsson631f3612004-09-23 19:25:06 +00001797 /* If we are inserting at the end just take advantage of the
1798 double linked list, insert will happen before the entry
1799 prev points to. */
1800 if (rulenum == c->num_rules) {
Martin Josefssoneb066cc2004-09-22 21:04:07 +00001801 prev = &c->rules;
Martin Josefssona5616dc2004-10-24 22:27:31 +00001802 } else if (rulenum + 1 <= c->num_rules/2) {
Martin Josefsson631f3612004-09-23 19:25:06 +00001803 r = iptcc_get_rule_num(c, rulenum + 1);
Martin Josefssona5616dc2004-10-24 22:27:31 +00001804 prev = &r->list;
1805 } else {
1806 r = iptcc_get_rule_num_reverse(c, c->num_rules - rulenum);
Martin Josefsson631f3612004-09-23 19:25:06 +00001807 prev = &r->list;
1808 }
Martin Josefssoneb066cc2004-09-22 21:04:07 +00001809
Harald Welteaae69be2004-08-29 23:32:14 +00001810 if (!(r = iptcc_alloc_rule(c, e->next_offset))) {
1811 errno = ENOMEM;
Harald Welte0113fe72004-01-06 19:04:02 +00001812 return 0;
Harald Welteaae69be2004-08-29 23:32:14 +00001813 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001814
Harald Welteaae69be2004-08-29 23:32:14 +00001815 memcpy(r->entry, e, e->next_offset);
1816 r->counter_map.maptype = COUNTER_MAP_SET;
1817
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001818 if (!iptcc_map_target(handle, r)) {
Harald Welteaae69be2004-08-29 23:32:14 +00001819 free(r);
1820 return 0;
1821 }
1822
Martin Josefssoneb066cc2004-09-22 21:04:07 +00001823 list_add_tail(&r->list, prev);
Harald Welteaae69be2004-08-29 23:32:14 +00001824 c->num_rules++;
1825
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001826 set_changed(handle);
Harald Welteaae69be2004-08-29 23:32:14 +00001827
1828 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00001829}
1830
1831/* Atomically replace rule `rulenum' in `chain' with `fw'. */
1832int
Rusty Russell79dee072000-05-02 16:45:16 +00001833TC_REPLACE_ENTRY(const IPT_CHAINLABEL chain,
1834 const STRUCT_ENTRY *e,
1835 unsigned int rulenum,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001836 struct xtc_handle *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001837{
Harald Welteaae69be2004-08-29 23:32:14 +00001838 struct chain_head *c;
1839 struct rule_head *r, *old;
Marc Bouchere6869a82000-03-20 06:03:29 +00001840
Rusty Russell79dee072000-05-02 16:45:16 +00001841 iptc_fn = TC_REPLACE_ENTRY;
Marc Bouchere6869a82000-03-20 06:03:29 +00001842
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001843 if (!(c = iptcc_find_label(chain, handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001844 errno = ENOENT;
1845 return 0;
1846 }
1847
Martin Josefsson0f9b8b12004-12-18 17:18:49 +00001848 if (rulenum >= c->num_rules) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001849 errno = E2BIG;
1850 return 0;
1851 }
1852
Martin Josefsson0f9b8b12004-12-18 17:18:49 +00001853 /* Take advantage of the double linked list if possible. */
1854 if (rulenum + 1 <= c->num_rules/2) {
1855 old = iptcc_get_rule_num(c, rulenum + 1);
1856 } else {
1857 old = iptcc_get_rule_num_reverse(c, c->num_rules - rulenum);
1858 }
1859
Harald Welteaae69be2004-08-29 23:32:14 +00001860 if (!(r = iptcc_alloc_rule(c, e->next_offset))) {
1861 errno = ENOMEM;
Marc Bouchere6869a82000-03-20 06:03:29 +00001862 return 0;
Harald Welteaae69be2004-08-29 23:32:14 +00001863 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001864
Harald Welteaae69be2004-08-29 23:32:14 +00001865 memcpy(r->entry, e, e->next_offset);
1866 r->counter_map.maptype = COUNTER_MAP_SET;
1867
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001868 if (!iptcc_map_target(handle, r)) {
Harald Welteaae69be2004-08-29 23:32:14 +00001869 free(r);
Harald Welte0113fe72004-01-06 19:04:02 +00001870 return 0;
Harald Welteaae69be2004-08-29 23:32:14 +00001871 }
Harald Welte0113fe72004-01-06 19:04:02 +00001872
Harald Welteaae69be2004-08-29 23:32:14 +00001873 list_add(&r->list, &old->list);
1874 iptcc_delete_rule(old);
1875
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001876 set_changed(handle);
Harald Welteaae69be2004-08-29 23:32:14 +00001877
1878 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00001879}
1880
Harald Welte0113fe72004-01-06 19:04:02 +00001881/* Append entry `fw' to chain `chain'. Equivalent to insert with
Marc Bouchere6869a82000-03-20 06:03:29 +00001882 rulenum = length of chain. */
1883int
Rusty Russell79dee072000-05-02 16:45:16 +00001884TC_APPEND_ENTRY(const IPT_CHAINLABEL chain,
1885 const STRUCT_ENTRY *e,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001886 struct xtc_handle *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001887{
Harald Welteaae69be2004-08-29 23:32:14 +00001888 struct chain_head *c;
1889 struct rule_head *r;
Marc Bouchere6869a82000-03-20 06:03:29 +00001890
Rusty Russell79dee072000-05-02 16:45:16 +00001891 iptc_fn = TC_APPEND_ENTRY;
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001892 if (!(c = iptcc_find_label(chain, handle))) {
Harald Welteaae69be2004-08-29 23:32:14 +00001893 DEBUGP("unable to find chain `%s'\n", chain);
Marc Bouchere6869a82000-03-20 06:03:29 +00001894 errno = ENOENT;
1895 return 0;
1896 }
1897
Harald Welteaae69be2004-08-29 23:32:14 +00001898 if (!(r = iptcc_alloc_rule(c, e->next_offset))) {
1899 DEBUGP("unable to allocate rule for chain `%s'\n", chain);
1900 errno = ENOMEM;
Harald Welte0113fe72004-01-06 19:04:02 +00001901 return 0;
Harald Welteaae69be2004-08-29 23:32:14 +00001902 }
Harald Welte0113fe72004-01-06 19:04:02 +00001903
Harald Welteaae69be2004-08-29 23:32:14 +00001904 memcpy(r->entry, e, e->next_offset);
1905 r->counter_map.maptype = COUNTER_MAP_SET;
1906
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001907 if (!iptcc_map_target(handle, r)) {
Martin Josefsson12009532004-09-23 18:24:29 +00001908 DEBUGP("unable to map target of rule for chain `%s'\n", chain);
Harald Welteaae69be2004-08-29 23:32:14 +00001909 free(r);
1910 return 0;
1911 }
1912
1913 list_add_tail(&r->list, &c->rules);
1914 c->num_rules++;
1915
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001916 set_changed(handle);
Harald Welteaae69be2004-08-29 23:32:14 +00001917
1918 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00001919}
1920
1921static inline int
Rusty Russell79dee072000-05-02 16:45:16 +00001922match_different(const STRUCT_ENTRY_MATCH *a,
Rusty Russelledf14cf2000-04-19 11:26:44 +00001923 const unsigned char *a_elems,
1924 const unsigned char *b_elems,
1925 unsigned char **maskptr)
Marc Bouchere6869a82000-03-20 06:03:29 +00001926{
Rusty Russell79dee072000-05-02 16:45:16 +00001927 const STRUCT_ENTRY_MATCH *b;
Rusty Russelledf14cf2000-04-19 11:26:44 +00001928 unsigned int i;
Marc Bouchere6869a82000-03-20 06:03:29 +00001929
1930 /* Offset of b is the same as a. */
Rusty Russell30fd6e52000-04-23 09:16:06 +00001931 b = (void *)b_elems + ((unsigned char *)a - a_elems);
Marc Bouchere6869a82000-03-20 06:03:29 +00001932
Rusty Russell228e98d2000-04-27 10:28:06 +00001933 if (a->u.match_size != b->u.match_size)
Marc Bouchere6869a82000-03-20 06:03:29 +00001934 return 1;
1935
Rusty Russell228e98d2000-04-27 10:28:06 +00001936 if (strcmp(a->u.user.name, b->u.user.name) != 0)
Marc Bouchere6869a82000-03-20 06:03:29 +00001937 return 1;
1938
Rusty Russell73ef09b2000-07-03 10:24:04 +00001939 *maskptr += ALIGN(sizeof(*a));
Rusty Russelledf14cf2000-04-19 11:26:44 +00001940
Rusty Russell73ef09b2000-07-03 10:24:04 +00001941 for (i = 0; i < a->u.match_size - ALIGN(sizeof(*a)); i++)
Rusty Russelledf14cf2000-04-19 11:26:44 +00001942 if (((a->data[i] ^ b->data[i]) & (*maskptr)[i]) != 0)
Rusty Russell90e712a2000-03-29 04:19:26 +00001943 return 1;
Rusty Russelledf14cf2000-04-19 11:26:44 +00001944 *maskptr += i;
1945 return 0;
1946}
1947
1948static inline int
Rusty Russell733e54b2004-12-16 14:22:23 +00001949target_same(struct rule_head *a, struct rule_head *b,const unsigned char *mask)
Rusty Russelledf14cf2000-04-19 11:26:44 +00001950{
1951 unsigned int i;
Rusty Russell733e54b2004-12-16 14:22:23 +00001952 STRUCT_ENTRY_TARGET *ta, *tb;
Marc Bouchere6869a82000-03-20 06:03:29 +00001953
Rusty Russell733e54b2004-12-16 14:22:23 +00001954 if (a->type != b->type)
1955 return 0;
1956
1957 ta = GET_TARGET(a->entry);
1958 tb = GET_TARGET(b->entry);
1959
1960 switch (a->type) {
1961 case IPTCC_R_FALLTHROUGH:
1962 return 1;
1963 case IPTCC_R_JUMP:
1964 return a->jump == b->jump;
1965 case IPTCC_R_STANDARD:
1966 return ((STRUCT_STANDARD_TARGET *)ta)->verdict
1967 == ((STRUCT_STANDARD_TARGET *)tb)->verdict;
1968 case IPTCC_R_MODULE:
1969 if (ta->u.target_size != tb->u.target_size)
1970 return 0;
1971 if (strcmp(ta->u.user.name, tb->u.user.name) != 0)
1972 return 0;
1973
1974 for (i = 0; i < ta->u.target_size - sizeof(*ta); i++)
Rusty Russelldaade442004-12-29 11:14:52 +00001975 if (((ta->data[i] ^ tb->data[i]) & mask[i]) != 0)
Rusty Russell733e54b2004-12-16 14:22:23 +00001976 return 0;
1977 return 1;
1978 default:
1979 fprintf(stderr, "ERROR: bad type %i\n", a->type);
1980 abort();
1981 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001982}
1983
Rusty Russell733e54b2004-12-16 14:22:23 +00001984static unsigned char *
Rusty Russell79dee072000-05-02 16:45:16 +00001985is_same(const STRUCT_ENTRY *a,
1986 const STRUCT_ENTRY *b,
1987 unsigned char *matchmask);
Marc Bouchere6869a82000-03-20 06:03:29 +00001988
Harald Welte0113fe72004-01-06 19:04:02 +00001989/* Delete the first rule in `chain' which matches `fw'. */
Marc Bouchere6869a82000-03-20 06:03:29 +00001990int
Rusty Russell79dee072000-05-02 16:45:16 +00001991TC_DELETE_ENTRY(const IPT_CHAINLABEL chain,
1992 const STRUCT_ENTRY *origfw,
1993 unsigned char *matchmask,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001994 struct xtc_handle *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001995{
Harald Welteaae69be2004-08-29 23:32:14 +00001996 struct chain_head *c;
Rusty Russelle45c7132004-12-16 13:21:44 +00001997 struct rule_head *r, *i;
Marc Bouchere6869a82000-03-20 06:03:29 +00001998
Rusty Russell79dee072000-05-02 16:45:16 +00001999 iptc_fn = TC_DELETE_ENTRY;
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002000 if (!(c = iptcc_find_label(chain, handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002001 errno = ENOENT;
2002 return 0;
2003 }
2004
Rusty Russelle45c7132004-12-16 13:21:44 +00002005 /* Create a rule_head from origfw. */
2006 r = iptcc_alloc_rule(c, origfw->next_offset);
2007 if (!r) {
Harald Welte0113fe72004-01-06 19:04:02 +00002008 errno = ENOMEM;
2009 return 0;
2010 }
2011
Rusty Russelle45c7132004-12-16 13:21:44 +00002012 memcpy(r->entry, origfw, origfw->next_offset);
2013 r->counter_map.maptype = COUNTER_MAP_NOMAP;
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002014 if (!iptcc_map_target(handle, r)) {
Rusty Russelle45c7132004-12-16 13:21:44 +00002015 DEBUGP("unable to map target of rule for chain `%s'\n", chain);
2016 free(r);
2017 return 0;
Patrick McHardyJesper Brouer04a1e4c2006-07-25 01:50:48 +00002018 } else {
2019 /* iptcc_map_target increment target chain references
2020 * since this is a fake rule only used for matching
2021 * the chain references count is decremented again.
2022 */
2023 if (r->type == IPTCC_R_JUMP
2024 && r->jump)
2025 r->jump->references--;
Rusty Russelle45c7132004-12-16 13:21:44 +00002026 }
Harald Welte0113fe72004-01-06 19:04:02 +00002027
Rusty Russelle45c7132004-12-16 13:21:44 +00002028 list_for_each_entry(i, &c->rules, list) {
Rusty Russell733e54b2004-12-16 14:22:23 +00002029 unsigned char *mask;
Harald Weltefe537072004-08-30 20:28:53 +00002030
Rusty Russell733e54b2004-12-16 14:22:23 +00002031 mask = is_same(r->entry, i->entry, matchmask);
2032 if (!mask)
2033 continue;
Martin Josefsson2a5dbbb2004-09-22 21:37:41 +00002034
Rusty Russell733e54b2004-12-16 14:22:23 +00002035 if (!target_same(r, i, mask))
2036 continue;
2037
2038 /* If we are about to delete the rule that is the
2039 * current iterator, move rule iterator back. next
2040 * pointer will then point to real next node */
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002041 if (i == handle->rule_iterator_cur) {
2042 handle->rule_iterator_cur =
2043 list_entry(handle->rule_iterator_cur->list.prev,
Rusty Russell733e54b2004-12-16 14:22:23 +00002044 struct rule_head, list);
Marc Bouchere6869a82000-03-20 06:03:29 +00002045 }
Rusty Russell733e54b2004-12-16 14:22:23 +00002046
2047 c->num_rules--;
2048 iptcc_delete_rule(i);
2049
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002050 set_changed(handle);
Rusty Russell733e54b2004-12-16 14:22:23 +00002051 free(r);
2052 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00002053 }
2054
Rusty Russelle45c7132004-12-16 13:21:44 +00002055 free(r);
Marc Bouchere6869a82000-03-20 06:03:29 +00002056 errno = ENOENT;
2057 return 0;
Rusty Russell7e53bf92000-03-20 07:03:28 +00002058}
Harald Welteaae69be2004-08-29 23:32:14 +00002059
Marc Bouchere6869a82000-03-20 06:03:29 +00002060
2061/* Delete the rule in position `rulenum' in `chain'. */
2062int
Rusty Russell79dee072000-05-02 16:45:16 +00002063TC_DELETE_NUM_ENTRY(const IPT_CHAINLABEL chain,
2064 unsigned int rulenum,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002065 struct xtc_handle *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00002066{
Harald Welteaae69be2004-08-29 23:32:14 +00002067 struct chain_head *c;
2068 struct rule_head *r;
Marc Bouchere6869a82000-03-20 06:03:29 +00002069
Rusty Russell79dee072000-05-02 16:45:16 +00002070 iptc_fn = TC_DELETE_NUM_ENTRY;
Harald Welteaae69be2004-08-29 23:32:14 +00002071
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002072 if (!(c = iptcc_find_label(chain, handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002073 errno = ENOENT;
2074 return 0;
2075 }
2076
Martin Josefssona5616dc2004-10-24 22:27:31 +00002077 if (rulenum >= c->num_rules) {
Martin Josefsson631f3612004-09-23 19:25:06 +00002078 errno = E2BIG;
2079 return 0;
2080 }
2081
2082 /* Take advantage of the double linked list if possible. */
Martin Josefssona5616dc2004-10-24 22:27:31 +00002083 if (rulenum + 1 <= c->num_rules/2) {
2084 r = iptcc_get_rule_num(c, rulenum + 1);
2085 } else {
2086 r = iptcc_get_rule_num_reverse(c, c->num_rules - rulenum);
Marc Bouchere6869a82000-03-20 06:03:29 +00002087 }
2088
Harald Welteaae69be2004-08-29 23:32:14 +00002089 /* If we are about to delete the rule that is the current
2090 * iterator, move rule iterator back. next pointer will then
2091 * point to real next node */
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002092 if (r == handle->rule_iterator_cur) {
2093 handle->rule_iterator_cur =
2094 list_entry(handle->rule_iterator_cur->list.prev,
Harald Welteaae69be2004-08-29 23:32:14 +00002095 struct rule_head, list);
Harald Welte0113fe72004-01-06 19:04:02 +00002096 }
Marc Bouchere6869a82000-03-20 06:03:29 +00002097
Harald Welteaae69be2004-08-29 23:32:14 +00002098 c->num_rules--;
2099 iptcc_delete_rule(r);
2100
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002101 set_changed(handle);
Martin Josefsson2a5dbbb2004-09-22 21:37:41 +00002102
Harald Welteaae69be2004-08-29 23:32:14 +00002103 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00002104}
2105
Marc Bouchere6869a82000-03-20 06:03:29 +00002106/* Flushes the entries in the given chain (ie. empties chain). */
2107int
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002108TC_FLUSH_ENTRIES(const IPT_CHAINLABEL chain, struct xtc_handle *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00002109{
Harald Welteaae69be2004-08-29 23:32:14 +00002110 struct chain_head *c;
2111 struct rule_head *r, *tmp;
Marc Bouchere6869a82000-03-20 06:03:29 +00002112
Harald Welte0113fe72004-01-06 19:04:02 +00002113 iptc_fn = TC_FLUSH_ENTRIES;
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002114 if (!(c = iptcc_find_label(chain, handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002115 errno = ENOENT;
2116 return 0;
2117 }
Marc Bouchere6869a82000-03-20 06:03:29 +00002118
Harald Welteaae69be2004-08-29 23:32:14 +00002119 list_for_each_entry_safe(r, tmp, &c->rules, list) {
2120 iptcc_delete_rule(r);
2121 }
2122
2123 c->num_rules = 0;
2124
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002125 set_changed(handle);
Harald Welteaae69be2004-08-29 23:32:14 +00002126
2127 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00002128}
2129
2130/* Zeroes the counters in a chain. */
2131int
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002132TC_ZERO_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;
Rusty Russell7e53bf92000-03-20 07:03:28 +00002136
Derrik Pates664c0a32005-02-01 13:28:14 +00002137 iptc_fn = TC_ZERO_ENTRIES;
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002138 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
Andy Gaye5bd1d72006-08-22 02:56:41 +00002143 if (c->counter_map.maptype == COUNTER_MAP_NORMAL_MAP)
2144 c->counter_map.maptype = COUNTER_MAP_ZEROED;
2145
Harald Welteaae69be2004-08-29 23:32:14 +00002146 list_for_each_entry(r, &c->rules, list) {
2147 if (r->counter_map.maptype == COUNTER_MAP_NORMAL_MAP)
2148 r->counter_map.maptype = COUNTER_MAP_ZEROED;
Marc Bouchere6869a82000-03-20 06:03:29 +00002149 }
Harald Welteaae69be2004-08-29 23:32:14 +00002150
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002151 set_changed(handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00002152
Marc Bouchere6869a82000-03-20 06:03:29 +00002153 return 1;
2154}
2155
Harald Welte1cef74d2001-01-05 15:22:59 +00002156STRUCT_COUNTERS *
2157TC_READ_COUNTER(const IPT_CHAINLABEL chain,
2158 unsigned int rulenum,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002159 struct xtc_handle *handle)
Harald Welte1cef74d2001-01-05 15:22:59 +00002160{
Harald Welteaae69be2004-08-29 23:32:14 +00002161 struct chain_head *c;
2162 struct rule_head *r;
Harald Welte1cef74d2001-01-05 15:22:59 +00002163
2164 iptc_fn = TC_READ_COUNTER;
2165 CHECK(*handle);
2166
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002167 if (!(c = iptcc_find_label(chain, handle))) {
Harald Welte1cef74d2001-01-05 15:22:59 +00002168 errno = ENOENT;
2169 return NULL;
2170 }
2171
Harald Welteaae69be2004-08-29 23:32:14 +00002172 if (!(r = iptcc_get_rule_num(c, rulenum))) {
Harald Welte0113fe72004-01-06 19:04:02 +00002173 errno = E2BIG;
2174 return NULL;
2175 }
2176
Harald Welteaae69be2004-08-29 23:32:14 +00002177 return &r->entry[0].counters;
Harald Welte1cef74d2001-01-05 15:22:59 +00002178}
2179
2180int
2181TC_ZERO_COUNTER(const IPT_CHAINLABEL chain,
2182 unsigned int rulenum,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +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_ZERO_COUNTER;
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002189 CHECK(handle);
Harald Welte1cef74d2001-01-05 15:22:59 +00002190
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002191 if (!(c = iptcc_find_label(chain, handle))) {
Harald Welte1cef74d2001-01-05 15:22:59 +00002192 errno = ENOENT;
2193 return 0;
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 0;
2199 }
2200
Harald Welteaae69be2004-08-29 23:32:14 +00002201 if (r->counter_map.maptype == COUNTER_MAP_NORMAL_MAP)
2202 r->counter_map.maptype = COUNTER_MAP_ZEROED;
Harald Welte1cef74d2001-01-05 15:22:59 +00002203
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002204 set_changed(handle);
Harald Welte1cef74d2001-01-05 15:22:59 +00002205
2206 return 1;
2207}
2208
2209int
2210TC_SET_COUNTER(const IPT_CHAINLABEL chain,
2211 unsigned int rulenum,
2212 STRUCT_COUNTERS *counters,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002213 struct xtc_handle *handle)
Harald Welte1cef74d2001-01-05 15:22:59 +00002214{
Harald Welteaae69be2004-08-29 23:32:14 +00002215 struct chain_head *c;
2216 struct rule_head *r;
Harald Welte1cef74d2001-01-05 15:22:59 +00002217 STRUCT_ENTRY *e;
Harald Welte1cef74d2001-01-05 15:22:59 +00002218
2219 iptc_fn = TC_SET_COUNTER;
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002220 CHECK(handle);
Harald Welte1cef74d2001-01-05 15:22:59 +00002221
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002222 if (!(c = iptcc_find_label(chain, handle))) {
Harald Welte1cef74d2001-01-05 15:22:59 +00002223 errno = ENOENT;
2224 return 0;
2225 }
Harald Welte0113fe72004-01-06 19:04:02 +00002226
Harald Welteaae69be2004-08-29 23:32:14 +00002227 if (!(r = iptcc_get_rule_num(c, rulenum))) {
Harald Welte0113fe72004-01-06 19:04:02 +00002228 errno = E2BIG;
2229 return 0;
2230 }
2231
Harald Welteaae69be2004-08-29 23:32:14 +00002232 e = r->entry;
2233 r->counter_map.maptype = COUNTER_MAP_SET;
Harald Welte0113fe72004-01-06 19:04:02 +00002234
2235 memcpy(&e->counters, counters, sizeof(STRUCT_COUNTERS));
Harald Welte1cef74d2001-01-05 15:22:59 +00002236
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002237 set_changed(handle);
Harald Welte1cef74d2001-01-05 15:22:59 +00002238
2239 return 1;
2240}
2241
Marc Bouchere6869a82000-03-20 06:03:29 +00002242/* Creates a new chain. */
2243/* To create a chain, create two rules: error node and unconditional
2244 * return. */
2245int
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002246TC_CREATE_CHAIN(const IPT_CHAINLABEL chain, struct xtc_handle *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00002247{
Harald Welteaae69be2004-08-29 23:32:14 +00002248 static struct chain_head *c;
Patrick McHardy1f23d3c2008-06-07 15:04:34 +02002249 int capacity;
2250 int exceeded;
Marc Bouchere6869a82000-03-20 06:03:29 +00002251
Rusty Russell79dee072000-05-02 16:45:16 +00002252 iptc_fn = TC_CREATE_CHAIN;
Marc Bouchere6869a82000-03-20 06:03:29 +00002253
2254 /* find_label doesn't cover built-in targets: DROP, ACCEPT,
2255 QUEUE, RETURN. */
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002256 if (iptcc_find_label(chain, handle)
Rusty Russell79dee072000-05-02 16:45:16 +00002257 || strcmp(chain, LABEL_DROP) == 0
2258 || strcmp(chain, LABEL_ACCEPT) == 0
Rusty Russell67088e72000-05-10 01:18:57 +00002259 || strcmp(chain, LABEL_QUEUE) == 0
Rusty Russell79dee072000-05-02 16:45:16 +00002260 || strcmp(chain, LABEL_RETURN) == 0) {
Harald Welteaae69be2004-08-29 23:32:14 +00002261 DEBUGP("Chain `%s' already exists\n", chain);
Marc Bouchere6869a82000-03-20 06:03:29 +00002262 errno = EEXIST;
2263 return 0;
2264 }
2265
Rusty Russell79dee072000-05-02 16:45:16 +00002266 if (strlen(chain)+1 > sizeof(IPT_CHAINLABEL)) {
Harald Welteaae69be2004-08-29 23:32:14 +00002267 DEBUGP("Chain name `%s' too long\n", chain);
Marc Bouchere6869a82000-03-20 06:03:29 +00002268 errno = EINVAL;
2269 return 0;
2270 }
2271
Harald Welteaae69be2004-08-29 23:32:14 +00002272 c = iptcc_alloc_chain_head(chain, 0);
2273 if (!c) {
2274 DEBUGP("Cannot allocate memory for chain `%s'\n", chain);
2275 errno = ENOMEM;
2276 return 0;
Marc Bouchere6869a82000-03-20 06:03:29 +00002277
Harald Welteaae69be2004-08-29 23:32:14 +00002278 }
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002279 handle->num_chains++; /* New user defined chain */
Marc Bouchere6869a82000-03-20 06:03:29 +00002280
Harald Welteaae69be2004-08-29 23:32:14 +00002281 DEBUGP("Creating chain `%s'\n", chain);
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002282 iptc_insert_chain(handle, c); /* Insert sorted */
Harald Welte0113fe72004-01-06 19:04:02 +00002283
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +00002284 /* Inserting chains don't change the correctness of the chain
2285 * index (except if its smaller than index[0], but that
2286 * handled by iptc_insert_chain). It only causes longer lists
2287 * in the buckets. Thus, only rebuild chain index when the
2288 * capacity is exceed with CHAIN_INDEX_INSERT_MAX chains.
2289 */
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002290 capacity = handle->chain_index_sz * CHAIN_INDEX_BUCKET_LEN;
2291 exceeded = handle->num_chains - capacity;
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +00002292 if (exceeded > CHAIN_INDEX_INSERT_MAX) {
2293 debug("Capacity(%d) exceeded(%d) rebuild (chains:%d)\n",
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002294 capacity, exceeded, handle->num_chains);
2295 iptcc_chain_index_rebuild(handle);
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +00002296 }
2297
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002298 set_changed(handle);
Harald Welte0113fe72004-01-06 19:04:02 +00002299
Harald Welteaae69be2004-08-29 23:32:14 +00002300 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00002301}
2302
2303/* Get the number of references to this chain. */
2304int
Rusty Russell79dee072000-05-02 16:45:16 +00002305TC_GET_REFERENCES(unsigned int *ref, const IPT_CHAINLABEL chain,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002306 struct xtc_handle *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00002307{
Harald Welteaae69be2004-08-29 23:32:14 +00002308 struct chain_head *c;
Marc Bouchere6869a82000-03-20 06:03:29 +00002309
Derrik Pates664c0a32005-02-01 13:28:14 +00002310 iptc_fn = TC_GET_REFERENCES;
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002311 if (!(c = iptcc_find_label(chain, handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002312 errno = ENOENT;
2313 return 0;
2314 }
2315
Harald Welteaae69be2004-08-29 23:32:14 +00002316 *ref = c->references;
2317
Marc Bouchere6869a82000-03-20 06:03:29 +00002318 return 1;
2319}
2320
2321/* Deletes a chain. */
2322int
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002323TC_DELETE_CHAIN(const IPT_CHAINLABEL chain, struct xtc_handle *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00002324{
Marc Bouchere6869a82000-03-20 06:03:29 +00002325 unsigned int references;
Harald Welteaae69be2004-08-29 23:32:14 +00002326 struct chain_head *c;
Rusty Russell7e53bf92000-03-20 07:03:28 +00002327
Rusty Russell79dee072000-05-02 16:45:16 +00002328 iptc_fn = TC_DELETE_CHAIN;
Marc Bouchere6869a82000-03-20 06:03:29 +00002329
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002330 if (!(c = iptcc_find_label(chain, handle))) {
Harald Welteaae69be2004-08-29 23:32:14 +00002331 DEBUGP("cannot find chain `%s'\n", chain);
Marc Bouchere6869a82000-03-20 06:03:29 +00002332 errno = ENOENT;
2333 return 0;
2334 }
2335
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002336 if (TC_BUILTIN(chain, handle)) {
Harald Welteaae69be2004-08-29 23:32:14 +00002337 DEBUGP("cannot remove builtin chain `%s'\n", chain);
2338 errno = EINVAL;
2339 return 0;
2340 }
2341
2342 if (!TC_GET_REFERENCES(&references, chain, handle)) {
2343 DEBUGP("cannot get references on chain `%s'\n", chain);
2344 return 0;
2345 }
2346
2347 if (references > 0) {
2348 DEBUGP("chain `%s' still has references\n", chain);
2349 errno = EMLINK;
2350 return 0;
2351 }
2352
2353 if (c->num_rules) {
2354 DEBUGP("chain `%s' is not empty\n", chain);
Marc Bouchere6869a82000-03-20 06:03:29 +00002355 errno = ENOTEMPTY;
2356 return 0;
2357 }
2358
Harald Welteaae69be2004-08-29 23:32:14 +00002359 /* If we are about to delete the chain that is the current
Jesper Dangaard Brouer48bde402008-01-15 17:06:48 +00002360 * iterator, move chain iterator forward. */
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002361 if (c == handle->chain_iterator_cur)
2362 iptcc_chain_iterator_advance(handle);
Harald Welte0113fe72004-01-06 19:04:02 +00002363
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002364 handle->num_chains--; /* One user defined chain deleted */
Jesper Dangaard Brouer48bde402008-01-15 17:06:48 +00002365
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +00002366 //list_del(&c->list); /* Done in iptcc_chain_index_delete_chain() */
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002367 iptcc_chain_index_delete_chain(c, handle);
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +00002368 free(c);
2369
Harald Welteaae69be2004-08-29 23:32:14 +00002370 DEBUGP("chain `%s' deleted\n", chain);
2371
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002372 set_changed(handle);
Harald Welteaae69be2004-08-29 23:32:14 +00002373
2374 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00002375}
2376
2377/* Renames a chain. */
Rusty Russell79dee072000-05-02 16:45:16 +00002378int TC_RENAME_CHAIN(const IPT_CHAINLABEL oldname,
2379 const IPT_CHAINLABEL newname,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002380 struct xtc_handle *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00002381{
Harald Welteaae69be2004-08-29 23:32:14 +00002382 struct chain_head *c;
Rusty Russell79dee072000-05-02 16:45:16 +00002383 iptc_fn = TC_RENAME_CHAIN;
Marc Bouchere6869a82000-03-20 06:03:29 +00002384
Harald Welte1de80462000-10-30 12:00:27 +00002385 /* find_label doesn't cover built-in targets: DROP, ACCEPT,
2386 QUEUE, RETURN. */
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002387 if (iptcc_find_label(newname, handle)
Rusty Russell79dee072000-05-02 16:45:16 +00002388 || strcmp(newname, LABEL_DROP) == 0
2389 || strcmp(newname, LABEL_ACCEPT) == 0
Harald Welte1de80462000-10-30 12:00:27 +00002390 || strcmp(newname, LABEL_QUEUE) == 0
Rusty Russell79dee072000-05-02 16:45:16 +00002391 || strcmp(newname, LABEL_RETURN) == 0) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002392 errno = EEXIST;
2393 return 0;
2394 }
2395
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002396 if (!(c = iptcc_find_label(oldname, handle))
2397 || TC_BUILTIN(oldname, handle)) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002398 errno = ENOENT;
2399 return 0;
2400 }
2401
Rusty Russell79dee072000-05-02 16:45:16 +00002402 if (strlen(newname)+1 > sizeof(IPT_CHAINLABEL)) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002403 errno = EINVAL;
2404 return 0;
2405 }
2406
Harald Welteaae69be2004-08-29 23:32:14 +00002407 strncpy(c->name, newname, sizeof(IPT_CHAINLABEL));
2408
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002409 set_changed(handle);
Harald Welte0113fe72004-01-06 19:04:02 +00002410
Marc Bouchere6869a82000-03-20 06:03:29 +00002411 return 1;
2412}
2413
2414/* Sets the policy on a built-in chain. */
2415int
Rusty Russell79dee072000-05-02 16:45:16 +00002416TC_SET_POLICY(const IPT_CHAINLABEL chain,
2417 const IPT_CHAINLABEL policy,
Harald Welte1cef74d2001-01-05 15:22:59 +00002418 STRUCT_COUNTERS *counters,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002419 struct xtc_handle *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00002420{
Harald Welteaae69be2004-08-29 23:32:14 +00002421 struct chain_head *c;
Marc Bouchere6869a82000-03-20 06:03:29 +00002422
Rusty Russell79dee072000-05-02 16:45:16 +00002423 iptc_fn = TC_SET_POLICY;
Marc Bouchere6869a82000-03-20 06:03:29 +00002424
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002425 if (!(c = iptcc_find_label(chain, handle))) {
Harald Welteaae69be2004-08-29 23:32:14 +00002426 DEBUGP("cannot find chain `%s'\n", chain);
2427 errno = ENOENT;
Marc Bouchere6869a82000-03-20 06:03:29 +00002428 return 0;
2429 }
2430
Harald Welteaae69be2004-08-29 23:32:14 +00002431 if (!iptcc_is_builtin(c)) {
2432 DEBUGP("cannot set policy of userdefinedchain `%s'\n", chain);
2433 errno = ENOENT;
2434 return 0;
2435 }
Marc Bouchere6869a82000-03-20 06:03:29 +00002436
Rusty Russell79dee072000-05-02 16:45:16 +00002437 if (strcmp(policy, LABEL_ACCEPT) == 0)
Harald Welteaae69be2004-08-29 23:32:14 +00002438 c->verdict = -NF_ACCEPT - 1;
Rusty Russell79dee072000-05-02 16:45:16 +00002439 else if (strcmp(policy, LABEL_DROP) == 0)
Harald Welteaae69be2004-08-29 23:32:14 +00002440 c->verdict = -NF_DROP - 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00002441 else {
2442 errno = EINVAL;
2443 return 0;
2444 }
Harald Welte1cef74d2001-01-05 15:22:59 +00002445
Harald Welte1cef74d2001-01-05 15:22:59 +00002446 if (counters) {
2447 /* set byte and packet counters */
Harald Welteaae69be2004-08-29 23:32:14 +00002448 memcpy(&c->counters, counters, sizeof(STRUCT_COUNTERS));
2449 c->counter_map.maptype = COUNTER_MAP_SET;
Harald Welte1cef74d2001-01-05 15:22:59 +00002450 } else {
Harald Welteaae69be2004-08-29 23:32:14 +00002451 c->counter_map.maptype = COUNTER_MAP_NOMAP;
Harald Welte1cef74d2001-01-05 15:22:59 +00002452 }
2453
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002454 set_changed(handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00002455
Marc Bouchere6869a82000-03-20 06:03:29 +00002456 return 1;
2457}
2458
2459/* Without this, on gcc 2.7.2.3, we get:
Rusty Russell79dee072000-05-02 16:45:16 +00002460 libiptc.c: In function `TC_COMMIT':
Marc Bouchere6869a82000-03-20 06:03:29 +00002461 libiptc.c:833: fixed or forbidden register was spilled.
2462 This may be due to a compiler bug or to impossible asm
2463 statements or clauses.
2464*/
2465static void
Rusty Russell79dee072000-05-02 16:45:16 +00002466subtract_counters(STRUCT_COUNTERS *answer,
2467 const STRUCT_COUNTERS *a,
2468 const STRUCT_COUNTERS *b)
Marc Bouchere6869a82000-03-20 06:03:29 +00002469{
2470 answer->pcnt = a->pcnt - b->pcnt;
2471 answer->bcnt = a->bcnt - b->bcnt;
2472}
2473
Harald Welteaae69be2004-08-29 23:32:14 +00002474
Jan Engelhardtdbb77542008-02-11 00:33:30 +01002475static void counters_nomap(STRUCT_COUNTERS_INFO *newcounters, unsigned int idx)
Harald Welteaae69be2004-08-29 23:32:14 +00002476{
Jan Engelhardtdbb77542008-02-11 00:33:30 +01002477 newcounters->counters[idx] = ((STRUCT_COUNTERS) { 0, 0});
Harald Welteaae69be2004-08-29 23:32:14 +00002478 DEBUGP_C("NOMAP => zero\n");
2479}
2480
2481static void counters_normal_map(STRUCT_COUNTERS_INFO *newcounters,
Jan Engelhardtdbb77542008-02-11 00:33:30 +01002482 STRUCT_REPLACE *repl, unsigned int idx,
Harald Welteaae69be2004-08-29 23:32:14 +00002483 unsigned int mappos)
2484{
2485 /* Original read: X.
2486 * Atomic read on replacement: X + Y.
2487 * Currently in kernel: Z.
2488 * Want in kernel: X + Y + Z.
2489 * => Add in X + Y
2490 * => Add in replacement read.
2491 */
Jan Engelhardtdbb77542008-02-11 00:33:30 +01002492 newcounters->counters[idx] = repl->counters[mappos];
Harald Welteaae69be2004-08-29 23:32:14 +00002493 DEBUGP_C("NORMAL_MAP => mappos %u \n", mappos);
2494}
2495
2496static void counters_map_zeroed(STRUCT_COUNTERS_INFO *newcounters,
Jan Engelhardtdbb77542008-02-11 00:33:30 +01002497 STRUCT_REPLACE *repl, unsigned int idx,
2498 unsigned int mappos, STRUCT_COUNTERS *counters)
Harald Welteaae69be2004-08-29 23:32:14 +00002499{
2500 /* Original read: X.
2501 * Atomic read on replacement: X + Y.
2502 * Currently in kernel: Z.
2503 * Want in kernel: Y + Z.
2504 * => Add in Y.
2505 * => Add in (replacement read - original read).
2506 */
Jan Engelhardtdbb77542008-02-11 00:33:30 +01002507 subtract_counters(&newcounters->counters[idx],
Harald Welteaae69be2004-08-29 23:32:14 +00002508 &repl->counters[mappos],
2509 counters);
2510 DEBUGP_C("ZEROED => mappos %u\n", mappos);
2511}
2512
2513static void counters_map_set(STRUCT_COUNTERS_INFO *newcounters,
Jan Engelhardtdbb77542008-02-11 00:33:30 +01002514 unsigned int idx, STRUCT_COUNTERS *counters)
Harald Welteaae69be2004-08-29 23:32:14 +00002515{
2516 /* Want to set counter (iptables-restore) */
2517
Jan Engelhardtdbb77542008-02-11 00:33:30 +01002518 memcpy(&newcounters->counters[idx], counters,
Harald Welteaae69be2004-08-29 23:32:14 +00002519 sizeof(STRUCT_COUNTERS));
2520
2521 DEBUGP_C("SET\n");
2522}
2523
2524
Marc Bouchere6869a82000-03-20 06:03:29 +00002525int
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002526TC_COMMIT(struct xtc_handle *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00002527{
2528 /* Replace, then map back the counters. */
Rusty Russell79dee072000-05-02 16:45:16 +00002529 STRUCT_REPLACE *repl;
2530 STRUCT_COUNTERS_INFO *newcounters;
Harald Welteaae69be2004-08-29 23:32:14 +00002531 struct chain_head *c;
2532 int ret;
Martin Josefsson841e4ae2003-05-02 15:30:11 +00002533 size_t counterlen;
Harald Welteaae69be2004-08-29 23:32:14 +00002534 int new_number;
2535 unsigned int new_size;
Marc Bouchere6869a82000-03-20 06:03:29 +00002536
Derrik Pates664c0a32005-02-01 13:28:14 +00002537 iptc_fn = TC_COMMIT;
Marc Bouchere6869a82000-03-20 06:03:29 +00002538 CHECK(*handle);
Martin Josefsson841e4ae2003-05-02 15:30:11 +00002539
Marc Bouchere6869a82000-03-20 06:03:29 +00002540 /* Don't commit if nothing changed. */
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002541 if (!handle->changed)
Marc Bouchere6869a82000-03-20 06:03:29 +00002542 goto finished;
2543
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002544 new_number = iptcc_compile_table_prep(handle, &new_size);
Harald Welteaae69be2004-08-29 23:32:14 +00002545 if (new_number < 0) {
2546 errno = ENOMEM;
Harald Welted6ba6f52005-11-12 10:39:40 +00002547 goto out_zero;
Harald Welteaae69be2004-08-29 23:32:14 +00002548 }
2549
2550 repl = malloc(sizeof(*repl) + new_size);
Marc Bouchere6869a82000-03-20 06:03:29 +00002551 if (!repl) {
2552 errno = ENOMEM;
Harald Welted6ba6f52005-11-12 10:39:40 +00002553 goto out_zero;
Marc Bouchere6869a82000-03-20 06:03:29 +00002554 }
Martin Josefssonad3b4f92004-09-22 22:04:07 +00002555 memset(repl, 0, sizeof(*repl) + new_size);
Harald Welteaae69be2004-08-29 23:32:14 +00002556
Rusty Russelle45c7132004-12-16 13:21:44 +00002557#if 0
2558 TC_DUMP_ENTRIES(*handle);
2559#endif
2560
Harald Welteaae69be2004-08-29 23:32:14 +00002561 counterlen = sizeof(STRUCT_COUNTERS_INFO)
2562 + sizeof(STRUCT_COUNTERS) * new_number;
Marc Bouchere6869a82000-03-20 06:03:29 +00002563
2564 /* These are the old counters we will get from kernel */
Rusty Russell79dee072000-05-02 16:45:16 +00002565 repl->counters = malloc(sizeof(STRUCT_COUNTERS)
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002566 * handle->info.num_entries);
Marc Bouchere6869a82000-03-20 06:03:29 +00002567 if (!repl->counters) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002568 errno = ENOMEM;
Harald Welted6ba6f52005-11-12 10:39:40 +00002569 goto out_free_repl;
Marc Bouchere6869a82000-03-20 06:03:29 +00002570 }
Marc Bouchere6869a82000-03-20 06:03:29 +00002571 /* These are the counters we're going to put back, later. */
2572 newcounters = malloc(counterlen);
2573 if (!newcounters) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002574 errno = ENOMEM;
Harald Welted6ba6f52005-11-12 10:39:40 +00002575 goto out_free_repl_counters;
Marc Bouchere6869a82000-03-20 06:03:29 +00002576 }
Harald Welteaae69be2004-08-29 23:32:14 +00002577 memset(newcounters, 0, counterlen);
Marc Bouchere6869a82000-03-20 06:03:29 +00002578
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002579 strcpy(repl->name, handle->info.name);
Harald Welteaae69be2004-08-29 23:32:14 +00002580 repl->num_entries = new_number;
2581 repl->size = new_size;
2582
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002583 repl->num_counters = handle->info.num_entries;
2584 repl->valid_hooks = handle->info.valid_hooks;
Harald Welteaae69be2004-08-29 23:32:14 +00002585
2586 DEBUGP("num_entries=%u, size=%u, num_counters=%u\n",
2587 repl->num_entries, repl->size, repl->num_counters);
2588
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002589 ret = iptcc_compile_table(handle, repl);
Harald Welteaae69be2004-08-29 23:32:14 +00002590 if (ret < 0) {
2591 errno = ret;
Harald Welted6ba6f52005-11-12 10:39:40 +00002592 goto out_free_newcounters;
Harald Welteaae69be2004-08-29 23:32:14 +00002593 }
2594
2595
2596#ifdef IPTC_DEBUG2
2597 {
2598 int fd = open("/tmp/libiptc-so_set_replace.blob",
2599 O_CREAT|O_WRONLY);
2600 if (fd >= 0) {
2601 write(fd, repl, sizeof(*repl) + repl->size);
2602 close(fd);
2603 }
2604 }
2605#endif
Marc Bouchere6869a82000-03-20 06:03:29 +00002606
Jan Engelhardt175f4512008-11-10 17:25:55 +01002607 ret = setsockopt(handle->sockfd, TC_IPPROTO, SO_SET_REPLACE, repl,
Harald Welted6ba6f52005-11-12 10:39:40 +00002608 sizeof(*repl) + repl->size);
Patrick McHardye0865ad2006-04-22 02:08:56 +00002609 if (ret < 0)
Harald Welted6ba6f52005-11-12 10:39:40 +00002610 goto out_free_newcounters;
Marc Bouchere6869a82000-03-20 06:03:29 +00002611
2612 /* Put counters back. */
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002613 strcpy(newcounters->name, handle->info.name);
Harald Welteaae69be2004-08-29 23:32:14 +00002614 newcounters->num_counters = new_number;
Marc Bouchere6869a82000-03-20 06:03:29 +00002615
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002616 list_for_each_entry(c, &handle->chains, list) {
Harald Welteaae69be2004-08-29 23:32:14 +00002617 struct rule_head *r;
Marc Bouchere6869a82000-03-20 06:03:29 +00002618
Harald Welteaae69be2004-08-29 23:32:14 +00002619 /* Builtin chains have their own counters */
2620 if (iptcc_is_builtin(c)) {
2621 DEBUGP("counter for chain-index %u: ", c->foot_index);
2622 switch(c->counter_map.maptype) {
2623 case COUNTER_MAP_NOMAP:
2624 counters_nomap(newcounters, c->foot_index);
2625 break;
2626 case COUNTER_MAP_NORMAL_MAP:
2627 counters_normal_map(newcounters, repl,
2628 c->foot_index,
2629 c->counter_map.mappos);
2630 break;
2631 case COUNTER_MAP_ZEROED:
2632 counters_map_zeroed(newcounters, repl,
2633 c->foot_index,
2634 c->counter_map.mappos,
2635 &c->counters);
2636 break;
2637 case COUNTER_MAP_SET:
2638 counters_map_set(newcounters, c->foot_index,
2639 &c->counters);
2640 break;
2641 }
2642 }
Harald Welte1cef74d2001-01-05 15:22:59 +00002643
Harald Welteaae69be2004-08-29 23:32:14 +00002644 list_for_each_entry(r, &c->rules, list) {
2645 DEBUGP("counter for index %u: ", r->index);
2646 switch (r->counter_map.maptype) {
2647 case COUNTER_MAP_NOMAP:
2648 counters_nomap(newcounters, r->index);
2649 break;
Harald Welte1cef74d2001-01-05 15:22:59 +00002650
Harald Welteaae69be2004-08-29 23:32:14 +00002651 case COUNTER_MAP_NORMAL_MAP:
2652 counters_normal_map(newcounters, repl,
2653 r->index,
2654 r->counter_map.mappos);
2655 break;
Harald Welte1cef74d2001-01-05 15:22:59 +00002656
Harald Welteaae69be2004-08-29 23:32:14 +00002657 case COUNTER_MAP_ZEROED:
2658 counters_map_zeroed(newcounters, repl,
2659 r->index,
2660 r->counter_map.mappos,
2661 &r->entry->counters);
2662 break;
2663
2664 case COUNTER_MAP_SET:
2665 counters_map_set(newcounters, r->index,
2666 &r->entry->counters);
2667 break;
2668 }
Marc Bouchere6869a82000-03-20 06:03:29 +00002669 }
2670 }
Rusty Russell62527ce2000-09-04 09:45:54 +00002671
Harald Welteaae69be2004-08-29 23:32:14 +00002672#ifdef IPTC_DEBUG2
2673 {
2674 int fd = open("/tmp/libiptc-so_set_add_counters.blob",
2675 O_CREAT|O_WRONLY);
2676 if (fd >= 0) {
2677 write(fd, newcounters, counterlen);
2678 close(fd);
2679 }
2680 }
2681#endif
2682
Jan Engelhardt175f4512008-11-10 17:25:55 +01002683 ret = setsockopt(handle->sockfd, TC_IPPROTO, SO_SET_ADD_COUNTERS,
Harald Welted6ba6f52005-11-12 10:39:40 +00002684 newcounters, counterlen);
Patrick McHardye0865ad2006-04-22 02:08:56 +00002685 if (ret < 0)
Harald Welted6ba6f52005-11-12 10:39:40 +00002686 goto out_free_newcounters;
Marc Bouchere6869a82000-03-20 06:03:29 +00002687
2688 free(repl->counters);
2689 free(repl);
2690 free(newcounters);
2691
Harald Welted6ba6f52005-11-12 10:39:40 +00002692finished:
Marc Bouchere6869a82000-03-20 06:03:29 +00002693 return 1;
Harald Welted6ba6f52005-11-12 10:39:40 +00002694
2695out_free_newcounters:
2696 free(newcounters);
2697out_free_repl_counters:
2698 free(repl->counters);
2699out_free_repl:
2700 free(repl);
2701out_zero:
2702 return 0;
Marc Bouchere6869a82000-03-20 06:03:29 +00002703}
2704
Marc Bouchere6869a82000-03-20 06:03:29 +00002705/* Translates errno numbers into more human-readable form than strerror. */
2706const char *
Rusty Russell79dee072000-05-02 16:45:16 +00002707TC_STRERROR(int err)
Marc Bouchere6869a82000-03-20 06:03:29 +00002708{
2709 unsigned int i;
2710 struct table_struct {
2711 void *fn;
2712 int err;
2713 const char *message;
2714 } table [] =
Harald Welte4ccfa632001-07-30 15:12:43 +00002715 { { TC_INIT, EPERM, "Permission denied (you must be root)" },
Rusty Russell79dee072000-05-02 16:45:16 +00002716 { TC_INIT, EINVAL, "Module is wrong version" },
Harald Welte4ccfa632001-07-30 15:12:43 +00002717 { TC_INIT, ENOENT,
2718 "Table does not exist (do you need to insmod?)" },
Rusty Russell79dee072000-05-02 16:45:16 +00002719 { TC_DELETE_CHAIN, ENOTEMPTY, "Chain is not empty" },
2720 { TC_DELETE_CHAIN, EINVAL, "Can't delete built-in chain" },
2721 { TC_DELETE_CHAIN, EMLINK,
Marc Bouchere6869a82000-03-20 06:03:29 +00002722 "Can't delete chain with references left" },
Rusty Russell79dee072000-05-02 16:45:16 +00002723 { TC_CREATE_CHAIN, EEXIST, "Chain already exists" },
2724 { TC_INSERT_ENTRY, E2BIG, "Index of insertion too big" },
2725 { TC_REPLACE_ENTRY, E2BIG, "Index of replacement too big" },
2726 { TC_DELETE_NUM_ENTRY, E2BIG, "Index of deletion too big" },
Harald Welte1cef74d2001-01-05 15:22:59 +00002727 { TC_READ_COUNTER, E2BIG, "Index of counter too big" },
2728 { TC_ZERO_COUNTER, E2BIG, "Index of counter too big" },
Rusty Russell79dee072000-05-02 16:45:16 +00002729 { TC_INSERT_ENTRY, ELOOP, "Loop found in table" },
2730 { TC_INSERT_ENTRY, EINVAL, "Target problem" },
Marc Bouchere6869a82000-03-20 06:03:29 +00002731 /* ENOENT for DELETE probably means no matching rule */
Rusty Russell79dee072000-05-02 16:45:16 +00002732 { TC_DELETE_ENTRY, ENOENT,
Marc Boucherc8264992000-04-22 22:34:44 +00002733 "Bad rule (does a matching rule exist in that chain?)" },
Rusty Russell79dee072000-05-02 16:45:16 +00002734 { TC_SET_POLICY, ENOENT,
Marc Boucherc8264992000-04-22 22:34:44 +00002735 "Bad built-in chain name" },
Rusty Russell79dee072000-05-02 16:45:16 +00002736 { TC_SET_POLICY, EINVAL,
Marc Boucherc8264992000-04-22 22:34:44 +00002737 "Bad policy name" },
Harald Welte4ccfa632001-07-30 15:12:43 +00002738
2739 { NULL, 0, "Incompatible with this kernel" },
2740 { NULL, ENOPROTOOPT, "iptables who? (do you need to insmod?)" },
2741 { NULL, ENOSYS, "Will be implemented real soon. I promise ;)" },
2742 { NULL, ENOMEM, "Memory allocation problem" },
2743 { NULL, ENOENT, "No chain/target/match by that name" },
Marc Bouchere6869a82000-03-20 06:03:29 +00002744 };
2745
2746 for (i = 0; i < sizeof(table)/sizeof(struct table_struct); i++) {
2747 if ((!table[i].fn || table[i].fn == iptc_fn)
2748 && table[i].err == err)
2749 return table[i].message;
2750 }
2751
2752 return strerror(err);
2753}