5 * Copyright (C) 2007-2013 Intel Corporation. All rights reserved.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
31 #include <sys/errno.h>
32 #include <sys/socket.h>
35 #include <linux/netfilter_ipv4/ip_tables.h>
38 #include "src/shared/util.h"
41 * Some comments on how the iptables API works (some of them from the
42 * source code from iptables and the kernel):
44 * - valid_hooks: bit indicates valid IDs for hook_entry
45 * - hook_entry[ID] offset to the chain start
46 * - overflows should be end of entry chains, and uncodintional policy nodes.
47 * - policy entry: last entry in a chain
48 * - user chain: end of last builtin + policy entry
49 * - final entry must be error node
50 * - Underflows must be unconditional and use the STANDARD target with
52 * - IPT_SO_GET_INFO and IPT_SO_GET_ENTRIES are used to read a table
53 * - IPT_SO_GET_INFO: struct ipt_getinfo (note the lack of table content)
54 * - IPT_SO_GET_ENTRIES: struct ipt_get_entries (contains only parts of the
55 * table header/meta info. The table is appended after the header. The entries
56 * are of the type struct ipt_entry.
57 * - After the ipt_entry the matches are appended. After the matches
58 * the target is appended.
59 * - ipt_entry->target_offset = Size of ipt_entry + matches
60 * - ipt_entry->next_offset = Size of ipt_entry + matches + target
61 * - IPT_SO_SET_REPLACE is used to write a table (contains the complete
62 * - hook_entry and overflow mark the begining and the end of a chain, e.g
63 * entry hook: pre/in/fwd/out/post -1/0/352/504/-1
64 * underflow: pre/in/fwd/out/post -1/200/352/904/-1
65 * means that INPUT starts at offset 0 and ends at 200 (the start offset to
66 * the last element). FORWARD has one entry starting/ending at 352. The entry
67 * has a size of 152. 352 + 152 = 504 which is the start of the OUTPUT chain
68 * which then ends at 904. PREROUTING and POSTROUTING are invalid hooks in
70 * - 'iptables -t filter -A INPUT -m mark --mark 999 -j LOG'
71 * writing that table looks like this:
73 * filter valid_hooks 0x0000000e num_entries 5 size 856
74 * entry hook: pre/in/fwd/out/post -1/0/376/528/-1
75 * underflow: pre/in/fwd/out/post -1/224/376/528/-1
76 * entry 0x699d30 offset 0 size 224
77 * RULE match 0x699da0 target 0x699dd0
78 * match mark match 0x3e7
79 * target LOG flags 0 level 4
82 * entry 0x699e10 offset 224 size 152
83 * RULE match 0x699e80 target 0x699e80
87 * entry 0x699ea8 offset 376 size 152
88 * RULE match 0x699f18 target 0x699f18
92 * entry 0x699f40 offset 528 size 152
93 * RULE match 0x699fb0 target 0x699fb0
97 * entry 0x699fd8 offset 680 size 176
98 * USER CHAIN (ERROR) match 0x69a048 target 0x69a048
100 * Reading the filter table looks like this:
102 * filter valid_hooks 0x0000000e num_entries 5 size 856
103 * entry hook: pre/in/fwd/out/post -1/0/376/528/-1
104 * underflow: pre/in/fwd/out/post -1/224/376/528/-1
105 * entry 0x25fec28 offset 0 size 224
106 * CHAIN (INPUT) match 0x25fec98 target 0x25fecc8
107 * match mark match 0x3e7
108 * target LOG flags 0 level 4
109 * src 0.0.0.0/0.0.0.0
110 * dst 0.0.0.0/0.0.0.0
111 * entry 0x25fed08 offset 224 size 152
112 * RULE match 0x25fed78 target 0x25fed78
114 * src 0.0.0.0/0.0.0.0
115 * dst 0.0.0.0/0.0.0.0
116 * entry 0x25feda0 offset 376 size 152
117 * CHAIN (FORWARD) match 0x25fee10 target 0x25fee10
119 * src 0.0.0.0/0.0.0.0
120 * dst 0.0.0.0/0.0.0.0
121 * entry 0x25fee38 offset 528 size 152
122 * CHAIN (OUTPUT) match 0x25feea8 target 0x25feea8
124 * src 0.0.0.0/0.0.0.0
125 * dst 0.0.0.0/0.0.0.0
126 * entry 0x25feed0 offset 680 size 176
130 static const char *hooknames[] = {
131 [NF_IP_PRE_ROUTING] = "PREROUTING",
132 [NF_IP_LOCAL_IN] = "INPUT",
133 [NF_IP_FORWARD] = "FORWARD",
134 [NF_IP_LOCAL_OUT] = "OUTPUT",
135 [NF_IP_POST_ROUTING] = "POSTROUTING",
138 #define LABEL_ACCEPT "ACCEPT"
139 #define LABEL_DROP "DROP"
140 #define LABEL_QUEUE "QUEUE"
141 #define LABEL_RETURN "RETURN"
143 #define XT_OPTION_OFFSET_SCALE 256
145 #define MIN_ALIGN (__alignof__(struct ipt_entry))
147 #define ALIGN(s) (((s) + ((MIN_ALIGN)-1)) & ~((MIN_ALIGN)-1))
149 struct error_target {
150 struct xt_entry_target t;
151 char error[IPT_TABLE_MAXNAMELEN];
154 struct connman_iptables_entry {
158 struct ipt_entry *entry;
161 struct connman_iptables {
165 struct ipt_getinfo *info;
166 struct ipt_get_entries *blob_entries;
168 unsigned int num_entries;
169 unsigned int old_entries;
172 unsigned int underflow[NF_INET_NUMHOOKS];
173 unsigned int hook_entry[NF_INET_NUMHOOKS];
178 static GHashTable *table_hash = NULL;
179 static bool debug_enabled = false;
181 typedef int (*iterate_entries_cb_t)(struct ipt_entry *entry, int builtin,
182 unsigned int hook, size_t size,
183 unsigned int offset, void *user_data);
185 static unsigned int next_hook_entry_index(unsigned int *valid_hooks)
189 if (*valid_hooks == 0)
190 return NF_INET_NUMHOOKS;
192 h = __builtin_ffs(*valid_hooks) - 1;
193 *valid_hooks ^= (1 << h);
198 static int iterate_entries(struct ipt_entry *entries,
199 unsigned int valid_hooks,
200 unsigned int *hook_entry,
201 unsigned int *underflow,
202 size_t size, iterate_entries_cb_t cb,
205 unsigned int offset, h, hook;
207 struct ipt_entry *entry;
209 h = next_hook_entry_index(&valid_hooks);
212 for (offset = 0, entry = entries; offset < size;
213 offset += entry->next_offset) {
215 entry = (void *)entries + offset;
218 * Updating builtin, hook and h is very tricky.
220 * - builtin is only set to the current hook number
221 * if the current entry is the hook entry (aka chain
222 * head). And only for builtin chains, never for
224 * - hook is the current hook number. If we
225 * look at user chains it needs to be NF_INET_NETNUMHOOKS.
226 * - h is the next hook entry. Thous we need to be carefully
227 * not to access the table when h is NF_INET_NETNUMHOOKS.
229 if (h < NF_INET_NUMHOOKS && hook_entry[h] == offset) {
234 if (h == NF_INET_NUMHOOKS)
237 if (h < NF_INET_NUMHOOKS && underflow[h] <= offset)
238 h = next_hook_entry_index(&valid_hooks);
240 err = cb(entry, builtin, hook, size, offset, user_data);
248 static int print_entry(struct ipt_entry *entry, int builtin, unsigned int hook,
249 size_t size, unsigned int offset,
252 iterate_entries_cb_t cb = user_data;
254 DBG("entry %p hook %d offset %d size %d", entry, hook,
255 offset, entry->next_offset);
257 return cb(entry, builtin, hook, size, offset, NULL);
260 static int target_to_verdict(const char *target_name)
262 if (!g_strcmp0(target_name, LABEL_ACCEPT))
263 return -NF_ACCEPT - 1;
265 if (!g_strcmp0(target_name, LABEL_DROP))
268 if (!g_strcmp0(target_name, LABEL_QUEUE))
269 return -NF_QUEUE - 1;
271 if (!g_strcmp0(target_name, LABEL_RETURN))
277 static bool is_builtin_target(const char *target_name)
279 if (!g_strcmp0(target_name, LABEL_ACCEPT) ||
280 !g_strcmp0(target_name, LABEL_DROP) ||
281 !g_strcmp0(target_name, LABEL_QUEUE) ||
282 !g_strcmp0(target_name, LABEL_RETURN))
288 static bool is_jump(struct connman_iptables_entry *e)
290 struct xt_entry_target *target;
292 target = ipt_get_target(e->entry);
294 if (!g_strcmp0(target->u.user.name, IPT_STANDARD_TARGET)) {
295 struct xt_standard_target *t;
297 t = (struct xt_standard_target *)target;
299 switch (t->verdict) {
315 static bool is_fallthrough(struct connman_iptables_entry *e)
317 struct xt_entry_target *target;
319 target = ipt_get_target(e->entry);
320 if (!g_strcmp0(target->u.user.name, IPT_STANDARD_TARGET)) {
321 struct xt_standard_target *t;
323 t = (struct xt_standard_target *)target;
330 static bool is_chain(struct connman_iptables *table,
331 struct connman_iptables_entry *e)
333 struct ipt_entry *entry;
334 struct xt_entry_target *target;
340 target = ipt_get_target(entry);
341 if (!g_strcmp0(target->u.user.name, IPT_ERROR_TARGET))
347 static GList *find_chain_head(struct connman_iptables *table,
348 const char *chain_name)
351 struct connman_iptables_entry *head;
352 struct ipt_entry *entry;
353 struct xt_entry_target *target;
356 for (list = table->entries; list; list = list->next) {
361 builtin = head->builtin;
362 if (builtin >= 0 && !g_strcmp0(hooknames[builtin], chain_name))
365 /* User defined chain */
366 target = ipt_get_target(entry);
367 if (!g_strcmp0(target->u.user.name, IPT_ERROR_TARGET) &&
368 !g_strcmp0((char *)target->data, chain_name))
375 static GList *find_chain_tail(struct connman_iptables *table,
376 const char *chain_name)
378 struct connman_iptables_entry *tail;
379 GList *chain_head, *list;
381 chain_head = find_chain_head(table, chain_name);
385 /* Then we look for the next chain */
386 for (list = chain_head->next; list; list = list->next) {
389 if (is_chain(table, tail))
393 /* Nothing found, we return the table end */
394 return g_list_last(table->entries);
398 static void update_offsets(struct connman_iptables *table)
401 struct connman_iptables_entry *entry, *prev_entry;
403 for (list = table->entries; list; list = list->next) {
406 if (list == table->entries) {
413 prev_entry = prev->data;
415 entry->offset = prev_entry->offset +
416 prev_entry->entry->next_offset;
420 static void update_targets_reference(struct connman_iptables *table,
421 struct connman_iptables_entry *entry_before,
422 struct connman_iptables_entry *modified_entry,
425 struct connman_iptables_entry *tmp;
426 struct xt_standard_target *t;
430 offset = modified_entry->entry->next_offset;
432 for (list = table->entries; list; list = list->next) {
438 t = (struct xt_standard_target *)ipt_get_target(tmp->entry);
441 if (t->verdict >= entry_before->offset)
442 t->verdict -= offset;
444 if (t->verdict > entry_before->offset)
445 t->verdict += offset;
449 if (is_fallthrough(modified_entry)) {
450 t = (struct xt_standard_target *)
451 ipt_get_target(modified_entry->entry);
453 t->verdict = entry_before->offset +
454 modified_entry->entry->target_offset +
455 ALIGN(sizeof(struct xt_standard_target));
456 t->target.u.target_size =
457 ALIGN(sizeof(struct xt_standard_target));
461 static int iptables_add_entry(struct connman_iptables *table,
462 struct ipt_entry *entry, GList *before,
465 struct connman_iptables_entry *e, *entry_before;
470 e = g_try_malloc0(sizeof(struct connman_iptables_entry));
475 e->builtin = builtin;
477 table->entries = g_list_insert_before(table->entries, before, e);
478 table->num_entries++;
479 table->size += entry->next_offset;
482 e->offset = table->size - entry->next_offset;
487 entry_before = before->data;
490 * We've just appended/insterted a new entry. All references
491 * should be bumped accordingly.
493 update_targets_reference(table, entry_before, e, false);
495 update_offsets(table);
500 static int remove_table_entry(struct connman_iptables *table,
501 struct connman_iptables_entry *entry)
505 table->num_entries--;
506 table->size -= entry->entry->next_offset;
507 removed = entry->entry->next_offset;
509 table->entries = g_list_remove(table->entries, entry);
511 g_free(entry->entry);
517 static void delete_update_hooks(struct connman_iptables *table,
518 int builtin, GList *chain_head,
521 struct connman_iptables_entry *e;
524 e = chain_head->data;
525 e->builtin = builtin;
527 table->underflow[builtin] -= removed;
529 for (list = chain_head->next; list; list = list->next) {
535 table->hook_entry[e->builtin] -= removed;
536 table->underflow[e->builtin] -= removed;
540 static int iptables_flush_chain(struct connman_iptables *table,
543 GList *chain_head, *chain_tail, *list, *next;
544 struct connman_iptables_entry *entry;
545 int builtin, removed = 0;
547 DBG("table %s chain %s", table->name, name);
549 chain_head = find_chain_head(table, name);
553 chain_tail = find_chain_tail(table, name);
557 entry = chain_head->data;
558 builtin = entry->builtin;
563 list = chain_head->next;
565 if (list == chain_tail->prev)
568 while (list != chain_tail->prev) {
570 next = g_list_next(list);
572 removed += remove_table_entry(table, entry);
578 delete_update_hooks(table, builtin, chain_tail->prev, removed);
580 update_offsets(table);
585 static int iptables_add_chain(struct connman_iptables *table,
589 struct ipt_entry *entry_head;
590 struct ipt_entry *entry_return;
591 struct error_target *error;
592 struct ipt_standard_target *standard;
593 u_int16_t entry_head_size, entry_return_size;
595 DBG("table %s chain %s", table->name, name);
597 last = g_list_last(table->entries);
600 * An empty chain is composed of:
601 * - A head entry, with no match and an error target.
602 * The error target data is the chain name.
603 * - A tail entry, with no match and a standard target.
604 * The standard target verdict is XT_RETURN (return to the
609 entry_head_size = ALIGN(sizeof(struct ipt_entry)) +
610 ALIGN(sizeof(struct error_target));
611 entry_head = g_try_malloc0(entry_head_size);
615 entry_head->target_offset = ALIGN(sizeof(struct ipt_entry));
616 entry_head->next_offset = entry_head_size;
618 error = (struct error_target *) entry_head->elems;
619 g_stpcpy(error->t.u.user.name, IPT_ERROR_TARGET);
620 error->t.u.user.target_size = ALIGN(sizeof(struct error_target));
621 g_stpcpy(error->error, name);
623 if (iptables_add_entry(table, entry_head, last, -1) < 0)
627 entry_return_size = ALIGN(sizeof(struct ipt_entry))+
628 ALIGN(sizeof(struct ipt_standard_target));
629 entry_return = g_try_malloc0(entry_return_size);
633 entry_return->target_offset = ALIGN(sizeof(struct ipt_entry));
634 entry_return->next_offset = entry_return_size;
636 standard = (struct ipt_standard_target *) entry_return->elems;
637 standard->target.u.user.target_size =
638 ALIGN(sizeof(struct ipt_standard_target));
639 standard->verdict = XT_RETURN;
641 if (iptables_add_entry(table, entry_return, last, -1) < 0)
647 g_free(entry_return);
654 static int iptables_delete_chain(struct connman_iptables *table,
657 struct connman_iptables_entry *entry;
658 GList *chain_head, *chain_tail;
660 DBG("table %s chain %s", table->name, name);
662 chain_head = find_chain_head(table, name);
666 entry = chain_head->data;
668 /* We cannot remove builtin chain */
669 if (entry->builtin >= 0)
672 chain_tail = find_chain_tail(table, name);
676 /* Chain must be flushed */
677 if (chain_head->next != chain_tail->prev)
680 remove_table_entry(table, entry);
682 entry = chain_tail->prev->data;
683 remove_table_entry(table, entry);
685 update_offsets(table);
690 static struct ipt_entry *new_rule(struct ipt_ip *ip,
691 const char *target_name, struct xtables_target *xt_t,
692 struct xtables_rule_match *xt_rm)
694 struct xtables_rule_match *tmp_xt_rm;
695 struct ipt_entry *new_entry;
696 size_t match_size, target_size;
699 for (tmp_xt_rm = xt_rm; tmp_xt_rm; tmp_xt_rm = tmp_xt_rm->next)
700 match_size += tmp_xt_rm->match->m->u.match_size;
703 target_size = xt_t->t->u.target_size;
705 target_size = ALIGN(sizeof(struct xt_standard_target));
707 new_entry = g_try_malloc0(ALIGN(sizeof(struct ipt_entry)) +
708 target_size + match_size);
712 memcpy(&new_entry->ip, ip, sizeof(struct ipt_ip));
714 new_entry->target_offset = ALIGN(sizeof(struct ipt_entry)) +
716 new_entry->next_offset = ALIGN(sizeof(struct ipt_entry)) +
717 target_size + match_size;
720 for (tmp_xt_rm = xt_rm; tmp_xt_rm;
721 tmp_xt_rm = tmp_xt_rm->next) {
722 memcpy(new_entry->elems + match_size, tmp_xt_rm->match->m,
723 tmp_xt_rm->match->m->u.match_size);
724 match_size += tmp_xt_rm->match->m->u.match_size;
728 struct xt_entry_target *entry_target;
730 entry_target = ipt_get_target(new_entry);
731 memcpy(entry_target, xt_t->t, target_size);
737 static void update_hooks(struct connman_iptables *table, GList *chain_head,
738 struct ipt_entry *entry)
741 struct connman_iptables_entry *head, *e;
747 head = chain_head->data;
749 builtin = head->builtin;
753 table->underflow[builtin] += entry->next_offset;
755 for (list = chain_head->next; list; list = list->next) {
758 builtin = e->builtin;
762 table->hook_entry[builtin] += entry->next_offset;
763 table->underflow[builtin] += entry->next_offset;
767 static struct ipt_entry *prepare_rule_inclusion(struct connman_iptables *table,
768 struct ipt_ip *ip, const char *chain_name,
769 const char *target_name,
770 struct xtables_target *xt_t,
771 int *builtin, struct xtables_rule_match *xt_rm,
774 GList *chain_tail, *chain_head;
775 struct ipt_entry *new_entry;
776 struct connman_iptables_entry *head;
778 chain_head = find_chain_head(table, chain_name);
782 chain_tail = find_chain_tail(table, chain_name);
786 new_entry = new_rule(ip, target_name, xt_t, xt_rm);
790 update_hooks(table, chain_head, new_entry);
793 * If the chain is builtin, and does not have any rule,
794 * then the one that we're inserting is becoming the head
795 * and thus needs the builtin flag.
797 head = chain_head->data;
798 if (head->builtin < 0)
800 else if (insert || chain_head == chain_tail->prev) {
801 *builtin = head->builtin;
808 static int iptables_append_rule(struct connman_iptables *table,
809 struct ipt_ip *ip, const char *chain_name,
810 const char *target_name,
811 struct xtables_target *xt_t,
812 struct xtables_rule_match *xt_rm)
814 struct ipt_entry *new_entry;
815 int builtin = -1, ret;
818 DBG("table %s chain %s", table->name, chain_name);
820 chain_tail = find_chain_tail(table, chain_name);
824 new_entry = prepare_rule_inclusion(table, ip, chain_name,
825 target_name, xt_t, &builtin, xt_rm, false);
829 ret = iptables_add_entry(table, new_entry, chain_tail->prev, builtin);
836 static int iptables_insert_rule(struct connman_iptables *table,
837 struct ipt_ip *ip, const char *chain_name,
838 const char *target_name,
839 struct xtables_target *xt_t,
840 struct xtables_rule_match *xt_rm)
842 struct ipt_entry *new_entry;
843 int builtin = -1, ret;
846 DBG("table %s chain %s", table->name, chain_name);
848 chain_head = find_chain_head(table, chain_name);
852 new_entry = prepare_rule_inclusion(table, ip, chain_name,
853 target_name, xt_t, &builtin, xt_rm, true);
858 chain_head = chain_head->next;
860 ret = iptables_add_entry(table, new_entry, chain_head, builtin);
867 static bool is_same_ipt_entry(struct ipt_entry *i_e1,
868 struct ipt_entry *i_e2)
870 if (memcmp(&i_e1->ip, &i_e2->ip, sizeof(struct ipt_ip)) != 0)
873 if (i_e1->target_offset != i_e2->target_offset)
876 if (i_e1->next_offset != i_e2->next_offset)
882 static bool is_same_target(struct xt_entry_target *xt_e_t1,
883 struct xt_entry_target *xt_e_t2)
887 if (!xt_e_t1 || !xt_e_t2)
890 if (g_strcmp0(xt_e_t1->u.user.name, "") == 0 &&
891 g_strcmp0(xt_e_t2->u.user.name, "") == 0) {
894 } else if (g_strcmp0(xt_e_t1->u.user.name, IPT_STANDARD_TARGET) == 0) {
895 struct xt_standard_target *xt_s_t1;
896 struct xt_standard_target *xt_s_t2;
898 xt_s_t1 = (struct xt_standard_target *) xt_e_t1;
899 xt_s_t2 = (struct xt_standard_target *) xt_e_t2;
901 if (xt_s_t1->verdict != xt_s_t2->verdict)
904 if (xt_e_t1->u.target_size != xt_e_t2->u.target_size)
907 if (g_strcmp0(xt_e_t1->u.user.name, xt_e_t2->u.user.name) != 0)
910 for (i = 0; i < xt_e_t1->u.target_size -
911 sizeof(struct xt_standard_target); i++) {
912 if ((xt_e_t1->data[i] ^ xt_e_t2->data[i]) != 0)
920 static bool is_same_match(struct xt_entry_match *xt_e_m1,
921 struct xt_entry_match *xt_e_m2)
925 if (!xt_e_m1 || !xt_e_m2)
928 if (xt_e_m1->u.match_size != xt_e_m2->u.match_size)
931 if (xt_e_m1->u.user.revision != xt_e_m2->u.user.revision)
934 if (g_strcmp0(xt_e_m1->u.user.name, xt_e_m2->u.user.name) != 0)
937 for (i = 0; i < xt_e_m1->u.match_size - sizeof(struct xt_entry_match);
939 if ((xt_e_m1->data[i] ^ xt_e_m2->data[i]) != 0)
946 static GList *find_existing_rule(struct connman_iptables *table,
947 struct ipt_ip *ip, const char *chain_name,
948 const char *target_name,
949 struct xtables_target *xt_t,
951 struct xtables_rule_match *xt_rm)
953 GList *chain_tail, *chain_head, *list;
954 struct xt_entry_target *xt_e_t = NULL;
955 struct xt_entry_match *xt_e_m = NULL;
956 struct connman_iptables_entry *entry;
957 struct ipt_entry *entry_test;
960 chain_head = find_chain_head(table, chain_name);
964 chain_tail = find_chain_tail(table, chain_name);
968 if (!xt_t && !matches)
971 entry_test = new_rule(ip, target_name, xt_t, xt_rm);
976 xt_e_t = ipt_get_target(entry_test);
978 xt_e_m = (struct xt_entry_match *)entry_test->elems;
980 entry = chain_head->data;
981 builtin = entry->builtin;
986 list = chain_head->next;
988 for (; list != chain_tail->prev; list = list->next) {
989 struct connman_iptables_entry *tmp;
990 struct ipt_entry *tmp_e;
995 if (!is_same_ipt_entry(entry_test, tmp_e))
999 struct xt_entry_target *tmp_xt_e_t;
1001 tmp_xt_e_t = ipt_get_target(tmp_e);
1003 if (!is_same_target(tmp_xt_e_t, xt_e_t))
1008 struct xt_entry_match *tmp_xt_e_m;
1010 tmp_xt_e_m = (struct xt_entry_match *)tmp_e->elems;
1012 if (!is_same_match(tmp_xt_e_m, xt_e_m))
1021 if (list != chain_tail->prev)
1027 static int iptables_delete_rule(struct connman_iptables *table,
1028 struct ipt_ip *ip, const char *chain_name,
1029 const char *target_name,
1030 struct xtables_target *xt_t,
1032 struct xtables_rule_match *xt_rm)
1034 struct connman_iptables_entry *entry;
1035 GList *chain_head, *chain_tail, *list;
1036 int builtin, removed;
1039 DBG("table %s chain %s", table->name, chain_name);
1043 chain_head = find_chain_head(table, chain_name);
1047 chain_tail = find_chain_tail(table, chain_name);
1051 list = find_existing_rule(table, ip, chain_name, target_name,
1052 xt_t, matches, xt_rm);
1056 entry = chain_head->data;
1057 builtin = entry->builtin;
1059 if (builtin >= 0 && list == chain_head) {
1061 * We are about to remove the first rule in the
1062 * chain. In this case we need to store the builtin
1063 * value to the new chain_head.
1065 * Note, for builtin chains, chain_head->next is
1066 * always valid. A builtin chain has always a policy
1069 chain_head = chain_head->next;
1071 entry = chain_head->data;
1072 entry->builtin = builtin;
1079 /* We have deleted a rule,
1080 * all references should be bumped accordingly */
1082 update_targets_reference(table, list->next->data,
1085 removed += remove_table_entry(table, entry);
1088 delete_update_hooks(table, builtin, chain_head, removed);
1090 update_offsets(table);
1095 static int iptables_change_policy(struct connman_iptables *table,
1096 const char *chain_name, const char *policy)
1098 GList *chain_head, *chain_tail;
1099 struct connman_iptables_entry *entry;
1100 struct xt_entry_target *target;
1101 struct xt_standard_target *t;
1104 DBG("table %s chain %s policy %s", table->name, chain_name, policy);
1106 verdict = target_to_verdict(policy);
1108 case -NF_ACCEPT - 1:
1115 chain_head = find_chain_head(table, chain_name);
1119 entry = chain_head->data;
1120 if (entry->builtin < 0)
1123 chain_tail = find_chain_tail(table, chain_name);
1127 entry = chain_tail->prev->data;
1128 target = ipt_get_target(entry->entry);
1130 t = (struct xt_standard_target *)target;
1131 t->verdict = verdict;
1136 static struct ipt_replace *iptables_blob(struct connman_iptables *table)
1138 struct ipt_replace *r;
1140 struct connman_iptables_entry *e;
1141 unsigned char *entry_index;
1143 r = g_try_malloc0(sizeof(struct ipt_replace) + table->size);
1147 memset(r, 0, sizeof(*r) + table->size);
1149 r->counters = g_try_malloc0(sizeof(struct xt_counters)
1150 * table->old_entries);
1156 g_stpcpy(r->name, table->info->name);
1157 r->num_entries = table->num_entries;
1158 r->size = table->size;
1160 r->num_counters = table->old_entries;
1161 r->valid_hooks = table->info->valid_hooks;
1163 memcpy(r->hook_entry, table->hook_entry, sizeof(table->hook_entry));
1164 memcpy(r->underflow, table->underflow, sizeof(table->underflow));
1166 entry_index = (unsigned char *)r->entries;
1167 for (list = table->entries; list; list = list->next) {
1170 memcpy(entry_index, e->entry, e->entry->next_offset);
1171 entry_index += e->entry->next_offset;
1177 static void dump_ip(struct ipt_entry *entry)
1179 struct ipt_ip *ip = &entry->ip;
1180 char ip_string[INET6_ADDRSTRLEN];
1181 char ip_mask[INET6_ADDRSTRLEN];
1183 if (strlen(ip->iniface))
1184 DBG("\tin %s", ip->iniface);
1186 if (strlen(ip->outiface))
1187 DBG("\tout %s", ip->outiface);
1189 if (inet_ntop(AF_INET, &ip->src, ip_string, INET6_ADDRSTRLEN) &&
1190 inet_ntop(AF_INET, &ip->smsk, ip_mask,
1192 DBG("\tsrc %s/%s", ip_string, ip_mask);
1194 if (inet_ntop(AF_INET, &ip->dst, ip_string, INET6_ADDRSTRLEN) &&
1195 inet_ntop(AF_INET, &ip->dmsk, ip_mask,
1197 DBG("\tdst %s/%s", ip_string, ip_mask);
1200 static void dump_target(struct ipt_entry *entry)
1203 struct xtables_target *xt_t;
1204 struct xt_entry_target *target;
1206 target = ipt_get_target(entry);
1208 if (!g_strcmp0(target->u.user.name, IPT_STANDARD_TARGET)) {
1209 struct xt_standard_target *t;
1211 t = (struct xt_standard_target *)target;
1213 switch (t->verdict) {
1215 DBG("\ttarget RETURN");
1218 case -NF_ACCEPT - 1:
1219 DBG("\ttarget ACCEPT");
1223 DBG("\ttarget DROP");
1227 DBG("\ttarget QUEUE");
1231 DBG("\ttarget STOP");
1235 DBG("\tJUMP %u", t->verdict);
1239 xt_t = xtables_find_target(IPT_STANDARD_TARGET,
1240 XTF_LOAD_MUST_SUCCEED);
1243 xt_t->print(NULL, target, 1);
1245 xt_t = xtables_find_target(target->u.user.name, XTF_TRY_LOAD);
1247 DBG("\ttarget %s", target->u.user.name);
1253 xt_t->print(NULL, target, 1);
1257 if (xt_t == xt_t->next)
1261 static void dump_match(struct ipt_entry *entry)
1263 struct xtables_match *xt_m;
1264 struct xt_entry_match *match;
1266 if (entry->elems == (unsigned char *)entry + entry->target_offset)
1269 match = (struct xt_entry_match *) entry->elems;
1271 if (!strlen(match->u.user.name))
1274 xt_m = xtables_find_match(match->u.user.name, XTF_TRY_LOAD, NULL);
1280 xt_m->print(NULL, match, 1);
1284 if (xt_m == xt_m->next)
1288 DBG("\tmatch %s", match->u.user.name);
1292 static int dump_entry(struct ipt_entry *entry, int builtin,
1293 unsigned int hook, size_t size, unsigned int offset,
1296 struct xt_entry_target *target;
1298 target = ipt_get_target(entry);
1300 if (offset + entry->next_offset == size) {
1301 DBG("\tEnd of CHAIN");
1305 if (!g_strcmp0(target->u.user.name, IPT_ERROR_TARGET)) {
1306 DBG("\tUSER CHAIN (%s) match %p target %p",
1307 target->data, entry->elems,
1308 (char *)entry + entry->target_offset);
1311 } else if (builtin >= 0) {
1312 DBG("\tCHAIN (%s) match %p target %p",
1313 hooknames[builtin], entry->elems,
1314 (char *)entry + entry->target_offset);
1316 DBG("\tRULE match %p target %p",
1318 (char *)entry + entry->target_offset);
1328 static void dump_table(struct connman_iptables *table)
1330 DBG("%s valid_hooks=0x%08x, num_entries=%u, size=%u",
1332 table->info->valid_hooks, table->info->num_entries,
1335 DBG("entry hook: pre/in/fwd/out/post %d/%d/%d/%d/%d",
1336 table->info->hook_entry[NF_IP_PRE_ROUTING],
1337 table->info->hook_entry[NF_IP_LOCAL_IN],
1338 table->info->hook_entry[NF_IP_FORWARD],
1339 table->info->hook_entry[NF_IP_LOCAL_OUT],
1340 table->info->hook_entry[NF_IP_POST_ROUTING]);
1341 DBG("underflow: pre/in/fwd/out/post %d/%d/%d/%d/%d",
1342 table->info->underflow[NF_IP_PRE_ROUTING],
1343 table->info->underflow[NF_IP_LOCAL_IN],
1344 table->info->underflow[NF_IP_FORWARD],
1345 table->info->underflow[NF_IP_LOCAL_OUT],
1346 table->info->underflow[NF_IP_POST_ROUTING]);
1348 iterate_entries(table->blob_entries->entrytable,
1349 table->info->valid_hooks,
1350 table->info->hook_entry,
1351 table->info->underflow,
1352 table->blob_entries->size,
1353 print_entry, dump_entry);
1356 static void dump_ipt_replace(struct ipt_replace *repl)
1358 DBG("%s valid_hooks 0x%08x num_entries %u size %u",
1359 repl->name, repl->valid_hooks, repl->num_entries,
1362 DBG("entry hook: pre/in/fwd/out/post %d/%d/%d/%d/%d",
1363 repl->hook_entry[NF_IP_PRE_ROUTING],
1364 repl->hook_entry[NF_IP_LOCAL_IN],
1365 repl->hook_entry[NF_IP_FORWARD],
1366 repl->hook_entry[NF_IP_LOCAL_OUT],
1367 repl->hook_entry[NF_IP_POST_ROUTING]);
1368 DBG("underflow: pre/in/fwd/out/post %d/%d/%d/%d/%d",
1369 repl->underflow[NF_IP_PRE_ROUTING],
1370 repl->underflow[NF_IP_LOCAL_IN],
1371 repl->underflow[NF_IP_FORWARD],
1372 repl->underflow[NF_IP_LOCAL_OUT],
1373 repl->underflow[NF_IP_POST_ROUTING]);
1375 iterate_entries(repl->entries, repl->valid_hooks,
1376 repl->hook_entry, repl->underflow,
1377 repl->size, print_entry, dump_entry);
1380 static int iptables_get_entries(struct connman_iptables *table)
1382 socklen_t entry_size;
1385 entry_size = sizeof(struct ipt_get_entries) + table->info->size;
1387 err = getsockopt(table->ipt_sock, IPPROTO_IP, IPT_SO_GET_ENTRIES,
1388 table->blob_entries, &entry_size);
1395 static int iptables_replace(struct connman_iptables *table,
1396 struct ipt_replace *r)
1400 err = setsockopt(table->ipt_sock, IPPROTO_IP, IPT_SO_SET_REPLACE, r,
1401 sizeof(*r) + r->size);
1408 static int add_entry(struct ipt_entry *entry, int builtin, unsigned int hook,
1409 size_t size, unsigned offset, void *user_data)
1411 struct connman_iptables *table = user_data;
1412 struct ipt_entry *new_entry;
1414 new_entry = g_try_malloc0(entry->next_offset);
1418 memcpy(new_entry, entry, entry->next_offset);
1420 return iptables_add_entry(table, new_entry, NULL, builtin);
1423 static void table_cleanup(struct connman_iptables *table)
1426 struct connman_iptables_entry *entry;
1431 if (table->ipt_sock >= 0)
1432 close(table->ipt_sock);
1434 for (list = table->entries; list; list = list->next) {
1437 g_free(entry->entry);
1441 g_list_free(table->entries);
1442 g_free(table->name);
1443 g_free(table->info);
1444 g_free(table->blob_entries);
1448 static struct connman_iptables *iptables_init(const char *table_name)
1450 struct connman_iptables *table = NULL;
1451 char *module = NULL;
1454 DBG("%s", table_name);
1456 if (xtables_insmod("ip_tables", NULL, TRUE) != 0)
1457 DBG("ip_tables module loading gives error but trying anyway");
1459 module = g_strconcat("iptable_", table_name, NULL);
1463 if (xtables_insmod(module, NULL, TRUE) != 0)
1464 DBG("%s module loading gives error but trying anyway", module);
1468 table = g_try_new0(struct connman_iptables, 1);
1472 table->info = g_try_new0(struct ipt_getinfo, 1);
1476 table->ipt_sock = socket(AF_INET, SOCK_RAW | SOCK_CLOEXEC, IPPROTO_RAW);
1477 if (table->ipt_sock < 0)
1480 s = sizeof(*table->info);
1481 g_stpcpy(table->info->name, table_name);
1482 if (getsockopt(table->ipt_sock, IPPROTO_IP, IPT_SO_GET_INFO,
1483 table->info, &s) < 0) {
1484 connman_error("iptables support missing error %d (%s)", errno,
1489 table->blob_entries = g_try_malloc0(sizeof(struct ipt_get_entries) +
1491 if (!table->blob_entries)
1494 g_stpcpy(table->blob_entries->name, table_name);
1495 table->blob_entries->size = table->info->size;
1497 if (iptables_get_entries(table) < 0)
1500 table->num_entries = 0;
1501 table->old_entries = table->info->num_entries;
1504 memcpy(table->underflow, table->info->underflow,
1505 sizeof(table->info->underflow));
1506 memcpy(table->hook_entry, table->info->hook_entry,
1507 sizeof(table->info->hook_entry));
1509 iterate_entries(table->blob_entries->entrytable,
1510 table->info->valid_hooks, table->info->hook_entry,
1511 table->info->underflow, table->blob_entries->size,
1520 table_cleanup(table);
1525 static struct option iptables_opts[] = {
1526 {.name = "append", .has_arg = 1, .val = 'A'},
1527 {.name = "compare", .has_arg = 1, .val = 'C'},
1528 {.name = "delete", .has_arg = 1, .val = 'D'},
1529 {.name = "flush-chain", .has_arg = 1, .val = 'F'},
1530 {.name = "insert", .has_arg = 1, .val = 'I'},
1531 {.name = "list", .has_arg = 2, .val = 'L'},
1532 {.name = "new-chain", .has_arg = 1, .val = 'N'},
1533 {.name = "policy", .has_arg = 1, .val = 'P'},
1534 {.name = "delete-chain", .has_arg = 1, .val = 'X'},
1535 {.name = "destination", .has_arg = 1, .val = 'd'},
1536 {.name = "in-interface", .has_arg = 1, .val = 'i'},
1537 {.name = "jump", .has_arg = 1, .val = 'j'},
1538 {.name = "match", .has_arg = 1, .val = 'm'},
1539 {.name = "out-interface", .has_arg = 1, .val = 'o'},
1540 {.name = "source", .has_arg = 1, .val = 's'},
1541 {.name = "table", .has_arg = 1, .val = 't'},
1545 struct xtables_globals iptables_globals = {
1547 .opts = iptables_opts,
1548 .orig_opts = iptables_opts,
1551 static struct xtables_target *prepare_target(struct connman_iptables *table,
1552 const char *target_name)
1554 struct xtables_target *xt_t = NULL;
1555 bool is_builtin, is_user_defined;
1556 GList *chain_head = NULL;
1560 is_user_defined = false;
1562 if (is_builtin_target(target_name))
1565 chain_head = find_chain_head(table, target_name);
1566 if (chain_head && chain_head->next)
1567 is_user_defined = true;
1570 if (is_builtin || is_user_defined)
1571 xt_t = xtables_find_target(IPT_STANDARD_TARGET,
1572 XTF_LOAD_MUST_SUCCEED);
1574 xt_t = xtables_find_target(target_name, XTF_TRY_LOAD);
1579 target_size = ALIGN(sizeof(struct ipt_entry_target)) + xt_t->size;
1581 xt_t->t = g_try_malloc0(target_size);
1585 xt_t->t->u.target_size = target_size;
1587 if (is_builtin || is_user_defined) {
1588 struct xt_standard_target *target;
1590 target = (struct xt_standard_target *)(xt_t->t);
1591 g_stpcpy(target->target.u.user.name, IPT_STANDARD_TARGET);
1594 target->verdict = target_to_verdict(target_name);
1595 else if (is_user_defined) {
1596 struct connman_iptables_entry *target_rule;
1598 target_rule = chain_head->next->data;
1599 target->verdict = target_rule->offset;
1602 g_stpcpy(xt_t->t->u.user.name, target_name);
1603 xt_t->t->u.user.revision = xt_t->revision;
1605 xt_t->init(xt_t->t);
1608 if (xt_t->x6_options)
1609 iptables_globals.opts =
1610 xtables_options_xfrm(
1611 iptables_globals.orig_opts,
1612 iptables_globals.opts,
1614 &xt_t->option_offset);
1616 iptables_globals.opts =
1617 xtables_merge_options(
1618 iptables_globals.orig_opts,
1619 iptables_globals.opts,
1621 &xt_t->option_offset);
1623 if (!iptables_globals.opts) {
1631 static struct xtables_match *prepare_matches(struct connman_iptables *table,
1632 struct xtables_rule_match **xt_rm,
1633 const char *match_name)
1635 struct xtables_match *xt_m;
1641 xt_m = xtables_find_match(match_name, XTF_LOAD_MUST_SUCCEED, xt_rm);
1642 match_size = ALIGN(sizeof(struct ipt_entry_match)) + xt_m->size;
1644 xt_m->m = g_try_malloc0(match_size);
1648 xt_m->m->u.match_size = match_size;
1649 g_stpcpy(xt_m->m->u.user.name, xt_m->name);
1650 xt_m->m->u.user.revision = xt_m->revision;
1653 xt_m->init(xt_m->m);
1655 if (xt_m->x6_options)
1656 iptables_globals.opts =
1657 xtables_options_xfrm(
1658 iptables_globals.orig_opts,
1659 iptables_globals.opts,
1661 &xt_m->option_offset);
1663 iptables_globals.opts =
1664 xtables_merge_options(
1665 iptables_globals.orig_opts,
1666 iptables_globals.opts,
1668 &xt_m->option_offset);
1670 if (!iptables_globals.opts) {
1673 if (xt_m == xt_m->next)
1682 static int parse_ip_and_mask(const char *str, struct in_addr *ip,
1683 struct in_addr *mask)
1686 uint32_t prefixlength;
1690 tokens = g_strsplit(str, "/", 2);
1694 if (!inet_pton(AF_INET, tokens[0], ip)) {
1700 prefixlength = strtol(tokens[1], NULL, 10);
1701 if (prefixlength > 31) {
1706 tmp = ~(0xffffffff >> prefixlength);
1711 mask->s_addr = htonl(tmp);
1712 ip->s_addr = ip->s_addr & mask->s_addr;
1720 static struct connman_iptables *get_table(const char *table_name)
1722 struct connman_iptables *table;
1725 table_name = "filter";
1727 table = g_hash_table_lookup(table_hash, table_name);
1731 table = iptables_init(table_name);
1735 table->name = g_strdup(table_name);
1736 g_hash_table_replace(table_hash, table->name, table);
1741 struct parse_context {
1745 struct xtables_target *xt_t;
1747 struct xtables_rule_match *xt_rm;
1750 static int prepare_getopt_args(const char *str, struct parse_context *ctx)
1755 tokens = g_strsplit_set(str, " ", -1);
1757 i = g_strv_length(tokens);
1759 /* Add space for the argv[0] value */
1762 /* Don't forget the last NULL entry */
1763 ctx->argv = g_try_malloc0((ctx->argc + 1) * sizeof(char *));
1770 * getopt_long() jumps over the first token; we need to add some
1771 * random argv[0] entry.
1773 ctx->argv[0] = g_strdup("argh");
1774 for (i = 1; i < ctx->argc; i++)
1775 ctx->argv[i] = tokens[i - 1];
1782 static int parse_xt_modules(int c, bool invert,
1783 struct parse_context *ctx)
1785 struct xtables_match *m;
1786 struct xtables_rule_match *rm;
1788 for (rm = ctx->xt_rm; rm; rm = rm->next) {
1789 if (rm->completed != 0)
1794 if (!m->x6_parse && !m->parse)
1797 if (c < (int) m->option_offset ||
1798 c >= (int) m->option_offset
1799 + XT_OPTION_OFFSET_SCALE)
1802 xtables_option_mpcall(c, ctx->argv, invert, m, NULL);
1808 if (!ctx->xt_t->x6_parse && !ctx->xt_t->parse)
1811 if (c < (int) ctx->xt_t->option_offset ||
1812 c >= (int) ctx->xt_t->option_offset
1813 + XT_OPTION_OFFSET_SCALE)
1816 xtables_option_tpcall(c, ctx->argv, invert, ctx->xt_t, NULL);
1821 static int final_check_xt_modules(struct parse_context *ctx)
1823 struct xtables_rule_match *rm;
1825 for (rm = ctx->xt_rm; rm; rm = rm->next)
1826 xtables_option_mfcall(rm->match);
1829 xtables_option_tfcall(ctx->xt_t);
1834 static int parse_rule_spec(struct connman_iptables *table,
1835 struct parse_context *ctx)
1838 * How the parser works:
1840 * - If getopt finds 's', 'd', 'i', 'o'.
1841 * just extract the information.
1842 * - if '!' is found, set the invert flag to true and
1843 * removes the '!' from the optarg string and jumps
1844 * back to getopt to reparse the current optarg string.
1845 * After reparsing the invert flag is reseted to false.
1846 * - If 'm' or 'j' is found then call either
1847 * prepare_matches() or prepare_target(). Those function
1848 * will modify (extend) the longopts for getopt_long.
1849 * That means getopt will change its matching context according
1850 * the loaded target.
1852 * Here an example with iptables-test
1854 * argv[0] = ./tools/iptables-test
1866 * getopt found 'm' then the optarg is "mark" and optind 7
1867 * The longopts array containts before hitting the `case 'm'`
1869 * val A has_arg 1 name append
1870 * val C has_arg 1 name compare
1871 * val D has_arg 1 name delete
1872 * val F has_arg 1 name flush-chain
1873 * val I has_arg 1 name insert
1874 * val L has_arg 2 name list
1875 * val N has_arg 1 name new-chain
1876 * val P has_arg 1 name policy
1877 * val X has_arg 1 name delete-chain
1878 * val d has_arg 1 name destination
1879 * val i has_arg 1 name in-interface
1880 * val j has_arg 1 name jump
1881 * val m has_arg 1 name match
1882 * val o has_arg 1 name out-interface
1883 * val s has_arg 1 name source
1884 * val t has_arg 1 name table
1886 * After executing the `case 'm'` block longopts is
1888 * val A has_arg 1 name append
1889 * val C has_arg 1 name compare
1890 * val D has_arg 1 name delete
1891 * val F has_arg 1 name flush-chain
1892 * val I has_arg 1 name insert
1893 * val L has_arg 2 name list
1894 * val N has_arg 1 name new-chain
1895 * val P has_arg 1 name policy
1896 * val X has_arg 1 name delete-chain
1897 * val d has_arg 1 name destination
1898 * val i has_arg 1 name in-interface
1899 * val j has_arg 1 name jump
1900 * val m has_arg 1 name match
1901 * val o has_arg 1 name out-interface
1902 * val s has_arg 1 name source
1903 * val t has_arg 1 name table
1904 * val has_arg 1 name mark
1906 * So the 'mark' matcher has added the 'mark' options
1907 * and getopt will then return c '256' optarg "999" optind 9
1908 * And we will hit the 'default' statement which then
1909 * will call the matchers parser (xt_m->parser() or
1910 * xtables_option_mpcall() depending on which version
1911 * of libxtables is found.
1913 struct xtables_match *xt_m;
1914 bool invert = false;
1917 ctx->ip = g_try_new0(struct ipt_ip, 1);
1922 * Tell getopt_long not to generate error messages for unknown
1923 * options and also reset optind back to 0.
1928 while ((c = getopt_long(ctx->argc, ctx->argv,
1930 iptables_globals.opts, NULL)) != -1) {
1933 /* Source specification */
1934 if (!parse_ip_and_mask(optarg,
1940 ctx->ip->invflags |= IPT_INV_SRCIP;
1944 /* Destination specification */
1945 if (!parse_ip_and_mask(optarg,
1951 ctx->ip->invflags |= IPT_INV_DSTIP;
1954 /* In interface specification */
1955 len = strlen(optarg);
1957 if (len + 1 > IFNAMSIZ)
1960 g_stpcpy(ctx->ip->iniface, optarg);
1961 memset(ctx->ip->iniface_mask, 0xff, len + 1);
1964 ctx->ip->invflags |= IPT_INV_VIA_IN;
1968 /* Out interface specification */
1969 len = strlen(optarg);
1971 if (len + 1 > IFNAMSIZ)
1974 g_stpcpy(ctx->ip->outiface, optarg);
1975 memset(ctx->ip->outiface_mask, 0xff, len + 1);
1978 ctx->ip->invflags |= IPT_INV_VIA_OUT;
1983 xt_m = prepare_matches(table, &ctx->xt_rm, optarg);
1988 ctx->xt_m = g_list_append(ctx->xt_m, xt_m);
1993 ctx->xt_t = prepare_target(table, optarg);
2001 if (optarg[0] == '!' && optarg[1] == '\0') {
2004 /* Remove the '!' from the optarg */
2008 * And recall getopt_long without reseting
2016 err = parse_xt_modules(c, invert, ctx);
2026 err = final_check_xt_modules(ctx);
2032 static void reset_xtables(void)
2034 struct xtables_match *xt_m;
2035 struct xtables_target *xt_t;
2038 * As side effect parsing a rule sets some global flags
2039 * which will be evaluated/verified. Let's reset them
2040 * to ensure we can parse more than one rule.
2042 * Clear all flags because the flags are only valid
2045 for (xt_m = xtables_matches; xt_m; xt_m = xt_m->next)
2048 for (xt_t = xtables_targets; xt_t; xt_t = xt_t->next) {
2054 * We need also to free the memory implicitly allocated
2055 * during parsing (see xtables_options_xfrm()).
2056 * Note xt_params is actually iptables_globals.
2058 if (xt_params->opts != xt_params->orig_opts) {
2059 g_free(xt_params->opts);
2060 xt_params->opts = xt_params->orig_opts;
2062 xt_params->option_offset = 0;
2065 static void cleanup_parse_context(struct parse_context *ctx)
2067 struct xtables_rule_match *rm, *tmp;
2070 g_strfreev(ctx->argv);
2073 g_free(ctx->xt_t->t);
2074 ctx->xt_t->t = NULL;
2077 for (list = ctx->xt_m; list; list = list->next) {
2078 struct xtables_match *xt_m = list->data;
2082 if (xt_m != xt_m->next)
2087 g_list_free(ctx->xt_m);
2089 for (tmp = NULL, rm = ctx->xt_rm; rm; rm = rm->next) {
2099 int __connman_iptables_dump(const char *table_name)
2101 struct connman_iptables *table;
2103 DBG("-t %s -L", table_name);
2105 table = get_table(table_name);
2114 int __connman_iptables_new_chain(const char *table_name,
2117 struct connman_iptables *table;
2119 DBG("-t %s -N %s", table_name, chain);
2121 table = get_table(table_name);
2125 return iptables_add_chain(table, chain);
2128 int __connman_iptables_delete_chain(const char *table_name,
2131 struct connman_iptables *table;
2133 DBG("-t %s -X %s", table_name, chain);
2135 table = get_table(table_name);
2139 return iptables_delete_chain(table, chain);
2142 int __connman_iptables_flush_chain(const char *table_name,
2145 struct connman_iptables *table;
2147 DBG("-t %s -F %s", table_name, chain);
2149 table = get_table(table_name);
2153 return iptables_flush_chain(table, chain);
2156 int __connman_iptables_change_policy(const char *table_name,
2160 struct connman_iptables *table;
2162 DBG("-t %s -F %s", table_name, chain);
2164 table = get_table(table_name);
2168 return iptables_change_policy(table, chain, policy);
2171 int __connman_iptables_append(const char *table_name,
2173 const char *rule_spec)
2175 struct connman_iptables *table;
2176 struct parse_context *ctx;
2177 const char *target_name;
2180 ctx = g_try_new0(struct parse_context, 1);
2184 DBG("-t %s -A %s %s", table_name, chain, rule_spec);
2186 err = prepare_getopt_args(rule_spec, ctx);
2190 table = get_table(table_name);
2196 err = parse_rule_spec(table, ctx);
2203 target_name = ctx->xt_t->name;
2205 err = iptables_append_rule(table, ctx->ip, chain,
2206 target_name, ctx->xt_t, ctx->xt_rm);
2208 cleanup_parse_context(ctx);
2214 int __connman_iptables_insert(const char *table_name,
2216 const char *rule_spec)
2218 struct connman_iptables *table;
2219 struct parse_context *ctx;
2220 const char *target_name;
2223 ctx = g_try_new0(struct parse_context, 1);
2227 DBG("-t %s -I %s %s", table_name, chain, rule_spec);
2229 err = prepare_getopt_args(rule_spec, ctx);
2233 table = get_table(table_name);
2239 err = parse_rule_spec(table, ctx);
2246 target_name = ctx->xt_t->name;
2248 err = iptables_insert_rule(table, ctx->ip, chain,
2249 target_name, ctx->xt_t, ctx->xt_rm);
2251 cleanup_parse_context(ctx);
2257 int __connman_iptables_delete(const char *table_name,
2259 const char *rule_spec)
2261 struct connman_iptables *table;
2262 struct parse_context *ctx;
2263 const char *target_name;
2266 ctx = g_try_new0(struct parse_context, 1);
2270 DBG("-t %s -D %s %s", table_name, chain, rule_spec);
2272 err = prepare_getopt_args(rule_spec, ctx);
2276 table = get_table(table_name);
2282 err = parse_rule_spec(table, ctx);
2289 target_name = ctx->xt_t->name;
2291 err = iptables_delete_rule(table, ctx->ip, chain,
2292 target_name, ctx->xt_t, ctx->xt_m,
2295 cleanup_parse_context(ctx);
2301 int __connman_iptables_commit(const char *table_name)
2303 struct connman_iptables *table;
2304 struct ipt_replace *repl;
2307 DBG("%s", table_name);
2309 table = g_hash_table_lookup(table_hash, table_name);
2313 repl = iptables_blob(table);
2316 dump_ipt_replace(repl);
2318 err = iptables_replace(table, repl);
2320 g_free(repl->counters);
2326 g_hash_table_remove(table_hash, table_name);
2331 static void remove_table(gpointer user_data)
2333 struct connman_iptables *table = user_data;
2335 table_cleanup(table);
2338 static int iterate_chains_cb(struct ipt_entry *entry, int builtin,
2339 unsigned int hook, size_t size,
2340 unsigned int offset, void *user_data)
2342 struct cb_data *cbd = user_data;
2343 connman_iptables_iterate_chains_cb_t cb = cbd->cb;
2344 struct xt_entry_target *target;
2346 if (offset + entry->next_offset == size)
2349 target = ipt_get_target(entry);
2351 if (!g_strcmp0(target->u.user.name, IPT_ERROR_TARGET))
2352 (*cb)((const char *)target->data, cbd->user_data);
2353 else if (builtin >= 0)
2354 (*cb)(hooknames[builtin], cbd->user_data);
2359 int __connman_iptables_iterate_chains(const char *table_name,
2360 connman_iptables_iterate_chains_cb_t cb,
2363 struct cb_data *cbd = cb_data_new(cb, user_data);
2364 struct connman_iptables *table;
2366 table = get_table(table_name);
2370 iterate_entries(table->blob_entries->entrytable,
2371 table->info->valid_hooks,
2372 table->info->hook_entry,
2373 table->info->underflow,
2374 table->blob_entries->size,
2375 iterate_chains_cb, cbd);
2382 int __connman_iptables_init(void)
2386 if (getenv("CONNMAN_IPTABLES_DEBUG"))
2387 debug_enabled = true;
2389 table_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
2390 NULL, remove_table);
2392 xtables_init_all(&iptables_globals, NFPROTO_IPV4);
2397 void __connman_iptables_cleanup(void)
2401 g_hash_table_destroy(table_hash);