blob: 0b6d5e3adf02c8e249701f09e94a527f5ef981b6 [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
Jesper Dangaard Brouera9fe5b32009-03-23 14:26:56 +010012 * 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
Jesper Dangaard Brouera9fe5b32009-03-23 14:26:56 +010020 * up after a ruleset change.
Harald Welteaae69be2004-08-29 23:32:14 +000021 * 2004-Aug-18: Harald Welte <laforge@netfilter.org>:
Jesper Dangaard Brouera9fe5b32009-03-23 14:26:56 +010022 * - further performance work: total reimplementation of libiptc.
Harald Welteaae69be2004-08-29 23:32:14 +000023 * - 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/)
Jesper Dangaard Brouerc9477d02009-03-23 14:27:44 +010026 *
27 * 2008-Jan+Jul: Jesper Dangaard Brouer <hawk@comx.dk>
28 * - performance work: speedup chain list "name" searching.
29 * - performance work: speedup initial ruleset parsing.
30 * - sponsored by ComX Networks A/S (http://www.comx.dk/)
Harald Welte3ea8f402003-06-23 18:25:59 +000031 */
Harald Welte15920d12004-05-16 09:05:07 +000032#include <sys/types.h>
33#include <sys/socket.h>
Stefan Tomanekd59b9db2011-03-08 22:42:51 +010034#include <stdbool.h>
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +020035#include <xtables.h>
Stephane Ouellette7cd00282004-05-14 08:21:06 +000036
Harald Welteaae69be2004-08-29 23:32:14 +000037#include "linux_list.h"
38
39//#define IPTC_DEBUG2 1
40
41#ifdef IPTC_DEBUG2
42#include <fcntl.h>
43#define DEBUGP(x, args...) fprintf(stderr, "%s: " x, __FUNCTION__, ## args)
44#define DEBUGP_C(x, args...) fprintf(stderr, x, ## args)
45#else
46#define DEBUGP(x, args...)
47#define DEBUGP_C(x, args...)
48#endif
49
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +000050#ifdef DEBUG
51#define debug(x, args...) fprintf(stderr, x, ## args)
52#else
53#define debug(x, args...)
54#endif
55
Marc Bouchere6869a82000-03-20 06:03:29 +000056static void *iptc_fn = NULL;
57
Patrick McHardy0b639362007-09-08 16:00:01 +000058static const char *hooknames[] = {
59 [HOOK_PRE_ROUTING] = "PREROUTING",
60 [HOOK_LOCAL_IN] = "INPUT",
61 [HOOK_FORWARD] = "FORWARD",
62 [HOOK_LOCAL_OUT] = "OUTPUT",
63 [HOOK_POST_ROUTING] = "POSTROUTING",
Rusty Russell10758b72000-09-14 07:37:33 +000064#ifdef HOOK_DROPPING
Patrick McHardy0b639362007-09-08 16:00:01 +000065 [HOOK_DROPPING] = "DROPPING"
Rusty Russell10758b72000-09-14 07:37:33 +000066#endif
Marc Bouchere6869a82000-03-20 06:03:29 +000067};
68
Harald Welteaae69be2004-08-29 23:32:14 +000069/* Convenience structures */
70struct ipt_error_target
71{
72 STRUCT_ENTRY_TARGET t;
73 char error[TABLE_MAXNAMELEN];
74};
75
76struct chain_head;
77struct rule_head;
78
Marc Bouchere6869a82000-03-20 06:03:29 +000079struct counter_map
80{
81 enum {
82 COUNTER_MAP_NOMAP,
83 COUNTER_MAP_NORMAL_MAP,
Harald Welte1cef74d2001-01-05 15:22:59 +000084 COUNTER_MAP_ZEROED,
85 COUNTER_MAP_SET
Marc Bouchere6869a82000-03-20 06:03:29 +000086 } maptype;
87 unsigned int mappos;
88};
89
Harald Welteaae69be2004-08-29 23:32:14 +000090enum iptcc_rule_type {
91 IPTCC_R_STANDARD, /* standard target (ACCEPT, ...) */
92 IPTCC_R_MODULE, /* extension module (SNAT, ...) */
93 IPTCC_R_FALLTHROUGH, /* fallthrough rule */
94 IPTCC_R_JUMP, /* jump to other chain */
Marc Bouchere6869a82000-03-20 06:03:29 +000095};
96
Harald Welteaae69be2004-08-29 23:32:14 +000097struct rule_head
Rusty Russell30fd6e52000-04-23 09:16:06 +000098{
Harald Welteaae69be2004-08-29 23:32:14 +000099 struct list_head list;
100 struct chain_head *chain;
101 struct counter_map counter_map;
102
103 unsigned int index; /* index (needed for counter_map) */
104 unsigned int offset; /* offset in rule blob */
105
106 enum iptcc_rule_type type;
107 struct chain_head *jump; /* jump target, if IPTCC_R_JUMP */
108
109 unsigned int size; /* size of entry data */
110 STRUCT_ENTRY entry[0];
111};
112
113struct chain_head
114{
115 struct list_head list;
Rusty Russell79dee072000-05-02 16:45:16 +0000116 char name[TABLE_MAXNAMELEN];
Harald Welteaae69be2004-08-29 23:32:14 +0000117 unsigned int hooknum; /* hook number+1 if builtin */
118 unsigned int references; /* how many jumps reference us */
119 int verdict; /* verdict if builtin */
120
121 STRUCT_COUNTERS counters; /* per-chain counters */
122 struct counter_map counter_map;
123
124 unsigned int num_rules; /* number of rules in list */
125 struct list_head rules; /* list of rules */
126
127 unsigned int index; /* index (needed for jump resolval) */
128 unsigned int head_offset; /* offset in rule blob */
129 unsigned int foot_index; /* index (needed for counter_map) */
130 unsigned int foot_offset; /* offset in rule blob */
Rusty Russell30fd6e52000-04-23 09:16:06 +0000131};
132
Rusty Russell79dee072000-05-02 16:45:16 +0000133STRUCT_TC_HANDLE
Marc Bouchere6869a82000-03-20 06:03:29 +0000134{
Jan Engelhardt175f4512008-11-10 17:25:55 +0100135 int sockfd;
Harald Welteaae69be2004-08-29 23:32:14 +0000136 int changed; /* Have changes been made? */
137
138 struct list_head chains;
Jesper Dangaard Brouera9fe5b32009-03-23 14:26:56 +0100139
Harald Welteaae69be2004-08-29 23:32:14 +0000140 struct chain_head *chain_iterator_cur;
141 struct rule_head *rule_iterator_cur;
142
Jesper Dangaard Brouer48bde402008-01-15 17:06:48 +0000143 unsigned int num_chains; /* number of user defined chains */
144
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000145 struct chain_head **chain_index; /* array for fast chain list access*/
146 unsigned int chain_index_sz;/* size of chain index array */
147
Jesper Dangaard Brouer4bae3f12008-07-03 20:31:42 +0200148 int sorted_offsets; /* if chains are received sorted from kernel,
149 * then the offsets are also sorted. Says if its
150 * possible to bsearch offsets using chain_index.
151 */
152
Rusty Russell79dee072000-05-02 16:45:16 +0000153 STRUCT_GETINFO info;
Harald Welteaae69be2004-08-29 23:32:14 +0000154 STRUCT_GET_ENTRIES *entries;
Marc Bouchere6869a82000-03-20 06:03:29 +0000155};
156
Jesper Dangaard Brouer4bae3f12008-07-03 20:31:42 +0200157enum bsearch_type {
158 BSEARCH_NAME, /* Binary search after chain name */
159 BSEARCH_OFFSET, /* Binary search based on offset */
160};
161
Harald Welteaae69be2004-08-29 23:32:14 +0000162/* allocate a new chain head for the cache */
163static struct chain_head *iptcc_alloc_chain_head(const char *name, int hooknum)
164{
165 struct chain_head *c = malloc(sizeof(*c));
166 if (!c)
167 return NULL;
168 memset(c, 0, sizeof(*c));
169
170 strncpy(c->name, name, TABLE_MAXNAMELEN);
171 c->hooknum = hooknum;
172 INIT_LIST_HEAD(&c->rules);
173
174 return c;
175}
176
177/* allocate and initialize a new rule for the cache */
178static struct rule_head *iptcc_alloc_rule(struct chain_head *c, unsigned int size)
179{
180 struct rule_head *r = malloc(sizeof(*r)+size);
181 if (!r)
182 return NULL;
183 memset(r, 0, sizeof(*r));
184
185 r->chain = c;
186 r->size = size;
187
188 return r;
189}
190
191/* notify us that the ruleset has been modified by the user */
Jesper Dangaard Brouer91093982008-01-15 17:01:58 +0000192static inline void
Jan Engelhardtfd187312008-11-10 16:59:27 +0100193set_changed(struct xtc_handle *h)
Rusty Russell175f6412000-03-24 09:32:20 +0000194{
Rusty Russell175f6412000-03-24 09:32:20 +0000195 h->changed = 1;
196}
197
Harald Welte380ba5f2002-02-13 16:19:55 +0000198#ifdef IPTC_DEBUG
Jan Engelhardtfd187312008-11-10 16:59:27 +0100199static void do_check(struct xtc_handle *h, unsigned int line);
Rusty Russell849779c2000-04-23 15:51:51 +0000200#define CHECK(h) do { if (!getenv("IPTC_NO_CHECK")) do_check((h), __LINE__); } while(0)
Rusty Russell30fd6e52000-04-23 09:16:06 +0000201#else
202#define CHECK(h)
203#endif
Marc Bouchere6869a82000-03-20 06:03:29 +0000204
Harald Welteaae69be2004-08-29 23:32:14 +0000205
206/**********************************************************************
207 * iptc blob utility functions (iptcb_*)
208 **********************************************************************/
209
Marc Bouchere6869a82000-03-20 06:03:29 +0000210static inline int
Harald Welteaae69be2004-08-29 23:32:14 +0000211iptcb_get_number(const STRUCT_ENTRY *i,
Rusty Russell79dee072000-05-02 16:45:16 +0000212 const STRUCT_ENTRY *seek,
Marc Bouchere6869a82000-03-20 06:03:29 +0000213 unsigned int *pos)
214{
215 if (i == seek)
216 return 1;
217 (*pos)++;
218 return 0;
219}
220
Marc Bouchere6869a82000-03-20 06:03:29 +0000221static inline int
Harald Welteaae69be2004-08-29 23:32:14 +0000222iptcb_get_entry_n(STRUCT_ENTRY *i,
Marc Bouchere6869a82000-03-20 06:03:29 +0000223 unsigned int number,
224 unsigned int *pos,
Rusty Russell79dee072000-05-02 16:45:16 +0000225 STRUCT_ENTRY **pe)
Marc Bouchere6869a82000-03-20 06:03:29 +0000226{
227 if (*pos == number) {
228 *pe = i;
229 return 1;
230 }
231 (*pos)++;
232 return 0;
233}
234
Harald Welteaae69be2004-08-29 23:32:14 +0000235static inline STRUCT_ENTRY *
Jan Engelhardtfd187312008-11-10 16:59:27 +0100236iptcb_get_entry(struct xtc_handle *h, unsigned int offset)
Harald Welteaae69be2004-08-29 23:32:14 +0000237{
238 return (STRUCT_ENTRY *)((char *)h->entries->entrytable + offset);
239}
240
241static unsigned int
Jan Engelhardtfd187312008-11-10 16:59:27 +0100242iptcb_entry2index(struct xtc_handle *const h, const STRUCT_ENTRY *seek)
Marc Bouchere6869a82000-03-20 06:03:29 +0000243{
244 unsigned int pos = 0;
Marc Bouchere6869a82000-03-20 06:03:29 +0000245
Harald Welteaae69be2004-08-29 23:32:14 +0000246 if (ENTRY_ITERATE(h->entries->entrytable, h->entries->size,
247 iptcb_get_number, seek, &pos) == 0) {
248 fprintf(stderr, "ERROR: offset %u not an entry!\n",
249 (unsigned int)((char *)seek - (char *)h->entries->entrytable));
250 abort();
251 }
252 return pos;
Marc Bouchere6869a82000-03-20 06:03:29 +0000253}
254
Harald Welte0113fe72004-01-06 19:04:02 +0000255static inline STRUCT_ENTRY *
Jan Engelhardtfd187312008-11-10 16:59:27 +0100256iptcb_offset2entry(struct xtc_handle *h, unsigned int offset)
Harald Welte0113fe72004-01-06 19:04:02 +0000257{
Harald Welteaae69be2004-08-29 23:32:14 +0000258 return (STRUCT_ENTRY *) ((void *)h->entries->entrytable+offset);
Harald Welte0113fe72004-01-06 19:04:02 +0000259}
260
Harald Welteaae69be2004-08-29 23:32:14 +0000261
Harald Welte0113fe72004-01-06 19:04:02 +0000262static inline unsigned long
Jan Engelhardtfd187312008-11-10 16:59:27 +0100263iptcb_entry2offset(struct xtc_handle *const h, const STRUCT_ENTRY *e)
Harald Welte0113fe72004-01-06 19:04:02 +0000264{
Harald Welteaae69be2004-08-29 23:32:14 +0000265 return (void *)e - (void *)h->entries->entrytable;
Harald Welte3ea8f402003-06-23 18:25:59 +0000266}
267
268static inline unsigned int
Jan Engelhardtfd187312008-11-10 16:59:27 +0100269iptcb_offset2index(struct xtc_handle *const h, unsigned int offset)
Harald Welte3ea8f402003-06-23 18:25:59 +0000270{
Harald Welteaae69be2004-08-29 23:32:14 +0000271 return iptcb_entry2index(h, iptcb_offset2entry(h, offset));
272}
273
274/* Returns 0 if not hook entry, else hooknumber + 1 */
275static inline unsigned int
Jan Engelhardtfd187312008-11-10 16:59:27 +0100276iptcb_ent_is_hook_entry(STRUCT_ENTRY *e, struct xtc_handle *h)
Harald Welteaae69be2004-08-29 23:32:14 +0000277{
278 unsigned int i;
279
280 for (i = 0; i < NUMHOOKS; i++) {
281 if ((h->info.valid_hooks & (1 << i))
282 && iptcb_get_entry(h, h->info.hook_entry[i]) == e)
283 return i+1;
284 }
285 return 0;
Harald Welte3ea8f402003-06-23 18:25:59 +0000286}
287
288
Harald Welteaae69be2004-08-29 23:32:14 +0000289/**********************************************************************
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000290 * Chain index (cache utility) functions
291 **********************************************************************
292 * The chain index is an array with pointers into the chain list, with
293 * CHAIN_INDEX_BUCKET_LEN spacing. This facilitates the ability to
294 * speedup chain list searching, by find a more optimal starting
295 * points when searching the linked list.
296 *
297 * The starting point can be found fast by using a binary search of
298 * the chain index. Thus, reducing the previous search complexity of
299 * O(n) to O(log(n/k) + k) where k is CHAIN_INDEX_BUCKET_LEN.
300 *
301 * A nice property of the chain index, is that the "bucket" list
302 * length is max CHAIN_INDEX_BUCKET_LEN (when just build, inserts will
303 * change this). Oppose to hashing, where the "bucket" list length can
304 * vary a lot.
305 */
306#ifndef CHAIN_INDEX_BUCKET_LEN
307#define CHAIN_INDEX_BUCKET_LEN 40
308#endif
309
310/* Another nice property of the chain index is that inserting/creating
311 * chains in chain list don't change the correctness of the chain
312 * index, it only causes longer lists in the buckets.
313 *
314 * To mitigate the performance penalty of longer bucket lists and the
315 * penalty of rebuilding, the chain index is rebuild only when
316 * CHAIN_INDEX_INSERT_MAX chains has been added.
317 */
318#ifndef CHAIN_INDEX_INSERT_MAX
319#define CHAIN_INDEX_INSERT_MAX 355
320#endif
321
322static inline unsigned int iptcc_is_builtin(struct chain_head *c);
323
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000324/* Use binary search in the chain index array, to find a chain_head
325 * pointer closest to the place of the searched name element.
326 *
327 * Notes that, binary search (obviously) requires that the chain list
328 * is sorted by name.
Jesper Dangaard Brouer4bae3f12008-07-03 20:31:42 +0200329 *
330 * The not so obvious: The chain index array, is actually both sorted
331 * by name and offset, at the same time!. This is only true because,
332 * chain are stored sorted in the kernel (as we pushed it in sorted).
333 *
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000334 */
335static struct list_head *
Jesper Dangaard Brouer4bae3f12008-07-03 20:31:42 +0200336__iptcc_bsearch_chain_index(const char *name, unsigned int offset,
Jan Engelhardtfd187312008-11-10 16:59:27 +0100337 unsigned int *idx, struct xtc_handle *handle,
Jesper Dangaard Brouer4bae3f12008-07-03 20:31:42 +0200338 enum bsearch_type type)
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000339{
340 unsigned int pos, end;
341 int res;
342
343 struct list_head *list_pos;
344 list_pos=&handle->chains;
345
346 /* Check for empty array, e.g. no user defined chains */
347 if (handle->chain_index_sz == 0) {
348 debug("WARNING: handle->chain_index_sz == 0\n");
349 return list_pos;
350 }
351
352 /* Init */
353 end = handle->chain_index_sz;
354 pos = end / 2;
355
Jesper Dangaard Brouer4bae3f12008-07-03 20:31:42 +0200356 debug("bsearch Find chain:%s (pos:%d end:%d) (offset:%d)\n",
357 name, pos, end, offset);
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000358
359 /* Loop */
360 loop:
361 if (!handle->chain_index[pos]) {
362 fprintf(stderr, "ERROR: NULL pointer chain_index[%d]\n", pos);
363 return &handle->chains; /* Be safe, return orig start pos */
364 }
365
Jesper Dangaard Brouer4bae3f12008-07-03 20:31:42 +0200366 debug("bsearch Index[%d] name:%s ",
367 pos, handle->chain_index[pos]->name);
368
369 /* Support for different compare functions */
370 switch (type) {
371 case BSEARCH_NAME:
372 res = strcmp(name, handle->chain_index[pos]->name);
373 break;
374 case BSEARCH_OFFSET:
375 debug("head_offset:[%d] foot_offset:[%d] ",
376 handle->chain_index[pos]->head_offset,
377 handle->chain_index[pos]->foot_offset);
378 res = offset - handle->chain_index[pos]->head_offset;
379 break;
380 default:
381 fprintf(stderr, "ERROR: %d not a valid bsearch type\n",
382 type);
383 abort();
384 break;
385 }
386 debug("res:%d ", res);
387
388
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000389 list_pos = &handle->chain_index[pos]->list;
Jan Engelhardtdbb77542008-02-11 00:33:30 +0100390 *idx = pos;
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000391
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000392 if (res == 0) { /* Found element, by direct hit */
393 debug("[found] Direct hit pos:%d end:%d\n", pos, end);
394 return list_pos;
395 } else if (res < 0) { /* Too far, jump back */
396 end = pos;
397 pos = pos / 2;
398
399 /* Exit case: First element of array */
400 if (end == 0) {
401 debug("[found] Reached first array elem (end%d)\n",end);
402 return list_pos;
403 }
404 debug("jump back to pos:%d (end:%d)\n", pos, end);
405 goto loop;
406 } else if (res > 0 ){ /* Not far enough, jump forward */
407
408 /* Exit case: Last element of array */
409 if (pos == handle->chain_index_sz-1) {
410 debug("[found] Last array elem (end:%d)\n", end);
411 return list_pos;
412 }
413
414 /* Exit case: Next index less, thus elem in this list section */
Jesper Dangaard Brouer4bae3f12008-07-03 20:31:42 +0200415 switch (type) {
416 case BSEARCH_NAME:
417 res = strcmp(name, handle->chain_index[pos+1]->name);
418 break;
419 case BSEARCH_OFFSET:
420 res = offset - handle->chain_index[pos+1]->head_offset;
421 break;
422 }
423
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000424 if (res < 0) {
425 debug("[found] closest list (end:%d)\n", end);
426 return list_pos;
427 }
428
429 pos = (pos+end)/2;
430 debug("jump forward to pos:%d (end:%d)\n", pos, end);
431 goto loop;
432 }
433
434 return list_pos;
435}
436
Jesper Dangaard Brouer4bae3f12008-07-03 20:31:42 +0200437/* Wrapper for string chain name based bsearch */
438static struct list_head *
439iptcc_bsearch_chain_index(const char *name, unsigned int *idx,
Jan Engelhardtfd187312008-11-10 16:59:27 +0100440 struct xtc_handle *handle)
Jesper Dangaard Brouer4bae3f12008-07-03 20:31:42 +0200441{
442 return __iptcc_bsearch_chain_index(name, 0, idx, handle, BSEARCH_NAME);
443}
444
445
446/* Wrapper for offset chain based bsearch */
447static struct list_head *
448iptcc_bsearch_chain_offset(unsigned int offset, unsigned int *idx,
Jan Engelhardtfd187312008-11-10 16:59:27 +0100449 struct xtc_handle *handle)
Jesper Dangaard Brouer4bae3f12008-07-03 20:31:42 +0200450{
451 struct list_head *pos;
452
453 /* If chains were not received sorted from kernel, then the
454 * offset bsearch is not possible.
455 */
456 if (!handle->sorted_offsets)
457 pos = handle->chains.next;
458 else
459 pos = __iptcc_bsearch_chain_index(NULL, offset, idx, handle,
460 BSEARCH_OFFSET);
461 return pos;
462}
463
464
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000465#ifdef DEBUG
466/* Trivial linear search of chain index. Function used for verifying
467 the output of bsearch function */
468static struct list_head *
Jan Engelhardtfd187312008-11-10 16:59:27 +0100469iptcc_linearly_search_chain_index(const char *name, struct xtc_handle *handle)
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000470{
471 unsigned int i=0;
472 int res=0;
473
474 struct list_head *list_pos;
475 list_pos = &handle->chains;
476
477 if (handle->chain_index_sz)
478 list_pos = &handle->chain_index[0]->list;
479
480 /* Linearly walk of chain index array */
481
482 for (i=0; i < handle->chain_index_sz; i++) {
483 if (handle->chain_index[i]) {
484 res = strcmp(handle->chain_index[i]->name, name);
485 if (res > 0)
486 break; // One step too far
487 list_pos = &handle->chain_index[i]->list;
488 if (res == 0)
489 break; // Direct hit
490 }
491 }
492
493 return list_pos;
494}
495#endif
496
Jan Engelhardtfd187312008-11-10 16:59:27 +0100497static int iptcc_chain_index_alloc(struct xtc_handle *h)
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000498{
499 unsigned int list_length = CHAIN_INDEX_BUCKET_LEN;
500 unsigned int array_elems;
501 unsigned int array_mem;
502
503 /* Allocate memory for the chain index array */
504 array_elems = (h->num_chains / list_length) +
505 (h->num_chains % list_length ? 1 : 0);
506 array_mem = sizeof(h->chain_index) * array_elems;
507
508 debug("Alloc Chain index, elems:%d mem:%d bytes\n",
509 array_elems, array_mem);
510
511 h->chain_index = malloc(array_mem);
Jan Engelhardt0eee3002008-11-26 17:18:08 +0100512 if (h->chain_index == NULL && array_mem > 0) {
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000513 h->chain_index_sz = 0;
514 return -ENOMEM;
515 }
516 memset(h->chain_index, 0, array_mem);
517 h->chain_index_sz = array_elems;
518
519 return 1;
520}
521
Jan Engelhardtfd187312008-11-10 16:59:27 +0100522static void iptcc_chain_index_free(struct xtc_handle *h)
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000523{
524 h->chain_index_sz = 0;
525 free(h->chain_index);
526}
527
528
529#ifdef DEBUG
Jan Engelhardtfd187312008-11-10 16:59:27 +0100530static void iptcc_chain_index_dump(struct xtc_handle *h)
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000531{
532 unsigned int i = 0;
533
534 /* Dump: contents of chain index array */
535 for (i=0; i < h->chain_index_sz; i++) {
536 if (h->chain_index[i]) {
537 fprintf(stderr, "Chain index[%d].name: %s\n",
538 i, h->chain_index[i]->name);
539 }
540 }
541}
542#endif
543
544/* Build the chain index */
Jan Engelhardtfd187312008-11-10 16:59:27 +0100545static int iptcc_chain_index_build(struct xtc_handle *h)
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000546{
547 unsigned int list_length = CHAIN_INDEX_BUCKET_LEN;
548 unsigned int chains = 0;
549 unsigned int cindex = 0;
550 struct chain_head *c;
551
552 /* Build up the chain index array here */
553 debug("Building chain index\n");
554
555 debug("Number of user defined chains:%d bucket_sz:%d array_sz:%d\n",
556 h->num_chains, list_length, h->chain_index_sz);
557
558 if (h->chain_index_sz == 0)
559 return 0;
560
561 list_for_each_entry(c, &h->chains, list) {
562
563 /* Issue: The index array needs to start after the
564 * builtin chains, as they are not sorted */
565 if (!iptcc_is_builtin(c)) {
566 cindex=chains / list_length;
567
568 /* Safe guard, break out on array limit, this
569 * is useful if chains are added and array is
570 * rebuild, without realloc of memory. */
571 if (cindex >= h->chain_index_sz)
572 break;
573
574 if ((chains % list_length)== 0) {
575 debug("\nIndex[%d] Chains:", cindex);
576 h->chain_index[cindex] = c;
577 }
578 chains++;
579 }
580 debug("%s, ", c->name);
581 }
582 debug("\n");
583
584 return 1;
585}
586
Jan Engelhardtfd187312008-11-10 16:59:27 +0100587static int iptcc_chain_index_rebuild(struct xtc_handle *h)
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000588{
589 debug("REBUILD chain index array\n");
590 iptcc_chain_index_free(h);
591 if ((iptcc_chain_index_alloc(h)) < 0)
592 return -ENOMEM;
593 iptcc_chain_index_build(h);
594 return 1;
595}
596
597/* Delete chain (pointer) from index array. Removing an element from
598 * the chain list only affects the chain index array, if the chain
599 * index points-to/uses that list pointer.
600 *
601 * There are different strategies, the simple and safe is to rebuild
602 * the chain index every time. The more advanced is to update the
603 * array index to point to the next element, but that requires some
604 * house keeping and boundry checks. The advanced is implemented, as
605 * the simple approach behaves badly when all chains are deleted
606 * because list_for_each processing will always hit the first chain
607 * index, thus causing a rebuild for every chain.
608 */
Jan Engelhardtfd187312008-11-10 16:59:27 +0100609static int iptcc_chain_index_delete_chain(struct chain_head *c, struct xtc_handle *h)
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000610{
Jan Engelhardt7d91a2a2011-05-30 01:39:54 +0200611 struct list_head *index_ptr, *next;
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000612 struct chain_head *c2;
Jan Engelhardtdbb77542008-02-11 00:33:30 +0100613 unsigned int idx, idx2;
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000614
Jan Engelhardtdbb77542008-02-11 00:33:30 +0100615 index_ptr = iptcc_bsearch_chain_index(c->name, &idx, h);
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000616
617 debug("Del chain[%s] c->list:%p index_ptr:%p\n",
618 c->name, &c->list, index_ptr);
619
620 /* Save the next pointer */
621 next = c->list.next;
622 list_del(&c->list);
623
624 if (index_ptr == &c->list) { /* Chain used as index ptr */
625
626 /* See if its possible to avoid a rebuild, by shifting
627 * to next pointer. Its possible if the next pointer
628 * is located in the same index bucket.
629 */
630 c2 = list_entry(next, struct chain_head, list);
Jan Engelhardt7d91a2a2011-05-30 01:39:54 +0200631 iptcc_bsearch_chain_index(c2->name, &idx2, h);
Jan Engelhardtdbb77542008-02-11 00:33:30 +0100632 if (idx != idx2) {
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000633 /* Rebuild needed */
634 return iptcc_chain_index_rebuild(h);
635 } else {
636 /* Avoiding rebuild */
637 debug("Update cindex[%d] with next ptr name:[%s]\n",
Jan Engelhardtdbb77542008-02-11 00:33:30 +0100638 idx, c2->name);
639 h->chain_index[idx]=c2;
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000640 return 0;
641 }
642 }
643 return 0;
644}
645
646
647/**********************************************************************
Harald Welteaae69be2004-08-29 23:32:14 +0000648 * iptc cache utility functions (iptcc_*)
649 **********************************************************************/
Harald Welte0113fe72004-01-06 19:04:02 +0000650
Harald Welteaae69be2004-08-29 23:32:14 +0000651/* Is the given chain builtin (1) or user-defined (0) */
Jesper Dangaard Brouer91093982008-01-15 17:01:58 +0000652static inline unsigned int iptcc_is_builtin(struct chain_head *c)
Harald Welteaae69be2004-08-29 23:32:14 +0000653{
654 return (c->hooknum ? 1 : 0);
655}
656
657/* Get a specific rule within a chain */
658static struct rule_head *iptcc_get_rule_num(struct chain_head *c,
659 unsigned int rulenum)
660{
661 struct rule_head *r;
662 unsigned int num = 0;
663
664 list_for_each_entry(r, &c->rules, list) {
665 num++;
666 if (num == rulenum)
667 return r;
668 }
669 return NULL;
670}
671
Martin Josefssona5616dc2004-10-24 22:27:31 +0000672/* Get a specific rule within a chain backwards */
673static struct rule_head *iptcc_get_rule_num_reverse(struct chain_head *c,
674 unsigned int rulenum)
675{
676 struct rule_head *r;
677 unsigned int num = 0;
678
679 list_for_each_entry_reverse(r, &c->rules, list) {
680 num++;
681 if (num == rulenum)
682 return r;
683 }
684 return NULL;
685}
686
Harald Welteaae69be2004-08-29 23:32:14 +0000687/* Returns chain head if found, otherwise NULL. */
688static struct chain_head *
Jan Engelhardtfd187312008-11-10 16:59:27 +0100689iptcc_find_chain_by_offset(struct xtc_handle *handle, unsigned int offset)
Harald Welteaae69be2004-08-29 23:32:14 +0000690{
691 struct list_head *pos;
Jesper Dangaard Brouer4bae3f12008-07-03 20:31:42 +0200692 struct list_head *list_start_pos;
693 unsigned int i;
Harald Welteaae69be2004-08-29 23:32:14 +0000694
695 if (list_empty(&handle->chains))
696 return NULL;
697
Jesper Dangaard Brouer4bae3f12008-07-03 20:31:42 +0200698 /* Find a smart place to start the search */
699 list_start_pos = iptcc_bsearch_chain_offset(offset, &i, handle);
700
701 /* Note that iptcc_bsearch_chain_offset() skips builtin
702 * chains, but this function is only used for finding jump
703 * targets, and a buildin chain is not a valid jump target */
704
705 debug("Offset:[%u] starting search at index:[%u]\n", offset, i);
706// list_for_each(pos, &handle->chains) {
707 list_for_each(pos, list_start_pos->prev) {
Harald Welteaae69be2004-08-29 23:32:14 +0000708 struct chain_head *c = list_entry(pos, struct chain_head, list);
Jesper Dangaard Brouer4bae3f12008-07-03 20:31:42 +0200709 debug(".");
710 if (offset >= c->head_offset && offset <= c->foot_offset) {
711 debug("Offset search found chain:[%s]\n", c->name);
Harald Welteaae69be2004-08-29 23:32:14 +0000712 return c;
Jesper Dangaard Brouer4bae3f12008-07-03 20:31:42 +0200713 }
Harald Welte0113fe72004-01-06 19:04:02 +0000714 }
715
Harald Welteaae69be2004-08-29 23:32:14 +0000716 return NULL;
Harald Welte0113fe72004-01-06 19:04:02 +0000717}
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000718
Harald Welteaae69be2004-08-29 23:32:14 +0000719/* Returns chain head if found, otherwise NULL. */
720static struct chain_head *
Jan Engelhardtfd187312008-11-10 16:59:27 +0100721iptcc_find_label(const char *name, struct xtc_handle *handle)
Harald Welteaae69be2004-08-29 23:32:14 +0000722{
723 struct list_head *pos;
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000724 struct list_head *list_start_pos;
725 unsigned int i=0;
726 int res;
Harald Welteaae69be2004-08-29 23:32:14 +0000727
728 if (list_empty(&handle->chains))
729 return NULL;
730
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000731 /* First look at builtin chains */
Harald Welteaae69be2004-08-29 23:32:14 +0000732 list_for_each(pos, &handle->chains) {
733 struct chain_head *c = list_entry(pos, struct chain_head, list);
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000734 if (!iptcc_is_builtin(c))
735 break;
Harald Welteaae69be2004-08-29 23:32:14 +0000736 if (!strcmp(c->name, name))
737 return c;
738 }
739
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000740 /* Find a smart place to start the search via chain index */
741 //list_start_pos = iptcc_linearly_search_chain_index(name, handle);
742 list_start_pos = iptcc_bsearch_chain_index(name, &i, handle);
743
744 /* Handel if bsearch bails out early */
745 if (list_start_pos == &handle->chains) {
746 list_start_pos = pos;
747 }
748#ifdef DEBUG
749 else {
750 /* Verify result of bsearch against linearly index search */
751 struct list_head *test_pos;
752 struct chain_head *test_c, *tmp_c;
753 test_pos = iptcc_linearly_search_chain_index(name, handle);
754 if (list_start_pos != test_pos) {
755 debug("BUG in chain_index search\n");
756 test_c=list_entry(test_pos, struct chain_head,list);
757 tmp_c =list_entry(list_start_pos,struct chain_head,list);
758 debug("Verify search found:\n");
759 debug(" Chain:%s\n", test_c->name);
760 debug("BSearch found:\n");
761 debug(" Chain:%s\n", tmp_c->name);
762 exit(42);
763 }
764 }
765#endif
766
767 /* Initial/special case, no user defined chains */
768 if (handle->num_chains == 0)
769 return NULL;
770
771 /* Start searching through the chain list */
772 list_for_each(pos, list_start_pos->prev) {
773 struct chain_head *c = list_entry(pos, struct chain_head, list);
774 res = strcmp(c->name, name);
775 debug("List search name:%s == %s res:%d\n", name, c->name, res);
776 if (res==0)
777 return c;
778
779 /* We can stop earlier as we know list is sorted */
780 if (res>0 && !iptcc_is_builtin(c)) { /* Walked too far*/
781 debug(" Not in list, walked too far, sorted list\n");
782 return NULL;
783 }
784
785 /* Stop on wrap around, if list head is reached */
786 if (pos == &handle->chains) {
787 debug("Stop, list head reached\n");
788 return NULL;
789 }
790 }
791
792 debug("List search NOT found name:%s\n", name);
Harald Welteaae69be2004-08-29 23:32:14 +0000793 return NULL;
794}
795
796/* called when rule is to be removed from cache */
797static void iptcc_delete_rule(struct rule_head *r)
798{
799 DEBUGP("deleting rule %p (offset %u)\n", r, r->offset);
800 /* clean up reference count of called chain */
801 if (r->type == IPTCC_R_JUMP
802 && r->jump)
803 r->jump->references--;
804
805 list_del(&r->list);
806 free(r);
807}
808
809
810/**********************************************************************
811 * RULESET PARSER (blob -> cache)
812 **********************************************************************/
813
Harald Welteaae69be2004-08-29 23:32:14 +0000814/* Delete policy rule of previous chain, since cache doesn't contain
815 * chain policy rules.
816 * WARNING: This function has ugly design and relies on a lot of context, only
817 * to be called from specific places within the parser */
Jan Engelhardtfd187312008-11-10 16:59:27 +0100818static int __iptcc_p_del_policy(struct xtc_handle *h, unsigned int num)
Harald Welteaae69be2004-08-29 23:32:14 +0000819{
Jan Engelhardt51651b62009-10-23 23:35:49 +0200820 const unsigned char *data;
821
Harald Welteaae69be2004-08-29 23:32:14 +0000822 if (h->chain_iterator_cur) {
823 /* policy rule is last rule */
824 struct rule_head *pr = (struct rule_head *)
825 h->chain_iterator_cur->rules.prev;
826
827 /* save verdict */
Jan Engelhardt51651b62009-10-23 23:35:49 +0200828 data = GET_TARGET(pr->entry)->data;
829 h->chain_iterator_cur->verdict = *(const int *)data;
Harald Welteaae69be2004-08-29 23:32:14 +0000830
831 /* save counter and counter_map information */
Jesper Dangaard Brouera9fe5b32009-03-23 14:26:56 +0100832 h->chain_iterator_cur->counter_map.maptype =
Jan Engelhardt7c4d6682009-10-26 18:43:54 +0100833 COUNTER_MAP_ZEROED;
Harald Welteaae69be2004-08-29 23:32:14 +0000834 h->chain_iterator_cur->counter_map.mappos = num-1;
Jesper Dangaard Brouera9fe5b32009-03-23 14:26:56 +0100835 memcpy(&h->chain_iterator_cur->counters, &pr->entry->counters,
Harald Welteaae69be2004-08-29 23:32:14 +0000836 sizeof(h->chain_iterator_cur->counters));
837
838 /* foot_offset points to verdict rule */
839 h->chain_iterator_cur->foot_index = num;
840 h->chain_iterator_cur->foot_offset = pr->offset;
841
842 /* delete rule from cache */
843 iptcc_delete_rule(pr);
Martin Josefsson8d1b38a2004-09-22 21:00:19 +0000844 h->chain_iterator_cur->num_rules--;
Harald Welteaae69be2004-08-29 23:32:14 +0000845
846 return 1;
847 }
848 return 0;
849}
850
Harald Welteec30b6c2005-02-01 16:45:56 +0000851/* alphabetically insert a chain into the list */
Christoph Paasch7cd15e32009-03-23 13:50:11 +0100852static void iptc_insert_chain(struct xtc_handle *h, struct chain_head *c)
Harald Welteec30b6c2005-02-01 16:45:56 +0000853{
854 struct chain_head *tmp;
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000855 struct list_head *list_start_pos;
856 unsigned int i=1;
857
858 /* Find a smart place to start the insert search */
859 list_start_pos = iptcc_bsearch_chain_index(c->name, &i, h);
860
861 /* Handle the case, where chain.name is smaller than index[0] */
862 if (i==0 && strcmp(c->name, h->chain_index[0]->name) <= 0) {
863 h->chain_index[0] = c; /* Update chain index head */
864 list_start_pos = h->chains.next;
865 debug("Update chain_index[0] with %s\n", c->name);
866 }
867
868 /* Handel if bsearch bails out early */
869 if (list_start_pos == &h->chains) {
870 list_start_pos = h->chains.next;
871 }
Harald Welteec30b6c2005-02-01 16:45:56 +0000872
Olaf Rempel9d3ed772005-03-04 23:08:30 +0000873 /* sort only user defined chains */
874 if (!c->hooknum) {
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000875 list_for_each_entry(tmp, list_start_pos->prev, list) {
Robert de Barthfeca0572005-07-31 07:04:59 +0000876 if (!tmp->hooknum && strcmp(c->name, tmp->name) <= 0) {
Olaf Rempel9d3ed772005-03-04 23:08:30 +0000877 list_add(&c->list, tmp->list.prev);
878 return;
879 }
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000880
881 /* Stop if list head is reached */
882 if (&tmp->list == &h->chains) {
883 debug("Insert, list head reached add to tail\n");
884 break;
885 }
Harald Welteec30b6c2005-02-01 16:45:56 +0000886 }
887 }
888
889 /* survived till end of list: add at tail */
890 list_add_tail(&c->list, &h->chains);
891}
892
Harald Welteaae69be2004-08-29 23:32:14 +0000893/* Another ugly helper function split out of cache_add_entry to make it less
894 * spaghetti code */
Jan Engelhardtfd187312008-11-10 16:59:27 +0100895static void __iptcc_p_add_chain(struct xtc_handle *h, struct chain_head *c,
Harald Welteaae69be2004-08-29 23:32:14 +0000896 unsigned int offset, unsigned int *num)
897{
Jesper Dangaard Brouer13364512007-12-12 15:20:42 +0000898 struct list_head *tail = h->chains.prev;
899 struct chain_head *ctail;
900
Harald Welteaae69be2004-08-29 23:32:14 +0000901 __iptcc_p_del_policy(h, *num);
902
903 c->head_offset = offset;
904 c->index = *num;
905
Jesper Dangaard Brouer13364512007-12-12 15:20:42 +0000906 /* Chains from kernel are already sorted, as they are inserted
907 * sorted. But there exists an issue when shifting to 1.4.0
908 * from an older version, as old versions allow last created
909 * chain to be unsorted.
910 */
911 if (iptcc_is_builtin(c)) /* Only user defined chains are sorted*/
912 list_add_tail(&c->list, &h->chains);
913 else {
914 ctail = list_entry(tail, struct chain_head, list);
Jesper Dangaard Brouer4bae3f12008-07-03 20:31:42 +0200915
Jesper Dangaard Brouer526d3e12008-07-03 20:29:34 +0200916 if (strcmp(c->name, ctail->name) > 0 ||
917 iptcc_is_builtin(ctail))
Jesper Dangaard Brouer13364512007-12-12 15:20:42 +0000918 list_add_tail(&c->list, &h->chains);/* Already sorted*/
Jesper Dangaard Brouer4bae3f12008-07-03 20:31:42 +0200919 else {
Jesper Dangaard Brouer13364512007-12-12 15:20:42 +0000920 iptc_insert_chain(h, c);/* Was not sorted */
Jesper Dangaard Brouer4bae3f12008-07-03 20:31:42 +0200921
922 /* Notice, if chains were not received sorted
923 * from kernel, then an offset bsearch is no
924 * longer valid.
925 */
926 h->sorted_offsets = 0;
927
928 debug("NOTICE: chain:[%s] was NOT sorted(ctail:%s)\n",
929 c->name, ctail->name);
930 }
Jesper Dangaard Brouer13364512007-12-12 15:20:42 +0000931 }
Jesper Dangaard Brouerd8cb7872007-11-28 08:40:26 +0000932
Harald Welteaae69be2004-08-29 23:32:14 +0000933 h->chain_iterator_cur = c;
934}
935
936/* main parser function: add an entry from the blob to the cache */
Jesper Dangaard Brouera9fe5b32009-03-23 14:26:56 +0100937static int cache_add_entry(STRUCT_ENTRY *e,
938 struct xtc_handle *h,
Harald Welteaae69be2004-08-29 23:32:14 +0000939 STRUCT_ENTRY **prev,
940 unsigned int *num)
941{
942 unsigned int builtin;
943 unsigned int offset = (char *)e - (char *)h->entries->entrytable;
944
945 DEBUGP("entering...");
946
947 /* Last entry ("policy rule"). End it.*/
948 if (iptcb_entry2offset(h,e) + e->next_offset == h->entries->size) {
949 /* This is the ERROR node at the end of the chain */
950 DEBUGP_C("%u:%u: end of table:\n", *num, offset);
951
952 __iptcc_p_del_policy(h, *num);
953
954 h->chain_iterator_cur = NULL;
955 goto out_inc;
956 }
957
958 /* We know this is the start of a new chain if it's an ERROR
959 * target, or a hook entry point */
960
961 if (strcmp(GET_TARGET(e)->u.user.name, ERROR_TARGET) == 0) {
Jesper Dangaard Brouera9fe5b32009-03-23 14:26:56 +0100962 struct chain_head *c =
Harald Welteaae69be2004-08-29 23:32:14 +0000963 iptcc_alloc_chain_head((const char *)GET_TARGET(e)->data, 0);
Jesper Dangaard Brouera9fe5b32009-03-23 14:26:56 +0100964 DEBUGP_C("%u:%u:new userdefined chain %s: %p\n", *num, offset,
Harald Welteaae69be2004-08-29 23:32:14 +0000965 (char *)c->name, c);
966 if (!c) {
967 errno = -ENOMEM;
968 return -1;
969 }
Jesper Dangaard Brouer48bde402008-01-15 17:06:48 +0000970 h->num_chains++; /* New user defined chain */
Harald Welteaae69be2004-08-29 23:32:14 +0000971
972 __iptcc_p_add_chain(h, c, offset, num);
973
974 } else if ((builtin = iptcb_ent_is_hook_entry(e, h)) != 0) {
975 struct chain_head *c =
Jesper Dangaard Brouera9fe5b32009-03-23 14:26:56 +0100976 iptcc_alloc_chain_head((char *)hooknames[builtin-1],
Harald Welteaae69be2004-08-29 23:32:14 +0000977 builtin);
Jesper Dangaard Brouera9fe5b32009-03-23 14:26:56 +0100978 DEBUGP_C("%u:%u new builtin chain: %p (rules=%p)\n",
Harald Welteaae69be2004-08-29 23:32:14 +0000979 *num, offset, c, &c->rules);
980 if (!c) {
981 errno = -ENOMEM;
982 return -1;
983 }
984
985 c->hooknum = builtin;
986
987 __iptcc_p_add_chain(h, c, offset, num);
988
989 /* FIXME: this is ugly. */
990 goto new_rule;
991 } else {
992 /* has to be normal rule */
993 struct rule_head *r;
994new_rule:
995
Jesper Dangaard Brouera9fe5b32009-03-23 14:26:56 +0100996 if (!(r = iptcc_alloc_rule(h->chain_iterator_cur,
Harald Welteaae69be2004-08-29 23:32:14 +0000997 e->next_offset))) {
998 errno = ENOMEM;
999 return -1;
1000 }
1001 DEBUGP_C("%u:%u normal rule: %p: ", *num, offset, r);
1002
1003 r->index = *num;
1004 r->offset = offset;
1005 memcpy(r->entry, e, e->next_offset);
1006 r->counter_map.maptype = COUNTER_MAP_NORMAL_MAP;
1007 r->counter_map.mappos = r->index;
1008
1009 /* handling of jumps, etc. */
1010 if (!strcmp(GET_TARGET(e)->u.user.name, STANDARD_TARGET)) {
1011 STRUCT_STANDARD_TARGET *t;
1012
1013 t = (STRUCT_STANDARD_TARGET *)GET_TARGET(e);
1014 if (t->target.u.target_size
1015 != ALIGN(sizeof(STRUCT_STANDARD_TARGET))) {
1016 errno = EINVAL;
1017 return -1;
1018 }
1019
1020 if (t->verdict < 0) {
1021 DEBUGP_C("standard, verdict=%d\n", t->verdict);
1022 r->type = IPTCC_R_STANDARD;
1023 } else if (t->verdict == r->offset+e->next_offset) {
1024 DEBUGP_C("fallthrough\n");
1025 r->type = IPTCC_R_FALLTHROUGH;
1026 } else {
1027 DEBUGP_C("jump, target=%u\n", t->verdict);
1028 r->type = IPTCC_R_JUMP;
1029 /* Jump target fixup has to be deferred
1030 * until second pass, since we migh not
1031 * yet have parsed the target */
1032 }
Martin Josefsson52c38022004-09-22 19:39:40 +00001033 } else {
1034 DEBUGP_C("module, target=%s\n", GET_TARGET(e)->u.user.name);
1035 r->type = IPTCC_R_MODULE;
Harald Welteaae69be2004-08-29 23:32:14 +00001036 }
1037
1038 list_add_tail(&r->list, &h->chain_iterator_cur->rules);
Martin Josefsson8d1b38a2004-09-22 21:00:19 +00001039 h->chain_iterator_cur->num_rules++;
Harald Welteaae69be2004-08-29 23:32:14 +00001040 }
1041out_inc:
1042 (*num)++;
1043 return 0;
1044}
1045
1046
1047/* parse an iptables blob into it's pieces */
Jan Engelhardtfd187312008-11-10 16:59:27 +01001048static int parse_table(struct xtc_handle *h)
Harald Welteaae69be2004-08-29 23:32:14 +00001049{
1050 STRUCT_ENTRY *prev;
1051 unsigned int num = 0;
1052 struct chain_head *c;
1053
Jesper Dangaard Brouer4bae3f12008-07-03 20:31:42 +02001054 /* Assume that chains offsets are sorted, this verified during
1055 parsing of ruleset (in __iptcc_p_add_chain())*/
1056 h->sorted_offsets = 1;
1057
Harald Welteaae69be2004-08-29 23:32:14 +00001058 /* First pass: over ruleset blob */
1059 ENTRY_ITERATE(h->entries->entrytable, h->entries->size,
1060 cache_add_entry, h, &prev, &num);
1061
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +00001062 /* Build the chain index, used for chain list search speedup */
1063 if ((iptcc_chain_index_alloc(h)) < 0)
1064 return -ENOMEM;
1065 iptcc_chain_index_build(h);
1066
Harald Welteaae69be2004-08-29 23:32:14 +00001067 /* Second pass: fixup parsed data from first pass */
1068 list_for_each_entry(c, &h->chains, list) {
1069 struct rule_head *r;
1070 list_for_each_entry(r, &c->rules, list) {
Jan Engelhardtdbb77542008-02-11 00:33:30 +01001071 struct chain_head *lc;
Harald Welteaae69be2004-08-29 23:32:14 +00001072 STRUCT_STANDARD_TARGET *t;
1073
1074 if (r->type != IPTCC_R_JUMP)
1075 continue;
1076
1077 t = (STRUCT_STANDARD_TARGET *)GET_TARGET(r->entry);
Jan Engelhardtdbb77542008-02-11 00:33:30 +01001078 lc = iptcc_find_chain_by_offset(h, t->verdict);
1079 if (!lc)
Harald Welteaae69be2004-08-29 23:32:14 +00001080 return -1;
Jan Engelhardtdbb77542008-02-11 00:33:30 +01001081 r->jump = lc;
1082 lc->references++;
Harald Welteaae69be2004-08-29 23:32:14 +00001083 }
1084 }
1085
Harald Welteaae69be2004-08-29 23:32:14 +00001086 return 1;
1087}
1088
1089
1090/**********************************************************************
1091 * RULESET COMPILATION (cache -> blob)
1092 **********************************************************************/
1093
1094/* Convenience structures */
1095struct iptcb_chain_start{
1096 STRUCT_ENTRY e;
1097 struct ipt_error_target name;
1098};
1099#define IPTCB_CHAIN_START_SIZE (sizeof(STRUCT_ENTRY) + \
1100 ALIGN(sizeof(struct ipt_error_target)))
1101
1102struct iptcb_chain_foot {
1103 STRUCT_ENTRY e;
1104 STRUCT_STANDARD_TARGET target;
1105};
1106#define IPTCB_CHAIN_FOOT_SIZE (sizeof(STRUCT_ENTRY) + \
1107 ALIGN(sizeof(STRUCT_STANDARD_TARGET)))
1108
1109struct iptcb_chain_error {
1110 STRUCT_ENTRY entry;
1111 struct ipt_error_target target;
1112};
1113#define IPTCB_CHAIN_ERROR_SIZE (sizeof(STRUCT_ENTRY) + \
1114 ALIGN(sizeof(struct ipt_error_target)))
1115
1116
1117
1118/* compile rule from cache into blob */
Jan Engelhardtfd187312008-11-10 16:59:27 +01001119static inline int iptcc_compile_rule (struct xtc_handle *h, STRUCT_REPLACE *repl, struct rule_head *r)
Harald Welteaae69be2004-08-29 23:32:14 +00001120{
1121 /* handle jumps */
1122 if (r->type == IPTCC_R_JUMP) {
1123 STRUCT_STANDARD_TARGET *t;
1124 t = (STRUCT_STANDARD_TARGET *)GET_TARGET(r->entry);
1125 /* memset for memcmp convenience on delete/replace */
1126 memset(t->target.u.user.name, 0, FUNCTION_MAXNAMELEN);
1127 strcpy(t->target.u.user.name, STANDARD_TARGET);
1128 /* Jumps can only happen to builtin chains, so we
1129 * can safely assume that they always have a header */
1130 t->verdict = r->jump->head_offset + IPTCB_CHAIN_START_SIZE;
1131 } else if (r->type == IPTCC_R_FALLTHROUGH) {
1132 STRUCT_STANDARD_TARGET *t;
1133 t = (STRUCT_STANDARD_TARGET *)GET_TARGET(r->entry);
1134 t->verdict = r->offset + r->size;
1135 }
Jesper Dangaard Brouera9fe5b32009-03-23 14:26:56 +01001136
Harald Welteaae69be2004-08-29 23:32:14 +00001137 /* copy entry from cache to blob */
1138 memcpy((char *)repl->entries+r->offset, r->entry, r->size);
1139
1140 return 1;
1141}
1142
1143/* compile chain from cache into blob */
Jan Engelhardtfd187312008-11-10 16:59:27 +01001144static int iptcc_compile_chain(struct xtc_handle *h, STRUCT_REPLACE *repl, struct chain_head *c)
Harald Welteaae69be2004-08-29 23:32:14 +00001145{
1146 int ret;
1147 struct rule_head *r;
1148 struct iptcb_chain_start *head;
1149 struct iptcb_chain_foot *foot;
1150
1151 /* only user-defined chains have heaer */
1152 if (!iptcc_is_builtin(c)) {
1153 /* put chain header in place */
1154 head = (void *)repl->entries + c->head_offset;
1155 head->e.target_offset = sizeof(STRUCT_ENTRY);
1156 head->e.next_offset = IPTCB_CHAIN_START_SIZE;
1157 strcpy(head->name.t.u.user.name, ERROR_TARGET);
Jesper Dangaard Brouera9fe5b32009-03-23 14:26:56 +01001158 head->name.t.u.target_size =
Harald Welteaae69be2004-08-29 23:32:14 +00001159 ALIGN(sizeof(struct ipt_error_target));
1160 strcpy(head->name.error, c->name);
1161 } else {
Jesper Dangaard Brouera9fe5b32009-03-23 14:26:56 +01001162 repl->hook_entry[c->hooknum-1] = c->head_offset;
Harald Welteaae69be2004-08-29 23:32:14 +00001163 repl->underflow[c->hooknum-1] = c->foot_offset;
1164 }
1165
1166 /* iterate over rules */
1167 list_for_each_entry(r, &c->rules, list) {
1168 ret = iptcc_compile_rule(h, repl, r);
1169 if (ret < 0)
1170 return ret;
1171 }
1172
1173 /* put chain footer in place */
1174 foot = (void *)repl->entries + c->foot_offset;
1175 foot->e.target_offset = sizeof(STRUCT_ENTRY);
1176 foot->e.next_offset = IPTCB_CHAIN_FOOT_SIZE;
1177 strcpy(foot->target.target.u.user.name, STANDARD_TARGET);
1178 foot->target.target.u.target_size =
1179 ALIGN(sizeof(STRUCT_STANDARD_TARGET));
1180 /* builtin targets have verdict, others return */
1181 if (iptcc_is_builtin(c))
1182 foot->target.verdict = c->verdict;
1183 else
1184 foot->target.verdict = RETURN;
1185 /* set policy-counters */
1186 memcpy(&foot->e.counters, &c->counters, sizeof(STRUCT_COUNTERS));
1187
1188 return 0;
1189}
1190
1191/* calculate offset and number for every rule in the cache */
Jan Engelhardtfd187312008-11-10 16:59:27 +01001192static int iptcc_compile_chain_offsets(struct xtc_handle *h, struct chain_head *c,
Harald Welteefa8fc22005-07-19 22:03:49 +00001193 unsigned int *offset, unsigned int *num)
Harald Welteaae69be2004-08-29 23:32:14 +00001194{
1195 struct rule_head *r;
1196
1197 c->head_offset = *offset;
1198 DEBUGP("%s: chain_head %u, offset=%u\n", c->name, *num, *offset);
1199
1200 if (!iptcc_is_builtin(c)) {
1201 /* Chain has header */
Jesper Dangaard Brouera9fe5b32009-03-23 14:26:56 +01001202 *offset += sizeof(STRUCT_ENTRY)
Harald Welteaae69be2004-08-29 23:32:14 +00001203 + ALIGN(sizeof(struct ipt_error_target));
1204 (*num)++;
1205 }
1206
1207 list_for_each_entry(r, &c->rules, list) {
1208 DEBUGP("rule %u, offset=%u, index=%u\n", *num, *offset, *num);
1209 r->offset = *offset;
1210 r->index = *num;
1211 *offset += r->size;
1212 (*num)++;
1213 }
1214
Jesper Dangaard Brouera9fe5b32009-03-23 14:26:56 +01001215 DEBUGP("%s; chain_foot %u, offset=%u, index=%u\n", c->name, *num,
Harald Welteaae69be2004-08-29 23:32:14 +00001216 *offset, *num);
1217 c->foot_offset = *offset;
1218 c->foot_index = *num;
1219 *offset += sizeof(STRUCT_ENTRY)
1220 + ALIGN(sizeof(STRUCT_STANDARD_TARGET));
1221 (*num)++;
1222
1223 return 1;
1224}
1225
1226/* put the pieces back together again */
Jan Engelhardtfd187312008-11-10 16:59:27 +01001227static int iptcc_compile_table_prep(struct xtc_handle *h, unsigned int *size)
Harald Welteaae69be2004-08-29 23:32:14 +00001228{
1229 struct chain_head *c;
1230 unsigned int offset = 0, num = 0;
1231 int ret = 0;
1232
1233 /* First pass: calculate offset for every rule */
1234 list_for_each_entry(c, &h->chains, list) {
1235 ret = iptcc_compile_chain_offsets(h, c, &offset, &num);
1236 if (ret < 0)
1237 return ret;
1238 }
1239
1240 /* Append one error rule at end of chain */
1241 num++;
1242 offset += sizeof(STRUCT_ENTRY)
1243 + ALIGN(sizeof(struct ipt_error_target));
1244
1245 /* ruleset size is now in offset */
1246 *size = offset;
1247 return num;
1248}
1249
Jan Engelhardtfd187312008-11-10 16:59:27 +01001250static int iptcc_compile_table(struct xtc_handle *h, STRUCT_REPLACE *repl)
Harald Welteaae69be2004-08-29 23:32:14 +00001251{
1252 struct chain_head *c;
1253 struct iptcb_chain_error *error;
1254
1255 /* Second pass: copy from cache to offsets, fill in jumps */
1256 list_for_each_entry(c, &h->chains, list) {
1257 int ret = iptcc_compile_chain(h, repl, c);
1258 if (ret < 0)
1259 return ret;
1260 }
1261
1262 /* Append error rule at end of chain */
1263 error = (void *)repl->entries + repl->size - IPTCB_CHAIN_ERROR_SIZE;
1264 error->entry.target_offset = sizeof(STRUCT_ENTRY);
1265 error->entry.next_offset = IPTCB_CHAIN_ERROR_SIZE;
Jesper Dangaard Brouera9fe5b32009-03-23 14:26:56 +01001266 error->target.t.u.user.target_size =
Harald Welteaae69be2004-08-29 23:32:14 +00001267 ALIGN(sizeof(struct ipt_error_target));
1268 strcpy((char *)&error->target.t.u.user.name, ERROR_TARGET);
1269 strcpy((char *)&error->target.error, "ERROR");
1270
1271 return 1;
1272}
1273
1274/**********************************************************************
1275 * EXTERNAL API (operates on cache only)
1276 **********************************************************************/
Marc Bouchere6869a82000-03-20 06:03:29 +00001277
1278/* Allocate handle of given size */
Jan Engelhardtfd187312008-11-10 16:59:27 +01001279static struct xtc_handle *
Harald Welte0113fe72004-01-06 19:04:02 +00001280alloc_handle(const char *tablename, unsigned int size, unsigned int num_rules)
Marc Bouchere6869a82000-03-20 06:03:29 +00001281{
Jan Engelhardtfd187312008-11-10 16:59:27 +01001282 struct xtc_handle *h;
Marc Bouchere6869a82000-03-20 06:03:29 +00001283
Harald Welteaae69be2004-08-29 23:32:14 +00001284 h = malloc(sizeof(STRUCT_TC_HANDLE));
1285 if (!h) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001286 errno = ENOMEM;
1287 return NULL;
1288 }
Harald Welteaae69be2004-08-29 23:32:14 +00001289 memset(h, 0, sizeof(*h));
1290 INIT_LIST_HEAD(&h->chains);
Marc Bouchere6869a82000-03-20 06:03:29 +00001291 strcpy(h->info.name, tablename);
Harald Welteaae69be2004-08-29 23:32:14 +00001292
Harald Welte0371c0c2004-09-19 21:00:12 +00001293 h->entries = malloc(sizeof(STRUCT_GET_ENTRIES) + size);
Harald Welteaae69be2004-08-29 23:32:14 +00001294 if (!h->entries)
1295 goto out_free_handle;
1296
1297 strcpy(h->entries->name, tablename);
Harald Welte0371c0c2004-09-19 21:00:12 +00001298 h->entries->size = size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001299
1300 return h;
Harald Welteaae69be2004-08-29 23:32:14 +00001301
1302out_free_handle:
1303 free(h);
1304
1305 return NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +00001306}
1307
Harald Welteaae69be2004-08-29 23:32:14 +00001308
Jan Engelhardtfd187312008-11-10 16:59:27 +01001309struct xtc_handle *
Rusty Russell79dee072000-05-02 16:45:16 +00001310TC_INIT(const char *tablename)
Marc Bouchere6869a82000-03-20 06:03:29 +00001311{
Jan Engelhardtfd187312008-11-10 16:59:27 +01001312 struct xtc_handle *h;
Rusty Russell79dee072000-05-02 16:45:16 +00001313 STRUCT_GETINFO info;
Harald Welteefa8fc22005-07-19 22:03:49 +00001314 unsigned int tmp;
Marc Bouchere6869a82000-03-20 06:03:29 +00001315 socklen_t s;
Jan Engelhardt175f4512008-11-10 17:25:55 +01001316 int sockfd;
Marc Bouchere6869a82000-03-20 06:03:29 +00001317
Rusty Russell79dee072000-05-02 16:45:16 +00001318 iptc_fn = TC_INIT;
Marc Bouchere6869a82000-03-20 06:03:29 +00001319
Martin Josefsson841e4ae2003-05-02 15:30:11 +00001320 if (strlen(tablename) >= TABLE_MAXNAMELEN) {
1321 errno = EINVAL;
1322 return NULL;
1323 }
Jan Engelhardt175f4512008-11-10 17:25:55 +01001324
1325 sockfd = socket(TC_AF, SOCK_RAW, IPPROTO_RAW);
1326 if (sockfd < 0)
1327 return NULL;
1328
Patrick McHardy2f932052008-04-02 14:01:53 +02001329retry:
Marc Bouchere6869a82000-03-20 06:03:29 +00001330 s = sizeof(info);
Martin Josefsson841e4ae2003-05-02 15:30:11 +00001331
Marc Bouchere6869a82000-03-20 06:03:29 +00001332 strcpy(info.name, tablename);
Derrik Pates664c0a32005-02-01 13:28:14 +00001333 if (getsockopt(sockfd, TC_IPPROTO, SO_GET_INFO, &info, &s) < 0) {
Jan Engelhardt175f4512008-11-10 17:25:55 +01001334 close(sockfd);
Marc Bouchere6869a82000-03-20 06:03:29 +00001335 return NULL;
Derrik Pates664c0a32005-02-01 13:28:14 +00001336 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001337
Harald Welteaae69be2004-08-29 23:32:14 +00001338 DEBUGP("valid_hooks=0x%08x, num_entries=%u, size=%u\n",
1339 info.valid_hooks, info.num_entries, info.size);
1340
Harald Welte0113fe72004-01-06 19:04:02 +00001341 if ((h = alloc_handle(info.name, info.size, info.num_entries))
Martin Josefsson841e4ae2003-05-02 15:30:11 +00001342 == NULL) {
Jan Engelhardt175f4512008-11-10 17:25:55 +01001343 close(sockfd);
Marc Bouchere6869a82000-03-20 06:03:29 +00001344 return NULL;
Martin Josefsson841e4ae2003-05-02 15:30:11 +00001345 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001346
Marc Bouchere6869a82000-03-20 06:03:29 +00001347 /* Initialize current state */
Jan Engelhardt175f4512008-11-10 17:25:55 +01001348 h->sockfd = sockfd;
Marc Bouchere6869a82000-03-20 06:03:29 +00001349 h->info = info;
Harald Welte0113fe72004-01-06 19:04:02 +00001350
Harald Welteaae69be2004-08-29 23:32:14 +00001351 h->entries->size = h->info.size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001352
Rusty Russell79dee072000-05-02 16:45:16 +00001353 tmp = sizeof(STRUCT_GET_ENTRIES) + h->info.size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001354
Jan Engelhardt175f4512008-11-10 17:25:55 +01001355 if (getsockopt(h->sockfd, TC_IPPROTO, SO_GET_ENTRIES, h->entries,
Harald Welteaae69be2004-08-29 23:32:14 +00001356 &tmp) < 0)
1357 goto error;
1358
1359#ifdef IPTC_DEBUG2
1360 {
Jesper Dangaard Brouera9fe5b32009-03-23 14:26:56 +01001361 int fd = open("/tmp/libiptc-so_get_entries.blob",
Harald Welteaae69be2004-08-29 23:32:14 +00001362 O_CREAT|O_WRONLY);
1363 if (fd >= 0) {
1364 write(fd, h->entries, tmp);
1365 close(fd);
1366 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001367 }
Harald Welteaae69be2004-08-29 23:32:14 +00001368#endif
1369
1370 if (parse_table(h) < 0)
1371 goto error;
Rusty Russell7e53bf92000-03-20 07:03:28 +00001372
Marc Bouchere6869a82000-03-20 06:03:29 +00001373 CHECK(h);
1374 return h;
Harald Welteaae69be2004-08-29 23:32:14 +00001375error:
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001376 TC_FREE(h);
Patrick McHardy2f932052008-04-02 14:01:53 +02001377 /* A different process changed the ruleset size, retry */
1378 if (errno == EAGAIN)
1379 goto retry;
Harald Welteaae69be2004-08-29 23:32:14 +00001380 return NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +00001381}
1382
Martin Josefsson841e4ae2003-05-02 15:30:11 +00001383void
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001384TC_FREE(struct xtc_handle *h)
Martin Josefsson841e4ae2003-05-02 15:30:11 +00001385{
Harald Welteaae69be2004-08-29 23:32:14 +00001386 struct chain_head *c, *tmp;
1387
Derrik Pates664c0a32005-02-01 13:28:14 +00001388 iptc_fn = TC_FREE;
Jan Engelhardt175f4512008-11-10 17:25:55 +01001389 close(h->sockfd);
Harald Welteaae69be2004-08-29 23:32:14 +00001390
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001391 list_for_each_entry_safe(c, tmp, &h->chains, list) {
Harald Welteaae69be2004-08-29 23:32:14 +00001392 struct rule_head *r, *rtmp;
1393
1394 list_for_each_entry_safe(r, rtmp, &c->rules, list) {
1395 free(r);
1396 }
1397
1398 free(c);
1399 }
1400
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001401 iptcc_chain_index_free(h);
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +00001402
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001403 free(h->entries);
1404 free(h);
Martin Josefsson841e4ae2003-05-02 15:30:11 +00001405}
1406
Marc Bouchere6869a82000-03-20 06:03:29 +00001407static inline int
Rusty Russell79dee072000-05-02 16:45:16 +00001408print_match(const STRUCT_ENTRY_MATCH *m)
Marc Bouchere6869a82000-03-20 06:03:29 +00001409{
Rusty Russell228e98d2000-04-27 10:28:06 +00001410 printf("Match name: `%s'\n", m->u.user.name);
Marc Bouchere6869a82000-03-20 06:03:29 +00001411 return 0;
1412}
1413
Jan Engelhardtfd187312008-11-10 16:59:27 +01001414static int dump_entry(STRUCT_ENTRY *e, struct xtc_handle *const handle);
Jesper Dangaard Brouera9fe5b32009-03-23 14:26:56 +01001415
Marc Bouchere6869a82000-03-20 06:03:29 +00001416void
Jan Engelhardtfd187312008-11-10 16:59:27 +01001417TC_DUMP_ENTRIES(struct xtc_handle *const handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001418{
Derrik Pates664c0a32005-02-01 13:28:14 +00001419 iptc_fn = TC_DUMP_ENTRIES;
Marc Bouchere6869a82000-03-20 06:03:29 +00001420 CHECK(handle);
Patrick McHardy97fb2f12007-09-08 16:52:25 +00001421
Rusty Russelle45c7132004-12-16 13:21:44 +00001422 printf("libiptc v%s. %u bytes.\n",
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +02001423 XTABLES_VERSION, handle->entries->size);
Marc Bouchere6869a82000-03-20 06:03:29 +00001424 printf("Table `%s'\n", handle->info.name);
Jan Engelhardtd73af642008-11-10 17:07:31 +01001425 printf("Hooks: pre/in/fwd/out/post = %x/%x/%x/%x/%x\n",
Rusty Russell67088e72000-05-10 01:18:57 +00001426 handle->info.hook_entry[HOOK_PRE_ROUTING],
1427 handle->info.hook_entry[HOOK_LOCAL_IN],
1428 handle->info.hook_entry[HOOK_FORWARD],
1429 handle->info.hook_entry[HOOK_LOCAL_OUT],
1430 handle->info.hook_entry[HOOK_POST_ROUTING]);
Jan Engelhardtd73af642008-11-10 17:07:31 +01001431 printf("Underflows: pre/in/fwd/out/post = %x/%x/%x/%x/%x\n",
Rusty Russell67088e72000-05-10 01:18:57 +00001432 handle->info.underflow[HOOK_PRE_ROUTING],
1433 handle->info.underflow[HOOK_LOCAL_IN],
1434 handle->info.underflow[HOOK_FORWARD],
1435 handle->info.underflow[HOOK_LOCAL_OUT],
1436 handle->info.underflow[HOOK_POST_ROUTING]);
Marc Bouchere6869a82000-03-20 06:03:29 +00001437
Harald Welteaae69be2004-08-29 23:32:14 +00001438 ENTRY_ITERATE(handle->entries->entrytable, handle->entries->size,
Rusty Russell79dee072000-05-02 16:45:16 +00001439 dump_entry, handle);
Harald Welte0113fe72004-01-06 19:04:02 +00001440}
Rusty Russell30fd6e52000-04-23 09:16:06 +00001441
Marc Bouchere6869a82000-03-20 06:03:29 +00001442/* Does this chain exist? */
Jan Engelhardtfd187312008-11-10 16:59:27 +01001443int TC_IS_CHAIN(const char *chain, struct xtc_handle *const handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001444{
Derrik Pates664c0a32005-02-01 13:28:14 +00001445 iptc_fn = TC_IS_CHAIN;
Harald Welteaae69be2004-08-29 23:32:14 +00001446 return iptcc_find_label(chain, handle) != NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +00001447}
1448
Jan Engelhardtfd187312008-11-10 16:59:27 +01001449static void iptcc_chain_iterator_advance(struct xtc_handle *handle)
Harald Welteaae69be2004-08-29 23:32:14 +00001450{
1451 struct chain_head *c = handle->chain_iterator_cur;
1452
1453 if (c->list.next == &handle->chains)
1454 handle->chain_iterator_cur = NULL;
1455 else
Jesper Dangaard Brouera9fe5b32009-03-23 14:26:56 +01001456 handle->chain_iterator_cur =
Harald Welteaae69be2004-08-29 23:32:14 +00001457 list_entry(c->list.next, struct chain_head, list);
1458}
Marc Bouchere6869a82000-03-20 06:03:29 +00001459
Rusty Russell30fd6e52000-04-23 09:16:06 +00001460/* Iterator functions to run through the chains. */
Marc Bouchere6869a82000-03-20 06:03:29 +00001461const char *
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001462TC_FIRST_CHAIN(struct xtc_handle *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001463{
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001464 struct chain_head *c = list_entry(handle->chains.next,
Harald Welteaae69be2004-08-29 23:32:14 +00001465 struct chain_head, list);
1466
1467 iptc_fn = TC_FIRST_CHAIN;
1468
1469
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001470 if (list_empty(&handle->chains)) {
Harald Welteaae69be2004-08-29 23:32:14 +00001471 DEBUGP(": no chains\n");
Harald Welte0113fe72004-01-06 19:04:02 +00001472 return NULL;
Harald Welteaae69be2004-08-29 23:32:14 +00001473 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001474
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001475 handle->chain_iterator_cur = c;
1476 iptcc_chain_iterator_advance(handle);
Harald Welte0113fe72004-01-06 19:04:02 +00001477
Harald Welteaae69be2004-08-29 23:32:14 +00001478 DEBUGP(": returning `%s'\n", c->name);
1479 return c->name;
Marc Bouchere6869a82000-03-20 06:03:29 +00001480}
1481
Rusty Russell30fd6e52000-04-23 09:16:06 +00001482/* Iterator functions to run through the chains. Returns NULL at end. */
1483const char *
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001484TC_NEXT_CHAIN(struct xtc_handle *handle)
Rusty Russell30fd6e52000-04-23 09:16:06 +00001485{
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001486 struct chain_head *c = handle->chain_iterator_cur;
Rusty Russell30fd6e52000-04-23 09:16:06 +00001487
Harald Welteaae69be2004-08-29 23:32:14 +00001488 iptc_fn = TC_NEXT_CHAIN;
1489
1490 if (!c) {
1491 DEBUGP(": no more chains\n");
Rusty Russell30fd6e52000-04-23 09:16:06 +00001492 return NULL;
Harald Welteaae69be2004-08-29 23:32:14 +00001493 }
Rusty Russell30fd6e52000-04-23 09:16:06 +00001494
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001495 iptcc_chain_iterator_advance(handle);
Jesper Dangaard Brouera9fe5b32009-03-23 14:26:56 +01001496
Harald Welteaae69be2004-08-29 23:32:14 +00001497 DEBUGP(": returning `%s'\n", c->name);
1498 return c->name;
Rusty Russell30fd6e52000-04-23 09:16:06 +00001499}
1500
1501/* Get first rule in the given chain: NULL for empty chain. */
Rusty Russell79dee072000-05-02 16:45:16 +00001502const STRUCT_ENTRY *
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001503TC_FIRST_RULE(const char *chain, struct xtc_handle *handle)
Rusty Russell30fd6e52000-04-23 09:16:06 +00001504{
Harald Welteaae69be2004-08-29 23:32:14 +00001505 struct chain_head *c;
1506 struct rule_head *r;
Rusty Russell30fd6e52000-04-23 09:16:06 +00001507
Harald Welteaae69be2004-08-29 23:32:14 +00001508 iptc_fn = TC_FIRST_RULE;
1509
1510 DEBUGP("first rule(%s): ", chain);
1511
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001512 c = iptcc_find_label(chain, handle);
Rusty Russell30fd6e52000-04-23 09:16:06 +00001513 if (!c) {
1514 errno = ENOENT;
1515 return NULL;
1516 }
1517
1518 /* Empty chain: single return/policy rule */
Harald Welteaae69be2004-08-29 23:32:14 +00001519 if (list_empty(&c->rules)) {
1520 DEBUGP_C("no rules, returning NULL\n");
Rusty Russell30fd6e52000-04-23 09:16:06 +00001521 return NULL;
Harald Welteaae69be2004-08-29 23:32:14 +00001522 }
Rusty Russell30fd6e52000-04-23 09:16:06 +00001523
Harald Welteaae69be2004-08-29 23:32:14 +00001524 r = list_entry(c->rules.next, struct rule_head, list);
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001525 handle->rule_iterator_cur = r;
Harald Welteaae69be2004-08-29 23:32:14 +00001526 DEBUGP_C("%p\n", r);
1527
1528 return r->entry;
Rusty Russell30fd6e52000-04-23 09:16:06 +00001529}
1530
1531/* Returns NULL when rules run out. */
Rusty Russell79dee072000-05-02 16:45:16 +00001532const STRUCT_ENTRY *
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001533TC_NEXT_RULE(const STRUCT_ENTRY *prev, struct xtc_handle *handle)
Rusty Russell30fd6e52000-04-23 09:16:06 +00001534{
Harald Welteaae69be2004-08-29 23:32:14 +00001535 struct rule_head *r;
Rusty Russell30fd6e52000-04-23 09:16:06 +00001536
Derrik Pates664c0a32005-02-01 13:28:14 +00001537 iptc_fn = TC_NEXT_RULE;
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001538 DEBUGP("rule_iterator_cur=%p...", handle->rule_iterator_cur);
Harald Welteaae69be2004-08-29 23:32:14 +00001539
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001540 if (handle->rule_iterator_cur == NULL) {
Harald Welteaae69be2004-08-29 23:32:14 +00001541 DEBUGP_C("returning NULL\n");
1542 return NULL;
1543 }
Jesper Dangaard Brouera9fe5b32009-03-23 14:26:56 +01001544
1545 r = list_entry(handle->rule_iterator_cur->list.next,
Harald Welteaae69be2004-08-29 23:32:14 +00001546 struct rule_head, list);
1547
1548 iptc_fn = TC_NEXT_RULE;
1549
Jesper Dangaard Brouera9fe5b32009-03-23 14:26:56 +01001550 DEBUGP_C("next=%p, head=%p...", &r->list,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001551 &handle->rule_iterator_cur->chain->rules);
Harald Welteaae69be2004-08-29 23:32:14 +00001552
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001553 if (&r->list == &handle->rule_iterator_cur->chain->rules) {
1554 handle->rule_iterator_cur = NULL;
Harald Welteaae69be2004-08-29 23:32:14 +00001555 DEBUGP_C("finished, returning NULL\n");
1556 return NULL;
1557 }
1558
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001559 handle->rule_iterator_cur = r;
Harald Welteaae69be2004-08-29 23:32:14 +00001560
1561 /* NOTE: prev is without any influence ! */
1562 DEBUGP_C("returning rule %p\n", r);
1563 return r->entry;
Rusty Russell30fd6e52000-04-23 09:16:06 +00001564}
1565
Marc Bouchere6869a82000-03-20 06:03:29 +00001566/* Returns a pointer to the target name of this position. */
Jan Engelhardt33690a12008-02-11 00:54:00 +01001567static const char *standard_target_map(int verdict)
Marc Bouchere6869a82000-03-20 06:03:29 +00001568{
Harald Welteaae69be2004-08-29 23:32:14 +00001569 switch (verdict) {
1570 case RETURN:
1571 return LABEL_RETURN;
1572 break;
1573 case -NF_ACCEPT-1:
1574 return LABEL_ACCEPT;
1575 break;
1576 case -NF_DROP-1:
1577 return LABEL_DROP;
1578 break;
1579 case -NF_QUEUE-1:
1580 return LABEL_QUEUE;
1581 break;
1582 default:
1583 fprintf(stderr, "ERROR: %d not a valid target)\n",
1584 verdict);
1585 abort();
1586 break;
1587 }
1588 /* not reached */
1589 return NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +00001590}
1591
Harald Welteaae69be2004-08-29 23:32:14 +00001592/* Returns a pointer to the target name of this position. */
1593const char *TC_GET_TARGET(const STRUCT_ENTRY *ce,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001594 struct xtc_handle *handle)
Harald Welteaae69be2004-08-29 23:32:14 +00001595{
1596 STRUCT_ENTRY *e = (STRUCT_ENTRY *)ce;
Rusty Russelle45c7132004-12-16 13:21:44 +00001597 struct rule_head *r = container_of(e, struct rule_head, entry[0]);
Jan Engelhardt51651b62009-10-23 23:35:49 +02001598 const unsigned char *data;
Harald Welteaae69be2004-08-29 23:32:14 +00001599
1600 iptc_fn = TC_GET_TARGET;
1601
1602 switch(r->type) {
1603 int spos;
1604 case IPTCC_R_FALLTHROUGH:
1605 return "";
1606 break;
1607 case IPTCC_R_JUMP:
1608 DEBUGP("r=%p, jump=%p, name=`%s'\n", r, r->jump, r->jump->name);
1609 return r->jump->name;
1610 break;
1611 case IPTCC_R_STANDARD:
Jan Engelhardt51651b62009-10-23 23:35:49 +02001612 data = GET_TARGET(e)->data;
1613 spos = *(const int *)data;
Harald Welteaae69be2004-08-29 23:32:14 +00001614 DEBUGP("r=%p, spos=%d'\n", r, spos);
1615 return standard_target_map(spos);
1616 break;
1617 case IPTCC_R_MODULE:
1618 return GET_TARGET(e)->u.user.name;
1619 break;
1620 }
1621 return NULL;
1622}
Marc Bouchere6869a82000-03-20 06:03:29 +00001623/* Is this a built-in chain? Actually returns hook + 1. */
1624int
Jan Engelhardtfd187312008-11-10 16:59:27 +01001625TC_BUILTIN(const char *chain, struct xtc_handle *const handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001626{
Harald Welteaae69be2004-08-29 23:32:14 +00001627 struct chain_head *c;
Jesper Dangaard Brouera9fe5b32009-03-23 14:26:56 +01001628
Harald Welteaae69be2004-08-29 23:32:14 +00001629 iptc_fn = TC_BUILTIN;
Marc Bouchere6869a82000-03-20 06:03:29 +00001630
Harald Welteaae69be2004-08-29 23:32:14 +00001631 c = iptcc_find_label(chain, handle);
1632 if (!c) {
1633 errno = ENOENT;
Martin Josefssonb0f3d2d2004-09-23 18:23:20 +00001634 return 0;
Marc Bouchere6869a82000-03-20 06:03:29 +00001635 }
Harald Welteaae69be2004-08-29 23:32:14 +00001636
1637 return iptcc_is_builtin(c);
Marc Bouchere6869a82000-03-20 06:03:29 +00001638}
1639
1640/* Get the policy of a given built-in chain */
1641const char *
Rusty Russell79dee072000-05-02 16:45:16 +00001642TC_GET_POLICY(const char *chain,
1643 STRUCT_COUNTERS *counters,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001644 struct xtc_handle *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001645{
Harald Welteaae69be2004-08-29 23:32:14 +00001646 struct chain_head *c;
Marc Bouchere6869a82000-03-20 06:03:29 +00001647
Harald Welteaae69be2004-08-29 23:32:14 +00001648 iptc_fn = TC_GET_POLICY;
1649
1650 DEBUGP("called for chain %s\n", chain);
1651
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001652 c = iptcc_find_label(chain, handle);
Harald Welteaae69be2004-08-29 23:32:14 +00001653 if (!c) {
1654 errno = ENOENT;
1655 return NULL;
1656 }
1657
1658 if (!iptcc_is_builtin(c))
Marc Bouchere6869a82000-03-20 06:03:29 +00001659 return NULL;
1660
Harald Welteaae69be2004-08-29 23:32:14 +00001661 *counters = c->counters;
Marc Bouchere6869a82000-03-20 06:03:29 +00001662
Harald Welteaae69be2004-08-29 23:32:14 +00001663 return standard_target_map(c->verdict);
Harald Welte0113fe72004-01-06 19:04:02 +00001664}
1665
1666static int
Harald Welteaae69be2004-08-29 23:32:14 +00001667iptcc_standard_map(struct rule_head *r, int verdict)
Harald Welte0113fe72004-01-06 19:04:02 +00001668{
Harald Welteaae69be2004-08-29 23:32:14 +00001669 STRUCT_ENTRY *e = r->entry;
Rusty Russell79dee072000-05-02 16:45:16 +00001670 STRUCT_STANDARD_TARGET *t;
Marc Bouchere6869a82000-03-20 06:03:29 +00001671
Rusty Russell79dee072000-05-02 16:45:16 +00001672 t = (STRUCT_STANDARD_TARGET *)GET_TARGET(e);
Marc Bouchere6869a82000-03-20 06:03:29 +00001673
Rusty Russell67088e72000-05-10 01:18:57 +00001674 if (t->target.u.target_size
Philip Blundell8c700902000-05-15 02:17:52 +00001675 != ALIGN(sizeof(STRUCT_STANDARD_TARGET))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001676 errno = EINVAL;
1677 return 0;
1678 }
1679 /* memset for memcmp convenience on delete/replace */
Rusty Russell79dee072000-05-02 16:45:16 +00001680 memset(t->target.u.user.name, 0, FUNCTION_MAXNAMELEN);
1681 strcpy(t->target.u.user.name, STANDARD_TARGET);
Marc Bouchere6869a82000-03-20 06:03:29 +00001682 t->verdict = verdict;
1683
Harald Welteaae69be2004-08-29 23:32:14 +00001684 r->type = IPTCC_R_STANDARD;
1685
Marc Bouchere6869a82000-03-20 06:03:29 +00001686 return 1;
1687}
Rusty Russell7e53bf92000-03-20 07:03:28 +00001688
Marc Bouchere6869a82000-03-20 06:03:29 +00001689static int
Jan Engelhardtfd187312008-11-10 16:59:27 +01001690iptcc_map_target(struct xtc_handle *const handle,
Harald Welteaae69be2004-08-29 23:32:14 +00001691 struct rule_head *r)
Marc Bouchere6869a82000-03-20 06:03:29 +00001692{
Harald Welteaae69be2004-08-29 23:32:14 +00001693 STRUCT_ENTRY *e = r->entry;
Harald Welte0113fe72004-01-06 19:04:02 +00001694 STRUCT_ENTRY_TARGET *t = GET_TARGET(e);
Marc Bouchere6869a82000-03-20 06:03:29 +00001695
Marc Bouchere6869a82000-03-20 06:03:29 +00001696 /* Maybe it's empty (=> fall through) */
Harald Welteaae69be2004-08-29 23:32:14 +00001697 if (strcmp(t->u.user.name, "") == 0) {
1698 r->type = IPTCC_R_FALLTHROUGH;
1699 return 1;
1700 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001701 /* Maybe it's a standard target name... */
Rusty Russell79dee072000-05-02 16:45:16 +00001702 else if (strcmp(t->u.user.name, LABEL_ACCEPT) == 0)
Harald Welteaae69be2004-08-29 23:32:14 +00001703 return iptcc_standard_map(r, -NF_ACCEPT - 1);
Rusty Russell79dee072000-05-02 16:45:16 +00001704 else if (strcmp(t->u.user.name, LABEL_DROP) == 0)
Harald Welteaae69be2004-08-29 23:32:14 +00001705 return iptcc_standard_map(r, -NF_DROP - 1);
Rusty Russell67088e72000-05-10 01:18:57 +00001706 else if (strcmp(t->u.user.name, LABEL_QUEUE) == 0)
Harald Welteaae69be2004-08-29 23:32:14 +00001707 return iptcc_standard_map(r, -NF_QUEUE - 1);
Rusty Russell79dee072000-05-02 16:45:16 +00001708 else if (strcmp(t->u.user.name, LABEL_RETURN) == 0)
Harald Welteaae69be2004-08-29 23:32:14 +00001709 return iptcc_standard_map(r, RETURN);
Rusty Russell79dee072000-05-02 16:45:16 +00001710 else if (TC_BUILTIN(t->u.user.name, handle)) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001711 /* Can't jump to builtins. */
1712 errno = EINVAL;
1713 return 0;
1714 } else {
1715 /* Maybe it's an existing chain name. */
Harald Welteaae69be2004-08-29 23:32:14 +00001716 struct chain_head *c;
1717 DEBUGP("trying to find chain `%s': ", t->u.user.name);
Marc Bouchere6869a82000-03-20 06:03:29 +00001718
Harald Welteaae69be2004-08-29 23:32:14 +00001719 c = iptcc_find_label(t->u.user.name, handle);
1720 if (c) {
1721 DEBUGP_C("found!\n");
1722 r->type = IPTCC_R_JUMP;
1723 r->jump = c;
1724 c->references++;
1725 return 1;
1726 }
1727 DEBUGP_C("not found :(\n");
Marc Bouchere6869a82000-03-20 06:03:29 +00001728 }
1729
1730 /* Must be a module? If not, kernel will reject... */
Rusty Russell3aef54d2005-01-03 03:48:40 +00001731 /* memset to all 0 for your memcmp convenience: don't clear version */
Rusty Russell228e98d2000-04-27 10:28:06 +00001732 memset(t->u.user.name + strlen(t->u.user.name),
Marc Bouchere6869a82000-03-20 06:03:29 +00001733 0,
Rusty Russell3aef54d2005-01-03 03:48:40 +00001734 FUNCTION_MAXNAMELEN - 1 - strlen(t->u.user.name));
Rusty Russell733e54b2004-12-16 14:22:23 +00001735 r->type = IPTCC_R_MODULE;
Harald Welteaae69be2004-08-29 23:32:14 +00001736 set_changed(handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001737 return 1;
1738}
1739
Harald Welte0113fe72004-01-06 19:04:02 +00001740/* Insert the entry `fw' in chain `chain' into position `rulenum'. */
Marc Bouchere6869a82000-03-20 06:03:29 +00001741int
Rusty Russell79dee072000-05-02 16:45:16 +00001742TC_INSERT_ENTRY(const IPT_CHAINLABEL chain,
1743 const STRUCT_ENTRY *e,
1744 unsigned int rulenum,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001745 struct xtc_handle *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001746{
Harald Welteaae69be2004-08-29 23:32:14 +00001747 struct chain_head *c;
Martin Josefssoneb066cc2004-09-22 21:04:07 +00001748 struct rule_head *r;
1749 struct list_head *prev;
Marc Bouchere6869a82000-03-20 06:03:29 +00001750
Rusty Russell79dee072000-05-02 16:45:16 +00001751 iptc_fn = TC_INSERT_ENTRY;
Harald Welteaae69be2004-08-29 23:32:14 +00001752
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001753 if (!(c = iptcc_find_label(chain, handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001754 errno = ENOENT;
1755 return 0;
1756 }
1757
Martin Josefssoneb066cc2004-09-22 21:04:07 +00001758 /* first rulenum index = 0
1759 first c->num_rules index = 1 */
1760 if (rulenum > c->num_rules) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001761 errno = E2BIG;
1762 return 0;
1763 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001764
Martin Josefsson631f3612004-09-23 19:25:06 +00001765 /* If we are inserting at the end just take advantage of the
1766 double linked list, insert will happen before the entry
1767 prev points to. */
1768 if (rulenum == c->num_rules) {
Martin Josefssoneb066cc2004-09-22 21:04:07 +00001769 prev = &c->rules;
Martin Josefssona5616dc2004-10-24 22:27:31 +00001770 } else if (rulenum + 1 <= c->num_rules/2) {
Martin Josefsson631f3612004-09-23 19:25:06 +00001771 r = iptcc_get_rule_num(c, rulenum + 1);
Martin Josefssona5616dc2004-10-24 22:27:31 +00001772 prev = &r->list;
1773 } else {
1774 r = iptcc_get_rule_num_reverse(c, c->num_rules - rulenum);
Martin Josefsson631f3612004-09-23 19:25:06 +00001775 prev = &r->list;
1776 }
Martin Josefssoneb066cc2004-09-22 21:04:07 +00001777
Harald Welteaae69be2004-08-29 23:32:14 +00001778 if (!(r = iptcc_alloc_rule(c, e->next_offset))) {
1779 errno = ENOMEM;
Harald Welte0113fe72004-01-06 19:04:02 +00001780 return 0;
Harald Welteaae69be2004-08-29 23:32:14 +00001781 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001782
Harald Welteaae69be2004-08-29 23:32:14 +00001783 memcpy(r->entry, e, e->next_offset);
1784 r->counter_map.maptype = COUNTER_MAP_SET;
1785
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001786 if (!iptcc_map_target(handle, r)) {
Harald Welteaae69be2004-08-29 23:32:14 +00001787 free(r);
1788 return 0;
1789 }
1790
Martin Josefssoneb066cc2004-09-22 21:04:07 +00001791 list_add_tail(&r->list, prev);
Harald Welteaae69be2004-08-29 23:32:14 +00001792 c->num_rules++;
1793
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001794 set_changed(handle);
Harald Welteaae69be2004-08-29 23:32:14 +00001795
1796 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00001797}
1798
1799/* Atomically replace rule `rulenum' in `chain' with `fw'. */
1800int
Rusty Russell79dee072000-05-02 16:45:16 +00001801TC_REPLACE_ENTRY(const IPT_CHAINLABEL chain,
1802 const STRUCT_ENTRY *e,
1803 unsigned int rulenum,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001804 struct xtc_handle *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001805{
Harald Welteaae69be2004-08-29 23:32:14 +00001806 struct chain_head *c;
1807 struct rule_head *r, *old;
Marc Bouchere6869a82000-03-20 06:03:29 +00001808
Rusty Russell79dee072000-05-02 16:45:16 +00001809 iptc_fn = TC_REPLACE_ENTRY;
Marc Bouchere6869a82000-03-20 06:03:29 +00001810
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001811 if (!(c = iptcc_find_label(chain, handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001812 errno = ENOENT;
1813 return 0;
1814 }
1815
Martin Josefsson0f9b8b12004-12-18 17:18:49 +00001816 if (rulenum >= c->num_rules) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001817 errno = E2BIG;
1818 return 0;
1819 }
1820
Martin Josefsson0f9b8b12004-12-18 17:18:49 +00001821 /* Take advantage of the double linked list if possible. */
1822 if (rulenum + 1 <= c->num_rules/2) {
1823 old = iptcc_get_rule_num(c, rulenum + 1);
1824 } else {
1825 old = iptcc_get_rule_num_reverse(c, c->num_rules - rulenum);
1826 }
1827
Harald Welteaae69be2004-08-29 23:32:14 +00001828 if (!(r = iptcc_alloc_rule(c, e->next_offset))) {
1829 errno = ENOMEM;
Marc Bouchere6869a82000-03-20 06:03:29 +00001830 return 0;
Harald Welteaae69be2004-08-29 23:32:14 +00001831 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001832
Harald Welteaae69be2004-08-29 23:32:14 +00001833 memcpy(r->entry, e, e->next_offset);
1834 r->counter_map.maptype = COUNTER_MAP_SET;
1835
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001836 if (!iptcc_map_target(handle, r)) {
Harald Welteaae69be2004-08-29 23:32:14 +00001837 free(r);
Harald Welte0113fe72004-01-06 19:04:02 +00001838 return 0;
Harald Welteaae69be2004-08-29 23:32:14 +00001839 }
Harald Welte0113fe72004-01-06 19:04:02 +00001840
Harald Welteaae69be2004-08-29 23:32:14 +00001841 list_add(&r->list, &old->list);
1842 iptcc_delete_rule(old);
1843
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001844 set_changed(handle);
Harald Welteaae69be2004-08-29 23:32:14 +00001845
1846 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00001847}
1848
Harald Welte0113fe72004-01-06 19:04:02 +00001849/* Append entry `fw' to chain `chain'. Equivalent to insert with
Marc Bouchere6869a82000-03-20 06:03:29 +00001850 rulenum = length of chain. */
1851int
Rusty Russell79dee072000-05-02 16:45:16 +00001852TC_APPEND_ENTRY(const IPT_CHAINLABEL chain,
1853 const STRUCT_ENTRY *e,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001854 struct xtc_handle *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001855{
Harald Welteaae69be2004-08-29 23:32:14 +00001856 struct chain_head *c;
1857 struct rule_head *r;
Marc Bouchere6869a82000-03-20 06:03:29 +00001858
Rusty Russell79dee072000-05-02 16:45:16 +00001859 iptc_fn = TC_APPEND_ENTRY;
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001860 if (!(c = iptcc_find_label(chain, handle))) {
Harald Welteaae69be2004-08-29 23:32:14 +00001861 DEBUGP("unable to find chain `%s'\n", chain);
Marc Bouchere6869a82000-03-20 06:03:29 +00001862 errno = ENOENT;
1863 return 0;
1864 }
1865
Harald Welteaae69be2004-08-29 23:32:14 +00001866 if (!(r = iptcc_alloc_rule(c, e->next_offset))) {
1867 DEBUGP("unable to allocate rule for chain `%s'\n", chain);
1868 errno = ENOMEM;
Harald Welte0113fe72004-01-06 19:04:02 +00001869 return 0;
Harald Welteaae69be2004-08-29 23:32:14 +00001870 }
Harald Welte0113fe72004-01-06 19:04:02 +00001871
Harald Welteaae69be2004-08-29 23:32:14 +00001872 memcpy(r->entry, e, e->next_offset);
1873 r->counter_map.maptype = COUNTER_MAP_SET;
1874
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001875 if (!iptcc_map_target(handle, r)) {
Martin Josefsson12009532004-09-23 18:24:29 +00001876 DEBUGP("unable to map target of rule for chain `%s'\n", chain);
Harald Welteaae69be2004-08-29 23:32:14 +00001877 free(r);
1878 return 0;
1879 }
1880
1881 list_add_tail(&r->list, &c->rules);
1882 c->num_rules++;
1883
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001884 set_changed(handle);
Harald Welteaae69be2004-08-29 23:32:14 +00001885
1886 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00001887}
1888
1889static inline int
Rusty Russell79dee072000-05-02 16:45:16 +00001890match_different(const STRUCT_ENTRY_MATCH *a,
Rusty Russelledf14cf2000-04-19 11:26:44 +00001891 const unsigned char *a_elems,
1892 const unsigned char *b_elems,
1893 unsigned char **maskptr)
Marc Bouchere6869a82000-03-20 06:03:29 +00001894{
Rusty Russell79dee072000-05-02 16:45:16 +00001895 const STRUCT_ENTRY_MATCH *b;
Rusty Russelledf14cf2000-04-19 11:26:44 +00001896 unsigned int i;
Marc Bouchere6869a82000-03-20 06:03:29 +00001897
1898 /* Offset of b is the same as a. */
Rusty Russell30fd6e52000-04-23 09:16:06 +00001899 b = (void *)b_elems + ((unsigned char *)a - a_elems);
Marc Bouchere6869a82000-03-20 06:03:29 +00001900
Rusty Russell228e98d2000-04-27 10:28:06 +00001901 if (a->u.match_size != b->u.match_size)
Marc Bouchere6869a82000-03-20 06:03:29 +00001902 return 1;
1903
Rusty Russell228e98d2000-04-27 10:28:06 +00001904 if (strcmp(a->u.user.name, b->u.user.name) != 0)
Marc Bouchere6869a82000-03-20 06:03:29 +00001905 return 1;
1906
Rusty Russell73ef09b2000-07-03 10:24:04 +00001907 *maskptr += ALIGN(sizeof(*a));
Rusty Russelledf14cf2000-04-19 11:26:44 +00001908
Rusty Russell73ef09b2000-07-03 10:24:04 +00001909 for (i = 0; i < a->u.match_size - ALIGN(sizeof(*a)); i++)
Rusty Russelledf14cf2000-04-19 11:26:44 +00001910 if (((a->data[i] ^ b->data[i]) & (*maskptr)[i]) != 0)
Rusty Russell90e712a2000-03-29 04:19:26 +00001911 return 1;
Rusty Russelledf14cf2000-04-19 11:26:44 +00001912 *maskptr += i;
1913 return 0;
1914}
1915
1916static inline int
Rusty Russell733e54b2004-12-16 14:22:23 +00001917target_same(struct rule_head *a, struct rule_head *b,const unsigned char *mask)
Rusty Russelledf14cf2000-04-19 11:26:44 +00001918{
1919 unsigned int i;
Rusty Russell733e54b2004-12-16 14:22:23 +00001920 STRUCT_ENTRY_TARGET *ta, *tb;
Marc Bouchere6869a82000-03-20 06:03:29 +00001921
Rusty Russell733e54b2004-12-16 14:22:23 +00001922 if (a->type != b->type)
1923 return 0;
1924
1925 ta = GET_TARGET(a->entry);
1926 tb = GET_TARGET(b->entry);
1927
1928 switch (a->type) {
1929 case IPTCC_R_FALLTHROUGH:
1930 return 1;
1931 case IPTCC_R_JUMP:
1932 return a->jump == b->jump;
1933 case IPTCC_R_STANDARD:
1934 return ((STRUCT_STANDARD_TARGET *)ta)->verdict
1935 == ((STRUCT_STANDARD_TARGET *)tb)->verdict;
1936 case IPTCC_R_MODULE:
1937 if (ta->u.target_size != tb->u.target_size)
1938 return 0;
1939 if (strcmp(ta->u.user.name, tb->u.user.name) != 0)
1940 return 0;
1941
1942 for (i = 0; i < ta->u.target_size - sizeof(*ta); i++)
Rusty Russelldaade442004-12-29 11:14:52 +00001943 if (((ta->data[i] ^ tb->data[i]) & mask[i]) != 0)
Rusty Russell733e54b2004-12-16 14:22:23 +00001944 return 0;
1945 return 1;
1946 default:
1947 fprintf(stderr, "ERROR: bad type %i\n", a->type);
1948 abort();
1949 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001950}
1951
Rusty Russell733e54b2004-12-16 14:22:23 +00001952static unsigned char *
Rusty Russell79dee072000-05-02 16:45:16 +00001953is_same(const STRUCT_ENTRY *a,
1954 const STRUCT_ENTRY *b,
1955 unsigned char *matchmask);
Marc Bouchere6869a82000-03-20 06:03:29 +00001956
Stefan Tomanekd59b9db2011-03-08 22:42:51 +01001957
1958/* find the first rule in `chain' which matches `fw' and remove it unless dry_run is set */
1959static int delete_entry(const IPT_CHAINLABEL chain, const STRUCT_ENTRY *origfw,
1960 unsigned char *matchmask, struct xtc_handle *handle,
1961 bool dry_run)
Marc Bouchere6869a82000-03-20 06:03:29 +00001962{
Harald Welteaae69be2004-08-29 23:32:14 +00001963 struct chain_head *c;
Rusty Russelle45c7132004-12-16 13:21:44 +00001964 struct rule_head *r, *i;
Marc Bouchere6869a82000-03-20 06:03:29 +00001965
Rusty Russell79dee072000-05-02 16:45:16 +00001966 iptc_fn = TC_DELETE_ENTRY;
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001967 if (!(c = iptcc_find_label(chain, handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001968 errno = ENOENT;
1969 return 0;
1970 }
1971
Rusty Russelle45c7132004-12-16 13:21:44 +00001972 /* Create a rule_head from origfw. */
1973 r = iptcc_alloc_rule(c, origfw->next_offset);
1974 if (!r) {
Harald Welte0113fe72004-01-06 19:04:02 +00001975 errno = ENOMEM;
1976 return 0;
1977 }
1978
Rusty Russelle45c7132004-12-16 13:21:44 +00001979 memcpy(r->entry, origfw, origfw->next_offset);
1980 r->counter_map.maptype = COUNTER_MAP_NOMAP;
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001981 if (!iptcc_map_target(handle, r)) {
Rusty Russelle45c7132004-12-16 13:21:44 +00001982 DEBUGP("unable to map target of rule for chain `%s'\n", chain);
1983 free(r);
1984 return 0;
Patrick McHardyJesper Brouer04a1e4c2006-07-25 01:50:48 +00001985 } else {
1986 /* iptcc_map_target increment target chain references
1987 * since this is a fake rule only used for matching
Jesper Dangaard Brouera9fe5b32009-03-23 14:26:56 +01001988 * the chain references count is decremented again.
Patrick McHardyJesper Brouer04a1e4c2006-07-25 01:50:48 +00001989 */
1990 if (r->type == IPTCC_R_JUMP
1991 && r->jump)
1992 r->jump->references--;
Rusty Russelle45c7132004-12-16 13:21:44 +00001993 }
Harald Welte0113fe72004-01-06 19:04:02 +00001994
Rusty Russelle45c7132004-12-16 13:21:44 +00001995 list_for_each_entry(i, &c->rules, list) {
Rusty Russell733e54b2004-12-16 14:22:23 +00001996 unsigned char *mask;
Harald Weltefe537072004-08-30 20:28:53 +00001997
Rusty Russell733e54b2004-12-16 14:22:23 +00001998 mask = is_same(r->entry, i->entry, matchmask);
1999 if (!mask)
2000 continue;
Martin Josefsson2a5dbbb2004-09-22 21:37:41 +00002001
Rusty Russell733e54b2004-12-16 14:22:23 +00002002 if (!target_same(r, i, mask))
2003 continue;
2004
Stefan Tomanekd59b9db2011-03-08 22:42:51 +01002005 /* if we are just doing a dry run, we simply skip the rest */
2006 if (dry_run)
2007 return 1;
2008
Rusty Russell733e54b2004-12-16 14:22:23 +00002009 /* If we are about to delete the rule that is the
2010 * current iterator, move rule iterator back. next
2011 * pointer will then point to real next node */
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002012 if (i == handle->rule_iterator_cur) {
Jesper Dangaard Brouera9fe5b32009-03-23 14:26:56 +01002013 handle->rule_iterator_cur =
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002014 list_entry(handle->rule_iterator_cur->list.prev,
Rusty Russell733e54b2004-12-16 14:22:23 +00002015 struct rule_head, list);
Marc Bouchere6869a82000-03-20 06:03:29 +00002016 }
Rusty Russell733e54b2004-12-16 14:22:23 +00002017
2018 c->num_rules--;
2019 iptcc_delete_rule(i);
2020
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002021 set_changed(handle);
Rusty Russell733e54b2004-12-16 14:22:23 +00002022 free(r);
2023 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00002024 }
2025
Rusty Russelle45c7132004-12-16 13:21:44 +00002026 free(r);
Marc Bouchere6869a82000-03-20 06:03:29 +00002027 errno = ENOENT;
2028 return 0;
Rusty Russell7e53bf92000-03-20 07:03:28 +00002029}
Harald Welteaae69be2004-08-29 23:32:14 +00002030
Stefan Tomanekd59b9db2011-03-08 22:42:51 +01002031/* check whether a specified rule is present */
2032int TC_CHECK_ENTRY(const IPT_CHAINLABEL chain, const STRUCT_ENTRY *origfw,
2033 unsigned char *matchmask, struct xtc_handle *handle)
2034{
2035 /* do a dry-run delete to find out whether a matching rule exists */
2036 return delete_entry(chain, origfw, matchmask, handle, true);
2037}
2038
2039/* Delete the first rule in `chain' which matches `fw'. */
2040int TC_DELETE_ENTRY(const IPT_CHAINLABEL chain, const STRUCT_ENTRY *origfw,
2041 unsigned char *matchmask, struct xtc_handle *handle)
2042{
2043 return delete_entry(chain, origfw, matchmask, handle, false);
2044}
Marc Bouchere6869a82000-03-20 06:03:29 +00002045
2046/* Delete the rule in position `rulenum' in `chain'. */
2047int
Rusty Russell79dee072000-05-02 16:45:16 +00002048TC_DELETE_NUM_ENTRY(const IPT_CHAINLABEL chain,
2049 unsigned int rulenum,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002050 struct xtc_handle *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00002051{
Harald Welteaae69be2004-08-29 23:32:14 +00002052 struct chain_head *c;
2053 struct rule_head *r;
Marc Bouchere6869a82000-03-20 06:03:29 +00002054
Rusty Russell79dee072000-05-02 16:45:16 +00002055 iptc_fn = TC_DELETE_NUM_ENTRY;
Harald Welteaae69be2004-08-29 23:32:14 +00002056
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002057 if (!(c = iptcc_find_label(chain, handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002058 errno = ENOENT;
2059 return 0;
2060 }
2061
Martin Josefssona5616dc2004-10-24 22:27:31 +00002062 if (rulenum >= c->num_rules) {
Martin Josefsson631f3612004-09-23 19:25:06 +00002063 errno = E2BIG;
2064 return 0;
2065 }
2066
2067 /* Take advantage of the double linked list if possible. */
Martin Josefssona5616dc2004-10-24 22:27:31 +00002068 if (rulenum + 1 <= c->num_rules/2) {
2069 r = iptcc_get_rule_num(c, rulenum + 1);
2070 } else {
2071 r = iptcc_get_rule_num_reverse(c, c->num_rules - rulenum);
Marc Bouchere6869a82000-03-20 06:03:29 +00002072 }
2073
Harald Welteaae69be2004-08-29 23:32:14 +00002074 /* If we are about to delete the rule that is the current
2075 * iterator, move rule iterator back. next pointer will then
2076 * point to real next node */
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002077 if (r == handle->rule_iterator_cur) {
Jesper Dangaard Brouera9fe5b32009-03-23 14:26:56 +01002078 handle->rule_iterator_cur =
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002079 list_entry(handle->rule_iterator_cur->list.prev,
Harald Welteaae69be2004-08-29 23:32:14 +00002080 struct rule_head, list);
Harald Welte0113fe72004-01-06 19:04:02 +00002081 }
Marc Bouchere6869a82000-03-20 06:03:29 +00002082
Harald Welteaae69be2004-08-29 23:32:14 +00002083 c->num_rules--;
2084 iptcc_delete_rule(r);
2085
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002086 set_changed(handle);
Martin Josefsson2a5dbbb2004-09-22 21:37:41 +00002087
Harald Welteaae69be2004-08-29 23:32:14 +00002088 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00002089}
2090
Marc Bouchere6869a82000-03-20 06:03:29 +00002091/* Flushes the entries in the given chain (ie. empties chain). */
2092int
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002093TC_FLUSH_ENTRIES(const IPT_CHAINLABEL chain, struct xtc_handle *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00002094{
Harald Welteaae69be2004-08-29 23:32:14 +00002095 struct chain_head *c;
2096 struct rule_head *r, *tmp;
Marc Bouchere6869a82000-03-20 06:03:29 +00002097
Harald Welte0113fe72004-01-06 19:04:02 +00002098 iptc_fn = TC_FLUSH_ENTRIES;
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002099 if (!(c = iptcc_find_label(chain, handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002100 errno = ENOENT;
2101 return 0;
2102 }
Marc Bouchere6869a82000-03-20 06:03:29 +00002103
Harald Welteaae69be2004-08-29 23:32:14 +00002104 list_for_each_entry_safe(r, tmp, &c->rules, list) {
2105 iptcc_delete_rule(r);
2106 }
2107
2108 c->num_rules = 0;
2109
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002110 set_changed(handle);
Harald Welteaae69be2004-08-29 23:32:14 +00002111
2112 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00002113}
2114
2115/* Zeroes the counters in a chain. */
2116int
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002117TC_ZERO_ENTRIES(const IPT_CHAINLABEL chain, struct xtc_handle *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00002118{
Harald Welteaae69be2004-08-29 23:32:14 +00002119 struct chain_head *c;
2120 struct rule_head *r;
Rusty Russell7e53bf92000-03-20 07:03:28 +00002121
Derrik Pates664c0a32005-02-01 13:28:14 +00002122 iptc_fn = TC_ZERO_ENTRIES;
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002123 if (!(c = iptcc_find_label(chain, handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002124 errno = ENOENT;
2125 return 0;
2126 }
Marc Bouchere6869a82000-03-20 06:03:29 +00002127
Andy Gaye5bd1d72006-08-22 02:56:41 +00002128 if (c->counter_map.maptype == COUNTER_MAP_NORMAL_MAP)
2129 c->counter_map.maptype = COUNTER_MAP_ZEROED;
2130
Harald Welteaae69be2004-08-29 23:32:14 +00002131 list_for_each_entry(r, &c->rules, list) {
2132 if (r->counter_map.maptype == COUNTER_MAP_NORMAL_MAP)
2133 r->counter_map.maptype = COUNTER_MAP_ZEROED;
Marc Bouchere6869a82000-03-20 06:03:29 +00002134 }
Harald Welteaae69be2004-08-29 23:32:14 +00002135
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002136 set_changed(handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00002137
Marc Bouchere6869a82000-03-20 06:03:29 +00002138 return 1;
2139}
2140
Harald Welte1cef74d2001-01-05 15:22:59 +00002141STRUCT_COUNTERS *
2142TC_READ_COUNTER(const IPT_CHAINLABEL chain,
2143 unsigned int rulenum,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002144 struct xtc_handle *handle)
Harald Welte1cef74d2001-01-05 15:22:59 +00002145{
Harald Welteaae69be2004-08-29 23:32:14 +00002146 struct chain_head *c;
2147 struct rule_head *r;
Harald Welte1cef74d2001-01-05 15:22:59 +00002148
2149 iptc_fn = TC_READ_COUNTER;
2150 CHECK(*handle);
2151
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002152 if (!(c = iptcc_find_label(chain, handle))) {
Harald Welte1cef74d2001-01-05 15:22:59 +00002153 errno = ENOENT;
2154 return NULL;
2155 }
2156
Harald Welteaae69be2004-08-29 23:32:14 +00002157 if (!(r = iptcc_get_rule_num(c, rulenum))) {
Harald Welte0113fe72004-01-06 19:04:02 +00002158 errno = E2BIG;
2159 return NULL;
2160 }
2161
Harald Welteaae69be2004-08-29 23:32:14 +00002162 return &r->entry[0].counters;
Harald Welte1cef74d2001-01-05 15:22:59 +00002163}
2164
2165int
2166TC_ZERO_COUNTER(const IPT_CHAINLABEL chain,
2167 unsigned int rulenum,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002168 struct xtc_handle *handle)
Harald Welte1cef74d2001-01-05 15:22:59 +00002169{
Harald Welteaae69be2004-08-29 23:32:14 +00002170 struct chain_head *c;
2171 struct rule_head *r;
Jesper Dangaard Brouera9fe5b32009-03-23 14:26:56 +01002172
Harald Welte1cef74d2001-01-05 15:22:59 +00002173 iptc_fn = TC_ZERO_COUNTER;
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002174 CHECK(handle);
Harald Welte1cef74d2001-01-05 15:22:59 +00002175
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002176 if (!(c = iptcc_find_label(chain, handle))) {
Harald Welte1cef74d2001-01-05 15:22:59 +00002177 errno = ENOENT;
2178 return 0;
2179 }
2180
Harald Welteaae69be2004-08-29 23:32:14 +00002181 if (!(r = iptcc_get_rule_num(c, rulenum))) {
Harald Welte0113fe72004-01-06 19:04:02 +00002182 errno = E2BIG;
2183 return 0;
2184 }
2185
Harald Welteaae69be2004-08-29 23:32:14 +00002186 if (r->counter_map.maptype == COUNTER_MAP_NORMAL_MAP)
2187 r->counter_map.maptype = COUNTER_MAP_ZEROED;
Harald Welte1cef74d2001-01-05 15:22:59 +00002188
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002189 set_changed(handle);
Harald Welte1cef74d2001-01-05 15:22:59 +00002190
2191 return 1;
2192}
2193
Jesper Dangaard Brouera9fe5b32009-03-23 14:26:56 +01002194int
Harald Welte1cef74d2001-01-05 15:22:59 +00002195TC_SET_COUNTER(const IPT_CHAINLABEL chain,
2196 unsigned int rulenum,
2197 STRUCT_COUNTERS *counters,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002198 struct xtc_handle *handle)
Harald Welte1cef74d2001-01-05 15:22:59 +00002199{
Harald Welteaae69be2004-08-29 23:32:14 +00002200 struct chain_head *c;
2201 struct rule_head *r;
Harald Welte1cef74d2001-01-05 15:22:59 +00002202 STRUCT_ENTRY *e;
Harald Welte1cef74d2001-01-05 15:22:59 +00002203
2204 iptc_fn = TC_SET_COUNTER;
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002205 CHECK(handle);
Harald Welte1cef74d2001-01-05 15:22:59 +00002206
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002207 if (!(c = iptcc_find_label(chain, handle))) {
Harald Welte1cef74d2001-01-05 15:22:59 +00002208 errno = ENOENT;
2209 return 0;
2210 }
Harald Welte0113fe72004-01-06 19:04:02 +00002211
Harald Welteaae69be2004-08-29 23:32:14 +00002212 if (!(r = iptcc_get_rule_num(c, rulenum))) {
Harald Welte0113fe72004-01-06 19:04:02 +00002213 errno = E2BIG;
2214 return 0;
2215 }
2216
Harald Welteaae69be2004-08-29 23:32:14 +00002217 e = r->entry;
2218 r->counter_map.maptype = COUNTER_MAP_SET;
Harald Welte0113fe72004-01-06 19:04:02 +00002219
2220 memcpy(&e->counters, counters, sizeof(STRUCT_COUNTERS));
Harald Welte1cef74d2001-01-05 15:22:59 +00002221
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002222 set_changed(handle);
Harald Welte1cef74d2001-01-05 15:22:59 +00002223
2224 return 1;
2225}
2226
Marc Bouchere6869a82000-03-20 06:03:29 +00002227/* Creates a new chain. */
2228/* To create a chain, create two rules: error node and unconditional
2229 * return. */
2230int
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002231TC_CREATE_CHAIN(const IPT_CHAINLABEL chain, struct xtc_handle *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00002232{
Harald Welteaae69be2004-08-29 23:32:14 +00002233 static struct chain_head *c;
Patrick McHardy1f23d3c2008-06-07 15:04:34 +02002234 int capacity;
2235 int exceeded;
Marc Bouchere6869a82000-03-20 06:03:29 +00002236
Rusty Russell79dee072000-05-02 16:45:16 +00002237 iptc_fn = TC_CREATE_CHAIN;
Marc Bouchere6869a82000-03-20 06:03:29 +00002238
2239 /* find_label doesn't cover built-in targets: DROP, ACCEPT,
2240 QUEUE, RETURN. */
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002241 if (iptcc_find_label(chain, handle)
Rusty Russell79dee072000-05-02 16:45:16 +00002242 || strcmp(chain, LABEL_DROP) == 0
2243 || strcmp(chain, LABEL_ACCEPT) == 0
Rusty Russell67088e72000-05-10 01:18:57 +00002244 || strcmp(chain, LABEL_QUEUE) == 0
Rusty Russell79dee072000-05-02 16:45:16 +00002245 || strcmp(chain, LABEL_RETURN) == 0) {
Harald Welteaae69be2004-08-29 23:32:14 +00002246 DEBUGP("Chain `%s' already exists\n", chain);
Marc Bouchere6869a82000-03-20 06:03:29 +00002247 errno = EEXIST;
2248 return 0;
2249 }
2250
Rusty Russell79dee072000-05-02 16:45:16 +00002251 if (strlen(chain)+1 > sizeof(IPT_CHAINLABEL)) {
Harald Welteaae69be2004-08-29 23:32:14 +00002252 DEBUGP("Chain name `%s' too long\n", chain);
Marc Bouchere6869a82000-03-20 06:03:29 +00002253 errno = EINVAL;
2254 return 0;
2255 }
2256
Harald Welteaae69be2004-08-29 23:32:14 +00002257 c = iptcc_alloc_chain_head(chain, 0);
2258 if (!c) {
2259 DEBUGP("Cannot allocate memory for chain `%s'\n", chain);
2260 errno = ENOMEM;
2261 return 0;
Marc Bouchere6869a82000-03-20 06:03:29 +00002262
Harald Welteaae69be2004-08-29 23:32:14 +00002263 }
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002264 handle->num_chains++; /* New user defined chain */
Marc Bouchere6869a82000-03-20 06:03:29 +00002265
Harald Welteaae69be2004-08-29 23:32:14 +00002266 DEBUGP("Creating chain `%s'\n", chain);
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002267 iptc_insert_chain(handle, c); /* Insert sorted */
Harald Welte0113fe72004-01-06 19:04:02 +00002268
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +00002269 /* Inserting chains don't change the correctness of the chain
2270 * index (except if its smaller than index[0], but that
2271 * handled by iptc_insert_chain). It only causes longer lists
2272 * in the buckets. Thus, only rebuild chain index when the
2273 * capacity is exceed with CHAIN_INDEX_INSERT_MAX chains.
2274 */
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002275 capacity = handle->chain_index_sz * CHAIN_INDEX_BUCKET_LEN;
2276 exceeded = handle->num_chains - capacity;
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +00002277 if (exceeded > CHAIN_INDEX_INSERT_MAX) {
2278 debug("Capacity(%d) exceeded(%d) rebuild (chains:%d)\n",
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002279 capacity, exceeded, handle->num_chains);
2280 iptcc_chain_index_rebuild(handle);
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +00002281 }
2282
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002283 set_changed(handle);
Harald Welte0113fe72004-01-06 19:04:02 +00002284
Harald Welteaae69be2004-08-29 23:32:14 +00002285 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00002286}
2287
2288/* Get the number of references to this chain. */
2289int
Rusty Russell79dee072000-05-02 16:45:16 +00002290TC_GET_REFERENCES(unsigned int *ref, const IPT_CHAINLABEL chain,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002291 struct xtc_handle *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00002292{
Harald Welteaae69be2004-08-29 23:32:14 +00002293 struct chain_head *c;
Marc Bouchere6869a82000-03-20 06:03:29 +00002294
Derrik Pates664c0a32005-02-01 13:28:14 +00002295 iptc_fn = TC_GET_REFERENCES;
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002296 if (!(c = iptcc_find_label(chain, handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002297 errno = ENOENT;
2298 return 0;
2299 }
2300
Harald Welteaae69be2004-08-29 23:32:14 +00002301 *ref = c->references;
2302
Marc Bouchere6869a82000-03-20 06:03:29 +00002303 return 1;
2304}
2305
2306/* Deletes a chain. */
2307int
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002308TC_DELETE_CHAIN(const IPT_CHAINLABEL chain, struct xtc_handle *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00002309{
Marc Bouchere6869a82000-03-20 06:03:29 +00002310 unsigned int references;
Harald Welteaae69be2004-08-29 23:32:14 +00002311 struct chain_head *c;
Rusty Russell7e53bf92000-03-20 07:03:28 +00002312
Rusty Russell79dee072000-05-02 16:45:16 +00002313 iptc_fn = TC_DELETE_CHAIN;
Marc Bouchere6869a82000-03-20 06:03:29 +00002314
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002315 if (!(c = iptcc_find_label(chain, handle))) {
Harald Welteaae69be2004-08-29 23:32:14 +00002316 DEBUGP("cannot find chain `%s'\n", chain);
Marc Bouchere6869a82000-03-20 06:03:29 +00002317 errno = ENOENT;
2318 return 0;
2319 }
2320
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002321 if (TC_BUILTIN(chain, handle)) {
Harald Welteaae69be2004-08-29 23:32:14 +00002322 DEBUGP("cannot remove builtin chain `%s'\n", chain);
2323 errno = EINVAL;
2324 return 0;
2325 }
2326
2327 if (!TC_GET_REFERENCES(&references, chain, handle)) {
2328 DEBUGP("cannot get references on chain `%s'\n", chain);
2329 return 0;
2330 }
2331
2332 if (references > 0) {
2333 DEBUGP("chain `%s' still has references\n", chain);
2334 errno = EMLINK;
2335 return 0;
2336 }
2337
2338 if (c->num_rules) {
2339 DEBUGP("chain `%s' is not empty\n", chain);
Marc Bouchere6869a82000-03-20 06:03:29 +00002340 errno = ENOTEMPTY;
2341 return 0;
2342 }
2343
Harald Welteaae69be2004-08-29 23:32:14 +00002344 /* If we are about to delete the chain that is the current
Jesper Dangaard Brouer48bde402008-01-15 17:06:48 +00002345 * iterator, move chain iterator forward. */
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002346 if (c == handle->chain_iterator_cur)
2347 iptcc_chain_iterator_advance(handle);
Harald Welte0113fe72004-01-06 19:04:02 +00002348
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002349 handle->num_chains--; /* One user defined chain deleted */
Jesper Dangaard Brouer48bde402008-01-15 17:06:48 +00002350
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +00002351 //list_del(&c->list); /* Done in iptcc_chain_index_delete_chain() */
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002352 iptcc_chain_index_delete_chain(c, handle);
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +00002353 free(c);
2354
Harald Welteaae69be2004-08-29 23:32:14 +00002355 DEBUGP("chain `%s' deleted\n", chain);
2356
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002357 set_changed(handle);
Harald Welteaae69be2004-08-29 23:32:14 +00002358
2359 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00002360}
2361
2362/* Renames a chain. */
Rusty Russell79dee072000-05-02 16:45:16 +00002363int TC_RENAME_CHAIN(const IPT_CHAINLABEL oldname,
2364 const IPT_CHAINLABEL newname,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002365 struct xtc_handle *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00002366{
Harald Welteaae69be2004-08-29 23:32:14 +00002367 struct chain_head *c;
Rusty Russell79dee072000-05-02 16:45:16 +00002368 iptc_fn = TC_RENAME_CHAIN;
Marc Bouchere6869a82000-03-20 06:03:29 +00002369
Harald Welte1de80462000-10-30 12:00:27 +00002370 /* find_label doesn't cover built-in targets: DROP, ACCEPT,
2371 QUEUE, RETURN. */
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002372 if (iptcc_find_label(newname, handle)
Rusty Russell79dee072000-05-02 16:45:16 +00002373 || strcmp(newname, LABEL_DROP) == 0
2374 || strcmp(newname, LABEL_ACCEPT) == 0
Harald Welte1de80462000-10-30 12:00:27 +00002375 || strcmp(newname, LABEL_QUEUE) == 0
Rusty Russell79dee072000-05-02 16:45:16 +00002376 || strcmp(newname, LABEL_RETURN) == 0) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002377 errno = EEXIST;
2378 return 0;
2379 }
2380
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002381 if (!(c = iptcc_find_label(oldname, handle))
2382 || TC_BUILTIN(oldname, handle)) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002383 errno = ENOENT;
2384 return 0;
2385 }
2386
Rusty Russell79dee072000-05-02 16:45:16 +00002387 if (strlen(newname)+1 > sizeof(IPT_CHAINLABEL)) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002388 errno = EINVAL;
2389 return 0;
2390 }
2391
Jesper Dangaard Brouer64ff47c2009-03-23 14:25:49 +01002392 /* This only unlinks "c" from the list, thus no free(c) */
2393 iptcc_chain_index_delete_chain(c, handle);
2394
2395 /* Change the name of the chain */
Harald Welteaae69be2004-08-29 23:32:14 +00002396 strncpy(c->name, newname, sizeof(IPT_CHAINLABEL));
Jesper Dangaard Brouer64ff47c2009-03-23 14:25:49 +01002397
2398 /* Insert sorted into to list again */
2399 iptc_insert_chain(handle, c);
2400
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002401 set_changed(handle);
Harald Welte0113fe72004-01-06 19:04:02 +00002402
Marc Bouchere6869a82000-03-20 06:03:29 +00002403 return 1;
2404}
2405
2406/* Sets the policy on a built-in chain. */
2407int
Rusty Russell79dee072000-05-02 16:45:16 +00002408TC_SET_POLICY(const IPT_CHAINLABEL chain,
2409 const IPT_CHAINLABEL policy,
Harald Welte1cef74d2001-01-05 15:22:59 +00002410 STRUCT_COUNTERS *counters,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002411 struct xtc_handle *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00002412{
Harald Welteaae69be2004-08-29 23:32:14 +00002413 struct chain_head *c;
Marc Bouchere6869a82000-03-20 06:03:29 +00002414
Rusty Russell79dee072000-05-02 16:45:16 +00002415 iptc_fn = TC_SET_POLICY;
Marc Bouchere6869a82000-03-20 06:03:29 +00002416
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002417 if (!(c = iptcc_find_label(chain, handle))) {
Harald Welteaae69be2004-08-29 23:32:14 +00002418 DEBUGP("cannot find chain `%s'\n", chain);
2419 errno = ENOENT;
Marc Bouchere6869a82000-03-20 06:03:29 +00002420 return 0;
2421 }
2422
Harald Welteaae69be2004-08-29 23:32:14 +00002423 if (!iptcc_is_builtin(c)) {
2424 DEBUGP("cannot set policy of userdefinedchain `%s'\n", chain);
2425 errno = ENOENT;
2426 return 0;
2427 }
Marc Bouchere6869a82000-03-20 06:03:29 +00002428
Rusty Russell79dee072000-05-02 16:45:16 +00002429 if (strcmp(policy, LABEL_ACCEPT) == 0)
Harald Welteaae69be2004-08-29 23:32:14 +00002430 c->verdict = -NF_ACCEPT - 1;
Rusty Russell79dee072000-05-02 16:45:16 +00002431 else if (strcmp(policy, LABEL_DROP) == 0)
Harald Welteaae69be2004-08-29 23:32:14 +00002432 c->verdict = -NF_DROP - 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00002433 else {
2434 errno = EINVAL;
2435 return 0;
2436 }
Harald Welte1cef74d2001-01-05 15:22:59 +00002437
Harald Welte1cef74d2001-01-05 15:22:59 +00002438 if (counters) {
2439 /* set byte and packet counters */
Harald Welteaae69be2004-08-29 23:32:14 +00002440 memcpy(&c->counters, counters, sizeof(STRUCT_COUNTERS));
2441 c->counter_map.maptype = COUNTER_MAP_SET;
Harald Welte1cef74d2001-01-05 15:22:59 +00002442 } else {
Harald Welteaae69be2004-08-29 23:32:14 +00002443 c->counter_map.maptype = COUNTER_MAP_NOMAP;
Harald Welte1cef74d2001-01-05 15:22:59 +00002444 }
2445
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002446 set_changed(handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00002447
Marc Bouchere6869a82000-03-20 06:03:29 +00002448 return 1;
2449}
2450
2451/* Without this, on gcc 2.7.2.3, we get:
Rusty Russell79dee072000-05-02 16:45:16 +00002452 libiptc.c: In function `TC_COMMIT':
Marc Bouchere6869a82000-03-20 06:03:29 +00002453 libiptc.c:833: fixed or forbidden register was spilled.
2454 This may be due to a compiler bug or to impossible asm
2455 statements or clauses.
2456*/
2457static void
Rusty Russell79dee072000-05-02 16:45:16 +00002458subtract_counters(STRUCT_COUNTERS *answer,
2459 const STRUCT_COUNTERS *a,
2460 const STRUCT_COUNTERS *b)
Marc Bouchere6869a82000-03-20 06:03:29 +00002461{
2462 answer->pcnt = a->pcnt - b->pcnt;
2463 answer->bcnt = a->bcnt - b->bcnt;
2464}
2465
Harald Welteaae69be2004-08-29 23:32:14 +00002466
Jan Engelhardtdbb77542008-02-11 00:33:30 +01002467static void counters_nomap(STRUCT_COUNTERS_INFO *newcounters, unsigned int idx)
Harald Welteaae69be2004-08-29 23:32:14 +00002468{
Jan Engelhardtdbb77542008-02-11 00:33:30 +01002469 newcounters->counters[idx] = ((STRUCT_COUNTERS) { 0, 0});
Harald Welteaae69be2004-08-29 23:32:14 +00002470 DEBUGP_C("NOMAP => zero\n");
2471}
2472
2473static void counters_normal_map(STRUCT_COUNTERS_INFO *newcounters,
Jan Engelhardtdbb77542008-02-11 00:33:30 +01002474 STRUCT_REPLACE *repl, unsigned int idx,
Harald Welteaae69be2004-08-29 23:32:14 +00002475 unsigned int mappos)
2476{
2477 /* Original read: X.
2478 * Atomic read on replacement: X + Y.
2479 * Currently in kernel: Z.
2480 * Want in kernel: X + Y + Z.
2481 * => Add in X + Y
2482 * => Add in replacement read.
2483 */
Jan Engelhardtdbb77542008-02-11 00:33:30 +01002484 newcounters->counters[idx] = repl->counters[mappos];
Harald Welteaae69be2004-08-29 23:32:14 +00002485 DEBUGP_C("NORMAL_MAP => mappos %u \n", mappos);
2486}
2487
2488static void counters_map_zeroed(STRUCT_COUNTERS_INFO *newcounters,
Jan Engelhardtdbb77542008-02-11 00:33:30 +01002489 STRUCT_REPLACE *repl, unsigned int idx,
2490 unsigned int mappos, STRUCT_COUNTERS *counters)
Harald Welteaae69be2004-08-29 23:32:14 +00002491{
2492 /* Original read: X.
2493 * Atomic read on replacement: X + Y.
2494 * Currently in kernel: Z.
2495 * Want in kernel: Y + Z.
2496 * => Add in Y.
2497 * => Add in (replacement read - original read).
2498 */
Jan Engelhardtdbb77542008-02-11 00:33:30 +01002499 subtract_counters(&newcounters->counters[idx],
Harald Welteaae69be2004-08-29 23:32:14 +00002500 &repl->counters[mappos],
2501 counters);
2502 DEBUGP_C("ZEROED => mappos %u\n", mappos);
2503}
2504
2505static void counters_map_set(STRUCT_COUNTERS_INFO *newcounters,
Jan Engelhardtdbb77542008-02-11 00:33:30 +01002506 unsigned int idx, STRUCT_COUNTERS *counters)
Harald Welteaae69be2004-08-29 23:32:14 +00002507{
2508 /* Want to set counter (iptables-restore) */
2509
Jan Engelhardtdbb77542008-02-11 00:33:30 +01002510 memcpy(&newcounters->counters[idx], counters,
Harald Welteaae69be2004-08-29 23:32:14 +00002511 sizeof(STRUCT_COUNTERS));
2512
2513 DEBUGP_C("SET\n");
2514}
2515
2516
Marc Bouchere6869a82000-03-20 06:03:29 +00002517int
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002518TC_COMMIT(struct xtc_handle *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00002519{
2520 /* Replace, then map back the counters. */
Rusty Russell79dee072000-05-02 16:45:16 +00002521 STRUCT_REPLACE *repl;
2522 STRUCT_COUNTERS_INFO *newcounters;
Harald Welteaae69be2004-08-29 23:32:14 +00002523 struct chain_head *c;
2524 int ret;
Martin Josefsson841e4ae2003-05-02 15:30:11 +00002525 size_t counterlen;
Harald Welteaae69be2004-08-29 23:32:14 +00002526 int new_number;
2527 unsigned int new_size;
Marc Bouchere6869a82000-03-20 06:03:29 +00002528
Derrik Pates664c0a32005-02-01 13:28:14 +00002529 iptc_fn = TC_COMMIT;
Marc Bouchere6869a82000-03-20 06:03:29 +00002530 CHECK(*handle);
Martin Josefsson841e4ae2003-05-02 15:30:11 +00002531
Marc Bouchere6869a82000-03-20 06:03:29 +00002532 /* Don't commit if nothing changed. */
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002533 if (!handle->changed)
Marc Bouchere6869a82000-03-20 06:03:29 +00002534 goto finished;
2535
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002536 new_number = iptcc_compile_table_prep(handle, &new_size);
Harald Welteaae69be2004-08-29 23:32:14 +00002537 if (new_number < 0) {
2538 errno = ENOMEM;
Harald Welted6ba6f52005-11-12 10:39:40 +00002539 goto out_zero;
Harald Welteaae69be2004-08-29 23:32:14 +00002540 }
2541
2542 repl = malloc(sizeof(*repl) + new_size);
Marc Bouchere6869a82000-03-20 06:03:29 +00002543 if (!repl) {
2544 errno = ENOMEM;
Harald Welted6ba6f52005-11-12 10:39:40 +00002545 goto out_zero;
Marc Bouchere6869a82000-03-20 06:03:29 +00002546 }
Martin Josefssonad3b4f92004-09-22 22:04:07 +00002547 memset(repl, 0, sizeof(*repl) + new_size);
Harald Welteaae69be2004-08-29 23:32:14 +00002548
Rusty Russelle45c7132004-12-16 13:21:44 +00002549#if 0
2550 TC_DUMP_ENTRIES(*handle);
2551#endif
2552
Harald Welteaae69be2004-08-29 23:32:14 +00002553 counterlen = sizeof(STRUCT_COUNTERS_INFO)
2554 + sizeof(STRUCT_COUNTERS) * new_number;
Marc Bouchere6869a82000-03-20 06:03:29 +00002555
2556 /* These are the old counters we will get from kernel */
Rusty Russell79dee072000-05-02 16:45:16 +00002557 repl->counters = malloc(sizeof(STRUCT_COUNTERS)
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002558 * handle->info.num_entries);
Marc Bouchere6869a82000-03-20 06:03:29 +00002559 if (!repl->counters) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002560 errno = ENOMEM;
Harald Welted6ba6f52005-11-12 10:39:40 +00002561 goto out_free_repl;
Marc Bouchere6869a82000-03-20 06:03:29 +00002562 }
Marc Bouchere6869a82000-03-20 06:03:29 +00002563 /* These are the counters we're going to put back, later. */
2564 newcounters = malloc(counterlen);
2565 if (!newcounters) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002566 errno = ENOMEM;
Harald Welted6ba6f52005-11-12 10:39:40 +00002567 goto out_free_repl_counters;
Marc Bouchere6869a82000-03-20 06:03:29 +00002568 }
Harald Welteaae69be2004-08-29 23:32:14 +00002569 memset(newcounters, 0, counterlen);
Marc Bouchere6869a82000-03-20 06:03:29 +00002570
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002571 strcpy(repl->name, handle->info.name);
Harald Welteaae69be2004-08-29 23:32:14 +00002572 repl->num_entries = new_number;
2573 repl->size = new_size;
2574
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002575 repl->num_counters = handle->info.num_entries;
2576 repl->valid_hooks = handle->info.valid_hooks;
Harald Welteaae69be2004-08-29 23:32:14 +00002577
2578 DEBUGP("num_entries=%u, size=%u, num_counters=%u\n",
2579 repl->num_entries, repl->size, repl->num_counters);
2580
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002581 ret = iptcc_compile_table(handle, repl);
Harald Welteaae69be2004-08-29 23:32:14 +00002582 if (ret < 0) {
2583 errno = ret;
Harald Welted6ba6f52005-11-12 10:39:40 +00002584 goto out_free_newcounters;
Harald Welteaae69be2004-08-29 23:32:14 +00002585 }
2586
2587
2588#ifdef IPTC_DEBUG2
2589 {
Jesper Dangaard Brouera9fe5b32009-03-23 14:26:56 +01002590 int fd = open("/tmp/libiptc-so_set_replace.blob",
Harald Welteaae69be2004-08-29 23:32:14 +00002591 O_CREAT|O_WRONLY);
2592 if (fd >= 0) {
2593 write(fd, repl, sizeof(*repl) + repl->size);
2594 close(fd);
2595 }
2596 }
2597#endif
Marc Bouchere6869a82000-03-20 06:03:29 +00002598
Jan Engelhardt175f4512008-11-10 17:25:55 +01002599 ret = setsockopt(handle->sockfd, TC_IPPROTO, SO_SET_REPLACE, repl,
Harald Welted6ba6f52005-11-12 10:39:40 +00002600 sizeof(*repl) + repl->size);
Patrick McHardye0865ad2006-04-22 02:08:56 +00002601 if (ret < 0)
Harald Welted6ba6f52005-11-12 10:39:40 +00002602 goto out_free_newcounters;
Marc Bouchere6869a82000-03-20 06:03:29 +00002603
2604 /* Put counters back. */
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002605 strcpy(newcounters->name, handle->info.name);
Harald Welteaae69be2004-08-29 23:32:14 +00002606 newcounters->num_counters = new_number;
Marc Bouchere6869a82000-03-20 06:03:29 +00002607
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002608 list_for_each_entry(c, &handle->chains, list) {
Harald Welteaae69be2004-08-29 23:32:14 +00002609 struct rule_head *r;
Marc Bouchere6869a82000-03-20 06:03:29 +00002610
Harald Welteaae69be2004-08-29 23:32:14 +00002611 /* Builtin chains have their own counters */
2612 if (iptcc_is_builtin(c)) {
2613 DEBUGP("counter for chain-index %u: ", c->foot_index);
2614 switch(c->counter_map.maptype) {
2615 case COUNTER_MAP_NOMAP:
2616 counters_nomap(newcounters, c->foot_index);
2617 break;
2618 case COUNTER_MAP_NORMAL_MAP:
2619 counters_normal_map(newcounters, repl,
Jesper Dangaard Brouera9fe5b32009-03-23 14:26:56 +01002620 c->foot_index,
Harald Welteaae69be2004-08-29 23:32:14 +00002621 c->counter_map.mappos);
2622 break;
2623 case COUNTER_MAP_ZEROED:
2624 counters_map_zeroed(newcounters, repl,
Jesper Dangaard Brouera9fe5b32009-03-23 14:26:56 +01002625 c->foot_index,
Harald Welteaae69be2004-08-29 23:32:14 +00002626 c->counter_map.mappos,
2627 &c->counters);
2628 break;
2629 case COUNTER_MAP_SET:
2630 counters_map_set(newcounters, c->foot_index,
2631 &c->counters);
2632 break;
2633 }
2634 }
Harald Welte1cef74d2001-01-05 15:22:59 +00002635
Harald Welteaae69be2004-08-29 23:32:14 +00002636 list_for_each_entry(r, &c->rules, list) {
2637 DEBUGP("counter for index %u: ", r->index);
2638 switch (r->counter_map.maptype) {
2639 case COUNTER_MAP_NOMAP:
2640 counters_nomap(newcounters, r->index);
2641 break;
Harald Welte1cef74d2001-01-05 15:22:59 +00002642
Harald Welteaae69be2004-08-29 23:32:14 +00002643 case COUNTER_MAP_NORMAL_MAP:
2644 counters_normal_map(newcounters, repl,
Jesper Dangaard Brouera9fe5b32009-03-23 14:26:56 +01002645 r->index,
Harald Welteaae69be2004-08-29 23:32:14 +00002646 r->counter_map.mappos);
2647 break;
Harald Welte1cef74d2001-01-05 15:22:59 +00002648
Harald Welteaae69be2004-08-29 23:32:14 +00002649 case COUNTER_MAP_ZEROED:
2650 counters_map_zeroed(newcounters, repl,
2651 r->index,
2652 r->counter_map.mappos,
2653 &r->entry->counters);
2654 break;
2655
2656 case COUNTER_MAP_SET:
2657 counters_map_set(newcounters, r->index,
2658 &r->entry->counters);
2659 break;
2660 }
Marc Bouchere6869a82000-03-20 06:03:29 +00002661 }
2662 }
Rusty Russell62527ce2000-09-04 09:45:54 +00002663
Harald Welteaae69be2004-08-29 23:32:14 +00002664#ifdef IPTC_DEBUG2
2665 {
Jesper Dangaard Brouera9fe5b32009-03-23 14:26:56 +01002666 int fd = open("/tmp/libiptc-so_set_add_counters.blob",
Harald Welteaae69be2004-08-29 23:32:14 +00002667 O_CREAT|O_WRONLY);
2668 if (fd >= 0) {
2669 write(fd, newcounters, counterlen);
2670 close(fd);
2671 }
2672 }
2673#endif
2674
Jan Engelhardt175f4512008-11-10 17:25:55 +01002675 ret = setsockopt(handle->sockfd, TC_IPPROTO, SO_SET_ADD_COUNTERS,
Harald Welted6ba6f52005-11-12 10:39:40 +00002676 newcounters, counterlen);
Patrick McHardye0865ad2006-04-22 02:08:56 +00002677 if (ret < 0)
Harald Welted6ba6f52005-11-12 10:39:40 +00002678 goto out_free_newcounters;
Marc Bouchere6869a82000-03-20 06:03:29 +00002679
2680 free(repl->counters);
2681 free(repl);
2682 free(newcounters);
2683
Harald Welted6ba6f52005-11-12 10:39:40 +00002684finished:
Marc Bouchere6869a82000-03-20 06:03:29 +00002685 return 1;
Harald Welted6ba6f52005-11-12 10:39:40 +00002686
2687out_free_newcounters:
2688 free(newcounters);
2689out_free_repl_counters:
2690 free(repl->counters);
2691out_free_repl:
2692 free(repl);
2693out_zero:
2694 return 0;
Marc Bouchere6869a82000-03-20 06:03:29 +00002695}
2696
Marc Bouchere6869a82000-03-20 06:03:29 +00002697/* Translates errno numbers into more human-readable form than strerror. */
2698const char *
Rusty Russell79dee072000-05-02 16:45:16 +00002699TC_STRERROR(int err)
Marc Bouchere6869a82000-03-20 06:03:29 +00002700{
2701 unsigned int i;
2702 struct table_struct {
2703 void *fn;
2704 int err;
2705 const char *message;
2706 } table [] =
Harald Welte4ccfa632001-07-30 15:12:43 +00002707 { { TC_INIT, EPERM, "Permission denied (you must be root)" },
Rusty Russell79dee072000-05-02 16:45:16 +00002708 { TC_INIT, EINVAL, "Module is wrong version" },
Jesper Dangaard Brouera9fe5b32009-03-23 14:26:56 +01002709 { TC_INIT, ENOENT,
Harald Welte4ccfa632001-07-30 15:12:43 +00002710 "Table does not exist (do you need to insmod?)" },
Rusty Russell79dee072000-05-02 16:45:16 +00002711 { TC_DELETE_CHAIN, ENOTEMPTY, "Chain is not empty" },
2712 { TC_DELETE_CHAIN, EINVAL, "Can't delete built-in chain" },
2713 { TC_DELETE_CHAIN, EMLINK,
Marc Bouchere6869a82000-03-20 06:03:29 +00002714 "Can't delete chain with references left" },
Rusty Russell79dee072000-05-02 16:45:16 +00002715 { TC_CREATE_CHAIN, EEXIST, "Chain already exists" },
2716 { TC_INSERT_ENTRY, E2BIG, "Index of insertion too big" },
2717 { TC_REPLACE_ENTRY, E2BIG, "Index of replacement too big" },
2718 { TC_DELETE_NUM_ENTRY, E2BIG, "Index of deletion too big" },
Harald Welte1cef74d2001-01-05 15:22:59 +00002719 { TC_READ_COUNTER, E2BIG, "Index of counter too big" },
2720 { TC_ZERO_COUNTER, E2BIG, "Index of counter too big" },
Rusty Russell79dee072000-05-02 16:45:16 +00002721 { TC_INSERT_ENTRY, ELOOP, "Loop found in table" },
2722 { TC_INSERT_ENTRY, EINVAL, "Target problem" },
Marc Bouchere6869a82000-03-20 06:03:29 +00002723 /* ENOENT for DELETE probably means no matching rule */
Rusty Russell79dee072000-05-02 16:45:16 +00002724 { TC_DELETE_ENTRY, ENOENT,
Marc Boucherc8264992000-04-22 22:34:44 +00002725 "Bad rule (does a matching rule exist in that chain?)" },
Rusty Russell79dee072000-05-02 16:45:16 +00002726 { TC_SET_POLICY, ENOENT,
Marc Boucherc8264992000-04-22 22:34:44 +00002727 "Bad built-in chain name" },
Rusty Russell79dee072000-05-02 16:45:16 +00002728 { TC_SET_POLICY, EINVAL,
Marc Boucherc8264992000-04-22 22:34:44 +00002729 "Bad policy name" },
Harald Welte4ccfa632001-07-30 15:12:43 +00002730
2731 { NULL, 0, "Incompatible with this kernel" },
2732 { NULL, ENOPROTOOPT, "iptables who? (do you need to insmod?)" },
2733 { NULL, ENOSYS, "Will be implemented real soon. I promise ;)" },
2734 { NULL, ENOMEM, "Memory allocation problem" },
2735 { NULL, ENOENT, "No chain/target/match by that name" },
Marc Bouchere6869a82000-03-20 06:03:29 +00002736 };
2737
2738 for (i = 0; i < sizeof(table)/sizeof(struct table_struct); i++) {
2739 if ((!table[i].fn || table[i].fn == iptc_fn)
2740 && table[i].err == err)
2741 return table[i].message;
2742 }
2743
2744 return strerror(err);
2745}