blob: b7bf785c221f6b7f3f714db660c996f2683ba39a [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 *
320iptcc_bsearch_chain_index(const char *name, unsigned int *index, TC_HANDLE_T handle)
321{
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;
349 (*index)=pos;
350
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;
539 unsigned int index, index2;
540
541 index_ptr = iptcc_bsearch_chain_index(c->name, &index, h);
542
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);
557 index_ptr2 = iptcc_bsearch_chain_index(c2->name, &index2, h);
558 if (index != index2) {
559 /* 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",
564 index, c2->name);
565 h->chain_index[index]=c2;
566 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) {
965 struct chain_head *c;
966 STRUCT_STANDARD_TARGET *t;
967
968 if (r->type != IPTCC_R_JUMP)
969 continue;
970
971 t = (STRUCT_STANDARD_TARGET *)GET_TARGET(r->entry);
972 c = iptcc_find_chain_by_offset(h, t->verdict);
973 if (!c)
974 return -1;
975 r->jump = c;
976 c->references++;
977 }
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++;
Marc Bouchere6869a82000-03-20 06:03:29 +00001229
1230 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);
1282 return NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +00001283}
1284
Martin Josefsson841e4ae2003-05-02 15:30:11 +00001285void
1286TC_FREE(TC_HANDLE_T *h)
1287{
Harald Welteaae69be2004-08-29 23:32:14 +00001288 struct chain_head *c, *tmp;
1289
Derrik Pates664c0a32005-02-01 13:28:14 +00001290 iptc_fn = TC_FREE;
1291 if (--sockfd_use == 0) {
1292 close(sockfd);
1293 sockfd = -1;
1294 }
Harald Welteaae69be2004-08-29 23:32:14 +00001295
1296 list_for_each_entry_safe(c, tmp, &(*h)->chains, list) {
1297 struct rule_head *r, *rtmp;
1298
1299 list_for_each_entry_safe(r, rtmp, &c->rules, list) {
1300 free(r);
1301 }
1302
1303 free(c);
1304 }
1305
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +00001306 iptcc_chain_index_free(*h);
1307
Harald Welteaae69be2004-08-29 23:32:14 +00001308 free((*h)->entries);
Martin Josefsson841e4ae2003-05-02 15:30:11 +00001309 free(*h);
Harald Welteaae69be2004-08-29 23:32:14 +00001310
Martin Josefsson841e4ae2003-05-02 15:30:11 +00001311 *h = NULL;
1312}
1313
Marc Bouchere6869a82000-03-20 06:03:29 +00001314static inline int
Rusty Russell79dee072000-05-02 16:45:16 +00001315print_match(const STRUCT_ENTRY_MATCH *m)
Marc Bouchere6869a82000-03-20 06:03:29 +00001316{
Rusty Russell228e98d2000-04-27 10:28:06 +00001317 printf("Match name: `%s'\n", m->u.user.name);
Marc Bouchere6869a82000-03-20 06:03:29 +00001318 return 0;
1319}
1320
Rusty Russell79dee072000-05-02 16:45:16 +00001321static int dump_entry(STRUCT_ENTRY *e, const TC_HANDLE_T handle);
1322
Marc Bouchere6869a82000-03-20 06:03:29 +00001323void
Rusty Russell79dee072000-05-02 16:45:16 +00001324TC_DUMP_ENTRIES(const TC_HANDLE_T handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001325{
Derrik Pates664c0a32005-02-01 13:28:14 +00001326 iptc_fn = TC_DUMP_ENTRIES;
Marc Bouchere6869a82000-03-20 06:03:29 +00001327 CHECK(handle);
Patrick McHardy97fb2f12007-09-08 16:52:25 +00001328
Rusty Russelle45c7132004-12-16 13:21:44 +00001329 printf("libiptc v%s. %u bytes.\n",
1330 IPTABLES_VERSION, handle->entries->size);
Marc Bouchere6869a82000-03-20 06:03:29 +00001331 printf("Table `%s'\n", handle->info.name);
1332 printf("Hooks: pre/in/fwd/out/post = %u/%u/%u/%u/%u\n",
Rusty Russell67088e72000-05-10 01:18:57 +00001333 handle->info.hook_entry[HOOK_PRE_ROUTING],
1334 handle->info.hook_entry[HOOK_LOCAL_IN],
1335 handle->info.hook_entry[HOOK_FORWARD],
1336 handle->info.hook_entry[HOOK_LOCAL_OUT],
1337 handle->info.hook_entry[HOOK_POST_ROUTING]);
Marc Bouchere6869a82000-03-20 06:03:29 +00001338 printf("Underflows: pre/in/fwd/out/post = %u/%u/%u/%u/%u\n",
Rusty Russell67088e72000-05-10 01:18:57 +00001339 handle->info.underflow[HOOK_PRE_ROUTING],
1340 handle->info.underflow[HOOK_LOCAL_IN],
1341 handle->info.underflow[HOOK_FORWARD],
1342 handle->info.underflow[HOOK_LOCAL_OUT],
1343 handle->info.underflow[HOOK_POST_ROUTING]);
Marc Bouchere6869a82000-03-20 06:03:29 +00001344
Harald Welteaae69be2004-08-29 23:32:14 +00001345 ENTRY_ITERATE(handle->entries->entrytable, handle->entries->size,
Rusty Russell79dee072000-05-02 16:45:16 +00001346 dump_entry, handle);
Harald Welte0113fe72004-01-06 19:04:02 +00001347}
Rusty Russell30fd6e52000-04-23 09:16:06 +00001348
Marc Bouchere6869a82000-03-20 06:03:29 +00001349/* Does this chain exist? */
Rusty Russell79dee072000-05-02 16:45:16 +00001350int TC_IS_CHAIN(const char *chain, const TC_HANDLE_T handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001351{
Derrik Pates664c0a32005-02-01 13:28:14 +00001352 iptc_fn = TC_IS_CHAIN;
Harald Welteaae69be2004-08-29 23:32:14 +00001353 return iptcc_find_label(chain, handle) != NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +00001354}
1355
Harald Welteaae69be2004-08-29 23:32:14 +00001356static void iptcc_chain_iterator_advance(TC_HANDLE_T handle)
1357{
1358 struct chain_head *c = handle->chain_iterator_cur;
1359
1360 if (c->list.next == &handle->chains)
1361 handle->chain_iterator_cur = NULL;
1362 else
1363 handle->chain_iterator_cur =
1364 list_entry(c->list.next, struct chain_head, list);
1365}
Marc Bouchere6869a82000-03-20 06:03:29 +00001366
Rusty Russell30fd6e52000-04-23 09:16:06 +00001367/* Iterator functions to run through the chains. */
Marc Bouchere6869a82000-03-20 06:03:29 +00001368const char *
Philip Blundell8c700902000-05-15 02:17:52 +00001369TC_FIRST_CHAIN(TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001370{
Harald Welteaae69be2004-08-29 23:32:14 +00001371 struct chain_head *c = list_entry((*handle)->chains.next,
1372 struct chain_head, list);
1373
1374 iptc_fn = TC_FIRST_CHAIN;
1375
1376
1377 if (list_empty(&(*handle)->chains)) {
1378 DEBUGP(": no chains\n");
Harald Welte0113fe72004-01-06 19:04:02 +00001379 return NULL;
Harald Welteaae69be2004-08-29 23:32:14 +00001380 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001381
Harald Welteaae69be2004-08-29 23:32:14 +00001382 (*handle)->chain_iterator_cur = c;
1383 iptcc_chain_iterator_advance(*handle);
Harald Welte0113fe72004-01-06 19:04:02 +00001384
Harald Welteaae69be2004-08-29 23:32:14 +00001385 DEBUGP(": returning `%s'\n", c->name);
1386 return c->name;
Marc Bouchere6869a82000-03-20 06:03:29 +00001387}
1388
Rusty Russell30fd6e52000-04-23 09:16:06 +00001389/* Iterator functions to run through the chains. Returns NULL at end. */
1390const char *
Rusty Russell79dee072000-05-02 16:45:16 +00001391TC_NEXT_CHAIN(TC_HANDLE_T *handle)
Rusty Russell30fd6e52000-04-23 09:16:06 +00001392{
Harald Welteaae69be2004-08-29 23:32:14 +00001393 struct chain_head *c = (*handle)->chain_iterator_cur;
Rusty Russell30fd6e52000-04-23 09:16:06 +00001394
Harald Welteaae69be2004-08-29 23:32:14 +00001395 iptc_fn = TC_NEXT_CHAIN;
1396
1397 if (!c) {
1398 DEBUGP(": no more chains\n");
Rusty Russell30fd6e52000-04-23 09:16:06 +00001399 return NULL;
Harald Welteaae69be2004-08-29 23:32:14 +00001400 }
Rusty Russell30fd6e52000-04-23 09:16:06 +00001401
Harald Welteaae69be2004-08-29 23:32:14 +00001402 iptcc_chain_iterator_advance(*handle);
1403
1404 DEBUGP(": returning `%s'\n", c->name);
1405 return c->name;
Rusty Russell30fd6e52000-04-23 09:16:06 +00001406}
1407
1408/* Get first rule in the given chain: NULL for empty chain. */
Rusty Russell79dee072000-05-02 16:45:16 +00001409const STRUCT_ENTRY *
Philip Blundell8c700902000-05-15 02:17:52 +00001410TC_FIRST_RULE(const char *chain, TC_HANDLE_T *handle)
Rusty Russell30fd6e52000-04-23 09:16:06 +00001411{
Harald Welteaae69be2004-08-29 23:32:14 +00001412 struct chain_head *c;
1413 struct rule_head *r;
Rusty Russell30fd6e52000-04-23 09:16:06 +00001414
Harald Welteaae69be2004-08-29 23:32:14 +00001415 iptc_fn = TC_FIRST_RULE;
1416
1417 DEBUGP("first rule(%s): ", chain);
1418
1419 c = iptcc_find_label(chain, *handle);
Rusty Russell30fd6e52000-04-23 09:16:06 +00001420 if (!c) {
1421 errno = ENOENT;
1422 return NULL;
1423 }
1424
1425 /* Empty chain: single return/policy rule */
Harald Welteaae69be2004-08-29 23:32:14 +00001426 if (list_empty(&c->rules)) {
1427 DEBUGP_C("no rules, returning NULL\n");
Rusty Russell30fd6e52000-04-23 09:16:06 +00001428 return NULL;
Harald Welteaae69be2004-08-29 23:32:14 +00001429 }
Rusty Russell30fd6e52000-04-23 09:16:06 +00001430
Harald Welteaae69be2004-08-29 23:32:14 +00001431 r = list_entry(c->rules.next, struct rule_head, list);
1432 (*handle)->rule_iterator_cur = r;
1433 DEBUGP_C("%p\n", r);
1434
1435 return r->entry;
Rusty Russell30fd6e52000-04-23 09:16:06 +00001436}
1437
1438/* Returns NULL when rules run out. */
Rusty Russell79dee072000-05-02 16:45:16 +00001439const STRUCT_ENTRY *
Philip Blundell8c700902000-05-15 02:17:52 +00001440TC_NEXT_RULE(const STRUCT_ENTRY *prev, TC_HANDLE_T *handle)
Rusty Russell30fd6e52000-04-23 09:16:06 +00001441{
Harald Welteaae69be2004-08-29 23:32:14 +00001442 struct rule_head *r;
Rusty Russell30fd6e52000-04-23 09:16:06 +00001443
Derrik Pates664c0a32005-02-01 13:28:14 +00001444 iptc_fn = TC_NEXT_RULE;
Harald Welteaae69be2004-08-29 23:32:14 +00001445 DEBUGP("rule_iterator_cur=%p...", (*handle)->rule_iterator_cur);
1446
1447 if (!(*handle)->rule_iterator_cur) {
1448 DEBUGP_C("returning NULL\n");
1449 return NULL;
1450 }
1451
1452 r = list_entry((*handle)->rule_iterator_cur->list.next,
1453 struct rule_head, list);
1454
1455 iptc_fn = TC_NEXT_RULE;
1456
1457 DEBUGP_C("next=%p, head=%p...", &r->list,
1458 &(*handle)->rule_iterator_cur->chain->rules);
1459
1460 if (&r->list == &(*handle)->rule_iterator_cur->chain->rules) {
1461 (*handle)->rule_iterator_cur = NULL;
1462 DEBUGP_C("finished, returning NULL\n");
1463 return NULL;
1464 }
1465
1466 (*handle)->rule_iterator_cur = r;
1467
1468 /* NOTE: prev is without any influence ! */
1469 DEBUGP_C("returning rule %p\n", r);
1470 return r->entry;
Rusty Russell30fd6e52000-04-23 09:16:06 +00001471}
1472
Marc Bouchere6869a82000-03-20 06:03:29 +00001473/* How many rules in this chain? */
1474unsigned int
Rusty Russell79dee072000-05-02 16:45:16 +00001475TC_NUM_RULES(const char *chain, TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001476{
Harald Welteaae69be2004-08-29 23:32:14 +00001477 struct chain_head *c;
1478 iptc_fn = TC_NUM_RULES;
Marc Bouchere6869a82000-03-20 06:03:29 +00001479 CHECK(*handle);
Harald Welteaae69be2004-08-29 23:32:14 +00001480
1481 c = iptcc_find_label(chain, *handle);
1482 if (!c) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001483 errno = ENOENT;
1484 return (unsigned int)-1;
1485 }
Harald Welteaae69be2004-08-29 23:32:14 +00001486
1487 return c->num_rules;
Marc Bouchere6869a82000-03-20 06:03:29 +00001488}
1489
Rusty Russell79dee072000-05-02 16:45:16 +00001490const STRUCT_ENTRY *TC_GET_RULE(const char *chain,
1491 unsigned int n,
1492 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001493{
Harald Welteaae69be2004-08-29 23:32:14 +00001494 struct chain_head *c;
1495 struct rule_head *r;
1496
1497 iptc_fn = TC_GET_RULE;
Marc Bouchere6869a82000-03-20 06:03:29 +00001498
1499 CHECK(*handle);
Harald Welteaae69be2004-08-29 23:32:14 +00001500
1501 c = iptcc_find_label(chain, *handle);
1502 if (!c) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001503 errno = ENOENT;
1504 return NULL;
1505 }
1506
Harald Welteaae69be2004-08-29 23:32:14 +00001507 r = iptcc_get_rule_num(c, n);
1508 if (!r)
1509 return NULL;
1510 return r->entry;
Marc Bouchere6869a82000-03-20 06:03:29 +00001511}
1512
1513/* Returns a pointer to the target name of this position. */
Harald Welteaae69be2004-08-29 23:32:14 +00001514const char *standard_target_map(int verdict)
Marc Bouchere6869a82000-03-20 06:03:29 +00001515{
Harald Welteaae69be2004-08-29 23:32:14 +00001516 switch (verdict) {
1517 case RETURN:
1518 return LABEL_RETURN;
1519 break;
1520 case -NF_ACCEPT-1:
1521 return LABEL_ACCEPT;
1522 break;
1523 case -NF_DROP-1:
1524 return LABEL_DROP;
1525 break;
1526 case -NF_QUEUE-1:
1527 return LABEL_QUEUE;
1528 break;
1529 default:
1530 fprintf(stderr, "ERROR: %d not a valid target)\n",
1531 verdict);
1532 abort();
1533 break;
1534 }
1535 /* not reached */
1536 return NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +00001537}
1538
Harald Welteaae69be2004-08-29 23:32:14 +00001539/* Returns a pointer to the target name of this position. */
1540const char *TC_GET_TARGET(const STRUCT_ENTRY *ce,
1541 TC_HANDLE_T *handle)
1542{
1543 STRUCT_ENTRY *e = (STRUCT_ENTRY *)ce;
Rusty Russelle45c7132004-12-16 13:21:44 +00001544 struct rule_head *r = container_of(e, struct rule_head, entry[0]);
Harald Welteaae69be2004-08-29 23:32:14 +00001545
1546 iptc_fn = TC_GET_TARGET;
1547
1548 switch(r->type) {
1549 int spos;
1550 case IPTCC_R_FALLTHROUGH:
1551 return "";
1552 break;
1553 case IPTCC_R_JUMP:
1554 DEBUGP("r=%p, jump=%p, name=`%s'\n", r, r->jump, r->jump->name);
1555 return r->jump->name;
1556 break;
1557 case IPTCC_R_STANDARD:
1558 spos = *(int *)GET_TARGET(e)->data;
1559 DEBUGP("r=%p, spos=%d'\n", r, spos);
1560 return standard_target_map(spos);
1561 break;
1562 case IPTCC_R_MODULE:
1563 return GET_TARGET(e)->u.user.name;
1564 break;
1565 }
1566 return NULL;
1567}
Marc Bouchere6869a82000-03-20 06:03:29 +00001568/* Is this a built-in chain? Actually returns hook + 1. */
1569int
Rusty Russell79dee072000-05-02 16:45:16 +00001570TC_BUILTIN(const char *chain, const TC_HANDLE_T handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001571{
Harald Welteaae69be2004-08-29 23:32:14 +00001572 struct chain_head *c;
1573
1574 iptc_fn = TC_BUILTIN;
Marc Bouchere6869a82000-03-20 06:03:29 +00001575
Harald Welteaae69be2004-08-29 23:32:14 +00001576 c = iptcc_find_label(chain, handle);
1577 if (!c) {
1578 errno = ENOENT;
Martin Josefssonb0f3d2d2004-09-23 18:23:20 +00001579 return 0;
Marc Bouchere6869a82000-03-20 06:03:29 +00001580 }
Harald Welteaae69be2004-08-29 23:32:14 +00001581
1582 return iptcc_is_builtin(c);
Marc Bouchere6869a82000-03-20 06:03:29 +00001583}
1584
1585/* Get the policy of a given built-in chain */
1586const char *
Rusty Russell79dee072000-05-02 16:45:16 +00001587TC_GET_POLICY(const char *chain,
1588 STRUCT_COUNTERS *counters,
1589 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001590{
Harald Welteaae69be2004-08-29 23:32:14 +00001591 struct chain_head *c;
Marc Bouchere6869a82000-03-20 06:03:29 +00001592
Harald Welteaae69be2004-08-29 23:32:14 +00001593 iptc_fn = TC_GET_POLICY;
1594
1595 DEBUGP("called for chain %s\n", chain);
1596
1597 c = iptcc_find_label(chain, *handle);
1598 if (!c) {
1599 errno = ENOENT;
1600 return NULL;
1601 }
1602
1603 if (!iptcc_is_builtin(c))
Marc Bouchere6869a82000-03-20 06:03:29 +00001604 return NULL;
1605
Harald Welteaae69be2004-08-29 23:32:14 +00001606 *counters = c->counters;
Marc Bouchere6869a82000-03-20 06:03:29 +00001607
Harald Welteaae69be2004-08-29 23:32:14 +00001608 return standard_target_map(c->verdict);
Harald Welte0113fe72004-01-06 19:04:02 +00001609}
1610
1611static int
Harald Welteaae69be2004-08-29 23:32:14 +00001612iptcc_standard_map(struct rule_head *r, int verdict)
Harald Welte0113fe72004-01-06 19:04:02 +00001613{
Harald Welteaae69be2004-08-29 23:32:14 +00001614 STRUCT_ENTRY *e = r->entry;
Rusty Russell79dee072000-05-02 16:45:16 +00001615 STRUCT_STANDARD_TARGET *t;
Marc Bouchere6869a82000-03-20 06:03:29 +00001616
Rusty Russell79dee072000-05-02 16:45:16 +00001617 t = (STRUCT_STANDARD_TARGET *)GET_TARGET(e);
Marc Bouchere6869a82000-03-20 06:03:29 +00001618
Rusty Russell67088e72000-05-10 01:18:57 +00001619 if (t->target.u.target_size
Philip Blundell8c700902000-05-15 02:17:52 +00001620 != ALIGN(sizeof(STRUCT_STANDARD_TARGET))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001621 errno = EINVAL;
1622 return 0;
1623 }
1624 /* memset for memcmp convenience on delete/replace */
Rusty Russell79dee072000-05-02 16:45:16 +00001625 memset(t->target.u.user.name, 0, FUNCTION_MAXNAMELEN);
1626 strcpy(t->target.u.user.name, STANDARD_TARGET);
Marc Bouchere6869a82000-03-20 06:03:29 +00001627 t->verdict = verdict;
1628
Harald Welteaae69be2004-08-29 23:32:14 +00001629 r->type = IPTCC_R_STANDARD;
1630
Marc Bouchere6869a82000-03-20 06:03:29 +00001631 return 1;
1632}
Rusty Russell7e53bf92000-03-20 07:03:28 +00001633
Marc Bouchere6869a82000-03-20 06:03:29 +00001634static int
Harald Welteaae69be2004-08-29 23:32:14 +00001635iptcc_map_target(const TC_HANDLE_T handle,
1636 struct rule_head *r)
Marc Bouchere6869a82000-03-20 06:03:29 +00001637{
Harald Welteaae69be2004-08-29 23:32:14 +00001638 STRUCT_ENTRY *e = r->entry;
Harald Welte0113fe72004-01-06 19:04:02 +00001639 STRUCT_ENTRY_TARGET *t = GET_TARGET(e);
Marc Bouchere6869a82000-03-20 06:03:29 +00001640
Marc Bouchere6869a82000-03-20 06:03:29 +00001641 /* Maybe it's empty (=> fall through) */
Harald Welteaae69be2004-08-29 23:32:14 +00001642 if (strcmp(t->u.user.name, "") == 0) {
1643 r->type = IPTCC_R_FALLTHROUGH;
1644 return 1;
1645 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001646 /* Maybe it's a standard target name... */
Rusty Russell79dee072000-05-02 16:45:16 +00001647 else if (strcmp(t->u.user.name, LABEL_ACCEPT) == 0)
Harald Welteaae69be2004-08-29 23:32:14 +00001648 return iptcc_standard_map(r, -NF_ACCEPT - 1);
Rusty Russell79dee072000-05-02 16:45:16 +00001649 else if (strcmp(t->u.user.name, LABEL_DROP) == 0)
Harald Welteaae69be2004-08-29 23:32:14 +00001650 return iptcc_standard_map(r, -NF_DROP - 1);
Rusty Russell67088e72000-05-10 01:18:57 +00001651 else if (strcmp(t->u.user.name, LABEL_QUEUE) == 0)
Harald Welteaae69be2004-08-29 23:32:14 +00001652 return iptcc_standard_map(r, -NF_QUEUE - 1);
Rusty Russell79dee072000-05-02 16:45:16 +00001653 else if (strcmp(t->u.user.name, LABEL_RETURN) == 0)
Harald Welteaae69be2004-08-29 23:32:14 +00001654 return iptcc_standard_map(r, RETURN);
Rusty Russell79dee072000-05-02 16:45:16 +00001655 else if (TC_BUILTIN(t->u.user.name, handle)) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001656 /* Can't jump to builtins. */
1657 errno = EINVAL;
1658 return 0;
1659 } else {
1660 /* Maybe it's an existing chain name. */
Harald Welteaae69be2004-08-29 23:32:14 +00001661 struct chain_head *c;
1662 DEBUGP("trying to find chain `%s': ", t->u.user.name);
Marc Bouchere6869a82000-03-20 06:03:29 +00001663
Harald Welteaae69be2004-08-29 23:32:14 +00001664 c = iptcc_find_label(t->u.user.name, handle);
1665 if (c) {
1666 DEBUGP_C("found!\n");
1667 r->type = IPTCC_R_JUMP;
1668 r->jump = c;
1669 c->references++;
1670 return 1;
1671 }
1672 DEBUGP_C("not found :(\n");
Marc Bouchere6869a82000-03-20 06:03:29 +00001673 }
1674
1675 /* Must be a module? If not, kernel will reject... */
Rusty Russell3aef54d2005-01-03 03:48:40 +00001676 /* memset to all 0 for your memcmp convenience: don't clear version */
Rusty Russell228e98d2000-04-27 10:28:06 +00001677 memset(t->u.user.name + strlen(t->u.user.name),
Marc Bouchere6869a82000-03-20 06:03:29 +00001678 0,
Rusty Russell3aef54d2005-01-03 03:48:40 +00001679 FUNCTION_MAXNAMELEN - 1 - strlen(t->u.user.name));
Rusty Russell733e54b2004-12-16 14:22:23 +00001680 r->type = IPTCC_R_MODULE;
Harald Welteaae69be2004-08-29 23:32:14 +00001681 set_changed(handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001682 return 1;
1683}
1684
Harald Welte0113fe72004-01-06 19:04:02 +00001685/* Insert the entry `fw' in chain `chain' into position `rulenum'. */
Marc Bouchere6869a82000-03-20 06:03:29 +00001686int
Rusty Russell79dee072000-05-02 16:45:16 +00001687TC_INSERT_ENTRY(const IPT_CHAINLABEL chain,
1688 const STRUCT_ENTRY *e,
1689 unsigned int rulenum,
1690 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001691{
Harald Welteaae69be2004-08-29 23:32:14 +00001692 struct chain_head *c;
Martin Josefssoneb066cc2004-09-22 21:04:07 +00001693 struct rule_head *r;
1694 struct list_head *prev;
Marc Bouchere6869a82000-03-20 06:03:29 +00001695
Rusty Russell79dee072000-05-02 16:45:16 +00001696 iptc_fn = TC_INSERT_ENTRY;
Harald Welteaae69be2004-08-29 23:32:14 +00001697
1698 if (!(c = iptcc_find_label(chain, *handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001699 errno = ENOENT;
1700 return 0;
1701 }
1702
Martin Josefssoneb066cc2004-09-22 21:04:07 +00001703 /* first rulenum index = 0
1704 first c->num_rules index = 1 */
1705 if (rulenum > c->num_rules) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001706 errno = E2BIG;
1707 return 0;
1708 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001709
Martin Josefsson631f3612004-09-23 19:25:06 +00001710 /* If we are inserting at the end just take advantage of the
1711 double linked list, insert will happen before the entry
1712 prev points to. */
1713 if (rulenum == c->num_rules) {
Martin Josefssoneb066cc2004-09-22 21:04:07 +00001714 prev = &c->rules;
Martin Josefssona5616dc2004-10-24 22:27:31 +00001715 } else if (rulenum + 1 <= c->num_rules/2) {
Martin Josefsson631f3612004-09-23 19:25:06 +00001716 r = iptcc_get_rule_num(c, rulenum + 1);
Martin Josefssona5616dc2004-10-24 22:27:31 +00001717 prev = &r->list;
1718 } else {
1719 r = iptcc_get_rule_num_reverse(c, c->num_rules - rulenum);
Martin Josefsson631f3612004-09-23 19:25:06 +00001720 prev = &r->list;
1721 }
Martin Josefssoneb066cc2004-09-22 21:04:07 +00001722
Harald Welteaae69be2004-08-29 23:32:14 +00001723 if (!(r = iptcc_alloc_rule(c, e->next_offset))) {
1724 errno = ENOMEM;
Harald Welte0113fe72004-01-06 19:04:02 +00001725 return 0;
Harald Welteaae69be2004-08-29 23:32:14 +00001726 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001727
Harald Welteaae69be2004-08-29 23:32:14 +00001728 memcpy(r->entry, e, e->next_offset);
1729 r->counter_map.maptype = COUNTER_MAP_SET;
1730
1731 if (!iptcc_map_target(*handle, r)) {
1732 free(r);
1733 return 0;
1734 }
1735
Martin Josefssoneb066cc2004-09-22 21:04:07 +00001736 list_add_tail(&r->list, prev);
Harald Welteaae69be2004-08-29 23:32:14 +00001737 c->num_rules++;
1738
1739 set_changed(*handle);
1740
1741 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00001742}
1743
1744/* Atomically replace rule `rulenum' in `chain' with `fw'. */
1745int
Rusty Russell79dee072000-05-02 16:45:16 +00001746TC_REPLACE_ENTRY(const IPT_CHAINLABEL chain,
1747 const STRUCT_ENTRY *e,
1748 unsigned int rulenum,
1749 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001750{
Harald Welteaae69be2004-08-29 23:32:14 +00001751 struct chain_head *c;
1752 struct rule_head *r, *old;
Marc Bouchere6869a82000-03-20 06:03:29 +00001753
Rusty Russell79dee072000-05-02 16:45:16 +00001754 iptc_fn = TC_REPLACE_ENTRY;
Marc Bouchere6869a82000-03-20 06:03:29 +00001755
Harald Welteaae69be2004-08-29 23:32:14 +00001756 if (!(c = iptcc_find_label(chain, *handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001757 errno = ENOENT;
1758 return 0;
1759 }
1760
Martin Josefsson0f9b8b12004-12-18 17:18:49 +00001761 if (rulenum >= c->num_rules) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001762 errno = E2BIG;
1763 return 0;
1764 }
1765
Martin Josefsson0f9b8b12004-12-18 17:18:49 +00001766 /* Take advantage of the double linked list if possible. */
1767 if (rulenum + 1 <= c->num_rules/2) {
1768 old = iptcc_get_rule_num(c, rulenum + 1);
1769 } else {
1770 old = iptcc_get_rule_num_reverse(c, c->num_rules - rulenum);
1771 }
1772
Harald Welteaae69be2004-08-29 23:32:14 +00001773 if (!(r = iptcc_alloc_rule(c, e->next_offset))) {
1774 errno = ENOMEM;
Marc Bouchere6869a82000-03-20 06:03:29 +00001775 return 0;
Harald Welteaae69be2004-08-29 23:32:14 +00001776 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001777
Harald Welteaae69be2004-08-29 23:32:14 +00001778 memcpy(r->entry, e, e->next_offset);
1779 r->counter_map.maptype = COUNTER_MAP_SET;
1780
1781 if (!iptcc_map_target(*handle, r)) {
1782 free(r);
Harald Welte0113fe72004-01-06 19:04:02 +00001783 return 0;
Harald Welteaae69be2004-08-29 23:32:14 +00001784 }
Harald Welte0113fe72004-01-06 19:04:02 +00001785
Harald Welteaae69be2004-08-29 23:32:14 +00001786 list_add(&r->list, &old->list);
1787 iptcc_delete_rule(old);
1788
1789 set_changed(*handle);
1790
1791 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00001792}
1793
Harald Welte0113fe72004-01-06 19:04:02 +00001794/* Append entry `fw' to chain `chain'. Equivalent to insert with
Marc Bouchere6869a82000-03-20 06:03:29 +00001795 rulenum = length of chain. */
1796int
Rusty Russell79dee072000-05-02 16:45:16 +00001797TC_APPEND_ENTRY(const IPT_CHAINLABEL chain,
1798 const STRUCT_ENTRY *e,
1799 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001800{
Harald Welteaae69be2004-08-29 23:32:14 +00001801 struct chain_head *c;
1802 struct rule_head *r;
Marc Bouchere6869a82000-03-20 06:03:29 +00001803
Rusty Russell79dee072000-05-02 16:45:16 +00001804 iptc_fn = TC_APPEND_ENTRY;
Harald Welteaae69be2004-08-29 23:32:14 +00001805 if (!(c = iptcc_find_label(chain, *handle))) {
1806 DEBUGP("unable to find chain `%s'\n", chain);
Marc Bouchere6869a82000-03-20 06:03:29 +00001807 errno = ENOENT;
1808 return 0;
1809 }
1810
Harald Welteaae69be2004-08-29 23:32:14 +00001811 if (!(r = iptcc_alloc_rule(c, e->next_offset))) {
1812 DEBUGP("unable to allocate rule for chain `%s'\n", chain);
1813 errno = ENOMEM;
Harald Welte0113fe72004-01-06 19:04:02 +00001814 return 0;
Harald Welteaae69be2004-08-29 23:32:14 +00001815 }
Harald Welte0113fe72004-01-06 19:04:02 +00001816
Harald Welteaae69be2004-08-29 23:32:14 +00001817 memcpy(r->entry, e, e->next_offset);
1818 r->counter_map.maptype = COUNTER_MAP_SET;
1819
1820 if (!iptcc_map_target(*handle, r)) {
Martin Josefsson12009532004-09-23 18:24:29 +00001821 DEBUGP("unable to map target of rule for chain `%s'\n", chain);
Harald Welteaae69be2004-08-29 23:32:14 +00001822 free(r);
1823 return 0;
1824 }
1825
1826 list_add_tail(&r->list, &c->rules);
1827 c->num_rules++;
1828
1829 set_changed(*handle);
1830
1831 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00001832}
1833
1834static inline int
Rusty Russell79dee072000-05-02 16:45:16 +00001835match_different(const STRUCT_ENTRY_MATCH *a,
Rusty Russelledf14cf2000-04-19 11:26:44 +00001836 const unsigned char *a_elems,
1837 const unsigned char *b_elems,
1838 unsigned char **maskptr)
Marc Bouchere6869a82000-03-20 06:03:29 +00001839{
Rusty Russell79dee072000-05-02 16:45:16 +00001840 const STRUCT_ENTRY_MATCH *b;
Rusty Russelledf14cf2000-04-19 11:26:44 +00001841 unsigned int i;
Marc Bouchere6869a82000-03-20 06:03:29 +00001842
1843 /* Offset of b is the same as a. */
Rusty Russell30fd6e52000-04-23 09:16:06 +00001844 b = (void *)b_elems + ((unsigned char *)a - a_elems);
Marc Bouchere6869a82000-03-20 06:03:29 +00001845
Rusty Russell228e98d2000-04-27 10:28:06 +00001846 if (a->u.match_size != b->u.match_size)
Marc Bouchere6869a82000-03-20 06:03:29 +00001847 return 1;
1848
Rusty Russell228e98d2000-04-27 10:28:06 +00001849 if (strcmp(a->u.user.name, b->u.user.name) != 0)
Marc Bouchere6869a82000-03-20 06:03:29 +00001850 return 1;
1851
Rusty Russell73ef09b2000-07-03 10:24:04 +00001852 *maskptr += ALIGN(sizeof(*a));
Rusty Russelledf14cf2000-04-19 11:26:44 +00001853
Rusty Russell73ef09b2000-07-03 10:24:04 +00001854 for (i = 0; i < a->u.match_size - ALIGN(sizeof(*a)); i++)
Rusty Russelledf14cf2000-04-19 11:26:44 +00001855 if (((a->data[i] ^ b->data[i]) & (*maskptr)[i]) != 0)
Rusty Russell90e712a2000-03-29 04:19:26 +00001856 return 1;
Rusty Russelledf14cf2000-04-19 11:26:44 +00001857 *maskptr += i;
1858 return 0;
1859}
1860
1861static inline int
Rusty Russell733e54b2004-12-16 14:22:23 +00001862target_same(struct rule_head *a, struct rule_head *b,const unsigned char *mask)
Rusty Russelledf14cf2000-04-19 11:26:44 +00001863{
1864 unsigned int i;
Rusty Russell733e54b2004-12-16 14:22:23 +00001865 STRUCT_ENTRY_TARGET *ta, *tb;
Marc Bouchere6869a82000-03-20 06:03:29 +00001866
Rusty Russell733e54b2004-12-16 14:22:23 +00001867 if (a->type != b->type)
1868 return 0;
1869
1870 ta = GET_TARGET(a->entry);
1871 tb = GET_TARGET(b->entry);
1872
1873 switch (a->type) {
1874 case IPTCC_R_FALLTHROUGH:
1875 return 1;
1876 case IPTCC_R_JUMP:
1877 return a->jump == b->jump;
1878 case IPTCC_R_STANDARD:
1879 return ((STRUCT_STANDARD_TARGET *)ta)->verdict
1880 == ((STRUCT_STANDARD_TARGET *)tb)->verdict;
1881 case IPTCC_R_MODULE:
1882 if (ta->u.target_size != tb->u.target_size)
1883 return 0;
1884 if (strcmp(ta->u.user.name, tb->u.user.name) != 0)
1885 return 0;
1886
1887 for (i = 0; i < ta->u.target_size - sizeof(*ta); i++)
Rusty Russelldaade442004-12-29 11:14:52 +00001888 if (((ta->data[i] ^ tb->data[i]) & mask[i]) != 0)
Rusty Russell733e54b2004-12-16 14:22:23 +00001889 return 0;
1890 return 1;
1891 default:
1892 fprintf(stderr, "ERROR: bad type %i\n", a->type);
1893 abort();
1894 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001895}
1896
Rusty Russell733e54b2004-12-16 14:22:23 +00001897static unsigned char *
Rusty Russell79dee072000-05-02 16:45:16 +00001898is_same(const STRUCT_ENTRY *a,
1899 const STRUCT_ENTRY *b,
1900 unsigned char *matchmask);
Marc Bouchere6869a82000-03-20 06:03:29 +00001901
Harald Welte0113fe72004-01-06 19:04:02 +00001902/* Delete the first rule in `chain' which matches `fw'. */
Marc Bouchere6869a82000-03-20 06:03:29 +00001903int
Rusty Russell79dee072000-05-02 16:45:16 +00001904TC_DELETE_ENTRY(const IPT_CHAINLABEL chain,
1905 const STRUCT_ENTRY *origfw,
1906 unsigned char *matchmask,
1907 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001908{
Harald Welteaae69be2004-08-29 23:32:14 +00001909 struct chain_head *c;
Rusty Russelle45c7132004-12-16 13:21:44 +00001910 struct rule_head *r, *i;
Marc Bouchere6869a82000-03-20 06:03:29 +00001911
Rusty Russell79dee072000-05-02 16:45:16 +00001912 iptc_fn = TC_DELETE_ENTRY;
Harald Welteaae69be2004-08-29 23:32:14 +00001913 if (!(c = iptcc_find_label(chain, *handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001914 errno = ENOENT;
1915 return 0;
1916 }
1917
Rusty Russelle45c7132004-12-16 13:21:44 +00001918 /* Create a rule_head from origfw. */
1919 r = iptcc_alloc_rule(c, origfw->next_offset);
1920 if (!r) {
Harald Welte0113fe72004-01-06 19:04:02 +00001921 errno = ENOMEM;
1922 return 0;
1923 }
1924
Rusty Russelle45c7132004-12-16 13:21:44 +00001925 memcpy(r->entry, origfw, origfw->next_offset);
1926 r->counter_map.maptype = COUNTER_MAP_NOMAP;
1927 if (!iptcc_map_target(*handle, r)) {
1928 DEBUGP("unable to map target of rule for chain `%s'\n", chain);
1929 free(r);
1930 return 0;
Patrick McHardyJesper Brouer04a1e4c2006-07-25 01:50:48 +00001931 } else {
1932 /* iptcc_map_target increment target chain references
1933 * since this is a fake rule only used for matching
1934 * the chain references count is decremented again.
1935 */
1936 if (r->type == IPTCC_R_JUMP
1937 && r->jump)
1938 r->jump->references--;
Rusty Russelle45c7132004-12-16 13:21:44 +00001939 }
Harald Welte0113fe72004-01-06 19:04:02 +00001940
Rusty Russelle45c7132004-12-16 13:21:44 +00001941 list_for_each_entry(i, &c->rules, list) {
Rusty Russell733e54b2004-12-16 14:22:23 +00001942 unsigned char *mask;
Harald Weltefe537072004-08-30 20:28:53 +00001943
Rusty Russell733e54b2004-12-16 14:22:23 +00001944 mask = is_same(r->entry, i->entry, matchmask);
1945 if (!mask)
1946 continue;
Martin Josefsson2a5dbbb2004-09-22 21:37:41 +00001947
Rusty Russell733e54b2004-12-16 14:22:23 +00001948 if (!target_same(r, i, mask))
1949 continue;
1950
1951 /* If we are about to delete the rule that is the
1952 * current iterator, move rule iterator back. next
1953 * pointer will then point to real next node */
1954 if (i == (*handle)->rule_iterator_cur) {
1955 (*handle)->rule_iterator_cur =
1956 list_entry((*handle)->rule_iterator_cur->list.prev,
1957 struct rule_head, list);
Marc Bouchere6869a82000-03-20 06:03:29 +00001958 }
Rusty Russell733e54b2004-12-16 14:22:23 +00001959
1960 c->num_rules--;
1961 iptcc_delete_rule(i);
1962
1963 set_changed(*handle);
1964 free(r);
1965 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00001966 }
1967
Rusty Russelle45c7132004-12-16 13:21:44 +00001968 free(r);
Marc Bouchere6869a82000-03-20 06:03:29 +00001969 errno = ENOENT;
1970 return 0;
Rusty Russell7e53bf92000-03-20 07:03:28 +00001971}
Harald Welteaae69be2004-08-29 23:32:14 +00001972
Marc Bouchere6869a82000-03-20 06:03:29 +00001973
1974/* Delete the rule in position `rulenum' in `chain'. */
1975int
Rusty Russell79dee072000-05-02 16:45:16 +00001976TC_DELETE_NUM_ENTRY(const IPT_CHAINLABEL chain,
1977 unsigned int rulenum,
1978 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001979{
Harald Welteaae69be2004-08-29 23:32:14 +00001980 struct chain_head *c;
1981 struct rule_head *r;
Marc Bouchere6869a82000-03-20 06:03:29 +00001982
Rusty Russell79dee072000-05-02 16:45:16 +00001983 iptc_fn = TC_DELETE_NUM_ENTRY;
Harald Welteaae69be2004-08-29 23:32:14 +00001984
1985 if (!(c = iptcc_find_label(chain, *handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001986 errno = ENOENT;
1987 return 0;
1988 }
1989
Martin Josefssona5616dc2004-10-24 22:27:31 +00001990 if (rulenum >= c->num_rules) {
Martin Josefsson631f3612004-09-23 19:25:06 +00001991 errno = E2BIG;
1992 return 0;
1993 }
1994
1995 /* Take advantage of the double linked list if possible. */
Martin Josefssona5616dc2004-10-24 22:27:31 +00001996 if (rulenum + 1 <= c->num_rules/2) {
1997 r = iptcc_get_rule_num(c, rulenum + 1);
1998 } else {
1999 r = iptcc_get_rule_num_reverse(c, c->num_rules - rulenum);
Marc Bouchere6869a82000-03-20 06:03:29 +00002000 }
2001
Harald Welteaae69be2004-08-29 23:32:14 +00002002 /* If we are about to delete the rule that is the current
2003 * iterator, move rule iterator back. next pointer will then
2004 * point to real next node */
2005 if (r == (*handle)->rule_iterator_cur) {
2006 (*handle)->rule_iterator_cur =
2007 list_entry((*handle)->rule_iterator_cur->list.prev,
2008 struct rule_head, list);
Harald Welte0113fe72004-01-06 19:04:02 +00002009 }
Marc Bouchere6869a82000-03-20 06:03:29 +00002010
Harald Welteaae69be2004-08-29 23:32:14 +00002011 c->num_rules--;
2012 iptcc_delete_rule(r);
2013
Martin Josefsson2a5dbbb2004-09-22 21:37:41 +00002014 set_changed(*handle);
2015
Harald Welteaae69be2004-08-29 23:32:14 +00002016 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00002017}
2018
2019/* Check the packet `fw' on chain `chain'. Returns the verdict, or
2020 NULL and sets errno. */
2021const char *
Rusty Russell79dee072000-05-02 16:45:16 +00002022TC_CHECK_PACKET(const IPT_CHAINLABEL chain,
2023 STRUCT_ENTRY *entry,
2024 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00002025{
Derrik Pates664c0a32005-02-01 13:28:14 +00002026 iptc_fn = TC_CHECK_PACKET;
Marc Bouchere6869a82000-03-20 06:03:29 +00002027 errno = ENOSYS;
2028 return NULL;
2029}
2030
2031/* Flushes the entries in the given chain (ie. empties chain). */
2032int
Rusty Russell79dee072000-05-02 16:45:16 +00002033TC_FLUSH_ENTRIES(const IPT_CHAINLABEL chain, TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00002034{
Harald Welteaae69be2004-08-29 23:32:14 +00002035 struct chain_head *c;
2036 struct rule_head *r, *tmp;
Marc Bouchere6869a82000-03-20 06:03:29 +00002037
Harald Welte0113fe72004-01-06 19:04:02 +00002038 iptc_fn = TC_FLUSH_ENTRIES;
Harald Welteaae69be2004-08-29 23:32:14 +00002039 if (!(c = iptcc_find_label(chain, *handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002040 errno = ENOENT;
2041 return 0;
2042 }
Marc Bouchere6869a82000-03-20 06:03:29 +00002043
Harald Welteaae69be2004-08-29 23:32:14 +00002044 list_for_each_entry_safe(r, tmp, &c->rules, list) {
2045 iptcc_delete_rule(r);
2046 }
2047
2048 c->num_rules = 0;
2049
2050 set_changed(*handle);
2051
2052 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00002053}
2054
2055/* Zeroes the counters in a chain. */
2056int
Rusty Russell79dee072000-05-02 16:45:16 +00002057TC_ZERO_ENTRIES(const IPT_CHAINLABEL chain, TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00002058{
Harald Welteaae69be2004-08-29 23:32:14 +00002059 struct chain_head *c;
2060 struct rule_head *r;
Rusty Russell7e53bf92000-03-20 07:03:28 +00002061
Derrik Pates664c0a32005-02-01 13:28:14 +00002062 iptc_fn = TC_ZERO_ENTRIES;
Harald Welteaae69be2004-08-29 23:32:14 +00002063 if (!(c = iptcc_find_label(chain, *handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002064 errno = ENOENT;
2065 return 0;
2066 }
Marc Bouchere6869a82000-03-20 06:03:29 +00002067
Andy Gaye5bd1d72006-08-22 02:56:41 +00002068 if (c->counter_map.maptype == COUNTER_MAP_NORMAL_MAP)
2069 c->counter_map.maptype = COUNTER_MAP_ZEROED;
2070
Harald Welteaae69be2004-08-29 23:32:14 +00002071 list_for_each_entry(r, &c->rules, list) {
2072 if (r->counter_map.maptype == COUNTER_MAP_NORMAL_MAP)
2073 r->counter_map.maptype = COUNTER_MAP_ZEROED;
Marc Bouchere6869a82000-03-20 06:03:29 +00002074 }
Harald Welteaae69be2004-08-29 23:32:14 +00002075
Rusty Russell175f6412000-03-24 09:32:20 +00002076 set_changed(*handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00002077
Marc Bouchere6869a82000-03-20 06:03:29 +00002078 return 1;
2079}
2080
Harald Welte1cef74d2001-01-05 15:22:59 +00002081STRUCT_COUNTERS *
2082TC_READ_COUNTER(const IPT_CHAINLABEL chain,
2083 unsigned int rulenum,
2084 TC_HANDLE_T *handle)
2085{
Harald Welteaae69be2004-08-29 23:32:14 +00002086 struct chain_head *c;
2087 struct rule_head *r;
Harald Welte1cef74d2001-01-05 15:22:59 +00002088
2089 iptc_fn = TC_READ_COUNTER;
2090 CHECK(*handle);
2091
Harald Welteaae69be2004-08-29 23:32:14 +00002092 if (!(c = iptcc_find_label(chain, *handle))) {
Harald Welte1cef74d2001-01-05 15:22:59 +00002093 errno = ENOENT;
2094 return NULL;
2095 }
2096
Harald Welteaae69be2004-08-29 23:32:14 +00002097 if (!(r = iptcc_get_rule_num(c, rulenum))) {
Harald Welte0113fe72004-01-06 19:04:02 +00002098 errno = E2BIG;
2099 return NULL;
2100 }
2101
Harald Welteaae69be2004-08-29 23:32:14 +00002102 return &r->entry[0].counters;
Harald Welte1cef74d2001-01-05 15:22:59 +00002103}
2104
2105int
2106TC_ZERO_COUNTER(const IPT_CHAINLABEL chain,
2107 unsigned int rulenum,
2108 TC_HANDLE_T *handle)
2109{
Harald Welteaae69be2004-08-29 23:32:14 +00002110 struct chain_head *c;
2111 struct rule_head *r;
Harald Welte1cef74d2001-01-05 15:22:59 +00002112
2113 iptc_fn = TC_ZERO_COUNTER;
2114 CHECK(*handle);
2115
Harald Welteaae69be2004-08-29 23:32:14 +00002116 if (!(c = iptcc_find_label(chain, *handle))) {
Harald Welte1cef74d2001-01-05 15:22:59 +00002117 errno = ENOENT;
2118 return 0;
2119 }
2120
Harald Welteaae69be2004-08-29 23:32:14 +00002121 if (!(r = iptcc_get_rule_num(c, rulenum))) {
Harald Welte0113fe72004-01-06 19:04:02 +00002122 errno = E2BIG;
2123 return 0;
2124 }
2125
Harald Welteaae69be2004-08-29 23:32:14 +00002126 if (r->counter_map.maptype == COUNTER_MAP_NORMAL_MAP)
2127 r->counter_map.maptype = COUNTER_MAP_ZEROED;
Harald Welte1cef74d2001-01-05 15:22:59 +00002128
2129 set_changed(*handle);
2130
2131 return 1;
2132}
2133
2134int
2135TC_SET_COUNTER(const IPT_CHAINLABEL chain,
2136 unsigned int rulenum,
2137 STRUCT_COUNTERS *counters,
2138 TC_HANDLE_T *handle)
2139{
Harald Welteaae69be2004-08-29 23:32:14 +00002140 struct chain_head *c;
2141 struct rule_head *r;
Harald Welte1cef74d2001-01-05 15:22:59 +00002142 STRUCT_ENTRY *e;
Harald Welte1cef74d2001-01-05 15:22:59 +00002143
2144 iptc_fn = TC_SET_COUNTER;
2145 CHECK(*handle);
2146
Harald Welteaae69be2004-08-29 23:32:14 +00002147 if (!(c = iptcc_find_label(chain, *handle))) {
Harald Welte1cef74d2001-01-05 15:22:59 +00002148 errno = ENOENT;
2149 return 0;
2150 }
Harald Welte0113fe72004-01-06 19:04:02 +00002151
Harald Welteaae69be2004-08-29 23:32:14 +00002152 if (!(r = iptcc_get_rule_num(c, rulenum))) {
Harald Welte0113fe72004-01-06 19:04:02 +00002153 errno = E2BIG;
2154 return 0;
2155 }
2156
Harald Welteaae69be2004-08-29 23:32:14 +00002157 e = r->entry;
2158 r->counter_map.maptype = COUNTER_MAP_SET;
Harald Welte0113fe72004-01-06 19:04:02 +00002159
2160 memcpy(&e->counters, counters, sizeof(STRUCT_COUNTERS));
Harald Welte1cef74d2001-01-05 15:22:59 +00002161
2162 set_changed(*handle);
2163
2164 return 1;
2165}
2166
Marc Bouchere6869a82000-03-20 06:03:29 +00002167/* Creates a new chain. */
2168/* To create a chain, create two rules: error node and unconditional
2169 * return. */
2170int
Rusty Russell79dee072000-05-02 16:45:16 +00002171TC_CREATE_CHAIN(const IPT_CHAINLABEL chain, TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00002172{
Harald Welteaae69be2004-08-29 23:32:14 +00002173 static struct chain_head *c;
Marc Bouchere6869a82000-03-20 06:03:29 +00002174
Rusty Russell79dee072000-05-02 16:45:16 +00002175 iptc_fn = TC_CREATE_CHAIN;
Marc Bouchere6869a82000-03-20 06:03:29 +00002176
2177 /* find_label doesn't cover built-in targets: DROP, ACCEPT,
2178 QUEUE, RETURN. */
Harald Welteaae69be2004-08-29 23:32:14 +00002179 if (iptcc_find_label(chain, *handle)
Rusty Russell79dee072000-05-02 16:45:16 +00002180 || strcmp(chain, LABEL_DROP) == 0
2181 || strcmp(chain, LABEL_ACCEPT) == 0
Rusty Russell67088e72000-05-10 01:18:57 +00002182 || strcmp(chain, LABEL_QUEUE) == 0
Rusty Russell79dee072000-05-02 16:45:16 +00002183 || strcmp(chain, LABEL_RETURN) == 0) {
Harald Welteaae69be2004-08-29 23:32:14 +00002184 DEBUGP("Chain `%s' already exists\n", chain);
Marc Bouchere6869a82000-03-20 06:03:29 +00002185 errno = EEXIST;
2186 return 0;
2187 }
2188
Rusty Russell79dee072000-05-02 16:45:16 +00002189 if (strlen(chain)+1 > sizeof(IPT_CHAINLABEL)) {
Harald Welteaae69be2004-08-29 23:32:14 +00002190 DEBUGP("Chain name `%s' too long\n", chain);
Marc Bouchere6869a82000-03-20 06:03:29 +00002191 errno = EINVAL;
2192 return 0;
2193 }
2194
Harald Welteaae69be2004-08-29 23:32:14 +00002195 c = iptcc_alloc_chain_head(chain, 0);
2196 if (!c) {
2197 DEBUGP("Cannot allocate memory for chain `%s'\n", chain);
2198 errno = ENOMEM;
2199 return 0;
Marc Bouchere6869a82000-03-20 06:03:29 +00002200
Harald Welteaae69be2004-08-29 23:32:14 +00002201 }
Jesper Dangaard Brouer48bde402008-01-15 17:06:48 +00002202 (*handle)->num_chains++; /* New user defined chain */
Marc Bouchere6869a82000-03-20 06:03:29 +00002203
Harald Welteaae69be2004-08-29 23:32:14 +00002204 DEBUGP("Creating chain `%s'\n", chain);
Jesper Dangaard Brouerd8cb7872007-11-28 08:40:26 +00002205 iptc_insert_chain(*handle, c); /* Insert sorted */
Harald Welte0113fe72004-01-06 19:04:02 +00002206
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +00002207 /* Inserting chains don't change the correctness of the chain
2208 * index (except if its smaller than index[0], but that
2209 * handled by iptc_insert_chain). It only causes longer lists
2210 * in the buckets. Thus, only rebuild chain index when the
2211 * capacity is exceed with CHAIN_INDEX_INSERT_MAX chains.
2212 */
2213 int capacity = (*handle)->chain_index_sz * CHAIN_INDEX_BUCKET_LEN;
2214 int exceeded = ((((*handle)->num_chains)-capacity));
2215 if (exceeded > CHAIN_INDEX_INSERT_MAX) {
2216 debug("Capacity(%d) exceeded(%d) rebuild (chains:%d)\n",
2217 capacity, exceeded, (*handle)->num_chains);
2218 iptcc_chain_index_rebuild(*handle);
2219 }
2220
Harald Welte0113fe72004-01-06 19:04:02 +00002221 set_changed(*handle);
2222
Harald Welteaae69be2004-08-29 23:32:14 +00002223 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00002224}
2225
2226/* Get the number of references to this chain. */
2227int
Rusty Russell79dee072000-05-02 16:45:16 +00002228TC_GET_REFERENCES(unsigned int *ref, const IPT_CHAINLABEL chain,
2229 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00002230{
Harald Welteaae69be2004-08-29 23:32:14 +00002231 struct chain_head *c;
Marc Bouchere6869a82000-03-20 06:03:29 +00002232
Derrik Pates664c0a32005-02-01 13:28:14 +00002233 iptc_fn = TC_GET_REFERENCES;
Harald Welteaae69be2004-08-29 23:32:14 +00002234 if (!(c = iptcc_find_label(chain, *handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002235 errno = ENOENT;
2236 return 0;
2237 }
2238
Harald Welteaae69be2004-08-29 23:32:14 +00002239 *ref = c->references;
2240
Marc Bouchere6869a82000-03-20 06:03:29 +00002241 return 1;
2242}
2243
2244/* Deletes a chain. */
2245int
Rusty Russell79dee072000-05-02 16:45:16 +00002246TC_DELETE_CHAIN(const IPT_CHAINLABEL chain, TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00002247{
Marc Bouchere6869a82000-03-20 06:03:29 +00002248 unsigned int references;
Harald Welteaae69be2004-08-29 23:32:14 +00002249 struct chain_head *c;
Rusty Russell7e53bf92000-03-20 07:03:28 +00002250
Rusty Russell79dee072000-05-02 16:45:16 +00002251 iptc_fn = TC_DELETE_CHAIN;
Marc Bouchere6869a82000-03-20 06:03:29 +00002252
Harald Welteaae69be2004-08-29 23:32:14 +00002253 if (!(c = iptcc_find_label(chain, *handle))) {
2254 DEBUGP("cannot find chain `%s'\n", chain);
Marc Bouchere6869a82000-03-20 06:03:29 +00002255 errno = ENOENT;
2256 return 0;
2257 }
2258
Harald Welteaae69be2004-08-29 23:32:14 +00002259 if (TC_BUILTIN(chain, *handle)) {
2260 DEBUGP("cannot remove builtin chain `%s'\n", chain);
2261 errno = EINVAL;
2262 return 0;
2263 }
2264
2265 if (!TC_GET_REFERENCES(&references, chain, handle)) {
2266 DEBUGP("cannot get references on chain `%s'\n", chain);
2267 return 0;
2268 }
2269
2270 if (references > 0) {
2271 DEBUGP("chain `%s' still has references\n", chain);
2272 errno = EMLINK;
2273 return 0;
2274 }
2275
2276 if (c->num_rules) {
2277 DEBUGP("chain `%s' is not empty\n", chain);
Marc Bouchere6869a82000-03-20 06:03:29 +00002278 errno = ENOTEMPTY;
2279 return 0;
2280 }
2281
Harald Welteaae69be2004-08-29 23:32:14 +00002282 /* If we are about to delete the chain that is the current
Jesper Dangaard Brouer48bde402008-01-15 17:06:48 +00002283 * iterator, move chain iterator forward. */
Harald Welteaae69be2004-08-29 23:32:14 +00002284 if (c == (*handle)->chain_iterator_cur)
2285 iptcc_chain_iterator_advance(*handle);
Harald Welte0113fe72004-01-06 19:04:02 +00002286
Jesper Dangaard Brouer48bde402008-01-15 17:06:48 +00002287 (*handle)->num_chains--; /* One user defined chain deleted */
2288
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +00002289 //list_del(&c->list); /* Done in iptcc_chain_index_delete_chain() */
2290 iptcc_chain_index_delete_chain(c, *handle);
2291 free(c);
2292
Harald Welteaae69be2004-08-29 23:32:14 +00002293 DEBUGP("chain `%s' deleted\n", chain);
2294
2295 set_changed(*handle);
2296
2297 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00002298}
2299
2300/* Renames a chain. */
Rusty Russell79dee072000-05-02 16:45:16 +00002301int TC_RENAME_CHAIN(const IPT_CHAINLABEL oldname,
2302 const IPT_CHAINLABEL newname,
2303 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00002304{
Harald Welteaae69be2004-08-29 23:32:14 +00002305 struct chain_head *c;
Rusty Russell79dee072000-05-02 16:45:16 +00002306 iptc_fn = TC_RENAME_CHAIN;
Marc Bouchere6869a82000-03-20 06:03:29 +00002307
Harald Welte1de80462000-10-30 12:00:27 +00002308 /* find_label doesn't cover built-in targets: DROP, ACCEPT,
2309 QUEUE, RETURN. */
Harald Welteaae69be2004-08-29 23:32:14 +00002310 if (iptcc_find_label(newname, *handle)
Rusty Russell79dee072000-05-02 16:45:16 +00002311 || strcmp(newname, LABEL_DROP) == 0
2312 || strcmp(newname, LABEL_ACCEPT) == 0
Harald Welte1de80462000-10-30 12:00:27 +00002313 || strcmp(newname, LABEL_QUEUE) == 0
Rusty Russell79dee072000-05-02 16:45:16 +00002314 || strcmp(newname, LABEL_RETURN) == 0) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002315 errno = EEXIST;
2316 return 0;
2317 }
2318
Harald Welteaae69be2004-08-29 23:32:14 +00002319 if (!(c = iptcc_find_label(oldname, *handle))
Rusty Russell79dee072000-05-02 16:45:16 +00002320 || TC_BUILTIN(oldname, *handle)) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002321 errno = ENOENT;
2322 return 0;
2323 }
2324
Rusty Russell79dee072000-05-02 16:45:16 +00002325 if (strlen(newname)+1 > sizeof(IPT_CHAINLABEL)) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002326 errno = EINVAL;
2327 return 0;
2328 }
2329
Harald Welteaae69be2004-08-29 23:32:14 +00002330 strncpy(c->name, newname, sizeof(IPT_CHAINLABEL));
2331
Harald Welte0113fe72004-01-06 19:04:02 +00002332 set_changed(*handle);
2333
Marc Bouchere6869a82000-03-20 06:03:29 +00002334 return 1;
2335}
2336
2337/* Sets the policy on a built-in chain. */
2338int
Rusty Russell79dee072000-05-02 16:45:16 +00002339TC_SET_POLICY(const IPT_CHAINLABEL chain,
2340 const IPT_CHAINLABEL policy,
Harald Welte1cef74d2001-01-05 15:22:59 +00002341 STRUCT_COUNTERS *counters,
Rusty Russell79dee072000-05-02 16:45:16 +00002342 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00002343{
Harald Welteaae69be2004-08-29 23:32:14 +00002344 struct chain_head *c;
Marc Bouchere6869a82000-03-20 06:03:29 +00002345
Rusty Russell79dee072000-05-02 16:45:16 +00002346 iptc_fn = TC_SET_POLICY;
Marc Bouchere6869a82000-03-20 06:03:29 +00002347
Harald Welteaae69be2004-08-29 23:32:14 +00002348 if (!(c = iptcc_find_label(chain, *handle))) {
2349 DEBUGP("cannot find chain `%s'\n", chain);
2350 errno = ENOENT;
Marc Bouchere6869a82000-03-20 06:03:29 +00002351 return 0;
2352 }
2353
Harald Welteaae69be2004-08-29 23:32:14 +00002354 if (!iptcc_is_builtin(c)) {
2355 DEBUGP("cannot set policy of userdefinedchain `%s'\n", chain);
2356 errno = ENOENT;
2357 return 0;
2358 }
Marc Bouchere6869a82000-03-20 06:03:29 +00002359
Rusty Russell79dee072000-05-02 16:45:16 +00002360 if (strcmp(policy, LABEL_ACCEPT) == 0)
Harald Welteaae69be2004-08-29 23:32:14 +00002361 c->verdict = -NF_ACCEPT - 1;
Rusty Russell79dee072000-05-02 16:45:16 +00002362 else if (strcmp(policy, LABEL_DROP) == 0)
Harald Welteaae69be2004-08-29 23:32:14 +00002363 c->verdict = -NF_DROP - 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00002364 else {
2365 errno = EINVAL;
2366 return 0;
2367 }
Harald Welte1cef74d2001-01-05 15:22:59 +00002368
Harald Welte1cef74d2001-01-05 15:22:59 +00002369 if (counters) {
2370 /* set byte and packet counters */
Harald Welteaae69be2004-08-29 23:32:14 +00002371 memcpy(&c->counters, counters, sizeof(STRUCT_COUNTERS));
2372 c->counter_map.maptype = COUNTER_MAP_SET;
Harald Welte1cef74d2001-01-05 15:22:59 +00002373 } else {
Harald Welteaae69be2004-08-29 23:32:14 +00002374 c->counter_map.maptype = COUNTER_MAP_NOMAP;
Harald Welte1cef74d2001-01-05 15:22:59 +00002375 }
2376
Rusty Russell175f6412000-03-24 09:32:20 +00002377 set_changed(*handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00002378
Marc Bouchere6869a82000-03-20 06:03:29 +00002379 return 1;
2380}
2381
2382/* Without this, on gcc 2.7.2.3, we get:
Rusty Russell79dee072000-05-02 16:45:16 +00002383 libiptc.c: In function `TC_COMMIT':
Marc Bouchere6869a82000-03-20 06:03:29 +00002384 libiptc.c:833: fixed or forbidden register was spilled.
2385 This may be due to a compiler bug or to impossible asm
2386 statements or clauses.
2387*/
2388static void
Rusty Russell79dee072000-05-02 16:45:16 +00002389subtract_counters(STRUCT_COUNTERS *answer,
2390 const STRUCT_COUNTERS *a,
2391 const STRUCT_COUNTERS *b)
Marc Bouchere6869a82000-03-20 06:03:29 +00002392{
2393 answer->pcnt = a->pcnt - b->pcnt;
2394 answer->bcnt = a->bcnt - b->bcnt;
2395}
2396
Harald Welteaae69be2004-08-29 23:32:14 +00002397
2398static void counters_nomap(STRUCT_COUNTERS_INFO *newcounters,
2399 unsigned int index)
2400{
2401 newcounters->counters[index] = ((STRUCT_COUNTERS) { 0, 0});
2402 DEBUGP_C("NOMAP => zero\n");
2403}
2404
2405static void counters_normal_map(STRUCT_COUNTERS_INFO *newcounters,
2406 STRUCT_REPLACE *repl,
2407 unsigned int index,
2408 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 */
2417 newcounters->counters[index] = repl->counters[mappos];
2418 DEBUGP_C("NORMAL_MAP => mappos %u \n", mappos);
2419}
2420
2421static void counters_map_zeroed(STRUCT_COUNTERS_INFO *newcounters,
2422 STRUCT_REPLACE *repl,
2423 unsigned int index,
2424 unsigned int mappos,
2425 STRUCT_COUNTERS *counters)
2426{
2427 /* Original read: X.
2428 * Atomic read on replacement: X + Y.
2429 * Currently in kernel: Z.
2430 * Want in kernel: Y + Z.
2431 * => Add in Y.
2432 * => Add in (replacement read - original read).
2433 */
2434 subtract_counters(&newcounters->counters[index],
2435 &repl->counters[mappos],
2436 counters);
2437 DEBUGP_C("ZEROED => mappos %u\n", mappos);
2438}
2439
2440static void counters_map_set(STRUCT_COUNTERS_INFO *newcounters,
2441 unsigned int index,
2442 STRUCT_COUNTERS *counters)
2443{
2444 /* Want to set counter (iptables-restore) */
2445
2446 memcpy(&newcounters->counters[index], counters,
2447 sizeof(STRUCT_COUNTERS));
2448
2449 DEBUGP_C("SET\n");
2450}
2451
2452
Marc Bouchere6869a82000-03-20 06:03:29 +00002453int
Rusty Russell79dee072000-05-02 16:45:16 +00002454TC_COMMIT(TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00002455{
2456 /* Replace, then map back the counters. */
Rusty Russell79dee072000-05-02 16:45:16 +00002457 STRUCT_REPLACE *repl;
2458 STRUCT_COUNTERS_INFO *newcounters;
Harald Welteaae69be2004-08-29 23:32:14 +00002459 struct chain_head *c;
2460 int ret;
Martin Josefsson841e4ae2003-05-02 15:30:11 +00002461 size_t counterlen;
Harald Welteaae69be2004-08-29 23:32:14 +00002462 int new_number;
2463 unsigned int new_size;
Marc Bouchere6869a82000-03-20 06:03:29 +00002464
Derrik Pates664c0a32005-02-01 13:28:14 +00002465 iptc_fn = TC_COMMIT;
Marc Bouchere6869a82000-03-20 06:03:29 +00002466 CHECK(*handle);
Martin Josefsson841e4ae2003-05-02 15:30:11 +00002467
Marc Bouchere6869a82000-03-20 06:03:29 +00002468 /* Don't commit if nothing changed. */
2469 if (!(*handle)->changed)
2470 goto finished;
2471
Harald Welteaae69be2004-08-29 23:32:14 +00002472 new_number = iptcc_compile_table_prep(*handle, &new_size);
2473 if (new_number < 0) {
2474 errno = ENOMEM;
Harald Welted6ba6f52005-11-12 10:39:40 +00002475 goto out_zero;
Harald Welteaae69be2004-08-29 23:32:14 +00002476 }
2477
2478 repl = malloc(sizeof(*repl) + new_size);
Marc Bouchere6869a82000-03-20 06:03:29 +00002479 if (!repl) {
2480 errno = ENOMEM;
Harald Welted6ba6f52005-11-12 10:39:40 +00002481 goto out_zero;
Marc Bouchere6869a82000-03-20 06:03:29 +00002482 }
Martin Josefssonad3b4f92004-09-22 22:04:07 +00002483 memset(repl, 0, sizeof(*repl) + new_size);
Harald Welteaae69be2004-08-29 23:32:14 +00002484
Rusty Russelle45c7132004-12-16 13:21:44 +00002485#if 0
2486 TC_DUMP_ENTRIES(*handle);
2487#endif
2488
Harald Welteaae69be2004-08-29 23:32:14 +00002489 counterlen = sizeof(STRUCT_COUNTERS_INFO)
2490 + sizeof(STRUCT_COUNTERS) * new_number;
Marc Bouchere6869a82000-03-20 06:03:29 +00002491
2492 /* These are the old counters we will get from kernel */
Rusty Russell79dee072000-05-02 16:45:16 +00002493 repl->counters = malloc(sizeof(STRUCT_COUNTERS)
Marc Bouchere6869a82000-03-20 06:03:29 +00002494 * (*handle)->info.num_entries);
2495 if (!repl->counters) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002496 errno = ENOMEM;
Harald Welted6ba6f52005-11-12 10:39:40 +00002497 goto out_free_repl;
Marc Bouchere6869a82000-03-20 06:03:29 +00002498 }
Marc Bouchere6869a82000-03-20 06:03:29 +00002499 /* These are the counters we're going to put back, later. */
2500 newcounters = malloc(counterlen);
2501 if (!newcounters) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002502 errno = ENOMEM;
Harald Welted6ba6f52005-11-12 10:39:40 +00002503 goto out_free_repl_counters;
Marc Bouchere6869a82000-03-20 06:03:29 +00002504 }
Harald Welteaae69be2004-08-29 23:32:14 +00002505 memset(newcounters, 0, counterlen);
Marc Bouchere6869a82000-03-20 06:03:29 +00002506
2507 strcpy(repl->name, (*handle)->info.name);
Harald Welteaae69be2004-08-29 23:32:14 +00002508 repl->num_entries = new_number;
2509 repl->size = new_size;
2510
Marc Bouchere6869a82000-03-20 06:03:29 +00002511 repl->num_counters = (*handle)->info.num_entries;
2512 repl->valid_hooks = (*handle)->info.valid_hooks;
Harald Welteaae69be2004-08-29 23:32:14 +00002513
2514 DEBUGP("num_entries=%u, size=%u, num_counters=%u\n",
2515 repl->num_entries, repl->size, repl->num_counters);
2516
2517 ret = iptcc_compile_table(*handle, repl);
2518 if (ret < 0) {
2519 errno = ret;
Harald Welted6ba6f52005-11-12 10:39:40 +00002520 goto out_free_newcounters;
Harald Welteaae69be2004-08-29 23:32:14 +00002521 }
2522
2523
2524#ifdef IPTC_DEBUG2
2525 {
2526 int fd = open("/tmp/libiptc-so_set_replace.blob",
2527 O_CREAT|O_WRONLY);
2528 if (fd >= 0) {
2529 write(fd, repl, sizeof(*repl) + repl->size);
2530 close(fd);
2531 }
2532 }
2533#endif
Marc Bouchere6869a82000-03-20 06:03:29 +00002534
Harald Welted6ba6f52005-11-12 10:39:40 +00002535 ret = setsockopt(sockfd, TC_IPPROTO, SO_SET_REPLACE, repl,
2536 sizeof(*repl) + repl->size);
Patrick McHardye0865ad2006-04-22 02:08:56 +00002537 if (ret < 0)
Harald Welted6ba6f52005-11-12 10:39:40 +00002538 goto out_free_newcounters;
Marc Bouchere6869a82000-03-20 06:03:29 +00002539
2540 /* Put counters back. */
2541 strcpy(newcounters->name, (*handle)->info.name);
Harald Welteaae69be2004-08-29 23:32:14 +00002542 newcounters->num_counters = new_number;
Marc Bouchere6869a82000-03-20 06:03:29 +00002543
Harald Welteaae69be2004-08-29 23:32:14 +00002544 list_for_each_entry(c, &(*handle)->chains, list) {
2545 struct rule_head *r;
Marc Bouchere6869a82000-03-20 06:03:29 +00002546
Harald Welteaae69be2004-08-29 23:32:14 +00002547 /* Builtin chains have their own counters */
2548 if (iptcc_is_builtin(c)) {
2549 DEBUGP("counter for chain-index %u: ", c->foot_index);
2550 switch(c->counter_map.maptype) {
2551 case COUNTER_MAP_NOMAP:
2552 counters_nomap(newcounters, c->foot_index);
2553 break;
2554 case COUNTER_MAP_NORMAL_MAP:
2555 counters_normal_map(newcounters, repl,
2556 c->foot_index,
2557 c->counter_map.mappos);
2558 break;
2559 case COUNTER_MAP_ZEROED:
2560 counters_map_zeroed(newcounters, repl,
2561 c->foot_index,
2562 c->counter_map.mappos,
2563 &c->counters);
2564 break;
2565 case COUNTER_MAP_SET:
2566 counters_map_set(newcounters, c->foot_index,
2567 &c->counters);
2568 break;
2569 }
2570 }
Harald Welte1cef74d2001-01-05 15:22:59 +00002571
Harald Welteaae69be2004-08-29 23:32:14 +00002572 list_for_each_entry(r, &c->rules, list) {
2573 DEBUGP("counter for index %u: ", r->index);
2574 switch (r->counter_map.maptype) {
2575 case COUNTER_MAP_NOMAP:
2576 counters_nomap(newcounters, r->index);
2577 break;
Harald Welte1cef74d2001-01-05 15:22:59 +00002578
Harald Welteaae69be2004-08-29 23:32:14 +00002579 case COUNTER_MAP_NORMAL_MAP:
2580 counters_normal_map(newcounters, repl,
2581 r->index,
2582 r->counter_map.mappos);
2583 break;
Harald Welte1cef74d2001-01-05 15:22:59 +00002584
Harald Welteaae69be2004-08-29 23:32:14 +00002585 case COUNTER_MAP_ZEROED:
2586 counters_map_zeroed(newcounters, repl,
2587 r->index,
2588 r->counter_map.mappos,
2589 &r->entry->counters);
2590 break;
2591
2592 case COUNTER_MAP_SET:
2593 counters_map_set(newcounters, r->index,
2594 &r->entry->counters);
2595 break;
2596 }
Marc Bouchere6869a82000-03-20 06:03:29 +00002597 }
2598 }
Rusty Russell62527ce2000-09-04 09:45:54 +00002599
Harald Welteaae69be2004-08-29 23:32:14 +00002600#ifdef IPTC_DEBUG2
2601 {
2602 int fd = open("/tmp/libiptc-so_set_add_counters.blob",
2603 O_CREAT|O_WRONLY);
2604 if (fd >= 0) {
2605 write(fd, newcounters, counterlen);
2606 close(fd);
2607 }
2608 }
2609#endif
2610
Harald Welted6ba6f52005-11-12 10:39:40 +00002611 ret = setsockopt(sockfd, TC_IPPROTO, SO_SET_ADD_COUNTERS,
2612 newcounters, counterlen);
Patrick McHardye0865ad2006-04-22 02:08:56 +00002613 if (ret < 0)
Harald Welted6ba6f52005-11-12 10:39:40 +00002614 goto out_free_newcounters;
Marc Bouchere6869a82000-03-20 06:03:29 +00002615
2616 free(repl->counters);
2617 free(repl);
2618 free(newcounters);
2619
Harald Welted6ba6f52005-11-12 10:39:40 +00002620finished:
Martin Josefsson841e4ae2003-05-02 15:30:11 +00002621 TC_FREE(handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00002622 return 1;
Harald Welted6ba6f52005-11-12 10:39:40 +00002623
2624out_free_newcounters:
2625 free(newcounters);
2626out_free_repl_counters:
2627 free(repl->counters);
2628out_free_repl:
2629 free(repl);
2630out_zero:
2631 return 0;
Marc Bouchere6869a82000-03-20 06:03:29 +00002632}
2633
2634/* Get raw socket. */
2635int
Patrick McHardy0b639362007-09-08 16:00:01 +00002636TC_GET_RAW_SOCKET(void)
Marc Bouchere6869a82000-03-20 06:03:29 +00002637{
2638 return sockfd;
2639}
2640
2641/* Translates errno numbers into more human-readable form than strerror. */
2642const char *
Rusty Russell79dee072000-05-02 16:45:16 +00002643TC_STRERROR(int err)
Marc Bouchere6869a82000-03-20 06:03:29 +00002644{
2645 unsigned int i;
2646 struct table_struct {
2647 void *fn;
2648 int err;
2649 const char *message;
2650 } table [] =
Harald Welte4ccfa632001-07-30 15:12:43 +00002651 { { TC_INIT, EPERM, "Permission denied (you must be root)" },
Rusty Russell79dee072000-05-02 16:45:16 +00002652 { TC_INIT, EINVAL, "Module is wrong version" },
Harald Welte4ccfa632001-07-30 15:12:43 +00002653 { TC_INIT, ENOENT,
2654 "Table does not exist (do you need to insmod?)" },
Rusty Russell79dee072000-05-02 16:45:16 +00002655 { TC_DELETE_CHAIN, ENOTEMPTY, "Chain is not empty" },
2656 { TC_DELETE_CHAIN, EINVAL, "Can't delete built-in chain" },
2657 { TC_DELETE_CHAIN, EMLINK,
Marc Bouchere6869a82000-03-20 06:03:29 +00002658 "Can't delete chain with references left" },
Rusty Russell79dee072000-05-02 16:45:16 +00002659 { TC_CREATE_CHAIN, EEXIST, "Chain already exists" },
2660 { TC_INSERT_ENTRY, E2BIG, "Index of insertion too big" },
2661 { TC_REPLACE_ENTRY, E2BIG, "Index of replacement too big" },
2662 { TC_DELETE_NUM_ENTRY, E2BIG, "Index of deletion too big" },
Harald Welte1cef74d2001-01-05 15:22:59 +00002663 { TC_READ_COUNTER, E2BIG, "Index of counter too big" },
2664 { TC_ZERO_COUNTER, E2BIG, "Index of counter too big" },
Rusty Russell79dee072000-05-02 16:45:16 +00002665 { TC_INSERT_ENTRY, ELOOP, "Loop found in table" },
2666 { TC_INSERT_ENTRY, EINVAL, "Target problem" },
Marc Bouchere6869a82000-03-20 06:03:29 +00002667 /* EINVAL for CHECK probably means bad interface. */
Rusty Russell79dee072000-05-02 16:45:16 +00002668 { TC_CHECK_PACKET, EINVAL,
Marc Boucherc8264992000-04-22 22:34:44 +00002669 "Bad arguments (does that interface exist?)" },
Harald Welte4ccfa632001-07-30 15:12:43 +00002670 { TC_CHECK_PACKET, ENOSYS,
2671 "Checking will most likely never get implemented" },
Marc Bouchere6869a82000-03-20 06:03:29 +00002672 /* ENOENT for DELETE probably means no matching rule */
Rusty Russell79dee072000-05-02 16:45:16 +00002673 { TC_DELETE_ENTRY, ENOENT,
Marc Boucherc8264992000-04-22 22:34:44 +00002674 "Bad rule (does a matching rule exist in that chain?)" },
Rusty Russell79dee072000-05-02 16:45:16 +00002675 { TC_SET_POLICY, ENOENT,
Marc Boucherc8264992000-04-22 22:34:44 +00002676 "Bad built-in chain name" },
Rusty Russell79dee072000-05-02 16:45:16 +00002677 { TC_SET_POLICY, EINVAL,
Marc Boucherc8264992000-04-22 22:34:44 +00002678 "Bad policy name" },
Harald Welte4ccfa632001-07-30 15:12:43 +00002679
2680 { NULL, 0, "Incompatible with this kernel" },
2681 { NULL, ENOPROTOOPT, "iptables who? (do you need to insmod?)" },
2682 { NULL, ENOSYS, "Will be implemented real soon. I promise ;)" },
2683 { NULL, ENOMEM, "Memory allocation problem" },
2684 { NULL, ENOENT, "No chain/target/match by that name" },
Marc Bouchere6869a82000-03-20 06:03:29 +00002685 };
2686
2687 for (i = 0; i < sizeof(table)/sizeof(struct table_struct); i++) {
2688 if ((!table[i].fn || table[i].fn == iptc_fn)
2689 && table[i].err == err)
2690 return table[i].message;
2691 }
2692
2693 return strerror(err);
2694}