blob: 2b788c198b6edfc30aca1924de92cda40becafd8 [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 +000049static int sockfd = -1;
Derrik Pates664c0a32005-02-01 13:28:14 +000050static int sockfd_use = 0;
Marc Bouchere6869a82000-03-20 06:03:29 +000051static void *iptc_fn = NULL;
52
Patrick McHardy0b639362007-09-08 16:00:01 +000053static const char *hooknames[] = {
54 [HOOK_PRE_ROUTING] = "PREROUTING",
55 [HOOK_LOCAL_IN] = "INPUT",
56 [HOOK_FORWARD] = "FORWARD",
57 [HOOK_LOCAL_OUT] = "OUTPUT",
58 [HOOK_POST_ROUTING] = "POSTROUTING",
Rusty Russell10758b72000-09-14 07:37:33 +000059#ifdef HOOK_DROPPING
Patrick McHardy0b639362007-09-08 16:00:01 +000060 [HOOK_DROPPING] = "DROPPING"
Rusty Russell10758b72000-09-14 07:37:33 +000061#endif
Marc Bouchere6869a82000-03-20 06:03:29 +000062};
63
Harald Welteaae69be2004-08-29 23:32:14 +000064/* Convenience structures */
65struct ipt_error_target
66{
67 STRUCT_ENTRY_TARGET t;
68 char error[TABLE_MAXNAMELEN];
69};
70
71struct chain_head;
72struct rule_head;
73
Marc Bouchere6869a82000-03-20 06:03:29 +000074struct counter_map
75{
76 enum {
77 COUNTER_MAP_NOMAP,
78 COUNTER_MAP_NORMAL_MAP,
Harald Welte1cef74d2001-01-05 15:22:59 +000079 COUNTER_MAP_ZEROED,
80 COUNTER_MAP_SET
Marc Bouchere6869a82000-03-20 06:03:29 +000081 } maptype;
82 unsigned int mappos;
83};
84
Harald Welteaae69be2004-08-29 23:32:14 +000085enum iptcc_rule_type {
86 IPTCC_R_STANDARD, /* standard target (ACCEPT, ...) */
87 IPTCC_R_MODULE, /* extension module (SNAT, ...) */
88 IPTCC_R_FALLTHROUGH, /* fallthrough rule */
89 IPTCC_R_JUMP, /* jump to other chain */
Marc Bouchere6869a82000-03-20 06:03:29 +000090};
91
Harald Welteaae69be2004-08-29 23:32:14 +000092struct rule_head
Rusty Russell30fd6e52000-04-23 09:16:06 +000093{
Harald Welteaae69be2004-08-29 23:32:14 +000094 struct list_head list;
95 struct chain_head *chain;
96 struct counter_map counter_map;
97
98 unsigned int index; /* index (needed for counter_map) */
99 unsigned int offset; /* offset in rule blob */
100
101 enum iptcc_rule_type type;
102 struct chain_head *jump; /* jump target, if IPTCC_R_JUMP */
103
104 unsigned int size; /* size of entry data */
105 STRUCT_ENTRY entry[0];
106};
107
108struct chain_head
109{
110 struct list_head list;
Rusty Russell79dee072000-05-02 16:45:16 +0000111 char name[TABLE_MAXNAMELEN];
Harald Welteaae69be2004-08-29 23:32:14 +0000112 unsigned int hooknum; /* hook number+1 if builtin */
113 unsigned int references; /* how many jumps reference us */
114 int verdict; /* verdict if builtin */
115
116 STRUCT_COUNTERS counters; /* per-chain counters */
117 struct counter_map counter_map;
118
119 unsigned int num_rules; /* number of rules in list */
120 struct list_head rules; /* list of rules */
121
122 unsigned int index; /* index (needed for jump resolval) */
123 unsigned int head_offset; /* offset in rule blob */
124 unsigned int foot_index; /* index (needed for counter_map) */
125 unsigned int foot_offset; /* offset in rule blob */
Rusty Russell30fd6e52000-04-23 09:16:06 +0000126};
127
Rusty Russell79dee072000-05-02 16:45:16 +0000128STRUCT_TC_HANDLE
Marc Bouchere6869a82000-03-20 06:03:29 +0000129{
Harald Welteaae69be2004-08-29 23:32:14 +0000130 int changed; /* Have changes been made? */
131
132 struct list_head chains;
133
134 struct chain_head *chain_iterator_cur;
135 struct rule_head *rule_iterator_cur;
136
Jesper Dangaard Brouer48bde402008-01-15 17:06:48 +0000137 unsigned int num_chains; /* number of user defined chains */
138
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000139 struct chain_head **chain_index; /* array for fast chain list access*/
140 unsigned int chain_index_sz;/* size of chain index array */
141
Rusty Russell79dee072000-05-02 16:45:16 +0000142 STRUCT_GETINFO info;
Harald Welteaae69be2004-08-29 23:32:14 +0000143 STRUCT_GET_ENTRIES *entries;
Marc Bouchere6869a82000-03-20 06:03:29 +0000144};
145
Harald Welteaae69be2004-08-29 23:32:14 +0000146/* allocate a new chain head for the cache */
147static struct chain_head *iptcc_alloc_chain_head(const char *name, int hooknum)
148{
149 struct chain_head *c = malloc(sizeof(*c));
150 if (!c)
151 return NULL;
152 memset(c, 0, sizeof(*c));
153
154 strncpy(c->name, name, TABLE_MAXNAMELEN);
155 c->hooknum = hooknum;
156 INIT_LIST_HEAD(&c->rules);
157
158 return c;
159}
160
161/* allocate and initialize a new rule for the cache */
162static struct rule_head *iptcc_alloc_rule(struct chain_head *c, unsigned int size)
163{
164 struct rule_head *r = malloc(sizeof(*r)+size);
165 if (!r)
166 return NULL;
167 memset(r, 0, sizeof(*r));
168
169 r->chain = c;
170 r->size = size;
171
172 return r;
173}
174
175/* notify us that the ruleset has been modified by the user */
Jesper Dangaard Brouer91093982008-01-15 17:01:58 +0000176static inline void
Rusty Russell79dee072000-05-02 16:45:16 +0000177set_changed(TC_HANDLE_T h)
Rusty Russell175f6412000-03-24 09:32:20 +0000178{
Rusty Russell175f6412000-03-24 09:32:20 +0000179 h->changed = 1;
180}
181
Harald Welte380ba5f2002-02-13 16:19:55 +0000182#ifdef IPTC_DEBUG
Rusty Russell79dee072000-05-02 16:45:16 +0000183static void do_check(TC_HANDLE_T h, unsigned int line);
Rusty Russell849779c2000-04-23 15:51:51 +0000184#define CHECK(h) do { if (!getenv("IPTC_NO_CHECK")) do_check((h), __LINE__); } while(0)
Rusty Russell30fd6e52000-04-23 09:16:06 +0000185#else
186#define CHECK(h)
187#endif
Marc Bouchere6869a82000-03-20 06:03:29 +0000188
Harald Welteaae69be2004-08-29 23:32:14 +0000189
190/**********************************************************************
191 * iptc blob utility functions (iptcb_*)
192 **********************************************************************/
193
Marc Bouchere6869a82000-03-20 06:03:29 +0000194static inline int
Harald Welteaae69be2004-08-29 23:32:14 +0000195iptcb_get_number(const STRUCT_ENTRY *i,
Rusty Russell79dee072000-05-02 16:45:16 +0000196 const STRUCT_ENTRY *seek,
Marc Bouchere6869a82000-03-20 06:03:29 +0000197 unsigned int *pos)
198{
199 if (i == seek)
200 return 1;
201 (*pos)++;
202 return 0;
203}
204
Marc Bouchere6869a82000-03-20 06:03:29 +0000205static inline int
Harald Welteaae69be2004-08-29 23:32:14 +0000206iptcb_get_entry_n(STRUCT_ENTRY *i,
Marc Bouchere6869a82000-03-20 06:03:29 +0000207 unsigned int number,
208 unsigned int *pos,
Rusty Russell79dee072000-05-02 16:45:16 +0000209 STRUCT_ENTRY **pe)
Marc Bouchere6869a82000-03-20 06:03:29 +0000210{
211 if (*pos == number) {
212 *pe = i;
213 return 1;
214 }
215 (*pos)++;
216 return 0;
217}
218
Harald Welteaae69be2004-08-29 23:32:14 +0000219static inline STRUCT_ENTRY *
220iptcb_get_entry(TC_HANDLE_T h, unsigned int offset)
221{
222 return (STRUCT_ENTRY *)((char *)h->entries->entrytable + offset);
223}
224
225static unsigned int
226iptcb_entry2index(const TC_HANDLE_T h, const STRUCT_ENTRY *seek)
Marc Bouchere6869a82000-03-20 06:03:29 +0000227{
228 unsigned int pos = 0;
Marc Bouchere6869a82000-03-20 06:03:29 +0000229
Harald Welteaae69be2004-08-29 23:32:14 +0000230 if (ENTRY_ITERATE(h->entries->entrytable, h->entries->size,
231 iptcb_get_number, seek, &pos) == 0) {
232 fprintf(stderr, "ERROR: offset %u not an entry!\n",
233 (unsigned int)((char *)seek - (char *)h->entries->entrytable));
234 abort();
235 }
236 return pos;
Marc Bouchere6869a82000-03-20 06:03:29 +0000237}
238
Harald Welte0113fe72004-01-06 19:04:02 +0000239static inline STRUCT_ENTRY *
Harald Welteaae69be2004-08-29 23:32:14 +0000240iptcb_offset2entry(TC_HANDLE_T h, unsigned int offset)
Harald Welte0113fe72004-01-06 19:04:02 +0000241{
Harald Welteaae69be2004-08-29 23:32:14 +0000242 return (STRUCT_ENTRY *) ((void *)h->entries->entrytable+offset);
Harald Welte0113fe72004-01-06 19:04:02 +0000243}
244
Harald Welteaae69be2004-08-29 23:32:14 +0000245
Harald Welte0113fe72004-01-06 19:04:02 +0000246static inline unsigned long
Harald Welteaae69be2004-08-29 23:32:14 +0000247iptcb_entry2offset(const TC_HANDLE_T h, const STRUCT_ENTRY *e)
Harald Welte0113fe72004-01-06 19:04:02 +0000248{
Harald Welteaae69be2004-08-29 23:32:14 +0000249 return (void *)e - (void *)h->entries->entrytable;
Harald Welte3ea8f402003-06-23 18:25:59 +0000250}
251
252static inline unsigned int
Harald Welteaae69be2004-08-29 23:32:14 +0000253iptcb_offset2index(const TC_HANDLE_T h, unsigned int offset)
Harald Welte3ea8f402003-06-23 18:25:59 +0000254{
Harald Welteaae69be2004-08-29 23:32:14 +0000255 return iptcb_entry2index(h, iptcb_offset2entry(h, offset));
256}
257
258/* Returns 0 if not hook entry, else hooknumber + 1 */
259static inline unsigned int
260iptcb_ent_is_hook_entry(STRUCT_ENTRY *e, TC_HANDLE_T h)
261{
262 unsigned int i;
263
264 for (i = 0; i < NUMHOOKS; i++) {
265 if ((h->info.valid_hooks & (1 << i))
266 && iptcb_get_entry(h, h->info.hook_entry[i]) == e)
267 return i+1;
268 }
269 return 0;
Harald Welte3ea8f402003-06-23 18:25:59 +0000270}
271
272
Harald Welteaae69be2004-08-29 23:32:14 +0000273/**********************************************************************
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000274 * Chain index (cache utility) functions
275 **********************************************************************
276 * The chain index is an array with pointers into the chain list, with
277 * CHAIN_INDEX_BUCKET_LEN spacing. This facilitates the ability to
278 * speedup chain list searching, by find a more optimal starting
279 * points when searching the linked list.
280 *
281 * The starting point can be found fast by using a binary search of
282 * the chain index. Thus, reducing the previous search complexity of
283 * O(n) to O(log(n/k) + k) where k is CHAIN_INDEX_BUCKET_LEN.
284 *
285 * A nice property of the chain index, is that the "bucket" list
286 * length is max CHAIN_INDEX_BUCKET_LEN (when just build, inserts will
287 * change this). Oppose to hashing, where the "bucket" list length can
288 * vary a lot.
289 */
290#ifndef CHAIN_INDEX_BUCKET_LEN
291#define CHAIN_INDEX_BUCKET_LEN 40
292#endif
293
294/* Another nice property of the chain index is that inserting/creating
295 * chains in chain list don't change the correctness of the chain
296 * index, it only causes longer lists in the buckets.
297 *
298 * To mitigate the performance penalty of longer bucket lists and the
299 * penalty of rebuilding, the chain index is rebuild only when
300 * CHAIN_INDEX_INSERT_MAX chains has been added.
301 */
302#ifndef CHAIN_INDEX_INSERT_MAX
303#define CHAIN_INDEX_INSERT_MAX 355
304#endif
305
306static inline unsigned int iptcc_is_builtin(struct chain_head *c);
307
308
309/* Use binary search in the chain index array, to find a chain_head
310 * pointer closest to the place of the searched name element.
311 *
312 * Notes that, binary search (obviously) requires that the chain list
313 * is sorted by name.
314 */
315static struct list_head *
Jan Engelhardtdbb77542008-02-11 00:33:30 +0100316iptcc_bsearch_chain_index(const char *name, unsigned int *idx, TC_HANDLE_T handle)
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000317{
318 unsigned int pos, end;
319 int res;
320
321 struct list_head *list_pos;
322 list_pos=&handle->chains;
323
324 /* Check for empty array, e.g. no user defined chains */
325 if (handle->chain_index_sz == 0) {
326 debug("WARNING: handle->chain_index_sz == 0\n");
327 return list_pos;
328 }
329
330 /* Init */
331 end = handle->chain_index_sz;
332 pos = end / 2;
333
334 debug("bsearch Find chain:%s (pos:%d end:%d)\n", name, pos, end);
335
336 /* Loop */
337 loop:
338 if (!handle->chain_index[pos]) {
339 fprintf(stderr, "ERROR: NULL pointer chain_index[%d]\n", pos);
340 return &handle->chains; /* Be safe, return orig start pos */
341 }
342
343 res = strcmp(name, handle->chain_index[pos]->name);
344 list_pos = &handle->chain_index[pos]->list;
Jan Engelhardtdbb77542008-02-11 00:33:30 +0100345 *idx = pos;
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000346
347 debug("bsearch Index[%d] name:%s res:%d ",
348 pos, handle->chain_index[pos]->name, res);
349
350 if (res == 0) { /* Found element, by direct hit */
351 debug("[found] Direct hit pos:%d end:%d\n", pos, end);
352 return list_pos;
353 } else if (res < 0) { /* Too far, jump back */
354 end = pos;
355 pos = pos / 2;
356
357 /* Exit case: First element of array */
358 if (end == 0) {
359 debug("[found] Reached first array elem (end%d)\n",end);
360 return list_pos;
361 }
362 debug("jump back to pos:%d (end:%d)\n", pos, end);
363 goto loop;
364 } else if (res > 0 ){ /* Not far enough, jump forward */
365
366 /* Exit case: Last element of array */
367 if (pos == handle->chain_index_sz-1) {
368 debug("[found] Last array elem (end:%d)\n", end);
369 return list_pos;
370 }
371
372 /* Exit case: Next index less, thus elem in this list section */
373 res = strcmp(name, handle->chain_index[pos+1]->name);
374 if (res < 0) {
375 debug("[found] closest list (end:%d)\n", end);
376 return list_pos;
377 }
378
379 pos = (pos+end)/2;
380 debug("jump forward to pos:%d (end:%d)\n", pos, end);
381 goto loop;
382 }
383
384 return list_pos;
385}
386
387#ifdef DEBUG
388/* Trivial linear search of chain index. Function used for verifying
389 the output of bsearch function */
390static struct list_head *
391iptcc_linearly_search_chain_index(const char *name, TC_HANDLE_T handle)
392{
393 unsigned int i=0;
394 int res=0;
395
396 struct list_head *list_pos;
397 list_pos = &handle->chains;
398
399 if (handle->chain_index_sz)
400 list_pos = &handle->chain_index[0]->list;
401
402 /* Linearly walk of chain index array */
403
404 for (i=0; i < handle->chain_index_sz; i++) {
405 if (handle->chain_index[i]) {
406 res = strcmp(handle->chain_index[i]->name, name);
407 if (res > 0)
408 break; // One step too far
409 list_pos = &handle->chain_index[i]->list;
410 if (res == 0)
411 break; // Direct hit
412 }
413 }
414
415 return list_pos;
416}
417#endif
418
419static int iptcc_chain_index_alloc(TC_HANDLE_T h)
420{
421 unsigned int list_length = CHAIN_INDEX_BUCKET_LEN;
422 unsigned int array_elems;
423 unsigned int array_mem;
424
425 /* Allocate memory for the chain index array */
426 array_elems = (h->num_chains / list_length) +
427 (h->num_chains % list_length ? 1 : 0);
428 array_mem = sizeof(h->chain_index) * array_elems;
429
430 debug("Alloc Chain index, elems:%d mem:%d bytes\n",
431 array_elems, array_mem);
432
433 h->chain_index = malloc(array_mem);
434 if (!h->chain_index) {
435 h->chain_index_sz = 0;
436 return -ENOMEM;
437 }
438 memset(h->chain_index, 0, array_mem);
439 h->chain_index_sz = array_elems;
440
441 return 1;
442}
443
444static void iptcc_chain_index_free(TC_HANDLE_T h)
445{
446 h->chain_index_sz = 0;
447 free(h->chain_index);
448}
449
450
451#ifdef DEBUG
452static void iptcc_chain_index_dump(TC_HANDLE_T h)
453{
454 unsigned int i = 0;
455
456 /* Dump: contents of chain index array */
457 for (i=0; i < h->chain_index_sz; i++) {
458 if (h->chain_index[i]) {
459 fprintf(stderr, "Chain index[%d].name: %s\n",
460 i, h->chain_index[i]->name);
461 }
462 }
463}
464#endif
465
466/* Build the chain index */
467static int iptcc_chain_index_build(TC_HANDLE_T h)
468{
469 unsigned int list_length = CHAIN_INDEX_BUCKET_LEN;
470 unsigned int chains = 0;
471 unsigned int cindex = 0;
472 struct chain_head *c;
473
474 /* Build up the chain index array here */
475 debug("Building chain index\n");
476
477 debug("Number of user defined chains:%d bucket_sz:%d array_sz:%d\n",
478 h->num_chains, list_length, h->chain_index_sz);
479
480 if (h->chain_index_sz == 0)
481 return 0;
482
483 list_for_each_entry(c, &h->chains, list) {
484
485 /* Issue: The index array needs to start after the
486 * builtin chains, as they are not sorted */
487 if (!iptcc_is_builtin(c)) {
488 cindex=chains / list_length;
489
490 /* Safe guard, break out on array limit, this
491 * is useful if chains are added and array is
492 * rebuild, without realloc of memory. */
493 if (cindex >= h->chain_index_sz)
494 break;
495
496 if ((chains % list_length)== 0) {
497 debug("\nIndex[%d] Chains:", cindex);
498 h->chain_index[cindex] = c;
499 }
500 chains++;
501 }
502 debug("%s, ", c->name);
503 }
504 debug("\n");
505
506 return 1;
507}
508
509static int iptcc_chain_index_rebuild(TC_HANDLE_T h)
510{
511 debug("REBUILD chain index array\n");
512 iptcc_chain_index_free(h);
513 if ((iptcc_chain_index_alloc(h)) < 0)
514 return -ENOMEM;
515 iptcc_chain_index_build(h);
516 return 1;
517}
518
519/* Delete chain (pointer) from index array. Removing an element from
520 * the chain list only affects the chain index array, if the chain
521 * index points-to/uses that list pointer.
522 *
523 * There are different strategies, the simple and safe is to rebuild
524 * the chain index every time. The more advanced is to update the
525 * array index to point to the next element, but that requires some
526 * house keeping and boundry checks. The advanced is implemented, as
527 * the simple approach behaves badly when all chains are deleted
528 * because list_for_each processing will always hit the first chain
529 * index, thus causing a rebuild for every chain.
530 */
531static int iptcc_chain_index_delete_chain(struct chain_head *c, TC_HANDLE_T h)
532{
533 struct list_head *index_ptr, *index_ptr2, *next;
534 struct chain_head *c2;
Jan Engelhardtdbb77542008-02-11 00:33:30 +0100535 unsigned int idx, idx2;
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000536
Jan Engelhardtdbb77542008-02-11 00:33:30 +0100537 index_ptr = iptcc_bsearch_chain_index(c->name, &idx, h);
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000538
539 debug("Del chain[%s] c->list:%p index_ptr:%p\n",
540 c->name, &c->list, index_ptr);
541
542 /* Save the next pointer */
543 next = c->list.next;
544 list_del(&c->list);
545
546 if (index_ptr == &c->list) { /* Chain used as index ptr */
547
548 /* See if its possible to avoid a rebuild, by shifting
549 * to next pointer. Its possible if the next pointer
550 * is located in the same index bucket.
551 */
552 c2 = list_entry(next, struct chain_head, list);
Jan Engelhardtdbb77542008-02-11 00:33:30 +0100553 index_ptr2 = iptcc_bsearch_chain_index(c2->name, &idx2, h);
554 if (idx != idx2) {
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000555 /* Rebuild needed */
556 return iptcc_chain_index_rebuild(h);
557 } else {
558 /* Avoiding rebuild */
559 debug("Update cindex[%d] with next ptr name:[%s]\n",
Jan Engelhardtdbb77542008-02-11 00:33:30 +0100560 idx, c2->name);
561 h->chain_index[idx]=c2;
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000562 return 0;
563 }
564 }
565 return 0;
566}
567
568
569/**********************************************************************
Harald Welteaae69be2004-08-29 23:32:14 +0000570 * iptc cache utility functions (iptcc_*)
571 **********************************************************************/
Harald Welte0113fe72004-01-06 19:04:02 +0000572
Harald Welteaae69be2004-08-29 23:32:14 +0000573/* Is the given chain builtin (1) or user-defined (0) */
Jesper Dangaard Brouer91093982008-01-15 17:01:58 +0000574static inline unsigned int iptcc_is_builtin(struct chain_head *c)
Harald Welteaae69be2004-08-29 23:32:14 +0000575{
576 return (c->hooknum ? 1 : 0);
577}
578
579/* Get a specific rule within a chain */
580static struct rule_head *iptcc_get_rule_num(struct chain_head *c,
581 unsigned int rulenum)
582{
583 struct rule_head *r;
584 unsigned int num = 0;
585
586 list_for_each_entry(r, &c->rules, list) {
587 num++;
588 if (num == rulenum)
589 return r;
590 }
591 return NULL;
592}
593
Martin Josefssona5616dc2004-10-24 22:27:31 +0000594/* Get a specific rule within a chain backwards */
595static struct rule_head *iptcc_get_rule_num_reverse(struct chain_head *c,
596 unsigned int rulenum)
597{
598 struct rule_head *r;
599 unsigned int num = 0;
600
601 list_for_each_entry_reverse(r, &c->rules, list) {
602 num++;
603 if (num == rulenum)
604 return r;
605 }
606 return NULL;
607}
608
Harald Welteaae69be2004-08-29 23:32:14 +0000609/* Returns chain head if found, otherwise NULL. */
610static struct chain_head *
611iptcc_find_chain_by_offset(TC_HANDLE_T handle, unsigned int offset)
612{
613 struct list_head *pos;
614
615 if (list_empty(&handle->chains))
616 return NULL;
617
618 list_for_each(pos, &handle->chains) {
619 struct chain_head *c = list_entry(pos, struct chain_head, list);
620 if (offset >= c->head_offset && offset <= c->foot_offset)
621 return c;
Harald Welte0113fe72004-01-06 19:04:02 +0000622 }
623
Harald Welteaae69be2004-08-29 23:32:14 +0000624 return NULL;
Harald Welte0113fe72004-01-06 19:04:02 +0000625}
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000626
Harald Welteaae69be2004-08-29 23:32:14 +0000627/* Returns chain head if found, otherwise NULL. */
628static struct chain_head *
629iptcc_find_label(const char *name, TC_HANDLE_T handle)
630{
631 struct list_head *pos;
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000632 struct list_head *list_start_pos;
633 unsigned int i=0;
634 int res;
Harald Welteaae69be2004-08-29 23:32:14 +0000635
636 if (list_empty(&handle->chains))
637 return NULL;
638
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000639 /* First look at builtin chains */
Harald Welteaae69be2004-08-29 23:32:14 +0000640 list_for_each(pos, &handle->chains) {
641 struct chain_head *c = list_entry(pos, struct chain_head, list);
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000642 if (!iptcc_is_builtin(c))
643 break;
Harald Welteaae69be2004-08-29 23:32:14 +0000644 if (!strcmp(c->name, name))
645 return c;
646 }
647
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000648 /* Find a smart place to start the search via chain index */
649 //list_start_pos = iptcc_linearly_search_chain_index(name, handle);
650 list_start_pos = iptcc_bsearch_chain_index(name, &i, handle);
651
652 /* Handel if bsearch bails out early */
653 if (list_start_pos == &handle->chains) {
654 list_start_pos = pos;
655 }
656#ifdef DEBUG
657 else {
658 /* Verify result of bsearch against linearly index search */
659 struct list_head *test_pos;
660 struct chain_head *test_c, *tmp_c;
661 test_pos = iptcc_linearly_search_chain_index(name, handle);
662 if (list_start_pos != test_pos) {
663 debug("BUG in chain_index search\n");
664 test_c=list_entry(test_pos, struct chain_head,list);
665 tmp_c =list_entry(list_start_pos,struct chain_head,list);
666 debug("Verify search found:\n");
667 debug(" Chain:%s\n", test_c->name);
668 debug("BSearch found:\n");
669 debug(" Chain:%s\n", tmp_c->name);
670 exit(42);
671 }
672 }
673#endif
674
675 /* Initial/special case, no user defined chains */
676 if (handle->num_chains == 0)
677 return NULL;
678
679 /* Start searching through the chain list */
680 list_for_each(pos, list_start_pos->prev) {
681 struct chain_head *c = list_entry(pos, struct chain_head, list);
682 res = strcmp(c->name, name);
683 debug("List search name:%s == %s res:%d\n", name, c->name, res);
684 if (res==0)
685 return c;
686
687 /* We can stop earlier as we know list is sorted */
688 if (res>0 && !iptcc_is_builtin(c)) { /* Walked too far*/
689 debug(" Not in list, walked too far, sorted list\n");
690 return NULL;
691 }
692
693 /* Stop on wrap around, if list head is reached */
694 if (pos == &handle->chains) {
695 debug("Stop, list head reached\n");
696 return NULL;
697 }
698 }
699
700 debug("List search NOT found name:%s\n", name);
Harald Welteaae69be2004-08-29 23:32:14 +0000701 return NULL;
702}
703
704/* called when rule is to be removed from cache */
705static void iptcc_delete_rule(struct rule_head *r)
706{
707 DEBUGP("deleting rule %p (offset %u)\n", r, r->offset);
708 /* clean up reference count of called chain */
709 if (r->type == IPTCC_R_JUMP
710 && r->jump)
711 r->jump->references--;
712
713 list_del(&r->list);
714 free(r);
715}
716
717
718/**********************************************************************
719 * RULESET PARSER (blob -> cache)
720 **********************************************************************/
721
Harald Welteaae69be2004-08-29 23:32:14 +0000722/* Delete policy rule of previous chain, since cache doesn't contain
723 * chain policy rules.
724 * WARNING: This function has ugly design and relies on a lot of context, only
725 * to be called from specific places within the parser */
726static int __iptcc_p_del_policy(TC_HANDLE_T h, unsigned int num)
727{
728 if (h->chain_iterator_cur) {
729 /* policy rule is last rule */
730 struct rule_head *pr = (struct rule_head *)
731 h->chain_iterator_cur->rules.prev;
732
733 /* save verdict */
734 h->chain_iterator_cur->verdict =
735 *(int *)GET_TARGET(pr->entry)->data;
736
737 /* save counter and counter_map information */
738 h->chain_iterator_cur->counter_map.maptype =
739 COUNTER_MAP_NORMAL_MAP;
740 h->chain_iterator_cur->counter_map.mappos = num-1;
741 memcpy(&h->chain_iterator_cur->counters, &pr->entry->counters,
742 sizeof(h->chain_iterator_cur->counters));
743
744 /* foot_offset points to verdict rule */
745 h->chain_iterator_cur->foot_index = num;
746 h->chain_iterator_cur->foot_offset = pr->offset;
747
748 /* delete rule from cache */
749 iptcc_delete_rule(pr);
Martin Josefsson8d1b38a2004-09-22 21:00:19 +0000750 h->chain_iterator_cur->num_rules--;
Harald Welteaae69be2004-08-29 23:32:14 +0000751
752 return 1;
753 }
754 return 0;
755}
756
Harald Welteec30b6c2005-02-01 16:45:56 +0000757/* alphabetically insert a chain into the list */
758static inline void iptc_insert_chain(TC_HANDLE_T h, struct chain_head *c)
759{
760 struct chain_head *tmp;
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000761 struct list_head *list_start_pos;
762 unsigned int i=1;
763
764 /* Find a smart place to start the insert search */
765 list_start_pos = iptcc_bsearch_chain_index(c->name, &i, h);
766
767 /* Handle the case, where chain.name is smaller than index[0] */
768 if (i==0 && strcmp(c->name, h->chain_index[0]->name) <= 0) {
769 h->chain_index[0] = c; /* Update chain index head */
770 list_start_pos = h->chains.next;
771 debug("Update chain_index[0] with %s\n", c->name);
772 }
773
774 /* Handel if bsearch bails out early */
775 if (list_start_pos == &h->chains) {
776 list_start_pos = h->chains.next;
777 }
Harald Welteec30b6c2005-02-01 16:45:56 +0000778
Olaf Rempel9d3ed772005-03-04 23:08:30 +0000779 /* sort only user defined chains */
780 if (!c->hooknum) {
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000781 list_for_each_entry(tmp, list_start_pos->prev, list) {
Robert de Barthfeca0572005-07-31 07:04:59 +0000782 if (!tmp->hooknum && strcmp(c->name, tmp->name) <= 0) {
Olaf Rempel9d3ed772005-03-04 23:08:30 +0000783 list_add(&c->list, tmp->list.prev);
784 return;
785 }
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000786
787 /* Stop if list head is reached */
788 if (&tmp->list == &h->chains) {
789 debug("Insert, list head reached add to tail\n");
790 break;
791 }
Harald Welteec30b6c2005-02-01 16:45:56 +0000792 }
793 }
794
795 /* survived till end of list: add at tail */
796 list_add_tail(&c->list, &h->chains);
797}
798
Harald Welteaae69be2004-08-29 23:32:14 +0000799/* Another ugly helper function split out of cache_add_entry to make it less
800 * spaghetti code */
801static void __iptcc_p_add_chain(TC_HANDLE_T h, struct chain_head *c,
802 unsigned int offset, unsigned int *num)
803{
Jesper Dangaard Brouer13364512007-12-12 15:20:42 +0000804 struct list_head *tail = h->chains.prev;
805 struct chain_head *ctail;
806
Harald Welteaae69be2004-08-29 23:32:14 +0000807 __iptcc_p_del_policy(h, *num);
808
809 c->head_offset = offset;
810 c->index = *num;
811
Jesper Dangaard Brouer13364512007-12-12 15:20:42 +0000812 /* Chains from kernel are already sorted, as they are inserted
813 * sorted. But there exists an issue when shifting to 1.4.0
814 * from an older version, as old versions allow last created
815 * chain to be unsorted.
816 */
817 if (iptcc_is_builtin(c)) /* Only user defined chains are sorted*/
818 list_add_tail(&c->list, &h->chains);
819 else {
820 ctail = list_entry(tail, struct chain_head, list);
821 if (strcmp(c->name, ctail->name) > 0)
822 list_add_tail(&c->list, &h->chains);/* Already sorted*/
823 else
824 iptc_insert_chain(h, c);/* Was not sorted */
825 }
Jesper Dangaard Brouerd8cb7872007-11-28 08:40:26 +0000826
Harald Welteaae69be2004-08-29 23:32:14 +0000827 h->chain_iterator_cur = c;
828}
829
830/* main parser function: add an entry from the blob to the cache */
831static int cache_add_entry(STRUCT_ENTRY *e,
832 TC_HANDLE_T h,
833 STRUCT_ENTRY **prev,
834 unsigned int *num)
835{
836 unsigned int builtin;
837 unsigned int offset = (char *)e - (char *)h->entries->entrytable;
838
839 DEBUGP("entering...");
840
841 /* Last entry ("policy rule"). End it.*/
842 if (iptcb_entry2offset(h,e) + e->next_offset == h->entries->size) {
843 /* This is the ERROR node at the end of the chain */
844 DEBUGP_C("%u:%u: end of table:\n", *num, offset);
845
846 __iptcc_p_del_policy(h, *num);
847
848 h->chain_iterator_cur = NULL;
849 goto out_inc;
850 }
851
852 /* We know this is the start of a new chain if it's an ERROR
853 * target, or a hook entry point */
854
855 if (strcmp(GET_TARGET(e)->u.user.name, ERROR_TARGET) == 0) {
856 struct chain_head *c =
857 iptcc_alloc_chain_head((const char *)GET_TARGET(e)->data, 0);
858 DEBUGP_C("%u:%u:new userdefined chain %s: %p\n", *num, offset,
859 (char *)c->name, c);
860 if (!c) {
861 errno = -ENOMEM;
862 return -1;
863 }
Jesper Dangaard Brouer48bde402008-01-15 17:06:48 +0000864 h->num_chains++; /* New user defined chain */
Harald Welteaae69be2004-08-29 23:32:14 +0000865
866 __iptcc_p_add_chain(h, c, offset, num);
867
868 } else if ((builtin = iptcb_ent_is_hook_entry(e, h)) != 0) {
869 struct chain_head *c =
870 iptcc_alloc_chain_head((char *)hooknames[builtin-1],
871 builtin);
872 DEBUGP_C("%u:%u new builtin chain: %p (rules=%p)\n",
873 *num, offset, c, &c->rules);
874 if (!c) {
875 errno = -ENOMEM;
876 return -1;
877 }
878
879 c->hooknum = builtin;
880
881 __iptcc_p_add_chain(h, c, offset, num);
882
883 /* FIXME: this is ugly. */
884 goto new_rule;
885 } else {
886 /* has to be normal rule */
887 struct rule_head *r;
888new_rule:
889
890 if (!(r = iptcc_alloc_rule(h->chain_iterator_cur,
891 e->next_offset))) {
892 errno = ENOMEM;
893 return -1;
894 }
895 DEBUGP_C("%u:%u normal rule: %p: ", *num, offset, r);
896
897 r->index = *num;
898 r->offset = offset;
899 memcpy(r->entry, e, e->next_offset);
900 r->counter_map.maptype = COUNTER_MAP_NORMAL_MAP;
901 r->counter_map.mappos = r->index;
902
903 /* handling of jumps, etc. */
904 if (!strcmp(GET_TARGET(e)->u.user.name, STANDARD_TARGET)) {
905 STRUCT_STANDARD_TARGET *t;
906
907 t = (STRUCT_STANDARD_TARGET *)GET_TARGET(e);
908 if (t->target.u.target_size
909 != ALIGN(sizeof(STRUCT_STANDARD_TARGET))) {
910 errno = EINVAL;
911 return -1;
912 }
913
914 if (t->verdict < 0) {
915 DEBUGP_C("standard, verdict=%d\n", t->verdict);
916 r->type = IPTCC_R_STANDARD;
917 } else if (t->verdict == r->offset+e->next_offset) {
918 DEBUGP_C("fallthrough\n");
919 r->type = IPTCC_R_FALLTHROUGH;
920 } else {
921 DEBUGP_C("jump, target=%u\n", t->verdict);
922 r->type = IPTCC_R_JUMP;
923 /* Jump target fixup has to be deferred
924 * until second pass, since we migh not
925 * yet have parsed the target */
926 }
Martin Josefsson52c38022004-09-22 19:39:40 +0000927 } else {
928 DEBUGP_C("module, target=%s\n", GET_TARGET(e)->u.user.name);
929 r->type = IPTCC_R_MODULE;
Harald Welteaae69be2004-08-29 23:32:14 +0000930 }
931
932 list_add_tail(&r->list, &h->chain_iterator_cur->rules);
Martin Josefsson8d1b38a2004-09-22 21:00:19 +0000933 h->chain_iterator_cur->num_rules++;
Harald Welteaae69be2004-08-29 23:32:14 +0000934 }
935out_inc:
936 (*num)++;
937 return 0;
938}
939
940
941/* parse an iptables blob into it's pieces */
942static int parse_table(TC_HANDLE_T h)
943{
944 STRUCT_ENTRY *prev;
945 unsigned int num = 0;
946 struct chain_head *c;
947
948 /* First pass: over ruleset blob */
949 ENTRY_ITERATE(h->entries->entrytable, h->entries->size,
950 cache_add_entry, h, &prev, &num);
951
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000952 /* Build the chain index, used for chain list search speedup */
953 if ((iptcc_chain_index_alloc(h)) < 0)
954 return -ENOMEM;
955 iptcc_chain_index_build(h);
956
Harald Welteaae69be2004-08-29 23:32:14 +0000957 /* Second pass: fixup parsed data from first pass */
958 list_for_each_entry(c, &h->chains, list) {
959 struct rule_head *r;
960 list_for_each_entry(r, &c->rules, list) {
Jan Engelhardtdbb77542008-02-11 00:33:30 +0100961 struct chain_head *lc;
Harald Welteaae69be2004-08-29 23:32:14 +0000962 STRUCT_STANDARD_TARGET *t;
963
964 if (r->type != IPTCC_R_JUMP)
965 continue;
966
967 t = (STRUCT_STANDARD_TARGET *)GET_TARGET(r->entry);
Jan Engelhardtdbb77542008-02-11 00:33:30 +0100968 lc = iptcc_find_chain_by_offset(h, t->verdict);
969 if (!lc)
Harald Welteaae69be2004-08-29 23:32:14 +0000970 return -1;
Jan Engelhardtdbb77542008-02-11 00:33:30 +0100971 r->jump = lc;
972 lc->references++;
Harald Welteaae69be2004-08-29 23:32:14 +0000973 }
974 }
975
976 /* FIXME: sort chains */
977
978 return 1;
979}
980
981
982/**********************************************************************
983 * RULESET COMPILATION (cache -> blob)
984 **********************************************************************/
985
986/* Convenience structures */
987struct iptcb_chain_start{
988 STRUCT_ENTRY e;
989 struct ipt_error_target name;
990};
991#define IPTCB_CHAIN_START_SIZE (sizeof(STRUCT_ENTRY) + \
992 ALIGN(sizeof(struct ipt_error_target)))
993
994struct iptcb_chain_foot {
995 STRUCT_ENTRY e;
996 STRUCT_STANDARD_TARGET target;
997};
998#define IPTCB_CHAIN_FOOT_SIZE (sizeof(STRUCT_ENTRY) + \
999 ALIGN(sizeof(STRUCT_STANDARD_TARGET)))
1000
1001struct iptcb_chain_error {
1002 STRUCT_ENTRY entry;
1003 struct ipt_error_target target;
1004};
1005#define IPTCB_CHAIN_ERROR_SIZE (sizeof(STRUCT_ENTRY) + \
1006 ALIGN(sizeof(struct ipt_error_target)))
1007
1008
1009
1010/* compile rule from cache into blob */
1011static inline int iptcc_compile_rule (TC_HANDLE_T h, STRUCT_REPLACE *repl, struct rule_head *r)
1012{
1013 /* handle jumps */
1014 if (r->type == IPTCC_R_JUMP) {
1015 STRUCT_STANDARD_TARGET *t;
1016 t = (STRUCT_STANDARD_TARGET *)GET_TARGET(r->entry);
1017 /* memset for memcmp convenience on delete/replace */
1018 memset(t->target.u.user.name, 0, FUNCTION_MAXNAMELEN);
1019 strcpy(t->target.u.user.name, STANDARD_TARGET);
1020 /* Jumps can only happen to builtin chains, so we
1021 * can safely assume that they always have a header */
1022 t->verdict = r->jump->head_offset + IPTCB_CHAIN_START_SIZE;
1023 } else if (r->type == IPTCC_R_FALLTHROUGH) {
1024 STRUCT_STANDARD_TARGET *t;
1025 t = (STRUCT_STANDARD_TARGET *)GET_TARGET(r->entry);
1026 t->verdict = r->offset + r->size;
1027 }
1028
1029 /* copy entry from cache to blob */
1030 memcpy((char *)repl->entries+r->offset, r->entry, r->size);
1031
1032 return 1;
1033}
1034
1035/* compile chain from cache into blob */
1036static int iptcc_compile_chain(TC_HANDLE_T h, STRUCT_REPLACE *repl, struct chain_head *c)
1037{
1038 int ret;
1039 struct rule_head *r;
1040 struct iptcb_chain_start *head;
1041 struct iptcb_chain_foot *foot;
1042
1043 /* only user-defined chains have heaer */
1044 if (!iptcc_is_builtin(c)) {
1045 /* put chain header in place */
1046 head = (void *)repl->entries + c->head_offset;
1047 head->e.target_offset = sizeof(STRUCT_ENTRY);
1048 head->e.next_offset = IPTCB_CHAIN_START_SIZE;
1049 strcpy(head->name.t.u.user.name, ERROR_TARGET);
1050 head->name.t.u.target_size =
1051 ALIGN(sizeof(struct ipt_error_target));
1052 strcpy(head->name.error, c->name);
1053 } else {
1054 repl->hook_entry[c->hooknum-1] = c->head_offset;
1055 repl->underflow[c->hooknum-1] = c->foot_offset;
1056 }
1057
1058 /* iterate over rules */
1059 list_for_each_entry(r, &c->rules, list) {
1060 ret = iptcc_compile_rule(h, repl, r);
1061 if (ret < 0)
1062 return ret;
1063 }
1064
1065 /* put chain footer in place */
1066 foot = (void *)repl->entries + c->foot_offset;
1067 foot->e.target_offset = sizeof(STRUCT_ENTRY);
1068 foot->e.next_offset = IPTCB_CHAIN_FOOT_SIZE;
1069 strcpy(foot->target.target.u.user.name, STANDARD_TARGET);
1070 foot->target.target.u.target_size =
1071 ALIGN(sizeof(STRUCT_STANDARD_TARGET));
1072 /* builtin targets have verdict, others return */
1073 if (iptcc_is_builtin(c))
1074 foot->target.verdict = c->verdict;
1075 else
1076 foot->target.verdict = RETURN;
1077 /* set policy-counters */
1078 memcpy(&foot->e.counters, &c->counters, sizeof(STRUCT_COUNTERS));
1079
1080 return 0;
1081}
1082
1083/* calculate offset and number for every rule in the cache */
1084static int iptcc_compile_chain_offsets(TC_HANDLE_T h, struct chain_head *c,
Harald Welteefa8fc22005-07-19 22:03:49 +00001085 unsigned int *offset, unsigned int *num)
Harald Welteaae69be2004-08-29 23:32:14 +00001086{
1087 struct rule_head *r;
1088
1089 c->head_offset = *offset;
1090 DEBUGP("%s: chain_head %u, offset=%u\n", c->name, *num, *offset);
1091
1092 if (!iptcc_is_builtin(c)) {
1093 /* Chain has header */
1094 *offset += sizeof(STRUCT_ENTRY)
1095 + ALIGN(sizeof(struct ipt_error_target));
1096 (*num)++;
1097 }
1098
1099 list_for_each_entry(r, &c->rules, list) {
1100 DEBUGP("rule %u, offset=%u, index=%u\n", *num, *offset, *num);
1101 r->offset = *offset;
1102 r->index = *num;
1103 *offset += r->size;
1104 (*num)++;
1105 }
1106
1107 DEBUGP("%s; chain_foot %u, offset=%u, index=%u\n", c->name, *num,
1108 *offset, *num);
1109 c->foot_offset = *offset;
1110 c->foot_index = *num;
1111 *offset += sizeof(STRUCT_ENTRY)
1112 + ALIGN(sizeof(STRUCT_STANDARD_TARGET));
1113 (*num)++;
1114
1115 return 1;
1116}
1117
1118/* put the pieces back together again */
1119static int iptcc_compile_table_prep(TC_HANDLE_T h, unsigned int *size)
1120{
1121 struct chain_head *c;
1122 unsigned int offset = 0, num = 0;
1123 int ret = 0;
1124
1125 /* First pass: calculate offset for every rule */
1126 list_for_each_entry(c, &h->chains, list) {
1127 ret = iptcc_compile_chain_offsets(h, c, &offset, &num);
1128 if (ret < 0)
1129 return ret;
1130 }
1131
1132 /* Append one error rule at end of chain */
1133 num++;
1134 offset += sizeof(STRUCT_ENTRY)
1135 + ALIGN(sizeof(struct ipt_error_target));
1136
1137 /* ruleset size is now in offset */
1138 *size = offset;
1139 return num;
1140}
1141
1142static int iptcc_compile_table(TC_HANDLE_T h, STRUCT_REPLACE *repl)
1143{
1144 struct chain_head *c;
1145 struct iptcb_chain_error *error;
1146
1147 /* Second pass: copy from cache to offsets, fill in jumps */
1148 list_for_each_entry(c, &h->chains, list) {
1149 int ret = iptcc_compile_chain(h, repl, c);
1150 if (ret < 0)
1151 return ret;
1152 }
1153
1154 /* Append error rule at end of chain */
1155 error = (void *)repl->entries + repl->size - IPTCB_CHAIN_ERROR_SIZE;
1156 error->entry.target_offset = sizeof(STRUCT_ENTRY);
1157 error->entry.next_offset = IPTCB_CHAIN_ERROR_SIZE;
1158 error->target.t.u.user.target_size =
1159 ALIGN(sizeof(struct ipt_error_target));
1160 strcpy((char *)&error->target.t.u.user.name, ERROR_TARGET);
1161 strcpy((char *)&error->target.error, "ERROR");
1162
1163 return 1;
1164}
1165
1166/**********************************************************************
1167 * EXTERNAL API (operates on cache only)
1168 **********************************************************************/
Marc Bouchere6869a82000-03-20 06:03:29 +00001169
1170/* Allocate handle of given size */
Rusty Russell79dee072000-05-02 16:45:16 +00001171static TC_HANDLE_T
Harald Welte0113fe72004-01-06 19:04:02 +00001172alloc_handle(const char *tablename, unsigned int size, unsigned int num_rules)
Marc Bouchere6869a82000-03-20 06:03:29 +00001173{
1174 size_t len;
Rusty Russell79dee072000-05-02 16:45:16 +00001175 TC_HANDLE_T h;
Marc Bouchere6869a82000-03-20 06:03:29 +00001176
Harald Welteaae69be2004-08-29 23:32:14 +00001177 len = sizeof(STRUCT_TC_HANDLE) + size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001178
Harald Welteaae69be2004-08-29 23:32:14 +00001179 h = malloc(sizeof(STRUCT_TC_HANDLE));
1180 if (!h) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001181 errno = ENOMEM;
1182 return NULL;
1183 }
Harald Welteaae69be2004-08-29 23:32:14 +00001184 memset(h, 0, sizeof(*h));
1185 INIT_LIST_HEAD(&h->chains);
Marc Bouchere6869a82000-03-20 06:03:29 +00001186 strcpy(h->info.name, tablename);
Harald Welteaae69be2004-08-29 23:32:14 +00001187
Harald Welte0371c0c2004-09-19 21:00:12 +00001188 h->entries = malloc(sizeof(STRUCT_GET_ENTRIES) + size);
Harald Welteaae69be2004-08-29 23:32:14 +00001189 if (!h->entries)
1190 goto out_free_handle;
1191
1192 strcpy(h->entries->name, tablename);
Harald Welte0371c0c2004-09-19 21:00:12 +00001193 h->entries->size = size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001194
1195 return h;
Harald Welteaae69be2004-08-29 23:32:14 +00001196
1197out_free_handle:
1198 free(h);
1199
1200 return NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +00001201}
1202
Harald Welteaae69be2004-08-29 23:32:14 +00001203
Rusty Russell79dee072000-05-02 16:45:16 +00001204TC_HANDLE_T
1205TC_INIT(const char *tablename)
Marc Bouchere6869a82000-03-20 06:03:29 +00001206{
Rusty Russell79dee072000-05-02 16:45:16 +00001207 TC_HANDLE_T h;
1208 STRUCT_GETINFO info;
Harald Welteefa8fc22005-07-19 22:03:49 +00001209 unsigned int tmp;
Marc Bouchere6869a82000-03-20 06:03:29 +00001210 socklen_t s;
1211
Rusty Russell79dee072000-05-02 16:45:16 +00001212 iptc_fn = TC_INIT;
Marc Bouchere6869a82000-03-20 06:03:29 +00001213
Martin Josefsson841e4ae2003-05-02 15:30:11 +00001214 if (strlen(tablename) >= TABLE_MAXNAMELEN) {
1215 errno = EINVAL;
1216 return NULL;
1217 }
1218
Derrik Pates664c0a32005-02-01 13:28:14 +00001219 if (sockfd_use == 0) {
1220 sockfd = socket(TC_AF, SOCK_RAW, IPPROTO_RAW);
1221 if (sockfd < 0)
1222 return NULL;
1223 }
1224 sockfd_use++;
Patrick McHardy2f932052008-04-02 14:01:53 +02001225retry:
Marc Bouchere6869a82000-03-20 06:03:29 +00001226 s = sizeof(info);
Martin Josefsson841e4ae2003-05-02 15:30:11 +00001227
Marc Bouchere6869a82000-03-20 06:03:29 +00001228 strcpy(info.name, tablename);
Derrik Pates664c0a32005-02-01 13:28:14 +00001229 if (getsockopt(sockfd, TC_IPPROTO, SO_GET_INFO, &info, &s) < 0) {
1230 if (--sockfd_use == 0) {
1231 close(sockfd);
1232 sockfd = -1;
1233 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001234 return NULL;
Derrik Pates664c0a32005-02-01 13:28:14 +00001235 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001236
Harald Welteaae69be2004-08-29 23:32:14 +00001237 DEBUGP("valid_hooks=0x%08x, num_entries=%u, size=%u\n",
1238 info.valid_hooks, info.num_entries, info.size);
1239
Harald Welte0113fe72004-01-06 19:04:02 +00001240 if ((h = alloc_handle(info.name, info.size, info.num_entries))
Martin Josefsson841e4ae2003-05-02 15:30:11 +00001241 == NULL) {
Derrik Pates664c0a32005-02-01 13:28:14 +00001242 if (--sockfd_use == 0) {
1243 close(sockfd);
1244 sockfd = -1;
1245 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001246 return NULL;
Martin Josefsson841e4ae2003-05-02 15:30:11 +00001247 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001248
Marc Bouchere6869a82000-03-20 06:03:29 +00001249 /* Initialize current state */
1250 h->info = info;
Harald Welte0113fe72004-01-06 19:04:02 +00001251
Harald Welteaae69be2004-08-29 23:32:14 +00001252 h->entries->size = h->info.size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001253
Rusty Russell79dee072000-05-02 16:45:16 +00001254 tmp = sizeof(STRUCT_GET_ENTRIES) + h->info.size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001255
Harald Welteaae69be2004-08-29 23:32:14 +00001256 if (getsockopt(sockfd, TC_IPPROTO, SO_GET_ENTRIES, h->entries,
1257 &tmp) < 0)
1258 goto error;
1259
1260#ifdef IPTC_DEBUG2
1261 {
1262 int fd = open("/tmp/libiptc-so_get_entries.blob",
1263 O_CREAT|O_WRONLY);
1264 if (fd >= 0) {
1265 write(fd, h->entries, tmp);
1266 close(fd);
1267 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001268 }
Harald Welteaae69be2004-08-29 23:32:14 +00001269#endif
1270
1271 if (parse_table(h) < 0)
1272 goto error;
Rusty Russell7e53bf92000-03-20 07:03:28 +00001273
Marc Bouchere6869a82000-03-20 06:03:29 +00001274 CHECK(h);
1275 return h;
Harald Welteaae69be2004-08-29 23:32:14 +00001276error:
1277 TC_FREE(&h);
Patrick McHardy2f932052008-04-02 14:01:53 +02001278 /* A different process changed the ruleset size, retry */
1279 if (errno == EAGAIN)
1280 goto retry;
Harald Welteaae69be2004-08-29 23:32:14 +00001281 return NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +00001282}
1283
Martin Josefsson841e4ae2003-05-02 15:30:11 +00001284void
1285TC_FREE(TC_HANDLE_T *h)
1286{
Harald Welteaae69be2004-08-29 23:32:14 +00001287 struct chain_head *c, *tmp;
1288
Derrik Pates664c0a32005-02-01 13:28:14 +00001289 iptc_fn = TC_FREE;
1290 if (--sockfd_use == 0) {
1291 close(sockfd);
1292 sockfd = -1;
1293 }
Harald Welteaae69be2004-08-29 23:32:14 +00001294
1295 list_for_each_entry_safe(c, tmp, &(*h)->chains, list) {
1296 struct rule_head *r, *rtmp;
1297
1298 list_for_each_entry_safe(r, rtmp, &c->rules, list) {
1299 free(r);
1300 }
1301
1302 free(c);
1303 }
1304
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +00001305 iptcc_chain_index_free(*h);
1306
Harald Welteaae69be2004-08-29 23:32:14 +00001307 free((*h)->entries);
Martin Josefsson841e4ae2003-05-02 15:30:11 +00001308 free(*h);
Harald Welteaae69be2004-08-29 23:32:14 +00001309
Martin Josefsson841e4ae2003-05-02 15:30:11 +00001310 *h = NULL;
1311}
1312
Marc Bouchere6869a82000-03-20 06:03:29 +00001313static inline int
Rusty Russell79dee072000-05-02 16:45:16 +00001314print_match(const STRUCT_ENTRY_MATCH *m)
Marc Bouchere6869a82000-03-20 06:03:29 +00001315{
Rusty Russell228e98d2000-04-27 10:28:06 +00001316 printf("Match name: `%s'\n", m->u.user.name);
Marc Bouchere6869a82000-03-20 06:03:29 +00001317 return 0;
1318}
1319
Rusty Russell79dee072000-05-02 16:45:16 +00001320static int dump_entry(STRUCT_ENTRY *e, const TC_HANDLE_T handle);
1321
Marc Bouchere6869a82000-03-20 06:03:29 +00001322void
Rusty Russell79dee072000-05-02 16:45:16 +00001323TC_DUMP_ENTRIES(const TC_HANDLE_T handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001324{
Derrik Pates664c0a32005-02-01 13:28:14 +00001325 iptc_fn = TC_DUMP_ENTRIES;
Marc Bouchere6869a82000-03-20 06:03:29 +00001326 CHECK(handle);
Patrick McHardy97fb2f12007-09-08 16:52:25 +00001327
Rusty Russelle45c7132004-12-16 13:21:44 +00001328 printf("libiptc v%s. %u bytes.\n",
1329 IPTABLES_VERSION, handle->entries->size);
Marc Bouchere6869a82000-03-20 06:03:29 +00001330 printf("Table `%s'\n", handle->info.name);
1331 printf("Hooks: pre/in/fwd/out/post = %u/%u/%u/%u/%u\n",
Rusty Russell67088e72000-05-10 01:18:57 +00001332 handle->info.hook_entry[HOOK_PRE_ROUTING],
1333 handle->info.hook_entry[HOOK_LOCAL_IN],
1334 handle->info.hook_entry[HOOK_FORWARD],
1335 handle->info.hook_entry[HOOK_LOCAL_OUT],
1336 handle->info.hook_entry[HOOK_POST_ROUTING]);
Marc Bouchere6869a82000-03-20 06:03:29 +00001337 printf("Underflows: pre/in/fwd/out/post = %u/%u/%u/%u/%u\n",
Rusty Russell67088e72000-05-10 01:18:57 +00001338 handle->info.underflow[HOOK_PRE_ROUTING],
1339 handle->info.underflow[HOOK_LOCAL_IN],
1340 handle->info.underflow[HOOK_FORWARD],
1341 handle->info.underflow[HOOK_LOCAL_OUT],
1342 handle->info.underflow[HOOK_POST_ROUTING]);
Marc Bouchere6869a82000-03-20 06:03:29 +00001343
Harald Welteaae69be2004-08-29 23:32:14 +00001344 ENTRY_ITERATE(handle->entries->entrytable, handle->entries->size,
Rusty Russell79dee072000-05-02 16:45:16 +00001345 dump_entry, handle);
Harald Welte0113fe72004-01-06 19:04:02 +00001346}
Rusty Russell30fd6e52000-04-23 09:16:06 +00001347
Marc Bouchere6869a82000-03-20 06:03:29 +00001348/* Does this chain exist? */
Rusty Russell79dee072000-05-02 16:45:16 +00001349int TC_IS_CHAIN(const char *chain, const TC_HANDLE_T handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001350{
Derrik Pates664c0a32005-02-01 13:28:14 +00001351 iptc_fn = TC_IS_CHAIN;
Harald Welteaae69be2004-08-29 23:32:14 +00001352 return iptcc_find_label(chain, handle) != NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +00001353}
1354
Harald Welteaae69be2004-08-29 23:32:14 +00001355static void iptcc_chain_iterator_advance(TC_HANDLE_T handle)
1356{
1357 struct chain_head *c = handle->chain_iterator_cur;
1358
1359 if (c->list.next == &handle->chains)
1360 handle->chain_iterator_cur = NULL;
1361 else
1362 handle->chain_iterator_cur =
1363 list_entry(c->list.next, struct chain_head, list);
1364}
Marc Bouchere6869a82000-03-20 06:03:29 +00001365
Rusty Russell30fd6e52000-04-23 09:16:06 +00001366/* Iterator functions to run through the chains. */
Marc Bouchere6869a82000-03-20 06:03:29 +00001367const char *
Philip Blundell8c700902000-05-15 02:17:52 +00001368TC_FIRST_CHAIN(TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001369{
Harald Welteaae69be2004-08-29 23:32:14 +00001370 struct chain_head *c = list_entry((*handle)->chains.next,
1371 struct chain_head, list);
1372
1373 iptc_fn = TC_FIRST_CHAIN;
1374
1375
1376 if (list_empty(&(*handle)->chains)) {
1377 DEBUGP(": no chains\n");
Harald Welte0113fe72004-01-06 19:04:02 +00001378 return NULL;
Harald Welteaae69be2004-08-29 23:32:14 +00001379 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001380
Harald Welteaae69be2004-08-29 23:32:14 +00001381 (*handle)->chain_iterator_cur = c;
1382 iptcc_chain_iterator_advance(*handle);
Harald Welte0113fe72004-01-06 19:04:02 +00001383
Harald Welteaae69be2004-08-29 23:32:14 +00001384 DEBUGP(": returning `%s'\n", c->name);
1385 return c->name;
Marc Bouchere6869a82000-03-20 06:03:29 +00001386}
1387
Rusty Russell30fd6e52000-04-23 09:16:06 +00001388/* Iterator functions to run through the chains. Returns NULL at end. */
1389const char *
Rusty Russell79dee072000-05-02 16:45:16 +00001390TC_NEXT_CHAIN(TC_HANDLE_T *handle)
Rusty Russell30fd6e52000-04-23 09:16:06 +00001391{
Harald Welteaae69be2004-08-29 23:32:14 +00001392 struct chain_head *c = (*handle)->chain_iterator_cur;
Rusty Russell30fd6e52000-04-23 09:16:06 +00001393
Harald Welteaae69be2004-08-29 23:32:14 +00001394 iptc_fn = TC_NEXT_CHAIN;
1395
1396 if (!c) {
1397 DEBUGP(": no more chains\n");
Rusty Russell30fd6e52000-04-23 09:16:06 +00001398 return NULL;
Harald Welteaae69be2004-08-29 23:32:14 +00001399 }
Rusty Russell30fd6e52000-04-23 09:16:06 +00001400
Harald Welteaae69be2004-08-29 23:32:14 +00001401 iptcc_chain_iterator_advance(*handle);
1402
1403 DEBUGP(": returning `%s'\n", c->name);
1404 return c->name;
Rusty Russell30fd6e52000-04-23 09:16:06 +00001405}
1406
1407/* Get first rule in the given chain: NULL for empty chain. */
Rusty Russell79dee072000-05-02 16:45:16 +00001408const STRUCT_ENTRY *
Philip Blundell8c700902000-05-15 02:17:52 +00001409TC_FIRST_RULE(const char *chain, TC_HANDLE_T *handle)
Rusty Russell30fd6e52000-04-23 09:16:06 +00001410{
Harald Welteaae69be2004-08-29 23:32:14 +00001411 struct chain_head *c;
1412 struct rule_head *r;
Rusty Russell30fd6e52000-04-23 09:16:06 +00001413
Harald Welteaae69be2004-08-29 23:32:14 +00001414 iptc_fn = TC_FIRST_RULE;
1415
1416 DEBUGP("first rule(%s): ", chain);
1417
1418 c = iptcc_find_label(chain, *handle);
Rusty Russell30fd6e52000-04-23 09:16:06 +00001419 if (!c) {
1420 errno = ENOENT;
1421 return NULL;
1422 }
1423
1424 /* Empty chain: single return/policy rule */
Harald Welteaae69be2004-08-29 23:32:14 +00001425 if (list_empty(&c->rules)) {
1426 DEBUGP_C("no rules, returning NULL\n");
Rusty Russell30fd6e52000-04-23 09:16:06 +00001427 return NULL;
Harald Welteaae69be2004-08-29 23:32:14 +00001428 }
Rusty Russell30fd6e52000-04-23 09:16:06 +00001429
Harald Welteaae69be2004-08-29 23:32:14 +00001430 r = list_entry(c->rules.next, struct rule_head, list);
1431 (*handle)->rule_iterator_cur = r;
1432 DEBUGP_C("%p\n", r);
1433
1434 return r->entry;
Rusty Russell30fd6e52000-04-23 09:16:06 +00001435}
1436
1437/* Returns NULL when rules run out. */
Rusty Russell79dee072000-05-02 16:45:16 +00001438const STRUCT_ENTRY *
Philip Blundell8c700902000-05-15 02:17:52 +00001439TC_NEXT_RULE(const STRUCT_ENTRY *prev, TC_HANDLE_T *handle)
Rusty Russell30fd6e52000-04-23 09:16:06 +00001440{
Harald Welteaae69be2004-08-29 23:32:14 +00001441 struct rule_head *r;
Rusty Russell30fd6e52000-04-23 09:16:06 +00001442
Derrik Pates664c0a32005-02-01 13:28:14 +00001443 iptc_fn = TC_NEXT_RULE;
Harald Welteaae69be2004-08-29 23:32:14 +00001444 DEBUGP("rule_iterator_cur=%p...", (*handle)->rule_iterator_cur);
1445
1446 if (!(*handle)->rule_iterator_cur) {
1447 DEBUGP_C("returning NULL\n");
1448 return NULL;
1449 }
1450
1451 r = list_entry((*handle)->rule_iterator_cur->list.next,
1452 struct rule_head, list);
1453
1454 iptc_fn = TC_NEXT_RULE;
1455
1456 DEBUGP_C("next=%p, head=%p...", &r->list,
1457 &(*handle)->rule_iterator_cur->chain->rules);
1458
1459 if (&r->list == &(*handle)->rule_iterator_cur->chain->rules) {
1460 (*handle)->rule_iterator_cur = NULL;
1461 DEBUGP_C("finished, returning NULL\n");
1462 return NULL;
1463 }
1464
1465 (*handle)->rule_iterator_cur = r;
1466
1467 /* NOTE: prev is without any influence ! */
1468 DEBUGP_C("returning rule %p\n", r);
1469 return r->entry;
Rusty Russell30fd6e52000-04-23 09:16:06 +00001470}
1471
Marc Bouchere6869a82000-03-20 06:03:29 +00001472/* How many rules in this chain? */
Jan Engelhardt33690a12008-02-11 00:54:00 +01001473static unsigned int
Rusty Russell79dee072000-05-02 16:45:16 +00001474TC_NUM_RULES(const char *chain, TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001475{
Harald Welteaae69be2004-08-29 23:32:14 +00001476 struct chain_head *c;
1477 iptc_fn = TC_NUM_RULES;
Marc Bouchere6869a82000-03-20 06:03:29 +00001478 CHECK(*handle);
Harald Welteaae69be2004-08-29 23:32:14 +00001479
1480 c = iptcc_find_label(chain, *handle);
1481 if (!c) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001482 errno = ENOENT;
1483 return (unsigned int)-1;
1484 }
Harald Welteaae69be2004-08-29 23:32:14 +00001485
1486 return c->num_rules;
Marc Bouchere6869a82000-03-20 06:03:29 +00001487}
1488
Jan Engelhardt33690a12008-02-11 00:54:00 +01001489static const STRUCT_ENTRY *
1490TC_GET_RULE(const char *chain, unsigned int n, TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001491{
Harald Welteaae69be2004-08-29 23:32:14 +00001492 struct chain_head *c;
1493 struct rule_head *r;
1494
1495 iptc_fn = TC_GET_RULE;
Marc Bouchere6869a82000-03-20 06:03:29 +00001496
1497 CHECK(*handle);
Harald Welteaae69be2004-08-29 23:32:14 +00001498
1499 c = iptcc_find_label(chain, *handle);
1500 if (!c) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001501 errno = ENOENT;
1502 return NULL;
1503 }
1504
Harald Welteaae69be2004-08-29 23:32:14 +00001505 r = iptcc_get_rule_num(c, n);
1506 if (!r)
1507 return NULL;
1508 return r->entry;
Marc Bouchere6869a82000-03-20 06:03:29 +00001509}
1510
1511/* Returns a pointer to the target name of this position. */
Jan Engelhardt33690a12008-02-11 00:54:00 +01001512static const char *standard_target_map(int verdict)
Marc Bouchere6869a82000-03-20 06:03:29 +00001513{
Harald Welteaae69be2004-08-29 23:32:14 +00001514 switch (verdict) {
1515 case RETURN:
1516 return LABEL_RETURN;
1517 break;
1518 case -NF_ACCEPT-1:
1519 return LABEL_ACCEPT;
1520 break;
1521 case -NF_DROP-1:
1522 return LABEL_DROP;
1523 break;
1524 case -NF_QUEUE-1:
1525 return LABEL_QUEUE;
1526 break;
1527 default:
1528 fprintf(stderr, "ERROR: %d not a valid target)\n",
1529 verdict);
1530 abort();
1531 break;
1532 }
1533 /* not reached */
1534 return NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +00001535}
1536
Harald Welteaae69be2004-08-29 23:32:14 +00001537/* Returns a pointer to the target name of this position. */
1538const char *TC_GET_TARGET(const STRUCT_ENTRY *ce,
1539 TC_HANDLE_T *handle)
1540{
1541 STRUCT_ENTRY *e = (STRUCT_ENTRY *)ce;
Rusty Russelle45c7132004-12-16 13:21:44 +00001542 struct rule_head *r = container_of(e, struct rule_head, entry[0]);
Harald Welteaae69be2004-08-29 23:32:14 +00001543
1544 iptc_fn = TC_GET_TARGET;
1545
1546 switch(r->type) {
1547 int spos;
1548 case IPTCC_R_FALLTHROUGH:
1549 return "";
1550 break;
1551 case IPTCC_R_JUMP:
1552 DEBUGP("r=%p, jump=%p, name=`%s'\n", r, r->jump, r->jump->name);
1553 return r->jump->name;
1554 break;
1555 case IPTCC_R_STANDARD:
1556 spos = *(int *)GET_TARGET(e)->data;
1557 DEBUGP("r=%p, spos=%d'\n", r, spos);
1558 return standard_target_map(spos);
1559 break;
1560 case IPTCC_R_MODULE:
1561 return GET_TARGET(e)->u.user.name;
1562 break;
1563 }
1564 return NULL;
1565}
Marc Bouchere6869a82000-03-20 06:03:29 +00001566/* Is this a built-in chain? Actually returns hook + 1. */
1567int
Rusty Russell79dee072000-05-02 16:45:16 +00001568TC_BUILTIN(const char *chain, const TC_HANDLE_T handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001569{
Harald Welteaae69be2004-08-29 23:32:14 +00001570 struct chain_head *c;
1571
1572 iptc_fn = TC_BUILTIN;
Marc Bouchere6869a82000-03-20 06:03:29 +00001573
Harald Welteaae69be2004-08-29 23:32:14 +00001574 c = iptcc_find_label(chain, handle);
1575 if (!c) {
1576 errno = ENOENT;
Martin Josefssonb0f3d2d2004-09-23 18:23:20 +00001577 return 0;
Marc Bouchere6869a82000-03-20 06:03:29 +00001578 }
Harald Welteaae69be2004-08-29 23:32:14 +00001579
1580 return iptcc_is_builtin(c);
Marc Bouchere6869a82000-03-20 06:03:29 +00001581}
1582
1583/* Get the policy of a given built-in chain */
1584const char *
Rusty Russell79dee072000-05-02 16:45:16 +00001585TC_GET_POLICY(const char *chain,
1586 STRUCT_COUNTERS *counters,
1587 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001588{
Harald Welteaae69be2004-08-29 23:32:14 +00001589 struct chain_head *c;
Marc Bouchere6869a82000-03-20 06:03:29 +00001590
Harald Welteaae69be2004-08-29 23:32:14 +00001591 iptc_fn = TC_GET_POLICY;
1592
1593 DEBUGP("called for chain %s\n", chain);
1594
1595 c = iptcc_find_label(chain, *handle);
1596 if (!c) {
1597 errno = ENOENT;
1598 return NULL;
1599 }
1600
1601 if (!iptcc_is_builtin(c))
Marc Bouchere6869a82000-03-20 06:03:29 +00001602 return NULL;
1603
Harald Welteaae69be2004-08-29 23:32:14 +00001604 *counters = c->counters;
Marc Bouchere6869a82000-03-20 06:03:29 +00001605
Harald Welteaae69be2004-08-29 23:32:14 +00001606 return standard_target_map(c->verdict);
Harald Welte0113fe72004-01-06 19:04:02 +00001607}
1608
1609static int
Harald Welteaae69be2004-08-29 23:32:14 +00001610iptcc_standard_map(struct rule_head *r, int verdict)
Harald Welte0113fe72004-01-06 19:04:02 +00001611{
Harald Welteaae69be2004-08-29 23:32:14 +00001612 STRUCT_ENTRY *e = r->entry;
Rusty Russell79dee072000-05-02 16:45:16 +00001613 STRUCT_STANDARD_TARGET *t;
Marc Bouchere6869a82000-03-20 06:03:29 +00001614
Rusty Russell79dee072000-05-02 16:45:16 +00001615 t = (STRUCT_STANDARD_TARGET *)GET_TARGET(e);
Marc Bouchere6869a82000-03-20 06:03:29 +00001616
Rusty Russell67088e72000-05-10 01:18:57 +00001617 if (t->target.u.target_size
Philip Blundell8c700902000-05-15 02:17:52 +00001618 != ALIGN(sizeof(STRUCT_STANDARD_TARGET))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001619 errno = EINVAL;
1620 return 0;
1621 }
1622 /* memset for memcmp convenience on delete/replace */
Rusty Russell79dee072000-05-02 16:45:16 +00001623 memset(t->target.u.user.name, 0, FUNCTION_MAXNAMELEN);
1624 strcpy(t->target.u.user.name, STANDARD_TARGET);
Marc Bouchere6869a82000-03-20 06:03:29 +00001625 t->verdict = verdict;
1626
Harald Welteaae69be2004-08-29 23:32:14 +00001627 r->type = IPTCC_R_STANDARD;
1628
Marc Bouchere6869a82000-03-20 06:03:29 +00001629 return 1;
1630}
Rusty Russell7e53bf92000-03-20 07:03:28 +00001631
Marc Bouchere6869a82000-03-20 06:03:29 +00001632static int
Harald Welteaae69be2004-08-29 23:32:14 +00001633iptcc_map_target(const TC_HANDLE_T handle,
1634 struct rule_head *r)
Marc Bouchere6869a82000-03-20 06:03:29 +00001635{
Harald Welteaae69be2004-08-29 23:32:14 +00001636 STRUCT_ENTRY *e = r->entry;
Harald Welte0113fe72004-01-06 19:04:02 +00001637 STRUCT_ENTRY_TARGET *t = GET_TARGET(e);
Marc Bouchere6869a82000-03-20 06:03:29 +00001638
Marc Bouchere6869a82000-03-20 06:03:29 +00001639 /* Maybe it's empty (=> fall through) */
Harald Welteaae69be2004-08-29 23:32:14 +00001640 if (strcmp(t->u.user.name, "") == 0) {
1641 r->type = IPTCC_R_FALLTHROUGH;
1642 return 1;
1643 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001644 /* Maybe it's a standard target name... */
Rusty Russell79dee072000-05-02 16:45:16 +00001645 else if (strcmp(t->u.user.name, LABEL_ACCEPT) == 0)
Harald Welteaae69be2004-08-29 23:32:14 +00001646 return iptcc_standard_map(r, -NF_ACCEPT - 1);
Rusty Russell79dee072000-05-02 16:45:16 +00001647 else if (strcmp(t->u.user.name, LABEL_DROP) == 0)
Harald Welteaae69be2004-08-29 23:32:14 +00001648 return iptcc_standard_map(r, -NF_DROP - 1);
Rusty Russell67088e72000-05-10 01:18:57 +00001649 else if (strcmp(t->u.user.name, LABEL_QUEUE) == 0)
Harald Welteaae69be2004-08-29 23:32:14 +00001650 return iptcc_standard_map(r, -NF_QUEUE - 1);
Rusty Russell79dee072000-05-02 16:45:16 +00001651 else if (strcmp(t->u.user.name, LABEL_RETURN) == 0)
Harald Welteaae69be2004-08-29 23:32:14 +00001652 return iptcc_standard_map(r, RETURN);
Rusty Russell79dee072000-05-02 16:45:16 +00001653 else if (TC_BUILTIN(t->u.user.name, handle)) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001654 /* Can't jump to builtins. */
1655 errno = EINVAL;
1656 return 0;
1657 } else {
1658 /* Maybe it's an existing chain name. */
Harald Welteaae69be2004-08-29 23:32:14 +00001659 struct chain_head *c;
1660 DEBUGP("trying to find chain `%s': ", t->u.user.name);
Marc Bouchere6869a82000-03-20 06:03:29 +00001661
Harald Welteaae69be2004-08-29 23:32:14 +00001662 c = iptcc_find_label(t->u.user.name, handle);
1663 if (c) {
1664 DEBUGP_C("found!\n");
1665 r->type = IPTCC_R_JUMP;
1666 r->jump = c;
1667 c->references++;
1668 return 1;
1669 }
1670 DEBUGP_C("not found :(\n");
Marc Bouchere6869a82000-03-20 06:03:29 +00001671 }
1672
1673 /* Must be a module? If not, kernel will reject... */
Rusty Russell3aef54d2005-01-03 03:48:40 +00001674 /* memset to all 0 for your memcmp convenience: don't clear version */
Rusty Russell228e98d2000-04-27 10:28:06 +00001675 memset(t->u.user.name + strlen(t->u.user.name),
Marc Bouchere6869a82000-03-20 06:03:29 +00001676 0,
Rusty Russell3aef54d2005-01-03 03:48:40 +00001677 FUNCTION_MAXNAMELEN - 1 - strlen(t->u.user.name));
Rusty Russell733e54b2004-12-16 14:22:23 +00001678 r->type = IPTCC_R_MODULE;
Harald Welteaae69be2004-08-29 23:32:14 +00001679 set_changed(handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001680 return 1;
1681}
1682
Harald Welte0113fe72004-01-06 19:04:02 +00001683/* Insert the entry `fw' in chain `chain' into position `rulenum'. */
Marc Bouchere6869a82000-03-20 06:03:29 +00001684int
Rusty Russell79dee072000-05-02 16:45:16 +00001685TC_INSERT_ENTRY(const IPT_CHAINLABEL chain,
1686 const STRUCT_ENTRY *e,
1687 unsigned int rulenum,
1688 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001689{
Harald Welteaae69be2004-08-29 23:32:14 +00001690 struct chain_head *c;
Martin Josefssoneb066cc2004-09-22 21:04:07 +00001691 struct rule_head *r;
1692 struct list_head *prev;
Marc Bouchere6869a82000-03-20 06:03:29 +00001693
Rusty Russell79dee072000-05-02 16:45:16 +00001694 iptc_fn = TC_INSERT_ENTRY;
Harald Welteaae69be2004-08-29 23:32:14 +00001695
1696 if (!(c = iptcc_find_label(chain, *handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001697 errno = ENOENT;
1698 return 0;
1699 }
1700
Martin Josefssoneb066cc2004-09-22 21:04:07 +00001701 /* first rulenum index = 0
1702 first c->num_rules index = 1 */
1703 if (rulenum > c->num_rules) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001704 errno = E2BIG;
1705 return 0;
1706 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001707
Martin Josefsson631f3612004-09-23 19:25:06 +00001708 /* If we are inserting at the end just take advantage of the
1709 double linked list, insert will happen before the entry
1710 prev points to. */
1711 if (rulenum == c->num_rules) {
Martin Josefssoneb066cc2004-09-22 21:04:07 +00001712 prev = &c->rules;
Martin Josefssona5616dc2004-10-24 22:27:31 +00001713 } else if (rulenum + 1 <= c->num_rules/2) {
Martin Josefsson631f3612004-09-23 19:25:06 +00001714 r = iptcc_get_rule_num(c, rulenum + 1);
Martin Josefssona5616dc2004-10-24 22:27:31 +00001715 prev = &r->list;
1716 } else {
1717 r = iptcc_get_rule_num_reverse(c, c->num_rules - rulenum);
Martin Josefsson631f3612004-09-23 19:25:06 +00001718 prev = &r->list;
1719 }
Martin Josefssoneb066cc2004-09-22 21:04:07 +00001720
Harald Welteaae69be2004-08-29 23:32:14 +00001721 if (!(r = iptcc_alloc_rule(c, e->next_offset))) {
1722 errno = ENOMEM;
Harald Welte0113fe72004-01-06 19:04:02 +00001723 return 0;
Harald Welteaae69be2004-08-29 23:32:14 +00001724 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001725
Harald Welteaae69be2004-08-29 23:32:14 +00001726 memcpy(r->entry, e, e->next_offset);
1727 r->counter_map.maptype = COUNTER_MAP_SET;
1728
1729 if (!iptcc_map_target(*handle, r)) {
1730 free(r);
1731 return 0;
1732 }
1733
Martin Josefssoneb066cc2004-09-22 21:04:07 +00001734 list_add_tail(&r->list, prev);
Harald Welteaae69be2004-08-29 23:32:14 +00001735 c->num_rules++;
1736
1737 set_changed(*handle);
1738
1739 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00001740}
1741
1742/* Atomically replace rule `rulenum' in `chain' with `fw'. */
1743int
Rusty Russell79dee072000-05-02 16:45:16 +00001744TC_REPLACE_ENTRY(const IPT_CHAINLABEL chain,
1745 const STRUCT_ENTRY *e,
1746 unsigned int rulenum,
1747 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001748{
Harald Welteaae69be2004-08-29 23:32:14 +00001749 struct chain_head *c;
1750 struct rule_head *r, *old;
Marc Bouchere6869a82000-03-20 06:03:29 +00001751
Rusty Russell79dee072000-05-02 16:45:16 +00001752 iptc_fn = TC_REPLACE_ENTRY;
Marc Bouchere6869a82000-03-20 06:03:29 +00001753
Harald Welteaae69be2004-08-29 23:32:14 +00001754 if (!(c = iptcc_find_label(chain, *handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001755 errno = ENOENT;
1756 return 0;
1757 }
1758
Martin Josefsson0f9b8b12004-12-18 17:18:49 +00001759 if (rulenum >= c->num_rules) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001760 errno = E2BIG;
1761 return 0;
1762 }
1763
Martin Josefsson0f9b8b12004-12-18 17:18:49 +00001764 /* Take advantage of the double linked list if possible. */
1765 if (rulenum + 1 <= c->num_rules/2) {
1766 old = iptcc_get_rule_num(c, rulenum + 1);
1767 } else {
1768 old = iptcc_get_rule_num_reverse(c, c->num_rules - rulenum);
1769 }
1770
Harald Welteaae69be2004-08-29 23:32:14 +00001771 if (!(r = iptcc_alloc_rule(c, e->next_offset))) {
1772 errno = ENOMEM;
Marc Bouchere6869a82000-03-20 06:03:29 +00001773 return 0;
Harald Welteaae69be2004-08-29 23:32:14 +00001774 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001775
Harald Welteaae69be2004-08-29 23:32:14 +00001776 memcpy(r->entry, e, e->next_offset);
1777 r->counter_map.maptype = COUNTER_MAP_SET;
1778
1779 if (!iptcc_map_target(*handle, r)) {
1780 free(r);
Harald Welte0113fe72004-01-06 19:04:02 +00001781 return 0;
Harald Welteaae69be2004-08-29 23:32:14 +00001782 }
Harald Welte0113fe72004-01-06 19:04:02 +00001783
Harald Welteaae69be2004-08-29 23:32:14 +00001784 list_add(&r->list, &old->list);
1785 iptcc_delete_rule(old);
1786
1787 set_changed(*handle);
1788
1789 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00001790}
1791
Harald Welte0113fe72004-01-06 19:04:02 +00001792/* Append entry `fw' to chain `chain'. Equivalent to insert with
Marc Bouchere6869a82000-03-20 06:03:29 +00001793 rulenum = length of chain. */
1794int
Rusty Russell79dee072000-05-02 16:45:16 +00001795TC_APPEND_ENTRY(const IPT_CHAINLABEL chain,
1796 const STRUCT_ENTRY *e,
1797 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001798{
Harald Welteaae69be2004-08-29 23:32:14 +00001799 struct chain_head *c;
1800 struct rule_head *r;
Marc Bouchere6869a82000-03-20 06:03:29 +00001801
Rusty Russell79dee072000-05-02 16:45:16 +00001802 iptc_fn = TC_APPEND_ENTRY;
Harald Welteaae69be2004-08-29 23:32:14 +00001803 if (!(c = iptcc_find_label(chain, *handle))) {
1804 DEBUGP("unable to find chain `%s'\n", chain);
Marc Bouchere6869a82000-03-20 06:03:29 +00001805 errno = ENOENT;
1806 return 0;
1807 }
1808
Harald Welteaae69be2004-08-29 23:32:14 +00001809 if (!(r = iptcc_alloc_rule(c, e->next_offset))) {
1810 DEBUGP("unable to allocate rule for chain `%s'\n", chain);
1811 errno = ENOMEM;
Harald Welte0113fe72004-01-06 19:04:02 +00001812 return 0;
Harald Welteaae69be2004-08-29 23:32:14 +00001813 }
Harald Welte0113fe72004-01-06 19:04:02 +00001814
Harald Welteaae69be2004-08-29 23:32:14 +00001815 memcpy(r->entry, e, e->next_offset);
1816 r->counter_map.maptype = COUNTER_MAP_SET;
1817
1818 if (!iptcc_map_target(*handle, r)) {
Martin Josefsson12009532004-09-23 18:24:29 +00001819 DEBUGP("unable to map target of rule for chain `%s'\n", chain);
Harald Welteaae69be2004-08-29 23:32:14 +00001820 free(r);
1821 return 0;
1822 }
1823
1824 list_add_tail(&r->list, &c->rules);
1825 c->num_rules++;
1826
1827 set_changed(*handle);
1828
1829 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00001830}
1831
1832static inline int
Rusty Russell79dee072000-05-02 16:45:16 +00001833match_different(const STRUCT_ENTRY_MATCH *a,
Rusty Russelledf14cf2000-04-19 11:26:44 +00001834 const unsigned char *a_elems,
1835 const unsigned char *b_elems,
1836 unsigned char **maskptr)
Marc Bouchere6869a82000-03-20 06:03:29 +00001837{
Rusty Russell79dee072000-05-02 16:45:16 +00001838 const STRUCT_ENTRY_MATCH *b;
Rusty Russelledf14cf2000-04-19 11:26:44 +00001839 unsigned int i;
Marc Bouchere6869a82000-03-20 06:03:29 +00001840
1841 /* Offset of b is the same as a. */
Rusty Russell30fd6e52000-04-23 09:16:06 +00001842 b = (void *)b_elems + ((unsigned char *)a - a_elems);
Marc Bouchere6869a82000-03-20 06:03:29 +00001843
Rusty Russell228e98d2000-04-27 10:28:06 +00001844 if (a->u.match_size != b->u.match_size)
Marc Bouchere6869a82000-03-20 06:03:29 +00001845 return 1;
1846
Rusty Russell228e98d2000-04-27 10:28:06 +00001847 if (strcmp(a->u.user.name, b->u.user.name) != 0)
Marc Bouchere6869a82000-03-20 06:03:29 +00001848 return 1;
1849
Rusty Russell73ef09b2000-07-03 10:24:04 +00001850 *maskptr += ALIGN(sizeof(*a));
Rusty Russelledf14cf2000-04-19 11:26:44 +00001851
Rusty Russell73ef09b2000-07-03 10:24:04 +00001852 for (i = 0; i < a->u.match_size - ALIGN(sizeof(*a)); i++)
Rusty Russelledf14cf2000-04-19 11:26:44 +00001853 if (((a->data[i] ^ b->data[i]) & (*maskptr)[i]) != 0)
Rusty Russell90e712a2000-03-29 04:19:26 +00001854 return 1;
Rusty Russelledf14cf2000-04-19 11:26:44 +00001855 *maskptr += i;
1856 return 0;
1857}
1858
1859static inline int
Rusty Russell733e54b2004-12-16 14:22:23 +00001860target_same(struct rule_head *a, struct rule_head *b,const unsigned char *mask)
Rusty Russelledf14cf2000-04-19 11:26:44 +00001861{
1862 unsigned int i;
Rusty Russell733e54b2004-12-16 14:22:23 +00001863 STRUCT_ENTRY_TARGET *ta, *tb;
Marc Bouchere6869a82000-03-20 06:03:29 +00001864
Rusty Russell733e54b2004-12-16 14:22:23 +00001865 if (a->type != b->type)
1866 return 0;
1867
1868 ta = GET_TARGET(a->entry);
1869 tb = GET_TARGET(b->entry);
1870
1871 switch (a->type) {
1872 case IPTCC_R_FALLTHROUGH:
1873 return 1;
1874 case IPTCC_R_JUMP:
1875 return a->jump == b->jump;
1876 case IPTCC_R_STANDARD:
1877 return ((STRUCT_STANDARD_TARGET *)ta)->verdict
1878 == ((STRUCT_STANDARD_TARGET *)tb)->verdict;
1879 case IPTCC_R_MODULE:
1880 if (ta->u.target_size != tb->u.target_size)
1881 return 0;
1882 if (strcmp(ta->u.user.name, tb->u.user.name) != 0)
1883 return 0;
1884
1885 for (i = 0; i < ta->u.target_size - sizeof(*ta); i++)
Rusty Russelldaade442004-12-29 11:14:52 +00001886 if (((ta->data[i] ^ tb->data[i]) & mask[i]) != 0)
Rusty Russell733e54b2004-12-16 14:22:23 +00001887 return 0;
1888 return 1;
1889 default:
1890 fprintf(stderr, "ERROR: bad type %i\n", a->type);
1891 abort();
1892 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001893}
1894
Rusty Russell733e54b2004-12-16 14:22:23 +00001895static unsigned char *
Rusty Russell79dee072000-05-02 16:45:16 +00001896is_same(const STRUCT_ENTRY *a,
1897 const STRUCT_ENTRY *b,
1898 unsigned char *matchmask);
Marc Bouchere6869a82000-03-20 06:03:29 +00001899
Harald Welte0113fe72004-01-06 19:04:02 +00001900/* Delete the first rule in `chain' which matches `fw'. */
Marc Bouchere6869a82000-03-20 06:03:29 +00001901int
Rusty Russell79dee072000-05-02 16:45:16 +00001902TC_DELETE_ENTRY(const IPT_CHAINLABEL chain,
1903 const STRUCT_ENTRY *origfw,
1904 unsigned char *matchmask,
1905 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001906{
Harald Welteaae69be2004-08-29 23:32:14 +00001907 struct chain_head *c;
Rusty Russelle45c7132004-12-16 13:21:44 +00001908 struct rule_head *r, *i;
Marc Bouchere6869a82000-03-20 06:03:29 +00001909
Rusty Russell79dee072000-05-02 16:45:16 +00001910 iptc_fn = TC_DELETE_ENTRY;
Harald Welteaae69be2004-08-29 23:32:14 +00001911 if (!(c = iptcc_find_label(chain, *handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001912 errno = ENOENT;
1913 return 0;
1914 }
1915
Rusty Russelle45c7132004-12-16 13:21:44 +00001916 /* Create a rule_head from origfw. */
1917 r = iptcc_alloc_rule(c, origfw->next_offset);
1918 if (!r) {
Harald Welte0113fe72004-01-06 19:04:02 +00001919 errno = ENOMEM;
1920 return 0;
1921 }
1922
Rusty Russelle45c7132004-12-16 13:21:44 +00001923 memcpy(r->entry, origfw, origfw->next_offset);
1924 r->counter_map.maptype = COUNTER_MAP_NOMAP;
1925 if (!iptcc_map_target(*handle, r)) {
1926 DEBUGP("unable to map target of rule for chain `%s'\n", chain);
1927 free(r);
1928 return 0;
Patrick McHardyJesper Brouer04a1e4c2006-07-25 01:50:48 +00001929 } else {
1930 /* iptcc_map_target increment target chain references
1931 * since this is a fake rule only used for matching
1932 * the chain references count is decremented again.
1933 */
1934 if (r->type == IPTCC_R_JUMP
1935 && r->jump)
1936 r->jump->references--;
Rusty Russelle45c7132004-12-16 13:21:44 +00001937 }
Harald Welte0113fe72004-01-06 19:04:02 +00001938
Rusty Russelle45c7132004-12-16 13:21:44 +00001939 list_for_each_entry(i, &c->rules, list) {
Rusty Russell733e54b2004-12-16 14:22:23 +00001940 unsigned char *mask;
Harald Weltefe537072004-08-30 20:28:53 +00001941
Rusty Russell733e54b2004-12-16 14:22:23 +00001942 mask = is_same(r->entry, i->entry, matchmask);
1943 if (!mask)
1944 continue;
Martin Josefsson2a5dbbb2004-09-22 21:37:41 +00001945
Rusty Russell733e54b2004-12-16 14:22:23 +00001946 if (!target_same(r, i, mask))
1947 continue;
1948
1949 /* If we are about to delete the rule that is the
1950 * current iterator, move rule iterator back. next
1951 * pointer will then point to real next node */
1952 if (i == (*handle)->rule_iterator_cur) {
1953 (*handle)->rule_iterator_cur =
1954 list_entry((*handle)->rule_iterator_cur->list.prev,
1955 struct rule_head, list);
Marc Bouchere6869a82000-03-20 06:03:29 +00001956 }
Rusty Russell733e54b2004-12-16 14:22:23 +00001957
1958 c->num_rules--;
1959 iptcc_delete_rule(i);
1960
1961 set_changed(*handle);
1962 free(r);
1963 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00001964 }
1965
Rusty Russelle45c7132004-12-16 13:21:44 +00001966 free(r);
Marc Bouchere6869a82000-03-20 06:03:29 +00001967 errno = ENOENT;
1968 return 0;
Rusty Russell7e53bf92000-03-20 07:03:28 +00001969}
Harald Welteaae69be2004-08-29 23:32:14 +00001970
Marc Bouchere6869a82000-03-20 06:03:29 +00001971
1972/* Delete the rule in position `rulenum' in `chain'. */
1973int
Rusty Russell79dee072000-05-02 16:45:16 +00001974TC_DELETE_NUM_ENTRY(const IPT_CHAINLABEL chain,
1975 unsigned int rulenum,
1976 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001977{
Harald Welteaae69be2004-08-29 23:32:14 +00001978 struct chain_head *c;
1979 struct rule_head *r;
Marc Bouchere6869a82000-03-20 06:03:29 +00001980
Rusty Russell79dee072000-05-02 16:45:16 +00001981 iptc_fn = TC_DELETE_NUM_ENTRY;
Harald Welteaae69be2004-08-29 23:32:14 +00001982
1983 if (!(c = iptcc_find_label(chain, *handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001984 errno = ENOENT;
1985 return 0;
1986 }
1987
Martin Josefssona5616dc2004-10-24 22:27:31 +00001988 if (rulenum >= c->num_rules) {
Martin Josefsson631f3612004-09-23 19:25:06 +00001989 errno = E2BIG;
1990 return 0;
1991 }
1992
1993 /* Take advantage of the double linked list if possible. */
Martin Josefssona5616dc2004-10-24 22:27:31 +00001994 if (rulenum + 1 <= c->num_rules/2) {
1995 r = iptcc_get_rule_num(c, rulenum + 1);
1996 } else {
1997 r = iptcc_get_rule_num_reverse(c, c->num_rules - rulenum);
Marc Bouchere6869a82000-03-20 06:03:29 +00001998 }
1999
Harald Welteaae69be2004-08-29 23:32:14 +00002000 /* If we are about to delete the rule that is the current
2001 * iterator, move rule iterator back. next pointer will then
2002 * point to real next node */
2003 if (r == (*handle)->rule_iterator_cur) {
2004 (*handle)->rule_iterator_cur =
2005 list_entry((*handle)->rule_iterator_cur->list.prev,
2006 struct rule_head, list);
Harald Welte0113fe72004-01-06 19:04:02 +00002007 }
Marc Bouchere6869a82000-03-20 06:03:29 +00002008
Harald Welteaae69be2004-08-29 23:32:14 +00002009 c->num_rules--;
2010 iptcc_delete_rule(r);
2011
Martin Josefsson2a5dbbb2004-09-22 21:37:41 +00002012 set_changed(*handle);
2013
Harald Welteaae69be2004-08-29 23:32:14 +00002014 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00002015}
2016
2017/* Check the packet `fw' on chain `chain'. Returns the verdict, or
2018 NULL and sets errno. */
2019const char *
Rusty Russell79dee072000-05-02 16:45:16 +00002020TC_CHECK_PACKET(const IPT_CHAINLABEL chain,
2021 STRUCT_ENTRY *entry,
2022 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00002023{
Derrik Pates664c0a32005-02-01 13:28:14 +00002024 iptc_fn = TC_CHECK_PACKET;
Marc Bouchere6869a82000-03-20 06:03:29 +00002025 errno = ENOSYS;
2026 return NULL;
2027}
2028
2029/* Flushes the entries in the given chain (ie. empties chain). */
2030int
Rusty Russell79dee072000-05-02 16:45:16 +00002031TC_FLUSH_ENTRIES(const IPT_CHAINLABEL chain, TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00002032{
Harald Welteaae69be2004-08-29 23:32:14 +00002033 struct chain_head *c;
2034 struct rule_head *r, *tmp;
Marc Bouchere6869a82000-03-20 06:03:29 +00002035
Harald Welte0113fe72004-01-06 19:04:02 +00002036 iptc_fn = TC_FLUSH_ENTRIES;
Harald Welteaae69be2004-08-29 23:32:14 +00002037 if (!(c = iptcc_find_label(chain, *handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002038 errno = ENOENT;
2039 return 0;
2040 }
Marc Bouchere6869a82000-03-20 06:03:29 +00002041
Harald Welteaae69be2004-08-29 23:32:14 +00002042 list_for_each_entry_safe(r, tmp, &c->rules, list) {
2043 iptcc_delete_rule(r);
2044 }
2045
2046 c->num_rules = 0;
2047
2048 set_changed(*handle);
2049
2050 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00002051}
2052
2053/* Zeroes the counters in a chain. */
2054int
Rusty Russell79dee072000-05-02 16:45:16 +00002055TC_ZERO_ENTRIES(const IPT_CHAINLABEL chain, TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00002056{
Harald Welteaae69be2004-08-29 23:32:14 +00002057 struct chain_head *c;
2058 struct rule_head *r;
Rusty Russell7e53bf92000-03-20 07:03:28 +00002059
Derrik Pates664c0a32005-02-01 13:28:14 +00002060 iptc_fn = TC_ZERO_ENTRIES;
Harald Welteaae69be2004-08-29 23:32:14 +00002061 if (!(c = iptcc_find_label(chain, *handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002062 errno = ENOENT;
2063 return 0;
2064 }
Marc Bouchere6869a82000-03-20 06:03:29 +00002065
Andy Gaye5bd1d72006-08-22 02:56:41 +00002066 if (c->counter_map.maptype == COUNTER_MAP_NORMAL_MAP)
2067 c->counter_map.maptype = COUNTER_MAP_ZEROED;
2068
Harald Welteaae69be2004-08-29 23:32:14 +00002069 list_for_each_entry(r, &c->rules, list) {
2070 if (r->counter_map.maptype == COUNTER_MAP_NORMAL_MAP)
2071 r->counter_map.maptype = COUNTER_MAP_ZEROED;
Marc Bouchere6869a82000-03-20 06:03:29 +00002072 }
Harald Welteaae69be2004-08-29 23:32:14 +00002073
Rusty Russell175f6412000-03-24 09:32:20 +00002074 set_changed(*handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00002075
Marc Bouchere6869a82000-03-20 06:03:29 +00002076 return 1;
2077}
2078
Harald Welte1cef74d2001-01-05 15:22:59 +00002079STRUCT_COUNTERS *
2080TC_READ_COUNTER(const IPT_CHAINLABEL chain,
2081 unsigned int rulenum,
2082 TC_HANDLE_T *handle)
2083{
Harald Welteaae69be2004-08-29 23:32:14 +00002084 struct chain_head *c;
2085 struct rule_head *r;
Harald Welte1cef74d2001-01-05 15:22:59 +00002086
2087 iptc_fn = TC_READ_COUNTER;
2088 CHECK(*handle);
2089
Harald Welteaae69be2004-08-29 23:32:14 +00002090 if (!(c = iptcc_find_label(chain, *handle))) {
Harald Welte1cef74d2001-01-05 15:22:59 +00002091 errno = ENOENT;
2092 return NULL;
2093 }
2094
Harald Welteaae69be2004-08-29 23:32:14 +00002095 if (!(r = iptcc_get_rule_num(c, rulenum))) {
Harald Welte0113fe72004-01-06 19:04:02 +00002096 errno = E2BIG;
2097 return NULL;
2098 }
2099
Harald Welteaae69be2004-08-29 23:32:14 +00002100 return &r->entry[0].counters;
Harald Welte1cef74d2001-01-05 15:22:59 +00002101}
2102
2103int
2104TC_ZERO_COUNTER(const IPT_CHAINLABEL chain,
2105 unsigned int rulenum,
2106 TC_HANDLE_T *handle)
2107{
Harald Welteaae69be2004-08-29 23:32:14 +00002108 struct chain_head *c;
2109 struct rule_head *r;
Harald Welte1cef74d2001-01-05 15:22:59 +00002110
2111 iptc_fn = TC_ZERO_COUNTER;
2112 CHECK(*handle);
2113
Harald Welteaae69be2004-08-29 23:32:14 +00002114 if (!(c = iptcc_find_label(chain, *handle))) {
Harald Welte1cef74d2001-01-05 15:22:59 +00002115 errno = ENOENT;
2116 return 0;
2117 }
2118
Harald Welteaae69be2004-08-29 23:32:14 +00002119 if (!(r = iptcc_get_rule_num(c, rulenum))) {
Harald Welte0113fe72004-01-06 19:04:02 +00002120 errno = E2BIG;
2121 return 0;
2122 }
2123
Harald Welteaae69be2004-08-29 23:32:14 +00002124 if (r->counter_map.maptype == COUNTER_MAP_NORMAL_MAP)
2125 r->counter_map.maptype = COUNTER_MAP_ZEROED;
Harald Welte1cef74d2001-01-05 15:22:59 +00002126
2127 set_changed(*handle);
2128
2129 return 1;
2130}
2131
2132int
2133TC_SET_COUNTER(const IPT_CHAINLABEL chain,
2134 unsigned int rulenum,
2135 STRUCT_COUNTERS *counters,
2136 TC_HANDLE_T *handle)
2137{
Harald Welteaae69be2004-08-29 23:32:14 +00002138 struct chain_head *c;
2139 struct rule_head *r;
Harald Welte1cef74d2001-01-05 15:22:59 +00002140 STRUCT_ENTRY *e;
Harald Welte1cef74d2001-01-05 15:22:59 +00002141
2142 iptc_fn = TC_SET_COUNTER;
2143 CHECK(*handle);
2144
Harald Welteaae69be2004-08-29 23:32:14 +00002145 if (!(c = iptcc_find_label(chain, *handle))) {
Harald Welte1cef74d2001-01-05 15:22:59 +00002146 errno = ENOENT;
2147 return 0;
2148 }
Harald Welte0113fe72004-01-06 19:04:02 +00002149
Harald Welteaae69be2004-08-29 23:32:14 +00002150 if (!(r = iptcc_get_rule_num(c, rulenum))) {
Harald Welte0113fe72004-01-06 19:04:02 +00002151 errno = E2BIG;
2152 return 0;
2153 }
2154
Harald Welteaae69be2004-08-29 23:32:14 +00002155 e = r->entry;
2156 r->counter_map.maptype = COUNTER_MAP_SET;
Harald Welte0113fe72004-01-06 19:04:02 +00002157
2158 memcpy(&e->counters, counters, sizeof(STRUCT_COUNTERS));
Harald Welte1cef74d2001-01-05 15:22:59 +00002159
2160 set_changed(*handle);
2161
2162 return 1;
2163}
2164
Marc Bouchere6869a82000-03-20 06:03:29 +00002165/* Creates a new chain. */
2166/* To create a chain, create two rules: error node and unconditional
2167 * return. */
2168int
Rusty Russell79dee072000-05-02 16:45:16 +00002169TC_CREATE_CHAIN(const IPT_CHAINLABEL chain, TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00002170{
Harald Welteaae69be2004-08-29 23:32:14 +00002171 static struct chain_head *c;
Marc Bouchere6869a82000-03-20 06:03:29 +00002172
Rusty Russell79dee072000-05-02 16:45:16 +00002173 iptc_fn = TC_CREATE_CHAIN;
Marc Bouchere6869a82000-03-20 06:03:29 +00002174
2175 /* find_label doesn't cover built-in targets: DROP, ACCEPT,
2176 QUEUE, RETURN. */
Harald Welteaae69be2004-08-29 23:32:14 +00002177 if (iptcc_find_label(chain, *handle)
Rusty Russell79dee072000-05-02 16:45:16 +00002178 || strcmp(chain, LABEL_DROP) == 0
2179 || strcmp(chain, LABEL_ACCEPT) == 0
Rusty Russell67088e72000-05-10 01:18:57 +00002180 || strcmp(chain, LABEL_QUEUE) == 0
Rusty Russell79dee072000-05-02 16:45:16 +00002181 || strcmp(chain, LABEL_RETURN) == 0) {
Harald Welteaae69be2004-08-29 23:32:14 +00002182 DEBUGP("Chain `%s' already exists\n", chain);
Marc Bouchere6869a82000-03-20 06:03:29 +00002183 errno = EEXIST;
2184 return 0;
2185 }
2186
Rusty Russell79dee072000-05-02 16:45:16 +00002187 if (strlen(chain)+1 > sizeof(IPT_CHAINLABEL)) {
Harald Welteaae69be2004-08-29 23:32:14 +00002188 DEBUGP("Chain name `%s' too long\n", chain);
Marc Bouchere6869a82000-03-20 06:03:29 +00002189 errno = EINVAL;
2190 return 0;
2191 }
2192
Harald Welteaae69be2004-08-29 23:32:14 +00002193 c = iptcc_alloc_chain_head(chain, 0);
2194 if (!c) {
2195 DEBUGP("Cannot allocate memory for chain `%s'\n", chain);
2196 errno = ENOMEM;
2197 return 0;
Marc Bouchere6869a82000-03-20 06:03:29 +00002198
Harald Welteaae69be2004-08-29 23:32:14 +00002199 }
Jesper Dangaard Brouer48bde402008-01-15 17:06:48 +00002200 (*handle)->num_chains++; /* New user defined chain */
Marc Bouchere6869a82000-03-20 06:03:29 +00002201
Harald Welteaae69be2004-08-29 23:32:14 +00002202 DEBUGP("Creating chain `%s'\n", chain);
Jesper Dangaard Brouerd8cb7872007-11-28 08:40:26 +00002203 iptc_insert_chain(*handle, c); /* Insert sorted */
Harald Welte0113fe72004-01-06 19:04:02 +00002204
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +00002205 /* Inserting chains don't change the correctness of the chain
2206 * index (except if its smaller than index[0], but that
2207 * handled by iptc_insert_chain). It only causes longer lists
2208 * in the buckets. Thus, only rebuild chain index when the
2209 * capacity is exceed with CHAIN_INDEX_INSERT_MAX chains.
2210 */
2211 int capacity = (*handle)->chain_index_sz * CHAIN_INDEX_BUCKET_LEN;
2212 int exceeded = ((((*handle)->num_chains)-capacity));
2213 if (exceeded > CHAIN_INDEX_INSERT_MAX) {
2214 debug("Capacity(%d) exceeded(%d) rebuild (chains:%d)\n",
2215 capacity, exceeded, (*handle)->num_chains);
2216 iptcc_chain_index_rebuild(*handle);
2217 }
2218
Harald Welte0113fe72004-01-06 19:04:02 +00002219 set_changed(*handle);
2220
Harald Welteaae69be2004-08-29 23:32:14 +00002221 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00002222}
2223
2224/* Get the number of references to this chain. */
2225int
Rusty Russell79dee072000-05-02 16:45:16 +00002226TC_GET_REFERENCES(unsigned int *ref, const IPT_CHAINLABEL chain,
2227 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00002228{
Harald Welteaae69be2004-08-29 23:32:14 +00002229 struct chain_head *c;
Marc Bouchere6869a82000-03-20 06:03:29 +00002230
Derrik Pates664c0a32005-02-01 13:28:14 +00002231 iptc_fn = TC_GET_REFERENCES;
Harald Welteaae69be2004-08-29 23:32:14 +00002232 if (!(c = iptcc_find_label(chain, *handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002233 errno = ENOENT;
2234 return 0;
2235 }
2236
Harald Welteaae69be2004-08-29 23:32:14 +00002237 *ref = c->references;
2238
Marc Bouchere6869a82000-03-20 06:03:29 +00002239 return 1;
2240}
2241
2242/* Deletes a chain. */
2243int
Rusty Russell79dee072000-05-02 16:45:16 +00002244TC_DELETE_CHAIN(const IPT_CHAINLABEL chain, TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00002245{
Marc Bouchere6869a82000-03-20 06:03:29 +00002246 unsigned int references;
Harald Welteaae69be2004-08-29 23:32:14 +00002247 struct chain_head *c;
Rusty Russell7e53bf92000-03-20 07:03:28 +00002248
Rusty Russell79dee072000-05-02 16:45:16 +00002249 iptc_fn = TC_DELETE_CHAIN;
Marc Bouchere6869a82000-03-20 06:03:29 +00002250
Harald Welteaae69be2004-08-29 23:32:14 +00002251 if (!(c = iptcc_find_label(chain, *handle))) {
2252 DEBUGP("cannot find chain `%s'\n", chain);
Marc Bouchere6869a82000-03-20 06:03:29 +00002253 errno = ENOENT;
2254 return 0;
2255 }
2256
Harald Welteaae69be2004-08-29 23:32:14 +00002257 if (TC_BUILTIN(chain, *handle)) {
2258 DEBUGP("cannot remove builtin chain `%s'\n", chain);
2259 errno = EINVAL;
2260 return 0;
2261 }
2262
2263 if (!TC_GET_REFERENCES(&references, chain, handle)) {
2264 DEBUGP("cannot get references on chain `%s'\n", chain);
2265 return 0;
2266 }
2267
2268 if (references > 0) {
2269 DEBUGP("chain `%s' still has references\n", chain);
2270 errno = EMLINK;
2271 return 0;
2272 }
2273
2274 if (c->num_rules) {
2275 DEBUGP("chain `%s' is not empty\n", chain);
Marc Bouchere6869a82000-03-20 06:03:29 +00002276 errno = ENOTEMPTY;
2277 return 0;
2278 }
2279
Harald Welteaae69be2004-08-29 23:32:14 +00002280 /* If we are about to delete the chain that is the current
Jesper Dangaard Brouer48bde402008-01-15 17:06:48 +00002281 * iterator, move chain iterator forward. */
Harald Welteaae69be2004-08-29 23:32:14 +00002282 if (c == (*handle)->chain_iterator_cur)
2283 iptcc_chain_iterator_advance(*handle);
Harald Welte0113fe72004-01-06 19:04:02 +00002284
Jesper Dangaard Brouer48bde402008-01-15 17:06:48 +00002285 (*handle)->num_chains--; /* One user defined chain deleted */
2286
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +00002287 //list_del(&c->list); /* Done in iptcc_chain_index_delete_chain() */
2288 iptcc_chain_index_delete_chain(c, *handle);
2289 free(c);
2290
Harald Welteaae69be2004-08-29 23:32:14 +00002291 DEBUGP("chain `%s' deleted\n", chain);
2292
2293 set_changed(*handle);
2294
2295 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00002296}
2297
2298/* Renames a chain. */
Rusty Russell79dee072000-05-02 16:45:16 +00002299int TC_RENAME_CHAIN(const IPT_CHAINLABEL oldname,
2300 const IPT_CHAINLABEL newname,
2301 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00002302{
Harald Welteaae69be2004-08-29 23:32:14 +00002303 struct chain_head *c;
Rusty Russell79dee072000-05-02 16:45:16 +00002304 iptc_fn = TC_RENAME_CHAIN;
Marc Bouchere6869a82000-03-20 06:03:29 +00002305
Harald Welte1de80462000-10-30 12:00:27 +00002306 /* find_label doesn't cover built-in targets: DROP, ACCEPT,
2307 QUEUE, RETURN. */
Harald Welteaae69be2004-08-29 23:32:14 +00002308 if (iptcc_find_label(newname, *handle)
Rusty Russell79dee072000-05-02 16:45:16 +00002309 || strcmp(newname, LABEL_DROP) == 0
2310 || strcmp(newname, LABEL_ACCEPT) == 0
Harald Welte1de80462000-10-30 12:00:27 +00002311 || strcmp(newname, LABEL_QUEUE) == 0
Rusty Russell79dee072000-05-02 16:45:16 +00002312 || strcmp(newname, LABEL_RETURN) == 0) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002313 errno = EEXIST;
2314 return 0;
2315 }
2316
Harald Welteaae69be2004-08-29 23:32:14 +00002317 if (!(c = iptcc_find_label(oldname, *handle))
Rusty Russell79dee072000-05-02 16:45:16 +00002318 || TC_BUILTIN(oldname, *handle)) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002319 errno = ENOENT;
2320 return 0;
2321 }
2322
Rusty Russell79dee072000-05-02 16:45:16 +00002323 if (strlen(newname)+1 > sizeof(IPT_CHAINLABEL)) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002324 errno = EINVAL;
2325 return 0;
2326 }
2327
Harald Welteaae69be2004-08-29 23:32:14 +00002328 strncpy(c->name, newname, sizeof(IPT_CHAINLABEL));
2329
Harald Welte0113fe72004-01-06 19:04:02 +00002330 set_changed(*handle);
2331
Marc Bouchere6869a82000-03-20 06:03:29 +00002332 return 1;
2333}
2334
2335/* Sets the policy on a built-in chain. */
2336int
Rusty Russell79dee072000-05-02 16:45:16 +00002337TC_SET_POLICY(const IPT_CHAINLABEL chain,
2338 const IPT_CHAINLABEL policy,
Harald Welte1cef74d2001-01-05 15:22:59 +00002339 STRUCT_COUNTERS *counters,
Rusty Russell79dee072000-05-02 16:45:16 +00002340 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00002341{
Harald Welteaae69be2004-08-29 23:32:14 +00002342 struct chain_head *c;
Marc Bouchere6869a82000-03-20 06:03:29 +00002343
Rusty Russell79dee072000-05-02 16:45:16 +00002344 iptc_fn = TC_SET_POLICY;
Marc Bouchere6869a82000-03-20 06:03:29 +00002345
Harald Welteaae69be2004-08-29 23:32:14 +00002346 if (!(c = iptcc_find_label(chain, *handle))) {
2347 DEBUGP("cannot find chain `%s'\n", chain);
2348 errno = ENOENT;
Marc Bouchere6869a82000-03-20 06:03:29 +00002349 return 0;
2350 }
2351
Harald Welteaae69be2004-08-29 23:32:14 +00002352 if (!iptcc_is_builtin(c)) {
2353 DEBUGP("cannot set policy of userdefinedchain `%s'\n", chain);
2354 errno = ENOENT;
2355 return 0;
2356 }
Marc Bouchere6869a82000-03-20 06:03:29 +00002357
Rusty Russell79dee072000-05-02 16:45:16 +00002358 if (strcmp(policy, LABEL_ACCEPT) == 0)
Harald Welteaae69be2004-08-29 23:32:14 +00002359 c->verdict = -NF_ACCEPT - 1;
Rusty Russell79dee072000-05-02 16:45:16 +00002360 else if (strcmp(policy, LABEL_DROP) == 0)
Harald Welteaae69be2004-08-29 23:32:14 +00002361 c->verdict = -NF_DROP - 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00002362 else {
2363 errno = EINVAL;
2364 return 0;
2365 }
Harald Welte1cef74d2001-01-05 15:22:59 +00002366
Harald Welte1cef74d2001-01-05 15:22:59 +00002367 if (counters) {
2368 /* set byte and packet counters */
Harald Welteaae69be2004-08-29 23:32:14 +00002369 memcpy(&c->counters, counters, sizeof(STRUCT_COUNTERS));
2370 c->counter_map.maptype = COUNTER_MAP_SET;
Harald Welte1cef74d2001-01-05 15:22:59 +00002371 } else {
Harald Welteaae69be2004-08-29 23:32:14 +00002372 c->counter_map.maptype = COUNTER_MAP_NOMAP;
Harald Welte1cef74d2001-01-05 15:22:59 +00002373 }
2374
Rusty Russell175f6412000-03-24 09:32:20 +00002375 set_changed(*handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00002376
Marc Bouchere6869a82000-03-20 06:03:29 +00002377 return 1;
2378}
2379
2380/* Without this, on gcc 2.7.2.3, we get:
Rusty Russell79dee072000-05-02 16:45:16 +00002381 libiptc.c: In function `TC_COMMIT':
Marc Bouchere6869a82000-03-20 06:03:29 +00002382 libiptc.c:833: fixed or forbidden register was spilled.
2383 This may be due to a compiler bug or to impossible asm
2384 statements or clauses.
2385*/
2386static void
Rusty Russell79dee072000-05-02 16:45:16 +00002387subtract_counters(STRUCT_COUNTERS *answer,
2388 const STRUCT_COUNTERS *a,
2389 const STRUCT_COUNTERS *b)
Marc Bouchere6869a82000-03-20 06:03:29 +00002390{
2391 answer->pcnt = a->pcnt - b->pcnt;
2392 answer->bcnt = a->bcnt - b->bcnt;
2393}
2394
Harald Welteaae69be2004-08-29 23:32:14 +00002395
Jan Engelhardtdbb77542008-02-11 00:33:30 +01002396static void counters_nomap(STRUCT_COUNTERS_INFO *newcounters, unsigned int idx)
Harald Welteaae69be2004-08-29 23:32:14 +00002397{
Jan Engelhardtdbb77542008-02-11 00:33:30 +01002398 newcounters->counters[idx] = ((STRUCT_COUNTERS) { 0, 0});
Harald Welteaae69be2004-08-29 23:32:14 +00002399 DEBUGP_C("NOMAP => zero\n");
2400}
2401
2402static void counters_normal_map(STRUCT_COUNTERS_INFO *newcounters,
Jan Engelhardtdbb77542008-02-11 00:33:30 +01002403 STRUCT_REPLACE *repl, unsigned int idx,
Harald Welteaae69be2004-08-29 23:32:14 +00002404 unsigned int mappos)
2405{
2406 /* Original read: X.
2407 * Atomic read on replacement: X + Y.
2408 * Currently in kernel: Z.
2409 * Want in kernel: X + Y + Z.
2410 * => Add in X + Y
2411 * => Add in replacement read.
2412 */
Jan Engelhardtdbb77542008-02-11 00:33:30 +01002413 newcounters->counters[idx] = repl->counters[mappos];
Harald Welteaae69be2004-08-29 23:32:14 +00002414 DEBUGP_C("NORMAL_MAP => mappos %u \n", mappos);
2415}
2416
2417static void counters_map_zeroed(STRUCT_COUNTERS_INFO *newcounters,
Jan Engelhardtdbb77542008-02-11 00:33:30 +01002418 STRUCT_REPLACE *repl, unsigned int idx,
2419 unsigned int mappos, STRUCT_COUNTERS *counters)
Harald Welteaae69be2004-08-29 23:32:14 +00002420{
2421 /* Original read: X.
2422 * Atomic read on replacement: X + Y.
2423 * Currently in kernel: Z.
2424 * Want in kernel: Y + Z.
2425 * => Add in Y.
2426 * => Add in (replacement read - original read).
2427 */
Jan Engelhardtdbb77542008-02-11 00:33:30 +01002428 subtract_counters(&newcounters->counters[idx],
Harald Welteaae69be2004-08-29 23:32:14 +00002429 &repl->counters[mappos],
2430 counters);
2431 DEBUGP_C("ZEROED => mappos %u\n", mappos);
2432}
2433
2434static void counters_map_set(STRUCT_COUNTERS_INFO *newcounters,
Jan Engelhardtdbb77542008-02-11 00:33:30 +01002435 unsigned int idx, STRUCT_COUNTERS *counters)
Harald Welteaae69be2004-08-29 23:32:14 +00002436{
2437 /* Want to set counter (iptables-restore) */
2438
Jan Engelhardtdbb77542008-02-11 00:33:30 +01002439 memcpy(&newcounters->counters[idx], counters,
Harald Welteaae69be2004-08-29 23:32:14 +00002440 sizeof(STRUCT_COUNTERS));
2441
2442 DEBUGP_C("SET\n");
2443}
2444
2445
Marc Bouchere6869a82000-03-20 06:03:29 +00002446int
Rusty Russell79dee072000-05-02 16:45:16 +00002447TC_COMMIT(TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00002448{
2449 /* Replace, then map back the counters. */
Rusty Russell79dee072000-05-02 16:45:16 +00002450 STRUCT_REPLACE *repl;
2451 STRUCT_COUNTERS_INFO *newcounters;
Harald Welteaae69be2004-08-29 23:32:14 +00002452 struct chain_head *c;
2453 int ret;
Martin Josefsson841e4ae2003-05-02 15:30:11 +00002454 size_t counterlen;
Harald Welteaae69be2004-08-29 23:32:14 +00002455 int new_number;
2456 unsigned int new_size;
Marc Bouchere6869a82000-03-20 06:03:29 +00002457
Derrik Pates664c0a32005-02-01 13:28:14 +00002458 iptc_fn = TC_COMMIT;
Marc Bouchere6869a82000-03-20 06:03:29 +00002459 CHECK(*handle);
Martin Josefsson841e4ae2003-05-02 15:30:11 +00002460
Marc Bouchere6869a82000-03-20 06:03:29 +00002461 /* Don't commit if nothing changed. */
2462 if (!(*handle)->changed)
2463 goto finished;
2464
Harald Welteaae69be2004-08-29 23:32:14 +00002465 new_number = iptcc_compile_table_prep(*handle, &new_size);
2466 if (new_number < 0) {
2467 errno = ENOMEM;
Harald Welted6ba6f52005-11-12 10:39:40 +00002468 goto out_zero;
Harald Welteaae69be2004-08-29 23:32:14 +00002469 }
2470
2471 repl = malloc(sizeof(*repl) + new_size);
Marc Bouchere6869a82000-03-20 06:03:29 +00002472 if (!repl) {
2473 errno = ENOMEM;
Harald Welted6ba6f52005-11-12 10:39:40 +00002474 goto out_zero;
Marc Bouchere6869a82000-03-20 06:03:29 +00002475 }
Martin Josefssonad3b4f92004-09-22 22:04:07 +00002476 memset(repl, 0, sizeof(*repl) + new_size);
Harald Welteaae69be2004-08-29 23:32:14 +00002477
Rusty Russelle45c7132004-12-16 13:21:44 +00002478#if 0
2479 TC_DUMP_ENTRIES(*handle);
2480#endif
2481
Harald Welteaae69be2004-08-29 23:32:14 +00002482 counterlen = sizeof(STRUCT_COUNTERS_INFO)
2483 + sizeof(STRUCT_COUNTERS) * new_number;
Marc Bouchere6869a82000-03-20 06:03:29 +00002484
2485 /* These are the old counters we will get from kernel */
Rusty Russell79dee072000-05-02 16:45:16 +00002486 repl->counters = malloc(sizeof(STRUCT_COUNTERS)
Marc Bouchere6869a82000-03-20 06:03:29 +00002487 * (*handle)->info.num_entries);
2488 if (!repl->counters) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002489 errno = ENOMEM;
Harald Welted6ba6f52005-11-12 10:39:40 +00002490 goto out_free_repl;
Marc Bouchere6869a82000-03-20 06:03:29 +00002491 }
Marc Bouchere6869a82000-03-20 06:03:29 +00002492 /* These are the counters we're going to put back, later. */
2493 newcounters = malloc(counterlen);
2494 if (!newcounters) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002495 errno = ENOMEM;
Harald Welted6ba6f52005-11-12 10:39:40 +00002496 goto out_free_repl_counters;
Marc Bouchere6869a82000-03-20 06:03:29 +00002497 }
Harald Welteaae69be2004-08-29 23:32:14 +00002498 memset(newcounters, 0, counterlen);
Marc Bouchere6869a82000-03-20 06:03:29 +00002499
2500 strcpy(repl->name, (*handle)->info.name);
Harald Welteaae69be2004-08-29 23:32:14 +00002501 repl->num_entries = new_number;
2502 repl->size = new_size;
2503
Marc Bouchere6869a82000-03-20 06:03:29 +00002504 repl->num_counters = (*handle)->info.num_entries;
2505 repl->valid_hooks = (*handle)->info.valid_hooks;
Harald Welteaae69be2004-08-29 23:32:14 +00002506
2507 DEBUGP("num_entries=%u, size=%u, num_counters=%u\n",
2508 repl->num_entries, repl->size, repl->num_counters);
2509
2510 ret = iptcc_compile_table(*handle, repl);
2511 if (ret < 0) {
2512 errno = ret;
Harald Welted6ba6f52005-11-12 10:39:40 +00002513 goto out_free_newcounters;
Harald Welteaae69be2004-08-29 23:32:14 +00002514 }
2515
2516
2517#ifdef IPTC_DEBUG2
2518 {
2519 int fd = open("/tmp/libiptc-so_set_replace.blob",
2520 O_CREAT|O_WRONLY);
2521 if (fd >= 0) {
2522 write(fd, repl, sizeof(*repl) + repl->size);
2523 close(fd);
2524 }
2525 }
2526#endif
Marc Bouchere6869a82000-03-20 06:03:29 +00002527
Harald Welted6ba6f52005-11-12 10:39:40 +00002528 ret = setsockopt(sockfd, TC_IPPROTO, SO_SET_REPLACE, repl,
2529 sizeof(*repl) + repl->size);
Patrick McHardye0865ad2006-04-22 02:08:56 +00002530 if (ret < 0)
Harald Welted6ba6f52005-11-12 10:39:40 +00002531 goto out_free_newcounters;
Marc Bouchere6869a82000-03-20 06:03:29 +00002532
2533 /* Put counters back. */
2534 strcpy(newcounters->name, (*handle)->info.name);
Harald Welteaae69be2004-08-29 23:32:14 +00002535 newcounters->num_counters = new_number;
Marc Bouchere6869a82000-03-20 06:03:29 +00002536
Harald Welteaae69be2004-08-29 23:32:14 +00002537 list_for_each_entry(c, &(*handle)->chains, list) {
2538 struct rule_head *r;
Marc Bouchere6869a82000-03-20 06:03:29 +00002539
Harald Welteaae69be2004-08-29 23:32:14 +00002540 /* Builtin chains have their own counters */
2541 if (iptcc_is_builtin(c)) {
2542 DEBUGP("counter for chain-index %u: ", c->foot_index);
2543 switch(c->counter_map.maptype) {
2544 case COUNTER_MAP_NOMAP:
2545 counters_nomap(newcounters, c->foot_index);
2546 break;
2547 case COUNTER_MAP_NORMAL_MAP:
2548 counters_normal_map(newcounters, repl,
2549 c->foot_index,
2550 c->counter_map.mappos);
2551 break;
2552 case COUNTER_MAP_ZEROED:
2553 counters_map_zeroed(newcounters, repl,
2554 c->foot_index,
2555 c->counter_map.mappos,
2556 &c->counters);
2557 break;
2558 case COUNTER_MAP_SET:
2559 counters_map_set(newcounters, c->foot_index,
2560 &c->counters);
2561 break;
2562 }
2563 }
Harald Welte1cef74d2001-01-05 15:22:59 +00002564
Harald Welteaae69be2004-08-29 23:32:14 +00002565 list_for_each_entry(r, &c->rules, list) {
2566 DEBUGP("counter for index %u: ", r->index);
2567 switch (r->counter_map.maptype) {
2568 case COUNTER_MAP_NOMAP:
2569 counters_nomap(newcounters, r->index);
2570 break;
Harald Welte1cef74d2001-01-05 15:22:59 +00002571
Harald Welteaae69be2004-08-29 23:32:14 +00002572 case COUNTER_MAP_NORMAL_MAP:
2573 counters_normal_map(newcounters, repl,
2574 r->index,
2575 r->counter_map.mappos);
2576 break;
Harald Welte1cef74d2001-01-05 15:22:59 +00002577
Harald Welteaae69be2004-08-29 23:32:14 +00002578 case COUNTER_MAP_ZEROED:
2579 counters_map_zeroed(newcounters, repl,
2580 r->index,
2581 r->counter_map.mappos,
2582 &r->entry->counters);
2583 break;
2584
2585 case COUNTER_MAP_SET:
2586 counters_map_set(newcounters, r->index,
2587 &r->entry->counters);
2588 break;
2589 }
Marc Bouchere6869a82000-03-20 06:03:29 +00002590 }
2591 }
Rusty Russell62527ce2000-09-04 09:45:54 +00002592
Harald Welteaae69be2004-08-29 23:32:14 +00002593#ifdef IPTC_DEBUG2
2594 {
2595 int fd = open("/tmp/libiptc-so_set_add_counters.blob",
2596 O_CREAT|O_WRONLY);
2597 if (fd >= 0) {
2598 write(fd, newcounters, counterlen);
2599 close(fd);
2600 }
2601 }
2602#endif
2603
Harald Welted6ba6f52005-11-12 10:39:40 +00002604 ret = setsockopt(sockfd, TC_IPPROTO, SO_SET_ADD_COUNTERS,
2605 newcounters, counterlen);
Patrick McHardye0865ad2006-04-22 02:08:56 +00002606 if (ret < 0)
Harald Welted6ba6f52005-11-12 10:39:40 +00002607 goto out_free_newcounters;
Marc Bouchere6869a82000-03-20 06:03:29 +00002608
2609 free(repl->counters);
2610 free(repl);
2611 free(newcounters);
2612
Harald Welted6ba6f52005-11-12 10:39:40 +00002613finished:
Martin Josefsson841e4ae2003-05-02 15:30:11 +00002614 TC_FREE(handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00002615 return 1;
Harald Welted6ba6f52005-11-12 10:39:40 +00002616
2617out_free_newcounters:
2618 free(newcounters);
2619out_free_repl_counters:
2620 free(repl->counters);
2621out_free_repl:
2622 free(repl);
2623out_zero:
2624 return 0;
Marc Bouchere6869a82000-03-20 06:03:29 +00002625}
2626
2627/* Get raw socket. */
2628int
Patrick McHardy0b639362007-09-08 16:00:01 +00002629TC_GET_RAW_SOCKET(void)
Marc Bouchere6869a82000-03-20 06:03:29 +00002630{
2631 return sockfd;
2632}
2633
2634/* Translates errno numbers into more human-readable form than strerror. */
2635const char *
Rusty Russell79dee072000-05-02 16:45:16 +00002636TC_STRERROR(int err)
Marc Bouchere6869a82000-03-20 06:03:29 +00002637{
2638 unsigned int i;
2639 struct table_struct {
2640 void *fn;
2641 int err;
2642 const char *message;
2643 } table [] =
Harald Welte4ccfa632001-07-30 15:12:43 +00002644 { { TC_INIT, EPERM, "Permission denied (you must be root)" },
Rusty Russell79dee072000-05-02 16:45:16 +00002645 { TC_INIT, EINVAL, "Module is wrong version" },
Harald Welte4ccfa632001-07-30 15:12:43 +00002646 { TC_INIT, ENOENT,
2647 "Table does not exist (do you need to insmod?)" },
Rusty Russell79dee072000-05-02 16:45:16 +00002648 { TC_DELETE_CHAIN, ENOTEMPTY, "Chain is not empty" },
2649 { TC_DELETE_CHAIN, EINVAL, "Can't delete built-in chain" },
2650 { TC_DELETE_CHAIN, EMLINK,
Marc Bouchere6869a82000-03-20 06:03:29 +00002651 "Can't delete chain with references left" },
Rusty Russell79dee072000-05-02 16:45:16 +00002652 { TC_CREATE_CHAIN, EEXIST, "Chain already exists" },
2653 { TC_INSERT_ENTRY, E2BIG, "Index of insertion too big" },
2654 { TC_REPLACE_ENTRY, E2BIG, "Index of replacement too big" },
2655 { TC_DELETE_NUM_ENTRY, E2BIG, "Index of deletion too big" },
Harald Welte1cef74d2001-01-05 15:22:59 +00002656 { TC_READ_COUNTER, E2BIG, "Index of counter too big" },
2657 { TC_ZERO_COUNTER, E2BIG, "Index of counter too big" },
Rusty Russell79dee072000-05-02 16:45:16 +00002658 { TC_INSERT_ENTRY, ELOOP, "Loop found in table" },
2659 { TC_INSERT_ENTRY, EINVAL, "Target problem" },
Marc Bouchere6869a82000-03-20 06:03:29 +00002660 /* EINVAL for CHECK probably means bad interface. */
Rusty Russell79dee072000-05-02 16:45:16 +00002661 { TC_CHECK_PACKET, EINVAL,
Marc Boucherc8264992000-04-22 22:34:44 +00002662 "Bad arguments (does that interface exist?)" },
Harald Welte4ccfa632001-07-30 15:12:43 +00002663 { TC_CHECK_PACKET, ENOSYS,
2664 "Checking will most likely never get implemented" },
Marc Bouchere6869a82000-03-20 06:03:29 +00002665 /* ENOENT for DELETE probably means no matching rule */
Rusty Russell79dee072000-05-02 16:45:16 +00002666 { TC_DELETE_ENTRY, ENOENT,
Marc Boucherc8264992000-04-22 22:34:44 +00002667 "Bad rule (does a matching rule exist in that chain?)" },
Rusty Russell79dee072000-05-02 16:45:16 +00002668 { TC_SET_POLICY, ENOENT,
Marc Boucherc8264992000-04-22 22:34:44 +00002669 "Bad built-in chain name" },
Rusty Russell79dee072000-05-02 16:45:16 +00002670 { TC_SET_POLICY, EINVAL,
Marc Boucherc8264992000-04-22 22:34:44 +00002671 "Bad policy name" },
Harald Welte4ccfa632001-07-30 15:12:43 +00002672
2673 { NULL, 0, "Incompatible with this kernel" },
2674 { NULL, ENOPROTOOPT, "iptables who? (do you need to insmod?)" },
2675 { NULL, ENOSYS, "Will be implemented real soon. I promise ;)" },
2676 { NULL, ENOMEM, "Memory allocation problem" },
2677 { NULL, ENOENT, "No chain/target/match by that name" },
Marc Bouchere6869a82000-03-20 06:03:29 +00002678 };
2679
2680 for (i = 0; i < sizeof(table)/sizeof(struct table_struct); i++) {
2681 if ((!table[i].fn || table[i].fn == iptc_fn)
2682 && table[i].err == err)
2683 return table[i].message;
2684 }
2685
2686 return strerror(err);
2687}