blob: d0f51b4a05dd74f7f3bd3a63666b1d67e6a6ce4a [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>
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +020029#include <xtables.h>
Stephane Ouellette7cd00282004-05-14 08:21:06 +000030
Harald Welteaae69be2004-08-29 23:32:14 +000031#include "linux_list.h"
32
33//#define IPTC_DEBUG2 1
34
35#ifdef IPTC_DEBUG2
36#include <fcntl.h>
37#define DEBUGP(x, args...) fprintf(stderr, "%s: " x, __FUNCTION__, ## args)
38#define DEBUGP_C(x, args...) fprintf(stderr, x, ## args)
39#else
40#define DEBUGP(x, args...)
41#define DEBUGP_C(x, args...)
42#endif
43
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +000044#ifdef DEBUG
45#define debug(x, args...) fprintf(stderr, x, ## args)
46#else
47#define debug(x, args...)
48#endif
49
Marc Bouchere6869a82000-03-20 06:03:29 +000050static int sockfd = -1;
Derrik Pates664c0a32005-02-01 13:28:14 +000051static int sockfd_use = 0;
Marc Bouchere6869a82000-03-20 06:03:29 +000052static void *iptc_fn = NULL;
53
Patrick McHardy0b639362007-09-08 16:00:01 +000054static const char *hooknames[] = {
55 [HOOK_PRE_ROUTING] = "PREROUTING",
56 [HOOK_LOCAL_IN] = "INPUT",
57 [HOOK_FORWARD] = "FORWARD",
58 [HOOK_LOCAL_OUT] = "OUTPUT",
59 [HOOK_POST_ROUTING] = "POSTROUTING",
Rusty Russell10758b72000-09-14 07:37:33 +000060#ifdef HOOK_DROPPING
Patrick McHardy0b639362007-09-08 16:00:01 +000061 [HOOK_DROPPING] = "DROPPING"
Rusty Russell10758b72000-09-14 07:37:33 +000062#endif
Marc Bouchere6869a82000-03-20 06:03:29 +000063};
64
Harald Welteaae69be2004-08-29 23:32:14 +000065/* Convenience structures */
66struct ipt_error_target
67{
68 STRUCT_ENTRY_TARGET t;
69 char error[TABLE_MAXNAMELEN];
70};
71
72struct chain_head;
73struct rule_head;
74
Marc Bouchere6869a82000-03-20 06:03:29 +000075struct counter_map
76{
77 enum {
78 COUNTER_MAP_NOMAP,
79 COUNTER_MAP_NORMAL_MAP,
Harald Welte1cef74d2001-01-05 15:22:59 +000080 COUNTER_MAP_ZEROED,
81 COUNTER_MAP_SET
Marc Bouchere6869a82000-03-20 06:03:29 +000082 } maptype;
83 unsigned int mappos;
84};
85
Harald Welteaae69be2004-08-29 23:32:14 +000086enum iptcc_rule_type {
87 IPTCC_R_STANDARD, /* standard target (ACCEPT, ...) */
88 IPTCC_R_MODULE, /* extension module (SNAT, ...) */
89 IPTCC_R_FALLTHROUGH, /* fallthrough rule */
90 IPTCC_R_JUMP, /* jump to other chain */
Marc Bouchere6869a82000-03-20 06:03:29 +000091};
92
Harald Welteaae69be2004-08-29 23:32:14 +000093struct rule_head
Rusty Russell30fd6e52000-04-23 09:16:06 +000094{
Harald Welteaae69be2004-08-29 23:32:14 +000095 struct list_head list;
96 struct chain_head *chain;
97 struct counter_map counter_map;
98
99 unsigned int index; /* index (needed for counter_map) */
100 unsigned int offset; /* offset in rule blob */
101
102 enum iptcc_rule_type type;
103 struct chain_head *jump; /* jump target, if IPTCC_R_JUMP */
104
105 unsigned int size; /* size of entry data */
106 STRUCT_ENTRY entry[0];
107};
108
109struct chain_head
110{
111 struct list_head list;
Rusty Russell79dee072000-05-02 16:45:16 +0000112 char name[TABLE_MAXNAMELEN];
Harald Welteaae69be2004-08-29 23:32:14 +0000113 unsigned int hooknum; /* hook number+1 if builtin */
114 unsigned int references; /* how many jumps reference us */
115 int verdict; /* verdict if builtin */
116
117 STRUCT_COUNTERS counters; /* per-chain counters */
118 struct counter_map counter_map;
119
120 unsigned int num_rules; /* number of rules in list */
121 struct list_head rules; /* list of rules */
122
123 unsigned int index; /* index (needed for jump resolval) */
124 unsigned int head_offset; /* offset in rule blob */
125 unsigned int foot_index; /* index (needed for counter_map) */
126 unsigned int foot_offset; /* offset in rule blob */
Rusty Russell30fd6e52000-04-23 09:16:06 +0000127};
128
Rusty Russell79dee072000-05-02 16:45:16 +0000129STRUCT_TC_HANDLE
Marc Bouchere6869a82000-03-20 06:03:29 +0000130{
Harald Welteaae69be2004-08-29 23:32:14 +0000131 int changed; /* Have changes been made? */
132
133 struct list_head chains;
134
135 struct chain_head *chain_iterator_cur;
136 struct rule_head *rule_iterator_cur;
137
Jesper Dangaard Brouer48bde402008-01-15 17:06:48 +0000138 unsigned int num_chains; /* number of user defined chains */
139
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000140 struct chain_head **chain_index; /* array for fast chain list access*/
141 unsigned int chain_index_sz;/* size of chain index array */
142
Rusty Russell79dee072000-05-02 16:45:16 +0000143 STRUCT_GETINFO info;
Harald Welteaae69be2004-08-29 23:32:14 +0000144 STRUCT_GET_ENTRIES *entries;
Marc Bouchere6869a82000-03-20 06:03:29 +0000145};
146
Harald Welteaae69be2004-08-29 23:32:14 +0000147/* allocate a new chain head for the cache */
148static struct chain_head *iptcc_alloc_chain_head(const char *name, int hooknum)
149{
150 struct chain_head *c = malloc(sizeof(*c));
151 if (!c)
152 return NULL;
153 memset(c, 0, sizeof(*c));
154
155 strncpy(c->name, name, TABLE_MAXNAMELEN);
156 c->hooknum = hooknum;
157 INIT_LIST_HEAD(&c->rules);
158
159 return c;
160}
161
162/* allocate and initialize a new rule for the cache */
163static struct rule_head *iptcc_alloc_rule(struct chain_head *c, unsigned int size)
164{
165 struct rule_head *r = malloc(sizeof(*r)+size);
166 if (!r)
167 return NULL;
168 memset(r, 0, sizeof(*r));
169
170 r->chain = c;
171 r->size = size;
172
173 return r;
174}
175
176/* notify us that the ruleset has been modified by the user */
Jesper Dangaard Brouer91093982008-01-15 17:01:58 +0000177static inline void
Rusty Russell79dee072000-05-02 16:45:16 +0000178set_changed(TC_HANDLE_T h)
Rusty Russell175f6412000-03-24 09:32:20 +0000179{
Rusty Russell175f6412000-03-24 09:32:20 +0000180 h->changed = 1;
181}
182
Harald Welte380ba5f2002-02-13 16:19:55 +0000183#ifdef IPTC_DEBUG
Rusty Russell79dee072000-05-02 16:45:16 +0000184static void do_check(TC_HANDLE_T h, unsigned int line);
Rusty Russell849779c2000-04-23 15:51:51 +0000185#define CHECK(h) do { if (!getenv("IPTC_NO_CHECK")) do_check((h), __LINE__); } while(0)
Rusty Russell30fd6e52000-04-23 09:16:06 +0000186#else
187#define CHECK(h)
188#endif
Marc Bouchere6869a82000-03-20 06:03:29 +0000189
Harald Welteaae69be2004-08-29 23:32:14 +0000190
191/**********************************************************************
192 * iptc blob utility functions (iptcb_*)
193 **********************************************************************/
194
Marc Bouchere6869a82000-03-20 06:03:29 +0000195static inline int
Harald Welteaae69be2004-08-29 23:32:14 +0000196iptcb_get_number(const STRUCT_ENTRY *i,
Rusty Russell79dee072000-05-02 16:45:16 +0000197 const STRUCT_ENTRY *seek,
Marc Bouchere6869a82000-03-20 06:03:29 +0000198 unsigned int *pos)
199{
200 if (i == seek)
201 return 1;
202 (*pos)++;
203 return 0;
204}
205
Marc Bouchere6869a82000-03-20 06:03:29 +0000206static inline int
Harald Welteaae69be2004-08-29 23:32:14 +0000207iptcb_get_entry_n(STRUCT_ENTRY *i,
Marc Bouchere6869a82000-03-20 06:03:29 +0000208 unsigned int number,
209 unsigned int *pos,
Rusty Russell79dee072000-05-02 16:45:16 +0000210 STRUCT_ENTRY **pe)
Marc Bouchere6869a82000-03-20 06:03:29 +0000211{
212 if (*pos == number) {
213 *pe = i;
214 return 1;
215 }
216 (*pos)++;
217 return 0;
218}
219
Harald Welteaae69be2004-08-29 23:32:14 +0000220static inline STRUCT_ENTRY *
221iptcb_get_entry(TC_HANDLE_T h, unsigned int offset)
222{
223 return (STRUCT_ENTRY *)((char *)h->entries->entrytable + offset);
224}
225
226static unsigned int
227iptcb_entry2index(const TC_HANDLE_T h, const STRUCT_ENTRY *seek)
Marc Bouchere6869a82000-03-20 06:03:29 +0000228{
229 unsigned int pos = 0;
Marc Bouchere6869a82000-03-20 06:03:29 +0000230
Harald Welteaae69be2004-08-29 23:32:14 +0000231 if (ENTRY_ITERATE(h->entries->entrytable, h->entries->size,
232 iptcb_get_number, seek, &pos) == 0) {
233 fprintf(stderr, "ERROR: offset %u not an entry!\n",
234 (unsigned int)((char *)seek - (char *)h->entries->entrytable));
235 abort();
236 }
237 return pos;
Marc Bouchere6869a82000-03-20 06:03:29 +0000238}
239
Harald Welte0113fe72004-01-06 19:04:02 +0000240static inline STRUCT_ENTRY *
Harald Welteaae69be2004-08-29 23:32:14 +0000241iptcb_offset2entry(TC_HANDLE_T h, unsigned int offset)
Harald Welte0113fe72004-01-06 19:04:02 +0000242{
Harald Welteaae69be2004-08-29 23:32:14 +0000243 return (STRUCT_ENTRY *) ((void *)h->entries->entrytable+offset);
Harald Welte0113fe72004-01-06 19:04:02 +0000244}
245
Harald Welteaae69be2004-08-29 23:32:14 +0000246
Harald Welte0113fe72004-01-06 19:04:02 +0000247static inline unsigned long
Harald Welteaae69be2004-08-29 23:32:14 +0000248iptcb_entry2offset(const TC_HANDLE_T h, const STRUCT_ENTRY *e)
Harald Welte0113fe72004-01-06 19:04:02 +0000249{
Harald Welteaae69be2004-08-29 23:32:14 +0000250 return (void *)e - (void *)h->entries->entrytable;
Harald Welte3ea8f402003-06-23 18:25:59 +0000251}
252
253static inline unsigned int
Harald Welteaae69be2004-08-29 23:32:14 +0000254iptcb_offset2index(const TC_HANDLE_T h, unsigned int offset)
Harald Welte3ea8f402003-06-23 18:25:59 +0000255{
Harald Welteaae69be2004-08-29 23:32:14 +0000256 return iptcb_entry2index(h, iptcb_offset2entry(h, offset));
257}
258
259/* Returns 0 if not hook entry, else hooknumber + 1 */
260static inline unsigned int
261iptcb_ent_is_hook_entry(STRUCT_ENTRY *e, TC_HANDLE_T h)
262{
263 unsigned int i;
264
265 for (i = 0; i < NUMHOOKS; i++) {
266 if ((h->info.valid_hooks & (1 << i))
267 && iptcb_get_entry(h, h->info.hook_entry[i]) == e)
268 return i+1;
269 }
270 return 0;
Harald Welte3ea8f402003-06-23 18:25:59 +0000271}
272
273
Harald Welteaae69be2004-08-29 23:32:14 +0000274/**********************************************************************
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000275 * Chain index (cache utility) functions
276 **********************************************************************
277 * The chain index is an array with pointers into the chain list, with
278 * CHAIN_INDEX_BUCKET_LEN spacing. This facilitates the ability to
279 * speedup chain list searching, by find a more optimal starting
280 * points when searching the linked list.
281 *
282 * The starting point can be found fast by using a binary search of
283 * the chain index. Thus, reducing the previous search complexity of
284 * O(n) to O(log(n/k) + k) where k is CHAIN_INDEX_BUCKET_LEN.
285 *
286 * A nice property of the chain index, is that the "bucket" list
287 * length is max CHAIN_INDEX_BUCKET_LEN (when just build, inserts will
288 * change this). Oppose to hashing, where the "bucket" list length can
289 * vary a lot.
290 */
291#ifndef CHAIN_INDEX_BUCKET_LEN
292#define CHAIN_INDEX_BUCKET_LEN 40
293#endif
294
295/* Another nice property of the chain index is that inserting/creating
296 * chains in chain list don't change the correctness of the chain
297 * index, it only causes longer lists in the buckets.
298 *
299 * To mitigate the performance penalty of longer bucket lists and the
300 * penalty of rebuilding, the chain index is rebuild only when
301 * CHAIN_INDEX_INSERT_MAX chains has been added.
302 */
303#ifndef CHAIN_INDEX_INSERT_MAX
304#define CHAIN_INDEX_INSERT_MAX 355
305#endif
306
307static inline unsigned int iptcc_is_builtin(struct chain_head *c);
308
309
310/* Use binary search in the chain index array, to find a chain_head
311 * pointer closest to the place of the searched name element.
312 *
313 * Notes that, binary search (obviously) requires that the chain list
314 * is sorted by name.
315 */
316static struct list_head *
Jan Engelhardtdbb77542008-02-11 00:33:30 +0100317iptcc_bsearch_chain_index(const char *name, unsigned int *idx, TC_HANDLE_T handle)
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000318{
319 unsigned int pos, end;
320 int res;
321
322 struct list_head *list_pos;
323 list_pos=&handle->chains;
324
325 /* Check for empty array, e.g. no user defined chains */
326 if (handle->chain_index_sz == 0) {
327 debug("WARNING: handle->chain_index_sz == 0\n");
328 return list_pos;
329 }
330
331 /* Init */
332 end = handle->chain_index_sz;
333 pos = end / 2;
334
335 debug("bsearch Find chain:%s (pos:%d end:%d)\n", name, pos, end);
336
337 /* Loop */
338 loop:
339 if (!handle->chain_index[pos]) {
340 fprintf(stderr, "ERROR: NULL pointer chain_index[%d]\n", pos);
341 return &handle->chains; /* Be safe, return orig start pos */
342 }
343
344 res = strcmp(name, handle->chain_index[pos]->name);
345 list_pos = &handle->chain_index[pos]->list;
Jan Engelhardtdbb77542008-02-11 00:33:30 +0100346 *idx = pos;
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000347
348 debug("bsearch Index[%d] name:%s res:%d ",
349 pos, handle->chain_index[pos]->name, res);
350
351 if (res == 0) { /* Found element, by direct hit */
352 debug("[found] Direct hit pos:%d end:%d\n", pos, end);
353 return list_pos;
354 } else if (res < 0) { /* Too far, jump back */
355 end = pos;
356 pos = pos / 2;
357
358 /* Exit case: First element of array */
359 if (end == 0) {
360 debug("[found] Reached first array elem (end%d)\n",end);
361 return list_pos;
362 }
363 debug("jump back to pos:%d (end:%d)\n", pos, end);
364 goto loop;
365 } else if (res > 0 ){ /* Not far enough, jump forward */
366
367 /* Exit case: Last element of array */
368 if (pos == handle->chain_index_sz-1) {
369 debug("[found] Last array elem (end:%d)\n", end);
370 return list_pos;
371 }
372
373 /* Exit case: Next index less, thus elem in this list section */
374 res = strcmp(name, handle->chain_index[pos+1]->name);
375 if (res < 0) {
376 debug("[found] closest list (end:%d)\n", end);
377 return list_pos;
378 }
379
380 pos = (pos+end)/2;
381 debug("jump forward to pos:%d (end:%d)\n", pos, end);
382 goto loop;
383 }
384
385 return list_pos;
386}
387
388#ifdef DEBUG
389/* Trivial linear search of chain index. Function used for verifying
390 the output of bsearch function */
391static struct list_head *
392iptcc_linearly_search_chain_index(const char *name, TC_HANDLE_T handle)
393{
394 unsigned int i=0;
395 int res=0;
396
397 struct list_head *list_pos;
398 list_pos = &handle->chains;
399
400 if (handle->chain_index_sz)
401 list_pos = &handle->chain_index[0]->list;
402
403 /* Linearly walk of chain index array */
404
405 for (i=0; i < handle->chain_index_sz; i++) {
406 if (handle->chain_index[i]) {
407 res = strcmp(handle->chain_index[i]->name, name);
408 if (res > 0)
409 break; // One step too far
410 list_pos = &handle->chain_index[i]->list;
411 if (res == 0)
412 break; // Direct hit
413 }
414 }
415
416 return list_pos;
417}
418#endif
419
420static int iptcc_chain_index_alloc(TC_HANDLE_T h)
421{
422 unsigned int list_length = CHAIN_INDEX_BUCKET_LEN;
423 unsigned int array_elems;
424 unsigned int array_mem;
425
426 /* Allocate memory for the chain index array */
427 array_elems = (h->num_chains / list_length) +
428 (h->num_chains % list_length ? 1 : 0);
429 array_mem = sizeof(h->chain_index) * array_elems;
430
431 debug("Alloc Chain index, elems:%d mem:%d bytes\n",
432 array_elems, array_mem);
433
434 h->chain_index = malloc(array_mem);
435 if (!h->chain_index) {
436 h->chain_index_sz = 0;
437 return -ENOMEM;
438 }
439 memset(h->chain_index, 0, array_mem);
440 h->chain_index_sz = array_elems;
441
442 return 1;
443}
444
445static void iptcc_chain_index_free(TC_HANDLE_T h)
446{
447 h->chain_index_sz = 0;
448 free(h->chain_index);
449}
450
451
452#ifdef DEBUG
453static void iptcc_chain_index_dump(TC_HANDLE_T h)
454{
455 unsigned int i = 0;
456
457 /* Dump: contents of chain index array */
458 for (i=0; i < h->chain_index_sz; i++) {
459 if (h->chain_index[i]) {
460 fprintf(stderr, "Chain index[%d].name: %s\n",
461 i, h->chain_index[i]->name);
462 }
463 }
464}
465#endif
466
467/* Build the chain index */
468static int iptcc_chain_index_build(TC_HANDLE_T h)
469{
470 unsigned int list_length = CHAIN_INDEX_BUCKET_LEN;
471 unsigned int chains = 0;
472 unsigned int cindex = 0;
473 struct chain_head *c;
474
475 /* Build up the chain index array here */
476 debug("Building chain index\n");
477
478 debug("Number of user defined chains:%d bucket_sz:%d array_sz:%d\n",
479 h->num_chains, list_length, h->chain_index_sz);
480
481 if (h->chain_index_sz == 0)
482 return 0;
483
484 list_for_each_entry(c, &h->chains, list) {
485
486 /* Issue: The index array needs to start after the
487 * builtin chains, as they are not sorted */
488 if (!iptcc_is_builtin(c)) {
489 cindex=chains / list_length;
490
491 /* Safe guard, break out on array limit, this
492 * is useful if chains are added and array is
493 * rebuild, without realloc of memory. */
494 if (cindex >= h->chain_index_sz)
495 break;
496
497 if ((chains % list_length)== 0) {
498 debug("\nIndex[%d] Chains:", cindex);
499 h->chain_index[cindex] = c;
500 }
501 chains++;
502 }
503 debug("%s, ", c->name);
504 }
505 debug("\n");
506
507 return 1;
508}
509
510static int iptcc_chain_index_rebuild(TC_HANDLE_T h)
511{
512 debug("REBUILD chain index array\n");
513 iptcc_chain_index_free(h);
514 if ((iptcc_chain_index_alloc(h)) < 0)
515 return -ENOMEM;
516 iptcc_chain_index_build(h);
517 return 1;
518}
519
520/* Delete chain (pointer) from index array. Removing an element from
521 * the chain list only affects the chain index array, if the chain
522 * index points-to/uses that list pointer.
523 *
524 * There are different strategies, the simple and safe is to rebuild
525 * the chain index every time. The more advanced is to update the
526 * array index to point to the next element, but that requires some
527 * house keeping and boundry checks. The advanced is implemented, as
528 * the simple approach behaves badly when all chains are deleted
529 * because list_for_each processing will always hit the first chain
530 * index, thus causing a rebuild for every chain.
531 */
532static int iptcc_chain_index_delete_chain(struct chain_head *c, TC_HANDLE_T h)
533{
534 struct list_head *index_ptr, *index_ptr2, *next;
535 struct chain_head *c2;
Jan Engelhardtdbb77542008-02-11 00:33:30 +0100536 unsigned int idx, idx2;
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000537
Jan Engelhardtdbb77542008-02-11 00:33:30 +0100538 index_ptr = iptcc_bsearch_chain_index(c->name, &idx, h);
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000539
540 debug("Del chain[%s] c->list:%p index_ptr:%p\n",
541 c->name, &c->list, index_ptr);
542
543 /* Save the next pointer */
544 next = c->list.next;
545 list_del(&c->list);
546
547 if (index_ptr == &c->list) { /* Chain used as index ptr */
548
549 /* See if its possible to avoid a rebuild, by shifting
550 * to next pointer. Its possible if the next pointer
551 * is located in the same index bucket.
552 */
553 c2 = list_entry(next, struct chain_head, list);
Jan Engelhardtdbb77542008-02-11 00:33:30 +0100554 index_ptr2 = iptcc_bsearch_chain_index(c2->name, &idx2, h);
555 if (idx != idx2) {
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000556 /* Rebuild needed */
557 return iptcc_chain_index_rebuild(h);
558 } else {
559 /* Avoiding rebuild */
560 debug("Update cindex[%d] with next ptr name:[%s]\n",
Jan Engelhardtdbb77542008-02-11 00:33:30 +0100561 idx, c2->name);
562 h->chain_index[idx]=c2;
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000563 return 0;
564 }
565 }
566 return 0;
567}
568
569
570/**********************************************************************
Harald Welteaae69be2004-08-29 23:32:14 +0000571 * iptc cache utility functions (iptcc_*)
572 **********************************************************************/
Harald Welte0113fe72004-01-06 19:04:02 +0000573
Harald Welteaae69be2004-08-29 23:32:14 +0000574/* Is the given chain builtin (1) or user-defined (0) */
Jesper Dangaard Brouer91093982008-01-15 17:01:58 +0000575static inline unsigned int iptcc_is_builtin(struct chain_head *c)
Harald Welteaae69be2004-08-29 23:32:14 +0000576{
577 return (c->hooknum ? 1 : 0);
578}
579
580/* Get a specific rule within a chain */
581static struct rule_head *iptcc_get_rule_num(struct chain_head *c,
582 unsigned int rulenum)
583{
584 struct rule_head *r;
585 unsigned int num = 0;
586
587 list_for_each_entry(r, &c->rules, list) {
588 num++;
589 if (num == rulenum)
590 return r;
591 }
592 return NULL;
593}
594
Martin Josefssona5616dc2004-10-24 22:27:31 +0000595/* Get a specific rule within a chain backwards */
596static struct rule_head *iptcc_get_rule_num_reverse(struct chain_head *c,
597 unsigned int rulenum)
598{
599 struct rule_head *r;
600 unsigned int num = 0;
601
602 list_for_each_entry_reverse(r, &c->rules, list) {
603 num++;
604 if (num == rulenum)
605 return r;
606 }
607 return NULL;
608}
609
Harald Welteaae69be2004-08-29 23:32:14 +0000610/* Returns chain head if found, otherwise NULL. */
611static struct chain_head *
612iptcc_find_chain_by_offset(TC_HANDLE_T handle, unsigned int offset)
613{
614 struct list_head *pos;
615
616 if (list_empty(&handle->chains))
617 return NULL;
618
619 list_for_each(pos, &handle->chains) {
620 struct chain_head *c = list_entry(pos, struct chain_head, list);
621 if (offset >= c->head_offset && offset <= c->foot_offset)
622 return c;
Harald Welte0113fe72004-01-06 19:04:02 +0000623 }
624
Harald Welteaae69be2004-08-29 23:32:14 +0000625 return NULL;
Harald Welte0113fe72004-01-06 19:04:02 +0000626}
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000627
Harald Welteaae69be2004-08-29 23:32:14 +0000628/* Returns chain head if found, otherwise NULL. */
629static struct chain_head *
630iptcc_find_label(const char *name, TC_HANDLE_T handle)
631{
632 struct list_head *pos;
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000633 struct list_head *list_start_pos;
634 unsigned int i=0;
635 int res;
Harald Welteaae69be2004-08-29 23:32:14 +0000636
637 if (list_empty(&handle->chains))
638 return NULL;
639
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000640 /* First look at builtin chains */
Harald Welteaae69be2004-08-29 23:32:14 +0000641 list_for_each(pos, &handle->chains) {
642 struct chain_head *c = list_entry(pos, struct chain_head, list);
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000643 if (!iptcc_is_builtin(c))
644 break;
Harald Welteaae69be2004-08-29 23:32:14 +0000645 if (!strcmp(c->name, name))
646 return c;
647 }
648
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000649 /* Find a smart place to start the search via chain index */
650 //list_start_pos = iptcc_linearly_search_chain_index(name, handle);
651 list_start_pos = iptcc_bsearch_chain_index(name, &i, handle);
652
653 /* Handel if bsearch bails out early */
654 if (list_start_pos == &handle->chains) {
655 list_start_pos = pos;
656 }
657#ifdef DEBUG
658 else {
659 /* Verify result of bsearch against linearly index search */
660 struct list_head *test_pos;
661 struct chain_head *test_c, *tmp_c;
662 test_pos = iptcc_linearly_search_chain_index(name, handle);
663 if (list_start_pos != test_pos) {
664 debug("BUG in chain_index search\n");
665 test_c=list_entry(test_pos, struct chain_head,list);
666 tmp_c =list_entry(list_start_pos,struct chain_head,list);
667 debug("Verify search found:\n");
668 debug(" Chain:%s\n", test_c->name);
669 debug("BSearch found:\n");
670 debug(" Chain:%s\n", tmp_c->name);
671 exit(42);
672 }
673 }
674#endif
675
676 /* Initial/special case, no user defined chains */
677 if (handle->num_chains == 0)
678 return NULL;
679
680 /* Start searching through the chain list */
681 list_for_each(pos, list_start_pos->prev) {
682 struct chain_head *c = list_entry(pos, struct chain_head, list);
683 res = strcmp(c->name, name);
684 debug("List search name:%s == %s res:%d\n", name, c->name, res);
685 if (res==0)
686 return c;
687
688 /* We can stop earlier as we know list is sorted */
689 if (res>0 && !iptcc_is_builtin(c)) { /* Walked too far*/
690 debug(" Not in list, walked too far, sorted list\n");
691 return NULL;
692 }
693
694 /* Stop on wrap around, if list head is reached */
695 if (pos == &handle->chains) {
696 debug("Stop, list head reached\n");
697 return NULL;
698 }
699 }
700
701 debug("List search NOT found name:%s\n", name);
Harald Welteaae69be2004-08-29 23:32:14 +0000702 return NULL;
703}
704
705/* called when rule is to be removed from cache */
706static void iptcc_delete_rule(struct rule_head *r)
707{
708 DEBUGP("deleting rule %p (offset %u)\n", r, r->offset);
709 /* clean up reference count of called chain */
710 if (r->type == IPTCC_R_JUMP
711 && r->jump)
712 r->jump->references--;
713
714 list_del(&r->list);
715 free(r);
716}
717
718
719/**********************************************************************
720 * RULESET PARSER (blob -> cache)
721 **********************************************************************/
722
Harald Welteaae69be2004-08-29 23:32:14 +0000723/* Delete policy rule of previous chain, since cache doesn't contain
724 * chain policy rules.
725 * WARNING: This function has ugly design and relies on a lot of context, only
726 * to be called from specific places within the parser */
727static int __iptcc_p_del_policy(TC_HANDLE_T h, unsigned int num)
728{
729 if (h->chain_iterator_cur) {
730 /* policy rule is last rule */
731 struct rule_head *pr = (struct rule_head *)
732 h->chain_iterator_cur->rules.prev;
733
734 /* save verdict */
735 h->chain_iterator_cur->verdict =
736 *(int *)GET_TARGET(pr->entry)->data;
737
738 /* save counter and counter_map information */
739 h->chain_iterator_cur->counter_map.maptype =
740 COUNTER_MAP_NORMAL_MAP;
741 h->chain_iterator_cur->counter_map.mappos = num-1;
742 memcpy(&h->chain_iterator_cur->counters, &pr->entry->counters,
743 sizeof(h->chain_iterator_cur->counters));
744
745 /* foot_offset points to verdict rule */
746 h->chain_iterator_cur->foot_index = num;
747 h->chain_iterator_cur->foot_offset = pr->offset;
748
749 /* delete rule from cache */
750 iptcc_delete_rule(pr);
Martin Josefsson8d1b38a2004-09-22 21:00:19 +0000751 h->chain_iterator_cur->num_rules--;
Harald Welteaae69be2004-08-29 23:32:14 +0000752
753 return 1;
754 }
755 return 0;
756}
757
Harald Welteec30b6c2005-02-01 16:45:56 +0000758/* alphabetically insert a chain into the list */
759static inline void iptc_insert_chain(TC_HANDLE_T h, struct chain_head *c)
760{
761 struct chain_head *tmp;
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000762 struct list_head *list_start_pos;
763 unsigned int i=1;
764
765 /* Find a smart place to start the insert search */
766 list_start_pos = iptcc_bsearch_chain_index(c->name, &i, h);
767
768 /* Handle the case, where chain.name is smaller than index[0] */
769 if (i==0 && strcmp(c->name, h->chain_index[0]->name) <= 0) {
770 h->chain_index[0] = c; /* Update chain index head */
771 list_start_pos = h->chains.next;
772 debug("Update chain_index[0] with %s\n", c->name);
773 }
774
775 /* Handel if bsearch bails out early */
776 if (list_start_pos == &h->chains) {
777 list_start_pos = h->chains.next;
778 }
Harald Welteec30b6c2005-02-01 16:45:56 +0000779
Olaf Rempel9d3ed772005-03-04 23:08:30 +0000780 /* sort only user defined chains */
781 if (!c->hooknum) {
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000782 list_for_each_entry(tmp, list_start_pos->prev, list) {
Robert de Barthfeca0572005-07-31 07:04:59 +0000783 if (!tmp->hooknum && strcmp(c->name, tmp->name) <= 0) {
Olaf Rempel9d3ed772005-03-04 23:08:30 +0000784 list_add(&c->list, tmp->list.prev);
785 return;
786 }
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000787
788 /* Stop if list head is reached */
789 if (&tmp->list == &h->chains) {
790 debug("Insert, list head reached add to tail\n");
791 break;
792 }
Harald Welteec30b6c2005-02-01 16:45:56 +0000793 }
794 }
795
796 /* survived till end of list: add at tail */
797 list_add_tail(&c->list, &h->chains);
798}
799
Harald Welteaae69be2004-08-29 23:32:14 +0000800/* Another ugly helper function split out of cache_add_entry to make it less
801 * spaghetti code */
802static void __iptcc_p_add_chain(TC_HANDLE_T h, struct chain_head *c,
803 unsigned int offset, unsigned int *num)
804{
Jesper Dangaard Brouer13364512007-12-12 15:20:42 +0000805 struct list_head *tail = h->chains.prev;
806 struct chain_head *ctail;
807
Harald Welteaae69be2004-08-29 23:32:14 +0000808 __iptcc_p_del_policy(h, *num);
809
810 c->head_offset = offset;
811 c->index = *num;
812
Jesper Dangaard Brouer13364512007-12-12 15:20:42 +0000813 /* Chains from kernel are already sorted, as they are inserted
814 * sorted. But there exists an issue when shifting to 1.4.0
815 * from an older version, as old versions allow last created
816 * chain to be unsorted.
817 */
818 if (iptcc_is_builtin(c)) /* Only user defined chains are sorted*/
819 list_add_tail(&c->list, &h->chains);
820 else {
821 ctail = list_entry(tail, struct chain_head, list);
822 if (strcmp(c->name, ctail->name) > 0)
823 list_add_tail(&c->list, &h->chains);/* Already sorted*/
824 else
825 iptc_insert_chain(h, c);/* Was not sorted */
826 }
Jesper Dangaard Brouerd8cb7872007-11-28 08:40:26 +0000827
Harald Welteaae69be2004-08-29 23:32:14 +0000828 h->chain_iterator_cur = c;
829}
830
831/* main parser function: add an entry from the blob to the cache */
832static int cache_add_entry(STRUCT_ENTRY *e,
833 TC_HANDLE_T h,
834 STRUCT_ENTRY **prev,
835 unsigned int *num)
836{
837 unsigned int builtin;
838 unsigned int offset = (char *)e - (char *)h->entries->entrytable;
839
840 DEBUGP("entering...");
841
842 /* Last entry ("policy rule"). End it.*/
843 if (iptcb_entry2offset(h,e) + e->next_offset == h->entries->size) {
844 /* This is the ERROR node at the end of the chain */
845 DEBUGP_C("%u:%u: end of table:\n", *num, offset);
846
847 __iptcc_p_del_policy(h, *num);
848
849 h->chain_iterator_cur = NULL;
850 goto out_inc;
851 }
852
853 /* We know this is the start of a new chain if it's an ERROR
854 * target, or a hook entry point */
855
856 if (strcmp(GET_TARGET(e)->u.user.name, ERROR_TARGET) == 0) {
857 struct chain_head *c =
858 iptcc_alloc_chain_head((const char *)GET_TARGET(e)->data, 0);
859 DEBUGP_C("%u:%u:new userdefined chain %s: %p\n", *num, offset,
860 (char *)c->name, c);
861 if (!c) {
862 errno = -ENOMEM;
863 return -1;
864 }
Jesper Dangaard Brouer48bde402008-01-15 17:06:48 +0000865 h->num_chains++; /* New user defined chain */
Harald Welteaae69be2004-08-29 23:32:14 +0000866
867 __iptcc_p_add_chain(h, c, offset, num);
868
869 } else if ((builtin = iptcb_ent_is_hook_entry(e, h)) != 0) {
870 struct chain_head *c =
871 iptcc_alloc_chain_head((char *)hooknames[builtin-1],
872 builtin);
873 DEBUGP_C("%u:%u new builtin chain: %p (rules=%p)\n",
874 *num, offset, c, &c->rules);
875 if (!c) {
876 errno = -ENOMEM;
877 return -1;
878 }
879
880 c->hooknum = builtin;
881
882 __iptcc_p_add_chain(h, c, offset, num);
883
884 /* FIXME: this is ugly. */
885 goto new_rule;
886 } else {
887 /* has to be normal rule */
888 struct rule_head *r;
889new_rule:
890
891 if (!(r = iptcc_alloc_rule(h->chain_iterator_cur,
892 e->next_offset))) {
893 errno = ENOMEM;
894 return -1;
895 }
896 DEBUGP_C("%u:%u normal rule: %p: ", *num, offset, r);
897
898 r->index = *num;
899 r->offset = offset;
900 memcpy(r->entry, e, e->next_offset);
901 r->counter_map.maptype = COUNTER_MAP_NORMAL_MAP;
902 r->counter_map.mappos = r->index;
903
904 /* handling of jumps, etc. */
905 if (!strcmp(GET_TARGET(e)->u.user.name, STANDARD_TARGET)) {
906 STRUCT_STANDARD_TARGET *t;
907
908 t = (STRUCT_STANDARD_TARGET *)GET_TARGET(e);
909 if (t->target.u.target_size
910 != ALIGN(sizeof(STRUCT_STANDARD_TARGET))) {
911 errno = EINVAL;
912 return -1;
913 }
914
915 if (t->verdict < 0) {
916 DEBUGP_C("standard, verdict=%d\n", t->verdict);
917 r->type = IPTCC_R_STANDARD;
918 } else if (t->verdict == r->offset+e->next_offset) {
919 DEBUGP_C("fallthrough\n");
920 r->type = IPTCC_R_FALLTHROUGH;
921 } else {
922 DEBUGP_C("jump, target=%u\n", t->verdict);
923 r->type = IPTCC_R_JUMP;
924 /* Jump target fixup has to be deferred
925 * until second pass, since we migh not
926 * yet have parsed the target */
927 }
Martin Josefsson52c38022004-09-22 19:39:40 +0000928 } else {
929 DEBUGP_C("module, target=%s\n", GET_TARGET(e)->u.user.name);
930 r->type = IPTCC_R_MODULE;
Harald Welteaae69be2004-08-29 23:32:14 +0000931 }
932
933 list_add_tail(&r->list, &h->chain_iterator_cur->rules);
Martin Josefsson8d1b38a2004-09-22 21:00:19 +0000934 h->chain_iterator_cur->num_rules++;
Harald Welteaae69be2004-08-29 23:32:14 +0000935 }
936out_inc:
937 (*num)++;
938 return 0;
939}
940
941
942/* parse an iptables blob into it's pieces */
943static int parse_table(TC_HANDLE_T h)
944{
945 STRUCT_ENTRY *prev;
946 unsigned int num = 0;
947 struct chain_head *c;
948
949 /* First pass: over ruleset blob */
950 ENTRY_ITERATE(h->entries->entrytable, h->entries->size,
951 cache_add_entry, h, &prev, &num);
952
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000953 /* Build the chain index, used for chain list search speedup */
954 if ((iptcc_chain_index_alloc(h)) < 0)
955 return -ENOMEM;
956 iptcc_chain_index_build(h);
957
Harald Welteaae69be2004-08-29 23:32:14 +0000958 /* Second pass: fixup parsed data from first pass */
959 list_for_each_entry(c, &h->chains, list) {
960 struct rule_head *r;
961 list_for_each_entry(r, &c->rules, list) {
Jan Engelhardtdbb77542008-02-11 00:33:30 +0100962 struct chain_head *lc;
Harald Welteaae69be2004-08-29 23:32:14 +0000963 STRUCT_STANDARD_TARGET *t;
964
965 if (r->type != IPTCC_R_JUMP)
966 continue;
967
968 t = (STRUCT_STANDARD_TARGET *)GET_TARGET(r->entry);
Jan Engelhardtdbb77542008-02-11 00:33:30 +0100969 lc = iptcc_find_chain_by_offset(h, t->verdict);
970 if (!lc)
Harald Welteaae69be2004-08-29 23:32:14 +0000971 return -1;
Jan Engelhardtdbb77542008-02-11 00:33:30 +0100972 r->jump = lc;
973 lc->references++;
Harald Welteaae69be2004-08-29 23:32:14 +0000974 }
975 }
976
977 /* FIXME: sort chains */
978
979 return 1;
980}
981
982
983/**********************************************************************
984 * RULESET COMPILATION (cache -> blob)
985 **********************************************************************/
986
987/* Convenience structures */
988struct iptcb_chain_start{
989 STRUCT_ENTRY e;
990 struct ipt_error_target name;
991};
992#define IPTCB_CHAIN_START_SIZE (sizeof(STRUCT_ENTRY) + \
993 ALIGN(sizeof(struct ipt_error_target)))
994
995struct iptcb_chain_foot {
996 STRUCT_ENTRY e;
997 STRUCT_STANDARD_TARGET target;
998};
999#define IPTCB_CHAIN_FOOT_SIZE (sizeof(STRUCT_ENTRY) + \
1000 ALIGN(sizeof(STRUCT_STANDARD_TARGET)))
1001
1002struct iptcb_chain_error {
1003 STRUCT_ENTRY entry;
1004 struct ipt_error_target target;
1005};
1006#define IPTCB_CHAIN_ERROR_SIZE (sizeof(STRUCT_ENTRY) + \
1007 ALIGN(sizeof(struct ipt_error_target)))
1008
1009
1010
1011/* compile rule from cache into blob */
1012static inline int iptcc_compile_rule (TC_HANDLE_T h, STRUCT_REPLACE *repl, struct rule_head *r)
1013{
1014 /* handle jumps */
1015 if (r->type == IPTCC_R_JUMP) {
1016 STRUCT_STANDARD_TARGET *t;
1017 t = (STRUCT_STANDARD_TARGET *)GET_TARGET(r->entry);
1018 /* memset for memcmp convenience on delete/replace */
1019 memset(t->target.u.user.name, 0, FUNCTION_MAXNAMELEN);
1020 strcpy(t->target.u.user.name, STANDARD_TARGET);
1021 /* Jumps can only happen to builtin chains, so we
1022 * can safely assume that they always have a header */
1023 t->verdict = r->jump->head_offset + IPTCB_CHAIN_START_SIZE;
1024 } else if (r->type == IPTCC_R_FALLTHROUGH) {
1025 STRUCT_STANDARD_TARGET *t;
1026 t = (STRUCT_STANDARD_TARGET *)GET_TARGET(r->entry);
1027 t->verdict = r->offset + r->size;
1028 }
1029
1030 /* copy entry from cache to blob */
1031 memcpy((char *)repl->entries+r->offset, r->entry, r->size);
1032
1033 return 1;
1034}
1035
1036/* compile chain from cache into blob */
1037static int iptcc_compile_chain(TC_HANDLE_T h, STRUCT_REPLACE *repl, struct chain_head *c)
1038{
1039 int ret;
1040 struct rule_head *r;
1041 struct iptcb_chain_start *head;
1042 struct iptcb_chain_foot *foot;
1043
1044 /* only user-defined chains have heaer */
1045 if (!iptcc_is_builtin(c)) {
1046 /* put chain header in place */
1047 head = (void *)repl->entries + c->head_offset;
1048 head->e.target_offset = sizeof(STRUCT_ENTRY);
1049 head->e.next_offset = IPTCB_CHAIN_START_SIZE;
1050 strcpy(head->name.t.u.user.name, ERROR_TARGET);
1051 head->name.t.u.target_size =
1052 ALIGN(sizeof(struct ipt_error_target));
1053 strcpy(head->name.error, c->name);
1054 } else {
1055 repl->hook_entry[c->hooknum-1] = c->head_offset;
1056 repl->underflow[c->hooknum-1] = c->foot_offset;
1057 }
1058
1059 /* iterate over rules */
1060 list_for_each_entry(r, &c->rules, list) {
1061 ret = iptcc_compile_rule(h, repl, r);
1062 if (ret < 0)
1063 return ret;
1064 }
1065
1066 /* put chain footer in place */
1067 foot = (void *)repl->entries + c->foot_offset;
1068 foot->e.target_offset = sizeof(STRUCT_ENTRY);
1069 foot->e.next_offset = IPTCB_CHAIN_FOOT_SIZE;
1070 strcpy(foot->target.target.u.user.name, STANDARD_TARGET);
1071 foot->target.target.u.target_size =
1072 ALIGN(sizeof(STRUCT_STANDARD_TARGET));
1073 /* builtin targets have verdict, others return */
1074 if (iptcc_is_builtin(c))
1075 foot->target.verdict = c->verdict;
1076 else
1077 foot->target.verdict = RETURN;
1078 /* set policy-counters */
1079 memcpy(&foot->e.counters, &c->counters, sizeof(STRUCT_COUNTERS));
1080
1081 return 0;
1082}
1083
1084/* calculate offset and number for every rule in the cache */
1085static int iptcc_compile_chain_offsets(TC_HANDLE_T h, struct chain_head *c,
Harald Welteefa8fc22005-07-19 22:03:49 +00001086 unsigned int *offset, unsigned int *num)
Harald Welteaae69be2004-08-29 23:32:14 +00001087{
1088 struct rule_head *r;
1089
1090 c->head_offset = *offset;
1091 DEBUGP("%s: chain_head %u, offset=%u\n", c->name, *num, *offset);
1092
1093 if (!iptcc_is_builtin(c)) {
1094 /* Chain has header */
1095 *offset += sizeof(STRUCT_ENTRY)
1096 + ALIGN(sizeof(struct ipt_error_target));
1097 (*num)++;
1098 }
1099
1100 list_for_each_entry(r, &c->rules, list) {
1101 DEBUGP("rule %u, offset=%u, index=%u\n", *num, *offset, *num);
1102 r->offset = *offset;
1103 r->index = *num;
1104 *offset += r->size;
1105 (*num)++;
1106 }
1107
1108 DEBUGP("%s; chain_foot %u, offset=%u, index=%u\n", c->name, *num,
1109 *offset, *num);
1110 c->foot_offset = *offset;
1111 c->foot_index = *num;
1112 *offset += sizeof(STRUCT_ENTRY)
1113 + ALIGN(sizeof(STRUCT_STANDARD_TARGET));
1114 (*num)++;
1115
1116 return 1;
1117}
1118
1119/* put the pieces back together again */
1120static int iptcc_compile_table_prep(TC_HANDLE_T h, unsigned int *size)
1121{
1122 struct chain_head *c;
1123 unsigned int offset = 0, num = 0;
1124 int ret = 0;
1125
1126 /* First pass: calculate offset for every rule */
1127 list_for_each_entry(c, &h->chains, list) {
1128 ret = iptcc_compile_chain_offsets(h, c, &offset, &num);
1129 if (ret < 0)
1130 return ret;
1131 }
1132
1133 /* Append one error rule at end of chain */
1134 num++;
1135 offset += sizeof(STRUCT_ENTRY)
1136 + ALIGN(sizeof(struct ipt_error_target));
1137
1138 /* ruleset size is now in offset */
1139 *size = offset;
1140 return num;
1141}
1142
1143static int iptcc_compile_table(TC_HANDLE_T h, STRUCT_REPLACE *repl)
1144{
1145 struct chain_head *c;
1146 struct iptcb_chain_error *error;
1147
1148 /* Second pass: copy from cache to offsets, fill in jumps */
1149 list_for_each_entry(c, &h->chains, list) {
1150 int ret = iptcc_compile_chain(h, repl, c);
1151 if (ret < 0)
1152 return ret;
1153 }
1154
1155 /* Append error rule at end of chain */
1156 error = (void *)repl->entries + repl->size - IPTCB_CHAIN_ERROR_SIZE;
1157 error->entry.target_offset = sizeof(STRUCT_ENTRY);
1158 error->entry.next_offset = IPTCB_CHAIN_ERROR_SIZE;
1159 error->target.t.u.user.target_size =
1160 ALIGN(sizeof(struct ipt_error_target));
1161 strcpy((char *)&error->target.t.u.user.name, ERROR_TARGET);
1162 strcpy((char *)&error->target.error, "ERROR");
1163
1164 return 1;
1165}
1166
1167/**********************************************************************
1168 * EXTERNAL API (operates on cache only)
1169 **********************************************************************/
Marc Bouchere6869a82000-03-20 06:03:29 +00001170
1171/* Allocate handle of given size */
Rusty Russell79dee072000-05-02 16:45:16 +00001172static TC_HANDLE_T
Harald Welte0113fe72004-01-06 19:04:02 +00001173alloc_handle(const char *tablename, unsigned int size, unsigned int num_rules)
Marc Bouchere6869a82000-03-20 06:03:29 +00001174{
1175 size_t len;
Rusty Russell79dee072000-05-02 16:45:16 +00001176 TC_HANDLE_T h;
Marc Bouchere6869a82000-03-20 06:03:29 +00001177
Harald Welteaae69be2004-08-29 23:32:14 +00001178 len = sizeof(STRUCT_TC_HANDLE) + size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001179
Harald Welteaae69be2004-08-29 23:32:14 +00001180 h = malloc(sizeof(STRUCT_TC_HANDLE));
1181 if (!h) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001182 errno = ENOMEM;
1183 return NULL;
1184 }
Harald Welteaae69be2004-08-29 23:32:14 +00001185 memset(h, 0, sizeof(*h));
1186 INIT_LIST_HEAD(&h->chains);
Marc Bouchere6869a82000-03-20 06:03:29 +00001187 strcpy(h->info.name, tablename);
Harald Welteaae69be2004-08-29 23:32:14 +00001188
Harald Welte0371c0c2004-09-19 21:00:12 +00001189 h->entries = malloc(sizeof(STRUCT_GET_ENTRIES) + size);
Harald Welteaae69be2004-08-29 23:32:14 +00001190 if (!h->entries)
1191 goto out_free_handle;
1192
1193 strcpy(h->entries->name, tablename);
Harald Welte0371c0c2004-09-19 21:00:12 +00001194 h->entries->size = size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001195
1196 return h;
Harald Welteaae69be2004-08-29 23:32:14 +00001197
1198out_free_handle:
1199 free(h);
1200
1201 return NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +00001202}
1203
Harald Welteaae69be2004-08-29 23:32:14 +00001204
Rusty Russell79dee072000-05-02 16:45:16 +00001205TC_HANDLE_T
1206TC_INIT(const char *tablename)
Marc Bouchere6869a82000-03-20 06:03:29 +00001207{
Rusty Russell79dee072000-05-02 16:45:16 +00001208 TC_HANDLE_T h;
1209 STRUCT_GETINFO info;
Harald Welteefa8fc22005-07-19 22:03:49 +00001210 unsigned int tmp;
Marc Bouchere6869a82000-03-20 06:03:29 +00001211 socklen_t s;
1212
Rusty Russell79dee072000-05-02 16:45:16 +00001213 iptc_fn = TC_INIT;
Marc Bouchere6869a82000-03-20 06:03:29 +00001214
Martin Josefsson841e4ae2003-05-02 15:30:11 +00001215 if (strlen(tablename) >= TABLE_MAXNAMELEN) {
1216 errno = EINVAL;
1217 return NULL;
1218 }
1219
Derrik Pates664c0a32005-02-01 13:28:14 +00001220 if (sockfd_use == 0) {
1221 sockfd = socket(TC_AF, SOCK_RAW, IPPROTO_RAW);
1222 if (sockfd < 0)
1223 return NULL;
1224 }
1225 sockfd_use++;
Patrick McHardy2f932052008-04-02 14:01:53 +02001226retry:
Marc Bouchere6869a82000-03-20 06:03:29 +00001227 s = sizeof(info);
Martin Josefsson841e4ae2003-05-02 15:30:11 +00001228
Marc Bouchere6869a82000-03-20 06:03:29 +00001229 strcpy(info.name, tablename);
Derrik Pates664c0a32005-02-01 13:28:14 +00001230 if (getsockopt(sockfd, TC_IPPROTO, SO_GET_INFO, &info, &s) < 0) {
1231 if (--sockfd_use == 0) {
1232 close(sockfd);
1233 sockfd = -1;
1234 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001235 return NULL;
Derrik Pates664c0a32005-02-01 13:28:14 +00001236 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001237
Harald Welteaae69be2004-08-29 23:32:14 +00001238 DEBUGP("valid_hooks=0x%08x, num_entries=%u, size=%u\n",
1239 info.valid_hooks, info.num_entries, info.size);
1240
Harald Welte0113fe72004-01-06 19:04:02 +00001241 if ((h = alloc_handle(info.name, info.size, info.num_entries))
Martin Josefsson841e4ae2003-05-02 15:30:11 +00001242 == NULL) {
Derrik Pates664c0a32005-02-01 13:28:14 +00001243 if (--sockfd_use == 0) {
1244 close(sockfd);
1245 sockfd = -1;
1246 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001247 return NULL;
Martin Josefsson841e4ae2003-05-02 15:30:11 +00001248 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001249
Marc Bouchere6869a82000-03-20 06:03:29 +00001250 /* Initialize current state */
1251 h->info = info;
Harald Welte0113fe72004-01-06 19:04:02 +00001252
Harald Welteaae69be2004-08-29 23:32:14 +00001253 h->entries->size = h->info.size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001254
Rusty Russell79dee072000-05-02 16:45:16 +00001255 tmp = sizeof(STRUCT_GET_ENTRIES) + h->info.size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001256
Harald Welteaae69be2004-08-29 23:32:14 +00001257 if (getsockopt(sockfd, TC_IPPROTO, SO_GET_ENTRIES, h->entries,
1258 &tmp) < 0)
1259 goto error;
1260
1261#ifdef IPTC_DEBUG2
1262 {
1263 int fd = open("/tmp/libiptc-so_get_entries.blob",
1264 O_CREAT|O_WRONLY);
1265 if (fd >= 0) {
1266 write(fd, h->entries, tmp);
1267 close(fd);
1268 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001269 }
Harald Welteaae69be2004-08-29 23:32:14 +00001270#endif
1271
1272 if (parse_table(h) < 0)
1273 goto error;
Rusty Russell7e53bf92000-03-20 07:03:28 +00001274
Marc Bouchere6869a82000-03-20 06:03:29 +00001275 CHECK(h);
1276 return h;
Harald Welteaae69be2004-08-29 23:32:14 +00001277error:
1278 TC_FREE(&h);
Patrick McHardy2f932052008-04-02 14:01:53 +02001279 /* A different process changed the ruleset size, retry */
1280 if (errno == EAGAIN)
1281 goto retry;
Harald Welteaae69be2004-08-29 23:32:14 +00001282 return NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +00001283}
1284
Martin Josefsson841e4ae2003-05-02 15:30:11 +00001285void
1286TC_FREE(TC_HANDLE_T *h)
1287{
Harald Welteaae69be2004-08-29 23:32:14 +00001288 struct chain_head *c, *tmp;
1289
Derrik Pates664c0a32005-02-01 13:28:14 +00001290 iptc_fn = TC_FREE;
1291 if (--sockfd_use == 0) {
1292 close(sockfd);
1293 sockfd = -1;
1294 }
Harald Welteaae69be2004-08-29 23:32:14 +00001295
1296 list_for_each_entry_safe(c, tmp, &(*h)->chains, list) {
1297 struct rule_head *r, *rtmp;
1298
1299 list_for_each_entry_safe(r, rtmp, &c->rules, list) {
1300 free(r);
1301 }
1302
1303 free(c);
1304 }
1305
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +00001306 iptcc_chain_index_free(*h);
1307
Harald Welteaae69be2004-08-29 23:32:14 +00001308 free((*h)->entries);
Martin Josefsson841e4ae2003-05-02 15:30:11 +00001309 free(*h);
Harald Welteaae69be2004-08-29 23:32:14 +00001310
Martin Josefsson841e4ae2003-05-02 15:30:11 +00001311 *h = NULL;
1312}
1313
Marc Bouchere6869a82000-03-20 06:03:29 +00001314static inline int
Rusty Russell79dee072000-05-02 16:45:16 +00001315print_match(const STRUCT_ENTRY_MATCH *m)
Marc Bouchere6869a82000-03-20 06:03:29 +00001316{
Rusty Russell228e98d2000-04-27 10:28:06 +00001317 printf("Match name: `%s'\n", m->u.user.name);
Marc Bouchere6869a82000-03-20 06:03:29 +00001318 return 0;
1319}
1320
Rusty Russell79dee072000-05-02 16:45:16 +00001321static int dump_entry(STRUCT_ENTRY *e, const TC_HANDLE_T handle);
1322
Marc Bouchere6869a82000-03-20 06:03:29 +00001323void
Rusty Russell79dee072000-05-02 16:45:16 +00001324TC_DUMP_ENTRIES(const TC_HANDLE_T handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001325{
Derrik Pates664c0a32005-02-01 13:28:14 +00001326 iptc_fn = TC_DUMP_ENTRIES;
Marc Bouchere6869a82000-03-20 06:03:29 +00001327 CHECK(handle);
Patrick McHardy97fb2f12007-09-08 16:52:25 +00001328
Rusty Russelle45c7132004-12-16 13:21:44 +00001329 printf("libiptc v%s. %u bytes.\n",
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +02001330 XTABLES_VERSION, handle->entries->size);
Marc Bouchere6869a82000-03-20 06:03:29 +00001331 printf("Table `%s'\n", handle->info.name);
1332 printf("Hooks: pre/in/fwd/out/post = %u/%u/%u/%u/%u\n",
Rusty Russell67088e72000-05-10 01:18:57 +00001333 handle->info.hook_entry[HOOK_PRE_ROUTING],
1334 handle->info.hook_entry[HOOK_LOCAL_IN],
1335 handle->info.hook_entry[HOOK_FORWARD],
1336 handle->info.hook_entry[HOOK_LOCAL_OUT],
1337 handle->info.hook_entry[HOOK_POST_ROUTING]);
Marc Bouchere6869a82000-03-20 06:03:29 +00001338 printf("Underflows: pre/in/fwd/out/post = %u/%u/%u/%u/%u\n",
Rusty Russell67088e72000-05-10 01:18:57 +00001339 handle->info.underflow[HOOK_PRE_ROUTING],
1340 handle->info.underflow[HOOK_LOCAL_IN],
1341 handle->info.underflow[HOOK_FORWARD],
1342 handle->info.underflow[HOOK_LOCAL_OUT],
1343 handle->info.underflow[HOOK_POST_ROUTING]);
Marc Bouchere6869a82000-03-20 06:03:29 +00001344
Harald Welteaae69be2004-08-29 23:32:14 +00001345 ENTRY_ITERATE(handle->entries->entrytable, handle->entries->size,
Rusty Russell79dee072000-05-02 16:45:16 +00001346 dump_entry, handle);
Harald Welte0113fe72004-01-06 19:04:02 +00001347}
Rusty Russell30fd6e52000-04-23 09:16:06 +00001348
Marc Bouchere6869a82000-03-20 06:03:29 +00001349/* Does this chain exist? */
Rusty Russell79dee072000-05-02 16:45:16 +00001350int TC_IS_CHAIN(const char *chain, const TC_HANDLE_T handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001351{
Derrik Pates664c0a32005-02-01 13:28:14 +00001352 iptc_fn = TC_IS_CHAIN;
Harald Welteaae69be2004-08-29 23:32:14 +00001353 return iptcc_find_label(chain, handle) != NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +00001354}
1355
Harald Welteaae69be2004-08-29 23:32:14 +00001356static void iptcc_chain_iterator_advance(TC_HANDLE_T handle)
1357{
1358 struct chain_head *c = handle->chain_iterator_cur;
1359
1360 if (c->list.next == &handle->chains)
1361 handle->chain_iterator_cur = NULL;
1362 else
1363 handle->chain_iterator_cur =
1364 list_entry(c->list.next, struct chain_head, list);
1365}
Marc Bouchere6869a82000-03-20 06:03:29 +00001366
Rusty Russell30fd6e52000-04-23 09:16:06 +00001367/* Iterator functions to run through the chains. */
Marc Bouchere6869a82000-03-20 06:03:29 +00001368const char *
Philip Blundell8c700902000-05-15 02:17:52 +00001369TC_FIRST_CHAIN(TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001370{
Harald Welteaae69be2004-08-29 23:32:14 +00001371 struct chain_head *c = list_entry((*handle)->chains.next,
1372 struct chain_head, list);
1373
1374 iptc_fn = TC_FIRST_CHAIN;
1375
1376
1377 if (list_empty(&(*handle)->chains)) {
1378 DEBUGP(": no chains\n");
Harald Welte0113fe72004-01-06 19:04:02 +00001379 return NULL;
Harald Welteaae69be2004-08-29 23:32:14 +00001380 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001381
Harald Welteaae69be2004-08-29 23:32:14 +00001382 (*handle)->chain_iterator_cur = c;
1383 iptcc_chain_iterator_advance(*handle);
Harald Welte0113fe72004-01-06 19:04:02 +00001384
Harald Welteaae69be2004-08-29 23:32:14 +00001385 DEBUGP(": returning `%s'\n", c->name);
1386 return c->name;
Marc Bouchere6869a82000-03-20 06:03:29 +00001387}
1388
Rusty Russell30fd6e52000-04-23 09:16:06 +00001389/* Iterator functions to run through the chains. Returns NULL at end. */
1390const char *
Rusty Russell79dee072000-05-02 16:45:16 +00001391TC_NEXT_CHAIN(TC_HANDLE_T *handle)
Rusty Russell30fd6e52000-04-23 09:16:06 +00001392{
Harald Welteaae69be2004-08-29 23:32:14 +00001393 struct chain_head *c = (*handle)->chain_iterator_cur;
Rusty Russell30fd6e52000-04-23 09:16:06 +00001394
Harald Welteaae69be2004-08-29 23:32:14 +00001395 iptc_fn = TC_NEXT_CHAIN;
1396
1397 if (!c) {
1398 DEBUGP(": no more chains\n");
Rusty Russell30fd6e52000-04-23 09:16:06 +00001399 return NULL;
Harald Welteaae69be2004-08-29 23:32:14 +00001400 }
Rusty Russell30fd6e52000-04-23 09:16:06 +00001401
Harald Welteaae69be2004-08-29 23:32:14 +00001402 iptcc_chain_iterator_advance(*handle);
1403
1404 DEBUGP(": returning `%s'\n", c->name);
1405 return c->name;
Rusty Russell30fd6e52000-04-23 09:16:06 +00001406}
1407
1408/* Get first rule in the given chain: NULL for empty chain. */
Rusty Russell79dee072000-05-02 16:45:16 +00001409const STRUCT_ENTRY *
Philip Blundell8c700902000-05-15 02:17:52 +00001410TC_FIRST_RULE(const char *chain, TC_HANDLE_T *handle)
Rusty Russell30fd6e52000-04-23 09:16:06 +00001411{
Harald Welteaae69be2004-08-29 23:32:14 +00001412 struct chain_head *c;
1413 struct rule_head *r;
Rusty Russell30fd6e52000-04-23 09:16:06 +00001414
Harald Welteaae69be2004-08-29 23:32:14 +00001415 iptc_fn = TC_FIRST_RULE;
1416
1417 DEBUGP("first rule(%s): ", chain);
1418
1419 c = iptcc_find_label(chain, *handle);
Rusty Russell30fd6e52000-04-23 09:16:06 +00001420 if (!c) {
1421 errno = ENOENT;
1422 return NULL;
1423 }
1424
1425 /* Empty chain: single return/policy rule */
Harald Welteaae69be2004-08-29 23:32:14 +00001426 if (list_empty(&c->rules)) {
1427 DEBUGP_C("no rules, returning NULL\n");
Rusty Russell30fd6e52000-04-23 09:16:06 +00001428 return NULL;
Harald Welteaae69be2004-08-29 23:32:14 +00001429 }
Rusty Russell30fd6e52000-04-23 09:16:06 +00001430
Harald Welteaae69be2004-08-29 23:32:14 +00001431 r = list_entry(c->rules.next, struct rule_head, list);
1432 (*handle)->rule_iterator_cur = r;
1433 DEBUGP_C("%p\n", r);
1434
1435 return r->entry;
Rusty Russell30fd6e52000-04-23 09:16:06 +00001436}
1437
1438/* Returns NULL when rules run out. */
Rusty Russell79dee072000-05-02 16:45:16 +00001439const STRUCT_ENTRY *
Philip Blundell8c700902000-05-15 02:17:52 +00001440TC_NEXT_RULE(const STRUCT_ENTRY *prev, TC_HANDLE_T *handle)
Rusty Russell30fd6e52000-04-23 09:16:06 +00001441{
Harald Welteaae69be2004-08-29 23:32:14 +00001442 struct rule_head *r;
Rusty Russell30fd6e52000-04-23 09:16:06 +00001443
Derrik Pates664c0a32005-02-01 13:28:14 +00001444 iptc_fn = TC_NEXT_RULE;
Harald Welteaae69be2004-08-29 23:32:14 +00001445 DEBUGP("rule_iterator_cur=%p...", (*handle)->rule_iterator_cur);
1446
1447 if (!(*handle)->rule_iterator_cur) {
1448 DEBUGP_C("returning NULL\n");
1449 return NULL;
1450 }
1451
1452 r = list_entry((*handle)->rule_iterator_cur->list.next,
1453 struct rule_head, list);
1454
1455 iptc_fn = TC_NEXT_RULE;
1456
1457 DEBUGP_C("next=%p, head=%p...", &r->list,
1458 &(*handle)->rule_iterator_cur->chain->rules);
1459
1460 if (&r->list == &(*handle)->rule_iterator_cur->chain->rules) {
1461 (*handle)->rule_iterator_cur = NULL;
1462 DEBUGP_C("finished, returning NULL\n");
1463 return NULL;
1464 }
1465
1466 (*handle)->rule_iterator_cur = r;
1467
1468 /* NOTE: prev is without any influence ! */
1469 DEBUGP_C("returning rule %p\n", r);
1470 return r->entry;
Rusty Russell30fd6e52000-04-23 09:16:06 +00001471}
1472
Marc Bouchere6869a82000-03-20 06:03:29 +00001473/* How many rules in this chain? */
Jan Engelhardt33690a12008-02-11 00:54:00 +01001474static unsigned int
Rusty Russell79dee072000-05-02 16:45:16 +00001475TC_NUM_RULES(const char *chain, TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001476{
Harald Welteaae69be2004-08-29 23:32:14 +00001477 struct chain_head *c;
1478 iptc_fn = TC_NUM_RULES;
Marc Bouchere6869a82000-03-20 06:03:29 +00001479 CHECK(*handle);
Harald Welteaae69be2004-08-29 23:32:14 +00001480
1481 c = iptcc_find_label(chain, *handle);
1482 if (!c) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001483 errno = ENOENT;
1484 return (unsigned int)-1;
1485 }
Harald Welteaae69be2004-08-29 23:32:14 +00001486
1487 return c->num_rules;
Marc Bouchere6869a82000-03-20 06:03:29 +00001488}
1489
Jan Engelhardt33690a12008-02-11 00:54:00 +01001490static const STRUCT_ENTRY *
1491TC_GET_RULE(const char *chain, unsigned int n, TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001492{
Harald Welteaae69be2004-08-29 23:32:14 +00001493 struct chain_head *c;
1494 struct rule_head *r;
1495
1496 iptc_fn = TC_GET_RULE;
Marc Bouchere6869a82000-03-20 06:03:29 +00001497
1498 CHECK(*handle);
Harald Welteaae69be2004-08-29 23:32:14 +00001499
1500 c = iptcc_find_label(chain, *handle);
1501 if (!c) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001502 errno = ENOENT;
1503 return NULL;
1504 }
1505
Harald Welteaae69be2004-08-29 23:32:14 +00001506 r = iptcc_get_rule_num(c, n);
1507 if (!r)
1508 return NULL;
1509 return r->entry;
Marc Bouchere6869a82000-03-20 06:03:29 +00001510}
1511
1512/* Returns a pointer to the target name of this position. */
Jan Engelhardt33690a12008-02-11 00:54:00 +01001513static const char *standard_target_map(int verdict)
Marc Bouchere6869a82000-03-20 06:03:29 +00001514{
Harald Welteaae69be2004-08-29 23:32:14 +00001515 switch (verdict) {
1516 case RETURN:
1517 return LABEL_RETURN;
1518 break;
1519 case -NF_ACCEPT-1:
1520 return LABEL_ACCEPT;
1521 break;
1522 case -NF_DROP-1:
1523 return LABEL_DROP;
1524 break;
1525 case -NF_QUEUE-1:
1526 return LABEL_QUEUE;
1527 break;
1528 default:
1529 fprintf(stderr, "ERROR: %d not a valid target)\n",
1530 verdict);
1531 abort();
1532 break;
1533 }
1534 /* not reached */
1535 return NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +00001536}
1537
Harald Welteaae69be2004-08-29 23:32:14 +00001538/* Returns a pointer to the target name of this position. */
1539const char *TC_GET_TARGET(const STRUCT_ENTRY *ce,
1540 TC_HANDLE_T *handle)
1541{
1542 STRUCT_ENTRY *e = (STRUCT_ENTRY *)ce;
Rusty Russelle45c7132004-12-16 13:21:44 +00001543 struct rule_head *r = container_of(e, struct rule_head, entry[0]);
Harald Welteaae69be2004-08-29 23:32:14 +00001544
1545 iptc_fn = TC_GET_TARGET;
1546
1547 switch(r->type) {
1548 int spos;
1549 case IPTCC_R_FALLTHROUGH:
1550 return "";
1551 break;
1552 case IPTCC_R_JUMP:
1553 DEBUGP("r=%p, jump=%p, name=`%s'\n", r, r->jump, r->jump->name);
1554 return r->jump->name;
1555 break;
1556 case IPTCC_R_STANDARD:
1557 spos = *(int *)GET_TARGET(e)->data;
1558 DEBUGP("r=%p, spos=%d'\n", r, spos);
1559 return standard_target_map(spos);
1560 break;
1561 case IPTCC_R_MODULE:
1562 return GET_TARGET(e)->u.user.name;
1563 break;
1564 }
1565 return NULL;
1566}
Marc Bouchere6869a82000-03-20 06:03:29 +00001567/* Is this a built-in chain? Actually returns hook + 1. */
1568int
Rusty Russell79dee072000-05-02 16:45:16 +00001569TC_BUILTIN(const char *chain, const TC_HANDLE_T handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001570{
Harald Welteaae69be2004-08-29 23:32:14 +00001571 struct chain_head *c;
1572
1573 iptc_fn = TC_BUILTIN;
Marc Bouchere6869a82000-03-20 06:03:29 +00001574
Harald Welteaae69be2004-08-29 23:32:14 +00001575 c = iptcc_find_label(chain, handle);
1576 if (!c) {
1577 errno = ENOENT;
Martin Josefssonb0f3d2d2004-09-23 18:23:20 +00001578 return 0;
Marc Bouchere6869a82000-03-20 06:03:29 +00001579 }
Harald Welteaae69be2004-08-29 23:32:14 +00001580
1581 return iptcc_is_builtin(c);
Marc Bouchere6869a82000-03-20 06:03:29 +00001582}
1583
1584/* Get the policy of a given built-in chain */
1585const char *
Rusty Russell79dee072000-05-02 16:45:16 +00001586TC_GET_POLICY(const char *chain,
1587 STRUCT_COUNTERS *counters,
1588 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001589{
Harald Welteaae69be2004-08-29 23:32:14 +00001590 struct chain_head *c;
Marc Bouchere6869a82000-03-20 06:03:29 +00001591
Harald Welteaae69be2004-08-29 23:32:14 +00001592 iptc_fn = TC_GET_POLICY;
1593
1594 DEBUGP("called for chain %s\n", chain);
1595
1596 c = iptcc_find_label(chain, *handle);
1597 if (!c) {
1598 errno = ENOENT;
1599 return NULL;
1600 }
1601
1602 if (!iptcc_is_builtin(c))
Marc Bouchere6869a82000-03-20 06:03:29 +00001603 return NULL;
1604
Harald Welteaae69be2004-08-29 23:32:14 +00001605 *counters = c->counters;
Marc Bouchere6869a82000-03-20 06:03:29 +00001606
Harald Welteaae69be2004-08-29 23:32:14 +00001607 return standard_target_map(c->verdict);
Harald Welte0113fe72004-01-06 19:04:02 +00001608}
1609
1610static int
Harald Welteaae69be2004-08-29 23:32:14 +00001611iptcc_standard_map(struct rule_head *r, int verdict)
Harald Welte0113fe72004-01-06 19:04:02 +00001612{
Harald Welteaae69be2004-08-29 23:32:14 +00001613 STRUCT_ENTRY *e = r->entry;
Rusty Russell79dee072000-05-02 16:45:16 +00001614 STRUCT_STANDARD_TARGET *t;
Marc Bouchere6869a82000-03-20 06:03:29 +00001615
Rusty Russell79dee072000-05-02 16:45:16 +00001616 t = (STRUCT_STANDARD_TARGET *)GET_TARGET(e);
Marc Bouchere6869a82000-03-20 06:03:29 +00001617
Rusty Russell67088e72000-05-10 01:18:57 +00001618 if (t->target.u.target_size
Philip Blundell8c700902000-05-15 02:17:52 +00001619 != ALIGN(sizeof(STRUCT_STANDARD_TARGET))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001620 errno = EINVAL;
1621 return 0;
1622 }
1623 /* memset for memcmp convenience on delete/replace */
Rusty Russell79dee072000-05-02 16:45:16 +00001624 memset(t->target.u.user.name, 0, FUNCTION_MAXNAMELEN);
1625 strcpy(t->target.u.user.name, STANDARD_TARGET);
Marc Bouchere6869a82000-03-20 06:03:29 +00001626 t->verdict = verdict;
1627
Harald Welteaae69be2004-08-29 23:32:14 +00001628 r->type = IPTCC_R_STANDARD;
1629
Marc Bouchere6869a82000-03-20 06:03:29 +00001630 return 1;
1631}
Rusty Russell7e53bf92000-03-20 07:03:28 +00001632
Marc Bouchere6869a82000-03-20 06:03:29 +00001633static int
Harald Welteaae69be2004-08-29 23:32:14 +00001634iptcc_map_target(const TC_HANDLE_T handle,
1635 struct rule_head *r)
Marc Bouchere6869a82000-03-20 06:03:29 +00001636{
Harald Welteaae69be2004-08-29 23:32:14 +00001637 STRUCT_ENTRY *e = r->entry;
Harald Welte0113fe72004-01-06 19:04:02 +00001638 STRUCT_ENTRY_TARGET *t = GET_TARGET(e);
Marc Bouchere6869a82000-03-20 06:03:29 +00001639
Marc Bouchere6869a82000-03-20 06:03:29 +00001640 /* Maybe it's empty (=> fall through) */
Harald Welteaae69be2004-08-29 23:32:14 +00001641 if (strcmp(t->u.user.name, "") == 0) {
1642 r->type = IPTCC_R_FALLTHROUGH;
1643 return 1;
1644 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001645 /* Maybe it's a standard target name... */
Rusty Russell79dee072000-05-02 16:45:16 +00001646 else if (strcmp(t->u.user.name, LABEL_ACCEPT) == 0)
Harald Welteaae69be2004-08-29 23:32:14 +00001647 return iptcc_standard_map(r, -NF_ACCEPT - 1);
Rusty Russell79dee072000-05-02 16:45:16 +00001648 else if (strcmp(t->u.user.name, LABEL_DROP) == 0)
Harald Welteaae69be2004-08-29 23:32:14 +00001649 return iptcc_standard_map(r, -NF_DROP - 1);
Rusty Russell67088e72000-05-10 01:18:57 +00001650 else if (strcmp(t->u.user.name, LABEL_QUEUE) == 0)
Harald Welteaae69be2004-08-29 23:32:14 +00001651 return iptcc_standard_map(r, -NF_QUEUE - 1);
Rusty Russell79dee072000-05-02 16:45:16 +00001652 else if (strcmp(t->u.user.name, LABEL_RETURN) == 0)
Harald Welteaae69be2004-08-29 23:32:14 +00001653 return iptcc_standard_map(r, RETURN);
Rusty Russell79dee072000-05-02 16:45:16 +00001654 else if (TC_BUILTIN(t->u.user.name, handle)) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001655 /* Can't jump to builtins. */
1656 errno = EINVAL;
1657 return 0;
1658 } else {
1659 /* Maybe it's an existing chain name. */
Harald Welteaae69be2004-08-29 23:32:14 +00001660 struct chain_head *c;
1661 DEBUGP("trying to find chain `%s': ", t->u.user.name);
Marc Bouchere6869a82000-03-20 06:03:29 +00001662
Harald Welteaae69be2004-08-29 23:32:14 +00001663 c = iptcc_find_label(t->u.user.name, handle);
1664 if (c) {
1665 DEBUGP_C("found!\n");
1666 r->type = IPTCC_R_JUMP;
1667 r->jump = c;
1668 c->references++;
1669 return 1;
1670 }
1671 DEBUGP_C("not found :(\n");
Marc Bouchere6869a82000-03-20 06:03:29 +00001672 }
1673
1674 /* Must be a module? If not, kernel will reject... */
Rusty Russell3aef54d2005-01-03 03:48:40 +00001675 /* memset to all 0 for your memcmp convenience: don't clear version */
Rusty Russell228e98d2000-04-27 10:28:06 +00001676 memset(t->u.user.name + strlen(t->u.user.name),
Marc Bouchere6869a82000-03-20 06:03:29 +00001677 0,
Rusty Russell3aef54d2005-01-03 03:48:40 +00001678 FUNCTION_MAXNAMELEN - 1 - strlen(t->u.user.name));
Rusty Russell733e54b2004-12-16 14:22:23 +00001679 r->type = IPTCC_R_MODULE;
Harald Welteaae69be2004-08-29 23:32:14 +00001680 set_changed(handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001681 return 1;
1682}
1683
Harald Welte0113fe72004-01-06 19:04:02 +00001684/* Insert the entry `fw' in chain `chain' into position `rulenum'. */
Marc Bouchere6869a82000-03-20 06:03:29 +00001685int
Rusty Russell79dee072000-05-02 16:45:16 +00001686TC_INSERT_ENTRY(const IPT_CHAINLABEL chain,
1687 const STRUCT_ENTRY *e,
1688 unsigned int rulenum,
1689 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001690{
Harald Welteaae69be2004-08-29 23:32:14 +00001691 struct chain_head *c;
Martin Josefssoneb066cc2004-09-22 21:04:07 +00001692 struct rule_head *r;
1693 struct list_head *prev;
Marc Bouchere6869a82000-03-20 06:03:29 +00001694
Rusty Russell79dee072000-05-02 16:45:16 +00001695 iptc_fn = TC_INSERT_ENTRY;
Harald Welteaae69be2004-08-29 23:32:14 +00001696
1697 if (!(c = iptcc_find_label(chain, *handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001698 errno = ENOENT;
1699 return 0;
1700 }
1701
Martin Josefssoneb066cc2004-09-22 21:04:07 +00001702 /* first rulenum index = 0
1703 first c->num_rules index = 1 */
1704 if (rulenum > c->num_rules) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001705 errno = E2BIG;
1706 return 0;
1707 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001708
Martin Josefsson631f3612004-09-23 19:25:06 +00001709 /* If we are inserting at the end just take advantage of the
1710 double linked list, insert will happen before the entry
1711 prev points to. */
1712 if (rulenum == c->num_rules) {
Martin Josefssoneb066cc2004-09-22 21:04:07 +00001713 prev = &c->rules;
Martin Josefssona5616dc2004-10-24 22:27:31 +00001714 } else if (rulenum + 1 <= c->num_rules/2) {
Martin Josefsson631f3612004-09-23 19:25:06 +00001715 r = iptcc_get_rule_num(c, rulenum + 1);
Martin Josefssona5616dc2004-10-24 22:27:31 +00001716 prev = &r->list;
1717 } else {
1718 r = iptcc_get_rule_num_reverse(c, c->num_rules - rulenum);
Martin Josefsson631f3612004-09-23 19:25:06 +00001719 prev = &r->list;
1720 }
Martin Josefssoneb066cc2004-09-22 21:04:07 +00001721
Harald Welteaae69be2004-08-29 23:32:14 +00001722 if (!(r = iptcc_alloc_rule(c, e->next_offset))) {
1723 errno = ENOMEM;
Harald Welte0113fe72004-01-06 19:04:02 +00001724 return 0;
Harald Welteaae69be2004-08-29 23:32:14 +00001725 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001726
Harald Welteaae69be2004-08-29 23:32:14 +00001727 memcpy(r->entry, e, e->next_offset);
1728 r->counter_map.maptype = COUNTER_MAP_SET;
1729
1730 if (!iptcc_map_target(*handle, r)) {
1731 free(r);
1732 return 0;
1733 }
1734
Martin Josefssoneb066cc2004-09-22 21:04:07 +00001735 list_add_tail(&r->list, prev);
Harald Welteaae69be2004-08-29 23:32:14 +00001736 c->num_rules++;
1737
1738 set_changed(*handle);
1739
1740 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00001741}
1742
1743/* Atomically replace rule `rulenum' in `chain' with `fw'. */
1744int
Rusty Russell79dee072000-05-02 16:45:16 +00001745TC_REPLACE_ENTRY(const IPT_CHAINLABEL chain,
1746 const STRUCT_ENTRY *e,
1747 unsigned int rulenum,
1748 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001749{
Harald Welteaae69be2004-08-29 23:32:14 +00001750 struct chain_head *c;
1751 struct rule_head *r, *old;
Marc Bouchere6869a82000-03-20 06:03:29 +00001752
Rusty Russell79dee072000-05-02 16:45:16 +00001753 iptc_fn = TC_REPLACE_ENTRY;
Marc Bouchere6869a82000-03-20 06:03:29 +00001754
Harald Welteaae69be2004-08-29 23:32:14 +00001755 if (!(c = iptcc_find_label(chain, *handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001756 errno = ENOENT;
1757 return 0;
1758 }
1759
Martin Josefsson0f9b8b12004-12-18 17:18:49 +00001760 if (rulenum >= c->num_rules) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001761 errno = E2BIG;
1762 return 0;
1763 }
1764
Martin Josefsson0f9b8b12004-12-18 17:18:49 +00001765 /* Take advantage of the double linked list if possible. */
1766 if (rulenum + 1 <= c->num_rules/2) {
1767 old = iptcc_get_rule_num(c, rulenum + 1);
1768 } else {
1769 old = iptcc_get_rule_num_reverse(c, c->num_rules - rulenum);
1770 }
1771
Harald Welteaae69be2004-08-29 23:32:14 +00001772 if (!(r = iptcc_alloc_rule(c, e->next_offset))) {
1773 errno = ENOMEM;
Marc Bouchere6869a82000-03-20 06:03:29 +00001774 return 0;
Harald Welteaae69be2004-08-29 23:32:14 +00001775 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001776
Harald Welteaae69be2004-08-29 23:32:14 +00001777 memcpy(r->entry, e, e->next_offset);
1778 r->counter_map.maptype = COUNTER_MAP_SET;
1779
1780 if (!iptcc_map_target(*handle, r)) {
1781 free(r);
Harald Welte0113fe72004-01-06 19:04:02 +00001782 return 0;
Harald Welteaae69be2004-08-29 23:32:14 +00001783 }
Harald Welte0113fe72004-01-06 19:04:02 +00001784
Harald Welteaae69be2004-08-29 23:32:14 +00001785 list_add(&r->list, &old->list);
1786 iptcc_delete_rule(old);
1787
1788 set_changed(*handle);
1789
1790 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00001791}
1792
Harald Welte0113fe72004-01-06 19:04:02 +00001793/* Append entry `fw' to chain `chain'. Equivalent to insert with
Marc Bouchere6869a82000-03-20 06:03:29 +00001794 rulenum = length of chain. */
1795int
Rusty Russell79dee072000-05-02 16:45:16 +00001796TC_APPEND_ENTRY(const IPT_CHAINLABEL chain,
1797 const STRUCT_ENTRY *e,
1798 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001799{
Harald Welteaae69be2004-08-29 23:32:14 +00001800 struct chain_head *c;
1801 struct rule_head *r;
Marc Bouchere6869a82000-03-20 06:03:29 +00001802
Rusty Russell79dee072000-05-02 16:45:16 +00001803 iptc_fn = TC_APPEND_ENTRY;
Harald Welteaae69be2004-08-29 23:32:14 +00001804 if (!(c = iptcc_find_label(chain, *handle))) {
1805 DEBUGP("unable to find chain `%s'\n", chain);
Marc Bouchere6869a82000-03-20 06:03:29 +00001806 errno = ENOENT;
1807 return 0;
1808 }
1809
Harald Welteaae69be2004-08-29 23:32:14 +00001810 if (!(r = iptcc_alloc_rule(c, e->next_offset))) {
1811 DEBUGP("unable to allocate rule for chain `%s'\n", chain);
1812 errno = ENOMEM;
Harald Welte0113fe72004-01-06 19:04:02 +00001813 return 0;
Harald Welteaae69be2004-08-29 23:32:14 +00001814 }
Harald Welte0113fe72004-01-06 19:04:02 +00001815
Harald Welteaae69be2004-08-29 23:32:14 +00001816 memcpy(r->entry, e, e->next_offset);
1817 r->counter_map.maptype = COUNTER_MAP_SET;
1818
1819 if (!iptcc_map_target(*handle, r)) {
Martin Josefsson12009532004-09-23 18:24:29 +00001820 DEBUGP("unable to map target of rule for chain `%s'\n", chain);
Harald Welteaae69be2004-08-29 23:32:14 +00001821 free(r);
1822 return 0;
1823 }
1824
1825 list_add_tail(&r->list, &c->rules);
1826 c->num_rules++;
1827
1828 set_changed(*handle);
1829
1830 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00001831}
1832
1833static inline int
Rusty Russell79dee072000-05-02 16:45:16 +00001834match_different(const STRUCT_ENTRY_MATCH *a,
Rusty Russelledf14cf2000-04-19 11:26:44 +00001835 const unsigned char *a_elems,
1836 const unsigned char *b_elems,
1837 unsigned char **maskptr)
Marc Bouchere6869a82000-03-20 06:03:29 +00001838{
Rusty Russell79dee072000-05-02 16:45:16 +00001839 const STRUCT_ENTRY_MATCH *b;
Rusty Russelledf14cf2000-04-19 11:26:44 +00001840 unsigned int i;
Marc Bouchere6869a82000-03-20 06:03:29 +00001841
1842 /* Offset of b is the same as a. */
Rusty Russell30fd6e52000-04-23 09:16:06 +00001843 b = (void *)b_elems + ((unsigned char *)a - a_elems);
Marc Bouchere6869a82000-03-20 06:03:29 +00001844
Rusty Russell228e98d2000-04-27 10:28:06 +00001845 if (a->u.match_size != b->u.match_size)
Marc Bouchere6869a82000-03-20 06:03:29 +00001846 return 1;
1847
Rusty Russell228e98d2000-04-27 10:28:06 +00001848 if (strcmp(a->u.user.name, b->u.user.name) != 0)
Marc Bouchere6869a82000-03-20 06:03:29 +00001849 return 1;
1850
Rusty Russell73ef09b2000-07-03 10:24:04 +00001851 *maskptr += ALIGN(sizeof(*a));
Rusty Russelledf14cf2000-04-19 11:26:44 +00001852
Rusty Russell73ef09b2000-07-03 10:24:04 +00001853 for (i = 0; i < a->u.match_size - ALIGN(sizeof(*a)); i++)
Rusty Russelledf14cf2000-04-19 11:26:44 +00001854 if (((a->data[i] ^ b->data[i]) & (*maskptr)[i]) != 0)
Rusty Russell90e712a2000-03-29 04:19:26 +00001855 return 1;
Rusty Russelledf14cf2000-04-19 11:26:44 +00001856 *maskptr += i;
1857 return 0;
1858}
1859
1860static inline int
Rusty Russell733e54b2004-12-16 14:22:23 +00001861target_same(struct rule_head *a, struct rule_head *b,const unsigned char *mask)
Rusty Russelledf14cf2000-04-19 11:26:44 +00001862{
1863 unsigned int i;
Rusty Russell733e54b2004-12-16 14:22:23 +00001864 STRUCT_ENTRY_TARGET *ta, *tb;
Marc Bouchere6869a82000-03-20 06:03:29 +00001865
Rusty Russell733e54b2004-12-16 14:22:23 +00001866 if (a->type != b->type)
1867 return 0;
1868
1869 ta = GET_TARGET(a->entry);
1870 tb = GET_TARGET(b->entry);
1871
1872 switch (a->type) {
1873 case IPTCC_R_FALLTHROUGH:
1874 return 1;
1875 case IPTCC_R_JUMP:
1876 return a->jump == b->jump;
1877 case IPTCC_R_STANDARD:
1878 return ((STRUCT_STANDARD_TARGET *)ta)->verdict
1879 == ((STRUCT_STANDARD_TARGET *)tb)->verdict;
1880 case IPTCC_R_MODULE:
1881 if (ta->u.target_size != tb->u.target_size)
1882 return 0;
1883 if (strcmp(ta->u.user.name, tb->u.user.name) != 0)
1884 return 0;
1885
1886 for (i = 0; i < ta->u.target_size - sizeof(*ta); i++)
Rusty Russelldaade442004-12-29 11:14:52 +00001887 if (((ta->data[i] ^ tb->data[i]) & mask[i]) != 0)
Rusty Russell733e54b2004-12-16 14:22:23 +00001888 return 0;
1889 return 1;
1890 default:
1891 fprintf(stderr, "ERROR: bad type %i\n", a->type);
1892 abort();
1893 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001894}
1895
Rusty Russell733e54b2004-12-16 14:22:23 +00001896static unsigned char *
Rusty Russell79dee072000-05-02 16:45:16 +00001897is_same(const STRUCT_ENTRY *a,
1898 const STRUCT_ENTRY *b,
1899 unsigned char *matchmask);
Marc Bouchere6869a82000-03-20 06:03:29 +00001900
Harald Welte0113fe72004-01-06 19:04:02 +00001901/* Delete the first rule in `chain' which matches `fw'. */
Marc Bouchere6869a82000-03-20 06:03:29 +00001902int
Rusty Russell79dee072000-05-02 16:45:16 +00001903TC_DELETE_ENTRY(const IPT_CHAINLABEL chain,
1904 const STRUCT_ENTRY *origfw,
1905 unsigned char *matchmask,
1906 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001907{
Harald Welteaae69be2004-08-29 23:32:14 +00001908 struct chain_head *c;
Rusty Russelle45c7132004-12-16 13:21:44 +00001909 struct rule_head *r, *i;
Marc Bouchere6869a82000-03-20 06:03:29 +00001910
Rusty Russell79dee072000-05-02 16:45:16 +00001911 iptc_fn = TC_DELETE_ENTRY;
Harald Welteaae69be2004-08-29 23:32:14 +00001912 if (!(c = iptcc_find_label(chain, *handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001913 errno = ENOENT;
1914 return 0;
1915 }
1916
Rusty Russelle45c7132004-12-16 13:21:44 +00001917 /* Create a rule_head from origfw. */
1918 r = iptcc_alloc_rule(c, origfw->next_offset);
1919 if (!r) {
Harald Welte0113fe72004-01-06 19:04:02 +00001920 errno = ENOMEM;
1921 return 0;
1922 }
1923
Rusty Russelle45c7132004-12-16 13:21:44 +00001924 memcpy(r->entry, origfw, origfw->next_offset);
1925 r->counter_map.maptype = COUNTER_MAP_NOMAP;
1926 if (!iptcc_map_target(*handle, r)) {
1927 DEBUGP("unable to map target of rule for chain `%s'\n", chain);
1928 free(r);
1929 return 0;
Patrick McHardyJesper Brouer04a1e4c2006-07-25 01:50:48 +00001930 } else {
1931 /* iptcc_map_target increment target chain references
1932 * since this is a fake rule only used for matching
1933 * the chain references count is decremented again.
1934 */
1935 if (r->type == IPTCC_R_JUMP
1936 && r->jump)
1937 r->jump->references--;
Rusty Russelle45c7132004-12-16 13:21:44 +00001938 }
Harald Welte0113fe72004-01-06 19:04:02 +00001939
Rusty Russelle45c7132004-12-16 13:21:44 +00001940 list_for_each_entry(i, &c->rules, list) {
Rusty Russell733e54b2004-12-16 14:22:23 +00001941 unsigned char *mask;
Harald Weltefe537072004-08-30 20:28:53 +00001942
Rusty Russell733e54b2004-12-16 14:22:23 +00001943 mask = is_same(r->entry, i->entry, matchmask);
1944 if (!mask)
1945 continue;
Martin Josefsson2a5dbbb2004-09-22 21:37:41 +00001946
Rusty Russell733e54b2004-12-16 14:22:23 +00001947 if (!target_same(r, i, mask))
1948 continue;
1949
1950 /* If we are about to delete the rule that is the
1951 * current iterator, move rule iterator back. next
1952 * pointer will then point to real next node */
1953 if (i == (*handle)->rule_iterator_cur) {
1954 (*handle)->rule_iterator_cur =
1955 list_entry((*handle)->rule_iterator_cur->list.prev,
1956 struct rule_head, list);
Marc Bouchere6869a82000-03-20 06:03:29 +00001957 }
Rusty Russell733e54b2004-12-16 14:22:23 +00001958
1959 c->num_rules--;
1960 iptcc_delete_rule(i);
1961
1962 set_changed(*handle);
1963 free(r);
1964 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00001965 }
1966
Rusty Russelle45c7132004-12-16 13:21:44 +00001967 free(r);
Marc Bouchere6869a82000-03-20 06:03:29 +00001968 errno = ENOENT;
1969 return 0;
Rusty Russell7e53bf92000-03-20 07:03:28 +00001970}
Harald Welteaae69be2004-08-29 23:32:14 +00001971
Marc Bouchere6869a82000-03-20 06:03:29 +00001972
1973/* Delete the rule in position `rulenum' in `chain'. */
1974int
Rusty Russell79dee072000-05-02 16:45:16 +00001975TC_DELETE_NUM_ENTRY(const IPT_CHAINLABEL chain,
1976 unsigned int rulenum,
1977 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001978{
Harald Welteaae69be2004-08-29 23:32:14 +00001979 struct chain_head *c;
1980 struct rule_head *r;
Marc Bouchere6869a82000-03-20 06:03:29 +00001981
Rusty Russell79dee072000-05-02 16:45:16 +00001982 iptc_fn = TC_DELETE_NUM_ENTRY;
Harald Welteaae69be2004-08-29 23:32:14 +00001983
1984 if (!(c = iptcc_find_label(chain, *handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001985 errno = ENOENT;
1986 return 0;
1987 }
1988
Martin Josefssona5616dc2004-10-24 22:27:31 +00001989 if (rulenum >= c->num_rules) {
Martin Josefsson631f3612004-09-23 19:25:06 +00001990 errno = E2BIG;
1991 return 0;
1992 }
1993
1994 /* Take advantage of the double linked list if possible. */
Martin Josefssona5616dc2004-10-24 22:27:31 +00001995 if (rulenum + 1 <= c->num_rules/2) {
1996 r = iptcc_get_rule_num(c, rulenum + 1);
1997 } else {
1998 r = iptcc_get_rule_num_reverse(c, c->num_rules - rulenum);
Marc Bouchere6869a82000-03-20 06:03:29 +00001999 }
2000
Harald Welteaae69be2004-08-29 23:32:14 +00002001 /* If we are about to delete the rule that is the current
2002 * iterator, move rule iterator back. next pointer will then
2003 * point to real next node */
2004 if (r == (*handle)->rule_iterator_cur) {
2005 (*handle)->rule_iterator_cur =
2006 list_entry((*handle)->rule_iterator_cur->list.prev,
2007 struct rule_head, list);
Harald Welte0113fe72004-01-06 19:04:02 +00002008 }
Marc Bouchere6869a82000-03-20 06:03:29 +00002009
Harald Welteaae69be2004-08-29 23:32:14 +00002010 c->num_rules--;
2011 iptcc_delete_rule(r);
2012
Martin Josefsson2a5dbbb2004-09-22 21:37:41 +00002013 set_changed(*handle);
2014
Harald Welteaae69be2004-08-29 23:32:14 +00002015 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00002016}
2017
2018/* Check the packet `fw' on chain `chain'. Returns the verdict, or
2019 NULL and sets errno. */
2020const char *
Rusty Russell79dee072000-05-02 16:45:16 +00002021TC_CHECK_PACKET(const IPT_CHAINLABEL chain,
2022 STRUCT_ENTRY *entry,
2023 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00002024{
Derrik Pates664c0a32005-02-01 13:28:14 +00002025 iptc_fn = TC_CHECK_PACKET;
Marc Bouchere6869a82000-03-20 06:03:29 +00002026 errno = ENOSYS;
2027 return NULL;
2028}
2029
2030/* Flushes the entries in the given chain (ie. empties chain). */
2031int
Rusty Russell79dee072000-05-02 16:45:16 +00002032TC_FLUSH_ENTRIES(const IPT_CHAINLABEL chain, TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00002033{
Harald Welteaae69be2004-08-29 23:32:14 +00002034 struct chain_head *c;
2035 struct rule_head *r, *tmp;
Marc Bouchere6869a82000-03-20 06:03:29 +00002036
Harald Welte0113fe72004-01-06 19:04:02 +00002037 iptc_fn = TC_FLUSH_ENTRIES;
Harald Welteaae69be2004-08-29 23:32:14 +00002038 if (!(c = iptcc_find_label(chain, *handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002039 errno = ENOENT;
2040 return 0;
2041 }
Marc Bouchere6869a82000-03-20 06:03:29 +00002042
Harald Welteaae69be2004-08-29 23:32:14 +00002043 list_for_each_entry_safe(r, tmp, &c->rules, list) {
2044 iptcc_delete_rule(r);
2045 }
2046
2047 c->num_rules = 0;
2048
2049 set_changed(*handle);
2050
2051 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00002052}
2053
2054/* Zeroes the counters in a chain. */
2055int
Rusty Russell79dee072000-05-02 16:45:16 +00002056TC_ZERO_ENTRIES(const IPT_CHAINLABEL chain, TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00002057{
Harald Welteaae69be2004-08-29 23:32:14 +00002058 struct chain_head *c;
2059 struct rule_head *r;
Rusty Russell7e53bf92000-03-20 07:03:28 +00002060
Derrik Pates664c0a32005-02-01 13:28:14 +00002061 iptc_fn = TC_ZERO_ENTRIES;
Harald Welteaae69be2004-08-29 23:32:14 +00002062 if (!(c = iptcc_find_label(chain, *handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002063 errno = ENOENT;
2064 return 0;
2065 }
Marc Bouchere6869a82000-03-20 06:03:29 +00002066
Andy Gaye5bd1d72006-08-22 02:56:41 +00002067 if (c->counter_map.maptype == COUNTER_MAP_NORMAL_MAP)
2068 c->counter_map.maptype = COUNTER_MAP_ZEROED;
2069
Harald Welteaae69be2004-08-29 23:32:14 +00002070 list_for_each_entry(r, &c->rules, list) {
2071 if (r->counter_map.maptype == COUNTER_MAP_NORMAL_MAP)
2072 r->counter_map.maptype = COUNTER_MAP_ZEROED;
Marc Bouchere6869a82000-03-20 06:03:29 +00002073 }
Harald Welteaae69be2004-08-29 23:32:14 +00002074
Rusty Russell175f6412000-03-24 09:32:20 +00002075 set_changed(*handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00002076
Marc Bouchere6869a82000-03-20 06:03:29 +00002077 return 1;
2078}
2079
Harald Welte1cef74d2001-01-05 15:22:59 +00002080STRUCT_COUNTERS *
2081TC_READ_COUNTER(const IPT_CHAINLABEL chain,
2082 unsigned int rulenum,
2083 TC_HANDLE_T *handle)
2084{
Harald Welteaae69be2004-08-29 23:32:14 +00002085 struct chain_head *c;
2086 struct rule_head *r;
Harald Welte1cef74d2001-01-05 15:22:59 +00002087
2088 iptc_fn = TC_READ_COUNTER;
2089 CHECK(*handle);
2090
Harald Welteaae69be2004-08-29 23:32:14 +00002091 if (!(c = iptcc_find_label(chain, *handle))) {
Harald Welte1cef74d2001-01-05 15:22:59 +00002092 errno = ENOENT;
2093 return NULL;
2094 }
2095
Harald Welteaae69be2004-08-29 23:32:14 +00002096 if (!(r = iptcc_get_rule_num(c, rulenum))) {
Harald Welte0113fe72004-01-06 19:04:02 +00002097 errno = E2BIG;
2098 return NULL;
2099 }
2100
Harald Welteaae69be2004-08-29 23:32:14 +00002101 return &r->entry[0].counters;
Harald Welte1cef74d2001-01-05 15:22:59 +00002102}
2103
2104int
2105TC_ZERO_COUNTER(const IPT_CHAINLABEL chain,
2106 unsigned int rulenum,
2107 TC_HANDLE_T *handle)
2108{
Harald Welteaae69be2004-08-29 23:32:14 +00002109 struct chain_head *c;
2110 struct rule_head *r;
Harald Welte1cef74d2001-01-05 15:22:59 +00002111
2112 iptc_fn = TC_ZERO_COUNTER;
2113 CHECK(*handle);
2114
Harald Welteaae69be2004-08-29 23:32:14 +00002115 if (!(c = iptcc_find_label(chain, *handle))) {
Harald Welte1cef74d2001-01-05 15:22:59 +00002116 errno = ENOENT;
2117 return 0;
2118 }
2119
Harald Welteaae69be2004-08-29 23:32:14 +00002120 if (!(r = iptcc_get_rule_num(c, rulenum))) {
Harald Welte0113fe72004-01-06 19:04:02 +00002121 errno = E2BIG;
2122 return 0;
2123 }
2124
Harald Welteaae69be2004-08-29 23:32:14 +00002125 if (r->counter_map.maptype == COUNTER_MAP_NORMAL_MAP)
2126 r->counter_map.maptype = COUNTER_MAP_ZEROED;
Harald Welte1cef74d2001-01-05 15:22:59 +00002127
2128 set_changed(*handle);
2129
2130 return 1;
2131}
2132
2133int
2134TC_SET_COUNTER(const IPT_CHAINLABEL chain,
2135 unsigned int rulenum,
2136 STRUCT_COUNTERS *counters,
2137 TC_HANDLE_T *handle)
2138{
Harald Welteaae69be2004-08-29 23:32:14 +00002139 struct chain_head *c;
2140 struct rule_head *r;
Harald Welte1cef74d2001-01-05 15:22:59 +00002141 STRUCT_ENTRY *e;
Harald Welte1cef74d2001-01-05 15:22:59 +00002142
2143 iptc_fn = TC_SET_COUNTER;
2144 CHECK(*handle);
2145
Harald Welteaae69be2004-08-29 23:32:14 +00002146 if (!(c = iptcc_find_label(chain, *handle))) {
Harald Welte1cef74d2001-01-05 15:22:59 +00002147 errno = ENOENT;
2148 return 0;
2149 }
Harald Welte0113fe72004-01-06 19:04:02 +00002150
Harald Welteaae69be2004-08-29 23:32:14 +00002151 if (!(r = iptcc_get_rule_num(c, rulenum))) {
Harald Welte0113fe72004-01-06 19:04:02 +00002152 errno = E2BIG;
2153 return 0;
2154 }
2155
Harald Welteaae69be2004-08-29 23:32:14 +00002156 e = r->entry;
2157 r->counter_map.maptype = COUNTER_MAP_SET;
Harald Welte0113fe72004-01-06 19:04:02 +00002158
2159 memcpy(&e->counters, counters, sizeof(STRUCT_COUNTERS));
Harald Welte1cef74d2001-01-05 15:22:59 +00002160
2161 set_changed(*handle);
2162
2163 return 1;
2164}
2165
Marc Bouchere6869a82000-03-20 06:03:29 +00002166/* Creates a new chain. */
2167/* To create a chain, create two rules: error node and unconditional
2168 * return. */
2169int
Rusty Russell79dee072000-05-02 16:45:16 +00002170TC_CREATE_CHAIN(const IPT_CHAINLABEL chain, TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00002171{
Harald Welteaae69be2004-08-29 23:32:14 +00002172 static struct chain_head *c;
Patrick McHardy1f23d3c2008-06-07 15:04:34 +02002173 int capacity;
2174 int exceeded;
Marc Bouchere6869a82000-03-20 06:03:29 +00002175
Rusty Russell79dee072000-05-02 16:45:16 +00002176 iptc_fn = TC_CREATE_CHAIN;
Marc Bouchere6869a82000-03-20 06:03:29 +00002177
2178 /* find_label doesn't cover built-in targets: DROP, ACCEPT,
2179 QUEUE, RETURN. */
Harald Welteaae69be2004-08-29 23:32:14 +00002180 if (iptcc_find_label(chain, *handle)
Rusty Russell79dee072000-05-02 16:45:16 +00002181 || strcmp(chain, LABEL_DROP) == 0
2182 || strcmp(chain, LABEL_ACCEPT) == 0
Rusty Russell67088e72000-05-10 01:18:57 +00002183 || strcmp(chain, LABEL_QUEUE) == 0
Rusty Russell79dee072000-05-02 16:45:16 +00002184 || strcmp(chain, LABEL_RETURN) == 0) {
Harald Welteaae69be2004-08-29 23:32:14 +00002185 DEBUGP("Chain `%s' already exists\n", chain);
Marc Bouchere6869a82000-03-20 06:03:29 +00002186 errno = EEXIST;
2187 return 0;
2188 }
2189
Rusty Russell79dee072000-05-02 16:45:16 +00002190 if (strlen(chain)+1 > sizeof(IPT_CHAINLABEL)) {
Harald Welteaae69be2004-08-29 23:32:14 +00002191 DEBUGP("Chain name `%s' too long\n", chain);
Marc Bouchere6869a82000-03-20 06:03:29 +00002192 errno = EINVAL;
2193 return 0;
2194 }
2195
Harald Welteaae69be2004-08-29 23:32:14 +00002196 c = iptcc_alloc_chain_head(chain, 0);
2197 if (!c) {
2198 DEBUGP("Cannot allocate memory for chain `%s'\n", chain);
2199 errno = ENOMEM;
2200 return 0;
Marc Bouchere6869a82000-03-20 06:03:29 +00002201
Harald Welteaae69be2004-08-29 23:32:14 +00002202 }
Jesper Dangaard Brouer48bde402008-01-15 17:06:48 +00002203 (*handle)->num_chains++; /* New user defined chain */
Marc Bouchere6869a82000-03-20 06:03:29 +00002204
Harald Welteaae69be2004-08-29 23:32:14 +00002205 DEBUGP("Creating chain `%s'\n", chain);
Jesper Dangaard Brouerd8cb7872007-11-28 08:40:26 +00002206 iptc_insert_chain(*handle, c); /* Insert sorted */
Harald Welte0113fe72004-01-06 19:04:02 +00002207
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +00002208 /* Inserting chains don't change the correctness of the chain
2209 * index (except if its smaller than index[0], but that
2210 * handled by iptc_insert_chain). It only causes longer lists
2211 * in the buckets. Thus, only rebuild chain index when the
2212 * capacity is exceed with CHAIN_INDEX_INSERT_MAX chains.
2213 */
Patrick McHardy1f23d3c2008-06-07 15:04:34 +02002214 capacity = (*handle)->chain_index_sz * CHAIN_INDEX_BUCKET_LEN;
2215 exceeded = ((((*handle)->num_chains)-capacity));
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +00002216 if (exceeded > CHAIN_INDEX_INSERT_MAX) {
2217 debug("Capacity(%d) exceeded(%d) rebuild (chains:%d)\n",
2218 capacity, exceeded, (*handle)->num_chains);
2219 iptcc_chain_index_rebuild(*handle);
2220 }
2221
Harald Welte0113fe72004-01-06 19:04:02 +00002222 set_changed(*handle);
2223
Harald Welteaae69be2004-08-29 23:32:14 +00002224 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00002225}
2226
2227/* Get the number of references to this chain. */
2228int
Rusty Russell79dee072000-05-02 16:45:16 +00002229TC_GET_REFERENCES(unsigned int *ref, const IPT_CHAINLABEL chain,
2230 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00002231{
Harald Welteaae69be2004-08-29 23:32:14 +00002232 struct chain_head *c;
Marc Bouchere6869a82000-03-20 06:03:29 +00002233
Derrik Pates664c0a32005-02-01 13:28:14 +00002234 iptc_fn = TC_GET_REFERENCES;
Harald Welteaae69be2004-08-29 23:32:14 +00002235 if (!(c = iptcc_find_label(chain, *handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002236 errno = ENOENT;
2237 return 0;
2238 }
2239
Harald Welteaae69be2004-08-29 23:32:14 +00002240 *ref = c->references;
2241
Marc Bouchere6869a82000-03-20 06:03:29 +00002242 return 1;
2243}
2244
2245/* Deletes a chain. */
2246int
Rusty Russell79dee072000-05-02 16:45:16 +00002247TC_DELETE_CHAIN(const IPT_CHAINLABEL chain, TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00002248{
Marc Bouchere6869a82000-03-20 06:03:29 +00002249 unsigned int references;
Harald Welteaae69be2004-08-29 23:32:14 +00002250 struct chain_head *c;
Rusty Russell7e53bf92000-03-20 07:03:28 +00002251
Rusty Russell79dee072000-05-02 16:45:16 +00002252 iptc_fn = TC_DELETE_CHAIN;
Marc Bouchere6869a82000-03-20 06:03:29 +00002253
Harald Welteaae69be2004-08-29 23:32:14 +00002254 if (!(c = iptcc_find_label(chain, *handle))) {
2255 DEBUGP("cannot find chain `%s'\n", chain);
Marc Bouchere6869a82000-03-20 06:03:29 +00002256 errno = ENOENT;
2257 return 0;
2258 }
2259
Harald Welteaae69be2004-08-29 23:32:14 +00002260 if (TC_BUILTIN(chain, *handle)) {
2261 DEBUGP("cannot remove builtin chain `%s'\n", chain);
2262 errno = EINVAL;
2263 return 0;
2264 }
2265
2266 if (!TC_GET_REFERENCES(&references, chain, handle)) {
2267 DEBUGP("cannot get references on chain `%s'\n", chain);
2268 return 0;
2269 }
2270
2271 if (references > 0) {
2272 DEBUGP("chain `%s' still has references\n", chain);
2273 errno = EMLINK;
2274 return 0;
2275 }
2276
2277 if (c->num_rules) {
2278 DEBUGP("chain `%s' is not empty\n", chain);
Marc Bouchere6869a82000-03-20 06:03:29 +00002279 errno = ENOTEMPTY;
2280 return 0;
2281 }
2282
Harald Welteaae69be2004-08-29 23:32:14 +00002283 /* If we are about to delete the chain that is the current
Jesper Dangaard Brouer48bde402008-01-15 17:06:48 +00002284 * iterator, move chain iterator forward. */
Harald Welteaae69be2004-08-29 23:32:14 +00002285 if (c == (*handle)->chain_iterator_cur)
2286 iptcc_chain_iterator_advance(*handle);
Harald Welte0113fe72004-01-06 19:04:02 +00002287
Jesper Dangaard Brouer48bde402008-01-15 17:06:48 +00002288 (*handle)->num_chains--; /* One user defined chain deleted */
2289
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +00002290 //list_del(&c->list); /* Done in iptcc_chain_index_delete_chain() */
2291 iptcc_chain_index_delete_chain(c, *handle);
2292 free(c);
2293
Harald Welteaae69be2004-08-29 23:32:14 +00002294 DEBUGP("chain `%s' deleted\n", chain);
2295
2296 set_changed(*handle);
2297
2298 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00002299}
2300
2301/* Renames a chain. */
Rusty Russell79dee072000-05-02 16:45:16 +00002302int TC_RENAME_CHAIN(const IPT_CHAINLABEL oldname,
2303 const IPT_CHAINLABEL newname,
2304 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00002305{
Harald Welteaae69be2004-08-29 23:32:14 +00002306 struct chain_head *c;
Rusty Russell79dee072000-05-02 16:45:16 +00002307 iptc_fn = TC_RENAME_CHAIN;
Marc Bouchere6869a82000-03-20 06:03:29 +00002308
Harald Welte1de80462000-10-30 12:00:27 +00002309 /* find_label doesn't cover built-in targets: DROP, ACCEPT,
2310 QUEUE, RETURN. */
Harald Welteaae69be2004-08-29 23:32:14 +00002311 if (iptcc_find_label(newname, *handle)
Rusty Russell79dee072000-05-02 16:45:16 +00002312 || strcmp(newname, LABEL_DROP) == 0
2313 || strcmp(newname, LABEL_ACCEPT) == 0
Harald Welte1de80462000-10-30 12:00:27 +00002314 || strcmp(newname, LABEL_QUEUE) == 0
Rusty Russell79dee072000-05-02 16:45:16 +00002315 || strcmp(newname, LABEL_RETURN) == 0) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002316 errno = EEXIST;
2317 return 0;
2318 }
2319
Harald Welteaae69be2004-08-29 23:32:14 +00002320 if (!(c = iptcc_find_label(oldname, *handle))
Rusty Russell79dee072000-05-02 16:45:16 +00002321 || TC_BUILTIN(oldname, *handle)) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002322 errno = ENOENT;
2323 return 0;
2324 }
2325
Rusty Russell79dee072000-05-02 16:45:16 +00002326 if (strlen(newname)+1 > sizeof(IPT_CHAINLABEL)) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002327 errno = EINVAL;
2328 return 0;
2329 }
2330
Harald Welteaae69be2004-08-29 23:32:14 +00002331 strncpy(c->name, newname, sizeof(IPT_CHAINLABEL));
2332
Harald Welte0113fe72004-01-06 19:04:02 +00002333 set_changed(*handle);
2334
Marc Bouchere6869a82000-03-20 06:03:29 +00002335 return 1;
2336}
2337
2338/* Sets the policy on a built-in chain. */
2339int
Rusty Russell79dee072000-05-02 16:45:16 +00002340TC_SET_POLICY(const IPT_CHAINLABEL chain,
2341 const IPT_CHAINLABEL policy,
Harald Welte1cef74d2001-01-05 15:22:59 +00002342 STRUCT_COUNTERS *counters,
Rusty Russell79dee072000-05-02 16:45:16 +00002343 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00002344{
Harald Welteaae69be2004-08-29 23:32:14 +00002345 struct chain_head *c;
Marc Bouchere6869a82000-03-20 06:03:29 +00002346
Rusty Russell79dee072000-05-02 16:45:16 +00002347 iptc_fn = TC_SET_POLICY;
Marc Bouchere6869a82000-03-20 06:03:29 +00002348
Harald Welteaae69be2004-08-29 23:32:14 +00002349 if (!(c = iptcc_find_label(chain, *handle))) {
2350 DEBUGP("cannot find chain `%s'\n", chain);
2351 errno = ENOENT;
Marc Bouchere6869a82000-03-20 06:03:29 +00002352 return 0;
2353 }
2354
Harald Welteaae69be2004-08-29 23:32:14 +00002355 if (!iptcc_is_builtin(c)) {
2356 DEBUGP("cannot set policy of userdefinedchain `%s'\n", chain);
2357 errno = ENOENT;
2358 return 0;
2359 }
Marc Bouchere6869a82000-03-20 06:03:29 +00002360
Rusty Russell79dee072000-05-02 16:45:16 +00002361 if (strcmp(policy, LABEL_ACCEPT) == 0)
Harald Welteaae69be2004-08-29 23:32:14 +00002362 c->verdict = -NF_ACCEPT - 1;
Rusty Russell79dee072000-05-02 16:45:16 +00002363 else if (strcmp(policy, LABEL_DROP) == 0)
Harald Welteaae69be2004-08-29 23:32:14 +00002364 c->verdict = -NF_DROP - 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00002365 else {
2366 errno = EINVAL;
2367 return 0;
2368 }
Harald Welte1cef74d2001-01-05 15:22:59 +00002369
Harald Welte1cef74d2001-01-05 15:22:59 +00002370 if (counters) {
2371 /* set byte and packet counters */
Harald Welteaae69be2004-08-29 23:32:14 +00002372 memcpy(&c->counters, counters, sizeof(STRUCT_COUNTERS));
2373 c->counter_map.maptype = COUNTER_MAP_SET;
Harald Welte1cef74d2001-01-05 15:22:59 +00002374 } else {
Harald Welteaae69be2004-08-29 23:32:14 +00002375 c->counter_map.maptype = COUNTER_MAP_NOMAP;
Harald Welte1cef74d2001-01-05 15:22:59 +00002376 }
2377
Rusty Russell175f6412000-03-24 09:32:20 +00002378 set_changed(*handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00002379
Marc Bouchere6869a82000-03-20 06:03:29 +00002380 return 1;
2381}
2382
2383/* Without this, on gcc 2.7.2.3, we get:
Rusty Russell79dee072000-05-02 16:45:16 +00002384 libiptc.c: In function `TC_COMMIT':
Marc Bouchere6869a82000-03-20 06:03:29 +00002385 libiptc.c:833: fixed or forbidden register was spilled.
2386 This may be due to a compiler bug or to impossible asm
2387 statements or clauses.
2388*/
2389static void
Rusty Russell79dee072000-05-02 16:45:16 +00002390subtract_counters(STRUCT_COUNTERS *answer,
2391 const STRUCT_COUNTERS *a,
2392 const STRUCT_COUNTERS *b)
Marc Bouchere6869a82000-03-20 06:03:29 +00002393{
2394 answer->pcnt = a->pcnt - b->pcnt;
2395 answer->bcnt = a->bcnt - b->bcnt;
2396}
2397
Harald Welteaae69be2004-08-29 23:32:14 +00002398
Jan Engelhardtdbb77542008-02-11 00:33:30 +01002399static void counters_nomap(STRUCT_COUNTERS_INFO *newcounters, unsigned int idx)
Harald Welteaae69be2004-08-29 23:32:14 +00002400{
Jan Engelhardtdbb77542008-02-11 00:33:30 +01002401 newcounters->counters[idx] = ((STRUCT_COUNTERS) { 0, 0});
Harald Welteaae69be2004-08-29 23:32:14 +00002402 DEBUGP_C("NOMAP => zero\n");
2403}
2404
2405static void counters_normal_map(STRUCT_COUNTERS_INFO *newcounters,
Jan Engelhardtdbb77542008-02-11 00:33:30 +01002406 STRUCT_REPLACE *repl, unsigned int idx,
Harald Welteaae69be2004-08-29 23:32:14 +00002407 unsigned int mappos)
2408{
2409 /* Original read: X.
2410 * Atomic read on replacement: X + Y.
2411 * Currently in kernel: Z.
2412 * Want in kernel: X + Y + Z.
2413 * => Add in X + Y
2414 * => Add in replacement read.
2415 */
Jan Engelhardtdbb77542008-02-11 00:33:30 +01002416 newcounters->counters[idx] = repl->counters[mappos];
Harald Welteaae69be2004-08-29 23:32:14 +00002417 DEBUGP_C("NORMAL_MAP => mappos %u \n", mappos);
2418}
2419
2420static void counters_map_zeroed(STRUCT_COUNTERS_INFO *newcounters,
Jan Engelhardtdbb77542008-02-11 00:33:30 +01002421 STRUCT_REPLACE *repl, unsigned int idx,
2422 unsigned int mappos, STRUCT_COUNTERS *counters)
Harald Welteaae69be2004-08-29 23:32:14 +00002423{
2424 /* Original read: X.
2425 * Atomic read on replacement: X + Y.
2426 * Currently in kernel: Z.
2427 * Want in kernel: Y + Z.
2428 * => Add in Y.
2429 * => Add in (replacement read - original read).
2430 */
Jan Engelhardtdbb77542008-02-11 00:33:30 +01002431 subtract_counters(&newcounters->counters[idx],
Harald Welteaae69be2004-08-29 23:32:14 +00002432 &repl->counters[mappos],
2433 counters);
2434 DEBUGP_C("ZEROED => mappos %u\n", mappos);
2435}
2436
2437static void counters_map_set(STRUCT_COUNTERS_INFO *newcounters,
Jan Engelhardtdbb77542008-02-11 00:33:30 +01002438 unsigned int idx, STRUCT_COUNTERS *counters)
Harald Welteaae69be2004-08-29 23:32:14 +00002439{
2440 /* Want to set counter (iptables-restore) */
2441
Jan Engelhardtdbb77542008-02-11 00:33:30 +01002442 memcpy(&newcounters->counters[idx], counters,
Harald Welteaae69be2004-08-29 23:32:14 +00002443 sizeof(STRUCT_COUNTERS));
2444
2445 DEBUGP_C("SET\n");
2446}
2447
2448
Marc Bouchere6869a82000-03-20 06:03:29 +00002449int
Rusty Russell79dee072000-05-02 16:45:16 +00002450TC_COMMIT(TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00002451{
2452 /* Replace, then map back the counters. */
Rusty Russell79dee072000-05-02 16:45:16 +00002453 STRUCT_REPLACE *repl;
2454 STRUCT_COUNTERS_INFO *newcounters;
Harald Welteaae69be2004-08-29 23:32:14 +00002455 struct chain_head *c;
2456 int ret;
Martin Josefsson841e4ae2003-05-02 15:30:11 +00002457 size_t counterlen;
Harald Welteaae69be2004-08-29 23:32:14 +00002458 int new_number;
2459 unsigned int new_size;
Marc Bouchere6869a82000-03-20 06:03:29 +00002460
Derrik Pates664c0a32005-02-01 13:28:14 +00002461 iptc_fn = TC_COMMIT;
Marc Bouchere6869a82000-03-20 06:03:29 +00002462 CHECK(*handle);
Martin Josefsson841e4ae2003-05-02 15:30:11 +00002463
Marc Bouchere6869a82000-03-20 06:03:29 +00002464 /* Don't commit if nothing changed. */
2465 if (!(*handle)->changed)
2466 goto finished;
2467
Harald Welteaae69be2004-08-29 23:32:14 +00002468 new_number = iptcc_compile_table_prep(*handle, &new_size);
2469 if (new_number < 0) {
2470 errno = ENOMEM;
Harald Welted6ba6f52005-11-12 10:39:40 +00002471 goto out_zero;
Harald Welteaae69be2004-08-29 23:32:14 +00002472 }
2473
2474 repl = malloc(sizeof(*repl) + new_size);
Marc Bouchere6869a82000-03-20 06:03:29 +00002475 if (!repl) {
2476 errno = ENOMEM;
Harald Welted6ba6f52005-11-12 10:39:40 +00002477 goto out_zero;
Marc Bouchere6869a82000-03-20 06:03:29 +00002478 }
Martin Josefssonad3b4f92004-09-22 22:04:07 +00002479 memset(repl, 0, sizeof(*repl) + new_size);
Harald Welteaae69be2004-08-29 23:32:14 +00002480
Rusty Russelle45c7132004-12-16 13:21:44 +00002481#if 0
2482 TC_DUMP_ENTRIES(*handle);
2483#endif
2484
Harald Welteaae69be2004-08-29 23:32:14 +00002485 counterlen = sizeof(STRUCT_COUNTERS_INFO)
2486 + sizeof(STRUCT_COUNTERS) * new_number;
Marc Bouchere6869a82000-03-20 06:03:29 +00002487
2488 /* These are the old counters we will get from kernel */
Rusty Russell79dee072000-05-02 16:45:16 +00002489 repl->counters = malloc(sizeof(STRUCT_COUNTERS)
Marc Bouchere6869a82000-03-20 06:03:29 +00002490 * (*handle)->info.num_entries);
2491 if (!repl->counters) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002492 errno = ENOMEM;
Harald Welted6ba6f52005-11-12 10:39:40 +00002493 goto out_free_repl;
Marc Bouchere6869a82000-03-20 06:03:29 +00002494 }
Marc Bouchere6869a82000-03-20 06:03:29 +00002495 /* These are the counters we're going to put back, later. */
2496 newcounters = malloc(counterlen);
2497 if (!newcounters) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002498 errno = ENOMEM;
Harald Welted6ba6f52005-11-12 10:39:40 +00002499 goto out_free_repl_counters;
Marc Bouchere6869a82000-03-20 06:03:29 +00002500 }
Harald Welteaae69be2004-08-29 23:32:14 +00002501 memset(newcounters, 0, counterlen);
Marc Bouchere6869a82000-03-20 06:03:29 +00002502
2503 strcpy(repl->name, (*handle)->info.name);
Harald Welteaae69be2004-08-29 23:32:14 +00002504 repl->num_entries = new_number;
2505 repl->size = new_size;
2506
Marc Bouchere6869a82000-03-20 06:03:29 +00002507 repl->num_counters = (*handle)->info.num_entries;
2508 repl->valid_hooks = (*handle)->info.valid_hooks;
Harald Welteaae69be2004-08-29 23:32:14 +00002509
2510 DEBUGP("num_entries=%u, size=%u, num_counters=%u\n",
2511 repl->num_entries, repl->size, repl->num_counters);
2512
2513 ret = iptcc_compile_table(*handle, repl);
2514 if (ret < 0) {
2515 errno = ret;
Harald Welted6ba6f52005-11-12 10:39:40 +00002516 goto out_free_newcounters;
Harald Welteaae69be2004-08-29 23:32:14 +00002517 }
2518
2519
2520#ifdef IPTC_DEBUG2
2521 {
2522 int fd = open("/tmp/libiptc-so_set_replace.blob",
2523 O_CREAT|O_WRONLY);
2524 if (fd >= 0) {
2525 write(fd, repl, sizeof(*repl) + repl->size);
2526 close(fd);
2527 }
2528 }
2529#endif
Marc Bouchere6869a82000-03-20 06:03:29 +00002530
Harald Welted6ba6f52005-11-12 10:39:40 +00002531 ret = setsockopt(sockfd, TC_IPPROTO, SO_SET_REPLACE, repl,
2532 sizeof(*repl) + repl->size);
Patrick McHardye0865ad2006-04-22 02:08:56 +00002533 if (ret < 0)
Harald Welted6ba6f52005-11-12 10:39:40 +00002534 goto out_free_newcounters;
Marc Bouchere6869a82000-03-20 06:03:29 +00002535
2536 /* Put counters back. */
2537 strcpy(newcounters->name, (*handle)->info.name);
Harald Welteaae69be2004-08-29 23:32:14 +00002538 newcounters->num_counters = new_number;
Marc Bouchere6869a82000-03-20 06:03:29 +00002539
Harald Welteaae69be2004-08-29 23:32:14 +00002540 list_for_each_entry(c, &(*handle)->chains, list) {
2541 struct rule_head *r;
Marc Bouchere6869a82000-03-20 06:03:29 +00002542
Harald Welteaae69be2004-08-29 23:32:14 +00002543 /* Builtin chains have their own counters */
2544 if (iptcc_is_builtin(c)) {
2545 DEBUGP("counter for chain-index %u: ", c->foot_index);
2546 switch(c->counter_map.maptype) {
2547 case COUNTER_MAP_NOMAP:
2548 counters_nomap(newcounters, c->foot_index);
2549 break;
2550 case COUNTER_MAP_NORMAL_MAP:
2551 counters_normal_map(newcounters, repl,
2552 c->foot_index,
2553 c->counter_map.mappos);
2554 break;
2555 case COUNTER_MAP_ZEROED:
2556 counters_map_zeroed(newcounters, repl,
2557 c->foot_index,
2558 c->counter_map.mappos,
2559 &c->counters);
2560 break;
2561 case COUNTER_MAP_SET:
2562 counters_map_set(newcounters, c->foot_index,
2563 &c->counters);
2564 break;
2565 }
2566 }
Harald Welte1cef74d2001-01-05 15:22:59 +00002567
Harald Welteaae69be2004-08-29 23:32:14 +00002568 list_for_each_entry(r, &c->rules, list) {
2569 DEBUGP("counter for index %u: ", r->index);
2570 switch (r->counter_map.maptype) {
2571 case COUNTER_MAP_NOMAP:
2572 counters_nomap(newcounters, r->index);
2573 break;
Harald Welte1cef74d2001-01-05 15:22:59 +00002574
Harald Welteaae69be2004-08-29 23:32:14 +00002575 case COUNTER_MAP_NORMAL_MAP:
2576 counters_normal_map(newcounters, repl,
2577 r->index,
2578 r->counter_map.mappos);
2579 break;
Harald Welte1cef74d2001-01-05 15:22:59 +00002580
Harald Welteaae69be2004-08-29 23:32:14 +00002581 case COUNTER_MAP_ZEROED:
2582 counters_map_zeroed(newcounters, repl,
2583 r->index,
2584 r->counter_map.mappos,
2585 &r->entry->counters);
2586 break;
2587
2588 case COUNTER_MAP_SET:
2589 counters_map_set(newcounters, r->index,
2590 &r->entry->counters);
2591 break;
2592 }
Marc Bouchere6869a82000-03-20 06:03:29 +00002593 }
2594 }
Rusty Russell62527ce2000-09-04 09:45:54 +00002595
Harald Welteaae69be2004-08-29 23:32:14 +00002596#ifdef IPTC_DEBUG2
2597 {
2598 int fd = open("/tmp/libiptc-so_set_add_counters.blob",
2599 O_CREAT|O_WRONLY);
2600 if (fd >= 0) {
2601 write(fd, newcounters, counterlen);
2602 close(fd);
2603 }
2604 }
2605#endif
2606
Harald Welted6ba6f52005-11-12 10:39:40 +00002607 ret = setsockopt(sockfd, TC_IPPROTO, SO_SET_ADD_COUNTERS,
2608 newcounters, counterlen);
Patrick McHardye0865ad2006-04-22 02:08:56 +00002609 if (ret < 0)
Harald Welted6ba6f52005-11-12 10:39:40 +00002610 goto out_free_newcounters;
Marc Bouchere6869a82000-03-20 06:03:29 +00002611
2612 free(repl->counters);
2613 free(repl);
2614 free(newcounters);
2615
Harald Welted6ba6f52005-11-12 10:39:40 +00002616finished:
Martin Josefsson841e4ae2003-05-02 15:30:11 +00002617 TC_FREE(handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00002618 return 1;
Harald Welted6ba6f52005-11-12 10:39:40 +00002619
2620out_free_newcounters:
2621 free(newcounters);
2622out_free_repl_counters:
2623 free(repl->counters);
2624out_free_repl:
2625 free(repl);
2626out_zero:
2627 return 0;
Marc Bouchere6869a82000-03-20 06:03:29 +00002628}
2629
2630/* Get raw socket. */
2631int
Patrick McHardy0b639362007-09-08 16:00:01 +00002632TC_GET_RAW_SOCKET(void)
Marc Bouchere6869a82000-03-20 06:03:29 +00002633{
2634 return sockfd;
2635}
2636
2637/* Translates errno numbers into more human-readable form than strerror. */
2638const char *
Rusty Russell79dee072000-05-02 16:45:16 +00002639TC_STRERROR(int err)
Marc Bouchere6869a82000-03-20 06:03:29 +00002640{
2641 unsigned int i;
2642 struct table_struct {
2643 void *fn;
2644 int err;
2645 const char *message;
2646 } table [] =
Harald Welte4ccfa632001-07-30 15:12:43 +00002647 { { TC_INIT, EPERM, "Permission denied (you must be root)" },
Rusty Russell79dee072000-05-02 16:45:16 +00002648 { TC_INIT, EINVAL, "Module is wrong version" },
Harald Welte4ccfa632001-07-30 15:12:43 +00002649 { TC_INIT, ENOENT,
2650 "Table does not exist (do you need to insmod?)" },
Rusty Russell79dee072000-05-02 16:45:16 +00002651 { TC_DELETE_CHAIN, ENOTEMPTY, "Chain is not empty" },
2652 { TC_DELETE_CHAIN, EINVAL, "Can't delete built-in chain" },
2653 { TC_DELETE_CHAIN, EMLINK,
Marc Bouchere6869a82000-03-20 06:03:29 +00002654 "Can't delete chain with references left" },
Rusty Russell79dee072000-05-02 16:45:16 +00002655 { TC_CREATE_CHAIN, EEXIST, "Chain already exists" },
2656 { TC_INSERT_ENTRY, E2BIG, "Index of insertion too big" },
2657 { TC_REPLACE_ENTRY, E2BIG, "Index of replacement too big" },
2658 { TC_DELETE_NUM_ENTRY, E2BIG, "Index of deletion too big" },
Harald Welte1cef74d2001-01-05 15:22:59 +00002659 { TC_READ_COUNTER, E2BIG, "Index of counter too big" },
2660 { TC_ZERO_COUNTER, E2BIG, "Index of counter too big" },
Rusty Russell79dee072000-05-02 16:45:16 +00002661 { TC_INSERT_ENTRY, ELOOP, "Loop found in table" },
2662 { TC_INSERT_ENTRY, EINVAL, "Target problem" },
Marc Bouchere6869a82000-03-20 06:03:29 +00002663 /* EINVAL for CHECK probably means bad interface. */
Rusty Russell79dee072000-05-02 16:45:16 +00002664 { TC_CHECK_PACKET, EINVAL,
Marc Boucherc8264992000-04-22 22:34:44 +00002665 "Bad arguments (does that interface exist?)" },
Harald Welte4ccfa632001-07-30 15:12:43 +00002666 { TC_CHECK_PACKET, ENOSYS,
2667 "Checking will most likely never get implemented" },
Marc Bouchere6869a82000-03-20 06:03:29 +00002668 /* ENOENT for DELETE probably means no matching rule */
Rusty Russell79dee072000-05-02 16:45:16 +00002669 { TC_DELETE_ENTRY, ENOENT,
Marc Boucherc8264992000-04-22 22:34:44 +00002670 "Bad rule (does a matching rule exist in that chain?)" },
Rusty Russell79dee072000-05-02 16:45:16 +00002671 { TC_SET_POLICY, ENOENT,
Marc Boucherc8264992000-04-22 22:34:44 +00002672 "Bad built-in chain name" },
Rusty Russell79dee072000-05-02 16:45:16 +00002673 { TC_SET_POLICY, EINVAL,
Marc Boucherc8264992000-04-22 22:34:44 +00002674 "Bad policy name" },
Harald Welte4ccfa632001-07-30 15:12:43 +00002675
2676 { NULL, 0, "Incompatible with this kernel" },
2677 { NULL, ENOPROTOOPT, "iptables who? (do you need to insmod?)" },
2678 { NULL, ENOSYS, "Will be implemented real soon. I promise ;)" },
2679 { NULL, ENOMEM, "Memory allocation problem" },
2680 { NULL, ENOENT, "No chain/target/match by that name" },
Marc Bouchere6869a82000-03-20 06:03:29 +00002681 };
2682
2683 for (i = 0; i < sizeof(table)/sizeof(struct table_struct); i++) {
2684 if ((!table[i].fn || table[i].fn == iptc_fn)
2685 && table[i].err == err)
2686 return table[i].message;
2687 }
2688
2689 return strerror(err);
2690}