blob: de8dc60ede89de41d3c2322621b9f4b4de0a6128 [file] [log] [blame]
Martin Josefsson0f9b8b12004-12-18 17:18:49 +00001/* Library which manipulates firewall rules. Version $Revision$ */
Marc Bouchere6869a82000-03-20 06:03:29 +00002
3/* Architecture of firewall rules is as follows:
4 *
5 * Chains go INPUT, FORWARD, OUTPUT then user chains.
6 * Each user chain starts with an ERROR node.
7 * Every chain ends with an unconditional jump: a RETURN for user chains,
8 * and a POLICY for built-ins.
9 */
10
Harald Welte3ea8f402003-06-23 18:25:59 +000011/* (C) 1999 Paul ``Rusty'' Russell - Placed under the GNU GPL (See
12 * COPYING for details).
Harald Welteaae69be2004-08-29 23:32:14 +000013 * (C) 2000-2004 by the Netfilter Core Team <coreteam@netfilter.org>
Harald Welte3ea8f402003-06-23 18:25:59 +000014 *
Harald Weltefbc85232003-06-24 17:37:21 +000015 * 2003-Jun-20: Harald Welte <laforge@netfilter.org>:
Harald Welte3ea8f402003-06-23 18:25:59 +000016 * - Reimplementation of chain cache to use offsets instead of entries
Harald Weltefbc85232003-06-24 17:37:21 +000017 * 2003-Jun-23: Harald Welte <laforge@netfilter.org>:
Harald Welte0113fe72004-01-06 19:04:02 +000018 * - performance optimization, sponsored by Astaro AG (http://www.astaro.com/)
Harald Weltefbc85232003-06-24 17:37:21 +000019 * don't rebuild the chain cache after every operation, instead fix it
20 * up after a ruleset change.
Harald Welteaae69be2004-08-29 23:32:14 +000021 * 2004-Aug-18: Harald Welte <laforge@netfilter.org>:
22 * - futher performance work: total reimplementation of libiptc.
23 * - libiptc now has a real internal (linked-list) represntation of the
24 * ruleset and a parser/compiler from/to this internal representation
25 * - again sponsored by Astaro AG (http://www.astaro.com/)
Harald Welte3ea8f402003-06-23 18:25:59 +000026 */
Harald Welte15920d12004-05-16 09:05:07 +000027#include <sys/types.h>
28#include <sys/socket.h>
Stephane Ouellette7cd00282004-05-14 08:21:06 +000029
Harald Welteaae69be2004-08-29 23:32:14 +000030#include "linux_list.h"
31
32//#define IPTC_DEBUG2 1
33
34#ifdef IPTC_DEBUG2
35#include <fcntl.h>
36#define DEBUGP(x, args...) fprintf(stderr, "%s: " x, __FUNCTION__, ## args)
37#define DEBUGP_C(x, args...) fprintf(stderr, x, ## args)
38#else
39#define DEBUGP(x, args...)
40#define DEBUGP_C(x, args...)
41#endif
42
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +000043#ifdef DEBUG
44#define debug(x, args...) fprintf(stderr, x, ## args)
45#else
46#define debug(x, args...)
47#endif
48
Marc Bouchere6869a82000-03-20 06:03:29 +000049#ifndef IPT_LIB_DIR
50#define IPT_LIB_DIR "/usr/local/lib/iptables"
51#endif
52
53static int sockfd = -1;
Derrik Pates664c0a32005-02-01 13:28:14 +000054static int sockfd_use = 0;
Marc Bouchere6869a82000-03-20 06:03:29 +000055static void *iptc_fn = NULL;
56
Patrick McHardy0b639362007-09-08 16:00:01 +000057static const char *hooknames[] = {
58 [HOOK_PRE_ROUTING] = "PREROUTING",
59 [HOOK_LOCAL_IN] = "INPUT",
60 [HOOK_FORWARD] = "FORWARD",
61 [HOOK_LOCAL_OUT] = "OUTPUT",
62 [HOOK_POST_ROUTING] = "POSTROUTING",
Rusty Russell10758b72000-09-14 07:37:33 +000063#ifdef HOOK_DROPPING
Patrick McHardy0b639362007-09-08 16:00:01 +000064 [HOOK_DROPPING] = "DROPPING"
Rusty Russell10758b72000-09-14 07:37:33 +000065#endif
Marc Bouchere6869a82000-03-20 06:03:29 +000066};
67
Harald Welteaae69be2004-08-29 23:32:14 +000068/* Convenience structures */
69struct ipt_error_target
70{
71 STRUCT_ENTRY_TARGET t;
72 char error[TABLE_MAXNAMELEN];
73};
74
75struct chain_head;
76struct rule_head;
77
Marc Bouchere6869a82000-03-20 06:03:29 +000078struct counter_map
79{
80 enum {
81 COUNTER_MAP_NOMAP,
82 COUNTER_MAP_NORMAL_MAP,
Harald Welte1cef74d2001-01-05 15:22:59 +000083 COUNTER_MAP_ZEROED,
84 COUNTER_MAP_SET
Marc Bouchere6869a82000-03-20 06:03:29 +000085 } maptype;
86 unsigned int mappos;
87};
88
Harald Welteaae69be2004-08-29 23:32:14 +000089enum iptcc_rule_type {
90 IPTCC_R_STANDARD, /* standard target (ACCEPT, ...) */
91 IPTCC_R_MODULE, /* extension module (SNAT, ...) */
92 IPTCC_R_FALLTHROUGH, /* fallthrough rule */
93 IPTCC_R_JUMP, /* jump to other chain */
Marc Bouchere6869a82000-03-20 06:03:29 +000094};
95
Harald Welteaae69be2004-08-29 23:32:14 +000096struct rule_head
Rusty Russell30fd6e52000-04-23 09:16:06 +000097{
Harald Welteaae69be2004-08-29 23:32:14 +000098 struct list_head list;
99 struct chain_head *chain;
100 struct counter_map counter_map;
101
102 unsigned int index; /* index (needed for counter_map) */
103 unsigned int offset; /* offset in rule blob */
104
105 enum iptcc_rule_type type;
106 struct chain_head *jump; /* jump target, if IPTCC_R_JUMP */
107
108 unsigned int size; /* size of entry data */
109 STRUCT_ENTRY entry[0];
110};
111
112struct chain_head
113{
114 struct list_head list;
Rusty Russell79dee072000-05-02 16:45:16 +0000115 char name[TABLE_MAXNAMELEN];
Harald Welteaae69be2004-08-29 23:32:14 +0000116 unsigned int hooknum; /* hook number+1 if builtin */
117 unsigned int references; /* how many jumps reference us */
118 int verdict; /* verdict if builtin */
119
120 STRUCT_COUNTERS counters; /* per-chain counters */
121 struct counter_map counter_map;
122
123 unsigned int num_rules; /* number of rules in list */
124 struct list_head rules; /* list of rules */
125
126 unsigned int index; /* index (needed for jump resolval) */
127 unsigned int head_offset; /* offset in rule blob */
128 unsigned int foot_index; /* index (needed for counter_map) */
129 unsigned int foot_offset; /* offset in rule blob */
Rusty Russell30fd6e52000-04-23 09:16:06 +0000130};
131
Rusty Russell79dee072000-05-02 16:45:16 +0000132STRUCT_TC_HANDLE
Marc Bouchere6869a82000-03-20 06:03:29 +0000133{
Harald Welteaae69be2004-08-29 23:32:14 +0000134 int changed; /* Have changes been made? */
135
136 struct list_head chains;
137
138 struct chain_head *chain_iterator_cur;
139 struct rule_head *rule_iterator_cur;
140
Jesper Dangaard Brouer48bde402008-01-15 17:06:48 +0000141 unsigned int num_chains; /* number of user defined chains */
142
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000143 struct chain_head **chain_index; /* array for fast chain list access*/
144 unsigned int chain_index_sz;/* size of chain index array */
145
Rusty Russell79dee072000-05-02 16:45:16 +0000146 STRUCT_GETINFO info;
Harald Welteaae69be2004-08-29 23:32:14 +0000147 STRUCT_GET_ENTRIES *entries;
Marc Bouchere6869a82000-03-20 06:03:29 +0000148};
149
Harald Welteaae69be2004-08-29 23:32:14 +0000150/* allocate a new chain head for the cache */
151static struct chain_head *iptcc_alloc_chain_head(const char *name, int hooknum)
152{
153 struct chain_head *c = malloc(sizeof(*c));
154 if (!c)
155 return NULL;
156 memset(c, 0, sizeof(*c));
157
158 strncpy(c->name, name, TABLE_MAXNAMELEN);
159 c->hooknum = hooknum;
160 INIT_LIST_HEAD(&c->rules);
161
162 return c;
163}
164
165/* allocate and initialize a new rule for the cache */
166static struct rule_head *iptcc_alloc_rule(struct chain_head *c, unsigned int size)
167{
168 struct rule_head *r = malloc(sizeof(*r)+size);
169 if (!r)
170 return NULL;
171 memset(r, 0, sizeof(*r));
172
173 r->chain = c;
174 r->size = size;
175
176 return r;
177}
178
179/* notify us that the ruleset has been modified by the user */
Jesper Dangaard Brouer91093982008-01-15 17:01:58 +0000180static inline void
Rusty Russell79dee072000-05-02 16:45:16 +0000181set_changed(TC_HANDLE_T h)
Rusty Russell175f6412000-03-24 09:32:20 +0000182{
Rusty Russell175f6412000-03-24 09:32:20 +0000183 h->changed = 1;
184}
185
Harald Welte380ba5f2002-02-13 16:19:55 +0000186#ifdef IPTC_DEBUG
Rusty Russell79dee072000-05-02 16:45:16 +0000187static void do_check(TC_HANDLE_T h, unsigned int line);
Rusty Russell849779c2000-04-23 15:51:51 +0000188#define CHECK(h) do { if (!getenv("IPTC_NO_CHECK")) do_check((h), __LINE__); } while(0)
Rusty Russell30fd6e52000-04-23 09:16:06 +0000189#else
190#define CHECK(h)
191#endif
Marc Bouchere6869a82000-03-20 06:03:29 +0000192
Harald Welteaae69be2004-08-29 23:32:14 +0000193
194/**********************************************************************
195 * iptc blob utility functions (iptcb_*)
196 **********************************************************************/
197
Marc Bouchere6869a82000-03-20 06:03:29 +0000198static inline int
Harald Welteaae69be2004-08-29 23:32:14 +0000199iptcb_get_number(const STRUCT_ENTRY *i,
Rusty Russell79dee072000-05-02 16:45:16 +0000200 const STRUCT_ENTRY *seek,
Marc Bouchere6869a82000-03-20 06:03:29 +0000201 unsigned int *pos)
202{
203 if (i == seek)
204 return 1;
205 (*pos)++;
206 return 0;
207}
208
Marc Bouchere6869a82000-03-20 06:03:29 +0000209static inline int
Harald Welteaae69be2004-08-29 23:32:14 +0000210iptcb_get_entry_n(STRUCT_ENTRY *i,
Marc Bouchere6869a82000-03-20 06:03:29 +0000211 unsigned int number,
212 unsigned int *pos,
Rusty Russell79dee072000-05-02 16:45:16 +0000213 STRUCT_ENTRY **pe)
Marc Bouchere6869a82000-03-20 06:03:29 +0000214{
215 if (*pos == number) {
216 *pe = i;
217 return 1;
218 }
219 (*pos)++;
220 return 0;
221}
222
Harald Welteaae69be2004-08-29 23:32:14 +0000223static inline STRUCT_ENTRY *
224iptcb_get_entry(TC_HANDLE_T h, unsigned int offset)
225{
226 return (STRUCT_ENTRY *)((char *)h->entries->entrytable + offset);
227}
228
229static unsigned int
230iptcb_entry2index(const TC_HANDLE_T h, const STRUCT_ENTRY *seek)
Marc Bouchere6869a82000-03-20 06:03:29 +0000231{
232 unsigned int pos = 0;
Marc Bouchere6869a82000-03-20 06:03:29 +0000233
Harald Welteaae69be2004-08-29 23:32:14 +0000234 if (ENTRY_ITERATE(h->entries->entrytable, h->entries->size,
235 iptcb_get_number, seek, &pos) == 0) {
236 fprintf(stderr, "ERROR: offset %u not an entry!\n",
237 (unsigned int)((char *)seek - (char *)h->entries->entrytable));
238 abort();
239 }
240 return pos;
Marc Bouchere6869a82000-03-20 06:03:29 +0000241}
242
Harald Welte0113fe72004-01-06 19:04:02 +0000243static inline STRUCT_ENTRY *
Harald Welteaae69be2004-08-29 23:32:14 +0000244iptcb_offset2entry(TC_HANDLE_T h, unsigned int offset)
Harald Welte0113fe72004-01-06 19:04:02 +0000245{
Harald Welteaae69be2004-08-29 23:32:14 +0000246 return (STRUCT_ENTRY *) ((void *)h->entries->entrytable+offset);
Harald Welte0113fe72004-01-06 19:04:02 +0000247}
248
Harald Welteaae69be2004-08-29 23:32:14 +0000249
Harald Welte0113fe72004-01-06 19:04:02 +0000250static inline unsigned long
Harald Welteaae69be2004-08-29 23:32:14 +0000251iptcb_entry2offset(const TC_HANDLE_T h, const STRUCT_ENTRY *e)
Harald Welte0113fe72004-01-06 19:04:02 +0000252{
Harald Welteaae69be2004-08-29 23:32:14 +0000253 return (void *)e - (void *)h->entries->entrytable;
Harald Welte3ea8f402003-06-23 18:25:59 +0000254}
255
256static inline unsigned int
Harald Welteaae69be2004-08-29 23:32:14 +0000257iptcb_offset2index(const TC_HANDLE_T h, unsigned int offset)
Harald Welte3ea8f402003-06-23 18:25:59 +0000258{
Harald Welteaae69be2004-08-29 23:32:14 +0000259 return iptcb_entry2index(h, iptcb_offset2entry(h, offset));
260}
261
262/* Returns 0 if not hook entry, else hooknumber + 1 */
263static inline unsigned int
264iptcb_ent_is_hook_entry(STRUCT_ENTRY *e, TC_HANDLE_T h)
265{
266 unsigned int i;
267
268 for (i = 0; i < NUMHOOKS; i++) {
269 if ((h->info.valid_hooks & (1 << i))
270 && iptcb_get_entry(h, h->info.hook_entry[i]) == e)
271 return i+1;
272 }
273 return 0;
Harald Welte3ea8f402003-06-23 18:25:59 +0000274}
275
276
Harald Welteaae69be2004-08-29 23:32:14 +0000277/**********************************************************************
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000278 * Chain index (cache utility) functions
279 **********************************************************************
280 * The chain index is an array with pointers into the chain list, with
281 * CHAIN_INDEX_BUCKET_LEN spacing. This facilitates the ability to
282 * speedup chain list searching, by find a more optimal starting
283 * points when searching the linked list.
284 *
285 * The starting point can be found fast by using a binary search of
286 * the chain index. Thus, reducing the previous search complexity of
287 * O(n) to O(log(n/k) + k) where k is CHAIN_INDEX_BUCKET_LEN.
288 *
289 * A nice property of the chain index, is that the "bucket" list
290 * length is max CHAIN_INDEX_BUCKET_LEN (when just build, inserts will
291 * change this). Oppose to hashing, where the "bucket" list length can
292 * vary a lot.
293 */
294#ifndef CHAIN_INDEX_BUCKET_LEN
295#define CHAIN_INDEX_BUCKET_LEN 40
296#endif
297
298/* Another nice property of the chain index is that inserting/creating
299 * chains in chain list don't change the correctness of the chain
300 * index, it only causes longer lists in the buckets.
301 *
302 * To mitigate the performance penalty of longer bucket lists and the
303 * penalty of rebuilding, the chain index is rebuild only when
304 * CHAIN_INDEX_INSERT_MAX chains has been added.
305 */
306#ifndef CHAIN_INDEX_INSERT_MAX
307#define CHAIN_INDEX_INSERT_MAX 355
308#endif
309
310static inline unsigned int iptcc_is_builtin(struct chain_head *c);
311
312
313/* Use binary search in the chain index array, to find a chain_head
314 * pointer closest to the place of the searched name element.
315 *
316 * Notes that, binary search (obviously) requires that the chain list
317 * is sorted by name.
318 */
319static struct list_head *
Jan Engelhardtdbb77542008-02-11 00:33:30 +0100320iptcc_bsearch_chain_index(const char *name, unsigned int *idx, TC_HANDLE_T handle)
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000321{
322 unsigned int pos, end;
323 int res;
324
325 struct list_head *list_pos;
326 list_pos=&handle->chains;
327
328 /* Check for empty array, e.g. no user defined chains */
329 if (handle->chain_index_sz == 0) {
330 debug("WARNING: handle->chain_index_sz == 0\n");
331 return list_pos;
332 }
333
334 /* Init */
335 end = handle->chain_index_sz;
336 pos = end / 2;
337
338 debug("bsearch Find chain:%s (pos:%d end:%d)\n", name, pos, end);
339
340 /* Loop */
341 loop:
342 if (!handle->chain_index[pos]) {
343 fprintf(stderr, "ERROR: NULL pointer chain_index[%d]\n", pos);
344 return &handle->chains; /* Be safe, return orig start pos */
345 }
346
347 res = strcmp(name, handle->chain_index[pos]->name);
348 list_pos = &handle->chain_index[pos]->list;
Jan Engelhardtdbb77542008-02-11 00:33:30 +0100349 *idx = pos;
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000350
351 debug("bsearch Index[%d] name:%s res:%d ",
352 pos, handle->chain_index[pos]->name, res);
353
354 if (res == 0) { /* Found element, by direct hit */
355 debug("[found] Direct hit pos:%d end:%d\n", pos, end);
356 return list_pos;
357 } else if (res < 0) { /* Too far, jump back */
358 end = pos;
359 pos = pos / 2;
360
361 /* Exit case: First element of array */
362 if (end == 0) {
363 debug("[found] Reached first array elem (end%d)\n",end);
364 return list_pos;
365 }
366 debug("jump back to pos:%d (end:%d)\n", pos, end);
367 goto loop;
368 } else if (res > 0 ){ /* Not far enough, jump forward */
369
370 /* Exit case: Last element of array */
371 if (pos == handle->chain_index_sz-1) {
372 debug("[found] Last array elem (end:%d)\n", end);
373 return list_pos;
374 }
375
376 /* Exit case: Next index less, thus elem in this list section */
377 res = strcmp(name, handle->chain_index[pos+1]->name);
378 if (res < 0) {
379 debug("[found] closest list (end:%d)\n", end);
380 return list_pos;
381 }
382
383 pos = (pos+end)/2;
384 debug("jump forward to pos:%d (end:%d)\n", pos, end);
385 goto loop;
386 }
387
388 return list_pos;
389}
390
391#ifdef DEBUG
392/* Trivial linear search of chain index. Function used for verifying
393 the output of bsearch function */
394static struct list_head *
395iptcc_linearly_search_chain_index(const char *name, TC_HANDLE_T handle)
396{
397 unsigned int i=0;
398 int res=0;
399
400 struct list_head *list_pos;
401 list_pos = &handle->chains;
402
403 if (handle->chain_index_sz)
404 list_pos = &handle->chain_index[0]->list;
405
406 /* Linearly walk of chain index array */
407
408 for (i=0; i < handle->chain_index_sz; i++) {
409 if (handle->chain_index[i]) {
410 res = strcmp(handle->chain_index[i]->name, name);
411 if (res > 0)
412 break; // One step too far
413 list_pos = &handle->chain_index[i]->list;
414 if (res == 0)
415 break; // Direct hit
416 }
417 }
418
419 return list_pos;
420}
421#endif
422
423static int iptcc_chain_index_alloc(TC_HANDLE_T h)
424{
425 unsigned int list_length = CHAIN_INDEX_BUCKET_LEN;
426 unsigned int array_elems;
427 unsigned int array_mem;
428
429 /* Allocate memory for the chain index array */
430 array_elems = (h->num_chains / list_length) +
431 (h->num_chains % list_length ? 1 : 0);
432 array_mem = sizeof(h->chain_index) * array_elems;
433
434 debug("Alloc Chain index, elems:%d mem:%d bytes\n",
435 array_elems, array_mem);
436
437 h->chain_index = malloc(array_mem);
438 if (!h->chain_index) {
439 h->chain_index_sz = 0;
440 return -ENOMEM;
441 }
442 memset(h->chain_index, 0, array_mem);
443 h->chain_index_sz = array_elems;
444
445 return 1;
446}
447
448static void iptcc_chain_index_free(TC_HANDLE_T h)
449{
450 h->chain_index_sz = 0;
451 free(h->chain_index);
452}
453
454
455#ifdef DEBUG
456static void iptcc_chain_index_dump(TC_HANDLE_T h)
457{
458 unsigned int i = 0;
459
460 /* Dump: contents of chain index array */
461 for (i=0; i < h->chain_index_sz; i++) {
462 if (h->chain_index[i]) {
463 fprintf(stderr, "Chain index[%d].name: %s\n",
464 i, h->chain_index[i]->name);
465 }
466 }
467}
468#endif
469
470/* Build the chain index */
471static int iptcc_chain_index_build(TC_HANDLE_T h)
472{
473 unsigned int list_length = CHAIN_INDEX_BUCKET_LEN;
474 unsigned int chains = 0;
475 unsigned int cindex = 0;
476 struct chain_head *c;
477
478 /* Build up the chain index array here */
479 debug("Building chain index\n");
480
481 debug("Number of user defined chains:%d bucket_sz:%d array_sz:%d\n",
482 h->num_chains, list_length, h->chain_index_sz);
483
484 if (h->chain_index_sz == 0)
485 return 0;
486
487 list_for_each_entry(c, &h->chains, list) {
488
489 /* Issue: The index array needs to start after the
490 * builtin chains, as they are not sorted */
491 if (!iptcc_is_builtin(c)) {
492 cindex=chains / list_length;
493
494 /* Safe guard, break out on array limit, this
495 * is useful if chains are added and array is
496 * rebuild, without realloc of memory. */
497 if (cindex >= h->chain_index_sz)
498 break;
499
500 if ((chains % list_length)== 0) {
501 debug("\nIndex[%d] Chains:", cindex);
502 h->chain_index[cindex] = c;
503 }
504 chains++;
505 }
506 debug("%s, ", c->name);
507 }
508 debug("\n");
509
510 return 1;
511}
512
513static int iptcc_chain_index_rebuild(TC_HANDLE_T h)
514{
515 debug("REBUILD chain index array\n");
516 iptcc_chain_index_free(h);
517 if ((iptcc_chain_index_alloc(h)) < 0)
518 return -ENOMEM;
519 iptcc_chain_index_build(h);
520 return 1;
521}
522
523/* Delete chain (pointer) from index array. Removing an element from
524 * the chain list only affects the chain index array, if the chain
525 * index points-to/uses that list pointer.
526 *
527 * There are different strategies, the simple and safe is to rebuild
528 * the chain index every time. The more advanced is to update the
529 * array index to point to the next element, but that requires some
530 * house keeping and boundry checks. The advanced is implemented, as
531 * the simple approach behaves badly when all chains are deleted
532 * because list_for_each processing will always hit the first chain
533 * index, thus causing a rebuild for every chain.
534 */
535static int iptcc_chain_index_delete_chain(struct chain_head *c, TC_HANDLE_T h)
536{
537 struct list_head *index_ptr, *index_ptr2, *next;
538 struct chain_head *c2;
Jan Engelhardtdbb77542008-02-11 00:33:30 +0100539 unsigned int idx, idx2;
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000540
Jan Engelhardtdbb77542008-02-11 00:33:30 +0100541 index_ptr = iptcc_bsearch_chain_index(c->name, &idx, h);
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000542
543 debug("Del chain[%s] c->list:%p index_ptr:%p\n",
544 c->name, &c->list, index_ptr);
545
546 /* Save the next pointer */
547 next = c->list.next;
548 list_del(&c->list);
549
550 if (index_ptr == &c->list) { /* Chain used as index ptr */
551
552 /* See if its possible to avoid a rebuild, by shifting
553 * to next pointer. Its possible if the next pointer
554 * is located in the same index bucket.
555 */
556 c2 = list_entry(next, struct chain_head, list);
Jan Engelhardtdbb77542008-02-11 00:33:30 +0100557 index_ptr2 = iptcc_bsearch_chain_index(c2->name, &idx2, h);
558 if (idx != idx2) {
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000559 /* Rebuild needed */
560 return iptcc_chain_index_rebuild(h);
561 } else {
562 /* Avoiding rebuild */
563 debug("Update cindex[%d] with next ptr name:[%s]\n",
Jan Engelhardtdbb77542008-02-11 00:33:30 +0100564 idx, c2->name);
565 h->chain_index[idx]=c2;
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000566 return 0;
567 }
568 }
569 return 0;
570}
571
572
573/**********************************************************************
Harald Welteaae69be2004-08-29 23:32:14 +0000574 * iptc cache utility functions (iptcc_*)
575 **********************************************************************/
Harald Welte0113fe72004-01-06 19:04:02 +0000576
Harald Welteaae69be2004-08-29 23:32:14 +0000577/* Is the given chain builtin (1) or user-defined (0) */
Jesper Dangaard Brouer91093982008-01-15 17:01:58 +0000578static inline unsigned int iptcc_is_builtin(struct chain_head *c)
Harald Welteaae69be2004-08-29 23:32:14 +0000579{
580 return (c->hooknum ? 1 : 0);
581}
582
583/* Get a specific rule within a chain */
584static struct rule_head *iptcc_get_rule_num(struct chain_head *c,
585 unsigned int rulenum)
586{
587 struct rule_head *r;
588 unsigned int num = 0;
589
590 list_for_each_entry(r, &c->rules, list) {
591 num++;
592 if (num == rulenum)
593 return r;
594 }
595 return NULL;
596}
597
Martin Josefssona5616dc2004-10-24 22:27:31 +0000598/* Get a specific rule within a chain backwards */
599static struct rule_head *iptcc_get_rule_num_reverse(struct chain_head *c,
600 unsigned int rulenum)
601{
602 struct rule_head *r;
603 unsigned int num = 0;
604
605 list_for_each_entry_reverse(r, &c->rules, list) {
606 num++;
607 if (num == rulenum)
608 return r;
609 }
610 return NULL;
611}
612
Harald Welteaae69be2004-08-29 23:32:14 +0000613/* Returns chain head if found, otherwise NULL. */
614static struct chain_head *
615iptcc_find_chain_by_offset(TC_HANDLE_T handle, unsigned int offset)
616{
617 struct list_head *pos;
618
619 if (list_empty(&handle->chains))
620 return NULL;
621
622 list_for_each(pos, &handle->chains) {
623 struct chain_head *c = list_entry(pos, struct chain_head, list);
624 if (offset >= c->head_offset && offset <= c->foot_offset)
625 return c;
Harald Welte0113fe72004-01-06 19:04:02 +0000626 }
627
Harald Welteaae69be2004-08-29 23:32:14 +0000628 return NULL;
Harald Welte0113fe72004-01-06 19:04:02 +0000629}
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000630
Harald Welteaae69be2004-08-29 23:32:14 +0000631/* Returns chain head if found, otherwise NULL. */
632static struct chain_head *
633iptcc_find_label(const char *name, TC_HANDLE_T handle)
634{
635 struct list_head *pos;
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000636 struct list_head *list_start_pos;
637 unsigned int i=0;
638 int res;
Harald Welteaae69be2004-08-29 23:32:14 +0000639
640 if (list_empty(&handle->chains))
641 return NULL;
642
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000643 /* First look at builtin chains */
Harald Welteaae69be2004-08-29 23:32:14 +0000644 list_for_each(pos, &handle->chains) {
645 struct chain_head *c = list_entry(pos, struct chain_head, list);
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000646 if (!iptcc_is_builtin(c))
647 break;
Harald Welteaae69be2004-08-29 23:32:14 +0000648 if (!strcmp(c->name, name))
649 return c;
650 }
651
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000652 /* Find a smart place to start the search via chain index */
653 //list_start_pos = iptcc_linearly_search_chain_index(name, handle);
654 list_start_pos = iptcc_bsearch_chain_index(name, &i, handle);
655
656 /* Handel if bsearch bails out early */
657 if (list_start_pos == &handle->chains) {
658 list_start_pos = pos;
659 }
660#ifdef DEBUG
661 else {
662 /* Verify result of bsearch against linearly index search */
663 struct list_head *test_pos;
664 struct chain_head *test_c, *tmp_c;
665 test_pos = iptcc_linearly_search_chain_index(name, handle);
666 if (list_start_pos != test_pos) {
667 debug("BUG in chain_index search\n");
668 test_c=list_entry(test_pos, struct chain_head,list);
669 tmp_c =list_entry(list_start_pos,struct chain_head,list);
670 debug("Verify search found:\n");
671 debug(" Chain:%s\n", test_c->name);
672 debug("BSearch found:\n");
673 debug(" Chain:%s\n", tmp_c->name);
674 exit(42);
675 }
676 }
677#endif
678
679 /* Initial/special case, no user defined chains */
680 if (handle->num_chains == 0)
681 return NULL;
682
683 /* Start searching through the chain list */
684 list_for_each(pos, list_start_pos->prev) {
685 struct chain_head *c = list_entry(pos, struct chain_head, list);
686 res = strcmp(c->name, name);
687 debug("List search name:%s == %s res:%d\n", name, c->name, res);
688 if (res==0)
689 return c;
690
691 /* We can stop earlier as we know list is sorted */
692 if (res>0 && !iptcc_is_builtin(c)) { /* Walked too far*/
693 debug(" Not in list, walked too far, sorted list\n");
694 return NULL;
695 }
696
697 /* Stop on wrap around, if list head is reached */
698 if (pos == &handle->chains) {
699 debug("Stop, list head reached\n");
700 return NULL;
701 }
702 }
703
704 debug("List search NOT found name:%s\n", name);
Harald Welteaae69be2004-08-29 23:32:14 +0000705 return NULL;
706}
707
708/* called when rule is to be removed from cache */
709static void iptcc_delete_rule(struct rule_head *r)
710{
711 DEBUGP("deleting rule %p (offset %u)\n", r, r->offset);
712 /* clean up reference count of called chain */
713 if (r->type == IPTCC_R_JUMP
714 && r->jump)
715 r->jump->references--;
716
717 list_del(&r->list);
718 free(r);
719}
720
721
722/**********************************************************************
723 * RULESET PARSER (blob -> cache)
724 **********************************************************************/
725
Harald Welteaae69be2004-08-29 23:32:14 +0000726/* Delete policy rule of previous chain, since cache doesn't contain
727 * chain policy rules.
728 * WARNING: This function has ugly design and relies on a lot of context, only
729 * to be called from specific places within the parser */
730static int __iptcc_p_del_policy(TC_HANDLE_T h, unsigned int num)
731{
732 if (h->chain_iterator_cur) {
733 /* policy rule is last rule */
734 struct rule_head *pr = (struct rule_head *)
735 h->chain_iterator_cur->rules.prev;
736
737 /* save verdict */
738 h->chain_iterator_cur->verdict =
739 *(int *)GET_TARGET(pr->entry)->data;
740
741 /* save counter and counter_map information */
742 h->chain_iterator_cur->counter_map.maptype =
743 COUNTER_MAP_NORMAL_MAP;
744 h->chain_iterator_cur->counter_map.mappos = num-1;
745 memcpy(&h->chain_iterator_cur->counters, &pr->entry->counters,
746 sizeof(h->chain_iterator_cur->counters));
747
748 /* foot_offset points to verdict rule */
749 h->chain_iterator_cur->foot_index = num;
750 h->chain_iterator_cur->foot_offset = pr->offset;
751
752 /* delete rule from cache */
753 iptcc_delete_rule(pr);
Martin Josefsson8d1b38a2004-09-22 21:00:19 +0000754 h->chain_iterator_cur->num_rules--;
Harald Welteaae69be2004-08-29 23:32:14 +0000755
756 return 1;
757 }
758 return 0;
759}
760
Harald Welteec30b6c2005-02-01 16:45:56 +0000761/* alphabetically insert a chain into the list */
762static inline void iptc_insert_chain(TC_HANDLE_T h, struct chain_head *c)
763{
764 struct chain_head *tmp;
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000765 struct list_head *list_start_pos;
766 unsigned int i=1;
767
768 /* Find a smart place to start the insert search */
769 list_start_pos = iptcc_bsearch_chain_index(c->name, &i, h);
770
771 /* Handle the case, where chain.name is smaller than index[0] */
772 if (i==0 && strcmp(c->name, h->chain_index[0]->name) <= 0) {
773 h->chain_index[0] = c; /* Update chain index head */
774 list_start_pos = h->chains.next;
775 debug("Update chain_index[0] with %s\n", c->name);
776 }
777
778 /* Handel if bsearch bails out early */
779 if (list_start_pos == &h->chains) {
780 list_start_pos = h->chains.next;
781 }
Harald Welteec30b6c2005-02-01 16:45:56 +0000782
Olaf Rempel9d3ed772005-03-04 23:08:30 +0000783 /* sort only user defined chains */
784 if (!c->hooknum) {
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000785 list_for_each_entry(tmp, list_start_pos->prev, list) {
Robert de Barthfeca0572005-07-31 07:04:59 +0000786 if (!tmp->hooknum && strcmp(c->name, tmp->name) <= 0) {
Olaf Rempel9d3ed772005-03-04 23:08:30 +0000787 list_add(&c->list, tmp->list.prev);
788 return;
789 }
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000790
791 /* Stop if list head is reached */
792 if (&tmp->list == &h->chains) {
793 debug("Insert, list head reached add to tail\n");
794 break;
795 }
Harald Welteec30b6c2005-02-01 16:45:56 +0000796 }
797 }
798
799 /* survived till end of list: add at tail */
800 list_add_tail(&c->list, &h->chains);
801}
802
Harald Welteaae69be2004-08-29 23:32:14 +0000803/* Another ugly helper function split out of cache_add_entry to make it less
804 * spaghetti code */
805static void __iptcc_p_add_chain(TC_HANDLE_T h, struct chain_head *c,
806 unsigned int offset, unsigned int *num)
807{
Jesper Dangaard Brouer13364512007-12-12 15:20:42 +0000808 struct list_head *tail = h->chains.prev;
809 struct chain_head *ctail;
810
Harald Welteaae69be2004-08-29 23:32:14 +0000811 __iptcc_p_del_policy(h, *num);
812
813 c->head_offset = offset;
814 c->index = *num;
815
Jesper Dangaard Brouer13364512007-12-12 15:20:42 +0000816 /* Chains from kernel are already sorted, as they are inserted
817 * sorted. But there exists an issue when shifting to 1.4.0
818 * from an older version, as old versions allow last created
819 * chain to be unsorted.
820 */
821 if (iptcc_is_builtin(c)) /* Only user defined chains are sorted*/
822 list_add_tail(&c->list, &h->chains);
823 else {
824 ctail = list_entry(tail, struct chain_head, list);
825 if (strcmp(c->name, ctail->name) > 0)
826 list_add_tail(&c->list, &h->chains);/* Already sorted*/
827 else
828 iptc_insert_chain(h, c);/* Was not sorted */
829 }
Jesper Dangaard Brouerd8cb7872007-11-28 08:40:26 +0000830
Harald Welteaae69be2004-08-29 23:32:14 +0000831 h->chain_iterator_cur = c;
832}
833
834/* main parser function: add an entry from the blob to the cache */
835static int cache_add_entry(STRUCT_ENTRY *e,
836 TC_HANDLE_T h,
837 STRUCT_ENTRY **prev,
838 unsigned int *num)
839{
840 unsigned int builtin;
841 unsigned int offset = (char *)e - (char *)h->entries->entrytable;
842
843 DEBUGP("entering...");
844
845 /* Last entry ("policy rule"). End it.*/
846 if (iptcb_entry2offset(h,e) + e->next_offset == h->entries->size) {
847 /* This is the ERROR node at the end of the chain */
848 DEBUGP_C("%u:%u: end of table:\n", *num, offset);
849
850 __iptcc_p_del_policy(h, *num);
851
852 h->chain_iterator_cur = NULL;
853 goto out_inc;
854 }
855
856 /* We know this is the start of a new chain if it's an ERROR
857 * target, or a hook entry point */
858
859 if (strcmp(GET_TARGET(e)->u.user.name, ERROR_TARGET) == 0) {
860 struct chain_head *c =
861 iptcc_alloc_chain_head((const char *)GET_TARGET(e)->data, 0);
862 DEBUGP_C("%u:%u:new userdefined chain %s: %p\n", *num, offset,
863 (char *)c->name, c);
864 if (!c) {
865 errno = -ENOMEM;
866 return -1;
867 }
Jesper Dangaard Brouer48bde402008-01-15 17:06:48 +0000868 h->num_chains++; /* New user defined chain */
Harald Welteaae69be2004-08-29 23:32:14 +0000869
870 __iptcc_p_add_chain(h, c, offset, num);
871
872 } else if ((builtin = iptcb_ent_is_hook_entry(e, h)) != 0) {
873 struct chain_head *c =
874 iptcc_alloc_chain_head((char *)hooknames[builtin-1],
875 builtin);
876 DEBUGP_C("%u:%u new builtin chain: %p (rules=%p)\n",
877 *num, offset, c, &c->rules);
878 if (!c) {
879 errno = -ENOMEM;
880 return -1;
881 }
882
883 c->hooknum = builtin;
884
885 __iptcc_p_add_chain(h, c, offset, num);
886
887 /* FIXME: this is ugly. */
888 goto new_rule;
889 } else {
890 /* has to be normal rule */
891 struct rule_head *r;
892new_rule:
893
894 if (!(r = iptcc_alloc_rule(h->chain_iterator_cur,
895 e->next_offset))) {
896 errno = ENOMEM;
897 return -1;
898 }
899 DEBUGP_C("%u:%u normal rule: %p: ", *num, offset, r);
900
901 r->index = *num;
902 r->offset = offset;
903 memcpy(r->entry, e, e->next_offset);
904 r->counter_map.maptype = COUNTER_MAP_NORMAL_MAP;
905 r->counter_map.mappos = r->index;
906
907 /* handling of jumps, etc. */
908 if (!strcmp(GET_TARGET(e)->u.user.name, STANDARD_TARGET)) {
909 STRUCT_STANDARD_TARGET *t;
910
911 t = (STRUCT_STANDARD_TARGET *)GET_TARGET(e);
912 if (t->target.u.target_size
913 != ALIGN(sizeof(STRUCT_STANDARD_TARGET))) {
914 errno = EINVAL;
915 return -1;
916 }
917
918 if (t->verdict < 0) {
919 DEBUGP_C("standard, verdict=%d\n", t->verdict);
920 r->type = IPTCC_R_STANDARD;
921 } else if (t->verdict == r->offset+e->next_offset) {
922 DEBUGP_C("fallthrough\n");
923 r->type = IPTCC_R_FALLTHROUGH;
924 } else {
925 DEBUGP_C("jump, target=%u\n", t->verdict);
926 r->type = IPTCC_R_JUMP;
927 /* Jump target fixup has to be deferred
928 * until second pass, since we migh not
929 * yet have parsed the target */
930 }
Martin Josefsson52c38022004-09-22 19:39:40 +0000931 } else {
932 DEBUGP_C("module, target=%s\n", GET_TARGET(e)->u.user.name);
933 r->type = IPTCC_R_MODULE;
Harald Welteaae69be2004-08-29 23:32:14 +0000934 }
935
936 list_add_tail(&r->list, &h->chain_iterator_cur->rules);
Martin Josefsson8d1b38a2004-09-22 21:00:19 +0000937 h->chain_iterator_cur->num_rules++;
Harald Welteaae69be2004-08-29 23:32:14 +0000938 }
939out_inc:
940 (*num)++;
941 return 0;
942}
943
944
945/* parse an iptables blob into it's pieces */
946static int parse_table(TC_HANDLE_T h)
947{
948 STRUCT_ENTRY *prev;
949 unsigned int num = 0;
950 struct chain_head *c;
951
952 /* First pass: over ruleset blob */
953 ENTRY_ITERATE(h->entries->entrytable, h->entries->size,
954 cache_add_entry, h, &prev, &num);
955
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000956 /* Build the chain index, used for chain list search speedup */
957 if ((iptcc_chain_index_alloc(h)) < 0)
958 return -ENOMEM;
959 iptcc_chain_index_build(h);
960
Harald Welteaae69be2004-08-29 23:32:14 +0000961 /* Second pass: fixup parsed data from first pass */
962 list_for_each_entry(c, &h->chains, list) {
963 struct rule_head *r;
964 list_for_each_entry(r, &c->rules, list) {
Jan Engelhardtdbb77542008-02-11 00:33:30 +0100965 struct chain_head *lc;
Harald Welteaae69be2004-08-29 23:32:14 +0000966 STRUCT_STANDARD_TARGET *t;
967
968 if (r->type != IPTCC_R_JUMP)
969 continue;
970
971 t = (STRUCT_STANDARD_TARGET *)GET_TARGET(r->entry);
Jan Engelhardtdbb77542008-02-11 00:33:30 +0100972 lc = iptcc_find_chain_by_offset(h, t->verdict);
973 if (!lc)
Harald Welteaae69be2004-08-29 23:32:14 +0000974 return -1;
Jan Engelhardtdbb77542008-02-11 00:33:30 +0100975 r->jump = lc;
976 lc->references++;
Harald Welteaae69be2004-08-29 23:32:14 +0000977 }
978 }
979
980 /* FIXME: sort chains */
981
982 return 1;
983}
984
985
986/**********************************************************************
987 * RULESET COMPILATION (cache -> blob)
988 **********************************************************************/
989
990/* Convenience structures */
991struct iptcb_chain_start{
992 STRUCT_ENTRY e;
993 struct ipt_error_target name;
994};
995#define IPTCB_CHAIN_START_SIZE (sizeof(STRUCT_ENTRY) + \
996 ALIGN(sizeof(struct ipt_error_target)))
997
998struct iptcb_chain_foot {
999 STRUCT_ENTRY e;
1000 STRUCT_STANDARD_TARGET target;
1001};
1002#define IPTCB_CHAIN_FOOT_SIZE (sizeof(STRUCT_ENTRY) + \
1003 ALIGN(sizeof(STRUCT_STANDARD_TARGET)))
1004
1005struct iptcb_chain_error {
1006 STRUCT_ENTRY entry;
1007 struct ipt_error_target target;
1008};
1009#define IPTCB_CHAIN_ERROR_SIZE (sizeof(STRUCT_ENTRY) + \
1010 ALIGN(sizeof(struct ipt_error_target)))
1011
1012
1013
1014/* compile rule from cache into blob */
1015static inline int iptcc_compile_rule (TC_HANDLE_T h, STRUCT_REPLACE *repl, struct rule_head *r)
1016{
1017 /* handle jumps */
1018 if (r->type == IPTCC_R_JUMP) {
1019 STRUCT_STANDARD_TARGET *t;
1020 t = (STRUCT_STANDARD_TARGET *)GET_TARGET(r->entry);
1021 /* memset for memcmp convenience on delete/replace */
1022 memset(t->target.u.user.name, 0, FUNCTION_MAXNAMELEN);
1023 strcpy(t->target.u.user.name, STANDARD_TARGET);
1024 /* Jumps can only happen to builtin chains, so we
1025 * can safely assume that they always have a header */
1026 t->verdict = r->jump->head_offset + IPTCB_CHAIN_START_SIZE;
1027 } else if (r->type == IPTCC_R_FALLTHROUGH) {
1028 STRUCT_STANDARD_TARGET *t;
1029 t = (STRUCT_STANDARD_TARGET *)GET_TARGET(r->entry);
1030 t->verdict = r->offset + r->size;
1031 }
1032
1033 /* copy entry from cache to blob */
1034 memcpy((char *)repl->entries+r->offset, r->entry, r->size);
1035
1036 return 1;
1037}
1038
1039/* compile chain from cache into blob */
1040static int iptcc_compile_chain(TC_HANDLE_T h, STRUCT_REPLACE *repl, struct chain_head *c)
1041{
1042 int ret;
1043 struct rule_head *r;
1044 struct iptcb_chain_start *head;
1045 struct iptcb_chain_foot *foot;
1046
1047 /* only user-defined chains have heaer */
1048 if (!iptcc_is_builtin(c)) {
1049 /* put chain header in place */
1050 head = (void *)repl->entries + c->head_offset;
1051 head->e.target_offset = sizeof(STRUCT_ENTRY);
1052 head->e.next_offset = IPTCB_CHAIN_START_SIZE;
1053 strcpy(head->name.t.u.user.name, ERROR_TARGET);
1054 head->name.t.u.target_size =
1055 ALIGN(sizeof(struct ipt_error_target));
1056 strcpy(head->name.error, c->name);
1057 } else {
1058 repl->hook_entry[c->hooknum-1] = c->head_offset;
1059 repl->underflow[c->hooknum-1] = c->foot_offset;
1060 }
1061
1062 /* iterate over rules */
1063 list_for_each_entry(r, &c->rules, list) {
1064 ret = iptcc_compile_rule(h, repl, r);
1065 if (ret < 0)
1066 return ret;
1067 }
1068
1069 /* put chain footer in place */
1070 foot = (void *)repl->entries + c->foot_offset;
1071 foot->e.target_offset = sizeof(STRUCT_ENTRY);
1072 foot->e.next_offset = IPTCB_CHAIN_FOOT_SIZE;
1073 strcpy(foot->target.target.u.user.name, STANDARD_TARGET);
1074 foot->target.target.u.target_size =
1075 ALIGN(sizeof(STRUCT_STANDARD_TARGET));
1076 /* builtin targets have verdict, others return */
1077 if (iptcc_is_builtin(c))
1078 foot->target.verdict = c->verdict;
1079 else
1080 foot->target.verdict = RETURN;
1081 /* set policy-counters */
1082 memcpy(&foot->e.counters, &c->counters, sizeof(STRUCT_COUNTERS));
1083
1084 return 0;
1085}
1086
1087/* calculate offset and number for every rule in the cache */
1088static int iptcc_compile_chain_offsets(TC_HANDLE_T h, struct chain_head *c,
Harald Welteefa8fc22005-07-19 22:03:49 +00001089 unsigned int *offset, unsigned int *num)
Harald Welteaae69be2004-08-29 23:32:14 +00001090{
1091 struct rule_head *r;
1092
1093 c->head_offset = *offset;
1094 DEBUGP("%s: chain_head %u, offset=%u\n", c->name, *num, *offset);
1095
1096 if (!iptcc_is_builtin(c)) {
1097 /* Chain has header */
1098 *offset += sizeof(STRUCT_ENTRY)
1099 + ALIGN(sizeof(struct ipt_error_target));
1100 (*num)++;
1101 }
1102
1103 list_for_each_entry(r, &c->rules, list) {
1104 DEBUGP("rule %u, offset=%u, index=%u\n", *num, *offset, *num);
1105 r->offset = *offset;
1106 r->index = *num;
1107 *offset += r->size;
1108 (*num)++;
1109 }
1110
1111 DEBUGP("%s; chain_foot %u, offset=%u, index=%u\n", c->name, *num,
1112 *offset, *num);
1113 c->foot_offset = *offset;
1114 c->foot_index = *num;
1115 *offset += sizeof(STRUCT_ENTRY)
1116 + ALIGN(sizeof(STRUCT_STANDARD_TARGET));
1117 (*num)++;
1118
1119 return 1;
1120}
1121
1122/* put the pieces back together again */
1123static int iptcc_compile_table_prep(TC_HANDLE_T h, unsigned int *size)
1124{
1125 struct chain_head *c;
1126 unsigned int offset = 0, num = 0;
1127 int ret = 0;
1128
1129 /* First pass: calculate offset for every rule */
1130 list_for_each_entry(c, &h->chains, list) {
1131 ret = iptcc_compile_chain_offsets(h, c, &offset, &num);
1132 if (ret < 0)
1133 return ret;
1134 }
1135
1136 /* Append one error rule at end of chain */
1137 num++;
1138 offset += sizeof(STRUCT_ENTRY)
1139 + ALIGN(sizeof(struct ipt_error_target));
1140
1141 /* ruleset size is now in offset */
1142 *size = offset;
1143 return num;
1144}
1145
1146static int iptcc_compile_table(TC_HANDLE_T h, STRUCT_REPLACE *repl)
1147{
1148 struct chain_head *c;
1149 struct iptcb_chain_error *error;
1150
1151 /* Second pass: copy from cache to offsets, fill in jumps */
1152 list_for_each_entry(c, &h->chains, list) {
1153 int ret = iptcc_compile_chain(h, repl, c);
1154 if (ret < 0)
1155 return ret;
1156 }
1157
1158 /* Append error rule at end of chain */
1159 error = (void *)repl->entries + repl->size - IPTCB_CHAIN_ERROR_SIZE;
1160 error->entry.target_offset = sizeof(STRUCT_ENTRY);
1161 error->entry.next_offset = IPTCB_CHAIN_ERROR_SIZE;
1162 error->target.t.u.user.target_size =
1163 ALIGN(sizeof(struct ipt_error_target));
1164 strcpy((char *)&error->target.t.u.user.name, ERROR_TARGET);
1165 strcpy((char *)&error->target.error, "ERROR");
1166
1167 return 1;
1168}
1169
1170/**********************************************************************
1171 * EXTERNAL API (operates on cache only)
1172 **********************************************************************/
Marc Bouchere6869a82000-03-20 06:03:29 +00001173
1174/* Allocate handle of given size */
Rusty Russell79dee072000-05-02 16:45:16 +00001175static TC_HANDLE_T
Harald Welte0113fe72004-01-06 19:04:02 +00001176alloc_handle(const char *tablename, unsigned int size, unsigned int num_rules)
Marc Bouchere6869a82000-03-20 06:03:29 +00001177{
1178 size_t len;
Rusty Russell79dee072000-05-02 16:45:16 +00001179 TC_HANDLE_T h;
Marc Bouchere6869a82000-03-20 06:03:29 +00001180
Harald Welteaae69be2004-08-29 23:32:14 +00001181 len = sizeof(STRUCT_TC_HANDLE) + size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001182
Harald Welteaae69be2004-08-29 23:32:14 +00001183 h = malloc(sizeof(STRUCT_TC_HANDLE));
1184 if (!h) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001185 errno = ENOMEM;
1186 return NULL;
1187 }
Harald Welteaae69be2004-08-29 23:32:14 +00001188 memset(h, 0, sizeof(*h));
1189 INIT_LIST_HEAD(&h->chains);
Marc Bouchere6869a82000-03-20 06:03:29 +00001190 strcpy(h->info.name, tablename);
Harald Welteaae69be2004-08-29 23:32:14 +00001191
Harald Welte0371c0c2004-09-19 21:00:12 +00001192 h->entries = malloc(sizeof(STRUCT_GET_ENTRIES) + size);
Harald Welteaae69be2004-08-29 23:32:14 +00001193 if (!h->entries)
1194 goto out_free_handle;
1195
1196 strcpy(h->entries->name, tablename);
Harald Welte0371c0c2004-09-19 21:00:12 +00001197 h->entries->size = size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001198
1199 return h;
Harald Welteaae69be2004-08-29 23:32:14 +00001200
1201out_free_handle:
1202 free(h);
1203
1204 return NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +00001205}
1206
Harald Welteaae69be2004-08-29 23:32:14 +00001207
Rusty Russell79dee072000-05-02 16:45:16 +00001208TC_HANDLE_T
1209TC_INIT(const char *tablename)
Marc Bouchere6869a82000-03-20 06:03:29 +00001210{
Rusty Russell79dee072000-05-02 16:45:16 +00001211 TC_HANDLE_T h;
1212 STRUCT_GETINFO info;
Harald Welteefa8fc22005-07-19 22:03:49 +00001213 unsigned int tmp;
Marc Bouchere6869a82000-03-20 06:03:29 +00001214 socklen_t s;
1215
Rusty Russell79dee072000-05-02 16:45:16 +00001216 iptc_fn = TC_INIT;
Marc Bouchere6869a82000-03-20 06:03:29 +00001217
Martin Josefsson841e4ae2003-05-02 15:30:11 +00001218 if (strlen(tablename) >= TABLE_MAXNAMELEN) {
1219 errno = EINVAL;
1220 return NULL;
1221 }
1222
Derrik Pates664c0a32005-02-01 13:28:14 +00001223 if (sockfd_use == 0) {
1224 sockfd = socket(TC_AF, SOCK_RAW, IPPROTO_RAW);
1225 if (sockfd < 0)
1226 return NULL;
1227 }
1228 sockfd_use++;
Patrick McHardy2f932052008-04-02 14:01:53 +02001229retry:
Marc Bouchere6869a82000-03-20 06:03:29 +00001230 s = sizeof(info);
Martin Josefsson841e4ae2003-05-02 15:30:11 +00001231
Marc Bouchere6869a82000-03-20 06:03:29 +00001232 strcpy(info.name, tablename);
Derrik Pates664c0a32005-02-01 13:28:14 +00001233 if (getsockopt(sockfd, TC_IPPROTO, SO_GET_INFO, &info, &s) < 0) {
1234 if (--sockfd_use == 0) {
1235 close(sockfd);
1236 sockfd = -1;
1237 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001238 return NULL;
Derrik Pates664c0a32005-02-01 13:28:14 +00001239 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001240
Harald Welteaae69be2004-08-29 23:32:14 +00001241 DEBUGP("valid_hooks=0x%08x, num_entries=%u, size=%u\n",
1242 info.valid_hooks, info.num_entries, info.size);
1243
Harald Welte0113fe72004-01-06 19:04:02 +00001244 if ((h = alloc_handle(info.name, info.size, info.num_entries))
Martin Josefsson841e4ae2003-05-02 15:30:11 +00001245 == NULL) {
Derrik Pates664c0a32005-02-01 13:28:14 +00001246 if (--sockfd_use == 0) {
1247 close(sockfd);
1248 sockfd = -1;
1249 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001250 return NULL;
Martin Josefsson841e4ae2003-05-02 15:30:11 +00001251 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001252
Marc Bouchere6869a82000-03-20 06:03:29 +00001253 /* Initialize current state */
1254 h->info = info;
Harald Welte0113fe72004-01-06 19:04:02 +00001255
Harald Welteaae69be2004-08-29 23:32:14 +00001256 h->entries->size = h->info.size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001257
Rusty Russell79dee072000-05-02 16:45:16 +00001258 tmp = sizeof(STRUCT_GET_ENTRIES) + h->info.size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001259
Harald Welteaae69be2004-08-29 23:32:14 +00001260 if (getsockopt(sockfd, TC_IPPROTO, SO_GET_ENTRIES, h->entries,
1261 &tmp) < 0)
1262 goto error;
1263
1264#ifdef IPTC_DEBUG2
1265 {
1266 int fd = open("/tmp/libiptc-so_get_entries.blob",
1267 O_CREAT|O_WRONLY);
1268 if (fd >= 0) {
1269 write(fd, h->entries, tmp);
1270 close(fd);
1271 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001272 }
Harald Welteaae69be2004-08-29 23:32:14 +00001273#endif
1274
1275 if (parse_table(h) < 0)
1276 goto error;
Rusty Russell7e53bf92000-03-20 07:03:28 +00001277
Marc Bouchere6869a82000-03-20 06:03:29 +00001278 CHECK(h);
1279 return h;
Harald Welteaae69be2004-08-29 23:32:14 +00001280error:
1281 TC_FREE(&h);
Patrick McHardy2f932052008-04-02 14:01:53 +02001282 /* A different process changed the ruleset size, retry */
1283 if (errno == EAGAIN)
1284 goto retry;
Harald Welteaae69be2004-08-29 23:32:14 +00001285 return NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +00001286}
1287
Martin Josefsson841e4ae2003-05-02 15:30:11 +00001288void
1289TC_FREE(TC_HANDLE_T *h)
1290{
Harald Welteaae69be2004-08-29 23:32:14 +00001291 struct chain_head *c, *tmp;
1292
Derrik Pates664c0a32005-02-01 13:28:14 +00001293 iptc_fn = TC_FREE;
1294 if (--sockfd_use == 0) {
1295 close(sockfd);
1296 sockfd = -1;
1297 }
Harald Welteaae69be2004-08-29 23:32:14 +00001298
1299 list_for_each_entry_safe(c, tmp, &(*h)->chains, list) {
1300 struct rule_head *r, *rtmp;
1301
1302 list_for_each_entry_safe(r, rtmp, &c->rules, list) {
1303 free(r);
1304 }
1305
1306 free(c);
1307 }
1308
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +00001309 iptcc_chain_index_free(*h);
1310
Harald Welteaae69be2004-08-29 23:32:14 +00001311 free((*h)->entries);
Martin Josefsson841e4ae2003-05-02 15:30:11 +00001312 free(*h);
Harald Welteaae69be2004-08-29 23:32:14 +00001313
Martin Josefsson841e4ae2003-05-02 15:30:11 +00001314 *h = NULL;
1315}
1316
Marc Bouchere6869a82000-03-20 06:03:29 +00001317static inline int
Rusty Russell79dee072000-05-02 16:45:16 +00001318print_match(const STRUCT_ENTRY_MATCH *m)
Marc Bouchere6869a82000-03-20 06:03:29 +00001319{
Rusty Russell228e98d2000-04-27 10:28:06 +00001320 printf("Match name: `%s'\n", m->u.user.name);
Marc Bouchere6869a82000-03-20 06:03:29 +00001321 return 0;
1322}
1323
Rusty Russell79dee072000-05-02 16:45:16 +00001324static int dump_entry(STRUCT_ENTRY *e, const TC_HANDLE_T handle);
1325
Marc Bouchere6869a82000-03-20 06:03:29 +00001326void
Rusty Russell79dee072000-05-02 16:45:16 +00001327TC_DUMP_ENTRIES(const TC_HANDLE_T handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001328{
Derrik Pates664c0a32005-02-01 13:28:14 +00001329 iptc_fn = TC_DUMP_ENTRIES;
Marc Bouchere6869a82000-03-20 06:03:29 +00001330 CHECK(handle);
Patrick McHardy97fb2f12007-09-08 16:52:25 +00001331
Rusty Russelle45c7132004-12-16 13:21:44 +00001332 printf("libiptc v%s. %u bytes.\n",
1333 IPTABLES_VERSION, handle->entries->size);
Marc Bouchere6869a82000-03-20 06:03:29 +00001334 printf("Table `%s'\n", handle->info.name);
1335 printf("Hooks: pre/in/fwd/out/post = %u/%u/%u/%u/%u\n",
Rusty Russell67088e72000-05-10 01:18:57 +00001336 handle->info.hook_entry[HOOK_PRE_ROUTING],
1337 handle->info.hook_entry[HOOK_LOCAL_IN],
1338 handle->info.hook_entry[HOOK_FORWARD],
1339 handle->info.hook_entry[HOOK_LOCAL_OUT],
1340 handle->info.hook_entry[HOOK_POST_ROUTING]);
Marc Bouchere6869a82000-03-20 06:03:29 +00001341 printf("Underflows: pre/in/fwd/out/post = %u/%u/%u/%u/%u\n",
Rusty Russell67088e72000-05-10 01:18:57 +00001342 handle->info.underflow[HOOK_PRE_ROUTING],
1343 handle->info.underflow[HOOK_LOCAL_IN],
1344 handle->info.underflow[HOOK_FORWARD],
1345 handle->info.underflow[HOOK_LOCAL_OUT],
1346 handle->info.underflow[HOOK_POST_ROUTING]);
Marc Bouchere6869a82000-03-20 06:03:29 +00001347
Harald Welteaae69be2004-08-29 23:32:14 +00001348 ENTRY_ITERATE(handle->entries->entrytable, handle->entries->size,
Rusty Russell79dee072000-05-02 16:45:16 +00001349 dump_entry, handle);
Harald Welte0113fe72004-01-06 19:04:02 +00001350}
Rusty Russell30fd6e52000-04-23 09:16:06 +00001351
Marc Bouchere6869a82000-03-20 06:03:29 +00001352/* Does this chain exist? */
Rusty Russell79dee072000-05-02 16:45:16 +00001353int TC_IS_CHAIN(const char *chain, const TC_HANDLE_T handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001354{
Derrik Pates664c0a32005-02-01 13:28:14 +00001355 iptc_fn = TC_IS_CHAIN;
Harald Welteaae69be2004-08-29 23:32:14 +00001356 return iptcc_find_label(chain, handle) != NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +00001357}
1358
Harald Welteaae69be2004-08-29 23:32:14 +00001359static void iptcc_chain_iterator_advance(TC_HANDLE_T handle)
1360{
1361 struct chain_head *c = handle->chain_iterator_cur;
1362
1363 if (c->list.next == &handle->chains)
1364 handle->chain_iterator_cur = NULL;
1365 else
1366 handle->chain_iterator_cur =
1367 list_entry(c->list.next, struct chain_head, list);
1368}
Marc Bouchere6869a82000-03-20 06:03:29 +00001369
Rusty Russell30fd6e52000-04-23 09:16:06 +00001370/* Iterator functions to run through the chains. */
Marc Bouchere6869a82000-03-20 06:03:29 +00001371const char *
Philip Blundell8c700902000-05-15 02:17:52 +00001372TC_FIRST_CHAIN(TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001373{
Harald Welteaae69be2004-08-29 23:32:14 +00001374 struct chain_head *c = list_entry((*handle)->chains.next,
1375 struct chain_head, list);
1376
1377 iptc_fn = TC_FIRST_CHAIN;
1378
1379
1380 if (list_empty(&(*handle)->chains)) {
1381 DEBUGP(": no chains\n");
Harald Welte0113fe72004-01-06 19:04:02 +00001382 return NULL;
Harald Welteaae69be2004-08-29 23:32:14 +00001383 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001384
Harald Welteaae69be2004-08-29 23:32:14 +00001385 (*handle)->chain_iterator_cur = c;
1386 iptcc_chain_iterator_advance(*handle);
Harald Welte0113fe72004-01-06 19:04:02 +00001387
Harald Welteaae69be2004-08-29 23:32:14 +00001388 DEBUGP(": returning `%s'\n", c->name);
1389 return c->name;
Marc Bouchere6869a82000-03-20 06:03:29 +00001390}
1391
Rusty Russell30fd6e52000-04-23 09:16:06 +00001392/* Iterator functions to run through the chains. Returns NULL at end. */
1393const char *
Rusty Russell79dee072000-05-02 16:45:16 +00001394TC_NEXT_CHAIN(TC_HANDLE_T *handle)
Rusty Russell30fd6e52000-04-23 09:16:06 +00001395{
Harald Welteaae69be2004-08-29 23:32:14 +00001396 struct chain_head *c = (*handle)->chain_iterator_cur;
Rusty Russell30fd6e52000-04-23 09:16:06 +00001397
Harald Welteaae69be2004-08-29 23:32:14 +00001398 iptc_fn = TC_NEXT_CHAIN;
1399
1400 if (!c) {
1401 DEBUGP(": no more chains\n");
Rusty Russell30fd6e52000-04-23 09:16:06 +00001402 return NULL;
Harald Welteaae69be2004-08-29 23:32:14 +00001403 }
Rusty Russell30fd6e52000-04-23 09:16:06 +00001404
Harald Welteaae69be2004-08-29 23:32:14 +00001405 iptcc_chain_iterator_advance(*handle);
1406
1407 DEBUGP(": returning `%s'\n", c->name);
1408 return c->name;
Rusty Russell30fd6e52000-04-23 09:16:06 +00001409}
1410
1411/* Get first rule in the given chain: NULL for empty chain. */
Rusty Russell79dee072000-05-02 16:45:16 +00001412const STRUCT_ENTRY *
Philip Blundell8c700902000-05-15 02:17:52 +00001413TC_FIRST_RULE(const char *chain, TC_HANDLE_T *handle)
Rusty Russell30fd6e52000-04-23 09:16:06 +00001414{
Harald Welteaae69be2004-08-29 23:32:14 +00001415 struct chain_head *c;
1416 struct rule_head *r;
Rusty Russell30fd6e52000-04-23 09:16:06 +00001417
Harald Welteaae69be2004-08-29 23:32:14 +00001418 iptc_fn = TC_FIRST_RULE;
1419
1420 DEBUGP("first rule(%s): ", chain);
1421
1422 c = iptcc_find_label(chain, *handle);
Rusty Russell30fd6e52000-04-23 09:16:06 +00001423 if (!c) {
1424 errno = ENOENT;
1425 return NULL;
1426 }
1427
1428 /* Empty chain: single return/policy rule */
Harald Welteaae69be2004-08-29 23:32:14 +00001429 if (list_empty(&c->rules)) {
1430 DEBUGP_C("no rules, returning NULL\n");
Rusty Russell30fd6e52000-04-23 09:16:06 +00001431 return NULL;
Harald Welteaae69be2004-08-29 23:32:14 +00001432 }
Rusty Russell30fd6e52000-04-23 09:16:06 +00001433
Harald Welteaae69be2004-08-29 23:32:14 +00001434 r = list_entry(c->rules.next, struct rule_head, list);
1435 (*handle)->rule_iterator_cur = r;
1436 DEBUGP_C("%p\n", r);
1437
1438 return r->entry;
Rusty Russell30fd6e52000-04-23 09:16:06 +00001439}
1440
1441/* Returns NULL when rules run out. */
Rusty Russell79dee072000-05-02 16:45:16 +00001442const STRUCT_ENTRY *
Philip Blundell8c700902000-05-15 02:17:52 +00001443TC_NEXT_RULE(const STRUCT_ENTRY *prev, TC_HANDLE_T *handle)
Rusty Russell30fd6e52000-04-23 09:16:06 +00001444{
Harald Welteaae69be2004-08-29 23:32:14 +00001445 struct rule_head *r;
Rusty Russell30fd6e52000-04-23 09:16:06 +00001446
Derrik Pates664c0a32005-02-01 13:28:14 +00001447 iptc_fn = TC_NEXT_RULE;
Harald Welteaae69be2004-08-29 23:32:14 +00001448 DEBUGP("rule_iterator_cur=%p...", (*handle)->rule_iterator_cur);
1449
1450 if (!(*handle)->rule_iterator_cur) {
1451 DEBUGP_C("returning NULL\n");
1452 return NULL;
1453 }
1454
1455 r = list_entry((*handle)->rule_iterator_cur->list.next,
1456 struct rule_head, list);
1457
1458 iptc_fn = TC_NEXT_RULE;
1459
1460 DEBUGP_C("next=%p, head=%p...", &r->list,
1461 &(*handle)->rule_iterator_cur->chain->rules);
1462
1463 if (&r->list == &(*handle)->rule_iterator_cur->chain->rules) {
1464 (*handle)->rule_iterator_cur = NULL;
1465 DEBUGP_C("finished, returning NULL\n");
1466 return NULL;
1467 }
1468
1469 (*handle)->rule_iterator_cur = r;
1470
1471 /* NOTE: prev is without any influence ! */
1472 DEBUGP_C("returning rule %p\n", r);
1473 return r->entry;
Rusty Russell30fd6e52000-04-23 09:16:06 +00001474}
1475
Marc Bouchere6869a82000-03-20 06:03:29 +00001476/* How many rules in this chain? */
Jan Engelhardt33690a12008-02-11 00:54:00 +01001477static unsigned int
Rusty Russell79dee072000-05-02 16:45:16 +00001478TC_NUM_RULES(const char *chain, TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001479{
Harald Welteaae69be2004-08-29 23:32:14 +00001480 struct chain_head *c;
1481 iptc_fn = TC_NUM_RULES;
Marc Bouchere6869a82000-03-20 06:03:29 +00001482 CHECK(*handle);
Harald Welteaae69be2004-08-29 23:32:14 +00001483
1484 c = iptcc_find_label(chain, *handle);
1485 if (!c) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001486 errno = ENOENT;
1487 return (unsigned int)-1;
1488 }
Harald Welteaae69be2004-08-29 23:32:14 +00001489
1490 return c->num_rules;
Marc Bouchere6869a82000-03-20 06:03:29 +00001491}
1492
Jan Engelhardt33690a12008-02-11 00:54:00 +01001493static const STRUCT_ENTRY *
1494TC_GET_RULE(const char *chain, unsigned int n, TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001495{
Harald Welteaae69be2004-08-29 23:32:14 +00001496 struct chain_head *c;
1497 struct rule_head *r;
1498
1499 iptc_fn = TC_GET_RULE;
Marc Bouchere6869a82000-03-20 06:03:29 +00001500
1501 CHECK(*handle);
Harald Welteaae69be2004-08-29 23:32:14 +00001502
1503 c = iptcc_find_label(chain, *handle);
1504 if (!c) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001505 errno = ENOENT;
1506 return NULL;
1507 }
1508
Harald Welteaae69be2004-08-29 23:32:14 +00001509 r = iptcc_get_rule_num(c, n);
1510 if (!r)
1511 return NULL;
1512 return r->entry;
Marc Bouchere6869a82000-03-20 06:03:29 +00001513}
1514
1515/* Returns a pointer to the target name of this position. */
Jan Engelhardt33690a12008-02-11 00:54:00 +01001516static const char *standard_target_map(int verdict)
Marc Bouchere6869a82000-03-20 06:03:29 +00001517{
Harald Welteaae69be2004-08-29 23:32:14 +00001518 switch (verdict) {
1519 case RETURN:
1520 return LABEL_RETURN;
1521 break;
1522 case -NF_ACCEPT-1:
1523 return LABEL_ACCEPT;
1524 break;
1525 case -NF_DROP-1:
1526 return LABEL_DROP;
1527 break;
1528 case -NF_QUEUE-1:
1529 return LABEL_QUEUE;
1530 break;
1531 default:
1532 fprintf(stderr, "ERROR: %d not a valid target)\n",
1533 verdict);
1534 abort();
1535 break;
1536 }
1537 /* not reached */
1538 return NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +00001539}
1540
Harald Welteaae69be2004-08-29 23:32:14 +00001541/* Returns a pointer to the target name of this position. */
1542const char *TC_GET_TARGET(const STRUCT_ENTRY *ce,
1543 TC_HANDLE_T *handle)
1544{
1545 STRUCT_ENTRY *e = (STRUCT_ENTRY *)ce;
Rusty Russelle45c7132004-12-16 13:21:44 +00001546 struct rule_head *r = container_of(e, struct rule_head, entry[0]);
Harald Welteaae69be2004-08-29 23:32:14 +00001547
1548 iptc_fn = TC_GET_TARGET;
1549
1550 switch(r->type) {
1551 int spos;
1552 case IPTCC_R_FALLTHROUGH:
1553 return "";
1554 break;
1555 case IPTCC_R_JUMP:
1556 DEBUGP("r=%p, jump=%p, name=`%s'\n", r, r->jump, r->jump->name);
1557 return r->jump->name;
1558 break;
1559 case IPTCC_R_STANDARD:
1560 spos = *(int *)GET_TARGET(e)->data;
1561 DEBUGP("r=%p, spos=%d'\n", r, spos);
1562 return standard_target_map(spos);
1563 break;
1564 case IPTCC_R_MODULE:
1565 return GET_TARGET(e)->u.user.name;
1566 break;
1567 }
1568 return NULL;
1569}
Marc Bouchere6869a82000-03-20 06:03:29 +00001570/* Is this a built-in chain? Actually returns hook + 1. */
1571int
Rusty Russell79dee072000-05-02 16:45:16 +00001572TC_BUILTIN(const char *chain, const TC_HANDLE_T handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001573{
Harald Welteaae69be2004-08-29 23:32:14 +00001574 struct chain_head *c;
1575
1576 iptc_fn = TC_BUILTIN;
Marc Bouchere6869a82000-03-20 06:03:29 +00001577
Harald Welteaae69be2004-08-29 23:32:14 +00001578 c = iptcc_find_label(chain, handle);
1579 if (!c) {
1580 errno = ENOENT;
Martin Josefssonb0f3d2d2004-09-23 18:23:20 +00001581 return 0;
Marc Bouchere6869a82000-03-20 06:03:29 +00001582 }
Harald Welteaae69be2004-08-29 23:32:14 +00001583
1584 return iptcc_is_builtin(c);
Marc Bouchere6869a82000-03-20 06:03:29 +00001585}
1586
1587/* Get the policy of a given built-in chain */
1588const char *
Rusty Russell79dee072000-05-02 16:45:16 +00001589TC_GET_POLICY(const char *chain,
1590 STRUCT_COUNTERS *counters,
1591 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001592{
Harald Welteaae69be2004-08-29 23:32:14 +00001593 struct chain_head *c;
Marc Bouchere6869a82000-03-20 06:03:29 +00001594
Harald Welteaae69be2004-08-29 23:32:14 +00001595 iptc_fn = TC_GET_POLICY;
1596
1597 DEBUGP("called for chain %s\n", chain);
1598
1599 c = iptcc_find_label(chain, *handle);
1600 if (!c) {
1601 errno = ENOENT;
1602 return NULL;
1603 }
1604
1605 if (!iptcc_is_builtin(c))
Marc Bouchere6869a82000-03-20 06:03:29 +00001606 return NULL;
1607
Harald Welteaae69be2004-08-29 23:32:14 +00001608 *counters = c->counters;
Marc Bouchere6869a82000-03-20 06:03:29 +00001609
Harald Welteaae69be2004-08-29 23:32:14 +00001610 return standard_target_map(c->verdict);
Harald Welte0113fe72004-01-06 19:04:02 +00001611}
1612
1613static int
Harald Welteaae69be2004-08-29 23:32:14 +00001614iptcc_standard_map(struct rule_head *r, int verdict)
Harald Welte0113fe72004-01-06 19:04:02 +00001615{
Harald Welteaae69be2004-08-29 23:32:14 +00001616 STRUCT_ENTRY *e = r->entry;
Rusty Russell79dee072000-05-02 16:45:16 +00001617 STRUCT_STANDARD_TARGET *t;
Marc Bouchere6869a82000-03-20 06:03:29 +00001618
Rusty Russell79dee072000-05-02 16:45:16 +00001619 t = (STRUCT_STANDARD_TARGET *)GET_TARGET(e);
Marc Bouchere6869a82000-03-20 06:03:29 +00001620
Rusty Russell67088e72000-05-10 01:18:57 +00001621 if (t->target.u.target_size
Philip Blundell8c700902000-05-15 02:17:52 +00001622 != ALIGN(sizeof(STRUCT_STANDARD_TARGET))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001623 errno = EINVAL;
1624 return 0;
1625 }
1626 /* memset for memcmp convenience on delete/replace */
Rusty Russell79dee072000-05-02 16:45:16 +00001627 memset(t->target.u.user.name, 0, FUNCTION_MAXNAMELEN);
1628 strcpy(t->target.u.user.name, STANDARD_TARGET);
Marc Bouchere6869a82000-03-20 06:03:29 +00001629 t->verdict = verdict;
1630
Harald Welteaae69be2004-08-29 23:32:14 +00001631 r->type = IPTCC_R_STANDARD;
1632
Marc Bouchere6869a82000-03-20 06:03:29 +00001633 return 1;
1634}
Rusty Russell7e53bf92000-03-20 07:03:28 +00001635
Marc Bouchere6869a82000-03-20 06:03:29 +00001636static int
Harald Welteaae69be2004-08-29 23:32:14 +00001637iptcc_map_target(const TC_HANDLE_T handle,
1638 struct rule_head *r)
Marc Bouchere6869a82000-03-20 06:03:29 +00001639{
Harald Welteaae69be2004-08-29 23:32:14 +00001640 STRUCT_ENTRY *e = r->entry;
Harald Welte0113fe72004-01-06 19:04:02 +00001641 STRUCT_ENTRY_TARGET *t = GET_TARGET(e);
Marc Bouchere6869a82000-03-20 06:03:29 +00001642
Marc Bouchere6869a82000-03-20 06:03:29 +00001643 /* Maybe it's empty (=> fall through) */
Harald Welteaae69be2004-08-29 23:32:14 +00001644 if (strcmp(t->u.user.name, "") == 0) {
1645 r->type = IPTCC_R_FALLTHROUGH;
1646 return 1;
1647 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001648 /* Maybe it's a standard target name... */
Rusty Russell79dee072000-05-02 16:45:16 +00001649 else if (strcmp(t->u.user.name, LABEL_ACCEPT) == 0)
Harald Welteaae69be2004-08-29 23:32:14 +00001650 return iptcc_standard_map(r, -NF_ACCEPT - 1);
Rusty Russell79dee072000-05-02 16:45:16 +00001651 else if (strcmp(t->u.user.name, LABEL_DROP) == 0)
Harald Welteaae69be2004-08-29 23:32:14 +00001652 return iptcc_standard_map(r, -NF_DROP - 1);
Rusty Russell67088e72000-05-10 01:18:57 +00001653 else if (strcmp(t->u.user.name, LABEL_QUEUE) == 0)
Harald Welteaae69be2004-08-29 23:32:14 +00001654 return iptcc_standard_map(r, -NF_QUEUE - 1);
Rusty Russell79dee072000-05-02 16:45:16 +00001655 else if (strcmp(t->u.user.name, LABEL_RETURN) == 0)
Harald Welteaae69be2004-08-29 23:32:14 +00001656 return iptcc_standard_map(r, RETURN);
Rusty Russell79dee072000-05-02 16:45:16 +00001657 else if (TC_BUILTIN(t->u.user.name, handle)) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001658 /* Can't jump to builtins. */
1659 errno = EINVAL;
1660 return 0;
1661 } else {
1662 /* Maybe it's an existing chain name. */
Harald Welteaae69be2004-08-29 23:32:14 +00001663 struct chain_head *c;
1664 DEBUGP("trying to find chain `%s': ", t->u.user.name);
Marc Bouchere6869a82000-03-20 06:03:29 +00001665
Harald Welteaae69be2004-08-29 23:32:14 +00001666 c = iptcc_find_label(t->u.user.name, handle);
1667 if (c) {
1668 DEBUGP_C("found!\n");
1669 r->type = IPTCC_R_JUMP;
1670 r->jump = c;
1671 c->references++;
1672 return 1;
1673 }
1674 DEBUGP_C("not found :(\n");
Marc Bouchere6869a82000-03-20 06:03:29 +00001675 }
1676
1677 /* Must be a module? If not, kernel will reject... */
Rusty Russell3aef54d2005-01-03 03:48:40 +00001678 /* memset to all 0 for your memcmp convenience: don't clear version */
Rusty Russell228e98d2000-04-27 10:28:06 +00001679 memset(t->u.user.name + strlen(t->u.user.name),
Marc Bouchere6869a82000-03-20 06:03:29 +00001680 0,
Rusty Russell3aef54d2005-01-03 03:48:40 +00001681 FUNCTION_MAXNAMELEN - 1 - strlen(t->u.user.name));
Rusty Russell733e54b2004-12-16 14:22:23 +00001682 r->type = IPTCC_R_MODULE;
Harald Welteaae69be2004-08-29 23:32:14 +00001683 set_changed(handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001684 return 1;
1685}
1686
Harald Welte0113fe72004-01-06 19:04:02 +00001687/* Insert the entry `fw' in chain `chain' into position `rulenum'. */
Marc Bouchere6869a82000-03-20 06:03:29 +00001688int
Rusty Russell79dee072000-05-02 16:45:16 +00001689TC_INSERT_ENTRY(const IPT_CHAINLABEL chain,
1690 const STRUCT_ENTRY *e,
1691 unsigned int rulenum,
1692 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001693{
Harald Welteaae69be2004-08-29 23:32:14 +00001694 struct chain_head *c;
Martin Josefssoneb066cc2004-09-22 21:04:07 +00001695 struct rule_head *r;
1696 struct list_head *prev;
Marc Bouchere6869a82000-03-20 06:03:29 +00001697
Rusty Russell79dee072000-05-02 16:45:16 +00001698 iptc_fn = TC_INSERT_ENTRY;
Harald Welteaae69be2004-08-29 23:32:14 +00001699
1700 if (!(c = iptcc_find_label(chain, *handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001701 errno = ENOENT;
1702 return 0;
1703 }
1704
Martin Josefssoneb066cc2004-09-22 21:04:07 +00001705 /* first rulenum index = 0
1706 first c->num_rules index = 1 */
1707 if (rulenum > c->num_rules) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001708 errno = E2BIG;
1709 return 0;
1710 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001711
Martin Josefsson631f3612004-09-23 19:25:06 +00001712 /* If we are inserting at the end just take advantage of the
1713 double linked list, insert will happen before the entry
1714 prev points to. */
1715 if (rulenum == c->num_rules) {
Martin Josefssoneb066cc2004-09-22 21:04:07 +00001716 prev = &c->rules;
Martin Josefssona5616dc2004-10-24 22:27:31 +00001717 } else if (rulenum + 1 <= c->num_rules/2) {
Martin Josefsson631f3612004-09-23 19:25:06 +00001718 r = iptcc_get_rule_num(c, rulenum + 1);
Martin Josefssona5616dc2004-10-24 22:27:31 +00001719 prev = &r->list;
1720 } else {
1721 r = iptcc_get_rule_num_reverse(c, c->num_rules - rulenum);
Martin Josefsson631f3612004-09-23 19:25:06 +00001722 prev = &r->list;
1723 }
Martin Josefssoneb066cc2004-09-22 21:04:07 +00001724
Harald Welteaae69be2004-08-29 23:32:14 +00001725 if (!(r = iptcc_alloc_rule(c, e->next_offset))) {
1726 errno = ENOMEM;
Harald Welte0113fe72004-01-06 19:04:02 +00001727 return 0;
Harald Welteaae69be2004-08-29 23:32:14 +00001728 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001729
Harald Welteaae69be2004-08-29 23:32:14 +00001730 memcpy(r->entry, e, e->next_offset);
1731 r->counter_map.maptype = COUNTER_MAP_SET;
1732
1733 if (!iptcc_map_target(*handle, r)) {
1734 free(r);
1735 return 0;
1736 }
1737
Martin Josefssoneb066cc2004-09-22 21:04:07 +00001738 list_add_tail(&r->list, prev);
Harald Welteaae69be2004-08-29 23:32:14 +00001739 c->num_rules++;
1740
1741 set_changed(*handle);
1742
1743 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00001744}
1745
1746/* Atomically replace rule `rulenum' in `chain' with `fw'. */
1747int
Rusty Russell79dee072000-05-02 16:45:16 +00001748TC_REPLACE_ENTRY(const IPT_CHAINLABEL chain,
1749 const STRUCT_ENTRY *e,
1750 unsigned int rulenum,
1751 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001752{
Harald Welteaae69be2004-08-29 23:32:14 +00001753 struct chain_head *c;
1754 struct rule_head *r, *old;
Marc Bouchere6869a82000-03-20 06:03:29 +00001755
Rusty Russell79dee072000-05-02 16:45:16 +00001756 iptc_fn = TC_REPLACE_ENTRY;
Marc Bouchere6869a82000-03-20 06:03:29 +00001757
Harald Welteaae69be2004-08-29 23:32:14 +00001758 if (!(c = iptcc_find_label(chain, *handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001759 errno = ENOENT;
1760 return 0;
1761 }
1762
Martin Josefsson0f9b8b12004-12-18 17:18:49 +00001763 if (rulenum >= c->num_rules) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001764 errno = E2BIG;
1765 return 0;
1766 }
1767
Martin Josefsson0f9b8b12004-12-18 17:18:49 +00001768 /* Take advantage of the double linked list if possible. */
1769 if (rulenum + 1 <= c->num_rules/2) {
1770 old = iptcc_get_rule_num(c, rulenum + 1);
1771 } else {
1772 old = iptcc_get_rule_num_reverse(c, c->num_rules - rulenum);
1773 }
1774
Harald Welteaae69be2004-08-29 23:32:14 +00001775 if (!(r = iptcc_alloc_rule(c, e->next_offset))) {
1776 errno = ENOMEM;
Marc Bouchere6869a82000-03-20 06:03:29 +00001777 return 0;
Harald Welteaae69be2004-08-29 23:32:14 +00001778 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001779
Harald Welteaae69be2004-08-29 23:32:14 +00001780 memcpy(r->entry, e, e->next_offset);
1781 r->counter_map.maptype = COUNTER_MAP_SET;
1782
1783 if (!iptcc_map_target(*handle, r)) {
1784 free(r);
Harald Welte0113fe72004-01-06 19:04:02 +00001785 return 0;
Harald Welteaae69be2004-08-29 23:32:14 +00001786 }
Harald Welte0113fe72004-01-06 19:04:02 +00001787
Harald Welteaae69be2004-08-29 23:32:14 +00001788 list_add(&r->list, &old->list);
1789 iptcc_delete_rule(old);
1790
1791 set_changed(*handle);
1792
1793 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00001794}
1795
Harald Welte0113fe72004-01-06 19:04:02 +00001796/* Append entry `fw' to chain `chain'. Equivalent to insert with
Marc Bouchere6869a82000-03-20 06:03:29 +00001797 rulenum = length of chain. */
1798int
Rusty Russell79dee072000-05-02 16:45:16 +00001799TC_APPEND_ENTRY(const IPT_CHAINLABEL chain,
1800 const STRUCT_ENTRY *e,
1801 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001802{
Harald Welteaae69be2004-08-29 23:32:14 +00001803 struct chain_head *c;
1804 struct rule_head *r;
Marc Bouchere6869a82000-03-20 06:03:29 +00001805
Rusty Russell79dee072000-05-02 16:45:16 +00001806 iptc_fn = TC_APPEND_ENTRY;
Harald Welteaae69be2004-08-29 23:32:14 +00001807 if (!(c = iptcc_find_label(chain, *handle))) {
1808 DEBUGP("unable to find chain `%s'\n", chain);
Marc Bouchere6869a82000-03-20 06:03:29 +00001809 errno = ENOENT;
1810 return 0;
1811 }
1812
Harald Welteaae69be2004-08-29 23:32:14 +00001813 if (!(r = iptcc_alloc_rule(c, e->next_offset))) {
1814 DEBUGP("unable to allocate rule for chain `%s'\n", chain);
1815 errno = ENOMEM;
Harald Welte0113fe72004-01-06 19:04:02 +00001816 return 0;
Harald Welteaae69be2004-08-29 23:32:14 +00001817 }
Harald Welte0113fe72004-01-06 19:04:02 +00001818
Harald Welteaae69be2004-08-29 23:32:14 +00001819 memcpy(r->entry, e, e->next_offset);
1820 r->counter_map.maptype = COUNTER_MAP_SET;
1821
1822 if (!iptcc_map_target(*handle, r)) {
Martin Josefsson12009532004-09-23 18:24:29 +00001823 DEBUGP("unable to map target of rule for chain `%s'\n", chain);
Harald Welteaae69be2004-08-29 23:32:14 +00001824 free(r);
1825 return 0;
1826 }
1827
1828 list_add_tail(&r->list, &c->rules);
1829 c->num_rules++;
1830
1831 set_changed(*handle);
1832
1833 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00001834}
1835
1836static inline int
Rusty Russell79dee072000-05-02 16:45:16 +00001837match_different(const STRUCT_ENTRY_MATCH *a,
Rusty Russelledf14cf2000-04-19 11:26:44 +00001838 const unsigned char *a_elems,
1839 const unsigned char *b_elems,
1840 unsigned char **maskptr)
Marc Bouchere6869a82000-03-20 06:03:29 +00001841{
Rusty Russell79dee072000-05-02 16:45:16 +00001842 const STRUCT_ENTRY_MATCH *b;
Rusty Russelledf14cf2000-04-19 11:26:44 +00001843 unsigned int i;
Marc Bouchere6869a82000-03-20 06:03:29 +00001844
1845 /* Offset of b is the same as a. */
Rusty Russell30fd6e52000-04-23 09:16:06 +00001846 b = (void *)b_elems + ((unsigned char *)a - a_elems);
Marc Bouchere6869a82000-03-20 06:03:29 +00001847
Rusty Russell228e98d2000-04-27 10:28:06 +00001848 if (a->u.match_size != b->u.match_size)
Marc Bouchere6869a82000-03-20 06:03:29 +00001849 return 1;
1850
Rusty Russell228e98d2000-04-27 10:28:06 +00001851 if (strcmp(a->u.user.name, b->u.user.name) != 0)
Marc Bouchere6869a82000-03-20 06:03:29 +00001852 return 1;
1853
Rusty Russell73ef09b2000-07-03 10:24:04 +00001854 *maskptr += ALIGN(sizeof(*a));
Rusty Russelledf14cf2000-04-19 11:26:44 +00001855
Rusty Russell73ef09b2000-07-03 10:24:04 +00001856 for (i = 0; i < a->u.match_size - ALIGN(sizeof(*a)); i++)
Rusty Russelledf14cf2000-04-19 11:26:44 +00001857 if (((a->data[i] ^ b->data[i]) & (*maskptr)[i]) != 0)
Rusty Russell90e712a2000-03-29 04:19:26 +00001858 return 1;
Rusty Russelledf14cf2000-04-19 11:26:44 +00001859 *maskptr += i;
1860 return 0;
1861}
1862
1863static inline int
Rusty Russell733e54b2004-12-16 14:22:23 +00001864target_same(struct rule_head *a, struct rule_head *b,const unsigned char *mask)
Rusty Russelledf14cf2000-04-19 11:26:44 +00001865{
1866 unsigned int i;
Rusty Russell733e54b2004-12-16 14:22:23 +00001867 STRUCT_ENTRY_TARGET *ta, *tb;
Marc Bouchere6869a82000-03-20 06:03:29 +00001868
Rusty Russell733e54b2004-12-16 14:22:23 +00001869 if (a->type != b->type)
1870 return 0;
1871
1872 ta = GET_TARGET(a->entry);
1873 tb = GET_TARGET(b->entry);
1874
1875 switch (a->type) {
1876 case IPTCC_R_FALLTHROUGH:
1877 return 1;
1878 case IPTCC_R_JUMP:
1879 return a->jump == b->jump;
1880 case IPTCC_R_STANDARD:
1881 return ((STRUCT_STANDARD_TARGET *)ta)->verdict
1882 == ((STRUCT_STANDARD_TARGET *)tb)->verdict;
1883 case IPTCC_R_MODULE:
1884 if (ta->u.target_size != tb->u.target_size)
1885 return 0;
1886 if (strcmp(ta->u.user.name, tb->u.user.name) != 0)
1887 return 0;
1888
1889 for (i = 0; i < ta->u.target_size - sizeof(*ta); i++)
Rusty Russelldaade442004-12-29 11:14:52 +00001890 if (((ta->data[i] ^ tb->data[i]) & mask[i]) != 0)
Rusty Russell733e54b2004-12-16 14:22:23 +00001891 return 0;
1892 return 1;
1893 default:
1894 fprintf(stderr, "ERROR: bad type %i\n", a->type);
1895 abort();
1896 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001897}
1898
Rusty Russell733e54b2004-12-16 14:22:23 +00001899static unsigned char *
Rusty Russell79dee072000-05-02 16:45:16 +00001900is_same(const STRUCT_ENTRY *a,
1901 const STRUCT_ENTRY *b,
1902 unsigned char *matchmask);
Marc Bouchere6869a82000-03-20 06:03:29 +00001903
Harald Welte0113fe72004-01-06 19:04:02 +00001904/* Delete the first rule in `chain' which matches `fw'. */
Marc Bouchere6869a82000-03-20 06:03:29 +00001905int
Rusty Russell79dee072000-05-02 16:45:16 +00001906TC_DELETE_ENTRY(const IPT_CHAINLABEL chain,
1907 const STRUCT_ENTRY *origfw,
1908 unsigned char *matchmask,
1909 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001910{
Harald Welteaae69be2004-08-29 23:32:14 +00001911 struct chain_head *c;
Rusty Russelle45c7132004-12-16 13:21:44 +00001912 struct rule_head *r, *i;
Marc Bouchere6869a82000-03-20 06:03:29 +00001913
Rusty Russell79dee072000-05-02 16:45:16 +00001914 iptc_fn = TC_DELETE_ENTRY;
Harald Welteaae69be2004-08-29 23:32:14 +00001915 if (!(c = iptcc_find_label(chain, *handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001916 errno = ENOENT;
1917 return 0;
1918 }
1919
Rusty Russelle45c7132004-12-16 13:21:44 +00001920 /* Create a rule_head from origfw. */
1921 r = iptcc_alloc_rule(c, origfw->next_offset);
1922 if (!r) {
Harald Welte0113fe72004-01-06 19:04:02 +00001923 errno = ENOMEM;
1924 return 0;
1925 }
1926
Rusty Russelle45c7132004-12-16 13:21:44 +00001927 memcpy(r->entry, origfw, origfw->next_offset);
1928 r->counter_map.maptype = COUNTER_MAP_NOMAP;
1929 if (!iptcc_map_target(*handle, r)) {
1930 DEBUGP("unable to map target of rule for chain `%s'\n", chain);
1931 free(r);
1932 return 0;
Patrick McHardyJesper Brouer04a1e4c2006-07-25 01:50:48 +00001933 } else {
1934 /* iptcc_map_target increment target chain references
1935 * since this is a fake rule only used for matching
1936 * the chain references count is decremented again.
1937 */
1938 if (r->type == IPTCC_R_JUMP
1939 && r->jump)
1940 r->jump->references--;
Rusty Russelle45c7132004-12-16 13:21:44 +00001941 }
Harald Welte0113fe72004-01-06 19:04:02 +00001942
Rusty Russelle45c7132004-12-16 13:21:44 +00001943 list_for_each_entry(i, &c->rules, list) {
Rusty Russell733e54b2004-12-16 14:22:23 +00001944 unsigned char *mask;
Harald Weltefe537072004-08-30 20:28:53 +00001945
Rusty Russell733e54b2004-12-16 14:22:23 +00001946 mask = is_same(r->entry, i->entry, matchmask);
1947 if (!mask)
1948 continue;
Martin Josefsson2a5dbbb2004-09-22 21:37:41 +00001949
Rusty Russell733e54b2004-12-16 14:22:23 +00001950 if (!target_same(r, i, mask))
1951 continue;
1952
1953 /* If we are about to delete the rule that is the
1954 * current iterator, move rule iterator back. next
1955 * pointer will then point to real next node */
1956 if (i == (*handle)->rule_iterator_cur) {
1957 (*handle)->rule_iterator_cur =
1958 list_entry((*handle)->rule_iterator_cur->list.prev,
1959 struct rule_head, list);
Marc Bouchere6869a82000-03-20 06:03:29 +00001960 }
Rusty Russell733e54b2004-12-16 14:22:23 +00001961
1962 c->num_rules--;
1963 iptcc_delete_rule(i);
1964
1965 set_changed(*handle);
1966 free(r);
1967 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00001968 }
1969
Rusty Russelle45c7132004-12-16 13:21:44 +00001970 free(r);
Marc Bouchere6869a82000-03-20 06:03:29 +00001971 errno = ENOENT;
1972 return 0;
Rusty Russell7e53bf92000-03-20 07:03:28 +00001973}
Harald Welteaae69be2004-08-29 23:32:14 +00001974
Marc Bouchere6869a82000-03-20 06:03:29 +00001975
1976/* Delete the rule in position `rulenum' in `chain'. */
1977int
Rusty Russell79dee072000-05-02 16:45:16 +00001978TC_DELETE_NUM_ENTRY(const IPT_CHAINLABEL chain,
1979 unsigned int rulenum,
1980 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001981{
Harald Welteaae69be2004-08-29 23:32:14 +00001982 struct chain_head *c;
1983 struct rule_head *r;
Marc Bouchere6869a82000-03-20 06:03:29 +00001984
Rusty Russell79dee072000-05-02 16:45:16 +00001985 iptc_fn = TC_DELETE_NUM_ENTRY;
Harald Welteaae69be2004-08-29 23:32:14 +00001986
1987 if (!(c = iptcc_find_label(chain, *handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001988 errno = ENOENT;
1989 return 0;
1990 }
1991
Martin Josefssona5616dc2004-10-24 22:27:31 +00001992 if (rulenum >= c->num_rules) {
Martin Josefsson631f3612004-09-23 19:25:06 +00001993 errno = E2BIG;
1994 return 0;
1995 }
1996
1997 /* Take advantage of the double linked list if possible. */
Martin Josefssona5616dc2004-10-24 22:27:31 +00001998 if (rulenum + 1 <= c->num_rules/2) {
1999 r = iptcc_get_rule_num(c, rulenum + 1);
2000 } else {
2001 r = iptcc_get_rule_num_reverse(c, c->num_rules - rulenum);
Marc Bouchere6869a82000-03-20 06:03:29 +00002002 }
2003
Harald Welteaae69be2004-08-29 23:32:14 +00002004 /* If we are about to delete the rule that is the current
2005 * iterator, move rule iterator back. next pointer will then
2006 * point to real next node */
2007 if (r == (*handle)->rule_iterator_cur) {
2008 (*handle)->rule_iterator_cur =
2009 list_entry((*handle)->rule_iterator_cur->list.prev,
2010 struct rule_head, list);
Harald Welte0113fe72004-01-06 19:04:02 +00002011 }
Marc Bouchere6869a82000-03-20 06:03:29 +00002012
Harald Welteaae69be2004-08-29 23:32:14 +00002013 c->num_rules--;
2014 iptcc_delete_rule(r);
2015
Martin Josefsson2a5dbbb2004-09-22 21:37:41 +00002016 set_changed(*handle);
2017
Harald Welteaae69be2004-08-29 23:32:14 +00002018 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00002019}
2020
2021/* Check the packet `fw' on chain `chain'. Returns the verdict, or
2022 NULL and sets errno. */
2023const char *
Rusty Russell79dee072000-05-02 16:45:16 +00002024TC_CHECK_PACKET(const IPT_CHAINLABEL chain,
2025 STRUCT_ENTRY *entry,
2026 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00002027{
Derrik Pates664c0a32005-02-01 13:28:14 +00002028 iptc_fn = TC_CHECK_PACKET;
Marc Bouchere6869a82000-03-20 06:03:29 +00002029 errno = ENOSYS;
2030 return NULL;
2031}
2032
2033/* Flushes the entries in the given chain (ie. empties chain). */
2034int
Rusty Russell79dee072000-05-02 16:45:16 +00002035TC_FLUSH_ENTRIES(const IPT_CHAINLABEL chain, TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00002036{
Harald Welteaae69be2004-08-29 23:32:14 +00002037 struct chain_head *c;
2038 struct rule_head *r, *tmp;
Marc Bouchere6869a82000-03-20 06:03:29 +00002039
Harald Welte0113fe72004-01-06 19:04:02 +00002040 iptc_fn = TC_FLUSH_ENTRIES;
Harald Welteaae69be2004-08-29 23:32:14 +00002041 if (!(c = iptcc_find_label(chain, *handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002042 errno = ENOENT;
2043 return 0;
2044 }
Marc Bouchere6869a82000-03-20 06:03:29 +00002045
Harald Welteaae69be2004-08-29 23:32:14 +00002046 list_for_each_entry_safe(r, tmp, &c->rules, list) {
2047 iptcc_delete_rule(r);
2048 }
2049
2050 c->num_rules = 0;
2051
2052 set_changed(*handle);
2053
2054 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00002055}
2056
2057/* Zeroes the counters in a chain. */
2058int
Rusty Russell79dee072000-05-02 16:45:16 +00002059TC_ZERO_ENTRIES(const IPT_CHAINLABEL chain, TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00002060{
Harald Welteaae69be2004-08-29 23:32:14 +00002061 struct chain_head *c;
2062 struct rule_head *r;
Rusty Russell7e53bf92000-03-20 07:03:28 +00002063
Derrik Pates664c0a32005-02-01 13:28:14 +00002064 iptc_fn = TC_ZERO_ENTRIES;
Harald Welteaae69be2004-08-29 23:32:14 +00002065 if (!(c = iptcc_find_label(chain, *handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002066 errno = ENOENT;
2067 return 0;
2068 }
Marc Bouchere6869a82000-03-20 06:03:29 +00002069
Andy Gaye5bd1d72006-08-22 02:56:41 +00002070 if (c->counter_map.maptype == COUNTER_MAP_NORMAL_MAP)
2071 c->counter_map.maptype = COUNTER_MAP_ZEROED;
2072
Harald Welteaae69be2004-08-29 23:32:14 +00002073 list_for_each_entry(r, &c->rules, list) {
2074 if (r->counter_map.maptype == COUNTER_MAP_NORMAL_MAP)
2075 r->counter_map.maptype = COUNTER_MAP_ZEROED;
Marc Bouchere6869a82000-03-20 06:03:29 +00002076 }
Harald Welteaae69be2004-08-29 23:32:14 +00002077
Rusty Russell175f6412000-03-24 09:32:20 +00002078 set_changed(*handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00002079
Marc Bouchere6869a82000-03-20 06:03:29 +00002080 return 1;
2081}
2082
Harald Welte1cef74d2001-01-05 15:22:59 +00002083STRUCT_COUNTERS *
2084TC_READ_COUNTER(const IPT_CHAINLABEL chain,
2085 unsigned int rulenum,
2086 TC_HANDLE_T *handle)
2087{
Harald Welteaae69be2004-08-29 23:32:14 +00002088 struct chain_head *c;
2089 struct rule_head *r;
Harald Welte1cef74d2001-01-05 15:22:59 +00002090
2091 iptc_fn = TC_READ_COUNTER;
2092 CHECK(*handle);
2093
Harald Welteaae69be2004-08-29 23:32:14 +00002094 if (!(c = iptcc_find_label(chain, *handle))) {
Harald Welte1cef74d2001-01-05 15:22:59 +00002095 errno = ENOENT;
2096 return NULL;
2097 }
2098
Harald Welteaae69be2004-08-29 23:32:14 +00002099 if (!(r = iptcc_get_rule_num(c, rulenum))) {
Harald Welte0113fe72004-01-06 19:04:02 +00002100 errno = E2BIG;
2101 return NULL;
2102 }
2103
Harald Welteaae69be2004-08-29 23:32:14 +00002104 return &r->entry[0].counters;
Harald Welte1cef74d2001-01-05 15:22:59 +00002105}
2106
2107int
2108TC_ZERO_COUNTER(const IPT_CHAINLABEL chain,
2109 unsigned int rulenum,
2110 TC_HANDLE_T *handle)
2111{
Harald Welteaae69be2004-08-29 23:32:14 +00002112 struct chain_head *c;
2113 struct rule_head *r;
Harald Welte1cef74d2001-01-05 15:22:59 +00002114
2115 iptc_fn = TC_ZERO_COUNTER;
2116 CHECK(*handle);
2117
Harald Welteaae69be2004-08-29 23:32:14 +00002118 if (!(c = iptcc_find_label(chain, *handle))) {
Harald Welte1cef74d2001-01-05 15:22:59 +00002119 errno = ENOENT;
2120 return 0;
2121 }
2122
Harald Welteaae69be2004-08-29 23:32:14 +00002123 if (!(r = iptcc_get_rule_num(c, rulenum))) {
Harald Welte0113fe72004-01-06 19:04:02 +00002124 errno = E2BIG;
2125 return 0;
2126 }
2127
Harald Welteaae69be2004-08-29 23:32:14 +00002128 if (r->counter_map.maptype == COUNTER_MAP_NORMAL_MAP)
2129 r->counter_map.maptype = COUNTER_MAP_ZEROED;
Harald Welte1cef74d2001-01-05 15:22:59 +00002130
2131 set_changed(*handle);
2132
2133 return 1;
2134}
2135
2136int
2137TC_SET_COUNTER(const IPT_CHAINLABEL chain,
2138 unsigned int rulenum,
2139 STRUCT_COUNTERS *counters,
2140 TC_HANDLE_T *handle)
2141{
Harald Welteaae69be2004-08-29 23:32:14 +00002142 struct chain_head *c;
2143 struct rule_head *r;
Harald Welte1cef74d2001-01-05 15:22:59 +00002144 STRUCT_ENTRY *e;
Harald Welte1cef74d2001-01-05 15:22:59 +00002145
2146 iptc_fn = TC_SET_COUNTER;
2147 CHECK(*handle);
2148
Harald Welteaae69be2004-08-29 23:32:14 +00002149 if (!(c = iptcc_find_label(chain, *handle))) {
Harald Welte1cef74d2001-01-05 15:22:59 +00002150 errno = ENOENT;
2151 return 0;
2152 }
Harald Welte0113fe72004-01-06 19:04:02 +00002153
Harald Welteaae69be2004-08-29 23:32:14 +00002154 if (!(r = iptcc_get_rule_num(c, rulenum))) {
Harald Welte0113fe72004-01-06 19:04:02 +00002155 errno = E2BIG;
2156 return 0;
2157 }
2158
Harald Welteaae69be2004-08-29 23:32:14 +00002159 e = r->entry;
2160 r->counter_map.maptype = COUNTER_MAP_SET;
Harald Welte0113fe72004-01-06 19:04:02 +00002161
2162 memcpy(&e->counters, counters, sizeof(STRUCT_COUNTERS));
Harald Welte1cef74d2001-01-05 15:22:59 +00002163
2164 set_changed(*handle);
2165
2166 return 1;
2167}
2168
Marc Bouchere6869a82000-03-20 06:03:29 +00002169/* Creates a new chain. */
2170/* To create a chain, create two rules: error node and unconditional
2171 * return. */
2172int
Rusty Russell79dee072000-05-02 16:45:16 +00002173TC_CREATE_CHAIN(const IPT_CHAINLABEL chain, TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00002174{
Harald Welteaae69be2004-08-29 23:32:14 +00002175 static struct chain_head *c;
Marc Bouchere6869a82000-03-20 06:03:29 +00002176
Rusty Russell79dee072000-05-02 16:45:16 +00002177 iptc_fn = TC_CREATE_CHAIN;
Marc Bouchere6869a82000-03-20 06:03:29 +00002178
2179 /* find_label doesn't cover built-in targets: DROP, ACCEPT,
2180 QUEUE, RETURN. */
Harald Welteaae69be2004-08-29 23:32:14 +00002181 if (iptcc_find_label(chain, *handle)
Rusty Russell79dee072000-05-02 16:45:16 +00002182 || strcmp(chain, LABEL_DROP) == 0
2183 || strcmp(chain, LABEL_ACCEPT) == 0
Rusty Russell67088e72000-05-10 01:18:57 +00002184 || strcmp(chain, LABEL_QUEUE) == 0
Rusty Russell79dee072000-05-02 16:45:16 +00002185 || strcmp(chain, LABEL_RETURN) == 0) {
Harald Welteaae69be2004-08-29 23:32:14 +00002186 DEBUGP("Chain `%s' already exists\n", chain);
Marc Bouchere6869a82000-03-20 06:03:29 +00002187 errno = EEXIST;
2188 return 0;
2189 }
2190
Rusty Russell79dee072000-05-02 16:45:16 +00002191 if (strlen(chain)+1 > sizeof(IPT_CHAINLABEL)) {
Harald Welteaae69be2004-08-29 23:32:14 +00002192 DEBUGP("Chain name `%s' too long\n", chain);
Marc Bouchere6869a82000-03-20 06:03:29 +00002193 errno = EINVAL;
2194 return 0;
2195 }
2196
Harald Welteaae69be2004-08-29 23:32:14 +00002197 c = iptcc_alloc_chain_head(chain, 0);
2198 if (!c) {
2199 DEBUGP("Cannot allocate memory for chain `%s'\n", chain);
2200 errno = ENOMEM;
2201 return 0;
Marc Bouchere6869a82000-03-20 06:03:29 +00002202
Harald Welteaae69be2004-08-29 23:32:14 +00002203 }
Jesper Dangaard Brouer48bde402008-01-15 17:06:48 +00002204 (*handle)->num_chains++; /* New user defined chain */
Marc Bouchere6869a82000-03-20 06:03:29 +00002205
Harald Welteaae69be2004-08-29 23:32:14 +00002206 DEBUGP("Creating chain `%s'\n", chain);
Jesper Dangaard Brouerd8cb7872007-11-28 08:40:26 +00002207 iptc_insert_chain(*handle, c); /* Insert sorted */
Harald Welte0113fe72004-01-06 19:04:02 +00002208
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +00002209 /* Inserting chains don't change the correctness of the chain
2210 * index (except if its smaller than index[0], but that
2211 * handled by iptc_insert_chain). It only causes longer lists
2212 * in the buckets. Thus, only rebuild chain index when the
2213 * capacity is exceed with CHAIN_INDEX_INSERT_MAX chains.
2214 */
2215 int capacity = (*handle)->chain_index_sz * CHAIN_INDEX_BUCKET_LEN;
2216 int exceeded = ((((*handle)->num_chains)-capacity));
2217 if (exceeded > CHAIN_INDEX_INSERT_MAX) {
2218 debug("Capacity(%d) exceeded(%d) rebuild (chains:%d)\n",
2219 capacity, exceeded, (*handle)->num_chains);
2220 iptcc_chain_index_rebuild(*handle);
2221 }
2222
Harald Welte0113fe72004-01-06 19:04:02 +00002223 set_changed(*handle);
2224
Harald Welteaae69be2004-08-29 23:32:14 +00002225 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00002226}
2227
2228/* Get the number of references to this chain. */
2229int
Rusty Russell79dee072000-05-02 16:45:16 +00002230TC_GET_REFERENCES(unsigned int *ref, const IPT_CHAINLABEL chain,
2231 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00002232{
Harald Welteaae69be2004-08-29 23:32:14 +00002233 struct chain_head *c;
Marc Bouchere6869a82000-03-20 06:03:29 +00002234
Derrik Pates664c0a32005-02-01 13:28:14 +00002235 iptc_fn = TC_GET_REFERENCES;
Harald Welteaae69be2004-08-29 23:32:14 +00002236 if (!(c = iptcc_find_label(chain, *handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002237 errno = ENOENT;
2238 return 0;
2239 }
2240
Harald Welteaae69be2004-08-29 23:32:14 +00002241 *ref = c->references;
2242
Marc Bouchere6869a82000-03-20 06:03:29 +00002243 return 1;
2244}
2245
2246/* Deletes a chain. */
2247int
Rusty Russell79dee072000-05-02 16:45:16 +00002248TC_DELETE_CHAIN(const IPT_CHAINLABEL chain, TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00002249{
Marc Bouchere6869a82000-03-20 06:03:29 +00002250 unsigned int references;
Harald Welteaae69be2004-08-29 23:32:14 +00002251 struct chain_head *c;
Rusty Russell7e53bf92000-03-20 07:03:28 +00002252
Rusty Russell79dee072000-05-02 16:45:16 +00002253 iptc_fn = TC_DELETE_CHAIN;
Marc Bouchere6869a82000-03-20 06:03:29 +00002254
Harald Welteaae69be2004-08-29 23:32:14 +00002255 if (!(c = iptcc_find_label(chain, *handle))) {
2256 DEBUGP("cannot find chain `%s'\n", chain);
Marc Bouchere6869a82000-03-20 06:03:29 +00002257 errno = ENOENT;
2258 return 0;
2259 }
2260
Harald Welteaae69be2004-08-29 23:32:14 +00002261 if (TC_BUILTIN(chain, *handle)) {
2262 DEBUGP("cannot remove builtin chain `%s'\n", chain);
2263 errno = EINVAL;
2264 return 0;
2265 }
2266
2267 if (!TC_GET_REFERENCES(&references, chain, handle)) {
2268 DEBUGP("cannot get references on chain `%s'\n", chain);
2269 return 0;
2270 }
2271
2272 if (references > 0) {
2273 DEBUGP("chain `%s' still has references\n", chain);
2274 errno = EMLINK;
2275 return 0;
2276 }
2277
2278 if (c->num_rules) {
2279 DEBUGP("chain `%s' is not empty\n", chain);
Marc Bouchere6869a82000-03-20 06:03:29 +00002280 errno = ENOTEMPTY;
2281 return 0;
2282 }
2283
Harald Welteaae69be2004-08-29 23:32:14 +00002284 /* If we are about to delete the chain that is the current
Jesper Dangaard Brouer48bde402008-01-15 17:06:48 +00002285 * iterator, move chain iterator forward. */
Harald Welteaae69be2004-08-29 23:32:14 +00002286 if (c == (*handle)->chain_iterator_cur)
2287 iptcc_chain_iterator_advance(*handle);
Harald Welte0113fe72004-01-06 19:04:02 +00002288
Jesper Dangaard Brouer48bde402008-01-15 17:06:48 +00002289 (*handle)->num_chains--; /* One user defined chain deleted */
2290
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +00002291 //list_del(&c->list); /* Done in iptcc_chain_index_delete_chain() */
2292 iptcc_chain_index_delete_chain(c, *handle);
2293 free(c);
2294
Harald Welteaae69be2004-08-29 23:32:14 +00002295 DEBUGP("chain `%s' deleted\n", chain);
2296
2297 set_changed(*handle);
2298
2299 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00002300}
2301
2302/* Renames a chain. */
Rusty Russell79dee072000-05-02 16:45:16 +00002303int TC_RENAME_CHAIN(const IPT_CHAINLABEL oldname,
2304 const IPT_CHAINLABEL newname,
2305 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00002306{
Harald Welteaae69be2004-08-29 23:32:14 +00002307 struct chain_head *c;
Rusty Russell79dee072000-05-02 16:45:16 +00002308 iptc_fn = TC_RENAME_CHAIN;
Marc Bouchere6869a82000-03-20 06:03:29 +00002309
Harald Welte1de80462000-10-30 12:00:27 +00002310 /* find_label doesn't cover built-in targets: DROP, ACCEPT,
2311 QUEUE, RETURN. */
Harald Welteaae69be2004-08-29 23:32:14 +00002312 if (iptcc_find_label(newname, *handle)
Rusty Russell79dee072000-05-02 16:45:16 +00002313 || strcmp(newname, LABEL_DROP) == 0
2314 || strcmp(newname, LABEL_ACCEPT) == 0
Harald Welte1de80462000-10-30 12:00:27 +00002315 || strcmp(newname, LABEL_QUEUE) == 0
Rusty Russell79dee072000-05-02 16:45:16 +00002316 || strcmp(newname, LABEL_RETURN) == 0) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002317 errno = EEXIST;
2318 return 0;
2319 }
2320
Harald Welteaae69be2004-08-29 23:32:14 +00002321 if (!(c = iptcc_find_label(oldname, *handle))
Rusty Russell79dee072000-05-02 16:45:16 +00002322 || TC_BUILTIN(oldname, *handle)) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002323 errno = ENOENT;
2324 return 0;
2325 }
2326
Rusty Russell79dee072000-05-02 16:45:16 +00002327 if (strlen(newname)+1 > sizeof(IPT_CHAINLABEL)) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002328 errno = EINVAL;
2329 return 0;
2330 }
2331
Harald Welteaae69be2004-08-29 23:32:14 +00002332 strncpy(c->name, newname, sizeof(IPT_CHAINLABEL));
2333
Harald Welte0113fe72004-01-06 19:04:02 +00002334 set_changed(*handle);
2335
Marc Bouchere6869a82000-03-20 06:03:29 +00002336 return 1;
2337}
2338
2339/* Sets the policy on a built-in chain. */
2340int
Rusty Russell79dee072000-05-02 16:45:16 +00002341TC_SET_POLICY(const IPT_CHAINLABEL chain,
2342 const IPT_CHAINLABEL policy,
Harald Welte1cef74d2001-01-05 15:22:59 +00002343 STRUCT_COUNTERS *counters,
Rusty Russell79dee072000-05-02 16:45:16 +00002344 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00002345{
Harald Welteaae69be2004-08-29 23:32:14 +00002346 struct chain_head *c;
Marc Bouchere6869a82000-03-20 06:03:29 +00002347
Rusty Russell79dee072000-05-02 16:45:16 +00002348 iptc_fn = TC_SET_POLICY;
Marc Bouchere6869a82000-03-20 06:03:29 +00002349
Harald Welteaae69be2004-08-29 23:32:14 +00002350 if (!(c = iptcc_find_label(chain, *handle))) {
2351 DEBUGP("cannot find chain `%s'\n", chain);
2352 errno = ENOENT;
Marc Bouchere6869a82000-03-20 06:03:29 +00002353 return 0;
2354 }
2355
Harald Welteaae69be2004-08-29 23:32:14 +00002356 if (!iptcc_is_builtin(c)) {
2357 DEBUGP("cannot set policy of userdefinedchain `%s'\n", chain);
2358 errno = ENOENT;
2359 return 0;
2360 }
Marc Bouchere6869a82000-03-20 06:03:29 +00002361
Rusty Russell79dee072000-05-02 16:45:16 +00002362 if (strcmp(policy, LABEL_ACCEPT) == 0)
Harald Welteaae69be2004-08-29 23:32:14 +00002363 c->verdict = -NF_ACCEPT - 1;
Rusty Russell79dee072000-05-02 16:45:16 +00002364 else if (strcmp(policy, LABEL_DROP) == 0)
Harald Welteaae69be2004-08-29 23:32:14 +00002365 c->verdict = -NF_DROP - 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00002366 else {
2367 errno = EINVAL;
2368 return 0;
2369 }
Harald Welte1cef74d2001-01-05 15:22:59 +00002370
Harald Welte1cef74d2001-01-05 15:22:59 +00002371 if (counters) {
2372 /* set byte and packet counters */
Harald Welteaae69be2004-08-29 23:32:14 +00002373 memcpy(&c->counters, counters, sizeof(STRUCT_COUNTERS));
2374 c->counter_map.maptype = COUNTER_MAP_SET;
Harald Welte1cef74d2001-01-05 15:22:59 +00002375 } else {
Harald Welteaae69be2004-08-29 23:32:14 +00002376 c->counter_map.maptype = COUNTER_MAP_NOMAP;
Harald Welte1cef74d2001-01-05 15:22:59 +00002377 }
2378
Rusty Russell175f6412000-03-24 09:32:20 +00002379 set_changed(*handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00002380
Marc Bouchere6869a82000-03-20 06:03:29 +00002381 return 1;
2382}
2383
2384/* Without this, on gcc 2.7.2.3, we get:
Rusty Russell79dee072000-05-02 16:45:16 +00002385 libiptc.c: In function `TC_COMMIT':
Marc Bouchere6869a82000-03-20 06:03:29 +00002386 libiptc.c:833: fixed or forbidden register was spilled.
2387 This may be due to a compiler bug or to impossible asm
2388 statements or clauses.
2389*/
2390static void
Rusty Russell79dee072000-05-02 16:45:16 +00002391subtract_counters(STRUCT_COUNTERS *answer,
2392 const STRUCT_COUNTERS *a,
2393 const STRUCT_COUNTERS *b)
Marc Bouchere6869a82000-03-20 06:03:29 +00002394{
2395 answer->pcnt = a->pcnt - b->pcnt;
2396 answer->bcnt = a->bcnt - b->bcnt;
2397}
2398
Harald Welteaae69be2004-08-29 23:32:14 +00002399
Jan Engelhardtdbb77542008-02-11 00:33:30 +01002400static void counters_nomap(STRUCT_COUNTERS_INFO *newcounters, unsigned int idx)
Harald Welteaae69be2004-08-29 23:32:14 +00002401{
Jan Engelhardtdbb77542008-02-11 00:33:30 +01002402 newcounters->counters[idx] = ((STRUCT_COUNTERS) { 0, 0});
Harald Welteaae69be2004-08-29 23:32:14 +00002403 DEBUGP_C("NOMAP => zero\n");
2404}
2405
2406static void counters_normal_map(STRUCT_COUNTERS_INFO *newcounters,
Jan Engelhardtdbb77542008-02-11 00:33:30 +01002407 STRUCT_REPLACE *repl, unsigned int idx,
Harald Welteaae69be2004-08-29 23:32:14 +00002408 unsigned int mappos)
2409{
2410 /* Original read: X.
2411 * Atomic read on replacement: X + Y.
2412 * Currently in kernel: Z.
2413 * Want in kernel: X + Y + Z.
2414 * => Add in X + Y
2415 * => Add in replacement read.
2416 */
Jan Engelhardtdbb77542008-02-11 00:33:30 +01002417 newcounters->counters[idx] = repl->counters[mappos];
Harald Welteaae69be2004-08-29 23:32:14 +00002418 DEBUGP_C("NORMAL_MAP => mappos %u \n", mappos);
2419}
2420
2421static void counters_map_zeroed(STRUCT_COUNTERS_INFO *newcounters,
Jan Engelhardtdbb77542008-02-11 00:33:30 +01002422 STRUCT_REPLACE *repl, unsigned int idx,
2423 unsigned int mappos, STRUCT_COUNTERS *counters)
Harald Welteaae69be2004-08-29 23:32:14 +00002424{
2425 /* Original read: X.
2426 * Atomic read on replacement: X + Y.
2427 * Currently in kernel: Z.
2428 * Want in kernel: Y + Z.
2429 * => Add in Y.
2430 * => Add in (replacement read - original read).
2431 */
Jan Engelhardtdbb77542008-02-11 00:33:30 +01002432 subtract_counters(&newcounters->counters[idx],
Harald Welteaae69be2004-08-29 23:32:14 +00002433 &repl->counters[mappos],
2434 counters);
2435 DEBUGP_C("ZEROED => mappos %u\n", mappos);
2436}
2437
2438static void counters_map_set(STRUCT_COUNTERS_INFO *newcounters,
Jan Engelhardtdbb77542008-02-11 00:33:30 +01002439 unsigned int idx, STRUCT_COUNTERS *counters)
Harald Welteaae69be2004-08-29 23:32:14 +00002440{
2441 /* Want to set counter (iptables-restore) */
2442
Jan Engelhardtdbb77542008-02-11 00:33:30 +01002443 memcpy(&newcounters->counters[idx], counters,
Harald Welteaae69be2004-08-29 23:32:14 +00002444 sizeof(STRUCT_COUNTERS));
2445
2446 DEBUGP_C("SET\n");
2447}
2448
2449
Marc Bouchere6869a82000-03-20 06:03:29 +00002450int
Rusty Russell79dee072000-05-02 16:45:16 +00002451TC_COMMIT(TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00002452{
2453 /* Replace, then map back the counters. */
Rusty Russell79dee072000-05-02 16:45:16 +00002454 STRUCT_REPLACE *repl;
2455 STRUCT_COUNTERS_INFO *newcounters;
Harald Welteaae69be2004-08-29 23:32:14 +00002456 struct chain_head *c;
2457 int ret;
Martin Josefsson841e4ae2003-05-02 15:30:11 +00002458 size_t counterlen;
Harald Welteaae69be2004-08-29 23:32:14 +00002459 int new_number;
2460 unsigned int new_size;
Marc Bouchere6869a82000-03-20 06:03:29 +00002461
Derrik Pates664c0a32005-02-01 13:28:14 +00002462 iptc_fn = TC_COMMIT;
Marc Bouchere6869a82000-03-20 06:03:29 +00002463 CHECK(*handle);
Martin Josefsson841e4ae2003-05-02 15:30:11 +00002464
Marc Bouchere6869a82000-03-20 06:03:29 +00002465 /* Don't commit if nothing changed. */
2466 if (!(*handle)->changed)
2467 goto finished;
2468
Harald Welteaae69be2004-08-29 23:32:14 +00002469 new_number = iptcc_compile_table_prep(*handle, &new_size);
2470 if (new_number < 0) {
2471 errno = ENOMEM;
Harald Welted6ba6f52005-11-12 10:39:40 +00002472 goto out_zero;
Harald Welteaae69be2004-08-29 23:32:14 +00002473 }
2474
2475 repl = malloc(sizeof(*repl) + new_size);
Marc Bouchere6869a82000-03-20 06:03:29 +00002476 if (!repl) {
2477 errno = ENOMEM;
Harald Welted6ba6f52005-11-12 10:39:40 +00002478 goto out_zero;
Marc Bouchere6869a82000-03-20 06:03:29 +00002479 }
Martin Josefssonad3b4f92004-09-22 22:04:07 +00002480 memset(repl, 0, sizeof(*repl) + new_size);
Harald Welteaae69be2004-08-29 23:32:14 +00002481
Rusty Russelle45c7132004-12-16 13:21:44 +00002482#if 0
2483 TC_DUMP_ENTRIES(*handle);
2484#endif
2485
Harald Welteaae69be2004-08-29 23:32:14 +00002486 counterlen = sizeof(STRUCT_COUNTERS_INFO)
2487 + sizeof(STRUCT_COUNTERS) * new_number;
Marc Bouchere6869a82000-03-20 06:03:29 +00002488
2489 /* These are the old counters we will get from kernel */
Rusty Russell79dee072000-05-02 16:45:16 +00002490 repl->counters = malloc(sizeof(STRUCT_COUNTERS)
Marc Bouchere6869a82000-03-20 06:03:29 +00002491 * (*handle)->info.num_entries);
2492 if (!repl->counters) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002493 errno = ENOMEM;
Harald Welted6ba6f52005-11-12 10:39:40 +00002494 goto out_free_repl;
Marc Bouchere6869a82000-03-20 06:03:29 +00002495 }
Marc Bouchere6869a82000-03-20 06:03:29 +00002496 /* These are the counters we're going to put back, later. */
2497 newcounters = malloc(counterlen);
2498 if (!newcounters) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002499 errno = ENOMEM;
Harald Welted6ba6f52005-11-12 10:39:40 +00002500 goto out_free_repl_counters;
Marc Bouchere6869a82000-03-20 06:03:29 +00002501 }
Harald Welteaae69be2004-08-29 23:32:14 +00002502 memset(newcounters, 0, counterlen);
Marc Bouchere6869a82000-03-20 06:03:29 +00002503
2504 strcpy(repl->name, (*handle)->info.name);
Harald Welteaae69be2004-08-29 23:32:14 +00002505 repl->num_entries = new_number;
2506 repl->size = new_size;
2507
Marc Bouchere6869a82000-03-20 06:03:29 +00002508 repl->num_counters = (*handle)->info.num_entries;
2509 repl->valid_hooks = (*handle)->info.valid_hooks;
Harald Welteaae69be2004-08-29 23:32:14 +00002510
2511 DEBUGP("num_entries=%u, size=%u, num_counters=%u\n",
2512 repl->num_entries, repl->size, repl->num_counters);
2513
2514 ret = iptcc_compile_table(*handle, repl);
2515 if (ret < 0) {
2516 errno = ret;
Harald Welted6ba6f52005-11-12 10:39:40 +00002517 goto out_free_newcounters;
Harald Welteaae69be2004-08-29 23:32:14 +00002518 }
2519
2520
2521#ifdef IPTC_DEBUG2
2522 {
2523 int fd = open("/tmp/libiptc-so_set_replace.blob",
2524 O_CREAT|O_WRONLY);
2525 if (fd >= 0) {
2526 write(fd, repl, sizeof(*repl) + repl->size);
2527 close(fd);
2528 }
2529 }
2530#endif
Marc Bouchere6869a82000-03-20 06:03:29 +00002531
Harald Welted6ba6f52005-11-12 10:39:40 +00002532 ret = setsockopt(sockfd, TC_IPPROTO, SO_SET_REPLACE, repl,
2533 sizeof(*repl) + repl->size);
Patrick McHardye0865ad2006-04-22 02:08:56 +00002534 if (ret < 0)
Harald Welted6ba6f52005-11-12 10:39:40 +00002535 goto out_free_newcounters;
Marc Bouchere6869a82000-03-20 06:03:29 +00002536
2537 /* Put counters back. */
2538 strcpy(newcounters->name, (*handle)->info.name);
Harald Welteaae69be2004-08-29 23:32:14 +00002539 newcounters->num_counters = new_number;
Marc Bouchere6869a82000-03-20 06:03:29 +00002540
Harald Welteaae69be2004-08-29 23:32:14 +00002541 list_for_each_entry(c, &(*handle)->chains, list) {
2542 struct rule_head *r;
Marc Bouchere6869a82000-03-20 06:03:29 +00002543
Harald Welteaae69be2004-08-29 23:32:14 +00002544 /* Builtin chains have their own counters */
2545 if (iptcc_is_builtin(c)) {
2546 DEBUGP("counter for chain-index %u: ", c->foot_index);
2547 switch(c->counter_map.maptype) {
2548 case COUNTER_MAP_NOMAP:
2549 counters_nomap(newcounters, c->foot_index);
2550 break;
2551 case COUNTER_MAP_NORMAL_MAP:
2552 counters_normal_map(newcounters, repl,
2553 c->foot_index,
2554 c->counter_map.mappos);
2555 break;
2556 case COUNTER_MAP_ZEROED:
2557 counters_map_zeroed(newcounters, repl,
2558 c->foot_index,
2559 c->counter_map.mappos,
2560 &c->counters);
2561 break;
2562 case COUNTER_MAP_SET:
2563 counters_map_set(newcounters, c->foot_index,
2564 &c->counters);
2565 break;
2566 }
2567 }
Harald Welte1cef74d2001-01-05 15:22:59 +00002568
Harald Welteaae69be2004-08-29 23:32:14 +00002569 list_for_each_entry(r, &c->rules, list) {
2570 DEBUGP("counter for index %u: ", r->index);
2571 switch (r->counter_map.maptype) {
2572 case COUNTER_MAP_NOMAP:
2573 counters_nomap(newcounters, r->index);
2574 break;
Harald Welte1cef74d2001-01-05 15:22:59 +00002575
Harald Welteaae69be2004-08-29 23:32:14 +00002576 case COUNTER_MAP_NORMAL_MAP:
2577 counters_normal_map(newcounters, repl,
2578 r->index,
2579 r->counter_map.mappos);
2580 break;
Harald Welte1cef74d2001-01-05 15:22:59 +00002581
Harald Welteaae69be2004-08-29 23:32:14 +00002582 case COUNTER_MAP_ZEROED:
2583 counters_map_zeroed(newcounters, repl,
2584 r->index,
2585 r->counter_map.mappos,
2586 &r->entry->counters);
2587 break;
2588
2589 case COUNTER_MAP_SET:
2590 counters_map_set(newcounters, r->index,
2591 &r->entry->counters);
2592 break;
2593 }
Marc Bouchere6869a82000-03-20 06:03:29 +00002594 }
2595 }
Rusty Russell62527ce2000-09-04 09:45:54 +00002596
Harald Welteaae69be2004-08-29 23:32:14 +00002597#ifdef IPTC_DEBUG2
2598 {
2599 int fd = open("/tmp/libiptc-so_set_add_counters.blob",
2600 O_CREAT|O_WRONLY);
2601 if (fd >= 0) {
2602 write(fd, newcounters, counterlen);
2603 close(fd);
2604 }
2605 }
2606#endif
2607
Harald Welted6ba6f52005-11-12 10:39:40 +00002608 ret = setsockopt(sockfd, TC_IPPROTO, SO_SET_ADD_COUNTERS,
2609 newcounters, counterlen);
Patrick McHardye0865ad2006-04-22 02:08:56 +00002610 if (ret < 0)
Harald Welted6ba6f52005-11-12 10:39:40 +00002611 goto out_free_newcounters;
Marc Bouchere6869a82000-03-20 06:03:29 +00002612
2613 free(repl->counters);
2614 free(repl);
2615 free(newcounters);
2616
Harald Welted6ba6f52005-11-12 10:39:40 +00002617finished:
Martin Josefsson841e4ae2003-05-02 15:30:11 +00002618 TC_FREE(handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00002619 return 1;
Harald Welted6ba6f52005-11-12 10:39:40 +00002620
2621out_free_newcounters:
2622 free(newcounters);
2623out_free_repl_counters:
2624 free(repl->counters);
2625out_free_repl:
2626 free(repl);
2627out_zero:
2628 return 0;
Marc Bouchere6869a82000-03-20 06:03:29 +00002629}
2630
2631/* Get raw socket. */
2632int
Patrick McHardy0b639362007-09-08 16:00:01 +00002633TC_GET_RAW_SOCKET(void)
Marc Bouchere6869a82000-03-20 06:03:29 +00002634{
2635 return sockfd;
2636}
2637
2638/* Translates errno numbers into more human-readable form than strerror. */
2639const char *
Rusty Russell79dee072000-05-02 16:45:16 +00002640TC_STRERROR(int err)
Marc Bouchere6869a82000-03-20 06:03:29 +00002641{
2642 unsigned int i;
2643 struct table_struct {
2644 void *fn;
2645 int err;
2646 const char *message;
2647 } table [] =
Harald Welte4ccfa632001-07-30 15:12:43 +00002648 { { TC_INIT, EPERM, "Permission denied (you must be root)" },
Rusty Russell79dee072000-05-02 16:45:16 +00002649 { TC_INIT, EINVAL, "Module is wrong version" },
Harald Welte4ccfa632001-07-30 15:12:43 +00002650 { TC_INIT, ENOENT,
2651 "Table does not exist (do you need to insmod?)" },
Rusty Russell79dee072000-05-02 16:45:16 +00002652 { TC_DELETE_CHAIN, ENOTEMPTY, "Chain is not empty" },
2653 { TC_DELETE_CHAIN, EINVAL, "Can't delete built-in chain" },
2654 { TC_DELETE_CHAIN, EMLINK,
Marc Bouchere6869a82000-03-20 06:03:29 +00002655 "Can't delete chain with references left" },
Rusty Russell79dee072000-05-02 16:45:16 +00002656 { TC_CREATE_CHAIN, EEXIST, "Chain already exists" },
2657 { TC_INSERT_ENTRY, E2BIG, "Index of insertion too big" },
2658 { TC_REPLACE_ENTRY, E2BIG, "Index of replacement too big" },
2659 { TC_DELETE_NUM_ENTRY, E2BIG, "Index of deletion too big" },
Harald Welte1cef74d2001-01-05 15:22:59 +00002660 { TC_READ_COUNTER, E2BIG, "Index of counter too big" },
2661 { TC_ZERO_COUNTER, E2BIG, "Index of counter too big" },
Rusty Russell79dee072000-05-02 16:45:16 +00002662 { TC_INSERT_ENTRY, ELOOP, "Loop found in table" },
2663 { TC_INSERT_ENTRY, EINVAL, "Target problem" },
Marc Bouchere6869a82000-03-20 06:03:29 +00002664 /* EINVAL for CHECK probably means bad interface. */
Rusty Russell79dee072000-05-02 16:45:16 +00002665 { TC_CHECK_PACKET, EINVAL,
Marc Boucherc8264992000-04-22 22:34:44 +00002666 "Bad arguments (does that interface exist?)" },
Harald Welte4ccfa632001-07-30 15:12:43 +00002667 { TC_CHECK_PACKET, ENOSYS,
2668 "Checking will most likely never get implemented" },
Marc Bouchere6869a82000-03-20 06:03:29 +00002669 /* ENOENT for DELETE probably means no matching rule */
Rusty Russell79dee072000-05-02 16:45:16 +00002670 { TC_DELETE_ENTRY, ENOENT,
Marc Boucherc8264992000-04-22 22:34:44 +00002671 "Bad rule (does a matching rule exist in that chain?)" },
Rusty Russell79dee072000-05-02 16:45:16 +00002672 { TC_SET_POLICY, ENOENT,
Marc Boucherc8264992000-04-22 22:34:44 +00002673 "Bad built-in chain name" },
Rusty Russell79dee072000-05-02 16:45:16 +00002674 { TC_SET_POLICY, EINVAL,
Marc Boucherc8264992000-04-22 22:34:44 +00002675 "Bad policy name" },
Harald Welte4ccfa632001-07-30 15:12:43 +00002676
2677 { NULL, 0, "Incompatible with this kernel" },
2678 { NULL, ENOPROTOOPT, "iptables who? (do you need to insmod?)" },
2679 { NULL, ENOSYS, "Will be implemented real soon. I promise ;)" },
2680 { NULL, ENOMEM, "Memory allocation problem" },
2681 { NULL, ENOENT, "No chain/target/match by that name" },
Marc Bouchere6869a82000-03-20 06:03:29 +00002682 };
2683
2684 for (i = 0; i < sizeof(table)/sizeof(struct table_struct); i++) {
2685 if ((!table[i].fn || table[i].fn == iptc_fn)
2686 && table[i].err == err)
2687 return table[i].message;
2688 }
2689
2690 return strerror(err);
2691}