blob: fe6b09b18f2b9395d5ba0d311813ae86f081d879 [file] [log] [blame]
Martin Josefsson0f9b8b12004-12-18 17:18:49 +00001/* Library which manipulates firewall rules. Version $Revision$ */
Marc Bouchere6869a82000-03-20 06:03:29 +00002
3/* Architecture of firewall rules is as follows:
4 *
5 * Chains go INPUT, FORWARD, OUTPUT then user chains.
6 * Each user chain starts with an ERROR node.
7 * Every chain ends with an unconditional jump: a RETURN for user chains,
8 * and a POLICY for built-ins.
9 */
10
Harald Welte3ea8f402003-06-23 18:25:59 +000011/* (C) 1999 Paul ``Rusty'' Russell - Placed under the GNU GPL (See
12 * COPYING for details).
Harald Welteaae69be2004-08-29 23:32:14 +000013 * (C) 2000-2004 by the Netfilter Core Team <coreteam@netfilter.org>
Harald Welte3ea8f402003-06-23 18:25:59 +000014 *
Harald Weltefbc85232003-06-24 17:37:21 +000015 * 2003-Jun-20: Harald Welte <laforge@netfilter.org>:
Harald Welte3ea8f402003-06-23 18:25:59 +000016 * - Reimplementation of chain cache to use offsets instead of entries
Harald Weltefbc85232003-06-24 17:37:21 +000017 * 2003-Jun-23: Harald Welte <laforge@netfilter.org>:
Harald Welte0113fe72004-01-06 19:04:02 +000018 * - performance optimization, sponsored by Astaro AG (http://www.astaro.com/)
Harald Weltefbc85232003-06-24 17:37:21 +000019 * don't rebuild the chain cache after every operation, instead fix it
20 * up after a ruleset change.
Harald Welteaae69be2004-08-29 23:32:14 +000021 * 2004-Aug-18: Harald Welte <laforge@netfilter.org>:
22 * - futher performance work: total reimplementation of libiptc.
23 * - libiptc now has a real internal (linked-list) represntation of the
24 * ruleset and a parser/compiler from/to this internal representation
25 * - again sponsored by Astaro AG (http://www.astaro.com/)
Harald Welte3ea8f402003-06-23 18:25:59 +000026 */
Harald Welte15920d12004-05-16 09:05:07 +000027#include <sys/types.h>
28#include <sys/socket.h>
Stephane Ouellette7cd00282004-05-14 08:21:06 +000029
Harald Welteaae69be2004-08-29 23:32:14 +000030#include "linux_list.h"
31
32//#define IPTC_DEBUG2 1
33
34#ifdef IPTC_DEBUG2
35#include <fcntl.h>
36#define DEBUGP(x, args...) fprintf(stderr, "%s: " x, __FUNCTION__, ## args)
37#define DEBUGP_C(x, args...) fprintf(stderr, x, ## args)
38#else
39#define DEBUGP(x, args...)
40#define DEBUGP_C(x, args...)
41#endif
42
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +000043#ifdef DEBUG
44#define debug(x, args...) fprintf(stderr, x, ## args)
45#else
46#define debug(x, args...)
47#endif
48
Marc Bouchere6869a82000-03-20 06:03:29 +000049#ifndef IPT_LIB_DIR
50#define IPT_LIB_DIR "/usr/local/lib/iptables"
51#endif
52
53static int sockfd = -1;
Derrik Pates664c0a32005-02-01 13:28:14 +000054static int sockfd_use = 0;
Marc Bouchere6869a82000-03-20 06:03:29 +000055static void *iptc_fn = NULL;
56
Patrick McHardy0b639362007-09-08 16:00:01 +000057static const char *hooknames[] = {
58 [HOOK_PRE_ROUTING] = "PREROUTING",
59 [HOOK_LOCAL_IN] = "INPUT",
60 [HOOK_FORWARD] = "FORWARD",
61 [HOOK_LOCAL_OUT] = "OUTPUT",
62 [HOOK_POST_ROUTING] = "POSTROUTING",
Rusty Russell10758b72000-09-14 07:37:33 +000063#ifdef HOOK_DROPPING
Patrick McHardy0b639362007-09-08 16:00:01 +000064 [HOOK_DROPPING] = "DROPPING"
Rusty Russell10758b72000-09-14 07:37:33 +000065#endif
Marc Bouchere6869a82000-03-20 06:03:29 +000066};
67
Harald Welteaae69be2004-08-29 23:32:14 +000068/* Convenience structures */
69struct ipt_error_target
70{
71 STRUCT_ENTRY_TARGET t;
72 char error[TABLE_MAXNAMELEN];
73};
74
75struct chain_head;
76struct rule_head;
77
Marc Bouchere6869a82000-03-20 06:03:29 +000078struct counter_map
79{
80 enum {
81 COUNTER_MAP_NOMAP,
82 COUNTER_MAP_NORMAL_MAP,
Harald Welte1cef74d2001-01-05 15:22:59 +000083 COUNTER_MAP_ZEROED,
84 COUNTER_MAP_SET
Marc Bouchere6869a82000-03-20 06:03:29 +000085 } maptype;
86 unsigned int mappos;
87};
88
Harald Welteaae69be2004-08-29 23:32:14 +000089enum iptcc_rule_type {
90 IPTCC_R_STANDARD, /* standard target (ACCEPT, ...) */
91 IPTCC_R_MODULE, /* extension module (SNAT, ...) */
92 IPTCC_R_FALLTHROUGH, /* fallthrough rule */
93 IPTCC_R_JUMP, /* jump to other chain */
Marc Bouchere6869a82000-03-20 06:03:29 +000094};
95
Harald Welteaae69be2004-08-29 23:32:14 +000096struct rule_head
Rusty Russell30fd6e52000-04-23 09:16:06 +000097{
Harald Welteaae69be2004-08-29 23:32:14 +000098 struct list_head list;
99 struct chain_head *chain;
100 struct counter_map counter_map;
101
102 unsigned int index; /* index (needed for counter_map) */
103 unsigned int offset; /* offset in rule blob */
104
105 enum iptcc_rule_type type;
106 struct chain_head *jump; /* jump target, if IPTCC_R_JUMP */
107
108 unsigned int size; /* size of entry data */
109 STRUCT_ENTRY entry[0];
110};
111
112struct chain_head
113{
114 struct list_head list;
Rusty Russell79dee072000-05-02 16:45:16 +0000115 char name[TABLE_MAXNAMELEN];
Harald Welteaae69be2004-08-29 23:32:14 +0000116 unsigned int hooknum; /* hook number+1 if builtin */
117 unsigned int references; /* how many jumps reference us */
118 int verdict; /* verdict if builtin */
119
120 STRUCT_COUNTERS counters; /* per-chain counters */
121 struct counter_map counter_map;
122
123 unsigned int num_rules; /* number of rules in list */
124 struct list_head rules; /* list of rules */
125
126 unsigned int index; /* index (needed for jump resolval) */
127 unsigned int head_offset; /* offset in rule blob */
128 unsigned int foot_index; /* index (needed for counter_map) */
129 unsigned int foot_offset; /* offset in rule blob */
Rusty Russell30fd6e52000-04-23 09:16:06 +0000130};
131
Rusty Russell79dee072000-05-02 16:45:16 +0000132STRUCT_TC_HANDLE
Marc Bouchere6869a82000-03-20 06:03:29 +0000133{
Harald Welteaae69be2004-08-29 23:32:14 +0000134 int changed; /* Have changes been made? */
135
136 struct list_head chains;
137
138 struct chain_head *chain_iterator_cur;
139 struct rule_head *rule_iterator_cur;
140
Jesper Dangaard Brouer48bde402008-01-15 17:06:48 +0000141 unsigned int num_chains; /* number of user defined chains */
142
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000143 struct chain_head **chain_index; /* array for fast chain list access*/
144 unsigned int chain_index_sz;/* size of chain index array */
145
Rusty Russell79dee072000-05-02 16:45:16 +0000146 STRUCT_GETINFO info;
Harald Welteaae69be2004-08-29 23:32:14 +0000147 STRUCT_GET_ENTRIES *entries;
Marc Bouchere6869a82000-03-20 06:03:29 +0000148};
149
Harald Welteaae69be2004-08-29 23:32:14 +0000150/* allocate a new chain head for the cache */
151static struct chain_head *iptcc_alloc_chain_head(const char *name, int hooknum)
152{
153 struct chain_head *c = malloc(sizeof(*c));
154 if (!c)
155 return NULL;
156 memset(c, 0, sizeof(*c));
157
158 strncpy(c->name, name, TABLE_MAXNAMELEN);
159 c->hooknum = hooknum;
160 INIT_LIST_HEAD(&c->rules);
161
162 return c;
163}
164
165/* allocate and initialize a new rule for the cache */
166static struct rule_head *iptcc_alloc_rule(struct chain_head *c, unsigned int size)
167{
168 struct rule_head *r = malloc(sizeof(*r)+size);
169 if (!r)
170 return NULL;
171 memset(r, 0, sizeof(*r));
172
173 r->chain = c;
174 r->size = size;
175
176 return r;
177}
178
179/* notify us that the ruleset has been modified by the user */
Jesper Dangaard Brouer91093982008-01-15 17:01:58 +0000180static inline void
Rusty Russell79dee072000-05-02 16:45:16 +0000181set_changed(TC_HANDLE_T h)
Rusty Russell175f6412000-03-24 09:32:20 +0000182{
Rusty Russell175f6412000-03-24 09:32:20 +0000183 h->changed = 1;
184}
185
Harald Welte380ba5f2002-02-13 16:19:55 +0000186#ifdef IPTC_DEBUG
Rusty Russell79dee072000-05-02 16:45:16 +0000187static void do_check(TC_HANDLE_T h, unsigned int line);
Rusty Russell849779c2000-04-23 15:51:51 +0000188#define CHECK(h) do { if (!getenv("IPTC_NO_CHECK")) do_check((h), __LINE__); } while(0)
Rusty Russell30fd6e52000-04-23 09:16:06 +0000189#else
190#define CHECK(h)
191#endif
Marc Bouchere6869a82000-03-20 06:03:29 +0000192
Harald Welteaae69be2004-08-29 23:32:14 +0000193
194/**********************************************************************
195 * iptc blob utility functions (iptcb_*)
196 **********************************************************************/
197
Marc Bouchere6869a82000-03-20 06:03:29 +0000198static inline int
Harald Welteaae69be2004-08-29 23:32:14 +0000199iptcb_get_number(const STRUCT_ENTRY *i,
Rusty Russell79dee072000-05-02 16:45:16 +0000200 const STRUCT_ENTRY *seek,
Marc Bouchere6869a82000-03-20 06:03:29 +0000201 unsigned int *pos)
202{
203 if (i == seek)
204 return 1;
205 (*pos)++;
206 return 0;
207}
208
Marc Bouchere6869a82000-03-20 06:03:29 +0000209static inline int
Harald Welteaae69be2004-08-29 23:32:14 +0000210iptcb_get_entry_n(STRUCT_ENTRY *i,
Marc Bouchere6869a82000-03-20 06:03:29 +0000211 unsigned int number,
212 unsigned int *pos,
Rusty Russell79dee072000-05-02 16:45:16 +0000213 STRUCT_ENTRY **pe)
Marc Bouchere6869a82000-03-20 06:03:29 +0000214{
215 if (*pos == number) {
216 *pe = i;
217 return 1;
218 }
219 (*pos)++;
220 return 0;
221}
222
Harald Welteaae69be2004-08-29 23:32:14 +0000223static inline STRUCT_ENTRY *
224iptcb_get_entry(TC_HANDLE_T h, unsigned int offset)
225{
226 return (STRUCT_ENTRY *)((char *)h->entries->entrytable + offset);
227}
228
229static unsigned int
230iptcb_entry2index(const TC_HANDLE_T h, const STRUCT_ENTRY *seek)
Marc Bouchere6869a82000-03-20 06:03:29 +0000231{
232 unsigned int pos = 0;
Marc Bouchere6869a82000-03-20 06:03:29 +0000233
Harald Welteaae69be2004-08-29 23:32:14 +0000234 if (ENTRY_ITERATE(h->entries->entrytable, h->entries->size,
235 iptcb_get_number, seek, &pos) == 0) {
236 fprintf(stderr, "ERROR: offset %u not an entry!\n",
237 (unsigned int)((char *)seek - (char *)h->entries->entrytable));
238 abort();
239 }
240 return pos;
Marc Bouchere6869a82000-03-20 06:03:29 +0000241}
242
Harald Welte0113fe72004-01-06 19:04:02 +0000243static inline STRUCT_ENTRY *
Harald Welteaae69be2004-08-29 23:32:14 +0000244iptcb_offset2entry(TC_HANDLE_T h, unsigned int offset)
Harald Welte0113fe72004-01-06 19:04:02 +0000245{
Harald Welteaae69be2004-08-29 23:32:14 +0000246 return (STRUCT_ENTRY *) ((void *)h->entries->entrytable+offset);
Harald Welte0113fe72004-01-06 19:04:02 +0000247}
248
Harald Welteaae69be2004-08-29 23:32:14 +0000249
Harald Welte0113fe72004-01-06 19:04:02 +0000250static inline unsigned long
Harald Welteaae69be2004-08-29 23:32:14 +0000251iptcb_entry2offset(const TC_HANDLE_T h, const STRUCT_ENTRY *e)
Harald Welte0113fe72004-01-06 19:04:02 +0000252{
Harald Welteaae69be2004-08-29 23:32:14 +0000253 return (void *)e - (void *)h->entries->entrytable;
Harald Welte3ea8f402003-06-23 18:25:59 +0000254}
255
256static inline unsigned int
Harald Welteaae69be2004-08-29 23:32:14 +0000257iptcb_offset2index(const TC_HANDLE_T h, unsigned int offset)
Harald Welte3ea8f402003-06-23 18:25:59 +0000258{
Harald Welteaae69be2004-08-29 23:32:14 +0000259 return iptcb_entry2index(h, iptcb_offset2entry(h, offset));
260}
261
262/* Returns 0 if not hook entry, else hooknumber + 1 */
263static inline unsigned int
264iptcb_ent_is_hook_entry(STRUCT_ENTRY *e, TC_HANDLE_T h)
265{
266 unsigned int i;
267
268 for (i = 0; i < NUMHOOKS; i++) {
269 if ((h->info.valid_hooks & (1 << i))
270 && iptcb_get_entry(h, h->info.hook_entry[i]) == e)
271 return i+1;
272 }
273 return 0;
Harald Welte3ea8f402003-06-23 18:25:59 +0000274}
275
276
Harald Welteaae69be2004-08-29 23:32:14 +0000277/**********************************************************************
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000278 * Chain index (cache utility) functions
279 **********************************************************************
280 * The chain index is an array with pointers into the chain list, with
281 * CHAIN_INDEX_BUCKET_LEN spacing. This facilitates the ability to
282 * speedup chain list searching, by find a more optimal starting
283 * points when searching the linked list.
284 *
285 * The starting point can be found fast by using a binary search of
286 * the chain index. Thus, reducing the previous search complexity of
287 * O(n) to O(log(n/k) + k) where k is CHAIN_INDEX_BUCKET_LEN.
288 *
289 * A nice property of the chain index, is that the "bucket" list
290 * length is max CHAIN_INDEX_BUCKET_LEN (when just build, inserts will
291 * change this). Oppose to hashing, where the "bucket" list length can
292 * vary a lot.
293 */
294#ifndef CHAIN_INDEX_BUCKET_LEN
295#define CHAIN_INDEX_BUCKET_LEN 40
296#endif
297
298/* Another nice property of the chain index is that inserting/creating
299 * chains in chain list don't change the correctness of the chain
300 * index, it only causes longer lists in the buckets.
301 *
302 * To mitigate the performance penalty of longer bucket lists and the
303 * penalty of rebuilding, the chain index is rebuild only when
304 * CHAIN_INDEX_INSERT_MAX chains has been added.
305 */
306#ifndef CHAIN_INDEX_INSERT_MAX
307#define CHAIN_INDEX_INSERT_MAX 355
308#endif
309
310static inline unsigned int iptcc_is_builtin(struct chain_head *c);
311
312
313/* Use binary search in the chain index array, to find a chain_head
314 * pointer closest to the place of the searched name element.
315 *
316 * Notes that, binary search (obviously) requires that the chain list
317 * is sorted by name.
318 */
319static struct list_head *
Jan Engelhardtdbb77542008-02-11 00:33:30 +0100320iptcc_bsearch_chain_index(const char *name, unsigned int *idx, TC_HANDLE_T handle)
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000321{
322 unsigned int pos, end;
323 int res;
324
325 struct list_head *list_pos;
326 list_pos=&handle->chains;
327
328 /* Check for empty array, e.g. no user defined chains */
329 if (handle->chain_index_sz == 0) {
330 debug("WARNING: handle->chain_index_sz == 0\n");
331 return list_pos;
332 }
333
334 /* Init */
335 end = handle->chain_index_sz;
336 pos = end / 2;
337
338 debug("bsearch Find chain:%s (pos:%d end:%d)\n", name, pos, end);
339
340 /* Loop */
341 loop:
342 if (!handle->chain_index[pos]) {
343 fprintf(stderr, "ERROR: NULL pointer chain_index[%d]\n", pos);
344 return &handle->chains; /* Be safe, return orig start pos */
345 }
346
347 res = strcmp(name, handle->chain_index[pos]->name);
348 list_pos = &handle->chain_index[pos]->list;
Jan Engelhardtdbb77542008-02-11 00:33:30 +0100349 *idx = pos;
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000350
351 debug("bsearch Index[%d] name:%s res:%d ",
352 pos, handle->chain_index[pos]->name, res);
353
354 if (res == 0) { /* Found element, by direct hit */
355 debug("[found] Direct hit pos:%d end:%d\n", pos, end);
356 return list_pos;
357 } else if (res < 0) { /* Too far, jump back */
358 end = pos;
359 pos = pos / 2;
360
361 /* Exit case: First element of array */
362 if (end == 0) {
363 debug("[found] Reached first array elem (end%d)\n",end);
364 return list_pos;
365 }
366 debug("jump back to pos:%d (end:%d)\n", pos, end);
367 goto loop;
368 } else if (res > 0 ){ /* Not far enough, jump forward */
369
370 /* Exit case: Last element of array */
371 if (pos == handle->chain_index_sz-1) {
372 debug("[found] Last array elem (end:%d)\n", end);
373 return list_pos;
374 }
375
376 /* Exit case: Next index less, thus elem in this list section */
377 res = strcmp(name, handle->chain_index[pos+1]->name);
378 if (res < 0) {
379 debug("[found] closest list (end:%d)\n", end);
380 return list_pos;
381 }
382
383 pos = (pos+end)/2;
384 debug("jump forward to pos:%d (end:%d)\n", pos, end);
385 goto loop;
386 }
387
388 return list_pos;
389}
390
391#ifdef DEBUG
392/* Trivial linear search of chain index. Function used for verifying
393 the output of bsearch function */
394static struct list_head *
395iptcc_linearly_search_chain_index(const char *name, TC_HANDLE_T handle)
396{
397 unsigned int i=0;
398 int res=0;
399
400 struct list_head *list_pos;
401 list_pos = &handle->chains;
402
403 if (handle->chain_index_sz)
404 list_pos = &handle->chain_index[0]->list;
405
406 /* Linearly walk of chain index array */
407
408 for (i=0; i < handle->chain_index_sz; i++) {
409 if (handle->chain_index[i]) {
410 res = strcmp(handle->chain_index[i]->name, name);
411 if (res > 0)
412 break; // One step too far
413 list_pos = &handle->chain_index[i]->list;
414 if (res == 0)
415 break; // Direct hit
416 }
417 }
418
419 return list_pos;
420}
421#endif
422
423static int iptcc_chain_index_alloc(TC_HANDLE_T h)
424{
425 unsigned int list_length = CHAIN_INDEX_BUCKET_LEN;
426 unsigned int array_elems;
427 unsigned int array_mem;
428
429 /* Allocate memory for the chain index array */
430 array_elems = (h->num_chains / list_length) +
431 (h->num_chains % list_length ? 1 : 0);
432 array_mem = sizeof(h->chain_index) * array_elems;
433
434 debug("Alloc Chain index, elems:%d mem:%d bytes\n",
435 array_elems, array_mem);
436
437 h->chain_index = malloc(array_mem);
438 if (!h->chain_index) {
439 h->chain_index_sz = 0;
440 return -ENOMEM;
441 }
442 memset(h->chain_index, 0, array_mem);
443 h->chain_index_sz = array_elems;
444
445 return 1;
446}
447
448static void iptcc_chain_index_free(TC_HANDLE_T h)
449{
450 h->chain_index_sz = 0;
451 free(h->chain_index);
452}
453
454
455#ifdef DEBUG
456static void iptcc_chain_index_dump(TC_HANDLE_T h)
457{
458 unsigned int i = 0;
459
460 /* Dump: contents of chain index array */
461 for (i=0; i < h->chain_index_sz; i++) {
462 if (h->chain_index[i]) {
463 fprintf(stderr, "Chain index[%d].name: %s\n",
464 i, h->chain_index[i]->name);
465 }
466 }
467}
468#endif
469
470/* Build the chain index */
471static int iptcc_chain_index_build(TC_HANDLE_T h)
472{
473 unsigned int list_length = CHAIN_INDEX_BUCKET_LEN;
474 unsigned int chains = 0;
475 unsigned int cindex = 0;
476 struct chain_head *c;
477
478 /* Build up the chain index array here */
479 debug("Building chain index\n");
480
481 debug("Number of user defined chains:%d bucket_sz:%d array_sz:%d\n",
482 h->num_chains, list_length, h->chain_index_sz);
483
484 if (h->chain_index_sz == 0)
485 return 0;
486
487 list_for_each_entry(c, &h->chains, list) {
488
489 /* Issue: The index array needs to start after the
490 * builtin chains, as they are not sorted */
491 if (!iptcc_is_builtin(c)) {
492 cindex=chains / list_length;
493
494 /* Safe guard, break out on array limit, this
495 * is useful if chains are added and array is
496 * rebuild, without realloc of memory. */
497 if (cindex >= h->chain_index_sz)
498 break;
499
500 if ((chains % list_length)== 0) {
501 debug("\nIndex[%d] Chains:", cindex);
502 h->chain_index[cindex] = c;
503 }
504 chains++;
505 }
506 debug("%s, ", c->name);
507 }
508 debug("\n");
509
510 return 1;
511}
512
513static int iptcc_chain_index_rebuild(TC_HANDLE_T h)
514{
515 debug("REBUILD chain index array\n");
516 iptcc_chain_index_free(h);
517 if ((iptcc_chain_index_alloc(h)) < 0)
518 return -ENOMEM;
519 iptcc_chain_index_build(h);
520 return 1;
521}
522
523/* Delete chain (pointer) from index array. Removing an element from
524 * the chain list only affects the chain index array, if the chain
525 * index points-to/uses that list pointer.
526 *
527 * There are different strategies, the simple and safe is to rebuild
528 * the chain index every time. The more advanced is to update the
529 * array index to point to the next element, but that requires some
530 * house keeping and boundry checks. The advanced is implemented, as
531 * the simple approach behaves badly when all chains are deleted
532 * because list_for_each processing will always hit the first chain
533 * index, thus causing a rebuild for every chain.
534 */
535static int iptcc_chain_index_delete_chain(struct chain_head *c, TC_HANDLE_T h)
536{
537 struct list_head *index_ptr, *index_ptr2, *next;
538 struct chain_head *c2;
Jan Engelhardtdbb77542008-02-11 00:33:30 +0100539 unsigned int idx, idx2;
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000540
Jan Engelhardtdbb77542008-02-11 00:33:30 +0100541 index_ptr = iptcc_bsearch_chain_index(c->name, &idx, h);
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000542
543 debug("Del chain[%s] c->list:%p index_ptr:%p\n",
544 c->name, &c->list, index_ptr);
545
546 /* Save the next pointer */
547 next = c->list.next;
548 list_del(&c->list);
549
550 if (index_ptr == &c->list) { /* Chain used as index ptr */
551
552 /* See if its possible to avoid a rebuild, by shifting
553 * to next pointer. Its possible if the next pointer
554 * is located in the same index bucket.
555 */
556 c2 = list_entry(next, struct chain_head, list);
Jan Engelhardtdbb77542008-02-11 00:33:30 +0100557 index_ptr2 = iptcc_bsearch_chain_index(c2->name, &idx2, h);
558 if (idx != idx2) {
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000559 /* Rebuild needed */
560 return iptcc_chain_index_rebuild(h);
561 } else {
562 /* Avoiding rebuild */
563 debug("Update cindex[%d] with next ptr name:[%s]\n",
Jan Engelhardtdbb77542008-02-11 00:33:30 +0100564 idx, c2->name);
565 h->chain_index[idx]=c2;
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000566 return 0;
567 }
568 }
569 return 0;
570}
571
572
573/**********************************************************************
Harald Welteaae69be2004-08-29 23:32:14 +0000574 * iptc cache utility functions (iptcc_*)
575 **********************************************************************/
Harald Welte0113fe72004-01-06 19:04:02 +0000576
Harald Welteaae69be2004-08-29 23:32:14 +0000577/* Is the given chain builtin (1) or user-defined (0) */
Jesper Dangaard Brouer91093982008-01-15 17:01:58 +0000578static inline unsigned int iptcc_is_builtin(struct chain_head *c)
Harald Welteaae69be2004-08-29 23:32:14 +0000579{
580 return (c->hooknum ? 1 : 0);
581}
582
583/* Get a specific rule within a chain */
584static struct rule_head *iptcc_get_rule_num(struct chain_head *c,
585 unsigned int rulenum)
586{
587 struct rule_head *r;
588 unsigned int num = 0;
589
590 list_for_each_entry(r, &c->rules, list) {
591 num++;
592 if (num == rulenum)
593 return r;
594 }
595 return NULL;
596}
597
Martin Josefssona5616dc2004-10-24 22:27:31 +0000598/* Get a specific rule within a chain backwards */
599static struct rule_head *iptcc_get_rule_num_reverse(struct chain_head *c,
600 unsigned int rulenum)
601{
602 struct rule_head *r;
603 unsigned int num = 0;
604
605 list_for_each_entry_reverse(r, &c->rules, list) {
606 num++;
607 if (num == rulenum)
608 return r;
609 }
610 return NULL;
611}
612
Harald Welteaae69be2004-08-29 23:32:14 +0000613/* Returns chain head if found, otherwise NULL. */
614static struct chain_head *
615iptcc_find_chain_by_offset(TC_HANDLE_T handle, unsigned int offset)
616{
617 struct list_head *pos;
618
619 if (list_empty(&handle->chains))
620 return NULL;
621
622 list_for_each(pos, &handle->chains) {
623 struct chain_head *c = list_entry(pos, struct chain_head, list);
624 if (offset >= c->head_offset && offset <= c->foot_offset)
625 return c;
Harald Welte0113fe72004-01-06 19:04:02 +0000626 }
627
Harald Welteaae69be2004-08-29 23:32:14 +0000628 return NULL;
Harald Welte0113fe72004-01-06 19:04:02 +0000629}
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000630
Harald Welteaae69be2004-08-29 23:32:14 +0000631/* Returns chain head if found, otherwise NULL. */
632static struct chain_head *
633iptcc_find_label(const char *name, TC_HANDLE_T handle)
634{
635 struct list_head *pos;
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000636 struct list_head *list_start_pos;
637 unsigned int i=0;
638 int res;
Harald Welteaae69be2004-08-29 23:32:14 +0000639
640 if (list_empty(&handle->chains))
641 return NULL;
642
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000643 /* First look at builtin chains */
Harald Welteaae69be2004-08-29 23:32:14 +0000644 list_for_each(pos, &handle->chains) {
645 struct chain_head *c = list_entry(pos, struct chain_head, list);
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000646 if (!iptcc_is_builtin(c))
647 break;
Harald Welteaae69be2004-08-29 23:32:14 +0000648 if (!strcmp(c->name, name))
649 return c;
650 }
651
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000652 /* Find a smart place to start the search via chain index */
653 //list_start_pos = iptcc_linearly_search_chain_index(name, handle);
654 list_start_pos = iptcc_bsearch_chain_index(name, &i, handle);
655
656 /* Handel if bsearch bails out early */
657 if (list_start_pos == &handle->chains) {
658 list_start_pos = pos;
659 }
660#ifdef DEBUG
661 else {
662 /* Verify result of bsearch against linearly index search */
663 struct list_head *test_pos;
664 struct chain_head *test_c, *tmp_c;
665 test_pos = iptcc_linearly_search_chain_index(name, handle);
666 if (list_start_pos != test_pos) {
667 debug("BUG in chain_index search\n");
668 test_c=list_entry(test_pos, struct chain_head,list);
669 tmp_c =list_entry(list_start_pos,struct chain_head,list);
670 debug("Verify search found:\n");
671 debug(" Chain:%s\n", test_c->name);
672 debug("BSearch found:\n");
673 debug(" Chain:%s\n", tmp_c->name);
674 exit(42);
675 }
676 }
677#endif
678
679 /* Initial/special case, no user defined chains */
680 if (handle->num_chains == 0)
681 return NULL;
682
683 /* Start searching through the chain list */
684 list_for_each(pos, list_start_pos->prev) {
685 struct chain_head *c = list_entry(pos, struct chain_head, list);
686 res = strcmp(c->name, name);
687 debug("List search name:%s == %s res:%d\n", name, c->name, res);
688 if (res==0)
689 return c;
690
691 /* We can stop earlier as we know list is sorted */
692 if (res>0 && !iptcc_is_builtin(c)) { /* Walked too far*/
693 debug(" Not in list, walked too far, sorted list\n");
694 return NULL;
695 }
696
697 /* Stop on wrap around, if list head is reached */
698 if (pos == &handle->chains) {
699 debug("Stop, list head reached\n");
700 return NULL;
701 }
702 }
703
704 debug("List search NOT found name:%s\n", name);
Harald Welteaae69be2004-08-29 23:32:14 +0000705 return NULL;
706}
707
708/* called when rule is to be removed from cache */
709static void iptcc_delete_rule(struct rule_head *r)
710{
711 DEBUGP("deleting rule %p (offset %u)\n", r, r->offset);
712 /* clean up reference count of called chain */
713 if (r->type == IPTCC_R_JUMP
714 && r->jump)
715 r->jump->references--;
716
717 list_del(&r->list);
718 free(r);
719}
720
721
722/**********************************************************************
723 * RULESET PARSER (blob -> cache)
724 **********************************************************************/
725
Harald Welteaae69be2004-08-29 23:32:14 +0000726/* Delete policy rule of previous chain, since cache doesn't contain
727 * chain policy rules.
728 * WARNING: This function has ugly design and relies on a lot of context, only
729 * to be called from specific places within the parser */
730static int __iptcc_p_del_policy(TC_HANDLE_T h, unsigned int num)
731{
732 if (h->chain_iterator_cur) {
733 /* policy rule is last rule */
734 struct rule_head *pr = (struct rule_head *)
735 h->chain_iterator_cur->rules.prev;
736
737 /* save verdict */
738 h->chain_iterator_cur->verdict =
739 *(int *)GET_TARGET(pr->entry)->data;
740
741 /* save counter and counter_map information */
742 h->chain_iterator_cur->counter_map.maptype =
743 COUNTER_MAP_NORMAL_MAP;
744 h->chain_iterator_cur->counter_map.mappos = num-1;
745 memcpy(&h->chain_iterator_cur->counters, &pr->entry->counters,
746 sizeof(h->chain_iterator_cur->counters));
747
748 /* foot_offset points to verdict rule */
749 h->chain_iterator_cur->foot_index = num;
750 h->chain_iterator_cur->foot_offset = pr->offset;
751
752 /* delete rule from cache */
753 iptcc_delete_rule(pr);
Martin Josefsson8d1b38a2004-09-22 21:00:19 +0000754 h->chain_iterator_cur->num_rules--;
Harald Welteaae69be2004-08-29 23:32:14 +0000755
756 return 1;
757 }
758 return 0;
759}
760
Harald Welteec30b6c2005-02-01 16:45:56 +0000761/* alphabetically insert a chain into the list */
762static inline void iptc_insert_chain(TC_HANDLE_T h, struct chain_head *c)
763{
764 struct chain_head *tmp;
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000765 struct list_head *list_start_pos;
766 unsigned int i=1;
767
768 /* Find a smart place to start the insert search */
769 list_start_pos = iptcc_bsearch_chain_index(c->name, &i, h);
770
771 /* Handle the case, where chain.name is smaller than index[0] */
772 if (i==0 && strcmp(c->name, h->chain_index[0]->name) <= 0) {
773 h->chain_index[0] = c; /* Update chain index head */
774 list_start_pos = h->chains.next;
775 debug("Update chain_index[0] with %s\n", c->name);
776 }
777
778 /* Handel if bsearch bails out early */
779 if (list_start_pos == &h->chains) {
780 list_start_pos = h->chains.next;
781 }
Harald Welteec30b6c2005-02-01 16:45:56 +0000782
Olaf Rempel9d3ed772005-03-04 23:08:30 +0000783 /* sort only user defined chains */
784 if (!c->hooknum) {
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000785 list_for_each_entry(tmp, list_start_pos->prev, list) {
Robert de Barthfeca0572005-07-31 07:04:59 +0000786 if (!tmp->hooknum && strcmp(c->name, tmp->name) <= 0) {
Olaf Rempel9d3ed772005-03-04 23:08:30 +0000787 list_add(&c->list, tmp->list.prev);
788 return;
789 }
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000790
791 /* Stop if list head is reached */
792 if (&tmp->list == &h->chains) {
793 debug("Insert, list head reached add to tail\n");
794 break;
795 }
Harald Welteec30b6c2005-02-01 16:45:56 +0000796 }
797 }
798
799 /* survived till end of list: add at tail */
800 list_add_tail(&c->list, &h->chains);
801}
802
Harald Welteaae69be2004-08-29 23:32:14 +0000803/* Another ugly helper function split out of cache_add_entry to make it less
804 * spaghetti code */
805static void __iptcc_p_add_chain(TC_HANDLE_T h, struct chain_head *c,
806 unsigned int offset, unsigned int *num)
807{
Jesper Dangaard Brouer13364512007-12-12 15:20:42 +0000808 struct list_head *tail = h->chains.prev;
809 struct chain_head *ctail;
810
Harald Welteaae69be2004-08-29 23:32:14 +0000811 __iptcc_p_del_policy(h, *num);
812
813 c->head_offset = offset;
814 c->index = *num;
815
Jesper Dangaard Brouer13364512007-12-12 15:20:42 +0000816 /* Chains from kernel are already sorted, as they are inserted
817 * sorted. But there exists an issue when shifting to 1.4.0
818 * from an older version, as old versions allow last created
819 * chain to be unsorted.
820 */
821 if (iptcc_is_builtin(c)) /* Only user defined chains are sorted*/
822 list_add_tail(&c->list, &h->chains);
823 else {
824 ctail = list_entry(tail, struct chain_head, list);
825 if (strcmp(c->name, ctail->name) > 0)
826 list_add_tail(&c->list, &h->chains);/* Already sorted*/
827 else
828 iptc_insert_chain(h, c);/* Was not sorted */
829 }
Jesper Dangaard Brouerd8cb7872007-11-28 08:40:26 +0000830
Harald Welteaae69be2004-08-29 23:32:14 +0000831 h->chain_iterator_cur = c;
832}
833
834/* main parser function: add an entry from the blob to the cache */
835static int cache_add_entry(STRUCT_ENTRY *e,
836 TC_HANDLE_T h,
837 STRUCT_ENTRY **prev,
838 unsigned int *num)
839{
840 unsigned int builtin;
841 unsigned int offset = (char *)e - (char *)h->entries->entrytable;
842
843 DEBUGP("entering...");
844
845 /* Last entry ("policy rule"). End it.*/
846 if (iptcb_entry2offset(h,e) + e->next_offset == h->entries->size) {
847 /* This is the ERROR node at the end of the chain */
848 DEBUGP_C("%u:%u: end of table:\n", *num, offset);
849
850 __iptcc_p_del_policy(h, *num);
851
852 h->chain_iterator_cur = NULL;
853 goto out_inc;
854 }
855
856 /* We know this is the start of a new chain if it's an ERROR
857 * target, or a hook entry point */
858
859 if (strcmp(GET_TARGET(e)->u.user.name, ERROR_TARGET) == 0) {
860 struct chain_head *c =
861 iptcc_alloc_chain_head((const char *)GET_TARGET(e)->data, 0);
862 DEBUGP_C("%u:%u:new userdefined chain %s: %p\n", *num, offset,
863 (char *)c->name, c);
864 if (!c) {
865 errno = -ENOMEM;
866 return -1;
867 }
Jesper Dangaard Brouer48bde402008-01-15 17:06:48 +0000868 h->num_chains++; /* New user defined chain */
Harald Welteaae69be2004-08-29 23:32:14 +0000869
870 __iptcc_p_add_chain(h, c, offset, num);
871
872 } else if ((builtin = iptcb_ent_is_hook_entry(e, h)) != 0) {
873 struct chain_head *c =
874 iptcc_alloc_chain_head((char *)hooknames[builtin-1],
875 builtin);
876 DEBUGP_C("%u:%u new builtin chain: %p (rules=%p)\n",
877 *num, offset, c, &c->rules);
878 if (!c) {
879 errno = -ENOMEM;
880 return -1;
881 }
882
883 c->hooknum = builtin;
884
885 __iptcc_p_add_chain(h, c, offset, num);
886
887 /* FIXME: this is ugly. */
888 goto new_rule;
889 } else {
890 /* has to be normal rule */
891 struct rule_head *r;
892new_rule:
893
894 if (!(r = iptcc_alloc_rule(h->chain_iterator_cur,
895 e->next_offset))) {
896 errno = ENOMEM;
897 return -1;
898 }
899 DEBUGP_C("%u:%u normal rule: %p: ", *num, offset, r);
900
901 r->index = *num;
902 r->offset = offset;
903 memcpy(r->entry, e, e->next_offset);
904 r->counter_map.maptype = COUNTER_MAP_NORMAL_MAP;
905 r->counter_map.mappos = r->index;
906
907 /* handling of jumps, etc. */
908 if (!strcmp(GET_TARGET(e)->u.user.name, STANDARD_TARGET)) {
909 STRUCT_STANDARD_TARGET *t;
910
911 t = (STRUCT_STANDARD_TARGET *)GET_TARGET(e);
912 if (t->target.u.target_size
913 != ALIGN(sizeof(STRUCT_STANDARD_TARGET))) {
914 errno = EINVAL;
915 return -1;
916 }
917
918 if (t->verdict < 0) {
919 DEBUGP_C("standard, verdict=%d\n", t->verdict);
920 r->type = IPTCC_R_STANDARD;
921 } else if (t->verdict == r->offset+e->next_offset) {
922 DEBUGP_C("fallthrough\n");
923 r->type = IPTCC_R_FALLTHROUGH;
924 } else {
925 DEBUGP_C("jump, target=%u\n", t->verdict);
926 r->type = IPTCC_R_JUMP;
927 /* Jump target fixup has to be deferred
928 * until second pass, since we migh not
929 * yet have parsed the target */
930 }
Martin Josefsson52c38022004-09-22 19:39:40 +0000931 } else {
932 DEBUGP_C("module, target=%s\n", GET_TARGET(e)->u.user.name);
933 r->type = IPTCC_R_MODULE;
Harald Welteaae69be2004-08-29 23:32:14 +0000934 }
935
936 list_add_tail(&r->list, &h->chain_iterator_cur->rules);
Martin Josefsson8d1b38a2004-09-22 21:00:19 +0000937 h->chain_iterator_cur->num_rules++;
Harald Welteaae69be2004-08-29 23:32:14 +0000938 }
939out_inc:
940 (*num)++;
941 return 0;
942}
943
944
945/* parse an iptables blob into it's pieces */
946static int parse_table(TC_HANDLE_T h)
947{
948 STRUCT_ENTRY *prev;
949 unsigned int num = 0;
950 struct chain_head *c;
951
952 /* First pass: over ruleset blob */
953 ENTRY_ITERATE(h->entries->entrytable, h->entries->size,
954 cache_add_entry, h, &prev, &num);
955
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000956 /* Build the chain index, used for chain list search speedup */
957 if ((iptcc_chain_index_alloc(h)) < 0)
958 return -ENOMEM;
959 iptcc_chain_index_build(h);
960
Harald Welteaae69be2004-08-29 23:32:14 +0000961 /* Second pass: fixup parsed data from first pass */
962 list_for_each_entry(c, &h->chains, list) {
963 struct rule_head *r;
964 list_for_each_entry(r, &c->rules, list) {
Jan Engelhardtdbb77542008-02-11 00:33:30 +0100965 struct chain_head *lc;
Harald Welteaae69be2004-08-29 23:32:14 +0000966 STRUCT_STANDARD_TARGET *t;
967
968 if (r->type != IPTCC_R_JUMP)
969 continue;
970
971 t = (STRUCT_STANDARD_TARGET *)GET_TARGET(r->entry);
Jan Engelhardtdbb77542008-02-11 00:33:30 +0100972 lc = iptcc_find_chain_by_offset(h, t->verdict);
973 if (!lc)
Harald Welteaae69be2004-08-29 23:32:14 +0000974 return -1;
Jan Engelhardtdbb77542008-02-11 00:33:30 +0100975 r->jump = lc;
976 lc->references++;
Harald Welteaae69be2004-08-29 23:32:14 +0000977 }
978 }
979
980 /* FIXME: sort chains */
981
982 return 1;
983}
984
985
986/**********************************************************************
987 * RULESET COMPILATION (cache -> blob)
988 **********************************************************************/
989
990/* Convenience structures */
991struct iptcb_chain_start{
992 STRUCT_ENTRY e;
993 struct ipt_error_target name;
994};
995#define IPTCB_CHAIN_START_SIZE (sizeof(STRUCT_ENTRY) + \
996 ALIGN(sizeof(struct ipt_error_target)))
997
998struct iptcb_chain_foot {
999 STRUCT_ENTRY e;
1000 STRUCT_STANDARD_TARGET target;
1001};
1002#define IPTCB_CHAIN_FOOT_SIZE (sizeof(STRUCT_ENTRY) + \
1003 ALIGN(sizeof(STRUCT_STANDARD_TARGET)))
1004
1005struct iptcb_chain_error {
1006 STRUCT_ENTRY entry;
1007 struct ipt_error_target target;
1008};
1009#define IPTCB_CHAIN_ERROR_SIZE (sizeof(STRUCT_ENTRY) + \
1010 ALIGN(sizeof(struct ipt_error_target)))
1011
1012
1013
1014/* compile rule from cache into blob */
1015static inline int iptcc_compile_rule (TC_HANDLE_T h, STRUCT_REPLACE *repl, struct rule_head *r)
1016{
1017 /* handle jumps */
1018 if (r->type == IPTCC_R_JUMP) {
1019 STRUCT_STANDARD_TARGET *t;
1020 t = (STRUCT_STANDARD_TARGET *)GET_TARGET(r->entry);
1021 /* memset for memcmp convenience on delete/replace */
1022 memset(t->target.u.user.name, 0, FUNCTION_MAXNAMELEN);
1023 strcpy(t->target.u.user.name, STANDARD_TARGET);
1024 /* Jumps can only happen to builtin chains, so we
1025 * can safely assume that they always have a header */
1026 t->verdict = r->jump->head_offset + IPTCB_CHAIN_START_SIZE;
1027 } else if (r->type == IPTCC_R_FALLTHROUGH) {
1028 STRUCT_STANDARD_TARGET *t;
1029 t = (STRUCT_STANDARD_TARGET *)GET_TARGET(r->entry);
1030 t->verdict = r->offset + r->size;
1031 }
1032
1033 /* copy entry from cache to blob */
1034 memcpy((char *)repl->entries+r->offset, r->entry, r->size);
1035
1036 return 1;
1037}
1038
1039/* compile chain from cache into blob */
1040static int iptcc_compile_chain(TC_HANDLE_T h, STRUCT_REPLACE *repl, struct chain_head *c)
1041{
1042 int ret;
1043 struct rule_head *r;
1044 struct iptcb_chain_start *head;
1045 struct iptcb_chain_foot *foot;
1046
1047 /* only user-defined chains have heaer */
1048 if (!iptcc_is_builtin(c)) {
1049 /* put chain header in place */
1050 head = (void *)repl->entries + c->head_offset;
1051 head->e.target_offset = sizeof(STRUCT_ENTRY);
1052 head->e.next_offset = IPTCB_CHAIN_START_SIZE;
1053 strcpy(head->name.t.u.user.name, ERROR_TARGET);
1054 head->name.t.u.target_size =
1055 ALIGN(sizeof(struct ipt_error_target));
1056 strcpy(head->name.error, c->name);
1057 } else {
1058 repl->hook_entry[c->hooknum-1] = c->head_offset;
1059 repl->underflow[c->hooknum-1] = c->foot_offset;
1060 }
1061
1062 /* iterate over rules */
1063 list_for_each_entry(r, &c->rules, list) {
1064 ret = iptcc_compile_rule(h, repl, r);
1065 if (ret < 0)
1066 return ret;
1067 }
1068
1069 /* put chain footer in place */
1070 foot = (void *)repl->entries + c->foot_offset;
1071 foot->e.target_offset = sizeof(STRUCT_ENTRY);
1072 foot->e.next_offset = IPTCB_CHAIN_FOOT_SIZE;
1073 strcpy(foot->target.target.u.user.name, STANDARD_TARGET);
1074 foot->target.target.u.target_size =
1075 ALIGN(sizeof(STRUCT_STANDARD_TARGET));
1076 /* builtin targets have verdict, others return */
1077 if (iptcc_is_builtin(c))
1078 foot->target.verdict = c->verdict;
1079 else
1080 foot->target.verdict = RETURN;
1081 /* set policy-counters */
1082 memcpy(&foot->e.counters, &c->counters, sizeof(STRUCT_COUNTERS));
1083
1084 return 0;
1085}
1086
1087/* calculate offset and number for every rule in the cache */
1088static int iptcc_compile_chain_offsets(TC_HANDLE_T h, struct chain_head *c,
Harald Welteefa8fc22005-07-19 22:03:49 +00001089 unsigned int *offset, unsigned int *num)
Harald Welteaae69be2004-08-29 23:32:14 +00001090{
1091 struct rule_head *r;
1092
1093 c->head_offset = *offset;
1094 DEBUGP("%s: chain_head %u, offset=%u\n", c->name, *num, *offset);
1095
1096 if (!iptcc_is_builtin(c)) {
1097 /* Chain has header */
1098 *offset += sizeof(STRUCT_ENTRY)
1099 + ALIGN(sizeof(struct ipt_error_target));
1100 (*num)++;
1101 }
1102
1103 list_for_each_entry(r, &c->rules, list) {
1104 DEBUGP("rule %u, offset=%u, index=%u\n", *num, *offset, *num);
1105 r->offset = *offset;
1106 r->index = *num;
1107 *offset += r->size;
1108 (*num)++;
1109 }
1110
1111 DEBUGP("%s; chain_foot %u, offset=%u, index=%u\n", c->name, *num,
1112 *offset, *num);
1113 c->foot_offset = *offset;
1114 c->foot_index = *num;
1115 *offset += sizeof(STRUCT_ENTRY)
1116 + ALIGN(sizeof(STRUCT_STANDARD_TARGET));
1117 (*num)++;
1118
1119 return 1;
1120}
1121
1122/* put the pieces back together again */
1123static int iptcc_compile_table_prep(TC_HANDLE_T h, unsigned int *size)
1124{
1125 struct chain_head *c;
1126 unsigned int offset = 0, num = 0;
1127 int ret = 0;
1128
1129 /* First pass: calculate offset for every rule */
1130 list_for_each_entry(c, &h->chains, list) {
1131 ret = iptcc_compile_chain_offsets(h, c, &offset, &num);
1132 if (ret < 0)
1133 return ret;
1134 }
1135
1136 /* Append one error rule at end of chain */
1137 num++;
1138 offset += sizeof(STRUCT_ENTRY)
1139 + ALIGN(sizeof(struct ipt_error_target));
1140
1141 /* ruleset size is now in offset */
1142 *size = offset;
1143 return num;
1144}
1145
1146static int iptcc_compile_table(TC_HANDLE_T h, STRUCT_REPLACE *repl)
1147{
1148 struct chain_head *c;
1149 struct iptcb_chain_error *error;
1150
1151 /* Second pass: copy from cache to offsets, fill in jumps */
1152 list_for_each_entry(c, &h->chains, list) {
1153 int ret = iptcc_compile_chain(h, repl, c);
1154 if (ret < 0)
1155 return ret;
1156 }
1157
1158 /* Append error rule at end of chain */
1159 error = (void *)repl->entries + repl->size - IPTCB_CHAIN_ERROR_SIZE;
1160 error->entry.target_offset = sizeof(STRUCT_ENTRY);
1161 error->entry.next_offset = IPTCB_CHAIN_ERROR_SIZE;
1162 error->target.t.u.user.target_size =
1163 ALIGN(sizeof(struct ipt_error_target));
1164 strcpy((char *)&error->target.t.u.user.name, ERROR_TARGET);
1165 strcpy((char *)&error->target.error, "ERROR");
1166
1167 return 1;
1168}
1169
1170/**********************************************************************
1171 * EXTERNAL API (operates on cache only)
1172 **********************************************************************/
Marc Bouchere6869a82000-03-20 06:03:29 +00001173
1174/* Allocate handle of given size */
Rusty Russell79dee072000-05-02 16:45:16 +00001175static TC_HANDLE_T
Harald Welte0113fe72004-01-06 19:04:02 +00001176alloc_handle(const char *tablename, unsigned int size, unsigned int num_rules)
Marc Bouchere6869a82000-03-20 06:03:29 +00001177{
1178 size_t len;
Rusty Russell79dee072000-05-02 16:45:16 +00001179 TC_HANDLE_T h;
Marc Bouchere6869a82000-03-20 06:03:29 +00001180
Harald Welteaae69be2004-08-29 23:32:14 +00001181 len = sizeof(STRUCT_TC_HANDLE) + size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001182
Harald Welteaae69be2004-08-29 23:32:14 +00001183 h = malloc(sizeof(STRUCT_TC_HANDLE));
1184 if (!h) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001185 errno = ENOMEM;
1186 return NULL;
1187 }
Harald Welteaae69be2004-08-29 23:32:14 +00001188 memset(h, 0, sizeof(*h));
1189 INIT_LIST_HEAD(&h->chains);
Marc Bouchere6869a82000-03-20 06:03:29 +00001190 strcpy(h->info.name, tablename);
Harald Welteaae69be2004-08-29 23:32:14 +00001191
Harald Welte0371c0c2004-09-19 21:00:12 +00001192 h->entries = malloc(sizeof(STRUCT_GET_ENTRIES) + size);
Harald Welteaae69be2004-08-29 23:32:14 +00001193 if (!h->entries)
1194 goto out_free_handle;
1195
1196 strcpy(h->entries->name, tablename);
Harald Welte0371c0c2004-09-19 21:00:12 +00001197 h->entries->size = size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001198
1199 return h;
Harald Welteaae69be2004-08-29 23:32:14 +00001200
1201out_free_handle:
1202 free(h);
1203
1204 return NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +00001205}
1206
Harald Welteaae69be2004-08-29 23:32:14 +00001207
Rusty Russell79dee072000-05-02 16:45:16 +00001208TC_HANDLE_T
1209TC_INIT(const char *tablename)
Marc Bouchere6869a82000-03-20 06:03:29 +00001210{
Rusty Russell79dee072000-05-02 16:45:16 +00001211 TC_HANDLE_T h;
1212 STRUCT_GETINFO info;
Harald Welteefa8fc22005-07-19 22:03:49 +00001213 unsigned int tmp;
Marc Bouchere6869a82000-03-20 06:03:29 +00001214 socklen_t s;
1215
Rusty Russell79dee072000-05-02 16:45:16 +00001216 iptc_fn = TC_INIT;
Marc Bouchere6869a82000-03-20 06:03:29 +00001217
Martin Josefsson841e4ae2003-05-02 15:30:11 +00001218 if (strlen(tablename) >= TABLE_MAXNAMELEN) {
1219 errno = EINVAL;
1220 return NULL;
1221 }
1222
Derrik Pates664c0a32005-02-01 13:28:14 +00001223 if (sockfd_use == 0) {
1224 sockfd = socket(TC_AF, SOCK_RAW, IPPROTO_RAW);
1225 if (sockfd < 0)
1226 return NULL;
1227 }
1228 sockfd_use++;
Patrick McHardy2f932052008-04-02 14:01:53 +02001229retry:
Marc Bouchere6869a82000-03-20 06:03:29 +00001230 s = sizeof(info);
Martin Josefsson841e4ae2003-05-02 15:30:11 +00001231
Marc Bouchere6869a82000-03-20 06:03:29 +00001232 strcpy(info.name, tablename);
Derrik Pates664c0a32005-02-01 13:28:14 +00001233 if (getsockopt(sockfd, TC_IPPROTO, SO_GET_INFO, &info, &s) < 0) {
1234 if (--sockfd_use == 0) {
1235 close(sockfd);
1236 sockfd = -1;
1237 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001238 return NULL;
Derrik Pates664c0a32005-02-01 13:28:14 +00001239 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001240
Harald Welteaae69be2004-08-29 23:32:14 +00001241 DEBUGP("valid_hooks=0x%08x, num_entries=%u, size=%u\n",
1242 info.valid_hooks, info.num_entries, info.size);
1243
Harald Welte0113fe72004-01-06 19:04:02 +00001244 if ((h = alloc_handle(info.name, info.size, info.num_entries))
Martin Josefsson841e4ae2003-05-02 15:30:11 +00001245 == NULL) {
Derrik Pates664c0a32005-02-01 13:28:14 +00001246 if (--sockfd_use == 0) {
1247 close(sockfd);
1248 sockfd = -1;
1249 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001250 return NULL;
Martin Josefsson841e4ae2003-05-02 15:30:11 +00001251 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001252
Marc Bouchere6869a82000-03-20 06:03:29 +00001253 /* Initialize current state */
1254 h->info = info;
Harald Welte0113fe72004-01-06 19:04:02 +00001255
Harald Welteaae69be2004-08-29 23:32:14 +00001256 h->entries->size = h->info.size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001257
Rusty Russell79dee072000-05-02 16:45:16 +00001258 tmp = sizeof(STRUCT_GET_ENTRIES) + h->info.size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001259
Harald Welteaae69be2004-08-29 23:32:14 +00001260 if (getsockopt(sockfd, TC_IPPROTO, SO_GET_ENTRIES, h->entries,
1261 &tmp) < 0)
1262 goto error;
1263
1264#ifdef IPTC_DEBUG2
1265 {
1266 int fd = open("/tmp/libiptc-so_get_entries.blob",
1267 O_CREAT|O_WRONLY);
1268 if (fd >= 0) {
1269 write(fd, h->entries, tmp);
1270 close(fd);
1271 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001272 }
Harald Welteaae69be2004-08-29 23:32:14 +00001273#endif
1274
1275 if (parse_table(h) < 0)
1276 goto error;
Rusty Russell7e53bf92000-03-20 07:03:28 +00001277
Marc Bouchere6869a82000-03-20 06:03:29 +00001278 CHECK(h);
1279 return h;
Harald Welteaae69be2004-08-29 23:32:14 +00001280error:
1281 TC_FREE(&h);
Patrick McHardy2f932052008-04-02 14:01:53 +02001282 /* A different process changed the ruleset size, retry */
1283 if (errno == EAGAIN)
1284 goto retry;
Harald Welteaae69be2004-08-29 23:32:14 +00001285 return NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +00001286}
1287
Martin Josefsson841e4ae2003-05-02 15:30:11 +00001288void
1289TC_FREE(TC_HANDLE_T *h)
1290{
Harald Welteaae69be2004-08-29 23:32:14 +00001291 struct chain_head *c, *tmp;
1292
Derrik Pates664c0a32005-02-01 13:28:14 +00001293 iptc_fn = TC_FREE;
1294 if (--sockfd_use == 0) {
1295 close(sockfd);
1296 sockfd = -1;
1297 }
Harald Welteaae69be2004-08-29 23:32:14 +00001298
1299 list_for_each_entry_safe(c, tmp, &(*h)->chains, list) {
1300 struct rule_head *r, *rtmp;
1301
1302 list_for_each_entry_safe(r, rtmp, &c->rules, list) {
1303 free(r);
1304 }
1305
1306 free(c);
1307 }
1308
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +00001309 iptcc_chain_index_free(*h);
1310
Harald Welteaae69be2004-08-29 23:32:14 +00001311 free((*h)->entries);
Martin Josefsson841e4ae2003-05-02 15:30:11 +00001312 free(*h);
Harald Welteaae69be2004-08-29 23:32:14 +00001313
Martin Josefsson841e4ae2003-05-02 15:30:11 +00001314 *h = NULL;
1315}
1316
Marc Bouchere6869a82000-03-20 06:03:29 +00001317static inline int
Rusty Russell79dee072000-05-02 16:45:16 +00001318print_match(const STRUCT_ENTRY_MATCH *m)
Marc Bouchere6869a82000-03-20 06:03:29 +00001319{
Rusty Russell228e98d2000-04-27 10:28:06 +00001320 printf("Match name: `%s'\n", m->u.user.name);
Marc Bouchere6869a82000-03-20 06:03:29 +00001321 return 0;
1322}
1323
Rusty Russell79dee072000-05-02 16:45:16 +00001324static int dump_entry(STRUCT_ENTRY *e, const TC_HANDLE_T handle);
1325
Marc Bouchere6869a82000-03-20 06:03:29 +00001326void
Rusty Russell79dee072000-05-02 16:45:16 +00001327TC_DUMP_ENTRIES(const TC_HANDLE_T handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001328{
Derrik Pates664c0a32005-02-01 13:28:14 +00001329 iptc_fn = TC_DUMP_ENTRIES;
Marc Bouchere6869a82000-03-20 06:03:29 +00001330 CHECK(handle);
Patrick McHardy97fb2f12007-09-08 16:52:25 +00001331
Rusty Russelle45c7132004-12-16 13:21:44 +00001332 printf("libiptc v%s. %u bytes.\n",
1333 IPTABLES_VERSION, handle->entries->size);
Marc Bouchere6869a82000-03-20 06:03:29 +00001334 printf("Table `%s'\n", handle->info.name);
1335 printf("Hooks: pre/in/fwd/out/post = %u/%u/%u/%u/%u\n",
Rusty Russell67088e72000-05-10 01:18:57 +00001336 handle->info.hook_entry[HOOK_PRE_ROUTING],
1337 handle->info.hook_entry[HOOK_LOCAL_IN],
1338 handle->info.hook_entry[HOOK_FORWARD],
1339 handle->info.hook_entry[HOOK_LOCAL_OUT],
1340 handle->info.hook_entry[HOOK_POST_ROUTING]);
Marc Bouchere6869a82000-03-20 06:03:29 +00001341 printf("Underflows: pre/in/fwd/out/post = %u/%u/%u/%u/%u\n",
Rusty Russell67088e72000-05-10 01:18:57 +00001342 handle->info.underflow[HOOK_PRE_ROUTING],
1343 handle->info.underflow[HOOK_LOCAL_IN],
1344 handle->info.underflow[HOOK_FORWARD],
1345 handle->info.underflow[HOOK_LOCAL_OUT],
1346 handle->info.underflow[HOOK_POST_ROUTING]);
Marc Bouchere6869a82000-03-20 06:03:29 +00001347
Harald Welteaae69be2004-08-29 23:32:14 +00001348 ENTRY_ITERATE(handle->entries->entrytable, handle->entries->size,
Rusty Russell79dee072000-05-02 16:45:16 +00001349 dump_entry, handle);
Harald Welte0113fe72004-01-06 19:04:02 +00001350}
Rusty Russell30fd6e52000-04-23 09:16:06 +00001351
Marc Bouchere6869a82000-03-20 06:03:29 +00001352/* Does this chain exist? */
Rusty Russell79dee072000-05-02 16:45:16 +00001353int TC_IS_CHAIN(const char *chain, const TC_HANDLE_T handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001354{
Derrik Pates664c0a32005-02-01 13:28:14 +00001355 iptc_fn = TC_IS_CHAIN;
Harald Welteaae69be2004-08-29 23:32:14 +00001356 return iptcc_find_label(chain, handle) != NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +00001357}
1358
Harald Welteaae69be2004-08-29 23:32:14 +00001359static void iptcc_chain_iterator_advance(TC_HANDLE_T handle)
1360{
1361 struct chain_head *c = handle->chain_iterator_cur;
1362
1363 if (c->list.next == &handle->chains)
1364 handle->chain_iterator_cur = NULL;
1365 else
1366 handle->chain_iterator_cur =
1367 list_entry(c->list.next, struct chain_head, list);
1368}
Marc Bouchere6869a82000-03-20 06:03:29 +00001369
Rusty Russell30fd6e52000-04-23 09:16:06 +00001370/* Iterator functions to run through the chains. */
Marc Bouchere6869a82000-03-20 06:03:29 +00001371const char *
Philip Blundell8c700902000-05-15 02:17:52 +00001372TC_FIRST_CHAIN(TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001373{
Harald Welteaae69be2004-08-29 23:32:14 +00001374 struct chain_head *c = list_entry((*handle)->chains.next,
1375 struct chain_head, list);
1376
1377 iptc_fn = TC_FIRST_CHAIN;
1378
1379
1380 if (list_empty(&(*handle)->chains)) {
1381 DEBUGP(": no chains\n");
Harald Welte0113fe72004-01-06 19:04:02 +00001382 return NULL;
Harald Welteaae69be2004-08-29 23:32:14 +00001383 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001384
Harald Welteaae69be2004-08-29 23:32:14 +00001385 (*handle)->chain_iterator_cur = c;
1386 iptcc_chain_iterator_advance(*handle);
Harald Welte0113fe72004-01-06 19:04:02 +00001387
Harald Welteaae69be2004-08-29 23:32:14 +00001388 DEBUGP(": returning `%s'\n", c->name);
1389 return c->name;
Marc Bouchere6869a82000-03-20 06:03:29 +00001390}
1391
Rusty Russell30fd6e52000-04-23 09:16:06 +00001392/* Iterator functions to run through the chains. Returns NULL at end. */
1393const char *
Rusty Russell79dee072000-05-02 16:45:16 +00001394TC_NEXT_CHAIN(TC_HANDLE_T *handle)
Rusty Russell30fd6e52000-04-23 09:16:06 +00001395{
Harald Welteaae69be2004-08-29 23:32:14 +00001396 struct chain_head *c = (*handle)->chain_iterator_cur;
Rusty Russell30fd6e52000-04-23 09:16:06 +00001397
Harald Welteaae69be2004-08-29 23:32:14 +00001398 iptc_fn = TC_NEXT_CHAIN;
1399
1400 if (!c) {
1401 DEBUGP(": no more chains\n");
Rusty Russell30fd6e52000-04-23 09:16:06 +00001402 return NULL;
Harald Welteaae69be2004-08-29 23:32:14 +00001403 }
Rusty Russell30fd6e52000-04-23 09:16:06 +00001404
Harald Welteaae69be2004-08-29 23:32:14 +00001405 iptcc_chain_iterator_advance(*handle);
1406
1407 DEBUGP(": returning `%s'\n", c->name);
1408 return c->name;
Rusty Russell30fd6e52000-04-23 09:16:06 +00001409}
1410
1411/* Get first rule in the given chain: NULL for empty chain. */
Rusty Russell79dee072000-05-02 16:45:16 +00001412const STRUCT_ENTRY *
Philip Blundell8c700902000-05-15 02:17:52 +00001413TC_FIRST_RULE(const char *chain, TC_HANDLE_T *handle)
Rusty Russell30fd6e52000-04-23 09:16:06 +00001414{
Harald Welteaae69be2004-08-29 23:32:14 +00001415 struct chain_head *c;
1416 struct rule_head *r;
Rusty Russell30fd6e52000-04-23 09:16:06 +00001417
Harald Welteaae69be2004-08-29 23:32:14 +00001418 iptc_fn = TC_FIRST_RULE;
1419
1420 DEBUGP("first rule(%s): ", chain);
1421
1422 c = iptcc_find_label(chain, *handle);
Rusty Russell30fd6e52000-04-23 09:16:06 +00001423 if (!c) {
1424 errno = ENOENT;
1425 return NULL;
1426 }
1427
1428 /* Empty chain: single return/policy rule */
Harald Welteaae69be2004-08-29 23:32:14 +00001429 if (list_empty(&c->rules)) {
1430 DEBUGP_C("no rules, returning NULL\n");
Rusty Russell30fd6e52000-04-23 09:16:06 +00001431 return NULL;
Harald Welteaae69be2004-08-29 23:32:14 +00001432 }
Rusty Russell30fd6e52000-04-23 09:16:06 +00001433
Harald Welteaae69be2004-08-29 23:32:14 +00001434 r = list_entry(c->rules.next, struct rule_head, list);
1435 (*handle)->rule_iterator_cur = r;
1436 DEBUGP_C("%p\n", r);
1437
1438 return r->entry;
Rusty Russell30fd6e52000-04-23 09:16:06 +00001439}
1440
1441/* Returns NULL when rules run out. */
Rusty Russell79dee072000-05-02 16:45:16 +00001442const STRUCT_ENTRY *
Philip Blundell8c700902000-05-15 02:17:52 +00001443TC_NEXT_RULE(const STRUCT_ENTRY *prev, TC_HANDLE_T *handle)
Rusty Russell30fd6e52000-04-23 09:16:06 +00001444{
Harald Welteaae69be2004-08-29 23:32:14 +00001445 struct rule_head *r;
Rusty Russell30fd6e52000-04-23 09:16:06 +00001446
Derrik Pates664c0a32005-02-01 13:28:14 +00001447 iptc_fn = TC_NEXT_RULE;
Harald Welteaae69be2004-08-29 23:32:14 +00001448 DEBUGP("rule_iterator_cur=%p...", (*handle)->rule_iterator_cur);
1449
1450 if (!(*handle)->rule_iterator_cur) {
1451 DEBUGP_C("returning NULL\n");
1452 return NULL;
1453 }
1454
1455 r = list_entry((*handle)->rule_iterator_cur->list.next,
1456 struct rule_head, list);
1457
1458 iptc_fn = TC_NEXT_RULE;
1459
1460 DEBUGP_C("next=%p, head=%p...", &r->list,
1461 &(*handle)->rule_iterator_cur->chain->rules);
1462
1463 if (&r->list == &(*handle)->rule_iterator_cur->chain->rules) {
1464 (*handle)->rule_iterator_cur = NULL;
1465 DEBUGP_C("finished, returning NULL\n");
1466 return NULL;
1467 }
1468
1469 (*handle)->rule_iterator_cur = r;
1470
1471 /* NOTE: prev is without any influence ! */
1472 DEBUGP_C("returning rule %p\n", r);
1473 return r->entry;
Rusty Russell30fd6e52000-04-23 09:16:06 +00001474}
1475
Marc Bouchere6869a82000-03-20 06:03:29 +00001476/* How many rules in this chain? */
1477unsigned int
Rusty Russell79dee072000-05-02 16:45:16 +00001478TC_NUM_RULES(const char *chain, TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001479{
Harald Welteaae69be2004-08-29 23:32:14 +00001480 struct chain_head *c;
1481 iptc_fn = TC_NUM_RULES;
Marc Bouchere6869a82000-03-20 06:03:29 +00001482 CHECK(*handle);
Harald Welteaae69be2004-08-29 23:32:14 +00001483
1484 c = iptcc_find_label(chain, *handle);
1485 if (!c) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001486 errno = ENOENT;
1487 return (unsigned int)-1;
1488 }
Harald Welteaae69be2004-08-29 23:32:14 +00001489
1490 return c->num_rules;
Marc Bouchere6869a82000-03-20 06:03:29 +00001491}
1492
Rusty Russell79dee072000-05-02 16:45:16 +00001493const STRUCT_ENTRY *TC_GET_RULE(const char *chain,
1494 unsigned int n,
1495 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001496{
Harald Welteaae69be2004-08-29 23:32:14 +00001497 struct chain_head *c;
1498 struct rule_head *r;
1499
1500 iptc_fn = TC_GET_RULE;
Marc Bouchere6869a82000-03-20 06:03:29 +00001501
1502 CHECK(*handle);
Harald Welteaae69be2004-08-29 23:32:14 +00001503
1504 c = iptcc_find_label(chain, *handle);
1505 if (!c) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001506 errno = ENOENT;
1507 return NULL;
1508 }
1509
Harald Welteaae69be2004-08-29 23:32:14 +00001510 r = iptcc_get_rule_num(c, n);
1511 if (!r)
1512 return NULL;
1513 return r->entry;
Marc Bouchere6869a82000-03-20 06:03:29 +00001514}
1515
1516/* Returns a pointer to the target name of this position. */
Harald Welteaae69be2004-08-29 23:32:14 +00001517const char *standard_target_map(int verdict)
Marc Bouchere6869a82000-03-20 06:03:29 +00001518{
Harald Welteaae69be2004-08-29 23:32:14 +00001519 switch (verdict) {
1520 case RETURN:
1521 return LABEL_RETURN;
1522 break;
1523 case -NF_ACCEPT-1:
1524 return LABEL_ACCEPT;
1525 break;
1526 case -NF_DROP-1:
1527 return LABEL_DROP;
1528 break;
1529 case -NF_QUEUE-1:
1530 return LABEL_QUEUE;
1531 break;
1532 default:
1533 fprintf(stderr, "ERROR: %d not a valid target)\n",
1534 verdict);
1535 abort();
1536 break;
1537 }
1538 /* not reached */
1539 return NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +00001540}
1541
Harald Welteaae69be2004-08-29 23:32:14 +00001542/* Returns a pointer to the target name of this position. */
1543const char *TC_GET_TARGET(const STRUCT_ENTRY *ce,
1544 TC_HANDLE_T *handle)
1545{
1546 STRUCT_ENTRY *e = (STRUCT_ENTRY *)ce;
Rusty Russelle45c7132004-12-16 13:21:44 +00001547 struct rule_head *r = container_of(e, struct rule_head, entry[0]);
Harald Welteaae69be2004-08-29 23:32:14 +00001548
1549 iptc_fn = TC_GET_TARGET;
1550
1551 switch(r->type) {
1552 int spos;
1553 case IPTCC_R_FALLTHROUGH:
1554 return "";
1555 break;
1556 case IPTCC_R_JUMP:
1557 DEBUGP("r=%p, jump=%p, name=`%s'\n", r, r->jump, r->jump->name);
1558 return r->jump->name;
1559 break;
1560 case IPTCC_R_STANDARD:
1561 spos = *(int *)GET_TARGET(e)->data;
1562 DEBUGP("r=%p, spos=%d'\n", r, spos);
1563 return standard_target_map(spos);
1564 break;
1565 case IPTCC_R_MODULE:
1566 return GET_TARGET(e)->u.user.name;
1567 break;
1568 }
1569 return NULL;
1570}
Marc Bouchere6869a82000-03-20 06:03:29 +00001571/* Is this a built-in chain? Actually returns hook + 1. */
1572int
Rusty Russell79dee072000-05-02 16:45:16 +00001573TC_BUILTIN(const char *chain, const TC_HANDLE_T handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001574{
Harald Welteaae69be2004-08-29 23:32:14 +00001575 struct chain_head *c;
1576
1577 iptc_fn = TC_BUILTIN;
Marc Bouchere6869a82000-03-20 06:03:29 +00001578
Harald Welteaae69be2004-08-29 23:32:14 +00001579 c = iptcc_find_label(chain, handle);
1580 if (!c) {
1581 errno = ENOENT;
Martin Josefssonb0f3d2d2004-09-23 18:23:20 +00001582 return 0;
Marc Bouchere6869a82000-03-20 06:03:29 +00001583 }
Harald Welteaae69be2004-08-29 23:32:14 +00001584
1585 return iptcc_is_builtin(c);
Marc Bouchere6869a82000-03-20 06:03:29 +00001586}
1587
1588/* Get the policy of a given built-in chain */
1589const char *
Rusty Russell79dee072000-05-02 16:45:16 +00001590TC_GET_POLICY(const char *chain,
1591 STRUCT_COUNTERS *counters,
1592 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001593{
Harald Welteaae69be2004-08-29 23:32:14 +00001594 struct chain_head *c;
Marc Bouchere6869a82000-03-20 06:03:29 +00001595
Harald Welteaae69be2004-08-29 23:32:14 +00001596 iptc_fn = TC_GET_POLICY;
1597
1598 DEBUGP("called for chain %s\n", chain);
1599
1600 c = iptcc_find_label(chain, *handle);
1601 if (!c) {
1602 errno = ENOENT;
1603 return NULL;
1604 }
1605
1606 if (!iptcc_is_builtin(c))
Marc Bouchere6869a82000-03-20 06:03:29 +00001607 return NULL;
1608
Harald Welteaae69be2004-08-29 23:32:14 +00001609 *counters = c->counters;
Marc Bouchere6869a82000-03-20 06:03:29 +00001610
Harald Welteaae69be2004-08-29 23:32:14 +00001611 return standard_target_map(c->verdict);
Harald Welte0113fe72004-01-06 19:04:02 +00001612}
1613
1614static int
Harald Welteaae69be2004-08-29 23:32:14 +00001615iptcc_standard_map(struct rule_head *r, int verdict)
Harald Welte0113fe72004-01-06 19:04:02 +00001616{
Harald Welteaae69be2004-08-29 23:32:14 +00001617 STRUCT_ENTRY *e = r->entry;
Rusty Russell79dee072000-05-02 16:45:16 +00001618 STRUCT_STANDARD_TARGET *t;
Marc Bouchere6869a82000-03-20 06:03:29 +00001619
Rusty Russell79dee072000-05-02 16:45:16 +00001620 t = (STRUCT_STANDARD_TARGET *)GET_TARGET(e);
Marc Bouchere6869a82000-03-20 06:03:29 +00001621
Rusty Russell67088e72000-05-10 01:18:57 +00001622 if (t->target.u.target_size
Philip Blundell8c700902000-05-15 02:17:52 +00001623 != ALIGN(sizeof(STRUCT_STANDARD_TARGET))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001624 errno = EINVAL;
1625 return 0;
1626 }
1627 /* memset for memcmp convenience on delete/replace */
Rusty Russell79dee072000-05-02 16:45:16 +00001628 memset(t->target.u.user.name, 0, FUNCTION_MAXNAMELEN);
1629 strcpy(t->target.u.user.name, STANDARD_TARGET);
Marc Bouchere6869a82000-03-20 06:03:29 +00001630 t->verdict = verdict;
1631
Harald Welteaae69be2004-08-29 23:32:14 +00001632 r->type = IPTCC_R_STANDARD;
1633
Marc Bouchere6869a82000-03-20 06:03:29 +00001634 return 1;
1635}
Rusty Russell7e53bf92000-03-20 07:03:28 +00001636
Marc Bouchere6869a82000-03-20 06:03:29 +00001637static int
Harald Welteaae69be2004-08-29 23:32:14 +00001638iptcc_map_target(const TC_HANDLE_T handle,
1639 struct rule_head *r)
Marc Bouchere6869a82000-03-20 06:03:29 +00001640{
Harald Welteaae69be2004-08-29 23:32:14 +00001641 STRUCT_ENTRY *e = r->entry;
Harald Welte0113fe72004-01-06 19:04:02 +00001642 STRUCT_ENTRY_TARGET *t = GET_TARGET(e);
Marc Bouchere6869a82000-03-20 06:03:29 +00001643
Marc Bouchere6869a82000-03-20 06:03:29 +00001644 /* Maybe it's empty (=> fall through) */
Harald Welteaae69be2004-08-29 23:32:14 +00001645 if (strcmp(t->u.user.name, "") == 0) {
1646 r->type = IPTCC_R_FALLTHROUGH;
1647 return 1;
1648 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001649 /* Maybe it's a standard target name... */
Rusty Russell79dee072000-05-02 16:45:16 +00001650 else if (strcmp(t->u.user.name, LABEL_ACCEPT) == 0)
Harald Welteaae69be2004-08-29 23:32:14 +00001651 return iptcc_standard_map(r, -NF_ACCEPT - 1);
Rusty Russell79dee072000-05-02 16:45:16 +00001652 else if (strcmp(t->u.user.name, LABEL_DROP) == 0)
Harald Welteaae69be2004-08-29 23:32:14 +00001653 return iptcc_standard_map(r, -NF_DROP - 1);
Rusty Russell67088e72000-05-10 01:18:57 +00001654 else if (strcmp(t->u.user.name, LABEL_QUEUE) == 0)
Harald Welteaae69be2004-08-29 23:32:14 +00001655 return iptcc_standard_map(r, -NF_QUEUE - 1);
Rusty Russell79dee072000-05-02 16:45:16 +00001656 else if (strcmp(t->u.user.name, LABEL_RETURN) == 0)
Harald Welteaae69be2004-08-29 23:32:14 +00001657 return iptcc_standard_map(r, RETURN);
Rusty Russell79dee072000-05-02 16:45:16 +00001658 else if (TC_BUILTIN(t->u.user.name, handle)) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001659 /* Can't jump to builtins. */
1660 errno = EINVAL;
1661 return 0;
1662 } else {
1663 /* Maybe it's an existing chain name. */
Harald Welteaae69be2004-08-29 23:32:14 +00001664 struct chain_head *c;
1665 DEBUGP("trying to find chain `%s': ", t->u.user.name);
Marc Bouchere6869a82000-03-20 06:03:29 +00001666
Harald Welteaae69be2004-08-29 23:32:14 +00001667 c = iptcc_find_label(t->u.user.name, handle);
1668 if (c) {
1669 DEBUGP_C("found!\n");
1670 r->type = IPTCC_R_JUMP;
1671 r->jump = c;
1672 c->references++;
1673 return 1;
1674 }
1675 DEBUGP_C("not found :(\n");
Marc Bouchere6869a82000-03-20 06:03:29 +00001676 }
1677
1678 /* Must be a module? If not, kernel will reject... */
Rusty Russell3aef54d2005-01-03 03:48:40 +00001679 /* memset to all 0 for your memcmp convenience: don't clear version */
Rusty Russell228e98d2000-04-27 10:28:06 +00001680 memset(t->u.user.name + strlen(t->u.user.name),
Marc Bouchere6869a82000-03-20 06:03:29 +00001681 0,
Rusty Russell3aef54d2005-01-03 03:48:40 +00001682 FUNCTION_MAXNAMELEN - 1 - strlen(t->u.user.name));
Rusty Russell733e54b2004-12-16 14:22:23 +00001683 r->type = IPTCC_R_MODULE;
Harald Welteaae69be2004-08-29 23:32:14 +00001684 set_changed(handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001685 return 1;
1686}
1687
Harald Welte0113fe72004-01-06 19:04:02 +00001688/* Insert the entry `fw' in chain `chain' into position `rulenum'. */
Marc Bouchere6869a82000-03-20 06:03:29 +00001689int
Rusty Russell79dee072000-05-02 16:45:16 +00001690TC_INSERT_ENTRY(const IPT_CHAINLABEL chain,
1691 const STRUCT_ENTRY *e,
1692 unsigned int rulenum,
1693 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001694{
Harald Welteaae69be2004-08-29 23:32:14 +00001695 struct chain_head *c;
Martin Josefssoneb066cc2004-09-22 21:04:07 +00001696 struct rule_head *r;
1697 struct list_head *prev;
Marc Bouchere6869a82000-03-20 06:03:29 +00001698
Rusty Russell79dee072000-05-02 16:45:16 +00001699 iptc_fn = TC_INSERT_ENTRY;
Harald Welteaae69be2004-08-29 23:32:14 +00001700
1701 if (!(c = iptcc_find_label(chain, *handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001702 errno = ENOENT;
1703 return 0;
1704 }
1705
Martin Josefssoneb066cc2004-09-22 21:04:07 +00001706 /* first rulenum index = 0
1707 first c->num_rules index = 1 */
1708 if (rulenum > c->num_rules) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001709 errno = E2BIG;
1710 return 0;
1711 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001712
Martin Josefsson631f3612004-09-23 19:25:06 +00001713 /* If we are inserting at the end just take advantage of the
1714 double linked list, insert will happen before the entry
1715 prev points to. */
1716 if (rulenum == c->num_rules) {
Martin Josefssoneb066cc2004-09-22 21:04:07 +00001717 prev = &c->rules;
Martin Josefssona5616dc2004-10-24 22:27:31 +00001718 } else if (rulenum + 1 <= c->num_rules/2) {
Martin Josefsson631f3612004-09-23 19:25:06 +00001719 r = iptcc_get_rule_num(c, rulenum + 1);
Martin Josefssona5616dc2004-10-24 22:27:31 +00001720 prev = &r->list;
1721 } else {
1722 r = iptcc_get_rule_num_reverse(c, c->num_rules - rulenum);
Martin Josefsson631f3612004-09-23 19:25:06 +00001723 prev = &r->list;
1724 }
Martin Josefssoneb066cc2004-09-22 21:04:07 +00001725
Harald Welteaae69be2004-08-29 23:32:14 +00001726 if (!(r = iptcc_alloc_rule(c, e->next_offset))) {
1727 errno = ENOMEM;
Harald Welte0113fe72004-01-06 19:04:02 +00001728 return 0;
Harald Welteaae69be2004-08-29 23:32:14 +00001729 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001730
Harald Welteaae69be2004-08-29 23:32:14 +00001731 memcpy(r->entry, e, e->next_offset);
1732 r->counter_map.maptype = COUNTER_MAP_SET;
1733
1734 if (!iptcc_map_target(*handle, r)) {
1735 free(r);
1736 return 0;
1737 }
1738
Martin Josefssoneb066cc2004-09-22 21:04:07 +00001739 list_add_tail(&r->list, prev);
Harald Welteaae69be2004-08-29 23:32:14 +00001740 c->num_rules++;
1741
1742 set_changed(*handle);
1743
1744 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00001745}
1746
1747/* Atomically replace rule `rulenum' in `chain' with `fw'. */
1748int
Rusty Russell79dee072000-05-02 16:45:16 +00001749TC_REPLACE_ENTRY(const IPT_CHAINLABEL chain,
1750 const STRUCT_ENTRY *e,
1751 unsigned int rulenum,
1752 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001753{
Harald Welteaae69be2004-08-29 23:32:14 +00001754 struct chain_head *c;
1755 struct rule_head *r, *old;
Marc Bouchere6869a82000-03-20 06:03:29 +00001756
Rusty Russell79dee072000-05-02 16:45:16 +00001757 iptc_fn = TC_REPLACE_ENTRY;
Marc Bouchere6869a82000-03-20 06:03:29 +00001758
Harald Welteaae69be2004-08-29 23:32:14 +00001759 if (!(c = iptcc_find_label(chain, *handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001760 errno = ENOENT;
1761 return 0;
1762 }
1763
Martin Josefsson0f9b8b12004-12-18 17:18:49 +00001764 if (rulenum >= c->num_rules) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001765 errno = E2BIG;
1766 return 0;
1767 }
1768
Martin Josefsson0f9b8b12004-12-18 17:18:49 +00001769 /* Take advantage of the double linked list if possible. */
1770 if (rulenum + 1 <= c->num_rules/2) {
1771 old = iptcc_get_rule_num(c, rulenum + 1);
1772 } else {
1773 old = iptcc_get_rule_num_reverse(c, c->num_rules - rulenum);
1774 }
1775
Harald Welteaae69be2004-08-29 23:32:14 +00001776 if (!(r = iptcc_alloc_rule(c, e->next_offset))) {
1777 errno = ENOMEM;
Marc Bouchere6869a82000-03-20 06:03:29 +00001778 return 0;
Harald Welteaae69be2004-08-29 23:32:14 +00001779 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001780
Harald Welteaae69be2004-08-29 23:32:14 +00001781 memcpy(r->entry, e, e->next_offset);
1782 r->counter_map.maptype = COUNTER_MAP_SET;
1783
1784 if (!iptcc_map_target(*handle, r)) {
1785 free(r);
Harald Welte0113fe72004-01-06 19:04:02 +00001786 return 0;
Harald Welteaae69be2004-08-29 23:32:14 +00001787 }
Harald Welte0113fe72004-01-06 19:04:02 +00001788
Harald Welteaae69be2004-08-29 23:32:14 +00001789 list_add(&r->list, &old->list);
1790 iptcc_delete_rule(old);
1791
1792 set_changed(*handle);
1793
1794 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00001795}
1796
Harald Welte0113fe72004-01-06 19:04:02 +00001797/* Append entry `fw' to chain `chain'. Equivalent to insert with
Marc Bouchere6869a82000-03-20 06:03:29 +00001798 rulenum = length of chain. */
1799int
Rusty Russell79dee072000-05-02 16:45:16 +00001800TC_APPEND_ENTRY(const IPT_CHAINLABEL chain,
1801 const STRUCT_ENTRY *e,
1802 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001803{
Harald Welteaae69be2004-08-29 23:32:14 +00001804 struct chain_head *c;
1805 struct rule_head *r;
Marc Bouchere6869a82000-03-20 06:03:29 +00001806
Rusty Russell79dee072000-05-02 16:45:16 +00001807 iptc_fn = TC_APPEND_ENTRY;
Harald Welteaae69be2004-08-29 23:32:14 +00001808 if (!(c = iptcc_find_label(chain, *handle))) {
1809 DEBUGP("unable to find chain `%s'\n", chain);
Marc Bouchere6869a82000-03-20 06:03:29 +00001810 errno = ENOENT;
1811 return 0;
1812 }
1813
Harald Welteaae69be2004-08-29 23:32:14 +00001814 if (!(r = iptcc_alloc_rule(c, e->next_offset))) {
1815 DEBUGP("unable to allocate rule for chain `%s'\n", chain);
1816 errno = ENOMEM;
Harald Welte0113fe72004-01-06 19:04:02 +00001817 return 0;
Harald Welteaae69be2004-08-29 23:32:14 +00001818 }
Harald Welte0113fe72004-01-06 19:04:02 +00001819
Harald Welteaae69be2004-08-29 23:32:14 +00001820 memcpy(r->entry, e, e->next_offset);
1821 r->counter_map.maptype = COUNTER_MAP_SET;
1822
1823 if (!iptcc_map_target(*handle, r)) {
Martin Josefsson12009532004-09-23 18:24:29 +00001824 DEBUGP("unable to map target of rule for chain `%s'\n", chain);
Harald Welteaae69be2004-08-29 23:32:14 +00001825 free(r);
1826 return 0;
1827 }
1828
1829 list_add_tail(&r->list, &c->rules);
1830 c->num_rules++;
1831
1832 set_changed(*handle);
1833
1834 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00001835}
1836
1837static inline int
Rusty Russell79dee072000-05-02 16:45:16 +00001838match_different(const STRUCT_ENTRY_MATCH *a,
Rusty Russelledf14cf2000-04-19 11:26:44 +00001839 const unsigned char *a_elems,
1840 const unsigned char *b_elems,
1841 unsigned char **maskptr)
Marc Bouchere6869a82000-03-20 06:03:29 +00001842{
Rusty Russell79dee072000-05-02 16:45:16 +00001843 const STRUCT_ENTRY_MATCH *b;
Rusty Russelledf14cf2000-04-19 11:26:44 +00001844 unsigned int i;
Marc Bouchere6869a82000-03-20 06:03:29 +00001845
1846 /* Offset of b is the same as a. */
Rusty Russell30fd6e52000-04-23 09:16:06 +00001847 b = (void *)b_elems + ((unsigned char *)a - a_elems);
Marc Bouchere6869a82000-03-20 06:03:29 +00001848
Rusty Russell228e98d2000-04-27 10:28:06 +00001849 if (a->u.match_size != b->u.match_size)
Marc Bouchere6869a82000-03-20 06:03:29 +00001850 return 1;
1851
Rusty Russell228e98d2000-04-27 10:28:06 +00001852 if (strcmp(a->u.user.name, b->u.user.name) != 0)
Marc Bouchere6869a82000-03-20 06:03:29 +00001853 return 1;
1854
Rusty Russell73ef09b2000-07-03 10:24:04 +00001855 *maskptr += ALIGN(sizeof(*a));
Rusty Russelledf14cf2000-04-19 11:26:44 +00001856
Rusty Russell73ef09b2000-07-03 10:24:04 +00001857 for (i = 0; i < a->u.match_size - ALIGN(sizeof(*a)); i++)
Rusty Russelledf14cf2000-04-19 11:26:44 +00001858 if (((a->data[i] ^ b->data[i]) & (*maskptr)[i]) != 0)
Rusty Russell90e712a2000-03-29 04:19:26 +00001859 return 1;
Rusty Russelledf14cf2000-04-19 11:26:44 +00001860 *maskptr += i;
1861 return 0;
1862}
1863
1864static inline int
Rusty Russell733e54b2004-12-16 14:22:23 +00001865target_same(struct rule_head *a, struct rule_head *b,const unsigned char *mask)
Rusty Russelledf14cf2000-04-19 11:26:44 +00001866{
1867 unsigned int i;
Rusty Russell733e54b2004-12-16 14:22:23 +00001868 STRUCT_ENTRY_TARGET *ta, *tb;
Marc Bouchere6869a82000-03-20 06:03:29 +00001869
Rusty Russell733e54b2004-12-16 14:22:23 +00001870 if (a->type != b->type)
1871 return 0;
1872
1873 ta = GET_TARGET(a->entry);
1874 tb = GET_TARGET(b->entry);
1875
1876 switch (a->type) {
1877 case IPTCC_R_FALLTHROUGH:
1878 return 1;
1879 case IPTCC_R_JUMP:
1880 return a->jump == b->jump;
1881 case IPTCC_R_STANDARD:
1882 return ((STRUCT_STANDARD_TARGET *)ta)->verdict
1883 == ((STRUCT_STANDARD_TARGET *)tb)->verdict;
1884 case IPTCC_R_MODULE:
1885 if (ta->u.target_size != tb->u.target_size)
1886 return 0;
1887 if (strcmp(ta->u.user.name, tb->u.user.name) != 0)
1888 return 0;
1889
1890 for (i = 0; i < ta->u.target_size - sizeof(*ta); i++)
Rusty Russelldaade442004-12-29 11:14:52 +00001891 if (((ta->data[i] ^ tb->data[i]) & mask[i]) != 0)
Rusty Russell733e54b2004-12-16 14:22:23 +00001892 return 0;
1893 return 1;
1894 default:
1895 fprintf(stderr, "ERROR: bad type %i\n", a->type);
1896 abort();
1897 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001898}
1899
Rusty Russell733e54b2004-12-16 14:22:23 +00001900static unsigned char *
Rusty Russell79dee072000-05-02 16:45:16 +00001901is_same(const STRUCT_ENTRY *a,
1902 const STRUCT_ENTRY *b,
1903 unsigned char *matchmask);
Marc Bouchere6869a82000-03-20 06:03:29 +00001904
Harald Welte0113fe72004-01-06 19:04:02 +00001905/* Delete the first rule in `chain' which matches `fw'. */
Marc Bouchere6869a82000-03-20 06:03:29 +00001906int
Rusty Russell79dee072000-05-02 16:45:16 +00001907TC_DELETE_ENTRY(const IPT_CHAINLABEL chain,
1908 const STRUCT_ENTRY *origfw,
1909 unsigned char *matchmask,
1910 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001911{
Harald Welteaae69be2004-08-29 23:32:14 +00001912 struct chain_head *c;
Rusty Russelle45c7132004-12-16 13:21:44 +00001913 struct rule_head *r, *i;
Marc Bouchere6869a82000-03-20 06:03:29 +00001914
Rusty Russell79dee072000-05-02 16:45:16 +00001915 iptc_fn = TC_DELETE_ENTRY;
Harald Welteaae69be2004-08-29 23:32:14 +00001916 if (!(c = iptcc_find_label(chain, *handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001917 errno = ENOENT;
1918 return 0;
1919 }
1920
Rusty Russelle45c7132004-12-16 13:21:44 +00001921 /* Create a rule_head from origfw. */
1922 r = iptcc_alloc_rule(c, origfw->next_offset);
1923 if (!r) {
Harald Welte0113fe72004-01-06 19:04:02 +00001924 errno = ENOMEM;
1925 return 0;
1926 }
1927
Rusty Russelle45c7132004-12-16 13:21:44 +00001928 memcpy(r->entry, origfw, origfw->next_offset);
1929 r->counter_map.maptype = COUNTER_MAP_NOMAP;
1930 if (!iptcc_map_target(*handle, r)) {
1931 DEBUGP("unable to map target of rule for chain `%s'\n", chain);
1932 free(r);
1933 return 0;
Patrick McHardyJesper Brouer04a1e4c2006-07-25 01:50:48 +00001934 } else {
1935 /* iptcc_map_target increment target chain references
1936 * since this is a fake rule only used for matching
1937 * the chain references count is decremented again.
1938 */
1939 if (r->type == IPTCC_R_JUMP
1940 && r->jump)
1941 r->jump->references--;
Rusty Russelle45c7132004-12-16 13:21:44 +00001942 }
Harald Welte0113fe72004-01-06 19:04:02 +00001943
Rusty Russelle45c7132004-12-16 13:21:44 +00001944 list_for_each_entry(i, &c->rules, list) {
Rusty Russell733e54b2004-12-16 14:22:23 +00001945 unsigned char *mask;
Harald Weltefe537072004-08-30 20:28:53 +00001946
Rusty Russell733e54b2004-12-16 14:22:23 +00001947 mask = is_same(r->entry, i->entry, matchmask);
1948 if (!mask)
1949 continue;
Martin Josefsson2a5dbbb2004-09-22 21:37:41 +00001950
Rusty Russell733e54b2004-12-16 14:22:23 +00001951 if (!target_same(r, i, mask))
1952 continue;
1953
1954 /* If we are about to delete the rule that is the
1955 * current iterator, move rule iterator back. next
1956 * pointer will then point to real next node */
1957 if (i == (*handle)->rule_iterator_cur) {
1958 (*handle)->rule_iterator_cur =
1959 list_entry((*handle)->rule_iterator_cur->list.prev,
1960 struct rule_head, list);
Marc Bouchere6869a82000-03-20 06:03:29 +00001961 }
Rusty Russell733e54b2004-12-16 14:22:23 +00001962
1963 c->num_rules--;
1964 iptcc_delete_rule(i);
1965
1966 set_changed(*handle);
1967 free(r);
1968 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00001969 }
1970
Rusty Russelle45c7132004-12-16 13:21:44 +00001971 free(r);
Marc Bouchere6869a82000-03-20 06:03:29 +00001972 errno = ENOENT;
1973 return 0;
Rusty Russell7e53bf92000-03-20 07:03:28 +00001974}
Harald Welteaae69be2004-08-29 23:32:14 +00001975
Marc Bouchere6869a82000-03-20 06:03:29 +00001976
1977/* Delete the rule in position `rulenum' in `chain'. */
1978int
Rusty Russell79dee072000-05-02 16:45:16 +00001979TC_DELETE_NUM_ENTRY(const IPT_CHAINLABEL chain,
1980 unsigned int rulenum,
1981 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001982{
Harald Welteaae69be2004-08-29 23:32:14 +00001983 struct chain_head *c;
1984 struct rule_head *r;
Marc Bouchere6869a82000-03-20 06:03:29 +00001985
Rusty Russell79dee072000-05-02 16:45:16 +00001986 iptc_fn = TC_DELETE_NUM_ENTRY;
Harald Welteaae69be2004-08-29 23:32:14 +00001987
1988 if (!(c = iptcc_find_label(chain, *handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001989 errno = ENOENT;
1990 return 0;
1991 }
1992
Martin Josefssona5616dc2004-10-24 22:27:31 +00001993 if (rulenum >= c->num_rules) {
Martin Josefsson631f3612004-09-23 19:25:06 +00001994 errno = E2BIG;
1995 return 0;
1996 }
1997
1998 /* Take advantage of the double linked list if possible. */
Martin Josefssona5616dc2004-10-24 22:27:31 +00001999 if (rulenum + 1 <= c->num_rules/2) {
2000 r = iptcc_get_rule_num(c, rulenum + 1);
2001 } else {
2002 r = iptcc_get_rule_num_reverse(c, c->num_rules - rulenum);
Marc Bouchere6869a82000-03-20 06:03:29 +00002003 }
2004
Harald Welteaae69be2004-08-29 23:32:14 +00002005 /* If we are about to delete the rule that is the current
2006 * iterator, move rule iterator back. next pointer will then
2007 * point to real next node */
2008 if (r == (*handle)->rule_iterator_cur) {
2009 (*handle)->rule_iterator_cur =
2010 list_entry((*handle)->rule_iterator_cur->list.prev,
2011 struct rule_head, list);
Harald Welte0113fe72004-01-06 19:04:02 +00002012 }
Marc Bouchere6869a82000-03-20 06:03:29 +00002013
Harald Welteaae69be2004-08-29 23:32:14 +00002014 c->num_rules--;
2015 iptcc_delete_rule(r);
2016
Martin Josefsson2a5dbbb2004-09-22 21:37:41 +00002017 set_changed(*handle);
2018
Harald Welteaae69be2004-08-29 23:32:14 +00002019 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00002020}
2021
2022/* Check the packet `fw' on chain `chain'. Returns the verdict, or
2023 NULL and sets errno. */
2024const char *
Rusty Russell79dee072000-05-02 16:45:16 +00002025TC_CHECK_PACKET(const IPT_CHAINLABEL chain,
2026 STRUCT_ENTRY *entry,
2027 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00002028{
Derrik Pates664c0a32005-02-01 13:28:14 +00002029 iptc_fn = TC_CHECK_PACKET;
Marc Bouchere6869a82000-03-20 06:03:29 +00002030 errno = ENOSYS;
2031 return NULL;
2032}
2033
2034/* Flushes the entries in the given chain (ie. empties chain). */
2035int
Rusty Russell79dee072000-05-02 16:45:16 +00002036TC_FLUSH_ENTRIES(const IPT_CHAINLABEL chain, TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00002037{
Harald Welteaae69be2004-08-29 23:32:14 +00002038 struct chain_head *c;
2039 struct rule_head *r, *tmp;
Marc Bouchere6869a82000-03-20 06:03:29 +00002040
Harald Welte0113fe72004-01-06 19:04:02 +00002041 iptc_fn = TC_FLUSH_ENTRIES;
Harald Welteaae69be2004-08-29 23:32:14 +00002042 if (!(c = iptcc_find_label(chain, *handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002043 errno = ENOENT;
2044 return 0;
2045 }
Marc Bouchere6869a82000-03-20 06:03:29 +00002046
Harald Welteaae69be2004-08-29 23:32:14 +00002047 list_for_each_entry_safe(r, tmp, &c->rules, list) {
2048 iptcc_delete_rule(r);
2049 }
2050
2051 c->num_rules = 0;
2052
2053 set_changed(*handle);
2054
2055 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00002056}
2057
2058/* Zeroes the counters in a chain. */
2059int
Rusty Russell79dee072000-05-02 16:45:16 +00002060TC_ZERO_ENTRIES(const IPT_CHAINLABEL chain, TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00002061{
Harald Welteaae69be2004-08-29 23:32:14 +00002062 struct chain_head *c;
2063 struct rule_head *r;
Rusty Russell7e53bf92000-03-20 07:03:28 +00002064
Derrik Pates664c0a32005-02-01 13:28:14 +00002065 iptc_fn = TC_ZERO_ENTRIES;
Harald Welteaae69be2004-08-29 23:32:14 +00002066 if (!(c = iptcc_find_label(chain, *handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002067 errno = ENOENT;
2068 return 0;
2069 }
Marc Bouchere6869a82000-03-20 06:03:29 +00002070
Andy Gaye5bd1d72006-08-22 02:56:41 +00002071 if (c->counter_map.maptype == COUNTER_MAP_NORMAL_MAP)
2072 c->counter_map.maptype = COUNTER_MAP_ZEROED;
2073
Harald Welteaae69be2004-08-29 23:32:14 +00002074 list_for_each_entry(r, &c->rules, list) {
2075 if (r->counter_map.maptype == COUNTER_MAP_NORMAL_MAP)
2076 r->counter_map.maptype = COUNTER_MAP_ZEROED;
Marc Bouchere6869a82000-03-20 06:03:29 +00002077 }
Harald Welteaae69be2004-08-29 23:32:14 +00002078
Rusty Russell175f6412000-03-24 09:32:20 +00002079 set_changed(*handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00002080
Marc Bouchere6869a82000-03-20 06:03:29 +00002081 return 1;
2082}
2083
Harald Welte1cef74d2001-01-05 15:22:59 +00002084STRUCT_COUNTERS *
2085TC_READ_COUNTER(const IPT_CHAINLABEL chain,
2086 unsigned int rulenum,
2087 TC_HANDLE_T *handle)
2088{
Harald Welteaae69be2004-08-29 23:32:14 +00002089 struct chain_head *c;
2090 struct rule_head *r;
Harald Welte1cef74d2001-01-05 15:22:59 +00002091
2092 iptc_fn = TC_READ_COUNTER;
2093 CHECK(*handle);
2094
Harald Welteaae69be2004-08-29 23:32:14 +00002095 if (!(c = iptcc_find_label(chain, *handle))) {
Harald Welte1cef74d2001-01-05 15:22:59 +00002096 errno = ENOENT;
2097 return NULL;
2098 }
2099
Harald Welteaae69be2004-08-29 23:32:14 +00002100 if (!(r = iptcc_get_rule_num(c, rulenum))) {
Harald Welte0113fe72004-01-06 19:04:02 +00002101 errno = E2BIG;
2102 return NULL;
2103 }
2104
Harald Welteaae69be2004-08-29 23:32:14 +00002105 return &r->entry[0].counters;
Harald Welte1cef74d2001-01-05 15:22:59 +00002106}
2107
2108int
2109TC_ZERO_COUNTER(const IPT_CHAINLABEL chain,
2110 unsigned int rulenum,
2111 TC_HANDLE_T *handle)
2112{
Harald Welteaae69be2004-08-29 23:32:14 +00002113 struct chain_head *c;
2114 struct rule_head *r;
Harald Welte1cef74d2001-01-05 15:22:59 +00002115
2116 iptc_fn = TC_ZERO_COUNTER;
2117 CHECK(*handle);
2118
Harald Welteaae69be2004-08-29 23:32:14 +00002119 if (!(c = iptcc_find_label(chain, *handle))) {
Harald Welte1cef74d2001-01-05 15:22:59 +00002120 errno = ENOENT;
2121 return 0;
2122 }
2123
Harald Welteaae69be2004-08-29 23:32:14 +00002124 if (!(r = iptcc_get_rule_num(c, rulenum))) {
Harald Welte0113fe72004-01-06 19:04:02 +00002125 errno = E2BIG;
2126 return 0;
2127 }
2128
Harald Welteaae69be2004-08-29 23:32:14 +00002129 if (r->counter_map.maptype == COUNTER_MAP_NORMAL_MAP)
2130 r->counter_map.maptype = COUNTER_MAP_ZEROED;
Harald Welte1cef74d2001-01-05 15:22:59 +00002131
2132 set_changed(*handle);
2133
2134 return 1;
2135}
2136
2137int
2138TC_SET_COUNTER(const IPT_CHAINLABEL chain,
2139 unsigned int rulenum,
2140 STRUCT_COUNTERS *counters,
2141 TC_HANDLE_T *handle)
2142{
Harald Welteaae69be2004-08-29 23:32:14 +00002143 struct chain_head *c;
2144 struct rule_head *r;
Harald Welte1cef74d2001-01-05 15:22:59 +00002145 STRUCT_ENTRY *e;
Harald Welte1cef74d2001-01-05 15:22:59 +00002146
2147 iptc_fn = TC_SET_COUNTER;
2148 CHECK(*handle);
2149
Harald Welteaae69be2004-08-29 23:32:14 +00002150 if (!(c = iptcc_find_label(chain, *handle))) {
Harald Welte1cef74d2001-01-05 15:22:59 +00002151 errno = ENOENT;
2152 return 0;
2153 }
Harald Welte0113fe72004-01-06 19:04:02 +00002154
Harald Welteaae69be2004-08-29 23:32:14 +00002155 if (!(r = iptcc_get_rule_num(c, rulenum))) {
Harald Welte0113fe72004-01-06 19:04:02 +00002156 errno = E2BIG;
2157 return 0;
2158 }
2159
Harald Welteaae69be2004-08-29 23:32:14 +00002160 e = r->entry;
2161 r->counter_map.maptype = COUNTER_MAP_SET;
Harald Welte0113fe72004-01-06 19:04:02 +00002162
2163 memcpy(&e->counters, counters, sizeof(STRUCT_COUNTERS));
Harald Welte1cef74d2001-01-05 15:22:59 +00002164
2165 set_changed(*handle);
2166
2167 return 1;
2168}
2169
Marc Bouchere6869a82000-03-20 06:03:29 +00002170/* Creates a new chain. */
2171/* To create a chain, create two rules: error node and unconditional
2172 * return. */
2173int
Rusty Russell79dee072000-05-02 16:45:16 +00002174TC_CREATE_CHAIN(const IPT_CHAINLABEL chain, TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00002175{
Harald Welteaae69be2004-08-29 23:32:14 +00002176 static struct chain_head *c;
Marc Bouchere6869a82000-03-20 06:03:29 +00002177
Rusty Russell79dee072000-05-02 16:45:16 +00002178 iptc_fn = TC_CREATE_CHAIN;
Marc Bouchere6869a82000-03-20 06:03:29 +00002179
2180 /* find_label doesn't cover built-in targets: DROP, ACCEPT,
2181 QUEUE, RETURN. */
Harald Welteaae69be2004-08-29 23:32:14 +00002182 if (iptcc_find_label(chain, *handle)
Rusty Russell79dee072000-05-02 16:45:16 +00002183 || strcmp(chain, LABEL_DROP) == 0
2184 || strcmp(chain, LABEL_ACCEPT) == 0
Rusty Russell67088e72000-05-10 01:18:57 +00002185 || strcmp(chain, LABEL_QUEUE) == 0
Rusty Russell79dee072000-05-02 16:45:16 +00002186 || strcmp(chain, LABEL_RETURN) == 0) {
Harald Welteaae69be2004-08-29 23:32:14 +00002187 DEBUGP("Chain `%s' already exists\n", chain);
Marc Bouchere6869a82000-03-20 06:03:29 +00002188 errno = EEXIST;
2189 return 0;
2190 }
2191
Rusty Russell79dee072000-05-02 16:45:16 +00002192 if (strlen(chain)+1 > sizeof(IPT_CHAINLABEL)) {
Harald Welteaae69be2004-08-29 23:32:14 +00002193 DEBUGP("Chain name `%s' too long\n", chain);
Marc Bouchere6869a82000-03-20 06:03:29 +00002194 errno = EINVAL;
2195 return 0;
2196 }
2197
Harald Welteaae69be2004-08-29 23:32:14 +00002198 c = iptcc_alloc_chain_head(chain, 0);
2199 if (!c) {
2200 DEBUGP("Cannot allocate memory for chain `%s'\n", chain);
2201 errno = ENOMEM;
2202 return 0;
Marc Bouchere6869a82000-03-20 06:03:29 +00002203
Harald Welteaae69be2004-08-29 23:32:14 +00002204 }
Jesper Dangaard Brouer48bde402008-01-15 17:06:48 +00002205 (*handle)->num_chains++; /* New user defined chain */
Marc Bouchere6869a82000-03-20 06:03:29 +00002206
Harald Welteaae69be2004-08-29 23:32:14 +00002207 DEBUGP("Creating chain `%s'\n", chain);
Jesper Dangaard Brouerd8cb7872007-11-28 08:40:26 +00002208 iptc_insert_chain(*handle, c); /* Insert sorted */
Harald Welte0113fe72004-01-06 19:04:02 +00002209
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +00002210 /* Inserting chains don't change the correctness of the chain
2211 * index (except if its smaller than index[0], but that
2212 * handled by iptc_insert_chain). It only causes longer lists
2213 * in the buckets. Thus, only rebuild chain index when the
2214 * capacity is exceed with CHAIN_INDEX_INSERT_MAX chains.
2215 */
2216 int capacity = (*handle)->chain_index_sz * CHAIN_INDEX_BUCKET_LEN;
2217 int exceeded = ((((*handle)->num_chains)-capacity));
2218 if (exceeded > CHAIN_INDEX_INSERT_MAX) {
2219 debug("Capacity(%d) exceeded(%d) rebuild (chains:%d)\n",
2220 capacity, exceeded, (*handle)->num_chains);
2221 iptcc_chain_index_rebuild(*handle);
2222 }
2223
Harald Welte0113fe72004-01-06 19:04:02 +00002224 set_changed(*handle);
2225
Harald Welteaae69be2004-08-29 23:32:14 +00002226 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00002227}
2228
2229/* Get the number of references to this chain. */
2230int
Rusty Russell79dee072000-05-02 16:45:16 +00002231TC_GET_REFERENCES(unsigned int *ref, const IPT_CHAINLABEL chain,
2232 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00002233{
Harald Welteaae69be2004-08-29 23:32:14 +00002234 struct chain_head *c;
Marc Bouchere6869a82000-03-20 06:03:29 +00002235
Derrik Pates664c0a32005-02-01 13:28:14 +00002236 iptc_fn = TC_GET_REFERENCES;
Harald Welteaae69be2004-08-29 23:32:14 +00002237 if (!(c = iptcc_find_label(chain, *handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002238 errno = ENOENT;
2239 return 0;
2240 }
2241
Harald Welteaae69be2004-08-29 23:32:14 +00002242 *ref = c->references;
2243
Marc Bouchere6869a82000-03-20 06:03:29 +00002244 return 1;
2245}
2246
2247/* Deletes a chain. */
2248int
Rusty Russell79dee072000-05-02 16:45:16 +00002249TC_DELETE_CHAIN(const IPT_CHAINLABEL chain, TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00002250{
Marc Bouchere6869a82000-03-20 06:03:29 +00002251 unsigned int references;
Harald Welteaae69be2004-08-29 23:32:14 +00002252 struct chain_head *c;
Rusty Russell7e53bf92000-03-20 07:03:28 +00002253
Rusty Russell79dee072000-05-02 16:45:16 +00002254 iptc_fn = TC_DELETE_CHAIN;
Marc Bouchere6869a82000-03-20 06:03:29 +00002255
Harald Welteaae69be2004-08-29 23:32:14 +00002256 if (!(c = iptcc_find_label(chain, *handle))) {
2257 DEBUGP("cannot find chain `%s'\n", chain);
Marc Bouchere6869a82000-03-20 06:03:29 +00002258 errno = ENOENT;
2259 return 0;
2260 }
2261
Harald Welteaae69be2004-08-29 23:32:14 +00002262 if (TC_BUILTIN(chain, *handle)) {
2263 DEBUGP("cannot remove builtin chain `%s'\n", chain);
2264 errno = EINVAL;
2265 return 0;
2266 }
2267
2268 if (!TC_GET_REFERENCES(&references, chain, handle)) {
2269 DEBUGP("cannot get references on chain `%s'\n", chain);
2270 return 0;
2271 }
2272
2273 if (references > 0) {
2274 DEBUGP("chain `%s' still has references\n", chain);
2275 errno = EMLINK;
2276 return 0;
2277 }
2278
2279 if (c->num_rules) {
2280 DEBUGP("chain `%s' is not empty\n", chain);
Marc Bouchere6869a82000-03-20 06:03:29 +00002281 errno = ENOTEMPTY;
2282 return 0;
2283 }
2284
Harald Welteaae69be2004-08-29 23:32:14 +00002285 /* If we are about to delete the chain that is the current
Jesper Dangaard Brouer48bde402008-01-15 17:06:48 +00002286 * iterator, move chain iterator forward. */
Harald Welteaae69be2004-08-29 23:32:14 +00002287 if (c == (*handle)->chain_iterator_cur)
2288 iptcc_chain_iterator_advance(*handle);
Harald Welte0113fe72004-01-06 19:04:02 +00002289
Jesper Dangaard Brouer48bde402008-01-15 17:06:48 +00002290 (*handle)->num_chains--; /* One user defined chain deleted */
2291
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +00002292 //list_del(&c->list); /* Done in iptcc_chain_index_delete_chain() */
2293 iptcc_chain_index_delete_chain(c, *handle);
2294 free(c);
2295
Harald Welteaae69be2004-08-29 23:32:14 +00002296 DEBUGP("chain `%s' deleted\n", chain);
2297
2298 set_changed(*handle);
2299
2300 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00002301}
2302
2303/* Renames a chain. */
Rusty Russell79dee072000-05-02 16:45:16 +00002304int TC_RENAME_CHAIN(const IPT_CHAINLABEL oldname,
2305 const IPT_CHAINLABEL newname,
2306 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00002307{
Harald Welteaae69be2004-08-29 23:32:14 +00002308 struct chain_head *c;
Rusty Russell79dee072000-05-02 16:45:16 +00002309 iptc_fn = TC_RENAME_CHAIN;
Marc Bouchere6869a82000-03-20 06:03:29 +00002310
Harald Welte1de80462000-10-30 12:00:27 +00002311 /* find_label doesn't cover built-in targets: DROP, ACCEPT,
2312 QUEUE, RETURN. */
Harald Welteaae69be2004-08-29 23:32:14 +00002313 if (iptcc_find_label(newname, *handle)
Rusty Russell79dee072000-05-02 16:45:16 +00002314 || strcmp(newname, LABEL_DROP) == 0
2315 || strcmp(newname, LABEL_ACCEPT) == 0
Harald Welte1de80462000-10-30 12:00:27 +00002316 || strcmp(newname, LABEL_QUEUE) == 0
Rusty Russell79dee072000-05-02 16:45:16 +00002317 || strcmp(newname, LABEL_RETURN) == 0) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002318 errno = EEXIST;
2319 return 0;
2320 }
2321
Harald Welteaae69be2004-08-29 23:32:14 +00002322 if (!(c = iptcc_find_label(oldname, *handle))
Rusty Russell79dee072000-05-02 16:45:16 +00002323 || TC_BUILTIN(oldname, *handle)) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002324 errno = ENOENT;
2325 return 0;
2326 }
2327
Rusty Russell79dee072000-05-02 16:45:16 +00002328 if (strlen(newname)+1 > sizeof(IPT_CHAINLABEL)) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002329 errno = EINVAL;
2330 return 0;
2331 }
2332
Harald Welteaae69be2004-08-29 23:32:14 +00002333 strncpy(c->name, newname, sizeof(IPT_CHAINLABEL));
2334
Harald Welte0113fe72004-01-06 19:04:02 +00002335 set_changed(*handle);
2336
Marc Bouchere6869a82000-03-20 06:03:29 +00002337 return 1;
2338}
2339
2340/* Sets the policy on a built-in chain. */
2341int
Rusty Russell79dee072000-05-02 16:45:16 +00002342TC_SET_POLICY(const IPT_CHAINLABEL chain,
2343 const IPT_CHAINLABEL policy,
Harald Welte1cef74d2001-01-05 15:22:59 +00002344 STRUCT_COUNTERS *counters,
Rusty Russell79dee072000-05-02 16:45:16 +00002345 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00002346{
Harald Welteaae69be2004-08-29 23:32:14 +00002347 struct chain_head *c;
Marc Bouchere6869a82000-03-20 06:03:29 +00002348
Rusty Russell79dee072000-05-02 16:45:16 +00002349 iptc_fn = TC_SET_POLICY;
Marc Bouchere6869a82000-03-20 06:03:29 +00002350
Harald Welteaae69be2004-08-29 23:32:14 +00002351 if (!(c = iptcc_find_label(chain, *handle))) {
2352 DEBUGP("cannot find chain `%s'\n", chain);
2353 errno = ENOENT;
Marc Bouchere6869a82000-03-20 06:03:29 +00002354 return 0;
2355 }
2356
Harald Welteaae69be2004-08-29 23:32:14 +00002357 if (!iptcc_is_builtin(c)) {
2358 DEBUGP("cannot set policy of userdefinedchain `%s'\n", chain);
2359 errno = ENOENT;
2360 return 0;
2361 }
Marc Bouchere6869a82000-03-20 06:03:29 +00002362
Rusty Russell79dee072000-05-02 16:45:16 +00002363 if (strcmp(policy, LABEL_ACCEPT) == 0)
Harald Welteaae69be2004-08-29 23:32:14 +00002364 c->verdict = -NF_ACCEPT - 1;
Rusty Russell79dee072000-05-02 16:45:16 +00002365 else if (strcmp(policy, LABEL_DROP) == 0)
Harald Welteaae69be2004-08-29 23:32:14 +00002366 c->verdict = -NF_DROP - 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00002367 else {
2368 errno = EINVAL;
2369 return 0;
2370 }
Harald Welte1cef74d2001-01-05 15:22:59 +00002371
Harald Welte1cef74d2001-01-05 15:22:59 +00002372 if (counters) {
2373 /* set byte and packet counters */
Harald Welteaae69be2004-08-29 23:32:14 +00002374 memcpy(&c->counters, counters, sizeof(STRUCT_COUNTERS));
2375 c->counter_map.maptype = COUNTER_MAP_SET;
Harald Welte1cef74d2001-01-05 15:22:59 +00002376 } else {
Harald Welteaae69be2004-08-29 23:32:14 +00002377 c->counter_map.maptype = COUNTER_MAP_NOMAP;
Harald Welte1cef74d2001-01-05 15:22:59 +00002378 }
2379
Rusty Russell175f6412000-03-24 09:32:20 +00002380 set_changed(*handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00002381
Marc Bouchere6869a82000-03-20 06:03:29 +00002382 return 1;
2383}
2384
2385/* Without this, on gcc 2.7.2.3, we get:
Rusty Russell79dee072000-05-02 16:45:16 +00002386 libiptc.c: In function `TC_COMMIT':
Marc Bouchere6869a82000-03-20 06:03:29 +00002387 libiptc.c:833: fixed or forbidden register was spilled.
2388 This may be due to a compiler bug or to impossible asm
2389 statements or clauses.
2390*/
2391static void
Rusty Russell79dee072000-05-02 16:45:16 +00002392subtract_counters(STRUCT_COUNTERS *answer,
2393 const STRUCT_COUNTERS *a,
2394 const STRUCT_COUNTERS *b)
Marc Bouchere6869a82000-03-20 06:03:29 +00002395{
2396 answer->pcnt = a->pcnt - b->pcnt;
2397 answer->bcnt = a->bcnt - b->bcnt;
2398}
2399
Harald Welteaae69be2004-08-29 23:32:14 +00002400
Jan Engelhardtdbb77542008-02-11 00:33:30 +01002401static void counters_nomap(STRUCT_COUNTERS_INFO *newcounters, unsigned int idx)
Harald Welteaae69be2004-08-29 23:32:14 +00002402{
Jan Engelhardtdbb77542008-02-11 00:33:30 +01002403 newcounters->counters[idx] = ((STRUCT_COUNTERS) { 0, 0});
Harald Welteaae69be2004-08-29 23:32:14 +00002404 DEBUGP_C("NOMAP => zero\n");
2405}
2406
2407static void counters_normal_map(STRUCT_COUNTERS_INFO *newcounters,
Jan Engelhardtdbb77542008-02-11 00:33:30 +01002408 STRUCT_REPLACE *repl, unsigned int idx,
Harald Welteaae69be2004-08-29 23:32:14 +00002409 unsigned int mappos)
2410{
2411 /* Original read: X.
2412 * Atomic read on replacement: X + Y.
2413 * Currently in kernel: Z.
2414 * Want in kernel: X + Y + Z.
2415 * => Add in X + Y
2416 * => Add in replacement read.
2417 */
Jan Engelhardtdbb77542008-02-11 00:33:30 +01002418 newcounters->counters[idx] = repl->counters[mappos];
Harald Welteaae69be2004-08-29 23:32:14 +00002419 DEBUGP_C("NORMAL_MAP => mappos %u \n", mappos);
2420}
2421
2422static void counters_map_zeroed(STRUCT_COUNTERS_INFO *newcounters,
Jan Engelhardtdbb77542008-02-11 00:33:30 +01002423 STRUCT_REPLACE *repl, unsigned int idx,
2424 unsigned int mappos, STRUCT_COUNTERS *counters)
Harald Welteaae69be2004-08-29 23:32:14 +00002425{
2426 /* Original read: X.
2427 * Atomic read on replacement: X + Y.
2428 * Currently in kernel: Z.
2429 * Want in kernel: Y + Z.
2430 * => Add in Y.
2431 * => Add in (replacement read - original read).
2432 */
Jan Engelhardtdbb77542008-02-11 00:33:30 +01002433 subtract_counters(&newcounters->counters[idx],
Harald Welteaae69be2004-08-29 23:32:14 +00002434 &repl->counters[mappos],
2435 counters);
2436 DEBUGP_C("ZEROED => mappos %u\n", mappos);
2437}
2438
2439static void counters_map_set(STRUCT_COUNTERS_INFO *newcounters,
Jan Engelhardtdbb77542008-02-11 00:33:30 +01002440 unsigned int idx, STRUCT_COUNTERS *counters)
Harald Welteaae69be2004-08-29 23:32:14 +00002441{
2442 /* Want to set counter (iptables-restore) */
2443
Jan Engelhardtdbb77542008-02-11 00:33:30 +01002444 memcpy(&newcounters->counters[idx], counters,
Harald Welteaae69be2004-08-29 23:32:14 +00002445 sizeof(STRUCT_COUNTERS));
2446
2447 DEBUGP_C("SET\n");
2448}
2449
2450
Marc Bouchere6869a82000-03-20 06:03:29 +00002451int
Rusty Russell79dee072000-05-02 16:45:16 +00002452TC_COMMIT(TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00002453{
2454 /* Replace, then map back the counters. */
Rusty Russell79dee072000-05-02 16:45:16 +00002455 STRUCT_REPLACE *repl;
2456 STRUCT_COUNTERS_INFO *newcounters;
Harald Welteaae69be2004-08-29 23:32:14 +00002457 struct chain_head *c;
2458 int ret;
Martin Josefsson841e4ae2003-05-02 15:30:11 +00002459 size_t counterlen;
Harald Welteaae69be2004-08-29 23:32:14 +00002460 int new_number;
2461 unsigned int new_size;
Marc Bouchere6869a82000-03-20 06:03:29 +00002462
Derrik Pates664c0a32005-02-01 13:28:14 +00002463 iptc_fn = TC_COMMIT;
Marc Bouchere6869a82000-03-20 06:03:29 +00002464 CHECK(*handle);
Martin Josefsson841e4ae2003-05-02 15:30:11 +00002465
Marc Bouchere6869a82000-03-20 06:03:29 +00002466 /* Don't commit if nothing changed. */
2467 if (!(*handle)->changed)
2468 goto finished;
2469
Harald Welteaae69be2004-08-29 23:32:14 +00002470 new_number = iptcc_compile_table_prep(*handle, &new_size);
2471 if (new_number < 0) {
2472 errno = ENOMEM;
Harald Welted6ba6f52005-11-12 10:39:40 +00002473 goto out_zero;
Harald Welteaae69be2004-08-29 23:32:14 +00002474 }
2475
2476 repl = malloc(sizeof(*repl) + new_size);
Marc Bouchere6869a82000-03-20 06:03:29 +00002477 if (!repl) {
2478 errno = ENOMEM;
Harald Welted6ba6f52005-11-12 10:39:40 +00002479 goto out_zero;
Marc Bouchere6869a82000-03-20 06:03:29 +00002480 }
Martin Josefssonad3b4f92004-09-22 22:04:07 +00002481 memset(repl, 0, sizeof(*repl) + new_size);
Harald Welteaae69be2004-08-29 23:32:14 +00002482
Rusty Russelle45c7132004-12-16 13:21:44 +00002483#if 0
2484 TC_DUMP_ENTRIES(*handle);
2485#endif
2486
Harald Welteaae69be2004-08-29 23:32:14 +00002487 counterlen = sizeof(STRUCT_COUNTERS_INFO)
2488 + sizeof(STRUCT_COUNTERS) * new_number;
Marc Bouchere6869a82000-03-20 06:03:29 +00002489
2490 /* These are the old counters we will get from kernel */
Rusty Russell79dee072000-05-02 16:45:16 +00002491 repl->counters = malloc(sizeof(STRUCT_COUNTERS)
Marc Bouchere6869a82000-03-20 06:03:29 +00002492 * (*handle)->info.num_entries);
2493 if (!repl->counters) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002494 errno = ENOMEM;
Harald Welted6ba6f52005-11-12 10:39:40 +00002495 goto out_free_repl;
Marc Bouchere6869a82000-03-20 06:03:29 +00002496 }
Marc Bouchere6869a82000-03-20 06:03:29 +00002497 /* These are the counters we're going to put back, later. */
2498 newcounters = malloc(counterlen);
2499 if (!newcounters) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002500 errno = ENOMEM;
Harald Welted6ba6f52005-11-12 10:39:40 +00002501 goto out_free_repl_counters;
Marc Bouchere6869a82000-03-20 06:03:29 +00002502 }
Harald Welteaae69be2004-08-29 23:32:14 +00002503 memset(newcounters, 0, counterlen);
Marc Bouchere6869a82000-03-20 06:03:29 +00002504
2505 strcpy(repl->name, (*handle)->info.name);
Harald Welteaae69be2004-08-29 23:32:14 +00002506 repl->num_entries = new_number;
2507 repl->size = new_size;
2508
Marc Bouchere6869a82000-03-20 06:03:29 +00002509 repl->num_counters = (*handle)->info.num_entries;
2510 repl->valid_hooks = (*handle)->info.valid_hooks;
Harald Welteaae69be2004-08-29 23:32:14 +00002511
2512 DEBUGP("num_entries=%u, size=%u, num_counters=%u\n",
2513 repl->num_entries, repl->size, repl->num_counters);
2514
2515 ret = iptcc_compile_table(*handle, repl);
2516 if (ret < 0) {
2517 errno = ret;
Harald Welted6ba6f52005-11-12 10:39:40 +00002518 goto out_free_newcounters;
Harald Welteaae69be2004-08-29 23:32:14 +00002519 }
2520
2521
2522#ifdef IPTC_DEBUG2
2523 {
2524 int fd = open("/tmp/libiptc-so_set_replace.blob",
2525 O_CREAT|O_WRONLY);
2526 if (fd >= 0) {
2527 write(fd, repl, sizeof(*repl) + repl->size);
2528 close(fd);
2529 }
2530 }
2531#endif
Marc Bouchere6869a82000-03-20 06:03:29 +00002532
Harald Welted6ba6f52005-11-12 10:39:40 +00002533 ret = setsockopt(sockfd, TC_IPPROTO, SO_SET_REPLACE, repl,
2534 sizeof(*repl) + repl->size);
Patrick McHardye0865ad2006-04-22 02:08:56 +00002535 if (ret < 0)
Harald Welted6ba6f52005-11-12 10:39:40 +00002536 goto out_free_newcounters;
Marc Bouchere6869a82000-03-20 06:03:29 +00002537
2538 /* Put counters back. */
2539 strcpy(newcounters->name, (*handle)->info.name);
Harald Welteaae69be2004-08-29 23:32:14 +00002540 newcounters->num_counters = new_number;
Marc Bouchere6869a82000-03-20 06:03:29 +00002541
Harald Welteaae69be2004-08-29 23:32:14 +00002542 list_for_each_entry(c, &(*handle)->chains, list) {
2543 struct rule_head *r;
Marc Bouchere6869a82000-03-20 06:03:29 +00002544
Harald Welteaae69be2004-08-29 23:32:14 +00002545 /* Builtin chains have their own counters */
2546 if (iptcc_is_builtin(c)) {
2547 DEBUGP("counter for chain-index %u: ", c->foot_index);
2548 switch(c->counter_map.maptype) {
2549 case COUNTER_MAP_NOMAP:
2550 counters_nomap(newcounters, c->foot_index);
2551 break;
2552 case COUNTER_MAP_NORMAL_MAP:
2553 counters_normal_map(newcounters, repl,
2554 c->foot_index,
2555 c->counter_map.mappos);
2556 break;
2557 case COUNTER_MAP_ZEROED:
2558 counters_map_zeroed(newcounters, repl,
2559 c->foot_index,
2560 c->counter_map.mappos,
2561 &c->counters);
2562 break;
2563 case COUNTER_MAP_SET:
2564 counters_map_set(newcounters, c->foot_index,
2565 &c->counters);
2566 break;
2567 }
2568 }
Harald Welte1cef74d2001-01-05 15:22:59 +00002569
Harald Welteaae69be2004-08-29 23:32:14 +00002570 list_for_each_entry(r, &c->rules, list) {
2571 DEBUGP("counter for index %u: ", r->index);
2572 switch (r->counter_map.maptype) {
2573 case COUNTER_MAP_NOMAP:
2574 counters_nomap(newcounters, r->index);
2575 break;
Harald Welte1cef74d2001-01-05 15:22:59 +00002576
Harald Welteaae69be2004-08-29 23:32:14 +00002577 case COUNTER_MAP_NORMAL_MAP:
2578 counters_normal_map(newcounters, repl,
2579 r->index,
2580 r->counter_map.mappos);
2581 break;
Harald Welte1cef74d2001-01-05 15:22:59 +00002582
Harald Welteaae69be2004-08-29 23:32:14 +00002583 case COUNTER_MAP_ZEROED:
2584 counters_map_zeroed(newcounters, repl,
2585 r->index,
2586 r->counter_map.mappos,
2587 &r->entry->counters);
2588 break;
2589
2590 case COUNTER_MAP_SET:
2591 counters_map_set(newcounters, r->index,
2592 &r->entry->counters);
2593 break;
2594 }
Marc Bouchere6869a82000-03-20 06:03:29 +00002595 }
2596 }
Rusty Russell62527ce2000-09-04 09:45:54 +00002597
Harald Welteaae69be2004-08-29 23:32:14 +00002598#ifdef IPTC_DEBUG2
2599 {
2600 int fd = open("/tmp/libiptc-so_set_add_counters.blob",
2601 O_CREAT|O_WRONLY);
2602 if (fd >= 0) {
2603 write(fd, newcounters, counterlen);
2604 close(fd);
2605 }
2606 }
2607#endif
2608
Harald Welted6ba6f52005-11-12 10:39:40 +00002609 ret = setsockopt(sockfd, TC_IPPROTO, SO_SET_ADD_COUNTERS,
2610 newcounters, counterlen);
Patrick McHardye0865ad2006-04-22 02:08:56 +00002611 if (ret < 0)
Harald Welted6ba6f52005-11-12 10:39:40 +00002612 goto out_free_newcounters;
Marc Bouchere6869a82000-03-20 06:03:29 +00002613
2614 free(repl->counters);
2615 free(repl);
2616 free(newcounters);
2617
Harald Welted6ba6f52005-11-12 10:39:40 +00002618finished:
Martin Josefsson841e4ae2003-05-02 15:30:11 +00002619 TC_FREE(handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00002620 return 1;
Harald Welted6ba6f52005-11-12 10:39:40 +00002621
2622out_free_newcounters:
2623 free(newcounters);
2624out_free_repl_counters:
2625 free(repl->counters);
2626out_free_repl:
2627 free(repl);
2628out_zero:
2629 return 0;
Marc Bouchere6869a82000-03-20 06:03:29 +00002630}
2631
2632/* Get raw socket. */
2633int
Patrick McHardy0b639362007-09-08 16:00:01 +00002634TC_GET_RAW_SOCKET(void)
Marc Bouchere6869a82000-03-20 06:03:29 +00002635{
2636 return sockfd;
2637}
2638
2639/* Translates errno numbers into more human-readable form than strerror. */
2640const char *
Rusty Russell79dee072000-05-02 16:45:16 +00002641TC_STRERROR(int err)
Marc Bouchere6869a82000-03-20 06:03:29 +00002642{
2643 unsigned int i;
2644 struct table_struct {
2645 void *fn;
2646 int err;
2647 const char *message;
2648 } table [] =
Harald Welte4ccfa632001-07-30 15:12:43 +00002649 { { TC_INIT, EPERM, "Permission denied (you must be root)" },
Rusty Russell79dee072000-05-02 16:45:16 +00002650 { TC_INIT, EINVAL, "Module is wrong version" },
Harald Welte4ccfa632001-07-30 15:12:43 +00002651 { TC_INIT, ENOENT,
2652 "Table does not exist (do you need to insmod?)" },
Rusty Russell79dee072000-05-02 16:45:16 +00002653 { TC_DELETE_CHAIN, ENOTEMPTY, "Chain is not empty" },
2654 { TC_DELETE_CHAIN, EINVAL, "Can't delete built-in chain" },
2655 { TC_DELETE_CHAIN, EMLINK,
Marc Bouchere6869a82000-03-20 06:03:29 +00002656 "Can't delete chain with references left" },
Rusty Russell79dee072000-05-02 16:45:16 +00002657 { TC_CREATE_CHAIN, EEXIST, "Chain already exists" },
2658 { TC_INSERT_ENTRY, E2BIG, "Index of insertion too big" },
2659 { TC_REPLACE_ENTRY, E2BIG, "Index of replacement too big" },
2660 { TC_DELETE_NUM_ENTRY, E2BIG, "Index of deletion too big" },
Harald Welte1cef74d2001-01-05 15:22:59 +00002661 { TC_READ_COUNTER, E2BIG, "Index of counter too big" },
2662 { TC_ZERO_COUNTER, E2BIG, "Index of counter too big" },
Rusty Russell79dee072000-05-02 16:45:16 +00002663 { TC_INSERT_ENTRY, ELOOP, "Loop found in table" },
2664 { TC_INSERT_ENTRY, EINVAL, "Target problem" },
Marc Bouchere6869a82000-03-20 06:03:29 +00002665 /* EINVAL for CHECK probably means bad interface. */
Rusty Russell79dee072000-05-02 16:45:16 +00002666 { TC_CHECK_PACKET, EINVAL,
Marc Boucherc8264992000-04-22 22:34:44 +00002667 "Bad arguments (does that interface exist?)" },
Harald Welte4ccfa632001-07-30 15:12:43 +00002668 { TC_CHECK_PACKET, ENOSYS,
2669 "Checking will most likely never get implemented" },
Marc Bouchere6869a82000-03-20 06:03:29 +00002670 /* ENOENT for DELETE probably means no matching rule */
Rusty Russell79dee072000-05-02 16:45:16 +00002671 { TC_DELETE_ENTRY, ENOENT,
Marc Boucherc8264992000-04-22 22:34:44 +00002672 "Bad rule (does a matching rule exist in that chain?)" },
Rusty Russell79dee072000-05-02 16:45:16 +00002673 { TC_SET_POLICY, ENOENT,
Marc Boucherc8264992000-04-22 22:34:44 +00002674 "Bad built-in chain name" },
Rusty Russell79dee072000-05-02 16:45:16 +00002675 { TC_SET_POLICY, EINVAL,
Marc Boucherc8264992000-04-22 22:34:44 +00002676 "Bad policy name" },
Harald Welte4ccfa632001-07-30 15:12:43 +00002677
2678 { NULL, 0, "Incompatible with this kernel" },
2679 { NULL, ENOPROTOOPT, "iptables who? (do you need to insmod?)" },
2680 { NULL, ENOSYS, "Will be implemented real soon. I promise ;)" },
2681 { NULL, ENOMEM, "Memory allocation problem" },
2682 { NULL, ENOENT, "No chain/target/match by that name" },
Marc Bouchere6869a82000-03-20 06:03:29 +00002683 };
2684
2685 for (i = 0; i < sizeof(table)/sizeof(struct table_struct); i++) {
2686 if ((!table[i].fn || table[i].fn == iptc_fn)
2687 && table[i].err == err)
2688 return table[i].message;
2689 }
2690
2691 return strerror(err);
2692}