1 /* Library which manipulates firewall rules. Version $Revision$ */
3 /* Architecture of firewall rules is as follows:
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.
11 /* (C) 1999 Paul ``Rusty'' Russell - Placed under the GNU GPL (See
12 * COPYING for details).
13 * (C) 2000-2004 by the Netfilter Core Team <coreteam@netfilter.org>
15 * 2003-Jun-20: Harald Welte <laforge@netfilter.org>:
16 * - Reimplementation of chain cache to use offsets instead of entries
17 * 2003-Jun-23: Harald Welte <laforge@netfilter.org>:
18 * - performance optimization, sponsored by Astaro AG (http://www.astaro.com/)
19 * don't rebuild the chain cache after every operation, instead fix it
20 * up after a ruleset change.
21 * 2004-Aug-18: Harald Welte <laforge@netfilter.org>:
22 * - further 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/)
27 * 2008-Jan+Jul: Jesper Dangaard Brouer <hawk@comx.dk>
28 * - performance work: speedup chain list "name" searching.
29 * - performance work: speedup initial ruleset parsing.
30 * - sponsored by ComX Networks A/S (http://www.comx.dk/)
34 #include <sys/types.h>
35 #include <sys/socket.h>
38 #include <libiptc/xtcshared.h>
40 #include "linux_list.h"
42 //#define IPTC_DEBUG2 1
46 #define DEBUGP(x, args...) fprintf(stderr, "%s: " x, __FUNCTION__, ## args)
47 #define DEBUGP_C(x, args...) fprintf(stderr, x, ## args)
49 #define DEBUGP(x, args...)
50 #define DEBUGP_C(x, args...)
54 #define debug(x, args...) fprintf(stderr, x, ## args)
56 #define debug(x, args...)
59 static void *iptc_fn = NULL;
61 static const char *hooknames[] = {
62 [HOOK_PRE_ROUTING] = "PREROUTING",
63 [HOOK_LOCAL_IN] = "INPUT",
64 [HOOK_FORWARD] = "FORWARD",
65 [HOOK_LOCAL_OUT] = "OUTPUT",
66 [HOOK_POST_ROUTING] = "POSTROUTING",
69 /* Convenience structures */
77 COUNTER_MAP_NORMAL_MAP,
84 enum iptcc_rule_type {
85 IPTCC_R_STANDARD, /* standard target (ACCEPT, ...) */
86 IPTCC_R_MODULE, /* extension module (SNAT, ...) */
87 IPTCC_R_FALLTHROUGH, /* fallthrough rule */
88 IPTCC_R_JUMP, /* jump to other chain */
93 struct list_head list;
94 struct chain_head *chain;
95 struct counter_map counter_map;
97 unsigned int index; /* index (needed for counter_map) */
98 unsigned int offset; /* offset in rule blob */
100 enum iptcc_rule_type type;
101 struct chain_head *jump; /* jump target, if IPTCC_R_JUMP */
103 unsigned int size; /* size of entry data */
104 STRUCT_ENTRY entry[0];
109 struct list_head list;
110 char name[TABLE_MAXNAMELEN];
111 unsigned int hooknum; /* hook number+1 if builtin */
112 unsigned int references; /* how many jumps reference us */
113 int verdict; /* verdict if builtin */
115 STRUCT_COUNTERS counters; /* per-chain counters */
116 struct counter_map counter_map;
118 unsigned int num_rules; /* number of rules in list */
119 struct list_head rules; /* list of rules */
121 unsigned int index; /* index (needed for jump resolval) */
122 unsigned int head_offset; /* offset in rule blob */
123 unsigned int foot_index; /* index (needed for counter_map) */
124 unsigned int foot_offset; /* offset in rule blob */
129 int changed; /* Have changes been made? */
131 struct list_head chains;
133 struct chain_head *chain_iterator_cur;
134 struct rule_head *rule_iterator_cur;
136 unsigned int num_chains; /* number of user defined chains */
138 struct chain_head **chain_index; /* array for fast chain list access*/
139 unsigned int chain_index_sz;/* size of chain index array */
141 int sorted_offsets; /* if chains are received sorted from kernel,
142 * then the offsets are also sorted. Says if its
143 * possible to bsearch offsets using chain_index.
147 STRUCT_GET_ENTRIES *entries;
151 BSEARCH_NAME, /* Binary search after chain name */
152 BSEARCH_OFFSET, /* Binary search based on offset */
155 /* allocate a new chain head for the cache */
156 static struct chain_head *iptcc_alloc_chain_head(const char *name, int hooknum)
158 struct chain_head *c = malloc(sizeof(*c));
161 memset(c, 0, sizeof(*c));
163 strncpy(c->name, name, TABLE_MAXNAMELEN);
164 c->hooknum = hooknum;
165 INIT_LIST_HEAD(&c->rules);
170 /* allocate and initialize a new rule for the cache */
171 static struct rule_head *iptcc_alloc_rule(struct chain_head *c, unsigned int size)
173 struct rule_head *r = malloc(sizeof(*r)+size);
176 memset(r, 0, sizeof(*r));
184 /* notify us that the ruleset has been modified by the user */
186 set_changed(struct xtc_handle *h)
192 static void do_check(struct xtc_handle *h, unsigned int line);
193 #define CHECK(h) do { if (!getenv("IPTC_NO_CHECK")) do_check((h), __LINE__); } while(0)
199 /**********************************************************************
200 * iptc blob utility functions (iptcb_*)
201 **********************************************************************/
204 iptcb_get_number(const STRUCT_ENTRY *i,
205 const STRUCT_ENTRY *seek,
215 iptcb_get_entry_n(STRUCT_ENTRY *i,
220 if (*pos == number) {
228 static inline STRUCT_ENTRY *
229 iptcb_get_entry(struct xtc_handle *h, unsigned int offset)
231 return (STRUCT_ENTRY *)((char *)h->entries->entrytable + offset);
235 iptcb_entry2index(struct xtc_handle *const h, const STRUCT_ENTRY *seek)
237 unsigned int pos = 0;
239 if (ENTRY_ITERATE(h->entries->entrytable, h->entries->size,
240 iptcb_get_number, seek, &pos) == 0) {
241 fprintf(stderr, "ERROR: offset %u not an entry!\n",
242 (unsigned int)((char *)seek - (char *)h->entries->entrytable));
248 static inline STRUCT_ENTRY *
249 iptcb_offset2entry(struct xtc_handle *h, unsigned int offset)
251 return (STRUCT_ENTRY *) ((void *)h->entries->entrytable+offset);
255 static inline unsigned long
256 iptcb_entry2offset(struct xtc_handle *const h, const STRUCT_ENTRY *e)
258 return (void *)e - (void *)h->entries->entrytable;
261 static inline unsigned int
262 iptcb_offset2index(struct xtc_handle *const h, unsigned int offset)
264 return iptcb_entry2index(h, iptcb_offset2entry(h, offset));
267 /* Returns 0 if not hook entry, else hooknumber + 1 */
268 static inline unsigned int
269 iptcb_ent_is_hook_entry(STRUCT_ENTRY *e, struct xtc_handle *h)
273 for (i = 0; i < NUMHOOKS; i++) {
274 if ((h->info.valid_hooks & (1 << i))
275 && iptcb_get_entry(h, h->info.hook_entry[i]) == e)
282 /**********************************************************************
283 * Chain index (cache utility) functions
284 **********************************************************************
285 * The chain index is an array with pointers into the chain list, with
286 * CHAIN_INDEX_BUCKET_LEN spacing. This facilitates the ability to
287 * speedup chain list searching, by find a more optimal starting
288 * points when searching the linked list.
290 * The starting point can be found fast by using a binary search of
291 * the chain index. Thus, reducing the previous search complexity of
292 * O(n) to O(log(n/k) + k) where k is CHAIN_INDEX_BUCKET_LEN.
294 * A nice property of the chain index, is that the "bucket" list
295 * length is max CHAIN_INDEX_BUCKET_LEN (when just build, inserts will
296 * change this). Oppose to hashing, where the "bucket" list length can
299 #ifndef CHAIN_INDEX_BUCKET_LEN
300 #define CHAIN_INDEX_BUCKET_LEN 40
303 /* Another nice property of the chain index is that inserting/creating
304 * chains in chain list don't change the correctness of the chain
305 * index, it only causes longer lists in the buckets.
307 * To mitigate the performance penalty of longer bucket lists and the
308 * penalty of rebuilding, the chain index is rebuild only when
309 * CHAIN_INDEX_INSERT_MAX chains has been added.
311 #ifndef CHAIN_INDEX_INSERT_MAX
312 #define CHAIN_INDEX_INSERT_MAX 355
315 static inline unsigned int iptcc_is_builtin(struct chain_head *c);
317 /* Use binary search in the chain index array, to find a chain_head
318 * pointer closest to the place of the searched name element.
320 * Notes that, binary search (obviously) requires that the chain list
323 * The not so obvious: The chain index array, is actually both sorted
324 * by name and offset, at the same time!. This is only true because,
325 * chain are stored sorted in the kernel (as we pushed it in sorted).
328 static struct list_head *
329 __iptcc_bsearch_chain_index(const char *name, unsigned int offset,
330 unsigned int *idx, struct xtc_handle *handle,
331 enum bsearch_type type)
333 unsigned int pos, end;
336 struct list_head *list_pos;
337 list_pos=&handle->chains;
339 /* Check for empty array, e.g. no user defined chains */
340 if (handle->chain_index_sz == 0) {
341 debug("WARNING: handle->chain_index_sz == 0\n");
346 end = handle->chain_index_sz;
349 debug("bsearch Find chain:%s (pos:%d end:%d) (offset:%d)\n",
350 name, pos, end, offset);
354 if (!handle->chain_index[pos]) {
355 fprintf(stderr, "ERROR: NULL pointer chain_index[%d]\n", pos);
356 return &handle->chains; /* Be safe, return orig start pos */
359 debug("bsearch Index[%d] name:%s ",
360 pos, handle->chain_index[pos]->name);
362 /* Support for different compare functions */
365 res = strcmp(name, handle->chain_index[pos]->name);
368 debug("head_offset:[%d] foot_offset:[%d] ",
369 handle->chain_index[pos]->head_offset,
370 handle->chain_index[pos]->foot_offset);
371 res = offset - handle->chain_index[pos]->head_offset;
374 fprintf(stderr, "ERROR: %d not a valid bsearch type\n",
379 debug("res:%d ", res);
382 list_pos = &handle->chain_index[pos]->list;
385 if (res == 0) { /* Found element, by direct hit */
386 debug("[found] Direct hit pos:%d end:%d\n", pos, end);
388 } else if (res < 0) { /* Too far, jump back */
392 /* Exit case: First element of array */
394 debug("[found] Reached first array elem (end%d)\n",end);
397 debug("jump back to pos:%d (end:%d)\n", pos, end);
399 } else { /* res > 0; Not far enough, jump forward */
401 /* Exit case: Last element of array */
402 if (pos == handle->chain_index_sz-1) {
403 debug("[found] Last array elem (end:%d)\n", end);
407 /* Exit case: Next index less, thus elem in this list section */
410 res = strcmp(name, handle->chain_index[pos+1]->name);
413 res = offset - handle->chain_index[pos+1]->head_offset;
418 debug("[found] closest list (end:%d)\n", end);
423 debug("jump forward to pos:%d (end:%d)\n", pos, end);
428 /* Wrapper for string chain name based bsearch */
429 static struct list_head *
430 iptcc_bsearch_chain_index(const char *name, unsigned int *idx,
431 struct xtc_handle *handle)
433 return __iptcc_bsearch_chain_index(name, 0, idx, handle, BSEARCH_NAME);
437 /* Wrapper for offset chain based bsearch */
438 static struct list_head *
439 iptcc_bsearch_chain_offset(unsigned int offset, unsigned int *idx,
440 struct xtc_handle *handle)
442 struct list_head *pos;
444 /* If chains were not received sorted from kernel, then the
445 * offset bsearch is not possible.
447 if (!handle->sorted_offsets)
448 pos = handle->chains.next;
450 pos = __iptcc_bsearch_chain_index(NULL, offset, idx, handle,
457 /* Trivial linear search of chain index. Function used for verifying
458 the output of bsearch function */
459 static struct list_head *
460 iptcc_linearly_search_chain_index(const char *name, struct xtc_handle *handle)
465 struct list_head *list_pos;
466 list_pos = &handle->chains;
468 if (handle->chain_index_sz)
469 list_pos = &handle->chain_index[0]->list;
471 /* Linearly walk of chain index array */
473 for (i=0; i < handle->chain_index_sz; i++) {
474 if (handle->chain_index[i]) {
475 res = strcmp(handle->chain_index[i]->name, name);
477 break; // One step too far
478 list_pos = &handle->chain_index[i]->list;
488 static int iptcc_chain_index_alloc(struct xtc_handle *h)
490 unsigned int list_length = CHAIN_INDEX_BUCKET_LEN;
491 unsigned int array_elems;
492 unsigned int array_mem;
494 /* Allocate memory for the chain index array */
495 array_elems = (h->num_chains / list_length) +
496 (h->num_chains % list_length ? 1 : 0);
497 array_mem = sizeof(h->chain_index) * array_elems;
499 debug("Alloc Chain index, elems:%d mem:%d bytes\n",
500 array_elems, array_mem);
502 h->chain_index = malloc(array_mem);
503 if (h->chain_index == NULL && array_mem > 0) {
504 h->chain_index_sz = 0;
507 memset(h->chain_index, 0, array_mem);
508 h->chain_index_sz = array_elems;
513 static void iptcc_chain_index_free(struct xtc_handle *h)
515 h->chain_index_sz = 0;
516 free(h->chain_index);
521 static void iptcc_chain_index_dump(struct xtc_handle *h)
525 /* Dump: contents of chain index array */
526 for (i=0; i < h->chain_index_sz; i++) {
527 if (h->chain_index[i]) {
528 fprintf(stderr, "Chain index[%d].name: %s\n",
529 i, h->chain_index[i]->name);
535 /* Build the chain index */
536 static int iptcc_chain_index_build(struct xtc_handle *h)
538 unsigned int list_length = CHAIN_INDEX_BUCKET_LEN;
539 unsigned int chains = 0;
540 unsigned int cindex = 0;
541 struct chain_head *c;
543 /* Build up the chain index array here */
544 debug("Building chain index\n");
546 debug("Number of user defined chains:%d bucket_sz:%d array_sz:%d\n",
547 h->num_chains, list_length, h->chain_index_sz);
549 if (h->chain_index_sz == 0)
552 list_for_each_entry(c, &h->chains, list) {
554 /* Issue: The index array needs to start after the
555 * builtin chains, as they are not sorted */
556 if (!iptcc_is_builtin(c)) {
557 cindex=chains / list_length;
559 /* Safe guard, break out on array limit, this
560 * is useful if chains are added and array is
561 * rebuild, without realloc of memory. */
562 if (cindex >= h->chain_index_sz)
565 if ((chains % list_length)== 0) {
566 debug("\nIndex[%d] Chains:", cindex);
567 h->chain_index[cindex] = c;
571 debug("%s, ", c->name);
578 static int iptcc_chain_index_rebuild(struct xtc_handle *h)
580 debug("REBUILD chain index array\n");
581 iptcc_chain_index_free(h);
582 if ((iptcc_chain_index_alloc(h)) < 0)
584 iptcc_chain_index_build(h);
588 /* Delete chain (pointer) from index array. Removing an element from
589 * the chain list only affects the chain index array, if the chain
590 * index points-to/uses that list pointer.
592 * There are different strategies, the simple and safe is to rebuild
593 * the chain index every time. The more advanced is to update the
594 * array index to point to the next element, but that requires some
595 * house keeping and boundry checks. The advanced is implemented, as
596 * the simple approach behaves badly when all chains are deleted
597 * because list_for_each processing will always hit the first chain
598 * index, thus causing a rebuild for every chain.
600 static int iptcc_chain_index_delete_chain(struct chain_head *c, struct xtc_handle *h)
602 struct list_head *index_ptr, *next;
603 struct chain_head *c2;
604 unsigned int idx, idx2;
606 index_ptr = iptcc_bsearch_chain_index(c->name, &idx, h);
608 debug("Del chain[%s] c->list:%p index_ptr:%p\n",
609 c->name, &c->list, index_ptr);
611 /* Save the next pointer */
615 if (index_ptr == &c->list) { /* Chain used as index ptr */
617 /* See if its possible to avoid a rebuild, by shifting
618 * to next pointer. Its possible if the next pointer
619 * is located in the same index bucket.
621 c2 = list_entry(next, struct chain_head, list);
622 iptcc_bsearch_chain_index(c2->name, &idx2, h);
625 return iptcc_chain_index_rebuild(h);
627 /* Avoiding rebuild */
628 debug("Update cindex[%d] with next ptr name:[%s]\n",
630 h->chain_index[idx]=c2;
638 /**********************************************************************
639 * iptc cache utility functions (iptcc_*)
640 **********************************************************************/
642 /* Is the given chain builtin (1) or user-defined (0) */
643 static inline unsigned int iptcc_is_builtin(struct chain_head *c)
645 return (c->hooknum ? 1 : 0);
648 /* Get a specific rule within a chain */
649 static struct rule_head *iptcc_get_rule_num(struct chain_head *c,
650 unsigned int rulenum)
653 unsigned int num = 0;
655 list_for_each_entry(r, &c->rules, list) {
663 /* Get a specific rule within a chain backwards */
664 static struct rule_head *iptcc_get_rule_num_reverse(struct chain_head *c,
665 unsigned int rulenum)
668 unsigned int num = 0;
670 list_for_each_entry_reverse(r, &c->rules, list) {
678 /* Returns chain head if found, otherwise NULL. */
679 static struct chain_head *
680 iptcc_find_chain_by_offset(struct xtc_handle *handle, unsigned int offset)
682 struct list_head *pos;
683 struct list_head *list_start_pos;
686 if (list_empty(&handle->chains))
689 /* Find a smart place to start the search */
690 list_start_pos = iptcc_bsearch_chain_offset(offset, &i, handle);
692 /* Note that iptcc_bsearch_chain_offset() skips builtin
693 * chains, but this function is only used for finding jump
694 * targets, and a buildin chain is not a valid jump target */
696 debug("Offset:[%u] starting search at index:[%u]\n", offset, i);
697 // list_for_each(pos, &handle->chains) {
698 list_for_each(pos, list_start_pos->prev) {
699 struct chain_head *c = list_entry(pos, struct chain_head, list);
701 if (offset >= c->head_offset && offset <= c->foot_offset) {
702 debug("Offset search found chain:[%s]\n", c->name);
710 /* Returns chain head if found, otherwise NULL. */
711 static struct chain_head *
712 iptcc_find_label(const char *name, struct xtc_handle *handle)
714 struct list_head *pos;
715 struct list_head *list_start_pos;
719 if (list_empty(&handle->chains))
722 /* First look at builtin chains */
723 list_for_each(pos, &handle->chains) {
724 struct chain_head *c = list_entry(pos, struct chain_head, list);
725 if (!iptcc_is_builtin(c))
727 if (!strcmp(c->name, name))
731 /* Find a smart place to start the search via chain index */
732 //list_start_pos = iptcc_linearly_search_chain_index(name, handle);
733 list_start_pos = iptcc_bsearch_chain_index(name, &i, handle);
735 /* Handel if bsearch bails out early */
736 if (list_start_pos == &handle->chains) {
737 list_start_pos = pos;
741 /* Verify result of bsearch against linearly index search */
742 struct list_head *test_pos;
743 struct chain_head *test_c, *tmp_c;
744 test_pos = iptcc_linearly_search_chain_index(name, handle);
745 if (list_start_pos != test_pos) {
746 debug("BUG in chain_index search\n");
747 test_c=list_entry(test_pos, struct chain_head,list);
748 tmp_c =list_entry(list_start_pos,struct chain_head,list);
749 debug("Verify search found:\n");
750 debug(" Chain:%s\n", test_c->name);
751 debug("BSearch found:\n");
752 debug(" Chain:%s\n", tmp_c->name);
758 /* Initial/special case, no user defined chains */
759 if (handle->num_chains == 0)
762 /* Start searching through the chain list */
763 list_for_each(pos, list_start_pos->prev) {
764 struct chain_head *c = list_entry(pos, struct chain_head, list);
765 res = strcmp(c->name, name);
766 debug("List search name:%s == %s res:%d\n", name, c->name, res);
770 /* We can stop earlier as we know list is sorted */
771 if (res>0 && !iptcc_is_builtin(c)) { /* Walked too far*/
772 debug(" Not in list, walked too far, sorted list\n");
776 /* Stop on wrap around, if list head is reached */
777 if (pos == &handle->chains) {
778 debug("Stop, list head reached\n");
783 debug("List search NOT found name:%s\n", name);
787 /* called when rule is to be removed from cache */
788 static void iptcc_delete_rule(struct rule_head *r)
790 DEBUGP("deleting rule %p (offset %u)\n", r, r->offset);
791 /* clean up reference count of called chain */
792 if (r->type == IPTCC_R_JUMP
794 r->jump->references--;
801 /**********************************************************************
802 * RULESET PARSER (blob -> cache)
803 **********************************************************************/
805 /* Delete policy rule of previous chain, since cache doesn't contain
806 * chain policy rules.
807 * WARNING: This function has ugly design and relies on a lot of context, only
808 * to be called from specific places within the parser */
809 static int __iptcc_p_del_policy(struct xtc_handle *h, unsigned int num)
811 const unsigned char *data;
813 if (h->chain_iterator_cur) {
814 /* policy rule is last rule */
815 struct rule_head *pr = (struct rule_head *)
816 h->chain_iterator_cur->rules.prev;
819 data = GET_TARGET(pr->entry)->data;
820 h->chain_iterator_cur->verdict = *(const int *)data;
822 /* save counter and counter_map information */
823 h->chain_iterator_cur->counter_map.maptype =
825 h->chain_iterator_cur->counter_map.mappos = num-1;
826 memcpy(&h->chain_iterator_cur->counters, &pr->entry->counters,
827 sizeof(h->chain_iterator_cur->counters));
829 /* foot_offset points to verdict rule */
830 h->chain_iterator_cur->foot_index = num;
831 h->chain_iterator_cur->foot_offset = pr->offset;
833 /* delete rule from cache */
834 iptcc_delete_rule(pr);
835 h->chain_iterator_cur->num_rules--;
842 /* alphabetically insert a chain into the list */
843 static void iptc_insert_chain(struct xtc_handle *h, struct chain_head *c)
845 struct chain_head *tmp;
846 struct list_head *list_start_pos;
849 /* Find a smart place to start the insert search */
850 list_start_pos = iptcc_bsearch_chain_index(c->name, &i, h);
852 /* Handle the case, where chain.name is smaller than index[0] */
853 if (i==0 && strcmp(c->name, h->chain_index[0]->name) <= 0) {
854 h->chain_index[0] = c; /* Update chain index head */
855 list_start_pos = h->chains.next;
856 debug("Update chain_index[0] with %s\n", c->name);
859 /* Handel if bsearch bails out early */
860 if (list_start_pos == &h->chains) {
861 list_start_pos = h->chains.next;
864 /* sort only user defined chains */
866 list_for_each_entry(tmp, list_start_pos->prev, list) {
867 if (!tmp->hooknum && strcmp(c->name, tmp->name) <= 0) {
868 list_add(&c->list, tmp->list.prev);
872 /* Stop if list head is reached */
873 if (&tmp->list == &h->chains) {
874 debug("Insert, list head reached add to tail\n");
880 /* survived till end of list: add at tail */
881 list_add_tail(&c->list, &h->chains);
884 /* Another ugly helper function split out of cache_add_entry to make it less
886 static void __iptcc_p_add_chain(struct xtc_handle *h, struct chain_head *c,
887 unsigned int offset, unsigned int *num)
889 struct list_head *tail = h->chains.prev;
890 struct chain_head *ctail;
892 __iptcc_p_del_policy(h, *num);
894 c->head_offset = offset;
897 /* Chains from kernel are already sorted, as they are inserted
898 * sorted. But there exists an issue when shifting to 1.4.0
899 * from an older version, as old versions allow last created
900 * chain to be unsorted.
902 if (iptcc_is_builtin(c)) /* Only user defined chains are sorted*/
903 list_add_tail(&c->list, &h->chains);
905 ctail = list_entry(tail, struct chain_head, list);
907 if (strcmp(c->name, ctail->name) > 0 ||
908 iptcc_is_builtin(ctail))
909 list_add_tail(&c->list, &h->chains);/* Already sorted*/
911 iptc_insert_chain(h, c);/* Was not sorted */
913 /* Notice, if chains were not received sorted
914 * from kernel, then an offset bsearch is no
917 h->sorted_offsets = 0;
919 debug("NOTICE: chain:[%s] was NOT sorted(ctail:%s)\n",
920 c->name, ctail->name);
924 h->chain_iterator_cur = c;
927 /* main parser function: add an entry from the blob to the cache */
928 static int cache_add_entry(STRUCT_ENTRY *e,
929 struct xtc_handle *h,
933 unsigned int builtin;
934 unsigned int offset = (char *)e - (char *)h->entries->entrytable;
936 DEBUGP("entering...");
938 /* Last entry ("policy rule"). End it.*/
939 if (iptcb_entry2offset(h,e) + e->next_offset == h->entries->size) {
940 /* This is the ERROR node at the end of the chain */
941 DEBUGP_C("%u:%u: end of table:\n", *num, offset);
943 __iptcc_p_del_policy(h, *num);
945 h->chain_iterator_cur = NULL;
949 /* We know this is the start of a new chain if it's an ERROR
950 * target, or a hook entry point */
952 if (strcmp(GET_TARGET(e)->u.user.name, ERROR_TARGET) == 0) {
953 struct chain_head *c =
954 iptcc_alloc_chain_head((const char *)GET_TARGET(e)->data, 0);
955 DEBUGP_C("%u:%u:new userdefined chain %s: %p\n", *num, offset,
961 h->num_chains++; /* New user defined chain */
963 __iptcc_p_add_chain(h, c, offset, num);
965 } else if ((builtin = iptcb_ent_is_hook_entry(e, h)) != 0) {
966 struct chain_head *c =
967 iptcc_alloc_chain_head((char *)hooknames[builtin-1],
969 DEBUGP_C("%u:%u new builtin chain: %p (rules=%p)\n",
970 *num, offset, c, &c->rules);
976 c->hooknum = builtin;
978 __iptcc_p_add_chain(h, c, offset, num);
980 /* FIXME: this is ugly. */
983 /* has to be normal rule */
987 if (!(r = iptcc_alloc_rule(h->chain_iterator_cur,
992 DEBUGP_C("%u:%u normal rule: %p: ", *num, offset, r);
996 memcpy(r->entry, e, e->next_offset);
997 r->counter_map.maptype = COUNTER_MAP_NORMAL_MAP;
998 r->counter_map.mappos = r->index;
1000 /* handling of jumps, etc. */
1001 if (!strcmp(GET_TARGET(e)->u.user.name, STANDARD_TARGET)) {
1002 STRUCT_STANDARD_TARGET *t;
1004 t = (STRUCT_STANDARD_TARGET *)GET_TARGET(e);
1005 if (t->target.u.target_size
1006 != ALIGN(sizeof(STRUCT_STANDARD_TARGET))) {
1012 if (t->verdict < 0) {
1013 DEBUGP_C("standard, verdict=%d\n", t->verdict);
1014 r->type = IPTCC_R_STANDARD;
1015 } else if (t->verdict == r->offset+e->next_offset) {
1016 DEBUGP_C("fallthrough\n");
1017 r->type = IPTCC_R_FALLTHROUGH;
1019 DEBUGP_C("jump, target=%u\n", t->verdict);
1020 r->type = IPTCC_R_JUMP;
1021 /* Jump target fixup has to be deferred
1022 * until second pass, since we migh not
1023 * yet have parsed the target */
1026 DEBUGP_C("module, target=%s\n", GET_TARGET(e)->u.user.name);
1027 r->type = IPTCC_R_MODULE;
1030 list_add_tail(&r->list, &h->chain_iterator_cur->rules);
1031 h->chain_iterator_cur->num_rules++;
1039 /* parse an iptables blob into it's pieces */
1040 static int parse_table(struct xtc_handle *h)
1043 unsigned int num = 0;
1044 struct chain_head *c;
1046 /* Assume that chains offsets are sorted, this verified during
1047 parsing of ruleset (in __iptcc_p_add_chain())*/
1048 h->sorted_offsets = 1;
1050 /* First pass: over ruleset blob */
1051 ENTRY_ITERATE(h->entries->entrytable, h->entries->size,
1052 cache_add_entry, h, &prev, &num);
1054 /* Build the chain index, used for chain list search speedup */
1055 if ((iptcc_chain_index_alloc(h)) < 0)
1057 iptcc_chain_index_build(h);
1059 /* Second pass: fixup parsed data from first pass */
1060 list_for_each_entry(c, &h->chains, list) {
1061 struct rule_head *r;
1062 list_for_each_entry(r, &c->rules, list) {
1063 struct chain_head *lc;
1064 STRUCT_STANDARD_TARGET *t;
1066 if (r->type != IPTCC_R_JUMP)
1069 t = (STRUCT_STANDARD_TARGET *)GET_TARGET(r->entry);
1070 lc = iptcc_find_chain_by_offset(h, t->verdict);
1082 /**********************************************************************
1083 * RULESET COMPILATION (cache -> blob)
1084 **********************************************************************/
1086 /* Convenience structures */
1087 struct iptcb_chain_start{
1089 struct xt_error_target name;
1091 #define IPTCB_CHAIN_START_SIZE (sizeof(STRUCT_ENTRY) + \
1092 ALIGN(sizeof(struct xt_error_target)))
1094 struct iptcb_chain_foot {
1096 STRUCT_STANDARD_TARGET target;
1098 #define IPTCB_CHAIN_FOOT_SIZE (sizeof(STRUCT_ENTRY) + \
1099 ALIGN(sizeof(STRUCT_STANDARD_TARGET)))
1101 struct iptcb_chain_error {
1103 struct xt_error_target target;
1105 #define IPTCB_CHAIN_ERROR_SIZE (sizeof(STRUCT_ENTRY) + \
1106 ALIGN(sizeof(struct xt_error_target)))
1110 /* compile rule from cache into blob */
1111 static inline int iptcc_compile_rule (struct xtc_handle *h, STRUCT_REPLACE *repl, struct rule_head *r)
1114 if (r->type == IPTCC_R_JUMP) {
1115 STRUCT_STANDARD_TARGET *t;
1116 t = (STRUCT_STANDARD_TARGET *)GET_TARGET(r->entry);
1117 /* memset for memcmp convenience on delete/replace */
1118 memset(t->target.u.user.name, 0, FUNCTION_MAXNAMELEN);
1119 strcpy(t->target.u.user.name, STANDARD_TARGET);
1120 /* Jumps can only happen to builtin chains, so we
1121 * can safely assume that they always have a header */
1122 t->verdict = r->jump->head_offset + IPTCB_CHAIN_START_SIZE;
1123 } else if (r->type == IPTCC_R_FALLTHROUGH) {
1124 STRUCT_STANDARD_TARGET *t;
1125 t = (STRUCT_STANDARD_TARGET *)GET_TARGET(r->entry);
1126 t->verdict = r->offset + r->size;
1129 /* copy entry from cache to blob */
1130 memcpy((char *)repl->entries+r->offset, r->entry, r->size);
1135 /* compile chain from cache into blob */
1136 static int iptcc_compile_chain(struct xtc_handle *h, STRUCT_REPLACE *repl, struct chain_head *c)
1139 struct rule_head *r;
1140 struct iptcb_chain_start *head;
1141 struct iptcb_chain_foot *foot;
1143 /* only user-defined chains have heaer */
1144 if (!iptcc_is_builtin(c)) {
1145 /* put chain header in place */
1146 head = (void *)repl->entries + c->head_offset;
1147 head->e.target_offset = sizeof(STRUCT_ENTRY);
1148 head->e.next_offset = IPTCB_CHAIN_START_SIZE;
1149 strcpy(head->name.target.u.user.name, ERROR_TARGET);
1150 head->name.target.u.target_size =
1151 ALIGN(sizeof(struct xt_error_target));
1152 strcpy(head->name.errorname, c->name);
1154 repl->hook_entry[c->hooknum-1] = c->head_offset;
1155 repl->underflow[c->hooknum-1] = c->foot_offset;
1158 /* iterate over rules */
1159 list_for_each_entry(r, &c->rules, list) {
1160 ret = iptcc_compile_rule(h, repl, r);
1165 /* put chain footer in place */
1166 foot = (void *)repl->entries + c->foot_offset;
1167 foot->e.target_offset = sizeof(STRUCT_ENTRY);
1168 foot->e.next_offset = IPTCB_CHAIN_FOOT_SIZE;
1169 strcpy(foot->target.target.u.user.name, STANDARD_TARGET);
1170 foot->target.target.u.target_size =
1171 ALIGN(sizeof(STRUCT_STANDARD_TARGET));
1172 /* builtin targets have verdict, others return */
1173 if (iptcc_is_builtin(c))
1174 foot->target.verdict = c->verdict;
1176 foot->target.verdict = RETURN;
1177 /* set policy-counters */
1178 memcpy(&foot->e.counters, &c->counters, sizeof(STRUCT_COUNTERS));
1183 /* calculate offset and number for every rule in the cache */
1184 static int iptcc_compile_chain_offsets(struct xtc_handle *h, struct chain_head *c,
1185 unsigned int *offset, unsigned int *num)
1187 struct rule_head *r;
1189 c->head_offset = *offset;
1190 DEBUGP("%s: chain_head %u, offset=%u\n", c->name, *num, *offset);
1192 if (!iptcc_is_builtin(c)) {
1193 /* Chain has header */
1194 *offset += sizeof(STRUCT_ENTRY)
1195 + ALIGN(sizeof(struct xt_error_target));
1199 list_for_each_entry(r, &c->rules, list) {
1200 DEBUGP("rule %u, offset=%u, index=%u\n", *num, *offset, *num);
1201 r->offset = *offset;
1207 DEBUGP("%s; chain_foot %u, offset=%u, index=%u\n", c->name, *num,
1209 c->foot_offset = *offset;
1210 c->foot_index = *num;
1211 *offset += sizeof(STRUCT_ENTRY)
1212 + ALIGN(sizeof(STRUCT_STANDARD_TARGET));
1218 /* put the pieces back together again */
1219 static int iptcc_compile_table_prep(struct xtc_handle *h, unsigned int *size)
1221 struct chain_head *c;
1222 unsigned int offset = 0, num = 0;
1225 /* First pass: calculate offset for every rule */
1226 list_for_each_entry(c, &h->chains, list) {
1227 ret = iptcc_compile_chain_offsets(h, c, &offset, &num);
1232 /* Append one error rule at end of chain */
1234 offset += sizeof(STRUCT_ENTRY)
1235 + ALIGN(sizeof(struct xt_error_target));
1237 /* ruleset size is now in offset */
1242 static int iptcc_compile_table(struct xtc_handle *h, STRUCT_REPLACE *repl)
1244 struct chain_head *c;
1245 struct iptcb_chain_error *error;
1247 /* Second pass: copy from cache to offsets, fill in jumps */
1248 list_for_each_entry(c, &h->chains, list) {
1249 int ret = iptcc_compile_chain(h, repl, c);
1254 /* Append error rule at end of chain */
1255 error = (void *)repl->entries + repl->size - IPTCB_CHAIN_ERROR_SIZE;
1256 error->entry.target_offset = sizeof(STRUCT_ENTRY);
1257 error->entry.next_offset = IPTCB_CHAIN_ERROR_SIZE;
1258 error->target.target.u.user.target_size =
1259 ALIGN(sizeof(struct xt_error_target));
1260 strcpy((char *)&error->target.target.u.user.name, ERROR_TARGET);
1261 strcpy((char *)&error->target.errorname, "ERROR");
1266 /**********************************************************************
1267 * EXTERNAL API (operates on cache only)
1268 **********************************************************************/
1270 /* Allocate handle of given size */
1271 static struct xtc_handle *
1272 alloc_handle(const char *tablename, unsigned int size, unsigned int num_rules)
1274 struct xtc_handle *h;
1276 h = malloc(sizeof(*h));
1281 memset(h, 0, sizeof(*h));
1282 INIT_LIST_HEAD(&h->chains);
1283 strcpy(h->info.name, tablename);
1285 h->entries = malloc(sizeof(STRUCT_GET_ENTRIES) + size);
1287 goto out_free_handle;
1289 strcpy(h->entries->name, tablename);
1290 h->entries->size = size;
1302 TC_INIT(const char *tablename)
1304 struct xtc_handle *h;
1305 STRUCT_GETINFO info;
1313 if (strlen(tablename) >= TABLE_MAXNAMELEN) {
1318 sockfd = socket(TC_AF, SOCK_RAW, IPPROTO_RAW);
1322 if (fcntl(sockfd, F_SETFD, FD_CLOEXEC) == -1) {
1323 fprintf(stderr, "Could not set close on exec: %s\n",
1330 strcpy(info.name, tablename);
1331 if (getsockopt(sockfd, TC_IPPROTO, SO_GET_INFO, &info, &s) < 0) {
1336 DEBUGP("valid_hooks=0x%08x, num_entries=%u, size=%u\n",
1337 info.valid_hooks, info.num_entries, info.size);
1339 if ((h = alloc_handle(info.name, info.size, info.num_entries))
1345 /* Initialize current state */
1349 h->entries->size = h->info.size;
1351 tmp = sizeof(STRUCT_GET_ENTRIES) + h->info.size;
1353 if (getsockopt(h->sockfd, TC_IPPROTO, SO_GET_ENTRIES, h->entries,
1359 int fd = open("/tmp/libiptc-so_get_entries.blob",
1362 write(fd, h->entries, tmp);
1368 if (parse_table(h) < 0)
1375 /* A different process changed the ruleset size, retry */
1376 if (errno == EAGAIN)
1382 TC_FREE(struct xtc_handle *h)
1384 struct chain_head *c, *tmp;
1389 list_for_each_entry_safe(c, tmp, &h->chains, list) {
1390 struct rule_head *r, *rtmp;
1392 list_for_each_entry_safe(r, rtmp, &c->rules, list) {
1399 iptcc_chain_index_free(h);
1406 print_match(const STRUCT_ENTRY_MATCH *m)
1408 printf("Match name: `%s'\n", m->u.user.name);
1412 static int dump_entry(STRUCT_ENTRY *e, struct xtc_handle *const handle);
1415 TC_DUMP_ENTRIES(struct xtc_handle *const handle)
1417 iptc_fn = TC_DUMP_ENTRIES;
1420 printf("libiptc v%s. %u bytes.\n",
1421 XTABLES_VERSION, handle->entries->size);
1422 printf("Table `%s'\n", handle->info.name);
1423 printf("Hooks: pre/in/fwd/out/post = %x/%x/%x/%x/%x\n",
1424 handle->info.hook_entry[HOOK_PRE_ROUTING],
1425 handle->info.hook_entry[HOOK_LOCAL_IN],
1426 handle->info.hook_entry[HOOK_FORWARD],
1427 handle->info.hook_entry[HOOK_LOCAL_OUT],
1428 handle->info.hook_entry[HOOK_POST_ROUTING]);
1429 printf("Underflows: pre/in/fwd/out/post = %x/%x/%x/%x/%x\n",
1430 handle->info.underflow[HOOK_PRE_ROUTING],
1431 handle->info.underflow[HOOK_LOCAL_IN],
1432 handle->info.underflow[HOOK_FORWARD],
1433 handle->info.underflow[HOOK_LOCAL_OUT],
1434 handle->info.underflow[HOOK_POST_ROUTING]);
1436 ENTRY_ITERATE(handle->entries->entrytable, handle->entries->size,
1437 dump_entry, handle);
1440 /* Does this chain exist? */
1441 int TC_IS_CHAIN(const char *chain, struct xtc_handle *const handle)
1443 iptc_fn = TC_IS_CHAIN;
1444 return iptcc_find_label(chain, handle) != NULL;
1447 static void iptcc_chain_iterator_advance(struct xtc_handle *handle)
1449 struct chain_head *c = handle->chain_iterator_cur;
1451 if (c->list.next == &handle->chains)
1452 handle->chain_iterator_cur = NULL;
1454 handle->chain_iterator_cur =
1455 list_entry(c->list.next, struct chain_head, list);
1458 /* Iterator functions to run through the chains. */
1460 TC_FIRST_CHAIN(struct xtc_handle *handle)
1462 struct chain_head *c = list_entry(handle->chains.next,
1463 struct chain_head, list);
1465 iptc_fn = TC_FIRST_CHAIN;
1468 if (list_empty(&handle->chains)) {
1469 DEBUGP(": no chains\n");
1473 handle->chain_iterator_cur = c;
1474 iptcc_chain_iterator_advance(handle);
1476 DEBUGP(": returning `%s'\n", c->name);
1480 /* Iterator functions to run through the chains. Returns NULL at end. */
1482 TC_NEXT_CHAIN(struct xtc_handle *handle)
1484 struct chain_head *c = handle->chain_iterator_cur;
1486 iptc_fn = TC_NEXT_CHAIN;
1489 DEBUGP(": no more chains\n");
1493 iptcc_chain_iterator_advance(handle);
1495 DEBUGP(": returning `%s'\n", c->name);
1499 /* Get first rule in the given chain: NULL for empty chain. */
1500 const STRUCT_ENTRY *
1501 TC_FIRST_RULE(const char *chain, struct xtc_handle *handle)
1503 struct chain_head *c;
1504 struct rule_head *r;
1506 iptc_fn = TC_FIRST_RULE;
1508 DEBUGP("first rule(%s): ", chain);
1510 c = iptcc_find_label(chain, handle);
1516 /* Empty chain: single return/policy rule */
1517 if (list_empty(&c->rules)) {
1518 DEBUGP_C("no rules, returning NULL\n");
1522 r = list_entry(c->rules.next, struct rule_head, list);
1523 handle->rule_iterator_cur = r;
1524 DEBUGP_C("%p\n", r);
1529 /* Returns NULL when rules run out. */
1530 const STRUCT_ENTRY *
1531 TC_NEXT_RULE(const STRUCT_ENTRY *prev, struct xtc_handle *handle)
1533 struct rule_head *r;
1535 iptc_fn = TC_NEXT_RULE;
1536 DEBUGP("rule_iterator_cur=%p...", handle->rule_iterator_cur);
1538 if (handle->rule_iterator_cur == NULL) {
1539 DEBUGP_C("returning NULL\n");
1543 r = list_entry(handle->rule_iterator_cur->list.next,
1544 struct rule_head, list);
1546 iptc_fn = TC_NEXT_RULE;
1548 DEBUGP_C("next=%p, head=%p...", &r->list,
1549 &handle->rule_iterator_cur->chain->rules);
1551 if (&r->list == &handle->rule_iterator_cur->chain->rules) {
1552 handle->rule_iterator_cur = NULL;
1553 DEBUGP_C("finished, returning NULL\n");
1557 handle->rule_iterator_cur = r;
1559 /* NOTE: prev is without any influence ! */
1560 DEBUGP_C("returning rule %p\n", r);
1564 /* Returns a pointer to the target name of this position. */
1565 static const char *standard_target_map(int verdict)
1569 return LABEL_RETURN;
1572 return LABEL_ACCEPT;
1581 fprintf(stderr, "ERROR: %d not a valid target)\n",
1590 /* Returns a pointer to the target name of this position. */
1591 const char *TC_GET_TARGET(const STRUCT_ENTRY *ce,
1592 struct xtc_handle *handle)
1594 STRUCT_ENTRY *e = (STRUCT_ENTRY *)ce;
1595 struct rule_head *r = container_of(e, struct rule_head, entry[0]);
1596 const unsigned char *data;
1598 iptc_fn = TC_GET_TARGET;
1602 case IPTCC_R_FALLTHROUGH:
1606 DEBUGP("r=%p, jump=%p, name=`%s'\n", r, r->jump, r->jump->name);
1607 return r->jump->name;
1609 case IPTCC_R_STANDARD:
1610 data = GET_TARGET(e)->data;
1611 spos = *(const int *)data;
1612 DEBUGP("r=%p, spos=%d'\n", r, spos);
1613 return standard_target_map(spos);
1615 case IPTCC_R_MODULE:
1616 return GET_TARGET(e)->u.user.name;
1621 /* Is this a built-in chain? Actually returns hook + 1. */
1623 TC_BUILTIN(const char *chain, struct xtc_handle *const handle)
1625 struct chain_head *c;
1627 iptc_fn = TC_BUILTIN;
1629 c = iptcc_find_label(chain, handle);
1635 return iptcc_is_builtin(c);
1638 /* Get the policy of a given built-in chain */
1640 TC_GET_POLICY(const char *chain,
1641 STRUCT_COUNTERS *counters,
1642 struct xtc_handle *handle)
1644 struct chain_head *c;
1646 iptc_fn = TC_GET_POLICY;
1648 DEBUGP("called for chain %s\n", chain);
1650 c = iptcc_find_label(chain, handle);
1656 if (!iptcc_is_builtin(c))
1659 *counters = c->counters;
1661 return standard_target_map(c->verdict);
1665 iptcc_standard_map(struct rule_head *r, int verdict)
1667 STRUCT_ENTRY *e = r->entry;
1668 STRUCT_STANDARD_TARGET *t;
1670 t = (STRUCT_STANDARD_TARGET *)GET_TARGET(e);
1672 if (t->target.u.target_size
1673 != ALIGN(sizeof(STRUCT_STANDARD_TARGET))) {
1677 /* memset for memcmp convenience on delete/replace */
1678 memset(t->target.u.user.name, 0, FUNCTION_MAXNAMELEN);
1679 strcpy(t->target.u.user.name, STANDARD_TARGET);
1680 t->verdict = verdict;
1682 r->type = IPTCC_R_STANDARD;
1688 iptcc_map_target(struct xtc_handle *const handle,
1689 struct rule_head *r)
1691 STRUCT_ENTRY *e = r->entry;
1692 STRUCT_ENTRY_TARGET *t = GET_TARGET(e);
1694 /* Maybe it's empty (=> fall through) */
1695 if (strcmp(t->u.user.name, "") == 0) {
1696 r->type = IPTCC_R_FALLTHROUGH;
1699 /* Maybe it's a standard target name... */
1700 else if (strcmp(t->u.user.name, LABEL_ACCEPT) == 0)
1701 return iptcc_standard_map(r, -NF_ACCEPT - 1);
1702 else if (strcmp(t->u.user.name, LABEL_DROP) == 0)
1703 return iptcc_standard_map(r, -NF_DROP - 1);
1704 else if (strcmp(t->u.user.name, LABEL_QUEUE) == 0)
1705 return iptcc_standard_map(r, -NF_QUEUE - 1);
1706 else if (strcmp(t->u.user.name, LABEL_RETURN) == 0)
1707 return iptcc_standard_map(r, RETURN);
1708 else if (TC_BUILTIN(t->u.user.name, handle)) {
1709 /* Can't jump to builtins. */
1713 /* Maybe it's an existing chain name. */
1714 struct chain_head *c;
1715 DEBUGP("trying to find chain `%s': ", t->u.user.name);
1717 c = iptcc_find_label(t->u.user.name, handle);
1719 DEBUGP_C("found!\n");
1720 r->type = IPTCC_R_JUMP;
1725 DEBUGP_C("not found :(\n");
1728 /* Must be a module? If not, kernel will reject... */
1729 /* memset to all 0 for your memcmp convenience: don't clear version */
1730 memset(t->u.user.name + strlen(t->u.user.name),
1732 FUNCTION_MAXNAMELEN - 1 - strlen(t->u.user.name));
1733 r->type = IPTCC_R_MODULE;
1734 set_changed(handle);
1738 /* Insert the entry `fw' in chain `chain' into position `rulenum'. */
1740 TC_INSERT_ENTRY(const IPT_CHAINLABEL chain,
1741 const STRUCT_ENTRY *e,
1742 unsigned int rulenum,
1743 struct xtc_handle *handle)
1745 struct chain_head *c;
1746 struct rule_head *r;
1747 struct list_head *prev;
1749 iptc_fn = TC_INSERT_ENTRY;
1751 if (!(c = iptcc_find_label(chain, handle))) {
1756 /* first rulenum index = 0
1757 first c->num_rules index = 1 */
1758 if (rulenum > c->num_rules) {
1763 /* If we are inserting at the end just take advantage of the
1764 double linked list, insert will happen before the entry
1766 if (rulenum == c->num_rules) {
1768 } else if (rulenum + 1 <= c->num_rules/2) {
1769 r = iptcc_get_rule_num(c, rulenum + 1);
1772 r = iptcc_get_rule_num_reverse(c, c->num_rules - rulenum);
1776 if (!(r = iptcc_alloc_rule(c, e->next_offset))) {
1781 memcpy(r->entry, e, e->next_offset);
1782 r->counter_map.maptype = COUNTER_MAP_SET;
1784 if (!iptcc_map_target(handle, r)) {
1789 list_add_tail(&r->list, prev);
1792 set_changed(handle);
1797 /* Atomically replace rule `rulenum' in `chain' with `fw'. */
1799 TC_REPLACE_ENTRY(const IPT_CHAINLABEL chain,
1800 const STRUCT_ENTRY *e,
1801 unsigned int rulenum,
1802 struct xtc_handle *handle)
1804 struct chain_head *c;
1805 struct rule_head *r, *old;
1807 iptc_fn = TC_REPLACE_ENTRY;
1809 if (!(c = iptcc_find_label(chain, handle))) {
1814 if (rulenum >= c->num_rules) {
1819 /* Take advantage of the double linked list if possible. */
1820 if (rulenum + 1 <= c->num_rules/2) {
1821 old = iptcc_get_rule_num(c, rulenum + 1);
1823 old = iptcc_get_rule_num_reverse(c, c->num_rules - rulenum);
1826 if (!(r = iptcc_alloc_rule(c, e->next_offset))) {
1831 memcpy(r->entry, e, e->next_offset);
1832 r->counter_map.maptype = COUNTER_MAP_SET;
1834 if (!iptcc_map_target(handle, r)) {
1839 list_add(&r->list, &old->list);
1840 iptcc_delete_rule(old);
1842 set_changed(handle);
1847 /* Append entry `fw' to chain `chain'. Equivalent to insert with
1848 rulenum = length of chain. */
1850 TC_APPEND_ENTRY(const IPT_CHAINLABEL chain,
1851 const STRUCT_ENTRY *e,
1852 struct xtc_handle *handle)
1854 struct chain_head *c;
1855 struct rule_head *r;
1857 iptc_fn = TC_APPEND_ENTRY;
1858 if (!(c = iptcc_find_label(chain, handle))) {
1859 DEBUGP("unable to find chain `%s'\n", chain);
1864 if (!(r = iptcc_alloc_rule(c, e->next_offset))) {
1865 DEBUGP("unable to allocate rule for chain `%s'\n", chain);
1870 memcpy(r->entry, e, e->next_offset);
1871 r->counter_map.maptype = COUNTER_MAP_SET;
1873 if (!iptcc_map_target(handle, r)) {
1874 DEBUGP("unable to map target of rule for chain `%s'\n", chain);
1879 list_add_tail(&r->list, &c->rules);
1882 set_changed(handle);
1888 match_different(const STRUCT_ENTRY_MATCH *a,
1889 const unsigned char *a_elems,
1890 const unsigned char *b_elems,
1891 unsigned char **maskptr)
1893 const STRUCT_ENTRY_MATCH *b;
1896 /* Offset of b is the same as a. */
1897 b = (void *)b_elems + ((unsigned char *)a - a_elems);
1899 if (a->u.match_size != b->u.match_size)
1902 if (strcmp(a->u.user.name, b->u.user.name) != 0)
1905 *maskptr += ALIGN(sizeof(*a));
1907 for (i = 0; i < a->u.match_size - ALIGN(sizeof(*a)); i++)
1908 if (((a->data[i] ^ b->data[i]) & (*maskptr)[i]) != 0)
1915 target_same(struct rule_head *a, struct rule_head *b,const unsigned char *mask)
1918 STRUCT_ENTRY_TARGET *ta, *tb;
1920 if (a->type != b->type)
1923 ta = GET_TARGET(a->entry);
1924 tb = GET_TARGET(b->entry);
1927 case IPTCC_R_FALLTHROUGH:
1930 return a->jump == b->jump;
1931 case IPTCC_R_STANDARD:
1932 return ((STRUCT_STANDARD_TARGET *)ta)->verdict
1933 == ((STRUCT_STANDARD_TARGET *)tb)->verdict;
1934 case IPTCC_R_MODULE:
1935 if (ta->u.target_size != tb->u.target_size)
1937 if (strcmp(ta->u.user.name, tb->u.user.name) != 0)
1940 for (i = 0; i < ta->u.target_size - sizeof(*ta); i++)
1941 if (((ta->data[i] ^ tb->data[i]) & mask[i]) != 0)
1945 fprintf(stderr, "ERROR: bad type %i\n", a->type);
1950 static unsigned char *
1951 is_same(const STRUCT_ENTRY *a,
1952 const STRUCT_ENTRY *b,
1953 unsigned char *matchmask);
1956 /* find the first rule in `chain' which matches `fw' and remove it unless dry_run is set */
1957 static int delete_entry(const IPT_CHAINLABEL chain, const STRUCT_ENTRY *origfw,
1958 unsigned char *matchmask, struct xtc_handle *handle,
1961 struct chain_head *c;
1962 struct rule_head *r, *i;
1964 iptc_fn = TC_DELETE_ENTRY;
1965 if (!(c = iptcc_find_label(chain, handle))) {
1970 /* Create a rule_head from origfw. */
1971 r = iptcc_alloc_rule(c, origfw->next_offset);
1977 memcpy(r->entry, origfw, origfw->next_offset);
1978 r->counter_map.maptype = COUNTER_MAP_NOMAP;
1979 if (!iptcc_map_target(handle, r)) {
1980 DEBUGP("unable to map target of rule for chain `%s'\n", chain);
1984 /* iptcc_map_target increment target chain references
1985 * since this is a fake rule only used for matching
1986 * the chain references count is decremented again.
1988 if (r->type == IPTCC_R_JUMP
1990 r->jump->references--;
1993 list_for_each_entry(i, &c->rules, list) {
1994 unsigned char *mask;
1996 mask = is_same(r->entry, i->entry, matchmask);
2000 if (!target_same(r, i, mask))
2003 /* if we are just doing a dry run, we simply skip the rest */
2009 /* If we are about to delete the rule that is the
2010 * current iterator, move rule iterator back. next
2011 * pointer will then point to real next node */
2012 if (i == handle->rule_iterator_cur) {
2013 handle->rule_iterator_cur =
2014 list_entry(handle->rule_iterator_cur->list.prev,
2015 struct rule_head, list);
2019 iptcc_delete_rule(i);
2021 set_changed(handle);
2031 /* check whether a specified rule is present */
2032 int TC_CHECK_ENTRY(const IPT_CHAINLABEL chain, const STRUCT_ENTRY *origfw,
2033 unsigned char *matchmask, struct xtc_handle *handle)
2035 /* do a dry-run delete to find out whether a matching rule exists */
2036 return delete_entry(chain, origfw, matchmask, handle, true);
2039 /* Delete the first rule in `chain' which matches `fw'. */
2040 int TC_DELETE_ENTRY(const IPT_CHAINLABEL chain, const STRUCT_ENTRY *origfw,
2041 unsigned char *matchmask, struct xtc_handle *handle)
2043 return delete_entry(chain, origfw, matchmask, handle, false);
2046 /* Delete the rule in position `rulenum' in `chain'. */
2048 TC_DELETE_NUM_ENTRY(const IPT_CHAINLABEL chain,
2049 unsigned int rulenum,
2050 struct xtc_handle *handle)
2052 struct chain_head *c;
2053 struct rule_head *r;
2055 iptc_fn = TC_DELETE_NUM_ENTRY;
2057 if (!(c = iptcc_find_label(chain, handle))) {
2062 if (rulenum >= c->num_rules) {
2067 /* Take advantage of the double linked list if possible. */
2068 if (rulenum + 1 <= c->num_rules/2) {
2069 r = iptcc_get_rule_num(c, rulenum + 1);
2071 r = iptcc_get_rule_num_reverse(c, c->num_rules - rulenum);
2074 /* If we are about to delete the rule that is the current
2075 * iterator, move rule iterator back. next pointer will then
2076 * point to real next node */
2077 if (r == handle->rule_iterator_cur) {
2078 handle->rule_iterator_cur =
2079 list_entry(handle->rule_iterator_cur->list.prev,
2080 struct rule_head, list);
2084 iptcc_delete_rule(r);
2086 set_changed(handle);
2091 /* Flushes the entries in the given chain (ie. empties chain). */
2093 TC_FLUSH_ENTRIES(const IPT_CHAINLABEL chain, struct xtc_handle *handle)
2095 struct chain_head *c;
2096 struct rule_head *r, *tmp;
2098 iptc_fn = TC_FLUSH_ENTRIES;
2099 if (!(c = iptcc_find_label(chain, handle))) {
2104 list_for_each_entry_safe(r, tmp, &c->rules, list) {
2105 iptcc_delete_rule(r);
2110 set_changed(handle);
2115 /* Zeroes the counters in a chain. */
2117 TC_ZERO_ENTRIES(const IPT_CHAINLABEL chain, struct xtc_handle *handle)
2119 struct chain_head *c;
2120 struct rule_head *r;
2122 iptc_fn = TC_ZERO_ENTRIES;
2123 if (!(c = iptcc_find_label(chain, handle))) {
2128 if (c->counter_map.maptype == COUNTER_MAP_NORMAL_MAP)
2129 c->counter_map.maptype = COUNTER_MAP_ZEROED;
2131 list_for_each_entry(r, &c->rules, list) {
2132 if (r->counter_map.maptype == COUNTER_MAP_NORMAL_MAP)
2133 r->counter_map.maptype = COUNTER_MAP_ZEROED;
2136 set_changed(handle);
2142 TC_READ_COUNTER(const IPT_CHAINLABEL chain,
2143 unsigned int rulenum,
2144 struct xtc_handle *handle)
2146 struct chain_head *c;
2147 struct rule_head *r;
2149 iptc_fn = TC_READ_COUNTER;
2152 if (!(c = iptcc_find_label(chain, handle))) {
2157 if (!(r = iptcc_get_rule_num(c, rulenum))) {
2162 return &r->entry[0].counters;
2166 TC_ZERO_COUNTER(const IPT_CHAINLABEL chain,
2167 unsigned int rulenum,
2168 struct xtc_handle *handle)
2170 struct chain_head *c;
2171 struct rule_head *r;
2173 iptc_fn = TC_ZERO_COUNTER;
2176 if (!(c = iptcc_find_label(chain, handle))) {
2181 if (!(r = iptcc_get_rule_num(c, rulenum))) {
2186 if (r->counter_map.maptype == COUNTER_MAP_NORMAL_MAP)
2187 r->counter_map.maptype = COUNTER_MAP_ZEROED;
2189 set_changed(handle);
2195 TC_SET_COUNTER(const IPT_CHAINLABEL chain,
2196 unsigned int rulenum,
2197 STRUCT_COUNTERS *counters,
2198 struct xtc_handle *handle)
2200 struct chain_head *c;
2201 struct rule_head *r;
2204 iptc_fn = TC_SET_COUNTER;
2207 if (!(c = iptcc_find_label(chain, handle))) {
2212 if (!(r = iptcc_get_rule_num(c, rulenum))) {
2218 r->counter_map.maptype = COUNTER_MAP_SET;
2220 memcpy(&e->counters, counters, sizeof(STRUCT_COUNTERS));
2222 set_changed(handle);
2227 /* Creates a new chain. */
2228 /* To create a chain, create two rules: error node and unconditional
2231 TC_CREATE_CHAIN(const IPT_CHAINLABEL chain, struct xtc_handle *handle)
2233 static struct chain_head *c;
2237 iptc_fn = TC_CREATE_CHAIN;
2239 /* find_label doesn't cover built-in targets: DROP, ACCEPT,
2241 if (iptcc_find_label(chain, handle)
2242 || strcmp(chain, LABEL_DROP) == 0
2243 || strcmp(chain, LABEL_ACCEPT) == 0
2244 || strcmp(chain, LABEL_QUEUE) == 0
2245 || strcmp(chain, LABEL_RETURN) == 0) {
2246 DEBUGP("Chain `%s' already exists\n", chain);
2251 if (strlen(chain)+1 > sizeof(IPT_CHAINLABEL)) {
2252 DEBUGP("Chain name `%s' too long\n", chain);
2257 c = iptcc_alloc_chain_head(chain, 0);
2259 DEBUGP("Cannot allocate memory for chain `%s'\n", chain);
2264 handle->num_chains++; /* New user defined chain */
2266 DEBUGP("Creating chain `%s'\n", chain);
2267 iptc_insert_chain(handle, c); /* Insert sorted */
2269 /* Inserting chains don't change the correctness of the chain
2270 * index (except if its smaller than index[0], but that
2271 * handled by iptc_insert_chain). It only causes longer lists
2272 * in the buckets. Thus, only rebuild chain index when the
2273 * capacity is exceed with CHAIN_INDEX_INSERT_MAX chains.
2275 capacity = handle->chain_index_sz * CHAIN_INDEX_BUCKET_LEN;
2276 exceeded = handle->num_chains - capacity;
2277 if (exceeded > CHAIN_INDEX_INSERT_MAX) {
2278 debug("Capacity(%d) exceeded(%d) rebuild (chains:%d)\n",
2279 capacity, exceeded, handle->num_chains);
2280 iptcc_chain_index_rebuild(handle);
2283 set_changed(handle);
2288 /* Get the number of references to this chain. */
2290 TC_GET_REFERENCES(unsigned int *ref, const IPT_CHAINLABEL chain,
2291 struct xtc_handle *handle)
2293 struct chain_head *c;
2295 iptc_fn = TC_GET_REFERENCES;
2296 if (!(c = iptcc_find_label(chain, handle))) {
2301 *ref = c->references;
2306 /* Deletes a chain. */
2308 TC_DELETE_CHAIN(const IPT_CHAINLABEL chain, struct xtc_handle *handle)
2310 unsigned int references;
2311 struct chain_head *c;
2313 iptc_fn = TC_DELETE_CHAIN;
2315 if (!(c = iptcc_find_label(chain, handle))) {
2316 DEBUGP("cannot find chain `%s'\n", chain);
2321 if (TC_BUILTIN(chain, handle)) {
2322 DEBUGP("cannot remove builtin chain `%s'\n", chain);
2327 if (!TC_GET_REFERENCES(&references, chain, handle)) {
2328 DEBUGP("cannot get references on chain `%s'\n", chain);
2332 if (references > 0) {
2333 DEBUGP("chain `%s' still has references\n", chain);
2339 DEBUGP("chain `%s' is not empty\n", chain);
2344 /* If we are about to delete the chain that is the current
2345 * iterator, move chain iterator forward. */
2346 if (c == handle->chain_iterator_cur)
2347 iptcc_chain_iterator_advance(handle);
2349 handle->num_chains--; /* One user defined chain deleted */
2351 //list_del(&c->list); /* Done in iptcc_chain_index_delete_chain() */
2352 iptcc_chain_index_delete_chain(c, handle);
2355 DEBUGP("chain `%s' deleted\n", chain);
2357 set_changed(handle);
2362 /* Renames a chain. */
2363 int TC_RENAME_CHAIN(const IPT_CHAINLABEL oldname,
2364 const IPT_CHAINLABEL newname,
2365 struct xtc_handle *handle)
2367 struct chain_head *c;
2368 iptc_fn = TC_RENAME_CHAIN;
2370 /* find_label doesn't cover built-in targets: DROP, ACCEPT,
2372 if (iptcc_find_label(newname, handle)
2373 || strcmp(newname, LABEL_DROP) == 0
2374 || strcmp(newname, LABEL_ACCEPT) == 0
2375 || strcmp(newname, LABEL_QUEUE) == 0
2376 || strcmp(newname, LABEL_RETURN) == 0) {
2381 if (!(c = iptcc_find_label(oldname, handle))
2382 || TC_BUILTIN(oldname, handle)) {
2387 if (strlen(newname)+1 > sizeof(IPT_CHAINLABEL)) {
2392 /* This only unlinks "c" from the list, thus no free(c) */
2393 iptcc_chain_index_delete_chain(c, handle);
2395 /* Change the name of the chain */
2396 strncpy(c->name, newname, sizeof(IPT_CHAINLABEL));
2398 /* Insert sorted into to list again */
2399 iptc_insert_chain(handle, c);
2401 set_changed(handle);
2406 /* Sets the policy on a built-in chain. */
2408 TC_SET_POLICY(const IPT_CHAINLABEL chain,
2409 const IPT_CHAINLABEL policy,
2410 STRUCT_COUNTERS *counters,
2411 struct xtc_handle *handle)
2413 struct chain_head *c;
2415 iptc_fn = TC_SET_POLICY;
2417 if (!(c = iptcc_find_label(chain, handle))) {
2418 DEBUGP("cannot find chain `%s'\n", chain);
2423 if (!iptcc_is_builtin(c)) {
2424 DEBUGP("cannot set policy of userdefinedchain `%s'\n", chain);
2429 if (strcmp(policy, LABEL_ACCEPT) == 0)
2430 c->verdict = -NF_ACCEPT - 1;
2431 else if (strcmp(policy, LABEL_DROP) == 0)
2432 c->verdict = -NF_DROP - 1;
2439 /* set byte and packet counters */
2440 memcpy(&c->counters, counters, sizeof(STRUCT_COUNTERS));
2441 c->counter_map.maptype = COUNTER_MAP_SET;
2443 c->counter_map.maptype = COUNTER_MAP_NOMAP;
2446 set_changed(handle);
2451 /* Without this, on gcc 2.7.2.3, we get:
2452 libiptc.c: In function `TC_COMMIT':
2453 libiptc.c:833: fixed or forbidden register was spilled.
2454 This may be due to a compiler bug or to impossible asm
2455 statements or clauses.
2458 subtract_counters(STRUCT_COUNTERS *answer,
2459 const STRUCT_COUNTERS *a,
2460 const STRUCT_COUNTERS *b)
2462 answer->pcnt = a->pcnt - b->pcnt;
2463 answer->bcnt = a->bcnt - b->bcnt;
2467 static void counters_nomap(STRUCT_COUNTERS_INFO *newcounters, unsigned int idx)
2469 newcounters->counters[idx] = ((STRUCT_COUNTERS) { 0, 0});
2470 DEBUGP_C("NOMAP => zero\n");
2473 static void counters_normal_map(STRUCT_COUNTERS_INFO *newcounters,
2474 STRUCT_REPLACE *repl, unsigned int idx,
2475 unsigned int mappos)
2477 /* Original read: X.
2478 * Atomic read on replacement: X + Y.
2479 * Currently in kernel: Z.
2480 * Want in kernel: X + Y + Z.
2482 * => Add in replacement read.
2484 newcounters->counters[idx] = repl->counters[mappos];
2485 DEBUGP_C("NORMAL_MAP => mappos %u \n", mappos);
2488 static void counters_map_zeroed(STRUCT_COUNTERS_INFO *newcounters,
2489 STRUCT_REPLACE *repl, unsigned int idx,
2490 unsigned int mappos, STRUCT_COUNTERS *counters)
2492 /* Original read: X.
2493 * Atomic read on replacement: X + Y.
2494 * Currently in kernel: Z.
2495 * Want in kernel: Y + Z.
2497 * => Add in (replacement read - original read).
2499 subtract_counters(&newcounters->counters[idx],
2500 &repl->counters[mappos],
2502 DEBUGP_C("ZEROED => mappos %u\n", mappos);
2505 static void counters_map_set(STRUCT_COUNTERS_INFO *newcounters,
2506 unsigned int idx, STRUCT_COUNTERS *counters)
2508 /* Want to set counter (iptables-restore) */
2510 memcpy(&newcounters->counters[idx], counters,
2511 sizeof(STRUCT_COUNTERS));
2518 TC_COMMIT(struct xtc_handle *handle)
2520 /* Replace, then map back the counters. */
2521 STRUCT_REPLACE *repl;
2522 STRUCT_COUNTERS_INFO *newcounters;
2523 struct chain_head *c;
2527 unsigned int new_size;
2529 iptc_fn = TC_COMMIT;
2532 /* Don't commit if nothing changed. */
2533 if (!handle->changed)
2536 new_number = iptcc_compile_table_prep(handle, &new_size);
2537 if (new_number < 0) {
2542 repl = malloc(sizeof(*repl) + new_size);
2547 memset(repl, 0, sizeof(*repl) + new_size);
2550 TC_DUMP_ENTRIES(*handle);
2553 counterlen = sizeof(STRUCT_COUNTERS_INFO)
2554 + sizeof(STRUCT_COUNTERS) * new_number;
2556 /* These are the old counters we will get from kernel */
2557 repl->counters = malloc(sizeof(STRUCT_COUNTERS)
2558 * handle->info.num_entries);
2559 if (!repl->counters) {
2563 /* These are the counters we're going to put back, later. */
2564 newcounters = malloc(counterlen);
2567 goto out_free_repl_counters;
2569 memset(newcounters, 0, counterlen);
2571 strcpy(repl->name, handle->info.name);
2572 repl->num_entries = new_number;
2573 repl->size = new_size;
2575 repl->num_counters = handle->info.num_entries;
2576 repl->valid_hooks = handle->info.valid_hooks;
2578 DEBUGP("num_entries=%u, size=%u, num_counters=%u\n",
2579 repl->num_entries, repl->size, repl->num_counters);
2581 ret = iptcc_compile_table(handle, repl);
2584 goto out_free_newcounters;
2590 int fd = open("/tmp/libiptc-so_set_replace.blob",
2593 write(fd, repl, sizeof(*repl) + repl->size);
2599 ret = setsockopt(handle->sockfd, TC_IPPROTO, SO_SET_REPLACE, repl,
2600 sizeof(*repl) + repl->size);
2602 goto out_free_newcounters;
2604 /* Put counters back. */
2605 strcpy(newcounters->name, handle->info.name);
2606 newcounters->num_counters = new_number;
2608 list_for_each_entry(c, &handle->chains, list) {
2609 struct rule_head *r;
2611 /* Builtin chains have their own counters */
2612 if (iptcc_is_builtin(c)) {
2613 DEBUGP("counter for chain-index %u: ", c->foot_index);
2614 switch(c->counter_map.maptype) {
2615 case COUNTER_MAP_NOMAP:
2616 counters_nomap(newcounters, c->foot_index);
2618 case COUNTER_MAP_NORMAL_MAP:
2619 counters_normal_map(newcounters, repl,
2621 c->counter_map.mappos);
2623 case COUNTER_MAP_ZEROED:
2624 counters_map_zeroed(newcounters, repl,
2626 c->counter_map.mappos,
2629 case COUNTER_MAP_SET:
2630 counters_map_set(newcounters, c->foot_index,
2636 list_for_each_entry(r, &c->rules, list) {
2637 DEBUGP("counter for index %u: ", r->index);
2638 switch (r->counter_map.maptype) {
2639 case COUNTER_MAP_NOMAP:
2640 counters_nomap(newcounters, r->index);
2643 case COUNTER_MAP_NORMAL_MAP:
2644 counters_normal_map(newcounters, repl,
2646 r->counter_map.mappos);
2649 case COUNTER_MAP_ZEROED:
2650 counters_map_zeroed(newcounters, repl,
2652 r->counter_map.mappos,
2653 &r->entry->counters);
2656 case COUNTER_MAP_SET:
2657 counters_map_set(newcounters, r->index,
2658 &r->entry->counters);
2666 int fd = open("/tmp/libiptc-so_set_add_counters.blob",
2669 write(fd, newcounters, counterlen);
2675 ret = setsockopt(handle->sockfd, TC_IPPROTO, SO_SET_ADD_COUNTERS,
2676 newcounters, counterlen);
2678 goto out_free_newcounters;
2680 free(repl->counters);
2687 out_free_newcounters:
2689 out_free_repl_counters:
2690 free(repl->counters);
2697 /* Translates errno numbers into more human-readable form than strerror. */
2699 TC_STRERROR(int err)
2702 struct table_struct {
2705 const char *message;
2707 { { TC_INIT, EPERM, "Permission denied (you must be root)" },
2708 { TC_INIT, EINVAL, "Module is wrong version" },
2710 "Table does not exist (do you need to insmod?)" },
2711 { TC_DELETE_CHAIN, ENOTEMPTY, "Chain is not empty" },
2712 { TC_DELETE_CHAIN, EINVAL, "Can't delete built-in chain" },
2713 { TC_DELETE_CHAIN, EMLINK,
2714 "Can't delete chain with references left" },
2715 { TC_CREATE_CHAIN, EEXIST, "Chain already exists" },
2716 { TC_INSERT_ENTRY, E2BIG, "Index of insertion too big" },
2717 { TC_REPLACE_ENTRY, E2BIG, "Index of replacement too big" },
2718 { TC_DELETE_NUM_ENTRY, E2BIG, "Index of deletion too big" },
2719 { TC_READ_COUNTER, E2BIG, "Index of counter too big" },
2720 { TC_ZERO_COUNTER, E2BIG, "Index of counter too big" },
2721 { TC_INSERT_ENTRY, ELOOP, "Loop found in table" },
2722 { TC_INSERT_ENTRY, EINVAL, "Target problem" },
2723 /* ENOENT for DELETE probably means no matching rule */
2724 { TC_DELETE_ENTRY, ENOENT,
2725 "Bad rule (does a matching rule exist in that chain?)" },
2726 { TC_SET_POLICY, ENOENT,
2727 "Bad built-in chain name" },
2728 { TC_SET_POLICY, EINVAL,
2729 "Bad policy name" },
2731 { NULL, 0, "Incompatible with this kernel" },
2732 { NULL, ENOPROTOOPT, "iptables who? (do you need to insmod?)" },
2733 { NULL, ENOSYS, "Will be implemented real soon. I promise ;)" },
2734 { NULL, ENOMEM, "Memory allocation problem" },
2735 { NULL, ENOENT, "No chain/target/match by that name" },
2738 for (i = 0; i < sizeof(table)/sizeof(struct table_struct); i++) {
2739 if ((!table[i].fn || table[i].fn == iptc_fn)
2740 && table[i].err == err)
2741 return table[i].message;
2744 return strerror(err);
2747 const struct xtc_ops TC_OPS = {
2748 .commit = TC_COMMIT,
2750 .builtin = TC_BUILTIN,
2751 .is_chain = TC_IS_CHAIN,
2752 .flush_entries = TC_FLUSH_ENTRIES,
2753 .create_chain = TC_CREATE_CHAIN,
2754 .set_policy = TC_SET_POLICY,
2755 .strerror = TC_STRERROR,